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