]> git.saurik.com Git - wxWidgets.git/blob - src/msw/frame.cpp
corrected Mac OS X resource file name
[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 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/setup.h"
33 #include "wx/frame.h"
34 #include "wx/menu.h"
35 #include "wx/app.h"
36 #include "wx/utils.h"
37 #include "wx/dialog.h"
38 #include "wx/settings.h"
39 #include "wx/dcclient.h"
40 #include "wx/mdi.h"
41 #include "wx/panel.h"
42 #endif // WX_PRECOMP
43
44 #include "wx/msw/private.h"
45
46 #if wxUSE_STATUSBAR
47 #include "wx/statusbr.h"
48 #include "wx/generic/statusbr.h"
49 #endif // wxUSE_STATUSBAR
50
51 #if wxUSE_TOOLBAR
52 #include "wx/toolbar.h"
53 #endif // wxUSE_TOOLBAR
54
55 #include "wx/menuitem.h"
56 #include "wx/log.h"
57
58 // ----------------------------------------------------------------------------
59 // globals
60 // ----------------------------------------------------------------------------
61
62 extern wxWindowList wxModelessWindows;
63 extern wxList WXDLLEXPORT wxPendingDelete;
64 extern const wxChar *wxFrameClassName;
65 extern wxMenu *wxCurrentPopupMenu;
66
67 // ----------------------------------------------------------------------------
68 // event tables
69 // ----------------------------------------------------------------------------
70
71 BEGIN_EVENT_TABLE(wxFrame, wxFrameBase)
72 EVT_ACTIVATE(wxFrame::OnActivate)
73 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged)
74 END_EVENT_TABLE()
75
76 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxWindow)
77
78 // ============================================================================
79 // implementation
80 // ============================================================================
81
82 // ----------------------------------------------------------------------------
83 // static class members
84 // ----------------------------------------------------------------------------
85
86 #if wxUSE_NATIVE_STATUSBAR
87 bool wxFrame::m_useNativeStatusBar = TRUE;
88 #else
89 bool wxFrame::m_useNativeStatusBar = FALSE;
90 #endif
91
92 // ----------------------------------------------------------------------------
93 // creation/destruction
94 // ----------------------------------------------------------------------------
95
96 void wxFrame::Init()
97 {
98 m_iconized =
99 m_maximizeOnShow = FALSE;
100
101 #if wxUSE_TOOLTIPS
102 m_hwndToolTip = 0;
103 #endif
104
105 // Data to save/restore when calling ShowFullScreen
106 m_fsStyle = 0;
107 m_fsOldWindowStyle = 0;
108 m_fsStatusBarFields = 0;
109 m_fsStatusBarHeight = 0;
110 m_fsToolBarHeight = 0;
111 // m_fsMenu = 0;
112 m_fsIsMaximized = FALSE;
113 m_fsIsShowing = FALSE;
114
115 m_winLastFocused = (wxWindow *)NULL;
116
117 // unlike (almost?) all other windows, frames are created hidden
118 m_isShown = FALSE;
119 }
120
121 bool wxFrame::Create(wxWindow *parent,
122 wxWindowID id,
123 const wxString& title,
124 const wxPoint& pos,
125 const wxSize& size,
126 long style,
127 const wxString& name)
128 {
129 SetName(name);
130 m_windowStyle = style;
131 m_frameMenuBar = NULL;
132 m_frameToolBar = NULL;
133 m_frameStatusBar = NULL;
134
135 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE));
136
137 if ( id > -1 )
138 m_windowId = id;
139 else
140 m_windowId = (int)NewControlId();
141
142 if (parent) parent->AddChild(this);
143
144 int x = pos.x;
145 int y = pos.y;
146 int width = size.x;
147 int height = size.y;
148
149 m_iconized = FALSE;
150
151 wxTopLevelWindows.Append(this);
152
153 MSWCreate(m_windowId, parent, wxFrameClassName, this, title,
154 x, y, width, height, style);
155
156 wxModelessWindows.Append(this);
157
158 return TRUE;
159 }
160
161 wxFrame::~wxFrame()
162 {
163 m_isBeingDeleted = TRUE;
164 wxTopLevelWindows.DeleteObject(this);
165
166 // the ~wxToolBar() code relies on the previous line to be executed before
167 // this one, i.e. the frame should remove itself from wxTopLevelWindows
168 // before destorying its toolbar
169 DeleteAllBars();
170
171 if (wxTheApp && (wxTopLevelWindows.Number() == 0))
172 {
173 wxTheApp->SetTopWindow(NULL);
174
175 if (wxTheApp->GetExitOnFrameDelete())
176 {
177 PostQuitMessage(0);
178 }
179 }
180
181 wxModelessWindows.DeleteObject(this);
182
183 // For some reason, wxWindows can activate another task altogether
184 // when a frame is destroyed after a modal dialog has been invoked.
185 // Try to bring the parent to the top.
186 // MT:Only do this if this frame is currently the active window, else weird
187 // things start to happen
188 if ( wxGetActiveWindow() == this )
189 if (GetParent() && GetParent()->GetHWND())
190 ::BringWindowToTop((HWND) GetParent()->GetHWND());
191 }
192
193 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
194 void wxFrame::DoGetClientSize(int *x, int *y) const
195 {
196 RECT rect;
197 ::GetClientRect(GetHwnd(), &rect);
198
199 #if wxUSE_STATUSBAR
200 if ( GetStatusBar() && GetStatusBar()->IsShown() )
201 {
202 int statusX, statusY;
203 GetStatusBar()->GetClientSize(&statusX, &statusY);
204 rect.bottom -= statusY;
205 }
206 #endif // wxUSE_STATUSBAR
207
208 wxPoint pt(GetClientAreaOrigin());
209 rect.bottom -= pt.y;
210 rect.right -= pt.x;
211
212 if ( x )
213 *x = rect.right;
214 if ( y )
215 *y = rect.bottom;
216 }
217
218 // Set the client size (i.e. leave the calculation of borders etc.
219 // to wxWindows)
220 void wxFrame::DoSetClientSize(int width, int height)
221 {
222 HWND hWnd = GetHwnd();
223
224 RECT rectClient;
225 ::GetClientRect(hWnd, &rectClient);
226
227 RECT rectTotal;
228 ::GetWindowRect(hWnd, &rectTotal);
229
230 // Find the difference between the entire window (title bar and all)
231 // and the client area; add this to the new client size to move the
232 // window
233 width += rectTotal.right - rectTotal.left - rectClient.right;
234 height += rectTotal.bottom - rectTotal.top - rectClient.bottom;
235
236 #if wxUSE_STATUSBAR
237 wxStatusBar *statbar = GetStatusBar();
238 if ( statbar && statbar->IsShown() )
239 {
240 // leave enough space for the status bar
241 height += statbar->GetSize().y;
242 }
243 #endif // wxUSE_STATUSBAR
244
245 // note that this takes the toolbar into account
246 wxPoint pt = GetClientAreaOrigin();
247 width += pt.x;
248 height += pt.y;
249
250 if ( !::MoveWindow(hWnd, rectTotal.left, rectTotal.top,
251 width, height, TRUE /* redraw */) )
252 {
253 wxLogLastError(_T("MoveWindow"));
254 }
255
256 wxSizeEvent event(wxSize(width, height), m_windowId);
257 event.SetEventObject(this);
258 GetEventHandler()->ProcessEvent(event);
259 }
260
261 void wxFrame::DoGetSize(int *width, int *height) const
262 {
263 RECT rect;
264 ::GetWindowRect(GetHwnd(), &rect);
265
266 *width = rect.right - rect.left;
267 *height = rect.bottom - rect.top;
268 }
269
270 void wxFrame::DoGetPosition(int *x, int *y) const
271 {
272 RECT rect;
273 ::GetWindowRect(GetHwnd(), &rect);
274
275 *x = rect.left;
276 *y = rect.top;
277 }
278
279 // ----------------------------------------------------------------------------
280 // variations around ::ShowWindow()
281 // ----------------------------------------------------------------------------
282
283 void wxFrame::DoShowWindow(int nShowCmd)
284 {
285 ::ShowWindow(GetHwnd(), nShowCmd);
286
287 m_iconized = nShowCmd == SW_MINIMIZE;
288 }
289
290 bool wxFrame::Show(bool show)
291 {
292 // don't use wxWindow version as we want to call DoShowWindow()
293 if ( !wxWindowBase::Show(show) )
294 return FALSE;
295
296 int nShowCmd;
297 if ( show )
298 {
299 if ( m_maximizeOnShow )
300 {
301 // show and maximize
302 nShowCmd = SW_MAXIMIZE;
303
304 m_maximizeOnShow = FALSE;
305 }
306 else // just show
307 {
308 nShowCmd = SW_SHOW;
309 }
310 }
311 else // hide
312 {
313 nShowCmd = SW_HIDE;
314 }
315
316 DoShowWindow(nShowCmd);
317
318 if ( show )
319 {
320 ::BringWindowToTop(GetHwnd());
321
322 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, m_windowId);
323 event.SetEventObject( this );
324 GetEventHandler()->ProcessEvent(event);
325 }
326 else // hide
327 {
328 // Try to highlight the correct window (the parent)
329 if ( GetParent() )
330 {
331 HWND hWndParent = GetHwndOf(GetParent());
332 if (hWndParent)
333 ::BringWindowToTop(hWndParent);
334 }
335 }
336
337 return TRUE;
338 }
339
340 void wxFrame::Iconize(bool iconize)
341 {
342 DoShowWindow(iconize ? SW_MINIMIZE : SW_RESTORE);
343 }
344
345 void wxFrame::Maximize(bool maximize)
346 {
347 if ( IsShown() )
348 {
349 // just maximize it directly
350 DoShowWindow(maximize ? SW_MAXIMIZE : SW_RESTORE);
351 }
352 else // hidden
353 {
354 // we can't maximize the hidden frame because it shows it as well, so
355 // just remember that we should do it later in this case
356 m_maximizeOnShow = TRUE;
357 }
358 }
359
360 void wxFrame::Restore()
361 {
362 DoShowWindow(SW_RESTORE);
363 }
364
365 bool wxFrame::IsIconized() const
366 {
367 ((wxFrame *)this)->m_iconized = (::IsIconic(GetHwnd()) != 0);
368 return m_iconized;
369 }
370
371 // Is it maximized?
372 bool wxFrame::IsMaximized() const
373 {
374 return (::IsZoomed(GetHwnd()) != 0);
375 }
376
377 void wxFrame::SetIcon(const wxIcon& icon)
378 {
379 wxFrameBase::SetIcon(icon);
380
381 #if defined(__WIN95__)
382 if ( m_icon.Ok() )
383 {
384 SendMessage(GetHwnd(), WM_SETICON,
385 (WPARAM)TRUE, (LPARAM)(HICON) m_icon.GetHICON());
386 }
387 #endif // __WIN95__
388 }
389
390 // generate an artificial resize event
391 void wxFrame::SendSizeEvent()
392 {
393 RECT r;
394 #ifdef __WIN16__
395 ::GetWindowRect(GetHwnd(), &r);
396 #else
397 if ( !::GetWindowRect(GetHwnd(), &r) )
398 {
399 wxLogLastError(_T("GetWindowRect"));
400 }
401 #endif
402
403 if ( !m_iconized )
404 {
405 (void)::PostMessage(GetHwnd(), WM_SIZE,
406 IsMaximized() ? SIZE_MAXIMIZED : SIZE_RESTORED,
407 MAKELPARAM(r.right - r.left, r.bottom - r.top));
408 }
409 }
410
411 #if wxUSE_STATUSBAR
412 wxStatusBar *wxFrame::OnCreateStatusBar(int number,
413 long style,
414 wxWindowID id,
415 const wxString& name)
416 {
417 wxStatusBar *statusBar = NULL;
418
419 #if wxUSE_NATIVE_STATUSBAR
420 if ( !UsesNativeStatusBar() )
421 {
422 statusBar = (wxStatusBar *)new wxStatusBarGeneric(this, id, style);
423 }
424 else
425 #endif
426 {
427 statusBar = new wxStatusBar(this, id, style, name);
428 }
429
430 // Set the height according to the font and the border size
431 wxClientDC dc(statusBar);
432 dc.SetFont(statusBar->GetFont());
433
434 wxCoord y;
435 dc.GetTextExtent(_T("X"), NULL, &y );
436
437 int height = (int)( (11*y)/10 + 2*statusBar->GetBorderY());
438
439 statusBar->SetSize(-1, -1, -1, height);
440
441 statusBar->SetFieldsCount(number);
442
443 return statusBar;
444 }
445
446 void wxFrame::PositionStatusBar()
447 {
448 if ( !m_frameStatusBar )
449 return;
450
451 int w, h;
452 GetClientSize(&w, &h);
453 int sw, sh;
454 m_frameStatusBar->GetSize(&sw, &sh);
455
456 // Since we wish the status bar to be directly under the client area,
457 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
458 m_frameStatusBar->SetSize(0, h, w, sh);
459 }
460 #endif // wxUSE_STATUSBAR
461
462 void wxFrame::DetachMenuBar()
463 {
464 if ( m_frameMenuBar )
465 {
466 m_frameMenuBar->Detach();
467 m_frameMenuBar = NULL;
468 }
469 }
470
471 void wxFrame::SetMenuBar(wxMenuBar *menubar)
472 {
473 if ( !menubar )
474 {
475 DetachMenuBar();
476
477 // actually remove the menu from the frame
478 m_hMenu = (WXHMENU)0;
479 InternalSetMenuBar();
480 }
481 else // set new non NULL menu bar
482 {
483 m_frameMenuBar = NULL;
484
485 // Can set a menubar several times.
486 // TODO: how to prevent a memory leak if you have a currently-unattached
487 // menubar? wxWindows assumes that the frame will delete the menu (otherwise
488 // there are problems for MDI).
489 if ( menubar->GetHMenu() )
490 {
491 m_hMenu = menubar->GetHMenu();
492 }
493 else
494 {
495 menubar->Detach();
496
497 m_hMenu = menubar->Create();
498
499 if ( !m_hMenu )
500 return;
501 }
502
503 InternalSetMenuBar();
504
505 m_frameMenuBar = menubar;
506 menubar->Attach(this);
507 }
508 }
509
510 void wxFrame::InternalSetMenuBar()
511 {
512 if ( !::SetMenu(GetHwnd(), (HMENU)m_hMenu) )
513 {
514 wxLogLastError(wxT("SetMenu"));
515 }
516 }
517
518 // Responds to colour changes, and passes event on to children.
519 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
520 {
521 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE));
522 Refresh();
523
524 if ( m_frameStatusBar )
525 {
526 wxSysColourChangedEvent event2;
527 event2.SetEventObject( m_frameStatusBar );
528 m_frameStatusBar->GetEventHandler()->ProcessEvent(event2);
529 }
530
531 // Propagate the event to the non-top-level children
532 wxWindow::OnSysColourChanged(event);
533 }
534
535 // Pass TRUE to show full screen, FALSE to restore.
536 bool wxFrame::ShowFullScreen(bool show, long style)
537 {
538 if (show)
539 {
540 if (IsFullScreen())
541 return FALSE;
542
543 m_fsIsShowing = TRUE;
544 m_fsStyle = style;
545
546 wxToolBar *theToolBar = GetToolBar();
547 wxStatusBar *theStatusBar = GetStatusBar();
548
549 int dummyWidth;
550
551 if (theToolBar)
552 theToolBar->GetSize(&dummyWidth, &m_fsToolBarHeight);
553 if (theStatusBar)
554 theStatusBar->GetSize(&dummyWidth, &m_fsStatusBarHeight);
555
556 // zap the toolbar, menubar, and statusbar
557
558 if ((style & wxFULLSCREEN_NOTOOLBAR) && theToolBar)
559 {
560 theToolBar->SetSize(-1,0);
561 theToolBar->Show(FALSE);
562 }
563
564 if (style & wxFULLSCREEN_NOMENUBAR)
565 SetMenu((HWND)GetHWND(), (HMENU) NULL);
566
567 // Save the number of fields in the statusbar
568 if ((style & wxFULLSCREEN_NOSTATUSBAR) && theStatusBar)
569 {
570 //m_fsStatusBarFields = theStatusBar->GetFieldsCount();
571 //SetStatusBar((wxStatusBar*) NULL);
572 //delete theStatusBar;
573 theStatusBar->Show(FALSE);
574 }
575 else
576 m_fsStatusBarFields = 0;
577
578 // zap the frame borders
579
580 // save the 'normal' window style
581 m_fsOldWindowStyle = GetWindowLong((HWND)GetHWND(), GWL_STYLE);
582
583 // save the old position, width & height, maximize state
584 m_fsOldSize = GetRect();
585 m_fsIsMaximized = IsMaximized();
586
587 // decide which window style flags to turn off
588 LONG newStyle = m_fsOldWindowStyle;
589 LONG offFlags = 0;
590
591 if (style & wxFULLSCREEN_NOBORDER)
592 offFlags |= WS_BORDER;
593 if (style & wxFULLSCREEN_NOCAPTION)
594 offFlags |= (WS_CAPTION | WS_SYSMENU);
595
596 newStyle &= (~offFlags);
597
598 // change our window style to be compatible with full-screen mode
599 SetWindowLong((HWND)GetHWND(), GWL_STYLE, newStyle);
600
601 // resize to the size of the desktop
602 int width, height;
603
604 RECT rect;
605 ::GetWindowRect(GetDesktopWindow(), &rect);
606 width = rect.right - rect.left;
607 height = rect.bottom - rect.top;
608
609 SetSize(width, height);
610
611 // now flush the window style cache and actually go full-screen
612 SetWindowPos((HWND)GetHWND(), HWND_TOP, 0, 0, width, height, SWP_FRAMECHANGED);
613
614 wxSizeEvent event(wxSize(width, height), GetId());
615 GetEventHandler()->ProcessEvent(event);
616
617 return TRUE;
618 }
619 else
620 {
621 if (!IsFullScreen())
622 return FALSE;
623
624 m_fsIsShowing = FALSE;
625
626 wxToolBar *theToolBar = GetToolBar();
627
628 // restore the toolbar, menubar, and statusbar
629 if (theToolBar && (m_fsStyle & wxFULLSCREEN_NOTOOLBAR))
630 {
631 theToolBar->SetSize(-1, m_fsToolBarHeight);
632 theToolBar->Show(TRUE);
633 }
634
635 if ((m_fsStyle & wxFULLSCREEN_NOSTATUSBAR)) // && (m_fsStatusBarFields > 0))
636 {
637 //CreateStatusBar(m_fsStatusBarFields);
638 if (GetStatusBar())
639 {
640 GetStatusBar()->Show(TRUE);
641 PositionStatusBar();
642 }
643 }
644
645 if ((m_fsStyle & wxFULLSCREEN_NOMENUBAR) && (m_hMenu != 0))
646 SetMenu((HWND)GetHWND(), (HMENU)m_hMenu);
647
648 Maximize(m_fsIsMaximized);
649 SetWindowLong((HWND)GetHWND(),GWL_STYLE, m_fsOldWindowStyle);
650 SetWindowPos((HWND)GetHWND(),HWND_TOP,m_fsOldSize.x, m_fsOldSize.y,
651 m_fsOldSize.width, m_fsOldSize.height, SWP_FRAMECHANGED);
652
653 return TRUE;
654 }
655 }
656
657 /*
658 * Frame window
659 *
660 */
661
662 bool wxFrame::MSWCreate(int id, wxWindow *parent, const wxChar *wclass, wxWindow *wx_win, const wxChar *title,
663 int x, int y, int width, int height, long style)
664
665 {
666 m_defaultIcon = (WXHICON) (wxSTD_FRAME_ICON ? wxSTD_FRAME_ICON : wxDEFAULT_FRAME_ICON);
667
668 // If child windows aren't properly drawn initially, WS_CLIPCHILDREN
669 // could be the culprit. But without it, you can get a lot of flicker.
670
671 DWORD msflags = 0;
672 if ( style & wxCAPTION )
673 {
674 if ( style & wxFRAME_TOOL_WINDOW )
675 msflags |= WS_POPUPWINDOW;
676 else
677 msflags |= WS_OVERLAPPED;
678 }
679 else
680 {
681 msflags |= WS_POPUP;
682 }
683
684 if (style & wxMINIMIZE_BOX)
685 msflags |= WS_MINIMIZEBOX;
686 if (style & wxMAXIMIZE_BOX)
687 msflags |= WS_MAXIMIZEBOX;
688 if (style & wxTHICK_FRAME)
689 msflags |= WS_THICKFRAME;
690 if (style & wxSYSTEM_MENU)
691 msflags |= WS_SYSMENU;
692 if ( style & wxMINIMIZE )
693 msflags |= WS_MINIMIZE;
694 if (style & wxMAXIMIZE)
695 msflags |= WS_MAXIMIZE;
696 if (style & wxCAPTION)
697 msflags |= WS_CAPTION;
698 if (style & wxCLIP_CHILDREN)
699 msflags |= WS_CLIPCHILDREN;
700
701 // Keep this in wxFrame because it saves recoding this function
702 // in wxTinyFrame
703 #if wxUSE_ITSY_BITSY && !defined(__WIN32__)
704 if (style & wxTINY_CAPTION_VERT)
705 msflags |= IBS_VERTCAPTION;
706 if (style & wxTINY_CAPTION_HORIZ)
707 msflags |= IBS_HORZCAPTION;
708 #else
709 if (style & wxTINY_CAPTION_VERT)
710 msflags |= WS_CAPTION;
711 if (style & wxTINY_CAPTION_HORIZ)
712 msflags |= WS_CAPTION;
713 #endif
714 if ((style & wxTHICK_FRAME) == 0)
715 msflags |= WS_BORDER;
716
717 WXDWORD extendedStyle = MakeExtendedStyle(style);
718
719 // make all frames appear in the win9x shell taskbar unless
720 // wxFRAME_TOOL_WINDOW or wxFRAME_NO_TASKBAR is given - without giving them
721 // WS_EX_APPWINDOW style, the child (i.e. owned) frames wouldn't appear in it
722 #if !defined(__WIN16__) && !defined(__SC__)
723 if ( (style & wxFRAME_TOOL_WINDOW) ||
724 (style & wxFRAME_NO_TASKBAR) )
725 extendedStyle |= WS_EX_TOOLWINDOW;
726 else if ( !(style & wxFRAME_NO_TASKBAR) )
727 extendedStyle |= WS_EX_APPWINDOW;
728 #endif
729
730 if (style & wxSTAY_ON_TOP)
731 extendedStyle |= WS_EX_TOPMOST;
732
733 #ifndef __WIN16__
734 if (m_exStyle & wxFRAME_EX_CONTEXTHELP)
735 extendedStyle |= WS_EX_CONTEXTHELP;
736 #endif
737
738 m_iconized = FALSE;
739 if ( !wxWindow::MSWCreate(id, parent, wclass, wx_win, title, x, y, width, height,
740 msflags, NULL, extendedStyle) )
741 return FALSE;
742
743 // Seems to be necessary if we use WS_POPUP
744 // style instead of WS_OVERLAPPED
745 if (width > -1 && height > -1)
746 ::PostMessage(GetHwnd(), WM_SIZE, SIZE_RESTORED, MAKELPARAM(width, height));
747
748 return TRUE;
749 }
750
751 // Default activation behaviour - set the focus for the first child
752 // subwindow found.
753 void wxFrame::OnActivate(wxActivateEvent& event)
754 {
755 if ( event.GetActive() )
756 {
757 // restore focus to the child which was last focused
758 wxLogTrace(_T("focus"), _T("wxFrame %08x activated."), m_hWnd);
759
760 wxWindow *parent = m_winLastFocused ? m_winLastFocused->GetParent()
761 : NULL;
762 if ( !parent )
763 {
764 parent = this;
765 }
766
767 wxSetFocusToChild(parent, &m_winLastFocused);
768 }
769 else // deactivating
770 {
771 // remember the last focused child if it is our child
772 m_winLastFocused = FindFocus();
773
774 // so we NULL it out if it's a child from some other frame
775 wxWindow *win = m_winLastFocused;
776 while ( win )
777 {
778 if ( win->IsTopLevel() )
779 {
780 if ( win != this )
781 {
782 m_winLastFocused = NULL;
783 }
784
785 break;
786 }
787
788 win = win->GetParent();
789 }
790
791 wxLogTrace(_T("focus"),
792 _T("wxFrame %08x deactivated, last focused: %08x."),
793 m_hWnd,
794 m_winLastFocused ? GetHwndOf(m_winLastFocused)
795 : NULL);
796
797 event.Skip();
798 }
799 }
800
801 // ----------------------------------------------------------------------------
802 // tool/status bar stuff
803 // ----------------------------------------------------------------------------
804
805 #if wxUSE_TOOLBAR
806
807 wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
808 {
809 if ( wxFrameBase::CreateToolBar(style, id, name) )
810 {
811 PositionToolBar();
812 }
813
814 return m_frameToolBar;
815 }
816
817 void wxFrame::PositionToolBar()
818 {
819 RECT rect;
820 ::GetClientRect(GetHwnd(), &rect);
821
822 #if wxUSE_STATUSBAR
823 if ( GetStatusBar() )
824 {
825 int statusX, statusY;
826 GetStatusBar()->GetClientSize(&statusX, &statusY);
827 rect.bottom -= statusY;
828 }
829 #endif // wxUSE_STATUSBAR
830
831 if ( GetToolBar() && GetToolBar()->IsShown() )
832 {
833 int tw, th;
834 GetToolBar()->GetSize(&tw, &th);
835
836 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL )
837 {
838 th = rect.bottom;
839 }
840 else
841 {
842 tw = rect.right;
843 }
844
845 // Use the 'real' MSW position here
846 GetToolBar()->SetSize(0, 0, tw, th, wxSIZE_NO_ADJUSTMENTS);
847 }
848 }
849 #endif // wxUSE_TOOLBAR
850
851 // ----------------------------------------------------------------------------
852 // frame state (iconized/maximized/...)
853 // ----------------------------------------------------------------------------
854
855 // propagate our state change to all child frames: this allows us to emulate X
856 // Windows behaviour where child frames float independently of the parent one
857 // on the desktop, but are iconized/restored with it
858 void wxFrame::IconizeChildFrames(bool bIconize)
859 {
860 for ( wxWindowList::Node *node = GetChildren().GetFirst();
861 node;
862 node = node->GetNext() )
863 {
864 wxWindow *win = node->GetData();
865
866 // iconizing the frames with this style under Win95 shell puts them at
867 // the bottom of the screen (as the MDI children) instead of making
868 // them appear in the taskbar because they are, by virtue of this
869 // style, not managed by the taskbar - instead leave Windows take care
870 // of them
871 #ifdef __WIN95__
872 if ( win->GetWindowStyle() & wxFRAME_TOOL_WINDOW )
873 continue;
874 #endif // Win95
875
876 // the child MDI frames are a special case and should not be touched by
877 // the parent frame - instead, they are managed by the user
878 wxFrame *frame = wxDynamicCast(win, wxFrame);
879 if ( frame && !frame->IsMDIChild() )
880 {
881 frame->Iconize(bIconize);
882 }
883 }
884 }
885
886 // ===========================================================================
887 // message processing
888 // ===========================================================================
889
890 // ---------------------------------------------------------------------------
891 // preprocessing
892 // ---------------------------------------------------------------------------
893
894 bool wxFrame::MSWTranslateMessage(WXMSG* pMsg)
895 {
896 if ( wxWindow::MSWTranslateMessage(pMsg) )
897 return TRUE;
898
899 // try the menu bar accels
900 wxMenuBar *menuBar = GetMenuBar();
901 if ( !menuBar )
902 return FALSE;
903
904 const wxAcceleratorTable& acceleratorTable = menuBar->GetAccelTable();
905 return acceleratorTable.Translate(this, pMsg);
906 }
907
908 // ---------------------------------------------------------------------------
909 // our private (non virtual) message handlers
910 // ---------------------------------------------------------------------------
911
912 bool wxFrame::HandlePaint()
913 {
914 RECT rect;
915 if ( GetUpdateRect(GetHwnd(), &rect, FALSE) )
916 {
917 if ( m_iconized )
918 {
919 HICON hIcon = m_icon.Ok() ? GetHiconOf(m_icon)
920 : (HICON)m_defaultIcon;
921
922 // Hold a pointer to the dc so long as the OnPaint() message
923 // is being processed
924 PAINTSTRUCT ps;
925 HDC hdc = ::BeginPaint(GetHwnd(), &ps);
926
927 // Erase background before painting or we get white background
928 MSWDefWindowProc(WM_ICONERASEBKGND, (WORD)(LONG)ps.hdc, 0L);
929
930 if ( hIcon )
931 {
932 RECT rect;
933 ::GetClientRect(GetHwnd(), &rect);
934
935 // FIXME: why hardcoded?
936 static const int icon_width = 32;
937 static const int icon_height = 32;
938
939 int icon_x = (int)((rect.right - icon_width)/2);
940 int icon_y = (int)((rect.bottom - icon_height)/2);
941
942 ::DrawIcon(hdc, icon_x, icon_y, hIcon);
943 }
944
945 ::EndPaint(GetHwnd(), &ps);
946
947 return TRUE;
948 }
949 else
950 {
951 return wxWindow::HandlePaint();
952 }
953 }
954 else
955 {
956 // nothing to paint - processed
957 return TRUE;
958 }
959 }
960
961 bool wxFrame::HandleSize(int x, int y, WXUINT id)
962 {
963 bool processed = FALSE;
964
965 switch ( id )
966 {
967 case SIZENORMAL:
968 // only do it it if we were iconized before, otherwise resizing the
969 // parent frame has a curious side effect of bringing it under it's
970 // children
971 if ( !m_iconized )
972 break;
973
974 // restore all child frames too
975 IconizeChildFrames(FALSE);
976
977 (void)SendIconizeEvent(FALSE);
978
979 // fall through
980
981 case SIZEFULLSCREEN:
982 m_iconized = FALSE;
983 break;
984
985 case SIZEICONIC:
986 // iconize all child frames too
987 IconizeChildFrames(TRUE);
988
989 (void)SendIconizeEvent();
990
991 m_iconized = TRUE;
992 break;
993 }
994
995 if ( !m_iconized )
996 {
997 PositionStatusBar();
998 PositionToolBar();
999
1000 wxSizeEvent event(wxSize(x, y), m_windowId);
1001 event.SetEventObject( this );
1002 processed = GetEventHandler()->ProcessEvent(event);
1003 }
1004
1005 return processed;
1006 }
1007
1008 bool wxFrame::HandleCommand(WXWORD id, WXWORD cmd, WXHWND control)
1009 {
1010 if ( control )
1011 {
1012 // In case it's e.g. a toolbar.
1013 wxWindow *win = wxFindWinFromHandle(control);
1014 if ( win )
1015 return win->MSWCommand(cmd, id);
1016 }
1017
1018 // handle here commands from menus and accelerators
1019 if ( cmd == 0 || cmd == 1 )
1020 {
1021 if ( wxCurrentPopupMenu )
1022 {
1023 wxMenu *popupMenu = wxCurrentPopupMenu;
1024 wxCurrentPopupMenu = NULL;
1025
1026 return popupMenu->MSWCommand(cmd, id);
1027 }
1028
1029 if ( ProcessCommand(id) )
1030 {
1031 return TRUE;
1032 }
1033 }
1034
1035 return FALSE;
1036 }
1037
1038 bool wxFrame::HandleMenuSelect(WXWORD nItem, WXWORD flags, WXHMENU hMenu)
1039 {
1040 int item;
1041 if ( flags == 0xFFFF && hMenu == 0 )
1042 {
1043 // menu was removed from screen
1044 item = -1;
1045 }
1046 else if ( !(flags & MF_POPUP) && !(flags & MF_SEPARATOR) )
1047 {
1048 item = nItem;
1049 }
1050 else
1051 {
1052 // don't give hints for separators (doesn't make sense) nor for the
1053 // items opening popup menus (they don't have them anyhow) but do clear
1054 // the status line - otherwise, we would be left with the help message
1055 // for the previous item which doesn't apply any more
1056 wxStatusBar *statbar = GetStatusBar();
1057 if ( statbar )
1058 {
1059 statbar->SetStatusText(wxEmptyString);
1060 }
1061
1062 return FALSE;
1063 }
1064
1065 wxMenuEvent event(wxEVT_MENU_HIGHLIGHT, item);
1066 event.SetEventObject( this );
1067
1068 return GetEventHandler()->ProcessEvent(event);
1069 }
1070
1071 // ---------------------------------------------------------------------------
1072 // the window proc for wxFrame
1073 // ---------------------------------------------------------------------------
1074
1075 long wxFrame::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
1076 {
1077 long rc = 0;
1078 bool processed = FALSE;
1079
1080 switch ( message )
1081 {
1082 case WM_CLOSE:
1083 // if we can't close, tell the system that we processed the
1084 // message - otherwise it would close us
1085 processed = !Close();
1086 break;
1087
1088 case WM_COMMAND:
1089 {
1090 WORD id, cmd;
1091 WXHWND hwnd;
1092 UnpackCommand((WXWPARAM)wParam, (WXLPARAM)lParam,
1093 &id, &hwnd, &cmd);
1094
1095 processed = HandleCommand(id, cmd, (WXHWND)hwnd);
1096 }
1097 break;
1098
1099 case WM_MENUSELECT:
1100 {
1101 WXWORD item, flags;
1102 WXHMENU hmenu;
1103 UnpackMenuSelect(wParam, lParam, &item, &flags, &hmenu);
1104
1105 processed = HandleMenuSelect(item, flags, hmenu);
1106 }
1107 break;
1108
1109 case WM_PAINT:
1110 processed = HandlePaint();
1111 break;
1112
1113 case WM_QUERYDRAGICON:
1114 {
1115 HICON hIcon = m_icon.Ok() ? GetHiconOf(m_icon)
1116 : (HICON)(m_defaultIcon);
1117 rc = (long)hIcon;
1118 processed = rc != 0;
1119 }
1120 break;
1121
1122 case WM_SIZE:
1123 processed = HandleSize(LOWORD(lParam), HIWORD(lParam), wParam);
1124 break;
1125 }
1126
1127 if ( !processed )
1128 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
1129
1130 return rc;
1131 }
1132