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