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