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