]> git.saurik.com Git - wxWidgets.git/blob - src/msw/frame.cpp
ce0c0b0edf81a7d0c3763be09c067d1d1d54adc4
[wxWidgets.git] / src / msw / frame.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: frame.cpp
3 // Purpose: wxFrame
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "frame.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/setup.h"
25 #include "wx/frame.h"
26 #include "wx/menu.h"
27 #include "wx/app.h"
28 #include "wx/utils.h"
29 #include "wx/dialog.h"
30 #include "wx/settings.h"
31 #include "wx/dcclient.h"
32 #endif
33
34 #include "wx/msw/private.h"
35 #include "wx/statusbr.h"
36 #include "wx/menuitem.h"
37
38 #ifdef LoadAccelerators
39 #undef LoadAccelerators
40 #endif
41
42 #if USE_NATIVE_STATUSBAR
43 #include <wx/msw/statbr95.h>
44 #endif
45
46 extern wxList wxModelessWindows;
47 extern wxList wxPendingDelete;
48 extern char wxFrameClassName[];
49
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)
58 END_EVENT_TABLE()
59
60 IMPLEMENT_DYNAMIC_CLASS(wxFrame, wxWindow)
61 #endif
62
63 #if USE_NATIVE_STATUSBAR
64 bool wxFrame::m_useNativeStatusBar = TRUE;
65 #else
66 bool wxFrame::m_useNativeStatusBar = FALSE;
67 #endif
68
69 wxFrame::wxFrame(void)
70 {
71 m_frameMenuBar = NULL;
72 m_frameStatusBar = NULL;
73
74 m_windowParent = NULL;
75 m_iconized = FALSE;
76 }
77
78 bool wxFrame::Create(wxWindow *parent,
79 const wxWindowID id,
80 const wxString& title,
81 const wxPoint& pos,
82 const wxSize& size,
83 const long style,
84 const wxString& name)
85 {
86 if (!parent)
87 wxTopLevelWindows.Append(this);
88
89 SetName(name);
90 // m_modalShowing = FALSE;
91 m_windowStyle = style;
92 m_frameMenuBar = NULL;
93 m_frameStatusBar = NULL;
94
95 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE));
96
97 // m_icon = NULL;
98 if ( id > -1 )
99 m_windowId = id;
100 else
101 m_windowId = (int)NewControlId();
102
103 if (parent) parent->AddChild(this);
104
105 int x = pos.x;
106 int y = pos.y;
107 int width = size.x;
108 int height = size.y;
109
110 m_iconized = FALSE;
111 MSWCreate(m_windowId, (wxWindow *)parent, wxFrameClassName, this, (char *)(const char *)title,
112 x, y, width, height, style);
113
114 wxModelessWindows.Append(this);
115 return TRUE;
116 }
117
118 wxFrame::~wxFrame(void)
119 {
120 m_isBeingDeleted = TRUE;
121 wxTopLevelWindows.DeleteObject(this);
122
123 if (m_frameStatusBar)
124 delete m_frameStatusBar;
125 if (m_frameMenuBar)
126 delete m_frameMenuBar;
127
128 /* New behaviour March 1998: check if it's the last top-level window */
129 // if (wxTheApp && (this == wxTheApp->GetTopWindow()))
130
131 if (wxTheApp && (wxTopLevelWindows.Number() == 0))
132 {
133 wxTheApp->SetTopWindow(NULL);
134
135 if (wxTheApp->GetExitOnFrameDelete())
136 {
137 PostQuitMessage(0);
138 }
139 }
140
141 wxModelessWindows.DeleteObject(this);
142
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());
148 }
149
150 WXHMENU wxFrame::GetWinMenu(void) const
151 {
152 return m_hMenu;
153 }
154
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
158 {
159 RECT rect;
160 GetClientRect((HWND) GetHWND(), &rect);
161
162 if ( m_frameStatusBar )
163 {
164 int statusX, statusY;
165 m_frameStatusBar->GetClientSize(&statusX, &statusY);
166 rect.bottom -= statusY;
167 }
168 *x = rect.right;
169 *y = rect.bottom;
170 }
171
172 // Set the client size (i.e. leave the calculation of borders etc.
173 // to wxWindows)
174 void wxFrame::SetClientSize(const int width, const int height)
175 {
176 HWND hWnd = (HWND) GetHWND();
177
178 RECT rect;
179 GetClientRect(hWnd, &rect);
180
181 RECT rect2;
182 GetWindowRect(hWnd, &rect2);
183
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
186 // window
187 int actual_width = rect2.right - rect2.left - rect.right + width;
188 int actual_height = rect2.bottom - rect2.top - rect.bottom + height;
189
190 if ( m_frameStatusBar )
191 {
192 int statusX, statusY;
193 m_frameStatusBar->GetClientSize(&statusX, &statusY);
194 actual_height += statusY;
195 }
196
197 POINT point;
198 point.x = rect2.left;
199 point.y = rect2.top;
200
201 MoveWindow(hWnd, point.x, point.y, actual_width, actual_height, (BOOL)TRUE);
202 #if WXWIN_COMPATIBILITY
203 GetEventHandler()->OldOnSize(width, height);
204 #else
205 wxSizeEvent event(wxSize(width, height), m_windowId);
206 event.SetEventObject( this );
207 GetEventHandler()->ProcessEvent(event);
208 #endif
209 }
210
211 void wxFrame::GetSize(int *width, int *height) const
212 {
213 RECT rect;
214 GetWindowRect((HWND) GetHWND(), &rect);
215 *width = rect.right - rect.left;
216 *height = rect.bottom - rect.top;
217 }
218
219 void wxFrame::GetPosition(int *x, int *y) const
220 {
221 RECT rect;
222 GetWindowRect((HWND) GetHWND(), &rect);
223 POINT point;
224 point.x = rect.left;
225 point.y = rect.top;
226
227 *x = point.x;
228 *y = point.y;
229 }
230
231 void wxFrame::SetSize(const int x, const int y, const int width, const int height, const int sizeFlags)
232 {
233 int currentX, currentY;
234 int x1 = x;
235 int y1 = y;
236 int w1 = width;
237 int h1 = height;
238
239 GetPosition(&currentX, &currentY);
240 if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
241 x1 = currentX;
242 if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
243 y1 = currentY;
244
245 int ww,hh ;
246 GetSize(&ww,&hh) ;
247 if (width == -1) w1 = ww ;
248 if (height==-1) h1 = hh ;
249
250 MoveWindow((HWND) GetHWND(), x1, y1, w1, h1, (BOOL)TRUE);
251
252 #if WXWIN_COMPATIBILITY
253 GetEventHandler()->OldOnSize(width, height);
254 #else
255 wxSizeEvent event(wxSize(width, height), m_windowId);
256 event.SetEventObject( this );
257 GetEventHandler()->ProcessEvent(event);
258 #endif
259 }
260
261 bool wxFrame::Show(const bool show)
262 {
263 int cshow;
264 if (show)
265 cshow = SW_SHOW;
266 else
267 cshow = SW_HIDE;
268
269 if (!show)
270 {
271 // Try to highlight the correct window (the parent)
272 HWND hWndParent = 0;
273 if (GetParent())
274 {
275 hWndParent = (HWND) GetParent()->GetHWND();
276 if (hWndParent)
277 ::BringWindowToTop(hWndParent);
278 }
279 }
280
281 ShowWindow((HWND) GetHWND(), (BOOL)cshow);
282 if (show)
283 {
284 BringWindowToTop((HWND) GetHWND());
285
286 #if WXWIN_COMPATIBILITY
287 OldOnActivate(TRUE);
288 #else
289 wxActivateEvent event(wxEVT_ACTIVATE, TRUE, m_windowId);
290 event.SetEventObject( this );
291 GetEventHandler()->ProcessEvent(event);
292 #endif
293 }
294 return TRUE;
295 }
296
297 void wxFrame::Iconize(const bool iconize)
298 {
299 if (!iconize)
300 Show(TRUE);
301
302 int cshow;
303 if (iconize)
304 cshow = SW_MINIMIZE;
305 else
306 cshow = SW_RESTORE;
307 ShowWindow((HWND) GetHWND(), (BOOL)cshow);
308 m_iconized = iconize;
309 }
310
311 // Equivalent to maximize/restore in Windows
312 void wxFrame::Maximize(const bool maximize)
313 {
314 Show(TRUE);
315 int cshow;
316 if (maximize)
317 cshow = SW_MAXIMIZE;
318 else
319 cshow = SW_RESTORE;
320 ShowWindow((HWND) GetHWND(), cshow);
321 m_iconized = FALSE;
322 }
323
324 bool wxFrame::IsIconized(void) const
325 {
326 ((wxFrame *)this)->m_iconized = (::IsIconic((HWND) GetHWND()) != 0);
327 return m_iconized;
328 }
329
330 void wxFrame::SetTitle(const wxString& title)
331 {
332 SetWindowText((HWND) GetHWND(), (const char *)title);
333 }
334
335 wxString wxFrame::GetTitle(void) const
336 {
337 GetWindowText((HWND) GetHWND(), wxBuffer, 1000);
338 return wxString(wxBuffer);
339 }
340
341 void wxFrame::SetIcon(const wxIcon& icon)
342 {
343 m_icon = icon;
344 #if defined(__WIN95__)
345 if ( m_icon.Ok() )
346 SendMessage((HWND) GetHWND(), WM_SETICON,
347 (WPARAM)TRUE, (LPARAM)(HICON) m_icon.GetHICON());
348 #endif
349 }
350
351 wxStatusBar *wxFrame::OnCreateStatusBar(const int number)
352 {
353 wxStatusBar *statusBar = NULL;
354
355 #if USE_NATIVE_STATUSBAR
356 if (UsesNativeStatusBar())
357 {
358 statusBar = new wxStatusBar95(this);
359 }
360 else
361 #endif
362 {
363 statusBar = new wxStatusBar(this, -1, wxPoint(0, 0), wxSize(100, 20));
364
365 // Set the height according to the font and the border size
366 wxClientDC dc(statusBar);
367 dc.SetFont(* statusBar->GetFont());
368
369 long x, y;
370 dc.GetTextExtent("X", &x, &y);
371
372 int height = (int)( (y * 1.1) + 2* statusBar->GetBorderY());
373
374 statusBar->SetSize(-1, -1, 100, height);
375 }
376
377 statusBar->SetFieldsCount(number);
378 return statusBar;
379 }
380
381 bool wxFrame::CreateStatusBar(const int number)
382 {
383 // VZ: calling CreateStatusBar twice is an error - why anyone would do it?
384 wxCHECK_MSG( m_frameStatusBar == NULL, FALSE,
385 "recreating status bar in wxFrame" );
386
387 m_frameStatusBar = OnCreateStatusBar(number);
388 if ( m_frameStatusBar )
389 {
390 PositionStatusBar();
391 return TRUE;
392 }
393 else
394 return FALSE;
395 }
396
397 void wxFrame::SetStatusText(const wxString& text, const int number)
398 {
399 wxCHECK_RET( m_frameStatusBar != NULL, "no statusbar to set text for" );
400
401 m_frameStatusBar->SetStatusText(text, number);
402 }
403
404 void wxFrame::SetStatusWidths(const int n, const int *widths_field)
405 {
406 wxCHECK_RET( m_frameStatusBar != NULL, "no statusbar to set widths for" );
407
408 m_frameStatusBar->SetStatusWidths(n, widths_field);
409 PositionStatusBar();
410 }
411
412 void wxFrame::PositionStatusBar(void)
413 {
414 // native status bar positions itself
415 if (m_frameStatusBar
416 #if USE_NATIVE_STATUSBAR
417 && !m_frameStatusBar->IsKindOf(CLASSINFO(wxStatusBar95))
418 #endif
419 )
420 {
421 int w, h;
422 GetClientSize(&w, &h);
423 int sw, sh;
424 m_frameStatusBar->GetSize(&sw, &sh);
425 m_frameStatusBar->SetSize(0, h, w, sh);
426 }
427 }
428
429 void wxFrame::SetMenuBar(wxMenuBar *menu_bar)
430 {
431 if (!menu_bar)
432 {
433 m_frameMenuBar = NULL;
434 return;
435 }
436
437 if (menu_bar->m_menuBarFrame)
438 return;
439
440 int i;
441 HMENU menu = CreateMenu();
442
443 for (i = 0; i < menu_bar->m_menuCount; i ++)
444 {
445 HMENU popup = (HMENU)menu_bar->m_menus[i]->m_hMenu;
446 //
447 // After looking Bounds Checker result, it seems that all
448 // menus must be individually destroyed. So, don't reset m_hMenu,
449 // to allow ~wxMenu to do the job.
450 //
451 menu_bar->m_menus[i]->m_savehMenu = (WXHMENU) popup;
452 // Uncommenting for the moment... JACS
453 menu_bar->m_menus[i]->m_hMenu = 0;
454 AppendMenu(menu, MF_POPUP | MF_STRING, (UINT)popup, menu_bar->m_titles[i]);
455 }
456
457 menu_bar->m_hMenu = (WXHMENU)menu;
458 if (m_frameMenuBar)
459 delete m_frameMenuBar;
460
461 this->m_hMenu = (WXHMENU) menu;
462
463 DWORD err = 0;
464 if (!SetMenu((HWND) GetHWND(), menu))
465 {
466 #ifdef __WIN32__
467 err = GetLastError();
468 #endif
469 }
470
471 m_frameMenuBar = menu_bar;
472 menu_bar->m_menuBarFrame = this;
473 }
474
475 bool wxFrame::LoadAccelerators(const wxString& table)
476 {
477 m_acceleratorTable = (WXHANDLE)
478 #ifdef __WIN32__
479 #ifdef UNICODE
480 ::LoadAcceleratorsW(wxGetInstance(), (const char *)table);
481 #else
482 ::LoadAcceleratorsA(wxGetInstance(), (const char *)table);
483 #endif
484 #else
485 ::LoadAccelerators(wxGetInstance(), (const char *)table);
486 #endif
487
488 // The above is necessary because LoadAccelerators is a macro
489 // which we have undefed earlier in the file to avoid confusion
490 // with wxFrame::LoadAccelerators. Ugh!
491
492 return (m_acceleratorTable != (WXHANDLE) NULL);
493 }
494
495 void wxFrame::Fit(void)
496 {
497 // Work out max. size
498 wxNode *node = GetChildren()->First();
499 int max_width = 0;
500 int max_height = 0;
501 while (node)
502 {
503 // Find a child that's a subwindow, but not a dialog box.
504 wxWindow *win = (wxWindow *)node->Data();
505
506 if (!win->IsKindOf(CLASSINFO(wxFrame)) &&
507 !win->IsKindOf(CLASSINFO(wxDialog)))
508 {
509 int width, height;
510 int x, y;
511 win->GetSize(&width, &height);
512 win->GetPosition(&x, &y);
513
514 if ((x + width) > max_width)
515 max_width = x + width;
516 if ((y + height) > max_height)
517 max_height = y + height;
518 }
519 node = node->Next();
520 }
521 SetClientSize(max_width, max_height);
522 }
523
524 // Responds to colour changes, and passes event on to children.
525 void wxFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
526 {
527 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_APPWORKSPACE));
528 Refresh();
529
530 if ( m_frameStatusBar )
531 {
532 wxSysColourChangedEvent event2;
533 event2.SetEventObject( m_frameStatusBar );
534 m_frameStatusBar->ProcessEvent(event2);
535 }
536
537 // Propagate the event to the non-top-level children
538 wxWindow::OnSysColourChanged(event);
539 }
540
541 /*
542 * Frame window
543 *
544 */
545
546 void wxFrame::MSWCreate(const int id, wxWindow *parent, const char *wclass, wxWindow *wx_win, const char *title,
547 const int x, const int y, const int width, const int height, const long style)
548
549 {
550 m_defaultIcon = (WXHICON) (wxSTD_FRAME_ICON ? wxSTD_FRAME_ICON : wxDEFAULT_FRAME_ICON);
551
552 // If child windows aren't properly drawn initially, WS_CLIPCHILDREN
553 // could be the culprit. But without it, you can get a lot of flicker.
554
555 // DWORD msflags = WS_POPUP | WS_CLIPCHILDREN ;
556
557 DWORD msflags = 0;
558 if ((style & wxCAPTION) == wxCAPTION)
559 msflags = WS_OVERLAPPED | WS_CLIPCHILDREN ; // WS_POPUP | WS_CLIPCHILDREN ;
560 else
561 msflags = WS_POPUP | WS_CLIPCHILDREN ;
562
563 if (style & wxMINIMIZE_BOX)
564 msflags |= WS_MINIMIZEBOX;
565 if (style & wxMAXIMIZE_BOX)
566 msflags |= WS_MAXIMIZEBOX;
567 if (style & wxTHICK_FRAME)
568 msflags |= WS_THICKFRAME;
569 if (style & wxSYSTEM_MENU)
570 msflags |= WS_SYSMENU;
571 if ((style & wxMINIMIZE) || (style & wxICONIZE))
572 msflags |= WS_MINIMIZE;
573 if (style & wxMAXIMIZE)
574 msflags |= WS_MAXIMIZE;
575 if (style & wxCAPTION)
576 msflags |= WS_CAPTION;
577
578 // Keep this in wxFrame because it saves recoding this function
579 // in wxTinyFrame
580 #if USE_ITSY_BITSY
581 if (style & wxTINY_CAPTION_VERT)
582 msflags |= IBS_VERTCAPTION;
583 if (style & wxTINY_CAPTION_HORIZ)
584 msflags |= IBS_HORZCAPTION;
585 #else
586 if (style & wxTINY_CAPTION_VERT)
587 msflags |= WS_CAPTION;
588 if (style & wxTINY_CAPTION_HORIZ)
589 msflags |= WS_CAPTION;
590 #endif
591 if ((style & wxTHICK_FRAME) == 0)
592 msflags |= WS_BORDER;
593
594 WXDWORD extendedStyle = MakeExtendedStyle(style);
595
596 if (style & wxSTAY_ON_TOP)
597 extendedStyle |= WS_EX_TOPMOST;
598
599 m_iconized = FALSE;
600 wxWindow::MSWCreate(id, parent, wclass, wx_win, title, x, y, width, height,
601 msflags, NULL, extendedStyle);
602 // Seems to be necessary if we use WS_POPUP
603 // style instead of WS_OVERLAPPED
604 if (width > -1 && height > -1)
605 ::PostMessage((HWND) GetHWND(), WM_SIZE, SIZE_RESTORED, MAKELPARAM(width, height));
606 }
607
608 bool wxFrame::MSWOnPaint(void)
609 {
610 #if DEBUG > 1
611 wxDebugMsg("wxFrameWnd::OnPaint %d\n", handle);
612 #endif
613 RECT rect;
614 if (GetUpdateRect((HWND) GetHWND(), &rect, FALSE))
615 {
616 if (m_iconized)
617 {
618 HICON the_icon;
619 if (m_icon.Ok())
620 the_icon = (HICON) m_icon.GetHICON();
621 if (the_icon == 0)
622 the_icon = (HICON) m_defaultIcon;
623
624 PAINTSTRUCT ps;
625 // Hold a pointer to the dc so long as the OnPaint() message
626 // is being processed
627 HDC cdc = BeginPaint((HWND) GetHWND(), &ps);
628
629 // Erase background before painting or we get white background
630 this->MSWDefWindowProc(WM_ICONERASEBKGND,(WORD)ps.hdc,0L);
631
632 if (the_icon)
633 {
634 RECT rect;
635 GetClientRect((HWND) GetHWND(), &rect);
636 int icon_width = 32;
637 int icon_height = 32;
638 int icon_x = (int)((rect.right - icon_width)/2);
639 int icon_y = (int)((rect.bottom - icon_height)/2);
640 DrawIcon(cdc, icon_x, icon_y, the_icon);
641 }
642
643 EndPaint((HWND) GetHWND(), &ps);
644 }
645
646 if (!m_iconized)
647 {
648 // m_paintHDC = (WXHDC) cdc;
649 GetEventHandler()->OldOnPaint();
650 // m_paintHDC = NULL;
651 }
652 return 0;
653 }
654 return 1;
655 }
656
657 WXHICON wxFrame::MSWOnQueryDragIcon(void)
658 {
659 if (m_icon.Ok() && (m_icon.GetHICON() != 0))
660 return m_icon.GetHICON();
661 else
662 return m_defaultIcon;
663 }
664
665 void wxFrame::MSWOnSize(const int x, const int y, const WXUINT id)
666 {
667 #if DEBUG > 1
668 wxDebugMsg("wxFrameWnd::OnSize %d\n", m_hWnd);
669 #endif
670 switch (id)
671 {
672 case SIZEFULLSCREEN:
673 case SIZENORMAL:
674 m_iconized = FALSE;
675 break;
676 case SIZEICONIC:
677 m_iconized = TRUE;
678 break;
679 }
680
681 if (!m_iconized)
682 {
683 // forward WM_SIZE to status bar control
684 #if USE_NATIVE_STATUSBAR
685 if (m_frameStatusBar && m_frameStatusBar->IsKindOf(CLASSINFO(wxStatusBar95)))
686 {
687 wxSizeEvent event(wxSize(x, y), m_frameStatusBar->GetId());
688 event.SetEventObject( m_frameStatusBar );
689
690 ((wxStatusBar95 *)m_frameStatusBar)->OnSize(event);
691 }
692 #endif
693
694 PositionStatusBar();
695 #if WXWIN_COMPATIBILITY
696 GetEventHandler()->OldOnSize(x, y);
697 #else
698 wxSizeEvent event(wxSize(x, y), m_windowId);
699 event.SetEventObject( this );
700 GetEventHandler()->ProcessEvent(event);
701 #endif
702 }
703 }
704
705 bool wxFrame::MSWOnClose(void)
706 {
707 #if DEBUG > 1
708 wxDebugMsg("wxFrameWnd::OnClose %d\n", handle);
709 #endif
710 return Close();
711 }
712
713 bool wxFrame::MSWOnCommand(const WXWORD id, const WXWORD cmd, const WXHWND control)
714 {
715 #if DEBUG > 1
716 wxDebugMsg("wxFrameWnd::OnCommand %d\n", handle);
717 #endif
718 if (cmd == 0 || cmd == 1 ) // Can be either a menu command or an accelerator.
719 {
720 // In case it's e.g. a toolbar.
721 wxWindow *win = wxFindWinFromHandle(control);
722 if (win)
723 return win->MSWCommand(cmd, id);
724
725 if (GetMenuBar() && GetMenuBar()->FindItemForId(id))
726 {
727 ProcessCommand(id);
728 return TRUE;
729 }
730 else
731 return FALSE;
732 }
733 else
734 return FALSE;
735 }
736
737 void wxFrame::MSWOnMenuHighlight(const WXWORD nItem, const WXWORD nFlags, const WXHMENU hSysMenu)
738 {
739 #if WXWIN_COMPATIBILITY
740 if (nFlags == 0xFFFF && hSysMenu == 0)
741 GetEventHandler()->OldOnMenuSelect(-1);
742 else if (nFlags != MF_SEPARATOR)
743 GetEventHandler()->OldOnMenuSelect(nItem);
744 #else
745 if (nFlags == 0xFFFF && hSysMenu == 0)
746 {
747 wxMenuEvent event(wxEVT_MENU_HIGHLIGHT, -1);
748 event.SetEventObject( this );
749 GetEventHandler()->ProcessEvent(event);
750 }
751 else if (nFlags != MF_SEPARATOR)
752 {
753 wxMenuEvent event(wxEVT_MENU_HIGHLIGHT, nItem);
754 event.SetEventObject( this );
755 GetEventHandler()->ProcessEvent(event);
756 }
757 #endif
758 }
759
760 bool wxFrame::MSWProcessMessage(WXMSG* pMsg)
761 {
762 if (m_acceleratorTable != 0 &&
763 ::TranslateAccelerator((HWND) GetHWND(), (HANDLE) m_acceleratorTable, (MSG *)pMsg))
764 return TRUE;
765
766 return FALSE;
767 }
768
769 // Default resizing behaviour - if only ONE subwindow,
770 // resize to client rectangle size
771 void wxFrame::OnSize(wxSizeEvent& event)
772 {
773 // Search for a child which is a subwindow, not another frame.
774 wxWindow *child = NULL;
775 // Count the number of _subwindow_ children
776 int noChildren = 0;
777 for(wxNode *node = GetChildren()->First(); node; node = node->Next())
778 {
779 wxWindow *win = (wxWindow *)node->Data();
780 if (!win->IsKindOf(CLASSINFO(wxFrame)) && !win->IsKindOf(CLASSINFO(wxDialog))
781 && (win != GetStatusBar()))
782 {
783 child = win;
784 noChildren ++;
785 }
786 }
787
788 // If not one child, call the Layout function if compiled in
789 if (!child || (noChildren > 1)
790 #if USE_CONSTRAINTS
791 || GetAutoLayout()
792 #endif
793 )
794 {
795 #if USE_CONSTRAINTS
796 if (GetAutoLayout())
797 Layout();
798 #endif
799 return;
800 }
801
802 if (child)
803 {
804 int client_x, client_y;
805
806 #if DEBUG > 1
807 wxDebugMsg("wxFrame::OnSize: about to set the child's size.\n");
808 #endif
809
810 GetClientSize(&client_x, &client_y);
811 child->SetSize(0, 0, client_x, client_y);
812 }
813
814 }
815
816 // Default activation behaviour - set the focus for the first child
817 // subwindow found.
818 void wxFrame::OnActivate(wxActivateEvent& event)
819 {
820 for(wxNode *node = GetChildren()->First(); node; node = node->Next())
821 {
822 // Find a child that's a subwindow, but not a dialog box.
823 wxWindow *child = (wxWindow *)node->Data();
824 if (!child->IsKindOf(CLASSINFO(wxFrame)) &&
825 !child->IsKindOf(CLASSINFO(wxDialog)))
826 {
827 #if DEBUG > 1
828 wxDebugMsg("wxFrame::OnActivate: about to set the child's focus.\n");
829 #endif
830 child->SetFocus();
831 return;
832 }
833 }
834 }
835
836 // The default implementation for the close window event - calls
837 // OnClose for backward compatibility.
838
839 void wxFrame::OnCloseWindow(wxCloseEvent& event)
840 {
841 // Compatibility
842 if ( GetEventHandler()->OnClose() || event.GetForce())
843 {
844 this->Destroy();
845 }
846 }
847
848 // Destroy the window (delayed, if a managed window)
849 bool wxFrame::Destroy(void)
850 {
851 if (!wxPendingDelete.Member(this))
852 wxPendingDelete.Append(this);
853 return TRUE;
854 }
855
856 // Default menu selection behaviour - display a help string
857 void wxFrame::OnMenuHighlight(wxMenuEvent& event)
858 {
859 if (GetStatusBar())
860 {
861 if (event.GetMenuId() == -1)
862 SetStatusText("");
863 else
864 {
865 wxMenuBar *menuBar = GetMenuBar();
866 if (menuBar)
867 {
868 wxString helpString(menuBar->GetHelpString(event.GetMenuId()));
869 if (helpString != "")
870 SetStatusText(helpString);
871 }
872 }
873 }
874 }
875
876 #if WXWIN_COMPATIBILITY
877 void wxFrame::OldOnSize(int x, int y)
878 {
879 #if WXWIN_COMPATIBILITY == 1
880 wxSizeEvent event(wxSize(x, y), m_windowId);
881 event.SetEventObject( this );
882 if (GetEventHandler()->ProcessEvent(event))
883 return;
884 #endif
885 // Search for a child which is a subwindow, not another frame.
886 wxWindow *child = NULL;
887 // Count the number of _subwindow_ children
888 int noChildren = 0;
889 for(wxNode *node = GetChildren()->First(); node; node = node->Next())
890 {
891 wxWindow *win = (wxWindow *)node->Data();
892 if (!win->IsKindOf(CLASSINFO(wxFrame)) && !win->IsKindOf(CLASSINFO(wxDialog)) && (win != GetStatusBar()))
893 {
894 child = win;
895 noChildren ++;
896 }
897 }
898
899 // If not one child, call the Layout function if compiled in
900 if (!child || (noChildren > 1)
901 #if USE_CONSTRAINTS
902 || GetAutoLayout()
903 #endif
904 )
905 {
906 #if USE_CONSTRAINTS
907 if (GetAutoLayout())
908 Layout();
909 #endif
910 return;
911 }
912
913 if (child)
914 {
915 int client_x, client_y;
916
917 #if DEBUG > 1
918 wxDebugMsg("wxFrame::OnSize: about to set the child's size.\n");
919 #endif
920
921 GetClientSize(&client_x, &client_y);
922 child->SetSize(0, 0, client_x, client_y);
923 }
924 }
925
926 // Default activation behaviour - set the focus for the first child
927 // subwindow found.
928 void wxFrame::OldOnActivate(bool flag)
929 {
930 #if WXWIN_COMPATIBILITY == 1
931 wxActivateEvent event(wxEVT_ACTIVATE, flag, m_windowId);
932 event.SetEventObject( this );
933 if (GetEventHandler()->ProcessEvent(event))
934 return;
935 #endif
936 for(wxNode *node = GetChildren()->First(); node; node = node->Next())
937 {
938 // Find a child that's a subwindow, but not a dialog box.
939 wxWindow *child = (wxWindow *)node->Data();
940 if (!child->IsKindOf(CLASSINFO(wxFrame)) &&
941 !child->IsKindOf(CLASSINFO(wxDialog)))
942 {
943 #if DEBUG > 1
944 wxDebugMsg("wxFrame::OnActivate: about to set the child's focus.\n");
945 #endif
946 child->SetFocus();
947 return;
948 }
949 }
950 }
951
952 // Default menu selection behaviour - display a help string
953 void wxFrame::OldOnMenuSelect(int id)
954 {
955 #if WXWIN_COMPATIBILITY == 1
956 wxMenuEvent event(wxEVT_MENU_HIGHLIGHT, id);
957 event.SetEventObject( this );
958 if (GetEventHandler()->ProcessEvent(event))
959 return;
960 #endif
961 if (GetStatusBar())
962 {
963 if (id == -1)
964 SetStatusText("");
965 else
966 {
967 wxMenuBar *menuBar = GetMenuBar();
968 if (menuBar)
969 {
970 wxString helpString(menuBar->GetHelpString(id));
971 if (helpString != "")
972 SetStatusText(helpString);
973 }
974 }
975 }
976 }
977 #endif
978
979 wxMenuBar *wxFrame::GetMenuBar(void) const
980 {
981 return m_frameMenuBar;
982 }
983
984 void wxFrame::Centre(const int direction)
985 {
986 int display_width, display_height, width, height, x, y;
987 wxDisplaySize(&display_width, &display_height);
988
989 GetSize(&width, &height);
990 GetPosition(&x, &y);
991
992 if (direction & wxHORIZONTAL)
993 x = (int)((display_width - width)/2);
994 if (direction & wxVERTICAL)
995 y = (int)((display_height - height)/2);
996
997 SetSize(x, y, width, height);
998 }
999
1000 // Call this to simulate a menu command
1001 void wxFrame::Command(int id)
1002 {
1003 ProcessCommand(id);
1004 }
1005
1006 void wxFrame::ProcessCommand(int id)
1007 {
1008 wxCommandEvent commandEvent(wxEVENT_TYPE_MENU_COMMAND, id);
1009 commandEvent.SetInt( id );
1010 commandEvent.SetEventObject( this );
1011
1012 wxMenuBar *bar = GetMenuBar() ;
1013 if (!bar)
1014 return;
1015
1016 // Motif does the job by itself!!
1017 #ifndef __MOTIF__
1018 wxMenuItem *item = bar->FindItemForId(id) ;
1019 if (item && item->IsCheckable())
1020 {
1021 //wxDebugMsg("Toggling id %d\n",id) ;
1022 bar->Check(id,!bar->Checked(id)) ;
1023 }
1024 #endif
1025 if (!ProcessEvent(commandEvent))
1026 OldOnMenuCommand(id);
1027 }
1028
1029 void wxFrame::OnIdle(wxIdleEvent& event)
1030 {
1031 DoMenuUpdates();
1032 }
1033
1034 // Query app for menu item updates (called from OnIdle)
1035 void wxFrame::DoMenuUpdates(void)
1036 {
1037 wxMenuBar* bar = GetMenuBar();
1038 if (!bar)
1039 return;
1040
1041 int i;
1042 for (i = 0; i < bar->m_menuCount; i++)
1043 {
1044 wxMenu* menu = bar->m_menus[i];
1045
1046 DoMenuUpdates(menu);
1047 }
1048 }
1049
1050 void wxFrame::DoMenuUpdates(wxMenu* menu)
1051 {
1052 wxNode* node = menu->m_menuItems.First();
1053 while (node)
1054 {
1055 wxMenuItem* item = (wxMenuItem*) node->Data();
1056 if ( !item->IsSeparator() )
1057 {
1058 wxWindowID id = item->GetId();
1059 wxUpdateUIEvent event(id);
1060 event.SetEventObject( this );
1061
1062 if (GetEventHandler()->ProcessEvent(event))
1063 {
1064 if (event.GetSetText())
1065 menu->SetLabel(id, event.GetText());
1066 if (event.GetSetChecked())
1067 menu->Check(id, event.GetChecked());
1068 if (event.GetSetEnabled())
1069 menu->Enable(id, event.GetEnabled());
1070 }
1071
1072 if (item->GetSubMenu())
1073 DoMenuUpdates(item->GetSubMenu());
1074 }
1075 node = node->Next();
1076 }
1077 }