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
)
238 GtkStateType state
= GTK_STATE_NORMAL
;
239 if (flags
& wxCONTROL_DISABLED
)
240 state
= GTK_STATE_INSENSITIVE
;
243 if (flags
& wxCONTROL_CURRENT
)
244 state
= GTK_STATE_PRELIGHT
;
256 dc
.LogicalToDeviceX(rect
.x
) - x_diff
, rect
.y
, rect
.width
, rect
.height
259 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
262 // draw a ">" or "v" button
264 wxRendererGTK::DrawTreeItemButton(wxWindow
* win
,
265 wxDC
& dc
, const wxRect
& rect
, int flags
)
267 GtkWidget
*tree
= GetTreeWidget();
269 GdkWindow
* gdk_window
= NULL
;
271 wxImplDC
*impl
= dc
.GetImpl();
272 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
274 gdk_window
= gtk_impl
->GetGDKWindow();
276 gdk_window
= dc
.GetGDKWindow();
278 wxASSERT_MSG( gdk_window
,
279 wxT("cannot use wxRendererNative on wxDC of this type") );
282 if ( flags
& wxCONTROL_CURRENT
)
283 state
= GTK_STATE_PRELIGHT
;
285 state
= GTK_STATE_NORMAL
;
288 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
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...
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
309 // ----------------------------------------------------------------------------
310 // splitter sash drawing
311 // ----------------------------------------------------------------------------
313 static int GetGtkSplitterFullSize()
315 static GtkWidget
*s_paned
= NULL
;
317 s_paned
= gtk_vpaned_new();
320 gtk_widget_style_get (s_paned
, "handle_size", &handle_size
, NULL
);
325 wxSplitterRenderParams
326 wxRendererGTK::GetSplitterParams(const wxWindow
*WXUNUSED(win
))
328 // we don't draw any border, hence 0 for the second field
329 return wxSplitterRenderParams
331 GetGtkSplitterFullSize(),
333 true // hot sensitive
338 wxRendererGTK::DrawSplitterBorder(wxWindow
* WXUNUSED(win
),
340 const wxRect
& WXUNUSED(rect
),
347 wxRendererGTK::DrawSplitterSash(wxWindow
*win
,
351 wxOrientation orient
,
354 if ( !win
->m_wxwindow
->window
)
356 // window not realized yet
360 GdkWindow
* gdk_window
= NULL
;
362 wxImplDC
*impl
= dc
.GetImpl();
363 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
365 gdk_window
= gtk_impl
->GetGDKWindow();
367 gdk_window
= dc
.GetGDKWindow();
369 wxASSERT_MSG( gdk_window
,
370 wxT("cannot use wxRendererNative on wxDC of this type") );
372 wxCoord full_size
= GetGtkSplitterFullSize();
374 // are we drawing vertical or horizontal splitter?
375 const bool isVert
= orient
== wxVERTICAL
;
381 int h
= win
->GetClientSize().GetHeight();
385 rect
.width
= full_size
;
390 int w
= win
->GetClientSize().GetWidth();
394 rect
.height
= full_size
;
399 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
404 win
->m_wxwindow
->style
,
406 flags
& wxCONTROL_CURRENT
? GTK_STATE_PRELIGHT
: GTK_STATE_NORMAL
,
408 NULL
/* no clipping */,
411 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
412 dc
.LogicalToDeviceY(rect
.y
),
415 isVert
? GTK_ORIENTATION_VERTICAL
: GTK_ORIENTATION_HORIZONTAL
420 wxRendererGTK::DrawDropArrow(wxWindow
*WXUNUSED(win
),
425 GtkWidget
*button
= GetButtonWidget();
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
;
434 wxImplDC
*impl
= dc
.GetImpl();
435 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
437 gdk_window
= gtk_impl
->GetGDKWindow();
439 gdk_window
= dc
.GetGDKWindow();
441 wxASSERT_MSG( gdk_window
,
442 wxT("cannot use wxRendererNative on wxDC of this type") );
444 // draw arrow so that there is even space horizontally
446 int arrowX
= rect
.width
/4 + 1;
447 int arrowWidth
= rect
.width
- (arrowX
*2);
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);
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
;
463 state
= GTK_STATE_NORMAL
;
465 // draw arrow on button
471 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
485 wxRendererGTK::DrawComboBoxDropButton(wxWindow
*win
,
490 DrawPushButton(win
,dc
,rect
,flags
);
491 DrawDropArrow(win
,dc
,rect
);
495 wxRendererGTK::DrawCheckBox(wxWindow
*WXUNUSED(win
),
500 GtkWidget
*button
= GetCheckButtonWidget();
502 GdkWindow
* gdk_window
= NULL
;
504 wxImplDC
*impl
= dc
.GetImpl();
505 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
507 gdk_window
= gtk_impl
->GetGDKWindow();
509 gdk_window
= dc
.GetGDKWindow();
511 wxASSERT_MSG( gdk_window
,
512 wxT("cannot use wxRendererNative on wxDC of this type") );
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
;
523 state
= GTK_STATE_NORMAL
;
530 flags
& wxCONTROL_CHECKED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
534 dc
.LogicalToDeviceX(rect
.x
)+2,
535 dc
.LogicalToDeviceY(rect
.y
)+3,
541 wxRendererGTK::DrawPushButton(wxWindow
*WXUNUSED(win
),
546 GtkWidget
*button
= GetButtonWidget();
548 GdkWindow
* gdk_window
= NULL
;
550 wxImplDC
*impl
= dc
.GetImpl();
551 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
553 gdk_window
= gtk_impl
->GetGDKWindow();
555 gdk_window
= dc
.GetGDKWindow();
557 wxASSERT_MSG( gdk_window
,
558 wxT("cannot use wxRendererNative on wxDC of this type") );
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
;
570 state
= GTK_STATE_NORMAL
;
577 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
581 rect
.x
, rect
.y
, rect
.width
, rect
.height
586 wxRendererGTK::DrawItemSelectionRect(wxWindow
*win
,
591 GdkWindow
* gdk_window
= NULL
;
593 wxImplDC
*impl
= dc
.GetImpl();
594 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
596 gdk_window
= gtk_impl
->GetGDKWindow();
598 gdk_window
= dc
.GetGDKWindow();
600 wxASSERT_MSG( gdk_window
,
601 wxT("cannot use wxRendererNative on wxDC of this type") );
604 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
608 if (flags
& wxCONTROL_SELECTED
)
610 // the wxCONTROL_FOCUSED state is deduced
611 // directly from the m_wxwindow by GTK+
612 state
= GTK_STATE_SELECTED
;
614 gtk_paint_flat_box( win
->m_widget
->style
,
621 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
622 dc
.LogicalToDeviceY(rect
.y
),
626 else // !wxCONTROL_SELECTED
628 state
= GTK_STATE_NORMAL
;
631 if ((flags
& wxCONTROL_CURRENT
) && (flags
& wxCONTROL_FOCUSED
))
633 gtk_paint_focus( win
->m_widget
->style
,
639 dc
.LogicalToDeviceX(rect
.x
),
640 dc
.LogicalToDeviceY(rect
.y
),
646 void wxRendererGTK::DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
648 GdkWindow
* gdk_window
= NULL
;
650 wxImplDC
*impl
= dc
.GetImpl();
651 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
653 gdk_window
= gtk_impl
->GetGDKWindow();
655 gdk_window
= dc
.GetGDKWindow();
657 wxASSERT_MSG( gdk_window
,
658 wxT("cannot use wxRendererNative on wxDC of this type") );
661 if (flags
& wxCONTROL_SELECTED
)
662 state
= GTK_STATE_SELECTED
;
664 state
= GTK_STATE_NORMAL
;
666 gtk_paint_focus( win
->m_widget
->style
,
672 dc
.LogicalToDeviceX(rect
.x
),
673 dc
.LogicalToDeviceY(rect
.y
),