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
;
45 #include <gdk/gdkkeysyms.h>
46 #if GTK_CHECK_VERSION(3,0,0)
47 #include <gdk/gdkkeysyms-compat.h>
50 //-----------------------------------------------------------------------------
51 // documentation on internals
52 //-----------------------------------------------------------------------------
55 I have been asked several times about writing some documentation about
56 the GTK port of wxWidgets, especially its internal structures. Obviously,
57 you cannot understand wxGTK without knowing a little about the GTK, but
58 some more information about what the wxWindow, which is the base class
59 for all other window classes, does seems required as well.
63 What does wxWindow do? It contains the common interface for the following
64 jobs of its descendants:
66 1) Define the rudimentary behaviour common to all window classes, such as
67 resizing, intercepting user input (so as to make it possible to use these
68 events for special purposes in a derived class), window names etc.
70 2) Provide the possibility to contain and manage children, if the derived
71 class is allowed to contain children, which holds true for those window
72 classes which do not display a native GTK widget. To name them, these
73 classes are wxPanel, wxScrolledWindow, wxDialog, wxFrame. The MDI frame-
74 work classes are a special case and are handled a bit differently from
75 the rest. The same holds true for the wxNotebook class.
77 3) Provide the possibility to draw into a client area of a window. This,
78 too, only holds true for classes that do not display a native GTK widget
81 4) Provide the entire mechanism for scrolling widgets. This actual inter-
82 face for this is usually in wxScrolledWindow, but the GTK implementation
85 5) A multitude of helper or extra methods for special purposes, such as
86 Drag'n'Drop, managing validators etc.
88 6) Display a border (sunken, raised, simple or none).
90 Normally one might expect, that one wxWidgets window would always correspond
91 to one GTK widget. Under GTK, there is no such all-round widget that has all
92 the functionality. Moreover, the GTK defines a client area as a different
93 widget from the actual widget you are handling. Last but not least some
94 special classes (e.g. wxFrame) handle different categories of widgets and
95 still have the possibility to draw something in the client area.
96 It was therefore required to write a special purpose GTK widget, that would
97 represent a client area in the sense of wxWidgets capable to do the jobs
98 2), 3) and 4). I have written this class and it resides in win_gtk.c of
101 All windows must have a widget, with which they interact with other under-
102 lying GTK widgets. It is this widget, e.g. that has to be resized etc and
103 the wxWindow class has a member variable called m_widget which holds a
104 pointer to this widget. When the window class represents a GTK native widget,
105 this is (in most cases) the only GTK widget the class manages. E.g. the
106 wxStaticText class handles only a GtkLabel widget a pointer to which you
107 can find in m_widget (defined in wxWindow)
109 When the class has a client area for drawing into and for containing children
110 it has to handle the client area widget (of the type wxPizza, defined in
111 win_gtk.cpp), but there could be any number of widgets, handled by a class.
112 The common rule for all windows is only, that the widget that interacts with
113 the rest of GTK must be referenced in m_widget and all other widgets must be
114 children of this widget on the GTK level. The top-most widget, which also
115 represents the client area, must be in the m_wxwindow field and must be of
118 As I said, the window classes that display a GTK native widget only have
119 one widget, so in the case of e.g. the wxButton class m_widget holds a
120 pointer to a GtkButton widget. But windows with client areas (for drawing
121 and children) have a m_widget field that is a pointer to a GtkScrolled-
122 Window and a m_wxwindow field that is pointer to a wxPizza and this
123 one is (in the GTK sense) a child of the GtkScrolledWindow.
125 If the m_wxwindow field is set, then all input to this widget is inter-
126 cepted and sent to the wxWidgets class. If not, all input to the widget
127 that gets pointed to by m_widget gets intercepted and sent to the class.
131 The design of scrolling in wxWidgets is markedly different from that offered
132 by the GTK itself and therefore we cannot simply take it as it is. In GTK,
133 clicking on a scrollbar belonging to scrolled window will inevitably move
134 the window. In wxWidgets, the scrollbar will only emit an event, send this
135 to (normally) a wxScrolledWindow and that class will call ScrollWindow()
136 which actually moves the window and its sub-windows. Note that wxPizza
137 memorizes how much it has been scrolled but that wxWidgets forgets this
138 so that the two coordinates systems have to be kept in synch. This is done
139 in various places using the pizza->m_scroll_x and pizza->m_scroll_y values.
143 Singularly the most broken code in GTK is the code that is supposed to
144 inform subwindows (child windows) about new positions. Very often, duplicate
145 events are sent without changes in size or position, equally often no
146 events are sent at all (All this is due to a bug in the GtkContainer code
147 which got fixed in GTK 1.2.6). For that reason, wxGTK completely ignores
148 GTK's own system and it simply waits for size events for toplevel windows
149 and then iterates down the respective size events to all window. This has
150 the disadvantage that windows might get size events before the GTK widget
151 actually has the reported size. This doesn't normally pose any problem, but
152 the OpenGL drawing routines rely on correct behaviour. Therefore, I have
153 added the m_nativeSizeEvents flag, which is true only for the OpenGL canvas,
154 i.e. the wxGLCanvas will emit a size event, when (and not before) the X11
155 window that is used for OpenGL output really has that size (as reported by
160 If someone at some point of time feels the immense desire to have a look at,
161 change or attempt to optimise the Refresh() logic, this person will need an
162 intimate understanding of what "draw" and "expose" events are and what
163 they are used for, in particular when used in connection with GTK's
164 own windowless widgets. Beware.
168 Cursors, too, have been a constant source of pleasure. The main difficulty
169 is that a GdkWindow inherits a cursor if the programmer sets a new cursor
170 for the parent. To prevent this from doing too much harm, SetCursor calls
171 GTKUpdateCursor, which will recursively re-set the cursors of all child windows.
172 Also don't forget that cursors (like much else) are connected to GdkWindows,
173 not GtkWidgets and that the "window" field of a GtkWidget might very well
174 point to the GdkWindow of the parent widget (-> "window-less widget") and
175 that the two obviously have very different meanings.
178 //-----------------------------------------------------------------------------
180 //-----------------------------------------------------------------------------
182 // Don't allow event propagation during drag
183 bool g_blockEventsOnDrag
;
184 // Don't allow mouse event propagation during scroll
185 bool g_blockEventsOnScroll
;
186 extern wxCursor g_globalCursor
;
188 // mouse capture state: the window which has it and if the mouse is currently
190 static wxWindowGTK
*g_captureWindow
= NULL
;
191 static bool g_captureWindowHasMouse
= false;
193 // The window that currently has focus:
194 static wxWindowGTK
*gs_currentFocus
= NULL
;
195 // The window that is scheduled to get focus in the next event loop iteration
196 // or NULL if there's no pending focus change:
197 static wxWindowGTK
*gs_pendingFocus
= NULL
;
199 // the window that has deferred focus-out event pending, if any (see
200 // GTKAddDeferredFocusOut() for details)
201 static wxWindowGTK
*gs_deferredFocusOut
= NULL
;
203 // global variables because GTK+ DnD want to have the
204 // mouse event that caused it
205 GdkEvent
*g_lastMouseEvent
= NULL
;
206 int g_lastButtonNumber
= 0;
208 //-----------------------------------------------------------------------------
210 //-----------------------------------------------------------------------------
212 // the trace mask used for the focus debugging messages
213 #define TRACE_FOCUS wxT("focus")
215 //-----------------------------------------------------------------------------
216 // "size_request" of m_widget
217 //-----------------------------------------------------------------------------
221 wxgtk_window_size_request_callback(GtkWidget
* WXUNUSED(widget
),
222 GtkRequisition
*requisition
,
226 win
->GetSize( &w
, &h
);
232 requisition
->height
= h
;
233 requisition
->width
= w
;
237 //-----------------------------------------------------------------------------
238 // "expose_event" of m_wxwindow
239 //-----------------------------------------------------------------------------
243 gtk_window_expose_callback( GtkWidget
*,
244 GdkEventExpose
*gdk_event
,
247 if (gdk_event
->window
== win
->GTKGetDrawingWindow())
249 win
->GetUpdateRegion() = wxRegion( gdk_event
->region
);
250 win
->GtkSendPaintEvents();
252 // Let parent window draw window-less widgets
257 #ifndef __WXUNIVERSAL__
258 //-----------------------------------------------------------------------------
259 // "expose_event" from m_wxwindow->parent, for drawing border
260 //-----------------------------------------------------------------------------
264 expose_event_border(GtkWidget
* widget
, GdkEventExpose
* gdk_event
, wxWindow
* win
)
266 if (gdk_event
->window
!= gtk_widget_get_parent_window(win
->m_wxwindow
))
273 gtk_widget_get_allocation(win
->m_wxwindow
, &alloc
);
274 const int x
= alloc
.x
;
275 const int y
= alloc
.y
;
276 const int w
= alloc
.width
;
277 const int h
= alloc
.height
;
279 if (w
<= 0 || h
<= 0)
282 if (win
->HasFlag(wxBORDER_SIMPLE
))
284 gdk_draw_rectangle(gdk_event
->window
,
285 gtk_widget_get_style(widget
)->black_gc
, false, x
, y
, w
- 1, h
- 1);
289 GtkShadowType shadow
= GTK_SHADOW_IN
;
290 if (win
->HasFlag(wxBORDER_RAISED
))
291 shadow
= GTK_SHADOW_OUT
;
293 // Style detail to use
295 if (win
->m_widget
== win
->m_wxwindow
)
296 // for non-scrollable wxWindows
299 // for scrollable ones
302 // clip rect is required to avoid painting background
303 // over upper left (w,h) of parent window
304 GdkRectangle clipRect
= { x
, y
, w
, h
};
306 gtk_widget_get_style(win
->m_wxwindow
), gdk_event
->window
, GTK_STATE_NORMAL
,
307 shadow
, &clipRect
, wxGTKPrivate::GetEntryWidget(), detail
, x
, y
, w
, h
);
313 //-----------------------------------------------------------------------------
314 // "parent_set" from m_wxwindow
315 //-----------------------------------------------------------------------------
319 parent_set(GtkWidget
* widget
, GtkWidget
* old_parent
, wxWindow
* win
)
323 g_signal_handlers_disconnect_by_func(
324 old_parent
, (void*)expose_event_border
, win
);
326 GtkWidget
* parent
= gtk_widget_get_parent(widget
);
329 g_signal_connect_after(parent
, "expose_event",
330 G_CALLBACK(expose_event_border
), win
);
334 #endif // !__WXUNIVERSAL__
336 //-----------------------------------------------------------------------------
337 // "key_press_event" from any window
338 //-----------------------------------------------------------------------------
340 // These are used when transforming Ctrl-alpha to ascii values 1-26
341 inline bool wxIsLowerChar(int code
)
343 return (code
>= 'a' && code
<= 'z' );
346 inline bool wxIsUpperChar(int code
)
348 return (code
>= 'A' && code
<= 'Z' );
352 // set WXTRACE to this to see the key event codes on the console
353 #define TRACE_KEYS wxT("keyevent")
355 // translates an X key symbol to WXK_XXX value
357 // if isChar is true it means that the value returned will be used for EVT_CHAR
358 // event and then we choose the logical WXK_XXX, i.e. '/' for GDK_KP_Divide,
359 // for example, while if it is false it means that the value is going to be
360 // used for KEY_DOWN/UP events and then we translate GDK_KP_Divide to
362 static long wxTranslateKeySymToWXKey(KeySym keysym
, bool isChar
)
368 // Shift, Control and Alt don't generate the CHAR events at all
371 key_code
= isChar
? 0 : WXK_SHIFT
;
375 key_code
= isChar
? 0 : WXK_CONTROL
;
383 key_code
= isChar
? 0 : WXK_ALT
;
386 // neither do the toggle modifies
387 case GDK_Scroll_Lock
:
388 key_code
= isChar
? 0 : WXK_SCROLL
;
392 key_code
= isChar
? 0 : WXK_CAPITAL
;
396 key_code
= isChar
? 0 : WXK_NUMLOCK
;
400 // various other special keys
413 case GDK_ISO_Left_Tab
:
420 key_code
= WXK_RETURN
;
424 key_code
= WXK_CLEAR
;
428 key_code
= WXK_PAUSE
;
432 key_code
= WXK_SELECT
;
436 key_code
= WXK_PRINT
;
440 key_code
= WXK_EXECUTE
;
444 key_code
= WXK_ESCAPE
;
447 // cursor and other extended keyboard keys
449 key_code
= WXK_DELETE
;
465 key_code
= WXK_RIGHT
;
472 case GDK_Prior
: // == GDK_Page_Up
473 key_code
= WXK_PAGEUP
;
476 case GDK_Next
: // == GDK_Page_Down
477 key_code
= WXK_PAGEDOWN
;
489 key_code
= WXK_INSERT
;
504 key_code
= (isChar
? '0' : int(WXK_NUMPAD0
)) + keysym
- GDK_KP_0
;
508 key_code
= isChar
? ' ' : int(WXK_NUMPAD_SPACE
);
512 key_code
= isChar
? WXK_TAB
: WXK_NUMPAD_TAB
;
516 key_code
= isChar
? WXK_RETURN
: WXK_NUMPAD_ENTER
;
520 key_code
= isChar
? WXK_F1
: WXK_NUMPAD_F1
;
524 key_code
= isChar
? WXK_F2
: WXK_NUMPAD_F2
;
528 key_code
= isChar
? WXK_F3
: WXK_NUMPAD_F3
;
532 key_code
= isChar
? WXK_F4
: WXK_NUMPAD_F4
;
536 key_code
= isChar
? WXK_HOME
: WXK_NUMPAD_HOME
;
540 key_code
= isChar
? WXK_LEFT
: WXK_NUMPAD_LEFT
;
544 key_code
= isChar
? WXK_UP
: WXK_NUMPAD_UP
;
548 key_code
= isChar
? WXK_RIGHT
: WXK_NUMPAD_RIGHT
;
552 key_code
= isChar
? WXK_DOWN
: WXK_NUMPAD_DOWN
;
555 case GDK_KP_Prior
: // == GDK_KP_Page_Up
556 key_code
= isChar
? WXK_PAGEUP
: WXK_NUMPAD_PAGEUP
;
559 case GDK_KP_Next
: // == GDK_KP_Page_Down
560 key_code
= isChar
? WXK_PAGEDOWN
: WXK_NUMPAD_PAGEDOWN
;
564 key_code
= isChar
? WXK_END
: WXK_NUMPAD_END
;
568 key_code
= isChar
? WXK_HOME
: WXK_NUMPAD_BEGIN
;
572 key_code
= isChar
? WXK_INSERT
: WXK_NUMPAD_INSERT
;
576 key_code
= isChar
? WXK_DELETE
: WXK_NUMPAD_DELETE
;
580 key_code
= isChar
? '=' : int(WXK_NUMPAD_EQUAL
);
583 case GDK_KP_Multiply
:
584 key_code
= isChar
? '*' : int(WXK_NUMPAD_MULTIPLY
);
588 key_code
= isChar
? '+' : int(WXK_NUMPAD_ADD
);
591 case GDK_KP_Separator
:
592 // FIXME: what is this?
593 key_code
= isChar
? '.' : int(WXK_NUMPAD_SEPARATOR
);
596 case GDK_KP_Subtract
:
597 key_code
= isChar
? '-' : int(WXK_NUMPAD_SUBTRACT
);
601 key_code
= isChar
? '.' : int(WXK_NUMPAD_DECIMAL
);
605 key_code
= isChar
? '/' : int(WXK_NUMPAD_DIVIDE
);
622 key_code
= WXK_F1
+ keysym
- GDK_F1
;
632 static inline bool wxIsAsciiKeysym(KeySym ks
)
637 static void wxFillOtherKeyEventFields(wxKeyEvent
& event
,
639 GdkEventKey
*gdk_event
)
641 event
.SetTimestamp( gdk_event
->time
);
642 event
.SetId(win
->GetId());
644 event
.m_shiftDown
= (gdk_event
->state
& GDK_SHIFT_MASK
) != 0;
645 event
.m_controlDown
= (gdk_event
->state
& GDK_CONTROL_MASK
) != 0;
646 event
.m_altDown
= (gdk_event
->state
& GDK_MOD1_MASK
) != 0;
647 event
.m_metaDown
= (gdk_event
->state
& GDK_META_MASK
) != 0;
649 // Normally we take the state of modifiers directly from the low level GDK
650 // event but unfortunately GDK uses a different convention from MSW for the
651 // key events corresponding to the modifier keys themselves: in it, when
652 // e.g. Shift key is pressed, GDK_SHIFT_MASK is not set while it is set
653 // when Shift is released. Under MSW the situation is exactly reversed and
654 // the modifier corresponding to the key is set when it is pressed and
655 // unset when it is released. To ensure consistent behaviour between
656 // platforms (and because it seems to make slightly more sense, although
657 // arguably both behaviours are reasonable) we follow MSW here.
659 // Final notice: we set the flags to the desired value instead of just
660 // inverting them because they are not set correctly (i.e. in the same way
661 // as for the real events generated by the user) for wxUIActionSimulator-
662 // produced events and it seems better to keep that class code the same
663 // among all platforms and fix the discrepancy here instead of adding
664 // wxGTK-specific code to wxUIActionSimulator.
665 const bool isPress
= gdk_event
->type
== GDK_KEY_PRESS
;
666 switch ( gdk_event
->keyval
)
670 event
.m_shiftDown
= isPress
;
675 event
.m_controlDown
= isPress
;
680 event
.m_altDown
= isPress
;
687 event
.m_metaDown
= isPress
;
691 event
.m_rawCode
= (wxUint32
) gdk_event
->keyval
;
692 event
.m_rawFlags
= gdk_event
->hardware_keycode
;
694 wxGetMousePosition(&event
.m_x
, &event
.m_y
);
695 win
->ScreenToClient(&event
.m_x
, &event
.m_y
);
696 event
.SetEventObject( win
);
701 wxTranslateGTKKeyEventToWx(wxKeyEvent
& event
,
703 GdkEventKey
*gdk_event
)
705 // VZ: it seems that GDK_KEY_RELEASE event doesn't set event->string
706 // but only event->keyval which is quite useless to us, so remember
707 // the last character from GDK_KEY_PRESS and reuse it as last resort
709 // NB: should be MT-safe as we're always called from the main thread only
714 } s_lastKeyPress
= { 0, 0 };
716 KeySym keysym
= gdk_event
->keyval
;
718 wxLogTrace(TRACE_KEYS
, wxT("Key %s event: keysym = %ld"),
719 event
.GetEventType() == wxEVT_KEY_UP
? wxT("release")
723 long key_code
= wxTranslateKeySymToWXKey(keysym
, false /* !isChar */);
727 // do we have the translation or is it a plain ASCII character?
728 if ( (gdk_event
->length
== 1) || wxIsAsciiKeysym(keysym
) )
730 // we should use keysym if it is ASCII as X does some translations
731 // like "I pressed while Control is down" => "Ctrl-I" == "TAB"
732 // which we don't want here (but which we do use for OnChar())
733 if ( !wxIsAsciiKeysym(keysym
) )
735 keysym
= (KeySym
)gdk_event
->string
[0];
738 // we want to always get the same key code when the same key is
739 // pressed regardless of the state of the modifiers, i.e. on a
740 // standard US keyboard pressing '5' or '%' ('5' key with
741 // Shift) should result in the same key code in OnKeyDown():
742 // '5' (although OnChar() will get either '5' or '%').
744 // to do it we first translate keysym to keycode (== scan code)
745 // and then back but always using the lower register
746 Display
*dpy
= (Display
*)wxGetDisplay();
747 KeyCode keycode
= XKeysymToKeycode(dpy
, keysym
);
749 wxLogTrace(TRACE_KEYS
, wxT("\t-> keycode %d"), keycode
);
751 KeySym keysymNormalized
= XKeycodeToKeysym(dpy
, keycode
, 0);
753 // use the normalized, i.e. lower register, keysym if we've
755 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
) )
851 } // anonymous namespace
855 gtk_window_key_press_callback( GtkWidget
*WXUNUSED(widget
),
856 GdkEventKey
*gdk_event
,
861 if (g_blockEventsOnDrag
)
864 wxKeyEvent
event( wxEVT_KEY_DOWN
);
866 bool return_after_IM
= false;
868 if( wxTranslateGTKKeyEventToWx(event
, win
, gdk_event
) )
870 // Send the CHAR_HOOK event first
871 if ( SendCharHookEvent(event
, win
) )
873 // Don't do anything at all with this event any more.
877 // Emit KEY_DOWN event
878 ret
= win
->HandleWindowEvent( event
);
882 // Return after IM processing as we cannot do
883 // anything with it anyhow.
884 return_after_IM
= true;
887 if (!ret
&& win
->m_imData
)
889 win
->m_imData
->lastKeyEvent
= gdk_event
;
891 // We should let GTK+ IM filter key event first. According to GTK+ 2.0 API
892 // docs, if IM filter returns true, no further processing should be done.
893 // we should send the key_down event anyway.
894 bool intercepted_by_IM
= gtk_im_context_filter_keypress(win
->m_imData
->context
, gdk_event
);
895 win
->m_imData
->lastKeyEvent
= NULL
;
896 if (intercepted_by_IM
)
898 wxLogTrace(TRACE_KEYS
, wxT("Key event intercepted by IM"));
909 wxWindowGTK
*ancestor
= win
;
912 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
915 wxCommandEvent
menu_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
916 ret
= ancestor
->HandleWindowEvent( menu_event
);
920 // if the accelerator wasn't handled as menu event, try
921 // it as button click (for compatibility with other
923 wxCommandEvent
button_event( wxEVT_COMMAND_BUTTON_CLICKED
, command
);
924 ret
= ancestor
->HandleWindowEvent( button_event
);
929 if (ancestor
->IsTopLevel())
931 ancestor
= ancestor
->GetParent();
934 #endif // wxUSE_ACCEL
936 // Only send wxEVT_CHAR event if not processed yet. Thus, ALT-x
937 // will only be sent if it is not in an accelerator table.
941 KeySym keysym
= gdk_event
->keyval
;
942 // Find key code for EVT_CHAR and EVT_CHAR_HOOK events
943 key_code
= wxTranslateKeySymToWXKey(keysym
, true /* isChar */);
946 if ( wxIsAsciiKeysym(keysym
) )
949 key_code
= (unsigned char)keysym
;
951 // gdk_event->string is actually deprecated
952 else if ( gdk_event
->length
== 1 )
954 key_code
= (unsigned char)gdk_event
->string
[0];
960 wxKeyEvent
eventChar(wxEVT_CHAR
, event
);
962 wxLogTrace(TRACE_KEYS
, wxT("Char event: %ld"), key_code
);
964 eventChar
.m_keyCode
= key_code
;
966 // To conform to the docs we need to translate Ctrl-alpha
967 // characters to values in the range 1-26.
968 if ( eventChar
.ControlDown() &&
969 ( wxIsLowerChar(key_code
) || wxIsUpperChar(key_code
) ))
971 if ( wxIsLowerChar(key_code
) )
972 eventChar
.m_keyCode
= key_code
- 'a' + 1;
973 if ( wxIsUpperChar(key_code
) )
974 eventChar
.m_keyCode
= key_code
- 'A' + 1;
976 eventChar
.m_uniChar
= event
.m_keyCode
;
980 ret
= win
->HandleWindowEvent(eventChar
);
990 gtk_wxwindow_commit_cb (GtkIMContext
* WXUNUSED(context
),
994 wxKeyEvent
event( wxEVT_CHAR
);
996 // take modifiers, cursor position, timestamp etc. from the last
997 // key_press_event that was fed into Input Method:
998 if (window
->m_imData
->lastKeyEvent
)
1000 wxFillOtherKeyEventFields(event
,
1001 window
, window
->m_imData
->lastKeyEvent
);
1005 event
.SetEventObject( window
);
1008 const wxString
data(wxGTK_CONV_BACK_SYS(str
));
1012 for( wxString::const_iterator pstr
= data
.begin(); pstr
!= data
.end(); ++pstr
)
1015 event
.m_uniChar
= *pstr
;
1016 // Backward compatible for ISO-8859-1
1017 event
.m_keyCode
= *pstr
< 256 ? event
.m_uniChar
: 0;
1018 wxLogTrace(TRACE_KEYS
, wxT("IM sent character '%c'"), event
.m_uniChar
);
1020 event
.m_keyCode
= (char)*pstr
;
1021 #endif // wxUSE_UNICODE
1023 // To conform to the docs we need to translate Ctrl-alpha
1024 // characters to values in the range 1-26.
1025 if ( event
.ControlDown() &&
1026 ( wxIsLowerChar(*pstr
) || wxIsUpperChar(*pstr
) ))
1028 if ( wxIsLowerChar(*pstr
) )
1029 event
.m_keyCode
= *pstr
- 'a' + 1;
1030 if ( wxIsUpperChar(*pstr
) )
1031 event
.m_keyCode
= *pstr
- 'A' + 1;
1033 event
.m_keyCode
= *pstr
- 'a' + 1;
1035 event
.m_uniChar
= event
.m_keyCode
;
1039 window
->HandleWindowEvent(event
);
1045 //-----------------------------------------------------------------------------
1046 // "key_release_event" from any window
1047 //-----------------------------------------------------------------------------
1051 gtk_window_key_release_callback( GtkWidget
* WXUNUSED(widget
),
1052 GdkEventKey
*gdk_event
,
1058 if (g_blockEventsOnDrag
)
1061 wxKeyEvent
event( wxEVT_KEY_UP
);
1062 if ( !wxTranslateGTKKeyEventToWx(event
, win
, gdk_event
) )
1064 // unknown key pressed, ignore (the event would be useless anyhow)
1068 return win
->GTKProcessEvent(event
);
1072 // ============================================================================
1074 // ============================================================================
1076 // ----------------------------------------------------------------------------
1077 // mouse event processing helpers
1078 // ----------------------------------------------------------------------------
1080 static void AdjustEventButtonState(wxMouseEvent
& event
)
1082 // GDK reports the old state of the button for a button press event, but
1083 // for compatibility with MSW and common sense we want m_leftDown be TRUE
1084 // for a LEFT_DOWN event, not FALSE, so we will invert
1085 // left/right/middleDown for the corresponding click events
1087 if ((event
.GetEventType() == wxEVT_LEFT_DOWN
) ||
1088 (event
.GetEventType() == wxEVT_LEFT_DCLICK
) ||
1089 (event
.GetEventType() == wxEVT_LEFT_UP
))
1091 event
.m_leftDown
= !event
.m_leftDown
;
1095 if ((event
.GetEventType() == wxEVT_MIDDLE_DOWN
) ||
1096 (event
.GetEventType() == wxEVT_MIDDLE_DCLICK
) ||
1097 (event
.GetEventType() == wxEVT_MIDDLE_UP
))
1099 event
.m_middleDown
= !event
.m_middleDown
;
1103 if ((event
.GetEventType() == wxEVT_RIGHT_DOWN
) ||
1104 (event
.GetEventType() == wxEVT_RIGHT_DCLICK
) ||
1105 (event
.GetEventType() == wxEVT_RIGHT_UP
))
1107 event
.m_rightDown
= !event
.m_rightDown
;
1111 if ((event
.GetEventType() == wxEVT_AUX1_DOWN
) ||
1112 (event
.GetEventType() == wxEVT_AUX1_DCLICK
))
1114 event
.m_aux1Down
= true;
1118 if ((event
.GetEventType() == wxEVT_AUX2_DOWN
) ||
1119 (event
.GetEventType() == wxEVT_AUX2_DCLICK
))
1121 event
.m_aux2Down
= true;
1126 // find the window to send the mouse event too
1128 wxWindowGTK
*FindWindowForMouseEvent(wxWindowGTK
*win
, wxCoord
& x
, wxCoord
& y
)
1133 if (win
->m_wxwindow
)
1135 wxPizza
* pizza
= WX_PIZZA(win
->m_wxwindow
);
1136 xx
+= pizza
->m_scroll_x
;
1137 yy
+= pizza
->m_scroll_y
;
1140 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
1143 wxWindowGTK
*child
= node
->GetData();
1145 node
= node
->GetNext();
1146 if (!child
->IsShown())
1149 if (child
->GTKIsTransparentForMouse())
1151 // wxStaticBox is transparent in the box itself
1152 int xx1
= child
->m_x
;
1153 int yy1
= child
->m_y
;
1154 int xx2
= child
->m_x
+ child
->m_width
;
1155 int yy2
= child
->m_y
+ child
->m_height
;
1158 if (((xx
>= xx1
) && (xx
<= xx1
+10) && (yy
>= yy1
) && (yy
<= yy2
)) ||
1160 ((xx
>= xx2
-10) && (xx
<= xx2
) && (yy
>= yy1
) && (yy
<= yy2
)) ||
1162 ((xx
>= xx1
) && (xx
<= xx2
) && (yy
>= yy1
) && (yy
<= yy1
+10)) ||
1164 ((xx
>= xx1
) && (xx
<= xx2
) && (yy
>= yy2
-1) && (yy
<= yy2
)))
1175 if ((child
->m_wxwindow
== NULL
) &&
1176 (child
->m_x
<= xx
) &&
1177 (child
->m_y
<= yy
) &&
1178 (child
->m_x
+child
->m_width
>= xx
) &&
1179 (child
->m_y
+child
->m_height
>= yy
))
1192 // ----------------------------------------------------------------------------
1193 // common event handlers helpers
1194 // ----------------------------------------------------------------------------
1196 bool wxWindowGTK::GTKProcessEvent(wxEvent
& event
) const
1198 // nothing special at this level
1199 return HandleWindowEvent(event
);
1202 bool wxWindowGTK::GTKShouldIgnoreEvent() const
1204 return !m_hasVMT
|| g_blockEventsOnDrag
;
1207 int wxWindowGTK::GTKCallbackCommonPrologue(GdkEventAny
*event
) const
1211 if (g_blockEventsOnDrag
)
1213 if (g_blockEventsOnScroll
)
1216 if (!GTKIsOwnWindow(event
->window
))
1222 // overloads for all GDK event types we use here: we need to have this as
1223 // GdkEventXXX can't be implicitly cast to GdkEventAny even if it, in fact,
1224 // derives from it in the sense that the structs have the same layout
1225 #define wxDEFINE_COMMON_PROLOGUE_OVERLOAD(T) \
1226 static int wxGtkCallbackCommonPrologue(T *event, wxWindowGTK *win) \
1228 return win->GTKCallbackCommonPrologue((GdkEventAny *)event); \
1231 wxDEFINE_COMMON_PROLOGUE_OVERLOAD(GdkEventButton
)
1232 wxDEFINE_COMMON_PROLOGUE_OVERLOAD(GdkEventMotion
)
1233 wxDEFINE_COMMON_PROLOGUE_OVERLOAD(GdkEventCrossing
)
1235 #undef wxDEFINE_COMMON_PROLOGUE_OVERLOAD
1237 #define wxCOMMON_CALLBACK_PROLOGUE(event, win) \
1238 const int rc = wxGtkCallbackCommonPrologue(event, win); \
1242 // all event handlers must have C linkage as they're called from GTK+ C code
1246 //-----------------------------------------------------------------------------
1247 // "button_press_event"
1248 //-----------------------------------------------------------------------------
1251 gtk_window_button_press_callback( GtkWidget
*widget
,
1252 GdkEventButton
*gdk_event
,
1255 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1257 g_lastButtonNumber
= gdk_event
->button
;
1259 // GDK sends surplus button down events
1260 // before a double click event. We
1261 // need to filter these out.
1262 if ((gdk_event
->type
== GDK_BUTTON_PRESS
) && (win
->m_wxwindow
))
1264 GdkEvent
*peek_event
= gdk_event_peek();
1267 if ((peek_event
->type
== GDK_2BUTTON_PRESS
) ||
1268 (peek_event
->type
== GDK_3BUTTON_PRESS
))
1270 gdk_event_free( peek_event
);
1275 gdk_event_free( peek_event
);
1280 wxEventType event_type
= wxEVT_NULL
;
1282 if ( gdk_event
->type
== GDK_2BUTTON_PRESS
&&
1283 gdk_event
->button
>= 1 && gdk_event
->button
<= 3 )
1285 // Reset GDK internal timestamp variables in order to disable GDK
1286 // triple click events. GDK will then next time believe no button has
1287 // been clicked just before, and send a normal button click event.
1288 GdkDisplay
* display
= gtk_widget_get_display (widget
);
1289 display
->button_click_time
[1] = 0;
1290 display
->button_click_time
[0] = 0;
1293 if (gdk_event
->button
== 1)
1295 // note that GDK generates triple click events which are not supported
1296 // by wxWidgets but still have to be passed to the app as otherwise
1297 // clicks would simply go missing
1298 switch (gdk_event
->type
)
1300 // we shouldn't get triple clicks at all for GTK2 because we
1301 // suppress them artificially using the code above but we still
1302 // should map them to something for GTK1 and not just ignore them
1303 // as this would lose clicks
1304 case GDK_3BUTTON_PRESS
: // we could also map this to DCLICK...
1305 case GDK_BUTTON_PRESS
:
1306 event_type
= wxEVT_LEFT_DOWN
;
1309 case GDK_2BUTTON_PRESS
:
1310 event_type
= wxEVT_LEFT_DCLICK
;
1314 // just to silence gcc warnings
1318 else if (gdk_event
->button
== 2)
1320 switch (gdk_event
->type
)
1322 case GDK_3BUTTON_PRESS
:
1323 case GDK_BUTTON_PRESS
:
1324 event_type
= wxEVT_MIDDLE_DOWN
;
1327 case GDK_2BUTTON_PRESS
:
1328 event_type
= wxEVT_MIDDLE_DCLICK
;
1335 else if (gdk_event
->button
== 3)
1337 switch (gdk_event
->type
)
1339 case GDK_3BUTTON_PRESS
:
1340 case GDK_BUTTON_PRESS
:
1341 event_type
= wxEVT_RIGHT_DOWN
;
1344 case GDK_2BUTTON_PRESS
:
1345 event_type
= wxEVT_RIGHT_DCLICK
;
1353 else if (gdk_event
->button
== 8)
1355 switch (gdk_event
->type
)
1357 case GDK_3BUTTON_PRESS
:
1358 case GDK_BUTTON_PRESS
:
1359 event_type
= wxEVT_AUX1_DOWN
;
1362 case GDK_2BUTTON_PRESS
:
1363 event_type
= wxEVT_AUX1_DCLICK
;
1371 else if (gdk_event
->button
== 9)
1373 switch (gdk_event
->type
)
1375 case GDK_3BUTTON_PRESS
:
1376 case GDK_BUTTON_PRESS
:
1377 event_type
= wxEVT_AUX2_DOWN
;
1380 case GDK_2BUTTON_PRESS
:
1381 event_type
= wxEVT_AUX2_DCLICK
;
1389 if ( event_type
== wxEVT_NULL
)
1391 // unknown mouse button or click type
1395 g_lastMouseEvent
= (GdkEvent
*) gdk_event
;
1397 wxMouseEvent
event( event_type
);
1398 InitMouseEvent( win
, event
, gdk_event
);
1400 AdjustEventButtonState(event
);
1402 // find the correct window to send the event to: it may be a different one
1403 // from the one which got it at GTK+ level because some controls don't have
1404 // their own X window and thus cannot get any events.
1405 if ( !g_captureWindow
)
1406 win
= FindWindowForMouseEvent(win
, event
.m_x
, event
.m_y
);
1408 // reset the event object and id in case win changed.
1409 event
.SetEventObject( win
);
1410 event
.SetId( win
->GetId() );
1412 bool ret
= win
->GTKProcessEvent( event
);
1413 g_lastMouseEvent
= NULL
;
1417 if ((event_type
== wxEVT_LEFT_DOWN
) && !win
->IsOfStandardClass() &&
1418 (gs_currentFocus
!= win
) /* && win->IsFocusable() */)
1423 if (event_type
== wxEVT_RIGHT_DOWN
)
1425 // generate a "context menu" event: this is similar to right mouse
1426 // click under many GUIs except that it is generated differently
1427 // (right up under MSW, ctrl-click under Mac, right down here) and
1429 // (a) it's a command event and so is propagated to the parent
1430 // (b) under some ports it can be generated from kbd too
1431 // (c) it uses screen coords (because of (a))
1432 wxContextMenuEvent
evtCtx(
1435 win
->ClientToScreen(event
.GetPosition()));
1436 evtCtx
.SetEventObject(win
);
1437 return win
->GTKProcessEvent(evtCtx
);
1443 //-----------------------------------------------------------------------------
1444 // "button_release_event"
1445 //-----------------------------------------------------------------------------
1448 gtk_window_button_release_callback( GtkWidget
*WXUNUSED(widget
),
1449 GdkEventButton
*gdk_event
,
1452 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1454 g_lastButtonNumber
= 0;
1456 wxEventType event_type
= wxEVT_NULL
;
1458 switch (gdk_event
->button
)
1461 event_type
= wxEVT_LEFT_UP
;
1465 event_type
= wxEVT_MIDDLE_UP
;
1469 event_type
= wxEVT_RIGHT_UP
;
1473 event_type
= wxEVT_AUX1_UP
;
1477 event_type
= wxEVT_AUX2_UP
;
1481 // unknown button, don't process
1485 g_lastMouseEvent
= (GdkEvent
*) gdk_event
;
1487 wxMouseEvent
event( event_type
);
1488 InitMouseEvent( win
, event
, gdk_event
);
1490 AdjustEventButtonState(event
);
1492 if ( !g_captureWindow
)
1493 win
= FindWindowForMouseEvent(win
, event
.m_x
, event
.m_y
);
1495 // reset the event object and id in case win changed.
1496 event
.SetEventObject( win
);
1497 event
.SetId( win
->GetId() );
1499 bool ret
= win
->GTKProcessEvent(event
);
1501 g_lastMouseEvent
= NULL
;
1506 //-----------------------------------------------------------------------------
1507 // "motion_notify_event"
1508 //-----------------------------------------------------------------------------
1511 gtk_window_motion_notify_callback( GtkWidget
* WXUNUSED(widget
),
1512 GdkEventMotion
*gdk_event
,
1515 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1517 if (gdk_event
->is_hint
)
1521 GdkModifierType state
;
1522 gdk_window_get_pointer(gdk_event
->window
, &x
, &y
, &state
);
1527 g_lastMouseEvent
= (GdkEvent
*) gdk_event
;
1529 wxMouseEvent
event( wxEVT_MOTION
);
1530 InitMouseEvent(win
, event
, gdk_event
);
1532 if ( g_captureWindow
)
1534 // synthesise a mouse enter or leave event if needed
1535 GdkWindow
*winUnderMouse
= gdk_window_at_pointer(NULL
, NULL
);
1536 // This seems to be necessary and actually been added to
1537 // GDK itself in version 2.0.X
1540 bool hasMouse
= winUnderMouse
== gdk_event
->window
;
1541 if ( hasMouse
!= g_captureWindowHasMouse
)
1543 // the mouse changed window
1544 g_captureWindowHasMouse
= hasMouse
;
1546 wxMouseEvent
eventM(g_captureWindowHasMouse
? wxEVT_ENTER_WINDOW
1547 : wxEVT_LEAVE_WINDOW
);
1548 InitMouseEvent(win
, eventM
, gdk_event
);
1549 eventM
.SetEventObject(win
);
1550 win
->GTKProcessEvent(eventM
);
1555 win
= FindWindowForMouseEvent(win
, event
.m_x
, event
.m_y
);
1557 // reset the event object and id in case win changed.
1558 event
.SetEventObject( win
);
1559 event
.SetId( win
->GetId() );
1562 if ( !g_captureWindow
)
1564 wxSetCursorEvent
cevent( event
.m_x
, event
.m_y
);
1565 if (win
->GTKProcessEvent( cevent
))
1567 win
->SetCursor( cevent
.GetCursor() );
1571 bool ret
= win
->GTKProcessEvent(event
);
1573 g_lastMouseEvent
= NULL
;
1578 //-----------------------------------------------------------------------------
1579 // "scroll_event" (mouse wheel event)
1580 //-----------------------------------------------------------------------------
1583 window_scroll_event_hscrollbar(GtkWidget
*, GdkEventScroll
* gdk_event
, wxWindow
* win
)
1585 if (gdk_event
->direction
!= GDK_SCROLL_LEFT
&&
1586 gdk_event
->direction
!= GDK_SCROLL_RIGHT
)
1591 GtkRange
*range
= win
->m_scrollBar
[wxWindow::ScrollDir_Horz
];
1593 if (range
&& gtk_widget_get_visible(GTK_WIDGET(range
)))
1595 GtkAdjustment
* adj
= gtk_range_get_adjustment(range
);
1596 double delta
= gtk_adjustment_get_step_increment(adj
) * 3;
1597 if (gdk_event
->direction
== GDK_SCROLL_LEFT
)
1600 gtk_range_set_value(range
, gtk_adjustment_get_value(adj
) + delta
);
1609 window_scroll_event(GtkWidget
*, GdkEventScroll
* gdk_event
, wxWindow
* win
)
1611 if (gdk_event
->direction
!= GDK_SCROLL_UP
&&
1612 gdk_event
->direction
!= GDK_SCROLL_DOWN
)
1617 wxMouseEvent
event(wxEVT_MOUSEWHEEL
);
1618 InitMouseEvent(win
, event
, gdk_event
);
1620 // FIXME: Get these values from GTK or GDK
1621 event
.m_linesPerAction
= 3;
1622 event
.m_wheelDelta
= 120;
1623 if (gdk_event
->direction
== GDK_SCROLL_UP
)
1624 event
.m_wheelRotation
= 120;
1626 event
.m_wheelRotation
= -120;
1628 if (win
->GTKProcessEvent(event
))
1631 GtkRange
*range
= win
->m_scrollBar
[wxWindow::ScrollDir_Vert
];
1633 if (range
&& gtk_widget_get_visible(GTK_WIDGET(range
)))
1635 GtkAdjustment
* adj
= gtk_range_get_adjustment(range
);
1636 double delta
= gtk_adjustment_get_step_increment(adj
) * 3;
1637 if (gdk_event
->direction
== GDK_SCROLL_UP
)
1640 gtk_range_set_value(range
, gtk_adjustment_get_value(adj
) + delta
);
1648 //-----------------------------------------------------------------------------
1650 //-----------------------------------------------------------------------------
1652 static gboolean
wxgtk_window_popup_menu_callback(GtkWidget
*, wxWindowGTK
* win
)
1654 wxContextMenuEvent
event(wxEVT_CONTEXT_MENU
, win
->GetId(), wxPoint(-1, -1));
1655 event
.SetEventObject(win
);
1656 return win
->GTKProcessEvent(event
);
1659 //-----------------------------------------------------------------------------
1661 //-----------------------------------------------------------------------------
1664 gtk_window_focus_in_callback( GtkWidget
* WXUNUSED(widget
),
1665 GdkEventFocus
*WXUNUSED(event
),
1668 return win
->GTKHandleFocusIn();
1671 //-----------------------------------------------------------------------------
1672 // "focus_out_event"
1673 //-----------------------------------------------------------------------------
1676 gtk_window_focus_out_callback( GtkWidget
* WXUNUSED(widget
),
1677 GdkEventFocus
* WXUNUSED(gdk_event
),
1680 return win
->GTKHandleFocusOut();
1683 //-----------------------------------------------------------------------------
1685 //-----------------------------------------------------------------------------
1688 wx_window_focus_callback(GtkWidget
*widget
,
1689 GtkDirectionType
WXUNUSED(direction
),
1692 // the default handler for focus signal in GtkScrolledWindow sets
1693 // focus to the window itself even if it doesn't accept focus, i.e. has no
1694 // GTK_CAN_FOCUS in its style -- work around this by forcibly preventing
1695 // the signal from reaching gtk_scrolled_window_focus() if we don't have
1696 // any children which might accept focus (we know we don't accept the focus
1697 // ourselves as this signal is only connected in this case)
1698 if ( win
->GetChildren().empty() )
1699 g_signal_stop_emission_by_name(widget
, "focus");
1701 // we didn't change the focus
1705 //-----------------------------------------------------------------------------
1706 // "enter_notify_event"
1707 //-----------------------------------------------------------------------------
1710 gtk_window_enter_callback( GtkWidget
*widget
,
1711 GdkEventCrossing
*gdk_event
,
1714 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1716 // Event was emitted after a grab
1717 if (gdk_event
->mode
!= GDK_CROSSING_NORMAL
) return FALSE
;
1721 GdkModifierType state
= (GdkModifierType
)0;
1723 gdk_window_get_pointer(gtk_widget_get_window(widget
), &x
, &y
, &state
);
1725 wxMouseEvent
event( wxEVT_ENTER_WINDOW
);
1726 InitMouseEvent(win
, event
, gdk_event
);
1727 wxPoint pt
= win
->GetClientAreaOrigin();
1728 event
.m_x
= x
+ pt
.x
;
1729 event
.m_y
= y
+ pt
.y
;
1731 if ( !g_captureWindow
)
1733 wxSetCursorEvent
cevent( event
.m_x
, event
.m_y
);
1734 if (win
->GTKProcessEvent( cevent
))
1736 win
->SetCursor( cevent
.GetCursor() );
1740 return win
->GTKProcessEvent(event
);
1743 //-----------------------------------------------------------------------------
1744 // "leave_notify_event"
1745 //-----------------------------------------------------------------------------
1748 gtk_window_leave_callback( GtkWidget
*widget
,
1749 GdkEventCrossing
*gdk_event
,
1752 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1754 // Event was emitted after an ungrab
1755 if (gdk_event
->mode
!= GDK_CROSSING_NORMAL
) return FALSE
;
1757 wxMouseEvent
event( wxEVT_LEAVE_WINDOW
);
1761 GdkModifierType state
= (GdkModifierType
)0;
1763 gdk_window_get_pointer(gtk_widget_get_window(widget
), &x
, &y
, &state
);
1765 InitMouseEvent(win
, event
, gdk_event
);
1767 return win
->GTKProcessEvent(event
);
1770 //-----------------------------------------------------------------------------
1771 // "value_changed" from scrollbar
1772 //-----------------------------------------------------------------------------
1775 gtk_scrollbar_value_changed(GtkRange
* range
, wxWindow
* win
)
1777 wxEventType eventType
= win
->GTKGetScrollEventType(range
);
1778 if (eventType
!= wxEVT_NULL
)
1780 // Convert scroll event type to scrollwin event type
1781 eventType
+= wxEVT_SCROLLWIN_TOP
- wxEVT_SCROLL_TOP
;
1783 // find the scrollbar which generated the event
1784 wxWindowGTK::ScrollDir dir
= win
->ScrollDirFromRange(range
);
1786 // generate the corresponding wx event
1787 const int orient
= wxWindow::OrientFromScrollDir(dir
);
1788 wxScrollWinEvent
event(eventType
, win
->GetScrollPos(orient
), orient
);
1789 event
.SetEventObject(win
);
1791 win
->GTKProcessEvent(event
);
1795 //-----------------------------------------------------------------------------
1796 // "button_press_event" from scrollbar
1797 //-----------------------------------------------------------------------------
1800 gtk_scrollbar_button_press_event(GtkRange
*, GdkEventButton
*, wxWindow
* win
)
1802 g_blockEventsOnScroll
= true;
1803 win
->m_mouseButtonDown
= true;
1808 //-----------------------------------------------------------------------------
1809 // "event_after" from scrollbar
1810 //-----------------------------------------------------------------------------
1813 gtk_scrollbar_event_after(GtkRange
* range
, GdkEvent
* event
, wxWindow
* win
)
1815 if (event
->type
== GDK_BUTTON_RELEASE
)
1817 g_signal_handlers_block_by_func(range
, (void*)gtk_scrollbar_event_after
, win
);
1819 const int orient
= wxWindow::OrientFromScrollDir(
1820 win
->ScrollDirFromRange(range
));
1821 wxScrollWinEvent
evt(wxEVT_SCROLLWIN_THUMBRELEASE
,
1822 win
->GetScrollPos(orient
), orient
);
1823 evt
.SetEventObject(win
);
1824 win
->GTKProcessEvent(evt
);
1828 //-----------------------------------------------------------------------------
1829 // "button_release_event" from scrollbar
1830 //-----------------------------------------------------------------------------
1833 gtk_scrollbar_button_release_event(GtkRange
* range
, GdkEventButton
*, wxWindow
* win
)
1835 g_blockEventsOnScroll
= false;
1836 win
->m_mouseButtonDown
= false;
1837 // If thumb tracking
1838 if (win
->m_isScrolling
)
1840 win
->m_isScrolling
= false;
1841 // Hook up handler to send thumb release event after this emission is finished.
1842 // To allow setting scroll position from event handler, sending event must
1843 // be deferred until after the GtkRange handler for this signal has run
1844 g_signal_handlers_unblock_by_func(range
, (void*)gtk_scrollbar_event_after
, win
);
1850 //-----------------------------------------------------------------------------
1851 // "realize" from m_widget
1852 //-----------------------------------------------------------------------------
1855 gtk_window_realized_callback(GtkWidget
* WXUNUSED(widget
), wxWindowGTK
* win
)
1857 win
->GTKHandleRealized();
1860 //-----------------------------------------------------------------------------
1861 // "unrealize" from m_wxwindow
1862 //-----------------------------------------------------------------------------
1864 static void unrealize(GtkWidget
*, wxWindowGTK
* win
)
1867 gtk_im_context_set_client_window(win
->m_imData
->context
, NULL
);
1870 //-----------------------------------------------------------------------------
1871 // "size_allocate" from m_wxwindow or m_widget
1872 //-----------------------------------------------------------------------------
1875 size_allocate(GtkWidget
*, GtkAllocation
* alloc
, wxWindow
* win
)
1877 int w
= alloc
->width
;
1878 int h
= alloc
->height
;
1879 if (win
->m_wxwindow
)
1881 int border_x
, border_y
;
1882 WX_PIZZA(win
->m_wxwindow
)->get_border_widths(border_x
, border_y
);
1888 if (win
->m_oldClientWidth
!= w
|| win
->m_oldClientHeight
!= h
)
1890 win
->m_oldClientWidth
= w
;
1891 win
->m_oldClientHeight
= h
;
1892 // this callback can be connected to m_wxwindow,
1893 // so always get size from m_widget->allocation
1895 gtk_widget_get_allocation(win
->m_widget
, &a
);
1896 win
->m_width
= a
.width
;
1897 win
->m_height
= a
.height
;
1898 if (!win
->m_nativeSizeEvent
)
1900 wxSizeEvent
event(win
->GetSize(), win
->GetId());
1901 event
.SetEventObject(win
);
1902 win
->GTKProcessEvent(event
);
1907 //-----------------------------------------------------------------------------
1909 //-----------------------------------------------------------------------------
1911 #if GTK_CHECK_VERSION(2, 8, 0)
1913 gtk_window_grab_broken( GtkWidget
*,
1914 GdkEventGrabBroken
*event
,
1917 // Mouse capture has been lost involuntarily, notify the application
1918 if(!event
->keyboard
&& wxWindow::GetCapture() == win
)
1920 wxMouseCaptureLostEvent
evt( win
->GetId() );
1921 evt
.SetEventObject( win
);
1922 win
->HandleWindowEvent( evt
);
1928 //-----------------------------------------------------------------------------
1930 //-----------------------------------------------------------------------------
1933 void gtk_window_style_set_callback( GtkWidget
*WXUNUSED(widget
),
1934 GtkStyle
*previous_style
,
1937 if (win
&& previous_style
)
1939 wxSysColourChangedEvent event
;
1940 event
.SetEventObject(win
);
1942 win
->GTKProcessEvent( event
);
1948 void wxWindowGTK::GTKHandleRealized()
1952 gtk_im_context_set_client_window
1955 m_wxwindow
? GTKGetDrawingWindow()
1956 : gtk_widget_get_window(m_widget
)
1960 // We cannot set colours and fonts before the widget
1961 // been realized, so we do this directly after realization
1962 // or otherwise in idle time
1964 if (m_needsStyleChange
)
1966 SetBackgroundStyle(GetBackgroundStyle());
1967 m_needsStyleChange
= false;
1970 wxWindowCreateEvent
event(static_cast<wxWindow
*>(this));
1971 event
.SetEventObject( this );
1972 GTKProcessEvent( event
);
1974 GTKUpdateCursor(true, false);
1977 // ----------------------------------------------------------------------------
1978 // this wxWindowBase function is implemented here (in platform-specific file)
1979 // because it is static and so couldn't be made virtual
1980 // ----------------------------------------------------------------------------
1982 wxWindow
*wxWindowBase::DoFindFocus()
1984 wxWindowGTK
*focus
= gs_pendingFocus
? gs_pendingFocus
: gs_currentFocus
;
1985 // the cast is necessary when we compile in wxUniversal mode
1986 return static_cast<wxWindow
*>(focus
);
1989 void wxWindowGTK::AddChildGTK(wxWindowGTK
* child
)
1991 wxASSERT_MSG(m_wxwindow
, "Cannot add a child to a window without a client area");
1993 // the window might have been scrolled already, we
1994 // have to adapt the position
1995 wxPizza
* pizza
= WX_PIZZA(m_wxwindow
);
1996 child
->m_x
+= pizza
->m_scroll_x
;
1997 child
->m_y
+= pizza
->m_scroll_y
;
1999 gtk_widget_set_size_request(
2000 child
->m_widget
, child
->m_width
, child
->m_height
);
2001 pizza
->put(child
->m_widget
, child
->m_x
, child
->m_y
);
2004 //-----------------------------------------------------------------------------
2006 //-----------------------------------------------------------------------------
2008 wxWindow
*wxGetActiveWindow()
2010 return wxWindow::FindFocus();
2014 wxMouseState
wxGetMouseState()
2020 GdkModifierType mask
;
2022 gdk_window_get_pointer(NULL
, &x
, &y
, &mask
);
2026 ms
.SetLeftDown((mask
& GDK_BUTTON1_MASK
) != 0);
2027 ms
.SetMiddleDown((mask
& GDK_BUTTON2_MASK
) != 0);
2028 ms
.SetRightDown((mask
& GDK_BUTTON3_MASK
) != 0);
2029 // see the comment in InitMouseEvent()
2030 ms
.SetAux1Down((mask
& GDK_BUTTON4_MASK
) != 0);
2031 ms
.SetAux2Down((mask
& GDK_BUTTON5_MASK
) != 0);
2033 ms
.SetControlDown((mask
& GDK_CONTROL_MASK
) != 0);
2034 ms
.SetShiftDown((mask
& GDK_SHIFT_MASK
) != 0);
2035 ms
.SetAltDown((mask
& GDK_MOD1_MASK
) != 0);
2036 ms
.SetMetaDown((mask
& GDK_META_MASK
) != 0);
2041 //-----------------------------------------------------------------------------
2043 //-----------------------------------------------------------------------------
2045 // in wxUniv/MSW this class is abstract because it doesn't have DoPopupMenu()
2047 #ifdef __WXUNIVERSAL__
2048 IMPLEMENT_ABSTRACT_CLASS(wxWindowGTK
, wxWindowBase
)
2049 #endif // __WXUNIVERSAL__
2051 void wxWindowGTK::Init()
2056 m_focusWidget
= NULL
;
2066 m_showOnIdle
= false;
2069 m_nativeSizeEvent
= false;
2071 m_isScrolling
= false;
2072 m_mouseButtonDown
= false;
2074 // initialize scrolling stuff
2075 for ( int dir
= 0; dir
< ScrollDir_Max
; dir
++ )
2077 m_scrollBar
[dir
] = NULL
;
2078 m_scrollPos
[dir
] = 0;
2082 m_oldClientHeight
= 0;
2084 m_clipPaintRegion
= false;
2086 m_needsStyleChange
= false;
2088 m_cursor
= *wxSTANDARD_CURSOR
;
2091 m_dirtyTabOrder
= false;
2094 wxWindowGTK::wxWindowGTK()
2099 wxWindowGTK::wxWindowGTK( wxWindow
*parent
,
2104 const wxString
&name
)
2108 Create( parent
, id
, pos
, size
, style
, name
);
2111 bool wxWindowGTK::Create( wxWindow
*parent
,
2116 const wxString
&name
)
2118 // Get default border
2119 wxBorder border
= GetBorder(style
);
2121 style
&= ~wxBORDER_MASK
;
2124 if (!PreCreation( parent
, pos
, size
) ||
2125 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
2127 wxFAIL_MSG( wxT("wxWindowGTK creation failed") );
2131 // We should accept the native look
2133 GtkScrolledWindowClass
*scroll_class
= GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) );
2134 scroll_class
->scrollbar_spacing
= 0;
2138 m_wxwindow
= wxPizza::New(m_windowStyle
);
2139 #ifndef __WXUNIVERSAL__
2140 if (HasFlag(wxPizza::BORDER_STYLES
))
2142 g_signal_connect(m_wxwindow
, "parent_set",
2143 G_CALLBACK(parent_set
), this);
2146 if (!HasFlag(wxHSCROLL
) && !HasFlag(wxVSCROLL
))
2147 m_widget
= m_wxwindow
;
2150 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
2152 GtkScrolledWindow
*scrolledWindow
= GTK_SCROLLED_WINDOW(m_widget
);
2154 // There is a conflict with default bindings at GTK+
2155 // level between scrolled windows and notebooks both of which want to use
2156 // Ctrl-PageUp/Down: scrolled windows for scrolling in the horizontal
2157 // direction and notebooks for changing pages -- we decide that if we don't
2158 // have wxHSCROLL style we can safely sacrifice horizontal scrolling if it
2159 // means we can get working keyboard navigation in notebooks
2160 if ( !HasFlag(wxHSCROLL
) )
2163 bindings
= gtk_binding_set_by_class(G_OBJECT_GET_CLASS(m_widget
));
2166 gtk_binding_entry_remove(bindings
, GDK_Page_Up
, GDK_CONTROL_MASK
);
2167 gtk_binding_entry_remove(bindings
, GDK_Page_Down
, GDK_CONTROL_MASK
);
2171 if (HasFlag(wxALWAYS_SHOW_SB
))
2173 gtk_scrolled_window_set_policy( scrolledWindow
, GTK_POLICY_ALWAYS
, GTK_POLICY_ALWAYS
);
2177 gtk_scrolled_window_set_policy( scrolledWindow
, GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
2180 m_scrollBar
[ScrollDir_Horz
] = GTK_RANGE(gtk_scrolled_window_get_hscrollbar(scrolledWindow
));
2181 m_scrollBar
[ScrollDir_Vert
] = GTK_RANGE(gtk_scrolled_window_get_vscrollbar(scrolledWindow
));
2182 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2183 gtk_range_set_inverted( m_scrollBar
[ScrollDir_Horz
], TRUE
);
2185 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
2187 // connect various scroll-related events
2188 for ( int dir
= 0; dir
< ScrollDir_Max
; dir
++ )
2190 // these handlers block mouse events to any window during scrolling
2191 // such as motion events and prevent GTK and wxWidgets from fighting
2192 // over where the slider should be
2193 g_signal_connect(m_scrollBar
[dir
], "button_press_event",
2194 G_CALLBACK(gtk_scrollbar_button_press_event
), this);
2195 g_signal_connect(m_scrollBar
[dir
], "button_release_event",
2196 G_CALLBACK(gtk_scrollbar_button_release_event
), this);
2198 gulong handler_id
= g_signal_connect(m_scrollBar
[dir
], "event_after",
2199 G_CALLBACK(gtk_scrollbar_event_after
), this);
2200 g_signal_handler_block(m_scrollBar
[dir
], handler_id
);
2202 // these handlers get notified when scrollbar slider moves
2203 g_signal_connect_after(m_scrollBar
[dir
], "value_changed",
2204 G_CALLBACK(gtk_scrollbar_value_changed
), this);
2207 gtk_widget_show( m_wxwindow
);
2209 g_object_ref(m_widget
);
2212 m_parent
->DoAddChild( this );
2214 m_focusWidget
= m_wxwindow
;
2216 SetCanFocus(AcceptsFocus());
2223 wxWindowGTK::~wxWindowGTK()
2227 if (gs_currentFocus
== this)
2228 gs_currentFocus
= NULL
;
2229 if (gs_pendingFocus
== this)
2230 gs_pendingFocus
= NULL
;
2232 if ( gs_deferredFocusOut
== this )
2233 gs_deferredFocusOut
= NULL
;
2237 // destroy children before destroying this window itself
2240 // unhook focus handlers to prevent stray events being
2241 // propagated to this (soon to be) dead object
2242 if (m_focusWidget
!= NULL
)
2244 g_signal_handlers_disconnect_by_func (m_focusWidget
,
2245 (gpointer
) gtk_window_focus_in_callback
,
2247 g_signal_handlers_disconnect_by_func (m_focusWidget
,
2248 (gpointer
) gtk_window_focus_out_callback
,
2255 // delete before the widgets to avoid a crash on solaris
2259 // avoid problem with GTK+ 2.18 where a frozen window causes the whole
2260 // TLW to be frozen, and if the window is then destroyed, nothing ever
2261 // gets painted again
2267 // Note that gtk_widget_destroy() does not destroy the widget, it just
2268 // emits the "destroy" signal. The widget is not actually destroyed
2269 // until its reference count drops to zero.
2270 gtk_widget_destroy(m_widget
);
2271 // Release our reference, should be the last one
2272 g_object_unref(m_widget
);
2278 bool wxWindowGTK::PreCreation( wxWindowGTK
*parent
, const wxPoint
&pos
, const wxSize
&size
)
2280 if ( GTKNeedsParent() )
2282 wxCHECK_MSG( parent
, false, wxT("Must have non-NULL parent") );
2285 // Use either the given size, or the default if -1 is given.
2286 // See wxWindowBase for these functions.
2287 m_width
= WidthDefault(size
.x
) ;
2288 m_height
= HeightDefault(size
.y
);
2296 void wxWindowGTK::PostCreation()
2298 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid window") );
2304 // these get reported to wxWidgets -> wxPaintEvent
2306 g_signal_connect (m_wxwindow
, "expose_event",
2307 G_CALLBACK (gtk_window_expose_callback
), this);
2309 if (GetLayoutDirection() == wxLayout_LeftToRight
)
2310 gtk_widget_set_redraw_on_allocate(m_wxwindow
, HasFlag(wxFULL_REPAINT_ON_RESIZE
));
2313 // Create input method handler
2314 m_imData
= new wxGtkIMData
;
2316 // Cannot handle drawing preedited text yet
2317 gtk_im_context_set_use_preedit( m_imData
->context
, FALSE
);
2319 g_signal_connect (m_imData
->context
, "commit",
2320 G_CALLBACK (gtk_wxwindow_commit_cb
), this);
2321 g_signal_connect(m_wxwindow
, "unrealize", G_CALLBACK(unrealize
), this);
2326 if (!GTK_IS_WINDOW(m_widget
))
2328 if (m_focusWidget
== NULL
)
2329 m_focusWidget
= m_widget
;
2333 g_signal_connect (m_focusWidget
, "focus_in_event",
2334 G_CALLBACK (gtk_window_focus_in_callback
), this);
2335 g_signal_connect (m_focusWidget
, "focus_out_event",
2336 G_CALLBACK (gtk_window_focus_out_callback
), this);
2340 g_signal_connect_after (m_focusWidget
, "focus_in_event",
2341 G_CALLBACK (gtk_window_focus_in_callback
), this);
2342 g_signal_connect_after (m_focusWidget
, "focus_out_event",
2343 G_CALLBACK (gtk_window_focus_out_callback
), this);
2347 if ( !AcceptsFocusFromKeyboard() )
2351 g_signal_connect(m_widget
, "focus",
2352 G_CALLBACK(wx_window_focus_callback
), this);
2355 // connect to the various key and mouse handlers
2357 GtkWidget
*connect_widget
= GetConnectWidget();
2359 ConnectWidget( connect_widget
);
2361 // We cannot set colours, fonts and cursors before the widget has been
2362 // realized, so we do this directly after realization -- unless the widget
2363 // was in fact realized already.
2364 if ( gtk_widget_get_realized(connect_widget
) )
2366 gtk_window_realized_callback(connect_widget
, this);
2370 g_signal_connect (connect_widget
, "realize",
2371 G_CALLBACK (gtk_window_realized_callback
), this);
2376 g_signal_connect(m_wxwindow
? m_wxwindow
: m_widget
, "size_allocate",
2377 G_CALLBACK(size_allocate
), this);
2380 #if GTK_CHECK_VERSION(2, 8, 0)
2381 if ( gtk_check_version(2,8,0) == NULL
)
2383 // Make sure we can notify the app when mouse capture is lost
2386 g_signal_connect (m_wxwindow
, "grab_broken_event",
2387 G_CALLBACK (gtk_window_grab_broken
), this);
2390 if ( connect_widget
!= m_wxwindow
)
2392 g_signal_connect (connect_widget
, "grab_broken_event",
2393 G_CALLBACK (gtk_window_grab_broken
), this);
2396 #endif // GTK+ >= 2.8
2398 if ( GTKShouldConnectSizeRequest() )
2400 // This is needed if we want to add our windows into native
2401 // GTK controls, such as the toolbar. With this callback, the
2402 // toolbar gets to know the correct size (the one set by the
2403 // programmer). Sadly, it misbehaves for wxComboBox.
2404 g_signal_connect (m_widget
, "size_request",
2405 G_CALLBACK (wxgtk_window_size_request_callback
),
2409 InheritAttributes();
2413 SetLayoutDirection(wxLayout_Default
);
2415 // unless the window was created initially hidden (i.e. Hide() had been
2416 // called before Create()), we should show it at GTK+ level as well
2418 gtk_widget_show( m_widget
);
2422 wxWindowGTK::GTKConnectWidget(const char *signal
, wxGTKCallback callback
)
2424 return g_signal_connect(m_widget
, signal
, callback
, this);
2427 void wxWindowGTK::ConnectWidget( GtkWidget
*widget
)
2429 g_signal_connect (widget
, "key_press_event",
2430 G_CALLBACK (gtk_window_key_press_callback
), this);
2431 g_signal_connect (widget
, "key_release_event",
2432 G_CALLBACK (gtk_window_key_release_callback
), this);
2433 g_signal_connect (widget
, "button_press_event",
2434 G_CALLBACK (gtk_window_button_press_callback
), this);
2435 g_signal_connect (widget
, "button_release_event",
2436 G_CALLBACK (gtk_window_button_release_callback
), this);
2437 g_signal_connect (widget
, "motion_notify_event",
2438 G_CALLBACK (gtk_window_motion_notify_callback
), this);
2440 g_signal_connect (widget
, "scroll_event",
2441 G_CALLBACK (window_scroll_event
), this);
2442 if (m_scrollBar
[ScrollDir_Horz
])
2443 g_signal_connect (m_scrollBar
[ScrollDir_Horz
], "scroll_event",
2444 G_CALLBACK (window_scroll_event_hscrollbar
), this);
2445 if (m_scrollBar
[ScrollDir_Vert
])
2446 g_signal_connect (m_scrollBar
[ScrollDir_Vert
], "scroll_event",
2447 G_CALLBACK (window_scroll_event
), this);
2449 g_signal_connect (widget
, "popup_menu",
2450 G_CALLBACK (wxgtk_window_popup_menu_callback
), this);
2451 g_signal_connect (widget
, "enter_notify_event",
2452 G_CALLBACK (gtk_window_enter_callback
), this);
2453 g_signal_connect (widget
, "leave_notify_event",
2454 G_CALLBACK (gtk_window_leave_callback
), this);
2456 if (IsTopLevel() && m_wxwindow
)
2457 g_signal_connect (m_wxwindow
, "style_set",
2458 G_CALLBACK (gtk_window_style_set_callback
), this);
2461 bool wxWindowGTK::Destroy()
2465 return wxWindowBase::Destroy();
2468 void wxWindowGTK::DoMoveWindow(int x
, int y
, int width
, int height
)
2470 gtk_widget_set_size_request(m_widget
, width
, height
);
2472 // inform the parent to perform the move
2473 wxASSERT_MSG(m_parent
&& m_parent
->m_wxwindow
,
2474 "the parent window has no client area?");
2475 WX_PIZZA(m_parent
->m_wxwindow
)->move(m_widget
, x
, y
);
2478 void wxWindowGTK::ConstrainSize()
2481 // GPE's window manager doesn't like size hints at all, esp. when the user
2482 // has to use the virtual keyboard, so don't constrain size there
2486 const wxSize minSize
= GetMinSize();
2487 const wxSize maxSize
= GetMaxSize();
2488 if (minSize
.x
> 0 && m_width
< minSize
.x
) m_width
= minSize
.x
;
2489 if (minSize
.y
> 0 && m_height
< minSize
.y
) m_height
= minSize
.y
;
2490 if (maxSize
.x
> 0 && m_width
> maxSize
.x
) m_width
= maxSize
.x
;
2491 if (maxSize
.y
> 0 && m_height
> maxSize
.y
) m_height
= maxSize
.y
;
2495 void wxWindowGTK::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
2497 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid window") );
2498 wxASSERT_MSG( (m_parent
!= NULL
), wxT("wxWindowGTK::SetSize requires parent.\n") );
2500 if ((sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) == 0 && (x
== -1 || y
== -1))
2502 int currentX
, currentY
;
2503 GetPosition(¤tX
, ¤tY
);
2509 AdjustForParentClientOrigin(x
, y
, sizeFlags
);
2511 // calculate the best size if we should auto size the window
2512 if ( ((sizeFlags
& wxSIZE_AUTO_WIDTH
) && width
== -1) ||
2513 ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) && height
== -1) )
2515 const wxSize sizeBest
= GetBestSize();
2516 if ( (sizeFlags
& wxSIZE_AUTO_WIDTH
) && width
== -1 )
2518 if ( (sizeFlags
& wxSIZE_AUTO_HEIGHT
) && height
== -1 )
2519 height
= sizeBest
.y
;
2522 const wxSize
oldSize(m_width
, m_height
);
2528 if (m_parent
->m_wxwindow
)
2530 wxPizza
* pizza
= WX_PIZZA(m_parent
->m_wxwindow
);
2531 m_x
= x
+ pizza
->m_scroll_x
;
2532 m_y
= y
+ pizza
->m_scroll_y
;
2534 int left_border
= 0;
2535 int right_border
= 0;
2537 int bottom_border
= 0;
2539 /* the default button has a border around it */
2540 if (gtk_widget_get_can_default(m_widget
))
2542 GtkBorder
*default_border
= NULL
;
2543 gtk_widget_style_get( m_widget
, "default_border", &default_border
, NULL
);
2546 left_border
+= default_border
->left
;
2547 right_border
+= default_border
->right
;
2548 top_border
+= default_border
->top
;
2549 bottom_border
+= default_border
->bottom
;
2550 gtk_border_free( default_border
);
2554 DoMoveWindow( m_x
- left_border
,
2556 m_width
+left_border
+right_border
,
2557 m_height
+top_border
+bottom_border
);
2560 if (m_width
!= oldSize
.x
|| m_height
!= oldSize
.y
)
2562 // update these variables to keep size_allocate handler
2563 // from sending another size event for this change
2564 GetClientSize( &m_oldClientWidth
, &m_oldClientHeight
);
2566 gtk_widget_queue_resize(m_widget
);
2567 if (!m_nativeSizeEvent
)
2569 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
2570 event
.SetEventObject( this );
2571 HandleWindowEvent( event
);
2574 if (sizeFlags
& wxSIZE_FORCE_EVENT
)
2576 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
2577 event
.SetEventObject( this );
2578 HandleWindowEvent( event
);
2582 bool wxWindowGTK::GTKShowFromOnIdle()
2584 if (IsShown() && m_showOnIdle
&& !gtk_widget_get_visible (m_widget
))
2586 GtkAllocation alloc
;
2589 alloc
.width
= m_width
;
2590 alloc
.height
= m_height
;
2591 gtk_widget_size_allocate( m_widget
, &alloc
);
2592 gtk_widget_show( m_widget
);
2593 wxShowEvent
eventShow(GetId(), true);
2594 eventShow
.SetEventObject(this);
2595 HandleWindowEvent(eventShow
);
2596 m_showOnIdle
= false;
2603 void wxWindowGTK::OnInternalIdle()
2605 if ( gs_deferredFocusOut
)
2606 GTKHandleDeferredFocusOut();
2608 // Check if we have to show window now
2609 if (GTKShowFromOnIdle()) return;
2611 if ( m_dirtyTabOrder
)
2613 m_dirtyTabOrder
= false;
2617 // Update style if the window was not yet realized when
2618 // SetBackgroundStyle() was called
2619 if (m_needsStyleChange
)
2621 SetBackgroundStyle(GetBackgroundStyle());
2622 m_needsStyleChange
= false;
2625 wxWindowBase::OnInternalIdle();
2628 void wxWindowGTK::DoGetSize( int *width
, int *height
) const
2630 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2632 if (width
) (*width
) = m_width
;
2633 if (height
) (*height
) = m_height
;
2636 void wxWindowGTK::DoSetClientSize( int width
, int height
)
2638 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2640 const wxSize size
= GetSize();
2641 const wxSize clientSize
= GetClientSize();
2642 SetSize(width
+ (size
.x
- clientSize
.x
), height
+ (size
.y
- clientSize
.y
));
2645 void wxWindowGTK::DoGetClientSize( int *width
, int *height
) const
2647 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2654 // if window is scrollable, account for scrollbars
2655 if ( GTK_IS_SCROLLED_WINDOW(m_widget
) )
2657 GtkPolicyType policy
[ScrollDir_Max
];
2658 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(m_widget
),
2659 &policy
[ScrollDir_Horz
],
2660 &policy
[ScrollDir_Vert
]);
2662 for ( int i
= 0; i
< ScrollDir_Max
; i
++ )
2664 // don't account for the scrollbars we don't have
2665 GtkRange
* const range
= m_scrollBar
[i
];
2669 // nor for the ones we have but don't current show
2670 switch ( policy
[i
] )
2672 case GTK_POLICY_NEVER
:
2673 // never shown so doesn't take any place
2676 case GTK_POLICY_ALWAYS
:
2677 // no checks necessary
2680 case GTK_POLICY_AUTOMATIC
:
2681 // may be shown or not, check
2682 GtkAdjustment
*adj
= gtk_range_get_adjustment(range
);
2683 if (gtk_adjustment_get_upper(adj
) <= gtk_adjustment_get_page_size(adj
))
2687 GtkScrolledWindowClass
*scroll_class
=
2688 GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) );
2691 gtk_widget_size_request(GTK_WIDGET(range
), &req
);
2692 if (i
== ScrollDir_Horz
)
2693 h
-= req
.height
+ scroll_class
->scrollbar_spacing
;
2695 w
-= req
.width
+ scroll_class
->scrollbar_spacing
;
2699 const wxSize sizeBorders
= DoGetBorderSize();
2709 if (width
) *width
= w
;
2710 if (height
) *height
= h
;
2713 wxSize
wxWindowGTK::DoGetBorderSize() const
2716 return wxWindowBase::DoGetBorderSize();
2719 WX_PIZZA(m_wxwindow
)->get_border_widths(x
, y
);
2721 return 2*wxSize(x
, y
);
2724 void wxWindowGTK::DoGetPosition( int *x
, int *y
) const
2726 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2730 if (!IsTopLevel() && m_parent
&& m_parent
->m_wxwindow
)
2732 wxPizza
* pizza
= WX_PIZZA(m_parent
->m_wxwindow
);
2733 dx
= pizza
->m_scroll_x
;
2734 dy
= pizza
->m_scroll_y
;
2737 if (m_x
== -1 && m_y
== -1)
2739 GdkWindow
*source
= NULL
;
2741 source
= gtk_widget_get_window(m_wxwindow
);
2743 source
= gtk_widget_get_window(m_widget
);
2749 gdk_window_get_origin( source
, &org_x
, &org_y
);
2752 m_parent
->ScreenToClient(&org_x
, &org_y
);
2754 const_cast<wxWindowGTK
*>(this)->m_x
= org_x
;
2755 const_cast<wxWindowGTK
*>(this)->m_y
= org_y
;
2759 if (x
) (*x
) = m_x
- dx
;
2760 if (y
) (*y
) = m_y
- dy
;
2763 void wxWindowGTK::DoClientToScreen( int *x
, int *y
) const
2765 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2767 if (gtk_widget_get_window(m_widget
) == NULL
) return;
2769 GdkWindow
*source
= NULL
;
2771 source
= gtk_widget_get_window(m_wxwindow
);
2773 source
= gtk_widget_get_window(m_widget
);
2777 gdk_window_get_origin( source
, &org_x
, &org_y
);
2781 if (!gtk_widget_get_has_window(m_widget
))
2784 gtk_widget_get_allocation(m_widget
, &a
);
2793 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2794 *x
= (GetClientSize().x
- *x
) + org_x
;
2802 void wxWindowGTK::DoScreenToClient( int *x
, int *y
) const
2804 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2806 if (!gtk_widget_get_realized(m_widget
)) return;
2808 GdkWindow
*source
= NULL
;
2810 source
= gtk_widget_get_window(m_wxwindow
);
2812 source
= gtk_widget_get_window(m_widget
);
2816 gdk_window_get_origin( source
, &org_x
, &org_y
);
2820 if (!gtk_widget_get_has_window(m_widget
))
2823 gtk_widget_get_allocation(m_widget
, &a
);
2831 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2832 *x
= (GetClientSize().x
- *x
) - org_x
;
2839 bool wxWindowGTK::Show( bool show
)
2841 if ( !wxWindowBase::Show(show
) )
2847 // notice that we may call Hide() before the window is created and this is
2848 // actually useful to create it hidden initially -- but we can't call
2849 // Show() before it is created
2852 wxASSERT_MSG( !show
, "can't show invalid window" );
2860 // defer until later
2864 gtk_widget_show(m_widget
);
2868 gtk_widget_hide(m_widget
);
2871 wxShowEvent
eventShow(GetId(), show
);
2872 eventShow
.SetEventObject(this);
2873 HandleWindowEvent(eventShow
);
2878 void wxWindowGTK::DoEnable( bool enable
)
2880 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2882 gtk_widget_set_sensitive( m_widget
, enable
);
2883 if (m_wxwindow
&& (m_wxwindow
!= m_widget
))
2884 gtk_widget_set_sensitive( m_wxwindow
, enable
);
2887 int wxWindowGTK::GetCharHeight() const
2889 wxCHECK_MSG( (m_widget
!= NULL
), 12, wxT("invalid window") );
2891 wxFont font
= GetFont();
2892 wxCHECK_MSG( font
.IsOk(), 12, wxT("invalid font") );
2894 PangoContext
* context
= gtk_widget_get_pango_context(m_widget
);
2899 PangoFontDescription
*desc
= font
.GetNativeFontInfo()->description
;
2900 PangoLayout
*layout
= pango_layout_new(context
);
2901 pango_layout_set_font_description(layout
, desc
);
2902 pango_layout_set_text(layout
, "H", 1);
2903 PangoLayoutLine
*line
= (PangoLayoutLine
*)pango_layout_get_lines(layout
)->data
;
2905 PangoRectangle rect
;
2906 pango_layout_line_get_extents(line
, NULL
, &rect
);
2908 g_object_unref (layout
);
2910 return (int) PANGO_PIXELS(rect
.height
);
2913 int wxWindowGTK::GetCharWidth() const
2915 wxCHECK_MSG( (m_widget
!= NULL
), 8, wxT("invalid window") );
2917 wxFont font
= GetFont();
2918 wxCHECK_MSG( font
.IsOk(), 8, wxT("invalid font") );
2920 PangoContext
* context
= gtk_widget_get_pango_context(m_widget
);
2925 PangoFontDescription
*desc
= font
.GetNativeFontInfo()->description
;
2926 PangoLayout
*layout
= pango_layout_new(context
);
2927 pango_layout_set_font_description(layout
, desc
);
2928 pango_layout_set_text(layout
, "g", 1);
2929 PangoLayoutLine
*line
= (PangoLayoutLine
*)pango_layout_get_lines(layout
)->data
;
2931 PangoRectangle rect
;
2932 pango_layout_line_get_extents(line
, NULL
, &rect
);
2934 g_object_unref (layout
);
2936 return (int) PANGO_PIXELS(rect
.width
);
2939 void wxWindowGTK::DoGetTextExtent( const wxString
& string
,
2943 int *externalLeading
,
2944 const wxFont
*theFont
) const
2946 wxFont fontToUse
= theFont
? *theFont
: GetFont();
2948 wxCHECK_RET( fontToUse
.IsOk(), wxT("invalid font") );
2957 PangoContext
*context
= NULL
;
2959 context
= gtk_widget_get_pango_context( m_widget
);
2968 PangoFontDescription
*desc
= fontToUse
.GetNativeFontInfo()->description
;
2969 PangoLayout
*layout
= pango_layout_new(context
);
2970 pango_layout_set_font_description(layout
, desc
);
2972 const wxCharBuffer data
= wxGTK_CONV( string
);
2974 pango_layout_set_text(layout
, data
, strlen(data
));
2977 PangoRectangle rect
;
2978 pango_layout_get_extents(layout
, NULL
, &rect
);
2980 if (x
) (*x
) = (wxCoord
) PANGO_PIXELS(rect
.width
);
2981 if (y
) (*y
) = (wxCoord
) PANGO_PIXELS(rect
.height
);
2984 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
2985 int baseline
= pango_layout_iter_get_baseline(iter
);
2986 pango_layout_iter_free(iter
);
2987 *descent
= *y
- PANGO_PIXELS(baseline
);
2989 if (externalLeading
) (*externalLeading
) = 0; // ??
2991 g_object_unref (layout
);
2994 void wxWindowGTK::GTKDisableFocusOutEvent()
2996 g_signal_handlers_block_by_func( m_focusWidget
,
2997 (gpointer
) gtk_window_focus_out_callback
, this);
3000 void wxWindowGTK::GTKEnableFocusOutEvent()
3002 g_signal_handlers_unblock_by_func( m_focusWidget
,
3003 (gpointer
) gtk_window_focus_out_callback
, this);
3006 bool wxWindowGTK::GTKHandleFocusIn()
3008 // Disable default focus handling for custom windows since the default GTK+
3009 // handler issues a repaint
3010 const bool retval
= m_wxwindow
? true : false;
3013 // NB: if there's still unprocessed deferred focus-out event (see
3014 // GTKHandleFocusOut() for explanation), we need to process it first so
3015 // that the order of focus events -- focus-out first, then focus-in
3016 // elsewhere -- is preserved
3017 if ( gs_deferredFocusOut
)
3019 if ( GTKNeedsToFilterSameWindowFocus() &&
3020 gs_deferredFocusOut
== this )
3022 // GTK+ focus changed from this wxWindow back to itself, so don't
3023 // emit any events at all
3024 wxLogTrace(TRACE_FOCUS
,
3025 "filtered out spurious focus change within %s(%p, %s)",
3026 GetClassInfo()->GetClassName(), this, GetLabel());
3027 gs_deferredFocusOut
= NULL
;
3031 // otherwise we need to send focus-out first
3032 wxASSERT_MSG ( gs_deferredFocusOut
!= this,
3033 "GTKHandleFocusIn(GTKFocus_Normal) called even though focus changed back to itself - derived class should handle this" );
3034 GTKHandleDeferredFocusOut();
3038 wxLogTrace(TRACE_FOCUS
,
3039 "handling focus_in event for %s(%p, %s)",
3040 GetClassInfo()->GetClassName(), this, GetLabel());
3043 gtk_im_context_focus_in(m_imData
->context
);
3045 gs_currentFocus
= this;
3046 gs_pendingFocus
= NULL
;
3049 // caret needs to be informed about focus change
3050 wxCaret
*caret
= GetCaret();
3053 caret
->OnSetFocus();
3055 #endif // wxUSE_CARET
3057 // Notify the parent keeping track of focus for the kbd navigation
3058 // purposes that we got it.
3059 wxChildFocusEvent
eventChildFocus(static_cast<wxWindow
*>(this));
3060 GTKProcessEvent(eventChildFocus
);
3062 wxFocusEvent
eventFocus(wxEVT_SET_FOCUS
, GetId());
3063 eventFocus
.SetEventObject(this);
3064 GTKProcessEvent(eventFocus
);
3069 bool wxWindowGTK::GTKHandleFocusOut()
3071 // Disable default focus handling for custom windows since the default GTK+
3072 // handler issues a repaint
3073 const bool retval
= m_wxwindow
? true : false;
3076 // NB: If a control is composed of several GtkWidgets and when focus
3077 // changes from one of them to another within the same wxWindow, we get
3078 // a focus-out event followed by focus-in for another GtkWidget owned
3079 // by the same wx control. We don't want to generate two spurious
3080 // wxEVT_SET_FOCUS events in this case, so we defer sending wx events
3081 // from GTKHandleFocusOut() until we know for sure it's not coming back
3082 // (i.e. in GTKHandleFocusIn() or at idle time).
3083 if ( GTKNeedsToFilterSameWindowFocus() )
3085 wxASSERT_MSG( gs_deferredFocusOut
== NULL
,
3086 "deferred focus out event already pending" );
3087 wxLogTrace(TRACE_FOCUS
,
3088 "deferring focus_out event for %s(%p, %s)",
3089 GetClassInfo()->GetClassName(), this, GetLabel());
3090 gs_deferredFocusOut
= this;
3094 GTKHandleFocusOutNoDeferring();
3099 void wxWindowGTK::GTKHandleFocusOutNoDeferring()
3101 wxLogTrace(TRACE_FOCUS
,
3102 "handling focus_out event for %s(%p, %s)",
3103 GetClassInfo()->GetClassName(), this, GetLabel());
3106 gtk_im_context_focus_out(m_imData
->context
);
3108 if ( gs_currentFocus
!= this )
3110 // Something is terribly wrong, gs_currentFocus is out of sync with the
3111 // real focus. We will reset it to NULL anyway, because after this
3112 // focus-out event is handled, one of the following with happen:
3114 // * either focus will go out of the app altogether, in which case
3115 // gs_currentFocus _should_ be NULL
3117 // * or it goes to another control, in which case focus-in event will
3118 // follow immediately and it will set gs_currentFocus to the right
3120 wxLogDebug("window %s(%p, %s) lost focus even though it didn't have it",
3121 GetClassInfo()->GetClassName(), this, GetLabel());
3123 gs_currentFocus
= NULL
;
3126 // caret needs to be informed about focus change
3127 wxCaret
*caret
= GetCaret();
3130 caret
->OnKillFocus();
3132 #endif // wxUSE_CARET
3134 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetId() );
3135 event
.SetEventObject( this );
3136 event
.SetWindow( FindFocus() );
3137 GTKProcessEvent( event
);
3141 void wxWindowGTK::GTKHandleDeferredFocusOut()
3143 // NB: See GTKHandleFocusOut() for explanation. This function is called
3144 // from either GTKHandleFocusIn() or OnInternalIdle() to process
3146 if ( gs_deferredFocusOut
)
3148 wxWindowGTK
*win
= gs_deferredFocusOut
;
3149 gs_deferredFocusOut
= NULL
;
3151 wxLogTrace(TRACE_FOCUS
,
3152 "processing deferred focus_out event for %s(%p, %s)",
3153 win
->GetClassInfo()->GetClassName(), win
, win
->GetLabel());
3155 win
->GTKHandleFocusOutNoDeferring();
3159 void wxWindowGTK::SetFocus()
3161 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
3163 // Setting "physical" focus is not immediate in GTK+ and while
3164 // gtk_widget_is_focus ("determines if the widget is the focus widget
3165 // within its toplevel", i.e. returns true for one widget per TLW, not
3166 // globally) returns true immediately after grabbing focus,
3167 // GTK_WIDGET_HAS_FOCUS (which returns true only for the one widget that
3168 // has focus at the moment) takes effect only after the window is shown
3169 // (if it was hidden at the moment of the call) or at the next event loop
3172 // Because we want to FindFocus() call immediately following
3173 // foo->SetFocus() to return foo, we have to keep track of "pending" focus
3175 gs_pendingFocus
= this;
3177 GtkWidget
*widget
= m_wxwindow
? m_wxwindow
: m_focusWidget
;
3179 if ( GTK_IS_CONTAINER(widget
) &&
3180 !gtk_widget_get_can_focus(widget
) )
3182 wxLogTrace(TRACE_FOCUS
,
3183 wxT("Setting focus to a child of %s(%p, %s)"),
3184 GetClassInfo()->GetClassName(), this, GetLabel().c_str());
3185 gtk_widget_child_focus(widget
, GTK_DIR_TAB_FORWARD
);
3189 wxLogTrace(TRACE_FOCUS
,
3190 wxT("Setting focus to %s(%p, %s)"),
3191 GetClassInfo()->GetClassName(), this, GetLabel().c_str());
3192 gtk_widget_grab_focus(widget
);
3196 void wxWindowGTK::SetCanFocus(bool canFocus
)
3198 gtk_widget_set_can_focus(m_widget
, canFocus
);
3200 if ( m_wxwindow
&& (m_widget
!= m_wxwindow
) )
3202 gtk_widget_set_can_focus(m_wxwindow
, canFocus
);
3206 bool wxWindowGTK::Reparent( wxWindowBase
*newParentBase
)
3208 wxCHECK_MSG( (m_widget
!= NULL
), false, wxT("invalid window") );
3210 wxWindowGTK
* const newParent
= (wxWindowGTK
*)newParentBase
;
3212 wxASSERT( GTK_IS_WIDGET(m_widget
) );
3214 if ( !wxWindowBase::Reparent(newParent
) )
3217 wxASSERT( GTK_IS_WIDGET(m_widget
) );
3219 // Notice that old m_parent pointer might be non-NULL here but the widget
3220 // still not have any parent at GTK level if it's a notebook page that had
3221 // been removed from the notebook so test this at GTK level and not wx one.
3222 if ( GtkWidget
*parentGTK
= gtk_widget_get_parent(m_widget
) )
3223 gtk_container_remove(GTK_CONTAINER(parentGTK
), m_widget
);
3225 wxASSERT( GTK_IS_WIDGET(m_widget
) );
3229 if (gtk_widget_get_visible (newParent
->m_widget
))
3231 m_showOnIdle
= true;
3232 gtk_widget_hide( m_widget
);
3234 /* insert GTK representation */
3235 newParent
->AddChildGTK(this);
3238 SetLayoutDirection(wxLayout_Default
);
3243 void wxWindowGTK::DoAddChild(wxWindowGTK
*child
)
3245 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid window") );
3246 wxASSERT_MSG( (child
!= NULL
), wxT("invalid child window") );
3251 /* insert GTK representation */
3255 void wxWindowGTK::AddChild(wxWindowBase
*child
)
3257 wxWindowBase::AddChild(child
);
3258 m_dirtyTabOrder
= true;
3259 wxTheApp
->WakeUpIdle();
3262 void wxWindowGTK::RemoveChild(wxWindowBase
*child
)
3264 wxWindowBase::RemoveChild(child
);
3265 m_dirtyTabOrder
= true;
3266 wxTheApp
->WakeUpIdle();
3270 wxLayoutDirection
wxWindowGTK::GTKGetLayout(GtkWidget
*widget
)
3272 return gtk_widget_get_direction(widget
) == GTK_TEXT_DIR_RTL
3273 ? wxLayout_RightToLeft
3274 : wxLayout_LeftToRight
;
3278 void wxWindowGTK::GTKSetLayout(GtkWidget
*widget
, wxLayoutDirection dir
)
3280 wxASSERT_MSG( dir
!= wxLayout_Default
, wxT("invalid layout direction") );
3282 gtk_widget_set_direction(widget
,
3283 dir
== wxLayout_RightToLeft
? GTK_TEXT_DIR_RTL
3284 : GTK_TEXT_DIR_LTR
);
3287 wxLayoutDirection
wxWindowGTK::GetLayoutDirection() const
3289 return GTKGetLayout(m_widget
);
3292 void wxWindowGTK::SetLayoutDirection(wxLayoutDirection dir
)
3294 if ( dir
== wxLayout_Default
)
3296 const wxWindow
*const parent
= GetParent();
3299 // inherit layout from parent.
3300 dir
= parent
->GetLayoutDirection();
3302 else // no parent, use global default layout
3304 dir
= wxTheApp
->GetLayoutDirection();
3308 if ( dir
== wxLayout_Default
)
3311 GTKSetLayout(m_widget
, dir
);
3313 if (m_wxwindow
&& (m_wxwindow
!= m_widget
))
3314 GTKSetLayout(m_wxwindow
, dir
);
3318 wxWindowGTK::AdjustForLayoutDirection(wxCoord x
,
3319 wxCoord
WXUNUSED(width
),
3320 wxCoord
WXUNUSED(widthTotal
)) const
3322 // We now mirror the coordinates of RTL windows in wxPizza
3326 void wxWindowGTK::DoMoveInTabOrder(wxWindow
*win
, WindowOrder move
)
3328 wxWindowBase::DoMoveInTabOrder(win
, move
);
3329 m_dirtyTabOrder
= true;
3330 wxTheApp
->WakeUpIdle();
3333 bool wxWindowGTK::DoNavigateIn(int flags
)
3335 if ( flags
& wxNavigationKeyEvent::WinChange
)
3337 wxFAIL_MSG( wxT("not implemented") );
3341 else // navigate inside the container
3343 wxWindow
*parent
= wxGetTopLevelParent((wxWindow
*)this);
3344 wxCHECK_MSG( parent
, false, wxT("every window must have a TLW parent") );
3346 GtkDirectionType dir
;
3347 dir
= flags
& wxNavigationKeyEvent::IsForward
? GTK_DIR_TAB_FORWARD
3348 : GTK_DIR_TAB_BACKWARD
;
3351 g_signal_emit_by_name(parent
->m_widget
, "focus", dir
, &rc
);
3357 bool wxWindowGTK::GTKWidgetNeedsMnemonic() const
3359 // none needed by default
3363 void wxWindowGTK::GTKWidgetDoSetMnemonic(GtkWidget
* WXUNUSED(w
))
3365 // nothing to do by default since none is needed
3368 void wxWindowGTK::RealizeTabOrder()
3372 if ( !m_children
.empty() )
3374 // we don't only construct the correct focus chain but also use
3375 // this opportunity to update the mnemonic widgets for the widgets
3378 GList
*chain
= NULL
;
3379 wxWindowGTK
* mnemonicWindow
= NULL
;
3381 for ( wxWindowList::const_iterator i
= m_children
.begin();
3382 i
!= m_children
.end();
3385 wxWindowGTK
*win
= *i
;
3387 bool focusableFromKeyboard
= win
->AcceptsFocusFromKeyboard();
3389 if ( mnemonicWindow
)
3391 if ( focusableFromKeyboard
)
3393 // wxComboBox et al. needs to focus on on a different
3394 // widget than m_widget, so if the main widget isn't
3395 // focusable try the connect widget
3396 GtkWidget
* w
= win
->m_widget
;
3397 if ( !gtk_widget_get_can_focus(w
) )
3399 w
= win
->GetConnectWidget();
3400 if ( !gtk_widget_get_can_focus(w
) )
3406 mnemonicWindow
->GTKWidgetDoSetMnemonic(w
);
3407 mnemonicWindow
= NULL
;
3411 else if ( win
->GTKWidgetNeedsMnemonic() )
3413 mnemonicWindow
= win
;
3416 if ( focusableFromKeyboard
)
3417 chain
= g_list_prepend(chain
, win
->m_widget
);
3420 chain
= g_list_reverse(chain
);
3422 gtk_container_set_focus_chain(GTK_CONTAINER(m_wxwindow
), chain
);
3427 gtk_container_unset_focus_chain(GTK_CONTAINER(m_wxwindow
));
3432 void wxWindowGTK::Raise()
3434 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
3436 if (m_wxwindow
&& gtk_widget_get_window(m_wxwindow
))
3438 gdk_window_raise(gtk_widget_get_window(m_wxwindow
));
3440 else if (gtk_widget_get_window(m_widget
))
3442 gdk_window_raise(gtk_widget_get_window(m_widget
));
3446 void wxWindowGTK::Lower()
3448 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
3450 if (m_wxwindow
&& gtk_widget_get_window(m_wxwindow
))
3452 gdk_window_lower(gtk_widget_get_window(m_wxwindow
));
3454 else if (gtk_widget_get_window(m_widget
))
3456 gdk_window_lower(gtk_widget_get_window(m_widget
));
3460 bool wxWindowGTK::SetCursor( const wxCursor
&cursor
)
3462 if ( !wxWindowBase::SetCursor(cursor
.IsOk() ? cursor
: *wxSTANDARD_CURSOR
) )
3470 void wxWindowGTK::GTKUpdateCursor(bool update_self
/*=true*/, bool recurse
/*=true*/)
3474 wxCursor
cursor(g_globalCursor
.IsOk() ? g_globalCursor
: GetCursor());
3475 if ( cursor
.IsOk() )
3477 wxArrayGdkWindows windowsThis
;
3478 GdkWindow
* window
= GTKGetWindow(windowsThis
);
3480 gdk_window_set_cursor( window
, cursor
.GetCursor() );
3483 const size_t count
= windowsThis
.size();
3484 for ( size_t n
= 0; n
< count
; n
++ )
3486 GdkWindow
*win
= windowsThis
[n
];
3487 // It can be zero if the window has not been realized yet.
3490 gdk_window_set_cursor(win
, cursor
.GetCursor());
3499 for (wxWindowList::iterator it
= GetChildren().begin(); it
!= GetChildren().end(); ++it
)
3501 (*it
)->GTKUpdateCursor( true );
3506 void wxWindowGTK::WarpPointer( int x
, int y
)
3508 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
3510 ClientToScreen(&x
, &y
);
3511 GdkDisplay
* display
= gtk_widget_get_display(m_widget
);
3512 GdkScreen
* screen
= gtk_widget_get_screen(m_widget
);
3514 GdkDeviceManager
* manager
= gdk_display_get_device_manager(display
);
3515 gdk_device_warp(gdk_device_manager_get_client_pointer(manager
), screen
, x
, y
);
3517 XWarpPointer(GDK_DISPLAY_XDISPLAY(display
),
3519 GDK_WINDOW_XID(gdk_screen_get_root_window(screen
)),
3524 wxWindowGTK::ScrollDir
wxWindowGTK::ScrollDirFromRange(GtkRange
*range
) const
3526 // find the scrollbar which generated the event
3527 for ( int dir
= 0; dir
< ScrollDir_Max
; dir
++ )
3529 if ( range
== m_scrollBar
[dir
] )
3530 return (ScrollDir
)dir
;
3533 wxFAIL_MSG( wxT("event from unknown scrollbar received") );
3535 return ScrollDir_Max
;
3538 bool wxWindowGTK::DoScrollByUnits(ScrollDir dir
, ScrollUnit unit
, int units
)
3540 bool changed
= false;
3541 GtkRange
* range
= m_scrollBar
[dir
];
3542 if ( range
&& units
)
3544 GtkAdjustment
* adj
= gtk_range_get_adjustment(range
);
3545 double inc
= unit
== ScrollUnit_Line
? gtk_adjustment_get_step_increment(adj
)
3546 : gtk_adjustment_get_page_increment(adj
);
3548 const int posOld
= wxRound(gtk_adjustment_get_value(adj
));
3549 gtk_range_set_value(range
, posOld
+ units
*inc
);
3551 changed
= wxRound(gtk_adjustment_get_value(adj
)) != posOld
;
3557 bool wxWindowGTK::ScrollLines(int lines
)
3559 return DoScrollByUnits(ScrollDir_Vert
, ScrollUnit_Line
, lines
);
3562 bool wxWindowGTK::ScrollPages(int pages
)
3564 return DoScrollByUnits(ScrollDir_Vert
, ScrollUnit_Page
, pages
);
3567 void wxWindowGTK::Refresh(bool WXUNUSED(eraseBackground
),
3570 if (m_widget
== NULL
|| !gtk_widget_get_mapped(m_widget
))
3575 GdkWindow
* window
= gtk_widget_get_window(m_wxwindow
);
3578 GdkRectangle r
= { rect
->x
, rect
->y
, rect
->width
, rect
->height
};
3579 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3580 r
.x
= gdk_window_get_width(window
) - r
.x
- rect
->width
;
3581 gdk_window_invalidate_rect(window
, &r
, true);
3584 gdk_window_invalidate_rect(window
, NULL
, true);
3589 gtk_widget_queue_draw_area(m_widget
, rect
->x
, rect
->y
, rect
->width
, rect
->height
);
3591 gtk_widget_queue_draw(m_widget
);
3595 void wxWindowGTK::Update()
3597 if (m_widget
&& gtk_widget_get_mapped(m_widget
))
3599 GdkDisplay
* display
= gtk_widget_get_display(m_widget
);
3600 // Flush everything out to the server, and wait for it to finish.
3601 // This ensures nothing will overwrite the drawing we are about to do.
3602 gdk_display_sync(display
);
3604 GdkWindow
* window
= GTKGetDrawingWindow();
3606 window
= gtk_widget_get_window(m_widget
);
3607 gdk_window_process_updates(window
, true);
3609 // Flush again, but no need to wait for it to finish
3610 gdk_display_flush(display
);
3614 bool wxWindowGTK::DoIsExposed( int x
, int y
) const
3616 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
3619 bool wxWindowGTK::DoIsExposed( int x
, int y
, int w
, int h
) const
3621 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3622 return m_updateRegion
.Contains(x
-w
, y
, w
, h
) != wxOutRegion
;
3624 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
3627 void wxWindowGTK::GtkSendPaintEvents()
3631 m_updateRegion
.Clear();
3635 // Clip to paint region in wxClientDC
3636 m_clipPaintRegion
= true;
3638 m_nativeUpdateRegion
= m_updateRegion
;
3640 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3642 // Transform m_updateRegion under RTL
3643 m_updateRegion
.Clear();
3646 gdk_drawable_get_size(gtk_widget_get_window(m_wxwindow
), &width
, NULL
);
3648 wxRegionIterator
upd( m_nativeUpdateRegion
);
3652 rect
.x
= upd
.GetX();
3653 rect
.y
= upd
.GetY();
3654 rect
.width
= upd
.GetWidth();
3655 rect
.height
= upd
.GetHeight();
3657 rect
.x
= width
- rect
.x
- rect
.width
;
3658 m_updateRegion
.Union( rect
);
3664 switch ( GetBackgroundStyle() )
3666 case wxBG_STYLE_ERASE
:
3668 wxWindowDC
dc( (wxWindow
*)this );
3669 dc
.SetDeviceClippingRegion( m_updateRegion
);
3671 // Work around gtk-qt <= 0.60 bug whereby the window colour
3675 GetOptionInt("gtk.window.force-background-colour") )
3677 dc
.SetBackground(GetBackgroundColour());
3681 wxEraseEvent
erase_event( GetId(), &dc
);
3682 erase_event
.SetEventObject( this );
3684 if ( HandleWindowEvent(erase_event
) )
3686 // background erased, don't do it again
3692 case wxBG_STYLE_SYSTEM
:
3693 if ( GetThemeEnabled() )
3695 // find ancestor from which to steal background
3696 wxWindow
*parent
= wxGetTopLevelParent((wxWindow
*)this);
3698 parent
= (wxWindow
*)this;
3700 if (gtk_widget_get_mapped(parent
->m_widget
))
3702 wxRegionIterator
upd( m_nativeUpdateRegion
);
3706 rect
.x
= upd
.GetX();
3707 rect
.y
= upd
.GetY();
3708 rect
.width
= upd
.GetWidth();
3709 rect
.height
= upd
.GetHeight();
3711 gtk_paint_flat_box(gtk_widget_get_style(parent
->m_widget
),
3712 GTKGetDrawingWindow(),
3713 gtk_widget_get_state(m_wxwindow
),
3726 case wxBG_STYLE_PAINT
:
3727 // nothing to do: window will be painted over in EVT_PAINT
3731 wxFAIL_MSG( "unsupported background style" );
3734 wxNcPaintEvent
nc_paint_event( GetId() );
3735 nc_paint_event
.SetEventObject( this );
3736 HandleWindowEvent( nc_paint_event
);
3738 wxPaintEvent
paint_event( GetId() );
3739 paint_event
.SetEventObject( this );
3740 HandleWindowEvent( paint_event
);
3742 m_clipPaintRegion
= false;
3744 m_updateRegion
.Clear();
3745 m_nativeUpdateRegion
.Clear();
3748 void wxWindowGTK::SetDoubleBuffered( bool on
)
3750 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
3753 gtk_widget_set_double_buffered( m_wxwindow
, on
);
3756 bool wxWindowGTK::IsDoubleBuffered() const
3758 return gtk_widget_get_double_buffered( m_wxwindow
);
3761 void wxWindowGTK::ClearBackground()
3763 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
3767 void wxWindowGTK::DoSetToolTip( wxToolTip
*tip
)
3769 if (m_tooltip
!= tip
)
3771 wxWindowBase::DoSetToolTip(tip
);
3774 m_tooltip
->GTKSetWindow(static_cast<wxWindow
*>(this));
3776 GTKApplyToolTip(NULL
);
3780 void wxWindowGTK::GTKApplyToolTip(const char* tip
)
3782 wxToolTip::GTKApply(GetConnectWidget(), tip
);
3784 #endif // wxUSE_TOOLTIPS
3786 bool wxWindowGTK::SetBackgroundColour( const wxColour
&colour
)
3788 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
3790 if (!wxWindowBase::SetBackgroundColour(colour
))
3795 // We need the pixel value e.g. for background clearing.
3796 m_backgroundColour
.CalcPixel(gtk_widget_get_colormap(m_widget
));
3799 // apply style change (forceStyle=true so that new style is applied
3800 // even if the bg colour changed from valid to wxNullColour)
3801 GTKApplyWidgetStyle(true);
3806 bool wxWindowGTK::SetForegroundColour( const wxColour
&colour
)
3808 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
3810 if (!wxWindowBase::SetForegroundColour(colour
))
3817 // We need the pixel value e.g. for background clearing.
3818 m_foregroundColour
.CalcPixel(gtk_widget_get_colormap(m_widget
));
3821 // apply style change (forceStyle=true so that new style is applied
3822 // even if the bg colour changed from valid to wxNullColour):
3823 GTKApplyWidgetStyle(true);
3828 PangoContext
*wxWindowGTK::GTKGetPangoDefaultContext()
3830 return gtk_widget_get_pango_context( m_widget
);
3833 GtkRcStyle
*wxWindowGTK::GTKCreateWidgetStyle(bool forceStyle
)
3835 // do we need to apply any changes at all?
3838 !m_foregroundColour
.IsOk() && !m_backgroundColour
.IsOk() )
3843 GtkRcStyle
*style
= gtk_rc_style_new();
3845 if ( m_font
.IsOk() )
3848 pango_font_description_copy( m_font
.GetNativeFontInfo()->description
);
3851 int flagsNormal
= 0,
3854 flagsInsensitive
= 0;
3856 if ( m_foregroundColour
.IsOk() )
3858 const GdkColor
*fg
= m_foregroundColour
.GetColor();
3860 style
->fg
[GTK_STATE_NORMAL
] =
3861 style
->text
[GTK_STATE_NORMAL
] = *fg
;
3862 flagsNormal
|= GTK_RC_FG
| GTK_RC_TEXT
;
3864 style
->fg
[GTK_STATE_PRELIGHT
] =
3865 style
->text
[GTK_STATE_PRELIGHT
] = *fg
;
3866 flagsPrelight
|= GTK_RC_FG
| GTK_RC_TEXT
;
3868 style
->fg
[GTK_STATE_ACTIVE
] =
3869 style
->text
[GTK_STATE_ACTIVE
] = *fg
;
3870 flagsActive
|= GTK_RC_FG
| GTK_RC_TEXT
;
3873 if ( m_backgroundColour
.IsOk() )
3875 const GdkColor
*bg
= m_backgroundColour
.GetColor();
3877 style
->bg
[GTK_STATE_NORMAL
] =
3878 style
->base
[GTK_STATE_NORMAL
] = *bg
;
3879 flagsNormal
|= GTK_RC_BG
| GTK_RC_BASE
;
3881 style
->bg
[GTK_STATE_PRELIGHT
] =
3882 style
->base
[GTK_STATE_PRELIGHT
] = *bg
;
3883 flagsPrelight
|= GTK_RC_BG
| GTK_RC_BASE
;
3885 style
->bg
[GTK_STATE_ACTIVE
] =
3886 style
->base
[GTK_STATE_ACTIVE
] = *bg
;
3887 flagsActive
|= GTK_RC_BG
| GTK_RC_BASE
;
3889 style
->bg
[GTK_STATE_INSENSITIVE
] =
3890 style
->base
[GTK_STATE_INSENSITIVE
] = *bg
;
3891 flagsInsensitive
|= GTK_RC_BG
| GTK_RC_BASE
;
3894 style
->color_flags
[GTK_STATE_NORMAL
] = (GtkRcFlags
)flagsNormal
;
3895 style
->color_flags
[GTK_STATE_PRELIGHT
] = (GtkRcFlags
)flagsPrelight
;
3896 style
->color_flags
[GTK_STATE_ACTIVE
] = (GtkRcFlags
)flagsActive
;
3897 style
->color_flags
[GTK_STATE_INSENSITIVE
] = (GtkRcFlags
)flagsInsensitive
;
3902 void wxWindowGTK::GTKApplyWidgetStyle(bool forceStyle
)
3904 GtkRcStyle
*style
= GTKCreateWidgetStyle(forceStyle
);
3907 DoApplyWidgetStyle(style
);
3908 g_object_unref(style
);
3911 // Style change may affect GTK+'s size calculation:
3912 InvalidateBestSize();
3915 void wxWindowGTK::DoApplyWidgetStyle(GtkRcStyle
*style
)
3919 // block the signal temporarily to avoid sending
3920 // wxSysColourChangedEvents when we change the colours ourselves
3921 bool unblock
= false;
3925 g_signal_handlers_block_by_func(
3926 m_wxwindow
, (void *)gtk_window_style_set_callback
, this);
3929 gtk_widget_modify_style(m_wxwindow
, style
);
3933 g_signal_handlers_unblock_by_func(
3934 m_wxwindow
, (void *)gtk_window_style_set_callback
, this);
3939 gtk_widget_modify_style(m_widget
, style
);
3943 bool wxWindowGTK::SetBackgroundStyle(wxBackgroundStyle style
)
3945 wxWindowBase::SetBackgroundStyle(style
);
3947 if ( style
== wxBG_STYLE_PAINT
)
3952 window
= GTKGetDrawingWindow();
3956 GtkWidget
* const w
= GetConnectWidget();
3957 window
= w
? gtk_widget_get_window(w
) : NULL
;
3962 // Make sure GDK/X11 doesn't refresh the window
3964 gdk_window_set_back_pixmap( window
, None
, False
);
3966 Display
* display
= GDK_WINDOW_DISPLAY(window
);
3969 m_needsStyleChange
= false;
3971 else // window not realized yet
3973 // Do in OnIdle, because the window is not yet available
3974 m_needsStyleChange
= true;
3977 // Don't apply widget style, or we get a grey background
3981 // apply style change (forceStyle=true so that new style is applied
3982 // even if the bg colour changed from valid to wxNullColour):
3983 GTKApplyWidgetStyle(true);
3989 // ----------------------------------------------------------------------------
3990 // Pop-up menu stuff
3991 // ----------------------------------------------------------------------------
3993 #if wxUSE_MENUS_NATIVE
3997 void wxPopupMenuPositionCallback( GtkMenu
*menu
,
3999 gboolean
* WXUNUSED(whatever
),
4000 gpointer user_data
)
4002 // ensure that the menu appears entirely on screen
4004 gtk_widget_get_child_requisition(GTK_WIDGET(menu
), &req
);
4006 wxSize sizeScreen
= wxGetDisplaySize();
4007 wxPoint
*pos
= (wxPoint
*)user_data
;
4009 gint xmax
= sizeScreen
.x
- req
.width
,
4010 ymax
= sizeScreen
.y
- req
.height
;
4012 *x
= pos
->x
< xmax
? pos
->x
: xmax
;
4013 *y
= pos
->y
< ymax
? pos
->y
: ymax
;
4017 bool wxWindowGTK::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
4019 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
4025 GtkMenuPositionFunc posfunc
;
4026 if ( x
== -1 && y
== -1 )
4028 // use GTK's default positioning algorithm
4034 pos
= ClientToScreen(wxPoint(x
, y
));
4036 posfunc
= wxPopupMenuPositionCallback
;
4039 menu
->m_popupShown
= true;
4041 GTK_MENU(menu
->m_menu
),
4042 NULL
, // parent menu shell
4043 NULL
, // parent menu item
4044 posfunc
, // function to position it
4045 userdata
, // client data
4046 0, // button used to activate it
4047 gtk_get_current_event_time()
4050 while (menu
->m_popupShown
)
4052 gtk_main_iteration();
4058 #endif // wxUSE_MENUS_NATIVE
4060 #if wxUSE_DRAG_AND_DROP
4062 void wxWindowGTK::SetDropTarget( wxDropTarget
*dropTarget
)
4064 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
4066 GtkWidget
*dnd_widget
= GetConnectWidget();
4068 if (m_dropTarget
) m_dropTarget
->GtkUnregisterWidget( dnd_widget
);
4070 if (m_dropTarget
) delete m_dropTarget
;
4071 m_dropTarget
= dropTarget
;
4073 if (m_dropTarget
) m_dropTarget
->GtkRegisterWidget( dnd_widget
);
4076 #endif // wxUSE_DRAG_AND_DROP
4078 GtkWidget
* wxWindowGTK::GetConnectWidget()
4080 GtkWidget
*connect_widget
= m_widget
;
4081 if (m_wxwindow
) connect_widget
= m_wxwindow
;
4083 return connect_widget
;
4086 bool wxWindowGTK::GTKIsOwnWindow(GdkWindow
*window
) const
4088 wxArrayGdkWindows windowsThis
;
4089 GdkWindow
* const winThis
= GTKGetWindow(windowsThis
);
4091 return winThis
? window
== winThis
4092 : windowsThis
.Index(window
) != wxNOT_FOUND
;
4095 GdkWindow
*wxWindowGTK::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
4097 return m_wxwindow
? GTKGetDrawingWindow() : gtk_widget_get_window(m_widget
);
4100 bool wxWindowGTK::SetFont( const wxFont
&font
)
4102 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
4104 if (!wxWindowBase::SetFont(font
))
4107 // apply style change (forceStyle=true so that new style is applied
4108 // even if the font changed from valid to wxNullFont):
4109 GTKApplyWidgetStyle(true);
4114 void wxWindowGTK::DoCaptureMouse()
4116 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
4118 GdkWindow
*window
= NULL
;
4120 window
= GTKGetDrawingWindow();
4122 window
= gtk_widget_get_window(GetConnectWidget());
4124 wxCHECK_RET( window
, wxT("CaptureMouse() failed") );
4126 const wxCursor
* cursor
= &m_cursor
;
4127 if (!cursor
->IsOk())
4128 cursor
= wxSTANDARD_CURSOR
;
4130 gdk_pointer_grab( window
, FALSE
,
4132 (GDK_BUTTON_PRESS_MASK
|
4133 GDK_BUTTON_RELEASE_MASK
|
4134 GDK_POINTER_MOTION_HINT_MASK
|
4135 GDK_POINTER_MOTION_MASK
),
4137 cursor
->GetCursor(),
4138 (guint32
)GDK_CURRENT_TIME
);
4139 g_captureWindow
= this;
4140 g_captureWindowHasMouse
= true;
4143 void wxWindowGTK::DoReleaseMouse()
4145 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
4147 wxCHECK_RET( g_captureWindow
, wxT("can't release mouse - not captured") );
4149 g_captureWindow
= NULL
;
4151 GdkWindow
*window
= NULL
;
4153 window
= GTKGetDrawingWindow();
4155 window
= gtk_widget_get_window(GetConnectWidget());
4160 gdk_pointer_ungrab ( (guint32
)GDK_CURRENT_TIME
);
4163 void wxWindowGTK::GTKReleaseMouseAndNotify()
4166 wxMouseCaptureLostEvent
evt(GetId());
4167 evt
.SetEventObject( this );
4168 HandleWindowEvent( evt
);
4172 wxWindow
*wxWindowBase::GetCapture()
4174 return (wxWindow
*)g_captureWindow
;
4177 bool wxWindowGTK::IsRetained() const
4182 void wxWindowGTK::SetScrollbar(int orient
,
4186 bool WXUNUSED(update
))
4188 const int dir
= ScrollDirFromOrient(orient
);
4189 GtkRange
* const sb
= m_scrollBar
[dir
];
4190 wxCHECK_RET( sb
, wxT("this window is not scrollable") );
4194 // GtkRange requires upper > lower
4199 g_signal_handlers_block_by_func(
4200 sb
, (void*)gtk_scrollbar_value_changed
, this);
4202 gtk_range_set_increments(sb
, 1, thumbVisible
);
4203 gtk_adjustment_set_page_size(gtk_range_get_adjustment(sb
), thumbVisible
);
4204 gtk_range_set_range(sb
, 0, range
);
4205 gtk_range_set_value(sb
, pos
);
4206 m_scrollPos
[dir
] = gtk_range_get_value(sb
);
4208 g_signal_handlers_unblock_by_func(
4209 sb
, (void*)gtk_scrollbar_value_changed
, this);
4212 void wxWindowGTK::SetScrollPos(int orient
, int pos
, bool WXUNUSED(refresh
))
4214 const int dir
= ScrollDirFromOrient(orient
);
4215 GtkRange
* const sb
= m_scrollBar
[dir
];
4216 wxCHECK_RET( sb
, wxT("this window is not scrollable") );
4218 // This check is more than an optimization. Without it, the slider
4219 // will not move smoothly while tracking when using wxScrollHelper.
4220 if (GetScrollPos(orient
) != pos
)
4222 g_signal_handlers_block_by_func(
4223 sb
, (void*)gtk_scrollbar_value_changed
, this);
4225 gtk_range_set_value(sb
, pos
);
4226 m_scrollPos
[dir
] = gtk_range_get_value(sb
);
4228 g_signal_handlers_unblock_by_func(
4229 sb
, (void*)gtk_scrollbar_value_changed
, this);
4233 int wxWindowGTK::GetScrollThumb(int orient
) const
4235 GtkRange
* const sb
= m_scrollBar
[ScrollDirFromOrient(orient
)];
4236 wxCHECK_MSG( sb
, 0, wxT("this window is not scrollable") );
4238 return wxRound(gtk_adjustment_get_page_size(gtk_range_get_adjustment(sb
)));
4241 int wxWindowGTK::GetScrollPos( int orient
) const
4243 GtkRange
* const sb
= m_scrollBar
[ScrollDirFromOrient(orient
)];
4244 wxCHECK_MSG( sb
, 0, wxT("this window is not scrollable") );
4246 return wxRound(gtk_range_get_value(sb
));
4249 int wxWindowGTK::GetScrollRange( int orient
) const
4251 GtkRange
* const sb
= m_scrollBar
[ScrollDirFromOrient(orient
)];
4252 wxCHECK_MSG( sb
, 0, wxT("this window is not scrollable") );
4254 return wxRound(gtk_adjustment_get_upper(gtk_range_get_adjustment(sb
)));
4257 // Determine if increment is the same as +/-x, allowing for some small
4258 // difference due to possible inexactness in floating point arithmetic
4259 static inline bool IsScrollIncrement(double increment
, double x
)
4261 wxASSERT(increment
> 0);
4262 const double tolerance
= 1.0 / 1024;
4263 return fabs(increment
- fabs(x
)) < tolerance
;
4266 wxEventType
wxWindowGTK::GTKGetScrollEventType(GtkRange
* range
)
4268 wxASSERT(range
== m_scrollBar
[0] || range
== m_scrollBar
[1]);
4270 const int barIndex
= range
== m_scrollBar
[1];
4272 const double value
= gtk_range_get_value(range
);
4274 // save previous position
4275 const double oldPos
= m_scrollPos
[barIndex
];
4276 // update current position
4277 m_scrollPos
[barIndex
] = value
;
4278 // If event should be ignored, or integral position has not changed
4279 if (!m_hasVMT
|| g_blockEventsOnDrag
|| wxRound(value
) == wxRound(oldPos
))
4284 wxEventType eventType
= wxEVT_SCROLL_THUMBTRACK
;
4287 // Difference from last change event
4288 const double diff
= value
- oldPos
;
4289 const bool isDown
= diff
> 0;
4291 GtkAdjustment
* adj
= gtk_range_get_adjustment(range
);
4292 if (IsScrollIncrement(gtk_adjustment_get_step_increment(adj
), diff
))
4294 eventType
= isDown
? wxEVT_SCROLL_LINEDOWN
: wxEVT_SCROLL_LINEUP
;
4296 else if (IsScrollIncrement(gtk_adjustment_get_page_increment(adj
), diff
))
4298 eventType
= isDown
? wxEVT_SCROLL_PAGEDOWN
: wxEVT_SCROLL_PAGEUP
;
4300 else if (m_mouseButtonDown
)
4302 // Assume track event
4303 m_isScrolling
= true;
4309 void wxWindowGTK::ScrollWindow( int dx
, int dy
, const wxRect
* WXUNUSED(rect
) )
4311 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
4313 wxCHECK_RET( m_wxwindow
!= NULL
, wxT("window needs client area for scrolling") );
4315 // No scrolling requested.
4316 if ((dx
== 0) && (dy
== 0)) return;
4318 m_clipPaintRegion
= true;
4320 WX_PIZZA(m_wxwindow
)->scroll(dx
, dy
);
4322 m_clipPaintRegion
= false;
4325 bool restoreCaret
= (GetCaret() != NULL
&& GetCaret()->IsVisible());
4328 wxRect
caretRect(GetCaret()->GetPosition(), GetCaret()->GetSize());
4330 caretRect
.width
+= dx
;
4333 caretRect
.x
+= dx
; caretRect
.width
-= dx
;
4336 caretRect
.height
+= dy
;
4339 caretRect
.y
+= dy
; caretRect
.height
-= dy
;
4342 RefreshRect(caretRect
);
4344 #endif // wxUSE_CARET
4347 void wxWindowGTK::GTKScrolledWindowSetBorder(GtkWidget
* w
, int wxstyle
)
4349 //RN: Note that static controls usually have no border on gtk, so maybe
4350 //it makes sense to treat that as simply no border at the wx level
4352 if (!(wxstyle
& wxNO_BORDER
) && !(wxstyle
& wxBORDER_STATIC
))
4354 GtkShadowType gtkstyle
;
4356 if(wxstyle
& wxBORDER_RAISED
)
4357 gtkstyle
= GTK_SHADOW_OUT
;
4358 else if ((wxstyle
& wxBORDER_SUNKEN
) || (wxstyle
& wxBORDER_THEME
))
4359 gtkstyle
= GTK_SHADOW_IN
;
4362 else if (wxstyle
& wxBORDER_DOUBLE
)
4363 gtkstyle
= GTK_SHADOW_ETCHED_IN
;
4366 gtkstyle
= GTK_SHADOW_IN
;
4368 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(w
),
4373 void wxWindowGTK::SetWindowStyleFlag( long style
)
4375 // Updates the internal variable. NB: Now m_windowStyle bits carry the _new_ style values already
4376 wxWindowBase::SetWindowStyleFlag(style
);
4379 // Find the wxWindow at the current mouse position, also returning the mouse
4381 wxWindow
* wxFindWindowAtPointer(wxPoint
& pt
)
4383 pt
= wxGetMousePosition();
4384 wxWindow
* found
= wxFindWindowAtPoint(pt
);
4388 // Get the current mouse position.
4389 wxPoint
wxGetMousePosition()
4391 /* This crashes when used within wxHelpContext,
4392 so we have to use the X-specific implementation below.
4394 GdkModifierType *mask;
4395 (void) gdk_window_get_pointer(NULL, &x, &y, mask);
4397 return wxPoint(x, y);
4401 GdkWindow
* windowAtPtr
= gdk_window_at_pointer(& x
, & y
);
4403 Display
*display
= windowAtPtr
? GDK_WINDOW_XDISPLAY(windowAtPtr
) : GDK_DISPLAY();
4404 Window rootWindow
= RootWindowOfScreen (DefaultScreenOfDisplay(display
));
4405 Window rootReturn
, childReturn
;
4406 int rootX
, rootY
, winX
, winY
;
4407 unsigned int maskReturn
;
4409 XQueryPointer (display
,
4413 &rootX
, &rootY
, &winX
, &winY
, &maskReturn
);
4414 return wxPoint(rootX
, rootY
);
4418 GdkWindow
* wxWindowGTK::GTKGetDrawingWindow() const
4420 GdkWindow
* window
= NULL
;
4422 window
= gtk_widget_get_window(m_wxwindow
);
4426 // ----------------------------------------------------------------------------
4428 // ----------------------------------------------------------------------------
4433 // this is called if we attempted to freeze unrealized widget when it finally
4434 // is realized (and so can be frozen):
4435 static void wx_frozen_widget_realize(GtkWidget
* w
, wxWindowGTK
* win
)
4437 wxASSERT( w
&& gtk_widget_get_has_window(w
) );
4438 wxASSERT( gtk_widget_get_realized(w
) );
4440 g_signal_handlers_disconnect_by_func
4443 (void*)wx_frozen_widget_realize
,
4448 if (w
== win
->m_wxwindow
)
4449 window
= win
->GTKGetDrawingWindow();
4451 window
= gtk_widget_get_window(w
);
4452 gdk_window_freeze_updates(window
);
4457 void wxWindowGTK::GTKFreezeWidget(GtkWidget
*w
)
4459 if ( !w
|| !gtk_widget_get_has_window(w
) )
4460 return; // window-less widget, cannot be frozen
4462 GdkWindow
* window
= gtk_widget_get_window(w
);
4465 // we can't thaw unrealized widgets because they don't have GdkWindow,
4466 // so set it up to be done immediately after realization:
4467 g_signal_connect_after
4471 G_CALLBACK(wx_frozen_widget_realize
),
4477 if (w
== m_wxwindow
)
4478 window
= GTKGetDrawingWindow();
4479 gdk_window_freeze_updates(window
);
4482 void wxWindowGTK::GTKThawWidget(GtkWidget
*w
)
4484 if ( !w
|| !gtk_widget_get_has_window(w
) )
4485 return; // window-less widget, cannot be frozen
4487 GdkWindow
* window
= gtk_widget_get_window(w
);
4490 // the widget wasn't realized yet, no need to thaw
4491 g_signal_handlers_disconnect_by_func
4494 (void*)wx_frozen_widget_realize
,
4500 if (w
== m_wxwindow
)
4501 window
= GTKGetDrawingWindow();
4502 gdk_window_thaw_updates(window
);
4505 void wxWindowGTK::DoFreeze()
4507 GTKFreezeWidget(m_widget
);
4508 if ( m_wxwindow
&& m_widget
!= m_wxwindow
)
4509 GTKFreezeWidget(m_wxwindow
);
4512 void wxWindowGTK::DoThaw()
4514 GTKThawWidget(m_widget
);
4515 if ( m_wxwindow
&& m_widget
!= m_wxwindow
)
4516 GTKThawWidget(m_wxwindow
);