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