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