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