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
= dc
.GetGDKWindow();
223 wxASSERT_MSG( gdk_window
,
224 wxT("cannot use wxRendererNative on wxDC of this type") );
227 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
234 flags
& wxCONTROL_DISABLED
? GTK_STATE_INSENSITIVE
: GTK_STATE_NORMAL
,
239 dc
.LogicalToDeviceX(rect
.x
) - x_diff
, rect
.y
, rect
.width
, rect
.height
242 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
245 // draw a ">" or "v" button
247 wxRendererGTK::DrawTreeItemButton(wxWindow
* win
,
248 wxDC
& dc
, const wxRect
& rect
, int flags
)
250 GtkWidget
*tree
= GetTreeWidget();
252 GdkWindow
* gdk_window
= dc
.GetGDKWindow();
253 wxASSERT_MSG( gdk_window
,
254 wxT("cannot use wxRendererNative on wxDC of this type") );
257 if ( flags
& wxCONTROL_CURRENT
)
258 state
= GTK_STATE_PRELIGHT
;
260 state
= GTK_STATE_NORMAL
;
263 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
266 // VZ: I don't know how to get the size of the expander so as to centre it
267 // in the given rectangle, +2/3 below is just what looks good here...
276 dc
.LogicalToDeviceX(rect
.x
) + 6 - x_diff
,
277 dc
.LogicalToDeviceY(rect
.y
) + 3,
278 flags
& wxCONTROL_EXPANDED
? GTK_EXPANDER_EXPANDED
279 : GTK_EXPANDER_COLLAPSED
284 // ----------------------------------------------------------------------------
285 // splitter sash drawing
286 // ----------------------------------------------------------------------------
288 static int GetGtkSplitterFullSize()
290 static GtkWidget
*s_paned
= NULL
;
292 s_paned
= gtk_vpaned_new();
295 gtk_widget_style_get (s_paned
, "handle_size", &handle_size
, NULL
);
300 wxSplitterRenderParams
301 wxRendererGTK::GetSplitterParams(const wxWindow
*WXUNUSED(win
))
303 // we don't draw any border, hence 0 for the second field
304 return wxSplitterRenderParams
306 GetGtkSplitterFullSize(),
308 true // hot sensitive
313 wxRendererGTK::DrawSplitterBorder(wxWindow
* WXUNUSED(win
),
315 const wxRect
& WXUNUSED(rect
),
322 wxRendererGTK::DrawSplitterSash(wxWindow
*win
,
326 wxOrientation orient
,
329 if ( !win
->m_wxwindow
->window
)
331 // window not realized yet
335 GdkWindow
* gdk_window
= dc
.GetGDKWindow();
336 wxASSERT_MSG( gdk_window
,
337 wxT("cannot use wxRendererNative on wxDC of this type") );
339 wxCoord full_size
= GetGtkSplitterFullSize();
341 // are we drawing vertical or horizontal splitter?
342 const bool isVert
= orient
== wxVERTICAL
;
348 int h
= win
->GetClientSize().GetHeight();
352 rect
.width
= full_size
;
357 int w
= win
->GetClientSize().GetWidth();
361 rect
.height
= full_size
;
366 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
371 win
->m_wxwindow
->style
,
373 flags
& wxCONTROL_CURRENT
? GTK_STATE_PRELIGHT
: GTK_STATE_NORMAL
,
375 NULL
/* no clipping */,
378 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
379 dc
.LogicalToDeviceY(rect
.y
),
382 isVert
? GTK_ORIENTATION_VERTICAL
: GTK_ORIENTATION_HORIZONTAL
387 wxRendererGTK::DrawDropArrow(wxWindow
*WXUNUSED(win
),
392 GtkWidget
*button
= GetButtonWidget();
394 // If we give GTK_PIZZA(win->m_wxwindow)->bin_window as
395 // a window for gtk_paint_xxx function, then it won't
396 // work for wxMemoryDC. So that is why we assume wxDC
397 // is wxWindowDC (wxClientDC, wxMemoryDC and wxPaintDC
398 // are derived from it) and use its m_window.
399 GdkWindow
* gdk_window
= dc
.GetGDKWindow();
400 wxASSERT_MSG( gdk_window
,
401 wxT("cannot use wxRendererNative on wxDC of this type") );
403 // draw arrow so that there is even space horizontally
405 int arrowX
= rect
.width
/4 + 1;
406 int arrowWidth
= rect
.width
- (arrowX
*2);
408 // scale arrow's height accoording to the width
409 int arrowHeight
= rect
.width
/3;
410 int arrowY
= (rect
.height
-arrowHeight
)/2 +
411 ((rect
.height
-arrowHeight
) & 1);
415 if ( flags
& wxCONTROL_PRESSED
)
416 state
= GTK_STATE_ACTIVE
;
417 else if ( flags
& wxCONTROL_DISABLED
)
418 state
= GTK_STATE_INSENSITIVE
;
419 else if ( flags
& wxCONTROL_CURRENT
)
420 state
= GTK_STATE_PRELIGHT
;
422 state
= GTK_STATE_NORMAL
;
424 // draw arrow on button
430 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
444 wxRendererGTK::DrawComboBoxDropButton(wxWindow
*win
,
449 DrawPushButton(win
,dc
,rect
,flags
);
450 DrawDropArrow(win
,dc
,rect
);
454 wxRendererGTK::DrawCheckBox(wxWindow
*WXUNUSED(win
),
459 GtkWidget
*button
= GetCheckButtonWidget();
461 // for reason why we do this, see DrawDropArrow
462 GdkWindow
* gdk_window
= dc
.GetGDKWindow();
463 wxASSERT_MSG( gdk_window
,
464 wxT("cannot use wxRendererNative on wxDC of this type") );
468 if ( flags
& wxCONTROL_PRESSED
)
469 state
= GTK_STATE_ACTIVE
;
470 else if ( flags
& wxCONTROL_DISABLED
)
471 state
= GTK_STATE_INSENSITIVE
;
472 else if ( flags
& wxCONTROL_CURRENT
)
473 state
= GTK_STATE_PRELIGHT
;
475 state
= GTK_STATE_NORMAL
;
482 flags
& wxCONTROL_CHECKED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
486 dc
.LogicalToDeviceX(rect
.x
)+2,
487 dc
.LogicalToDeviceY(rect
.y
)+3,
493 wxRendererGTK::DrawPushButton(wxWindow
*WXUNUSED(win
),
498 GtkWidget
*button
= GetButtonWidget();
500 // for reason why we do this, see DrawDropArrow
501 GdkWindow
* gdk_window
= dc
.GetGDKWindow();
502 wxASSERT_MSG( gdk_window
,
503 wxT("cannot use wxRendererNative on wxDC of this type") );
508 if ( flags
& wxCONTROL_PRESSED
)
509 state
= GTK_STATE_ACTIVE
;
510 else if ( flags
& wxCONTROL_DISABLED
)
511 state
= GTK_STATE_INSENSITIVE
;
512 else if ( flags
& wxCONTROL_CURRENT
)
513 state
= GTK_STATE_PRELIGHT
;
515 state
= GTK_STATE_NORMAL
;
522 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
526 rect
.x
, rect
.y
, rect
.width
, rect
.height
531 wxRendererGTK::DrawItemSelectionRect(wxWindow
*win
,
536 GdkWindow
* gdk_window
= dc
.GetGDKWindow();
537 wxASSERT_MSG( gdk_window
,
538 wxT("cannot use wxRendererNative on wxDC of this type") );
541 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
545 if (flags
& wxCONTROL_SELECTED
)
547 // the wxCONTROL_FOCUSED state is deduced
548 // directly from the m_wxwindow by GTK+
549 state
= GTK_STATE_SELECTED
;
551 gtk_paint_flat_box( win
->m_widget
->style
,
558 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
559 dc
.LogicalToDeviceY(rect
.y
),
563 else // !wxCONTROL_SELECTED
565 state
= GTK_STATE_NORMAL
;
568 if (flags
& wxCONTROL_CURRENT
)
570 gtk_paint_focus( win
->m_widget
->style
,
576 dc
.LogicalToDeviceX(rect
.x
),
577 dc
.LogicalToDeviceY(rect
.y
),
583 void wxRendererGTK::DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
585 GdkWindow
* gdk_window
= dc
.GetGDKWindow();
586 wxASSERT_MSG( gdk_window
,
587 wxT("cannot use wxRendererNative on wxDC of this type") );
590 if (flags
& wxCONTROL_SELECTED
)
591 state
= GTK_STATE_SELECTED
;
593 state
= GTK_STATE_NORMAL
;
595 gtk_paint_focus( win
->m_widget
->style
,
601 dc
.LogicalToDeviceX(rect
.x
),
602 dc
.LogicalToDeviceY(rect
.y
),