1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/window.cpp
3 // Purpose: wxWindowGTK implementation
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
14 #define XWarpPointer XWARPPOINTER
17 #include "wx/window.h"
22 #include "wx/toplevel.h"
23 #include "wx/dcclient.h"
25 #include "wx/settings.h"
26 #include "wx/msgdlg.h"
31 #include "wx/tooltip.h"
33 #include "wx/fontutil.h"
34 #include "wx/scopeguard.h"
35 #include "wx/sysopt.h"
39 #include "wx/gtk/private.h"
40 #include "wx/gtk/private/win_gtk.h"
41 #include "wx/gtk/private/event.h"
42 using namespace wxGTKImpl
;
46 #include <gdk/gdkkeysyms.h>
47 #if GTK_CHECK_VERSION(3,0,0)
48 #include <gdk/gdkkeysyms-compat.h>
51 //-----------------------------------------------------------------------------
52 // documentation on internals
53 //-----------------------------------------------------------------------------
56 I have been asked several times about writing some documentation about
57 the GTK port of wxWidgets, especially its internal structures. Obviously,
58 you cannot understand wxGTK without knowing a little about the GTK, but
59 some more information about what the wxWindow, which is the base class
60 for all other window classes, does seems required as well.
64 What does wxWindow do? It contains the common interface for the following
65 jobs of its descendants:
67 1) Define the rudimentary behaviour common to all window classes, such as
68 resizing, intercepting user input (so as to make it possible to use these
69 events for special purposes in a derived class), window names etc.
71 2) Provide the possibility to contain and manage children, if the derived
72 class is allowed to contain children, which holds true for those window
73 classes which do not display a native GTK widget. To name them, these
74 classes are wxPanel, wxScrolledWindow, wxDialog, wxFrame. The MDI frame-
75 work classes are a special case and are handled a bit differently from
76 the rest. The same holds true for the wxNotebook class.
78 3) Provide the possibility to draw into a client area of a window. This,
79 too, only holds true for classes that do not display a native GTK widget
82 4) Provide the entire mechanism for scrolling widgets. This actual inter-
83 face for this is usually in wxScrolledWindow, but the GTK implementation
86 5) A multitude of helper or extra methods for special purposes, such as
87 Drag'n'Drop, managing validators etc.
89 6) Display a border (sunken, raised, simple or none).
91 Normally one might expect, that one wxWidgets window would always correspond
92 to one GTK widget. Under GTK, there is no such all-round widget that has all
93 the functionality. Moreover, the GTK defines a client area as a different
94 widget from the actual widget you are handling. Last but not least some
95 special classes (e.g. wxFrame) handle different categories of widgets and
96 still have the possibility to draw something in the client area.
97 It was therefore required to write a special purpose GTK widget, that would
98 represent a client area in the sense of wxWidgets capable to do the jobs
99 2), 3) and 4). I have written this class and it resides in win_gtk.c of
102 All windows must have a widget, with which they interact with other under-
103 lying GTK widgets. It is this widget, e.g. that has to be resized etc and
104 the wxWindow class has a member variable called m_widget which holds a
105 pointer to this widget. When the window class represents a GTK native widget,
106 this is (in most cases) the only GTK widget the class manages. E.g. the
107 wxStaticText class handles only a GtkLabel widget a pointer to which you
108 can find in m_widget (defined in wxWindow)
110 When the class has a client area for drawing into and for containing children
111 it has to handle the client area widget (of the type wxPizza, defined in
112 win_gtk.cpp), but there could be any number of widgets, handled by a class.
113 The common rule for all windows is only, that the widget that interacts with
114 the rest of GTK must be referenced in m_widget and all other widgets must be
115 children of this widget on the GTK level. The top-most widget, which also
116 represents the client area, must be in the m_wxwindow field and must be of
119 As I said, the window classes that display a GTK native widget only have
120 one widget, so in the case of e.g. the wxButton class m_widget holds a
121 pointer to a GtkButton widget. But windows with client areas (for drawing
122 and children) have a m_widget field that is a pointer to a GtkScrolled-
123 Window and a m_wxwindow field that is pointer to a wxPizza and this
124 one is (in the GTK sense) a child of the GtkScrolledWindow.
126 If the m_wxwindow field is set, then all input to this widget is inter-
127 cepted and sent to the wxWidgets class. If not, all input to the widget
128 that gets pointed to by m_widget gets intercepted and sent to the class.
132 The design of scrolling in wxWidgets is markedly different from that offered
133 by the GTK itself and therefore we cannot simply take it as it is. In GTK,
134 clicking on a scrollbar belonging to scrolled window will inevitably move
135 the window. In wxWidgets, the scrollbar will only emit an event, send this
136 to (normally) a wxScrolledWindow and that class will call ScrollWindow()
137 which actually moves the window and its sub-windows. Note that wxPizza
138 memorizes how much it has been scrolled but that wxWidgets forgets this
139 so that the two coordinates systems have to be kept in synch. This is done
140 in various places using the pizza->m_scroll_x and pizza->m_scroll_y values.
144 Singularly the most broken code in GTK is the code that is supposed to
145 inform subwindows (child windows) about new positions. Very often, duplicate
146 events are sent without changes in size or position, equally often no
147 events are sent at all (All this is due to a bug in the GtkContainer code
148 which got fixed in GTK 1.2.6). For that reason, wxGTK completely ignores
149 GTK's own system and it simply waits for size events for toplevel windows
150 and then iterates down the respective size events to all window. This has
151 the disadvantage that windows might get size events before the GTK widget
152 actually has the reported size. This doesn't normally pose any problem, but
153 the OpenGL drawing routines rely on correct behaviour. Therefore, I have
154 added the m_nativeSizeEvents flag, which is true only for the OpenGL canvas,
155 i.e. the wxGLCanvas will emit a size event, when (and not before) the X11
156 window that is used for OpenGL output really has that size (as reported by
161 If someone at some point of time feels the immense desire to have a look at,
162 change or attempt to optimise the Refresh() logic, this person will need an
163 intimate understanding of what "draw" and "expose" events are and what
164 they are used for, in particular when used in connection with GTK's
165 own windowless widgets. Beware.
169 Cursors, too, have been a constant source of pleasure. The main difficulty
170 is that a GdkWindow inherits a cursor if the programmer sets a new cursor
171 for the parent. To prevent this from doing too much harm, SetCursor calls
172 GTKUpdateCursor, which will recursively re-set the cursors of all child windows.
173 Also don't forget that cursors (like much else) are connected to GdkWindows,
174 not GtkWidgets and that the "window" field of a GtkWidget might very well
175 point to the GdkWindow of the parent widget (-> "window-less widget") and
176 that the two obviously have very different meanings.
179 //-----------------------------------------------------------------------------
181 //-----------------------------------------------------------------------------
183 // Don't allow event propagation during drag
184 bool g_blockEventsOnDrag
;
185 // Don't allow mouse event propagation during scroll
186 bool g_blockEventsOnScroll
;
187 extern wxCursor g_globalCursor
;
189 // mouse capture state: the window which has it and if the mouse is currently
191 static wxWindowGTK
*g_captureWindow
= NULL
;
192 static bool g_captureWindowHasMouse
= false;
194 // The window that currently has focus:
195 static wxWindowGTK
*gs_currentFocus
= NULL
;
196 // The window that is scheduled to get focus in the next event loop iteration
197 // or NULL if there's no pending focus change:
198 static wxWindowGTK
*gs_pendingFocus
= NULL
;
200 // the window that has deferred focus-out event pending, if any (see
201 // GTKAddDeferredFocusOut() for details)
202 static wxWindowGTK
*gs_deferredFocusOut
= NULL
;
204 // global variables because GTK+ DnD want to have the
205 // mouse event that caused it
206 GdkEvent
*g_lastMouseEvent
= NULL
;
207 int g_lastButtonNumber
= 0;
209 //-----------------------------------------------------------------------------
211 //-----------------------------------------------------------------------------
213 // the trace mask used for the focus debugging messages
214 #define TRACE_FOCUS wxT("focus")
216 //-----------------------------------------------------------------------------
217 // "size_request" of m_widget
218 //-----------------------------------------------------------------------------
222 wxgtk_window_size_request_callback(GtkWidget
* WXUNUSED(widget
),
223 GtkRequisition
*requisition
,
227 win
->GetSize( &w
, &h
);
233 requisition
->height
= h
;
234 requisition
->width
= w
;
238 //-----------------------------------------------------------------------------
239 // "expose_event" of m_wxwindow
240 //-----------------------------------------------------------------------------
244 gtk_window_expose_callback( GtkWidget
*,
245 GdkEventExpose
*gdk_event
,
248 if (gdk_event
->window
== win
->GTKGetDrawingWindow())
250 win
->GetUpdateRegion() = wxRegion( gdk_event
->region
);
251 win
->GtkSendPaintEvents();
253 // Let parent window draw window-less widgets
258 #ifndef __WXUNIVERSAL__
259 //-----------------------------------------------------------------------------
260 // "expose_event" from m_wxwindow->parent, for drawing border
261 //-----------------------------------------------------------------------------
265 expose_event_border(GtkWidget
* widget
, GdkEventExpose
* gdk_event
, wxWindow
* win
)
267 if (gdk_event
->window
!= gtk_widget_get_parent_window(win
->m_wxwindow
))
274 gtk_widget_get_allocation(win
->m_wxwindow
, &alloc
);
275 const int x
= alloc
.x
;
276 const int y
= alloc
.y
;
277 const int w
= alloc
.width
;
278 const int h
= alloc
.height
;
280 if (w
<= 0 || h
<= 0)
283 if (win
->HasFlag(wxBORDER_SIMPLE
))
285 gdk_draw_rectangle(gdk_event
->window
,
286 gtk_widget_get_style(widget
)->black_gc
, false, x
, y
, w
- 1, h
- 1);
290 GtkShadowType shadow
= GTK_SHADOW_IN
;
291 if (win
->HasFlag(wxBORDER_RAISED
))
292 shadow
= GTK_SHADOW_OUT
;
294 // Style detail to use
296 if (win
->m_widget
== win
->m_wxwindow
)
297 // for non-scrollable wxWindows
300 // for scrollable ones
303 // clip rect is required to avoid painting background
304 // over upper left (w,h) of parent window
305 GdkRectangle clipRect
= { x
, y
, w
, h
};
307 gtk_widget_get_style(win
->m_wxwindow
), gdk_event
->window
, GTK_STATE_NORMAL
,
308 shadow
, &clipRect
, wxGTKPrivate
::GetEntryWidget(), detail
, x
, y
, w
, h
);
314 //-----------------------------------------------------------------------------
315 // "parent_set" from m_wxwindow
316 //-----------------------------------------------------------------------------
320 parent_set(GtkWidget
* widget
, GtkWidget
* old_parent
, wxWindow
* win
)
324 g_signal_handlers_disconnect_by_func(
325 old_parent
, (void*)expose_event_border
, win
);
327 GtkWidget
* parent
= gtk_widget_get_parent(widget
);
330 g_signal_connect_after(parent
, "expose_event",
331 G_CALLBACK(expose_event_border
), win
);
335 #endif // !__WXUNIVERSAL__
337 //-----------------------------------------------------------------------------
338 // "key_press_event" from any window
339 //-----------------------------------------------------------------------------
341 // set WXTRACE to this to see the key event codes on the console
342 #define TRACE_KEYS wxT("keyevent")
344 // translates an X key symbol to WXK_XXX value
346 // if isChar is true it means that the value returned will be used for EVT_CHAR
347 // event and then we choose the logical WXK_XXX, i.e. '/' for GDK_KP_Divide,
348 // for example, while if it is false it means that the value is going to be
349 // used for KEY_DOWN/UP events and then we translate GDK_KP_Divide to
351 static long wxTranslateKeySymToWXKey(KeySym keysym
, bool isChar
)
357 // Shift, Control and Alt don't generate the CHAR events at all
360 key_code
= isChar ?
0 : WXK_SHIFT
;
364 key_code
= isChar ?
0 : WXK_CONTROL
;
372 key_code
= isChar ?
0 : WXK_ALT
;
375 // neither do the toggle modifies
376 case GDK_Scroll_Lock
:
377 key_code
= isChar ?
0 : WXK_SCROLL
;
381 key_code
= isChar ?
0 : WXK_CAPITAL
;
385 key_code
= isChar ?
0 : WXK_NUMLOCK
;
389 // various other special keys
402 case GDK_ISO_Left_Tab
:
409 key_code
= WXK_RETURN
;
413 key_code
= WXK_CLEAR
;
417 key_code
= WXK_PAUSE
;
421 key_code
= WXK_SELECT
;
425 key_code
= WXK_PRINT
;
429 key_code
= WXK_EXECUTE
;
433 key_code
= WXK_ESCAPE
;
436 // cursor and other extended keyboard keys
438 key_code
= WXK_DELETE
;
454 key_code
= WXK_RIGHT
;
461 case GDK_Prior
: // == GDK_Page_Up
462 key_code
= WXK_PAGEUP
;
465 case GDK_Next
: // == GDK_Page_Down
466 key_code
= WXK_PAGEDOWN
;
478 key_code
= WXK_INSERT
;
493 key_code
= (isChar ?
'0' : int(WXK_NUMPAD0
)) + keysym
- GDK_KP_0
;
497 key_code
= isChar ?
' ' : int(WXK_NUMPAD_SPACE
);
501 key_code
= isChar ? WXK_TAB
: WXK_NUMPAD_TAB
;
505 key_code
= isChar ? WXK_RETURN
: WXK_NUMPAD_ENTER
;
509 key_code
= isChar ? WXK_F1
: WXK_NUMPAD_F1
;
513 key_code
= isChar ? WXK_F2
: WXK_NUMPAD_F2
;
517 key_code
= isChar ? WXK_F3
: WXK_NUMPAD_F3
;
521 key_code
= isChar ? WXK_F4
: WXK_NUMPAD_F4
;
525 key_code
= isChar ? WXK_HOME
: WXK_NUMPAD_HOME
;
529 key_code
= isChar ? WXK_LEFT
: WXK_NUMPAD_LEFT
;
533 key_code
= isChar ? WXK_UP
: WXK_NUMPAD_UP
;
537 key_code
= isChar ? WXK_RIGHT
: WXK_NUMPAD_RIGHT
;
541 key_code
= isChar ? WXK_DOWN
: WXK_NUMPAD_DOWN
;
544 case GDK_KP_Prior
: // == GDK_KP_Page_Up
545 key_code
= isChar ? WXK_PAGEUP
: WXK_NUMPAD_PAGEUP
;
548 case GDK_KP_Next
: // == GDK_KP_Page_Down
549 key_code
= isChar ? WXK_PAGEDOWN
: WXK_NUMPAD_PAGEDOWN
;
553 key_code
= isChar ? WXK_END
: WXK_NUMPAD_END
;
557 key_code
= isChar ? WXK_HOME
: WXK_NUMPAD_BEGIN
;
561 key_code
= isChar ? WXK_INSERT
: WXK_NUMPAD_INSERT
;
565 key_code
= isChar ? WXK_DELETE
: WXK_NUMPAD_DELETE
;
569 key_code
= isChar ?
'=' : int(WXK_NUMPAD_EQUAL
);
572 case GDK_KP_Multiply
:
573 key_code
= isChar ?
'*' : int(WXK_NUMPAD_MULTIPLY
);
577 key_code
= isChar ?
'+' : int(WXK_NUMPAD_ADD
);
580 case GDK_KP_Separator
:
581 // FIXME: what is this?
582 key_code
= isChar ?
'.' : int(WXK_NUMPAD_SEPARATOR
);
585 case GDK_KP_Subtract
:
586 key_code
= isChar ?
'-' : int(WXK_NUMPAD_SUBTRACT
);
590 key_code
= isChar ?
'.' : int(WXK_NUMPAD_DECIMAL
);
594 key_code
= isChar ?
'/' : int(WXK_NUMPAD_DIVIDE
);
611 key_code
= WXK_F1
+ keysym
- GDK_F1
;
621 static inline bool wxIsAsciiKeysym(KeySym ks
)
626 static void wxFillOtherKeyEventFields(wxKeyEvent
& event
,
628 GdkEventKey
*gdk_event
)
630 event
.SetTimestamp( gdk_event
->time
);
631 event
.SetId(win
->GetId());
633 event
.m_shiftDown
= (gdk_event
->state
& GDK_SHIFT_MASK
) != 0;
634 event
.m_controlDown
= (gdk_event
->state
& GDK_CONTROL_MASK
) != 0;
635 event
.m_altDown
= (gdk_event
->state
& GDK_MOD1_MASK
) != 0;
636 event
.m_metaDown
= (gdk_event
->state
& GDK_META_MASK
) != 0;
638 // Normally we take the state of modifiers directly from the low level GDK
639 // event but unfortunately GDK uses a different convention from MSW for the
640 // key events corresponding to the modifier keys themselves: in it, when
641 // e.g. Shift key is pressed, GDK_SHIFT_MASK is not set while it is set
642 // when Shift is released. Under MSW the situation is exactly reversed and
643 // the modifier corresponding to the key is set when it is pressed and
644 // unset when it is released. To ensure consistent behaviour between
645 // platforms (and because it seems to make slightly more sense, although
646 // arguably both behaviours are reasonable) we follow MSW here.
648 // Final notice: we set the flags to the desired value instead of just
649 // inverting them because they are not set correctly (i.e. in the same way
650 // as for the real events generated by the user) for wxUIActionSimulator-
651 // produced events and it seems better to keep that class code the same
652 // among all platforms and fix the discrepancy here instead of adding
653 // wxGTK-specific code to wxUIActionSimulator.
654 const bool isPress
= gdk_event
->type
== GDK_KEY_PRESS
;
655 switch ( gdk_event
->keyval
)
659 event
.m_shiftDown
= isPress
;
664 event
.m_controlDown
= isPress
;
669 event
.m_altDown
= isPress
;
676 event
.m_metaDown
= isPress
;
680 event
.m_rawCode
= (wxUint32
) gdk_event
->keyval
;
681 event
.m_rawFlags
= gdk_event
->hardware_keycode
;
683 wxGetMousePosition(&event
.m_x
, &event
.m_y
);
684 win
->ScreenToClient(&event
.m_x
, &event
.m_y
);
685 event
.SetEventObject( win
);
690 wxTranslateGTKKeyEventToWx(wxKeyEvent
& event
,
692 GdkEventKey
*gdk_event
)
694 // VZ: it seems that GDK_KEY_RELEASE event doesn't set event->string
695 // but only event->keyval which is quite useless to us, so remember
696 // the last character from GDK_KEY_PRESS and reuse it as last resort
698 // NB: should be MT-safe as we're always called from the main thread only
703 } s_lastKeyPress
= { 0, 0 };
705 KeySym keysym
= gdk_event
->keyval
;
707 wxLogTrace(TRACE_KEYS
, wxT("Key %s event: keysym = %ld"),
708 event
.GetEventType() == wxEVT_KEY_UP ?
wxT("release")
712 long key_code
= wxTranslateKeySymToWXKey(keysym
, false /* !isChar */);
716 // do we have the translation or is it a plain ASCII character?
717 if ( (gdk_event
->length
== 1) || wxIsAsciiKeysym(keysym
) )
719 // we should use keysym if it is ASCII as X does some translations
720 // like "I pressed while Control is down" => "Ctrl-I" == "TAB"
721 // which we don't want here (but which we do use for OnChar())
722 if ( !wxIsAsciiKeysym(keysym
) )
724 keysym
= (KeySym
)gdk_event
->string
[0];
727 // we want to always get the same key code when the same key is
728 // pressed regardless of the state of the modifiers, i.e. on a
729 // standard US keyboard pressing '5' or '%' ('5' key with
730 // Shift) should result in the same key code in OnKeyDown():
731 // '5' (although OnChar() will get either '5' or '%').
733 // to do it we first translate keysym to keycode (== scan code)
734 // and then back but always using the lower register
735 Display
*dpy
= (Display
*)wxGetDisplay();
736 KeyCode keycode
= XKeysymToKeycode(dpy
, keysym
);
738 wxLogTrace(TRACE_KEYS
, wxT("\t-> keycode %d"), keycode
);
740 KeySym keysymNormalized
= XKeycodeToKeysym(dpy
, keycode
, 0);
742 // use the normalized, i.e. lower register, keysym if we've
744 key_code
= keysymNormalized ? keysymNormalized
: keysym
;
746 // as explained above, we want to have lower register key codes
747 // normally but for the letter keys we want to have the upper ones
749 // NB: don't use XConvertCase() here, we want to do it for letters
751 key_code
= toupper(key_code
);
753 else // non ASCII key, what to do?
755 // by default, ignore it
758 // but if we have cached information from the last KEY_PRESS
759 if ( gdk_event
->type
== GDK_KEY_RELEASE
)
762 if ( keysym
== s_lastKeyPress
.keysym
)
764 key_code
= s_lastKeyPress
.keycode
;
769 if ( gdk_event
->type
== GDK_KEY_PRESS
)
771 // remember it to be reused for KEY_UP event later
772 s_lastKeyPress
.keysym
= keysym
;
773 s_lastKeyPress
.keycode
= key_code
;
777 wxLogTrace(TRACE_KEYS
, wxT("\t-> wxKeyCode %ld"), key_code
);
779 // sending unknown key events doesn't really make sense
783 event
.m_keyCode
= key_code
;
786 event
.m_uniChar
= gdk_keyval_to_unicode(key_code ? key_code
: keysym
);
787 if ( !event
.m_uniChar
&& event
.m_keyCode
<= WXK_DELETE
)
789 // Set Unicode key code to the ASCII equivalent for compatibility. E.g.
790 // let RETURN generate the key event with both key and Unicode key
792 event
.m_uniChar
= event
.m_keyCode
;
794 #endif // wxUSE_UNICODE
796 // now fill all the other fields
797 wxFillOtherKeyEventFields(event
, win
, gdk_event
);
805 GtkIMContext
*context
;
806 GdkEventKey
*lastKeyEvent
;
810 context
= gtk_im_multicontext_new();
815 g_object_unref (context
);
822 // Send wxEVT_CHAR_HOOK event to the parent of the window and return true only
823 // if it was processed (and not skipped).
824 bool SendCharHookEvent(const wxKeyEvent
& event
, wxWindow
*win
)
826 // wxEVT_CHAR_HOOK must be sent to allow the parent windows (e.g. a dialog
827 // which typically closes when Esc key is pressed in any of its controls)
828 // to handle key events in all of its children unless the mouse is captured
829 // in which case we consider that the keyboard should be "captured" too.
830 if ( !g_captureWindow
)
832 wxKeyEvent
eventCharHook(wxEVT_CHAR_HOOK
, event
);
833 if ( win
->HandleWindowEvent(eventCharHook
) )
840 // Adjust wxEVT_CHAR event key code fields. This function takes care of two
842 // (a) Ctrl-letter key presses generate key codes in range 1..26
843 // (b) Unicode key codes are same as key codes for the codes in 1..255 range
844 void AdjustCharEventKeyCodes(wxKeyEvent
& event
)
846 const int code
= event
.m_keyCode
;
848 // Check for (a) above.
849 if ( event
.ControlDown() )
851 // We intentionally don't use isupper/lower() here, we really need
852 // ASCII letters only as it doesn't make sense to translate any other
853 // ones into this range which has only 26 slots.
854 if ( code
>= 'a' && code
<= 'z' )
855 event
.m_keyCode
= code
- 'a' + 1;
856 else if ( code
>= 'A' && code
<= 'Z' )
857 event
.m_keyCode
= code
- 'A' + 1;
860 // Adjust the Unicode equivalent in the same way too.
861 if ( event
.m_keyCode
!= code
)
862 event
.m_uniChar
= event
.m_keyCode
;
863 #endif // wxUSE_UNICODE
867 // Check for (b) from above.
869 // FIXME: Should we do it for key codes up to 255?
870 if ( !event
.m_uniChar
&& code
< WXK_DELETE
)
871 event
.m_uniChar
= code
;
872 #endif // wxUSE_UNICODE
875 } // anonymous namespace
879 gtk_window_key_press_callback( GtkWidget
*WXUNUSED(widget
),
880 GdkEventKey
*gdk_event
,
885 if (g_blockEventsOnDrag
)
888 wxKeyEvent
event( wxEVT_KEY_DOWN
);
890 bool return_after_IM
= false;
892 if( wxTranslateGTKKeyEventToWx(event
, win
, gdk_event
) )
894 // Send the CHAR_HOOK event first
895 if ( SendCharHookEvent(event
, win
) )
897 // Don't do anything at all with this event any more.
901 // Emit KEY_DOWN event
902 ret
= win
->HandleWindowEvent( event
);
906 // Return after IM processing as we cannot do
907 // anything with it anyhow.
908 return_after_IM
= true;
911 if (!ret
&& win
->m_imData
)
913 win
->m_imData
->lastKeyEvent
= gdk_event
;
915 // We should let GTK+ IM filter key event first. According to GTK+ 2.0 API
916 // docs, if IM filter returns true, no further processing should be done.
917 // we should send the key_down event anyway.
918 bool intercepted_by_IM
= gtk_im_context_filter_keypress(win
->m_imData
->context
, gdk_event
);
919 win
->m_imData
->lastKeyEvent
= NULL
;
920 if (intercepted_by_IM
)
922 wxLogTrace(TRACE_KEYS
, wxT("Key event intercepted by IM"));
933 wxWindowGTK
*ancestor
= win
;
936 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
939 wxCommandEvent
menu_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
940 ret
= ancestor
->HandleWindowEvent( menu_event
);
944 // if the accelerator wasn't handled as menu event, try
945 // it as button click (for compatibility with other
947 wxCommandEvent
button_event( wxEVT_COMMAND_BUTTON_CLICKED
, command
);
948 ret
= ancestor
->HandleWindowEvent( button_event
);
953 if (ancestor
->IsTopLevel())
955 ancestor
= ancestor
->GetParent();
958 #endif // wxUSE_ACCEL
960 // Only send wxEVT_CHAR event if not processed yet. Thus, ALT-x
961 // will only be sent if it is not in an accelerator table.
965 KeySym keysym
= gdk_event
->keyval
;
966 // Find key code for EVT_CHAR and EVT_CHAR_HOOK events
967 key_code
= wxTranslateKeySymToWXKey(keysym
, true /* isChar */);
970 if ( wxIsAsciiKeysym(keysym
) )
973 key_code
= (unsigned char)keysym
;
975 // gdk_event->string is actually deprecated
976 else if ( gdk_event
->length
== 1 )
978 key_code
= (unsigned char)gdk_event
->string
[0];
984 wxKeyEvent
eventChar(wxEVT_CHAR
, event
);
986 wxLogTrace(TRACE_KEYS
, wxT("Char event: %ld"), key_code
);
988 eventChar
.m_keyCode
= key_code
;
990 AdjustCharEventKeyCodes(eventChar
);
992 ret
= win
->HandleWindowEvent(eventChar
);
1002 gtk_wxwindow_commit_cb (GtkIMContext
* WXUNUSED(context
),
1006 wxKeyEvent
event( wxEVT_CHAR
);
1008 // take modifiers, cursor position, timestamp etc. from the last
1009 // key_press_event that was fed into Input Method:
1010 if (window
->m_imData
->lastKeyEvent
)
1012 wxFillOtherKeyEventFields(event
,
1013 window
, window
->m_imData
->lastKeyEvent
);
1017 event
.SetEventObject( window
);
1020 const wxString
data(wxGTK_CONV_BACK_SYS(str
));
1024 for( wxString
::const_iterator pstr
= data
.begin(); pstr
!= data
.end(); ++pstr
)
1027 event
.m_uniChar
= *pstr
;
1028 // Backward compatible for ISO-8859-1
1029 event
.m_keyCode
= *pstr
< 256 ? event
.m_uniChar
: 0;
1030 wxLogTrace(TRACE_KEYS
, wxT("IM sent character '%c'"), event
.m_uniChar
);
1032 event
.m_keyCode
= (char)*pstr
;
1033 #endif // wxUSE_UNICODE
1035 AdjustCharEventKeyCodes(event
);
1037 window
->HandleWindowEvent(event
);
1043 //-----------------------------------------------------------------------------
1044 // "key_release_event" from any window
1045 //-----------------------------------------------------------------------------
1049 gtk_window_key_release_callback( GtkWidget
* WXUNUSED(widget
),
1050 GdkEventKey
*gdk_event
,
1056 if (g_blockEventsOnDrag
)
1059 wxKeyEvent
event( wxEVT_KEY_UP
);
1060 if ( !wxTranslateGTKKeyEventToWx(event
, win
, gdk_event
) )
1062 // unknown key pressed, ignore (the event would be useless anyhow)
1066 return win
->GTKProcessEvent(event
);
1070 // ============================================================================
1072 // ============================================================================
1074 // ----------------------------------------------------------------------------
1075 // mouse event processing helpers
1076 // ----------------------------------------------------------------------------
1078 static void AdjustEventButtonState(wxMouseEvent
& event
)
1080 // GDK reports the old state of the button for a button press event, but
1081 // for compatibility with MSW and common sense we want m_leftDown be TRUE
1082 // for a LEFT_DOWN event, not FALSE, so we will invert
1083 // left/right/middleDown for the corresponding click events
1085 if ((event
.GetEventType() == wxEVT_LEFT_DOWN
) ||
1086 (event
.GetEventType() == wxEVT_LEFT_DCLICK
) ||
1087 (event
.GetEventType() == wxEVT_LEFT_UP
))
1089 event
.m_leftDown
= !event
.m_leftDown
;
1093 if ((event
.GetEventType() == wxEVT_MIDDLE_DOWN
) ||
1094 (event
.GetEventType() == wxEVT_MIDDLE_DCLICK
) ||
1095 (event
.GetEventType() == wxEVT_MIDDLE_UP
))
1097 event
.m_middleDown
= !event
.m_middleDown
;
1101 if ((event
.GetEventType() == wxEVT_RIGHT_DOWN
) ||
1102 (event
.GetEventType() == wxEVT_RIGHT_DCLICK
) ||
1103 (event
.GetEventType() == wxEVT_RIGHT_UP
))
1105 event
.m_rightDown
= !event
.m_rightDown
;
1109 if ((event
.GetEventType() == wxEVT_AUX1_DOWN
) ||
1110 (event
.GetEventType() == wxEVT_AUX1_DCLICK
))
1112 event
.m_aux1Down
= true;
1116 if ((event
.GetEventType() == wxEVT_AUX2_DOWN
) ||
1117 (event
.GetEventType() == wxEVT_AUX2_DCLICK
))
1119 event
.m_aux2Down
= true;
1124 // find the window to send the mouse event too
1126 wxWindowGTK
*FindWindowForMouseEvent(wxWindowGTK
*win
, wxCoord
& x
, wxCoord
& y
)
1131 if (win
->m_wxwindow
)
1133 wxPizza
* pizza
= WX_PIZZA(win
->m_wxwindow
);
1134 xx
+= pizza
->m_scroll_x
;
1135 yy
+= pizza
->m_scroll_y
;
1138 wxWindowList
::compatibility_iterator node
= win
->GetChildren().GetFirst();
1141 wxWindowGTK
*child
= node
->GetData();
1143 node
= node
->GetNext();
1144 if (!child
->IsShown())
1147 if (child
->GTKIsTransparentForMouse())
1149 // wxStaticBox is transparent in the box itself
1150 int xx1
= child
->m_x
;
1151 int yy1
= child
->m_y
;
1152 int xx2
= child
->m_x
+ child
->m_width
;
1153 int yy2
= child
->m_y
+ child
->m_height
;
1156 if (((xx
>= xx1
) && (xx
<= xx1
+10) && (yy
>= yy1
) && (yy
<= yy2
)) ||
1158 ((xx
>= xx2
-10) && (xx
<= xx2
) && (yy
>= yy1
) && (yy
<= yy2
)) ||
1160 ((xx
>= xx1
) && (xx
<= xx2
) && (yy
>= yy1
) && (yy
<= yy1
+10)) ||
1162 ((xx
>= xx1
) && (xx
<= xx2
) && (yy
>= yy2
-1) && (yy
<= yy2
)))
1173 if ((child
->m_wxwindow
== NULL
) &&
1174 (child
->m_x
<= xx
) &&
1175 (child
->m_y
<= yy
) &&
1176 (child
->m_x
+child
->m_width
>= xx
) &&
1177 (child
->m_y
+child
->m_height
>= yy
))
1190 // ----------------------------------------------------------------------------
1191 // common event handlers helpers
1192 // ----------------------------------------------------------------------------
1194 bool wxWindowGTK
::GTKProcessEvent(wxEvent
& event
) const
1196 // nothing special at this level
1197 return HandleWindowEvent(event
);
1200 bool wxWindowGTK
::GTKShouldIgnoreEvent() const
1202 return !m_hasVMT
|| g_blockEventsOnDrag
;
1205 int wxWindowGTK
::GTKCallbackCommonPrologue(GdkEventAny
*event
) const
1209 if (g_blockEventsOnDrag
)
1211 if (g_blockEventsOnScroll
)
1214 if (!GTKIsOwnWindow(event
->window
))
1220 // overloads for all GDK event types we use here: we need to have this as
1221 // GdkEventXXX can't be implicitly cast to GdkEventAny even if it, in fact,
1222 // derives from it in the sense that the structs have the same layout
1223 #define wxDEFINE_COMMON_PROLOGUE_OVERLOAD(T) \
1224 static int wxGtkCallbackCommonPrologue(T *event, wxWindowGTK *win) \
1226 return win->GTKCallbackCommonPrologue((GdkEventAny *)event); \
1229 wxDEFINE_COMMON_PROLOGUE_OVERLOAD(GdkEventButton
)
1230 wxDEFINE_COMMON_PROLOGUE_OVERLOAD(GdkEventMotion
)
1231 wxDEFINE_COMMON_PROLOGUE_OVERLOAD(GdkEventCrossing
)
1233 #undef wxDEFINE_COMMON_PROLOGUE_OVERLOAD
1235 #define wxCOMMON_CALLBACK_PROLOGUE(event, win) \
1236 const int rc = wxGtkCallbackCommonPrologue(event, win); \
1240 // all event handlers must have C linkage as they're called from GTK+ C code
1244 //-----------------------------------------------------------------------------
1245 // "button_press_event"
1246 //-----------------------------------------------------------------------------
1249 gtk_window_button_press_callback( GtkWidget
*widget
,
1250 GdkEventButton
*gdk_event
,
1253 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1255 g_lastButtonNumber
= gdk_event
->button
;
1257 // GDK sends surplus button down events
1258 // before a double click event. We
1259 // need to filter these out.
1260 if ((gdk_event
->type
== GDK_BUTTON_PRESS
) && (win
->m_wxwindow
))
1262 GdkEvent
*peek_event
= gdk_event_peek();
1265 if ((peek_event
->type
== GDK_2BUTTON_PRESS
) ||
1266 (peek_event
->type
== GDK_3BUTTON_PRESS
))
1268 gdk_event_free( peek_event
);
1273 gdk_event_free( peek_event
);
1278 wxEventType event_type
= wxEVT_NULL
;
1280 if ( gdk_event
->type
== GDK_2BUTTON_PRESS
&&
1281 gdk_event
->button
>= 1 && gdk_event
->button
<= 3 )
1283 // Reset GDK internal timestamp variables in order to disable GDK
1284 // triple click events. GDK will then next time believe no button has
1285 // been clicked just before, and send a normal button click event.
1286 GdkDisplay
* display
= gtk_widget_get_display (widget
);
1287 display
->button_click_time
[1] = 0;
1288 display
->button_click_time
[0] = 0;
1291 if (gdk_event
->button
== 1)
1293 // note that GDK generates triple click events which are not supported
1294 // by wxWidgets but still have to be passed to the app as otherwise
1295 // clicks would simply go missing
1296 switch (gdk_event
->type
)
1298 // we shouldn't get triple clicks at all for GTK2 because we
1299 // suppress them artificially using the code above but we still
1300 // should map them to something for GTK1 and not just ignore them
1301 // as this would lose clicks
1302 case GDK_3BUTTON_PRESS
: // we could also map this to DCLICK...
1303 case GDK_BUTTON_PRESS
:
1304 event_type
= wxEVT_LEFT_DOWN
;
1307 case GDK_2BUTTON_PRESS
:
1308 event_type
= wxEVT_LEFT_DCLICK
;
1312 // just to silence gcc warnings
1316 else if (gdk_event
->button
== 2)
1318 switch (gdk_event
->type
)
1320 case GDK_3BUTTON_PRESS
:
1321 case GDK_BUTTON_PRESS
:
1322 event_type
= wxEVT_MIDDLE_DOWN
;
1325 case GDK_2BUTTON_PRESS
:
1326 event_type
= wxEVT_MIDDLE_DCLICK
;
1333 else if (gdk_event
->button
== 3)
1335 switch (gdk_event
->type
)
1337 case GDK_3BUTTON_PRESS
:
1338 case GDK_BUTTON_PRESS
:
1339 event_type
= wxEVT_RIGHT_DOWN
;
1342 case GDK_2BUTTON_PRESS
:
1343 event_type
= wxEVT_RIGHT_DCLICK
;
1351 else if (gdk_event
->button
== 8)
1353 switch (gdk_event
->type
)
1355 case GDK_3BUTTON_PRESS
:
1356 case GDK_BUTTON_PRESS
:
1357 event_type
= wxEVT_AUX1_DOWN
;
1360 case GDK_2BUTTON_PRESS
:
1361 event_type
= wxEVT_AUX1_DCLICK
;
1369 else if (gdk_event
->button
== 9)
1371 switch (gdk_event
->type
)
1373 case GDK_3BUTTON_PRESS
:
1374 case GDK_BUTTON_PRESS
:
1375 event_type
= wxEVT_AUX2_DOWN
;
1378 case GDK_2BUTTON_PRESS
:
1379 event_type
= wxEVT_AUX2_DCLICK
;
1387 if ( event_type
== wxEVT_NULL
)
1389 // unknown mouse button or click type
1393 g_lastMouseEvent
= (GdkEvent
*) gdk_event
;
1395 wxMouseEvent
event( event_type
);
1396 InitMouseEvent( win
, event
, gdk_event
);
1398 AdjustEventButtonState(event
);
1400 // find the correct window to send the event to: it may be a different one
1401 // from the one which got it at GTK+ level because some controls don't have
1402 // their own X window and thus cannot get any events.
1403 if ( !g_captureWindow
)
1404 win
= FindWindowForMouseEvent(win
, event
.m_x
, event
.m_y
);
1406 // reset the event object and id in case win changed.
1407 event
.SetEventObject( win
);
1408 event
.SetId( win
->GetId() );
1410 bool ret
= win
->GTKProcessEvent( event
);
1411 g_lastMouseEvent
= NULL
;
1415 if ((event_type
== wxEVT_LEFT_DOWN
) && !win
->IsOfStandardClass() &&
1416 (gs_currentFocus
!= win
) /* && win->IsFocusable() */)
1421 if (event_type
== wxEVT_RIGHT_DOWN
)
1423 // generate a "context menu" event: this is similar to right mouse
1424 // click under many GUIs except that it is generated differently
1425 // (right up under MSW, ctrl-click under Mac, right down here) and
1427 // (a) it's a command event and so is propagated to the parent
1428 // (b) under some ports it can be generated from kbd too
1429 // (c) it uses screen coords (because of (a))
1430 wxContextMenuEvent
evtCtx(
1433 win
->ClientToScreen(event
.GetPosition()));
1434 evtCtx
.SetEventObject(win
);
1435 return win
->GTKProcessEvent(evtCtx
);
1441 //-----------------------------------------------------------------------------
1442 // "button_release_event"
1443 //-----------------------------------------------------------------------------
1446 gtk_window_button_release_callback( GtkWidget
*WXUNUSED(widget
),
1447 GdkEventButton
*gdk_event
,
1450 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1452 g_lastButtonNumber
= 0;
1454 wxEventType event_type
= wxEVT_NULL
;
1456 switch (gdk_event
->button
)
1459 event_type
= wxEVT_LEFT_UP
;
1463 event_type
= wxEVT_MIDDLE_UP
;
1467 event_type
= wxEVT_RIGHT_UP
;
1471 event_type
= wxEVT_AUX1_UP
;
1475 event_type
= wxEVT_AUX2_UP
;
1479 // unknown button, don't process
1483 g_lastMouseEvent
= (GdkEvent
*) gdk_event
;
1485 wxMouseEvent
event( event_type
);
1486 InitMouseEvent( win
, event
, gdk_event
);
1488 AdjustEventButtonState(event
);
1490 if ( !g_captureWindow
)
1491 win
= FindWindowForMouseEvent(win
, event
.m_x
, event
.m_y
);
1493 // reset the event object and id in case win changed.
1494 event
.SetEventObject( win
);
1495 event
.SetId( win
->GetId() );
1497 bool ret
= win
->GTKProcessEvent(event
);
1499 g_lastMouseEvent
= NULL
;
1504 //-----------------------------------------------------------------------------
1505 // "motion_notify_event"
1506 //-----------------------------------------------------------------------------
1509 gtk_window_motion_notify_callback( GtkWidget
* WXUNUSED(widget
),
1510 GdkEventMotion
*gdk_event
,
1513 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1515 if (gdk_event
->is_hint
)
1519 GdkModifierType state
;
1520 gdk_window_get_pointer(gdk_event
->window
, &x
, &y
, &state
);
1525 g_lastMouseEvent
= (GdkEvent
*) gdk_event
;
1527 wxMouseEvent
event( wxEVT_MOTION
);
1528 InitMouseEvent(win
, event
, gdk_event
);
1530 if ( g_captureWindow
)
1532 // synthesise a mouse enter or leave event if needed
1533 GdkWindow
*winUnderMouse
= gdk_window_at_pointer(NULL
, NULL
);
1534 // This seems to be necessary and actually been added to
1535 // GDK itself in version 2.0.X
1538 bool hasMouse
= winUnderMouse
== gdk_event
->window
;
1539 if ( hasMouse
!= g_captureWindowHasMouse
)
1541 // the mouse changed window
1542 g_captureWindowHasMouse
= hasMouse
;
1544 wxMouseEvent
eventM(g_captureWindowHasMouse ? wxEVT_ENTER_WINDOW
1545 : wxEVT_LEAVE_WINDOW
);
1546 InitMouseEvent(win
, eventM
, gdk_event
);
1547 eventM
.SetEventObject(win
);
1548 win
->GTKProcessEvent(eventM
);
1553 win
= FindWindowForMouseEvent(win
, event
.m_x
, event
.m_y
);
1555 // reset the event object and id in case win changed.
1556 event
.SetEventObject( win
);
1557 event
.SetId( win
->GetId() );
1560 if ( !g_captureWindow
)
1562 wxSetCursorEvent
cevent( event
.m_x
, event
.m_y
);
1563 if (win
->GTKProcessEvent( cevent
))
1565 win
->SetCursor( cevent
.GetCursor() );
1569 bool ret
= win
->GTKProcessEvent(event
);
1571 g_lastMouseEvent
= NULL
;
1576 //-----------------------------------------------------------------------------
1577 // "scroll_event" (mouse wheel event)
1578 //-----------------------------------------------------------------------------
1581 window_scroll_event_hscrollbar(GtkWidget
*, GdkEventScroll
* gdk_event
, wxWindow
* win
)
1583 if (gdk_event
->direction
!= GDK_SCROLL_LEFT
&&
1584 gdk_event
->direction
!= GDK_SCROLL_RIGHT
)
1589 GtkRange
*range
= win
->m_scrollBar
[wxWindow
::ScrollDir_Horz
];
1591 if (range
&& gtk_widget_get_visible(GTK_WIDGET(range
)))
1593 GtkAdjustment
* adj
= gtk_range_get_adjustment(range
);
1594 double delta
= gtk_adjustment_get_step_increment(adj
) * 3;
1595 if (gdk_event
->direction
== GDK_SCROLL_LEFT
)
1598 gtk_range_set_value(range
, gtk_adjustment_get_value(adj
) + delta
);
1607 window_scroll_event(GtkWidget
*, GdkEventScroll
* gdk_event
, wxWindow
* win
)
1609 if (gdk_event
->direction
!= GDK_SCROLL_UP
&&
1610 gdk_event
->direction
!= GDK_SCROLL_DOWN
)
1615 wxMouseEvent
event(wxEVT_MOUSEWHEEL
);
1616 InitMouseEvent(win
, event
, gdk_event
);
1618 // FIXME: Get these values from GTK or GDK
1619 event
.m_linesPerAction
= 3;
1620 event
.m_wheelDelta
= 120;
1621 if (gdk_event
->direction
== GDK_SCROLL_UP
)
1622 event
.m_wheelRotation
= 120;
1624 event
.m_wheelRotation
= -120;
1626 if (win
->GTKProcessEvent(event
))
1629 GtkRange
*range
= win
->m_scrollBar
[wxWindow
::ScrollDir_Vert
];
1631 if (range
&& gtk_widget_get_visible(GTK_WIDGET(range
)))
1633 GtkAdjustment
* adj
= gtk_range_get_adjustment(range
);
1634 double delta
= gtk_adjustment_get_step_increment(adj
) * 3;
1635 if (gdk_event
->direction
== GDK_SCROLL_UP
)
1638 gtk_range_set_value(range
, gtk_adjustment_get_value(adj
) + delta
);
1646 //-----------------------------------------------------------------------------
1648 //-----------------------------------------------------------------------------
1650 static gboolean
wxgtk_window_popup_menu_callback(GtkWidget
*, wxWindowGTK
* win
)
1652 wxContextMenuEvent
event(wxEVT_CONTEXT_MENU
, win
->GetId(), wxPoint(-1, -1));
1653 event
.SetEventObject(win
);
1654 return win
->GTKProcessEvent(event
);
1657 //-----------------------------------------------------------------------------
1659 //-----------------------------------------------------------------------------
1662 gtk_window_focus_in_callback( GtkWidget
* WXUNUSED(widget
),
1663 GdkEventFocus
*WXUNUSED(event
),
1666 return win
->GTKHandleFocusIn();
1669 //-----------------------------------------------------------------------------
1670 // "focus_out_event"
1671 //-----------------------------------------------------------------------------
1674 gtk_window_focus_out_callback( GtkWidget
* WXUNUSED(widget
),
1675 GdkEventFocus
* WXUNUSED(gdk_event
),
1678 return win
->GTKHandleFocusOut();
1681 //-----------------------------------------------------------------------------
1683 //-----------------------------------------------------------------------------
1686 wx_window_focus_callback(GtkWidget
*widget
,
1687 GtkDirectionType
WXUNUSED(direction
),
1690 // the default handler for focus signal in GtkScrolledWindow sets
1691 // focus to the window itself even if it doesn't accept focus, i.e. has no
1692 // GTK_CAN_FOCUS in its style -- work around this by forcibly preventing
1693 // the signal from reaching gtk_scrolled_window_focus() if we don't have
1694 // any children which might accept focus (we know we don't accept the focus
1695 // ourselves as this signal is only connected in this case)
1696 if ( win
->GetChildren().empty() )
1697 g_signal_stop_emission_by_name(widget
, "focus");
1699 // we didn't change the focus
1703 //-----------------------------------------------------------------------------
1704 // "enter_notify_event"
1705 //-----------------------------------------------------------------------------
1708 gtk_window_enter_callback( GtkWidget
*widget
,
1709 GdkEventCrossing
*gdk_event
,
1712 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1714 // Event was emitted after a grab
1715 if (gdk_event
->mode
!= GDK_CROSSING_NORMAL
) return FALSE
;
1719 GdkModifierType state
= (GdkModifierType
)0;
1721 gdk_window_get_pointer(gtk_widget_get_window(widget
), &x
, &y
, &state
);
1723 wxMouseEvent
event( wxEVT_ENTER_WINDOW
);
1724 InitMouseEvent(win
, event
, gdk_event
);
1725 wxPoint pt
= win
->GetClientAreaOrigin();
1726 event
.m_x
= x
+ pt
.x
;
1727 event
.m_y
= y
+ pt
.y
;
1729 if ( !g_captureWindow
)
1731 wxSetCursorEvent
cevent( event
.m_x
, event
.m_y
);
1732 if (win
->GTKProcessEvent( cevent
))
1734 win
->SetCursor( cevent
.GetCursor() );
1738 return win
->GTKProcessEvent(event
);
1741 //-----------------------------------------------------------------------------
1742 // "leave_notify_event"
1743 //-----------------------------------------------------------------------------
1746 gtk_window_leave_callback( GtkWidget
*widget
,
1747 GdkEventCrossing
*gdk_event
,
1750 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1752 // Event was emitted after an ungrab
1753 if (gdk_event
->mode
!= GDK_CROSSING_NORMAL
) return FALSE
;
1755 wxMouseEvent
event( wxEVT_LEAVE_WINDOW
);
1759 GdkModifierType state
= (GdkModifierType
)0;
1761 gdk_window_get_pointer(gtk_widget_get_window(widget
), &x
, &y
, &state
);
1763 InitMouseEvent(win
, event
, gdk_event
);
1765 return win
->GTKProcessEvent(event
);
1768 //-----------------------------------------------------------------------------
1769 // "value_changed" from scrollbar
1770 //-----------------------------------------------------------------------------
1773 gtk_scrollbar_value_changed(GtkRange
* range
, wxWindow
* win
)
1775 wxEventType eventType
= win
->GTKGetScrollEventType(range
);
1776 if (eventType
!= wxEVT_NULL
)
1778 // Convert scroll event type to scrollwin event type
1779 eventType
+= wxEVT_SCROLLWIN_TOP
- wxEVT_SCROLL_TOP
;
1781 // find the scrollbar which generated the event
1782 wxWindowGTK
::ScrollDir dir
= win
->ScrollDirFromRange(range
);
1784 // generate the corresponding wx event
1785 const int orient
= wxWindow
::OrientFromScrollDir(dir
);
1786 wxScrollWinEvent
event(eventType
, win
->GetScrollPos(orient
), orient
);
1787 event
.SetEventObject(win
);
1789 win
->GTKProcessEvent(event
);
1793 //-----------------------------------------------------------------------------
1794 // "button_press_event" from scrollbar
1795 //-----------------------------------------------------------------------------
1798 gtk_scrollbar_button_press_event(GtkRange
*, GdkEventButton
*, wxWindow
* win
)
1800 g_blockEventsOnScroll
= true;
1801 win
->m_mouseButtonDown
= true;
1806 //-----------------------------------------------------------------------------
1807 // "event_after" from scrollbar
1808 //-----------------------------------------------------------------------------
1811 gtk_scrollbar_event_after(GtkRange
* range
, GdkEvent
* event
, wxWindow
* win
)
1813 if (event
->type
== GDK_BUTTON_RELEASE
)
1815 g_signal_handlers_block_by_func(range
, (void*)gtk_scrollbar_event_after
, win
);
1817 const int orient
= wxWindow
::OrientFromScrollDir(
1818 win
->ScrollDirFromRange(range
));
1819 wxScrollWinEvent
evt(wxEVT_SCROLLWIN_THUMBRELEASE
,
1820 win
->GetScrollPos(orient
), orient
);
1821 evt
.SetEventObject(win
);
1822 win
->GTKProcessEvent(evt
);
1826 //-----------------------------------------------------------------------------
1827 // "button_release_event" from scrollbar
1828 //-----------------------------------------------------------------------------
1831 gtk_scrollbar_button_release_event(GtkRange
* range
, GdkEventButton
*, wxWindow
* win
)
1833 g_blockEventsOnScroll
= false;
1834 win
->m_mouseButtonDown
= false;
1835 // If thumb tracking
1836 if (win
->m_isScrolling
)
1838 win
->m_isScrolling
= false;
1839 // Hook up handler to send thumb release event after this emission is finished.
1840 // To allow setting scroll position from event handler, sending event must
1841 // be deferred until after the GtkRange handler for this signal has run
1842 g_signal_handlers_unblock_by_func(range
, (void*)gtk_scrollbar_event_after
, win
);
1848 //-----------------------------------------------------------------------------
1849 // "realize" from m_widget
1850 //-----------------------------------------------------------------------------
1853 gtk_window_realized_callback(GtkWidget
* WXUNUSED(widget
), wxWindowGTK
* win
)
1855 win
->GTKHandleRealized();
1858 //-----------------------------------------------------------------------------
1859 // "unrealize" from m_wxwindow
1860 //-----------------------------------------------------------------------------
1862 static void unrealize(GtkWidget
*, wxWindowGTK
* win
)
1865 gtk_im_context_set_client_window(win
->m_imData
->context
, NULL
);
1868 //-----------------------------------------------------------------------------
1869 // "size_allocate" from m_wxwindow or m_widget
1870 //-----------------------------------------------------------------------------
1873 size_allocate(GtkWidget
*, GtkAllocation
* alloc
, wxWindow
* win
)
1875 int w
= alloc
->width
;
1876 int h
= alloc
->height
;
1877 if (win
->m_wxwindow
)
1879 int border_x
, border_y
;
1880 WX_PIZZA(win
->m_wxwindow
)->get_border_widths(border_x
, border_y
);
1886 if (win
->m_oldClientWidth
!= w
|| win
->m_oldClientHeight
!= h
)
1888 win
->m_oldClientWidth
= w
;
1889 win
->m_oldClientHeight
= h
;
1890 // this callback can be connected to m_wxwindow,
1891 // so always get size from m_widget->allocation
1893 gtk_widget_get_allocation(win
->m_widget
, &a
);
1894 win
->m_width
= a
.width
;
1895 win
->m_height
= a
.height
;
1896 if (!win
->m_nativeSizeEvent
)
1898 wxSizeEvent
event(win
->GetSize(), win
->GetId());
1899 event
.SetEventObject(win
);
1900 win
->GTKProcessEvent(event
);
1905 //-----------------------------------------------------------------------------
1907 //-----------------------------------------------------------------------------
1909 #if GTK_CHECK_VERSION(2, 8, 0)
1911 gtk_window_grab_broken( GtkWidget
*,
1912 GdkEventGrabBroken
*event
,
1915 // Mouse capture has been lost involuntarily, notify the application
1916 if(!event
->keyboard
&& wxWindow
::GetCapture() == win
)
1918 wxMouseCaptureLostEvent
evt( win
->GetId() );
1919 evt
.SetEventObject( win
);
1920 win
->HandleWindowEvent( evt
);
1926 //-----------------------------------------------------------------------------
1928 //-----------------------------------------------------------------------------
1931 void gtk_window_style_set_callback( GtkWidget
*WXUNUSED(widget
),
1932 GtkStyle
*previous_style
,
1935 if (win
&& previous_style
)
1937 wxSysColourChangedEvent event
;
1938 event
.SetEventObject(win
);
1940 win
->GTKProcessEvent( event
);
1946 void wxWindowGTK
::GTKHandleRealized()
1950 gtk_im_context_set_client_window
1953 m_wxwindow ?
GTKGetDrawingWindow()
1954 : gtk_widget_get_window(m_widget
)
1958 // We cannot set colours and fonts before the widget
1959 // been realized, so we do this directly after realization
1960 // or otherwise in idle time
1962 if (m_needsStyleChange
)
1964 SetBackgroundStyle(GetBackgroundStyle());
1965 m_needsStyleChange
= false;
1968 wxWindowCreateEvent
event(static_cast<wxWindow
*>(this));
1969 event
.SetEventObject( this );
1970 GTKProcessEvent( event
);
1972 GTKUpdateCursor(true, false);
1975 // ----------------------------------------------------------------------------
1976 // this wxWindowBase function is implemented here (in platform-specific file)
1977 // because it is static and so couldn't be made virtual
1978 // ----------------------------------------------------------------------------
1980 wxWindow
*wxWindowBase
::DoFindFocus()
1982 wxWindowGTK
*focus
= gs_pendingFocus ? gs_pendingFocus
: gs_currentFocus
;
1983 // the cast is necessary when we compile in wxUniversal mode
1984 return static_cast<wxWindow
*>(focus
);
1987 void wxWindowGTK
::AddChildGTK(wxWindowGTK
* child
)
1989 wxASSERT_MSG(m_wxwindow
, "Cannot add a child to a window without a client area");
1991 // the window might have been scrolled already, we
1992 // have to adapt the position
1993 wxPizza
* pizza
= WX_PIZZA(m_wxwindow
);
1994 child
->m_x
+= pizza
->m_scroll_x
;
1995 child
->m_y
+= pizza
->m_scroll_y
;
1997 gtk_widget_set_size_request(
1998 child
->m_widget
, child
->m_width
, child
->m_height
);
1999 pizza
->put(child
->m_widget
, child
->m_x
, child
->m_y
);
2002 //-----------------------------------------------------------------------------
2004 //-----------------------------------------------------------------------------
2006 wxWindow
*wxGetActiveWindow()
2008 return wxWindow
::FindFocus();
2012 wxMouseState
wxGetMouseState()
2018 GdkModifierType mask
;
2020 gdk_window_get_pointer(NULL
, &x
, &y
, &mask
);
2024 ms
.SetLeftDown((mask
& GDK_BUTTON1_MASK
) != 0);
2025 ms
.SetMiddleDown((mask
& GDK_BUTTON2_MASK
) != 0);
2026 ms
.SetRightDown((mask
& GDK_BUTTON3_MASK
) != 0);
2027 // see the comment in InitMouseEvent()
2028 ms
.SetAux1Down((mask
& GDK_BUTTON4_MASK
) != 0);
2029 ms
.SetAux2Down((mask
& GDK_BUTTON5_MASK
) != 0);
2031 ms
.SetControlDown((mask
& GDK_CONTROL_MASK
) != 0);
2032 ms
.SetShiftDown((mask
& GDK_SHIFT_MASK
) != 0);
2033 ms
.SetAltDown((mask
& GDK_MOD1_MASK
) != 0);
2034 ms
.SetMetaDown((mask
& GDK_META_MASK
) != 0);
2039 //-----------------------------------------------------------------------------
2041 //-----------------------------------------------------------------------------
2043 // in wxUniv/MSW this class is abstract because it doesn't have DoPopupMenu()
2045 #ifdef __WXUNIVERSAL__
2046 IMPLEMENT_ABSTRACT_CLASS(wxWindowGTK
, wxWindowBase
)
2047 #endif // __WXUNIVERSAL__
2049 void wxWindowGTK
::Init()
2054 m_focusWidget
= NULL
;
2064 m_showOnIdle
= false;
2067 m_nativeSizeEvent
= false;
2069 m_isScrolling
= false;
2070 m_mouseButtonDown
= false;
2072 // initialize scrolling stuff
2073 for ( int dir
= 0; dir
< ScrollDir_Max
; dir
++ )
2075 m_scrollBar
[dir
] = NULL
;
2076 m_scrollPos
[dir
] = 0;
2080 m_oldClientHeight
= 0;
2082 m_clipPaintRegion
= false;
2084 m_needsStyleChange
= false;
2086 m_cursor
= *wxSTANDARD_CURSOR
;
2089 m_dirtyTabOrder
= false;
2092 wxWindowGTK
::wxWindowGTK()
2097 wxWindowGTK
::wxWindowGTK( wxWindow
*parent
,
2102 const wxString
&name
)
2106 Create( parent
, id
, pos
, size
, style
, name
);
2109 bool wxWindowGTK
::Create( wxWindow
*parent
,
2114 const wxString
&name
)
2116 // Get default border
2117 wxBorder border
= GetBorder(style
);
2119 style
&= ~wxBORDER_MASK
;
2122 if (!PreCreation( parent
, pos
, size
) ||
2123 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
2125 wxFAIL_MSG( wxT("wxWindowGTK creation failed") );
2129 // We should accept the native look
2131 GtkScrolledWindowClass
*scroll_class
= GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) );
2132 scroll_class
->scrollbar_spacing
= 0;
2136 m_wxwindow
= wxPizza
::New(m_windowStyle
);
2137 #ifndef __WXUNIVERSAL__
2138 if (HasFlag(wxPizza
::BORDER_STYLES
))
2140 g_signal_connect(m_wxwindow
, "parent_set",
2141 G_CALLBACK(parent_set
), this);
2144 if (!HasFlag(wxHSCROLL
) && !HasFlag(wxVSCROLL
))
2145 m_widget
= m_wxwindow
;
2148 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
2150 GtkScrolledWindow
*scrolledWindow
= GTK_SCROLLED_WINDOW(m_widget
);
2152 // There is a conflict with default bindings at GTK+
2153 // level between scrolled windows and notebooks both of which want to use
2154 // Ctrl-PageUp/Down: scrolled windows for scrolling in the horizontal
2155 // direction and notebooks for changing pages -- we decide that if we don't
2156 // have wxHSCROLL style we can safely sacrifice horizontal scrolling if it
2157 // means we can get working keyboard navigation in notebooks
2158 if ( !HasFlag(wxHSCROLL
) )
2161 bindings
= gtk_binding_set_by_class(G_OBJECT_GET_CLASS(m_widget
));
2164 gtk_binding_entry_remove(bindings
, GDK_Page_Up
, GDK_CONTROL_MASK
);
2165 gtk_binding_entry_remove(bindings
, GDK_Page_Down
, GDK_CONTROL_MASK
);
2169 if (HasFlag(wxALWAYS_SHOW_SB
))
2171 gtk_scrolled_window_set_policy( scrolledWindow
, GTK_POLICY_ALWAYS
, GTK_POLICY_ALWAYS
);
2175 gtk_scrolled_window_set_policy( scrolledWindow
, GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
2178 m_scrollBar
[ScrollDir_Horz
] = GTK_RANGE(gtk_scrolled_window_get_hscrollbar(scrolledWindow
));
2179 m_scrollBar
[ScrollDir_Vert
] = GTK_RANGE(gtk_scrolled_window_get_vscrollbar(scrolledWindow
));
2180 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2181 gtk_range_set_inverted( m_scrollBar
[ScrollDir_Horz
], TRUE
);
2183 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
2185 // connect various scroll-related events
2186 for ( int dir
= 0; dir
< ScrollDir_Max
; dir
++ )
2188 // these handlers block mouse events to any window during scrolling
2189 // such as motion events and prevent GTK and wxWidgets from fighting
2190 // over where the slider should be
2191 g_signal_connect(m_scrollBar
[dir
], "button_press_event",
2192 G_CALLBACK(gtk_scrollbar_button_press_event
), this);
2193 g_signal_connect(m_scrollBar
[dir
], "button_release_event",
2194 G_CALLBACK(gtk_scrollbar_button_release_event
), this);
2196 gulong handler_id
= g_signal_connect(m_scrollBar
[dir
], "event_after",
2197 G_CALLBACK(gtk_scrollbar_event_after
), this);
2198 g_signal_handler_block(m_scrollBar
[dir
], handler_id
);
2200 // these handlers get notified when scrollbar slider moves
2201 g_signal_connect_after(m_scrollBar
[dir
], "value_changed",
2202 G_CALLBACK(gtk_scrollbar_value_changed
), this);
2205 gtk_widget_show( m_wxwindow
);
2207 g_object_ref(m_widget
);
2210 m_parent
->DoAddChild( this );
2212 m_focusWidget
= m_wxwindow
;
2214 SetCanFocus(AcceptsFocus());
2221 wxWindowGTK
::~wxWindowGTK()
2225 if (gs_currentFocus
== this)
2226 gs_currentFocus
= NULL
;
2227 if (gs_pendingFocus
== this)
2228 gs_pendingFocus
= NULL
;
2230 if ( gs_deferredFocusOut
== this )
2231 gs_deferredFocusOut
= NULL
;
2235 // destroy children before destroying this window itself
2238 // unhook focus handlers to prevent stray events being
2239 // propagated to this (soon to be) dead object
2240 if (m_focusWidget
!= NULL
)
2242 g_signal_handlers_disconnect_by_func (m_focusWidget
,
2243 (gpointer
) gtk_window_focus_in_callback
,
2245 g_signal_handlers_disconnect_by_func (m_focusWidget
,
2246 (gpointer
) gtk_window_focus_out_callback
,
2253 // delete before the widgets to avoid a crash on solaris
2257 // avoid problem with GTK+ 2.18 where a frozen window causes the whole
2258 // TLW to be frozen, and if the window is then destroyed, nothing ever
2259 // gets painted again
2265 // Note that gtk_widget_destroy() does not destroy the widget, it just
2266 // emits the "destroy" signal. The widget is not actually destroyed
2267 // until its reference count drops to zero.
2268 gtk_widget_destroy(m_widget
);
2269 // Release our reference, should be the last one
2270 g_object_unref(m_widget
);
2276 bool wxWindowGTK
::PreCreation( wxWindowGTK
*parent
, const wxPoint
&pos
, const wxSize
&size
)
2278 if ( GTKNeedsParent() )
2280 wxCHECK_MSG( parent
, false, wxT("Must have non-NULL parent") );
2283 // Use either the given size, or the default if -1 is given.
2284 // See wxWindowBase for these functions.
2285 m_width
= WidthDefault(size
.x
) ;
2286 m_height
= HeightDefault(size
.y
);
2294 void wxWindowGTK
::PostCreation()
2296 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid window") );
2302 // these get reported to wxWidgets -> wxPaintEvent
2304 g_signal_connect (m_wxwindow
, "expose_event",
2305 G_CALLBACK (gtk_window_expose_callback
), this);
2307 if (GetLayoutDirection() == wxLayout_LeftToRight
)
2308 gtk_widget_set_redraw_on_allocate(m_wxwindow
, HasFlag(wxFULL_REPAINT_ON_RESIZE
));
2311 // Create input method handler
2312 m_imData
= new wxGtkIMData
;
2314 // Cannot handle drawing preedited text yet
2315 gtk_im_context_set_use_preedit( m_imData
->context
, FALSE
);
2317 g_signal_connect (m_imData
->context
, "commit",
2318 G_CALLBACK (gtk_wxwindow_commit_cb
), this);
2319 g_signal_connect(m_wxwindow
, "unrealize", G_CALLBACK(unrealize
), this);
2324 if (!GTK_IS_WINDOW(m_widget
))
2326 if (m_focusWidget
== NULL
)
2327 m_focusWidget
= m_widget
;
2331 g_signal_connect (m_focusWidget
, "focus_in_event",
2332 G_CALLBACK (gtk_window_focus_in_callback
), this);
2333 g_signal_connect (m_focusWidget
, "focus_out_event",
2334 G_CALLBACK (gtk_window_focus_out_callback
), this);
2338 g_signal_connect_after (m_focusWidget
, "focus_in_event",
2339 G_CALLBACK (gtk_window_focus_in_callback
), this);
2340 g_signal_connect_after (m_focusWidget
, "focus_out_event",
2341 G_CALLBACK (gtk_window_focus_out_callback
), this);
2345 if ( !AcceptsFocusFromKeyboard() )
2349 g_signal_connect(m_widget
, "focus",
2350 G_CALLBACK(wx_window_focus_callback
), this);
2353 // connect to the various key and mouse handlers
2355 GtkWidget
*connect_widget
= GetConnectWidget();
2357 ConnectWidget( connect_widget
);
2359 // We cannot set colours, fonts and cursors before the widget has been
2360 // realized, so we do this directly after realization -- unless the widget
2361 // was in fact realized already.
2362 if ( gtk_widget_get_realized(connect_widget
) )
2364 gtk_window_realized_callback(connect_widget
, this);
2368 g_signal_connect (connect_widget
, "realize",
2369 G_CALLBACK (gtk_window_realized_callback
), this);
2374 g_signal_connect(m_wxwindow ? m_wxwindow
: m_widget
, "size_allocate",
2375 G_CALLBACK(size_allocate
), this);
2378 #if GTK_CHECK_VERSION(2, 8, 0)
2379 if ( gtk_check_version(2,8,0) == NULL
)
2381 // Make sure we can notify the app when mouse capture is lost
2384 g_signal_connect (m_wxwindow
, "grab_broken_event",
2385 G_CALLBACK (gtk_window_grab_broken
), this);
2388 if ( connect_widget
!= m_wxwindow
)
2390 g_signal_connect (connect_widget
, "grab_broken_event",
2391 G_CALLBACK (gtk_window_grab_broken
), this);
2394 #endif // GTK+ >= 2.8
2396 if ( GTKShouldConnectSizeRequest() )
2398 // This is needed if we want to add our windows into native
2399 // GTK controls, such as the toolbar. With this callback, the
2400 // toolbar gets to know the correct size (the one set by the
2401 // programmer). Sadly, it misbehaves for wxComboBox.
2402 g_signal_connect (m_widget
, "size_request",
2403 G_CALLBACK (wxgtk_window_size_request_callback
),
2407 InheritAttributes();
2411 SetLayoutDirection(wxLayout_Default
);
2413 // unless the window was created initially hidden (i.e. Hide() had been
2414 // called before Create()), we should show it at GTK+ level as well
2416 gtk_widget_show( m_widget
);
2420 wxWindowGTK
::GTKConnectWidget(const char *signal
, wxGTKCallback callback
)
2422 return g_signal_connect(m_widget
, signal
, callback
, this);
2425 void wxWindowGTK
::ConnectWidget( GtkWidget
*widget
)
2427 g_signal_connect (widget
, "key_press_event",
2428 G_CALLBACK (gtk_window_key_press_callback
), this);
2429 g_signal_connect (widget
, "key_release_event",
2430 G_CALLBACK (gtk_window_key_release_callback
), this);
2431 g_signal_connect (widget
, "button_press_event",
2432 G_CALLBACK (gtk_window_button_press_callback
), this);
2433 g_signal_connect (widget
, "button_release_event",
2434 G_CALLBACK (gtk_window_button_release_callback
), this);
2435 g_signal_connect (widget
, "motion_notify_event",
2436 G_CALLBACK (gtk_window_motion_notify_callback
), this);
2438 g_signal_connect (widget
, "scroll_event",
2439 G_CALLBACK (window_scroll_event
), this);
2440 if (m_scrollBar
[ScrollDir_Horz
])
2441 g_signal_connect (m_scrollBar
[ScrollDir_Horz
], "scroll_event",
2442 G_CALLBACK (window_scroll_event_hscrollbar
), this);
2443 if (m_scrollBar
[ScrollDir_Vert
])
2444 g_signal_connect (m_scrollBar
[ScrollDir_Vert
], "scroll_event",
2445 G_CALLBACK (window_scroll_event
), this);
2447 g_signal_connect (widget
, "popup_menu",
2448 G_CALLBACK (wxgtk_window_popup_menu_callback
), this);
2449 g_signal_connect (widget
, "enter_notify_event",
2450 G_CALLBACK (gtk_window_enter_callback
), this);
2451 g_signal_connect (widget
, "leave_notify_event",
2452 G_CALLBACK (gtk_window_leave_callback
), this);
2454 if (IsTopLevel() && m_wxwindow
)
2455 g_signal_connect (m_wxwindow
, "style_set",
2456 G_CALLBACK (gtk_window_style_set_callback
), this);
2459 bool wxWindowGTK
::Destroy()
2463 return wxWindowBase
::Destroy();
2466 void wxWindowGTK
::DoMoveWindow(int x
, int y
, int width
, int height
)
2468 gtk_widget_set_size_request(m_widget
, width
, height
);
2470 // inform the parent to perform the move
2471 wxASSERT_MSG(m_parent
&& m_parent
->m_wxwindow
,
2472 "the parent window has no client area?");
2473 WX_PIZZA(m_parent
->m_wxwindow
)->move(m_widget
, x
, y
);
2476 void wxWindowGTK
::ConstrainSize()
2479 // GPE's window manager doesn't like size hints at all, esp. when the user
2480 // has to use the virtual keyboard, so don't constrain size there
2484 const wxSize minSize
= GetMinSize();
2485 const wxSize maxSize
= GetMaxSize();
2486 if (minSize
.x
> 0 && m_width
< minSize
.x
) m_width
= minSize
.x
;
2487 if (minSize
.y
> 0 && m_height
< minSize
.y
) m_height
= minSize
.y
;
2488 if (maxSize
.x
> 0 && m_width
> maxSize
.x
) m_width
= maxSize
.x
;
2489 if (maxSize
.y
> 0 && m_height
> maxSize
.y
) m_height
= maxSize
.y
;
2493 void wxWindowGTK
::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
2495 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid window") );
2496 wxASSERT_MSG( (m_parent
!= NULL
), wxT("wxWindowGTK::SetSize requires parent.\n") );
2498 if ((sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) == 0 && (x
== -1 || y
== -1))
2500 int currentX
, currentY
;
2501 GetPosition(¤tX
, ¤tY
);
2507 AdjustForParentClientOrigin(x
, y
, sizeFlags
);
2509 // calculate the best size if we should auto size the window
2510 if ( ((sizeFlags
& wxSIZE_AUTO_WIDTH
) && width
== -1) ||
2511 ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) && height
== -1) )
2513 const wxSize sizeBest
= GetBestSize();
2514 if ( (sizeFlags
& wxSIZE_AUTO_WIDTH
) && width
== -1 )
2516 if ( (sizeFlags
& wxSIZE_AUTO_HEIGHT
) && height
== -1 )
2517 height
= sizeBest
.y
;
2520 const wxSize
oldSize(m_width
, m_height
);
2526 if (m_parent
->m_wxwindow
)
2528 wxPizza
* pizza
= WX_PIZZA(m_parent
->m_wxwindow
);
2529 m_x
= x
+ pizza
->m_scroll_x
;
2530 m_y
= y
+ pizza
->m_scroll_y
;
2532 int left_border
= 0;
2533 int right_border
= 0;
2535 int bottom_border
= 0;
2537 /* the default button has a border around it */
2538 if (gtk_widget_get_can_default(m_widget
))
2540 GtkBorder
*default_border
= NULL
;
2541 gtk_widget_style_get( m_widget
, "default_border", &default_border
, NULL
);
2544 left_border
+= default_border
->left
;
2545 right_border
+= default_border
->right
;
2546 top_border
+= default_border
->top
;
2547 bottom_border
+= default_border
->bottom
;
2548 gtk_border_free( default_border
);
2552 DoMoveWindow( m_x
- left_border
,
2554 m_width
+left_border
+right_border
,
2555 m_height
+top_border
+bottom_border
);
2558 if (m_width
!= oldSize
.x
|| m_height
!= oldSize
.y
)
2560 // update these variables to keep size_allocate handler
2561 // from sending another size event for this change
2562 GetClientSize( &m_oldClientWidth
, &m_oldClientHeight
);
2564 gtk_widget_queue_resize(m_widget
);
2565 if (!m_nativeSizeEvent
)
2567 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
2568 event
.SetEventObject( this );
2569 HandleWindowEvent( event
);
2572 if (sizeFlags
& wxSIZE_FORCE_EVENT
)
2574 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
2575 event
.SetEventObject( this );
2576 HandleWindowEvent( event
);
2580 bool wxWindowGTK
::GTKShowFromOnIdle()
2582 if (IsShown() && m_showOnIdle
&& !gtk_widget_get_visible (m_widget
))
2584 GtkAllocation alloc
;
2587 alloc
.width
= m_width
;
2588 alloc
.height
= m_height
;
2589 gtk_widget_size_allocate( m_widget
, &alloc
);
2590 gtk_widget_show( m_widget
);
2591 wxShowEvent
eventShow(GetId(), true);
2592 eventShow
.SetEventObject(this);
2593 HandleWindowEvent(eventShow
);
2594 m_showOnIdle
= false;
2601 void wxWindowGTK
::OnInternalIdle()
2603 if ( gs_deferredFocusOut
)
2604 GTKHandleDeferredFocusOut();
2606 // Check if we have to show window now
2607 if (GTKShowFromOnIdle()) return;
2609 if ( m_dirtyTabOrder
)
2611 m_dirtyTabOrder
= false;
2615 // Update style if the window was not yet realized when
2616 // SetBackgroundStyle() was called
2617 if (m_needsStyleChange
)
2619 SetBackgroundStyle(GetBackgroundStyle());
2620 m_needsStyleChange
= false;
2623 wxWindowBase
::OnInternalIdle();
2626 void wxWindowGTK
::DoGetSize( int *width
, int *height
) const
2628 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2630 if (width
) (*width
) = m_width
;
2631 if (height
) (*height
) = m_height
;
2634 void wxWindowGTK
::DoSetClientSize( int width
, int height
)
2636 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2638 const wxSize size
= GetSize();
2639 const wxSize clientSize
= GetClientSize();
2640 SetSize(width
+ (size
.x
- clientSize
.x
), height
+ (size
.y
- clientSize
.y
));
2643 void wxWindowGTK
::DoGetClientSize( int *width
, int *height
) const
2645 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2652 // if window is scrollable, account for scrollbars
2653 if ( GTK_IS_SCROLLED_WINDOW(m_widget
) )
2655 GtkPolicyType policy
[ScrollDir_Max
];
2656 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(m_widget
),
2657 &policy
[ScrollDir_Horz
],
2658 &policy
[ScrollDir_Vert
]);
2660 for ( int i
= 0; i
< ScrollDir_Max
; i
++ )
2662 // don't account for the scrollbars we don't have
2663 GtkRange
* const range
= m_scrollBar
[i
];
2667 // nor for the ones we have but don't current show
2668 switch ( policy
[i
] )
2670 case GTK_POLICY_NEVER
:
2671 // never shown so doesn't take any place
2674 case GTK_POLICY_ALWAYS
:
2675 // no checks necessary
2678 case GTK_POLICY_AUTOMATIC
:
2679 // may be shown or not, check
2680 GtkAdjustment
*adj
= gtk_range_get_adjustment(range
);
2681 if (gtk_adjustment_get_upper(adj
) <= gtk_adjustment_get_page_size(adj
))
2685 GtkScrolledWindowClass
*scroll_class
=
2686 GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) );
2689 gtk_widget_size_request(GTK_WIDGET(range
), &req
);
2690 if (i
== ScrollDir_Horz
)
2691 h
-= req
.height
+ scroll_class
->scrollbar_spacing
;
2693 w
-= req
.width
+ scroll_class
->scrollbar_spacing
;
2697 const wxSize sizeBorders
= DoGetBorderSize();
2707 if (width
) *width
= w
;
2708 if (height
) *height
= h
;
2711 wxSize wxWindowGTK
::DoGetBorderSize() const
2714 return wxWindowBase
::DoGetBorderSize();
2717 WX_PIZZA(m_wxwindow
)->get_border_widths(x
, y
);
2719 return 2*wxSize(x
, y
);
2722 void wxWindowGTK
::DoGetPosition( int *x
, int *y
) const
2724 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2728 if (!IsTopLevel() && m_parent
&& m_parent
->m_wxwindow
)
2730 wxPizza
* pizza
= WX_PIZZA(m_parent
->m_wxwindow
);
2731 dx
= pizza
->m_scroll_x
;
2732 dy
= pizza
->m_scroll_y
;
2735 if (m_x
== -1 && m_y
== -1)
2737 GdkWindow
*source
= NULL
;
2739 source
= gtk_widget_get_window(m_wxwindow
);
2741 source
= gtk_widget_get_window(m_widget
);
2747 gdk_window_get_origin( source
, &org_x
, &org_y
);
2750 m_parent
->ScreenToClient(&org_x
, &org_y
);
2752 const_cast<wxWindowGTK
*>(this)->m_x
= org_x
;
2753 const_cast<wxWindowGTK
*>(this)->m_y
= org_y
;
2757 if (x
) (*x
) = m_x
- dx
;
2758 if (y
) (*y
) = m_y
- dy
;
2761 void wxWindowGTK
::DoClientToScreen( int *x
, int *y
) const
2763 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2765 if (gtk_widget_get_window(m_widget
) == NULL
) return;
2767 GdkWindow
*source
= NULL
;
2769 source
= gtk_widget_get_window(m_wxwindow
);
2771 source
= gtk_widget_get_window(m_widget
);
2775 gdk_window_get_origin( source
, &org_x
, &org_y
);
2779 if (!gtk_widget_get_has_window(m_widget
))
2782 gtk_widget_get_allocation(m_widget
, &a
);
2791 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2792 *x
= (GetClientSize().x
- *x
) + org_x
;
2800 void wxWindowGTK
::DoScreenToClient( int *x
, int *y
) const
2802 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2804 if (!gtk_widget_get_realized(m_widget
)) return;
2806 GdkWindow
*source
= NULL
;
2808 source
= gtk_widget_get_window(m_wxwindow
);
2810 source
= gtk_widget_get_window(m_widget
);
2814 gdk_window_get_origin( source
, &org_x
, &org_y
);
2818 if (!gtk_widget_get_has_window(m_widget
))
2821 gtk_widget_get_allocation(m_widget
, &a
);
2829 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2830 *x
= (GetClientSize().x
- *x
) - org_x
;
2837 bool wxWindowGTK
::Show( bool show
)
2839 if ( !wxWindowBase
::Show(show
) )
2845 // notice that we may call Hide() before the window is created and this is
2846 // actually useful to create it hidden initially -- but we can't call
2847 // Show() before it is created
2850 wxASSERT_MSG( !show
, "can't show invalid window" );
2858 // defer until later
2862 gtk_widget_show(m_widget
);
2866 gtk_widget_hide(m_widget
);
2869 wxShowEvent
eventShow(GetId(), show
);
2870 eventShow
.SetEventObject(this);
2871 HandleWindowEvent(eventShow
);
2876 void wxWindowGTK
::DoEnable( bool enable
)
2878 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2880 gtk_widget_set_sensitive( m_widget
, enable
);
2881 if (m_wxwindow
&& (m_wxwindow
!= m_widget
))
2882 gtk_widget_set_sensitive( m_wxwindow
, enable
);
2885 int wxWindowGTK
::GetCharHeight() const
2887 wxCHECK_MSG( (m_widget
!= NULL
), 12, wxT("invalid window") );
2889 wxFont font
= GetFont();
2890 wxCHECK_MSG( font
.IsOk(), 12, wxT("invalid font") );
2892 PangoContext
* context
= gtk_widget_get_pango_context(m_widget
);
2897 PangoFontDescription
*desc
= font
.GetNativeFontInfo()->description
;
2898 PangoLayout
*layout
= pango_layout_new(context
);
2899 pango_layout_set_font_description(layout
, desc
);
2900 pango_layout_set_text(layout
, "H", 1);
2901 PangoLayoutLine
*line
= (PangoLayoutLine
*)pango_layout_get_lines(layout
)->data
;
2903 PangoRectangle rect
;
2904 pango_layout_line_get_extents(line
, NULL
, &rect
);
2906 g_object_unref (layout
);
2908 return (int) PANGO_PIXELS(rect
.height
);
2911 int wxWindowGTK
::GetCharWidth() const
2913 wxCHECK_MSG( (m_widget
!= NULL
), 8, wxT("invalid window") );
2915 wxFont font
= GetFont();
2916 wxCHECK_MSG( font
.IsOk(), 8, wxT("invalid font") );
2918 PangoContext
* context
= gtk_widget_get_pango_context(m_widget
);
2923 PangoFontDescription
*desc
= font
.GetNativeFontInfo()->description
;
2924 PangoLayout
*layout
= pango_layout_new(context
);
2925 pango_layout_set_font_description(layout
, desc
);
2926 pango_layout_set_text(layout
, "g", 1);
2927 PangoLayoutLine
*line
= (PangoLayoutLine
*)pango_layout_get_lines(layout
)->data
;
2929 PangoRectangle rect
;
2930 pango_layout_line_get_extents(line
, NULL
, &rect
);
2932 g_object_unref (layout
);
2934 return (int) PANGO_PIXELS(rect
.width
);
2937 void wxWindowGTK
::DoGetTextExtent( const wxString
& string
,
2941 int *externalLeading
,
2942 const wxFont
*theFont
) const
2944 wxFont fontToUse
= theFont ?
*theFont
: GetFont();
2946 wxCHECK_RET( fontToUse
.IsOk(), wxT("invalid font") );
2955 PangoContext
*context
= NULL
;
2957 context
= gtk_widget_get_pango_context( m_widget
);
2966 PangoFontDescription
*desc
= fontToUse
.GetNativeFontInfo()->description
;
2967 PangoLayout
*layout
= pango_layout_new(context
);
2968 pango_layout_set_font_description(layout
, desc
);
2970 const wxCharBuffer data
= wxGTK_CONV( string
);
2972 pango_layout_set_text(layout
, data
, strlen(data
));
2975 PangoRectangle rect
;
2976 pango_layout_get_extents(layout
, NULL
, &rect
);
2978 if (x
) (*x
) = (wxCoord
) PANGO_PIXELS(rect
.width
);
2979 if (y
) (*y
) = (wxCoord
) PANGO_PIXELS(rect
.height
);
2982 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
2983 int baseline
= pango_layout_iter_get_baseline(iter
);
2984 pango_layout_iter_free(iter
);
2985 *descent
= *y
- PANGO_PIXELS(baseline
);
2987 if (externalLeading
) (*externalLeading
) = 0; // ??
2989 g_object_unref (layout
);
2992 void wxWindowGTK
::GTKDisableFocusOutEvent()
2994 g_signal_handlers_block_by_func( m_focusWidget
,
2995 (gpointer
) gtk_window_focus_out_callback
, this);
2998 void wxWindowGTK
::GTKEnableFocusOutEvent()
3000 g_signal_handlers_unblock_by_func( m_focusWidget
,
3001 (gpointer
) gtk_window_focus_out_callback
, this);
3004 bool wxWindowGTK
::GTKHandleFocusIn()
3006 // Disable default focus handling for custom windows since the default GTK+
3007 // handler issues a repaint
3008 const bool retval
= m_wxwindow ?
true : false;
3011 // NB: if there's still unprocessed deferred focus-out event (see
3012 // GTKHandleFocusOut() for explanation), we need to process it first so
3013 // that the order of focus events -- focus-out first, then focus-in
3014 // elsewhere -- is preserved
3015 if ( gs_deferredFocusOut
)
3017 if ( GTKNeedsToFilterSameWindowFocus() &&
3018 gs_deferredFocusOut
== this )
3020 // GTK+ focus changed from this wxWindow back to itself, so don't
3021 // emit any events at all
3022 wxLogTrace(TRACE_FOCUS
,
3023 "filtered out spurious focus change within %s(%p, %s)",
3024 GetClassInfo()->GetClassName(), this, GetLabel());
3025 gs_deferredFocusOut
= NULL
;
3029 // otherwise we need to send focus-out first
3030 wxASSERT_MSG ( gs_deferredFocusOut
!= this,
3031 "GTKHandleFocusIn(GTKFocus_Normal) called even though focus changed back to itself - derived class should handle this" );
3032 GTKHandleDeferredFocusOut();
3036 wxLogTrace(TRACE_FOCUS
,
3037 "handling focus_in event for %s(%p, %s)",
3038 GetClassInfo()->GetClassName(), this, GetLabel());
3041 gtk_im_context_focus_in(m_imData
->context
);
3043 gs_currentFocus
= this;
3044 gs_pendingFocus
= NULL
;
3047 // caret needs to be informed about focus change
3048 wxCaret
*caret
= GetCaret();
3051 caret
->OnSetFocus();
3053 #endif // wxUSE_CARET
3055 // Notify the parent keeping track of focus for the kbd navigation
3056 // purposes that we got it.
3057 wxChildFocusEvent
eventChildFocus(static_cast<wxWindow
*>(this));
3058 GTKProcessEvent(eventChildFocus
);
3060 wxFocusEvent
eventFocus(wxEVT_SET_FOCUS
, GetId());
3061 eventFocus
.SetEventObject(this);
3062 GTKProcessEvent(eventFocus
);
3067 bool wxWindowGTK
::GTKHandleFocusOut()
3069 // Disable default focus handling for custom windows since the default GTK+
3070 // handler issues a repaint
3071 const bool retval
= m_wxwindow ?
true : false;
3074 // NB: If a control is composed of several GtkWidgets and when focus
3075 // changes from one of them to another within the same wxWindow, we get
3076 // a focus-out event followed by focus-in for another GtkWidget owned
3077 // by the same wx control. We don't want to generate two spurious
3078 // wxEVT_SET_FOCUS events in this case, so we defer sending wx events
3079 // from GTKHandleFocusOut() until we know for sure it's not coming back
3080 // (i.e. in GTKHandleFocusIn() or at idle time).
3081 if ( GTKNeedsToFilterSameWindowFocus() )
3083 wxASSERT_MSG( gs_deferredFocusOut
== NULL
,
3084 "deferred focus out event already pending" );
3085 wxLogTrace(TRACE_FOCUS
,
3086 "deferring focus_out event for %s(%p, %s)",
3087 GetClassInfo()->GetClassName(), this, GetLabel());
3088 gs_deferredFocusOut
= this;
3092 GTKHandleFocusOutNoDeferring();
3097 void wxWindowGTK
::GTKHandleFocusOutNoDeferring()
3099 wxLogTrace(TRACE_FOCUS
,
3100 "handling focus_out event for %s(%p, %s)",
3101 GetClassInfo()->GetClassName(), this, GetLabel());
3104 gtk_im_context_focus_out(m_imData
->context
);
3106 if ( gs_currentFocus
!= this )
3108 // Something is terribly wrong, gs_currentFocus is out of sync with the
3109 // real focus. We will reset it to NULL anyway, because after this
3110 // focus-out event is handled, one of the following with happen:
3112 // * either focus will go out of the app altogether, in which case
3113 // gs_currentFocus _should_ be NULL
3115 // * or it goes to another control, in which case focus-in event will
3116 // follow immediately and it will set gs_currentFocus to the right
3118 wxLogDebug("window %s(%p, %s) lost focus even though it didn't have it",
3119 GetClassInfo()->GetClassName(), this, GetLabel());
3121 gs_currentFocus
= NULL
;
3124 // caret needs to be informed about focus change
3125 wxCaret
*caret
= GetCaret();
3128 caret
->OnKillFocus();
3130 #endif // wxUSE_CARET
3132 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetId() );
3133 event
.SetEventObject( this );
3134 event
.SetWindow( FindFocus() );
3135 GTKProcessEvent( event
);
3139 void wxWindowGTK
::GTKHandleDeferredFocusOut()
3141 // NB: See GTKHandleFocusOut() for explanation. This function is called
3142 // from either GTKHandleFocusIn() or OnInternalIdle() to process
3144 if ( gs_deferredFocusOut
)
3146 wxWindowGTK
*win
= gs_deferredFocusOut
;
3147 gs_deferredFocusOut
= NULL
;
3149 wxLogTrace(TRACE_FOCUS
,
3150 "processing deferred focus_out event for %s(%p, %s)",
3151 win
->GetClassInfo()->GetClassName(), win
, win
->GetLabel());
3153 win
->GTKHandleFocusOutNoDeferring();
3157 void wxWindowGTK
::SetFocus()
3159 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
3161 // Setting "physical" focus is not immediate in GTK+ and while
3162 // gtk_widget_is_focus ("determines if the widget is the focus widget
3163 // within its toplevel", i.e. returns true for one widget per TLW, not
3164 // globally) returns true immediately after grabbing focus,
3165 // GTK_WIDGET_HAS_FOCUS (which returns true only for the one widget that
3166 // has focus at the moment) takes effect only after the window is shown
3167 // (if it was hidden at the moment of the call) or at the next event loop
3170 // Because we want to FindFocus() call immediately following
3171 // foo->SetFocus() to return foo, we have to keep track of "pending" focus
3173 gs_pendingFocus
= this;
3175 GtkWidget
*widget
= m_wxwindow ? m_wxwindow
: m_focusWidget
;
3177 if ( GTK_IS_CONTAINER(widget
) &&
3178 !gtk_widget_get_can_focus(widget
) )
3180 wxLogTrace(TRACE_FOCUS
,
3181 wxT("Setting focus to a child of %s(%p, %s)"),
3182 GetClassInfo()->GetClassName(), this, GetLabel().c_str());
3183 gtk_widget_child_focus(widget
, GTK_DIR_TAB_FORWARD
);
3187 wxLogTrace(TRACE_FOCUS
,
3188 wxT("Setting focus to %s(%p, %s)"),
3189 GetClassInfo()->GetClassName(), this, GetLabel().c_str());
3190 gtk_widget_grab_focus(widget
);
3194 void wxWindowGTK
::SetCanFocus(bool canFocus
)
3196 gtk_widget_set_can_focus(m_widget
, canFocus
);
3198 if ( m_wxwindow
&& (m_widget
!= m_wxwindow
) )
3200 gtk_widget_set_can_focus(m_wxwindow
, canFocus
);
3204 bool wxWindowGTK
::Reparent( wxWindowBase
*newParentBase
)
3206 wxCHECK_MSG( (m_widget
!= NULL
), false, wxT("invalid window") );
3208 wxWindowGTK
* const newParent
= (wxWindowGTK
*)newParentBase
;
3210 wxASSERT( GTK_IS_WIDGET(m_widget
) );
3212 if ( !wxWindowBase
::Reparent(newParent
) )
3215 wxASSERT( GTK_IS_WIDGET(m_widget
) );
3217 // Notice that old m_parent pointer might be non-NULL here but the widget
3218 // still not have any parent at GTK level if it's a notebook page that had
3219 // been removed from the notebook so test this at GTK level and not wx one.
3220 if ( GtkWidget
*parentGTK
= gtk_widget_get_parent(m_widget
) )
3221 gtk_container_remove(GTK_CONTAINER(parentGTK
), m_widget
);
3223 wxASSERT( GTK_IS_WIDGET(m_widget
) );
3227 if (gtk_widget_get_visible (newParent
->m_widget
))
3229 m_showOnIdle
= true;
3230 gtk_widget_hide( m_widget
);
3232 /* insert GTK representation */
3233 newParent
->AddChildGTK(this);
3236 SetLayoutDirection(wxLayout_Default
);
3241 void wxWindowGTK
::DoAddChild(wxWindowGTK
*child
)
3243 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid window") );
3244 wxASSERT_MSG( (child
!= NULL
), wxT("invalid child window") );
3249 /* insert GTK representation */
3253 void wxWindowGTK
::AddChild(wxWindowBase
*child
)
3255 wxWindowBase
::AddChild(child
);
3256 m_dirtyTabOrder
= true;
3257 wxTheApp
->WakeUpIdle();
3260 void wxWindowGTK
::RemoveChild(wxWindowBase
*child
)
3262 wxWindowBase
::RemoveChild(child
);
3263 m_dirtyTabOrder
= true;
3264 wxTheApp
->WakeUpIdle();
3268 wxLayoutDirection wxWindowGTK
::GTKGetLayout(GtkWidget
*widget
)
3270 return gtk_widget_get_direction(widget
) == GTK_TEXT_DIR_RTL
3271 ? wxLayout_RightToLeft
3272 : wxLayout_LeftToRight
;
3276 void wxWindowGTK
::GTKSetLayout(GtkWidget
*widget
, wxLayoutDirection dir
)
3278 wxASSERT_MSG( dir
!= wxLayout_Default
, wxT("invalid layout direction") );
3280 gtk_widget_set_direction(widget
,
3281 dir
== wxLayout_RightToLeft ? GTK_TEXT_DIR_RTL
3282 : GTK_TEXT_DIR_LTR
);
3285 wxLayoutDirection wxWindowGTK
::GetLayoutDirection() const
3287 return GTKGetLayout(m_widget
);
3290 void wxWindowGTK
::SetLayoutDirection(wxLayoutDirection dir
)
3292 if ( dir
== wxLayout_Default
)
3294 const wxWindow
*const parent
= GetParent();
3297 // inherit layout from parent.
3298 dir
= parent
->GetLayoutDirection();
3300 else // no parent, use global default layout
3302 dir
= wxTheApp
->GetLayoutDirection();
3306 if ( dir
== wxLayout_Default
)
3309 GTKSetLayout(m_widget
, dir
);
3311 if (m_wxwindow
&& (m_wxwindow
!= m_widget
))
3312 GTKSetLayout(m_wxwindow
, dir
);
3316 wxWindowGTK
::AdjustForLayoutDirection(wxCoord x
,
3317 wxCoord
WXUNUSED(width
),
3318 wxCoord
WXUNUSED(widthTotal
)) const
3320 // We now mirror the coordinates of RTL windows in wxPizza
3324 void wxWindowGTK
::DoMoveInTabOrder(wxWindow
*win
, WindowOrder move
)
3326 wxWindowBase
::DoMoveInTabOrder(win
, move
);
3327 m_dirtyTabOrder
= true;
3328 wxTheApp
->WakeUpIdle();
3331 bool wxWindowGTK
::DoNavigateIn(int flags
)
3333 if ( flags
& wxNavigationKeyEvent
::WinChange
)
3335 wxFAIL_MSG( wxT("not implemented") );
3339 else // navigate inside the container
3341 wxWindow
*parent
= wxGetTopLevelParent((wxWindow
*)this);
3342 wxCHECK_MSG( parent
, false, wxT("every window must have a TLW parent") );
3344 GtkDirectionType dir
;
3345 dir
= flags
& wxNavigationKeyEvent
::IsForward ? GTK_DIR_TAB_FORWARD
3346 : GTK_DIR_TAB_BACKWARD
;
3349 g_signal_emit_by_name(parent
->m_widget
, "focus", dir
, &rc
);
3355 bool wxWindowGTK
::GTKWidgetNeedsMnemonic() const
3357 // none needed by default
3361 void wxWindowGTK
::GTKWidgetDoSetMnemonic(GtkWidget
* WXUNUSED(w
))
3363 // nothing to do by default since none is needed
3366 void wxWindowGTK
::RealizeTabOrder()
3370 if ( !m_children
.empty() )
3372 // we don't only construct the correct focus chain but also use
3373 // this opportunity to update the mnemonic widgets for the widgets
3376 GList
*chain
= NULL
;
3377 wxWindowGTK
* mnemonicWindow
= NULL
;
3379 for ( wxWindowList
::const_iterator i
= m_children
.begin();
3380 i
!= m_children
.end();
3383 wxWindowGTK
*win
= *i
;
3385 bool focusableFromKeyboard
= win
->AcceptsFocusFromKeyboard();
3387 if ( mnemonicWindow
)
3389 if ( focusableFromKeyboard
)
3391 // wxComboBox et al. needs to focus on on a different
3392 // widget than m_widget, so if the main widget isn't
3393 // focusable try the connect widget
3394 GtkWidget
* w
= win
->m_widget
;
3395 if ( !gtk_widget_get_can_focus(w
) )
3397 w
= win
->GetConnectWidget();
3398 if ( !gtk_widget_get_can_focus(w
) )
3404 mnemonicWindow
->GTKWidgetDoSetMnemonic(w
);
3405 mnemonicWindow
= NULL
;
3409 else if ( win
->GTKWidgetNeedsMnemonic() )
3411 mnemonicWindow
= win
;
3414 if ( focusableFromKeyboard
)
3415 chain
= g_list_prepend(chain
, win
->m_widget
);
3418 chain
= g_list_reverse(chain
);
3420 gtk_container_set_focus_chain(GTK_CONTAINER(m_wxwindow
), chain
);
3425 gtk_container_unset_focus_chain(GTK_CONTAINER(m_wxwindow
));
3430 void wxWindowGTK
::Raise()
3432 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
3434 if (m_wxwindow
&& gtk_widget_get_window(m_wxwindow
))
3436 gdk_window_raise(gtk_widget_get_window(m_wxwindow
));
3438 else if (gtk_widget_get_window(m_widget
))
3440 gdk_window_raise(gtk_widget_get_window(m_widget
));
3444 void wxWindowGTK
::Lower()
3446 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
3448 if (m_wxwindow
&& gtk_widget_get_window(m_wxwindow
))
3450 gdk_window_lower(gtk_widget_get_window(m_wxwindow
));
3452 else if (gtk_widget_get_window(m_widget
))
3454 gdk_window_lower(gtk_widget_get_window(m_widget
));
3458 bool wxWindowGTK
::SetCursor( const wxCursor
&cursor
)
3460 if ( !wxWindowBase
::SetCursor(cursor
.IsOk() ? cursor
: *wxSTANDARD_CURSOR
) )
3468 void wxWindowGTK
::GTKUpdateCursor(bool update_self
/*=true*/, bool recurse
/*=true*/)
3472 wxCursor
cursor(g_globalCursor
.IsOk() ? g_globalCursor
: GetCursor());
3473 if ( cursor
.IsOk() )
3475 wxArrayGdkWindows windowsThis
;
3476 GdkWindow
* window
= GTKGetWindow(windowsThis
);
3478 gdk_window_set_cursor( window
, cursor
.GetCursor() );
3481 const size_t count
= windowsThis
.size();
3482 for ( size_t n
= 0; n
< count
; n
++ )
3484 GdkWindow
*win
= windowsThis
[n
];
3485 // It can be zero if the window has not been realized yet.
3488 gdk_window_set_cursor(win
, cursor
.GetCursor());
3497 for (wxWindowList
::iterator it
= GetChildren().begin(); it
!= GetChildren().end(); ++it
)
3499 (*it
)->GTKUpdateCursor( true );
3504 void wxWindowGTK
::WarpPointer( int x
, int y
)
3506 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
3508 ClientToScreen(&x
, &y
);
3509 GdkDisplay
* display
= gtk_widget_get_display(m_widget
);
3510 GdkScreen
* screen
= gtk_widget_get_screen(m_widget
);
3512 GdkDeviceManager
* manager
= gdk_display_get_device_manager(display
);
3513 gdk_device_warp(gdk_device_manager_get_client_pointer(manager
), screen
, x
, y
);
3515 XWarpPointer(GDK_DISPLAY_XDISPLAY(display
),
3517 GDK_WINDOW_XID(gdk_screen_get_root_window(screen
)),
3522 wxWindowGTK
::ScrollDir wxWindowGTK
::ScrollDirFromRange(GtkRange
*range
) const
3524 // find the scrollbar which generated the event
3525 for ( int dir
= 0; dir
< ScrollDir_Max
; dir
++ )
3527 if ( range
== m_scrollBar
[dir
] )
3528 return (ScrollDir
)dir
;
3531 wxFAIL_MSG( wxT("event from unknown scrollbar received") );
3533 return ScrollDir_Max
;
3536 bool wxWindowGTK
::DoScrollByUnits(ScrollDir dir
, ScrollUnit unit
, int units
)
3538 bool changed
= false;
3539 GtkRange
* range
= m_scrollBar
[dir
];
3540 if ( range
&& units
)
3542 GtkAdjustment
* adj
= gtk_range_get_adjustment(range
);
3543 double inc
= unit
== ScrollUnit_Line ?
gtk_adjustment_get_step_increment(adj
)
3544 : gtk_adjustment_get_page_increment(adj
);
3546 const int posOld
= wxRound(gtk_adjustment_get_value(adj
));
3547 gtk_range_set_value(range
, posOld
+ units
*inc
);
3549 changed
= wxRound(gtk_adjustment_get_value(adj
)) != posOld
;
3555 bool wxWindowGTK
::ScrollLines(int lines
)
3557 return DoScrollByUnits(ScrollDir_Vert
, ScrollUnit_Line
, lines
);
3560 bool wxWindowGTK
::ScrollPages(int pages
)
3562 return DoScrollByUnits(ScrollDir_Vert
, ScrollUnit_Page
, pages
);
3565 void wxWindowGTK
::Refresh(bool WXUNUSED(eraseBackground
),
3570 if (gtk_widget_get_mapped(m_wxwindow
))
3572 GdkWindow
* window
= gtk_widget_get_window(m_wxwindow
);
3575 GdkRectangle r
= { rect
->x
, rect
->y
, rect
->width
, rect
->height
};
3576 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3577 r
.x
= gdk_window_get_width(window
) - r
.x
- rect
->width
;
3578 gdk_window_invalidate_rect(window
, &r
, true);
3581 gdk_window_invalidate_rect(window
, NULL
, true);
3586 if (gtk_widget_get_mapped(m_widget
))
3589 gtk_widget_queue_draw_area(m_widget
, rect
->x
, rect
->y
, rect
->width
, rect
->height
);
3591 gtk_widget_queue_draw(m_widget
);
3596 void wxWindowGTK
::Update()
3598 if (m_widget
&& gtk_widget_get_mapped(m_widget
))
3600 GdkDisplay
* display
= gtk_widget_get_display(m_widget
);
3601 // Flush everything out to the server, and wait for it to finish.
3602 // This ensures nothing will overwrite the drawing we are about to do.
3603 gdk_display_sync(display
);
3605 GdkWindow
* window
= GTKGetDrawingWindow();
3607 window
= gtk_widget_get_window(m_widget
);
3608 gdk_window_process_updates(window
, true);
3610 // Flush again, but no need to wait for it to finish
3611 gdk_display_flush(display
);
3615 bool wxWindowGTK
::DoIsExposed( int x
, int y
) const
3617 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
3620 bool wxWindowGTK
::DoIsExposed( int x
, int y
, int w
, int h
) const
3622 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3623 return m_updateRegion
.Contains(x
-w
, y
, w
, h
) != wxOutRegion
;
3625 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
3628 void wxWindowGTK
::GtkSendPaintEvents()
3632 m_updateRegion
.Clear();
3636 // Clip to paint region in wxClientDC
3637 m_clipPaintRegion
= true;
3639 m_nativeUpdateRegion
= m_updateRegion
;
3641 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3643 // Transform m_updateRegion under RTL
3644 m_updateRegion
.Clear();
3647 gdk_drawable_get_size(gtk_widget_get_window(m_wxwindow
), &width
, NULL
);
3649 wxRegionIterator
upd( m_nativeUpdateRegion
);
3653 rect
.x
= upd
.GetX();
3654 rect
.y
= upd
.GetY();
3655 rect
.width
= upd
.GetWidth();
3656 rect
.height
= upd
.GetHeight();
3658 rect
.x
= width
- rect
.x
- rect
.width
;
3659 m_updateRegion
.Union( rect
);
3665 switch ( GetBackgroundStyle() )
3667 case wxBG_STYLE_ERASE
:
3669 wxWindowDC
dc( (wxWindow
*)this );
3670 dc
.SetDeviceClippingRegion( m_updateRegion
);
3672 // Work around gtk-qt <= 0.60 bug whereby the window colour
3676 GetOptionInt("gtk.window.force-background-colour") )
3678 dc
.SetBackground(GetBackgroundColour());
3682 wxEraseEvent
erase_event( GetId(), &dc
);
3683 erase_event
.SetEventObject( this );
3685 if ( HandleWindowEvent(erase_event
) )
3687 // background erased, don't do it again
3693 case wxBG_STYLE_SYSTEM
:
3694 if ( GetThemeEnabled() )
3696 // find ancestor from which to steal background
3697 wxWindow
*parent
= wxGetTopLevelParent((wxWindow
*)this);
3699 parent
= (wxWindow
*)this;
3701 if (gtk_widget_get_mapped(parent
->m_widget
))
3703 wxRegionIterator
upd( m_nativeUpdateRegion
);
3707 rect
.x
= upd
.GetX();
3708 rect
.y
= upd
.GetY();
3709 rect
.width
= upd
.GetWidth();
3710 rect
.height
= upd
.GetHeight();
3712 gtk_paint_flat_box(gtk_widget_get_style(parent
->m_widget
),
3713 GTKGetDrawingWindow(),
3714 gtk_widget_get_state(m_wxwindow
),
3727 case wxBG_STYLE_PAINT
:
3728 // nothing to do: window will be painted over in EVT_PAINT
3732 wxFAIL_MSG( "unsupported background style" );
3735 wxNcPaintEvent
nc_paint_event( GetId() );
3736 nc_paint_event
.SetEventObject( this );
3737 HandleWindowEvent( nc_paint_event
);
3739 wxPaintEvent
paint_event( GetId() );
3740 paint_event
.SetEventObject( this );
3741 HandleWindowEvent( paint_event
);
3743 m_clipPaintRegion
= false;
3745 m_updateRegion
.Clear();
3746 m_nativeUpdateRegion
.Clear();
3749 void wxWindowGTK
::SetDoubleBuffered( bool on
)
3751 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
3754 gtk_widget_set_double_buffered( m_wxwindow
, on
);
3757 bool wxWindowGTK
::IsDoubleBuffered() const
3759 return gtk_widget_get_double_buffered( m_wxwindow
);
3762 void wxWindowGTK
::ClearBackground()
3764 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
3768 void wxWindowGTK
::DoSetToolTip( wxToolTip
*tip
)
3770 if (m_tooltip
!= tip
)
3772 wxWindowBase
::DoSetToolTip(tip
);
3775 m_tooltip
->GTKSetWindow(static_cast<wxWindow
*>(this));
3777 GTKApplyToolTip(NULL
);
3781 void wxWindowGTK
::GTKApplyToolTip(const char* tip
)
3783 wxToolTip
::GTKApply(GetConnectWidget(), tip
);
3785 #endif // wxUSE_TOOLTIPS
3787 bool wxWindowGTK
::SetBackgroundColour( const wxColour
&colour
)
3789 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
3791 if (!wxWindowBase
::SetBackgroundColour(colour
))
3796 // We need the pixel value e.g. for background clearing.
3797 m_backgroundColour
.CalcPixel(gtk_widget_get_colormap(m_widget
));
3800 // apply style change (forceStyle=true so that new style is applied
3801 // even if the bg colour changed from valid to wxNullColour)
3802 GTKApplyWidgetStyle(true);
3807 bool wxWindowGTK
::SetForegroundColour( const wxColour
&colour
)
3809 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
3811 if (!wxWindowBase
::SetForegroundColour(colour
))
3818 // We need the pixel value e.g. for background clearing.
3819 m_foregroundColour
.CalcPixel(gtk_widget_get_colormap(m_widget
));
3822 // apply style change (forceStyle=true so that new style is applied
3823 // even if the bg colour changed from valid to wxNullColour):
3824 GTKApplyWidgetStyle(true);
3829 PangoContext
*wxWindowGTK
::GTKGetPangoDefaultContext()
3831 return gtk_widget_get_pango_context( m_widget
);
3834 GtkRcStyle
*wxWindowGTK
::GTKCreateWidgetStyle(bool forceStyle
)
3836 // do we need to apply any changes at all?
3839 !m_foregroundColour
.IsOk() && !m_backgroundColour
.IsOk() )
3844 GtkRcStyle
*style
= gtk_rc_style_new();
3846 if ( m_font
.IsOk() )
3849 pango_font_description_copy( m_font
.GetNativeFontInfo()->description
);
3852 int flagsNormal
= 0,
3855 flagsInsensitive
= 0;
3857 if ( m_foregroundColour
.IsOk() )
3859 const GdkColor
*fg
= m_foregroundColour
.GetColor();
3861 style
->fg
[GTK_STATE_NORMAL
] =
3862 style
->text
[GTK_STATE_NORMAL
] = *fg
;
3863 flagsNormal
|= GTK_RC_FG
| GTK_RC_TEXT
;
3865 style
->fg
[GTK_STATE_PRELIGHT
] =
3866 style
->text
[GTK_STATE_PRELIGHT
] = *fg
;
3867 flagsPrelight
|= GTK_RC_FG
| GTK_RC_TEXT
;
3869 style
->fg
[GTK_STATE_ACTIVE
] =
3870 style
->text
[GTK_STATE_ACTIVE
] = *fg
;
3871 flagsActive
|= GTK_RC_FG
| GTK_RC_TEXT
;
3874 if ( m_backgroundColour
.IsOk() )
3876 const GdkColor
*bg
= m_backgroundColour
.GetColor();
3878 style
->bg
[GTK_STATE_NORMAL
] =
3879 style
->base
[GTK_STATE_NORMAL
] = *bg
;
3880 flagsNormal
|= GTK_RC_BG
| GTK_RC_BASE
;
3882 style
->bg
[GTK_STATE_PRELIGHT
] =
3883 style
->base
[GTK_STATE_PRELIGHT
] = *bg
;
3884 flagsPrelight
|= GTK_RC_BG
| GTK_RC_BASE
;
3886 style
->bg
[GTK_STATE_ACTIVE
] =
3887 style
->base
[GTK_STATE_ACTIVE
] = *bg
;
3888 flagsActive
|= GTK_RC_BG
| GTK_RC_BASE
;
3890 style
->bg
[GTK_STATE_INSENSITIVE
] =
3891 style
->base
[GTK_STATE_INSENSITIVE
] = *bg
;
3892 flagsInsensitive
|= GTK_RC_BG
| GTK_RC_BASE
;
3895 style
->color_flags
[GTK_STATE_NORMAL
] = (GtkRcFlags
)flagsNormal
;
3896 style
->color_flags
[GTK_STATE_PRELIGHT
] = (GtkRcFlags
)flagsPrelight
;
3897 style
->color_flags
[GTK_STATE_ACTIVE
] = (GtkRcFlags
)flagsActive
;
3898 style
->color_flags
[GTK_STATE_INSENSITIVE
] = (GtkRcFlags
)flagsInsensitive
;
3903 void wxWindowGTK
::GTKApplyWidgetStyle(bool forceStyle
)
3905 GtkRcStyle
*style
= GTKCreateWidgetStyle(forceStyle
);
3908 DoApplyWidgetStyle(style
);
3909 g_object_unref(style
);
3912 // Style change may affect GTK+'s size calculation:
3913 InvalidateBestSize();
3916 void wxWindowGTK
::DoApplyWidgetStyle(GtkRcStyle
*style
)
3920 // block the signal temporarily to avoid sending
3921 // wxSysColourChangedEvents when we change the colours ourselves
3922 bool unblock
= false;
3926 g_signal_handlers_block_by_func(
3927 m_wxwindow
, (void *)gtk_window_style_set_callback
, this);
3930 gtk_widget_modify_style(m_wxwindow
, style
);
3934 g_signal_handlers_unblock_by_func(
3935 m_wxwindow
, (void *)gtk_window_style_set_callback
, this);
3940 gtk_widget_modify_style(m_widget
, style
);
3944 bool wxWindowGTK
::SetBackgroundStyle(wxBackgroundStyle style
)
3946 wxWindowBase
::SetBackgroundStyle(style
);
3948 if ( style
== wxBG_STYLE_PAINT
)
3953 window
= GTKGetDrawingWindow();
3957 GtkWidget
* const w
= GetConnectWidget();
3958 window
= w ?
gtk_widget_get_window(w
) : NULL
;
3963 // Make sure GDK/X11 doesn't refresh the window
3965 gdk_window_set_back_pixmap( window
, None
, False
);
3967 Display
* display
= GDK_WINDOW_DISPLAY(window
);
3970 m_needsStyleChange
= false;
3972 else // window not realized yet
3974 // Do in OnIdle, because the window is not yet available
3975 m_needsStyleChange
= true;
3978 // Don't apply widget style, or we get a grey background
3982 // apply style change (forceStyle=true so that new style is applied
3983 // even if the bg colour changed from valid to wxNullColour):
3984 GTKApplyWidgetStyle(true);
3990 // ----------------------------------------------------------------------------
3991 // Pop-up menu stuff
3992 // ----------------------------------------------------------------------------
3994 #if wxUSE_MENUS_NATIVE
3998 void wxPopupMenuPositionCallback( GtkMenu
*menu
,
4000 gboolean
* WXUNUSED(whatever
),
4001 gpointer user_data
)
4003 // ensure that the menu appears entirely on screen
4005 gtk_widget_get_child_requisition(GTK_WIDGET(menu
), &req
);
4007 wxSize sizeScreen
= wxGetDisplaySize();
4008 wxPoint
*pos
= (wxPoint
*)user_data
;
4010 gint xmax
= sizeScreen
.x
- req
.width
,
4011 ymax
= sizeScreen
.y
- req
.height
;
4013 *x
= pos
->x
< xmax ? pos
->x
: xmax
;
4014 *y
= pos
->y
< ymax ? pos
->y
: ymax
;
4018 bool wxWindowGTK
::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
4020 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
4022 // For compatibility with other ports, pretend that the window showing the
4023 // menu has focus while the menu is shown. This is needed because the popup
4024 // menu actually steals the focus from the window it's associated it in
4025 // wxGTK unlike, say, wxMSW.
4026 wxWindowGTK
* const oldPendingFocus
= gs_pendingFocus
;
4027 gs_pendingFocus
= this;
4028 wxON_BLOCK_EXIT_SET( gs_pendingFocus
, oldPendingFocus
);
4034 GtkMenuPositionFunc posfunc
;
4035 if ( x
== -1 && y
== -1 )
4037 // use GTK's default positioning algorithm
4043 pos
= ClientToScreen(wxPoint(x
, y
));
4045 posfunc
= wxPopupMenuPositionCallback
;
4048 menu
->m_popupShown
= true;
4050 GTK_MENU(menu
->m_menu
),
4051 NULL
, // parent menu shell
4052 NULL
, // parent menu item
4053 posfunc
, // function to position it
4054 userdata
, // client data
4055 0, // button used to activate it
4056 gtk_get_current_event_time()
4059 while (menu
->m_popupShown
)
4061 gtk_main_iteration();
4067 #endif // wxUSE_MENUS_NATIVE
4069 #if wxUSE_DRAG_AND_DROP
4071 void wxWindowGTK
::SetDropTarget( wxDropTarget
*dropTarget
)
4073 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
4075 GtkWidget
*dnd_widget
= GetConnectWidget();
4077 if (m_dropTarget
) m_dropTarget
->GtkUnregisterWidget( dnd_widget
);
4079 if (m_dropTarget
) delete m_dropTarget
;
4080 m_dropTarget
= dropTarget
;
4082 if (m_dropTarget
) m_dropTarget
->GtkRegisterWidget( dnd_widget
);
4085 #endif // wxUSE_DRAG_AND_DROP
4087 GtkWidget
* wxWindowGTK
::GetConnectWidget()
4089 GtkWidget
*connect_widget
= m_widget
;
4090 if (m_wxwindow
) connect_widget
= m_wxwindow
;
4092 return connect_widget
;
4095 bool wxWindowGTK
::GTKIsOwnWindow(GdkWindow
*window
) const
4097 wxArrayGdkWindows windowsThis
;
4098 GdkWindow
* const winThis
= GTKGetWindow(windowsThis
);
4100 return winThis ? window
== winThis
4101 : windowsThis
.Index(window
) != wxNOT_FOUND
;
4104 GdkWindow
*wxWindowGTK
::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
4106 return m_wxwindow ?
GTKGetDrawingWindow() : gtk_widget_get_window(m_widget
);
4109 bool wxWindowGTK
::SetFont( const wxFont
&font
)
4111 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
4113 if (!wxWindowBase
::SetFont(font
))
4116 // apply style change (forceStyle=true so that new style is applied
4117 // even if the font changed from valid to wxNullFont):
4118 GTKApplyWidgetStyle(true);
4123 void wxWindowGTK
::DoCaptureMouse()
4125 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
4127 GdkWindow
*window
= NULL
;
4129 window
= GTKGetDrawingWindow();
4131 window
= gtk_widget_get_window(GetConnectWidget());
4133 wxCHECK_RET( window
, wxT("CaptureMouse() failed") );
4135 const wxCursor
* cursor
= &m_cursor
;
4136 if (!cursor
->IsOk())
4137 cursor
= wxSTANDARD_CURSOR
;
4139 gdk_pointer_grab( window
, FALSE
,
4141 (GDK_BUTTON_PRESS_MASK
|
4142 GDK_BUTTON_RELEASE_MASK
|
4143 GDK_POINTER_MOTION_HINT_MASK
|
4144 GDK_POINTER_MOTION_MASK
),
4146 cursor
->GetCursor(),
4147 (guint32
)GDK_CURRENT_TIME
);
4148 g_captureWindow
= this;
4149 g_captureWindowHasMouse
= true;
4152 void wxWindowGTK
::DoReleaseMouse()
4154 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
4156 wxCHECK_RET( g_captureWindow
, wxT("can't release mouse - not captured") );
4158 g_captureWindow
= NULL
;
4160 GdkWindow
*window
= NULL
;
4162 window
= GTKGetDrawingWindow();
4164 window
= gtk_widget_get_window(GetConnectWidget());
4169 gdk_pointer_ungrab ( (guint32
)GDK_CURRENT_TIME
);
4172 void wxWindowGTK
::GTKReleaseMouseAndNotify()
4175 wxMouseCaptureLostEvent
evt(GetId());
4176 evt
.SetEventObject( this );
4177 HandleWindowEvent( evt
);
4181 wxWindow
*wxWindowBase
::GetCapture()
4183 return (wxWindow
*)g_captureWindow
;
4186 bool wxWindowGTK
::IsRetained() const
4191 void wxWindowGTK
::SetScrollbar(int orient
,
4195 bool WXUNUSED(update
))
4197 const int dir
= ScrollDirFromOrient(orient
);
4198 GtkRange
* const sb
= m_scrollBar
[dir
];
4199 wxCHECK_RET( sb
, wxT("this window is not scrollable") );
4203 // GtkRange requires upper > lower
4208 g_signal_handlers_block_by_func(
4209 sb
, (void*)gtk_scrollbar_value_changed
, this);
4211 gtk_range_set_increments(sb
, 1, thumbVisible
);
4212 gtk_adjustment_set_page_size(gtk_range_get_adjustment(sb
), thumbVisible
);
4213 gtk_range_set_range(sb
, 0, range
);
4214 gtk_range_set_value(sb
, pos
);
4215 m_scrollPos
[dir
] = gtk_range_get_value(sb
);
4217 g_signal_handlers_unblock_by_func(
4218 sb
, (void*)gtk_scrollbar_value_changed
, this);
4221 void wxWindowGTK
::SetScrollPos(int orient
, int pos
, bool WXUNUSED(refresh
))
4223 const int dir
= ScrollDirFromOrient(orient
);
4224 GtkRange
* const sb
= m_scrollBar
[dir
];
4225 wxCHECK_RET( sb
, wxT("this window is not scrollable") );
4227 // This check is more than an optimization. Without it, the slider
4228 // will not move smoothly while tracking when using wxScrollHelper.
4229 if (GetScrollPos(orient
) != pos
)
4231 g_signal_handlers_block_by_func(
4232 sb
, (void*)gtk_scrollbar_value_changed
, this);
4234 gtk_range_set_value(sb
, pos
);
4235 m_scrollPos
[dir
] = gtk_range_get_value(sb
);
4237 g_signal_handlers_unblock_by_func(
4238 sb
, (void*)gtk_scrollbar_value_changed
, this);
4242 int wxWindowGTK
::GetScrollThumb(int orient
) const
4244 GtkRange
* const sb
= m_scrollBar
[ScrollDirFromOrient(orient
)];
4245 wxCHECK_MSG( sb
, 0, wxT("this window is not scrollable") );
4247 return wxRound(gtk_adjustment_get_page_size(gtk_range_get_adjustment(sb
)));
4250 int wxWindowGTK
::GetScrollPos( int orient
) const
4252 GtkRange
* const sb
= m_scrollBar
[ScrollDirFromOrient(orient
)];
4253 wxCHECK_MSG( sb
, 0, wxT("this window is not scrollable") );
4255 return wxRound(gtk_range_get_value(sb
));
4258 int wxWindowGTK
::GetScrollRange( int orient
) const
4260 GtkRange
* const sb
= m_scrollBar
[ScrollDirFromOrient(orient
)];
4261 wxCHECK_MSG( sb
, 0, wxT("this window is not scrollable") );
4263 return wxRound(gtk_adjustment_get_upper(gtk_range_get_adjustment(sb
)));
4266 // Determine if increment is the same as +/-x, allowing for some small
4267 // difference due to possible inexactness in floating point arithmetic
4268 static inline bool IsScrollIncrement(double increment
, double x
)
4270 wxASSERT(increment
> 0);
4271 const double tolerance
= 1.0 / 1024;
4272 return fabs(increment
- fabs(x
)) < tolerance
;
4275 wxEventType wxWindowGTK
::GTKGetScrollEventType(GtkRange
* range
)
4277 wxASSERT(range
== m_scrollBar
[0] || range
== m_scrollBar
[1]);
4279 const int barIndex
= range
== m_scrollBar
[1];
4281 const double value
= gtk_range_get_value(range
);
4283 // save previous position
4284 const double oldPos
= m_scrollPos
[barIndex
];
4285 // update current position
4286 m_scrollPos
[barIndex
] = value
;
4287 // If event should be ignored, or integral position has not changed
4288 if (!m_hasVMT
|| g_blockEventsOnDrag
|| wxRound(value
) == wxRound(oldPos
))
4293 wxEventType eventType
= wxEVT_SCROLL_THUMBTRACK
;
4296 // Difference from last change event
4297 const double diff
= value
- oldPos
;
4298 const bool isDown
= diff
> 0;
4300 GtkAdjustment
* adj
= gtk_range_get_adjustment(range
);
4301 if (IsScrollIncrement(gtk_adjustment_get_step_increment(adj
), diff
))
4303 eventType
= isDown ? wxEVT_SCROLL_LINEDOWN
: wxEVT_SCROLL_LINEUP
;
4305 else if (IsScrollIncrement(gtk_adjustment_get_page_increment(adj
), diff
))
4307 eventType
= isDown ? wxEVT_SCROLL_PAGEDOWN
: wxEVT_SCROLL_PAGEUP
;
4309 else if (m_mouseButtonDown
)
4311 // Assume track event
4312 m_isScrolling
= true;
4318 void wxWindowGTK
::ScrollWindow( int dx
, int dy
, const wxRect
* WXUNUSED(rect
) )
4320 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
4322 wxCHECK_RET( m_wxwindow
!= NULL
, wxT("window needs client area for scrolling") );
4324 // No scrolling requested.
4325 if ((dx
== 0) && (dy
== 0)) return;
4327 m_clipPaintRegion
= true;
4329 WX_PIZZA(m_wxwindow
)->scroll(dx
, dy
);
4331 m_clipPaintRegion
= false;
4334 bool restoreCaret
= (GetCaret() != NULL
&& GetCaret()->IsVisible());
4337 wxRect
caretRect(GetCaret()->GetPosition(), GetCaret()->GetSize());
4339 caretRect
.width
+= dx
;
4342 caretRect
.x
+= dx
; caretRect
.width
-= dx
;
4345 caretRect
.height
+= dy
;
4348 caretRect
.y
+= dy
; caretRect
.height
-= dy
;
4351 RefreshRect(caretRect
);
4353 #endif // wxUSE_CARET
4356 void wxWindowGTK
::GTKScrolledWindowSetBorder(GtkWidget
* w
, int wxstyle
)
4358 //RN: Note that static controls usually have no border on gtk, so maybe
4359 //it makes sense to treat that as simply no border at the wx level
4361 if (!(wxstyle
& wxNO_BORDER
) && !(wxstyle
& wxBORDER_STATIC
))
4363 GtkShadowType gtkstyle
;
4365 if(wxstyle
& wxBORDER_RAISED
)
4366 gtkstyle
= GTK_SHADOW_OUT
;
4367 else if ((wxstyle
& wxBORDER_SUNKEN
) || (wxstyle
& wxBORDER_THEME
))
4368 gtkstyle
= GTK_SHADOW_IN
;
4371 else if (wxstyle
& wxBORDER_DOUBLE
)
4372 gtkstyle
= GTK_SHADOW_ETCHED_IN
;
4375 gtkstyle
= GTK_SHADOW_IN
;
4377 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(w
),
4382 void wxWindowGTK
::SetWindowStyleFlag( long style
)
4384 // Updates the internal variable. NB: Now m_windowStyle bits carry the _new_ style values already
4385 wxWindowBase
::SetWindowStyleFlag(style
);
4388 // Find the wxWindow at the current mouse position, also returning the mouse
4390 wxWindow
* wxFindWindowAtPointer(wxPoint
& pt
)
4392 pt
= wxGetMousePosition();
4393 wxWindow
* found
= wxFindWindowAtPoint(pt
);
4397 // Get the current mouse position.
4398 wxPoint
wxGetMousePosition()
4400 /* This crashes when used within wxHelpContext,
4401 so we have to use the X-specific implementation below.
4403 GdkModifierType *mask;
4404 (void) gdk_window_get_pointer(NULL, &x, &y, mask);
4406 return wxPoint(x, y);
4410 GdkWindow
* windowAtPtr
= gdk_window_at_pointer(& x
, & y
);
4412 Display
*display
= windowAtPtr ?
GDK_WINDOW_XDISPLAY(windowAtPtr
) : GDK_DISPLAY();
4413 Window rootWindow
= RootWindowOfScreen (DefaultScreenOfDisplay(display
));
4414 Window rootReturn
, childReturn
;
4415 int rootX
, rootY
, winX
, winY
;
4416 unsigned int maskReturn
;
4418 XQueryPointer (display
,
4422 &rootX
, &rootY
, &winX
, &winY
, &maskReturn
);
4423 return wxPoint(rootX
, rootY
);
4427 GdkWindow
* wxWindowGTK
::GTKGetDrawingWindow() const
4429 GdkWindow
* window
= NULL
;
4431 window
= gtk_widget_get_window(m_wxwindow
);
4435 // ----------------------------------------------------------------------------
4437 // ----------------------------------------------------------------------------
4442 // this is called if we attempted to freeze unrealized widget when it finally
4443 // is realized (and so can be frozen):
4444 static void wx_frozen_widget_realize(GtkWidget
* w
, wxWindowGTK
* win
)
4446 wxASSERT( w
&& gtk_widget_get_has_window(w
) );
4447 wxASSERT( gtk_widget_get_realized(w
) );
4449 g_signal_handlers_disconnect_by_func
4452 (void*)wx_frozen_widget_realize
,
4457 if (w
== win
->m_wxwindow
)
4458 window
= win
->GTKGetDrawingWindow();
4460 window
= gtk_widget_get_window(w
);
4461 gdk_window_freeze_updates(window
);
4466 void wxWindowGTK
::GTKFreezeWidget(GtkWidget
*w
)
4468 if ( !w
|| !gtk_widget_get_has_window(w
) )
4469 return; // window-less widget, cannot be frozen
4471 GdkWindow
* window
= gtk_widget_get_window(w
);
4474 // we can't thaw unrealized widgets because they don't have GdkWindow,
4475 // so set it up to be done immediately after realization:
4476 g_signal_connect_after
4480 G_CALLBACK(wx_frozen_widget_realize
),
4486 if (w
== m_wxwindow
)
4487 window
= GTKGetDrawingWindow();
4488 gdk_window_freeze_updates(window
);
4491 void wxWindowGTK
::GTKThawWidget(GtkWidget
*w
)
4493 if ( !w
|| !gtk_widget_get_has_window(w
) )
4494 return; // window-less widget, cannot be frozen
4496 GdkWindow
* window
= gtk_widget_get_window(w
);
4499 // the widget wasn't realized yet, no need to thaw
4500 g_signal_handlers_disconnect_by_func
4503 (void*)wx_frozen_widget_realize
,
4509 if (w
== m_wxwindow
)
4510 window
= GTKGetDrawingWindow();
4511 gdk_window_thaw_updates(window
);
4514 void wxWindowGTK
::DoFreeze()
4516 GTKFreezeWidget(m_widget
);
4517 if ( m_wxwindow
&& m_widget
!= m_wxwindow
)
4518 GTKFreezeWidget(m_wxwindow
);
4521 void wxWindowGTK
::DoThaw()
4523 GTKThawWidget(m_widget
);
4524 if ( m_wxwindow
&& m_widget
!= m_wxwindow
)
4525 GTKThawWidget(m_wxwindow
);