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