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"
33 #include "wx/module.h"
36 #include "wx/gtk/dc.h"
40 // ----------------------------------------------------------------------------
41 // wxRendererGTK: our wxRendererNative implementation
42 // ----------------------------------------------------------------------------
44 class WXDLLEXPORT wxRendererGTK
: public wxDelegateRendererNative
47 // draw the header control button (used by wxListCtrl)
48 virtual int DrawHeaderButton(wxWindow
*win
,
52 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
,
53 wxHeaderButtonParams
* params
= NULL
);
55 // draw the expanded/collapsed icon for a tree control item
56 virtual void DrawTreeItemButton(wxWindow
*win
,
61 virtual void DrawSplitterBorder(wxWindow
*win
,
65 virtual void DrawSplitterSash(wxWindow
*win
,
72 virtual void DrawComboBoxDropButton(wxWindow
*win
,
77 virtual void DrawDropArrow(wxWindow
*win
,
82 virtual void DrawCheckBox(wxWindow
*win
,
87 virtual void DrawPushButton(wxWindow
*win
,
92 virtual void DrawItemSelectionRect(wxWindow
*win
,
97 virtual void DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
= 0);
99 virtual wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
105 // used by DrawPushButton and DrawDropArrow
106 static GtkWidget
*GetButtonWidget();
108 // used by DrawTreeItemButton()
109 static GtkWidget
*GetTreeWidget();
111 // used by DrawCheckBox()
112 static GtkWidget
*GetCheckButtonWidget();
114 // Used by DrawHeaderButton
115 static GtkWidget
*GetHeaderButtonWidget();
117 static GtkWidget
* GetSplitterWidget();
119 // container for created widgets
120 static GtkContainer
* GetContainer();
121 static GtkWidget
* ms_container
;
124 // Module for destroying created widgets
125 class wxRendererGTK::Module
: public wxModule
128 virtual bool OnInit()
132 virtual void OnExit()
134 if (wxRendererGTK::ms_container
)
137 gtk_widget_get_parent(wxRendererGTK::ms_container
);
138 gtk_widget_destroy(parent
);
141 DECLARE_DYNAMIC_CLASS(wxRendererGTK::Module
)
143 IMPLEMENT_DYNAMIC_CLASS(wxRendererGTK::Module
, wxModule
)
145 // ============================================================================
147 // ============================================================================
149 GtkWidget
* wxRendererGTK::ms_container
;
152 wxRendererNative
& wxRendererNative::GetDefault()
154 static wxRendererGTK s_rendererGTK
;
156 return s_rendererGTK
;
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 GtkContainer
* wxRendererGTK::GetContainer()
165 if (ms_container
== NULL
)
167 GtkWidget
* window
= gtk_window_new(GTK_WINDOW_POPUP
);
168 ms_container
= gtk_fixed_new();
169 gtk_container_add(GTK_CONTAINER(window
), ms_container
);
171 return GTK_CONTAINER(ms_container
);
175 wxRendererGTK::GetButtonWidget()
177 static GtkWidget
*s_button
= NULL
;
181 s_button
= gtk_button_new();
182 gtk_container_add(GetContainer(), s_button
);
183 gtk_widget_realize( s_button
);
190 wxRendererGTK::GetCheckButtonWidget()
192 static GtkWidget
*s_button
= NULL
;
196 s_button
= gtk_check_button_new();
197 gtk_container_add(GetContainer(), s_button
);
198 gtk_widget_realize( s_button
);
205 wxRendererGTK::GetTreeWidget()
207 static GtkWidget
*s_tree
= NULL
;
211 s_tree
= gtk_tree_view_new();
212 gtk_container_add(GetContainer(), s_tree
);
213 gtk_widget_realize( s_tree
);
220 GtkWidget
*GetEntryWidget()
222 static GtkWidget
*s_entry
= NULL
;
223 static GtkWidget
*s_window
= NULL
;
227 s_window
= gtk_window_new( GTK_WINDOW_POPUP
);
228 gtk_widget_realize( s_window
);
229 s_entry
= gtk_entry_new();
230 gtk_container_add( GTK_CONTAINER(s_window
), s_entry
);
231 gtk_widget_realize( s_entry
);
237 // This one just gets the button used by the column header. Although it's
238 // still a gtk_button the themes will typically differentiate and draw them
239 // differently if the button is in a treeview.
241 wxRendererGTK::GetHeaderButtonWidget()
243 static GtkWidget
*s_button
= NULL
;
247 // Get the dummy tree widget, give it a column, and then use the
248 // widget in the column header for the rendering code.
249 GtkWidget
* treewidget
= GetTreeWidget();
250 GtkTreeViewColumn
* column
= gtk_tree_view_column_new();
251 gtk_tree_view_append_column(GTK_TREE_VIEW(treewidget
), column
);
252 s_button
= column
->button
;
258 GtkWidget
* wxRendererGTK::GetSplitterWidget()
260 static GtkWidget
* widget
;
263 widget
= gtk_vpaned_new();
264 gtk_container_add(GetContainer(), widget
);
265 gtk_widget_realize(widget
);
270 // ----------------------------------------------------------------------------
271 // list/tree controls drawing
272 // ----------------------------------------------------------------------------
275 wxRendererGTK::DrawHeaderButton(wxWindow
*win
,
279 wxHeaderSortIconType sortArrow
,
280 wxHeaderButtonParams
* params
)
283 GtkWidget
*button
= GetHeaderButtonWidget();
285 GdkWindow
* gdk_window
= NULL
;
287 wxDCImpl
*impl
= dc
.GetImpl();
288 wxGTKDCImpl
*gtk_impl
= wxDynamicCast( impl
, wxGTKDCImpl
);
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 (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
301 GtkStateType state
= GTK_STATE_NORMAL
;
302 if (flags
& wxCONTROL_DISABLED
)
303 state
= GTK_STATE_INSENSITIVE
;
306 if (flags
& wxCONTROL_CURRENT
)
307 state
= GTK_STATE_PRELIGHT
;
319 dc
.LogicalToDeviceX(rect
.x
) - x_diff
, rect
.y
, rect
.width
, rect
.height
322 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
325 // draw a ">" or "v" button
327 wxRendererGTK::DrawTreeItemButton(wxWindow
* win
,
328 wxDC
& dc
, const wxRect
& rect
, int flags
)
330 GtkWidget
*tree
= GetTreeWidget();
332 GdkWindow
* gdk_window
= NULL
;
334 wxDCImpl
*impl
= dc
.GetImpl();
335 wxGTKDCImpl
*gtk_impl
= wxDynamicCast( impl
, wxGTKDCImpl
);
337 gdk_window
= gtk_impl
->GetGDKWindow();
339 gdk_window
= dc
.GetGDKWindow();
341 wxASSERT_MSG( gdk_window
,
342 wxT("cannot use wxRendererNative on wxDC of this type") );
345 if ( flags
& wxCONTROL_CURRENT
)
346 state
= GTK_STATE_PRELIGHT
;
348 state
= GTK_STATE_NORMAL
;
351 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
354 // VZ: I don't know how to get the size of the expander so as to centre it
355 // in the given rectangle, +2/3 below is just what looks good here...
364 dc
.LogicalToDeviceX(rect
.x
) + 6 - x_diff
,
365 dc
.LogicalToDeviceY(rect
.y
) + 3,
366 flags
& wxCONTROL_EXPANDED
? GTK_EXPANDER_EXPANDED
367 : GTK_EXPANDER_COLLAPSED
372 // ----------------------------------------------------------------------------
373 // splitter sash drawing
374 // ----------------------------------------------------------------------------
376 static int GetGtkSplitterFullSize(GtkWidget
* widget
)
379 gtk_widget_style_get(widget
, "handle_size", &handle_size
, NULL
);
384 wxSplitterRenderParams
385 wxRendererGTK::GetSplitterParams(const wxWindow
*WXUNUSED(win
))
387 // we don't draw any border, hence 0 for the second field
388 return wxSplitterRenderParams
390 GetGtkSplitterFullSize(GetSplitterWidget()),
392 true // hot sensitive
397 wxRendererGTK::DrawSplitterBorder(wxWindow
* WXUNUSED(win
),
399 const wxRect
& WXUNUSED(rect
),
406 wxRendererGTK::DrawSplitterSash(wxWindow
*win
,
410 wxOrientation orient
,
413 if ( !win
->m_wxwindow
->window
)
415 // window not realized yet
419 GdkWindow
* gdk_window
= NULL
;
421 wxDCImpl
*impl
= dc
.GetImpl();
422 wxGTKDCImpl
*gtk_impl
= wxDynamicCast( impl
, wxGTKDCImpl
);
424 gdk_window
= gtk_impl
->GetGDKWindow();
426 gdk_window
= dc
.GetGDKWindow();
428 wxASSERT_MSG( gdk_window
,
429 wxT("cannot use wxRendererNative on wxDC of this type") );
431 wxCoord full_size
= GetGtkSplitterFullSize(GetSplitterWidget());
433 // are we drawing vertical or horizontal splitter?
434 const bool isVert
= orient
== wxVERTICAL
;
442 rect
.width
= full_size
;
443 rect
.height
= size
.y
;
449 rect
.height
= full_size
;
454 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
459 win
->m_wxwindow
->style
,
461 flags
& wxCONTROL_CURRENT
? GTK_STATE_PRELIGHT
: GTK_STATE_NORMAL
,
463 NULL
/* no clipping */,
466 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
467 dc
.LogicalToDeviceY(rect
.y
),
470 isVert
? GTK_ORIENTATION_VERTICAL
: GTK_ORIENTATION_HORIZONTAL
475 wxRendererGTK::DrawDropArrow(wxWindow
*WXUNUSED(win
),
480 GtkWidget
*button
= GetButtonWidget();
482 // If we give GTK_PIZZA(win->m_wxwindow)->bin_window as
483 // a window for gtk_paint_xxx function, then it won't
484 // work for wxMemoryDC. So that is why we assume wxDC
485 // is wxWindowDC (wxClientDC, wxMemoryDC and wxPaintDC
486 // are derived from it) and use its m_window.
487 GdkWindow
* gdk_window
= NULL
;
489 wxDCImpl
*impl
= dc
.GetImpl();
490 wxGTKDCImpl
*gtk_impl
= wxDynamicCast( impl
, wxGTKDCImpl
);
492 gdk_window
= gtk_impl
->GetGDKWindow();
494 gdk_window
= dc
.GetGDKWindow();
496 wxASSERT_MSG( gdk_window
,
497 wxT("cannot use wxRendererNative on wxDC of this type") );
499 // draw arrow so that there is even space horizontally
501 int arrowX
= rect
.width
/4 + 1;
502 int arrowWidth
= rect
.width
- (arrowX
*2);
504 // scale arrow's height accoording to the width
505 int arrowHeight
= rect
.width
/3;
506 int arrowY
= (rect
.height
-arrowHeight
)/2 +
507 ((rect
.height
-arrowHeight
) & 1);
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
;
520 // draw arrow on button
526 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
540 wxRendererGTK::DrawComboBoxDropButton(wxWindow
*win
,
545 DrawPushButton(win
,dc
,rect
,flags
);
546 DrawDropArrow(win
,dc
,rect
);
550 wxRendererGTK::DrawCheckBox(wxWindow
*WXUNUSED(win
),
555 GtkWidget
*button
= GetCheckButtonWidget();
557 GdkWindow
* gdk_window
= NULL
;
559 wxDCImpl
*impl
= dc
.GetImpl();
560 wxGTKDCImpl
*gtk_impl
= wxDynamicCast( impl
, wxGTKDCImpl
);
562 gdk_window
= gtk_impl
->GetGDKWindow();
564 gdk_window
= dc
.GetGDKWindow();
566 wxASSERT_MSG( gdk_window
,
567 wxT("cannot use wxRendererNative on wxDC of this type") );
571 if ( flags
& wxCONTROL_PRESSED
)
572 state
= GTK_STATE_ACTIVE
;
573 else if ( flags
& wxCONTROL_DISABLED
)
574 state
= GTK_STATE_INSENSITIVE
;
575 else if ( flags
& wxCONTROL_CURRENT
)
576 state
= GTK_STATE_PRELIGHT
;
578 state
= GTK_STATE_NORMAL
;
585 flags
& wxCONTROL_CHECKED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
589 dc
.LogicalToDeviceX(rect
.x
)+2,
590 dc
.LogicalToDeviceY(rect
.y
)+3,
596 wxRendererGTK::DrawPushButton(wxWindow
*WXUNUSED(win
),
601 GtkWidget
*button
= GetButtonWidget();
603 GdkWindow
* gdk_window
= NULL
;
605 wxDCImpl
*impl
= dc
.GetImpl();
606 wxGTKDCImpl
*gtk_impl
= wxDynamicCast( impl
, wxGTKDCImpl
);
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") );
618 if ( flags
& wxCONTROL_PRESSED
)
619 state
= GTK_STATE_ACTIVE
;
620 else if ( flags
& wxCONTROL_DISABLED
)
621 state
= GTK_STATE_INSENSITIVE
;
622 else if ( flags
& wxCONTROL_CURRENT
)
623 state
= GTK_STATE_PRELIGHT
;
625 state
= GTK_STATE_NORMAL
;
632 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
636 rect
.x
, rect
.y
, rect
.width
, rect
.height
641 wxRendererGTK::DrawItemSelectionRect(wxWindow
*win
,
646 GdkWindow
* gdk_window
= NULL
;
648 wxDCImpl
*impl
= dc
.GetImpl();
649 wxGTKDCImpl
*gtk_impl
= wxDynamicCast( impl
, wxGTKDCImpl
);
651 gdk_window
= gtk_impl
->GetGDKWindow();
653 gdk_window
= dc
.GetGDKWindow();
655 wxASSERT_MSG( gdk_window
,
656 wxT("cannot use wxRendererNative on wxDC of this type") );
659 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
663 if (flags
& wxCONTROL_SELECTED
)
665 // the wxCONTROL_FOCUSED state is deduced
666 // directly from the m_wxwindow by GTK+
667 state
= GTK_STATE_SELECTED
;
669 gtk_paint_flat_box( win
->m_widget
->style
,
676 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
677 dc
.LogicalToDeviceY(rect
.y
),
681 else // !wxCONTROL_SELECTED
683 state
= GTK_STATE_NORMAL
;
686 if ((flags
& wxCONTROL_CURRENT
) && (flags
& wxCONTROL_FOCUSED
))
688 gtk_paint_focus( win
->m_widget
->style
,
694 dc
.LogicalToDeviceX(rect
.x
),
695 dc
.LogicalToDeviceY(rect
.y
),
701 void wxRendererGTK::DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
703 GdkWindow
* gdk_window
= NULL
;
705 wxDCImpl
*impl
= dc
.GetImpl();
706 wxGTKDCImpl
*gtk_impl
= wxDynamicCast( impl
, wxGTKDCImpl
);
708 gdk_window
= gtk_impl
->GetGDKWindow();
710 gdk_window
= dc
.GetGDKWindow();
712 wxASSERT_MSG( gdk_window
,
713 wxT("cannot use wxRendererNative on wxDC of this type") );
716 if (flags
& wxCONTROL_SELECTED
)
717 state
= GTK_STATE_SELECTED
;
719 state
= GTK_STATE_NORMAL
;
721 gtk_paint_focus( win
->m_widget
->style
,
727 dc
.LogicalToDeviceX(rect
.x
),
728 dc
.LogicalToDeviceY(rect
.y
),