]> git.saurik.com Git - wxWidgets.git/blob - src/msw/frame.cpp
Cured problem introduced by LEAVE/ENTER OnIdle code; bugs in gauge sizing
[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 = 0;
556 if ((style & wxCAPTION) == wxCAPTION)
557 msflags = WS_OVERLAPPED;
558 else
559 msflags = WS_POPUP;
560
561 if (style & wxMINIMIZE_BOX)
562 msflags |= WS_MINIMIZEBOX;
563 if (style & wxMAXIMIZE_BOX)
564 msflags |= WS_MAXIMIZEBOX;
565 if (style & wxTHICK_FRAME)
566 msflags |= WS_THICKFRAME;
567 if (style & wxSYSTEM_MENU)
568 msflags |= WS_SYSMENU;
569 if ((style & wxMINIMIZE) || (style & wxICONIZE))
570 msflags |= WS_MINIMIZE;
571 if (style & wxMAXIMIZE)
572 msflags |= WS_MAXIMIZE;
573 if (style & wxCAPTION)
574 msflags |= WS_CAPTION;
575 if (style & wxCLIP_CHILDREN)
576 msflags |= WS_CLIPCHILDREN;
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 else
646 {
647 GetEventHandler()->OldOnPaint();
648 }
649 return 0;
650 }
651 return 1;
652 }
653
654 WXHICON wxFrame::MSWOnQueryDragIcon(void)
655 {
656 if (m_icon.Ok() && (m_icon.GetHICON() != 0))
657 return m_icon.GetHICON();
658 else
659 return m_defaultIcon;
660 }
661
662 void wxFrame::MSWOnSize(const int x, const int y, const WXUINT id)
663 {
664 #if DEBUG > 1
665 wxDebugMsg("wxFrameWnd::OnSize %d\n", m_hWnd);
666 #endif
667 switch (id)
668 {
669 case SIZEFULLSCREEN:
670 case SIZENORMAL:
671 m_iconized = FALSE;
672 break;
673 case SIZEICONIC:
674 m_iconized = TRUE;
675 break;
676 }
677
678 if (!m_iconized)
679 {
680 // forward WM_SIZE to status bar control
681 #if USE_NATIVE_STATUSBAR
682 if (m_frameStatusBar && m_frameStatusBar->IsKindOf(CLASSINFO(wxStatusBar95)))
683 {
684 wxSizeEvent event(wxSize(x, y), m_frameStatusBar->GetId());
685 event.SetEventObject( m_frameStatusBar );
686
687 ((wxStatusBar95 *)m_frameStatusBar)->OnSize(event);
688 }
689 #endif
690
691 PositionStatusBar();
692 #if WXWIN_COMPATIBILITY
693 GetEventHandler()->OldOnSize(x, y);
694 #else
695 wxSizeEvent event(wxSize(x, y), m_windowId);
696 event.SetEventObject( this );
697 GetEventHandler()->ProcessEvent(event);
698 #endif
699 }
700 }
701
702 bool wxFrame::MSWOnClose(void)
703 {
704 #if DEBUG > 1
705 wxDebugMsg("wxFrameWnd::OnClose %d\n", handle);
706 #endif
707 return Close();
708 }
709
710 bool wxFrame::MSWOnCommand(const WXWORD id, const WXWORD cmd, const WXHWND control)
711 {
712 #if DEBUG > 1
713 wxDebugMsg("wxFrameWnd::OnCommand %d\n", handle);
714 #endif
715 if (cmd == 0 || cmd == 1 ) // Can be either a menu command or an accelerator.
716 {
717 // In case it's e.g. a toolbar.
718 wxWindow *win = wxFindWinFromHandle(control);
719 if (win)
720 return win->MSWCommand(cmd, id);
721
722 if (GetMenuBar() && GetMenuBar()->FindItemForId(id))
723 {
724 ProcessCommand(id);
725 return TRUE;
726 }
727 else
728 return FALSE;
729 }
730 else
731 return FALSE;
732 }
733
734 void wxFrame::MSWOnMenuHighlight(const WXWORD nItem, const WXWORD nFlags, const WXHMENU hSysMenu)
735 {
736 #if WXWIN_COMPATIBILITY
737 if (nFlags == 0xFFFF && hSysMenu == 0)
738 GetEventHandler()->OldOnMenuSelect(-1);
739 else if (nFlags != MF_SEPARATOR)
740 GetEventHandler()->OldOnMenuSelect(nItem);
741 #else
742 if (nFlags == 0xFFFF && hSysMenu == 0)
743 {
744 wxMenuEvent event(wxEVT_MENU_HIGHLIGHT, -1);
745 event.SetEventObject( this );
746 GetEventHandler()->ProcessEvent(event);
747 }
748 else if (nFlags != MF_SEPARATOR)
749 {
750 wxMenuEvent event(wxEVT_MENU_HIGHLIGHT, nItem);
751 event.SetEventObject( this );
752 GetEventHandler()->ProcessEvent(event);
753 }
754 #endif
755 }
756
757 bool wxFrame::MSWProcessMessage(WXMSG* pMsg)
758 {
759 if (m_acceleratorTable != 0 &&
760 ::TranslateAccelerator((HWND) GetHWND(), (HANDLE) m_acceleratorTable, (MSG *)pMsg))
761 return TRUE;
762
763 return FALSE;
764 }
765
766 // Default resizing behaviour - if only ONE subwindow,
767 // resize to client rectangle size
768 void wxFrame::OnSize(wxSizeEvent& event)
769 {
770 // Search for a child which is a subwindow, not another frame.
771 wxWindow *child = NULL;
772 // Count the number of _subwindow_ children
773 int noChildren = 0;
774 for(wxNode *node = GetChildren()->First(); node; node = node->Next())
775 {
776 wxWindow *win = (wxWindow *)node->Data();
777 if (!win->IsKindOf(CLASSINFO(wxFrame)) && !win->IsKindOf(CLASSINFO(wxDialog))
778 && (win != GetStatusBar()))
779 {
780 child = win;
781 noChildren ++;
782 }
783 }
784
785 // If not one child, call the Layout function if compiled in
786 if (!child || (noChildren > 1)
787 #if USE_CONSTRAINTS
788 || GetAutoLayout()
789 #endif
790 )
791 {
792 #if USE_CONSTRAINTS
793 if (GetAutoLayout())
794 Layout();
795 #endif
796 return;
797 }
798
799 if (child)
800 {
801 int client_x, client_y;
802
803 #if DEBUG > 1
804 wxDebugMsg("wxFrame::OnSize: about to set the child's size.\n");
805 #endif
806
807 GetClientSize(&client_x, &client_y);
808 child->SetSize(0, 0, client_x, client_y);
809 }
810
811 }
812
813 // Default activation behaviour - set the focus for the first child
814 // subwindow found.
815 void wxFrame::OnActivate(wxActivateEvent& event)
816 {
817 for(wxNode *node = GetChildren()->First(); node; node = node->Next())
818 {
819 // Find a child that's a subwindow, but not a dialog box.
820 wxWindow *child = (wxWindow *)node->Data();
821 if (!child->IsKindOf(CLASSINFO(wxFrame)) &&
822 !child->IsKindOf(CLASSINFO(wxDialog)))
823 {
824 #if DEBUG > 1
825 wxDebugMsg("wxFrame::OnActivate: about to set the child's focus.\n");
826 #endif
827 child->SetFocus();
828 return;
829 }
830 }
831 }
832
833 // The default implementation for the close window event - calls
834 // OnClose for backward compatibility.
835
836 void wxFrame::OnCloseWindow(wxCloseEvent& event)
837 {
838 // Compatibility
839 if ( GetEventHandler()->OnClose() || event.GetForce())
840 {
841 this->Destroy();
842 }
843 }
844
845 // Destroy the window (delayed, if a managed window)
846 bool wxFrame::Destroy(void)
847 {
848 if (!wxPendingDelete.Member(this))
849 wxPendingDelete.Append(this);
850 return TRUE;
851 }
852
853 // Default menu selection behaviour - display a help string
854 void wxFrame::OnMenuHighlight(wxMenuEvent& event)
855 {
856 if (GetStatusBar())
857 {
858 if (event.GetMenuId() == -1)
859 SetStatusText("");
860 else
861 {
862 wxMenuBar *menuBar = GetMenuBar();
863 if (menuBar)
864 {
865 wxString helpString(menuBar->GetHelpString(event.GetMenuId()));
866 if (helpString != "")
867 SetStatusText(helpString);
868 }
869 }
870 }
871 }
872
873 #if WXWIN_COMPATIBILITY
874 void wxFrame::OldOnSize(int x, int y)
875 {
876 #if WXWIN_COMPATIBILITY == 1
877 wxSizeEvent event(wxSize(x, y), m_windowId);
878 event.SetEventObject( this );
879 if (GetEventHandler()->ProcessEvent(event))
880 return;
881 #endif
882 // Search for a child which is a subwindow, not another frame.
883 wxWindow *child = NULL;
884 // Count the number of _subwindow_ children
885 int noChildren = 0;
886 for(wxNode *node = GetChildren()->First(); node; node = node->Next())
887 {
888 wxWindow *win = (wxWindow *)node->Data();
889 if (!win->IsKindOf(CLASSINFO(wxFrame)) && !win->IsKindOf(CLASSINFO(wxDialog)) && (win != GetStatusBar()))
890 {
891 child = win;
892 noChildren ++;
893 }
894 }
895
896 // If not one child, call the Layout function if compiled in
897 if (!child || (noChildren > 1)
898 #if USE_CONSTRAINTS
899 || GetAutoLayout()
900 #endif
901 )
902 {
903 #if USE_CONSTRAINTS
904 if (GetAutoLayout())
905 Layout();
906 #endif
907 return;
908 }
909
910 if (child)
911 {
912 int client_x, client_y;
913
914 #if DEBUG > 1
915 wxDebugMsg("wxFrame::OnSize: about to set the child's size.\n");
916 #endif
917
918 GetClientSize(&client_x, &client_y);
919 child->SetSize(0, 0, client_x, client_y);
920 }
921 }
922
923 // Default activation behaviour - set the focus for the first child
924 // subwindow found.
925 void wxFrame::OldOnActivate(bool flag)
926 {
927 #if WXWIN_COMPATIBILITY == 1
928 wxActivateEvent event(wxEVT_ACTIVATE, flag, m_windowId);
929 event.SetEventObject( this );
930 if (GetEventHandler()->ProcessEvent(event))
931 return;
932 #endif
933 for(wxNode *node = GetChildren()->First(); node; node = node->Next())
934 {
935 // Find a child that's a subwindow, but not a dialog box.
936 wxWindow *child = (wxWindow *)node->Data();
937 if (!child->IsKindOf(CLASSINFO(wxFrame)) &&
938 !child->IsKindOf(CLASSINFO(wxDialog)))
939 {
940 #if DEBUG > 1
941 wxDebugMsg("wxFrame::OnActivate: about to set the child's focus.\n");
942 #endif
943 child->SetFocus();
944 return;
945 }
946 }
947 }
948
949 // Default menu selection behaviour - display a help string
950 void wxFrame::OldOnMenuSelect(int id)
951 {
952 #if WXWIN_COMPATIBILITY == 1
953 wxMenuEvent event(wxEVT_MENU_HIGHLIGHT, id);
954 event.SetEventObject( this );
955 if (GetEventHandler()->ProcessEvent(event))
956 return;
957 #endif
958 if (GetStatusBar())
959 {
960 if (id == -1)
961 SetStatusText("");
962 else
963 {
964 wxMenuBar *menuBar = GetMenuBar();
965 if (menuBar)
966 {
967 wxString helpString(menuBar->GetHelpString(id));
968 if (helpString != "")
969 SetStatusText(helpString);
970 }
971 }
972 }
973 }
974 #endif
975
976 wxMenuBar *wxFrame::GetMenuBar(void) const
977 {
978 return m_frameMenuBar;
979 }
980
981 void wxFrame::Centre(const int direction)
982 {
983 int display_width, display_height, width, height, x, y;
984 wxDisplaySize(&display_width, &display_height);
985
986 GetSize(&width, &height);
987 GetPosition(&x, &y);
988
989 if (direction & wxHORIZONTAL)
990 x = (int)((display_width - width)/2);
991 if (direction & wxVERTICAL)
992 y = (int)((display_height - height)/2);
993
994 SetSize(x, y, width, height);
995 }
996
997 // Call this to simulate a menu command
998 void wxFrame::Command(int id)
999 {
1000 ProcessCommand(id);
1001 }
1002
1003 void wxFrame::ProcessCommand(int id)
1004 {
1005 wxCommandEvent commandEvent(wxEVENT_TYPE_MENU_COMMAND, id);
1006 commandEvent.SetInt( id );
1007 commandEvent.SetEventObject( this );
1008
1009 wxMenuBar *bar = GetMenuBar() ;
1010 if (!bar)
1011 return;
1012
1013 // Motif does the job by itself!!
1014 #ifndef __MOTIF__
1015 wxMenuItem *item = bar->FindItemForId(id) ;
1016 if (item && item->IsCheckable())
1017 {
1018 //wxDebugMsg("Toggling id %d\n",id) ;
1019 bar->Check(id,!bar->Checked(id)) ;
1020 }
1021 #endif
1022 if (!ProcessEvent(commandEvent))
1023 OldOnMenuCommand(id);
1024 }
1025
1026 void wxFrame::OnIdle(wxIdleEvent& event)
1027 {
1028 DoMenuUpdates();
1029 }
1030
1031 // Query app for menu item updates (called from OnIdle)
1032 void wxFrame::DoMenuUpdates(void)
1033 {
1034 wxMenuBar* bar = GetMenuBar();
1035 if (!bar)
1036 return;
1037
1038 int i;
1039 for (i = 0; i < bar->m_menuCount; i++)
1040 {
1041 wxMenu* menu = bar->m_menus[i];
1042
1043 DoMenuUpdates(menu);
1044 }
1045 }
1046
1047 void wxFrame::DoMenuUpdates(wxMenu* menu)
1048 {
1049 wxNode* node = menu->m_menuItems.First();
1050 while (node)
1051 {
1052 wxMenuItem* item = (wxMenuItem*) node->Data();
1053 if ( !item->IsSeparator() )
1054 {
1055 wxWindowID id = item->GetId();
1056 wxUpdateUIEvent event(id);
1057 event.SetEventObject( this );
1058
1059 if (GetEventHandler()->ProcessEvent(event))
1060 {
1061 if (event.GetSetText())
1062 menu->SetLabel(id, event.GetText());
1063 if (event.GetSetChecked())
1064 menu->Check(id, event.GetChecked());
1065 if (event.GetSetEnabled())
1066 menu->Enable(id, event.GetEnabled());
1067 }
1068
1069 if (item->GetSubMenu())
1070 DoMenuUpdates(item->GetSubMenu());
1071 }
1072 node = node->Next();
1073 }
1074 }