1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/renderer.cpp
3 // Purpose: implementation of wxRendererNative for wxGTK
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/renderer.h"
30 #include "wx/window.h"
31 #include "wx/dcclient.h"
32 #include "wx/settings.h"
36 #include "wx/gtk/win_gtk.h"
38 // ----------------------------------------------------------------------------
39 // wxRendererGTK: our wxRendererNative implementation
40 // ----------------------------------------------------------------------------
42 class WXDLLEXPORT wxRendererGTK
: public wxDelegateRendererNative
45 // draw the header control button (used by wxListCtrl)
46 virtual int DrawHeaderButton(wxWindow
*win
,
50 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
,
51 wxHeaderButtonParams
* params
= NULL
);
53 // draw the expanded/collapsed icon for a tree control item
54 virtual void DrawTreeItemButton(wxWindow
*win
,
59 virtual void DrawSplitterBorder(wxWindow
*win
,
63 virtual void DrawSplitterSash(wxWindow
*win
,
70 virtual void DrawComboBoxDropButton(wxWindow
*win
,
75 virtual void DrawDropArrow(wxWindow
*win
,
80 virtual void DrawCheckBox(wxWindow
*win
,
85 virtual void DrawPushButton(wxWindow
*win
,
90 virtual void DrawItemSelectionRect(wxWindow
*win
,
95 virtual void DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
= 0);
97 virtual wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
100 // FIXME: shouldn't we destroy these windows somewhere?
102 // used by DrawPushButton and DrawDropArrow
103 static GtkWidget
*GetButtonWidget();
105 // used by DrawTreeItemButton()
106 static GtkWidget
*GetTreeWidget();
108 // used by DrawCheckBox()
109 static GtkWidget
*GetCheckButtonWidget();
111 // Used by DrawHeaderButton
112 static GtkWidget
*GetHeaderButtonWidget();
115 // ============================================================================
117 // ============================================================================
120 wxRendererNative
& wxRendererNative::GetDefault()
122 static wxRendererGTK s_rendererGTK
;
124 return s_rendererGTK
;
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
132 wxRendererGTK::GetButtonWidget()
134 static GtkWidget
*s_button
= NULL
;
135 static GtkWidget
*s_window
= NULL
;
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
);
150 wxRendererGTK::GetCheckButtonWidget()
152 static GtkWidget
*s_button
= NULL
;
153 static GtkWidget
*s_window
= NULL
;
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
);
168 wxRendererGTK::GetTreeWidget()
170 static GtkWidget
*s_tree
= NULL
;
171 static GtkWidget
*s_window
= NULL
;
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
);
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.
190 wxRendererGTK::GetHeaderButtonWidget()
192 static GtkWidget
*s_button
= NULL
;
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
;
207 // ----------------------------------------------------------------------------
208 // list/tree controls drawing
209 // ----------------------------------------------------------------------------
212 wxRendererGTK::DrawHeaderButton(wxWindow
*win
,
216 wxHeaderSortIconType sortArrow
,
217 wxHeaderButtonParams
* params
)
220 GtkWidget
*button
= GetHeaderButtonWidget();
222 GdkWindow
* gdk_window
= NULL
;
224 wxImplDC
*impl
= dc
.GetImpl();
225 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
227 gdk_window
= gtk_impl
->GetGDKWindow();
229 gdk_window
= dc
.GetGDKWindow();
231 wxASSERT_MSG( gdk_window
,
232 wxT("cannot use wxRendererNative on wxDC of this type") );
235 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
242 flags
& wxCONTROL_DISABLED
? GTK_STATE_INSENSITIVE
: GTK_STATE_NORMAL
,
247 dc
.LogicalToDeviceX(rect
.x
) - x_diff
, rect
.y
, rect
.width
, rect
.height
250 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
253 // draw a ">" or "v" button
255 wxRendererGTK::DrawTreeItemButton(wxWindow
* win
,
256 wxDC
& dc
, const wxRect
& rect
, int flags
)
258 GtkWidget
*tree
= GetTreeWidget();
260 GdkWindow
* gdk_window
= NULL
;
262 wxImplDC
*impl
= dc
.GetImpl();
263 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
265 gdk_window
= gtk_impl
->GetGDKWindow();
267 gdk_window
= dc
.GetGDKWindow();
269 wxASSERT_MSG( gdk_window
,
270 wxT("cannot use wxRendererNative on wxDC of this type") );
273 if ( flags
& wxCONTROL_CURRENT
)
274 state
= GTK_STATE_PRELIGHT
;
276 state
= GTK_STATE_NORMAL
;
279 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
282 // VZ: I don't know how to get the size of the expander so as to centre it
283 // in the given rectangle, +2/3 below is just what looks good here...
292 dc
.LogicalToDeviceX(rect
.x
) + 6 - x_diff
,
293 dc
.LogicalToDeviceY(rect
.y
) + 3,
294 flags
& wxCONTROL_EXPANDED
? GTK_EXPANDER_EXPANDED
295 : GTK_EXPANDER_COLLAPSED
300 // ----------------------------------------------------------------------------
301 // splitter sash drawing
302 // ----------------------------------------------------------------------------
304 static int GetGtkSplitterFullSize()
306 static GtkWidget
*s_paned
= NULL
;
308 s_paned
= gtk_vpaned_new();
311 gtk_widget_style_get (s_paned
, "handle_size", &handle_size
, NULL
);
316 wxSplitterRenderParams
317 wxRendererGTK::GetSplitterParams(const wxWindow
*WXUNUSED(win
))
319 // we don't draw any border, hence 0 for the second field
320 return wxSplitterRenderParams
322 GetGtkSplitterFullSize(),
324 true // hot sensitive
329 wxRendererGTK::DrawSplitterBorder(wxWindow
* WXUNUSED(win
),
331 const wxRect
& WXUNUSED(rect
),
338 wxRendererGTK::DrawSplitterSash(wxWindow
*win
,
342 wxOrientation orient
,
345 if ( !win
->m_wxwindow
->window
)
347 // window not realized yet
351 GdkWindow
* gdk_window
= NULL
;
353 wxImplDC
*impl
= dc
.GetImpl();
354 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
356 gdk_window
= gtk_impl
->GetGDKWindow();
358 gdk_window
= dc
.GetGDKWindow();
360 wxASSERT_MSG( gdk_window
,
361 wxT("cannot use wxRendererNative on wxDC of this type") );
363 wxCoord full_size
= GetGtkSplitterFullSize();
365 // are we drawing vertical or horizontal splitter?
366 const bool isVert
= orient
== wxVERTICAL
;
372 int h
= win
->GetClientSize().GetHeight();
376 rect
.width
= full_size
;
381 int w
= win
->GetClientSize().GetWidth();
385 rect
.height
= full_size
;
390 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
395 win
->m_wxwindow
->style
,
397 flags
& wxCONTROL_CURRENT
? GTK_STATE_PRELIGHT
: GTK_STATE_NORMAL
,
399 NULL
/* no clipping */,
402 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
403 dc
.LogicalToDeviceY(rect
.y
),
406 isVert
? GTK_ORIENTATION_VERTICAL
: GTK_ORIENTATION_HORIZONTAL
411 wxRendererGTK::DrawDropArrow(wxWindow
*WXUNUSED(win
),
416 GtkWidget
*button
= GetButtonWidget();
418 // If we give GTK_PIZZA(win->m_wxwindow)->bin_window as
419 // a window for gtk_paint_xxx function, then it won't
420 // work for wxMemoryDC. So that is why we assume wxDC
421 // is wxWindowDC (wxClientDC, wxMemoryDC and wxPaintDC
422 // are derived from it) and use its m_window.
423 GdkWindow
* gdk_window
= NULL
;
425 wxImplDC
*impl
= dc
.GetImpl();
426 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
428 gdk_window
= gtk_impl
->GetGDKWindow();
430 gdk_window
= dc
.GetGDKWindow();
432 wxASSERT_MSG( gdk_window
,
433 wxT("cannot use wxRendererNative on wxDC of this type") );
435 // draw arrow so that there is even space horizontally
437 int arrowX
= rect
.width
/4 + 1;
438 int arrowWidth
= rect
.width
- (arrowX
*2);
440 // scale arrow's height accoording to the width
441 int arrowHeight
= rect
.width
/3;
442 int arrowY
= (rect
.height
-arrowHeight
)/2 +
443 ((rect
.height
-arrowHeight
) & 1);
447 if ( flags
& wxCONTROL_PRESSED
)
448 state
= GTK_STATE_ACTIVE
;
449 else if ( flags
& wxCONTROL_DISABLED
)
450 state
= GTK_STATE_INSENSITIVE
;
451 else if ( flags
& wxCONTROL_CURRENT
)
452 state
= GTK_STATE_PRELIGHT
;
454 state
= GTK_STATE_NORMAL
;
456 // draw arrow on button
462 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
476 wxRendererGTK::DrawComboBoxDropButton(wxWindow
*win
,
481 DrawPushButton(win
,dc
,rect
,flags
);
482 DrawDropArrow(win
,dc
,rect
);
486 wxRendererGTK::DrawCheckBox(wxWindow
*WXUNUSED(win
),
491 GtkWidget
*button
= GetCheckButtonWidget();
493 GdkWindow
* gdk_window
= NULL
;
495 wxImplDC
*impl
= dc
.GetImpl();
496 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
498 gdk_window
= gtk_impl
->GetGDKWindow();
500 gdk_window
= dc
.GetGDKWindow();
502 wxASSERT_MSG( gdk_window
,
503 wxT("cannot use wxRendererNative on wxDC of this type") );
507 if ( flags
& wxCONTROL_PRESSED
)
508 state
= GTK_STATE_ACTIVE
;
509 else if ( flags
& wxCONTROL_DISABLED
)
510 state
= GTK_STATE_INSENSITIVE
;
511 else if ( flags
& wxCONTROL_CURRENT
)
512 state
= GTK_STATE_PRELIGHT
;
514 state
= GTK_STATE_NORMAL
;
521 flags
& wxCONTROL_CHECKED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
525 dc
.LogicalToDeviceX(rect
.x
)+2,
526 dc
.LogicalToDeviceY(rect
.y
)+3,
532 wxRendererGTK::DrawPushButton(wxWindow
*WXUNUSED(win
),
537 GtkWidget
*button
= GetButtonWidget();
539 GdkWindow
* gdk_window
= NULL
;
541 wxImplDC
*impl
= dc
.GetImpl();
542 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
544 gdk_window
= gtk_impl
->GetGDKWindow();
546 gdk_window
= dc
.GetGDKWindow();
548 wxASSERT_MSG( gdk_window
,
549 wxT("cannot use wxRendererNative on wxDC of this type") );
554 if ( flags
& wxCONTROL_PRESSED
)
555 state
= GTK_STATE_ACTIVE
;
556 else if ( flags
& wxCONTROL_DISABLED
)
557 state
= GTK_STATE_INSENSITIVE
;
558 else if ( flags
& wxCONTROL_CURRENT
)
559 state
= GTK_STATE_PRELIGHT
;
561 state
= GTK_STATE_NORMAL
;
568 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
572 rect
.x
, rect
.y
, rect
.width
, rect
.height
577 wxRendererGTK::DrawItemSelectionRect(wxWindow
*win
,
582 GdkWindow
* gdk_window
= NULL
;
584 wxImplDC
*impl
= dc
.GetImpl();
585 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
587 gdk_window
= gtk_impl
->GetGDKWindow();
589 gdk_window
= dc
.GetGDKWindow();
591 wxASSERT_MSG( gdk_window
,
592 wxT("cannot use wxRendererNative on wxDC of this type") );
595 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
599 if (flags
& wxCONTROL_SELECTED
)
601 // the wxCONTROL_FOCUSED state is deduced
602 // directly from the m_wxwindow by GTK+
603 state
= GTK_STATE_SELECTED
;
605 gtk_paint_flat_box( win
->m_widget
->style
,
612 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
613 dc
.LogicalToDeviceY(rect
.y
),
617 else // !wxCONTROL_SELECTED
619 state
= GTK_STATE_NORMAL
;
622 if (flags
& wxCONTROL_CURRENT
)
624 gtk_paint_focus( win
->m_widget
->style
,
630 dc
.LogicalToDeviceX(rect
.x
),
631 dc
.LogicalToDeviceY(rect
.y
),
637 void wxRendererGTK::DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
639 GdkWindow
* gdk_window
= NULL
;
641 wxImplDC
*impl
= dc
.GetImpl();
642 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
644 gdk_window
= gtk_impl
->GetGDKWindow();
646 gdk_window
= dc
.GetGDKWindow();
648 wxASSERT_MSG( gdk_window
,
649 wxT("cannot use wxRendererNative on wxDC of this type") );
652 if (flags
& wxCONTROL_SELECTED
)
653 state
= GTK_STATE_SELECTED
;
655 state
= GTK_STATE_NORMAL
;
657 gtk_paint_focus( win
->m_widget
->style
,
663 dc
.LogicalToDeviceX(rect
.x
),
664 dc
.LogicalToDeviceY(rect
.y
),