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"
43 #include <gdk/gdkkeysyms.h>
44 #if GTK_CHECK_VERSION(3,0,0)
45 #include <gdk/gdkkeysyms-compat.h>
48 #if !GTK_CHECK_VERSION(2,10,0)
49 // GTK+ can reliably detect Meta key state only since 2.10 when
50 // GDK_META_MASK was introduced -- there wasn't any way to detect it
51 // in older versions. wxGTK used GDK_MOD2_MASK for this purpose, but
52 // GDK_MOD2_MASK is documented as:
54 // the fifth modifier key (it depends on the modifier mapping of the X
55 // server which key is interpreted as this modifier)
57 // In other words, it isn't guaranteed to map to Meta. This is a real
58 // problem: it is common to map NumLock to it (in fact, it's an exception
59 // if the X server _doesn't_ use it for NumLock). So the old code caused
60 // wxKeyEvent::MetaDown() to always return true as long as NumLock was on
61 // on many systems, which broke all applications using
62 // wxKeyEvent::GetModifiers() to check modifiers state (see e.g. here:
63 // http://tinyurl.com/56lsk2).
65 // Because of this, it's better to not detect Meta key state at all than
66 // to detect it incorrectly. Hence the following #define, which causes
67 // m_metaDown to be always set to false.
68 #define GDK_META_MASK 0
71 //-----------------------------------------------------------------------------
72 // documentation on internals
73 //-----------------------------------------------------------------------------
76 I have been asked several times about writing some documentation about
77 the GTK port of wxWidgets, especially its internal structures. Obviously,
78 you cannot understand wxGTK without knowing a little about the GTK, but
79 some more information about what the wxWindow, which is the base class
80 for all other window classes, does seems required as well.
84 What does wxWindow do? It contains the common interface for the following
85 jobs of its descendants:
87 1) Define the rudimentary behaviour common to all window classes, such as
88 resizing, intercepting user input (so as to make it possible to use these
89 events for special purposes in a derived class), window names etc.
91 2) Provide the possibility to contain and manage children, if the derived
92 class is allowed to contain children, which holds true for those window
93 classes which do not display a native GTK widget. To name them, these
94 classes are wxPanel, wxScrolledWindow, wxDialog, wxFrame. The MDI frame-
95 work classes are a special case and are handled a bit differently from
96 the rest. The same holds true for the wxNotebook class.
98 3) Provide the possibility to draw into a client area of a window. This,
99 too, only holds true for classes that do not display a native GTK widget
102 4) Provide the entire mechanism for scrolling widgets. This actual inter-
103 face for this is usually in wxScrolledWindow, but the GTK implementation
106 5) A multitude of helper or extra methods for special purposes, such as
107 Drag'n'Drop, managing validators etc.
109 6) Display a border (sunken, raised, simple or none).
111 Normally one might expect, that one wxWidgets window would always correspond
112 to one GTK widget. Under GTK, there is no such all-round widget that has all
113 the functionality. Moreover, the GTK defines a client area as a different
114 widget from the actual widget you are handling. Last but not least some
115 special classes (e.g. wxFrame) handle different categories of widgets and
116 still have the possibility to draw something in the client area.
117 It was therefore required to write a special purpose GTK widget, that would
118 represent a client area in the sense of wxWidgets capable to do the jobs
119 2), 3) and 4). I have written this class and it resides in win_gtk.c of
122 All windows must have a widget, with which they interact with other under-
123 lying GTK widgets. It is this widget, e.g. that has to be resized etc and
124 the wxWindow class has a member variable called m_widget which holds a
125 pointer to this widget. When the window class represents a GTK native widget,
126 this is (in most cases) the only GTK widget the class manages. E.g. the
127 wxStaticText class handles only a GtkLabel widget a pointer to which you
128 can find in m_widget (defined in wxWindow)
130 When the class has a client area for drawing into and for containing children
131 it has to handle the client area widget (of the type wxPizza, defined in
132 win_gtk.cpp), but there could be any number of widgets, handled by a class.
133 The common rule for all windows is only, that the widget that interacts with
134 the rest of GTK must be referenced in m_widget and all other widgets must be
135 children of this widget on the GTK level. The top-most widget, which also
136 represents the client area, must be in the m_wxwindow field and must be of
139 As I said, the window classes that display a GTK native widget only have
140 one widget, so in the case of e.g. the wxButton class m_widget holds a
141 pointer to a GtkButton widget. But windows with client areas (for drawing
142 and children) have a m_widget field that is a pointer to a GtkScrolled-
143 Window and a m_wxwindow field that is pointer to a wxPizza and this
144 one is (in the GTK sense) a child of the GtkScrolledWindow.
146 If the m_wxwindow field is set, then all input to this widget is inter-
147 cepted and sent to the wxWidgets class. If not, all input to the widget
148 that gets pointed to by m_widget gets intercepted and sent to the class.
152 The design of scrolling in wxWidgets is markedly different from that offered
153 by the GTK itself and therefore we cannot simply take it as it is. In GTK,
154 clicking on a scrollbar belonging to scrolled window will inevitably move
155 the window. In wxWidgets, the scrollbar will only emit an event, send this
156 to (normally) a wxScrolledWindow and that class will call ScrollWindow()
157 which actually moves the window and its sub-windows. Note that wxPizza
158 memorizes how much it has been scrolled but that wxWidgets forgets this
159 so that the two coordinates systems have to be kept in synch. This is done
160 in various places using the pizza->m_scroll_x and pizza->m_scroll_y values.
164 Singularly the most broken code in GTK is the code that is supposed to
165 inform subwindows (child windows) about new positions. Very often, duplicate
166 events are sent without changes in size or position, equally often no
167 events are sent at all (All this is due to a bug in the GtkContainer code
168 which got fixed in GTK 1.2.6). For that reason, wxGTK completely ignores
169 GTK's own system and it simply waits for size events for toplevel windows
170 and then iterates down the respective size events to all window. This has
171 the disadvantage that windows might get size events before the GTK widget
172 actually has the reported size. This doesn't normally pose any problem, but
173 the OpenGL drawing routines rely on correct behaviour. Therefore, I have
174 added the m_nativeSizeEvents flag, which is true only for the OpenGL canvas,
175 i.e. the wxGLCanvas will emit a size event, when (and not before) the X11
176 window that is used for OpenGL output really has that size (as reported by
181 If someone at some point of time feels the immense desire to have a look at,
182 change or attempt to optimise the Refresh() logic, this person will need an
183 intimate understanding of what "draw" and "expose" events are and what
184 they are used for, in particular when used in connection with GTK's
185 own windowless widgets. Beware.
189 Cursors, too, have been a constant source of pleasure. The main difficulty
190 is that a GdkWindow inherits a cursor if the programmer sets a new cursor
191 for the parent. To prevent this from doing too much harm, SetCursor calls
192 GTKUpdateCursor, which will recursively re-set the cursors of all child windows.
193 Also don't forget that cursors (like much else) are connected to GdkWindows,
194 not GtkWidgets and that the "window" field of a GtkWidget might very well
195 point to the GdkWindow of the parent widget (-> "window-less widget") and
196 that the two obviously have very different meanings.
199 //-----------------------------------------------------------------------------
201 //-----------------------------------------------------------------------------
203 // Don't allow event propagation during drag
204 bool g_blockEventsOnDrag
;
205 // Don't allow mouse event propagation during scroll
206 bool g_blockEventsOnScroll
;
207 extern wxCursor g_globalCursor
;
209 // mouse capture state: the window which has it and if the mouse is currently
211 static wxWindowGTK
*g_captureWindow
= NULL
;
212 static bool g_captureWindowHasMouse
= false;
214 // The window that currently has focus:
215 static wxWindowGTK
*gs_currentFocus
= NULL
;
216 // The window that is scheduled to get focus in the next event loop iteration
217 // or NULL if there's no pending focus change:
218 static wxWindowGTK
*gs_pendingFocus
= NULL
;
220 // the window that has deferred focus-out event pending, if any (see
221 // GTKAddDeferredFocusOut() for details)
222 static wxWindowGTK
*gs_deferredFocusOut
= NULL
;
224 // global variables because GTK+ DnD want to have the
225 // mouse event that caused it
226 GdkEvent
*g_lastMouseEvent
= NULL
;
227 int g_lastButtonNumber
= 0;
229 //-----------------------------------------------------------------------------
231 //-----------------------------------------------------------------------------
233 // the trace mask used for the focus debugging messages
234 #define TRACE_FOCUS wxT("focus")
236 //-----------------------------------------------------------------------------
237 // missing gdk functions
238 //-----------------------------------------------------------------------------
241 gdk_window_warp_pointer (GdkWindow
*window
,
246 window
= gdk_get_default_root_window();
248 if (!GDK_WINDOW_DESTROYED(window
))
250 XWarpPointer (GDK_WINDOW_XDISPLAY(window
),
251 None
, /* not source window -> move from anywhere */
252 GDK_WINDOW_XID(window
), /* dest window */
253 0, 0, 0, 0, /* not source window -> move from anywhere */
259 //-----------------------------------------------------------------------------
260 // "size_request" of m_widget
261 //-----------------------------------------------------------------------------
265 wxgtk_window_size_request_callback(GtkWidget
* WXUNUSED(widget
),
266 GtkRequisition
*requisition
,
270 win
->GetSize( &w
, &h
);
276 requisition
->height
= h
;
277 requisition
->width
= w
;
281 //-----------------------------------------------------------------------------
282 // "expose_event" of m_wxwindow
283 //-----------------------------------------------------------------------------
287 gtk_window_expose_callback( GtkWidget
*,
288 GdkEventExpose
*gdk_event
,
291 if (gdk_event
->window
== win
->GTKGetDrawingWindow())
293 win
->GetUpdateRegion() = wxRegion( gdk_event
->region
);
294 win
->GtkSendPaintEvents();
296 // Let parent window draw window-less widgets
301 #ifndef __WXUNIVERSAL__
302 //-----------------------------------------------------------------------------
303 // "expose_event" from m_wxwindow->parent, for drawing border
304 //-----------------------------------------------------------------------------
308 expose_event_border(GtkWidget
* widget
, GdkEventExpose
* gdk_event
, wxWindow
* win
)
310 if (gdk_event
->window
!= gtk_widget_get_parent_window(win
->m_wxwindow
))
317 gtk_widget_get_allocation(win
->m_wxwindow
, &alloc
);
318 const int x
= alloc
.x
;
319 const int y
= alloc
.y
;
320 const int w
= alloc
.width
;
321 const int h
= alloc
.height
;
323 if (w
<= 0 || h
<= 0)
326 if (win
->HasFlag(wxBORDER_SIMPLE
))
328 gdk_draw_rectangle(gdk_event
->window
,
329 gtk_widget_get_style(widget
)->black_gc
, false, x
, y
, w
- 1, h
- 1);
333 GtkShadowType shadow
= GTK_SHADOW_IN
;
334 if (win
->HasFlag(wxBORDER_RAISED
))
335 shadow
= GTK_SHADOW_OUT
;
337 // Style detail to use
339 if (win
->m_widget
== win
->m_wxwindow
)
340 // for non-scrollable wxWindows
343 // for scrollable ones
346 // clip rect is required to avoid painting background
347 // over upper left (w,h) of parent window
348 GdkRectangle clipRect
= { x
, y
, w
, h
};
350 gtk_widget_get_style(win
->m_wxwindow
), gdk_event
->window
, GTK_STATE_NORMAL
,
351 shadow
, &clipRect
, wxGTKPrivate::GetEntryWidget(), detail
, x
, y
, w
, h
);
357 //-----------------------------------------------------------------------------
358 // "parent_set" from m_wxwindow
359 //-----------------------------------------------------------------------------
363 parent_set(GtkWidget
* widget
, GtkObject
* old_parent
, wxWindow
* win
)
367 g_signal_handlers_disconnect_by_func(
368 old_parent
, (void*)expose_event_border
, win
);
370 GtkWidget
* parent
= gtk_widget_get_parent(widget
);
373 g_signal_connect_after(parent
, "expose_event",
374 G_CALLBACK(expose_event_border
), win
);
378 #endif // !__WXUNIVERSAL__
380 //-----------------------------------------------------------------------------
381 // "key_press_event" from any window
382 //-----------------------------------------------------------------------------
384 // These are used when transforming Ctrl-alpha to ascii values 1-26
385 inline bool wxIsLowerChar(int code
)
387 return (code
>= 'a' && code
<= 'z' );
390 inline bool wxIsUpperChar(int code
)
392 return (code
>= 'A' && code
<= 'Z' );
396 // set WXTRACE to this to see the key event codes on the console
397 #define TRACE_KEYS wxT("keyevent")
399 // translates an X key symbol to WXK_XXX value
401 // if isChar is true it means that the value returned will be used for EVT_CHAR
402 // event and then we choose the logical WXK_XXX, i.e. '/' for GDK_KP_Divide,
403 // for example, while if it is false it means that the value is going to be
404 // used for KEY_DOWN/UP events and then we translate GDK_KP_Divide to
406 static long wxTranslateKeySymToWXKey(KeySym keysym
, bool isChar
)
412 // Shift, Control and Alt don't generate the CHAR events at all
415 key_code
= isChar
? 0 : WXK_SHIFT
;
419 key_code
= isChar
? 0 : WXK_CONTROL
;
427 key_code
= isChar
? 0 : WXK_ALT
;
430 // neither do the toggle modifies
431 case GDK_Scroll_Lock
:
432 key_code
= isChar
? 0 : WXK_SCROLL
;
436 key_code
= isChar
? 0 : WXK_CAPITAL
;
440 key_code
= isChar
? 0 : WXK_NUMLOCK
;
444 // various other special keys
457 case GDK_ISO_Left_Tab
:
464 key_code
= WXK_RETURN
;
468 key_code
= WXK_CLEAR
;
472 key_code
= WXK_PAUSE
;
476 key_code
= WXK_SELECT
;
480 key_code
= WXK_PRINT
;
484 key_code
= WXK_EXECUTE
;
488 key_code
= WXK_ESCAPE
;
491 // cursor and other extended keyboard keys
493 key_code
= WXK_DELETE
;
509 key_code
= WXK_RIGHT
;
516 case GDK_Prior
: // == GDK_Page_Up
517 key_code
= WXK_PAGEUP
;
520 case GDK_Next
: // == GDK_Page_Down
521 key_code
= WXK_PAGEDOWN
;
533 key_code
= WXK_INSERT
;
548 key_code
= (isChar
? '0' : int(WXK_NUMPAD0
)) + keysym
- GDK_KP_0
;
552 key_code
= isChar
? ' ' : int(WXK_NUMPAD_SPACE
);
556 key_code
= isChar
? WXK_TAB
: WXK_NUMPAD_TAB
;
560 key_code
= isChar
? WXK_RETURN
: WXK_NUMPAD_ENTER
;
564 key_code
= isChar
? WXK_F1
: WXK_NUMPAD_F1
;
568 key_code
= isChar
? WXK_F2
: WXK_NUMPAD_F2
;
572 key_code
= isChar
? WXK_F3
: WXK_NUMPAD_F3
;
576 key_code
= isChar
? WXK_F4
: WXK_NUMPAD_F4
;
580 key_code
= isChar
? WXK_HOME
: WXK_NUMPAD_HOME
;
584 key_code
= isChar
? WXK_LEFT
: WXK_NUMPAD_LEFT
;
588 key_code
= isChar
? WXK_UP
: WXK_NUMPAD_UP
;
592 key_code
= isChar
? WXK_RIGHT
: WXK_NUMPAD_RIGHT
;
596 key_code
= isChar
? WXK_DOWN
: WXK_NUMPAD_DOWN
;
599 case GDK_KP_Prior
: // == GDK_KP_Page_Up
600 key_code
= isChar
? WXK_PAGEUP
: WXK_NUMPAD_PAGEUP
;
603 case GDK_KP_Next
: // == GDK_KP_Page_Down
604 key_code
= isChar
? WXK_PAGEDOWN
: WXK_NUMPAD_PAGEDOWN
;
608 key_code
= isChar
? WXK_END
: WXK_NUMPAD_END
;
612 key_code
= isChar
? WXK_HOME
: WXK_NUMPAD_BEGIN
;
616 key_code
= isChar
? WXK_INSERT
: WXK_NUMPAD_INSERT
;
620 key_code
= isChar
? WXK_DELETE
: WXK_NUMPAD_DELETE
;
624 key_code
= isChar
? '=' : int(WXK_NUMPAD_EQUAL
);
627 case GDK_KP_Multiply
:
628 key_code
= isChar
? '*' : int(WXK_NUMPAD_MULTIPLY
);
632 key_code
= isChar
? '+' : int(WXK_NUMPAD_ADD
);
635 case GDK_KP_Separator
:
636 // FIXME: what is this?
637 key_code
= isChar
? '.' : int(WXK_NUMPAD_SEPARATOR
);
640 case GDK_KP_Subtract
:
641 key_code
= isChar
? '-' : int(WXK_NUMPAD_SUBTRACT
);
645 key_code
= isChar
? '.' : int(WXK_NUMPAD_DECIMAL
);
649 key_code
= isChar
? '/' : int(WXK_NUMPAD_DIVIDE
);
666 key_code
= WXK_F1
+ keysym
- GDK_F1
;
676 static inline bool wxIsAsciiKeysym(KeySym ks
)
681 static void wxFillOtherKeyEventFields(wxKeyEvent
& event
,
683 GdkEventKey
*gdk_event
)
685 event
.SetTimestamp( gdk_event
->time
);
686 event
.SetId(win
->GetId());
688 event
.m_shiftDown
= (gdk_event
->state
& GDK_SHIFT_MASK
) != 0;
689 event
.m_controlDown
= (gdk_event
->state
& GDK_CONTROL_MASK
) != 0;
690 event
.m_altDown
= (gdk_event
->state
& GDK_MOD1_MASK
) != 0;
691 event
.m_metaDown
= (gdk_event
->state
& GDK_META_MASK
) != 0;
693 // Normally we take the state of modifiers directly from the low level GDK
694 // event but unfortunately GDK uses a different convention from MSW for the
695 // key events corresponding to the modifier keys themselves: in it, when
696 // e.g. Shift key is pressed, GDK_SHIFT_MASK is not set while it is set
697 // when Shift is released. Under MSW the situation is exactly reversed and
698 // the modifier corresponding to the key is set when it is pressed and
699 // unset when it is released. To ensure consistent behaviour between
700 // platforms (and because it seems to make slightly more sense, although
701 // arguably both behaviours are reasonable) we follow MSW here.
703 // Final notice: we set the flags to the desired value instead of just
704 // inverting them because they are not set correctly (i.e. in the same way
705 // as for the real events generated by the user) for wxUIActionSimulator-
706 // produced events and it seems better to keep that class code the same
707 // among all platforms and fix the discrepancy here instead of adding
708 // wxGTK-specific code to wxUIActionSimulator.
709 const bool isPress
= gdk_event
->type
== GDK_KEY_PRESS
;
710 switch ( gdk_event
->keyval
)
714 event
.m_shiftDown
= isPress
;
719 event
.m_controlDown
= isPress
;
724 event
.m_altDown
= isPress
;
731 event
.m_metaDown
= isPress
;
735 event
.m_rawCode
= (wxUint32
) gdk_event
->keyval
;
736 event
.m_rawFlags
= gdk_event
->hardware_keycode
;
738 wxGetMousePosition(&event
.m_x
, &event
.m_y
);
739 win
->ScreenToClient(&event
.m_x
, &event
.m_y
);
740 event
.SetEventObject( win
);
745 wxTranslateGTKKeyEventToWx(wxKeyEvent
& event
,
747 GdkEventKey
*gdk_event
)
749 // VZ: it seems that GDK_KEY_RELEASE event doesn't set event->string
750 // but only event->keyval which is quite useless to us, so remember
751 // the last character from GDK_KEY_PRESS and reuse it as last resort
753 // NB: should be MT-safe as we're always called from the main thread only
758 } s_lastKeyPress
= { 0, 0 };
760 KeySym keysym
= gdk_event
->keyval
;
762 wxLogTrace(TRACE_KEYS
, wxT("Key %s event: keysym = %ld"),
763 event
.GetEventType() == wxEVT_KEY_UP
? wxT("release")
767 long key_code
= wxTranslateKeySymToWXKey(keysym
, false /* !isChar */);
771 // do we have the translation or is it a plain ASCII character?
772 if ( (gdk_event
->length
== 1) || wxIsAsciiKeysym(keysym
) )
774 // we should use keysym if it is ASCII as X does some translations
775 // like "I pressed while Control is down" => "Ctrl-I" == "TAB"
776 // which we don't want here (but which we do use for OnChar())
777 if ( !wxIsAsciiKeysym(keysym
) )
779 keysym
= (KeySym
)gdk_event
->string
[0];
782 // we want to always get the same key code when the same key is
783 // pressed regardless of the state of the modifiers, i.e. on a
784 // standard US keyboard pressing '5' or '%' ('5' key with
785 // Shift) should result in the same key code in OnKeyDown():
786 // '5' (although OnChar() will get either '5' or '%').
788 // to do it we first translate keysym to keycode (== scan code)
789 // and then back but always using the lower register
790 Display
*dpy
= (Display
*)wxGetDisplay();
791 KeyCode keycode
= XKeysymToKeycode(dpy
, keysym
);
793 wxLogTrace(TRACE_KEYS
, wxT("\t-> keycode %d"), keycode
);
795 KeySym keysymNormalized
= XKeycodeToKeysym(dpy
, keycode
, 0);
797 // use the normalized, i.e. lower register, keysym if we've
799 key_code
= keysymNormalized
? keysymNormalized
: keysym
;
801 // as explained above, we want to have lower register key codes
802 // normally but for the letter keys we want to have the upper ones
804 // NB: don't use XConvertCase() here, we want to do it for letters
806 key_code
= toupper(key_code
);
808 else // non ASCII key, what to do?
810 // by default, ignore it
813 // but if we have cached information from the last KEY_PRESS
814 if ( gdk_event
->type
== GDK_KEY_RELEASE
)
817 if ( keysym
== s_lastKeyPress
.keysym
)
819 key_code
= s_lastKeyPress
.keycode
;
824 if ( gdk_event
->type
== GDK_KEY_PRESS
)
826 // remember it to be reused for KEY_UP event later
827 s_lastKeyPress
.keysym
= keysym
;
828 s_lastKeyPress
.keycode
= key_code
;
832 wxLogTrace(TRACE_KEYS
, wxT("\t-> wxKeyCode %ld"), key_code
);
834 // sending unknown key events doesn't really make sense
838 event
.m_keyCode
= key_code
;
841 event
.m_uniChar
= gdk_keyval_to_unicode(key_code
? key_code
: keysym
);
842 if ( !event
.m_uniChar
&& event
.m_keyCode
<= WXK_DELETE
)
844 // Set Unicode key code to the ASCII equivalent for compatibility. E.g.
845 // let RETURN generate the key event with both key and Unicode key
847 event
.m_uniChar
= event
.m_keyCode
;
849 #endif // wxUSE_UNICODE
851 // now fill all the other fields
852 wxFillOtherKeyEventFields(event
, win
, gdk_event
);
860 GtkIMContext
*context
;
861 GdkEventKey
*lastKeyEvent
;
865 context
= gtk_im_multicontext_new();
870 g_object_unref (context
);
877 // Send wxEVT_CHAR_HOOK event to the parent of the window and if it wasn't
878 // processed, send wxEVT_CHAR to the window itself. Return true if either of
881 SendCharHookAndCharEvents(const wxKeyEvent
& event
, wxWindow
*win
)
883 // wxEVT_CHAR_HOOK must be sent to the top level parent window to allow it
884 // to handle key events in all of its children unless the mouse is captured
885 // in which case we consider that the keyboard should be "captured" too.
886 if ( !g_captureWindow
)
888 wxWindow
* const parent
= wxGetTopLevelParent(win
);
891 // We need to make a copy of the event object because it is
892 // modified while it's handled, notably its WasProcessed() flag
893 // is set after it had been processed once.
894 wxKeyEvent
eventCharHook(event
);
895 eventCharHook
.SetEventType(wxEVT_CHAR_HOOK
);
896 if ( parent
->HandleWindowEvent(eventCharHook
) )
901 // As above, make a copy of the event first.
902 wxKeyEvent
eventChar(event
);
903 eventChar
.SetEventType(wxEVT_CHAR
);
904 return win
->HandleWindowEvent(eventChar
);
907 } // anonymous namespace
911 gtk_window_key_press_callback( GtkWidget
*WXUNUSED(widget
),
912 GdkEventKey
*gdk_event
,
917 if (g_blockEventsOnDrag
)
920 wxKeyEvent
event( wxEVT_KEY_DOWN
);
922 bool return_after_IM
= false;
924 if( wxTranslateGTKKeyEventToWx(event
, win
, gdk_event
) )
926 // Emit KEY_DOWN event
927 ret
= win
->HandleWindowEvent( event
);
931 // Return after IM processing as we cannot do
932 // anything with it anyhow.
933 return_after_IM
= true;
936 if (!ret
&& win
->m_imData
)
938 win
->m_imData
->lastKeyEvent
= gdk_event
;
940 // We should let GTK+ IM filter key event first. According to GTK+ 2.0 API
941 // docs, if IM filter returns true, no further processing should be done.
942 // we should send the key_down event anyway.
943 bool intercepted_by_IM
= gtk_im_context_filter_keypress(win
->m_imData
->context
, gdk_event
);
944 win
->m_imData
->lastKeyEvent
= NULL
;
945 if (intercepted_by_IM
)
947 wxLogTrace(TRACE_KEYS
, wxT("Key event intercepted by IM"));
958 wxWindowGTK
*ancestor
= win
;
961 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
964 wxCommandEvent
menu_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
965 ret
= ancestor
->HandleWindowEvent( menu_event
);
969 // if the accelerator wasn't handled as menu event, try
970 // it as button click (for compatibility with other
972 wxCommandEvent
button_event( wxEVT_COMMAND_BUTTON_CLICKED
, command
);
973 ret
= ancestor
->HandleWindowEvent( button_event
);
978 if (ancestor
->IsTopLevel())
980 ancestor
= ancestor
->GetParent();
983 #endif // wxUSE_ACCEL
985 // Only send wxEVT_CHAR event if not processed yet. Thus, ALT-x
986 // will only be sent if it is not in an accelerator table.
990 KeySym keysym
= gdk_event
->keyval
;
991 // Find key code for EVT_CHAR and EVT_CHAR_HOOK events
992 key_code
= wxTranslateKeySymToWXKey(keysym
, true /* isChar */);
995 if ( wxIsAsciiKeysym(keysym
) )
998 key_code
= (unsigned char)keysym
;
1000 // gdk_event->string is actually deprecated
1001 else if ( gdk_event
->length
== 1 )
1003 key_code
= (unsigned char)gdk_event
->string
[0];
1009 wxLogTrace(TRACE_KEYS
, wxT("Char event: %ld"), key_code
);
1011 event
.m_keyCode
= key_code
;
1013 // To conform to the docs we need to translate Ctrl-alpha
1014 // characters to values in the range 1-26.
1015 if ( event
.ControlDown() &&
1016 ( wxIsLowerChar(key_code
) || wxIsUpperChar(key_code
) ))
1018 if ( wxIsLowerChar(key_code
) )
1019 event
.m_keyCode
= key_code
- 'a' + 1;
1020 if ( wxIsUpperChar(key_code
) )
1021 event
.m_keyCode
= key_code
- 'A' + 1;
1023 event
.m_uniChar
= event
.m_keyCode
;
1027 ret
= SendCharHookAndCharEvents(event
, win
);
1037 gtk_wxwindow_commit_cb (GtkIMContext
* WXUNUSED(context
),
1041 wxKeyEvent
event( wxEVT_KEY_DOWN
);
1043 // take modifiers, cursor position, timestamp etc. from the last
1044 // key_press_event that was fed into Input Method:
1045 if (window
->m_imData
->lastKeyEvent
)
1047 wxFillOtherKeyEventFields(event
,
1048 window
, window
->m_imData
->lastKeyEvent
);
1052 event
.SetEventObject( window
);
1055 const wxString
data(wxGTK_CONV_BACK_SYS(str
));
1059 for( wxString::const_iterator pstr
= data
.begin(); pstr
!= data
.end(); ++pstr
)
1062 event
.m_uniChar
= *pstr
;
1063 // Backward compatible for ISO-8859-1
1064 event
.m_keyCode
= *pstr
< 256 ? event
.m_uniChar
: 0;
1065 wxLogTrace(TRACE_KEYS
, wxT("IM sent character '%c'"), event
.m_uniChar
);
1067 event
.m_keyCode
= (char)*pstr
;
1068 #endif // wxUSE_UNICODE
1070 // To conform to the docs we need to translate Ctrl-alpha
1071 // characters to values in the range 1-26.
1072 if ( event
.ControlDown() &&
1073 ( wxIsLowerChar(*pstr
) || wxIsUpperChar(*pstr
) ))
1075 if ( wxIsLowerChar(*pstr
) )
1076 event
.m_keyCode
= *pstr
- 'a' + 1;
1077 if ( wxIsUpperChar(*pstr
) )
1078 event
.m_keyCode
= *pstr
- 'A' + 1;
1080 event
.m_keyCode
= *pstr
- 'a' + 1;
1082 event
.m_uniChar
= event
.m_keyCode
;
1086 SendCharHookAndCharEvents(event
, window
);
1092 //-----------------------------------------------------------------------------
1093 // "key_release_event" from any window
1094 //-----------------------------------------------------------------------------
1098 gtk_window_key_release_callback( GtkWidget
* WXUNUSED(widget
),
1099 GdkEventKey
*gdk_event
,
1105 if (g_blockEventsOnDrag
)
1108 wxKeyEvent
event( wxEVT_KEY_UP
);
1109 if ( !wxTranslateGTKKeyEventToWx(event
, win
, gdk_event
) )
1111 // unknown key pressed, ignore (the event would be useless anyhow)
1115 return win
->GTKProcessEvent(event
);
1119 // ============================================================================
1121 // ============================================================================
1123 // ----------------------------------------------------------------------------
1124 // mouse event processing helpers
1125 // ----------------------------------------------------------------------------
1127 // init wxMouseEvent with the info from GdkEventXXX struct
1128 template<typename T
> void InitMouseEvent(wxWindowGTK
*win
,
1129 wxMouseEvent
& event
,
1132 event
.SetTimestamp( gdk_event
->time
);
1133 event
.m_shiftDown
= (gdk_event
->state
& GDK_SHIFT_MASK
) != 0;
1134 event
.m_controlDown
= (gdk_event
->state
& GDK_CONTROL_MASK
) != 0;
1135 event
.m_altDown
= (gdk_event
->state
& GDK_MOD1_MASK
) != 0;
1136 event
.m_metaDown
= (gdk_event
->state
& GDK_META_MASK
) != 0;
1137 event
.m_leftDown
= (gdk_event
->state
& GDK_BUTTON1_MASK
) != 0;
1138 event
.m_middleDown
= (gdk_event
->state
& GDK_BUTTON2_MASK
) != 0;
1139 event
.m_rightDown
= (gdk_event
->state
& GDK_BUTTON3_MASK
) != 0;
1141 // In gdk/win32 VK_XBUTTON1 is translated to GDK_BUTTON4_MASK
1142 // and VK_XBUTTON2 to GDK_BUTTON5_MASK. In x11/gdk buttons 4/5
1143 // are wheel rotation and buttons 8/9 don't change the state.
1144 event
.m_aux1Down
= (gdk_event
->state
& GDK_BUTTON4_MASK
) != 0;
1145 event
.m_aux2Down
= (gdk_event
->state
& GDK_BUTTON5_MASK
) != 0;
1147 wxPoint pt
= win
->GetClientAreaOrigin();
1148 event
.m_x
= (wxCoord
)gdk_event
->x
- pt
.x
;
1149 event
.m_y
= (wxCoord
)gdk_event
->y
- pt
.y
;
1151 if ((win
->m_wxwindow
) && (win
->GetLayoutDirection() == wxLayout_RightToLeft
))
1153 // origin in the upper right corner
1155 gtk_widget_get_allocation(win
->m_wxwindow
, &a
);
1156 int window_width
= a
.width
;
1157 event
.m_x
= window_width
- event
.m_x
;
1160 event
.SetEventObject( win
);
1161 event
.SetId( win
->GetId() );
1162 event
.SetTimestamp( gdk_event
->time
);
1165 static void AdjustEventButtonState(wxMouseEvent
& event
)
1167 // GDK reports the old state of the button for a button press event, but
1168 // for compatibility with MSW and common sense we want m_leftDown be TRUE
1169 // for a LEFT_DOWN event, not FALSE, so we will invert
1170 // left/right/middleDown for the corresponding click events
1172 if ((event
.GetEventType() == wxEVT_LEFT_DOWN
) ||
1173 (event
.GetEventType() == wxEVT_LEFT_DCLICK
) ||
1174 (event
.GetEventType() == wxEVT_LEFT_UP
))
1176 event
.m_leftDown
= !event
.m_leftDown
;
1180 if ((event
.GetEventType() == wxEVT_MIDDLE_DOWN
) ||
1181 (event
.GetEventType() == wxEVT_MIDDLE_DCLICK
) ||
1182 (event
.GetEventType() == wxEVT_MIDDLE_UP
))
1184 event
.m_middleDown
= !event
.m_middleDown
;
1188 if ((event
.GetEventType() == wxEVT_RIGHT_DOWN
) ||
1189 (event
.GetEventType() == wxEVT_RIGHT_DCLICK
) ||
1190 (event
.GetEventType() == wxEVT_RIGHT_UP
))
1192 event
.m_rightDown
= !event
.m_rightDown
;
1196 if ((event
.GetEventType() == wxEVT_AUX1_DOWN
) ||
1197 (event
.GetEventType() == wxEVT_AUX1_DCLICK
))
1199 event
.m_aux1Down
= true;
1203 if ((event
.GetEventType() == wxEVT_AUX2_DOWN
) ||
1204 (event
.GetEventType() == wxEVT_AUX2_DCLICK
))
1206 event
.m_aux2Down
= true;
1211 // find the window to send the mouse event too
1213 wxWindowGTK
*FindWindowForMouseEvent(wxWindowGTK
*win
, wxCoord
& x
, wxCoord
& y
)
1218 if (win
->m_wxwindow
)
1220 wxPizza
* pizza
= WX_PIZZA(win
->m_wxwindow
);
1221 xx
+= pizza
->m_scroll_x
;
1222 yy
+= pizza
->m_scroll_y
;
1225 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
1228 wxWindowGTK
*child
= node
->GetData();
1230 node
= node
->GetNext();
1231 if (!child
->IsShown())
1234 if (child
->GTKIsTransparentForMouse())
1236 // wxStaticBox is transparent in the box itself
1237 int xx1
= child
->m_x
;
1238 int yy1
= child
->m_y
;
1239 int xx2
= child
->m_x
+ child
->m_width
;
1240 int yy2
= child
->m_y
+ child
->m_height
;
1243 if (((xx
>= xx1
) && (xx
<= xx1
+10) && (yy
>= yy1
) && (yy
<= yy2
)) ||
1245 ((xx
>= xx2
-10) && (xx
<= xx2
) && (yy
>= yy1
) && (yy
<= yy2
)) ||
1247 ((xx
>= xx1
) && (xx
<= xx2
) && (yy
>= yy1
) && (yy
<= yy1
+10)) ||
1249 ((xx
>= xx1
) && (xx
<= xx2
) && (yy
>= yy2
-1) && (yy
<= yy2
)))
1260 if ((child
->m_wxwindow
== NULL
) &&
1261 (child
->m_x
<= xx
) &&
1262 (child
->m_y
<= yy
) &&
1263 (child
->m_x
+child
->m_width
>= xx
) &&
1264 (child
->m_y
+child
->m_height
>= yy
))
1277 // ----------------------------------------------------------------------------
1278 // common event handlers helpers
1279 // ----------------------------------------------------------------------------
1281 bool wxWindowGTK::GTKProcessEvent(wxEvent
& event
) const
1283 // nothing special at this level
1284 return HandleWindowEvent(event
);
1287 bool wxWindowGTK::GTKShouldIgnoreEvent() const
1289 return !m_hasVMT
|| g_blockEventsOnDrag
;
1292 int wxWindowGTK::GTKCallbackCommonPrologue(GdkEventAny
*event
) const
1296 if (g_blockEventsOnDrag
)
1298 if (g_blockEventsOnScroll
)
1301 if (!GTKIsOwnWindow(event
->window
))
1307 // overloads for all GDK event types we use here: we need to have this as
1308 // GdkEventXXX can't be implicitly cast to GdkEventAny even if it, in fact,
1309 // derives from it in the sense that the structs have the same layout
1310 #define wxDEFINE_COMMON_PROLOGUE_OVERLOAD(T) \
1311 static int wxGtkCallbackCommonPrologue(T *event, wxWindowGTK *win) \
1313 return win->GTKCallbackCommonPrologue((GdkEventAny *)event); \
1316 wxDEFINE_COMMON_PROLOGUE_OVERLOAD(GdkEventButton
)
1317 wxDEFINE_COMMON_PROLOGUE_OVERLOAD(GdkEventMotion
)
1318 wxDEFINE_COMMON_PROLOGUE_OVERLOAD(GdkEventCrossing
)
1320 #undef wxDEFINE_COMMON_PROLOGUE_OVERLOAD
1322 #define wxCOMMON_CALLBACK_PROLOGUE(event, win) \
1323 const int rc = wxGtkCallbackCommonPrologue(event, win); \
1327 // all event handlers must have C linkage as they're called from GTK+ C code
1331 //-----------------------------------------------------------------------------
1332 // "button_press_event"
1333 //-----------------------------------------------------------------------------
1336 gtk_window_button_press_callback( GtkWidget
*widget
,
1337 GdkEventButton
*gdk_event
,
1340 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1342 g_lastButtonNumber
= gdk_event
->button
;
1344 // GDK sends surplus button down events
1345 // before a double click event. We
1346 // need to filter these out.
1347 if ((gdk_event
->type
== GDK_BUTTON_PRESS
) && (win
->m_wxwindow
))
1349 GdkEvent
*peek_event
= gdk_event_peek();
1352 if ((peek_event
->type
== GDK_2BUTTON_PRESS
) ||
1353 (peek_event
->type
== GDK_3BUTTON_PRESS
))
1355 gdk_event_free( peek_event
);
1360 gdk_event_free( peek_event
);
1365 wxEventType event_type
= wxEVT_NULL
;
1367 if ( gdk_event
->type
== GDK_2BUTTON_PRESS
&&
1368 gdk_event
->button
>= 1 && gdk_event
->button
<= 3 )
1370 // Reset GDK internal timestamp variables in order to disable GDK
1371 // triple click events. GDK will then next time believe no button has
1372 // been clicked just before, and send a normal button click event.
1373 GdkDisplay
* display
= gtk_widget_get_display (widget
);
1374 display
->button_click_time
[1] = 0;
1375 display
->button_click_time
[0] = 0;
1378 if (gdk_event
->button
== 1)
1380 // note that GDK generates triple click events which are not supported
1381 // by wxWidgets but still have to be passed to the app as otherwise
1382 // clicks would simply go missing
1383 switch (gdk_event
->type
)
1385 // we shouldn't get triple clicks at all for GTK2 because we
1386 // suppress them artificially using the code above but we still
1387 // should map them to something for GTK1 and not just ignore them
1388 // as this would lose clicks
1389 case GDK_3BUTTON_PRESS
: // we could also map this to DCLICK...
1390 case GDK_BUTTON_PRESS
:
1391 event_type
= wxEVT_LEFT_DOWN
;
1394 case GDK_2BUTTON_PRESS
:
1395 event_type
= wxEVT_LEFT_DCLICK
;
1399 // just to silence gcc warnings
1403 else if (gdk_event
->button
== 2)
1405 switch (gdk_event
->type
)
1407 case GDK_3BUTTON_PRESS
:
1408 case GDK_BUTTON_PRESS
:
1409 event_type
= wxEVT_MIDDLE_DOWN
;
1412 case GDK_2BUTTON_PRESS
:
1413 event_type
= wxEVT_MIDDLE_DCLICK
;
1420 else if (gdk_event
->button
== 3)
1422 switch (gdk_event
->type
)
1424 case GDK_3BUTTON_PRESS
:
1425 case GDK_BUTTON_PRESS
:
1426 event_type
= wxEVT_RIGHT_DOWN
;
1429 case GDK_2BUTTON_PRESS
:
1430 event_type
= wxEVT_RIGHT_DCLICK
;
1438 else if (gdk_event
->button
== 8)
1440 switch (gdk_event
->type
)
1442 case GDK_3BUTTON_PRESS
:
1443 case GDK_BUTTON_PRESS
:
1444 event_type
= wxEVT_AUX1_DOWN
;
1447 case GDK_2BUTTON_PRESS
:
1448 event_type
= wxEVT_AUX1_DCLICK
;
1456 else if (gdk_event
->button
== 9)
1458 switch (gdk_event
->type
)
1460 case GDK_3BUTTON_PRESS
:
1461 case GDK_BUTTON_PRESS
:
1462 event_type
= wxEVT_AUX2_DOWN
;
1465 case GDK_2BUTTON_PRESS
:
1466 event_type
= wxEVT_AUX2_DCLICK
;
1474 if ( event_type
== wxEVT_NULL
)
1476 // unknown mouse button or click type
1480 g_lastMouseEvent
= (GdkEvent
*) gdk_event
;
1482 wxMouseEvent
event( event_type
);
1483 InitMouseEvent( win
, event
, gdk_event
);
1485 AdjustEventButtonState(event
);
1487 // find the correct window to send the event to: it may be a different one
1488 // from the one which got it at GTK+ level because some controls don't have
1489 // their own X window and thus cannot get any events.
1490 if ( !g_captureWindow
)
1491 win
= FindWindowForMouseEvent(win
, event
.m_x
, event
.m_y
);
1493 // reset the event object and id in case win changed.
1494 event
.SetEventObject( win
);
1495 event
.SetId( win
->GetId() );
1497 bool ret
= win
->GTKProcessEvent( event
);
1498 g_lastMouseEvent
= NULL
;
1502 if ((event_type
== wxEVT_LEFT_DOWN
) && !win
->IsOfStandardClass() &&
1503 (gs_currentFocus
!= win
) /* && win->IsFocusable() */)
1508 if (event_type
== wxEVT_RIGHT_DOWN
)
1510 // generate a "context menu" event: this is similar to right mouse
1511 // click under many GUIs except that it is generated differently
1512 // (right up under MSW, ctrl-click under Mac, right down here) and
1514 // (a) it's a command event and so is propagated to the parent
1515 // (b) under some ports it can be generated from kbd too
1516 // (c) it uses screen coords (because of (a))
1517 wxContextMenuEvent
evtCtx(
1520 win
->ClientToScreen(event
.GetPosition()));
1521 evtCtx
.SetEventObject(win
);
1522 return win
->GTKProcessEvent(evtCtx
);
1528 //-----------------------------------------------------------------------------
1529 // "button_release_event"
1530 //-----------------------------------------------------------------------------
1533 gtk_window_button_release_callback( GtkWidget
*WXUNUSED(widget
),
1534 GdkEventButton
*gdk_event
,
1537 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1539 g_lastButtonNumber
= 0;
1541 wxEventType event_type
= wxEVT_NULL
;
1543 switch (gdk_event
->button
)
1546 event_type
= wxEVT_LEFT_UP
;
1550 event_type
= wxEVT_MIDDLE_UP
;
1554 event_type
= wxEVT_RIGHT_UP
;
1558 event_type
= wxEVT_AUX1_UP
;
1562 event_type
= wxEVT_AUX2_UP
;
1566 // unknown button, don't process
1570 g_lastMouseEvent
= (GdkEvent
*) gdk_event
;
1572 wxMouseEvent
event( event_type
);
1573 InitMouseEvent( win
, event
, gdk_event
);
1575 AdjustEventButtonState(event
);
1577 if ( !g_captureWindow
)
1578 win
= FindWindowForMouseEvent(win
, event
.m_x
, event
.m_y
);
1580 // reset the event object and id in case win changed.
1581 event
.SetEventObject( win
);
1582 event
.SetId( win
->GetId() );
1584 bool ret
= win
->GTKProcessEvent(event
);
1586 g_lastMouseEvent
= NULL
;
1591 //-----------------------------------------------------------------------------
1592 // "motion_notify_event"
1593 //-----------------------------------------------------------------------------
1596 gtk_window_motion_notify_callback( GtkWidget
* WXUNUSED(widget
),
1597 GdkEventMotion
*gdk_event
,
1600 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1602 if (gdk_event
->is_hint
)
1606 GdkModifierType state
;
1607 gdk_window_get_pointer(gdk_event
->window
, &x
, &y
, &state
);
1612 g_lastMouseEvent
= (GdkEvent
*) gdk_event
;
1614 wxMouseEvent
event( wxEVT_MOTION
);
1615 InitMouseEvent(win
, event
, gdk_event
);
1617 if ( g_captureWindow
)
1619 // synthesise a mouse enter or leave event if needed
1620 GdkWindow
*winUnderMouse
= gdk_window_at_pointer(NULL
, NULL
);
1621 // This seems to be necessary and actually been added to
1622 // GDK itself in version 2.0.X
1625 bool hasMouse
= winUnderMouse
== gdk_event
->window
;
1626 if ( hasMouse
!= g_captureWindowHasMouse
)
1628 // the mouse changed window
1629 g_captureWindowHasMouse
= hasMouse
;
1631 wxMouseEvent
eventM(g_captureWindowHasMouse
? wxEVT_ENTER_WINDOW
1632 : wxEVT_LEAVE_WINDOW
);
1633 InitMouseEvent(win
, eventM
, gdk_event
);
1634 eventM
.SetEventObject(win
);
1635 win
->GTKProcessEvent(eventM
);
1640 win
= FindWindowForMouseEvent(win
, event
.m_x
, event
.m_y
);
1642 // reset the event object and id in case win changed.
1643 event
.SetEventObject( win
);
1644 event
.SetId( win
->GetId() );
1647 if ( !g_captureWindow
)
1649 wxSetCursorEvent
cevent( event
.m_x
, event
.m_y
);
1650 if (win
->GTKProcessEvent( cevent
))
1652 win
->SetCursor( cevent
.GetCursor() );
1656 bool ret
= win
->GTKProcessEvent(event
);
1658 g_lastMouseEvent
= NULL
;
1663 //-----------------------------------------------------------------------------
1664 // "scroll_event" (mouse wheel event)
1665 //-----------------------------------------------------------------------------
1668 window_scroll_event_hscrollbar(GtkWidget
*, GdkEventScroll
* gdk_event
, wxWindow
* win
)
1670 if (gdk_event
->direction
!= GDK_SCROLL_LEFT
&&
1671 gdk_event
->direction
!= GDK_SCROLL_RIGHT
)
1676 GtkRange
*range
= win
->m_scrollBar
[wxWindow::ScrollDir_Horz
];
1678 if (range
&& gtk_widget_get_visible(GTK_WIDGET(range
)))
1680 GtkAdjustment
* adj
= gtk_range_get_adjustment(range
);
1681 double delta
= gtk_adjustment_get_step_increment(adj
) * 3;
1682 if (gdk_event
->direction
== GDK_SCROLL_LEFT
)
1685 gtk_range_set_value(range
, gtk_adjustment_get_value(adj
) + delta
);
1694 window_scroll_event(GtkWidget
*, GdkEventScroll
* gdk_event
, wxWindow
* win
)
1696 if (gdk_event
->direction
!= GDK_SCROLL_UP
&&
1697 gdk_event
->direction
!= GDK_SCROLL_DOWN
)
1702 wxMouseEvent
event(wxEVT_MOUSEWHEEL
);
1703 InitMouseEvent(win
, event
, gdk_event
);
1705 // FIXME: Get these values from GTK or GDK
1706 event
.m_linesPerAction
= 3;
1707 event
.m_wheelDelta
= 120;
1708 if (gdk_event
->direction
== GDK_SCROLL_UP
)
1709 event
.m_wheelRotation
= 120;
1711 event
.m_wheelRotation
= -120;
1713 if (win
->GTKProcessEvent(event
))
1716 GtkRange
*range
= win
->m_scrollBar
[wxWindow::ScrollDir_Vert
];
1718 if (range
&& gtk_widget_get_visible(GTK_WIDGET(range
)))
1720 GtkAdjustment
* adj
= gtk_range_get_adjustment(range
);
1721 double delta
= gtk_adjustment_get_step_increment(adj
) * 3;
1722 if (gdk_event
->direction
== GDK_SCROLL_UP
)
1725 gtk_range_set_value(range
, gtk_adjustment_get_value(adj
) + delta
);
1733 //-----------------------------------------------------------------------------
1735 //-----------------------------------------------------------------------------
1737 static gboolean
wxgtk_window_popup_menu_callback(GtkWidget
*, wxWindowGTK
* win
)
1739 wxContextMenuEvent
event(wxEVT_CONTEXT_MENU
, win
->GetId(), wxPoint(-1, -1));
1740 event
.SetEventObject(win
);
1741 return win
->GTKProcessEvent(event
);
1744 //-----------------------------------------------------------------------------
1746 //-----------------------------------------------------------------------------
1749 gtk_window_focus_in_callback( GtkWidget
* WXUNUSED(widget
),
1750 GdkEventFocus
*WXUNUSED(event
),
1753 return win
->GTKHandleFocusIn();
1756 //-----------------------------------------------------------------------------
1757 // "focus_out_event"
1758 //-----------------------------------------------------------------------------
1761 gtk_window_focus_out_callback( GtkWidget
* WXUNUSED(widget
),
1762 GdkEventFocus
* WXUNUSED(gdk_event
),
1765 return win
->GTKHandleFocusOut();
1768 //-----------------------------------------------------------------------------
1770 //-----------------------------------------------------------------------------
1773 wx_window_focus_callback(GtkWidget
*widget
,
1774 GtkDirectionType
WXUNUSED(direction
),
1777 // the default handler for focus signal in GtkScrolledWindow sets
1778 // focus to the window itself even if it doesn't accept focus, i.e. has no
1779 // GTK_CAN_FOCUS in its style -- work around this by forcibly preventing
1780 // the signal from reaching gtk_scrolled_window_focus() if we don't have
1781 // any children which might accept focus (we know we don't accept the focus
1782 // ourselves as this signal is only connected in this case)
1783 if ( win
->GetChildren().empty() )
1784 g_signal_stop_emission_by_name(widget
, "focus");
1786 // we didn't change the focus
1790 //-----------------------------------------------------------------------------
1791 // "enter_notify_event"
1792 //-----------------------------------------------------------------------------
1795 gtk_window_enter_callback( GtkWidget
*widget
,
1796 GdkEventCrossing
*gdk_event
,
1799 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1801 // Event was emitted after a grab
1802 if (gdk_event
->mode
!= GDK_CROSSING_NORMAL
) return FALSE
;
1806 GdkModifierType state
= (GdkModifierType
)0;
1808 gdk_window_get_pointer(gtk_widget_get_window(widget
), &x
, &y
, &state
);
1810 wxMouseEvent
event( wxEVT_ENTER_WINDOW
);
1811 InitMouseEvent(win
, event
, gdk_event
);
1812 wxPoint pt
= win
->GetClientAreaOrigin();
1813 event
.m_x
= x
+ pt
.x
;
1814 event
.m_y
= y
+ pt
.y
;
1816 if ( !g_captureWindow
)
1818 wxSetCursorEvent
cevent( event
.m_x
, event
.m_y
);
1819 if (win
->GTKProcessEvent( cevent
))
1821 win
->SetCursor( cevent
.GetCursor() );
1825 return win
->GTKProcessEvent(event
);
1828 //-----------------------------------------------------------------------------
1829 // "leave_notify_event"
1830 //-----------------------------------------------------------------------------
1833 gtk_window_leave_callback( GtkWidget
*widget
,
1834 GdkEventCrossing
*gdk_event
,
1837 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1839 // Event was emitted after an ungrab
1840 if (gdk_event
->mode
!= GDK_CROSSING_NORMAL
) return FALSE
;
1842 wxMouseEvent
event( wxEVT_LEAVE_WINDOW
);
1846 GdkModifierType state
= (GdkModifierType
)0;
1848 gdk_window_get_pointer(gtk_widget_get_window(widget
), &x
, &y
, &state
);
1850 InitMouseEvent(win
, event
, gdk_event
);
1852 return win
->GTKProcessEvent(event
);
1855 //-----------------------------------------------------------------------------
1856 // "value_changed" from scrollbar
1857 //-----------------------------------------------------------------------------
1860 gtk_scrollbar_value_changed(GtkRange
* range
, wxWindow
* win
)
1862 wxEventType eventType
= win
->GTKGetScrollEventType(range
);
1863 if (eventType
!= wxEVT_NULL
)
1865 // Convert scroll event type to scrollwin event type
1866 eventType
+= wxEVT_SCROLLWIN_TOP
- wxEVT_SCROLL_TOP
;
1868 // find the scrollbar which generated the event
1869 wxWindowGTK::ScrollDir dir
= win
->ScrollDirFromRange(range
);
1871 // generate the corresponding wx event
1872 const int orient
= wxWindow::OrientFromScrollDir(dir
);
1873 wxScrollWinEvent
event(eventType
, win
->GetScrollPos(orient
), orient
);
1874 event
.SetEventObject(win
);
1876 win
->GTKProcessEvent(event
);
1880 //-----------------------------------------------------------------------------
1881 // "button_press_event" from scrollbar
1882 //-----------------------------------------------------------------------------
1885 gtk_scrollbar_button_press_event(GtkRange
*, GdkEventButton
*, wxWindow
* win
)
1887 g_blockEventsOnScroll
= true;
1888 win
->m_mouseButtonDown
= true;
1893 //-----------------------------------------------------------------------------
1894 // "event_after" from scrollbar
1895 //-----------------------------------------------------------------------------
1898 gtk_scrollbar_event_after(GtkRange
* range
, GdkEvent
* event
, wxWindow
* win
)
1900 if (event
->type
== GDK_BUTTON_RELEASE
)
1902 g_signal_handlers_block_by_func(range
, (void*)gtk_scrollbar_event_after
, win
);
1904 const int orient
= wxWindow::OrientFromScrollDir(
1905 win
->ScrollDirFromRange(range
));
1906 wxScrollWinEvent
evt(wxEVT_SCROLLWIN_THUMBRELEASE
,
1907 win
->GetScrollPos(orient
), orient
);
1908 evt
.SetEventObject(win
);
1909 win
->GTKProcessEvent(evt
);
1913 //-----------------------------------------------------------------------------
1914 // "button_release_event" from scrollbar
1915 //-----------------------------------------------------------------------------
1918 gtk_scrollbar_button_release_event(GtkRange
* range
, GdkEventButton
*, wxWindow
* win
)
1920 g_blockEventsOnScroll
= false;
1921 win
->m_mouseButtonDown
= false;
1922 // If thumb tracking
1923 if (win
->m_isScrolling
)
1925 win
->m_isScrolling
= false;
1926 // Hook up handler to send thumb release event after this emission is finished.
1927 // To allow setting scroll position from event handler, sending event must
1928 // be deferred until after the GtkRange handler for this signal has run
1929 g_signal_handlers_unblock_by_func(range
, (void*)gtk_scrollbar_event_after
, win
);
1935 //-----------------------------------------------------------------------------
1936 // "realize" from m_widget
1937 //-----------------------------------------------------------------------------
1940 gtk_window_realized_callback(GtkWidget
* widget
, wxWindow
* win
)
1944 gtk_im_context_set_client_window( win
->m_imData
->context
,
1945 win
->m_wxwindow
? win
->GTKGetDrawingWindow() : gtk_widget_get_window(widget
));
1948 // We cannot set colours and fonts before the widget
1949 // been realized, so we do this directly after realization
1950 // or otherwise in idle time
1952 if (win
->m_needsStyleChange
)
1954 win
->SetBackgroundStyle(win
->GetBackgroundStyle());
1955 win
->m_needsStyleChange
= false;
1958 wxWindowCreateEvent
event( win
);
1959 event
.SetEventObject( win
);
1960 win
->GTKProcessEvent( event
);
1962 win
->GTKUpdateCursor(true, false);
1965 //-----------------------------------------------------------------------------
1966 // "size_allocate" from m_wxwindow or m_widget
1967 //-----------------------------------------------------------------------------
1970 size_allocate(GtkWidget
*, GtkAllocation
* alloc
, wxWindow
* win
)
1972 int w
= alloc
->width
;
1973 int h
= alloc
->height
;
1974 if (win
->m_wxwindow
)
1976 int border_x
, border_y
;
1977 WX_PIZZA(win
->m_wxwindow
)->get_border_widths(border_x
, border_y
);
1983 if (win
->m_oldClientWidth
!= w
|| win
->m_oldClientHeight
!= h
)
1985 win
->m_oldClientWidth
= w
;
1986 win
->m_oldClientHeight
= h
;
1987 // this callback can be connected to m_wxwindow,
1988 // so always get size from m_widget->allocation
1990 gtk_widget_get_allocation(win
->m_widget
, &a
);
1991 win
->m_width
= a
.width
;
1992 win
->m_height
= a
.height
;
1993 if (!win
->m_nativeSizeEvent
)
1995 wxSizeEvent
event(win
->GetSize(), win
->GetId());
1996 event
.SetEventObject(win
);
1997 win
->GTKProcessEvent(event
);
2002 //-----------------------------------------------------------------------------
2004 //-----------------------------------------------------------------------------
2006 #if GTK_CHECK_VERSION(2, 8, 0)
2008 gtk_window_grab_broken( GtkWidget
*,
2009 GdkEventGrabBroken
*event
,
2012 // Mouse capture has been lost involuntarily, notify the application
2013 if(!event
->keyboard
&& wxWindow::GetCapture() == win
)
2015 wxMouseCaptureLostEvent
evt( win
->GetId() );
2016 evt
.SetEventObject( win
);
2017 win
->HandleWindowEvent( evt
);
2023 //-----------------------------------------------------------------------------
2025 //-----------------------------------------------------------------------------
2028 void gtk_window_style_set_callback( GtkWidget
*WXUNUSED(widget
),
2029 GtkStyle
*previous_style
,
2032 if (win
&& previous_style
)
2034 wxSysColourChangedEvent event
;
2035 event
.SetEventObject(win
);
2037 win
->GTKProcessEvent( event
);
2043 // ----------------------------------------------------------------------------
2044 // this wxWindowBase function is implemented here (in platform-specific file)
2045 // because it is static and so couldn't be made virtual
2046 // ----------------------------------------------------------------------------
2048 wxWindow
*wxWindowBase::DoFindFocus()
2050 wxWindowGTK
*focus
= gs_pendingFocus
? gs_pendingFocus
: gs_currentFocus
;
2051 // the cast is necessary when we compile in wxUniversal mode
2052 return static_cast<wxWindow
*>(focus
);
2055 void wxWindowGTK::AddChildGTK(wxWindowGTK
* child
)
2057 wxASSERT_MSG(m_wxwindow
, "Cannot add a child to a window without a client area");
2059 // the window might have been scrolled already, we
2060 // have to adapt the position
2061 wxPizza
* pizza
= WX_PIZZA(m_wxwindow
);
2062 child
->m_x
+= pizza
->m_scroll_x
;
2063 child
->m_y
+= pizza
->m_scroll_y
;
2065 gtk_widget_set_size_request(
2066 child
->m_widget
, child
->m_width
, child
->m_height
);
2067 pizza
->put(child
->m_widget
, child
->m_x
, child
->m_y
);
2070 //-----------------------------------------------------------------------------
2072 //-----------------------------------------------------------------------------
2074 wxWindow
*wxGetActiveWindow()
2076 return wxWindow::FindFocus();
2080 wxMouseState
wxGetMouseState()
2086 GdkModifierType mask
;
2088 gdk_window_get_pointer(NULL
, &x
, &y
, &mask
);
2092 ms
.SetLeftDown((mask
& GDK_BUTTON1_MASK
) != 0);
2093 ms
.SetMiddleDown((mask
& GDK_BUTTON2_MASK
) != 0);
2094 ms
.SetRightDown((mask
& GDK_BUTTON3_MASK
) != 0);
2095 // see the comment in InitMouseEvent()
2096 ms
.SetAux1Down((mask
& GDK_BUTTON4_MASK
) != 0);
2097 ms
.SetAux2Down((mask
& GDK_BUTTON5_MASK
) != 0);
2099 ms
.SetControlDown((mask
& GDK_CONTROL_MASK
) != 0);
2100 ms
.SetShiftDown((mask
& GDK_SHIFT_MASK
) != 0);
2101 ms
.SetAltDown((mask
& GDK_MOD1_MASK
) != 0);
2102 ms
.SetMetaDown((mask
& GDK_META_MASK
) != 0);
2107 //-----------------------------------------------------------------------------
2109 //-----------------------------------------------------------------------------
2111 // in wxUniv/MSW this class is abstract because it doesn't have DoPopupMenu()
2113 #ifdef __WXUNIVERSAL__
2114 IMPLEMENT_ABSTRACT_CLASS(wxWindowGTK
, wxWindowBase
)
2115 #endif // __WXUNIVERSAL__
2117 void wxWindowGTK::Init()
2122 m_focusWidget
= NULL
;
2132 m_showOnIdle
= false;
2135 m_nativeSizeEvent
= false;
2137 m_isScrolling
= false;
2138 m_mouseButtonDown
= false;
2140 // initialize scrolling stuff
2141 for ( int dir
= 0; dir
< ScrollDir_Max
; dir
++ )
2143 m_scrollBar
[dir
] = NULL
;
2144 m_scrollPos
[dir
] = 0;
2148 m_oldClientHeight
= 0;
2150 m_clipPaintRegion
= false;
2152 m_needsStyleChange
= false;
2154 m_cursor
= *wxSTANDARD_CURSOR
;
2157 m_dirtyTabOrder
= false;
2160 wxWindowGTK::wxWindowGTK()
2165 wxWindowGTK::wxWindowGTK( wxWindow
*parent
,
2170 const wxString
&name
)
2174 Create( parent
, id
, pos
, size
, style
, name
);
2177 bool wxWindowGTK::Create( wxWindow
*parent
,
2182 const wxString
&name
)
2184 // Get default border
2185 wxBorder border
= GetBorder(style
);
2187 style
&= ~wxBORDER_MASK
;
2190 if (!PreCreation( parent
, pos
, size
) ||
2191 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
2193 wxFAIL_MSG( wxT("wxWindowGTK creation failed") );
2197 // We should accept the native look
2199 GtkScrolledWindowClass
*scroll_class
= GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) );
2200 scroll_class
->scrollbar_spacing
= 0;
2204 m_wxwindow
= wxPizza::New(m_windowStyle
);
2205 #ifndef __WXUNIVERSAL__
2206 if (HasFlag(wxPizza::BORDER_STYLES
))
2208 g_signal_connect(m_wxwindow
, "parent_set",
2209 G_CALLBACK(parent_set
), this);
2212 if (!HasFlag(wxHSCROLL
) && !HasFlag(wxVSCROLL
))
2213 m_widget
= m_wxwindow
;
2216 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
2218 GtkScrolledWindow
*scrolledWindow
= GTK_SCROLLED_WINDOW(m_widget
);
2220 // There is a conflict with default bindings at GTK+
2221 // level between scrolled windows and notebooks both of which want to use
2222 // Ctrl-PageUp/Down: scrolled windows for scrolling in the horizontal
2223 // direction and notebooks for changing pages -- we decide that if we don't
2224 // have wxHSCROLL style we can safely sacrifice horizontal scrolling if it
2225 // means we can get working keyboard navigation in notebooks
2226 if ( !HasFlag(wxHSCROLL
) )
2229 bindings
= gtk_binding_set_by_class(G_OBJECT_GET_CLASS(m_widget
));
2232 gtk_binding_entry_remove(bindings
, GDK_Page_Up
, GDK_CONTROL_MASK
);
2233 gtk_binding_entry_remove(bindings
, GDK_Page_Down
, GDK_CONTROL_MASK
);
2237 if (HasFlag(wxALWAYS_SHOW_SB
))
2239 gtk_scrolled_window_set_policy( scrolledWindow
, GTK_POLICY_ALWAYS
, GTK_POLICY_ALWAYS
);
2243 gtk_scrolled_window_set_policy( scrolledWindow
, GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
2246 m_scrollBar
[ScrollDir_Horz
] = GTK_RANGE(gtk_scrolled_window_get_hscrollbar(scrolledWindow
));
2247 m_scrollBar
[ScrollDir_Vert
] = GTK_RANGE(gtk_scrolled_window_get_vscrollbar(scrolledWindow
));
2248 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2249 gtk_range_set_inverted( m_scrollBar
[ScrollDir_Horz
], TRUE
);
2251 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
2253 // connect various scroll-related events
2254 for ( int dir
= 0; dir
< ScrollDir_Max
; dir
++ )
2256 // these handlers block mouse events to any window during scrolling
2257 // such as motion events and prevent GTK and wxWidgets from fighting
2258 // over where the slider should be
2259 g_signal_connect(m_scrollBar
[dir
], "button_press_event",
2260 G_CALLBACK(gtk_scrollbar_button_press_event
), this);
2261 g_signal_connect(m_scrollBar
[dir
], "button_release_event",
2262 G_CALLBACK(gtk_scrollbar_button_release_event
), this);
2264 gulong handler_id
= g_signal_connect(m_scrollBar
[dir
], "event_after",
2265 G_CALLBACK(gtk_scrollbar_event_after
), this);
2266 g_signal_handler_block(m_scrollBar
[dir
], handler_id
);
2268 // these handlers get notified when scrollbar slider moves
2269 g_signal_connect_after(m_scrollBar
[dir
], "value_changed",
2270 G_CALLBACK(gtk_scrollbar_value_changed
), this);
2273 gtk_widget_show( m_wxwindow
);
2275 g_object_ref(m_widget
);
2278 m_parent
->DoAddChild( this );
2280 m_focusWidget
= m_wxwindow
;
2282 SetCanFocus(AcceptsFocus());
2289 wxWindowGTK::~wxWindowGTK()
2293 if (gs_currentFocus
== this)
2294 gs_currentFocus
= NULL
;
2295 if (gs_pendingFocus
== this)
2296 gs_pendingFocus
= NULL
;
2298 if ( gs_deferredFocusOut
== this )
2299 gs_deferredFocusOut
= NULL
;
2303 // destroy children before destroying this window itself
2306 // unhook focus handlers to prevent stray events being
2307 // propagated to this (soon to be) dead object
2308 if (m_focusWidget
!= NULL
)
2310 g_signal_handlers_disconnect_by_func (m_focusWidget
,
2311 (gpointer
) gtk_window_focus_in_callback
,
2313 g_signal_handlers_disconnect_by_func (m_focusWidget
,
2314 (gpointer
) gtk_window_focus_out_callback
,
2321 // delete before the widgets to avoid a crash on solaris
2324 // avoid problem with GTK+ 2.18 where a frozen window causes the whole
2325 // TLW to be frozen, and if the window is then destroyed, nothing ever
2326 // gets painted again
2332 // Note that gtk_widget_destroy() does not destroy the widget, it just
2333 // emits the "destroy" signal. The widget is not actually destroyed
2334 // until its reference count drops to zero.
2335 gtk_widget_destroy(m_widget
);
2336 // Release our reference, should be the last one
2337 g_object_unref(m_widget
);
2343 bool wxWindowGTK::PreCreation( wxWindowGTK
*parent
, const wxPoint
&pos
, const wxSize
&size
)
2345 if ( GTKNeedsParent() )
2347 wxCHECK_MSG( parent
, false, wxT("Must have non-NULL parent") );
2350 // Use either the given size, or the default if -1 is given.
2351 // See wxWindowBase for these functions.
2352 m_width
= WidthDefault(size
.x
) ;
2353 m_height
= HeightDefault(size
.y
);
2361 void wxWindowGTK::PostCreation()
2363 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid window") );
2369 // these get reported to wxWidgets -> wxPaintEvent
2371 g_signal_connect (m_wxwindow
, "expose_event",
2372 G_CALLBACK (gtk_window_expose_callback
), this);
2374 if (GetLayoutDirection() == wxLayout_LeftToRight
)
2375 gtk_widget_set_redraw_on_allocate(m_wxwindow
, HasFlag(wxFULL_REPAINT_ON_RESIZE
));
2378 // Create input method handler
2379 m_imData
= new wxGtkIMData
;
2381 // Cannot handle drawing preedited text yet
2382 gtk_im_context_set_use_preedit( m_imData
->context
, FALSE
);
2384 g_signal_connect (m_imData
->context
, "commit",
2385 G_CALLBACK (gtk_wxwindow_commit_cb
), this);
2390 if (!GTK_IS_WINDOW(m_widget
))
2392 if (m_focusWidget
== NULL
)
2393 m_focusWidget
= m_widget
;
2397 g_signal_connect (m_focusWidget
, "focus_in_event",
2398 G_CALLBACK (gtk_window_focus_in_callback
), this);
2399 g_signal_connect (m_focusWidget
, "focus_out_event",
2400 G_CALLBACK (gtk_window_focus_out_callback
), this);
2404 g_signal_connect_after (m_focusWidget
, "focus_in_event",
2405 G_CALLBACK (gtk_window_focus_in_callback
), this);
2406 g_signal_connect_after (m_focusWidget
, "focus_out_event",
2407 G_CALLBACK (gtk_window_focus_out_callback
), this);
2411 if ( !AcceptsFocusFromKeyboard() )
2415 g_signal_connect(m_widget
, "focus",
2416 G_CALLBACK(wx_window_focus_callback
), this);
2419 // connect to the various key and mouse handlers
2421 GtkWidget
*connect_widget
= GetConnectWidget();
2423 ConnectWidget( connect_widget
);
2425 /* We cannot set colours, fonts and cursors before the widget has
2426 been realized, so we do this directly after realization */
2427 g_signal_connect (connect_widget
, "realize",
2428 G_CALLBACK (gtk_window_realized_callback
), this);
2432 g_signal_connect(m_wxwindow
? m_wxwindow
: m_widget
, "size_allocate",
2433 G_CALLBACK(size_allocate
), this);
2436 #if GTK_CHECK_VERSION(2, 8, 0)
2437 if ( gtk_check_version(2,8,0) == NULL
)
2439 // Make sure we can notify the app when mouse capture is lost
2442 g_signal_connect (m_wxwindow
, "grab_broken_event",
2443 G_CALLBACK (gtk_window_grab_broken
), this);
2446 if ( connect_widget
!= m_wxwindow
)
2448 g_signal_connect (connect_widget
, "grab_broken_event",
2449 G_CALLBACK (gtk_window_grab_broken
), this);
2452 #endif // GTK+ >= 2.8
2454 if ( GTKShouldConnectSizeRequest() )
2456 // This is needed if we want to add our windows into native
2457 // GTK controls, such as the toolbar. With this callback, the
2458 // toolbar gets to know the correct size (the one set by the
2459 // programmer). Sadly, it misbehaves for wxComboBox.
2460 g_signal_connect (m_widget
, "size_request",
2461 G_CALLBACK (wxgtk_window_size_request_callback
),
2465 InheritAttributes();
2469 SetLayoutDirection(wxLayout_Default
);
2471 // unless the window was created initially hidden (i.e. Hide() had been
2472 // called before Create()), we should show it at GTK+ level as well
2474 gtk_widget_show( m_widget
);
2477 gulong
wxWindowGTK::GTKConnectWidget(const char *signal
, void (*callback
)())
2479 return g_signal_connect(m_widget
, signal
, callback
, this);
2482 void wxWindowGTK::ConnectWidget( GtkWidget
*widget
)
2484 g_signal_connect (widget
, "key_press_event",
2485 G_CALLBACK (gtk_window_key_press_callback
), this);
2486 g_signal_connect (widget
, "key_release_event",
2487 G_CALLBACK (gtk_window_key_release_callback
), this);
2488 g_signal_connect (widget
, "button_press_event",
2489 G_CALLBACK (gtk_window_button_press_callback
), this);
2490 g_signal_connect (widget
, "button_release_event",
2491 G_CALLBACK (gtk_window_button_release_callback
), this);
2492 g_signal_connect (widget
, "motion_notify_event",
2493 G_CALLBACK (gtk_window_motion_notify_callback
), this);
2495 g_signal_connect (widget
, "scroll_event",
2496 G_CALLBACK (window_scroll_event
), this);
2497 if (m_scrollBar
[ScrollDir_Horz
])
2498 g_signal_connect (m_scrollBar
[ScrollDir_Horz
], "scroll_event",
2499 G_CALLBACK (window_scroll_event_hscrollbar
), this);
2500 if (m_scrollBar
[ScrollDir_Vert
])
2501 g_signal_connect (m_scrollBar
[ScrollDir_Vert
], "scroll_event",
2502 G_CALLBACK (window_scroll_event
), this);
2504 g_signal_connect (widget
, "popup_menu",
2505 G_CALLBACK (wxgtk_window_popup_menu_callback
), this);
2506 g_signal_connect (widget
, "enter_notify_event",
2507 G_CALLBACK (gtk_window_enter_callback
), this);
2508 g_signal_connect (widget
, "leave_notify_event",
2509 G_CALLBACK (gtk_window_leave_callback
), this);
2511 if (IsTopLevel() && m_wxwindow
)
2512 g_signal_connect (m_wxwindow
, "style_set",
2513 G_CALLBACK (gtk_window_style_set_callback
), this);
2516 bool wxWindowGTK::Destroy()
2520 return wxWindowBase::Destroy();
2523 void wxWindowGTK::DoMoveWindow(int x
, int y
, int width
, int height
)
2525 gtk_widget_set_size_request(m_widget
, width
, height
);
2527 // inform the parent to perform the move
2528 wxASSERT_MSG(m_parent
&& m_parent
->m_wxwindow
,
2529 "the parent window has no client area?");
2530 WX_PIZZA(m_parent
->m_wxwindow
)->move(m_widget
, x
, y
);
2533 void wxWindowGTK::ConstrainSize()
2536 // GPE's window manager doesn't like size hints at all, esp. when the user
2537 // has to use the virtual keyboard, so don't constrain size there
2541 const wxSize minSize
= GetMinSize();
2542 const wxSize maxSize
= GetMaxSize();
2543 if (minSize
.x
> 0 && m_width
< minSize
.x
) m_width
= minSize
.x
;
2544 if (minSize
.y
> 0 && m_height
< minSize
.y
) m_height
= minSize
.y
;
2545 if (maxSize
.x
> 0 && m_width
> maxSize
.x
) m_width
= maxSize
.x
;
2546 if (maxSize
.y
> 0 && m_height
> maxSize
.y
) m_height
= maxSize
.y
;
2550 void wxWindowGTK::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
2552 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid window") );
2553 wxASSERT_MSG( (m_parent
!= NULL
), wxT("wxWindowGTK::SetSize requires parent.\n") );
2555 int currentX
, currentY
;
2556 GetPosition(¤tX
, ¤tY
);
2557 if (x
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
2559 if (y
== -1 && !(sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
2561 AdjustForParentClientOrigin(x
, y
, sizeFlags
);
2563 // calculate the best size if we should auto size the window
2564 if ( ((sizeFlags
& wxSIZE_AUTO_WIDTH
) && width
== -1) ||
2565 ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) && height
== -1) )
2567 const wxSize sizeBest
= GetBestSize();
2568 if ( (sizeFlags
& wxSIZE_AUTO_WIDTH
) && width
== -1 )
2570 if ( (sizeFlags
& wxSIZE_AUTO_HEIGHT
) && height
== -1 )
2571 height
= sizeBest
.y
;
2574 const wxSize
oldSize(m_width
, m_height
);
2580 if (m_parent
->m_wxwindow
)
2582 wxPizza
* pizza
= WX_PIZZA(m_parent
->m_wxwindow
);
2583 m_x
= x
+ pizza
->m_scroll_x
;
2584 m_y
= y
+ pizza
->m_scroll_y
;
2586 int left_border
= 0;
2587 int right_border
= 0;
2589 int bottom_border
= 0;
2591 /* the default button has a border around it */
2592 if (gtk_widget_get_can_default(m_widget
))
2594 GtkBorder
*default_border
= NULL
;
2595 gtk_widget_style_get( m_widget
, "default_border", &default_border
, NULL
);
2598 left_border
+= default_border
->left
;
2599 right_border
+= default_border
->right
;
2600 top_border
+= default_border
->top
;
2601 bottom_border
+= default_border
->bottom
;
2602 gtk_border_free( default_border
);
2606 DoMoveWindow( m_x
- left_border
,
2608 m_width
+left_border
+right_border
,
2609 m_height
+top_border
+bottom_border
);
2612 if (m_width
!= oldSize
.x
|| m_height
!= oldSize
.y
)
2614 // update these variables to keep size_allocate handler
2615 // from sending another size event for this change
2616 GetClientSize( &m_oldClientWidth
, &m_oldClientHeight
);
2618 gtk_widget_queue_resize(m_widget
);
2619 if (!m_nativeSizeEvent
)
2621 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
2622 event
.SetEventObject( this );
2623 HandleWindowEvent( event
);
2626 if (sizeFlags
& wxSIZE_FORCE_EVENT
)
2628 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
2629 event
.SetEventObject( this );
2630 HandleWindowEvent( event
);
2634 bool wxWindowGTK::GTKShowFromOnIdle()
2636 if (IsShown() && m_showOnIdle
&& !gtk_widget_get_visible (m_widget
))
2638 GtkAllocation alloc
;
2641 alloc
.width
= m_width
;
2642 alloc
.height
= m_height
;
2643 gtk_widget_size_allocate( m_widget
, &alloc
);
2644 gtk_widget_show( m_widget
);
2645 wxShowEvent
eventShow(GetId(), true);
2646 eventShow
.SetEventObject(this);
2647 HandleWindowEvent(eventShow
);
2648 m_showOnIdle
= false;
2655 void wxWindowGTK::OnInternalIdle()
2657 if ( gs_deferredFocusOut
)
2658 GTKHandleDeferredFocusOut();
2660 // Check if we have to show window now
2661 if (GTKShowFromOnIdle()) return;
2663 if ( m_dirtyTabOrder
)
2665 m_dirtyTabOrder
= false;
2669 // Update style if the window was not yet realized when
2670 // SetBackgroundStyle() was called
2671 if (m_needsStyleChange
)
2673 SetBackgroundStyle(GetBackgroundStyle());
2674 m_needsStyleChange
= false;
2677 wxWindowBase::OnInternalIdle();
2680 void wxWindowGTK::DoGetSize( int *width
, int *height
) const
2682 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2684 if (width
) (*width
) = m_width
;
2685 if (height
) (*height
) = m_height
;
2688 void wxWindowGTK::DoSetClientSize( int width
, int height
)
2690 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2692 const wxSize size
= GetSize();
2693 const wxSize clientSize
= GetClientSize();
2694 SetSize(width
+ (size
.x
- clientSize
.x
), height
+ (size
.y
- clientSize
.y
));
2697 void wxWindowGTK::DoGetClientSize( int *width
, int *height
) const
2699 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2706 // if window is scrollable, account for scrollbars
2707 if ( GTK_IS_SCROLLED_WINDOW(m_widget
) )
2709 GtkPolicyType policy
[ScrollDir_Max
];
2710 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(m_widget
),
2711 &policy
[ScrollDir_Horz
],
2712 &policy
[ScrollDir_Vert
]);
2714 for ( int i
= 0; i
< ScrollDir_Max
; i
++ )
2716 // don't account for the scrollbars we don't have
2717 GtkRange
* const range
= m_scrollBar
[i
];
2721 // nor for the ones we have but don't current show
2722 switch ( policy
[i
] )
2724 case GTK_POLICY_NEVER
:
2725 // never shown so doesn't take any place
2728 case GTK_POLICY_ALWAYS
:
2729 // no checks necessary
2732 case GTK_POLICY_AUTOMATIC
:
2733 // may be shown or not, check
2734 GtkAdjustment
*adj
= gtk_range_get_adjustment(range
);
2735 if (gtk_adjustment_get_upper(adj
) <= gtk_adjustment_get_page_size(adj
))
2739 GtkScrolledWindowClass
*scroll_class
=
2740 GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) );
2743 gtk_widget_size_request(GTK_WIDGET(range
), &req
);
2744 if (i
== ScrollDir_Horz
)
2745 h
-= req
.height
+ scroll_class
->scrollbar_spacing
;
2747 w
-= req
.width
+ scroll_class
->scrollbar_spacing
;
2751 const wxSize sizeBorders
= DoGetBorderSize();
2761 if (width
) *width
= w
;
2762 if (height
) *height
= h
;
2765 wxSize
wxWindowGTK::DoGetBorderSize() const
2768 return wxWindowBase::DoGetBorderSize();
2771 WX_PIZZA(m_wxwindow
)->get_border_widths(x
, y
);
2773 return 2*wxSize(x
, y
);
2776 void wxWindowGTK::DoGetPosition( int *x
, int *y
) const
2778 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2782 if (!IsTopLevel() && m_parent
&& m_parent
->m_wxwindow
)
2784 wxPizza
* pizza
= WX_PIZZA(m_parent
->m_wxwindow
);
2785 dx
= pizza
->m_scroll_x
;
2786 dy
= pizza
->m_scroll_y
;
2789 if (m_x
== -1 && m_y
== -1)
2791 GdkWindow
*source
= NULL
;
2793 source
= gtk_widget_get_window(m_wxwindow
);
2795 source
= gtk_widget_get_window(m_widget
);
2801 gdk_window_get_origin( source
, &org_x
, &org_y
);
2804 m_parent
->ScreenToClient(&org_x
, &org_y
);
2806 const_cast<wxWindowGTK
*>(this)->m_x
= org_x
;
2807 const_cast<wxWindowGTK
*>(this)->m_y
= org_y
;
2811 if (x
) (*x
) = m_x
- dx
;
2812 if (y
) (*y
) = m_y
- dy
;
2815 void wxWindowGTK::DoClientToScreen( int *x
, int *y
) const
2817 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2819 if (gtk_widget_get_window(m_widget
) == NULL
) return;
2821 GdkWindow
*source
= NULL
;
2823 source
= gtk_widget_get_window(m_wxwindow
);
2825 source
= gtk_widget_get_window(m_widget
);
2829 gdk_window_get_origin( source
, &org_x
, &org_y
);
2833 if (!gtk_widget_get_has_window(m_widget
))
2836 gtk_widget_get_allocation(m_widget
, &a
);
2845 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2846 *x
= (GetClientSize().x
- *x
) + org_x
;
2854 void wxWindowGTK::DoScreenToClient( int *x
, int *y
) const
2856 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2858 if (!gtk_widget_get_realized(m_widget
)) return;
2860 GdkWindow
*source
= NULL
;
2862 source
= gtk_widget_get_window(m_wxwindow
);
2864 source
= gtk_widget_get_window(m_widget
);
2868 gdk_window_get_origin( source
, &org_x
, &org_y
);
2872 if (!gtk_widget_get_has_window(m_widget
))
2875 gtk_widget_get_allocation(m_widget
, &a
);
2883 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2884 *x
= (GetClientSize().x
- *x
) - org_x
;
2891 bool wxWindowGTK::Show( bool show
)
2893 if ( !wxWindowBase::Show(show
) )
2899 // notice that we may call Hide() before the window is created and this is
2900 // actually useful to create it hidden initially -- but we can't call
2901 // Show() before it is created
2904 wxASSERT_MSG( !show
, "can't show invalid window" );
2912 // defer until later
2916 gtk_widget_show(m_widget
);
2920 gtk_widget_hide(m_widget
);
2923 wxShowEvent
eventShow(GetId(), show
);
2924 eventShow
.SetEventObject(this);
2925 HandleWindowEvent(eventShow
);
2930 void wxWindowGTK::DoEnable( bool enable
)
2932 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2934 gtk_widget_set_sensitive( m_widget
, enable
);
2935 if (m_wxwindow
&& (m_wxwindow
!= m_widget
))
2936 gtk_widget_set_sensitive( m_wxwindow
, enable
);
2939 int wxWindowGTK::GetCharHeight() const
2941 wxCHECK_MSG( (m_widget
!= NULL
), 12, wxT("invalid window") );
2943 wxFont font
= GetFont();
2944 wxCHECK_MSG( font
.IsOk(), 12, wxT("invalid font") );
2946 PangoContext
* context
= gtk_widget_get_pango_context(m_widget
);
2951 PangoFontDescription
*desc
= font
.GetNativeFontInfo()->description
;
2952 PangoLayout
*layout
= pango_layout_new(context
);
2953 pango_layout_set_font_description(layout
, desc
);
2954 pango_layout_set_text(layout
, "H", 1);
2955 PangoLayoutLine
*line
= (PangoLayoutLine
*)pango_layout_get_lines(layout
)->data
;
2957 PangoRectangle rect
;
2958 pango_layout_line_get_extents(line
, NULL
, &rect
);
2960 g_object_unref (layout
);
2962 return (int) PANGO_PIXELS(rect
.height
);
2965 int wxWindowGTK::GetCharWidth() const
2967 wxCHECK_MSG( (m_widget
!= NULL
), 8, wxT("invalid window") );
2969 wxFont font
= GetFont();
2970 wxCHECK_MSG( font
.IsOk(), 8, wxT("invalid font") );
2972 PangoContext
* context
= gtk_widget_get_pango_context(m_widget
);
2977 PangoFontDescription
*desc
= font
.GetNativeFontInfo()->description
;
2978 PangoLayout
*layout
= pango_layout_new(context
);
2979 pango_layout_set_font_description(layout
, desc
);
2980 pango_layout_set_text(layout
, "g", 1);
2981 PangoLayoutLine
*line
= (PangoLayoutLine
*)pango_layout_get_lines(layout
)->data
;
2983 PangoRectangle rect
;
2984 pango_layout_line_get_extents(line
, NULL
, &rect
);
2986 g_object_unref (layout
);
2988 return (int) PANGO_PIXELS(rect
.width
);
2991 void wxWindowGTK::DoGetTextExtent( const wxString
& string
,
2995 int *externalLeading
,
2996 const wxFont
*theFont
) const
2998 wxFont fontToUse
= theFont
? *theFont
: GetFont();
3000 wxCHECK_RET( fontToUse
.IsOk(), wxT("invalid font") );
3009 PangoContext
*context
= NULL
;
3011 context
= gtk_widget_get_pango_context( m_widget
);
3020 PangoFontDescription
*desc
= fontToUse
.GetNativeFontInfo()->description
;
3021 PangoLayout
*layout
= pango_layout_new(context
);
3022 pango_layout_set_font_description(layout
, desc
);
3024 const wxCharBuffer data
= wxGTK_CONV( string
);
3026 pango_layout_set_text(layout
, data
, strlen(data
));
3029 PangoRectangle rect
;
3030 pango_layout_get_extents(layout
, NULL
, &rect
);
3032 if (x
) (*x
) = (wxCoord
) PANGO_PIXELS(rect
.width
);
3033 if (y
) (*y
) = (wxCoord
) PANGO_PIXELS(rect
.height
);
3036 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
3037 int baseline
= pango_layout_iter_get_baseline(iter
);
3038 pango_layout_iter_free(iter
);
3039 *descent
= *y
- PANGO_PIXELS(baseline
);
3041 if (externalLeading
) (*externalLeading
) = 0; // ??
3043 g_object_unref (layout
);
3046 void wxWindowGTK::GTKDisableFocusOutEvent()
3048 g_signal_handlers_block_by_func( m_focusWidget
,
3049 (gpointer
) gtk_window_focus_out_callback
, this);
3052 void wxWindowGTK::GTKEnableFocusOutEvent()
3054 g_signal_handlers_unblock_by_func( m_focusWidget
,
3055 (gpointer
) gtk_window_focus_out_callback
, this);
3058 bool wxWindowGTK::GTKHandleFocusIn()
3060 // Disable default focus handling for custom windows since the default GTK+
3061 // handler issues a repaint
3062 const bool retval
= m_wxwindow
? true : false;
3065 // NB: if there's still unprocessed deferred focus-out event (see
3066 // GTKHandleFocusOut() for explanation), we need to process it first so
3067 // that the order of focus events -- focus-out first, then focus-in
3068 // elsewhere -- is preserved
3069 if ( gs_deferredFocusOut
)
3071 if ( GTKNeedsToFilterSameWindowFocus() &&
3072 gs_deferredFocusOut
== this )
3074 // GTK+ focus changed from this wxWindow back to itself, so don't
3075 // emit any events at all
3076 wxLogTrace(TRACE_FOCUS
,
3077 "filtered out spurious focus change within %s(%p, %s)",
3078 GetClassInfo()->GetClassName(), this, GetLabel());
3079 gs_deferredFocusOut
= NULL
;
3083 // otherwise we need to send focus-out first
3084 wxASSERT_MSG ( gs_deferredFocusOut
!= this,
3085 "GTKHandleFocusIn(GTKFocus_Normal) called even though focus changed back to itself - derived class should handle this" );
3086 GTKHandleDeferredFocusOut();
3090 wxLogTrace(TRACE_FOCUS
,
3091 "handling focus_in event for %s(%p, %s)",
3092 GetClassInfo()->GetClassName(), this, GetLabel());
3095 gtk_im_context_focus_in(m_imData
->context
);
3097 gs_currentFocus
= this;
3098 gs_pendingFocus
= NULL
;
3101 // caret needs to be informed about focus change
3102 wxCaret
*caret
= GetCaret();
3105 caret
->OnSetFocus();
3107 #endif // wxUSE_CARET
3109 // Notify the parent keeping track of focus for the kbd navigation
3110 // purposes that we got it.
3111 wxChildFocusEvent
eventChildFocus(static_cast<wxWindow
*>(this));
3112 GTKProcessEvent(eventChildFocus
);
3114 wxFocusEvent
eventFocus(wxEVT_SET_FOCUS
, GetId());
3115 eventFocus
.SetEventObject(this);
3116 GTKProcessEvent(eventFocus
);
3121 bool wxWindowGTK::GTKHandleFocusOut()
3123 // Disable default focus handling for custom windows since the default GTK+
3124 // handler issues a repaint
3125 const bool retval
= m_wxwindow
? true : false;
3128 // NB: If a control is composed of several GtkWidgets and when focus
3129 // changes from one of them to another within the same wxWindow, we get
3130 // a focus-out event followed by focus-in for another GtkWidget owned
3131 // by the same wx control. We don't want to generate two spurious
3132 // wxEVT_SET_FOCUS events in this case, so we defer sending wx events
3133 // from GTKHandleFocusOut() until we know for sure it's not coming back
3134 // (i.e. in GTKHandleFocusIn() or at idle time).
3135 if ( GTKNeedsToFilterSameWindowFocus() )
3137 wxASSERT_MSG( gs_deferredFocusOut
== NULL
,
3138 "deferred focus out event already pending" );
3139 wxLogTrace(TRACE_FOCUS
,
3140 "deferring focus_out event for %s(%p, %s)",
3141 GetClassInfo()->GetClassName(), this, GetLabel());
3142 gs_deferredFocusOut
= this;
3146 GTKHandleFocusOutNoDeferring();
3151 void wxWindowGTK::GTKHandleFocusOutNoDeferring()
3153 wxLogTrace(TRACE_FOCUS
,
3154 "handling focus_out event for %s(%p, %s)",
3155 GetClassInfo()->GetClassName(), this, GetLabel());
3158 gtk_im_context_focus_out(m_imData
->context
);
3160 if ( gs_currentFocus
!= this )
3162 // Something is terribly wrong, gs_currentFocus is out of sync with the
3163 // real focus. We will reset it to NULL anyway, because after this
3164 // focus-out event is handled, one of the following with happen:
3166 // * either focus will go out of the app altogether, in which case
3167 // gs_currentFocus _should_ be NULL
3169 // * or it goes to another control, in which case focus-in event will
3170 // follow immediately and it will set gs_currentFocus to the right
3172 wxLogDebug("window %s(%p, %s) lost focus even though it didn't have it",
3173 GetClassInfo()->GetClassName(), this, GetLabel());
3175 gs_currentFocus
= NULL
;
3178 // caret needs to be informed about focus change
3179 wxCaret
*caret
= GetCaret();
3182 caret
->OnKillFocus();
3184 #endif // wxUSE_CARET
3186 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetId() );
3187 event
.SetEventObject( this );
3188 GTKProcessEvent( event
);
3192 void wxWindowGTK::GTKHandleDeferredFocusOut()
3194 // NB: See GTKHandleFocusOut() for explanation. This function is called
3195 // from either GTKHandleFocusIn() or OnInternalIdle() to process
3197 if ( gs_deferredFocusOut
)
3199 wxWindowGTK
*win
= gs_deferredFocusOut
;
3200 gs_deferredFocusOut
= NULL
;
3202 wxLogTrace(TRACE_FOCUS
,
3203 "processing deferred focus_out event for %s(%p, %s)",
3204 win
->GetClassInfo()->GetClassName(), win
, win
->GetLabel());
3206 win
->GTKHandleFocusOutNoDeferring();
3210 void wxWindowGTK::SetFocus()
3212 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
3214 // Setting "physical" focus is not immediate in GTK+ and while
3215 // gtk_widget_is_focus ("determines if the widget is the focus widget
3216 // within its toplevel", i.e. returns true for one widget per TLW, not
3217 // globally) returns true immediately after grabbing focus,
3218 // GTK_WIDGET_HAS_FOCUS (which returns true only for the one widget that
3219 // has focus at the moment) takes effect only after the window is shown
3220 // (if it was hidden at the moment of the call) or at the next event loop
3223 // Because we want to FindFocus() call immediately following
3224 // foo->SetFocus() to return foo, we have to keep track of "pending" focus
3226 gs_pendingFocus
= this;
3228 GtkWidget
*widget
= m_wxwindow
? m_wxwindow
: m_focusWidget
;
3230 if ( GTK_IS_CONTAINER(widget
) &&
3231 !gtk_widget_get_can_focus(widget
) )
3233 wxLogTrace(TRACE_FOCUS
,
3234 wxT("Setting focus to a child of %s(%p, %s)"),
3235 GetClassInfo()->GetClassName(), this, GetLabel().c_str());
3236 gtk_widget_child_focus(widget
, GTK_DIR_TAB_FORWARD
);
3240 wxLogTrace(TRACE_FOCUS
,
3241 wxT("Setting focus to %s(%p, %s)"),
3242 GetClassInfo()->GetClassName(), this, GetLabel().c_str());
3243 gtk_widget_grab_focus(widget
);
3247 void wxWindowGTK::SetCanFocus(bool canFocus
)
3249 gtk_widget_set_can_focus(m_widget
, canFocus
);
3251 if ( m_wxwindow
&& (m_widget
!= m_wxwindow
) )
3253 gtk_widget_set_can_focus(m_wxwindow
, canFocus
);
3257 bool wxWindowGTK::Reparent( wxWindowBase
*newParentBase
)
3259 wxCHECK_MSG( (m_widget
!= NULL
), false, wxT("invalid window") );
3261 wxWindowGTK
* const newParent
= (wxWindowGTK
*)newParentBase
;
3263 wxASSERT( GTK_IS_WIDGET(m_widget
) );
3265 if ( !wxWindowBase::Reparent(newParent
) )
3268 wxASSERT( GTK_IS_WIDGET(m_widget
) );
3270 // Notice that old m_parent pointer might be non-NULL here but the widget
3271 // still not have any parent at GTK level if it's a notebook page that had
3272 // been removed from the notebook so test this at GTK level and not wx one.
3273 if ( GtkWidget
*parentGTK
= gtk_widget_get_parent(m_widget
) )
3274 gtk_container_remove(GTK_CONTAINER(parentGTK
), m_widget
);
3276 wxASSERT( GTK_IS_WIDGET(m_widget
) );
3280 if (gtk_widget_get_visible (newParent
->m_widget
))
3282 m_showOnIdle
= true;
3283 gtk_widget_hide( m_widget
);
3285 /* insert GTK representation */
3286 newParent
->AddChildGTK(this);
3289 SetLayoutDirection(wxLayout_Default
);
3294 void wxWindowGTK::DoAddChild(wxWindowGTK
*child
)
3296 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid window") );
3297 wxASSERT_MSG( (child
!= NULL
), wxT("invalid child window") );
3302 /* insert GTK representation */
3306 void wxWindowGTK::AddChild(wxWindowBase
*child
)
3308 wxWindowBase::AddChild(child
);
3309 m_dirtyTabOrder
= true;
3310 wxTheApp
->WakeUpIdle();
3313 void wxWindowGTK::RemoveChild(wxWindowBase
*child
)
3315 wxWindowBase::RemoveChild(child
);
3316 m_dirtyTabOrder
= true;
3317 wxTheApp
->WakeUpIdle();
3321 wxLayoutDirection
wxWindowGTK::GTKGetLayout(GtkWidget
*widget
)
3323 return gtk_widget_get_direction(widget
) == GTK_TEXT_DIR_RTL
3324 ? wxLayout_RightToLeft
3325 : wxLayout_LeftToRight
;
3329 void wxWindowGTK::GTKSetLayout(GtkWidget
*widget
, wxLayoutDirection dir
)
3331 wxASSERT_MSG( dir
!= wxLayout_Default
, wxT("invalid layout direction") );
3333 gtk_widget_set_direction(widget
,
3334 dir
== wxLayout_RightToLeft
? GTK_TEXT_DIR_RTL
3335 : GTK_TEXT_DIR_LTR
);
3338 wxLayoutDirection
wxWindowGTK::GetLayoutDirection() const
3340 return GTKGetLayout(m_widget
);
3343 void wxWindowGTK::SetLayoutDirection(wxLayoutDirection dir
)
3345 if ( dir
== wxLayout_Default
)
3347 const wxWindow
*const parent
= GetParent();
3350 // inherit layout from parent.
3351 dir
= parent
->GetLayoutDirection();
3353 else // no parent, use global default layout
3355 dir
= wxTheApp
->GetLayoutDirection();
3359 if ( dir
== wxLayout_Default
)
3362 GTKSetLayout(m_widget
, dir
);
3364 if (m_wxwindow
&& (m_wxwindow
!= m_widget
))
3365 GTKSetLayout(m_wxwindow
, dir
);
3369 wxWindowGTK::AdjustForLayoutDirection(wxCoord x
,
3370 wxCoord
WXUNUSED(width
),
3371 wxCoord
WXUNUSED(widthTotal
)) const
3373 // We now mirror the coordinates of RTL windows in wxPizza
3377 void wxWindowGTK::DoMoveInTabOrder(wxWindow
*win
, WindowOrder move
)
3379 wxWindowBase::DoMoveInTabOrder(win
, move
);
3380 m_dirtyTabOrder
= true;
3381 wxTheApp
->WakeUpIdle();
3384 bool wxWindowGTK::DoNavigateIn(int flags
)
3386 if ( flags
& wxNavigationKeyEvent::WinChange
)
3388 wxFAIL_MSG( wxT("not implemented") );
3392 else // navigate inside the container
3394 wxWindow
*parent
= wxGetTopLevelParent((wxWindow
*)this);
3395 wxCHECK_MSG( parent
, false, wxT("every window must have a TLW parent") );
3397 GtkDirectionType dir
;
3398 dir
= flags
& wxNavigationKeyEvent::IsForward
? GTK_DIR_TAB_FORWARD
3399 : GTK_DIR_TAB_BACKWARD
;
3402 g_signal_emit_by_name(parent
->m_widget
, "focus", dir
, &rc
);
3408 bool wxWindowGTK::GTKWidgetNeedsMnemonic() const
3410 // none needed by default
3414 void wxWindowGTK::GTKWidgetDoSetMnemonic(GtkWidget
* WXUNUSED(w
))
3416 // nothing to do by default since none is needed
3419 void wxWindowGTK::RealizeTabOrder()
3423 if ( !m_children
.empty() )
3425 // we don't only construct the correct focus chain but also use
3426 // this opportunity to update the mnemonic widgets for the widgets
3429 GList
*chain
= NULL
;
3430 wxWindowGTK
* mnemonicWindow
= NULL
;
3432 for ( wxWindowList::const_iterator i
= m_children
.begin();
3433 i
!= m_children
.end();
3436 wxWindowGTK
*win
= *i
;
3438 bool focusableFromKeyboard
= win
->AcceptsFocusFromKeyboard();
3440 if ( mnemonicWindow
)
3442 if ( focusableFromKeyboard
)
3444 // wxComboBox et al. needs to focus on on a different
3445 // widget than m_widget, so if the main widget isn't
3446 // focusable try the connect widget
3447 GtkWidget
* w
= win
->m_widget
;
3448 if ( !gtk_widget_get_can_focus(w
) )
3450 w
= win
->GetConnectWidget();
3451 if ( !gtk_widget_get_can_focus(w
) )
3457 mnemonicWindow
->GTKWidgetDoSetMnemonic(w
);
3458 mnemonicWindow
= NULL
;
3462 else if ( win
->GTKWidgetNeedsMnemonic() )
3464 mnemonicWindow
= win
;
3467 if ( focusableFromKeyboard
)
3468 chain
= g_list_prepend(chain
, win
->m_widget
);
3471 chain
= g_list_reverse(chain
);
3473 gtk_container_set_focus_chain(GTK_CONTAINER(m_wxwindow
), chain
);
3478 gtk_container_unset_focus_chain(GTK_CONTAINER(m_wxwindow
));
3483 void wxWindowGTK::Raise()
3485 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
3487 if (m_wxwindow
&& gtk_widget_get_window(m_wxwindow
))
3489 gdk_window_raise(gtk_widget_get_window(m_wxwindow
));
3491 else if (gtk_widget_get_window(m_widget
))
3493 gdk_window_raise(gtk_widget_get_window(m_widget
));
3497 void wxWindowGTK::Lower()
3499 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
3501 if (m_wxwindow
&& gtk_widget_get_window(m_wxwindow
))
3503 gdk_window_lower(gtk_widget_get_window(m_wxwindow
));
3505 else if (gtk_widget_get_window(m_widget
))
3507 gdk_window_lower(gtk_widget_get_window(m_widget
));
3511 bool wxWindowGTK::SetCursor( const wxCursor
&cursor
)
3513 if ( !wxWindowBase::SetCursor(cursor
.IsOk() ? cursor
: *wxSTANDARD_CURSOR
) )
3521 void wxWindowGTK::GTKUpdateCursor(bool update_self
/*=true*/, bool recurse
/*=true*/)
3525 wxCursor
cursor(g_globalCursor
.IsOk() ? g_globalCursor
: GetCursor());
3526 if ( cursor
.IsOk() )
3528 wxArrayGdkWindows windowsThis
;
3529 GdkWindow
* window
= GTKGetWindow(windowsThis
);
3531 gdk_window_set_cursor( window
, cursor
.GetCursor() );
3534 const size_t count
= windowsThis
.size();
3535 for ( size_t n
= 0; n
< count
; n
++ )
3537 GdkWindow
*win
= windowsThis
[n
];
3538 // It can be zero if the window has not been realized yet.
3541 gdk_window_set_cursor(win
, cursor
.GetCursor());
3550 for (wxWindowList::iterator it
= GetChildren().begin(); it
!= GetChildren().end(); ++it
)
3552 (*it
)->GTKUpdateCursor( true );
3557 void wxWindowGTK::WarpPointer( int x
, int y
)
3559 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
3561 // We provide this function ourselves as it is
3562 // missing in GDK (top of this file).
3564 GdkWindow
*window
= NULL
;
3566 window
= gtk_widget_get_window(m_wxwindow
);
3568 window
= gtk_widget_get_window(GetConnectWidget());
3571 gdk_window_warp_pointer( window
, x
, y
);
3574 wxWindowGTK::ScrollDir
wxWindowGTK::ScrollDirFromRange(GtkRange
*range
) const
3576 // find the scrollbar which generated the event
3577 for ( int dir
= 0; dir
< ScrollDir_Max
; dir
++ )
3579 if ( range
== m_scrollBar
[dir
] )
3580 return (ScrollDir
)dir
;
3583 wxFAIL_MSG( wxT("event from unknown scrollbar received") );
3585 return ScrollDir_Max
;
3588 bool wxWindowGTK::DoScrollByUnits(ScrollDir dir
, ScrollUnit unit
, int units
)
3590 bool changed
= false;
3591 GtkRange
* range
= m_scrollBar
[dir
];
3592 if ( range
&& units
)
3594 GtkAdjustment
* adj
= gtk_range_get_adjustment(range
);
3595 double inc
= unit
== ScrollUnit_Line
? gtk_adjustment_get_step_increment(adj
)
3596 : gtk_adjustment_get_page_increment(adj
);
3598 const int posOld
= wxRound(gtk_adjustment_get_value(adj
));
3599 gtk_range_set_value(range
, posOld
+ units
*inc
);
3601 changed
= wxRound(gtk_adjustment_get_value(adj
)) != posOld
;
3607 bool wxWindowGTK::ScrollLines(int lines
)
3609 return DoScrollByUnits(ScrollDir_Vert
, ScrollUnit_Line
, lines
);
3612 bool wxWindowGTK::ScrollPages(int pages
)
3614 return DoScrollByUnits(ScrollDir_Vert
, ScrollUnit_Page
, pages
);
3617 void wxWindowGTK::Refresh(bool WXUNUSED(eraseBackground
),
3622 // it is valid to call Refresh() for a window which hasn't been created
3623 // yet, it simply doesn't do anything in this case
3630 gtk_widget_queue_draw_area( m_widget
, rect
->x
, rect
->y
, rect
->width
, rect
->height
);
3632 gtk_widget_queue_draw( m_widget
);
3636 // Just return if the widget or one of its ancestors isn't mapped
3638 for (w
= m_wxwindow
; w
!= NULL
; w
= gtk_widget_get_parent(w
))
3639 if (!gtk_widget_get_mapped (w
))
3642 GdkWindow
* window
= GTKGetDrawingWindow();
3646 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3647 x
= GetClientSize().x
- x
- rect
->width
;
3651 r
.width
= rect
->width
;
3652 r
.height
= rect
->height
;
3653 gdk_window_invalidate_rect(window
, &r
, true);
3656 gdk_window_invalidate_rect(window
, NULL
, true);
3660 void wxWindowGTK::Update()
3662 if (m_widget
&& gtk_widget_get_mapped(m_widget
))
3664 GdkDisplay
* display
= gtk_widget_get_display(m_widget
);
3665 // Flush everything out to the server, and wait for it to finish.
3666 // This ensures nothing will overwrite the drawing we are about to do.
3667 gdk_display_sync(display
);
3669 GdkWindow
* window
= GTKGetDrawingWindow();
3671 window
= gtk_widget_get_window(m_widget
);
3672 gdk_window_process_updates(window
, true);
3674 // Flush again, but no need to wait for it to finish
3675 gdk_display_flush(display
);
3679 bool wxWindowGTK::DoIsExposed( int x
, int y
) const
3681 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
3684 bool wxWindowGTK::DoIsExposed( int x
, int y
, int w
, int h
) const
3686 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3687 return m_updateRegion
.Contains(x
-w
, y
, w
, h
) != wxOutRegion
;
3689 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
3692 void wxWindowGTK::GtkSendPaintEvents()
3696 m_updateRegion
.Clear();
3700 // Clip to paint region in wxClientDC
3701 m_clipPaintRegion
= true;
3703 m_nativeUpdateRegion
= m_updateRegion
;
3705 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3707 // Transform m_updateRegion under RTL
3708 m_updateRegion
.Clear();
3711 gdk_drawable_get_size(gtk_widget_get_window(m_wxwindow
), &width
, NULL
);
3713 wxRegionIterator
upd( m_nativeUpdateRegion
);
3717 rect
.x
= upd
.GetX();
3718 rect
.y
= upd
.GetY();
3719 rect
.width
= upd
.GetWidth();
3720 rect
.height
= upd
.GetHeight();
3722 rect
.x
= width
- rect
.x
- rect
.width
;
3723 m_updateRegion
.Union( rect
);
3729 switch ( GetBackgroundStyle() )
3731 case wxBG_STYLE_ERASE
:
3733 wxWindowDC
dc( (wxWindow
*)this );
3734 dc
.SetDeviceClippingRegion( m_updateRegion
);
3736 // Work around gtk-qt <= 0.60 bug whereby the window colour
3740 GetOptionInt("gtk.window.force-background-colour") )
3742 dc
.SetBackground(GetBackgroundColour());
3746 wxEraseEvent
erase_event( GetId(), &dc
);
3747 erase_event
.SetEventObject( this );
3749 if ( HandleWindowEvent(erase_event
) )
3751 // background erased, don't do it again
3757 case wxBG_STYLE_SYSTEM
:
3758 if ( GetThemeEnabled() )
3760 // find ancestor from which to steal background
3761 wxWindow
*parent
= wxGetTopLevelParent((wxWindow
*)this);
3763 parent
= (wxWindow
*)this;
3765 if (gtk_widget_get_mapped(parent
->m_widget
))
3767 wxRegionIterator
upd( m_nativeUpdateRegion
);
3771 rect
.x
= upd
.GetX();
3772 rect
.y
= upd
.GetY();
3773 rect
.width
= upd
.GetWidth();
3774 rect
.height
= upd
.GetHeight();
3776 gtk_paint_flat_box(gtk_widget_get_style(parent
->m_widget
),
3777 GTKGetDrawingWindow(),
3778 gtk_widget_get_state(m_wxwindow
),
3791 case wxBG_STYLE_PAINT
:
3792 // nothing to do: window will be painted over in EVT_PAINT
3796 wxFAIL_MSG( "unsupported background style" );
3799 wxNcPaintEvent
nc_paint_event( GetId() );
3800 nc_paint_event
.SetEventObject( this );
3801 HandleWindowEvent( nc_paint_event
);
3803 wxPaintEvent
paint_event( GetId() );
3804 paint_event
.SetEventObject( this );
3805 HandleWindowEvent( paint_event
);
3807 m_clipPaintRegion
= false;
3809 m_updateRegion
.Clear();
3810 m_nativeUpdateRegion
.Clear();
3813 void wxWindowGTK::SetDoubleBuffered( bool on
)
3815 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
3818 gtk_widget_set_double_buffered( m_wxwindow
, on
);
3821 bool wxWindowGTK::IsDoubleBuffered() const
3823 return gtk_widget_get_double_buffered( m_wxwindow
);
3826 void wxWindowGTK::ClearBackground()
3828 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
3832 void wxWindowGTK::DoSetToolTip( wxToolTip
*tip
)
3834 if (m_tooltip
!= tip
)
3836 wxWindowBase::DoSetToolTip(tip
);
3839 m_tooltip
->GTKSetWindow(static_cast<wxWindow
*>(this));
3841 GTKApplyToolTip(NULL
);
3845 void wxWindowGTK::GTKApplyToolTip(const char* tip
)
3847 wxToolTip::GTKApply(GetConnectWidget(), tip
);
3849 #endif // wxUSE_TOOLTIPS
3851 bool wxWindowGTK::SetBackgroundColour( const wxColour
&colour
)
3853 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
3855 if (!wxWindowBase::SetBackgroundColour(colour
))
3860 // We need the pixel value e.g. for background clearing.
3861 m_backgroundColour
.CalcPixel(gtk_widget_get_colormap(m_widget
));
3864 // apply style change (forceStyle=true so that new style is applied
3865 // even if the bg colour changed from valid to wxNullColour)
3866 GTKApplyWidgetStyle(true);
3871 bool wxWindowGTK::SetForegroundColour( const wxColour
&colour
)
3873 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
3875 if (!wxWindowBase::SetForegroundColour(colour
))
3882 // We need the pixel value e.g. for background clearing.
3883 m_foregroundColour
.CalcPixel(gtk_widget_get_colormap(m_widget
));
3886 // apply style change (forceStyle=true so that new style is applied
3887 // even if the bg colour changed from valid to wxNullColour):
3888 GTKApplyWidgetStyle(true);
3893 PangoContext
*wxWindowGTK::GTKGetPangoDefaultContext()
3895 return gtk_widget_get_pango_context( m_widget
);
3898 GtkRcStyle
*wxWindowGTK::GTKCreateWidgetStyle(bool forceStyle
)
3900 // do we need to apply any changes at all?
3903 !m_foregroundColour
.IsOk() && !m_backgroundColour
.IsOk() )
3908 GtkRcStyle
*style
= gtk_rc_style_new();
3910 if ( m_font
.IsOk() )
3913 pango_font_description_copy( m_font
.GetNativeFontInfo()->description
);
3916 int flagsNormal
= 0,
3919 flagsInsensitive
= 0;
3921 if ( m_foregroundColour
.IsOk() )
3923 const GdkColor
*fg
= m_foregroundColour
.GetColor();
3925 style
->fg
[GTK_STATE_NORMAL
] =
3926 style
->text
[GTK_STATE_NORMAL
] = *fg
;
3927 flagsNormal
|= GTK_RC_FG
| GTK_RC_TEXT
;
3929 style
->fg
[GTK_STATE_PRELIGHT
] =
3930 style
->text
[GTK_STATE_PRELIGHT
] = *fg
;
3931 flagsPrelight
|= GTK_RC_FG
| GTK_RC_TEXT
;
3933 style
->fg
[GTK_STATE_ACTIVE
] =
3934 style
->text
[GTK_STATE_ACTIVE
] = *fg
;
3935 flagsActive
|= GTK_RC_FG
| GTK_RC_TEXT
;
3938 if ( m_backgroundColour
.IsOk() )
3940 const GdkColor
*bg
= m_backgroundColour
.GetColor();
3942 style
->bg
[GTK_STATE_NORMAL
] =
3943 style
->base
[GTK_STATE_NORMAL
] = *bg
;
3944 flagsNormal
|= GTK_RC_BG
| GTK_RC_BASE
;
3946 style
->bg
[GTK_STATE_PRELIGHT
] =
3947 style
->base
[GTK_STATE_PRELIGHT
] = *bg
;
3948 flagsPrelight
|= GTK_RC_BG
| GTK_RC_BASE
;
3950 style
->bg
[GTK_STATE_ACTIVE
] =
3951 style
->base
[GTK_STATE_ACTIVE
] = *bg
;
3952 flagsActive
|= GTK_RC_BG
| GTK_RC_BASE
;
3954 style
->bg
[GTK_STATE_INSENSITIVE
] =
3955 style
->base
[GTK_STATE_INSENSITIVE
] = *bg
;
3956 flagsInsensitive
|= GTK_RC_BG
| GTK_RC_BASE
;
3959 style
->color_flags
[GTK_STATE_NORMAL
] = (GtkRcFlags
)flagsNormal
;
3960 style
->color_flags
[GTK_STATE_PRELIGHT
] = (GtkRcFlags
)flagsPrelight
;
3961 style
->color_flags
[GTK_STATE_ACTIVE
] = (GtkRcFlags
)flagsActive
;
3962 style
->color_flags
[GTK_STATE_INSENSITIVE
] = (GtkRcFlags
)flagsInsensitive
;
3967 void wxWindowGTK::GTKApplyWidgetStyle(bool forceStyle
)
3969 GtkRcStyle
*style
= GTKCreateWidgetStyle(forceStyle
);
3972 DoApplyWidgetStyle(style
);
3973 g_object_unref(style
);
3976 // Style change may affect GTK+'s size calculation:
3977 InvalidateBestSize();
3980 void wxWindowGTK::DoApplyWidgetStyle(GtkRcStyle
*style
)
3984 // block the signal temporarily to avoid sending
3985 // wxSysColourChangedEvents when we change the colours ourselves
3986 bool unblock
= false;
3990 g_signal_handlers_block_by_func(
3991 m_wxwindow
, (void *)gtk_window_style_set_callback
, this);
3994 gtk_widget_modify_style(m_wxwindow
, style
);
3998 g_signal_handlers_unblock_by_func(
3999 m_wxwindow
, (void *)gtk_window_style_set_callback
, this);
4004 gtk_widget_modify_style(m_widget
, style
);
4008 bool wxWindowGTK::SetBackgroundStyle(wxBackgroundStyle style
)
4010 wxWindowBase::SetBackgroundStyle(style
);
4012 if ( style
== wxBG_STYLE_PAINT
)
4017 window
= GTKGetDrawingWindow();
4021 GtkWidget
* const w
= GetConnectWidget();
4022 window
= w
? gtk_widget_get_window(w
) : NULL
;
4027 // Make sure GDK/X11 doesn't refresh the window
4029 gdk_window_set_back_pixmap( window
, None
, False
);
4031 Display
* display
= GDK_WINDOW_DISPLAY(window
);
4034 m_needsStyleChange
= false;
4036 else // window not realized yet
4038 // Do in OnIdle, because the window is not yet available
4039 m_needsStyleChange
= true;
4042 // Don't apply widget style, or we get a grey background
4046 // apply style change (forceStyle=true so that new style is applied
4047 // even if the bg colour changed from valid to wxNullColour):
4048 GTKApplyWidgetStyle(true);
4054 // ----------------------------------------------------------------------------
4055 // Pop-up menu stuff
4056 // ----------------------------------------------------------------------------
4058 #if wxUSE_MENUS_NATIVE
4062 void wxPopupMenuPositionCallback( GtkMenu
*menu
,
4064 gboolean
* WXUNUSED(whatever
),
4065 gpointer user_data
)
4067 // ensure that the menu appears entirely on screen
4069 gtk_widget_get_child_requisition(GTK_WIDGET(menu
), &req
);
4071 wxSize sizeScreen
= wxGetDisplaySize();
4072 wxPoint
*pos
= (wxPoint
*)user_data
;
4074 gint xmax
= sizeScreen
.x
- req
.width
,
4075 ymax
= sizeScreen
.y
- req
.height
;
4077 *x
= pos
->x
< xmax
? pos
->x
: xmax
;
4078 *y
= pos
->y
< ymax
? pos
->y
: ymax
;
4082 bool wxWindowGTK::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
4084 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
4090 GtkMenuPositionFunc posfunc
;
4091 if ( x
== -1 && y
== -1 )
4093 // use GTK's default positioning algorithm
4099 pos
= ClientToScreen(wxPoint(x
, y
));
4101 posfunc
= wxPopupMenuPositionCallback
;
4104 menu
->m_popupShown
= true;
4106 GTK_MENU(menu
->m_menu
),
4107 NULL
, // parent menu shell
4108 NULL
, // parent menu item
4109 posfunc
, // function to position it
4110 userdata
, // client data
4111 0, // button used to activate it
4112 gtk_get_current_event_time()
4115 while (menu
->m_popupShown
)
4117 gtk_main_iteration();
4123 #endif // wxUSE_MENUS_NATIVE
4125 #if wxUSE_DRAG_AND_DROP
4127 void wxWindowGTK::SetDropTarget( wxDropTarget
*dropTarget
)
4129 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
4131 GtkWidget
*dnd_widget
= GetConnectWidget();
4133 if (m_dropTarget
) m_dropTarget
->GtkUnregisterWidget( dnd_widget
);
4135 if (m_dropTarget
) delete m_dropTarget
;
4136 m_dropTarget
= dropTarget
;
4138 if (m_dropTarget
) m_dropTarget
->GtkRegisterWidget( dnd_widget
);
4141 #endif // wxUSE_DRAG_AND_DROP
4143 GtkWidget
* wxWindowGTK::GetConnectWidget()
4145 GtkWidget
*connect_widget
= m_widget
;
4146 if (m_wxwindow
) connect_widget
= m_wxwindow
;
4148 return connect_widget
;
4151 bool wxWindowGTK::GTKIsOwnWindow(GdkWindow
*window
) const
4153 wxArrayGdkWindows windowsThis
;
4154 GdkWindow
* const winThis
= GTKGetWindow(windowsThis
);
4156 return winThis
? window
== winThis
4157 : windowsThis
.Index(window
) != wxNOT_FOUND
;
4160 GdkWindow
*wxWindowGTK::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
4162 return m_wxwindow
? GTKGetDrawingWindow() : gtk_widget_get_window(m_widget
);
4165 bool wxWindowGTK::SetFont( const wxFont
&font
)
4167 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
4169 if (!wxWindowBase::SetFont(font
))
4172 // apply style change (forceStyle=true so that new style is applied
4173 // even if the font changed from valid to wxNullFont):
4174 GTKApplyWidgetStyle(true);
4179 void wxWindowGTK::DoCaptureMouse()
4181 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
4183 GdkWindow
*window
= NULL
;
4185 window
= GTKGetDrawingWindow();
4187 window
= gtk_widget_get_window(GetConnectWidget());
4189 wxCHECK_RET( window
, wxT("CaptureMouse() failed") );
4191 const wxCursor
* cursor
= &m_cursor
;
4192 if (!cursor
->IsOk())
4193 cursor
= wxSTANDARD_CURSOR
;
4195 gdk_pointer_grab( window
, FALSE
,
4197 (GDK_BUTTON_PRESS_MASK
|
4198 GDK_BUTTON_RELEASE_MASK
|
4199 GDK_POINTER_MOTION_HINT_MASK
|
4200 GDK_POINTER_MOTION_MASK
),
4202 cursor
->GetCursor(),
4203 (guint32
)GDK_CURRENT_TIME
);
4204 g_captureWindow
= this;
4205 g_captureWindowHasMouse
= true;
4208 void wxWindowGTK::DoReleaseMouse()
4210 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
4212 wxCHECK_RET( g_captureWindow
, wxT("can't release mouse - not captured") );
4214 g_captureWindow
= NULL
;
4216 GdkWindow
*window
= NULL
;
4218 window
= GTKGetDrawingWindow();
4220 window
= gtk_widget_get_window(GetConnectWidget());
4225 gdk_pointer_ungrab ( (guint32
)GDK_CURRENT_TIME
);
4228 void wxWindowGTK::GTKReleaseMouseAndNotify()
4231 wxMouseCaptureLostEvent
evt(GetId());
4232 evt
.SetEventObject( this );
4233 HandleWindowEvent( evt
);
4237 wxWindow
*wxWindowBase::GetCapture()
4239 return (wxWindow
*)g_captureWindow
;
4242 bool wxWindowGTK::IsRetained() const
4247 void wxWindowGTK::SetScrollbar(int orient
,
4251 bool WXUNUSED(update
))
4253 const int dir
= ScrollDirFromOrient(orient
);
4254 GtkRange
* const sb
= m_scrollBar
[dir
];
4255 wxCHECK_RET( sb
, wxT("this window is not scrollable") );
4259 // GtkRange requires upper > lower
4264 g_signal_handlers_block_by_func(
4265 sb
, (void*)gtk_scrollbar_value_changed
, this);
4267 gtk_range_set_increments(sb
, 1, thumbVisible
);
4268 gtk_adjustment_set_page_size(gtk_range_get_adjustment(sb
), thumbVisible
);
4269 gtk_range_set_range(sb
, 0, range
);
4270 gtk_range_set_value(sb
, pos
);
4271 m_scrollPos
[dir
] = gtk_range_get_value(sb
);
4273 g_signal_handlers_unblock_by_func(
4274 sb
, (void*)gtk_scrollbar_value_changed
, this);
4277 void wxWindowGTK::SetScrollPos(int orient
, int pos
, bool WXUNUSED(refresh
))
4279 const int dir
= ScrollDirFromOrient(orient
);
4280 GtkRange
* const sb
= m_scrollBar
[dir
];
4281 wxCHECK_RET( sb
, wxT("this window is not scrollable") );
4283 // This check is more than an optimization. Without it, the slider
4284 // will not move smoothly while tracking when using wxScrollHelper.
4285 if (GetScrollPos(orient
) != pos
)
4287 g_signal_handlers_block_by_func(
4288 sb
, (void*)gtk_scrollbar_value_changed
, this);
4290 gtk_range_set_value(sb
, pos
);
4291 m_scrollPos
[dir
] = gtk_range_get_value(sb
);
4293 g_signal_handlers_unblock_by_func(
4294 sb
, (void*)gtk_scrollbar_value_changed
, this);
4298 int wxWindowGTK::GetScrollThumb(int orient
) const
4300 GtkRange
* const sb
= m_scrollBar
[ScrollDirFromOrient(orient
)];
4301 wxCHECK_MSG( sb
, 0, wxT("this window is not scrollable") );
4303 return wxRound(gtk_adjustment_get_page_size(gtk_range_get_adjustment(sb
)));
4306 int wxWindowGTK::GetScrollPos( int orient
) const
4308 GtkRange
* const sb
= m_scrollBar
[ScrollDirFromOrient(orient
)];
4309 wxCHECK_MSG( sb
, 0, wxT("this window is not scrollable") );
4311 return wxRound(gtk_range_get_value(sb
));
4314 int wxWindowGTK::GetScrollRange( int orient
) const
4316 GtkRange
* const sb
= m_scrollBar
[ScrollDirFromOrient(orient
)];
4317 wxCHECK_MSG( sb
, 0, wxT("this window is not scrollable") );
4319 return wxRound(gtk_adjustment_get_upper(gtk_range_get_adjustment(sb
)));
4322 // Determine if increment is the same as +/-x, allowing for some small
4323 // difference due to possible inexactness in floating point arithmetic
4324 static inline bool IsScrollIncrement(double increment
, double x
)
4326 wxASSERT(increment
> 0);
4327 const double tolerance
= 1.0 / 1024;
4328 return fabs(increment
- fabs(x
)) < tolerance
;
4331 wxEventType
wxWindowGTK::GTKGetScrollEventType(GtkRange
* range
)
4333 wxASSERT(range
== m_scrollBar
[0] || range
== m_scrollBar
[1]);
4335 const int barIndex
= range
== m_scrollBar
[1];
4337 const double value
= gtk_range_get_value(range
);
4339 // save previous position
4340 const double oldPos
= m_scrollPos
[barIndex
];
4341 // update current position
4342 m_scrollPos
[barIndex
] = value
;
4343 // If event should be ignored, or integral position has not changed
4344 if (!m_hasVMT
|| g_blockEventsOnDrag
|| wxRound(value
) == wxRound(oldPos
))
4349 wxEventType eventType
= wxEVT_SCROLL_THUMBTRACK
;
4352 // Difference from last change event
4353 const double diff
= value
- oldPos
;
4354 const bool isDown
= diff
> 0;
4356 GtkAdjustment
* adj
= gtk_range_get_adjustment(range
);
4357 if (IsScrollIncrement(gtk_adjustment_get_step_increment(adj
), diff
))
4359 eventType
= isDown
? wxEVT_SCROLL_LINEDOWN
: wxEVT_SCROLL_LINEUP
;
4361 else if (IsScrollIncrement(gtk_adjustment_get_page_increment(adj
), diff
))
4363 eventType
= isDown
? wxEVT_SCROLL_PAGEDOWN
: wxEVT_SCROLL_PAGEUP
;
4365 else if (m_mouseButtonDown
)
4367 // Assume track event
4368 m_isScrolling
= true;
4374 void wxWindowGTK::ScrollWindow( int dx
, int dy
, const wxRect
* WXUNUSED(rect
) )
4376 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
4378 wxCHECK_RET( m_wxwindow
!= NULL
, wxT("window needs client area for scrolling") );
4380 // No scrolling requested.
4381 if ((dx
== 0) && (dy
== 0)) return;
4383 m_clipPaintRegion
= true;
4385 WX_PIZZA(m_wxwindow
)->scroll(dx
, dy
);
4387 m_clipPaintRegion
= false;
4390 bool restoreCaret
= (GetCaret() != NULL
&& GetCaret()->IsVisible());
4393 wxRect
caretRect(GetCaret()->GetPosition(), GetCaret()->GetSize());
4395 caretRect
.width
+= dx
;
4398 caretRect
.x
+= dx
; caretRect
.width
-= dx
;
4401 caretRect
.height
+= dy
;
4404 caretRect
.y
+= dy
; caretRect
.height
-= dy
;
4407 RefreshRect(caretRect
);
4409 #endif // wxUSE_CARET
4412 void wxWindowGTK::GTKScrolledWindowSetBorder(GtkWidget
* w
, int wxstyle
)
4414 //RN: Note that static controls usually have no border on gtk, so maybe
4415 //it makes sense to treat that as simply no border at the wx level
4417 if (!(wxstyle
& wxNO_BORDER
) && !(wxstyle
& wxBORDER_STATIC
))
4419 GtkShadowType gtkstyle
;
4421 if(wxstyle
& wxBORDER_RAISED
)
4422 gtkstyle
= GTK_SHADOW_OUT
;
4423 else if ((wxstyle
& wxBORDER_SUNKEN
) || (wxstyle
& wxBORDER_THEME
))
4424 gtkstyle
= GTK_SHADOW_IN
;
4427 else if (wxstyle
& wxBORDER_DOUBLE
)
4428 gtkstyle
= GTK_SHADOW_ETCHED_IN
;
4431 gtkstyle
= GTK_SHADOW_IN
;
4433 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(w
),
4438 void wxWindowGTK::SetWindowStyleFlag( long style
)
4440 // Updates the internal variable. NB: Now m_windowStyle bits carry the _new_ style values already
4441 wxWindowBase::SetWindowStyleFlag(style
);
4444 // Find the wxWindow at the current mouse position, also returning the mouse
4446 wxWindow
* wxFindWindowAtPointer(wxPoint
& pt
)
4448 pt
= wxGetMousePosition();
4449 wxWindow
* found
= wxFindWindowAtPoint(pt
);
4453 // Get the current mouse position.
4454 wxPoint
wxGetMousePosition()
4456 /* This crashes when used within wxHelpContext,
4457 so we have to use the X-specific implementation below.
4459 GdkModifierType *mask;
4460 (void) gdk_window_get_pointer(NULL, &x, &y, mask);
4462 return wxPoint(x, y);
4466 GdkWindow
* windowAtPtr
= gdk_window_at_pointer(& x
, & y
);
4468 Display
*display
= windowAtPtr
? GDK_WINDOW_XDISPLAY(windowAtPtr
) : GDK_DISPLAY();
4469 Window rootWindow
= RootWindowOfScreen (DefaultScreenOfDisplay(display
));
4470 Window rootReturn
, childReturn
;
4471 int rootX
, rootY
, winX
, winY
;
4472 unsigned int maskReturn
;
4474 XQueryPointer (display
,
4478 &rootX
, &rootY
, &winX
, &winY
, &maskReturn
);
4479 return wxPoint(rootX
, rootY
);
4483 GdkWindow
* wxWindowGTK::GTKGetDrawingWindow() const
4485 GdkWindow
* window
= NULL
;
4487 window
= gtk_widget_get_window(m_wxwindow
);
4491 // ----------------------------------------------------------------------------
4493 // ----------------------------------------------------------------------------
4498 // this is called if we attempted to freeze unrealized widget when it finally
4499 // is realized (and so can be frozen):
4500 static void wx_frozen_widget_realize(GtkWidget
* w
, wxWindowGTK
* win
)
4502 wxASSERT( w
&& gtk_widget_get_has_window(w
) );
4503 wxASSERT( gtk_widget_get_realized(w
) );
4505 g_signal_handlers_disconnect_by_func
4508 (void*)wx_frozen_widget_realize
,
4513 if (w
== win
->m_wxwindow
)
4514 window
= win
->GTKGetDrawingWindow();
4516 window
= gtk_widget_get_window(w
);
4517 gdk_window_freeze_updates(window
);
4522 void wxWindowGTK::GTKFreezeWidget(GtkWidget
*w
)
4524 if ( !w
|| !gtk_widget_get_has_window(w
) )
4525 return; // window-less widget, cannot be frozen
4527 GdkWindow
* window
= gtk_widget_get_window(w
);
4530 // we can't thaw unrealized widgets because they don't have GdkWindow,
4531 // so set it up to be done immediately after realization:
4532 g_signal_connect_after
4536 G_CALLBACK(wx_frozen_widget_realize
),
4542 if (w
== m_wxwindow
)
4543 window
= GTKGetDrawingWindow();
4544 gdk_window_freeze_updates(window
);
4547 void wxWindowGTK::GTKThawWidget(GtkWidget
*w
)
4549 if ( !w
|| !gtk_widget_get_has_window(w
) )
4550 return; // window-less widget, cannot be frozen
4552 GdkWindow
* window
= gtk_widget_get_window(w
);
4555 // the widget wasn't realized yet, no need to thaw
4556 g_signal_handlers_disconnect_by_func
4559 (void*)wx_frozen_widget_realize
,
4565 if (w
== m_wxwindow
)
4566 window
= GTKGetDrawingWindow();
4567 gdk_window_thaw_updates(window
);
4570 void wxWindowGTK::DoFreeze()
4572 GTKFreezeWidget(m_widget
);
4573 if ( m_wxwindow
&& m_widget
!= m_wxwindow
)
4574 GTKFreezeWidget(m_wxwindow
);
4577 void wxWindowGTK::DoThaw()
4579 GTKThawWidget(m_widget
);
4580 if ( m_wxwindow
&& m_widget
!= m_wxwindow
)
4581 GTKThawWidget(m_wxwindow
);