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"
32 #include "wx/dcclient.h"
36 #include "wx/gtk/win_gtk.h"
38 #include "wx/settings.h"
40 // RR: After a correction to the orientation of the sash
41 // this doesn't seem to be required anymore and it
42 // seems to confuse some themes so USE_ERASE_RECT=0
43 #define USE_ERASE_RECT 0
45 // ----------------------------------------------------------------------------
46 // wxRendererGTK: our wxRendererNative implementation
47 // ----------------------------------------------------------------------------
49 class WXDLLEXPORT wxRendererGTK
: public wxDelegateRendererNative
52 // draw the header control button (used by wxListCtrl)
53 virtual void DrawHeaderButton(wxWindow
*win
,
58 // draw the expanded/collapsed icon for a tree control item
59 virtual void DrawTreeItemButton(wxWindow
*win
,
64 virtual void DrawSplitterBorder(wxWindow
*win
,
68 virtual void DrawSplitterSash(wxWindow
*win
,
75 virtual void DrawComboBoxDropButton(wxWindow
*win
,
80 virtual void DrawDropArrow(wxWindow
*win
,
85 virtual void DrawCheckBox(wxWindow
*win
,
90 virtual void DrawPushButton(wxWindow
*win
,
95 virtual void DrawItemSelectionRect(wxWindow
*win
,
100 virtual wxSplitterRenderParams
GetSplitterParams(const wxWindow
*win
);
103 // FIXME: shouldn't we destroy these windows somewhere?
105 // used by DrawHeaderButton and DrawPushButton
106 static GtkWidget
*GetButtonWidget();
108 // used by DrawTreeItemButton()
109 static GtkWidget
*GetTreeWidget();
111 // used by DrawCheckBox()
112 static GtkWidget
*GetCheckButtonWidget();
115 // ============================================================================
117 // ============================================================================
120 wxRendererNative
& wxRendererNative::GetDefault()
122 static wxRendererGTK s_rendererGTK
;
124 return s_rendererGTK
;
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
132 wxRendererGTK::GetButtonWidget()
134 static GtkWidget
*s_button
= NULL
;
135 static GtkWidget
*s_window
= NULL
;
139 s_window
= gtk_window_new( GTK_WINDOW_POPUP
);
140 gtk_widget_realize( s_window
);
141 s_button
= gtk_button_new();
142 gtk_container_add( GTK_CONTAINER(s_window
), s_button
);
143 gtk_widget_realize( s_button
);
150 wxRendererGTK::GetCheckButtonWidget()
152 static GtkWidget
*s_button
= NULL
;
153 static GtkWidget
*s_window
= NULL
;
157 s_window
= gtk_window_new( GTK_WINDOW_POPUP
);
158 gtk_widget_realize( s_window
);
159 s_button
= gtk_check_button_new();
160 gtk_container_add( GTK_CONTAINER(s_window
), s_button
);
161 gtk_widget_realize( s_button
);
168 wxRendererGTK::GetTreeWidget()
170 static GtkWidget
*s_tree
= NULL
;
171 static GtkWidget
*s_window
= NULL
;
175 s_tree
= gtk_tree_view_new();
176 s_window
= gtk_window_new( GTK_WINDOW_POPUP
);
177 gtk_widget_realize( s_window
);
178 gtk_container_add( GTK_CONTAINER(s_window
), s_tree
);
179 gtk_widget_realize( s_tree
);
185 // ----------------------------------------------------------------------------
186 // list/tree controls drawing
187 // ----------------------------------------------------------------------------
190 wxRendererGTK::DrawHeaderButton(wxWindow
*win
,
196 GtkWidget
*button
= GetButtonWidget();
201 // FIXME: I suppose GTK_PIZZA(win->m_wxwindow)->bin_window doesn't work with wxMemoryDC.
202 // Maybe use code similar as in DrawPushButton below?
203 GTK_PIZZA(win
->m_wxwindow
)->bin_window
,
204 flags
& wxCONTROL_DISABLED
? GTK_STATE_INSENSITIVE
: GTK_STATE_NORMAL
,
209 dc
.XLOG2DEV(rect
.x
), rect
.y
, rect
.width
, rect
.height
213 // draw a ">" or "v" button
215 wxRendererGTK::DrawTreeItemButton(wxWindow
* win
,
216 wxDC
& dc
, const wxRect
& rect
, int flags
)
218 GtkWidget
*tree
= GetTreeWidget();
221 if ( flags
& wxCONTROL_CURRENT
)
222 state
= GTK_STATE_PRELIGHT
;
224 state
= GTK_STATE_NORMAL
;
226 // VZ: I don't know how to get the size of the expander so as to centre it
227 // in the given rectangle, +2/3 below is just what looks good here...
231 GTK_PIZZA(win
->m_wxwindow
)->bin_window
,
236 dc
.LogicalToDeviceX(rect
.x
) + 2,
237 dc
.LogicalToDeviceY(rect
.y
) + 3,
238 flags
& wxCONTROL_EXPANDED
? GTK_EXPANDER_EXPANDED
239 : GTK_EXPANDER_COLLAPSED
244 // ----------------------------------------------------------------------------
245 // splitter sash drawing
246 // ----------------------------------------------------------------------------
248 static int GetGtkSplitterFullSize()
250 static GtkWidget
*s_paned
= NULL
;
252 s_paned
= gtk_vpaned_new();
255 gtk_widget_style_get (s_paned
, "handle_size", &handle_size
, NULL
);
260 wxSplitterRenderParams
261 wxRendererGTK::GetSplitterParams(const wxWindow
*WXUNUSED(win
))
263 // we don't draw any border, hence 0 for the second field
264 return wxSplitterRenderParams
266 GetGtkSplitterFullSize(),
268 true // hot sensitive
273 wxRendererGTK::DrawSplitterBorder(wxWindow
* WXUNUSED(win
),
275 const wxRect
& WXUNUSED(rect
),
282 wxRendererGTK::DrawSplitterSash(wxWindow
*win
,
286 wxOrientation orient
,
289 if ( !win
->m_wxwindow
->window
)
291 // window not realized yet
295 wxCoord full_size
= GetGtkSplitterFullSize();
297 // are we drawing vertical or horizontal splitter?
298 const bool isVert
= orient
== wxVERTICAL
;
302 GdkRectangle erase_rect
;
307 int h
= win
->GetClientSize().GetHeight();
311 rect
.width
= full_size
;
315 erase_rect
.x
= position
;
317 erase_rect
.width
= full_size
;
318 erase_rect
.height
= h
;
323 int w
= win
->GetClientSize().GetWidth();
327 rect
.height
= full_size
;
331 erase_rect
.y
= position
;
333 erase_rect
.height
= full_size
;
334 erase_rect
.width
= w
;
339 // we must erase everything first, otherwise the garbage
340 // from the old sash is left when dragging it
343 win
->m_wxwindow
->style
,
344 GTK_PIZZA(win
->m_wxwindow
)->bin_window
,
349 (char *)"viewportbin", // const_cast
359 win
->m_wxwindow
->style
,
360 GTK_PIZZA(win
->m_wxwindow
)->bin_window
,
361 flags
& wxCONTROL_CURRENT
? GTK_STATE_PRELIGHT
: GTK_STATE_NORMAL
,
363 NULL
/* no clipping */,
370 isVert
? GTK_ORIENTATION_VERTICAL
: GTK_ORIENTATION_HORIZONTAL
375 wxRendererGTK::DrawDropArrow(wxWindow
*win
,
380 GtkWidget
*button
= GetButtonWidget();
382 // If we give GTK_PIZZA(win->m_wxwindow)->bin_window as
383 // a window for gtk_paint_xxx function, then it won't
384 // work for wxMemoryDC. So that is why we assume wxDC
385 // is wxWindowDC (wxClientDC, wxMemoryDC and wxPaintDC
386 // are derived from it) and use its m_window.
387 wxWindowDC
& wdc
= (wxWindowDC
&)dc
;
389 // only doing debug-time checking here (it should
390 // probably be enough)
391 wxASSERT ( wdc
.IsKindOf(CLASSINFO(wxWindowDC
)) );
393 // draw arrow so that there is even space horizontally
395 int arrowX
= rect
.width
/4 + 1;
396 int arrowWidth
= rect
.width
- (arrowX
*2);
398 // scale arrow's height accoording to the width
399 int arrowHeight
= rect
.width
/3;
400 int arrowY
= (rect
.height
-arrowHeight
)/2 +
401 ((rect
.height
-arrowHeight
) & 1);
405 if ( flags
& wxCONTROL_PRESSED
)
406 state
= GTK_STATE_ACTIVE
;
407 else if ( flags
& wxCONTROL_DISABLED
)
408 state
= GTK_STATE_INSENSITIVE
;
409 else if ( flags
& wxCONTROL_CURRENT
)
410 state
= GTK_STATE_PRELIGHT
;
412 state
= GTK_STATE_NORMAL
;
414 // draw arrow on button
420 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
434 wxRendererGTK::DrawComboBoxDropButton(wxWindow
*win
,
439 DrawPushButton(win
,dc
,rect
,flags
);
440 DrawDropArrow(win
,dc
,rect
);
444 wxRendererGTK::DrawCheckBox(wxWindow
*win
,
449 GtkWidget
*button
= GetCheckButtonWidget();
451 // for reason why we do this, see DrawDropArrow
452 wxWindowDC
& wdc
= (wxWindowDC
&)dc
;
453 wxASSERT ( wdc
.IsKindOf(CLASSINFO(wxWindowDC
)) );
457 if ( flags
& wxCONTROL_PRESSED
)
458 state
= GTK_STATE_ACTIVE
;
459 else if ( flags
& wxCONTROL_DISABLED
)
460 state
= GTK_STATE_INSENSITIVE
;
461 else if ( flags
& wxCONTROL_CURRENT
)
462 state
= GTK_STATE_PRELIGHT
;
464 state
= GTK_STATE_NORMAL
;
471 flags
& wxCONTROL_CHECKED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
475 dc
.LogicalToDeviceX(rect
.x
)+2,
476 dc
.LogicalToDeviceY(rect
.y
)+3,
482 wxRendererGTK::DrawPushButton(wxWindow
*win
,
487 GtkWidget
*button
= GetButtonWidget();
489 // for reason why we do this, see DrawDropArrow
490 wxWindowDC
& wdc
= (wxWindowDC
&)dc
;
491 wxASSERT ( wdc
.IsKindOf(CLASSINFO(wxWindowDC
)) );
496 if ( flags
& wxCONTROL_PRESSED
)
497 state
= GTK_STATE_ACTIVE
;
498 else if ( flags
& wxCONTROL_DISABLED
)
499 state
= GTK_STATE_INSENSITIVE
;
500 else if ( flags
& wxCONTROL_CURRENT
)
501 state
= GTK_STATE_PRELIGHT
;
503 state
= GTK_STATE_NORMAL
;
510 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
514 rect
.x
, rect
.y
, rect
.width
, rect
.height
519 wxRendererGTK::DrawItemSelectionRect(wxWindow
*win
,
524 // for reason why we do this, see DrawDropArrow
525 wxWindowDC
& wdc
= (wxWindowDC
&)dc
;
526 wxASSERT ( wdc
.IsKindOf(CLASSINFO(wxWindowDC
)) );
529 if (flags
& wxCONTROL_SELECTED
)
531 if (flags
& wxCONTROL_FOCUSED
)
532 state
= GTK_STATE_SELECTED
;
534 state
= GTK_STATE_INSENSITIVE
;
536 gtk_paint_flat_box( win
->m_wxwindow
->style
,
537 GTK_PIZZA(win
->m_wxwindow
)->bin_window
,
543 dc
.LogicalToDeviceX(rect
.x
),
544 dc
.LogicalToDeviceY(rect
.y
),
549 if (flags
& wxCONTROL_CURRENT
)
551 dc
.SetPen( *wxBLACK_PEN
);
552 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
553 dc
.DrawRectangle( rect
);