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