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