]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/window.cpp
Add short-cut for wxDataViewListIndexModel for generic code
[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
dbb61779 374 if (win->HasFlag(wxSUNKEN_BORDER) || win->HasFlag(wxBORDER_THEME))
ed673c6a
RR
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" {
89954433 431void wxgtk_window_size_request_callback(GtkWidget *WXUNUSED(widget),
e1f448ee
VZ
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
89954433 449void wxgtk_combo_size_request_callback(GtkWidget *WXUNUSED(widget),
024e9a4c
RR
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" {
89954433 481static int gtk_window_expose_callback( GtkWidget *WXUNUSED(widget),
1e6feb95
VZ
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 929#if wxUSE_UNICODE
934960d1
JJ
930#if 0
931 // this is not gtk1.x
932 event.m_uniChar = gdk_keyval_to_unicode(gdk_event->keyval);
933#endif
c4d25c01 934#endif
c958d025
RR
935 wxGetMousePosition( &x, &y );
936 win->ScreenToClient( &x, &y );
a3c15d89
VS
937 event.m_x = x;
938 event.m_y = y;
939 event.SetEventObject( win );
940}
941
942
74710601
VZ
943static bool
944wxTranslateGTKKeyEventToWx(wxKeyEvent& event,
945 wxWindowGTK *win,
946 GdkEventKey *gdk_event)
47d67540 947{
1c6896d7
VZ
948 // VZ: it seems that GDK_KEY_RELEASE event doesn't set event->string
949 // but only event->keyval which is quite useless to us, so remember
950 // the last character from GDK_KEY_PRESS and reuse it as last resort
951 //
952 // NB: should be MT-safe as we're always called from the main thread only
953 static struct
954 {
955 KeySym keysym;
956 long keycode;
957 } s_lastKeyPress = { 0, 0 };
958
959 KeySym keysym = gdk_event->keyval;
0a62b197 960
ada7d2c0 961 wxLogTrace(TRACE_KEYS, _T("Key %s event: keysym = %ld"),
0a62b197
VZ
962 event.GetEventType() == wxEVT_KEY_UP ? _T("release")
963 : _T("press"),
964 keysym);
965
0a164d4c 966 long key_code = wxTranslateKeySymToWXKey(keysym, false /* !isChar */);
1c6896d7
VZ
967
968 if ( !key_code )
969 {
970 // do we have the translation or is it a plain ASCII character?
971 if ( (gdk_event->length == 1) || wxIsAsciiKeysym(keysym) )
972 {
973 // we should use keysym if it is ASCII as X does some translations
974 // like "I pressed while Control is down" => "Ctrl-I" == "TAB"
975 // which we don't want here (but which we do use for OnChar())
976 if ( !wxIsAsciiKeysym(keysym) )
977 {
978 keysym = (KeySym)gdk_event->string[0];
979 }
a2053b27 980
1c6896d7 981 // we want to always get the same key code when the same key is
90e572f1 982 // pressed regardless of the state of the modifiers, i.e. on a
1c6896d7
VZ
983 // standard US keyboard pressing '5' or '%' ('5' key with
984 // Shift) should result in the same key code in OnKeyDown():
985 // '5' (although OnChar() will get either '5' or '%').
986 //
987 // to do it we first translate keysym to keycode (== scan code)
988 // and then back but always using the lower register
989 Display *dpy = (Display *)wxGetDisplay();
990 KeyCode keycode = XKeysymToKeycode(dpy, keysym);
0a62b197
VZ
991
992 wxLogTrace(TRACE_KEYS, _T("\t-> keycode %d"), keycode);
993
1c6896d7
VZ
994 KeySym keysymNormalized = XKeycodeToKeysym(dpy, keycode, 0);
995
996 // use the normalized, i.e. lower register, keysym if we've
997 // got one
998 key_code = keysymNormalized ? keysymNormalized : keysym;
999
1000 // as explained above, we want to have lower register key codes
1001 // normally but for the letter keys we want to have the upper ones
1002 //
1003 // NB: don't use XConvertCase() here, we want to do it for letters
1004 // only
1005 key_code = toupper(key_code);
1006 }
1007 else // non ASCII key, what to do?
1008 {
1009 // by default, ignore it
1010 key_code = 0;
1011
1012 // but if we have cached information from the last KEY_PRESS
1013 if ( gdk_event->type == GDK_KEY_RELEASE )
1014 {
1015 // then reuse it
1016 if ( keysym == s_lastKeyPress.keysym )
1017 {
1018 key_code = s_lastKeyPress.keycode;
1019 }
1020 }
1021 }
1022
1023 if ( gdk_event->type == GDK_KEY_PRESS )
1024 {
1025 // remember it to be reused for KEY_UP event later
1026 s_lastKeyPress.keysym = keysym;
1027 s_lastKeyPress.keycode = key_code;
1028 }
1029 }
1030
ada7d2c0 1031 wxLogTrace(TRACE_KEYS, _T("\t-> wxKeyCode %ld"), key_code);
c801d85f 1032
74710601 1033 // sending unknown key events doesn't really make sense
1c6896d7 1034 if ( !key_code )
0a164d4c 1035 return false;
3d6f7261 1036
1c6896d7 1037 // now fill all the other fields
a3c15d89 1038 wxFillOtherKeyEventFields(event, win, gdk_event);
0a164d4c 1039
f5e27805 1040 event.m_keyCode = key_code;
74710601 1041
0a164d4c 1042 return true;
74710601
VZ
1043}
1044
7c5e6fc6 1045
865bb325 1046extern "C" {
74710601
VZ
1047static gint gtk_window_key_press_callback( GtkWidget *widget,
1048 GdkEventKey *gdk_event,
1049 wxWindow *win )
1050{
1051 DEBUG_MAIN_THREAD
1052
1053 if (g_isIdle)
1054 wxapp_install_idle_handler();
1055
1c6896d7
VZ
1056 if (!win->m_hasVMT)
1057 return FALSE;
1058 if (g_blockEventsOnDrag)
1059 return FALSE;
f1272160
RR
1060
1061
1062 wxKeyEvent event( wxEVT_KEY_DOWN );
1063 bool ret = false;
1064 bool return_after_IM = false;
1065
173ca738 1066 if ( wxTranslateGTKKeyEventToWx(event, win, gdk_event) )
84dc821c
JS
1067 {
1068 // Emit KEY_DOWN event
1069 ret = win->GetEventHandler()->ProcessEvent( event );
1070 }
1071 else
f1272160
RR
1072 {
1073 // Return after IM processing as we cannot do
1074 // anything with it anyhow.
1075 return_after_IM = true;
1076 }
1077
f1272160
RR
1078 // This is for GTK+ 1.2 only. The char event generatation for GTK+ 2.0 is done
1079 // in the "commit" handler.
0a164d4c 1080
fdfb8475 1081 // 2005.02.02 modified by Hong Jen Yee (hzysoft@sina.com.tw).
0a164d4c
WS
1082 // In GTK+ 1.2, strings sent by IMs are also regarded as key_press events whose
1083 // keyCodes cannot be recognized by wxWidgets. These MBCS strings, however, are
1084 // composed of more than one character, which means gdk_event->length will always
fdfb8475 1085 // greater than one. When gtk_event->length == 1, this may be an ASCII character
0a164d4c 1086 // and can be translated by wx. However, when MBCS characters are sent by IM,
fdfb8475
RR
1087 // gdk_event->length will >= 2. So neither should we pass it to accelerator table,
1088 // nor should we pass it to controls. The following explanation was excerpted
1089 // from GDK documentation.
1090 // gint length : the length of string.
1091 // gchar *string : a null-terminated multi-byte string containing the composed
1092 // characters resulting from the key press. When text is being input, in a GtkEntry
1093 // for example, it is these characters which should be added to the input buffer.
1094 // When using Input Methods to support internationalized text input, the composed
1095 // characters appear here after the pre-editing has been completed.
1096
ad975781 1097 if ( (!ret) && (gdk_event->length > 1) ) // If this event contains a pre-edited string from IM.
fdfb8475
RR
1098 {
1099 // We should translate this key event into wxEVT_CHAR not wxEVT_KEY_DOWN.
1100 #if wxUSE_UNICODE // GTK+ 1.2 is not UTF-8 based.
1101 const wxWCharBuffer string = wxConvLocal.cMB2WC( gdk_event->string );
ad975781
RR
1102 if( !string )
1103 return false;
fdfb8475
RR
1104 #else
1105 const char* string = gdk_event->string;
1106 #endif
1107
90e572f1 1108 // Implement OnCharHook by checking ancestor top level windows
f1272160 1109 wxWindow *parent = win;
fdfb8475
RR
1110 while (parent && !parent->IsTopLevel())
1111 parent = parent->GetParent();
1112
f1272160 1113 for( const wxChar* pstr = string; *pstr; pstr++ )
fdfb8475
RR
1114 {
1115 #if wxUSE_UNICODE
1116 event.m_uniChar = *pstr;
1117 // Backward compatible for ISO-8859-1
1118 event.m_keyCode = *pstr < 256 ? event.m_uniChar : 0;
1119 #else
1120 event.m_keyCode = *pstr;
1121 #endif
1122 if (parent)
1123 {
1124 event.SetEventType( wxEVT_CHAR_HOOK );
1125 ret = parent->GetEventHandler()->ProcessEvent( event );
1126 }
1127 if (!ret)
1128 {
1129 event.SetEventType(wxEVT_CHAR);
f1272160 1130 win->GetEventHandler()->ProcessEvent( event );
fdfb8475
RR
1131 }
1132 }
ad975781 1133 return true;
fdfb8475 1134 }
0a164d4c 1135
84dc821c
JS
1136 if (return_after_IM)
1137 return false;
1138
50b58dec
RD
1139#if wxUSE_ACCEL
1140 if (!ret)
1141 {
1142 wxWindowGTK *ancestor = win;
1143 while (ancestor)
1144 {
1145 int command = ancestor->GetAcceleratorTable()->GetCommand( event );
1146 if (command != -1)
1147 {
1148 wxCommandEvent command_event( wxEVT_COMMAND_MENU_SELECTED, command );
1149 ret = ancestor->GetEventHandler()->ProcessEvent( command_event );
1150 break;
1151 }
1152 if (ancestor->IsTopLevel())
1153 break;
1154 ancestor = ancestor->GetParent();
1155 }
1156 }
1157#endif // wxUSE_ACCEL
1158
1ec3a984
RR
1159 // Only send wxEVT_CHAR event if not processed yet. Thus, ALT-x
1160 // will only be sent if it is not in an accelerator table.
2dde25b9 1161 if (!ret)
d728dd40 1162 {
7c5e6fc6 1163 long key_code;
1c6896d7 1164 KeySym keysym = gdk_event->keyval;
36025bcc 1165 // Find key code for EVT_CHAR and EVT_CHAR_HOOK events
0a164d4c 1166 key_code = wxTranslateKeySymToWXKey(keysym, true /* isChar */);
36025bcc 1167 if ( !key_code )
1c6896d7 1168 {
0b187670 1169 if ( wxIsAsciiKeysym(keysym) )
1ec3a984 1170 {
36025bcc
VS
1171 // ASCII key
1172 key_code = (unsigned char)keysym;
1173 }
0b187670
RR
1174 // gdk_event->string is actually deprecated
1175 else if ( gdk_event->length == 1 )
1176 {
1177 key_code = (unsigned char)gdk_event->string[0];
1178 }
36025bcc 1179 }
7c5e6fc6 1180
36025bcc
VS
1181 if ( key_code )
1182 {
1183 wxLogTrace(TRACE_KEYS, _T("Char event: %ld"), key_code);
7c5e6fc6 1184
36025bcc 1185 event.m_keyCode = key_code;
7c5e6fc6 1186
90e572f1 1187 // Implement OnCharHook by checking ancestor top level windows
36025bcc
VS
1188 wxWindow *parent = win;
1189 while (parent && !parent->IsTopLevel())
1190 parent = parent->GetParent();
1191 if (parent)
1192 {
1193 event.SetEventType( wxEVT_CHAR_HOOK );
1194 ret = parent->GetEventHandler()->ProcessEvent( event );
1195 }
1196
1197 if (!ret)
1198 {
1199 event.SetEventType(wxEVT_CHAR);
1200 ret = win->GetEventHandler()->ProcessEvent( event );
1ec3a984 1201 }
f17393f1 1202 }
d728dd40 1203 }
4d3ab2a0 1204
f1272160
RR
1205
1206
f1272160 1207
d728dd40 1208
1ec3a984 1209 // win is a control: tab can be propagated up
f17393f1 1210 if ( !ret &&
5664fc32 1211 ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab)) &&
90e572f1 1212// VZ: testing for wxTE_PROCESS_TAB shouldn't be done here - the control may
f6bcfd97
BP
1213// have this style, yet choose not to process this particular TAB in which
1214// case TAB must still work as a navigational character
f5f3247d
JS
1215// JS: enabling again to make consistent with other platforms
1216// (with wxTE_PROCESS_TAB you have to call Navigate to get default
1217// navigation behaviour)
eedc82f4 1218#if wxUSE_TEXTCTRL
8700aedc 1219 (! (win->HasFlag(wxTE_PROCESS_TAB) && win->IsKindOf(CLASSINFO(wxTextCtrl)) )) &&
f5f3247d 1220#endif
f17393f1 1221 win->GetParent() && (win->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
b292e2f5
RR
1222 {
1223 wxNavigationKeyEvent new_event;
8253c7fd 1224 new_event.SetEventObject( win->GetParent() );
1ec3a984 1225 // GDK reports GDK_ISO_Left_Tab for SHIFT-TAB
5664fc32 1226 new_event.SetDirection( (gdk_event->keyval == GDK_Tab) );
1ec3a984 1227 // CTRL-TAB changes the (parent) window, i.e. switch notebook page
b98d804b 1228 new_event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) );
b292e2f5 1229 new_event.SetCurrentFocus( win );
8253c7fd 1230 ret = win->GetParent()->GetEventHandler()->ProcessEvent( new_event );
b292e2f5 1231 }
ff8bfdbb 1232
1ec3a984 1233 // generate wxID_CANCEL if <esc> has been pressed (typically in dialogs)
f17393f1 1234 if ( !ret &&
b98d804b
RR
1235 (gdk_event->keyval == GDK_Escape) )
1236 {
6ce16bdb
VZ
1237 // however only do it if we have a Cancel button in the dialog,
1238 // otherwise the user code may get confused by the events from a
1239 // non-existing button and, worse, a wxButton might get button event
1240 // from another button which is not really expected
1241 wxWindow *winForCancel = win,
1242 *btnCancel = NULL;
1243 while ( winForCancel )
1244 {
1245 btnCancel = winForCancel->FindWindow(wxID_CANCEL);
1246 if ( btnCancel )
1247 {
1248 // found a cancel button
1249 break;
1250 }
1251
1252 if ( winForCancel->IsTopLevel() )
1253 {
1254 // no need to look further
1255 break;
1256 }
1257
1258 // maybe our parent has a cancel button?
1259 winForCancel = winForCancel->GetParent();
1260 }
1261
1262 if ( btnCancel )
1263 {
17a1ebd1
VZ
1264 wxCommandEvent eventClick(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
1265 eventClick.SetEventObject(btnCancel);
1266 ret = btnCancel->GetEventHandler()->ProcessEvent(eventClick);
6ce16bdb 1267 }
b98d804b 1268 }
c50f1fb9 1269
2b5f62a0 1270 if (ret)
801aa178 1271 {
2b5f62a0
VZ
1272 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" );
1273 return TRUE;
1274 }
1275
1276 return FALSE;
1277}
865bb325 1278}
2b5f62a0 1279
b666df2c
RR
1280//-----------------------------------------------------------------------------
1281// "key_release_event" from any window
1282//-----------------------------------------------------------------------------
1283
865bb325 1284extern "C" {
74710601
VZ
1285static gint gtk_window_key_release_callback( GtkWidget *widget,
1286 GdkEventKey *gdk_event,
1287 wxWindowGTK *win )
b666df2c 1288{
3ac8d3bc
RR
1289 DEBUG_MAIN_THREAD
1290
c50f1fb9 1291 if (g_isIdle)
a2053b27
RR
1292 wxapp_install_idle_handler();
1293
74710601
VZ
1294 if (!win->m_hasVMT)
1295 return FALSE;
b666df2c 1296
74710601
VZ
1297 if (g_blockEventsOnDrag)
1298 return FALSE;
b666df2c
RR
1299
1300 wxKeyEvent event( wxEVT_KEY_UP );
74710601 1301 if ( !wxTranslateGTKKeyEventToWx(event, win, gdk_event) )
b666df2c 1302 {
90e572f1 1303 // unknown key pressed, ignore (the event would be useless anyhow)
74710601 1304 return FALSE;
b666df2c
RR
1305 }
1306
74710601
VZ
1307 if ( !win->GetEventHandler()->ProcessEvent( event ) )
1308 return FALSE;
1309
1310 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_release_event" );
1311 return TRUE;
b666df2c 1312}
865bb325 1313}
b666df2c 1314
c5f9d156
VS
1315// ============================================================================
1316// the mouse events
1317// ============================================================================
1318
d1f2ac45
VZ
1319// ----------------------------------------------------------------------------
1320// mouse event processing helpers
1321// ----------------------------------------------------------------------------
1322
50f00d0c
VS
1323// init wxMouseEvent with the info from GdkEventXXX struct
1324template<typename T> void InitMouseEvent(wxWindowGTK *win,
1325 wxMouseEvent& event,
1326 T *gdk_event)
1327{
1328 event.SetTimestamp( gdk_event->time );
1329 event.m_shiftDown = (gdk_event->state & GDK_SHIFT_MASK);
1330 event.m_controlDown = (gdk_event->state & GDK_CONTROL_MASK);
1331 event.m_altDown = (gdk_event->state & GDK_MOD1_MASK);
1332 event.m_metaDown = (gdk_event->state & GDK_MOD2_MASK);
1333 event.m_leftDown = (gdk_event->state & GDK_BUTTON1_MASK);
1334 event.m_middleDown = (gdk_event->state & GDK_BUTTON2_MASK);
1335 event.m_rightDown = (gdk_event->state & GDK_BUTTON3_MASK);
1336 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
1337 {
1338 event.m_linesPerAction = 3;
60773911 1339 event.m_wheelDelta = 120;
50f00d0c
VS
1340 if (((GdkEventButton*)gdk_event)->button == 4)
1341 event.m_wheelRotation = 120;
1342 else if (((GdkEventButton*)gdk_event)->button == 5)
1343 event.m_wheelRotation = -120;
1344 }
1345
1346 wxPoint pt = win->GetClientAreaOrigin();
1347 event.m_x = (wxCoord)gdk_event->x - pt.x;
1348 event.m_y = (wxCoord)gdk_event->y - pt.y;
1349
1350 event.SetEventObject( win );
1351 event.SetId( win->GetId() );
1352 event.SetTimestamp( gdk_event->time );
1353}
c5f9d156 1354
2daa0ce9
VZ
1355static void AdjustEventButtonState(wxMouseEvent& event)
1356{
1357 // GDK reports the old state of the button for a button press event, but
de6185e2 1358 // for compatibility with MSW and common sense we want m_leftDown be true
2daa0ce9
VZ
1359 // for a LEFT_DOWN event, not FALSE, so we will invert
1360 // left/right/middleDown for the corresponding click events
1e6feb95 1361
1a8caf94
RR
1362 if ((event.GetEventType() == wxEVT_LEFT_DOWN) ||
1363 (event.GetEventType() == wxEVT_LEFT_DCLICK) ||
1364 (event.GetEventType() == wxEVT_LEFT_UP))
1365 {
1366 event.m_leftDown = !event.m_leftDown;
1367 return;
1368 }
1369
1370 if ((event.GetEventType() == wxEVT_MIDDLE_DOWN) ||
1371 (event.GetEventType() == wxEVT_MIDDLE_DCLICK) ||
1372 (event.GetEventType() == wxEVT_MIDDLE_UP))
2daa0ce9 1373 {
1a8caf94
RR
1374 event.m_middleDown = !event.m_middleDown;
1375 return;
1376 }
1377
1378 if ((event.GetEventType() == wxEVT_RIGHT_DOWN) ||
1379 (event.GetEventType() == wxEVT_RIGHT_DCLICK) ||
1380 (event.GetEventType() == wxEVT_RIGHT_UP))
1381 {
1382 event.m_rightDown = !event.m_rightDown;
1383 return;
2daa0ce9
VZ
1384 }
1385}
1386
d1f2ac45
VZ
1387// find the window to send the mouse event too
1388static
1389wxWindowGTK *FindWindowForMouseEvent(wxWindowGTK *win, wxCoord& x, wxCoord& y)
1390{
7d4909b2
RR
1391 wxCoord xx = x;
1392 wxCoord yy = y;
1393
d1f2ac45
VZ
1394 if (win->m_wxwindow)
1395 {
1396 GtkPizza *pizza = GTK_PIZZA(win->m_wxwindow);
7d4909b2
RR
1397 xx += pizza->xoffset;
1398 yy += pizza->yoffset;
d1f2ac45
VZ
1399 }
1400
222ed1d6 1401 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
d1f2ac45
VZ
1402 while (node)
1403 {
b1d4dd7a 1404 wxWindowGTK *child = node->GetData();
d1f2ac45 1405
b1d4dd7a 1406 node = node->GetNext();
d1f2ac45
VZ
1407 if (!child->IsShown())
1408 continue;
1409
1410 if (child->IsTransparentForMouse())
1411 {
1412 // wxStaticBox is transparent in the box itself
1413 int xx1 = child->m_x;
1414 int yy1 = child->m_y;
1415 int xx2 = child->m_x + child->m_width;
7408cf7f 1416 int yy2 = child->m_y + child->m_height;
d1f2ac45
VZ
1417
1418 // left
7d4909b2 1419 if (((xx >= xx1) && (xx <= xx1+10) && (yy >= yy1) && (yy <= yy2)) ||
d1f2ac45 1420 // right
7d4909b2 1421 ((xx >= xx2-10) && (xx <= xx2) && (yy >= yy1) && (yy <= yy2)) ||
d1f2ac45 1422 // top
7d4909b2 1423 ((xx >= xx1) && (xx <= xx2) && (yy >= yy1) && (yy <= yy1+10)) ||
d1f2ac45 1424 // bottom
7d4909b2 1425 ((xx >= xx1) && (xx <= xx2) && (yy >= yy2-1) && (yy <= yy2)))
d1f2ac45
VZ
1426 {
1427 win = child;
1428 x -= child->m_x;
1429 y -= child->m_y;
1430 break;
1431 }
1432
1433 }
1434 else
1435 {
1436 if ((child->m_wxwindow == (GtkWidget*) NULL) &&
7d4909b2
RR
1437 (child->m_x <= xx) &&
1438 (child->m_y <= yy) &&
af3653dd
RR
1439 (child->m_x+child->m_width >= xx) &&
1440 (child->m_y+child->m_height >= yy))
d1f2ac45
VZ
1441 {
1442 win = child;
1443 x -= child->m_x;
1444 y -= child->m_y;
1445 break;
1446 }
1447 }
1448 }
1449
1450 return win;
1451}
1452
c801d85f 1453//-----------------------------------------------------------------------------
2f2aa628
RR
1454// "button_press_event"
1455//-----------------------------------------------------------------------------
c801d85f 1456
865bb325 1457extern "C" {
d1f2ac45
VZ
1458static gint gtk_window_button_press_callback( GtkWidget *widget,
1459 GdkEventButton *gdk_event,
1460 wxWindowGTK *win )
903f689b 1461{
3ac8d3bc
RR
1462 DEBUG_MAIN_THREAD
1463
c50f1fb9 1464 if (g_isIdle)
a2053b27
RR
1465 wxapp_install_idle_handler();
1466
1467/*
223d09f6 1468 wxPrintf( wxT("1) OnButtonPress from ") );
a2053b27
RR
1469 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
1470 wxPrintf( win->GetClassInfo()->GetClassName() );
223d09f6 1471 wxPrintf( wxT(".\n") );
a2053b27 1472*/
a2053b27 1473 if (!win->m_hasVMT) return FALSE;
f5e27805 1474 if (g_blockEventsOnDrag) return TRUE;
76ed8f8d 1475 if (g_blockEventsOnScroll) return TRUE;
c801d85f 1476
034be888
RR
1477 if (!win->IsOwnGtkWindow( gdk_event->window )) return FALSE;
1478
1e4095f7
VZ
1479 g_lastButtonNumber = gdk_event->button;
1480
edc09871 1481 if (win->m_wxwindow && (g_focusWindow != win) && win->IsFocusable())
c801d85f 1482 {
afbe906a 1483 gtk_widget_grab_focus( win->m_wxwindow );
c801d85f 1484/*
afbe906a
RR
1485 wxPrintf( wxT("GrabFocus from ") );
1486 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
1487 wxPrintf( win->GetClassInfo()->GetClassName() );
1488 wxPrintf( wxT(".\n") );
c801d85f 1489*/
362c6693 1490 }
47d67540 1491
90e572f1 1492 // GDK sends surplus button down events
2b5f62a0
VZ
1493 // before a double click event. We
1494 // need to filter these out.
1495 if (gdk_event->type == GDK_BUTTON_PRESS)
1496 {
1497 GdkEvent *peek_event = gdk_event_peek();
1498 if (peek_event)
1499 {
8b8a8e0e
RR
1500 if ((peek_event->type == GDK_2BUTTON_PRESS) ||
1501 (peek_event->type == GDK_3BUTTON_PRESS))
2b5f62a0
VZ
1502 {
1503 gdk_event_free( peek_event );
1504 return TRUE;
1505 }
1506 else
1507 {
1508 gdk_event_free( peek_event );
1509 }
1510 }
1511 }
1512
2daa0ce9 1513 wxEventType event_type = wxEVT_NULL;
47d67540 1514
f5e27805 1515 if (gdk_event->button == 1)
c801d85f 1516 {
f3f0d961 1517 // note that GDK generates triple click events which are not supported
77ffb593 1518 // by wxWidgets but still have to be passed to the app as otherwise
f3f0d961 1519 // clicks would simply go missing
f5e27805
RR
1520 switch (gdk_event->type)
1521 {
15475ced
VZ
1522 // we shouldn't get triple clicks at all for GTK2 because we
1523 // suppress them artificially using the code above but we still
1524 // should map them to something for GTK1 and not just ignore them
1525 // as this would lose clicks
f3f0d961
VZ
1526 case GDK_3BUTTON_PRESS: // we could also map this to DCLICK...
1527 case GDK_BUTTON_PRESS:
1528 event_type = wxEVT_LEFT_DOWN;
1529 break;
1530
1531 case GDK_2BUTTON_PRESS:
1532 event_type = wxEVT_LEFT_DCLICK;
1533 break;
b1f50e65
VZ
1534
1535 default:
1536 // just to silence gcc warnings
1537 ;
f5e27805 1538 }
362c6693 1539 }
f5e27805 1540 else if (gdk_event->button == 2)
c801d85f 1541 {
f5e27805
RR
1542 switch (gdk_event->type)
1543 {
15475ced 1544 case GDK_3BUTTON_PRESS:
f3f0d961
VZ
1545 case GDK_BUTTON_PRESS:
1546 event_type = wxEVT_MIDDLE_DOWN;
1547 break;
1548
1549 case GDK_2BUTTON_PRESS:
1550 event_type = wxEVT_MIDDLE_DCLICK;
1551 break;
b1f50e65
VZ
1552
1553 default:
1554 ;
f5e27805 1555 }
362c6693 1556 }
f5e27805 1557 else if (gdk_event->button == 3)
c801d85f 1558 {
f5e27805
RR
1559 switch (gdk_event->type)
1560 {
15475ced 1561 case GDK_3BUTTON_PRESS:
f3f0d961
VZ
1562 case GDK_BUTTON_PRESS:
1563 event_type = wxEVT_RIGHT_DOWN;
1564 break;
1565
1566 case GDK_2BUTTON_PRESS:
1567 event_type = wxEVT_RIGHT_DCLICK;
1568 break;
b1f50e65
VZ
1569
1570 default:
1571 ;
2b5f62a0
VZ
1572 }
1573 }
f3f0d961 1574 else if (gdk_event->button == 4 || gdk_event->button == 5)
2b5f62a0 1575 {
f3f0d961 1576 if (gdk_event->type == GDK_BUTTON_PRESS )
2b5f62a0 1577 {
f3f0d961 1578 event_type = wxEVT_MOUSEWHEEL;
2b5f62a0
VZ
1579 }
1580 }
47d67540 1581
2daa0ce9
VZ
1582 if ( event_type == wxEVT_NULL )
1583 {
1584 // unknown mouse button or click type
1585 return FALSE;
1586 }
1587
1e4095f7 1588 g_lastMouseEvent = (GdkEvent*) gdk_event;
24bd6cb9 1589
f5e27805 1590 wxMouseEvent event( event_type );
c5f9d156 1591 InitMouseEvent( win, event, gdk_event );
47d67540 1592
2daa0ce9 1593 AdjustEventButtonState(event);
94633ad9 1594
90e572f1 1595 // wxListBox actually gets mouse events from the item, so we need to give it
3ae4c570
VZ
1596 // a chance to correct this
1597 win->FixUpMouseEvent(widget, event.m_x, event.m_y);
2daa0ce9 1598
90e572f1
MR
1599 // find the correct window to send the event to: it may be a different one
1600 // from the one which got it at GTK+ level because some controls don't have
d1f2ac45
VZ
1601 // their own X window and thus cannot get any events.
1602 if ( !g_captureWindow )
1603 win = FindWindowForMouseEvent(win, event.m_x, event.m_y);
ff8bfdbb 1604
defdd888 1605 wxGtkTimeLastClick = gdk_event->time;
34adc954 1606
df135f2b
RR
1607 if (event_type == wxEVT_LEFT_DCLICK)
1608 {
1609 // GTK 1.2 crashes when intercepting double
1610 // click events from both wxSpinButton and
1611 // wxSpinCtrl
1612 if (GTK_IS_SPIN_BUTTON(win->m_widget))
1613 {
1614 // Just disable this event for now.
1615 return FALSE;
1616 }
1617 }
df135f2b 1618
f5e27805 1619 if (win->GetEventHandler()->ProcessEvent( event ))
034be888 1620 {
f5e27805 1621 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "button_press_event" );
a9796db8 1622 g_lastMouseEvent = NULL;
f03fc89f 1623 return TRUE;
034be888 1624 }
a9796db8 1625 g_lastMouseEvent = NULL;
47d67540 1626
ac103441
RR
1627 if (event_type == wxEVT_RIGHT_DOWN)
1628 {
1629 // generate a "context menu" event: this is similar to right mouse
1630 // click under many GUIs except that it is generated differently
1631 // (right up under MSW, ctrl-click under Mac, right down here) and
1632 //
1633 // (a) it's a command event and so is propagated to the parent
1634 // (b) under some ports it can be generated from kbd too
1635 // (c) it uses screen coords (because of (a))
1636 wxContextMenuEvent evtCtx(
1637 wxEVT_CONTEXT_MENU,
1638 win->GetId(),
1639 win->ClientToScreen(event.GetPosition()));
1640 evtCtx.SetEventObject(win);
1641 return win->GetEventHandler()->ProcessEvent(evtCtx);
1642 }
1643
034be888 1644 return FALSE;
362c6693 1645}
865bb325 1646}
c801d85f
KB
1647
1648//-----------------------------------------------------------------------------
97b3455a 1649// "button_release_event"
2f2aa628 1650//-----------------------------------------------------------------------------
c801d85f 1651
865bb325 1652extern "C" {
2b5f62a0
VZ
1653static gint gtk_window_button_release_callback( GtkWidget *widget,
1654 GdkEventButton *gdk_event,
1655 wxWindowGTK *win )
47d67540 1656{
3ac8d3bc
RR
1657 DEBUG_MAIN_THREAD
1658
c50f1fb9 1659 if (g_isIdle)
a2053b27
RR
1660 wxapp_install_idle_handler();
1661
1662 if (!win->m_hasVMT) return FALSE;
034be888
RR
1663 if (g_blockEventsOnDrag) return FALSE;
1664 if (g_blockEventsOnScroll) return FALSE;
c801d85f 1665
034be888 1666 if (!win->IsOwnGtkWindow( gdk_event->window )) return FALSE;
47d67540 1667
1e4095f7
VZ
1668 g_lastButtonNumber = 0;
1669
f5e27805 1670 wxEventType event_type = wxEVT_NULL;
47d67540 1671
f5e27805
RR
1672 switch (gdk_event->button)
1673 {
2b5f62a0
VZ
1674 case 1:
1675 event_type = wxEVT_LEFT_UP;
1676 break;
1677
1678 case 2:
1679 event_type = wxEVT_MIDDLE_UP;
1680 break;
1681
1682 case 3:
1683 event_type = wxEVT_RIGHT_UP;
1684 break;
1685
1686 default:
1687 // unknwon button, don't process
1688 return FALSE;
f5e27805 1689 }
47d67540 1690
1e4095f7 1691 g_lastMouseEvent = (GdkEvent*) gdk_event;
24bd6cb9 1692
f5e27805 1693 wxMouseEvent event( event_type );
c5f9d156 1694 InitMouseEvent( win, event, gdk_event );
f5e27805 1695
2daa0ce9
VZ
1696 AdjustEventButtonState(event);
1697
3ae4c570
VZ
1698 // same wxListBox hack as above
1699 win->FixUpMouseEvent(widget, event.m_x, event.m_y);
e2762ff0 1700
d1f2ac45
VZ
1701 if ( !g_captureWindow )
1702 win = FindWindowForMouseEvent(win, event.m_x, event.m_y);
47d67540 1703
f5e27805 1704 if (win->GetEventHandler()->ProcessEvent( event ))
034be888 1705 {
f5e27805 1706 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "button_release_event" );
f03fc89f 1707 return TRUE;
034be888 1708 }
47d67540 1709
034be888 1710 return FALSE;
362c6693 1711}
865bb325 1712}
c801d85f
KB
1713
1714//-----------------------------------------------------------------------------
2f2aa628
RR
1715// "motion_notify_event"
1716//-----------------------------------------------------------------------------
c801d85f 1717
865bb325 1718extern "C" {
1e6feb95
VZ
1719static gint gtk_window_motion_notify_callback( GtkWidget *widget,
1720 GdkEventMotion *gdk_event,
1721 wxWindowGTK *win )
47d67540 1722{
3ac8d3bc
RR
1723 DEBUG_MAIN_THREAD
1724
c50f1fb9 1725 if (g_isIdle)
a2053b27
RR
1726 wxapp_install_idle_handler();
1727
1728 if (!win->m_hasVMT) return FALSE;
034be888
RR
1729 if (g_blockEventsOnDrag) return FALSE;
1730 if (g_blockEventsOnScroll) return FALSE;
148cd9b6 1731
034be888
RR
1732 if (!win->IsOwnGtkWindow( gdk_event->window )) return FALSE;
1733
ff8bfdbb 1734 if (gdk_event->is_hint)
aae24d21 1735 {
f7a11f8c
RR
1736 int x = 0;
1737 int y = 0;
1738 GdkModifierType state;
1739 gdk_window_get_pointer(gdk_event->window, &x, &y, &state);
1740 gdk_event->x = x;
1741 gdk_event->y = y;
aae24d21 1742 }
ff8bfdbb 1743
1e4095f7
VZ
1744 g_lastMouseEvent = (GdkEvent*) gdk_event;
1745
c801d85f 1746/*
e380f72b
RR
1747 printf( "OnMotion from " );
1748 if (win->GetClassInfo() && win->GetClassInfo()->GetClassName())
1749 printf( win->GetClassInfo()->GetClassName() );
1750 printf( ".\n" );
aae24d21 1751*/
47d67540 1752
e380f72b 1753 wxMouseEvent event( wxEVT_MOTION );
c5f9d156 1754 InitMouseEvent(win, event, gdk_event);
e380f72b 1755
50382578 1756 if ( g_captureWindow )
2f2aa628 1757 {
1e6feb95
VZ
1758 // synthetize a mouse enter or leave event if needed
1759 GdkWindow *winUnderMouse = gdk_window_at_pointer(NULL, NULL);
7c5e6fc6 1760 // This seems to be necessary and actually been added to
50382578
RR
1761 // GDK itself in version 2.0.X
1762 gdk_flush();
7c5e6fc6 1763
1e6feb95
VZ
1764 bool hasMouse = winUnderMouse == gdk_event->window;
1765 if ( hasMouse != g_captureWindowHasMouse )
1766 {
1767 // the mouse changed window
1768 g_captureWindowHasMouse = hasMouse;
1769
17a1ebd1
VZ
1770 wxMouseEvent eventM(g_captureWindowHasMouse ? wxEVT_ENTER_WINDOW
1771 : wxEVT_LEAVE_WINDOW);
1772 InitMouseEvent(win, eventM, gdk_event);
1773 eventM.SetEventObject(win);
1774 win->GetEventHandler()->ProcessEvent(eventM);
1e6feb95
VZ
1775 }
1776 }
1777 else // no capture
1778 {
d1f2ac45 1779 win = FindWindowForMouseEvent(win, event.m_x, event.m_y);
2f2aa628 1780 }
47d67540 1781
a9796db8
VZ
1782 bool ret = win->GetEventHandler()->ProcessEvent( event );
1783 g_lastMouseEvent = NULL;
1784
1785 if ( ret )
034be888 1786 {
e380f72b 1787 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "motion_notify_event" );
034be888 1788 }
47d67540 1789
a9796db8 1790 return ret ? TRUE : FALSE;
362c6693 1791}
865bb325 1792}
c801d85f
KB
1793
1794//-----------------------------------------------------------------------------
2f2aa628
RR
1795// "focus_in_event"
1796//-----------------------------------------------------------------------------
c801d85f 1797
6aeb6f2a
VZ
1798// send the wxChildFocusEvent and wxFocusEvent, common code of
1799// gtk_window_focus_in_callback() and SetFocus()
1800static bool DoSendFocusEvents(wxWindow *win)
1801{
1802 // Notify the parent keeping track of focus for the kbd navigation
1803 // purposes that we got it.
1804 wxChildFocusEvent eventChildFocus(win);
1805 (void)win->GetEventHandler()->ProcessEvent(eventChildFocus);
1806
1807 wxFocusEvent eventFocus(wxEVT_SET_FOCUS, win->GetId());
1808 eventFocus.SetEventObject(win);
1809
1810 return win->GetEventHandler()->ProcessEvent(eventFocus);
1811}
1812
865bb325 1813extern "C" {
1e6feb95
VZ
1814static gint gtk_window_focus_in_callback( GtkWidget *widget,
1815 GdkEvent *WXUNUSED(event),
1816 wxWindow *win )
c801d85f 1817{
3ac8d3bc 1818 DEBUG_MAIN_THREAD
0a164d4c 1819
c50f1fb9 1820 if (g_isIdle)
a2053b27
RR
1821 wxapp_install_idle_handler();
1822
1e6feb95 1823 g_focusWindowLast =
b292e2f5 1824 g_focusWindow = win;
ff8bfdbb 1825
6cad4f1b
VZ
1826 wxLogTrace(TRACE_FOCUS,
1827 _T("%s: focus in"), win->GetName().c_str());
7de59551 1828
b79395c5
RR
1829#ifdef HAVE_XIM
1830 if (win->m_ic)
1831 gdk_im_begin(win->m_ic, win->m_wxwindow->window);
1832#endif
1833
1e6feb95 1834#if wxUSE_CARET
f6bcfd97
BP
1835 // caret needs to be informed about focus change
1836 wxCaret *caret = win->GetCaret();
1837 if ( caret )
1838 {
1839 caret->OnSetFocus();
1840 }
1841#endif // wxUSE_CARET
1842
6cad4f1b
VZ
1843 // does the window itself think that it has the focus?
1844 if ( !win->m_hasFocus )
5cd09f0b 1845 {
6cad4f1b 1846 // not yet, notify it
0a164d4c
WS
1847 win->m_hasFocus = true;
1848
6cad4f1b
VZ
1849 if ( DoSendFocusEvents(win) )
1850 {
1851 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "focus_in_event" );
1852 return TRUE;
1853 }
034be888 1854 }
ca298c88 1855
034be888 1856 return FALSE;
362c6693 1857}
865bb325 1858}
c801d85f
KB
1859
1860//-----------------------------------------------------------------------------
2f2aa628
RR
1861// "focus_out_event"
1862//-----------------------------------------------------------------------------
c801d85f 1863
865bb325 1864extern "C" {
89954433
VZ
1865static gint gtk_window_focus_out_callback( GtkWidget *WXUNUSED(widget),
1866 GdkEventFocus *WXUNUSED(gdk_event),
1867 wxWindowGTK *win )
c801d85f 1868{
3ac8d3bc
RR
1869 DEBUG_MAIN_THREAD
1870
c50f1fb9 1871 if (g_isIdle)
a2053b27
RR
1872 wxapp_install_idle_handler();
1873
6cad4f1b
VZ
1874 wxLogTrace( TRACE_FOCUS,
1875 _T("%s: focus out"), win->GetName().c_str() );
b231914f 1876
148cd9b6 1877
3379ed37 1878 wxWindowGTK *winFocus = wxFindFocusedChild(win);
f6bcfd97
BP
1879 if ( winFocus )
1880 win = winFocus;
1881
1e6feb95 1882 g_focusWindow = (wxWindowGTK *)NULL;
148cd9b6 1883
b79395c5
RR
1884#ifdef HAVE_XIM
1885 if (win->m_ic)
1886 gdk_im_end();
1887#endif
1888
1e6feb95 1889#if wxUSE_CARET
f6bcfd97
BP
1890 // caret needs to be informed about focus change
1891 wxCaret *caret = win->GetCaret();
1892 if ( caret )
1893 {
1894 caret->OnKillFocus();
1895 }
1896#endif // wxUSE_CARET
1897
6cad4f1b
VZ
1898 // don't send the window a kill focus event if it thinks that it doesn't
1899 // have focus already
1900 if ( win->m_hasFocus )
5cd09f0b 1901 {
0a164d4c 1902 win->m_hasFocus = false;
6cad4f1b
VZ
1903
1904 wxFocusEvent event( wxEVT_KILL_FOCUS, win->GetId() );
1905 event.SetEventObject( win );
1906
a752b8c4
VZ
1907 // even if we did process the event in wx code, still let GTK itself
1908 // process it too as otherwise bad things happen, especially in GTK2
1909 // where the text control simply aborts the program if it doesn't get
1910 // the matching focus out event
1911 (void)win->GetEventHandler()->ProcessEvent( event );
034be888 1912 }
ca298c88 1913
034be888 1914 return FALSE;
362c6693 1915}
865bb325 1916}
c801d85f 1917
b4071e91
RR
1918//-----------------------------------------------------------------------------
1919// "enter_notify_event"
1920//-----------------------------------------------------------------------------
1921
865bb325 1922extern "C" {
edc1d330
VZ
1923static
1924gint gtk_window_enter_callback( GtkWidget *widget,
1925 GdkEventCrossing *gdk_event,
1926 wxWindowGTK *win )
b4071e91 1927{
3ac8d3bc
RR
1928 DEBUG_MAIN_THREAD
1929
c50f1fb9 1930 if (g_isIdle)
a2053b27 1931 wxapp_install_idle_handler();
ca298c88 1932
a2053b27
RR
1933 if (!win->m_hasVMT) return FALSE;
1934 if (g_blockEventsOnDrag) return FALSE;
47d67540 1935
7f5f144a
RR
1936 // Event was emitted after a grab
1937 if (gdk_event->mode != GDK_CROSSING_NORMAL) return FALSE;
7c5e6fc6 1938
a2053b27 1939 if (!win->IsOwnGtkWindow( gdk_event->window )) return FALSE;
b292e2f5 1940
4a33eba6
RR
1941 int x = 0;
1942 int y = 0;
1943 GdkModifierType state = (GdkModifierType)0;
ff8bfdbb 1944
a2053b27 1945 gdk_window_get_pointer( widget->window, &x, &y, &state );
ff8bfdbb 1946
edc1d330 1947 wxMouseEvent event( wxEVT_ENTER_WINDOW );
c5f9d156
VS
1948 InitMouseEvent(win, event, gdk_event);
1949 wxPoint pt = win->GetClientAreaOrigin();
1950 event.m_x = x + pt.x;
1951 event.m_y = y + pt.y;
ff8bfdbb 1952
e380f72b 1953 if (win->GetEventHandler()->ProcessEvent( event ))
034be888 1954 {
e380f72b 1955 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "enter_notify_event" );
034be888
RR
1956 return TRUE;
1957 }
ca298c88 1958
034be888 1959 return FALSE;
b4071e91 1960}
865bb325 1961}
47d67540 1962
b4071e91
RR
1963//-----------------------------------------------------------------------------
1964// "leave_notify_event"
1965//-----------------------------------------------------------------------------
1966
865bb325 1967extern "C" {
1e6feb95 1968static gint gtk_window_leave_callback( GtkWidget *widget, GdkEventCrossing *gdk_event, wxWindowGTK *win )
b4071e91 1969{
3ac8d3bc
RR
1970 DEBUG_MAIN_THREAD
1971
c50f1fb9 1972 if (g_isIdle)
a2053b27 1973 wxapp_install_idle_handler();
acfd422a 1974
a2053b27
RR
1975 if (!win->m_hasVMT) return FALSE;
1976 if (g_blockEventsOnDrag) return FALSE;
b292e2f5 1977
7f5f144a
RR
1978 // Event was emitted after an ungrab
1979 if (gdk_event->mode != GDK_CROSSING_NORMAL) return FALSE;
7c5e6fc6 1980
a2053b27 1981 if (!win->IsOwnGtkWindow( gdk_event->window )) return FALSE;
c50f1fb9 1982
e380f72b 1983 wxMouseEvent event( wxEVT_LEAVE_WINDOW );
d1367c3d 1984 event.SetTimestamp( gdk_event->time );
e380f72b 1985 event.SetEventObject( win );
47d67540 1986
4a33eba6
RR
1987 int x = 0;
1988 int y = 0;
1989 GdkModifierType state = (GdkModifierType)0;
ff8bfdbb 1990
4a33eba6 1991 gdk_window_get_pointer( widget->window, &x, &y, &state );
ff8bfdbb 1992
74710601
VZ
1993 event.m_shiftDown = (state & GDK_SHIFT_MASK) != 0;
1994 event.m_controlDown = (state & GDK_CONTROL_MASK) != 0;
1995 event.m_altDown = (state & GDK_MOD1_MASK) != 0;
1996 event.m_metaDown = (state & GDK_MOD2_MASK) != 0;
1997 event.m_leftDown = (state & GDK_BUTTON1_MASK) != 0;
1998 event.m_middleDown = (state & GDK_BUTTON2_MASK) != 0;
1999 event.m_rightDown = (state & GDK_BUTTON3_MASK) != 0;
4a33eba6 2000
c5f9d156
VS
2001 wxPoint pt = win->GetClientAreaOrigin();
2002 event.m_x = x + pt.x;
2003 event.m_y = y + pt.y;
ff8bfdbb 2004
e380f72b 2005 if (win->GetEventHandler()->ProcessEvent( event ))
034be888 2006 {
e380f72b 2007 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "leave_notify_event" );
034be888
RR
2008 return TRUE;
2009 }
ca298c88 2010
034be888 2011 return FALSE;
b4071e91 2012}
865bb325 2013}
47d67540 2014
c801d85f 2015//-----------------------------------------------------------------------------
2f2aa628
RR
2016// "value_changed" from m_vAdjust
2017//-----------------------------------------------------------------------------
c801d85f 2018
865bb325 2019extern "C" {
9e691f46
VZ
2020static void gtk_window_vscroll_callback( GtkAdjustment *adjust,
2021 SCROLLBAR_CBACK_ARG
2022 wxWindowGTK *win )
c801d85f 2023{
3ac8d3bc
RR
2024 DEBUG_MAIN_THREAD
2025
c50f1fb9 2026 if (g_isIdle)
a2053b27 2027 wxapp_install_idle_handler();
c801d85f 2028
a2053b27 2029 if (g_blockEventsOnDrag) return;
47d67540 2030
a2053b27 2031 if (!win->m_hasVMT) return;
148cd9b6 2032
5e014a0c 2033 float diff = adjust->value - win->m_oldVerticalPos;
e380f72b 2034 if (fabs(diff) < 0.2) return;
148cd9b6 2035
5e014a0c 2036 win->m_oldVerticalPos = adjust->value;
47d67540 2037
6728fb61
RL
2038 GtkScrolledWindow *sw = GTK_SCROLLED_WINDOW(win->m_widget);
2039 wxEventType command = GtkScrollWinTypeToWx(GET_SCROLL_TYPE(sw->vscrollbar));
148cd9b6 2040
5e014a0c 2041 int value = (int)(adjust->value+0.5);
c801d85f 2042
c5b42c87 2043 wxScrollWinEvent event( command, value, wxVERTICAL );
e380f72b
RR
2044 event.SetEventObject( win );
2045 win->GetEventHandler()->ProcessEvent( event );
362c6693 2046}
865bb325 2047}
c801d85f
KB
2048
2049//-----------------------------------------------------------------------------
2f2aa628
RR
2050// "value_changed" from m_hAdjust
2051//-----------------------------------------------------------------------------
c801d85f 2052
865bb325 2053extern "C" {
9e691f46
VZ
2054static void gtk_window_hscroll_callback( GtkAdjustment *adjust,
2055 SCROLLBAR_CBACK_ARG
2056 wxWindowGTK *win )
47d67540 2057{
3ac8d3bc
RR
2058 DEBUG_MAIN_THREAD
2059
c50f1fb9 2060 if (g_isIdle)
a2053b27 2061 wxapp_install_idle_handler();
47d67540 2062
a2053b27
RR
2063 if (g_blockEventsOnDrag) return;
2064 if (!win->m_hasVMT) return;
47d67540 2065
5e014a0c 2066 float diff = adjust->value - win->m_oldHorizontalPos;
e380f72b 2067 if (fabs(diff) < 0.2) return;
148cd9b6 2068
6728fb61
RL
2069 GtkScrolledWindow *sw = GTK_SCROLLED_WINDOW(win->m_widget);
2070 wxEventType command = GtkScrollWinTypeToWx(GET_SCROLL_TYPE(sw->hscrollbar));
148cd9b6 2071
9e691f46 2072 win->m_oldHorizontalPos = adjust->value;
148cd9b6 2073
5e014a0c 2074 int value = (int)(adjust->value+0.5);
47d67540 2075
c5b42c87 2076 wxScrollWinEvent event( command, value, wxHORIZONTAL );
e380f72b
RR
2077 event.SetEventObject( win );
2078 win->GetEventHandler()->ProcessEvent( event );
362c6693 2079}
865bb325 2080}
c801d85f 2081
cb43b372
RR
2082//-----------------------------------------------------------------------------
2083// "button_press_event" from scrollbar
2084//-----------------------------------------------------------------------------
2085
865bb325 2086extern "C" {
2a23d363
RR
2087static gint gtk_scrollbar_button_press_callback( GtkRange *widget,
2088 GdkEventButton *gdk_event,
1e6feb95 2089 wxWindowGTK *win)
cb43b372 2090{
3ac8d3bc
RR
2091 DEBUG_MAIN_THREAD
2092
c50f1fb9 2093 if (g_isIdle)
a2053b27
RR
2094 wxapp_install_idle_handler();
2095
d6d26e04 2096
0a164d4c 2097 g_blockEventsOnScroll = true;
9e691f46
VZ
2098
2099 // FIXME: there is no 'slider' field in GTK+ 2.0 any more
2a23d363 2100 win->m_isScrolling = (gdk_event->window == widget->slider);
47d67540 2101
e380f72b 2102 return FALSE;
cb43b372 2103}
865bb325 2104}
cb43b372
RR
2105
2106//-----------------------------------------------------------------------------
2107// "button_release_event" from scrollbar
2108//-----------------------------------------------------------------------------
2109
865bb325 2110extern "C" {
88413fec 2111static gint gtk_scrollbar_button_release_callback( GtkRange *widget,
8bbe427f 2112 GdkEventButton *WXUNUSED(gdk_event),
1e6feb95 2113 wxWindowGTK *win)
cb43b372 2114{
3ac8d3bc
RR
2115 DEBUG_MAIN_THREAD
2116
1ecc4d80 2117// don't test here as we can release the mouse while being over
5e014a0c 2118// a different window than the slider
76ed8f8d
RR
2119//
2120// if (gdk_event->window != widget->slider) return FALSE;
cb43b372 2121
0a164d4c 2122 g_blockEventsOnScroll = false;
47d67540 2123
2a23d363 2124 if (win->m_isScrolling)
88413fec 2125 {
d6d26e04 2126 wxEventType command = wxEVT_SCROLLWIN_THUMBRELEASE;
2a23d363
RR
2127 int value = -1;
2128 int dir = -1;
2daa0ce9 2129
2a23d363
RR
2130 GtkScrolledWindow *scrolledWindow = GTK_SCROLLED_WINDOW(win->m_widget);
2131 if (widget == GTK_RANGE(scrolledWindow->hscrollbar))
2132 {
2133 value = (int)(win->m_hAdjust->value+0.5);
2134 dir = wxHORIZONTAL;
2135 }
2136 if (widget == GTK_RANGE(scrolledWindow->vscrollbar))
2137 {
2138 value = (int)(win->m_vAdjust->value+0.5);
2139 dir = wxVERTICAL;
2140 }
2daa0ce9 2141
2a23d363 2142 wxScrollWinEvent event( command, value, dir );
2a23d363
RR
2143 event.SetEventObject( win );
2144 win->GetEventHandler()->ProcessEvent( event );
88413fec
RR
2145 }
2146
0a164d4c 2147 win->m_isScrolling = false;
2daa0ce9 2148
e380f72b 2149 return FALSE;
cb43b372 2150}
865bb325 2151}
cb43b372 2152
f03fc89f
VZ
2153// ----------------------------------------------------------------------------
2154// this wxWindowBase function is implemented here (in platform-specific file)
2155// because it is static and so couldn't be made virtual
2156// ----------------------------------------------------------------------------
2b07d713 2157
0fe02759 2158wxWindow *wxWindowBase::DoFindFocus()
2b07d713 2159{
1e6feb95
VZ
2160 // the cast is necessary when we compile in wxUniversal mode
2161 return (wxWindow *)g_focusWindow;
2b07d713 2162}
ca298c88 2163
a2053b27
RR
2164//-----------------------------------------------------------------------------
2165// "realize" from m_widget
2166//-----------------------------------------------------------------------------
2167
b79395c5
RR
2168/* We cannot set colours and fonts before the widget has
2169 been realized, so we do this directly after realization. */
a2053b27 2170
865bb325 2171extern "C" {
a2053b27 2172static gint
89954433 2173gtk_window_realized_callback( GtkWidget *WXUNUSED(widget), wxWindow *win )
a2053b27 2174{
3ac8d3bc
RR
2175 DEBUG_MAIN_THREAD
2176
c50f1fb9 2177 if (g_isIdle)
a2053b27 2178 wxapp_install_idle_handler();
0a164d4c 2179
3c679789
RR
2180 wxWindowCreateEvent event( win );
2181 event.SetEventObject( win );
2182 win->GetEventHandler()->ProcessEvent( event );
1e6feb95 2183
a2053b27
RR
2184 return FALSE;
2185}
865bb325 2186}
a2053b27 2187
b79395c5
RR
2188//-----------------------------------------------------------------------------
2189// "size_allocate"
2190//-----------------------------------------------------------------------------
2191
865bb325 2192extern "C" {
8f75cb6c 2193static
adc1999b
RR
2194void gtk_window_size_callback( GtkWidget *WXUNUSED(widget),
2195 GtkAllocation *WXUNUSED(alloc),
8f75cb6c
RR
2196 wxWindow *win )
2197{
2198 if (g_isIdle)
2199 wxapp_install_idle_handler();
2daa0ce9 2200
5b8a521e 2201 if (!win->m_hasScrolling) return;
2daa0ce9 2202
5b8a521e
RR
2203 int client_width = 0;
2204 int client_height = 0;
2205 win->GetClientSize( &client_width, &client_height );
2206 if ((client_width == win->m_oldClientWidth) && (client_height == win->m_oldClientHeight))
2207 return;
2daa0ce9 2208
5b8a521e
RR
2209 win->m_oldClientWidth = client_width;
2210 win->m_oldClientHeight = client_height;
2daa0ce9 2211
5b8a521e
RR
2212 if (!win->m_nativeSizeEvent)
2213 {
2214 wxSizeEvent event( win->GetSize(), win->GetId() );
2215 event.SetEventObject( win );
2216 win->GetEventHandler()->ProcessEvent( event );
2217 }
8f75cb6c 2218}
865bb325 2219}
8f75cb6c
RR
2220
2221
3ed2e7ce
VZ
2222#ifdef HAVE_XIM
2223 #define WXUNUSED_UNLESS_XIM(param) param
2224#else
2225 #define WXUNUSED_UNLESS_XIM(param) WXUNUSED(param)
2226#endif
2227
b79395c5
RR
2228/* Resize XIM window */
2229
865bb325 2230extern "C" {
3ed2e7ce 2231static
8f75cb6c 2232void gtk_wxwindow_size_callback( GtkWidget* WXUNUSED_UNLESS_XIM(widget),
89954433 2233 GtkAllocation* WXUNUSED(alloc),
1e6feb95 2234 wxWindowGTK* WXUNUSED_UNLESS_XIM(win) )
b79395c5
RR
2235{
2236 if (g_isIdle)
2237 wxapp_install_idle_handler();
2daa0ce9 2238
9a8c7620 2239#ifdef HAVE_XIM
b79395c5
RR
2240 if (!win->m_ic)
2241 return;
2242
b79395c5
RR
2243 if (gdk_ic_get_style (win->m_ic) & GDK_IM_PREEDIT_POSITION)
2244 {
2245 gint width, height;
2246
3ed2e7ce 2247 gdk_window_get_size (widget->window, &width, &height);
b79395c5
RR
2248 win->m_icattr->preedit_area.width = width;
2249 win->m_icattr->preedit_area.height = height;
2250 gdk_ic_set_attr (win->m_ic, win->m_icattr, GDK_IC_PREEDIT_AREA);
2251 }
9a8c7620 2252#endif // HAVE_XIM
b79395c5 2253}
865bb325 2254}
b79395c5 2255
63081513
RR
2256//-----------------------------------------------------------------------------
2257// "realize" from m_wxwindow
2258//-----------------------------------------------------------------------------
2259
2260/* Initialize XIM support */
2261
865bb325 2262extern "C" {
63081513 2263static gint
3ed2e7ce 2264gtk_wxwindow_realized_callback( GtkWidget * WXUNUSED_UNLESS_XIM(widget),
1e6feb95 2265 wxWindowGTK * WXUNUSED_UNLESS_XIM(win) )
63081513
RR
2266{
2267 if (g_isIdle)
2268 wxapp_install_idle_handler();
2269
d06800f1 2270#ifdef HAVE_XIM
63081513
RR
2271 if (win->m_ic) return FALSE;
2272 if (!widget) return FALSE;
2273 if (!gdk_im_ready()) return FALSE;
2274
2275 win->m_icattr = gdk_ic_attr_new();
2276 if (!win->m_icattr) return FALSE;
2daa0ce9 2277
63081513
RR
2278 gint width, height;
2279 GdkEventMask mask;
2280 GdkColormap *colormap;
2281 GdkICAttr *attr = win->m_icattr;
b58b1dfc 2282 unsigned attrmask = GDK_IC_ALL_REQ;
63081513 2283 GdkIMStyle style;
b79395c5
RR
2284 GdkIMStyle supported_style = (GdkIMStyle)
2285 (GDK_IM_PREEDIT_NONE |
1e6feb95
VZ
2286 GDK_IM_PREEDIT_NOTHING |
2287 GDK_IM_PREEDIT_POSITION |
2288 GDK_IM_STATUS_NONE |
2289 GDK_IM_STATUS_NOTHING);
63081513
RR
2290
2291 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET)
1e6feb95 2292 supported_style = (GdkIMStyle)(supported_style & ~GDK_IM_PREEDIT_POSITION);
63081513
RR
2293
2294 attr->style = style = gdk_im_decide_style (supported_style);
2295 attr->client_window = widget->window;
2296
2297 if ((colormap = gtk_widget_get_colormap (widget)) !=
1e6feb95 2298 gtk_widget_get_default_colormap ())
63081513 2299 {
5cd09f0b
RR
2300 attrmask |= GDK_IC_PREEDIT_COLORMAP;
2301 attr->preedit_colormap = colormap;
63081513 2302 }
2daa0ce9 2303
63081513
RR
2304 attrmask |= GDK_IC_PREEDIT_FOREGROUND;
2305 attrmask |= GDK_IC_PREEDIT_BACKGROUND;
2306 attr->preedit_foreground = widget->style->fg[GTK_STATE_NORMAL];
2307 attr->preedit_background = widget->style->base[GTK_STATE_NORMAL];
2308
2309 switch (style & GDK_IM_PREEDIT_MASK)
2310 {
1e6feb95
VZ
2311 case GDK_IM_PREEDIT_POSITION:
2312 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET)
2313 {
2314 g_warning ("over-the-spot style requires fontset");
2315 break;
2316 }
63081513 2317
1e6feb95 2318 gdk_window_get_size (widget->window, &width, &height);
63081513 2319
1e6feb95
VZ
2320 attrmask |= GDK_IC_PREEDIT_POSITION_REQ;
2321 attr->spot_location.x = 0;
2322 attr->spot_location.y = height;
2323 attr->preedit_area.x = 0;
2324 attr->preedit_area.y = 0;
2325 attr->preedit_area.width = width;
2326 attr->preedit_area.height = height;
2327 attr->preedit_fontset = widget->style->font;
63081513 2328
1e6feb95 2329 break;
b79395c5 2330 }
2daa0ce9 2331
b58b1dfc 2332 win->m_ic = gdk_ic_new (attr, (GdkICAttributesType)attrmask);
2daa0ce9 2333
63081513 2334 if (win->m_ic == NULL)
1e6feb95 2335 g_warning ("Can't create input context.");
63081513 2336 else
1e6feb95
VZ
2337 {
2338 mask = gdk_window_get_events (widget->window);
2339 mask = (GdkEventMask)(mask | gdk_ic_get_events (win->m_ic));
2340 gdk_window_set_events (widget->window, mask);
2341
2342 if (GTK_WIDGET_HAS_FOCUS(widget))
2343 gdk_im_begin (win->m_ic, widget->window);
2344 }
2345#endif // HAVE_XIM
63081513
RR
2346
2347 return FALSE;
2348}
865bb325 2349}
63081513 2350
6ca41e57 2351//-----------------------------------------------------------------------------
1e6feb95 2352// InsertChild for wxWindowGTK.
6ca41e57
RR
2353//-----------------------------------------------------------------------------
2354
1e6feb95 2355/* Callback for wxWindowGTK. This very strange beast has to be used because
b1170810
RR
2356 * C++ has no virtual methods in a constructor. We have to emulate a
2357 * virtual function here as wxNotebook requires a different way to insert
2358 * a child in it. I had opted for creating a wxNotebookPage window class
2359 * which would have made this superfluous (such in the MDI window system),
2360 * but no-one was listening to me... */
6ca41e57 2361
1e6feb95 2362static void wxInsertChildInWindow( wxWindowGTK* parent, wxWindowGTK* child )
6ca41e57 2363{
bf0c00c6
RR
2364 /* the window might have been scrolled already, do we
2365 have to adapt the position */
da048e3d
RR
2366 GtkPizza *pizza = GTK_PIZZA(parent->m_wxwindow);
2367 child->m_x += pizza->xoffset;
2368 child->m_y += pizza->yoffset;
148cd9b6 2369
da048e3d 2370 gtk_pizza_put( GTK_PIZZA(parent->m_wxwindow),
a2053b27
RR
2371 GTK_WIDGET(child->m_widget),
2372 child->m_x,
2373 child->m_y,
2374 child->m_width,
2375 child->m_height );
6ca41e57
RR
2376}
2377
bbe0af5b
RR
2378//-----------------------------------------------------------------------------
2379// global functions
2380//-----------------------------------------------------------------------------
2381
1e6feb95 2382wxWindow *wxGetActiveWindow()
bbe0af5b 2383{
6cad4f1b 2384 return wxWindow::FindFocus();
bbe0af5b
RR
2385}
2386
7dd40b6f
RD
2387
2388wxMouseState wxGetMouseState()
2389{
2390 wxMouseState ms;
2391
2392 gint x;
2393 gint y;
2394 GdkModifierType mask;
2395
2396 gdk_window_get_pointer(NULL, &x, &y, &mask);
2397
2398 ms.SetX(x);
2399 ms.SetY(y);
2400 ms.SetLeftDown(mask & GDK_BUTTON1_MASK);
2401 ms.SetMiddleDown(mask & GDK_BUTTON2_MASK);
2402 ms.SetRightDown(mask & GDK_BUTTON3_MASK);
2403
2404 ms.SetControlDown(mask & GDK_CONTROL_MASK);
2405 ms.SetShiftDown(mask & GDK_SHIFT_MASK);
2406 ms.SetAltDown(mask & GDK_MOD1_MASK);
2407 ms.SetMetaDown(mask & GDK_MOD2_MASK);
3d257b8d 2408
7dd40b6f
RD
2409 return ms;
2410}
3d257b8d 2411
c801d85f 2412//-----------------------------------------------------------------------------
1e6feb95 2413// wxWindowGTK
c801d85f
KB
2414//-----------------------------------------------------------------------------
2415
6522713c
VZ
2416// in wxUniv/MSW this class is abstract because it doesn't have DoPopupMenu()
2417// method
1e6feb95 2418#ifdef __WXUNIVERSAL__
6522713c
VZ
2419 IMPLEMENT_ABSTRACT_CLASS(wxWindowGTK, wxWindowBase)
2420#else // __WXGTK__
1e6feb95 2421 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
6522713c 2422#endif // __WXUNIVERSAL__/__WXGTK__
c801d85f 2423
1e6feb95 2424void wxWindowGTK::Init()
c801d85f 2425{
f03fc89f 2426 // GTK specific
a2053b27 2427 m_widget = (GtkWidget *) NULL;
e380f72b 2428 m_wxwindow = (GtkWidget *) NULL;
76fcf0f2 2429 m_focusWidget = (GtkWidget *) NULL;
8bbe427f 2430
f03fc89f 2431 // position/size
a2053b27
RR
2432 m_x = 0;
2433 m_y = 0;
2434 m_width = 0;
e380f72b 2435 m_height = 0;
8bbe427f 2436
0a164d4c
WS
2437 m_sizeSet = false;
2438 m_hasVMT = false;
2439 m_needParent = true;
2440 m_isBeingDeleted = false;
148cd9b6 2441
0a164d4c
WS
2442 m_noExpose = false;
2443 m_nativeSizeEvent = false;
94633ad9 2444
0a164d4c
WS
2445 m_hasScrolling = false;
2446 m_isScrolling = false;
f03fc89f 2447
a2053b27 2448 m_hAdjust = (GtkAdjustment*) NULL;
e380f72b 2449 m_vAdjust = (GtkAdjustment*) NULL;
815ac4a7 2450 m_oldHorizontalPos =
e380f72b 2451 m_oldVerticalPos = 0.0;
815ac4a7
VZ
2452 m_oldClientWidth =
2453 m_oldClientHeight = 0;
8bbe427f 2454
0a164d4c 2455 m_resizing = false;
8bbe427f 2456
ddb6bc71 2457 m_insertCallback = (wxInsertChildFunction) NULL;
8bbe427f 2458
0a164d4c
WS
2459 m_acceptsFocus = false;
2460 m_hasFocus = false;
148cd9b6 2461
0a164d4c 2462 m_clipPaintRegion = false;
b6fa52db 2463
c7382f91
JS
2464 m_needsStyleChange = false;
2465
5e014a0c 2466 m_cursor = *wxSTANDARD_CURSOR;
2daa0ce9 2467
63081513
RR
2468#ifdef HAVE_XIM
2469 m_ic = (GdkIC*) NULL;
2470 m_icattr = (GdkICAttr*) NULL;
2471#endif
362c6693 2472}
c801d85f 2473
1e6feb95 2474wxWindowGTK::wxWindowGTK()
68995f26
VZ
2475{
2476 Init();
2477}
2478
1e6feb95
VZ
2479wxWindowGTK::wxWindowGTK( wxWindow *parent,
2480 wxWindowID id,
2481 const wxPoint &pos,
2482 const wxSize &size,
2483 long style,
2484 const wxString &name )
6ca41e57 2485{
68995f26
VZ
2486 Init();
2487
e380f72b 2488 Create( parent, id, pos, size, style, name );
6ca41e57 2489}
8bbe427f 2490
1e6feb95
VZ
2491bool wxWindowGTK::Create( wxWindow *parent,
2492 wxWindowID id,
2493 const wxPoint &pos,
2494 const wxSize &size,
2495 long style,
2496 const wxString &name )
c801d85f 2497{
dbb61779
JS
2498 // Get default border
2499 wxBorder border = GetBorder(style);
2500 style &= ~wxBORDER_MASK;
2501 style |= border;
2502
4dcaf11a
RR
2503 if (!PreCreation( parent, pos, size ) ||
2504 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
2505 {
1e6feb95 2506 wxFAIL_MSG( wxT("wxWindowGTK creation failed") );
0a164d4c 2507 return false;
4dcaf11a 2508 }
47d67540 2509
ddb6bc71 2510 m_insertCallback = wxInsertChildInWindow;
2b5f62a0 2511
e380f72b 2512 m_widget = gtk_scrolled_window_new( (GtkAdjustment *) NULL, (GtkAdjustment *) NULL );
38c7b3d3 2513 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_FOCUS );
ff8bfdbb 2514
f03fc89f 2515 GtkScrolledWindow *scrolledWindow = GTK_SCROLLED_WINDOW(m_widget);
47d67540 2516
dd00f3f6 2517 GtkScrolledWindowClass *scroll_class = GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget) );
e380f72b 2518 scroll_class->scrollbar_spacing = 0;
47d67540 2519
f03fc89f 2520 gtk_scrolled_window_set_policy( scrolledWindow, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
47d67540 2521
f03fc89f
VZ
2522 m_hAdjust = gtk_range_get_adjustment( GTK_RANGE(scrolledWindow->hscrollbar) );
2523 m_vAdjust = gtk_range_get_adjustment( GTK_RANGE(scrolledWindow->vscrollbar) );
47d67540 2524
da048e3d 2525 m_wxwindow = gtk_pizza_new();
0fc5dbf5 2526
1e6feb95 2527#ifndef __WXUNIVERSAL__
da048e3d 2528 GtkPizza *pizza = GTK_PIZZA(m_wxwindow);
b292e2f5 2529
f03fc89f 2530 if (HasFlag(wxRAISED_BORDER))
034be888 2531 {
da048e3d 2532 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_OUT );
034be888 2533 }
dbb61779 2534 else if (HasFlag(wxSUNKEN_BORDER) || HasFlag(wxBORDER_THEME))
034be888 2535 {
da048e3d 2536 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_IN );
5e014a0c
RR
2537 }
2538 else if (HasFlag(wxSIMPLE_BORDER))
2539 {
da048e3d 2540 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_THIN );
034be888
RR
2541 }
2542 else
2543 {
da048e3d 2544 gtk_pizza_set_shadow_type( pizza, GTK_MYSHADOW_NONE );
034be888 2545 }
1e6feb95 2546#endif // __WXUNIVERSAL__
47d67540 2547
4e5a4c69
RR
2548 gtk_container_add( GTK_CONTAINER(m_widget), m_wxwindow );
2549
3da17724 2550 GTK_WIDGET_SET_FLAGS( m_wxwindow, GTK_CAN_FOCUS );
0a164d4c 2551 m_acceptsFocus = true;
ca298c88 2552
e380f72b 2553 // I _really_ don't want scrollbars in the beginning
a2053b27
RR
2554 m_vAdjust->lower = 0.0;
2555 m_vAdjust->upper = 1.0;
2556 m_vAdjust->value = 0.0;
2557 m_vAdjust->step_increment = 1.0;
2558 m_vAdjust->page_increment = 1.0;
2559 m_vAdjust->page_size = 5.0;
2560 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "changed" );
2561 m_hAdjust->lower = 0.0;
2562 m_hAdjust->upper = 1.0;
2563 m_hAdjust->value = 0.0;
2564 m_hAdjust->step_increment = 1.0;
2565 m_hAdjust->page_increment = 1.0;
2566 m_hAdjust->page_size = 5.0;
2567 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "changed" );
f03fc89f
VZ
2568
2569 // these handlers block mouse events to any window during scrolling such as
77ffb593 2570 // motion events and prevent GTK and wxWidgets from fighting over where the
f03fc89f
VZ
2571 // slider should be
2572
2573 gtk_signal_connect( GTK_OBJECT(scrolledWindow->vscrollbar), "button_press_event",
76ed8f8d
RR
2574 (GtkSignalFunc)gtk_scrollbar_button_press_callback, (gpointer) this );
2575
f03fc89f 2576 gtk_signal_connect( GTK_OBJECT(scrolledWindow->hscrollbar), "button_press_event",
76ed8f8d
RR
2577 (GtkSignalFunc)gtk_scrollbar_button_press_callback, (gpointer) this );
2578
f03fc89f 2579 gtk_signal_connect( GTK_OBJECT(scrolledWindow->vscrollbar), "button_release_event",
76ed8f8d
RR
2580 (GtkSignalFunc)gtk_scrollbar_button_release_callback, (gpointer) this );
2581
f03fc89f 2582 gtk_signal_connect( GTK_OBJECT(scrolledWindow->hscrollbar), "button_release_event",
76ed8f8d 2583 (GtkSignalFunc)gtk_scrollbar_button_release_callback, (gpointer) this );
8bbe427f 2584
034be888 2585 // these handlers get notified when screen updates are required either when
76ed8f8d
RR
2586 // scrolling or when the window size (and therefore scrollbar configuration)
2587 // has changed
2588
2589 gtk_signal_connect( GTK_OBJECT(m_hAdjust), "value_changed",
2590 (GtkSignalFunc) gtk_window_hscroll_callback, (gpointer) this );
2591 gtk_signal_connect( GTK_OBJECT(m_vAdjust), "value_changed",
2592 (GtkSignalFunc) gtk_window_vscroll_callback, (gpointer) this );
2593
f03fc89f 2594 gtk_widget_show( m_wxwindow );
47d67540 2595
f03fc89f
VZ
2596 if (m_parent)
2597 m_parent->DoAddChild( this );
94633ad9 2598
76fcf0f2 2599 m_focusWidget = m_wxwindow;
8bbe427f 2600
e380f72b 2601 PostCreation();
8bbe427f 2602
0a164d4c 2603 return true;
362c6693 2604}
c801d85f 2605
1e6feb95 2606wxWindowGTK::~wxWindowGTK()
c801d85f 2607{
7de59551
RD
2608 SendDestroyEvent();
2609
44cd54c2
JS
2610 if (g_focusWindow == this)
2611 g_focusWindow = NULL;
2612
3e679f01
VZ
2613 if ( g_delayedFocus == this )
2614 g_delayedFocus = NULL;
2615
0a164d4c
WS
2616 m_isBeingDeleted = true;
2617 m_hasVMT = false;
47d67540 2618
02c3e53b
JS
2619 // destroy children before destroying this window itself
2620 DestroyChildren();
2621
2622 // unhook focus handlers to prevent stray events being
2623 // propagated to this (soon to be) dead object
2624 if (m_focusWidget != NULL)
2625 {
2626 gtk_signal_disconnect_by_func( GTK_OBJECT(m_focusWidget),
2627 (GtkSignalFunc) gtk_window_focus_in_callback, (gpointer) this );
2628 gtk_signal_disconnect_by_func( GTK_OBJECT(m_focusWidget),
2629 (GtkSignalFunc) gtk_window_focus_out_callback, (gpointer) this );
2630 }
2631
f03fc89f 2632 if (m_widget)
0a164d4c 2633 Show( false );
8bbe427f 2634
63081513
RR
2635#ifdef HAVE_XIM
2636 if (m_ic)
2637 gdk_ic_destroy (m_ic);
2638 if (m_icattr)
2639 gdk_ic_attr_destroy (m_icattr);
2640#endif
2641
f03fc89f 2642 if (m_wxwindow)
a2053b27 2643 {
f03fc89f 2644 gtk_widget_destroy( m_wxwindow );
c50f1fb9 2645 m_wxwindow = (GtkWidget*) NULL;
a2053b27 2646 }
8bbe427f 2647
f03fc89f 2648 if (m_widget)
a2053b27 2649 {
f03fc89f 2650 gtk_widget_destroy( m_widget );
c50f1fb9 2651 m_widget = (GtkWidget*) NULL;
a2053b27 2652 }
362c6693 2653}
c801d85f 2654
1e6feb95 2655bool wxWindowGTK::PreCreation( wxWindowGTK *parent, const wxPoint &pos, const wxSize &size )
c801d85f 2656{
0a164d4c 2657 wxCHECK_MSG( !m_needParent || parent, false, wxT("Need complete parent.") );
8bbe427f 2658
a7c26d10
RD
2659 // Use either the given size, or the default if -1 is given.
2660 // See wxWindowBase for these functions.
3013a903 2661 m_width = WidthDefault(size.x) ;
f03fc89f 2662 m_height = HeightDefault(size.y);
8bbe427f 2663
43a18898
RR
2664 m_x = (int)pos.x;
2665 m_y = (int)pos.y;
8bbe427f 2666
0a164d4c 2667 return true;
c801d85f
KB
2668}
2669
1e6feb95 2670void wxWindowGTK::PostCreation()
c801d85f 2671{
82b978d7
RD
2672 wxASSERT_MSG( (m_widget != NULL), wxT("invalid window") );
2673
43a18898
RR
2674 if (m_wxwindow)
2675 {
147bc491 2676 if (!m_noExpose)
b02da6b1 2677 {
77ffb593 2678 // these get reported to wxWidgets -> wxPaintEvent
1e6feb95 2679
b420fb6a
RR
2680 gtk_pizza_set_external( GTK_PIZZA(m_wxwindow), TRUE );
2681
147bc491
RR
2682 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "expose_event",
2683 GTK_SIGNAL_FUNC(gtk_window_expose_callback), (gpointer)this );
2684
2685 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "draw",
2686 GTK_SIGNAL_FUNC(gtk_window_draw_callback), (gpointer)this );
0fc5dbf5 2687
e441e1f4 2688 if (!HasFlag(wxFULL_REPAINT_ON_RESIZE))
e22454be
RR
2689 {
2690 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "event",
2691 GTK_SIGNAL_FUNC(gtk_window_event_event_callback), (gpointer)this );
2692 }
93d23d8f 2693 }
2b5f62a0 2694
67d78217 2695 // these are called when the "sunken" or "raised" borders are drawn
034be888
RR
2696 gtk_signal_connect( GTK_OBJECT(m_widget), "expose_event",
2697 GTK_SIGNAL_FUNC(gtk_window_own_expose_callback), (gpointer)this );
2698
2699 gtk_signal_connect( GTK_OBJECT(m_widget), "draw",
2700 GTK_SIGNAL_FUNC(gtk_window_own_draw_callback), (gpointer)this );
43a18898 2701 }
47d67540 2702
76fcf0f2 2703 // focus handling
63081513 2704
06fda9e8
RR
2705 if (!GTK_IS_WINDOW(m_widget))
2706 {
2707 if (m_focusWidget == NULL)
2708 m_focusWidget = m_widget;
0a164d4c 2709
06fda9e8
RR
2710 gtk_signal_connect( GTK_OBJECT(m_focusWidget), "focus_in_event",
2711 GTK_SIGNAL_FUNC(gtk_window_focus_in_callback), (gpointer)this );
76fcf0f2 2712
4ecd9342 2713 gtk_signal_connect_after( GTK_OBJECT(m_focusWidget), "focus_out_event",
06fda9e8
RR
2714 GTK_SIGNAL_FUNC(gtk_window_focus_out_callback), (gpointer)this );
2715 }
76fcf0f2
RR
2716
2717 // connect to the various key and mouse handlers
63081513 2718
a2053b27 2719 GtkWidget *connect_widget = GetConnectWidget();
f03fc89f 2720
a2053b27 2721 ConnectWidget( connect_widget );
47d67540 2722
63081513 2723 /* We cannot set colours, fonts and cursors before the widget has
a2053b27
RR
2724 been realized, so we do this directly after realization */
2725 gtk_signal_connect( GTK_OBJECT(connect_widget), "realize",
c50f1fb9 2726 GTK_SIGNAL_FUNC(gtk_window_realized_callback), (gpointer) this );
2daa0ce9 2727
63081513
RR
2728 if (m_wxwindow)
2729 {
47c93b63 2730 // Catch native resize events
8f75cb6c
RR
2731 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "size_allocate",
2732 GTK_SIGNAL_FUNC(gtk_window_size_callback), (gpointer)this );
2daa0ce9 2733
47c93b63 2734 // Initialize XIM support
63081513
RR
2735 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "realize",
2736 GTK_SIGNAL_FUNC(gtk_wxwindow_realized_callback), (gpointer) this );
8f75cb6c 2737
47c93b63 2738 // And resize XIM window
b79395c5
RR
2739 gtk_signal_connect( GTK_OBJECT(m_wxwindow), "size_allocate",
2740 GTK_SIGNAL_FUNC(gtk_wxwindow_size_callback), (gpointer)this );
63081513 2741 }
2daa0ce9 2742
024e9a4c
RR
2743 if (GTK_IS_COMBO(m_widget))
2744 {
2745 GtkCombo *gcombo = GTK_COMBO(m_widget);
0a164d4c 2746
024e9a4c
RR
2747 gtk_signal_connect( GTK_OBJECT(gcombo->entry), "size_request",
2748 GTK_SIGNAL_FUNC(wxgtk_combo_size_request_callback),
2749 (gpointer) this );
2750 }
2751 else
47c93b63
RR
2752 {
2753 // This is needed if we want to add our windows into native
024e9a4c 2754 // GTK controls, such as the toolbar. With this callback, the
47c93b63 2755 // toolbar gets to know the correct size (the one set by the
024e9a4c 2756 // programmer). Sadly, it misbehaves for wxComboBox.
47c93b63 2757 gtk_signal_connect( GTK_OBJECT(m_widget), "size_request",
e1f448ee
VZ
2758 GTK_SIGNAL_FUNC(wxgtk_window_size_request_callback),
2759 (gpointer) this );
47c93b63 2760 }
1e6feb95 2761
40bab631
VS
2762 InheritAttributes();
2763
0a164d4c 2764 m_hasVMT = true;
a433fbd5
VZ
2765
2766 // unless the window was created initially hidden (i.e. Hide() had been
2767 // called before Create()), we should show it at GTK+ level as well
2768 if ( IsShown() )
2769 gtk_widget_show( m_widget );
b4071e91
RR
2770}
2771
1e6feb95 2772void wxWindowGTK::ConnectWidget( GtkWidget *widget )
b4071e91 2773{
43a18898
RR
2774 gtk_signal_connect( GTK_OBJECT(widget), "key_press_event",
2775 GTK_SIGNAL_FUNC(gtk_window_key_press_callback), (gpointer)this );
c801d85f 2776
b666df2c
RR
2777 gtk_signal_connect( GTK_OBJECT(widget), "key_release_event",
2778 GTK_SIGNAL_FUNC(gtk_window_key_release_callback), (gpointer)this );
2779
43a18898
RR
2780 gtk_signal_connect( GTK_OBJECT(widget), "button_press_event",
2781 GTK_SIGNAL_FUNC(gtk_window_button_press_callback), (gpointer)this );
47d67540 2782
43a18898
RR
2783 gtk_signal_connect( GTK_OBJECT(widget), "button_release_event",
2784 GTK_SIGNAL_FUNC(gtk_window_button_release_callback), (gpointer)this );
47d67540 2785
43a18898
RR
2786 gtk_signal_connect( GTK_OBJECT(widget), "motion_notify_event",
2787 GTK_SIGNAL_FUNC(gtk_window_motion_notify_callback), (gpointer)this );
47d67540 2788
43a18898
RR
2789 gtk_signal_connect( GTK_OBJECT(widget), "enter_notify_event",
2790 GTK_SIGNAL_FUNC(gtk_window_enter_callback), (gpointer)this );
47d67540 2791
43a18898
RR
2792 gtk_signal_connect( GTK_OBJECT(widget), "leave_notify_event",
2793 GTK_SIGNAL_FUNC(gtk_window_leave_callback), (gpointer)this );
362c6693 2794}
c801d85f 2795
1e6feb95 2796bool wxWindowGTK::Destroy()
c801d85f 2797{
82b978d7 2798 wxASSERT_MSG( (m_widget != NULL), wxT("invalid window") );
47d67540 2799
0a164d4c 2800 m_hasVMT = false;
c801d85f 2801
f03fc89f 2802 return wxWindowBase::Destroy();
362c6693 2803}
c801d85f 2804
1e6feb95 2805void wxWindowGTK::DoMoveWindow(int x, int y, int width, int height)
23efdd02
RR
2806{
2807 gtk_pizza_set_size( GTK_PIZZA(m_parent->m_wxwindow), m_widget, x, y, width, height );
2808}
2daa0ce9 2809
1e6feb95 2810void wxWindowGTK::DoSetSize( int x, int y, int width, int height, int sizeFlags )
c801d85f 2811{
82b978d7 2812 wxASSERT_MSG( (m_widget != NULL), wxT("invalid window") );
1e6feb95 2813 wxASSERT_MSG( (m_parent != NULL), wxT("wxWindowGTK::SetSize requires parent.\n") );
8bbe427f 2814
33611ebb 2815/*
f94fca1b 2816 printf( "DoSetSize: name %s, x,y,w,h: %d,%d,%d,%d \n", GetName().c_str(), x,y,width,height );
33611ebb
RR
2817*/
2818
e27ce4e9 2819 if (m_resizing) return; /* I don't like recursions */
0a164d4c 2820 m_resizing = true;
1e6feb95 2821
b9f29261
VS
2822 int currentX, currentY;
2823 GetPosition(&currentX, &currentY);
443c834d 2824 if (x == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
b9f29261 2825 x = currentX;
443c834d 2826 if (y == -1 && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
b9f29261 2827 y = currentY;
a200c35e
VS
2828 AdjustForParentClientOrigin(x, y, sizeFlags);
2829
a2053b27 2830 if (m_parent->m_wxwindow == NULL) /* i.e. wxNotebook */
fb1585ae 2831 {
e27ce4e9 2832 /* don't set the size for children of wxNotebook, just take the values. */
fb1585ae
RR
2833 m_x = x;
2834 m_y = y;
2835 m_width = width;
ba4e3652 2836 m_height = height;
fb1585ae 2837 }
ba4e3652 2838 else
fb1585ae 2839 {
da048e3d 2840 GtkPizza *pizza = GTK_PIZZA(m_parent->m_wxwindow);
85ad5eb5 2841 if ((sizeFlags & wxSIZE_ALLOW_MINUS_ONE) == 0)
ba4e3652 2842 {
da048e3d
RR
2843 if (x != -1) m_x = x + pizza->xoffset;
2844 if (y != -1) m_y = y + pizza->yoffset;
ba4e3652
RR
2845 }
2846 else
2847 {
da048e3d
RR
2848 m_x = x + pizza->xoffset;
2849 m_y = y + pizza->yoffset;
ba4e3652 2850 }
47d67540 2851
a63d48fa 2852 // calculate the best size if we should auto size the window
c7e111cd
VZ
2853 if ( ((sizeFlags & wxSIZE_AUTO_WIDTH) && width == -1) ||
2854 ((sizeFlags & wxSIZE_AUTO_HEIGHT) && height == -1) )
ba4e3652 2855 {
a63d48fa 2856 const wxSize sizeBest = GetBestSize();
c7e111cd 2857 if ( (sizeFlags & wxSIZE_AUTO_WIDTH) && width == -1 )
a63d48fa 2858 width = sizeBest.x;
c7e111cd 2859 if ( (sizeFlags & wxSIZE_AUTO_HEIGHT) && height == -1 )
a63d48fa 2860 height = sizeBest.y;
ba4e3652
RR
2861 }
2862
a63d48fa
VZ
2863 if (width != -1)
2864 m_width = width;
2865 if (height != -1)
2866 m_height = height;
8bbe427f 2867
e7dda1ff
VS
2868 int minWidth = GetMinWidth(),
2869 minHeight = GetMinHeight(),
2870 maxWidth = GetMaxWidth(),
2871 maxHeight = GetMaxHeight();
2872
2873 if ((minWidth != -1) && (m_width < minWidth)) m_width = minWidth;
2874 if ((minHeight != -1) && (m_height < minHeight)) m_height = minHeight;
2875 if ((maxWidth != -1) && (m_width > maxWidth)) m_width = maxWidth;
2876 if ((maxHeight != -1) && (m_height > maxHeight)) m_height = maxHeight;
47d67540 2877
863e0817
RR
2878 int left_border = 0;
2879 int right_border = 0;
2880 int top_border = 0;
c50f1fb9 2881 int bottom_border = 0;
f03fc89f 2882
863e0817 2883 /* the default button has a border around it */
29f538ce 2884 if (GTK_WIDGET_CAN_DEFAULT(m_widget))
c50f1fb9 2885 {
863e0817
RR
2886 left_border = 6;
2887 right_border = 6;
2888 top_border = 6;
c50f1fb9 2889 bottom_border = 5;
863e0817 2890 }
c50f1fb9 2891
863e0817
RR
2892 DoMoveWindow( m_x-top_border,
2893 m_y-left_border,
2894 m_width+left_border+right_border,
2895 m_height+top_border+bottom_border );
54517652 2896 }
148cd9b6 2897
5b8a521e
RR
2898 if (m_hasScrolling)
2899 {
1e6feb95 2900 /* Sometimes the client area changes size without the
b6fa52db
RR
2901 whole windows's size changing, but if the whole
2902 windows's size doesn't change, no wxSizeEvent will
2903 normally be sent. Here we add an extra test if
2904 the client test has been changed and this will
2905 be used then. */
5b8a521e
RR
2906 GetClientSize( &m_oldClientWidth, &m_oldClientHeight );
2907 }
2908
54517652 2909/*
6d693bb4
RR
2910 wxPrintf( "OnSize sent from " );
2911 if (GetClassInfo() && GetClassInfo()->GetClassName())
2912 wxPrintf( GetClassInfo()->GetClassName() );
2913 wxPrintf( " %d %d %d %d\n", (int)m_x, (int)m_y, (int)m_width, (int)m_height );
2914*/
2915
30760ce7
RR
2916 if (!m_nativeSizeEvent)
2917 {
2918 wxSizeEvent event( wxSize(m_width,m_height), GetId() );
2919 event.SetEventObject( this );
2920 GetEventHandler()->ProcessEvent( event );
2921 }
6d693bb4 2922
0a164d4c 2923 m_resizing = false;
362c6693 2924}
c801d85f 2925
1e6feb95 2926void wxWindowGTK::OnInternalIdle()
9390a202 2927{
c7382f91
JS
2928 // Update style if the window was not yet realized
2929 // and SetBackgroundStyle(wxBG_STYLE_CUSTOM) was called
2930 if (m_needsStyleChange)
2931 {
2932 SetBackgroundStyle(GetBackgroundStyle());
2933 m_needsStyleChange = false;
2934 }
a589495e 2935
beab25bd 2936 // Update invalidated regions.
010afced 2937 GtkUpdate();
0fc5dbf5 2938
9146082c
RR
2939 wxCursor cursor = m_cursor;
2940 if (g_globalCursor.Ok()) cursor = g_globalCursor;
c50f1fb9 2941
f7a11f8c 2942 if (cursor.Ok())
9146082c 2943 {
3017f78d 2944 /* I now set the cursor anew in every OnInternalIdle call
b02da6b1
VZ
2945 as setting the cursor in a parent window also effects the
2946 windows above so that checking for the current cursor is
2947 not possible. */
148cd9b6 2948
9146082c 2949 if (m_wxwindow)
6a008b33 2950 {
da048e3d 2951 GdkWindow *window = GTK_PIZZA(m_wxwindow)->bin_window;
6a008b33 2952 if (window)
c50f1fb9 2953 gdk_window_set_cursor( window, cursor.GetCursor() );
6a008b33
VZ
2954
2955 if (!g_globalCursor.Ok())
2956 cursor = *wxSTANDARD_CURSOR;
2957
2958 window = m_widget->window;
5e014a0c 2959 if ((window) && !(GTK_WIDGET_NO_WINDOW(m_widget)))
9146082c 2960 gdk_window_set_cursor( window, cursor.GetCursor() );
5e014a0c 2961
6a008b33 2962 }
b3d006af 2963 else if ( m_widget )
6a008b33 2964 {
9146082c 2965 GdkWindow *window = m_widget->window;
b3d006af 2966 if ( window && !GTK_WIDGET_NO_WINDOW(m_widget) )
9146082c 2967 gdk_window_set_cursor( window, cursor.GetCursor() );
6a008b33 2968 }
9146082c 2969 }
6a008b33 2970
24bd6cb9 2971 if (wxUpdateUIEvent::CanUpdate(this) && IsShown())
e39af974 2972 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
9390a202
RR
2973}
2974
1e6feb95 2975void wxWindowGTK::DoGetSize( int *width, int *height ) const
c801d85f 2976{
82b978d7 2977 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
47d67540 2978
fb1585ae
RR
2979 if (width) (*width) = m_width;
2980 if (height) (*height) = m_height;
362c6693 2981}
c801d85f 2982
1e6feb95 2983void wxWindowGTK::DoSetClientSize( int width, int height )
c801d85f 2984{
82b978d7
RD
2985 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
2986
1ecc4d80 2987 if (!m_wxwindow)
c801d85f 2988 {
1ecc4d80 2989 SetSize( width, height );
c801d85f
KB
2990 }
2991 else
2992 {
1ecc4d80
RR
2993 int dw = 0;
2994 int dh = 0;
2995
1e6feb95 2996#ifndef __WXUNIVERSAL__
dbb61779 2997 if (HasFlag(wxRAISED_BORDER) || HasFlag(wxSUNKEN_BORDER) || HasFlag(wxBORDER_THEME))
98d3fdbe 2998 {
5e014a0c 2999 /* when using GTK 1.2 we set the shadow border size to 2 */
6a008b33 3000 dw += 2 * 2;
98d3fdbe
RR
3001 dh += 2 * 2;
3002 }
5e014a0c
RR
3003 if (HasFlag(wxSIMPLE_BORDER))
3004 {
3005 /* when using GTK 1.2 we set the simple border size to 1 */
3006 dw += 1 * 2;
3007 dh += 1 * 2;
3008 }
1e6feb95 3009#endif // __WXUNIVERSAL__
034be888 3010
5b8a521e 3011 if (m_hasScrolling)
98d3fdbe 3012 {
324dbfec 3013 GtkScrolledWindow *scroll_window = GTK_SCROLLED_WINDOW(m_widget);
2daa0ce9 3014
9000c624
RR
3015 GtkRequisition vscroll_req;
3016 vscroll_req.width = 2;
3017 vscroll_req.height = 2;
dd00f3f6 3018 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(scroll_window->vscrollbar) )->size_request )
9000c624 3019 (scroll_window->vscrollbar, &vscroll_req );
2daa0ce9 3020
9000c624
RR
3021 GtkRequisition hscroll_req;
3022 hscroll_req.width = 2;
3023 hscroll_req.height = 2;
dd00f3f6 3024 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(scroll_window->hscrollbar) )->size_request )
9000c624
RR
3025 (scroll_window->hscrollbar, &hscroll_req );
3026
dd00f3f6 3027 GtkScrolledWindowClass *scroll_class = GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget) );
324dbfec 3028
1ecc4d80
RR
3029 if (scroll_window->vscrollbar_visible)
3030 {
9000c624 3031 dw += vscroll_req.width;
1ecc4d80
RR
3032 dw += scroll_class->scrollbar_spacing;
3033 }
3034
3035 if (scroll_window->hscrollbar_visible)
3036 {
9000c624 3037 dh += hscroll_req.height;
63cc5d9d 3038 dh += scroll_class->scrollbar_spacing;
1ecc4d80 3039 }
9000c624 3040 }
1ecc4d80 3041
034be888 3042 SetSize( width+dw, height+dh );
1ecc4d80 3043 }
362c6693 3044}
c801d85f 3045
1e6feb95 3046void wxWindowGTK::DoGetClientSize( int *width, int *height ) const
c801d85f 3047{
82b978d7 3048 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
47d67540 3049
1ecc4d80
RR
3050 if (!m_wxwindow)
3051 {
3052 if (width) (*width) = m_width;
3053 if (height) (*height) = m_height;
c801d85f
KB
3054 }
3055 else
3056 {
1ecc4d80
RR
3057 int dw = 0;
3058 int dh = 0;
3059
1e6feb95 3060#ifndef __WXUNIVERSAL__
dbb61779 3061 if (HasFlag(wxRAISED_BORDER) || HasFlag(wxSUNKEN_BORDER) || HasFlag(wxBORDER_THEME))
98d3fdbe 3062 {
5e014a0c 3063 /* when using GTK 1.2 we set the shadow border size to 2 */
6a008b33 3064 dw += 2 * 2;
98d3fdbe
RR
3065 dh += 2 * 2;
3066 }
5e014a0c
RR
3067 if (HasFlag(wxSIMPLE_BORDER))
3068 {
3069 /* when using GTK 1.2 we set the simple border size to 1 */
3070 dw += 1 * 2;
3071 dh += 1 * 2;
3072 }
1e6feb95 3073#endif // __WXUNIVERSAL__
9000c624 3074
5b8a521e 3075 if (m_hasScrolling)
98d3fdbe 3076 {
6a008b33 3077 GtkScrolledWindow *scroll_window = GTK_SCROLLED_WINDOW(m_widget);
2daa0ce9 3078
9000c624
RR
3079 GtkRequisition vscroll_req;
3080 vscroll_req.width = 2;
3081 vscroll_req.height = 2;
dd00f3f6 3082 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(scroll_window->vscrollbar) )->size_request )
9000c624 3083 (scroll_window->vscrollbar, &vscroll_req );
2daa0ce9 3084
9000c624
RR
3085 GtkRequisition hscroll_req;
3086 hscroll_req.width = 2;
3087 hscroll_req.height = 2;
dd00f3f6 3088 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(scroll_window->hscrollbar) )->size_request )
9000c624
RR
3089 (scroll_window->hscrollbar, &hscroll_req );
3090
dd00f3f6 3091 GtkScrolledWindowClass *scroll_class = GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget) );
6a008b33 3092
1ecc4d80
RR
3093 if (scroll_window->vscrollbar_visible)
3094 {
9000c624 3095 dw += vscroll_req.width;
1ecc4d80
RR
3096 dw += scroll_class->scrollbar_spacing;
3097 }
3098
3099 if (scroll_window->hscrollbar_visible)
3100 {
9000c624 3101 dh += hscroll_req.height;
1ecc4d80
RR
3102 dh += scroll_class->scrollbar_spacing;
3103 }
6a008b33 3104 }
47d67540 3105
1ecc4d80
RR
3106 if (width) (*width) = m_width - dw;
3107 if (height) (*height) = m_height - dh;
3108 }
1e6feb95 3109
f94fca1b
RR
3110/*
3111 printf( "GetClientSize, name %s ", GetName().c_str() );
3112 if (width) printf( " width = %d", (*width) );
3113 if (height) printf( " height = %d", (*height) );
3114 printf( "\n" );
3115*/
362c6693 3116}
c801d85f 3117
1e6feb95 3118void wxWindowGTK::DoGetPosition( int *x, int *y ) const
c801d85f 3119{
82b978d7 3120 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
47d67540 3121
bf0c00c6
RR
3122 int dx = 0;
3123 int dy = 0;
3124 if (m_parent && m_parent->m_wxwindow)
3125 {
da048e3d 3126 GtkPizza *pizza = GTK_PIZZA(m_parent->m_wxwindow);
b02da6b1
VZ
3127 dx = pizza->xoffset;
3128 dy = pizza->yoffset;
bf0c00c6 3129 }
94633ad9 3130
496beb3f
VS
3131 if (x) (*x) = m_x - dx;
3132 if (y) (*y) = m_y - dy;
362c6693 3133}
c801d85f 3134
1e6feb95 3135void wxWindowGTK::DoClientToScreen( int *x, int *y ) const
c801d85f 3136{
82b978d7 3137 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
47d67540 3138
a2053b27
RR
3139 if (!m_widget->window) return;
3140
43a18898
RR
3141 GdkWindow *source = (GdkWindow *) NULL;
3142 if (m_wxwindow)
da048e3d 3143 source = GTK_PIZZA(m_wxwindow)->bin_window;
43a18898
RR
3144 else
3145 source = m_widget->window;
47d67540 3146
43a18898
RR
3147 int org_x = 0;
3148 int org_y = 0;
3149 gdk_window_get_origin( source, &org_x, &org_y );
c801d85f 3150
43a18898 3151 if (!m_wxwindow)
c801d85f 3152 {
43a18898
RR
3153 if (GTK_WIDGET_NO_WINDOW (m_widget))
3154 {
3155 org_x += m_widget->allocation.x;
3156 org_y += m_widget->allocation.y;
3157 }
362c6693 3158 }
47d67540 3159
43a18898
RR
3160 if (x) *x += org_x;
3161 if (y) *y += org_y;
362c6693 3162}
c801d85f 3163
1e6feb95 3164void wxWindowGTK::DoScreenToClient( int *x, int *y ) const
c801d85f 3165{
82b978d7 3166 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
47d67540 3167
a2053b27
RR
3168 if (!m_widget->window) return;
3169
1ecc4d80
RR
3170 GdkWindow *source = (GdkWindow *) NULL;
3171 if (m_wxwindow)
da048e3d 3172 source = GTK_PIZZA(m_wxwindow)->bin_window;
1ecc4d80
RR
3173 else
3174 source = m_widget->window;
47d67540 3175
1ecc4d80
RR
3176 int org_x = 0;
3177 int org_y = 0;
3178 gdk_window_get_origin( source, &org_x, &org_y );
c801d85f 3179
1ecc4d80 3180 if (!m_wxwindow)
c801d85f 3181 {
1ecc4d80
RR
3182 if (GTK_WIDGET_NO_WINDOW (m_widget))
3183 {
3184 org_x += m_widget->allocation.x;
3185 org_y += m_widget->allocation.y;
3186 }
362c6693 3187 }
47d67540 3188
1ecc4d80
RR
3189 if (x) *x -= org_x;
3190 if (y) *y -= org_y;
362c6693 3191}
c801d85f 3192
1e6feb95 3193bool wxWindowGTK::Show( bool show )
c801d85f 3194{
0a164d4c 3195 wxCHECK_MSG( (m_widget != NULL), false, wxT("invalid window") );
47d67540 3196
739730ca
RR
3197 if (!wxWindowBase::Show(show))
3198 {
3199 // nothing to do
0a164d4c 3200 return false;
739730ca 3201 }
8bbe427f 3202
f03fc89f
VZ
3203 if (show)
3204 gtk_widget_show( m_widget );
1ecc4d80 3205 else
f03fc89f 3206 gtk_widget_hide( m_widget );
8bbe427f 3207
2b5f62a0 3208 wxShowEvent eventShow(GetId(), show);
687706f5 3209 eventShow.SetEventObject(this);
2b5f62a0
VZ
3210
3211 GetEventHandler()->ProcessEvent(eventShow);
3212
0a164d4c 3213 return true;
362c6693 3214}
c801d85f 3215
47a8a4d5 3216void wxWindowGTK::DoEnable( bool enable )
c801d85f 3217{
15506ffe 3218 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
82b978d7 3219
f03fc89f
VZ
3220 gtk_widget_set_sensitive( m_widget, enable );
3221 if ( m_wxwindow )
3222 gtk_widget_set_sensitive( m_wxwindow, enable );
362c6693 3223}
c801d85f 3224
1e6feb95 3225int wxWindowGTK::GetCharHeight() const
2f2aa628 3226{
82b978d7 3227 wxCHECK_MSG( (m_widget != NULL), 12, wxT("invalid window") );
47d67540 3228
cc402e64
VZ
3229 wxFont font = GetFont();
3230 wxCHECK_MSG( font.Ok(), 12, wxT("invalid font") );
2f2aa628 3231
cc402e64 3232 GdkFont *gfont = font.GetInternalFont( 1.0 );
f03fc89f 3233
cc402e64 3234 return gfont->ascent + gfont->descent;
362c6693 3235}
c801d85f 3236
1e6feb95 3237int wxWindowGTK::GetCharWidth() const
c33c4050 3238{
82b978d7 3239 wxCHECK_MSG( (m_widget != NULL), 8, wxT("invalid window") );
47d67540 3240
cc402e64
VZ
3241 wxFont font = GetFont();
3242 wxCHECK_MSG( font.Ok(), 8, wxT("invalid font") );
47d67540 3243
cc402e64 3244 GdkFont *gfont = font.GetInternalFont( 1.0 );
ff8bfdbb 3245
cc402e64 3246 return gdk_string_width( gfont, "g" );
c33c4050
RR
3247}
3248
1e6feb95 3249void wxWindowGTK::GetTextExtent( const wxString& string,
0a164d4c
WS
3250 int *x,
3251 int *y,
3252 int *descent,
3253 int *externalLeading,
3254 const wxFont *theFont ) const
c33c4050 3255{
cc402e64 3256 wxFont fontToUse = theFont ? *theFont : GetFont();
47d67540 3257
223d09f6 3258 wxCHECK_RET( fontToUse.Ok(), wxT("invalid font") );
2b5f62a0 3259
0a164d4c 3260 if (string.empty())
48d011c8 3261 {
b15ed747
RR
3262 if (x) (*x) = 0;
3263 if (y) (*y) = 0;
48d011c8
RR
3264 return;
3265 }
47d67540 3266
463c1fa1 3267 GdkFont *font = fontToUse.GetInternalFont( 1.0 );
fab591c5 3268 if (x) (*x) = gdk_string_width( font, wxGTK_CONV( string ) );
463c1fa1
RR
3269 if (y) (*y) = font->ascent + font->descent;
3270 if (descent) (*descent) = font->descent;
3271 if (externalLeading) (*externalLeading) = 0; // ??
c33c4050
RR
3272}
3273
1e6feb95 3274void wxWindowGTK::SetFocus()
c801d85f 3275{
82b978d7 3276 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
6cad4f1b
VZ
3277 if ( m_hasFocus )
3278 {
3279 // don't do anything if we already have focus
3280 return;
3281 }
2daa0ce9 3282
354aa1e3
RR
3283 if (m_wxwindow)
3284 {
173348db 3285 if (!GTK_WIDGET_HAS_FOCUS (m_wxwindow))
b231914f 3286 {
173348db 3287 gtk_widget_grab_focus (m_wxwindow);
b231914f 3288 }
354aa1e3 3289 }
b231914f 3290 else if (m_widget)
c801d85f 3291 {
173348db 3292 if (GTK_WIDGET_CAN_FOCUS(m_widget) && !GTK_WIDGET_HAS_FOCUS (m_widget) )
463c1fa1 3293 {
0a164d4c 3294
d7fa7eaa 3295 if (!GTK_WIDGET_REALIZED(m_widget))
6aeb6f2a 3296 {
6cad4f1b
VZ
3297 // we can't set the focus to the widget now so we remember that
3298 // it should be focused and will do it later, during the idle
3299 // time, as soon as we can
3300 wxLogTrace(TRACE_FOCUS,
3301 _T("Delaying setting focus to %s(%s)"),
6aeb6f2a
VZ
3302 GetClassInfo()->GetClassName(), GetLabel().c_str());
3303
d7fa7eaa 3304 g_delayedFocus = this;
6aeb6f2a 3305 }
d7fa7eaa 3306 else
6aeb6f2a 3307 {
6cad4f1b
VZ
3308 wxLogTrace(TRACE_FOCUS,
3309 _T("Setting focus to %s(%s)"),
6aeb6f2a
VZ
3310 GetClassInfo()->GetClassName(), GetLabel().c_str());
3311
d7fa7eaa 3312 gtk_widget_grab_focus (m_widget);
6aeb6f2a 3313 }
463c1fa1 3314 }
0a164d4c 3315 else
eccd5602 3316 if (GTK_IS_CONTAINER(m_widget))
ff8bfdbb 3317 {
59060b8c 3318 gtk_container_focus( GTK_CONTAINER(m_widget), GTK_DIR_TAB_FORWARD );
ff8bfdbb
VZ
3319 }
3320 else
3321 {
6cad4f1b
VZ
3322 wxLogTrace(TRACE_FOCUS,
3323 _T("Can't set focus to %s(%s)"),
3324 GetClassInfo()->GetClassName(), GetLabel().c_str());
ff8bfdbb 3325 }
362c6693 3326 }
362c6693 3327}
c801d85f 3328
1e6feb95 3329bool wxWindowGTK::AcceptsFocus() const
b292e2f5 3330{
f03fc89f 3331 return m_acceptsFocus && wxWindowBase::AcceptsFocus();
b292e2f5
RR
3332}
3333
1e6feb95 3334bool wxWindowGTK::Reparent( wxWindowBase *newParentBase )
463c1fa1 3335{
0a164d4c 3336 wxCHECK_MSG( (m_widget != NULL), false, wxT("invalid window") );
c50f1fb9 3337
1e6feb95
VZ
3338 wxWindowGTK *oldParent = m_parent,
3339 *newParent = (wxWindowGTK *)newParentBase;
a2053b27 3340
5fd11f09
RR
3341 wxASSERT( GTK_IS_WIDGET(m_widget) );
3342
f03fc89f 3343 if ( !wxWindowBase::Reparent(newParent) )
0a164d4c 3344 return false;
8bbe427f 3345
5fd11f09
RR
3346 wxASSERT( GTK_IS_WIDGET(m_widget) );
3347
3348 /* prevent GTK from deleting the widget arbitrarily */
3349 gtk_widget_ref( m_widget );
3350
8ce63e9d
RR
3351 if (oldParent)
3352 {
3017f78d 3353 gtk_container_remove( GTK_CONTAINER(m_widget->parent), m_widget );
8ce63e9d 3354 }
c50f1fb9 3355
5fd11f09
RR
3356 wxASSERT( GTK_IS_WIDGET(m_widget) );
3357
8ce63e9d
RR
3358 if (newParent)
3359 {
3360 /* insert GTK representation */
3361 (*(newParent->m_insertCallback))(newParent, this);
3362 }
c50f1fb9 3363
5fd11f09
RR
3364 /* reverse: prevent GTK from deleting the widget arbitrarily */
3365 gtk_widget_unref( m_widget );
148cd9b6 3366
0a164d4c 3367 return true;
362c6693 3368}
c801d85f 3369
1e6feb95 3370void wxWindowGTK::DoAddChild(wxWindowGTK *child)
ddb6bc71 3371{
223d09f6 3372 wxASSERT_MSG( (m_widget != NULL), wxT("invalid window") );
ddb6bc71 3373
223d09f6 3374 wxASSERT_MSG( (child != NULL), wxT("invalid child window") );
ddb6bc71 3375
223d09f6 3376 wxASSERT_MSG( (m_insertCallback != NULL), wxT("invalid child insertion function") );
c50f1fb9 3377
ddb6bc71
RR
3378 /* add to list */
3379 AddChild( child );
c50f1fb9 3380
ddb6bc71
RR
3381 /* insert GTK representation */
3382 (*m_insertCallback)(this, child);
3383}
3384
1e6feb95 3385void wxWindowGTK::Raise()
362c6693 3386{
82b978d7
RD
3387 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
3388
fdfb8475
RR
3389 if (m_wxwindow && m_wxwindow->window)
3390 {
3391 gdk_window_raise( m_wxwindow->window );
3392 }
0a164d4c 3393 else if (m_widget->window)
fdfb8475
RR
3394 {
3395 gdk_window_raise( m_widget->window );
3396 }
362c6693
RR
3397}
3398
1e6feb95 3399void wxWindowGTK::Lower()
362c6693 3400{
82b978d7
RD
3401 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
3402
fdfb8475
RR
3403 if (m_wxwindow && m_wxwindow->window)
3404 {
3405 gdk_window_lower( m_wxwindow->window );
3406 }
0a164d4c 3407 else if (m_widget->window)
fdfb8475
RR
3408 {
3409 gdk_window_lower( m_widget->window );
3410 }
362c6693 3411}
c801d85f 3412
1e6feb95 3413bool wxWindowGTK::SetCursor( const wxCursor &cursor )
86b29a61 3414{
0a164d4c 3415 wxCHECK_MSG( (m_widget != NULL), false, wxT("invalid window") );
86b29a61 3416
ebdb3bc7 3417 if ( cursor.IsSameAs(m_cursor) )
0a164d4c 3418 return false;
f6bcfd97
BP
3419
3420 if (g_isIdle)
3421 wxapp_install_idle_handler();
1e6feb95 3422
ebdb3bc7
VZ
3423 return wxWindowBase::SetCursor( cursor.IsOk() ? cursor
3424 : *wxSTANDARD_CURSOR );
362c6693 3425}
c801d85f 3426
1e6feb95 3427void wxWindowGTK::WarpPointer( int x, int y )
4f22cf8d 3428{
82b978d7
RD
3429 wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
3430
3bcc8d15
RR
3431 // We provide this function ourselves as it is
3432 // missing in GDK (top of this file).
148cd9b6 3433
ed673c6a
RR
3434 GdkWindow *window = (GdkWindow*) NULL;
3435 if (m_wxwindow)
da048e3d 3436 window = GTK_PIZZA(m_wxwindow)->bin_window;
ed673c6a
RR
3437 else
3438 window = GetConnectWidget()->window;
148cd9b6 3439
ed673c6a
RR
3440 if (window)
3441 gdk_window_warp_pointer( window, x, y );
4f22cf8d
RR
3442}
3443
3013a903 3444
1e6feb95 3445void wxWindowGTK::Refresh( bool eraseBackground, const wxRect *rect )
c801d85f 3446{
a67f1484
VZ
3447 if (!m_widget)
3448 return;
3449 if (!m_widget->window)
3450 return;
2b5f62a0 3451
ea323db3
RR
3452 if (g_isIdle)
3453 wxapp_install_idle_handler();
2b5f62a0 3454
a67f1484 3455 wxRect myRect;
75625d79
SN
3456 if (m_wxwindow && rect)
3457 {
3458 myRect.SetSize(wxSize( m_wxwindow->allocation.width,
3459 m_wxwindow->allocation.height));
a67f1484
VZ
3460 if ( myRect.Intersect(*rect).IsEmpty() )
3461 {
75625d79
SN
3462 // nothing to do, rectangle is empty
3463 return;
a67f1484
VZ
3464 }
3465
75625d79
SN
3466 rect = &myRect;
3467 }
3468
a67f1484 3469 // schedule the area for later updating in GtkUpdate()
139adb6a 3470 if (eraseBackground && m_wxwindow && m_wxwindow->window)
c801d85f 3471 {
139adb6a
RR
3472 if (rect)
3473 {
3bcc8d15 3474 m_clearRegion.Union( rect->x, rect->y, rect->width, rect->height );
139adb6a
RR
3475 }
3476 else
3477 {
3bcc8d15
RR
3478 m_clearRegion.Clear();
3479 m_clearRegion.Union( 0, 0, m_wxwindow->allocation.width, m_wxwindow->allocation.height );
139adb6a
RR
3480 }
3481 }
ff8bfdbb 3482
3bcc8d15 3483 if (rect)
139adb6a
RR
3484 {
3485 if (m_wxwindow)
b02da6b1 3486 {
3bcc8d15 3487 m_updateRegion.Union( rect->x, rect->y, rect->width, rect->height );
b02da6b1 3488 }
ff8bfdbb 3489 else
b6fa52db 3490 {
3bcc8d15
RR
3491 GdkRectangle gdk_rect;
3492 gdk_rect.x = rect->x;
3493 gdk_rect.y = rect->y;
3494 gdk_rect.width = rect->width;
3495 gdk_rect.height = rect->height;
3496 gtk_widget_draw( m_widget, &gdk_rect );
b6fa52db 3497 }
362c6693 3498 }
c801d85f 3499 else
139adb6a 3500 {
139adb6a 3501 if (m_wxwindow)
b02da6b1 3502 {
3bcc8d15
RR
3503 m_updateRegion.Clear();
3504 m_updateRegion.Union( 0, 0, m_wxwindow->allocation.width, m_wxwindow->allocation.height );
b02da6b1 3505 }
139adb6a 3506 else
b6fa52db 3507 {
3bcc8d15 3508 gtk_widget_draw( m_widget, (GdkRectangle*) NULL );
b6fa52db 3509 }
139adb6a 3510 }
362c6693 3511}
c801d85f 3512
beab25bd 3513void wxWindowGTK::Update()
010afced
RR
3514{
3515 GtkUpdate();
1b965a9c
VZ
3516
3517 // when we call Update() we really want to update the window immediately on
90e572f1 3518 // screen, even if it means flushing the entire queue and hence slowing down
1b965a9c
VZ
3519 // everything -- but it should still be done, it's just that Update() should
3520 // be called very rarely
3521 gdk_flush();
010afced
RR
3522}
3523
3524void wxWindowGTK::GtkUpdate()
beab25bd
RR
3525{
3526 if (!m_updateRegion.IsEmpty())
23716407 3527 GtkSendPaintEvents();
a67f1484
VZ
3528
3529 // for consistency with other platforms (and also because it's convenient
3530 // to be able to update an entire TLW by calling Update() only once), we
3531 // should also update all our children here
3532 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
3533 node;
3534 node = node->GetNext() )
3535 {
3536 node->GetData()->GtkUpdate();
3537 }
beab25bd
RR
3538}
3539
3540void wxWindowGTK::GtkSendPaintEvents()
3541{
3bcc8d15
RR
3542 if (!m_wxwindow)
3543 {
3544 m_clearRegion.Clear();
3545 m_updateRegion.Clear();
3546 return;
3547 }
beab25bd 3548
f90566f5 3549 // Clip to paint region in wxClientDC
0a164d4c 3550 m_clipPaintRegion = true;
fab591c5 3551
b15ed747
RR
3552 // widget to draw on
3553 GtkPizza *pizza = GTK_PIZZA (m_wxwindow);
2b5f62a0 3554
aac97549 3555 if (GetThemeEnabled() && (GetBackgroundStyle() == wxBG_STYLE_SYSTEM))
f90566f5
RR
3556 {
3557 // find ancestor from which to steal background
cd5e74ba 3558 wxWindow *parent = wxGetTopLevelParent((wxWindow *)this);
f90566f5 3559 if (!parent)
cc06fe74 3560 parent = (wxWindow*)this;
2b5f62a0 3561
822cf31c 3562 if (GTK_WIDGET_MAPPED(parent->m_widget))
f90566f5 3563 {
822cf31c
KH
3564 wxRegionIterator upd( m_updateRegion );
3565 while (upd)
3566 {
3567 GdkRectangle rect;
3568 rect.x = upd.GetX();
3569 rect.y = upd.GetY();
3570 rect.width = upd.GetWidth();
3571 rect.height = upd.GetHeight();
3572
3573 gtk_paint_flat_box( parent->m_widget->style,
3574 pizza->bin_window,
3575 (GtkStateType)GTK_WIDGET_STATE(m_wxwindow),
3576 GTK_SHADOW_NONE,
3577 &rect,
3578 parent->m_widget,
3579 (char *)"base",
3580 0, 0, -1, -1 );
3581
60d8e886 3582 ++upd;
822cf31c 3583 }
f90566f5
RR
3584 }
3585 }
3586 else
b15ed747 3587
b15ed747 3588 // if (!m_clearRegion.IsEmpty()) // Always send an erase event under GTK 1.2
beab25bd 3589 {
3bcc8d15 3590 wxWindowDC dc( (wxWindow*)this );
62cb3cd8
RR
3591 if (m_clearRegion.IsEmpty())
3592 dc.SetClippingRegion( m_updateRegion );
3593 else
3594 dc.SetClippingRegion( m_clearRegion );
0fc5dbf5 3595
3bcc8d15
RR
3596 wxEraseEvent erase_event( GetId(), &dc );
3597 erase_event.SetEventObject( this );
0fc5dbf5 3598
c7382f91 3599 if (!GetEventHandler()->ProcessEvent(erase_event) && GetBackgroundStyle() != wxBG_STYLE_CUSTOM)
beab25bd 3600 {
994bc575
RR
3601 if (!g_eraseGC)
3602 {
f90566f5 3603 g_eraseGC = gdk_gc_new( pizza->bin_window );
994bc575
RR
3604 gdk_gc_set_fill( g_eraseGC, GDK_SOLID );
3605 }
1cd3409d 3606 gdk_gc_set_foreground( g_eraseGC, GetBackgroundColour().GetColor() );
0fc5dbf5 3607
3bcc8d15 3608 wxRegionIterator upd( m_clearRegion );
beab25bd
RR
3609 while (upd)
3610 {
f90566f5
RR
3611 gdk_draw_rectangle( pizza->bin_window, g_eraseGC, 1,
3612 upd.GetX(), upd.GetY(), upd.GetWidth(), upd.GetHeight() );
beab25bd
RR
3613 upd ++;
3614 }
3615 }
3bcc8d15 3616 m_clearRegion.Clear();
beab25bd 3617 }
beab25bd
RR
3618
3619 wxNcPaintEvent nc_paint_event( GetId() );
3620 nc_paint_event.SetEventObject( this );
3621 GetEventHandler()->ProcessEvent( nc_paint_event );
3622
3623 wxPaintEvent paint_event( GetId() );
3624 paint_event.SetEventObject( this );
3625 GetEventHandler()->ProcessEvent( paint_event );
3626
0a164d4c 3627 m_clipPaintRegion = false;
c89f5c02 3628
3cbab641 3629#if !defined(__WXUNIVERSAL__)
8f3e7ecc 3630 // The following code will result in all window-less widgets
77ffb593 3631 // being redrawn because the wxWidgets class is allowed to
8f3e7ecc 3632 // paint over the window-less widgets.
0fc5dbf5 3633
8f3e7ecc
RR
3634 GList *children = pizza->children;
3635 while (children)
c89f5c02 3636 {
8f3e7ecc
RR
3637 GtkPizzaChild *child = (GtkPizzaChild*) children->data;
3638 children = children->next;
2b5f62a0 3639
8f3e7ecc
RR
3640 if (GTK_WIDGET_NO_WINDOW (child->widget) &&
3641 GTK_WIDGET_DRAWABLE (child->widget))
3642 {
3643 // Get intersection of widget area and update region
3644 wxRegion region( m_updateRegion );
0fc5dbf5 3645
8f3e7ecc
RR
3646 GdkEventExpose gdk_event;
3647 gdk_event.type = GDK_EXPOSE;
3648 gdk_event.window = pizza->bin_window;
3649 gdk_event.count = 0;
4106cebb 3650 gdk_event.send_event = TRUE;
0fc5dbf5 3651
8f3e7ecc
RR
3652 wxRegionIterator upd( m_updateRegion );
3653 while (upd)
c89f5c02 3654 {
8f3e7ecc
RR
3655 GdkRectangle rect;
3656 rect.x = upd.GetX();
3657 rect.y = upd.GetY();
3658 rect.width = upd.GetWidth();
3659 rect.height = upd.GetHeight();
0fc5dbf5 3660
8f3e7ecc
RR
3661 if (gtk_widget_intersect (child->widget, &rect, &gdk_event.area))
3662 {
3663 gtk_widget_event (child->widget, (GdkEvent*) &gdk_event);
3664 }
0fc5dbf5 3665
8f3e7ecc 3666 upd ++;
c89f5c02
RR
3667 }
3668 }
3669 }
4106cebb 3670#endif // native GTK 1
c89f5c02
RR
3671
3672 m_updateRegion.Clear();
beab25bd
RR
3673}
3674
596f1d11 3675void wxWindowGTK::ClearBackground()
c801d85f 3676{
82b978d7
RD
3677 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
3678
f234c60c
RR
3679 if (m_wxwindow && m_wxwindow->window)
3680 {
d7fa7eaa
RR
3681 m_clearRegion.Clear();
3682 wxSize size( GetClientSize() );
3683 m_clearRegion.Union( 0,0,size.x,size.y );
2b5f62a0 3684
d7fa7eaa 3685 // Better do this in idle?
010afced 3686 GtkUpdate();
f234c60c 3687 }
362c6693 3688}
c801d85f 3689
ff8bfdbb 3690#if wxUSE_TOOLTIPS
1e6feb95 3691void wxWindowGTK::DoSetToolTip( wxToolTip *tip )
b1170810 3692{
f03fc89f 3693 wxWindowBase::DoSetToolTip(tip);
ff8bfdbb 3694
f03fc89f 3695 if (m_tooltip)
3379ed37 3696 m_tooltip->Apply( (wxWindow *)this );
b1170810
RR
3697}
3698
1e6feb95 3699void wxWindowGTK::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
b1170810 3700{
aa154cb1
RR
3701 wxString tmp( tip );
3702 gtk_tooltips_set_tip( tips, GetConnectWidget(), wxGTK_CONV(tmp), (gchar*) NULL );
301cd871 3703}
ff8bfdbb 3704#endif // wxUSE_TOOLTIPS
b1170810 3705
1e6feb95 3706bool wxWindowGTK::SetBackgroundColour( const wxColour &colour )
c801d85f 3707{
0a164d4c 3708 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid window") );
8bbe427f 3709
739730ca 3710 if (!wxWindowBase::SetBackgroundColour(colour))
44dfb5ce 3711 return false;
c50f1fb9 3712
5edef14e 3713 if (colour.Ok())
994bc575 3714 {
5edef14e
VS
3715 // We need the pixel value e.g. for background clearing.
3716 m_backgroundColour.CalcPixel(gtk_widget_get_colormap(m_widget));
994bc575 3717 }
ca298c88 3718
5edef14e 3719 // apply style change (forceStyle=true so that new style is applied
c7382f91
JS
3720 // even if the bg colour changed from valid to wxNullColour)
3721 if (GetBackgroundStyle() != wxBG_STYLE_CUSTOM)
3722 ApplyWidgetStyle(true);
ea323db3 3723
5edef14e 3724 return true;
6de97a3b
RR
3725}
3726
1e6feb95 3727bool wxWindowGTK::SetForegroundColour( const wxColour &colour )
6de97a3b 3728{
0a164d4c 3729 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid window") );
8bbe427f 3730
739730ca
RR
3731 if (!wxWindowBase::SetForegroundColour(colour))
3732 {
5edef14e 3733 return false;
739730ca 3734 }
0a164d4c 3735
5edef14e 3736 if (colour.Ok())
ea323db3 3737 {
5edef14e
VS
3738 // We need the pixel value e.g. for background clearing.
3739 m_foregroundColour.CalcPixel(gtk_widget_get_colormap(m_widget));
ea323db3 3740 }
f03fc89f 3741
5edef14e
VS
3742 // apply style change (forceStyle=true so that new style is applied
3743 // even if the bg colour changed from valid to wxNullColour):
3744 ApplyWidgetStyle(true);
3745
44dfb5ce 3746 return true;
58614078
RR
3747}
3748
5edef14e 3749GtkRcStyle *wxWindowGTK::CreateWidgetStyle(bool forceStyle)
58614078 3750{
f40fdaa3 3751 // do we need to apply any changes at all?
5edef14e 3752 if ( !forceStyle &&
984e8d0b
VS
3753 !m_font.Ok() &&
3754 !m_foregroundColour.Ok() && !m_backgroundColour.Ok() )
fb65642c 3755 {
f40fdaa3 3756 return NULL;
fb65642c
RR
3757 }
3758
f40fdaa3 3759 GtkRcStyle *style = gtk_rc_style_new();
1ecc4d80 3760
984e8d0b 3761 if ( m_font.Ok() )
db434467 3762 {
f40fdaa3
VS
3763 wxString xfontname = m_font.GetNativeFontInfo()->GetXFontName();
3764 style->fontset_name = g_strdup(xfontname.c_str());
288059b2 3765 }
1ecc4d80 3766
fe161a26 3767 if ( m_foregroundColour.Ok() )
1ecc4d80 3768 {
5edef14e 3769 GdkColor *fg = m_foregroundColour.GetColor();
0a164d4c 3770
5edef14e 3771 style->fg[GTK_STATE_NORMAL] = *fg;
f40fdaa3 3772 style->color_flags[GTK_STATE_NORMAL] = GTK_RC_FG;
0a164d4c 3773
5edef14e 3774 style->fg[GTK_STATE_PRELIGHT] = *fg;
f40fdaa3 3775 style->color_flags[GTK_STATE_PRELIGHT] = GTK_RC_FG;
0a164d4c 3776
5edef14e 3777 style->fg[GTK_STATE_ACTIVE] = *fg;
f40fdaa3 3778 style->color_flags[GTK_STATE_ACTIVE] = GTK_RC_FG;
1ecc4d80
RR
3779 }
3780
fe161a26 3781 if ( m_backgroundColour.Ok() )
1ecc4d80 3782 {
5edef14e
VS
3783 GdkColor *bg = m_backgroundColour.GetColor();
3784
3785 style->bg[GTK_STATE_NORMAL] = *bg;
3786 style->base[GTK_STATE_NORMAL] = *bg;
f40fdaa3
VS
3787 style->color_flags[GTK_STATE_NORMAL] = (GtkRcFlags)
3788 (style->color_flags[GTK_STATE_NORMAL] | GTK_RC_BG | GTK_RC_BASE);
0a164d4c 3789
5edef14e
VS
3790 style->bg[GTK_STATE_PRELIGHT] = *bg;
3791 style->base[GTK_STATE_PRELIGHT] = *bg;
f40fdaa3
VS
3792 style->color_flags[GTK_STATE_PRELIGHT] = (GtkRcFlags)
3793 (style->color_flags[GTK_STATE_PRELIGHT] | GTK_RC_BG | GTK_RC_BASE);
0a164d4c 3794
5edef14e
VS
3795 style->bg[GTK_STATE_ACTIVE] = *bg;
3796 style->base[GTK_STATE_ACTIVE] = *bg;
f40fdaa3
VS
3797 style->color_flags[GTK_STATE_ACTIVE] = (GtkRcFlags)
3798 (style->color_flags[GTK_STATE_ACTIVE] | GTK_RC_BG | GTK_RC_BASE);
0a164d4c 3799
5edef14e
VS
3800 style->bg[GTK_STATE_INSENSITIVE] = *bg;
3801 style->base[GTK_STATE_INSENSITIVE] = *bg;
f40fdaa3
VS
3802 style->color_flags[GTK_STATE_INSENSITIVE] = (GtkRcFlags)
3803 (style->color_flags[GTK_STATE_INSENSITIVE] | GTK_RC_BG | GTK_RC_BASE);
1ecc4d80 3804 }
0a164d4c 3805
f40fdaa3 3806 return style;
a81258be
RR
3807}
3808
f8e045e2 3809void wxWindowGTK::ApplyWidgetStyle(bool forceStyle)
a81258be 3810{
f8e045e2
RD
3811 GtkRcStyle *style = CreateWidgetStyle(forceStyle);
3812 if ( style )
3813 {
7074ce35 3814 DoApplyWidgetStyle(style);
f8e045e2
RD
3815 gtk_rc_style_unref(style);
3816 }
6dd18972
VS
3817
3818 // Style change may affect GTK+'s size calculation:
3819 InvalidateBestSize();
6de97a3b
RR
3820}
3821
7074ce35
VS
3822void wxWindowGTK::DoApplyWidgetStyle(GtkRcStyle *style)
3823{
3824 if (m_wxwindow)
7074ce35 3825 gtk_widget_modify_style(m_wxwindow, style);
7cb93e45
RR
3826 else
3827 gtk_widget_modify_style(m_widget, style);
7074ce35
VS
3828}
3829
c7382f91
JS
3830bool wxWindowGTK::SetBackgroundStyle(wxBackgroundStyle style)
3831{
3832 wxWindowBase::SetBackgroundStyle(style);
0a164d4c 3833
c7382f91
JS
3834 if (style == wxBG_STYLE_CUSTOM)
3835 {
3836 GdkWindow *window = (GdkWindow*) NULL;
3837 if (m_wxwindow)
3838 window = GTK_PIZZA(m_wxwindow)->bin_window;
3839 else
3840 window = GetConnectWidget()->window;
3841
3842 if (window)
3843 {
3844 // Make sure GDK/X11 doesn't refresh the window
3845 // automatically.
3846 gdk_window_set_back_pixmap( window, None, False );
3847#ifdef __X__
3848 Display* display = GDK_WINDOW_DISPLAY(window);
3849 XFlush(display);
3850#endif
3851 m_needsStyleChange = false;
3852 }
3853 else
3854 // Do in OnIdle, because the window is not yet available
3855 m_needsStyleChange = true;
0a164d4c 3856
c7382f91
JS
3857 // Don't apply widget style, or we get a grey background
3858 }
3859 else
3860 {
3861 // apply style change (forceStyle=true so that new style is applied
3862 // even if the bg colour changed from valid to wxNullColour):
3863 ApplyWidgetStyle(true);
3864 }
3865 return true;
3866}
7074ce35 3867
06cfab17 3868#if wxUSE_DRAG_AND_DROP
ac57418f 3869
1e6feb95 3870void wxWindowGTK::SetDropTarget( wxDropTarget *dropTarget )
c801d85f 3871{
82b978d7
RD
3872 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
3873
1ecc4d80 3874 GtkWidget *dnd_widget = GetConnectWidget();
47d67540 3875
1ecc4d80 3876 if (m_dropTarget) m_dropTarget->UnregisterWidget( dnd_widget );
47d67540 3877
1ecc4d80
RR
3878 if (m_dropTarget) delete m_dropTarget;
3879 m_dropTarget = dropTarget;
47d67540 3880
1ecc4d80 3881 if (m_dropTarget) m_dropTarget->RegisterWidget( dnd_widget );
362c6693 3882}
c801d85f 3883
f03fc89f 3884#endif // wxUSE_DRAG_AND_DROP
ac57418f 3885
1e6feb95 3886GtkWidget* wxWindowGTK::GetConnectWidget()
e3e65dac 3887{
1ecc4d80
RR
3888 GtkWidget *connect_widget = m_widget;
3889 if (m_wxwindow) connect_widget = m_wxwindow;
47d67540 3890
1ecc4d80 3891 return connect_widget;
e3e65dac 3892}
47d67540 3893
1e6feb95 3894bool wxWindowGTK::IsOwnGtkWindow( GdkWindow *window )
903f689b 3895{
148cd9b6 3896 if (m_wxwindow)
da048e3d 3897 return (window == GTK_PIZZA(m_wxwindow)->bin_window);
148cd9b6 3898
1ecc4d80 3899 return (window == m_widget->window);
903f689b
RR
3900}
3901
1e6feb95 3902bool wxWindowGTK::SetFont( const wxFont &font )
c801d85f 3903{
0a164d4c 3904 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid window") );
9c288e4d 3905
5edef14e
VS
3906 if (!wxWindowBase::SetFont(font))
3907 return false;
c801d85f 3908
5edef14e
VS
3909 // apply style change (forceStyle=true so that new style is applied
3910 // even if the font changed from valid to wxNullFont):
0a164d4c 3911 ApplyWidgetStyle(true);
5edef14e
VS
3912
3913 return true;
362c6693 3914}
c801d85f 3915
94633ad9 3916void wxWindowGTK::DoCaptureMouse()
c801d85f 3917{
82b978d7
RD
3918 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
3919
ed673c6a 3920 GdkWindow *window = (GdkWindow*) NULL;
b231914f
VZ
3921 if (m_wxwindow)
3922 window = GTK_PIZZA(m_wxwindow)->bin_window;
ed673c6a 3923 else
b231914f 3924 window = GetConnectWidget()->window;
148cd9b6 3925
e4606ed9 3926 wxCHECK_RET( window, _T("CaptureMouse() failed") );
c50f1fb9 3927
f516d986 3928 const wxCursor* cursor = &m_cursor;
cca602ac
JS
3929 if (!cursor->Ok())
3930 cursor = wxSTANDARD_CURSOR;
3931
ed673c6a 3932 gdk_pointer_grab( window, FALSE,
1ecc4d80
RR
3933 (GdkEventMask)
3934 (GDK_BUTTON_PRESS_MASK |
3935 GDK_BUTTON_RELEASE_MASK |
148cd9b6 3936 GDK_POINTER_MOTION_HINT_MASK |
1ecc4d80 3937 GDK_POINTER_MOTION_MASK),
ff8bfdbb 3938 (GdkWindow *) NULL,
cca602ac 3939 cursor->GetCursor(),
b02da6b1 3940 (guint32)GDK_CURRENT_TIME );
b231914f 3941 g_captureWindow = this;
0a164d4c 3942 g_captureWindowHasMouse = true;
362c6693 3943}
c801d85f 3944
94633ad9 3945void wxWindowGTK::DoReleaseMouse()
c801d85f 3946{
82b978d7
RD
3947 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
3948
e4606ed9 3949 wxCHECK_RET( g_captureWindow, wxT("can't release mouse - not captured") );
47d67540 3950
c43430bb
VS
3951 g_captureWindow = (wxWindowGTK*) NULL;
3952
ed673c6a 3953 GdkWindow *window = (GdkWindow*) NULL;
b231914f
VZ
3954 if (m_wxwindow)
3955 window = GTK_PIZZA(m_wxwindow)->bin_window;
ed673c6a 3956 else
b231914f 3957 window = GetConnectWidget()->window;
148cd9b6 3958
b02da6b1
VZ
3959 if (!window)
3960 return;
c50f1fb9 3961
b02da6b1 3962 gdk_pointer_ungrab ( (guint32)GDK_CURRENT_TIME );
362c6693 3963}
c801d85f 3964
1e6feb95
VZ
3965/* static */
3966wxWindow *wxWindowBase::GetCapture()
3967{
3968 return (wxWindow *)g_captureWindow;
3969}
3970
3971bool wxWindowGTK::IsRetained() const
c801d85f 3972{
0a164d4c 3973 return false;
362c6693 3974}
c801d85f 3975
1e6feb95 3976void wxWindowGTK::SetScrollbar( int orient, int pos, int thumbVisible,
cb43b372 3977 int range, bool refresh )
c801d85f 3978{
82b978d7
RD
3979 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
3980
223d09f6 3981 wxCHECK_RET( m_wxwindow != NULL, wxT("window needs client area for scrolling") );
c801d85f 3982
0a164d4c 3983 m_hasScrolling = true;
47d67540 3984
1ecc4d80 3985 if (orient == wxHORIZONTAL)
cb43b372 3986 {
1ecc4d80
RR
3987 float fpos = (float)pos;
3988 float frange = (float)range;
3989 float fthumb = (float)thumbVisible;
3990 if (fpos > frange-fthumb) fpos = frange-fthumb;
3991 if (fpos < 0.0) fpos = 0.0;
3992
3993 if ((fabs(frange-m_hAdjust->upper) < 0.2) &&
3994 (fabs(fthumb-m_hAdjust->page_size) < 0.2))
3995 {
3996 SetScrollPos( orient, pos, refresh );
3997 return;
3998 }
47d67540 3999
1ecc4d80 4000 m_oldHorizontalPos = fpos;
47d67540 4001
1ecc4d80
RR
4002 m_hAdjust->lower = 0.0;
4003 m_hAdjust->upper = frange;
4004 m_hAdjust->value = fpos;
4005 m_hAdjust->step_increment = 1.0;
4006 m_hAdjust->page_increment = (float)(wxMax(fthumb,0));
4007 m_hAdjust->page_size = fthumb;
cb43b372 4008 }
1ecc4d80
RR
4009 else
4010 {
4011 float fpos = (float)pos;
4012 float frange = (float)range;
4013 float fthumb = (float)thumbVisible;
4014 if (fpos > frange-fthumb) fpos = frange-fthumb;
4015 if (fpos < 0.0) fpos = 0.0;
4016
4017 if ((fabs(frange-m_vAdjust->upper) < 0.2) &&
4018 (fabs(fthumb-m_vAdjust->page_size) < 0.2))
4019 {
4020 SetScrollPos( orient, pos, refresh );
4021 return;
4022 }
47d67540 4023
1ecc4d80 4024 m_oldVerticalPos = fpos;
47d67540 4025
1ecc4d80
RR
4026 m_vAdjust->lower = 0.0;
4027 m_vAdjust->upper = frange;
4028 m_vAdjust->value = fpos;
4029 m_vAdjust->step_increment = 1.0;
4030 m_vAdjust->page_increment = (float)(wxMax(fthumb,0));
4031 m_vAdjust->page_size = fthumb;
4032 }
47d67540 4033
eb082a08
RR
4034 if (orient == wxHORIZONTAL)
4035 gtk_signal_emit_by_name( GTK_OBJECT(m_hAdjust), "changed" );
4036 else
4037 gtk_signal_emit_by_name( GTK_OBJECT(m_vAdjust), "changed" );
362c6693 4038}
c801d85f 4039
87a3ebe9
VZ
4040void wxWindowGTK::GtkUpdateScrollbar(int orient)
4041{
4042 GtkAdjustment *adj = orient == wxHORIZONTAL ? m_hAdjust : m_vAdjust;
4043 GtkSignalFunc fn = orient == wxHORIZONTAL
4044 ? (GtkSignalFunc)gtk_window_hscroll_callback
4045 : (GtkSignalFunc)gtk_window_vscroll_callback;
4046
4047 gtk_signal_disconnect_by_func(GTK_OBJECT(adj), fn, (gpointer)this);
4048 gtk_signal_emit_by_name(GTK_OBJECT(adj), "value_changed");
4049 gtk_signal_connect(GTK_OBJECT(adj), "value_changed", fn, (gpointer)this);
4050}
4051
1e6feb95 4052void wxWindowGTK::SetScrollPos( int orient, int pos, bool WXUNUSED(refresh) )
c801d85f 4053{
82b978d7 4054 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
223d09f6 4055 wxCHECK_RET( m_wxwindow != NULL, wxT("window needs client area for scrolling") );
1ecc4d80 4056
87a3ebe9 4057 GtkAdjustment *adj = orient == wxHORIZONTAL ? m_hAdjust : m_vAdjust;
1ecc4d80 4058
87a3ebe9
VZ
4059 float fpos = (float)pos;
4060 if (fpos > adj->upper - adj->page_size)
4061 fpos = adj->upper - adj->page_size;
4062 if (fpos < 0.0)
4063 fpos = 0.0;
4064 *(orient == wxHORIZONTAL ? &m_oldHorizontalPos : &m_oldVerticalPos) = fpos;
ff8bfdbb 4065
87a3ebe9
VZ
4066 if (fabs(fpos-adj->value) < 0.2)
4067 return;
4068 adj->value = fpos;
47d67540 4069
87a3ebe9 4070 if ( m_wxwindow->window )
47d67540 4071 {
cb43b372 4072 }
362c6693 4073}
c801d85f 4074
1e6feb95 4075int wxWindowGTK::GetScrollThumb( int orient ) const
c801d85f 4076{
82b978d7
RD
4077 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid window") );
4078
223d09f6 4079 wxCHECK_MSG( m_wxwindow != NULL, 0, wxT("window needs client area for scrolling") );
47d67540 4080
1ecc4d80
RR
4081 if (orient == wxHORIZONTAL)
4082 return (int)(m_hAdjust->page_size+0.5);
4083 else
4084 return (int)(m_vAdjust->page_size+0.5);
362c6693 4085}
c801d85f 4086
1e6feb95 4087int wxWindowGTK::GetScrollPos( int orient ) const
c801d85f 4088{
82b978d7 4089 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid window") );
47d67540 4090
223d09f6 4091 wxCHECK_MSG( m_wxwindow != NULL, 0, wxT("window needs client area for scrolling") );
c801d85f 4092
1ecc4d80
RR
4093 if (orient == wxHORIZONTAL)
4094 return (int)(m_hAdjust->value+0.5);
4095 else
4096 return (int)(m_vAdjust->value+0.5);
362c6693 4097}
c801d85f 4098
1e6feb95 4099int wxWindowGTK::GetScrollRange( int orient ) const
c801d85f 4100{
82b978d7 4101 wxCHECK_MSG( m_widget != NULL, 0, wxT("invalid window") );
47d67540 4102
223d09f6 4103 wxCHECK_MSG( m_wxwindow != NULL, 0, wxT("window needs client area for scrolling") );
c801d85f 4104
1ecc4d80
RR
4105 if (orient == wxHORIZONTAL)
4106 return (int)(m_hAdjust->upper+0.5);
4107 else
4108 return (int)(m_vAdjust->upper+0.5);
362c6693 4109}
c801d85f 4110
1e6feb95 4111void wxWindowGTK::ScrollWindow( int dx, int dy, const wxRect* WXUNUSED(rect) )
c801d85f 4112{
82b978d7
RD
4113 wxCHECK_RET( m_widget != NULL, wxT("invalid window") );
4114
223d09f6 4115 wxCHECK_RET( m_wxwindow != NULL, wxT("window needs client area for scrolling") );
2b5f62a0 4116
f47ae6e7 4117 // No scrolling requested.
8e217128 4118 if ((dx == 0) && (dy == 0)) return;
0fc5dbf5 4119
35917d22
RR
4120 if (!m_updateRegion.IsEmpty())
4121 {
4122 m_updateRegion.Offset( dx, dy );
0fc5dbf5 4123
35917d22
RR
4124 int cw = 0;
4125 int ch = 0;
4126 GetClientSize( &cw, &ch );
4127 m_updateRegion.Intersect( 0, 0, cw, ch );
4128 }
0fc5dbf5 4129
3bcc8d15
RR
4130 if (!m_clearRegion.IsEmpty())
4131 {
4132 m_clearRegion.Offset( dx, dy );
0fc5dbf5 4133
3bcc8d15
RR
4134 int cw = 0;
4135 int ch = 0;
4136 GetClientSize( &cw, &ch );
4137 m_clearRegion.Intersect( 0, 0, cw, ch );
4138 }
3fc6e5fa 4139
0a164d4c 4140 m_clipPaintRegion = true;
0fc5dbf5 4141
da048e3d 4142 gtk_pizza_scroll( GTK_PIZZA(m_wxwindow), -dx, -dy );
0fc5dbf5 4143
0a164d4c 4144 m_clipPaintRegion = false;
c801d85f 4145}
3723b7b1 4146
015dca24
MR
4147void wxWindowGTK::SetWindowStyleFlag( long style )
4148{
4149 // Updates the internal variable. NB: Now m_windowStyle bits carry the _new_ style values already
4150 wxWindowBase::SetWindowStyleFlag(style);
4151}
4e5a4c69 4152
3723b7b1
JS
4153// Find the wxWindow at the current mouse position, also returning the mouse
4154// position.
4155wxWindow* wxFindWindowAtPointer(wxPoint& pt)
4156{
59a12e90
JS
4157 pt = wxGetMousePosition();
4158 wxWindow* found = wxFindWindowAtPoint(pt);
4159 return found;
3723b7b1
JS
4160}
4161
4162// Get the current mouse position.
4163wxPoint wxGetMousePosition()
4164{
59a12e90
JS
4165 /* This crashes when used within wxHelpContext,
4166 so we have to use the X-specific implementation below.
4167 gint x, y;
4168 GdkModifierType *mask;
4169 (void) gdk_window_get_pointer(NULL, &x, &y, mask);
4170
4171 return wxPoint(x, y);
4172 */
4173
3723b7b1
JS
4174 int x, y;
4175 GdkWindow* windowAtPtr = gdk_window_at_pointer(& x, & y);
59a12e90 4176
37d81cc2 4177 Display *display = windowAtPtr ? GDK_WINDOW_XDISPLAY(windowAtPtr) : GDK_DISPLAY();
59a12e90
JS
4178 Window rootWindow = RootWindowOfScreen (DefaultScreenOfDisplay(display));
4179 Window rootReturn, childReturn;
4180 int rootX, rootY, winX, winY;
4181 unsigned int maskReturn;
4182
4183 XQueryPointer (display,
5cd09f0b
RR
4184 rootWindow,
4185 &rootReturn,
59a12e90
JS
4186 &childReturn,
4187 &rootX, &rootY, &winX, &winY, &maskReturn);
4188 return wxPoint(rootX, rootY);
4189
3723b7b1
JS
4190}
4191
224016a8
JS
4192// Needed for implementing e.g. combobox on wxGTK within a modal dialog.
4193void wxAddGrab(wxWindow* window)
4194{
4195 gtk_grab_add( (GtkWidget*) window->GetHandle() );
4196}
4197
4198void wxRemoveGrab(wxWindow* window)
4199{
4200 gtk_grab_remove( (GtkWidget*) window->GetHandle() );
4201}
4202
4e5a4c69 4203// ----------------------------------------------------------------------------
224016a8 4204// wxWinModule
4e5a4c69
RR
4205// ----------------------------------------------------------------------------
4206
4207class wxWinModule : public wxModule
4208{
4209public:
4210 bool OnInit();
4211 void OnExit();
4212
4213private:
4214 DECLARE_DYNAMIC_CLASS(wxWinModule)
4215};
4216
4217IMPLEMENT_DYNAMIC_CLASS(wxWinModule, wxModule)
4218
4219bool wxWinModule::OnInit()
4220{
994bc575
RR
4221 // g_eraseGC = gdk_gc_new( GDK_ROOT_PARENT() );
4222 // gdk_gc_set_fill( g_eraseGC, GDK_SOLID );
0fc5dbf5 4223
0a164d4c 4224 return true;
4e5a4c69
RR
4225}
4226
4227void wxWinModule::OnExit()
4228{
994bc575
RR
4229 if (g_eraseGC)
4230 gdk_gc_unref( g_eraseGC );
4e5a4c69 4231}
08f53168
PC
4232
4233GdkWindow* wxWindowGTK::GTKGetDrawingWindow() const
4234{
4235 GdkWindow* window = NULL;
4236 if (m_wxwindow)
4237 window = GTK_PIZZA(m_wxwindow)->bin_window;
4238 return window;
4239}