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();
175 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
176 wxASSERT_MSG( gdk_window
,
177 wxT("cannot use wxRendererNative on wxDC of this type") );
180 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
183 GtkStateType state
= GTK_STATE_NORMAL
;
184 if (flags
& wxCONTROL_DISABLED
)
185 state
= GTK_STATE_INSENSITIVE
;
188 if (flags
& wxCONTROL_CURRENT
)
189 state
= GTK_STATE_PRELIGHT
;
201 dc
.LogicalToDeviceX(rect
.x
) - x_diff
, rect
.y
, rect
.width
, rect
.height
204 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
207 // draw a ">" or "v" button
209 wxRendererGTK::DrawTreeItemButton(wxWindow
* win
,
210 wxDC
& dc
, const wxRect
& rect
, int flags
)
212 GtkWidget
*tree
= wxGTKPrivate::GetTreeWidget();
214 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
215 wxASSERT_MSG( gdk_window
,
216 wxT("cannot use wxRendererNative on wxDC of this type") );
219 if ( flags
& wxCONTROL_CURRENT
)
220 state
= GTK_STATE_PRELIGHT
;
222 state
= GTK_STATE_NORMAL
;
225 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
228 // VZ: I don't know how to get the size of the expander so as to centre it
229 // in the given rectangle, +2/3 below is just what looks good here...
238 dc
.LogicalToDeviceX(rect
.x
) + 6 - x_diff
,
239 dc
.LogicalToDeviceY(rect
.y
) + 3,
240 flags
& wxCONTROL_EXPANDED
? GTK_EXPANDER_EXPANDED
241 : GTK_EXPANDER_COLLAPSED
246 // ----------------------------------------------------------------------------
247 // splitter sash drawing
248 // ----------------------------------------------------------------------------
250 static int GetGtkSplitterFullSize(GtkWidget
* widget
)
253 gtk_widget_style_get(widget
, "handle_size", &handle_size
, NULL
);
258 wxSplitterRenderParams
259 wxRendererGTK::GetSplitterParams(const wxWindow
*WXUNUSED(win
))
261 // we don't draw any border, hence 0 for the second field
262 return wxSplitterRenderParams
264 GetGtkSplitterFullSize(wxGTKPrivate::GetSplitterWidget()),
266 true // hot sensitive
271 wxRendererGTK::DrawSplitterBorder(wxWindow
* WXUNUSED(win
),
273 const wxRect
& WXUNUSED(rect
),
280 wxRendererGTK::DrawSplitterSash(wxWindow
* win
,
284 wxOrientation orient
,
287 if ( !win
->m_wxwindow
->window
)
289 // window not realized yet
293 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
294 wxASSERT_MSG( gdk_window
,
295 wxT("cannot use wxRendererNative on wxDC of this type") );
297 wxCoord full_size
= GetGtkSplitterFullSize(wxGTKPrivate::GetSplitterWidget());
299 // are we drawing vertical or horizontal splitter?
300 const bool isVert
= orient
== wxVERTICAL
;
308 rect
.width
= full_size
;
309 rect
.height
= size
.y
;
315 rect
.height
= full_size
;
320 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
325 win
->m_wxwindow
->style
,
327 flags
& wxCONTROL_CURRENT
? GTK_STATE_PRELIGHT
: GTK_STATE_NORMAL
,
329 NULL
/* no clipping */,
332 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
333 dc
.LogicalToDeviceY(rect
.y
),
336 isVert
? GTK_ORIENTATION_VERTICAL
: GTK_ORIENTATION_HORIZONTAL
341 wxRendererGTK::DrawDropArrow(wxWindow
* win
,
346 GtkWidget
*button
= wxGTKPrivate::GetButtonWidget();
348 // If we give WX_PIZZA(win->m_wxwindow)->bin_window as
349 // a window for gtk_paint_xxx function, then it won't
350 // work for wxMemoryDC. So that is why we assume wxDC
351 // is wxWindowDC (wxClientDC, wxMemoryDC and wxPaintDC
352 // are derived from it) and use its m_window.
353 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
354 wxASSERT_MSG( gdk_window
,
355 wxT("cannot use wxRendererNative on wxDC of this type") );
357 // draw arrow so that there is even space horizontally
359 int arrowX
= rect
.width
/4 + 1;
360 int arrowWidth
= rect
.width
- (arrowX
*2);
362 // scale arrow's height accoording to the width
363 int arrowHeight
= rect
.width
/3;
364 int arrowY
= (rect
.height
-arrowHeight
)/2 +
365 ((rect
.height
-arrowHeight
) & 1);
369 if ( flags
& wxCONTROL_PRESSED
)
370 state
= GTK_STATE_ACTIVE
;
371 else if ( flags
& wxCONTROL_DISABLED
)
372 state
= GTK_STATE_INSENSITIVE
;
373 else if ( flags
& wxCONTROL_CURRENT
)
374 state
= GTK_STATE_PRELIGHT
;
376 state
= GTK_STATE_NORMAL
;
378 // draw arrow on button
384 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
398 wxRendererGTK::DrawComboBoxDropButton(wxWindow
*win
,
403 DrawPushButton(win
,dc
,rect
,flags
);
404 DrawDropArrow(win
,dc
,rect
);
408 wxRendererGTK::GetCheckBoxSize(wxWindow
*WXUNUSED(win
))
410 gint indicator_size
, indicator_spacing
;
411 gtk_widget_style_get(wxGTKPrivate::GetCheckButtonWidget(),
412 "indicator_size", &indicator_size
,
413 "indicator_spacing", &indicator_spacing
,
416 int size
= indicator_size
+ indicator_spacing
* 2;
417 return wxSize(size
, size
);
421 wxRendererGTK::DrawCheckBox(wxWindow
* win
,
426 GtkWidget
*button
= wxGTKPrivate::GetCheckButtonWidget();
428 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
429 wxASSERT_MSG( gdk_window
,
430 wxT("cannot use wxRendererNative on wxDC of this type") );
432 gint indicator_size
, indicator_spacing
;
433 gtk_widget_style_get(button
,
434 "indicator_size", &indicator_size
,
435 "indicator_spacing", &indicator_spacing
,
440 if ( flags
& wxCONTROL_PRESSED
)
441 state
= GTK_STATE_ACTIVE
;
442 else if ( flags
& wxCONTROL_DISABLED
)
443 state
= GTK_STATE_INSENSITIVE
;
444 else if ( flags
& wxCONTROL_CURRENT
)
445 state
= GTK_STATE_PRELIGHT
;
447 state
= GTK_STATE_NORMAL
;
454 flags
& wxCONTROL_CHECKED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
458 dc
.LogicalToDeviceX(rect
.x
) + indicator_spacing
,
459 dc
.LogicalToDeviceY(rect
.y
) + indicator_spacing
,
460 indicator_size
, indicator_size
465 wxRendererGTK::DrawPushButton(wxWindow
* win
,
470 GtkWidget
*button
= wxGTKPrivate::GetButtonWidget();
472 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
473 wxASSERT_MSG( gdk_window
,
474 wxT("cannot use wxRendererNative on wxDC of this type") );
479 if ( flags
& wxCONTROL_PRESSED
)
480 state
= GTK_STATE_ACTIVE
;
481 else if ( flags
& wxCONTROL_DISABLED
)
482 state
= GTK_STATE_INSENSITIVE
;
483 else if ( flags
& wxCONTROL_CURRENT
)
484 state
= GTK_STATE_PRELIGHT
;
486 state
= GTK_STATE_NORMAL
;
493 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
497 dc
.LogicalToDeviceX(rect
.x
),
498 dc
.LogicalToDeviceY(rect
.y
),
505 wxRendererGTK::DrawItemSelectionRect(wxWindow
* win
,
510 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
511 wxASSERT_MSG( gdk_window
,
512 wxT("cannot use wxRendererNative on wxDC of this type") );
515 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
519 if (flags
& wxCONTROL_SELECTED
)
521 // the wxCONTROL_FOCUSED state is deduced
522 // directly from the m_wxwindow by GTK+
523 state
= GTK_STATE_SELECTED
;
525 gtk_paint_flat_box( win
->m_widget
->style
,
532 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
533 dc
.LogicalToDeviceY(rect
.y
),
537 else // !wxCONTROL_SELECTED
539 state
= GTK_STATE_NORMAL
;
542 if ((flags
& wxCONTROL_CURRENT
) && (flags
& wxCONTROL_FOCUSED
))
544 gtk_paint_focus( win
->m_widget
->style
,
549 // Detail "treeview" causes warning with GTK+ 2.12 Clearlooks theme:
550 // "... no property named `row-ending-details'"
551 // Using "treeview-middle" would fix the warning, but the right
552 // edge of the focus rect is not getting erased properly either.
553 // Better to not specify this detail unless the drawing is fixed.
555 dc
.LogicalToDeviceX(rect
.x
),
556 dc
.LogicalToDeviceY(rect
.y
),
562 void wxRendererGTK::DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
564 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
565 wxASSERT_MSG( gdk_window
,
566 wxT("cannot use wxRendererNative on wxDC of this type") );
569 if (flags
& wxCONTROL_SELECTED
)
570 state
= GTK_STATE_SELECTED
;
572 state
= GTK_STATE_NORMAL
;
574 gtk_paint_focus( win
->m_widget
->style
,
580 dc
.LogicalToDeviceX(rect
.x
),
581 dc
.LogicalToDeviceY(rect
.y
),
586 // Uses the theme to draw the border and fill for something like a wxTextCtrl
587 void wxRendererGTK::DrawTextCtrl(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
589 GtkWidget
*entry
= wxGTKPrivate::GetTextEntryWidget();
591 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
593 GtkStateType state
= GTK_STATE_NORMAL
;
594 if ( flags
& wxCONTROL_DISABLED
)
595 state
= GTK_STATE_INSENSITIVE
;
597 if (flags
& wxCONTROL_CURRENT
)
598 GTK_WIDGET_SET_FLAGS( entry
, GTK_HAS_FOCUS
);
600 GTK_WIDGET_UNSET_FLAGS( entry
, GTK_HAS_FOCUS
);
611 dc
.LogicalToDeviceX(rect
.x
),
612 dc
.LogicalToDeviceY(rect
.y
),
618 // Draw the equivallent of a wxComboBox
619 void wxRendererGTK::DrawComboBox(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
621 GtkWidget
*combo
= wxGTKPrivate::GetComboBoxWidget();
623 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
625 GtkStateType state
= GTK_STATE_NORMAL
;
626 if ( flags
& wxCONTROL_DISABLED
)
627 state
= GTK_STATE_INSENSITIVE
;
629 if (flags
& wxCONTROL_CURRENT
)
630 GTK_WIDGET_SET_FLAGS( combo
, GTK_HAS_FOCUS
);
632 GTK_WIDGET_UNSET_FLAGS( combo
, GTK_HAS_FOCUS
);
643 dc
.LogicalToDeviceX(rect
.x
),
644 dc
.LogicalToDeviceY(rect
.y
),
650 int extent
= rect
.height
/ 2;
651 r
.x
+= rect
.width
- extent
- extent
/2;
667 dc
.LogicalToDeviceX(r
.x
),
668 dc
.LogicalToDeviceY(r
.y
),
674 r
.x
+= rect
.width
- 2*extent
;
682 GTK_SHADOW_ETCHED_OUT
,
686 dc
.LogicalToDeviceX(r
.x
),
687 dc
.LogicalToDeviceY(r
.y
+1),
694 void wxRendererGTK::DrawChoice(wxWindow
* win
, wxDC
& dc
,
695 const wxRect
& rect
, int flags
)
697 DrawComboBox( win
, dc
, rect
, flags
);
701 // Draw a themed radio button
702 void wxRendererGTK::DrawRadioButton(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
704 GtkWidget
*button
= wxGTKPrivate::GetRadioButtonWidget();
706 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
708 GtkShadowType shadow_type
= GTK_SHADOW_OUT
;
709 if ( flags
& wxCONTROL_CHECKED
)
710 shadow_type
= GTK_SHADOW_IN
;
711 else if ( flags
& wxCONTROL_UNDETERMINED
)
712 shadow_type
= GTK_SHADOW_ETCHED_IN
;
714 GtkStateType state
= GTK_STATE_NORMAL
;
715 if ( flags
& wxCONTROL_DISABLED
)
716 state
= GTK_STATE_INSENSITIVE
;
717 if ( flags
& wxCONTROL_PRESSED
)
718 state
= GTK_STATE_ACTIVE
;
720 Don't know when to set this
721 state_type = GTK_STATE_PRELIGHT;
733 dc
.LogicalToDeviceX(rect
.x
),
734 dc
.LogicalToDeviceY(rect
.y
),
735 rect
.width
, rect
.height