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