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