refactoring/simplification of code dealing with scrollbars and their events
[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 __GTKWINDOWH__
11 #define __GTKWINDOWH__
12
13 // helper structure that holds class that holds GtkIMContext object and
14 // some additional data needed for key events processing
15 struct wxGtkIMData;
16
17 //-----------------------------------------------------------------------------
18 // callback definition for inserting a window (internal)
19 //-----------------------------------------------------------------------------
20
21 class WXDLLIMPEXP_CORE wxWindowGTK;
22 typedef void (*wxInsertChildFunction)( wxWindowGTK*, wxWindowGTK* );
23
24 //-----------------------------------------------------------------------------
25 // wxWindowGTK
26 //-----------------------------------------------------------------------------
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
48 // implement base class (pure) virtual methods
49 // -------------------------------------------
50
51 virtual void SetLabel(const wxString& WXUNUSED(label)) { }
52 virtual wxString GetLabel() const { return wxEmptyString; }
53
54 virtual bool Destroy();
55
56 virtual void Raise();
57 virtual void Lower();
58
59 virtual bool Show( bool show = true );
60 virtual bool Enable( bool enable = true );
61
62 virtual void SetWindowStyleFlag( long style );
63
64 virtual bool IsRetained() const;
65
66 virtual void SetFocus();
67 virtual bool AcceptsFocus() const;
68
69 virtual bool Reparent( wxWindowBase *newParent );
70
71 virtual void WarpPointer(int x, int y);
72
73 virtual void Refresh( bool eraseBackground = true,
74 const wxRect *rect = (const wxRect *) NULL );
75 virtual void Update();
76 virtual void ClearBackground();
77
78 virtual bool SetBackgroundColour( const wxColour &colour );
79 virtual bool SetForegroundColour( const wxColour &colour );
80 virtual bool SetCursor( const wxCursor &cursor );
81 virtual bool SetFont( const wxFont &font );
82
83 virtual bool SetBackgroundStyle(wxBackgroundStyle style) ;
84
85 virtual int GetCharHeight() const;
86 virtual int GetCharWidth() const;
87 virtual void GetTextExtent(const wxString& string,
88 int *x, int *y,
89 int *descent = (int *) NULL,
90 int *externalLeading = (int *) NULL,
91 const wxFont *theFont = (const wxFont *) NULL)
92 const;
93
94 virtual void SetScrollbar( int orient, int pos, int thumbVisible,
95 int range, bool refresh = true );
96 virtual void SetScrollPos( int orient, int pos, bool refresh = true );
97 virtual int GetScrollPos( int orient ) const;
98 virtual int GetScrollThumb( int orient ) const;
99 virtual int GetScrollRange( int orient ) const;
100 virtual void ScrollWindow( int dx, int dy,
101 const wxRect* rect = (wxRect *) NULL );
102 virtual bool ScrollLines(int lines);
103 virtual bool ScrollPages(int pages);
104
105 #if wxUSE_DRAG_AND_DROP
106 virtual void SetDropTarget( wxDropTarget *dropTarget );
107 #endif // wxUSE_DRAG_AND_DROP
108
109 virtual void AddChild( wxWindowBase *child );
110 virtual void RemoveChild( wxWindowBase *child );
111
112 // currently wxGTK2-only
113 void SetDoubleBuffered(bool on);
114
115 // implementation
116 // --------------
117
118 virtual WXWidget GetHandle() const { return m_widget; }
119
120 // I don't want users to override what's done in idle so everything that
121 // has to be done in idle time in order for wxGTK to work is done in
122 // OnInternalIdle
123 virtual void OnInternalIdle();
124
125 // Internal represention of Update()
126 void GtkUpdate();
127
128 // For compatibility across platforms (not in event table)
129 void OnIdle(wxIdleEvent& WXUNUSED(event)) {}
130
131 // wxGTK-specific: called recursively by Enable,
132 // to give widgets an opportunity to correct their colours after they
133 // have been changed by Enable
134 virtual void OnParentEnable( bool WXUNUSED(enable) ) {}
135
136 // Used by all window classes in the widget creation process.
137 bool PreCreation( wxWindowGTK *parent, const wxPoint &pos, const wxSize &size );
138 void PostCreation();
139
140 // Internal addition of child windows. differs from class
141 // to class not by using virtual functions but by using
142 // the m_insertCallback.
143 void DoAddChild(wxWindowGTK *child);
144
145 // This methods sends wxPaintEvents to the window. It reads the
146 // update region, breaks it up into rects and sends an event
147 // for each rect. It is also responsible for background erase
148 // events and NC paint events. It is called from "draw" and
149 // "expose" handlers as well as from ::Update()
150 void GtkSendPaintEvents();
151
152 // The methods below are required because many native widgets
153 // are composed of several subwidgets and setting a style for
154 // the widget means setting it for all subwidgets as well.
155 // also, it is not clear which native widget is the top
156 // widget where (most of) the input goes. even tooltips have
157 // to be applied to all subwidgets.
158 virtual GtkWidget* GetConnectWidget();
159 virtual bool IsOwnGtkWindow( GdkWindow *window );
160 void ConnectWidget( GtkWidget *widget );
161
162 protected:
163 // Override GTKWidgetNeedsMnemonic and return true if your
164 // needs to set its mnemonic widget, such as for a
165 // GtkLabel for wxStaticText, then do the actual
166 // setting of the widget inside GTKWidgetDoSetMnemonic
167 virtual bool GTKWidgetNeedsMnemonic() const;
168 virtual void GTKWidgetDoSetMnemonic(GtkWidget* w);
169
170 public:
171 // Returns the default context which usually is anti-aliased
172 PangoContext *GtkGetPangoDefaultContext();
173
174 #if wxUSE_TOOLTIPS
175 virtual void ApplyToolTip( GtkTooltips *tips, const wxChar *tip );
176 #endif // wxUSE_TOOLTIPS
177
178 // Called from GTK signal handlers. it indicates that
179 // the layouting functions have to be called later on
180 // (i.e. in idle time, implemented in OnInternalIdle() ).
181 void GtkUpdateSize() { m_sizeSet = false; }
182
183 // fix up the mouse event coords, used by wxListBox only so far
184 virtual void FixUpMouseEvent(GtkWidget * WXUNUSED(widget),
185 wxCoord& WXUNUSED(x),
186 wxCoord& WXUNUSED(y)) { }
187
188 // is this window transparent for the mouse events (as wxStaticBox is)?
189 virtual bool IsTransparentForMouse() const { return false; }
190
191 // is this a radiobutton (used by radiobutton code itself only)?
192 virtual bool IsRadioButton() const { return false; }
193
194 // Common scroll event handling code for wxWindow and wxScrollBar
195 wxEventType GetScrollEventType(GtkRange* range);
196
197 void BlockScrollEvent();
198 void UnblockScrollEvent();
199
200 // position and size of the window
201 int m_x, m_y;
202 int m_width, m_height;
203 int m_oldClientWidth,m_oldClientHeight;
204
205 // see the docs in src/gtk/window.cpp
206 GtkWidget *m_widget; // mostly the widget seen by the rest of GTK
207 GtkWidget *m_wxwindow; // mostly the client area as per wxWidgets
208
209 // this widget will be queried for GTK's focus events
210 GtkWidget *m_focusWidget;
211
212 wxGtkIMData *m_imData;
213
214
215 // indices for the arrays below
216 enum ScrollDir { ScrollDir_Horz, ScrollDir_Vert, ScrollDir_Max };
217
218 // horizontal/vertical scroll bar
219 GtkRange* m_scrollBar[ScrollDir_Max];
220
221 // horizontal/vertical scroll position
222 double m_scrollPos[ScrollDir_Max];
223
224 // if true, don't notify about adjustment change (without resetting the
225 // flag, so this has to be done manually)
226 bool m_blockValueChanged[ScrollDir_Max];
227
228 // return the scroll direction index corresponding to the given orientation
229 // (which is wxVERTICAL or wxHORIZONTAL)
230 static ScrollDir ScrollDirFromOrient(int orient)
231 {
232 return orient == wxVERTICAL ? ScrollDir_Vert : ScrollDir_Horz;
233 }
234
235 // return the orientation for the given scrolling direction
236 static int OrientFromScrollDir(ScrollDir dir)
237 {
238 return dir == ScrollDir_Horz ? wxHORIZONTAL : wxVERTICAL;
239 }
240
241 // find the direction of the given scrollbar (must be one of ours)
242 ScrollDir ScrollDirFromRange(GtkRange *range) const;
243
244
245 // extra (wxGTK-specific) flags
246 bool m_needParent:1; // ! wxFrame, wxDialog, wxNotebookPage ?
247 bool m_noExpose:1; // wxGLCanvas has its own redrawing
248 bool m_nativeSizeEvent:1; // wxGLCanvas sends wxSizeEvent upon "alloc_size"
249 bool m_hasScrolling:1;
250 bool m_hasVMT:1;
251 bool m_sizeSet:1;
252 bool m_resizing:1;
253 bool m_acceptsFocus:1; // true if not static
254 bool m_hasFocus:1; // true if == FindFocus()
255 bool m_isScrolling:1; // dragging scrollbar thumb?
256 bool m_clipPaintRegion:1; // true after ScrollWindow()
257 bool m_dirtyTabOrder:1; // tab order changed, GTK focus
258 // chain needs update
259 bool m_needsStyleChange:1; // May not be able to change
260 // background style until OnIdle
261 bool m_mouseButtonDown:1;
262 bool m_blockScrollEvent:1;
263
264 // C++ has no virtual methods in the constrcutor of any class but we need
265 // different methods of inserting a child window into a wxFrame,
266 // wxMDIFrame, wxNotebook etc. this is the callback that will get used.
267 wxInsertChildFunction m_insertCallback;
268
269 protected:
270 // implement the base class pure virtuals
271 virtual void DoClientToScreen( int *x, int *y ) const;
272 virtual void DoScreenToClient( int *x, int *y ) const;
273 virtual void DoGetPosition( int *x, int *y ) const;
274 virtual void DoGetSize( int *width, int *height ) const;
275 virtual void DoGetClientSize( int *width, int *height ) const;
276 virtual void DoSetSize(int x, int y,
277 int width, int height,
278 int sizeFlags = wxSIZE_AUTO);
279 virtual void DoSetClientSize(int width, int height);
280 virtual void DoMoveWindow(int x, int y, int width, int height);
281
282 #if wxUSE_MENUS_NATIVE
283 virtual bool DoPopupMenu( wxMenu *menu, int x, int y );
284 #endif // wxUSE_MENUS_NATIVE
285
286 virtual void DoCaptureMouse();
287 virtual void DoReleaseMouse();
288
289 #if wxUSE_TOOLTIPS
290 virtual void DoSetToolTip( wxToolTip *tip );
291 #endif // wxUSE_TOOLTIPS
292
293 // common part of all ctors (not virtual because called from ctor)
294 void Init();
295
296 virtual void DoMoveInTabOrder(wxWindow *win, MoveKind move);
297
298 // Copies m_children tab order to GTK focus chain:
299 void RealizeTabOrder();
300
301 // Called by ApplyWidgetStyle (which is called by SetFont() and
302 // SetXXXColour etc to apply style changed to native widgets) to create
303 // modified GTK style with non-standard attributes. If forceStyle=true,
304 // creates empty GtkRcStyle if there are no modifications, otherwise
305 // returns NULL in such case.
306 GtkRcStyle *CreateWidgetStyle(bool forceStyle = false);
307
308 // Overridden in many GTK widgets who have to handle subwidgets
309 virtual void ApplyWidgetStyle(bool forceStyle = false);
310
311 // helper function to ease native widgets wrapping, called by
312 // ApplyWidgetStyle -- override this, not ApplyWidgetStyle
313 virtual void DoApplyWidgetStyle(GtkRcStyle *style);
314
315 // sets the border of a given GtkScrolledWindow from a wx style
316 static void GtkScrolledWindowSetBorder(GtkWidget* w, int style);
317
318 private:
319 enum ScrollUnit { ScrollUnit_Line, ScrollUnit_Page, ScrollUnit_Max };
320
321 // common part of ScrollLines() and ScrollPages() and could be used, in the
322 // future, for horizontal scrolling as well
323 //
324 // return true if we scrolled, false otherwise (on error or simply if we
325 // are already at the end)
326 bool DoScrollByUnits(ScrollDir dir, ScrollUnit unit, int units);
327
328
329 DECLARE_DYNAMIC_CLASS(wxWindowGTK)
330 DECLARE_NO_COPY_CLASS(wxWindowGTK)
331 };
332
333 extern WXDLLIMPEXP_CORE wxWindow *wxFindFocusedChild(wxWindowGTK *win);
334
335 #endif // __GTKWINDOWH__