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