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