summaryrefslogtreecommitdiffstats
path: root/examples/iconview/simple_dd/main.cpp
blob: 16d3b53c595b23d93174cfc3075c92fae75c2d40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/****************************************************************************
**
** Copyright (C) 1992-2008 Trolltech ASA.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "main.h"

const char* red_icon[]={
"16 16 2 1",
"r c red",
". c None",
"................",
"................",
"..rrrrrrrrrrrr..",
"..rrrrrrrrrrrr..",
"..rrrrrrrrrrrr..",
"..rrr......rrr..",
"..rrr......rrr..",
"..rrr......rrr..",
"..rrr......rrr..",
"..rrr......rrr..",
"..rrr......rrr..",
"..rrrrrrrrrrrr..",
"..rrrrrrrrrrrr..",
"..rrrrrrrrrrrr..",
"................",
"................"};

const char* blue_icon[]={
"16 16 2 1",
"b c blue",
". c None",
"................",
"................",
"..bbbbbbbbbbbb..",
"..bbbbbbbbbbbb..",
"..bbbbbbbbbbbb..",
"..bbb......bbb..",
"..bbb......bbb..",
"..bbb......bbb..",
"..bbb......bbb..",
"..bbb......bbb..",
"..bbb......bbb..",
"..bbbbbbbbbbbb..",
"..bbbbbbbbbbbb..",
"..bbbbbbbbbbbb..",
"................",
"................"};

const char* green_icon[]={
"16 16 2 1",
"g c green",
". c None",
"................",
"................",
"..gggggggggggg..",
"..gggggggggggg..",
"..gggggggggggg..",
"..ggg......ggg..",
"..ggg......ggg..",
"..ggg......ggg..",
"..ggg......ggg..",
"..ggg......ggg..",
"..ggg......ggg..",
"..gggggggggggg..",
"..gggggggggggg..",
"..gggggggggggg..",
"................",
"................"};


// ListBox -- low level drag and drop

DDListBox::DDListBox( TQWidget * parent, const char * name, WFlags f ) :
    TQListBox( parent, name, f )
{
    setAcceptDrops( TRUE );
    dragging = FALSE;
}


void DDListBox::dragEnterEvent( TQDragEnterEvent *evt )
{
    if ( TQTextDrag::canDecode( evt ) ) 
	evt->accept();
}


void DDListBox::dropEvent( TQDropEvent *evt )
{
    TQString text;

    if ( TQTextDrag::decode( evt, text ) ) 
	insertItem( text );
}


void DDListBox::mousePressEvent( TQMouseEvent *evt )
{
    TQListBox::mousePressEvent( evt );
    dragging = TRUE;
}


void DDListBox::mouseMoveEvent( TQMouseEvent * )
{
    if ( dragging ) {
	TQDragObject *d = new TQTextDrag( currentText(), this );
	d->dragCopy(); // do NOT delete d.
	dragging = FALSE;
    }
}


// IconViewIcon -- high level drag and drop


bool DDIconViewItem::acceptDrop( const TQMimeSource *mime ) const
{
    if ( mime->provides( "text/plain" ) )
	return TRUE;
    return FALSE;
}


void DDIconViewItem::dropped( TQDropEvent *evt, const TQValueList<TQIconDragItem>& )
{
    TQString label;

    if ( TQTextDrag::decode( evt, label ) ) 
	setText( label );
}


// IconView -- high level drag and drop

TQDragObject *DDIconView::dragObject()
{
  return new TQTextDrag( currentItem()->text(), this );
}

void DDIconView::slotNewItem( TQDropEvent *evt, const TQValueList<TQIconDragItem>& )
{
    TQString label;

    if ( TQTextDrag::decode( evt, label ) ) {
	DDIconViewItem *item = new DDIconViewItem( this, label );
	item->setRenameEnabled( TRUE );
    }
}



int main( int argc, char *argv[] )
{
    TQApplication app( argc, argv );

    // Create and show the widgets
    TQSplitter *split = new TQSplitter();
    DDIconView *iv   = new DDIconView( split );
    (void)	       new DDListBox( split );
    app.setMainWidget( split );
    split->resize( 600, 400 );
    split->show();

    // Set up the connection so that we can drop items into the icon view
    TQObject::connect( 
	iv, TQ_SIGNAL(dropped(TQDropEvent*, const TQValueList<TQIconDragItem>&)), 
	iv, TQ_SLOT(slotNewItem(TQDropEvent*, const TQValueList<TQIconDragItem>&)));

    // Populate the TQIconView with icons
    DDIconViewItem *item;
    item = new DDIconViewItem( iv, "Red",   TQPixmap( red_icon ) );
    item->setRenameEnabled( TRUE );
    item = new DDIconViewItem( iv, "Green", TQPixmap( green_icon ) );
    item->setRenameEnabled( TRUE );
    item = new DDIconViewItem( iv, "Blue",  TQPixmap( blue_icon ) );
    item->setRenameEnabled( TRUE );

    return app.exec();
}