]> git.saurik.com Git - wxWidgets.git/blob - src/motif/frame.cpp
added Add/InsertTool() (patch 672032)
[wxWidgets.git] / src / motif / frame.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: motif/frame.cpp
3 // Purpose: wxFrame
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 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "frame.h"
22 #endif
23
24
25 #ifdef __VMS
26 #define XtDisplay XTDISPLAY
27 #define XtWindow XTWINDOW
28 #define XtScreen XTSCREEN
29 #endif
30
31 #include "wx/frame.h"
32 #include "wx/statusbr.h"
33 #include "wx/toolbar.h"
34 #include "wx/menu.h"
35 #include "wx/settings.h"
36 #include "wx/utils.h"
37 #include "wx/log.h"
38 #include "wx/app.h"
39
40 #ifdef __VMS__
41 #pragma message disable nosimpint
42 #endif
43
44 #if defined(__ultrix) || defined(__sgi)
45 #include <Xm/Frame.h>
46 #endif
47
48 #include <Xm/Xm.h>
49 #include <X11/Shell.h>
50 #include <X11/Core.h>
51 #if XmVersion >= 1002
52 #include <Xm/XmAll.h>
53 #else
54 #include <Xm/Frame.h>
55 #endif
56 #include <Xm/MwmUtil.h>
57 #include <Xm/BulletinB.h>
58 #include <Xm/Form.h>
59 #include <Xm/MainW.h>
60 #include <Xm/RowColumn.h>
61 #include <Xm/Label.h>
62 #include <Xm/AtomMgr.h>
63 #include <Xm/LabelG.h>
64 #include <Xm/Frame.h>
65 #if XmVersion > 1000
66 #include <Xm/Protocols.h>
67 #endif
68
69 #ifdef __VMS__
70 #pragma message enable nosimpint
71 #endif
72
73 #include "wx/motif/private.h"
74 #include "wx/unix/utilsx11.h"
75
76 // ----------------------------------------------------------------------------
77 // private functions
78 // ----------------------------------------------------------------------------
79
80 static void wxFrameMapProc(Widget frameShell, XtPointer clientData,
81 XCrossingEvent* event);
82
83 // ----------------------------------------------------------------------------
84 // globals
85 // ----------------------------------------------------------------------------
86
87 extern wxList wxModelessWindows;
88 extern wxList wxPendingDelete;
89
90 // TODO: this should be tidied so that any frame can be the
91 // top frame
92 // static bool wxTopLevelUsed = FALSE;
93
94 // ----------------------------------------------------------------------------
95 // wxWin macros
96 // ----------------------------------------------------------------------------
97
98 BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
99 EVT_ACTIVATE(wxFrame::OnActivate)
100 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged)
101 END_EVENT_TABLE()
102
103 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxWindow)
104
105 // ============================================================================
106 // implementation
107 // ============================================================================
108
109 // ----------------------------------------------------------------------------
110 // frame construction
111 // ----------------------------------------------------------------------------
112
113 void wxFrame::Init()
114 {
115 m_iconized = FALSE;
116
117 //// Motif-specific
118 m_frameShell = (WXWidget) NULL;
119 m_mainWidget = (WXWidget) NULL;;
120 m_workArea = (WXWidget) NULL;;
121 m_clientArea = (WXWidget) NULL;;
122 // m_visibleStatus = TRUE;
123 }
124
125 bool wxFrame::Create(wxWindow *parent,
126 wxWindowID id,
127 const wxString& title,
128 const wxPoint& pos,
129 const wxSize& size,
130 long style,
131 const wxString& name)
132 {
133 if( !wxTopLevelWindow::Create( parent, id, title, pos, size, style,
134 name ) )
135 return FALSE;
136
137 m_backgroundColour =
138 wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
139 m_foregroundColour = *wxBLACK;
140 m_font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
141
142 int x = pos.x, y = pos.y;
143 int width = size.x, height = size.y;
144
145 // Set reasonable values for position and size if defaults have been
146 // requested
147 //
148 // MB TODO: something better than these arbitrary values ?
149 // VZ should use X resources for this...
150 if ( width == -1 )
151 width = 400;
152 if ( height == -1 )
153 height = 400;
154
155 int displayW, displayH;
156 wxDisplaySize( &displayW, &displayH );
157
158 if ( x == -1 )
159 {
160 x = (displayW - width) / 2;
161 if (x < 10) x = 10;
162 }
163 if ( y == -1 )
164 {
165 y = (displayH - height) / 2;
166 if (y < 10) y = 10;
167 }
168
169 SetTitle( title );
170
171 wxLogTrace(wxTRACE_Messages,
172 "Created frame (0x%p) with work area 0x%p and client "
173 "area 0x%p", m_mainWidget, m_workArea, m_clientArea);
174
175 XtAddEventHandler((Widget) m_clientArea, ExposureMask,FALSE,
176 wxUniversalRepaintProc, (XtPointer) this);
177
178 if (x > -1)
179 XtVaSetValues((Widget) m_frameShell, XmNx, x, NULL);
180 if (y > -1)
181 XtVaSetValues((Widget) m_frameShell, XmNy, y, NULL);
182 if (width > -1)
183 XtVaSetValues((Widget) m_frameShell, XmNwidth, width, NULL);
184 if (height > -1)
185 XtVaSetValues((Widget) m_frameShell, XmNheight, height, NULL);
186
187 ChangeFont(FALSE);
188
189 ChangeBackgroundColour();
190
191 PreResize();
192
193 wxSizeEvent sizeEvent(wxSize(width, height), GetId());
194 sizeEvent.SetEventObject(this);
195
196 GetEventHandler()->ProcessEvent(sizeEvent);
197
198 return TRUE;
199 }
200
201 bool wxFrame::DoCreate( wxWindow* parent, wxWindowID id,
202 const wxString& title,
203 const wxPoint& pos,
204 const wxSize& size,
205 long style,
206 const wxString& name )
207 {
208 static bool wxTopLevelUsed = FALSE; /* this is global */
209 WXWidget frameShell;
210
211 if (wxTopLevelUsed)
212 {
213 // Change suggested by Matthew Flatt
214 frameShell = (WXWidget)XtAppCreateShell( name,
215 wxTheApp->GetClassName(),
216 topLevelShellWidgetClass,
217 (Display*) wxGetDisplay(),
218 NULL, 0 );
219 }
220 else
221 {
222 frameShell = wxTheApp->GetTopLevelWidget();
223 wxTopLevelUsed = TRUE;
224 }
225
226 XtVaSetValues((Widget) frameShell,
227 // Allows menu to resize
228 XmNallowShellResize, True,
229 XmNdeleteResponse, XmDO_NOTHING,
230 XmNmappedWhenManaged, False,
231 XmNiconic, (style & wxICONIZE) ? TRUE : FALSE,
232 NULL);
233
234 m_frameShell = frameShell;
235
236 m_mainWidget = (WXWidget) XtVaCreateManagedWidget("main_window",
237 xmMainWindowWidgetClass, (Widget) frameShell,
238 XmNresizePolicy, XmRESIZE_NONE,
239 NULL);
240
241 m_workArea = (WXWidget) XtVaCreateWidget("form",
242 xmFormWidgetClass, (Widget) m_mainWidget,
243 XmNresizePolicy, XmRESIZE_NONE,
244 NULL);
245
246 m_clientArea = (WXWidget) XtVaCreateWidget("client",
247 xmBulletinBoardWidgetClass, (Widget) m_workArea,
248 XmNmarginWidth, 0,
249 XmNmarginHeight, 0,
250 XmNrightAttachment, XmATTACH_FORM,
251 XmNleftAttachment, XmATTACH_FORM,
252 XmNtopAttachment, XmATTACH_FORM,
253 XmNbottomAttachment, XmATTACH_FORM,
254 NULL);
255
256 XtVaSetValues((Widget) m_mainWidget,
257 XmNworkWindow, (Widget) m_workArea,
258 NULL);
259
260 XtManageChild((Widget) m_clientArea);
261 XtManageChild((Widget) m_workArea);
262
263 XtTranslations ptr = XtParseTranslationTable( "<Configure>: resize()" );
264 XtOverrideTranslations( (Widget) m_workArea, ptr );
265 XtFree( (char *)ptr );
266
267 /* Part of show-&-hide fix */
268 XtAddEventHandler( (Widget)frameShell, StructureNotifyMask,
269 False, (XtEventHandler)wxFrameMapProc,
270 (XtPointer)this );
271
272 XtRealizeWidget((Widget) frameShell);
273
274 wxAddWindowToTable( (Widget)m_workArea, this);
275 wxAddWindowToTable( (Widget)m_clientArea, this);
276
277 wxModelessWindows.Append( this );
278
279 return TRUE;
280 }
281
282 wxFrame::~wxFrame()
283 {
284 m_isBeingDeleted = TRUE;
285
286 if (m_clientArea)
287 {
288 XtRemoveEventHandler((Widget) m_clientArea, ExposureMask, FALSE,
289 wxUniversalRepaintProc, (XtPointer) this);
290 }
291
292 if (GetMainWidget())
293 Show(FALSE);
294
295 if (m_frameMenuBar)
296 {
297 m_frameMenuBar->DestroyMenuBar();
298
299 // Hack to stop core dump on Ultrix, OSF, for some strange reason.
300 #if MOTIF_MENUBAR_DELETE_FIX
301 GetMenuBar()->SetMainWidget((WXWidget) NULL);
302 #endif
303 delete m_frameMenuBar;
304 m_frameMenuBar = NULL;
305 }
306
307 if (m_frameStatusBar)
308 {
309 delete m_frameStatusBar;
310 m_frameStatusBar = NULL;
311 }
312 }
313
314 void wxFrame::DoDestroy()
315 {
316 Widget frameShell = (Widget)GetShellWidget();
317
318 XtRemoveEventHandler( frameShell, StructureNotifyMask,
319 False, (XtEventHandler)wxFrameMapProc,
320 (XtPointer)this );
321
322 if( m_clientArea )
323 {
324 wxDeleteWindowFromTable( (Widget)m_clientArea );
325 XtDestroyWidget( (Widget)m_clientArea );
326 }
327
328 if( m_workArea )
329 {
330 XtVaSetValues( (Widget)m_mainWidget,
331 XmNworkWindow, (Widget)NULL,
332 NULL );
333
334 wxDeleteWindowFromTable( (Widget)m_workArea );
335 XtDestroyWidget( (Widget)m_workArea );
336 }
337
338 if( m_mainWidget )
339 XtDestroyWidget( (Widget)m_mainWidget );
340
341 if( frameShell )
342 XtDestroyWidget( frameShell );
343 }
344
345 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
346 void wxFrame::DoGetClientSize(int *x, int *y) const
347 {
348 Dimension xx, yy;
349 XtVaGetValues((Widget) m_workArea, XmNwidth, &xx, XmNheight, &yy, NULL);
350
351 if (m_frameStatusBar)
352 {
353 int sbw, sbh;
354 m_frameStatusBar->GetSize(& sbw, & sbh);
355 yy -= sbh;
356 }
357 #if wxUSE_TOOLBAR
358 if (m_frameToolBar)
359 {
360 int tbw, tbh;
361 m_frameToolBar->GetSize(& tbw, & tbh);
362 if (m_frameToolBar->GetWindowStyleFlag() & wxTB_VERTICAL)
363 xx -= tbw;
364 else
365 yy -= tbh;
366 }
367 #endif // wxUSE_TOOLBAR
368 *x = xx; *y = yy;
369 }
370
371 // Set the client size (i.e. leave the calculation of borders etc.
372 // to wxWindows)
373 void wxFrame::DoSetClientSize(int width, int height)
374 {
375 // Calculate how large the new main window should be
376 // by finding the difference between the client area and the
377 // main window area, and adding on to the new client area
378 if (width > -1)
379 XtVaSetValues((Widget) m_workArea, XmNwidth, width, NULL);
380
381 if (height > -1)
382 {
383 if (m_frameStatusBar)
384 {
385 int sbw, sbh;
386 m_frameStatusBar->GetSize(& sbw, & sbh);
387 height += sbh;
388 }
389 #if wxUSE_TOOLBAR
390 if (m_frameToolBar)
391 {
392 int tbw, tbh;
393 m_frameToolBar->GetSize(& tbw, & tbh);
394 if (m_frameToolBar->GetWindowStyleFlag() & wxTB_VERTICAL)
395 width += tbw;
396 else
397 height += tbh;
398 }
399 #endif // wxUSE_TOOLBAR
400
401 XtVaSetValues((Widget) m_workArea, XmNheight, height, NULL);
402 }
403 PreResize();
404
405 wxSizeEvent sizeEvent(wxSize(width, height), GetId());
406 sizeEvent.SetEventObject(this);
407
408 GetEventHandler()->ProcessEvent(sizeEvent);
409
410 }
411
412 void wxFrame::DoGetSize(int *width, int *height) const
413 {
414 Dimension xx, yy;
415 XtVaGetValues((Widget) m_frameShell, XmNwidth, &xx, XmNheight, &yy, NULL);
416 *width = xx; *height = yy;
417 }
418
419 void wxFrame::DoGetPosition(int *x, int *y) const
420 {
421 Window parent_window = XtWindow((Widget) m_frameShell),
422 next_parent = XtWindow((Widget) m_frameShell),
423 root = RootWindowOfScreen(XtScreen((Widget) m_frameShell));
424
425 // search for the parent that is child of ROOT, because the WM may
426 // reparent twice and notify only the next parent (like FVWM)
427 while (next_parent != root) {
428 Window *theChildren; unsigned int n;
429 parent_window = next_parent;
430 XQueryTree(XtDisplay((Widget) m_frameShell), parent_window, &root,
431 &next_parent, &theChildren, &n);
432 XFree(theChildren); // not needed
433 }
434 int xx, yy; unsigned int dummy;
435 XGetGeometry(XtDisplay((Widget) m_frameShell), parent_window, &root,
436 &xx, &yy, &dummy, &dummy, &dummy, &dummy);
437 if (x) *x = xx;
438 if (y) *y = yy;
439 }
440
441 void wxFrame::DoSetSize(int x, int y, int width, int height, int WXUNUSED(sizeFlags))
442 {
443 if (x > -1)
444 XtVaSetValues((Widget) m_frameShell, XmNx, x, NULL);
445 if (y > -1)
446 XtVaSetValues((Widget) m_frameShell, XmNy, y, NULL);
447 if (width > -1)
448 XtVaSetValues((Widget) m_mainWidget, XmNwidth, width, NULL);
449 if (height > -1)
450 XtVaSetValues((Widget) m_mainWidget, XmNheight, height, NULL);
451
452 if (!(height == -1 && width == -1))
453 {
454 PreResize();
455 }
456 }
457
458 bool wxFrame::Show( bool show )
459 {
460 if( !wxTopLevelWindowMotif::Show( show ) )
461 return FALSE;
462
463 m_isShown = show;
464
465 Widget shell = (Widget)GetShellWidget();
466 if (!shell)
467 return wxWindow::Show(show);
468
469 SetVisibleStatus(show);
470 if (show)
471 {
472 XtMapWidget (shell);
473 XRaiseWindow (XtDisplay(shell), XtWindow(shell));
474 }
475 else
476 {
477 XtUnmapWidget(shell);
478 }
479
480 return TRUE;
481 }
482
483 void wxFrame::SetTitle(const wxString& title)
484 {
485 wxString oldTitle = GetTitle();
486 if( title == oldTitle )
487 return;
488
489 wxTopLevelWindow::SetTitle( title );
490
491 if( !title.empty() )
492 XtVaSetValues( (Widget)m_frameShell,
493 XmNtitle, title.c_str(),
494 XmNiconName, title.c_str(),
495 NULL );
496 }
497
498 void wxFrame::DoSetIcon(const wxIcon& icon)
499 {
500 if (!m_frameShell)
501 return;
502
503 if (!icon.Ok() || !icon.GetPixmap())
504 return;
505
506 XtVaSetValues((Widget) m_frameShell, XtNiconPixmap, icon.GetPixmap(), NULL);
507 }
508
509 void wxFrame::SetIcon(const wxIcon& icon)
510 {
511 SetIcons( wxIconBundle( icon ) );
512 }
513
514 void wxFrame::SetIcons(const wxIconBundle& icons)
515 {
516 wxFrameBase::SetIcons( icons );
517
518 if (!m_frameShell)
519 return;
520
521 DoSetIcon( m_icons.GetIcon( -1 ) );
522 wxSetIconsX11(GetXDisplay(),
523 (WXWindow) XtWindow( (Widget) m_frameShell ), icons);
524 }
525
526 void wxFrame::PositionStatusBar()
527 {
528 if (!m_frameStatusBar)
529 return;
530
531 int w, h;
532 GetClientSize(&w, &h);
533 int sw, sh;
534 m_frameStatusBar->GetSize(&sw, &sh);
535
536 // Since we wish the status bar to be directly under the client area,
537 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
538 m_frameStatusBar->SetSize(0, h, w, sh);
539 }
540
541 WXWidget wxFrame::GetMenuBarWidget() const
542 {
543 if (GetMenuBar())
544 return GetMenuBar()->GetMainWidget();
545 else
546 return (WXWidget) NULL;
547 }
548
549 void wxFrame::SetMenuBar(wxMenuBar *menuBar)
550 {
551 if (!menuBar)
552 {
553 m_frameMenuBar = NULL;
554 return;
555 }
556
557 // Currently can't set it twice
558 // wxASSERT_MSG( (m_frameMenuBar == (wxMenuBar*) NULL), "Cannot set the menubar more than once");
559
560 if (m_frameMenuBar)
561 {
562 m_frameMenuBar->DestroyMenuBar();
563 delete m_frameMenuBar;
564 }
565
566 m_frameMenuBar = menuBar;
567 m_frameMenuBar->CreateMenuBar(this);
568 }
569
570 // Responds to colour changes, and passes event on to children.
571 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
572 {
573 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
574 Refresh();
575
576 if ( m_frameStatusBar )
577 {
578 wxSysColourChangedEvent event2;
579 event2.SetEventObject( m_frameStatusBar );
580 m_frameStatusBar->ProcessEvent(event2);
581 }
582
583 // Propagate the event to the non-top-level children
584 wxWindow::OnSysColourChanged(event);
585 }
586
587 // Default activation behaviour - set the focus for the first child
588 // subwindow found.
589 void wxFrame::OnActivate(wxActivateEvent& event)
590 {
591 if (!event.GetActive())
592 return;
593
594 for(wxWindowList::Node *node = GetChildren().GetFirst(); node;
595 node = node->GetNext())
596 {
597 // Find a child that's a subwindow, but not a dialog box.
598 wxWindow *child = node->GetData();
599 if (!child->IsTopLevel())
600 {
601 child->SetFocus();
602 return;
603 }
604 }
605 }
606
607 void wxFrame::SendSizeEvent()
608 {
609 wxSizeEvent event(GetSize(), GetId());
610 event.SetEventObject(this);
611 GetEventHandler()->AddPendingEvent(event);
612 }
613
614 #if wxUSE_TOOLBAR
615
616 wxToolBar* wxFrame::CreateToolBar(long style,
617 wxWindowID id,
618 const wxString& name)
619 {
620 if ( wxFrameBase::CreateToolBar(style, id, name) )
621 {
622 PositionToolBar();
623 }
624
625 return m_frameToolBar;
626 }
627
628 void wxFrame::SetToolBar(wxToolBar *toolbar)
629 {
630 wxFrameBase::SetToolBar(toolbar);
631 SendSizeEvent();
632 }
633
634 void wxFrame::PositionToolBar()
635 {
636 wxToolBar* tb = GetToolBar();
637 if (tb)
638 {
639 int cw, ch;
640 GetClientSize(& cw, &ch);
641
642 int tw, th;
643 tb->GetSize(& tw, & th);
644
645 if (tb->GetWindowStyleFlag() & wxTB_VERTICAL)
646 {
647 // Use the 'real' position. wxSIZE_NO_ADJUSTMENTS
648 // means, pretend we don't have toolbar/status bar, so we
649 // have the original client size.
650 th = ch + th;
651 }
652 else
653 {
654 // Use the 'real' position
655 tw = cw;
656 }
657
658 tb->SetSize(0, 0, tw, th, wxSIZE_NO_ADJUSTMENTS);
659 }
660 }
661 #endif // wxUSE_TOOLBAR
662
663 //// Motif-specific
664 bool wxFrame::PreResize()
665 {
666 #if wxUSE_TOOLBAR
667 PositionToolBar();
668 #endif // wxUSE_TOOLBAR
669
670 #if wxUSE_STATUSBAR
671 PositionStatusBar();
672 #endif // wxUSE_STATUSBAR
673
674 return TRUE;
675 }
676
677 WXWidget wxFrame::GetClientWidget() const
678 {
679 return m_clientArea;
680 }
681
682 void wxFrame::ChangeFont(bool WXUNUSED(keepOriginalSize))
683 {
684 // TODO
685 }
686
687 void wxFrame::ChangeBackgroundColour()
688 {
689 if (GetClientWidget())
690 DoChangeBackgroundColour(GetClientWidget(), m_backgroundColour);
691 }
692
693 void wxFrame::ChangeForegroundColour()
694 {
695 if (GetClientWidget())
696 DoChangeForegroundColour(GetClientWidget(), m_foregroundColour);
697 }
698
699 /* MATTEW: Used to insure that hide-&-show within an event cycle works */
700 static void wxFrameMapProc( Widget frameShell, XtPointer clientData,
701 XCrossingEvent* event )
702 {
703 wxFrame *tli = (wxFrame*)clientData;
704
705 XEvent *e = (XEvent *)event;
706
707 if( e->xany.type == MapNotify )
708 {
709 // Iconize fix
710 XtVaSetValues( frameShell, XmNiconic, (Boolean)False, NULL );
711 if( !tli->GetVisibleStatus() )
712 {
713 /* We really wanted this to be hidden! */
714 XtUnmapWidget( frameShell );
715 }
716 }
717 else if( e->xany.type == UnmapNotify )
718 // Iconize fix
719 XtVaSetValues( frameShell, XmNiconic, (Boolean)True, NULL );
720 }