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