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