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