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