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