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