1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/window.cpp
3 // Purpose: wxWindowGTK implementation
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
14 #define XWarpPointer XWARPPOINTER
17 #include "wx/window.h"
22 #include "wx/toplevel.h"
23 #include "wx/dcclient.h"
25 #include "wx/settings.h"
26 #include "wx/msgdlg.h"
31 #include "wx/tooltip.h"
33 #include "wx/fontutil.h"
34 #include "wx/scopeguard.h"
35 #include "wx/sysopt.h"
39 #include "wx/gtk/private.h"
40 #include "wx/gtk/private/win_gtk.h"
41 #include "wx/gtk/private/event.h"
42 using namespace wxGTKImpl
;
46 #include <gdk/gdkkeysyms.h>
47 #if GTK_CHECK_VERSION(3,0,0)
48 #include <gdk/gdkkeysyms-compat.h>
51 #if wxUSE_GRAPHICS_CONTEXT
52 #include "wx/graphics.h"
53 #include "wx/scopedptr.h"
54 #endif // wxUSE_GRAPHICS_CONTEXT
56 // gdk_window_set_composited() is only supported since 2.12
57 #define wxGTK_VERSION_REQUIRED_FOR_COMPOSITING 2,12,0
58 #define wxGTK_HAS_COMPOSITING_SUPPORT GTK_CHECK_VERSION(2,12,0)
60 //-----------------------------------------------------------------------------
61 // documentation on internals
62 //-----------------------------------------------------------------------------
65 I have been asked several times about writing some documentation about
66 the GTK port of wxWidgets, especially its internal structures. Obviously,
67 you cannot understand wxGTK without knowing a little about the GTK, but
68 some more information about what the wxWindow, which is the base class
69 for all other window classes, does seems required as well.
73 What does wxWindow do? It contains the common interface for the following
74 jobs of its descendants:
76 1) Define the rudimentary behaviour common to all window classes, such as
77 resizing, intercepting user input (so as to make it possible to use these
78 events for special purposes in a derived class), window names etc.
80 2) Provide the possibility to contain and manage children, if the derived
81 class is allowed to contain children, which holds true for those window
82 classes which do not display a native GTK widget. To name them, these
83 classes are wxPanel, wxScrolledWindow, wxDialog, wxFrame. The MDI frame-
84 work classes are a special case and are handled a bit differently from
85 the rest. The same holds true for the wxNotebook class.
87 3) Provide the possibility to draw into a client area of a window. This,
88 too, only holds true for classes that do not display a native GTK widget
91 4) Provide the entire mechanism for scrolling widgets. This actual inter-
92 face for this is usually in wxScrolledWindow, but the GTK implementation
95 5) A multitude of helper or extra methods for special purposes, such as
96 Drag'n'Drop, managing validators etc.
98 6) Display a border (sunken, raised, simple or none).
100 Normally one might expect, that one wxWidgets window would always correspond
101 to one GTK widget. Under GTK, there is no such all-round widget that has all
102 the functionality. Moreover, the GTK defines a client area as a different
103 widget from the actual widget you are handling. Last but not least some
104 special classes (e.g. wxFrame) handle different categories of widgets and
105 still have the possibility to draw something in the client area.
106 It was therefore required to write a special purpose GTK widget, that would
107 represent a client area in the sense of wxWidgets capable to do the jobs
108 2), 3) and 4). I have written this class and it resides in win_gtk.c of
111 All windows must have a widget, with which they interact with other under-
112 lying GTK widgets. It is this widget, e.g. that has to be resized etc and
113 the wxWindow class has a member variable called m_widget which holds a
114 pointer to this widget. When the window class represents a GTK native widget,
115 this is (in most cases) the only GTK widget the class manages. E.g. the
116 wxStaticText class handles only a GtkLabel widget a pointer to which you
117 can find in m_widget (defined in wxWindow)
119 When the class has a client area for drawing into and for containing children
120 it has to handle the client area widget (of the type wxPizza, defined in
121 win_gtk.cpp), but there could be any number of widgets, handled by a class.
122 The common rule for all windows is only, that the widget that interacts with
123 the rest of GTK must be referenced in m_widget and all other widgets must be
124 children of this widget on the GTK level. The top-most widget, which also
125 represents the client area, must be in the m_wxwindow field and must be of
128 As I said, the window classes that display a GTK native widget only have
129 one widget, so in the case of e.g. the wxButton class m_widget holds a
130 pointer to a GtkButton widget. But windows with client areas (for drawing
131 and children) have a m_widget field that is a pointer to a GtkScrolled-
132 Window and a m_wxwindow field that is pointer to a wxPizza and this
133 one is (in the GTK sense) a child of the GtkScrolledWindow.
135 If the m_wxwindow field is set, then all input to this widget is inter-
136 cepted and sent to the wxWidgets class. If not, all input to the widget
137 that gets pointed to by m_widget gets intercepted and sent to the class.
141 The design of scrolling in wxWidgets is markedly different from that offered
142 by the GTK itself and therefore we cannot simply take it as it is. In GTK,
143 clicking on a scrollbar belonging to scrolled window will inevitably move
144 the window. In wxWidgets, the scrollbar will only emit an event, send this
145 to (normally) a wxScrolledWindow and that class will call ScrollWindow()
146 which actually moves the window and its sub-windows. Note that wxPizza
147 memorizes how much it has been scrolled but that wxWidgets forgets this
148 so that the two coordinates systems have to be kept in synch. This is done
149 in various places using the pizza->m_scroll_x and pizza->m_scroll_y values.
153 Singularly the most broken code in GTK is the code that is supposed to
154 inform subwindows (child windows) about new positions. Very often, duplicate
155 events are sent without changes in size or position, equally often no
156 events are sent at all (All this is due to a bug in the GtkContainer code
157 which got fixed in GTK 1.2.6). For that reason, wxGTK completely ignores
158 GTK's own system and it simply waits for size events for toplevel windows
159 and then iterates down the respective size events to all window. This has
160 the disadvantage that windows might get size events before the GTK widget
161 actually has the reported size. This doesn't normally pose any problem, but
162 the OpenGL drawing routines rely on correct behaviour. Therefore, I have
163 added the m_nativeSizeEvents flag, which is true only for the OpenGL canvas,
164 i.e. the wxGLCanvas will emit a size event, when (and not before) the X11
165 window that is used for OpenGL output really has that size (as reported by
170 If someone at some point of time feels the immense desire to have a look at,
171 change or attempt to optimise the Refresh() logic, this person will need an
172 intimate understanding of what "draw" and "expose" events are and what
173 they are used for, in particular when used in connection with GTK's
174 own windowless widgets. Beware.
178 Cursors, too, have been a constant source of pleasure. The main difficulty
179 is that a GdkWindow inherits a cursor if the programmer sets a new cursor
180 for the parent. To prevent this from doing too much harm, SetCursor calls
181 GTKUpdateCursor, which will recursively re-set the cursors of all child windows.
182 Also don't forget that cursors (like much else) are connected to GdkWindows,
183 not GtkWidgets and that the "window" field of a GtkWidget might very well
184 point to the GdkWindow of the parent widget (-> "window-less widget") and
185 that the two obviously have very different meanings.
188 //-----------------------------------------------------------------------------
190 //-----------------------------------------------------------------------------
192 // Don't allow event propagation during drag
193 bool g_blockEventsOnDrag
;
194 // Don't allow mouse event propagation during scroll
195 bool g_blockEventsOnScroll
;
196 extern wxCursor g_globalCursor
;
198 // mouse capture state: the window which has it and if the mouse is currently
200 static wxWindowGTK
*g_captureWindow
= NULL
;
201 static bool g_captureWindowHasMouse
= false;
203 // The window that currently has focus:
204 static wxWindowGTK
*gs_currentFocus
= NULL
;
205 // The window that is scheduled to get focus in the next event loop iteration
206 // or NULL if there's no pending focus change:
207 static wxWindowGTK
*gs_pendingFocus
= NULL
;
209 // the window that has deferred focus-out event pending, if any (see
210 // GTKAddDeferredFocusOut() for details)
211 static wxWindowGTK
*gs_deferredFocusOut
= NULL
;
213 // global variables because GTK+ DnD want to have the
214 // mouse event that caused it
215 GdkEvent
*g_lastMouseEvent
= NULL
;
216 int g_lastButtonNumber
= 0;
218 //-----------------------------------------------------------------------------
220 //-----------------------------------------------------------------------------
222 // the trace mask used for the focus debugging messages
223 #define TRACE_FOCUS wxT("focus")
225 //-----------------------------------------------------------------------------
226 // "size_request" of m_widget
227 //-----------------------------------------------------------------------------
231 wxgtk_window_size_request_callback(GtkWidget
* WXUNUSED(widget
),
232 GtkRequisition
*requisition
,
236 win
->GetSize( &w
, &h
);
242 requisition
->height
= h
;
243 requisition
->width
= w
;
247 //-----------------------------------------------------------------------------
248 // "expose_event" of m_wxwindow
249 //-----------------------------------------------------------------------------
253 gtk_window_expose_callback( GtkWidget
*,
254 GdkEventExpose
*gdk_event
,
257 if (gdk_event
->window
== win
->GTKGetDrawingWindow())
259 win
->GetUpdateRegion() = wxRegion( gdk_event
->region
);
260 win
->GtkSendPaintEvents();
262 // Let parent window draw window-less widgets
267 #ifndef __WXUNIVERSAL__
268 //-----------------------------------------------------------------------------
269 // "expose_event" from m_wxwindow->parent, for drawing border
270 //-----------------------------------------------------------------------------
274 expose_event_border(GtkWidget
* widget
, GdkEventExpose
* gdk_event
, wxWindow
* win
)
276 if (gdk_event
->window
!= gtk_widget_get_parent_window(win
->m_wxwindow
))
283 gtk_widget_get_allocation(win
->m_wxwindow
, &alloc
);
284 const int x
= alloc
.x
;
285 const int y
= alloc
.y
;
286 const int w
= alloc
.width
;
287 const int h
= alloc
.height
;
289 if (w
<= 0 || h
<= 0)
292 if (win
->HasFlag(wxBORDER_SIMPLE
))
294 gdk_draw_rectangle(gdk_event
->window
,
295 gtk_widget_get_style(widget
)->black_gc
, false, x
, y
, w
- 1, h
- 1);
299 GtkShadowType shadow
= GTK_SHADOW_IN
;
300 if (win
->HasFlag(wxBORDER_RAISED
))
301 shadow
= GTK_SHADOW_OUT
;
303 // Style detail to use
305 if (win
->m_widget
== win
->m_wxwindow
)
306 // for non-scrollable wxWindows
309 // for scrollable ones
312 // clip rect is required to avoid painting background
313 // over upper left (w,h) of parent window
314 GdkRectangle clipRect
= { x
, y
, w
, h
};
316 gtk_widget_get_style(win
->m_wxwindow
), gdk_event
->window
, GTK_STATE_NORMAL
,
317 shadow
, &clipRect
, wxGTKPrivate::GetEntryWidget(), detail
, x
, y
, w
, h
);
323 //-----------------------------------------------------------------------------
324 // "parent_set" from m_wxwindow
325 //-----------------------------------------------------------------------------
329 parent_set(GtkWidget
* widget
, GtkWidget
* old_parent
, wxWindow
* win
)
333 g_signal_handlers_disconnect_by_func(
334 old_parent
, (void*)expose_event_border
, win
);
336 GtkWidget
* parent
= gtk_widget_get_parent(widget
);
339 g_signal_connect_after(parent
, "expose_event",
340 G_CALLBACK(expose_event_border
), win
);
344 #endif // !__WXUNIVERSAL__
346 //-----------------------------------------------------------------------------
347 // "key_press_event" from any window
348 //-----------------------------------------------------------------------------
350 // set WXTRACE to this to see the key event codes on the console
351 #define TRACE_KEYS wxT("keyevent")
353 // translates an X key symbol to WXK_XXX value
355 // if isChar is true it means that the value returned will be used for EVT_CHAR
356 // event and then we choose the logical WXK_XXX, i.e. '/' for GDK_KP_Divide,
357 // for example, while if it is false it means that the value is going to be
358 // used for KEY_DOWN/UP events and then we translate GDK_KP_Divide to
360 static long wxTranslateKeySymToWXKey(KeySym keysym
, bool isChar
)
366 // Shift, Control and Alt don't generate the CHAR events at all
369 key_code
= isChar
? 0 : WXK_SHIFT
;
373 key_code
= isChar
? 0 : WXK_CONTROL
;
381 key_code
= isChar
? 0 : WXK_ALT
;
384 // neither do the toggle modifies
385 case GDK_Scroll_Lock
:
386 key_code
= isChar
? 0 : WXK_SCROLL
;
390 key_code
= isChar
? 0 : WXK_CAPITAL
;
394 key_code
= isChar
? 0 : WXK_NUMLOCK
;
398 // various other special keys
411 case GDK_ISO_Left_Tab
:
418 key_code
= WXK_RETURN
;
422 key_code
= WXK_CLEAR
;
426 key_code
= WXK_PAUSE
;
430 key_code
= WXK_SELECT
;
434 key_code
= WXK_PRINT
;
438 key_code
= WXK_EXECUTE
;
442 key_code
= WXK_ESCAPE
;
445 // cursor and other extended keyboard keys
447 key_code
= WXK_DELETE
;
463 key_code
= WXK_RIGHT
;
470 case GDK_Prior
: // == GDK_Page_Up
471 key_code
= WXK_PAGEUP
;
474 case GDK_Next
: // == GDK_Page_Down
475 key_code
= WXK_PAGEDOWN
;
487 key_code
= WXK_INSERT
;
502 key_code
= (isChar
? '0' : int(WXK_NUMPAD0
)) + keysym
- GDK_KP_0
;
506 key_code
= isChar
? ' ' : int(WXK_NUMPAD_SPACE
);
510 key_code
= isChar
? WXK_TAB
: WXK_NUMPAD_TAB
;
514 key_code
= isChar
? WXK_RETURN
: WXK_NUMPAD_ENTER
;
518 key_code
= isChar
? WXK_F1
: WXK_NUMPAD_F1
;
522 key_code
= isChar
? WXK_F2
: WXK_NUMPAD_F2
;
526 key_code
= isChar
? WXK_F3
: WXK_NUMPAD_F3
;
530 key_code
= isChar
? WXK_F4
: WXK_NUMPAD_F4
;
534 key_code
= isChar
? WXK_HOME
: WXK_NUMPAD_HOME
;
538 key_code
= isChar
? WXK_LEFT
: WXK_NUMPAD_LEFT
;
542 key_code
= isChar
? WXK_UP
: WXK_NUMPAD_UP
;
546 key_code
= isChar
? WXK_RIGHT
: WXK_NUMPAD_RIGHT
;
550 key_code
= isChar
? WXK_DOWN
: WXK_NUMPAD_DOWN
;
553 case GDK_KP_Prior
: // == GDK_KP_Page_Up
554 key_code
= isChar
? WXK_PAGEUP
: WXK_NUMPAD_PAGEUP
;
557 case GDK_KP_Next
: // == GDK_KP_Page_Down
558 key_code
= isChar
? WXK_PAGEDOWN
: WXK_NUMPAD_PAGEDOWN
;
562 key_code
= isChar
? WXK_END
: WXK_NUMPAD_END
;
566 key_code
= isChar
? WXK_HOME
: WXK_NUMPAD_BEGIN
;
570 key_code
= isChar
? WXK_INSERT
: WXK_NUMPAD_INSERT
;
574 key_code
= isChar
? WXK_DELETE
: WXK_NUMPAD_DELETE
;
578 key_code
= isChar
? '=' : int(WXK_NUMPAD_EQUAL
);
581 case GDK_KP_Multiply
:
582 key_code
= isChar
? '*' : int(WXK_NUMPAD_MULTIPLY
);
586 key_code
= isChar
? '+' : int(WXK_NUMPAD_ADD
);
589 case GDK_KP_Separator
:
590 // FIXME: what is this?
591 key_code
= isChar
? '.' : int(WXK_NUMPAD_SEPARATOR
);
594 case GDK_KP_Subtract
:
595 key_code
= isChar
? '-' : int(WXK_NUMPAD_SUBTRACT
);
599 key_code
= isChar
? '.' : int(WXK_NUMPAD_DECIMAL
);
603 key_code
= isChar
? '/' : int(WXK_NUMPAD_DIVIDE
);
620 key_code
= WXK_F1
+ keysym
- GDK_F1
;
630 static inline bool wxIsAsciiKeysym(KeySym ks
)
635 static void wxFillOtherKeyEventFields(wxKeyEvent
& event
,
637 GdkEventKey
*gdk_event
)
639 event
.SetTimestamp( gdk_event
->time
);
640 event
.SetId(win
->GetId());
642 event
.m_shiftDown
= (gdk_event
->state
& GDK_SHIFT_MASK
) != 0;
643 event
.m_controlDown
= (gdk_event
->state
& GDK_CONTROL_MASK
) != 0;
644 event
.m_altDown
= (gdk_event
->state
& GDK_MOD1_MASK
) != 0;
645 event
.m_metaDown
= (gdk_event
->state
& GDK_META_MASK
) != 0;
647 // Normally we take the state of modifiers directly from the low level GDK
648 // event but unfortunately GDK uses a different convention from MSW for the
649 // key events corresponding to the modifier keys themselves: in it, when
650 // e.g. Shift key is pressed, GDK_SHIFT_MASK is not set while it is set
651 // when Shift is released. Under MSW the situation is exactly reversed and
652 // the modifier corresponding to the key is set when it is pressed and
653 // unset when it is released. To ensure consistent behaviour between
654 // platforms (and because it seems to make slightly more sense, although
655 // arguably both behaviours are reasonable) we follow MSW here.
657 // Final notice: we set the flags to the desired value instead of just
658 // inverting them because they are not set correctly (i.e. in the same way
659 // as for the real events generated by the user) for wxUIActionSimulator-
660 // produced events and it seems better to keep that class code the same
661 // among all platforms and fix the discrepancy here instead of adding
662 // wxGTK-specific code to wxUIActionSimulator.
663 const bool isPress
= gdk_event
->type
== GDK_KEY_PRESS
;
664 switch ( gdk_event
->keyval
)
668 event
.m_shiftDown
= isPress
;
673 event
.m_controlDown
= isPress
;
678 event
.m_altDown
= isPress
;
685 event
.m_metaDown
= isPress
;
689 event
.m_rawCode
= (wxUint32
) gdk_event
->keyval
;
690 event
.m_rawFlags
= gdk_event
->hardware_keycode
;
692 wxGetMousePosition(&event
.m_x
, &event
.m_y
);
693 win
->ScreenToClient(&event
.m_x
, &event
.m_y
);
694 event
.SetEventObject( win
);
699 wxTranslateGTKKeyEventToWx(wxKeyEvent
& event
,
701 GdkEventKey
*gdk_event
)
703 // VZ: it seems that GDK_KEY_RELEASE event doesn't set event->string
704 // but only event->keyval which is quite useless to us, so remember
705 // the last character from GDK_KEY_PRESS and reuse it as last resort
707 // NB: should be MT-safe as we're always called from the main thread only
712 } s_lastKeyPress
= { 0, 0 };
714 KeySym keysym
= gdk_event
->keyval
;
716 wxLogTrace(TRACE_KEYS
, wxT("Key %s event: keysym = %ld"),
717 event
.GetEventType() == wxEVT_KEY_UP
? wxT("release")
721 long key_code
= wxTranslateKeySymToWXKey(keysym
, false /* !isChar */);
725 // do we have the translation or is it a plain ASCII character?
726 if ( (gdk_event
->length
== 1) || wxIsAsciiKeysym(keysym
) )
728 // we should use keysym if it is ASCII as X does some translations
729 // like "I pressed while Control is down" => "Ctrl-I" == "TAB"
730 // which we don't want here (but which we do use for OnChar())
731 if ( !wxIsAsciiKeysym(keysym
) )
733 keysym
= (KeySym
)gdk_event
->string
[0];
736 // we want to always get the same key code when the same key is
737 // pressed regardless of the state of the modifiers, i.e. on a
738 // standard US keyboard pressing '5' or '%' ('5' key with
739 // Shift) should result in the same key code in OnKeyDown():
740 // '5' (although OnChar() will get either '5' or '%').
742 // to do it we first translate keysym to keycode (== scan code)
743 // and then back but always using the lower register
744 Display
*dpy
= (Display
*)wxGetDisplay();
745 KeyCode keycode
= XKeysymToKeycode(dpy
, keysym
);
747 wxLogTrace(TRACE_KEYS
, wxT("\t-> keycode %d"), keycode
);
749 KeySym keysymNormalized
= XKeycodeToKeysym(dpy
, keycode
, 0);
751 // use the normalized, i.e. lower register, keysym if we've
753 key_code
= keysymNormalized
? keysymNormalized
: keysym
;
755 // as explained above, we want to have lower register key codes
756 // normally but for the letter keys we want to have the upper ones
758 // NB: don't use XConvertCase() here, we want to do it for letters
760 key_code
= toupper(key_code
);
762 else // non ASCII key, what to do?
764 // by default, ignore it
767 // but if we have cached information from the last KEY_PRESS
768 if ( gdk_event
->type
== GDK_KEY_RELEASE
)
771 if ( keysym
== s_lastKeyPress
.keysym
)
773 key_code
= s_lastKeyPress
.keycode
;
778 if ( gdk_event
->type
== GDK_KEY_PRESS
)
780 // remember it to be reused for KEY_UP event later
781 s_lastKeyPress
.keysym
= keysym
;
782 s_lastKeyPress
.keycode
= key_code
;
786 wxLogTrace(TRACE_KEYS
, wxT("\t-> wxKeyCode %ld"), key_code
);
788 // sending unknown key events doesn't really make sense
792 event
.m_keyCode
= key_code
;
795 event
.m_uniChar
= gdk_keyval_to_unicode(key_code
? key_code
: keysym
);
796 if ( !event
.m_uniChar
&& event
.m_keyCode
<= WXK_DELETE
)
798 // Set Unicode key code to the ASCII equivalent for compatibility. E.g.
799 // let RETURN generate the key event with both key and Unicode key
801 event
.m_uniChar
= event
.m_keyCode
;
803 #endif // wxUSE_UNICODE
805 // now fill all the other fields
806 wxFillOtherKeyEventFields(event
, win
, gdk_event
);
814 GtkIMContext
*context
;
815 GdkEventKey
*lastKeyEvent
;
819 context
= gtk_im_multicontext_new();
824 g_object_unref (context
);
831 // Send wxEVT_CHAR_HOOK event to the parent of the window and return true only
832 // if it was processed (and not skipped).
833 bool SendCharHookEvent(const wxKeyEvent
& event
, wxWindow
*win
)
835 // wxEVT_CHAR_HOOK must be sent to allow the parent windows (e.g. a dialog
836 // which typically closes when Esc key is pressed in any of its controls)
837 // to handle key events in all of its children unless the mouse is captured
838 // in which case we consider that the keyboard should be "captured" too.
839 if ( !g_captureWindow
)
841 wxKeyEvent
eventCharHook(wxEVT_CHAR_HOOK
, event
);
842 if ( win
->HandleWindowEvent(eventCharHook
)
843 && !event
.IsNextEventAllowed() )
850 // Adjust wxEVT_CHAR event key code fields. This function takes care of two
852 // (a) Ctrl-letter key presses generate key codes in range 1..26
853 // (b) Unicode key codes are same as key codes for the codes in 1..255 range
854 void AdjustCharEventKeyCodes(wxKeyEvent
& event
)
856 const int code
= event
.m_keyCode
;
858 // Check for (a) above.
859 if ( event
.ControlDown() )
861 // We intentionally don't use isupper/lower() here, we really need
862 // ASCII letters only as it doesn't make sense to translate any other
863 // ones into this range which has only 26 slots.
864 if ( code
>= 'a' && code
<= 'z' )
865 event
.m_keyCode
= code
- 'a' + 1;
866 else if ( code
>= 'A' && code
<= 'Z' )
867 event
.m_keyCode
= code
- 'A' + 1;
870 // Adjust the Unicode equivalent in the same way too.
871 if ( event
.m_keyCode
!= code
)
872 event
.m_uniChar
= event
.m_keyCode
;
873 #endif // wxUSE_UNICODE
877 // Check for (b) from above.
879 // FIXME: Should we do it for key codes up to 255?
880 if ( !event
.m_uniChar
&& code
< WXK_DELETE
)
881 event
.m_uniChar
= code
;
882 #endif // wxUSE_UNICODE
885 } // anonymous namespace
889 gtk_window_key_press_callback( GtkWidget
*WXUNUSED(widget
),
890 GdkEventKey
*gdk_event
,
895 if (g_blockEventsOnDrag
)
898 wxKeyEvent
event( wxEVT_KEY_DOWN
);
900 bool return_after_IM
= false;
902 if( wxTranslateGTKKeyEventToWx(event
, win
, gdk_event
) )
904 // Send the CHAR_HOOK event first
905 if ( SendCharHookEvent(event
, win
) )
907 // Don't do anything at all with this event any more.
911 // Emit KEY_DOWN event
912 ret
= win
->HandleWindowEvent( event
);
916 // Return after IM processing as we cannot do
917 // anything with it anyhow.
918 return_after_IM
= true;
921 if (!ret
&& win
->m_imData
)
923 win
->m_imData
->lastKeyEvent
= gdk_event
;
925 // We should let GTK+ IM filter key event first. According to GTK+ 2.0 API
926 // docs, if IM filter returns true, no further processing should be done.
927 // we should send the key_down event anyway.
928 bool intercepted_by_IM
= gtk_im_context_filter_keypress(win
->m_imData
->context
, gdk_event
);
929 win
->m_imData
->lastKeyEvent
= NULL
;
930 if (intercepted_by_IM
)
932 wxLogTrace(TRACE_KEYS
, wxT("Key event intercepted by IM"));
943 wxWindowGTK
*ancestor
= win
;
946 int command
= ancestor
->GetAcceleratorTable()->GetCommand( event
);
949 wxCommandEvent
menu_event( wxEVT_COMMAND_MENU_SELECTED
, command
);
950 ret
= ancestor
->HandleWindowEvent( menu_event
);
954 // if the accelerator wasn't handled as menu event, try
955 // it as button click (for compatibility with other
957 wxCommandEvent
button_event( wxEVT_COMMAND_BUTTON_CLICKED
, command
);
958 ret
= ancestor
->HandleWindowEvent( button_event
);
963 if (ancestor
->IsTopLevel())
965 ancestor
= ancestor
->GetParent();
968 #endif // wxUSE_ACCEL
970 // Only send wxEVT_CHAR event if not processed yet. Thus, ALT-x
971 // will only be sent if it is not in an accelerator table.
975 KeySym keysym
= gdk_event
->keyval
;
976 // Find key code for EVT_CHAR and EVT_CHAR_HOOK events
977 key_code
= wxTranslateKeySymToWXKey(keysym
, true /* isChar */);
980 if ( wxIsAsciiKeysym(keysym
) )
983 key_code
= (unsigned char)keysym
;
985 // gdk_event->string is actually deprecated
986 else if ( gdk_event
->length
== 1 )
988 key_code
= (unsigned char)gdk_event
->string
[0];
994 wxKeyEvent
eventChar(wxEVT_CHAR
, event
);
996 wxLogTrace(TRACE_KEYS
, wxT("Char event: %ld"), key_code
);
998 eventChar
.m_keyCode
= key_code
;
1000 AdjustCharEventKeyCodes(eventChar
);
1002 ret
= win
->HandleWindowEvent(eventChar
);
1012 gtk_wxwindow_commit_cb (GtkIMContext
* WXUNUSED(context
),
1016 wxKeyEvent
event( wxEVT_CHAR
);
1018 // take modifiers, cursor position, timestamp etc. from the last
1019 // key_press_event that was fed into Input Method:
1020 if (window
->m_imData
->lastKeyEvent
)
1022 wxFillOtherKeyEventFields(event
,
1023 window
, window
->m_imData
->lastKeyEvent
);
1027 event
.SetEventObject( window
);
1030 const wxString
data(wxGTK_CONV_BACK_SYS(str
));
1034 for( wxString::const_iterator pstr
= data
.begin(); pstr
!= data
.end(); ++pstr
)
1037 event
.m_uniChar
= *pstr
;
1038 // Backward compatible for ISO-8859-1
1039 event
.m_keyCode
= *pstr
< 256 ? event
.m_uniChar
: 0;
1040 wxLogTrace(TRACE_KEYS
, wxT("IM sent character '%c'"), event
.m_uniChar
);
1042 event
.m_keyCode
= (char)*pstr
;
1043 #endif // wxUSE_UNICODE
1045 AdjustCharEventKeyCodes(event
);
1047 window
->HandleWindowEvent(event
);
1053 //-----------------------------------------------------------------------------
1054 // "key_release_event" from any window
1055 //-----------------------------------------------------------------------------
1059 gtk_window_key_release_callback( GtkWidget
* WXUNUSED(widget
),
1060 GdkEventKey
*gdk_event
,
1066 if (g_blockEventsOnDrag
)
1069 wxKeyEvent
event( wxEVT_KEY_UP
);
1070 if ( !wxTranslateGTKKeyEventToWx(event
, win
, gdk_event
) )
1072 // unknown key pressed, ignore (the event would be useless anyhow)
1076 return win
->GTKProcessEvent(event
);
1080 //-----------------------------------------------------------------------------
1081 // key and mouse events, after, from m_widget
1082 //-----------------------------------------------------------------------------
1085 static gboolean
key_and_mouse_event_after(GtkWidget
* widget
, GdkEventKey
*, wxWindow
*)
1087 // If a widget does not handle a key or mouse event, GTK+ sends it up the
1088 // parent chain until it is handled. These events are not supposed to
1089 // propagate in wxWidgets, so prevent it unless widget is in a native
1091 return WX_IS_PIZZA(gtk_widget_get_parent(widget
));
1095 // ============================================================================
1097 // ============================================================================
1099 // ----------------------------------------------------------------------------
1100 // mouse event processing helpers
1101 // ----------------------------------------------------------------------------
1103 static void AdjustEventButtonState(wxMouseEvent
& event
)
1105 // GDK reports the old state of the button for a button press event, but
1106 // for compatibility with MSW and common sense we want m_leftDown be TRUE
1107 // for a LEFT_DOWN event, not FALSE, so we will invert
1108 // left/right/middleDown for the corresponding click events
1110 if ((event
.GetEventType() == wxEVT_LEFT_DOWN
) ||
1111 (event
.GetEventType() == wxEVT_LEFT_DCLICK
) ||
1112 (event
.GetEventType() == wxEVT_LEFT_UP
))
1114 event
.m_leftDown
= !event
.m_leftDown
;
1118 if ((event
.GetEventType() == wxEVT_MIDDLE_DOWN
) ||
1119 (event
.GetEventType() == wxEVT_MIDDLE_DCLICK
) ||
1120 (event
.GetEventType() == wxEVT_MIDDLE_UP
))
1122 event
.m_middleDown
= !event
.m_middleDown
;
1126 if ((event
.GetEventType() == wxEVT_RIGHT_DOWN
) ||
1127 (event
.GetEventType() == wxEVT_RIGHT_DCLICK
) ||
1128 (event
.GetEventType() == wxEVT_RIGHT_UP
))
1130 event
.m_rightDown
= !event
.m_rightDown
;
1134 if ((event
.GetEventType() == wxEVT_AUX1_DOWN
) ||
1135 (event
.GetEventType() == wxEVT_AUX1_DCLICK
))
1137 event
.m_aux1Down
= true;
1141 if ((event
.GetEventType() == wxEVT_AUX2_DOWN
) ||
1142 (event
.GetEventType() == wxEVT_AUX2_DCLICK
))
1144 event
.m_aux2Down
= true;
1149 // find the window to send the mouse event too
1151 wxWindowGTK
*FindWindowForMouseEvent(wxWindowGTK
*win
, wxCoord
& x
, wxCoord
& y
)
1156 if (win
->m_wxwindow
)
1158 wxPizza
* pizza
= WX_PIZZA(win
->m_wxwindow
);
1159 xx
+= pizza
->m_scroll_x
;
1160 yy
+= pizza
->m_scroll_y
;
1163 wxWindowList::compatibility_iterator node
= win
->GetChildren().GetFirst();
1166 wxWindowGTK
*child
= node
->GetData();
1168 node
= node
->GetNext();
1169 if (!child
->IsShown())
1172 if (child
->GTKIsTransparentForMouse())
1174 // wxStaticBox is transparent in the box itself
1175 int xx1
= child
->m_x
;
1176 int yy1
= child
->m_y
;
1177 int xx2
= child
->m_x
+ child
->m_width
;
1178 int yy2
= child
->m_y
+ child
->m_height
;
1181 if (((xx
>= xx1
) && (xx
<= xx1
+10) && (yy
>= yy1
) && (yy
<= yy2
)) ||
1183 ((xx
>= xx2
-10) && (xx
<= xx2
) && (yy
>= yy1
) && (yy
<= yy2
)) ||
1185 ((xx
>= xx1
) && (xx
<= xx2
) && (yy
>= yy1
) && (yy
<= yy1
+10)) ||
1187 ((xx
>= xx1
) && (xx
<= xx2
) && (yy
>= yy2
-1) && (yy
<= yy2
)))
1198 if ((child
->m_wxwindow
== NULL
) &&
1199 (child
->m_x
<= xx
) &&
1200 (child
->m_y
<= yy
) &&
1201 (child
->m_x
+child
->m_width
>= xx
) &&
1202 (child
->m_y
+child
->m_height
>= yy
))
1215 // ----------------------------------------------------------------------------
1216 // common event handlers helpers
1217 // ----------------------------------------------------------------------------
1219 bool wxWindowGTK::GTKProcessEvent(wxEvent
& event
) const
1221 // nothing special at this level
1222 return HandleWindowEvent(event
);
1225 bool wxWindowGTK::GTKShouldIgnoreEvent() const
1227 return !m_hasVMT
|| g_blockEventsOnDrag
;
1230 int wxWindowGTK::GTKCallbackCommonPrologue(GdkEventAny
*event
) const
1234 if (g_blockEventsOnDrag
)
1236 if (g_blockEventsOnScroll
)
1239 if (!GTKIsOwnWindow(event
->window
))
1245 // overloads for all GDK event types we use here: we need to have this as
1246 // GdkEventXXX can't be implicitly cast to GdkEventAny even if it, in fact,
1247 // derives from it in the sense that the structs have the same layout
1248 #define wxDEFINE_COMMON_PROLOGUE_OVERLOAD(T) \
1249 static int wxGtkCallbackCommonPrologue(T *event, wxWindowGTK *win) \
1251 return win->GTKCallbackCommonPrologue((GdkEventAny *)event); \
1254 wxDEFINE_COMMON_PROLOGUE_OVERLOAD(GdkEventButton
)
1255 wxDEFINE_COMMON_PROLOGUE_OVERLOAD(GdkEventMotion
)
1256 wxDEFINE_COMMON_PROLOGUE_OVERLOAD(GdkEventCrossing
)
1258 #undef wxDEFINE_COMMON_PROLOGUE_OVERLOAD
1260 #define wxCOMMON_CALLBACK_PROLOGUE(event, win) \
1261 const int rc = wxGtkCallbackCommonPrologue(event, win); \
1265 // all event handlers must have C linkage as they're called from GTK+ C code
1269 //-----------------------------------------------------------------------------
1270 // "button_press_event"
1271 //-----------------------------------------------------------------------------
1274 gtk_window_button_press_callback( GtkWidget
*widget
,
1275 GdkEventButton
*gdk_event
,
1278 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1280 g_lastButtonNumber
= gdk_event
->button
;
1282 // GDK sends surplus button down events
1283 // before a double click event. We
1284 // need to filter these out.
1285 if ((gdk_event
->type
== GDK_BUTTON_PRESS
) && (win
->m_wxwindow
))
1287 GdkEvent
*peek_event
= gdk_event_peek();
1290 if ((peek_event
->type
== GDK_2BUTTON_PRESS
) ||
1291 (peek_event
->type
== GDK_3BUTTON_PRESS
))
1293 gdk_event_free( peek_event
);
1298 gdk_event_free( peek_event
);
1303 wxEventType event_type
= wxEVT_NULL
;
1305 if ( gdk_event
->type
== GDK_2BUTTON_PRESS
&&
1306 gdk_event
->button
>= 1 && gdk_event
->button
<= 3 )
1308 // Reset GDK internal timestamp variables in order to disable GDK
1309 // triple click events. GDK will then next time believe no button has
1310 // been clicked just before, and send a normal button click event.
1311 GdkDisplay
* display
= gtk_widget_get_display (widget
);
1312 display
->button_click_time
[1] = 0;
1313 display
->button_click_time
[0] = 0;
1316 if (gdk_event
->button
== 1)
1318 // note that GDK generates triple click events which are not supported
1319 // by wxWidgets but still have to be passed to the app as otherwise
1320 // clicks would simply go missing
1321 switch (gdk_event
->type
)
1323 // we shouldn't get triple clicks at all for GTK2 because we
1324 // suppress them artificially using the code above but we still
1325 // should map them to something for GTK1 and not just ignore them
1326 // as this would lose clicks
1327 case GDK_3BUTTON_PRESS
: // we could also map this to DCLICK...
1328 case GDK_BUTTON_PRESS
:
1329 event_type
= wxEVT_LEFT_DOWN
;
1332 case GDK_2BUTTON_PRESS
:
1333 event_type
= wxEVT_LEFT_DCLICK
;
1337 // just to silence gcc warnings
1341 else if (gdk_event
->button
== 2)
1343 switch (gdk_event
->type
)
1345 case GDK_3BUTTON_PRESS
:
1346 case GDK_BUTTON_PRESS
:
1347 event_type
= wxEVT_MIDDLE_DOWN
;
1350 case GDK_2BUTTON_PRESS
:
1351 event_type
= wxEVT_MIDDLE_DCLICK
;
1358 else if (gdk_event
->button
== 3)
1360 switch (gdk_event
->type
)
1362 case GDK_3BUTTON_PRESS
:
1363 case GDK_BUTTON_PRESS
:
1364 event_type
= wxEVT_RIGHT_DOWN
;
1367 case GDK_2BUTTON_PRESS
:
1368 event_type
= wxEVT_RIGHT_DCLICK
;
1376 else if (gdk_event
->button
== 8)
1378 switch (gdk_event
->type
)
1380 case GDK_3BUTTON_PRESS
:
1381 case GDK_BUTTON_PRESS
:
1382 event_type
= wxEVT_AUX1_DOWN
;
1385 case GDK_2BUTTON_PRESS
:
1386 event_type
= wxEVT_AUX1_DCLICK
;
1394 else if (gdk_event
->button
== 9)
1396 switch (gdk_event
->type
)
1398 case GDK_3BUTTON_PRESS
:
1399 case GDK_BUTTON_PRESS
:
1400 event_type
= wxEVT_AUX2_DOWN
;
1403 case GDK_2BUTTON_PRESS
:
1404 event_type
= wxEVT_AUX2_DCLICK
;
1412 if ( event_type
== wxEVT_NULL
)
1414 // unknown mouse button or click type
1418 g_lastMouseEvent
= (GdkEvent
*) gdk_event
;
1420 wxMouseEvent
event( event_type
);
1421 InitMouseEvent( win
, event
, gdk_event
);
1423 AdjustEventButtonState(event
);
1425 // find the correct window to send the event to: it may be a different one
1426 // from the one which got it at GTK+ level because some controls don't have
1427 // their own X window and thus cannot get any events.
1428 if ( !g_captureWindow
)
1429 win
= FindWindowForMouseEvent(win
, event
.m_x
, event
.m_y
);
1431 // reset the event object and id in case win changed.
1432 event
.SetEventObject( win
);
1433 event
.SetId( win
->GetId() );
1435 bool ret
= win
->GTKProcessEvent( event
);
1436 g_lastMouseEvent
= NULL
;
1440 if ((event_type
== wxEVT_LEFT_DOWN
) && !win
->IsOfStandardClass() &&
1441 (gs_currentFocus
!= win
) /* && win->IsFocusable() */)
1446 if (event_type
== wxEVT_RIGHT_DOWN
)
1448 // generate a "context menu" event: this is similar to right mouse
1449 // click under many GUIs except that it is generated differently
1450 // (right up under MSW, ctrl-click under Mac, right down here) and
1452 // (a) it's a command event and so is propagated to the parent
1453 // (b) under some ports it can be generated from kbd too
1454 // (c) it uses screen coords (because of (a))
1455 wxContextMenuEvent
evtCtx(
1458 win
->ClientToScreen(event
.GetPosition()));
1459 evtCtx
.SetEventObject(win
);
1460 return win
->GTKProcessEvent(evtCtx
);
1466 //-----------------------------------------------------------------------------
1467 // "button_release_event"
1468 //-----------------------------------------------------------------------------
1471 gtk_window_button_release_callback( GtkWidget
*WXUNUSED(widget
),
1472 GdkEventButton
*gdk_event
,
1475 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1477 g_lastButtonNumber
= 0;
1479 wxEventType event_type
= wxEVT_NULL
;
1481 switch (gdk_event
->button
)
1484 event_type
= wxEVT_LEFT_UP
;
1488 event_type
= wxEVT_MIDDLE_UP
;
1492 event_type
= wxEVT_RIGHT_UP
;
1496 event_type
= wxEVT_AUX1_UP
;
1500 event_type
= wxEVT_AUX2_UP
;
1504 // unknown button, don't process
1508 g_lastMouseEvent
= (GdkEvent
*) gdk_event
;
1510 wxMouseEvent
event( event_type
);
1511 InitMouseEvent( win
, event
, gdk_event
);
1513 AdjustEventButtonState(event
);
1515 if ( !g_captureWindow
)
1516 win
= FindWindowForMouseEvent(win
, event
.m_x
, event
.m_y
);
1518 // reset the event object and id in case win changed.
1519 event
.SetEventObject( win
);
1520 event
.SetId( win
->GetId() );
1522 bool ret
= win
->GTKProcessEvent(event
);
1524 g_lastMouseEvent
= NULL
;
1529 //-----------------------------------------------------------------------------
1530 // "motion_notify_event"
1531 //-----------------------------------------------------------------------------
1534 gtk_window_motion_notify_callback( GtkWidget
* WXUNUSED(widget
),
1535 GdkEventMotion
*gdk_event
,
1538 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1540 if (gdk_event
->is_hint
)
1544 GdkModifierType state
;
1545 gdk_window_get_pointer(gdk_event
->window
, &x
, &y
, &state
);
1550 g_lastMouseEvent
= (GdkEvent
*) gdk_event
;
1552 wxMouseEvent
event( wxEVT_MOTION
);
1553 InitMouseEvent(win
, event
, gdk_event
);
1555 if ( g_captureWindow
)
1557 // synthesise a mouse enter or leave event if needed
1558 GdkWindow
*winUnderMouse
= gdk_window_at_pointer(NULL
, NULL
);
1559 // This seems to be necessary and actually been added to
1560 // GDK itself in version 2.0.X
1563 bool hasMouse
= winUnderMouse
== gdk_event
->window
;
1564 if ( hasMouse
!= g_captureWindowHasMouse
)
1566 // the mouse changed window
1567 g_captureWindowHasMouse
= hasMouse
;
1569 wxMouseEvent
eventM(g_captureWindowHasMouse
? wxEVT_ENTER_WINDOW
1570 : wxEVT_LEAVE_WINDOW
);
1571 InitMouseEvent(win
, eventM
, gdk_event
);
1572 eventM
.SetEventObject(win
);
1573 win
->GTKProcessEvent(eventM
);
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() );
1585 if ( !g_captureWindow
)
1587 wxSetCursorEvent
cevent( event
.m_x
, event
.m_y
);
1588 if (win
->GTKProcessEvent( cevent
))
1590 win
->SetCursor( cevent
.GetCursor() );
1594 bool ret
= win
->GTKProcessEvent(event
);
1596 g_lastMouseEvent
= NULL
;
1601 //-----------------------------------------------------------------------------
1602 // "scroll_event" (mouse wheel event)
1603 //-----------------------------------------------------------------------------
1606 window_scroll_event_hscrollbar(GtkWidget
*, GdkEventScroll
* gdk_event
, wxWindow
* win
)
1608 if (gdk_event
->direction
!= GDK_SCROLL_LEFT
&&
1609 gdk_event
->direction
!= GDK_SCROLL_RIGHT
)
1614 GtkRange
*range
= win
->m_scrollBar
[wxWindow::ScrollDir_Horz
];
1616 if (range
&& gtk_widget_get_visible(GTK_WIDGET(range
)))
1618 GtkAdjustment
* adj
= gtk_range_get_adjustment(range
);
1619 double delta
= gtk_adjustment_get_step_increment(adj
) * 3;
1620 if (gdk_event
->direction
== GDK_SCROLL_LEFT
)
1623 gtk_range_set_value(range
, gtk_adjustment_get_value(adj
) + delta
);
1632 window_scroll_event(GtkWidget
*, GdkEventScroll
* gdk_event
, wxWindow
* win
)
1634 if (gdk_event
->direction
!= GDK_SCROLL_UP
&&
1635 gdk_event
->direction
!= GDK_SCROLL_DOWN
)
1640 wxMouseEvent
event(wxEVT_MOUSEWHEEL
);
1641 InitMouseEvent(win
, event
, gdk_event
);
1643 // FIXME: Get these values from GTK or GDK
1644 event
.m_linesPerAction
= 3;
1645 event
.m_wheelDelta
= 120;
1646 if (gdk_event
->direction
== GDK_SCROLL_UP
)
1647 event
.m_wheelRotation
= 120;
1649 event
.m_wheelRotation
= -120;
1651 if (win
->GTKProcessEvent(event
))
1654 GtkRange
*range
= win
->m_scrollBar
[wxWindow::ScrollDir_Vert
];
1656 if (range
&& gtk_widget_get_visible(GTK_WIDGET(range
)))
1658 GtkAdjustment
* adj
= gtk_range_get_adjustment(range
);
1659 double delta
= gtk_adjustment_get_step_increment(adj
) * 3;
1660 if (gdk_event
->direction
== GDK_SCROLL_UP
)
1663 gtk_range_set_value(range
, gtk_adjustment_get_value(adj
) + delta
);
1671 //-----------------------------------------------------------------------------
1673 //-----------------------------------------------------------------------------
1675 static gboolean
wxgtk_window_popup_menu_callback(GtkWidget
*, wxWindowGTK
* win
)
1677 wxContextMenuEvent
event(wxEVT_CONTEXT_MENU
, win
->GetId(), wxPoint(-1, -1));
1678 event
.SetEventObject(win
);
1679 return win
->GTKProcessEvent(event
);
1682 //-----------------------------------------------------------------------------
1684 //-----------------------------------------------------------------------------
1687 gtk_window_focus_in_callback( GtkWidget
* WXUNUSED(widget
),
1688 GdkEventFocus
*WXUNUSED(event
),
1691 return win
->GTKHandleFocusIn();
1694 //-----------------------------------------------------------------------------
1695 // "focus_out_event"
1696 //-----------------------------------------------------------------------------
1699 gtk_window_focus_out_callback( GtkWidget
* WXUNUSED(widget
),
1700 GdkEventFocus
* WXUNUSED(gdk_event
),
1703 return win
->GTKHandleFocusOut();
1706 //-----------------------------------------------------------------------------
1708 //-----------------------------------------------------------------------------
1711 wx_window_focus_callback(GtkWidget
*widget
,
1712 GtkDirectionType
WXUNUSED(direction
),
1715 // the default handler for focus signal in GtkScrolledWindow sets
1716 // focus to the window itself even if it doesn't accept focus, i.e. has no
1717 // GTK_CAN_FOCUS in its style -- work around this by forcibly preventing
1718 // the signal from reaching gtk_scrolled_window_focus() if we don't have
1719 // any children which might accept focus (we know we don't accept the focus
1720 // ourselves as this signal is only connected in this case)
1721 if ( win
->GetChildren().empty() )
1722 g_signal_stop_emission_by_name(widget
, "focus");
1724 // we didn't change the focus
1728 //-----------------------------------------------------------------------------
1729 // "enter_notify_event"
1730 //-----------------------------------------------------------------------------
1733 gtk_window_enter_callback( GtkWidget
*widget
,
1734 GdkEventCrossing
*gdk_event
,
1737 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1739 // Event was emitted after a grab
1740 if (gdk_event
->mode
!= GDK_CROSSING_NORMAL
) return FALSE
;
1744 GdkModifierType state
= (GdkModifierType
)0;
1746 gdk_window_get_pointer(gtk_widget_get_window(widget
), &x
, &y
, &state
);
1748 wxMouseEvent
event( wxEVT_ENTER_WINDOW
);
1749 InitMouseEvent(win
, event
, gdk_event
);
1750 wxPoint pt
= win
->GetClientAreaOrigin();
1751 event
.m_x
= x
+ pt
.x
;
1752 event
.m_y
= y
+ pt
.y
;
1754 if ( !g_captureWindow
)
1756 wxSetCursorEvent
cevent( event
.m_x
, event
.m_y
);
1757 if (win
->GTKProcessEvent( cevent
))
1759 win
->SetCursor( cevent
.GetCursor() );
1763 return win
->GTKProcessEvent(event
);
1766 //-----------------------------------------------------------------------------
1767 // "leave_notify_event"
1768 //-----------------------------------------------------------------------------
1771 gtk_window_leave_callback( GtkWidget
*widget
,
1772 GdkEventCrossing
*gdk_event
,
1775 wxCOMMON_CALLBACK_PROLOGUE(gdk_event
, win
);
1777 // Event was emitted after an ungrab
1778 if (gdk_event
->mode
!= GDK_CROSSING_NORMAL
) return FALSE
;
1780 wxMouseEvent
event( wxEVT_LEAVE_WINDOW
);
1784 GdkModifierType state
= (GdkModifierType
)0;
1786 gdk_window_get_pointer(gtk_widget_get_window(widget
), &x
, &y
, &state
);
1788 InitMouseEvent(win
, event
, gdk_event
);
1790 return win
->GTKProcessEvent(event
);
1793 //-----------------------------------------------------------------------------
1794 // "value_changed" from scrollbar
1795 //-----------------------------------------------------------------------------
1798 gtk_scrollbar_value_changed(GtkRange
* range
, wxWindow
* win
)
1800 wxEventType eventType
= win
->GTKGetScrollEventType(range
);
1801 if (eventType
!= wxEVT_NULL
)
1803 // Convert scroll event type to scrollwin event type
1804 eventType
+= wxEVT_SCROLLWIN_TOP
- wxEVT_SCROLL_TOP
;
1806 // find the scrollbar which generated the event
1807 wxWindowGTK::ScrollDir dir
= win
->ScrollDirFromRange(range
);
1809 // generate the corresponding wx event
1810 const int orient
= wxWindow::OrientFromScrollDir(dir
);
1811 wxScrollWinEvent
event(eventType
, win
->GetScrollPos(orient
), orient
);
1812 event
.SetEventObject(win
);
1814 win
->GTKProcessEvent(event
);
1818 //-----------------------------------------------------------------------------
1819 // "button_press_event" from scrollbar
1820 //-----------------------------------------------------------------------------
1823 gtk_scrollbar_button_press_event(GtkRange
*, GdkEventButton
*, wxWindow
* win
)
1825 g_blockEventsOnScroll
= true;
1826 win
->m_mouseButtonDown
= true;
1831 //-----------------------------------------------------------------------------
1832 // "event_after" from scrollbar
1833 //-----------------------------------------------------------------------------
1836 gtk_scrollbar_event_after(GtkRange
* range
, GdkEvent
* event
, wxWindow
* win
)
1838 if (event
->type
== GDK_BUTTON_RELEASE
)
1840 g_signal_handlers_block_by_func(range
, (void*)gtk_scrollbar_event_after
, win
);
1842 const int orient
= wxWindow::OrientFromScrollDir(
1843 win
->ScrollDirFromRange(range
));
1844 wxScrollWinEvent
evt(wxEVT_SCROLLWIN_THUMBRELEASE
,
1845 win
->GetScrollPos(orient
), orient
);
1846 evt
.SetEventObject(win
);
1847 win
->GTKProcessEvent(evt
);
1851 //-----------------------------------------------------------------------------
1852 // "button_release_event" from scrollbar
1853 //-----------------------------------------------------------------------------
1856 gtk_scrollbar_button_release_event(GtkRange
* range
, GdkEventButton
*, wxWindow
* win
)
1858 g_blockEventsOnScroll
= false;
1859 win
->m_mouseButtonDown
= false;
1860 // If thumb tracking
1861 if (win
->m_isScrolling
)
1863 win
->m_isScrolling
= false;
1864 // Hook up handler to send thumb release event after this emission is finished.
1865 // To allow setting scroll position from event handler, sending event must
1866 // be deferred until after the GtkRange handler for this signal has run
1867 g_signal_handlers_unblock_by_func(range
, (void*)gtk_scrollbar_event_after
, win
);
1873 //-----------------------------------------------------------------------------
1874 // "realize" from m_widget
1875 //-----------------------------------------------------------------------------
1878 gtk_window_realized_callback(GtkWidget
* WXUNUSED(widget
), wxWindowGTK
* win
)
1880 win
->GTKHandleRealized();
1883 //-----------------------------------------------------------------------------
1884 // "unrealize" from m_wxwindow
1885 //-----------------------------------------------------------------------------
1887 static void unrealize(GtkWidget
*, wxWindowGTK
* win
)
1890 gtk_im_context_set_client_window(win
->m_imData
->context
, NULL
);
1893 //-----------------------------------------------------------------------------
1894 // "size_allocate" from m_wxwindow or m_widget
1895 //-----------------------------------------------------------------------------
1898 size_allocate(GtkWidget
*, GtkAllocation
* alloc
, wxWindow
* win
)
1900 int w
= alloc
->width
;
1901 int h
= alloc
->height
;
1902 if (win
->m_wxwindow
)
1904 int border_x
, border_y
;
1905 WX_PIZZA(win
->m_wxwindow
)->get_border_widths(border_x
, border_y
);
1911 if (win
->m_oldClientWidth
!= w
|| win
->m_oldClientHeight
!= h
)
1913 win
->m_oldClientWidth
= w
;
1914 win
->m_oldClientHeight
= h
;
1915 // this callback can be connected to m_wxwindow,
1916 // so always get size from m_widget->allocation
1918 gtk_widget_get_allocation(win
->m_widget
, &a
);
1919 win
->m_width
= a
.width
;
1920 win
->m_height
= a
.height
;
1921 if (!win
->m_nativeSizeEvent
)
1923 wxSizeEvent
event(win
->GetSize(), win
->GetId());
1924 event
.SetEventObject(win
);
1925 win
->GTKProcessEvent(event
);
1930 //-----------------------------------------------------------------------------
1932 //-----------------------------------------------------------------------------
1934 #if GTK_CHECK_VERSION(2, 8, 0)
1936 gtk_window_grab_broken( GtkWidget
*,
1937 GdkEventGrabBroken
*event
,
1940 // Mouse capture has been lost involuntarily, notify the application
1941 if(!event
->keyboard
&& wxWindow::GetCapture() == win
)
1943 wxMouseCaptureLostEvent
evt( win
->GetId() );
1944 evt
.SetEventObject( win
);
1945 win
->HandleWindowEvent( evt
);
1951 //-----------------------------------------------------------------------------
1953 //-----------------------------------------------------------------------------
1956 void gtk_window_style_set_callback( GtkWidget
*WXUNUSED(widget
),
1957 GtkStyle
*previous_style
,
1960 if (win
&& previous_style
)
1962 if (win
->IsTopLevel())
1964 wxSysColourChangedEvent event
;
1965 event
.SetEventObject(win
);
1966 win
->GTKProcessEvent(event
);
1970 // Border width could change, which will change client size.
1971 // Make sure size event occurs for this
1972 win
->m_oldClientWidth
= 0;
1979 void wxWindowGTK::GTKHandleRealized()
1983 gtk_im_context_set_client_window
1986 m_wxwindow
? GTKGetDrawingWindow()
1987 : gtk_widget_get_window(m_widget
)
1991 // Use composited window if background is transparent, if supported.
1992 if (m_backgroundStyle
== wxBG_STYLE_TRANSPARENT
)
1994 #if wxGTK_HAS_COMPOSITING_SUPPORT
1995 if (IsTransparentBackgroundSupported())
1997 GdkWindow
* const window
= GTKGetDrawingWindow();
1999 gdk_window_set_composited(window
, true);
2002 #endif // wxGTK_HAS_COMPOSITING_SUPPORT
2004 // We revert to erase mode if transparency is not supported
2005 m_backgroundStyle
= wxBG_STYLE_ERASE
;
2010 // We cannot set colours and fonts before the widget
2011 // been realized, so we do this directly after realization
2012 // or otherwise in idle time
2014 if (m_needsStyleChange
)
2016 SetBackgroundStyle(GetBackgroundStyle());
2017 m_needsStyleChange
= false;
2020 wxWindowCreateEvent
event(static_cast<wxWindow
*>(this));
2021 event
.SetEventObject( this );
2022 GTKProcessEvent( event
);
2024 GTKUpdateCursor(true, false);
2027 // ----------------------------------------------------------------------------
2028 // this wxWindowBase function is implemented here (in platform-specific file)
2029 // because it is static and so couldn't be made virtual
2030 // ----------------------------------------------------------------------------
2032 wxWindow
*wxWindowBase::DoFindFocus()
2034 wxWindowGTK
*focus
= gs_pendingFocus
? gs_pendingFocus
: gs_currentFocus
;
2035 // the cast is necessary when we compile in wxUniversal mode
2036 return static_cast<wxWindow
*>(focus
);
2039 void wxWindowGTK::AddChildGTK(wxWindowGTK
* child
)
2041 wxASSERT_MSG(m_wxwindow
, "Cannot add a child to a window without a client area");
2043 // the window might have been scrolled already, we
2044 // have to adapt the position
2045 wxPizza
* pizza
= WX_PIZZA(m_wxwindow
);
2046 child
->m_x
+= pizza
->m_scroll_x
;
2047 child
->m_y
+= pizza
->m_scroll_y
;
2049 gtk_widget_set_size_request(
2050 child
->m_widget
, child
->m_width
, child
->m_height
);
2051 pizza
->put(child
->m_widget
, child
->m_x
, child
->m_y
);
2054 //-----------------------------------------------------------------------------
2056 //-----------------------------------------------------------------------------
2058 wxWindow
*wxGetActiveWindow()
2060 return wxWindow::FindFocus();
2064 wxMouseState
wxGetMouseState()
2070 GdkModifierType mask
;
2072 gdk_window_get_pointer(NULL
, &x
, &y
, &mask
);
2076 ms
.SetLeftDown((mask
& GDK_BUTTON1_MASK
) != 0);
2077 ms
.SetMiddleDown((mask
& GDK_BUTTON2_MASK
) != 0);
2078 ms
.SetRightDown((mask
& GDK_BUTTON3_MASK
) != 0);
2079 // see the comment in InitMouseEvent()
2080 ms
.SetAux1Down((mask
& GDK_BUTTON4_MASK
) != 0);
2081 ms
.SetAux2Down((mask
& GDK_BUTTON5_MASK
) != 0);
2083 ms
.SetControlDown((mask
& GDK_CONTROL_MASK
) != 0);
2084 ms
.SetShiftDown((mask
& GDK_SHIFT_MASK
) != 0);
2085 ms
.SetAltDown((mask
& GDK_MOD1_MASK
) != 0);
2086 ms
.SetMetaDown((mask
& GDK_META_MASK
) != 0);
2091 //-----------------------------------------------------------------------------
2093 //-----------------------------------------------------------------------------
2095 // in wxUniv/MSW this class is abstract because it doesn't have DoPopupMenu()
2097 #ifdef __WXUNIVERSAL__
2098 IMPLEMENT_ABSTRACT_CLASS(wxWindowGTK
, wxWindowBase
)
2099 #endif // __WXUNIVERSAL__
2101 void wxWindowGTK::Init()
2106 m_focusWidget
= NULL
;
2116 m_showOnIdle
= false;
2119 m_nativeSizeEvent
= false;
2121 m_isScrolling
= false;
2122 m_mouseButtonDown
= false;
2124 // initialize scrolling stuff
2125 for ( int dir
= 0; dir
< ScrollDir_Max
; dir
++ )
2127 m_scrollBar
[dir
] = NULL
;
2128 m_scrollPos
[dir
] = 0;
2132 m_oldClientHeight
= 0;
2134 m_clipPaintRegion
= false;
2136 m_needsStyleChange
= false;
2138 m_cursor
= *wxSTANDARD_CURSOR
;
2141 m_dirtyTabOrder
= false;
2144 wxWindowGTK::wxWindowGTK()
2149 wxWindowGTK::wxWindowGTK( wxWindow
*parent
,
2154 const wxString
&name
)
2158 Create( parent
, id
, pos
, size
, style
, name
);
2161 bool wxWindowGTK::Create( wxWindow
*parent
,
2166 const wxString
&name
)
2168 // Get default border
2169 wxBorder border
= GetBorder(style
);
2171 style
&= ~wxBORDER_MASK
;
2174 if (!PreCreation( parent
, pos
, size
) ||
2175 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
2177 wxFAIL_MSG( wxT("wxWindowGTK creation failed") );
2181 // We should accept the native look
2183 GtkScrolledWindowClass
*scroll_class
= GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) );
2184 scroll_class
->scrollbar_spacing
= 0;
2188 m_wxwindow
= wxPizza::New(m_windowStyle
);
2189 #ifndef __WXUNIVERSAL__
2190 if (HasFlag(wxPizza::BORDER_STYLES
))
2192 g_signal_connect(m_wxwindow
, "parent_set",
2193 G_CALLBACK(parent_set
), this);
2196 if (!HasFlag(wxHSCROLL
) && !HasFlag(wxVSCROLL
))
2197 m_widget
= m_wxwindow
;
2200 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
2202 GtkScrolledWindow
*scrolledWindow
= GTK_SCROLLED_WINDOW(m_widget
);
2204 // There is a conflict with default bindings at GTK+
2205 // level between scrolled windows and notebooks both of which want to use
2206 // Ctrl-PageUp/Down: scrolled windows for scrolling in the horizontal
2207 // direction and notebooks for changing pages -- we decide that if we don't
2208 // have wxHSCROLL style we can safely sacrifice horizontal scrolling if it
2209 // means we can get working keyboard navigation in notebooks
2210 if ( !HasFlag(wxHSCROLL
) )
2213 bindings
= gtk_binding_set_by_class(G_OBJECT_GET_CLASS(m_widget
));
2216 gtk_binding_entry_remove(bindings
, GDK_Page_Up
, GDK_CONTROL_MASK
);
2217 gtk_binding_entry_remove(bindings
, GDK_Page_Down
, GDK_CONTROL_MASK
);
2221 if (HasFlag(wxALWAYS_SHOW_SB
))
2223 gtk_scrolled_window_set_policy( scrolledWindow
, GTK_POLICY_ALWAYS
, GTK_POLICY_ALWAYS
);
2227 gtk_scrolled_window_set_policy( scrolledWindow
, GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
2230 m_scrollBar
[ScrollDir_Horz
] = GTK_RANGE(gtk_scrolled_window_get_hscrollbar(scrolledWindow
));
2231 m_scrollBar
[ScrollDir_Vert
] = GTK_RANGE(gtk_scrolled_window_get_vscrollbar(scrolledWindow
));
2232 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2233 gtk_range_set_inverted( m_scrollBar
[ScrollDir_Horz
], TRUE
);
2235 gtk_container_add( GTK_CONTAINER(m_widget
), m_wxwindow
);
2237 // connect various scroll-related events
2238 for ( int dir
= 0; dir
< ScrollDir_Max
; dir
++ )
2240 // these handlers block mouse events to any window during scrolling
2241 // such as motion events and prevent GTK and wxWidgets from fighting
2242 // over where the slider should be
2243 g_signal_connect(m_scrollBar
[dir
], "button_press_event",
2244 G_CALLBACK(gtk_scrollbar_button_press_event
), this);
2245 g_signal_connect(m_scrollBar
[dir
], "button_release_event",
2246 G_CALLBACK(gtk_scrollbar_button_release_event
), this);
2248 gulong handler_id
= g_signal_connect(m_scrollBar
[dir
], "event_after",
2249 G_CALLBACK(gtk_scrollbar_event_after
), this);
2250 g_signal_handler_block(m_scrollBar
[dir
], handler_id
);
2252 // these handlers get notified when scrollbar slider moves
2253 g_signal_connect_after(m_scrollBar
[dir
], "value_changed",
2254 G_CALLBACK(gtk_scrollbar_value_changed
), this);
2257 gtk_widget_show( m_wxwindow
);
2259 g_object_ref(m_widget
);
2262 m_parent
->DoAddChild( this );
2264 m_focusWidget
= m_wxwindow
;
2266 SetCanFocus(AcceptsFocus());
2273 wxWindowGTK::~wxWindowGTK()
2277 if (gs_currentFocus
== this)
2278 gs_currentFocus
= NULL
;
2279 if (gs_pendingFocus
== this)
2280 gs_pendingFocus
= NULL
;
2282 if ( gs_deferredFocusOut
== this )
2283 gs_deferredFocusOut
= NULL
;
2287 // destroy children before destroying this window itself
2290 // unhook focus handlers to prevent stray events being
2291 // propagated to this (soon to be) dead object
2292 if (m_focusWidget
!= NULL
)
2294 g_signal_handlers_disconnect_by_func (m_focusWidget
,
2295 (gpointer
) gtk_window_focus_in_callback
,
2297 g_signal_handlers_disconnect_by_func (m_focusWidget
,
2298 (gpointer
) gtk_window_focus_out_callback
,
2305 // delete before the widgets to avoid a crash on solaris
2309 // avoid problem with GTK+ 2.18 where a frozen window causes the whole
2310 // TLW to be frozen, and if the window is then destroyed, nothing ever
2311 // gets painted again
2317 // Note that gtk_widget_destroy() does not destroy the widget, it just
2318 // emits the "destroy" signal. The widget is not actually destroyed
2319 // until its reference count drops to zero.
2320 gtk_widget_destroy(m_widget
);
2321 // Release our reference, should be the last one
2322 g_object_unref(m_widget
);
2328 bool wxWindowGTK::PreCreation( wxWindowGTK
*parent
, const wxPoint
&pos
, const wxSize
&size
)
2330 if ( GTKNeedsParent() )
2332 wxCHECK_MSG( parent
, false, wxT("Must have non-NULL parent") );
2335 // Use either the given size, or the default if -1 is given.
2336 // See wxWindowBase for these functions.
2337 m_width
= WidthDefault(size
.x
) ;
2338 m_height
= HeightDefault(size
.y
);
2346 void wxWindowGTK::PostCreation()
2348 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid window") );
2350 #if wxGTK_HAS_COMPOSITING_SUPPORT
2351 // Set RGBA visual as soon as possible to minimize the possibility that
2352 // somebody uses the wrong one.
2353 if ( m_backgroundStyle
== wxBG_STYLE_TRANSPARENT
&&
2354 IsTransparentBackgroundSupported() )
2356 GdkScreen
*screen
= gtk_widget_get_screen (m_widget
);
2358 GdkColormap
*rgba_colormap
= gdk_screen_get_rgba_colormap (screen
);
2361 gtk_widget_set_colormap(m_widget
, rgba_colormap
);
2363 #endif // wxGTK_HAS_COMPOSITING_SUPPORT
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);
2386 g_signal_connect(m_wxwindow
, "unrealize", G_CALLBACK(unrealize
), this);
2391 if (!GTK_IS_WINDOW(m_widget
))
2393 if (m_focusWidget
== NULL
)
2394 m_focusWidget
= m_widget
;
2398 g_signal_connect (m_focusWidget
, "focus_in_event",
2399 G_CALLBACK (gtk_window_focus_in_callback
), this);
2400 g_signal_connect (m_focusWidget
, "focus_out_event",
2401 G_CALLBACK (gtk_window_focus_out_callback
), this);
2405 g_signal_connect_after (m_focusWidget
, "focus_in_event",
2406 G_CALLBACK (gtk_window_focus_in_callback
), this);
2407 g_signal_connect_after (m_focusWidget
, "focus_out_event",
2408 G_CALLBACK (gtk_window_focus_out_callback
), this);
2412 if ( !AcceptsFocusFromKeyboard() )
2416 g_signal_connect(m_widget
, "focus",
2417 G_CALLBACK(wx_window_focus_callback
), this);
2420 // connect to the various key and mouse handlers
2422 GtkWidget
*connect_widget
= GetConnectWidget();
2424 ConnectWidget( connect_widget
);
2426 // connect handler to prevent events from propagating up parent chain
2427 g_signal_connect_after(m_widget
,
2428 "key_press_event", G_CALLBACK(key_and_mouse_event_after
), this);
2429 g_signal_connect_after(m_widget
,
2430 "key_release_event", G_CALLBACK(key_and_mouse_event_after
), this);
2431 g_signal_connect_after(m_widget
,
2432 "button_press_event", G_CALLBACK(key_and_mouse_event_after
), this);
2433 g_signal_connect_after(m_widget
,
2434 "button_release_event", G_CALLBACK(key_and_mouse_event_after
), this);
2435 g_signal_connect_after(m_widget
,
2436 "motion_notify_event", G_CALLBACK(key_and_mouse_event_after
), this);
2438 // We cannot set colours, fonts and cursors before the widget has been
2439 // realized, so we do this directly after realization -- unless the widget
2440 // was in fact realized already.
2441 if ( gtk_widget_get_realized(connect_widget
) )
2443 gtk_window_realized_callback(connect_widget
, this);
2447 g_signal_connect (connect_widget
, "realize",
2448 G_CALLBACK (gtk_window_realized_callback
), this);
2453 g_signal_connect(m_wxwindow
? m_wxwindow
: m_widget
, "size_allocate",
2454 G_CALLBACK(size_allocate
), this);
2457 #if GTK_CHECK_VERSION(2, 8, 0)
2458 if ( gtk_check_version(2,8,0) == NULL
)
2460 // Make sure we can notify the app when mouse capture is lost
2463 g_signal_connect (m_wxwindow
, "grab_broken_event",
2464 G_CALLBACK (gtk_window_grab_broken
), this);
2467 if ( connect_widget
!= m_wxwindow
)
2469 g_signal_connect (connect_widget
, "grab_broken_event",
2470 G_CALLBACK (gtk_window_grab_broken
), this);
2473 #endif // GTK+ >= 2.8
2475 if ( GTKShouldConnectSizeRequest() )
2477 // This is needed if we want to add our windows into native
2478 // GTK controls, such as the toolbar. With this callback, the
2479 // toolbar gets to know the correct size (the one set by the
2480 // programmer). Sadly, it misbehaves for wxComboBox.
2481 g_signal_connect (m_widget
, "size_request",
2482 G_CALLBACK (wxgtk_window_size_request_callback
),
2486 InheritAttributes();
2490 SetLayoutDirection(wxLayout_Default
);
2492 // unless the window was created initially hidden (i.e. Hide() had been
2493 // called before Create()), we should show it at GTK+ level as well
2495 gtk_widget_show( m_widget
);
2499 wxWindowGTK::GTKConnectWidget(const char *signal
, wxGTKCallback callback
)
2501 return g_signal_connect(m_widget
, signal
, callback
, this);
2504 void wxWindowGTK::ConnectWidget( GtkWidget
*widget
)
2506 g_signal_connect (widget
, "key_press_event",
2507 G_CALLBACK (gtk_window_key_press_callback
), this);
2508 g_signal_connect (widget
, "key_release_event",
2509 G_CALLBACK (gtk_window_key_release_callback
), this);
2510 g_signal_connect (widget
, "button_press_event",
2511 G_CALLBACK (gtk_window_button_press_callback
), this);
2512 g_signal_connect (widget
, "button_release_event",
2513 G_CALLBACK (gtk_window_button_release_callback
), this);
2514 g_signal_connect (widget
, "motion_notify_event",
2515 G_CALLBACK (gtk_window_motion_notify_callback
), this);
2517 g_signal_connect (widget
, "scroll_event",
2518 G_CALLBACK (window_scroll_event
), this);
2519 if (m_scrollBar
[ScrollDir_Horz
])
2520 g_signal_connect (m_scrollBar
[ScrollDir_Horz
], "scroll_event",
2521 G_CALLBACK (window_scroll_event_hscrollbar
), this);
2522 if (m_scrollBar
[ScrollDir_Vert
])
2523 g_signal_connect (m_scrollBar
[ScrollDir_Vert
], "scroll_event",
2524 G_CALLBACK (window_scroll_event
), this);
2526 g_signal_connect (widget
, "popup_menu",
2527 G_CALLBACK (wxgtk_window_popup_menu_callback
), this);
2528 g_signal_connect (widget
, "enter_notify_event",
2529 G_CALLBACK (gtk_window_enter_callback
), this);
2530 g_signal_connect (widget
, "leave_notify_event",
2531 G_CALLBACK (gtk_window_leave_callback
), this);
2533 if (m_wxwindow
&& (IsTopLevel() || HasFlag(wxBORDER_RAISED
| wxBORDER_SUNKEN
| wxBORDER_THEME
)))
2534 g_signal_connect (m_wxwindow
, "style_set",
2535 G_CALLBACK (gtk_window_style_set_callback
), this);
2538 bool wxWindowGTK::Destroy()
2542 return wxWindowBase::Destroy();
2545 void wxWindowGTK::DoMoveWindow(int x
, int y
, int width
, int height
)
2547 gtk_widget_set_size_request(m_widget
, width
, height
);
2549 // inform the parent to perform the move
2550 wxASSERT_MSG(m_parent
&& m_parent
->m_wxwindow
,
2551 "the parent window has no client area?");
2552 WX_PIZZA(m_parent
->m_wxwindow
)->move(m_widget
, x
, y
);
2555 void wxWindowGTK::ConstrainSize()
2558 // GPE's window manager doesn't like size hints at all, esp. when the user
2559 // has to use the virtual keyboard, so don't constrain size there
2563 const wxSize minSize
= GetMinSize();
2564 const wxSize maxSize
= GetMaxSize();
2565 if (minSize
.x
> 0 && m_width
< minSize
.x
) m_width
= minSize
.x
;
2566 if (minSize
.y
> 0 && m_height
< minSize
.y
) m_height
= minSize
.y
;
2567 if (maxSize
.x
> 0 && m_width
> maxSize
.x
) m_width
= maxSize
.x
;
2568 if (maxSize
.y
> 0 && m_height
> maxSize
.y
) m_height
= maxSize
.y
;
2572 void wxWindowGTK::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
2574 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid window") );
2575 wxASSERT_MSG( (m_parent
!= NULL
), wxT("wxWindowGTK::SetSize requires parent.\n") );
2577 if ((sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) == 0 && (x
== -1 || y
== -1))
2579 int currentX
, currentY
;
2580 GetPosition(¤tX
, ¤tY
);
2586 AdjustForParentClientOrigin(x
, y
, sizeFlags
);
2588 // calculate the best size if we should auto size the window
2589 if ( ((sizeFlags
& wxSIZE_AUTO_WIDTH
) && width
== -1) ||
2590 ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) && height
== -1) )
2592 const wxSize sizeBest
= GetBestSize();
2593 if ( (sizeFlags
& wxSIZE_AUTO_WIDTH
) && width
== -1 )
2595 if ( (sizeFlags
& wxSIZE_AUTO_HEIGHT
) && height
== -1 )
2596 height
= sizeBest
.y
;
2599 const wxSize
oldSize(m_width
, m_height
);
2605 if (m_parent
->m_wxwindow
)
2607 wxPizza
* pizza
= WX_PIZZA(m_parent
->m_wxwindow
);
2608 m_x
= x
+ pizza
->m_scroll_x
;
2609 m_y
= y
+ pizza
->m_scroll_y
;
2611 int left_border
= 0;
2612 int right_border
= 0;
2614 int bottom_border
= 0;
2616 /* the default button has a border around it */
2617 if (gtk_widget_get_can_default(m_widget
))
2619 GtkBorder
*default_border
= NULL
;
2620 gtk_widget_style_get( m_widget
, "default_border", &default_border
, NULL
);
2623 left_border
+= default_border
->left
;
2624 right_border
+= default_border
->right
;
2625 top_border
+= default_border
->top
;
2626 bottom_border
+= default_border
->bottom
;
2627 gtk_border_free( default_border
);
2631 DoMoveWindow( m_x
- left_border
,
2633 m_width
+left_border
+right_border
,
2634 m_height
+top_border
+bottom_border
);
2637 if (m_width
!= oldSize
.x
|| m_height
!= oldSize
.y
)
2639 // update these variables to keep size_allocate handler
2640 // from sending another size event for this change
2641 GetClientSize( &m_oldClientWidth
, &m_oldClientHeight
);
2643 gtk_widget_queue_resize(m_widget
);
2644 if (!m_nativeSizeEvent
)
2646 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
2647 event
.SetEventObject( this );
2648 HandleWindowEvent( event
);
2651 if (sizeFlags
& wxSIZE_FORCE_EVENT
)
2653 wxSizeEvent
event( wxSize(m_width
,m_height
), GetId() );
2654 event
.SetEventObject( this );
2655 HandleWindowEvent( event
);
2659 bool wxWindowGTK::GTKShowFromOnIdle()
2661 if (IsShown() && m_showOnIdle
&& !gtk_widget_get_visible (m_widget
))
2663 GtkAllocation alloc
;
2666 alloc
.width
= m_width
;
2667 alloc
.height
= m_height
;
2668 gtk_widget_size_allocate( m_widget
, &alloc
);
2669 gtk_widget_show( m_widget
);
2670 wxShowEvent
eventShow(GetId(), true);
2671 eventShow
.SetEventObject(this);
2672 HandleWindowEvent(eventShow
);
2673 m_showOnIdle
= false;
2680 void wxWindowGTK::OnInternalIdle()
2682 if ( gs_deferredFocusOut
)
2683 GTKHandleDeferredFocusOut();
2685 // Check if we have to show window now
2686 if (GTKShowFromOnIdle()) return;
2688 if ( m_dirtyTabOrder
)
2690 m_dirtyTabOrder
= false;
2694 wxWindowBase::OnInternalIdle();
2697 void wxWindowGTK::DoGetSize( int *width
, int *height
) const
2699 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2701 if (width
) (*width
) = m_width
;
2702 if (height
) (*height
) = m_height
;
2705 void wxWindowGTK::DoSetClientSize( int width
, int height
)
2707 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2709 const wxSize size
= GetSize();
2710 const wxSize clientSize
= GetClientSize();
2711 SetSize(width
+ (size
.x
- clientSize
.x
), height
+ (size
.y
- clientSize
.y
));
2714 void wxWindowGTK::DoGetClientSize( int *width
, int *height
) const
2716 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2723 // if window is scrollable, account for scrollbars
2724 if ( GTK_IS_SCROLLED_WINDOW(m_widget
) )
2726 GtkPolicyType policy
[ScrollDir_Max
];
2727 gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(m_widget
),
2728 &policy
[ScrollDir_Horz
],
2729 &policy
[ScrollDir_Vert
]);
2731 for ( int i
= 0; i
< ScrollDir_Max
; i
++ )
2733 // don't account for the scrollbars we don't have
2734 GtkRange
* const range
= m_scrollBar
[i
];
2738 // nor for the ones we have but don't current show
2739 switch ( policy
[i
] )
2741 case GTK_POLICY_NEVER
:
2742 // never shown so doesn't take any place
2745 case GTK_POLICY_ALWAYS
:
2746 // no checks necessary
2749 case GTK_POLICY_AUTOMATIC
:
2750 // may be shown or not, check
2751 GtkAdjustment
*adj
= gtk_range_get_adjustment(range
);
2752 if (gtk_adjustment_get_upper(adj
) <= gtk_adjustment_get_page_size(adj
))
2756 GtkScrolledWindowClass
*scroll_class
=
2757 GTK_SCROLLED_WINDOW_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) );
2760 gtk_widget_size_request(GTK_WIDGET(range
), &req
);
2761 if (i
== ScrollDir_Horz
)
2762 h
-= req
.height
+ scroll_class
->scrollbar_spacing
;
2764 w
-= req
.width
+ scroll_class
->scrollbar_spacing
;
2768 const wxSize sizeBorders
= DoGetBorderSize();
2778 if (width
) *width
= w
;
2779 if (height
) *height
= h
;
2782 wxSize
wxWindowGTK::DoGetBorderSize() const
2785 return wxWindowBase::DoGetBorderSize();
2788 WX_PIZZA(m_wxwindow
)->get_border_widths(x
, y
);
2790 return 2*wxSize(x
, y
);
2793 void wxWindowGTK::DoGetPosition( int *x
, int *y
) const
2795 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2799 if (!IsTopLevel() && m_parent
&& m_parent
->m_wxwindow
)
2801 wxPizza
* pizza
= WX_PIZZA(m_parent
->m_wxwindow
);
2802 dx
= pizza
->m_scroll_x
;
2803 dy
= pizza
->m_scroll_y
;
2806 if (m_x
== -1 && m_y
== -1)
2808 GdkWindow
*source
= NULL
;
2810 source
= gtk_widget_get_window(m_wxwindow
);
2812 source
= gtk_widget_get_window(m_widget
);
2818 gdk_window_get_origin( source
, &org_x
, &org_y
);
2821 m_parent
->ScreenToClient(&org_x
, &org_y
);
2823 const_cast<wxWindowGTK
*>(this)->m_x
= org_x
;
2824 const_cast<wxWindowGTK
*>(this)->m_y
= org_y
;
2828 if (x
) (*x
) = m_x
- dx
;
2829 if (y
) (*y
) = m_y
- dy
;
2832 void wxWindowGTK::DoClientToScreen( int *x
, int *y
) const
2834 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2836 if (gtk_widget_get_window(m_widget
) == NULL
) return;
2838 GdkWindow
*source
= NULL
;
2840 source
= gtk_widget_get_window(m_wxwindow
);
2842 source
= gtk_widget_get_window(m_widget
);
2846 gdk_window_get_origin( source
, &org_x
, &org_y
);
2850 if (!gtk_widget_get_has_window(m_widget
))
2853 gtk_widget_get_allocation(m_widget
, &a
);
2862 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2863 *x
= (GetClientSize().x
- *x
) + org_x
;
2871 void wxWindowGTK::DoScreenToClient( int *x
, int *y
) const
2873 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2875 if (!gtk_widget_get_realized(m_widget
)) return;
2877 GdkWindow
*source
= NULL
;
2879 source
= gtk_widget_get_window(m_wxwindow
);
2881 source
= gtk_widget_get_window(m_widget
);
2885 gdk_window_get_origin( source
, &org_x
, &org_y
);
2889 if (!gtk_widget_get_has_window(m_widget
))
2892 gtk_widget_get_allocation(m_widget
, &a
);
2900 if (GetLayoutDirection() == wxLayout_RightToLeft
)
2901 *x
= (GetClientSize().x
- *x
) - org_x
;
2908 bool wxWindowGTK::Show( bool show
)
2910 if ( !wxWindowBase::Show(show
) )
2916 // notice that we may call Hide() before the window is created and this is
2917 // actually useful to create it hidden initially -- but we can't call
2918 // Show() before it is created
2921 wxASSERT_MSG( !show
, "can't show invalid window" );
2929 // defer until later
2933 gtk_widget_show(m_widget
);
2937 gtk_widget_hide(m_widget
);
2940 wxShowEvent
eventShow(GetId(), show
);
2941 eventShow
.SetEventObject(this);
2942 HandleWindowEvent(eventShow
);
2947 void wxWindowGTK::DoEnable( bool enable
)
2949 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
2951 gtk_widget_set_sensitive( m_widget
, enable
);
2952 if (m_wxwindow
&& (m_wxwindow
!= m_widget
))
2953 gtk_widget_set_sensitive( m_wxwindow
, enable
);
2956 int wxWindowGTK::GetCharHeight() const
2958 wxCHECK_MSG( (m_widget
!= NULL
), 12, wxT("invalid window") );
2960 wxFont font
= GetFont();
2961 wxCHECK_MSG( font
.IsOk(), 12, wxT("invalid font") );
2963 PangoContext
* context
= gtk_widget_get_pango_context(m_widget
);
2968 PangoFontDescription
*desc
= font
.GetNativeFontInfo()->description
;
2969 PangoLayout
*layout
= pango_layout_new(context
);
2970 pango_layout_set_font_description(layout
, desc
);
2971 pango_layout_set_text(layout
, "H", 1);
2972 PangoLayoutLine
*line
= (PangoLayoutLine
*)pango_layout_get_lines(layout
)->data
;
2974 PangoRectangle rect
;
2975 pango_layout_line_get_extents(line
, NULL
, &rect
);
2977 g_object_unref (layout
);
2979 return (int) PANGO_PIXELS(rect
.height
);
2982 int wxWindowGTK::GetCharWidth() const
2984 wxCHECK_MSG( (m_widget
!= NULL
), 8, wxT("invalid window") );
2986 wxFont font
= GetFont();
2987 wxCHECK_MSG( font
.IsOk(), 8, wxT("invalid font") );
2989 PangoContext
* context
= gtk_widget_get_pango_context(m_widget
);
2994 PangoFontDescription
*desc
= font
.GetNativeFontInfo()->description
;
2995 PangoLayout
*layout
= pango_layout_new(context
);
2996 pango_layout_set_font_description(layout
, desc
);
2997 pango_layout_set_text(layout
, "g", 1);
2998 PangoLayoutLine
*line
= (PangoLayoutLine
*)pango_layout_get_lines(layout
)->data
;
3000 PangoRectangle rect
;
3001 pango_layout_line_get_extents(line
, NULL
, &rect
);
3003 g_object_unref (layout
);
3005 return (int) PANGO_PIXELS(rect
.width
);
3008 void wxWindowGTK::DoGetTextExtent( const wxString
& string
,
3012 int *externalLeading
,
3013 const wxFont
*theFont
) const
3015 wxFont fontToUse
= theFont
? *theFont
: GetFont();
3017 wxCHECK_RET( fontToUse
.IsOk(), wxT("invalid font") );
3026 PangoContext
*context
= NULL
;
3028 context
= gtk_widget_get_pango_context( m_widget
);
3037 PangoFontDescription
*desc
= fontToUse
.GetNativeFontInfo()->description
;
3038 PangoLayout
*layout
= pango_layout_new(context
);
3039 pango_layout_set_font_description(layout
, desc
);
3041 const wxCharBuffer data
= wxGTK_CONV( string
);
3043 pango_layout_set_text(layout
, data
, strlen(data
));
3046 PangoRectangle rect
;
3047 pango_layout_get_extents(layout
, NULL
, &rect
);
3049 if (x
) (*x
) = (wxCoord
) PANGO_PIXELS(rect
.width
);
3050 if (y
) (*y
) = (wxCoord
) PANGO_PIXELS(rect
.height
);
3053 PangoLayoutIter
*iter
= pango_layout_get_iter(layout
);
3054 int baseline
= pango_layout_iter_get_baseline(iter
);
3055 pango_layout_iter_free(iter
);
3056 *descent
= *y
- PANGO_PIXELS(baseline
);
3058 if (externalLeading
) (*externalLeading
) = 0; // ??
3060 g_object_unref (layout
);
3063 void wxWindowGTK::GTKDisableFocusOutEvent()
3065 g_signal_handlers_block_by_func( m_focusWidget
,
3066 (gpointer
) gtk_window_focus_out_callback
, this);
3069 void wxWindowGTK::GTKEnableFocusOutEvent()
3071 g_signal_handlers_unblock_by_func( m_focusWidget
,
3072 (gpointer
) gtk_window_focus_out_callback
, this);
3075 bool wxWindowGTK::GTKHandleFocusIn()
3077 // Disable default focus handling for custom windows since the default GTK+
3078 // handler issues a repaint
3079 const bool retval
= m_wxwindow
? true : false;
3082 // NB: if there's still unprocessed deferred focus-out event (see
3083 // GTKHandleFocusOut() for explanation), we need to process it first so
3084 // that the order of focus events -- focus-out first, then focus-in
3085 // elsewhere -- is preserved
3086 if ( gs_deferredFocusOut
)
3088 if ( GTKNeedsToFilterSameWindowFocus() &&
3089 gs_deferredFocusOut
== this )
3091 // GTK+ focus changed from this wxWindow back to itself, so don't
3092 // emit any events at all
3093 wxLogTrace(TRACE_FOCUS
,
3094 "filtered out spurious focus change within %s(%p, %s)",
3095 GetClassInfo()->GetClassName(), this, GetLabel());
3096 gs_deferredFocusOut
= NULL
;
3100 // otherwise we need to send focus-out first
3101 wxASSERT_MSG ( gs_deferredFocusOut
!= this,
3102 "GTKHandleFocusIn(GTKFocus_Normal) called even though focus changed back to itself - derived class should handle this" );
3103 GTKHandleDeferredFocusOut();
3107 wxLogTrace(TRACE_FOCUS
,
3108 "handling focus_in event for %s(%p, %s)",
3109 GetClassInfo()->GetClassName(), this, GetLabel());
3112 gtk_im_context_focus_in(m_imData
->context
);
3114 gs_currentFocus
= this;
3115 gs_pendingFocus
= NULL
;
3118 // caret needs to be informed about focus change
3119 wxCaret
*caret
= GetCaret();
3122 caret
->OnSetFocus();
3124 #endif // wxUSE_CARET
3126 // Notify the parent keeping track of focus for the kbd navigation
3127 // purposes that we got it.
3128 wxChildFocusEvent
eventChildFocus(static_cast<wxWindow
*>(this));
3129 GTKProcessEvent(eventChildFocus
);
3131 wxFocusEvent
eventFocus(wxEVT_SET_FOCUS
, GetId());
3132 eventFocus
.SetEventObject(this);
3133 GTKProcessEvent(eventFocus
);
3138 bool wxWindowGTK::GTKHandleFocusOut()
3140 // Disable default focus handling for custom windows since the default GTK+
3141 // handler issues a repaint
3142 const bool retval
= m_wxwindow
? true : false;
3145 // NB: If a control is composed of several GtkWidgets and when focus
3146 // changes from one of them to another within the same wxWindow, we get
3147 // a focus-out event followed by focus-in for another GtkWidget owned
3148 // by the same wx control. We don't want to generate two spurious
3149 // wxEVT_SET_FOCUS events in this case, so we defer sending wx events
3150 // from GTKHandleFocusOut() until we know for sure it's not coming back
3151 // (i.e. in GTKHandleFocusIn() or at idle time).
3152 if ( GTKNeedsToFilterSameWindowFocus() )
3154 wxASSERT_MSG( gs_deferredFocusOut
== NULL
,
3155 "deferred focus out event already pending" );
3156 wxLogTrace(TRACE_FOCUS
,
3157 "deferring focus_out event for %s(%p, %s)",
3158 GetClassInfo()->GetClassName(), this, GetLabel());
3159 gs_deferredFocusOut
= this;
3163 GTKHandleFocusOutNoDeferring();
3168 void wxWindowGTK::GTKHandleFocusOutNoDeferring()
3170 wxLogTrace(TRACE_FOCUS
,
3171 "handling focus_out event for %s(%p, %s)",
3172 GetClassInfo()->GetClassName(), this, GetLabel());
3175 gtk_im_context_focus_out(m_imData
->context
);
3177 if ( gs_currentFocus
!= this )
3179 // Something is terribly wrong, gs_currentFocus is out of sync with the
3180 // real focus. We will reset it to NULL anyway, because after this
3181 // focus-out event is handled, one of the following with happen:
3183 // * either focus will go out of the app altogether, in which case
3184 // gs_currentFocus _should_ be NULL
3186 // * or it goes to another control, in which case focus-in event will
3187 // follow immediately and it will set gs_currentFocus to the right
3189 wxLogDebug("window %s(%p, %s) lost focus even though it didn't have it",
3190 GetClassInfo()->GetClassName(), this, GetLabel());
3192 gs_currentFocus
= NULL
;
3195 // caret needs to be informed about focus change
3196 wxCaret
*caret
= GetCaret();
3199 caret
->OnKillFocus();
3201 #endif // wxUSE_CARET
3203 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetId() );
3204 event
.SetEventObject( this );
3205 event
.SetWindow( FindFocus() );
3206 GTKProcessEvent( event
);
3210 void wxWindowGTK::GTKHandleDeferredFocusOut()
3212 // NB: See GTKHandleFocusOut() for explanation. This function is called
3213 // from either GTKHandleFocusIn() or OnInternalIdle() to process
3215 if ( gs_deferredFocusOut
)
3217 wxWindowGTK
*win
= gs_deferredFocusOut
;
3218 gs_deferredFocusOut
= NULL
;
3220 wxLogTrace(TRACE_FOCUS
,
3221 "processing deferred focus_out event for %s(%p, %s)",
3222 win
->GetClassInfo()->GetClassName(), win
, win
->GetLabel());
3224 win
->GTKHandleFocusOutNoDeferring();
3228 void wxWindowGTK::SetFocus()
3230 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
3232 // Setting "physical" focus is not immediate in GTK+ and while
3233 // gtk_widget_is_focus ("determines if the widget is the focus widget
3234 // within its toplevel", i.e. returns true for one widget per TLW, not
3235 // globally) returns true immediately after grabbing focus,
3236 // GTK_WIDGET_HAS_FOCUS (which returns true only for the one widget that
3237 // has focus at the moment) takes effect only after the window is shown
3238 // (if it was hidden at the moment of the call) or at the next event loop
3241 // Because we want to FindFocus() call immediately following
3242 // foo->SetFocus() to return foo, we have to keep track of "pending" focus
3244 gs_pendingFocus
= this;
3246 GtkWidget
*widget
= m_wxwindow
? m_wxwindow
: m_focusWidget
;
3248 if ( GTK_IS_CONTAINER(widget
) &&
3249 !gtk_widget_get_can_focus(widget
) )
3251 wxLogTrace(TRACE_FOCUS
,
3252 wxT("Setting focus to a child of %s(%p, %s)"),
3253 GetClassInfo()->GetClassName(), this, GetLabel().c_str());
3254 gtk_widget_child_focus(widget
, GTK_DIR_TAB_FORWARD
);
3258 wxLogTrace(TRACE_FOCUS
,
3259 wxT("Setting focus to %s(%p, %s)"),
3260 GetClassInfo()->GetClassName(), this, GetLabel().c_str());
3261 gtk_widget_grab_focus(widget
);
3265 void wxWindowGTK::SetCanFocus(bool canFocus
)
3267 gtk_widget_set_can_focus(m_widget
, canFocus
);
3269 if ( m_wxwindow
&& (m_widget
!= m_wxwindow
) )
3271 gtk_widget_set_can_focus(m_wxwindow
, canFocus
);
3275 bool wxWindowGTK::Reparent( wxWindowBase
*newParentBase
)
3277 wxCHECK_MSG( (m_widget
!= NULL
), false, wxT("invalid window") );
3279 wxWindowGTK
* const newParent
= (wxWindowGTK
*)newParentBase
;
3281 wxASSERT( GTK_IS_WIDGET(m_widget
) );
3283 if ( !wxWindowBase::Reparent(newParent
) )
3286 wxASSERT( GTK_IS_WIDGET(m_widget
) );
3288 // Notice that old m_parent pointer might be non-NULL here but the widget
3289 // still not have any parent at GTK level if it's a notebook page that had
3290 // been removed from the notebook so test this at GTK level and not wx one.
3291 if ( GtkWidget
*parentGTK
= gtk_widget_get_parent(m_widget
) )
3292 gtk_container_remove(GTK_CONTAINER(parentGTK
), m_widget
);
3294 wxASSERT( GTK_IS_WIDGET(m_widget
) );
3298 if (gtk_widget_get_visible (newParent
->m_widget
))
3300 m_showOnIdle
= true;
3301 gtk_widget_hide( m_widget
);
3303 /* insert GTK representation */
3304 newParent
->AddChildGTK(this);
3307 SetLayoutDirection(wxLayout_Default
);
3312 void wxWindowGTK::DoAddChild(wxWindowGTK
*child
)
3314 wxASSERT_MSG( (m_widget
!= NULL
), wxT("invalid window") );
3315 wxASSERT_MSG( (child
!= NULL
), wxT("invalid child window") );
3320 /* insert GTK representation */
3324 void wxWindowGTK::AddChild(wxWindowBase
*child
)
3326 wxWindowBase::AddChild(child
);
3327 m_dirtyTabOrder
= true;
3328 wxTheApp
->WakeUpIdle();
3331 void wxWindowGTK::RemoveChild(wxWindowBase
*child
)
3333 wxWindowBase::RemoveChild(child
);
3334 m_dirtyTabOrder
= true;
3335 wxTheApp
->WakeUpIdle();
3339 wxLayoutDirection
wxWindowGTK::GTKGetLayout(GtkWidget
*widget
)
3341 return gtk_widget_get_direction(widget
) == GTK_TEXT_DIR_RTL
3342 ? wxLayout_RightToLeft
3343 : wxLayout_LeftToRight
;
3347 void wxWindowGTK::GTKSetLayout(GtkWidget
*widget
, wxLayoutDirection dir
)
3349 wxASSERT_MSG( dir
!= wxLayout_Default
, wxT("invalid layout direction") );
3351 gtk_widget_set_direction(widget
,
3352 dir
== wxLayout_RightToLeft
? GTK_TEXT_DIR_RTL
3353 : GTK_TEXT_DIR_LTR
);
3356 wxLayoutDirection
wxWindowGTK::GetLayoutDirection() const
3358 return GTKGetLayout(m_widget
);
3361 void wxWindowGTK::SetLayoutDirection(wxLayoutDirection dir
)
3363 if ( dir
== wxLayout_Default
)
3365 const wxWindow
*const parent
= GetParent();
3368 // inherit layout from parent.
3369 dir
= parent
->GetLayoutDirection();
3371 else // no parent, use global default layout
3373 dir
= wxTheApp
->GetLayoutDirection();
3377 if ( dir
== wxLayout_Default
)
3380 GTKSetLayout(m_widget
, dir
);
3382 if (m_wxwindow
&& (m_wxwindow
!= m_widget
))
3383 GTKSetLayout(m_wxwindow
, dir
);
3387 wxWindowGTK::AdjustForLayoutDirection(wxCoord x
,
3388 wxCoord
WXUNUSED(width
),
3389 wxCoord
WXUNUSED(widthTotal
)) const
3391 // We now mirror the coordinates of RTL windows in wxPizza
3395 void wxWindowGTK::DoMoveInTabOrder(wxWindow
*win
, WindowOrder move
)
3397 wxWindowBase::DoMoveInTabOrder(win
, move
);
3398 m_dirtyTabOrder
= true;
3399 wxTheApp
->WakeUpIdle();
3402 bool wxWindowGTK::DoNavigateIn(int flags
)
3404 if ( flags
& wxNavigationKeyEvent::WinChange
)
3406 wxFAIL_MSG( wxT("not implemented") );
3410 else // navigate inside the container
3412 wxWindow
*parent
= wxGetTopLevelParent((wxWindow
*)this);
3413 wxCHECK_MSG( parent
, false, wxT("every window must have a TLW parent") );
3415 GtkDirectionType dir
;
3416 dir
= flags
& wxNavigationKeyEvent::IsForward
? GTK_DIR_TAB_FORWARD
3417 : GTK_DIR_TAB_BACKWARD
;
3420 g_signal_emit_by_name(parent
->m_widget
, "focus", dir
, &rc
);
3426 bool wxWindowGTK::GTKWidgetNeedsMnemonic() const
3428 // none needed by default
3432 void wxWindowGTK::GTKWidgetDoSetMnemonic(GtkWidget
* WXUNUSED(w
))
3434 // nothing to do by default since none is needed
3437 void wxWindowGTK::RealizeTabOrder()
3441 if ( !m_children
.empty() )
3443 // we don't only construct the correct focus chain but also use
3444 // this opportunity to update the mnemonic widgets for the widgets
3447 GList
*chain
= NULL
;
3448 wxWindowGTK
* mnemonicWindow
= NULL
;
3450 for ( wxWindowList::const_iterator i
= m_children
.begin();
3451 i
!= m_children
.end();
3454 wxWindowGTK
*win
= *i
;
3456 bool focusableFromKeyboard
= win
->AcceptsFocusFromKeyboard();
3458 if ( mnemonicWindow
)
3460 if ( focusableFromKeyboard
)
3462 // wxComboBox et al. needs to focus on on a different
3463 // widget than m_widget, so if the main widget isn't
3464 // focusable try the connect widget
3465 GtkWidget
* w
= win
->m_widget
;
3466 if ( !gtk_widget_get_can_focus(w
) )
3468 w
= win
->GetConnectWidget();
3469 if ( !gtk_widget_get_can_focus(w
) )
3475 mnemonicWindow
->GTKWidgetDoSetMnemonic(w
);
3476 mnemonicWindow
= NULL
;
3480 else if ( win
->GTKWidgetNeedsMnemonic() )
3482 mnemonicWindow
= win
;
3485 if ( focusableFromKeyboard
)
3486 chain
= g_list_prepend(chain
, win
->m_widget
);
3489 chain
= g_list_reverse(chain
);
3491 gtk_container_set_focus_chain(GTK_CONTAINER(m_wxwindow
), chain
);
3496 gtk_container_unset_focus_chain(GTK_CONTAINER(m_wxwindow
));
3501 void wxWindowGTK::Raise()
3503 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
3505 if (m_wxwindow
&& gtk_widget_get_window(m_wxwindow
))
3507 gdk_window_raise(gtk_widget_get_window(m_wxwindow
));
3509 else if (gtk_widget_get_window(m_widget
))
3511 gdk_window_raise(gtk_widget_get_window(m_widget
));
3515 void wxWindowGTK::Lower()
3517 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
3519 if (m_wxwindow
&& gtk_widget_get_window(m_wxwindow
))
3521 gdk_window_lower(gtk_widget_get_window(m_wxwindow
));
3523 else if (gtk_widget_get_window(m_widget
))
3525 gdk_window_lower(gtk_widget_get_window(m_widget
));
3529 bool wxWindowGTK::SetCursor( const wxCursor
&cursor
)
3531 if ( !wxWindowBase::SetCursor(cursor
.IsOk() ? cursor
: *wxSTANDARD_CURSOR
) )
3539 void wxWindowGTK::GTKUpdateCursor(bool update_self
/*=true*/, bool recurse
/*=true*/)
3543 wxCursor
cursor(g_globalCursor
.IsOk() ? g_globalCursor
: GetCursor());
3544 if ( cursor
.IsOk() )
3546 wxArrayGdkWindows windowsThis
;
3547 GdkWindow
* window
= GTKGetWindow(windowsThis
);
3549 gdk_window_set_cursor( window
, cursor
.GetCursor() );
3552 const size_t count
= windowsThis
.size();
3553 for ( size_t n
= 0; n
< count
; n
++ )
3555 GdkWindow
*win
= windowsThis
[n
];
3556 // It can be zero if the window has not been realized yet.
3559 gdk_window_set_cursor(win
, cursor
.GetCursor());
3568 for (wxWindowList::iterator it
= GetChildren().begin(); it
!= GetChildren().end(); ++it
)
3570 (*it
)->GTKUpdateCursor( true );
3575 void wxWindowGTK::WarpPointer( int x
, int y
)
3577 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
3579 ClientToScreen(&x
, &y
);
3580 GdkDisplay
* display
= gtk_widget_get_display(m_widget
);
3581 GdkScreen
* screen
= gtk_widget_get_screen(m_widget
);
3583 GdkDeviceManager
* manager
= gdk_display_get_device_manager(display
);
3584 gdk_device_warp(gdk_device_manager_get_client_pointer(manager
), screen
, x
, y
);
3586 XWarpPointer(GDK_DISPLAY_XDISPLAY(display
),
3588 GDK_WINDOW_XID(gdk_screen_get_root_window(screen
)),
3593 wxWindowGTK::ScrollDir
wxWindowGTK::ScrollDirFromRange(GtkRange
*range
) const
3595 // find the scrollbar which generated the event
3596 for ( int dir
= 0; dir
< ScrollDir_Max
; dir
++ )
3598 if ( range
== m_scrollBar
[dir
] )
3599 return (ScrollDir
)dir
;
3602 wxFAIL_MSG( wxT("event from unknown scrollbar received") );
3604 return ScrollDir_Max
;
3607 bool wxWindowGTK::DoScrollByUnits(ScrollDir dir
, ScrollUnit unit
, int units
)
3609 bool changed
= false;
3610 GtkRange
* range
= m_scrollBar
[dir
];
3611 if ( range
&& units
)
3613 GtkAdjustment
* adj
= gtk_range_get_adjustment(range
);
3614 double inc
= unit
== ScrollUnit_Line
? gtk_adjustment_get_step_increment(adj
)
3615 : gtk_adjustment_get_page_increment(adj
);
3617 const int posOld
= wxRound(gtk_adjustment_get_value(adj
));
3618 gtk_range_set_value(range
, posOld
+ units
*inc
);
3620 changed
= wxRound(gtk_adjustment_get_value(adj
)) != posOld
;
3626 bool wxWindowGTK::ScrollLines(int lines
)
3628 return DoScrollByUnits(ScrollDir_Vert
, ScrollUnit_Line
, lines
);
3631 bool wxWindowGTK::ScrollPages(int pages
)
3633 return DoScrollByUnits(ScrollDir_Vert
, ScrollUnit_Page
, pages
);
3636 void wxWindowGTK::Refresh(bool WXUNUSED(eraseBackground
),
3641 if (gtk_widget_get_mapped(m_wxwindow
))
3643 GdkWindow
* window
= gtk_widget_get_window(m_wxwindow
);
3646 GdkRectangle r
= { rect
->x
, rect
->y
, rect
->width
, rect
->height
};
3647 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3648 r
.x
= gdk_window_get_width(window
) - r
.x
- rect
->width
;
3649 gdk_window_invalidate_rect(window
, &r
, true);
3652 gdk_window_invalidate_rect(window
, NULL
, true);
3657 if (gtk_widget_get_mapped(m_widget
))
3660 gtk_widget_queue_draw_area(m_widget
, rect
->x
, rect
->y
, rect
->width
, rect
->height
);
3662 gtk_widget_queue_draw(m_widget
);
3667 void wxWindowGTK::Update()
3669 if (m_widget
&& gtk_widget_get_mapped(m_widget
))
3671 GdkDisplay
* display
= gtk_widget_get_display(m_widget
);
3672 // Flush everything out to the server, and wait for it to finish.
3673 // This ensures nothing will overwrite the drawing we are about to do.
3674 gdk_display_sync(display
);
3676 GdkWindow
* window
= GTKGetDrawingWindow();
3678 window
= gtk_widget_get_window(m_widget
);
3679 gdk_window_process_updates(window
, true);
3681 // Flush again, but no need to wait for it to finish
3682 gdk_display_flush(display
);
3686 bool wxWindowGTK::DoIsExposed( int x
, int y
) const
3688 return m_updateRegion
.Contains(x
, y
) != wxOutRegion
;
3691 bool wxWindowGTK::DoIsExposed( int x
, int y
, int w
, int h
) const
3693 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3694 return m_updateRegion
.Contains(x
-w
, y
, w
, h
) != wxOutRegion
;
3696 return m_updateRegion
.Contains(x
, y
, w
, h
) != wxOutRegion
;
3699 void wxWindowGTK::GtkSendPaintEvents()
3703 m_updateRegion
.Clear();
3707 // Clip to paint region in wxClientDC
3708 m_clipPaintRegion
= true;
3710 m_nativeUpdateRegion
= m_updateRegion
;
3712 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3714 // Transform m_updateRegion under RTL
3715 m_updateRegion
.Clear();
3718 gdk_drawable_get_size(gtk_widget_get_window(m_wxwindow
), &width
, NULL
);
3720 wxRegionIterator
upd( m_nativeUpdateRegion
);
3724 rect
.x
= upd
.GetX();
3725 rect
.y
= upd
.GetY();
3726 rect
.width
= upd
.GetWidth();
3727 rect
.height
= upd
.GetHeight();
3729 rect
.x
= width
- rect
.x
- rect
.width
;
3730 m_updateRegion
.Union( rect
);
3736 switch ( GetBackgroundStyle() )
3738 #if wxUSE_GRAPHICS_CONTEXT
3739 case wxBG_STYLE_TRANSPARENT
:
3741 // Set a transparent background, so that overlaying in parent
3742 // might indeed let see through where this child did not
3743 // explicitly paint.
3744 // NB: it works also for top level windows (but this is the
3745 // windows manager which then does the compositing job)
3746 wxScopedPtr
<wxGraphicsContext
> gc (wxGraphicsContext::Create( this ));
3747 cairo_t
*cairo_context
= (cairo_t
*)gc
->GetNativeContext();
3749 gc
->Clip (m_nativeUpdateRegion
);
3750 cairo_set_operator (cairo_context
, CAIRO_OPERATOR_CLEAR
);
3751 cairo_paint (cairo_context
);
3754 #endif // wxUSE_GRAPHICS_CONTEXT
3756 case wxBG_STYLE_ERASE
:
3758 wxWindowDC
dc( (wxWindow
*)this );
3759 dc
.SetDeviceClippingRegion( m_updateRegion
);
3761 // Work around gtk-qt <= 0.60 bug whereby the window colour
3765 GetOptionInt("gtk.window.force-background-colour") )
3767 dc
.SetBackground(GetBackgroundColour());
3771 wxEraseEvent
erase_event( GetId(), &dc
);
3772 erase_event
.SetEventObject( this );
3774 if ( HandleWindowEvent(erase_event
) )
3776 // background erased, don't do it again
3782 case wxBG_STYLE_SYSTEM
:
3783 if ( GetThemeEnabled() )
3785 // find ancestor from which to steal background
3786 wxWindow
*parent
= wxGetTopLevelParent((wxWindow
*)this);
3788 parent
= (wxWindow
*)this;
3790 if (gtk_widget_get_mapped(parent
->m_widget
))
3792 wxRegionIterator
upd( m_nativeUpdateRegion
);
3796 rect
.x
= upd
.GetX();
3797 rect
.y
= upd
.GetY();
3798 rect
.width
= upd
.GetWidth();
3799 rect
.height
= upd
.GetHeight();
3801 gtk_paint_flat_box(gtk_widget_get_style(parent
->m_widget
),
3802 GTKGetDrawingWindow(),
3803 gtk_widget_get_state(m_wxwindow
),
3816 case wxBG_STYLE_PAINT
:
3817 // nothing to do: window will be painted over in EVT_PAINT
3821 wxFAIL_MSG( "unsupported background style" );
3824 wxNcPaintEvent
nc_paint_event( GetId() );
3825 nc_paint_event
.SetEventObject( this );
3826 HandleWindowEvent( nc_paint_event
);
3828 wxPaintEvent
paint_event( GetId() );
3829 paint_event
.SetEventObject( this );
3830 HandleWindowEvent( paint_event
);
3832 #if wxUSE_GRAPHICS_CONTEXT
3833 { // now composite children which need it
3834 wxScopedPtr
<wxGraphicsContext
> gc (wxGraphicsContext::Create( this ));
3835 cairo_t
*cairo_context
= (cairo_t
*)gc
->GetNativeContext();
3837 // Overlay all our composite children on top of the painted area
3838 wxWindowList::compatibility_iterator node
;
3839 for ( node
= m_children
.GetFirst(); node
; node
= node
->GetNext() )
3841 wxWindow
*compositeChild
= node
->GetData();
3842 if (compositeChild
->GetBackgroundStyle() == wxBG_STYLE_TRANSPARENT
)
3844 GtkWidget
*child
= compositeChild
->m_wxwindow
;
3846 // The source data is the (composited) child
3847 gdk_cairo_set_source_pixmap (cairo_context
, child
->window
,
3848 child
->allocation
.x
,
3849 child
->allocation
.y
);
3851 // Draw no more than our expose event intersects our child
3852 gc
->Clip (m_nativeUpdateRegion
);
3853 gc
->Clip (child
->allocation
.x
, child
->allocation
.y
,
3854 child
->allocation
.width
, child
->allocation
.height
);
3856 cairo_set_operator (cairo_context
, CAIRO_OPERATOR_OVER
);
3857 cairo_paint (cairo_context
);
3863 #endif // wxUSE_GRAPHICS_CONTEXT
3865 m_clipPaintRegion
= false;
3867 m_updateRegion
.Clear();
3868 m_nativeUpdateRegion
.Clear();
3871 void wxWindowGTK::SetDoubleBuffered( bool on
)
3873 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid window") );
3876 gtk_widget_set_double_buffered( m_wxwindow
, on
);
3879 bool wxWindowGTK::IsDoubleBuffered() const
3881 return gtk_widget_get_double_buffered( m_wxwindow
);
3884 void wxWindowGTK::ClearBackground()
3886 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
3890 void wxWindowGTK::DoSetToolTip( wxToolTip
*tip
)
3892 if (m_tooltip
!= tip
)
3894 wxWindowBase::DoSetToolTip(tip
);
3897 m_tooltip
->GTKSetWindow(static_cast<wxWindow
*>(this));
3899 GTKApplyToolTip(NULL
);
3903 void wxWindowGTK::GTKApplyToolTip(const char* tip
)
3905 wxToolTip::GTKApply(GetConnectWidget(), tip
);
3907 #endif // wxUSE_TOOLTIPS
3909 bool wxWindowGTK::SetBackgroundColour( const wxColour
&colour
)
3911 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
3913 if (!wxWindowBase::SetBackgroundColour(colour
))
3918 // We need the pixel value e.g. for background clearing.
3919 m_backgroundColour
.CalcPixel(gtk_widget_get_colormap(m_widget
));
3922 // apply style change (forceStyle=true so that new style is applied
3923 // even if the bg colour changed from valid to wxNullColour)
3924 GTKApplyWidgetStyle(true);
3929 bool wxWindowGTK::SetForegroundColour( const wxColour
&colour
)
3931 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
3933 if (!wxWindowBase::SetForegroundColour(colour
))
3940 // We need the pixel value e.g. for background clearing.
3941 m_foregroundColour
.CalcPixel(gtk_widget_get_colormap(m_widget
));
3944 // apply style change (forceStyle=true so that new style is applied
3945 // even if the bg colour changed from valid to wxNullColour):
3946 GTKApplyWidgetStyle(true);
3951 PangoContext
*wxWindowGTK::GTKGetPangoDefaultContext()
3953 return gtk_widget_get_pango_context( m_widget
);
3956 GtkRcStyle
*wxWindowGTK::GTKCreateWidgetStyle(bool forceStyle
)
3958 // do we need to apply any changes at all?
3961 !m_foregroundColour
.IsOk() && !m_backgroundColour
.IsOk() )
3966 GtkRcStyle
*style
= gtk_rc_style_new();
3968 if ( m_font
.IsOk() )
3971 pango_font_description_copy( m_font
.GetNativeFontInfo()->description
);
3974 int flagsNormal
= 0,
3977 flagsInsensitive
= 0;
3979 if ( m_foregroundColour
.IsOk() )
3981 const GdkColor
*fg
= m_foregroundColour
.GetColor();
3983 style
->fg
[GTK_STATE_NORMAL
] =
3984 style
->text
[GTK_STATE_NORMAL
] = *fg
;
3985 flagsNormal
|= GTK_RC_FG
| GTK_RC_TEXT
;
3987 style
->fg
[GTK_STATE_PRELIGHT
] =
3988 style
->text
[GTK_STATE_PRELIGHT
] = *fg
;
3989 flagsPrelight
|= GTK_RC_FG
| GTK_RC_TEXT
;
3991 style
->fg
[GTK_STATE_ACTIVE
] =
3992 style
->text
[GTK_STATE_ACTIVE
] = *fg
;
3993 flagsActive
|= GTK_RC_FG
| GTK_RC_TEXT
;
3996 if ( m_backgroundColour
.IsOk() )
3998 const GdkColor
*bg
= m_backgroundColour
.GetColor();
4000 style
->bg
[GTK_STATE_NORMAL
] =
4001 style
->base
[GTK_STATE_NORMAL
] = *bg
;
4002 flagsNormal
|= GTK_RC_BG
| GTK_RC_BASE
;
4004 style
->bg
[GTK_STATE_PRELIGHT
] =
4005 style
->base
[GTK_STATE_PRELIGHT
] = *bg
;
4006 flagsPrelight
|= GTK_RC_BG
| GTK_RC_BASE
;
4008 style
->bg
[GTK_STATE_ACTIVE
] =
4009 style
->base
[GTK_STATE_ACTIVE
] = *bg
;
4010 flagsActive
|= GTK_RC_BG
| GTK_RC_BASE
;
4012 style
->bg
[GTK_STATE_INSENSITIVE
] =
4013 style
->base
[GTK_STATE_INSENSITIVE
] = *bg
;
4014 flagsInsensitive
|= GTK_RC_BG
| GTK_RC_BASE
;
4017 style
->color_flags
[GTK_STATE_NORMAL
] = (GtkRcFlags
)flagsNormal
;
4018 style
->color_flags
[GTK_STATE_PRELIGHT
] = (GtkRcFlags
)flagsPrelight
;
4019 style
->color_flags
[GTK_STATE_ACTIVE
] = (GtkRcFlags
)flagsActive
;
4020 style
->color_flags
[GTK_STATE_INSENSITIVE
] = (GtkRcFlags
)flagsInsensitive
;
4025 void wxWindowGTK::GTKApplyWidgetStyle(bool forceStyle
)
4027 GtkRcStyle
*style
= GTKCreateWidgetStyle(forceStyle
);
4030 DoApplyWidgetStyle(style
);
4031 g_object_unref(style
);
4034 // Style change may affect GTK+'s size calculation:
4035 InvalidateBestSize();
4038 void wxWindowGTK::DoApplyWidgetStyle(GtkRcStyle
*style
)
4042 // block the signal temporarily to avoid sending
4043 // wxSysColourChangedEvents when we change the colours ourselves
4044 bool unblock
= false;
4048 g_signal_handlers_block_by_func(
4049 m_wxwindow
, (void *)gtk_window_style_set_callback
, this);
4052 gtk_widget_modify_style(m_wxwindow
, style
);
4056 g_signal_handlers_unblock_by_func(
4057 m_wxwindow
, (void *)gtk_window_style_set_callback
, this);
4062 gtk_widget_modify_style(m_widget
, style
);
4066 bool wxWindowGTK::SetBackgroundStyle(wxBackgroundStyle style
)
4068 if (!wxWindowBase::SetBackgroundStyle(style
))
4074 window
= GTKGetDrawingWindow();
4078 GtkWidget
* const w
= GetConnectWidget();
4079 window
= w
? gtk_widget_get_window(w
) : NULL
;
4082 bool wantNoBackPixmap
= style
== wxBG_STYLE_PAINT
|| style
== wxBG_STYLE_TRANSPARENT
;
4084 if ( wantNoBackPixmap
)
4088 // Make sure GDK/X11 doesn't refresh the window
4090 gdk_window_set_back_pixmap( window
, None
, False
);
4091 m_needsStyleChange
= false;
4093 else // window not realized yet
4095 // Do when window is realized
4096 m_needsStyleChange
= true;
4099 // Don't apply widget style, or we get a grey background
4103 // apply style change (forceStyle=true so that new style is applied
4104 // even if the bg colour changed from valid to wxNullColour):
4105 GTKApplyWidgetStyle(true);
4111 bool wxWindowGTK::IsTransparentBackgroundSupported(wxString
* reason
) const
4113 #if wxGTK_HAS_COMPOSITING_SUPPORT && wxUSE_GRAPHICS_CONTEXT
4114 if (gtk_check_version(wxGTK_VERSION_REQUIRED_FOR_COMPOSITING
) != NULL
)
4118 *reason
= _("GTK+ installed on this machine is too old to "
4119 "support screen compositing, please install "
4120 "GTK+ 2.12 or later.");
4126 // NB: We don't check here if the particular kind of widget supports
4127 // transparency, we check only if it would be possible for a generic window
4129 wxCHECK_MSG ( m_widget
, false, "Window must be created first" );
4131 if (!gdk_screen_is_composited(gtk_widget_get_screen(m_widget
)))
4135 *reason
= _("Compositing not supported by this system, "
4136 "please enable it in your Window Manager.");
4143 #elif !wxGTK_HAS_COMPOSITING_SUPPORT
4146 *reason
= _("This program was compiled with a too old version of GTK+, "
4147 "please rebuild with GTK+ 2.12 or newer.");
4149 #elif !wxUSE_GRAPHICS_CONTEXT
4152 *reason
= _("wxUSE_GRAPHICS_CONTEXT required for compositing window, "
4153 "please rebuild wxWidgets with support for it.");
4155 #endif // wxGTK_HAS_COMPOSITING_SUPPORT/!wxGTK_HAS_COMPOSITING_SUPPORT
4160 // ----------------------------------------------------------------------------
4161 // Pop-up menu stuff
4162 // ----------------------------------------------------------------------------
4164 #if wxUSE_MENUS_NATIVE
4168 void wxPopupMenuPositionCallback( GtkMenu
*menu
,
4170 gboolean
* WXUNUSED(whatever
),
4171 gpointer user_data
)
4173 // ensure that the menu appears entirely on screen
4175 gtk_widget_get_child_requisition(GTK_WIDGET(menu
), &req
);
4177 wxSize sizeScreen
= wxGetDisplaySize();
4178 wxPoint
*pos
= (wxPoint
*)user_data
;
4180 gint xmax
= sizeScreen
.x
- req
.width
,
4181 ymax
= sizeScreen
.y
- req
.height
;
4183 *x
= pos
->x
< xmax
? pos
->x
: xmax
;
4184 *y
= pos
->y
< ymax
? pos
->y
: ymax
;
4188 bool wxWindowGTK::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
4190 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
4192 // For compatibility with other ports, pretend that the window showing the
4193 // menu has focus while the menu is shown. This is needed because the popup
4194 // menu actually steals the focus from the window it's associated it in
4195 // wxGTK unlike, say, wxMSW.
4196 wxWindowGTK
* const oldPendingFocus
= gs_pendingFocus
;
4197 gs_pendingFocus
= this;
4198 wxON_BLOCK_EXIT_SET( gs_pendingFocus
, oldPendingFocus
);
4204 GtkMenuPositionFunc posfunc
;
4205 if ( x
== -1 && y
== -1 )
4207 // use GTK's default positioning algorithm
4213 pos
= ClientToScreen(wxPoint(x
, y
));
4215 posfunc
= wxPopupMenuPositionCallback
;
4218 menu
->m_popupShown
= true;
4220 GTK_MENU(menu
->m_menu
),
4221 NULL
, // parent menu shell
4222 NULL
, // parent menu item
4223 posfunc
, // function to position it
4224 userdata
, // client data
4225 0, // button used to activate it
4226 gtk_get_current_event_time()
4229 while (menu
->m_popupShown
)
4231 gtk_main_iteration();
4237 #endif // wxUSE_MENUS_NATIVE
4239 #if wxUSE_DRAG_AND_DROP
4241 void wxWindowGTK::SetDropTarget( wxDropTarget
*dropTarget
)
4243 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
4245 GtkWidget
*dnd_widget
= GetConnectWidget();
4247 if (m_dropTarget
) m_dropTarget
->GtkUnregisterWidget( dnd_widget
);
4249 if (m_dropTarget
) delete m_dropTarget
;
4250 m_dropTarget
= dropTarget
;
4252 if (m_dropTarget
) m_dropTarget
->GtkRegisterWidget( dnd_widget
);
4255 #endif // wxUSE_DRAG_AND_DROP
4257 GtkWidget
* wxWindowGTK::GetConnectWidget()
4259 GtkWidget
*connect_widget
= m_widget
;
4260 if (m_wxwindow
) connect_widget
= m_wxwindow
;
4262 return connect_widget
;
4265 bool wxWindowGTK::GTKIsOwnWindow(GdkWindow
*window
) const
4267 wxArrayGdkWindows windowsThis
;
4268 GdkWindow
* const winThis
= GTKGetWindow(windowsThis
);
4270 return winThis
? window
== winThis
4271 : windowsThis
.Index(window
) != wxNOT_FOUND
;
4274 GdkWindow
*wxWindowGTK::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
4276 return m_wxwindow
? GTKGetDrawingWindow() : gtk_widget_get_window(m_widget
);
4279 bool wxWindowGTK::SetFont( const wxFont
&font
)
4281 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid window") );
4283 if (!wxWindowBase::SetFont(font
))
4286 // apply style change (forceStyle=true so that new style is applied
4287 // even if the font changed from valid to wxNullFont):
4288 GTKApplyWidgetStyle(true);
4293 void wxWindowGTK::DoCaptureMouse()
4295 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
4297 GdkWindow
*window
= NULL
;
4299 window
= GTKGetDrawingWindow();
4301 window
= gtk_widget_get_window(GetConnectWidget());
4303 wxCHECK_RET( window
, wxT("CaptureMouse() failed") );
4305 const wxCursor
* cursor
= &m_cursor
;
4306 if (!cursor
->IsOk())
4307 cursor
= wxSTANDARD_CURSOR
;
4309 gdk_pointer_grab( window
, FALSE
,
4311 (GDK_BUTTON_PRESS_MASK
|
4312 GDK_BUTTON_RELEASE_MASK
|
4313 GDK_POINTER_MOTION_HINT_MASK
|
4314 GDK_POINTER_MOTION_MASK
),
4316 cursor
->GetCursor(),
4317 (guint32
)GDK_CURRENT_TIME
);
4318 g_captureWindow
= this;
4319 g_captureWindowHasMouse
= true;
4322 void wxWindowGTK::DoReleaseMouse()
4324 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
4326 wxCHECK_RET( g_captureWindow
, wxT("can't release mouse - not captured") );
4328 g_captureWindow
= NULL
;
4330 GdkWindow
*window
= NULL
;
4332 window
= GTKGetDrawingWindow();
4334 window
= gtk_widget_get_window(GetConnectWidget());
4339 gdk_pointer_ungrab ( (guint32
)GDK_CURRENT_TIME
);
4342 void wxWindowGTK::GTKReleaseMouseAndNotify()
4345 wxMouseCaptureLostEvent
evt(GetId());
4346 evt
.SetEventObject( this );
4347 HandleWindowEvent( evt
);
4351 wxWindow
*wxWindowBase::GetCapture()
4353 return (wxWindow
*)g_captureWindow
;
4356 bool wxWindowGTK::IsRetained() const
4361 void wxWindowGTK::SetScrollbar(int orient
,
4365 bool WXUNUSED(update
))
4367 const int dir
= ScrollDirFromOrient(orient
);
4368 GtkRange
* const sb
= m_scrollBar
[dir
];
4369 wxCHECK_RET( sb
, wxT("this window is not scrollable") );
4373 // GtkRange requires upper > lower
4378 g_signal_handlers_block_by_func(
4379 sb
, (void*)gtk_scrollbar_value_changed
, this);
4381 gtk_range_set_increments(sb
, 1, thumbVisible
);
4382 gtk_adjustment_set_page_size(gtk_range_get_adjustment(sb
), thumbVisible
);
4383 gtk_range_set_range(sb
, 0, range
);
4384 gtk_range_set_value(sb
, pos
);
4385 m_scrollPos
[dir
] = gtk_range_get_value(sb
);
4387 g_signal_handlers_unblock_by_func(
4388 sb
, (void*)gtk_scrollbar_value_changed
, this);
4391 void wxWindowGTK::SetScrollPos(int orient
, int pos
, bool WXUNUSED(refresh
))
4393 const int dir
= ScrollDirFromOrient(orient
);
4394 GtkRange
* const sb
= m_scrollBar
[dir
];
4395 wxCHECK_RET( sb
, wxT("this window is not scrollable") );
4397 // This check is more than an optimization. Without it, the slider
4398 // will not move smoothly while tracking when using wxScrollHelper.
4399 if (GetScrollPos(orient
) != pos
)
4401 g_signal_handlers_block_by_func(
4402 sb
, (void*)gtk_scrollbar_value_changed
, this);
4404 gtk_range_set_value(sb
, pos
);
4405 m_scrollPos
[dir
] = gtk_range_get_value(sb
);
4407 g_signal_handlers_unblock_by_func(
4408 sb
, (void*)gtk_scrollbar_value_changed
, this);
4412 int wxWindowGTK::GetScrollThumb(int orient
) const
4414 GtkRange
* const sb
= m_scrollBar
[ScrollDirFromOrient(orient
)];
4415 wxCHECK_MSG( sb
, 0, wxT("this window is not scrollable") );
4417 return wxRound(gtk_adjustment_get_page_size(gtk_range_get_adjustment(sb
)));
4420 int wxWindowGTK::GetScrollPos( int orient
) const
4422 GtkRange
* const sb
= m_scrollBar
[ScrollDirFromOrient(orient
)];
4423 wxCHECK_MSG( sb
, 0, wxT("this window is not scrollable") );
4425 return wxRound(gtk_range_get_value(sb
));
4428 int wxWindowGTK::GetScrollRange( int orient
) const
4430 GtkRange
* const sb
= m_scrollBar
[ScrollDirFromOrient(orient
)];
4431 wxCHECK_MSG( sb
, 0, wxT("this window is not scrollable") );
4433 return wxRound(gtk_adjustment_get_upper(gtk_range_get_adjustment(sb
)));
4436 // Determine if increment is the same as +/-x, allowing for some small
4437 // difference due to possible inexactness in floating point arithmetic
4438 static inline bool IsScrollIncrement(double increment
, double x
)
4440 wxASSERT(increment
> 0);
4441 const double tolerance
= 1.0 / 1024;
4442 return fabs(increment
- fabs(x
)) < tolerance
;
4445 wxEventType
wxWindowGTK::GTKGetScrollEventType(GtkRange
* range
)
4447 wxASSERT(range
== m_scrollBar
[0] || range
== m_scrollBar
[1]);
4449 const int barIndex
= range
== m_scrollBar
[1];
4451 const double value
= gtk_range_get_value(range
);
4453 // save previous position
4454 const double oldPos
= m_scrollPos
[barIndex
];
4455 // update current position
4456 m_scrollPos
[barIndex
] = value
;
4457 // If event should be ignored, or integral position has not changed
4458 if (!m_hasVMT
|| g_blockEventsOnDrag
|| wxRound(value
) == wxRound(oldPos
))
4463 wxEventType eventType
= wxEVT_SCROLL_THUMBTRACK
;
4466 // Difference from last change event
4467 const double diff
= value
- oldPos
;
4468 const bool isDown
= diff
> 0;
4470 GtkAdjustment
* adj
= gtk_range_get_adjustment(range
);
4471 if (IsScrollIncrement(gtk_adjustment_get_step_increment(adj
), diff
))
4473 eventType
= isDown
? wxEVT_SCROLL_LINEDOWN
: wxEVT_SCROLL_LINEUP
;
4475 else if (IsScrollIncrement(gtk_adjustment_get_page_increment(adj
), diff
))
4477 eventType
= isDown
? wxEVT_SCROLL_PAGEDOWN
: wxEVT_SCROLL_PAGEUP
;
4479 else if (m_mouseButtonDown
)
4481 // Assume track event
4482 m_isScrolling
= true;
4488 void wxWindowGTK::ScrollWindow( int dx
, int dy
, const wxRect
* WXUNUSED(rect
) )
4490 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid window") );
4492 wxCHECK_RET( m_wxwindow
!= NULL
, wxT("window needs client area for scrolling") );
4494 // No scrolling requested.
4495 if ((dx
== 0) && (dy
== 0)) return;
4497 m_clipPaintRegion
= true;
4499 WX_PIZZA(m_wxwindow
)->scroll(dx
, dy
);
4501 m_clipPaintRegion
= false;
4504 bool restoreCaret
= (GetCaret() != NULL
&& GetCaret()->IsVisible());
4507 wxRect
caretRect(GetCaret()->GetPosition(), GetCaret()->GetSize());
4509 caretRect
.width
+= dx
;
4512 caretRect
.x
+= dx
; caretRect
.width
-= dx
;
4515 caretRect
.height
+= dy
;
4518 caretRect
.y
+= dy
; caretRect
.height
-= dy
;
4521 RefreshRect(caretRect
);
4523 #endif // wxUSE_CARET
4526 void wxWindowGTK::GTKScrolledWindowSetBorder(GtkWidget
* w
, int wxstyle
)
4528 //RN: Note that static controls usually have no border on gtk, so maybe
4529 //it makes sense to treat that as simply no border at the wx level
4531 if (!(wxstyle
& wxNO_BORDER
) && !(wxstyle
& wxBORDER_STATIC
))
4533 GtkShadowType gtkstyle
;
4535 if(wxstyle
& wxBORDER_RAISED
)
4536 gtkstyle
= GTK_SHADOW_OUT
;
4537 else if ((wxstyle
& wxBORDER_SUNKEN
) || (wxstyle
& wxBORDER_THEME
))
4538 gtkstyle
= GTK_SHADOW_IN
;
4541 else if (wxstyle
& wxBORDER_DOUBLE
)
4542 gtkstyle
= GTK_SHADOW_ETCHED_IN
;
4545 gtkstyle
= GTK_SHADOW_IN
;
4547 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW(w
),
4552 // Find the wxWindow at the current mouse position, also returning the mouse
4554 wxWindow
* wxFindWindowAtPointer(wxPoint
& pt
)
4556 pt
= wxGetMousePosition();
4557 wxWindow
* found
= wxFindWindowAtPoint(pt
);
4561 // Get the current mouse position.
4562 wxPoint
wxGetMousePosition()
4564 wxWindow
* tlw
= NULL
;
4565 if (!wxTopLevelWindows
.empty())
4566 tlw
= wxTopLevelWindows
.front();
4567 GdkDisplay
* display
;
4568 if (tlw
&& tlw
->m_widget
)
4569 display
= gtk_widget_get_display(tlw
->m_widget
);
4571 display
= gdk_display_get_default();
4574 gdk_display_get_pointer(display
, NULL
, &x
, &y
, NULL
);
4575 return wxPoint(x
, y
);
4578 GdkWindow
* wxWindowGTK::GTKGetDrawingWindow() const
4580 GdkWindow
* window
= NULL
;
4582 window
= gtk_widget_get_window(m_wxwindow
);
4586 // ----------------------------------------------------------------------------
4588 // ----------------------------------------------------------------------------
4593 // this is called if we attempted to freeze unrealized widget when it finally
4594 // is realized (and so can be frozen):
4595 static void wx_frozen_widget_realize(GtkWidget
* w
, wxWindowGTK
* win
)
4597 wxASSERT( w
&& gtk_widget_get_has_window(w
) );
4598 wxASSERT( gtk_widget_get_realized(w
) );
4600 g_signal_handlers_disconnect_by_func
4603 (void*)wx_frozen_widget_realize
,
4608 if (w
== win
->m_wxwindow
)
4609 window
= win
->GTKGetDrawingWindow();
4611 window
= gtk_widget_get_window(w
);
4612 gdk_window_freeze_updates(window
);
4617 void wxWindowGTK::GTKFreezeWidget(GtkWidget
*w
)
4619 if ( !w
|| !gtk_widget_get_has_window(w
) )
4620 return; // window-less widget, cannot be frozen
4622 GdkWindow
* window
= gtk_widget_get_window(w
);
4625 // we can't thaw unrealized widgets because they don't have GdkWindow,
4626 // so set it up to be done immediately after realization:
4627 g_signal_connect_after
4631 G_CALLBACK(wx_frozen_widget_realize
),
4637 if (w
== m_wxwindow
)
4638 window
= GTKGetDrawingWindow();
4639 gdk_window_freeze_updates(window
);
4642 void wxWindowGTK::GTKThawWidget(GtkWidget
*w
)
4644 if ( !w
|| !gtk_widget_get_has_window(w
) )
4645 return; // window-less widget, cannot be frozen
4647 GdkWindow
* window
= gtk_widget_get_window(w
);
4650 // the widget wasn't realized yet, no need to thaw
4651 g_signal_handlers_disconnect_by_func
4654 (void*)wx_frozen_widget_realize
,
4660 if (w
== m_wxwindow
)
4661 window
= GTKGetDrawingWindow();
4662 gdk_window_thaw_updates(window
);
4665 void wxWindowGTK::DoFreeze()
4667 GTKFreezeWidget(m_widget
);
4668 if ( m_wxwindow
&& m_widget
!= m_wxwindow
)
4669 GTKFreezeWidget(m_wxwindow
);
4672 void wxWindowGTK::DoThaw()
4674 GTKThawWidget(m_widget
);
4675 if ( m_wxwindow
&& m_widget
!= m_wxwindow
)
4676 GTKThawWidget(m_wxwindow
);