]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/frame.cpp
suppress an assert when a combobox receives WM_KILLFOCUS while it is being destroyed
[wxWidgets.git] / src / msw / frame.cpp
... / ...
CommitLineData
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 and Markus Holzem
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
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#if wxUSE_STATUSBAR
46 #include "wx/statusbr.h"
47 #include "wx/generic/statusbr.h"
48#endif // wxUSE_STATUSBAR
49
50#if wxUSE_TOOLBAR
51 #include "wx/toolbar.h"
52#endif // wxUSE_TOOLBAR
53
54#include "wx/menuitem.h"
55#include "wx/log.h"
56
57#ifdef __WXUNIVERSAL__
58 #include "wx/univ/theme.h"
59 #include "wx/univ/colschem.h"
60#endif // __WXUNIVERSAL__
61
62// ----------------------------------------------------------------------------
63// globals
64// ----------------------------------------------------------------------------
65
66#if wxUSE_MENUS_NATIVE
67 extern wxMenu *wxCurrentPopupMenu;
68#endif // wxUSE_MENUS_NATIVE
69
70// ----------------------------------------------------------------------------
71// event tables
72// ----------------------------------------------------------------------------
73
74BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
75 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged)
76END_EVENT_TABLE()
77
78IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxTopLevelWindow)
79
80// ============================================================================
81// implementation
82// ============================================================================
83
84// ----------------------------------------------------------------------------
85// static class members
86// ----------------------------------------------------------------------------
87
88#if wxUSE_STATUSBAR
89 #if wxUSE_NATIVE_STATUSBAR
90 bool wxFrame::m_useNativeStatusBar = TRUE;
91 #else
92 bool wxFrame::m_useNativeStatusBar = FALSE;
93 #endif
94#endif // wxUSE_NATIVE_STATUSBAR
95
96// ----------------------------------------------------------------------------
97// creation/destruction
98// ----------------------------------------------------------------------------
99
100void wxFrame::Init()
101{
102#if wxUSE_TOOLTIPS
103 m_hwndToolTip = 0;
104#endif
105
106 // Data to save/restore when calling ShowFullScreen
107 m_fsStatusBarFields = 0;
108 m_fsStatusBarHeight = 0;
109 m_fsToolBarHeight = 0;
110
111 m_wasMinimized = FALSE;
112}
113
114bool wxFrame::Create(wxWindow *parent,
115 wxWindowID id,
116 const wxString& title,
117 const wxPoint& pos,
118 const wxSize& size,
119 long style,
120 const wxString& name)
121{
122 if ( !wxTopLevelWindow::Create(parent, id, title, pos, size, style, name) )
123 return FALSE;
124
125 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
126
127 wxModelessWindows.Append(this);
128
129 return TRUE;
130}
131
132wxFrame::~wxFrame()
133{
134 m_isBeingDeleted = TRUE;
135
136 DeleteAllBars();
137}
138
139// ----------------------------------------------------------------------------
140// wxFrame client size calculations
141// ----------------------------------------------------------------------------
142
143void wxFrame::DoSetClientSize(int width, int height)
144{
145 // leave enough space for the status bar if we have (and show) it
146#if wxUSE_STATUSBAR
147 wxStatusBar *statbar = GetStatusBar();
148 if ( statbar && statbar->IsShown() )
149 {
150 height += statbar->GetSize().y;
151 }
152#endif // wxUSE_STATUSBAR
153
154 // call GetClientAreaOrigin() to take the toolbar into account
155 wxPoint pt = GetClientAreaOrigin();
156 width += pt.x;
157 height += pt.y;
158
159 wxTopLevelWindow::DoSetClientSize(width, height);
160}
161
162// Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
163void wxFrame::DoGetClientSize(int *x, int *y) const
164{
165 wxTopLevelWindow::DoGetClientSize(x, y);
166
167 // account for the possible toolbar
168 wxPoint pt = GetClientAreaOrigin();
169 if ( x )
170 *x -= pt.x;
171
172 if ( y )
173 *y -= pt.y;
174
175#if wxUSE_STATUSBAR
176 // adjust client area height to take the status bar into account
177 if ( y )
178 {
179 wxStatusBar *statbar = GetStatusBar();
180 if ( statbar && statbar->IsShown() )
181 {
182 *y -= statbar->GetClientSize().y;
183 }
184 }
185#endif // wxUSE_STATUSBAR
186}
187
188// ----------------------------------------------------------------------------
189// wxFrame: various geometry-related functions
190// ----------------------------------------------------------------------------
191
192void wxFrame::Raise()
193{
194#ifdef __WIN16__
195 // no SetForegroundWindow() in Win16
196 wxFrameBase::Raise();
197#else // Win32
198 ::SetForegroundWindow(GetHwnd());
199#endif // Win16/32
200}
201
202// generate an artificial resize event
203void wxFrame::SendSizeEvent()
204{
205 if ( !m_iconized )
206 {
207 RECT r = wxGetWindowRect(GetHwnd());
208
209 (void)::PostMessage(GetHwnd(), WM_SIZE,
210 IsMaximized() ? SIZE_MAXIMIZED : SIZE_RESTORED,
211 MAKELPARAM(r.right - r.left, r.bottom - r.top));
212 }
213}
214
215#if wxUSE_STATUSBAR
216wxStatusBar *wxFrame::OnCreateStatusBar(int number,
217 long style,
218 wxWindowID id,
219 const wxString& name)
220{
221 wxStatusBar *statusBar = NULL;
222
223#if wxUSE_NATIVE_STATUSBAR
224 if ( !UsesNativeStatusBar() )
225 {
226 statusBar = (wxStatusBar *)new wxStatusBarGeneric(this, id, style);
227 }
228 else
229#endif
230 {
231 statusBar = new wxStatusBar(this, id, style, name);
232 }
233
234 statusBar->SetFieldsCount(number);
235
236 return statusBar;
237}
238
239void wxFrame::PositionStatusBar()
240{
241 if ( !m_frameStatusBar || !m_frameStatusBar->IsShown() )
242 return;
243
244 int w, h;
245 GetClientSize(&w, &h);
246 int sw, sh;
247 m_frameStatusBar->GetSize(&sw, &sh);
248
249 // Since we wish the status bar to be directly under the client area,
250 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
251 m_frameStatusBar->SetSize(0, h, w, sh);
252}
253#endif // wxUSE_STATUSBAR
254
255#if wxUSE_MENUS_NATIVE
256
257void wxFrame::AttachMenuBar(wxMenuBar *menubar)
258{
259 wxFrameBase::AttachMenuBar(menubar);
260
261 if ( !menubar )
262 {
263 // actually remove the menu from the frame
264 m_hMenu = (WXHMENU)0;
265 InternalSetMenuBar();
266 }
267 else // set new non NULL menu bar
268 {
269 // Can set a menubar several times.
270 if ( menubar->GetHMenu() )
271 {
272 m_hMenu = menubar->GetHMenu();
273 }
274 else // no HMENU yet
275 {
276 m_hMenu = menubar->Create();
277
278 if ( !m_hMenu )
279 {
280 wxFAIL_MSG( _T("failed to create menu bar") );
281 return;
282 }
283 }
284
285 InternalSetMenuBar();
286 }
287}
288
289void wxFrame::InternalSetMenuBar()
290{
291#ifndef __WXMICROWIN__
292 if ( !::SetMenu(GetHwnd(), (HMENU)m_hMenu) )
293 {
294 wxLogLastError(wxT("SetMenu"));
295 }
296#endif
297}
298
299#endif // wxUSE_MENUS_NATIVE
300
301// Responds to colour changes, and passes event on to children.
302void wxFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
303{
304 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
305 Refresh();
306
307#if wxUSE_STATUSBAR
308 if ( m_frameStatusBar )
309 {
310 wxSysColourChangedEvent event2;
311 event2.SetEventObject( m_frameStatusBar );
312 m_frameStatusBar->GetEventHandler()->ProcessEvent(event2);
313 }
314#endif // wxUSE_STATUSBAR
315
316 // Propagate the event to the non-top-level children
317 wxWindow::OnSysColourChanged(event);
318}
319
320// Pass TRUE to show full screen, FALSE to restore.
321bool wxFrame::ShowFullScreen(bool show, long style)
322{
323 if ( IsFullScreen() == show )
324 return FALSE;
325
326 if (show)
327 {
328#if wxUSE_TOOLBAR
329 wxToolBar *theToolBar = GetToolBar();
330 if (theToolBar)
331 theToolBar->GetSize(NULL, &m_fsToolBarHeight);
332
333 // zap the toolbar, menubar, and statusbar
334
335 if ((style & wxFULLSCREEN_NOTOOLBAR) && theToolBar)
336 {
337 theToolBar->SetSize(-1,0);
338 theToolBar->Show(FALSE);
339 }
340#endif // wxUSE_TOOLBAR
341
342#ifndef __WXMICROWIN__
343 if (style & wxFULLSCREEN_NOMENUBAR)
344 SetMenu((HWND)GetHWND(), (HMENU) NULL);
345#endif
346
347#if wxUSE_STATUSBAR
348 wxStatusBar *theStatusBar = GetStatusBar();
349 if (theStatusBar)
350 theStatusBar->GetSize(NULL, &m_fsStatusBarHeight);
351
352 // Save the number of fields in the statusbar
353 if ((style & wxFULLSCREEN_NOSTATUSBAR) && theStatusBar)
354 {
355 //m_fsStatusBarFields = theStatusBar->GetFieldsCount();
356 //SetStatusBar((wxStatusBar*) NULL);
357 //delete theStatusBar;
358 theStatusBar->Show(FALSE);
359 }
360 else
361 m_fsStatusBarFields = 0;
362#endif // wxUSE_STATUSBAR
363 }
364 else
365 {
366#if wxUSE_TOOLBAR
367 wxToolBar *theToolBar = GetToolBar();
368
369 // restore the toolbar, menubar, and statusbar
370 if (theToolBar && (m_fsStyle & wxFULLSCREEN_NOTOOLBAR))
371 {
372 theToolBar->SetSize(-1, m_fsToolBarHeight);
373 theToolBar->Show(TRUE);
374 }
375#endif // wxUSE_TOOLBAR
376
377#if wxUSE_STATUSBAR
378 if ( m_fsStyle & wxFULLSCREEN_NOSTATUSBAR )
379 {
380 //CreateStatusBar(m_fsStatusBarFields);
381 if (GetStatusBar())
382 {
383 GetStatusBar()->Show(TRUE);
384 PositionStatusBar();
385 }
386 }
387#endif // wxUSE_STATUSBAR
388
389#ifndef __WXMICROWIN__
390 if ((m_fsStyle & wxFULLSCREEN_NOMENUBAR) && (m_hMenu != 0))
391 SetMenu((HWND)GetHWND(), (HMENU)m_hMenu);
392#endif
393 }
394
395 return wxFrameBase::ShowFullScreen(show, style);
396}
397
398// ----------------------------------------------------------------------------
399// tool/status bar stuff
400// ----------------------------------------------------------------------------
401
402#if wxUSE_TOOLBAR
403
404wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
405{
406 if ( wxFrameBase::CreateToolBar(style, id, name) )
407 {
408 PositionToolBar();
409 }
410
411 return m_frameToolBar;
412}
413
414void wxFrame::PositionToolBar()
415{
416 wxToolBar *toolbar = GetToolBar();
417 if ( toolbar && toolbar->IsShown() )
418 {
419 // don't call our (or even wxTopLevelWindow) version because we want
420 // the real (full) client area size, not excluding the tool/status bar
421 int width, height;
422 wxWindow::DoGetClientSize(&width, &height);
423
424#if wxUSE_STATUSBAR
425 wxStatusBar *statbar = GetStatusBar();
426 if ( statbar && statbar->IsShown() )
427 {
428 height -= statbar->GetClientSize().y;
429 }
430#endif // wxUSE_STATUSBAR
431
432 int tw, th;
433 toolbar->GetSize(&tw, &th);
434
435 if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL )
436 {
437 th = height;
438 }
439 else
440 {
441 tw = width;
442 if ( toolbar->GetWindowStyleFlag() & wxTB_FLAT )
443 th -= 3;
444 }
445
446 // use the 'real' MSW position here, don't offset relativly to the
447 // client area origin
448 toolbar->SetSize(0, 0, tw, th, wxSIZE_NO_ADJUSTMENTS);
449 }
450}
451
452#endif // wxUSE_TOOLBAR
453
454// ----------------------------------------------------------------------------
455// frame state (iconized/maximized/...)
456// ----------------------------------------------------------------------------
457
458// propagate our state change to all child frames: this allows us to emulate X
459// Windows behaviour where child frames float independently of the parent one
460// on the desktop, but are iconized/restored with it
461void wxFrame::IconizeChildFrames(bool bIconize)
462{
463 for ( wxWindowList::Node *node = GetChildren().GetFirst();
464 node;
465 node = node->GetNext() )
466 {
467 wxWindow *win = node->GetData();
468
469 // iconizing the frames with this style under Win95 shell puts them at
470 // the bottom of the screen (as the MDI children) instead of making
471 // them appear in the taskbar because they are, by virtue of this
472 // style, not managed by the taskbar - instead leave Windows take care
473 // of them
474#ifdef __WIN95__
475 if ( win->GetWindowStyle() & wxFRAME_TOOL_WINDOW )
476 continue;
477#endif // Win95
478
479 // the child MDI frames are a special case and should not be touched by
480 // the parent frame - instead, they are managed by the user
481 wxFrame *frame = wxDynamicCast(win, wxFrame);
482 if ( frame
483#if wxUSE_MDI_ARCHITECTURE
484 && !wxDynamicCast(frame, wxMDIChildFrame)
485#endif // wxUSE_MDI_ARCHITECTURE
486 )
487 {
488 // we don't want to restore the child frames which had been
489 // iconized even before we were iconized, so save the child frame
490 // status when iconizing the parent frame and check it when
491 // restoring it
492 if ( bIconize )
493 {
494 // note that we shouldn't touch the hidden frames neither
495 // because iconizing/restoring them would show them as a side
496 // effect
497 frame->m_wasMinimized = frame->IsIconized() || !frame->IsShown();
498 }
499
500 // this test works for both iconizing and restoring
501 if ( !frame->m_wasMinimized )
502 frame->Iconize(bIconize);
503 }
504 }
505}
506
507WXHICON wxFrame::GetDefaultIcon() const
508{
509 return (WXHICON)(wxSTD_FRAME_ICON ? wxSTD_FRAME_ICON
510 : wxDEFAULT_FRAME_ICON);
511}
512
513// ===========================================================================
514// message processing
515// ===========================================================================
516
517// ---------------------------------------------------------------------------
518// preprocessing
519// ---------------------------------------------------------------------------
520
521bool wxFrame::MSWTranslateMessage(WXMSG* pMsg)
522{
523 if ( wxWindow::MSWTranslateMessage(pMsg) )
524 return TRUE;
525
526#if wxUSE_MENUS && wxUSE_ACCEL && !defined(__WXUNIVERSAL__)
527 // try the menu bar accels
528 wxMenuBar *menuBar = GetMenuBar();
529 if ( !menuBar )
530 return FALSE;
531
532 const wxAcceleratorTable& acceleratorTable = menuBar->GetAccelTable();
533 return acceleratorTable.Translate(this, pMsg);
534#else
535 return FALSE;
536#endif // wxUSE_MENUS && wxUSE_ACCEL
537}
538
539// ---------------------------------------------------------------------------
540// our private (non virtual) message handlers
541// ---------------------------------------------------------------------------
542
543bool wxFrame::HandlePaint()
544{
545 RECT rect;
546 if ( GetUpdateRect(GetHwnd(), &rect, FALSE) )
547 {
548#ifndef __WXMICROWIN__
549 if ( m_iconized )
550 {
551 const wxIcon& icon = GetIcon();
552 HICON hIcon = icon.Ok() ? GetHiconOf(icon)
553 : (HICON)GetDefaultIcon();
554
555 // Hold a pointer to the dc so long as the OnPaint() message
556 // is being processed
557 PAINTSTRUCT ps;
558 HDC hdc = ::BeginPaint(GetHwnd(), &ps);
559
560 // Erase background before painting or we get white background
561 MSWDefWindowProc(WM_ICONERASEBKGND, (WORD)(LONG)ps.hdc, 0L);
562
563 if ( hIcon )
564 {
565 RECT rect;
566 ::GetClientRect(GetHwnd(), &rect);
567
568 // FIXME: why hardcoded?
569 static const int icon_width = 32;
570 static const int icon_height = 32;
571
572 int icon_x = (int)((rect.right - icon_width)/2);
573 int icon_y = (int)((rect.bottom - icon_height)/2);
574
575 ::DrawIcon(hdc, icon_x, icon_y, hIcon);
576 }
577
578 ::EndPaint(GetHwnd(), &ps);
579
580 return TRUE;
581 }
582 else
583 #endif
584 {
585 return wxWindow::HandlePaint();
586 }
587 }
588 else
589 {
590 // nothing to paint - processed
591 return TRUE;
592 }
593}
594
595bool wxFrame::HandleSize(int x, int y, WXUINT id)
596{
597 bool processed = FALSE;
598#ifndef __WXMICROWIN__
599
600 switch ( id )
601 {
602 case SIZENORMAL:
603 // only do it it if we were iconized before, otherwise resizing the
604 // parent frame has a curious side effect of bringing it under it's
605 // children
606 if ( !m_iconized )
607 break;
608
609 // restore all child frames too
610 IconizeChildFrames(FALSE);
611
612 (void)SendIconizeEvent(FALSE);
613
614 // fall through
615
616 case SIZEFULLSCREEN:
617 m_iconized = FALSE;
618 break;
619
620 case SIZEICONIC:
621 // iconize all child frames too
622 IconizeChildFrames(TRUE);
623
624 (void)SendIconizeEvent();
625
626 m_iconized = TRUE;
627 break;
628 }
629#endif
630
631 if ( !m_iconized )
632 {
633#if wxUSE_STATUSBAR
634 PositionStatusBar();
635#endif // wxUSE_STATUSBAR
636
637#if wxUSE_TOOLBAR
638 PositionToolBar();
639#endif // wxUSE_TOOLBAR
640
641 processed = wxWindow::HandleSize(x, y, id);
642 }
643
644 return processed;
645}
646
647bool wxFrame::HandleCommand(WXWORD id, WXWORD cmd, WXHWND control)
648{
649 if ( control )
650 {
651 // In case it's e.g. a toolbar.
652 wxWindow *win = wxFindWinFromHandle(control);
653 if ( win )
654 return win->MSWCommand(cmd, id);
655 }
656
657 // handle here commands from menus and accelerators
658 if ( cmd == 0 || cmd == 1 )
659 {
660#if wxUSE_MENUS_NATIVE
661 if ( wxCurrentPopupMenu )
662 {
663 wxMenu *popupMenu = wxCurrentPopupMenu;
664 wxCurrentPopupMenu = NULL;
665
666 return popupMenu->MSWCommand(cmd, id);
667 }
668#endif // wxUSE_MENUS_NATIVE
669
670 if ( ProcessCommand(id) )
671 {
672 return TRUE;
673 }
674 }
675
676 return FALSE;
677}
678
679bool wxFrame::HandleMenuSelect(WXWORD nItem, WXWORD flags, WXHMENU hMenu)
680{
681 int item;
682 if ( flags == 0xFFFF && hMenu == 0 )
683 {
684 // menu was removed from screen
685 item = -1;
686 }
687#ifndef __WXMICROWIN__
688 else if ( !(flags & MF_POPUP) && !(flags & MF_SEPARATOR) )
689 {
690 item = nItem;
691 }
692#endif
693 else
694 {
695 // don't give hints for separators (doesn't make sense) nor for the
696 // items opening popup menus (they don't have them anyhow) but do clear
697 // the status line - otherwise, we would be left with the help message
698 // for the previous item which doesn't apply any more
699 DoGiveHelp(wxEmptyString, FALSE);
700
701 return FALSE;
702 }
703
704 wxMenuEvent event(wxEVT_MENU_HIGHLIGHT, item);
705 event.SetEventObject(this);
706
707 return GetEventHandler()->ProcessEvent(event);
708}
709
710bool wxFrame::HandleMenuLoop(const wxEventType& evtType, WXWORD isPopup)
711{
712 // we don't have the menu id here, so we use the id to specify if the event
713 // was from a popup menu or a normal one
714 wxMenuEvent event(evtType, isPopup ? -1 : 0);
715 event.SetEventObject(this);
716
717 return GetEventHandler()->ProcessEvent(event);
718}
719
720// ---------------------------------------------------------------------------
721// the window proc for wxFrame
722// ---------------------------------------------------------------------------
723
724long wxFrame::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
725{
726 long rc = 0;
727 bool processed = FALSE;
728
729 switch ( message )
730 {
731 case WM_CLOSE:
732 // if we can't close, tell the system that we processed the
733 // message - otherwise it would close us
734 processed = !Close();
735 break;
736
737 case WM_SIZE:
738 processed = HandleSize(LOWORD(lParam), HIWORD(lParam), wParam);
739 break;
740
741 case WM_COMMAND:
742 {
743 WORD id, cmd;
744 WXHWND hwnd;
745 UnpackCommand((WXWPARAM)wParam, (WXLPARAM)lParam,
746 &id, &hwnd, &cmd);
747
748 processed = HandleCommand(id, cmd, (WXHWND)hwnd);
749 }
750 break;
751
752 case WM_PAINT:
753 processed = HandlePaint();
754 break;
755
756#ifndef __WXMICROWIN__
757 case WM_MENUSELECT:
758 {
759 WXWORD item, flags;
760 WXHMENU hmenu;
761 UnpackMenuSelect(wParam, lParam, &item, &flags, &hmenu);
762
763 processed = HandleMenuSelect(item, flags, hmenu);
764 }
765 break;
766
767#ifndef __WIN16__
768 case WM_ENTERMENULOOP:
769 processed = HandleMenuLoop(wxEVT_MENU_OPEN, wParam);
770 break;
771
772 case WM_EXITMENULOOP:
773 processed = HandleMenuLoop(wxEVT_MENU_CLOSE, wParam);
774 break;
775#endif // __WIN16__
776
777 case WM_QUERYDRAGICON:
778 {
779 const wxIcon& icon = GetIcon();
780 HICON hIcon = icon.Ok() ? GetHiconOf(icon)
781 : (HICON)GetDefaultIcon();
782 rc = (long)hIcon;
783 processed = rc != 0;
784 }
785 break;
786#endif // !__WXMICROWIN__
787 }
788
789 if ( !processed )
790 rc = wxFrameBase::MSWWindowProc(message, wParam, lParam);
791
792 return rc;
793}
794