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