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