Code for single instance and bring-to-top under CE.
[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 licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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/dialog.h"
35 #include "wx/string.h"
36 #include "wx/log.h"
37 #include "wx/intl.h"
38 #include "wx/frame.h"
39 #include "wx/containr.h" // wxSetFocusToChild()
40 #endif //WX_PRECOMP
41
42 #include "wx/module.h"
43
44 #include "wx/msw/private.h"
45 #if defined(__WXWINCE__)
46 #include <ole2.h>
47 #include <shellapi.h>
48 #if _WIN32_WCE < 400
49 #include <aygshell.h>
50 #endif
51 #include "wx/msw/wince/missing.h"
52 #endif
53
54 #include "wx/msw/missing.h"
55 #include "wx/msw/winundef.h"
56
57 #include "wx/display.h"
58
59 #ifndef ICON_BIG
60 #define ICON_BIG 1
61 #endif
62
63 #ifndef ICON_SMALL
64 #define ICON_SMALL 0
65 #endif
66
67 // ----------------------------------------------------------------------------
68 // stubs for missing functions under MicroWindows
69 // ----------------------------------------------------------------------------
70
71 #ifdef __WXMICROWIN__
72
73 // static inline bool IsIconic(HWND WXUNUSED(hwnd)) { return FALSE; }
74 static inline bool IsZoomed(HWND WXUNUSED(hwnd)) { return FALSE; }
75
76 #endif // __WXMICROWIN__
77
78 // NB: wxDlgProc must be defined here and not in dialog.cpp because the latter
79 // is not included by wxUniv build which does need wxDlgProc
80 LONG APIENTRY _EXPORT
81 wxDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
82
83 // ----------------------------------------------------------------------------
84 // globals
85 // ----------------------------------------------------------------------------
86
87 // list of all frames and modeless dialogs
88 wxWindowList wxModelessWindows;
89
90 // the name of the default wxWindows class
91 #ifdef __WXWINCE__
92 extern wxChar *wxCanvasClassName;
93 #else
94 extern const wxChar *wxCanvasClassName;
95 #endif
96
97 // ----------------------------------------------------------------------------
98 // wxTLWHiddenParentModule: used to manage the hidden parent window (we need a
99 // module to ensure that the window is always deleted)
100 // ----------------------------------------------------------------------------
101
102 class wxTLWHiddenParentModule : public wxModule
103 {
104 public:
105 // module init/finalize
106 virtual bool OnInit();
107 virtual void OnExit();
108
109 // get the hidden window (creates on demand)
110 static HWND GetHWND();
111
112 private:
113 // the HWND of the hidden parent
114 static HWND ms_hwnd;
115
116 // the class used to create it
117 static const wxChar *ms_className;
118
119 DECLARE_DYNAMIC_CLASS(wxTLWHiddenParentModule)
120 };
121
122 IMPLEMENT_DYNAMIC_CLASS(wxTLWHiddenParentModule, wxModule)
123
124 // ============================================================================
125 // wxTopLevelWindowMSW implementation
126 // ============================================================================
127
128 BEGIN_EVENT_TABLE(wxTopLevelWindowMSW, wxTopLevelWindowBase)
129 EVT_ACTIVATE(wxTopLevelWindowMSW::OnActivate)
130 END_EVENT_TABLE()
131
132 // ----------------------------------------------------------------------------
133 // wxTopLevelWindowMSW creation
134 // ----------------------------------------------------------------------------
135
136 void wxTopLevelWindowMSW::Init()
137 {
138 m_iconized =
139 m_maximizeOnShow = FALSE;
140
141 // Data to save/restore when calling ShowFullScreen
142 m_fsStyle = 0;
143 m_fsOldWindowStyle = 0;
144 m_fsIsMaximized = FALSE;
145 m_fsIsShowing = FALSE;
146
147 m_winLastFocused = (wxWindow *)NULL;
148 }
149
150 WXDWORD wxTopLevelWindowMSW::MSWGetStyle(long style, WXDWORD *exflags) const
151 {
152 // let the base class deal with the common styles but fix the ones which
153 // don't make sense for us (we also deal with the borders ourselves)
154 WXDWORD msflags = wxWindow::MSWGetStyle
155 (
156 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exflags
157 ) & ~WS_CHILD & ~WS_VISIBLE;
158
159 #if defined(__WXWINCE__) && _WIN32_WCE < 400
160 msflags |= WS_VISIBLE;
161 #endif
162
163 // first select the kind of window being created
164 //
165 // note that if we don't set WS_POPUP, Windows assumes WS_OVERLAPPED and
166 // creates a window with both caption and border, hence we also test it
167 // below in some other cases
168 if ( style & wxFRAME_TOOL_WINDOW )
169 {
170 msflags |= WS_POPUP;
171 }
172 //else: WS_OVERLAPPED is 0 anyhow, so it is on by default
173
174 // border and caption styles
175 if ( style & wxRESIZE_BORDER )
176 msflags |= WS_THICKFRAME;
177 else if ( exflags && ((style & wxBORDER_DOUBLE) || (style & wxBORDER_RAISED)) )
178 *exflags |= WS_EX_DLGMODALFRAME;
179 else if ( !(style & wxBORDER_NONE) )
180 msflags |= WS_BORDER;
181 else
182 msflags |= WS_POPUP;
183
184 // normally we consider that all windows without caption must be popups,
185 // but CE is an exception: there windows normally do not have the caption
186 // but shouldn't be made popups as popups can't have menus and don't look
187 // like normal windows anyhow
188 if ( style & wxCAPTION )
189 msflags |= WS_CAPTION;
190 #ifndef __WXWINCE__
191 else
192 msflags |= WS_POPUP;
193 #endif // !__WXWINCE__
194
195 // next translate the individual flags
196 if ( style & wxMINIMIZE_BOX )
197 msflags |= WS_MINIMIZEBOX;
198 if ( style & wxMAXIMIZE_BOX )
199 msflags |= WS_MAXIMIZEBOX;
200 if ( style & wxSYSTEM_MENU )
201 msflags |= WS_SYSMENU;
202
203 // NB: under CE these 2 styles are not supported currently, we should
204 // call Minimize()/Maximize() "manually" if we want to support them
205 if ( style & wxMINIMIZE )
206 msflags |= WS_MINIMIZE;
207 if ( style & wxMAXIMIZE )
208 msflags |= WS_MAXIMIZE;
209
210 // Keep this here because it saves recoding this function in wxTinyFrame
211 if ( style & (wxTINY_CAPTION_VERT | wxTINY_CAPTION_HORIZ) )
212 msflags |= WS_CAPTION;
213
214 if ( exflags )
215 {
216 // there is no taskbar under CE, so omit all this
217 #ifndef __WXWINCE__
218 if ( !(GetExtraStyle() & wxTOPLEVEL_EX_DIALOG) )
219 {
220 if ( style & wxFRAME_TOOL_WINDOW )
221 {
222 // create the palette-like window
223 *exflags |= WS_EX_TOOLWINDOW;
224
225 // tool windows shouldn't appear on the taskbar (as documented)
226 style |= wxFRAME_NO_TASKBAR;
227 }
228
229 // We have to solve 2 different problems here:
230 //
231 // 1. frames with wxFRAME_NO_TASKBAR flag shouldn't appear in the
232 // taskbar even if they don't have a parent
233 //
234 // 2. frames without this style should appear in the taskbar even
235 // if they're owned (Windows only puts non owned windows into
236 // the taskbar normally)
237 //
238 // The second one is solved here by using WS_EX_APPWINDOW flag, the
239 // first one is dealt with in our MSWGetParent() method
240 // implementation
241 if ( !(style & wxFRAME_NO_TASKBAR) && GetParent() )
242 {
243 // need to force the frame to appear in the taskbar
244 *exflags |= WS_EX_APPWINDOW;
245 }
246 //else: nothing to do [here]
247 }
248 #endif // !__WXWINCE__
249
250 if ( style & wxSTAY_ON_TOP )
251 *exflags |= WS_EX_TOPMOST;
252
253 if ( GetExtraStyle() & wxFRAME_EX_CONTEXTHELP )
254 *exflags |= WS_EX_CONTEXTHELP;
255 }
256
257 return msflags;
258 }
259
260 WXHWND wxTopLevelWindowMSW::MSWGetParent() const
261 {
262 // for the frames without wxFRAME_FLOAT_ON_PARENT style we should use NULL
263 // parent HWND or it would be always on top of its parent which is not what
264 // we usually want (in fact, we only want it for frames with the
265 // wxFRAME_FLOAT_ON_PARENT flag)
266 HWND hwndParent = NULL;
267 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT) )
268 {
269 const wxWindow *parent = GetParent();
270
271 if ( !parent )
272 {
273 // this flag doesn't make sense then and will be ignored
274 wxFAIL_MSG( _T("wxFRAME_FLOAT_ON_PARENT but no parent?") );
275 }
276 else
277 {
278 hwndParent = GetHwndOf(parent);
279 }
280 }
281 //else: don't float on parent, must not be owned
282
283 // now deal with the 2nd taskbar-related problem (see comments above in
284 // MSWGetStyle())
285 if ( HasFlag(wxFRAME_NO_TASKBAR) && !hwndParent )
286 {
287 // use hidden parent
288 hwndParent = wxTLWHiddenParentModule::GetHWND();
289 }
290
291 return (WXHWND)hwndParent;
292 }
293
294 bool wxTopLevelWindowMSW::CreateDialog(const void *dlgTemplate,
295 const wxString& title,
296 const wxPoint& pos,
297 const wxSize& size)
298 {
299 #ifdef __WXMICROWIN__
300 // no dialogs support under MicroWin yet
301 return CreateFrame(title, pos, size);
302 #else // !__WXMICROWIN__
303 wxWindow *parent = GetParent();
304
305 // for the dialogs without wxDIALOG_NO_PARENT style, use the top level
306 // app window as parent - this avoids creating modal dialogs without
307 // parent
308 if ( !parent && !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT) )
309 {
310 parent = wxTheApp->GetTopWindow();
311
312 if ( parent )
313 {
314 // don't use transient windows as parents, this is dangerous as it
315 // can lead to a crash if the parent is destroyed before the child
316 //
317 // also don't use the window which is currently hidden as then the
318 // dialog would be hidden as well
319 if ( (parent->GetExtraStyle() & wxWS_EX_TRANSIENT) ||
320 !parent->IsShown() )
321 {
322 parent = NULL;
323 }
324 }
325 }
326
327 m_hWnd = (WXHWND)::CreateDialogIndirect
328 (
329 wxGetInstance(),
330 (DLGTEMPLATE*)dlgTemplate,
331 parent ? GetHwndOf(parent) : NULL,
332 (DLGPROC)wxDlgProc
333 );
334
335 if ( !m_hWnd )
336 {
337 wxFAIL_MSG(wxT("Failed to create dialog. Incorrect DLGTEMPLATE?"));
338
339 wxLogSysError(wxT("Can't create dialog using memory template"));
340
341 return FALSE;
342 }
343
344 WXDWORD exflags;
345 (void)MSWGetCreateWindowFlags(&exflags);
346
347 if ( exflags )
348 {
349 ::SetWindowLong(GetHwnd(), GWL_EXSTYLE, exflags);
350 ::SetWindowPos(GetHwnd(),
351 exflags & WS_EX_TOPMOST ? HWND_TOPMOST : 0,
352 0, 0, 0, 0,
353 SWP_NOSIZE |
354 SWP_NOMOVE |
355 (exflags & WS_EX_TOPMOST ? 0 : SWP_NOZORDER) |
356 SWP_NOACTIVATE);
357 }
358
359 #if defined(__WIN95__)
360 // For some reason, the system menu is activated when we use the
361 // WS_EX_CONTEXTHELP style, so let's set a reasonable icon
362 if ( exflags & WS_EX_CONTEXTHELP )
363 {
364 wxFrame *winTop = wxDynamicCast(wxTheApp->GetTopWindow(), wxFrame);
365 if ( winTop )
366 {
367 wxIcon icon = winTop->GetIcon();
368 if ( icon.Ok() )
369 {
370 ::SendMessage(GetHwnd(), WM_SETICON,
371 (WPARAM)TRUE,
372 (LPARAM)GetHiconOf(icon));
373 }
374 }
375 }
376 #endif // __WIN95__
377
378 // move the dialog to its initial position without forcing repainting
379 int x, y, w, h;
380 (void)MSWGetCreateWindowCoords(pos, size, x, y, w, h);
381
382 if ( x == (int)CW_USEDEFAULT )
383 {
384 // centre it on the screen - what else can we do?
385 wxSize sizeDpy = wxGetDisplaySize();
386
387 x = (sizeDpy.x - w) / 2;
388 y = (sizeDpy.y - h) / 2;
389 }
390
391 #ifndef __WXWINCE__
392 if ( !::MoveWindow(GetHwnd(), x, y, w, h, FALSE) )
393 {
394 wxLogLastError(wxT("MoveWindow"));
395 }
396 #endif
397
398 if ( !title.empty() )
399 {
400 ::SetWindowText(GetHwnd(), title);
401 }
402
403 SubclassWin(m_hWnd);
404
405 return TRUE;
406 #endif // __WXMICROWIN__/!__WXMICROWIN__
407 }
408
409 bool wxTopLevelWindowMSW::CreateFrame(const wxString& title,
410 const wxPoint& pos,
411 const wxSize& size)
412 {
413 WXDWORD exflags;
414 WXDWORD flags = MSWGetCreateWindowFlags(&exflags);
415
416 return MSWCreate(wxCanvasClassName, title, pos, size, flags, exflags);
417 }
418
419 bool wxTopLevelWindowMSW::Create(wxWindow *parent,
420 wxWindowID id,
421 const wxString& title,
422 const wxPoint& pos,
423 const wxSize& size,
424 long style,
425 const wxString& name)
426 {
427 bool ret wxDUMMY_INITIALIZE(false);
428
429 // init our fields
430 Init();
431
432 wxSize sizeReal = size;
433 if ( !sizeReal.IsFullySpecified() )
434 {
435 sizeReal.SetDefaults(GetDefaultSize());
436 }
437
438 m_windowStyle = style;
439
440 SetName(name);
441
442 m_windowId = id == -1 ? NewControlId() : id;
443
444 wxTopLevelWindows.Append(this);
445
446 if ( parent )
447 parent->AddChild(this);
448
449 if ( GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
450 {
451 // we have different dialog templates to allows creation of dialogs
452 // with & without captions under MSWindows, resizeable or not (but a
453 // resizeable dialog always has caption - otherwise it would look too
454 // strange)
455
456 // we need 3 additional WORDs for dialog menu, class and title (as we
457 // don't use DS_SETFONT we don't need the fourth WORD for the font)
458 static const int dlgsize = sizeof(DLGTEMPLATE) + (sizeof(WORD) * 3);
459 DLGTEMPLATE *dlgTemplate = (DLGTEMPLATE *)malloc(dlgsize);
460 memset(dlgTemplate, 0, dlgsize);
461
462 // these values are arbitrary, they won't be used normally anyhow
463 dlgTemplate->x = 34;
464 dlgTemplate->y = 22;
465 dlgTemplate->cx = 144;
466 dlgTemplate->cy = 75;
467
468 // reuse the code in MSWGetStyle() but correct the results slightly for
469 // the dialog
470 dlgTemplate->style = MSWGetStyle(style, NULL);
471
472 // all dialogs are popups
473 dlgTemplate->style |= WS_POPUP;
474
475 // force 3D-look if necessary, it looks impossibly ugly otherwise
476 if ( style & (wxRESIZE_BORDER | wxCAPTION) )
477 dlgTemplate->style |= DS_MODALFRAME;
478
479 ret = CreateDialog(dlgTemplate, title, pos, sizeReal);
480 free(dlgTemplate);
481 }
482 else // !dialog
483 {
484 ret = CreateFrame(title, pos, sizeReal);
485 }
486
487 if ( ret && !(GetWindowStyleFlag() & wxCLOSE_BOX) )
488 {
489 EnableCloseButton(false);
490 }
491
492 // for some reason we need to manually send ourselves this message as
493 // otherwise the mnemonics are always shown -- even if they're configured
494 // to be hidden until "Alt" is pressed in the control panel
495 //
496 // this could indicate a bug somewhere else but for now this is the only
497 // fix we have
498 if ( ret )
499 {
500 SendMessage
501 (
502 GetHwnd(),
503 WM_UPDATEUISTATE,
504 MAKEWPARAM(UIS_INITIALIZE, UISF_HIDEFOCUS | UISF_HIDEACCEL),
505 0
506 );
507 }
508
509 return ret;
510 }
511
512 wxTopLevelWindowMSW::~wxTopLevelWindowMSW()
513 {
514 if ( wxModelessWindows.Find(this) )
515 wxModelessWindows.DeleteObject(this);
516
517 // after destroying an owned window, Windows activates the next top level
518 // window in Z order but it may be different from our owner (to reproduce
519 // this simply Alt-TAB to another application and back before closing the
520 // owned frame) whereas we always want to yield activation to our parent
521 if ( HasFlag(wxFRAME_FLOAT_ON_PARENT) )
522 {
523 wxWindow *parent = GetParent();
524 if ( parent )
525 {
526 ::BringWindowToTop(GetHwndOf(parent));
527 }
528 }
529 }
530
531 // ----------------------------------------------------------------------------
532 // wxTopLevelWindowMSW showing
533 // ----------------------------------------------------------------------------
534
535 void wxTopLevelWindowMSW::DoShowWindow(int nShowCmd)
536 {
537 ::ShowWindow(GetHwnd(), nShowCmd);
538
539 m_iconized = nShowCmd == SW_MINIMIZE;
540 }
541
542 bool wxTopLevelWindowMSW::Show(bool show)
543 {
544 // don't use wxWindow version as we want to call DoShowWindow() ourselves
545 if ( !wxWindowBase::Show(show) )
546 return FALSE;
547
548 int nShowCmd;
549 if ( show )
550 {
551 if ( m_maximizeOnShow )
552 {
553 // show and maximize
554 nShowCmd = SW_MAXIMIZE;
555
556 m_maximizeOnShow = FALSE;
557 }
558 else // just show
559 {
560 if ( GetWindowStyle() & wxFRAME_TOOL_WINDOW )
561 nShowCmd = SW_SHOWNA;
562 else
563 nShowCmd = SW_SHOW;
564 }
565 }
566 else // hide
567 {
568 nShowCmd = SW_HIDE;
569 }
570
571 DoShowWindow(nShowCmd);
572
573 if ( show )
574 {
575 ::BringWindowToTop(GetHwnd());
576
577 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, m_windowId);
578 event.SetEventObject( this );
579 GetEventHandler()->ProcessEvent(event);
580 }
581 else // hide
582 {
583 // Try to highlight the correct window (the parent)
584 if ( GetParent() )
585 {
586 HWND hWndParent = GetHwndOf(GetParent());
587 if (hWndParent)
588 ::BringWindowToTop(hWndParent);
589 }
590 }
591
592 return TRUE;
593 }
594
595 // ----------------------------------------------------------------------------
596 // wxTopLevelWindowMSW maximize/minimize
597 // ----------------------------------------------------------------------------
598
599 void wxTopLevelWindowMSW::Maximize(bool maximize)
600 {
601 if ( IsShown() )
602 {
603 // just maximize it directly
604 DoShowWindow(maximize ? SW_MAXIMIZE : SW_RESTORE);
605 }
606 else // hidden
607 {
608 // we can't maximize the hidden frame because it shows it as well, so
609 // just remember that we should do it later in this case
610 m_maximizeOnShow = maximize;
611 }
612 }
613
614 bool wxTopLevelWindowMSW::IsMaximized() const
615 {
616 #ifdef __WXWINCE__
617 return FALSE;
618 #else
619 return ::IsZoomed(GetHwnd()) != 0;
620 #endif
621 }
622
623 void wxTopLevelWindowMSW::Iconize(bool iconize)
624 {
625 DoShowWindow(iconize ? SW_MINIMIZE : SW_RESTORE);
626 }
627
628 bool wxTopLevelWindowMSW::IsIconized() const
629 {
630 #ifdef __WXWINCE__
631 return FALSE;
632 #else
633 // also update the current state
634 ((wxTopLevelWindowMSW *)this)->m_iconized = ::IsIconic(GetHwnd()) != 0;
635
636 return m_iconized;
637 #endif
638 }
639
640 void wxTopLevelWindowMSW::Restore()
641 {
642 DoShowWindow(SW_RESTORE);
643 }
644
645 // ----------------------------------------------------------------------------
646 // wxTopLevelWindowMSW fullscreen
647 // ----------------------------------------------------------------------------
648
649 bool wxTopLevelWindowMSW::ShowFullScreen(bool show, long style)
650 {
651 if ( show == IsFullScreen() )
652 {
653 // nothing to do
654 return TRUE;
655 }
656
657 m_fsIsShowing = show;
658
659 if ( show )
660 {
661 m_fsStyle = style;
662
663 // zap the frame borders
664
665 // save the 'normal' window style
666 m_fsOldWindowStyle = GetWindowLong(GetHwnd(), GWL_STYLE);
667
668 // save the old position, width & height, maximize state
669 m_fsOldSize = GetRect();
670 m_fsIsMaximized = IsMaximized();
671
672 // decide which window style flags to turn off
673 LONG newStyle = m_fsOldWindowStyle;
674 LONG offFlags = 0;
675
676 if (style & wxFULLSCREEN_NOBORDER)
677 {
678 offFlags |= WS_BORDER;
679 #ifndef __WXWINCE__
680 offFlags |= WS_THICKFRAME;
681 #endif
682 }
683 if (style & wxFULLSCREEN_NOCAPTION)
684 offFlags |= WS_CAPTION | WS_SYSMENU;
685
686 newStyle &= ~offFlags;
687
688 // change our window style to be compatible with full-screen mode
689 ::SetWindowLong(GetHwnd(), GWL_STYLE, newStyle);
690
691 wxRect rect;
692 #if wxUSE_DISPLAY
693 // resize to the size of the display containing us
694 int dpy = wxDisplay::GetFromWindow(this);
695 if ( dpy != wxNOT_FOUND )
696 {
697 rect = wxDisplay(dpy).GetGeometry();
698 }
699 else // fall back to the main desktop
700 #else // wxUSE_DISPLAY
701 {
702 // resize to the size of the desktop
703 wxCopyRECTToRect(wxGetWindowRect(::GetDesktopWindow()), rect);
704 #ifdef __WXWINCE__
705 // FIXME: size of the bottom menu (toolbar)
706 // should be taken in account
707 rect.height += rect.y;
708 rect.y = 0;
709 #endif
710 }
711 #endif // wxUSE_DISPLAY
712
713 SetSize(rect);
714
715 // now flush the window style cache and actually go full-screen
716 long flags = SWP_FRAMECHANGED;
717
718 // showing the frame full screen should also show it if it's still
719 // hidden
720 if ( !IsShown() )
721 {
722 // don't call wxWindow version to avoid flicker from calling
723 // ::ShowWindow() -- we're going to show the window at the correct
724 // location directly below -- but do call the wxWindowBase version
725 // to sync the internal m_isShown flag
726 wxWindowBase::Show();
727
728 flags |= SWP_SHOWWINDOW;
729 }
730
731 SetWindowPos(GetHwnd(), HWND_TOP,
732 rect.x, rect.y, rect.width, rect.height,
733 flags);
734
735 #if defined(__WXWINCE__) && _WIN32_WCE < 400
736 ::SHFullScreen(GetHwnd(), SHFS_HIDETASKBAR | SHFS_HIDESIPBUTTON);
737 #endif
738
739 // finally send an event allowing the window to relayout itself &c
740 wxSizeEvent event(rect.GetSize(), GetId());
741 GetEventHandler()->ProcessEvent(event);
742 }
743 else // stop showing full screen
744 {
745 #if defined(__WXWINCE__) && _WIN32_WCE < 400
746 ::SHFullScreen(GetHwnd(), SHFS_SHOWTASKBAR | SHFS_SHOWSIPBUTTON);
747 #endif
748 Maximize(m_fsIsMaximized);
749 SetWindowLong(GetHwnd(),GWL_STYLE, m_fsOldWindowStyle);
750 SetWindowPos(GetHwnd(),HWND_TOP,m_fsOldSize.x, m_fsOldSize.y,
751 m_fsOldSize.width, m_fsOldSize.height, SWP_FRAMECHANGED);
752 }
753
754 return TRUE;
755 }
756
757 // ----------------------------------------------------------------------------
758 // wxTopLevelWindowMSW misc
759 // ----------------------------------------------------------------------------
760
761 void wxTopLevelWindowMSW::SetIcon(const wxIcon& icon)
762 {
763 SetIcons( wxIconBundle( icon ) );
764 }
765
766 void wxTopLevelWindowMSW::SetIcons(const wxIconBundle& icons)
767 {
768 wxTopLevelWindowBase::SetIcons(icons);
769
770 #if defined(__WIN95__) && !defined(__WXMICROWIN__)
771 const wxIcon& sml = icons.GetIcon( wxSize( 16, 16 ) );
772 if( sml.Ok() && sml.GetWidth() == 16 && sml.GetHeight() == 16 )
773 {
774 ::SendMessage( GetHwndOf( this ), WM_SETICON, ICON_SMALL,
775 (LPARAM)GetHiconOf(sml) );
776 }
777
778 const wxIcon& big = icons.GetIcon( wxSize( 32, 32 ) );
779 if( big.Ok() && big.GetWidth() == 32 && big.GetHeight() == 32 )
780 {
781 ::SendMessage( GetHwndOf( this ), WM_SETICON, ICON_BIG,
782 (LPARAM)GetHiconOf(big) );
783 }
784 #endif // __WIN95__
785 }
786
787 bool wxTopLevelWindowMSW::EnableCloseButton(bool enable)
788 {
789 #if !defined(__WXMICROWIN__)
790 // get system (a.k.a. window) menu
791 HMENU hmenu = GetSystemMenu(GetHwnd(), FALSE /* get it */);
792 if ( !hmenu )
793 {
794 // no system menu at all -- ok if we want to remove the close button
795 // anyhow, but bad if we want to show it
796 return !enable;
797 }
798
799 // enabling/disabling the close item from it also automatically
800 // disables/enables the close title bar button
801 if ( ::EnableMenuItem(hmenu, SC_CLOSE,
802 MF_BYCOMMAND |
803 (enable ? MF_ENABLED : MF_GRAYED)) == -1 )
804 {
805 wxLogLastError(_T("EnableMenuItem(SC_CLOSE)"));
806
807 return FALSE;
808 }
809
810 // update appearance immediately
811 if ( !::DrawMenuBar(GetHwnd()) )
812 {
813 wxLogLastError(_T("DrawMenuBar"));
814 }
815 #endif // !__WXMICROWIN__
816
817 return TRUE;
818 }
819
820 #ifndef __WXWINCE__
821
822 bool wxTopLevelWindowMSW::SetShape(const wxRegion& region)
823 {
824 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), FALSE,
825 _T("Shaped windows must be created with the wxFRAME_SHAPED style."));
826
827 // The empty region signifies that the shape should be removed from the
828 // window.
829 if ( region.IsEmpty() )
830 {
831 if (::SetWindowRgn(GetHwnd(), NULL, TRUE) == 0)
832 {
833 wxLogLastError(_T("SetWindowRgn"));
834 return FALSE;
835 }
836 return TRUE;
837 }
838
839 // Windows takes ownership of the region, so
840 // we'll have to make a copy of the region to give to it.
841 DWORD noBytes = ::GetRegionData(GetHrgnOf(region), 0, NULL);
842 RGNDATA *rgnData = (RGNDATA*) new char[noBytes];
843 ::GetRegionData(GetHrgnOf(region), noBytes, rgnData);
844 HRGN hrgn = ::ExtCreateRegion(NULL, noBytes, rgnData);
845 delete[] (char*) rgnData;
846
847 // SetWindowRgn expects the region to be in coordinants
848 // relative to the window, not the client area. Figure
849 // out the offset, if any.
850 RECT rect;
851 DWORD dwStyle = ::GetWindowLong(GetHwnd(), GWL_STYLE);
852 DWORD dwExStyle = ::GetWindowLong(GetHwnd(), GWL_EXSTYLE);
853 ::GetClientRect(GetHwnd(), &rect);
854 ::AdjustWindowRectEx(&rect, dwStyle, FALSE, dwExStyle);
855 ::OffsetRgn(hrgn, -rect.left, -rect.top);
856
857 // Now call the shape API with the new region.
858 if (::SetWindowRgn(GetHwnd(), hrgn, TRUE) == 0)
859 {
860 wxLogLastError(_T("SetWindowRgn"));
861 return FALSE;
862 }
863 return TRUE;
864 }
865
866 #endif // !__WXWINCE__
867
868 // ----------------------------------------------------------------------------
869 // wxTopLevelWindow event handling
870 // ----------------------------------------------------------------------------
871
872 // Default activation behaviour - set the focus for the first child
873 // subwindow found.
874 void wxTopLevelWindowMSW::OnActivate(wxActivateEvent& event)
875 {
876 if ( event.GetActive() )
877 {
878 // restore focus to the child which was last focused unless we already
879 // have it
880 wxLogTrace(_T("focus"), _T("wxTLW %08x activated."), (int) m_hWnd);
881
882 wxWindow *winFocus = FindFocus();
883 if ( !winFocus || wxGetTopLevelParent(winFocus) != this )
884 {
885 wxWindow *parent = m_winLastFocused ? m_winLastFocused->GetParent()
886 : NULL;
887 if ( !parent )
888 {
889 parent = this;
890 }
891
892 wxSetFocusToChild(parent, &m_winLastFocused);
893 }
894 }
895 else // deactivating
896 {
897 // remember the last focused child if it is our child
898 m_winLastFocused = FindFocus();
899
900 if ( m_winLastFocused )
901 {
902 // let it know that it doesn't have focus any more
903 m_winLastFocused->HandleKillFocus((WXHWND)NULL);
904
905 // and don't remember it if it's a child from some other frame
906 if ( wxGetTopLevelParent(m_winLastFocused) != this )
907 {
908 m_winLastFocused = NULL;
909 }
910 }
911
912 wxLogTrace(_T("focus"),
913 _T("wxTLW %08x deactivated, last focused: %08x."),
914 (int) m_hWnd,
915 (int) (m_winLastFocused ? GetHwndOf(m_winLastFocused)
916 : NULL));
917
918 event.Skip();
919 }
920 }
921
922 // the DialogProc for all wxWindows dialogs
923 LONG APIENTRY _EXPORT
924 wxDlgProc(HWND hDlg,
925 UINT message,
926 WPARAM WXUNUSED(wParam),
927 LPARAM WXUNUSED(lParam))
928 {
929 switch ( message )
930 {
931 case WM_INITDIALOG:
932 // for this message, returning TRUE tells system to set focus to
933 // the first control in the dialog box, but as we set the focus
934 // ourselves, we return FALSE from here as well, so fall through
935 #ifdef __WXWINCE__
936 {
937 SHINITDLGINFO shidi;
938 shidi.dwMask = SHIDIM_FLAGS;
939 shidi.dwFlags = SHIDIF_DONEBUTTON |
940 SHIDIF_SIZEDLGFULLSCREEN;
941 shidi.hDlg = hDlg;
942 SHInitDialog( &shidi );
943 }
944 #endif
945
946 default:
947 // for all the other ones, FALSE means that we didn't process the
948 // message
949 return FALSE;
950 }
951 }
952
953 // ============================================================================
954 // wxTLWHiddenParentModule implementation
955 // ============================================================================
956
957 HWND wxTLWHiddenParentModule::ms_hwnd = NULL;
958
959 const wxChar *wxTLWHiddenParentModule::ms_className = NULL;
960
961 bool wxTLWHiddenParentModule::OnInit()
962 {
963 ms_hwnd = NULL;
964 ms_className = NULL;
965
966 return TRUE;
967 }
968
969 void wxTLWHiddenParentModule::OnExit()
970 {
971 if ( ms_hwnd )
972 {
973 if ( !::DestroyWindow(ms_hwnd) )
974 {
975 wxLogLastError(_T("DestroyWindow(hidden TLW parent)"));
976 }
977
978 ms_hwnd = NULL;
979 }
980
981 if ( ms_className )
982 {
983 if ( !::UnregisterClass(ms_className, wxGetInstance()) )
984 {
985 wxLogLastError(_T("UnregisterClass(\"wxTLWHiddenParent\")"));
986 }
987
988 ms_className = NULL;
989 }
990 }
991
992 /* static */
993 HWND wxTLWHiddenParentModule::GetHWND()
994 {
995 if ( !ms_hwnd )
996 {
997 if ( !ms_className )
998 {
999 static const wxChar *HIDDEN_PARENT_CLASS = _T("wxTLWHiddenParent");
1000
1001 WNDCLASS wndclass;
1002 wxZeroMemory(wndclass);
1003
1004 wndclass.lpfnWndProc = DefWindowProc;
1005 wndclass.hInstance = wxGetInstance();
1006 wndclass.lpszClassName = HIDDEN_PARENT_CLASS;
1007
1008 if ( !::RegisterClass(&wndclass) )
1009 {
1010 wxLogLastError(_T("RegisterClass(\"wxTLWHiddenParent\")"));
1011 }
1012 else
1013 {
1014 ms_className = HIDDEN_PARENT_CLASS;
1015 }
1016 }
1017
1018 ms_hwnd = ::CreateWindow(ms_className, wxEmptyString, 0, 0, 0, 0, 0, NULL,
1019 (HMENU)NULL, wxGetInstance(), NULL);
1020 if ( !ms_hwnd )
1021 {
1022 wxLogLastError(_T("CreateWindow(hidden TLW parent)"));
1023 }
1024 }
1025
1026 return ms_hwnd;
1027 }
1028
1029