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
);
203 GtkWidget
*GetScrolledWidget()
205 static GtkWidget
*s_entry
= NULL
;
206 static GtkWidget
*s_window
= NULL
;
210 s_window
= gtk_window_new( GTK_WINDOW_POPUP
);
211 gtk_widget_realize( s_window
);
212 s_entry
= gtk_scrolled_window_new( NULL
, NULL
);
213 gtk_container_add( GTK_CONTAINER(s_window
), s_entry
);
214 gtk_widget_realize( s_entry
);
220 // This one just gets the button used by the column header. Although it's
221 // still a gtk_button the themes will typically differentiate and draw them
222 // differently if the button is in a treeview.
224 wxRendererGTK::GetHeaderButtonWidget()
226 static GtkWidget
*s_button
= NULL
;
230 // Get the dummy tree widget, give it a column, and then use the
231 // widget in the column header for the rendering code.
232 GtkWidget
* treewidget
= GetTreeWidget();
233 GtkTreeViewColumn
* column
= gtk_tree_view_column_new();
234 gtk_tree_view_append_column(GTK_TREE_VIEW(treewidget
), column
);
235 s_button
= column
->button
;
241 // ----------------------------------------------------------------------------
242 // list/tree controls drawing
243 // ----------------------------------------------------------------------------
246 wxRendererGTK::DrawHeaderButton(wxWindow
*win
,
250 wxHeaderSortIconType sortArrow
,
251 wxHeaderButtonParams
* params
)
254 GtkWidget
*button
= GetHeaderButtonWidget();
256 GdkWindow
* gdk_window
= NULL
;
258 wxImplDC
*impl
= dc
.GetImpl();
259 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
261 gdk_window
= gtk_impl
->GetGDKWindow();
263 gdk_window
= dc
.GetGDKWindow();
265 wxASSERT_MSG( gdk_window
,
266 wxT("cannot use wxRendererNative on wxDC of this type") );
269 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
272 GtkStateType state
= GTK_STATE_NORMAL
;
273 if (flags
& wxCONTROL_DISABLED
)
274 state
= GTK_STATE_INSENSITIVE
;
277 if (flags
& wxCONTROL_CURRENT
)
278 state
= GTK_STATE_PRELIGHT
;
290 dc
.LogicalToDeviceX(rect
.x
) - x_diff
, rect
.y
, rect
.width
, rect
.height
293 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
296 // draw a ">" or "v" button
298 wxRendererGTK::DrawTreeItemButton(wxWindow
* win
,
299 wxDC
& dc
, const wxRect
& rect
, int flags
)
301 GtkWidget
*tree
= GetTreeWidget();
303 GdkWindow
* gdk_window
= NULL
;
305 wxImplDC
*impl
= dc
.GetImpl();
306 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
308 gdk_window
= gtk_impl
->GetGDKWindow();
310 gdk_window
= dc
.GetGDKWindow();
312 wxASSERT_MSG( gdk_window
,
313 wxT("cannot use wxRendererNative on wxDC of this type") );
316 if ( flags
& wxCONTROL_CURRENT
)
317 state
= GTK_STATE_PRELIGHT
;
319 state
= GTK_STATE_NORMAL
;
322 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
325 // VZ: I don't know how to get the size of the expander so as to centre it
326 // in the given rectangle, +2/3 below is just what looks good here...
335 dc
.LogicalToDeviceX(rect
.x
) + 6 - x_diff
,
336 dc
.LogicalToDeviceY(rect
.y
) + 3,
337 flags
& wxCONTROL_EXPANDED
? GTK_EXPANDER_EXPANDED
338 : GTK_EXPANDER_COLLAPSED
343 // ----------------------------------------------------------------------------
344 // splitter sash drawing
345 // ----------------------------------------------------------------------------
347 static int GetGtkSplitterFullSize()
349 static GtkWidget
*s_paned
= NULL
;
351 s_paned
= gtk_vpaned_new();
354 gtk_widget_style_get (s_paned
, "handle_size", &handle_size
, NULL
);
359 wxSplitterRenderParams
360 wxRendererGTK::GetSplitterParams(const wxWindow
*WXUNUSED(win
))
362 // we don't draw any border, hence 0 for the second field
363 return wxSplitterRenderParams
365 GetGtkSplitterFullSize(),
367 true // hot sensitive
372 wxRendererGTK::DrawSplitterBorder(wxWindow
* WXUNUSED(win
),
374 const wxRect
& WXUNUSED(rect
),
381 wxRendererGTK::DrawSplitterSash(wxWindow
*win
,
385 wxOrientation orient
,
388 if ( !win
->m_wxwindow
->window
)
390 // window not realized yet
394 GdkWindow
* gdk_window
= NULL
;
396 wxImplDC
*impl
= dc
.GetImpl();
397 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
399 gdk_window
= gtk_impl
->GetGDKWindow();
401 gdk_window
= dc
.GetGDKWindow();
403 wxASSERT_MSG( gdk_window
,
404 wxT("cannot use wxRendererNative on wxDC of this type") );
406 wxCoord full_size
= GetGtkSplitterFullSize();
408 // are we drawing vertical or horizontal splitter?
409 const bool isVert
= orient
== wxVERTICAL
;
417 rect
.width
= full_size
;
418 rect
.height
= size
.y
;
424 rect
.height
= full_size
;
429 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
434 win
->m_wxwindow
->style
,
436 flags
& wxCONTROL_CURRENT
? GTK_STATE_PRELIGHT
: GTK_STATE_NORMAL
,
438 NULL
/* no clipping */,
441 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
442 dc
.LogicalToDeviceY(rect
.y
),
445 isVert
? GTK_ORIENTATION_VERTICAL
: GTK_ORIENTATION_HORIZONTAL
450 wxRendererGTK::DrawDropArrow(wxWindow
*WXUNUSED(win
),
455 GtkWidget
*button
= GetButtonWidget();
457 // If we give GTK_PIZZA(win->m_wxwindow)->bin_window as
458 // a window for gtk_paint_xxx function, then it won't
459 // work for wxMemoryDC. So that is why we assume wxDC
460 // is wxWindowDC (wxClientDC, wxMemoryDC and wxPaintDC
461 // are derived from it) and use its m_window.
462 GdkWindow
* gdk_window
= NULL
;
464 wxImplDC
*impl
= dc
.GetImpl();
465 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
467 gdk_window
= gtk_impl
->GetGDKWindow();
469 gdk_window
= dc
.GetGDKWindow();
471 wxASSERT_MSG( gdk_window
,
472 wxT("cannot use wxRendererNative on wxDC of this type") );
474 // draw arrow so that there is even space horizontally
476 int arrowX
= rect
.width
/4 + 1;
477 int arrowWidth
= rect
.width
- (arrowX
*2);
479 // scale arrow's height accoording to the width
480 int arrowHeight
= rect
.width
/3;
481 int arrowY
= (rect
.height
-arrowHeight
)/2 +
482 ((rect
.height
-arrowHeight
) & 1);
486 if ( flags
& wxCONTROL_PRESSED
)
487 state
= GTK_STATE_ACTIVE
;
488 else if ( flags
& wxCONTROL_DISABLED
)
489 state
= GTK_STATE_INSENSITIVE
;
490 else if ( flags
& wxCONTROL_CURRENT
)
491 state
= GTK_STATE_PRELIGHT
;
493 state
= GTK_STATE_NORMAL
;
495 // draw arrow on button
501 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
515 wxRendererGTK::DrawComboBoxDropButton(wxWindow
*win
,
520 DrawPushButton(win
,dc
,rect
,flags
);
521 DrawDropArrow(win
,dc
,rect
);
525 wxRendererGTK::DrawCheckBox(wxWindow
*WXUNUSED(win
),
530 GtkWidget
*button
= GetCheckButtonWidget();
532 GdkWindow
* gdk_window
= NULL
;
534 wxImplDC
*impl
= dc
.GetImpl();
535 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
537 gdk_window
= gtk_impl
->GetGDKWindow();
539 gdk_window
= dc
.GetGDKWindow();
541 wxASSERT_MSG( gdk_window
,
542 wxT("cannot use wxRendererNative on wxDC of this type") );
546 if ( flags
& wxCONTROL_PRESSED
)
547 state
= GTK_STATE_ACTIVE
;
548 else if ( flags
& wxCONTROL_DISABLED
)
549 state
= GTK_STATE_INSENSITIVE
;
550 else if ( flags
& wxCONTROL_CURRENT
)
551 state
= GTK_STATE_PRELIGHT
;
553 state
= GTK_STATE_NORMAL
;
560 flags
& wxCONTROL_CHECKED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
564 dc
.LogicalToDeviceX(rect
.x
)+2,
565 dc
.LogicalToDeviceY(rect
.y
)+3,
571 wxRendererGTK::DrawPushButton(wxWindow
*WXUNUSED(win
),
576 GtkWidget
*button
= GetButtonWidget();
578 GdkWindow
* gdk_window
= NULL
;
580 wxImplDC
*impl
= dc
.GetImpl();
581 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
583 gdk_window
= gtk_impl
->GetGDKWindow();
585 gdk_window
= dc
.GetGDKWindow();
587 wxASSERT_MSG( gdk_window
,
588 wxT("cannot use wxRendererNative on wxDC of this type") );
593 if ( flags
& wxCONTROL_PRESSED
)
594 state
= GTK_STATE_ACTIVE
;
595 else if ( flags
& wxCONTROL_DISABLED
)
596 state
= GTK_STATE_INSENSITIVE
;
597 else if ( flags
& wxCONTROL_CURRENT
)
598 state
= GTK_STATE_PRELIGHT
;
600 state
= GTK_STATE_NORMAL
;
607 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
611 rect
.x
, rect
.y
, rect
.width
, rect
.height
616 wxRendererGTK::DrawItemSelectionRect(wxWindow
*win
,
621 GdkWindow
* gdk_window
= NULL
;
623 wxImplDC
*impl
= dc
.GetImpl();
624 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
626 gdk_window
= gtk_impl
->GetGDKWindow();
628 gdk_window
= dc
.GetGDKWindow();
630 wxASSERT_MSG( gdk_window
,
631 wxT("cannot use wxRendererNative on wxDC of this type") );
634 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
638 if (flags
& wxCONTROL_SELECTED
)
640 // the wxCONTROL_FOCUSED state is deduced
641 // directly from the m_wxwindow by GTK+
642 state
= GTK_STATE_SELECTED
;
644 gtk_paint_flat_box( win
->m_widget
->style
,
651 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
652 dc
.LogicalToDeviceY(rect
.y
),
656 else // !wxCONTROL_SELECTED
658 state
= GTK_STATE_NORMAL
;
661 if ((flags
& wxCONTROL_CURRENT
) && (flags
& wxCONTROL_FOCUSED
))
663 gtk_paint_focus( win
->m_widget
->style
,
669 dc
.LogicalToDeviceX(rect
.x
),
670 dc
.LogicalToDeviceY(rect
.y
),
676 void wxRendererGTK::DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
678 GdkWindow
* gdk_window
= NULL
;
680 wxImplDC
*impl
= dc
.GetImpl();
681 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
683 gdk_window
= gtk_impl
->GetGDKWindow();
685 gdk_window
= dc
.GetGDKWindow();
687 wxASSERT_MSG( gdk_window
,
688 wxT("cannot use wxRendererNative on wxDC of this type") );
691 if (flags
& wxCONTROL_SELECTED
)
692 state
= GTK_STATE_SELECTED
;
694 state
= GTK_STATE_NORMAL
;
696 gtk_paint_focus( win
->m_widget
->style
,
702 dc
.LogicalToDeviceX(rect
.x
),
703 dc
.LogicalToDeviceY(rect
.y
),