Merge "selected" and "active" child in wxAuiMDIParentFrame.
[wxWidgets.git] / src / aui / tabmdi.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/aui/tabmdi.cpp
3 // Purpose: Generic MDI (Multiple Document Interface) classes
4 // Author: Hans Van Leemputten
5 // Modified by: Benjamin I. Williams / Kirix Corporation
6 // Created: 29/07/2002
7 // RCS-ID: $Id$
8 // Copyright: (c) Hans Van Leemputten
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_AUI
28 #if wxUSE_MDI
29
30 #include "wx/aui/tabmdi.h"
31
32 #ifndef WX_PRECOMP
33 #include "wx/panel.h"
34 #include "wx/menu.h"
35 #include "wx/intl.h"
36 #include "wx/log.h"
37 #include "wx/settings.h"
38 #endif //WX_PRECOMP
39
40 #include "wx/stockitem.h"
41 #include "wx/aui/dockart.h"
42
43 enum MDI_MENU_ID
44 {
45 wxWINDOWCLOSE = 4001,
46 wxWINDOWCLOSEALL,
47 wxWINDOWNEXT,
48 wxWINDOWPREV
49 };
50
51 //-----------------------------------------------------------------------------
52 // wxAuiMDIParentFrame
53 //-----------------------------------------------------------------------------
54
55 IMPLEMENT_DYNAMIC_CLASS(wxAuiMDIParentFrame, wxFrame)
56
57 BEGIN_EVENT_TABLE(wxAuiMDIParentFrame, wxFrame)
58 #if wxUSE_MENUS
59 EVT_MENU (wxID_ANY, wxAuiMDIParentFrame::DoHandleMenu)
60 EVT_UPDATE_UI (wxID_ANY, wxAuiMDIParentFrame::DoHandleUpdateUI)
61 #endif
62 END_EVENT_TABLE()
63
64 wxAuiMDIParentFrame::wxAuiMDIParentFrame()
65 {
66 Init();
67 }
68
69 wxAuiMDIParentFrame::wxAuiMDIParentFrame(wxWindow *parent,
70 wxWindowID id,
71 const wxString& title,
72 const wxPoint& pos,
73 const wxSize& size,
74 long style,
75 const wxString& name)
76 {
77 Init();
78 (void)Create(parent, id, title, pos, size, style, name);
79 }
80
81 wxAuiMDIParentFrame::~wxAuiMDIParentFrame()
82 {
83 // Avoid having GetActiveChild() called after m_pClientWindow is destroyed
84 SendDestroyEvent();
85 // Make sure the client window is destructed before the menu bars are!
86 wxDELETE(m_pClientWindow);
87
88 #if wxUSE_MENUS
89 wxDELETE(m_pMyMenuBar);
90 RemoveWindowMenu(GetMenuBar());
91 wxDELETE(m_pWindowMenu);
92 #endif // wxUSE_MENUS
93 }
94
95 bool wxAuiMDIParentFrame::Create(wxWindow *parent,
96 wxWindowID id,
97 const wxString& title,
98 const wxPoint& pos,
99 const wxSize& size,
100 long style,
101 const wxString& name)
102 {
103 #if wxUSE_MENUS
104 // this style can be used to prevent a window from having the standard MDI
105 // "Window" menu
106 if (!(style & wxFRAME_NO_WINDOW_MENU))
107 {
108 m_pWindowMenu = new wxMenu;
109 m_pWindowMenu->Append(wxWINDOWCLOSE, _("Cl&ose"));
110 m_pWindowMenu->Append(wxWINDOWCLOSEALL, _("Close All"));
111 m_pWindowMenu->AppendSeparator();
112 m_pWindowMenu->Append(wxWINDOWNEXT, _("&Next"));
113 m_pWindowMenu->Append(wxWINDOWPREV, _("&Previous"));
114 }
115 #endif // wxUSE_MENUS
116
117 if ( !wxFrame::Create(parent, id, title, pos, size, style, name) )
118 return false;
119
120 m_pClientWindow = OnCreateClient();
121 return m_pClientWindow != NULL;
122 }
123
124
125 void wxAuiMDIParentFrame::SetArtProvider(wxAuiTabArt* provider)
126 {
127 if (m_pClientWindow)
128 {
129 m_pClientWindow->SetArtProvider(provider);
130 }
131 }
132
133 wxAuiTabArt* wxAuiMDIParentFrame::GetArtProvider()
134 {
135 if (!m_pClientWindow)
136 return NULL;
137
138 return m_pClientWindow->GetArtProvider();
139 }
140
141 wxAuiNotebook* wxAuiMDIParentFrame::GetNotebook() const
142 {
143 return static_cast<wxAuiNotebook*>(m_pClientWindow);
144 }
145
146
147
148 #if wxUSE_MENUS
149 void wxAuiMDIParentFrame::SetWindowMenu(wxMenu* pMenu)
150 {
151 // Replace the window menu from the currently loaded menu bar.
152 wxMenuBar *pMenuBar = GetMenuBar();
153
154 if (m_pWindowMenu)
155 {
156 RemoveWindowMenu(pMenuBar);
157 wxDELETE(m_pWindowMenu);
158 }
159
160 if (pMenu)
161 {
162 m_pWindowMenu = pMenu;
163 AddWindowMenu(pMenuBar);
164 }
165 }
166
167 void wxAuiMDIParentFrame::SetMenuBar(wxMenuBar* pMenuBar)
168 {
169 // Remove the Window menu from the old menu bar
170 RemoveWindowMenu(GetMenuBar());
171
172 // Add the Window menu to the new menu bar.
173 AddWindowMenu(pMenuBar);
174
175 wxFrame::SetMenuBar(pMenuBar);
176 //m_pMyMenuBar = GetMenuBar();
177 }
178 #endif // wxUSE_MENUS
179
180 void wxAuiMDIParentFrame::SetChildMenuBar(wxAuiMDIChildFrame* pChild)
181 {
182 #if wxUSE_MENUS
183 if (!pChild)
184 {
185 // No Child, set Our menu bar back.
186 if (m_pMyMenuBar)
187 SetMenuBar(m_pMyMenuBar);
188 else
189 SetMenuBar(GetMenuBar());
190
191 // Make sure we know our menu bar is in use
192 m_pMyMenuBar = NULL;
193 }
194 else
195 {
196 if (pChild->GetMenuBar() == NULL)
197 return;
198
199 // Do we need to save the current bar?
200 if (m_pMyMenuBar == NULL)
201 m_pMyMenuBar = GetMenuBar();
202
203 SetMenuBar(pChild->GetMenuBar());
204 }
205 #endif // wxUSE_MENUS
206 }
207
208 bool wxAuiMDIParentFrame::ProcessEvent(wxEvent& event)
209 {
210 // stops the same event being processed repeatedly
211 if (m_pLastEvt == &event)
212 return false;
213 m_pLastEvt = &event;
214
215 // let the active child (if any) process the event first.
216 bool res = false;
217 wxAuiMDIChildFrame* pActiveChild = GetActiveChild();
218 if (pActiveChild &&
219 event.IsCommandEvent() &&
220 event.GetEventObject() != m_pClientWindow &&
221 !(event.GetEventType() == wxEVT_ACTIVATE ||
222 event.GetEventType() == wxEVT_SET_FOCUS ||
223 event.GetEventType() == wxEVT_KILL_FOCUS ||
224 event.GetEventType() == wxEVT_CHILD_FOCUS ||
225 event.GetEventType() == wxEVT_COMMAND_SET_FOCUS ||
226 event.GetEventType() == wxEVT_COMMAND_KILL_FOCUS )
227 )
228 {
229 res = pActiveChild->GetEventHandler()->ProcessEvent(event);
230 }
231
232 if (!res)
233 {
234 // if the event was not handled this frame will handle it,
235 // which is why we need the protection code at the beginning
236 // of this method
237 res = wxEvtHandler::ProcessEvent(event);
238 }
239
240 m_pLastEvt = NULL;
241
242 return res;
243 }
244
245 wxAuiMDIChildFrame *wxAuiMDIParentFrame::GetActiveChild() const
246 {
247 return GetClientWindow()->GetActiveChild();
248 }
249
250 void wxAuiMDIParentFrame::SetActiveChild(wxAuiMDIChildFrame* pChildFrame)
251 {
252 if (GetClientWindow()->GetActiveChild() != pChildFrame)
253 {
254 GetClientWindow()->SetActiveChild(pChildFrame);
255 }
256 }
257
258 wxAuiMDIClientWindow *wxAuiMDIParentFrame::GetClientWindow() const
259 {
260 return m_pClientWindow;
261 }
262
263 wxAuiMDIClientWindow *wxAuiMDIParentFrame::OnCreateClient()
264 {
265 return new wxAuiMDIClientWindow( this );
266 }
267
268 void wxAuiMDIParentFrame::ActivateNext()
269 {
270 if (m_pClientWindow && m_pClientWindow->GetSelection() != wxNOT_FOUND)
271 {
272 size_t active = m_pClientWindow->GetSelection() + 1;
273 if (active >= m_pClientWindow->GetPageCount())
274 active = 0;
275
276 m_pClientWindow->SetSelection(active);
277 }
278 }
279
280 void wxAuiMDIParentFrame::ActivatePrevious()
281 {
282 if (m_pClientWindow && m_pClientWindow->GetSelection() != wxNOT_FOUND)
283 {
284 int active = m_pClientWindow->GetSelection() - 1;
285 if (active < 0)
286 active = m_pClientWindow->GetPageCount() - 1;
287
288 m_pClientWindow->SetSelection(active);
289 }
290 }
291
292 void wxAuiMDIParentFrame::Init()
293 {
294 m_pLastEvt = NULL;
295 m_pClientWindow = NULL;
296 #if wxUSE_MENUS
297 m_pWindowMenu = NULL;
298 m_pMyMenuBar = NULL;
299 #endif // wxUSE_MENUS
300 }
301
302 #if wxUSE_MENUS
303 void wxAuiMDIParentFrame::RemoveWindowMenu(wxMenuBar* pMenuBar)
304 {
305 if (pMenuBar && m_pWindowMenu)
306 {
307 // Remove old window menu
308 int pos = pMenuBar->FindMenu(_("&Window"));
309 if (pos != wxNOT_FOUND)
310 {
311 // DBG:: We're going to delete the wrong menu!!!
312 wxASSERT(m_pWindowMenu == pMenuBar->GetMenu(pos));
313 pMenuBar->Remove(pos);
314 }
315 }
316 }
317
318 void wxAuiMDIParentFrame::AddWindowMenu(wxMenuBar *pMenuBar)
319 {
320 if (pMenuBar && m_pWindowMenu)
321 {
322 int pos = pMenuBar->FindMenu(wxGetStockLabel(wxID_HELP,wxSTOCK_NOFLAGS));
323 if (pos == wxNOT_FOUND)
324 pMenuBar->Append(m_pWindowMenu, _("&Window"));
325 else
326 pMenuBar->Insert(pos, m_pWindowMenu, _("&Window"));
327 }
328 }
329
330 void wxAuiMDIParentFrame::DoHandleMenu(wxCommandEvent& event)
331 {
332 switch (event.GetId())
333 {
334 case wxWINDOWCLOSE:
335 {
336 wxAuiMDIChildFrame* pActiveChild = GetActiveChild();
337 if (pActiveChild)
338 pActiveChild->Close();
339 break;
340 }
341 case wxWINDOWCLOSEALL:
342 {
343 wxAuiMDIChildFrame* pActiveChild;
344 while ((pActiveChild = GetActiveChild()) != NULL)
345 {
346 if (!pActiveChild->Close())
347 {
348 return; // failure
349 }
350 }
351 break;
352 }
353 case wxWINDOWNEXT:
354 ActivateNext();
355 break;
356 case wxWINDOWPREV:
357 ActivatePrevious();
358 break;
359 default:
360 event.Skip();
361 }
362 }
363
364 void wxAuiMDIParentFrame::DoHandleUpdateUI(wxUpdateUIEvent& event)
365 {
366 switch (event.GetId())
367 {
368 case wxWINDOWCLOSE:
369 case wxWINDOWCLOSEALL:
370 {
371 wxAuiMDIClientWindow* client_window = GetClientWindow();
372 wxCHECK_RET(client_window, wxS("Missing MDI Client Window"));
373 size_t pages = client_window->GetPageCount();
374 event.Enable(pages >= 1);
375 break;
376 }
377
378 case wxWINDOWNEXT:
379 case wxWINDOWPREV:
380 {
381 wxAuiMDIClientWindow* client_window = GetClientWindow();
382 wxCHECK_RET(client_window, wxS("Missing MDI Client Window"));
383 size_t pages = client_window->GetPageCount();
384 event.Enable(pages >= 2);
385 break;
386 }
387
388 default:
389 event.Skip();
390 }
391 }
392 #endif // wxUSE_MENUS
393
394 void wxAuiMDIParentFrame::DoGetClientSize(int* width, int* height) const
395 {
396 wxFrame::DoGetClientSize(width, height);
397 }
398
399 void wxAuiMDIParentFrame::Tile(wxOrientation orient)
400 {
401 wxAuiMDIClientWindow* client_window = GetClientWindow();
402 wxASSERT_MSG(client_window, wxT("Missing MDI Client Window"));
403
404 int cur_idx = client_window->GetSelection();
405 if (cur_idx == -1)
406 return;
407
408 if (orient == wxVERTICAL)
409 {
410 client_window->Split(cur_idx, wxLEFT);
411 }
412 else if (orient == wxHORIZONTAL)
413 {
414 client_window->Split(cur_idx, wxTOP);
415 }
416 }
417
418
419 //-----------------------------------------------------------------------------
420 // wxAuiMDIChildFrame
421 //-----------------------------------------------------------------------------
422
423 IMPLEMENT_DYNAMIC_CLASS(wxAuiMDIChildFrame, wxPanel)
424
425 BEGIN_EVENT_TABLE(wxAuiMDIChildFrame, wxPanel)
426 EVT_MENU_HIGHLIGHT_ALL(wxAuiMDIChildFrame::OnMenuHighlight)
427 EVT_ACTIVATE(wxAuiMDIChildFrame::OnActivate)
428 EVT_CLOSE(wxAuiMDIChildFrame::OnCloseWindow)
429 END_EVENT_TABLE()
430
431 wxAuiMDIChildFrame::wxAuiMDIChildFrame()
432 {
433 Init();
434 }
435
436 wxAuiMDIChildFrame::wxAuiMDIChildFrame(wxAuiMDIParentFrame *parent,
437 wxWindowID id,
438 const wxString& title,
439 const wxPoint& WXUNUSED(pos),
440 const wxSize& size,
441 long style,
442 const wxString& name)
443 {
444 Init();
445
446 // There are two ways to create an tabbed mdi child fram without
447 // making it the active document. Either Show(false) can be called
448 // before Create() (as is customary on some ports with wxFrame-type
449 // windows), or wxMINIMIZE can be passed in the style flags. Note that
450 // wxAuiMDIChildFrame is not really derived from wxFrame, as wxMDIChildFrame
451 // is, but those are the expected symantics. No style flag is passed
452 // onto the panel underneath.
453 if (style & wxMINIMIZE)
454 m_activateOnCreate = false;
455
456 Create(parent, id, title, wxDefaultPosition, size, 0, name);
457 }
458
459 wxAuiMDIChildFrame::~wxAuiMDIChildFrame()
460 {
461 wxAuiMDIParentFrame* pParentFrame = GetMDIParentFrame();
462 if (pParentFrame)
463 {
464 if (pParentFrame->GetActiveChild() == this)
465 {
466 pParentFrame->SetActiveChild(NULL);
467 pParentFrame->SetChildMenuBar(NULL);
468 }
469 wxAuiMDIClientWindow* pClientWindow = pParentFrame->GetClientWindow();
470 wxASSERT(pClientWindow);
471 int idx = pClientWindow->GetPageIndex(this);
472 if (idx != wxNOT_FOUND)
473 {
474 pClientWindow->RemovePage(idx);
475 }
476 }
477
478 #if wxUSE_MENUS
479 wxDELETE(m_pMenuBar);
480 #endif // wxUSE_MENUS
481 }
482
483 bool wxAuiMDIChildFrame::Create(wxAuiMDIParentFrame* parent,
484 wxWindowID id,
485 const wxString& title,
486 const wxPoint& WXUNUSED(pos),
487 const wxSize& size,
488 long style,
489 const wxString& name)
490 {
491 wxAuiMDIClientWindow* pClientWindow = parent->GetClientWindow();
492 wxASSERT_MSG((pClientWindow != NULL), wxT("Missing MDI client window."));
493
494 // see comment in constructor
495 if (style & wxMINIMIZE)
496 m_activateOnCreate = false;
497
498 wxSize cli_size = pClientWindow->GetClientSize();
499
500 // create the window off-screen to prevent flicker
501 wxPanel::Create(pClientWindow,
502 id,
503 wxPoint(cli_size.x+1, cli_size.y+1),
504 size,
505 wxNO_BORDER, name);
506
507 DoShow(false);
508
509 SetMDIParentFrame(parent);
510
511 m_title = title;
512
513 pClientWindow->AddPage(this, title, m_activateOnCreate);
514
515 // Check that the parent notion of the active child coincides with our one.
516 // This is less obvious that it seems because we must honour
517 // m_activateOnCreate flag but only if it's not the first child because
518 // this one becomes active unconditionally.
519 wxASSERT_MSG
520 (
521 (m_activateOnCreate || pClientWindow->GetPageCount() == 1)
522 == (parent->GetActiveChild() == this),
523 wxS("Logic error: child [not] activated when it should [not] have been.")
524 );
525
526 pClientWindow->Refresh();
527
528 return true;
529 }
530
531 bool wxAuiMDIChildFrame::Destroy()
532 {
533 wxAuiMDIParentFrame* pParentFrame = GetMDIParentFrame();
534 wxASSERT_MSG(pParentFrame, wxT("Missing MDI Parent Frame"));
535
536 wxAuiMDIClientWindow* pClientWindow = pParentFrame->GetClientWindow();
537 wxASSERT_MSG(pClientWindow, wxT("Missing MDI Client Window"));
538
539 if (pParentFrame->GetActiveChild() == this)
540 {
541 // deactivate ourself
542 wxActivateEvent event(wxEVT_ACTIVATE, false, GetId());
543 event.SetEventObject(this);
544 GetEventHandler()->ProcessEvent(event);
545
546 pParentFrame->SetChildMenuBar(NULL);
547 }
548
549 size_t page_count = pClientWindow->GetPageCount();
550 for (size_t pos = 0; pos < page_count; pos++)
551 {
552 if (pClientWindow->GetPage(pos) == this)
553 return pClientWindow->DeletePage(pos);
554 }
555
556 return false;
557 }
558
559 #if wxUSE_MENUS
560 void wxAuiMDIChildFrame::SetMenuBar(wxMenuBar *menu_bar)
561 {
562 wxMenuBar *pOldMenuBar = m_pMenuBar;
563 m_pMenuBar = menu_bar;
564
565 if (m_pMenuBar)
566 {
567 wxAuiMDIParentFrame* pParentFrame = GetMDIParentFrame();
568 wxASSERT_MSG(pParentFrame, wxT("Missing MDI Parent Frame"));
569
570 m_pMenuBar->SetParent(pParentFrame);
571 if (pParentFrame->GetActiveChild() == this)
572 {
573 // replace current menu bars
574 if (pOldMenuBar)
575 pParentFrame->SetChildMenuBar(NULL);
576 pParentFrame->SetChildMenuBar(this);
577 }
578 }
579 }
580
581 wxMenuBar *wxAuiMDIChildFrame::GetMenuBar() const
582 {
583 return m_pMenuBar;
584 }
585 #endif // wxUSE_MENUS
586
587 void wxAuiMDIChildFrame::SetTitle(const wxString& title)
588 {
589 m_title = title;
590
591 wxAuiMDIParentFrame* pParentFrame = GetMDIParentFrame();
592 wxASSERT_MSG(pParentFrame, wxT("Missing MDI Parent Frame"));
593
594 wxAuiMDIClientWindow* pClientWindow = pParentFrame->GetClientWindow();
595 if (pClientWindow != NULL)
596 {
597 size_t pos;
598 for (pos = 0; pos < pClientWindow->GetPageCount(); pos++)
599 {
600 if (pClientWindow->GetPage(pos) == this)
601 {
602 pClientWindow->SetPageText(pos, m_title);
603 break;
604 }
605 }
606 }
607 }
608
609 wxString wxAuiMDIChildFrame::GetTitle() const
610 {
611 return m_title;
612 }
613
614 void wxAuiMDIChildFrame::SetIcons(const wxIconBundle& icons)
615 {
616 // get icon with the system icon size
617 SetIcon(icons.GetIcon(-1));
618 m_iconBundle = icons;
619 }
620
621 const wxIconBundle& wxAuiMDIChildFrame::GetIcons() const
622 {
623 return m_iconBundle;
624 }
625
626 void wxAuiMDIChildFrame::SetIcon(const wxIcon& icon)
627 {
628 wxAuiMDIParentFrame* pParentFrame = GetMDIParentFrame();
629 wxASSERT_MSG(pParentFrame, wxT("Missing MDI Parent Frame"));
630
631 m_icon = icon;
632
633 wxBitmap bmp;
634 bmp.CopyFromIcon(m_icon);
635
636 wxAuiMDIClientWindow* pClientWindow = pParentFrame->GetClientWindow();
637 if (pClientWindow != NULL)
638 {
639 int idx = pClientWindow->GetPageIndex(this);
640
641 if (idx != -1)
642 {
643 pClientWindow->SetPageBitmap((size_t)idx, bmp);
644 }
645 }
646 }
647
648 const wxIcon& wxAuiMDIChildFrame::GetIcon() const
649 {
650 return m_icon;
651 }
652
653
654 void wxAuiMDIChildFrame::Activate()
655 {
656 wxAuiMDIParentFrame* pParentFrame = GetMDIParentFrame();
657 wxASSERT_MSG(pParentFrame, wxT("Missing MDI Parent Frame"));
658
659 wxAuiMDIClientWindow* pClientWindow = pParentFrame->GetClientWindow();
660
661 if (pClientWindow != NULL)
662 {
663 size_t pos;
664 for (pos = 0; pos < pClientWindow->GetPageCount(); pos++)
665 {
666 if (pClientWindow->GetPage(pos) == this)
667 {
668 pClientWindow->SetSelection(pos);
669 break;
670 }
671 }
672 }
673 }
674
675 void wxAuiMDIChildFrame::OnMenuHighlight(wxMenuEvent& event)
676 {
677 #if wxUSE_STATUSBAR
678 if (m_pMDIParentFrame)
679 {
680 // we don't have any help text for this item,
681 // but may be the MDI frame does?
682 m_pMDIParentFrame->OnMenuHighlight(event);
683 }
684 #else
685 wxUnusedVar(event);
686 #endif // wxUSE_STATUSBAR
687 }
688
689 void wxAuiMDIChildFrame::OnActivate(wxActivateEvent& WXUNUSED(event))
690 {
691 // do nothing
692 }
693
694 void wxAuiMDIChildFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
695 {
696 Destroy();
697 }
698
699 void wxAuiMDIChildFrame::SetMDIParentFrame(wxAuiMDIParentFrame* parentFrame)
700 {
701 m_pMDIParentFrame = parentFrame;
702 }
703
704 wxAuiMDIParentFrame* wxAuiMDIChildFrame::GetMDIParentFrame() const
705 {
706 return m_pMDIParentFrame;
707 }
708
709 void wxAuiMDIChildFrame::Init()
710 {
711 m_activateOnCreate = true;
712 m_pMDIParentFrame = NULL;
713 #if wxUSE_MENUS
714 m_pMenuBar = NULL;
715 #endif // wxUSE_MENUS
716 }
717
718 bool wxAuiMDIChildFrame::Show(bool show)
719 {
720 // wxAuiMDIChildFrame uses m_activateOnCreate only to decide whether to
721 // activate the frame when it is created. After Create() is called,
722 // m_activateOnCreate will never be read again. Therefore, calling this
723 // function after Create() is pointless and you probably want to call
724 // Activate() instead.
725 wxCHECK_MSG( !GetHandle(), false,
726 wxS("Show() has no effect after Create(). Do you mean Activate()?") );
727
728 m_activateOnCreate = show;
729
730 // do nothing
731 return true;
732 }
733
734 void wxAuiMDIChildFrame::DoShow(bool show)
735 {
736 wxWindow::Show(show);
737 }
738
739 void wxAuiMDIChildFrame::DoSetSize(int x, int y, int width, int height, int sizeFlags)
740 {
741 m_mdiNewRect = wxRect(x, y, width, height);
742 #ifdef __WXGTK__
743 wxPanel::DoSetSize(x,y,width, height, sizeFlags);
744 #else
745 wxUnusedVar(sizeFlags);
746 #endif
747 }
748
749 void wxAuiMDIChildFrame::DoMoveWindow(int x, int y, int width, int height)
750 {
751 m_mdiNewRect = wxRect(x, y, width, height);
752 }
753
754 void wxAuiMDIChildFrame::ApplyMDIChildFrameRect()
755 {
756 if (m_mdiCurRect != m_mdiNewRect)
757 {
758 wxPanel::DoMoveWindow(m_mdiNewRect.x, m_mdiNewRect.y,
759 m_mdiNewRect.width, m_mdiNewRect.height);
760 m_mdiCurRect = m_mdiNewRect;
761 }
762 }
763
764
765 //-----------------------------------------------------------------------------
766 // wxAuiMDIClientWindow
767 //-----------------------------------------------------------------------------
768
769 IMPLEMENT_DYNAMIC_CLASS(wxAuiMDIClientWindow, wxAuiNotebook)
770
771 BEGIN_EVENT_TABLE(wxAuiMDIClientWindow, wxAuiNotebook)
772 EVT_AUINOTEBOOK_PAGE_CHANGED(wxID_ANY, wxAuiMDIClientWindow::OnPageChanged)
773 EVT_AUINOTEBOOK_PAGE_CLOSE(wxID_ANY, wxAuiMDIClientWindow::OnPageClose)
774 EVT_SIZE(wxAuiMDIClientWindow::OnSize)
775 END_EVENT_TABLE()
776
777 wxAuiMDIClientWindow::wxAuiMDIClientWindow()
778 {
779 }
780
781 wxAuiMDIClientWindow::wxAuiMDIClientWindow(wxAuiMDIParentFrame* parent, long style)
782 {
783 CreateClient(parent, style);
784 }
785
786 bool wxAuiMDIClientWindow::CreateClient(wxAuiMDIParentFrame* parent, long style)
787 {
788 SetWindowStyleFlag(style);
789
790 wxSize caption_icon_size =
791 wxSize(wxSystemSettings::GetMetric(wxSYS_SMALLICON_X),
792 wxSystemSettings::GetMetric(wxSYS_SMALLICON_Y));
793 SetUniformBitmapSize(caption_icon_size);
794
795 if (!wxAuiNotebook::Create(parent,
796 wxID_ANY,
797 wxPoint(0,0),
798 wxSize(100, 100),
799 wxAUI_NB_DEFAULT_STYLE | wxNO_BORDER))
800 {
801 return false;
802 }
803
804 wxColour bkcolour = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
805 SetOwnBackgroundColour(bkcolour);
806
807 m_mgr.GetArtProvider()->SetColour(wxAUI_DOCKART_BACKGROUND_COLOUR, bkcolour);
808
809 return true;
810 }
811
812 int wxAuiMDIClientWindow::SetSelection(size_t nPage)
813 {
814 return wxAuiNotebook::SetSelection(nPage);
815 }
816
817 wxAuiMDIChildFrame* wxAuiMDIClientWindow::GetActiveChild()
818 {
819 const int sel = GetSelection();
820 if ( sel == wxNOT_FOUND )
821 return NULL;
822
823 return wxStaticCast(GetPage(sel), wxAuiMDIChildFrame);
824 }
825
826 void wxAuiMDIClientWindow::PageChanged(int old_selection, int new_selection)
827 {
828 // don't do anything if the page doesn't actually change
829 if (old_selection == new_selection)
830 return;
831
832 /*
833 // don't do anything if the new page is already active
834 if (new_selection != -1)
835 {
836 wxAuiMDIChildFrame* child = (wxAuiMDIChildFrame*)GetPage(new_selection);
837 if (child->GetMDIParentFrame()->GetActiveChild() == child)
838 return;
839 }*/
840
841
842 // notify old active child that it has been deactivated
843 if ((old_selection != -1) && (old_selection < (int)GetPageCount()))
844 {
845 wxAuiMDIChildFrame* old_child = (wxAuiMDIChildFrame*)GetPage(old_selection);
846 wxASSERT_MSG(old_child, wxT("wxAuiMDIClientWindow::PageChanged - null page pointer"));
847
848 wxActivateEvent event(wxEVT_ACTIVATE, false, old_child->GetId());
849 event.SetEventObject(old_child);
850 old_child->GetEventHandler()->ProcessEvent(event);
851 }
852
853 // notify new active child that it has been activated
854 if (new_selection != -1)
855 {
856 wxAuiMDIChildFrame* active_child = (wxAuiMDIChildFrame*)GetPage(new_selection);
857 wxASSERT_MSG(active_child, wxT("wxAuiMDIClientWindow::PageChanged - null page pointer"));
858
859 wxActivateEvent event(wxEVT_ACTIVATE, true, active_child->GetId());
860 event.SetEventObject(active_child);
861 active_child->GetEventHandler()->ProcessEvent(event);
862
863 if (active_child->GetMDIParentFrame())
864 {
865 active_child->GetMDIParentFrame()->SetActiveChild(active_child);
866 active_child->GetMDIParentFrame()->SetChildMenuBar(active_child);
867 }
868 }
869
870
871 }
872
873 void wxAuiMDIClientWindow::OnPageClose(wxAuiNotebookEvent& evt)
874 {
875 wxAuiMDIChildFrame* wnd;
876 wnd = static_cast<wxAuiMDIChildFrame*>(GetPage(evt.GetSelection()));
877
878 wnd->Close();
879
880 // regardless of the result of wnd->Close(), we've
881 // already taken care of the close operations, so
882 // suppress further processing
883 evt.Veto();
884 }
885
886 void wxAuiMDIClientWindow::OnPageChanged(wxAuiNotebookEvent& evt)
887 {
888 PageChanged(evt.GetOldSelection(), evt.GetSelection());
889 }
890
891 void wxAuiMDIClientWindow::OnSize(wxSizeEvent& evt)
892 {
893 wxAuiNotebook::OnSize(evt);
894
895 for (size_t pos = 0; pos < GetPageCount(); pos++)
896 ((wxAuiMDIChildFrame *)GetPage(pos))->ApplyMDIChildFrameRect();
897 }
898
899 #endif //wxUSE_AUI
900 #endif // wxUSE_MDI