]> git.saurik.com Git - wxWidgets.git/blob - src/msw/frame.cpp
corrections to directory management for Mac OS X
[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 void wxFrame::RemoveChild( wxWindowBase *child )
803 {
804 if ( child == m_winLastFocused )
805 m_winLastFocused = NULL;
806 wxFrameBase::RemoveChild(child);
807 }
808
809 // ----------------------------------------------------------------------------
810 // tool/status bar stuff
811 // ----------------------------------------------------------------------------
812
813 #if wxUSE_TOOLBAR
814
815 wxToolBar* wxFrame::CreateToolBar(long style, wxWindowID id, const wxString& name)
816 {
817 if ( wxFrameBase::CreateToolBar(style, id, name) )
818 {
819 PositionToolBar();
820 }
821
822 return m_frameToolBar;
823 }
824
825 void wxFrame::PositionToolBar()
826 {
827 RECT rect;
828 ::GetClientRect(GetHwnd(), &rect);
829
830 #if wxUSE_STATUSBAR
831 if ( GetStatusBar() )
832 {
833 int statusX, statusY;
834 GetStatusBar()->GetClientSize(&statusX, &statusY);
835 rect.bottom -= statusY;
836 }
837 #endif // wxUSE_STATUSBAR
838
839 if ( GetToolBar() && GetToolBar()->IsShown() )
840 {
841 int tw, th;
842 GetToolBar()->GetSize(&tw, &th);
843
844 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL )
845 {
846 th = rect.bottom;
847 }
848 else
849 {
850 tw = rect.right;
851 }
852
853 // Use the 'real' MSW position here
854 GetToolBar()->SetSize(0, 0, tw, th, wxSIZE_NO_ADJUSTMENTS);
855 }
856 }
857 #endif // wxUSE_TOOLBAR
858
859 // ----------------------------------------------------------------------------
860 // frame state (iconized/maximized/...)
861 // ----------------------------------------------------------------------------
862
863 // propagate our state change to all child frames: this allows us to emulate X
864 // Windows behaviour where child frames float independently of the parent one
865 // on the desktop, but are iconized/restored with it
866 void wxFrame::IconizeChildFrames(bool bIconize)
867 {
868 for ( wxWindowList::Node *node = GetChildren().GetFirst();
869 node;
870 node = node->GetNext() )
871 {
872 wxWindow *win = node->GetData();
873
874 // iconizing the frames with this style under Win95 shell puts them at
875 // the bottom of the screen (as the MDI children) instead of making
876 // them appear in the taskbar because they are, by virtue of this
877 // style, not managed by the taskbar - instead leave Windows take care
878 // of them
879 #ifdef __WIN95__
880 if ( win->GetWindowStyle() & wxFRAME_TOOL_WINDOW )
881 continue;
882 #endif // Win95
883
884 // the child MDI frames are a special case and should not be touched by
885 // the parent frame - instead, they are managed by the user
886 wxFrame *frame = wxDynamicCast(win, wxFrame);
887 if ( frame && !frame->IsMDIChild() )
888 {
889 frame->Iconize(bIconize);
890 }
891 }
892 }
893
894 // ===========================================================================
895 // message processing
896 // ===========================================================================
897
898 // ---------------------------------------------------------------------------
899 // preprocessing
900 // ---------------------------------------------------------------------------
901
902 bool wxFrame::MSWTranslateMessage(WXMSG* pMsg)
903 {
904 if ( wxWindow::MSWTranslateMessage(pMsg) )
905 return TRUE;
906
907 // try the menu bar accels
908 wxMenuBar *menuBar = GetMenuBar();
909 if ( !menuBar )
910 return FALSE;
911
912 const wxAcceleratorTable& acceleratorTable = menuBar->GetAccelTable();
913 return acceleratorTable.Translate(this, pMsg);
914 }
915
916 // ---------------------------------------------------------------------------
917 // our private (non virtual) message handlers
918 // ---------------------------------------------------------------------------
919
920 bool wxFrame::HandlePaint()
921 {
922 RECT rect;
923 if ( GetUpdateRect(GetHwnd(), &rect, FALSE) )
924 {
925 if ( m_iconized )
926 {
927 HICON hIcon = m_icon.Ok() ? GetHiconOf(m_icon)
928 : (HICON)m_defaultIcon;
929
930 // Hold a pointer to the dc so long as the OnPaint() message
931 // is being processed
932 PAINTSTRUCT ps;
933 HDC hdc = ::BeginPaint(GetHwnd(), &ps);
934
935 // Erase background before painting or we get white background
936 MSWDefWindowProc(WM_ICONERASEBKGND, (WORD)(LONG)ps.hdc, 0L);
937
938 if ( hIcon )
939 {
940 RECT rect;
941 ::GetClientRect(GetHwnd(), &rect);
942
943 // FIXME: why hardcoded?
944 static const int icon_width = 32;
945 static const int icon_height = 32;
946
947 int icon_x = (int)((rect.right - icon_width)/2);
948 int icon_y = (int)((rect.bottom - icon_height)/2);
949
950 ::DrawIcon(hdc, icon_x, icon_y, hIcon);
951 }
952
953 ::EndPaint(GetHwnd(), &ps);
954
955 return TRUE;
956 }
957 else
958 {
959 return wxWindow::HandlePaint();
960 }
961 }
962 else
963 {
964 // nothing to paint - processed
965 return TRUE;
966 }
967 }
968
969 bool wxFrame::HandleSize(int x, int y, WXUINT id)
970 {
971 bool processed = FALSE;
972
973 switch ( id )
974 {
975 case SIZENORMAL:
976 // only do it it if we were iconized before, otherwise resizing the
977 // parent frame has a curious side effect of bringing it under it's
978 // children
979 if ( !m_iconized )
980 break;
981
982 // restore all child frames too
983 IconizeChildFrames(FALSE);
984
985 // fall through
986
987 case SIZEFULLSCREEN:
988 m_iconized = FALSE;
989 break;
990
991 case SIZEICONIC:
992 // iconize all child frames too
993 IconizeChildFrames(TRUE);
994
995 m_iconized = TRUE;
996 break;
997 }
998
999 if ( !m_iconized )
1000 {
1001 PositionStatusBar();
1002 PositionToolBar();
1003
1004 wxSizeEvent event(wxSize(x, y), m_windowId);
1005 event.SetEventObject( this );
1006 processed = GetEventHandler()->ProcessEvent(event);
1007 }
1008
1009 return processed;
1010 }
1011
1012 bool wxFrame::HandleCommand(WXWORD id, WXWORD cmd, WXHWND control)
1013 {
1014 if ( control )
1015 {
1016 // In case it's e.g. a toolbar.
1017 wxWindow *win = wxFindWinFromHandle(control);
1018 if ( win )
1019 return win->MSWCommand(cmd, id);
1020 }
1021
1022 // handle here commands from menus and accelerators
1023 if ( cmd == 0 || cmd == 1 )
1024 {
1025 if ( wxCurrentPopupMenu )
1026 {
1027 wxMenu *popupMenu = wxCurrentPopupMenu;
1028 wxCurrentPopupMenu = NULL;
1029
1030 return popupMenu->MSWCommand(cmd, id);
1031 }
1032
1033 if ( ProcessCommand(id) )
1034 {
1035 return TRUE;
1036 }
1037 }
1038
1039 return FALSE;
1040 }
1041
1042 bool wxFrame::HandleMenuSelect(WXWORD nItem, WXWORD flags, WXHMENU hMenu)
1043 {
1044 int item;
1045 if ( flags == 0xFFFF && hMenu == 0 )
1046 {
1047 // menu was removed from screen
1048 item = -1;
1049 }
1050 else if ( !(flags & MF_POPUP) && !(flags & MF_SEPARATOR) )
1051 {
1052 item = nItem;
1053 }
1054 else
1055 {
1056 // don't give hints for separators (doesn't make sense) nor for the
1057 // items opening popup menus (they don't have them anyhow) but do clear
1058 // the status line - otherwise, we would be left with the help message
1059 // for the previous item which doesn't apply any more
1060 wxStatusBar *statbar = GetStatusBar();
1061 if ( statbar )
1062 {
1063 statbar->SetStatusText(wxEmptyString);
1064 }
1065
1066 return FALSE;
1067 }
1068
1069 wxMenuEvent event(wxEVT_MENU_HIGHLIGHT, item);
1070 event.SetEventObject( this );
1071
1072 return GetEventHandler()->ProcessEvent(event);
1073 }
1074
1075 // ---------------------------------------------------------------------------
1076 // the window proc for wxFrame
1077 // ---------------------------------------------------------------------------
1078
1079 long wxFrame::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
1080 {
1081 long rc = 0;
1082 bool processed = FALSE;
1083
1084 switch ( message )
1085 {
1086 case WM_CLOSE:
1087 // if we can't close, tell the system that we processed the
1088 // message - otherwise it would close us
1089 processed = !Close();
1090 break;
1091
1092 case WM_COMMAND:
1093 {
1094 WORD id, cmd;
1095 WXHWND hwnd;
1096 UnpackCommand((WXWPARAM)wParam, (WXLPARAM)lParam,
1097 &id, &hwnd, &cmd);
1098
1099 processed = HandleCommand(id, cmd, (WXHWND)hwnd);
1100 }
1101 break;
1102
1103 case WM_MENUSELECT:
1104 {
1105 WXWORD item, flags;
1106 WXHMENU hmenu;
1107 UnpackMenuSelect(wParam, lParam, &item, &flags, &hmenu);
1108
1109 processed = HandleMenuSelect(item, flags, hmenu);
1110 }
1111 break;
1112
1113 case WM_PAINT:
1114 processed = HandlePaint();
1115 break;
1116
1117 case WM_QUERYDRAGICON:
1118 {
1119 HICON hIcon = m_icon.Ok() ? GetHiconOf(m_icon)
1120 : (HICON)(m_defaultIcon);
1121 rc = (long)hIcon;
1122 processed = rc != 0;
1123 }
1124 break;
1125
1126 case WM_SIZE:
1127 processed = HandleSize(LOWORD(lParam), HIWORD(lParam), wParam);
1128 break;
1129 }
1130
1131 if ( !processed )
1132 rc = wxWindow::MSWWindowProc(message, wParam, lParam);
1133
1134 return rc;
1135 }
1136