]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/renderer.cpp
Improve validation of wxCheckBox styles.
[wxWidgets.git] / src / gtk1 / renderer.cpp
CommitLineData
9c7f49f5 1///////////////////////////////////////////////////////////////////////////////
cdccdfab 2// Name: src/gtk1/renderer.cpp
38c4cb6a 3// Purpose: implementation of wxRendererNative for wxGTK
9c7f49f5
VZ
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 20.07.2003
7// RCS-ID: $Id$
8// Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
526954c5 9// Licence: wxWindows licence
9c7f49f5
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// for compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
e1bf3ad3 27#include "wx/renderer.h"
cdccdfab
WS
28
29#ifndef WX_PRECOMP
30 #include "wx/window.h"
da80ae71 31 #include "wx/dc.h"
cdccdfab
WS
32#endif
33
9c7f49f5 34#include <gtk/gtk.h>
3cbab641 35#include "wx/gtk1/win_gtk.h"
10d30222 36#include "wx/gtk1/dcclient.h"
9c7f49f5 37
91af0895
WS
38// RR: After a correction to the orientation of the sash
39// this doesn't seem to be required anymore and it
40// seems to confuse some themes so USE_ERASE_RECT=0
41#define USE_ERASE_RECT 0
42
9c7f49f5 43// ----------------------------------------------------------------------------
38c4cb6a 44// wxRendererGTK: our wxRendererNative implementation
9c7f49f5
VZ
45// ----------------------------------------------------------------------------
46
38c4cb6a 47class WXDLLEXPORT wxRendererGTK : public wxDelegateRendererNative
9c7f49f5
VZ
48{
49public:
50 // draw the header control button (used by wxListCtrl)
87ebb0b7
VZ
51 virtual int DrawHeaderButton(wxWindow *win,
52 wxDC& dc,
53 const wxRect& rect,
54 int flags = 0,
55 wxHeaderSortIconType sortArrow = wxHDR_SORT_ICON_NONE,
56 wxHeaderButtonParams* params=NULL);
9c7f49f5 57
d16cf3cd
VZ
58 virtual void DrawSplitterBorder(wxWindow *win,
59 wxDC& dc,
af99040c
VZ
60 const wxRect& rect,
61 int flags = 0);
95155e75
VZ
62 virtual void DrawSplitterSash(wxWindow *win,
63 wxDC& dc,
64 const wxSize& size,
d16cf3cd 65 wxCoord position,
af99040c
VZ
66 wxOrientation orient,
67 int flags = 0);
d16cf3cd 68
38511687
VZ
69 virtual void DrawComboBoxDropButton(wxWindow *win,
70 wxDC& dc,
71 const wxRect& rect,
72 int flags = 0);
73
4c85ab75
VZ
74 virtual void DrawDropArrow(wxWindow *win,
75 wxDC& dc,
76 const wxRect& rect,
77 int flags = 0);
78
af99040c 79 virtual wxSplitterRenderParams GetSplitterParams(const wxWindow *win);
e1befae3
VZ
80
81private:
bc13e772
VZ
82 // FIXME: shouldn't we destroy these windows somewhere?
83
e1befae3 84 // used by DrawHeaderButton and DrawComboBoxDropButton
bc13e772 85 static GtkWidget *GetButtonWidget();
9c7f49f5
VZ
86};
87
88// ============================================================================
89// implementation
90// ============================================================================
91
92/* static */
f0244295 93wxRendererNative& wxRendererNative::GetDefault()
9c7f49f5
VZ
94{
95 static wxRendererGTK s_rendererGTK;
96
97 return s_rendererGTK;
98}
99
a4622f29 100// ----------------------------------------------------------------------------
bc13e772 101// helper functions
a4622f29
VZ
102// ----------------------------------------------------------------------------
103
bc13e772
VZ
104GtkWidget *
105wxRendererGTK::GetButtonWidget()
106{
107 static GtkWidget *s_button = NULL;
108 static GtkWidget *s_window = NULL;
a4622f29 109
bc13e772
VZ
110 if ( !s_button )
111 {
112 s_window = gtk_window_new( GTK_WINDOW_POPUP );
113 gtk_widget_realize( s_window );
114 s_button = gtk_button_new();
115 gtk_container_add( GTK_CONTAINER(s_window), s_button );
116 gtk_widget_realize( s_button );
117 }
118
119 return s_button;
120}
121
d16cf3cd
VZ
122// ----------------------------------------------------------------------------
123// list/tree controls drawing
124// ----------------------------------------------------------------------------
125
87ebb0b7 126int
9c7f49f5
VZ
127wxRendererGTK::DrawHeaderButton(wxWindow *win,
128 wxDC& dc,
129 const wxRect& rect,
d1b8a743
VZ
130 int flags,
131 wxHeaderSortIconType WXUNUSED(sortArrow),
132 wxHeaderButtonParams* WXUNUSED(params))
9c7f49f5 133{
9b311923 134
bc13e772 135 GtkWidget *button = GetButtonWidget();
9b311923 136
9c7f49f5
VZ
137 gtk_paint_box
138 (
bc13e772 139 button->style,
a4622f29
VZ
140 // FIXME: I suppose GTK_PIZZA(win->m_wxwindow)->bin_window doesn't work with wxMemoryDC.
141 // Maybe use code similar as in DrawComboBoxDropButton below?
9c7f49f5
VZ
142 GTK_PIZZA(win->m_wxwindow)->bin_window,
143 flags & wxCONTROL_DISABLED ? GTK_STATE_INSENSITIVE : GTK_STATE_NORMAL,
144 GTK_SHADOW_OUT,
38511687 145 NULL,
bc13e772 146 button,
9b311923 147 "button",
10d30222 148 dc.LogicalToDeviceX(rect.x) -1, rect.y -1, rect.width +2, rect.height +2
9c7f49f5 149 );
87ebb0b7
VZ
150
151 return rect.width + 2;
9c7f49f5
VZ
152}
153
d16cf3cd
VZ
154// ----------------------------------------------------------------------------
155// splitter sash drawing
156// ----------------------------------------------------------------------------
157
3cbab641
MR
158// the full sash width (should be even)
159static const wxCoord SASH_SIZE = 8;
af99040c 160
3cbab641
MR
161// margin around the sash
162static const wxCoord SASH_MARGIN = 2;
d16cf3cd 163
38418827
RR
164static int GetGtkSplitterFullSize()
165{
38418827 166 return SASH_SIZE + SASH_MARGIN;
38418827
RR
167}
168
af99040c 169wxSplitterRenderParams
38418827 170wxRendererGTK::GetSplitterParams(const wxWindow *WXUNUSED(win))
d16cf3cd 171{
af99040c
VZ
172 // we don't draw any border, hence 0 for the second field
173 return wxSplitterRenderParams
174 (
38418827 175 GetGtkSplitterFullSize(),
af99040c 176 0,
af99040c 177 false // not
af99040c 178 );
d16cf3cd
VZ
179}
180
181void
182wxRendererGTK::DrawSplitterBorder(wxWindow * WXUNUSED(win),
183 wxDC& WXUNUSED(dc),
af99040c
VZ
184 const wxRect& WXUNUSED(rect),
185 int WXUNUSED(flags))
d16cf3cd
VZ
186{
187 // nothing to do
188}
95155e75 189
95155e75
VZ
190void
191wxRendererGTK::DrawSplitterSash(wxWindow *win,
89954433 192 wxDC& WXUNUSED(dc),
95155e75 193 const wxSize& size,
d16cf3cd 194 wxCoord position,
af99040c 195 wxOrientation orient,
3cbab641 196 int WXUNUSED(flags))
95155e75
VZ
197{
198 if ( !win->m_wxwindow->window )
199 {
0100b858 200 // window not realized yet
95155e75
VZ
201 return;
202 }
91af0895 203
38418827 204 wxCoord full_size = GetGtkSplitterFullSize();
95155e75 205
d16cf3cd
VZ
206 // are we drawing vertical or horizontal splitter?
207 const bool isVert = orient == wxVERTICAL;
208
d16cf3cd 209 GdkRectangle rect;
91af0895 210#if USE_ERASE_RECT
35468934 211 GdkRectangle erase_rect;
91af0895
WS
212#endif
213
d16cf3cd
VZ
214 if ( isVert )
215 {
0100b858 216 int h = win->GetClientSize().GetHeight();
e1befae3 217
d16cf3cd 218 rect.x = position;
0100b858 219 rect.y = 0;
38418827 220 rect.width = full_size;
0100b858 221 rect.height = h;
e1befae3 222
91af0895 223#if USE_ERASE_RECT
35468934
RR
224 erase_rect.x = position;
225 erase_rect.y = 0;
38418827 226 erase_rect.width = full_size;
35468934 227 erase_rect.height = h;
91af0895 228#endif
d16cf3cd
VZ
229 }
230 else // horz
231 {
0100b858 232 int w = win->GetClientSize().GetWidth();
e1befae3 233
0100b858 234 rect.x = 0;
d16cf3cd 235 rect.y = position;
38418827 236 rect.height = full_size;
0100b858 237 rect.width = w;
e1befae3 238
91af0895 239#if USE_ERASE_RECT
35468934
RR
240 erase_rect.y = position;
241 erase_rect.x = 0;
38418827 242 erase_rect.height = full_size;
35468934 243 erase_rect.width = w;
91af0895 244#endif
d16cf3cd
VZ
245 }
246
91af0895 247#if USE_ERASE_RECT
ab5ea030
RR
248 // we must erase everything first, otherwise the garbage
249 // from the old sash is left when dragging it
35468934
RR
250 gtk_paint_flat_box
251 (
252 win->m_wxwindow->style,
253 GTK_PIZZA(win->m_wxwindow)->bin_window,
254 GTK_STATE_NORMAL,
255 GTK_SHADOW_NONE,
256 NULL,
257 win->m_wxwindow,
ab5ea030 258 (char *)"viewportbin", // const_cast
35468934
RR
259 erase_rect.x,
260 erase_rect.y,
261 erase_rect.width,
262 erase_rect.height
263 );
ab5ea030 264#endif
35468934 265
d16cf3cd
VZ
266
267 // leave some margin before sash itself
268 position += SASH_MARGIN / 2;
269
270 // and finally draw it using GTK paint functions
271 typedef void (*GtkPaintLineFunc)(GtkStyle *, GdkWindow *,
272 GtkStateType,
273 GdkRectangle *, GtkWidget *,
ef9bfb71 274 gchar *,
ef9bfb71 275 gint, gint, gint);
d16cf3cd
VZ
276
277 GtkPaintLineFunc func = isVert ? gtk_paint_vline : gtk_paint_hline;
278
279 (*func)
95155e75
VZ
280 (
281 win->m_wxwindow->style,
d16cf3cd 282 GTK_PIZZA(win->m_wxwindow)->bin_window,
95155e75 283 GTK_STATE_NORMAL,
d16cf3cd 284 NULL,
95155e75 285 win->m_wxwindow,
d16cf3cd
VZ
286 (char *)"paned", // const_cast
287 0, isVert ? size.y : size.x, position + SASH_SIZE / 2 - 1
95155e75
VZ
288 );
289
290 gtk_paint_box
291 (
292 win->m_wxwindow->style,
d16cf3cd 293 GTK_PIZZA(win->m_wxwindow)->bin_window,
95155e75
VZ
294 GTK_STATE_NORMAL,
295 GTK_SHADOW_OUT,
d3b9f782 296 NULL,
95155e75
VZ
297 win->m_wxwindow,
298 (char *)"paned", // const_cast
d16cf3cd
VZ
299 isVert ? position : size.x - 2*SASH_SIZE,
300 isVert ? size.y - 2*SASH_SIZE : position,
301 SASH_SIZE, SASH_SIZE
95155e75
VZ
302 );
303}
304
4c85ab75 305void
89954433 306wxRendererGTK::DrawDropArrow(wxWindow *WXUNUSED(win),
4c85ab75
VZ
307 wxDC& dc,
308 const wxRect& rect,
309 int flags)
38511687 310{
bc13e772 311 GtkWidget *button = GetButtonWidget();
38511687 312
4c85ab75
VZ
313 // If we give GTK_PIZZA(win->m_wxwindow)->bin_window as
314 // a window for gtk_paint_xxx function, then it won't
315 // work for wxMemoryDC. So that is why we assume wxDC
316 // is wxWindowDC (wxClientDC, wxMemoryDC and wxPaintDC
317 // are derived from it) and use its m_window.
10d30222
VZ
318 wxWindowDCImpl * const impl = wxDynamicCast(dc.GetImpl(), wxWindowDCImpl);
319 wxCHECK_RET( impl, "must have a window DC" );
320
321 GdkWindow* gdk_window = impl->GetGDKWindow();
a4622f29 322
4c85ab75
VZ
323 // draw arrow so that there is even space horizontally
324 // on both sides
325 int arrowX = rect.width/4 + 1;
326 int arrowWidth = rect.width - (arrowX*2);
327
328 // scale arrow's height accoording to the width
329 int arrowHeight = rect.width/3;
330 int arrowY = (rect.height-arrowHeight)/2 +
331 ((rect.height-arrowHeight) & 1);
332
e1befae3 333 GtkStateType state;
a4622f29 334
3203621a
JS
335 if ( flags & wxCONTROL_PRESSED )
336 state = GTK_STATE_ACTIVE;
a4622f29
VZ
337 else if ( flags & wxCONTROL_DISABLED )
338 state = GTK_STATE_INSENSITIVE;
3203621a
JS
339 else if ( flags & wxCONTROL_CURRENT )
340 state = GTK_STATE_PRELIGHT;
e1befae3
VZ
341 else
342 state = GTK_STATE_NORMAL;
a4622f29 343
a4622f29 344 // draw arrow on button
a4622f29
VZ
345 gtk_paint_arrow
346 (
bc13e772 347 button->style,
2e992e06 348 gdk_window,
a4622f29 349 state,
e1befae3 350 flags & wxCONTROL_PRESSED ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
a4622f29 351 NULL,
bc13e772 352 button,
a4622f29
VZ
353 "arrow",
354 GTK_ARROW_DOWN,
a8ac548e 355 FALSE,
4c85ab75
VZ
356 rect.x + arrowX,
357 rect.y + arrowY,
358 arrowWidth,
359 arrowHeight
a4622f29 360 );
38511687
VZ
361}
362
4c85ab75
VZ
363void
364wxRendererGTK::DrawComboBoxDropButton(wxWindow *win,
365 wxDC& dc,
366 const wxRect& rect,
367 int flags)
368{
369 GtkWidget *button = GetButtonWidget();
370
371 // for reason why we do this, see DrawDropArrow
10d30222
VZ
372 wxWindowDCImpl * const impl = wxDynamicCast(dc.GetImpl(), wxWindowDCImpl);
373 wxCHECK_RET( impl, "must have a window DC" );
374
375 GdkWindow* gdk_window = impl->GetGDKWindow();
4c85ab75
VZ
376
377 // draw button
378 GtkStateType state;
379
3203621a
JS
380 if ( flags & wxCONTROL_PRESSED )
381 state = GTK_STATE_ACTIVE;
4c85ab75
VZ
382 else if ( flags & wxCONTROL_DISABLED )
383 state = GTK_STATE_INSENSITIVE;
3203621a
JS
384 else if ( flags & wxCONTROL_CURRENT )
385 state = GTK_STATE_PRELIGHT;
4c85ab75
VZ
386 else
387 state = GTK_STATE_NORMAL;
388
389 gtk_paint_box
390 (
391 button->style,
2e992e06 392 gdk_window,
4c85ab75
VZ
393 state,
394 flags & wxCONTROL_PRESSED ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
395 NULL,
396 button,
397 "button",
398 rect.x, rect.y, rect.width, rect.height
399 );
400
401 // draw arrow on button
402 DrawDropArrow(win,dc,rect,flags);
403
404}