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"
37 #include "wx/dialog.h"
38 #include "wx/settings.h"
39 #include "wx/dcclient.h"
42 #include "wx/msw/private.h"
45 #include "wx/statusbr.h"
47 #if wxUSE_NATIVE_STATUSBAR
48 #include "wx/msw/statbr95.h"
50 #endif // wxUSE_STATUSBAR
53 #include "wx/toolbar.h"
54 #endif // wxUSE_TOOLBAR
56 #include "wx/menuitem.h"
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 extern wxWindowList wxModelessWindows
;
64 extern wxList WXDLLEXPORT wxPendingDelete
;
65 extern wxChar wxFrameClassName
[];
66 extern wxMenu
*wxCurrentPopupMenu
;
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 #if !USE_SHARED_LIBRARY
73 BEGIN_EVENT_TABLE(wxFrame
, wxFrameBase
)
74 EVT_ACTIVATE(wxFrame::OnActivate
)
75 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged
)
78 IMPLEMENT_DYNAMIC_CLASS(wxFrame
, wxWindow
)
81 // ============================================================================
83 // ============================================================================
85 // ----------------------------------------------------------------------------
86 // static class members
87 // ----------------------------------------------------------------------------
89 #if wxUSE_NATIVE_STATUSBAR
90 bool wxFrame::m_useNativeStatusBar
= TRUE
;
92 bool wxFrame::m_useNativeStatusBar
= FALSE
;
95 // ----------------------------------------------------------------------------
96 // creation/destruction
97 // ----------------------------------------------------------------------------
108 bool wxFrame::Create(wxWindow
*parent
,
110 const wxString
& title
,
114 const wxString
& name
)
117 m_windowStyle
= style
;
118 m_frameMenuBar
= NULL
;
119 m_frameToolBar
= NULL
;
120 m_frameStatusBar
= NULL
;
122 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
127 m_windowId
= (int)NewControlId();
129 if (parent
) parent
->AddChild(this);
138 // we pass NULL as parent to MSWCreate because frames with parents behave
139 // very strangely under Win95 shell
140 // Alteration by JACS: keep normal Windows behaviour (float on top of parent)
142 if ((m_windowStyle
& wxFRAME_FLOAT_ON_PARENT
) == 0)
146 wxTopLevelWindows
.Append(this);
148 MSWCreate(m_windowId
, parent
, wxFrameClassName
, this, title
,
149 x
, y
, width
, height
, style
);
151 wxModelessWindows
.Append(this);
157 m_isBeingDeleted
= TRUE
;
158 wxTopLevelWindows
.DeleteObject(this);
162 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
164 wxTheApp
->SetTopWindow(NULL
);
166 if (wxTheApp
->GetExitOnFrameDelete())
172 wxModelessWindows
.DeleteObject(this);
174 // For some reason, wxWindows can activate another task altogether
175 // when a frame is destroyed after a modal dialog has been invoked.
176 // Try to bring the parent to the top.
177 // MT:Only do this if this frame is currently the active window, else weird
178 // things start to happen
179 if ( wxGetActiveWindow() == this )
180 if (GetParent() && GetParent()->GetHWND())
181 ::BringWindowToTop((HWND
) GetParent()->GetHWND());
184 // Get size *available for subwindows* i.e. excluding menu bar, toolbar etc.
185 void wxFrame::DoGetClientSize(int *x
, int *y
) const
188 ::GetClientRect(GetHwnd(), &rect
);
191 if ( GetStatusBar() )
193 int statusX
, statusY
;
194 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
195 rect
.bottom
-= statusY
;
197 #endif // wxUSE_STATUSBAR
199 wxPoint
pt(GetClientAreaOrigin());
209 // Set the client size (i.e. leave the calculation of borders etc.
211 void wxFrame::DoSetClientSize(int width
, int height
)
213 HWND hWnd
= GetHwnd();
216 ::GetClientRect(hWnd
, &rect
);
219 GetWindowRect(hWnd
, &rect2
);
221 // Find the difference between the entire window (title bar and all)
222 // and the client area; add this to the new client size to move the
224 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
225 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
228 if ( GetStatusBar() )
230 int statusX
, statusY
;
231 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
232 actual_height
+= statusY
;
234 #endif // wxUSE_STATUSBAR
236 wxPoint
pt(GetClientAreaOrigin());
237 actual_width
+= pt
.y
;
238 actual_height
+= pt
.x
;
241 point
.x
= rect2
.left
;
244 MoveWindow(hWnd
, point
.x
, point
.y
, actual_width
, actual_height
, (BOOL
)TRUE
);
246 wxSizeEvent
event(wxSize(width
, height
), m_windowId
);
247 event
.SetEventObject( this );
248 GetEventHandler()->ProcessEvent(event
);
251 void wxFrame::DoGetSize(int *width
, int *height
) const
254 GetWindowRect(GetHwnd(), &rect
);
255 *width
= rect
.right
- rect
.left
;
256 *height
= rect
.bottom
- rect
.top
;
259 void wxFrame::DoGetPosition(int *x
, int *y
) const
262 GetWindowRect(GetHwnd(), &rect
);
271 // ----------------------------------------------------------------------------
272 // variations around ::ShowWindow()
273 // ----------------------------------------------------------------------------
275 void wxFrame::DoShowWindow(int nShowCmd
)
277 ::ShowWindow(GetHwnd(), nShowCmd
);
279 m_iconized
= nShowCmd
== SW_MINIMIZE
;
282 bool wxFrame::Show(bool show
)
284 DoShowWindow(show
? SW_SHOW
: SW_HIDE
);
288 ::BringWindowToTop(GetHwnd());
290 wxActivateEvent
event(wxEVT_ACTIVATE
, TRUE
, m_windowId
);
291 event
.SetEventObject( this );
292 GetEventHandler()->ProcessEvent(event
);
296 // Try to highlight the correct window (the parent)
299 HWND hWndParent
= GetHwndOf(GetParent());
301 ::BringWindowToTop(hWndParent
);
308 void wxFrame::Iconize(bool iconize
)
310 DoShowWindow(iconize
? SW_MINIMIZE
: SW_RESTORE
);
313 void wxFrame::Maximize(bool maximize
)
315 DoShowWindow(maximize
? SW_MAXIMIZE
: SW_RESTORE
);
318 void wxFrame::Restore()
320 DoShowWindow(SW_RESTORE
);
323 bool wxFrame::IsIconized() const
325 ((wxFrame
*)this)->m_iconized
= (::IsIconic(GetHwnd()) != 0);
330 bool wxFrame::IsMaximized() const
332 return (::IsZoomed(GetHwnd()) != 0);
335 void wxFrame::SetIcon(const wxIcon
& icon
)
337 wxFrameBase::SetIcon(icon
);
339 #if defined(__WIN95__)
342 SendMessage(GetHwnd(), WM_SETICON
,
343 (WPARAM
)TRUE
, (LPARAM
)(HICON
) m_icon
.GetHICON());
349 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
,
352 const wxString
& name
)
354 wxStatusBar
*statusBar
= NULL
;
356 #if wxUSE_NATIVE_STATUSBAR
357 if ( UsesNativeStatusBar() )
359 statusBar
= new wxStatusBar95(this, id
, style
);
361 statusBar
->SetFieldsCount(number
);
366 statusBar
= wxFrameBase::OnCreateStatusBar(number
, style
, id
, name
);
372 void wxFrame::PositionStatusBar()
374 // native status bar positions itself
375 if ( m_frameStatusBar
376 #if wxUSE_NATIVE_STATUSBAR
377 && !m_frameStatusBar
->IsKindOf(CLASSINFO(wxStatusBar95
))
382 GetClientSize(&w
, &h
);
384 m_frameStatusBar
->GetSize(&sw
, &sh
);
386 // Since we wish the status bar to be directly under the client area,
387 // we use the adjusted sizes without using wxSIZE_NO_ADJUSTMENTS.
388 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
391 #endif // wxUSE_STATUSBAR
393 void wxFrame::DetachMenuBar()
397 m_frameMenuBar
->Detach();
398 m_frameMenuBar
= NULL
;
402 void wxFrame::SetMenuBar(wxMenuBar
*menu_bar
)
410 m_frameMenuBar
= NULL
;
412 // Can set a menubar several times.
413 // TODO: how to prevent a memory leak if you have a currently-unattached
414 // menubar? wxWindows assumes that the frame will delete the menu (otherwise
415 // there are problems for MDI).
416 if (menu_bar
->GetHMenu())
418 m_hMenu
= menu_bar
->GetHMenu();
424 m_hMenu
= menu_bar
->Create();
430 InternalSetMenuBar();
432 m_frameMenuBar
= menu_bar
;
433 menu_bar
->Attach(this);
435 #if 0 // Old code that assumes only one call of SetMenuBar per frame.
442 wxCHECK_RET( !menu_bar
->GetFrame(), wxT("this menubar is already attached") );
445 delete m_frameMenuBar
;
447 m_hMenu
= menu_bar
->Create();
452 InternalSetMenuBar();
454 m_frameMenuBar
= menu_bar
;
455 menu_bar
->Attach(this);
459 void wxFrame::InternalSetMenuBar()
461 if ( !::SetMenu(GetHwnd(), (HMENU
)m_hMenu
) )
463 wxLogLastError("SetMenu");
467 // Responds to colour changes, and passes event on to children.
468 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
470 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
473 if ( m_frameStatusBar
)
475 wxSysColourChangedEvent event2
;
476 event2
.SetEventObject( m_frameStatusBar
);
477 m_frameStatusBar
->GetEventHandler()->ProcessEvent(event2
);
480 // Propagate the event to the non-top-level children
481 wxWindow::OnSysColourChanged(event
);
489 bool wxFrame::MSWCreate(int id
, wxWindow
*parent
, const wxChar
*wclass
, wxWindow
*wx_win
, const wxChar
*title
,
490 int x
, int y
, int width
, int height
, long style
)
493 m_defaultIcon
= (WXHICON
) (wxSTD_FRAME_ICON
? wxSTD_FRAME_ICON
: wxDEFAULT_FRAME_ICON
);
495 // If child windows aren't properly drawn initially, WS_CLIPCHILDREN
496 // could be the culprit. But without it, you can get a lot of flicker.
499 if ((style
& wxCAPTION
) == wxCAPTION
)
500 msflags
= WS_OVERLAPPED
;
504 if (style
& wxMINIMIZE_BOX
)
505 msflags
|= WS_MINIMIZEBOX
;
506 if (style
& wxMAXIMIZE_BOX
)
507 msflags
|= WS_MAXIMIZEBOX
;
508 if (style
& wxTHICK_FRAME
)
509 msflags
|= WS_THICKFRAME
;
510 if (style
& wxSYSTEM_MENU
)
511 msflags
|= WS_SYSMENU
;
512 if ((style
& wxMINIMIZE
) || (style
& wxICONIZE
))
513 msflags
|= WS_MINIMIZE
;
514 if (style
& wxMAXIMIZE
)
515 msflags
|= WS_MAXIMIZE
;
516 if (style
& wxCAPTION
)
517 msflags
|= WS_CAPTION
;
518 if (style
& wxCLIP_CHILDREN
)
519 msflags
|= WS_CLIPCHILDREN
;
521 // Keep this in wxFrame because it saves recoding this function
524 if (style
& wxTINY_CAPTION_VERT
)
525 msflags
|= IBS_VERTCAPTION
;
526 if (style
& wxTINY_CAPTION_HORIZ
)
527 msflags
|= IBS_HORZCAPTION
;
529 if (style
& wxTINY_CAPTION_VERT
)
530 msflags
|= WS_CAPTION
;
531 if (style
& wxTINY_CAPTION_HORIZ
)
532 msflags
|= WS_CAPTION
;
534 if ((style
& wxTHICK_FRAME
) == 0)
535 msflags
|= WS_BORDER
;
537 WXDWORD extendedStyle
= MakeExtendedStyle(style
);
539 #if !defined(__WIN16__) && !defined(__SC__)
540 if (style
& wxFRAME_TOOL_WINDOW
)
541 extendedStyle
|= WS_EX_TOOLWINDOW
;
544 if (style
& wxSTAY_ON_TOP
)
545 extendedStyle
|= WS_EX_TOPMOST
;
548 if ( !wxWindow::MSWCreate(id
, parent
, wclass
, wx_win
, title
, x
, y
, width
, height
,
549 msflags
, NULL
, extendedStyle
) )
552 // Seems to be necessary if we use WS_POPUP
553 // style instead of WS_OVERLAPPED
554 if (width
> -1 && height
> -1)
555 ::PostMessage(GetHwnd(), WM_SIZE
, SIZE_RESTORED
, MAKELPARAM(width
, height
));
560 // Default activation behaviour - set the focus for the first child
562 void wxFrame::OnActivate(wxActivateEvent
& event
)
564 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
566 node
= node
->GetNext() )
568 // FIXME all this is totally bogus - we need to do the same as wxPanel,
569 // but how to do it without duplicating the code?
572 wxWindow
*child
= node
->GetData();
574 if ( !child
->IsTopLevel()
576 && !wxDynamicCast(child
, wxToolBar
)
577 #endif // wxUSE_TOOLBAR
579 && !wxDynamicCast(child
, wxStatusBar
)
580 #endif // wxUSE_STATUSBAR
589 // ----------------------------------------------------------------------------
590 // tool/status bar stuff
591 // ----------------------------------------------------------------------------
595 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
597 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
602 return m_frameToolBar
;
605 void wxFrame::PositionToolBar()
608 ::GetClientRect(GetHwnd(), &rect
);
611 if ( GetStatusBar() )
613 int statusX
, statusY
;
614 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
615 rect
.bottom
-= statusY
;
617 #endif // wxUSE_STATUSBAR
622 GetToolBar()->GetSize(&tw
, &th
);
624 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
633 // Use the 'real' MSW position here
634 GetToolBar()->SetSize(0, 0, tw
, th
, wxSIZE_NO_ADJUSTMENTS
);
637 #endif // wxUSE_TOOLBAR
639 // ----------------------------------------------------------------------------
640 // frame state (iconized/maximized/...)
641 // ----------------------------------------------------------------------------
643 // propagate our state change to all child frames: this allows us to emulate X
644 // Windows behaviour where child frames float independently of the parent one
645 // on the desktop, but are iconized/restored with it
646 void wxFrame::IconizeChildFrames(bool bIconize
)
648 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
650 node
= node
->GetNext() )
652 wxWindow
*win
= node
->GetData();
654 if ( win
->IsKindOf(CLASSINFO(wxFrame
)) )
656 ((wxFrame
*)win
)->Iconize(bIconize
);
661 // ===========================================================================
662 // message processing
663 // ===========================================================================
665 // ---------------------------------------------------------------------------
667 // ---------------------------------------------------------------------------
669 bool wxFrame::MSWTranslateMessage(WXMSG
* pMsg
)
671 if ( wxWindow::MSWTranslateMessage(pMsg
) )
674 // try the menu bar accels
675 wxMenuBar
*menuBar
= GetMenuBar();
679 const wxAcceleratorTable
& acceleratorTable
= menuBar
->GetAccelTable();
680 return acceleratorTable
.Translate(this, pMsg
);
683 // ---------------------------------------------------------------------------
684 // our private (non virtual) message handlers
685 // ---------------------------------------------------------------------------
687 bool wxFrame::HandlePaint()
690 if ( GetUpdateRect(GetHwnd(), &rect
, FALSE
) )
694 HICON hIcon
= m_icon
.Ok() ? GetHiconOf(m_icon
)
695 : (HICON
)m_defaultIcon
;
697 // Hold a pointer to the dc so long as the OnPaint() message
698 // is being processed
700 HDC hdc
= ::BeginPaint(GetHwnd(), &ps
);
702 // Erase background before painting or we get white background
703 MSWDefWindowProc(WM_ICONERASEBKGND
, (WORD
)(LONG
)ps
.hdc
, 0L);
708 ::GetClientRect(GetHwnd(), &rect
);
710 // FIXME: why hardcoded?
711 static const int icon_width
= 32;
712 static const int icon_height
= 32;
714 int icon_x
= (int)((rect
.right
- icon_width
)/2);
715 int icon_y
= (int)((rect
.bottom
- icon_height
)/2);
717 ::DrawIcon(hdc
, icon_x
, icon_y
, hIcon
);
720 ::EndPaint(GetHwnd(), &ps
);
726 return wxWindow::HandlePaint();
731 // nothing to paint - processed
736 bool wxFrame::HandleSize(int x
, int y
, WXUINT id
)
738 bool processed
= FALSE
;
743 // only do it it if we were iconized before, otherwise resizing the
744 // parent frame has a curious side effect of bringing it under it's
749 // restore all child frames too
750 IconizeChildFrames(FALSE
);
759 // iconize all child frames too
760 IconizeChildFrames(TRUE
);
768 // forward WM_SIZE to status bar control
769 #if wxUSE_NATIVE_STATUSBAR
770 if (m_frameStatusBar
&& m_frameStatusBar
->IsKindOf(CLASSINFO(wxStatusBar95
)))
772 wxSizeEvent
event(wxSize(x
, y
), m_frameStatusBar
->GetId());
773 event
.SetEventObject( m_frameStatusBar
);
775 ((wxStatusBar95
*)m_frameStatusBar
)->OnSize(event
);
777 #endif // wxUSE_NATIVE_STATUSBAR
782 wxSizeEvent
event(wxSize(x
, y
), m_windowId
);
783 event
.SetEventObject( this );
784 processed
= GetEventHandler()->ProcessEvent(event
);
790 bool wxFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
)
794 // In case it's e.g. a toolbar.
795 wxWindow
*win
= wxFindWinFromHandle(control
);
797 return win
->MSWCommand(cmd
, id
);
800 // handle here commands from menus and accelerators
801 if ( cmd
== 0 || cmd
== 1 )
803 if ( wxCurrentPopupMenu
)
805 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
806 wxCurrentPopupMenu
= NULL
;
808 return popupMenu
->MSWCommand(cmd
, id
);
811 if ( ProcessCommand(id
) )
820 bool wxFrame::HandleMenuSelect(WXWORD nItem
, WXWORD flags
, WXHMENU hMenu
)
823 if ( flags
== 0xFFFF && hMenu
== 0 )
825 // menu was removed from screen
828 else if ( !(flags
& MF_POPUP
) && !(flags
& MF_SEPARATOR
) )
834 // don't give hints for separators (doesn't make sense) nor for the
835 // items opening popup menus (they don't have them anyhow)
839 wxMenuEvent
event(wxEVT_MENU_HIGHLIGHT
, item
);
840 event
.SetEventObject( this );
842 return GetEventHandler()->ProcessEvent(event
);
845 // ---------------------------------------------------------------------------
846 // the window proc for wxFrame
847 // ---------------------------------------------------------------------------
849 long wxFrame::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
852 bool processed
= FALSE
;
857 // if we can't close, tell the system that we processed the
858 // message - otherwise it would close us
859 processed
= !Close();
866 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
869 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
877 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
879 processed
= HandleMenuSelect(item
, flags
, hmenu
);
884 processed
= HandlePaint();
887 case WM_QUERYDRAGICON
:
889 HICON hIcon
= m_icon
.Ok() ? GetHiconOf(m_icon
)
890 : (HICON
)(m_defaultIcon
);
897 processed
= HandleSize(LOWORD(lParam
), HIWORD(lParam
), wParam
);
902 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);