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