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 // wxFrame size management: we exclude the areas taken by menu/status/toolbars
591 // from the client area, so the client area is what's really available for the
593 // ----------------------------------------------------------------------------
595 // Checks if there is a toolbar, and returns the first free client position
596 wxPoint
wxFrame::GetClientAreaOrigin() const
602 GetToolBar()->GetSize(& w
, & h
);
604 if (GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
616 void wxFrame::DoScreenToClient(int *x
, int *y
) const
618 wxWindow::DoScreenToClient(x
, y
);
620 // We may be faking the client origin.
621 // So a window that's really at (0, 30) may appear
622 // (to wxWin apps) to be at (0, 0).
623 wxPoint
pt(GetClientAreaOrigin());
628 void wxFrame::DoClientToScreen(int *x
, int *y
) const
630 // We may be faking the client origin.
631 // So a window that's really at (0, 30) may appear
632 // (to wxWin apps) to be at (0, 0).
633 wxPoint
pt1(GetClientAreaOrigin());
637 wxWindow::DoClientToScreen(x
, y
);
640 // ----------------------------------------------------------------------------
641 // tool/status bar stuff
642 // ----------------------------------------------------------------------------
646 wxToolBar
* wxFrame::CreateToolBar(long style
, wxWindowID id
, const wxString
& name
)
648 if ( wxFrameBase::CreateToolBar(style
, id
, name
) )
653 return m_frameToolBar
;
656 void wxFrame::PositionToolBar()
659 ::GetClientRect(GetHwnd(), &rect
);
662 if ( GetStatusBar() )
664 int statusX
, statusY
;
665 GetStatusBar()->GetClientSize(&statusX
, &statusY
);
666 rect
.bottom
-= statusY
;
668 #endif // wxUSE_STATUSBAR
673 GetToolBar()->GetSize(&tw
, &th
);
675 if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL
)
684 // Use the 'real' MSW position here
685 GetToolBar()->SetSize(0, 0, tw
, th
, wxSIZE_NO_ADJUSTMENTS
);
688 #endif // wxUSE_TOOLBAR
690 // ----------------------------------------------------------------------------
691 // frame state (iconized/maximized/...)
692 // ----------------------------------------------------------------------------
694 // propagate our state change to all child frames: this allows us to emulate X
695 // Windows behaviour where child frames float independently of the parent one
696 // on the desktop, but are iconized/restored with it
697 void wxFrame::IconizeChildFrames(bool bIconize
)
699 for ( wxWindowList::Node
*node
= GetChildren().GetFirst();
701 node
= node
->GetNext() )
703 wxWindow
*win
= node
->GetData();
705 if ( win
->IsKindOf(CLASSINFO(wxFrame
)) )
707 ((wxFrame
*)win
)->Iconize(bIconize
);
712 // ===========================================================================
713 // message processing
714 // ===========================================================================
716 // ---------------------------------------------------------------------------
718 // ---------------------------------------------------------------------------
720 bool wxFrame::MSWTranslateMessage(WXMSG
* pMsg
)
722 if ( wxWindow::MSWTranslateMessage(pMsg
) )
725 // try the menu bar accels
726 wxMenuBar
*menuBar
= GetMenuBar();
730 const wxAcceleratorTable
& acceleratorTable
= menuBar
->GetAccelTable();
731 return acceleratorTable
.Translate(this, pMsg
);
734 // ---------------------------------------------------------------------------
735 // our private (non virtual) message handlers
736 // ---------------------------------------------------------------------------
738 bool wxFrame::HandlePaint()
741 if ( GetUpdateRect(GetHwnd(), &rect
, FALSE
) )
745 HICON hIcon
= m_icon
.Ok() ? GetHiconOf(m_icon
)
746 : (HICON
)m_defaultIcon
;
748 // Hold a pointer to the dc so long as the OnPaint() message
749 // is being processed
751 HDC hdc
= ::BeginPaint(GetHwnd(), &ps
);
753 // Erase background before painting or we get white background
754 MSWDefWindowProc(WM_ICONERASEBKGND
, (WORD
)(LONG
)ps
.hdc
, 0L);
759 ::GetClientRect(GetHwnd(), &rect
);
761 // FIXME: why hardcoded?
762 static const int icon_width
= 32;
763 static const int icon_height
= 32;
765 int icon_x
= (int)((rect
.right
- icon_width
)/2);
766 int icon_y
= (int)((rect
.bottom
- icon_height
)/2);
768 ::DrawIcon(hdc
, icon_x
, icon_y
, hIcon
);
771 ::EndPaint(GetHwnd(), &ps
);
777 return wxWindow::HandlePaint();
782 // nothing to paint - processed
787 bool wxFrame::HandleSize(int x
, int y
, WXUINT id
)
789 bool processed
= FALSE
;
794 // only do it it if we were iconized before, otherwise resizing the
795 // parent frame has a curious side effect of bringing it under it's
800 // restore all child frames too
801 IconizeChildFrames(FALSE
);
810 // iconize all child frames too
811 IconizeChildFrames(TRUE
);
819 // forward WM_SIZE to status bar control
820 #if wxUSE_NATIVE_STATUSBAR
821 if (m_frameStatusBar
&& m_frameStatusBar
->IsKindOf(CLASSINFO(wxStatusBar95
)))
823 wxSizeEvent
event(wxSize(x
, y
), m_frameStatusBar
->GetId());
824 event
.SetEventObject( m_frameStatusBar
);
826 ((wxStatusBar95
*)m_frameStatusBar
)->OnSize(event
);
828 #endif // wxUSE_NATIVE_STATUSBAR
833 wxSizeEvent
event(wxSize(x
, y
), m_windowId
);
834 event
.SetEventObject( this );
835 processed
= GetEventHandler()->ProcessEvent(event
);
841 bool wxFrame::HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
)
845 // In case it's e.g. a toolbar.
846 wxWindow
*win
= wxFindWinFromHandle(control
);
848 return win
->MSWCommand(cmd
, id
);
851 // handle here commands from menus and accelerators
852 if ( cmd
== 0 || cmd
== 1 )
854 if ( wxCurrentPopupMenu
)
856 wxMenu
*popupMenu
= wxCurrentPopupMenu
;
857 wxCurrentPopupMenu
= NULL
;
859 return popupMenu
->MSWCommand(cmd
, id
);
862 if ( ProcessCommand(id
) )
871 bool wxFrame::HandleMenuSelect(WXWORD nItem
, WXWORD flags
, WXHMENU hMenu
)
874 if ( flags
== 0xFFFF && hMenu
== 0 )
876 // menu was removed from screen
879 else if ( !(flags
& MF_POPUP
) && !(flags
& MF_SEPARATOR
) )
885 // don't give hints for separators (doesn't make sense) nor for the
886 // items opening popup menus (they don't have them anyhow)
890 wxMenuEvent
event(wxEVT_MENU_HIGHLIGHT
, item
);
891 event
.SetEventObject( this );
893 return GetEventHandler()->ProcessEvent(event
);
896 // ---------------------------------------------------------------------------
897 // the window proc for wxFrame
898 // ---------------------------------------------------------------------------
900 long wxFrame::MSWWindowProc(WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
903 bool processed
= FALSE
;
908 // if we can't close, tell the system that we processed the
909 // message - otherwise it would close us
910 processed
= !Close();
917 UnpackCommand((WXWPARAM
)wParam
, (WXLPARAM
)lParam
,
920 processed
= HandleCommand(id
, cmd
, (WXHWND
)hwnd
);
928 UnpackMenuSelect(wParam
, lParam
, &item
, &flags
, &hmenu
);
930 processed
= HandleMenuSelect(item
, flags
, hmenu
);
935 processed
= HandlePaint();
938 case WM_QUERYDRAGICON
:
940 HICON hIcon
= m_icon
.Ok() ? GetHiconOf(m_icon
)
941 : (HICON
)(m_defaultIcon
);
948 processed
= HandleSize(LOWORD(lParam
), HIWORD(lParam
), wParam
);
953 rc
= wxWindow::MSWWindowProc(message
, wParam
, lParam
);