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