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