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