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