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