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 // Licence: 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 virtual int GetHeaderButtonHeight(wxWindow
*win
);
59 virtual int GetHeaderButtonMargin(wxWindow
*win
);
62 // draw the expanded/collapsed icon for a tree control item
63 virtual void DrawTreeItemButton(wxWindow
*win
,
68 virtual void DrawSplitterBorder(wxWindow
*win
,
72 virtual void DrawSplitterSash(wxWindow
*win
,
79 virtual void DrawComboBoxDropButton(wxWindow
*win
,
84 virtual void DrawDropArrow(wxWindow
*win
,
89 virtual void DrawCheckBox(wxWindow
*win
,
94 virtual void DrawPushButton(wxWindow
*win
,
99 virtual void DrawItemSelectionRect(wxWindow
*win
,
104 virtual void DrawChoice(wxWindow
* win
,
109 virtual void DrawComboBox(wxWindow
* win
,
114 virtual void DrawTextCtrl(wxWindow
* win
,
119 virtual void DrawRadioBitmap(wxWindow
* win
,
124 virtual void DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
= 0);
126 virtual wxSize
GetCheckBoxSize(wxWindow
*win
);
128 virtual wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
131 // ============================================================================
133 // ============================================================================
136 wxRendererNative
& wxRendererNative::GetDefault()
138 static wxRendererGTK s_rendererGTK
;
140 return s_rendererGTK
;
143 static GdkWindow
* wxGetGdkWindowForDC(wxWindow
* win
, wxDC
& dc
)
145 GdkWindow
* gdk_window
= NULL
;
147 #if wxUSE_GRAPHICS_CONTEXT
148 if ( dc
.IsKindOf( CLASSINFO(wxGCDC
) ) )
149 gdk_window
= win
->GTKGetDrawingWindow();
154 wxDCImpl
*impl
= dc
.GetImpl();
155 wxGTKDCImpl
*gtk_impl
= wxDynamicCast( impl
, wxGTKDCImpl
);
157 gdk_window
= gtk_impl
->GetGDKWindow();
159 gdk_window
= dc
.GetGDKWindow();
163 #if !wxUSE_GRAPHICS_CONTEXT
170 // ----------------------------------------------------------------------------
171 // list/tree controls drawing
172 // ----------------------------------------------------------------------------
175 wxRendererGTK::DrawHeaderButton(wxWindow
*win
,
179 wxHeaderSortIconType sortArrow
,
180 wxHeaderButtonParams
* params
)
183 GtkWidget
*button
= wxGTKPrivate::GetHeaderButtonWidget();
184 if (flags
& wxCONTROL_SPECIAL
)
185 button
= wxGTKPrivate::GetHeaderButtonWidgetFirst();
186 if (flags
& wxCONTROL_DIRTY
)
187 button
= wxGTKPrivate::GetHeaderButtonWidgetLast();
189 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
190 wxASSERT_MSG( gdk_window
,
191 wxT("cannot use wxRendererNative on wxDC of this type") );
194 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
197 GtkStateType state
= GTK_STATE_NORMAL
;
198 if (flags
& wxCONTROL_DISABLED
)
199 state
= GTK_STATE_INSENSITIVE
;
202 if (flags
& wxCONTROL_CURRENT
)
203 state
= GTK_STATE_PRELIGHT
;
215 dc
.LogicalToDeviceX(rect
.x
) - x_diff
, rect
.y
, rect
.width
, rect
.height
218 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
221 int wxRendererGTK::GetHeaderButtonHeight(wxWindow
*WXUNUSED(win
))
223 GtkWidget
*button
= wxGTKPrivate::GetHeaderButtonWidget();
226 GTK_WIDGET_GET_CLASS(button
)->size_request(button
, &req
);
231 int wxRendererGTK::GetHeaderButtonMargin(wxWindow
*WXUNUSED(win
))
233 wxFAIL_MSG( "GetHeaderButtonMargin() not implemented" );
238 // draw a ">" or "v" button
240 wxRendererGTK::DrawTreeItemButton(wxWindow
* win
,
241 wxDC
& dc
, const wxRect
& rect
, int flags
)
243 GtkWidget
*tree
= wxGTKPrivate::GetTreeWidget();
245 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
246 wxASSERT_MSG( gdk_window
,
247 wxT("cannot use wxRendererNative on wxDC of this type") );
250 if ( flags
& wxCONTROL_CURRENT
)
251 state
= GTK_STATE_PRELIGHT
;
253 state
= GTK_STATE_NORMAL
;
256 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
259 // VZ: I don't know how to get the size of the expander so as to centre it
260 // in the given rectangle, +2/3 below is just what looks good here...
269 dc
.LogicalToDeviceX(rect
.x
) + 6 - x_diff
,
270 dc
.LogicalToDeviceY(rect
.y
) + 3,
271 flags
& wxCONTROL_EXPANDED
? GTK_EXPANDER_EXPANDED
272 : GTK_EXPANDER_COLLAPSED
277 // ----------------------------------------------------------------------------
278 // splitter sash drawing
279 // ----------------------------------------------------------------------------
281 static int GetGtkSplitterFullSize(GtkWidget
* widget
)
284 gtk_widget_style_get(widget
, "handle_size", &handle_size
, NULL
);
289 wxSplitterRenderParams
290 wxRendererGTK::GetSplitterParams(const wxWindow
*WXUNUSED(win
))
292 // we don't draw any border, hence 0 for the second field
293 return wxSplitterRenderParams
295 GetGtkSplitterFullSize(wxGTKPrivate::GetSplitterWidget()),
297 true // hot sensitive
302 wxRendererGTK::DrawSplitterBorder(wxWindow
* WXUNUSED(win
),
304 const wxRect
& WXUNUSED(rect
),
311 wxRendererGTK::DrawSplitterSash(wxWindow
* win
,
315 wxOrientation orient
,
318 if ( !win
->m_wxwindow
->window
)
320 // window not realized yet
324 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
325 wxASSERT_MSG( gdk_window
,
326 wxT("cannot use wxRendererNative on wxDC of this type") );
328 wxCoord full_size
= GetGtkSplitterFullSize(wxGTKPrivate::GetSplitterWidget());
330 // are we drawing vertical or horizontal splitter?
331 const bool isVert
= orient
== wxVERTICAL
;
339 rect
.width
= full_size
;
340 rect
.height
= size
.y
;
346 rect
.height
= full_size
;
351 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
356 win
->m_wxwindow
->style
,
358 flags
& wxCONTROL_CURRENT
? GTK_STATE_PRELIGHT
: GTK_STATE_NORMAL
,
360 NULL
/* no clipping */,
363 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
364 dc
.LogicalToDeviceY(rect
.y
),
367 isVert
? GTK_ORIENTATION_VERTICAL
: GTK_ORIENTATION_HORIZONTAL
372 wxRendererGTK::DrawDropArrow(wxWindow
* win
,
377 GtkWidget
*button
= wxGTKPrivate::GetButtonWidget();
379 // If we give WX_PIZZA(win->m_wxwindow)->bin_window as
380 // a window for gtk_paint_xxx function, then it won't
381 // work for wxMemoryDC. So that is why we assume wxDC
382 // is wxWindowDC (wxClientDC, wxMemoryDC and wxPaintDC
383 // are derived from it) and use its m_window.
384 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
385 wxASSERT_MSG( gdk_window
,
386 wxT("cannot use wxRendererNative on wxDC of this type") );
388 // draw arrow so that there is even space horizontally
390 int arrowX
= rect
.width
/4 + 1;
391 int arrowWidth
= rect
.width
- (arrowX
*2);
393 // scale arrow's height accoording to the width
394 int arrowHeight
= rect
.width
/3;
395 int arrowY
= (rect
.height
-arrowHeight
)/2 +
396 ((rect
.height
-arrowHeight
) & 1);
400 if ( flags
& wxCONTROL_PRESSED
)
401 state
= GTK_STATE_ACTIVE
;
402 else if ( flags
& wxCONTROL_DISABLED
)
403 state
= GTK_STATE_INSENSITIVE
;
404 else if ( flags
& wxCONTROL_CURRENT
)
405 state
= GTK_STATE_PRELIGHT
;
407 state
= GTK_STATE_NORMAL
;
409 // draw arrow on button
415 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
429 wxRendererGTK::DrawComboBoxDropButton(wxWindow
*win
,
434 DrawPushButton(win
,dc
,rect
,flags
);
435 DrawDropArrow(win
,dc
,rect
);
439 wxRendererGTK::GetCheckBoxSize(wxWindow
*WXUNUSED(win
))
441 gint indicator_size
, indicator_spacing
;
442 gtk_widget_style_get(wxGTKPrivate::GetCheckButtonWidget(),
443 "indicator_size", &indicator_size
,
444 "indicator_spacing", &indicator_spacing
,
447 int size
= indicator_size
+ indicator_spacing
* 2;
448 return wxSize(size
, size
);
452 wxRendererGTK::DrawCheckBox(wxWindow
* win
,
457 GtkWidget
*button
= wxGTKPrivate::GetCheckButtonWidget();
459 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
460 wxASSERT_MSG( gdk_window
,
461 wxT("cannot use wxRendererNative on wxDC of this type") );
463 gint indicator_size
, indicator_spacing
;
464 gtk_widget_style_get(button
,
465 "indicator_size", &indicator_size
,
466 "indicator_spacing", &indicator_spacing
,
471 if ( flags
& wxCONTROL_PRESSED
)
472 state
= GTK_STATE_ACTIVE
;
473 else if ( flags
& wxCONTROL_DISABLED
)
474 state
= GTK_STATE_INSENSITIVE
;
475 else if ( flags
& wxCONTROL_CURRENT
)
476 state
= GTK_STATE_PRELIGHT
;
478 state
= GTK_STATE_NORMAL
;
480 GtkShadowType shadow_type
;
482 if ( flags
& wxCONTROL_UNDETERMINED
)
483 shadow_type
= GTK_SHADOW_ETCHED_IN
;
484 else if ( flags
& wxCONTROL_CHECKED
)
485 shadow_type
= GTK_SHADOW_IN
;
487 shadow_type
= GTK_SHADOW_OUT
;
498 dc
.LogicalToDeviceX(rect
.x
) + indicator_spacing
,
499 dc
.LogicalToDeviceY(rect
.y
) + indicator_spacing
,
500 indicator_size
, indicator_size
505 wxRendererGTK::DrawPushButton(wxWindow
* win
,
510 GtkWidget
*button
= wxGTKPrivate::GetButtonWidget();
512 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
513 wxASSERT_MSG( gdk_window
,
514 wxT("cannot use wxRendererNative on wxDC of this type") );
519 if ( flags
& wxCONTROL_PRESSED
)
520 state
= GTK_STATE_ACTIVE
;
521 else if ( flags
& wxCONTROL_DISABLED
)
522 state
= GTK_STATE_INSENSITIVE
;
523 else if ( flags
& wxCONTROL_CURRENT
)
524 state
= GTK_STATE_PRELIGHT
;
526 state
= GTK_STATE_NORMAL
;
533 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
537 dc
.LogicalToDeviceX(rect
.x
),
538 dc
.LogicalToDeviceY(rect
.y
),
545 wxRendererGTK::DrawItemSelectionRect(wxWindow
* win
,
550 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
551 wxASSERT_MSG( gdk_window
,
552 wxT("cannot use wxRendererNative on wxDC of this type") );
554 if (flags
& wxCONTROL_SELECTED
)
557 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
560 // the wxCONTROL_FOCUSED state is deduced
561 // directly from the m_wxwindow by GTK+
562 gtk_paint_flat_box(wxGTKPrivate::GetTreeWidget()->style
,
569 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
570 dc
.LogicalToDeviceY(rect
.y
),
575 if ((flags
& wxCONTROL_CURRENT
) && (flags
& wxCONTROL_FOCUSED
))
576 DrawFocusRect(win
, dc
, rect
, flags
);
579 void wxRendererGTK::DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
581 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
582 wxASSERT_MSG( gdk_window
,
583 wxT("cannot use wxRendererNative on wxDC of this type") );
586 if (flags
& wxCONTROL_SELECTED
)
587 state
= GTK_STATE_SELECTED
;
589 state
= GTK_STATE_NORMAL
;
591 gtk_paint_focus( win
->m_widget
->style
,
597 dc
.LogicalToDeviceX(rect
.x
),
598 dc
.LogicalToDeviceY(rect
.y
),
603 // Uses the theme to draw the border and fill for something like a wxTextCtrl
604 void wxRendererGTK::DrawTextCtrl(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
606 GtkWidget
*entry
= wxGTKPrivate::GetTextEntryWidget();
608 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
610 GtkStateType state
= GTK_STATE_NORMAL
;
611 if ( flags
& wxCONTROL_DISABLED
)
612 state
= GTK_STATE_INSENSITIVE
;
614 if (flags
& wxCONTROL_CURRENT
)
615 GTK_WIDGET_SET_FLAGS( entry
, GTK_HAS_FOCUS
);
617 GTK_WIDGET_UNSET_FLAGS( entry
, GTK_HAS_FOCUS
);
628 dc
.LogicalToDeviceX(rect
.x
),
629 dc
.LogicalToDeviceY(rect
.y
),
635 // Draw the equivallent of a wxComboBox
636 void wxRendererGTK::DrawComboBox(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
638 GtkWidget
*combo
= wxGTKPrivate::GetComboBoxWidget();
640 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
642 GtkStateType state
= GTK_STATE_NORMAL
;
643 if ( flags
& wxCONTROL_DISABLED
)
644 state
= GTK_STATE_INSENSITIVE
;
646 if (flags
& wxCONTROL_CURRENT
)
647 GTK_WIDGET_SET_FLAGS( combo
, GTK_HAS_FOCUS
);
649 GTK_WIDGET_UNSET_FLAGS( combo
, GTK_HAS_FOCUS
);
660 dc
.LogicalToDeviceX(rect
.x
),
661 dc
.LogicalToDeviceY(rect
.y
),
667 int extent
= rect
.height
/ 2;
668 r
.x
+= rect
.width
- extent
- extent
/2;
684 dc
.LogicalToDeviceX(r
.x
),
685 dc
.LogicalToDeviceY(r
.y
),
691 r
.x
+= rect
.width
- 2*extent
;
699 GTK_SHADOW_ETCHED_OUT
,
703 dc
.LogicalToDeviceX(r
.x
),
704 dc
.LogicalToDeviceY(r
.y
+1),
711 void wxRendererGTK::DrawChoice(wxWindow
* win
, wxDC
& dc
,
712 const wxRect
& rect
, int flags
)
714 DrawComboBox( win
, dc
, rect
, flags
);
718 // Draw a themed radio button
719 void wxRendererGTK::DrawRadioBitmap(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
721 GtkWidget
*button
= wxGTKPrivate::GetRadioButtonWidget();
723 GdkWindow
* gdk_window
= wxGetGdkWindowForDC(win
, dc
);
725 GtkShadowType shadow_type
= GTK_SHADOW_OUT
;
726 if ( flags
& wxCONTROL_CHECKED
)
727 shadow_type
= GTK_SHADOW_IN
;
728 else if ( flags
& wxCONTROL_UNDETERMINED
)
729 shadow_type
= GTK_SHADOW_ETCHED_IN
;
731 GtkStateType state
= GTK_STATE_NORMAL
;
732 if ( flags
& wxCONTROL_DISABLED
)
733 state
= GTK_STATE_INSENSITIVE
;
734 if ( flags
& wxCONTROL_PRESSED
)
735 state
= GTK_STATE_ACTIVE
;
737 Don't know when to set this
738 state_type = GTK_STATE_PRELIGHT;
750 dc
.LogicalToDeviceX(rect
.x
),
751 dc
.LogicalToDeviceY(rect
.y
),
752 rect
.width
, rect
.height