]> git.saurik.com Git - wxWidgets.git/blob - src/motif/mdi.cpp
Update with recent changes.
[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/tab.h"
22 #include "wx/mdi.h"
23 #include "wx/menu.h"
24 #include "wx/settings.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 if (clientWindow->RemovePage(this))
430 clientWindow->Refresh();
431
432 // Set the selection to the first remaining page
433 if (clientWindow->GetPageCount() > 0)
434 {
435 wxMDIChildFrame* child = (wxMDIChildFrame*) clientWindow->GetPage(0);
436 parentFrame->SetActiveChild(child);
437 parentFrame->SetChildMenuBar(child);
438 }
439 else
440 {
441 parentFrame->SetActiveChild((wxMDIChildFrame*) NULL);
442 parentFrame->SetChildMenuBar((wxMDIChildFrame*) NULL);
443 }
444 }
445 }
446
447 #if 0
448 // Implementation: intercept and act upon raise and lower commands.
449 void wxMDIChildFrame::OnRaise()
450 {
451 wxMDIParentFrame* parentFrame = (wxMDIParentFrame*) GetParent() ;
452 wxMDIChildFrame* oldActiveChild = parentFrame->GetActiveChild();
453 parentFrame->SetActiveChild(this);
454
455 if (oldActiveChild)
456 {
457 wxActivateEvent event(wxEVT_ACTIVATE, FALSE, oldActiveChild->GetId());
458 event.SetEventObject( oldActiveChild );
459 oldActiveChild->GetEventHandler()->ProcessEvent(event);
460 }
461
462 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, this->GetId());
463 event.SetEventObject( this );
464 this->GetEventHandler()->ProcessEvent(event);
465 }
466
467 void wxMDIChildFrame::OnLower()
468 {
469 wxMDIParentFrame* parentFrame = (wxMDIParentFrame*) GetParent() ;
470 wxMDIChildFrame* oldActiveChild = parentFrame->GetActiveChild();
471
472 if (oldActiveChild == this)
473 {
474 wxActivateEvent event(wxEVT_ACTIVATE, FALSE, oldActiveChild->GetId());
475 event.SetEventObject( oldActiveChild );
476 oldActiveChild->GetEventHandler()->ProcessEvent(event);
477 }
478 // TODO: unfortunately we don't now know which is the top-most child,
479 // so make the active child NULL.
480 parentFrame->SetActiveChild((wxMDIChildFrame*) NULL);
481 }
482 #endif
483
484 // Set the client size (i.e. leave the calculation of borders etc.
485 // to wxWindows)
486 void wxMDIChildFrame::DoSetClientSize(int width, int height)
487 {
488 wxWindow::DoSetClientSize(width, height);
489 }
490
491 void wxMDIChildFrame::DoGetClientSize(int* width, int* height) const
492 {
493 wxWindow::DoGetSize(width, height);
494 }
495
496 void wxMDIChildFrame::DoSetSize(int x, int y, int width, int height, int sizeFlags)
497 {
498 wxWindow::DoSetSize(x, y, width, height, sizeFlags);
499 }
500
501 void wxMDIChildFrame::DoGetSize(int* width, int* height) const
502 {
503 wxWindow::DoGetSize(width, height);
504 }
505
506 void wxMDIChildFrame::DoGetPosition(int *x, int *y) const
507 {
508 wxWindow::DoGetPosition(x, y);
509 }
510
511 bool wxMDIChildFrame::Show(bool show)
512 {
513 SetVisibleStatus( show );
514 return wxWindow::Show(show);
515 }
516
517 void wxMDIChildFrame::SetMenuBar(wxMenuBar *menuBar)
518 {
519 // Don't create the underlying menubar yet; need to recreate
520 // it every time the child is activated.
521 m_frameMenuBar = menuBar;
522
523 // We make the assumption that if you're setting the menubar,
524 // this is the currently active child.
525 GetMDIParentFrame()->SetChildMenuBar(this);
526 }
527
528 // Set icon
529 void wxMDIChildFrame::SetIcon(const wxIcon& icon)
530 {
531 m_icons = wxIconBundle( icon );
532
533 if (icon.Ok())
534 {
535 // Not appropriate since there are no icons in
536 // a tabbed window
537 }
538 }
539
540 void wxMDIChildFrame::SetIcons(const wxIconBundle& icons)
541 {
542 m_icons = icons;
543 }
544
545 void wxMDIChildFrame::SetTitle(const wxString& title)
546 {
547 wxTopLevelWindow::SetTitle( title );
548 wxMDIClientWindow* clientWindow = GetMDIParentFrame()->GetClientWindow();
549 int pageNo = clientWindow->FindPagePosition(this);
550 if (pageNo > -1)
551 clientWindow->SetPageText(pageNo, title);
552 }
553
554 // MDI operations
555 void wxMDIChildFrame::Maximize()
556 {
557 // TODO
558 }
559
560 void wxMDIChildFrame::Iconize(bool WXUNUSED(iconize))
561 {
562 // TODO
563 }
564
565 bool wxMDIChildFrame::IsIconized() const
566 {
567 return FALSE;
568 }
569
570 // Is it maximized? Always maximized under Motif, using the
571 // tabbed MDI implementation.
572 bool wxMDIChildFrame::IsMaximized(void) const
573 {
574 return TRUE;
575 }
576
577 void wxMDIChildFrame::Restore()
578 {
579 // TODO
580 }
581
582 void wxMDIChildFrame::Activate()
583 {
584 // TODO
585 }
586
587 void wxMDIChildFrame::CaptureMouse()
588 {
589 wxWindow::CaptureMouse();
590 }
591
592 void wxMDIChildFrame::ReleaseMouse()
593 {
594 wxWindow::ReleaseMouse();
595 }
596
597 void wxMDIChildFrame::Raise()
598 {
599 wxWindow::Raise();
600 }
601
602 void wxMDIChildFrame::Lower(void)
603 {
604 wxWindow::Raise();
605 }
606
607 void wxMDIChildFrame::SetSizeHints(int WXUNUSED(minW), int WXUNUSED(minH), int WXUNUSED(maxW), int WXUNUSED(maxH), int WXUNUSED(incW), int WXUNUSED(incH))
608 {
609 }
610
611 // Client window
612
613 wxMDIClientWindow::wxMDIClientWindow()
614 {
615 }
616
617 wxMDIClientWindow::~wxMDIClientWindow()
618 {
619 // By the time this destructor is called, the child frames will have been
620 // deleted and removed from the notebook/client window.
621 DestroyChildren();
622
623 m_mainWidget = (WXWidget) 0;
624 }
625
626 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame *parent, long style)
627 {
628 SetWindowStyleFlag(style);
629
630 // m_windowParent = parent;
631 // m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
632
633 bool success = wxNotebook::Create(parent, wxID_NOTEBOOK_CLIENT_AREA, wxPoint(0, 0), wxSize(100, 100), 0);
634 if (success)
635 {
636 wxFont font(10, wxSWISS, wxNORMAL, wxNORMAL);
637 wxFont selFont(10, wxSWISS, wxNORMAL, wxBOLD);
638 GetTabView()->SetTabFont(font);
639 GetTabView()->SetSelectedTabFont(selFont);
640 GetTabView()->SetTabSize(120, 18);
641 GetTabView()->SetTabSelectionHeight(20);
642 return TRUE;
643 }
644 else
645 return FALSE;
646 }
647
648 void wxMDIClientWindow::DoSetSize(int x, int y, int width, int height, int sizeFlags)
649 {
650 wxWindow::DoSetSize(x, y, width, height, sizeFlags);
651 }
652
653 void wxMDIClientWindow::DoSetClientSize(int width, int height)
654 {
655 wxWindow::DoSetClientSize(width, height);
656 }
657
658 void wxMDIClientWindow::DoGetClientSize(int *width, int *height) const
659 {
660 wxWindow::DoGetClientSize(width, height);
661 }
662
663 void wxMDIClientWindow::DoGetSize(int *width, int *height) const
664 {
665 wxWindow::DoGetSize(width, height);
666 }
667
668 void wxMDIClientWindow::DoGetPosition(int *x, int *y) const
669 {
670 wxWindow::DoGetPosition(x, y);
671 }
672
673 void wxMDIClientWindow::OnScroll(wxScrollEvent& event)
674 {
675 // Default(); // Default processing: OBSOLETE FUNCTION
676 event.Skip();
677 }
678
679 void wxMDIClientWindow::OnPageChanged(wxNotebookEvent& event)
680 {
681 // Notify child that it has been activated
682 if (event.GetOldSelection() != -1)
683 {
684 wxMDIChildFrame* oldChild = (wxMDIChildFrame*) GetPage(event.GetOldSelection());
685 if (oldChild)
686 {
687 wxActivateEvent event(wxEVT_ACTIVATE, FALSE, oldChild->GetId());
688 event.SetEventObject( oldChild );
689 oldChild->GetEventHandler()->ProcessEvent(event);
690 }
691 }
692 if (event.GetSelection() != -1)
693 {
694 wxMDIChildFrame* activeChild = (wxMDIChildFrame*) GetPage(event.GetSelection());
695 if (activeChild)
696 {
697 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, activeChild->GetId());
698 event.SetEventObject( activeChild );
699 activeChild->GetEventHandler()->ProcessEvent(event);
700
701 if (activeChild->GetMDIParentFrame())
702 {
703 activeChild->GetMDIParentFrame()->SetActiveChild(activeChild);
704 activeChild->GetMDIParentFrame()->SetChildMenuBar(activeChild);
705 }
706 }
707 }
708 event.Skip();
709 }