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