]> git.saurik.com Git - wxWidgets.git/blob - src/aui/auibook.cpp
String formatting fixes
[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: Jens Lody
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/dcclient.h"
28 #include "wx/dcmemory.h"
29 #endif
30
31 #include "wx/aui/tabmdi.h"
32
33 #ifdef __WXMAC__
34 #include "wx/osx/private.h"
35 #endif
36
37 #include "wx/arrimpl.cpp"
38 WX_DEFINE_OBJARRAY(wxAuiNotebookPageArray)
39 WX_DEFINE_OBJARRAY(wxAuiTabContainerButtonArray)
40
41 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CLOSE, wxAuiNotebookEvent);
42 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CLOSED, wxAuiNotebookEvent);
43 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING, wxAuiNotebookEvent);
44 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGED, wxAuiNotebookEvent);
45 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_BUTTON, wxAuiNotebookEvent);
46 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_BEGIN_DRAG, wxAuiNotebookEvent);
47 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_END_DRAG, wxAuiNotebookEvent);
48 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_CANCEL_DRAG, wxAuiNotebookEvent);
49 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_DRAG_MOTION, wxAuiNotebookEvent);
50 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_ALLOW_DND, wxAuiNotebookEvent);
51 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_BG_DCLICK, wxAuiNotebookEvent);
52 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_DRAG_DONE, wxAuiNotebookEvent);
53 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_TAB_MIDDLE_UP, wxAuiNotebookEvent);
54 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_TAB_MIDDLE_DOWN, wxAuiNotebookEvent);
55 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_TAB_RIGHT_UP, wxAuiNotebookEvent);
56 wxDEFINE_EVENT(wxEVT_COMMAND_AUINOTEBOOK_TAB_RIGHT_DOWN, wxAuiNotebookEvent);
57
58 IMPLEMENT_CLASS(wxAuiNotebook, wxControl)
59 IMPLEMENT_CLASS(wxAuiTabCtrl, wxControl)
60 IMPLEMENT_DYNAMIC_CLASS(wxAuiNotebookEvent, wxBookCtrlEvent)
61
62
63 // -- wxAuiTabContainer class implementation --
64
65
66 // wxAuiTabContainer is a class which contains information about each
67 // tab. It also can render an entire tab control to a specified DC.
68 // It's not a window class itself, because this code will be used by
69 // the wxFrameMananger, where it is disadvantageous to have separate
70 // windows for each tab control in the case of "docked tabs"
71
72 // A derived class, wxAuiTabCtrl, is an actual wxWindow-derived window
73 // which can be used as a tab control in the normal sense.
74
75
76 wxAuiTabContainer::wxAuiTabContainer()
77 {
78 m_tabOffset = 0;
79 m_flags = 0;
80 m_art = new wxAuiDefaultTabArt;
81
82 AddButton(wxAUI_BUTTON_LEFT, wxLEFT);
83 AddButton(wxAUI_BUTTON_RIGHT, wxRIGHT);
84 AddButton(wxAUI_BUTTON_WINDOWLIST, wxRIGHT);
85 AddButton(wxAUI_BUTTON_CLOSE, wxRIGHT);
86 }
87
88 wxAuiTabContainer::~wxAuiTabContainer()
89 {
90 delete m_art;
91 }
92
93 void wxAuiTabContainer::SetArtProvider(wxAuiTabArt* art)
94 {
95 delete m_art;
96 m_art = art;
97
98 if (m_art)
99 {
100 m_art->SetFlags(m_flags);
101 }
102 }
103
104 wxAuiTabArt* wxAuiTabContainer::GetArtProvider() const
105 {
106 return m_art;
107 }
108
109 void wxAuiTabContainer::SetFlags(unsigned int flags)
110 {
111 m_flags = flags;
112
113 // check for new close button settings
114 RemoveButton(wxAUI_BUTTON_LEFT);
115 RemoveButton(wxAUI_BUTTON_RIGHT);
116 RemoveButton(wxAUI_BUTTON_WINDOWLIST);
117 RemoveButton(wxAUI_BUTTON_CLOSE);
118
119
120 if (flags & wxAUI_NB_SCROLL_BUTTONS)
121 {
122 AddButton(wxAUI_BUTTON_LEFT, wxLEFT);
123 AddButton(wxAUI_BUTTON_RIGHT, wxRIGHT);
124 }
125
126 if (flags & wxAUI_NB_WINDOWLIST_BUTTON)
127 {
128 AddButton(wxAUI_BUTTON_WINDOWLIST, wxRIGHT);
129 }
130
131 if (flags & wxAUI_NB_CLOSE_BUTTON)
132 {
133 AddButton(wxAUI_BUTTON_CLOSE, wxRIGHT);
134 }
135
136 if (m_art)
137 {
138 m_art->SetFlags(m_flags);
139 }
140 }
141
142 unsigned int wxAuiTabContainer::GetFlags() const
143 {
144 return m_flags;
145 }
146
147
148 void wxAuiTabContainer::SetNormalFont(const wxFont& font)
149 {
150 m_art->SetNormalFont(font);
151 }
152
153 void wxAuiTabContainer::SetSelectedFont(const wxFont& font)
154 {
155 m_art->SetSelectedFont(font);
156 }
157
158 void wxAuiTabContainer::SetMeasuringFont(const wxFont& font)
159 {
160 m_art->SetMeasuringFont(font);
161 }
162
163 void wxAuiTabContainer::SetColour(const wxColour& colour)
164 {
165 m_art->SetColour(colour);
166 }
167
168 void wxAuiTabContainer::SetActiveColour(const wxColour& colour)
169 {
170 m_art->SetActiveColour(colour);
171 }
172
173 void wxAuiTabContainer::SetRect(const wxRect& rect)
174 {
175 m_rect = rect;
176
177 if (m_art)
178 {
179 m_art->SetSizingInfo(rect.GetSize(), m_pages.GetCount());
180 }
181 }
182
183 bool wxAuiTabContainer::AddPage(wxWindow* page,
184 const wxAuiNotebookPage& info)
185 {
186 wxAuiNotebookPage page_info;
187 page_info = info;
188 page_info.window = page;
189
190 m_pages.Add(page_info);
191
192 // let the art provider know how many pages we have
193 if (m_art)
194 {
195 m_art->SetSizingInfo(m_rect.GetSize(), m_pages.GetCount());
196 }
197
198 return true;
199 }
200
201 bool wxAuiTabContainer::InsertPage(wxWindow* page,
202 const wxAuiNotebookPage& info,
203 size_t idx)
204 {
205 wxAuiNotebookPage page_info;
206 page_info = info;
207 page_info.window = page;
208
209 if (idx >= m_pages.GetCount())
210 m_pages.Add(page_info);
211 else
212 m_pages.Insert(page_info, idx);
213
214 // let the art provider know how many pages we have
215 if (m_art)
216 {
217 m_art->SetSizingInfo(m_rect.GetSize(), m_pages.GetCount());
218 }
219
220 return true;
221 }
222
223 bool wxAuiTabContainer::MovePage(wxWindow* page,
224 size_t new_idx)
225 {
226 int idx = GetIdxFromWindow(page);
227 if (idx == -1)
228 return false;
229
230 // get page entry, make a copy of it
231 wxAuiNotebookPage p = GetPage(idx);
232
233 // remove old page entry
234 RemovePage(page);
235
236 // insert page where it should be
237 InsertPage(page, p, new_idx);
238
239 return true;
240 }
241
242 bool wxAuiTabContainer::RemovePage(wxWindow* wnd)
243 {
244 size_t i, page_count = m_pages.GetCount();
245 for (i = 0; i < page_count; ++i)
246 {
247 wxAuiNotebookPage& page = m_pages.Item(i);
248 if (page.window == wnd)
249 {
250 m_pages.RemoveAt(i);
251
252 // let the art provider know how many pages we have
253 if (m_art)
254 {
255 m_art->SetSizingInfo(m_rect.GetSize(), m_pages.GetCount());
256 }
257
258 return true;
259 }
260 }
261
262 return false;
263 }
264
265 bool wxAuiTabContainer::SetActivePage(wxWindow* wnd)
266 {
267 bool found = false;
268
269 size_t i, page_count = m_pages.GetCount();
270 for (i = 0; i < page_count; ++i)
271 {
272 wxAuiNotebookPage& page = m_pages.Item(i);
273 if (page.window == wnd)
274 {
275 page.active = true;
276 found = true;
277 }
278 else
279 {
280 page.active = false;
281 }
282 }
283
284 return found;
285 }
286
287 void wxAuiTabContainer::SetNoneActive()
288 {
289 size_t i, page_count = m_pages.GetCount();
290 for (i = 0; i < page_count; ++i)
291 {
292 wxAuiNotebookPage& page = m_pages.Item(i);
293 page.active = false;
294 }
295 }
296
297 bool wxAuiTabContainer::SetActivePage(size_t page)
298 {
299 if (page >= m_pages.GetCount())
300 return false;
301
302 return SetActivePage(m_pages.Item(page).window);
303 }
304
305 int wxAuiTabContainer::GetActivePage() const
306 {
307 size_t i, page_count = m_pages.GetCount();
308 for (i = 0; i < page_count; ++i)
309 {
310 wxAuiNotebookPage& page = m_pages.Item(i);
311 if (page.active)
312 return i;
313 }
314
315 return -1;
316 }
317
318 wxWindow* wxAuiTabContainer::GetWindowFromIdx(size_t idx) const
319 {
320 if (idx >= m_pages.GetCount())
321 return NULL;
322
323 return m_pages[idx].window;
324 }
325
326 int wxAuiTabContainer::GetIdxFromWindow(wxWindow* wnd) const
327 {
328 const size_t page_count = m_pages.GetCount();
329 for ( size_t i = 0; i < page_count; ++i )
330 {
331 wxAuiNotebookPage& page = m_pages.Item(i);
332 if (page.window == wnd)
333 return i;
334 }
335 return wxNOT_FOUND;
336 }
337
338 wxAuiNotebookPage& wxAuiTabContainer::GetPage(size_t idx)
339 {
340 wxASSERT_MSG(idx < m_pages.GetCount(), wxT("Invalid Page index"));
341
342 return m_pages[idx];
343 }
344
345 const wxAuiNotebookPage& wxAuiTabContainer::GetPage(size_t idx) const
346 {
347 wxASSERT_MSG(idx < m_pages.GetCount(), wxT("Invalid Page index"));
348
349 return m_pages[idx];
350 }
351
352 wxAuiNotebookPageArray& wxAuiTabContainer::GetPages()
353 {
354 return m_pages;
355 }
356
357 size_t wxAuiTabContainer::GetPageCount() const
358 {
359 return m_pages.GetCount();
360 }
361
362 void wxAuiTabContainer::AddButton(int id,
363 int location,
364 const wxBitmap& normalBitmap,
365 const wxBitmap& disabledBitmap)
366 {
367 wxAuiTabContainerButton button;
368 button.id = id;
369 button.bitmap = normalBitmap;
370 button.disBitmap = disabledBitmap;
371 button.location = location;
372 button.curState = wxAUI_BUTTON_STATE_NORMAL;
373
374 m_buttons.Add(button);
375 }
376
377 void wxAuiTabContainer::RemoveButton(int id)
378 {
379 size_t i, button_count = m_buttons.GetCount();
380
381 for (i = 0; i < button_count; ++i)
382 {
383 if (m_buttons.Item(i).id == id)
384 {
385 m_buttons.RemoveAt(i);
386 return;
387 }
388 }
389 }
390
391
392
393 size_t wxAuiTabContainer::GetTabOffset() const
394 {
395 return m_tabOffset;
396 }
397
398 void wxAuiTabContainer::SetTabOffset(size_t offset)
399 {
400 m_tabOffset = offset;
401 }
402
403
404
405
406 // Render() renders the tab catalog to the specified DC
407 // It is a virtual function and can be overridden to
408 // provide custom drawing capabilities
409 void wxAuiTabContainer::Render(wxDC* raw_dc, wxWindow* wnd)
410 {
411 if (!raw_dc || !raw_dc->IsOk())
412 return;
413
414 wxMemoryDC dc;
415
416 // use the same layout direction as the window DC uses to ensure that the
417 // text is rendered correctly
418 dc.SetLayoutDirection(raw_dc->GetLayoutDirection());
419
420 wxBitmap bmp;
421 size_t i;
422 size_t page_count = m_pages.GetCount();
423 size_t button_count = m_buttons.GetCount();
424
425 // create off-screen bitmap
426 bmp.Create(m_rect.GetWidth(), m_rect.GetHeight());
427 dc.SelectObject(bmp);
428
429 if (!dc.IsOk())
430 return;
431
432 // find out if size of tabs is larger than can be
433 // afforded on screen
434 int total_width = 0;
435 int visible_width = 0;
436 for (i = 0; i < page_count; ++i)
437 {
438 wxAuiNotebookPage& page = m_pages.Item(i);
439
440 // determine if a close button is on this tab
441 bool close_button = false;
442 if ((m_flags & wxAUI_NB_CLOSE_ON_ALL_TABS) != 0 ||
443 ((m_flags & wxAUI_NB_CLOSE_ON_ACTIVE_TAB) != 0 && page.active))
444 {
445 close_button = true;
446 }
447
448
449 int x_extent = 0;
450 wxSize size = m_art->GetTabSize(dc,
451 wnd,
452 page.caption,
453 page.bitmap,
454 page.active,
455 close_button ?
456 wxAUI_BUTTON_STATE_NORMAL :
457 wxAUI_BUTTON_STATE_HIDDEN,
458 &x_extent);
459
460 if (i+1 < page_count)
461 total_width += x_extent;
462 else
463 total_width += size.x;
464
465 if (i >= m_tabOffset)
466 {
467 if (i+1 < page_count)
468 visible_width += x_extent;
469 else
470 visible_width += size.x;
471 }
472 }
473
474 if (total_width > m_rect.GetWidth() || m_tabOffset != 0)
475 {
476 // show left/right buttons
477 for (i = 0; i < button_count; ++i)
478 {
479 wxAuiTabContainerButton& button = m_buttons.Item(i);
480 if (button.id == wxAUI_BUTTON_LEFT ||
481 button.id == wxAUI_BUTTON_RIGHT)
482 {
483 button.curState &= ~wxAUI_BUTTON_STATE_HIDDEN;
484 }
485 }
486 }
487 else
488 {
489 // hide left/right buttons
490 for (i = 0; i < button_count; ++i)
491 {
492 wxAuiTabContainerButton& button = m_buttons.Item(i);
493 if (button.id == wxAUI_BUTTON_LEFT ||
494 button.id == wxAUI_BUTTON_RIGHT)
495 {
496 button.curState |= wxAUI_BUTTON_STATE_HIDDEN;
497 }
498 }
499 }
500
501 // determine whether left button should be enabled
502 for (i = 0; i < button_count; ++i)
503 {
504 wxAuiTabContainerButton& button = m_buttons.Item(i);
505 if (button.id == wxAUI_BUTTON_LEFT)
506 {
507 if (m_tabOffset == 0)
508 button.curState |= wxAUI_BUTTON_STATE_DISABLED;
509 else
510 button.curState &= ~wxAUI_BUTTON_STATE_DISABLED;
511 }
512 if (button.id == wxAUI_BUTTON_RIGHT)
513 {
514 if (visible_width < m_rect.GetWidth() - ((int)button_count*16))
515 button.curState |= wxAUI_BUTTON_STATE_DISABLED;
516 else
517 button.curState &= ~wxAUI_BUTTON_STATE_DISABLED;
518 }
519 }
520
521
522
523 // draw background
524 m_art->DrawBackground(dc, wnd, m_rect);
525
526 // draw buttons
527 int left_buttons_width = 0;
528 int right_buttons_width = 0;
529
530 int offset = 0;
531
532 // draw the buttons on the right side
533 offset = m_rect.x + m_rect.width;
534 for (i = 0; i < button_count; ++i)
535 {
536 wxAuiTabContainerButton& button = m_buttons.Item(button_count - i - 1);
537
538 if (button.location != wxRIGHT)
539 continue;
540 if (button.curState & wxAUI_BUTTON_STATE_HIDDEN)
541 continue;
542
543 wxRect button_rect = m_rect;
544 button_rect.SetY(1);
545 button_rect.SetWidth(offset);
546
547 m_art->DrawButton(dc,
548 wnd,
549 button_rect,
550 button.id,
551 button.curState,
552 wxRIGHT,
553 &button.rect);
554
555 offset -= button.rect.GetWidth();
556 right_buttons_width += button.rect.GetWidth();
557 }
558
559
560
561 offset = 0;
562
563 // draw the buttons on the left side
564
565 for (i = 0; i < button_count; ++i)
566 {
567 wxAuiTabContainerButton& button = m_buttons.Item(button_count - i - 1);
568
569 if (button.location != wxLEFT)
570 continue;
571 if (button.curState & wxAUI_BUTTON_STATE_HIDDEN)
572 continue;
573
574 wxRect button_rect(offset, 1, 1000, m_rect.height);
575
576 m_art->DrawButton(dc,
577 wnd,
578 button_rect,
579 button.id,
580 button.curState,
581 wxLEFT,
582 &button.rect);
583
584 offset += button.rect.GetWidth();
585 left_buttons_width += button.rect.GetWidth();
586 }
587
588 offset = left_buttons_width;
589
590 if (offset == 0)
591 offset += m_art->GetIndentSize();
592
593
594 // prepare the tab-close-button array
595 // make sure tab button entries which aren't used are marked as hidden
596 for (i = page_count; i < m_tabCloseButtons.GetCount(); ++i)
597 m_tabCloseButtons.Item(i).curState = wxAUI_BUTTON_STATE_HIDDEN;
598
599 // make sure there are enough tab button entries to accommodate all tabs
600 while (m_tabCloseButtons.GetCount() < page_count)
601 {
602 wxAuiTabContainerButton tempbtn;
603 tempbtn.id = wxAUI_BUTTON_CLOSE;
604 tempbtn.location = wxCENTER;
605 tempbtn.curState = wxAUI_BUTTON_STATE_HIDDEN;
606 m_tabCloseButtons.Add(tempbtn);
607 }
608
609
610 // buttons before the tab offset must be set to hidden
611 for (i = 0; i < m_tabOffset; ++i)
612 {
613 m_tabCloseButtons.Item(i).curState = wxAUI_BUTTON_STATE_HIDDEN;
614 }
615
616
617 // draw the tabs
618
619 size_t active = 999;
620 int active_offset = 0;
621 wxRect active_rect;
622
623 int x_extent = 0;
624 wxRect rect = m_rect;
625 rect.y = 0;
626 rect.height = m_rect.height;
627
628 for (i = m_tabOffset; i < page_count; ++i)
629 {
630 wxAuiNotebookPage& page = m_pages.Item(i);
631 wxAuiTabContainerButton& tab_button = m_tabCloseButtons.Item(i);
632
633 // determine if a close button is on this tab
634 if ((m_flags & wxAUI_NB_CLOSE_ON_ALL_TABS) != 0 ||
635 ((m_flags & wxAUI_NB_CLOSE_ON_ACTIVE_TAB) != 0 && page.active))
636 {
637 if (tab_button.curState == wxAUI_BUTTON_STATE_HIDDEN)
638 {
639 tab_button.id = wxAUI_BUTTON_CLOSE;
640 tab_button.curState = wxAUI_BUTTON_STATE_NORMAL;
641 tab_button.location = wxCENTER;
642 }
643 }
644 else
645 {
646 tab_button.curState = wxAUI_BUTTON_STATE_HIDDEN;
647 }
648
649 rect.x = offset;
650 rect.width = m_rect.width - right_buttons_width - offset - 2;
651
652 if (rect.width <= 0)
653 break;
654
655 m_art->DrawTab(dc,
656 wnd,
657 page,
658 rect,
659 tab_button.curState,
660 &page.rect,
661 &tab_button.rect,
662 &x_extent);
663
664 if (page.active)
665 {
666 active = i;
667 active_offset = offset;
668 active_rect = rect;
669 }
670
671 offset += x_extent;
672 }
673
674
675 // make sure to deactivate buttons which are off the screen to the right
676 for (++i; i < m_tabCloseButtons.GetCount(); ++i)
677 {
678 m_tabCloseButtons.Item(i).curState = wxAUI_BUTTON_STATE_HIDDEN;
679 }
680
681
682 // draw the active tab again so it stands in the foreground
683 if (active >= m_tabOffset && active < m_pages.GetCount())
684 {
685 wxAuiNotebookPage& page = m_pages.Item(active);
686
687 wxAuiTabContainerButton& tab_button = m_tabCloseButtons.Item(active);
688
689 rect.x = active_offset;
690 m_art->DrawTab(dc,
691 wnd,
692 page,
693 active_rect,
694 tab_button.curState,
695 &page.rect,
696 &tab_button.rect,
697 &x_extent);
698 }
699
700
701 raw_dc->Blit(m_rect.x, m_rect.y,
702 m_rect.GetWidth(), m_rect.GetHeight(),
703 &dc, 0, 0);
704 }
705
706 // Is the tab visible?
707 bool wxAuiTabContainer::IsTabVisible(int tabPage, int tabOffset, wxDC* dc, wxWindow* wnd)
708 {
709 if (!dc || !dc->IsOk())
710 return false;
711
712 size_t i;
713 size_t page_count = m_pages.GetCount();
714 size_t button_count = m_buttons.GetCount();
715
716 // Hasn't been rendered yet; assume it's visible
717 if (m_tabCloseButtons.GetCount() < page_count)
718 return true;
719
720 // First check if both buttons are disabled - if so, there's no need to
721 // check further for visibility.
722 int arrowButtonVisibleCount = 0;
723 for (i = 0; i < button_count; ++i)
724 {
725 wxAuiTabContainerButton& button = m_buttons.Item(i);
726 if (button.id == wxAUI_BUTTON_LEFT ||
727 button.id == wxAUI_BUTTON_RIGHT)
728 {
729 if ((button.curState & wxAUI_BUTTON_STATE_HIDDEN) == 0)
730 arrowButtonVisibleCount ++;
731 }
732 }
733
734 // Tab must be visible
735 if (arrowButtonVisibleCount == 0)
736 return true;
737
738 // If tab is less than the given offset, it must be invisible by definition
739 if (tabPage < tabOffset)
740 return false;
741
742 // draw buttons
743 int left_buttons_width = 0;
744 int right_buttons_width = 0;
745
746 int offset = 0;
747
748 // calculate size of the buttons on the right side
749 offset = m_rect.x + m_rect.width;
750 for (i = 0; i < button_count; ++i)
751 {
752 wxAuiTabContainerButton& button = m_buttons.Item(button_count - i - 1);
753
754 if (button.location != wxRIGHT)
755 continue;
756 if (button.curState & wxAUI_BUTTON_STATE_HIDDEN)
757 continue;
758
759 offset -= button.rect.GetWidth();
760 right_buttons_width += button.rect.GetWidth();
761 }
762
763 offset = 0;
764
765 // calculate size of the buttons on the left side
766 for (i = 0; i < button_count; ++i)
767 {
768 wxAuiTabContainerButton& button = m_buttons.Item(button_count - i - 1);
769
770 if (button.location != wxLEFT)
771 continue;
772 if (button.curState & wxAUI_BUTTON_STATE_HIDDEN)
773 continue;
774
775 offset += button.rect.GetWidth();
776 left_buttons_width += button.rect.GetWidth();
777 }
778
779 offset = left_buttons_width;
780
781 if (offset == 0)
782 offset += m_art->GetIndentSize();
783
784 wxRect active_rect;
785
786 wxRect rect = m_rect;
787 rect.y = 0;
788 rect.height = m_rect.height;
789
790 // See if the given page is visible at the given tab offset (effectively scroll position)
791 for (i = tabOffset; i < page_count; ++i)
792 {
793 wxAuiNotebookPage& page = m_pages.Item(i);
794 wxAuiTabContainerButton& tab_button = m_tabCloseButtons.Item(i);
795
796 rect.x = offset;
797 rect.width = m_rect.width - right_buttons_width - offset - 2;
798
799 if (rect.width <= 0)
800 return false; // haven't found the tab, and we've run out of space, so return false
801
802 int x_extent = 0;
803 m_art->GetTabSize(*dc,
804 wnd,
805 page.caption,
806 page.bitmap,
807 page.active,
808 tab_button.curState,
809 &x_extent);
810
811 offset += x_extent;
812
813 if (i == (size_t) tabPage)
814 {
815 // If not all of the tab is visible, and supposing there's space to display it all,
816 // we could do better so we return false.
817 if (((m_rect.width - right_buttons_width - offset - 2) <= 0) && ((m_rect.width - right_buttons_width - left_buttons_width) > x_extent))
818 return false;
819 else
820 return true;
821 }
822 }
823
824 // Shouldn't really get here, but if it does, assume the tab is visible to prevent
825 // further looping in calling code.
826 return true;
827 }
828
829 // Make the tab visible if it wasn't already
830 void wxAuiTabContainer::MakeTabVisible(int tabPage, wxWindow* win)
831 {
832 wxClientDC dc(win);
833 if (!IsTabVisible(tabPage, GetTabOffset(), & dc, win))
834 {
835 int i;
836 for (i = 0; i < (int) m_pages.GetCount(); i++)
837 {
838 if (IsTabVisible(tabPage, i, & dc, win))
839 {
840 SetTabOffset(i);
841 win->Refresh();
842 return;
843 }
844 }
845 }
846 }
847
848 // TabHitTest() tests if a tab was hit, passing the window pointer
849 // back if that condition was fulfilled. The function returns
850 // true if a tab was hit, otherwise false
851 bool wxAuiTabContainer::TabHitTest(int x, int y, wxWindow** hit) const
852 {
853 if (!m_rect.Contains(x,y))
854 return false;
855
856 wxAuiTabContainerButton* btn = NULL;
857 if (ButtonHitTest(x, y, &btn) && !(btn->curState & wxAUI_BUTTON_STATE_DISABLED))
858 {
859 if (m_buttons.Index(*btn) != wxNOT_FOUND)
860 return false;
861 }
862
863 size_t i, page_count = m_pages.GetCount();
864
865 for (i = m_tabOffset; i < page_count; ++i)
866 {
867 wxAuiNotebookPage& page = m_pages.Item(i);
868 if (page.rect.Contains(x,y))
869 {
870 if (hit)
871 *hit = page.window;
872 return true;
873 }
874 }
875
876 return false;
877 }
878
879 // ButtonHitTest() tests if a button was hit. The function returns
880 // true if a button was hit, otherwise false
881 bool wxAuiTabContainer::ButtonHitTest(int x, int y,
882 wxAuiTabContainerButton** hit) const
883 {
884 if (!m_rect.Contains(x,y))
885 return false;
886
887 size_t i, button_count;
888
889
890 button_count = m_buttons.GetCount();
891 for (i = 0; i < button_count; ++i)
892 {
893 wxAuiTabContainerButton& button = m_buttons.Item(i);
894 if (button.rect.Contains(x,y) &&
895 !(button.curState & wxAUI_BUTTON_STATE_HIDDEN ))
896 {
897 if (hit)
898 *hit = &button;
899 return true;
900 }
901 }
902
903 button_count = m_tabCloseButtons.GetCount();
904 for (i = 0; i < button_count; ++i)
905 {
906 wxAuiTabContainerButton& button = m_tabCloseButtons.Item(i);
907 if (button.rect.Contains(x,y) &&
908 !(button.curState & (wxAUI_BUTTON_STATE_HIDDEN |
909 wxAUI_BUTTON_STATE_DISABLED)))
910 {
911 if (hit)
912 *hit = &button;
913 return true;
914 }
915 }
916
917 return false;
918 }
919
920
921
922 // the utility function ShowWnd() is the same as show,
923 // except it handles wxAuiMDIChildFrame windows as well,
924 // as the Show() method on this class is "unplugged"
925 static void ShowWnd(wxWindow* wnd, bool show)
926 {
927 #if wxUSE_MDI
928 if (wxDynamicCast(wnd, wxAuiMDIChildFrame))
929 {
930 wxAuiMDIChildFrame* cf = (wxAuiMDIChildFrame*)wnd;
931 cf->DoShow(show);
932 }
933 else
934 #endif
935 {
936 wnd->Show(show);
937 }
938 }
939
940
941 // DoShowHide() this function shows the active window, then
942 // hides all of the other windows (in that order)
943 void wxAuiTabContainer::DoShowHide()
944 {
945 wxAuiNotebookPageArray& pages = GetPages();
946 size_t i, page_count = pages.GetCount();
947
948 // show new active page first
949 for (i = 0; i < page_count; ++i)
950 {
951 wxAuiNotebookPage& page = pages.Item(i);
952 if (page.active)
953 {
954 ShowWnd(page.window, true);
955 break;
956 }
957 }
958
959 // hide all other pages
960 for (i = 0; i < page_count; ++i)
961 {
962 wxAuiNotebookPage& page = pages.Item(i);
963 if (!page.active)
964 ShowWnd(page.window, false);
965 }
966 }
967
968
969
970
971
972
973 // -- wxAuiTabCtrl class implementation --
974
975
976
977 BEGIN_EVENT_TABLE(wxAuiTabCtrl, wxControl)
978 EVT_PAINT(wxAuiTabCtrl::OnPaint)
979 EVT_ERASE_BACKGROUND(wxAuiTabCtrl::OnEraseBackground)
980 EVT_SIZE(wxAuiTabCtrl::OnSize)
981 EVT_LEFT_DOWN(wxAuiTabCtrl::OnLeftDown)
982 EVT_LEFT_DCLICK(wxAuiTabCtrl::OnLeftDClick)
983 EVT_LEFT_UP(wxAuiTabCtrl::OnLeftUp)
984 EVT_MIDDLE_DOWN(wxAuiTabCtrl::OnMiddleDown)
985 EVT_MIDDLE_UP(wxAuiTabCtrl::OnMiddleUp)
986 EVT_RIGHT_DOWN(wxAuiTabCtrl::OnRightDown)
987 EVT_RIGHT_UP(wxAuiTabCtrl::OnRightUp)
988 EVT_MOTION(wxAuiTabCtrl::OnMotion)
989 EVT_LEAVE_WINDOW(wxAuiTabCtrl::OnLeaveWindow)
990 EVT_AUINOTEBOOK_BUTTON(wxID_ANY, wxAuiTabCtrl::OnButton)
991 EVT_SET_FOCUS(wxAuiTabCtrl::OnSetFocus)
992 EVT_KILL_FOCUS(wxAuiTabCtrl::OnKillFocus)
993 EVT_CHAR(wxAuiTabCtrl::OnChar)
994 EVT_MOUSE_CAPTURE_LOST(wxAuiTabCtrl::OnCaptureLost)
995 END_EVENT_TABLE()
996
997
998 wxAuiTabCtrl::wxAuiTabCtrl(wxWindow* parent,
999 wxWindowID id,
1000 const wxPoint& pos,
1001 const wxSize& size,
1002 long style) : wxControl(parent, id, pos, size, style)
1003 {
1004 SetName(wxT("wxAuiTabCtrl"));
1005 m_clickPt = wxDefaultPosition;
1006 m_isDragging = false;
1007 m_hoverButton = NULL;
1008 m_pressedButton = NULL;
1009 }
1010
1011 wxAuiTabCtrl::~wxAuiTabCtrl()
1012 {
1013 }
1014
1015 void wxAuiTabCtrl::OnPaint(wxPaintEvent&)
1016 {
1017 wxPaintDC dc(this);
1018
1019 dc.SetFont(GetFont());
1020
1021 if (GetPageCount() > 0)
1022 Render(&dc, this);
1023 }
1024
1025 void wxAuiTabCtrl::OnEraseBackground(wxEraseEvent& WXUNUSED(evt))
1026 {
1027 }
1028
1029 void wxAuiTabCtrl::OnSize(wxSizeEvent& evt)
1030 {
1031 wxSize s = evt.GetSize();
1032 wxRect r(0, 0, s.GetWidth(), s.GetHeight());
1033 SetRect(r);
1034 }
1035
1036 void wxAuiTabCtrl::OnLeftDown(wxMouseEvent& evt)
1037 {
1038 CaptureMouse();
1039 m_clickPt = wxDefaultPosition;
1040 m_isDragging = false;
1041 m_clickTab = NULL;
1042 m_pressedButton = NULL;
1043
1044
1045 wxWindow* wnd;
1046 if (TabHitTest(evt.m_x, evt.m_y, &wnd))
1047 {
1048 int new_selection = GetIdxFromWindow(wnd);
1049
1050 // wxAuiNotebooks always want to receive this event
1051 // even if the tab is already active, because they may
1052 // have multiple tab controls
1053 if ((new_selection != GetActivePage() ||
1054 wxDynamicCast(GetParent(), wxAuiNotebook)) && !m_hoverButton)
1055 {
1056 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING, m_windowId);
1057 e.SetSelection(new_selection);
1058 e.SetOldSelection(GetActivePage());
1059 e.SetEventObject(this);
1060 GetEventHandler()->ProcessEvent(e);
1061 }
1062
1063 m_clickPt.x = evt.m_x;
1064 m_clickPt.y = evt.m_y;
1065 m_clickTab = wnd;
1066 }
1067
1068 if (m_hoverButton)
1069 {
1070 m_pressedButton = m_hoverButton;
1071 m_pressedButton->curState = wxAUI_BUTTON_STATE_PRESSED;
1072 Refresh();
1073 Update();
1074 }
1075 }
1076
1077 void wxAuiTabCtrl::OnCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
1078 {
1079 if (m_isDragging)
1080 {
1081 m_isDragging = false;
1082
1083 wxAuiNotebookEvent evt(wxEVT_COMMAND_AUINOTEBOOK_CANCEL_DRAG, m_windowId);
1084 evt.SetSelection(GetIdxFromWindow(m_clickTab));
1085 evt.SetOldSelection(evt.GetSelection());
1086 evt.SetEventObject(this);
1087 GetEventHandler()->ProcessEvent(evt);
1088 }
1089 }
1090
1091 void wxAuiTabCtrl::OnLeftUp(wxMouseEvent& evt)
1092 {
1093 if (GetCapture() == this)
1094 ReleaseMouse();
1095
1096 if (m_isDragging)
1097 {
1098 m_isDragging = false;
1099
1100 wxAuiNotebookEvent evt(wxEVT_COMMAND_AUINOTEBOOK_END_DRAG, m_windowId);
1101 evt.SetSelection(GetIdxFromWindow(m_clickTab));
1102 evt.SetOldSelection(evt.GetSelection());
1103 evt.SetEventObject(this);
1104 GetEventHandler()->ProcessEvent(evt);
1105
1106 return;
1107 }
1108
1109 if (m_pressedButton)
1110 {
1111 // make sure we're still clicking the button
1112 wxAuiTabContainerButton* button = NULL;
1113 if (!ButtonHitTest(evt.m_x, evt.m_y, &button) ||
1114 button->curState & wxAUI_BUTTON_STATE_DISABLED)
1115 return;
1116
1117 if (button != m_pressedButton)
1118 {
1119 m_pressedButton = NULL;
1120 return;
1121 }
1122
1123 Refresh();
1124 Update();
1125
1126 if (!(m_pressedButton->curState & wxAUI_BUTTON_STATE_DISABLED))
1127 {
1128 wxAuiNotebookEvent evt(wxEVT_COMMAND_AUINOTEBOOK_BUTTON, m_windowId);
1129 evt.SetSelection(GetIdxFromWindow(m_clickTab));
1130 evt.SetInt(m_pressedButton->id);
1131 evt.SetEventObject(this);
1132 GetEventHandler()->ProcessEvent(evt);
1133 }
1134
1135 m_pressedButton = NULL;
1136 }
1137
1138 m_clickPt = wxDefaultPosition;
1139 m_isDragging = false;
1140 m_clickTab = NULL;
1141 }
1142
1143 void wxAuiTabCtrl::OnMiddleUp(wxMouseEvent& evt)
1144 {
1145 wxWindow* wnd = NULL;
1146 if (!TabHitTest(evt.m_x, evt.m_y, &wnd))
1147 return;
1148
1149 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_TAB_MIDDLE_UP, m_windowId);
1150 e.SetEventObject(this);
1151 e.SetSelection(GetIdxFromWindow(wnd));
1152 GetEventHandler()->ProcessEvent(e);
1153 }
1154
1155 void wxAuiTabCtrl::OnMiddleDown(wxMouseEvent& evt)
1156 {
1157 wxWindow* wnd = NULL;
1158 if (!TabHitTest(evt.m_x, evt.m_y, &wnd))
1159 return;
1160
1161 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_TAB_MIDDLE_DOWN, m_windowId);
1162 e.SetEventObject(this);
1163 e.SetSelection(GetIdxFromWindow(wnd));
1164 GetEventHandler()->ProcessEvent(e);
1165 }
1166
1167 void wxAuiTabCtrl::OnRightUp(wxMouseEvent& evt)
1168 {
1169 wxWindow* wnd = NULL;
1170 if (!TabHitTest(evt.m_x, evt.m_y, &wnd))
1171 return;
1172
1173 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_TAB_RIGHT_UP, m_windowId);
1174 e.SetEventObject(this);
1175 e.SetSelection(GetIdxFromWindow(wnd));
1176 GetEventHandler()->ProcessEvent(e);
1177 }
1178
1179 void wxAuiTabCtrl::OnRightDown(wxMouseEvent& evt)
1180 {
1181 wxWindow* wnd = NULL;
1182 if (!TabHitTest(evt.m_x, evt.m_y, &wnd))
1183 return;
1184
1185 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_TAB_RIGHT_DOWN, m_windowId);
1186 e.SetEventObject(this);
1187 e.SetSelection(GetIdxFromWindow(wnd));
1188 GetEventHandler()->ProcessEvent(e);
1189 }
1190
1191 void wxAuiTabCtrl::OnLeftDClick(wxMouseEvent& evt)
1192 {
1193 wxWindow* wnd;
1194 wxAuiTabContainerButton* button;
1195 if (!TabHitTest(evt.m_x, evt.m_y, &wnd) && !ButtonHitTest(evt.m_x, evt.m_y, &button))
1196 {
1197 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_BG_DCLICK, m_windowId);
1198 e.SetEventObject(this);
1199 GetEventHandler()->ProcessEvent(e);
1200 }
1201 }
1202
1203 void wxAuiTabCtrl::OnMotion(wxMouseEvent& evt)
1204 {
1205 wxPoint pos = evt.GetPosition();
1206
1207 // check if the mouse is hovering above a button
1208 wxAuiTabContainerButton* button;
1209 if (ButtonHitTest(pos.x, pos.y, &button) && !(button->curState & wxAUI_BUTTON_STATE_DISABLED))
1210 {
1211 if (m_hoverButton && button != m_hoverButton)
1212 {
1213 m_hoverButton->curState = wxAUI_BUTTON_STATE_NORMAL;
1214 m_hoverButton = NULL;
1215 Refresh();
1216 Update();
1217 }
1218
1219 if (button->curState != wxAUI_BUTTON_STATE_HOVER)
1220 {
1221 button->curState = wxAUI_BUTTON_STATE_HOVER;
1222 Refresh();
1223 Update();
1224
1225 m_hoverButton = button;
1226 return;
1227 }
1228 }
1229 else
1230 {
1231 if (m_hoverButton)
1232 {
1233 m_hoverButton->curState = wxAUI_BUTTON_STATE_NORMAL;
1234 m_hoverButton = NULL;
1235 Refresh();
1236 Update();
1237 }
1238 }
1239
1240 wxWindow* wnd = NULL;
1241
1242 #if wxUSE_TOOLTIPS
1243 if (evt.Moving() && TabHitTest(evt.m_x, evt.m_y, &wnd))
1244 {
1245 wxString tooltip(m_pages[GetIdxFromWindow(wnd)].tooltip);
1246
1247 // If the text changes, set it else, keep old, to avoid
1248 // 'moving tooltip' effect
1249 if (GetToolTipText() != tooltip)
1250 SetToolTip(tooltip);
1251 }
1252 else
1253 UnsetToolTip();
1254 #endif
1255
1256 if (!evt.LeftIsDown() || m_clickPt == wxDefaultPosition)
1257 return;
1258
1259 if (m_isDragging)
1260 {
1261 wxAuiNotebookEvent evt(wxEVT_COMMAND_AUINOTEBOOK_DRAG_MOTION, m_windowId);
1262 evt.SetSelection(GetIdxFromWindow(m_clickTab));
1263 evt.SetOldSelection(evt.GetSelection());
1264 evt.SetEventObject(this);
1265 GetEventHandler()->ProcessEvent(evt);
1266 return;
1267 }
1268
1269
1270 int drag_x_threshold = wxSystemSettings::GetMetric(wxSYS_DRAG_X);
1271 int drag_y_threshold = wxSystemSettings::GetMetric(wxSYS_DRAG_Y);
1272
1273 if (abs(pos.x - m_clickPt.x) > drag_x_threshold ||
1274 abs(pos.y - m_clickPt.y) > drag_y_threshold)
1275 {
1276 wxAuiNotebookEvent evt(wxEVT_COMMAND_AUINOTEBOOK_BEGIN_DRAG, m_windowId);
1277 evt.SetSelection(GetIdxFromWindow(m_clickTab));
1278 evt.SetOldSelection(evt.GetSelection());
1279 evt.SetEventObject(this);
1280 GetEventHandler()->ProcessEvent(evt);
1281
1282 m_isDragging = true;
1283 }
1284 }
1285
1286 void wxAuiTabCtrl::OnLeaveWindow(wxMouseEvent& WXUNUSED(event))
1287 {
1288 if (m_hoverButton)
1289 {
1290 m_hoverButton->curState = wxAUI_BUTTON_STATE_NORMAL;
1291 m_hoverButton = NULL;
1292 Refresh();
1293 Update();
1294 }
1295 }
1296
1297 void wxAuiTabCtrl::OnButton(wxAuiNotebookEvent& event)
1298 {
1299 int button = event.GetInt();
1300
1301 if (button == wxAUI_BUTTON_LEFT || button == wxAUI_BUTTON_RIGHT)
1302 {
1303 if (button == wxAUI_BUTTON_LEFT)
1304 {
1305 if (GetTabOffset() > 0)
1306 {
1307 SetTabOffset(GetTabOffset()-1);
1308 Refresh();
1309 Update();
1310 }
1311 }
1312 else
1313 {
1314 SetTabOffset(GetTabOffset()+1);
1315 Refresh();
1316 Update();
1317 }
1318 }
1319 else if (button == wxAUI_BUTTON_WINDOWLIST)
1320 {
1321 int idx = GetArtProvider()->ShowDropDown(this, m_pages, GetActivePage());
1322
1323 if (idx != -1)
1324 {
1325 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING, m_windowId);
1326 e.SetSelection(idx);
1327 e.SetOldSelection(GetActivePage());
1328 e.SetEventObject(this);
1329 GetEventHandler()->ProcessEvent(e);
1330 }
1331 }
1332 else
1333 {
1334 event.Skip();
1335 }
1336 }
1337
1338 void wxAuiTabCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event))
1339 {
1340 Refresh();
1341 }
1342
1343 void wxAuiTabCtrl::OnKillFocus(wxFocusEvent& WXUNUSED(event))
1344 {
1345 Refresh();
1346 }
1347
1348 void wxAuiTabCtrl::OnChar(wxKeyEvent& event)
1349 {
1350 if (GetActivePage() == -1)
1351 {
1352 event.Skip();
1353 return;
1354 }
1355
1356 // We can't leave tab processing to the system; on Windows, tabs and keys
1357 // get eaten by the system and not processed properly if we specify both
1358 // wxTAB_TRAVERSAL and wxWANTS_CHARS. And if we specify just wxTAB_TRAVERSAL,
1359 // we don't key arrow key events.
1360
1361 int key = event.GetKeyCode();
1362
1363 if (key == WXK_NUMPAD_PAGEUP)
1364 key = WXK_PAGEUP;
1365 if (key == WXK_NUMPAD_PAGEDOWN)
1366 key = WXK_PAGEDOWN;
1367 if (key == WXK_NUMPAD_HOME)
1368 key = WXK_HOME;
1369 if (key == WXK_NUMPAD_END)
1370 key = WXK_END;
1371 if (key == WXK_NUMPAD_LEFT)
1372 key = WXK_LEFT;
1373 if (key == WXK_NUMPAD_RIGHT)
1374 key = WXK_RIGHT;
1375
1376 if (key == WXK_TAB || key == WXK_PAGEUP || key == WXK_PAGEDOWN)
1377 {
1378 bool bCtrlDown = event.ControlDown();
1379 bool bShiftDown = event.ShiftDown();
1380
1381 bool bForward = (key == WXK_TAB && !bShiftDown) || (key == WXK_PAGEDOWN);
1382 bool bWindowChange = (key == WXK_PAGEUP) || (key == WXK_PAGEDOWN) || bCtrlDown;
1383 bool bFromTab = (key == WXK_TAB);
1384
1385 wxAuiNotebook* nb = wxDynamicCast(GetParent(), wxAuiNotebook);
1386 if (!nb)
1387 {
1388 event.Skip();
1389 return;
1390 }
1391
1392 wxNavigationKeyEvent keyEvent;
1393 keyEvent.SetDirection(bForward);
1394 keyEvent.SetWindowChange(bWindowChange);
1395 keyEvent.SetFromTab(bFromTab);
1396 keyEvent.SetEventObject(nb);
1397
1398 if (!nb->GetEventHandler()->ProcessEvent(keyEvent))
1399 {
1400 // Not processed? Do an explicit tab into the page.
1401 wxWindow* win = GetWindowFromIdx(GetActivePage());
1402 if (win)
1403 win->SetFocus();
1404 }
1405 return;
1406 }
1407
1408 if (m_pages.GetCount() < 2)
1409 {
1410 event.Skip();
1411 return;
1412 }
1413
1414 int newPage = -1;
1415
1416 int forwardKey, backwardKey;
1417 if (GetLayoutDirection() == wxLayout_RightToLeft)
1418 {
1419 forwardKey = WXK_LEFT;
1420 backwardKey = WXK_RIGHT;
1421 }
1422 else
1423 {
1424 forwardKey = WXK_RIGHT;
1425 backwardKey = WXK_LEFT;
1426 }
1427
1428 if (key == forwardKey)
1429 {
1430 if (m_pages.GetCount() > 1)
1431 {
1432 if (GetActivePage() == -1)
1433 newPage = 0;
1434 else if (GetActivePage() < (int) (m_pages.GetCount() - 1))
1435 newPage = GetActivePage() + 1;
1436 }
1437 }
1438 else if (key == backwardKey)
1439 {
1440 if (m_pages.GetCount() > 1)
1441 {
1442 if (GetActivePage() == -1)
1443 newPage = (int) (m_pages.GetCount() - 1);
1444 else if (GetActivePage() > 0)
1445 newPage = GetActivePage() - 1;
1446 }
1447 }
1448 else if (key == WXK_HOME)
1449 {
1450 newPage = 0;
1451 }
1452 else if (key == WXK_END)
1453 {
1454 newPage = (int) (m_pages.GetCount() - 1);
1455 }
1456 else
1457 event.Skip();
1458
1459 if (newPage != -1)
1460 {
1461 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING, m_windowId);
1462 e.SetSelection(newPage);
1463 e.SetOldSelection(newPage);
1464 e.SetEventObject(this);
1465 this->GetEventHandler()->ProcessEvent(e);
1466 }
1467 else
1468 event.Skip();
1469 }
1470
1471 // wxTabFrame is an interesting case. It's important that all child pages
1472 // of the multi-notebook control are all actually children of that control
1473 // (and not grandchildren). wxTabFrame facilitates this. There is one
1474 // instance of wxTabFrame for each tab control inside the multi-notebook.
1475 // It's important to know that wxTabFrame is not a real window, but it merely
1476 // used to capture the dimensions/positioning of the internal tab control and
1477 // it's managed page windows
1478
1479 class wxTabFrame : public wxWindow
1480 {
1481 public:
1482
1483 wxTabFrame()
1484 {
1485 m_tabs = NULL;
1486 m_rect = wxRect(0,0,200,200);
1487 m_tabCtrlHeight = 20;
1488 }
1489
1490 ~wxTabFrame()
1491 {
1492 wxDELETE(m_tabs);
1493 }
1494
1495 void SetTabCtrlHeight(int h)
1496 {
1497 m_tabCtrlHeight = h;
1498 }
1499
1500 protected:
1501 void DoSetSize(int x, int y,
1502 int width, int height,
1503 int WXUNUSED(sizeFlags = wxSIZE_AUTO))
1504 {
1505 m_rect = wxRect(x, y, width, height);
1506 DoSizing();
1507 }
1508
1509 void DoGetClientSize(int* x, int* y) const
1510 {
1511 *x = m_rect.width;
1512 *y = m_rect.height;
1513 }
1514
1515 public:
1516 bool Show( bool WXUNUSED(show = true) ) { return false; }
1517
1518 void DoSizing()
1519 {
1520 if (!m_tabs)
1521 return;
1522
1523 if (m_tabs->IsFrozen() || m_tabs->GetParent()->IsFrozen())
1524 return;
1525
1526 m_tab_rect = wxRect(m_rect.x, m_rect.y, m_rect.width, m_tabCtrlHeight);
1527 if (m_tabs->GetFlags() & wxAUI_NB_BOTTOM)
1528 {
1529 m_tab_rect = wxRect (m_rect.x, m_rect.y + m_rect.height - m_tabCtrlHeight, m_rect.width, m_tabCtrlHeight);
1530 m_tabs->SetSize (m_rect.x, m_rect.y + m_rect.height - m_tabCtrlHeight, m_rect.width, m_tabCtrlHeight);
1531 m_tabs->SetRect (wxRect(0, 0, m_rect.width, m_tabCtrlHeight));
1532 }
1533 else //TODO: if (GetFlags() & wxAUI_NB_TOP)
1534 {
1535 m_tab_rect = wxRect (m_rect.x, m_rect.y, m_rect.width, m_tabCtrlHeight);
1536 m_tabs->SetSize (m_rect.x, m_rect.y, m_rect.width, m_tabCtrlHeight);
1537 m_tabs->SetRect (wxRect(0, 0, m_rect.width, m_tabCtrlHeight));
1538 }
1539 // TODO: else if (GetFlags() & wxAUI_NB_LEFT){}
1540 // TODO: else if (GetFlags() & wxAUI_NB_RIGHT){}
1541
1542 m_tabs->Refresh();
1543 m_tabs->Update();
1544
1545 wxAuiNotebookPageArray& pages = m_tabs->GetPages();
1546 size_t i, page_count = pages.GetCount();
1547
1548 for (i = 0; i < page_count; ++i)
1549 {
1550 int height = m_rect.height - m_tabCtrlHeight;
1551 if ( height < 0 )
1552 {
1553 // avoid passing negative height to wxWindow::SetSize(), this
1554 // results in assert failures/GTK+ warnings
1555 height = 0;
1556 }
1557
1558 wxAuiNotebookPage& page = pages.Item(i);
1559 if (m_tabs->GetFlags() & wxAUI_NB_BOTTOM)
1560 {
1561 page.window->SetSize(m_rect.x, m_rect.y, m_rect.width, height);
1562 }
1563 else //TODO: if (GetFlags() & wxAUI_NB_TOP)
1564 {
1565 page.window->SetSize(m_rect.x, m_rect.y + m_tabCtrlHeight,
1566 m_rect.width, height);
1567 }
1568 // TODO: else if (GetFlags() & wxAUI_NB_LEFT){}
1569 // TODO: else if (GetFlags() & wxAUI_NB_RIGHT){}
1570
1571 #if wxUSE_MDI
1572 if (wxDynamicCast(page.window, wxAuiMDIChildFrame))
1573 {
1574 wxAuiMDIChildFrame* wnd = (wxAuiMDIChildFrame*)page.window;
1575 wnd->ApplyMDIChildFrameRect();
1576 }
1577 #endif
1578 }
1579 }
1580
1581 protected:
1582 void DoGetSize(int* x, int* y) const
1583 {
1584 if (x)
1585 *x = m_rect.GetWidth();
1586 if (y)
1587 *y = m_rect.GetHeight();
1588 }
1589
1590 public:
1591 void Update()
1592 {
1593 // does nothing
1594 }
1595
1596 wxRect m_rect;
1597 wxRect m_tab_rect;
1598 wxAuiTabCtrl* m_tabs;
1599 int m_tabCtrlHeight;
1600 };
1601
1602
1603 const int wxAuiBaseTabCtrlId = 5380;
1604
1605
1606 // -- wxAuiNotebook class implementation --
1607
1608 #define EVT_AUI_RANGE(id1, id2, event, func) \
1609 wx__DECLARE_EVT2(event, id1, id2, wxAuiNotebookEventHandler(func))
1610
1611 BEGIN_EVENT_TABLE(wxAuiNotebook, wxControl)
1612 EVT_SIZE(wxAuiNotebook::OnSize)
1613 EVT_CHILD_FOCUS(wxAuiNotebook::OnChildFocusNotebook)
1614 EVT_AUI_RANGE(wxAuiBaseTabCtrlId, wxAuiBaseTabCtrlId+500,
1615 wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING,
1616 wxAuiNotebook::OnTabClicked)
1617 EVT_AUI_RANGE(wxAuiBaseTabCtrlId, wxAuiBaseTabCtrlId+500,
1618 wxEVT_COMMAND_AUINOTEBOOK_BEGIN_DRAG,
1619 wxAuiNotebook::OnTabBeginDrag)
1620 EVT_AUI_RANGE(wxAuiBaseTabCtrlId, wxAuiBaseTabCtrlId+500,
1621 wxEVT_COMMAND_AUINOTEBOOK_END_DRAG,
1622 wxAuiNotebook::OnTabEndDrag)
1623 EVT_AUI_RANGE(wxAuiBaseTabCtrlId, wxAuiBaseTabCtrlId+500,
1624 wxEVT_COMMAND_AUINOTEBOOK_CANCEL_DRAG,
1625 wxAuiNotebook::OnTabCancelDrag)
1626 EVT_AUI_RANGE(wxAuiBaseTabCtrlId, wxAuiBaseTabCtrlId+500,
1627 wxEVT_COMMAND_AUINOTEBOOK_DRAG_MOTION,
1628 wxAuiNotebook::OnTabDragMotion)
1629 EVT_AUI_RANGE(wxAuiBaseTabCtrlId, wxAuiBaseTabCtrlId+500,
1630 wxEVT_COMMAND_AUINOTEBOOK_BUTTON,
1631 wxAuiNotebook::OnTabButton)
1632 EVT_AUI_RANGE(wxAuiBaseTabCtrlId, wxAuiBaseTabCtrlId+500,
1633 wxEVT_COMMAND_AUINOTEBOOK_TAB_MIDDLE_DOWN,
1634 wxAuiNotebook::OnTabMiddleDown)
1635 EVT_AUI_RANGE(wxAuiBaseTabCtrlId, wxAuiBaseTabCtrlId+500,
1636 wxEVT_COMMAND_AUINOTEBOOK_TAB_MIDDLE_UP,
1637 wxAuiNotebook::OnTabMiddleUp)
1638 EVT_AUI_RANGE(wxAuiBaseTabCtrlId, wxAuiBaseTabCtrlId+500,
1639 wxEVT_COMMAND_AUINOTEBOOK_TAB_RIGHT_DOWN,
1640 wxAuiNotebook::OnTabRightDown)
1641 EVT_AUI_RANGE(wxAuiBaseTabCtrlId, wxAuiBaseTabCtrlId+500,
1642 wxEVT_COMMAND_AUINOTEBOOK_TAB_RIGHT_UP,
1643 wxAuiNotebook::OnTabRightUp)
1644 EVT_AUI_RANGE(wxAuiBaseTabCtrlId, wxAuiBaseTabCtrlId+500,
1645 wxEVT_COMMAND_AUINOTEBOOK_BG_DCLICK,
1646 wxAuiNotebook::OnTabBgDClick)
1647 EVT_NAVIGATION_KEY(wxAuiNotebook::OnNavigationKeyNotebook)
1648 END_EVENT_TABLE()
1649
1650 void wxAuiNotebook::Init()
1651 {
1652 m_curPage = -1;
1653 m_tabIdCounter = wxAuiBaseTabCtrlId;
1654 m_dummyWnd = NULL;
1655 m_tabCtrlHeight = 20;
1656 m_requestedBmpSize = wxDefaultSize;
1657 m_requestedTabCtrlHeight = -1;
1658 }
1659
1660 bool wxAuiNotebook::Create(wxWindow* parent,
1661 wxWindowID id,
1662 const wxPoint& pos,
1663 const wxSize& size,
1664 long style)
1665 {
1666 if (!wxControl::Create(parent, id, pos, size, style))
1667 return false;
1668
1669 InitNotebook(style);
1670
1671 return true;
1672 }
1673
1674 // InitNotebook() contains common initialization
1675 // code called by all constructors
1676 void wxAuiNotebook::InitNotebook(long style)
1677 {
1678 SetName(wxT("wxAuiNotebook"));
1679 m_curPage = -1;
1680 m_tabIdCounter = wxAuiBaseTabCtrlId;
1681 m_dummyWnd = NULL;
1682 m_flags = (unsigned int)style;
1683 m_tabCtrlHeight = 20;
1684
1685 m_normalFont = *wxNORMAL_FONT;
1686 m_selectedFont = *wxNORMAL_FONT;
1687 m_selectedFont.SetWeight(wxBOLD);
1688
1689 SetArtProvider(new wxAuiDefaultTabArt);
1690
1691 m_dummyWnd = new wxWindow(this, wxID_ANY, wxPoint(0,0), wxSize(0,0));
1692 m_dummyWnd->SetSize(200, 200);
1693 m_dummyWnd->Show(false);
1694
1695 m_mgr.SetManagedWindow(this);
1696 m_mgr.SetFlags(wxAUI_MGR_DEFAULT);
1697 m_mgr.SetDockSizeConstraint(1.0, 1.0); // no dock size constraint
1698
1699 m_mgr.AddPane(m_dummyWnd,
1700 wxAuiPaneInfo().Name(wxT("dummy")).Bottom().CaptionVisible(false).Show(false));
1701
1702 m_mgr.Update();
1703 }
1704
1705 wxAuiNotebook::~wxAuiNotebook()
1706 {
1707 // Indicate we're deleting pages
1708 SendDestroyEvent();
1709
1710 while ( GetPageCount() > 0 )
1711 DeletePage(0);
1712
1713 m_mgr.UnInit();
1714 }
1715
1716 void wxAuiNotebook::SetArtProvider(wxAuiTabArt* art)
1717 {
1718 m_tabs.SetArtProvider(art);
1719
1720 // Update the height and do nothing else if it did something but otherwise
1721 // (i.e. if the new art provider uses the same height as the old one) we
1722 // need to manually set the art provider for all tabs ourselves.
1723 if ( !UpdateTabCtrlHeight() )
1724 {
1725 wxAuiPaneInfoArray& all_panes = m_mgr.GetAllPanes();
1726 const size_t pane_count = all_panes.GetCount();
1727 for (size_t i = 0; i < pane_count; ++i)
1728 {
1729 wxAuiPaneInfo& pane = all_panes.Item(i);
1730 if (pane.name == wxT("dummy"))
1731 continue;
1732 wxTabFrame* tab_frame = (wxTabFrame*)pane.window;
1733 wxAuiTabCtrl* tabctrl = tab_frame->m_tabs;
1734 tabctrl->SetArtProvider(art->Clone());
1735 }
1736 }
1737 }
1738
1739 // SetTabCtrlHeight() is the highest-level override of the
1740 // tab height. A call to this function effectively enforces a
1741 // specified tab ctrl height, overriding all other considerations,
1742 // such as text or bitmap height. It overrides any call to
1743 // SetUniformBitmapSize(). Specifying a height of -1 reverts
1744 // any previous call and returns to the default behaviour
1745
1746 void wxAuiNotebook::SetTabCtrlHeight(int height)
1747 {
1748 m_requestedTabCtrlHeight = height;
1749
1750 // if window is already initialized, recalculate the tab height
1751 if (m_dummyWnd)
1752 {
1753 UpdateTabCtrlHeight();
1754 }
1755 }
1756
1757
1758 // SetUniformBitmapSize() ensures that all tabs will have
1759 // the same height, even if some tabs don't have bitmaps
1760 // Passing wxDefaultSize to this function will instruct
1761 // the control to use dynamic tab height-- so when a tab
1762 // with a large bitmap is added, the tab ctrl's height will
1763 // automatically increase to accommodate the bitmap
1764
1765 void wxAuiNotebook::SetUniformBitmapSize(const wxSize& size)
1766 {
1767 m_requestedBmpSize = size;
1768
1769 // if window is already initialized, recalculate the tab height
1770 if (m_dummyWnd)
1771 {
1772 UpdateTabCtrlHeight();
1773 }
1774 }
1775
1776 // UpdateTabCtrlHeight() does the actual tab resizing. It's meant
1777 // to be used internally
1778 bool wxAuiNotebook::UpdateTabCtrlHeight()
1779 {
1780 // get the tab ctrl height we will use
1781 int height = CalculateTabCtrlHeight();
1782
1783 // if the tab control height needs to change, update
1784 // all of our tab controls with the new height
1785 if (m_tabCtrlHeight == height)
1786 return false;
1787
1788 wxAuiTabArt* art = m_tabs.GetArtProvider();
1789
1790 m_tabCtrlHeight = height;
1791
1792 wxAuiPaneInfoArray& all_panes = m_mgr.GetAllPanes();
1793 size_t i, pane_count = all_panes.GetCount();
1794 for (i = 0; i < pane_count; ++i)
1795 {
1796 wxAuiPaneInfo& pane = all_panes.Item(i);
1797 if (pane.name == wxT("dummy"))
1798 continue;
1799 wxTabFrame* tab_frame = (wxTabFrame*)pane.window;
1800 wxAuiTabCtrl* tabctrl = tab_frame->m_tabs;
1801 tab_frame->SetTabCtrlHeight(m_tabCtrlHeight);
1802 tabctrl->SetArtProvider(art->Clone());
1803 tab_frame->DoSizing();
1804 }
1805
1806 return true;
1807 }
1808
1809 void wxAuiNotebook::UpdateHintWindowSize()
1810 {
1811 wxSize size = CalculateNewSplitSize();
1812
1813 // the placeholder hint window should be set to this size
1814 wxAuiPaneInfo& info = m_mgr.GetPane(wxT("dummy"));
1815 if (info.IsOk())
1816 {
1817 info.MinSize(size);
1818 info.BestSize(size);
1819 m_dummyWnd->SetSize(size);
1820 }
1821 }
1822
1823
1824 // calculates the size of the new split
1825 wxSize wxAuiNotebook::CalculateNewSplitSize()
1826 {
1827 // count number of tab controls
1828 int tab_ctrl_count = 0;
1829 wxAuiPaneInfoArray& all_panes = m_mgr.GetAllPanes();
1830 size_t i, pane_count = all_panes.GetCount();
1831 for (i = 0; i < pane_count; ++i)
1832 {
1833 wxAuiPaneInfo& pane = all_panes.Item(i);
1834 if (pane.name == wxT("dummy"))
1835 continue;
1836 tab_ctrl_count++;
1837 }
1838
1839 wxSize new_split_size;
1840
1841 // if there is only one tab control, the first split
1842 // should happen around the middle
1843 if (tab_ctrl_count < 2)
1844 {
1845 new_split_size = GetClientSize();
1846 new_split_size.x /= 2;
1847 new_split_size.y /= 2;
1848 }
1849 else
1850 {
1851 // this is in place of a more complicated calculation
1852 // that needs to be implemented
1853 new_split_size = wxSize(180,180);
1854 }
1855
1856 return new_split_size;
1857 }
1858
1859 int wxAuiNotebook::CalculateTabCtrlHeight()
1860 {
1861 // if a fixed tab ctrl height is specified,
1862 // just return that instead of calculating a
1863 // tab height
1864 if (m_requestedTabCtrlHeight != -1)
1865 return m_requestedTabCtrlHeight;
1866
1867 // find out new best tab height
1868 wxAuiTabArt* art = m_tabs.GetArtProvider();
1869
1870 return art->GetBestTabCtrlSize(this,
1871 m_tabs.GetPages(),
1872 m_requestedBmpSize);
1873 }
1874
1875
1876 wxAuiTabArt* wxAuiNotebook::GetArtProvider() const
1877 {
1878 return m_tabs.GetArtProvider();
1879 }
1880
1881 void wxAuiNotebook::SetWindowStyleFlag(long style)
1882 {
1883 wxControl::SetWindowStyleFlag(style);
1884
1885 m_flags = (unsigned int)style;
1886
1887 // if the control is already initialized
1888 if (m_mgr.GetManagedWindow() == (wxWindow*)this)
1889 {
1890 // let all of the tab children know about the new style
1891
1892 wxAuiPaneInfoArray& all_panes = m_mgr.GetAllPanes();
1893 size_t i, pane_count = all_panes.GetCount();
1894 for (i = 0; i < pane_count; ++i)
1895 {
1896 wxAuiPaneInfo& pane = all_panes.Item(i);
1897 if (pane.name == wxT("dummy"))
1898 continue;
1899 wxTabFrame* tabframe = (wxTabFrame*)pane.window;
1900 wxAuiTabCtrl* tabctrl = tabframe->m_tabs;
1901 tabctrl->SetFlags(m_flags);
1902 tabframe->DoSizing();
1903 tabctrl->Refresh();
1904 tabctrl->Update();
1905 }
1906 }
1907 }
1908
1909
1910 bool wxAuiNotebook::AddPage(wxWindow* page,
1911 const wxString& caption,
1912 bool select,
1913 const wxBitmap& bitmap)
1914 {
1915 return InsertPage(GetPageCount(), page, caption, select, bitmap);
1916 }
1917
1918 bool wxAuiNotebook::InsertPage(size_t page_idx,
1919 wxWindow* page,
1920 const wxString& caption,
1921 bool select,
1922 const wxBitmap& bitmap)
1923 {
1924 wxASSERT_MSG(page, wxT("page pointer must be non-NULL"));
1925 if (!page)
1926 return false;
1927
1928 page->Reparent(this);
1929
1930 wxAuiNotebookPage info;
1931 info.window = page;
1932 info.caption = caption;
1933 info.bitmap = bitmap;
1934 info.active = false;
1935
1936 // if there are currently no tabs, the first added
1937 // tab must be active
1938 if (m_tabs.GetPageCount() == 0)
1939 info.active = true;
1940
1941 m_tabs.InsertPage(page, info, page_idx);
1942
1943 // if that was the first page added, even if
1944 // select is false, it must become the "current page"
1945 // (though no select events will be fired)
1946 if (!select && m_tabs.GetPageCount() == 1)
1947 select = true;
1948 //m_curPage = GetPageIndex(page);
1949
1950 wxAuiTabCtrl* active_tabctrl = GetActiveTabCtrl();
1951 if (page_idx >= active_tabctrl->GetPageCount())
1952 active_tabctrl->AddPage(page, info);
1953 else
1954 active_tabctrl->InsertPage(page, info, page_idx);
1955
1956 UpdateTabCtrlHeight();
1957 DoSizing();
1958 active_tabctrl->DoShowHide();
1959
1960 // adjust selected index
1961 if(m_curPage >= (int) page_idx)
1962 m_curPage++;
1963
1964 if (select)
1965 {
1966 SetSelectionToWindow(page);
1967 }
1968
1969 return true;
1970 }
1971
1972
1973 // DeletePage() removes a tab from the multi-notebook,
1974 // and destroys the window as well
1975 bool wxAuiNotebook::DeletePage(size_t page_idx)
1976 {
1977 if (page_idx >= m_tabs.GetPageCount())
1978 return false;
1979
1980 wxWindow* wnd = m_tabs.GetWindowFromIdx(page_idx);
1981
1982 // hide the window in advance, as this will
1983 // prevent flicker
1984 ShowWnd(wnd, false);
1985
1986 if (!RemovePage(page_idx))
1987 return false;
1988
1989 #if wxUSE_MDI
1990 // actually destroy the window now
1991 if (wxDynamicCast(wnd, wxAuiMDIChildFrame))
1992 {
1993 // delete the child frame with pending delete, as is
1994 // customary with frame windows
1995 if (!wxPendingDelete.Member(wnd))
1996 wxPendingDelete.Append(wnd);
1997 }
1998 else
1999 #endif
2000 {
2001 wnd->Destroy();
2002 }
2003
2004 return true;
2005 }
2006
2007
2008
2009 // RemovePage() removes a tab from the multi-notebook,
2010 // but does not destroy the window
2011 bool wxAuiNotebook::RemovePage(size_t page_idx)
2012 {
2013 // save active window pointer
2014 wxWindow* active_wnd = NULL;
2015 if (m_curPage >= 0)
2016 active_wnd = m_tabs.GetWindowFromIdx(m_curPage);
2017
2018 // save pointer of window being deleted
2019 wxWindow* wnd = m_tabs.GetWindowFromIdx(page_idx);
2020 wxWindow* new_active = NULL;
2021
2022 // make sure we found the page
2023 if (!wnd)
2024 return false;
2025
2026 // find out which onscreen tab ctrl owns this tab
2027 wxAuiTabCtrl* ctrl;
2028 int ctrl_idx;
2029 if (!FindTab(wnd, &ctrl, &ctrl_idx))
2030 return false;
2031
2032 bool is_curpage = (m_curPage == (int)page_idx);
2033 bool is_active_in_split = ctrl->GetPage(ctrl_idx).active;
2034
2035
2036 // remove the tab from main catalog
2037 if (!m_tabs.RemovePage(wnd))
2038 return false;
2039
2040 // remove the tab from the onscreen tab ctrl
2041 ctrl->RemovePage(wnd);
2042
2043 if (is_active_in_split)
2044 {
2045 int ctrl_new_page_count = (int)ctrl->GetPageCount();
2046
2047 if (ctrl_idx >= ctrl_new_page_count)
2048 ctrl_idx = ctrl_new_page_count-1;
2049
2050 if (ctrl_idx >= 0 && ctrl_idx < (int)ctrl->GetPageCount())
2051 {
2052 // set new page as active in the tab split
2053 ctrl->SetActivePage(ctrl_idx);
2054
2055 // if the page deleted was the current page for the
2056 // entire tab control, then record the window
2057 // pointer of the new active page for activation
2058 if (is_curpage)
2059 {
2060 new_active = ctrl->GetWindowFromIdx(ctrl_idx);
2061 }
2062 }
2063 }
2064 else
2065 {
2066 // we are not deleting the active page, so keep it the same
2067 new_active = active_wnd;
2068 }
2069
2070
2071 if (!new_active)
2072 {
2073 // we haven't yet found a new page to active,
2074 // so select the next page from the main tab
2075 // catalogue
2076
2077 if (page_idx < m_tabs.GetPageCount())
2078 {
2079 new_active = m_tabs.GetPage(page_idx).window;
2080 }
2081
2082 if (!new_active && m_tabs.GetPageCount() > 0)
2083 {
2084 new_active = m_tabs.GetPage(0).window;
2085 }
2086 }
2087
2088
2089 RemoveEmptyTabFrames();
2090
2091 m_curPage = wxNOT_FOUND;
2092
2093 // set new active pane unless we're being destroyed anyhow
2094 if (new_active && !m_isBeingDeleted)
2095 SetSelectionToWindow(new_active);
2096
2097 return true;
2098 }
2099
2100 // GetPageIndex() returns the index of the page, or -1 if the
2101 // page could not be located in the notebook
2102 int wxAuiNotebook::GetPageIndex(wxWindow* page_wnd) const
2103 {
2104 return m_tabs.GetIdxFromWindow(page_wnd);
2105 }
2106
2107
2108
2109 // SetPageText() changes the tab caption of the specified page
2110 bool wxAuiNotebook::SetPageText(size_t page_idx, const wxString& text)
2111 {
2112 if (page_idx >= m_tabs.GetPageCount())
2113 return false;
2114
2115 // update our own tab catalog
2116 wxAuiNotebookPage& page_info = m_tabs.GetPage(page_idx);
2117 page_info.caption = text;
2118
2119 // update what's on screen
2120 wxAuiTabCtrl* ctrl;
2121 int ctrl_idx;
2122 if (FindTab(page_info.window, &ctrl, &ctrl_idx))
2123 {
2124 wxAuiNotebookPage& info = ctrl->GetPage(ctrl_idx);
2125 info.caption = text;
2126 ctrl->Refresh();
2127 ctrl->Update();
2128 }
2129
2130 return true;
2131 }
2132
2133 // returns the page caption
2134 wxString wxAuiNotebook::GetPageText(size_t page_idx) const
2135 {
2136 if (page_idx >= m_tabs.GetPageCount())
2137 return wxEmptyString;
2138
2139 // update our own tab catalog
2140 const wxAuiNotebookPage& page_info = m_tabs.GetPage(page_idx);
2141 return page_info.caption;
2142 }
2143
2144 bool wxAuiNotebook::SetPageToolTip(size_t page_idx, const wxString& text)
2145 {
2146 if (page_idx >= m_tabs.GetPageCount())
2147 return false;
2148
2149 // update our own tab catalog
2150 wxAuiNotebookPage& page_info = m_tabs.GetPage(page_idx);
2151 page_info.tooltip = text;
2152
2153 wxAuiTabCtrl* ctrl;
2154 int ctrl_idx;
2155 if (!FindTab(page_info.window, &ctrl, &ctrl_idx))
2156 return false;
2157
2158 wxAuiNotebookPage& info = ctrl->GetPage(ctrl_idx);
2159 info.tooltip = text;
2160
2161 // NB: we don't update the tooltip if it is already being displayed, it
2162 // typically never happens, no need to code that
2163 return true;
2164 }
2165
2166 wxString wxAuiNotebook::GetPageToolTip(size_t page_idx) const
2167 {
2168 if (page_idx >= m_tabs.GetPageCount())
2169 return wxString();
2170
2171 const wxAuiNotebookPage& page_info = m_tabs.GetPage(page_idx);
2172 return page_info.tooltip;
2173 }
2174
2175 bool wxAuiNotebook::SetPageBitmap(size_t page_idx, const wxBitmap& bitmap)
2176 {
2177 if (page_idx >= m_tabs.GetPageCount())
2178 return false;
2179
2180 // update our own tab catalog
2181 wxAuiNotebookPage& page_info = m_tabs.GetPage(page_idx);
2182 page_info.bitmap = bitmap;
2183
2184 // tab height might have changed
2185 UpdateTabCtrlHeight();
2186
2187 // update what's on screen
2188 wxAuiTabCtrl* ctrl;
2189 int ctrl_idx;
2190 if (FindTab(page_info.window, &ctrl, &ctrl_idx))
2191 {
2192 wxAuiNotebookPage& info = ctrl->GetPage(ctrl_idx);
2193 info.bitmap = bitmap;
2194 ctrl->Refresh();
2195 ctrl->Update();
2196 }
2197
2198 return true;
2199 }
2200
2201 // returns the page bitmap
2202 wxBitmap wxAuiNotebook::GetPageBitmap(size_t page_idx) const
2203 {
2204 if (page_idx >= m_tabs.GetPageCount())
2205 return wxBitmap();
2206
2207 // update our own tab catalog
2208 const wxAuiNotebookPage& page_info = m_tabs.GetPage(page_idx);
2209 return page_info.bitmap;
2210 }
2211
2212 // GetSelection() returns the index of the currently active page
2213 int wxAuiNotebook::GetSelection() const
2214 {
2215 return m_curPage;
2216 }
2217
2218 // SetSelection() sets the currently active page
2219 int wxAuiNotebook::SetSelection(size_t new_page)
2220 {
2221 return DoModifySelection(new_page, true);
2222 }
2223
2224 void wxAuiNotebook::SetSelectionToWindow(wxWindow *win)
2225 {
2226 const int idx = m_tabs.GetIdxFromWindow(win);
2227 wxCHECK_RET( idx != wxNOT_FOUND, wxT("invalid notebook page") );
2228
2229
2230 // since a tab was clicked, let the parent know that we received
2231 // the focus, even if we will assign that focus immediately
2232 // to the child tab in the SetSelection call below
2233 // (the child focus event will also let wxAuiManager, if any,
2234 // know that the notebook control has been activated)
2235
2236 wxWindow* parent = GetParent();
2237 if (parent)
2238 {
2239 wxChildFocusEvent eventFocus(this);
2240 parent->GetEventHandler()->ProcessEvent(eventFocus);
2241 }
2242
2243
2244 SetSelection(idx);
2245 }
2246
2247 // GetPageCount() returns the total number of
2248 // pages managed by the multi-notebook
2249 size_t wxAuiNotebook::GetPageCount() const
2250 {
2251 return m_tabs.GetPageCount();
2252 }
2253
2254 // GetPage() returns the wxWindow pointer of the
2255 // specified page
2256 wxWindow* wxAuiNotebook::GetPage(size_t page_idx) const
2257 {
2258 wxASSERT(page_idx < m_tabs.GetPageCount());
2259
2260 return m_tabs.GetWindowFromIdx(page_idx);
2261 }
2262
2263 // DoSizing() performs all sizing operations in each tab control
2264 void wxAuiNotebook::DoSizing()
2265 {
2266 wxAuiPaneInfoArray& all_panes = m_mgr.GetAllPanes();
2267 size_t i, pane_count = all_panes.GetCount();
2268 for (i = 0; i < pane_count; ++i)
2269 {
2270 if (all_panes.Item(i).name == wxT("dummy"))
2271 continue;
2272
2273 wxTabFrame* tabframe = (wxTabFrame*)all_panes.Item(i).window;
2274 tabframe->DoSizing();
2275 }
2276 }
2277
2278 // GetActiveTabCtrl() returns the active tab control. It is
2279 // called to determine which control gets new windows being added
2280 wxAuiTabCtrl* wxAuiNotebook::GetActiveTabCtrl()
2281 {
2282 if (m_curPage >= 0 && m_curPage < (int)m_tabs.GetPageCount())
2283 {
2284 wxAuiTabCtrl* ctrl;
2285 int idx;
2286
2287 // find the tab ctrl with the current page
2288 if (FindTab(m_tabs.GetPage(m_curPage).window,
2289 &ctrl, &idx))
2290 {
2291 return ctrl;
2292 }
2293 }
2294
2295 // no current page, just find the first tab ctrl
2296 wxAuiPaneInfoArray& all_panes = m_mgr.GetAllPanes();
2297 size_t i, pane_count = all_panes.GetCount();
2298 for (i = 0; i < pane_count; ++i)
2299 {
2300 if (all_panes.Item(i).name == wxT("dummy"))
2301 continue;
2302
2303 wxTabFrame* tabframe = (wxTabFrame*)all_panes.Item(i).window;
2304 return tabframe->m_tabs;
2305 }
2306
2307 // If there is no tabframe at all, create one
2308 wxTabFrame* tabframe = new wxTabFrame;
2309 tabframe->SetTabCtrlHeight(m_tabCtrlHeight);
2310 tabframe->m_tabs = new wxAuiTabCtrl(this,
2311 m_tabIdCounter++,
2312 wxDefaultPosition,
2313 wxDefaultSize,
2314 wxNO_BORDER|wxWANTS_CHARS);
2315 tabframe->m_tabs->SetFlags(m_flags);
2316 tabframe->m_tabs->SetArtProvider(m_tabs.GetArtProvider()->Clone());
2317 m_mgr.AddPane(tabframe,
2318 wxAuiPaneInfo().Center().CaptionVisible(false));
2319
2320 m_mgr.Update();
2321
2322 return tabframe->m_tabs;
2323 }
2324
2325 // FindTab() finds the tab control that currently contains the window as well
2326 // as the index of the window in the tab control. It returns true if the
2327 // window was found, otherwise false.
2328 bool wxAuiNotebook::FindTab(wxWindow* page, wxAuiTabCtrl** ctrl, int* idx)
2329 {
2330 wxAuiPaneInfoArray& all_panes = m_mgr.GetAllPanes();
2331 size_t i, pane_count = all_panes.GetCount();
2332 for (i = 0; i < pane_count; ++i)
2333 {
2334 if (all_panes.Item(i).name == wxT("dummy"))
2335 continue;
2336
2337 wxTabFrame* tabframe = (wxTabFrame*)all_panes.Item(i).window;
2338
2339 int page_idx = tabframe->m_tabs->GetIdxFromWindow(page);
2340 if (page_idx != -1)
2341 {
2342 *ctrl = tabframe->m_tabs;
2343 *idx = page_idx;
2344 return true;
2345 }
2346 }
2347
2348 return false;
2349 }
2350
2351 void wxAuiNotebook::Split(size_t page, int direction)
2352 {
2353 wxSize cli_size = GetClientSize();
2354
2355 // get the page's window pointer
2356 wxWindow* wnd = GetPage(page);
2357 if (!wnd)
2358 return;
2359
2360 // notebooks with 1 or less pages can't be split
2361 if (GetPageCount() < 2)
2362 return;
2363
2364 // find out which tab control the page currently belongs to
2365 wxAuiTabCtrl *src_tabs, *dest_tabs;
2366 int src_idx = -1;
2367 src_tabs = NULL;
2368 if (!FindTab(wnd, &src_tabs, &src_idx))
2369 return;
2370 if (!src_tabs || src_idx == -1)
2371 return;
2372
2373 // choose a split size
2374 wxSize split_size;
2375 if (GetPageCount() > 2)
2376 {
2377 split_size = CalculateNewSplitSize();
2378 }
2379 else
2380 {
2381 // because there are two panes, always split them
2382 // equally
2383 split_size = GetClientSize();
2384 split_size.x /= 2;
2385 split_size.y /= 2;
2386 }
2387
2388
2389 // create a new tab frame
2390 wxTabFrame* new_tabs = new wxTabFrame;
2391 new_tabs->m_rect = wxRect(wxPoint(0,0), split_size);
2392 new_tabs->SetTabCtrlHeight(m_tabCtrlHeight);
2393 new_tabs->m_tabs = new wxAuiTabCtrl(this,
2394 m_tabIdCounter++,
2395 wxDefaultPosition,
2396 wxDefaultSize,
2397 wxNO_BORDER|wxWANTS_CHARS);
2398 new_tabs->m_tabs->SetArtProvider(m_tabs.GetArtProvider()->Clone());
2399 new_tabs->m_tabs->SetFlags(m_flags);
2400 dest_tabs = new_tabs->m_tabs;
2401
2402 // create a pane info structure with the information
2403 // about where the pane should be added
2404 wxAuiPaneInfo paneInfo = wxAuiPaneInfo().Bottom().CaptionVisible(false);
2405 wxPoint mouse_pt;
2406
2407 if (direction == wxLEFT)
2408 {
2409 paneInfo.Left();
2410 mouse_pt = wxPoint(0, cli_size.y/2);
2411 }
2412 else if (direction == wxRIGHT)
2413 {
2414 paneInfo.Right();
2415 mouse_pt = wxPoint(cli_size.x, cli_size.y/2);
2416 }
2417 else if (direction == wxTOP)
2418 {
2419 paneInfo.Top();
2420 mouse_pt = wxPoint(cli_size.x/2, 0);
2421 }
2422 else if (direction == wxBOTTOM)
2423 {
2424 paneInfo.Bottom();
2425 mouse_pt = wxPoint(cli_size.x/2, cli_size.y);
2426 }
2427
2428 m_mgr.AddPane(new_tabs, paneInfo, mouse_pt);
2429 m_mgr.Update();
2430
2431 // remove the page from the source tabs
2432 wxAuiNotebookPage page_info = src_tabs->GetPage(src_idx);
2433 page_info.active = false;
2434 src_tabs->RemovePage(page_info.window);
2435 if (src_tabs->GetPageCount() > 0)
2436 {
2437 src_tabs->SetActivePage((size_t)0);
2438 src_tabs->DoShowHide();
2439 src_tabs->Refresh();
2440 }
2441
2442
2443 // add the page to the destination tabs
2444 dest_tabs->InsertPage(page_info.window, page_info, 0);
2445
2446 if (src_tabs->GetPageCount() == 0)
2447 {
2448 RemoveEmptyTabFrames();
2449 }
2450
2451 DoSizing();
2452 dest_tabs->DoShowHide();
2453 dest_tabs->Refresh();
2454
2455 // force the set selection function reset the selection
2456 m_curPage = -1;
2457
2458 // set the active page to the one we just split off
2459 SetSelectionToPage(page_info);
2460
2461 UpdateHintWindowSize();
2462 }
2463
2464
2465 void wxAuiNotebook::OnSize(wxSizeEvent& evt)
2466 {
2467 UpdateHintWindowSize();
2468
2469 evt.Skip();
2470 }
2471
2472 void wxAuiNotebook::OnTabClicked(wxAuiNotebookEvent& evt)
2473 {
2474 wxAuiTabCtrl* ctrl = (wxAuiTabCtrl*)evt.GetEventObject();
2475 wxASSERT(ctrl != NULL);
2476
2477 wxWindow* wnd = ctrl->GetWindowFromIdx(evt.GetSelection());
2478 wxASSERT(wnd != NULL);
2479
2480 SetSelectionToWindow(wnd);
2481 }
2482
2483 void wxAuiNotebook::OnTabBgDClick(wxAuiNotebookEvent& WXUNUSED(evt))
2484 {
2485 // notify owner that the tabbar background has been double-clicked
2486 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_BG_DCLICK, m_windowId);
2487 e.SetEventObject(this);
2488 GetEventHandler()->ProcessEvent(e);
2489 }
2490
2491 void wxAuiNotebook::OnTabBeginDrag(wxAuiNotebookEvent&)
2492 {
2493 m_lastDragX = 0;
2494 }
2495
2496 void wxAuiNotebook::OnTabDragMotion(wxAuiNotebookEvent& evt)
2497 {
2498 wxPoint screen_pt = ::wxGetMousePosition();
2499 wxPoint client_pt = ScreenToClient(screen_pt);
2500 wxPoint zero(0,0);
2501
2502 wxAuiTabCtrl* src_tabs = (wxAuiTabCtrl*)evt.GetEventObject();
2503 wxAuiTabCtrl* dest_tabs = GetTabCtrlFromPoint(client_pt);
2504
2505 if (dest_tabs == src_tabs)
2506 {
2507 if (src_tabs)
2508 {
2509 src_tabs->SetCursor(wxCursor(wxCURSOR_ARROW));
2510 }
2511
2512 // always hide the hint for inner-tabctrl drag
2513 m_mgr.HideHint();
2514
2515 // if tab moving is not allowed, leave
2516 if (!(m_flags & wxAUI_NB_TAB_MOVE))
2517 {
2518 return;
2519 }
2520
2521 wxPoint pt = dest_tabs->ScreenToClient(screen_pt);
2522 wxWindow* dest_location_tab;
2523
2524 // this is an inner-tab drag/reposition
2525 if (dest_tabs->TabHitTest(pt.x, pt.y, &dest_location_tab))
2526 {
2527 int src_idx = evt.GetSelection();
2528 int dest_idx = dest_tabs->GetIdxFromWindow(dest_location_tab);
2529
2530 // prevent jumpy drag
2531 if ((src_idx == dest_idx) || dest_idx == -1 ||
2532 (src_idx > dest_idx && m_lastDragX <= pt.x) ||
2533 (src_idx < dest_idx && m_lastDragX >= pt.x))
2534 {
2535 m_lastDragX = pt.x;
2536 return;
2537 }
2538
2539
2540 wxWindow* src_tab = dest_tabs->GetWindowFromIdx(src_idx);
2541 dest_tabs->MovePage(src_tab, dest_idx);
2542 dest_tabs->SetActivePage((size_t)dest_idx);
2543 dest_tabs->DoShowHide();
2544 dest_tabs->Refresh();
2545 m_lastDragX = pt.x;
2546
2547 }
2548
2549 return;
2550 }
2551
2552
2553 // if external drag is allowed, check if the tab is being dragged
2554 // over a different wxAuiNotebook control
2555 if (m_flags & wxAUI_NB_TAB_EXTERNAL_MOVE)
2556 {
2557 wxWindow* tab_ctrl = ::wxFindWindowAtPoint(screen_pt);
2558
2559 // if we aren't over any window, stop here
2560 if (!tab_ctrl)
2561 return;
2562
2563 // make sure we are not over the hint window
2564 if (!wxDynamicCast(tab_ctrl, wxFrame))
2565 {
2566 while (tab_ctrl)
2567 {
2568 if (wxDynamicCast(tab_ctrl, wxAuiTabCtrl))
2569 break;
2570 tab_ctrl = tab_ctrl->GetParent();
2571 }
2572
2573 if (tab_ctrl)
2574 {
2575 wxAuiNotebook* nb = (wxAuiNotebook*)tab_ctrl->GetParent();
2576
2577 if (nb != this)
2578 {
2579 wxRect hint_rect = tab_ctrl->GetClientRect();
2580 tab_ctrl->ClientToScreen(&hint_rect.x, &hint_rect.y);
2581 m_mgr.ShowHint(hint_rect);
2582 return;
2583 }
2584 }
2585 }
2586 else
2587 {
2588 if (!dest_tabs)
2589 {
2590 // we are either over a hint window, or not over a tab
2591 // window, and there is no where to drag to, so exit
2592 return;
2593 }
2594 }
2595 }
2596
2597
2598 // if there are less than two panes, split can't happen, so leave
2599 if (m_tabs.GetPageCount() < 2)
2600 return;
2601
2602 // if tab moving is not allowed, leave
2603 if (!(m_flags & wxAUI_NB_TAB_SPLIT))
2604 return;
2605
2606
2607 if (src_tabs)
2608 {
2609 src_tabs->SetCursor(wxCursor(wxCURSOR_SIZING));
2610 }
2611
2612
2613 if (dest_tabs)
2614 {
2615 wxRect hint_rect = dest_tabs->GetRect();
2616 ClientToScreen(&hint_rect.x, &hint_rect.y);
2617 m_mgr.ShowHint(hint_rect);
2618 }
2619 else
2620 {
2621 m_mgr.DrawHintRect(m_dummyWnd, client_pt, zero);
2622 }
2623 }
2624
2625
2626
2627 void wxAuiNotebook::OnTabEndDrag(wxAuiNotebookEvent& evt)
2628 {
2629 m_mgr.HideHint();
2630
2631
2632 wxAuiTabCtrl* src_tabs = (wxAuiTabCtrl*)evt.GetEventObject();
2633 wxCHECK_RET( src_tabs, wxT("no source object?") );
2634
2635 src_tabs->SetCursor(wxCursor(wxCURSOR_ARROW));
2636
2637 // get the mouse position, which will be used to determine the drop point
2638 wxPoint mouse_screen_pt = ::wxGetMousePosition();
2639 wxPoint mouse_client_pt = ScreenToClient(mouse_screen_pt);
2640
2641
2642
2643 // check for an external move
2644 if (m_flags & wxAUI_NB_TAB_EXTERNAL_MOVE)
2645 {
2646 wxWindow* tab_ctrl = ::wxFindWindowAtPoint(mouse_screen_pt);
2647
2648 while (tab_ctrl)
2649 {
2650 if (wxDynamicCast(tab_ctrl, wxAuiTabCtrl))
2651 break;
2652 tab_ctrl = tab_ctrl->GetParent();
2653 }
2654
2655 if (tab_ctrl)
2656 {
2657 wxAuiNotebook* nb = (wxAuiNotebook*)tab_ctrl->GetParent();
2658
2659 if (nb != this)
2660 {
2661 // find out from the destination control
2662 // if it's ok to drop this tab here
2663 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_ALLOW_DND, m_windowId);
2664 e.SetSelection(evt.GetSelection());
2665 e.SetOldSelection(evt.GetSelection());
2666 e.SetEventObject(this);
2667 e.SetDragSource(this);
2668 e.Veto(); // dropping must be explicitly approved by control owner
2669
2670 nb->GetEventHandler()->ProcessEvent(e);
2671
2672 if (!e.IsAllowed())
2673 {
2674 // no answer or negative answer
2675 m_mgr.HideHint();
2676 return;
2677 }
2678
2679 // drop was allowed
2680 int src_idx = evt.GetSelection();
2681 wxWindow* src_page = src_tabs->GetWindowFromIdx(src_idx);
2682
2683 // Check that it's not an impossible parent relationship
2684 wxWindow* p = nb;
2685 while (p && !p->IsTopLevel())
2686 {
2687 if (p == src_page)
2688 {
2689 return;
2690 }
2691 p = p->GetParent();
2692 }
2693
2694 // get main index of the page
2695 int main_idx = m_tabs.GetIdxFromWindow(src_page);
2696 wxCHECK_RET( main_idx != wxNOT_FOUND, wxT("no source page?") );
2697
2698
2699 // make a copy of the page info
2700 wxAuiNotebookPage page_info = m_tabs.GetPage(main_idx);
2701
2702 // remove the page from the source notebook
2703 RemovePage(main_idx);
2704
2705 // reparent the page
2706 src_page->Reparent(nb);
2707
2708
2709 // found out the insert idx
2710 wxAuiTabCtrl* dest_tabs = (wxAuiTabCtrl*)tab_ctrl;
2711 wxPoint pt = dest_tabs->ScreenToClient(mouse_screen_pt);
2712
2713 wxWindow* target = NULL;
2714 int insert_idx = -1;
2715 dest_tabs->TabHitTest(pt.x, pt.y, &target);
2716 if (target)
2717 {
2718 insert_idx = dest_tabs->GetIdxFromWindow(target);
2719 }
2720
2721
2722 // add the page to the new notebook
2723 if (insert_idx == -1)
2724 insert_idx = dest_tabs->GetPageCount();
2725 dest_tabs->InsertPage(page_info.window, page_info, insert_idx);
2726 nb->m_tabs.AddPage(page_info.window, page_info);
2727
2728 nb->DoSizing();
2729 dest_tabs->DoShowHide();
2730 dest_tabs->Refresh();
2731
2732 // set the selection in the destination tab control
2733 nb->SetSelectionToPage(page_info);
2734
2735 // notify owner that the tab has been dragged
2736 wxAuiNotebookEvent e2(wxEVT_COMMAND_AUINOTEBOOK_DRAG_DONE, m_windowId);
2737 e2.SetSelection(evt.GetSelection());
2738 e2.SetOldSelection(evt.GetSelection());
2739 e2.SetEventObject(this);
2740 GetEventHandler()->ProcessEvent(e2);
2741
2742 return;
2743 }
2744 }
2745 }
2746
2747
2748
2749
2750 // only perform a tab split if it's allowed
2751 wxAuiTabCtrl* dest_tabs = NULL;
2752
2753 if ((m_flags & wxAUI_NB_TAB_SPLIT) && m_tabs.GetPageCount() >= 2)
2754 {
2755 // If the pointer is in an existing tab frame, do a tab insert
2756 wxWindow* hit_wnd = ::wxFindWindowAtPoint(mouse_screen_pt);
2757 wxTabFrame* tab_frame = (wxTabFrame*)GetTabFrameFromTabCtrl(hit_wnd);
2758 int insert_idx = -1;
2759 if (tab_frame)
2760 {
2761 dest_tabs = tab_frame->m_tabs;
2762
2763 if (dest_tabs == src_tabs)
2764 return;
2765
2766
2767 wxPoint pt = dest_tabs->ScreenToClient(mouse_screen_pt);
2768 wxWindow* target = NULL;
2769 dest_tabs->TabHitTest(pt.x, pt.y, &target);
2770 if (target)
2771 {
2772 insert_idx = dest_tabs->GetIdxFromWindow(target);
2773 }
2774 }
2775 else
2776 {
2777 wxPoint zero(0,0);
2778 wxRect rect = m_mgr.CalculateHintRect(m_dummyWnd,
2779 mouse_client_pt,
2780 zero);
2781 if (rect.IsEmpty())
2782 {
2783 // there is no suitable drop location here, exit out
2784 return;
2785 }
2786
2787 // If there is no tabframe at all, create one
2788 wxTabFrame* new_tabs = new wxTabFrame;
2789 new_tabs->m_rect = wxRect(wxPoint(0,0), CalculateNewSplitSize());
2790 new_tabs->SetTabCtrlHeight(m_tabCtrlHeight);
2791 new_tabs->m_tabs = new wxAuiTabCtrl(this,
2792 m_tabIdCounter++,
2793 wxDefaultPosition,
2794 wxDefaultSize,
2795 wxNO_BORDER|wxWANTS_CHARS);
2796 new_tabs->m_tabs->SetArtProvider(m_tabs.GetArtProvider()->Clone());
2797 new_tabs->m_tabs->SetFlags(m_flags);
2798
2799 m_mgr.AddPane(new_tabs,
2800 wxAuiPaneInfo().Bottom().CaptionVisible(false),
2801 mouse_client_pt);
2802 m_mgr.Update();
2803 dest_tabs = new_tabs->m_tabs;
2804 }
2805
2806
2807
2808 // remove the page from the source tabs
2809 wxAuiNotebookPage page_info = src_tabs->GetPage(evt.GetSelection());
2810 page_info.active = false;
2811 src_tabs->RemovePage(page_info.window);
2812 if (src_tabs->GetPageCount() > 0)
2813 {
2814 src_tabs->SetActivePage((size_t)0);
2815 src_tabs->DoShowHide();
2816 src_tabs->Refresh();
2817 }
2818
2819
2820
2821 // add the page to the destination tabs
2822 if (insert_idx == -1)
2823 insert_idx = dest_tabs->GetPageCount();
2824 dest_tabs->InsertPage(page_info.window, page_info, insert_idx);
2825
2826 if (src_tabs->GetPageCount() == 0)
2827 {
2828 RemoveEmptyTabFrames();
2829 }
2830
2831 DoSizing();
2832 dest_tabs->DoShowHide();
2833 dest_tabs->Refresh();
2834
2835 // force the set selection function reset the selection
2836 m_curPage = -1;
2837
2838 // set the active page to the one we just split off
2839 SetSelectionToPage(page_info);
2840
2841 UpdateHintWindowSize();
2842 }
2843
2844 // notify owner that the tab has been dragged
2845 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_DRAG_DONE, m_windowId);
2846 e.SetSelection(evt.GetSelection());
2847 e.SetOldSelection(evt.GetSelection());
2848 e.SetEventObject(this);
2849 GetEventHandler()->ProcessEvent(e);
2850 }
2851
2852
2853
2854 void wxAuiNotebook::OnTabCancelDrag(wxAuiNotebookEvent& command_evt)
2855 {
2856 wxAuiNotebookEvent& evt = (wxAuiNotebookEvent&)command_evt;
2857
2858 m_mgr.HideHint();
2859
2860 wxAuiTabCtrl* src_tabs = (wxAuiTabCtrl*)evt.GetEventObject();
2861 wxCHECK_RET( src_tabs, wxT("no source object?") );
2862
2863 src_tabs->SetCursor(wxCursor(wxCURSOR_ARROW));
2864 }
2865
2866 wxAuiTabCtrl* wxAuiNotebook::GetTabCtrlFromPoint(const wxPoint& pt)
2867 {
2868 // if we've just removed the last tab from the source
2869 // tab set, the remove the tab control completely
2870 wxAuiPaneInfoArray& all_panes = m_mgr.GetAllPanes();
2871 size_t i, pane_count = all_panes.GetCount();
2872 for (i = 0; i < pane_count; ++i)
2873 {
2874 if (all_panes.Item(i).name == wxT("dummy"))
2875 continue;
2876
2877 wxTabFrame* tabframe = (wxTabFrame*)all_panes.Item(i).window;
2878 if (tabframe->m_tab_rect.Contains(pt))
2879 return tabframe->m_tabs;
2880 }
2881
2882 return NULL;
2883 }
2884
2885 wxWindow* wxAuiNotebook::GetTabFrameFromTabCtrl(wxWindow* tab_ctrl)
2886 {
2887 // if we've just removed the last tab from the source
2888 // tab set, the remove the tab control completely
2889 wxAuiPaneInfoArray& all_panes = m_mgr.GetAllPanes();
2890 size_t i, pane_count = all_panes.GetCount();
2891 for (i = 0; i < pane_count; ++i)
2892 {
2893 if (all_panes.Item(i).name == wxT("dummy"))
2894 continue;
2895
2896 wxTabFrame* tabframe = (wxTabFrame*)all_panes.Item(i).window;
2897 if (tabframe->m_tabs == tab_ctrl)
2898 {
2899 return tabframe;
2900 }
2901 }
2902
2903 return NULL;
2904 }
2905
2906 void wxAuiNotebook::RemoveEmptyTabFrames()
2907 {
2908 // if we've just removed the last tab from the source
2909 // tab set, the remove the tab control completely
2910 wxAuiPaneInfoArray all_panes = m_mgr.GetAllPanes();
2911 size_t i, pane_count = all_panes.GetCount();
2912 for (i = 0; i < pane_count; ++i)
2913 {
2914 if (all_panes.Item(i).name == wxT("dummy"))
2915 continue;
2916
2917 wxTabFrame* tab_frame = (wxTabFrame*)all_panes.Item(i).window;
2918 if (tab_frame->m_tabs->GetPageCount() == 0)
2919 {
2920 m_mgr.DetachPane(tab_frame);
2921
2922 // use pending delete because sometimes during
2923 // window closing, refreshs are pending
2924 if (!wxPendingDelete.Member(tab_frame->m_tabs))
2925 wxPendingDelete.Append(tab_frame->m_tabs);
2926
2927 tab_frame->m_tabs = NULL;
2928
2929 delete tab_frame;
2930 }
2931 }
2932
2933
2934 // check to see if there is still a center pane;
2935 // if there isn't, make a frame the center pane
2936 wxAuiPaneInfoArray panes = m_mgr.GetAllPanes();
2937 pane_count = panes.GetCount();
2938 wxWindow* first_good = NULL;
2939 bool center_found = false;
2940 for (i = 0; i < pane_count; ++i)
2941 {
2942 if (panes.Item(i).name == wxT("dummy"))
2943 continue;
2944 if (panes.Item(i).dock_direction == wxAUI_DOCK_CENTRE)
2945 center_found = true;
2946 if (!first_good)
2947 first_good = panes.Item(i).window;
2948 }
2949
2950 if (!center_found && first_good)
2951 {
2952 m_mgr.GetPane(first_good).Centre();
2953 }
2954
2955 if (!m_isBeingDeleted)
2956 m_mgr.Update();
2957 }
2958
2959 void wxAuiNotebook::OnChildFocusNotebook(wxChildFocusEvent& evt)
2960 {
2961 evt.Skip();
2962
2963 // if we're dragging a tab, don't change the current selection.
2964 // This code prevents a bug that used to happen when the hint window
2965 // was hidden. In the bug, the focus would return to the notebook
2966 // child, which would then enter this handler and call
2967 // SetSelection, which is not desired turn tab dragging.
2968
2969 wxAuiPaneInfoArray& all_panes = m_mgr.GetAllPanes();
2970 size_t i, pane_count = all_panes.GetCount();
2971 for (i = 0; i < pane_count; ++i)
2972 {
2973 wxAuiPaneInfo& pane = all_panes.Item(i);
2974 if (pane.name == wxT("dummy"))
2975 continue;
2976 wxTabFrame* tabframe = (wxTabFrame*)pane.window;
2977 if (tabframe->m_tabs->IsDragging())
2978 return;
2979 }
2980
2981
2982 // change the tab selection to the child
2983 // which was focused
2984 int idx = m_tabs.GetIdxFromWindow(evt.GetWindow());
2985 if (idx != -1 && idx != m_curPage)
2986 {
2987 SetSelection(idx);
2988 }
2989 }
2990
2991 void wxAuiNotebook::OnNavigationKeyNotebook(wxNavigationKeyEvent& event)
2992 {
2993 if ( event.IsWindowChange() ) {
2994 // change pages
2995 // FIXME: the problem with this is that if we have a split notebook,
2996 // we selection may go all over the place.
2997 AdvanceSelection(event.GetDirection());
2998 }
2999 else {
3000 // we get this event in 3 cases
3001 //
3002 // a) one of our pages might have generated it because the user TABbed
3003 // out from it in which case we should propagate the event upwards and
3004 // our parent will take care of setting the focus to prev/next sibling
3005 //
3006 // or
3007 //
3008 // b) the parent panel wants to give the focus to us so that we
3009 // forward it to our selected page. We can't deal with this in
3010 // OnSetFocus() because we don't know which direction the focus came
3011 // from in this case and so can't choose between setting the focus to
3012 // first or last panel child
3013 //
3014 // or
3015 //
3016 // c) we ourselves (see MSWTranslateMessage) generated the event
3017 //
3018 wxWindow * const parent = GetParent();
3019
3020 // the wxObject* casts are required to avoid MinGW GCC 2.95.3 ICE
3021 const bool isFromParent = event.GetEventObject() == (wxObject*) parent;
3022 const bool isFromSelf = event.GetEventObject() == (wxObject*) this;
3023
3024 if ( isFromParent || isFromSelf )
3025 {
3026 // no, it doesn't come from child, case (b) or (c): forward to a
3027 // page but only if direction is backwards (TAB) or from ourselves,
3028 if ( GetSelection() != wxNOT_FOUND &&
3029 (!event.GetDirection() || isFromSelf) )
3030 {
3031 // so that the page knows that the event comes from it's parent
3032 // and is being propagated downwards
3033 event.SetEventObject(this);
3034
3035 wxWindow *page = GetPage(GetSelection());
3036 if ( !page->GetEventHandler()->ProcessEvent(event) )
3037 {
3038 page->SetFocus();
3039 }
3040 //else: page manages focus inside it itself
3041 }
3042 else // otherwise set the focus to the notebook itself
3043 {
3044 SetFocus();
3045 }
3046 }
3047 else
3048 {
3049 // it comes from our child, case (a), pass to the parent, but only
3050 // if the direction is forwards. Otherwise set the focus to the
3051 // notebook itself. The notebook is always the 'first' control of a
3052 // page.
3053 if ( !event.GetDirection() )
3054 {
3055 SetFocus();
3056 }
3057 else if ( parent )
3058 {
3059 event.SetCurrentFocus(this);
3060 parent->GetEventHandler()->ProcessEvent(event);
3061 }
3062 }
3063 }
3064 }
3065
3066 void wxAuiNotebook::OnTabButton(wxAuiNotebookEvent& evt)
3067 {
3068 wxAuiTabCtrl* tabs = (wxAuiTabCtrl*)evt.GetEventObject();
3069
3070 int button_id = evt.GetInt();
3071
3072 if (button_id == wxAUI_BUTTON_CLOSE)
3073 {
3074 int selection = evt.GetSelection();
3075
3076 if (selection == -1)
3077 {
3078 // if the close button is to the right, use the active
3079 // page selection to determine which page to close
3080 selection = tabs->GetActivePage();
3081 }
3082
3083 if (selection != -1)
3084 {
3085 wxWindow* close_wnd = tabs->GetWindowFromIdx(selection);
3086
3087 // ask owner if it's ok to close the tab
3088 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CLOSE, m_windowId);
3089 e.SetSelection(m_tabs.GetIdxFromWindow(close_wnd));
3090 const int idx = m_tabs.GetIdxFromWindow(close_wnd);
3091 e.SetSelection(idx);
3092 e.SetOldSelection(evt.GetSelection());
3093 e.SetEventObject(this);
3094 GetEventHandler()->ProcessEvent(e);
3095 if (!e.IsAllowed())
3096 return;
3097
3098
3099 #if wxUSE_MDI
3100 if (wxDynamicCast(close_wnd, wxAuiMDIChildFrame))
3101 {
3102 close_wnd->Close();
3103 }
3104 else
3105 #endif
3106 {
3107 int main_idx = m_tabs.GetIdxFromWindow(close_wnd);
3108 wxCHECK_RET( main_idx != wxNOT_FOUND, wxT("no page to delete?") );
3109
3110 DeletePage(main_idx);
3111 }
3112
3113 // notify owner that the tab has been closed
3114 wxAuiNotebookEvent e2(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CLOSED, m_windowId);
3115 e2.SetSelection(idx);
3116 e2.SetEventObject(this);
3117 GetEventHandler()->ProcessEvent(e2);
3118 }
3119 }
3120 }
3121
3122
3123 void wxAuiNotebook::OnTabMiddleDown(wxAuiNotebookEvent& evt)
3124 {
3125 // patch event through to owner
3126 wxAuiTabCtrl* tabs = (wxAuiTabCtrl*)evt.GetEventObject();
3127 wxWindow* wnd = tabs->GetWindowFromIdx(evt.GetSelection());
3128
3129 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_TAB_MIDDLE_DOWN, m_windowId);
3130 e.SetSelection(m_tabs.GetIdxFromWindow(wnd));
3131 e.SetEventObject(this);
3132 GetEventHandler()->ProcessEvent(e);
3133 }
3134
3135 void wxAuiNotebook::OnTabMiddleUp(wxAuiNotebookEvent& evt)
3136 {
3137 // if the wxAUI_NB_MIDDLE_CLICK_CLOSE is specified, middle
3138 // click should act like a tab close action. However, first
3139 // give the owner an opportunity to handle the middle up event
3140 // for custom action
3141
3142 wxAuiTabCtrl* tabs = (wxAuiTabCtrl*)evt.GetEventObject();
3143 wxWindow* wnd = tabs->GetWindowFromIdx(evt.GetSelection());
3144
3145 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_TAB_MIDDLE_UP, m_windowId);
3146 e.SetSelection(m_tabs.GetIdxFromWindow(wnd));
3147 e.SetEventObject(this);
3148 if (GetEventHandler()->ProcessEvent(e))
3149 return;
3150 if (!e.IsAllowed())
3151 return;
3152
3153 // check if we are supposed to close on middle-up
3154 if ((m_flags & wxAUI_NB_MIDDLE_CLICK_CLOSE) == 0)
3155 return;
3156
3157 // simulate the user pressing the close button on the tab
3158 evt.SetInt(wxAUI_BUTTON_CLOSE);
3159 OnTabButton(evt);
3160 }
3161
3162 void wxAuiNotebook::OnTabRightDown(wxAuiNotebookEvent& evt)
3163 {
3164 // patch event through to owner
3165 wxAuiTabCtrl* tabs = (wxAuiTabCtrl*)evt.GetEventObject();
3166 wxWindow* wnd = tabs->GetWindowFromIdx(evt.GetSelection());
3167
3168 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_TAB_RIGHT_DOWN, m_windowId);
3169 e.SetSelection(m_tabs.GetIdxFromWindow(wnd));
3170 e.SetEventObject(this);
3171 GetEventHandler()->ProcessEvent(e);
3172 }
3173
3174 void wxAuiNotebook::OnTabRightUp(wxAuiNotebookEvent& evt)
3175 {
3176 // patch event through to owner
3177 wxAuiTabCtrl* tabs = (wxAuiTabCtrl*)evt.GetEventObject();
3178 wxWindow* wnd = tabs->GetWindowFromIdx(evt.GetSelection());
3179
3180 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_TAB_RIGHT_UP, m_windowId);
3181 e.SetSelection(m_tabs.GetIdxFromWindow(wnd));
3182 e.SetEventObject(this);
3183 GetEventHandler()->ProcessEvent(e);
3184 }
3185
3186 // Sets the normal font
3187 void wxAuiNotebook::SetNormalFont(const wxFont& font)
3188 {
3189 m_normalFont = font;
3190 GetArtProvider()->SetNormalFont(font);
3191 }
3192
3193 // Sets the selected tab font
3194 void wxAuiNotebook::SetSelectedFont(const wxFont& font)
3195 {
3196 m_selectedFont = font;
3197 GetArtProvider()->SetSelectedFont(font);
3198 }
3199
3200 // Sets the measuring font
3201 void wxAuiNotebook::SetMeasuringFont(const wxFont& font)
3202 {
3203 GetArtProvider()->SetMeasuringFont(font);
3204 }
3205
3206 // Sets the tab font
3207 bool wxAuiNotebook::SetFont(const wxFont& font)
3208 {
3209 wxControl::SetFont(font);
3210
3211 wxFont normalFont(font);
3212 wxFont selectedFont(normalFont);
3213 selectedFont.SetWeight(wxBOLD);
3214
3215 SetNormalFont(normalFont);
3216 SetSelectedFont(selectedFont);
3217 SetMeasuringFont(selectedFont);
3218
3219 return true;
3220 }
3221
3222 // Gets the tab control height
3223 int wxAuiNotebook::GetTabCtrlHeight() const
3224 {
3225 return m_tabCtrlHeight;
3226 }
3227
3228 // Gets the height of the notebook for a given page height
3229 int wxAuiNotebook::GetHeightForPageHeight(int pageHeight)
3230 {
3231 UpdateTabCtrlHeight();
3232
3233 int tabCtrlHeight = GetTabCtrlHeight();
3234 int decorHeight = 2;
3235 return tabCtrlHeight + pageHeight + decorHeight;
3236 }
3237
3238 // Shows the window menu
3239 bool wxAuiNotebook::ShowWindowMenu()
3240 {
3241 wxAuiTabCtrl* tabCtrl = GetActiveTabCtrl();
3242
3243 int idx = tabCtrl->GetArtProvider()->ShowDropDown(tabCtrl, tabCtrl->GetPages(), tabCtrl->GetActivePage());
3244
3245 if (idx != -1)
3246 {
3247 wxAuiNotebookEvent e(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING, tabCtrl->GetId());
3248 e.SetSelection(idx);
3249 e.SetOldSelection(tabCtrl->GetActivePage());
3250 e.SetEventObject(tabCtrl);
3251 GetEventHandler()->ProcessEvent(e);
3252
3253 return true;
3254 }
3255 else
3256 return false;
3257 }
3258
3259 void wxAuiNotebook::DoThaw()
3260 {
3261 DoSizing();
3262
3263 wxBookCtrlBase::DoThaw();
3264 }
3265
3266 void wxAuiNotebook::SetPageSize (const wxSize& WXUNUSED(size))
3267 {
3268 wxFAIL_MSG("Not implemented for wxAuiNotebook");
3269 }
3270
3271 int wxAuiNotebook::HitTest (const wxPoint& WXUNUSED(pt), long* WXUNUSED(flags)) const
3272 {
3273 wxFAIL_MSG("Not implemented for wxAuiNotebook");
3274 return wxNOT_FOUND;
3275 }
3276
3277 int wxAuiNotebook::GetPageImage(size_t WXUNUSED(n)) const
3278 {
3279 wxFAIL_MSG("Not implemented for wxAuiNotebook");
3280 return -1;
3281 }
3282
3283 bool wxAuiNotebook::SetPageImage(size_t n, int imageId)
3284 {
3285 return SetPageBitmap(n, GetImageList()->GetBitmap(imageId));
3286 }
3287
3288 int wxAuiNotebook::ChangeSelection(size_t n)
3289 {
3290 return DoModifySelection(n, false);
3291 }
3292
3293 bool wxAuiNotebook::AddPage(wxWindow *page, const wxString &text, bool select,
3294 int imageId)
3295 {
3296 if(HasImageList())
3297 {
3298 return AddPage(page, text, select, GetImageList()->GetBitmap(imageId));
3299 }
3300 else
3301 {
3302 return AddPage(page, text, select, wxNullBitmap);
3303 }
3304 }
3305
3306 bool wxAuiNotebook::DeleteAllPages()
3307 {
3308 size_t count = GetPageCount();
3309 for(size_t i = 0; i < count; i++)
3310 {
3311 DeletePage(0);
3312 }
3313 return true;
3314 }
3315
3316 bool wxAuiNotebook::InsertPage(size_t index, wxWindow *page,
3317 const wxString &text, bool select,
3318 int imageId)
3319 {
3320 if(HasImageList())
3321 {
3322 return InsertPage(index, page, text, select,
3323 GetImageList()->GetBitmap(imageId));
3324 }
3325 else
3326 {
3327 return InsertPage(index, page, text, select, wxNullBitmap);
3328 }
3329 }
3330
3331 int wxAuiNotebook::DoModifySelection(size_t n, bool events)
3332 {
3333 wxWindow* wnd = m_tabs.GetWindowFromIdx(n);
3334 if (!wnd)
3335 return m_curPage;
3336
3337 // don't change the page unless necessary;
3338 // however, clicking again on a tab should give it the focus.
3339 if ((int)n == m_curPage)
3340 {
3341 wxAuiTabCtrl* ctrl;
3342 int ctrl_idx;
3343 if (FindTab(wnd, &ctrl, &ctrl_idx))
3344 {
3345 if (FindFocus() != ctrl)
3346 ctrl->SetFocus();
3347 }
3348 return m_curPage;
3349 }
3350
3351 bool vetoed = false;
3352
3353 wxAuiNotebookEvent evt(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING, m_windowId);
3354
3355 if(events)
3356 {
3357 evt.SetSelection(n);
3358 evt.SetOldSelection(m_curPage);
3359 evt.SetEventObject(this);
3360 GetEventHandler()->ProcessEvent(evt);
3361 vetoed = !evt.IsAllowed();
3362 }
3363
3364 if (!vetoed)
3365 {
3366 int old_curpage = m_curPage;
3367 m_curPage = n;
3368
3369 // program allows the page change
3370 if(events)
3371 {
3372 evt.SetEventType(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGED);
3373 (void)GetEventHandler()->ProcessEvent(evt);
3374 }
3375
3376
3377 wxAuiTabCtrl* ctrl;
3378 int ctrl_idx;
3379 if (FindTab(wnd, &ctrl, &ctrl_idx))
3380 {
3381 m_tabs.SetActivePage(wnd);
3382
3383 ctrl->SetActivePage(ctrl_idx);
3384 DoSizing();
3385 ctrl->DoShowHide();
3386
3387 ctrl->MakeTabVisible(ctrl_idx, ctrl);
3388
3389 // set fonts
3390 wxAuiPaneInfoArray& all_panes = m_mgr.GetAllPanes();
3391 size_t i, pane_count = all_panes.GetCount();
3392 for (i = 0; i < pane_count; ++i)
3393 {
3394 wxAuiPaneInfo& pane = all_panes.Item(i);
3395 if (pane.name == wxT("dummy"))
3396 continue;
3397 wxAuiTabCtrl* tabctrl = ((wxTabFrame*)pane.window)->m_tabs;
3398 if (tabctrl != ctrl)
3399 tabctrl->SetSelectedFont(m_normalFont);
3400 else
3401 tabctrl->SetSelectedFont(m_selectedFont);
3402 tabctrl->Refresh();
3403 }
3404
3405 // Set the focus to the page if we're not currently focused on the tab.
3406 // This is Firefox-like behaviour.
3407 if (wnd->IsShownOnScreen() && FindFocus() != ctrl)
3408 wnd->SetFocus();
3409
3410 return old_curpage;
3411 }
3412 }
3413
3414 return m_curPage;
3415 }
3416
3417
3418 #endif // wxUSE_AUI