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"
33 #include "wx/module.h"
36 #include "wx/dcgraph.h"
37 #include "wx/gtk/dc.h"
38 #include "wx/gtk/private.h"
42 // ----------------------------------------------------------------------------
43 // wxRendererGTK: our wxRendererNative implementation
44 // ----------------------------------------------------------------------------
46 class WXDLLEXPORT wxRendererGTK
: public wxDelegateRendererNative
49 // draw the header control button (used by wxListCtrl)
50 virtual int DrawHeaderButton(wxWindow
*win
,
54 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
,
55 wxHeaderButtonParams
* params
= NULL
);
57 // draw the expanded/collapsed icon for a tree control item
58 virtual void DrawTreeItemButton(wxWindow
*win
,
63 virtual void DrawSplitterBorder(wxWindow
*win
,
67 virtual void DrawSplitterSash(wxWindow
*win
,
74 virtual void DrawComboBoxDropButton(wxWindow
*win
,
79 virtual void DrawDropArrow(wxWindow
*win
,
84 virtual void DrawCheckBox(wxWindow
*win
,
89 virtual void DrawPushButton(wxWindow
*win
,
94 virtual void DrawItemSelectionRect(wxWindow
*win
,
99 virtual void DrawChoice(wxWindow
* win
,
104 virtual void DrawComboBox(wxWindow
* win
,
109 virtual void DrawTextCtrl(wxWindow
* win
,
114 virtual void DrawRadioButton(wxWindow
* win
,
119 virtual void DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
= 0);
121 virtual wxSize
GetCheckBoxSize(wxWindow
*win
);
123 virtual wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
126 // ============================================================================
128 // ============================================================================
131 wxRendererNative
& wxRendererNative::GetDefault()
133 static wxRendererGTK s_rendererGTK
;
135 return s_rendererGTK
;
138 static GdkWindow
* wxGetGdkWindowForDC(wxWindow
* win
, wxDC
& dc
)
140 GdkWindow
* gdk_window
= NULL
;
142 #if wxUSE_GRAPHICS_CONTEXT
143 if ( dc
.IsKindOf( CLASSINFO(wxGCDC
) ) )
144 gdk_window
= win
->GTKGetDrawingWindow();
149 wxDCImpl
*impl
= dc
.GetImpl();
150 wxGTKDCImpl
*gtk_impl
= wxDynamicCast( impl
, wxGTKDCImpl
);
152 gdk_window
= gtk_impl
->GetGDKWindow();
154 gdk_window
= dc
.GetGDKWindow();
160 // ----------------------------------------------------------------------------
161 // list/tree controls drawing
162 // ----------------------------------------------------------------------------
165 wxRendererGTK::DrawHeaderButton(wxWindow
*win
,
169 wxHeaderSortIconType sortArrow
,
170 wxHeaderButtonParams
* params
)
173 GtkWidget
*button
= wxGTKPrivate::GetHeaderButtonWidget();
174 if (flags
& wxCONTROL_SPECIAL
)
175 button
= wxGTKPrivate::GetHeaderButtonWidgetFirst();
177 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
178 wxASSERT_MSG( gdk_window
,
179 wxT("cannot use wxRendererNative on wxDC of this type") );
182 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
185 GtkStateType state
= GTK_STATE_NORMAL
;
186 if (flags
& wxCONTROL_DISABLED
)
187 state
= GTK_STATE_INSENSITIVE
;
190 if (flags
& wxCONTROL_CURRENT
)
191 state
= GTK_STATE_PRELIGHT
;
203 dc
.LogicalToDeviceX(rect
.x
) - x_diff
, rect
.y
, rect
.width
, rect
.height
206 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
209 // draw a ">" or "v" button
211 wxRendererGTK::DrawTreeItemButton(wxWindow
* win
,
212 wxDC
& dc
, const wxRect
& rect
, int flags
)
214 GtkWidget
*tree
= wxGTKPrivate::GetTreeWidget();
216 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
217 wxASSERT_MSG( gdk_window
,
218 wxT("cannot use wxRendererNative on wxDC of this type") );
221 if ( flags
& wxCONTROL_CURRENT
)
222 state
= GTK_STATE_PRELIGHT
;
224 state
= GTK_STATE_NORMAL
;
227 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
230 // VZ: I don't know how to get the size of the expander so as to centre it
231 // in the given rectangle, +2/3 below is just what looks good here...
240 dc
.LogicalToDeviceX(rect
.x
) + 6 - x_diff
,
241 dc
.LogicalToDeviceY(rect
.y
) + 3,
242 flags
& wxCONTROL_EXPANDED
? GTK_EXPANDER_EXPANDED
243 : GTK_EXPANDER_COLLAPSED
248 // ----------------------------------------------------------------------------
249 // splitter sash drawing
250 // ----------------------------------------------------------------------------
252 static int GetGtkSplitterFullSize(GtkWidget
* widget
)
255 gtk_widget_style_get(widget
, "handle_size", &handle_size
, NULL
);
260 wxSplitterRenderParams
261 wxRendererGTK::GetSplitterParams(const wxWindow
*WXUNUSED(win
))
263 // we don't draw any border, hence 0 for the second field
264 return wxSplitterRenderParams
266 GetGtkSplitterFullSize(wxGTKPrivate::GetSplitterWidget()),
268 true // hot sensitive
273 wxRendererGTK::DrawSplitterBorder(wxWindow
* WXUNUSED(win
),
275 const wxRect
& WXUNUSED(rect
),
282 wxRendererGTK::DrawSplitterSash(wxWindow
* win
,
286 wxOrientation orient
,
289 if ( !win
->m_wxwindow
->window
)
291 // window not realized yet
295 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
296 wxASSERT_MSG( gdk_window
,
297 wxT("cannot use wxRendererNative on wxDC of this type") );
299 wxCoord full_size
= GetGtkSplitterFullSize(wxGTKPrivate::GetSplitterWidget());
301 // are we drawing vertical or horizontal splitter?
302 const bool isVert
= orient
== wxVERTICAL
;
310 rect
.width
= full_size
;
311 rect
.height
= size
.y
;
317 rect
.height
= full_size
;
322 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
327 win
->m_wxwindow
->style
,
329 flags
& wxCONTROL_CURRENT
? GTK_STATE_PRELIGHT
: GTK_STATE_NORMAL
,
331 NULL
/* no clipping */,
334 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
335 dc
.LogicalToDeviceY(rect
.y
),
338 isVert
? GTK_ORIENTATION_VERTICAL
: GTK_ORIENTATION_HORIZONTAL
343 wxRendererGTK::DrawDropArrow(wxWindow
* win
,
348 GtkWidget
*button
= wxGTKPrivate::GetButtonWidget();
350 // If we give WX_PIZZA(win->m_wxwindow)->bin_window as
351 // a window for gtk_paint_xxx function, then it won't
352 // work for wxMemoryDC. So that is why we assume wxDC
353 // is wxWindowDC (wxClientDC, wxMemoryDC and wxPaintDC
354 // are derived from it) and use its m_window.
355 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
356 wxASSERT_MSG( gdk_window
,
357 wxT("cannot use wxRendererNative on wxDC of this type") );
359 // draw arrow so that there is even space horizontally
361 int arrowX
= rect
.width
/4 + 1;
362 int arrowWidth
= rect
.width
- (arrowX
*2);
364 // scale arrow's height accoording to the width
365 int arrowHeight
= rect
.width
/3;
366 int arrowY
= (rect
.height
-arrowHeight
)/2 +
367 ((rect
.height
-arrowHeight
) & 1);
371 if ( flags
& wxCONTROL_PRESSED
)
372 state
= GTK_STATE_ACTIVE
;
373 else if ( flags
& wxCONTROL_DISABLED
)
374 state
= GTK_STATE_INSENSITIVE
;
375 else if ( flags
& wxCONTROL_CURRENT
)
376 state
= GTK_STATE_PRELIGHT
;
378 state
= GTK_STATE_NORMAL
;
380 // draw arrow on button
386 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
400 wxRendererGTK::DrawComboBoxDropButton(wxWindow
*win
,
405 DrawPushButton(win
,dc
,rect
,flags
);
406 DrawDropArrow(win
,dc
,rect
);
410 wxRendererGTK::GetCheckBoxSize(wxWindow
*WXUNUSED(win
))
412 gint indicator_size
, indicator_spacing
;
413 gtk_widget_style_get(wxGTKPrivate::GetCheckButtonWidget(),
414 "indicator_size", &indicator_size
,
415 "indicator_spacing", &indicator_spacing
,
418 int size
= indicator_size
+ indicator_spacing
* 2;
419 return wxSize(size
, size
);
423 wxRendererGTK::DrawCheckBox(wxWindow
* win
,
428 GtkWidget
*button
= wxGTKPrivate::GetCheckButtonWidget();
430 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
431 wxASSERT_MSG( gdk_window
,
432 wxT("cannot use wxRendererNative on wxDC of this type") );
434 gint indicator_size
, indicator_spacing
;
435 gtk_widget_style_get(button
,
436 "indicator_size", &indicator_size
,
437 "indicator_spacing", &indicator_spacing
,
442 if ( flags
& wxCONTROL_PRESSED
)
443 state
= GTK_STATE_ACTIVE
;
444 else if ( flags
& wxCONTROL_DISABLED
)
445 state
= GTK_STATE_INSENSITIVE
;
446 else if ( flags
& wxCONTROL_CURRENT
)
447 state
= GTK_STATE_PRELIGHT
;
449 state
= GTK_STATE_NORMAL
;
456 flags
& wxCONTROL_CHECKED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
460 dc
.LogicalToDeviceX(rect
.x
) + indicator_spacing
,
461 dc
.LogicalToDeviceY(rect
.y
) + indicator_spacing
,
462 indicator_size
, indicator_size
467 wxRendererGTK::DrawPushButton(wxWindow
* win
,
472 GtkWidget
*button
= wxGTKPrivate::GetButtonWidget();
474 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
475 wxASSERT_MSG( gdk_window
,
476 wxT("cannot use wxRendererNative on wxDC of this type") );
481 if ( flags
& wxCONTROL_PRESSED
)
482 state
= GTK_STATE_ACTIVE
;
483 else if ( flags
& wxCONTROL_DISABLED
)
484 state
= GTK_STATE_INSENSITIVE
;
485 else if ( flags
& wxCONTROL_CURRENT
)
486 state
= GTK_STATE_PRELIGHT
;
488 state
= GTK_STATE_NORMAL
;
495 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
499 dc
.LogicalToDeviceX(rect
.x
),
500 dc
.LogicalToDeviceY(rect
.y
),
507 wxRendererGTK::DrawItemSelectionRect(wxWindow
* win
,
512 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
513 wxASSERT_MSG( gdk_window
,
514 wxT("cannot use wxRendererNative on wxDC of this type") );
517 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
521 if (flags
& wxCONTROL_SELECTED
)
523 // the wxCONTROL_FOCUSED state is deduced
524 // directly from the m_wxwindow by GTK+
525 state
= GTK_STATE_SELECTED
;
527 gtk_paint_flat_box( win
->m_widget
->style
,
534 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
535 dc
.LogicalToDeviceY(rect
.y
),
539 else // !wxCONTROL_SELECTED
541 state
= GTK_STATE_NORMAL
;
544 if ((flags
& wxCONTROL_CURRENT
) && (flags
& wxCONTROL_FOCUSED
))
546 gtk_paint_focus( win
->m_widget
->style
,
551 // Detail "treeview" causes warning with GTK+ 2.12 Clearlooks theme:
552 // "... no property named `row-ending-details'"
553 // Using "treeview-middle" would fix the warning, but the right
554 // edge of the focus rect is not getting erased properly either.
555 // Better to not specify this detail unless the drawing is fixed.
557 dc
.LogicalToDeviceX(rect
.x
),
558 dc
.LogicalToDeviceY(rect
.y
),
564 void wxRendererGTK::DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
566 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
567 wxASSERT_MSG( gdk_window
,
568 wxT("cannot use wxRendererNative on wxDC of this type") );
571 if (flags
& wxCONTROL_SELECTED
)
572 state
= GTK_STATE_SELECTED
;
574 state
= GTK_STATE_NORMAL
;
576 gtk_paint_focus( win
->m_widget
->style
,
582 dc
.LogicalToDeviceX(rect
.x
),
583 dc
.LogicalToDeviceY(rect
.y
),
588 // Uses the theme to draw the border and fill for something like a wxTextCtrl
589 void wxRendererGTK::DrawTextCtrl(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
591 GtkWidget
*entry
= wxGTKPrivate::GetTextEntryWidget();
593 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
595 GtkStateType state
= GTK_STATE_NORMAL
;
596 if ( flags
& wxCONTROL_DISABLED
)
597 state
= GTK_STATE_INSENSITIVE
;
599 if (flags
& wxCONTROL_CURRENT
)
600 GTK_WIDGET_SET_FLAGS( entry
, GTK_HAS_FOCUS
);
602 GTK_WIDGET_UNSET_FLAGS( entry
, GTK_HAS_FOCUS
);
613 dc
.LogicalToDeviceX(rect
.x
),
614 dc
.LogicalToDeviceY(rect
.y
),
620 // Draw the equivallent of a wxComboBox
621 void wxRendererGTK::DrawComboBox(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
623 GtkWidget
*combo
= wxGTKPrivate::GetComboBoxWidget();
625 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
627 GtkStateType state
= GTK_STATE_NORMAL
;
628 if ( flags
& wxCONTROL_DISABLED
)
629 state
= GTK_STATE_INSENSITIVE
;
631 if (flags
& wxCONTROL_CURRENT
)
632 GTK_WIDGET_SET_FLAGS( combo
, GTK_HAS_FOCUS
);
634 GTK_WIDGET_UNSET_FLAGS( combo
, GTK_HAS_FOCUS
);
645 dc
.LogicalToDeviceX(rect
.x
),
646 dc
.LogicalToDeviceY(rect
.y
),
652 int extent
= rect
.height
/ 2;
653 r
.x
+= rect
.width
- extent
- extent
/2;
669 dc
.LogicalToDeviceX(r
.x
),
670 dc
.LogicalToDeviceY(r
.y
),
676 r
.x
+= rect
.width
- 2*extent
;
684 GTK_SHADOW_ETCHED_OUT
,
688 dc
.LogicalToDeviceX(r
.x
),
689 dc
.LogicalToDeviceY(r
.y
+1),
696 void wxRendererGTK::DrawChoice(wxWindow
* win
, wxDC
& dc
,
697 const wxRect
& rect
, int flags
)
699 DrawComboBox( win
, dc
, rect
, flags
);
703 // Draw a themed radio button
704 void wxRendererGTK::DrawRadioButton(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
706 GtkWidget
*button
= wxGTKPrivate::GetRadioButtonWidget();
708 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
710 GtkShadowType shadow_type
= GTK_SHADOW_OUT
;
711 if ( flags
& wxCONTROL_CHECKED
)
712 shadow_type
= GTK_SHADOW_IN
;
713 else if ( flags
& wxCONTROL_UNDETERMINED
)
714 shadow_type
= GTK_SHADOW_ETCHED_IN
;
716 GtkStateType state
= GTK_STATE_NORMAL
;
717 if ( flags
& wxCONTROL_DISABLED
)
718 state
= GTK_STATE_INSENSITIVE
;
719 if ( flags
& wxCONTROL_PRESSED
)
720 state
= GTK_STATE_ACTIVE
;
722 Don't know when to set this
723 state_type = GTK_STATE_PRELIGHT;
735 dc
.LogicalToDeviceX(rect
.x
),
736 dc
.LogicalToDeviceY(rect
.y
),
737 rect
.width
, rect
.height