]> git.saurik.com Git - wxWidgets.git/blob - src/common/framecmn.cpp
Improve inotify()-based wxFileSystemWatcher to handle creation/deletion.
[wxWidgets.git] / src / common / framecmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/framecmn.cpp
3 // Purpose: common (for all platforms) wxFrame functions
4 // Author: Julian Smart, Vadim Zeitlin
5 // Created: 01/02/97
6 // Id: $Id$
7 // Copyright: (c) 1998 Robert Roebling and Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #include "wx/frame.h"
27
28 #ifndef WX_PRECOMP
29 #include "wx/menu.h"
30 #include "wx/menuitem.h"
31 #include "wx/dcclient.h"
32 #include "wx/toolbar.h"
33 #include "wx/statusbr.h"
34 #endif // WX_PRECOMP
35
36 extern WXDLLEXPORT_DATA(const char) wxFrameNameStr[] = "frame";
37 extern WXDLLEXPORT_DATA(const char) wxStatusLineNameStr[] = "status_line";
38
39 // ----------------------------------------------------------------------------
40 // event table
41 // ----------------------------------------------------------------------------
42
43 #if wxUSE_MENUS && wxUSE_STATUSBAR
44
45 BEGIN_EVENT_TABLE(wxFrameBase, wxTopLevelWindow)
46 EVT_MENU_OPEN(wxFrameBase::OnMenuOpen)
47 EVT_MENU_CLOSE(wxFrameBase::OnMenuClose)
48
49 EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight)
50 END_EVENT_TABLE()
51
52 #endif // wxUSE_MENUS && wxUSE_STATUSBAR
53
54 // ============================================================================
55 // implementation
56 // ============================================================================
57
58 // ----------------------------------------------------------------------------
59 // XTI
60 // ----------------------------------------------------------------------------
61
62 wxDEFINE_FLAGS( wxFrameStyle )
63 wxBEGIN_FLAGS( wxFrameStyle )
64 // new style border flags, we put them first to
65 // use them for streaming out
66 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
67 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
68 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
69 wxFLAGS_MEMBER(wxBORDER_RAISED)
70 wxFLAGS_MEMBER(wxBORDER_STATIC)
71 wxFLAGS_MEMBER(wxBORDER_NONE)
72
73 // old style border flags
74 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
75 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
76 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
77 wxFLAGS_MEMBER(wxRAISED_BORDER)
78 wxFLAGS_MEMBER(wxSTATIC_BORDER)
79 wxFLAGS_MEMBER(wxBORDER)
80
81 // standard window styles
82 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
83 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
84 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
85 wxFLAGS_MEMBER(wxWANTS_CHARS)
86 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
87 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
88 wxFLAGS_MEMBER(wxVSCROLL)
89 wxFLAGS_MEMBER(wxHSCROLL)
90
91 // frame styles
92 wxFLAGS_MEMBER(wxSTAY_ON_TOP)
93 wxFLAGS_MEMBER(wxCAPTION)
94 #if WXWIN_COMPATIBILITY_2_6
95 wxFLAGS_MEMBER(wxTHICK_FRAME)
96 #endif // WXWIN_COMPATIBILITY_2_6
97 wxFLAGS_MEMBER(wxSYSTEM_MENU)
98 wxFLAGS_MEMBER(wxRESIZE_BORDER)
99 #if WXWIN_COMPATIBILITY_2_6
100 wxFLAGS_MEMBER(wxRESIZE_BOX)
101 #endif // WXWIN_COMPATIBILITY_2_6
102 wxFLAGS_MEMBER(wxCLOSE_BOX)
103 wxFLAGS_MEMBER(wxMAXIMIZE_BOX)
104 wxFLAGS_MEMBER(wxMINIMIZE_BOX)
105
106 wxFLAGS_MEMBER(wxFRAME_TOOL_WINDOW)
107 wxFLAGS_MEMBER(wxFRAME_FLOAT_ON_PARENT)
108
109 wxFLAGS_MEMBER(wxFRAME_SHAPED)
110 wxEND_FLAGS( wxFrameStyle )
111
112 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxFrame, wxTopLevelWindow, "wx/frame.h")
113
114 wxBEGIN_PROPERTIES_TABLE(wxFrame)
115 wxEVENT_PROPERTY( Menu, wxEVT_COMMAND_MENU_SELECTED, wxCommandEvent)
116
117 wxPROPERTY( Title,wxString, SetTitle, GetTitle, wxString(), 0 /*flags*/, \
118 wxT("Helpstring"), wxT("group"))
119 wxPROPERTY_FLAGS( WindowStyle, wxFrameStyle, long, SetWindowStyleFlag, \
120 GetWindowStyleFlag, wxEMPTY_PARAMETER_VALUE, 0 /*flags*/, \
121 wxT("Helpstring"), wxT("group")) // style
122 #if wxUSE_MENUS
123 wxPROPERTY( MenuBar, wxMenuBar *, SetMenuBar, GetMenuBar, wxEMPTY_PARAMETER_VALUE, \
124 0 /*flags*/, wxT("Helpstring"), wxT("group"))
125 #endif
126 wxEND_PROPERTIES_TABLE()
127
128 wxEMPTY_HANDLERS_TABLE(wxFrame)
129
130 wxCONSTRUCTOR_6( wxFrame, wxWindow*, Parent, wxWindowID, Id, wxString, Title, \
131 wxPoint, Position, wxSize, Size, long, WindowStyle)
132
133 // ----------------------------------------------------------------------------
134 // construction/destruction
135 // ----------------------------------------------------------------------------
136
137 wxFrameBase::wxFrameBase()
138 {
139 #if wxUSE_MENUS
140 m_frameMenuBar = NULL;
141 #endif // wxUSE_MENUS
142
143 #if wxUSE_TOOLBAR
144 m_frameToolBar = NULL;
145 #endif // wxUSE_TOOLBAR
146
147 #if wxUSE_STATUSBAR
148 m_frameStatusBar = NULL;
149 #endif // wxUSE_STATUSBAR
150
151 m_statusBarPane = 0;
152 }
153
154 wxFrameBase::~wxFrameBase()
155 {
156 // this destructor is required for Darwin
157 }
158
159 wxFrame *wxFrameBase::New(wxWindow *parent,
160 wxWindowID id,
161 const wxString& title,
162 const wxPoint& pos,
163 const wxSize& size,
164 long style,
165 const wxString& name)
166 {
167 return new wxFrame(parent, id, title, pos, size, style, name);
168 }
169
170 void wxFrameBase::DeleteAllBars()
171 {
172 #if wxUSE_MENUS
173 wxDELETE(m_frameMenuBar);
174 #endif // wxUSE_MENUS
175
176 #if wxUSE_STATUSBAR
177 wxDELETE(m_frameStatusBar);
178 #endif // wxUSE_STATUSBAR
179
180 #if wxUSE_TOOLBAR
181 wxDELETE(m_frameToolBar);
182 #endif // wxUSE_TOOLBAR
183 }
184
185 bool wxFrameBase::IsOneOfBars(const wxWindow *win) const
186 {
187 #if wxUSE_MENUS
188 if ( win == GetMenuBar() )
189 return true;
190 #endif // wxUSE_MENUS
191
192 #if wxUSE_STATUSBAR
193 if ( win == GetStatusBar() )
194 return true;
195 #endif // wxUSE_STATUSBAR
196
197 #if wxUSE_TOOLBAR
198 if ( win == GetToolBar() )
199 return true;
200 #endif // wxUSE_TOOLBAR
201
202 wxUnusedVar(win);
203
204 return false;
205 }
206
207 // ----------------------------------------------------------------------------
208 // wxFrame size management: we exclude the areas taken by menu/status/toolbars
209 // from the client area, so the client area is what's really available for the
210 // frame contents
211 // ----------------------------------------------------------------------------
212
213 // get the origin of the client area in the client coordinates
214 wxPoint wxFrameBase::GetClientAreaOrigin() const
215 {
216 wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin();
217
218 #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__)
219 wxToolBar *toolbar = GetToolBar();
220 if ( toolbar && toolbar->IsShown() )
221 {
222 int w, h;
223 toolbar->GetSize(&w, &h);
224
225 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
226 {
227 pt.x += w;
228 }
229 else
230 {
231 pt.y += h;
232 }
233 }
234 #endif // wxUSE_TOOLBAR
235
236 return pt;
237 }
238
239 // ----------------------------------------------------------------------------
240 // misc
241 // ----------------------------------------------------------------------------
242
243 #if wxUSE_MENUS
244
245 bool wxFrameBase::ProcessCommand(int id)
246 {
247 wxMenuItem* const item = FindItemInMenuBar(id);
248 if ( !item )
249 return false;
250
251 return ProcessCommand(item);
252 }
253
254 bool wxFrameBase::ProcessCommand(wxMenuItem *item)
255 {
256 wxCHECK_MSG( item, false, wxS("Menu item can't be NULL") );
257
258 if (!item->IsEnabled())
259 return true;
260
261 if ((item->GetKind() == wxITEM_RADIO) && item->IsChecked() )
262 return true;
263
264 int checked;
265 if (item->IsCheckable())
266 {
267 item->Toggle();
268
269 // use the new value
270 checked = item->IsChecked();
271 }
272 else // Uncheckable item.
273 {
274 checked = -1;
275 }
276
277 wxMenu* const menu = item->GetMenu();
278 wxCHECK_MSG( menu, false, wxS("Menu item should be attached to a menu") );
279
280 return menu->SendEvent(item->GetId(), checked);
281 }
282
283 #endif // wxUSE_MENUS
284
285 // Do the UI update processing for this window. This is
286 // provided for the application to call if it wants to
287 // force a UI update, particularly for the menus and toolbar.
288 void wxFrameBase::UpdateWindowUI(long flags)
289 {
290 wxWindowBase::UpdateWindowUI(flags);
291
292 #if wxUSE_TOOLBAR
293 if (GetToolBar())
294 GetToolBar()->UpdateWindowUI(flags);
295 #endif
296
297 #if wxUSE_MENUS
298 if (GetMenuBar())
299 {
300 // If coming from an idle event, we only want to update the menus if
301 // we're in the wxUSE_IDLEMENUUPDATES configuration, otherwise they
302 // will be update when the menu is opened later
303 #if !wxUSE_IDLEMENUUPDATES
304 if ( !(flags & wxUPDATE_UI_FROMIDLE) )
305 #endif // wxUSE_IDLEMENUUPDATES
306 DoMenuUpdates();
307 }
308 #endif // wxUSE_MENUS
309 }
310
311 // ----------------------------------------------------------------------------
312 // event handlers for status bar updates from menus
313 // ----------------------------------------------------------------------------
314
315 #if wxUSE_MENUS && wxUSE_STATUSBAR
316
317 void wxFrameBase::OnMenuHighlight(wxMenuEvent& event)
318 {
319 #if wxUSE_STATUSBAR
320 (void)ShowMenuHelp(event.GetMenuId());
321 #endif // wxUSE_STATUSBAR
322 }
323
324 void wxFrameBase::OnMenuOpen(wxMenuEvent& event)
325 {
326 #if wxUSE_IDLEMENUUPDATES
327 wxUnusedVar(event);
328 #else // !wxUSE_IDLEMENUUPDATES
329 // as we didn't update the menus from idle time, do it now
330 DoMenuUpdates(event.GetMenu());
331 #endif // wxUSE_IDLEMENUUPDATES/!wxUSE_IDLEMENUUPDATES
332 }
333
334 void wxFrameBase::OnMenuClose(wxMenuEvent& WXUNUSED(event))
335 {
336 DoGiveHelp(wxEmptyString, false);
337 }
338
339 #endif // wxUSE_MENUS && wxUSE_STATUSBAR
340
341 // Implement internal behaviour (menu updating on some platforms)
342 void wxFrameBase::OnInternalIdle()
343 {
344 wxTopLevelWindow::OnInternalIdle();
345
346 #if wxUSE_MENUS && wxUSE_IDLEMENUUPDATES
347 if (wxUpdateUIEvent::CanUpdate(this))
348 DoMenuUpdates();
349 #endif
350 }
351
352 // ----------------------------------------------------------------------------
353 // status bar stuff
354 // ----------------------------------------------------------------------------
355
356 #if wxUSE_STATUSBAR
357
358 wxStatusBar* wxFrameBase::CreateStatusBar(int number,
359 long style,
360 wxWindowID id,
361 const wxString& name)
362 {
363 // the main status bar can only be created once (or else it should be
364 // deleted before calling CreateStatusBar() again)
365 wxCHECK_MSG( !m_frameStatusBar, NULL,
366 wxT("recreating status bar in wxFrame") );
367
368 SetStatusBar(OnCreateStatusBar(number, style, id, name));
369
370 return m_frameStatusBar;
371 }
372
373 wxStatusBar *wxFrameBase::OnCreateStatusBar(int number,
374 long style,
375 wxWindowID id,
376 const wxString& name)
377 {
378 wxStatusBar *statusBar = new wxStatusBar(this, id, style, name);
379
380 statusBar->SetFieldsCount(number);
381
382 return statusBar;
383 }
384
385 void wxFrameBase::SetStatusText(const wxString& text, int number)
386 {
387 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
388
389 m_frameStatusBar->SetStatusText(text, number);
390 }
391
392 void wxFrameBase::SetStatusWidths(int n, const int widths_field[] )
393 {
394 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set widths for") );
395
396 m_frameStatusBar->SetStatusWidths(n, widths_field);
397
398 PositionStatusBar();
399 }
400
401 void wxFrameBase::PushStatusText(const wxString& text, int number)
402 {
403 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
404
405 m_frameStatusBar->PushStatusText(text, number);
406 }
407
408 void wxFrameBase::PopStatusText(int number)
409 {
410 wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") );
411
412 m_frameStatusBar->PopStatusText(number);
413 }
414
415 bool wxFrameBase::ShowMenuHelp(int menuId)
416 {
417 #if wxUSE_MENUS
418 // if no help string found, we will clear the status bar text
419 //
420 // NB: wxID_NONE is used for (sub)menus themselves by wxMSW
421 wxString helpString;
422 if ( menuId != wxID_SEPARATOR && menuId != wxID_NONE )
423 {
424 const wxMenuItem * const item = FindItemInMenuBar(menuId);
425 if ( item && !item->IsSeparator() )
426 helpString = item->GetHelp();
427
428 // notice that it's ok if we don't find the item because it might
429 // belong to the popup menu, so don't assert here
430 }
431
432 DoGiveHelp(helpString, true);
433
434 return !helpString.empty();
435 #else // !wxUSE_MENUS
436 return false;
437 #endif // wxUSE_MENUS/!wxUSE_MENUS
438 }
439
440 void wxFrameBase::SetStatusBar(wxStatusBar *statBar)
441 {
442 bool hadBar = m_frameStatusBar != NULL;
443 m_frameStatusBar = statBar;
444
445 if ( (m_frameStatusBar != NULL) != hadBar )
446 {
447 PositionStatusBar();
448
449 DoLayout();
450 }
451 }
452
453 #endif // wxUSE_STATUSBAR
454
455 #if wxUSE_MENUS || wxUSE_TOOLBAR
456 void wxFrameBase::DoGiveHelp(const wxString& help, bool show)
457 {
458 #if wxUSE_STATUSBAR
459 if ( m_statusBarPane < 0 )
460 {
461 // status bar messages disabled
462 return;
463 }
464
465 wxStatusBar *statbar = GetStatusBar();
466 if ( !statbar )
467 return;
468
469 wxString text;
470 if ( show )
471 {
472 // remember the old status bar text if this is the first time we're
473 // called since the menu has been opened as we're going to overwrite it
474 // in our DoGiveHelp() and we want to restore it when the menu is
475 // closed
476 //
477 // note that it would be logical to do this in OnMenuOpen() but under
478 // MSW we get an EVT_MENU_HIGHLIGHT before EVT_MENU_OPEN, strangely
479 // enough, and so this doesn't work and instead we use the ugly trick
480 // with using special m_oldStatusText value as "menu opened" (but it is
481 // arguably better than adding yet another member variable to wxFrame
482 // on all platforms)
483 if ( m_oldStatusText.empty() )
484 {
485 m_oldStatusText = statbar->GetStatusText(m_statusBarPane);
486 if ( m_oldStatusText.empty() )
487 {
488 // use special value to prevent us from doing this the next time
489 m_oldStatusText += wxT('\0');
490 }
491 }
492
493 m_lastHelpShown =
494 text = help;
495 }
496 else // hide help, restore the original text
497 {
498 // clear the last shown help string but remember its value
499 wxString lastHelpShown;
500 lastHelpShown.swap(m_lastHelpShown);
501
502 // also clear the old status text but remember it too to restore it
503 // below
504 text.swap(m_oldStatusText);
505
506 if ( statbar->GetStatusText(m_statusBarPane) != lastHelpShown )
507 {
508 // if the text was changed with an explicit SetStatusText() call
509 // from the user code in the meanwhile, do not overwrite it with
510 // the old status bar contents -- this is almost certainly not what
511 // the user expects and would be very hard to avoid from user code
512 return;
513 }
514 }
515
516 statbar->SetStatusText(text, m_statusBarPane);
517 #else
518 wxUnusedVar(help);
519 wxUnusedVar(show);
520 #endif // wxUSE_STATUSBAR
521 }
522 #endif // wxUSE_MENUS || wxUSE_TOOLBAR
523
524
525 // ----------------------------------------------------------------------------
526 // toolbar stuff
527 // ----------------------------------------------------------------------------
528
529 #if wxUSE_TOOLBAR
530
531 wxToolBar* wxFrameBase::CreateToolBar(long style,
532 wxWindowID id,
533 const wxString& name)
534 {
535 // the main toolbar can't be recreated (unless it was explicitly deleted
536 // before)
537 wxCHECK_MSG( !m_frameToolBar, NULL,
538 wxT("recreating toolbar in wxFrame") );
539
540 if ( style == -1 )
541 {
542 // use default style
543 //
544 // NB: we don't specify the default value in the method declaration
545 // because
546 // a) this allows us to have different defaults for different
547 // platforms (even if we don't have them right now)
548 // b) we don't need to include wx/toolbar.h in the header then
549 style = wxBORDER_NONE | wxTB_HORIZONTAL | wxTB_FLAT;
550 }
551
552 SetToolBar(OnCreateToolBar(style, id, name));
553
554 return m_frameToolBar;
555 }
556
557 wxToolBar* wxFrameBase::OnCreateToolBar(long style,
558 wxWindowID id,
559 const wxString& name)
560 {
561 #if defined(__WXWINCE__) && defined(__POCKETPC__)
562 return new wxToolMenuBar(this, id,
563 wxDefaultPosition, wxDefaultSize,
564 style, name);
565 #else
566 return new wxToolBar(this, id,
567 wxDefaultPosition, wxDefaultSize,
568 style, name);
569 #endif
570 }
571
572 void wxFrameBase::SetToolBar(wxToolBar *toolbar)
573 {
574 if ( (toolbar != NULL) != (m_frameToolBar != NULL) )
575 {
576 // the toolbar visibility must have changed so we need to both position
577 // the toolbar itself (if it appeared) and to relayout the frame
578 // contents in any case
579
580 if ( toolbar )
581 {
582 // we need to assign it to m_frameToolBar for PositionToolBar() to
583 // do anything
584 m_frameToolBar = toolbar;
585 PositionToolBar();
586 }
587 //else: tricky: do not reset m_frameToolBar yet as otherwise DoLayout()
588 // wouldn't recognize the (still existing) toolbar as one of our
589 // bars and wouldn't layout the single child of the frame correctly
590
591
592 // and this is even more tricky: we want DoLayout() to recognize the
593 // old toolbar for the purpose of not counting it among our non-bar
594 // children but we don't want to reserve any more space for it so we
595 // temporarily hide it
596 if ( m_frameToolBar )
597 m_frameToolBar->Hide();
598
599 DoLayout();
600
601 if ( m_frameToolBar )
602 m_frameToolBar->Show();
603 }
604
605 // this might have been already done above but it's simpler to just always
606 // do it unconditionally instead of testing for whether we already did it
607 m_frameToolBar = toolbar;
608 }
609
610 #endif // wxUSE_TOOLBAR
611
612 // ----------------------------------------------------------------------------
613 // menus
614 // ----------------------------------------------------------------------------
615
616 #if wxUSE_MENUS
617
618 // update all menus
619 void wxFrameBase::DoMenuUpdates(wxMenu* menu)
620 {
621 if (menu)
622 {
623 wxEvtHandler* source = GetEventHandler();
624 menu->UpdateUI(source);
625 }
626 else
627 {
628 wxMenuBar* bar = GetMenuBar();
629 if (bar != NULL)
630 bar->UpdateMenus();
631 }
632 }
633
634 void wxFrameBase::DetachMenuBar()
635 {
636 if ( m_frameMenuBar )
637 {
638 m_frameMenuBar->Detach();
639 m_frameMenuBar = NULL;
640 }
641 }
642
643 void wxFrameBase::AttachMenuBar(wxMenuBar *menubar)
644 {
645 if ( menubar )
646 {
647 menubar->Attach((wxFrame *)this);
648 m_frameMenuBar = menubar;
649 }
650 }
651
652 void wxFrameBase::SetMenuBar(wxMenuBar *menubar)
653 {
654 if ( menubar == GetMenuBar() )
655 {
656 // nothing to do
657 return;
658 }
659
660 DetachMenuBar();
661
662 this->AttachMenuBar(menubar);
663 }
664
665 wxMenuItem *wxFrameBase::FindItemInMenuBar(int menuId) const
666 {
667 const wxMenuBar * const menuBar = GetMenuBar();
668
669 return menuBar ? menuBar->FindItem(menuId) : NULL;
670 }
671
672 #endif // wxUSE_MENUS