1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "frame.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
29 #include "wx/dialog.h"
30 #include "wx/settings.h"
31 #include "wx/dcclient.h"
34 #include "wx/msw/private.h"
35 #include "wx/statusbr.h"
36 #include "wx/menuitem.h"
38 #ifdef LoadAccelerators
39 #undef LoadAccelerators
42 #if USE_NATIVE_STATUSBAR
43 #include <wx/msw/statbr95.h>
46 extern wxList wxModelessWindows
;
47 extern wxList wxPendingDelete
;
48 extern char wxFrameClassName
[];
50 #if !USE_SHARED_LIBRARY
51 BEGIN_EVENT_TABLE(wxFrame
, wxWindow
)
52 EVT_SIZE(wxFrame::OnSize
)
53 EVT_ACTIVATE(wxFrame::OnActivate
)
54 EVT_MENU_HIGHLIGHT_ALL(wxFrame::OnMenuHighlight
)
55 EVT_SYS_COLOUR_CHANGED(wxFrame::OnSysColourChanged
)
56 EVT_IDLE(wxFrame::OnIdle
)
57 EVT_CLOSE(wxFrame::OnCloseWindow
)
60 IMPLEMENT_DYNAMIC_CLASS(wxFrame
, wxWindow
)
63 #if USE_NATIVE_STATUSBAR
64 bool wxFrame::m_useNativeStatusBar
= TRUE
;
66 bool wxFrame::m_useNativeStatusBar
= FALSE
;
69 wxFrame::wxFrame(void)
71 m_frameMenuBar
= NULL
;
72 m_frameStatusBar
= NULL
;
74 m_windowParent
= NULL
;
78 bool wxFrame::Create(wxWindow
*parent
,
80 const wxString
& title
,
87 wxTopLevelWindows
.Append(this);
90 // m_modalShowing = FALSE;
91 m_windowStyle
= style
;
92 m_frameMenuBar
= NULL
;
93 m_frameStatusBar
= NULL
;
95 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
101 m_windowId
= (int)NewControlId();
103 if (parent
) parent
->AddChild(this);
111 MSWCreate(m_windowId
, (wxWindow
*)parent
, wxFrameClassName
, this, (char *)(const char *)title
,
112 x
, y
, width
, height
, style
);
114 wxModelessWindows
.Append(this);
118 wxFrame::~wxFrame(void)
120 m_isBeingDeleted
= TRUE
;
121 wxTopLevelWindows
.DeleteObject(this);
123 if (m_frameStatusBar
)
124 delete m_frameStatusBar
;
126 delete m_frameMenuBar
;
128 /* New behaviour March 1998: check if it's the last top-level window */
129 // if (wxTheApp && (this == wxTheApp->GetTopWindow()))
131 if (wxTheApp
&& (wxTopLevelWindows
.Number() == 0))
133 wxTheApp
->SetTopWindow(NULL
);
135 if (wxTheApp
->GetExitOnFrameDelete())
141 wxModelessWindows
.DeleteObject(this);
143 // For some reason, wxWindows can activate another task altogether
144 // when a frame is destroyed after a modal dialog has been invoked.
145 // Try to bring the parent to the top.
146 if (GetParent() && GetParent()->GetHWND())
147 ::BringWindowToTop((HWND
) GetParent()->GetHWND());
150 WXHMENU
wxFrame::GetWinMenu(void) const
155 // Get size *available for subwindows* i.e. excluding menu bar etc.
156 // For XView, this is the same as GetSize
157 void wxFrame::GetClientSize(int *x
, int *y
) const
160 GetClientRect((HWND
) GetHWND(), &rect
);
162 if ( m_frameStatusBar
)
164 int statusX
, statusY
;
165 m_frameStatusBar
->GetClientSize(&statusX
, &statusY
);
166 rect
.bottom
-= statusY
;
172 // Set the client size (i.e. leave the calculation of borders etc.
174 void wxFrame::SetClientSize(int width
, int height
)
176 HWND hWnd
= (HWND
) GetHWND();
179 GetClientRect(hWnd
, &rect
);
182 GetWindowRect(hWnd
, &rect2
);
184 // Find the difference between the entire window (title bar and all)
185 // and the client area; add this to the new client size to move the
187 int actual_width
= rect2
.right
- rect2
.left
- rect
.right
+ width
;
188 int actual_height
= rect2
.bottom
- rect2
.top
- rect
.bottom
+ height
;
190 if ( m_frameStatusBar
)
192 int statusX
, statusY
;
193 m_frameStatusBar
->GetClientSize(&statusX
, &statusY
);
194 actual_height
+= statusY
;
198 point
.x
= rect2
.left
;
201 MoveWindow(hWnd
, point
.x
, point
.y
, actual_width
, actual_height
, (BOOL
)TRUE
);
203 wxSizeEvent
event(wxSize(width
, height
), m_windowId
);
204 event
.SetEventObject( this );
205 GetEventHandler()->ProcessEvent(event
);
208 void wxFrame::GetSize(int *width
, int *height
) const
211 GetWindowRect((HWND
) GetHWND(), &rect
);
212 *width
= rect
.right
- rect
.left
;
213 *height
= rect
.bottom
- rect
.top
;
216 void wxFrame::GetPosition(int *x
, int *y
) const
219 GetWindowRect((HWND
) GetHWND(), &rect
);
228 void wxFrame::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
230 int currentX
, currentY
;
236 GetPosition(¤tX
, ¤tY
);
237 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
239 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
244 if (width
== -1) w1
= ww
;
245 if (height
==-1) h1
= hh
;
247 MoveWindow((HWND
) GetHWND(), x1
, y1
, w1
, h1
, (BOOL
)TRUE
);
249 wxSizeEvent
event(wxSize(width
, height
), m_windowId
);
250 event
.SetEventObject( this );
251 GetEventHandler()->ProcessEvent(event
);
254 bool wxFrame::Show(bool show
)
264 // Try to highlight the correct window (the parent)
268 hWndParent
= (HWND
) GetParent()->GetHWND();
270 ::BringWindowToTop(hWndParent
);
274 ShowWindow((HWND
) GetHWND(), (BOOL
)cshow
);
277 BringWindowToTop((HWND
) GetHWND());
279 wxActivateEvent
event(wxEVT_ACTIVATE
, TRUE
, m_windowId
);
280 event
.SetEventObject( this );
281 GetEventHandler()->ProcessEvent(event
);
286 void wxFrame::Iconize(bool iconize
)
296 ShowWindow((HWND
) GetHWND(), (BOOL
)cshow
);
297 m_iconized
= iconize
;
300 // Equivalent to maximize/restore in Windows
301 void wxFrame::Maximize(bool maximize
)
309 ShowWindow((HWND
) GetHWND(), cshow
);
313 bool wxFrame::IsIconized(void) const
315 ((wxFrame
*)this)->m_iconized
= (::IsIconic((HWND
) GetHWND()) != 0);
319 void wxFrame::SetTitle(const wxString
& title
)
321 SetWindowText((HWND
) GetHWND(), (const char *)title
);
324 wxString
wxFrame::GetTitle(void) const
326 GetWindowText((HWND
) GetHWND(), wxBuffer
, 1000);
327 return wxString(wxBuffer
);
330 void wxFrame::SetIcon(const wxIcon
& icon
)
333 #if defined(__WIN95__)
335 SendMessage((HWND
) GetHWND(), WM_SETICON
,
336 (WPARAM
)TRUE
, (LPARAM
)(HICON
) m_icon
.GetHICON());
340 wxStatusBar
*wxFrame::OnCreateStatusBar(int number
)
342 wxStatusBar
*statusBar
= NULL
;
344 #if USE_NATIVE_STATUSBAR
345 if (UsesNativeStatusBar())
347 statusBar
= new wxStatusBar95(this);
352 statusBar
= new wxStatusBar(this, -1, wxPoint(0, 0), wxSize(100, 20));
354 // Set the height according to the font and the border size
355 wxClientDC
dc(statusBar
);
356 dc
.SetFont(* statusBar
->GetFont());
359 dc
.GetTextExtent("X", &x
, &y
);
361 int height
= (int)( (y
* 1.1) + 2* statusBar
->GetBorderY());
363 statusBar
->SetSize(-1, -1, 100, height
);
366 statusBar
->SetFieldsCount(number
);
370 bool wxFrame::CreateStatusBar(int number
)
372 // VZ: calling CreateStatusBar twice is an error - why anyone would do it?
373 wxCHECK_MSG( m_frameStatusBar
== NULL
, FALSE
,
374 "recreating status bar in wxFrame" );
376 m_frameStatusBar
= OnCreateStatusBar(number
);
377 if ( m_frameStatusBar
)
386 void wxFrame::SetStatusText(const wxString
& text
, int number
)
388 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set text for" );
390 m_frameStatusBar
->SetStatusText(text
, number
);
393 void wxFrame::SetStatusWidths(int n
, const int widths_field
[])
395 wxCHECK_RET( m_frameStatusBar
!= NULL
, "no statusbar to set widths for" );
397 m_frameStatusBar
->SetStatusWidths(n
, widths_field
);
401 void wxFrame::PositionStatusBar(void)
403 // native status bar positions itself
405 #if USE_NATIVE_STATUSBAR
406 && !m_frameStatusBar
->IsKindOf(CLASSINFO(wxStatusBar95
))
411 GetClientSize(&w
, &h
);
413 m_frameStatusBar
->GetSize(&sw
, &sh
);
414 m_frameStatusBar
->SetSize(0, h
, w
, sh
);
418 void wxFrame::SetMenuBar(wxMenuBar
*menu_bar
)
422 m_frameMenuBar
= NULL
;
426 if (menu_bar
->m_menuBarFrame
)
430 HMENU menu
= CreateMenu();
432 for (i
= 0; i
< menu_bar
->m_menuCount
; i
++)
434 HMENU popup
= (HMENU
)menu_bar
->m_menus
[i
]->m_hMenu
;
436 // After looking Bounds Checker result, it seems that all
437 // menus must be individually destroyed. So, don't reset m_hMenu,
438 // to allow ~wxMenu to do the job.
440 menu_bar
->m_menus
[i
]->m_savehMenu
= (WXHMENU
) popup
;
441 // Uncommenting for the moment... JACS
442 menu_bar
->m_menus
[i
]->m_hMenu
= 0;
443 AppendMenu(menu
, MF_POPUP
| MF_STRING
, (UINT
)popup
, menu_bar
->m_titles
[i
]);
446 menu_bar
->m_hMenu
= (WXHMENU
)menu
;
448 delete m_frameMenuBar
;
450 this->m_hMenu
= (WXHMENU
) menu
;
453 if (!SetMenu((HWND
) GetHWND(), menu
))
456 err
= GetLastError();
460 m_frameMenuBar
= menu_bar
;
461 menu_bar
->m_menuBarFrame
= this;
464 bool wxFrame::LoadAccelerators(const wxString
& table
)
466 m_acceleratorTable
= (WXHANDLE
)
469 ::LoadAcceleratorsW(wxGetInstance(), (const char *)table
);
471 ::LoadAcceleratorsA(wxGetInstance(), (const char *)table
);
474 ::LoadAccelerators(wxGetInstance(), (const char *)table
);
477 // The above is necessary because LoadAccelerators is a macro
478 // which we have undefed earlier in the file to avoid confusion
479 // with wxFrame::LoadAccelerators. Ugh!
481 return (m_acceleratorTable
!= (WXHANDLE
) NULL
);
484 void wxFrame::Fit(void)
486 // Work out max. size
487 wxNode
*node
= GetChildren()->First();
492 // Find a child that's a subwindow, but not a dialog box.
493 wxWindow
*win
= (wxWindow
*)node
->Data();
495 if (!win
->IsKindOf(CLASSINFO(wxFrame
)) &&
496 !win
->IsKindOf(CLASSINFO(wxDialog
)))
500 win
->GetSize(&width
, &height
);
501 win
->GetPosition(&x
, &y
);
503 if ((x
+ width
) > max_width
)
504 max_width
= x
+ width
;
505 if ((y
+ height
) > max_height
)
506 max_height
= y
+ height
;
510 SetClientSize(max_width
, max_height
);
513 // Responds to colour changes, and passes event on to children.
514 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent
& event
)
516 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE
));
519 if ( m_frameStatusBar
)
521 wxSysColourChangedEvent event2
;
522 event2
.SetEventObject( m_frameStatusBar
);
523 m_frameStatusBar
->ProcessEvent(event2
);
526 // Propagate the event to the non-top-level children
527 wxWindow::OnSysColourChanged(event
);
535 void wxFrame::MSWCreate(int id
, wxWindow
*parent
, const char *wclass
, wxWindow
*wx_win
, const char *title
,
536 int x
, int y
, int width
, int height
, long style
)
539 m_defaultIcon
= (WXHICON
) (wxSTD_FRAME_ICON
? wxSTD_FRAME_ICON
: wxDEFAULT_FRAME_ICON
);
541 // If child windows aren't properly drawn initially, WS_CLIPCHILDREN
542 // could be the culprit. But without it, you can get a lot of flicker.
545 if ((style
& wxCAPTION
) == wxCAPTION
)
546 msflags
= WS_OVERLAPPED
;
550 if (style
& wxMINIMIZE_BOX
)
551 msflags
|= WS_MINIMIZEBOX
;
552 if (style
& wxMAXIMIZE_BOX
)
553 msflags
|= WS_MAXIMIZEBOX
;
554 if (style
& wxTHICK_FRAME
)
555 msflags
|= WS_THICKFRAME
;
556 if (style
& wxSYSTEM_MENU
)
557 msflags
|= WS_SYSMENU
;
558 if ((style
& wxMINIMIZE
) || (style
& wxICONIZE
))
559 msflags
|= WS_MINIMIZE
;
560 if (style
& wxMAXIMIZE
)
561 msflags
|= WS_MAXIMIZE
;
562 if (style
& wxCAPTION
)
563 msflags
|= WS_CAPTION
;
564 if (style
& wxCLIP_CHILDREN
)
565 msflags
|= WS_CLIPCHILDREN
;
567 // Keep this in wxFrame because it saves recoding this function
570 if (style
& wxTINY_CAPTION_VERT
)
571 msflags
|= IBS_VERTCAPTION
;
572 if (style
& wxTINY_CAPTION_HORIZ
)
573 msflags
|= IBS_HORZCAPTION
;
575 if (style
& wxTINY_CAPTION_VERT
)
576 msflags
|= WS_CAPTION
;
577 if (style
& wxTINY_CAPTION_HORIZ
)
578 msflags
|= WS_CAPTION
;
580 if ((style
& wxTHICK_FRAME
) == 0)
581 msflags
|= WS_BORDER
;
583 WXDWORD extendedStyle
= MakeExtendedStyle(style
);
585 if (style
& wxSTAY_ON_TOP
)
586 extendedStyle
|= WS_EX_TOPMOST
;
589 wxWindow::MSWCreate(id
, parent
, wclass
, wx_win
, title
, x
, y
, width
, height
,
590 msflags
, NULL
, extendedStyle
);
591 // Seems to be necessary if we use WS_POPUP
592 // style instead of WS_OVERLAPPED
593 if (width
> -1 && height
> -1)
594 ::PostMessage((HWND
) GetHWND(), WM_SIZE
, SIZE_RESTORED
, MAKELPARAM(width
, height
));
597 bool wxFrame::MSWOnPaint(void)
600 wxDebugMsg("wxFrameWnd::OnPaint %d\n", handle
);
603 if (GetUpdateRect((HWND
) GetHWND(), &rect
, FALSE
))
609 the_icon
= (HICON
) m_icon
.GetHICON();
611 the_icon
= (HICON
) m_defaultIcon
;
614 // Hold a pointer to the dc so long as the OnPaint() message
615 // is being processed
616 HDC cdc
= BeginPaint((HWND
) GetHWND(), &ps
);
618 // Erase background before painting or we get white background
619 this->MSWDefWindowProc(WM_ICONERASEBKGND
,(WORD
)ps
.hdc
,0L);
624 GetClientRect((HWND
) GetHWND(), &rect
);
626 int icon_height
= 32;
627 int icon_x
= (int)((rect
.right
- icon_width
)/2);
628 int icon_y
= (int)((rect
.bottom
- icon_height
)/2);
629 DrawIcon(cdc
, icon_x
, icon_y
, the_icon
);
632 EndPaint((HWND
) GetHWND(), &ps
);
636 wxPaintEvent
event(m_windowId
);
637 event
.m_eventObject
= this;
638 if (!GetEventHandler()->ProcessEvent(event
))
646 WXHICON
wxFrame::MSWOnQueryDragIcon(void)
648 if (m_icon
.Ok() && (m_icon
.GetHICON() != 0))
649 return m_icon
.GetHICON();
651 return m_defaultIcon
;
654 void wxFrame::MSWOnSize(int x
, int y
, WXUINT id
)
657 wxDebugMsg("wxFrameWnd::OnSize %d\n", m_hWnd
);
672 // forward WM_SIZE to status bar control
673 #if USE_NATIVE_STATUSBAR
674 if (m_frameStatusBar
&& m_frameStatusBar
->IsKindOf(CLASSINFO(wxStatusBar95
)))
676 wxSizeEvent
event(wxSize(x
, y
), m_frameStatusBar
->GetId());
677 event
.SetEventObject( m_frameStatusBar
);
679 ((wxStatusBar95
*)m_frameStatusBar
)->OnSize(event
);
684 wxSizeEvent
event(wxSize(x
, y
), m_windowId
);
685 event
.SetEventObject( this );
686 if (!GetEventHandler()->ProcessEvent(event
))
691 bool wxFrame::MSWOnClose(void)
694 wxDebugMsg("wxFrameWnd::OnClose %d\n", handle
);
699 bool wxFrame::MSWOnCommand(WXWORD id
, WXWORD cmd
, WXHWND control
)
702 wxDebugMsg("wxFrameWnd::OnCommand %d\n", handle
);
704 if (cmd
== 0 || cmd
== 1 ) // Can be either a menu command or an accelerator.
706 // In case it's e.g. a toolbar.
707 wxWindow
*win
= wxFindWinFromHandle(control
);
709 return win
->MSWCommand(cmd
, id
);
711 if (GetMenuBar() && GetMenuBar()->FindItemForId(id
))
723 void wxFrame::MSWOnMenuHighlight(WXWORD nItem
, WXWORD nFlags
, WXHMENU hSysMenu
)
725 if (nFlags
== 0xFFFF && hSysMenu
== 0)
727 wxMenuEvent
event(wxEVT_MENU_HIGHLIGHT
, -1);
728 event
.SetEventObject( this );
729 GetEventHandler()->ProcessEvent(event
);
731 else if (nFlags
!= MF_SEPARATOR
)
733 wxMenuEvent
event(wxEVT_MENU_HIGHLIGHT
, nItem
);
734 event
.SetEventObject( this );
735 GetEventHandler()->ProcessEvent(event
);
739 bool wxFrame::MSWProcessMessage(WXMSG
* pMsg
)
741 if (m_acceleratorTable
!= 0 &&
742 ::TranslateAccelerator((HWND
) GetHWND(), (HANDLE
) m_acceleratorTable
, (MSG
*)pMsg
))
748 // Default resizing behaviour - if only ONE subwindow,
749 // resize to client rectangle size
750 void wxFrame::OnSize(wxSizeEvent
& event
)
752 // Search for a child which is a subwindow, not another frame.
753 wxWindow
*child
= NULL
;
754 // Count the number of _subwindow_ children
756 for(wxNode
*node
= GetChildren()->First(); node
; node
= node
->Next())
758 wxWindow
*win
= (wxWindow
*)node
->Data();
759 if (!win
->IsKindOf(CLASSINFO(wxFrame
)) && !win
->IsKindOf(CLASSINFO(wxDialog
))
760 && (win
!= GetStatusBar()))
767 // If not one child, call the Layout function if compiled in
768 if (!child
|| (noChildren
> 1)
783 int client_x
, client_y
;
786 wxDebugMsg("wxFrame::OnSize: about to set the child's size.\n");
789 GetClientSize(&client_x
, &client_y
);
790 child
->SetSize(0, 0, client_x
, client_y
);
795 // Default activation behaviour - set the focus for the first child
797 void wxFrame::OnActivate(wxActivateEvent
& event
)
799 for(wxNode
*node
= GetChildren()->First(); node
; node
= node
->Next())
801 // Find a child that's a subwindow, but not a dialog box.
802 wxWindow
*child
= (wxWindow
*)node
->Data();
803 if (!child
->IsKindOf(CLASSINFO(wxFrame
)) &&
804 !child
->IsKindOf(CLASSINFO(wxDialog
)))
807 wxDebugMsg("wxFrame::OnActivate: about to set the child's focus.\n");
815 // The default implementation for the close window event - calls
816 // OnClose for backward compatibility.
818 void wxFrame::OnCloseWindow(wxCloseEvent
& event
)
821 if ( GetEventHandler()->OnClose() || event
.GetForce())
827 // Destroy the window (delayed, if a managed window)
828 bool wxFrame::Destroy(void)
830 if (!wxPendingDelete
.Member(this))
831 wxPendingDelete
.Append(this);
835 // Default menu selection behaviour - display a help string
836 void wxFrame::OnMenuHighlight(wxMenuEvent
& event
)
840 if (event
.GetMenuId() == -1)
844 wxMenuBar
*menuBar
= GetMenuBar();
847 wxString
helpString(menuBar
->GetHelpString(event
.GetMenuId()));
848 if (helpString
!= "")
849 SetStatusText(helpString
);
855 wxMenuBar
*wxFrame::GetMenuBar(void) const
857 return m_frameMenuBar
;
860 void wxFrame::Centre(int direction
)
862 int display_width
, display_height
, width
, height
, x
, y
;
863 wxDisplaySize(&display_width
, &display_height
);
865 GetSize(&width
, &height
);
868 if (direction
& wxHORIZONTAL
)
869 x
= (int)((display_width
- width
)/2);
870 if (direction
& wxVERTICAL
)
871 y
= (int)((display_height
- height
)/2);
873 SetSize(x
, y
, width
, height
);
876 // Call this to simulate a menu command
877 void wxFrame::Command(int id
)
882 void wxFrame::ProcessCommand(int id
)
884 wxCommandEvent
commandEvent(wxEVENT_TYPE_MENU_COMMAND
, id
);
885 commandEvent
.SetInt( id
);
886 commandEvent
.SetEventObject( this );
888 wxMenuBar
*bar
= GetMenuBar() ;
892 wxMenuItem
*item
= bar
->FindItemForId(id
) ;
893 if (item
&& item
->IsCheckable())
895 bar
->Check(id
,!bar
->Checked(id
)) ;
897 GetEventHandler()->ProcessEvent(commandEvent
);
900 void wxFrame::OnIdle(wxIdleEvent
& event
)
905 // Query app for menu item updates (called from OnIdle)
906 void wxFrame::DoMenuUpdates(void)
908 wxMenuBar
* bar
= GetMenuBar();
913 for (i
= 0; i
< bar
->m_menuCount
; i
++)
915 wxMenu
* menu
= bar
->m_menus
[i
];
921 void wxFrame::DoMenuUpdates(wxMenu
* menu
)
923 wxNode
* node
= menu
->m_menuItems
.First();
926 wxMenuItem
* item
= (wxMenuItem
*) node
->Data();
927 if ( !item
->IsSeparator() )
929 wxWindowID id
= item
->GetId();
930 wxUpdateUIEvent
event(id
);
931 event
.SetEventObject( this );
933 if (GetEventHandler()->ProcessEvent(event
))
935 if (event
.GetSetText())
936 menu
->SetLabel(id
, event
.GetText());
937 if (event
.GetSetChecked())
938 menu
->Check(id
, event
.GetChecked());
939 if (event
.GetSetEnabled())
940 menu
->Enable(id
, event
.GetEnabled());
943 if (item
->GetSubMenu())
944 DoMenuUpdates(item
->GetSubMenu());