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