]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: dnd.cpp | |
3 | // Purpose: wxDropTarget class | |
4 | // Author: Robert Roebling | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 1998 Robert Roebling | |
7 | // Licence: wxWindows licence | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | #ifdef __GNUG__ | |
11 | #pragma implementation "dnd.h" | |
12 | #endif | |
13 | ||
14 | #include "wx/dnd.h" | |
15 | ||
16 | #if wxUSE_DRAG_AND_DROP | |
17 | ||
18 | #include "wx/window.h" | |
19 | #include "wx/app.h" | |
20 | #include "wx/gdicmn.h" | |
21 | #include "wx/intl.h" | |
22 | #include "wx/utils.h" | |
23 | ||
24 | #include "gdk/gdk.h" | |
25 | #include "gtk/gtk.h" | |
26 | #include "gdk/gdkprivate.h" | |
27 | ||
28 | #include <X11/Xlib.h> | |
29 | ||
30 | // ---------------------------------------------------------------------------- | |
31 | // global | |
32 | // ---------------------------------------------------------------------------- | |
33 | ||
34 | extern bool g_blockEventsOnDrag; | |
35 | ||
36 | #ifdef NEW_GTK_DND_CODE | |
37 | ||
38 | #include "gtk/gtkdnd.h" | |
39 | #include "gtk/gtkselection.h" | |
40 | ||
41 | // ---------------------------------------------------------------------------- | |
42 | // "drag_leave" | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
45 | static void target_drag_leave( GtkWidget *WXUNUSED(widget), | |
46 | GdkDragContext *WXUNUSED(context), | |
47 | guint WXUNUSED(time) ) | |
48 | { | |
49 | printf( "leave.\n" ); | |
50 | } | |
51 | ||
52 | // ---------------------------------------------------------------------------- | |
53 | // "drag_motion" | |
54 | // ---------------------------------------------------------------------------- | |
55 | ||
56 | static gboolean target_drag_motion( GtkWidget *WXUNUSED(widget), | |
57 | GdkDragContext *context, | |
58 | gint WXUNUSED(x), | |
59 | gint WXUNUSED(y), | |
60 | guint time ) | |
61 | { | |
62 | printf( "motion.\n" ); | |
63 | gdk_drag_status( context, context->suggested_action, time ); | |
64 | return TRUE; | |
65 | } | |
66 | ||
67 | // ---------------------------------------------------------------------------- | |
68 | // "drag_drop" | |
69 | // ---------------------------------------------------------------------------- | |
70 | ||
71 | static gboolean target_drag_drop( GtkWidget *widget, | |
72 | GdkDragContext *context, | |
73 | gint x, | |
74 | gint y, | |
75 | guint time ) | |
76 | { | |
77 | printf( "drop at: %d,%d.\n", x, y ); | |
78 | ||
79 | if (context->targets) | |
80 | { | |
81 | gtk_drag_get_data( widget, | |
82 | context, | |
83 | GPOINTER_TO_INT (context->targets->data), | |
84 | time ); | |
85 | } | |
86 | return FALSE; | |
87 | } | |
88 | ||
89 | // ---------------------------------------------------------------------------- | |
90 | // "drag_data_received" | |
91 | // ---------------------------------------------------------------------------- | |
92 | ||
93 | static void target_drag_data_received( GtkWidget *WXUNUSED(widget), | |
94 | GdkDragContext *context, | |
95 | gint x, | |
96 | gint y, | |
97 | GtkSelectionData *data, | |
98 | guint WXUNUSED(info), | |
99 | guint time ) | |
100 | { | |
101 | printf( "data receive at: %d,%d.\n", x, y ); | |
102 | ||
103 | if ((data->length >= 0) && (data->format == 8)) | |
104 | { | |
105 | wxString str = (const char*)data->data; | |
106 | printf( "Received %s\n.", WXSTRINGCAST str ); | |
107 | gtk_drag_finish( context, TRUE, FALSE, time ); | |
108 | return; | |
109 | } | |
110 | ||
111 | gtk_drag_finish (context, FALSE, FALSE, time); | |
112 | } | |
113 | ||
114 | // ---------------------------------------------------------------------------- | |
115 | // wxDropTarget | |
116 | // ---------------------------------------------------------------------------- | |
117 | ||
118 | wxDropTarget::wxDropTarget() | |
119 | { | |
120 | } | |
121 | ||
122 | wxDropTarget::~wxDropTarget() | |
123 | { | |
124 | } | |
125 | ||
126 | void wxDropTarget::UnregisterWidget( GtkWidget *widget ) | |
127 | { | |
128 | wxCHECK_RET( widget != NULL, "unregister widget is NULL" ); | |
129 | ||
130 | gtk_drag_dest_set( widget, | |
131 | (GtkDestDefaults) 0, | |
132 | (GtkTargetEntry*) NULL, | |
133 | 0, | |
134 | (GdkDragAction) 0 ); | |
135 | ||
136 | gtk_signal_disconnect_by_func( GTK_OBJECT(widget), | |
137 | GTK_SIGNAL_FUNC(target_drag_leave), (gpointer) this ); | |
138 | ||
139 | gtk_signal_disconnect_by_func( GTK_OBJECT(widget), | |
140 | GTK_SIGNAL_FUNC(target_drag_motion), (gpointer) this ); | |
141 | ||
142 | gtk_signal_disconnect_by_func( GTK_OBJECT(widget), | |
143 | GTK_SIGNAL_FUNC(target_drag_drop), (gpointer) this ); | |
144 | ||
145 | gtk_signal_disconnect_by_func( GTK_OBJECT(widget), | |
146 | GTK_SIGNAL_FUNC(target_drag_data_received), (gpointer) this ); | |
147 | } | |
148 | ||
149 | void wxDropTarget::RegisterWidget( GtkWidget *widget ) | |
150 | { | |
151 | wxCHECK_RET( widget != NULL, "register widget is NULL" ); | |
152 | ||
153 | GtkTargetEntry format; | |
154 | format.info = 0; | |
155 | format.flags = 0; | |
156 | char buf[100]; | |
157 | ||
158 | int valid = 0; | |
159 | for ( size_t i = 0; i < GetFormatCount(); i++ ) | |
160 | { | |
161 | wxDataFormat df = GetFormat( i ); | |
162 | switch (df) | |
163 | { | |
164 | case wxDF_TEXT: | |
165 | format.target = "text/plain"; | |
166 | valid++; | |
167 | break; | |
168 | case wxDF_FILENAME: | |
169 | format.target = "file:ALL"; | |
170 | valid++; | |
171 | break; | |
172 | case wxDF_PRIVATE: | |
173 | wxPrivateDropTarget *pdt = (wxPrivateDropTarget *)this; | |
174 | strcpy( buf, WXSTRINGCAST pdt->GetID() ); | |
175 | format.target = buf; | |
176 | valid++; | |
177 | default: | |
178 | break; | |
179 | } | |
180 | } | |
181 | ||
182 | wxASSERT_MSG( valid != 0, "No valid DnD format supported." ); | |
183 | ||
184 | gtk_drag_dest_set( widget, | |
185 | GTK_DEST_DEFAULT_ALL, | |
186 | &format, | |
187 | 1, | |
188 | (GdkDragAction)(GDK_ACTION_COPY | GDK_ACTION_MOVE) ); | |
189 | ||
190 | gtk_signal_connect( GTK_OBJECT(widget), "drag_leave", | |
191 | GTK_SIGNAL_FUNC(target_drag_leave), (gpointer) this ); | |
192 | ||
193 | gtk_signal_connect( GTK_OBJECT(widget), "drag_motion", | |
194 | GTK_SIGNAL_FUNC(target_drag_motion), (gpointer) this ); | |
195 | ||
196 | gtk_signal_connect( GTK_OBJECT(widget), "drag_drop", | |
197 | GTK_SIGNAL_FUNC(target_drag_drop), (gpointer) this ); | |
198 | ||
199 | gtk_signal_connect( GTK_OBJECT(widget), "drag_data_received", | |
200 | GTK_SIGNAL_FUNC(target_drag_data_received), (gpointer) this ); | |
201 | } | |
202 | ||
203 | // ---------------------------------------------------------------------------- | |
204 | // wxTextDropTarget | |
205 | // ---------------------------------------------------------------------------- | |
206 | ||
207 | bool wxTextDropTarget::OnDrop( long x, long y, const void *data, size_t WXUNUSED(size) ) | |
208 | { | |
209 | OnDropText( x, y, (const char*)data ); | |
210 | return TRUE; | |
211 | } | |
212 | ||
213 | bool wxTextDropTarget::OnDropText( long x, long y, const char *psz ) | |
214 | { | |
215 | printf( "Got dropped text: %s.\n", psz ); | |
216 | printf( "At x: %d, y: %d.\n", (int)x, (int)y ); | |
217 | return TRUE; | |
218 | } | |
219 | ||
220 | size_t wxTextDropTarget::GetFormatCount() const | |
221 | { | |
222 | return 1; | |
223 | } | |
224 | ||
225 | wxDataFormat wxTextDropTarget::GetFormat(size_t WXUNUSED(n)) const | |
226 | { | |
227 | return wxDF_TEXT; | |
228 | } | |
229 | ||
230 | // ---------------------------------------------------------------------------- | |
231 | // wxFileDropTarget | |
232 | // ---------------------------------------------------------------------------- | |
233 | ||
234 | bool wxFileDropTarget::OnDropFiles( long x, long y, size_t nFiles, const char * const aszFiles[] ) | |
235 | { | |
236 | printf( "Got %d dropped files.\n", (int)nFiles ); | |
237 | printf( "At x: %d, y: %d.\n", (int)x, (int)y ); | |
238 | for (size_t i = 0; i < nFiles; i++) | |
239 | { | |
240 | printf( aszFiles[i] ); | |
241 | printf( "\n" ); | |
242 | } | |
243 | return TRUE; | |
244 | } | |
245 | ||
246 | bool wxFileDropTarget::OnDrop(long x, long y, const void *data, size_t size ) | |
247 | { | |
248 | size_t number = 0; | |
249 | size_t i; | |
250 | char *text = (char*) data; | |
251 | for (i = 0; i < size; i++) | |
252 | if (text[i] == 0) number++; | |
253 | ||
254 | if (number == 0) return TRUE; | |
255 | ||
256 | char **files = new char*[number]; | |
257 | ||
258 | text = (char*) data; | |
259 | for (i = 0; i < number; i++) | |
260 | { | |
261 | files[i] = text; | |
262 | int len = strlen( text ); | |
263 | text += len+1; | |
264 | } | |
265 | ||
266 | bool ret = OnDropFiles( x, y, 1, files ); | |
267 | ||
268 | free( files ); | |
269 | ||
270 | return ret; | |
271 | } | |
272 | ||
273 | size_t wxFileDropTarget::GetFormatCount() const | |
274 | { | |
275 | return 1; | |
276 | } | |
277 | ||
278 | wxDataFormat wxFileDropTarget::GetFormat(size_t WXUNUSED(n)) const | |
279 | { | |
280 | return wxDF_FILENAME; | |
281 | } | |
282 | ||
283 | //------------------------------------------------------------------------- | |
284 | // wxDropSource | |
285 | //------------------------------------------------------------------------- | |
286 | ||
287 | wxDropSource::wxDropSource( wxWindow *win ) | |
288 | { | |
289 | g_blockEventsOnDrag = TRUE; | |
290 | ||
291 | m_window = win; | |
292 | m_widget = win->m_widget; | |
293 | if (win->m_wxwindow) m_widget = win->m_wxwindow; | |
294 | ||
295 | m_data = (wxDataObject *) NULL; | |
296 | m_retValue = wxDragCancel; | |
297 | ||
298 | m_defaultCursor = wxCursor( wxCURSOR_NO_ENTRY ); | |
299 | m_goaheadCursor = wxCursor( wxCURSOR_HAND ); | |
300 | } | |
301 | ||
302 | wxDropSource::wxDropSource( wxDataObject &data, wxWindow *win ) | |
303 | { | |
304 | g_blockEventsOnDrag = TRUE; | |
305 | ||
306 | m_window = win; | |
307 | m_widget = win->m_widget; | |
308 | if (win->m_wxwindow) m_widget = win->m_wxwindow; | |
309 | m_retValue = wxDragCancel; | |
310 | ||
311 | m_data = &data; | |
312 | ||
313 | m_defaultCursor = wxCursor( wxCURSOR_NO_ENTRY ); | |
314 | m_goaheadCursor = wxCursor( wxCURSOR_HAND ); | |
315 | } | |
316 | ||
317 | void wxDropSource::SetData( wxDataObject &data ) | |
318 | { | |
319 | m_data = &data; | |
320 | } | |
321 | ||
322 | wxDropSource::~wxDropSource(void) | |
323 | { | |
324 | // if (m_data) delete m_data; | |
325 | ||
326 | g_blockEventsOnDrag = FALSE; | |
327 | } | |
328 | ||
329 | wxDragResult wxDropSource::DoDragDrop( bool WXUNUSED(bAllowMove) ) | |
330 | { | |
331 | wxASSERT_MSG( m_data, "wxDragSource: no data" ); | |
332 | ||
333 | if (!m_data) return (wxDragResult) wxDragNone; | |
334 | if (m_data->GetDataSize() == 0) return (wxDragResult) wxDragNone; | |
335 | ||
336 | RegisterWindow(); | |
337 | ||
338 | // TODO | |
339 | ||
340 | UnregisterWindow(); | |
341 | ||
342 | g_blockEventsOnDrag = FALSE; | |
343 | ||
344 | return m_retValue; | |
345 | } | |
346 | ||
347 | void wxDropSource::RegisterWindow(void) | |
348 | { | |
349 | if (!m_data) return; | |
350 | ||
351 | wxString formats; | |
352 | ||
353 | wxDataFormat df = m_data->GetPreferredFormat(); | |
354 | ||
355 | switch (df) | |
356 | { | |
357 | case wxDF_TEXT: | |
358 | formats += "text/plain"; | |
359 | break; | |
360 | case wxDF_FILENAME: | |
361 | formats += "file:ALL"; | |
362 | break; | |
363 | default: | |
364 | break; | |
365 | } | |
366 | ||
367 | char *str = WXSTRINGCAST formats; | |
368 | ||
369 | // TODO | |
370 | } | |
371 | ||
372 | void wxDropSource::UnregisterWindow(void) | |
373 | { | |
374 | if (!m_widget) return; | |
375 | ||
376 | // TODO | |
377 | } | |
378 | ||
379 | ||
380 | #else // NEW_CODE | |
381 | ||
382 | GtkWidget *shape_create_icon ( const wxIcon &shape, | |
383 | gint x, | |
384 | gint y, | |
385 | gint px, | |
386 | gint py, | |
387 | gint window_type); | |
388 | ||
389 | /* XPM */ | |
390 | static char * gv_xpm[] = { | |
391 | "40 34 3 1", | |
392 | " s None c None", | |
393 | ". c black", | |
394 | "X c white", | |
395 | " ", | |
396 | " ", | |
397 | " ...... ", | |
398 | " ..XXXXXX.. ", | |
399 | " .XXXXXXXXXX. ", | |
400 | " .XXXXXXXXXXXX. ", | |
401 | " .XXXXXXXXXXXX. ", | |
402 | " .XXXXXXXXXXXXXX. ", | |
403 | " .XXX..XXXX..XXX. ", | |
404 | " ....XX....XX....XX. ", | |
405 | " .XXX.XXX..XXXX..XXX.... ", | |
406 | " .XXXXXXXXXXXXXXXXXXX.XXX. ", | |
407 | " .XXXXXXXXXXXXXXXXXXXXXXXX. ", | |
408 | " .XXXXXXXXXXXXXXXXXXXXXXXX. ", | |
409 | " ..XXXXXXXXXXXXXXXXXXXXXX. ", | |
410 | " .XXXXXXXXXXXXXXXXXX... ", | |
411 | " ..XXXXXXXXXXXXXXXX. ", | |
412 | " .XXXXXXXXXXXXXXXX. ", | |
413 | " .XXXXXXXXXXXXXXXX. ", | |
414 | " .XXXXXXXXXXXXXXXXX. ", | |
415 | " .XXXXXXXXXXXXXXXXX. ", | |
416 | " .XXXXXXXXXXXXXXXXXX. ", | |
417 | " .XXXXXXXXXXXXXXXXXXX. ", | |
418 | " .XXXXXXXXXXXXXXXXXXXXX. ", | |
419 | " .XXXXXXXXXXXXXX.XXXXXXX. ", | |
420 | " .XXXXXXX.XXXXXXX.XXXXXXX. ", | |
421 | " .XXXXXXXX.XXXXXXX.XXXXXXX. ", | |
422 | " .XXXXXXX...XXXXX...XXXXX. ", | |
423 | " .XXXXXXX. ..... ..... ", | |
424 | " ..XXXX.. ", | |
425 | " .... ", | |
426 | " ", | |
427 | " ", | |
428 | " "}; | |
429 | ||
430 | /* XPM */ | |
431 | static char * page_xpm[] = { | |
432 | /* width height ncolors chars_per_pixel */ | |
433 | "32 32 5 1", | |
434 | /* colors */ | |
435 | " s None c None", | |
436 | ". c black", | |
437 | "X c wheat", | |
438 | "o c tan", | |
439 | "O c #6699FF", | |
440 | /* pixels */ | |
441 | " ................... ", | |
442 | " .XXXXXXXXXXXXXXXXX.. ", | |
443 | " .XXXXXXXXXXXXXXXXX.o. ", | |
444 | " .XXXXXXXXXXXXXXXXX.oo. ", | |
445 | " .XXXXXXXXXXXXXXXXX.ooo. ", | |
446 | " .XXXXXXXXXXXXXXXXX.oooo. ", | |
447 | " .XXXXXXXXXXXXXXXXX....... ", | |
448 | " .XXXXXOOOOOOOOOOXXXooooo. ", | |
449 | " .XXXXXXXXXXXXXXXXXXooooo. ", | |
450 | " .XXXXXOOOOOOOOOOXXXXXXXX. ", | |
451 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
452 | " .XXXXXXXOOOOOOOOOXXXXXXX. ", | |
453 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
454 | " .XXXXXXOOOOOOOOOOXXXXXXX. ", | |
455 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
456 | " .XXXXXOOOOOOOOOOXXXXXXXX. ", | |
457 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
458 | " .XXXXXXXOOOOOOOOOXXXXXXX. ", | |
459 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
460 | " .XXXXXXOOOOOOOOOOXXXXXXX. ", | |
461 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
462 | " .XXXXXOOOOOOOOOOXXXXXXXX. ", | |
463 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
464 | " .XXXXXXOOOOOOOOOOXXXXXXX. ", | |
465 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
466 | " .XXXXXOOOOOOOXXXXXXXXXXX. ", | |
467 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
468 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
469 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
470 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
471 | " .XXXXXXXXXXXXXXXXXXXXXXX. ", | |
472 | " ......................... "}; | |
473 | ||
474 | ||
475 | //----------------------------------------------------------------------------- | |
476 | // globals | |
477 | //----------------------------------------------------------------------------- | |
478 | ||
479 | wxDropSource *gs_currentDropSource = (wxDropSource*) NULL; | |
480 | ||
481 | //----------------------------------------------------------------------------- | |
482 | // "drop_enter_event" | |
483 | //----------------------------------------------------------------------------- | |
484 | ||
485 | static void gtk_target_enter_callback( GtkWidget *WXUNUSED(widget), | |
486 | GdkEventDropEnter *WXUNUSED(event), | |
487 | wxDropTarget *target ) | |
488 | { | |
489 | if (target) | |
490 | target->OnEnter(); | |
491 | } | |
492 | ||
493 | //----------------------------------------------------------------------------- | |
494 | // "drop_leave_event" | |
495 | //----------------------------------------------------------------------------- | |
496 | ||
497 | static void gtk_target_leave_callback( GtkWidget *WXUNUSED(widget), | |
498 | GdkEventDropLeave *WXUNUSED(event), | |
499 | wxDropTarget *target ) | |
500 | { | |
501 | if (target) | |
502 | target->OnLeave(); | |
503 | } | |
504 | ||
505 | //----------------------------------------------------------------------------- | |
506 | // "drop_data_available_event" | |
507 | //----------------------------------------------------------------------------- | |
508 | ||
509 | static void gtk_target_callback( GtkWidget *widget, | |
510 | GdkEventDropDataAvailable *event, | |
511 | wxDropTarget *target ) | |
512 | { | |
513 | if (target) | |
514 | { | |
515 | int x = 0; | |
516 | int y = 0; | |
517 | gdk_window_get_pointer( widget->window, &x, &y, (GdkModifierType *) NULL ); | |
518 | /* | |
519 | printf( "Drop data is of type %s.\n", event->data_type ); | |
520 | */ | |
521 | target->OnDrop( x, y, (const void*)event->data, (size_t)event->data_numbytes ); | |
522 | } | |
523 | ||
524 | /* | |
525 | g_free (event->data); | |
526 | g_free (event->data_type); | |
527 | */ | |
528 | } | |
529 | ||
530 | // ---------------------------------------------------------------------------- | |
531 | // wxDropTarget | |
532 | // ---------------------------------------------------------------------------- | |
533 | ||
534 | wxDropTarget::wxDropTarget() | |
535 | { | |
536 | m_format = (wxDataFormat*) NULL; | |
537 | } | |
538 | ||
539 | wxDropTarget::~wxDropTarget() | |
540 | { | |
541 | if (m_format) delete m_format; | |
542 | } | |
543 | ||
544 | wxDataFormat &wxDropTarget::GetFormat(size_t n) const | |
545 | { | |
546 | return (*m_format); | |
547 | } | |
548 | ||
549 | void wxDropTarget::UnregisterWidget( GtkWidget *widget ) | |
550 | { | |
551 | if (!widget) return; | |
552 | ||
553 | gtk_signal_disconnect_by_func( GTK_OBJECT(widget), | |
554 | GTK_SIGNAL_FUNC(gtk_target_callback), (gpointer) this ); | |
555 | ||
556 | gtk_widget_dnd_drop_set( widget, FALSE, (gchar **) NULL, 0, FALSE ); | |
557 | } | |
558 | ||
559 | void wxDropTarget::RegisterWidget( GtkWidget *widget ) | |
560 | { | |
561 | wxString formats; | |
562 | int valid = 0; | |
563 | ||
564 | for ( size_t i = 0; i < GetFormatCount(); i++ ) | |
565 | { | |
566 | switch (GetFormat(i).GetType()) | |
567 | { | |
568 | case wxDF_TEXT: | |
569 | { | |
570 | if (i > 0) formats += ";"; | |
571 | formats += "text/plain"; | |
572 | valid++; | |
573 | break; | |
574 | } | |
575 | case wxDF_FILENAME: | |
576 | { | |
577 | if (i > 0) formats += ";"; | |
578 | formats += "file:ALL"; | |
579 | valid++; | |
580 | break; | |
581 | } | |
582 | case wxDF_PRIVATE: | |
583 | { | |
584 | if (i > 0) formats += ";"; | |
585 | wxPrivateDropTarget *pdt = (wxPrivateDropTarget *)this; | |
586 | formats += pdt->GetId(); | |
587 | valid++; | |
588 | break; | |
589 | } | |
590 | default: | |
591 | break; | |
592 | } | |
593 | } | |
594 | ||
595 | char *str = WXSTRINGCAST formats; | |
596 | ||
597 | gtk_widget_dnd_drop_set( widget, TRUE, &str, valid, FALSE ); | |
598 | ||
599 | gtk_signal_connect( GTK_OBJECT(widget), "drop_data_available_event", | |
600 | GTK_SIGNAL_FUNC(gtk_target_callback), (gpointer) this ); | |
601 | ||
602 | gtk_signal_connect( GTK_OBJECT(widget), "drop_enter_event", | |
603 | GTK_SIGNAL_FUNC(gtk_target_enter_callback), (gpointer) this ); | |
604 | ||
605 | gtk_signal_connect( GTK_OBJECT(widget), "drop_leave_event", | |
606 | GTK_SIGNAL_FUNC(gtk_target_leave_callback), (gpointer) this ); | |
607 | } | |
608 | ||
609 | // ---------------------------------------------------------------------------- | |
610 | // wxTextDropTarget | |
611 | // ---------------------------------------------------------------------------- | |
612 | ||
613 | wxTextDropTarget::wxTextDropTarget() | |
614 | { | |
615 | m_format = new wxDataFormat( wxDF_TEXT ); | |
616 | } | |
617 | ||
618 | bool wxTextDropTarget::OnDrop( long x, long y, const void *data, size_t WXUNUSED(size) ) | |
619 | { | |
620 | OnDropText( x, y, (const char*)data ); | |
621 | return TRUE; | |
622 | } | |
623 | ||
624 | bool wxTextDropTarget::OnDropText( long x, long y, const char *psz ) | |
625 | { | |
626 | /* | |
627 | printf( "Got dropped text: %s.\n", psz ); | |
628 | printf( "At x: %d, y: %d.\n", (int)x, (int)y ); | |
629 | */ | |
630 | return TRUE; | |
631 | } | |
632 | ||
633 | size_t wxTextDropTarget::GetFormatCount() const | |
634 | { | |
635 | return 1; | |
636 | } | |
637 | ||
638 | // ---------------------------------------------------------------------------- | |
639 | // wxPrivateDropTarget | |
640 | // ---------------------------------------------------------------------------- | |
641 | ||
642 | wxPrivateDropTarget::wxPrivateDropTarget() | |
643 | { | |
644 | m_id = wxTheApp->GetAppName(); | |
645 | m_format = new wxDataFormat( m_id ); | |
646 | } | |
647 | ||
648 | void wxPrivateDropTarget::SetId( const wxString& id ) | |
649 | { | |
650 | m_id = id; | |
651 | m_format->SetId( id ); | |
652 | } | |
653 | ||
654 | size_t wxPrivateDropTarget::GetFormatCount() const | |
655 | { | |
656 | return 1; | |
657 | } | |
658 | ||
659 | // ---------------------------------------------------------------------------- | |
660 | // wxFileDropTarget | |
661 | // ---------------------------------------------------------------------------- | |
662 | ||
663 | wxFileDropTarget::wxFileDropTarget() | |
664 | { | |
665 | m_format = new wxDataFormat( wxDF_FILENAME ); | |
666 | } | |
667 | ||
668 | bool wxFileDropTarget::OnDropFiles( long x, long y, size_t nFiles, const char * const aszFiles[] ) | |
669 | { | |
670 | printf( "Got %d dropped files.\n", (int)nFiles ); | |
671 | printf( "At x: %d, y: %d.\n", (int)x, (int)y ); | |
672 | ||
673 | for (size_t i = 0; i < nFiles; i++) | |
674 | { | |
675 | printf( aszFiles[i] ); | |
676 | printf( "\n" ); | |
677 | } | |
678 | ||
679 | return TRUE; | |
680 | } | |
681 | ||
682 | bool wxFileDropTarget::OnDrop(long x, long y, const void *data, size_t size ) | |
683 | { | |
684 | size_t number = 0; | |
685 | size_t i; | |
686 | char *text = (char*) data; | |
687 | for ( i = 0; i < size; i++) | |
688 | if (text[i] == 0) number++; | |
689 | ||
690 | if (number == 0) return TRUE; | |
691 | ||
692 | char **files = new char*[number]; | |
693 | ||
694 | text = (char*) data; | |
695 | for (i = 0; i < number; i++) | |
696 | { | |
697 | files[i] = text; | |
698 | int len = strlen( text ); | |
699 | text += len+1; | |
700 | } | |
701 | ||
702 | bool ret = OnDropFiles( x, y, 1, files ); | |
703 | ||
704 | free( files ); | |
705 | ||
706 | return ret; | |
707 | } | |
708 | ||
709 | size_t wxFileDropTarget::GetFormatCount() const | |
710 | { | |
711 | return 1; | |
712 | } | |
713 | ||
714 | //------------------------------------------------------------------------- | |
715 | // wxDropSource | |
716 | //------------------------------------------------------------------------- | |
717 | ||
718 | static void | |
719 | shape_motion (GtkWidget *widget, | |
720 | GdkEventMotion * /*event*/); | |
721 | ||
722 | //----------------------------------------------------------------------------- | |
723 | // "drag_request_event" | |
724 | //----------------------------------------------------------------------------- | |
725 | ||
726 | void gtk_drag_callback( GtkWidget *widget, GdkEventDragRequest *event, wxDropSource *source ) | |
727 | { | |
728 | wxDataBroker *data = source->m_data; | |
729 | ||
730 | if (!data) return; | |
731 | ||
732 | wxNode *node = data->m_dataObjects.First(); | |
733 | { | |
734 | wxDataObject *dobj = (wxDataObject*) node->Data(); | |
735 | ||
736 | if ((strcmp(event->data_type,"file:ALL") == 0) && | |
737 | (dobj->GetFormat().GetType() == wxDF_FILENAME)) | |
738 | { | |
739 | wxFileDataObject *file_object = (wxFileDataObject*) dobj; | |
740 | ||
741 | wxString text = file_object->GetFiles(); | |
742 | ||
743 | char *s = WXSTRINGCAST text; | |
744 | ||
745 | gtk_widget_dnd_data_set( widget, | |
746 | (GdkEvent*)event, | |
747 | (unsigned char*) s, | |
748 | (int) text.Length()+1 ); | |
749 | ||
750 | source->m_retValue = wxDragCopy; | |
751 | ||
752 | return; | |
753 | } | |
754 | ||
755 | if ((strcmp(event->data_type,"text/plain") == 0) && | |
756 | (dobj->GetFormat().GetType() == wxDF_TEXT)) | |
757 | { | |
758 | wxTextDataObject *text_object = (wxTextDataObject*) dobj; | |
759 | ||
760 | wxString text = text_object->GetText(); | |
761 | ||
762 | char *s = WXSTRINGCAST text; | |
763 | ||
764 | gtk_widget_dnd_data_set( widget, | |
765 | (GdkEvent*)event, | |
766 | (unsigned char*) s, | |
767 | (int) text.Length()+1 ); | |
768 | ||
769 | source->m_retValue = wxDragCopy; | |
770 | ||
771 | return; | |
772 | } | |
773 | ||
774 | if (dobj->GetFormat().GetType() == wxDF_PRIVATE) | |
775 | { | |
776 | wxPrivateDataObject *pdo = (wxPrivateDataObject*) dobj; | |
777 | ||
778 | if (pdo->GetId() == event->data_type) | |
779 | { | |
780 | gtk_widget_dnd_data_set( widget, | |
781 | (GdkEvent*)event, | |
782 | (unsigned char*) pdo->GetData(), | |
783 | (int) pdo->GetSize() ); | |
784 | ||
785 | source->m_retValue = wxDragCopy; | |
786 | ||
787 | return; | |
788 | } | |
789 | } | |
790 | ||
791 | node = node->Next(); | |
792 | } | |
793 | } | |
794 | ||
795 | wxDropSource::wxDropSource( wxWindow *win, const wxIcon &go, const wxIcon &stop ) | |
796 | { | |
797 | g_blockEventsOnDrag = TRUE; | |
798 | ||
799 | m_window = win; | |
800 | m_widget = win->m_widget; | |
801 | if (win->m_wxwindow) m_widget = win->m_wxwindow; | |
802 | ||
803 | m_data = (wxDataBroker*) NULL; | |
804 | m_retValue = wxDragCancel; | |
805 | ||
806 | m_defaultCursor = wxCursor( wxCURSOR_NO_ENTRY ); | |
807 | m_goaheadCursor = wxCursor( wxCURSOR_HAND ); | |
808 | ||
809 | m_goIcon = go; | |
810 | if (wxNullIcon == go) m_goIcon = wxIcon( page_xpm ); | |
811 | m_stopIcon = stop; | |
812 | if (wxNullIcon == stop) m_stopIcon = wxIcon( gv_xpm ); | |
813 | } | |
814 | ||
815 | wxDropSource::wxDropSource( wxDataObject *data, wxWindow *win, const wxIcon &go, const wxIcon &stop ) | |
816 | { | |
817 | g_blockEventsOnDrag = TRUE; | |
818 | ||
819 | m_window = win; | |
820 | m_widget = win->m_widget; | |
821 | if (win->m_wxwindow) m_widget = win->m_wxwindow; | |
822 | m_retValue = wxDragCancel; | |
823 | ||
824 | if (data) | |
825 | { | |
826 | m_data = new wxDataBroker(); | |
827 | m_data->Add( data ); | |
828 | } | |
829 | else | |
830 | { | |
831 | m_data = (wxDataBroker*) NULL; | |
832 | } | |
833 | ||
834 | m_defaultCursor = wxCursor( wxCURSOR_NO_ENTRY ); | |
835 | m_goaheadCursor = wxCursor( wxCURSOR_HAND ); | |
836 | ||
837 | m_goIcon = go; | |
838 | if (wxNullIcon == go) m_goIcon = wxIcon( page_xpm ); | |
839 | m_stopIcon = stop; | |
840 | if (wxNullIcon == stop) m_stopIcon = wxIcon( gv_xpm ); | |
841 | } | |
842 | ||
843 | wxDropSource::wxDropSource( wxDataBroker *data, wxWindow *win ) | |
844 | { | |
845 | g_blockEventsOnDrag = TRUE; | |
846 | ||
847 | m_window = win; | |
848 | m_widget = win->m_widget; | |
849 | if (win->m_wxwindow) m_widget = win->m_wxwindow; | |
850 | m_retValue = wxDragCancel; | |
851 | ||
852 | m_data = data; | |
853 | ||
854 | m_defaultCursor = wxCursor( wxCURSOR_NO_ENTRY ); | |
855 | m_goaheadCursor = wxCursor( wxCURSOR_HAND ); | |
856 | } | |
857 | ||
858 | void wxDropSource::SetData( wxDataObject *data ) | |
859 | { | |
860 | if (m_data) delete m_data; | |
861 | ||
862 | if (data) | |
863 | { | |
864 | m_data = new wxDataBroker(); | |
865 | m_data->Add( data ); | |
866 | } | |
867 | else | |
868 | { | |
869 | m_data = (wxDataBroker*) NULL; | |
870 | } | |
871 | } | |
872 | ||
873 | void wxDropSource::SetData( wxDataBroker *data ) | |
874 | { | |
875 | if (m_data) delete m_data; | |
876 | ||
877 | m_data = data; | |
878 | } | |
879 | ||
880 | wxDropSource::~wxDropSource(void) | |
881 | { | |
882 | if (m_data) delete m_data; | |
883 | ||
884 | g_blockEventsOnDrag = FALSE; | |
885 | } | |
886 | ||
887 | wxDragResult wxDropSource::DoDragDrop( bool WXUNUSED(bAllowMove) ) | |
888 | { | |
889 | if (gdk_dnd.dnd_grabbed) return (wxDragResult) wxDragNone; | |
890 | if (gdk_dnd.drag_really) return (wxDragResult) wxDragNone; | |
891 | ||
892 | wxASSERT_MSG( m_data, "wxDragSource: no data" ); | |
893 | ||
894 | if (!m_data) return (wxDragResult) wxDragNone; | |
895 | ||
896 | static GtkWidget *drag_icon = (GtkWidget*) NULL; | |
897 | static GtkWidget *drop_icon = (GtkWidget*) NULL; | |
898 | ||
899 | GdkPoint hotspot_1 = {0,-5 }; | |
900 | ||
901 | if (!drag_icon) | |
902 | { | |
903 | drag_icon = shape_create_icon ( m_stopIcon, | |
904 | 440, 140, 0,0, GTK_WINDOW_POPUP); | |
905 | ||
906 | gtk_signal_connect (GTK_OBJECT (drag_icon), "destroy", | |
907 | GTK_SIGNAL_FUNC(gtk_widget_destroyed), | |
908 | &drag_icon); | |
909 | ||
910 | gtk_widget_hide (drag_icon); | |
911 | } | |
912 | ||
913 | GdkPoint hotspot_2 = {-5,-5}; | |
914 | ||
915 | if (!drop_icon) | |
916 | { | |
917 | drop_icon = shape_create_icon ( m_goIcon, | |
918 | 440, 140, 0,0, GTK_WINDOW_POPUP); | |
919 | ||
920 | gtk_signal_connect (GTK_OBJECT (drop_icon), "destroy", | |
921 | GTK_SIGNAL_FUNC(gtk_widget_destroyed), | |
922 | &drop_icon); | |
923 | ||
924 | gtk_widget_hide (drop_icon); | |
925 | } | |
926 | ||
927 | ||
928 | gdk_dnd_set_drag_shape( drag_icon->window, | |
929 | &hotspot_1, | |
930 | drop_icon->window, | |
931 | &hotspot_2); | |
932 | ||
933 | ||
934 | GdkWindowPrivate *wp = (GdkWindowPrivate*) m_widget->window; | |
935 | ||
936 | RegisterWindow(); | |
937 | ||
938 | gdk_dnd.drag_perhaps = TRUE; | |
939 | ||
940 | gdk_dnd.dnd_drag_start.x = 5; | |
941 | gdk_dnd.dnd_drag_start.y = 5; | |
942 | gdk_dnd.real_sw = wp; | |
943 | ||
944 | if (gdk_dnd.drag_startwindows) | |
945 | { | |
946 | g_free( gdk_dnd.drag_startwindows ); | |
947 | gdk_dnd.drag_startwindows = (GdkWindow **) NULL; | |
948 | } | |
949 | gdk_dnd.drag_numwindows = gdk_dnd.drag_really = 0; | |
950 | ||
951 | XWindowAttributes dnd_winattr; | |
952 | XGetWindowAttributes( gdk_display, wp->xwindow, &dnd_winattr ); | |
953 | wp->dnd_drag_savedeventmask = dnd_winattr.your_event_mask; | |
954 | ||
955 | gdk_dnd_drag_addwindow( m_widget->window ); | |
956 | ||
957 | GdkEventDragBegin ev; | |
958 | ev.type = GDK_DRAG_BEGIN; | |
959 | ev.window = m_widget->window; | |
960 | ev.u.allflags = 0; | |
961 | ev.u.flags.protocol_version = DND_PROTOCOL_VERSION; | |
962 | ||
963 | gdk_event_put( (GdkEvent*)&ev ); | |
964 | ||
965 | XGrabPointer( gdk_display, wp->xwindow, False, | |
966 | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask, | |
967 | GrabModeAsync, GrabModeAsync, gdk_root_window, None, CurrentTime ); | |
968 | ||
969 | gdk_dnd_set_drag_cursors( m_defaultCursor.GetCursor(), m_goaheadCursor.GetCursor() ); | |
970 | ||
971 | gdk_dnd.dnd_grabbed = TRUE; | |
972 | gdk_dnd.drag_really = 1; | |
973 | ||
974 | int x = 0; | |
975 | int y = 0; | |
976 | wxGetMousePosition( &x, &y ); | |
977 | ||
978 | gdk_dnd_display_drag_cursor( x, y, FALSE, TRUE ); | |
979 | ||
980 | gs_currentDropSource = this; | |
981 | ||
982 | while (gdk_dnd.drag_really || gdk_dnd.drag_perhaps) wxYield(); | |
983 | ||
984 | gs_currentDropSource = (wxDropSource*) NULL; | |
985 | ||
986 | UnregisterWindow(); | |
987 | ||
988 | g_blockEventsOnDrag = FALSE; | |
989 | ||
990 | return m_retValue; | |
991 | } | |
992 | ||
993 | void wxDropSource::RegisterWindow(void) | |
994 | { | |
995 | if (!m_data) return; | |
996 | ||
997 | wxString formats; | |
998 | ||
999 | wxNode *node = m_data->m_dataObjects.First(); | |
1000 | while (node) | |
1001 | { | |
1002 | wxDataObject* dobj = (wxDataObject*) node->Data(); | |
1003 | ||
1004 | switch (dobj->GetFormat().GetType()) | |
1005 | { | |
1006 | case wxDF_TEXT: | |
1007 | { | |
1008 | formats += "text/plain"; | |
1009 | break; | |
1010 | } | |
1011 | case wxDF_FILENAME: | |
1012 | { | |
1013 | formats += "file:ALL"; | |
1014 | break; | |
1015 | } | |
1016 | case wxDF_PRIVATE: | |
1017 | { | |
1018 | wxPrivateDataObject* pdo = (wxPrivateDataObject*) m_data; | |
1019 | formats += pdo->GetId(); | |
1020 | break; | |
1021 | } | |
1022 | default: | |
1023 | break; | |
1024 | } | |
1025 | node = node->Next(); | |
1026 | } | |
1027 | ||
1028 | char *str = WXSTRINGCAST formats; | |
1029 | ||
1030 | gtk_widget_dnd_drag_set( m_widget, TRUE, &str, 1 ); | |
1031 | ||
1032 | gtk_signal_connect( GTK_OBJECT(m_widget), "drag_request_event", | |
1033 | GTK_SIGNAL_FUNC(gtk_drag_callback), (gpointer)this ); | |
1034 | } | |
1035 | ||
1036 | void wxDropSource::UnregisterWindow(void) | |
1037 | { | |
1038 | if (!m_widget) return; | |
1039 | ||
1040 | gtk_widget_dnd_drag_set( m_widget, FALSE, (gchar **) NULL, 0 ); | |
1041 | ||
1042 | gtk_signal_disconnect_by_data( GTK_OBJECT(m_widget), (gpointer)this ); | |
1043 | } | |
1044 | ||
1045 | ||
1046 | /* | |
1047 | * Shaped Windows | |
1048 | */ | |
1049 | static GdkWindow *root_win = (GdkWindow*) NULL; | |
1050 | ||
1051 | typedef struct _cursoroffset {gint x,y;} CursorOffset; | |
1052 | ||
1053 | static void | |
1054 | shape_pressed (GtkWidget *widget, GdkEventButton *event) | |
1055 | { | |
1056 | CursorOffset *p; | |
1057 | ||
1058 | /* ignore double and triple click */ | |
1059 | if (event->type != GDK_BUTTON_PRESS) | |
1060 | return; | |
1061 | ||
1062 | p = (CursorOffset *)gtk_object_get_user_data (GTK_OBJECT(widget)); | |
1063 | p->x = (int) event->x; | |
1064 | p->y = (int) event->y; | |
1065 | ||
1066 | gtk_grab_add (widget); | |
1067 | gdk_pointer_grab (widget->window, TRUE, | |
1068 | (GdkEventMask) | |
1069 | (GDK_BUTTON_RELEASE_MASK | | |
1070 | GDK_BUTTON_MOTION_MASK | | |
1071 | GDK_POINTER_MOTION_HINT_MASK), | |
1072 | (GdkWindow*)NULL, | |
1073 | (GdkCursor*) NULL, 0); | |
1074 | } | |
1075 | ||
1076 | ||
1077 | static void | |
1078 | shape_released (GtkWidget *widget) | |
1079 | { | |
1080 | gtk_grab_remove (widget); | |
1081 | gdk_pointer_ungrab (0); | |
1082 | } | |
1083 | ||
1084 | static void | |
1085 | shape_motion (GtkWidget *widget, | |
1086 | GdkEventMotion * /*event*/ ) | |
1087 | { | |
1088 | gint xp, yp; | |
1089 | CursorOffset * p; | |
1090 | GdkModifierType mask; | |
1091 | ||
1092 | p = (CursorOffset *)gtk_object_get_user_data (GTK_OBJECT (widget)); | |
1093 | ||
1094 | /* | |
1095 | * Can't use event->x / event->y here | |
1096 | * because I need absolute coordinates. | |
1097 | */ | |
1098 | ||
1099 | gdk_window_get_pointer (root_win, &xp, &yp, &mask); | |
1100 | gtk_widget_set_uposition (widget, xp - p->x, yp - p->y); | |
1101 | ||
1102 | if (gs_currentDropSource) gs_currentDropSource->GiveFeedback( wxDragCopy, FALSE ); | |
1103 | } | |
1104 | ||
1105 | GtkWidget * | |
1106 | shape_create_icon (const wxIcon &shape, | |
1107 | gint x, | |
1108 | gint y, | |
1109 | gint px, | |
1110 | gint py, | |
1111 | gint window_type) | |
1112 | { | |
1113 | /* | |
1114 | * GDK_WINDOW_TOPLEVEL works also, giving you a title border | |
1115 | */ | |
1116 | GtkWidget *window = gtk_window_new ((GtkWindowType)window_type); | |
1117 | ||
1118 | GtkWidget *fixed = gtk_fixed_new (); | |
1119 | gtk_widget_set_usize (fixed, 100,100); | |
1120 | gtk_container_add (GTK_CONTAINER (window), fixed); | |
1121 | gtk_widget_show (fixed); | |
1122 | ||
1123 | gtk_widget_set_events (window, | |
1124 | gtk_widget_get_events (window) | | |
1125 | GDK_BUTTON_MOTION_MASK | | |
1126 | GDK_POINTER_MOTION_HINT_MASK | | |
1127 | GDK_BUTTON_PRESS_MASK); | |
1128 | ||
1129 | gtk_widget_realize (window); | |
1130 | ||
1131 | GdkBitmap *mask = (GdkBitmap*) NULL; | |
1132 | if (shape.GetMask()) mask = shape.GetMask()->GetBitmap(); | |
1133 | ||
1134 | GtkWidget *pixmap = gtk_pixmap_new (shape.GetPixmap(), mask); | |
1135 | gtk_fixed_put (GTK_FIXED (fixed), pixmap, px,py); | |
1136 | gtk_widget_show (pixmap); | |
1137 | ||
1138 | gtk_widget_shape_combine_mask (window, mask, px,py); | |
1139 | ||
1140 | ||
1141 | gtk_signal_connect (GTK_OBJECT (window), "button_press_event", | |
1142 | GTK_SIGNAL_FUNC (shape_pressed),NULL); | |
1143 | gtk_signal_connect (GTK_OBJECT (window), "button_release_event", | |
1144 | GTK_SIGNAL_FUNC (shape_released),NULL); | |
1145 | gtk_signal_connect (GTK_OBJECT (window), "motion_notify_event", | |
1146 | GTK_SIGNAL_FUNC (shape_motion),NULL); | |
1147 | ||
1148 | CursorOffset*icon_pos = g_new (CursorOffset, 1); | |
1149 | gtk_object_set_user_data(GTK_OBJECT(window), icon_pos); | |
1150 | ||
1151 | gtk_widget_set_uposition (window, x, y); | |
1152 | gtk_widget_show (window); | |
1153 | ||
1154 | return window; | |
1155 | } | |
1156 | ||
1157 | #endif | |
1158 | // NEW_GTK_DND_CODE | |
1159 | ||
1160 | #endif | |
1161 | ||
1162 | // wxUSE_DRAG_AND_DROP |