]> git.saurik.com Git - wxWidgets.git/blob - src/aui/tabartgtk.cpp
Remove wxT from prototype
[wxWidgets.git] / src / aui / tabartgtk.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/aui/tabartgtk.cpp
3 // Purpose: implementation of the wxAuiGTKTabArt
4 // Author: Jens Lody and Teodor Petrov
5 // Modified by:
6 // Created: 2012-03-23
7 // RCS-ID: $Id$
8 // Copyright: (c) 2012 Jens Lody <jens@codeblocks.org>
9 // and Teodor Petrov
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
12
13 // ============================================================================
14 // declarations
15 // ============================================================================
16
17 // ----------------------------------------------------------------------------
18 // headers
19 // ----------------------------------------------------------------------------
20
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
23
24 #ifdef __BORLANDC__
25 #pragma hdrstop
26 #endif
27
28 #if wxUSE_AUI
29
30 #ifndef WX_PRECOMP
31 #include "wx/dc.h"
32 #include "wx/dcclient.h"
33 #include "wx/settings.h"
34 #include "wx/image.h"
35 #endif
36
37 #include "wx/gtk/dc.h"
38 #include "wx/gtk/private.h"
39
40 #include <gtk/gtk.h>
41
42 #include "wx/aui/auibook.h"
43 #include "wx/aui/tabartgtk.h"
44 #include "wx/renderer.h"
45
46 namespace
47 {
48
49 static int s_CloseIconSize = 16; // default size
50
51 }
52
53 wxAuiGtkTabArt::wxAuiGtkTabArt()
54
55 {
56 }
57
58 wxAuiTabArt* wxAuiGtkTabArt::Clone()
59 {
60 wxAuiGtkTabArt* clone = new wxAuiGtkTabArt();
61
62 clone->SetNormalFont(m_normalFont);
63 clone->SetSelectedFont(m_normalFont);
64 clone->SetMeasuringFont(m_normalFont);
65
66 return clone;
67 }
68
69 void wxAuiGtkTabArt::DrawBackground(wxDC& dc, wxWindow* WXUNUSED(wnd), const wxRect& rect)
70 {
71 wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl();
72 GdkWindow* window = impldc->GetGDKWindow();
73
74 gtk_style_apply_default_background(gtk_widget_get_style(wxGTKPrivate::GetNotebookWidget()),
75 window,
76 true,
77 GTK_STATE_NORMAL,
78 NULL,
79 rect.x, rect.y, rect.width, rect.height);
80 }
81
82 void ButtonStateAndShadow(int button_state, GtkStateType &state, GtkShadowType &shadow)
83 {
84
85 if (button_state & wxAUI_BUTTON_STATE_DISABLED)
86 {
87 state = GTK_STATE_INSENSITIVE;
88 shadow = GTK_SHADOW_ETCHED_IN;
89 }
90 else if (button_state & wxAUI_BUTTON_STATE_HOVER)
91 {
92 state = GTK_STATE_PRELIGHT;
93 shadow = GTK_SHADOW_OUT;
94 }
95 else if (button_state & wxAUI_BUTTON_STATE_PRESSED)
96 {
97 state = GTK_STATE_ACTIVE;
98 shadow = GTK_SHADOW_IN;
99 }
100 else
101 {
102 state = GTK_STATE_NORMAL;
103 shadow = GTK_SHADOW_OUT;
104 }
105 }
106
107 wxRect DrawCloseButton(wxDC& dc,
108 GtkWidget *widget,
109 int button_state,
110 wxRect const &in_rect,
111 int orientation,
112 GdkRectangle* clipRect)
113 {
114 GtkStyle *style_button = gtk_widget_get_style(wxGTKPrivate::GetButtonWidget());
115 int xthickness = style_button->xthickness;
116 int ythickness = style_button->ythickness;
117
118 wxBitmap bmp(gtk_widget_render_icon(widget, GTK_STOCK_CLOSE, GTK_ICON_SIZE_SMALL_TOOLBAR, "tab"));
119
120 if(bmp.GetWidth() != s_CloseIconSize || bmp.GetHeight() != s_CloseIconSize)
121 {
122 wxImage img = bmp.ConvertToImage();
123 img.Rescale(s_CloseIconSize, s_CloseIconSize);
124 bmp = img;
125 }
126
127 int button_size = s_CloseIconSize + 2 * xthickness;
128
129 wxRect out_rect;
130
131 if (orientation == wxLEFT)
132 out_rect.x = in_rect.x - ythickness;
133 else
134 out_rect.x = in_rect.x + in_rect.width - button_size - ythickness;
135
136 out_rect.y = in_rect.y + (in_rect.height - button_size) / 2;
137 out_rect.width = button_size;
138 out_rect.height = button_size;
139
140 wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl();
141 GdkWindow* window = impldc->GetGDKWindow();
142
143 if (button_state == wxAUI_BUTTON_STATE_HOVER)
144 {
145 gtk_paint_box(style_button, window,
146 GTK_STATE_PRELIGHT, GTK_SHADOW_OUT, clipRect, widget, "button",
147 out_rect.x, out_rect.y, out_rect.width, out_rect.height);
148 }
149 else if (button_state == wxAUI_BUTTON_STATE_PRESSED)
150 {
151 gtk_paint_box(style_button, window,
152 GTK_STATE_ACTIVE, GTK_SHADOW_IN, clipRect, widget, "button",
153 out_rect.x, out_rect.y, out_rect.width, out_rect.height);
154 }
155
156
157 dc.DrawBitmap(bmp, out_rect.x + xthickness, out_rect.y + ythickness, true);
158
159 return out_rect;
160 }
161
162 void wxAuiGtkTabArt::DrawTab(wxDC& dc, wxWindow* wnd, const wxAuiNotebookPage& page,
163 const wxRect& in_rect, int close_button_state, wxRect* out_tab_rect,
164 wxRect* out_button_rect, int* x_extent)
165 {
166 GtkWidget *widget = wnd->GetHandle();
167 GtkStyle *style_notebook = gtk_widget_get_style(wxGTKPrivate::GetNotebookWidget());
168
169 wxRect const &window_rect = wnd->GetRect();
170
171 int focus_width = 0;
172
173 gtk_widget_style_get(wxGTKPrivate::GetNotebookWidget(),
174 "focus-line-width", &focus_width,
175 NULL);
176
177 int tab_pos;
178 if (m_flags &wxAUI_NB_BOTTOM)
179 tab_pos = wxAUI_NB_BOTTOM;
180 else //if (m_flags & wxAUI_NB_TOP) {}
181 tab_pos = wxAUI_NB_TOP;
182
183 // TODO: else if (m_flags &wxAUI_NB_LEFT) {}
184 // TODO: else if (m_flags &wxAUI_NB_RIGHT) {}
185
186 // figure out the size of the tab
187 wxSize tab_size = GetTabSize(dc, wnd, page.caption, page.bitmap,
188 page.active, close_button_state, x_extent);
189
190 wxRect tab_rect = in_rect;
191 tab_rect.width = tab_size.x;
192 tab_rect.height = tab_size.y;
193 tab_rect.y += 2 * GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder;
194
195 if (page.active)
196 tab_rect.height += 2 * GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder;
197 // if no bitmap is set, we need a tiny correction
198 if (! page.bitmap.IsOk())
199 tab_rect.height += 1;
200
201 int gap_rect_height = 6 * GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder;
202 int gap_rect_x = 1, gap_start = 0, gap_width = 0;
203 int gap_rect_y = tab_rect.y - gap_rect_height;
204 int gap_rect_width = window_rect.width;
205
206 switch (tab_pos)
207 {
208 case wxAUI_NB_TOP:
209 tab_rect.y -= 2 * GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder;
210 if (!page.active)
211 tab_rect.y += 2 * GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder;
212 gap_rect_y = tab_rect.y + tab_rect.height - GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder / 2;
213 // fall through
214 case wxAUI_NB_BOTTOM:
215 gap_start = tab_rect.x - GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_vborder / 2;
216 gap_width = tab_rect.width;
217 break;
218 // TODO: case wxAUI_NB_LEFT: break;
219 // TODO: case wxAUI_NB_RIGHT: break;
220 }
221 tab_rect.y += GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder / 2;
222 gap_rect_y += GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder / 2;
223
224 int padding = focus_width + GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder;
225
226 int clip_width = tab_rect.width;
227 if (tab_rect.x + tab_rect.width > in_rect.x + in_rect.width)
228 clip_width = (in_rect.x + in_rect.width) - tab_rect.x;
229
230 dc.SetClippingRegion(tab_rect.x, tab_rect.y - GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_vborder, clip_width, tab_rect.height + GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_vborder);
231
232 GdkRectangle area;
233 area.x = tab_rect.x - GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_vborder;
234 area.y = tab_rect.y - 2 * GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder;
235 area.width = clip_width + GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_vborder;
236 area.height = tab_rect.height + 2 * GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder;
237
238 wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl();
239 GdkWindow* window = impldc->GetGDKWindow();
240
241 if (tab_pos == wxAUI_NB_BOTTOM)
242 {
243 if (page.active)
244 {
245 gtk_paint_box_gap(style_notebook, window, GTK_STATE_NORMAL, GTK_SHADOW_OUT,
246 NULL, widget,
247 const_cast<char*>("notebook"),
248 gap_rect_x, gap_rect_y,
249 gap_rect_width, gap_rect_height,
250 GTK_POS_BOTTOM, gap_start , gap_width);
251 }
252 gtk_paint_extension(style_notebook, window,
253 page.active ? GTK_STATE_NORMAL : GTK_STATE_ACTIVE, GTK_SHADOW_OUT,
254 &area, widget,
255 const_cast<char*>("tab"),
256 tab_rect.x, tab_rect.y,
257 tab_rect.width, tab_rect.height,
258 GTK_POS_TOP);
259 }
260 else
261 {
262 if (page.active)
263 {
264 gtk_paint_box_gap(style_notebook, window, GTK_STATE_NORMAL, GTK_SHADOW_OUT,
265 NULL, widget,
266 const_cast<char*>("notebook"),
267 gap_rect_x, gap_rect_y,
268 gap_rect_width, gap_rect_height,
269 GTK_POS_TOP, gap_start , gap_width);
270 }
271 gtk_paint_extension(style_notebook, window,
272 page.active ? GTK_STATE_NORMAL : GTK_STATE_ACTIVE, GTK_SHADOW_OUT,
273 &area, widget,
274 const_cast<char*>("tab"),
275 tab_rect.x, tab_rect.y,
276 tab_rect.width, tab_rect.height,
277 GTK_POS_BOTTOM);
278 }
279
280 wxCoord textX = tab_rect.x + padding + style_notebook->xthickness;
281
282 int bitmap_offset = 0;
283 if (page.bitmap.IsOk())
284 {
285 bitmap_offset = textX;
286
287 // draw bitmap
288 int bitmapY = tab_rect.y +(tab_rect.height - page.bitmap.GetHeight()) / 2;
289 if(!page.active)
290 {
291 if (tab_pos == wxAUI_NB_TOP)
292 bitmapY += style_notebook->ythickness / 2;
293 else
294 bitmapY -= style_notebook->ythickness / 2;
295 }
296 dc.DrawBitmap(page.bitmap,
297 bitmap_offset,
298 bitmapY,
299 true);
300
301 textX += page.bitmap.GetWidth() + padding;
302 }
303
304 wxCoord textW, textH, textY;
305
306 dc.SetFont(m_normalFont);
307 dc.GetTextExtent(page.caption, &textW, &textH);
308 textY = tab_rect.y + (tab_rect.height - textH) / 2;
309 if(!page.active)
310 {
311 if (tab_pos == wxAUI_NB_TOP)
312 textY += style_notebook->ythickness / 2;
313 else
314 textY -= style_notebook->ythickness / 2;
315 }
316
317 // draw tab text
318 GdkColor text_colour = page.active ? style_notebook->fg[GTK_STATE_NORMAL] : style_notebook->fg[GTK_STATE_ACTIVE];
319 dc.SetTextForeground(wxColor(text_colour));
320 GdkRectangle focus_area;
321
322 int padding_focus = padding - focus_width;
323 focus_area.x = tab_rect.x + padding_focus;
324 focus_area.y = textY - focus_width;
325 focus_area.width = tab_rect.width - 2 * padding_focus;
326 focus_area.height = textH + 2 * focus_width;
327
328 if(page.active && (wnd->FindFocus() == wnd) && focus_area.x <= (area.x + area.width))
329 {
330 // clipping seems not to work here, so we we have to recalc the focus-area manually
331 if((focus_area.x + focus_area.width) > (area.x + area.width))
332 focus_area.width = area.x + area.width - focus_area.x + focus_width - GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_vborder;
333 gtk_paint_focus (style_notebook, window,
334 GTK_STATE_ACTIVE, NULL, widget, "tab",
335 focus_area.x, focus_area.y, focus_area.width, focus_area.height);
336 }
337
338 dc.DrawText(page.caption, textX, textY);
339
340 // draw close-button on tab (if enabled)
341 if (close_button_state != wxAUI_BUTTON_STATE_HIDDEN)
342 {
343 wxRect rect(tab_rect.x, tab_rect.y, tab_rect.width - style_notebook->xthickness, tab_rect.height);
344 if(!page.active)
345 {
346 if (tab_pos == wxAUI_NB_TOP)
347 rect.y += style_notebook->ythickness / 2;
348 else
349 rect.y -= style_notebook->ythickness / 2;
350 }
351 *out_button_rect = DrawCloseButton(dc, widget, close_button_state, rect, wxRIGHT, &area);
352 }
353
354 tab_rect.width = std::min(tab_rect.width, clip_width);
355 *out_tab_rect = tab_rect;
356
357 dc.DestroyClippingRegion();
358 }
359
360 wxRect DrawSimpleArrow(wxDC& dc,
361 GtkWidget *widget,
362 int button_state,
363 wxRect const &in_rect,
364 int orientation,
365 GtkArrowType arrow_type)
366 {
367 int scroll_arrow_hlength, scroll_arrow_vlength;
368 gtk_widget_style_get(widget,
369 "scroll-arrow-hlength", &scroll_arrow_hlength,
370 "scroll-arrow-vlength", &scroll_arrow_vlength,
371 NULL);
372
373 GtkStateType state;
374 GtkShadowType shadow;
375 ButtonStateAndShadow(button_state, state, shadow);
376
377 wxRect out_rect;
378
379 if (orientation == wxLEFT)
380 out_rect.x = in_rect.x;
381 else
382 out_rect.x = in_rect.x + in_rect.width - scroll_arrow_hlength;
383 out_rect.y = (in_rect.y + in_rect.height - 3 * gtk_widget_get_style(wxGTKPrivate::GetNotebookWidget())->ythickness - scroll_arrow_vlength) / 2;
384 out_rect.width = scroll_arrow_hlength;
385 out_rect.height = scroll_arrow_vlength;
386
387 wxGTKDCImpl *impldc = (wxGTKDCImpl*) dc.GetImpl();
388 GdkWindow* window = impldc->GetGDKWindow();
389 gtk_paint_arrow (gtk_widget_get_style(wxGTKPrivate::GetButtonWidget()), window, state, shadow, NULL, widget, "notebook",
390 arrow_type, TRUE, out_rect.x, out_rect.y, out_rect.width, out_rect.height);
391
392 return out_rect;
393 }
394
395 void wxAuiGtkTabArt::DrawButton(wxDC& dc, wxWindow* wnd,
396 const wxRect& in_rect,
397 int bitmap_id,
398 int button_state,
399 int orientation,
400 wxRect* out_rect)
401 {
402 GtkWidget *widget = wnd->GetHandle();
403 wxRect rect = in_rect;
404 if (m_flags &wxAUI_NB_BOTTOM)
405 rect.y += 2 * gtk_widget_get_style(wxGTKPrivate::GetButtonWidget())->ythickness;
406
407 switch (bitmap_id)
408 {
409 case wxAUI_BUTTON_CLOSE:
410 rect.y -= 2 * gtk_widget_get_style(wxGTKPrivate::GetButtonWidget())->ythickness;
411 rect = DrawCloseButton(dc, widget, button_state, rect, orientation, NULL);
412 break;
413
414 case wxAUI_BUTTON_LEFT:
415 rect = DrawSimpleArrow(dc, widget, button_state, rect, orientation, GTK_ARROW_LEFT);
416 break;
417
418 case wxAUI_BUTTON_RIGHT:
419 rect = DrawSimpleArrow(dc, widget, button_state, rect, orientation, GTK_ARROW_RIGHT);
420 break;
421
422 case wxAUI_BUTTON_WINDOWLIST:
423 {
424 rect.height -= 4 * gtk_widget_get_style(wxGTKPrivate::GetButtonWidget())->ythickness;
425 rect.width = rect.height;
426 rect.x = in_rect.x + in_rect.width - rect.width;
427
428 if (button_state == wxAUI_BUTTON_STATE_HOVER)
429 wxRendererNative::Get().DrawComboBoxDropButton(wnd, dc, rect, wxCONTROL_CURRENT);
430 else if (button_state == wxAUI_BUTTON_STATE_PRESSED)
431 wxRendererNative::Get().DrawComboBoxDropButton(wnd, dc, rect, wxCONTROL_PRESSED);
432 else
433 wxRendererNative::Get().DrawDropArrow(wnd, dc, rect);
434 }
435 break;
436 }
437
438 *out_rect = rect;
439 }
440
441
442 int wxAuiGtkTabArt::GetBestTabCtrlSize(wxWindow* wnd,
443 const wxAuiNotebookPageArray& pages,
444 const wxSize& required_bmp_size)
445 {
446 SetMeasuringFont(m_normalFont);
447 SetSelectedFont(m_normalFont);
448 int tab_height = 3 * gtk_widget_get_style(wxGTKPrivate::GetNotebookWidget())->ythickness + wxAuiGenericTabArt::GetBestTabCtrlSize(wnd, pages, required_bmp_size);
449 return tab_height;
450 }
451
452 wxSize wxAuiGtkTabArt::GetTabSize(wxDC& dc,
453 wxWindow* wnd,
454 const wxString& caption,
455 const wxBitmap& bitmap,
456 bool active,
457 int close_button_state,
458 int* x_extent)
459 {
460 wxSize s = wxAuiGenericTabArt::GetTabSize(dc, wnd, caption, bitmap, active, close_button_state, x_extent);
461
462 int overlap = 0;
463 gtk_widget_style_get (wnd->GetHandle(),
464 "focus-line-width", &overlap,
465 NULL);
466 *x_extent -= overlap;
467 return s;
468 }
469 #endif // wxUSE_AUI