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 wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
98 // FIXME: shouldn't we destroy these windows somewhere?
100 // used by DrawPushButton and DrawDropArrow
101 static GtkWidget
*GetButtonWidget();
103 // used by DrawTreeItemButton()
104 static GtkWidget
*GetTreeWidget();
106 // used by DrawCheckBox()
107 static GtkWidget
*GetCheckButtonWidget();
109 // Used by DrawHeaderButton
110 static GtkWidget
*GetHeaderButtonWidget();
113 // ============================================================================
115 // ============================================================================
118 wxRendererNative
& wxRendererNative::GetDefault()
120 static wxRendererGTK s_rendererGTK
;
122 return s_rendererGTK
;
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
130 wxRendererGTK::GetButtonWidget()
132 static GtkWidget
*s_button
= NULL
;
133 static GtkWidget
*s_window
= NULL
;
137 s_window
= gtk_window_new( GTK_WINDOW_POPUP
);
138 gtk_widget_realize( s_window
);
139 s_button
= gtk_button_new();
140 gtk_container_add( GTK_CONTAINER(s_window
), s_button
);
141 gtk_widget_realize( s_button
);
148 wxRendererGTK::GetCheckButtonWidget()
150 static GtkWidget
*s_button
= NULL
;
151 static GtkWidget
*s_window
= NULL
;
155 s_window
= gtk_window_new( GTK_WINDOW_POPUP
);
156 gtk_widget_realize( s_window
);
157 s_button
= gtk_check_button_new();
158 gtk_container_add( GTK_CONTAINER(s_window
), s_button
);
159 gtk_widget_realize( s_button
);
166 wxRendererGTK::GetTreeWidget()
168 static GtkWidget
*s_tree
= NULL
;
169 static GtkWidget
*s_window
= NULL
;
173 s_tree
= gtk_tree_view_new();
174 s_window
= gtk_window_new( GTK_WINDOW_POPUP
);
175 gtk_widget_realize( s_window
);
176 gtk_container_add( GTK_CONTAINER(s_window
), s_tree
);
177 gtk_widget_realize( s_tree
);
184 // This one just gets the button used by the column header. Although it's
185 // still a gtk_button the themes will typically differentiate and draw them
186 // differently if the button is in a treeview.
188 wxRendererGTK::GetHeaderButtonWidget()
190 static GtkWidget
*s_button
= NULL
;
194 // Get the dummy tree widget, give it a column, and then use the
195 // widget in the column header for the rendering code.
196 GtkWidget
* treewidget
= GetTreeWidget();
197 GtkTreeViewColumn
* column
= gtk_tree_view_column_new();
198 gtk_tree_view_append_column(GTK_TREE_VIEW(treewidget
), column
);
199 s_button
= column
->button
;
205 // ----------------------------------------------------------------------------
206 // list/tree controls drawing
207 // ----------------------------------------------------------------------------
210 wxRendererGTK::DrawHeaderButton(wxWindow
*win
,
214 wxHeaderSortIconType sortArrow
,
215 wxHeaderButtonParams
* params
)
218 GtkWidget
*button
= GetHeaderButtonWidget();
220 GdkWindow
* gdk_window
= dc
.GetGDKWindow();
221 wxASSERT_MSG( gdk_window
,
222 wxT("cannot use wxRendererNative on wxDC of this type") );
225 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
232 flags
& wxCONTROL_DISABLED
? GTK_STATE_INSENSITIVE
: GTK_STATE_NORMAL
,
237 dc
.LogicalToDeviceX(rect
.x
) - x_diff
, rect
.y
, rect
.width
, rect
.height
240 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
243 // draw a ">" or "v" button
245 wxRendererGTK::DrawTreeItemButton(wxWindow
* win
,
246 wxDC
& dc
, const wxRect
& rect
, int flags
)
248 GtkWidget
*tree
= GetTreeWidget();
250 GdkWindow
* gdk_window
= dc
.GetGDKWindow();
251 wxASSERT_MSG( gdk_window
,
252 wxT("cannot use wxRendererNative on wxDC of this type") );
255 if ( flags
& wxCONTROL_CURRENT
)
256 state
= GTK_STATE_PRELIGHT
;
258 state
= GTK_STATE_NORMAL
;
261 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
264 // VZ: I don't know how to get the size of the expander so as to centre it
265 // in the given rectangle, +2/3 below is just what looks good here...
274 dc
.LogicalToDeviceX(rect
.x
) + 6 - x_diff
,
275 dc
.LogicalToDeviceY(rect
.y
) + 3,
276 flags
& wxCONTROL_EXPANDED
? GTK_EXPANDER_EXPANDED
277 : GTK_EXPANDER_COLLAPSED
282 // ----------------------------------------------------------------------------
283 // splitter sash drawing
284 // ----------------------------------------------------------------------------
286 static int GetGtkSplitterFullSize()
288 static GtkWidget
*s_paned
= NULL
;
290 s_paned
= gtk_vpaned_new();
293 gtk_widget_style_get (s_paned
, "handle_size", &handle_size
, NULL
);
298 wxSplitterRenderParams
299 wxRendererGTK::GetSplitterParams(const wxWindow
*WXUNUSED(win
))
301 // we don't draw any border, hence 0 for the second field
302 return wxSplitterRenderParams
304 GetGtkSplitterFullSize(),
306 true // hot sensitive
311 wxRendererGTK::DrawSplitterBorder(wxWindow
* WXUNUSED(win
),
313 const wxRect
& WXUNUSED(rect
),
320 wxRendererGTK::DrawSplitterSash(wxWindow
*win
,
324 wxOrientation orient
,
327 if ( !win
->m_wxwindow
->window
)
329 // window not realized yet
333 GdkWindow
* gdk_window
= dc
.GetGDKWindow();
334 wxASSERT_MSG( gdk_window
,
335 wxT("cannot use wxRendererNative on wxDC of this type") );
337 wxCoord full_size
= GetGtkSplitterFullSize();
339 // are we drawing vertical or horizontal splitter?
340 const bool isVert
= orient
== wxVERTICAL
;
346 int h
= win
->GetClientSize().GetHeight();
350 rect
.width
= full_size
;
355 int w
= win
->GetClientSize().GetWidth();
359 rect
.height
= full_size
;
364 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
369 win
->m_wxwindow
->style
,
371 flags
& wxCONTROL_CURRENT
? GTK_STATE_PRELIGHT
: GTK_STATE_NORMAL
,
373 NULL
/* no clipping */,
376 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
377 dc
.LogicalToDeviceY(rect
.y
),
380 isVert
? GTK_ORIENTATION_VERTICAL
: GTK_ORIENTATION_HORIZONTAL
385 wxRendererGTK::DrawDropArrow(wxWindow
*WXUNUSED(win
),
390 GtkWidget
*button
= GetButtonWidget();
392 // If we give GTK_PIZZA(win->m_wxwindow)->bin_window as
393 // a window for gtk_paint_xxx function, then it won't
394 // work for wxMemoryDC. So that is why we assume wxDC
395 // is wxWindowDC (wxClientDC, wxMemoryDC and wxPaintDC
396 // are derived from it) and use its m_window.
397 GdkWindow
* gdk_window
= dc
.GetGDKWindow();
398 wxASSERT_MSG( gdk_window
,
399 wxT("cannot use wxRendererNative on wxDC of this type") );
401 // draw arrow so that there is even space horizontally
403 int arrowX
= rect
.width
/4 + 1;
404 int arrowWidth
= rect
.width
- (arrowX
*2);
406 // scale arrow's height accoording to the width
407 int arrowHeight
= rect
.width
/3;
408 int arrowY
= (rect
.height
-arrowHeight
)/2 +
409 ((rect
.height
-arrowHeight
) & 1);
413 if ( flags
& wxCONTROL_PRESSED
)
414 state
= GTK_STATE_ACTIVE
;
415 else if ( flags
& wxCONTROL_DISABLED
)
416 state
= GTK_STATE_INSENSITIVE
;
417 else if ( flags
& wxCONTROL_CURRENT
)
418 state
= GTK_STATE_PRELIGHT
;
420 state
= GTK_STATE_NORMAL
;
422 // draw arrow on button
428 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
442 wxRendererGTK::DrawComboBoxDropButton(wxWindow
*win
,
447 DrawPushButton(win
,dc
,rect
,flags
);
448 DrawDropArrow(win
,dc
,rect
);
452 wxRendererGTK::DrawCheckBox(wxWindow
*WXUNUSED(win
),
457 GtkWidget
*button
= GetCheckButtonWidget();
459 // for reason why we do this, see DrawDropArrow
460 GdkWindow
* gdk_window
= dc
.GetGDKWindow();
461 wxASSERT_MSG( gdk_window
,
462 wxT("cannot use wxRendererNative on wxDC of this type") );
466 if ( flags
& wxCONTROL_PRESSED
)
467 state
= GTK_STATE_ACTIVE
;
468 else if ( flags
& wxCONTROL_DISABLED
)
469 state
= GTK_STATE_INSENSITIVE
;
470 else if ( flags
& wxCONTROL_CURRENT
)
471 state
= GTK_STATE_PRELIGHT
;
473 state
= GTK_STATE_NORMAL
;
480 flags
& wxCONTROL_CHECKED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
484 dc
.LogicalToDeviceX(rect
.x
)+2,
485 dc
.LogicalToDeviceY(rect
.y
)+3,
491 wxRendererGTK::DrawPushButton(wxWindow
*WXUNUSED(win
),
496 GtkWidget
*button
= GetButtonWidget();
498 // for reason why we do this, see DrawDropArrow
499 GdkWindow
* gdk_window
= dc
.GetGDKWindow();
500 wxASSERT_MSG( gdk_window
,
501 wxT("cannot use wxRendererNative on wxDC of this type") );
506 if ( flags
& wxCONTROL_PRESSED
)
507 state
= GTK_STATE_ACTIVE
;
508 else if ( flags
& wxCONTROL_DISABLED
)
509 state
= GTK_STATE_INSENSITIVE
;
510 else if ( flags
& wxCONTROL_CURRENT
)
511 state
= GTK_STATE_PRELIGHT
;
513 state
= GTK_STATE_NORMAL
;
520 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
524 rect
.x
, rect
.y
, rect
.width
, rect
.height
529 wxRendererGTK::DrawItemSelectionRect(wxWindow
*win
,
534 GdkWindow
* gdk_window
= dc
.GetGDKWindow();
535 wxASSERT_MSG( gdk_window
,
536 wxT("cannot use wxRendererNative on wxDC of this type") );
539 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
543 if (flags
& wxCONTROL_SELECTED
)
545 // the wxCONTROL_FOCUSED state is deduced
546 // directly from the m_wxwindow by GTK+
547 state
= GTK_STATE_SELECTED
;
549 gtk_paint_flat_box( win
->m_widget
->style
,
556 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
557 dc
.LogicalToDeviceY(rect
.y
),
561 else // !wxCONTROL_SELECTED
563 state
= GTK_STATE_NORMAL
;
566 if (flags
& wxCONTROL_CURRENT
)
568 gtk_paint_focus( win
->m_widget
->style
,
574 dc
.LogicalToDeviceX(rect
.x
),
575 dc
.LogicalToDeviceY(rect
.y
),