Fix [ 1574240 ] wx.RadioButton doesn't navigate correctly
[wxWidgets.git] / src / gtk / window.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/window.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #ifdef __VMS
14 #define XWarpPointer XWARPPOINTER
15 #endif
16
17 #include "wx/window.h"
18
19 #ifndef WX_PRECOMP
20 #include "wx/intl.h"
21 #include "wx/log.h"
22 #include "wx/app.h"
23 #include "wx/utils.h"
24 #include "wx/frame.h"
25 #include "wx/dcclient.h"
26 #include "wx/menu.h"
27 #include "wx/dialog.h"
28 #include "wx/settings.h"
29 #include "wx/msgdlg.h"
30 #include "wx/textctrl.h"
31 #include "wx/radiobut.h"
32 #include "wx/toolbar.h"
33 #include "wx/combobox.h"
34 #include "wx/layout.h"
35 #include "wx/statusbr.h"
36 #include "wx/math.h"
37 #include "wx/module.h"
38 #endif
39
40 #if wxUSE_DRAG_AND_DROP
41 #include "wx/dnd.h"
42 #endif
43
44 #if wxUSE_TOOLTIPS
45 #include "wx/tooltip.h"
46 #endif
47
48 #if wxUSE_CARET
49 #include "wx/caret.h"
50 #endif // wxUSE_CARET
51
52 #include "wx/fontutil.h"
53
54 #ifdef __WXDEBUG__
55 #include "wx/thread.h"
56 #endif
57
58 #include <ctype.h>
59
60 // FIXME: Due to a hack we use GtkCombo in here, which is deprecated since gtk2.3.0
61 #include <gtk/gtkversion.h>
62 #if defined(GTK_DISABLE_DEPRECATED) && GTK_CHECK_VERSION(2,3,0)
63 #undef GTK_DISABLE_DEPRECATED
64 #endif
65
66 #include "wx/gtk/private.h"
67 #include <gdk/gdkprivate.h>
68 #include <gdk/gdkkeysyms.h>
69 #include <gdk/gdkx.h>
70
71 #include <gtk/gtk.h>
72 #include <gtk/gtkprivate.h>
73
74 #include "wx/gtk/win_gtk.h"
75
76 #include <pango/pangox.h>
77
78 #ifdef HAVE_XIM
79 #undef HAVE_XIM
80 #endif
81
82 //-----------------------------------------------------------------------------
83 // documentation on internals
84 //-----------------------------------------------------------------------------
85
86 /*
87 I have been asked several times about writing some documentation about
88 the GTK port of wxWidgets, especially its internal structures. Obviously,
89 you cannot understand wxGTK without knowing a little about the GTK, but
90 some more information about what the wxWindow, which is the base class
91 for all other window classes, does seems required as well.
92
93 I)
94
95 What does wxWindow do? It contains the common interface for the following
96 jobs of its descendants:
97
98 1) Define the rudimentary behaviour common to all window classes, such as
99 resizing, intercepting user input (so as to make it possible to use these
100 events for special purposes in a derived class), window names etc.
101
102 2) Provide the possibility to contain and manage children, if the derived
103 class is allowed to contain children, which holds true for those window
104 classes which do not display a native GTK widget. To name them, these
105 classes are wxPanel, wxScrolledWindow, wxDialog, wxFrame. The MDI frame-
106 work classes are a special case and are handled a bit differently from
107 the rest. The same holds true for the wxNotebook class.
108
109 3) Provide the possibility to draw into a client area of a window. This,
110 too, only holds true for classes that do not display a native GTK widget
111 as above.
112
113 4) Provide the entire mechanism for scrolling widgets. This actual inter-
114 face for this is usually in wxScrolledWindow, but the GTK implementation
115 is in this class.
116
117 5) A multitude of helper or extra methods for special purposes, such as
118 Drag'n'Drop, managing validators etc.
119
120 6) Display a border (sunken, raised, simple or none).
121
122 Normally one might expect, that one wxWidgets window would always correspond
123 to one GTK widget. Under GTK, there is no such all-round widget that has all
124 the functionality. Moreover, the GTK defines a client area as a different
125 widget from the actual widget you are handling. Last but not least some
126 special classes (e.g. wxFrame) handle different categories of widgets and
127 still have the possibility to draw something in the client area.
128 It was therefore required to write a special purpose GTK widget, that would
129 represent a client area in the sense of wxWidgets capable to do the jobs
130 2), 3) and 4). I have written this class and it resides in win_gtk.c of
131 this directory.
132
133 All windows must have a widget, with which they interact with other under-
134 lying GTK widgets. It is this widget, e.g. that has to be resized etc and
135 the wxWindow class has a member variable called m_widget which holds a
136 pointer to this widget. When the window class represents a GTK native widget,
137 this is (in most cases) the only GTK widget the class manages. E.g. the
138 wxStaticText class handles only a GtkLabel widget a pointer to which you
139 can find in m_widget (defined in wxWindow)
140
141 When the class has a client area for drawing into and for containing children
142 it has to handle the client area widget (of the type GtkPizza, defined in
143 win_gtk.c), but there could be any number of widgets, handled by a class
144 The common rule for all windows is only, that the widget that interacts with
145 the rest of GTK must be referenced in m_widget and all other widgets must be
146 children of this widget on the GTK level. The top-most widget, which also
147 represents the client area, must be in the m_wxwindow field and must be of
148 the type GtkPizza.
149
150 As I said, the window classes that display a GTK native widget only have
151 one widget, so in the case of e.g. the wxButton class m_widget holds a
152 pointer to a GtkButton widget. But windows with client areas (for drawing
153 and children) have a m_widget field that is a pointer to a GtkScrolled-
154 Window and a m_wxwindow field that is pointer to a GtkPizza and this
155 one is (in the GTK sense) a child of the GtkScrolledWindow.
156
157 If the m_wxwindow field is set, then all input to this widget is inter-
158 cepted and sent to the wxWidgets class. If not, all input to the widget
159 that gets pointed to by m_widget gets intercepted and sent to the class.
160
161 II)
162
163 The design of scrolling in wxWidgets is markedly different from that offered
164 by the GTK itself and therefore we cannot simply take it as it is. In GTK,
165 clicking on a scrollbar belonging to scrolled window will inevitably move
166 the window. In wxWidgets, the scrollbar will only emit an event, send this
167 to (normally) a wxScrolledWindow and that class will call ScrollWindow()
168 which actually moves the window and its sub-windows. Note that GtkPizza
169 memorizes how much it has been scrolled but that wxWidgets forgets this
170 so that the two coordinates systems have to be kept in synch. This is done
171 in various places using the pizza->xoffset and pizza->yoffset values.
172
173 III)
174
175 Singularly the most broken code in GTK is the code that is supposed to
176 inform subwindows (child windows) about new positions. Very often, duplicate
177 events are sent without changes in size or position, equally often no
178 events are sent at all (All this is due to a bug in the GtkContainer code
179 which got fixed in GTK 1.2.6). For that reason, wxGTK completely ignores
180 GTK's own system and it simply waits for size events for toplevel windows
181 and then iterates down the respective size events to all window. This has
182 the disadvantage that windows might get size events before the GTK widget
183 actually has the reported size. This doesn't normally pose any problem, but
184 the OpenGL drawing routines rely on correct behaviour. Therefore, I have
185 added the m_nativeSizeEvents flag, which is true only for the OpenGL canvas,
186 i.e. the wxGLCanvas will emit a size event, when (and not before) the X11
187 window that is used for OpenGL output really has that size (as reported by
188 GTK).
189
190 IV)
191
192 If someone at some point of time feels the immense desire to have a look at,
193 change or attempt to optimise the Refresh() logic, this person will need an
194 intimate understanding of what "draw" and "expose" events are and what
195 they are used for, in particular when used in connection with GTK's
196 own windowless widgets. Beware.
197
198 V)
199
200 Cursors, too, have been a constant source of pleasure. The main difficulty
201 is that a GdkWindow inherits a cursor if the programmer sets a new cursor
202 for the parent. To prevent this from doing too much harm, I use idle time
203 to set the cursor over and over again, starting from the toplevel windows
204 and ending with the youngest generation (speaking of parent and child windows).
205 Also don't forget that cursors (like much else) are connected to GdkWindows,
206 not GtkWidgets and that the "window" field of a GtkWidget might very well
207 point to the GdkWindow of the parent widget (-> "window-less widget") and
208 that the two obviously have very different meanings.
209
210 */
211
212 //-----------------------------------------------------------------------------
213 // data
214 //-----------------------------------------------------------------------------
215
216 extern bool g_blockEventsOnDrag;
217 extern bool g_blockEventsOnScroll;
218 extern wxCursor g_globalCursor;
219
220 // mouse capture state: the window which has it and if the mouse is currently
221 // inside it
222 static wxWindowGTK *g_captureWindow = (wxWindowGTK*) NULL;
223 static bool g_captureWindowHasMouse = false;
224
225 wxWindowGTK *g_focusWindow = (wxWindowGTK*) NULL;
226
227 // the last window which had the focus - this is normally never NULL (except
228 // if we never had focus at all) as even when g_focusWindow is NULL it still
229 // keeps its previous value
230 wxWindowGTK *g_focusWindowLast = (wxWindowGTK*) NULL;
231
232 // If a window get the focus set but has not been realized
233 // yet, defer setting the focus to idle time.
234 wxWindowGTK *g_delayedFocus = (wxWindowGTK*) NULL;
235
236 extern bool g_mainThreadLocked;
237
238 //-----------------------------------------------------------------------------
239 // debug
240 //-----------------------------------------------------------------------------
241
242 #ifdef __WXDEBUG__
243
244 #if wxUSE_THREADS
245 # define DEBUG_MAIN_THREAD if (wxThread::IsMain() && g_mainThreadLocked) printf("gui reentrance");
246 #else
247 # define DEBUG_MAIN_THREAD
248 #endif
249 #else
250 #define DEBUG_MAIN_THREAD
251 #endif // Debug
252
253 // the trace mask used for the focus debugging messages
254 #define TRACE_FOCUS _T("focus")
255
256 //-----------------------------------------------------------------------------
257 // missing gdk functions
258 //-----------------------------------------------------------------------------
259
260 void
261 gdk_window_warp_pointer (GdkWindow *window,
262 gint x,
263 gint y)
264 {
265 if (!window)
266 window = gdk_get_default_root_window();
267
268 if (!GDK_WINDOW_DESTROYED(window))
269 {
270 XWarpPointer (GDK_WINDOW_XDISPLAY(window),
271 None, /* not source window -> move from anywhere */
272 GDK_WINDOW_XID(window), /* dest window */
273 0, 0, 0, 0, /* not source window -> move from anywhere */
274 x, y );
275 }
276 }
277
278 //-----------------------------------------------------------------------------
279 // local code (see below)
280 //-----------------------------------------------------------------------------
281
282 // returns the child of win which currently has focus or NULL if not found
283 //
284 // Note: can't be static, needed by textctrl.cpp.
285 wxWindow *wxFindFocusedChild(wxWindowGTK *win)
286 {
287 wxWindow *winFocus = wxWindowGTK::FindFocus();
288 if ( !winFocus )
289 return (wxWindow *)NULL;
290
291 if ( winFocus == win )
292 return (wxWindow *)win;
293
294 for ( wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
295 node;
296 node = node->GetNext() )
297 {
298 wxWindow *child = wxFindFocusedChild(node->GetData());
299 if ( child )
300 return child;
301 }
302
303 return (wxWindow *)NULL;
304 }
305
306 static void GetScrollbarWidth(GtkWidget* widget, int& w, int& h)
307 {
308 GtkScrolledWindow* scroll_window = GTK_SCROLLED_WINDOW(widget);
309 GtkScrolledWindowClass* scroll_class = GTK_SCROLLED_WINDOW_CLASS(GTK_OBJECT_GET_CLASS(scroll_window));
310 GtkRequisition scroll_req;
311
312 w = 0;
313 if (scroll_window->vscrollbar_visible)
314 {
315 scroll_req.width = 2;
316 scroll_req.height = 2;
317 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(scroll_window->vscrollbar) )->size_request )
318 (scroll_window->vscrollbar, &scroll_req );
319 w = scroll_req.width +
320 scroll_class->scrollbar_spacing;
321 }
322
323 h = 0;
324 if (scroll_window->hscrollbar_visible)
325 {
326 scroll_req.width = 2;
327 scroll_req.height = 2;
328 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(scroll_window->hscrollbar) )->size_request )
329 (scroll_window->hscrollbar, &scroll_req );
330 h = scroll_req.height +
331 scroll_class->scrollbar_spacing;
332 }
333 }
334
335 static void draw_frame( GtkWidget *widget, wxWindowGTK *win )
336 {
337 // wxUniversal widgets draw the borders and scrollbars themselves
338 #ifndef __WXUNIVERSAL__
339 if (!win->m_hasVMT)
340 return;
341
342 int dx = 0;
343 int dy = 0;
344 if (GTK_WIDGET_NO_WINDOW (widget))
345 {
346 dx += widget->allocation.x;
347 dy += widget->allocation.y;
348 }
349
350 int x = dx;
351 int y = dy;
352
353 int dw = 0;
354 int dh = 0;
355 if (win->m_hasScrolling)
356 {
357 GetScrollbarWidth(widget, dw, dh);
358
359 if (win->GetLayoutDirection() == wxLayout_RightToLeft)
360 {
361 // This is actually wrong for old GTK+ version
362 // which do not display the scrollbar on the
363 // left side in RTL
364 x += dw;
365 }
366 }
367
368 int w = widget->allocation.width-dw;
369 int h = widget->allocation.height-dh;
370
371 if (win->HasFlag(wxRAISED_BORDER))
372 {
373 gtk_paint_shadow (widget->style,
374 widget->window,
375 GTK_STATE_NORMAL,
376 GTK_SHADOW_OUT,
377 NULL, NULL, NULL, // FIXME: No clipping?
378 x, y, w, h );
379 return;
380 }
381
382 if (win->HasFlag(wxSUNKEN_BORDER))
383 {
384 gtk_paint_shadow (widget->style,
385 widget->window,
386 GTK_STATE_NORMAL,
387 GTK_SHADOW_IN,
388 NULL, NULL, NULL, // FIXME: No clipping?
389 x, y, w, h );
390 return;
391 }
392
393 if (win->HasFlag(wxSIMPLE_BORDER))
394 {
395 GdkGC *gc;
396 gc = gdk_gc_new( widget->window );
397 gdk_gc_set_foreground( gc, &widget->style->black );
398 gdk_draw_rectangle( widget->window, gc, FALSE, x, y, w-1, h-1 );
399 g_object_unref (gc);
400 return;
401 }
402 #endif // __WXUNIVERSAL__
403 }
404
405 //-----------------------------------------------------------------------------
406 // "expose_event" of m_widget
407 //-----------------------------------------------------------------------------
408
409 extern "C" {
410 static gboolean
411 gtk_window_own_expose_callback( GtkWidget *widget,
412 GdkEventExpose *gdk_event,
413 wxWindowGTK *win )
414 {
415 if (gdk_event->count == 0)
416 draw_frame(widget, win);
417 return false;
418 }
419 }
420
421 //-----------------------------------------------------------------------------
422 // "size_request" of m_widget
423 //-----------------------------------------------------------------------------
424
425 // make it extern because wxStaticText needs to disconnect this one
426 extern "C" {
427 void wxgtk_window_size_request_callback(GtkWidget *widget,
428 GtkRequisition *requisition,
429 wxWindow *win)
430 {
431 int w, h;
432 win->GetSize( &w, &h );
433 if (w < 2)
434 w = 2;
435 if (h < 2)
436 h = 2;
437
438 requisition->height = h;
439 requisition->width = w;
440 }
441 }
442
443 extern "C" {
444 static
445 void wxgtk_combo_size_request_callback(GtkWidget *widget,
446 GtkRequisition *requisition,
447 wxComboBox *win)
448 {
449 // This callback is actually hooked into the text entry
450 // of the combo box, not the GtkHBox.
451
452 int w, h;
453 win->GetSize( &w, &h );
454 if (w < 2)
455 w = 2;
456 if (h < 2)
457 h = 2;
458
459 GtkCombo *gcombo = GTK_COMBO(win->m_widget);
460
461 GtkRequisition entry_req;
462 entry_req.width = 2;
463 entry_req.height = 2;
464 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(gcombo->button) )->size_request )
465 (gcombo->button, &entry_req );
466
467 requisition->width = w - entry_req.width;
468 requisition->height = entry_req.height;
469 }
470 }
471
472 //-----------------------------------------------------------------------------
473 // "expose_event" of m_wxwindow
474 //-----------------------------------------------------------------------------
475
476 extern "C" {
477 static gboolean
478 gtk_window_expose_callback( GtkWidget *widget,
479 GdkEventExpose *gdk_event,
480 wxWindow *win )
481 {
482 DEBUG_MAIN_THREAD
483
484 // don't need to install idle handler, its done from "event" signal
485
486 // This callback gets called in drawing-idle time under
487 // GTK 2.0, so we don't need to defer anything to idle
488 // time anymore.
489
490 GtkPizza *pizza = GTK_PIZZA( widget );
491 if (gdk_event->window != pizza->bin_window) return FALSE;
492
493
494 #if 0
495 if (win->GetName())
496 {
497 wxPrintf( wxT("OnExpose from ") );
498 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
499 wxPrintf( win->GetClassInfo()->GetClassName() );
500 wxPrintf( wxT(" %d %d %d %d\n"), (int)gdk_event->area.x,
501 (int)gdk_event->area.y,
502 (int)gdk_event->area.width,
503 (int)gdk_event->area.height );
504 }
505
506 gtk_paint_box
507 (
508 win->m_wxwindow->style,
509 pizza->bin_window,
510 GTK_STATE_NORMAL,
511 GTK_SHADOW_OUT,
512 (GdkRectangle*) NULL,
513 win->m_wxwindow,
514 (char *)"button", // const_cast
515 20,20,24,24
516 );
517 #endif
518
519 win->GetUpdateRegion() = wxRegion( gdk_event->region );
520
521 win->GtkSendPaintEvents();
522
523 // Let parent window draw window-less widgets
524 return FALSE;
525 }
526 }
527
528 //-----------------------------------------------------------------------------
529 // "key_press_event" from any window
530 //-----------------------------------------------------------------------------
531
532 // These are used when transforming Ctrl-alpha to ascii values 1-26
533 inline bool wxIsLowerChar(int code)
534 {
535 return (code >= 'a' && code <= 'z' );
536 }
537
538 inline bool wxIsUpperChar(int code)
539 {
540 return (code >= 'A' && code <= 'Z' );
541 }
542
543
544 // set WXTRACE to this to see the key event codes on the console
545 #define TRACE_KEYS _T("keyevent")
546
547 // translates an X key symbol to WXK_XXX value
548 //
549 // if isChar is true it means that the value returned will be used for EVT_CHAR
550 // event and then we choose the logical WXK_XXX, i.e. '/' for GDK_KP_Divide,
551 // for example, while if it is false it means that the value is going to be
552 // used for KEY_DOWN/UP events and then we translate GDK_KP_Divide to
553 // WXK_NUMPAD_DIVIDE
554 static long wxTranslateKeySymToWXKey(KeySym keysym, bool isChar)
555 {
556 long key_code;
557
558 switch ( keysym )
559 {
560 // Shift, Control and Alt don't generate the CHAR events at all
561 case GDK_Shift_L:
562 case GDK_Shift_R:
563 key_code = isChar ? 0 : WXK_SHIFT;
564 break;
565 case GDK_Control_L:
566 case GDK_Control_R:
567 key_code = isChar ? 0 : WXK_CONTROL;
568 break;
569 case GDK_Meta_L:
570 case GDK_Meta_R:
571 case GDK_Alt_L:
572 case GDK_Alt_R:
573 case GDK_Super_L:
574 case GDK_Super_R:
575 key_code = isChar ? 0 : WXK_ALT;
576 break;
577
578 // neither do the toggle modifies
579 case GDK_Scroll_Lock:
580 key_code = isChar ? 0 : WXK_SCROLL;
581 break;
582
583 case GDK_Caps_Lock:
584 key_code = isChar ? 0 : WXK_CAPITAL;
585 break;
586
587 case GDK_Num_Lock:
588 key_code = isChar ? 0 : WXK_NUMLOCK;
589 break;
590
591
592 // various other special keys
593 case GDK_Menu:
594 key_code = WXK_MENU;
595 break;
596
597 case GDK_Help:
598 key_code = WXK_HELP;
599 break;
600
601 case GDK_BackSpace:
602 key_code = WXK_BACK;
603 break;
604
605 case GDK_ISO_Left_Tab:
606 case GDK_Tab:
607 key_code = WXK_TAB;
608 break;
609
610 case GDK_Linefeed:
611 case GDK_Return:
612 key_code = WXK_RETURN;
613 break;
614
615 case GDK_Clear:
616 key_code = WXK_CLEAR;
617 break;
618
619 case GDK_Pause:
620 key_code = WXK_PAUSE;
621 break;
622
623 case GDK_Select:
624 key_code = WXK_SELECT;
625 break;
626
627 case GDK_Print:
628 key_code = WXK_PRINT;
629 break;
630
631 case GDK_Execute:
632 key_code = WXK_EXECUTE;
633 break;
634
635 case GDK_Escape:
636 key_code = WXK_ESCAPE;
637 break;
638
639 // cursor and other extended keyboard keys
640 case GDK_Delete:
641 key_code = WXK_DELETE;
642 break;
643
644 case GDK_Home:
645 key_code = WXK_HOME;
646 break;
647
648 case GDK_Left:
649 key_code = WXK_LEFT;
650 break;
651
652 case GDK_Up:
653 key_code = WXK_UP;
654 break;
655
656 case GDK_Right:
657 key_code = WXK_RIGHT;
658 break;
659
660 case GDK_Down:
661 key_code = WXK_DOWN;
662 break;
663
664 case GDK_Prior: // == GDK_Page_Up
665 key_code = WXK_PAGEUP;
666 break;
667
668 case GDK_Next: // == GDK_Page_Down
669 key_code = WXK_PAGEDOWN;
670 break;
671
672 case GDK_End:
673 key_code = WXK_END;
674 break;
675
676 case GDK_Begin:
677 key_code = WXK_HOME;
678 break;
679
680 case GDK_Insert:
681 key_code = WXK_INSERT;
682 break;
683
684
685 // numpad keys
686 case GDK_KP_0:
687 case GDK_KP_1:
688 case GDK_KP_2:
689 case GDK_KP_3:
690 case GDK_KP_4:
691 case GDK_KP_5:
692 case GDK_KP_6:
693 case GDK_KP_7:
694 case GDK_KP_8:
695 case GDK_KP_9:
696 key_code = (isChar ? '0' : WXK_NUMPAD0) + keysym - GDK_KP_0;
697 break;
698
699 case GDK_KP_Space:
700 key_code = isChar ? ' ' : WXK_NUMPAD_SPACE;
701 break;
702
703 case GDK_KP_Tab:
704 key_code = isChar ? WXK_TAB : WXK_NUMPAD_TAB;
705 break;
706
707 case GDK_KP_Enter:
708 key_code = isChar ? WXK_RETURN : WXK_NUMPAD_ENTER;
709 break;
710
711 case GDK_KP_F1:
712 key_code = isChar ? WXK_F1 : WXK_NUMPAD_F1;
713 break;
714
715 case GDK_KP_F2:
716 key_code = isChar ? WXK_F2 : WXK_NUMPAD_F2;
717 break;
718
719 case GDK_KP_F3:
720 key_code = isChar ? WXK_F3 : WXK_NUMPAD_F3;
721 break;
722
723 case GDK_KP_F4:
724 key_code = isChar ? WXK_F4 : WXK_NUMPAD_F4;
725 break;
726
727 case GDK_KP_Home:
728 key_code = isChar ? WXK_HOME : WXK_NUMPAD_HOME;
729 break;
730
731 case GDK_KP_Left:
732 key_code = isChar ? WXK_LEFT : WXK_NUMPAD_LEFT;
733 break;
734
735 case GDK_KP_Up:
736 key_code = isChar ? WXK_UP : WXK_NUMPAD_UP;
737 break;
738
739 case GDK_KP_Right:
740 key_code = isChar ? WXK_RIGHT : WXK_NUMPAD_RIGHT;
741 break;
742
743 case GDK_KP_Down:
744 key_code = isChar ? WXK_DOWN : WXK_NUMPAD_DOWN;
745 break;
746
747 case GDK_KP_Prior: // == GDK_KP_Page_Up
748 key_code = isChar ? WXK_PAGEUP : WXK_NUMPAD_PAGEUP;
749 break;
750
751 case GDK_KP_Next: // == GDK_KP_Page_Down
752 key_code = isChar ? WXK_PAGEDOWN : WXK_NUMPAD_PAGEDOWN;
753 break;
754
755 case GDK_KP_End:
756 key_code = isChar ? WXK_END : WXK_NUMPAD_END;
757 break;
758
759 case GDK_KP_Begin:
760 key_code = isChar ? WXK_HOME : WXK_NUMPAD_BEGIN;
761 break;
762
763 case GDK_KP_Insert:
764 key_code = isChar ? WXK_INSERT : WXK_NUMPAD_INSERT;
765 break;
766
767 case GDK_KP_Delete:
768 key_code = isChar ? WXK_DELETE : WXK_NUMPAD_DELETE;
769 break;
770
771 case GDK_KP_Equal:
772 key_code = isChar ? '=' : WXK_NUMPAD_EQUAL;
773 break;
774
775 case GDK_KP_Multiply:
776 key_code = isChar ? '*' : WXK_NUMPAD_MULTIPLY;
777 break;
778
779 case GDK_KP_Add:
780 key_code = isChar ? '+' : WXK_NUMPAD_ADD;
781 break;
782
783 case GDK_KP_Separator:
784 // FIXME: what is this?
785 key_code = isChar ? '.' : WXK_NUMPAD_SEPARATOR;
786 break;
787
788 case GDK_KP_Subtract:
789 key_code = isChar ? '-' : WXK_NUMPAD_SUBTRACT;
790 break;
791
792 case GDK_KP_Decimal:
793 key_code = isChar ? '.' : WXK_NUMPAD_DECIMAL;
794 break;
795
796 case GDK_KP_Divide:
797 key_code = isChar ? '/' : WXK_NUMPAD_DIVIDE;
798 break;
799
800
801 // function keys
802 case GDK_F1:
803 case GDK_F2:
804 case GDK_F3:
805 case GDK_F4:
806 case GDK_F5:
807 case GDK_F6:
808 case GDK_F7:
809 case GDK_F8:
810 case GDK_F9:
811 case GDK_F10:
812 case GDK_F11:
813 case GDK_F12:
814 key_code = WXK_F1 + keysym - GDK_F1;
815 break;
816
817 default:
818 key_code = 0;
819 }
820
821 return key_code;
822 }
823
824 static inline bool wxIsAsciiKeysym(KeySym ks)
825 {
826 return ks < 256;
827 }
828
829 static void wxFillOtherKeyEventFields(wxKeyEvent& event,
830 wxWindowGTK *win,
831 GdkEventKey *gdk_event)
832 {
833 int x = 0;
834 int y = 0;
835 GdkModifierType state;
836 if (gdk_event->window)
837 gdk_window_get_pointer(gdk_event->window, &x, &y, &state);
838
839 event.SetTimestamp( gdk_event->time );
840 event.SetId(win->GetId());
841 event.m_shiftDown = (gdk_event->state & GDK_SHIFT_MASK) != 0;
842 event.m_controlDown = (gdk_event->state & GDK_CONTROL_MASK) != 0;
843 event.m_altDown = (gdk_event->state & GDK_MOD1_MASK) != 0;
844 event.m_metaDown = (gdk_event->state & GDK_MOD2_MASK) != 0;
845 event.m_scanCode = gdk_event->keyval;
846 event.m_rawCode = (wxUint32) gdk_event->keyval;
847 event.m_rawFlags = 0;
848 #if wxUSE_UNICODE
849 event.m_uniChar = gdk_keyval_to_unicode(gdk_event->keyval);
850 #endif
851 wxGetMousePosition( &x, &y );
852 win->ScreenToClient( &x, &y );
853 event.m_x = x;
854 event.m_y = y;
855 event.SetEventObject( win );
856 }
857
858
859 static bool
860 wxTranslateGTKKeyEventToWx(wxKeyEvent& event,
861 wxWindowGTK *win,
862 GdkEventKey *gdk_event)
863 {
864 // VZ: it seems that GDK_KEY_RELEASE event doesn't set event->string
865 // but only event->keyval which is quite useless to us, so remember
866 // the last character from GDK_KEY_PRESS and reuse it as last resort
867 //
868 // NB: should be MT-safe as we're always called from the main thread only
869 static struct
870 {
871 KeySym keysym;
872 long keycode;
873 } s_lastKeyPress = { 0, 0 };
874
875 KeySym keysym = gdk_event->keyval;
876
877 wxLogTrace(TRACE_KEYS, _T("Key %s event: keysym = %ld"),
878 event.GetEventType() == wxEVT_KEY_UP ? _T("release")
879 : _T("press"),
880 keysym);
881
882 long key_code = wxTranslateKeySymToWXKey(keysym, false /* !isChar */);
883
884 if ( !key_code )
885 {
886 // do we have the translation or is it a plain ASCII character?
887 if ( (gdk_event->length == 1) || wxIsAsciiKeysym(keysym) )
888 {
889 // we should use keysym if it is ASCII as X does some translations
890 // like "I pressed while Control is down" => "Ctrl-I" == "TAB"
891 // which we don't want here (but which we do use for OnChar())
892 if ( !wxIsAsciiKeysym(keysym) )
893 {
894 keysym = (KeySym)gdk_event->string[0];
895 }
896
897 // we want to always get the same key code when the same key is
898 // pressed regardless of the state of the modifiers, i.e. on a
899 // standard US keyboard pressing '5' or '%' ('5' key with
900 // Shift) should result in the same key code in OnKeyDown():
901 // '5' (although OnChar() will get either '5' or '%').
902 //
903 // to do it we first translate keysym to keycode (== scan code)
904 // and then back but always using the lower register
905 Display *dpy = (Display *)wxGetDisplay();
906 KeyCode keycode = XKeysymToKeycode(dpy, keysym);
907
908 wxLogTrace(TRACE_KEYS, _T("\t-> keycode %d"), keycode);
909
910 KeySym keysymNormalized = XKeycodeToKeysym(dpy, keycode, 0);
911
912 // use the normalized, i.e. lower register, keysym if we've
913 // got one
914 key_code = keysymNormalized ? keysymNormalized : keysym;
915
916 // as explained above, we want to have lower register key codes
917 // normally but for the letter keys we want to have the upper ones
918 //
919 // NB: don't use XConvertCase() here, we want to do it for letters
920 // only
921 key_code = toupper(key_code);
922 }
923 else // non ASCII key, what to do?
924 {
925 // by default, ignore it
926 key_code = 0;
927
928 // but if we have cached information from the last KEY_PRESS
929 if ( gdk_event->type == GDK_KEY_RELEASE )
930 {
931 // then reuse it
932 if ( keysym == s_lastKeyPress.keysym )
933 {
934 key_code = s_lastKeyPress.keycode;
935 }
936 }
937 }
938
939 if ( gdk_event->type == GDK_KEY_PRESS )
940 {
941 // remember it to be reused for KEY_UP event later
942 s_lastKeyPress.keysym = keysym;
943 s_lastKeyPress.keycode = key_code;
944 }
945 }
946
947 wxLogTrace(TRACE_KEYS, _T("\t-> wxKeyCode %ld"), key_code);
948
949 // sending unknown key events doesn't really make sense
950 if ( !key_code )
951 return false;
952
953 // now fill all the other fields
954 wxFillOtherKeyEventFields(event, win, gdk_event);
955
956 event.m_keyCode = key_code;
957 #if wxUSE_UNICODE
958 if ( gdk_event->type == GDK_KEY_PRESS || gdk_event->type == GDK_KEY_RELEASE )
959 {
960 event.m_uniChar = key_code;
961 }
962 #endif
963
964 return true;
965 }
966
967
968 struct wxGtkIMData
969 {
970 GtkIMContext *context;
971 GdkEventKey *lastKeyEvent;
972
973 wxGtkIMData()
974 {
975 context = gtk_im_multicontext_new();
976 lastKeyEvent = NULL;
977 }
978 ~wxGtkIMData()
979 {
980 g_object_unref (context);
981 }
982 };
983
984 extern "C" {
985 static gboolean
986 gtk_window_key_press_callback( GtkWidget *widget,
987 GdkEventKey *gdk_event,
988 wxWindow *win )
989 {
990 DEBUG_MAIN_THREAD
991
992 // don't need to install idle handler, its done from "event" signal
993
994 if (!win->m_hasVMT)
995 return FALSE;
996 if (g_blockEventsOnDrag)
997 return FALSE;
998
999
1000 wxKeyEvent event( wxEVT_KEY_DOWN );
1001 bool ret = false;
1002 bool return_after_IM = false;
1003
1004 if( wxTranslateGTKKeyEventToWx(event, win, gdk_event) )
1005 {
1006 // Emit KEY_DOWN event
1007 ret = win->GetEventHandler()->ProcessEvent( event );
1008 }
1009 else
1010 {
1011 // Return after IM processing as we cannot do
1012 // anything with it anyhow.
1013 return_after_IM = true;
1014 }
1015
1016 // 2005.01.26 modified by Hong Jen Yee (hzysoft@sina.com.tw):
1017 // When we get a key_press event here, it could be originate
1018 // from the current widget or its child widgets. However, only the widget
1019 // with the INPUT FOCUS can generate the INITIAL key_press event. That is,
1020 // if the CURRENT widget doesn't have the FOCUS at all, this event definitely
1021 // originated from its child widgets and shouldn't be passed to IM context.
1022 // In fact, what a GTK+ IM should do is filtering keyEvents and convert them
1023 // into text input ONLY WHEN THE WIDGET HAS INPUT FOCUS. Besides, when current
1024 // widgets has both IM context and input focus, the event should be filtered
1025 // by gtk_im_context_filter_keypress().
1026 // Then, we should, according to GTK+ 2.0 API doc, return whatever it returns.
1027 if ((!ret) && (win->m_imData != NULL) && ( wxWindow::FindFocus() == win ))
1028 {
1029 // We should let GTK+ IM filter key event first. According to GTK+ 2.0 API
1030 // docs, if IM filter returns true, no further processing should be done.
1031 // we should send the key_down event anyway.
1032 bool intercepted_by_IM = gtk_im_context_filter_keypress(win->m_imData->context, gdk_event);
1033 win->m_imData->lastKeyEvent = NULL;
1034 if (intercepted_by_IM)
1035 {
1036 wxLogTrace(TRACE_KEYS, _T("Key event intercepted by IM"));
1037 return TRUE;
1038 }
1039 }
1040
1041 if (return_after_IM)
1042 return FALSE;
1043
1044 #if wxUSE_ACCEL
1045 if (!ret)
1046 {
1047 wxWindowGTK *ancestor = win;
1048 while (ancestor)
1049 {
1050 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1051 if (command != -1)
1052 {
1053 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1054 ret = ancestor->GetEventHandler()->ProcessEvent( command_event );
1055 break;
1056 }
1057 if (ancestor->IsTopLevel())
1058 break;
1059 ancestor = ancestor->GetParent();
1060 }
1061 }
1062 #endif // wxUSE_ACCEL
1063
1064 // Only send wxEVT_CHAR event if not processed yet. Thus, ALT-x
1065 // will only be sent if it is not in an accelerator table.
1066 if (!ret)
1067 {
1068 long key_code;
1069 KeySym keysym = gdk_event->keyval;
1070 // Find key code for EVT_CHAR and EVT_CHAR_HOOK events
1071 key_code = wxTranslateKeySymToWXKey(keysym, true /* isChar */);
1072 if ( !key_code )
1073 {
1074 if ( wxIsAsciiKeysym(keysym) )
1075 {
1076 // ASCII key
1077 key_code = (unsigned char)keysym;
1078 }
1079 // gdk_event->string is actually deprecated
1080 else if ( gdk_event->length == 1 )
1081 {
1082 key_code = (unsigned char)gdk_event->string[0];
1083 }
1084 }
1085
1086 if ( key_code )
1087 {
1088 wxLogTrace(TRACE_KEYS, _T("Char event: %ld"), key_code);
1089
1090 event.m_keyCode = key_code;
1091
1092 // To conform to the docs we need to translate Ctrl-alpha
1093 // characters to values in the range 1-26.
1094 if ( event.ControlDown() &&
1095 ( wxIsLowerChar(key_code) || wxIsUpperChar(key_code) ))
1096 {
1097 if ( wxIsLowerChar(key_code) )
1098 event.m_keyCode = key_code - 'a' + 1;
1099 if ( wxIsUpperChar(key_code) )
1100 event.m_keyCode = key_code - 'A' + 1;
1101 #if wxUSE_UNICODE
1102 event.m_uniChar = event.m_keyCode;
1103 #endif
1104 }
1105
1106 // Implement OnCharHook by checking ancestor top level windows
1107 wxWindow *parent = win;
1108 while (parent && !parent->IsTopLevel())
1109 parent = parent->GetParent();
1110 if (parent)
1111 {
1112 event.SetEventType( wxEVT_CHAR_HOOK );
1113 ret = parent->GetEventHandler()->ProcessEvent( event );
1114 }
1115
1116 if (!ret)
1117 {
1118 event.SetEventType(wxEVT_CHAR);
1119 ret = win->GetEventHandler()->ProcessEvent( event );
1120 }
1121 }
1122 }
1123
1124 // win is a control: tab can be propagated up
1125 if ( !ret &&
1126 ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab)) &&
1127 // VZ: testing for wxTE_PROCESS_TAB shouldn't be done here - the control may
1128 // have this style, yet choose not to process this particular TAB in which
1129 // case TAB must still work as a navigational character
1130 // JS: enabling again to make consistent with other platforms
1131 // (with wxTE_PROCESS_TAB you have to call Navigate to get default
1132 // navigation behaviour)
1133 #if wxUSE_TEXTCTRL
1134 (! (win->HasFlag(wxTE_PROCESS_TAB) && win->IsKindOf(CLASSINFO(wxTextCtrl)) )) &&
1135 #endif
1136 win->GetParent() && (win->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
1137 {
1138 wxNavigationKeyEvent new_event;
1139 new_event.SetEventObject( win->GetParent() );
1140 // GDK reports GDK_ISO_Left_Tab for SHIFT-TAB
1141 new_event.SetDirection( (gdk_event->keyval == GDK_Tab) );
1142 // CTRL-TAB changes the (parent) window, i.e. switch notebook page
1143 new_event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) );
1144 new_event.SetCurrentFocus( win );
1145 ret = win->GetParent()->GetEventHandler()->ProcessEvent( new_event );
1146 }
1147
1148 return ret;
1149 }
1150 }
1151
1152 extern "C" {
1153 static void
1154 gtk_wxwindow_commit_cb (GtkIMContext *context,
1155 const gchar *str,
1156 wxWindow *window)
1157 {
1158 wxKeyEvent event( wxEVT_KEY_DOWN );
1159
1160 // take modifiers, cursor position, timestamp etc. from the last
1161 // key_press_event that was fed into Input Method:
1162 if (window->m_imData->lastKeyEvent)
1163 {
1164 wxFillOtherKeyEventFields(event,
1165 window, window->m_imData->lastKeyEvent);
1166 }
1167
1168 const wxWxCharBuffer data(wxGTK_CONV_BACK(str));
1169 if( !data )
1170 return;
1171
1172 bool ret = false;
1173
1174 // Implement OnCharHook by checking ancestor top level windows
1175 wxWindow *parent = window;
1176 while (parent && !parent->IsTopLevel())
1177 parent = parent->GetParent();
1178
1179 for( const wxChar* pstr = data; *pstr; pstr++ )
1180 {
1181 #if wxUSE_UNICODE
1182 event.m_uniChar = *pstr;
1183 // Backward compatible for ISO-8859-1
1184 event.m_keyCode = *pstr < 256 ? event.m_uniChar : 0;
1185 wxLogTrace(TRACE_KEYS, _T("IM sent character '%c'"), event.m_uniChar);
1186 #else
1187 event.m_keyCode = *pstr;
1188 #endif // wxUSE_UNICODE
1189
1190 // To conform to the docs we need to translate Ctrl-alpha
1191 // characters to values in the range 1-26.
1192 if ( event.ControlDown() &&
1193 ( wxIsLowerChar(*pstr) || wxIsUpperChar(*pstr) ))
1194 {
1195 if ( wxIsLowerChar(*pstr) )
1196 event.m_keyCode = *pstr - 'a' + 1;
1197 if ( wxIsUpperChar(*pstr) )
1198 event.m_keyCode = *pstr - 'A' + 1;
1199
1200 event.m_keyCode = *pstr - 'a' + 1;
1201 #if wxUSE_UNICODE
1202 event.m_uniChar = event.m_keyCode;
1203 #endif
1204 }
1205
1206 if (parent)
1207 {
1208 event.SetEventType( wxEVT_CHAR_HOOK );
1209 ret = parent->GetEventHandler()->ProcessEvent( event );
1210 }
1211
1212 if (!ret)
1213 {
1214 event.SetEventType(wxEVT_CHAR);
1215 ret = window->GetEventHandler()->ProcessEvent( event );
1216 }
1217 }
1218 }
1219 }
1220
1221
1222 //-----------------------------------------------------------------------------
1223 // "key_release_event" from any window
1224 //-----------------------------------------------------------------------------
1225
1226 extern "C" {
1227 static gboolean
1228 gtk_window_key_release_callback( GtkWidget *widget,
1229 GdkEventKey *gdk_event,
1230 wxWindowGTK *win )
1231 {
1232 DEBUG_MAIN_THREAD
1233
1234 // don't need to install idle handler, its done from "event" signal
1235
1236 if (!win->m_hasVMT)
1237 return FALSE;
1238
1239 if (g_blockEventsOnDrag)
1240 return FALSE;
1241
1242 wxKeyEvent event( wxEVT_KEY_UP );
1243 if ( !wxTranslateGTKKeyEventToWx(event, win, gdk_event) )
1244 {
1245 // unknown key pressed, ignore (the event would be useless anyhow)
1246 return FALSE;
1247 }
1248
1249 return win->GTKProcessEvent(event);
1250 }
1251 }
1252
1253 // ============================================================================
1254 // the mouse events
1255 // ============================================================================
1256
1257 // ----------------------------------------------------------------------------
1258 // mouse event processing helpers
1259 // ----------------------------------------------------------------------------
1260
1261 // init wxMouseEvent with the info from GdkEventXXX struct
1262 template<typename T> void InitMouseEvent(wxWindowGTK *win,
1263 wxMouseEvent& event,
1264 T *gdk_event)
1265 {
1266 event.SetTimestamp( gdk_event->time );
1267 event.m_shiftDown = (gdk_event->state & GDK_SHIFT_MASK);
1268 event.m_controlDown = (gdk_event->state & GDK_CONTROL_MASK);
1269 event.m_altDown = (gdk_event->state & GDK_MOD1_MASK);
1270 event.m_metaDown = (gdk_event->state & GDK_MOD2_MASK);
1271 event.m_leftDown = (gdk_event->state & GDK_BUTTON1_MASK);
1272 event.m_middleDown = (gdk_event->state & GDK_BUTTON2_MASK);
1273 event.m_rightDown = (gdk_event->state & GDK_BUTTON3_MASK);
1274 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
1275 {
1276 event.m_linesPerAction = 3;
1277 event.m_wheelDelta = 120;
1278 if (((GdkEventButton*)gdk_event)->button == 4)
1279 event.m_wheelRotation = 120;
1280 else if (((GdkEventButton*)gdk_event)->button == 5)
1281 event.m_wheelRotation = -120;
1282 }
1283
1284 wxPoint pt = win->GetClientAreaOrigin();
1285 event.m_x = (wxCoord)gdk_event->x - pt.x;
1286 event.m_y = (wxCoord)gdk_event->y - pt.y;
1287
1288 if ((win->m_wxwindow) && (win->GetLayoutDirection() == wxLayout_RightToLeft))
1289 {
1290 // origin in the upper right corner
1291 int window_width = gtk_pizza_get_rtl_offset( GTK_PIZZA(win->m_wxwindow) );
1292 event.m_x = window_width - event.m_x;
1293 }
1294
1295 event.SetEventObject( win );
1296 event.SetId( win->GetId() );
1297 event.SetTimestamp( gdk_event->time );
1298 }
1299
1300 static void AdjustEventButtonState(wxMouseEvent& event)
1301 {
1302 // GDK reports the old state of the button for a button press event, but
1303 // for compatibility with MSW and common sense we want m_leftDown be TRUE
1304 // for a LEFT_DOWN event, not FALSE, so we will invert
1305 // left/right/middleDown for the corresponding click events
1306
1307 if ((event.GetEventType() == wxEVT_LEFT_DOWN) ||
1308 (event.GetEventType() == wxEVT_LEFT_DCLICK) ||
1309 (event.GetEventType() == wxEVT_LEFT_UP))
1310 {
1311 event.m_leftDown = !event.m_leftDown;
1312 return;
1313 }
1314
1315 if ((event.GetEventType() == wxEVT_MIDDLE_DOWN) ||
1316 (event.GetEventType() == wxEVT_MIDDLE_DCLICK) ||
1317 (event.GetEventType() == wxEVT_MIDDLE_UP))
1318 {
1319 event.m_middleDown = !event.m_middleDown;
1320 return;
1321 }
1322
1323 if ((event.GetEventType() == wxEVT_RIGHT_DOWN) ||
1324 (event.GetEventType() == wxEVT_RIGHT_DCLICK) ||
1325 (event.GetEventType() == wxEVT_RIGHT_UP))
1326 {
1327 event.m_rightDown = !event.m_rightDown;
1328 return;
1329 }
1330 }
1331
1332 // find the window to send the mouse event too
1333 static
1334 wxWindowGTK *FindWindowForMouseEvent(wxWindowGTK *win, wxCoord& x, wxCoord& y)
1335 {
1336 wxCoord xx = x;
1337 wxCoord yy = y;
1338
1339 if (win->m_wxwindow)
1340 {
1341 GtkPizza *pizza = GTK_PIZZA(win->m_wxwindow);
1342 xx += gtk_pizza_get_xoffset( pizza );
1343 yy += gtk_pizza_get_yoffset( pizza );
1344 }
1345
1346 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
1347 while (node)
1348 {
1349 wxWindowGTK *child = node->GetData();
1350
1351 node = node->GetNext();
1352 if (!child->IsShown())
1353 continue;
1354
1355 if (child->IsTransparentForMouse())
1356 {
1357 // wxStaticBox is transparent in the box itself
1358 int xx1 = child->m_x;
1359 int yy1 = child->m_y;
1360 int xx2 = child->m_x + child->m_width;
1361 int yy2 = child->m_y + child->m_height;
1362
1363 // left
1364 if (((xx >= xx1) && (xx <= xx1+10) && (yy >= yy1) && (yy <= yy2)) ||
1365 // right
1366 ((xx >= xx2-10) && (xx <= xx2) && (yy >= yy1) && (yy <= yy2)) ||
1367 // top
1368 ((xx >= xx1) && (xx <= xx2) && (yy >= yy1) && (yy <= yy1+10)) ||
1369 // bottom
1370 ((xx >= xx1) && (xx <= xx2) && (yy >= yy2-1) && (yy <= yy2)))
1371 {
1372 win = child;
1373 x -= child->m_x;
1374 y -= child->m_y;
1375 break;
1376 }
1377
1378 }
1379 else
1380 {
1381 if ((child->m_wxwindow == (GtkWidget*) NULL) &&
1382 (child->m_x <= xx) &&
1383 (child->m_y <= yy) &&
1384 (child->m_x+child->m_width >= xx) &&
1385 (child->m_y+child->m_height >= yy))
1386 {
1387 win = child;
1388 x -= child->m_x;
1389 y -= child->m_y;
1390 break;
1391 }
1392 }
1393 }
1394
1395 return win;
1396 }
1397
1398 // ----------------------------------------------------------------------------
1399 // common event handlers helpers
1400 // ----------------------------------------------------------------------------
1401
1402 bool wxWindowGTK::GTKProcessEvent(wxEvent& event) const
1403 {
1404 // nothing special at this level
1405 return GetEventHandler()->ProcessEvent(event);
1406 }
1407
1408 int wxWindowGTK::GTKCallbackCommonPrologue(GdkEventAny *event) const
1409 {
1410 DEBUG_MAIN_THREAD
1411
1412 // don't need to install idle handler, its done from "event" signal
1413
1414 if (!m_hasVMT)
1415 return FALSE;
1416 if (g_blockEventsOnDrag)
1417 return TRUE;
1418 if (g_blockEventsOnScroll)
1419 return TRUE;
1420
1421 if (!GTKIsOwnWindow(event->window))
1422 return FALSE;
1423
1424 return -1;
1425 }
1426
1427 // overloads for all GDK event types we use here: we need to have this as
1428 // GdkEventXXX can't be implicitly cast to GdkEventAny even if it, in fact,
1429 // derives from it in the sense that the structs have the same layout
1430 #define wxDEFINE_COMMON_PROLOGUE_OVERLOAD(T) \
1431 static int wxGtkCallbackCommonPrologue(T *event, wxWindowGTK *win) \
1432 { \
1433 return win->GTKCallbackCommonPrologue((GdkEventAny *)event); \
1434 }
1435
1436 wxDEFINE_COMMON_PROLOGUE_OVERLOAD(GdkEventButton)
1437 wxDEFINE_COMMON_PROLOGUE_OVERLOAD(GdkEventMotion)
1438 wxDEFINE_COMMON_PROLOGUE_OVERLOAD(GdkEventCrossing)
1439
1440 #undef wxDEFINE_COMMON_PROLOGUE_OVERLOAD
1441
1442 #define wxCOMMON_CALLBACK_PROLOGUE(event, win) \
1443 const int rc = wxGtkCallbackCommonPrologue(event, win); \
1444 if ( rc != -1 ) \
1445 return rc
1446
1447 // send the wxChildFocusEvent and wxFocusEvent, common code of
1448 // gtk_window_focus_in_callback() and SetFocus()
1449 static bool DoSendFocusEvents(wxWindow *win)
1450 {
1451 // Notify the parent keeping track of focus for the kbd navigation
1452 // purposes that we got it.
1453 wxChildFocusEvent eventChildFocus(win);
1454 (void)win->GetEventHandler()->ProcessEvent(eventChildFocus);
1455
1456 wxFocusEvent eventFocus(wxEVT_SET_FOCUS, win->GetId());
1457 eventFocus.SetEventObject(win);
1458
1459 return win->GetEventHandler()->ProcessEvent(eventFocus);
1460 }
1461
1462 // all event handlers must have C linkage as they're called from GTK+ C code
1463 extern "C"
1464 {
1465
1466 //-----------------------------------------------------------------------------
1467 // "button_press_event"
1468 //-----------------------------------------------------------------------------
1469
1470 static gboolean
1471 gtk_window_button_press_callback( GtkWidget *widget,
1472 GdkEventButton *gdk_event,
1473 wxWindowGTK *win )
1474 {
1475 wxCOMMON_CALLBACK_PROLOGUE(gdk_event, win);
1476
1477 if (win->m_wxwindow && (g_focusWindow != win) && win->AcceptsFocus())
1478 {
1479 gtk_widget_grab_focus( win->m_wxwindow );
1480 }
1481
1482 // GDK sends surplus button down events
1483 // before a double click event. We
1484 // need to filter these out.
1485 if (gdk_event->type == GDK_BUTTON_PRESS)
1486 {
1487 GdkEvent *peek_event = gdk_event_peek();
1488 if (peek_event)
1489 {
1490 if ((peek_event->type == GDK_2BUTTON_PRESS) ||
1491 (peek_event->type == GDK_3BUTTON_PRESS))
1492 {
1493 gdk_event_free( peek_event );
1494 return TRUE;
1495 }
1496 else
1497 {
1498 gdk_event_free( peek_event );
1499 }
1500 }
1501 }
1502
1503 wxEventType event_type = wxEVT_NULL;
1504
1505 // GdkDisplay is a GTK+ 2.2.0 thing
1506 #if defined(__WXGTK20__) && GTK_CHECK_VERSION(2, 2, 0)
1507 if ( gdk_event->type == GDK_2BUTTON_PRESS &&
1508 !gtk_check_version(2,2,0) &&
1509 gdk_event->button >= 1 && gdk_event->button <= 3 )
1510 {
1511 // Reset GDK internal timestamp variables in order to disable GDK
1512 // triple click events. GDK will then next time believe no button has
1513 // been clicked just before, and send a normal button click event.
1514 GdkDisplay* display = gtk_widget_get_display (widget);
1515 display->button_click_time[1] = 0;
1516 display->button_click_time[0] = 0;
1517 }
1518 #endif // GTK 2+
1519
1520 if (gdk_event->button == 1)
1521 {
1522 // note that GDK generates triple click events which are not supported
1523 // by wxWidgets but still have to be passed to the app as otherwise
1524 // clicks would simply go missing
1525 switch (gdk_event->type)
1526 {
1527 // we shouldn't get triple clicks at all for GTK2 because we
1528 // suppress them artificially using the code above but we still
1529 // should map them to something for GTK1 and not just ignore them
1530 // as this would lose clicks
1531 case GDK_3BUTTON_PRESS: // we could also map this to DCLICK...
1532 case GDK_BUTTON_PRESS:
1533 event_type = wxEVT_LEFT_DOWN;
1534 break;
1535
1536 case GDK_2BUTTON_PRESS:
1537 event_type = wxEVT_LEFT_DCLICK;
1538 break;
1539
1540 default:
1541 // just to silence gcc warnings
1542 ;
1543 }
1544 }
1545 else if (gdk_event->button == 2)
1546 {
1547 switch (gdk_event->type)
1548 {
1549 case GDK_3BUTTON_PRESS:
1550 case GDK_BUTTON_PRESS:
1551 event_type = wxEVT_MIDDLE_DOWN;
1552 break;
1553
1554 case GDK_2BUTTON_PRESS:
1555 event_type = wxEVT_MIDDLE_DCLICK;
1556 break;
1557
1558 default:
1559 ;
1560 }
1561 }
1562 else if (gdk_event->button == 3)
1563 {
1564 switch (gdk_event->type)
1565 {
1566 case GDK_3BUTTON_PRESS:
1567 case GDK_BUTTON_PRESS:
1568 event_type = wxEVT_RIGHT_DOWN;
1569 break;
1570
1571 case GDK_2BUTTON_PRESS:
1572 event_type = wxEVT_RIGHT_DCLICK;
1573 break;
1574
1575 default:
1576 ;
1577 }
1578 }
1579 else if (gdk_event->button == 4 || gdk_event->button == 5)
1580 {
1581 if (gdk_event->type == GDK_BUTTON_PRESS )
1582 {
1583 event_type = wxEVT_MOUSEWHEEL;
1584 }
1585 }
1586
1587 if ( event_type == wxEVT_NULL )
1588 {
1589 // unknown mouse button or click type
1590 return FALSE;
1591 }
1592
1593 wxMouseEvent event( event_type );
1594 InitMouseEvent( win, event, gdk_event );
1595
1596 AdjustEventButtonState(event);
1597
1598 // wxListBox actually gets mouse events from the item, so we need to give it
1599 // a chance to correct this
1600 win->FixUpMouseEvent(widget, event.m_x, event.m_y);
1601
1602 // find the correct window to send the event to: it may be a different one
1603 // from the one which got it at GTK+ level because some controls don't have
1604 // their own X window and thus cannot get any events.
1605 if ( !g_captureWindow )
1606 win = FindWindowForMouseEvent(win, event.m_x, event.m_y);
1607
1608 // reset the event object and id in case win changed.
1609 event.SetEventObject( win );
1610 event.SetId( win->GetId() );
1611
1612 if (win->GTKProcessEvent( event ))
1613 {
1614 return TRUE;
1615 }
1616
1617 if (event_type == wxEVT_RIGHT_DOWN)
1618 {
1619 // generate a "context menu" event: this is similar to right mouse
1620 // click under many GUIs except that it is generated differently
1621 // (right up under MSW, ctrl-click under Mac, right down here) and
1622 //
1623 // (a) it's a command event and so is propagated to the parent
1624 // (b) under some ports it can be generated from kbd too
1625 // (c) it uses screen coords (because of (a))
1626 wxContextMenuEvent evtCtx(
1627 wxEVT_CONTEXT_MENU,
1628 win->GetId(),
1629 win->ClientToScreen(event.GetPosition()));
1630 evtCtx.SetEventObject(win);
1631 return win->GTKProcessEvent(evtCtx);
1632 }
1633
1634 return FALSE;
1635 }
1636
1637 //-----------------------------------------------------------------------------
1638 // "button_release_event"
1639 //-----------------------------------------------------------------------------
1640
1641 static gboolean
1642 gtk_window_button_release_callback( GtkWidget *widget,
1643 GdkEventButton *gdk_event,
1644 wxWindowGTK *win )
1645 {
1646 wxCOMMON_CALLBACK_PROLOGUE(gdk_event, win);
1647
1648 wxEventType event_type = wxEVT_NULL;
1649
1650 switch (gdk_event->button)
1651 {
1652 case 1:
1653 event_type = wxEVT_LEFT_UP;
1654 break;
1655
1656 case 2:
1657 event_type = wxEVT_MIDDLE_UP;
1658 break;
1659
1660 case 3:
1661 event_type = wxEVT_RIGHT_UP;
1662 break;
1663
1664 default:
1665 // unknown button, don't process
1666 return FALSE;
1667 }
1668
1669 wxMouseEvent event( event_type );
1670 InitMouseEvent( win, event, gdk_event );
1671
1672 AdjustEventButtonState(event);
1673
1674 // same wxListBox hack as above
1675 win->FixUpMouseEvent(widget, event.m_x, event.m_y);
1676
1677 if ( !g_captureWindow )
1678 win = FindWindowForMouseEvent(win, event.m_x, event.m_y);
1679
1680 // reset the event object and id in case win changed.
1681 event.SetEventObject( win );
1682 event.SetId( win->GetId() );
1683
1684 return win->GTKProcessEvent(event);
1685 }
1686
1687 //-----------------------------------------------------------------------------
1688 // "motion_notify_event"
1689 //-----------------------------------------------------------------------------
1690
1691 static gboolean
1692 gtk_window_motion_notify_callback( GtkWidget *widget,
1693 GdkEventMotion *gdk_event,
1694 wxWindowGTK *win )
1695 {
1696 wxCOMMON_CALLBACK_PROLOGUE(gdk_event, win);
1697
1698 if (gdk_event->is_hint)
1699 {
1700 int x = 0;
1701 int y = 0;
1702 GdkModifierType state;
1703 gdk_window_get_pointer(gdk_event->window, &x, &y, &state);
1704 gdk_event->x = x;
1705 gdk_event->y = y;
1706 }
1707
1708 wxMouseEvent event( wxEVT_MOTION );
1709 InitMouseEvent(win, event, gdk_event);
1710
1711 if ( g_captureWindow )
1712 {
1713 // synthesise a mouse enter or leave event if needed
1714 GdkWindow *winUnderMouse = gdk_window_at_pointer(NULL, NULL);
1715 // This seems to be necessary and actually been added to
1716 // GDK itself in version 2.0.X
1717 gdk_flush();
1718
1719 bool hasMouse = winUnderMouse == gdk_event->window;
1720 if ( hasMouse != g_captureWindowHasMouse )
1721 {
1722 // the mouse changed window
1723 g_captureWindowHasMouse = hasMouse;
1724
1725 wxMouseEvent eventM(g_captureWindowHasMouse ? wxEVT_ENTER_WINDOW
1726 : wxEVT_LEAVE_WINDOW);
1727 InitMouseEvent(win, eventM, gdk_event);
1728 eventM.SetEventObject(win);
1729 win->GTKProcessEvent(eventM);
1730 }
1731 }
1732 else // no capture
1733 {
1734 win = FindWindowForMouseEvent(win, event.m_x, event.m_y);
1735
1736 // reset the event object and id in case win changed.
1737 event.SetEventObject( win );
1738 event.SetId( win->GetId() );
1739 }
1740
1741 if ( !g_captureWindow )
1742 {
1743 wxSetCursorEvent cevent( event.m_x, event.m_y );
1744 if (win->GTKProcessEvent( cevent ))
1745 {
1746 win->SetCursor( cevent.GetCursor() );
1747 }
1748 }
1749
1750 return win->GTKProcessEvent(event);
1751 }
1752
1753 //-----------------------------------------------------------------------------
1754 // "scroll_event", (mouse wheel event)
1755 //-----------------------------------------------------------------------------
1756
1757 static gboolean
1758 window_scroll_event(GtkWidget*, GdkEventScroll* gdk_event, wxWindow* win)
1759 {
1760 DEBUG_MAIN_THREAD
1761
1762 // don't need to install idle handler, its done from "event" signal
1763
1764 if (gdk_event->direction != GDK_SCROLL_UP &&
1765 gdk_event->direction != GDK_SCROLL_DOWN)
1766 {
1767 return false;
1768 }
1769
1770 wxMouseEvent event(wxEVT_MOUSEWHEEL);
1771 // Can't use InitMouse macro because scroll events don't have button
1772 event.SetTimestamp( gdk_event->time );
1773 event.m_shiftDown = (gdk_event->state & GDK_SHIFT_MASK);
1774 event.m_controlDown = (gdk_event->state & GDK_CONTROL_MASK);
1775 event.m_altDown = (gdk_event->state & GDK_MOD1_MASK);
1776 event.m_metaDown = (gdk_event->state & GDK_MOD2_MASK);
1777 event.m_leftDown = (gdk_event->state & GDK_BUTTON1_MASK);
1778 event.m_middleDown = (gdk_event->state & GDK_BUTTON2_MASK);
1779 event.m_rightDown = (gdk_event->state & GDK_BUTTON3_MASK);
1780 event.m_linesPerAction = 3;
1781 event.m_wheelDelta = 120;
1782 if (gdk_event->direction == GDK_SCROLL_UP)
1783 event.m_wheelRotation = 120;
1784 else
1785 event.m_wheelRotation = -120;
1786
1787 wxPoint pt = win->GetClientAreaOrigin();
1788 event.m_x = (wxCoord)gdk_event->x - pt.x;
1789 event.m_y = (wxCoord)gdk_event->y - pt.y;
1790
1791 event.SetEventObject( win );
1792 event.SetId( win->GetId() );
1793 event.SetTimestamp( gdk_event->time );
1794
1795 return win->GTKProcessEvent(event);
1796 }
1797
1798 //-----------------------------------------------------------------------------
1799 // "popup-menu"
1800 //-----------------------------------------------------------------------------
1801
1802 static gboolean wxgtk_window_popup_menu_callback(GtkWidget*, wxWindowGTK* win)
1803 {
1804 wxContextMenuEvent event(wxEVT_CONTEXT_MENU, win->GetId(), wxPoint(-1, -1));
1805 event.SetEventObject(win);
1806 return win->GTKProcessEvent(event);
1807 }
1808
1809 //-----------------------------------------------------------------------------
1810 // "focus_in_event"
1811 //-----------------------------------------------------------------------------
1812
1813 static gboolean
1814 gtk_window_focus_in_callback( GtkWidget *widget,
1815 GdkEventFocus *WXUNUSED(event),
1816 wxWindow *win )
1817 {
1818 DEBUG_MAIN_THREAD
1819
1820 // don't need to install idle handler, its done from "event" signal
1821
1822 if (win->m_imData)
1823 gtk_im_context_focus_in(win->m_imData->context);
1824
1825 g_focusWindowLast =
1826 g_focusWindow = win;
1827
1828 wxLogTrace(TRACE_FOCUS,
1829 _T("%s: focus in"), win->GetName().c_str());
1830
1831 #ifdef HAVE_XIM
1832 if (win->m_ic)
1833 gdk_im_begin(win->m_ic, win->m_wxwindow->window);
1834 #endif
1835
1836 #if wxUSE_CARET
1837 // caret needs to be informed about focus change
1838 wxCaret *caret = win->GetCaret();
1839 if ( caret )
1840 {
1841 caret->OnSetFocus();
1842 }
1843 #endif // wxUSE_CARET
1844
1845 gboolean ret = FALSE;
1846
1847 // does the window itself think that it has the focus?
1848 if ( !win->m_hasFocus )
1849 {
1850 // not yet, notify it
1851 win->m_hasFocus = true;
1852
1853 (void)DoSendFocusEvents(win);
1854
1855 ret = TRUE;
1856 }
1857
1858 // Disable default focus handling for custom windows
1859 // since the default GTK+ handler issues a repaint
1860 if (win->m_wxwindow)
1861 return ret;
1862
1863 return FALSE;
1864 }
1865
1866 //-----------------------------------------------------------------------------
1867 // "focus_out_event"
1868 //-----------------------------------------------------------------------------
1869
1870 static gboolean
1871 gtk_window_focus_out_callback( GtkWidget *widget,
1872 GdkEventFocus *gdk_event,
1873 wxWindowGTK *win )
1874 {
1875 DEBUG_MAIN_THREAD
1876
1877 // don't need to install idle handler, its done from "event" signal
1878
1879 if (win->m_imData)
1880 gtk_im_context_focus_out(win->m_imData->context);
1881
1882 wxLogTrace( TRACE_FOCUS,
1883 _T("%s: focus out"), win->GetName().c_str() );
1884
1885
1886 wxWindowGTK *winFocus = wxFindFocusedChild(win);
1887 if ( winFocus )
1888 win = winFocus;
1889
1890 g_focusWindow = (wxWindowGTK *)NULL;
1891
1892 #ifdef HAVE_XIM
1893 if (win->m_ic)
1894 gdk_im_end();
1895 #endif
1896
1897 #if wxUSE_CARET
1898 // caret needs to be informed about focus change
1899 wxCaret *caret = win->GetCaret();
1900 if ( caret )
1901 {
1902 caret->OnKillFocus();
1903 }
1904 #endif // wxUSE_CARET
1905
1906 gboolean ret = FALSE;
1907
1908 // don't send the window a kill focus event if it thinks that it doesn't
1909 // have focus already
1910 if ( win->m_hasFocus )
1911 {
1912 win->m_hasFocus = false;
1913
1914 wxFocusEvent event( wxEVT_KILL_FOCUS, win->GetId() );
1915 event.SetEventObject( win );
1916
1917 (void)win->GTKProcessEvent( event );
1918
1919 ret = TRUE;
1920 }
1921
1922 // Disable default focus handling for custom windows
1923 // since the default GTK+ handler issues a repaint
1924 if (win->m_wxwindow)
1925 return ret;
1926
1927 return FALSE;
1928 }
1929
1930 //-----------------------------------------------------------------------------
1931 // "enter_notify_event"
1932 //-----------------------------------------------------------------------------
1933
1934 static gboolean
1935 gtk_window_enter_callback( GtkWidget *widget,
1936 GdkEventCrossing *gdk_event,
1937 wxWindowGTK *win )
1938 {
1939 wxCOMMON_CALLBACK_PROLOGUE(gdk_event, win);
1940
1941 // Event was emitted after a grab
1942 if (gdk_event->mode != GDK_CROSSING_NORMAL) return FALSE;
1943
1944 int x = 0;
1945 int y = 0;
1946 GdkModifierType state = (GdkModifierType)0;
1947
1948 gdk_window_get_pointer( widget->window, &x, &y, &state );
1949
1950 wxMouseEvent event( wxEVT_ENTER_WINDOW );
1951 InitMouseEvent(win, event, gdk_event);
1952 wxPoint pt = win->GetClientAreaOrigin();
1953 event.m_x = x + pt.x;
1954 event.m_y = y + pt.y;
1955
1956 if ( !g_captureWindow )
1957 {
1958 wxSetCursorEvent cevent( event.m_x, event.m_y );
1959 if (win->GTKProcessEvent( cevent ))
1960 {
1961 win->SetCursor( cevent.GetCursor() );
1962 }
1963 }
1964
1965 return win->GTKProcessEvent(event);
1966 }
1967
1968 //-----------------------------------------------------------------------------
1969 // "leave_notify_event"
1970 //-----------------------------------------------------------------------------
1971
1972 static gboolean
1973 gtk_window_leave_callback( GtkWidget *widget,
1974 GdkEventCrossing *gdk_event,
1975 wxWindowGTK *win )
1976 {
1977 wxCOMMON_CALLBACK_PROLOGUE(gdk_event, win);
1978
1979 // Event was emitted after an ungrab
1980 if (gdk_event->mode != GDK_CROSSING_NORMAL) return FALSE;
1981
1982 wxMouseEvent event( wxEVT_LEAVE_WINDOW );
1983 event.SetTimestamp( gdk_event->time );
1984 event.SetEventObject( win );
1985
1986 int x = 0;
1987 int y = 0;
1988 GdkModifierType state = (GdkModifierType)0;
1989
1990 gdk_window_get_pointer( widget->window, &x, &y, &state );
1991
1992 event.m_shiftDown = (state & GDK_SHIFT_MASK) != 0;
1993 event.m_controlDown = (state & GDK_CONTROL_MASK) != 0;
1994 event.m_altDown = (state & GDK_MOD1_MASK) != 0;
1995 event.m_metaDown = (state & GDK_MOD2_MASK) != 0;
1996 event.m_leftDown = (state & GDK_BUTTON1_MASK) != 0;
1997 event.m_middleDown = (state & GDK_BUTTON2_MASK) != 0;
1998 event.m_rightDown = (state & GDK_BUTTON3_MASK) != 0;
1999
2000 wxPoint pt = win->GetClientAreaOrigin();
2001 event.m_x = x + pt.x;
2002 event.m_y = y + pt.y;
2003
2004 return win->GTKProcessEvent(event);
2005 }
2006
2007 //-----------------------------------------------------------------------------
2008 // "value_changed" from scrollbar
2009 //-----------------------------------------------------------------------------
2010
2011 static void
2012 gtk_scrollbar_value_changed(GtkRange* range, wxWindow* win)
2013 {
2014 wxEventType eventType = win->GetScrollEventType(range);
2015 if (eventType != wxEVT_NULL)
2016 {
2017 // Convert scroll event type to scrollwin event type
2018 eventType += wxEVT_SCROLLWIN_TOP - wxEVT_SCROLL_TOP;
2019
2020 // find the scrollbar which generated the event
2021 wxWindowGTK::ScrollDir dir = win->ScrollDirFromRange(range);
2022
2023 // generate the corresponding wx event
2024 const int orient = win->OrientFromScrollDir(dir);
2025 wxScrollWinEvent event(eventType, win->GetScrollPos(orient), orient);
2026 event.SetEventObject(win);
2027
2028 win->m_blockValueChanged[dir] = true;
2029 win->GTKProcessEvent(event);
2030 win->m_blockValueChanged[dir] = false;
2031 }
2032 }
2033
2034 //-----------------------------------------------------------------------------
2035 // "button_press_event" from scrollbar
2036 //-----------------------------------------------------------------------------
2037
2038 static gboolean
2039 gtk_scrollbar_button_press_event(GtkRange*, GdkEventButton*, wxWindow* win)
2040 {
2041 DEBUG_MAIN_THREAD
2042
2043 // don't need to install idle handler, its done from "event" signal
2044
2045 g_blockEventsOnScroll = true;
2046 win->m_mouseButtonDown = true;
2047
2048 return false;
2049 }
2050
2051 //-----------------------------------------------------------------------------
2052 // "event_after" from scrollbar
2053 //-----------------------------------------------------------------------------
2054
2055 static void
2056 gtk_scrollbar_event_after(GtkRange* range, GdkEvent* event, wxWindow* win)
2057 {
2058 if (event->type == GDK_BUTTON_RELEASE)
2059 {
2060 g_signal_handlers_block_by_func(range, (void*)gtk_scrollbar_event_after, win);
2061
2062 const int orient = win->OrientFromScrollDir(
2063 win->ScrollDirFromRange(range));
2064 wxScrollWinEvent event(wxEVT_SCROLLWIN_THUMBRELEASE, win->GetScrollPos(orient), orient);
2065 event.SetEventObject(win);
2066 win->GTKProcessEvent(event);
2067 }
2068 }
2069
2070 //-----------------------------------------------------------------------------
2071 // "button_release_event" from scrollbar
2072 //-----------------------------------------------------------------------------
2073
2074 static gboolean
2075 gtk_scrollbar_button_release_event(GtkRange* range, GdkEventButton*, wxWindow* win)
2076 {
2077 DEBUG_MAIN_THREAD
2078
2079 g_blockEventsOnScroll = false;
2080 win->m_mouseButtonDown = false;
2081 // If thumb tracking
2082 if (win->m_isScrolling)
2083 {
2084 win->m_isScrolling = false;
2085 // Hook up handler to send thumb release event after this emission is finished.
2086 // To allow setting scroll position from event handler, sending event must
2087 // be deferred until after the GtkRange handler for this signal has run
2088 g_signal_handlers_unblock_by_func(range, (void*)gtk_scrollbar_event_after, win);
2089 }
2090
2091 return false;
2092 }
2093
2094 //-----------------------------------------------------------------------------
2095 // "realize" from m_widget
2096 //-----------------------------------------------------------------------------
2097
2098 /* We cannot set colours and fonts before the widget has
2099 been realized, so we do this directly after realization. */
2100
2101 static void
2102 gtk_window_realized_callback( GtkWidget *m_widget, wxWindow *win )
2103 {
2104 DEBUG_MAIN_THREAD
2105
2106 if (g_isIdle)
2107 wxapp_install_idle_handler();
2108
2109 if (win->m_imData)
2110 {
2111 GtkPizza *pizza = GTK_PIZZA( m_widget );
2112 gtk_im_context_set_client_window( win->m_imData->context,
2113 pizza->bin_window );
2114 }
2115
2116 wxWindowCreateEvent event( win );
2117 event.SetEventObject( win );
2118 win->GTKProcessEvent( event );
2119 }
2120
2121 //-----------------------------------------------------------------------------
2122 // "size_allocate"
2123 //-----------------------------------------------------------------------------
2124
2125 static
2126 void gtk_window_size_callback( GtkWidget *WXUNUSED(widget),
2127 GtkAllocation *alloc,
2128 wxWindow *win )
2129 {
2130 if (g_isIdle)
2131 wxapp_install_idle_handler();
2132
2133 int client_width = 0;
2134 int client_height = 0;
2135 win->GetClientSize( &client_width, &client_height );
2136 if ((client_width == win->m_oldClientWidth) && (client_height == win->m_oldClientHeight))
2137 return;
2138
2139 #if 0
2140 wxPrintf( wxT("size_allocate ") );
2141 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
2142 wxPrintf( win->GetClassInfo()->GetClassName() );
2143 wxPrintf( wxT(" %d %d %d %d\n"),
2144 alloc->x,
2145 alloc->y,
2146 alloc->width,
2147 alloc->height );
2148 #endif
2149
2150 GTK_PIZZA(win->m_wxwindow)->m_width = win->GetClientSize().x;
2151
2152 win->m_oldClientWidth = client_width;
2153 win->m_oldClientHeight = client_height;
2154
2155 if (!win->m_nativeSizeEvent)
2156 {
2157 wxSizeEvent event( win->GetSize(), win->GetId() );
2158 event.SetEventObject( win );
2159 win->GTKProcessEvent( event );
2160 }
2161 }
2162
2163
2164 #ifdef HAVE_XIM
2165 #define WXUNUSED_UNLESS_XIM(param) param
2166 #else
2167 #define WXUNUSED_UNLESS_XIM(param) WXUNUSED(param)
2168 #endif
2169
2170 /* Resize XIM window */
2171 static
2172 void gtk_wxwindow_size_callback( GtkWidget* WXUNUSED_UNLESS_XIM(widget),
2173 GtkAllocation* WXUNUSED_UNLESS_XIM(alloc),
2174 wxWindowGTK* WXUNUSED_UNLESS_XIM(win) )
2175 {
2176 if (g_isIdle)
2177 wxapp_install_idle_handler();
2178
2179 #ifdef HAVE_XIM
2180 if (!win->m_ic)
2181 return;
2182
2183 if (gdk_ic_get_style (win->m_ic) & GDK_IM_PREEDIT_POSITION)
2184 {
2185 gint width, height;
2186
2187 gdk_drawable_get_size (widget->window, &width, &height);
2188 win->m_icattr->preedit_area.width = width;
2189 win->m_icattr->preedit_area.height = height;
2190 gdk_ic_set_attr (win->m_ic, win->m_icattr, GDK_IC_PREEDIT_AREA);
2191 }
2192 #endif // HAVE_XIM
2193 }
2194
2195 //-----------------------------------------------------------------------------
2196 // "realize" from m_wxwindow
2197 //-----------------------------------------------------------------------------
2198
2199 /* Initialize XIM support */
2200
2201 static void
2202 gtk_wxwindow_realized_callback( GtkWidget * WXUNUSED_UNLESS_XIM(widget),
2203 wxWindowGTK * WXUNUSED_UNLESS_XIM(win) )
2204 {
2205 if (g_isIdle)
2206 wxapp_install_idle_handler();
2207
2208 #ifdef HAVE_XIM
2209 if (win->m_ic) return;
2210 if (!widget) return;
2211 if (!gdk_im_ready()) return;
2212
2213 win->m_icattr = gdk_ic_attr_new();
2214 if (!win->m_icattr) return;
2215
2216 gint width, height;
2217 GdkEventMask mask;
2218 GdkColormap *colormap;
2219 GdkICAttr *attr = win->m_icattr;
2220 unsigned attrmask = GDK_IC_ALL_REQ;
2221 GdkIMStyle style;
2222 GdkIMStyle supported_style = (GdkIMStyle)
2223 (GDK_IM_PREEDIT_NONE |
2224 GDK_IM_PREEDIT_NOTHING |
2225 GDK_IM_PREEDIT_POSITION |
2226 GDK_IM_STATUS_NONE |
2227 GDK_IM_STATUS_NOTHING);
2228
2229 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET)
2230 supported_style = (GdkIMStyle)(supported_style & ~GDK_IM_PREEDIT_POSITION);
2231
2232 attr->style = style = gdk_im_decide_style (supported_style);
2233 attr->client_window = widget->window;
2234
2235 if ((colormap = gtk_widget_get_colormap (widget)) !=
2236 gtk_widget_get_default_colormap ())
2237 {
2238 attrmask |= GDK_IC_PREEDIT_COLORMAP;
2239 attr->preedit_colormap = colormap;
2240 }
2241
2242 attrmask |= GDK_IC_PREEDIT_FOREGROUND;
2243 attrmask |= GDK_IC_PREEDIT_BACKGROUND;
2244 attr->preedit_foreground = widget->style->fg[GTK_STATE_NORMAL];
2245 attr->preedit_background = widget->style->base[GTK_STATE_NORMAL];
2246
2247 switch (style & GDK_IM_PREEDIT_MASK)
2248 {
2249 case GDK_IM_PREEDIT_POSITION:
2250 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET)
2251 {
2252 g_warning ("over-the-spot style requires fontset");
2253 break;
2254 }
2255
2256 gdk_drawable_get_size (widget->window, &width, &height);
2257
2258 attrmask |= GDK_IC_PREEDIT_POSITION_REQ;
2259 attr->spot_location.x = 0;
2260 attr->spot_location.y = height;
2261 attr->preedit_area.x = 0;
2262 attr->preedit_area.y = 0;
2263 attr->preedit_area.width = width;
2264 attr->preedit_area.height = height;
2265 attr->preedit_fontset = widget->style->font;
2266
2267 break;
2268 }
2269
2270 win->m_ic = gdk_ic_new (attr, (GdkICAttributesType)attrmask);
2271
2272 if (win->m_ic == NULL)
2273 g_warning ("Can't create input context.");
2274 else
2275 {
2276 mask = gdk_window_get_events (widget->window);
2277 mask = (GdkEventMask)(mask | gdk_ic_get_events (win->m_ic));
2278 gdk_window_set_events (widget->window, mask);
2279
2280 if (GTK_WIDGET_HAS_FOCUS(widget))
2281 gdk_im_begin (win->m_ic, widget->window);
2282 }
2283 #endif // HAVE_XIM
2284 }
2285
2286 } // extern "C"
2287
2288 // ----------------------------------------------------------------------------
2289 // this wxWindowBase function is implemented here (in platform-specific file)
2290 // because it is static and so couldn't be made virtual
2291 // ----------------------------------------------------------------------------
2292
2293 wxWindow *wxWindowBase::DoFindFocus()
2294 {
2295 // the cast is necessary when we compile in wxUniversal mode
2296 return (wxWindow *)g_focusWindow;
2297 }
2298
2299 //-----------------------------------------------------------------------------
2300 // InsertChild for wxWindowGTK.
2301 //-----------------------------------------------------------------------------
2302
2303 /* Callback for wxWindowGTK. This very strange beast has to be used because
2304 * C++ has no virtual methods in a constructor. We have to emulate a
2305 * virtual function here as wxNotebook requires a different way to insert
2306 * a child in it. I had opted for creating a wxNotebookPage window class
2307 * which would have made this superfluous (such in the MDI window system),
2308 * but no-one was listening to me... */
2309
2310 static void wxInsertChildInWindow( wxWindowGTK* parent, wxWindowGTK* child )
2311 {
2312 /* the window might have been scrolled already, do we
2313 have to adapt the position */
2314 GtkPizza *pizza = GTK_PIZZA(parent->m_wxwindow);
2315 child->m_x += gtk_pizza_get_xoffset( pizza );
2316 child->m_y += gtk_pizza_get_yoffset( pizza );
2317
2318 gtk_pizza_put( GTK_PIZZA(parent->m_wxwindow),
2319 GTK_WIDGET(child->m_widget),
2320 child->m_x,
2321 child->m_y,
2322 child->m_width,
2323 child->m_height );
2324 }
2325
2326 //-----------------------------------------------------------------------------
2327 // global functions
2328 //-----------------------------------------------------------------------------
2329
2330 wxWindow *wxGetActiveWindow()
2331 {
2332 return wxWindow::FindFocus();
2333 }
2334
2335
2336 wxMouseState wxGetMouseState()
2337 {
2338 wxMouseState ms;
2339
2340 gint x;
2341 gint y;
2342 GdkModifierType mask;
2343
2344 gdk_window_get_pointer(NULL, &x, &y, &mask);
2345
2346 ms.SetX(x);
2347 ms.SetY(y);
2348 ms.SetLeftDown(mask & GDK_BUTTON1_MASK);
2349 ms.SetMiddleDown(mask & GDK_BUTTON2_MASK);
2350 ms.SetRightDown(mask & GDK_BUTTON3_MASK);
2351
2352 ms.SetControlDown(mask & GDK_CONTROL_MASK);
2353 ms.SetShiftDown(mask & GDK_SHIFT_MASK);
2354 ms.SetAltDown(mask & GDK_MOD1_MASK);
2355 ms.SetMetaDown(mask & GDK_MOD2_MASK);
2356
2357 return ms;
2358 }
2359
2360 //-----------------------------------------------------------------------------
2361 // wxWindowGTK
2362 //-----------------------------------------------------------------------------
2363
2364 // in wxUniv/MSW this class is abstract because it doesn't have DoPopupMenu()
2365 // method
2366 #ifdef __WXUNIVERSAL__
2367 IMPLEMENT_ABSTRACT_CLASS(wxWindowGTK, wxWindowBase)
2368 #else // __WXGTK__
2369 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
2370 #endif // __WXUNIVERSAL__/__WXGTK__
2371
2372 void wxWindowGTK::Init()
2373 {
2374 // GTK specific
2375 m_widget = (GtkWidget *) NULL;
2376 m_wxwindow = (GtkWidget *) NULL;
2377 m_focusWidget = (GtkWidget *) NULL;
2378
2379 // position/size
2380 m_x = 0;
2381 m_y = 0;
2382 m_width = 0;
2383 m_height = 0;
2384
2385 m_sizeSet = false;
2386 m_hasVMT = false;
2387 m_needParent = true;
2388 m_isBeingDeleted = false;
2389
2390 m_showOnIdle= false;
2391
2392 m_noExpose = false;
2393 m_nativeSizeEvent = false;
2394
2395 m_hasScrolling = false;
2396 m_isScrolling = false;
2397 m_mouseButtonDown = false;
2398 m_blockScrollEvent = false;
2399
2400 // initialize scrolling stuff
2401 for ( int dir = 0; dir < ScrollDir_Max; dir++ )
2402 {
2403 m_scrollBar[dir] = NULL;
2404 m_scrollPos[dir] = 0;
2405 m_blockValueChanged[dir] = false;
2406 }
2407
2408 m_oldClientWidth =
2409 m_oldClientHeight = 0;
2410
2411 m_resizing = false;
2412
2413 m_insertCallback = (wxInsertChildFunction) NULL;
2414
2415 m_acceptsFocus = false;
2416 m_hasFocus = false;
2417
2418 m_clipPaintRegion = false;
2419
2420 m_needsStyleChange = false;
2421
2422 m_cursor = *wxSTANDARD_CURSOR;
2423
2424 m_imData = NULL;
2425 m_dirtyTabOrder = false;
2426 }
2427
2428 wxWindowGTK::wxWindowGTK()
2429 {
2430 Init();
2431 }
2432
2433 wxWindowGTK::wxWindowGTK( wxWindow *parent,
2434 wxWindowID id,
2435 const wxPoint &pos,
2436 const wxSize &size,
2437 long style,
2438 const wxString &name )
2439 {
2440 Init();
2441
2442 Create( parent, id, pos, size, style, name );
2443 }
2444
2445 bool wxWindowGTK::Create( wxWindow *parent,
2446 wxWindowID id,
2447 const wxPoint &pos,
2448 const wxSize &size,
2449 long style,
2450 const wxString &name )
2451 {
2452 if (!PreCreation( parent, pos, size ) ||
2453 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
2454 {
2455 wxFAIL_MSG( wxT("wxWindowGTK creation failed") );
2456 return false;
2457 }
2458
2459 m_insertCallback = wxInsertChildInWindow;
2460
2461 m_widget = gtk_scrolled_window_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
2462 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
2463
2464 GtkScrolledWindow *scrolledWindow = GTK_SCROLLED_WINDOW(m_widget);
2465
2466 GtkScrolledWindowClass *scroll_class = GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget) );
2467 scroll_class->scrollbar_spacing = 0;
2468
2469 gtk_scrolled_window_set_policy( scrolledWindow, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
2470
2471 m_scrollBar[ScrollDir_Horz] = GTK_RANGE(scrolledWindow->hscrollbar);
2472 m_scrollBar[ScrollDir_Vert] = GTK_RANGE(scrolledWindow->vscrollbar);
2473 if (GetLayoutDirection() == wxLayout_RightToLeft)
2474 gtk_range_set_inverted( m_scrollBar[ScrollDir_Horz], TRUE );
2475
2476 m_wxwindow = gtk_pizza_new();
2477
2478 #ifndef __WXUNIVERSAL__
2479 GtkPizza *pizza = GTK_PIZZA(m_wxwindow);
2480
2481 if (HasFlag(wxRAISED_BORDER))
2482 {
2483 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_OUT );
2484 }
2485 else if (HasFlag(wxSUNKEN_BORDER))
2486 {
2487 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_IN );
2488 }
2489 else if (HasFlag(wxSIMPLE_BORDER))
2490 {
2491 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_THIN );
2492 }
2493 else
2494 {
2495 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_NONE );
2496 }
2497 #endif // __WXUNIVERSAL__
2498
2499 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
2500
2501 GTK_WIDGET_SET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
2502 m_acceptsFocus = true;
2503
2504 // connect various scroll-related events
2505 for ( int dir = 0; dir < ScrollDir_Max; dir++ )
2506 {
2507 // these handlers block mouse events to any window during scrolling
2508 // such as motion events and prevent GTK and wxWidgets from fighting
2509 // over where the slider should be
2510 g_signal_connect(m_scrollBar[dir], "button_press_event",
2511 G_CALLBACK(gtk_scrollbar_button_press_event), this);
2512 g_signal_connect(m_scrollBar[dir], "button_release_event",
2513 G_CALLBACK(gtk_scrollbar_button_release_event), this);
2514
2515 gulong handler_id = g_signal_connect(m_scrollBar[dir], "event_after",
2516 G_CALLBACK(gtk_scrollbar_event_after), this);
2517 g_signal_handler_block(m_scrollBar[dir], handler_id);
2518
2519 // these handlers get notified when scrollbar slider moves
2520 g_signal_connect(m_scrollBar[dir], "value_changed",
2521 G_CALLBACK(gtk_scrollbar_value_changed), this);
2522 }
2523
2524 gtk_widget_show( m_wxwindow );
2525
2526 if (m_parent)
2527 m_parent->DoAddChild( this );
2528
2529 m_focusWidget = m_wxwindow;
2530
2531 PostCreation();
2532
2533 return true;
2534 }
2535
2536 wxWindowGTK::~wxWindowGTK()
2537 {
2538 SendDestroyEvent();
2539
2540 if (g_focusWindow == this)
2541 g_focusWindow = NULL;
2542
2543 if ( g_delayedFocus == this )
2544 g_delayedFocus = NULL;
2545
2546 m_isBeingDeleted = true;
2547 m_hasVMT = false;
2548
2549 // destroy children before destroying this window itself
2550 DestroyChildren();
2551
2552 // unhook focus handlers to prevent stray events being
2553 // propagated to this (soon to be) dead object
2554 if (m_focusWidget != NULL)
2555 {
2556 g_signal_handlers_disconnect_by_func (m_focusWidget,
2557 (gpointer) gtk_window_focus_in_callback,
2558 this);
2559 g_signal_handlers_disconnect_by_func (m_focusWidget,
2560 (gpointer) gtk_window_focus_out_callback,
2561 this);
2562 }
2563
2564 if (m_widget)
2565 Show( false );
2566
2567 #ifdef HAVE_XIM
2568 if (m_ic)
2569 gdk_ic_destroy (m_ic);
2570 if (m_icattr)
2571 gdk_ic_attr_destroy (m_icattr);
2572 #endif
2573
2574 // delete before the widgets to avoid a crash on solaris
2575 delete m_imData;
2576
2577 if (m_wxwindow)
2578 {
2579 gtk_widget_destroy( m_wxwindow );
2580 m_wxwindow = (GtkWidget*) NULL;
2581 }
2582
2583 if (m_widget)
2584 {
2585 gtk_widget_destroy( m_widget );
2586 m_widget = (GtkWidget*) NULL;
2587 }
2588 }
2589
2590 bool wxWindowGTK::PreCreation( wxWindowGTK *parent, const wxPoint &pos, const wxSize &size )
2591 {
2592 wxCHECK_MSG( !m_needParent || parent, false, wxT("Need complete parent.") );
2593
2594 // Use either the given size, or the default if -1 is given.
2595 // See wxWindowBase for these functions.
2596 m_width = WidthDefault(size.x) ;
2597 m_height = HeightDefault(size.y);
2598
2599 m_x = (int)pos.x;
2600 m_y = (int)pos.y;
2601
2602 return true;
2603 }
2604
2605 void wxWindowGTK::PostCreation()
2606 {
2607 wxASSERT_MSG( (m_widget != NULL), wxT("invalid window") );
2608
2609 if (m_wxwindow)
2610 {
2611 if (!m_noExpose)
2612 {
2613 // these get reported to wxWidgets -> wxPaintEvent
2614
2615 g_signal_connect (m_wxwindow, "expose_event",
2616 G_CALLBACK (gtk_window_expose_callback), this);
2617
2618 if (GetLayoutDirection() == wxLayout_LeftToRight)
2619 gtk_widget_set_redraw_on_allocate( GTK_WIDGET(m_wxwindow), HasFlag( wxFULL_REPAINT_ON_RESIZE ) );
2620 }
2621
2622 // Create input method handler
2623 m_imData = new wxGtkIMData;
2624
2625 // Cannot handle drawing preedited text yet
2626 gtk_im_context_set_use_preedit( m_imData->context, FALSE );
2627
2628 g_signal_connect (m_imData->context, "commit",
2629 G_CALLBACK (gtk_wxwindow_commit_cb), this);
2630
2631 // these are called when the "sunken" or "raised" borders are drawn
2632 g_signal_connect (m_widget, "expose_event",
2633 G_CALLBACK (gtk_window_own_expose_callback), this);
2634 }
2635
2636 // focus handling
2637
2638 if (!GTK_IS_WINDOW(m_widget))
2639 {
2640 if (m_focusWidget == NULL)
2641 m_focusWidget = m_widget;
2642
2643 if (m_wxwindow)
2644 {
2645 g_signal_connect (m_focusWidget, "focus_in_event",
2646 G_CALLBACK (gtk_window_focus_in_callback), this);
2647 g_signal_connect (m_focusWidget, "focus_out_event",
2648 G_CALLBACK (gtk_window_focus_out_callback), this);
2649 }
2650 else
2651 {
2652 g_signal_connect_after (m_focusWidget, "focus_in_event",
2653 G_CALLBACK (gtk_window_focus_in_callback), this);
2654 g_signal_connect_after (m_focusWidget, "focus_out_event",
2655 G_CALLBACK (gtk_window_focus_out_callback), this);
2656 }
2657 }
2658
2659 // connect to the various key and mouse handlers
2660
2661 GtkWidget *connect_widget = GetConnectWidget();
2662
2663 ConnectWidget( connect_widget );
2664
2665 /* We cannot set colours, fonts and cursors before the widget has
2666 been realized, so we do this directly after realization */
2667 g_signal_connect (connect_widget, "realize",
2668 G_CALLBACK (gtk_window_realized_callback), this);
2669
2670 if (m_wxwindow)
2671 {
2672 // Catch native resize events
2673 g_signal_connect (m_wxwindow, "size_allocate",
2674 G_CALLBACK (gtk_window_size_callback), this);
2675
2676 // Initialize XIM support
2677 g_signal_connect (m_wxwindow, "realize",
2678 G_CALLBACK (gtk_wxwindow_realized_callback), this);
2679
2680 // And resize XIM window
2681 g_signal_connect (m_wxwindow, "size_allocate",
2682 G_CALLBACK (gtk_wxwindow_size_callback), this);
2683 }
2684
2685 if (GTK_IS_COMBO(m_widget))
2686 {
2687 GtkCombo *gcombo = GTK_COMBO(m_widget);
2688
2689 g_signal_connect (gcombo->entry, "size_request",
2690 G_CALLBACK (wxgtk_combo_size_request_callback),
2691 this);
2692 }
2693 #ifdef GTK_IS_FILE_CHOOSER_BUTTON
2694 else if (!gtk_check_version(2,6,0) && GTK_IS_FILE_CHOOSER_BUTTON(m_widget))
2695 {
2696 // If we connect to the "size_request" signal of a GtkFileChooserButton
2697 // then that control won't be sized properly when placed inside sizers
2698 // (this can be tested removing this elseif and running XRC or WIDGETS samples)
2699 // FIXME: what should be done here ?
2700 }
2701 #endif
2702 else
2703 {
2704 // This is needed if we want to add our windows into native
2705 // GTK controls, such as the toolbar. With this callback, the
2706 // toolbar gets to know the correct size (the one set by the
2707 // programmer). Sadly, it misbehaves for wxComboBox.
2708 g_signal_connect (m_widget, "size_request",
2709 G_CALLBACK (wxgtk_window_size_request_callback),
2710 this);
2711 }
2712
2713 InheritAttributes();
2714
2715 m_hasVMT = true;
2716
2717 SetLayoutDirection(wxLayout_Default);
2718
2719 // unless the window was created initially hidden (i.e. Hide() had been
2720 // called before Create()), we should show it at GTK+ level as well
2721 if ( IsShown() )
2722 gtk_widget_show( m_widget );
2723 }
2724
2725 void wxWindowGTK::ConnectWidget( GtkWidget *widget )
2726 {
2727 g_signal_connect (widget, "key_press_event",
2728 G_CALLBACK (gtk_window_key_press_callback), this);
2729 g_signal_connect (widget, "key_release_event",
2730 G_CALLBACK (gtk_window_key_release_callback), this);
2731 g_signal_connect (widget, "button_press_event",
2732 G_CALLBACK (gtk_window_button_press_callback), this);
2733 g_signal_connect (widget, "button_release_event",
2734 G_CALLBACK (gtk_window_button_release_callback), this);
2735 g_signal_connect (widget, "motion_notify_event",
2736 G_CALLBACK (gtk_window_motion_notify_callback), this);
2737 g_signal_connect (widget, "scroll_event",
2738 G_CALLBACK (window_scroll_event), this);
2739 g_signal_connect (widget, "popup_menu",
2740 G_CALLBACK (wxgtk_window_popup_menu_callback), this);
2741 g_signal_connect (widget, "enter_notify_event",
2742 G_CALLBACK (gtk_window_enter_callback), this);
2743 g_signal_connect (widget, "leave_notify_event",
2744 G_CALLBACK (gtk_window_leave_callback), this);
2745 }
2746
2747 bool wxWindowGTK::Destroy()
2748 {
2749 wxASSERT_MSG( (m_widget != NULL), wxT("invalid window") );
2750
2751 m_hasVMT = false;
2752
2753 return wxWindowBase::Destroy();
2754 }
2755
2756 void wxWindowGTK::DoMoveWindow(int x, int y, int width, int height)
2757 {
2758 // inform the parent to perform the move
2759 gtk_pizza_set_size( GTK_PIZZA(m_parent->m_wxwindow), m_widget, x, y, width, height );
2760
2761 }
2762
2763 void wxWindowGTK::DoSetSize( int x, int y, int width, int height, int sizeFlags )
2764 {
2765 wxASSERT_MSG( (m_widget != NULL), wxT("invalid window") );
2766 wxASSERT_MSG( (m_parent != NULL), wxT("wxWindowGTK::SetSize requires parent.\n") );
2767
2768 if (m_resizing) return; /* I don't like recursions */
2769 m_resizing = true;
2770
2771 int currentX, currentY;
2772 GetPosition(&currentX, &currentY);
2773 if (x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
2774 x = currentX;
2775 if (y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
2776 y = currentY;
2777 AdjustForParentClientOrigin(x, y, sizeFlags);
2778
2779 // calculate the best size if we should auto size the window
2780 if ( ((sizeFlags & wxSIZE_AUTO_WIDTH) && width == -1) ||
2781 ((sizeFlags & wxSIZE_AUTO_HEIGHT) && height == -1) )
2782 {
2783 const wxSize sizeBest = GetBestSize();
2784 if ( (sizeFlags & wxSIZE_AUTO_WIDTH) && width == -1 )
2785 width = sizeBest.x;
2786 if ( (sizeFlags & wxSIZE_AUTO_HEIGHT) && height == -1 )
2787 height = sizeBest.y;
2788 }
2789
2790 if (width != -1)
2791 m_width = width;
2792 if (height != -1)
2793 m_height = height;
2794
2795 int minWidth = GetMinWidth(),
2796 minHeight = GetMinHeight(),
2797 maxWidth = GetMaxWidth(),
2798 maxHeight = GetMaxHeight();
2799
2800 if ((minWidth != -1) && (m_width < minWidth )) m_width = minWidth;
2801 if ((minHeight != -1) && (m_height < minHeight)) m_height = minHeight;
2802 if ((maxWidth != -1) && (m_width > maxWidth )) m_width = maxWidth;
2803 if ((maxHeight != -1) && (m_height > maxHeight)) m_height = maxHeight;
2804
2805 #if wxUSE_TOOLBAR_NATIVE
2806 if (wxDynamicCast(GetParent(), wxToolBar))
2807 {
2808 // don't take the x,y values, they're wrong because toolbar sets them
2809 GtkWidget *widget = GTK_WIDGET(m_widget);
2810 gtk_widget_set_size_request (widget, m_width, m_height);
2811 }
2812 else
2813 #endif
2814 if (m_parent->m_wxwindow == NULL) // i.e. wxNotebook
2815 {
2816 // don't set the size for children of wxNotebook, just take the values.
2817 m_x = x;
2818 m_y = y;
2819 m_width = width;
2820 m_height = height;
2821 }
2822 else
2823 {
2824 GtkPizza *pizza = GTK_PIZZA(m_parent->m_wxwindow);
2825 if ((sizeFlags & wxSIZE_ALLOW_MINUS_ONE) == 0)
2826 {
2827 if (x != -1) m_x = x + gtk_pizza_get_xoffset( pizza );
2828 if (y != -1) m_y = y + gtk_pizza_get_yoffset( pizza );
2829 }
2830 else
2831 {
2832 m_x = x + gtk_pizza_get_xoffset( pizza );
2833 m_y = y + gtk_pizza_get_yoffset( pizza );
2834 }
2835
2836 int left_border = 0;
2837 int right_border = 0;
2838 int top_border = 0;
2839 int bottom_border = 0;
2840
2841 /* the default button has a border around it */
2842 if (GTK_WIDGET_CAN_DEFAULT(m_widget))
2843 {
2844 GtkBorder *default_border = NULL;
2845 gtk_widget_style_get( m_widget, "default_border", &default_border, NULL );
2846 if (default_border)
2847 {
2848 left_border += default_border->left;
2849 right_border += default_border->right;
2850 top_border += default_border->top;
2851 bottom_border += default_border->bottom;
2852 g_free( default_border );
2853 }
2854 }
2855
2856 DoMoveWindow( m_x-top_border,
2857 m_y-left_border,
2858 m_width+left_border+right_border,
2859 m_height+top_border+bottom_border );
2860 }
2861
2862 if (m_hasScrolling)
2863 {
2864 /* Sometimes the client area changes size without the
2865 whole windows's size changing, but if the whole
2866 windows's size doesn't change, no wxSizeEvent will
2867 normally be sent. Here we add an extra test if
2868 the client test has been changed and this will
2869 be used then. */
2870 GetClientSize( &m_oldClientWidth, &m_oldClientHeight );
2871 }
2872
2873 /*
2874 wxPrintf( "OnSize sent from " );
2875 if (GetClassInfo() && GetClassInfo()->GetClassName())
2876 wxPrintf( GetClassInfo()->GetClassName() );
2877 wxPrintf( " %d %d %d %d\n", (int)m_x, (int)m_y, (int)m_width, (int)m_height );
2878 */
2879
2880 if (!m_nativeSizeEvent)
2881 {
2882 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
2883 event.SetEventObject( this );
2884 GetEventHandler()->ProcessEvent( event );
2885 }
2886
2887 m_resizing = false;
2888 }
2889
2890 bool wxWindowGTK::GtkShowFromOnIdle()
2891 {
2892 if (IsShown() && m_showOnIdle && !GTK_WIDGET_VISIBLE (m_widget))
2893 {
2894 GtkAllocation alloc;
2895 alloc.x = m_x;
2896 alloc.y = m_y;
2897 alloc.width = m_width;
2898 alloc.height = m_height;
2899 gtk_widget_size_allocate( m_widget, &alloc );
2900 gtk_widget_show( m_widget );
2901 wxShowEvent eventShow(GetId(), true);
2902 eventShow.SetEventObject(this);
2903 GetEventHandler()->ProcessEvent(eventShow);
2904 m_showOnIdle = false;
2905 return true;
2906 }
2907
2908 return false;
2909 }
2910
2911 void wxWindowGTK::OnInternalIdle()
2912 {
2913 // Check if we have to show window now
2914 if (GtkShowFromOnIdle()) return;
2915
2916 if ( m_dirtyTabOrder )
2917 {
2918 m_dirtyTabOrder = false;
2919 RealizeTabOrder();
2920 }
2921
2922 // Update style if the window was not yet realized
2923 // and SetBackgroundStyle(wxBG_STYLE_CUSTOM) was called
2924 if (m_needsStyleChange)
2925 {
2926 SetBackgroundStyle(GetBackgroundStyle());
2927 m_needsStyleChange = false;
2928 }
2929
2930 wxCursor cursor = m_cursor;
2931 if (g_globalCursor.Ok()) cursor = g_globalCursor;
2932
2933 if (cursor.Ok())
2934 {
2935 /* I now set the cursor anew in every OnInternalIdle call
2936 as setting the cursor in a parent window also effects the
2937 windows above so that checking for the current cursor is
2938 not possible. */
2939
2940 if (m_wxwindow)
2941 {
2942 GdkWindow *window = GTK_PIZZA(m_wxwindow)->bin_window;
2943 if (window)
2944 gdk_window_set_cursor( window, cursor.GetCursor() );
2945
2946 if (!g_globalCursor.Ok())
2947 cursor = *wxSTANDARD_CURSOR;
2948
2949 window = m_widget->window;
2950 if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget)))
2951 gdk_window_set_cursor( window, cursor.GetCursor() );
2952
2953 }
2954 else if ( m_widget )
2955 {
2956 GdkWindow *window = m_widget->window;
2957 if ( window && !GTK_WIDGET_NO_WINDOW(m_widget) )
2958 gdk_window_set_cursor( window, cursor.GetCursor() );
2959 }
2960 }
2961
2962 if (wxUpdateUIEvent::CanUpdate(this))
2963 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2964 }
2965
2966 void wxWindowGTK::DoGetSize( int *width, int *height ) const
2967 {
2968 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
2969
2970 if (width) (*width) = m_width;
2971 if (height) (*height) = m_height;
2972 }
2973
2974 void wxWindowGTK::DoSetClientSize( int width, int height )
2975 {
2976 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
2977
2978 if (m_wxwindow)
2979 {
2980 int dw = 0;
2981 int dh = 0;
2982
2983 if (m_hasScrolling)
2984 {
2985 GetScrollbarWidth(m_widget, dw, dh);
2986 }
2987
2988 #ifndef __WXUNIVERSAL__
2989 if (HasFlag(wxRAISED_BORDER) || HasFlag(wxSUNKEN_BORDER))
2990 {
2991 // shadow border size is 2
2992 dw += 2 * 2;
2993 dh += 2 * 2;
2994 }
2995 if (HasFlag(wxSIMPLE_BORDER))
2996 {
2997 // simple border size is 1
2998 dw += 1 * 2;
2999 dh += 1 * 2;
3000 }
3001 #endif // __WXUNIVERSAL__
3002
3003 width += dw;
3004 height += dh;
3005 }
3006
3007 SetSize(width, height);
3008 }
3009
3010 void wxWindowGTK::DoGetClientSize( int *width, int *height ) const
3011 {
3012 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
3013
3014 int w = m_width;
3015 int h = m_height;
3016
3017 if (m_wxwindow)
3018 {
3019 int dw = 0;
3020 int dh = 0;
3021
3022 if (m_hasScrolling)
3023 GetScrollbarWidth(m_widget, dw, dh);
3024
3025 #ifndef __WXUNIVERSAL__
3026 if (HasFlag(wxRAISED_BORDER) || HasFlag(wxSUNKEN_BORDER))
3027 {
3028 // shadow border size is 2
3029 dw += 2 * 2;
3030 dh += 2 * 2;
3031 }
3032 if (HasFlag(wxSIMPLE_BORDER))
3033 {
3034 // simple border size is 1
3035 dw += 1 * 2;
3036 dh += 1 * 2;
3037 }
3038 #endif // __WXUNIVERSAL__
3039
3040 w -= dw;
3041 h -= dh;
3042 if (w < 0)
3043 w = 0;
3044 if (h < 0)
3045 h = 0;
3046 }
3047
3048 if (width) *width = w;
3049 if (height) *height = h;
3050 }
3051
3052 void wxWindowGTK::DoGetPosition( int *x, int *y ) const
3053 {
3054 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
3055
3056 int dx = 0;
3057 int dy = 0;
3058 if (m_parent && m_parent->m_wxwindow)
3059 {
3060 GtkPizza *pizza = GTK_PIZZA(m_parent->m_wxwindow);
3061 dx = gtk_pizza_get_xoffset( pizza );
3062 dy = gtk_pizza_get_yoffset( pizza );
3063 }
3064
3065 if (m_x == -1 && m_y == -1)
3066 {
3067 GdkWindow *source = (GdkWindow *) NULL;
3068 if (m_wxwindow)
3069 source = GTK_PIZZA(m_wxwindow)->bin_window;
3070 else
3071 source = m_widget->window;
3072
3073 if (source)
3074 {
3075 int org_x = 0;
3076 int org_y = 0;
3077 gdk_window_get_origin( source, &org_x, &org_y );
3078
3079 if (GetParent())
3080 GetParent()->ScreenToClient(&org_x, &org_y);
3081
3082 ((wxWindowGTK*) this)->m_x = org_x;
3083 ((wxWindowGTK*) this)->m_y = org_y;
3084 }
3085 }
3086
3087 if (x) (*x) = m_x - dx;
3088 if (y) (*y) = m_y - dy;
3089 }
3090
3091 void wxWindowGTK::DoClientToScreen( int *x, int *y ) const
3092 {
3093 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
3094
3095 if (!m_widget->window) return;
3096
3097 GdkWindow *source = (GdkWindow *) NULL;
3098 if (m_wxwindow)
3099 source = GTK_PIZZA(m_wxwindow)->bin_window;
3100 else
3101 source = m_widget->window;
3102
3103 int org_x = 0;
3104 int org_y = 0;
3105 gdk_window_get_origin( source, &org_x, &org_y );
3106
3107 if (!m_wxwindow)
3108 {
3109 if (GTK_WIDGET_NO_WINDOW (m_widget))
3110 {
3111 org_x += m_widget->allocation.x;
3112 org_y += m_widget->allocation.y;
3113 }
3114 }
3115
3116
3117 if (x)
3118 {
3119 if (GetLayoutDirection() == wxLayout_RightToLeft)
3120 *x = (GetClientSize().x - *x) + org_x;
3121 else
3122 *x += org_x;
3123 }
3124
3125 if (y) *y += org_y;
3126 }
3127
3128 void wxWindowGTK::DoScreenToClient( int *x, int *y ) const
3129 {
3130 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
3131
3132 if (!m_widget->window) return;
3133
3134 GdkWindow *source = (GdkWindow *) NULL;
3135 if (m_wxwindow)
3136 source = GTK_PIZZA(m_wxwindow)->bin_window;
3137 else
3138 source = m_widget->window;
3139
3140 int org_x = 0;
3141 int org_y = 0;
3142 gdk_window_get_origin( source, &org_x, &org_y );
3143
3144 if (!m_wxwindow)
3145 {
3146 if (GTK_WIDGET_NO_WINDOW (m_widget))
3147 {
3148 org_x += m_widget->allocation.x;
3149 org_y += m_widget->allocation.y;
3150 }
3151 }
3152
3153 if (x)
3154 {
3155 if (GetLayoutDirection() == wxLayout_RightToLeft)
3156 *x = (GetClientSize().x - *x) - org_x;
3157 else
3158 *x -= org_x;
3159 }
3160 if (y) *y -= org_y;
3161 }
3162
3163 bool wxWindowGTK::Show( bool show )
3164 {
3165 wxCHECK_MSG( (m_widget != NULL), false, wxT("invalid window") );
3166
3167 if (!wxWindowBase::Show(show))
3168 {
3169 // nothing to do
3170 return false;
3171 }
3172
3173 if (show)
3174 {
3175 if (!m_showOnIdle)
3176 {
3177 gtk_widget_show( m_widget );
3178 wxShowEvent eventShow(GetId(), show);
3179 eventShow.SetEventObject(this);
3180 GetEventHandler()->ProcessEvent(eventShow);
3181 }
3182 }
3183 else
3184 {
3185 gtk_widget_hide( m_widget );
3186 wxShowEvent eventShow(GetId(), show);
3187 eventShow.SetEventObject(this);
3188 GetEventHandler()->ProcessEvent(eventShow);
3189 }
3190
3191 return true;
3192 }
3193
3194 static void wxWindowNotifyEnable(wxWindowGTK* win, bool enable)
3195 {
3196 win->OnParentEnable(enable);
3197
3198 // Recurse, so that children have the opportunity to Do The Right Thing
3199 // and reset colours that have been messed up by a parent's (really ancestor's)
3200 // Enable call
3201 for ( wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
3202 node;
3203 node = node->GetNext() )
3204 {
3205 wxWindow *child = node->GetData();
3206 if (!child->IsKindOf(CLASSINFO(wxDialog)) && !child->IsKindOf(CLASSINFO(wxFrame)))
3207 wxWindowNotifyEnable(child, enable);
3208 }
3209 }
3210
3211 bool wxWindowGTK::Enable( bool enable )
3212 {
3213 wxCHECK_MSG( (m_widget != NULL), false, wxT("invalid window") );
3214
3215 if (!wxWindowBase::Enable(enable))
3216 {
3217 // nothing to do
3218 return false;
3219 }
3220
3221 gtk_widget_set_sensitive( m_widget, enable );
3222 if ( m_wxwindow )
3223 gtk_widget_set_sensitive( m_wxwindow, enable );
3224
3225 wxWindowNotifyEnable(this, enable);
3226
3227 return true;
3228 }
3229
3230 int wxWindowGTK::GetCharHeight() const
3231 {
3232 wxCHECK_MSG( (m_widget != NULL), 12, wxT("invalid window") );
3233
3234 wxFont font = GetFont();
3235 wxCHECK_MSG( font.Ok(), 12, wxT("invalid font") );
3236
3237 PangoContext *context = NULL;
3238 if (m_widget)
3239 context = gtk_widget_get_pango_context( m_widget );
3240
3241 if (!context)
3242 return 0;
3243
3244 PangoFontDescription *desc = font.GetNativeFontInfo()->description;
3245 PangoLayout *layout = pango_layout_new(context);
3246 pango_layout_set_font_description(layout, desc);
3247 pango_layout_set_text(layout, "H", 1);
3248 PangoLayoutLine *line = (PangoLayoutLine *)pango_layout_get_lines(layout)->data;
3249
3250 PangoRectangle rect;
3251 pango_layout_line_get_extents(line, NULL, &rect);
3252
3253 g_object_unref (layout);
3254
3255 return (int) PANGO_PIXELS(rect.height);
3256 }
3257
3258 int wxWindowGTK::GetCharWidth() const
3259 {
3260 wxCHECK_MSG( (m_widget != NULL), 8, wxT("invalid window") );
3261
3262 wxFont font = GetFont();
3263 wxCHECK_MSG( font.Ok(), 8, wxT("invalid font") );
3264
3265 PangoContext *context = NULL;
3266 if (m_widget)
3267 context = gtk_widget_get_pango_context( m_widget );
3268
3269 if (!context)
3270 return 0;
3271
3272 PangoFontDescription *desc = font.GetNativeFontInfo()->description;
3273 PangoLayout *layout = pango_layout_new(context);
3274 pango_layout_set_font_description(layout, desc);
3275 pango_layout_set_text(layout, "g", 1);
3276 PangoLayoutLine *line = (PangoLayoutLine *)pango_layout_get_lines(layout)->data;
3277
3278 PangoRectangle rect;
3279 pango_layout_line_get_extents(line, NULL, &rect);
3280
3281 g_object_unref (layout);
3282
3283 return (int) PANGO_PIXELS(rect.width);
3284 }
3285
3286 void wxWindowGTK::GetTextExtent( const wxString& string,
3287 int *x,
3288 int *y,
3289 int *descent,
3290 int *externalLeading,
3291 const wxFont *theFont ) const
3292 {
3293 wxFont fontToUse = theFont ? *theFont : GetFont();
3294
3295 wxCHECK_RET( fontToUse.Ok(), wxT("invalid font") );
3296
3297 if (string.empty())
3298 {
3299 if (x) (*x) = 0;
3300 if (y) (*y) = 0;
3301 return;
3302 }
3303
3304 PangoContext *context = NULL;
3305 if (m_widget)
3306 context = gtk_widget_get_pango_context( m_widget );
3307
3308 if (!context)
3309 {
3310 if (x) (*x) = 0;
3311 if (y) (*y) = 0;
3312 return;
3313 }
3314
3315 PangoFontDescription *desc = fontToUse.GetNativeFontInfo()->description;
3316 PangoLayout *layout = pango_layout_new(context);
3317 pango_layout_set_font_description(layout, desc);
3318 {
3319 const wxCharBuffer data = wxGTK_CONV( string );
3320 if ( data )
3321 pango_layout_set_text(layout, data, strlen(data));
3322 }
3323
3324 PangoRectangle rect;
3325 pango_layout_get_extents(layout, NULL, &rect);
3326
3327 if (x) (*x) = (wxCoord) PANGO_PIXELS(rect.width);
3328 if (y) (*y) = (wxCoord) PANGO_PIXELS(rect.height);
3329 if (descent)
3330 {
3331 PangoLayoutIter *iter = pango_layout_get_iter(layout);
3332 int baseline = pango_layout_iter_get_baseline(iter);
3333 pango_layout_iter_free(iter);
3334 *descent = *y - PANGO_PIXELS(baseline);
3335 }
3336 if (externalLeading) (*externalLeading) = 0; // ??
3337
3338 g_object_unref (layout);
3339 }
3340
3341 bool wxWindowGTK::GTKSetDelayedFocusIfNeeded()
3342 {
3343 if ( g_delayedFocus == this )
3344 {
3345 if ( GTK_WIDGET_REALIZED(m_widget) )
3346 {
3347 gtk_widget_grab_focus(m_widget);
3348 g_delayedFocus = NULL;
3349
3350 return true;
3351 }
3352 }
3353
3354 return false;
3355 }
3356
3357 void wxWindowGTK::SetFocus()
3358 {
3359 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
3360 if ( m_hasFocus )
3361 {
3362 // don't do anything if we already have focus
3363 return;
3364 }
3365
3366 if (m_wxwindow)
3367 {
3368 if (!GTK_WIDGET_HAS_FOCUS (m_wxwindow))
3369 {
3370 gtk_widget_grab_focus (m_wxwindow);
3371 }
3372 }
3373 else if (m_widget)
3374 {
3375 if (GTK_IS_CONTAINER(m_widget))
3376 {
3377 if (IsKindOf(CLASSINFO(wxRadioButton)))
3378 {
3379 gtk_widget_grab_focus (m_widget);
3380 return;
3381 }
3382
3383 gtk_widget_child_focus( m_widget, GTK_DIR_TAB_FORWARD );
3384 }
3385 else
3386 if (GTK_WIDGET_CAN_FOCUS(m_widget) && !GTK_WIDGET_HAS_FOCUS (m_widget) )
3387 {
3388
3389 if (!GTK_WIDGET_REALIZED(m_widget))
3390 {
3391 // we can't set the focus to the widget now so we remember that
3392 // it should be focused and will do it later, during the idle
3393 // time, as soon as we can
3394 wxLogTrace(TRACE_FOCUS,
3395 _T("Delaying setting focus to %s(%s)"),
3396 GetClassInfo()->GetClassName(), GetLabel().c_str());
3397
3398 g_delayedFocus = this;
3399 }
3400 else
3401 {
3402 wxLogTrace(TRACE_FOCUS,
3403 _T("Setting focus to %s(%s)"),
3404 GetClassInfo()->GetClassName(), GetLabel().c_str());
3405
3406 gtk_widget_grab_focus (m_widget);
3407 }
3408 }
3409 else
3410 {
3411 wxLogTrace(TRACE_FOCUS,
3412 _T("Can't set focus to %s(%s)"),
3413 GetClassInfo()->GetClassName(), GetLabel().c_str());
3414 }
3415 }
3416 }
3417
3418 bool wxWindowGTK::AcceptsFocus() const
3419 {
3420 return m_acceptsFocus && wxWindowBase::AcceptsFocus();
3421 }
3422
3423 bool wxWindowGTK::Reparent( wxWindowBase *newParentBase )
3424 {
3425 wxCHECK_MSG( (m_widget != NULL), false, wxT("invalid window") );
3426
3427 wxWindowGTK *oldParent = m_parent,
3428 *newParent = (wxWindowGTK *)newParentBase;
3429
3430 wxASSERT( GTK_IS_WIDGET(m_widget) );
3431
3432 if ( !wxWindowBase::Reparent(newParent) )
3433 return false;
3434
3435 wxASSERT( GTK_IS_WIDGET(m_widget) );
3436
3437 /* prevent GTK from deleting the widget arbitrarily */
3438 gtk_widget_ref( m_widget );
3439
3440 if (oldParent)
3441 {
3442 gtk_container_remove( GTK_CONTAINER(m_widget->parent), m_widget );
3443 }
3444
3445 wxASSERT( GTK_IS_WIDGET(m_widget) );
3446
3447 if (newParent)
3448 {
3449 if (GTK_WIDGET_VISIBLE (newParent->m_widget))
3450 {
3451 m_showOnIdle = true;
3452 gtk_widget_hide( m_widget );
3453 }
3454
3455 /* insert GTK representation */
3456 (*(newParent->m_insertCallback))(newParent, this);
3457 }
3458
3459 /* reverse: prevent GTK from deleting the widget arbitrarily */
3460 gtk_widget_unref( m_widget );
3461
3462 SetLayoutDirection(wxLayout_Default);
3463
3464 return true;
3465 }
3466
3467 void wxWindowGTK::DoAddChild(wxWindowGTK *child)
3468 {
3469 wxASSERT_MSG( (m_widget != NULL), wxT("invalid window") );
3470
3471 wxASSERT_MSG( (child != NULL), wxT("invalid child window") );
3472
3473 wxASSERT_MSG( (m_insertCallback != NULL), wxT("invalid child insertion function") );
3474
3475 /* add to list */
3476 AddChild( child );
3477
3478 /* insert GTK representation */
3479 (*m_insertCallback)(this, child);
3480 }
3481
3482 void wxWindowGTK::AddChild(wxWindowBase *child)
3483 {
3484 wxWindowBase::AddChild(child);
3485 m_dirtyTabOrder = true;
3486 if (g_isIdle)
3487 wxapp_install_idle_handler();
3488 }
3489
3490 void wxWindowGTK::RemoveChild(wxWindowBase *child)
3491 {
3492 wxWindowBase::RemoveChild(child);
3493 m_dirtyTabOrder = true;
3494 if (g_isIdle)
3495 wxapp_install_idle_handler();
3496 }
3497
3498 /* static */
3499 wxLayoutDirection wxWindowGTK::GTKGetLayout(GtkWidget *widget)
3500 {
3501 return gtk_widget_get_direction(GTK_WIDGET(widget)) == GTK_TEXT_DIR_RTL
3502 ? wxLayout_RightToLeft
3503 : wxLayout_LeftToRight;
3504 }
3505
3506 /* static */
3507 void wxWindowGTK::GTKSetLayout(GtkWidget *widget, wxLayoutDirection dir)
3508 {
3509 wxASSERT_MSG( dir != wxLayout_Default, _T("invalid layout direction") );
3510
3511 gtk_widget_set_direction(GTK_WIDGET(widget),
3512 dir == wxLayout_RightToLeft ? GTK_TEXT_DIR_RTL
3513 : GTK_TEXT_DIR_LTR);
3514 }
3515
3516 wxLayoutDirection wxWindowGTK::GetLayoutDirection() const
3517 {
3518 return GTKGetLayout(m_widget);
3519 }
3520
3521 void wxWindowGTK::SetLayoutDirection(wxLayoutDirection dir)
3522 {
3523 if ( dir == wxLayout_Default )
3524 {
3525 const wxWindow *const parent = GetParent();
3526 if ( parent )
3527 {
3528 // inherit layout from parent.
3529 dir = parent->GetLayoutDirection();
3530 }
3531 else // no parent, use global default layout
3532 {
3533 dir = wxTheApp->GetLayoutDirection();
3534 }
3535 }
3536
3537 if ( dir == wxLayout_Default )
3538 return;
3539
3540 GTKSetLayout(m_widget, dir);
3541
3542 if (m_wxwindow)
3543 GTKSetLayout(m_wxwindow, dir);
3544 }
3545
3546 wxCoord
3547 wxWindowGTK::AdjustForLayoutDirection(wxCoord x,
3548 wxCoord WXUNUSED(width),
3549 wxCoord WXUNUSED(widthTotal)) const
3550 {
3551 // We now mirrors the coordinates of RTL windows in GtkPizza
3552 return x;
3553 }
3554
3555 void wxWindowGTK::DoMoveInTabOrder(wxWindow *win, MoveKind move)
3556 {
3557 wxWindowBase::DoMoveInTabOrder(win, move);
3558 m_dirtyTabOrder = true;
3559 if (g_isIdle)
3560 wxapp_install_idle_handler();
3561 }
3562
3563 bool wxWindowGTK::GTKWidgetNeedsMnemonic() const
3564 {
3565 // none needed by default
3566 return false;
3567 }
3568
3569 void wxWindowGTK::GTKWidgetDoSetMnemonic(GtkWidget* WXUNUSED(w))
3570 {
3571 // nothing to do by default since none is needed
3572 }
3573
3574 void wxWindowGTK::RealizeTabOrder()
3575 {
3576 if (m_wxwindow)
3577 {
3578 if ( !m_children.empty() )
3579 {
3580 // we don't only construct the correct focus chain but also use
3581 // this opportunity to update the mnemonic widgets for the widgets
3582 // that need them
3583
3584 GList *chain = NULL;
3585 wxWindowGTK* mnemonicWindow = NULL;
3586
3587 for ( wxWindowList::const_iterator i = m_children.begin();
3588 i != m_children.end();
3589 ++i )
3590 {
3591 wxWindowGTK *win = *i;
3592
3593 if ( mnemonicWindow )
3594 {
3595 if ( win->AcceptsFocusFromKeyboard() )
3596 {
3597 // wxComboBox et al. needs to focus on on a different
3598 // widget than m_widget, so if the main widget isn't
3599 // focusable try the connect widget
3600 GtkWidget* w = win->m_widget;
3601 if ( !GTK_WIDGET_CAN_FOCUS(w) )
3602 {
3603 w = win->GetConnectWidget();
3604 if ( !GTK_WIDGET_CAN_FOCUS(w) )
3605 w = NULL;
3606 }
3607
3608 if ( w )
3609 {
3610 mnemonicWindow->GTKWidgetDoSetMnemonic(w);
3611 mnemonicWindow = NULL;
3612 }
3613 }
3614 }
3615 else if ( win->GTKWidgetNeedsMnemonic() )
3616 {
3617 mnemonicWindow = win;
3618 }
3619
3620 chain = g_list_prepend(chain, win->m_widget);
3621 }
3622
3623 chain = g_list_reverse(chain);
3624
3625 gtk_container_set_focus_chain(GTK_CONTAINER(m_wxwindow), chain);
3626 g_list_free(chain);
3627 }
3628 else // no children
3629 {
3630 gtk_container_unset_focus_chain(GTK_CONTAINER(m_wxwindow));
3631 }
3632 }
3633 }
3634
3635 void wxWindowGTK::Raise()
3636 {
3637 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
3638
3639 if (m_wxwindow && m_wxwindow->window)
3640 {
3641 gdk_window_raise( m_wxwindow->window );
3642 }
3643 else if (m_widget->window)
3644 {
3645 gdk_window_raise( m_widget->window );
3646 }
3647 }
3648
3649 void wxWindowGTK::Lower()
3650 {
3651 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
3652
3653 if (m_wxwindow && m_wxwindow->window)
3654 {
3655 gdk_window_lower( m_wxwindow->window );
3656 }
3657 else if (m_widget->window)
3658 {
3659 gdk_window_lower( m_widget->window );
3660 }
3661 }
3662
3663 bool wxWindowGTK::SetCursor( const wxCursor &cursor )
3664 {
3665 if ( !wxWindowBase::SetCursor(cursor.Ok() ? cursor : *wxSTANDARD_CURSOR) )
3666 return false;
3667
3668 GTKUpdateCursor();
3669
3670 return true;
3671 }
3672
3673 void wxWindowGTK::GTKUpdateCursor()
3674 {
3675 wxCursor cursor(g_globalCursor.Ok() ? g_globalCursor : GetCursor());
3676 if ( cursor.Ok() )
3677 {
3678 wxArrayGdkWindows windowsThis;
3679 GdkWindow * const winThis = GTKGetWindow(windowsThis);
3680 if ( winThis )
3681 {
3682 gdk_window_set_cursor(winThis, cursor.GetCursor());
3683 }
3684 else
3685 {
3686 const size_t count = windowsThis.size();
3687 for ( size_t n = 0; n < count; n++ )
3688 {
3689 GdkWindow *win = windowsThis[n];
3690 if ( !win )
3691 {
3692 wxFAIL_MSG(_T("NULL window returned by GTKGetWindow()?"));
3693 continue;
3694 }
3695
3696 gdk_window_set_cursor(win, cursor.GetCursor());
3697 }
3698 }
3699 }
3700 }
3701
3702 void wxWindowGTK::WarpPointer( int x, int y )
3703 {
3704 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
3705
3706 // We provide this function ourselves as it is
3707 // missing in GDK (top of this file).
3708
3709 GdkWindow *window = (GdkWindow*) NULL;
3710 if (m_wxwindow)
3711 window = GTK_PIZZA(m_wxwindow)->bin_window;
3712 else
3713 window = GetConnectWidget()->window;
3714
3715 if (window)
3716 gdk_window_warp_pointer( window, x, y );
3717 }
3718
3719 wxWindowGTK::ScrollDir wxWindowGTK::ScrollDirFromRange(GtkRange *range) const
3720 {
3721 // find the scrollbar which generated the event
3722 for ( int dir = 0; dir < ScrollDir_Max; dir++ )
3723 {
3724 if ( range == m_scrollBar[dir] )
3725 return (ScrollDir)dir;
3726 }
3727
3728 wxFAIL_MSG( _T("event from unknown scrollbar received") );
3729
3730 return ScrollDir_Max;
3731 }
3732
3733 bool wxWindowGTK::DoScrollByUnits(ScrollDir dir, ScrollUnit unit, int units)
3734 {
3735 bool changed = false;
3736 GtkRange* range = m_scrollBar[dir];
3737 if ( range && units )
3738 {
3739 GtkAdjustment* adj = range->adjustment;
3740 gdouble inc = unit == ScrollUnit_Line ? adj->step_increment
3741 : adj->page_increment;
3742
3743 const int posOld = int(adj->value + 0.5);
3744 gtk_range_set_value(range, posOld + units*inc);
3745
3746 changed = int(adj->value + 0.5) != posOld;
3747 }
3748
3749 return changed;
3750 }
3751
3752 bool wxWindowGTK::ScrollLines(int lines)
3753 {
3754 return DoScrollByUnits(ScrollDir_Vert, ScrollUnit_Line, lines);
3755 }
3756
3757 bool wxWindowGTK::ScrollPages(int pages)
3758 {
3759 return DoScrollByUnits(ScrollDir_Vert, ScrollUnit_Page, pages);
3760 }
3761
3762 void wxWindowGTK::Refresh( bool eraseBackground, const wxRect *rect )
3763 {
3764 if (!m_widget)
3765 return;
3766 if (!m_widget->window)
3767 return;
3768
3769 if (m_wxwindow)
3770 {
3771 if (!GTK_PIZZA(m_wxwindow)->bin_window) return;
3772
3773 GdkRectangle gdk_rect,
3774 *p;
3775 if (rect)
3776 {
3777 gdk_rect.x = rect->x;
3778 gdk_rect.y = rect->y;
3779 gdk_rect.width = rect->width;
3780 gdk_rect.height = rect->height;
3781 if (GetLayoutDirection() == wxLayout_RightToLeft)
3782 gdk_rect.x = GetClientSize().x - gdk_rect.x - gdk_rect.width;
3783
3784 p = &gdk_rect;
3785 }
3786 else // invalidate everything
3787 {
3788 p = NULL;
3789 }
3790
3791 gdk_window_invalidate_rect( GTK_PIZZA(m_wxwindow)->bin_window, p, TRUE );
3792 }
3793 }
3794
3795 void wxWindowGTK::Update()
3796 {
3797 GtkUpdate();
3798
3799 // when we call Update() we really want to update the window immediately on
3800 // screen, even if it means flushing the entire queue and hence slowing down
3801 // everything -- but it should still be done, it's just that Update() should
3802 // be called very rarely
3803 gdk_flush();
3804 }
3805
3806 void wxWindowGTK::GtkUpdate()
3807 {
3808 if (m_wxwindow && GTK_PIZZA(m_wxwindow)->bin_window)
3809 gdk_window_process_updates( GTK_PIZZA(m_wxwindow)->bin_window, FALSE );
3810 if (m_widget && m_widget->window)
3811 gdk_window_process_updates( m_widget->window, FALSE );
3812
3813 // for consistency with other platforms (and also because it's convenient
3814 // to be able to update an entire TLW by calling Update() only once), we
3815 // should also update all our children here
3816 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
3817 node;
3818 node = node->GetNext() )
3819 {
3820 node->GetData()->GtkUpdate();
3821 }
3822 }
3823
3824 bool wxWindowGTK::DoIsExposed( int x, int y ) const
3825 {
3826 return m_updateRegion.Contains(x, y) != wxOutRegion;
3827 }
3828
3829
3830 bool wxWindowGTK::DoIsExposed( int x, int y, int w, int h ) const
3831 {
3832 if (GetLayoutDirection() == wxLayout_RightToLeft)
3833 return m_updateRegion.Contains(x-w, y, w, h) != wxOutRegion;
3834 else
3835 return m_updateRegion.Contains(x, y, w, h) != wxOutRegion;
3836 }
3837
3838 void wxWindowGTK::GtkSendPaintEvents()
3839 {
3840 if (!m_wxwindow)
3841 {
3842 m_updateRegion.Clear();
3843 return;
3844 }
3845
3846 // Clip to paint region in wxClientDC
3847 m_clipPaintRegion = true;
3848
3849 m_nativeUpdateRegion = m_updateRegion;
3850
3851 if (GetLayoutDirection() == wxLayout_RightToLeft)
3852 {
3853 // Transform m_updateRegion under RTL
3854 m_updateRegion.Clear();
3855
3856 gint width;
3857 gdk_window_get_geometry( GTK_PIZZA(m_wxwindow)->bin_window,
3858 NULL, NULL, &width, NULL, NULL );
3859
3860 wxRegionIterator upd( m_nativeUpdateRegion );
3861 while (upd)
3862 {
3863 wxRect rect;
3864 rect.x = upd.GetX();
3865 rect.y = upd.GetY();
3866 rect.width = upd.GetWidth();
3867 rect.height = upd.GetHeight();
3868
3869 rect.x = width - rect.x - rect.width;
3870 m_updateRegion.Union( rect );
3871
3872 ++upd;
3873 }
3874 }
3875
3876 // widget to draw on
3877 GtkPizza *pizza = GTK_PIZZA (m_wxwindow);
3878
3879 if (GetThemeEnabled() && (GetBackgroundStyle() == wxBG_STYLE_SYSTEM))
3880 {
3881 // find ancestor from which to steal background
3882 wxWindow *parent = wxGetTopLevelParent((wxWindow *)this);
3883 if (!parent)
3884 parent = (wxWindow*)this;
3885
3886 if (GTK_WIDGET_MAPPED(parent->m_widget))
3887 {
3888 wxRegionIterator upd( m_nativeUpdateRegion );
3889 while (upd)
3890 {
3891 GdkRectangle rect;
3892 rect.x = upd.GetX();
3893 rect.y = upd.GetY();
3894 rect.width = upd.GetWidth();
3895 rect.height = upd.GetHeight();
3896
3897 gtk_paint_flat_box( parent->m_widget->style,
3898 pizza->bin_window,
3899 (GtkStateType)GTK_WIDGET_STATE(m_wxwindow),
3900 GTK_SHADOW_NONE,
3901 &rect,
3902 parent->m_widget,
3903 (char *)"base",
3904 0, 0, -1, -1 );
3905
3906 ++upd;
3907 }
3908 }
3909 }
3910 else
3911 {
3912 wxWindowDC dc( (wxWindow*)this );
3913 dc.SetClippingRegion( m_updateRegion );
3914
3915 wxEraseEvent erase_event( GetId(), &dc );
3916 erase_event.SetEventObject( this );
3917
3918 GetEventHandler()->ProcessEvent(erase_event);
3919 }
3920
3921 wxNcPaintEvent nc_paint_event( GetId() );
3922 nc_paint_event.SetEventObject( this );
3923 GetEventHandler()->ProcessEvent( nc_paint_event );
3924
3925 wxPaintEvent paint_event( GetId() );
3926 paint_event.SetEventObject( this );
3927 GetEventHandler()->ProcessEvent( paint_event );
3928
3929 m_clipPaintRegion = false;
3930
3931 m_updateRegion.Clear();
3932 m_nativeUpdateRegion.Clear();
3933 }
3934
3935 void wxWindowGTK::SetDoubleBuffered( bool on )
3936 {
3937 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
3938
3939 if ( m_wxwindow )
3940 gtk_widget_set_double_buffered( m_wxwindow, on );
3941 }
3942
3943 bool wxWindowGTK::IsDoubleBuffered() const
3944 {
3945 return GTK_WIDGET_DOUBLE_BUFFERED( m_wxwindow );
3946 }
3947
3948 void wxWindowGTK::ClearBackground()
3949 {
3950 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
3951 }
3952
3953 #if wxUSE_TOOLTIPS
3954 void wxWindowGTK::DoSetToolTip( wxToolTip *tip )
3955 {
3956 wxWindowBase::DoSetToolTip(tip);
3957
3958 if (m_tooltip)
3959 m_tooltip->Apply( (wxWindow *)this );
3960 }
3961
3962 void wxWindowGTK::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
3963 {
3964 wxString tmp( tip );
3965 gtk_tooltips_set_tip( tips, GetConnectWidget(), wxGTK_CONV(tmp), (gchar*) NULL );
3966 }
3967 #endif // wxUSE_TOOLTIPS
3968
3969 bool wxWindowGTK::SetBackgroundColour( const wxColour &colour )
3970 {
3971 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid window") );
3972
3973 if (!wxWindowBase::SetBackgroundColour(colour))
3974 return false;
3975
3976 if (colour.Ok())
3977 {
3978 // We need the pixel value e.g. for background clearing.
3979 m_backgroundColour.CalcPixel(gtk_widget_get_colormap(m_widget));
3980 }
3981
3982 // apply style change (forceStyle=true so that new style is applied
3983 // even if the bg colour changed from valid to wxNullColour)
3984 if (GetBackgroundStyle() != wxBG_STYLE_CUSTOM)
3985 ApplyWidgetStyle(true);
3986
3987 return true;
3988 }
3989
3990 bool wxWindowGTK::SetForegroundColour( const wxColour &colour )
3991 {
3992 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid window") );
3993
3994 if (!wxWindowBase::SetForegroundColour(colour))
3995 {
3996 return false;
3997 }
3998
3999 if (colour.Ok())
4000 {
4001 // We need the pixel value e.g. for background clearing.
4002 m_foregroundColour.CalcPixel(gtk_widget_get_colormap(m_widget));
4003 }
4004
4005 // apply style change (forceStyle=true so that new style is applied
4006 // even if the bg colour changed from valid to wxNullColour):
4007 ApplyWidgetStyle(true);
4008
4009 return true;
4010 }
4011
4012 PangoContext *wxWindowGTK::GtkGetPangoDefaultContext()
4013 {
4014 return gtk_widget_get_pango_context( m_widget );
4015 }
4016
4017 GtkRcStyle *wxWindowGTK::CreateWidgetStyle(bool forceStyle)
4018 {
4019 // do we need to apply any changes at all?
4020 if ( !forceStyle &&
4021 !m_font.Ok() &&
4022 !m_foregroundColour.Ok() && !m_backgroundColour.Ok() )
4023 {
4024 return NULL;
4025 }
4026
4027 GtkRcStyle *style = gtk_rc_style_new();
4028
4029 if ( m_font.Ok() )
4030 {
4031 style->font_desc =
4032 pango_font_description_copy( m_font.GetNativeFontInfo()->description );
4033 }
4034
4035 if ( m_foregroundColour.Ok() )
4036 {
4037 const GdkColor *fg = m_foregroundColour.GetColor();
4038
4039 style->fg[GTK_STATE_NORMAL] = *fg;
4040 style->color_flags[GTK_STATE_NORMAL] = GTK_RC_FG;
4041
4042 style->fg[GTK_STATE_PRELIGHT] = *fg;
4043 style->color_flags[GTK_STATE_PRELIGHT] = GTK_RC_FG;
4044
4045 style->fg[GTK_STATE_ACTIVE] = *fg;
4046 style->color_flags[GTK_STATE_ACTIVE] = GTK_RC_FG;
4047 }
4048
4049 if ( m_backgroundColour.Ok() )
4050 {
4051 const GdkColor *bg = m_backgroundColour.GetColor();
4052
4053 style->bg[GTK_STATE_NORMAL] = *bg;
4054 style->base[GTK_STATE_NORMAL] = *bg;
4055 style->color_flags[GTK_STATE_NORMAL] = (GtkRcFlags)
4056 (style->color_flags[GTK_STATE_NORMAL] | GTK_RC_BG | GTK_RC_BASE);
4057
4058 style->bg[GTK_STATE_PRELIGHT] = *bg;
4059 style->base[GTK_STATE_PRELIGHT] = *bg;
4060 style->color_flags[GTK_STATE_PRELIGHT] = (GtkRcFlags)
4061 (style->color_flags[GTK_STATE_PRELIGHT] | GTK_RC_BG | GTK_RC_BASE);
4062
4063 style->bg[GTK_STATE_ACTIVE] = *bg;
4064 style->base[GTK_STATE_ACTIVE] = *bg;
4065 style->color_flags[GTK_STATE_ACTIVE] = (GtkRcFlags)
4066 (style->color_flags[GTK_STATE_ACTIVE] | GTK_RC_BG | GTK_RC_BASE);
4067
4068 style->bg[GTK_STATE_INSENSITIVE] = *bg;
4069 style->base[GTK_STATE_INSENSITIVE] = *bg;
4070 style->color_flags[GTK_STATE_INSENSITIVE] = (GtkRcFlags)
4071 (style->color_flags[GTK_STATE_INSENSITIVE] | GTK_RC_BG | GTK_RC_BASE);
4072 }
4073
4074 return style;
4075 }
4076
4077 void wxWindowGTK::ApplyWidgetStyle(bool forceStyle)
4078 {
4079 GtkRcStyle *style = CreateWidgetStyle(forceStyle);
4080 if ( style )
4081 {
4082 DoApplyWidgetStyle(style);
4083 gtk_rc_style_unref(style);
4084 }
4085
4086 // Style change may affect GTK+'s size calculation:
4087 InvalidateBestSize();
4088 }
4089
4090 void wxWindowGTK::DoApplyWidgetStyle(GtkRcStyle *style)
4091 {
4092 if (m_wxwindow)
4093 gtk_widget_modify_style(m_wxwindow, style);
4094 else
4095 gtk_widget_modify_style(m_widget, style);
4096 }
4097
4098 bool wxWindowGTK::SetBackgroundStyle(wxBackgroundStyle style)
4099 {
4100 wxWindowBase::SetBackgroundStyle(style);
4101
4102 if (style == wxBG_STYLE_CUSTOM)
4103 {
4104 GdkWindow *window = (GdkWindow*) NULL;
4105 if (m_wxwindow)
4106 window = GTK_PIZZA(m_wxwindow)->bin_window;
4107 else
4108 window = GetConnectWidget()->window;
4109
4110 if (window)
4111 {
4112 // Make sure GDK/X11 doesn't refresh the window
4113 // automatically.
4114 gdk_window_set_back_pixmap( window, None, False );
4115 #ifdef __X__
4116 Display* display = GDK_WINDOW_DISPLAY(window);
4117 XFlush(display);
4118 #endif
4119 m_needsStyleChange = false;
4120 }
4121 else
4122 // Do in OnIdle, because the window is not yet available
4123 m_needsStyleChange = true;
4124
4125 // Don't apply widget style, or we get a grey background
4126 }
4127 else
4128 {
4129 // apply style change (forceStyle=true so that new style is applied
4130 // even if the bg colour changed from valid to wxNullColour):
4131 ApplyWidgetStyle(true);
4132 }
4133 return true;
4134 }
4135
4136 #if wxUSE_DRAG_AND_DROP
4137
4138 void wxWindowGTK::SetDropTarget( wxDropTarget *dropTarget )
4139 {
4140 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
4141
4142 GtkWidget *dnd_widget = GetConnectWidget();
4143
4144 if (m_dropTarget) m_dropTarget->UnregisterWidget( dnd_widget );
4145
4146 if (m_dropTarget) delete m_dropTarget;
4147 m_dropTarget = dropTarget;
4148
4149 if (m_dropTarget) m_dropTarget->RegisterWidget( dnd_widget );
4150 }
4151
4152 #endif // wxUSE_DRAG_AND_DROP
4153
4154 GtkWidget* wxWindowGTK::GetConnectWidget()
4155 {
4156 GtkWidget *connect_widget = m_widget;
4157 if (m_wxwindow) connect_widget = m_wxwindow;
4158
4159 return connect_widget;
4160 }
4161
4162 bool wxWindowGTK::GTKIsOwnWindow(GdkWindow *window) const
4163 {
4164 wxArrayGdkWindows windowsThis;
4165 GdkWindow * const winThis = GTKGetWindow(windowsThis);
4166
4167 return winThis ? window == winThis
4168 : windowsThis.Index(window) != wxNOT_FOUND;
4169 }
4170
4171 GdkWindow *wxWindowGTK::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
4172 {
4173 return m_wxwindow ? GTK_PIZZA(m_wxwindow)->bin_window : m_widget->window;
4174 }
4175
4176 bool wxWindowGTK::SetFont( const wxFont &font )
4177 {
4178 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid window") );
4179
4180 if (!wxWindowBase::SetFont(font))
4181 return false;
4182
4183 // apply style change (forceStyle=true so that new style is applied
4184 // even if the font changed from valid to wxNullFont):
4185 ApplyWidgetStyle(true);
4186
4187 return true;
4188 }
4189
4190 void wxWindowGTK::DoCaptureMouse()
4191 {
4192 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
4193
4194 GdkWindow *window = (GdkWindow*) NULL;
4195 if (m_wxwindow)
4196 window = GTK_PIZZA(m_wxwindow)->bin_window;
4197 else
4198 window = GetConnectWidget()->window;
4199
4200 wxCHECK_RET( window, _T("CaptureMouse() failed") );
4201
4202 const wxCursor* cursor = &m_cursor;
4203 if (!cursor->Ok())
4204 cursor = wxSTANDARD_CURSOR;
4205
4206 gdk_pointer_grab( window, FALSE,
4207 (GdkEventMask)
4208 (GDK_BUTTON_PRESS_MASK |
4209 GDK_BUTTON_RELEASE_MASK |
4210 GDK_POINTER_MOTION_HINT_MASK |
4211 GDK_POINTER_MOTION_MASK),
4212 (GdkWindow *) NULL,
4213 cursor->GetCursor(),
4214 (guint32)GDK_CURRENT_TIME );
4215 g_captureWindow = this;
4216 g_captureWindowHasMouse = true;
4217 }
4218
4219 void wxWindowGTK::DoReleaseMouse()
4220 {
4221 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
4222
4223 wxCHECK_RET( g_captureWindow, wxT("can't release mouse - not captured") );
4224
4225 g_captureWindow = (wxWindowGTK*) NULL;
4226
4227 GdkWindow *window = (GdkWindow*) NULL;
4228 if (m_wxwindow)
4229 window = GTK_PIZZA(m_wxwindow)->bin_window;
4230 else
4231 window = GetConnectWidget()->window;
4232
4233 if (!window)
4234 return;
4235
4236 gdk_pointer_ungrab ( (guint32)GDK_CURRENT_TIME );
4237 }
4238
4239 /* static */
4240 wxWindow *wxWindowBase::GetCapture()
4241 {
4242 return (wxWindow *)g_captureWindow;
4243 }
4244
4245 bool wxWindowGTK::IsRetained() const
4246 {
4247 return false;
4248 }
4249
4250 void wxWindowGTK::BlockScrollEvent()
4251 {
4252 wxASSERT(!m_blockScrollEvent);
4253 m_blockScrollEvent = true;
4254 }
4255
4256 void wxWindowGTK::UnblockScrollEvent()
4257 {
4258 wxASSERT(m_blockScrollEvent);
4259 m_blockScrollEvent = false;
4260 }
4261
4262 void wxWindowGTK::SetScrollbar(int orient,
4263 int pos,
4264 int thumbVisible,
4265 int range,
4266 bool WXUNUSED(update))
4267 {
4268 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
4269 wxCHECK_RET( m_wxwindow != NULL, wxT("window needs client area for scrolling") );
4270
4271 if (range > 0)
4272 {
4273 m_hasScrolling = true;
4274 }
4275 else
4276 {
4277 // GtkRange requires upper > lower
4278 range =
4279 thumbVisible = 1;
4280 }
4281
4282 if (pos > range - thumbVisible)
4283 pos = range - thumbVisible;
4284 if (pos < 0)
4285 pos = 0;
4286 GtkAdjustment* adj = m_scrollBar[ScrollDirFromOrient(orient)]->adjustment;
4287 adj->step_increment = 1;
4288 adj->page_increment =
4289 adj->page_size = thumbVisible;
4290 adj->upper = range;
4291 SetScrollPos(orient, pos);
4292 gtk_adjustment_changed(adj);
4293 }
4294
4295 void wxWindowGTK::SetScrollPos(int orient, int pos, bool WXUNUSED(refresh))
4296 {
4297 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
4298 wxCHECK_RET( m_wxwindow != NULL, wxT("window needs client area for scrolling") );
4299
4300 // This check is more than an optimization. Without it, the slider
4301 // will not move smoothly while tracking when using wxScrollHelper.
4302 if (GetScrollPos(orient) != pos)
4303 {
4304 const int dir = ScrollDirFromOrient(orient);
4305 GtkAdjustment* adj = m_scrollBar[dir]->adjustment;
4306 const int max = int(adj->upper - adj->page_size);
4307 if (pos > max)
4308 pos = max;
4309 if (pos < 0)
4310 pos = 0;
4311 m_scrollPos[dir] = adj->value = pos;
4312
4313 // If a "value_changed" signal emission is not already in progress
4314 if (!m_blockValueChanged[dir])
4315 {
4316 gtk_adjustment_value_changed(adj);
4317 }
4318 }
4319 }
4320
4321 int wxWindowGTK::GetScrollThumb(int orient) const
4322 {
4323 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid window") );
4324 wxCHECK_MSG( m_wxwindow != NULL, 0, wxT("window needs client area for scrolling") );
4325
4326 return int(m_scrollBar[ScrollDirFromOrient(orient)]->adjustment->page_size);
4327 }
4328
4329 int wxWindowGTK::GetScrollPos( int orient ) const
4330 {
4331 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid window") );
4332 wxCHECK_MSG( m_wxwindow != NULL, 0, wxT("window needs client area for scrolling") );
4333
4334 return int(m_scrollBar[ScrollDirFromOrient(orient)]->adjustment->value + 0.5);
4335 }
4336
4337 int wxWindowGTK::GetScrollRange( int orient ) const
4338 {
4339 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid window") );
4340 wxCHECK_MSG( m_wxwindow != NULL, 0, wxT("window needs client area for scrolling") );
4341
4342 return int(m_scrollBar[ScrollDirFromOrient(orient)]->adjustment->upper);
4343 }
4344
4345 // Determine if increment is the same as +/-x, allowing for some small
4346 // difference due to possible inexactness in floating point arithmetic
4347 static inline bool IsScrollIncrement(double increment, double x)
4348 {
4349 wxASSERT(increment > 0);
4350 const double tolerance = 1.0 / 1024;
4351 return fabs(increment - fabs(x)) < tolerance;
4352 }
4353
4354 wxEventType wxWindowGTK::GetScrollEventType(GtkRange* range)
4355 {
4356 DEBUG_MAIN_THREAD
4357
4358 if (g_isIdle)
4359 wxapp_install_idle_handler();
4360
4361 wxASSERT(range == m_scrollBar[0] || range == m_scrollBar[1]);
4362
4363 const int barIndex = range == m_scrollBar[1];
4364 GtkAdjustment* adj = range->adjustment;
4365
4366 const int value = int(adj->value + 0.5);
4367
4368 // save previous position
4369 const double oldPos = m_scrollPos[barIndex];
4370 // update current position
4371 m_scrollPos[barIndex] = adj->value;
4372 // If event should be ignored, or integral position has not changed
4373 if (!m_hasVMT || g_blockEventsOnDrag || value == int(oldPos + 0.5))
4374 {
4375 return wxEVT_NULL;
4376 }
4377
4378 wxEventType eventType = wxEVT_SCROLL_THUMBTRACK;
4379 if (!m_isScrolling)
4380 {
4381 // Difference from last change event
4382 const double diff = adj->value - oldPos;
4383 const bool isDown = diff > 0;
4384
4385 if (IsScrollIncrement(adj->step_increment, diff))
4386 {
4387 eventType = isDown ? wxEVT_SCROLL_LINEDOWN : wxEVT_SCROLL_LINEUP;
4388 }
4389 else if (IsScrollIncrement(adj->page_increment, diff))
4390 {
4391 eventType = isDown ? wxEVT_SCROLL_PAGEDOWN : wxEVT_SCROLL_PAGEUP;
4392 }
4393 else if (m_mouseButtonDown)
4394 {
4395 // Assume track event
4396 m_isScrolling = true;
4397 }
4398 }
4399 return eventType;
4400 }
4401
4402 void wxWindowGTK::ScrollWindow( int dx, int dy, const wxRect* WXUNUSED(rect) )
4403 {
4404 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
4405
4406 wxCHECK_RET( m_wxwindow != NULL, wxT("window needs client area for scrolling") );
4407
4408 // No scrolling requested.
4409 if ((dx == 0) && (dy == 0)) return;
4410
4411 m_clipPaintRegion = true;
4412
4413 if (GetLayoutDirection() == wxLayout_RightToLeft)
4414 gtk_pizza_scroll( GTK_PIZZA(m_wxwindow), dx, -dy );
4415 else
4416 gtk_pizza_scroll( GTK_PIZZA(m_wxwindow), -dx, -dy );
4417
4418 m_clipPaintRegion = false;
4419 }
4420
4421 void wxWindowGTK::GtkScrolledWindowSetBorder(GtkWidget* w, int wxstyle)
4422 {
4423 //RN: Note that static controls usually have no border on gtk, so maybe
4424 //it makes sense to treat that as simply no border at the wx level
4425 //as well...
4426 if (!(wxstyle & wxNO_BORDER) && !(wxstyle & wxBORDER_STATIC))
4427 {
4428 GtkShadowType gtkstyle;
4429
4430 if(wxstyle & wxBORDER_RAISED)
4431 gtkstyle = GTK_SHADOW_OUT;
4432 else if (wxstyle & wxBORDER_SUNKEN)
4433 gtkstyle = GTK_SHADOW_IN;
4434 else if (wxstyle & wxBORDER_DOUBLE)
4435 gtkstyle = GTK_SHADOW_ETCHED_IN;
4436 else //default
4437 gtkstyle = GTK_SHADOW_IN;
4438
4439 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(w),
4440 gtkstyle );
4441 }
4442 }
4443
4444 void wxWindowGTK::SetWindowStyleFlag( long style )
4445 {
4446 // Updates the internal variable. NB: Now m_windowStyle bits carry the _new_ style values already
4447 wxWindowBase::SetWindowStyleFlag(style);
4448 }
4449
4450 // Find the wxWindow at the current mouse position, also returning the mouse
4451 // position.
4452 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
4453 {
4454 pt = wxGetMousePosition();
4455 wxWindow* found = wxFindWindowAtPoint(pt);
4456 return found;
4457 }
4458
4459 // Get the current mouse position.
4460 wxPoint wxGetMousePosition()
4461 {
4462 /* This crashes when used within wxHelpContext,
4463 so we have to use the X-specific implementation below.
4464 gint x, y;
4465 GdkModifierType *mask;
4466 (void) gdk_window_get_pointer(NULL, &x, &y, mask);
4467
4468 return wxPoint(x, y);
4469 */
4470
4471 int x, y;
4472 GdkWindow* windowAtPtr = gdk_window_at_pointer(& x, & y);
4473
4474 Display *display = windowAtPtr ? GDK_WINDOW_XDISPLAY(windowAtPtr) : GDK_DISPLAY();
4475 Window rootWindow = RootWindowOfScreen (DefaultScreenOfDisplay(display));
4476 Window rootReturn, childReturn;
4477 int rootX, rootY, winX, winY;
4478 unsigned int maskReturn;
4479
4480 XQueryPointer (display,
4481 rootWindow,
4482 &rootReturn,
4483 &childReturn,
4484 &rootX, &rootY, &winX, &winY, &maskReturn);
4485 return wxPoint(rootX, rootY);
4486
4487 }
4488
4489 // Needed for implementing e.g. combobox on wxGTK within a modal dialog.
4490 void wxAddGrab(wxWindow* window)
4491 {
4492 gtk_grab_add( (GtkWidget*) window->GetHandle() );
4493 }
4494
4495 void wxRemoveGrab(wxWindow* window)
4496 {
4497 gtk_grab_remove( (GtkWidget*) window->GetHandle() );
4498 }