]> git.saurik.com Git - wxWidgets.git/blob - src/aui/tabmdi.cpp
added notebook.cpp and tabmdi.cpp
[wxWidgets.git] / src / aui / tabmdi.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/mdig.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_MDI
28
29 #include "wx/aui/tabmdi.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/panel.h"
33 #include "wx/menu.h"
34 #include "wx/intl.h"
35 #include "wx/log.h"
36 #endif //WX_PRECOMP
37
38 #include "wx/stockitem.h"
39
40 enum MDI_MENU_ID
41 {
42 wxWINDOWCLOSE = 4001,
43 wxWINDOWCLOSEALL,
44 wxWINDOWNEXT,
45 wxWINDOWPREV
46 };
47
48 //-----------------------------------------------------------------------------
49 // wxTabMDIParentFrame
50 //-----------------------------------------------------------------------------
51
52 IMPLEMENT_DYNAMIC_CLASS(wxTabMDIParentFrame, wxFrame)
53
54 BEGIN_EVENT_TABLE(wxTabMDIParentFrame, wxFrame)
55 #if wxUSE_MENUS
56 EVT_MENU (wxID_ANY, wxTabMDIParentFrame::DoHandleMenu)
57 #endif
58 END_EVENT_TABLE()
59
60 wxTabMDIParentFrame::wxTabMDIParentFrame()
61 {
62 Init();
63 }
64
65 wxTabMDIParentFrame::wxTabMDIParentFrame(wxWindow *parent,
66 wxWindowID id,
67 const wxString& title,
68 const wxPoint& pos,
69 const wxSize& size,
70 long style,
71 const wxString& name)
72 {
73 Init();
74 (void)Create(parent, id, title, pos, size, style, name);
75 }
76
77 wxTabMDIParentFrame::~wxTabMDIParentFrame()
78 {
79 // Make sure the client window is destructed before the menu bars are!
80 wxDELETE(m_pClientWindow);
81
82 #if wxUSE_MENUS
83 RemoveWindowMenu(GetMenuBar());
84 delete m_pWindowMenu;
85 #endif // wxUSE_MENUS
86 }
87
88 bool wxTabMDIParentFrame::Create(wxWindow *parent,
89 wxWindowID id,
90 const wxString& title,
91 const wxPoint& pos,
92 const wxSize& size,
93 long style,
94 const wxString& name)
95 {
96 #if wxUSE_MENUS
97 // this style can be used to prevent a window from having the standard MDI
98 // "Window" menu
99 if (!(style & wxFRAME_NO_WINDOW_MENU))
100 {
101 m_pWindowMenu = new wxMenu;
102 m_pWindowMenu->Append(wxWINDOWCLOSE, _("Cl&ose"));
103 m_pWindowMenu->Append(wxWINDOWCLOSEALL, _("Close All"));
104 m_pWindowMenu->AppendSeparator();
105 m_pWindowMenu->Append(wxWINDOWNEXT, _("&Next"));
106 m_pWindowMenu->Append(wxWINDOWPREV, _("&Previous"));
107 }
108 #endif // wxUSE_MENUS
109
110 wxFrame::Create(parent, id, title, pos, size, style, name);
111 OnCreateClient();
112 return true;
113 }
114
115 #if wxUSE_MENUS
116 void wxTabMDIParentFrame::SetWindowMenu(wxMenu* pMenu)
117 {
118 // Replace the window menu from the currently loaded menu bar.
119 wxMenuBar *pMenuBar = GetMenuBar();
120
121 if (m_pWindowMenu)
122 {
123 RemoveWindowMenu(pMenuBar);
124 wxDELETE(m_pWindowMenu);
125 }
126
127 if (pMenu)
128 {
129 m_pWindowMenu = pMenu;
130 AddWindowMenu(pMenuBar);
131 }
132 }
133
134 void wxTabMDIParentFrame::SetMenuBar(wxMenuBar *pMenuBar)
135 {
136 // Remove the Window menu from the old menu bar
137 RemoveWindowMenu(GetMenuBar());
138
139 // Add the Window menu to the new menu bar.
140 AddWindowMenu(pMenuBar);
141
142 wxFrame::SetMenuBar(pMenuBar);
143 m_pMyMenuBar = GetMenuBar();
144 }
145 #endif // wxUSE_MENUS
146
147 void wxTabMDIParentFrame::SetChildMenuBar(wxTabMDIChildFrame* pChild)
148 {
149 #if wxUSE_MENUS
150 if (!pChild)
151 {
152 // No Child, set Our menu bar back.
153 SetMenuBar(m_pMyMenuBar);
154
155 // Make sure we know our menu bar is in use
156 m_pMyMenuBar = NULL;
157 }
158 else
159 {
160 if (pChild->GetMenuBar() == NULL)
161 return;
162
163 // Do we need to save the current bar?
164 if (m_pMyMenuBar == NULL)
165 m_pMyMenuBar = GetMenuBar();
166
167 SetMenuBar(pChild->GetMenuBar());
168 }
169 #endif // wxUSE_MENUS
170 }
171
172 bool wxTabMDIParentFrame::ProcessEvent(wxEvent& event)
173 {
174 // Stops the same event being processed repeatedly
175 static wxEventType inEvent = wxEVT_NULL;
176 if (inEvent == event.GetEventType())
177 return false;
178
179 inEvent = event.GetEventType();
180
181 // Let the active child (if any) process the event first.
182 bool res = false;
183 if (m_pActiveChild &&
184 event.IsCommandEvent() &&
185 event.GetEventObject() != m_pClientWindow &&
186 !(event.GetEventType() == wxEVT_ACTIVATE ||
187 event.GetEventType() == wxEVT_SET_FOCUS ||
188 event.GetEventType() == wxEVT_KILL_FOCUS ||
189 event.GetEventType() == wxEVT_CHILD_FOCUS ||
190 event.GetEventType() == wxEVT_COMMAND_SET_FOCUS ||
191 event.GetEventType() == wxEVT_COMMAND_KILL_FOCUS )
192 )
193 {
194 res = m_pActiveChild->GetEventHandler()->ProcessEvent(event);
195 }
196
197 // If the event was not handled this frame will handle it!
198 if (!res)
199 {
200 //res = GetEventHandler()->ProcessEvent(event);
201 res = wxEvtHandler::ProcessEvent(event);
202 }
203
204 inEvent = wxEVT_NULL;
205
206 return res;
207 }
208
209 wxTabMDIChildFrame *wxTabMDIParentFrame::GetActiveChild() const
210 {
211 return m_pActiveChild;
212 }
213
214 void wxTabMDIParentFrame::SetActiveChild(wxTabMDIChildFrame* pChildFrame)
215 {
216 m_pActiveChild = pChildFrame;
217 }
218
219 wxTabMDIClientWindow *wxTabMDIParentFrame::GetClientWindow() const
220 {
221 return m_pClientWindow;
222 }
223
224 wxTabMDIClientWindow *wxTabMDIParentFrame::OnCreateClient()
225 {
226 m_pClientWindow = new wxTabMDIClientWindow( this );
227 return m_pClientWindow;
228 }
229
230 void wxTabMDIParentFrame::ActivateNext()
231 {
232 if (m_pClientWindow && m_pClientWindow->GetSelection() != -1)
233 {
234 size_t active = m_pClientWindow->GetSelection() + 1;
235 if (active >= m_pClientWindow->GetPageCount())
236 active = 0;
237
238 m_pClientWindow->SetSelection(active);
239 }
240 }
241
242 void wxTabMDIParentFrame::ActivatePrevious()
243 {
244 if (m_pClientWindow && m_pClientWindow->GetSelection() != -1)
245 {
246 int active = m_pClientWindow->GetSelection() - 1;
247 if (active < 0)
248 active = m_pClientWindow->GetPageCount() - 1;
249
250 m_pClientWindow->SetSelection(active);
251 }
252 }
253
254 void wxTabMDIParentFrame::Init()
255 {
256 m_pClientWindow = NULL;
257 m_pActiveChild = NULL;
258 #if wxUSE_MENUS
259 m_pWindowMenu = NULL;
260 m_pMyMenuBar = NULL;
261 #endif // wxUSE_MENUS
262 }
263
264 #if wxUSE_MENUS
265 void wxTabMDIParentFrame::RemoveWindowMenu(wxMenuBar* pMenuBar)
266 {
267 if (pMenuBar && m_pWindowMenu)
268 {
269 // Remove old window menu
270 int pos = pMenuBar->FindMenu(_("&Window"));
271 if (pos != wxNOT_FOUND)
272 {
273 // DBG:: We're going to delete the wrong menu!!!
274 wxASSERT(m_pWindowMenu == pMenuBar->GetMenu(pos));
275 pMenuBar->Remove(pos);
276 }
277 }
278 }
279
280 void wxTabMDIParentFrame::AddWindowMenu(wxMenuBar *pMenuBar)
281 {
282 if (pMenuBar && m_pWindowMenu)
283 {
284 int pos = pMenuBar->FindMenu(wxGetStockLabel(wxID_HELP,false));
285 if (pos == wxNOT_FOUND)
286 pMenuBar->Append(m_pWindowMenu, _("&Window"));
287 else
288 pMenuBar->Insert(pos, m_pWindowMenu, _("&Window"));
289 }
290 }
291
292 void wxTabMDIParentFrame::DoHandleMenu(wxCommandEvent& event)
293 {
294 switch (event.GetId())
295 {
296 case wxWINDOWCLOSE:
297 if (m_pActiveChild)
298 m_pActiveChild->Close();
299 break;
300 case wxWINDOWCLOSEALL:
301 while (m_pActiveChild)
302 {
303 if (!m_pActiveChild->Close())
304 {
305 return; // failure
306 }
307 else
308 {
309 delete m_pActiveChild;
310 m_pActiveChild = NULL;
311 }
312 }
313 break;
314 case wxWINDOWNEXT:
315 ActivateNext();
316 break;
317 case wxWINDOWPREV:
318 ActivatePrevious();
319 break;
320 default:
321 event.Skip();
322 }
323 }
324 #endif // wxUSE_MENUS
325
326 void wxTabMDIParentFrame::DoGetClientSize(int* width, int* height) const
327 {
328 wxFrame::DoGetClientSize(width, height);
329 }
330
331 //-----------------------------------------------------------------------------
332 // wxTabMDIChildFrame
333 //-----------------------------------------------------------------------------
334
335 IMPLEMENT_DYNAMIC_CLASS(wxTabMDIChildFrame, wxPanel)
336
337 BEGIN_EVENT_TABLE(wxTabMDIChildFrame, wxPanel)
338 EVT_MENU_HIGHLIGHT_ALL(wxTabMDIChildFrame::OnMenuHighlight)
339 EVT_ACTIVATE(wxTabMDIChildFrame::OnActivate)
340 EVT_CLOSE(wxTabMDIChildFrame::OnCloseWindow)
341 END_EVENT_TABLE()
342
343 wxTabMDIChildFrame::wxTabMDIChildFrame()
344 {
345 Init();
346 }
347
348 wxTabMDIChildFrame::wxTabMDIChildFrame(wxTabMDIParentFrame *parent,
349 wxWindowID id,
350 const wxString& title,
351 const wxPoint& WXUNUSED(pos),
352 const wxSize& size,
353 long style,
354 const wxString& name)
355 {
356 Init();
357 Create(parent, id, title, wxDefaultPosition, size, style, name);
358 }
359
360 wxTabMDIChildFrame::~wxTabMDIChildFrame()
361 {
362 #if wxUSE_MENUS
363 wxDELETE(m_pMenuBar);
364 #endif // wxUSE_MENUS
365 }
366
367 bool wxTabMDIChildFrame::Create(wxTabMDIParentFrame* parent,
368 wxWindowID id,
369 const wxString& title,
370 const wxPoint& WXUNUSED(pos),
371 const wxSize& size,
372 long style,
373 const wxString& name)
374 {
375 wxTabMDIClientWindow* pClientWindow = parent->GetClientWindow();
376 wxASSERT_MSG((pClientWindow != (wxWindow*) NULL), wxT("Missing MDI client window."));
377
378 wxPanel::Create(pClientWindow, id, wxDefaultPosition, size, style|wxNO_BORDER, name);
379
380 SetMDIParentFrame(parent);
381
382 // this is the currently active child
383 parent->SetActiveChild(this);
384
385 m_title = title;
386
387 pClientWindow->AddPage(this, title, true);
388 pClientWindow->Refresh();
389
390 return true;
391 }
392
393 bool wxTabMDIChildFrame::Destroy()
394 {
395 wxTabMDIParentFrame* pParentFrame = GetMDIParentFrame();
396 wxASSERT_MSG(pParentFrame, wxT("Missing MDI Parent Frame"));
397
398 wxTabMDIClientWindow* pClientWindow = pParentFrame->GetClientWindow();
399 wxASSERT_MSG(pClientWindow, wxT("Missing MDI Client Window"));
400
401 bool bActive = false;
402 if (pParentFrame->GetActiveChild() == this)
403 {
404 pParentFrame->SetActiveChild(NULL);
405 pParentFrame->SetChildMenuBar(NULL);
406 bActive = true;
407 }
408
409 size_t pos, page_count = pClientWindow->GetPageCount();
410 for (pos = 0; pos < page_count; pos++)
411 {
412 if (pClientWindow->GetPage(pos) == this)
413 return pClientWindow->DeletePage(pos);
414 }
415
416 return false;
417 }
418
419 /*
420 wxTabMDIParentFrame* pParentFrame = GetMDIParentFrame();
421 wxASSERT_MSG(pParentFrame, wxT("Missing MDI Parent Frame"));
422
423 bool bActive = false;
424 if (pParentFrame->GetActiveChild() == this)
425 {
426 pParentFrame->SetActiveChild(NULL);
427 pParentFrame->SetChildMenuBar(NULL);
428 bActive = true;
429 }
430
431 wxTabMDIClientWindow* pClientWindow = pParentFrame->GetClientWindow();
432
433 // remove page if it is still there
434 size_t pos;
435 for (pos = 0; pos < pClientWindow->GetPageCount(); pos++)
436 {
437 if (pClientWindow->GetPage(pos) == this)
438 {
439 if (pClientWindow->RemovePage(pos))
440 pClientWindow->Refresh();
441 break;
442 }
443 }
444
445 if (bActive)
446 {
447 // Set the new selection to the a remaining page
448 if (pos < pClientWindow->GetPageCount())
449 {
450 pClientWindow->SetSelection(pos);
451 }
452 else
453 {
454 if (pClientWindow->GetPageCount() >= 1)
455 pClientWindow->SetSelection(pClientWindow->GetPageCount() - 1);
456 }
457 }
458
459 // delete the child frame with pending delete, as is
460 // customary with frame windows
461 if (!wxPendingDelete.Member(this))
462 wxPendingDelete.Append(this);
463
464 return true;
465 */
466
467
468 #if wxUSE_MENUS
469 void wxTabMDIChildFrame::SetMenuBar(wxMenuBar *menu_bar)
470 {
471 wxMenuBar *pOldMenuBar = m_pMenuBar;
472 m_pMenuBar = menu_bar;
473
474 if (m_pMenuBar)
475 {
476 wxTabMDIParentFrame* pParentFrame = GetMDIParentFrame();
477 wxASSERT_MSG(pParentFrame, wxT("Missing MDI Parent Frame"));
478
479 m_pMenuBar->SetParent(pParentFrame);
480 if (pParentFrame->GetActiveChild() == this)
481 {
482 // replace current menu bars
483 if (pOldMenuBar)
484 pParentFrame->SetChildMenuBar(NULL);
485 pParentFrame->SetChildMenuBar(this);
486 }
487 }
488 }
489
490 wxMenuBar *wxTabMDIChildFrame::GetMenuBar() const
491 {
492 return m_pMenuBar;
493 }
494 #endif // wxUSE_MENUS
495
496 void wxTabMDIChildFrame::SetTitle(const wxString& title)
497 {
498 m_title = title;
499
500 wxTabMDIParentFrame* pParentFrame = GetMDIParentFrame();
501 wxASSERT_MSG(pParentFrame, wxT("Missing MDI Parent Frame"));
502
503 wxTabMDIClientWindow* pClientWindow = pParentFrame->GetClientWindow();
504 if (pClientWindow != NULL)
505 {
506 size_t pos;
507 for (pos = 0; pos < pClientWindow->GetPageCount(); pos++)
508 {
509 if (pClientWindow->GetPage(pos) == this)
510 {
511 pClientWindow->SetPageText(pos, m_title);
512 break;
513 }
514 }
515 }
516 }
517
518 wxString wxTabMDIChildFrame::GetTitle() const
519 {
520 return m_title;
521 }
522
523 void wxTabMDIChildFrame::Activate()
524 {
525 wxTabMDIParentFrame* pParentFrame = GetMDIParentFrame();
526 wxASSERT_MSG(pParentFrame, wxT("Missing MDI Parent Frame"));
527
528 wxTabMDIClientWindow* pClientWindow = pParentFrame->GetClientWindow();
529
530 if (pClientWindow != NULL)
531 {
532 size_t pos;
533 for (pos = 0; pos < pClientWindow->GetPageCount(); pos++)
534 {
535 if (pClientWindow->GetPage(pos) == this)
536 {
537 pClientWindow->SetSelection(pos);
538 break;
539 }
540 }
541 }
542 }
543
544 void wxTabMDIChildFrame::OnMenuHighlight(wxMenuEvent& event)
545 {
546 #if wxUSE_STATUSBAR
547 if (m_pMDIParentFrame)
548 {
549 // we don't have any help text for this item,
550 // but may be the MDI frame does?
551 m_pMDIParentFrame->OnMenuHighlight(event);
552 }
553 #else
554 wxUnusedVar(event);
555 #endif // wxUSE_STATUSBAR
556 }
557
558 void wxTabMDIChildFrame::OnActivate(wxActivateEvent& WXUNUSED(event))
559 {
560 // do nothing
561 }
562
563 void wxTabMDIChildFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
564 {
565 Destroy();
566 }
567
568 void wxTabMDIChildFrame::SetMDIParentFrame(wxTabMDIParentFrame* parentFrame)
569 {
570 m_pMDIParentFrame = parentFrame;
571 }
572
573 wxTabMDIParentFrame* wxTabMDIChildFrame::GetMDIParentFrame() const
574 {
575 return m_pMDIParentFrame;
576 }
577
578 void wxTabMDIChildFrame::Init()
579 {
580 m_pMDIParentFrame = NULL;
581 #if wxUSE_MENUS
582 m_pMenuBar = NULL;
583 #endif // wxUSE_MENUS
584 }
585
586 bool wxTabMDIChildFrame::Show(bool WXUNUSED(show))
587 {
588 // do nothing
589 return true;
590 }
591
592 void wxTabMDIChildFrame::DoShow(bool show)
593 {
594 wxWindow::Show(show);
595 }
596
597 void wxTabMDIChildFrame::DoSetSize(int x, int y, int width, int height, int WXUNUSED(sizeFlags))
598 {
599 m_mdi_newrect = wxRect(x, y, width, height);
600 }
601
602 void wxTabMDIChildFrame::DoMoveWindow(int x, int y, int width, int height)
603 {
604 m_mdi_newrect = wxRect(x, y, width, height);
605 }
606
607 void wxTabMDIChildFrame::ApplyMDIChildFrameRect()
608 {
609 if (m_mdi_currect != m_mdi_newrect)
610 {
611 wxPanel::DoMoveWindow(m_mdi_newrect.x, m_mdi_newrect.y,
612 m_mdi_newrect.width, m_mdi_newrect.height);
613 m_mdi_currect = m_mdi_newrect;
614 }
615 }
616
617
618 //-----------------------------------------------------------------------------
619 // wxTabMDIClientWindow
620 //-----------------------------------------------------------------------------
621
622 IMPLEMENT_DYNAMIC_CLASS(wxTabMDIClientWindow, wxAuiMultiNotebook)
623
624 BEGIN_EVENT_TABLE(wxTabMDIClientWindow, wxAuiMultiNotebook)
625 EVT_AUINOTEBOOK_PAGE_CHANGED(-1, wxTabMDIClientWindow::OnPageChanged)
626 EVT_SIZE(wxTabMDIClientWindow::OnSize)
627 END_EVENT_TABLE()
628
629 wxTabMDIClientWindow::wxTabMDIClientWindow()
630 {
631 }
632
633 wxTabMDIClientWindow::wxTabMDIClientWindow(wxTabMDIParentFrame* parent, long style)
634 {
635 CreateClient(parent, style);
636 }
637
638 wxTabMDIClientWindow::~wxTabMDIClientWindow()
639 {
640 DestroyChildren();
641 }
642
643 bool wxTabMDIClientWindow::CreateClient(wxTabMDIParentFrame* parent, long style)
644 {
645 SetWindowStyleFlag(style);
646
647 if (!wxAuiMultiNotebook::Create(parent,
648 -1,
649 wxPoint(0,0),
650 wxSize(100, 100),
651 wxNO_BORDER))
652 {
653 return false;
654 }
655
656 wxColour bkcolour = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
657 SetBackgroundColour(bkcolour);
658
659 m_mgr.GetArtProvider()->SetColour(wxAUI_ART_BACKGROUND_COLOUR, bkcolour);
660
661 return true;
662 }
663
664 int wxTabMDIClientWindow::SetSelection(size_t nPage)
665 {
666 return wxAuiMultiNotebook::SetSelection(nPage);
667 }
668
669 void wxTabMDIClientWindow::PageChanged(int old_selection, int new_selection)
670 {
671 // don't do anything if the page doesn't actually change
672 if (old_selection == new_selection)
673 return;
674
675 // don't do anything if the new page is already active
676 if (new_selection != -1)
677 {
678 wxTabMDIChildFrame* child = (wxTabMDIChildFrame*)GetPage(new_selection);
679 if (child->GetMDIParentFrame()->GetActiveChild() == child)
680 return;
681 }
682
683 // notify old active child that it has been deactivated
684 if (old_selection != -1)
685 {
686 wxTabMDIChildFrame* old_child = (wxTabMDIChildFrame*)GetPage(old_selection);
687 wxASSERT_MSG(old_child, wxT("wxTabMDIClientWindow::PageChanged - null page pointer"));
688
689 wxActivateEvent event(wxEVT_ACTIVATE, false, old_child->GetId());
690 event.SetEventObject(old_child);
691 old_child->GetEventHandler()->ProcessEvent(event);
692 }
693
694 // notify new active child that it has been activated
695 if (new_selection != -1)
696 {
697 wxTabMDIChildFrame* active_child = (wxTabMDIChildFrame*)GetPage(new_selection);
698 wxASSERT_MSG(active_child, wxT("wxTabMDIClientWindow::PageChanged - null page pointer"));
699
700 wxActivateEvent event(wxEVT_ACTIVATE, true, active_child->GetId());
701 event.SetEventObject(active_child);
702 active_child->GetEventHandler()->ProcessEvent(event);
703
704 if (active_child->GetMDIParentFrame())
705 {
706 active_child->GetMDIParentFrame()->SetActiveChild(active_child);
707 active_child->GetMDIParentFrame()->SetChildMenuBar(active_child);
708 }
709 }
710 }
711
712 void wxTabMDIClientWindow::OnPageChanged(wxAuiNotebookEvent& evt)
713 {
714 PageChanged(evt.GetOldSelection(), evt.GetSelection());
715 evt.Skip();
716 }
717
718 void wxTabMDIClientWindow::OnSize(wxSizeEvent& evt)
719 {
720 wxAuiMultiNotebook::OnSize(evt);
721
722 for (size_t pos = 0; pos < GetPageCount(); pos++)
723 ((wxTabMDIChildFrame *)GetPage(pos))->ApplyMDIChildFrameRect();
724 }
725
726 #endif // wxUSE_MDI