moved OnActivate() logic from wxFrame to wxDialog -- this fixes infinite loop when...
[wxWidgets.git] / src / msw / toplevel.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/toplevel.cpp
3 // Purpose: implements wxTopLevelWindow for MSW
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 24.09.01
7 // RCS-ID: $Id$
8 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "toplevel.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/app.h"
33 #include "wx/toplevel.h"
34 #include "wx/string.h"
35 #include "wx/log.h"
36 #include "wx/intl.h"
37 #include "wx/frame.h"
38 #endif //WX_PRECOMP
39
40 #include "wx/msw/private.h"
41
42 #include "wx/popupwin.h"
43
44 #ifndef ICON_BIG
45 #define ICON_BIG 1
46 #endif
47
48 #ifndef ICON_SMALL
49 #define ICON_SMALL 0
50 #endif
51
52 // ----------------------------------------------------------------------------
53 // stubs for missing functions under MicroWindows
54 // ----------------------------------------------------------------------------
55
56 #ifdef __WXMICROWIN__
57
58 // static inline bool IsIconic(HWND WXUNUSED(hwnd)) { return FALSE; }
59 static inline bool IsZoomed(HWND WXUNUSED(hwnd)) { return FALSE; }
60
61 #endif // __WXMICROWIN__
62
63 // ----------------------------------------------------------------------------
64 // globals
65 // ----------------------------------------------------------------------------
66
67 // list of all frames and modeless dialogs
68 wxWindowList wxModelessWindows;
69
70 // the name of the default wxWindows class
71 extern const wxChar *wxCanvasClassName;
72
73 // the hidden parent for wxFRAME_NO_TASKBAR unowned frames
74 wxWindow *wxTopLevelWindowMSW::ms_hiddenParent = NULL;
75
76 // ============================================================================
77 // wxTopLevelWindowMSW implementation
78 // ============================================================================
79
80 BEGIN_EVENT_TABLE(wxTopLevelWindowMSW, wxTopLevelWindowBase)
81 EVT_ACTIVATE(wxTopLevelWindowMSW::OnActivate)
82 END_EVENT_TABLE()
83
84 // ----------------------------------------------------------------------------
85 // wxDialog helpers
86 // ----------------------------------------------------------------------------
87
88 // Dialog window proc
89 LONG APIENTRY _EXPORT
90 wxDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
91 {
92 switch ( message )
93 {
94 case WM_INITDIALOG:
95 // for this message, returning TRUE tells system to set focus to
96 // the first control in the dialog box, but as we set the focus
97 // ourselves, we return FALSE from here as well, so fall through
98
99 default:
100 // for all the other ones, FALSE means that we didn't process the
101 // message
102 return FALSE;
103 }
104 }
105
106 // ----------------------------------------------------------------------------
107 // wxTopLevelWindowMSW creation
108 // ----------------------------------------------------------------------------
109
110 void wxTopLevelWindowMSW::Init()
111 {
112 m_iconized =
113 m_maximizeOnShow = FALSE;
114
115 // unlike (almost?) all other windows, frames are created hidden
116 m_isShown = FALSE;
117
118 // Data to save/restore when calling ShowFullScreen
119 m_fsStyle = 0;
120 m_fsOldWindowStyle = 0;
121 m_fsIsMaximized = FALSE;
122 m_fsIsShowing = FALSE;
123
124 m_winLastFocused = (wxWindow *)NULL;
125 }
126
127 WXDWORD wxTopLevelWindowMSW::MSWGetStyle(long style, WXDWORD *exflags) const
128 {
129 // let the base class deal with the common styles but fix the ones which
130 // don't make sense for us (we also deal with the borders ourselves)
131 WXDWORD msflags = wxWindow::MSWGetStyle
132 (
133 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exflags
134 ) & ~WS_CHILD;
135
136 // first select the kind of window being created
137 //
138 // note that if we don't set WS_POPUP, Windows assumes WS_OVERLAPPED and
139 // creates a window with both caption and border, hence we also test it
140 // below in some other cases
141 if ( style & wxFRAME_TOOL_WINDOW )
142 msflags |= WS_POPUP;
143 else
144 msflags |= WS_OVERLAPPED;
145
146 // border and caption styles
147 if ( style & wxRESIZE_BORDER )
148 msflags |= WS_THICKFRAME;
149 else if ( !(style & wxBORDER_NONE) )
150 msflags |= WS_BORDER;
151 else
152 msflags |= WS_POPUP;
153
154 if ( style & wxCAPTION )
155 msflags |= WS_CAPTION;
156 else
157 msflags |= WS_POPUP;
158
159 // next translate the individual flags
160 if ( style & wxMINIMIZE_BOX )
161 msflags |= WS_MINIMIZEBOX;
162 if ( style & wxMAXIMIZE_BOX )
163 msflags |= WS_MAXIMIZEBOX;
164 if ( style & wxSYSTEM_MENU )
165 msflags |= WS_SYSMENU;
166 if ( style & wxMINIMIZE )
167 msflags |= WS_MINIMIZE;
168 if ( style & wxMAXIMIZE )
169 msflags |= WS_MAXIMIZE;
170
171 // Keep this here because it saves recoding this function in wxTinyFrame
172 #if wxUSE_ITSY_BITSY && !defined(__WIN32__)
173 if ( style & wxTINY_CAPTION_VERT )
174 msflags |= IBS_VERTCAPTION;
175 if ( style & wxTINY_CAPTION_HORIZ )
176 msflags |= IBS_HORZCAPTION;
177 #else
178 if ( style & (wxTINY_CAPTION_VERT | wxTINY_CAPTION_HORIZ) )
179 msflags |= WS_CAPTION;
180 #endif
181
182 if ( exflags )
183 {
184 #if !defined(__WIN16__) && !defined(__SC__)
185 if ( !(GetExtraStyle() & wxTOPLEVEL_EX_DIALOG) )
186 {
187 if ( style & wxFRAME_TOOL_WINDOW )
188 {
189 // create the palette-like window
190 *exflags |= WS_EX_TOOLWINDOW;
191 }
192
193 // We have to solve 2 different problems here:
194 //
195 // 1. frames with wxFRAME_NO_TASKBAR flag shouldn't appear in the
196 // taskbar even if they don't have a parent
197 //
198 // 2. frames without this style should appear in the taskbar even
199 // if they're owned (Windows only puts non owned windows into
200 // the taskbar normally)
201 //
202 // The second one is solved here by using WS_EX_APPWINDOW flag, the
203 // first one is dealt with in our MSWGetParent() method
204 // implementation
205 if ( !(style & wxFRAME_NO_TASKBAR) && GetParent() )
206 {
207 // need to force the frame to appear in the taskbar
208 *exflags |= WS_EX_APPWINDOW;
209 }
210 //else: nothing to do [here]
211 }
212 #endif // !Win16
213
214 if ( style & wxSTAY_ON_TOP )
215 *exflags |= WS_EX_TOPMOST;
216
217 #ifdef __WIN32__
218 if ( GetExtraStyle() & wxFRAME_EX_CONTEXTHELP )
219 *exflags |= WS_EX_CONTEXTHELP;
220 #endif // __WIN32__
221 }
222
223 return msflags;
224 }
225
226 WXHWND wxTopLevelWindowMSW::MSWGetParent() const
227 {
228 // for the frames without wxFRAME_FLOAT_ON_PARENT style we should use NULL
229 // parent HWND or it would be always on top of its parent which is not what
230 // we usually want (in fact, we only want it for frames with the
231 // wxFRAME_FLOAT_ON_PARENT flag)
232 wxWindow *parent;
233 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT) )
234 {
235 parent = GetParent();
236
237 // this flag doesn't make sense then and will be ignored
238 wxASSERT_MSG( parent,
239 _T("wxFRAME_FLOAT_ON_PARENT but no parent?") );
240 }
241 else // don't float on parent, must not be owned
242 {
243 parent = NULL;
244 }
245
246 // now deal with the 2nd taskbar-related problem (see comments above in
247 // MSWGetStyle())
248 if ( HasFlag(wxFRAME_NO_TASKBAR) && !parent )
249 {
250 if ( !ms_hiddenParent )
251 {
252 ms_hiddenParent = new wxTopLevelWindowMSW(NULL, -1, _T(""));
253
254 // we shouldn't leave it in wxTopLevelWindows or we wouldn't
255 // terminate the app when the last user-created frame is deleted --
256 // see ~wxTopLevelWindowMSW
257 wxTopLevelWindows.DeleteObject(ms_hiddenParent);
258 }
259
260 parent = ms_hiddenParent;
261 }
262
263 return parent ? parent->GetHWND() : NULL;
264 }
265
266 bool wxTopLevelWindowMSW::CreateDialog(const void *dlgTemplate,
267 const wxString& title,
268 const wxPoint& pos,
269 const wxSize& size)
270 {
271 #ifdef __WXMICROWIN__
272 // no dialogs support under MicroWin yet
273 return CreateFrame(title, pos, size);
274 #else // !__WXMICROWIN__
275 wxWindow *parent = GetParent();
276
277 // for the dialogs without wxDIALOG_NO_PARENT style, use the top level
278 // app window as parent - this avoids creating modal dialogs without
279 // parent
280 if ( !parent && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT) )
281 {
282 parent = wxTheApp->GetTopWindow();
283
284 if ( parent )
285 {
286 // don't use transient windows as parents, this is dangerous as it
287 // can lead to a crash if the parent is destroyed before the child
288 //
289 // also don't use the window which is currently hidden as then the
290 // dialog would be hidden as well
291 if ( (parent->GetExtraStyle() & wxWS_EX_TRANSIENT) ||
292 !parent->IsShown() )
293 {
294 parent = NULL;
295 }
296 }
297 }
298
299 m_hWnd = (WXHWND)::CreateDialogIndirect
300 (
301 wxGetInstance(),
302 (DLGTEMPLATE*)dlgTemplate,
303 parent ? GetHwndOf(parent) : NULL,
304 (DLGPROC)wxDlgProc
305 );
306
307 if ( !m_hWnd )
308 {
309 wxFAIL_MSG(_("Failed to create dialog. Incorrect DLGTEMPLATE?"));
310
311 wxLogSysError(_("Can't create dialog using memory template"));
312
313 return FALSE;
314 }
315
316 WXDWORD exflags;
317 (void)MSWGetCreateWindowFlags(&exflags);
318
319 if ( exflags )
320 {
321 ::SetWindowLong(GetHwnd(), GWL_EXSTYLE, exflags);
322 ::SetWindowPos(GetHwnd(), NULL, 0, 0, 0, 0,
323 SWP_NOSIZE |
324 SWP_NOMOVE |
325 SWP_NOZORDER |
326 SWP_NOACTIVATE);
327 }
328
329 #if defined(__WIN95__)
330 // For some reason, the system menu is activated when we use the
331 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
332 if ( exflags & WS_EX_CONTEXTHELP )
333 {
334 wxFrame *winTop = wxDynamicCast(wxTheApp->GetTopWindow(), wxFrame);
335 if ( winTop )
336 {
337 wxIcon icon = winTop->GetIcon();
338 if ( icon.Ok() )
339 {
340 ::SendMessage(GetHwnd(), WM_SETICON,
341 (WPARAM)TRUE,
342 (LPARAM)GetHiconOf(icon));
343 }
344 }
345 }
346 #endif // __WIN95__
347
348 // move the dialog to its initial position without forcing repainting
349 int x, y, w, h;
350 if ( !MSWGetCreateWindowCoords(pos, size, x, y, w, h) )
351 {
352 x =
353 w = (int)CW_USEDEFAULT;
354 }
355
356 // we can't use CW_USEDEFAULT here as we're not calling CreateWindow()
357 // and passing CW_USEDEFAULT to MoveWindow() results in resizing the
358 // window to (0, 0) size which breaks quite a lot of things, e.g. the
359 // sizer calculation in wxSizer::Fit()
360 if ( w == (int)CW_USEDEFAULT )
361 {
362 // the exact number doesn't matter, the dialog will be resized
363 // again soon anyhow but it should be big enough to allow
364 // calculation relying on "totalSize - clientSize > 0" work, i.e.
365 // at least greater than the title bar height
366 w =
367 h = 100;
368 }
369
370 if ( x == (int)CW_USEDEFAULT )
371 {
372 // centre it on the screen - what else can we do?
373 wxSize sizeDpy = wxGetDisplaySize();
374
375 x = (sizeDpy.x - w) / 2;
376 y = (sizeDpy.y - h) / 2;
377 }
378
379 if ( !::MoveWindow(GetHwnd(), x, y, w, h, FALSE) )
380 {
381 wxLogLastError(wxT("MoveWindow"));
382 }
383
384 if ( !title.empty() )
385 {
386 ::SetWindowText(GetHwnd(), title);
387 }
388
389 SubclassWin(m_hWnd);
390
391 return TRUE;
392 #endif // __WXMICROWIN__/!__WXMICROWIN__
393 }
394
395 bool wxTopLevelWindowMSW::CreateFrame(const wxString& title,
396 const wxPoint& pos,
397 const wxSize& size)
398 {
399 WXDWORD exflags;
400 WXDWORD flags = MSWGetCreateWindowFlags(&exflags);
401
402 return MSWCreate(wxCanvasClassName, title, pos, size, flags, exflags);
403 }
404
405 bool wxTopLevelWindowMSW::Create(wxWindow *parent,
406 wxWindowID id,
407 const wxString& title,
408 const wxPoint& pos,
409 const wxSize& size,
410 long style,
411 const wxString& name)
412 {
413 // init our fields
414 Init();
415
416 m_windowStyle = style;
417
418 SetName(name);
419
420 m_windowId = id == -1 ? NewControlId() : id;
421
422 wxTopLevelWindows.Append(this);
423
424 if ( parent )
425 parent->AddChild(this);
426
427 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
428 {
429 // we have different dialog templates to allows creation of dialogs
430 // with & without captions under MSWindows, resizeable or not (but a
431 // resizeable dialog always has caption - otherwise it would look too
432 // strange)
433
434 // we need 3 additional WORDs for dialog menu, class and title (as we
435 // don't use DS_SETFONT we don't need the fourth WORD for the font)
436 static const int dlgsize = sizeof(DLGTEMPLATE) + (sizeof(WORD) * 3);
437 DLGTEMPLATE *dlgTemplate = (DLGTEMPLATE *)malloc(dlgsize);
438 memset(dlgTemplate, 0, dlgsize);
439
440 // these values are arbitrary, they won't be used normally anyhow
441 dlgTemplate->x = 34;
442 dlgTemplate->y = 22;
443 dlgTemplate->cx = 144;
444 dlgTemplate->cy = 75;
445
446 // reuse the code in MSWGetStyle() but correct the results slightly for
447 // the dialog
448 dlgTemplate->style = MSWGetStyle(style, NULL);
449
450 // all dialogs are popups
451 dlgTemplate->style |= WS_POPUP;
452
453 // force 3D-look if necessary, it looks impossibly ugly otherwise
454 if ( style & (wxRESIZE_BORDER | wxCAPTION) )
455 dlgTemplate->style |= DS_MODALFRAME;
456
457 bool ret = CreateDialog(dlgTemplate, title, pos, size);
458 free(dlgTemplate);
459
460 return ret;
461 }
462 else // !dialog
463 {
464 return CreateFrame(title, pos, size);
465 }
466 }
467
468 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
469 {
470 if ( this == ms_hiddenParent )
471 {
472 // stop [infinite] recursion which would otherwise happen when we do
473 // "delete ms_hiddenParent" below
474 return;
475 }
476
477 wxTopLevelWindows.DeleteObject(this);
478
479 if ( wxModelessWindows.Find(this) )
480 wxModelessWindows.DeleteObject(this);
481
482 // after destroying an owned window, Windows activates the next top level
483 // window in Z order but it may be different from our owner (to reproduce
484 // this simply Alt-TAB to another application and back before closing the
485 // owned frame) whereas we always want to yield activation to our parent
486 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT) )
487 {
488 wxWindow *parent = GetParent();
489 if ( parent )
490 {
491 ::BringWindowToTop(GetHwndOf(parent));
492 }
493 }
494
495 // If this is the last top-level window, exit.
496 if ( wxTheApp && (wxTopLevelWindows.Number() == 0) )
497 {
498 if ( ms_hiddenParent )
499 {
500 delete ms_hiddenParent;
501 ms_hiddenParent = NULL;
502 }
503
504 wxTheApp->SetTopWindow(NULL);
505
506 if ( wxTheApp->GetExitOnFrameDelete() )
507 {
508 ::PostQuitMessage(0);
509 }
510 }
511 }
512
513 // ----------------------------------------------------------------------------
514 // wxTopLevelWindowMSW showing
515 // ----------------------------------------------------------------------------
516
517 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd)
518 {
519 ::ShowWindow(GetHwnd(), nShowCmd);
520
521 m_iconized = nShowCmd == SW_MINIMIZE;
522 }
523
524 bool wxTopLevelWindowMSW::Show(bool show)
525 {
526 // don't use wxWindow version as we want to call DoShowWindow() ourselves
527 if ( !wxWindowBase::Show(show) )
528 return FALSE;
529
530 int nShowCmd;
531 if ( show )
532 {
533 if ( m_maximizeOnShow )
534 {
535 // show and maximize
536 nShowCmd = SW_MAXIMIZE;
537
538 m_maximizeOnShow = FALSE;
539 }
540 else // just show
541 {
542 nShowCmd = SW_SHOW;
543 }
544 }
545 else // hide
546 {
547 nShowCmd = SW_HIDE;
548 }
549
550 DoShowWindow(nShowCmd);
551
552 if ( show )
553 {
554 ::BringWindowToTop(GetHwnd());
555
556 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, m_windowId);
557 event.SetEventObject( this );
558 GetEventHandler()->ProcessEvent(event);
559 }
560 else // hide
561 {
562 // Try to highlight the correct window (the parent)
563 if ( GetParent() )
564 {
565 HWND hWndParent = GetHwndOf(GetParent());
566 if (hWndParent)
567 ::BringWindowToTop(hWndParent);
568 }
569 }
570
571 return TRUE;
572 }
573
574 // ----------------------------------------------------------------------------
575 // wxTopLevelWindowMSW maximize/minimize
576 // ----------------------------------------------------------------------------
577
578 void wxTopLevelWindowMSW::Maximize(bool maximize)
579 {
580 if ( IsShown() )
581 {
582 // just maximize it directly
583 DoShowWindow(maximize ? SW_MAXIMIZE : SW_RESTORE);
584 }
585 else // hidden
586 {
587 // we can't maximize the hidden frame because it shows it as well, so
588 // just remember that we should do it later in this case
589 m_maximizeOnShow = TRUE;
590 }
591 }
592
593 bool wxTopLevelWindowMSW::IsMaximized() const
594 {
595 return ::IsZoomed(GetHwnd()) != 0;
596 }
597
598 void wxTopLevelWindowMSW::Iconize(bool iconize)
599 {
600 DoShowWindow(iconize ? SW_MINIMIZE : SW_RESTORE);
601 }
602
603 bool wxTopLevelWindowMSW::IsIconized() const
604 {
605 // also update the current state
606 ((wxTopLevelWindowMSW *)this)->m_iconized = ::IsIconic(GetHwnd()) != 0;
607
608 return m_iconized;
609 }
610
611 void wxTopLevelWindowMSW::Restore()
612 {
613 DoShowWindow(SW_RESTORE);
614 }
615
616 // ----------------------------------------------------------------------------
617 // wxTopLevelWindowMSW fullscreen
618 // ----------------------------------------------------------------------------
619
620 bool wxTopLevelWindowMSW::ShowFullScreen(bool show, long style)
621 {
622 if (show)
623 {
624 if (IsFullScreen())
625 return FALSE;
626
627 m_fsIsShowing = TRUE;
628 m_fsStyle = style;
629
630 // zap the frame borders
631
632 // save the 'normal' window style
633 m_fsOldWindowStyle = GetWindowLong((HWND)GetHWND(), GWL_STYLE);
634
635 // save the old position, width & height, maximize state
636 m_fsOldSize = GetRect();
637 m_fsIsMaximized = IsMaximized();
638
639 // decide which window style flags to turn off
640 LONG newStyle = m_fsOldWindowStyle;
641 LONG offFlags = 0;
642
643 if (style & wxFULLSCREEN_NOBORDER)
644 offFlags |= WS_BORDER | WS_THICKFRAME;
645 if (style & wxFULLSCREEN_NOCAPTION)
646 offFlags |= (WS_CAPTION | WS_SYSMENU);
647
648 newStyle &= (~offFlags);
649
650 // change our window style to be compatible with full-screen mode
651 ::SetWindowLong((HWND)GetHWND(), GWL_STYLE, newStyle);
652
653 // resize to the size of the desktop
654 int width, height;
655
656 RECT rect = wxGetWindowRect(::GetDesktopWindow());
657 width = rect.right - rect.left;
658 height = rect.bottom - rect.top;
659
660 SetSize(width, height);
661
662 // now flush the window style cache and actually go full-screen
663 SetWindowPos((HWND)GetHWND(), HWND_TOP, 0, 0, width, height, SWP_FRAMECHANGED);
664
665 wxSizeEvent event(wxSize(width, height), GetId());
666 GetEventHandler()->ProcessEvent(event);
667
668 return TRUE;
669 }
670 else
671 {
672 if (!IsFullScreen())
673 return FALSE;
674
675 m_fsIsShowing = FALSE;
676
677 Maximize(m_fsIsMaximized);
678 SetWindowLong((HWND)GetHWND(),GWL_STYLE, m_fsOldWindowStyle);
679 SetWindowPos((HWND)GetHWND(),HWND_TOP,m_fsOldSize.x, m_fsOldSize.y,
680 m_fsOldSize.width, m_fsOldSize.height, SWP_FRAMECHANGED);
681
682 return TRUE;
683 }
684 }
685
686 // ----------------------------------------------------------------------------
687 // wxTopLevelWindowMSW misc
688 // ----------------------------------------------------------------------------
689
690 void wxTopLevelWindowMSW::SetIcon(const wxIcon& icon)
691 {
692 SetIcons( wxIconBundle( icon ) );
693 }
694
695 void wxTopLevelWindowMSW::SetIcons(const wxIconBundle& icons)
696 {
697 wxTopLevelWindowBase::SetIcons(icons);
698
699 #if defined(__WIN95__) && !defined(__WXMICROWIN__)
700 const wxIcon& sml = icons.GetIcon( wxSize( 16, 16 ) );
701 if( sml.Ok() && sml.GetWidth() == 16 && sml.GetHeight() == 16 )
702 {
703 ::SendMessage( GetHwndOf( this ), WM_SETICON, ICON_SMALL,
704 (LPARAM)GetHiconOf(sml) );
705 }
706
707 const wxIcon& big = icons.GetIcon( wxSize( 32, 32 ) );
708 if( big.Ok() && big.GetWidth() == 32 && big.GetHeight() == 32 )
709 {
710 ::SendMessage( GetHwndOf( this ), WM_SETICON, ICON_BIG,
711 (LPARAM)GetHiconOf(big) );
712 }
713 #endif // __WIN95__
714 }
715
716 bool wxTopLevelWindowMSW::EnableCloseButton(bool enable)
717 {
718 #ifndef __WXMICROWIN__
719 // get system (a.k.a. window) menu
720 HMENU hmenu = ::GetSystemMenu(GetHwnd(), FALSE /* get it */);
721 if ( !hmenu )
722 {
723 wxLogLastError(_T("GetSystemMenu"));
724
725 return FALSE;
726 }
727
728 // enabling/disabling the close item from it also automatically
729 // disables/enables the close title bar button
730 if ( ::EnableMenuItem(hmenu, SC_CLOSE,
731 MF_BYCOMMAND |
732 (enable ? MF_ENABLED : MF_GRAYED)) == -1 )
733 {
734 wxLogLastError(_T("EnableMenuItem(SC_CLOSE)"));
735
736 return FALSE;
737 }
738
739 // update appearance immediately
740 if ( !::DrawMenuBar(GetHwnd()) )
741 {
742 wxLogLastError(_T("DrawMenuBar"));
743 }
744 #endif // !__WXMICROWIN__
745
746 return TRUE;
747 }
748
749 // ----------------------------------------------------------------------------
750 // wxTopLevelWindow event handling
751 // ----------------------------------------------------------------------------
752
753 // Default activation behaviour - set the focus for the first child
754 // subwindow found.
755 void wxTopLevelWindowMSW::OnActivate(wxActivateEvent& event)
756 {
757 if ( event.GetActive() )
758 {
759 // restore focus to the child which was last focused
760 wxLogTrace(_T("focus"), _T("wxTLW %08x activated."), m_hWnd);
761
762 wxWindow *parent = m_winLastFocused ? m_winLastFocused->GetParent()
763 : NULL;
764 if ( !parent )
765 {
766 parent = this;
767 }
768
769 wxSetFocusToChild(parent, &m_winLastFocused);
770 }
771 else // deactivating
772 {
773 // remember the last focused child if it is our child
774 m_winLastFocused = FindFocus();
775
776 // so we NULL it out if it's a child from some other frame
777 wxWindow *win = m_winLastFocused;
778 while ( win )
779 {
780 if ( win->IsTopLevel() )
781 {
782 if ( win != this )
783 {
784 m_winLastFocused = NULL;
785 }
786
787 break;
788 }
789
790 win = win->GetParent();
791 }
792
793 wxLogTrace(_T("focus"),
794 _T("wxTLW %08x deactivated, last focused: %08x."),
795 m_hWnd,
796 m_winLastFocused ? GetHwndOf(m_winLastFocused)
797 : NULL);
798
799 event.Skip();
800 }
801 }
802