New magic number for positioning the tree triangle.
[wxWidgets.git] / src / gtk / renderer.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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
29 #ifndef WX_PRECOMP
30 #include "wx/window.h"
31 #include "wx/dcclient.h"
32 #include "wx/settings.h"
33 #endif
34
35 #include <gtk/gtk.h>
36 #include "wx/gtk/win_gtk.h"
37
38 // ----------------------------------------------------------------------------
39 // wxRendererGTK: our wxRendererNative implementation
40 // ----------------------------------------------------------------------------
41
42 class WXDLLEXPORT wxRendererGTK : public wxDelegateRendererNative
43 {
44 public:
45 // draw the header control button (used by wxListCtrl)
46 virtual void DrawHeaderButton(wxWindow *win,
47 wxDC& dc,
48 const wxRect& rect,
49 int flags = 0,
50 wxHeaderSortIconType sortArrow = wxHDR_SORT_ICON_NONE,
51 wxHeaderButtonParams* params = NULL);
52
53 // draw the expanded/collapsed icon for a tree control item
54 virtual void DrawTreeItemButton(wxWindow *win,
55 wxDC& dc,
56 const wxRect& rect,
57 int flags = 0);
58
59 virtual void DrawSplitterBorder(wxWindow *win,
60 wxDC& dc,
61 const wxRect& rect,
62 int flags = 0);
63 virtual void DrawSplitterSash(wxWindow *win,
64 wxDC& dc,
65 const wxSize& size,
66 wxCoord position,
67 wxOrientation orient,
68 int flags = 0);
69
70 virtual void DrawComboBoxDropButton(wxWindow *win,
71 wxDC& dc,
72 const wxRect& rect,
73 int flags = 0);
74
75 virtual void DrawDropArrow(wxWindow *win,
76 wxDC& dc,
77 const wxRect& rect,
78 int flags = 0);
79
80 virtual void DrawCheckBox(wxWindow *win,
81 wxDC& dc,
82 const wxRect& rect,
83 int flags = 0);
84
85 virtual void DrawPushButton(wxWindow *win,
86 wxDC& dc,
87 const wxRect& rect,
88 int flags = 0);
89
90 virtual void DrawItemSelectionRect(wxWindow *win,
91 wxDC& dc,
92 const wxRect& rect,
93 int flags = 0);
94
95 virtual wxSplitterRenderParams GetSplitterParams(const wxWindow *win);
96
97 private:
98 // FIXME: shouldn't we destroy these windows somewhere?
99
100 // used by DrawHeaderButton and DrawPushButton
101 static GtkWidget *GetButtonWidget();
102
103 // used by DrawTreeItemButton()
104 static GtkWidget *GetTreeWidget();
105
106 // used by DrawCheckBox()
107 static GtkWidget *GetCheckButtonWidget();
108 };
109
110 // ============================================================================
111 // implementation
112 // ============================================================================
113
114 /* static */
115 wxRendererNative& wxRendererNative::GetDefault()
116 {
117 static wxRendererGTK s_rendererGTK;
118
119 return s_rendererGTK;
120 }
121
122 // ----------------------------------------------------------------------------
123 // helper functions
124 // ----------------------------------------------------------------------------
125
126 GtkWidget *
127 wxRendererGTK::GetButtonWidget()
128 {
129 static GtkWidget *s_button = NULL;
130 static GtkWidget *s_window = NULL;
131
132 if ( !s_button )
133 {
134 s_window = gtk_window_new( GTK_WINDOW_POPUP );
135 gtk_widget_realize( s_window );
136 s_button = gtk_button_new();
137 gtk_container_add( GTK_CONTAINER(s_window), s_button );
138 gtk_widget_realize( s_button );
139 }
140
141 return s_button;
142 }
143
144 GtkWidget *
145 wxRendererGTK::GetCheckButtonWidget()
146 {
147 static GtkWidget *s_button = NULL;
148 static GtkWidget *s_window = NULL;
149
150 if ( !s_button )
151 {
152 s_window = gtk_window_new( GTK_WINDOW_POPUP );
153 gtk_widget_realize( s_window );
154 s_button = gtk_check_button_new();
155 gtk_container_add( GTK_CONTAINER(s_window), s_button );
156 gtk_widget_realize( s_button );
157 }
158
159 return s_button;
160 }
161
162 GtkWidget *
163 wxRendererGTK::GetTreeWidget()
164 {
165 static GtkWidget *s_tree = NULL;
166 static GtkWidget *s_window = NULL;
167
168 if ( !s_tree )
169 {
170 s_tree = gtk_tree_view_new();
171 s_window = gtk_window_new( GTK_WINDOW_POPUP );
172 gtk_widget_realize( s_window );
173 gtk_container_add( GTK_CONTAINER(s_window), s_tree );
174 gtk_widget_realize( s_tree );
175 }
176
177 return s_tree;
178 }
179
180 // ----------------------------------------------------------------------------
181 // list/tree controls drawing
182 // ----------------------------------------------------------------------------
183
184 void
185 wxRendererGTK::DrawHeaderButton(wxWindow *win,
186 wxDC& dc,
187 const wxRect& rect,
188 int flags,
189 wxHeaderSortIconType sortArrow,
190 wxHeaderButtonParams* params)
191 {
192
193 GtkWidget *button = GetButtonWidget();
194
195 int x_diff = 0;
196 if (win->GetLayoutDirection() == wxLayout_RightToLeft)
197 x_diff = rect.width;
198
199 gtk_paint_box
200 (
201 button->style,
202 // FIXME: I suppose GTK_PIZZA(win->m_wxwindow)->bin_window doesn't work with wxMemoryDC.
203 // Maybe use code similar as in DrawPushButton below?
204 GTK_PIZZA(win->m_wxwindow)->bin_window,
205 flags & wxCONTROL_DISABLED ? GTK_STATE_INSENSITIVE : GTK_STATE_NORMAL,
206 GTK_SHADOW_OUT,
207 NULL,
208 button,
209 "button",
210 dc.LogicalToDeviceX(rect.x) - x_diff, rect.y, rect.width, rect.height
211 );
212
213 DrawHeaderButtonContents(win, dc, rect, flags, sortArrow, params);
214 }
215
216 // draw a ">" or "v" button
217 void
218 wxRendererGTK::DrawTreeItemButton(wxWindow* win,
219 wxDC& dc, const wxRect& rect, int flags)
220 {
221 GtkWidget *tree = GetTreeWidget();
222
223 GtkStateType state;
224 if ( flags & wxCONTROL_CURRENT )
225 state = GTK_STATE_PRELIGHT;
226 else
227 state = GTK_STATE_NORMAL;
228
229 int x_diff = 0;
230 if (win->GetLayoutDirection() == wxLayout_RightToLeft)
231 x_diff = rect.width;
232
233 // VZ: I don't know how to get the size of the expander so as to centre it
234 // in the given rectangle, +2/3 below is just what looks good here...
235 gtk_paint_expander
236 (
237 tree->style,
238 GTK_PIZZA(win->m_wxwindow)->bin_window,
239 state,
240 NULL,
241 tree,
242 "treeview",
243 dc.LogicalToDeviceX(rect.x) + 6 - x_diff,
244 dc.LogicalToDeviceY(rect.y) + 3,
245 flags & wxCONTROL_EXPANDED ? GTK_EXPANDER_EXPANDED
246 : GTK_EXPANDER_COLLAPSED
247 );
248 }
249
250
251 // ----------------------------------------------------------------------------
252 // splitter sash drawing
253 // ----------------------------------------------------------------------------
254
255 static int GetGtkSplitterFullSize()
256 {
257 static GtkWidget *s_paned = NULL;
258 if (s_paned == NULL)
259 s_paned = gtk_vpaned_new();
260
261 gint handle_size;
262 gtk_widget_style_get (s_paned, "handle_size", &handle_size, NULL);
263
264 return handle_size;
265 }
266
267 wxSplitterRenderParams
268 wxRendererGTK::GetSplitterParams(const wxWindow *WXUNUSED(win))
269 {
270 // we don't draw any border, hence 0 for the second field
271 return wxSplitterRenderParams
272 (
273 GetGtkSplitterFullSize(),
274 0,
275 true // hot sensitive
276 );
277 }
278
279 void
280 wxRendererGTK::DrawSplitterBorder(wxWindow * WXUNUSED(win),
281 wxDC& WXUNUSED(dc),
282 const wxRect& WXUNUSED(rect),
283 int WXUNUSED(flags))
284 {
285 // nothing to do
286 }
287
288 void
289 wxRendererGTK::DrawSplitterSash(wxWindow *win,
290 wxDC& dc,
291 const wxSize& size,
292 wxCoord position,
293 wxOrientation orient,
294 int flags)
295 {
296 if ( !win->m_wxwindow->window )
297 {
298 // window not realized yet
299 return;
300 }
301
302 wxCoord full_size = GetGtkSplitterFullSize();
303
304 // are we drawing vertical or horizontal splitter?
305 const bool isVert = orient == wxVERTICAL;
306
307 GdkRectangle rect;
308
309 if ( isVert )
310 {
311 int h = win->GetClientSize().GetHeight();
312
313 rect.x = position;
314 rect.y = 0;
315 rect.width = full_size;
316 rect.height = h;
317 }
318 else // horz
319 {
320 int w = win->GetClientSize().GetWidth();
321
322 rect.x = 0;
323 rect.y = position;
324 rect.height = full_size;
325 rect.width = w;
326 }
327
328 int x_diff = 0;
329 if (win->GetLayoutDirection() == wxLayout_RightToLeft)
330 x_diff = rect.width;
331
332 gtk_paint_handle
333 (
334 win->m_wxwindow->style,
335 GTK_PIZZA(win->m_wxwindow)->bin_window,
336 flags & wxCONTROL_CURRENT ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
337 GTK_SHADOW_NONE,
338 NULL /* no clipping */,
339 win->m_wxwindow,
340 "paned",
341 dc.LogicalToDeviceX(rect.x) - x_diff,
342 dc.LogicalToDeviceY(rect.y),
343 rect.width,
344 rect.height,
345 isVert ? GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL
346 );
347 }
348
349 void
350 wxRendererGTK::DrawDropArrow(wxWindow *win,
351 wxDC& dc,
352 const wxRect& rect,
353 int flags)
354 {
355 GtkWidget *button = GetButtonWidget();
356
357 // If we give GTK_PIZZA(win->m_wxwindow)->bin_window as
358 // a window for gtk_paint_xxx function, then it won't
359 // work for wxMemoryDC. So that is why we assume wxDC
360 // is wxWindowDC (wxClientDC, wxMemoryDC and wxPaintDC
361 // are derived from it) and use its m_window.
362 wxWindowDC& wdc = (wxWindowDC&)dc;
363
364 // only doing debug-time checking here (it should
365 // probably be enough)
366 wxASSERT ( wdc.IsKindOf(CLASSINFO(wxWindowDC)) );
367
368 // draw arrow so that there is even space horizontally
369 // on both sides
370 int arrowX = rect.width/4 + 1;
371 int arrowWidth = rect.width - (arrowX*2);
372
373 // scale arrow's height accoording to the width
374 int arrowHeight = rect.width/3;
375 int arrowY = (rect.height-arrowHeight)/2 +
376 ((rect.height-arrowHeight) & 1);
377
378 GtkStateType state;
379
380 if ( flags & wxCONTROL_PRESSED )
381 state = GTK_STATE_ACTIVE;
382 else if ( flags & wxCONTROL_DISABLED )
383 state = GTK_STATE_INSENSITIVE;
384 else if ( flags & wxCONTROL_CURRENT )
385 state = GTK_STATE_PRELIGHT;
386 else
387 state = GTK_STATE_NORMAL;
388
389 // draw arrow on button
390 gtk_paint_arrow
391 (
392 button->style,
393 wdc.m_window,
394 state,
395 flags & wxCONTROL_PRESSED ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
396 NULL,
397 button,
398 "arrow",
399 GTK_ARROW_DOWN,
400 FALSE,
401 rect.x + arrowX,
402 rect.y + arrowY,
403 arrowWidth,
404 arrowHeight
405 );
406 }
407
408 void
409 wxRendererGTK::DrawComboBoxDropButton(wxWindow *win,
410 wxDC& dc,
411 const wxRect& rect,
412 int flags)
413 {
414 DrawPushButton(win,dc,rect,flags);
415 DrawDropArrow(win,dc,rect);
416 }
417
418 void
419 wxRendererGTK::DrawCheckBox(wxWindow *win,
420 wxDC& dc,
421 const wxRect& rect,
422 int flags )
423 {
424 GtkWidget *button = GetCheckButtonWidget();
425
426 // for reason why we do this, see DrawDropArrow
427 wxWindowDC& wdc = (wxWindowDC&)dc;
428 wxASSERT ( wdc.IsKindOf(CLASSINFO(wxWindowDC)) );
429
430 GtkStateType state;
431
432 if ( flags & wxCONTROL_PRESSED )
433 state = GTK_STATE_ACTIVE;
434 else if ( flags & wxCONTROL_DISABLED )
435 state = GTK_STATE_INSENSITIVE;
436 else if ( flags & wxCONTROL_CURRENT )
437 state = GTK_STATE_PRELIGHT;
438 else
439 state = GTK_STATE_NORMAL;
440
441 gtk_paint_check
442 (
443 button->style,
444 wdc.m_window,
445 state,
446 flags & wxCONTROL_CHECKED ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
447 NULL,
448 button,
449 "cellcheck",
450 dc.LogicalToDeviceX(rect.x)+2,
451 dc.LogicalToDeviceY(rect.y)+3,
452 13, 13
453 );
454 }
455
456 void
457 wxRendererGTK::DrawPushButton(wxWindow *win,
458 wxDC& dc,
459 const wxRect& rect,
460 int flags)
461 {
462 GtkWidget *button = GetButtonWidget();
463
464 // for reason why we do this, see DrawDropArrow
465 wxWindowDC& wdc = (wxWindowDC&)dc;
466 wxASSERT ( wdc.IsKindOf(CLASSINFO(wxWindowDC)) );
467
468 // draw button
469 GtkStateType state;
470
471 if ( flags & wxCONTROL_PRESSED )
472 state = GTK_STATE_ACTIVE;
473 else if ( flags & wxCONTROL_DISABLED )
474 state = GTK_STATE_INSENSITIVE;
475 else if ( flags & wxCONTROL_CURRENT )
476 state = GTK_STATE_PRELIGHT;
477 else
478 state = GTK_STATE_NORMAL;
479
480 gtk_paint_box
481 (
482 button->style,
483 wdc.m_window,
484 state,
485 flags & wxCONTROL_PRESSED ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
486 NULL,
487 button,
488 "button",
489 rect.x, rect.y, rect.width, rect.height
490 );
491 }
492
493 void
494 wxRendererGTK::DrawItemSelectionRect(wxWindow *win,
495 wxDC& dc,
496 const wxRect& rect,
497 int flags )
498 {
499 GtkStateType state;
500 if (flags & wxCONTROL_SELECTED)
501 {
502 if (flags & wxCONTROL_FOCUSED)
503 state = GTK_STATE_SELECTED;
504 else
505 state = GTK_STATE_INSENSITIVE;
506
507 gtk_paint_flat_box( win->m_wxwindow->style,
508 GTK_PIZZA(win->m_wxwindow)->bin_window,
509 state,
510 GTK_SHADOW_NONE,
511 NULL,
512 win->m_wxwindow,
513 "treeview",
514 dc.LogicalToDeviceX(rect.x),
515 dc.LogicalToDeviceY(rect.y),
516 rect.width,
517 rect.height );
518 }
519
520 if (flags & wxCONTROL_CURRENT)
521 {
522 dc.SetPen( *wxBLACK_PEN );
523 dc.SetBrush( *wxTRANSPARENT_BRUSH );
524 dc.DrawRectangle( rect );
525 }
526 }