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