Correct orientation of splitter sash again.
[wxWidgets.git] / src / gtk / renderer.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/renderer.cpp
3 // Purpose: implementation of wxRendererNative for wxGTK
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 20.07.2003
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/renderer.h"
28 #include <gtk/gtk.h>
29 #include "wx/gtk/win_gtk.h"
30
31 #include "wx/window.h"
32 #include "wx/dc.h"
33 #include "wx/dcclient.h"
34
35 #ifdef __WXGTK20__
36 #include "wx/settings.h"
37 #endif // GTK 2.0
38
39 #ifdef __WXGTK20__
40 #define WXUNUSED_IN_GTK1(arg) arg
41 #else
42 #define WXUNUSED_IN_GTK1(arg)
43 #endif
44
45 // ----------------------------------------------------------------------------
46 // wxRendererGTK: our wxRendererNative implementation
47 // ----------------------------------------------------------------------------
48
49 class WXDLLEXPORT wxRendererGTK : public wxDelegateRendererNative
50 {
51 public:
52 // draw the header control button (used by wxListCtrl)
53 virtual void DrawHeaderButton(wxWindow *win,
54 wxDC& dc,
55 const wxRect& rect,
56 int flags = 0);
57
58 #ifdef __WXGTK20__
59 // draw the expanded/collapsed icon for a tree control item
60 virtual void DrawTreeItemButton(wxWindow *win,
61 wxDC& dc,
62 const wxRect& rect,
63 int flags = 0);
64 #endif // GTK+ 2.0
65
66 virtual void DrawSplitterBorder(wxWindow *win,
67 wxDC& dc,
68 const wxRect& rect,
69 int flags = 0);
70 virtual void DrawSplitterSash(wxWindow *win,
71 wxDC& dc,
72 const wxSize& size,
73 wxCoord position,
74 wxOrientation orient,
75 int flags = 0);
76
77 virtual void DrawComboBoxDropButton(wxWindow *win,
78 wxDC& dc,
79 const wxRect& rect,
80 int flags = 0);
81
82 virtual wxSplitterRenderParams GetSplitterParams(const wxWindow *win);
83
84 private:
85 // FIXME: shouldn't we destroy these windows somewhere?
86
87 // used by DrawHeaderButton and DrawComboBoxDropButton
88 static GtkWidget *GetButtonWidget();
89
90 #ifdef __WXGTK20__
91 // used by DrawTreeItemButton()
92 static GtkWidget *GetTreeWidget();
93 #endif // GTK+ 2.0
94 };
95
96 // ============================================================================
97 // implementation
98 // ============================================================================
99
100 /* static */
101 wxRendererNative& wxRendererNative::GetDefault()
102 {
103 static wxRendererGTK s_rendererGTK;
104
105 return s_rendererGTK;
106 }
107
108 // ----------------------------------------------------------------------------
109 // helper functions
110 // ----------------------------------------------------------------------------
111
112 GtkWidget *
113 wxRendererGTK::GetButtonWidget()
114 {
115 static GtkWidget *s_button = NULL;
116 static GtkWidget *s_window = NULL;
117
118 if ( !s_button )
119 {
120 s_window = gtk_window_new( GTK_WINDOW_POPUP );
121 gtk_widget_realize( s_window );
122 s_button = gtk_button_new();
123 gtk_container_add( GTK_CONTAINER(s_window), s_button );
124 gtk_widget_realize( s_button );
125 }
126
127 return s_button;
128 }
129
130 #ifdef __WXGTK20__
131
132 GtkWidget *
133 wxRendererGTK::GetTreeWidget()
134 {
135 static GtkWidget *s_tree = NULL;
136 static GtkWidget *s_window = NULL;
137
138 if ( !s_tree )
139 {
140 s_tree = gtk_tree_view_new();
141 s_window = gtk_window_new( GTK_WINDOW_POPUP );
142 gtk_widget_realize( s_window );
143 gtk_container_add( GTK_CONTAINER(s_window), s_tree );
144 gtk_widget_realize( s_tree );
145 }
146
147 return s_tree;
148 }
149
150 #endif // GTK+ 2.0
151
152 // ----------------------------------------------------------------------------
153 // list/tree controls drawing
154 // ----------------------------------------------------------------------------
155
156 void
157 wxRendererGTK::DrawHeaderButton(wxWindow *win,
158 wxDC& dc,
159 const wxRect& rect,
160 int flags)
161 {
162
163 GtkWidget *button = GetButtonWidget();
164
165 gtk_paint_box
166 (
167 button->style,
168 // FIXME: I suppose GTK_PIZZA(win->m_wxwindow)->bin_window doesn't work with wxMemoryDC.
169 // Maybe use code similar as in DrawComboBoxDropButton below?
170 GTK_PIZZA(win->m_wxwindow)->bin_window,
171 flags & wxCONTROL_DISABLED ? GTK_STATE_INSENSITIVE : GTK_STATE_NORMAL,
172 GTK_SHADOW_OUT,
173 NULL,
174 button,
175 "button",
176 dc.XLOG2DEV(rect.x) -1, rect.y -1, rect.width +2, rect.height +2
177 );
178 }
179
180 #ifdef __WXGTK20__
181
182 // draw a ">" or "v" button
183 void
184 wxRendererGTK::DrawTreeItemButton(wxWindow* win,
185 wxDC& dc, const wxRect& rect, int flags)
186 {
187 GtkWidget *tree = GetTreeWidget();
188
189 GtkStateType state;
190 if ( flags & wxCONTROL_CURRENT )
191 state = GTK_STATE_PRELIGHT;
192 else
193 state = GTK_STATE_NORMAL;
194
195 // VZ: I don't know how to get the size of the expander so as to centre it
196 // in the given rectangle, +2/3 below is just what looks good here...
197 gtk_paint_expander
198 (
199 tree->style,
200 GTK_PIZZA(win->m_wxwindow)->bin_window,
201 state,
202 NULL,
203 tree,
204 "treeview",
205 dc.LogicalToDeviceX(rect.x) + 2,
206 dc.LogicalToDeviceY(rect.y) + 3,
207 flags & wxCONTROL_EXPANDED ? GTK_EXPANDER_EXPANDED
208 : GTK_EXPANDER_COLLAPSED
209 );
210 }
211
212 #endif // GTK+ 2.0
213
214 // ----------------------------------------------------------------------------
215 // splitter sash drawing
216 // ----------------------------------------------------------------------------
217
218 #ifndef __WXGTK20__
219 // the full sash width (should be even)
220 static const wxCoord SASH_SIZE = 8;
221
222 // margin around the sash
223 static const wxCoord SASH_MARGIN = 2;
224 #endif // GTK+ 2.x/1.x
225
226 static int GetGtkSplitterFullSize()
227 {
228 #ifdef __WXGTK20__
229 static GtkWidget *s_paned = NULL;
230 if (s_paned == NULL)
231 s_paned = gtk_vpaned_new();
232
233 gint handle_size;
234 gtk_widget_style_get (s_paned, "handle_size", &handle_size, NULL);
235
236 return handle_size;
237 #else
238 return SASH_SIZE + SASH_MARGIN;
239 #endif
240 }
241
242 wxSplitterRenderParams
243 wxRendererGTK::GetSplitterParams(const wxWindow *WXUNUSED(win))
244 {
245 // we don't draw any border, hence 0 for the second field
246 return wxSplitterRenderParams
247 (
248 GetGtkSplitterFullSize(),
249 0,
250 #ifdef __WXGTK20__
251 true // hot sensitive
252 #else // GTK+ 1.x
253 false // not
254 #endif // GTK+ 2.x/1.x
255 );
256 }
257
258 void
259 wxRendererGTK::DrawSplitterBorder(wxWindow * WXUNUSED(win),
260 wxDC& WXUNUSED(dc),
261 const wxRect& WXUNUSED(rect),
262 int WXUNUSED(flags))
263 {
264 // nothing to do
265 }
266
267 void
268 wxRendererGTK::DrawSplitterSash(wxWindow *win,
269 wxDC& dc,
270 const wxSize& size,
271 wxCoord position,
272 wxOrientation orient,
273 int WXUNUSED_IN_GTK1(flags))
274 {
275 if ( !win->m_wxwindow->window )
276 {
277 // window not realized yet
278 return;
279 }
280
281 wxCoord full_size = GetGtkSplitterFullSize();
282
283 // are we drawing vertical or horizontal splitter?
284 const bool isVert = orient == wxVERTICAL;
285
286 GdkRectangle rect;
287 GdkRectangle erase_rect;
288 if ( isVert )
289 {
290 int h = win->GetClientSize().GetHeight();
291
292 rect.x = position;
293 rect.y = 0;
294 rect.width = full_size;
295 rect.height = h;
296
297 erase_rect.x = position;
298 erase_rect.y = 0;
299 erase_rect.width = full_size;
300 erase_rect.height = h;
301 }
302 else // horz
303 {
304 int w = win->GetClientSize().GetWidth();
305
306 rect.x = 0;
307 rect.y = position;
308 rect.height = full_size;
309 rect.width = w;
310
311 erase_rect.y = position;
312 erase_rect.x = 0;
313 erase_rect.height = full_size;
314 erase_rect.width = w;
315 }
316
317 #if 0
318 // RR: After a correction to the orientation of the sash
319 // this doesn't seem to be required anymore and it
320 // seems to confuse some themes
321
322 // we must erase everything first, otherwise the garbage
323 // from the old sash is left when dragging it
324 gtk_paint_flat_box
325 (
326 win->m_wxwindow->style,
327 GTK_PIZZA(win->m_wxwindow)->bin_window,
328 GTK_STATE_NORMAL,
329 GTK_SHADOW_NONE,
330 NULL,
331 win->m_wxwindow,
332 (char *)"viewportbin", // const_cast
333 erase_rect.x,
334 erase_rect.y,
335 erase_rect.width,
336 erase_rect.height
337 );
338 #endif
339
340 #ifdef __WXGTK20__
341 gtk_paint_handle
342 (
343 win->m_wxwindow->style,
344 GTK_PIZZA(win->m_wxwindow)->bin_window,
345 flags & wxCONTROL_CURRENT ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
346 GTK_SHADOW_NONE,
347 NULL /* no clipping */,
348 win->m_wxwindow,
349 "paned",
350 rect.x,
351 rect.y,
352 rect.width,
353 rect.height,
354 isVert ? GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL
355 );
356 #else // GTK+ 1.x
357
358 // leave some margin before sash itself
359 position += SASH_MARGIN / 2;
360
361 // and finally draw it using GTK paint functions
362 typedef void (*GtkPaintLineFunc)(GtkStyle *, GdkWindow *,
363 GtkStateType,
364 GdkRectangle *, GtkWidget *,
365 gchar *,
366 gint, gint, gint);
367
368 GtkPaintLineFunc func = isVert ? gtk_paint_vline : gtk_paint_hline;
369
370 (*func)
371 (
372 win->m_wxwindow->style,
373 GTK_PIZZA(win->m_wxwindow)->bin_window,
374 GTK_STATE_NORMAL,
375 NULL,
376 win->m_wxwindow,
377 (char *)"paned", // const_cast
378 0, isVert ? size.y : size.x, position + SASH_SIZE / 2 - 1
379 );
380
381 gtk_paint_box
382 (
383 win->m_wxwindow->style,
384 GTK_PIZZA(win->m_wxwindow)->bin_window,
385 GTK_STATE_NORMAL,
386 GTK_SHADOW_OUT,
387 (GdkRectangle*) NULL,
388 win->m_wxwindow,
389 (char *)"paned", // const_cast
390 isVert ? position : size.x - 2*SASH_SIZE,
391 isVert ? size.y - 2*SASH_SIZE : position,
392 SASH_SIZE, SASH_SIZE
393 );
394 #endif // GTK+ 2.x/1.x
395 }
396
397 void wxRendererGTK::DrawComboBoxDropButton(wxWindow *win,
398 wxDC& dc,
399 const wxRect& rect,
400 int flags)
401 {
402 GtkWidget *button = GetButtonWidget();
403
404 // device context must inherit from wxWindowDC
405 // (so it must be wxClientDC, wxMemoryDC or wxPaintDC)
406 wxWindowDC& wdc = (wxWindowDC&)dc;
407
408 // only doing debug-time checking here (it should probably be enough)
409 wxASSERT ( wdc.IsKindOf(CLASSINFO(wxWindowDC)) );
410
411 GtkStateType state;
412
413 if ( flags & wxCONTROL_CURRENT )
414 state = GTK_STATE_PRELIGHT;
415 else if ( flags & wxCONTROL_DISABLED )
416 state = GTK_STATE_INSENSITIVE;
417 else
418 state = GTK_STATE_NORMAL;
419
420 // draw arrow on button
421 gtk_paint_arrow
422 (
423 button->style,
424 wdc.m_window,
425 state,
426 flags & wxCONTROL_PRESSED ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
427 NULL,
428 button,
429 "arrow",
430 GTK_ARROW_DOWN,
431 FALSE,
432 rect.x + 1, rect.y + 1, rect.width - 2, rect.height - 2
433 );
434 }
435