many fixes to toolbar positioning code
[wxWidgets.git] / src / msw / frame.cpp
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
72 BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
73 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged)
74 END_EVENT_TABLE()
75
76 #if wxUSE_EXTENDED_RTTI
77 WX_DEFINE_FLAGS( wxFrameStyle )
78
79 wxBEGIN_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
127 wxEND_FLAGS( wxFrameStyle )
128
129 IMPLEMENT_DYNAMIC_CLASS_XTI(wxFrame, wxTopLevelWindow,"wx/frame.h")
130
131 wxBEGIN_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"))
137 wxEND_PROPERTIES_TABLE()
138
139 wxBEGIN_HANDLERS_TABLE(wxFrame)
140 wxEND_HANDLERS_TABLE()
141
142 wxCONSTRUCTOR_6( wxFrame , wxWindow* , Parent , wxWindowID , Id , wxString , Title , wxPoint , Position , wxSize , Size , long , WindowStyle)
143
144 #else
145 IMPLEMENT_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
168 void 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
181 bool 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
210 wxFrame::~wxFrame()
211 {
212 m_isBeingDeleted = true;
213 DeleteAllBars();
214 }
215
216 // ----------------------------------------------------------------------------
217 // wxFrame client size calculations
218 // ----------------------------------------------------------------------------
219
220 void 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.
256 void 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
307 void wxFrame::Raise()
308 {
309 ::SetForegroundWindow(GetHwnd());
310 }
311
312 // generate an artificial resize event
313 void wxFrame::SendSizeEvent()
314 {
315 if ( !m_iconized )
316 {
317 RECT r = wxGetWindowRect(GetHwnd());
318
319 (void)::PostMessage(GetHwnd(), WM_SIZE,
320 IsMaximized() ? SIZE_MAXIMIZED : SIZE_RESTORED,
321 MAKELPARAM(r.right - r.left, r.bottom - r.top));
322 }
323 }
324
325 #if wxUSE_STATUSBAR
326 wxStatusBar *wxFrame::OnCreateStatusBar(int number,
327 long style,
328 wxWindowID id,
329 const wxString& name)
330 {
331 wxStatusBar *statusBar wxDUMMY_INITIALIZE(NULL);
332
333 #if wxUSE_NATIVE_STATUSBAR
334 if ( !UsesNativeStatusBar() )
335 {
336 statusBar = (wxStatusBar *)new wxStatusBarGeneric(this, id, style);
337 }
338 else
339 #endif
340 {
341 statusBar = new wxStatusBar(this, id, style, name);
342 }
343
344 statusBar->SetFieldsCount(number);
345
346 return statusBar;
347 }
348
349 void wxFrame::PositionStatusBar()
350 {
351 if ( !m_frameStatusBar || !m_frameStatusBar->IsShown() )
352 return;
353
354 int w, h;
355 GetClientSize(&w, &h);
356
357 int sw, sh;
358 m_frameStatusBar->GetSize(&sw, &sh);
359
360 // Since we wish the status bar to be directly under the client area,
361 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
362 m_frameStatusBar->SetSize(0, h, w, sh);
363 }
364
365 #endif // wxUSE_STATUSBAR
366
367 #if wxUSE_MENUS_NATIVE
368
369 void wxFrame::AttachMenuBar(wxMenuBar *menubar)
370 {
371 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
372
373 wxMenu *autoMenu = NULL;
374
375 if( menubar->GetMenuCount() == 1 )
376 {
377 autoMenu = wxTopLevelWindowMSW::ButtonMenu::DuplicateMenu(menubar->GetMenu(0));
378 SetRightMenu(wxID_ANY, menubar->GetLabelTop(0), autoMenu);
379 }
380 else
381 {
382 autoMenu = new wxMenu;
383
384 for( size_t n = 0; n < menubar->GetMenuCount(); n++ )
385 {
386 wxMenu *item = menubar->GetMenu(n);
387 wxString label = menubar->GetLabelTop(n);
388 wxMenu *new_item = wxTopLevelWindowMSW::ButtonMenu::DuplicateMenu(item);
389 autoMenu->Append(wxID_ANY, label, new_item);
390 }
391
392 SetRightMenu(wxID_ANY, _("Menu"), autoMenu);
393 }
394
395 #elif defined(WINCE_WITHOUT_COMMANDBAR)
396 if (!GetToolBar())
397 {
398 wxToolMenuBar* toolBar = new wxToolMenuBar(this, wxID_ANY,
399 wxDefaultPosition, wxDefaultSize,
400 wxBORDER_NONE | wxTB_HORIZONTAL,
401 wxToolBarNameStr, menubar);
402 SetToolBar(toolBar);
403 menubar->SetToolBar(toolBar);
404 }
405 // Now adjust size for menu bar
406 int menuHeight = 26;
407
408 //When the main window is created using CW_USEDEFAULT the height of the
409 // is created is not taken into account). So we resize the window after
410 // if a menubar is present
411 {
412 RECT rc;
413 ::GetWindowRect((HWND) GetHWND(), &rc);
414 // adjust for menu / titlebar height
415 rc.bottom -= (2*menuHeight-1);
416
417 ::MoveWindow((HWND) GetHWND(), rc.left, rc.top, rc.right, rc.bottom, FALSE);
418 }
419 #endif
420
421 wxFrameBase::AttachMenuBar(menubar);
422
423 if ( !menubar )
424 {
425 // actually remove the menu from the frame
426 m_hMenu = (WXHMENU)0;
427 InternalSetMenuBar();
428 }
429 else // set new non NULL menu bar
430 {
431 #if !defined(__WXWINCE__) || defined(WINCE_WITH_COMMANDBAR)
432 // Can set a menubar several times.
433 if ( menubar->GetHMenu() )
434 {
435 m_hMenu = menubar->GetHMenu();
436 }
437 else // no HMENU yet
438 {
439 m_hMenu = menubar->Create();
440
441 if ( !m_hMenu )
442 {
443 wxFAIL_MSG( _T("failed to create menu bar") );
444 return;
445 }
446 }
447 #endif
448 InternalSetMenuBar();
449 }
450 }
451
452 void wxFrame::InternalSetMenuBar()
453 {
454 #if defined(__WXMICROWIN__) || defined(__WXWINCE__)
455 // Nothing
456 #else
457 if ( !::SetMenu(GetHwnd(), (HMENU)m_hMenu) )
458 {
459 wxLogLastError(wxT("SetMenu"));
460 }
461 #endif
462 }
463
464 #endif // wxUSE_MENUS_NATIVE
465
466 // Responds to colour changes, and passes event on to children.
467 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
468 {
469 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
470 Refresh();
471
472 #if wxUSE_STATUSBAR
473 if ( m_frameStatusBar )
474 {
475 wxSysColourChangedEvent event2;
476 event2.SetEventObject( m_frameStatusBar );
477 m_frameStatusBar->GetEventHandler()->ProcessEvent(event2);
478 }
479 #endif // wxUSE_STATUSBAR
480
481 // Propagate the event to the non-top-level children
482 wxWindow::OnSysColourChanged(event);
483 }
484
485 // Pass true to show full screen, false to restore.
486 bool wxFrame::ShowFullScreen(bool show, long style)
487 {
488 // TODO-CE: add support for CE
489 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
490 if ( IsFullScreen() == show )
491 return false;
492
493 if (show)
494 {
495 // zap the toolbar, menubar, and statusbar if needed
496 //
497 // TODO: hide commandbar for WINCE_WITH_COMMANDBAR
498 #if wxUSE_TOOLBAR
499 wxToolBar *theToolBar = GetToolBar();
500
501 if ((style & wxFULLSCREEN_NOTOOLBAR) && theToolBar)
502 {
503 if ( theToolBar->IsShown() )
504 {
505 theToolBar->SetSize(wxDefaultCoord,0);
506 theToolBar->Show(false);
507 }
508 else // prevent it from being restored later
509 {
510 style &= ~wxFULLSCREEN_NOTOOLBAR;
511 }
512 }
513 #endif // wxUSE_TOOLBAR
514
515 if (style & wxFULLSCREEN_NOMENUBAR)
516 SetMenu((HWND)GetHWND(), (HMENU) NULL);
517
518 #if wxUSE_STATUSBAR
519 wxStatusBar *theStatusBar = GetStatusBar();
520
521 // Save the number of fields in the statusbar
522 if ((style & wxFULLSCREEN_NOSTATUSBAR) && theStatusBar)
523 {
524 if ( theStatusBar->IsShown() )
525 theStatusBar->Show(false);
526 else
527 style &= ~wxFULLSCREEN_NOSTATUSBAR;
528 }
529 #endif // wxUSE_STATUSBAR
530 }
531 else // restore to normal
532 {
533 // restore the toolbar, menubar, and statusbar if we had hid them
534 #if wxUSE_TOOLBAR
535 wxToolBar *theToolBar = GetToolBar();
536
537 if ((m_fsStyle & wxFULLSCREEN_NOTOOLBAR) && theToolBar)
538 {
539 theToolBar->Show(true);
540 }
541 #endif // wxUSE_TOOLBAR
542
543 if (m_fsStyle & wxFULLSCREEN_NOMENUBAR)
544 {
545 WXHMENU menu = m_hMenu;
546
547 #if wxUSE_MDI_ARCHITECTURE
548 wxMDIParentFrame *frame = wxDynamicCast(this, wxMDIParentFrame);
549 if (frame)
550 {
551 wxMDIChildFrame *child = frame->GetActiveChild();
552 if (child)
553 {
554 menu = child->GetWinMenu();
555 }
556 }
557 #endif // wxUSE_MDI_ARCHITECTURE
558
559 if (menu)
560 {
561 ::SetMenu(GetHwnd(), (HMENU)menu);
562 }
563 }
564
565 #if wxUSE_STATUSBAR
566 wxStatusBar *theStatusBar = GetStatusBar();
567
568 if ((m_fsStyle & wxFULLSCREEN_NOSTATUSBAR) && theStatusBar)
569 {
570 theStatusBar->Show(true);
571 PositionStatusBar();
572 }
573 #endif // wxUSE_STATUSBAR
574 }
575 #endif // !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
576
577 return wxFrameBase::ShowFullScreen(show, style);
578 }
579
580 // ----------------------------------------------------------------------------
581 // tool/status bar stuff
582 // ----------------------------------------------------------------------------
583
584 #if wxUSE_TOOLBAR
585
586 wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
587 {
588 #if defined(WINCE_WITHOUT_COMMANDBAR)
589 // We may already have a toolbar from calling SetMenuBar.
590 if (GetToolBar())
591 return GetToolBar();
592 #endif
593 if ( wxFrameBase::CreateToolBar(style, id, name) )
594 {
595 PositionToolBar();
596 }
597
598 return m_frameToolBar;
599 }
600
601 void wxFrame::PositionToolBar()
602 {
603 int x = 0, y = 0;
604 wxToolBar *toolbar = GetToolBar();
605 if ( toolbar && toolbar->IsShown() )
606 {
607 #if defined(WINCE_WITHOUT_COMMANDBAR)
608 // We want to do something different in WinCE, because
609 // the toolbar should be associated with the commandbar,
610 // and not an independent window.
611 // TODO
612 #else
613 // don't call our (or even wxTopLevelWindow) version because we want
614 // the real (full) client area size, not excluding the tool/status bar
615 int width, height;
616 wxWindow::DoGetClientSize(&width, &height);
617
618 #if wxUSE_STATUSBAR
619 wxStatusBar *statbar = GetStatusBar();
620 if ( statbar && statbar->IsShown() )
621 {
622 height -= statbar->GetClientSize().y;
623 }
624 #endif // wxUSE_STATUSBAR
625
626 int tx, ty, tw, th;
627 toolbar->GetPosition( &tx, &ty );
628 toolbar->GetSize( &tw, &th );
629
630 if ( toolbar->HasFlag(wxTB_BOTTOM) )
631 {
632 x = 0;
633 y = height - th;
634 }
635 else if ( toolbar->HasFlag(wxTB_RIGHT) )
636 {
637 x = width - tw;
638 y = 0;
639 }
640 else // left or top
641 {
642 x = 0;
643 y = 0;
644 }
645
646 #if defined(WINCE_WITH_COMMANDBAR)
647 // We're using a commandbar - so we have to allow for it.
648 if (GetMenuBar() && GetMenuBar()->GetCommandBar())
649 {
650 RECT rect;
651 ::GetWindowRect((HWND) GetMenuBar()->GetCommandBar(), &rect);
652 y = rect.bottom - rect.top;
653 }
654 #endif // WINCE_WITH_COMMANDBAR
655
656 if ( toolbar->HasFlag(wxTB_BOTTOM) )
657 {
658 if ( ty < 0 && ( -ty == th ) )
659 ty = height - th;
660 if ( tx < 0 && (-tx == tw ) )
661 tx = 0;
662 }
663 else if ( toolbar->HasFlag(wxTB_RIGHT) )
664 {
665 if( ty < 0 && ( -ty == th ) )
666 ty = 0;
667 if( tx < 0 && ( -tx == tw ) )
668 tx = width - tw;
669 }
670 else // left or top
671 {
672 if (ty < 0 && (-ty == th))
673 ty = 0;
674 if (tx < 0 && (-tx == tw))
675 tx = 0;
676 }
677
678 int desiredW = tw;
679 int desiredH = th;
680
681 if ( toolbar->IsVertical() )
682 {
683 desiredH = height;
684 }
685 else
686 {
687 desiredW = width;
688 }
689
690 // use the 'real' MSW position here, don't offset relativly to the
691 // client area origin
692
693 // Optimise such that we don't have to always resize the toolbar
694 // when the frame changes, otherwise we'll get a lot of flicker.
695 bool heightChanging wxDUMMY_INITIALIZE(true);
696 bool widthChanging wxDUMMY_INITIALIZE(true);
697
698 if ( toolbar->IsVertical() )
699 {
700 // It's OK if the current height is greater than what can be shown.
701 heightChanging = (desiredH > th) ;
702 widthChanging = (desiredW != tw) ;
703
704 // The next time around, we may not have to set the size
705 if (heightChanging)
706 desiredH = desiredH + 200;
707 }
708 else
709 {
710 // It's OK if the current width is greater than what can be shown.
711 widthChanging = (desiredW > tw) ;
712 heightChanging = (desiredH != th) ;
713
714 // The next time around, we may not have to set the size
715 if (widthChanging)
716 desiredW = desiredW + 200;
717 }
718
719 if (tx != 0 || ty != 0 || widthChanging || heightChanging)
720 toolbar->SetSize(x, y, desiredW, desiredH, wxSIZE_NO_ADJUSTMENTS);
721
722 #endif // __WXWINCE__
723 }
724 }
725
726 #endif // wxUSE_TOOLBAR
727
728 // ----------------------------------------------------------------------------
729 // frame state (iconized/maximized/...)
730 // ----------------------------------------------------------------------------
731
732 // propagate our state change to all child frames: this allows us to emulate X
733 // Windows behaviour where child frames float independently of the parent one
734 // on the desktop, but are iconized/restored with it
735 void wxFrame::IconizeChildFrames(bool bIconize)
736 {
737 m_iconized = bIconize;
738
739 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
740 node;
741 node = node->GetNext() )
742 {
743 wxWindow *win = node->GetData();
744
745 // iconizing the frames with this style under Win95 shell puts them at
746 // the bottom of the screen (as the MDI children) instead of making
747 // them appear in the taskbar because they are, by virtue of this
748 // style, not managed by the taskbar - instead leave Windows take care
749 // of them
750 if ( win->GetWindowStyle() & wxFRAME_TOOL_WINDOW )
751 continue;
752
753 // the child MDI frames are a special case and should not be touched by
754 // the parent frame - instead, they are managed by the user
755 wxFrame *frame = wxDynamicCast(win, wxFrame);
756 if ( frame
757 #if wxUSE_MDI_ARCHITECTURE
758 && !frame->IsMDIChild()
759 #endif // wxUSE_MDI_ARCHITECTURE
760 )
761 {
762 // we don't want to restore the child frames which had been
763 // iconized even before we were iconized, so save the child frame
764 // status when iconizing the parent frame and check it when
765 // restoring it
766 if ( bIconize )
767 {
768 frame->m_wasMinimized = frame->IsIconized();
769 }
770
771 // note that we shouldn't touch the hidden frames neither because
772 // iconizing/restoring them would show them as a side effect
773 if ( !frame->m_wasMinimized && frame->IsShown() )
774 frame->Iconize(bIconize);
775 }
776 }
777 }
778
779 WXHICON wxFrame::GetDefaultIcon() const
780 {
781 // we don't have any standard icons (any more)
782 return (WXHICON)0;
783 }
784
785 // ===========================================================================
786 // message processing
787 // ===========================================================================
788
789 // ---------------------------------------------------------------------------
790 // preprocessing
791 // ---------------------------------------------------------------------------
792
793 bool wxFrame::MSWDoTranslateMessage(wxFrame *frame, WXMSG *pMsg)
794 {
795 if ( wxWindow::MSWTranslateMessage(pMsg) )
796 return true;
797
798 #if wxUSE_MENUS && wxUSE_ACCEL && !defined(__WXUNIVERSAL__)
799 // try the menu bar accels
800 wxMenuBar *menuBar = GetMenuBar();
801 if ( menuBar )
802 {
803 const wxAcceleratorTable& acceleratorTable = menuBar->GetAccelTable();
804 return acceleratorTable.Translate(frame, pMsg);
805 }
806 #endif // wxUSE_MENUS && wxUSE_ACCEL
807
808 return false;
809 }
810
811 // ---------------------------------------------------------------------------
812 // our private (non virtual) message handlers
813 // ---------------------------------------------------------------------------
814
815 bool wxFrame::HandlePaint()
816 {
817 RECT rect;
818 if ( ::GetUpdateRect(GetHwnd(), &rect, FALSE) )
819 {
820 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
821 if ( m_iconized )
822 {
823 const wxIcon& icon = GetIcon();
824 HICON hIcon = icon.Ok() ? GetHiconOf(icon)
825 : (HICON)GetDefaultIcon();
826
827 // Hold a pointer to the dc so long as the OnPaint() message
828 // is being processed
829 PAINTSTRUCT ps;
830 HDC hdc = ::BeginPaint(GetHwnd(), &ps);
831
832 // Erase background before painting or we get white background
833 MSWDefWindowProc(WM_ICONERASEBKGND, (WORD)(LONG)ps.hdc, 0L);
834
835 if ( hIcon )
836 {
837 RECT rect;
838 ::GetClientRect(GetHwnd(), &rect);
839
840 // FIXME: why hardcoded?
841 static const int icon_width = 32;
842 static const int icon_height = 32;
843
844 int icon_x = (int)((rect.right - icon_width)/2);
845 int icon_y = (int)((rect.bottom - icon_height)/2);
846
847 ::DrawIcon(hdc, icon_x, icon_y, hIcon);
848 }
849
850 ::EndPaint(GetHwnd(), &ps);
851
852 return true;
853 }
854 else
855 #endif
856 {
857 return wxWindow::HandlePaint();
858 }
859 }
860 else
861 {
862 // nothing to paint - processed
863 return true;
864 }
865 }
866
867 bool wxFrame::HandleSize(int WXUNUSED(x), int WXUNUSED(y), WXUINT id)
868 {
869 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
870 switch ( id )
871 {
872 case SIZE_RESTORED:
873 case SIZE_MAXIMIZED:
874 // only do it it if we were iconized before, otherwise resizing the
875 // parent frame has a curious side effect of bringing it under it's
876 // children
877 if ( !m_iconized )
878 break;
879
880 // restore all child frames too
881 IconizeChildFrames(false);
882
883 (void)SendIconizeEvent(false);
884 break;
885
886 case SIZE_MINIMIZED:
887 // iconize all child frames too
888 IconizeChildFrames(true);
889 break;
890 }
891 #else
892 wxUnusedVar(id);
893 #endif // !__WXWINCE__
894
895 if ( !m_iconized )
896 {
897 #if wxUSE_STATUSBAR
898 PositionStatusBar();
899 #endif // wxUSE_STATUSBAR
900
901 #if wxUSE_TOOLBAR
902 PositionToolBar();
903 #endif // wxUSE_TOOLBAR
904
905 #if defined(WINCE_WITH_COMMANDBAR)
906 // Position the menu command bar
907 if (GetMenuBar() && GetMenuBar()->GetCommandBar())
908 {
909 RECT rect;
910 ::GetWindowRect((HWND) GetMenuBar()->GetCommandBar(), &rect);
911 wxSize clientSz = GetClientSize();
912
913 if ( !::MoveWindow((HWND) GetMenuBar()->GetCommandBar(), 0, 0, clientSz.x, rect.bottom - rect.top, true ) )
914 {
915 wxLogLastError(wxT("MoveWindow"));
916 }
917
918 }
919 #endif // WINCE_WITH_COMMANDBAR
920 }
921
922 // call the base class version to generate the appropriate events
923 return false;
924 }
925
926 bool wxFrame::HandleCommand(WXWORD id, WXWORD cmd, WXHWND control)
927 {
928 if ( control )
929 {
930 // In case it's e.g. a toolbar.
931 wxWindow *win = wxFindWinFromHandle(control);
932 if ( win )
933 return win->MSWCommand(cmd, id);
934 }
935
936 // handle here commands from menus and accelerators
937 if ( cmd == 0 || cmd == 1 )
938 {
939 #if wxUSE_MENUS_NATIVE
940 if ( wxCurrentPopupMenu )
941 {
942 wxMenu *popupMenu = wxCurrentPopupMenu;
943 wxCurrentPopupMenu = NULL;
944
945 return popupMenu->MSWCommand(cmd, id);
946 }
947 #endif // wxUSE_MENUS_NATIVE
948
949 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
950 // handle here commands from Smartphone menu bar
951 if ( wxTopLevelWindow::HandleCommand(id, cmd, control ) )
952 {
953 return true;
954 }
955 #endif // __SMARTPHONE__ && __WXWINCE__
956
957 if ( ProcessCommand(id) )
958 {
959 return true;
960 }
961 }
962
963 return false;
964 }
965
966 bool wxFrame::HandleMenuSelect(WXWORD nItem, WXWORD flags, WXHMENU hMenu)
967 {
968 int item;
969 if ( flags == 0xFFFF && hMenu == 0 )
970 {
971 // menu was removed from screen
972 item = -1;
973 }
974 #ifndef __WXMICROWIN__
975 else if ( !(flags & MF_POPUP) && !(flags & MF_SEPARATOR) )
976 {
977 item = nItem;
978 }
979 #endif
980 else
981 {
982 // don't give hints for separators (doesn't make sense) nor for the
983 // items opening popup menus (they don't have them anyhow) but do clear
984 // the status line - otherwise, we would be left with the help message
985 // for the previous item which doesn't apply any more
986 DoGiveHelp(wxEmptyString, false);
987
988 return false;
989 }
990
991 wxMenuEvent event(wxEVT_MENU_HIGHLIGHT, item);
992 event.SetEventObject(this);
993
994 return GetEventHandler()->ProcessEvent(event);
995 }
996
997 bool wxFrame::HandleMenuLoop(const wxEventType& evtType, WXWORD isPopup)
998 {
999 // we don't have the menu id here, so we use the id to specify if the event
1000 // was from a popup menu or a normal one
1001 wxMenuEvent event(evtType, isPopup ? -1 : 0);
1002 event.SetEventObject(this);
1003
1004 return GetEventHandler()->ProcessEvent(event);
1005 }
1006
1007 // ---------------------------------------------------------------------------
1008 // the window proc for wxFrame
1009 // ---------------------------------------------------------------------------
1010
1011 WXLRESULT wxFrame::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
1012 {
1013 WXLRESULT rc = 0;
1014 bool processed = false;
1015
1016 switch ( message )
1017 {
1018 case WM_CLOSE:
1019 // if we can't close, tell the system that we processed the
1020 // message - otherwise it would close us
1021 processed = !Close();
1022 break;
1023
1024 case WM_SIZE:
1025 processed = HandleSize(LOWORD(lParam), HIWORD(lParam), wParam);
1026 break;
1027
1028 case WM_COMMAND:
1029 {
1030 WORD id, cmd;
1031 WXHWND hwnd;
1032 UnpackCommand((WXWPARAM)wParam, (WXLPARAM)lParam,
1033 &id, &hwnd, &cmd);
1034
1035 processed = HandleCommand(id, cmd, (WXHWND)hwnd);
1036 }
1037 break;
1038
1039 case WM_PAINT:
1040 processed = HandlePaint();
1041 break;
1042
1043 case WM_INITMENUPOPUP:
1044 processed = HandleInitMenuPopup((WXHMENU) wParam);
1045 break;
1046
1047 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
1048 case WM_MENUSELECT:
1049 {
1050 WXWORD item, flags;
1051 WXHMENU hmenu;
1052 UnpackMenuSelect(wParam, lParam, &item, &flags, &hmenu);
1053
1054 processed = HandleMenuSelect(item, flags, hmenu);
1055 }
1056 break;
1057
1058 case WM_EXITMENULOOP:
1059 processed = HandleMenuLoop(wxEVT_MENU_CLOSE, (WXWORD)wParam);
1060 break;
1061
1062 case WM_QUERYDRAGICON:
1063 {
1064 const wxIcon& icon = GetIcon();
1065 HICON hIcon = icon.Ok() ? GetHiconOf(icon)
1066 : (HICON)GetDefaultIcon();
1067 rc = (long)hIcon;
1068 processed = rc != 0;
1069 }
1070 break;
1071 #endif // !__WXMICROWIN__
1072 }
1073
1074 if ( !processed )
1075 rc = wxFrameBase::MSWWindowProc(message, wParam, lParam);
1076
1077 return rc;
1078 }
1079
1080 // handle WM_INITMENUPOPUP message
1081 bool wxFrame::HandleInitMenuPopup(WXHMENU hMenu)
1082 {
1083 wxMenu* menu = NULL;
1084 if (GetMenuBar())
1085 {
1086 int nCount = GetMenuBar()->GetMenuCount();
1087 for (int n = 0; n < nCount; n++)
1088 {
1089 if (GetMenuBar()->GetMenu(n)->GetHMenu() == hMenu)
1090 {
1091 menu = GetMenuBar()->GetMenu(n);
1092 break;
1093 }
1094 }
1095 }
1096
1097 wxMenuEvent event(wxEVT_MENU_OPEN, 0, menu);
1098 event.SetEventObject(this);
1099
1100 return GetEventHandler()->ProcessEvent(event);
1101 }
1102
1103 // ----------------------------------------------------------------------------
1104 // wxFrame size management: we exclude the areas taken by menu/status/toolbars
1105 // from the client area, so the client area is what's really available for the
1106 // frame contents
1107 // ----------------------------------------------------------------------------
1108
1109 // get the origin of the client area in the client coordinates
1110 wxPoint wxFrame::GetClientAreaOrigin() const
1111 {
1112 wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin();
1113
1114 #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__) && \
1115 (!defined(__WXWINCE__) || (_WIN32_WCE >= 400 && !defined(__POCKETPC__) && !defined(__SMARTPHONE__)))
1116 wxToolBar * const toolbar = GetToolBar();
1117 if ( toolbar && toolbar->IsShown() )
1118 {
1119 const wxSize sizeTB = toolbar->GetSize();
1120
1121 if ( toolbar->HasFlag(wxTB_TOP) )
1122 {
1123 pt.y += sizeTB.y;
1124 }
1125 else if ( toolbar->HasFlag(wxTB_LEFT) )
1126 {
1127 pt.x += sizeTB.x;
1128 }
1129 }
1130 #endif // wxUSE_TOOLBAR
1131
1132 #if defined(WINCE_WITH_COMMANDBAR)
1133 if (GetMenuBar() && GetMenuBar()->GetCommandBar())
1134 {
1135 RECT rect;
1136 ::GetWindowRect((HWND) GetMenuBar()->GetCommandBar(), &rect);
1137 pt.y += (rect.bottom - rect.top);
1138 }
1139 #endif
1140
1141 return pt;
1142 }