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