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();
196 // FIXME: I suppose GTK_PIZZA(win->m_wxwindow)->bin_window doesn't work with wxMemoryDC.
197 // Maybe use code similar as in DrawPushButton below?
198 GTK_PIZZA(win
->m_wxwindow
)->bin_window
,
199 flags
& wxCONTROL_DISABLED
? GTK_STATE_INSENSITIVE
: GTK_STATE_NORMAL
,
204 dc
.LogicalToDeviceX(rect
.x
), rect
.y
, rect
.width
, rect
.height
207 DrawHeaderButtonContents(win
, dc
, rect
, flags
, params
);
210 // draw a ">" or "v" button
212 wxRendererGTK::DrawTreeItemButton(wxWindow
* win
,
213 wxDC
& dc
, const wxRect
& rect
, int flags
)
215 GtkWidget
*tree
= GetTreeWidget();
218 if ( flags
& wxCONTROL_CURRENT
)
219 state
= GTK_STATE_PRELIGHT
;
221 state
= GTK_STATE_NORMAL
;
224 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
227 // VZ: I don't know how to get the size of the expander so as to centre it
228 // in the given rectangle, +2/3 below is just what looks good here...
232 GTK_PIZZA(win
->m_wxwindow
)->bin_window
,
237 dc
.LogicalToDeviceX(rect
.x
) + 2 - x_diff
,
238 dc
.LogicalToDeviceY(rect
.y
) + 3,
239 flags
& wxCONTROL_EXPANDED
? GTK_EXPANDER_EXPANDED
240 : GTK_EXPANDER_COLLAPSED
245 // ----------------------------------------------------------------------------
246 // splitter sash drawing
247 // ----------------------------------------------------------------------------
249 static int GetGtkSplitterFullSize()
251 static GtkWidget
*s_paned
= NULL
;
253 s_paned
= gtk_vpaned_new();
256 gtk_widget_style_get (s_paned
, "handle_size", &handle_size
, NULL
);
261 wxSplitterRenderParams
262 wxRendererGTK::GetSplitterParams(const wxWindow
*WXUNUSED(win
))
264 // we don't draw any border, hence 0 for the second field
265 return wxSplitterRenderParams
267 GetGtkSplitterFullSize(),
269 true // hot sensitive
274 wxRendererGTK::DrawSplitterBorder(wxWindow
* WXUNUSED(win
),
276 const wxRect
& WXUNUSED(rect
),
283 wxRendererGTK::DrawSplitterSash(wxWindow
*win
,
287 wxOrientation orient
,
290 if ( !win
->m_wxwindow
->window
)
292 // window not realized yet
296 wxCoord full_size
= GetGtkSplitterFullSize();
298 // are we drawing vertical or horizontal splitter?
299 const bool isVert
= orient
== wxVERTICAL
;
305 int h
= win
->GetClientSize().GetHeight();
309 rect
.width
= full_size
;
314 int w
= win
->GetClientSize().GetWidth();
318 rect
.height
= full_size
;
323 if (win
->GetLayoutDirection() == wxLayout_RightToLeft
)
328 win
->m_wxwindow
->style
,
329 GTK_PIZZA(win
->m_wxwindow
)->bin_window
,
330 flags
& wxCONTROL_CURRENT
? GTK_STATE_PRELIGHT
: GTK_STATE_NORMAL
,
332 NULL
/* no clipping */,
335 dc
.LogicalToDeviceX(rect
.x
) - x_diff
,
336 dc
.LogicalToDeviceY(rect
.y
),
339 isVert
? GTK_ORIENTATION_VERTICAL
: GTK_ORIENTATION_HORIZONTAL
344 wxRendererGTK::DrawDropArrow(wxWindow
*win
,
349 GtkWidget
*button
= GetButtonWidget();
351 // If we give GTK_PIZZA(win->m_wxwindow)->bin_window as
352 // a window for gtk_paint_xxx function, then it won't
353 // work for wxMemoryDC. So that is why we assume wxDC
354 // is wxWindowDC (wxClientDC, wxMemoryDC and wxPaintDC
355 // are derived from it) and use its m_window.
356 wxWindowDC
& wdc
= (wxWindowDC
&)dc
;
358 // only doing debug-time checking here (it should
359 // probably be enough)
360 wxASSERT ( wdc
.IsKindOf(CLASSINFO(wxWindowDC
)) );
362 // draw arrow so that there is even space horizontally
364 int arrowX
= rect
.width
/4 + 1;
365 int arrowWidth
= rect
.width
- (arrowX
*2);
367 // scale arrow's height accoording to the width
368 int arrowHeight
= rect
.width
/3;
369 int arrowY
= (rect
.height
-arrowHeight
)/2 +
370 ((rect
.height
-arrowHeight
) & 1);
374 if ( flags
& wxCONTROL_PRESSED
)
375 state
= GTK_STATE_ACTIVE
;
376 else if ( flags
& wxCONTROL_DISABLED
)
377 state
= GTK_STATE_INSENSITIVE
;
378 else if ( flags
& wxCONTROL_CURRENT
)
379 state
= GTK_STATE_PRELIGHT
;
381 state
= GTK_STATE_NORMAL
;
383 // draw arrow on button
389 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
403 wxRendererGTK::DrawComboBoxDropButton(wxWindow
*win
,
408 DrawPushButton(win
,dc
,rect
,flags
);
409 DrawDropArrow(win
,dc
,rect
);
413 wxRendererGTK::DrawCheckBox(wxWindow
*win
,
418 GtkWidget
*button
= GetCheckButtonWidget();
420 // for reason why we do this, see DrawDropArrow
421 wxWindowDC
& wdc
= (wxWindowDC
&)dc
;
422 wxASSERT ( wdc
.IsKindOf(CLASSINFO(wxWindowDC
)) );
426 if ( flags
& wxCONTROL_PRESSED
)
427 state
= GTK_STATE_ACTIVE
;
428 else if ( flags
& wxCONTROL_DISABLED
)
429 state
= GTK_STATE_INSENSITIVE
;
430 else if ( flags
& wxCONTROL_CURRENT
)
431 state
= GTK_STATE_PRELIGHT
;
433 state
= GTK_STATE_NORMAL
;
440 flags
& wxCONTROL_CHECKED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
444 dc
.LogicalToDeviceX(rect
.x
)+2,
445 dc
.LogicalToDeviceY(rect
.y
)+3,
451 wxRendererGTK::DrawPushButton(wxWindow
*win
,
456 GtkWidget
*button
= GetButtonWidget();
458 // for reason why we do this, see DrawDropArrow
459 wxWindowDC
& wdc
= (wxWindowDC
&)dc
;
460 wxASSERT ( wdc
.IsKindOf(CLASSINFO(wxWindowDC
)) );
465 if ( flags
& wxCONTROL_PRESSED
)
466 state
= GTK_STATE_ACTIVE
;
467 else if ( flags
& wxCONTROL_DISABLED
)
468 state
= GTK_STATE_INSENSITIVE
;
469 else if ( flags
& wxCONTROL_CURRENT
)
470 state
= GTK_STATE_PRELIGHT
;
472 state
= GTK_STATE_NORMAL
;
479 flags
& wxCONTROL_PRESSED
? GTK_SHADOW_IN
: GTK_SHADOW_OUT
,
483 rect
.x
, rect
.y
, rect
.width
, rect
.height
488 wxRendererGTK::DrawItemSelectionRect(wxWindow
*win
,
494 if (flags
& wxCONTROL_SELECTED
)
496 if (flags
& wxCONTROL_FOCUSED
)
497 state
= GTK_STATE_SELECTED
;
499 state
= GTK_STATE_INSENSITIVE
;
501 gtk_paint_flat_box( win
->m_wxwindow
->style
,
502 GTK_PIZZA(win
->m_wxwindow
)->bin_window
,
508 dc
.LogicalToDeviceX(rect
.x
),
509 dc
.LogicalToDeviceY(rect
.y
),
514 if (flags
& wxCONTROL_CURRENT
)
516 dc
.SetPen( *wxBLACK_PEN
);
517 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
518 dc
.DrawRectangle( rect
);