Refactor wxGTK IM-related code to allow future modifications.
[wxWidgets.git] / include / wx / gtk / window.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/gtk/window.h
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifndef _WX_GTK_WINDOW_H_
11 #define _WX_GTK_WINDOW_H_
12
13 #include "wx/dynarray.h"
14
15 #ifdef __WXGTK3__
16 typedef struct _cairo cairo_t;
17 typedef struct _GtkStyleProvider GtkStyleProvider;
18 #define WXUNUSED_IN_GTK3(x)
19 #else
20 #define WXUNUSED_IN_GTK3(x) x
21 #endif
22
23 // helper structure that holds class that holds GtkIMContext object and
24 // some additional data needed for key events processing
25 struct wxGtkIMData;
26
27 typedef struct _GdkEventKey GdkEventKey;
28 typedef struct _GtkIMContext GtkIMContext;
29
30 WX_DEFINE_EXPORTED_ARRAY_PTR(GdkWindow *, wxArrayGdkWindows);
31
32 extern "C"
33 {
34
35 typedef void (*wxGTKCallback)();
36
37 }
38
39 //-----------------------------------------------------------------------------
40 // wxWindowGTK
41 //-----------------------------------------------------------------------------
42
43 class WXDLLIMPEXP_CORE wxWindowGTK : public wxWindowBase
44 {
45 public:
46 // creating the window
47 // -------------------
48 wxWindowGTK();
49 wxWindowGTK(wxWindow *parent,
50 wxWindowID id,
51 const wxPoint& pos = wxDefaultPosition,
52 const wxSize& size = wxDefaultSize,
53 long style = 0,
54 const wxString& name = wxPanelNameStr);
55 bool Create(wxWindow *parent,
56 wxWindowID id,
57 const wxPoint& pos = wxDefaultPosition,
58 const wxSize& size = wxDefaultSize,
59 long style = 0,
60 const wxString& name = wxPanelNameStr);
61 virtual ~wxWindowGTK();
62
63 // implement base class (pure) virtual methods
64 // -------------------------------------------
65
66 virtual void Raise();
67 virtual void Lower();
68
69 virtual bool Show( bool show = true );
70
71 virtual bool IsRetained() const;
72
73 virtual void SetFocus();
74
75 // hint from wx to native GTK+ tab traversal code
76 virtual void SetCanFocus(bool canFocus);
77
78 virtual bool Reparent( wxWindowBase *newParent );
79
80 virtual void WarpPointer(int x, int y);
81
82 virtual void Refresh( bool eraseBackground = true,
83 const wxRect *rect = (const wxRect *) NULL );
84 virtual void Update();
85 virtual void ClearBackground();
86
87 virtual bool SetBackgroundColour( const wxColour &colour );
88 virtual bool SetForegroundColour( const wxColour &colour );
89 virtual bool SetCursor( const wxCursor &cursor );
90 virtual bool SetFont( const wxFont &font );
91
92 virtual bool SetBackgroundStyle(wxBackgroundStyle style) ;
93 virtual bool IsTransparentBackgroundSupported(wxString* reason = NULL) const;
94
95 virtual int GetCharHeight() const;
96 virtual int GetCharWidth() const;
97
98 virtual void SetScrollbar( int orient, int pos, int thumbVisible,
99 int range, bool refresh = true );
100 virtual void SetScrollPos( int orient, int pos, bool refresh = true );
101 virtual int GetScrollPos( int orient ) const;
102 virtual int GetScrollThumb( int orient ) const;
103 virtual int GetScrollRange( int orient ) const;
104 virtual void ScrollWindow( int dx, int dy,
105 const wxRect* rect = NULL );
106 virtual bool ScrollLines(int lines);
107 virtual bool ScrollPages(int pages);
108
109 #if wxUSE_DRAG_AND_DROP
110 virtual void SetDropTarget( wxDropTarget *dropTarget );
111 #endif // wxUSE_DRAG_AND_DROP
112
113 virtual void AddChild( wxWindowBase *child );
114 virtual void RemoveChild( wxWindowBase *child );
115
116 virtual void SetLayoutDirection(wxLayoutDirection dir);
117 virtual wxLayoutDirection GetLayoutDirection() const;
118 virtual wxCoord AdjustForLayoutDirection(wxCoord x,
119 wxCoord width,
120 wxCoord widthTotal) const;
121
122 virtual bool DoIsExposed( int x, int y ) const;
123 virtual bool DoIsExposed( int x, int y, int w, int h ) const;
124
125 // currently wxGTK2-only
126 void SetDoubleBuffered(bool on);
127 virtual bool IsDoubleBuffered() const;
128
129 // SetLabel(), which does nothing in wxWindow
130 virtual void SetLabel(const wxString& label) { m_gtkLabel = label; }
131 virtual wxString GetLabel() const { return m_gtkLabel; }
132
133 // implementation
134 // --------------
135
136 virtual WXWidget GetHandle() const { return m_widget; }
137
138 // many important things are done here, this function must be called
139 // regularly
140 virtual void OnInternalIdle();
141
142 // For compatibility across platforms (not in event table)
143 void OnIdle(wxIdleEvent& WXUNUSED(event)) {}
144
145 // Used by all window classes in the widget creation process.
146 bool PreCreation( wxWindowGTK *parent, const wxPoint &pos, const wxSize &size );
147 void PostCreation();
148
149 // Internal addition of child windows
150 void DoAddChild(wxWindowGTK *child);
151
152 // This method sends wxPaintEvents to the window.
153 // It is also responsible for background erase events.
154 #ifdef __WXGTK3__
155 void GTKSendPaintEvents(cairo_t* cr);
156 #else
157 void GTKSendPaintEvents(const GdkRegion* region);
158 #endif
159
160 // The methods below are required because many native widgets
161 // are composed of several subwidgets and setting a style for
162 // the widget means setting it for all subwidgets as well.
163 // also, it is not clear which native widget is the top
164 // widget where (most of) the input goes. even tooltips have
165 // to be applied to all subwidgets.
166 virtual GtkWidget* GetConnectWidget();
167 void ConnectWidget( GtkWidget *widget );
168
169 // Called from several event handlers, if it returns true or false, the
170 // same value should be immediately returned by the handler without doing
171 // anything else. If it returns -1, the handler should continue as usual
172 int GTKCallbackCommonPrologue(struct _GdkEventAny *event) const;
173
174 // Simplified form of GTKCallbackCommonPrologue() which can be used from
175 // GTK callbacks without return value to check if the event should be
176 // ignored: if this returns true, the event shouldn't be handled
177 bool GTKShouldIgnoreEvent() const;
178
179
180 // override this if some events should never be consumed by wxWidgets but
181 // but have to be left for the native control
182 //
183 // base version just calls HandleWindowEvent()
184 virtual bool GTKProcessEvent(wxEvent& event) const;
185
186 // Map GTK widget direction of the given widget to/from wxLayoutDirection
187 static wxLayoutDirection GTKGetLayout(GtkWidget *widget);
188 static void GTKSetLayout(GtkWidget *widget, wxLayoutDirection dir);
189
190 // This is called when capture is taken from the window. It will
191 // fire off capture lost events.
192 void GTKReleaseMouseAndNotify();
193
194 GdkWindow* GTKGetDrawingWindow() const;
195
196 bool GTKHandleFocusIn();
197 bool GTKHandleFocusOut();
198 void GTKHandleFocusOutNoDeferring();
199 static void GTKHandleDeferredFocusOut();
200
201 // Called when m_widget becomes realized. Derived classes must call the
202 // base class method if they override it.
203 virtual void GTKHandleRealized();
204 void GTKHandleUnrealize();
205
206 protected:
207 // for controls composed of multiple GTK widgets, return true to eliminate
208 // spurious focus events if the focus changes between GTK+ children within
209 // the same wxWindow
210 virtual bool GTKNeedsToFilterSameWindowFocus() const { return false; }
211
212 // Override GTKWidgetNeedsMnemonic and return true if your
213 // needs to set its mnemonic widget, such as for a
214 // GtkLabel for wxStaticText, then do the actual
215 // setting of the widget inside GTKWidgetDoSetMnemonic
216 virtual bool GTKWidgetNeedsMnemonic() const;
217 virtual void GTKWidgetDoSetMnemonic(GtkWidget* w);
218
219 // Get the GdkWindows making part of this window: usually there will be
220 // only one of them in which case it should be returned directly by this
221 // function. If there is more than one GdkWindow (can be the case for
222 // composite widgets), return NULL and fill in the provided array
223 //
224 // This is not pure virtual for backwards compatibility but almost
225 // certainly must be overridden in any wxControl-derived class!
226 virtual GdkWindow *GTKGetWindow(wxArrayGdkWindows& windows) const;
227
228 // Check if the given window makes part of this widget
229 bool GTKIsOwnWindow(GdkWindow *window) const;
230
231 public:
232 // Returns the default context which usually is anti-aliased
233 PangoContext *GTKGetPangoDefaultContext();
234
235 #if wxUSE_TOOLTIPS
236 // applies tooltip to the widget (tip must be UTF-8 encoded)
237 virtual void GTKApplyToolTip(const char* tip);
238 #endif // wxUSE_TOOLTIPS
239
240 // Called when a window should delay showing itself
241 // until idle time used in Reparent().
242 void GTKShowOnIdle() { m_showOnIdle = true; }
243
244 // This is called from the various OnInternalIdle methods
245 bool GTKShowFromOnIdle();
246
247 // is this window transparent for the mouse events (as wxStaticBox is)?
248 virtual bool GTKIsTransparentForMouse() const { return false; }
249
250 // Common scroll event handling code for wxWindow and wxScrollBar
251 wxEventType GTKGetScrollEventType(GtkRange* range);
252
253 // position and size of the window
254 int m_x, m_y;
255 int m_width, m_height;
256 int m_clientWidth, m_clientHeight;
257 // Whether the client size variables above are known to be correct
258 // (because they have been validated by a size-allocate) and should
259 // be used to report client size
260 bool m_useCachedClientSize;
261
262 // see the docs in src/gtk/window.cpp
263 GtkWidget *m_widget; // mostly the widget seen by the rest of GTK
264 GtkWidget *m_wxwindow; // mostly the client area as per wxWidgets
265
266 // label for use with GetLabelSetLabel
267 wxString m_gtkLabel;
268
269 // return true if the window is of a standard (i.e. not wxWidgets') class
270 bool IsOfStandardClass() const { return m_wxwindow == NULL; }
271
272 // this widget will be queried for GTK's focus events
273 GtkWidget *m_focusWidget;
274
275 void GTKDisableFocusOutEvent();
276 void GTKEnableFocusOutEvent();
277
278
279 // Input method support
280
281 // The IM context used for generic, i.e. non-native, windows.
282 //
283 // It might be a good idea to avoid allocating it unless key events from
284 // this window are really needed but currently we do it unconditionally.
285 //
286 // For native widgets (i.e. those for which IsOfStandardClass() returns
287 // true) it is NULL.
288 GtkIMContext* m_imContext;
289
290 // Pointer to the event being currently processed by the IME or NULL if not
291 // inside key handling.
292 GdkEventKey* m_imKeyEvent;
293
294 // This method generalizes gtk_im_context_filter_keypress(): for the
295 // generic windows it does just that but it's overridden by the classes
296 // wrapping native widgets that use IM themselves and so provide specific
297 // methods for accessing it such gtk_entry_im_context_filter_keypress().
298 virtual int GTKIMFilterKeypress(GdkEventKey* event) const;
299
300
301 // indices for the arrays below
302 enum ScrollDir { ScrollDir_Horz, ScrollDir_Vert, ScrollDir_Max };
303
304 // horizontal/vertical scroll bar
305 GtkRange* m_scrollBar[ScrollDir_Max];
306
307 // horizontal/vertical scroll position
308 double m_scrollPos[ScrollDir_Max];
309
310 // return the scroll direction index corresponding to the given orientation
311 // (which is wxVERTICAL or wxHORIZONTAL)
312 static ScrollDir ScrollDirFromOrient(int orient)
313 {
314 return orient == wxVERTICAL ? ScrollDir_Vert : ScrollDir_Horz;
315 }
316
317 // return the orientation for the given scrolling direction
318 static int OrientFromScrollDir(ScrollDir dir)
319 {
320 return dir == ScrollDir_Horz ? wxHORIZONTAL : wxVERTICAL;
321 }
322
323 // find the direction of the given scrollbar (must be one of ours)
324 ScrollDir ScrollDirFromRange(GtkRange *range) const;
325
326 // set the current cursor for all GdkWindows making part of this widget
327 // (see GTKGetWindow)
328 void GTKUpdateCursor(bool update_self = true, bool recurse = true);
329
330 // extra (wxGTK-specific) flags
331 bool m_noExpose:1; // wxGLCanvas has its own redrawing
332 bool m_nativeSizeEvent:1; // wxGLCanvas sends wxSizeEvent upon "alloc_size"
333 bool m_isScrolling:1; // dragging scrollbar thumb?
334 bool m_clipPaintRegion:1; // true after ScrollWindow()
335 wxRegion m_nativeUpdateRegion; // not transformed for RTL
336 bool m_dirtyTabOrder:1; // tab order changed, GTK focus
337 // chain needs update
338 bool m_mouseButtonDown:1;
339
340 bool m_showOnIdle:1; // postpone showing the window until idle
341
342 protected:
343 // implement the base class pure virtuals
344 virtual void DoGetTextExtent(const wxString& string,
345 int *x, int *y,
346 int *descent = NULL,
347 int *externalLeading = NULL,
348 const wxFont *font = NULL) const;
349 virtual void DoClientToScreen( int *x, int *y ) const;
350 virtual void DoScreenToClient( int *x, int *y ) const;
351 virtual void DoGetPosition( int *x, int *y ) const;
352 virtual void DoGetSize( int *width, int *height ) const;
353 virtual void DoGetClientSize( int *width, int *height ) const;
354 virtual void DoSetSize(int x, int y,
355 int width, int height,
356 int sizeFlags = wxSIZE_AUTO);
357 virtual void DoSetClientSize(int width, int height);
358 virtual wxSize DoGetBorderSize() const;
359 virtual void DoMoveWindow(int x, int y, int width, int height);
360 virtual void DoEnable(bool enable);
361
362 #if wxUSE_MENUS_NATIVE
363 virtual bool DoPopupMenu( wxMenu *menu, int x, int y );
364 #endif // wxUSE_MENUS_NATIVE
365
366 virtual void DoCaptureMouse();
367 virtual void DoReleaseMouse();
368
369 virtual void DoFreeze();
370 virtual void DoThaw();
371
372 void GTKFreezeWidget(GtkWidget *w);
373 void GTKThawWidget(GtkWidget *w);
374 void GTKDisconnect(void* instance);
375
376 #if wxUSE_TOOLTIPS
377 virtual void DoSetToolTip( wxToolTip *tip );
378 #endif // wxUSE_TOOLTIPS
379
380 // Create a GtkScrolledWindow containing the given widget (usually
381 // m_wxwindow but not necessarily) and assigns it to m_widget. Also shows
382 // the widget passed to it.
383 //
384 // Can be only called if we have either wxHSCROLL or wxVSCROLL in our
385 // style.
386 void GTKCreateScrolledWindowWith(GtkWidget* view);
387
388 virtual void DoMoveInTabOrder(wxWindow *win, WindowOrder move);
389 virtual bool DoNavigateIn(int flags);
390
391
392 // Copies m_children tab order to GTK focus chain:
393 void RealizeTabOrder();
394
395 #ifndef __WXGTK3__
396 // Called by ApplyWidgetStyle (which is called by SetFont() and
397 // SetXXXColour etc to apply style changed to native widgets) to create
398 // modified GTK style with non-standard attributes.
399 GtkRcStyle* GTKCreateWidgetStyle();
400 #endif
401
402 void GTKApplyWidgetStyle(bool forceStyle = false);
403
404 // helper function to ease native widgets wrapping, called by
405 // ApplyWidgetStyle -- override this, not ApplyWidgetStyle
406 virtual void DoApplyWidgetStyle(GtkRcStyle *style);
407
408 void GTKApplyStyle(GtkWidget* widget, GtkRcStyle* style);
409
410 // sets the border of a given GtkScrolledWindow from a wx style
411 static void GTKScrolledWindowSetBorder(GtkWidget* w, int style);
412
413 // Connect the given function to the specified signal on m_widget.
414 //
415 // This is just a wrapper for g_signal_connect() and returns the handler id
416 // just as it does.
417 unsigned long GTKConnectWidget(const char *signal, wxGTKCallback callback);
418
419 void ConstrainSize();
420
421 private:
422 void Init();
423
424 // return true if this window must have a non-NULL parent, false if it can
425 // be created without parent (normally only top level windows but in wxGTK
426 // there is also the exception of wxMenuBar)
427 virtual bool GTKNeedsParent() const { return !IsTopLevel(); }
428
429 enum ScrollUnit { ScrollUnit_Line, ScrollUnit_Page, ScrollUnit_Max };
430
431 // common part of ScrollLines() and ScrollPages() and could be used, in the
432 // future, for horizontal scrolling as well
433 //
434 // return true if we scrolled, false otherwise (on error or simply if we
435 // are already at the end)
436 bool DoScrollByUnits(ScrollDir dir, ScrollUnit unit, int units);
437 virtual void AddChildGTK(wxWindowGTK* child);
438
439 #ifdef __WXGTK3__
440 // paint context is stashed here so wxPaintDC can use it
441 cairo_t* m_paintContext;
442 // style provider for "background-image"
443 GtkStyleProvider* m_styleProvider;
444
445 public:
446 cairo_t* GTKPaintContext() const
447 {
448 return m_paintContext;
449 }
450 #endif
451
452 DECLARE_DYNAMIC_CLASS(wxWindowGTK)
453 wxDECLARE_NO_COPY_CLASS(wxWindowGTK);
454 };
455
456 #endif // _WX_GTK_WINDOW_H_