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