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