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"
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
);
103 // used by DrawPushButton and DrawDropArrow
104 static GtkWidget
*GetButtonWidget();
106 // used by DrawTreeItemButton()
107 static GtkWidget
*GetTreeWidget();
109 // used by DrawCheckBox()
110 static GtkWidget
*GetCheckButtonWidget();
112 // Used by DrawHeaderButton
113 static GtkWidget
*GetHeaderButtonWidget();
115 static GtkWidget
* GetSplitterWidget();
117 // container for created widgets
118 static GtkContainer
* GetContainer();
119 static GtkWidget
* ms_container
;
122 // Module for destroying created widgets
123 class wxRendererGTK::Module
: public wxModule
126 virtual bool OnInit()
130 virtual void OnExit()
132 if (wxRendererGTK::ms_container
)
135 gtk_widget_get_parent(wxRendererGTK::ms_container
);
136 gtk_widget_destroy(parent
);
139 DECLARE_DYNAMIC_CLASS(wxRendererGTK::Module
)
141 IMPLEMENT_DYNAMIC_CLASS(wxRendererGTK::Module
, wxModule
)
143 // ============================================================================
145 // ============================================================================
147 GtkWidget
* wxRendererGTK::ms_container
;
150 wxRendererNative
& wxRendererNative::GetDefault()
152 static wxRendererGTK s_rendererGTK
;
154 return s_rendererGTK
;
157 // ----------------------------------------------------------------------------
159 // ----------------------------------------------------------------------------
161 GtkContainer
* wxRendererGTK::GetContainer()
163 if (ms_container
== NULL
)
165 GtkWidget
* window
= gtk_window_new(GTK_WINDOW_POPUP
);
166 ms_container
= gtk_fixed_new();
167 gtk_container_add(GTK_CONTAINER(window
), ms_container
);
169 return GTK_CONTAINER(ms_container
);
173 wxRendererGTK::GetButtonWidget()
175 static GtkWidget
*s_button
= NULL
;
179 s_button
= gtk_button_new();
180 gtk_container_add(GetContainer(), s_button
);
181 gtk_widget_realize( s_button
);
188 wxRendererGTK::GetCheckButtonWidget()
190 static GtkWidget
*s_button
= NULL
;
194 s_button
= gtk_check_button_new();
195 gtk_container_add(GetContainer(), s_button
);
196 gtk_widget_realize( s_button
);
203 wxRendererGTK::GetTreeWidget()
205 static GtkWidget
*s_tree
= NULL
;
209 s_tree
= gtk_tree_view_new();
210 gtk_container_add(GetContainer(), s_tree
);
211 gtk_widget_realize( s_tree
);
218 GtkWidget
*GetEntryWidget()
220 static GtkWidget
*s_entry
= NULL
;
221 static GtkWidget
*s_window
= NULL
;
225 s_window
= gtk_window_new( GTK_WINDOW_POPUP
);
226 gtk_widget_realize( s_window
);
227 s_entry
= gtk_entry_new();
228 gtk_container_add( GTK_CONTAINER(s_window
), s_entry
);
229 gtk_widget_realize( s_entry
);
235 // This one just gets the button used by the column header. Although it's
236 // still a gtk_button the themes will typically differentiate and draw them
237 // differently if the button is in a treeview.
239 wxRendererGTK::GetHeaderButtonWidget()
241 static GtkWidget
*s_button
= NULL
;
245 // Get the dummy tree widget, give it a column, and then use the
246 // widget in the column header for the rendering code.
247 GtkWidget
* treewidget
= GetTreeWidget();
248 GtkTreeViewColumn
* column
= gtk_tree_view_column_new();
249 gtk_tree_view_append_column(GTK_TREE_VIEW(treewidget
), column
);
250 s_button
= column
->button
;
256 GtkWidget
* wxRendererGTK::GetSplitterWidget()
258 static GtkWidget
* widget
;
261 widget
= gtk_vpaned_new();
262 gtk_container_add(GetContainer(), widget
);
263 gtk_widget_realize(widget
);
268 // ----------------------------------------------------------------------------
269 // list/tree controls drawing
270 // ----------------------------------------------------------------------------
273 wxRendererGTK::DrawHeaderButton(wxWindow
*win
,
277 wxHeaderSortIconType sortArrow
,
278 wxHeaderButtonParams
* params
)
281 GtkWidget
*button
= GetHeaderButtonWidget();
283 GdkWindow
* gdk_window
= NULL
;
285 wxImplDC
*impl
= dc
.GetImpl();
286 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
288 gdk_window
= gtk_impl
->GetGDKWindow();
290 gdk_window
= dc
.GetGDKWindow();
292 wxASSERT_MSG( gdk_window
,
293 wxT("cannot use wxRendererNative on wxDC of this type") );
296 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
299 GtkStateType state
= GTK_STATE_NORMAL
;
300 if (flags
& wxCONTROL_DISABLED
)
301 state
= GTK_STATE_INSENSITIVE
;
304 if (flags
& wxCONTROL_CURRENT
)
305 state
= GTK_STATE_PRELIGHT
;
317 dc
.LogicalToDeviceX(rect
.x
) - x_diff
, rect
.y
, rect
.width
, rect
.height
320 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
323 // draw a ">" or "v" button
325 wxRendererGTK::DrawTreeItemButton(wxWindow
* win
,
326 wxDC
& dc
, const wxRect
& rect
, int flags
)
328 GtkWidget
*tree
= GetTreeWidget();
330 GdkWindow
* gdk_window
= NULL
;
332 wxImplDC
*impl
= dc
.GetImpl();
333 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
335 gdk_window
= gtk_impl
->GetGDKWindow();
337 gdk_window
= dc
.GetGDKWindow();
339 wxASSERT_MSG( gdk_window
,
340 wxT("cannot use wxRendererNative on wxDC of this type") );
343 if ( flags
& wxCONTROL_CURRENT
)
344 state
= GTK_STATE_PRELIGHT
;
346 state
= GTK_STATE_NORMAL
;
349 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
352 // VZ: I don't know how to get the size of the expander so as to centre it
353 // in the given rectangle, +2/3 below is just what looks good here...
362 dc
.LogicalToDeviceX(rect
.x
) + 6 - x_diff
,
363 dc
.LogicalToDeviceY(rect
.y
) + 3,
364 flags
& wxCONTROL_EXPANDED
? GTK_EXPANDER_EXPANDED
365 : GTK_EXPANDER_COLLAPSED
370 // ----------------------------------------------------------------------------
371 // splitter sash drawing
372 // ----------------------------------------------------------------------------
374 static int GetGtkSplitterFullSize(GtkWidget
* widget
)
377 gtk_widget_style_get(widget
, "handle_size", &handle_size
, NULL
);
382 wxSplitterRenderParams
383 wxRendererGTK::GetSplitterParams(const wxWindow
*WXUNUSED(win
))
385 // we don't draw any border, hence 0 for the second field
386 return wxSplitterRenderParams
388 GetGtkSplitterFullSize(GetSplitterWidget()),
390 true // hot sensitive
395 wxRendererGTK::DrawSplitterBorder(wxWindow
* WXUNUSED(win
),
397 const wxRect
& WXUNUSED(rect
),
404 wxRendererGTK::DrawSplitterSash(wxWindow
*win
,
408 wxOrientation orient
,
411 if ( !win
->m_wxwindow
->window
)
413 // window not realized yet
417 GdkWindow
* gdk_window
= NULL
;
419 wxImplDC
*impl
= dc
.GetImpl();
420 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
422 gdk_window
= gtk_impl
->GetGDKWindow();
424 gdk_window
= dc
.GetGDKWindow();
426 wxASSERT_MSG( gdk_window
,
427 wxT("cannot use wxRendererNative on wxDC of this type") );
429 wxCoord full_size
= GetGtkSplitterFullSize(GetSplitterWidget());
431 // are we drawing vertical or horizontal splitter?
432 const bool isVert
= orient
== wxVERTICAL
;
440 rect
.width
= full_size
;
441 rect
.height
= size
.y
;
447 rect
.height
= full_size
;
452 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
457 win
->m_wxwindow
->style
,
459 flags
& wxCONTROL_CURRENT
? GTK_STATE_PRELIGHT
: GTK_STATE_NORMAL
,
461 NULL
/* no clipping */,
464 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
465 dc
.LogicalToDeviceY(rect
.y
),
468 isVert
? GTK_ORIENTATION_VERTICAL
: GTK_ORIENTATION_HORIZONTAL
473 wxRendererGTK::DrawDropArrow(wxWindow
*WXUNUSED(win
),
478 GtkWidget
*button
= GetButtonWidget();
480 // If we give GTK_PIZZA(win->m_wxwindow)->bin_window as
481 // a window for gtk_paint_xxx function, then it won't
482 // work for wxMemoryDC. So that is why we assume wxDC
483 // is wxWindowDC (wxClientDC, wxMemoryDC and wxPaintDC
484 // are derived from it) and use its m_window.
485 GdkWindow
* gdk_window
= NULL
;
487 wxImplDC
*impl
= dc
.GetImpl();
488 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
490 gdk_window
= gtk_impl
->GetGDKWindow();
492 gdk_window
= dc
.GetGDKWindow();
494 wxASSERT_MSG( gdk_window
,
495 wxT("cannot use wxRendererNative on wxDC of this type") );
497 // draw arrow so that there is even space horizontally
499 int arrowX
= rect
.width
/4 + 1;
500 int arrowWidth
= rect
.width
- (arrowX
*2);
502 // scale arrow's height accoording to the width
503 int arrowHeight
= rect
.width
/3;
504 int arrowY
= (rect
.height
-arrowHeight
)/2 +
505 ((rect
.height
-arrowHeight
) & 1);
509 if ( flags
& wxCONTROL_PRESSED
)
510 state
= GTK_STATE_ACTIVE
;
511 else if ( flags
& wxCONTROL_DISABLED
)
512 state
= GTK_STATE_INSENSITIVE
;
513 else if ( flags
& wxCONTROL_CURRENT
)
514 state
= GTK_STATE_PRELIGHT
;
516 state
= GTK_STATE_NORMAL
;
518 // draw arrow on button
524 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
538 wxRendererGTK::DrawComboBoxDropButton(wxWindow
*win
,
543 DrawPushButton(win
,dc
,rect
,flags
);
544 DrawDropArrow(win
,dc
,rect
);
548 wxRendererGTK::DrawCheckBox(wxWindow
*WXUNUSED(win
),
553 GtkWidget
*button
= GetCheckButtonWidget();
555 GdkWindow
* gdk_window
= NULL
;
557 wxImplDC
*impl
= dc
.GetImpl();
558 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
560 gdk_window
= gtk_impl
->GetGDKWindow();
562 gdk_window
= dc
.GetGDKWindow();
564 wxASSERT_MSG( gdk_window
,
565 wxT("cannot use wxRendererNative on wxDC of this type") );
569 if ( flags
& wxCONTROL_PRESSED
)
570 state
= GTK_STATE_ACTIVE
;
571 else if ( flags
& wxCONTROL_DISABLED
)
572 state
= GTK_STATE_INSENSITIVE
;
573 else if ( flags
& wxCONTROL_CURRENT
)
574 state
= GTK_STATE_PRELIGHT
;
576 state
= GTK_STATE_NORMAL
;
583 flags
& wxCONTROL_CHECKED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
587 dc
.LogicalToDeviceX(rect
.x
)+2,
588 dc
.LogicalToDeviceY(rect
.y
)+3,
594 wxRendererGTK::DrawPushButton(wxWindow
*WXUNUSED(win
),
599 GtkWidget
*button
= GetButtonWidget();
601 GdkWindow
* gdk_window
= NULL
;
603 wxImplDC
*impl
= dc
.GetImpl();
604 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
606 gdk_window
= gtk_impl
->GetGDKWindow();
608 gdk_window
= dc
.GetGDKWindow();
610 wxASSERT_MSG( gdk_window
,
611 wxT("cannot use wxRendererNative on wxDC of this type") );
616 if ( flags
& wxCONTROL_PRESSED
)
617 state
= GTK_STATE_ACTIVE
;
618 else if ( flags
& wxCONTROL_DISABLED
)
619 state
= GTK_STATE_INSENSITIVE
;
620 else if ( flags
& wxCONTROL_CURRENT
)
621 state
= GTK_STATE_PRELIGHT
;
623 state
= GTK_STATE_NORMAL
;
630 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
634 rect
.x
, rect
.y
, rect
.width
, rect
.height
639 wxRendererGTK::DrawItemSelectionRect(wxWindow
*win
,
644 GdkWindow
* gdk_window
= NULL
;
646 wxImplDC
*impl
= dc
.GetImpl();
647 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
649 gdk_window
= gtk_impl
->GetGDKWindow();
651 gdk_window
= dc
.GetGDKWindow();
653 wxASSERT_MSG( gdk_window
,
654 wxT("cannot use wxRendererNative on wxDC of this type") );
657 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
661 if (flags
& wxCONTROL_SELECTED
)
663 // the wxCONTROL_FOCUSED state is deduced
664 // directly from the m_wxwindow by GTK+
665 state
= GTK_STATE_SELECTED
;
667 gtk_paint_flat_box( win
->m_widget
->style
,
674 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
675 dc
.LogicalToDeviceY(rect
.y
),
679 else // !wxCONTROL_SELECTED
681 state
= GTK_STATE_NORMAL
;
684 if ((flags
& wxCONTROL_CURRENT
) && (flags
& wxCONTROL_FOCUSED
))
686 gtk_paint_focus( win
->m_widget
->style
,
692 dc
.LogicalToDeviceX(rect
.x
),
693 dc
.LogicalToDeviceY(rect
.y
),
699 void wxRendererGTK::DrawFocusRect(wxWindow
* win
, wxDC
& dc
, const wxRect
& rect
, int flags
)
701 GdkWindow
* gdk_window
= NULL
;
703 wxImplDC
*impl
= dc
.GetImpl();
704 wxGTKImplDC
*gtk_impl
= wxDynamicCast( impl
, wxGTKImplDC
);
706 gdk_window
= gtk_impl
->GetGDKWindow();
708 gdk_window
= dc
.GetGDKWindow();
710 wxASSERT_MSG( gdk_window
,
711 wxT("cannot use wxRendererNative on wxDC of this type") );
714 if (flags
& wxCONTROL_SELECTED
)
715 state
= GTK_STATE_SELECTED
;
717 state
= GTK_STATE_NORMAL
;
719 gtk_paint_focus( win
->m_widget
->style
,
725 dc
.LogicalToDeviceX(rect
.x
),
726 dc
.LogicalToDeviceY(rect
.y
),