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/sysopt.h"
38 #include "wx/gtk/private.h"
39 #include "wx/gtk/private/win_gtk.h"
40 #include "wx/gtk/private/event.h"
41 using namespace wxGTKImpl
;
43 #ifdef GDK_WINDOWING_X11
49 #include <gdk/gdkkeysyms.h>
50 #if GTK_CHECK_VERSION(3,0,0)
51 #include <gdk/gdkkeysyms-compat.h>
54 // gdk_window_set_composited() is only supported since 2.12
55 #define wxGTK_VERSION_REQUIRED_FOR_COMPOSITING 2,12,0
56 #define wxGTK_HAS_COMPOSITING_SUPPORT GTK_CHECK_VERSION(2,12,0)
58 //-----------------------------------------------------------------------------
59 // documentation on internals
60 //-----------------------------------------------------------------------------
63 I have been asked several times about writing some documentation about
64 the GTK port of wxWidgets, especially its internal structures. Obviously,
65 you cannot understand wxGTK without knowing a little about the GTK, but
66 some more information about what the wxWindow, which is the base class
67 for all other window classes, does seems required as well.
71 What does wxWindow do? It contains the common interface for the following
72 jobs of its descendants:
74 1) Define the rudimentary behaviour common to all window classes, such as
75 resizing, intercepting user input (so as to make it possible to use these
76 events for special purposes in a derived class), window names etc.
78 2) Provide the possibility to contain and manage children, if the derived
79 class is allowed to contain children, which holds true for those window
80 classes which do not display a native GTK widget. To name them, these
81 classes are wxPanel, wxScrolledWindow, wxDialog, wxFrame. The MDI frame-
82 work classes are a special case and are handled a bit differently from
83 the rest. The same holds true for the wxNotebook class.
85 3) Provide the possibility to draw into a client area of a window. This,
86 too, only holds true for classes that do not display a native GTK widget
89 4) Provide the entire mechanism for scrolling widgets. This actual inter-
90 face for this is usually in wxScrolledWindow, but the GTK implementation
93 5) A multitude of helper or extra methods for special purposes, such as
94 Drag'n'Drop, managing validators etc.
96 6) Display a border (sunken, raised, simple or none).
98 Normally one might expect, that one wxWidgets window would always correspond
99 to one GTK widget. Under GTK, there is no such all-round widget that has all
100 the functionality. Moreover, the GTK defines a client area as a different
101 widget from the actual widget you are handling. Last but not least some
102 special classes (e.g. wxFrame) handle different categories of widgets and
103 still have the possibility to draw something in the client area.
104 It was therefore required to write a special purpose GTK widget, that would
105 represent a client area in the sense of wxWidgets capable to do the jobs
106 2), 3) and 4). I have written this class and it resides in win_gtk.c of
109 All windows must have a widget, with which they interact with other under-
110 lying GTK widgets. It is this widget, e.g. that has to be resized etc and
111 the wxWindow class has a member variable called m_widget which holds a
112 pointer to this widget. When the window class represents a GTK native widget,
113 this is (in most cases) the only GTK widget the class manages. E.g. the
114 wxStaticText class handles only a GtkLabel widget a pointer to which you
115 can find in m_widget (defined in wxWindow)
117 When the class has a client area for drawing into and for containing children
118 it has to handle the client area widget (of the type wxPizza, defined in
119 win_gtk.cpp), but there could be any number of widgets, handled by a class.
120 The common rule for all windows is only, that the widget that interacts with
121 the rest of GTK must be referenced in m_widget and all other widgets must be
122 children of this widget on the GTK level. The top-most widget, which also
123 represents the client area, must be in the m_wxwindow field and must be of
126 As I said, the window classes that display a GTK native widget only have
127 one widget, so in the case of e.g. the wxButton class m_widget holds a
128 pointer to a GtkButton widget. But windows with client areas (for drawing
129 and children) have a m_widget field that is a pointer to a GtkScrolled-
130 Window and a m_wxwindow field that is pointer to a wxPizza and this
131 one is (in the GTK sense) a child of the GtkScrolledWindow.
133 If the m_wxwindow field is set, then all input to this widget is inter-
134 cepted and sent to the wxWidgets class. If not, all input to the widget
135 that gets pointed to by m_widget gets intercepted and sent to the class.
139 The design of scrolling in wxWidgets is markedly different from that offered
140 by the GTK itself and therefore we cannot simply take it as it is. In GTK,
141 clicking on a scrollbar belonging to scrolled window will inevitably move
142 the window. In wxWidgets, the scrollbar will only emit an event, send this
143 to (normally) a wxScrolledWindow and that class will call ScrollWindow()
144 which actually moves the window and its sub-windows. Note that wxPizza
145 memorizes how much it has been scrolled but that wxWidgets forgets this
146 so that the two coordinates systems have to be kept in synch. This is done
147 in various places using the pizza->m_scroll_x and pizza->m_scroll_y values.
151 Singularly the most broken code in GTK is the code that is supposed to
152 inform subwindows (child windows) about new positions. Very often, duplicate
153 events are sent without changes in size or position, equally often no
154 events are sent at all (All this is due to a bug in the GtkContainer code
155 which got fixed in GTK 1.2.6). For that reason, wxGTK completely ignores
156 GTK's own system and it simply waits for size events for toplevel windows
157 and then iterates down the respective size events to all window. This has
158 the disadvantage that windows might get size events before the GTK widget
159 actually has the reported size. This doesn't normally pose any problem, but
160 the OpenGL drawing routines rely on correct behaviour. Therefore, I have
161 added the m_nativeSizeEvents flag, which is true only for the OpenGL canvas,
162 i.e. the wxGLCanvas will emit a size event, when (and not before) the X11
163 window that is used for OpenGL output really has that size (as reported by
168 If someone at some point of time feels the immense desire to have a look at,
169 change or attempt to optimise the Refresh() logic, this person will need an
170 intimate understanding of what "draw" and "expose" events are and what
171 they are used for, in particular when used in connection with GTK's
172 own windowless widgets. Beware.
176 Cursors, too, have been a constant source of pleasure. The main difficulty
177 is that a GdkWindow inherits a cursor if the programmer sets a new cursor
178 for the parent. To prevent this from doing too much harm, SetCursor calls
179 GTKUpdateCursor, which will recursively re-set the cursors of all child windows.
180 Also don't forget that cursors (like much else) are connected to GdkWindows,
181 not GtkWidgets and that the "window" field of a GtkWidget might very well
182 point to the GdkWindow of the parent widget (-> "window-less widget") and
183 that the two obviously have very different meanings.
186 //-----------------------------------------------------------------------------
188 //-----------------------------------------------------------------------------
190 // Don't allow event propagation during drag
191 bool g_blockEventsOnDrag
;
192 // Don't allow mouse event propagation during scroll
193 bool g_blockEventsOnScroll
;
194 extern wxCursor g_globalCursor
;
196 // mouse capture state: the window which has it and if the mouse is currently
198 static wxWindowGTK
*g_captureWindow
= NULL
;
199 static bool g_captureWindowHasMouse
= false;
201 // The window that currently has focus:
202 static wxWindowGTK
*gs_currentFocus
= NULL
;
203 // The window that is scheduled to get focus in the next event loop iteration
204 // or NULL if there's no pending focus change:
205 static wxWindowGTK
*gs_pendingFocus
= NULL
;
207 // the window that has deferred focus-out event pending, if any (see
208 // GTKAddDeferredFocusOut() for details)
209 static wxWindowGTK
*gs_deferredFocusOut
= NULL
;
211 // global variables because GTK+ DnD want to have the
212 // mouse event that caused it
213 GdkEvent
*g_lastMouseEvent
= NULL
;
214 int g_lastButtonNumber
= 0;
216 //-----------------------------------------------------------------------------
218 //-----------------------------------------------------------------------------
220 // the trace mask used for the focus debugging messages
221 #define TRACE_FOCUS wxT("focus")
223 //-----------------------------------------------------------------------------
224 // "size_request" of m_widget
225 //-----------------------------------------------------------------------------
229 wxgtk_window_size_request_callback(GtkWidget
* WXUNUSED(widget
),
230 GtkRequisition
*requisition
,
234 win
->GetSize( &w
, &h
);
240 requisition
->height
= h
;
241 requisition
->width
= w
;
245 //-----------------------------------------------------------------------------
246 // "expose_event" of m_wxwindow
247 //-----------------------------------------------------------------------------
251 gtk_window_expose_callback( GtkWidget
*,
252 GdkEventExpose
*gdk_event
,
255 if (gdk_event
->window
== win
->GTKGetDrawingWindow())
257 win
->GetUpdateRegion() = wxRegion( gdk_event
->region
);
258 win
->GtkSendPaintEvents();
260 // Let parent window draw window-less widgets
265 #ifndef __WXUNIVERSAL__
266 //-----------------------------------------------------------------------------
267 // "expose_event" from m_wxwindow->parent, for drawing border
268 //-----------------------------------------------------------------------------
272 expose_event_border(GtkWidget
* widget
, GdkEventExpose
* gdk_event
, wxWindow
* win
)
274 if (gdk_event
->window
!= gtk_widget_get_parent_window(win
->m_wxwindow
))
281 gtk_widget_get_allocation(win
->m_wxwindow
, &alloc
);
282 const int x
= alloc
.x
;
283 const int y
= alloc
.y
;
284 const int w
= alloc
.width
;
285 const int h
= alloc
.height
;
287 if (w
<= 0 || h
<= 0)
290 if (win
->HasFlag(wxBORDER_SIMPLE
))
292 gdk_draw_rectangle(gdk_event
->window
,
293 gtk_widget_get_style(widget
)->black_gc
, false, x
, y
, w
- 1, h
- 1);
297 GtkShadowType shadow
= GTK_SHADOW_IN
;
298 if (win
->HasFlag(wxBORDER_RAISED
))
299 shadow
= GTK_SHADOW_OUT
;
301 // Style detail to use
303 if (win
->m_widget
== win
->m_wxwindow
)
304 // for non-scrollable wxWindows
307 // for scrollable ones
310 // clip rect is required to avoid painting background
311 // over upper left (w,h) of parent window
312 GdkRectangle clipRect
= { x
, y
, w
, h
};
314 gtk_widget_get_style(win
->m_wxwindow
), gdk_event
->window
, GTK_STATE_NORMAL
,
315 shadow
, &clipRect
, wxGTKPrivate::GetEntryWidget(), detail
, x
, y
, w
, h
);
321 //-----------------------------------------------------------------------------
322 // "parent_set" from m_wxwindow
323 //-----------------------------------------------------------------------------
327 parent_set(GtkWidget
* widget
, GtkWidget
* old_parent
, wxWindow
* win
)
331 g_signal_handlers_disconnect_by_func(
332 old_parent
, (void*)expose_event_border
, win
);
334 GtkWidget
* parent
= gtk_widget_get_parent(widget
);
337 g_signal_connect_after(parent
, "expose_event",
338 G_CALLBACK(expose_event_border
), win
);
342 #endif // !__WXUNIVERSAL__
344 //-----------------------------------------------------------------------------
345 // "key_press_event" from any window
346 //-----------------------------------------------------------------------------
348 // set WXTRACE to this to see the key event codes on the console
349 #define TRACE_KEYS wxT("keyevent")
351 // translates an X key symbol to WXK_XXX value
353 // if isChar is true it means that the value returned will be used for EVT_CHAR
354 // event and then we choose the logical WXK_XXX, i.e. '/' for GDK_KP_Divide,
355 // for example, while if it is false it means that the value is going to be
356 // used for KEY_DOWN/UP events and then we translate GDK_KP_Divide to
358 static long wxTranslateKeySymToWXKey(KeySym keysym
, bool isChar
)
364 // Shift, Control and Alt don't generate the CHAR events at all
367 key_code
= isChar
? 0 : WXK_SHIFT
;
371 key_code
= isChar
? 0 : WXK_CONTROL
;
379 key_code
= isChar
? 0 : WXK_ALT
;
382 // neither do the toggle modifies
383 case GDK_Scroll_Lock
:
384 key_code
= isChar
? 0 : WXK_SCROLL
;
388 key_code
= isChar
? 0 : WXK_CAPITAL
;
392 key_code
= isChar
? 0 : WXK_NUMLOCK
;
396 // various other special keys
409 case GDK_ISO_Left_Tab
:
416 key_code
= WXK_RETURN
;
420 key_code
= WXK_CLEAR
;
424 key_code
= WXK_PAUSE
;
428 key_code
= WXK_SELECT
;
432 key_code
= WXK_PRINT
;
436 key_code
= WXK_EXECUTE
;
440 key_code
= WXK_ESCAPE
;
443 // cursor and other extended keyboard keys
445 key_code
= WXK_DELETE
;
461 key_code
= WXK_RIGHT
;
468 case GDK_Prior
: // == GDK_Page_Up
469 key_code
= WXK_PAGEUP
;
472 case GDK_Next
: // == GDK_Page_Down
473 key_code
= WXK_PAGEDOWN
;
485 key_code
= WXK_INSERT
;
500 key_code
= (isChar
? '0' : int(WXK_NUMPAD0
)) + keysym
- GDK_KP_0
;
504 key_code
= isChar
? ' ' : int(WXK_NUMPAD_SPACE
);
508 key_code
= isChar
? WXK_TAB
: WXK_NUMPAD_TAB
;
512 key_code
= isChar
? WXK_RETURN
: WXK_NUMPAD_ENTER
;
516 key_code
= isChar
? WXK_F1
: WXK_NUMPAD_F1
;
520 key_code
= isChar
? WXK_F2
: WXK_NUMPAD_F2
;
524 key_code
= isChar
? WXK_F3
: WXK_NUMPAD_F3
;
528 key_code
= isChar
? WXK_F4
: WXK_NUMPAD_F4
;
532 key_code
= isChar
? WXK_HOME
: WXK_NUMPAD_HOME
;
536 key_code
= isChar
? WXK_LEFT
: WXK_NUMPAD_LEFT
;
540 key_code
= isChar
? WXK_UP
: WXK_NUMPAD_UP
;
544 key_code
= isChar
? WXK_RIGHT
: WXK_NUMPAD_RIGHT
;
548 key_code
= isChar
? WXK_DOWN
: WXK_NUMPAD_DOWN
;
551 case GDK_KP_Prior
: // == GDK_KP_Page_Up
552 key_code
= isChar
? WXK_PAGEUP
: WXK_NUMPAD_PAGEUP
;
555 case GDK_KP_Next
: // == GDK_KP_Page_Down
556 key_code
= isChar
? WXK_PAGEDOWN
: WXK_NUMPAD_PAGEDOWN
;
560 key_code
= isChar
? WXK_END
: WXK_NUMPAD_END
;
564 key_code
= isChar
? WXK_HOME
: WXK_NUMPAD_BEGIN
;
568 key_code
= isChar
? WXK_INSERT
: WXK_NUMPAD_INSERT
;
572 key_code
= isChar
? WXK_DELETE
: WXK_NUMPAD_DELETE
;
576 key_code
= isChar
? '=' : int(WXK_NUMPAD_EQUAL
);
579 case GDK_KP_Multiply
:
580 key_code
= isChar
? '*' : int(WXK_NUMPAD_MULTIPLY
);
584 key_code
= isChar
? '+' : int(WXK_NUMPAD_ADD
);
587 case GDK_KP_Separator
:
588 // FIXME: what is this?
589 key_code
= isChar
? '.' : int(WXK_NUMPAD_SEPARATOR
);
592 case GDK_KP_Subtract
:
593 key_code
= isChar
? '-' : int(WXK_NUMPAD_SUBTRACT
);
597 key_code
= isChar
? '.' : int(WXK_NUMPAD_DECIMAL
);
601 key_code
= isChar
? '/' : int(WXK_NUMPAD_DIVIDE
);
618 key_code
= WXK_F1
+ keysym
- GDK_F1
;
628 static inline bool wxIsAsciiKeysym(KeySym ks
)
633 static void wxFillOtherKeyEventFields(wxKeyEvent
& event
,
635 GdkEventKey
*gdk_event
)
637 event
.SetTimestamp( gdk_event
->time
);
638 event
.SetId(win
->GetId());
640 event
.m_shiftDown
= (gdk_event
->state
& GDK_SHIFT_MASK
) != 0;
641 event
.m_controlDown
= (gdk_event
->state
& GDK_CONTROL_MASK
) != 0;
642 event
.m_altDown
= (gdk_event
->state
& GDK_MOD1_MASK
) != 0;
643 event
.m_metaDown
= (gdk_event
->state
& GDK_META_MASK
) != 0;
645 // Normally we take the state of modifiers directly from the low level GDK
646 // event but unfortunately GDK uses a different convention from MSW for the
647 // key events corresponding to the modifier keys themselves: in it, when
648 // e.g. Shift key is pressed, GDK_SHIFT_MASK is not set while it is set
649 // when Shift is released. Under MSW the situation is exactly reversed and
650 // the modifier corresponding to the key is set when it is pressed and
651 // unset when it is released. To ensure consistent behaviour between
652 // platforms (and because it seems to make slightly more sense, although
653 // arguably both behaviours are reasonable) we follow MSW here.
655 // Final notice: we set the flags to the desired value instead of just
656 // inverting them because they are not set correctly (i.e. in the same way
657 // as for the real events generated by the user) for wxUIActionSimulator-
658 // produced events and it seems better to keep that class code the same
659 // among all platforms and fix the discrepancy here instead of adding
660 // wxGTK-specific code to wxUIActionSimulator.
661 const bool isPress
= gdk_event
->type
== GDK_KEY_PRESS
;
662 switch ( gdk_event
->keyval
)
666 event
.m_shiftDown
= isPress
;
671 event
.m_controlDown
= isPress
;
676 event
.m_altDown
= isPress
;
683 event
.m_metaDown
= isPress
;
687 event
.m_rawCode
= (wxUint32
) gdk_event
->keyval
;
688 event
.m_rawFlags
= gdk_event
->hardware_keycode
;
690 wxGetMousePosition(&event
.m_x
, &event
.m_y
);
691 win
->ScreenToClient(&event
.m_x
, &event
.m_y
);
692 event
.SetEventObject( win
);
697 wxTranslateGTKKeyEventToWx(wxKeyEvent
& event
,
699 GdkEventKey
*gdk_event
)
701 // VZ: it seems that GDK_KEY_RELEASE event doesn't set event->string
702 // but only event->keyval which is quite useless to us, so remember
703 // the last character from GDK_KEY_PRESS and reuse it as last resort
705 // NB: should be MT-safe as we're always called from the main thread only
710 } s_lastKeyPress
= { 0, 0 };
712 KeySym keysym
= gdk_event
->keyval
;
714 wxLogTrace(TRACE_KEYS
, wxT("Key %s event: keysym = %ld"),
715 event
.GetEventType() == wxEVT_KEY_UP
? wxT("release")
719 long key_code
= wxTranslateKeySymToWXKey(keysym
, false /* !isChar */);
723 // do we have the translation or is it a plain ASCII character?
724 if ( (gdk_event
->length
== 1) || wxIsAsciiKeysym(keysym
) )
726 // we should use keysym if it is ASCII as X does some translations
727 // like "I pressed while Control is down" => "Ctrl-I" == "TAB"
728 // which we don't want here (but which we do use for OnChar())
729 if ( !wxIsAsciiKeysym(keysym
) )
731 keysym
= (KeySym
)gdk_event
->string
[0];
734 #ifdef GDK_WINDOWING_X11
735 // we want to always get the same key code when the same key is
736 // pressed regardless of the state of the modifiers, i.e. on a
737 // standard US keyboard pressing '5' or '%' ('5' key with
738 // Shift) should result in the same key code in OnKeyDown():
739 // '5' (although OnChar() will get either '5' or '%').
741 // to do it we first translate keysym to keycode (== scan code)
742 // and then back but always using the lower register
743 Display
*dpy
= (Display
*)wxGetDisplay();
744 KeyCode keycode
= XKeysymToKeycode(dpy
, keysym
);
746 wxLogTrace(TRACE_KEYS
, wxT("\t-> keycode %d"), keycode
);
748 KeySym keysymNormalized
= XKeycodeToKeysym(dpy
, keycode
, 0);
750 // use the normalized, i.e. lower register, keysym if we've
752 key_code
= keysymNormalized
? keysymNormalized
: keysym
;
757 // as explained above, we want to have lower register key codes
758 // normally but for the letter keys we want to have the upper ones
760 // NB: don't use XConvertCase() here, we want to do it for letters
762 key_code
= toupper(key_code
);
764 else // non ASCII key, what to do?
766 // by default, ignore it
769 // but if we have cached information from the last KEY_PRESS
770 if ( gdk_event
->type
== GDK_KEY_RELEASE
)
773 if ( keysym
== s_lastKeyPress
.keysym
)
775 key_code
= s_lastKeyPress
.keycode
;
780 if ( gdk_event
->type
== GDK_KEY_PRESS
)
782 // remember it to be reused for KEY_UP event later
783 s_lastKeyPress
.keysym
= keysym
;
784 s_lastKeyPress
.keycode
= key_code
;
788 wxLogTrace(TRACE_KEYS
, wxT("\t-> wxKeyCode %ld"), key_code
);
790 // sending unknown key events doesn't really make sense
794 event
.m_keyCode
= key_code
;
797 event
.m_uniChar
= gdk_keyval_to_unicode(key_code
? key_code
: keysym
);
798 if ( !event
.m_uniChar
&& event
.m_keyCode
<= WXK_DELETE
)
800 // Set Unicode key code to the ASCII equivalent for compatibility. E.g.
801 // let RETURN generate the key event with both key and Unicode key
803 event
.m_uniChar
= event
.m_keyCode
;
805 #endif // wxUSE_UNICODE
807 // now fill all the other fields
808 wxFillOtherKeyEventFields(event
, win
, gdk_event
);
816 GtkIMContext
*context
;
817 GdkEventKey
*lastKeyEvent
;
821 context
= gtk_im_multicontext_new();
826 g_object_unref (context
);
833 // Send wxEVT_CHAR_HOOK event to the parent of the window and return true only
834 // if it was processed (and not skipped).
835 bool SendCharHookEvent(const wxKeyEvent
& event
, wxWindow
*win
)
837 // wxEVT_CHAR_HOOK must be sent to allow the parent windows (e.g. a dialog
838 // which typically closes when Esc key is pressed in any of its controls)
839 // to handle key events in all of its children unless the mouse is captured
840 // in which case we consider that the keyboard should be "captured" too.
841 if ( !g_captureWindow
)
843 wxKeyEvent
eventCharHook(wxEVT_CHAR_HOOK
, event
);
844 if ( win
->HandleWindowEvent(eventCharHook
)
845 && !event
.IsNextEventAllowed() )
852 // Adjust wxEVT_CHAR event key code fields. This function takes care of two
854 // (a) Ctrl-letter key presses generate key codes in range 1..26
855 // (b) Unicode key codes are same as key codes for the codes in 1..255 range
856 void AdjustCharEventKeyCodes(wxKeyEvent
& event
)
858 const int code
= event
.m_keyCode
;
860 // Check for (a) above.
861 if ( event
.ControlDown() )
863 // We intentionally don't use isupper/lower() here, we really need
864 // ASCII letters only as it doesn't make sense to translate any other
865 // ones into this range which has only 26 slots.
866 if ( code
>= 'a' && code
<= 'z' )
867 event
.m_keyCode
= code
- 'a' + 1;
868 else if ( code
>= 'A' && code
<= 'Z' )
869 event
.m_keyCode
= code
- 'A' + 1;
872 // Adjust the Unicode equivalent in the same way too.
873 if ( event
.m_keyCode
!= code
)
874 event
.m_uniChar
= event
.m_keyCode
;
875 #endif // wxUSE_UNICODE
879 // Check for (b) from above.
881 // FIXME: Should we do it for key codes up to 255?
882 if ( !event
.m_uniChar
&& code
< WXK_DELETE
)
883 event
.m_uniChar
= code
;
884 #endif // wxUSE_UNICODE
887 } // anonymous namespace
889 // If a widget does not handle a key or mouse event, GTK+ sends it up the
890 // parent chain until it is handled. These events are not supposed to propagate
891 // in wxWidgets, so this code avoids handling them in any parent wxWindow,
892 // while still allowing the event to propagate so things like native keyboard
893 // navigation will work.
894 #define wxPROCESS_EVENT_ONCE(EventType, event) \
895 static EventType eventPrev; \
896 if (memcmp(&eventPrev, event, sizeof(EventType)) == 0) \
902 gtk_window_key_press_callback( GtkWidget
*WXUNUSED(widget
),
903 GdkEventKey
*gdk_event
,
908 if (g_blockEventsOnDrag
)
911 wxPROCESS_EVENT_ONCE(GdkEventKey
, gdk_event
);
913 wxKeyEvent
event( wxEVT_KEY_DOWN
);
915 bool return_after_IM
= false;
917 if( wxTranslateGTKKeyEventToWx(event
, win
, gdk_event
) )
919 // Send the CHAR_HOOK event first
920 if ( SendCharHookEvent(event
, win
) )
922 // Don't do anything at all with this event any more.
926 // Emit KEY_DOWN event
927 ret
= win
->HandleWindowEvent( event
);
931 // Return after IM processing as we cannot do
932 // anything with it anyhow.
933 return_after_IM
= true;
936 if (!ret
&& win
->m_imData
)
938 win
->m_imData
->lastKeyEvent
= gdk_event
;
940 // We should let GTK+ IM filter key event first. According to GTK+ 2.0 API
941 // docs, if IM filter returns true, no further processing should be done.
942 // we should send the key_down event anyway.
943 bool intercepted_by_IM
= gtk_im_context_filter_keypress(win
->m_imData
->context
, gdk_event
);
944 win
->m_imData
->lastKeyEvent
= NULL
;
945 if (intercepted_by_IM
)
947 wxLogTrace(TRACE_KEYS
, wxT("Key event intercepted by IM"));
958 wxWindowGTK
*ancestor
= win
;
961 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
964 wxCommandEvent
menu_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
965 ret
= ancestor
->HandleWindowEvent( menu_event
);
969 // if the accelerator wasn't handled as menu event, try
970 // it as button click (for compatibility with other
972 wxCommandEvent
button_event( wxEVT_COMMAND_BUTTON_CLICKED
, command
);
973 ret
= ancestor
->HandleWindowEvent( button_event
);
978 if (ancestor
->IsTopLevel())
980 ancestor
= ancestor
->GetParent();
983 #endif // wxUSE_ACCEL
985 // Only send wxEVT_CHAR event if not processed yet. Thus, ALT-x
986 // will only be sent if it is not in an accelerator table.
990 KeySym keysym
= gdk_event
->keyval
;
991 // Find key code for EVT_CHAR and EVT_CHAR_HOOK events
992 key_code
= wxTranslateKeySymToWXKey(keysym
, true /* isChar */);
995 if ( wxIsAsciiKeysym(keysym
) )
998 key_code
= (unsigned char)keysym
;
1000 // gdk_event->string is actually deprecated
1001 else if ( gdk_event
->length
== 1 )
1003 key_code
= (unsigned char)gdk_event
->string
[0];
1009 wxKeyEvent
eventChar(wxEVT_CHAR
, event
);
1011 wxLogTrace(TRACE_KEYS
, wxT("Char event: %ld"), key_code
);
1013 eventChar
.m_keyCode
= key_code
;
1015 AdjustCharEventKeyCodes(eventChar
);
1017 ret
= win
->HandleWindowEvent(eventChar
);
1027 gtk_wxwindow_commit_cb (GtkIMContext
* WXUNUSED(context
),
1031 wxKeyEvent
event( wxEVT_CHAR
);
1033 // take modifiers, cursor position, timestamp etc. from the last
1034 // key_press_event that was fed into Input Method:
1035 if (window
->m_imData
->lastKeyEvent
)
1037 wxFillOtherKeyEventFields(event
,
1038 window
, window
->m_imData
->lastKeyEvent
);
1042 event
.SetEventObject( window
);
1045 const wxString
data(wxGTK_CONV_BACK_SYS(str
));
1049 for( wxString::const_iterator pstr
= data
.begin(); pstr
!= data
.end(); ++pstr
)
1052 event
.m_uniChar
= *pstr
;
1053 // Backward compatible for ISO-8859-1
1054 event
.m_keyCode
= *pstr
< 256 ? event
.m_uniChar
: 0;
1055 wxLogTrace(TRACE_KEYS
, wxT("IM sent character '%c'"), event
.m_uniChar
);
1057 event
.m_keyCode
= (char)*pstr
;
1058 #endif // wxUSE_UNICODE
1060 AdjustCharEventKeyCodes(event
);
1062 window
->HandleWindowEvent(event
);
1068 //-----------------------------------------------------------------------------
1069 // "key_release_event" from any window
1070 //-----------------------------------------------------------------------------
1074 gtk_window_key_release_callback( GtkWidget
* WXUNUSED(widget
),
1075 GdkEventKey
*gdk_event
,
1081 if (g_blockEventsOnDrag
)
1084 wxPROCESS_EVENT_ONCE(GdkEventKey
, gdk_event
);
1086 wxKeyEvent
event( wxEVT_KEY_UP
);
1087 if ( !wxTranslateGTKKeyEventToWx(event
, win
, gdk_event
) )
1089 // unknown key pressed, ignore (the event would be useless anyhow)
1093 return win
->GTKProcessEvent(event
);
1097 // ============================================================================
1099 // ============================================================================
1101 // ----------------------------------------------------------------------------
1102 // mouse event processing helpers
1103 // ----------------------------------------------------------------------------
1105 static void AdjustEventButtonState(wxMouseEvent
& event
)
1107 // GDK reports the old state of the button for a button press event, but
1108 // for compatibility with MSW and common sense we want m_leftDown be TRUE
1109 // for a LEFT_DOWN event, not FALSE, so we will invert
1110 // left/right/middleDown for the corresponding click events
1112 if ((event
.GetEventType() == wxEVT_LEFT_DOWN
) ||
1113 (event
.GetEventType() == wxEVT_LEFT_DCLICK
) ||
1114 (event
.GetEventType() == wxEVT_LEFT_UP
))
1116 event
.m_leftDown
= !event
.m_leftDown
;
1120 if ((event
.GetEventType() == wxEVT_MIDDLE_DOWN
) ||
1121 (event
.GetEventType() == wxEVT_MIDDLE_DCLICK
) ||
1122 (event
.GetEventType() == wxEVT_MIDDLE_UP
))
1124 event
.m_middleDown
= !event
.m_middleDown
;
1128 if ((event
.GetEventType() == wxEVT_RIGHT_DOWN
) ||
1129 (event
.GetEventType() == wxEVT_RIGHT_DCLICK
) ||
1130 (event
.GetEventType() == wxEVT_RIGHT_UP
))
1132 event
.m_rightDown
= !event
.m_rightDown
;
1136 if ((event
.GetEventType() == wxEVT_AUX1_DOWN
) ||
1137 (event
.GetEventType() == wxEVT_AUX1_DCLICK
))
1139 event
.m_aux1Down
= true;
1143 if ((event
.GetEventType() == wxEVT_AUX2_DOWN
) ||
1144 (event
.GetEventType() == wxEVT_AUX2_DCLICK
))
1146 event
.m_aux2Down
= true;
1151 // find the window to send the mouse event to
1153 wxWindowGTK
*FindWindowForMouseEvent(wxWindowGTK
*win
, wxCoord
& x
, wxCoord
& y
)
1158 if (win
->m_wxwindow
)
1160 wxPizza
* pizza
= WX_PIZZA(win
->m_wxwindow
);
1161 xx
+= pizza
->m_scroll_x
;
1162 yy
+= pizza
->m_scroll_y
;
1165 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
1168 wxWindow
* child
= static_cast<wxWindow
*>(node
->GetData());
1170 node
= node
->GetNext();
1171 if (!child
->IsShown())
1174 if (child
->GTKIsTransparentForMouse())
1176 // wxStaticBox is transparent in the box itself
1177 int xx1
= child
->m_x
;
1178 int yy1
= child
->m_y
;
1179 int xx2
= child
->m_x
+ child
->m_width
;
1180 int yy2
= child
->m_y
+ child
->m_height
;
1183 if (((xx
>= xx1
) && (xx
<= xx1
+10) && (yy
>= yy1
) && (yy
<= yy2
)) ||
1185 ((xx
>= xx2
-10) && (xx
<= xx2
) && (yy
>= yy1
) && (yy
<= yy2
)) ||
1187 ((xx
>= xx1
) && (xx
<= xx2
) && (yy
>= yy1
) && (yy
<= yy1
+10)) ||
1189 ((xx
>= xx1
) && (xx
<= xx2
) && (yy
>= yy2
-1) && (yy
<= yy2
)))
1200 if ((child
->m_wxwindow
== NULL
) &&
1201 win
->IsClientAreaChild(child
) &&
1202 (child
->m_x
<= xx
) &&
1203 (child
->m_y
<= yy
) &&
1204 (child
->m_x
+child
->m_width
>= xx
) &&
1205 (child
->m_y
+child
->m_height
>= yy
))
1218 // ----------------------------------------------------------------------------
1219 // common event handlers helpers
1220 // ----------------------------------------------------------------------------
1222 bool wxWindowGTK::GTKProcessEvent(wxEvent
& event
) const
1224 // nothing special at this level
1225 return HandleWindowEvent(event
);
1228 bool wxWindowGTK::GTKShouldIgnoreEvent() const
1230 return !m_hasVMT
|| g_blockEventsOnDrag
;
1233 int wxWindowGTK::GTKCallbackCommonPrologue(GdkEventAny
*event
) const
1237 if (g_blockEventsOnDrag
)
1239 if (g_blockEventsOnScroll
)
1242 if (!GTKIsOwnWindow(event
->window
))
1248 // overloads for all GDK event types we use here: we need to have this as
1249 // GdkEventXXX can't be implicitly cast to GdkEventAny even if it, in fact,
1250 // derives from it in the sense that the structs have the same layout
1251 #define wxDEFINE_COMMON_PROLOGUE_OVERLOAD(T) \
1252 static int wxGtkCallbackCommonPrologue(T *event, wxWindowGTK *win) \
1254 return win->GTKCallbackCommonPrologue((GdkEventAny *)event); \
1257 wxDEFINE_COMMON_PROLOGUE_OVERLOAD(GdkEventButton
)
1258 wxDEFINE_COMMON_PROLOGUE_OVERLOAD(GdkEventMotion
)
1259 wxDEFINE_COMMON_PROLOGUE_OVERLOAD(GdkEventCrossing
)
1261 #undef wxDEFINE_COMMON_PROLOGUE_OVERLOAD
1263 #define wxCOMMON_CALLBACK_PROLOGUE(event, win) \
1264 const int rc = wxGtkCallbackCommonPrologue(event, win); \
1268 // all event handlers must have C linkage as they're called from GTK+ C code
1272 //-----------------------------------------------------------------------------
1273 // "button_press_event"
1274 //-----------------------------------------------------------------------------
1277 gtk_window_button_press_callback( GtkWidget
*widget
,
1278 GdkEventButton
*gdk_event
,
1281 wxPROCESS_EVENT_ONCE(GdkEventButton
, gdk_event
);
1283 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1285 g_lastButtonNumber
= gdk_event
->button
;
1287 // GDK sends surplus button down events
1288 // before a double click event. We
1289 // need to filter these out.
1290 if ((gdk_event
->type
== GDK_BUTTON_PRESS
) && (win
->m_wxwindow
))
1292 GdkEvent
*peek_event
= gdk_event_peek();
1295 if ((peek_event
->type
== GDK_2BUTTON_PRESS
) ||
1296 (peek_event
->type
== GDK_3BUTTON_PRESS
))
1298 gdk_event_free( peek_event
);
1303 gdk_event_free( peek_event
);
1308 wxEventType event_type
= wxEVT_NULL
;
1310 if ( gdk_event
->type
== GDK_2BUTTON_PRESS
&&
1311 gdk_event
->button
>= 1 && gdk_event
->button
<= 3 )
1313 // Reset GDK internal timestamp variables in order to disable GDK
1314 // triple click events. GDK will then next time believe no button has
1315 // been clicked just before, and send a normal button click event.
1316 GdkDisplay
* display
= gtk_widget_get_display (widget
);
1317 display
->button_click_time
[1] = 0;
1318 display
->button_click_time
[0] = 0;
1321 if (gdk_event
->button
== 1)
1323 // note that GDK generates triple click events which are not supported
1324 // by wxWidgets but still have to be passed to the app as otherwise
1325 // clicks would simply go missing
1326 switch (gdk_event
->type
)
1328 // we shouldn't get triple clicks at all for GTK2 because we
1329 // suppress them artificially using the code above but we still
1330 // should map them to something for GTK1 and not just ignore them
1331 // as this would lose clicks
1332 case GDK_3BUTTON_PRESS
: // we could also map this to DCLICK...
1333 case GDK_BUTTON_PRESS
:
1334 event_type
= wxEVT_LEFT_DOWN
;
1337 case GDK_2BUTTON_PRESS
:
1338 event_type
= wxEVT_LEFT_DCLICK
;
1342 // just to silence gcc warnings
1346 else if (gdk_event
->button
== 2)
1348 switch (gdk_event
->type
)
1350 case GDK_3BUTTON_PRESS
:
1351 case GDK_BUTTON_PRESS
:
1352 event_type
= wxEVT_MIDDLE_DOWN
;
1355 case GDK_2BUTTON_PRESS
:
1356 event_type
= wxEVT_MIDDLE_DCLICK
;
1363 else if (gdk_event
->button
== 3)
1365 switch (gdk_event
->type
)
1367 case GDK_3BUTTON_PRESS
:
1368 case GDK_BUTTON_PRESS
:
1369 event_type
= wxEVT_RIGHT_DOWN
;
1372 case GDK_2BUTTON_PRESS
:
1373 event_type
= wxEVT_RIGHT_DCLICK
;
1381 else if (gdk_event
->button
== 8)
1383 switch (gdk_event
->type
)
1385 case GDK_3BUTTON_PRESS
:
1386 case GDK_BUTTON_PRESS
:
1387 event_type
= wxEVT_AUX1_DOWN
;
1390 case GDK_2BUTTON_PRESS
:
1391 event_type
= wxEVT_AUX1_DCLICK
;
1399 else if (gdk_event
->button
== 9)
1401 switch (gdk_event
->type
)
1403 case GDK_3BUTTON_PRESS
:
1404 case GDK_BUTTON_PRESS
:
1405 event_type
= wxEVT_AUX2_DOWN
;
1408 case GDK_2BUTTON_PRESS
:
1409 event_type
= wxEVT_AUX2_DCLICK
;
1417 if ( event_type
== wxEVT_NULL
)
1419 // unknown mouse button or click type
1423 g_lastMouseEvent
= (GdkEvent
*) gdk_event
;
1425 wxMouseEvent
event( event_type
);
1426 InitMouseEvent( win
, event
, gdk_event
);
1428 AdjustEventButtonState(event
);
1430 // find the correct window to send the event to: it may be a different one
1431 // from the one which got it at GTK+ level because some controls don't have
1432 // their own X window and thus cannot get any events.
1433 if ( !g_captureWindow
)
1434 win
= FindWindowForMouseEvent(win
, event
.m_x
, event
.m_y
);
1436 // reset the event object and id in case win changed.
1437 event
.SetEventObject( win
);
1438 event
.SetId( win
->GetId() );
1440 bool ret
= win
->GTKProcessEvent( event
);
1441 g_lastMouseEvent
= NULL
;
1445 if ((event_type
== wxEVT_LEFT_DOWN
) && !win
->IsOfStandardClass() &&
1446 (gs_currentFocus
!= win
) /* && win->IsFocusable() */)
1451 if (event_type
== wxEVT_RIGHT_DOWN
)
1453 // generate a "context menu" event: this is similar to right mouse
1454 // click under many GUIs except that it is generated differently
1455 // (right up under MSW, ctrl-click under Mac, right down here) and
1457 // (a) it's a command event and so is propagated to the parent
1458 // (b) under some ports it can be generated from kbd too
1459 // (c) it uses screen coords (because of (a))
1460 wxContextMenuEvent
evtCtx(
1463 win
->ClientToScreen(event
.GetPosition()));
1464 evtCtx
.SetEventObject(win
);
1465 return win
->GTKProcessEvent(evtCtx
);
1471 //-----------------------------------------------------------------------------
1472 // "button_release_event"
1473 //-----------------------------------------------------------------------------
1476 gtk_window_button_release_callback( GtkWidget
*WXUNUSED(widget
),
1477 GdkEventButton
*gdk_event
,
1480 wxPROCESS_EVENT_ONCE(GdkEventButton
, gdk_event
);
1482 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1484 g_lastButtonNumber
= 0;
1486 wxEventType event_type
= wxEVT_NULL
;
1488 switch (gdk_event
->button
)
1491 event_type
= wxEVT_LEFT_UP
;
1495 event_type
= wxEVT_MIDDLE_UP
;
1499 event_type
= wxEVT_RIGHT_UP
;
1503 event_type
= wxEVT_AUX1_UP
;
1507 event_type
= wxEVT_AUX2_UP
;
1511 // unknown button, don't process
1515 g_lastMouseEvent
= (GdkEvent
*) gdk_event
;
1517 wxMouseEvent
event( event_type
);
1518 InitMouseEvent( win
, event
, gdk_event
);
1520 AdjustEventButtonState(event
);
1522 if ( !g_captureWindow
)
1523 win
= FindWindowForMouseEvent(win
, event
.m_x
, event
.m_y
);
1525 // reset the event object and id in case win changed.
1526 event
.SetEventObject( win
);
1527 event
.SetId( win
->GetId() );
1529 bool ret
= win
->GTKProcessEvent(event
);
1531 g_lastMouseEvent
= NULL
;
1536 //-----------------------------------------------------------------------------
1537 // "motion_notify_event"
1538 //-----------------------------------------------------------------------------
1541 gtk_window_motion_notify_callback( GtkWidget
* WXUNUSED(widget
),
1542 GdkEventMotion
*gdk_event
,
1545 wxPROCESS_EVENT_ONCE(GdkEventMotion
, gdk_event
);
1547 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1549 if (gdk_event
->is_hint
)
1553 GdkModifierType state
;
1554 gdk_window_get_pointer(gdk_event
->window
, &x
, &y
, &state
);
1559 g_lastMouseEvent
= (GdkEvent
*) gdk_event
;
1561 wxMouseEvent
event( wxEVT_MOTION
);
1562 InitMouseEvent(win
, event
, gdk_event
);
1564 if ( g_captureWindow
)
1566 // synthesise a mouse enter or leave event if needed
1567 GdkWindow
*winUnderMouse
= gdk_window_at_pointer(NULL
, NULL
);
1568 // This seems to be necessary and actually been added to
1569 // GDK itself in version 2.0.X
1572 bool hasMouse
= winUnderMouse
== gdk_event
->window
;
1573 if ( hasMouse
!= g_captureWindowHasMouse
)
1575 // the mouse changed window
1576 g_captureWindowHasMouse
= hasMouse
;
1578 wxMouseEvent
eventM(g_captureWindowHasMouse
? wxEVT_ENTER_WINDOW
1579 : wxEVT_LEAVE_WINDOW
);
1580 InitMouseEvent(win
, eventM
, gdk_event
);
1581 eventM
.SetEventObject(win
);
1582 win
->GTKProcessEvent(eventM
);
1587 win
= FindWindowForMouseEvent(win
, event
.m_x
, event
.m_y
);
1589 // reset the event object and id in case win changed.
1590 event
.SetEventObject( win
);
1591 event
.SetId( win
->GetId() );
1594 if ( !g_captureWindow
)
1596 wxSetCursorEvent
cevent( event
.m_x
, event
.m_y
);
1597 if (win
->GTKProcessEvent( cevent
))
1599 win
->SetCursor( cevent
.GetCursor() );
1603 bool ret
= win
->GTKProcessEvent(event
);
1605 g_lastMouseEvent
= NULL
;
1610 //-----------------------------------------------------------------------------
1611 // "scroll_event" (mouse wheel event)
1612 //-----------------------------------------------------------------------------
1615 window_scroll_event_hscrollbar(GtkWidget
*, GdkEventScroll
* gdk_event
, wxWindow
* win
)
1617 if (gdk_event
->direction
!= GDK_SCROLL_LEFT
&&
1618 gdk_event
->direction
!= GDK_SCROLL_RIGHT
)
1623 GtkRange
*range
= win
->m_scrollBar
[wxWindow::ScrollDir_Horz
];
1625 if (range
&& gtk_widget_get_visible(GTK_WIDGET(range
)))
1627 GtkAdjustment
* adj
= gtk_range_get_adjustment(range
);
1628 double delta
= gtk_adjustment_get_step_increment(adj
) * 3;
1629 if (gdk_event
->direction
== GDK_SCROLL_LEFT
)
1632 gtk_range_set_value(range
, gtk_adjustment_get_value(adj
) + delta
);
1641 window_scroll_event(GtkWidget
*, GdkEventScroll
* gdk_event
, wxWindow
* win
)
1643 wxMouseEvent
event(wxEVT_MOUSEWHEEL
);
1644 InitMouseEvent(win
, event
, gdk_event
);
1646 // FIXME: Get these values from GTK or GDK
1647 event
.m_linesPerAction
= 3;
1648 event
.m_wheelDelta
= 120;
1650 // Determine the scroll direction.
1651 switch (gdk_event
->direction
)
1654 case GDK_SCROLL_RIGHT
:
1655 event
.m_wheelRotation
= 120;
1658 case GDK_SCROLL_DOWN
:
1659 case GDK_SCROLL_LEFT
:
1660 event
.m_wheelRotation
= -120;
1664 return false; // Unknown/unhandled direction
1667 // And the scroll axis.
1668 switch (gdk_event
->direction
)
1671 case GDK_SCROLL_DOWN
:
1672 event
.m_wheelAxis
= wxMOUSE_WHEEL_VERTICAL
;
1675 case GDK_SCROLL_LEFT
:
1676 case GDK_SCROLL_RIGHT
:
1677 event
.m_wheelAxis
= wxMOUSE_WHEEL_HORIZONTAL
;
1681 if (win
->GTKProcessEvent(event
))
1684 GtkRange
*range
= win
->m_scrollBar
[wxWindow::ScrollDir_Vert
];
1686 if (range
&& gtk_widget_get_visible(GTK_WIDGET(range
)))
1688 GtkAdjustment
* adj
= gtk_range_get_adjustment(range
);
1689 double delta
= gtk_adjustment_get_step_increment(adj
) * 3;
1690 if (gdk_event
->direction
== GDK_SCROLL_UP
)
1693 gtk_range_set_value(range
, gtk_adjustment_get_value(adj
) + delta
);
1701 //-----------------------------------------------------------------------------
1703 //-----------------------------------------------------------------------------
1705 static gboolean
wxgtk_window_popup_menu_callback(GtkWidget
*, wxWindowGTK
* win
)
1707 wxContextMenuEvent
event(wxEVT_CONTEXT_MENU
, win
->GetId(), wxPoint(-1, -1));
1708 event
.SetEventObject(win
);
1709 return win
->GTKProcessEvent(event
);
1712 //-----------------------------------------------------------------------------
1714 //-----------------------------------------------------------------------------
1717 gtk_window_focus_in_callback( GtkWidget
* WXUNUSED(widget
),
1718 GdkEventFocus
*WXUNUSED(event
),
1721 return win
->GTKHandleFocusIn();
1724 //-----------------------------------------------------------------------------
1725 // "focus_out_event"
1726 //-----------------------------------------------------------------------------
1729 gtk_window_focus_out_callback( GtkWidget
* WXUNUSED(widget
),
1730 GdkEventFocus
* WXUNUSED(gdk_event
),
1733 return win
->GTKHandleFocusOut();
1736 //-----------------------------------------------------------------------------
1738 //-----------------------------------------------------------------------------
1741 wx_window_focus_callback(GtkWidget
*widget
,
1742 GtkDirectionType
WXUNUSED(direction
),
1745 // the default handler for focus signal in GtkScrolledWindow sets
1746 // focus to the window itself even if it doesn't accept focus, i.e. has no
1747 // GTK_CAN_FOCUS in its style -- work around this by forcibly preventing
1748 // the signal from reaching gtk_scrolled_window_focus() if we don't have
1749 // any children which might accept focus (we know we don't accept the focus
1750 // ourselves as this signal is only connected in this case)
1751 if ( win
->GetChildren().empty() )
1752 g_signal_stop_emission_by_name(widget
, "focus");
1754 // we didn't change the focus
1758 //-----------------------------------------------------------------------------
1759 // "enter_notify_event"
1760 //-----------------------------------------------------------------------------
1763 gtk_window_enter_callback( GtkWidget
*widget
,
1764 GdkEventCrossing
*gdk_event
,
1767 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1769 // Event was emitted after a grab
1770 if (gdk_event
->mode
!= GDK_CROSSING_NORMAL
) return FALSE
;
1774 GdkModifierType state
= (GdkModifierType
)0;
1776 gdk_window_get_pointer(gtk_widget_get_window(widget
), &x
, &y
, &state
);
1778 wxMouseEvent
event( wxEVT_ENTER_WINDOW
);
1779 InitMouseEvent(win
, event
, gdk_event
);
1780 wxPoint pt
= win
->GetClientAreaOrigin();
1781 event
.m_x
= x
+ pt
.x
;
1782 event
.m_y
= y
+ pt
.y
;
1784 if ( !g_captureWindow
)
1786 wxSetCursorEvent
cevent( event
.m_x
, event
.m_y
);
1787 if (win
->GTKProcessEvent( cevent
))
1789 win
->SetCursor( cevent
.GetCursor() );
1793 return win
->GTKProcessEvent(event
);
1796 //-----------------------------------------------------------------------------
1797 // "leave_notify_event"
1798 //-----------------------------------------------------------------------------
1801 gtk_window_leave_callback( GtkWidget
*widget
,
1802 GdkEventCrossing
*gdk_event
,
1805 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1807 // Event was emitted after an ungrab
1808 if (gdk_event
->mode
!= GDK_CROSSING_NORMAL
) return FALSE
;
1810 wxMouseEvent
event( wxEVT_LEAVE_WINDOW
);
1814 GdkModifierType state
= (GdkModifierType
)0;
1816 gdk_window_get_pointer(gtk_widget_get_window(widget
), &x
, &y
, &state
);
1818 InitMouseEvent(win
, event
, gdk_event
);
1820 return win
->GTKProcessEvent(event
);
1823 //-----------------------------------------------------------------------------
1824 // "value_changed" from scrollbar
1825 //-----------------------------------------------------------------------------
1828 gtk_scrollbar_value_changed(GtkRange
* range
, wxWindow
* win
)
1830 wxEventType eventType
= win
->GTKGetScrollEventType(range
);
1831 if (eventType
!= wxEVT_NULL
)
1833 // Convert scroll event type to scrollwin event type
1834 eventType
+= wxEVT_SCROLLWIN_TOP
- wxEVT_SCROLL_TOP
;
1836 // find the scrollbar which generated the event
1837 wxWindowGTK::ScrollDir dir
= win
->ScrollDirFromRange(range
);
1839 // generate the corresponding wx event
1840 const int orient
= wxWindow::OrientFromScrollDir(dir
);
1841 wxScrollWinEvent
event(eventType
, win
->GetScrollPos(orient
), orient
);
1842 event
.SetEventObject(win
);
1844 win
->GTKProcessEvent(event
);
1848 //-----------------------------------------------------------------------------
1849 // "button_press_event" from scrollbar
1850 //-----------------------------------------------------------------------------
1853 gtk_scrollbar_button_press_event(GtkRange
*, GdkEventButton
*, wxWindow
* win
)
1855 g_blockEventsOnScroll
= true;
1856 win
->m_mouseButtonDown
= true;
1861 //-----------------------------------------------------------------------------
1862 // "event_after" from scrollbar
1863 //-----------------------------------------------------------------------------
1866 gtk_scrollbar_event_after(GtkRange
* range
, GdkEvent
* event
, wxWindow
* win
)
1868 if (event
->type
== GDK_BUTTON_RELEASE
)
1870 g_signal_handlers_block_by_func(range
, (void*)gtk_scrollbar_event_after
, win
);
1872 const int orient
= wxWindow::OrientFromScrollDir(
1873 win
->ScrollDirFromRange(range
));
1874 wxScrollWinEvent
evt(wxEVT_SCROLLWIN_THUMBRELEASE
,
1875 win
->GetScrollPos(orient
), orient
);
1876 evt
.SetEventObject(win
);
1877 win
->GTKProcessEvent(evt
);
1881 //-----------------------------------------------------------------------------
1882 // "button_release_event" from scrollbar
1883 //-----------------------------------------------------------------------------
1886 gtk_scrollbar_button_release_event(GtkRange
* range
, GdkEventButton
*, wxWindow
* win
)
1888 g_blockEventsOnScroll
= false;
1889 win
->m_mouseButtonDown
= false;
1890 // If thumb tracking
1891 if (win
->m_isScrolling
)
1893 win
->m_isScrolling
= false;
1894 // Hook up handler to send thumb release event after this emission is finished.
1895 // To allow setting scroll position from event handler, sending event must
1896 // be deferred until after the GtkRange handler for this signal has run
1897 g_signal_handlers_unblock_by_func(range
, (void*)gtk_scrollbar_event_after
, win
);
1903 //-----------------------------------------------------------------------------
1904 // "realize" from m_widget
1905 //-----------------------------------------------------------------------------
1908 gtk_window_realized_callback(GtkWidget
* WXUNUSED(widget
), wxWindowGTK
* win
)
1910 win
->GTKHandleRealized();
1913 //-----------------------------------------------------------------------------
1914 // "unrealize" from m_wxwindow
1915 //-----------------------------------------------------------------------------
1917 static void unrealize(GtkWidget
*, wxWindowGTK
* win
)
1920 gtk_im_context_set_client_window(win
->m_imData
->context
, NULL
);
1923 //-----------------------------------------------------------------------------
1924 // "size_allocate" from m_wxwindow or m_widget
1925 //-----------------------------------------------------------------------------
1928 size_allocate(GtkWidget
*, GtkAllocation
* alloc
, wxWindow
* win
)
1930 int w
= alloc
->width
;
1931 int h
= alloc
->height
;
1932 if (win
->m_wxwindow
)
1934 int border_x
, border_y
;
1935 WX_PIZZA(win
->m_wxwindow
)->get_border_widths(border_x
, border_y
);
1941 if (win
->m_oldClientWidth
!= w
|| win
->m_oldClientHeight
!= h
)
1943 win
->m_oldClientWidth
= w
;
1944 win
->m_oldClientHeight
= h
;
1945 // this callback can be connected to m_wxwindow,
1946 // so always get size from m_widget->allocation
1948 gtk_widget_get_allocation(win
->m_widget
, &a
);
1949 win
->m_width
= a
.width
;
1950 win
->m_height
= a
.height
;
1951 if (!win
->m_nativeSizeEvent
)
1953 wxSizeEvent
event(win
->GetSize(), win
->GetId());
1954 event
.SetEventObject(win
);
1955 win
->GTKProcessEvent(event
);
1960 //-----------------------------------------------------------------------------
1962 //-----------------------------------------------------------------------------
1964 #if GTK_CHECK_VERSION(2, 8, 0)
1966 gtk_window_grab_broken( GtkWidget
*,
1967 GdkEventGrabBroken
*event
,
1970 // Mouse capture has been lost involuntarily, notify the application
1971 if(!event
->keyboard
&& wxWindow::GetCapture() == win
)
1973 wxMouseCaptureLostEvent
evt( win
->GetId() );
1974 evt
.SetEventObject( win
);
1975 win
->HandleWindowEvent( evt
);
1981 //-----------------------------------------------------------------------------
1983 //-----------------------------------------------------------------------------
1986 void gtk_window_style_set_callback( GtkWidget
*WXUNUSED(widget
),
1987 GtkStyle
*previous_style
,
1990 if (win
&& previous_style
)
1992 if (win
->IsTopLevel())
1994 wxSysColourChangedEvent event
;
1995 event
.SetEventObject(win
);
1996 win
->GTKProcessEvent(event
);
2000 // Border width could change, which will change client size.
2001 // Make sure size event occurs for this
2002 win
->m_oldClientWidth
= 0;
2009 void wxWindowGTK::GTKHandleRealized()
2013 gtk_im_context_set_client_window
2016 m_wxwindow
? GTKGetDrawingWindow()
2017 : gtk_widget_get_window(m_widget
)
2021 // Use composited window if background is transparent, if supported.
2022 if (m_backgroundStyle
== wxBG_STYLE_TRANSPARENT
)
2024 #if wxGTK_HAS_COMPOSITING_SUPPORT
2025 if (IsTransparentBackgroundSupported())
2027 GdkWindow
* const window
= GTKGetDrawingWindow();
2029 gdk_window_set_composited(window
, true);
2032 #endif // wxGTK_HAS_COMPOSITING_SUPPORT
2034 // We revert to erase mode if transparency is not supported
2035 m_backgroundStyle
= wxBG_STYLE_ERASE
;
2040 // We cannot set colours and fonts before the widget
2041 // been realized, so we do this directly after realization
2042 // or otherwise in idle time
2044 if (m_needsStyleChange
)
2046 SetBackgroundStyle(GetBackgroundStyle());
2047 m_needsStyleChange
= false;
2050 wxWindowCreateEvent
event(static_cast<wxWindow
*>(this));
2051 event
.SetEventObject( this );
2052 GTKProcessEvent( event
);
2054 GTKUpdateCursor(true, false);
2057 // ----------------------------------------------------------------------------
2058 // this wxWindowBase function is implemented here (in platform-specific file)
2059 // because it is static and so couldn't be made virtual
2060 // ----------------------------------------------------------------------------
2062 wxWindow
*wxWindowBase::DoFindFocus()
2064 // For compatibility with wxMSW, pretend that showing a popup menu doesn't
2065 // change the focus and that it remains on the window showing it, even
2066 // though the real focus does change in GTK.
2067 extern wxMenu
*wxCurrentPopupMenu
;
2068 if ( wxCurrentPopupMenu
)
2069 return wxCurrentPopupMenu
->GetInvokingWindow();
2071 wxWindowGTK
*focus
= gs_pendingFocus
? gs_pendingFocus
: gs_currentFocus
;
2072 // the cast is necessary when we compile in wxUniversal mode
2073 return static_cast<wxWindow
*>(focus
);
2076 void wxWindowGTK::AddChildGTK(wxWindowGTK
* child
)
2078 wxASSERT_MSG(m_wxwindow
, "Cannot add a child to a window without a client area");
2080 // the window might have been scrolled already, we
2081 // have to adapt the position
2082 wxPizza
* pizza
= WX_PIZZA(m_wxwindow
);
2083 child
->m_x
+= pizza
->m_scroll_x
;
2084 child
->m_y
+= pizza
->m_scroll_y
;
2086 gtk_widget_set_size_request(
2087 child
->m_widget
, child
->m_width
, child
->m_height
);
2088 pizza
->put(child
->m_widget
, child
->m_x
, child
->m_y
);
2091 //-----------------------------------------------------------------------------
2093 //-----------------------------------------------------------------------------
2095 wxWindow
*wxGetActiveWindow()
2097 return wxWindow::FindFocus();
2101 wxMouseState
wxGetMouseState()
2107 GdkModifierType mask
;
2109 gdk_window_get_pointer(NULL
, &x
, &y
, &mask
);
2113 ms
.SetLeftDown((mask
& GDK_BUTTON1_MASK
) != 0);
2114 ms
.SetMiddleDown((mask
& GDK_BUTTON2_MASK
) != 0);
2115 ms
.SetRightDown((mask
& GDK_BUTTON3_MASK
) != 0);
2116 // see the comment in InitMouseEvent()
2117 ms
.SetAux1Down((mask
& GDK_BUTTON4_MASK
) != 0);
2118 ms
.SetAux2Down((mask
& GDK_BUTTON5_MASK
) != 0);
2120 ms
.SetControlDown((mask
& GDK_CONTROL_MASK
) != 0);
2121 ms
.SetShiftDown((mask
& GDK_SHIFT_MASK
) != 0);
2122 ms
.SetAltDown((mask
& GDK_MOD1_MASK
) != 0);
2123 ms
.SetMetaDown((mask
& GDK_META_MASK
) != 0);
2128 //-----------------------------------------------------------------------------
2130 //-----------------------------------------------------------------------------
2132 // in wxUniv/MSW this class is abstract because it doesn't have DoPopupMenu()
2134 #ifdef __WXUNIVERSAL__
2135 IMPLEMENT_ABSTRACT_CLASS(wxWindowGTK
, wxWindowBase
)
2136 #endif // __WXUNIVERSAL__
2138 void wxWindowGTK::Init()
2143 m_focusWidget
= NULL
;
2153 m_showOnIdle
= false;
2156 m_nativeSizeEvent
= false;
2158 m_isScrolling
= false;
2159 m_mouseButtonDown
= false;
2161 // initialize scrolling stuff
2162 for ( int dir
= 0; dir
< ScrollDir_Max
; dir
++ )
2164 m_scrollBar
[dir
] = NULL
;
2165 m_scrollPos
[dir
] = 0;
2169 m_oldClientHeight
= 0;
2171 m_clipPaintRegion
= false;
2173 m_needsStyleChange
= false;
2175 m_cursor
= *wxSTANDARD_CURSOR
;
2178 m_dirtyTabOrder
= false;
2181 wxWindowGTK::wxWindowGTK()
2186 wxWindowGTK::wxWindowGTK( wxWindow
*parent
,
2191 const wxString
&name
)
2195 Create( parent
, id
, pos
, size
, style
, name
);
2198 bool wxWindowGTK::Create( wxWindow
*parent
,
2203 const wxString
&name
)
2205 // Get default border
2206 wxBorder border
= GetBorder(style
);
2208 style
&= ~wxBORDER_MASK
;
2211 if (!PreCreation( parent
, pos
, size
) ||
2212 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
2214 wxFAIL_MSG( wxT("wxWindowGTK creation failed") );
2218 // We should accept the native look
2220 GtkScrolledWindowClass
*scroll_class
= GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) );
2221 scroll_class
->scrollbar_spacing
= 0;
2225 m_wxwindow
= wxPizza::New(m_windowStyle
);
2226 #ifndef __WXUNIVERSAL__
2227 if (HasFlag(wxPizza::BORDER_STYLES
))
2229 g_signal_connect(m_wxwindow
, "parent_set",
2230 G_CALLBACK(parent_set
), this);
2233 if (!HasFlag(wxHSCROLL
) && !HasFlag(wxVSCROLL
))
2234 m_widget
= m_wxwindow
;
2237 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
2239 GtkScrolledWindow
*scrolledWindow
= GTK_SCROLLED_WINDOW(m_widget
);
2241 // There is a conflict with default bindings at GTK+
2242 // level between scrolled windows and notebooks both of which want to use
2243 // Ctrl-PageUp/Down: scrolled windows for scrolling in the horizontal
2244 // direction and notebooks for changing pages -- we decide that if we don't
2245 // have wxHSCROLL style we can safely sacrifice horizontal scrolling if it
2246 // means we can get working keyboard navigation in notebooks
2247 if ( !HasFlag(wxHSCROLL
) )
2250 bindings
= gtk_binding_set_by_class(G_OBJECT_GET_CLASS(m_widget
));
2253 gtk_binding_entry_remove(bindings
, GDK_Page_Up
, GDK_CONTROL_MASK
);
2254 gtk_binding_entry_remove(bindings
, GDK_Page_Down
, GDK_CONTROL_MASK
);
2258 if (HasFlag(wxALWAYS_SHOW_SB
))
2260 gtk_scrolled_window_set_policy( scrolledWindow
, GTK_POLICY_ALWAYS
, GTK_POLICY_ALWAYS
);
2264 gtk_scrolled_window_set_policy( scrolledWindow
, GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
2267 m_scrollBar
[ScrollDir_Horz
] = GTK_RANGE(gtk_scrolled_window_get_hscrollbar(scrolledWindow
));
2268 m_scrollBar
[ScrollDir_Vert
] = GTK_RANGE(gtk_scrolled_window_get_vscrollbar(scrolledWindow
));
2269 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2270 gtk_range_set_inverted( m_scrollBar
[ScrollDir_Horz
], TRUE
);
2272 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
2274 // connect various scroll-related events
2275 for ( int dir
= 0; dir
< ScrollDir_Max
; dir
++ )
2277 // these handlers block mouse events to any window during scrolling
2278 // such as motion events and prevent GTK and wxWidgets from fighting
2279 // over where the slider should be
2280 g_signal_connect(m_scrollBar
[dir
], "button_press_event",
2281 G_CALLBACK(gtk_scrollbar_button_press_event
), this);
2282 g_signal_connect(m_scrollBar
[dir
], "button_release_event",
2283 G_CALLBACK(gtk_scrollbar_button_release_event
), this);
2285 gulong handler_id
= g_signal_connect(m_scrollBar
[dir
], "event_after",
2286 G_CALLBACK(gtk_scrollbar_event_after
), this);
2287 g_signal_handler_block(m_scrollBar
[dir
], handler_id
);
2289 // these handlers get notified when scrollbar slider moves
2290 g_signal_connect_after(m_scrollBar
[dir
], "value_changed",
2291 G_CALLBACK(gtk_scrollbar_value_changed
), this);
2294 gtk_widget_show( m_wxwindow
);
2296 g_object_ref(m_widget
);
2299 m_parent
->DoAddChild( this );
2301 m_focusWidget
= m_wxwindow
;
2303 SetCanFocus(AcceptsFocus());
2310 wxWindowGTK::~wxWindowGTK()
2314 if (gs_currentFocus
== this)
2315 gs_currentFocus
= NULL
;
2316 if (gs_pendingFocus
== this)
2317 gs_pendingFocus
= NULL
;
2319 if ( gs_deferredFocusOut
== this )
2320 gs_deferredFocusOut
= NULL
;
2324 // destroy children before destroying this window itself
2327 // unhook focus handlers to prevent stray events being
2328 // propagated to this (soon to be) dead object
2329 if (m_focusWidget
!= NULL
)
2331 g_signal_handlers_disconnect_by_func (m_focusWidget
,
2332 (gpointer
) gtk_window_focus_in_callback
,
2334 g_signal_handlers_disconnect_by_func (m_focusWidget
,
2335 (gpointer
) gtk_window_focus_out_callback
,
2342 // delete before the widgets to avoid a crash on solaris
2346 // avoid problem with GTK+ 2.18 where a frozen window causes the whole
2347 // TLW to be frozen, and if the window is then destroyed, nothing ever
2348 // gets painted again
2354 // Note that gtk_widget_destroy() does not destroy the widget, it just
2355 // emits the "destroy" signal. The widget is not actually destroyed
2356 // until its reference count drops to zero.
2357 gtk_widget_destroy(m_widget
);
2358 // Release our reference, should be the last one
2359 g_object_unref(m_widget
);
2365 bool wxWindowGTK::PreCreation( wxWindowGTK
*parent
, const wxPoint
&pos
, const wxSize
&size
)
2367 if ( GTKNeedsParent() )
2369 wxCHECK_MSG( parent
, false, wxT("Must have non-NULL parent") );
2372 // Use either the given size, or the default if -1 is given.
2373 // See wxWindowBase for these functions.
2374 m_width
= WidthDefault(size
.x
) ;
2375 m_height
= HeightDefault(size
.y
);
2377 if (pos
!= wxDefaultPosition
)
2386 void wxWindowGTK::PostCreation()
2388 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid window") );
2390 #if wxGTK_HAS_COMPOSITING_SUPPORT
2391 // Set RGBA visual as soon as possible to minimize the possibility that
2392 // somebody uses the wrong one.
2393 if ( m_backgroundStyle
== wxBG_STYLE_TRANSPARENT
&&
2394 IsTransparentBackgroundSupported() )
2396 GdkScreen
*screen
= gtk_widget_get_screen (m_widget
);
2398 GdkColormap
*rgba_colormap
= gdk_screen_get_rgba_colormap (screen
);
2401 gtk_widget_set_colormap(m_widget
, rgba_colormap
);
2403 #endif // wxGTK_HAS_COMPOSITING_SUPPORT
2409 // these get reported to wxWidgets -> wxPaintEvent
2411 g_signal_connect (m_wxwindow
, "expose_event",
2412 G_CALLBACK (gtk_window_expose_callback
), this);
2414 if (GetLayoutDirection() == wxLayout_LeftToRight
)
2415 gtk_widget_set_redraw_on_allocate(m_wxwindow
, HasFlag(wxFULL_REPAINT_ON_RESIZE
));
2418 // Create input method handler
2419 m_imData
= new wxGtkIMData
;
2421 // Cannot handle drawing preedited text yet
2422 gtk_im_context_set_use_preedit( m_imData
->context
, FALSE
);
2424 g_signal_connect (m_imData
->context
, "commit",
2425 G_CALLBACK (gtk_wxwindow_commit_cb
), this);
2426 g_signal_connect(m_wxwindow
, "unrealize", G_CALLBACK(unrealize
), this);
2431 if (!GTK_IS_WINDOW(m_widget
))
2433 if (m_focusWidget
== NULL
)
2434 m_focusWidget
= m_widget
;
2438 g_signal_connect (m_focusWidget
, "focus_in_event",
2439 G_CALLBACK (gtk_window_focus_in_callback
), this);
2440 g_signal_connect (m_focusWidget
, "focus_out_event",
2441 G_CALLBACK (gtk_window_focus_out_callback
), this);
2445 g_signal_connect_after (m_focusWidget
, "focus_in_event",
2446 G_CALLBACK (gtk_window_focus_in_callback
), this);
2447 g_signal_connect_after (m_focusWidget
, "focus_out_event",
2448 G_CALLBACK (gtk_window_focus_out_callback
), this);
2452 if ( !AcceptsFocusFromKeyboard() )
2456 g_signal_connect(m_widget
, "focus",
2457 G_CALLBACK(wx_window_focus_callback
), this);
2460 // connect to the various key and mouse handlers
2462 GtkWidget
*connect_widget
= GetConnectWidget();
2464 ConnectWidget( connect_widget
);
2466 // We cannot set colours, fonts and cursors before the widget has been
2467 // realized, so we do this directly after realization -- unless the widget
2468 // was in fact realized already.
2469 if ( gtk_widget_get_realized(connect_widget
) )
2471 gtk_window_realized_callback(connect_widget
, this);
2475 g_signal_connect (connect_widget
, "realize",
2476 G_CALLBACK (gtk_window_realized_callback
), this);
2481 g_signal_connect(m_wxwindow
? m_wxwindow
: m_widget
, "size_allocate",
2482 G_CALLBACK(size_allocate
), this);
2485 #if GTK_CHECK_VERSION(2, 8, 0)
2486 if ( gtk_check_version(2,8,0) == NULL
)
2488 // Make sure we can notify the app when mouse capture is lost
2491 g_signal_connect (m_wxwindow
, "grab_broken_event",
2492 G_CALLBACK (gtk_window_grab_broken
), this);
2495 if ( connect_widget
!= m_wxwindow
)
2497 g_signal_connect (connect_widget
, "grab_broken_event",
2498 G_CALLBACK (gtk_window_grab_broken
), this);
2501 #endif // GTK+ >= 2.8
2503 if ( GTKShouldConnectSizeRequest() )
2505 // This is needed if we want to add our windows into native
2506 // GTK controls, such as the toolbar. With this callback, the
2507 // toolbar gets to know the correct size (the one set by the
2508 // programmer). Sadly, it misbehaves for wxComboBox.
2509 g_signal_connect (m_widget
, "size_request",
2510 G_CALLBACK (wxgtk_window_size_request_callback
),
2514 InheritAttributes();
2518 SetLayoutDirection(wxLayout_Default
);
2520 // unless the window was created initially hidden (i.e. Hide() had been
2521 // called before Create()), we should show it at GTK+ level as well
2523 gtk_widget_show( m_widget
);
2527 wxWindowGTK::GTKConnectWidget(const char *signal
, wxGTKCallback callback
)
2529 return g_signal_connect(m_widget
, signal
, callback
, this);
2532 void wxWindowGTK::ConnectWidget( GtkWidget
*widget
)
2534 g_signal_connect (widget
, "key_press_event",
2535 G_CALLBACK (gtk_window_key_press_callback
), this);
2536 g_signal_connect (widget
, "key_release_event",
2537 G_CALLBACK (gtk_window_key_release_callback
), this);
2538 g_signal_connect (widget
, "button_press_event",
2539 G_CALLBACK (gtk_window_button_press_callback
), this);
2540 g_signal_connect (widget
, "button_release_event",
2541 G_CALLBACK (gtk_window_button_release_callback
), this);
2542 g_signal_connect (widget
, "motion_notify_event",
2543 G_CALLBACK (gtk_window_motion_notify_callback
), this);
2545 g_signal_connect (widget
, "scroll_event",
2546 G_CALLBACK (window_scroll_event
), this);
2547 if (m_scrollBar
[ScrollDir_Horz
])
2548 g_signal_connect (m_scrollBar
[ScrollDir_Horz
], "scroll_event",
2549 G_CALLBACK (window_scroll_event_hscrollbar
), this);
2550 if (m_scrollBar
[ScrollDir_Vert
])
2551 g_signal_connect (m_scrollBar
[ScrollDir_Vert
], "scroll_event",
2552 G_CALLBACK (window_scroll_event
), this);
2554 g_signal_connect (widget
, "popup_menu",
2555 G_CALLBACK (wxgtk_window_popup_menu_callback
), this);
2556 g_signal_connect (widget
, "enter_notify_event",
2557 G_CALLBACK (gtk_window_enter_callback
), this);
2558 g_signal_connect (widget
, "leave_notify_event",
2559 G_CALLBACK (gtk_window_leave_callback
), this);
2561 if (m_wxwindow
&& (IsTopLevel() || HasFlag(wxBORDER_RAISED
| wxBORDER_SUNKEN
| wxBORDER_THEME
)))
2562 g_signal_connect (m_wxwindow
, "style_set",
2563 G_CALLBACK (gtk_window_style_set_callback
), this);
2566 bool wxWindowGTK::Destroy()
2570 return wxWindowBase::Destroy();
2573 static GSList
* gs_queueResizeList
;
2576 static gboolean
queue_resize(void*)
2578 gdk_threads_enter();
2579 for (GSList
* p
= gs_queueResizeList
; p
; p
= p
->next
)
2583 gtk_widget_queue_resize(GTK_WIDGET(p
->data
));
2584 g_object_remove_weak_pointer(G_OBJECT(p
->data
), &p
->data
);
2587 g_slist_free(gs_queueResizeList
);
2588 gs_queueResizeList
= NULL
;
2589 gdk_threads_leave();
2594 void wxWindowGTK::DoMoveWindow(int x
, int y
, int width
, int height
)
2596 GtkWidget
* parent
= gtk_widget_get_parent(m_widget
);
2597 if (WX_IS_PIZZA(parent
))
2599 WX_PIZZA(parent
)->move(m_widget
, x
, y
);
2600 gtk_widget_set_size_request(m_widget
, width
, height
);
2603 // With GTK3, gtk_widget_queue_resize() is ignored while a size-allocate
2604 // is in progress. This situation is common in wxWidgets, since
2605 // size-allocate can generate wxSizeEvent and size event handlers often
2606 // call SetSize(), directly or indirectly. Work around this by deferring
2607 // the queue-resize until after size-allocate processing is finished.
2608 if (g_slist_find(gs_queueResizeList
, m_widget
) == NULL
)
2610 if (gs_queueResizeList
== NULL
)
2611 g_idle_add_full(GTK_PRIORITY_RESIZE
, queue_resize
, NULL
, NULL
);
2612 gs_queueResizeList
= g_slist_prepend(gs_queueResizeList
, m_widget
);
2613 g_object_add_weak_pointer(G_OBJECT(m_widget
), &gs_queueResizeList
->data
);
2617 void wxWindowGTK::ConstrainSize()
2620 // GPE's window manager doesn't like size hints at all, esp. when the user
2621 // has to use the virtual keyboard, so don't constrain size there
2625 const wxSize minSize
= GetMinSize();
2626 const wxSize maxSize
= GetMaxSize();
2627 if (minSize
.x
> 0 && m_width
< minSize
.x
) m_width
= minSize
.x
;
2628 if (minSize
.y
> 0 && m_height
< minSize
.y
) m_height
= minSize
.y
;
2629 if (maxSize
.x
> 0 && m_width
> maxSize
.x
) m_width
= maxSize
.x
;
2630 if (maxSize
.y
> 0 && m_height
> maxSize
.y
) m_height
= maxSize
.y
;
2634 void wxWindowGTK::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
2636 wxCHECK_RET(m_widget
, "invalid window");
2638 int scrollX
= 0, scrollY
= 0;
2639 GtkWidget
* parent
= gtk_widget_get_parent(m_widget
);
2640 if (WX_IS_PIZZA(parent
))
2642 wxPizza
* pizza
= WX_PIZZA(parent
);
2643 scrollX
= pizza
->m_scroll_x
;
2644 scrollY
= pizza
->m_scroll_y
;
2646 if (x
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
2650 if (y
!= -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
2655 // calculate the best size if we should auto size the window
2656 if ( ((sizeFlags
& wxSIZE_AUTO_WIDTH
) && width
== -1) ||
2657 ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) && height
== -1) )
2659 const wxSize sizeBest
= GetBestSize();
2660 if ( (sizeFlags
& wxSIZE_AUTO_WIDTH
) && width
== -1 )
2662 if ( (sizeFlags
& wxSIZE_AUTO_HEIGHT
) && height
== -1 )
2663 height
= sizeBest
.y
;
2671 const bool sizeChange
= m_width
!= width
|| m_height
!= height
;
2672 if (sizeChange
|| m_x
!= x
|| m_y
!= y
)
2679 /* the default button has a border around it */
2680 if (gtk_widget_get_can_default(m_widget
))
2682 GtkBorder
*default_border
= NULL
;
2683 gtk_widget_style_get( m_widget
, "default_border", &default_border
, NULL
);
2686 x
-= default_border
->left
;
2687 y
-= default_border
->top
;
2688 width
+= default_border
->left
+ default_border
->right
;
2689 height
+= default_border
->top
+ default_border
->bottom
;
2690 gtk_border_free( default_border
);
2694 DoMoveWindow(x
, y
, width
, height
);
2697 if ((sizeChange
&& !m_nativeSizeEvent
) || (sizeFlags
& wxSIZE_FORCE_EVENT
))
2699 // update these variables to keep size_allocate handler
2700 // from sending another size event for this change
2701 GetClientSize( &m_oldClientWidth
, &m_oldClientHeight
);
2703 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
2704 event
.SetEventObject( this );
2705 HandleWindowEvent( event
);
2709 bool wxWindowGTK::GTKShowFromOnIdle()
2711 if (IsShown() && m_showOnIdle
&& !gtk_widget_get_visible (m_widget
))
2713 GtkAllocation alloc
;
2716 alloc
.width
= m_width
;
2717 alloc
.height
= m_height
;
2718 gtk_widget_size_allocate( m_widget
, &alloc
);
2719 gtk_widget_show( m_widget
);
2720 wxShowEvent
eventShow(GetId(), true);
2721 eventShow
.SetEventObject(this);
2722 HandleWindowEvent(eventShow
);
2723 m_showOnIdle
= false;
2730 void wxWindowGTK::OnInternalIdle()
2732 if ( gs_deferredFocusOut
)
2733 GTKHandleDeferredFocusOut();
2735 // Check if we have to show window now
2736 if (GTKShowFromOnIdle()) return;
2738 if ( m_dirtyTabOrder
)
2740 m_dirtyTabOrder
= false;
2744 wxWindowBase::OnInternalIdle();
2747 void wxWindowGTK::DoGetSize( int *width
, int *height
) const
2749 if (width
) (*width
) = m_width
;
2750 if (height
) (*height
) = m_height
;
2753 void wxWindowGTK::DoSetClientSize( int width
, int height
)
2755 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2757 const wxSize size
= GetSize();
2758 const wxSize clientSize
= GetClientSize();
2759 SetSize(width
+ (size
.x
- clientSize
.x
), height
+ (size
.y
- clientSize
.y
));
2762 void wxWindowGTK::DoGetClientSize( int *width
, int *height
) const
2764 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2771 // if window is scrollable, account for scrollbars
2772 if ( GTK_IS_SCROLLED_WINDOW(m_widget
) )
2774 GtkPolicyType policy
[ScrollDir_Max
];
2775 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(m_widget
),
2776 &policy
[ScrollDir_Horz
],
2777 &policy
[ScrollDir_Vert
]);
2779 for ( int i
= 0; i
< ScrollDir_Max
; i
++ )
2781 // don't account for the scrollbars we don't have
2782 GtkRange
* const range
= m_scrollBar
[i
];
2786 // nor for the ones we have but don't current show
2787 switch ( policy
[i
] )
2789 case GTK_POLICY_NEVER
:
2790 // never shown so doesn't take any place
2793 case GTK_POLICY_ALWAYS
:
2794 // no checks necessary
2797 case GTK_POLICY_AUTOMATIC
:
2798 // may be shown or not, check
2799 GtkAdjustment
*adj
= gtk_range_get_adjustment(range
);
2800 if (gtk_adjustment_get_upper(adj
) <= gtk_adjustment_get_page_size(adj
))
2804 GtkScrolledWindowClass
*scroll_class
=
2805 GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) );
2808 gtk_widget_size_request(GTK_WIDGET(range
), &req
);
2809 if (i
== ScrollDir_Horz
)
2810 h
-= req
.height
+ scroll_class
->scrollbar_spacing
;
2812 w
-= req
.width
+ scroll_class
->scrollbar_spacing
;
2816 const wxSize sizeBorders
= DoGetBorderSize();
2826 if (width
) *width
= w
;
2827 if (height
) *height
= h
;
2830 wxSize
wxWindowGTK::DoGetBorderSize() const
2833 return wxWindowBase::DoGetBorderSize();
2836 WX_PIZZA(m_wxwindow
)->get_border_widths(x
, y
);
2838 return 2*wxSize(x
, y
);
2841 void wxWindowGTK::DoGetPosition( int *x
, int *y
) const
2845 GtkWidget
* parent
= NULL
;
2847 parent
= gtk_widget_get_parent(m_widget
);
2848 if (WX_IS_PIZZA(parent
))
2850 wxPizza
* pizza
= WX_PIZZA(parent
);
2851 dx
= pizza
->m_scroll_x
;
2852 dy
= pizza
->m_scroll_y
;
2854 if (x
) (*x
) = m_x
- dx
;
2855 if (y
) (*y
) = m_y
- dy
;
2858 void wxWindowGTK::DoClientToScreen( int *x
, int *y
) const
2860 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2862 if (gtk_widget_get_window(m_widget
) == NULL
) return;
2864 GdkWindow
*source
= NULL
;
2866 source
= gtk_widget_get_window(m_wxwindow
);
2868 source
= gtk_widget_get_window(m_widget
);
2872 gdk_window_get_origin( source
, &org_x
, &org_y
);
2876 if (!gtk_widget_get_has_window(m_widget
))
2879 gtk_widget_get_allocation(m_widget
, &a
);
2888 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2889 *x
= (GetClientSize().x
- *x
) + org_x
;
2897 void wxWindowGTK::DoScreenToClient( int *x
, int *y
) const
2899 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2901 if (!gtk_widget_get_realized(m_widget
)) return;
2903 GdkWindow
*source
= NULL
;
2905 source
= gtk_widget_get_window(m_wxwindow
);
2907 source
= gtk_widget_get_window(m_widget
);
2911 gdk_window_get_origin( source
, &org_x
, &org_y
);
2915 if (!gtk_widget_get_has_window(m_widget
))
2918 gtk_widget_get_allocation(m_widget
, &a
);
2926 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2927 *x
= (GetClientSize().x
- *x
) - org_x
;
2934 bool wxWindowGTK::Show( bool show
)
2936 if ( !wxWindowBase::Show(show
) )
2942 // notice that we may call Hide() before the window is created and this is
2943 // actually useful to create it hidden initially -- but we can't call
2944 // Show() before it is created
2947 wxASSERT_MSG( !show
, "can't show invalid window" );
2955 // defer until later
2959 gtk_widget_show(m_widget
);
2963 gtk_widget_hide(m_widget
);
2966 wxShowEvent
eventShow(GetId(), show
);
2967 eventShow
.SetEventObject(this);
2968 HandleWindowEvent(eventShow
);
2973 void wxWindowGTK::DoEnable( bool enable
)
2975 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2977 gtk_widget_set_sensitive( m_widget
, enable
);
2978 if (m_wxwindow
&& (m_wxwindow
!= m_widget
))
2979 gtk_widget_set_sensitive( m_wxwindow
, enable
);
2982 int wxWindowGTK::GetCharHeight() const
2984 wxCHECK_MSG( (m_widget
!= NULL
), 12, wxT("invalid window") );
2986 wxFont font
= GetFont();
2987 wxCHECK_MSG( font
.IsOk(), 12, wxT("invalid font") );
2989 PangoContext
* context
= gtk_widget_get_pango_context(m_widget
);
2994 PangoFontDescription
*desc
= font
.GetNativeFontInfo()->description
;
2995 PangoLayout
*layout
= pango_layout_new(context
);
2996 pango_layout_set_font_description(layout
, desc
);
2997 pango_layout_set_text(layout
, "H", 1);
2998 PangoLayoutLine
*line
= (PangoLayoutLine
*)pango_layout_get_lines(layout
)->data
;
3000 PangoRectangle rect
;
3001 pango_layout_line_get_extents(line
, NULL
, &rect
);
3003 g_object_unref (layout
);
3005 return (int) PANGO_PIXELS(rect
.height
);
3008 int wxWindowGTK::GetCharWidth() const
3010 wxCHECK_MSG( (m_widget
!= NULL
), 8, wxT("invalid window") );
3012 wxFont font
= GetFont();
3013 wxCHECK_MSG( font
.IsOk(), 8, wxT("invalid font") );
3015 PangoContext
* context
= gtk_widget_get_pango_context(m_widget
);
3020 PangoFontDescription
*desc
= font
.GetNativeFontInfo()->description
;
3021 PangoLayout
*layout
= pango_layout_new(context
);
3022 pango_layout_set_font_description(layout
, desc
);
3023 pango_layout_set_text(layout
, "g", 1);
3024 PangoLayoutLine
*line
= (PangoLayoutLine
*)pango_layout_get_lines(layout
)->data
;
3026 PangoRectangle rect
;
3027 pango_layout_line_get_extents(line
, NULL
, &rect
);
3029 g_object_unref (layout
);
3031 return (int) PANGO_PIXELS(rect
.width
);
3034 void wxWindowGTK::DoGetTextExtent( const wxString
& string
,
3038 int *externalLeading
,
3039 const wxFont
*theFont
) const
3041 wxFont fontToUse
= theFont
? *theFont
: GetFont();
3043 wxCHECK_RET( fontToUse
.IsOk(), wxT("invalid font") );
3052 PangoContext
*context
= NULL
;
3054 context
= gtk_widget_get_pango_context( m_widget
);
3063 PangoFontDescription
*desc
= fontToUse
.GetNativeFontInfo()->description
;
3064 PangoLayout
*layout
= pango_layout_new(context
);
3065 pango_layout_set_font_description(layout
, desc
);
3067 const wxCharBuffer data
= wxGTK_CONV( string
);
3069 pango_layout_set_text(layout
, data
, strlen(data
));
3072 PangoRectangle rect
;
3073 pango_layout_get_extents(layout
, NULL
, &rect
);
3075 if (x
) (*x
) = (wxCoord
) PANGO_PIXELS(rect
.width
);
3076 if (y
) (*y
) = (wxCoord
) PANGO_PIXELS(rect
.height
);
3079 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
3080 int baseline
= pango_layout_iter_get_baseline(iter
);
3081 pango_layout_iter_free(iter
);
3082 *descent
= *y
- PANGO_PIXELS(baseline
);
3084 if (externalLeading
) (*externalLeading
) = 0; // ??
3086 g_object_unref (layout
);
3089 void wxWindowGTK::GTKDisableFocusOutEvent()
3091 g_signal_handlers_block_by_func( m_focusWidget
,
3092 (gpointer
) gtk_window_focus_out_callback
, this);
3095 void wxWindowGTK::GTKEnableFocusOutEvent()
3097 g_signal_handlers_unblock_by_func( m_focusWidget
,
3098 (gpointer
) gtk_window_focus_out_callback
, this);
3101 bool wxWindowGTK::GTKHandleFocusIn()
3103 // Disable default focus handling for custom windows since the default GTK+
3104 // handler issues a repaint
3105 const bool retval
= m_wxwindow
? true : false;
3108 // NB: if there's still unprocessed deferred focus-out event (see
3109 // GTKHandleFocusOut() for explanation), we need to process it first so
3110 // that the order of focus events -- focus-out first, then focus-in
3111 // elsewhere -- is preserved
3112 if ( gs_deferredFocusOut
)
3114 if ( GTKNeedsToFilterSameWindowFocus() &&
3115 gs_deferredFocusOut
== this )
3117 // GTK+ focus changed from this wxWindow back to itself, so don't
3118 // emit any events at all
3119 wxLogTrace(TRACE_FOCUS
,
3120 "filtered out spurious focus change within %s(%p, %s)",
3121 GetClassInfo()->GetClassName(), this, GetLabel());
3122 gs_deferredFocusOut
= NULL
;
3126 // otherwise we need to send focus-out first
3127 wxASSERT_MSG ( gs_deferredFocusOut
!= this,
3128 "GTKHandleFocusIn(GTKFocus_Normal) called even though focus changed back to itself - derived class should handle this" );
3129 GTKHandleDeferredFocusOut();
3133 wxLogTrace(TRACE_FOCUS
,
3134 "handling focus_in event for %s(%p, %s)",
3135 GetClassInfo()->GetClassName(), this, GetLabel());
3138 gtk_im_context_focus_in(m_imData
->context
);
3140 gs_currentFocus
= this;
3141 gs_pendingFocus
= NULL
;
3144 // caret needs to be informed about focus change
3145 wxCaret
*caret
= GetCaret();
3148 caret
->OnSetFocus();
3150 #endif // wxUSE_CARET
3152 // Notify the parent keeping track of focus for the kbd navigation
3153 // purposes that we got it.
3154 wxChildFocusEvent
eventChildFocus(static_cast<wxWindow
*>(this));
3155 GTKProcessEvent(eventChildFocus
);
3157 wxFocusEvent
eventFocus(wxEVT_SET_FOCUS
, GetId());
3158 eventFocus
.SetEventObject(this);
3159 GTKProcessEvent(eventFocus
);
3164 bool wxWindowGTK::GTKHandleFocusOut()
3166 // Disable default focus handling for custom windows since the default GTK+
3167 // handler issues a repaint
3168 const bool retval
= m_wxwindow
? true : false;
3171 // NB: If a control is composed of several GtkWidgets and when focus
3172 // changes from one of them to another within the same wxWindow, we get
3173 // a focus-out event followed by focus-in for another GtkWidget owned
3174 // by the same wx control. We don't want to generate two spurious
3175 // wxEVT_SET_FOCUS events in this case, so we defer sending wx events
3176 // from GTKHandleFocusOut() until we know for sure it's not coming back
3177 // (i.e. in GTKHandleFocusIn() or at idle time).
3178 if ( GTKNeedsToFilterSameWindowFocus() )
3180 wxASSERT_MSG( gs_deferredFocusOut
== NULL
,
3181 "deferred focus out event already pending" );
3182 wxLogTrace(TRACE_FOCUS
,
3183 "deferring focus_out event for %s(%p, %s)",
3184 GetClassInfo()->GetClassName(), this, GetLabel());
3185 gs_deferredFocusOut
= this;
3189 GTKHandleFocusOutNoDeferring();
3194 void wxWindowGTK::GTKHandleFocusOutNoDeferring()
3196 wxLogTrace(TRACE_FOCUS
,
3197 "handling focus_out event for %s(%p, %s)",
3198 GetClassInfo()->GetClassName(), this, GetLabel());
3201 gtk_im_context_focus_out(m_imData
->context
);
3203 if ( gs_currentFocus
!= this )
3205 // Something is terribly wrong, gs_currentFocus is out of sync with the
3206 // real focus. We will reset it to NULL anyway, because after this
3207 // focus-out event is handled, one of the following with happen:
3209 // * either focus will go out of the app altogether, in which case
3210 // gs_currentFocus _should_ be NULL
3212 // * or it goes to another control, in which case focus-in event will
3213 // follow immediately and it will set gs_currentFocus to the right
3215 wxLogDebug("window %s(%p, %s) lost focus even though it didn't have it",
3216 GetClassInfo()->GetClassName(), this, GetLabel());
3218 gs_currentFocus
= NULL
;
3221 // caret needs to be informed about focus change
3222 wxCaret
*caret
= GetCaret();
3225 caret
->OnKillFocus();
3227 #endif // wxUSE_CARET
3229 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetId() );
3230 event
.SetEventObject( this );
3231 event
.SetWindow( FindFocus() );
3232 GTKProcessEvent( event
);
3236 void wxWindowGTK::GTKHandleDeferredFocusOut()
3238 // NB: See GTKHandleFocusOut() for explanation. This function is called
3239 // from either GTKHandleFocusIn() or OnInternalIdle() to process
3241 if ( gs_deferredFocusOut
)
3243 wxWindowGTK
*win
= gs_deferredFocusOut
;
3244 gs_deferredFocusOut
= NULL
;
3246 wxLogTrace(TRACE_FOCUS
,
3247 "processing deferred focus_out event for %s(%p, %s)",
3248 win
->GetClassInfo()->GetClassName(), win
, win
->GetLabel());
3250 win
->GTKHandleFocusOutNoDeferring();
3254 void wxWindowGTK::SetFocus()
3256 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
3258 // Setting "physical" focus is not immediate in GTK+ and while
3259 // gtk_widget_is_focus ("determines if the widget is the focus widget
3260 // within its toplevel", i.e. returns true for one widget per TLW, not
3261 // globally) returns true immediately after grabbing focus,
3262 // GTK_WIDGET_HAS_FOCUS (which returns true only for the one widget that
3263 // has focus at the moment) takes effect only after the window is shown
3264 // (if it was hidden at the moment of the call) or at the next event loop
3267 // Because we want to FindFocus() call immediately following
3268 // foo->SetFocus() to return foo, we have to keep track of "pending" focus
3270 gs_pendingFocus
= this;
3272 GtkWidget
*widget
= m_wxwindow
? m_wxwindow
: m_focusWidget
;
3274 if ( GTK_IS_CONTAINER(widget
) &&
3275 !gtk_widget_get_can_focus(widget
) )
3277 wxLogTrace(TRACE_FOCUS
,
3278 wxT("Setting focus to a child of %s(%p, %s)"),
3279 GetClassInfo()->GetClassName(), this, GetLabel().c_str());
3280 gtk_widget_child_focus(widget
, GTK_DIR_TAB_FORWARD
);
3284 wxLogTrace(TRACE_FOCUS
,
3285 wxT("Setting focus to %s(%p, %s)"),
3286 GetClassInfo()->GetClassName(), this, GetLabel().c_str());
3287 gtk_widget_grab_focus(widget
);
3291 void wxWindowGTK::SetCanFocus(bool canFocus
)
3293 gtk_widget_set_can_focus(m_widget
, canFocus
);
3295 if ( m_wxwindow
&& (m_widget
!= m_wxwindow
) )
3297 gtk_widget_set_can_focus(m_wxwindow
, canFocus
);
3301 bool wxWindowGTK::Reparent( wxWindowBase
*newParentBase
)
3303 wxCHECK_MSG( (m_widget
!= NULL
), false, wxT("invalid window") );
3305 wxWindowGTK
* const newParent
= (wxWindowGTK
*)newParentBase
;
3307 wxASSERT( GTK_IS_WIDGET(m_widget
) );
3309 if ( !wxWindowBase::Reparent(newParent
) )
3312 wxASSERT( GTK_IS_WIDGET(m_widget
) );
3314 // Notice that old m_parent pointer might be non-NULL here but the widget
3315 // still not have any parent at GTK level if it's a notebook page that had
3316 // been removed from the notebook so test this at GTK level and not wx one.
3317 if ( GtkWidget
*parentGTK
= gtk_widget_get_parent(m_widget
) )
3318 gtk_container_remove(GTK_CONTAINER(parentGTK
), m_widget
);
3320 wxASSERT( GTK_IS_WIDGET(m_widget
) );
3324 if (gtk_widget_get_visible (newParent
->m_widget
))
3326 m_showOnIdle
= true;
3327 gtk_widget_hide( m_widget
);
3329 /* insert GTK representation */
3330 newParent
->AddChildGTK(this);
3333 SetLayoutDirection(wxLayout_Default
);
3338 void wxWindowGTK::DoAddChild(wxWindowGTK
*child
)
3340 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid window") );
3341 wxASSERT_MSG( (child
!= NULL
), wxT("invalid child window") );
3346 /* insert GTK representation */
3350 void wxWindowGTK::AddChild(wxWindowBase
*child
)
3352 wxWindowBase::AddChild(child
);
3353 m_dirtyTabOrder
= true;
3354 wxTheApp
->WakeUpIdle();
3357 void wxWindowGTK::RemoveChild(wxWindowBase
*child
)
3359 wxWindowBase::RemoveChild(child
);
3360 m_dirtyTabOrder
= true;
3361 wxTheApp
->WakeUpIdle();
3365 wxLayoutDirection
wxWindowGTK::GTKGetLayout(GtkWidget
*widget
)
3367 return gtk_widget_get_direction(widget
) == GTK_TEXT_DIR_RTL
3368 ? wxLayout_RightToLeft
3369 : wxLayout_LeftToRight
;
3373 void wxWindowGTK::GTKSetLayout(GtkWidget
*widget
, wxLayoutDirection dir
)
3375 wxASSERT_MSG( dir
!= wxLayout_Default
, wxT("invalid layout direction") );
3377 gtk_widget_set_direction(widget
,
3378 dir
== wxLayout_RightToLeft
? GTK_TEXT_DIR_RTL
3379 : GTK_TEXT_DIR_LTR
);
3382 wxLayoutDirection
wxWindowGTK::GetLayoutDirection() const
3384 return GTKGetLayout(m_widget
);
3387 void wxWindowGTK::SetLayoutDirection(wxLayoutDirection dir
)
3389 if ( dir
== wxLayout_Default
)
3391 const wxWindow
*const parent
= GetParent();
3394 // inherit layout from parent.
3395 dir
= parent
->GetLayoutDirection();
3397 else // no parent, use global default layout
3399 dir
= wxTheApp
->GetLayoutDirection();
3403 if ( dir
== wxLayout_Default
)
3406 GTKSetLayout(m_widget
, dir
);
3408 if (m_wxwindow
&& (m_wxwindow
!= m_widget
))
3409 GTKSetLayout(m_wxwindow
, dir
);
3413 wxWindowGTK::AdjustForLayoutDirection(wxCoord x
,
3414 wxCoord
WXUNUSED(width
),
3415 wxCoord
WXUNUSED(widthTotal
)) const
3417 // We now mirror the coordinates of RTL windows in wxPizza
3421 void wxWindowGTK::DoMoveInTabOrder(wxWindow
*win
, WindowOrder move
)
3423 wxWindowBase::DoMoveInTabOrder(win
, move
);
3424 m_dirtyTabOrder
= true;
3425 wxTheApp
->WakeUpIdle();
3428 bool wxWindowGTK::DoNavigateIn(int flags
)
3430 if ( flags
& wxNavigationKeyEvent::WinChange
)
3432 wxFAIL_MSG( wxT("not implemented") );
3436 else // navigate inside the container
3438 wxWindow
*parent
= wxGetTopLevelParent((wxWindow
*)this);
3439 wxCHECK_MSG( parent
, false, wxT("every window must have a TLW parent") );
3441 GtkDirectionType dir
;
3442 dir
= flags
& wxNavigationKeyEvent::IsForward
? GTK_DIR_TAB_FORWARD
3443 : GTK_DIR_TAB_BACKWARD
;
3446 g_signal_emit_by_name(parent
->m_widget
, "focus", dir
, &rc
);
3452 bool wxWindowGTK::GTKWidgetNeedsMnemonic() const
3454 // none needed by default
3458 void wxWindowGTK::GTKWidgetDoSetMnemonic(GtkWidget
* WXUNUSED(w
))
3460 // nothing to do by default since none is needed
3463 void wxWindowGTK::RealizeTabOrder()
3467 if ( !m_children
.empty() )
3469 // we don't only construct the correct focus chain but also use
3470 // this opportunity to update the mnemonic widgets for the widgets
3473 GList
*chain
= NULL
;
3474 wxWindowGTK
* mnemonicWindow
= NULL
;
3476 for ( wxWindowList::const_iterator i
= m_children
.begin();
3477 i
!= m_children
.end();
3480 wxWindowGTK
*win
= *i
;
3482 bool focusableFromKeyboard
= win
->AcceptsFocusFromKeyboard();
3484 if ( mnemonicWindow
)
3486 if ( focusableFromKeyboard
)
3488 // wxComboBox et al. needs to focus on on a different
3489 // widget than m_widget, so if the main widget isn't
3490 // focusable try the connect widget
3491 GtkWidget
* w
= win
->m_widget
;
3492 if ( !gtk_widget_get_can_focus(w
) )
3494 w
= win
->GetConnectWidget();
3495 if ( !gtk_widget_get_can_focus(w
) )
3501 mnemonicWindow
->GTKWidgetDoSetMnemonic(w
);
3502 mnemonicWindow
= NULL
;
3506 else if ( win
->GTKWidgetNeedsMnemonic() )
3508 mnemonicWindow
= win
;
3511 if ( focusableFromKeyboard
)
3512 chain
= g_list_prepend(chain
, win
->m_widget
);
3515 chain
= g_list_reverse(chain
);
3517 gtk_container_set_focus_chain(GTK_CONTAINER(m_wxwindow
), chain
);
3522 gtk_container_unset_focus_chain(GTK_CONTAINER(m_wxwindow
));
3527 void wxWindowGTK::Raise()
3529 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
3531 if (m_wxwindow
&& gtk_widget_get_window(m_wxwindow
))
3533 gdk_window_raise(gtk_widget_get_window(m_wxwindow
));
3535 else if (gtk_widget_get_window(m_widget
))
3537 gdk_window_raise(gtk_widget_get_window(m_widget
));
3541 void wxWindowGTK::Lower()
3543 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
3545 if (m_wxwindow
&& gtk_widget_get_window(m_wxwindow
))
3547 gdk_window_lower(gtk_widget_get_window(m_wxwindow
));
3549 else if (gtk_widget_get_window(m_widget
))
3551 gdk_window_lower(gtk_widget_get_window(m_widget
));
3555 bool wxWindowGTK::SetCursor( const wxCursor
&cursor
)
3557 if ( !wxWindowBase::SetCursor(cursor
.IsOk() ? cursor
: *wxSTANDARD_CURSOR
) )
3565 void wxWindowGTK::GTKUpdateCursor(bool update_self
/*=true*/, bool recurse
/*=true*/)
3569 wxCursor
cursor(g_globalCursor
.IsOk() ? g_globalCursor
: GetCursor());
3570 if ( cursor
.IsOk() )
3572 wxArrayGdkWindows windowsThis
;
3573 GdkWindow
* window
= GTKGetWindow(windowsThis
);
3575 gdk_window_set_cursor( window
, cursor
.GetCursor() );
3578 const size_t count
= windowsThis
.size();
3579 for ( size_t n
= 0; n
< count
; n
++ )
3581 GdkWindow
*win
= windowsThis
[n
];
3582 // It can be zero if the window has not been realized yet.
3585 gdk_window_set_cursor(win
, cursor
.GetCursor());
3594 for (wxWindowList::iterator it
= GetChildren().begin(); it
!= GetChildren().end(); ++it
)
3596 (*it
)->GTKUpdateCursor( true );
3601 void wxWindowGTK::WarpPointer( int x
, int y
)
3603 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
3605 ClientToScreen(&x
, &y
);
3606 GdkDisplay
* display
= gtk_widget_get_display(m_widget
);
3607 GdkScreen
* screen
= gtk_widget_get_screen(m_widget
);
3609 GdkDeviceManager
* manager
= gdk_display_get_device_manager(display
);
3610 gdk_device_warp(gdk_device_manager_get_client_pointer(manager
), screen
, x
, y
);
3612 #ifdef GDK_WINDOWING_X11
3613 XWarpPointer(GDK_DISPLAY_XDISPLAY(display
),
3615 GDK_WINDOW_XID(gdk_screen_get_root_window(screen
)),
3621 wxWindowGTK::ScrollDir
wxWindowGTK::ScrollDirFromRange(GtkRange
*range
) const
3623 // find the scrollbar which generated the event
3624 for ( int dir
= 0; dir
< ScrollDir_Max
; dir
++ )
3626 if ( range
== m_scrollBar
[dir
] )
3627 return (ScrollDir
)dir
;
3630 wxFAIL_MSG( wxT("event from unknown scrollbar received") );
3632 return ScrollDir_Max
;
3635 bool wxWindowGTK::DoScrollByUnits(ScrollDir dir
, ScrollUnit unit
, int units
)
3637 bool changed
= false;
3638 GtkRange
* range
= m_scrollBar
[dir
];
3639 if ( range
&& units
)
3641 GtkAdjustment
* adj
= gtk_range_get_adjustment(range
);
3642 double inc
= unit
== ScrollUnit_Line
? gtk_adjustment_get_step_increment(adj
)
3643 : gtk_adjustment_get_page_increment(adj
);
3645 const int posOld
= wxRound(gtk_adjustment_get_value(adj
));
3646 gtk_range_set_value(range
, posOld
+ units
*inc
);
3648 changed
= wxRound(gtk_adjustment_get_value(adj
)) != posOld
;
3654 bool wxWindowGTK::ScrollLines(int lines
)
3656 return DoScrollByUnits(ScrollDir_Vert
, ScrollUnit_Line
, lines
);
3659 bool wxWindowGTK::ScrollPages(int pages
)
3661 return DoScrollByUnits(ScrollDir_Vert
, ScrollUnit_Page
, pages
);
3664 void wxWindowGTK::Refresh(bool WXUNUSED(eraseBackground
),
3669 if (gtk_widget_get_mapped(m_wxwindow
))
3671 GdkWindow
* window
= gtk_widget_get_window(m_wxwindow
);
3674 GdkRectangle r
= { rect
->x
, rect
->y
, rect
->width
, rect
->height
};
3675 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3676 r
.x
= gdk_window_get_width(window
) - r
.x
- rect
->width
;
3677 gdk_window_invalidate_rect(window
, &r
, true);
3680 gdk_window_invalidate_rect(window
, NULL
, true);
3685 if (gtk_widget_get_mapped(m_widget
))
3688 gtk_widget_queue_draw_area(m_widget
, rect
->x
, rect
->y
, rect
->width
, rect
->height
);
3690 gtk_widget_queue_draw(m_widget
);
3695 void wxWindowGTK::Update()
3697 if (m_widget
&& gtk_widget_get_mapped(m_widget
))
3699 GdkDisplay
* display
= gtk_widget_get_display(m_widget
);
3700 // Flush everything out to the server, and wait for it to finish.
3701 // This ensures nothing will overwrite the drawing we are about to do.
3702 gdk_display_sync(display
);
3704 GdkWindow
* window
= GTKGetDrawingWindow();
3706 window
= gtk_widget_get_window(m_widget
);
3707 gdk_window_process_updates(window
, true);
3709 // Flush again, but no need to wait for it to finish
3710 gdk_display_flush(display
);
3714 bool wxWindowGTK::DoIsExposed( int x
, int y
) const
3716 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
3719 bool wxWindowGTK::DoIsExposed( int x
, int y
, int w
, int h
) const
3721 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3722 return m_updateRegion
.Contains(x
-w
, y
, w
, h
) != wxOutRegion
;
3724 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
3727 void wxWindowGTK::GtkSendPaintEvents()
3731 m_updateRegion
.Clear();
3734 #if wxGTK_HAS_COMPOSITING_SUPPORT
3737 // Clip to paint region in wxClientDC
3738 m_clipPaintRegion
= true;
3740 m_nativeUpdateRegion
= m_updateRegion
;
3742 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3744 // Transform m_updateRegion under RTL
3745 m_updateRegion
.Clear();
3748 gdk_drawable_get_size(gtk_widget_get_window(m_wxwindow
), &width
, NULL
);
3750 wxRegionIterator
upd( m_nativeUpdateRegion
);
3754 rect
.x
= upd
.GetX();
3755 rect
.y
= upd
.GetY();
3756 rect
.width
= upd
.GetWidth();
3757 rect
.height
= upd
.GetHeight();
3759 rect
.x
= width
- rect
.x
- rect
.width
;
3760 m_updateRegion
.Union( rect
);
3766 switch ( GetBackgroundStyle() )
3768 case wxBG_STYLE_TRANSPARENT
:
3769 #if wxGTK_HAS_COMPOSITING_SUPPORT
3770 if (IsTransparentBackgroundSupported())
3772 // Set a transparent background, so that overlaying in parent
3773 // might indeed let see through where this child did not
3774 // explicitly paint.
3775 // NB: it works also for top level windows (but this is the
3776 // windows manager which then does the compositing job)
3777 cr
= gdk_cairo_create(m_wxwindow
->window
);
3778 gdk_cairo_region(cr
, m_nativeUpdateRegion
.GetRegion());
3781 cairo_set_operator(cr
, CAIRO_OPERATOR_CLEAR
);
3783 cairo_set_operator(cr
, CAIRO_OPERATOR_OVER
);
3784 cairo_surface_flush(cairo_get_target(cr
));
3786 #endif // wxGTK_HAS_COMPOSITING_SUPPORT
3789 case wxBG_STYLE_ERASE
:
3791 wxWindowDC
dc( (wxWindow
*)this );
3792 dc
.SetDeviceClippingRegion( m_updateRegion
);
3794 // Work around gtk-qt <= 0.60 bug whereby the window colour
3798 GetOptionInt("gtk.window.force-background-colour") )
3800 dc
.SetBackground(GetBackgroundColour());
3804 wxEraseEvent
erase_event( GetId(), &dc
);
3805 erase_event
.SetEventObject( this );
3807 if ( HandleWindowEvent(erase_event
) )
3809 // background erased, don't do it again
3815 case wxBG_STYLE_SYSTEM
:
3816 if ( GetThemeEnabled() )
3818 // find ancestor from which to steal background
3819 wxWindow
*parent
= wxGetTopLevelParent((wxWindow
*)this);
3821 parent
= (wxWindow
*)this;
3823 if (gtk_widget_get_mapped(parent
->m_widget
))
3825 wxRegionIterator
upd( m_nativeUpdateRegion
);
3829 rect
.x
= upd
.GetX();
3830 rect
.y
= upd
.GetY();
3831 rect
.width
= upd
.GetWidth();
3832 rect
.height
= upd
.GetHeight();
3834 gtk_paint_flat_box(gtk_widget_get_style(parent
->m_widget
),
3835 GTKGetDrawingWindow(),
3836 gtk_widget_get_state(m_wxwindow
),
3849 case wxBG_STYLE_PAINT
:
3850 // nothing to do: window will be painted over in EVT_PAINT
3854 wxFAIL_MSG( "unsupported background style" );
3857 wxNcPaintEvent
nc_paint_event( GetId() );
3858 nc_paint_event
.SetEventObject( this );
3859 HandleWindowEvent( nc_paint_event
);
3861 wxPaintEvent
paint_event( GetId() );
3862 paint_event
.SetEventObject( this );
3863 HandleWindowEvent( paint_event
);
3865 #if wxGTK_HAS_COMPOSITING_SUPPORT
3866 if (IsTransparentBackgroundSupported())
3867 { // now composite children which need it
3868 // Overlay all our composite children on top of the painted area
3869 wxWindowList::compatibility_iterator node
;
3870 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
3872 wxWindow
*compositeChild
= node
->GetData();
3873 if (compositeChild
->GetBackgroundStyle() == wxBG_STYLE_TRANSPARENT
)
3877 cr
= gdk_cairo_create(m_wxwindow
->window
);
3878 gdk_cairo_region(cr
, m_nativeUpdateRegion
.GetRegion());
3882 GtkWidget
*child
= compositeChild
->m_wxwindow
;
3883 GtkAllocation alloc
;
3884 gtk_widget_get_allocation(child
, &alloc
);
3886 // The source data is the (composited) child
3887 gdk_cairo_set_source_window(
3888 cr
, gtk_widget_get_window(child
), alloc
.x
, alloc
.y
);
3896 #endif // wxGTK_HAS_COMPOSITING_SUPPORT
3898 m_clipPaintRegion
= false;
3900 m_updateRegion
.Clear();
3901 m_nativeUpdateRegion
.Clear();
3904 void wxWindowGTK::SetDoubleBuffered( bool on
)
3906 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
3909 gtk_widget_set_double_buffered( m_wxwindow
, on
);
3912 bool wxWindowGTK::IsDoubleBuffered() const
3914 return gtk_widget_get_double_buffered( m_wxwindow
);
3917 void wxWindowGTK::ClearBackground()
3919 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
3923 void wxWindowGTK::DoSetToolTip( wxToolTip
*tip
)
3925 if (m_tooltip
!= tip
)
3927 wxWindowBase::DoSetToolTip(tip
);
3930 m_tooltip
->GTKSetWindow(static_cast<wxWindow
*>(this));
3932 GTKApplyToolTip(NULL
);
3936 void wxWindowGTK::GTKApplyToolTip(const char* tip
)
3938 wxToolTip::GTKApply(GetConnectWidget(), tip
);
3940 #endif // wxUSE_TOOLTIPS
3942 bool wxWindowGTK::SetBackgroundColour( const wxColour
&colour
)
3944 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
3946 if (!wxWindowBase::SetBackgroundColour(colour
))
3951 // We need the pixel value e.g. for background clearing.
3952 m_backgroundColour
.CalcPixel(gtk_widget_get_colormap(m_widget
));
3955 // apply style change (forceStyle=true so that new style is applied
3956 // even if the bg colour changed from valid to wxNullColour)
3957 GTKApplyWidgetStyle(true);
3962 bool wxWindowGTK::SetForegroundColour( const wxColour
&colour
)
3964 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
3966 if (!wxWindowBase::SetForegroundColour(colour
))
3973 // We need the pixel value e.g. for background clearing.
3974 m_foregroundColour
.CalcPixel(gtk_widget_get_colormap(m_widget
));
3977 // apply style change (forceStyle=true so that new style is applied
3978 // even if the bg colour changed from valid to wxNullColour):
3979 GTKApplyWidgetStyle(true);
3984 PangoContext
*wxWindowGTK::GTKGetPangoDefaultContext()
3986 return gtk_widget_get_pango_context( m_widget
);
3989 GtkRcStyle
*wxWindowGTK::GTKCreateWidgetStyle(bool forceStyle
)
3991 // do we need to apply any changes at all?
3994 !m_foregroundColour
.IsOk() && !m_backgroundColour
.IsOk() )
3999 GtkRcStyle
*style
= gtk_rc_style_new();
4001 if ( m_font
.IsOk() )
4004 pango_font_description_copy( m_font
.GetNativeFontInfo()->description
);
4007 int flagsNormal
= 0,
4010 flagsInsensitive
= 0;
4012 if ( m_foregroundColour
.IsOk() )
4014 const GdkColor
*fg
= m_foregroundColour
.GetColor();
4016 style
->fg
[GTK_STATE_NORMAL
] =
4017 style
->text
[GTK_STATE_NORMAL
] = *fg
;
4018 flagsNormal
|= GTK_RC_FG
| GTK_RC_TEXT
;
4020 style
->fg
[GTK_STATE_PRELIGHT
] =
4021 style
->text
[GTK_STATE_PRELIGHT
] = *fg
;
4022 flagsPrelight
|= GTK_RC_FG
| GTK_RC_TEXT
;
4024 style
->fg
[GTK_STATE_ACTIVE
] =
4025 style
->text
[GTK_STATE_ACTIVE
] = *fg
;
4026 flagsActive
|= GTK_RC_FG
| GTK_RC_TEXT
;
4029 if ( m_backgroundColour
.IsOk() )
4031 const GdkColor
*bg
= m_backgroundColour
.GetColor();
4033 style
->bg
[GTK_STATE_NORMAL
] =
4034 style
->base
[GTK_STATE_NORMAL
] = *bg
;
4035 flagsNormal
|= GTK_RC_BG
| GTK_RC_BASE
;
4037 style
->bg
[GTK_STATE_PRELIGHT
] =
4038 style
->base
[GTK_STATE_PRELIGHT
] = *bg
;
4039 flagsPrelight
|= GTK_RC_BG
| GTK_RC_BASE
;
4041 style
->bg
[GTK_STATE_ACTIVE
] =
4042 style
->base
[GTK_STATE_ACTIVE
] = *bg
;
4043 flagsActive
|= GTK_RC_BG
| GTK_RC_BASE
;
4045 style
->bg
[GTK_STATE_INSENSITIVE
] =
4046 style
->base
[GTK_STATE_INSENSITIVE
] = *bg
;
4047 flagsInsensitive
|= GTK_RC_BG
| GTK_RC_BASE
;
4050 style
->color_flags
[GTK_STATE_NORMAL
] = (GtkRcFlags
)flagsNormal
;
4051 style
->color_flags
[GTK_STATE_PRELIGHT
] = (GtkRcFlags
)flagsPrelight
;
4052 style
->color_flags
[GTK_STATE_ACTIVE
] = (GtkRcFlags
)flagsActive
;
4053 style
->color_flags
[GTK_STATE_INSENSITIVE
] = (GtkRcFlags
)flagsInsensitive
;
4058 void wxWindowGTK::GTKApplyWidgetStyle(bool forceStyle
)
4060 GtkRcStyle
*style
= GTKCreateWidgetStyle(forceStyle
);
4063 DoApplyWidgetStyle(style
);
4064 g_object_unref(style
);
4067 // Style change may affect GTK+'s size calculation:
4068 InvalidateBestSize();
4071 void wxWindowGTK::DoApplyWidgetStyle(GtkRcStyle
*style
)
4075 // block the signal temporarily to avoid sending
4076 // wxSysColourChangedEvents when we change the colours ourselves
4077 bool unblock
= false;
4081 g_signal_handlers_block_by_func(
4082 m_wxwindow
, (void *)gtk_window_style_set_callback
, this);
4085 gtk_widget_modify_style(m_wxwindow
, style
);
4089 g_signal_handlers_unblock_by_func(
4090 m_wxwindow
, (void *)gtk_window_style_set_callback
, this);
4095 gtk_widget_modify_style(m_widget
, style
);
4099 bool wxWindowGTK::SetBackgroundStyle(wxBackgroundStyle style
)
4101 if (!wxWindowBase::SetBackgroundStyle(style
))
4107 window
= GTKGetDrawingWindow();
4111 GtkWidget
* const w
= GetConnectWidget();
4112 window
= w
? gtk_widget_get_window(w
) : NULL
;
4115 bool wantNoBackPixmap
= style
== wxBG_STYLE_PAINT
|| style
== wxBG_STYLE_TRANSPARENT
;
4117 if ( wantNoBackPixmap
)
4121 // Make sure GDK/X11 doesn't refresh the window
4123 gdk_window_set_back_pixmap( window
, NULL
, FALSE
);
4124 m_needsStyleChange
= false;
4126 else // window not realized yet
4128 // Do when window is realized
4129 m_needsStyleChange
= true;
4132 // Don't apply widget style, or we get a grey background
4136 // apply style change (forceStyle=true so that new style is applied
4137 // even if the bg colour changed from valid to wxNullColour):
4138 GTKApplyWidgetStyle(true);
4144 bool wxWindowGTK::IsTransparentBackgroundSupported(wxString
* reason
) const
4146 #if wxGTK_HAS_COMPOSITING_SUPPORT
4147 if (gtk_check_version(wxGTK_VERSION_REQUIRED_FOR_COMPOSITING
) != NULL
)
4151 *reason
= _("GTK+ installed on this machine is too old to "
4152 "support screen compositing, please install "
4153 "GTK+ 2.12 or later.");
4159 // NB: We don't check here if the particular kind of widget supports
4160 // transparency, we check only if it would be possible for a generic window
4162 wxCHECK_MSG ( m_widget
, false, "Window must be created first" );
4164 if (!gdk_screen_is_composited(gtk_widget_get_screen(m_widget
)))
4168 *reason
= _("Compositing not supported by this system, "
4169 "please enable it in your Window Manager.");
4179 *reason
= _("This program was compiled with a too old version of GTK+, "
4180 "please rebuild with GTK+ 2.12 or newer.");
4182 #endif // wxGTK_HAS_COMPOSITING_SUPPORT/!wxGTK_HAS_COMPOSITING_SUPPORT
4187 // ----------------------------------------------------------------------------
4188 // Pop-up menu stuff
4189 // ----------------------------------------------------------------------------
4191 #if wxUSE_MENUS_NATIVE
4195 void wxPopupMenuPositionCallback( GtkMenu
*menu
,
4197 gboolean
* WXUNUSED(whatever
),
4198 gpointer user_data
)
4200 // ensure that the menu appears entirely on screen
4202 gtk_widget_get_child_requisition(GTK_WIDGET(menu
), &req
);
4204 wxSize sizeScreen
= wxGetDisplaySize();
4205 wxPoint
*pos
= (wxPoint
*)user_data
;
4207 gint xmax
= sizeScreen
.x
- req
.width
,
4208 ymax
= sizeScreen
.y
- req
.height
;
4210 *x
= pos
->x
< xmax
? pos
->x
: xmax
;
4211 *y
= pos
->y
< ymax
? pos
->y
: ymax
;
4215 bool wxWindowGTK::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
4217 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
4223 GtkMenuPositionFunc posfunc
;
4224 if ( x
== -1 && y
== -1 )
4226 // use GTK's default positioning algorithm
4232 pos
= ClientToScreen(wxPoint(x
, y
));
4234 posfunc
= wxPopupMenuPositionCallback
;
4237 menu
->m_popupShown
= true;
4239 GTK_MENU(menu
->m_menu
),
4240 NULL
, // parent menu shell
4241 NULL
, // parent menu item
4242 posfunc
, // function to position it
4243 userdata
, // client data
4244 0, // button used to activate it
4245 gtk_get_current_event_time()
4248 while (menu
->m_popupShown
)
4250 gtk_main_iteration();
4256 #endif // wxUSE_MENUS_NATIVE
4258 #if wxUSE_DRAG_AND_DROP
4260 void wxWindowGTK::SetDropTarget( wxDropTarget
*dropTarget
)
4262 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
4264 GtkWidget
*dnd_widget
= GetConnectWidget();
4266 if (m_dropTarget
) m_dropTarget
->GtkUnregisterWidget( dnd_widget
);
4268 if (m_dropTarget
) delete m_dropTarget
;
4269 m_dropTarget
= dropTarget
;
4271 if (m_dropTarget
) m_dropTarget
->GtkRegisterWidget( dnd_widget
);
4274 #endif // wxUSE_DRAG_AND_DROP
4276 GtkWidget
* wxWindowGTK::GetConnectWidget()
4278 GtkWidget
*connect_widget
= m_widget
;
4279 if (m_wxwindow
) connect_widget
= m_wxwindow
;
4281 return connect_widget
;
4284 bool wxWindowGTK::GTKIsOwnWindow(GdkWindow
*window
) const
4286 wxArrayGdkWindows windowsThis
;
4287 GdkWindow
* const winThis
= GTKGetWindow(windowsThis
);
4289 return winThis
? window
== winThis
4290 : windowsThis
.Index(window
) != wxNOT_FOUND
;
4293 GdkWindow
*wxWindowGTK::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
4295 return m_wxwindow
? GTKGetDrawingWindow() : gtk_widget_get_window(m_widget
);
4298 bool wxWindowGTK::SetFont( const wxFont
&font
)
4300 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
4302 if (!wxWindowBase::SetFont(font
))
4305 // apply style change (forceStyle=true so that new style is applied
4306 // even if the font changed from valid to wxNullFont):
4307 GTKApplyWidgetStyle(true);
4312 void wxWindowGTK::DoCaptureMouse()
4314 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
4316 GdkWindow
*window
= NULL
;
4318 window
= GTKGetDrawingWindow();
4320 window
= gtk_widget_get_window(GetConnectWidget());
4322 wxCHECK_RET( window
, wxT("CaptureMouse() failed") );
4324 const wxCursor
* cursor
= &m_cursor
;
4325 if (!cursor
->IsOk())
4326 cursor
= wxSTANDARD_CURSOR
;
4328 gdk_pointer_grab( window
, FALSE
,
4330 (GDK_BUTTON_PRESS_MASK
|
4331 GDK_BUTTON_RELEASE_MASK
|
4332 GDK_POINTER_MOTION_HINT_MASK
|
4333 GDK_POINTER_MOTION_MASK
),
4335 cursor
->GetCursor(),
4336 (guint32
)GDK_CURRENT_TIME
);
4337 g_captureWindow
= this;
4338 g_captureWindowHasMouse
= true;
4341 void wxWindowGTK::DoReleaseMouse()
4343 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
4345 wxCHECK_RET( g_captureWindow
, wxT("can't release mouse - not captured") );
4347 g_captureWindow
= NULL
;
4349 GdkWindow
*window
= NULL
;
4351 window
= GTKGetDrawingWindow();
4353 window
= gtk_widget_get_window(GetConnectWidget());
4358 gdk_pointer_ungrab ( (guint32
)GDK_CURRENT_TIME
);
4361 void wxWindowGTK::GTKReleaseMouseAndNotify()
4364 wxMouseCaptureLostEvent
evt(GetId());
4365 evt
.SetEventObject( this );
4366 HandleWindowEvent( evt
);
4370 wxWindow
*wxWindowBase::GetCapture()
4372 return (wxWindow
*)g_captureWindow
;
4375 bool wxWindowGTK::IsRetained() const
4380 void wxWindowGTK::SetScrollbar(int orient
,
4384 bool WXUNUSED(update
))
4386 const int dir
= ScrollDirFromOrient(orient
);
4387 GtkRange
* const sb
= m_scrollBar
[dir
];
4388 wxCHECK_RET( sb
, wxT("this window is not scrollable") );
4392 // GtkRange requires upper > lower
4397 g_signal_handlers_block_by_func(
4398 sb
, (void*)gtk_scrollbar_value_changed
, this);
4400 gtk_range_set_increments(sb
, 1, thumbVisible
);
4401 gtk_adjustment_set_page_size(gtk_range_get_adjustment(sb
), thumbVisible
);
4402 gtk_range_set_range(sb
, 0, range
);
4403 gtk_range_set_value(sb
, pos
);
4404 m_scrollPos
[dir
] = gtk_range_get_value(sb
);
4406 g_signal_handlers_unblock_by_func(
4407 sb
, (void*)gtk_scrollbar_value_changed
, this);
4410 void wxWindowGTK::SetScrollPos(int orient
, int pos
, bool WXUNUSED(refresh
))
4412 const int dir
= ScrollDirFromOrient(orient
);
4413 GtkRange
* const sb
= m_scrollBar
[dir
];
4414 wxCHECK_RET( sb
, wxT("this window is not scrollable") );
4416 // This check is more than an optimization. Without it, the slider
4417 // will not move smoothly while tracking when using wxScrollHelper.
4418 if (GetScrollPos(orient
) != pos
)
4420 g_signal_handlers_block_by_func(
4421 sb
, (void*)gtk_scrollbar_value_changed
, this);
4423 gtk_range_set_value(sb
, pos
);
4424 m_scrollPos
[dir
] = gtk_range_get_value(sb
);
4426 g_signal_handlers_unblock_by_func(
4427 sb
, (void*)gtk_scrollbar_value_changed
, this);
4431 int wxWindowGTK::GetScrollThumb(int orient
) const
4433 GtkRange
* const sb
= m_scrollBar
[ScrollDirFromOrient(orient
)];
4434 wxCHECK_MSG( sb
, 0, wxT("this window is not scrollable") );
4436 return wxRound(gtk_adjustment_get_page_size(gtk_range_get_adjustment(sb
)));
4439 int wxWindowGTK::GetScrollPos( int orient
) const
4441 GtkRange
* const sb
= m_scrollBar
[ScrollDirFromOrient(orient
)];
4442 wxCHECK_MSG( sb
, 0, wxT("this window is not scrollable") );
4444 return wxRound(gtk_range_get_value(sb
));
4447 int wxWindowGTK::GetScrollRange( int orient
) const
4449 GtkRange
* const sb
= m_scrollBar
[ScrollDirFromOrient(orient
)];
4450 wxCHECK_MSG( sb
, 0, wxT("this window is not scrollable") );
4452 return wxRound(gtk_adjustment_get_upper(gtk_range_get_adjustment(sb
)));
4455 // Determine if increment is the same as +/-x, allowing for some small
4456 // difference due to possible inexactness in floating point arithmetic
4457 static inline bool IsScrollIncrement(double increment
, double x
)
4459 wxASSERT(increment
> 0);
4460 const double tolerance
= 1.0 / 1024;
4461 return fabs(increment
- fabs(x
)) < tolerance
;
4464 wxEventType
wxWindowGTK::GTKGetScrollEventType(GtkRange
* range
)
4466 wxASSERT(range
== m_scrollBar
[0] || range
== m_scrollBar
[1]);
4468 const int barIndex
= range
== m_scrollBar
[1];
4470 const double value
= gtk_range_get_value(range
);
4472 // save previous position
4473 const double oldPos
= m_scrollPos
[barIndex
];
4474 // update current position
4475 m_scrollPos
[barIndex
] = value
;
4476 // If event should be ignored, or integral position has not changed
4477 if (!m_hasVMT
|| g_blockEventsOnDrag
|| wxRound(value
) == wxRound(oldPos
))
4482 wxEventType eventType
= wxEVT_SCROLL_THUMBTRACK
;
4485 // Difference from last change event
4486 const double diff
= value
- oldPos
;
4487 const bool isDown
= diff
> 0;
4489 GtkAdjustment
* adj
= gtk_range_get_adjustment(range
);
4490 if (IsScrollIncrement(gtk_adjustment_get_step_increment(adj
), diff
))
4492 eventType
= isDown
? wxEVT_SCROLL_LINEDOWN
: wxEVT_SCROLL_LINEUP
;
4494 else if (IsScrollIncrement(gtk_adjustment_get_page_increment(adj
), diff
))
4496 eventType
= isDown
? wxEVT_SCROLL_PAGEDOWN
: wxEVT_SCROLL_PAGEUP
;
4498 else if (m_mouseButtonDown
)
4500 // Assume track event
4501 m_isScrolling
= true;
4507 void wxWindowGTK::ScrollWindow( int dx
, int dy
, const wxRect
* WXUNUSED(rect
) )
4509 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
4511 wxCHECK_RET( m_wxwindow
!= NULL
, wxT("window needs client area for scrolling") );
4513 // No scrolling requested.
4514 if ((dx
== 0) && (dy
== 0)) return;
4516 m_clipPaintRegion
= true;
4518 WX_PIZZA(m_wxwindow
)->scroll(dx
, dy
);
4520 m_clipPaintRegion
= false;
4523 bool restoreCaret
= (GetCaret() != NULL
&& GetCaret()->IsVisible());
4526 wxRect
caretRect(GetCaret()->GetPosition(), GetCaret()->GetSize());
4528 caretRect
.width
+= dx
;
4531 caretRect
.x
+= dx
; caretRect
.width
-= dx
;
4534 caretRect
.height
+= dy
;
4537 caretRect
.y
+= dy
; caretRect
.height
-= dy
;
4540 RefreshRect(caretRect
);
4542 #endif // wxUSE_CARET
4545 void wxWindowGTK::GTKScrolledWindowSetBorder(GtkWidget
* w
, int wxstyle
)
4547 //RN: Note that static controls usually have no border on gtk, so maybe
4548 //it makes sense to treat that as simply no border at the wx level
4550 if (!(wxstyle
& wxNO_BORDER
) && !(wxstyle
& wxBORDER_STATIC
))
4552 GtkShadowType gtkstyle
;
4554 if(wxstyle
& wxBORDER_RAISED
)
4555 gtkstyle
= GTK_SHADOW_OUT
;
4556 else if ((wxstyle
& wxBORDER_SUNKEN
) || (wxstyle
& wxBORDER_THEME
))
4557 gtkstyle
= GTK_SHADOW_IN
;
4560 else if (wxstyle
& wxBORDER_DOUBLE
)
4561 gtkstyle
= GTK_SHADOW_ETCHED_IN
;
4564 gtkstyle
= GTK_SHADOW_IN
;
4566 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(w
),
4571 // Find the wxWindow at the current mouse position, also returning the mouse
4573 wxWindow
* wxFindWindowAtPointer(wxPoint
& pt
)
4575 pt
= wxGetMousePosition();
4576 wxWindow
* found
= wxFindWindowAtPoint(pt
);
4580 // Get the current mouse position.
4581 wxPoint
wxGetMousePosition()
4583 wxWindow
* tlw
= NULL
;
4584 if (!wxTopLevelWindows
.empty())
4585 tlw
= wxTopLevelWindows
.front();
4586 GdkDisplay
* display
;
4587 if (tlw
&& tlw
->m_widget
)
4588 display
= gtk_widget_get_display(tlw
->m_widget
);
4590 display
= gdk_display_get_default();
4593 gdk_display_get_pointer(display
, NULL
, &x
, &y
, NULL
);
4594 return wxPoint(x
, y
);
4597 GdkWindow
* wxWindowGTK::GTKGetDrawingWindow() const
4599 GdkWindow
* window
= NULL
;
4601 window
= gtk_widget_get_window(m_wxwindow
);
4605 // ----------------------------------------------------------------------------
4607 // ----------------------------------------------------------------------------
4612 // this is called if we attempted to freeze unrealized widget when it finally
4613 // is realized (and so can be frozen):
4614 static void wx_frozen_widget_realize(GtkWidget
* w
, wxWindowGTK
* win
)
4616 wxASSERT( w
&& gtk_widget_get_has_window(w
) );
4617 wxASSERT( gtk_widget_get_realized(w
) );
4619 g_signal_handlers_disconnect_by_func
4622 (void*)wx_frozen_widget_realize
,
4627 if (w
== win
->m_wxwindow
)
4628 window
= win
->GTKGetDrawingWindow();
4630 window
= gtk_widget_get_window(w
);
4631 gdk_window_freeze_updates(window
);
4636 void wxWindowGTK::GTKFreezeWidget(GtkWidget
*w
)
4638 if ( !w
|| !gtk_widget_get_has_window(w
) )
4639 return; // window-less widget, cannot be frozen
4641 GdkWindow
* window
= gtk_widget_get_window(w
);
4644 // we can't thaw unrealized widgets because they don't have GdkWindow,
4645 // so set it up to be done immediately after realization:
4646 g_signal_connect_after
4650 G_CALLBACK(wx_frozen_widget_realize
),
4656 if (w
== m_wxwindow
)
4657 window
= GTKGetDrawingWindow();
4658 gdk_window_freeze_updates(window
);
4661 void wxWindowGTK::GTKThawWidget(GtkWidget
*w
)
4663 if ( !w
|| !gtk_widget_get_has_window(w
) )
4664 return; // window-less widget, cannot be frozen
4666 GdkWindow
* window
= gtk_widget_get_window(w
);
4669 // the widget wasn't realized yet, no need to thaw
4670 g_signal_handlers_disconnect_by_func
4673 (void*)wx_frozen_widget_realize
,
4679 if (w
== m_wxwindow
)
4680 window
= GTKGetDrawingWindow();
4681 gdk_window_thaw_updates(window
);
4684 void wxWindowGTK::DoFreeze()
4686 GTKFreezeWidget(m_widget
);
4687 if ( m_wxwindow
&& m_widget
!= m_wxwindow
)
4688 GTKFreezeWidget(m_wxwindow
);
4691 void wxWindowGTK::DoThaw()
4693 GTKThawWidget(m_widget
);
4694 if ( m_wxwindow
&& m_widget
!= m_wxwindow
)
4695 GTKThawWidget(m_wxwindow
);