1 /////////////////////////////////////////////////////////////////////////////// 
   2 // Name:        msw/toplevel.cpp 
   3 // Purpose:     implements wxTopLevelWindow for MSW 
   4 // Author:      Vadim Zeitlin 
   8 // Copyright:   (c) 2001 SciTech Software, Inc. (www.scitechsoft.com) 
   9 // License:     wxWindows license 
  10 /////////////////////////////////////////////////////////////////////////////// 
  12 // ============================================================================ 
  14 // ============================================================================ 
  16 // ---------------------------------------------------------------------------- 
  18 // ---------------------------------------------------------------------------- 
  21     #pragma implementation "toplevel.h" 
  24 // For compilers that support precompilation, includes "wx.h". 
  25 #include "wx/wxprec.h" 
  33     #include "wx/toplevel.h" 
  34     #include "wx/string.h" 
  38     #include "wx/control.h" 
  41 #include "wx/os2/private.h" 
  43 // ---------------------------------------------------------------------------- 
  44 // stubs for missing functions under MicroWindows 
  45 // ---------------------------------------------------------------------------- 
  48 // ---------------------------------------------------------------------------- 
  50 // ---------------------------------------------------------------------------- 
  52 // list of all frames and modeless dialogs 
  53 wxWindowList wxModelessWindows
; 
  55 // the name of the default wxWindows class 
  56 extern void          wxAssociateWinWithHandle( HWND         hWnd
 
  59 bool                 wxTopLevelWindowOS2::m_sbInitialized 
= FALSE
; 
  61 // ============================================================================ 
  62 // wxTopLevelWindowMSW implementation 
  63 // ============================================================================ 
  66 MRESULT EXPENTRY 
wxDlgProc( HWND 
WXUNUSED(hWnd
) 
  68                            ,MPARAM 
WXUNUSED(wParam
) 
  69                            ,MPARAM 
WXUNUSED(lParam
) 
  72     if (uMessage 
== WM_INITDLG
) 
  75         // For this message, returning TRUE tells system to set focus to the 
  76         // first control in the dialog box. 
  83         // For all the other ones, FALSE means that we didn't process the 
  86         return (MRESULT
)FALSE
; 
  90 // ---------------------------------------------------------------------------- 
  91 // wxTopLevelWindowOS2 creation 
  92 // ---------------------------------------------------------------------------- 
  94 void wxTopLevelWindowOS2::Init() 
  96     m_bIconized 
= m_bMaximizeOnShow 
= FALSE
; 
  99     // Unlike (almost?) all other windows, frames are created hidden 
 104     // Data to save/restore when calling ShowFullScreen 
 106     m_lFsOldWindowStyle 
= 0; 
 107     m_bFsIsMaximized    
= FALSE
; 
 108     m_bFsIsShowing      
= FALSE
; 
 110     m_hFrame    
= NULLHANDLE
; 
 111     memset(&m_vSwp
, 0, sizeof(SWP
)); 
 112     memset(&m_vSwpClient
, 0, sizeof(SWP
)); 
 113 } // end of wxTopLevelWindowIOS2::Init 
 115 long wxTopLevelWindowOS2::OS2GetCreateWindowFlags( 
 119     long                            lStyle 
= GetWindowStyle(); 
 122     if (lStyle 
== wxDEFAULT_FRAME_STYLE
) 
 123         lMsflags 
= FCF_SIZEBORDER 
| FCF_TITLEBAR 
| FCF_SYSMENU 
| 
 124                         FCF_MINMAX 
| FCF_TASKLIST
; 
 127         if ((lStyle 
& wxCAPTION
) == wxCAPTION
) 
 128             lMsflags 
= FCF_TASKLIST
; 
 130             lMsflags 
= FCF_NOMOVEWITHOWNER
; 
 132         if ((lStyle 
& wxVSCROLL
) == wxVSCROLL
) 
 133             lMsflags 
|= FCF_VERTSCROLL
; 
 134         if ((lStyle 
& wxHSCROLL
) == wxHSCROLL
) 
 135             lMsflags 
|= FCF_HORZSCROLL
; 
 136         if (lStyle 
& wxMINIMIZE_BOX
) 
 137             lMsflags 
|= FCF_MINBUTTON
; 
 138         if (lStyle 
& wxMAXIMIZE_BOX
) 
 139             lMsflags 
|= FCF_MAXBUTTON
; 
 140         if (lStyle 
& wxTHICK_FRAME
) 
 141             lMsflags 
|= FCF_DLGBORDER
; 
 142         if (lStyle 
& wxSYSTEM_MENU
) 
 143             lMsflags 
|= FCF_SYSMENU
; 
 144         if (lStyle 
& wxCAPTION
) 
 145             lMsflags 
|= FCF_TASKLIST
; 
 146         if (lStyle 
& wxCLIP_CHILDREN
) 
 148             // Invalid for frame windows under PM 
 151         if (lStyle 
& wxTINY_CAPTION_VERT
) 
 152             lMsflags 
|= FCF_TASKLIST
; 
 153         if (lStyle 
& wxTINY_CAPTION_HORIZ
) 
 154             lMsflags 
|= FCF_TASKLIST
; 
 156         if ((lStyle 
& wxTHICK_FRAME
) == 0) 
 157             lMsflags 
|= FCF_BORDER
; 
 158         if (lStyle 
& wxFRAME_TOOL_WINDOW
) 
 159             *plExflags 
= kFrameToolWindow
; 
 161         if (lStyle 
& wxSTAY_ON_TOP
) 
 162             lMsflags 
|= FCF_SYSMODAL
; 
 165 } // end of wxTopLevelWindowOS2::OS2GetCreateWindowFlags 
 167 bool wxTopLevelWindowOS2::CreateDialog( 
 169 , const wxString
&                   rsTitle
 
 170 , const wxPoint
&                    rPos
 
 171 , const wxSize
&                     rSize
 
 174     wxWindow
*                       pParent 
= GetParent(); 
 177     // For the dialogs without wxDIALOG_NO_PARENT style, use the top level 
 178     // app window as parent - this avoids creating modal dialogs without 
 181     if (!pParent 
&& !(GetWindowStyleFlag() & wxDIALOG_NO_PARENT
)) 
 183         pParent 
= wxTheApp
->GetTopWindow(); 
 188             // Don't use transient windows as parents, this is dangerous as it 
 189             // can lead to a crash if the parent is destroyed before the child 
 191             // also don't use the window which is currently hidden as then the 
 192             // dialog would be hidden as well 
 193             if ((pParent
->GetExtraStyle() & wxWS_EX_TRANSIENT
) || 
 205         hWndOwner 
= GetHwndOf(pParent
); 
 207         hWndOwner 
= HWND_DESKTOP
; 
 209     hWndDlg 
= ::WinLoadDlg( HWND_DESKTOP
 
 213                            ,(ULONG
)ulDlgTemplate
 
 217     m_hWnd 
= (WXHWND
) hWndDlg
; 
 221         wxFAIL_MSG(_("Did you forget to include wx/os2/wx.rc in your resources?")); 
 223         wxLogSysError(_("Can't create dialog using template '%ul'"), ulDlgTemplate
); 
 229     // Move the dialog to its initial position without forcing repainting 
 236     if (!OS2GetCreateWindowCoords( rPos
 
 244         nX 
= nWidth 
= (int)CW_USEDEFAULT
; 
 248     // We can't use CW_USEDEFAULT here as we're not calling CreateWindow() 
 249     // and passing CW_USEDEFAULT to MoveWindow() results in resizing the 
 250     // window to (0, 0) size which breaks quite a lot of things, e.g. the 
 251     // sizer calculation in wxSizer::Fit() 
 253     if (nWidth 
== (int)CW_USEDEFAULT
) 
 256         // The exact number doesn't matter, the dialog will be resized 
 257         // again soon anyhow but it should be big enough to allow 
 258         // calculation relying on "totalSize - clientSize > 0" work, i.e. 
 259         // at least greater than the title bar height 
 261         nWidth 
= nHeight 
= 100; 
 263     if (nX 
== (int)CW_USEDEFAULT
) 
 266         // Centre it on the screen - what else can we do? 
 268         wxSize                      vSizeDpy 
= wxGetDisplaySize(); 
 270         nX 
= (vSizeDpy
.x 
- nWidth
) / 2; 
 271         nY 
= (vSizeDpy
.y 
- nHeight
) / 2; 
 273     m_backgroundColour
.Set(wxString("LIGHT GREY")); 
 275     LONG                            lColor 
= (LONG
)m_backgroundColour
.GetPixel(); 
 277     if (!::WinSetPresParam( m_hWnd
 
 286     ::WinSetWindowPos( GetHwnd() 
 292                       ,SWP_MOVE 
| SWP_SIZE 
| SWP_ZORDER 
| SWP_SHOW
 
 294     ::WinQueryWindowPos(GetHwnd(), GetSwp()); 
 298 } // end of wxTopLevelWindowOS2::CreateDialog 
 300 bool wxTopLevelWindowOS2::CreateFrame( 
 301   const wxString
&                   rsTitle
 
 302 , const wxPoint
&                    rPos
 
 303 , const wxSize
&                     rSize
 
 307     long                            lFlags 
= OS2GetCreateWindowFlags(&lExflags
); 
 308     long                            lStyle 
= GetWindowStyleFlag(); 
 311     int                             nWidth 
= rSize
.x
; 
 312     int                             nHeight 
= rSize
.y
; 
 313     ULONG                           ulStyleFlags 
= 0L; 
 316     wxWindow
*                       pParent 
= GetParent(); 
 322         hParent 
= GetHwndOf(pParent
); 
 324         hParent 
= HWND_DESKTOP
; 
 326     if ((lStyle 
& wxMINIMIZE
) || (lStyle 
& wxICONIZE
)) 
 327         ulStyleFlags 
|= WS_MINIMIZED
; 
 328     if (lStyle 
& wxMAXIMIZE
) 
 329         ulStyleFlags 
|= WS_MAXIMIZED
; 
 332     // Clear the visible flag, we always call show 
 334     ulStyleFlags 
&= (unsigned long)~WS_VISIBLE
; 
 338     // Create the frame window:  We break ranks with other ports now 
 339     // and instead of calling down into the base wxWindow class' OS2Create 
 340     // we do all our own stuff here.  We will set the needed pieces 
 341     // of wxWindow manually, here. 
 344      hFrame 
= ::WinCreateStdWindow( hParent
 
 345                                    ,ulStyleFlags          
// frame-window style 
 346                                    ,(PULONG
)&lFlags       
// window style 
 347                                    ,(PSZ
)wxFrameClassName 
// class name 
 348                                    ,(PSZ
)rsTitle
.c_str()  // window title 
 349                                    ,0L                    // default client style 
 350                                    ,NULLHANDLE            
// resource in executable file 
 352                                    ,&hClient              
// receives client window handle 
 356         vError 
= ::WinGetLastError(vHabmain
); 
 357         sError 
= wxPMErrorToStr(vError
); 
 358         wxLogError("Error creating frame. Error: %s\n", sError
); 
 363     // wxWindow class' m_hWnd set here and needed associations 
 367     wxAssociateWinWithHandle(m_hWnd
, this); 
 368     wxAssociateWinWithHandle(m_hFrame
, this); 
 370     m_backgroundColour
.Set(wxString("MEDIUM GREY")); 
 372     LONG                            lColor 
= (LONG
)m_backgroundColour
.GetPixel(); 
 374     if (!::WinSetPresParam( m_hWnd
 
 380         vError 
= ::WinGetLastError(vHabmain
); 
 381         sError 
= wxPMErrorToStr(vError
); 
 382         wxLogError("Error creating frame. Error: %s\n", sError
); 
 387     // Now need to subclass window.  Instead of calling the SubClassWin in wxWindow 
 388     // we manually subclass here because we don't want to use the main wxWndProc 
 391     m_fnOldWndProc 
= (WXFARPROC
) ::WinSubclassWindow(m_hFrame
, (PFNWP
)wxFrameMainWndProc
); 
 394     // Now size everything.  If adding a menu the client will need to be resized. 
 399         nY 
= pParent
->GetSize().y 
- (nY 
+ nHeight
); 
 405         ::WinQueryWindowRect(HWND_DESKTOP
, &vRect
); 
 406         nY 
= vRect
.yTop 
- (nY 
+ nHeight
); 
 408     if (!::WinSetWindowPos( m_hFrame
 
 414                            ,SWP_SIZE 
| SWP_MOVE 
| SWP_ACTIVATE 
| SWP_ZORDER
 
 417         vError 
= ::WinGetLastError(vHabmain
); 
 418         sError 
= wxPMErrorToStr(vError
); 
 419         wxLogError("Error sizing frame. Error: %s\n", sError
); 
 422     lStyle 
=  ::WinQueryWindowULong( m_hWnd
 
 425     lStyle 
|= WS_CLIPCHILDREN
; 
 426     ::WinSetWindowULong( m_hWnd
 
 431 } // end of wxTopLevelWindowOS2::CreateFrame 
 433 bool wxTopLevelWindowOS2::Create( 
 436 , const wxString
&                   rsTitle
 
 437 , const wxPoint
&                    rPos
 
 438 , const wxSize
&                     rSize
 
 440 , const wxString
&                   rsName
 
 447     m_windowStyle 
= lStyle
; 
 449     m_windowId 
= vId 
== -1 ? NewControlId() : vId
; 
 450     wxTopLevelWindows
.Append(this); 
 452         pParent
->AddChild(this); 
 454     if (GetExtraStyle() & wxTOPLEVEL_EX_DIALOG
) 
 457         // We have different dialog templates to allows creation of dialogs 
 458         // with & without captions under OS2indows, resizeable or not (but a 
 459         // resizeable dialog always has caption - otherwise it would look too 
 464         if (lStyle 
& wxRESIZE_BORDER
) 
 465             ulDlgTemplate 
= (ULONG
)kResizeableDialog
; 
 466         else if (lStyle 
& wxCAPTION
) 
 467             ulDlgTemplate 
= (ULONG
)kCaptionDialog
; 
 469             ulDlgTemplate 
= (ULONG
)kNoCaptionDialog
; 
 470         return CreateDialog( ulDlgTemplate
 
 478         return CreateFrame( rsTitle
 
 483 } // end of wxTopLevelWindowOS2::Create 
 485 wxTopLevelWindowOS2::~wxTopLevelWindowOS2() 
 487     wxTopLevelWindows
.DeleteObject(this); 
 489     if (wxModelessWindows
.Find(this)) 
 490         wxModelessWindows
.DeleteObject(this); 
 493     // If this is the last top-level window, exit. 
 495     if (wxTheApp 
&& (wxTopLevelWindows
.Number() == 0)) 
 497         wxTheApp
->SetTopWindow(NULL
); 
 498         if ( wxTheApp
->GetExitOnFrameDelete() ) 
 500             ::WinPostMsg(NULL
, WM_QUIT
, 0, 0); 
 503 } // end of wxTopLevelWindowOS2::~wxTopLevelWindowOS2 
 505 // ---------------------------------------------------------------------------- 
 506 // wxTopLevelWindowOS2 client size 
 507 // ---------------------------------------------------------------------------- 
 509 void wxTopLevelWindowOS2::DoSetClientSize( 
 515     // Call GetClientAreaOrigin() to take the toolbar into account 
 517     wxPoint                         vPt 
= GetClientAreaOrigin(); 
 522     wxWindow::DoSetClientSize( nWidth
 
 525 } // end of wxTopLevelWindowOS2::DoSetClientSize 
 527 void wxTopLevelWindowOS2::DoGetClientSize( 
 532     wxWindow::DoGetClientSize( pnX
 
 536     wxPoint                         vPt 
= GetClientAreaOrigin(); 
 543 } // end of wxTopLevelWindowOS2::DoGetClientSize 
 545 // ---------------------------------------------------------------------------- 
 546 // wxTopLevelWindowOS2 showing 
 547 // ---------------------------------------------------------------------------- 
 549 void wxTopLevelWindowOS2::DoShowWindow( 
 553     ::WinShowWindow(m_hFrame
, (BOOL
)(nShowCmd 
& SWP_SHOW
)); 
 556     // Need to artificially send a size event as wxApps often expect to do some 
 557     // final child control sizing 
 559     m_bIconized 
= nShowCmd 
== SWP_MINIMIZE
; 
 560 } // end of wxTopLevelWindowOS2::DoShowWindow 
 562 bool wxTopLevelWindowOS2::Show( 
 572         if (m_bMaximizeOnShow
) 
 574             nShowCmd 
= SWP_MAXIMIZE
; 
 575             m_bMaximizeOnShow 
= FALSE
; 
 586     DoShowWindow(nShowCmd
); 
 590         wxActivateEvent             
vEvent(wxEVT_ACTIVATE
, TRUE
, m_windowId
); 
 592         ::WinQueryWindowPos(m_hFrame
, &vSwp
); 
 593         m_bIconized 
= vSwp
.fl 
& SWP_MINIMIZE
; 
 594         ::WinQueryWindowPos(m_hWnd
, &m_vSwpClient
); 
 595         ::WinSendMsg(m_hFrame
, WM_UPDATEFRAME
, (MPARAM
)~0, 0); 
 596         ::WinQueryWindowPos(m_hWnd
, &vSwp
); 
 597         ::WinEnableWindow(m_hFrame
, TRUE
); 
 600         // Deal with children 
 602         MoveChildren(m_vSwpClient
.cy 
- vSwp
.cy
); 
 603         vEvent
.SetEventObject(this); 
 604         GetEventHandler()->ProcessEvent(vEvent
); 
 609         // Try to highlight the correct window (the parent) 
 613             HWND                    hWndParent 
= GetHwndOf(GetParent()); 
 615             ::WinQueryWindowPos(hWndParent
, &vSwp
); 
 616             m_bIconized 
= vSwp
.fl 
& SWP_MINIMIZE
; 
 618                 ::WinSetWindowPos( hWndParent
 
 624                                   ,SWP_ZORDER 
| SWP_ACTIVATE 
| SWP_SHOW 
| SWP_MOVE
 
 626             ::WinEnableWindow(hWndParent
, TRUE
); 
 630 } // end of wxTopLevelWindowOS2::Show 
 632 // ---------------------------------------------------------------------------- 
 633 // wxTopLevelWindowOS2 maximize/minimize 
 634 // ---------------------------------------------------------------------------- 
 636 void wxTopLevelWindowOS2::Maximize( 
 643         // Just maximize it directly 
 645         DoShowWindow(bMaximize 
? SWP_MAXIMIZE 
: SWP_RESTORE
); 
 650         // We can't maximize the hidden frame because it shows it as well, so 
 651         // just remember that we should do it later in this case 
 653         m_bMaximizeOnShow 
= TRUE
; 
 655 } // end of wxTopLevelWindowOS2::Maximize 
 657 bool wxTopLevelWindowOS2::IsMaximized() const 
 661     ::WinQueryWindowPos(m_hFrame
, (PSWP
)&m_vSwp
); 
 662     return (m_vSwp
.fl 
& SWP_MAXIMIZE
); 
 663 } // end of wxTopLevelWindowOS2::IsMaximized 
 665 void wxTopLevelWindowOS2::Iconize( 
 669     DoShowWindow(bIconize 
? SWP_MINIMIZE 
: SWP_RESTORE
); 
 670 } // end of wxTopLevelWindowOS2::Iconize 
 672 bool wxTopLevelWindowOS2::IsIconized() const 
 674     // also update the current state 
 675     ::WinQueryWindowPos(m_hFrame
, (PSWP
)&m_vSwp
); 
 676     if (m_vSwp
.fl 
& SWP_MINIMIZE
) 
 677         ((wxTopLevelWindow
*)this)->m_bIconized 
= TRUE
; 
 679         ((wxTopLevelWindow
*)this)->m_bIconized 
= FALSE
; 
 681 } // end of wxTopLevelWindowOS2::IsIconized 
 683 void wxTopLevelWindowOS2::Restore() 
 685     DoShowWindow(SWP_RESTORE
); 
 686 } // end of wxTopLevelWindowOS2::Restore 
 688 // generate an artificial resize event 
 689 void wxTopLevelWindowOS2::SendSizeEvent() 
 693         RECTL                       vRect 
= wxGetWindowRect(GetHwnd()); 
 695         (void)::WinPostMsg( m_hFrame
 
 697                            ,MPFROM2SHORT(vRect
.xRight 
- vRect
.xLeft
, vRect
.yTop 
- vRect
.yBottom
) 
 698                            ,MPFROM2SHORT(vRect
.xRight 
- vRect
.xLeft
, vRect
.yTop 
- vRect
.yBottom
) 
 701 } // end of wxTopLevelWindowOS2::SendSizeEvent 
 703 // ---------------------------------------------------------------------------- 
 704 // wxTopLevelWindowOS2 fullscreen 
 705 // ---------------------------------------------------------------------------- 
 707 bool wxTopLevelWindowOS2::ShowFullScreen( 
 717         m_bFsIsShowing 
= TRUE
; 
 721         // Zap the frame borders 
 725         // Save the 'normal' window lStyle 
 727         m_lFsOldWindowStyle 
= ::WinQueryWindowULong( (HWND
)GetHWND() 
 732         // Save the old position, width & height, maximize state 
 734         m_vFsOldSize 
= GetRect(); 
 735         m_bFsIsMaximized 
= IsMaximized(); 
 738         // Decide which window lStyle flags to turn off 
 740         LONG                        lNewStyle 
= m_lFsOldWindowStyle
; 
 743         if (lStyle 
& wxFULLSCREEN_NOBORDER
) 
 744             lOffFlags 
|= FCF_BORDER
; 
 745         if (lStyle 
& wxFULLSCREEN_NOCAPTION
) 
 746             lOffFlags 
|= (FCF_TASKLIST 
| FCF_SYSMENU
); 
 748         lNewStyle 
&= (~lOffFlags
); 
 751         // Change our window style to be compatible with full-screen mode 
 753         ::WinSetWindowULong( (HWND
)GetHWND() 
 759         // Resize to the size of the desktop 
 763         RECTL                       vRect 
= wxGetWindowRect(HWND_DESKTOP
); 
 765         nWidth 
= vRect
.xRight 
- vRect
.xLeft
; 
 766         nHeight 
= vRect
.yTop 
- vRect
.yBottom
; 
 773         // Now flush the window style cache and actually go full-screen 
 775         ::WinSetWindowPos( m_hFrame
 
 784         wxSizeEvent                 
vEvent( wxSize( nWidth
 
 790         GetEventHandler()->ProcessEvent(vEvent
); 
 798         m_bFsIsShowing 
= FALSE
; 
 799         Maximize(m_bFsIsMaximized
); 
 800         ::WinSetWindowULong( (HWND
)GetHWND() 
 804         ::WinSetWindowPos( m_hFrame
 
 814 } // end of wxTopLevelWindowOS2::ShowFullScreen 
 816 // ---------------------------------------------------------------------------- 
 817 // wxTopLevelWindowOS2 misc 
 818 // ---------------------------------------------------------------------------- 
 820 void wxTopLevelWindowOS2::SetIcon( 
 827     wxTopLevelWindowBase::SetIcon(rIcon
); 
 831         ::WinSendMsg( m_hFrame
 
 833                      ,(MPARAM
)((HPOINTER
)m_icon
.GetHICON()) 
 836         ::WinSendMsg( m_hFrame
 
 842 } // end of wxTopLevelWindowOS2::SetIcon 
 844 bool wxTopLevelWindowOS2::EnableCloseButton( 
 849     // Get system (a.k.a. window) menu 
 851     HMENU                           hMenu 
= ::WinWindowFromID(m_hFrame
, FID_SYSMENU
); 
 855         wxLogLastError(_T("GetSystemMenu")); 
 860     // Enabling/disabling the close item from it also automatically 
 861     // disables/enables the close title bar button 
 864         (void)::WinSendMsg( hMenu
 
 866                            ,MPFROM2SHORT(SC_CLOSE
, FALSE
) 
 867                            ,MPFROM2SHORT(MIA_DISABLED
, FALSE
) 
 870         (void)::WinSendMsg( hMenu
 
 872                            ,MPFROM2SHORT(SC_CLOSE
, FALSE
) 
 873                            ,MPFROM2SHORT(MIA_DISABLED
, MIA_DISABLED
) 
 877     // Update appearance immediately 
 879     ::WinSendMsg( m_hFrame
 
 885 } // end of wxTopLevelWindowOS2::EnableCloseButton