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