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 GtkWidget
*GetEntryWidget()
187 static GtkWidget
*s_entry
= NULL
;
188 static GtkWidget
*s_window
= NULL
;
192 s_window
= gtk_window_new( GTK_WINDOW_POPUP
);
193 gtk_widget_realize( s_window
);
194 s_entry
= gtk_entry_new();
195 gtk_container_add( GTK_CONTAINER(s_window
), s_entry
);
196 gtk_widget_realize( s_entry
);
202 // This one just gets the button used by the column header. Although it's
203 // still a gtk_button the themes will typically differentiate and draw them
204 // differently if the button is in a treeview.
206 wxRendererGTK::GetHeaderButtonWidget()
208 static GtkWidget
*s_button
= NULL
;
212 // Get the dummy tree widget, give it a column, and then use the
213 // widget in the column header for the rendering code.
214 GtkWidget
* treewidget
= GetTreeWidget();
215 GtkTreeViewColumn
* column
= gtk_tree_view_column_new();
216 gtk_tree_view_append_column(GTK_TREE_VIEW(treewidget
), column
);
217 s_button
= column
->button
;
223 // ----------------------------------------------------------------------------
224 // list/tree controls drawing
225 // ----------------------------------------------------------------------------
228 wxRendererGTK::DrawHeaderButton(wxWindow
*win
,
232 wxHeaderSortIconType sortArrow
,
233 wxHeaderButtonParams
* params
)
236 GtkWidget
*button
= GetHeaderButtonWidget();
238 GdkWindow
* gdk_window
= NULL
;
240 wxImplDC
*impl
= dc
.GetImpl();
241 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
243 gdk_window
= gtk_impl
->GetGDKWindow();
245 gdk_window
= dc
.GetGDKWindow();
247 wxASSERT_MSG( gdk_window
,
248 wxT("cannot use wxRendererNative on wxDC of this type") );
251 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
254 GtkStateType state
= GTK_STATE_NORMAL
;
255 if (flags
& wxCONTROL_DISABLED
)
256 state
= GTK_STATE_INSENSITIVE
;
259 if (flags
& wxCONTROL_CURRENT
)
260 state
= GTK_STATE_PRELIGHT
;
272 dc
.LogicalToDeviceX(rect
.x
) - x_diff
, rect
.y
, rect
.width
, rect
.height
275 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
278 // draw a ">" or "v" button
280 wxRendererGTK::DrawTreeItemButton(wxWindow
* win
,
281 wxDC
& dc
, const wxRect
& rect
, int flags
)
283 GtkWidget
*tree
= GetTreeWidget();
285 GdkWindow
* gdk_window
= NULL
;
287 wxImplDC
*impl
= dc
.GetImpl();
288 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
290 gdk_window
= gtk_impl
->GetGDKWindow();
292 gdk_window
= dc
.GetGDKWindow();
294 wxASSERT_MSG( gdk_window
,
295 wxT("cannot use wxRendererNative on wxDC of this type") );
298 if ( flags
& wxCONTROL_CURRENT
)
299 state
= GTK_STATE_PRELIGHT
;
301 state
= GTK_STATE_NORMAL
;
304 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
307 // VZ: I don't know how to get the size of the expander so as to centre it
308 // in the given rectangle, +2/3 below is just what looks good here...
317 dc
.LogicalToDeviceX(rect
.x
) + 6 - x_diff
,
318 dc
.LogicalToDeviceY(rect
.y
) + 3,
319 flags
& wxCONTROL_EXPANDED
? GTK_EXPANDER_EXPANDED
320 : GTK_EXPANDER_COLLAPSED
325 // ----------------------------------------------------------------------------
326 // splitter sash drawing
327 // ----------------------------------------------------------------------------
329 static int GetGtkSplitterFullSize()
331 static GtkWidget
*s_paned
= NULL
;
333 s_paned
= gtk_vpaned_new();
336 gtk_widget_style_get (s_paned
, "handle_size", &handle_size
, NULL
);
341 wxSplitterRenderParams
342 wxRendererGTK::GetSplitterParams(const wxWindow
*WXUNUSED(win
))
344 // we don't draw any border, hence 0 for the second field
345 return wxSplitterRenderParams
347 GetGtkSplitterFullSize(),
349 true // hot sensitive
354 wxRendererGTK::DrawSplitterBorder(wxWindow
* WXUNUSED(win
),
356 const wxRect
& WXUNUSED(rect
),
363 wxRendererGTK::DrawSplitterSash(wxWindow
*win
,
367 wxOrientation orient
,
370 if ( !win
->m_wxwindow
->window
)
372 // window not realized yet
376 GdkWindow
* gdk_window
= NULL
;
378 wxImplDC
*impl
= dc
.GetImpl();
379 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
381 gdk_window
= gtk_impl
->GetGDKWindow();
383 gdk_window
= dc
.GetGDKWindow();
385 wxASSERT_MSG( gdk_window
,
386 wxT("cannot use wxRendererNative on wxDC of this type") );
388 wxCoord full_size
= GetGtkSplitterFullSize();
390 // are we drawing vertical or horizontal splitter?
391 const bool isVert
= orient
== wxVERTICAL
;
399 rect
.width
= full_size
;
400 rect
.height
= size
.y
;
406 rect
.height
= full_size
;
411 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
416 win
->m_wxwindow
->style
,
418 flags
& wxCONTROL_CURRENT
? GTK_STATE_PRELIGHT
: GTK_STATE_NORMAL
,
420 NULL
/* no clipping */,
423 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
424 dc
.LogicalToDeviceY(rect
.y
),
427 isVert
? GTK_ORIENTATION_VERTICAL
: GTK_ORIENTATION_HORIZONTAL
432 wxRendererGTK::DrawDropArrow(wxWindow
*WXUNUSED(win
),
437 GtkWidget
*button
= GetButtonWidget();
439 // If we give GTK_PIZZA(win->m_wxwindow)->bin_window as
440 // a window for gtk_paint_xxx function, then it won't
441 // work for wxMemoryDC. So that is why we assume wxDC
442 // is wxWindowDC (wxClientDC, wxMemoryDC and wxPaintDC
443 // are derived from it) and use its m_window.
444 GdkWindow
* gdk_window
= NULL
;
446 wxImplDC
*impl
= dc
.GetImpl();
447 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
449 gdk_window
= gtk_impl
->GetGDKWindow();
451 gdk_window
= dc
.GetGDKWindow();
453 wxASSERT_MSG( gdk_window
,
454 wxT("cannot use wxRendererNative on wxDC of this type") );
456 // draw arrow so that there is even space horizontally
458 int arrowX
= rect
.width
/4 + 1;
459 int arrowWidth
= rect
.width
- (arrowX
*2);
461 // scale arrow's height accoording to the width
462 int arrowHeight
= rect
.width
/3;
463 int arrowY
= (rect
.height
-arrowHeight
)/2 +
464 ((rect
.height
-arrowHeight
) & 1);
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
;
477 // draw arrow on button
483 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
497 wxRendererGTK::DrawComboBoxDropButton(wxWindow
*win
,
502 DrawPushButton(win
,dc
,rect
,flags
);
503 DrawDropArrow(win
,dc
,rect
);
507 wxRendererGTK::DrawCheckBox(wxWindow
*WXUNUSED(win
),
512 GtkWidget
*button
= GetCheckButtonWidget();
514 GdkWindow
* gdk_window
= NULL
;
516 wxImplDC
*impl
= dc
.GetImpl();
517 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
519 gdk_window
= gtk_impl
->GetGDKWindow();
521 gdk_window
= dc
.GetGDKWindow();
523 wxASSERT_MSG( gdk_window
,
524 wxT("cannot use wxRendererNative on wxDC of this type") );
528 if ( flags
& wxCONTROL_PRESSED
)
529 state
= GTK_STATE_ACTIVE
;
530 else if ( flags
& wxCONTROL_DISABLED
)
531 state
= GTK_STATE_INSENSITIVE
;
532 else if ( flags
& wxCONTROL_CURRENT
)
533 state
= GTK_STATE_PRELIGHT
;
535 state
= GTK_STATE_NORMAL
;
542 flags
& wxCONTROL_CHECKED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
546 dc
.LogicalToDeviceX(rect
.x
)+2,
547 dc
.LogicalToDeviceY(rect
.y
)+3,
553 wxRendererGTK::DrawPushButton(wxWindow
*WXUNUSED(win
),
558 GtkWidget
*button
= GetButtonWidget();
560 GdkWindow
* gdk_window
= NULL
;
562 wxImplDC
*impl
= dc
.GetImpl();
563 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
565 gdk_window
= gtk_impl
->GetGDKWindow();
567 gdk_window
= dc
.GetGDKWindow();
569 wxASSERT_MSG( gdk_window
,
570 wxT("cannot use wxRendererNative on wxDC of this type") );
575 if ( flags
& wxCONTROL_PRESSED
)
576 state
= GTK_STATE_ACTIVE
;
577 else if ( flags
& wxCONTROL_DISABLED
)
578 state
= GTK_STATE_INSENSITIVE
;
579 else if ( flags
& wxCONTROL_CURRENT
)
580 state
= GTK_STATE_PRELIGHT
;
582 state
= GTK_STATE_NORMAL
;
589 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
593 rect
.x
, rect
.y
, rect
.width
, rect
.height
598 wxRendererGTK::DrawItemSelectionRect(wxWindow
*win
,
603 GdkWindow
* gdk_window
= NULL
;
605 wxImplDC
*impl
= dc
.GetImpl();
606 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
608 gdk_window
= gtk_impl
->GetGDKWindow();
610 gdk_window
= dc
.GetGDKWindow();
612 wxASSERT_MSG( gdk_window
,
613 wxT("cannot use wxRendererNative on wxDC of this type") );
616 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
620 if (flags
& wxCONTROL_SELECTED
)
622 // the wxCONTROL_FOCUSED state is deduced
623 // directly from the m_wxwindow by GTK+
624 state
= GTK_STATE_SELECTED
;
626 gtk_paint_flat_box( win
->m_widget
->style
,
633 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
634 dc
.LogicalToDeviceY(rect
.y
),
638 else // !wxCONTROL_SELECTED
640 state
= GTK_STATE_NORMAL
;
643 if ((flags
& wxCONTROL_CURRENT
) && (flags
& wxCONTROL_FOCUSED
))
645 gtk_paint_focus( win
->m_widget
->style
,
651 dc
.LogicalToDeviceX(rect
.x
),
652 dc
.LogicalToDeviceY(rect
.y
),
658 void wxRendererGTK::DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
660 GdkWindow
* gdk_window
= NULL
;
662 wxImplDC
*impl
= dc
.GetImpl();
663 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
665 gdk_window
= gtk_impl
->GetGDKWindow();
667 gdk_window
= dc
.GetGDKWindow();
669 wxASSERT_MSG( gdk_window
,
670 wxT("cannot use wxRendererNative on wxDC of this type") );
673 if (flags
& wxCONTROL_SELECTED
)
674 state
= GTK_STATE_SELECTED
;
676 state
= GTK_STATE_NORMAL
;
678 gtk_paint_focus( win
->m_widget
->style
,
684 dc
.LogicalToDeviceX(rect
.x
),
685 dc
.LogicalToDeviceY(rect
.y
),