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"
36 #include "wx/gtk/win_gtk.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 wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
98 // FIXME: shouldn't we destroy these windows somewhere?
100 // used by DrawHeaderButton and DrawPushButton
101 static GtkWidget
*GetButtonWidget();
103 // used by DrawTreeItemButton()
104 static GtkWidget
*GetTreeWidget();
106 // used by DrawCheckBox()
107 static GtkWidget
*GetCheckButtonWidget();
110 // ============================================================================
112 // ============================================================================
115 wxRendererNative
& wxRendererNative::GetDefault()
117 static wxRendererGTK s_rendererGTK
;
119 return s_rendererGTK
;
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
127 wxRendererGTK::GetButtonWidget()
129 static GtkWidget
*s_button
= NULL
;
130 static GtkWidget
*s_window
= NULL
;
134 s_window
= gtk_window_new( GTK_WINDOW_POPUP
);
135 gtk_widget_realize( s_window
);
136 s_button
= gtk_button_new();
137 gtk_container_add( GTK_CONTAINER(s_window
), s_button
);
138 gtk_widget_realize( s_button
);
145 wxRendererGTK::GetCheckButtonWidget()
147 static GtkWidget
*s_button
= NULL
;
148 static GtkWidget
*s_window
= NULL
;
152 s_window
= gtk_window_new( GTK_WINDOW_POPUP
);
153 gtk_widget_realize( s_window
);
154 s_button
= gtk_check_button_new();
155 gtk_container_add( GTK_CONTAINER(s_window
), s_button
);
156 gtk_widget_realize( s_button
);
163 wxRendererGTK::GetTreeWidget()
165 static GtkWidget
*s_tree
= NULL
;
166 static GtkWidget
*s_window
= NULL
;
170 s_tree
= gtk_tree_view_new();
171 s_window
= gtk_window_new( GTK_WINDOW_POPUP
);
172 gtk_widget_realize( s_window
);
173 gtk_container_add( GTK_CONTAINER(s_window
), s_tree
);
174 gtk_widget_realize( s_tree
);
180 // ----------------------------------------------------------------------------
181 // list/tree controls drawing
182 // ----------------------------------------------------------------------------
185 wxRendererGTK::DrawHeaderButton(wxWindow
*win
,
189 wxHeaderSortIconType sortArrow
,
190 wxHeaderButtonParams
* params
)
193 GtkWidget
*button
= GetButtonWidget();
195 GdkWindow
* gdk_window
= dc
.GetGDKWindow();
196 wxASSERT_MSG( gdk_window
,
197 wxT("cannot use wxRendererNative on wxDC of this type") );
200 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
207 flags
& wxCONTROL_DISABLED
? GTK_STATE_INSENSITIVE
: GTK_STATE_NORMAL
,
212 dc
.LogicalToDeviceX(rect
.x
) - x_diff
, rect
.y
, rect
.width
, rect
.height
215 return DrawHeaderButtonContents(win
, dc
, rect
, flags
, sortArrow
, params
);
218 // draw a ">" or "v" button
220 wxRendererGTK::DrawTreeItemButton(wxWindow
* win
,
221 wxDC
& dc
, const wxRect
& rect
, int flags
)
223 GtkWidget
*tree
= GetTreeWidget();
225 GdkWindow
* gdk_window
= dc
.GetGDKWindow();
226 wxASSERT_MSG( gdk_window
,
227 wxT("cannot use wxRendererNative on wxDC of this type") );
230 if ( flags
& wxCONTROL_CURRENT
)
231 state
= GTK_STATE_PRELIGHT
;
233 state
= GTK_STATE_NORMAL
;
236 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
239 // VZ: I don't know how to get the size of the expander so as to centre it
240 // in the given rectangle, +2/3 below is just what looks good here...
249 dc
.LogicalToDeviceX(rect
.x
) + 6 - x_diff
,
250 dc
.LogicalToDeviceY(rect
.y
) + 3,
251 flags
& wxCONTROL_EXPANDED
? GTK_EXPANDER_EXPANDED
252 : GTK_EXPANDER_COLLAPSED
257 // ----------------------------------------------------------------------------
258 // splitter sash drawing
259 // ----------------------------------------------------------------------------
261 static int GetGtkSplitterFullSize()
263 static GtkWidget
*s_paned
= NULL
;
265 s_paned
= gtk_vpaned_new();
268 gtk_widget_style_get (s_paned
, "handle_size", &handle_size
, NULL
);
273 wxSplitterRenderParams
274 wxRendererGTK::GetSplitterParams(const wxWindow
*WXUNUSED(win
))
276 // we don't draw any border, hence 0 for the second field
277 return wxSplitterRenderParams
279 GetGtkSplitterFullSize(),
281 true // hot sensitive
286 wxRendererGTK::DrawSplitterBorder(wxWindow
* WXUNUSED(win
),
288 const wxRect
& WXUNUSED(rect
),
295 wxRendererGTK::DrawSplitterSash(wxWindow
*win
,
299 wxOrientation orient
,
302 if ( !win
->m_wxwindow
->window
)
304 // window not realized yet
308 GdkWindow
* gdk_window
= dc
.GetGDKWindow();
309 wxASSERT_MSG( gdk_window
,
310 wxT("cannot use wxRendererNative on wxDC of this type") );
312 wxCoord full_size
= GetGtkSplitterFullSize();
314 // are we drawing vertical or horizontal splitter?
315 const bool isVert
= orient
== wxVERTICAL
;
321 int h
= win
->GetClientSize().GetHeight();
325 rect
.width
= full_size
;
330 int w
= win
->GetClientSize().GetWidth();
334 rect
.height
= full_size
;
339 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
344 win
->m_wxwindow
->style
,
346 flags
& wxCONTROL_CURRENT
? GTK_STATE_PRELIGHT
: GTK_STATE_NORMAL
,
348 NULL
/* no clipping */,
351 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
352 dc
.LogicalToDeviceY(rect
.y
),
355 isVert
? GTK_ORIENTATION_VERTICAL
: GTK_ORIENTATION_HORIZONTAL
360 wxRendererGTK::DrawDropArrow(wxWindow
*WXUNUSED(win
),
365 GtkWidget
*button
= GetButtonWidget();
367 // If we give GTK_PIZZA(win->m_wxwindow)->bin_window as
368 // a window for gtk_paint_xxx function, then it won't
369 // work for wxMemoryDC. So that is why we assume wxDC
370 // is wxWindowDC (wxClientDC, wxMemoryDC and wxPaintDC
371 // are derived from it) and use its m_window.
372 GdkWindow
* gdk_window
= dc
.GetGDKWindow();
373 wxASSERT_MSG( gdk_window
,
374 wxT("cannot use wxRendererNative on wxDC of this type") );
376 // draw arrow so that there is even space horizontally
378 int arrowX
= rect
.width
/4 + 1;
379 int arrowWidth
= rect
.width
- (arrowX
*2);
381 // scale arrow's height accoording to the width
382 int arrowHeight
= rect
.width
/3;
383 int arrowY
= (rect
.height
-arrowHeight
)/2 +
384 ((rect
.height
-arrowHeight
) & 1);
388 if ( flags
& wxCONTROL_PRESSED
)
389 state
= GTK_STATE_ACTIVE
;
390 else if ( flags
& wxCONTROL_DISABLED
)
391 state
= GTK_STATE_INSENSITIVE
;
392 else if ( flags
& wxCONTROL_CURRENT
)
393 state
= GTK_STATE_PRELIGHT
;
395 state
= GTK_STATE_NORMAL
;
397 // draw arrow on button
403 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
417 wxRendererGTK::DrawComboBoxDropButton(wxWindow
*win
,
422 DrawPushButton(win
,dc
,rect
,flags
);
423 DrawDropArrow(win
,dc
,rect
);
427 wxRendererGTK::DrawCheckBox(wxWindow
*WXUNUSED(win
),
432 GtkWidget
*button
= GetCheckButtonWidget();
434 // for reason why we do this, see DrawDropArrow
435 GdkWindow
* gdk_window
= dc
.GetGDKWindow();
436 wxASSERT_MSG( gdk_window
,
437 wxT("cannot use wxRendererNative on wxDC of this type") );
441 if ( flags
& wxCONTROL_PRESSED
)
442 state
= GTK_STATE_ACTIVE
;
443 else if ( flags
& wxCONTROL_DISABLED
)
444 state
= GTK_STATE_INSENSITIVE
;
445 else if ( flags
& wxCONTROL_CURRENT
)
446 state
= GTK_STATE_PRELIGHT
;
448 state
= GTK_STATE_NORMAL
;
455 flags
& wxCONTROL_CHECKED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
459 dc
.LogicalToDeviceX(rect
.x
)+2,
460 dc
.LogicalToDeviceY(rect
.y
)+3,
466 wxRendererGTK::DrawPushButton(wxWindow
*WXUNUSED(win
),
471 GtkWidget
*button
= GetButtonWidget();
473 // for reason why we do this, see DrawDropArrow
474 GdkWindow
* gdk_window
= dc
.GetGDKWindow();
475 wxASSERT_MSG( gdk_window
,
476 wxT("cannot use wxRendererNative on wxDC of this type") );
481 if ( flags
& wxCONTROL_PRESSED
)
482 state
= GTK_STATE_ACTIVE
;
483 else if ( flags
& wxCONTROL_DISABLED
)
484 state
= GTK_STATE_INSENSITIVE
;
485 else if ( flags
& wxCONTROL_CURRENT
)
486 state
= GTK_STATE_PRELIGHT
;
488 state
= GTK_STATE_NORMAL
;
495 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
499 rect
.x
, rect
.y
, rect
.width
, rect
.height
504 wxRendererGTK::DrawItemSelectionRect(wxWindow
*win
,
509 GdkWindow
* gdk_window
= dc
.GetGDKWindow();
510 wxASSERT_MSG( gdk_window
,
511 wxT("cannot use wxRendererNative on wxDC of this type") );
514 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
518 if (flags
& wxCONTROL_SELECTED
)
520 // the wxCONTROL_FOCUSED state is deduced
521 // directly from the m_wxwindow by GTK+
522 state
= GTK_STATE_SELECTED
;
524 gtk_paint_flat_box( win
->m_widget
->style
,
531 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
532 dc
.LogicalToDeviceY(rect
.y
),
537 if (flags
& wxCONTROL_CURRENT
)
539 gtk_paint_focus( win
->m_widget
->style
,
545 dc
.LogicalToDeviceX(rect
.x
),
546 dc
.LogicalToDeviceY(rect
.y
),