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