window.h 17 KB

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