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