]> git.saurik.com Git - wxWidgets.git/blob - src/motif/mdi.cpp
Compilation fix (missing headers).
[wxWidgets.git] / src / motif / mdi.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mdi.cpp
3 // Purpose: MDI classes
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "mdi.h"
14 #endif
15
16 #ifdef __VMS
17 #define XtDisplay XTDISPLAY
18 #define XtWindow XTWINDOW
19 #endif
20
21 #include "wx/mdi.h"
22 #include "wx/menu.h"
23 #include "wx/settings.h"
24 #include "wx/icon.h"
25
26 #ifdef __VMS__
27 #pragma message disable nosimpint
28 #endif
29 #include <Xm/Xm.h>
30 #include <Xm/BulletinB.h>
31 #include <Xm/Form.h>
32 #include <Xm/MainW.h>
33 #include <Xm/RowColumn.h>
34 #include <Xm/CascadeBG.h>
35 #include <Xm/Text.h>
36 #include <Xm/PushBG.h>
37 #include <Xm/AtomMgr.h>
38 #include <Xm/Protocols.h>
39 #ifdef __VMS__
40 #pragma message enable nosimpint
41 #endif
42
43 #include "wx/motif/private.h"
44
45 extern wxList wxModelessWindows;
46
47 // Implemented in frame.cpp
48 extern void wxFrameFocusProc(Widget workArea, XtPointer clientData,
49 XmAnyCallbackStruct *cbs);
50
51 #define wxID_NOTEBOOK_CLIENT_AREA wxID_HIGHEST + 100
52
53 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame, wxFrame)
54 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame, wxFrame)
55 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow, wxNotebook)
56
57 BEGIN_EVENT_TABLE(wxMDIParentFrame, wxFrame)
58 EVT_SIZE(wxMDIParentFrame::OnSize)
59 EVT_ACTIVATE(wxMDIParentFrame::OnActivate)
60 EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame::OnSysColourChanged)
61 EVT_MENU_HIGHLIGHT_ALL(wxMDIParentFrame::OnMenuHighlight)
62 END_EVENT_TABLE()
63
64 BEGIN_EVENT_TABLE(wxMDIClientWindow, wxNotebook)
65 EVT_SCROLL(wxMDIClientWindow::OnScroll)
66 EVT_NOTEBOOK_PAGE_CHANGED(wxID_NOTEBOOK_CLIENT_AREA, wxMDIClientWindow::OnPageChanged)
67 END_EVENT_TABLE()
68
69
70 // Parent frame
71
72 wxMDIParentFrame::wxMDIParentFrame()
73 {
74 m_clientWindow = (wxMDIClientWindow*) NULL;
75 m_activeChild = (wxMDIChildFrame*) NULL;
76 m_activeMenuBar = (wxMenuBar*) NULL;
77 }
78
79 bool wxMDIParentFrame::Create(wxWindow *parent,
80 wxWindowID id,
81 const wxString& title,
82 const wxPoint& pos,
83 const wxSize& size,
84 long style,
85 const wxString& name)
86 {
87 m_clientWindow = (wxMDIClientWindow*) NULL;
88 m_activeChild = (wxMDIChildFrame*) NULL;
89 m_activeMenuBar = (wxMenuBar*) NULL;
90
91 bool success = wxFrame::Create(parent, id, title, pos, size, style, name);
92 if (success)
93 {
94 // TODO: app cannot override OnCreateClient since
95 // wxMDIParentFrame::OnCreateClient will still be called
96 // (we're in the constructor). How to resolve?
97
98 m_clientWindow = OnCreateClient();
99
100 // Uses own style for client style
101 m_clientWindow->CreateClient(this, GetWindowStyleFlag());
102
103 int w, h;
104 GetClientSize(& w, & h);
105 m_clientWindow->SetSize(0, 0, w, h);
106 return TRUE;
107 }
108 else
109 return FALSE;
110 }
111
112 wxMDIParentFrame::~wxMDIParentFrame()
113 {
114 // Make sure we delete the client window last of all
115 RemoveChild(m_clientWindow);
116
117 DestroyChildren();
118
119 delete m_clientWindow;
120 m_clientWindow = NULL;
121 }
122
123 void wxMDIParentFrame::SetMenuBar(wxMenuBar *menu_bar)
124 {
125 m_frameMenuBar = menu_bar;
126
127 SetChildMenuBar((wxMDIChildFrame*) NULL);
128 }
129
130 void wxMDIParentFrame::OnSize(wxSizeEvent& WXUNUSED(event))
131 {
132 #if wxUSE_CONSTRAINTS
133 if (GetAutoLayout())
134 Layout();
135 #endif
136 int x = 0;
137 int y = 0;
138 int width, height;
139 GetClientSize(&width, &height);
140
141 if ( GetClientWindow() )
142 GetClientWindow()->SetSize(x, y, width, height);
143 }
144
145 void wxMDIParentFrame::DoGetClientSize(int *width, int *height) const
146 {
147 wxFrame::DoGetClientSize(width, height);
148 }
149
150 void wxMDIParentFrame::OnActivate(wxActivateEvent& WXUNUSED(event))
151 {
152 // Do nothing
153 }
154
155 // Returns the active MDI child window
156 wxMDIChildFrame *wxMDIParentFrame::GetActiveChild() const
157 {
158 return m_activeChild;
159 }
160
161 // Create the client window class (don't Create the window,
162 // just return a new class)
163 wxMDIClientWindow *wxMDIParentFrame::OnCreateClient()
164 {
165 return new wxMDIClientWindow ;
166 }
167
168 // Set the child's menu into the parent frame
169 void wxMDIParentFrame::SetChildMenuBar(wxMDIChildFrame* child)
170 {
171 wxMenuBar* oldMenuBar = m_activeMenuBar;
172
173 if (child == (wxMDIChildFrame*) NULL) // No child: use parent frame
174 {
175 if (GetMenuBar() && (GetMenuBar() != m_activeMenuBar))
176 {
177 // if (m_activeMenuBar)
178 // m_activeMenuBar->DestroyMenuBar();
179
180 m_activeMenuBar = GetMenuBar();
181 m_activeMenuBar->CreateMenuBar(this);
182 /*
183 if (oldMenuBar && XtIsManaged((Widget) oldMenuBar->GetMainWidget()))
184 XtUnmanageChild((Widget) oldMenuBar->GetMainWidget());
185 */
186 if (oldMenuBar && oldMenuBar->GetMainWidget())
187 XtUnmapWidget((Widget) oldMenuBar->GetMainWidget());
188
189 }
190 }
191 else if (child->GetMenuBar() == (wxMenuBar*) NULL) // No child menu bar: use parent frame
192 {
193 if (GetMenuBar() && (GetMenuBar() != m_activeMenuBar))
194 {
195 // if (m_activeMenuBar)
196 // m_activeMenuBar->DestroyMenuBar();
197 m_activeMenuBar = GetMenuBar();
198 m_activeMenuBar->CreateMenuBar(this);
199 /*
200 if (oldMenuBar && XtIsManaged((Widget) oldMenuBar->GetMainWidget()))
201 XtUnmanageChild((Widget) oldMenuBar->GetMainWidget());
202 */
203 if (oldMenuBar && oldMenuBar->GetMainWidget())
204 XtUnmapWidget((Widget) oldMenuBar->GetMainWidget());
205 }
206 }
207 else // The child has a menubar
208 {
209 if (child->GetMenuBar() != m_activeMenuBar)
210 {
211 // if (m_activeMenuBar)
212 // m_activeMenuBar->DestroyMenuBar();
213
214 m_activeMenuBar = child->GetMenuBar();
215 m_activeMenuBar->CreateMenuBar(this);
216 /*
217 if (oldMenuBar && XtIsManaged((Widget) oldMenuBar->GetMainWidget()))
218 XtUnmanageChild((Widget) oldMenuBar->GetMainWidget());
219 */
220 if (oldMenuBar && oldMenuBar->GetMainWidget())
221 XtUnmapWidget((Widget) oldMenuBar->GetMainWidget());
222 }
223 }
224 }
225
226 // Redirect events to active child first
227 bool wxMDIParentFrame::ProcessEvent(wxEvent& event)
228 {
229 // Stops the same event being processed repeatedly
230 static wxEventType inEvent = wxEVT_NULL;
231 if (inEvent == event.GetEventType())
232 return FALSE;
233
234 inEvent = event.GetEventType();
235
236 bool res = FALSE;
237 if (m_activeChild && event.IsKindOf(CLASSINFO(wxCommandEvent)))
238 {
239 res = m_activeChild->GetEventHandler()->ProcessEvent(event);
240 }
241
242 if (!res)
243 res = GetEventHandler()->wxEvtHandler::ProcessEvent(event);
244
245 inEvent = wxEVT_NULL;
246
247 return res;
248 }
249
250 void wxMDIParentFrame::DoSetSize(int x, int y,
251 int width, int height,
252 int sizeFlags)
253 {
254 wxWindow::DoSetSize(x, y, width, height, sizeFlags);
255 }
256
257 void wxMDIParentFrame::DoSetClientSize(int width, int height)
258 {
259 wxWindow::DoSetClientSize(width, height);
260 }
261
262 // Responds to colour changes, and passes event on to children.
263 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
264 {
265 // TODO
266
267 // Propagate the event to the non-top-level children
268 wxFrame::OnSysColourChanged(event);
269 }
270
271 // MDI operations
272 void wxMDIParentFrame::Cascade()
273 {
274 // TODO
275 }
276
277 void wxMDIParentFrame::Tile()
278 {
279 // TODO
280 }
281
282 void wxMDIParentFrame::ArrangeIcons()
283 {
284 // TODO
285 }
286
287 void wxMDIParentFrame::ActivateNext()
288 {
289 // TODO
290 }
291
292 void wxMDIParentFrame::ActivatePrevious()
293 {
294 // TODO
295 }
296
297 // Default menu selection behaviour - display a help string
298 void wxMDIParentFrame::OnMenuHighlight(wxMenuEvent& event)
299 {
300 if (GetStatusBar())
301 {
302 if (event.GetMenuId() == -1)
303 SetStatusText("");
304 else
305 {
306 wxMenuBar *menuBar = (wxMenuBar*) NULL;
307 if (GetActiveChild())
308 menuBar = GetActiveChild()->GetMenuBar();
309 else
310 menuBar = GetMenuBar();
311 if (menuBar)
312 {
313 wxString helpString(menuBar->GetHelpString(event.GetMenuId()));
314 if (helpString != "")
315 SetStatusText(helpString);
316 }
317 }
318 }
319 }
320
321 // Child frame
322
323 wxMDIChildFrame::wxMDIChildFrame()
324 {
325 m_mdiParentFrame = (wxMDIParentFrame*) NULL;
326 }
327
328 bool wxMDIChildFrame::Create(wxMDIParentFrame *parent,
329 wxWindowID id,
330 const wxString& title,
331 const wxPoint& pos,
332 const wxSize& size,
333 long style,
334 const wxString& name)
335 {
336 SetName(name);
337 SetWindowStyleFlag(style);
338
339 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
340 m_foregroundColour = *wxBLACK;
341 m_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
342
343 if ( id > -1 )
344 m_windowId = id;
345 else
346 m_windowId = (int)NewControlId();
347
348 wxMDIClientWindow* clientWindow = parent->GetClientWindow();
349
350 wxASSERT_MSG( (clientWindow != (wxWindow*) NULL), "Missing MDI client window.");
351
352 if (clientWindow) clientWindow->AddChild(this);
353
354 SetMDIParentFrame(parent);
355
356 int width = size.x;
357 int height = size.y;
358 if (width == -1)
359 width = 200; // TODO: give reasonable default
360 if (height == -1)
361 height = 200; // TODO: give reasonable default
362
363 // We're deactivating the old child
364 wxMDIChildFrame* oldActiveChild = parent->GetActiveChild();
365 if (oldActiveChild)
366 {
367 wxActivateEvent event(wxEVT_ACTIVATE, FALSE, oldActiveChild->GetId());
368 event.SetEventObject( oldActiveChild );
369 oldActiveChild->GetEventHandler()->ProcessEvent(event);
370 }
371
372 // This is the currently active child
373 parent->SetActiveChild((wxMDIChildFrame*) this);
374
375 // This time we'll try a bog-standard bulletin board for
376 // the 'frame'. A main window doesn't seem to work.
377
378 m_mainWidget = (WXWidget) XtVaCreateWidget("client",
379 xmBulletinBoardWidgetClass, (Widget) clientWindow->GetTopWidget(),
380 XmNmarginWidth, 0,
381 XmNmarginHeight, 0,
382 /*
383 XmNrightAttachment, XmATTACH_FORM,
384 XmNleftAttachment, XmATTACH_FORM,
385 XmNtopAttachment, XmATTACH_FORM,
386 XmNbottomAttachment, XmATTACH_FORM,
387 */
388 XmNresizePolicy, XmRESIZE_NONE,
389 NULL);
390
391 XtAddEventHandler((Widget) m_mainWidget, ExposureMask,FALSE,
392 wxUniversalRepaintProc, (XtPointer) this);
393
394 SetCanAddEventHandler(TRUE);
395 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
396
397 ChangeBackgroundColour();
398
399 XtManageChild((Widget) m_mainWidget);
400
401 SetTitle(title);
402
403 clientWindow->AddPage(this, title, TRUE);
404 clientWindow->Refresh();
405
406 // Positions the toolbar and status bar -- but we don't have any.
407 // PreResize();
408
409 wxModelessWindows.Append(this);
410 return TRUE;
411 }
412
413
414 wxMDIChildFrame::~wxMDIChildFrame()
415 {
416 if (m_mainWidget)
417 XtRemoveEventHandler((Widget) m_mainWidget, ExposureMask,FALSE,
418 wxUniversalRepaintProc, (XtPointer) this);
419
420 if (GetMDIParentFrame())
421 {
422 wxMDIParentFrame* parentFrame = GetMDIParentFrame();
423
424 if (parentFrame->GetActiveChild() == this)
425 parentFrame->SetActiveChild((wxMDIChildFrame*) NULL);
426 wxMDIClientWindow* clientWindow = parentFrame->GetClientWindow();
427
428 // Remove page if still there
429 {
430 int i = clientWindow->FindPage(this);
431
432 if (i != -1)
433 {
434 clientWindow->RemovePage(i);
435 clientWindow->Refresh();
436 }
437 }
438
439 // Set the selection to the first remaining page
440 if (clientWindow->GetPageCount() > 0)
441 {
442 wxMDIChildFrame* child = (wxMDIChildFrame*) clientWindow->GetPage(0);
443 parentFrame->SetActiveChild(child);
444 parentFrame->SetChildMenuBar(child);
445 }
446 else
447 {
448 parentFrame->SetActiveChild((wxMDIChildFrame*) NULL);
449 parentFrame->SetChildMenuBar((wxMDIChildFrame*) NULL);
450 }
451 }
452 }
453
454 #if 0
455 // Implementation: intercept and act upon raise and lower commands.
456 void wxMDIChildFrame::OnRaise()
457 {
458 wxMDIParentFrame* parentFrame = (wxMDIParentFrame*) GetParent() ;
459 wxMDIChildFrame* oldActiveChild = parentFrame->GetActiveChild();
460 parentFrame->SetActiveChild(this);
461
462 if (oldActiveChild)
463 {
464 wxActivateEvent event(wxEVT_ACTIVATE, FALSE, oldActiveChild->GetId());
465 event.SetEventObject( oldActiveChild );
466 oldActiveChild->GetEventHandler()->ProcessEvent(event);
467 }
468
469 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, this->GetId());
470 event.SetEventObject( this );
471 this->GetEventHandler()->ProcessEvent(event);
472 }
473
474 void wxMDIChildFrame::OnLower()
475 {
476 wxMDIParentFrame* parentFrame = (wxMDIParentFrame*) GetParent() ;
477 wxMDIChildFrame* oldActiveChild = parentFrame->GetActiveChild();
478
479 if (oldActiveChild == this)
480 {
481 wxActivateEvent event(wxEVT_ACTIVATE, FALSE, oldActiveChild->GetId());
482 event.SetEventObject( oldActiveChild );
483 oldActiveChild->GetEventHandler()->ProcessEvent(event);
484 }
485 // TODO: unfortunately we don't now know which is the top-most child,
486 // so make the active child NULL.
487 parentFrame->SetActiveChild((wxMDIChildFrame*) NULL);
488 }
489 #endif
490
491 // Set the client size (i.e. leave the calculation of borders etc.
492 // to wxWindows)
493 void wxMDIChildFrame::DoSetClientSize(int width, int height)
494 {
495 wxWindow::DoSetClientSize(width, height);
496 }
497
498 void wxMDIChildFrame::DoGetClientSize(int* width, int* height) const
499 {
500 wxWindow::DoGetSize(width, height);
501 }
502
503 void wxMDIChildFrame::DoSetSize(int x, int y, int width, int height, int sizeFlags)
504 {
505 wxWindow::DoSetSize(x, y, width, height, sizeFlags);
506 }
507
508 void wxMDIChildFrame::DoGetSize(int* width, int* height) const
509 {
510 wxWindow::DoGetSize(width, height);
511 }
512
513 void wxMDIChildFrame::DoGetPosition(int *x, int *y) const
514 {
515 wxWindow::DoGetPosition(x, y);
516 }
517
518 bool wxMDIChildFrame::Show(bool show)
519 {
520 SetVisibleStatus( show );
521 return wxWindow::Show(show);
522 }
523
524 void wxMDIChildFrame::SetMenuBar(wxMenuBar *menuBar)
525 {
526 // Don't create the underlying menubar yet; need to recreate
527 // it every time the child is activated.
528 m_frameMenuBar = menuBar;
529
530 // We make the assumption that if you're setting the menubar,
531 // this is the currently active child.
532 GetMDIParentFrame()->SetChildMenuBar(this);
533 }
534
535 // Set icon
536 void wxMDIChildFrame::SetIcon(const wxIcon& icon)
537 {
538 m_icons = wxIconBundle( icon );
539
540 if (icon.Ok())
541 {
542 // Not appropriate since there are no icons in
543 // a tabbed window
544 }
545 }
546
547 void wxMDIChildFrame::SetIcons(const wxIconBundle& icons)
548 {
549 m_icons = icons;
550 }
551
552 void wxMDIChildFrame::SetTitle(const wxString& title)
553 {
554 wxTopLevelWindow::SetTitle( title );
555 wxMDIClientWindow* clientWindow = GetMDIParentFrame()->GetClientWindow();
556
557 // Remove page if still there
558 {
559 int i = clientWindow->FindPage(this);
560
561 if (i != -1)
562 clientWindow->SetPageText(i, title);
563 }
564 }
565
566 // MDI operations
567 void wxMDIChildFrame::Maximize()
568 {
569 // TODO
570 }
571
572 void wxMDIChildFrame::Iconize(bool WXUNUSED(iconize))
573 {
574 // TODO
575 }
576
577 bool wxMDIChildFrame::IsIconized() const
578 {
579 return FALSE;
580 }
581
582 // Is it maximized? Always maximized under Motif, using the
583 // tabbed MDI implementation.
584 bool wxMDIChildFrame::IsMaximized(void) const
585 {
586 return TRUE;
587 }
588
589 void wxMDIChildFrame::Restore()
590 {
591 // TODO
592 }
593
594 void wxMDIChildFrame::Activate()
595 {
596 // TODO
597 }
598
599 void wxMDIChildFrame::CaptureMouse()
600 {
601 wxWindow::CaptureMouse();
602 }
603
604 void wxMDIChildFrame::ReleaseMouse()
605 {
606 wxWindow::ReleaseMouse();
607 }
608
609 void wxMDIChildFrame::Raise()
610 {
611 wxWindow::Raise();
612 }
613
614 void wxMDIChildFrame::Lower(void)
615 {
616 wxWindow::Raise();
617 }
618
619 void wxMDIChildFrame::SetSizeHints(int WXUNUSED(minW), int WXUNUSED(minH), int WXUNUSED(maxW), int WXUNUSED(maxH), int WXUNUSED(incW), int WXUNUSED(incH))
620 {
621 }
622
623 // Client window
624
625 wxMDIClientWindow::wxMDIClientWindow()
626 {
627 }
628
629 wxMDIClientWindow::~wxMDIClientWindow()
630 {
631 // By the time this destructor is called, the child frames will have been
632 // deleted and removed from the notebook/client window.
633 DestroyChildren();
634
635 m_mainWidget = (WXWidget) 0;
636 }
637
638 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame *parent, long style)
639 {
640 SetWindowStyleFlag(style);
641
642 // m_windowParent = parent;
643 // m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
644
645 bool success = wxNotebook::Create(parent, wxID_NOTEBOOK_CLIENT_AREA, wxPoint(0, 0), wxSize(100, 100), 0);
646 if (success)
647 {
648 wxFont font(10, wxSWISS, wxNORMAL, wxNORMAL);
649 SetFont(font);
650 return TRUE;
651 }
652 else
653 return FALSE;
654 }
655
656 int wxMDIClientWindow::FindPage(const wxNotebookPage* page)
657 {
658 for (int i = GetPageCount() - 1; i >= 0; --i)
659 {
660 if (GetPage(i) == page)
661 return i;
662 }
663
664 return -1;
665 }
666
667 void wxMDIClientWindow::DoSetSize(int x, int y, int width, int height, int sizeFlags)
668 {
669 wxWindow::DoSetSize(x, y, width, height, sizeFlags);
670 }
671
672 void wxMDIClientWindow::DoSetClientSize(int width, int height)
673 {
674 wxWindow::DoSetClientSize(width, height);
675 }
676
677 void wxMDIClientWindow::DoGetClientSize(int *width, int *height) const
678 {
679 wxWindow::DoGetClientSize(width, height);
680 }
681
682 void wxMDIClientWindow::DoGetSize(int *width, int *height) const
683 {
684 wxWindow::DoGetSize(width, height);
685 }
686
687 void wxMDIClientWindow::DoGetPosition(int *x, int *y) const
688 {
689 wxWindow::DoGetPosition(x, y);
690 }
691
692 void wxMDIClientWindow::OnScroll(wxScrollEvent& event)
693 {
694 // Default(); // Default processing: OBSOLETE FUNCTION
695 event.Skip();
696 }
697
698 void wxMDIClientWindow::OnPageChanged(wxNotebookEvent& event)
699 {
700 // Notify child that it has been activated
701 if (event.GetOldSelection() != -1)
702 {
703 wxMDIChildFrame* oldChild = (wxMDIChildFrame*) GetPage(event.GetOldSelection());
704 if (oldChild)
705 {
706 wxActivateEvent event(wxEVT_ACTIVATE, FALSE, oldChild->GetId());
707 event.SetEventObject( oldChild );
708 oldChild->GetEventHandler()->ProcessEvent(event);
709 }
710 }
711 if (event.GetSelection() != -1)
712 {
713 wxMDIChildFrame* activeChild = (wxMDIChildFrame*) GetPage(event.GetSelection());
714 if (activeChild)
715 {
716 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, activeChild->GetId());
717 event.SetEventObject( activeChild );
718 activeChild->GetEventHandler()->ProcessEvent(event);
719
720 if (activeChild->GetMDIParentFrame())
721 {
722 activeChild->GetMDIParentFrame()->SetActiveChild(activeChild);
723 activeChild->GetMDIParentFrame()->SetChildMenuBar(activeChild);
724 }
725 }
726 }
727 event.Skip();
728 }