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