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