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