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"
37 // ----------------------------------------------------------------------------
38 // wxRendererGTK: our wxRendererNative implementation
39 // ----------------------------------------------------------------------------
41 class WXDLLEXPORT wxRendererGTK
: public wxDelegateRendererNative
44 // draw the header control button (used by wxListCtrl)
45 virtual int DrawHeaderButton(wxWindow
*win
,
49 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
,
50 wxHeaderButtonParams
* params
= NULL
);
52 // draw the expanded/collapsed icon for a tree control item
53 virtual void DrawTreeItemButton(wxWindow
*win
,
58 virtual void DrawSplitterBorder(wxWindow
*win
,
62 virtual void DrawSplitterSash(wxWindow
*win
,
69 virtual void DrawComboBoxDropButton(wxWindow
*win
,
74 virtual void DrawDropArrow(wxWindow
*win
,
79 virtual void DrawCheckBox(wxWindow
*win
,
84 virtual void DrawPushButton(wxWindow
*win
,
89 virtual void DrawItemSelectionRect(wxWindow
*win
,
94 virtual void DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
= 0);
96 virtual wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
99 // FIXME: shouldn't we destroy these windows somewhere?
101 // used by DrawPushButton and DrawDropArrow
102 static GtkWidget
*GetButtonWidget();
104 // used by DrawTreeItemButton()
105 static GtkWidget
*GetTreeWidget();
107 // used by DrawCheckBox()
108 static GtkWidget
*GetCheckButtonWidget();
110 // Used by DrawHeaderButton
111 static GtkWidget
*GetHeaderButtonWidget();
114 // ============================================================================
116 // ============================================================================
119 wxRendererNative
& wxRendererNative::GetDefault()
121 static wxRendererGTK s_rendererGTK
;
123 return s_rendererGTK
;
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
131 wxRendererGTK::GetButtonWidget()
133 static GtkWidget
*s_button
= NULL
;
134 static GtkWidget
*s_window
= NULL
;
138 s_window
= gtk_window_new( GTK_WINDOW_POPUP
);
139 gtk_widget_realize( s_window
);
140 s_button
= gtk_button_new();
141 gtk_container_add( GTK_CONTAINER(s_window
), s_button
);
142 gtk_widget_realize( s_button
);
149 wxRendererGTK::GetCheckButtonWidget()
151 static GtkWidget
*s_button
= NULL
;
152 static GtkWidget
*s_window
= NULL
;
156 s_window
= gtk_window_new( GTK_WINDOW_POPUP
);
157 gtk_widget_realize( s_window
);
158 s_button
= gtk_check_button_new();
159 gtk_container_add( GTK_CONTAINER(s_window
), s_button
);
160 gtk_widget_realize( s_button
);
167 wxRendererGTK::GetTreeWidget()
169 static GtkWidget
*s_tree
= NULL
;
170 static GtkWidget
*s_window
= NULL
;
174 s_tree
= gtk_tree_view_new();
175 s_window
= gtk_window_new( GTK_WINDOW_POPUP
);
176 gtk_widget_realize( s_window
);
177 gtk_container_add( GTK_CONTAINER(s_window
), s_tree
);
178 gtk_widget_realize( s_tree
);
185 // This one just gets the button used by the column header. Although it's
186 // still a gtk_button the themes will typically differentiate and draw them
187 // differently if the button is in a treeview.
189 wxRendererGTK::GetHeaderButtonWidget()
191 static GtkWidget
*s_button
= NULL
;
195 // Get the dummy tree widget, give it a column, and then use the
196 // widget in the column header for the rendering code.
197 GtkWidget
* treewidget
= GetTreeWidget();
198 GtkTreeViewColumn
* column
= gtk_tree_view_column_new();
199 gtk_tree_view_append_column(GTK_TREE_VIEW(treewidget
), column
);
200 s_button
= column
->button
;
206 // ----------------------------------------------------------------------------
207 // list/tree controls drawing
208 // ----------------------------------------------------------------------------
211 wxRendererGTK::DrawHeaderButton(wxWindow
*win
,
215 wxHeaderSortIconType sortArrow
,
216 wxHeaderButtonParams
* params
)
219 GtkWidget
*button
= GetHeaderButtonWidget();
221 GdkWindow
* gdk_window
= NULL
;
223 wxImplDC
*impl
= dc
.GetImpl();
224 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
226 gdk_window
= gtk_impl
->GetGDKWindow();
228 gdk_window
= dc
.GetGDKWindow();
230 wxASSERT_MSG( gdk_window
,
231 wxT("cannot use wxRendererNative on wxDC of this type") );
234 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
237 GtkStateType state
= GTK_STATE_NORMAL
;
238 if (flags
& wxCONTROL_DISABLED
)
239 state
= GTK_STATE_INSENSITIVE
;
242 if (flags
& wxCONTROL_CURRENT
)
243 state
= GTK_STATE_PRELIGHT
;
255 dc
.LogicalToDeviceX(rect
.x
) - x_diff
, rect
.y
, rect
.width
, rect
.height
258 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
261 // draw a ">" or "v" button
263 wxRendererGTK::DrawTreeItemButton(wxWindow
* win
,
264 wxDC
& dc
, const wxRect
& rect
, int flags
)
266 GtkWidget
*tree
= GetTreeWidget();
268 GdkWindow
* gdk_window
= NULL
;
270 wxImplDC
*impl
= dc
.GetImpl();
271 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
273 gdk_window
= gtk_impl
->GetGDKWindow();
275 gdk_window
= dc
.GetGDKWindow();
277 wxASSERT_MSG( gdk_window
,
278 wxT("cannot use wxRendererNative on wxDC of this type") );
281 if ( flags
& wxCONTROL_CURRENT
)
282 state
= GTK_STATE_PRELIGHT
;
284 state
= GTK_STATE_NORMAL
;
287 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
290 // VZ: I don't know how to get the size of the expander so as to centre it
291 // in the given rectangle, +2/3 below is just what looks good here...
300 dc
.LogicalToDeviceX(rect
.x
) + 6 - x_diff
,
301 dc
.LogicalToDeviceY(rect
.y
) + 3,
302 flags
& wxCONTROL_EXPANDED
? GTK_EXPANDER_EXPANDED
303 : GTK_EXPANDER_COLLAPSED
308 // ----------------------------------------------------------------------------
309 // splitter sash drawing
310 // ----------------------------------------------------------------------------
312 static int GetGtkSplitterFullSize()
314 static GtkWidget
*s_paned
= NULL
;
316 s_paned
= gtk_vpaned_new();
319 gtk_widget_style_get (s_paned
, "handle_size", &handle_size
, NULL
);
324 wxSplitterRenderParams
325 wxRendererGTK::GetSplitterParams(const wxWindow
*WXUNUSED(win
))
327 // we don't draw any border, hence 0 for the second field
328 return wxSplitterRenderParams
330 GetGtkSplitterFullSize(),
332 true // hot sensitive
337 wxRendererGTK::DrawSplitterBorder(wxWindow
* WXUNUSED(win
),
339 const wxRect
& WXUNUSED(rect
),
346 wxRendererGTK::DrawSplitterSash(wxWindow
*win
,
350 wxOrientation orient
,
353 if ( !win
->m_wxwindow
->window
)
355 // window not realized yet
359 GdkWindow
* gdk_window
= NULL
;
361 wxImplDC
*impl
= dc
.GetImpl();
362 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
364 gdk_window
= gtk_impl
->GetGDKWindow();
366 gdk_window
= dc
.GetGDKWindow();
368 wxASSERT_MSG( gdk_window
,
369 wxT("cannot use wxRendererNative on wxDC of this type") );
371 wxCoord full_size
= GetGtkSplitterFullSize();
373 // are we drawing vertical or horizontal splitter?
374 const bool isVert
= orient
== wxVERTICAL
;
382 rect
.width
= full_size
;
383 rect
.height
= size
.y
;
389 rect
.height
= full_size
;
394 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
399 win
->m_wxwindow
->style
,
401 flags
& wxCONTROL_CURRENT
? GTK_STATE_PRELIGHT
: GTK_STATE_NORMAL
,
403 NULL
/* no clipping */,
406 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
407 dc
.LogicalToDeviceY(rect
.y
),
410 isVert
? GTK_ORIENTATION_VERTICAL
: GTK_ORIENTATION_HORIZONTAL
415 wxRendererGTK::DrawDropArrow(wxWindow
*WXUNUSED(win
),
420 GtkWidget
*button
= GetButtonWidget();
422 // If we give GTK_PIZZA(win->m_wxwindow)->bin_window as
423 // a window for gtk_paint_xxx function, then it won't
424 // work for wxMemoryDC. So that is why we assume wxDC
425 // is wxWindowDC (wxClientDC, wxMemoryDC and wxPaintDC
426 // are derived from it) and use its m_window.
427 GdkWindow
* gdk_window
= NULL
;
429 wxImplDC
*impl
= dc
.GetImpl();
430 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
432 gdk_window
= gtk_impl
->GetGDKWindow();
434 gdk_window
= dc
.GetGDKWindow();
436 wxASSERT_MSG( gdk_window
,
437 wxT("cannot use wxRendererNative on wxDC of this type") );
439 // draw arrow so that there is even space horizontally
441 int arrowX
= rect
.width
/4 + 1;
442 int arrowWidth
= rect
.width
- (arrowX
*2);
444 // scale arrow's height accoording to the width
445 int arrowHeight
= rect
.width
/3;
446 int arrowY
= (rect
.height
-arrowHeight
)/2 +
447 ((rect
.height
-arrowHeight
) & 1);
451 if ( flags
& wxCONTROL_PRESSED
)
452 state
= GTK_STATE_ACTIVE
;
453 else if ( flags
& wxCONTROL_DISABLED
)
454 state
= GTK_STATE_INSENSITIVE
;
455 else if ( flags
& wxCONTROL_CURRENT
)
456 state
= GTK_STATE_PRELIGHT
;
458 state
= GTK_STATE_NORMAL
;
460 // draw arrow on button
466 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
480 wxRendererGTK::DrawComboBoxDropButton(wxWindow
*win
,
485 DrawPushButton(win
,dc
,rect
,flags
);
486 DrawDropArrow(win
,dc
,rect
);
490 wxRendererGTK::DrawCheckBox(wxWindow
*WXUNUSED(win
),
495 GtkWidget
*button
= GetCheckButtonWidget();
497 GdkWindow
* gdk_window
= NULL
;
499 wxImplDC
*impl
= dc
.GetImpl();
500 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
502 gdk_window
= gtk_impl
->GetGDKWindow();
504 gdk_window
= dc
.GetGDKWindow();
506 wxASSERT_MSG( gdk_window
,
507 wxT("cannot use wxRendererNative on wxDC of this type") );
511 if ( flags
& wxCONTROL_PRESSED
)
512 state
= GTK_STATE_ACTIVE
;
513 else if ( flags
& wxCONTROL_DISABLED
)
514 state
= GTK_STATE_INSENSITIVE
;
515 else if ( flags
& wxCONTROL_CURRENT
)
516 state
= GTK_STATE_PRELIGHT
;
518 state
= GTK_STATE_NORMAL
;
525 flags
& wxCONTROL_CHECKED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
529 dc
.LogicalToDeviceX(rect
.x
)+2,
530 dc
.LogicalToDeviceY(rect
.y
)+3,
536 wxRendererGTK::DrawPushButton(wxWindow
*WXUNUSED(win
),
541 GtkWidget
*button
= GetButtonWidget();
543 GdkWindow
* gdk_window
= NULL
;
545 wxImplDC
*impl
= dc
.GetImpl();
546 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
548 gdk_window
= gtk_impl
->GetGDKWindow();
550 gdk_window
= dc
.GetGDKWindow();
552 wxASSERT_MSG( gdk_window
,
553 wxT("cannot use wxRendererNative on wxDC of this type") );
558 if ( flags
& wxCONTROL_PRESSED
)
559 state
= GTK_STATE_ACTIVE
;
560 else if ( flags
& wxCONTROL_DISABLED
)
561 state
= GTK_STATE_INSENSITIVE
;
562 else if ( flags
& wxCONTROL_CURRENT
)
563 state
= GTK_STATE_PRELIGHT
;
565 state
= GTK_STATE_NORMAL
;
572 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
576 rect
.x
, rect
.y
, rect
.width
, rect
.height
581 wxRendererGTK::DrawItemSelectionRect(wxWindow
*win
,
586 GdkWindow
* gdk_window
= NULL
;
588 wxImplDC
*impl
= dc
.GetImpl();
589 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
591 gdk_window
= gtk_impl
->GetGDKWindow();
593 gdk_window
= dc
.GetGDKWindow();
595 wxASSERT_MSG( gdk_window
,
596 wxT("cannot use wxRendererNative on wxDC of this type") );
599 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
603 if (flags
& wxCONTROL_SELECTED
)
605 // the wxCONTROL_FOCUSED state is deduced
606 // directly from the m_wxwindow by GTK+
607 state
= GTK_STATE_SELECTED
;
609 gtk_paint_flat_box( win
->m_widget
->style
,
616 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
617 dc
.LogicalToDeviceY(rect
.y
),
621 else // !wxCONTROL_SELECTED
623 state
= GTK_STATE_NORMAL
;
626 if ((flags
& wxCONTROL_CURRENT
) && (flags
& wxCONTROL_FOCUSED
))
628 gtk_paint_focus( win
->m_widget
->style
,
634 dc
.LogicalToDeviceX(rect
.x
),
635 dc
.LogicalToDeviceY(rect
.y
),
641 void wxRendererGTK::DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
643 GdkWindow
* gdk_window
= NULL
;
645 wxImplDC
*impl
= dc
.GetImpl();
646 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
648 gdk_window
= gtk_impl
->GetGDKWindow();
650 gdk_window
= dc
.GetGDKWindow();
652 wxASSERT_MSG( gdk_window
,
653 wxT("cannot use wxRendererNative on wxDC of this type") );
656 if (flags
& wxCONTROL_SELECTED
)
657 state
= GTK_STATE_SELECTED
;
659 state
= GTK_STATE_NORMAL
;
661 gtk_paint_focus( win
->m_widget
->style
,
667 dc
.LogicalToDeviceX(rect
.x
),
668 dc
.LogicalToDeviceY(rect
.y
),