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