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