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