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