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