Support hover over effect for DrawHeaderButton for GTK+
[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 int 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 void DrawFocusRect(wxWindow* win, wxDC& dc, const wxRect& rect, int flags = 0);
96
97 virtual wxSplitterRenderParams GetSplitterParams(const wxWindow *win);
98
99 private:
100 // FIXME: shouldn't we destroy these windows somewhere?
101
102 // used by DrawPushButton and DrawDropArrow
103 static GtkWidget *GetButtonWidget();
104
105 // used by DrawTreeItemButton()
106 static GtkWidget *GetTreeWidget();
107
108 // used by DrawCheckBox()
109 static GtkWidget *GetCheckButtonWidget();
110
111 // Used by DrawHeaderButton
112 static GtkWidget *GetHeaderButtonWidget();
113 };
114
115 // ============================================================================
116 // implementation
117 // ============================================================================
118
119 /* static */
120 wxRendererNative& wxRendererNative::GetDefault()
121 {
122 static wxRendererGTK s_rendererGTK;
123
124 return s_rendererGTK;
125 }
126
127 // ----------------------------------------------------------------------------
128 // helper functions
129 // ----------------------------------------------------------------------------
130
131 GtkWidget *
132 wxRendererGTK::GetButtonWidget()
133 {
134 static GtkWidget *s_button = NULL;
135 static GtkWidget *s_window = NULL;
136
137 if ( !s_button )
138 {
139 s_window = gtk_window_new( GTK_WINDOW_POPUP );
140 gtk_widget_realize( s_window );
141 s_button = gtk_button_new();
142 gtk_container_add( GTK_CONTAINER(s_window), s_button );
143 gtk_widget_realize( s_button );
144 }
145
146 return s_button;
147 }
148
149 GtkWidget *
150 wxRendererGTK::GetCheckButtonWidget()
151 {
152 static GtkWidget *s_button = NULL;
153 static GtkWidget *s_window = NULL;
154
155 if ( !s_button )
156 {
157 s_window = gtk_window_new( GTK_WINDOW_POPUP );
158 gtk_widget_realize( s_window );
159 s_button = gtk_check_button_new();
160 gtk_container_add( GTK_CONTAINER(s_window), s_button );
161 gtk_widget_realize( s_button );
162 }
163
164 return s_button;
165 }
166
167 GtkWidget *
168 wxRendererGTK::GetTreeWidget()
169 {
170 static GtkWidget *s_tree = NULL;
171 static GtkWidget *s_window = NULL;
172
173 if ( !s_tree )
174 {
175 s_tree = gtk_tree_view_new();
176 s_window = gtk_window_new( GTK_WINDOW_POPUP );
177 gtk_widget_realize( s_window );
178 gtk_container_add( GTK_CONTAINER(s_window), s_tree );
179 gtk_widget_realize( s_tree );
180 }
181
182 return s_tree;
183 }
184
185
186 // This one just gets the button used by the column header. Although it's
187 // still a gtk_button the themes will typically differentiate and draw them
188 // differently if the button is in a treeview.
189 GtkWidget *
190 wxRendererGTK::GetHeaderButtonWidget()
191 {
192 static GtkWidget *s_button = NULL;
193
194 if ( !s_button )
195 {
196 // Get the dummy tree widget, give it a column, and then use the
197 // widget in the column header for the rendering code.
198 GtkWidget* treewidget = GetTreeWidget();
199 GtkTreeViewColumn* column = gtk_tree_view_column_new();
200 gtk_tree_view_append_column(GTK_TREE_VIEW(treewidget), column);
201 s_button = column->button;
202 }
203
204 return s_button;
205 }
206
207 // ----------------------------------------------------------------------------
208 // list/tree controls drawing
209 // ----------------------------------------------------------------------------
210
211 int
212 wxRendererGTK::DrawHeaderButton(wxWindow *win,
213 wxDC& dc,
214 const wxRect& rect,
215 int flags,
216 wxHeaderSortIconType sortArrow,
217 wxHeaderButtonParams* params)
218 {
219
220 GtkWidget *button = GetHeaderButtonWidget();
221
222 GdkWindow* gdk_window = NULL;
223 #if wxUSE_NEW_DC
224 wxImplDC *impl = dc.GetImpl();
225 wxGTKImplDC *gtk_impl = wxDynamicCast( impl, wxGTKImplDC );
226 if (gtk_impl)
227 gdk_window = gtk_impl->GetGDKWindow();
228 #else
229 gdk_window = dc.GetGDKWindow();
230 #endif
231 wxASSERT_MSG( gdk_window,
232 wxT("cannot use wxRendererNative on wxDC of this type") );
233
234 int x_diff = 0;
235 if (win->GetLayoutDirection() == wxLayout_RightToLeft)
236 x_diff = rect.width;
237
238 GtkStateType state = GTK_STATE_NORMAL;
239 if (flags & wxCONTROL_DISABLED)
240 state = GTK_STATE_INSENSITIVE;
241 else
242 {
243 if (flags & wxCONTROL_CURRENT)
244 state = GTK_STATE_PRELIGHT;
245 }
246
247 gtk_paint_box
248 (
249 button->style,
250 gdk_window,
251 state,
252 GTK_SHADOW_OUT,
253 NULL,
254 button,
255 "button",
256 dc.LogicalToDeviceX(rect.x) - x_diff, rect.y, rect.width, rect.height
257 );
258
259 return DrawHeaderButtonContents(win, dc, rect, flags, sortArrow, params);
260 }
261
262 // draw a ">" or "v" button
263 void
264 wxRendererGTK::DrawTreeItemButton(wxWindow* win,
265 wxDC& dc, const wxRect& rect, int flags)
266 {
267 GtkWidget *tree = GetTreeWidget();
268
269 GdkWindow* gdk_window = NULL;
270 #if wxUSE_NEW_DC
271 wxImplDC *impl = dc.GetImpl();
272 wxGTKImplDC *gtk_impl = wxDynamicCast( impl, wxGTKImplDC );
273 if (gtk_impl)
274 gdk_window = gtk_impl->GetGDKWindow();
275 #else
276 gdk_window = dc.GetGDKWindow();
277 #endif
278 wxASSERT_MSG( gdk_window,
279 wxT("cannot use wxRendererNative on wxDC of this type") );
280
281 GtkStateType state;
282 if ( flags & wxCONTROL_CURRENT )
283 state = GTK_STATE_PRELIGHT;
284 else
285 state = GTK_STATE_NORMAL;
286
287 int x_diff = 0;
288 if (win->GetLayoutDirection() == wxLayout_RightToLeft)
289 x_diff = rect.width;
290
291 // VZ: I don't know how to get the size of the expander so as to centre it
292 // in the given rectangle, +2/3 below is just what looks good here...
293 gtk_paint_expander
294 (
295 tree->style,
296 gdk_window,
297 state,
298 NULL,
299 tree,
300 "treeview",
301 dc.LogicalToDeviceX(rect.x) + 6 - x_diff,
302 dc.LogicalToDeviceY(rect.y) + 3,
303 flags & wxCONTROL_EXPANDED ? GTK_EXPANDER_EXPANDED
304 : GTK_EXPANDER_COLLAPSED
305 );
306 }
307
308
309 // ----------------------------------------------------------------------------
310 // splitter sash drawing
311 // ----------------------------------------------------------------------------
312
313 static int GetGtkSplitterFullSize()
314 {
315 static GtkWidget *s_paned = NULL;
316 if (s_paned == NULL)
317 s_paned = gtk_vpaned_new();
318
319 gint handle_size;
320 gtk_widget_style_get (s_paned, "handle_size", &handle_size, NULL);
321
322 return handle_size;
323 }
324
325 wxSplitterRenderParams
326 wxRendererGTK::GetSplitterParams(const wxWindow *WXUNUSED(win))
327 {
328 // we don't draw any border, hence 0 for the second field
329 return wxSplitterRenderParams
330 (
331 GetGtkSplitterFullSize(),
332 0,
333 true // hot sensitive
334 );
335 }
336
337 void
338 wxRendererGTK::DrawSplitterBorder(wxWindow * WXUNUSED(win),
339 wxDC& WXUNUSED(dc),
340 const wxRect& WXUNUSED(rect),
341 int WXUNUSED(flags))
342 {
343 // nothing to do
344 }
345
346 void
347 wxRendererGTK::DrawSplitterSash(wxWindow *win,
348 wxDC& dc,
349 const wxSize& size,
350 wxCoord position,
351 wxOrientation orient,
352 int flags)
353 {
354 if ( !win->m_wxwindow->window )
355 {
356 // window not realized yet
357 return;
358 }
359
360 GdkWindow* gdk_window = NULL;
361 #if wxUSE_NEW_DC
362 wxImplDC *impl = dc.GetImpl();
363 wxGTKImplDC *gtk_impl = wxDynamicCast( impl, wxGTKImplDC );
364 if (gtk_impl)
365 gdk_window = gtk_impl->GetGDKWindow();
366 #else
367 gdk_window = dc.GetGDKWindow();
368 #endif
369 wxASSERT_MSG( gdk_window,
370 wxT("cannot use wxRendererNative on wxDC of this type") );
371
372 wxCoord full_size = GetGtkSplitterFullSize();
373
374 // are we drawing vertical or horizontal splitter?
375 const bool isVert = orient == wxVERTICAL;
376
377 GdkRectangle rect;
378
379 if ( isVert )
380 {
381 int h = win->GetClientSize().GetHeight();
382
383 rect.x = position;
384 rect.y = 0;
385 rect.width = full_size;
386 rect.height = h;
387 }
388 else // horz
389 {
390 int w = win->GetClientSize().GetWidth();
391
392 rect.x = 0;
393 rect.y = position;
394 rect.height = full_size;
395 rect.width = w;
396 }
397
398 int x_diff = 0;
399 if (win->GetLayoutDirection() == wxLayout_RightToLeft)
400 x_diff = rect.width;
401
402 gtk_paint_handle
403 (
404 win->m_wxwindow->style,
405 gdk_window,
406 flags & wxCONTROL_CURRENT ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
407 GTK_SHADOW_NONE,
408 NULL /* no clipping */,
409 win->m_wxwindow,
410 "paned",
411 dc.LogicalToDeviceX(rect.x) - x_diff,
412 dc.LogicalToDeviceY(rect.y),
413 rect.width,
414 rect.height,
415 isVert ? GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL
416 );
417 }
418
419 void
420 wxRendererGTK::DrawDropArrow(wxWindow *WXUNUSED(win),
421 wxDC& dc,
422 const wxRect& rect,
423 int flags)
424 {
425 GtkWidget *button = GetButtonWidget();
426
427 // If we give GTK_PIZZA(win->m_wxwindow)->bin_window as
428 // a window for gtk_paint_xxx function, then it won't
429 // work for wxMemoryDC. So that is why we assume wxDC
430 // is wxWindowDC (wxClientDC, wxMemoryDC and wxPaintDC
431 // are derived from it) and use its m_window.
432 GdkWindow* gdk_window = NULL;
433 #if wxUSE_NEW_DC
434 wxImplDC *impl = dc.GetImpl();
435 wxGTKImplDC *gtk_impl = wxDynamicCast( impl, wxGTKImplDC );
436 if (gtk_impl)
437 gdk_window = gtk_impl->GetGDKWindow();
438 #else
439 gdk_window = dc.GetGDKWindow();
440 #endif
441 wxASSERT_MSG( gdk_window,
442 wxT("cannot use wxRendererNative on wxDC of this type") );
443
444 // draw arrow so that there is even space horizontally
445 // on both sides
446 int arrowX = rect.width/4 + 1;
447 int arrowWidth = rect.width - (arrowX*2);
448
449 // scale arrow's height accoording to the width
450 int arrowHeight = rect.width/3;
451 int arrowY = (rect.height-arrowHeight)/2 +
452 ((rect.height-arrowHeight) & 1);
453
454 GtkStateType state;
455
456 if ( flags & wxCONTROL_PRESSED )
457 state = GTK_STATE_ACTIVE;
458 else if ( flags & wxCONTROL_DISABLED )
459 state = GTK_STATE_INSENSITIVE;
460 else if ( flags & wxCONTROL_CURRENT )
461 state = GTK_STATE_PRELIGHT;
462 else
463 state = GTK_STATE_NORMAL;
464
465 // draw arrow on button
466 gtk_paint_arrow
467 (
468 button->style,
469 gdk_window,
470 state,
471 flags & wxCONTROL_PRESSED ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
472 NULL,
473 button,
474 "arrow",
475 GTK_ARROW_DOWN,
476 FALSE,
477 rect.x + arrowX,
478 rect.y + arrowY,
479 arrowWidth,
480 arrowHeight
481 );
482 }
483
484 void
485 wxRendererGTK::DrawComboBoxDropButton(wxWindow *win,
486 wxDC& dc,
487 const wxRect& rect,
488 int flags)
489 {
490 DrawPushButton(win,dc,rect,flags);
491 DrawDropArrow(win,dc,rect);
492 }
493
494 void
495 wxRendererGTK::DrawCheckBox(wxWindow *WXUNUSED(win),
496 wxDC& dc,
497 const wxRect& rect,
498 int flags )
499 {
500 GtkWidget *button = GetCheckButtonWidget();
501
502 GdkWindow* gdk_window = NULL;
503 #if wxUSE_NEW_DC
504 wxImplDC *impl = dc.GetImpl();
505 wxGTKImplDC *gtk_impl = wxDynamicCast( impl, wxGTKImplDC );
506 if (gtk_impl)
507 gdk_window = gtk_impl->GetGDKWindow();
508 #else
509 gdk_window = dc.GetGDKWindow();
510 #endif
511 wxASSERT_MSG( gdk_window,
512 wxT("cannot use wxRendererNative on wxDC of this type") );
513
514 GtkStateType state;
515
516 if ( flags & wxCONTROL_PRESSED )
517 state = GTK_STATE_ACTIVE;
518 else if ( flags & wxCONTROL_DISABLED )
519 state = GTK_STATE_INSENSITIVE;
520 else if ( flags & wxCONTROL_CURRENT )
521 state = GTK_STATE_PRELIGHT;
522 else
523 state = GTK_STATE_NORMAL;
524
525 gtk_paint_check
526 (
527 button->style,
528 gdk_window,
529 state,
530 flags & wxCONTROL_CHECKED ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
531 NULL,
532 button,
533 "cellcheck",
534 dc.LogicalToDeviceX(rect.x)+2,
535 dc.LogicalToDeviceY(rect.y)+3,
536 13, 13
537 );
538 }
539
540 void
541 wxRendererGTK::DrawPushButton(wxWindow *WXUNUSED(win),
542 wxDC& dc,
543 const wxRect& rect,
544 int flags)
545 {
546 GtkWidget *button = GetButtonWidget();
547
548 GdkWindow* gdk_window = NULL;
549 #if wxUSE_NEW_DC
550 wxImplDC *impl = dc.GetImpl();
551 wxGTKImplDC *gtk_impl = wxDynamicCast( impl, wxGTKImplDC );
552 if (gtk_impl)
553 gdk_window = gtk_impl->GetGDKWindow();
554 #else
555 gdk_window = dc.GetGDKWindow();
556 #endif
557 wxASSERT_MSG( gdk_window,
558 wxT("cannot use wxRendererNative on wxDC of this type") );
559
560 // draw button
561 GtkStateType state;
562
563 if ( flags & wxCONTROL_PRESSED )
564 state = GTK_STATE_ACTIVE;
565 else if ( flags & wxCONTROL_DISABLED )
566 state = GTK_STATE_INSENSITIVE;
567 else if ( flags & wxCONTROL_CURRENT )
568 state = GTK_STATE_PRELIGHT;
569 else
570 state = GTK_STATE_NORMAL;
571
572 gtk_paint_box
573 (
574 button->style,
575 gdk_window,
576 state,
577 flags & wxCONTROL_PRESSED ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
578 NULL,
579 button,
580 "button",
581 rect.x, rect.y, rect.width, rect.height
582 );
583 }
584
585 void
586 wxRendererGTK::DrawItemSelectionRect(wxWindow *win,
587 wxDC& dc,
588 const wxRect& rect,
589 int flags )
590 {
591 GdkWindow* gdk_window = NULL;
592 #if wxUSE_NEW_DC
593 wxImplDC *impl = dc.GetImpl();
594 wxGTKImplDC *gtk_impl = wxDynamicCast( impl, wxGTKImplDC );
595 if (gtk_impl)
596 gdk_window = gtk_impl->GetGDKWindow();
597 #else
598 gdk_window = dc.GetGDKWindow();
599 #endif
600 wxASSERT_MSG( gdk_window,
601 wxT("cannot use wxRendererNative on wxDC of this type") );
602
603 int x_diff = 0;
604 if (win->GetLayoutDirection() == wxLayout_RightToLeft)
605 x_diff = rect.width;
606
607 GtkStateType state;
608 if (flags & wxCONTROL_SELECTED)
609 {
610 // the wxCONTROL_FOCUSED state is deduced
611 // directly from the m_wxwindow by GTK+
612 state = GTK_STATE_SELECTED;
613
614 gtk_paint_flat_box( win->m_widget->style,
615 gdk_window,
616 state,
617 GTK_SHADOW_NONE,
618 NULL,
619 win->m_wxwindow,
620 "cell_even",
621 dc.LogicalToDeviceX(rect.x) - x_diff,
622 dc.LogicalToDeviceY(rect.y),
623 rect.width,
624 rect.height );
625 }
626 else // !wxCONTROL_SELECTED
627 {
628 state = GTK_STATE_NORMAL;
629 }
630
631 if ((flags & wxCONTROL_CURRENT) && (flags & wxCONTROL_FOCUSED))
632 {
633 gtk_paint_focus( win->m_widget->style,
634 gdk_window,
635 state,
636 NULL,
637 win->m_wxwindow,
638 "treeview",
639 dc.LogicalToDeviceX(rect.x),
640 dc.LogicalToDeviceY(rect.y),
641 rect.width,
642 rect.height );
643 }
644 }
645
646 void wxRendererGTK::DrawFocusRect(wxWindow* win, wxDC& dc, const wxRect& rect, int flags)
647 {
648 GdkWindow* gdk_window = NULL;
649 #if wxUSE_NEW_DC
650 wxImplDC *impl = dc.GetImpl();
651 wxGTKImplDC *gtk_impl = wxDynamicCast( impl, wxGTKImplDC );
652 if (gtk_impl)
653 gdk_window = gtk_impl->GetGDKWindow();
654 #else
655 gdk_window = dc.GetGDKWindow();
656 #endif
657 wxASSERT_MSG( gdk_window,
658 wxT("cannot use wxRendererNative on wxDC of this type") );
659
660 GtkStateType state;
661 if (flags & wxCONTROL_SELECTED)
662 state = GTK_STATE_SELECTED;
663 else
664 state = GTK_STATE_NORMAL;
665
666 gtk_paint_focus( win->m_widget->style,
667 gdk_window,
668 state,
669 NULL,
670 win->m_wxwindow,
671 NULL,
672 dc.LogicalToDeviceX(rect.x),
673 dc.LogicalToDeviceY(rect.y),
674 rect.width,
675 rect.height );
676 }