]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/renderer.cpp
Tweaks for demos on MacOSX
[wxWidgets.git] / src / gtk / renderer.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/renderer.cpp
3 // Purpose: implementation of wxRendererNative for wxGTK
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>
9 // License: wxWindows licence
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
27 #include "wx/renderer.h"
28 #include <gtk/gtk.h>
29 #include "wx/gtk/win_gtk.h"
30
31 #include "wx/window.h"
32 #include "wx/dc.h"
33 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35
36 // RR: After a correction to the orientation of the sash
37 // this doesn't seem to be required anymore and it
38 // seems to confuse some themes so USE_ERASE_RECT=0
39 #define USE_ERASE_RECT 0
40
41 // ----------------------------------------------------------------------------
42 // wxRendererGTK: our wxRendererNative implementation
43 // ----------------------------------------------------------------------------
44
45 class WXDLLEXPORT wxRendererGTK : public wxDelegateRendererNative
46 {
47 public:
48 // draw the header control button (used by wxListCtrl)
49 virtual void DrawHeaderButton(wxWindow *win,
50 wxDC& dc,
51 const wxRect& rect,
52 int flags = 0);
53
54 // draw the expanded/collapsed icon for a tree control item
55 virtual void DrawTreeItemButton(wxWindow *win,
56 wxDC& dc,
57 const wxRect& rect,
58 int flags = 0);
59
60 virtual void DrawSplitterBorder(wxWindow *win,
61 wxDC& dc,
62 const wxRect& rect,
63 int flags = 0);
64 virtual void DrawSplitterSash(wxWindow *win,
65 wxDC& dc,
66 const wxSize& size,
67 wxCoord position,
68 wxOrientation orient,
69 int flags = 0);
70
71 virtual void DrawComboBoxDropButton(wxWindow *win,
72 wxDC& dc,
73 const wxRect& rect,
74 int flags = 0);
75
76 virtual void DrawDropArrow(wxWindow *win,
77 wxDC& dc,
78 const wxRect& rect,
79 int flags = 0);
80
81 virtual wxSplitterRenderParams GetSplitterParams(const wxWindow *win);
82
83 private:
84 // FIXME: shouldn't we destroy these windows somewhere?
85
86 // used by DrawHeaderButton and DrawComboBoxDropButton
87 static GtkWidget *GetButtonWidget();
88
89 // used by DrawTreeItemButton()
90 static GtkWidget *GetTreeWidget();
91 };
92
93 // ============================================================================
94 // implementation
95 // ============================================================================
96
97 /* static */
98 wxRendererNative& wxRendererNative::GetDefault()
99 {
100 static wxRendererGTK s_rendererGTK;
101
102 return s_rendererGTK;
103 }
104
105 // ----------------------------------------------------------------------------
106 // helper functions
107 // ----------------------------------------------------------------------------
108
109 GtkWidget *
110 wxRendererGTK::GetButtonWidget()
111 {
112 static GtkWidget *s_button = NULL;
113 static GtkWidget *s_window = NULL;
114
115 if ( !s_button )
116 {
117 s_window = gtk_window_new( GTK_WINDOW_POPUP );
118 gtk_widget_realize( s_window );
119 s_button = gtk_button_new();
120 gtk_container_add( GTK_CONTAINER(s_window), s_button );
121 gtk_widget_realize( s_button );
122 }
123
124 return s_button;
125 }
126
127 GtkWidget *
128 wxRendererGTK::GetTreeWidget()
129 {
130 static GtkWidget *s_tree = NULL;
131 static GtkWidget *s_window = NULL;
132
133 if ( !s_tree )
134 {
135 s_tree = gtk_tree_view_new();
136 s_window = gtk_window_new( GTK_WINDOW_POPUP );
137 gtk_widget_realize( s_window );
138 gtk_container_add( GTK_CONTAINER(s_window), s_tree );
139 gtk_widget_realize( s_tree );
140 }
141
142 return s_tree;
143 }
144
145 // ----------------------------------------------------------------------------
146 // list/tree controls drawing
147 // ----------------------------------------------------------------------------
148
149 void
150 wxRendererGTK::DrawHeaderButton(wxWindow *win,
151 wxDC& dc,
152 const wxRect& rect,
153 int flags)
154 {
155
156 GtkWidget *button = GetButtonWidget();
157
158 gtk_paint_box
159 (
160 button->style,
161 // FIXME: I suppose GTK_PIZZA(win->m_wxwindow)->bin_window doesn't work with wxMemoryDC.
162 // Maybe use code similar as in DrawComboBoxDropButton below?
163 GTK_PIZZA(win->m_wxwindow)->bin_window,
164 flags & wxCONTROL_DISABLED ? GTK_STATE_INSENSITIVE : GTK_STATE_NORMAL,
165 GTK_SHADOW_OUT,
166 NULL,
167 button,
168 "button",
169 dc.XLOG2DEV(rect.x) -1, rect.y -1, rect.width +2, rect.height +2
170 );
171 }
172
173 // draw a ">" or "v" button
174 void
175 wxRendererGTK::DrawTreeItemButton(wxWindow* win,
176 wxDC& dc, const wxRect& rect, int flags)
177 {
178 GtkWidget *tree = GetTreeWidget();
179
180 GtkStateType state;
181 if ( flags & wxCONTROL_CURRENT )
182 state = GTK_STATE_PRELIGHT;
183 else
184 state = GTK_STATE_NORMAL;
185
186 // VZ: I don't know how to get the size of the expander so as to centre it
187 // in the given rectangle, +2/3 below is just what looks good here...
188 gtk_paint_expander
189 (
190 tree->style,
191 GTK_PIZZA(win->m_wxwindow)->bin_window,
192 state,
193 NULL,
194 tree,
195 "treeview",
196 dc.LogicalToDeviceX(rect.x) + 2,
197 dc.LogicalToDeviceY(rect.y) + 3,
198 flags & wxCONTROL_EXPANDED ? GTK_EXPANDER_EXPANDED
199 : GTK_EXPANDER_COLLAPSED
200 );
201 }
202
203
204 // ----------------------------------------------------------------------------
205 // splitter sash drawing
206 // ----------------------------------------------------------------------------
207
208 static int GetGtkSplitterFullSize()
209 {
210 static GtkWidget *s_paned = NULL;
211 if (s_paned == NULL)
212 s_paned = gtk_vpaned_new();
213
214 gint handle_size;
215 gtk_widget_style_get (s_paned, "handle_size", &handle_size, NULL);
216
217 return handle_size;
218 }
219
220 wxSplitterRenderParams
221 wxRendererGTK::GetSplitterParams(const wxWindow *WXUNUSED(win))
222 {
223 // we don't draw any border, hence 0 for the second field
224 return wxSplitterRenderParams
225 (
226 GetGtkSplitterFullSize(),
227 0,
228 true // hot sensitive
229 );
230 }
231
232 void
233 wxRendererGTK::DrawSplitterBorder(wxWindow * WXUNUSED(win),
234 wxDC& WXUNUSED(dc),
235 const wxRect& WXUNUSED(rect),
236 int WXUNUSED(flags))
237 {
238 // nothing to do
239 }
240
241 void
242 wxRendererGTK::DrawSplitterSash(wxWindow *win,
243 wxDC& dc,
244 const wxSize& size,
245 wxCoord position,
246 wxOrientation orient,
247 int flags)
248 {
249 if ( !win->m_wxwindow->window )
250 {
251 // window not realized yet
252 return;
253 }
254
255 wxCoord full_size = GetGtkSplitterFullSize();
256
257 // are we drawing vertical or horizontal splitter?
258 const bool isVert = orient == wxVERTICAL;
259
260 GdkRectangle rect;
261 #if USE_ERASE_RECT
262 GdkRectangle erase_rect;
263 #endif
264
265 if ( isVert )
266 {
267 int h = win->GetClientSize().GetHeight();
268
269 rect.x = position;
270 rect.y = 0;
271 rect.width = full_size;
272 rect.height = h;
273
274 #if USE_ERASE_RECT
275 erase_rect.x = position;
276 erase_rect.y = 0;
277 erase_rect.width = full_size;
278 erase_rect.height = h;
279 #endif
280 }
281 else // horz
282 {
283 int w = win->GetClientSize().GetWidth();
284
285 rect.x = 0;
286 rect.y = position;
287 rect.height = full_size;
288 rect.width = w;
289
290 #if USE_ERASE_RECT
291 erase_rect.y = position;
292 erase_rect.x = 0;
293 erase_rect.height = full_size;
294 erase_rect.width = w;
295 #endif
296 }
297
298 #if USE_ERASE_RECT
299 // we must erase everything first, otherwise the garbage
300 // from the old sash is left when dragging it
301 gtk_paint_flat_box
302 (
303 win->m_wxwindow->style,
304 GTK_PIZZA(win->m_wxwindow)->bin_window,
305 GTK_STATE_NORMAL,
306 GTK_SHADOW_NONE,
307 NULL,
308 win->m_wxwindow,
309 (char *)"viewportbin", // const_cast
310 erase_rect.x,
311 erase_rect.y,
312 erase_rect.width,
313 erase_rect.height
314 );
315 #endif
316
317 gtk_paint_handle
318 (
319 win->m_wxwindow->style,
320 GTK_PIZZA(win->m_wxwindow)->bin_window,
321 flags & wxCONTROL_CURRENT ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
322 GTK_SHADOW_NONE,
323 NULL /* no clipping */,
324 win->m_wxwindow,
325 "paned",
326 rect.x,
327 rect.y,
328 rect.width,
329 rect.height,
330 isVert ? GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL
331 );
332 }
333
334 void
335 wxRendererGTK::DrawDropArrow(wxWindow *win,
336 wxDC& dc,
337 const wxRect& rect,
338 int flags)
339 {
340 GtkWidget *button = GetButtonWidget();
341
342 // If we give GTK_PIZZA(win->m_wxwindow)->bin_window as
343 // a window for gtk_paint_xxx function, then it won't
344 // work for wxMemoryDC. So that is why we assume wxDC
345 // is wxWindowDC (wxClientDC, wxMemoryDC and wxPaintDC
346 // are derived from it) and use its m_window.
347 wxWindowDC& wdc = (wxWindowDC&)dc;
348
349 // only doing debug-time checking here (it should
350 // probably be enough)
351 wxASSERT ( wdc.IsKindOf(CLASSINFO(wxWindowDC)) );
352
353 // draw arrow so that there is even space horizontally
354 // on both sides
355 int arrowX = rect.width/4 + 1;
356 int arrowWidth = rect.width - (arrowX*2);
357
358 // scale arrow's height accoording to the width
359 int arrowHeight = rect.width/3;
360 int arrowY = (rect.height-arrowHeight)/2 +
361 ((rect.height-arrowHeight) & 1);
362
363 GtkStateType state;
364
365 if ( flags & wxCONTROL_PRESSED )
366 state = GTK_STATE_ACTIVE;
367 else if ( flags & wxCONTROL_DISABLED )
368 state = GTK_STATE_INSENSITIVE;
369 else if ( flags & wxCONTROL_CURRENT )
370 state = GTK_STATE_PRELIGHT;
371 else
372 state = GTK_STATE_NORMAL;
373
374 // draw arrow on button
375 gtk_paint_arrow
376 (
377 button->style,
378 wdc.m_window,
379 state,
380 flags & wxCONTROL_PRESSED ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
381 NULL,
382 button,
383 "arrow",
384 GTK_ARROW_DOWN,
385 FALSE,
386 rect.x + arrowX,
387 rect.y + arrowY,
388 arrowWidth,
389 arrowHeight
390 );
391 }
392
393 void
394 wxRendererGTK::DrawComboBoxDropButton(wxWindow *win,
395 wxDC& dc,
396 const wxRect& rect,
397 int flags)
398 {
399 GtkWidget *button = GetButtonWidget();
400
401 // for reason why we do this, see DrawDropArrow
402 wxWindowDC& wdc = (wxWindowDC&)dc;
403 wxASSERT ( wdc.IsKindOf(CLASSINFO(wxWindowDC)) );
404
405 // draw button
406 GtkStateType state;
407
408 if ( flags & wxCONTROL_PRESSED )
409 state = GTK_STATE_ACTIVE;
410 else if ( flags & wxCONTROL_DISABLED )
411 state = GTK_STATE_INSENSITIVE;
412 else if ( flags & wxCONTROL_CURRENT )
413 state = GTK_STATE_PRELIGHT;
414 else
415 state = GTK_STATE_NORMAL;
416
417 gtk_paint_box
418 (
419 button->style,
420 wdc.m_window,
421 state,
422 flags & wxCONTROL_PRESSED ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
423 NULL,
424 button,
425 "button",
426 rect.x, rect.y, rect.width, rect.height
427 );
428
429 // draw arrow on button
430 DrawDropArrow(win,dc,rect,flags);
431
432 }
433