]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/renderer.cpp
correction to last commit: don't test unsetenv() return value, it's void under Darwin
[wxWidgets.git] / src / gtk / renderer.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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
29 #ifndef WX_PRECOMP
30 #include "wx/window.h"
31 #include "wx/dcclient.h"
32 #include "wx/settings.h"
33 #endif
34
35 #include <gtk/gtk.h>
36 #include "wx/gtk/win_gtk.h"
37
38 // ----------------------------------------------------------------------------
39 // wxRendererGTK: our wxRendererNative implementation
40 // ----------------------------------------------------------------------------
41
42 class WXDLLEXPORT wxRendererGTK : public wxDelegateRendererNative
43 {
44 public:
45 // draw the header control button (used by wxListCtrl)
46 virtual int DrawHeaderButton(wxWindow *win,
47 wxDC& dc,
48 const wxRect& rect,
49 int flags = 0,
50 wxHeaderSortIconType sortArrow = wxHDR_SORT_ICON_NONE,
51 wxHeaderButtonParams* params = NULL);
52
53 // draw the expanded/collapsed icon for a tree control item
54 virtual void DrawTreeItemButton(wxWindow *win,
55 wxDC& dc,
56 const wxRect& rect,
57 int flags = 0);
58
59 virtual void DrawSplitterBorder(wxWindow *win,
60 wxDC& dc,
61 const wxRect& rect,
62 int flags = 0);
63 virtual void DrawSplitterSash(wxWindow *win,
64 wxDC& dc,
65 const wxSize& size,
66 wxCoord position,
67 wxOrientation orient,
68 int flags = 0);
69
70 virtual void DrawComboBoxDropButton(wxWindow *win,
71 wxDC& dc,
72 const wxRect& rect,
73 int flags = 0);
74
75 virtual void DrawDropArrow(wxWindow *win,
76 wxDC& dc,
77 const wxRect& rect,
78 int flags = 0);
79
80 virtual void DrawCheckBox(wxWindow *win,
81 wxDC& dc,
82 const wxRect& rect,
83 int flags = 0);
84
85 virtual void DrawPushButton(wxWindow *win,
86 wxDC& dc,
87 const wxRect& rect,
88 int flags = 0);
89
90 virtual void DrawItemSelectionRect(wxWindow *win,
91 wxDC& dc,
92 const wxRect& rect,
93 int flags = 0);
94
95 virtual wxSplitterRenderParams GetSplitterParams(const wxWindow *win);
96
97 private:
98 // FIXME: shouldn't we destroy these windows somewhere?
99
100 // used by DrawPushButton and DrawDropArrow
101 static GtkWidget *GetButtonWidget();
102
103 // used by DrawTreeItemButton()
104 static GtkWidget *GetTreeWidget();
105
106 // used by DrawCheckBox()
107 static GtkWidget *GetCheckButtonWidget();
108
109 // Used by DrawHeaderButton
110 static GtkWidget *GetHeaderButtonWidget();
111 };
112
113 // ============================================================================
114 // implementation
115 // ============================================================================
116
117 /* static */
118 wxRendererNative& wxRendererNative::GetDefault()
119 {
120 static wxRendererGTK s_rendererGTK;
121
122 return s_rendererGTK;
123 }
124
125 // ----------------------------------------------------------------------------
126 // helper functions
127 // ----------------------------------------------------------------------------
128
129 GtkWidget *
130 wxRendererGTK::GetButtonWidget()
131 {
132 static GtkWidget *s_button = NULL;
133 static GtkWidget *s_window = NULL;
134
135 if ( !s_button )
136 {
137 s_window = gtk_window_new( GTK_WINDOW_POPUP );
138 gtk_widget_realize( s_window );
139 s_button = gtk_button_new();
140 gtk_container_add( GTK_CONTAINER(s_window), s_button );
141 gtk_widget_realize( s_button );
142 }
143
144 return s_button;
145 }
146
147 GtkWidget *
148 wxRendererGTK::GetCheckButtonWidget()
149 {
150 static GtkWidget *s_button = NULL;
151 static GtkWidget *s_window = NULL;
152
153 if ( !s_button )
154 {
155 s_window = gtk_window_new( GTK_WINDOW_POPUP );
156 gtk_widget_realize( s_window );
157 s_button = gtk_check_button_new();
158 gtk_container_add( GTK_CONTAINER(s_window), s_button );
159 gtk_widget_realize( s_button );
160 }
161
162 return s_button;
163 }
164
165 GtkWidget *
166 wxRendererGTK::GetTreeWidget()
167 {
168 static GtkWidget *s_tree = NULL;
169 static GtkWidget *s_window = NULL;
170
171 if ( !s_tree )
172 {
173 s_tree = gtk_tree_view_new();
174 s_window = gtk_window_new( GTK_WINDOW_POPUP );
175 gtk_widget_realize( s_window );
176 gtk_container_add( GTK_CONTAINER(s_window), s_tree );
177 gtk_widget_realize( s_tree );
178 }
179
180 return s_tree;
181 }
182
183
184 // This one just gets the button used by the column header. Although it's
185 // still a gtk_button the themes will typically differentiate and draw them
186 // differently if the button is in a treeview.
187 GtkWidget *
188 wxRendererGTK::GetHeaderButtonWidget()
189 {
190 static GtkWidget *s_button = NULL;
191
192 if ( !s_button )
193 {
194 // Get the dummy tree widget, give it a column, and then use the
195 // widget in the column header for the rendering code.
196 GtkWidget* treewidget = GetTreeWidget();
197 GtkTreeViewColumn* column = gtk_tree_view_column_new();
198 gtk_tree_view_append_column(GTK_TREE_VIEW(treewidget), column);
199 s_button = column->button;
200 }
201
202 return s_button;
203 }
204
205 // ----------------------------------------------------------------------------
206 // list/tree controls drawing
207 // ----------------------------------------------------------------------------
208
209 int
210 wxRendererGTK::DrawHeaderButton(wxWindow *win,
211 wxDC& dc,
212 const wxRect& rect,
213 int flags,
214 wxHeaderSortIconType sortArrow,
215 wxHeaderButtonParams* params)
216 {
217
218 GtkWidget *button = GetHeaderButtonWidget();
219
220 GdkWindow* gdk_window = dc.GetGDKWindow();
221 wxASSERT_MSG( gdk_window,
222 wxT("cannot use wxRendererNative on wxDC of this type") );
223
224 int x_diff = 0;
225 if (win->GetLayoutDirection() == wxLayout_RightToLeft)
226 x_diff = rect.width;
227
228 gtk_paint_box
229 (
230 button->style,
231 gdk_window,
232 flags & wxCONTROL_DISABLED ? GTK_STATE_INSENSITIVE : GTK_STATE_NORMAL,
233 GTK_SHADOW_OUT,
234 NULL,
235 button,
236 "button",
237 dc.LogicalToDeviceX(rect.x) - x_diff, rect.y, rect.width, rect.height
238 );
239
240 return DrawHeaderButtonContents(win, dc, rect, flags, sortArrow, params);
241 }
242
243 // draw a ">" or "v" button
244 void
245 wxRendererGTK::DrawTreeItemButton(wxWindow* win,
246 wxDC& dc, const wxRect& rect, int flags)
247 {
248 GtkWidget *tree = GetTreeWidget();
249
250 GdkWindow* gdk_window = dc.GetGDKWindow();
251 wxASSERT_MSG( gdk_window,
252 wxT("cannot use wxRendererNative on wxDC of this type") );
253
254 GtkStateType state;
255 if ( flags & wxCONTROL_CURRENT )
256 state = GTK_STATE_PRELIGHT;
257 else
258 state = GTK_STATE_NORMAL;
259
260 int x_diff = 0;
261 if (win->GetLayoutDirection() == wxLayout_RightToLeft)
262 x_diff = rect.width;
263
264 // VZ: I don't know how to get the size of the expander so as to centre it
265 // in the given rectangle, +2/3 below is just what looks good here...
266 gtk_paint_expander
267 (
268 tree->style,
269 gdk_window,
270 state,
271 NULL,
272 tree,
273 "treeview",
274 dc.LogicalToDeviceX(rect.x) + 6 - x_diff,
275 dc.LogicalToDeviceY(rect.y) + 3,
276 flags & wxCONTROL_EXPANDED ? GTK_EXPANDER_EXPANDED
277 : GTK_EXPANDER_COLLAPSED
278 );
279 }
280
281
282 // ----------------------------------------------------------------------------
283 // splitter sash drawing
284 // ----------------------------------------------------------------------------
285
286 static int GetGtkSplitterFullSize()
287 {
288 static GtkWidget *s_paned = NULL;
289 if (s_paned == NULL)
290 s_paned = gtk_vpaned_new();
291
292 gint handle_size;
293 gtk_widget_style_get (s_paned, "handle_size", &handle_size, NULL);
294
295 return handle_size;
296 }
297
298 wxSplitterRenderParams
299 wxRendererGTK::GetSplitterParams(const wxWindow *WXUNUSED(win))
300 {
301 // we don't draw any border, hence 0 for the second field
302 return wxSplitterRenderParams
303 (
304 GetGtkSplitterFullSize(),
305 0,
306 true // hot sensitive
307 );
308 }
309
310 void
311 wxRendererGTK::DrawSplitterBorder(wxWindow * WXUNUSED(win),
312 wxDC& WXUNUSED(dc),
313 const wxRect& WXUNUSED(rect),
314 int WXUNUSED(flags))
315 {
316 // nothing to do
317 }
318
319 void
320 wxRendererGTK::DrawSplitterSash(wxWindow *win,
321 wxDC& dc,
322 const wxSize& size,
323 wxCoord position,
324 wxOrientation orient,
325 int flags)
326 {
327 if ( !win->m_wxwindow->window )
328 {
329 // window not realized yet
330 return;
331 }
332
333 GdkWindow* gdk_window = dc.GetGDKWindow();
334 wxASSERT_MSG( gdk_window,
335 wxT("cannot use wxRendererNative on wxDC of this type") );
336
337 wxCoord full_size = GetGtkSplitterFullSize();
338
339 // are we drawing vertical or horizontal splitter?
340 const bool isVert = orient == wxVERTICAL;
341
342 GdkRectangle rect;
343
344 if ( isVert )
345 {
346 int h = win->GetClientSize().GetHeight();
347
348 rect.x = position;
349 rect.y = 0;
350 rect.width = full_size;
351 rect.height = h;
352 }
353 else // horz
354 {
355 int w = win->GetClientSize().GetWidth();
356
357 rect.x = 0;
358 rect.y = position;
359 rect.height = full_size;
360 rect.width = w;
361 }
362
363 int x_diff = 0;
364 if (win->GetLayoutDirection() == wxLayout_RightToLeft)
365 x_diff = rect.width;
366
367 gtk_paint_handle
368 (
369 win->m_wxwindow->style,
370 gdk_window,
371 flags & wxCONTROL_CURRENT ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
372 GTK_SHADOW_NONE,
373 NULL /* no clipping */,
374 win->m_wxwindow,
375 "paned",
376 dc.LogicalToDeviceX(rect.x) - x_diff,
377 dc.LogicalToDeviceY(rect.y),
378 rect.width,
379 rect.height,
380 isVert ? GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL
381 );
382 }
383
384 void
385 wxRendererGTK::DrawDropArrow(wxWindow *WXUNUSED(win),
386 wxDC& dc,
387 const wxRect& rect,
388 int flags)
389 {
390 GtkWidget *button = GetButtonWidget();
391
392 // If we give GTK_PIZZA(win->m_wxwindow)->bin_window as
393 // a window for gtk_paint_xxx function, then it won't
394 // work for wxMemoryDC. So that is why we assume wxDC
395 // is wxWindowDC (wxClientDC, wxMemoryDC and wxPaintDC
396 // are derived from it) and use its m_window.
397 GdkWindow* gdk_window = dc.GetGDKWindow();
398 wxASSERT_MSG( gdk_window,
399 wxT("cannot use wxRendererNative on wxDC of this type") );
400
401 // draw arrow so that there is even space horizontally
402 // on both sides
403 int arrowX = rect.width/4 + 1;
404 int arrowWidth = rect.width - (arrowX*2);
405
406 // scale arrow's height accoording to the width
407 int arrowHeight = rect.width/3;
408 int arrowY = (rect.height-arrowHeight)/2 +
409 ((rect.height-arrowHeight) & 1);
410
411 GtkStateType state;
412
413 if ( flags & wxCONTROL_PRESSED )
414 state = GTK_STATE_ACTIVE;
415 else if ( flags & wxCONTROL_DISABLED )
416 state = GTK_STATE_INSENSITIVE;
417 else if ( flags & wxCONTROL_CURRENT )
418 state = GTK_STATE_PRELIGHT;
419 else
420 state = GTK_STATE_NORMAL;
421
422 // draw arrow on button
423 gtk_paint_arrow
424 (
425 button->style,
426 gdk_window,
427 state,
428 flags & wxCONTROL_PRESSED ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
429 NULL,
430 button,
431 "arrow",
432 GTK_ARROW_DOWN,
433 FALSE,
434 rect.x + arrowX,
435 rect.y + arrowY,
436 arrowWidth,
437 arrowHeight
438 );
439 }
440
441 void
442 wxRendererGTK::DrawComboBoxDropButton(wxWindow *win,
443 wxDC& dc,
444 const wxRect& rect,
445 int flags)
446 {
447 DrawPushButton(win,dc,rect,flags);
448 DrawDropArrow(win,dc,rect);
449 }
450
451 void
452 wxRendererGTK::DrawCheckBox(wxWindow *WXUNUSED(win),
453 wxDC& dc,
454 const wxRect& rect,
455 int flags )
456 {
457 GtkWidget *button = GetCheckButtonWidget();
458
459 // for reason why we do this, see DrawDropArrow
460 GdkWindow* gdk_window = dc.GetGDKWindow();
461 wxASSERT_MSG( gdk_window,
462 wxT("cannot use wxRendererNative on wxDC of this type") );
463
464 GtkStateType state;
465
466 if ( flags & wxCONTROL_PRESSED )
467 state = GTK_STATE_ACTIVE;
468 else if ( flags & wxCONTROL_DISABLED )
469 state = GTK_STATE_INSENSITIVE;
470 else if ( flags & wxCONTROL_CURRENT )
471 state = GTK_STATE_PRELIGHT;
472 else
473 state = GTK_STATE_NORMAL;
474
475 gtk_paint_check
476 (
477 button->style,
478 gdk_window,
479 state,
480 flags & wxCONTROL_CHECKED ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
481 NULL,
482 button,
483 "cellcheck",
484 dc.LogicalToDeviceX(rect.x)+2,
485 dc.LogicalToDeviceY(rect.y)+3,
486 13, 13
487 );
488 }
489
490 void
491 wxRendererGTK::DrawPushButton(wxWindow *WXUNUSED(win),
492 wxDC& dc,
493 const wxRect& rect,
494 int flags)
495 {
496 GtkWidget *button = GetButtonWidget();
497
498 // for reason why we do this, see DrawDropArrow
499 GdkWindow* gdk_window = dc.GetGDKWindow();
500 wxASSERT_MSG( gdk_window,
501 wxT("cannot use wxRendererNative on wxDC of this type") );
502
503 // draw button
504 GtkStateType state;
505
506 if ( flags & wxCONTROL_PRESSED )
507 state = GTK_STATE_ACTIVE;
508 else if ( flags & wxCONTROL_DISABLED )
509 state = GTK_STATE_INSENSITIVE;
510 else if ( flags & wxCONTROL_CURRENT )
511 state = GTK_STATE_PRELIGHT;
512 else
513 state = GTK_STATE_NORMAL;
514
515 gtk_paint_box
516 (
517 button->style,
518 gdk_window,
519 state,
520 flags & wxCONTROL_PRESSED ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
521 NULL,
522 button,
523 "button",
524 rect.x, rect.y, rect.width, rect.height
525 );
526 }
527
528 void
529 wxRendererGTK::DrawItemSelectionRect(wxWindow *win,
530 wxDC& dc,
531 const wxRect& rect,
532 int flags )
533 {
534 GdkWindow* gdk_window = dc.GetGDKWindow();
535 wxASSERT_MSG( gdk_window,
536 wxT("cannot use wxRendererNative on wxDC of this type") );
537
538 int x_diff = 0;
539 if (win->GetLayoutDirection() == wxLayout_RightToLeft)
540 x_diff = rect.width;
541
542 GtkStateType state;
543 if (flags & wxCONTROL_SELECTED)
544 {
545 // the wxCONTROL_FOCUSED state is deduced
546 // directly from the m_wxwindow by GTK+
547 state = GTK_STATE_SELECTED;
548
549 gtk_paint_flat_box( win->m_widget->style,
550 gdk_window,
551 state,
552 GTK_SHADOW_NONE,
553 NULL,
554 win->m_wxwindow,
555 "cell_even",
556 dc.LogicalToDeviceX(rect.x) - x_diff,
557 dc.LogicalToDeviceY(rect.y),
558 rect.width,
559 rect.height );
560 }
561 else // !wxCONTROL_SELECTED
562 {
563 state = GTK_STATE_NORMAL;
564 }
565
566 if (flags & wxCONTROL_CURRENT)
567 {
568 gtk_paint_focus( win->m_widget->style,
569 gdk_window,
570 state,
571 NULL,
572 win->m_wxwindow,
573 "treeview",
574 dc.LogicalToDeviceX(rect.x),
575 dc.LogicalToDeviceY(rect.y),
576 rect.width,
577 rect.height );
578 }
579 }