1 ///////////////////////////////////////////////////////////////////////////// 
   4 // Author:      Julian Smart 
   8 // Copyright:   (c) Julian Smart and Markus Holzem 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // ============================================================================ 
  14 // ============================================================================ 
  16 // ---------------------------------------------------------------------------- 
  18 // ---------------------------------------------------------------------------- 
  21     #pragma implementation "frame.h" 
  24 // For compilers that support precompilation, includes "wx.h". 
  25 #include "wx/wxprec.h" 
  36     #include "wx/dialog.h" 
  37     #include "wx/settings.h" 
  38     #include "wx/dcclient.h" 
  43 #include "wx/msw/private.h" 
  46     #include "wx/statusbr.h" 
  47     #include "wx/generic/statusbr.h" 
  48 #endif // wxUSE_STATUSBAR 
  51     #include "wx/toolbar.h" 
  52 #endif // wxUSE_TOOLBAR 
  54 #include "wx/menuitem.h" 
  57 #ifdef __WXUNIVERSAL__ 
  58     #include "wx/univ/theme.h" 
  59     #include "wx/univ/colschem.h" 
  60 #endif // __WXUNIVERSAL__ 
  62 // ---------------------------------------------------------------------------- 
  64 // ---------------------------------------------------------------------------- 
  66 #if wxUSE_MENUS_NATIVE 
  67     extern wxMenu 
*wxCurrentPopupMenu
; 
  68 #endif // wxUSE_MENUS_NATIVE 
  70 // ---------------------------------------------------------------------------- 
  72 // ---------------------------------------------------------------------------- 
  74 BEGIN_EVENT_TABLE(wxFrame
, wxFrameBase
) 
  75     EVT_ACTIVATE(wxFrame::OnActivate
) 
  76     EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged
) 
  79 IMPLEMENT_DYNAMIC_CLASS(wxFrame
, wxTopLevelWindow
) 
  81 // ============================================================================ 
  83 // ============================================================================ 
  85 // ---------------------------------------------------------------------------- 
  86 // static class members 
  87 // ---------------------------------------------------------------------------- 
  90     #if wxUSE_NATIVE_STATUSBAR 
  91         bool wxFrame::m_useNativeStatusBar 
= TRUE
; 
  93         bool wxFrame::m_useNativeStatusBar 
= FALSE
; 
  95 #endif // wxUSE_NATIVE_STATUSBAR 
  97 // ---------------------------------------------------------------------------- 
  98 // creation/destruction 
  99 // ---------------------------------------------------------------------------- 
 107     // Data to save/restore when calling ShowFullScreen 
 108     m_fsStatusBarFields 
= 0; 
 109     m_fsStatusBarHeight 
= 0; 
 110     m_fsToolBarHeight 
= 0; 
 113     m_wasMinimized 
= FALSE
; 
 115     m_winLastFocused 
= (wxWindow 
*)NULL
; 
 118 bool wxFrame::Create(wxWindow 
*parent
, 
 120                      const wxString
& title
, 
 124                      const wxString
& name
) 
 126     if ( !wxTopLevelWindow::Create(parent
, id
, title
, pos
, size
, style
, name
) ) 
 129     SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
)); 
 131     wxModelessWindows
.Append(this); 
 138     m_isBeingDeleted 
= TRUE
; 
 143 // ---------------------------------------------------------------------------- 
 144 // wxFrame client size calculations 
 145 // ---------------------------------------------------------------------------- 
 147 void wxFrame::DoSetClientSize(int width
, int height
) 
 149     // leave enough space for the status bar if we have (and show) it 
 151     wxStatusBar 
*statbar 
= GetStatusBar(); 
 152     if ( statbar 
&& statbar
->IsShown() ) 
 154         height 
+= statbar
->GetSize().y
; 
 156 #endif // wxUSE_STATUSBAR 
 158     // call GetClientAreaOrigin() to take the toolbar into account 
 159     wxPoint pt 
= GetClientAreaOrigin(); 
 163     wxTopLevelWindow::DoSetClientSize(width
, height
); 
 166 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc. 
 167 void wxFrame::DoGetClientSize(int *x
, int *y
) const 
 169     wxTopLevelWindow::DoGetClientSize(x
, y
); 
 171     // account for the possible toolbar 
 172     wxPoint pt 
= GetClientAreaOrigin(); 
 180     // adjust client area height to take the status bar into account 
 183         wxStatusBar 
*statbar 
= GetStatusBar(); 
 184         if ( statbar 
&& statbar
->IsShown() ) 
 186             *y 
-= statbar
->GetClientSize().y
; 
 189 #endif // wxUSE_STATUSBAR 
 192 // ---------------------------------------------------------------------------- 
 193 // wxFrame: various geometry-related functions 
 194 // ---------------------------------------------------------------------------- 
 196 void wxFrame::Raise() 
 199     // no SetForegroundWindow() in Win16 
 200     wxFrameBase::Raise(); 
 202     ::SetForegroundWindow(GetHwnd()); 
 206 // generate an artificial resize event 
 207 void wxFrame::SendSizeEvent() 
 211         RECT r 
= wxGetWindowRect(GetHwnd()); 
 213         (void)::PostMessage(GetHwnd(), WM_SIZE
, 
 214                             IsMaximized() ? SIZE_MAXIMIZED 
: SIZE_RESTORED
, 
 215                             MAKELPARAM(r
.right 
- r
.left
, r
.bottom 
- r
.top
)); 
 220 wxStatusBar 
*wxFrame::OnCreateStatusBar(int number
, 
 223                                         const wxString
& name
) 
 225     wxStatusBar 
*statusBar 
= NULL
; 
 227 #if wxUSE_NATIVE_STATUSBAR 
 228     if ( !UsesNativeStatusBar() ) 
 230         statusBar 
= (wxStatusBar 
*)new wxStatusBarGeneric(this, id
, style
); 
 235         statusBar 
= new wxStatusBar(this, id
, style
, name
); 
 238     statusBar
->SetFieldsCount(number
); 
 243 void wxFrame::PositionStatusBar() 
 245     if ( !m_frameStatusBar 
|| !m_frameStatusBar
->IsShown() ) 
 249     GetClientSize(&w
, &h
); 
 251     m_frameStatusBar
->GetSize(&sw
, &sh
); 
 253     // Since we wish the status bar to be directly under the client area, 
 254     // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS. 
 255     m_frameStatusBar
->SetSize(0, h
, w
, sh
); 
 257 #endif // wxUSE_STATUSBAR 
 259 #if wxUSE_MENUS_NATIVE 
 261 void wxFrame::AttachMenuBar(wxMenuBar 
*menubar
) 
 263     wxFrameBase::AttachMenuBar(menubar
); 
 267         // actually remove the menu from the frame 
 268         m_hMenu 
= (WXHMENU
)0; 
 269         InternalSetMenuBar(); 
 271     else // set new non NULL menu bar 
 273         // Can set a menubar several times. 
 274         if ( menubar
->GetHMenu() ) 
 276             m_hMenu 
= menubar
->GetHMenu(); 
 280             m_hMenu 
= menubar
->Create(); 
 284                 wxFAIL_MSG( _T("failed to create menu bar") ); 
 289         InternalSetMenuBar(); 
 293 void wxFrame::InternalSetMenuBar() 
 295 #ifndef __WXMICROWIN__ 
 296     if ( !::SetMenu(GetHwnd(), (HMENU
)m_hMenu
) ) 
 298         wxLogLastError(wxT("SetMenu")); 
 303 #endif // wxUSE_MENUS_NATIVE 
 305 // Responds to colour changes, and passes event on to children. 
 306 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
) 
 308     SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE
)); 
 312     if ( m_frameStatusBar 
) 
 314         wxSysColourChangedEvent event2
; 
 315         event2
.SetEventObject( m_frameStatusBar 
); 
 316         m_frameStatusBar
->GetEventHandler()->ProcessEvent(event2
); 
 318 #endif // wxUSE_STATUSBAR 
 320     // Propagate the event to the non-top-level children 
 321     wxWindow::OnSysColourChanged(event
); 
 324 // Pass TRUE to show full screen, FALSE to restore. 
 325 bool wxFrame::ShowFullScreen(bool show
, long style
) 
 327     if ( IsFullScreen() == show 
)  
 333         wxToolBar 
*theToolBar 
= GetToolBar(); 
 335             theToolBar
->GetSize(NULL
, &m_fsToolBarHeight
); 
 337         // zap the toolbar, menubar, and statusbar 
 339         if ((style 
& wxFULLSCREEN_NOTOOLBAR
) && theToolBar
) 
 341             theToolBar
->SetSize(-1,0); 
 342             theToolBar
->Show(FALSE
); 
 344 #endif // wxUSE_TOOLBAR 
 346 #ifndef __WXMICROWIN__ 
 347         if (style 
& wxFULLSCREEN_NOMENUBAR
) 
 348             SetMenu((HWND
)GetHWND(), (HMENU
) NULL
); 
 352         wxStatusBar 
*theStatusBar 
= GetStatusBar(); 
 354             theStatusBar
->GetSize(NULL
, &m_fsStatusBarHeight
); 
 356         // Save the number of fields in the statusbar 
 357         if ((style 
& wxFULLSCREEN_NOSTATUSBAR
) && theStatusBar
) 
 359             //m_fsStatusBarFields = theStatusBar->GetFieldsCount(); 
 360             //SetStatusBar((wxStatusBar*) NULL); 
 361             //delete theStatusBar; 
 362             theStatusBar
->Show(FALSE
); 
 365             m_fsStatusBarFields 
= 0; 
 366 #endif // wxUSE_STATUSBAR 
 371         wxToolBar 
*theToolBar 
= GetToolBar(); 
 373         // restore the toolbar, menubar, and statusbar 
 374         if (theToolBar 
&& (m_fsStyle 
& wxFULLSCREEN_NOTOOLBAR
)) 
 376             theToolBar
->SetSize(-1, m_fsToolBarHeight
); 
 377             theToolBar
->Show(TRUE
); 
 379 #endif // wxUSE_TOOLBAR 
 382         if ( m_fsStyle 
& wxFULLSCREEN_NOSTATUSBAR 
) 
 384             //CreateStatusBar(m_fsStatusBarFields); 
 387                 GetStatusBar()->Show(TRUE
); 
 391 #endif // wxUSE_STATUSBAR 
 393 #ifndef __WXMICROWIN__ 
 394         if ((m_fsStyle 
& wxFULLSCREEN_NOMENUBAR
) && (m_hMenu 
!= 0)) 
 395             SetMenu((HWND
)GetHWND(), (HMENU
)m_hMenu
); 
 399     return wxFrameBase::ShowFullScreen(show
, style
); 
 402 // Default activation behaviour - set the focus for the first child 
 404 void wxFrame::OnActivate(wxActivateEvent
& event
) 
 406     if ( event
.GetActive() ) 
 408         // restore focus to the child which was last focused 
 409         wxLogTrace(_T("focus"), _T("wxFrame %08x activated."), m_hWnd
); 
 411         wxWindow 
*parent 
= m_winLastFocused 
? m_winLastFocused
->GetParent() 
 418         wxSetFocusToChild(parent
, &m_winLastFocused
); 
 422         // remember the last focused child if it is our child 
 423         m_winLastFocused 
= FindFocus(); 
 425         // so we NULL it out if it's a child from some other frame 
 426         wxWindow 
*win 
= m_winLastFocused
; 
 429             if ( win
->IsTopLevel() ) 
 433                     m_winLastFocused 
= NULL
; 
 439             win 
= win
->GetParent(); 
 442         wxLogTrace(_T("focus"), 
 443                    _T("wxFrame %08x deactivated, last focused: %08x."), 
 445                    m_winLastFocused 
? GetHwndOf(m_winLastFocused
) 
 452 // ---------------------------------------------------------------------------- 
 453 // tool/status bar stuff 
 454 // ---------------------------------------------------------------------------- 
 458 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
) 
 460     if ( wxFrameBase::CreateToolBar(style
, id
, name
) ) 
 465     return m_frameToolBar
; 
 468 void wxFrame::PositionToolBar() 
 470     wxToolBar 
*toolbar 
= GetToolBar(); 
 471     if ( toolbar 
&& toolbar
->IsShown() ) 
 473         // don't call our (or even wxTopLevelWindow) version because we want 
 474         // the real (full) client area size, not excluding the tool/status bar 
 476         wxWindow::DoGetClientSize(&width
, &height
); 
 479         wxStatusBar 
*statbar 
= GetStatusBar(); 
 480         if ( statbar 
&& statbar
->IsShown() ) 
 482             height 
-= statbar
->GetClientSize().y
; 
 484 #endif // wxUSE_STATUSBAR 
 487         toolbar
->GetSize(&tw
, &th
); 
 489         if ( toolbar
->GetWindowStyleFlag() & wxTB_VERTICAL 
) 
 496             if ( toolbar
->GetWindowStyleFlag() & wxTB_FLAT 
) 
 500         // use the 'real' MSW position here, don't offset relativly to the 
 501         // client area origin 
 502         toolbar
->SetSize(0, 0, tw
, th
, wxSIZE_NO_ADJUSTMENTS
); 
 506 #endif // wxUSE_TOOLBAR 
 508 // ---------------------------------------------------------------------------- 
 509 // frame state (iconized/maximized/...) 
 510 // ---------------------------------------------------------------------------- 
 512 // propagate our state change to all child frames: this allows us to emulate X 
 513 // Windows behaviour where child frames float independently of the parent one 
 514 // on the desktop, but are iconized/restored with it 
 515 void wxFrame::IconizeChildFrames(bool bIconize
) 
 517     for ( wxWindowList::Node 
*node 
= GetChildren().GetFirst(); 
 519           node 
= node
->GetNext() ) 
 521         wxWindow 
*win 
= node
->GetData(); 
 523         // iconizing the frames with this style under Win95 shell puts them at 
 524         // the bottom of the screen (as the MDI children) instead of making 
 525         // them appear in the taskbar because they are, by virtue of this 
 526         // style, not managed by the taskbar - instead leave Windows take care 
 529         if ( win
->GetWindowStyle() & wxFRAME_TOOL_WINDOW 
) 
 533         // the child MDI frames are a special case and should not be touched by 
 534         // the parent frame - instead, they are managed by the user 
 535         wxFrame 
*frame 
= wxDynamicCast(win
, wxFrame
); 
 537 #if wxUSE_MDI_ARCHITECTURE 
 538                 && !wxDynamicCast(frame
, wxMDIChildFrame
) 
 539 #endif // wxUSE_MDI_ARCHITECTURE 
 542             // we don't want to restore the child frames which had been 
 543             // iconized even before we were iconized, so save the child frame 
 544             // status when iconizing the parent frame and check it when 
 548                 // note that we shouldn't touch the hidden frames neither 
 549                 // because iconizing/restoring them would show them as a side 
 551                 frame
->m_wasMinimized 
= frame
->IsIconized() || !frame
->IsShown(); 
 554             // this test works for both iconizing and restoring 
 555             if ( !frame
->m_wasMinimized 
) 
 556                 frame
->Iconize(bIconize
); 
 561 WXHICON 
wxFrame::GetDefaultIcon() const 
 563     return (WXHICON
)(wxSTD_FRAME_ICON 
? wxSTD_FRAME_ICON
 
 564                                       : wxDEFAULT_FRAME_ICON
); 
 567 // =========================================================================== 
 568 // message processing 
 569 // =========================================================================== 
 571 // --------------------------------------------------------------------------- 
 573 // --------------------------------------------------------------------------- 
 575 bool wxFrame::MSWTranslateMessage(WXMSG
* pMsg
) 
 577     if ( wxWindow::MSWTranslateMessage(pMsg
) ) 
 580 #if wxUSE_MENUS && wxUSE_ACCEL && !defined(__WXUNIVERSAL__) 
 581     // try the menu bar accels 
 582     wxMenuBar 
*menuBar 
= GetMenuBar(); 
 586     const wxAcceleratorTable
& acceleratorTable 
= menuBar
->GetAccelTable(); 
 587     return acceleratorTable
.Translate(this, pMsg
); 
 590 #endif // wxUSE_MENUS && wxUSE_ACCEL 
 593 // --------------------------------------------------------------------------- 
 594 // our private (non virtual) message handlers 
 595 // --------------------------------------------------------------------------- 
 597 bool wxFrame::HandlePaint() 
 600     if ( GetUpdateRect(GetHwnd(), &rect
, FALSE
) ) 
 602 #ifndef __WXMICROWIN__ 
 605             const wxIcon
& icon 
= GetIcon(); 
 606             HICON hIcon 
= icon
.Ok() ? GetHiconOf(icon
) 
 607                                     : (HICON
)GetDefaultIcon(); 
 609             // Hold a pointer to the dc so long as the OnPaint() message 
 610             // is being processed 
 612             HDC hdc 
= ::BeginPaint(GetHwnd(), &ps
); 
 614             // Erase background before painting or we get white background 
 615             MSWDefWindowProc(WM_ICONERASEBKGND
, (WORD
)(LONG
)ps
.hdc
, 0L); 
 620                 ::GetClientRect(GetHwnd(), &rect
); 
 622                 // FIXME: why hardcoded? 
 623                 static const int icon_width 
= 32; 
 624                 static const int icon_height 
= 32; 
 626                 int icon_x 
= (int)((rect
.right 
- icon_width
)/2); 
 627                 int icon_y 
= (int)((rect
.bottom 
- icon_height
)/2); 
 629                 ::DrawIcon(hdc
, icon_x
, icon_y
, hIcon
); 
 632             ::EndPaint(GetHwnd(), &ps
); 
 639             return wxWindow::HandlePaint(); 
 644         // nothing to paint - processed 
 649 bool wxFrame::HandleSize(int x
, int y
, WXUINT id
) 
 651     bool processed 
= FALSE
; 
 652 #ifndef __WXMICROWIN__ 
 657             // only do it it if we were iconized before, otherwise resizing the 
 658             // parent frame has a curious side effect of bringing it under it's 
 663             // restore all child frames too 
 664             IconizeChildFrames(FALSE
); 
 666             (void)SendIconizeEvent(FALSE
); 
 675             // iconize all child frames too 
 676             IconizeChildFrames(TRUE
); 
 678             (void)SendIconizeEvent(); 
 689 #endif // wxUSE_STATUSBAR 
 693 #endif // wxUSE_TOOLBAR 
 695         processed 
= wxWindow::HandleSize(x
, y
, id
); 
 701 bool wxFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
) 
 705         // In case it's e.g. a toolbar. 
 706         wxWindow 
*win 
= wxFindWinFromHandle(control
); 
 708             return win
->MSWCommand(cmd
, id
); 
 711     // handle here commands from menus and accelerators 
 712     if ( cmd 
== 0 || cmd 
== 1 ) 
 714 #if wxUSE_MENUS_NATIVE 
 715         if ( wxCurrentPopupMenu 
) 
 717             wxMenu 
*popupMenu 
= wxCurrentPopupMenu
; 
 718             wxCurrentPopupMenu 
= NULL
; 
 720             return popupMenu
->MSWCommand(cmd
, id
); 
 722 #endif // wxUSE_MENUS_NATIVE 
 724         if ( ProcessCommand(id
) ) 
 733 bool wxFrame::HandleMenuSelect(WXWORD nItem
, WXWORD flags
, WXHMENU hMenu
) 
 736     if ( flags 
== 0xFFFF && hMenu 
== 0 ) 
 738         // menu was removed from screen 
 741 #ifndef __WXMICROWIN__ 
 742     else if ( !(flags 
& MF_POPUP
) && !(flags 
& MF_SEPARATOR
) ) 
 750         // don't give hints for separators (doesn't make sense) nor for the 
 751         // items opening popup menus (they don't have them anyhow) but do clear 
 752         // the status line - otherwise, we would be left with the help message 
 753         // for the previous item which doesn't apply any more 
 754         wxStatusBar 
*statbar 
= GetStatusBar(); 
 757             statbar
->SetStatusText(wxEmptyString
); 
 759 #endif // wxUSE_STATUSBAR 
 764     wxMenuEvent 
event(wxEVT_MENU_HIGHLIGHT
, item
); 
 765     event
.SetEventObject(this); 
 767     return GetEventHandler()->ProcessEvent(event
); 
 770 bool wxFrame::HandleMenuLoop(const wxEventType
& evtType
, WXWORD isPopup
) 
 772     // we don't have the menu id here, so we use the id to specify if the event 
 773     // was from a popup menu or a normal one 
 774     wxMenuEvent 
event(evtType
, isPopup 
? -1 : 0); 
 775     event
.SetEventObject(this); 
 777     return GetEventHandler()->ProcessEvent(event
); 
 780 // --------------------------------------------------------------------------- 
 781 // the window proc for wxFrame 
 782 // --------------------------------------------------------------------------- 
 784 long wxFrame::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
) 
 787     bool processed 
= FALSE
; 
 792             // if we can't close, tell the system that we processed the 
 793             // message - otherwise it would close us 
 794             processed 
= !Close(); 
 798             processed 
= HandleSize(LOWORD(lParam
), HIWORD(lParam
), wParam
); 
 805                 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
, 
 808                 processed 
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
); 
 813             processed 
= HandlePaint(); 
 816 #ifndef __WXMICROWIN__ 
 821                 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
); 
 823                 processed 
= HandleMenuSelect(item
, flags
, hmenu
); 
 828         case WM_ENTERMENULOOP
: 
 829             processed 
= HandleMenuLoop(wxEVT_MENU_OPEN
, wParam
); 
 832         case WM_EXITMENULOOP
: 
 833             processed 
= HandleMenuLoop(wxEVT_MENU_CLOSE
, wParam
); 
 837         case WM_QUERYDRAGICON
: 
 839                 const wxIcon
& icon 
= GetIcon(); 
 840                 HICON hIcon 
= icon
.Ok() ? GetHiconOf(icon
) 
 841                                         : (HICON
)GetDefaultIcon(); 
 846 #endif // !__WXMICROWIN__ 
 850         rc 
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);