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