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