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