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