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