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 void DrawHeaderButton(wxWindow
*win
,
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 wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
97 // FIXME: shouldn't we destroy these windows somewhere?
99 // used by DrawHeaderButton and DrawPushButton
100 static GtkWidget
*GetButtonWidget();
102 // used by DrawTreeItemButton()
103 static GtkWidget
*GetTreeWidget();
105 // used by DrawCheckBox()
106 static GtkWidget
*GetCheckButtonWidget();
109 // ============================================================================
111 // ============================================================================
114 wxRendererNative
& wxRendererNative::GetDefault()
116 static wxRendererGTK s_rendererGTK
;
118 return s_rendererGTK
;
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
126 wxRendererGTK::GetButtonWidget()
128 static GtkWidget
*s_button
= NULL
;
129 static GtkWidget
*s_window
= NULL
;
133 s_window
= gtk_window_new( GTK_WINDOW_POPUP
);
134 gtk_widget_realize( s_window
);
135 s_button
= gtk_button_new();
136 gtk_container_add( GTK_CONTAINER(s_window
), s_button
);
137 gtk_widget_realize( s_button
);
144 wxRendererGTK::GetCheckButtonWidget()
146 static GtkWidget
*s_button
= NULL
;
147 static GtkWidget
*s_window
= NULL
;
151 s_window
= gtk_window_new( GTK_WINDOW_POPUP
);
152 gtk_widget_realize( s_window
);
153 s_button
= gtk_check_button_new();
154 gtk_container_add( GTK_CONTAINER(s_window
), s_button
);
155 gtk_widget_realize( s_button
);
162 wxRendererGTK::GetTreeWidget()
164 static GtkWidget
*s_tree
= NULL
;
165 static GtkWidget
*s_window
= NULL
;
169 s_tree
= gtk_tree_view_new();
170 s_window
= gtk_window_new( GTK_WINDOW_POPUP
);
171 gtk_widget_realize( s_window
);
172 gtk_container_add( GTK_CONTAINER(s_window
), s_tree
);
173 gtk_widget_realize( s_tree
);
179 // ----------------------------------------------------------------------------
180 // list/tree controls drawing
181 // ----------------------------------------------------------------------------
184 wxRendererGTK::DrawHeaderButton(wxWindow
*win
,
188 wxHeaderButtonParams
* params
)
191 GtkWidget
*button
= GetButtonWidget();
194 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
200 // FIXME: I suppose GTK_PIZZA(win->m_wxwindow)->bin_window doesn't work with wxMemoryDC.
201 // Maybe use code similar as in DrawPushButton below?
202 GTK_PIZZA(win
->m_wxwindow
)->bin_window
,
203 flags
& wxCONTROL_DISABLED
? GTK_STATE_INSENSITIVE
: GTK_STATE_NORMAL
,
208 dc
.LogicalToDeviceX(rect
.x
) - x_diff
, rect
.y
, rect
.width
, rect
.height
211 DrawHeaderButtonContents(win
, dc
, rect
, flags
, params
);
214 // draw a ">" or "v" button
216 wxRendererGTK::DrawTreeItemButton(wxWindow
* win
,
217 wxDC
& dc
, const wxRect
& rect
, int flags
)
219 GtkWidget
*tree
= GetTreeWidget();
222 if ( flags
& wxCONTROL_CURRENT
)
223 state
= GTK_STATE_PRELIGHT
;
225 state
= GTK_STATE_NORMAL
;
228 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
231 // VZ: I don't know how to get the size of the expander so as to centre it
232 // in the given rectangle, +2/3 below is just what looks good here...
236 GTK_PIZZA(win
->m_wxwindow
)->bin_window
,
241 dc
.LogicalToDeviceX(rect
.x
) + 2 - x_diff
,
242 dc
.LogicalToDeviceY(rect
.y
) + 3,
243 flags
& wxCONTROL_EXPANDED
? GTK_EXPANDER_EXPANDED
244 : GTK_EXPANDER_COLLAPSED
249 // ----------------------------------------------------------------------------
250 // splitter sash drawing
251 // ----------------------------------------------------------------------------
253 static int GetGtkSplitterFullSize()
255 static GtkWidget
*s_paned
= NULL
;
257 s_paned
= gtk_vpaned_new();
260 gtk_widget_style_get (s_paned
, "handle_size", &handle_size
, NULL
);
265 wxSplitterRenderParams
266 wxRendererGTK::GetSplitterParams(const wxWindow
*WXUNUSED(win
))
268 // we don't draw any border, hence 0 for the second field
269 return wxSplitterRenderParams
271 GetGtkSplitterFullSize(),
273 true // hot sensitive
278 wxRendererGTK::DrawSplitterBorder(wxWindow
* WXUNUSED(win
),
280 const wxRect
& WXUNUSED(rect
),
287 wxRendererGTK::DrawSplitterSash(wxWindow
*win
,
291 wxOrientation orient
,
294 if ( !win
->m_wxwindow
->window
)
296 // window not realized yet
300 wxCoord full_size
= GetGtkSplitterFullSize();
302 // are we drawing vertical or horizontal splitter?
303 const bool isVert
= orient
== wxVERTICAL
;
309 int h
= win
->GetClientSize().GetHeight();
313 rect
.width
= full_size
;
318 int w
= win
->GetClientSize().GetWidth();
322 rect
.height
= full_size
;
327 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
332 win
->m_wxwindow
->style
,
333 GTK_PIZZA(win
->m_wxwindow
)->bin_window
,
334 flags
& wxCONTROL_CURRENT
? GTK_STATE_PRELIGHT
: GTK_STATE_NORMAL
,
336 NULL
/* no clipping */,
339 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
340 dc
.LogicalToDeviceY(rect
.y
),
343 isVert
? GTK_ORIENTATION_VERTICAL
: GTK_ORIENTATION_HORIZONTAL
348 wxRendererGTK::DrawDropArrow(wxWindow
*win
,
353 GtkWidget
*button
= GetButtonWidget();
355 // If we give GTK_PIZZA(win->m_wxwindow)->bin_window as
356 // a window for gtk_paint_xxx function, then it won't
357 // work for wxMemoryDC. So that is why we assume wxDC
358 // is wxWindowDC (wxClientDC, wxMemoryDC and wxPaintDC
359 // are derived from it) and use its m_window.
360 wxWindowDC
& wdc
= (wxWindowDC
&)dc
;
362 // only doing debug-time checking here (it should
363 // probably be enough)
364 wxASSERT ( wdc
.IsKindOf(CLASSINFO(wxWindowDC
)) );
366 // draw arrow so that there is even space horizontally
368 int arrowX
= rect
.width
/4 + 1;
369 int arrowWidth
= rect
.width
- (arrowX
*2);
371 // scale arrow's height accoording to the width
372 int arrowHeight
= rect
.width
/3;
373 int arrowY
= (rect
.height
-arrowHeight
)/2 +
374 ((rect
.height
-arrowHeight
) & 1);
378 if ( flags
& wxCONTROL_PRESSED
)
379 state
= GTK_STATE_ACTIVE
;
380 else if ( flags
& wxCONTROL_DISABLED
)
381 state
= GTK_STATE_INSENSITIVE
;
382 else if ( flags
& wxCONTROL_CURRENT
)
383 state
= GTK_STATE_PRELIGHT
;
385 state
= GTK_STATE_NORMAL
;
387 // draw arrow on button
393 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
407 wxRendererGTK::DrawComboBoxDropButton(wxWindow
*win
,
412 DrawPushButton(win
,dc
,rect
,flags
);
413 DrawDropArrow(win
,dc
,rect
);
417 wxRendererGTK::DrawCheckBox(wxWindow
*win
,
422 GtkWidget
*button
= GetCheckButtonWidget();
424 // for reason why we do this, see DrawDropArrow
425 wxWindowDC
& wdc
= (wxWindowDC
&)dc
;
426 wxASSERT ( wdc
.IsKindOf(CLASSINFO(wxWindowDC
)) );
430 if ( flags
& wxCONTROL_PRESSED
)
431 state
= GTK_STATE_ACTIVE
;
432 else if ( flags
& wxCONTROL_DISABLED
)
433 state
= GTK_STATE_INSENSITIVE
;
434 else if ( flags
& wxCONTROL_CURRENT
)
435 state
= GTK_STATE_PRELIGHT
;
437 state
= GTK_STATE_NORMAL
;
444 flags
& wxCONTROL_CHECKED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
448 dc
.LogicalToDeviceX(rect
.x
)+2,
449 dc
.LogicalToDeviceY(rect
.y
)+3,
455 wxRendererGTK::DrawPushButton(wxWindow
*win
,
460 GtkWidget
*button
= GetButtonWidget();
462 // for reason why we do this, see DrawDropArrow
463 wxWindowDC
& wdc
= (wxWindowDC
&)dc
;
464 wxASSERT ( wdc
.IsKindOf(CLASSINFO(wxWindowDC
)) );
469 if ( flags
& wxCONTROL_PRESSED
)
470 state
= GTK_STATE_ACTIVE
;
471 else if ( flags
& wxCONTROL_DISABLED
)
472 state
= GTK_STATE_INSENSITIVE
;
473 else if ( flags
& wxCONTROL_CURRENT
)
474 state
= GTK_STATE_PRELIGHT
;
476 state
= GTK_STATE_NORMAL
;
483 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
487 rect
.x
, rect
.y
, rect
.width
, rect
.height
492 wxRendererGTK::DrawItemSelectionRect(wxWindow
*win
,
498 if (flags
& wxCONTROL_SELECTED
)
500 if (flags
& wxCONTROL_FOCUSED
)
501 state
= GTK_STATE_SELECTED
;
503 state
= GTK_STATE_INSENSITIVE
;
505 gtk_paint_flat_box( win
->m_wxwindow
->style
,
506 GTK_PIZZA(win
->m_wxwindow
)->bin_window
,
512 dc
.LogicalToDeviceX(rect
.x
),
513 dc
.LogicalToDeviceY(rect
.y
),
518 if (flags
& wxCONTROL_CURRENT
)
520 dc
.SetPen( *wxBLACK_PEN
);
521 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
522 dc
.DrawRectangle( rect
);