1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/aui/tabartgtk.cpp
3 // Purpose: implementation of the wxAuiGTKTabArt
4 // Author: Jens Lody and Teodor Petrov
8 // Copyright: (c) 2012 Jens Lody <jens@codeblocks.org>
10 // Licence: wxWindows licence
11 ///////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
32 #include "wx/dcclient.h"
33 #include "wx/settings.h"
37 #include "wx/gtk/dc.h"
38 #include "wx/gtk/private.h"
42 #include "wx/aui/auibook.h"
43 #include "wx/aui/tabartgtk.h"
44 #include "wx/renderer.h"
49 static int s_CloseIconSize
= 16; // default size
53 wxAuiGtkTabArt::wxAuiGtkTabArt()
58 wxAuiTabArt
* wxAuiGtkTabArt::Clone()
60 wxAuiGtkTabArt
* clone
= new wxAuiGtkTabArt();
62 clone
->SetNormalFont(m_normalFont
);
63 clone
->SetSelectedFont(m_normalFont
);
64 clone
->SetMeasuringFont(m_normalFont
);
69 void wxAuiGtkTabArt::DrawBackground(wxDC
& dc
, wxWindow
* WXUNUSED(wnd
), const wxRect
& rect
)
71 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
72 GdkWindow
* window
= impldc
->GetGDKWindow();
74 gtk_style_apply_default_background(gtk_widget_get_style(wxGTKPrivate::GetNotebookWidget()),
79 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
82 void ButtonStateAndShadow(int button_state
, GtkStateType
&state
, GtkShadowType
&shadow
)
85 if (button_state
& wxAUI_BUTTON_STATE_DISABLED
)
87 state
= GTK_STATE_INSENSITIVE
;
88 shadow
= GTK_SHADOW_ETCHED_IN
;
90 else if (button_state
& wxAUI_BUTTON_STATE_HOVER
)
92 state
= GTK_STATE_PRELIGHT
;
93 shadow
= GTK_SHADOW_OUT
;
95 else if (button_state
& wxAUI_BUTTON_STATE_PRESSED
)
97 state
= GTK_STATE_ACTIVE
;
98 shadow
= GTK_SHADOW_IN
;
102 state
= GTK_STATE_NORMAL
;
103 shadow
= GTK_SHADOW_OUT
;
107 wxRect
DrawCloseButton(wxDC
& dc
,
110 wxRect
const &in_rect
,
112 GdkRectangle
* clipRect
)
114 GtkStyle
*style_button
= gtk_widget_get_style(wxGTKPrivate::GetButtonWidget());
115 int xthickness
= style_button
->xthickness
;
116 int ythickness
= style_button
->ythickness
;
118 wxBitmap
bmp(gtk_widget_render_icon(widget
, GTK_STOCK_CLOSE
, GTK_ICON_SIZE_SMALL_TOOLBAR
, "tab"));
120 if(bmp
.GetWidth() != s_CloseIconSize
|| bmp
.GetHeight() != s_CloseIconSize
)
122 wxImage img
= bmp
.ConvertToImage();
123 img
.Rescale(s_CloseIconSize
, s_CloseIconSize
);
127 int button_size
= s_CloseIconSize
+ 2 * xthickness
;
131 if (orientation
== wxLEFT
)
132 out_rect
.x
= in_rect
.x
- ythickness
;
134 out_rect
.x
= in_rect
.x
+ in_rect
.width
- button_size
- ythickness
;
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
;
140 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
141 GdkWindow
* window
= impldc
->GetGDKWindow();
143 if (button_state
== wxAUI_BUTTON_STATE_HOVER
)
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
);
149 else if (button_state
== wxAUI_BUTTON_STATE_PRESSED
)
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
);
157 dc
.DrawBitmap(bmp
, out_rect
.x
+ xthickness
, out_rect
.y
+ ythickness
, true);
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
)
166 GtkWidget
*widget
= wnd
->GetHandle();
167 GtkStyle
*style_notebook
= gtk_widget_get_style(wxGTKPrivate::GetNotebookWidget());
169 wxRect
const &window_rect
= wnd
->GetRect();
173 gtk_widget_style_get(wxGTKPrivate::GetNotebookWidget(),
174 "focus-line-width", &focus_width
,
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
;
183 // TODO: else if (m_flags &wxAUI_NB_LEFT) {}
184 // TODO: else if (m_flags &wxAUI_NB_RIGHT) {}
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
);
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
;
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;
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
;
209 tab_rect
.y
-= 2 * GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder
;
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;
214 case wxAUI_NB_BOTTOM
:
215 gap_start
= tab_rect
.x
- GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_vborder
/ 2;
216 gap_width
= tab_rect
.width
;
218 // TODO: case wxAUI_NB_LEFT: break;
219 // TODO: case wxAUI_NB_RIGHT: break;
221 tab_rect
.y
+= GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder
/ 2;
222 gap_rect_y
+= GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder
/ 2;
224 int padding
= focus_width
+ GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder
;
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
;
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
);
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
;
238 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
239 GdkWindow
* window
= impldc
->GetGDKWindow();
241 if (tab_pos
== wxAUI_NB_BOTTOM
)
245 gtk_paint_box_gap(style_notebook
, window
, GTK_STATE_NORMAL
, GTK_SHADOW_OUT
,
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
);
252 gtk_paint_extension(style_notebook
, window
,
253 page
.active
? GTK_STATE_NORMAL
: GTK_STATE_ACTIVE
, GTK_SHADOW_OUT
,
255 const_cast<char*>("tab"),
256 tab_rect
.x
, tab_rect
.y
,
257 tab_rect
.width
, tab_rect
.height
,
264 gtk_paint_box_gap(style_notebook
, window
, GTK_STATE_NORMAL
, GTK_SHADOW_OUT
,
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
);
271 gtk_paint_extension(style_notebook
, window
,
272 page
.active
? GTK_STATE_NORMAL
: GTK_STATE_ACTIVE
, GTK_SHADOW_OUT
,
274 const_cast<char*>("tab"),
275 tab_rect
.x
, tab_rect
.y
,
276 tab_rect
.width
, tab_rect
.height
,
280 wxCoord textX
= tab_rect
.x
+ padding
+ style_notebook
->xthickness
;
282 int bitmap_offset
= 0;
283 if (page
.bitmap
.IsOk())
285 bitmap_offset
= textX
;
288 int bitmapY
= tab_rect
.y
+(tab_rect
.height
- page
.bitmap
.GetHeight()) / 2;
291 if (tab_pos
== wxAUI_NB_TOP
)
292 bitmapY
+= style_notebook
->ythickness
/ 2;
294 bitmapY
-= style_notebook
->ythickness
/ 2;
296 dc
.DrawBitmap(page
.bitmap
,
301 textX
+= page
.bitmap
.GetWidth() + padding
;
304 wxCoord textW
, textH
, textY
;
306 dc
.SetFont(m_normalFont
);
307 dc
.GetTextExtent(page
.caption
, &textW
, &textH
);
308 textY
= tab_rect
.y
+ (tab_rect
.height
- textH
) / 2;
311 if (tab_pos
== wxAUI_NB_TOP
)
312 textY
+= style_notebook
->ythickness
/ 2;
314 textY
-= style_notebook
->ythickness
/ 2;
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
;
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
;
328 if(page
.active
&& (wnd
->FindFocus() == wnd
) && focus_area
.x
<= (area
.x
+ area
.width
))
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
);
338 dc
.DrawText(page
.caption
, textX
, textY
);
340 // draw close-button on tab (if enabled)
341 if (close_button_state
!= wxAUI_BUTTON_STATE_HIDDEN
)
343 wxRect
rect(tab_rect
.x
, tab_rect
.y
, tab_rect
.width
- style_notebook
->xthickness
, tab_rect
.height
);
346 if (tab_pos
== wxAUI_NB_TOP
)
347 rect
.y
+= style_notebook
->ythickness
/ 2;
349 rect
.y
-= style_notebook
->ythickness
/ 2;
351 *out_button_rect
= DrawCloseButton(dc
, widget
, close_button_state
, rect
, wxRIGHT
, &area
);
354 tab_rect
.width
= std::min(tab_rect
.width
, clip_width
);
355 *out_tab_rect
= tab_rect
;
357 dc
.DestroyClippingRegion();
360 wxRect
DrawSimpleArrow(wxDC
& dc
,
363 wxRect
const &in_rect
,
365 GtkArrowType arrow_type
)
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
,
374 GtkShadowType shadow
;
375 ButtonStateAndShadow(button_state
, state
, shadow
);
379 if (orientation
== wxLEFT
)
380 out_rect
.x
= in_rect
.x
;
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
;
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
);
395 void wxAuiGtkTabArt::DrawButton(wxDC
& dc
, wxWindow
* wnd
,
396 const wxRect
& in_rect
,
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
;
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
);
414 case wxAUI_BUTTON_LEFT
:
415 rect
= DrawSimpleArrow(dc
, widget
, button_state
, rect
, orientation
, GTK_ARROW_LEFT
);
418 case wxAUI_BUTTON_RIGHT
:
419 rect
= DrawSimpleArrow(dc
, widget
, button_state
, rect
, orientation
, GTK_ARROW_RIGHT
);
422 case wxAUI_BUTTON_WINDOWLIST
:
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
;
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
);
433 wxRendererNative::Get().DrawDropArrow(wnd
, dc
, rect
);
442 int wxAuiGtkTabArt::GetBestTabCtrlSize(wxWindow
* wnd
,
443 const wxAuiNotebookPageArray
& pages
,
444 const wxSize
& required_bmp_size
)
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
);
452 wxSize
wxAuiGtkTabArt::GetTabSize(wxDC
& dc
,
454 const wxString
& caption
,
455 const wxBitmap
& bitmap
,
457 int close_button_state
,
460 wxSize s
= wxAuiGenericTabArt::GetTabSize(dc
, wnd
, caption
, bitmap
, active
, close_button_state
, x_extent
);
463 gtk_widget_style_get (wnd
->GetHandle(),
464 "focus-line-width", &overlap
,
466 *x_extent
-= overlap
;