]> git.saurik.com Git - wxWidgets.git/blob - src/aui/auibook.cpp
379f0d61ba2a42c36039ab66b35351fd8f354e5d
[wxWidgets.git] / src / aui / auibook.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/aui/auibook.cpp
3 // Purpose: wxaui: wx advanced user interface - notebook
4 // Author: Benjamin I. Williams
5 // Modified by:
6 // Created: 2006-06-28
7 // Copyright: (C) Copyright 2006, Kirix Corporation, All Rights Reserved
8 // Licence: wxWindows Library Licence, Version 3.1
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ----------------------------------------------------------------------------
12 // headers
13 // ----------------------------------------------------------------------------
14
15 #include "wx/wxprec.h"
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 #if wxUSE_AUI
22
23 #include "wx/aui/auibook.h"
24
25 #ifndef WX_PRECOMP
26 #include "wx/settings.h"
27 #include "wx/image.h"
28 #include "wx/menu.h"
29 #endif
30
31 #include "wx/aui/tabmdi.h"
32 #include "wx/dcbuffer.h"
33
34 #include "wx/renderer.h"
35
36 #ifdef __WXMAC__
37 #include "wx/osx/private.h"
38 #endif
39
40 #include "wx/arrimpl.cpp"
41 WX_DEFINE_OBJARRAY(wxAuiNotebookPageArray)
42 WX_DEFINE_OBJARRAY(wxAuiTabContainerButtonArray)
43
44 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CLOSE, wxAuiNotebookEvent);
45 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CLOSED, wxAuiNotebookEvent);
46 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING, wxAuiNotebookEvent);
47 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGED, wxAuiNotebookEvent);
48 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_BUTTON, wxAuiNotebookEvent);
49 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_BEGIN_DRAG, wxAuiNotebookEvent);
50 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_END_DRAG, wxAuiNotebookEvent);
51 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_DRAG_MOTION, wxAuiNotebookEvent);
52 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_ALLOW_DND, wxAuiNotebookEvent);
53 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_BG_DCLICK, wxAuiNotebookEvent);
54 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_DRAG_DONE, wxAuiNotebookEvent);
55 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_TAB_MIDDLE_UP, wxAuiNotebookEvent);
56 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_TAB_MIDDLE_DOWN, wxAuiNotebookEvent);
57 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_TAB_RIGHT_UP, wxAuiNotebookEvent);
58 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_TAB_RIGHT_DOWN, wxAuiNotebookEvent);
59
60 IMPLEMENT_CLASS(wxAuiNotebook, wxControl)
61 IMPLEMENT_CLASS(wxAuiTabCtrl, wxControl)
62 IMPLEMENT_DYNAMIC_CLASS(wxAuiNotebookEvent, wxEvent)
63
64
65
66
67
68 // these functions live in dockart.cpp -- they'll eventually
69 // be moved to a new utility cpp file
70
71 wxColor wxAuiStepColour(const wxColor& c, int percent);
72
73 wxBitmap wxAuiBitmapFromBits(const unsigned char bits[], int w, int h,
74 const wxColour& color);
75
76 wxString wxAuiChopText(wxDC& dc, const wxString& text, int max_size);
77
78 static void DrawButtons(wxDC& dc,
79 const wxRect& _rect,
80 const wxBitmap& bmp,
81 const wxColour& bkcolour,
82 int button_state)
83 {
84 wxRect rect = _rect;
85
86 if (button_state == wxAUI_BUTTON_STATE_PRESSED)
87 {
88 rect.x++;
89 rect.y++;
90 }
91
92 if (button_state == wxAUI_BUTTON_STATE_HOVER ||
93 button_state == wxAUI_BUTTON_STATE_PRESSED)
94 {
95 dc.SetBrush(wxBrush(wxAuiStepColour(bkcolour, 120)));
96 dc.SetPen(wxPen(wxAuiStepColour(bkcolour, 75)));
97
98 // draw the background behind the button
99 dc.DrawRectangle(rect.x, rect.y, 15, 15);
100 }
101
102 // draw the button itself
103 dc.DrawBitmap(bmp, rect.x, rect.y, true);
104 }
105
106 static void IndentPressedBitmap(wxRect* rect, int button_state)
107 {
108 if (button_state == wxAUI_BUTTON_STATE_PRESSED)
109 {
110 rect->x++;
111 rect->y++;
112 }
113 }
114
115
116
117 // -- GUI helper classes and functions --
118
119 class wxAuiCommandCapture : public wxEvtHandler
120 {
121 public:
122
123 wxAuiCommandCapture() { m_last_id = 0; }
124 int GetCommandId() const { return m_last_id; }
125
126 bool ProcessEvent(wxEvent& evt)
127 {
128 if (evt.GetEventType() == wxEVT_COMMAND_MENU_SELECTED)
129 {
130 m_last_id = evt.GetId();
131 return true;
132 }
133
134 if (GetNextHandler())
135 return GetNextHandler()->ProcessEvent(evt);
136
137 return false;
138 }
139
140 private:
141 int m_last_id;
142 };
143
144
145 // -- bitmaps --
146
147 #if defined( __WXMAC__ )
148 static const unsigned char close_bits[]={
149 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFE, 0x03, 0xF8, 0x01, 0xF0, 0x19, 0xF3,
150 0xB8, 0xE3, 0xF0, 0xE1, 0xE0, 0xE0, 0xF0, 0xE1, 0xB8, 0xE3, 0x19, 0xF3,
151 0x01, 0xF0, 0x03, 0xF8, 0x0F, 0xFE, 0xFF, 0xFF };
152 #elif defined( __WXGTK__)
153 static const unsigned char close_bits[]={
154 0xff, 0xff, 0xff, 0xff, 0x07, 0xf0, 0xfb, 0xef, 0xdb, 0xed, 0x8b, 0xe8,
155 0x1b, 0xec, 0x3b, 0xee, 0x1b, 0xec, 0x8b, 0xe8, 0xdb, 0xed, 0xfb, 0xef,
156 0x07, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
157 #else
158 static const unsigned char close_bits[]={
159 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe7, 0xf3, 0xcf, 0xf9,
160 0x9f, 0xfc, 0x3f, 0xfe, 0x3f, 0xfe, 0x9f, 0xfc, 0xcf, 0xf9, 0xe7, 0xf3,
161 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
162 #endif
163
164 static const unsigned char left_bits[] = {
165 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x7f, 0xfe, 0x3f, 0xfe,
166 0x1f, 0xfe, 0x0f, 0xfe, 0x1f, 0xfe, 0x3f, 0xfe, 0x7f, 0xfe, 0xff, 0xfe,
167 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
168
169 static const unsigned char right_bits[] = {
170 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xdf, 0xff, 0x9f, 0xff, 0x1f, 0xff,
171 0x1f, 0xfe, 0x1f, 0xfc, 0x1f, 0xfe, 0x1f, 0xff, 0x9f, 0xff, 0xdf, 0xff,
172 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
173
174 static const unsigned char list_bits[] = {
175 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
176 0x0f, 0xf8, 0xff, 0xff, 0x0f, 0xf8, 0x1f, 0xfc, 0x3f, 0xfe, 0x7f, 0xff,
177 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
178
179
180
181
182
183
184 // -- wxAuiDefaultTabArt class implementation --
185
186 wxAuiDefaultTabArt::wxAuiDefaultTabArt()
187 {
188 m_normal_font = *wxNORMAL_FONT;
189 m_selected_font = *wxNORMAL_FONT;
190 m_selected_font.SetWeight(wxBOLD);
191 m_measuring_font = m_selected_font;
192
193 m_fixed_tab_width = 100;
194 m_tab_ctrl_height = 0;
195
196 #if defined( __WXMAC__ ) && wxOSX_USE_COCOA_OR_CARBON
197 wxColor base_colour = wxColour( wxMacCreateCGColorFromHITheme(kThemeBrushToolbarBackground));
198 #else
199 wxColor base_colour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
200 #endif
201
202 // the base_colour is too pale to use as our base colour,
203 // so darken it a bit --
204 if ((255-base_colour.Red()) +
205 (255-base_colour.Green()) +
206 (255-base_colour.Blue()) < 60)
207 {
208 base_colour = wxAuiStepColour(base_colour, 92);
209 }
210
211 m_base_colour = base_colour;
212 wxColor border_colour = wxAuiStepColour(base_colour, 75);
213
214 m_border_pen = wxPen(border_colour);
215 m_base_colour_pen = wxPen(m_base_colour);
216 m_base_colour_brush = wxBrush(m_base_colour);
217
218 m_active_close_bmp = wxAuiBitmapFromBits(close_bits, 16, 16, *wxBLACK);
219 m_disabled_close_bmp = wxAuiBitmapFromBits(close_bits, 16, 16, wxColour(128,128,128));
220
221 m_active_left_bmp = wxAuiBitmapFromBits(left_bits, 16, 16, *wxBLACK);
222 m_disabled_left_bmp = wxAuiBitmapFromBits(left_bits, 16, 16, wxColour(128,128,128));
223
224 m_active_right_bmp = wxAuiBitmapFromBits(right_bits, 16, 16, *wxBLACK);
225 m_disabled_right_bmp = wxAuiBitmapFromBits(right_bits, 16, 16, wxColour(128,128,128));
226
227 m_active_windowlist_bmp = wxAuiBitmapFromBits(list_bits, 16, 16, *wxBLACK);
228 m_disabled_windowlist_bmp = wxAuiBitmapFromBits(list_bits, 16, 16, wxColour(128,128,128));
229
230 m_flags = 0;
231 }
232
233 wxAuiDefaultTabArt::~wxAuiDefaultTabArt()
234 {
235 }
236
237 wxAuiTabArt* wxAuiDefaultTabArt::Clone()
238 {
239 wxAuiDefaultTabArt* art = new wxAuiDefaultTabArt;
240 art->SetNormalFont(m_normal_font);
241 art->SetSelectedFont(m_selected_font);
242 art->SetMeasuringFont(m_measuring_font);
243
244 return art;
245 }
246
247 void wxAuiDefaultTabArt::SetFlags(unsigned int flags)
248 {
249 m_flags = flags;
250 }
251
252 void wxAuiDefaultTabArt::SetSizingInfo(const wxSize& tab_ctrl_size,
253 size_t tab_count)
254 {
255 m_fixed_tab_width = 100;
256
257 int tot_width = (int)tab_ctrl_size.x - GetIndentSize() - 4;
258
259 if (m_flags & wxAUI_NB_CLOSE_BUTTON)
260 tot_width -= m_active_close_bmp.GetWidth();
261 if (m_flags & wxAUI_NB_WINDOWLIST_BUTTON)
262 tot_width -= m_active_windowlist_bmp.GetWidth();
263
264 if (tab_count > 0)
265 {
266 m_fixed_tab_width = tot_width/(int)tab_count;
267 }
268
269
270 if (m_fixed_tab_width < 100)
271 m_fixed_tab_width = 100;
272
273 if (m_fixed_tab_width > tot_width/2)
274 m_fixed_tab_width = tot_width/2;
275
276 if (m_fixed_tab_width > 220)
277 m_fixed_tab_width = 220;
278
279 m_tab_ctrl_height = tab_ctrl_size.y;
280 }
281
282
283 void wxAuiDefaultTabArt::DrawBackground(wxDC& dc,
284 wxWindow* WXUNUSED(wnd),
285 const wxRect& rect)
286 {
287 // draw background
288
289 wxColor top_color = wxAuiStepColour(m_base_colour, 90);
290 wxColor bottom_color = wxAuiStepColour(m_base_colour, 170);
291 wxRect r;
292
293 if (m_flags &wxAUI_NB_BOTTOM)
294 r = wxRect(rect.x, rect.y, rect.width+2, rect.height);
295 // TODO: else if (m_flags &wxAUI_NB_LEFT) {}
296 // TODO: else if (m_flags &wxAUI_NB_RIGHT) {}
297 else //for wxAUI_NB_TOP
298 r = wxRect(rect.x, rect.y, rect.width+2, rect.height-3);
299
300 dc.GradientFillLinear(r, top_color, bottom_color, wxSOUTH);
301
302
303 // draw base lines
304
305 dc.SetPen(m_border_pen);
306 int y = rect.GetHeight();
307 int w = rect.GetWidth();
308
309 if (m_flags &wxAUI_NB_BOTTOM)
310 {
311 dc.SetBrush(wxBrush(bottom_color));
312 dc.DrawRectangle(-1, 0, w+2, 4);
313 }
314 // TODO: else if (m_flags &wxAUI_NB_LEFT) {}
315 // TODO: else if (m_flags &wxAUI_NB_RIGHT) {}
316 else //for wxAUI_NB_TOP
317 {
318 dc.SetBrush(m_base_colour_brush);
319 dc.DrawRectangle(-1, y-4, w+2, 4);
320 }
321 }
322
323
324 // DrawTab() draws an individual tab.
325 //
326 // dc - output dc
327 // in_rect - rectangle the tab should be confined to
328 // caption - tab's caption
329 // active - whether or not the tab is active
330 // out_rect - actual output rectangle
331 // x_extent - the advance x; where the next tab should start
332
333 void wxAuiDefaultTabArt::DrawTab(wxDC& dc,
334 wxWindow* wnd,
335 const wxAuiNotebookPage& page,
336 const wxRect& in_rect,
337 int close_button_state,
338 wxRect* out_tab_rect,
339 wxRect* out_button_rect,
340 int* x_extent)
341 {
342 wxCoord normal_textx, normal_texty;
343 wxCoord selected_textx, selected_texty;
344 wxCoord texty;
345
346 // if the caption is empty, measure some temporary text
347 wxString caption = page.caption;
348 if (caption.empty())
349 caption = wxT("Xj");
350
351 dc.SetFont(m_selected_font);
352 dc.GetTextExtent(caption, &selected_textx, &selected_texty);
353
354 dc.SetFont(m_normal_font);
355 dc.GetTextExtent(caption, &normal_textx, &normal_texty);
356
357 // figure out the size of the tab
358 wxSize tab_size = GetTabSize(dc,
359 wnd,
360 page.caption,
361 page.bitmap,
362 page.active,
363 close_button_state,
364 x_extent);
365
366 wxCoord tab_height = m_tab_ctrl_height - 3;
367 wxCoord tab_width = tab_size.x;
368 wxCoord tab_x = in_rect.x;
369 wxCoord tab_y = in_rect.y + in_rect.height - tab_height;
370
371
372 caption = page.caption;
373
374
375 // select pen, brush and font for the tab to be drawn
376
377 if (page.active)
378 {
379 dc.SetFont(m_selected_font);
380 texty = selected_texty;
381 }
382 else
383 {
384 dc.SetFont(m_normal_font);
385 texty = normal_texty;
386 }
387
388
389 // create points that will make the tab outline
390
391 int clip_width = tab_width;
392 if (tab_x + clip_width > in_rect.x + in_rect.width)
393 clip_width = (in_rect.x + in_rect.width) - tab_x;
394
395 /*
396 wxPoint clip_points[6];
397 clip_points[0] = wxPoint(tab_x, tab_y+tab_height-3);
398 clip_points[1] = wxPoint(tab_x, tab_y+2);
399 clip_points[2] = wxPoint(tab_x+2, tab_y);
400 clip_points[3] = wxPoint(tab_x+clip_width-1, tab_y);
401 clip_points[4] = wxPoint(tab_x+clip_width+1, tab_y+2);
402 clip_points[5] = wxPoint(tab_x+clip_width+1, tab_y+tab_height-3);
403
404 // FIXME: these ports don't provide wxRegion ctor from array of points
405 #if !defined(__WXDFB__) && !defined(__WXCOCOA__)
406 // set the clipping region for the tab --
407 wxRegion clipping_region(WXSIZEOF(clip_points), clip_points);
408 dc.SetClippingRegion(clipping_region);
409 #endif // !wxDFB && !wxCocoa
410 */
411 // since the above code above doesn't play well with WXDFB or WXCOCOA,
412 // we'll just use a rectangle for the clipping region for now --
413 dc.SetClippingRegion(tab_x, tab_y, clip_width+1, tab_height-3);
414
415
416 wxPoint border_points[6];
417 if (m_flags &wxAUI_NB_BOTTOM)
418 {
419 border_points[0] = wxPoint(tab_x, tab_y);
420 border_points[1] = wxPoint(tab_x, tab_y+tab_height-6);
421 border_points[2] = wxPoint(tab_x+2, tab_y+tab_height-4);
422 border_points[3] = wxPoint(tab_x+tab_width-2, tab_y+tab_height-4);
423 border_points[4] = wxPoint(tab_x+tab_width, tab_y+tab_height-6);
424 border_points[5] = wxPoint(tab_x+tab_width, tab_y);
425 }
426 else //if (m_flags & wxAUI_NB_TOP) {}
427 {
428 border_points[0] = wxPoint(tab_x, tab_y+tab_height-4);
429 border_points[1] = wxPoint(tab_x, tab_y+2);
430 border_points[2] = wxPoint(tab_x+2, tab_y);
431 border_points[3] = wxPoint(tab_x+tab_width-2, tab_y);
432 border_points[4] = wxPoint(tab_x+tab_width, tab_y+2);
433 border_points[5] = wxPoint(tab_x+tab_width, tab_y+tab_height-4);
434 }
435 // TODO: else if (m_flags &wxAUI_NB_LEFT) {}
436 // TODO: else if (m_flags &wxAUI_NB_RIGHT) {}
437
438 int drawn_tab_yoff = border_points[1].y;
439 int drawn_tab_height = border_points[0].y - border_points[1].y;
440
441
442 if (page.active)
443 {
444 // draw active tab
445
446 // draw base background color
447 wxRect r(tab_x, tab_y, tab_width, tab_height);
448 dc.SetPen(m_base_colour_pen);
449 dc.SetBrush(m_base_colour_brush);
450 dc.DrawRectangle(r.x+1, r.y+1, r.width-1, r.height-4);
451
452 // this white helps fill out the gradient at the top of the tab
453 dc.SetPen(*wxWHITE_PEN);
454 dc.SetBrush(*wxWHITE_BRUSH);
455 dc.DrawRectangle(r.x+2, r.y+1, r.width-3, r.height-4);
456
457 // these two points help the rounded corners appear more antialiased
458 dc.SetPen(m_base_colour_pen);
459 dc.DrawPoint(r.x+2, r.y+1);
460 dc.DrawPoint(r.x+r.width-2, r.y+1);
461
462 // set rectangle down a bit for gradient drawing
463 r.SetHeight(r.GetHeight()/2);
464 r.x += 2;
465 r.width -= 2;
466 r.y += r.height;
467 r.y -= 2;
468
469 // draw gradient background
470 wxColor top_color = *wxWHITE;
471 wxColor bottom_color = m_base_colour;
472 dc.GradientFillLinear(r, bottom_color, top_color, wxNORTH);
473 }
474 else
475 {
476 // draw inactive tab
477
478 wxRect r(tab_x, tab_y+1, tab_width, tab_height-3);
479
480 // start the gradent up a bit and leave the inside border inset
481 // by a pixel for a 3D look. Only the top half of the inactive
482 // tab will have a slight gradient
483 r.x += 3;
484 r.y++;
485 r.width -= 4;
486 r.height /= 2;
487 r.height--;
488
489 // -- draw top gradient fill for glossy look
490 wxColor top_color = m_base_colour;
491 wxColor bottom_color = wxAuiStepColour(top_color, 160);
492 dc.GradientFillLinear(r, bottom_color, top_color, wxNORTH);
493
494 r.y += r.height;
495 r.y--;
496
497 // -- draw bottom fill for glossy look
498 top_color = m_base_colour;
499 bottom_color = m_base_colour;
500 dc.GradientFillLinear(r, top_color, bottom_color, wxSOUTH);
501 }
502
503 // draw tab outline
504 dc.SetPen(m_border_pen);
505 dc.SetBrush(*wxTRANSPARENT_BRUSH);
506 dc.DrawPolygon(WXSIZEOF(border_points), border_points);
507
508 // there are two horizontal grey lines at the bottom of the tab control,
509 // this gets rid of the top one of those lines in the tab control
510 if (page.active)
511 {
512 if (m_flags &wxAUI_NB_BOTTOM)
513 dc.SetPen(wxPen(wxColour(wxAuiStepColour(m_base_colour, 170))));
514 // TODO: else if (m_flags &wxAUI_NB_LEFT) {}
515 // TODO: else if (m_flags &wxAUI_NB_RIGHT) {}
516 else //for wxAUI_NB_TOP
517 dc.SetPen(m_base_colour_pen);
518 dc.DrawLine(border_points[0].x+1,
519 border_points[0].y,
520 border_points[5].x,
521 border_points[5].y);
522 }
523
524
525 int text_offset = tab_x + 8;
526 int close_button_width = 0;
527 if (close_button_state != wxAUI_BUTTON_STATE_HIDDEN)
528 {
529 close_button_width = m_active_close_bmp.GetWidth();
530 }
531
532 int bitmap_offset = 0;
533 if (page.bitmap.IsOk())
534 {
535 bitmap_offset = tab_x + 8;
536
537 // draw bitmap
538 dc.DrawBitmap(page.bitmap,
539 bitmap_offset,
540 drawn_tab_yoff + (drawn_tab_height/2) - (page.bitmap.GetHeight()/2),
541 true);
542
543 text_offset = bitmap_offset + page.bitmap.GetWidth();
544 text_offset += 3; // bitmap padding
545
546 }
547 else
548 {
549 text_offset = tab_x + 8;
550 }
551
552
553 wxString draw_text = wxAuiChopText(dc,
554 caption,
555 tab_width - (text_offset-tab_x) - close_button_width);
556
557 // draw tab text
558 dc.DrawText(draw_text,
559 text_offset,
560 drawn_tab_yoff + (drawn_tab_height)/2 - (texty/2) - 1);
561
562 // draw focus rectangle
563 if (page.active && (wnd->FindFocus() == wnd))
564 {
565 wxRect focusRectText(text_offset, (drawn_tab_yoff + (drawn_tab_height)/2 - (texty/2) - 1),
566 selected_textx, selected_texty);
567
568 wxRect focusRect;
569 wxRect focusRectBitmap;
570
571 if (page.bitmap.IsOk())
572 focusRectBitmap = wxRect(bitmap_offset, drawn_tab_yoff + (drawn_tab_height/2) - (page.bitmap.GetHeight()/2),
573 page.bitmap.GetWidth(), page.bitmap.GetHeight());
574
575 if (page.bitmap.IsOk() && draw_text.IsEmpty())
576 focusRect = focusRectBitmap;
577 else if (!page.bitmap.IsOk() && !draw_text.IsEmpty())
578 focusRect = focusRectText;
579 else if (page.bitmap.IsOk() && !draw_text.IsEmpty())
580 focusRect = focusRectText.Union(focusRectBitmap);
581
582 focusRect.Inflate(2, 2);
583
584 wxRendererNative::Get().DrawFocusRect(wnd, dc, focusRect, 0);
585 }
586
587 // draw close button if necessary
588 if (close_button_state != wxAUI_BUTTON_STATE_HIDDEN)
589 {
590 wxBitmap bmp = m_disabled_close_bmp;
591
592 if (close_button_state == wxAUI_BUTTON_STATE_HOVER ||
593 close_button_state == wxAUI_BUTTON_STATE_PRESSED)
594 {
595 bmp = m_active_close_bmp;
596 }
597
598 wxRect rect(tab_x + tab_width - close_button_width - 1,
599 tab_y + (tab_height/2) - (bmp.GetHeight()/2),
600 close_button_width,
601 tab_height);
602 IndentPressedBitmap(&rect, close_button_state);
603 dc.DrawBitmap(bmp, rect.x, rect.y, true);
604
605 *out_button_rect = rect;
606 }
607
608 *out_tab_rect = wxRect(tab_x, tab_y, tab_width, tab_height);
609
610 dc.DestroyClippingRegion();
611 }
612
613 int wxAuiDefaultTabArt::GetIndentSize()
614 {
615 return 5;
616 }
617
618 wxSize wxAuiDefaultTabArt::GetTabSize(wxDC& dc,
619 wxWindow* WXUNUSED(wnd),
620 const wxString& caption,
621 const wxBitmap& bitmap,
622 bool WXUNUSED(active),
623 int close_button_state,
624 int* x_extent)
625 {
626 wxCoord measured_textx, measured_texty, tmp;
627
628 dc.SetFont(m_measuring_font);
629 dc.GetTextExtent(caption, &measured_textx, &measured_texty);
630
631 dc.GetTextExtent(wxT("ABCDEFXj"), &tmp, &measured_texty);
632
633 // add padding around the text
634 wxCoord tab_width = measured_textx;
635 wxCoord tab_height = measured_texty;
636
637 // if the close button is showing, add space for it
638 if (close_button_state != wxAUI_BUTTON_STATE_HIDDEN)
639 tab_width += m_active_close_bmp.GetWidth() + 3;
640
641 // if there's a bitmap, add space for it
642 if (bitmap.IsOk())
643 {
644 tab_width += bitmap.GetWidth();
645 tab_width += 3; // right side bitmap padding
646 tab_height = wxMax(tab_height, bitmap.GetHeight());
647 }
648
649 // add padding
650 tab_width += 16;
651 tab_height += 10;
652
653 if (m_flags & wxAUI_NB_TAB_FIXED_WIDTH)
654 {
655 tab_width = m_fixed_tab_width;
656 }
657
658 *x_extent = tab_width;
659
660 return wxSize(tab_width, tab_height);
661 }
662
663
664 void wxAuiDefaultTabArt::DrawButton(wxDC& dc,
665 wxWindow* WXUNUSED(wnd),
666 const wxRect& in_rect,
667 int bitmap_id,
668 int button_state,
669 int orientation,
670 wxRect* out_rect)
671 {
672 wxBitmap bmp;
673 wxRect rect;
674
675 switch (bitmap_id)
676 {
677 case wxAUI_BUTTON_CLOSE:
678 if (button_state & wxAUI_BUTTON_STATE_DISABLED)
679 bmp = m_disabled_close_bmp;
680 else
681 bmp = m_active_close_bmp;
682 break;
683 case wxAUI_BUTTON_LEFT:
684 if (button_state & wxAUI_BUTTON_STATE_DISABLED)
685 bmp = m_disabled_left_bmp;
686 else
687 bmp = m_active_left_bmp;
688 break;
689 case wxAUI_BUTTON_RIGHT:
690 if (button_state & wxAUI_BUTTON_STATE_DISABLED)
691 bmp = m_disabled_right_bmp;
692 else
693 bmp = m_active_right_bmp;
694 break;
695 case wxAUI_BUTTON_WINDOWLIST:
696 if (button_state & wxAUI_BUTTON_STATE_DISABLED)
697 bmp = m_disabled_windowlist_bmp;
698 else
699 bmp = m_active_windowlist_bmp;
700 break;
701 }
702
703
704 if (!bmp.IsOk())
705 return;
706
707 rect = in_rect;
708
709 if (orientation == wxLEFT)
710 {
711 rect.SetX(in_rect.x);
712 rect.SetY(((in_rect.y + in_rect.height)/2) - (bmp.GetHeight()/2));
713 rect.SetWidth(bmp.GetWidth());
714 rect.SetHeight(bmp.GetHeight());
715 }
716 else
717 {
718 rect = wxRect(in_rect.x + in_rect.width - bmp.GetWidth(),
719 ((in_rect.y + in_rect.height)/2) - (bmp.GetHeight()/2),
720 bmp.GetWidth(), bmp.GetHeight());
721 }
722
723 IndentPressedBitmap(&rect, button_state);
724 dc.DrawBitmap(bmp, rect.x, rect.y, true);
725
726 *out_rect = rect;
727 }
728
729 int wxAuiDefaultTabArt::ShowDropDown(wxWindow* wnd,
730 const wxAuiNotebookPageArray& pages,
731 int /*active_idx*/)
732 {
733 wxMenu menuPopup;
734
735 size_t i, count = pages.GetCount();
736 for (i = 0; i < count; ++i)
737 {
738 const wxAuiNotebookPage& page = pages.Item(i);
739 wxString caption = page.caption;
740
741 // if there is no caption, make it a space. This will prevent
742 // an assert in the menu code.
743 if (caption.IsEmpty())
744 caption = wxT(" ");
745
746 wxMenuItem* item = new wxMenuItem(NULL, 1000+i, caption);
747 if (page.bitmap.IsOk())
748 item->SetBitmap(page.bitmap);
749 menuPopup.Append(item);
750 }
751
752 // find out where to put the popup menu of window items
753 wxPoint pt = ::wxGetMousePosition();
754 pt = wnd->ScreenToClient(pt);
755
756 // find out the screen coordinate at the bottom of the tab ctrl
757 wxRect cli_rect = wnd->GetClientRect();
758 pt.y = cli_rect.y + cli_rect.height;
759
760 wxAuiCommandCapture* cc = new wxAuiCommandCapture;
761 wnd->PushEventHandler(cc);
762 wnd->PopupMenu(&menuPopup, pt);
763 int command = cc->GetCommandId();
764 wnd->PopEventHandler(true);
765
766 if (command >= 1000)
767 return command-1000;
768
769 return -1;
770 }
771
772 int wxAuiDefaultTabArt::GetBestTabCtrlSize(wxWindow* wnd,
773 const wxAuiNotebookPageArray& pages,
774 const wxSize& required_bmp_size)
775 {
776 wxClientDC dc(wnd);
777 dc.SetFont(m_measuring_font);
778
779 // sometimes a standard bitmap size needs to be enforced, especially
780 // if some tabs have bitmaps and others don't. This is important because
781 // it prevents the tab control from resizing when tabs are added.
782 wxBitmap measure_bmp;
783 if (required_bmp_size.IsFullySpecified())
784 {
785 measure_bmp.Create(required_bmp_size.x,
786 required_bmp_size.y);
787 }
788
789
790 int max_y = 0;
791 size_t i, page_count = pages.GetCount();
792 for (i = 0; i < page_count; ++i)
793 {
794 wxAuiNotebookPage& page = pages.Item(i);
795
796 wxBitmap bmp;
797 if (measure_bmp.IsOk())
798 bmp = measure_bmp;
799 else
800 bmp = page.bitmap;
801
802 // we don't use the caption text because we don't
803 // want tab heights to be different in the case
804 // of a very short piece of text on one tab and a very
805 // tall piece of text on another tab
806 int x_ext = 0;
807 wxSize s = GetTabSize(dc,
808 wnd,
809 wxT("ABCDEFGHIj"),
810 bmp,
811 true,
812 wxAUI_BUTTON_STATE_HIDDEN,
813 &x_ext);
814
815 max_y = wxMax(max_y, s.y);
816 }
817
818 return max_y+2;
819 }
820
821 void wxAuiDefaultTabArt::SetNormalFont(const wxFont& font)
822 {
823 m_normal_font = font;
824 }
825
826 void wxAuiDefaultTabArt::SetSelectedFont(const wxFont& font)
827 {
828 m_selected_font = font;
829 }
830
831 void wxAuiDefaultTabArt::SetMeasuringFont(const wxFont& font)
832 {
833 m_measuring_font = font;
834 }
835
836
837 // -- wxAuiSimpleTabArt class implementation --
838
839 wxAuiSimpleTabArt::wxAuiSimpleTabArt()
840 {
841 m_normal_font = *wxNORMAL_FONT;
842 m_selected_font = *wxNORMAL_FONT;
843 m_selected_font.SetWeight(wxBOLD);
844 m_measuring_font = m_selected_font;
845
846 m_flags = 0;
847 m_fixed_tab_width = 100;
848
849 wxColour base_colour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
850
851 wxColour background_colour = base_colour;
852 wxColour normaltab_colour = base_colour;
853 wxColour selectedtab_colour = *wxWHITE;
854
855 m_bkbrush = wxBrush(background_colour);
856 m_normal_bkbrush = wxBrush(normaltab_colour);
857 m_normal_bkpen = wxPen(normaltab_colour);
858 m_selected_bkbrush = wxBrush(selectedtab_colour);
859 m_selected_bkpen = wxPen(selectedtab_colour);
860
861 m_active_close_bmp = wxAuiBitmapFromBits(close_bits, 16, 16, *wxBLACK);
862 m_disabled_close_bmp = wxAuiBitmapFromBits(close_bits, 16, 16, wxColour(128,128,128));
863
864 m_active_left_bmp = wxAuiBitmapFromBits(left_bits, 16, 16, *wxBLACK);
865 m_disabled_left_bmp = wxAuiBitmapFromBits(left_bits, 16, 16, wxColour(128,128,128));
866
867 m_active_right_bmp = wxAuiBitmapFromBits(right_bits, 16, 16, *wxBLACK);
868 m_disabled_right_bmp = wxAuiBitmapFromBits(right_bits, 16, 16, wxColour(128,128,128));
869
870 m_active_windowlist_bmp = wxAuiBitmapFromBits(list_bits, 16, 16, *wxBLACK);
871 m_disabled_windowlist_bmp = wxAuiBitmapFromBits(list_bits, 16, 16, wxColour(128,128,128));
872
873 }
874
875 wxAuiSimpleTabArt::~wxAuiSimpleTabArt()
876 {
877 }
878
879 wxAuiTabArt* wxAuiSimpleTabArt::Clone()
880 {
881 return static_cast<wxAuiTabArt*>(new wxAuiSimpleTabArt);
882 }
883
884
885 void wxAuiSimpleTabArt::SetFlags(unsigned int flags)
886 {
887 m_flags = flags;
888 }
889
890 void wxAuiSimpleTabArt::SetSizingInfo(const wxSize& tab_ctrl_size,
891 size_t tab_count)
892 {
893 m_fixed_tab_width = 100;
894
895 int tot_width = (int)tab_ctrl_size.x - GetIndentSize() - 4;
896
897 if (m_flags & wxAUI_NB_CLOSE_BUTTON)
898 tot_width -= m_active_close_bmp.GetWidth();
899 if (m_flags & wxAUI_NB_WINDOWLIST_BUTTON)
900 tot_width -= m_active_windowlist_bmp.GetWidth();
901
902 if (tab_count > 0)
903 {
904 m_fixed_tab_width = tot_width/(int)tab_count;
905 }
906
907
908 if (m_fixed_tab_width < 100)
909 m_fixed_tab_width = 100;
910
911 if (m_fixed_tab_width > tot_width/2)
912 m_fixed_tab_width = tot_width/2;
913
914 if (m_fixed_tab_width > 220)
915 m_fixed_tab_width = 220;
916 }
917
918 void wxAuiSimpleTabArt::DrawBackground(wxDC& dc,
919 wxWindow* WXUNUSED(wnd),
920 const wxRect& rect)
921 {
922 // draw background
923 dc.SetBrush(m_bkbrush);
924 dc.SetPen(*wxTRANSPARENT_PEN);
925 dc.DrawRectangle(-1, -1, rect.GetWidth()+2, rect.GetHeight()+2);
926
927 // draw base line
928 dc.SetPen(*wxGREY_PEN);
929 dc.DrawLine(0, rect.GetHeight()-1, rect.GetWidth(), rect.GetHeight()-1);
930 }
931
932
933 // DrawTab() draws an individual tab.
934 //
935 // dc - output dc
936 // in_rect - rectangle the tab should be confined to
937 // caption - tab's caption
938 // active - whether or not the tab is active
939 // out_rect - actual output rectangle
940 // x_extent - the advance x; where the next tab should start
941
942 void wxAuiSimpleTabArt::DrawTab(wxDC& dc,
943 wxWindow* wnd,
944 const wxAuiNotebookPage& page,
945 const wxRect& in_rect,
946 int close_button_state,
947 wxRect* out_tab_rect,
948 wxRect* out_button_rect,
949 int* x_extent)
950 {
951 wxCoord normal_textx, normal_texty;
952 wxCoord selected_textx, selected_texty;
953 wxCoord textx, texty;
954
955 // if the caption is empty, measure some temporary text
956 wxString caption = page.caption;
957 if (caption.empty())
958 caption = wxT("Xj");
959
960 dc.SetFont(m_selected_font);
961 dc.GetTextExtent(caption, &selected_textx, &selected_texty);
962
963 dc.SetFont(m_normal_font);
964 dc.GetTextExtent(caption, &normal_textx, &normal_texty);
965
966 // figure out the size of the tab
967 wxSize tab_size = GetTabSize(dc,
968 wnd,
969 page.caption,
970 page.bitmap,
971 page.active,
972 close_button_state,
973 x_extent);
974
975 wxCoord tab_height = tab_size.y;
976 wxCoord tab_width = tab_size.x;
977 wxCoord tab_x = in_rect.x;
978 wxCoord tab_y = in_rect.y + in_rect.height - tab_height;
979
980 caption = page.caption;
981
982 // select pen, brush and font for the tab to be drawn
983
984 if (page.active)
985 {
986 dc.SetPen(m_selected_bkpen);
987 dc.SetBrush(m_selected_bkbrush);
988 dc.SetFont(m_selected_font);
989 textx = selected_textx;
990 texty = selected_texty;
991 }
992 else
993 {
994 dc.SetPen(m_normal_bkpen);
995 dc.SetBrush(m_normal_bkbrush);
996 dc.SetFont(m_normal_font);
997 textx = normal_textx;
998 texty = normal_texty;
999 }
1000
1001
1002 // -- draw line --
1003
1004 wxPoint points[7];
1005 points[0].x = tab_x;
1006 points[0].y = tab_y + tab_height - 1;
1007 points[1].x = tab_x + tab_height - 3;
1008 points[1].y = tab_y + 2;
1009 points[2].x = tab_x + tab_height + 3;
1010 points[2].y = tab_y;
1011 points[3].x = tab_x + tab_width - 2;
1012 points[3].y = tab_y;
1013 points[4].x = tab_x + tab_width;
1014 points[4].y = tab_y + 2;
1015 points[5].x = tab_x + tab_width;
1016 points[5].y = tab_y + tab_height - 1;
1017 points[6] = points[0];
1018
1019 dc.SetClippingRegion(in_rect);
1020
1021 dc.DrawPolygon(WXSIZEOF(points) - 1, points);
1022
1023 dc.SetPen(*wxGREY_PEN);
1024
1025 //dc.DrawLines(active ? WXSIZEOF(points) - 1 : WXSIZEOF(points), points);
1026 dc.DrawLines(WXSIZEOF(points), points);
1027
1028
1029 int text_offset;
1030
1031 int close_button_width = 0;
1032 if (close_button_state != wxAUI_BUTTON_STATE_HIDDEN)
1033 {
1034 close_button_width = m_active_close_bmp.GetWidth();
1035 text_offset = tab_x + (tab_height/2) + ((tab_width-close_button_width)/2) - (textx/2);
1036 }
1037 else
1038 {
1039 text_offset = tab_x + (tab_height/3) + (tab_width/2) - (textx/2);
1040 }
1041
1042 // set minimum text offset
1043 if (text_offset < tab_x + tab_height)
1044 text_offset = tab_x + tab_height;
1045
1046 // chop text if necessary
1047 wxString draw_text = wxAuiChopText(dc,
1048 caption,
1049 tab_width - (text_offset-tab_x) - close_button_width);
1050
1051 // draw tab text
1052 dc.DrawText(draw_text,
1053 text_offset,
1054 (tab_y + tab_height)/2 - (texty/2) + 1);
1055
1056
1057 // draw focus rectangle
1058 if (page.active && (wnd->FindFocus() == wnd))
1059 {
1060 wxRect focusRect(text_offset, ((tab_y + tab_height)/2 - (texty/2) + 1),
1061 selected_textx, selected_texty);
1062
1063 focusRect.Inflate(2, 2);
1064
1065 wxRendererNative::Get().DrawFocusRect(wnd, dc, focusRect, 0);
1066 }
1067
1068 // draw close button if necessary
1069 if (close_button_state != wxAUI_BUTTON_STATE_HIDDEN)
1070 {
1071 wxBitmap bmp;
1072 if (page.active)
1073 bmp = m_active_close_bmp;
1074 else
1075 bmp = m_disabled_close_bmp;
1076
1077 wxRect rect(tab_x + tab_width - close_button_width - 1,
1078 tab_y + (tab_height/2) - (bmp.GetHeight()/2) + 1,
1079 close_button_width,
1080 tab_height - 1);
1081 DrawButtons(dc, rect, bmp, *wxWHITE, close_button_state);
1082
1083 *out_button_rect = rect;
1084 }
1085
1086
1087 *out_tab_rect = wxRect(tab_x, tab_y, tab_width, tab_height);
1088
1089 dc.DestroyClippingRegion();
1090 }
1091
1092 int wxAuiSimpleTabArt::GetIndentSize()
1093 {
1094 return 0;
1095 }
1096
1097 wxSize wxAuiSimpleTabArt::GetTabSize(wxDC& dc,
1098 wxWindow* WXUNUSED(wnd),
1099 const wxString& caption,
1100 const wxBitmap& WXUNUSED(bitmap),
1101 bool WXUNUSED(active),
1102 int close_button_state,
1103 int* x_extent)
1104 {
1105 wxCoord measured_textx, measured_texty;
1106
1107 dc.SetFont(m_measuring_font);
1108 dc.GetTextExtent(caption, &measured_textx, &measured_texty);
1109
1110 wxCoord tab_height = measured_texty + 4;
1111 wxCoord tab_width = measured_textx + tab_height + 5;
1112
1113 if (close_button_state != wxAUI_BUTTON_STATE_HIDDEN)
1114 tab_width += m_active_close_bmp.GetWidth();
1115
1116 if (m_flags & wxAUI_NB_TAB_FIXED_WIDTH)
1117 {
1118 tab_width = m_fixed_tab_width;
1119 }
1120
1121 *x_extent = tab_width - (tab_height/2) - 1;
1122
1123 return wxSize(tab_width, tab_height);
1124 }
1125
1126
1127 void wxAuiSimpleTabArt::DrawButton(wxDC& dc,
1128 wxWindow* WXUNUSED(wnd),
1129 const wxRect& in_rect,
1130 int bitmap_id,
1131 int button_state,
1132 int orientation,
1133 wxRect* out_rect)
1134 {
1135 wxBitmap bmp;
1136 wxRect rect;
1137
1138 switch (bitmap_id)
1139 {
1140 case wxAUI_BUTTON_CLOSE:
1141 if (button_state & wxAUI_BUTTON_STATE_DISABLED)
1142 bmp = m_disabled_close_bmp;
1143 else
1144 bmp = m_active_close_bmp;
1145 break;
1146 case wxAUI_BUTTON_LEFT:
1147 if (button_state & wxAUI_BUTTON_STATE_DISABLED)
1148 bmp = m_disabled_left_bmp;
1149 else
1150 bmp = m_active_left_bmp;
1151 break;
1152 case wxAUI_BUTTON_RIGHT:
1153 if (button_state & wxAUI_BUTTON_STATE_DISABLED)
1154 bmp = m_disabled_right_bmp;
1155 else
1156 bmp = m_active_right_bmp;
1157 break;
1158 case wxAUI_BUTTON_WINDOWLIST:
1159 if (button_state & wxAUI_BUTTON_STATE_DISABLED)
1160 bmp = m_disabled_windowlist_bmp;
1161 else
1162 bmp = m_active_windowlist_bmp;
1163 break;
1164 }
1165
1166 if (!bmp.IsOk())
1167 return;
1168
1169 rect = in_rect;
1170
1171 if (orientation == wxLEFT)
1172 {
1173 rect.SetX(in_rect.x);
1174 rect.SetY(((in_rect.y + in_rect.height)/2) - (bmp.GetHeight()/2));
1175 rect.SetWidth(bmp.GetWidth());
1176 rect.SetHeight(bmp.GetHeight());
1177 }
1178 else
1179 {
1180 rect = wxRect(in_rect.x + in_rect.width - bmp.GetWidth(),
1181 ((in_rect.y + in_rect.height)/2) - (bmp.GetHeight()/2),
1182 bmp.GetWidth(), bmp.GetHeight());
1183 }
1184
1185
1186 DrawButtons(dc, rect, bmp, *wxWHITE, button_state);
1187
1188 *out_rect = rect;
1189 }
1190
1191 int wxAuiSimpleTabArt::ShowDropDown(wxWindow* wnd,
1192 const wxAuiNotebookPageArray& pages,
1193 int active_idx)
1194 {
1195 wxMenu menuPopup;
1196
1197 size_t i, count = pages.GetCount();
1198 for (i = 0; i < count; ++i)
1199 {
1200 const wxAuiNotebookPage& page = pages.Item(i);
1201 menuPopup.AppendCheckItem(1000+i, page.caption);
1202 }
1203
1204 if (active_idx != -1)
1205 {
1206 menuPopup.Check(1000+active_idx, true);
1207 }
1208
1209 // find out where to put the popup menu of window
1210 // items. Subtract 100 for now to center the menu
1211 // a bit, until a better mechanism can be implemented
1212 wxPoint pt = ::wxGetMousePosition();
1213 pt = wnd->ScreenToClient(pt);
1214 if (pt.x < 100)
1215 pt.x = 0;
1216 else
1217 pt.x -= 100;
1218
1219 // find out the screen coordinate at the bottom of the tab ctrl
1220 wxRect cli_rect = wnd->GetClientRect();
1221 pt.y = cli_rect.y + cli_rect.height;
1222
1223 wxAuiCommandCapture* cc = new wxAuiCommandCapture;
1224 wnd->PushEventHandler(cc);
1225 wnd->PopupMenu(&menuPopup, pt);
1226 int command = cc->GetCommandId();
1227 wnd->PopEventHandler(true);
1228
1229 if (command >= 1000)
1230 return command-1000;
1231
1232 return -1;
1233 }
1234
1235 int wxAuiSimpleTabArt::GetBestTabCtrlSize(wxWindow* wnd,
1236 const wxAuiNotebookPageArray& WXUNUSED(pages),
1237 const wxSize& WXUNUSED(required_bmp_size))
1238 {
1239 wxClientDC dc(wnd);
1240 dc.SetFont(m_measuring_font);
1241 int x_ext = 0;
1242 wxSize s = GetTabSize(dc,
1243 wnd,
1244 wxT("ABCDEFGHIj"),
1245 wxNullBitmap,
1246 true,
1247 wxAUI_BUTTON_STATE_HIDDEN,
1248 &x_ext);
1249 return s.y+3;
1250 }
1251
1252 void wxAuiSimpleTabArt::SetNormalFont(const wxFont& font)
1253 {
1254 m_normal_font = font;
1255 }
1256
1257 void wxAuiSimpleTabArt::SetSelectedFont(const wxFont& font)
1258 {
1259 m_selected_font = font;
1260 }
1261
1262 void wxAuiSimpleTabArt::SetMeasuringFont(const wxFont& font)
1263 {
1264 m_measuring_font = font;
1265 }
1266
1267
1268
1269
1270 // -- wxAuiTabContainer class implementation --
1271
1272
1273 // wxAuiTabContainer is a class which contains information about each
1274 // tab. It also can render an entire tab control to a specified DC.
1275 // It's not a window class itself, because this code will be used by
1276 // the wxFrameMananger, where it is disadvantageous to have separate
1277 // windows for each tab control in the case of "docked tabs"
1278
1279 // A derived class, wxAuiTabCtrl, is an actual wxWindow-derived window
1280 // which can be used as a tab control in the normal sense.
1281
1282
1283 wxAuiTabContainer::wxAuiTabContainer()
1284 {
1285 m_tab_offset = 0;
1286 m_flags = 0;
1287 m_art = new wxAuiDefaultTabArt;
1288
1289 AddButton(wxAUI_BUTTON_LEFT, wxLEFT);
1290 AddButton(wxAUI_BUTTON_RIGHT, wxRIGHT);
1291 AddButton(wxAUI_BUTTON_WINDOWLIST, wxRIGHT);
1292 AddButton(wxAUI_BUTTON_CLOSE, wxRIGHT);
1293 }
1294
1295 wxAuiTabContainer::~wxAuiTabContainer()
1296 {
1297 delete m_art;
1298 }
1299
1300 void wxAuiTabContainer::SetArtProvider(wxAuiTabArt* art)
1301 {
1302 delete m_art;
1303 m_art = art;
1304
1305 if (m_art)
1306 {
1307 m_art->SetFlags(m_flags);
1308 }
1309 }
1310
1311 wxAuiTabArt* wxAuiTabContainer::GetArtProvider() const
1312 {
1313 return m_art;
1314 }
1315
1316 void wxAuiTabContainer::SetFlags(unsigned int flags)
1317 {
1318 m_flags = flags;
1319
1320 // check for new close button settings
1321 RemoveButton(wxAUI_BUTTON_LEFT);
1322 RemoveButton(wxAUI_BUTTON_RIGHT);
1323 RemoveButton(wxAUI_BUTTON_WINDOWLIST);
1324 RemoveButton(wxAUI_BUTTON_CLOSE);
1325
1326
1327 if (flags & wxAUI_NB_SCROLL_BUTTONS)
1328 {
1329 AddButton(wxAUI_BUTTON_LEFT, wxLEFT);
1330 AddButton(wxAUI_BUTTON_RIGHT, wxRIGHT);
1331 }
1332
1333 if (flags & wxAUI_NB_WINDOWLIST_BUTTON)
1334 {
1335 AddButton(wxAUI_BUTTON_WINDOWLIST, wxRIGHT);
1336 }
1337
1338 if (flags & wxAUI_NB_CLOSE_BUTTON)
1339 {
1340 AddButton(wxAUI_BUTTON_CLOSE, wxRIGHT);
1341 }
1342
1343 if (m_art)
1344 {
1345 m_art->SetFlags(m_flags);
1346 }
1347 }
1348
1349 unsigned int wxAuiTabContainer::GetFlags() const
1350 {
1351 return m_flags;
1352 }
1353
1354
1355 void wxAuiTabContainer::SetNormalFont(const wxFont& font)
1356 {
1357 m_art->SetNormalFont(font);
1358 }
1359
1360 void wxAuiTabContainer::SetSelectedFont(const wxFont& font)
1361 {
1362 m_art->SetSelectedFont(font);
1363 }
1364
1365 void wxAuiTabContainer::SetMeasuringFont(const wxFont& font)
1366 {
1367 m_art->SetMeasuringFont(font);
1368 }
1369
1370 void wxAuiTabContainer::SetRect(const wxRect& rect)
1371 {
1372 m_rect = rect;
1373
1374 if (m_art)
1375 {
1376 m_art->SetSizingInfo(rect.GetSize(), m_pages.GetCount());
1377 }
1378 }
1379
1380 bool wxAuiTabContainer::AddPage(wxWindow* page,
1381 const wxAuiNotebookPage& info)
1382 {
1383 wxAuiNotebookPage page_info;
1384 page_info = info;
1385 page_info.window = page;
1386
1387 m_pages.Add(page_info);
1388
1389 // let the art provider know how many pages we have
1390 if (m_art)
1391 {
1392 m_art->SetSizingInfo(m_rect.GetSize(), m_pages.GetCount());
1393 }
1394
1395 return true;
1396 }
1397
1398 bool wxAuiTabContainer::InsertPage(wxWindow* page,
1399 const wxAuiNotebookPage& info,
1400 size_t idx)
1401 {
1402 wxAuiNotebookPage page_info;
1403 page_info = info;
1404 page_info.window = page;
1405
1406 if (idx >= m_pages.GetCount())
1407 m_pages.Add(page_info);
1408 else
1409 m_pages.Insert(page_info, idx);
1410
1411 // let the art provider know how many pages we have
1412 if (m_art)
1413 {
1414 m_art->SetSizingInfo(m_rect.GetSize(), m_pages.GetCount());
1415 }
1416
1417 return true;
1418 }
1419
1420 bool wxAuiTabContainer::MovePage(wxWindow* page,
1421 size_t new_idx)
1422 {
1423 int idx = GetIdxFromWindow(page);
1424 if (idx == -1)
1425 return false;
1426
1427 // get page entry, make a copy of it
1428 wxAuiNotebookPage p = GetPage(idx);
1429
1430 // remove old page entry
1431 RemovePage(page);
1432
1433 // insert page where it should be
1434 InsertPage(page, p, new_idx);
1435
1436 return true;
1437 }
1438
1439 bool wxAuiTabContainer::RemovePage(wxWindow* wnd)
1440 {
1441 size_t i, page_count = m_pages.GetCount();
1442 for (i = 0; i < page_count; ++i)
1443 {
1444 wxAuiNotebookPage& page = m_pages.Item(i);
1445 if (page.window == wnd)
1446 {
1447 m_pages.RemoveAt(i);
1448
1449 // let the art provider know how many pages we have
1450 if (m_art)
1451 {
1452 m_art->SetSizingInfo(m_rect.GetSize(), m_pages.GetCount());
1453 }
1454
1455 return true;
1456 }
1457 }
1458
1459 return false;
1460 }
1461
1462 bool wxAuiTabContainer::SetActivePage(wxWindow* wnd)
1463 {
1464 bool found = false;
1465
1466 size_t i, page_count = m_pages.GetCount();
1467 for (i = 0; i < page_count; ++i)
1468 {
1469 wxAuiNotebookPage& page = m_pages.Item(i);
1470 if (page.window == wnd)
1471 {
1472 page.active = true;
1473 found = true;
1474 }
1475 else
1476 {
1477 page.active = false;
1478 }
1479 }
1480
1481 return found;
1482 }
1483
1484 void wxAuiTabContainer::SetNoneActive()
1485 {
1486 size_t i, page_count = m_pages.GetCount();
1487 for (i = 0; i < page_count; ++i)
1488 {
1489 wxAuiNotebookPage& page = m_pages.Item(i);
1490 page.active = false;
1491 }
1492 }
1493
1494 bool wxAuiTabContainer::SetActivePage(size_t page)
1495 {
1496 if (page >= m_pages.GetCount())
1497 return false;
1498
1499 return SetActivePage(m_pages.Item(page).window);
1500 }
1501
1502 int wxAuiTabContainer::GetActivePage() const
1503 {
1504 size_t i, page_count = m_pages.GetCount();
1505 for (i = 0; i < page_count; ++i)
1506 {
1507 wxAuiNotebookPage& page = m_pages.Item(i);
1508 if (page.active)
1509 return i;
1510 }
1511
1512 return -1;
1513 }
1514
1515 wxWindow* wxAuiTabContainer::GetWindowFromIdx(size_t idx) const
1516 {
1517 if (idx >= m_pages.GetCount())
1518 return NULL;
1519
1520 return m_pages[idx].window;
1521 }
1522
1523 int wxAuiTabContainer::GetIdxFromWindow(wxWindow* wnd) const
1524 {
1525 const size_t page_count = m_pages.GetCount();
1526 for ( size_t i = 0; i < page_count; ++i )
1527 {
1528 wxAuiNotebookPage& page = m_pages.Item(i);
1529 if (page.window == wnd)
1530 return i;
1531 }
1532 return wxNOT_FOUND;
1533 }
1534
1535 wxAuiNotebookPage& wxAuiTabContainer::GetPage(size_t idx)
1536 {
1537 wxASSERT_MSG(idx < m_pages.GetCount(), wxT("Invalid Page index"));
1538
1539 return m_pages[idx];
1540 }
1541
1542 const wxAuiNotebookPage& wxAuiTabContainer::GetPage(size_t idx) const
1543 {
1544 wxASSERT_MSG(idx < m_pages.GetCount(), wxT("Invalid Page index"));
1545
1546 return m_pages[idx];
1547 }
1548
1549 wxAuiNotebookPageArray& wxAuiTabContainer::GetPages()
1550 {
1551 return m_pages;
1552 }
1553
1554 size_t wxAuiTabContainer::GetPageCount() const
1555 {
1556 return m_pages.GetCount();
1557 }
1558
1559 void wxAuiTabContainer::AddButton(int id,
1560 int location,
1561 const wxBitmap& normal_bitmap,
1562 const wxBitmap& disabled_bitmap)
1563 {
1564 wxAuiTabContainerButton button;
1565 button.id = id;
1566 button.bitmap = normal_bitmap;
1567 button.dis_bitmap = disabled_bitmap;
1568 button.location = location;
1569 button.cur_state = wxAUI_BUTTON_STATE_NORMAL;
1570
1571 m_buttons.Add(button);
1572 }
1573
1574 void wxAuiTabContainer::RemoveButton(int id)
1575 {
1576 size_t i, button_count = m_buttons.GetCount();
1577
1578 for (i = 0; i < button_count; ++i)
1579 {
1580 if (m_buttons.Item(i).id == id)
1581 {
1582 m_buttons.RemoveAt(i);
1583 return;
1584 }
1585 }
1586 }
1587
1588
1589
1590 size_t wxAuiTabContainer::GetTabOffset() const
1591 {
1592 return m_tab_offset;
1593 }
1594
1595 void wxAuiTabContainer::SetTabOffset(size_t offset)
1596 {
1597 m_tab_offset = offset;
1598 }
1599
1600
1601
1602
1603 // Render() renders the tab catalog to the specified DC
1604 // It is a virtual function and can be overridden to
1605 // provide custom drawing capabilities
1606 void wxAuiTabContainer::Render(wxDC* raw_dc, wxWindow* wnd)
1607 {
1608 if (!raw_dc || !raw_dc->IsOk())
1609 return;
1610
1611 wxMemoryDC dc;
1612
1613 // use the same layout direction as the window DC uses to ensure that the
1614 // text is rendered correctly
1615 dc.SetLayoutDirection(raw_dc->GetLayoutDirection());
1616
1617 wxBitmap bmp;
1618 size_t i;
1619 size_t page_count = m_pages.GetCount();
1620 size_t button_count = m_buttons.GetCount();
1621
1622 // create off-screen bitmap
1623 bmp.Create(m_rect.GetWidth(), m_rect.GetHeight());
1624 dc.SelectObject(bmp);
1625
1626 if (!dc.IsOk())
1627 return;
1628
1629 // find out if size of tabs is larger than can be
1630 // afforded on screen
1631 int total_width = 0;
1632 int visible_width = 0;
1633 for (i = 0; i < page_count; ++i)
1634 {
1635 wxAuiNotebookPage& page = m_pages.Item(i);
1636
1637 // determine if a close button is on this tab
1638 bool close_button = false;
1639 if ((m_flags & wxAUI_NB_CLOSE_ON_ALL_TABS) != 0 ||
1640 ((m_flags & wxAUI_NB_CLOSE_ON_ACTIVE_TAB) != 0 && page.active))
1641 {
1642 close_button = true;
1643 }
1644
1645
1646 int x_extent = 0;
1647 wxSize size = m_art->GetTabSize(dc,
1648 wnd,
1649 page.caption,
1650 page.bitmap,
1651 page.active,
1652 close_button ?
1653 wxAUI_BUTTON_STATE_NORMAL :
1654 wxAUI_BUTTON_STATE_HIDDEN,
1655 &x_extent);
1656
1657 if (i+1 < page_count)
1658 total_width += x_extent;
1659 else
1660 total_width += size.x;
1661
1662 if (i >= m_tab_offset)
1663 {
1664 if (i+1 < page_count)
1665 visible_width += x_extent;
1666 else
1667 visible_width += size.x;
1668 }
1669 }
1670
1671 if (total_width > m_rect.GetWidth() || m_tab_offset != 0)
1672 {
1673 // show left/right buttons
1674 for (i = 0; i < button_count; ++i)
1675 {
1676 wxAuiTabContainerButton& button = m_buttons.Item(i);
1677 if (button.id == wxAUI_BUTTON_LEFT ||
1678 button.id == wxAUI_BUTTON_RIGHT)
1679 {
1680 button.cur_state &= ~wxAUI_BUTTON_STATE_HIDDEN;
1681 }
1682 }
1683 }
1684 else
1685 {
1686 // hide left/right buttons
1687 for (i = 0; i < button_count; ++i)
1688 {
1689 wxAuiTabContainerButton& button = m_buttons.Item(i);
1690 if (button.id == wxAUI_BUTTON_LEFT ||
1691 button.id == wxAUI_BUTTON_RIGHT)
1692 {
1693 button.cur_state |= wxAUI_BUTTON_STATE_HIDDEN;
1694 }
1695 }
1696 }
1697
1698 // determine whether left button should be enabled
1699 for (i = 0; i < button_count; ++i)
1700 {
1701 wxAuiTabContainerButton& button = m_buttons.Item(i);
1702 if (button.id == wxAUI_BUTTON_LEFT)
1703 {
1704 if (m_tab_offset == 0)
1705 button.cur_state |= wxAUI_BUTTON_STATE_DISABLED;
1706 else
1707 button.cur_state &= ~wxAUI_BUTTON_STATE_DISABLED;
1708 }
1709 if (button.id == wxAUI_BUTTON_RIGHT)
1710 {
1711 if (visible_width < m_rect.GetWidth() - ((int)button_count*16))
1712 button.cur_state |= wxAUI_BUTTON_STATE_DISABLED;
1713 else
1714 button.cur_state &= ~wxAUI_BUTTON_STATE_DISABLED;
1715 }
1716 }
1717
1718
1719
1720 // draw background
1721 m_art->DrawBackground(dc, wnd, m_rect);
1722
1723 // draw buttons
1724 int left_buttons_width = 0;
1725 int right_buttons_width = 0;
1726
1727 int offset = 0;
1728
1729 // draw the buttons on the right side
1730 offset = m_rect.x + m_rect.width;
1731 for (i = 0; i < button_count; ++i)
1732 {
1733 wxAuiTabContainerButton& button = m_buttons.Item(button_count - i - 1);
1734
1735 if (button.location != wxRIGHT)
1736 continue;
1737 if (button.cur_state & wxAUI_BUTTON_STATE_HIDDEN)
1738 continue;
1739
1740 wxRect button_rect = m_rect;
1741 button_rect.SetY(1);
1742 button_rect.SetWidth(offset);
1743
1744 m_art->DrawButton(dc,
1745 wnd,
1746 button_rect,
1747 button.id,
1748 button.cur_state,
1749 wxRIGHT,
1750 &button.rect);
1751
1752 offset -= button.rect.GetWidth();
1753 right_buttons_width += button.rect.GetWidth();
1754 }
1755
1756
1757
1758 offset = 0;
1759
1760 // draw the buttons on the left side
1761
1762 for (i = 0; i < button_count; ++i)
1763 {
1764 wxAuiTabContainerButton& button = m_buttons.Item(button_count - i - 1);
1765
1766 if (button.location != wxLEFT)
1767 continue;
1768 if (button.cur_state & wxAUI_BUTTON_STATE_HIDDEN)
1769 continue;
1770
1771 wxRect button_rect(offset, 1, 1000, m_rect.height);
1772
1773 m_art->DrawButton(dc,
1774 wnd,
1775 button_rect,
1776 button.id,
1777 button.cur_state,
1778 wxLEFT,
1779 &button.rect);
1780
1781 offset += button.rect.GetWidth();
1782 left_buttons_width += button.rect.GetWidth();
1783 }
1784
1785 offset = left_buttons_width;
1786
1787 if (offset == 0)
1788 offset += m_art->GetIndentSize();
1789
1790
1791 // prepare the tab-close-button array
1792 // make sure tab button entries which aren't used are marked as hidden
1793 for (i = page_count; i < m_tab_close_buttons.GetCount(); ++i)
1794 m_tab_close_buttons.Item(i).cur_state = wxAUI_BUTTON_STATE_HIDDEN;
1795
1796 // make sure there are enough tab button entries to accommodate all tabs
1797 while (m_tab_close_buttons.GetCount() < page_count)
1798 {
1799 wxAuiTabContainerButton tempbtn;
1800 tempbtn.id = wxAUI_BUTTON_CLOSE;
1801 tempbtn.location = wxCENTER;
1802 tempbtn.cur_state = wxAUI_BUTTON_STATE_HIDDEN;
1803 m_tab_close_buttons.Add(tempbtn);
1804 }
1805
1806
1807 // buttons before the tab offset must be set to hidden
1808 for (i = 0; i < m_tab_offset; ++i)
1809 {
1810 m_tab_close_buttons.Item(i).cur_state = wxAUI_BUTTON_STATE_HIDDEN;
1811 }
1812
1813
1814 // draw the tabs
1815
1816 size_t active = 999;
1817 int active_offset = 0;
1818 wxRect active_rect;
1819
1820 int x_extent = 0;
1821 wxRect rect = m_rect;
1822 rect.y = 0;
1823 rect.height = m_rect.height;
1824
1825 for (i = m_tab_offset; i < page_count; ++i)
1826 {
1827 wxAuiNotebookPage& page = m_pages.Item(i);
1828 wxAuiTabContainerButton& tab_button = m_tab_close_buttons.Item(i);
1829
1830 // determine if a close button is on this tab
1831 if ((m_flags & wxAUI_NB_CLOSE_ON_ALL_TABS) != 0 ||
1832 ((m_flags & wxAUI_NB_CLOSE_ON_ACTIVE_TAB) != 0 && page.active))
1833 {
1834 if (tab_button.cur_state == wxAUI_BUTTON_STATE_HIDDEN)
1835 {
1836 tab_button.id = wxAUI_BUTTON_CLOSE;
1837 tab_button.cur_state = wxAUI_BUTTON_STATE_NORMAL;
1838 tab_button.location = wxCENTER;
1839 }
1840 }
1841 else
1842 {
1843 tab_button.cur_state = wxAUI_BUTTON_STATE_HIDDEN;
1844 }
1845
1846 rect.x = offset;
1847 rect.width = m_rect.width - right_buttons_width - offset - 2;
1848
1849 if (rect.width <= 0)
1850 break;
1851
1852 m_art->DrawTab(dc,
1853 wnd,
1854 page,
1855 rect,
1856 tab_button.cur_state,
1857 &page.rect,
1858 &tab_button.rect,
1859 &x_extent);
1860
1861 if (page.active)
1862 {
1863 active = i;
1864 active_offset = offset;
1865 active_rect = rect;
1866 }
1867
1868 offset += x_extent;
1869 }
1870
1871
1872 // make sure to deactivate buttons which are off the screen to the right
1873 for (++i; i < m_tab_close_buttons.GetCount(); ++i)
1874 {
1875 m_tab_close_buttons.Item(i).cur_state = wxAUI_BUTTON_STATE_HIDDEN;
1876 }
1877
1878
1879 // draw the active tab again so it stands in the foreground
1880 if (active >= m_tab_offset && active < m_pages.GetCount())
1881 {
1882 wxAuiNotebookPage& page = m_pages.Item(active);
1883
1884 wxAuiTabContainerButton& tab_button = m_tab_close_buttons.Item(active);
1885
1886 rect.x = active_offset;
1887 m_art->DrawTab(dc,
1888 wnd,
1889 page,
1890 active_rect,
1891 tab_button.cur_state,
1892 &page.rect,
1893 &tab_button.rect,
1894 &x_extent);
1895 }
1896
1897
1898 raw_dc->Blit(m_rect.x, m_rect.y,
1899 m_rect.GetWidth(), m_rect.GetHeight(),
1900 &dc, 0, 0);
1901 }
1902
1903 // Is the tab visible?
1904 bool wxAuiTabContainer::IsTabVisible(int tabPage, int tabOffset, wxDC* dc, wxWindow* wnd)
1905 {
1906 if (!dc || !dc->IsOk())
1907 return false;
1908
1909 size_t i;
1910 size_t page_count = m_pages.GetCount();
1911 size_t button_count = m_buttons.GetCount();
1912
1913 // Hasn't been rendered yet; assume it's visible
1914 if (m_tab_close_buttons.GetCount() < page_count)
1915 return true;
1916
1917 // First check if both buttons are disabled - if so, there's no need to
1918 // check further for visibility.
1919 int arrowButtonVisibleCount = 0;
1920 for (i = 0; i < button_count; ++i)
1921 {
1922 wxAuiTabContainerButton& button = m_buttons.Item(i);
1923 if (button.id == wxAUI_BUTTON_LEFT ||
1924 button.id == wxAUI_BUTTON_RIGHT)
1925 {
1926 if ((button.cur_state & wxAUI_BUTTON_STATE_HIDDEN) == 0)
1927 arrowButtonVisibleCount ++;
1928 }
1929 }
1930
1931 // Tab must be visible
1932 if (arrowButtonVisibleCount == 0)
1933 return true;
1934
1935 // If tab is less than the given offset, it must be invisible by definition
1936 if (tabPage < tabOffset)
1937 return false;
1938
1939 // draw buttons
1940 int left_buttons_width = 0;
1941 int right_buttons_width = 0;
1942
1943 int offset = 0;
1944
1945 // calculate size of the buttons on the right side
1946 offset = m_rect.x + m_rect.width;
1947 for (i = 0; i < button_count; ++i)
1948 {
1949 wxAuiTabContainerButton& button = m_buttons.Item(button_count - i - 1);
1950
1951 if (button.location != wxRIGHT)
1952 continue;
1953 if (button.cur_state & wxAUI_BUTTON_STATE_HIDDEN)
1954 continue;
1955
1956 offset -= button.rect.GetWidth();
1957 right_buttons_width += button.rect.GetWidth();
1958 }
1959
1960 offset = 0;
1961
1962 // calculate size of the buttons on the left side
1963 for (i = 0; i < button_count; ++i)
1964 {
1965 wxAuiTabContainerButton& button = m_buttons.Item(button_count - i - 1);
1966
1967 if (button.location != wxLEFT)
1968 continue;
1969 if (button.cur_state & wxAUI_BUTTON_STATE_HIDDEN)
1970 continue;
1971
1972 offset += button.rect.GetWidth();
1973 left_buttons_width += button.rect.GetWidth();
1974 }
1975
1976 offset = left_buttons_width;
1977
1978 if (offset == 0)
1979 offset += m_art->GetIndentSize();
1980
1981 wxRect active_rect;
1982
1983 wxRect rect = m_rect;
1984 rect.y = 0;
1985 rect.height = m_rect.height;
1986
1987 // See if the given page is visible at the given tab offset (effectively scroll position)
1988 for (i = tabOffset; i < page_count; ++i)
1989 {
1990 wxAuiNotebookPage& page = m_pages.Item(i);
1991 wxAuiTabContainerButton& tab_button = m_tab_close_buttons.Item(i);
1992
1993 rect.x = offset;
1994 rect.width = m_rect.width - right_buttons_width - offset - 2;
1995
1996 if (rect.width <= 0)
1997 return false; // haven't found the tab, and we've run out of space, so return false
1998
1999 int x_extent = 0;
2000 wxSize size = m_art->GetTabSize(*dc,
2001 wnd,
2002 page.caption,
2003 page.bitmap,
2004 page.active,
2005 tab_button.cur_state,
2006 &x_extent);
2007
2008 offset += x_extent;
2009
2010 if (i == (size_t) tabPage)
2011 {
2012 // If not all of the tab is visible, and supposing there's space to display it all,
2013 // we could do better so we return false.
2014 if (((m_rect.width - right_buttons_width - offset - 2) <= 0) && ((m_rect.width - right_buttons_width - left_buttons_width) > x_extent))
2015 return false;
2016 else
2017 return true;
2018 }
2019 }
2020
2021 // Shouldn't really get here, but if it does, assume the tab is visible to prevent
2022 // further looping in calling code.
2023 return true;
2024 }
2025
2026 // Make the tab visible if it wasn't already
2027 void wxAuiTabContainer::MakeTabVisible(int tabPage, wxWindow* win)
2028 {
2029 wxClientDC dc(win);
2030 if (!IsTabVisible(tabPage, GetTabOffset(), & dc, win))
2031 {
2032 int i;
2033 for (i = 0; i < (int) m_pages.GetCount(); i++)
2034 {
2035 if (IsTabVisible(tabPage, i, & dc, win))
2036 {
2037 SetTabOffset(i);
2038 win->Refresh();
2039 return;
2040 }
2041 }
2042 }
2043 }
2044
2045 // TabHitTest() tests if a tab was hit, passing the window pointer
2046 // back if that condition was fulfilled. The function returns
2047 // true if a tab was hit, otherwise false
2048 bool wxAuiTabContainer::TabHitTest(int x, int y, wxWindow** hit) const
2049 {
2050 if (!m_rect.Contains(x,y))
2051 return false;
2052
2053 wxAuiTabContainerButton* btn = NULL;
2054 if (ButtonHitTest(x, y, &btn))
2055 {
2056 if (m_buttons.Index(*btn) != wxNOT_FOUND)
2057 return false;
2058 }
2059
2060 size_t i, page_count = m_pages.GetCount();
2061
2062 for (i = m_tab_offset; i < page_count; ++i)
2063 {
2064 wxAuiNotebookPage& page = m_pages.Item(i);
2065 if (page.rect.Contains(x,y))
2066 {
2067 if (hit)
2068 *hit = page.window;
2069 return true;
2070 }
2071 }
2072
2073 return false;
2074 }
2075
2076 // ButtonHitTest() tests if a button was hit. The function returns
2077 // true if a button was hit, otherwise false
2078 bool wxAuiTabContainer::ButtonHitTest(int x, int y,
2079 wxAuiTabContainerButton** hit) const
2080 {
2081 if (!m_rect.Contains(x,y))
2082 return false;
2083
2084 size_t i, button_count;
2085
2086
2087 button_count = m_buttons.GetCount();
2088 for (i = 0; i < button_count; ++i)
2089 {
2090 wxAuiTabContainerButton& button = m_buttons.Item(i);
2091 if (button.rect.Contains(x,y) &&
2092 !(button.cur_state & (wxAUI_BUTTON_STATE_HIDDEN |
2093 wxAUI_BUTTON_STATE_DISABLED)))
2094 {
2095 if (hit)
2096 *hit = &button;
2097 return true;
2098 }
2099 }
2100
2101 button_count = m_tab_close_buttons.GetCount();
2102 for (i = 0; i < button_count; ++i)
2103 {
2104 wxAuiTabContainerButton& button = m_tab_close_buttons.Item(i);
2105 if (button.rect.Contains(x,y) &&
2106 !(button.cur_state & (wxAUI_BUTTON_STATE_HIDDEN |
2107 wxAUI_BUTTON_STATE_DISABLED)))
2108 {
2109 if (hit)
2110 *hit = &button;
2111 return true;
2112 }
2113 }
2114
2115 return false;
2116 }
2117
2118
2119
2120 // the utility function ShowWnd() is the same as show,
2121 // except it handles wxAuiMDIChildFrame windows as well,
2122 // as the Show() method on this class is "unplugged"
2123 static void ShowWnd(wxWindow* wnd, bool show)
2124 {
2125 #if wxUSE_MDI
2126 if (wnd->IsKindOf(CLASSINFO(wxAuiMDIChildFrame)))
2127 {
2128 wxAuiMDIChildFrame* cf = (wxAuiMDIChildFrame*)wnd;
2129 cf->DoShow(show);
2130 }
2131 else
2132 #endif
2133 {
2134 wnd->Show(show);
2135 }
2136 }
2137
2138
2139 // DoShowHide() this function shows the active window, then
2140 // hides all of the other windows (in that order)
2141 void wxAuiTabContainer::DoShowHide()
2142 {
2143 wxAuiNotebookPageArray& pages = GetPages();
2144 size_t i, page_count = pages.GetCount();
2145
2146 // show new active page first
2147 for (i = 0; i < page_count; ++i)
2148 {
2149 wxAuiNotebookPage& page = pages.Item(i);
2150 if (page.active)
2151 {
2152 ShowWnd(page.window, true);
2153 break;
2154 }
2155 }
2156
2157 // hide all other pages
2158 for (i = 0; i < page_count; ++i)
2159 {
2160 wxAuiNotebookPage& page = pages.Item(i);
2161 if (!page.active)
2162 ShowWnd(page.window, false);
2163 }
2164 }
2165
2166
2167
2168
2169
2170
2171 // -- wxAuiTabCtrl class implementation --
2172
2173
2174
2175 BEGIN_EVENT_TABLE(wxAuiTabCtrl, wxControl)
2176 EVT_PAINT(wxAuiTabCtrl::OnPaint)
2177 EVT_ERASE_BACKGROUND(wxAuiTabCtrl::OnEraseBackground)
2178 EVT_SIZE(wxAuiTabCtrl::OnSize)
2179 EVT_LEFT_DOWN(wxAuiTabCtrl::OnLeftDown)
2180 EVT_LEFT_DCLICK(wxAuiTabCtrl::OnLeftDClick)
2181 EVT_LEFT_UP(wxAuiTabCtrl::OnLeftUp)
2182 EVT_MIDDLE_DOWN(wxAuiTabCtrl::OnMiddleDown)
2183 EVT_MIDDLE_UP(wxAuiTabCtrl::OnMiddleUp)
2184 EVT_RIGHT_DOWN(wxAuiTabCtrl::OnRightDown)
2185 EVT_RIGHT_UP(wxAuiTabCtrl::OnRightUp)
2186 EVT_MOTION(wxAuiTabCtrl::OnMotion)
2187 EVT_LEAVE_WINDOW(wxAuiTabCtrl::OnLeaveWindow)
2188 EVT_AUINOTEBOOK_BUTTON(wxID_ANY, wxAuiTabCtrl::OnButton)
2189 EVT_SET_FOCUS(wxAuiTabCtrl::OnSetFocus)
2190 EVT_KILL_FOCUS(wxAuiTabCtrl::OnKillFocus)
2191 EVT_CHAR(wxAuiTabCtrl::OnChar)
2192 EVT_MOUSE_CAPTURE_LOST(wxAuiTabCtrl::OnCaptureLost)
2193 END_EVENT_TABLE()
2194
2195
2196 wxAuiTabCtrl::wxAuiTabCtrl(wxWindow* parent,
2197 wxWindowID id,
2198 const wxPoint& pos,
2199 const wxSize& size,
2200 long style) : wxControl(parent, id, pos, size, style)
2201 {
2202 SetName(wxT("wxAuiTabCtrl"));
2203 m_click_pt = wxDefaultPosition;
2204 m_is_dragging = false;
2205 m_hover_button = NULL;
2206 m_pressed_button = NULL;
2207 }
2208
2209 wxAuiTabCtrl::~wxAuiTabCtrl()
2210 {
2211 }
2212
2213 void wxAuiTabCtrl::OnPaint(wxPaintEvent&)
2214 {
2215 wxPaintDC dc(this);
2216
2217 dc.SetFont(GetFont());
2218
2219 if (GetPageCount() > 0)
2220 Render(&dc, this);
2221 }
2222
2223 void wxAuiTabCtrl::OnEraseBackground(wxEraseEvent& WXUNUSED(evt))
2224 {
2225 }
2226
2227 void wxAuiTabCtrl::OnSize(wxSizeEvent& evt)
2228 {
2229 wxSize s = evt.GetSize();
2230 wxRect r(0, 0, s.GetWidth(), s.GetHeight());
2231 SetRect(r);
2232 }
2233
2234 void wxAuiTabCtrl::OnLeftDown(wxMouseEvent& evt)
2235 {
2236 CaptureMouse();
2237 m_click_pt = wxDefaultPosition;
2238 m_is_dragging = false;
2239 m_click_tab = NULL;
2240 m_pressed_button = NULL;
2241
2242
2243 wxWindow* wnd;
2244 if (TabHitTest(evt.m_x, evt.m_y, &wnd))
2245 {
2246 int new_selection = GetIdxFromWindow(wnd);
2247
2248 // wxAuiNotebooks always want to receive this event
2249 // even if the tab is already active, because they may
2250 // have multiple tab controls
2251 if (new_selection != GetActivePage() ||
2252 GetParent()->IsKindOf(CLASSINFO(wxAuiNotebook)))
2253 {
2254 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING, m_windowId);
2255 e.SetSelection(new_selection);
2256 e.SetOldSelection(GetActivePage());
2257 e.SetEventObject(this);
2258 GetEventHandler()->ProcessEvent(e);
2259 }
2260
2261 m_click_pt.x = evt.m_x;
2262 m_click_pt.y = evt.m_y;
2263 m_click_tab = wnd;
2264 }
2265
2266 if (m_hover_button)
2267 {
2268 m_pressed_button = m_hover_button;
2269 m_pressed_button->cur_state = wxAUI_BUTTON_STATE_PRESSED;
2270 Refresh();
2271 Update();
2272 }
2273 }
2274
2275 void wxAuiTabCtrl::OnCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
2276 {
2277 }
2278
2279 void wxAuiTabCtrl::OnLeftUp(wxMouseEvent& evt)
2280 {
2281 if (GetCapture() == this)
2282 ReleaseMouse();
2283
2284 if (m_is_dragging)
2285 {
2286 m_is_dragging = false;
2287
2288 wxAuiNotebookEvent evt(wxEVT_COMMAND_AUINOTEBOOK_END_DRAG, m_windowId);
2289 evt.SetSelection(GetIdxFromWindow(m_click_tab));
2290 evt.SetOldSelection(evt.GetSelection());
2291 evt.SetEventObject(this);
2292 GetEventHandler()->ProcessEvent(evt);
2293
2294 return;
2295 }
2296
2297 if (m_pressed_button)
2298 {
2299 // make sure we're still clicking the button
2300 wxAuiTabContainerButton* button = NULL;
2301 if (!ButtonHitTest(evt.m_x, evt.m_y, &button))
2302 return;
2303
2304 if (button != m_pressed_button)
2305 {
2306 m_pressed_button = NULL;
2307 return;
2308 }
2309
2310 Refresh();
2311 Update();
2312
2313 if (!(m_pressed_button->cur_state & wxAUI_BUTTON_STATE_DISABLED))
2314 {
2315 wxAuiNotebookEvent evt(wxEVT_COMMAND_AUINOTEBOOK_BUTTON, m_windowId);
2316 evt.SetSelection(GetIdxFromWindow(m_click_tab));
2317 evt.SetInt(m_pressed_button->id);
2318 evt.SetEventObject(this);
2319 GetEventHandler()->ProcessEvent(evt);
2320 }
2321
2322 m_pressed_button = NULL;
2323 }
2324
2325 m_click_pt = wxDefaultPosition;
2326 m_is_dragging = false;
2327 m_click_tab = NULL;
2328 }
2329
2330 void wxAuiTabCtrl::OnMiddleUp(wxMouseEvent& evt)
2331 {
2332 wxWindow* wnd = NULL;
2333 if (!TabHitTest(evt.m_x, evt.m_y, &wnd))
2334 return;
2335
2336 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_TAB_MIDDLE_UP, m_windowId);
2337 e.SetEventObject(this);
2338 e.SetSelection(GetIdxFromWindow(wnd));
2339 GetEventHandler()->ProcessEvent(e);
2340 }
2341
2342 void wxAuiTabCtrl::OnMiddleDown(wxMouseEvent& evt)
2343 {
2344 wxWindow* wnd = NULL;
2345 if (!TabHitTest(evt.m_x, evt.m_y, &wnd))
2346 return;
2347
2348 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_TAB_MIDDLE_DOWN, m_windowId);
2349 e.SetEventObject(this);
2350 e.SetSelection(GetIdxFromWindow(wnd));
2351 GetEventHandler()->ProcessEvent(e);
2352 }
2353
2354 void wxAuiTabCtrl::OnRightUp(wxMouseEvent& evt)
2355 {
2356 wxWindow* wnd = NULL;
2357 if (!TabHitTest(evt.m_x, evt.m_y, &wnd))
2358 return;
2359
2360 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_TAB_RIGHT_UP, m_windowId);
2361 e.SetEventObject(this);
2362 e.SetSelection(GetIdxFromWindow(wnd));
2363 GetEventHandler()->ProcessEvent(e);
2364 }
2365
2366 void wxAuiTabCtrl::OnRightDown(wxMouseEvent& evt)
2367 {
2368 wxWindow* wnd = NULL;
2369 if (!TabHitTest(evt.m_x, evt.m_y, &wnd))
2370 return;
2371
2372 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_TAB_RIGHT_DOWN, m_windowId);
2373 e.SetEventObject(this);
2374 e.SetSelection(GetIdxFromWindow(wnd));
2375 GetEventHandler()->ProcessEvent(e);
2376 }
2377
2378 void wxAuiTabCtrl::OnLeftDClick(wxMouseEvent& evt)
2379 {
2380 wxWindow* wnd;
2381 wxAuiTabContainerButton* button;
2382 if (!TabHitTest(evt.m_x, evt.m_y, &wnd) && !ButtonHitTest(evt.m_x, evt.m_y, &button))
2383 {
2384 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_BG_DCLICK, m_windowId);
2385 e.SetEventObject(this);
2386 GetEventHandler()->ProcessEvent(e);
2387 }
2388 }
2389
2390 void wxAuiTabCtrl::OnMotion(wxMouseEvent& evt)
2391 {
2392 wxPoint pos = evt.GetPosition();
2393
2394 // check if the mouse is hovering above a button
2395 wxAuiTabContainerButton* button;
2396 if (ButtonHitTest(pos.x, pos.y, &button))
2397 {
2398 if (m_hover_button && button != m_hover_button)
2399 {
2400 m_hover_button->cur_state = wxAUI_BUTTON_STATE_NORMAL;
2401 m_hover_button = NULL;
2402 Refresh();
2403 Update();
2404 }
2405
2406 if (button->cur_state != wxAUI_BUTTON_STATE_HOVER)
2407 {
2408 button->cur_state = wxAUI_BUTTON_STATE_HOVER;
2409 Refresh();
2410 Update();
2411 m_hover_button = button;
2412 return;
2413 }
2414 }
2415 else
2416 {
2417 if (m_hover_button)
2418 {
2419 m_hover_button->cur_state = wxAUI_BUTTON_STATE_NORMAL;
2420 m_hover_button = NULL;
2421 Refresh();
2422 Update();
2423 }
2424 }
2425
2426
2427 if (!evt.LeftIsDown() || m_click_pt == wxDefaultPosition)
2428 return;
2429
2430 if (m_is_dragging)
2431 {
2432 wxAuiNotebookEvent evt(wxEVT_COMMAND_AUINOTEBOOK_DRAG_MOTION, m_windowId);
2433 evt.SetSelection(GetIdxFromWindow(m_click_tab));
2434 evt.SetOldSelection(evt.GetSelection());
2435 evt.SetEventObject(this);
2436 GetEventHandler()->ProcessEvent(evt);
2437 return;
2438 }
2439
2440
2441 int drag_x_threshold = wxSystemSettings::GetMetric(wxSYS_DRAG_X);
2442 int drag_y_threshold = wxSystemSettings::GetMetric(wxSYS_DRAG_Y);
2443
2444 if (abs(pos.x - m_click_pt.x) > drag_x_threshold ||
2445 abs(pos.y - m_click_pt.y) > drag_y_threshold)
2446 {
2447 wxAuiNotebookEvent evt(wxEVT_COMMAND_AUINOTEBOOK_BEGIN_DRAG, m_windowId);
2448 evt.SetSelection(GetIdxFromWindow(m_click_tab));
2449 evt.SetOldSelection(evt.GetSelection());
2450 evt.SetEventObject(this);
2451 GetEventHandler()->ProcessEvent(evt);
2452
2453 m_is_dragging = true;
2454 }
2455 }
2456
2457 void wxAuiTabCtrl::OnLeaveWindow(wxMouseEvent& WXUNUSED(event))
2458 {
2459 if (m_hover_button)
2460 {
2461 m_hover_button->cur_state = wxAUI_BUTTON_STATE_NORMAL;
2462 m_hover_button = NULL;
2463 Refresh();
2464 Update();
2465 }
2466 }
2467
2468 void wxAuiTabCtrl::OnButton(wxAuiNotebookEvent& event)
2469 {
2470 int button = event.GetInt();
2471
2472 if (button == wxAUI_BUTTON_LEFT || button == wxAUI_BUTTON_RIGHT)
2473 {
2474 if (button == wxAUI_BUTTON_LEFT)
2475 {
2476 if (GetTabOffset() > 0)
2477 {
2478 SetTabOffset(GetTabOffset()-1);
2479 Refresh();
2480 Update();
2481 }
2482 }
2483 else
2484 {
2485 SetTabOffset(GetTabOffset()+1);
2486 Refresh();
2487 Update();
2488 }
2489 }
2490 else if (button == wxAUI_BUTTON_WINDOWLIST)
2491 {
2492 int idx = GetArtProvider()->ShowDropDown(this, m_pages, GetActivePage());
2493
2494 if (idx != -1)
2495 {
2496 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING, m_windowId);
2497 e.SetSelection(idx);
2498 e.SetOldSelection(GetActivePage());
2499 e.SetEventObject(this);
2500 GetEventHandler()->ProcessEvent(e);
2501 }
2502 }
2503 else
2504 {
2505 event.Skip();
2506 }
2507 }
2508
2509 void wxAuiTabCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event))
2510 {
2511 Refresh();
2512 }
2513
2514 void wxAuiTabCtrl::OnKillFocus(wxFocusEvent& WXUNUSED(event))
2515 {
2516 Refresh();
2517 }
2518
2519 void wxAuiTabCtrl::OnChar(wxKeyEvent& event)
2520 {
2521 if (GetActivePage() == -1)
2522 {
2523 event.Skip();
2524 return;
2525 }
2526
2527 // We can't leave tab processing to the system; on Windows, tabs and keys
2528 // get eaten by the system and not processed properly if we specify both
2529 // wxTAB_TRAVERSAL and wxWANTS_CHARS. And if we specify just wxTAB_TRAVERSAL,
2530 // we don't key arrow key events.
2531
2532 int key = event.GetKeyCode();
2533
2534 if (key == WXK_NUMPAD_PAGEUP)
2535 key = WXK_PAGEUP;
2536 if (key == WXK_NUMPAD_PAGEDOWN)
2537 key = WXK_PAGEDOWN;
2538 if (key == WXK_NUMPAD_HOME)
2539 key = WXK_HOME;
2540 if (key == WXK_NUMPAD_END)
2541 key = WXK_END;
2542 if (key == WXK_NUMPAD_LEFT)
2543 key = WXK_LEFT;
2544 if (key == WXK_NUMPAD_RIGHT)
2545 key = WXK_RIGHT;
2546
2547 if (key == WXK_TAB || key == WXK_PAGEUP || key == WXK_PAGEDOWN)
2548 {
2549 bool bCtrlDown = event.ControlDown();
2550 bool bShiftDown = event.ShiftDown();
2551
2552 bool bForward = (key == WXK_TAB && !bShiftDown) || (key == WXK_PAGEDOWN);
2553 bool bWindowChange = (key == WXK_PAGEUP) || (key == WXK_PAGEDOWN) || bCtrlDown;
2554 bool bFromTab = (key == WXK_TAB);
2555
2556 wxAuiNotebook* nb = wxDynamicCast(GetParent(), wxAuiNotebook);
2557 if (!nb)
2558 {
2559 event.Skip();
2560 return;
2561 }
2562
2563 wxNavigationKeyEvent keyEvent;
2564 keyEvent.SetDirection(bForward);
2565 keyEvent.SetWindowChange(bWindowChange);
2566 keyEvent.SetFromTab(bFromTab);
2567 keyEvent.SetEventObject(nb);
2568
2569 if (!nb->GetEventHandler()->ProcessEvent(keyEvent))
2570 {
2571 // Not processed? Do an explicit tab into the page.
2572 wxWindow* win = GetWindowFromIdx(GetActivePage());
2573 if (win)
2574 win->SetFocus();
2575 }
2576 return;
2577 }
2578
2579 if (m_pages.GetCount() < 2)
2580 {
2581 event.Skip();
2582 return;
2583 }
2584
2585 int newPage = -1;
2586
2587 int forwardKey, backwardKey;
2588 if (GetLayoutDirection() == wxLayout_RightToLeft)
2589 {
2590 forwardKey = WXK_LEFT;
2591 backwardKey = WXK_RIGHT;
2592 }
2593 else
2594 {
2595 forwardKey = WXK_RIGHT;
2596 backwardKey = WXK_LEFT;
2597 }
2598
2599 if (key == forwardKey)
2600 {
2601 if (m_pages.GetCount() > 1)
2602 {
2603 if (GetActivePage() == -1)
2604 newPage = 0;
2605 else if (GetActivePage() < (int) (m_pages.GetCount() - 1))
2606 newPage = GetActivePage() + 1;
2607 }
2608 }
2609 else if (key == backwardKey)
2610 {
2611 if (m_pages.GetCount() > 1)
2612 {
2613 if (GetActivePage() == -1)
2614 newPage = (int) (m_pages.GetCount() - 1);
2615 else if (GetActivePage() > 0)
2616 newPage = GetActivePage() - 1;
2617 }
2618 }
2619 else if (key == WXK_HOME)
2620 {
2621 newPage = 0;
2622 }
2623 else if (key == WXK_END)
2624 {
2625 newPage = (int) (m_pages.GetCount() - 1);
2626 }
2627 else
2628 event.Skip();
2629
2630 if (newPage != -1)
2631 {
2632 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING, m_windowId);
2633 e.SetSelection(newPage);
2634 e.SetOldSelection(newPage);
2635 e.SetEventObject(this);
2636 this->GetEventHandler()->ProcessEvent(e);
2637 }
2638 else
2639 event.Skip();
2640 }
2641
2642 // wxTabFrame is an interesting case. It's important that all child pages
2643 // of the multi-notebook control are all actually children of that control
2644 // (and not grandchildren). wxTabFrame facilitates this. There is one
2645 // instance of wxTabFrame for each tab control inside the multi-notebook.
2646 // It's important to know that wxTabFrame is not a real window, but it merely
2647 // used to capture the dimensions/positioning of the internal tab control and
2648 // it's managed page windows
2649
2650 class wxTabFrame : public wxWindow
2651 {
2652 public:
2653
2654 wxTabFrame()
2655 {
2656 m_tabs = NULL;
2657 m_rect = wxRect(0,0,200,200);
2658 m_tab_ctrl_height = 20;
2659 }
2660
2661 ~wxTabFrame()
2662 {
2663 wxDELETE(m_tabs);
2664 }
2665
2666 void SetTabCtrlHeight(int h)
2667 {
2668 m_tab_ctrl_height = h;
2669 }
2670
2671 protected:
2672 void DoSetSize(int x, int y,
2673 int width, int height,
2674 int WXUNUSED(sizeFlags = wxSIZE_AUTO))
2675 {
2676 m_rect = wxRect(x, y, width, height);
2677 DoSizing();
2678 }
2679
2680 void DoGetClientSize(int* x, int* y) const
2681 {
2682 *x = m_rect.width;
2683 *y = m_rect.height;
2684 }
2685
2686 public:
2687 bool Show( bool WXUNUSED(show = true) ) { return false; }
2688
2689 void DoSizing()
2690 {
2691 if (!m_tabs)
2692 return;
2693
2694 if (m_tabs->IsFrozen() || m_tabs->GetParent()->IsFrozen())
2695 return;
2696
2697 m_tab_rect = wxRect(m_rect.x, m_rect.y, m_rect.width, m_tab_ctrl_height);
2698 if (m_tabs->GetFlags() & wxAUI_NB_BOTTOM)
2699 {
2700 m_tab_rect = wxRect (m_rect.x, m_rect.y + m_rect.height - m_tab_ctrl_height, m_rect.width, m_tab_ctrl_height);
2701 m_tabs->SetSize (m_rect.x, m_rect.y + m_rect.height - m_tab_ctrl_height, m_rect.width, m_tab_ctrl_height);
2702 m_tabs->SetRect (wxRect(0, 0, m_rect.width, m_tab_ctrl_height));
2703 }
2704 else //TODO: if (GetFlags() & wxAUI_NB_TOP)
2705 {
2706 m_tab_rect = wxRect (m_rect.x, m_rect.y, m_rect.width, m_tab_ctrl_height);
2707 m_tabs->SetSize (m_rect.x, m_rect.y, m_rect.width, m_tab_ctrl_height);
2708 m_tabs->SetRect (wxRect(0, 0, m_rect.width, m_tab_ctrl_height));
2709 }
2710 // TODO: else if (GetFlags() & wxAUI_NB_LEFT){}
2711 // TODO: else if (GetFlags() & wxAUI_NB_RIGHT){}
2712
2713 m_tabs->Refresh();
2714 m_tabs->Update();
2715
2716 wxAuiNotebookPageArray& pages = m_tabs->GetPages();
2717 size_t i, page_count = pages.GetCount();
2718
2719 for (i = 0; i < page_count; ++i)
2720 {
2721 int height = m_rect.height - m_tab_ctrl_height;
2722 if ( height < 0 )
2723 {
2724 // avoid passing negative height to wxWindow::SetSize(), this
2725 // results in assert failures/GTK+ warnings
2726 height = 0;
2727 }
2728
2729 wxAuiNotebookPage& page = pages.Item(i);
2730 if (m_tabs->GetFlags() & wxAUI_NB_BOTTOM)
2731 {
2732 page.window->SetSize(m_rect.x, m_rect.y, m_rect.width, height);
2733 }
2734 else //TODO: if (GetFlags() & wxAUI_NB_TOP)
2735 {
2736 page.window->SetSize(m_rect.x, m_rect.y + m_tab_ctrl_height,
2737 m_rect.width, height);
2738 }
2739 // TODO: else if (GetFlags() & wxAUI_NB_LEFT){}
2740 // TODO: else if (GetFlags() & wxAUI_NB_RIGHT){}
2741
2742 #if wxUSE_MDI
2743 if (page.window->IsKindOf(CLASSINFO(wxAuiMDIChildFrame)))
2744 {
2745 wxAuiMDIChildFrame* wnd = (wxAuiMDIChildFrame*)page.window;
2746 wnd->ApplyMDIChildFrameRect();
2747 }
2748 #endif
2749 }
2750 }
2751
2752 protected:
2753 void DoGetSize(int* x, int* y) const
2754 {
2755 if (x)
2756 *x = m_rect.GetWidth();
2757 if (y)
2758 *y = m_rect.GetHeight();
2759 }
2760
2761 public:
2762 void Update()
2763 {
2764 // does nothing
2765 }
2766
2767 wxRect m_rect;
2768 wxRect m_tab_rect;
2769 wxAuiTabCtrl* m_tabs;
2770 int m_tab_ctrl_height;
2771 };
2772
2773
2774 const int wxAuiBaseTabCtrlId = 5380;
2775
2776
2777 // -- wxAuiNotebook class implementation --
2778
2779 #define EVT_AUI_RANGE(id1, id2, event, func) \
2780 wx__DECLARE_EVT2(event, id1, id2, wxAuiNotebookEventHandler(func))
2781
2782 BEGIN_EVENT_TABLE(wxAuiNotebook, wxControl)
2783 EVT_SIZE(wxAuiNotebook::OnSize)
2784 EVT_CHILD_FOCUS(wxAuiNotebook::OnChildFocusNotebook)
2785 EVT_AUI_RANGE(wxAuiBaseTabCtrlId, wxAuiBaseTabCtrlId+500,
2786 wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING,
2787 wxAuiNotebook::OnTabClicked)
2788 EVT_AUI_RANGE(wxAuiBaseTabCtrlId, wxAuiBaseTabCtrlId+500,
2789 wxEVT_COMMAND_AUINOTEBOOK_BEGIN_DRAG,
2790 wxAuiNotebook::OnTabBeginDrag)
2791 EVT_AUI_RANGE(wxAuiBaseTabCtrlId, wxAuiBaseTabCtrlId+500,
2792 wxEVT_COMMAND_AUINOTEBOOK_END_DRAG,
2793 wxAuiNotebook::OnTabEndDrag)
2794 EVT_AUI_RANGE(wxAuiBaseTabCtrlId, wxAuiBaseTabCtrlId+500,
2795 wxEVT_COMMAND_AUINOTEBOOK_DRAG_MOTION,
2796 wxAuiNotebook::OnTabDragMotion)
2797 EVT_AUI_RANGE(wxAuiBaseTabCtrlId, wxAuiBaseTabCtrlId+500,
2798 wxEVT_COMMAND_AUINOTEBOOK_BUTTON,
2799 wxAuiNotebook::OnTabButton)
2800 EVT_AUI_RANGE(wxAuiBaseTabCtrlId, wxAuiBaseTabCtrlId+500,
2801 wxEVT_COMMAND_AUINOTEBOOK_TAB_MIDDLE_DOWN,
2802 wxAuiNotebook::OnTabMiddleDown)
2803 EVT_AUI_RANGE(wxAuiBaseTabCtrlId, wxAuiBaseTabCtrlId+500,
2804 wxEVT_COMMAND_AUINOTEBOOK_TAB_MIDDLE_UP,
2805 wxAuiNotebook::OnTabMiddleUp)
2806 EVT_AUI_RANGE(wxAuiBaseTabCtrlId, wxAuiBaseTabCtrlId+500,
2807 wxEVT_COMMAND_AUINOTEBOOK_TAB_RIGHT_DOWN,
2808 wxAuiNotebook::OnTabRightDown)
2809 EVT_AUI_RANGE(wxAuiBaseTabCtrlId, wxAuiBaseTabCtrlId+500,
2810 wxEVT_COMMAND_AUINOTEBOOK_TAB_RIGHT_UP,
2811 wxAuiNotebook::OnTabRightUp)
2812 EVT_AUI_RANGE(wxAuiBaseTabCtrlId, wxAuiBaseTabCtrlId+500,
2813 wxEVT_COMMAND_AUINOTEBOOK_BG_DCLICK,
2814 wxAuiNotebook::OnTabBgDClick)
2815 EVT_NAVIGATION_KEY(wxAuiNotebook::OnNavigationKeyNotebook)
2816
2817 #ifdef wxHAS_NATIVE_TAB_TRAVERSAL
2818 WX_EVENT_TABLE_CONTROL_CONTAINER(wxAuiNotebook)
2819 #else
2820 // Avoid clash with container event handler functions
2821 EVT_SET_FOCUS(wxAuiNotebook::OnFocus)
2822 #endif
2823 END_EVENT_TABLE()
2824
2825 WX_DELEGATE_TO_CONTROL_CONTAINER(wxAuiNotebook, wxControl)
2826
2827 wxAuiNotebook::wxAuiNotebook()
2828 {
2829 m_curpage = -1;
2830 m_tab_id_counter = wxAuiBaseTabCtrlId;
2831 m_dummy_wnd = NULL;
2832 m_tab_ctrl_height = 20;
2833 m_requested_bmp_size = wxDefaultSize;
2834 m_requested_tabctrl_height = -1;
2835 }
2836
2837 wxAuiNotebook::wxAuiNotebook(wxWindow *parent,
2838 wxWindowID id,
2839 const wxPoint& pos,
2840 const wxSize& size,
2841 long style) : wxControl(parent, id, pos, size, style)
2842 {
2843 m_dummy_wnd = NULL;
2844 m_requested_bmp_size = wxDefaultSize;
2845 m_requested_tabctrl_height = -1;
2846 InitNotebook(style);
2847 }
2848
2849 bool wxAuiNotebook::Create(wxWindow* parent,
2850 wxWindowID id,
2851 const wxPoint& pos,
2852 const wxSize& size,
2853 long style)
2854 {
2855 if (!wxControl::Create(parent, id, pos, size, style))
2856 return false;
2857
2858 InitNotebook(style);
2859
2860 return true;
2861 }
2862
2863 // InitNotebook() contains common initialization
2864 // code called by all constructors
2865 void wxAuiNotebook::InitNotebook(long style)
2866 {
2867 WX_INIT_CONTROL_CONTAINER();
2868 // SetCanFocus(false);
2869
2870 SetName(wxT("wxAuiNotebook"));
2871 m_curpage = -1;
2872 m_tab_id_counter = wxAuiBaseTabCtrlId;
2873 m_dummy_wnd = NULL;
2874 m_flags = (unsigned int)style;
2875 m_tab_ctrl_height = 20;
2876
2877 m_normal_font = *wxNORMAL_FONT;
2878 m_selected_font = *wxNORMAL_FONT;
2879 m_selected_font.SetWeight(wxBOLD);
2880
2881 SetArtProvider(new wxAuiDefaultTabArt);
2882
2883 m_dummy_wnd = new wxWindow(this, wxID_ANY, wxPoint(0,0), wxSize(0,0));
2884 m_dummy_wnd->SetSize(200, 200);
2885 m_dummy_wnd->Show(false);
2886
2887 m_mgr.SetManagedWindow(this);
2888 m_mgr.SetFlags(wxAUI_MGR_DEFAULT);
2889 m_mgr.SetDockSizeConstraint(1.0, 1.0); // no dock size constraint
2890
2891 m_mgr.AddPane(m_dummy_wnd,
2892 wxAuiPaneInfo().Name(wxT("dummy")).Bottom().CaptionVisible(false).Show(false));
2893
2894 m_mgr.Update();
2895 }
2896
2897 wxAuiNotebook::~wxAuiNotebook()
2898 {
2899 // Indicate we're deleting pages
2900 SendDestroyEvent();
2901
2902 while ( GetPageCount() > 0 )
2903 DeletePage(0);
2904
2905 m_mgr.UnInit();
2906 }
2907
2908 void wxAuiNotebook::SetArtProvider(wxAuiTabArt* art)
2909 {
2910 m_tabs.SetArtProvider(art);
2911
2912 UpdateTabCtrlHeight();
2913 }
2914
2915 // SetTabCtrlHeight() is the highest-level override of the
2916 // tab height. A call to this function effectively enforces a
2917 // specified tab ctrl height, overriding all other considerations,
2918 // such as text or bitmap height. It overrides any call to
2919 // SetUniformBitmapSize(). Specifying a height of -1 reverts
2920 // any previous call and returns to the default behavior
2921
2922 void wxAuiNotebook::SetTabCtrlHeight(int height)
2923 {
2924 m_requested_tabctrl_height = height;
2925
2926 // if window is already initialized, recalculate the tab height
2927 if (m_dummy_wnd)
2928 {
2929 UpdateTabCtrlHeight();
2930 }
2931 }
2932
2933
2934 // SetUniformBitmapSize() ensures that all tabs will have
2935 // the same height, even if some tabs don't have bitmaps
2936 // Passing wxDefaultSize to this function will instruct
2937 // the control to use dynamic tab height-- so when a tab
2938 // with a large bitmap is added, the tab ctrl's height will
2939 // automatically increase to accommodate the bitmap
2940
2941 void wxAuiNotebook::SetUniformBitmapSize(const wxSize& size)
2942 {
2943 m_requested_bmp_size = size;
2944
2945 // if window is already initialized, recalculate the tab height
2946 if (m_dummy_wnd)
2947 {
2948 UpdateTabCtrlHeight();
2949 }
2950 }
2951
2952 // UpdateTabCtrlHeight() does the actual tab resizing. It's meant
2953 // to be used interally
2954 void wxAuiNotebook::UpdateTabCtrlHeight()
2955 {
2956 // get the tab ctrl height we will use
2957 int height = CalculateTabCtrlHeight();
2958
2959 // if the tab control height needs to change, update
2960 // all of our tab controls with the new height
2961 if (m_tab_ctrl_height != height)
2962 {
2963 wxAuiTabArt* art = m_tabs.GetArtProvider();
2964
2965 m_tab_ctrl_height = height;
2966
2967 wxAuiPaneInfoArray& all_panes = m_mgr.GetAllPanes();
2968 size_t i, pane_count = all_panes.GetCount();
2969 for (i = 0; i < pane_count; ++i)
2970 {
2971 wxAuiPaneInfo& pane = all_panes.Item(i);
2972 if (pane.name == wxT("dummy"))
2973 continue;
2974 wxTabFrame* tab_frame = (wxTabFrame*)pane.window;
2975 wxAuiTabCtrl* tabctrl = tab_frame->m_tabs;
2976 tab_frame->SetTabCtrlHeight(m_tab_ctrl_height);
2977 tabctrl->SetArtProvider(art->Clone());
2978 tab_frame->DoSizing();
2979 }
2980 }
2981 }
2982
2983 void wxAuiNotebook::UpdateHintWindowSize()
2984 {
2985 wxSize size = CalculateNewSplitSize();
2986
2987 // the placeholder hint window should be set to this size
2988 wxAuiPaneInfo& info = m_mgr.GetPane(wxT("dummy"));
2989 if (info.IsOk())
2990 {
2991 info.MinSize(size);
2992 info.BestSize(size);
2993 m_dummy_wnd->SetSize(size);
2994 }
2995 }
2996
2997
2998 // calculates the size of the new split
2999 wxSize wxAuiNotebook::CalculateNewSplitSize()
3000 {
3001 // count number of tab controls
3002 int tab_ctrl_count = 0;
3003 wxAuiPaneInfoArray& all_panes = m_mgr.GetAllPanes();
3004 size_t i, pane_count = all_panes.GetCount();
3005 for (i = 0; i < pane_count; ++i)
3006 {
3007 wxAuiPaneInfo& pane = all_panes.Item(i);
3008 if (pane.name == wxT("dummy"))
3009 continue;
3010 tab_ctrl_count++;
3011 }
3012
3013 wxSize new_split_size;
3014
3015 // if there is only one tab control, the first split
3016 // should happen around the middle
3017 if (tab_ctrl_count < 2)
3018 {
3019 new_split_size = GetClientSize();
3020 new_split_size.x /= 2;
3021 new_split_size.y /= 2;
3022 }
3023 else
3024 {
3025 // this is in place of a more complicated calculation
3026 // that needs to be implemented
3027 new_split_size = wxSize(180,180);
3028 }
3029
3030 return new_split_size;
3031 }
3032
3033 int wxAuiNotebook::CalculateTabCtrlHeight()
3034 {
3035 // if a fixed tab ctrl height is specified,
3036 // just return that instead of calculating a
3037 // tab height
3038 if (m_requested_tabctrl_height != -1)
3039 return m_requested_tabctrl_height;
3040
3041 // find out new best tab height
3042 wxAuiTabArt* art = m_tabs.GetArtProvider();
3043
3044 return art->GetBestTabCtrlSize(this,
3045 m_tabs.GetPages(),
3046 m_requested_bmp_size);
3047 }
3048
3049
3050 wxAuiTabArt* wxAuiNotebook::GetArtProvider() const
3051 {
3052 return m_tabs.GetArtProvider();
3053 }
3054
3055 void wxAuiNotebook::SetWindowStyleFlag(long style)
3056 {
3057 wxControl::SetWindowStyleFlag(style);
3058
3059 m_flags = (unsigned int)style;
3060
3061 // if the control is already initialized
3062 if (m_mgr.GetManagedWindow() == (wxWindow*)this)
3063 {
3064 // let all of the tab children know about the new style
3065
3066 wxAuiPaneInfoArray& all_panes = m_mgr.GetAllPanes();
3067 size_t i, pane_count = all_panes.GetCount();
3068 for (i = 0; i < pane_count; ++i)
3069 {
3070 wxAuiPaneInfo& pane = all_panes.Item(i);
3071 if (pane.name == wxT("dummy"))
3072 continue;
3073 wxTabFrame* tabframe = (wxTabFrame*)pane.window;
3074 wxAuiTabCtrl* tabctrl = tabframe->m_tabs;
3075 tabctrl->SetFlags(m_flags);
3076 tabframe->DoSizing();
3077 tabctrl->Refresh();
3078 tabctrl->Update();
3079 }
3080 }
3081 }
3082
3083
3084 bool wxAuiNotebook::AddPage(wxWindow* page,
3085 const wxString& caption,
3086 bool select,
3087 const wxBitmap& bitmap)
3088 {
3089 return InsertPage(GetPageCount(), page, caption, select, bitmap);
3090 }
3091
3092 bool wxAuiNotebook::InsertPage(size_t page_idx,
3093 wxWindow* page,
3094 const wxString& caption,
3095 bool select,
3096 const wxBitmap& bitmap)
3097 {
3098 wxASSERT_MSG(page, wxT("page pointer must be non-NULL"));
3099 if (!page)
3100 return false;
3101
3102 page->Reparent(this);
3103
3104 wxAuiNotebookPage info;
3105 info.window = page;
3106 info.caption = caption;
3107 info.bitmap = bitmap;
3108 info.active = false;
3109
3110 // if there are currently no tabs, the first added
3111 // tab must be active
3112 if (m_tabs.GetPageCount() == 0)
3113 info.active = true;
3114
3115 m_tabs.InsertPage(page, info, page_idx);
3116
3117 // if that was the first page added, even if
3118 // select is false, it must become the "current page"
3119 // (though no select events will be fired)
3120 if (!select && m_tabs.GetPageCount() == 1)
3121 select = true;
3122 //m_curpage = GetPageIndex(page);
3123
3124 wxAuiTabCtrl* active_tabctrl = GetActiveTabCtrl();
3125 if (page_idx >= active_tabctrl->GetPageCount())
3126 active_tabctrl->AddPage(page, info);
3127 else
3128 active_tabctrl->InsertPage(page, info, page_idx);
3129
3130 UpdateTabCtrlHeight();
3131 DoSizing();
3132 active_tabctrl->DoShowHide();
3133
3134 // adjust selected index
3135 if(m_curpage >= (int) page_idx)
3136 m_curpage++;
3137
3138 if (select)
3139 {
3140 SetSelectionToWindow(page);
3141 }
3142
3143 return true;
3144 }
3145
3146
3147 // DeletePage() removes a tab from the multi-notebook,
3148 // and destroys the window as well
3149 bool wxAuiNotebook::DeletePage(size_t page_idx)
3150 {
3151 if (page_idx >= m_tabs.GetPageCount())
3152 return false;
3153
3154 wxWindow* wnd = m_tabs.GetWindowFromIdx(page_idx);
3155
3156 // hide the window in advance, as this will
3157 // prevent flicker
3158 ShowWnd(wnd, false);
3159
3160 if (!RemovePage(page_idx))
3161 return false;
3162
3163 #if wxUSE_MDI
3164 // actually destroy the window now
3165 if (wnd->IsKindOf(CLASSINFO(wxAuiMDIChildFrame)))
3166 {
3167 // delete the child frame with pending delete, as is
3168 // customary with frame windows
3169 if (!wxPendingDelete.Member(wnd))
3170 wxPendingDelete.Append(wnd);
3171 }
3172 else
3173 #endif
3174 {
3175 wnd->Destroy();
3176 }
3177
3178 return true;
3179 }
3180
3181
3182
3183 // RemovePage() removes a tab from the multi-notebook,
3184 // but does not destroy the window
3185 bool wxAuiNotebook::RemovePage(size_t page_idx)
3186 {
3187 // save active window pointer
3188 wxWindow* active_wnd = NULL;
3189 if (m_curpage >= 0)
3190 active_wnd = m_tabs.GetWindowFromIdx(m_curpage);
3191
3192 // save pointer of window being deleted
3193 wxWindow* wnd = m_tabs.GetWindowFromIdx(page_idx);
3194 wxWindow* new_active = NULL;
3195
3196 // make sure we found the page
3197 if (!wnd)
3198 return false;
3199
3200 // find out which onscreen tab ctrl owns this tab
3201 wxAuiTabCtrl* ctrl;
3202 int ctrl_idx;
3203 if (!FindTab(wnd, &ctrl, &ctrl_idx))
3204 return false;
3205
3206 bool is_curpage = (m_curpage == (int)page_idx);
3207 bool is_active_in_split = ctrl->GetPage(ctrl_idx).active;
3208
3209
3210 // remove the tab from main catalog
3211 if (!m_tabs.RemovePage(wnd))
3212 return false;
3213
3214 // remove the tab from the onscreen tab ctrl
3215 ctrl->RemovePage(wnd);
3216
3217 if (is_active_in_split)
3218 {
3219 int ctrl_new_page_count = (int)ctrl->GetPageCount();
3220
3221 if (ctrl_idx >= ctrl_new_page_count)
3222 ctrl_idx = ctrl_new_page_count-1;
3223
3224 if (ctrl_idx >= 0 && ctrl_idx < (int)ctrl->GetPageCount())
3225 {
3226 // set new page as active in the tab split
3227 ctrl->SetActivePage(ctrl_idx);
3228
3229 // if the page deleted was the current page for the
3230 // entire tab control, then record the window
3231 // pointer of the new active page for activation
3232 if (is_curpage)
3233 {
3234 new_active = ctrl->GetWindowFromIdx(ctrl_idx);
3235 }
3236 }
3237 }
3238 else
3239 {
3240 // we are not deleting the active page, so keep it the same
3241 new_active = active_wnd;
3242 }
3243
3244
3245 if (!new_active)
3246 {
3247 // we haven't yet found a new page to active,
3248 // so select the next page from the main tab
3249 // catalogue
3250
3251 if (page_idx < m_tabs.GetPageCount())
3252 {
3253 new_active = m_tabs.GetPage(page_idx).window;
3254 }
3255
3256 if (!new_active && m_tabs.GetPageCount() > 0)
3257 {
3258 new_active = m_tabs.GetPage(0).window;
3259 }
3260 }
3261
3262
3263 RemoveEmptyTabFrames();
3264
3265 m_curpage = wxNOT_FOUND;
3266
3267 // set new active pane unless we're being destroyed anyhow
3268 if (new_active && !m_isBeingDeleted)
3269 SetSelectionToWindow(new_active);
3270
3271 return true;
3272 }
3273
3274 // GetPageIndex() returns the index of the page, or -1 if the
3275 // page could not be located in the notebook
3276 int wxAuiNotebook::GetPageIndex(wxWindow* page_wnd) const
3277 {
3278 return m_tabs.GetIdxFromWindow(page_wnd);
3279 }
3280
3281
3282
3283 // SetPageText() changes the tab caption of the specified page
3284 bool wxAuiNotebook::SetPageText(size_t page_idx, const wxString& text)
3285 {
3286 if (page_idx >= m_tabs.GetPageCount())
3287 return false;
3288
3289 // update our own tab catalog
3290 wxAuiNotebookPage& page_info = m_tabs.GetPage(page_idx);
3291 page_info.caption = text;
3292
3293 // update what's on screen
3294 wxAuiTabCtrl* ctrl;
3295 int ctrl_idx;
3296 if (FindTab(page_info.window, &ctrl, &ctrl_idx))
3297 {
3298 wxAuiNotebookPage& info = ctrl->GetPage(ctrl_idx);
3299 info.caption = text;
3300 ctrl->Refresh();
3301 ctrl->Update();
3302 }
3303
3304 return true;
3305 }
3306
3307 // returns the page caption
3308 wxString wxAuiNotebook::GetPageText(size_t page_idx) const
3309 {
3310 if (page_idx >= m_tabs.GetPageCount())
3311 return wxEmptyString;
3312
3313 // update our own tab catalog
3314 const wxAuiNotebookPage& page_info = m_tabs.GetPage(page_idx);
3315 return page_info.caption;
3316 }
3317
3318 bool wxAuiNotebook::SetPageBitmap(size_t page_idx, const wxBitmap& bitmap)
3319 {
3320 if (page_idx >= m_tabs.GetPageCount())
3321 return false;
3322
3323 // update our own tab catalog
3324 wxAuiNotebookPage& page_info = m_tabs.GetPage(page_idx);
3325 page_info.bitmap = bitmap;
3326
3327 // tab height might have changed
3328 UpdateTabCtrlHeight();
3329
3330 // update what's on screen
3331 wxAuiTabCtrl* ctrl;
3332 int ctrl_idx;
3333 if (FindTab(page_info.window, &ctrl, &ctrl_idx))
3334 {
3335 wxAuiNotebookPage& info = ctrl->GetPage(ctrl_idx);
3336 info.bitmap = bitmap;
3337 ctrl->Refresh();
3338 ctrl->Update();
3339 }
3340
3341 return true;
3342 }
3343
3344 // returns the page bitmap
3345 wxBitmap wxAuiNotebook::GetPageBitmap(size_t page_idx) const
3346 {
3347 if (page_idx >= m_tabs.GetPageCount())
3348 return wxBitmap();
3349
3350 // update our own tab catalog
3351 const wxAuiNotebookPage& page_info = m_tabs.GetPage(page_idx);
3352 return page_info.bitmap;
3353 }
3354
3355 // GetSelection() returns the index of the currently active page
3356 int wxAuiNotebook::GetSelection() const
3357 {
3358 return m_curpage;
3359 }
3360
3361 // SetSelection() sets the currently active page
3362 size_t wxAuiNotebook::SetSelection(size_t new_page)
3363 {
3364 wxWindow* wnd = m_tabs.GetWindowFromIdx(new_page);
3365 if (!wnd)
3366 return m_curpage;
3367
3368 // don't change the page unless necessary;
3369 // however, clicking again on a tab should give it the focus.
3370 if ((int)new_page == m_curpage)
3371 {
3372 wxAuiTabCtrl* ctrl;
3373 int ctrl_idx;
3374 if (FindTab(wnd, &ctrl, &ctrl_idx))
3375 {
3376 if (FindFocus() != ctrl)
3377 ctrl->SetFocus();
3378 }
3379 return m_curpage;
3380 }
3381
3382 wxAuiNotebookEvent evt(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING, m_windowId);
3383 evt.SetSelection(new_page);
3384 evt.SetOldSelection(m_curpage);
3385 evt.SetEventObject(this);
3386 if (!GetEventHandler()->ProcessEvent(evt) || evt.IsAllowed())
3387 {
3388 int old_curpage = m_curpage;
3389 m_curpage = new_page;
3390
3391 // program allows the page change
3392 evt.SetEventType(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGED);
3393 (void)GetEventHandler()->ProcessEvent(evt);
3394
3395
3396 wxAuiTabCtrl* ctrl;
3397 int ctrl_idx;
3398 if (FindTab(wnd, &ctrl, &ctrl_idx))
3399 {
3400 m_tabs.SetActivePage(wnd);
3401
3402 ctrl->SetActivePage(ctrl_idx);
3403 DoSizing();
3404 ctrl->DoShowHide();
3405
3406 ctrl->MakeTabVisible(ctrl_idx, ctrl);
3407
3408 // set fonts
3409 wxAuiPaneInfoArray& all_panes = m_mgr.GetAllPanes();
3410 size_t i, pane_count = all_panes.GetCount();
3411 for (i = 0; i < pane_count; ++i)
3412 {
3413 wxAuiPaneInfo& pane = all_panes.Item(i);
3414 if (pane.name == wxT("dummy"))
3415 continue;
3416 wxAuiTabCtrl* tabctrl = ((wxTabFrame*)pane.window)->m_tabs;
3417 if (tabctrl != ctrl)
3418 tabctrl->SetSelectedFont(m_normal_font);
3419 else
3420 tabctrl->SetSelectedFont(m_selected_font);
3421 tabctrl->Refresh();
3422 }
3423
3424 // Set the focus to the page if we're not currently focused on the tab.
3425 // This is Firefox-like behaviour.
3426 if (wnd->IsShownOnScreen() && FindFocus() != ctrl)
3427 wnd->SetFocus();
3428
3429 return old_curpage;
3430 }
3431 }
3432
3433 return m_curpage;
3434 }
3435
3436 void wxAuiNotebook::SetSelectionToWindow(wxWindow *win)
3437 {
3438 const int idx = m_tabs.GetIdxFromWindow(win);
3439 wxCHECK_RET( idx != wxNOT_FOUND, wxT("invalid notebook page") );
3440
3441
3442 // since a tab was clicked, let the parent know that we received
3443 // the focus, even if we will assign that focus immediately
3444 // to the child tab in the SetSelection call below
3445 // (the child focus event will also let wxAuiManager, if any,
3446 // know that the notebook control has been activated)
3447
3448 wxWindow* parent = GetParent();
3449 if (parent)
3450 {
3451 wxChildFocusEvent eventFocus(this);
3452 parent->GetEventHandler()->ProcessEvent(eventFocus);
3453 }
3454
3455
3456 SetSelection(idx);
3457 }
3458
3459 // GetPageCount() returns the total number of
3460 // pages managed by the multi-notebook
3461 size_t wxAuiNotebook::GetPageCount() const
3462 {
3463 return m_tabs.GetPageCount();
3464 }
3465
3466 // GetPage() returns the wxWindow pointer of the
3467 // specified page
3468 wxWindow* wxAuiNotebook::GetPage(size_t page_idx) const
3469 {
3470 wxASSERT(page_idx < m_tabs.GetPageCount());
3471
3472 return m_tabs.GetWindowFromIdx(page_idx);
3473 }
3474
3475 // DoSizing() performs all sizing operations in each tab control
3476 void wxAuiNotebook::DoSizing()
3477 {
3478 wxAuiPaneInfoArray& all_panes = m_mgr.GetAllPanes();
3479 size_t i, pane_count = all_panes.GetCount();
3480 for (i = 0; i < pane_count; ++i)
3481 {
3482 if (all_panes.Item(i).name == wxT("dummy"))
3483 continue;
3484
3485 wxTabFrame* tabframe = (wxTabFrame*)all_panes.Item(i).window;
3486 tabframe->DoSizing();
3487 }
3488 }
3489
3490 // GetActiveTabCtrl() returns the active tab control. It is
3491 // called to determine which control gets new windows being added
3492 wxAuiTabCtrl* wxAuiNotebook::GetActiveTabCtrl()
3493 {
3494 if (m_curpage >= 0 && m_curpage < (int)m_tabs.GetPageCount())
3495 {
3496 wxAuiTabCtrl* ctrl;
3497 int idx;
3498
3499 // find the tab ctrl with the current page
3500 if (FindTab(m_tabs.GetPage(m_curpage).window,
3501 &ctrl, &idx))
3502 {
3503 return ctrl;
3504 }
3505 }
3506
3507 // no current page, just find the first tab ctrl
3508 wxAuiPaneInfoArray& all_panes = m_mgr.GetAllPanes();
3509 size_t i, pane_count = all_panes.GetCount();
3510 for (i = 0; i < pane_count; ++i)
3511 {
3512 if (all_panes.Item(i).name == wxT("dummy"))
3513 continue;
3514
3515 wxTabFrame* tabframe = (wxTabFrame*)all_panes.Item(i).window;
3516 return tabframe->m_tabs;
3517 }
3518
3519 // If there is no tabframe at all, create one
3520 wxTabFrame* tabframe = new wxTabFrame;
3521 tabframe->SetTabCtrlHeight(m_tab_ctrl_height);
3522 tabframe->m_tabs = new wxAuiTabCtrl(this,
3523 m_tab_id_counter++,
3524 wxDefaultPosition,
3525 wxDefaultSize,
3526 wxNO_BORDER|wxWANTS_CHARS);
3527 tabframe->m_tabs->SetFlags(m_flags);
3528 tabframe->m_tabs->SetArtProvider(m_tabs.GetArtProvider()->Clone());
3529 m_mgr.AddPane(tabframe,
3530 wxAuiPaneInfo().Center().CaptionVisible(false));
3531
3532 m_mgr.Update();
3533
3534 return tabframe->m_tabs;
3535 }
3536
3537 // FindTab() finds the tab control that currently contains the window as well
3538 // as the index of the window in the tab control. It returns true if the
3539 // window was found, otherwise false.
3540 bool wxAuiNotebook::FindTab(wxWindow* page, wxAuiTabCtrl** ctrl, int* idx)
3541 {
3542 wxAuiPaneInfoArray& all_panes = m_mgr.GetAllPanes();
3543 size_t i, pane_count = all_panes.GetCount();
3544 for (i = 0; i < pane_count; ++i)
3545 {
3546 if (all_panes.Item(i).name == wxT("dummy"))
3547 continue;
3548
3549 wxTabFrame* tabframe = (wxTabFrame*)all_panes.Item(i).window;
3550
3551 int page_idx = tabframe->m_tabs->GetIdxFromWindow(page);
3552 if (page_idx != -1)
3553 {
3554 *ctrl = tabframe->m_tabs;
3555 *idx = page_idx;
3556 return true;
3557 }
3558 }
3559
3560 return false;
3561 }
3562
3563 void wxAuiNotebook::Split(size_t page, int direction)
3564 {
3565 wxSize cli_size = GetClientSize();
3566
3567 // get the page's window pointer
3568 wxWindow* wnd = GetPage(page);
3569 if (!wnd)
3570 return;
3571
3572 // notebooks with 1 or less pages can't be split
3573 if (GetPageCount() < 2)
3574 return;
3575
3576 // find out which tab control the page currently belongs to
3577 wxAuiTabCtrl *src_tabs, *dest_tabs;
3578 int src_idx = -1;
3579 src_tabs = NULL;
3580 if (!FindTab(wnd, &src_tabs, &src_idx))
3581 return;
3582 if (!src_tabs || src_idx == -1)
3583 return;
3584
3585 // choose a split size
3586 wxSize split_size;
3587 if (GetPageCount() > 2)
3588 {
3589 split_size = CalculateNewSplitSize();
3590 }
3591 else
3592 {
3593 // because there are two panes, always split them
3594 // equally
3595 split_size = GetClientSize();
3596 split_size.x /= 2;
3597 split_size.y /= 2;
3598 }
3599
3600
3601 // create a new tab frame
3602 wxTabFrame* new_tabs = new wxTabFrame;
3603 new_tabs->m_rect = wxRect(wxPoint(0,0), split_size);
3604 new_tabs->SetTabCtrlHeight(m_tab_ctrl_height);
3605 new_tabs->m_tabs = new wxAuiTabCtrl(this,
3606 m_tab_id_counter++,
3607 wxDefaultPosition,
3608 wxDefaultSize,
3609 wxNO_BORDER|wxWANTS_CHARS);
3610 new_tabs->m_tabs->SetArtProvider(m_tabs.GetArtProvider()->Clone());
3611 new_tabs->m_tabs->SetFlags(m_flags);
3612 dest_tabs = new_tabs->m_tabs;
3613
3614 // create a pane info structure with the information
3615 // about where the pane should be added
3616 wxAuiPaneInfo pane_info = wxAuiPaneInfo().Bottom().CaptionVisible(false);
3617 wxPoint mouse_pt;
3618
3619 if (direction == wxLEFT)
3620 {
3621 pane_info.Left();
3622 mouse_pt = wxPoint(0, cli_size.y/2);
3623 }
3624 else if (direction == wxRIGHT)
3625 {
3626 pane_info.Right();
3627 mouse_pt = wxPoint(cli_size.x, cli_size.y/2);
3628 }
3629 else if (direction == wxTOP)
3630 {
3631 pane_info.Top();
3632 mouse_pt = wxPoint(cli_size.x/2, 0);
3633 }
3634 else if (direction == wxBOTTOM)
3635 {
3636 pane_info.Bottom();
3637 mouse_pt = wxPoint(cli_size.x/2, cli_size.y);
3638 }
3639
3640 m_mgr.AddPane(new_tabs, pane_info, mouse_pt);
3641 m_mgr.Update();
3642
3643 // remove the page from the source tabs
3644 wxAuiNotebookPage page_info = src_tabs->GetPage(src_idx);
3645 page_info.active = false;
3646 src_tabs->RemovePage(page_info.window);
3647 if (src_tabs->GetPageCount() > 0)
3648 {
3649 src_tabs->SetActivePage((size_t)0);
3650 src_tabs->DoShowHide();
3651 src_tabs->Refresh();
3652 }
3653
3654
3655 // add the page to the destination tabs
3656 dest_tabs->InsertPage(page_info.window, page_info, 0);
3657
3658 if (src_tabs->GetPageCount() == 0)
3659 {
3660 RemoveEmptyTabFrames();
3661 }
3662
3663 DoSizing();
3664 dest_tabs->DoShowHide();
3665 dest_tabs->Refresh();
3666
3667 // force the set selection function reset the selection
3668 m_curpage = -1;
3669
3670 // set the active page to the one we just split off
3671 SetSelectionToPage(page_info);
3672
3673 UpdateHintWindowSize();
3674 }
3675
3676
3677 void wxAuiNotebook::OnSize(wxSizeEvent& evt)
3678 {
3679 UpdateHintWindowSize();
3680
3681 evt.Skip();
3682 }
3683
3684 void wxAuiNotebook::OnTabClicked(wxAuiNotebookEvent& evt)
3685 {
3686 wxAuiTabCtrl* ctrl = (wxAuiTabCtrl*)evt.GetEventObject();
3687 wxASSERT(ctrl != NULL);
3688
3689 wxWindow* wnd = ctrl->GetWindowFromIdx(evt.GetSelection());
3690 wxASSERT(wnd != NULL);
3691
3692 SetSelectionToWindow(wnd);
3693 }
3694
3695 void wxAuiNotebook::OnTabBgDClick(wxAuiNotebookEvent& WXUNUSED(evt))
3696 {
3697 // notify owner that the tabbar background has been double-clicked
3698 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_BG_DCLICK, m_windowId);
3699 e.SetEventObject(this);
3700 GetEventHandler()->ProcessEvent(e);
3701 }
3702
3703 void wxAuiNotebook::OnTabBeginDrag(wxAuiNotebookEvent&)
3704 {
3705 m_last_drag_x = 0;
3706 }
3707
3708 void wxAuiNotebook::OnTabDragMotion(wxAuiNotebookEvent& evt)
3709 {
3710 wxPoint screen_pt = ::wxGetMousePosition();
3711 wxPoint client_pt = ScreenToClient(screen_pt);
3712 wxPoint zero(0,0);
3713
3714 wxAuiTabCtrl* src_tabs = (wxAuiTabCtrl*)evt.GetEventObject();
3715 wxAuiTabCtrl* dest_tabs = GetTabCtrlFromPoint(client_pt);
3716
3717 if (dest_tabs == src_tabs)
3718 {
3719 if (src_tabs)
3720 {
3721 src_tabs->SetCursor(wxCursor(wxCURSOR_ARROW));
3722 }
3723
3724 // always hide the hint for inner-tabctrl drag
3725 m_mgr.HideHint();
3726
3727 // if tab moving is not allowed, leave
3728 if (!(m_flags & wxAUI_NB_TAB_MOVE))
3729 {
3730 return;
3731 }
3732
3733 wxPoint pt = dest_tabs->ScreenToClient(screen_pt);
3734 wxWindow* dest_location_tab;
3735
3736 // this is an inner-tab drag/reposition
3737 if (dest_tabs->TabHitTest(pt.x, pt.y, &dest_location_tab))
3738 {
3739 int src_idx = evt.GetSelection();
3740 int dest_idx = dest_tabs->GetIdxFromWindow(dest_location_tab);
3741
3742 // prevent jumpy drag
3743 if ((src_idx == dest_idx) || dest_idx == -1 ||
3744 (src_idx > dest_idx && m_last_drag_x <= pt.x) ||
3745 (src_idx < dest_idx && m_last_drag_x >= pt.x))
3746 {
3747 m_last_drag_x = pt.x;
3748 return;
3749 }
3750
3751
3752 wxWindow* src_tab = dest_tabs->GetWindowFromIdx(src_idx);
3753 dest_tabs->MovePage(src_tab, dest_idx);
3754 dest_tabs->SetActivePage((size_t)dest_idx);
3755 dest_tabs->DoShowHide();
3756 dest_tabs->Refresh();
3757 m_last_drag_x = pt.x;
3758
3759 }
3760
3761 return;
3762 }
3763
3764
3765 // if external drag is allowed, check if the tab is being dragged
3766 // over a different wxAuiNotebook control
3767 if (m_flags & wxAUI_NB_TAB_EXTERNAL_MOVE)
3768 {
3769 wxWindow* tab_ctrl = ::wxFindWindowAtPoint(screen_pt);
3770
3771 // if we aren't over any window, stop here
3772 if (!tab_ctrl)
3773 return;
3774
3775 // make sure we are not over the hint window
3776 if (!tab_ctrl->IsKindOf(CLASSINFO(wxFrame)))
3777 {
3778 while (tab_ctrl)
3779 {
3780 if (tab_ctrl->IsKindOf(CLASSINFO(wxAuiTabCtrl)))
3781 break;
3782 tab_ctrl = tab_ctrl->GetParent();
3783 }
3784
3785 if (tab_ctrl)
3786 {
3787 wxAuiNotebook* nb = (wxAuiNotebook*)tab_ctrl->GetParent();
3788
3789 if (nb != this)
3790 {
3791 wxRect hint_rect = tab_ctrl->GetClientRect();
3792 tab_ctrl->ClientToScreen(&hint_rect.x, &hint_rect.y);
3793 m_mgr.ShowHint(hint_rect);
3794 return;
3795 }
3796 }
3797 }
3798 else
3799 {
3800 if (!dest_tabs)
3801 {
3802 // we are either over a hint window, or not over a tab
3803 // window, and there is no where to drag to, so exit
3804 return;
3805 }
3806 }
3807 }
3808
3809
3810 // if there are less than two panes, split can't happen, so leave
3811 if (m_tabs.GetPageCount() < 2)
3812 return;
3813
3814 // if tab moving is not allowed, leave
3815 if (!(m_flags & wxAUI_NB_TAB_SPLIT))
3816 return;
3817
3818
3819 if (src_tabs)
3820 {
3821 src_tabs->SetCursor(wxCursor(wxCURSOR_SIZING));
3822 }
3823
3824
3825 if (dest_tabs)
3826 {
3827 wxRect hint_rect = dest_tabs->GetRect();
3828 ClientToScreen(&hint_rect.x, &hint_rect.y);
3829 m_mgr.ShowHint(hint_rect);
3830 }
3831 else
3832 {
3833 m_mgr.DrawHintRect(m_dummy_wnd, client_pt, zero);
3834 }
3835 }
3836
3837
3838
3839 void wxAuiNotebook::OnTabEndDrag(wxAuiNotebookEvent& evt)
3840 {
3841 m_mgr.HideHint();
3842
3843
3844 wxAuiTabCtrl* src_tabs = (wxAuiTabCtrl*)evt.GetEventObject();
3845 wxCHECK_RET( src_tabs, wxT("no source object?") );
3846
3847 src_tabs->SetCursor(wxCursor(wxCURSOR_ARROW));
3848
3849 // get the mouse position, which will be used to determine the drop point
3850 wxPoint mouse_screen_pt = ::wxGetMousePosition();
3851 wxPoint mouse_client_pt = ScreenToClient(mouse_screen_pt);
3852
3853
3854
3855 // check for an external move
3856 if (m_flags & wxAUI_NB_TAB_EXTERNAL_MOVE)
3857 {
3858 wxWindow* tab_ctrl = ::wxFindWindowAtPoint(mouse_screen_pt);
3859
3860 while (tab_ctrl)
3861 {
3862 if (tab_ctrl->IsKindOf(CLASSINFO(wxAuiTabCtrl)))
3863 break;
3864 tab_ctrl = tab_ctrl->GetParent();
3865 }
3866
3867 if (tab_ctrl)
3868 {
3869 wxAuiNotebook* nb = (wxAuiNotebook*)tab_ctrl->GetParent();
3870
3871 if (nb != this)
3872 {
3873 // find out from the destination control
3874 // if it's ok to drop this tab here
3875 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_ALLOW_DND, m_windowId);
3876 e.SetSelection(evt.GetSelection());
3877 e.SetOldSelection(evt.GetSelection());
3878 e.SetEventObject(this);
3879 e.SetDragSource(this);
3880 e.Veto(); // dropping must be explicitly approved by control owner
3881
3882 nb->GetEventHandler()->ProcessEvent(e);
3883
3884 if (!e.IsAllowed())
3885 {
3886 // no answer or negative answer
3887 m_mgr.HideHint();
3888 return;
3889 }
3890
3891 // drop was allowed
3892 int src_idx = evt.GetSelection();
3893 wxWindow* src_page = src_tabs->GetWindowFromIdx(src_idx);
3894
3895 // Check that it's not an impossible parent relationship
3896 wxWindow* p = nb;
3897 while (p && !p->IsTopLevel())
3898 {
3899 if (p == src_page)
3900 {
3901 return;
3902 }
3903 p = p->GetParent();
3904 }
3905
3906 // get main index of the page
3907 int main_idx = m_tabs.GetIdxFromWindow(src_page);
3908 wxCHECK_RET( main_idx != wxNOT_FOUND, wxT("no source page?") );
3909
3910
3911 // make a copy of the page info
3912 wxAuiNotebookPage page_info = m_tabs.GetPage(main_idx);
3913
3914 // remove the page from the source notebook
3915 RemovePage(main_idx);
3916
3917 // reparent the page
3918 src_page->Reparent(nb);
3919
3920
3921 // found out the insert idx
3922 wxAuiTabCtrl* dest_tabs = (wxAuiTabCtrl*)tab_ctrl;
3923 wxPoint pt = dest_tabs->ScreenToClient(mouse_screen_pt);
3924
3925 wxWindow* target = NULL;
3926 int insert_idx = -1;
3927 dest_tabs->TabHitTest(pt.x, pt.y, &target);
3928 if (target)
3929 {
3930 insert_idx = dest_tabs->GetIdxFromWindow(target);
3931 }
3932
3933
3934 // add the page to the new notebook
3935 if (insert_idx == -1)
3936 insert_idx = dest_tabs->GetPageCount();
3937 dest_tabs->InsertPage(page_info.window, page_info, insert_idx);
3938 nb->m_tabs.AddPage(page_info.window, page_info);
3939
3940 nb->DoSizing();
3941 dest_tabs->DoShowHide();
3942 dest_tabs->Refresh();
3943
3944 // set the selection in the destination tab control
3945 nb->SetSelectionToPage(page_info);
3946
3947 // notify owner that the tab has been dragged
3948 wxAuiNotebookEvent e2(wxEVT_COMMAND_AUINOTEBOOK_DRAG_DONE, m_windowId);
3949 e2.SetSelection(evt.GetSelection());
3950 e2.SetOldSelection(evt.GetSelection());
3951 e2.SetEventObject(this);
3952 GetEventHandler()->ProcessEvent(e2);
3953
3954 return;
3955 }
3956 }
3957 }
3958
3959
3960
3961
3962 // only perform a tab split if it's allowed
3963 wxAuiTabCtrl* dest_tabs = NULL;
3964
3965 if ((m_flags & wxAUI_NB_TAB_SPLIT) && m_tabs.GetPageCount() >= 2)
3966 {
3967 // If the pointer is in an existing tab frame, do a tab insert
3968 wxWindow* hit_wnd = ::wxFindWindowAtPoint(mouse_screen_pt);
3969 wxTabFrame* tab_frame = (wxTabFrame*)GetTabFrameFromTabCtrl(hit_wnd);
3970 int insert_idx = -1;
3971 if (tab_frame)
3972 {
3973 dest_tabs = tab_frame->m_tabs;
3974
3975 if (dest_tabs == src_tabs)
3976 return;
3977
3978
3979 wxPoint pt = dest_tabs->ScreenToClient(mouse_screen_pt);
3980 wxWindow* target = NULL;
3981 dest_tabs->TabHitTest(pt.x, pt.y, &target);
3982 if (target)
3983 {
3984 insert_idx = dest_tabs->GetIdxFromWindow(target);
3985 }
3986 }
3987 else
3988 {
3989 wxPoint zero(0,0);
3990 wxRect rect = m_mgr.CalculateHintRect(m_dummy_wnd,
3991 mouse_client_pt,
3992 zero);
3993 if (rect.IsEmpty())
3994 {
3995 // there is no suitable drop location here, exit out
3996 return;
3997 }
3998
3999 // If there is no tabframe at all, create one
4000 wxTabFrame* new_tabs = new wxTabFrame;
4001 new_tabs->m_rect = wxRect(wxPoint(0,0), CalculateNewSplitSize());
4002 new_tabs->SetTabCtrlHeight(m_tab_ctrl_height);
4003 new_tabs->m_tabs = new wxAuiTabCtrl(this,
4004 m_tab_id_counter++,
4005 wxDefaultPosition,
4006 wxDefaultSize,
4007 wxNO_BORDER|wxWANTS_CHARS);
4008 new_tabs->m_tabs->SetArtProvider(m_tabs.GetArtProvider()->Clone());
4009 new_tabs->m_tabs->SetFlags(m_flags);
4010
4011 m_mgr.AddPane(new_tabs,
4012 wxAuiPaneInfo().Bottom().CaptionVisible(false),
4013 mouse_client_pt);
4014 m_mgr.Update();
4015 dest_tabs = new_tabs->m_tabs;
4016 }
4017
4018
4019
4020 // remove the page from the source tabs
4021 wxAuiNotebookPage page_info = src_tabs->GetPage(evt.GetSelection());
4022 page_info.active = false;
4023 src_tabs->RemovePage(page_info.window);
4024 if (src_tabs->GetPageCount() > 0)
4025 {
4026 src_tabs->SetActivePage((size_t)0);
4027 src_tabs->DoShowHide();
4028 src_tabs->Refresh();
4029 }
4030
4031
4032
4033 // add the page to the destination tabs
4034 if (insert_idx == -1)
4035 insert_idx = dest_tabs->GetPageCount();
4036 dest_tabs->InsertPage(page_info.window, page_info, insert_idx);
4037
4038 if (src_tabs->GetPageCount() == 0)
4039 {
4040 RemoveEmptyTabFrames();
4041 }
4042
4043 DoSizing();
4044 dest_tabs->DoShowHide();
4045 dest_tabs->Refresh();
4046
4047 // force the set selection function reset the selection
4048 m_curpage = -1;
4049
4050 // set the active page to the one we just split off
4051 SetSelectionToPage(page_info);
4052
4053 UpdateHintWindowSize();
4054 }
4055
4056 // notify owner that the tab has been dragged
4057 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_DRAG_DONE, m_windowId);
4058 e.SetSelection(evt.GetSelection());
4059 e.SetOldSelection(evt.GetSelection());
4060 e.SetEventObject(this);
4061 GetEventHandler()->ProcessEvent(e);
4062 }
4063
4064
4065
4066 wxAuiTabCtrl* wxAuiNotebook::GetTabCtrlFromPoint(const wxPoint& pt)
4067 {
4068 // if we've just removed the last tab from the source
4069 // tab set, the remove the tab control completely
4070 wxAuiPaneInfoArray& all_panes = m_mgr.GetAllPanes();
4071 size_t i, pane_count = all_panes.GetCount();
4072 for (i = 0; i < pane_count; ++i)
4073 {
4074 if (all_panes.Item(i).name == wxT("dummy"))
4075 continue;
4076
4077 wxTabFrame* tabframe = (wxTabFrame*)all_panes.Item(i).window;
4078 if (tabframe->m_tab_rect.Contains(pt))
4079 return tabframe->m_tabs;
4080 }
4081
4082 return NULL;
4083 }
4084
4085 wxWindow* wxAuiNotebook::GetTabFrameFromTabCtrl(wxWindow* tab_ctrl)
4086 {
4087 // if we've just removed the last tab from the source
4088 // tab set, the remove the tab control completely
4089 wxAuiPaneInfoArray& all_panes = m_mgr.GetAllPanes();
4090 size_t i, pane_count = all_panes.GetCount();
4091 for (i = 0; i < pane_count; ++i)
4092 {
4093 if (all_panes.Item(i).name == wxT("dummy"))
4094 continue;
4095
4096 wxTabFrame* tabframe = (wxTabFrame*)all_panes.Item(i).window;
4097 if (tabframe->m_tabs == tab_ctrl)
4098 {
4099 return tabframe;
4100 }
4101 }
4102
4103 return NULL;
4104 }
4105
4106 void wxAuiNotebook::RemoveEmptyTabFrames()
4107 {
4108 // if we've just removed the last tab from the source
4109 // tab set, the remove the tab control completely
4110 wxAuiPaneInfoArray all_panes = m_mgr.GetAllPanes();
4111 size_t i, pane_count = all_panes.GetCount();
4112 for (i = 0; i < pane_count; ++i)
4113 {
4114 if (all_panes.Item(i).name == wxT("dummy"))
4115 continue;
4116
4117 wxTabFrame* tab_frame = (wxTabFrame*)all_panes.Item(i).window;
4118 if (tab_frame->m_tabs->GetPageCount() == 0)
4119 {
4120 m_mgr.DetachPane(tab_frame);
4121
4122 // use pending delete because sometimes during
4123 // window closing, refreshs are pending
4124 if (!wxPendingDelete.Member(tab_frame->m_tabs))
4125 wxPendingDelete.Append(tab_frame->m_tabs);
4126
4127 tab_frame->m_tabs = NULL;
4128
4129 delete tab_frame;
4130 }
4131 }
4132
4133
4134 // check to see if there is still a center pane;
4135 // if there isn't, make a frame the center pane
4136 wxAuiPaneInfoArray panes = m_mgr.GetAllPanes();
4137 pane_count = panes.GetCount();
4138 wxWindow* first_good = NULL;
4139 bool center_found = false;
4140 for (i = 0; i < pane_count; ++i)
4141 {
4142 if (panes.Item(i).name == wxT("dummy"))
4143 continue;
4144 if (panes.Item(i).dock_direction == wxAUI_DOCK_CENTRE)
4145 center_found = true;
4146 if (!first_good)
4147 first_good = panes.Item(i).window;
4148 }
4149
4150 if (!center_found && first_good)
4151 {
4152 m_mgr.GetPane(first_good).Centre();
4153 }
4154
4155 if (!m_isBeingDeleted)
4156 m_mgr.Update();
4157 }
4158
4159 void wxAuiNotebook::OnChildFocusNotebook(wxChildFocusEvent& evt)
4160 {
4161 // if we're dragging a tab, don't change the current selection.
4162 // This code prevents a bug that used to happen when the hint window
4163 // was hidden. In the bug, the focus would return to the notebook
4164 // child, which would then enter this handler and call
4165 // SetSelection, which is not desired turn tab dragging.
4166
4167 wxAuiPaneInfoArray& all_panes = m_mgr.GetAllPanes();
4168 size_t i, pane_count = all_panes.GetCount();
4169 for (i = 0; i < pane_count; ++i)
4170 {
4171 wxAuiPaneInfo& pane = all_panes.Item(i);
4172 if (pane.name == wxT("dummy"))
4173 continue;
4174 wxTabFrame* tabframe = (wxTabFrame*)pane.window;
4175 if (tabframe->m_tabs->IsDragging())
4176 return;
4177 }
4178
4179
4180 // change the tab selection to the child
4181 // which was focused
4182 int idx = m_tabs.GetIdxFromWindow(evt.GetWindow());
4183 if (idx != -1 && idx != m_curpage)
4184 {
4185 SetSelection(idx);
4186 }
4187 }
4188
4189 void wxAuiNotebook::OnNavigationKeyNotebook(wxNavigationKeyEvent& event)
4190 {
4191 if ( event.IsWindowChange() ) {
4192 // change pages
4193 // FIXME: the problem with this is that if we have a split notebook,
4194 // we selection may go all over the place.
4195 AdvanceSelection(event.GetDirection());
4196 }
4197 else {
4198 // we get this event in 3 cases
4199 //
4200 // a) one of our pages might have generated it because the user TABbed
4201 // out from it in which case we should propagate the event upwards and
4202 // our parent will take care of setting the focus to prev/next sibling
4203 //
4204 // or
4205 //
4206 // b) the parent panel wants to give the focus to us so that we
4207 // forward it to our selected page. We can't deal with this in
4208 // OnSetFocus() because we don't know which direction the focus came
4209 // from in this case and so can't choose between setting the focus to
4210 // first or last panel child
4211 //
4212 // or
4213 //
4214 // c) we ourselves (see MSWTranslateMessage) generated the event
4215 //
4216 wxWindow * const parent = GetParent();
4217
4218 // the wxObject* casts are required to avoid MinGW GCC 2.95.3 ICE
4219 const bool isFromParent = event.GetEventObject() == (wxObject*) parent;
4220 const bool isFromSelf = event.GetEventObject() == (wxObject*) this;
4221
4222 if ( isFromParent || isFromSelf )
4223 {
4224 // no, it doesn't come from child, case (b) or (c): forward to a
4225 // page but only if direction is backwards (TAB) or from ourselves,
4226 if ( GetSelection() != wxNOT_FOUND &&
4227 (!event.GetDirection() || isFromSelf) )
4228 {
4229 // so that the page knows that the event comes from it's parent
4230 // and is being propagated downwards
4231 event.SetEventObject(this);
4232
4233 wxWindow *page = GetPage(GetSelection());
4234 if ( !page->GetEventHandler()->ProcessEvent(event) )
4235 {
4236 page->SetFocus();
4237 }
4238 //else: page manages focus inside it itself
4239 }
4240 else // otherwise set the focus to the notebook itself
4241 {
4242 SetFocus();
4243 }
4244 }
4245 else
4246 {
4247 // it comes from our child, case (a), pass to the parent, but only
4248 // if the direction is forwards. Otherwise set the focus to the
4249 // notebook itself. The notebook is always the 'first' control of a
4250 // page.
4251 if ( !event.GetDirection() )
4252 {
4253 SetFocus();
4254 }
4255 else if ( parent )
4256 {
4257 event.SetCurrentFocus(this);
4258 parent->GetEventHandler()->ProcessEvent(event);
4259 }
4260 }
4261 }
4262 }
4263
4264 void wxAuiNotebook::OnTabButton(wxAuiNotebookEvent& evt)
4265 {
4266 wxAuiTabCtrl* tabs = (wxAuiTabCtrl*)evt.GetEventObject();
4267
4268 int button_id = evt.GetInt();
4269
4270 if (button_id == wxAUI_BUTTON_CLOSE)
4271 {
4272 int selection = evt.GetSelection();
4273
4274 if (selection == -1)
4275 {
4276 // if the close button is to the right, use the active
4277 // page selection to determine which page to close
4278 selection = tabs->GetActivePage();
4279 }
4280
4281 if (selection != -1)
4282 {
4283 wxWindow* close_wnd = tabs->GetWindowFromIdx(selection);
4284
4285 // ask owner if it's ok to close the tab
4286 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CLOSE, m_windowId);
4287 e.SetSelection(m_tabs.GetIdxFromWindow(close_wnd));
4288 const int idx = m_tabs.GetIdxFromWindow(close_wnd);
4289 e.SetSelection(idx);
4290 e.SetOldSelection(evt.GetSelection());
4291 e.SetEventObject(this);
4292 GetEventHandler()->ProcessEvent(e);
4293 if (!e.IsAllowed())
4294 return;
4295
4296
4297 #if wxUSE_MDI
4298 if (close_wnd->IsKindOf(CLASSINFO(wxAuiMDIChildFrame)))
4299 {
4300 close_wnd->Close();
4301 }
4302 else
4303 #endif
4304 {
4305 int main_idx = m_tabs.GetIdxFromWindow(close_wnd);
4306 wxCHECK_RET( main_idx != wxNOT_FOUND, wxT("no page to delete?") );
4307
4308 DeletePage(main_idx);
4309 }
4310
4311 // notify owner that the tab has been closed
4312 wxAuiNotebookEvent e2(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CLOSED, m_windowId);
4313 e2.SetSelection(idx);
4314 e2.SetEventObject(this);
4315 GetEventHandler()->ProcessEvent(e2);
4316 }
4317 }
4318 }
4319
4320
4321 void wxAuiNotebook::OnTabMiddleDown(wxAuiNotebookEvent& evt)
4322 {
4323 // patch event through to owner
4324 wxAuiTabCtrl* tabs = (wxAuiTabCtrl*)evt.GetEventObject();
4325 wxWindow* wnd = tabs->GetWindowFromIdx(evt.GetSelection());
4326
4327 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_TAB_MIDDLE_DOWN, m_windowId);
4328 e.SetSelection(m_tabs.GetIdxFromWindow(wnd));
4329 e.SetEventObject(this);
4330 GetEventHandler()->ProcessEvent(e);
4331 }
4332
4333 void wxAuiNotebook::OnTabMiddleUp(wxAuiNotebookEvent& evt)
4334 {
4335 // if the wxAUI_NB_MIDDLE_CLICK_CLOSE is specified, middle
4336 // click should act like a tab close action. However, first
4337 // give the owner an opportunity to handle the middle up event
4338 // for custom action
4339
4340 wxAuiTabCtrl* tabs = (wxAuiTabCtrl*)evt.GetEventObject();
4341 wxWindow* wnd = tabs->GetWindowFromIdx(evt.GetSelection());
4342
4343 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_TAB_MIDDLE_UP, m_windowId);
4344 e.SetSelection(m_tabs.GetIdxFromWindow(wnd));
4345 e.SetEventObject(this);
4346 if (GetEventHandler()->ProcessEvent(e))
4347 return;
4348 if (!e.IsAllowed())
4349 return;
4350
4351 // check if we are supposed to close on middle-up
4352 if ((m_flags & wxAUI_NB_MIDDLE_CLICK_CLOSE) == 0)
4353 return;
4354
4355 // simulate the user pressing the close button on the tab
4356 evt.SetInt(wxAUI_BUTTON_CLOSE);
4357 OnTabButton(evt);
4358 }
4359
4360 void wxAuiNotebook::OnTabRightDown(wxAuiNotebookEvent& evt)
4361 {
4362 // patch event through to owner
4363 wxAuiTabCtrl* tabs = (wxAuiTabCtrl*)evt.GetEventObject();
4364 wxWindow* wnd = tabs->GetWindowFromIdx(evt.GetSelection());
4365
4366 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_TAB_RIGHT_DOWN, m_windowId);
4367 e.SetSelection(m_tabs.GetIdxFromWindow(wnd));
4368 e.SetEventObject(this);
4369 GetEventHandler()->ProcessEvent(e);
4370 }
4371
4372 void wxAuiNotebook::OnTabRightUp(wxAuiNotebookEvent& evt)
4373 {
4374 // patch event through to owner
4375 wxAuiTabCtrl* tabs = (wxAuiTabCtrl*)evt.GetEventObject();
4376 wxWindow* wnd = tabs->GetWindowFromIdx(evt.GetSelection());
4377
4378 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_TAB_RIGHT_UP, m_windowId);
4379 e.SetSelection(m_tabs.GetIdxFromWindow(wnd));
4380 e.SetEventObject(this);
4381 GetEventHandler()->ProcessEvent(e);
4382 }
4383
4384 // Sets the normal font
4385 void wxAuiNotebook::SetNormalFont(const wxFont& font)
4386 {
4387 m_normal_font = font;
4388 GetArtProvider()->SetNormalFont(font);
4389 }
4390
4391 // Sets the selected tab font
4392 void wxAuiNotebook::SetSelectedFont(const wxFont& font)
4393 {
4394 m_selected_font = font;
4395 GetArtProvider()->SetSelectedFont(font);
4396 }
4397
4398 // Sets the measuring font
4399 void wxAuiNotebook::SetMeasuringFont(const wxFont& font)
4400 {
4401 GetArtProvider()->SetMeasuringFont(font);
4402 }
4403
4404 // Sets the tab font
4405 bool wxAuiNotebook::SetFont(const wxFont& font)
4406 {
4407 wxControl::SetFont(font);
4408
4409 wxFont normalFont(font);
4410 wxFont selectedFont(normalFont);
4411 selectedFont.SetWeight(wxBOLD);
4412
4413 SetNormalFont(normalFont);
4414 SetSelectedFont(selectedFont);
4415 SetMeasuringFont(selectedFont);
4416
4417 return true;
4418 }
4419
4420 // Gets the tab control height
4421 int wxAuiNotebook::GetTabCtrlHeight() const
4422 {
4423 return m_tab_ctrl_height;
4424 }
4425
4426 // Gets the height of the notebook for a given page height
4427 int wxAuiNotebook::GetHeightForPageHeight(int pageHeight)
4428 {
4429 UpdateTabCtrlHeight();
4430
4431 int tabCtrlHeight = GetTabCtrlHeight();
4432 int decorHeight = 2;
4433 return tabCtrlHeight + pageHeight + decorHeight;
4434 }
4435
4436 // Advances the selection, generation page selection events
4437 void wxAuiNotebook::AdvanceSelection(bool forward)
4438 {
4439 if (GetPageCount() <= 1)
4440 return;
4441
4442 int currentSelection = GetSelection();
4443
4444 if (forward)
4445 {
4446 if (currentSelection == (int) (GetPageCount() - 1))
4447 return;
4448 else if (currentSelection == -1)
4449 currentSelection = 0;
4450 else
4451 currentSelection ++;
4452 }
4453 else
4454 {
4455 if (currentSelection <= 0)
4456 return;
4457 else
4458 currentSelection --;
4459 }
4460
4461 SetSelection(currentSelection);
4462 }
4463
4464 // Shows the window menu
4465 bool wxAuiNotebook::ShowWindowMenu()
4466 {
4467 wxAuiTabCtrl* tabCtrl = GetActiveTabCtrl();
4468
4469 int idx = tabCtrl->GetArtProvider()->ShowDropDown(tabCtrl, tabCtrl->GetPages(), tabCtrl->GetActivePage());
4470
4471 if (idx != -1)
4472 {
4473 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING, tabCtrl->GetId());
4474 e.SetSelection(idx);
4475 e.SetOldSelection(tabCtrl->GetActivePage());
4476 e.SetEventObject(tabCtrl);
4477 GetEventHandler()->ProcessEvent(e);
4478
4479 return true;
4480 }
4481 else
4482 return false;
4483 }
4484
4485 void wxAuiNotebook::Thaw()
4486 {
4487 DoSizing();
4488
4489 wxControl::Thaw();
4490 }
4491
4492 #endif // wxUSE_AUI