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
;
383 rect
.width
= full_size
;
384 rect
.height
= size
.y
;
390 rect
.height
= full_size
;
395 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
400 win
->m_wxwindow
->style
,
402 flags
& wxCONTROL_CURRENT
? GTK_STATE_PRELIGHT
: GTK_STATE_NORMAL
,
404 NULL
/* no clipping */,
407 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
408 dc
.LogicalToDeviceY(rect
.y
),
411 isVert
? GTK_ORIENTATION_VERTICAL
: GTK_ORIENTATION_HORIZONTAL
416 wxRendererGTK::DrawDropArrow(wxWindow
*WXUNUSED(win
),
421 GtkWidget
*button
= GetButtonWidget();
423 // If we give GTK_PIZZA(win->m_wxwindow)->bin_window as
424 // a window for gtk_paint_xxx function, then it won't
425 // work for wxMemoryDC. So that is why we assume wxDC
426 // is wxWindowDC (wxClientDC, wxMemoryDC and wxPaintDC
427 // are derived from it) and use its m_window.
428 GdkWindow
* gdk_window
= NULL
;
430 wxImplDC
*impl
= dc
.GetImpl();
431 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
433 gdk_window
= gtk_impl
->GetGDKWindow();
435 gdk_window
= dc
.GetGDKWindow();
437 wxASSERT_MSG( gdk_window
,
438 wxT("cannot use wxRendererNative on wxDC of this type") );
440 // draw arrow so that there is even space horizontally
442 int arrowX
= rect
.width
/4 + 1;
443 int arrowWidth
= rect
.width
- (arrowX
*2);
445 // scale arrow's height accoording to the width
446 int arrowHeight
= rect
.width
/3;
447 int arrowY
= (rect
.height
-arrowHeight
)/2 +
448 ((rect
.height
-arrowHeight
) & 1);
452 if ( flags
& wxCONTROL_PRESSED
)
453 state
= GTK_STATE_ACTIVE
;
454 else if ( flags
& wxCONTROL_DISABLED
)
455 state
= GTK_STATE_INSENSITIVE
;
456 else if ( flags
& wxCONTROL_CURRENT
)
457 state
= GTK_STATE_PRELIGHT
;
459 state
= GTK_STATE_NORMAL
;
461 // draw arrow on button
467 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
481 wxRendererGTK::DrawComboBoxDropButton(wxWindow
*win
,
486 DrawPushButton(win
,dc
,rect
,flags
);
487 DrawDropArrow(win
,dc
,rect
);
491 wxRendererGTK::DrawCheckBox(wxWindow
*WXUNUSED(win
),
496 GtkWidget
*button
= GetCheckButtonWidget();
498 GdkWindow
* gdk_window
= NULL
;
500 wxImplDC
*impl
= dc
.GetImpl();
501 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
503 gdk_window
= gtk_impl
->GetGDKWindow();
505 gdk_window
= dc
.GetGDKWindow();
507 wxASSERT_MSG( gdk_window
,
508 wxT("cannot use wxRendererNative on wxDC of this type") );
512 if ( flags
& wxCONTROL_PRESSED
)
513 state
= GTK_STATE_ACTIVE
;
514 else if ( flags
& wxCONTROL_DISABLED
)
515 state
= GTK_STATE_INSENSITIVE
;
516 else if ( flags
& wxCONTROL_CURRENT
)
517 state
= GTK_STATE_PRELIGHT
;
519 state
= GTK_STATE_NORMAL
;
526 flags
& wxCONTROL_CHECKED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
530 dc
.LogicalToDeviceX(rect
.x
)+2,
531 dc
.LogicalToDeviceY(rect
.y
)+3,
537 wxRendererGTK::DrawPushButton(wxWindow
*WXUNUSED(win
),
542 GtkWidget
*button
= GetButtonWidget();
544 GdkWindow
* gdk_window
= NULL
;
546 wxImplDC
*impl
= dc
.GetImpl();
547 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
549 gdk_window
= gtk_impl
->GetGDKWindow();
551 gdk_window
= dc
.GetGDKWindow();
553 wxASSERT_MSG( gdk_window
,
554 wxT("cannot use wxRendererNative on wxDC of this type") );
559 if ( flags
& wxCONTROL_PRESSED
)
560 state
= GTK_STATE_ACTIVE
;
561 else if ( flags
& wxCONTROL_DISABLED
)
562 state
= GTK_STATE_INSENSITIVE
;
563 else if ( flags
& wxCONTROL_CURRENT
)
564 state
= GTK_STATE_PRELIGHT
;
566 state
= GTK_STATE_NORMAL
;
573 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
577 rect
.x
, rect
.y
, rect
.width
, rect
.height
582 wxRendererGTK::DrawItemSelectionRect(wxWindow
*win
,
587 GdkWindow
* gdk_window
= NULL
;
589 wxImplDC
*impl
= dc
.GetImpl();
590 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
592 gdk_window
= gtk_impl
->GetGDKWindow();
594 gdk_window
= dc
.GetGDKWindow();
596 wxASSERT_MSG( gdk_window
,
597 wxT("cannot use wxRendererNative on wxDC of this type") );
600 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
604 if (flags
& wxCONTROL_SELECTED
)
606 // the wxCONTROL_FOCUSED state is deduced
607 // directly from the m_wxwindow by GTK+
608 state
= GTK_STATE_SELECTED
;
610 gtk_paint_flat_box( win
->m_widget
->style
,
617 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
618 dc
.LogicalToDeviceY(rect
.y
),
622 else // !wxCONTROL_SELECTED
624 state
= GTK_STATE_NORMAL
;
627 if ((flags
& wxCONTROL_CURRENT
) && (flags
& wxCONTROL_FOCUSED
))
629 gtk_paint_focus( win
->m_widget
->style
,
635 dc
.LogicalToDeviceX(rect
.x
),
636 dc
.LogicalToDeviceY(rect
.y
),
642 void wxRendererGTK::DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
644 GdkWindow
* gdk_window
= NULL
;
646 wxImplDC
*impl
= dc
.GetImpl();
647 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
649 gdk_window
= gtk_impl
->GetGDKWindow();
651 gdk_window
= dc
.GetGDKWindow();
653 wxASSERT_MSG( gdk_window
,
654 wxT("cannot use wxRendererNative on wxDC of this type") );
657 if (flags
& wxCONTROL_SELECTED
)
658 state
= GTK_STATE_SELECTED
;
660 state
= GTK_STATE_NORMAL
;
662 gtk_paint_focus( win
->m_widget
->style
,
668 dc
.LogicalToDeviceX(rect
.x
),
669 dc
.LogicalToDeviceY(rect
.y
),