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