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