]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/frame.cpp
fix wxMenu leak (reopened #9089)
[wxWidgets.git] / src / msw / frame.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/msw/frame.cpp
3// Purpose: wxFrame
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
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 __BORLANDC__
24 #pragma hdrstop
25#endif
26
27#include "wx/frame.h"
28
29#ifndef WX_PRECOMP
30 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
31 #include "wx/app.h"
32 #include "wx/menu.h"
33 #include "wx/utils.h"
34 #include "wx/dialog.h"
35 #include "wx/settings.h"
36 #include "wx/dcclient.h"
37 #include "wx/mdi.h"
38 #include "wx/panel.h"
39 #include "wx/log.h"
40 #include "wx/toolbar.h"
41 #include "wx/statusbr.h"
42 #include "wx/menuitem.h"
43#endif // WX_PRECOMP
44
45#include "wx/msw/private.h"
46
47#if defined(__POCKETPC__) || defined(__SMARTPHONE__)
48 #include <ole2.h>
49 #include <aygshell.h>
50 #include "wx/msw/winundef.h"
51#endif
52
53#include "wx/generic/statusbr.h"
54
55#ifdef __WXUNIVERSAL__
56 #include "wx/univ/theme.h"
57 #include "wx/univ/colschem.h"
58#endif // __WXUNIVERSAL__
59
60// ----------------------------------------------------------------------------
61// globals
62// ----------------------------------------------------------------------------
63
64#if wxUSE_MENUS_NATIVE
65 extern wxMenu *wxCurrentPopupMenu;
66#endif // wxUSE_MENUS_NATIVE
67
68// ----------------------------------------------------------------------------
69// event tables
70// ----------------------------------------------------------------------------
71
72BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
73 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged)
74END_EVENT_TABLE()
75
76#if wxUSE_EXTENDED_RTTI
77WX_DEFINE_FLAGS( wxFrameStyle )
78
79wxBEGIN_FLAGS( wxFrameStyle )
80 // new style border flags, we put them first to
81 // use them for streaming out
82 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
83 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
84 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
85 wxFLAGS_MEMBER(wxBORDER_RAISED)
86 wxFLAGS_MEMBER(wxBORDER_STATIC)
87 wxFLAGS_MEMBER(wxBORDER_NONE)
88
89 // old style border flags
90 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
91 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
92 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
93 wxFLAGS_MEMBER(wxRAISED_BORDER)
94 wxFLAGS_MEMBER(wxSTATIC_BORDER)
95 wxFLAGS_MEMBER(wxBORDER)
96
97 // standard window styles
98 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
99 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
100 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
101 wxFLAGS_MEMBER(wxWANTS_CHARS)
102 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
103 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
104 wxFLAGS_MEMBER(wxVSCROLL)
105 wxFLAGS_MEMBER(wxHSCROLL)
106
107 // frame styles
108 wxFLAGS_MEMBER(wxSTAY_ON_TOP)
109 wxFLAGS_MEMBER(wxCAPTION)
110#if WXWIN_COMPATIBILITY_2_6
111 wxFLAGS_MEMBER(wxTHICK_FRAME)
112#endif // WXWIN_COMPATIBILITY_2_6
113 wxFLAGS_MEMBER(wxSYSTEM_MENU)
114 wxFLAGS_MEMBER(wxRESIZE_BORDER)
115#if WXWIN_COMPATIBILITY_2_6
116 wxFLAGS_MEMBER(wxRESIZE_BOX)
117#endif // WXWIN_COMPATIBILITY_2_6
118 wxFLAGS_MEMBER(wxCLOSE_BOX)
119 wxFLAGS_MEMBER(wxMAXIMIZE_BOX)
120 wxFLAGS_MEMBER(wxMINIMIZE_BOX)
121
122 wxFLAGS_MEMBER(wxFRAME_TOOL_WINDOW)
123 wxFLAGS_MEMBER(wxFRAME_FLOAT_ON_PARENT)
124
125 wxFLAGS_MEMBER(wxFRAME_SHAPED)
126
127wxEND_FLAGS( wxFrameStyle )
128
129IMPLEMENT_DYNAMIC_CLASS_XTI(wxFrame, wxTopLevelWindow,"wx/frame.h")
130
131wxBEGIN_PROPERTIES_TABLE(wxFrame)
132 wxEVENT_PROPERTY( Menu , wxEVT_COMMAND_MENU_SELECTED , wxCommandEvent)
133
134 wxPROPERTY( Title,wxString, SetTitle, GetTitle, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
135 wxPROPERTY_FLAGS( WindowStyle , wxFrameStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
136 wxPROPERTY( MenuBar , wxMenuBar * , SetMenuBar , GetMenuBar , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
137wxEND_PROPERTIES_TABLE()
138
139wxBEGIN_HANDLERS_TABLE(wxFrame)
140wxEND_HANDLERS_TABLE()
141
142wxCONSTRUCTOR_6( wxFrame , wxWindow* , Parent , wxWindowID , Id , wxString , Title , wxPoint , Position , wxSize , Size , long , WindowStyle)
143
144#else
145IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
146#endif
147
148// ============================================================================
149// implementation
150// ============================================================================
151
152// ----------------------------------------------------------------------------
153// static class members
154// ----------------------------------------------------------------------------
155
156#if wxUSE_STATUSBAR
157 #if wxUSE_NATIVE_STATUSBAR
158 bool wxFrame::m_useNativeStatusBar = true;
159 #else
160 bool wxFrame::m_useNativeStatusBar = false;
161 #endif
162#endif // wxUSE_NATIVE_STATUSBAR
163
164// ----------------------------------------------------------------------------
165// creation/destruction
166// ----------------------------------------------------------------------------
167
168void wxFrame::Init()
169{
170#if wxUSE_MENUS
171 m_hMenu = NULL;
172#endif // wxUSE_MENUS
173
174#if wxUSE_TOOLTIPS
175 m_hwndToolTip = 0;
176#endif
177
178 m_wasMinimized = false;
179}
180
181bool wxFrame::Create(wxWindow *parent,
182 wxWindowID id,
183 const wxString& title,
184 const wxPoint& pos,
185 const wxSize& size,
186 long style,
187 const wxString& name)
188{
189 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
190 return false;
191
192 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
193
194#if defined(__SMARTPHONE__)
195 SetLeftMenu(wxID_EXIT, _("Done"));
196#endif
197
198#if wxUSE_ACCEL && defined(__POCKETPC__)
199 // The guidelines state that Ctrl+Q should quit the app.
200 // Let's define an accelerator table to send wxID_EXIT.
201 wxAcceleratorEntry entries[1];
202 entries[0].Set(wxACCEL_CTRL, 'Q', wxID_EXIT);
203 wxAcceleratorTable accel(1, entries);
204 SetAcceleratorTable(accel);
205#endif // wxUSE_ACCEL && __POCKETPC__
206
207 return true;
208}
209
210wxFrame::~wxFrame()
211{
212 m_isBeingDeleted = true;
213 DeleteAllBars();
214}
215
216// ----------------------------------------------------------------------------
217// wxFrame client size calculations
218// ----------------------------------------------------------------------------
219
220void wxFrame::DoSetClientSize(int width, int height)
221{
222 // leave enough space for the status bar if we have (and show) it
223#if wxUSE_STATUSBAR
224 wxStatusBar *statbar = GetStatusBar();
225 if ( statbar && statbar->IsShown() )
226 {
227 height += statbar->GetSize().y;
228 }
229#endif // wxUSE_STATUSBAR
230
231 // call GetClientAreaOrigin() to take the toolbar into account
232 wxPoint pt = GetClientAreaOrigin();
233 width += pt.x;
234 height += pt.y;
235
236#if wxUSE_TOOLBAR
237 wxToolBar * const toolbar = GetToolBar();
238 if ( toolbar )
239 {
240 if ( toolbar->HasFlag(wxTB_RIGHT | wxTB_BOTTOM) )
241 {
242 const wxSize sizeTB = toolbar->GetSize();
243 if ( toolbar->HasFlag(wxTB_RIGHT) )
244 width -= sizeTB.x;
245 else // wxTB_BOTTOM
246 height -= sizeTB.y;
247 }
248 //else: toolbar already taken into account by GetClientAreaOrigin()
249 }
250#endif // wxUSE_TOOLBAR
251
252 wxTopLevelWindow::DoSetClientSize(width, height);
253}
254
255// Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
256void wxFrame::DoGetClientSize(int *x, int *y) const
257{
258 wxTopLevelWindow::DoGetClientSize(x, y);
259
260 // account for the possible toolbar
261 wxPoint pt = GetClientAreaOrigin();
262 if ( x )
263 *x -= pt.x;
264
265 if ( y )
266 *y -= pt.y;
267
268#if wxUSE_TOOLBAR
269 wxToolBar * const toolbar = GetToolBar();
270 if ( toolbar )
271 {
272 if ( toolbar->HasFlag(wxTB_RIGHT | wxTB_BOTTOM) )
273 {
274 const wxSize sizeTB = toolbar->GetSize();
275 if ( toolbar->HasFlag(wxTB_RIGHT) )
276 {
277 if ( x )
278 *x -= sizeTB.x;
279 }
280 else // wxTB_BOTTOM
281 {
282 if ( y )
283 *y -= sizeTB.y;
284 }
285 }
286 //else: toolbar already taken into account by GetClientAreaOrigin()
287 }
288#endif // wxUSE_TOOLBAR
289
290#if wxUSE_STATUSBAR
291 // adjust client area height to take the status bar into account
292 if ( y )
293 {
294 wxStatusBar *statbar = GetStatusBar();
295 if ( statbar && statbar->IsShown() )
296 {
297 *y -= statbar->GetClientSize().y;
298 }
299 }
300#endif // wxUSE_STATUSBAR
301}
302
303// ----------------------------------------------------------------------------
304// wxFrame: various geometry-related functions
305// ----------------------------------------------------------------------------
306
307void wxFrame::Raise()
308{
309 ::SetForegroundWindow(GetHwnd());
310}
311
312// generate an artificial resize event
313void wxFrame::SendSizeEvent(int flags)
314{
315 if ( !m_iconized )
316 {
317 RECT r = wxGetWindowRect(GetHwnd());
318
319 if ( flags & wxSEND_EVENT_POST )
320 {
321 ::PostMessage(GetHwnd(), WM_SIZE,
322 IsMaximized() ? SIZE_MAXIMIZED : SIZE_RESTORED,
323 MAKELPARAM(r.right - r.left, r.bottom - r.top));
324 }
325 else // send it
326 {
327 ::SendMessage(GetHwnd(), WM_SIZE,
328 IsMaximized() ? SIZE_MAXIMIZED : SIZE_RESTORED,
329 MAKELPARAM(r.right - r.left, r.bottom - r.top));
330 }
331 }
332}
333
334#if wxUSE_STATUSBAR
335wxStatusBar *wxFrame::OnCreateStatusBar(int number,
336 long style,
337 wxWindowID id,
338 const wxString& name)
339{
340 wxStatusBar *statusBar wxDUMMY_INITIALIZE(NULL);
341
342#if wxUSE_NATIVE_STATUSBAR
343 if ( !UsesNativeStatusBar() )
344 {
345 statusBar = (wxStatusBar *)new wxStatusBarGeneric(this, id, style);
346 }
347 else
348#endif
349 {
350 statusBar = new wxStatusBar(this, id, style, name);
351 }
352
353 statusBar->SetFieldsCount(number);
354
355 return statusBar;
356}
357
358void wxFrame::PositionStatusBar()
359{
360 if ( !m_frameStatusBar || !m_frameStatusBar->IsShown() )
361 return;
362
363 int w, h;
364 GetClientSize(&w, &h);
365
366 int sw, sh;
367 m_frameStatusBar->GetSize(&sw, &sh);
368
369 int x = 0;
370#if wxUSE_TOOLBAR
371 wxToolBar * const toolbar = GetToolBar();
372 if ( toolbar && !toolbar->HasFlag(wxTB_TOP) )
373 {
374 const wxSize sizeTB = toolbar->GetSize();
375
376 if ( toolbar->HasFlag(wxTB_LEFT | wxTB_RIGHT) )
377 {
378 if ( toolbar->HasFlag(wxTB_LEFT) )
379 x -= sizeTB.x;
380
381 w += sizeTB.x;
382 }
383 else // wxTB_BOTTOM
384 {
385 // we need to position the status bar below the toolbar
386 h += sizeTB.y;
387 }
388 }
389 //else: no adjustments necessary for the toolbar on top
390#endif // wxUSE_TOOLBAR
391
392 // Since we wish the status bar to be directly under the client area,
393 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
394 m_frameStatusBar->SetSize(x, h, w, sh);
395}
396
397#endif // wxUSE_STATUSBAR
398
399#if wxUSE_MENUS_NATIVE
400
401void wxFrame::AttachMenuBar(wxMenuBar *menubar)
402{
403#if defined(__SMARTPHONE__) && defined(__WXWINCE__)
404
405 wxMenu *autoMenu = NULL;
406
407 if( menubar->GetMenuCount() == 1 )
408 {
409 autoMenu = wxTopLevelWindowMSW::ButtonMenu::DuplicateMenu(menubar->GetMenu(0));
410 SetRightMenu(wxID_ANY, menubar->GetMenuLabel(0), autoMenu);
411 }
412 else
413 {
414 autoMenu = new wxMenu;
415
416 for( size_t n = 0; n < menubar->GetMenuCount(); n++ )
417 {
418 wxMenu *item = menubar->GetMenu(n);
419 wxString label = menubar->GetMenuLabel(n);
420 wxMenu *new_item = wxTopLevelWindowMSW::ButtonMenu::DuplicateMenu(item);
421 autoMenu->Append(wxID_ANY, label, new_item);
422 }
423
424 SetRightMenu(wxID_ANY, _("Menu"), autoMenu);
425 }
426
427#elif defined(WINCE_WITHOUT_COMMANDBAR)
428 if (!GetToolBar())
429 {
430 wxToolMenuBar* toolBar = new wxToolMenuBar(this, wxID_ANY,
431 wxDefaultPosition, wxDefaultSize,
432 wxBORDER_NONE | wxTB_HORIZONTAL,
433 wxToolBarNameStr, menubar);
434 SetToolBar(toolBar);
435 menubar->SetToolBar(toolBar);
436 }
437 // Now adjust size for menu bar
438 int menuHeight = 26;
439
440 //When the main window is created using CW_USEDEFAULT the height of the
441 // is created is not taken into account). So we resize the window after
442 // if a menubar is present
443 {
444 RECT rc;
445 ::GetWindowRect((HWND) GetHWND(), &rc);
446 // adjust for menu / titlebar height
447 rc.bottom -= (2*menuHeight-1);
448
449 ::MoveWindow((HWND) GetHWND(), rc.left, rc.top, rc.right, rc.bottom, FALSE);
450 }
451#endif
452
453 wxFrameBase::AttachMenuBar(menubar);
454
455 if ( !menubar )
456 {
457 // actually remove the menu from the frame
458 m_hMenu = (WXHMENU)0;
459 InternalSetMenuBar();
460 }
461 else // set new non NULL menu bar
462 {
463#if !defined(__WXWINCE__) || defined(WINCE_WITH_COMMANDBAR)
464 // Can set a menubar several times.
465 if ( menubar->GetHMenu() )
466 {
467 m_hMenu = menubar->GetHMenu();
468 }
469 else // no HMENU yet
470 {
471 m_hMenu = menubar->Create();
472
473 if ( !m_hMenu )
474 {
475 wxFAIL_MSG( _T("failed to create menu bar") );
476 return;
477 }
478 }
479#endif
480 InternalSetMenuBar();
481 }
482}
483
484void wxFrame::InternalSetMenuBar()
485{
486#if defined(__WXMICROWIN__) || defined(__WXWINCE__)
487 // Nothing
488#else
489 if ( !::SetMenu(GetHwnd(), (HMENU)m_hMenu) )
490 {
491 wxLogLastError(wxT("SetMenu"));
492 }
493#endif
494}
495
496#endif // wxUSE_MENUS_NATIVE
497
498// Responds to colour changes, and passes event on to children.
499void wxFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
500{
501 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
502 Refresh();
503
504#if wxUSE_STATUSBAR
505 if ( m_frameStatusBar )
506 {
507 wxSysColourChangedEvent event2;
508 event2.SetEventObject( m_frameStatusBar );
509 m_frameStatusBar->HandleWindowEvent(event2);
510 }
511#endif // wxUSE_STATUSBAR
512
513 // Propagate the event to the non-top-level children
514 wxWindow::OnSysColourChanged(event);
515}
516
517// Pass true to show full screen, false to restore.
518bool wxFrame::ShowFullScreen(bool show, long style)
519{
520 // TODO-CE: add support for CE
521#if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
522 if ( IsFullScreen() == show )
523 return false;
524
525 if (show)
526 {
527 // zap the toolbar, menubar, and statusbar if needed
528 //
529 // TODO: hide commandbar for WINCE_WITH_COMMANDBAR
530#if wxUSE_TOOLBAR
531 wxToolBar *theToolBar = GetToolBar();
532
533 if ((style & wxFULLSCREEN_NOTOOLBAR) && theToolBar)
534 {
535 if ( theToolBar->IsShown() )
536 {
537 theToolBar->SetSize(wxDefaultCoord,0);
538 theToolBar->Show(false);
539 }
540 else // prevent it from being restored later
541 {
542 style &= ~wxFULLSCREEN_NOTOOLBAR;
543 }
544 }
545#endif // wxUSE_TOOLBAR
546
547 if (style & wxFULLSCREEN_NOMENUBAR)
548 SetMenu((HWND)GetHWND(), (HMENU) NULL);
549
550#if wxUSE_STATUSBAR
551 wxStatusBar *theStatusBar = GetStatusBar();
552
553 // Save the number of fields in the statusbar
554 if ((style & wxFULLSCREEN_NOSTATUSBAR) && theStatusBar)
555 {
556 if ( theStatusBar->IsShown() )
557 theStatusBar->Show(false);
558 else
559 style &= ~wxFULLSCREEN_NOSTATUSBAR;
560 }
561#endif // wxUSE_STATUSBAR
562 }
563 else // restore to normal
564 {
565 // restore the toolbar, menubar, and statusbar if we had hid them
566#if wxUSE_TOOLBAR
567 wxToolBar *theToolBar = GetToolBar();
568
569 if ((m_fsStyle & wxFULLSCREEN_NOTOOLBAR) && theToolBar)
570 {
571 theToolBar->Show(true);
572 }
573#endif // wxUSE_TOOLBAR
574
575#if wxUSE_MENUS
576 if (m_fsStyle & wxFULLSCREEN_NOMENUBAR)
577 {
578 WXHMENU menu = m_hMenu;
579
580#if wxUSE_MDI_ARCHITECTURE
581 wxMDIParentFrame *frame = wxDynamicCast(this, wxMDIParentFrame);
582 if (frame)
583 {
584 wxMDIChildFrame *child = frame->GetActiveChild();
585 if (child)
586 {
587 menu = child->GetWinMenu();
588 }
589 }
590#endif // wxUSE_MDI_ARCHITECTURE
591
592 if (menu)
593 {
594 ::SetMenu(GetHwnd(), (HMENU)menu);
595 }
596 }
597#endif // wxUSE_MENUS
598
599#if wxUSE_STATUSBAR
600 wxStatusBar *theStatusBar = GetStatusBar();
601
602 if ((m_fsStyle & wxFULLSCREEN_NOSTATUSBAR) && theStatusBar)
603 {
604 theStatusBar->Show(true);
605 PositionStatusBar();
606 }
607#endif // wxUSE_STATUSBAR
608 }
609#endif // !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
610
611 return wxFrameBase::ShowFullScreen(show, style);
612}
613
614// ----------------------------------------------------------------------------
615// tool/status bar stuff
616// ----------------------------------------------------------------------------
617
618#if wxUSE_TOOLBAR
619
620wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
621{
622#if defined(WINCE_WITHOUT_COMMANDBAR)
623 // We may already have a toolbar from calling SetMenuBar.
624 if (GetToolBar())
625 return GetToolBar();
626#endif
627 if ( wxFrameBase::CreateToolBar(style, id, name) )
628 {
629 PositionToolBar();
630 }
631
632 return m_frameToolBar;
633}
634
635void wxFrame::PositionToolBar()
636{
637 // TODO: we want to do something different in WinCE, because the toolbar
638 // should be associated with the commandbar, instead of being
639 // independent window.
640#if !defined(WINCE_WITHOUT_COMMANDBAR)
641 wxToolBar *toolbar = GetToolBar();
642 if ( toolbar && toolbar->IsShown() )
643 {
644 // don't call our (or even wxTopLevelWindow) version because we want
645 // the real (full) client area size, not excluding the tool/status bar
646 int width, height;
647 wxWindow::DoGetClientSize(&width, &height);
648
649#if wxUSE_STATUSBAR
650 wxStatusBar *statbar = GetStatusBar();
651 if ( statbar && statbar->IsShown() )
652 {
653 height -= statbar->GetClientSize().y;
654 }
655#endif // wxUSE_STATUSBAR
656
657 int tx, ty, tw, th;
658 toolbar->GetPosition( &tx, &ty );
659 toolbar->GetSize( &tw, &th );
660
661 int x = 0, y = 0;
662 if ( toolbar->HasFlag(wxTB_BOTTOM) )
663 {
664 x = 0;
665 y = height - th;
666 }
667 else if ( toolbar->HasFlag(wxTB_RIGHT) )
668 {
669 x = width - tw;
670 y = 0;
671 }
672 else // left or top
673 {
674 x = 0;
675 y = 0;
676 }
677
678#if defined(WINCE_WITH_COMMANDBAR)
679 // We're using a commandbar - so we have to allow for it.
680 if (GetMenuBar() && GetMenuBar()->GetCommandBar())
681 {
682 RECT rect;
683 ::GetWindowRect((HWND) GetMenuBar()->GetCommandBar(), &rect);
684 y = rect.bottom - rect.top;
685 }
686#endif // WINCE_WITH_COMMANDBAR
687
688 if ( toolbar->HasFlag(wxTB_BOTTOM) )
689 {
690 if ( ty < 0 && ( -ty == th ) )
691 ty = height - th;
692 if ( tx < 0 && (-tx == tw ) )
693 tx = 0;
694 }
695 else if ( toolbar->HasFlag(wxTB_RIGHT) )
696 {
697 if( ty < 0 && ( -ty == th ) )
698 ty = 0;
699 if( tx < 0 && ( -tx == tw ) )
700 tx = width - tw;
701 }
702 else // left or top
703 {
704 if (ty < 0 && (-ty == th))
705 ty = 0;
706 if (tx < 0 && (-tx == tw))
707 tx = 0;
708 }
709
710 int desiredW = tw;
711 int desiredH = th;
712
713 if ( toolbar->IsVertical() )
714 {
715 desiredH = height;
716 }
717 else
718 {
719 desiredW = width;
720 }
721
722 // use the 'real' MSW position here, don't offset relativly to the
723 // client area origin
724
725 // Optimise such that we don't have to always resize the toolbar
726 // when the frame changes, otherwise we'll get a lot of flicker.
727 bool heightChanging wxDUMMY_INITIALIZE(true);
728 bool widthChanging wxDUMMY_INITIALIZE(true);
729
730 if ( toolbar->IsVertical() )
731 {
732 // It's OK if the current height is greater than what can be shown.
733 heightChanging = (desiredH > th) ;
734 widthChanging = (desiredW != tw) ;
735
736 // The next time around, we may not have to set the size
737 if (heightChanging)
738 desiredH = desiredH + 200;
739 }
740 else
741 {
742 // It's OK if the current width is greater than what can be shown.
743 widthChanging = (desiredW > tw) ;
744 heightChanging = (desiredH != th) ;
745
746 // The next time around, we may not have to set the size
747 if (widthChanging)
748 desiredW = desiredW + 200;
749 }
750
751 if (tx != 0 || ty != 0 || widthChanging || heightChanging)
752 toolbar->SetSize(x, y, desiredW, desiredH, wxSIZE_NO_ADJUSTMENTS);
753
754 }
755#endif // !WINCE_WITH_COMMANDBAR
756}
757
758#endif // wxUSE_TOOLBAR
759
760// ----------------------------------------------------------------------------
761// frame state (iconized/maximized/...)
762// ----------------------------------------------------------------------------
763
764// propagate our state change to all child frames: this allows us to emulate X
765// Windows behaviour where child frames float independently of the parent one
766// on the desktop, but are iconized/restored with it
767void wxFrame::IconizeChildFrames(bool bIconize)
768{
769 m_iconized = bIconize;
770
771 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
772 node;
773 node = node->GetNext() )
774 {
775 wxWindow *win = node->GetData();
776
777 // iconizing the frames with this style under Win95 shell puts them at
778 // the bottom of the screen (as the MDI children) instead of making
779 // them appear in the taskbar because they are, by virtue of this
780 // style, not managed by the taskbar - instead leave Windows take care
781 // of them
782 if ( win->GetWindowStyle() & wxFRAME_TOOL_WINDOW )
783 continue;
784
785 // the child MDI frames are a special case and should not be touched by
786 // the parent frame - instead, they are managed by the user
787 wxFrame *frame = wxDynamicCast(win, wxFrame);
788 if ( frame
789#if wxUSE_MDI_ARCHITECTURE
790 && !frame->IsMDIChild()
791#endif // wxUSE_MDI_ARCHITECTURE
792 )
793 {
794 // we don't want to restore the child frames which had been
795 // iconized even before we were iconized, so save the child frame
796 // status when iconizing the parent frame and check it when
797 // restoring it
798 if ( bIconize )
799 {
800 frame->m_wasMinimized = frame->IsIconized();
801 }
802
803 // note that we shouldn't touch the hidden frames neither because
804 // iconizing/restoring them would show them as a side effect
805 if ( !frame->m_wasMinimized && frame->IsShown() )
806 frame->Iconize(bIconize);
807 }
808 }
809}
810
811WXHICON wxFrame::GetDefaultIcon() const
812{
813 // we don't have any standard icons (any more)
814 return (WXHICON)0;
815}
816
817// ===========================================================================
818// message processing
819// ===========================================================================
820
821// ---------------------------------------------------------------------------
822// preprocessing
823// ---------------------------------------------------------------------------
824
825bool wxFrame::MSWDoTranslateMessage(wxFrame *frame, WXMSG *pMsg)
826{
827 if ( wxWindow::MSWTranslateMessage(pMsg) )
828 return true;
829
830#if wxUSE_MENUS && wxUSE_ACCEL && !defined(__WXUNIVERSAL__)
831 // try the menu bar accels
832 wxMenuBar *menuBar = GetMenuBar();
833 if ( menuBar )
834 {
835 const wxAcceleratorTable& acceleratorTable = menuBar->GetAccelTable();
836 return acceleratorTable.Translate(frame, pMsg);
837 }
838#endif // wxUSE_MENUS && wxUSE_ACCEL
839
840 return false;
841}
842
843// ---------------------------------------------------------------------------
844// our private (non virtual) message handlers
845// ---------------------------------------------------------------------------
846
847bool wxFrame::HandleSize(int WXUNUSED(x), int WXUNUSED(y), WXUINT id)
848{
849#if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
850 switch ( id )
851 {
852 case SIZE_RESTORED:
853 case SIZE_MAXIMIZED:
854 // only do it it if we were iconized before, otherwise resizing the
855 // parent frame has a curious side effect of bringing it under it's
856 // children
857 if ( !m_iconized )
858 break;
859
860 // restore all child frames too
861 IconizeChildFrames(false);
862
863 (void)SendIconizeEvent(false);
864 break;
865
866 case SIZE_MINIMIZED:
867 // iconize all child frames too
868 IconizeChildFrames(true);
869 break;
870 }
871#else
872 wxUnusedVar(id);
873#endif // !__WXWINCE__
874
875 if ( !m_iconized )
876 {
877#if wxUSE_STATUSBAR
878 PositionStatusBar();
879#endif // wxUSE_STATUSBAR
880
881#if wxUSE_TOOLBAR
882 PositionToolBar();
883#endif // wxUSE_TOOLBAR
884
885#if defined(WINCE_WITH_COMMANDBAR)
886 // Position the menu command bar
887 if (GetMenuBar() && GetMenuBar()->GetCommandBar())
888 {
889 RECT rect;
890 ::GetWindowRect((HWND) GetMenuBar()->GetCommandBar(), &rect);
891 wxSize clientSz = GetClientSize();
892
893 if ( !::MoveWindow((HWND) GetMenuBar()->GetCommandBar(), 0, 0, clientSz.x, rect.bottom - rect.top, true ) )
894 {
895 wxLogLastError(wxT("MoveWindow"));
896 }
897
898 }
899#endif // WINCE_WITH_COMMANDBAR
900 }
901
902 // call the base class version to generate the appropriate events
903 return false;
904}
905
906bool wxFrame::HandleCommand(WXWORD id_, WXWORD cmd, WXHWND control)
907{
908 // we only need to handle the menu and accelerator commands from the items
909 // of our menu bar, base wxWindow class already handles the rest
910 if ( !control && (cmd == 0 /* menu */ || cmd == 1 /* accel */) )
911 {
912#if wxUSE_MENUS_NATIVE
913 if ( !wxCurrentPopupMenu )
914#endif // wxUSE_MENUS_NATIVE
915 {
916 wxMenuBar * const mbar = GetMenuBar();
917 if ( mbar )
918 {
919 // sign extend to int from short before comparing with the
920 // other int ids
921 const int id = (signed short)id_;
922
923 wxMenuItem * const mitem = mbar->FindItem(id);
924 if ( mitem )
925 return ProcessCommand(mitem);
926 }
927 }
928 }
929
930 return wxFrameBase::HandleCommand(id_, cmd, control);;
931}
932
933#if wxUSE_MENUS
934
935bool wxFrame::HandleMenuSelect(WXWORD nItem, WXWORD flags, WXHMENU hMenu)
936{
937 int item;
938 if ( flags == 0xFFFF && hMenu == 0 )
939 {
940 // menu was removed from screen
941 item = -1;
942 }
943#ifndef __WXMICROWIN__
944 else if ( !(flags & MF_POPUP) && !(flags & MF_SEPARATOR) )
945 {
946 item = nItem;
947 }
948#endif
949 else
950 {
951 // don't give hints for separators (doesn't make sense) nor for the
952 // items opening popup menus (they don't have them anyhow) but do clear
953 // the status line - otherwise, we would be left with the help message
954 // for the previous item which doesn't apply any more
955 DoGiveHelp(wxEmptyString, true);
956
957 return false;
958 }
959
960 wxMenuEvent event(wxEVT_MENU_HIGHLIGHT, item);
961 event.SetEventObject(this);
962
963 return HandleWindowEvent(event);
964}
965
966bool wxFrame::HandleMenuLoop(const wxEventType& evtType, WXWORD isPopup)
967{
968 // we don't have the menu id here, so we use the id to specify if the event
969 // was from a popup menu or a normal one
970 wxMenuEvent event(evtType, isPopup ? -1 : 0);
971 event.SetEventObject(this);
972
973 return HandleWindowEvent(event);
974}
975
976bool wxFrame::HandleInitMenuPopup(WXHMENU hMenu)
977{
978 wxMenu* menu = NULL;
979 if (GetMenuBar())
980 {
981 int nCount = GetMenuBar()->GetMenuCount();
982 for (int n = 0; n < nCount; n++)
983 {
984 if (GetMenuBar()->GetMenu(n)->GetHMenu() == hMenu)
985 {
986 menu = GetMenuBar()->GetMenu(n);
987 break;
988 }
989 }
990 }
991
992 wxMenuEvent event(wxEVT_MENU_OPEN, 0, menu);
993 event.SetEventObject(this);
994
995 return HandleWindowEvent(event);
996}
997
998#endif // wxUSE_MENUS
999
1000// ---------------------------------------------------------------------------
1001// the window proc for wxFrame
1002// ---------------------------------------------------------------------------
1003
1004WXLRESULT wxFrame::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
1005{
1006 WXLRESULT rc = 0;
1007 bool processed = false;
1008
1009 switch ( message )
1010 {
1011 case WM_CLOSE:
1012 // if we can't close, tell the system that we processed the
1013 // message - otherwise it would close us
1014 processed = !Close();
1015 break;
1016
1017 case WM_SIZE:
1018 processed = HandleSize(LOWORD(lParam), HIWORD(lParam), wParam);
1019 break;
1020
1021 case WM_COMMAND:
1022 {
1023 WORD id, cmd;
1024 WXHWND hwnd;
1025 UnpackCommand((WXWPARAM)wParam, (WXLPARAM)lParam,
1026 &id, &hwnd, &cmd);
1027
1028 HandleCommand(id, cmd, (WXHWND)hwnd);
1029
1030 // don't pass WM_COMMAND to the base class whether we processed
1031 // it or not because we did generate an event for it (our
1032 // HandleCommand() calls the base class version) and we must
1033 // not do it again or the handlers which skip the event would
1034 // be called twice
1035 processed = true;
1036 }
1037 break;
1038
1039#if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
1040#if wxUSE_MENUS
1041 case WM_INITMENUPOPUP:
1042 processed = HandleInitMenuPopup((WXHMENU) wParam);
1043 break;
1044
1045 case WM_MENUSELECT:
1046 {
1047 WXWORD item, flags;
1048 WXHMENU hmenu;
1049 UnpackMenuSelect(wParam, lParam, &item, &flags, &hmenu);
1050
1051 processed = HandleMenuSelect(item, flags, hmenu);
1052 }
1053 break;
1054
1055 case WM_EXITMENULOOP:
1056 processed = HandleMenuLoop(wxEVT_MENU_CLOSE, (WXWORD)wParam);
1057 break;
1058#endif // wxUSE_MENUS
1059
1060 case WM_QUERYDRAGICON:
1061 {
1062 const wxIcon& icon = GetIcon();
1063 HICON hIcon = icon.Ok() ? GetHiconOf(icon)
1064 : (HICON)GetDefaultIcon();
1065 rc = (WXLRESULT)hIcon;
1066 processed = rc != 0;
1067 }
1068 break;
1069#endif // !__WXMICROWIN__
1070 }
1071
1072 if ( !processed )
1073 rc = wxFrameBase::MSWWindowProc(message, wParam, lParam);
1074
1075 return rc;
1076}
1077
1078// ----------------------------------------------------------------------------
1079// wxFrame size management: we exclude the areas taken by menu/status/toolbars
1080// from the client area, so the client area is what's really available for the
1081// frame contents
1082// ----------------------------------------------------------------------------
1083
1084// get the origin of the client area in the client coordinates
1085wxPoint wxFrame::GetClientAreaOrigin() const
1086{
1087 wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin();
1088
1089#if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__) && \
1090 (!defined(__WXWINCE__) || (_WIN32_WCE >= 400 && !defined(__POCKETPC__) && !defined(__SMARTPHONE__)))
1091 wxToolBar * const toolbar = GetToolBar();
1092 if ( toolbar && toolbar->IsShown() )
1093 {
1094 const wxSize sizeTB = toolbar->GetSize();
1095
1096 if ( toolbar->HasFlag(wxTB_TOP) )
1097 {
1098 pt.y += sizeTB.y;
1099 }
1100 else if ( toolbar->HasFlag(wxTB_LEFT) )
1101 {
1102 pt.x += sizeTB.x;
1103 }
1104 }
1105#endif // wxUSE_TOOLBAR
1106
1107#if defined(WINCE_WITH_COMMANDBAR)
1108 if (GetMenuBar() && GetMenuBar()->GetCommandBar())
1109 {
1110 RECT rect;
1111 ::GetWindowRect((HWND) GetMenuBar()->GetCommandBar(), &rect);
1112 pt.y += (rect.bottom - rect.top);
1113 }
1114#endif
1115
1116 return pt;
1117}