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