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()//:
62 wxAuiTabArt
* wxAuiGtkTabArt::Clone()
64 wxAuiGtkTabArt
* clone
= new wxAuiGtkTabArt();
66 clone
->SetNormalFont(m_normalFont
);
67 clone
->SetSelectedFont(m_normalFont
);
68 clone
->SetMeasuringFont(m_normalFont
);
73 void wxAuiGtkTabArt::DrawBackground(wxDC
& dc
, wxWindow
* WXUNUSED(wnd
), const wxRect
& rect
)
75 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
76 GdkWindow
* window
= impldc
->GetGDKWindow();
78 gtk_style_apply_default_background(gtk_widget_get_style(wxGTKPrivate::GetNotebookWidget()),
83 rect
.x
, rect
.y
, rect
.width
, rect
.height
);
86 void ButtonStateAndShadow(int button_state
, GtkStateType
&state
, GtkShadowType
&shadow
)
89 if (button_state
& wxAUI_BUTTON_STATE_DISABLED
)
91 state
= GTK_STATE_INSENSITIVE
;
92 shadow
= GTK_SHADOW_ETCHED_IN
;
94 else if (button_state
& wxAUI_BUTTON_STATE_HOVER
)
96 state
= GTK_STATE_PRELIGHT
;
97 shadow
= GTK_SHADOW_OUT
;
99 else if (button_state
& wxAUI_BUTTON_STATE_PRESSED
)
101 state
= GTK_STATE_ACTIVE
;
102 shadow
= GTK_SHADOW_IN
;
106 state
= GTK_STATE_NORMAL
;
107 shadow
= GTK_SHADOW_OUT
;
111 wxRect
DrawCloseButton(wxDC
& dc
,
114 wxRect
const &in_rect
,
116 GdkRectangle
* clipRect
)
118 GtkStyle
*style_button
= gtk_widget_get_style(wxGTKPrivate::GetButtonWidget());
119 int xthickness
= style_button
->xthickness
;
120 int ythickness
= style_button
->ythickness
;
122 wxBitmap
bmp(gtk_widget_render_icon(widget
, GTK_STOCK_CLOSE
, GTK_ICON_SIZE_SMALL_TOOLBAR
, "tab"));
124 if(bmp
.GetWidth() != s_CloseIconSize
|| bmp
.GetHeight() != s_CloseIconSize
)
126 wxImage img
= bmp
.ConvertToImage();
127 img
.Rescale(s_CloseIconSize
, s_CloseIconSize
);
131 int button_size
= s_CloseIconSize
+ 2 * xthickness
;
135 if (orientation
== wxLEFT
)
136 out_rect
.x
= in_rect
.x
- ythickness
;
138 out_rect
.x
= in_rect
.x
+ in_rect
.width
- button_size
- ythickness
;
140 out_rect
.y
= in_rect
.y
+ (in_rect
.height
- button_size
) / 2;
141 out_rect
.width
= button_size
;
142 out_rect
.height
= button_size
;
144 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
145 GdkWindow
* window
= impldc
->GetGDKWindow();
147 if (button_state
== wxAUI_BUTTON_STATE_HOVER
)
149 gtk_paint_box(style_button
, window
,
150 GTK_STATE_PRELIGHT
, GTK_SHADOW_OUT
, clipRect
, widget
, "button",
151 out_rect
.x
, out_rect
.y
, out_rect
.width
, out_rect
.height
);
153 else if (button_state
== wxAUI_BUTTON_STATE_PRESSED
)
155 gtk_paint_box(style_button
, window
,
156 GTK_STATE_ACTIVE
, GTK_SHADOW_IN
, clipRect
, widget
, "button",
157 out_rect
.x
, out_rect
.y
, out_rect
.width
, out_rect
.height
);
161 dc
.DrawBitmap(bmp
, out_rect
.x
+ xthickness
, out_rect
.y
+ ythickness
, true);
166 void wxAuiGtkTabArt::DrawTab(wxDC
& dc
, wxWindow
* wnd
, const wxAuiNotebookPage
& page
,
167 const wxRect
& in_rect
, int close_button_state
, wxRect
* out_tab_rect
,
168 wxRect
* out_button_rect
, int* x_extent
)
170 GtkWidget
*widget
= wnd
->GetHandle();
171 GtkStyle
*style_notebook
= gtk_widget_get_style(wxGTKPrivate::GetNotebookWidget());
173 wxRect
const &window_rect
= wnd
->GetRect();
177 gtk_widget_style_get(wxGTKPrivate::GetNotebookWidget(),
178 "focus-line-width", &focus_width
,
181 int gap_x
= 0, gap_width
= 0;
183 if (m_flags
&wxAUI_NB_BOTTOM
)
184 tab_pos
= wxAUI_NB_BOTTOM
;
185 else //if (m_flags & wxAUI_NB_TOP) {}
186 tab_pos
= wxAUI_NB_TOP
;
188 // TODO: else if (m_flags &wxAUI_NB_LEFT) {}
189 // TODO: else if (m_flags &wxAUI_NB_RIGHT) {}
191 // figure out the size of the tab
192 wxSize tab_size
= GetTabSize(dc
, wnd
, page
.caption
, page
.bitmap
,
193 page
.active
, close_button_state
, x_extent
);
195 wxRect tab_rect
= in_rect
;
196 tab_rect
.width
= tab_size
.x
;
197 tab_rect
.height
= tab_size
.y
;
198 tab_rect
.y
+= 2 * GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder
;
201 tab_rect
.height
+= 2 * GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder
;
202 // if no bitmap is set, we need a tiny correction
203 if (! page
.bitmap
.IsOk())
204 tab_rect
.height
+= 1;
206 int gap_height
= 6 * GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder
;
207 int gap_y
= tab_rect
.y
- gap_height
;
212 tab_rect
.y
-= 2 * GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder
;
214 tab_rect
.y
+= 2 * GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder
;
215 gap_y
= tab_rect
.y
+ tab_rect
.height
- GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder
/ 2;
217 case wxAUI_NB_BOTTOM
:
218 gap_x
= tab_rect
.x
- GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_vborder
/ 2;
219 gap_width
= tab_rect
.width
;
222 tab_rect
.y
+= GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder
/ 2;
223 gap_y
+= GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder
/ 2;
225 int padding
= focus_width
+ GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder
;
227 int clip_width
= tab_rect
.width
;
228 if (tab_rect
.x
+ tab_rect
.width
> in_rect
.x
+ in_rect
.width
)
229 clip_width
= (in_rect
.x
+ in_rect
.width
) - tab_rect
.x
;
231 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
);
234 area
.x
= tab_rect
.x
- GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_vborder
;
235 area
.y
= tab_rect
.y
- 2 * GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder
;
236 area
.width
= clip_width
+ GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_vborder
;
237 area
.height
= tab_rect
.height
+ 2 * GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_hborder
;
239 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
240 GdkWindow
* window
= impldc
->GetGDKWindow();
242 if (tab_pos
== wxAUI_NB_BOTTOM
)
246 gtk_paint_box_gap(style_notebook
, window
, GTK_STATE_NORMAL
, GTK_SHADOW_OUT
,
248 const_cast<char*>("notebook"),
250 window_rect
.width
, gap_height
,
251 GTK_POS_BOTTOM
, gap_x
, gap_width
);
253 gtk_paint_extension(style_notebook
, window
,
254 page
.active
? GTK_STATE_NORMAL
: GTK_STATE_ACTIVE
, GTK_SHADOW_OUT
,
256 const_cast<char*>("tab"),
257 tab_rect
.x
, tab_rect
.y
,
258 tab_rect
.width
, tab_rect
.height
,
265 gtk_paint_box_gap(style_notebook
, window
, GTK_STATE_NORMAL
, GTK_SHADOW_OUT
,
267 const_cast<char*>("notebook"),
269 window_rect
.width
, gap_height
,
270 GTK_POS_TOP
, gap_x
, gap_width
);
272 gtk_paint_extension(style_notebook
, window
,
273 page
.active
? GTK_STATE_NORMAL
: GTK_STATE_ACTIVE
, GTK_SHADOW_OUT
,
275 const_cast<char*>("tab"),
276 tab_rect
.x
, tab_rect
.y
,
277 tab_rect
.width
, tab_rect
.height
,
281 wxCoord textX
= tab_rect
.x
+ padding
+ style_notebook
->xthickness
;
283 int bitmap_offset
= 0;
284 if (page
.bitmap
.IsOk())
286 bitmap_offset
= textX
;
289 int bitmapY
= tab_rect
.y
+(tab_rect
.height
- page
.bitmap
.GetHeight()) / 2;
292 if (tab_pos
== wxAUI_NB_TOP
)
293 bitmapY
+= style_notebook
->ythickness
/ 2;
295 bitmapY
-= style_notebook
->ythickness
/ 2;
297 dc
.DrawBitmap(page
.bitmap
,
302 textX
+= page
.bitmap
.GetWidth() + padding
;
305 wxCoord textW
, textH
, textY
;
307 dc
.SetFont(m_normalFont
);
308 dc
.GetTextExtent(page
.caption
, &textW
, &textH
);
309 textY
= tab_rect
.y
+ (tab_rect
.height
- textH
) / 2;
312 if (tab_pos
== wxAUI_NB_TOP
)
313 textY
+= style_notebook
->ythickness
/ 2;
315 textY
-= style_notebook
->ythickness
/ 2;
319 GdkColor text_colour
= page
.active
? style_notebook
->fg
[GTK_STATE_NORMAL
] : style_notebook
->fg
[GTK_STATE_ACTIVE
];
320 dc
.SetTextForeground(wxColor(text_colour
));
321 GdkRectangle focus_area
;
323 int padding_focus
= padding
- focus_width
;
324 focus_area
.x
= tab_rect
.x
+ padding_focus
;
325 focus_area
.y
= textY
- focus_width
;
326 focus_area
.width
= tab_rect
.width
- 2 * padding_focus
;
327 focus_area
.height
= textH
+ 2 * focus_width
;
329 if(page
.active
&& (wnd
->FindFocus() == wnd
) && focus_area
.x
<= (area
.x
+ area
.width
))
331 // clipping seems not to work here, so we we have to recalc the focus-area manually
332 if((focus_area
.x
+ focus_area
.width
) > (area
.x
+ area
.width
))
333 focus_area
.width
= area
.x
+ area
.width
- focus_area
.x
+ focus_width
- GTK_NOTEBOOK (wxGTKPrivate::GetNotebookWidget())->tab_vborder
;
334 gtk_paint_focus (style_notebook
, window
,
335 GTK_STATE_ACTIVE
, NULL
, widget
, "tab",
336 focus_area
.x
, focus_area
.y
, focus_area
.width
, focus_area
.height
);
339 dc
.DrawText(page
.caption
, textX
, textY
);
341 // draw close-button on tab (if enabled)
342 if (close_button_state
!= wxAUI_BUTTON_STATE_HIDDEN
)
344 wxRect
rect(tab_rect
.x
, tab_rect
.y
, tab_rect
.width
- style_notebook
->xthickness
, tab_rect
.height
);
347 if (tab_pos
== wxAUI_NB_TOP
)
348 rect
.y
+= style_notebook
->ythickness
/ 2;
350 rect
.y
-= style_notebook
->ythickness
/ 2;
352 *out_button_rect
= DrawCloseButton(dc
, widget
, close_button_state
, rect
, wxRIGHT
, &area
);
355 tab_rect
.width
= std::min(tab_rect
.width
, clip_width
);
356 *out_tab_rect
= tab_rect
;
358 dc
.DestroyClippingRegion();
361 wxRect
DrawSimpleArrow(wxDC
& dc
,
364 wxRect
const &in_rect
,
366 GtkArrowType arrow_type
)
368 int scroll_arrow_hlength
, scroll_arrow_vlength
;
369 gtk_widget_style_get(widget
,
370 "scroll-arrow-hlength", &scroll_arrow_hlength
,
371 "scroll-arrow-vlength", &scroll_arrow_vlength
,
375 GtkShadowType shadow
;
376 ButtonStateAndShadow(button_state
, state
, shadow
);
380 if (orientation
== wxLEFT
)
381 out_rect
.x
= in_rect
.x
;
383 out_rect
.x
= in_rect
.x
+ in_rect
.width
- scroll_arrow_hlength
;
384 out_rect
.y
= (in_rect
.y
+ in_rect
.height
- 3 * gtk_widget_get_style(wxGTKPrivate::GetNotebookWidget())->ythickness
- scroll_arrow_vlength
) / 2;
385 out_rect
.width
= scroll_arrow_hlength
;
386 out_rect
.height
= scroll_arrow_vlength
;
388 wxGTKDCImpl
*impldc
= (wxGTKDCImpl
*) dc
.GetImpl();
389 GdkWindow
* window
= impldc
->GetGDKWindow();
390 gtk_paint_arrow (gtk_widget_get_style(wxGTKPrivate::GetButtonWidget()), window
, state
, shadow
, NULL
, widget
, "notebook",
391 arrow_type
, TRUE
, out_rect
.x
, out_rect
.y
, out_rect
.width
, out_rect
.height
);
396 void wxAuiGtkTabArt::DrawButton(wxDC
& dc
, wxWindow
* wnd
,
397 const wxRect
& in_rect
,
403 GtkWidget
*widget
= wnd
->GetHandle();
404 wxRect rect
= in_rect
;
405 if (m_flags
&wxAUI_NB_BOTTOM
)
406 rect
.y
+= 2 * gtk_widget_get_style(wxGTKPrivate::GetButtonWidget())->ythickness
;
410 case wxAUI_BUTTON_CLOSE
:
411 rect
.y
-= 2 * gtk_widget_get_style(wxGTKPrivate::GetButtonWidget())->ythickness
;
412 rect
= DrawCloseButton(dc
, widget
, button_state
, rect
, orientation
, NULL
);
415 case wxAUI_BUTTON_LEFT
:
416 rect
= DrawSimpleArrow(dc
, widget
, button_state
, rect
, orientation
, GTK_ARROW_LEFT
);
419 case wxAUI_BUTTON_RIGHT
:
420 rect
= DrawSimpleArrow(dc
, widget
, button_state
, rect
, orientation
, GTK_ARROW_RIGHT
);
423 case wxAUI_BUTTON_WINDOWLIST
:
425 rect
.height
-= 4 * gtk_widget_get_style(wxGTKPrivate::GetButtonWidget())->ythickness
;
426 rect
.width
= rect
.height
;
427 rect
.x
= in_rect
.x
+ in_rect
.width
- rect
.width
;
429 if (button_state
== wxAUI_BUTTON_STATE_HOVER
)
430 wxRendererNative::Get().DrawComboBoxDropButton(wnd
, dc
, rect
, wxCONTROL_CURRENT
);
431 else if (button_state
== wxAUI_BUTTON_STATE_PRESSED
)
432 wxRendererNative::Get().DrawComboBoxDropButton(wnd
, dc
, rect
, wxCONTROL_PRESSED
);
434 wxRendererNative::Get().DrawDropArrow(wnd
, dc
, rect
);
443 int wxAuiGtkTabArt::GetBestTabCtrlSize(wxWindow
* wnd
,
444 const wxAuiNotebookPageArray
& pages
,
445 const wxSize
& required_bmp_size
)
447 SetMeasuringFont(m_normalFont
);
448 SetSelectedFont(m_normalFont
);
449 int tab_height
= 3 * gtk_widget_get_style(wxGTKPrivate::GetNotebookWidget())->ythickness
+ wxAuiGenericTabArt::GetBestTabCtrlSize(wnd
, pages
, required_bmp_size
);
453 wxSize
wxAuiGtkTabArt::GetTabSize(wxDC
& dc
,
455 const wxString
& caption
,
456 const wxBitmap
& bitmap
,
458 int close_button_state
,
461 wxSize s
= wxAuiGenericTabArt::GetTabSize(dc
, wnd
, caption
, bitmap
, active
, close_button_state
, x_extent
);
464 gtk_widget_style_get (wnd
->GetHandle(),
465 "focus-line-width", &overlap
,
467 *x_extent
-= overlap
;