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