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