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