mdi menu updates fixed (after SF 1012653)
[wxWidgets.git] / src / msw / mdi.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/mdi.cpp
3 // Purpose: MDI classes for wxMSW
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "mdi.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_MDI && !defined(__WXUNIVERSAL__)
32
33 #ifndef WX_PRECOMP
34 #include "wx/setup.h"
35 #include "wx/frame.h"
36 #include "wx/menu.h"
37 #include "wx/app.h"
38 #include "wx/utils.h"
39 #include "wx/dialog.h"
40 #if wxUSE_STATUSBAR
41 #include "wx/statusbr.h"
42 #endif
43 #include "wx/settings.h"
44 #include "wx/intl.h"
45 #include "wx/log.h"
46 #endif
47
48 #include "wx/mdi.h"
49 #include "wx/msw/private.h"
50
51 #if wxUSE_STATUSBAR && wxUSE_NATIVE_STATUSBAR
52 #include "wx/msw/statbr95.h"
53 #endif
54
55 #if wxUSE_TOOLBAR
56 #include "wx/toolbar.h"
57 #endif // wxUSE_TOOLBAR
58
59 #include <string.h>
60
61 // ---------------------------------------------------------------------------
62 // global variables
63 // ---------------------------------------------------------------------------
64
65 extern wxMenu *wxCurrentPopupMenu;
66
67 extern const wxChar *wxMDIFrameClassName; // from app.cpp
68 extern const wxChar *wxMDIChildFrameClassName;
69 extern const wxChar *wxMDIChildFrameClassNameNoRedraw;
70 extern void wxAssociateWinWithHandle(HWND hWnd, wxWindow *win);
71 extern void wxRemoveHandleAssociation(wxWindow *win);
72
73 static HWND invalidHandle = 0;
74
75 // ---------------------------------------------------------------------------
76 // constants
77 // ---------------------------------------------------------------------------
78
79 static const int IDM_WINDOWTILE = 4001;
80 static const int IDM_WINDOWTILEHOR = 4001;
81 static const int IDM_WINDOWCASCADE = 4002;
82 static const int IDM_WINDOWICONS = 4003;
83 static const int IDM_WINDOWNEXT = 4004;
84 static const int IDM_WINDOWTILEVERT = 4005;
85 static const int IDM_WINDOWPREV = 4006;
86
87 // This range gives a maximum of 500 MDI children. Should be enough :-)
88 static const int wxFIRST_MDI_CHILD = 4100;
89 static const int wxLAST_MDI_CHILD = 4600;
90
91 // Status border dimensions
92 static const int wxTHICK_LINE_BORDER = 3;
93 static const int wxTHICK_LINE_WIDTH = 1;
94
95 // ---------------------------------------------------------------------------
96 // private functions
97 // ---------------------------------------------------------------------------
98
99 // set the MDI menus (by sending the WM_MDISETMENU message) and update the menu
100 // of the parent of win (which is supposed to be the MDI client window)
101 static void MDISetMenu(wxWindow *win, HMENU hmenuFrame, HMENU hmenuWindow);
102
103 // insert the window menu (subMenu) into menu just before "Help" submenu or at
104 // the very end if not found
105 static void InsertWindowMenu(wxWindow *win, WXHMENU menu, HMENU subMenu);
106
107 // Remove the window menu
108 static void RemoveWindowMenu(wxWindow *win, WXHMENU menu);
109
110 // is this an id of an MDI child?
111 inline bool IsMdiCommandId(int id)
112 {
113 return (id >= wxFIRST_MDI_CHILD) && (id <= wxLAST_MDI_CHILD);
114 }
115
116 // unpack the parameters of WM_MDIACTIVATE message
117 static void UnpackMDIActivate(WXWPARAM wParam, WXLPARAM lParam,
118 WXWORD *activate, WXHWND *hwndAct, WXHWND *hwndDeact);
119
120 // return the HMENU of the MDI menu
121 static inline HMENU GetMDIWindowMenu(wxMDIParentFrame *frame)
122 {
123 wxMenu *menu = frame->GetWindowMenu();
124 return menu ? GetHmenuOf(menu) : 0;
125 }
126
127 // ===========================================================================
128 // implementation
129 // ===========================================================================
130
131 // ---------------------------------------------------------------------------
132 // wxWin macros
133 // ---------------------------------------------------------------------------
134
135 IMPLEMENT_DYNAMIC_CLASS(wxMDIParentFrame, wxFrame)
136 IMPLEMENT_DYNAMIC_CLASS(wxMDIChildFrame, wxFrame)
137 IMPLEMENT_DYNAMIC_CLASS(wxMDIClientWindow, wxWindow)
138
139 BEGIN_EVENT_TABLE(wxMDIParentFrame, wxFrame)
140 EVT_SIZE(wxMDIParentFrame::OnSize)
141 EVT_SYS_COLOUR_CHANGED(wxMDIParentFrame::OnSysColourChanged)
142 END_EVENT_TABLE()
143
144 BEGIN_EVENT_TABLE(wxMDIChildFrame, wxFrame)
145 EVT_IDLE(wxMDIChildFrame::OnIdle)
146 END_EVENT_TABLE()
147
148 BEGIN_EVENT_TABLE(wxMDIClientWindow, wxWindow)
149 EVT_SCROLL(wxMDIClientWindow::OnScroll)
150 END_EVENT_TABLE()
151
152 // ===========================================================================
153 // wxMDIParentFrame: the frame which contains the client window which manages
154 // the children
155 // ===========================================================================
156
157 wxMDIParentFrame::wxMDIParentFrame()
158 {
159 m_clientWindow = NULL;
160 m_currentChild = NULL;
161 m_windowMenu = (wxMenu*) NULL;
162 m_parentFrameActive = true;
163 }
164
165 bool wxMDIParentFrame::Create(wxWindow *parent,
166 wxWindowID id,
167 const wxString& title,
168 const wxPoint& pos,
169 const wxSize& size,
170 long style,
171 const wxString& name)
172 {
173 m_clientWindow = NULL;
174 m_currentChild = NULL;
175
176 // this style can be used to prevent a window from having the standard MDI
177 // "Window" menu
178 if ( style & wxFRAME_NO_WINDOW_MENU )
179 {
180 m_windowMenu = (wxMenu *)NULL;
181 }
182 else // normal case: we have the window menu, so construct it
183 {
184 m_windowMenu = new wxMenu;
185
186 m_windowMenu->Append(IDM_WINDOWCASCADE, _("&Cascade"));
187 m_windowMenu->Append(IDM_WINDOWTILEHOR, _("Tile &Horizontally"));
188 m_windowMenu->Append(IDM_WINDOWTILEVERT, _("Tile &Vertically"));
189 m_windowMenu->AppendSeparator();
190 m_windowMenu->Append(IDM_WINDOWICONS, _("&Arrange Icons"));
191 m_windowMenu->Append(IDM_WINDOWNEXT, _("&Next"));
192 m_windowMenu->Append(IDM_WINDOWPREV, _("&Previous"));
193 }
194
195 m_parentFrameActive = true;
196
197 if (!parent)
198 wxTopLevelWindows.Append(this);
199
200 SetName(name);
201 m_windowStyle = style;
202
203 if ( parent )
204 parent->AddChild(this);
205
206 if ( id != wxID_ANY )
207 m_windowId = id;
208 else
209 m_windowId = NewControlId();
210
211 WXDWORD exflags;
212 WXDWORD msflags = MSWGetCreateWindowFlags(&exflags);
213 msflags &= ~WS_VSCROLL;
214 msflags &= ~WS_HSCROLL;
215
216 if ( !wxWindow::MSWCreate(wxMDIFrameClassName,
217 title,
218 pos, size,
219 msflags,
220 exflags) )
221 {
222 return false;
223 }
224
225 // unlike (almost?) all other windows, frames are created hidden
226 m_isShown = false;
227
228 return true;
229 }
230
231 wxMDIParentFrame::~wxMDIParentFrame()
232 {
233 // see comment in ~wxMDIChildFrame
234 #if wxUSE_TOOLBAR
235 m_frameToolBar = NULL;
236 #endif
237 #if wxUSE_STATUSBAR
238 m_frameStatusBar = NULL;
239 #endif // wxUSE_STATUSBAR
240
241 DestroyChildren();
242
243 if (m_windowMenu)
244 {
245 delete m_windowMenu;
246 m_windowMenu = (wxMenu*) NULL;
247 }
248
249 // the MDI frame menubar is not automatically deleted by Windows unlike for
250 // the normal frames
251 if ( m_hMenu )
252 {
253 ::DestroyMenu((HMENU)m_hMenu);
254 m_hMenu = (WXHMENU)NULL;
255 }
256
257 if ( m_clientWindow )
258 {
259 if ( m_clientWindow->MSWGetOldWndProc() )
260 m_clientWindow->UnsubclassWin();
261
262 m_clientWindow->SetHWND(0);
263 delete m_clientWindow;
264 }
265 }
266
267 #if wxUSE_MENUS_NATIVE
268
269 void wxMDIParentFrame::InternalSetMenuBar()
270 {
271 m_parentFrameActive = true;
272
273 InsertWindowMenu(GetClientWindow(), m_hMenu, GetMDIWindowMenu(this));
274 }
275
276 #endif // wxUSE_MENUS_NATIVE
277
278 void wxMDIParentFrame::SetWindowMenu(wxMenu* menu)
279 {
280 if (m_windowMenu)
281 {
282 if (GetMenuBar())
283 {
284 // Remove old window menu
285 RemoveWindowMenu(GetClientWindow(), m_hMenu);
286 }
287
288 delete m_windowMenu;
289 m_windowMenu = (wxMenu*) NULL;
290 }
291
292 if (menu)
293 {
294 m_windowMenu = menu;
295 if (GetMenuBar())
296 {
297 InsertWindowMenu(GetClientWindow(), m_hMenu,
298 GetHmenuOf(m_windowMenu));
299 }
300 }
301 }
302
303 void wxMDIParentFrame::DoMenuUpdates(wxMenu* menu)
304 {
305 wxMDIChildFrame *child = GetActiveChild();
306 if ( child )
307 {
308 wxEvtHandler* source = child->GetEventHandler();
309 wxMenuBar* bar = child->GetMenuBar();
310
311 if (menu)
312 {
313 menu->UpdateUI(source);
314 }
315 else
316 {
317 if ( bar != NULL )
318 {
319 int nCount = bar->GetMenuCount();
320 for (int n = 0; n < nCount; n++)
321 bar->GetMenu(n)->UpdateUI(source);
322 }
323 }
324 }
325 else
326 {
327 wxFrameBase::DoMenuUpdates(menu);
328 }
329 }
330
331 void wxMDIParentFrame::OnSize(wxSizeEvent&)
332 {
333 if ( GetClientWindow() )
334 {
335 int width, height;
336 GetClientSize(&width, &height);
337
338 GetClientWindow()->SetSize(0, 0, width, height);
339 }
340 }
341
342 // Returns the active MDI child window
343 wxMDIChildFrame *wxMDIParentFrame::GetActiveChild() const
344 {
345 HWND hWnd = (HWND)::SendMessage(GetWinHwnd(GetClientWindow()),
346 WM_MDIGETACTIVE, 0, 0L);
347 if ( hWnd == 0 )
348 return NULL;
349 else
350 return (wxMDIChildFrame *)wxFindWinFromHandle((WXHWND) hWnd);
351 }
352
353 // Create the client window class (don't Create the window, just return a new
354 // class)
355 wxMDIClientWindow *wxMDIParentFrame::OnCreateClient()
356 {
357 return new wxMDIClientWindow;
358 }
359
360 // Responds to colour changes, and passes event on to children.
361 void wxMDIParentFrame::OnSysColourChanged(wxSysColourChangedEvent& event)
362 {
363 if ( m_clientWindow )
364 {
365 m_clientWindow->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE));
366 m_clientWindow->Refresh();
367 }
368
369 event.Skip();
370 }
371
372 WXHICON wxMDIParentFrame::GetDefaultIcon() const
373 {
374 // we don't have any standard icons (any more)
375 return (WXHICON)0;
376 }
377
378 // ---------------------------------------------------------------------------
379 // MDI operations
380 // ---------------------------------------------------------------------------
381
382 void wxMDIParentFrame::Cascade()
383 {
384 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDICASCADE, 0, 0);
385 }
386
387 // TODO: add a direction argument (hor/vert)
388 void wxMDIParentFrame::Tile()
389 {
390 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDITILE, MDITILE_HORIZONTAL, 0);
391 }
392
393 void wxMDIParentFrame::ArrangeIcons()
394 {
395 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDIICONARRANGE, 0, 0);
396 }
397
398 void wxMDIParentFrame::ActivateNext()
399 {
400 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT, 0, 0);
401 }
402
403 void wxMDIParentFrame::ActivatePrevious()
404 {
405 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT, 0, 1);
406 }
407
408 // ---------------------------------------------------------------------------
409 // the MDI parent frame window proc
410 // ---------------------------------------------------------------------------
411
412 WXLRESULT wxMDIParentFrame::MSWWindowProc(WXUINT message,
413 WXWPARAM wParam,
414 WXLPARAM lParam)
415 {
416 WXLRESULT rc = 0;
417 bool processed = false;
418
419 switch ( message )
420 {
421 case WM_ACTIVATE:
422 {
423 WXWORD state, minimized;
424 WXHWND hwnd;
425 UnpackActivate(wParam, lParam, &state, &minimized, &hwnd);
426
427 processed = HandleActivate(state, minimized != 0, hwnd);
428 }
429 break;
430
431 case WM_COMMAND:
432 {
433 WXWORD id, cmd;
434 WXHWND hwnd;
435 UnpackCommand(wParam, lParam, &id, &hwnd, &cmd);
436
437 (void)HandleCommand(id, cmd, hwnd);
438
439 // even if the frame didn't process it, there is no need to try it
440 // once again (i.e. call wxFrame::HandleCommand()) - we just did it,
441 // so pretend we processed the message anyhow
442 processed = true;
443 }
444
445 // always pass this message DefFrameProc(), otherwise MDI menu
446 // commands (and sys commands - more surprisingly!) won't work
447 MSWDefWindowProc(message, wParam, lParam);
448 break;
449
450 case WM_CREATE:
451 m_clientWindow = OnCreateClient();
452 // Uses own style for client style
453 if ( !m_clientWindow->CreateClient(this, GetWindowStyleFlag()) )
454 {
455 wxLogMessage(_("Failed to create MDI parent frame."));
456
457 rc = -1;
458 }
459
460 processed = true;
461 break;
462
463 case WM_ERASEBKGND:
464 processed = true;
465
466 // we erase background ourselves
467 rc = true;
468 break;
469
470 case WM_MENUSELECT:
471 {
472 WXWORD item, flags;
473 WXHMENU hmenu;
474 UnpackMenuSelect(wParam, lParam, &item, &flags, &hmenu);
475
476 if ( m_parentFrameActive )
477 {
478 processed = HandleMenuSelect(item, flags, hmenu);
479 }
480 else if (m_currentChild)
481 {
482 processed = m_currentChild->
483 HandleMenuSelect(item, flags, hmenu);
484 }
485 }
486 break;
487
488 case WM_SIZE:
489 // as we don't (usually) resize the MDI client to exactly fit the
490 // client area (we put it below the toolbar, above statusbar &c),
491 // we should not pass this one to DefFrameProc
492 break;
493 }
494
495 if ( !processed )
496 rc = wxFrame::MSWWindowProc(message, wParam, lParam);
497
498 return rc;
499 }
500
501 bool wxMDIParentFrame::HandleActivate(int state, bool minimized, WXHWND activate)
502 {
503 bool processed = false;
504
505 if ( wxWindow::HandleActivate(state, minimized, activate) )
506 {
507 // already processed
508 processed = true;
509 }
510
511 // If this window is an MDI parent, we must also send an OnActivate message
512 // to the current child.
513 if ( (m_currentChild != NULL) &&
514 ((state == WA_ACTIVE) || (state == WA_CLICKACTIVE)) )
515 {
516 wxActivateEvent event(wxEVT_ACTIVATE, true, m_currentChild->GetId());
517 event.SetEventObject( m_currentChild );
518 if ( m_currentChild->GetEventHandler()->ProcessEvent(event) )
519 processed = true;
520 }
521
522 return processed;
523 }
524
525 bool wxMDIParentFrame::HandleCommand(WXWORD id, WXWORD cmd, WXHWND hwnd)
526 {
527 // In case it's e.g. a toolbar.
528 if ( hwnd )
529 {
530 wxWindow *win = wxFindWinFromHandle(hwnd);
531 if ( win )
532 return win->MSWCommand(cmd, id);
533 }
534
535 // is it one of standard MDI commands?
536 WXWPARAM wParam = 0;
537 WXLPARAM lParam = 0;
538 int msg;
539 switch ( id )
540 {
541 case IDM_WINDOWCASCADE:
542 msg = WM_MDICASCADE;
543 wParam = MDITILE_SKIPDISABLED;
544 break;
545
546 case IDM_WINDOWTILEHOR:
547 wParam |= MDITILE_HORIZONTAL;
548 // fall through
549
550 case IDM_WINDOWTILEVERT:
551 if ( !wParam )
552 wParam = MDITILE_VERTICAL;
553 msg = WM_MDITILE;
554 wParam |= MDITILE_SKIPDISABLED;
555 break;
556
557 case IDM_WINDOWICONS:
558 msg = WM_MDIICONARRANGE;
559 break;
560
561 case IDM_WINDOWNEXT:
562 msg = WM_MDINEXT;
563 lParam = 0; // next child
564 break;
565
566 case IDM_WINDOWPREV:
567 msg = WM_MDINEXT;
568 lParam = 1; // previous child
569 break;
570
571 default:
572 msg = 0;
573 }
574
575 if ( msg )
576 {
577 ::SendMessage(GetWinHwnd(GetClientWindow()), msg, wParam, lParam);
578
579 return true;
580 }
581
582 // FIXME VZ: what does this test do??
583 if (id >= 0xF000)
584 {
585 return false; // Get WndProc to call default proc
586 }
587
588 if ( IsMdiCommandId(id) )
589 {
590 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
591 while ( node )
592 {
593 wxWindow *child = node->GetData();
594 if ( child->GetHWND() )
595 {
596 long childId = wxGetWindowId(child->GetHWND());
597 if (childId == (long)id)
598 {
599 ::SendMessage( GetWinHwnd(GetClientWindow()),
600 WM_MDIACTIVATE,
601 (WPARAM)child->GetHWND(), 0);
602 return true;
603 }
604 }
605 node = node->GetNext();
606 }
607 }
608 else if ( m_parentFrameActive )
609 {
610 return ProcessCommand(id);
611 }
612 else if ( m_currentChild )
613 {
614 return m_currentChild->HandleCommand(id, cmd, hwnd);
615 }
616 else
617 {
618 // this shouldn't happen because it means that our messages are being
619 // lost (they're not sent to the parent frame nor to the children)
620 wxFAIL_MSG(wxT("MDI parent frame is not active, yet there is no active MDI child?"));
621 }
622
623 return false;
624 }
625
626 WXLRESULT wxMDIParentFrame::MSWDefWindowProc(WXUINT message,
627 WXWPARAM wParam,
628 WXLPARAM lParam)
629 {
630 WXHWND clientWnd;
631 if ( GetClientWindow() )
632 clientWnd = GetClientWindow()->GetHWND();
633 else
634 clientWnd = 0;
635
636 return DefFrameProc(GetHwnd(), (HWND)clientWnd, message, wParam, lParam);
637 }
638
639 bool wxMDIParentFrame::MSWTranslateMessage(WXMSG* msg)
640 {
641 MSG *pMsg = (MSG *)msg;
642
643 // first let the current child get it
644 if ( m_currentChild && m_currentChild->GetHWND() &&
645 m_currentChild->MSWTranslateMessage(msg) )
646 {
647 return true;
648 }
649
650 // then try out accel table (will also check the menu accels)
651 if ( wxFrame::MSWTranslateMessage(msg) )
652 {
653 return true;
654 }
655
656 // finally, check for MDI specific built in accel keys
657 if ( pMsg->message == WM_KEYDOWN || pMsg->message == WM_SYSKEYDOWN )
658 {
659 if ( ::TranslateMDISysAccel(GetWinHwnd(GetClientWindow()), pMsg))
660 return true;
661 }
662
663 return false;
664 }
665
666 // ===========================================================================
667 // wxMDIChildFrame
668 // ===========================================================================
669
670 void wxMDIChildFrame::Init()
671 {
672 m_needsResize = true;
673 }
674
675 bool wxMDIChildFrame::Create(wxMDIParentFrame *parent,
676 wxWindowID id,
677 const wxString& title,
678 const wxPoint& pos,
679 const wxSize& size,
680 long style,
681 const wxString& name)
682 {
683 SetName(name);
684 wxWindowBase::Show(true); // MDI child frame starts off shown
685
686 if ( id != wxID_ANY )
687 m_windowId = id;
688 else
689 m_windowId = (int)NewControlId();
690
691 if ( parent )
692 {
693 parent->AddChild(this);
694 }
695
696 int x = pos.x;
697 int y = pos.y;
698 int width = size.x;
699 int height = size.y;
700
701 MDICREATESTRUCT mcs;
702
703 mcs.szClass = style & wxFULL_REPAINT_ON_RESIZE
704 ? wxMDIChildFrameClassName
705 : wxMDIChildFrameClassNameNoRedraw;
706 mcs.szTitle = title;
707 mcs.hOwner = wxGetInstance();
708 if (x != wxDefaultCoord)
709 mcs.x = x;
710 else
711 mcs.x = CW_USEDEFAULT;
712
713 if (y != wxDefaultCoord)
714 mcs.y = y;
715 else
716 mcs.y = CW_USEDEFAULT;
717
718 if (width != wxDefaultCoord)
719 mcs.cx = width;
720 else
721 mcs.cx = CW_USEDEFAULT;
722
723 if (height != wxDefaultCoord)
724 mcs.cy = height;
725 else
726 mcs.cy = CW_USEDEFAULT;
727
728 DWORD msflags = WS_OVERLAPPED | WS_CLIPCHILDREN | WS_VISIBLE ;
729 if (style & wxMINIMIZE_BOX)
730 msflags |= WS_MINIMIZEBOX;
731 if (style & wxMAXIMIZE_BOX)
732 msflags |= WS_MAXIMIZEBOX;
733 if (style & wxTHICK_FRAME)
734 msflags |= WS_THICKFRAME;
735 if (style & wxSYSTEM_MENU)
736 msflags |= WS_SYSMENU;
737 if ((style & wxMINIMIZE) || (style & wxICONIZE))
738 msflags |= WS_MINIMIZE;
739 if (style & wxMAXIMIZE)
740 msflags |= WS_MAXIMIZE;
741 if (style & wxCAPTION)
742 msflags |= WS_CAPTION;
743
744 mcs.style = msflags;
745
746 mcs.lParam = 0;
747
748 wxWindowCreationHook hook(this);
749
750 m_hWnd = (WXHWND)::SendMessage(GetWinHwnd(parent->GetClientWindow()),
751 WM_MDICREATE, 0, (LONG)(LPSTR)&mcs);
752
753 wxAssociateWinWithHandle((HWND) GetHWND(), this);
754
755 return true;
756 }
757
758 wxMDIChildFrame::~wxMDIChildFrame()
759 {
760 // will be destroyed by DestroyChildren() but reset them before calling it
761 // to avoid using dangling pointers if a callback comes in the meanwhile
762 #if wxUSE_TOOLBAR
763 m_frameToolBar = NULL;
764 #endif
765 #if wxUSE_STATUSBAR
766 m_frameStatusBar = NULL;
767 #endif // wxUSE_STATUSBAR
768
769 DestroyChildren();
770
771 RemoveWindowMenu(NULL, m_hMenu);
772
773 MSWDestroyWindow();
774 }
775
776 // Set the client size (i.e. leave the calculation of borders etc.
777 // to wxWidgets)
778 void wxMDIChildFrame::DoSetClientSize(int width, int height)
779 {
780 HWND hWnd = GetHwnd();
781
782 RECT rect;
783 ::GetClientRect(hWnd, &rect);
784
785 RECT rect2;
786 GetWindowRect(hWnd, &rect2);
787
788 // Find the difference between the entire window (title bar and all)
789 // and the client area; add this to the new client size to move the
790 // window
791 int actual_width = rect2.right - rect2.left - rect.right + width;
792 int actual_height = rect2.bottom - rect2.top - rect.bottom + height;
793
794 #if wxUSE_STATUSBAR
795 if (GetStatusBar() && GetStatusBar()->IsShown())
796 {
797 int sx, sy;
798 GetStatusBar()->GetSize(&sx, &sy);
799 actual_height += sy;
800 }
801 #endif // wxUSE_STATUSBAR
802
803 POINT point;
804 point.x = rect2.left;
805 point.y = rect2.top;
806
807 // If there's an MDI parent, must subtract the parent's top left corner
808 // since MoveWindow moves relative to the parent
809 wxMDIParentFrame *mdiParent = (wxMDIParentFrame *)GetParent();
810 ::ScreenToClient((HWND) mdiParent->GetClientWindow()->GetHWND(), &point);
811
812 MoveWindow(hWnd, point.x, point.y, actual_width, actual_height, (BOOL)true);
813
814 wxSizeEvent event(wxSize(width, height), m_windowId);
815 event.SetEventObject( this );
816 GetEventHandler()->ProcessEvent(event);
817 }
818
819 void wxMDIChildFrame::DoGetPosition(int *x, int *y) const
820 {
821 RECT rect;
822 GetWindowRect(GetHwnd(), &rect);
823 POINT point;
824 point.x = rect.left;
825 point.y = rect.top;
826
827 // Since we now have the absolute screen coords,
828 // if there's a parent we must subtract its top left corner
829 wxMDIParentFrame *mdiParent = (wxMDIParentFrame *)GetParent();
830 ::ScreenToClient((HWND) mdiParent->GetClientWindow()->GetHWND(), &point);
831
832 *x = point.x;
833 *y = point.y;
834 }
835
836 void wxMDIChildFrame::InternalSetMenuBar()
837 {
838 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
839
840 InsertWindowMenu(parent->GetClientWindow(),
841 m_hMenu, GetMDIWindowMenu(parent));
842
843 parent->m_parentFrameActive = false;
844 }
845
846 WXHICON wxMDIChildFrame::GetDefaultIcon() const
847 {
848 // we don't have any standard icons (any more)
849 return (WXHICON)0;
850 }
851
852 // ---------------------------------------------------------------------------
853 // MDI operations
854 // ---------------------------------------------------------------------------
855
856 void wxMDIChildFrame::Maximize(bool maximize)
857 {
858 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
859 if ( parent && parent->GetClientWindow() )
860 {
861 ::SendMessage(GetWinHwnd(parent->GetClientWindow()),
862 maximize ? WM_MDIMAXIMIZE : WM_MDIRESTORE,
863 (WPARAM)GetHwnd(), 0);
864 }
865 }
866
867 void wxMDIChildFrame::Restore()
868 {
869 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
870 if ( parent && parent->GetClientWindow() )
871 {
872 ::SendMessage(GetWinHwnd(parent->GetClientWindow()), WM_MDIRESTORE,
873 (WPARAM) GetHwnd(), 0);
874 }
875 }
876
877 void wxMDIChildFrame::Activate()
878 {
879 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
880 if ( parent && parent->GetClientWindow() )
881 {
882 ::SendMessage(GetWinHwnd(parent->GetClientWindow()), WM_MDIACTIVATE,
883 (WPARAM) GetHwnd(), 0);
884 }
885 }
886
887 // ---------------------------------------------------------------------------
888 // MDI window proc and message handlers
889 // ---------------------------------------------------------------------------
890
891 WXLRESULT wxMDIChildFrame::MSWWindowProc(WXUINT message,
892 WXWPARAM wParam,
893 WXLPARAM lParam)
894 {
895 WXLRESULT rc = 0;
896 bool processed = false;
897
898 switch ( message )
899 {
900 case WM_COMMAND:
901 {
902 WORD id, cmd;
903 WXHWND hwnd;
904 UnpackCommand((WXWPARAM)wParam, (WXLPARAM)lParam,
905 &id, &hwnd, &cmd);
906
907 processed = HandleCommand(id, cmd, (WXHWND)hwnd);
908 }
909 break;
910
911 case WM_GETMINMAXINFO:
912 processed = HandleGetMinMaxInfo((MINMAXINFO *)lParam);
913 break;
914
915 case WM_MDIACTIVATE:
916 {
917 WXWORD act;
918 WXHWND hwndAct, hwndDeact;
919 UnpackMDIActivate(wParam, lParam, &act, &hwndAct, &hwndDeact);
920
921 processed = HandleMDIActivate(act, hwndAct, hwndDeact);
922 }
923 // fall through
924
925 case WM_MOVE:
926 // must pass WM_MOVE to DefMDIChildProc() to recalculate MDI client
927 // scrollbars if necessary
928
929 // fall through
930
931 case WM_SIZE:
932 // must pass WM_SIZE to DefMDIChildProc(), otherwise many weird
933 // things happen
934 MSWDefWindowProc(message, wParam, lParam);
935 break;
936
937 case WM_SYSCOMMAND:
938 // DefMDIChildProc handles SC_{NEXT/PREV}WINDOW here, so pass it
939 // the message (the base class version does not)
940 return MSWDefWindowProc(message, wParam, lParam);
941
942 case WM_WINDOWPOSCHANGING:
943 processed = HandleWindowPosChanging((LPWINDOWPOS)lParam);
944 break;
945 }
946
947 if ( !processed )
948 rc = wxFrame::MSWWindowProc(message, wParam, lParam);
949
950 return rc;
951 }
952
953 bool wxMDIChildFrame::HandleCommand(WXWORD id, WXWORD cmd, WXHWND hwnd)
954 {
955 // In case it's e.g. a toolbar.
956 if ( hwnd )
957 {
958 wxWindow *win = wxFindWinFromHandle(hwnd);
959 if (win)
960 return win->MSWCommand(cmd, id);
961 }
962
963 if (wxCurrentPopupMenu)
964 {
965 wxMenu *popupMenu = wxCurrentPopupMenu;
966 wxCurrentPopupMenu = NULL;
967 if (popupMenu->MSWCommand(cmd, id))
968 return true;
969 }
970
971 bool processed;
972 if (GetMenuBar() && GetMenuBar()->FindItem(id))
973 {
974 processed = ProcessCommand(id);
975 }
976 else
977 {
978 processed = false;
979 }
980
981 return processed;
982 }
983
984 bool wxMDIChildFrame::HandleMDIActivate(long WXUNUSED(activate),
985 WXHWND hwndAct,
986 WXHWND hwndDeact)
987 {
988 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
989
990 HMENU menuToSet = 0;
991
992 bool activated;
993
994 if ( m_hWnd == hwndAct )
995 {
996 activated = true;
997 parent->m_currentChild = this;
998
999 HMENU child_menu = (HMENU)GetWinMenu();
1000 if ( child_menu )
1001 {
1002 parent->m_parentFrameActive = false;
1003
1004 menuToSet = child_menu;
1005 }
1006 }
1007 else if ( m_hWnd == hwndDeact )
1008 {
1009 wxASSERT_MSG( parent->m_currentChild == this,
1010 wxT("can't deactivate MDI child which wasn't active!") );
1011
1012 activated = false;
1013 parent->m_currentChild = NULL;
1014
1015 HMENU parent_menu = (HMENU)parent->GetWinMenu();
1016
1017 // activate the the parent menu only when there is no other child
1018 // that has been activated
1019 if ( parent_menu && !hwndAct )
1020 {
1021 parent->m_parentFrameActive = true;
1022
1023 menuToSet = parent_menu;
1024 }
1025 }
1026 else
1027 {
1028 // we have nothing to do with it
1029 return false;
1030 }
1031
1032 if ( menuToSet )
1033 {
1034 MDISetMenu(parent->GetClientWindow(),
1035 menuToSet, GetMDIWindowMenu(parent));
1036 }
1037
1038 wxActivateEvent event(wxEVT_ACTIVATE, activated, m_windowId);
1039 event.SetEventObject( this );
1040
1041 ResetWindowStyle((void *)NULL);
1042
1043 return GetEventHandler()->ProcessEvent(event);
1044 }
1045
1046 bool wxMDIChildFrame::HandleWindowPosChanging(void *pos)
1047 {
1048 WINDOWPOS *lpPos = (WINDOWPOS *)pos;
1049 #if defined(__WIN95__)
1050 if (!(lpPos->flags & SWP_NOSIZE))
1051 {
1052 RECT rectClient;
1053 DWORD dwExStyle = ::GetWindowLong(GetHwnd(), GWL_EXSTYLE);
1054 DWORD dwStyle = ::GetWindowLong(GetHwnd(), GWL_STYLE);
1055 if (ResetWindowStyle((void *) & rectClient) && (dwStyle & WS_MAXIMIZE))
1056 {
1057 ::AdjustWindowRectEx(&rectClient, dwStyle, false, dwExStyle);
1058 lpPos->x = rectClient.left;
1059 lpPos->y = rectClient.top;
1060 lpPos->cx = rectClient.right - rectClient.left;
1061 lpPos->cy = rectClient.bottom - rectClient.top;
1062 }
1063 #if wxUSE_TOOLBAR
1064 wxMDIParentFrame* pFrameWnd = (wxMDIParentFrame *)GetParent();
1065 if (pFrameWnd && pFrameWnd->GetToolBar() && pFrameWnd->GetToolBar()->IsShown())
1066 {
1067 pFrameWnd->GetToolBar()->Refresh();
1068 }
1069 #endif
1070 }
1071 #endif // Win95
1072
1073 return false;
1074 }
1075
1076 bool wxMDIChildFrame::HandleGetMinMaxInfo(void *mmInfo)
1077 {
1078 MINMAXINFO *info = (MINMAXINFO *)mmInfo;
1079
1080 // let the default window proc calculate the size of MDI children
1081 // frames because it is based on the size of the MDI client window,
1082 // not on the values specified in wxWindow m_max variables
1083 bool processed = MSWDefWindowProc(WM_GETMINMAXINFO, 0, (LPARAM)mmInfo) != 0;
1084
1085 int minWidth = GetMinWidth(),
1086 minHeight = GetMinHeight();
1087
1088 // but allow GetSizeHints() to set the min size
1089 if ( minWidth != -1 )
1090 {
1091 info->ptMinTrackSize.x = minWidth;
1092
1093 processed = true;
1094 }
1095
1096 if ( minHeight != -1 )
1097 {
1098 info->ptMinTrackSize.y = minHeight;
1099
1100 processed = true;
1101 }
1102
1103 return processed;
1104 }
1105
1106 // ---------------------------------------------------------------------------
1107 // MDI specific message translation/preprocessing
1108 // ---------------------------------------------------------------------------
1109
1110 WXLRESULT wxMDIChildFrame::MSWDefWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
1111 {
1112 return DefMDIChildProc(GetHwnd(),
1113 (UINT)message, (WPARAM)wParam, (LPARAM)lParam);
1114 }
1115
1116 bool wxMDIChildFrame::MSWTranslateMessage(WXMSG* msg)
1117 {
1118 return wxFrame::MSWTranslateMessage(msg);
1119 }
1120
1121 // ---------------------------------------------------------------------------
1122 // misc
1123 // ---------------------------------------------------------------------------
1124
1125 void wxMDIChildFrame::MSWDestroyWindow()
1126 {
1127 invalidHandle = GetHwnd();
1128
1129 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
1130
1131 // Must make sure this handle is invalidated (set to NULL) since all sorts
1132 // of things could happen after the child client is destroyed, but before
1133 // the wxFrame is destroyed.
1134
1135 HWND oldHandle = (HWND)GetHWND();
1136 SendMessage(GetWinHwnd(parent->GetClientWindow()), WM_MDIDESTROY,
1137 (WPARAM)oldHandle, 0);
1138
1139 if (parent->GetActiveChild() == (wxMDIChildFrame*) NULL)
1140 ResetWindowStyle((void*) NULL);
1141
1142 invalidHandle = 0;
1143
1144 if (m_hMenu)
1145 {
1146 ::DestroyMenu((HMENU) m_hMenu);
1147 m_hMenu = 0;
1148 }
1149 wxRemoveHandleAssociation(this);
1150 m_hWnd = 0;
1151 }
1152
1153 // Change the client window's extended style so we don't get a client edge
1154 // style when a child is maximised (a double border looks silly.)
1155 bool wxMDIChildFrame::ResetWindowStyle(void *vrect)
1156 {
1157 #if defined(__WIN95__)
1158 RECT *rect = (RECT *)vrect;
1159 wxMDIParentFrame* pFrameWnd = (wxMDIParentFrame *)GetParent();
1160 wxMDIChildFrame* pChild = pFrameWnd->GetActiveChild();
1161 if (!pChild || (pChild == this))
1162 {
1163 HWND hwndClient = GetWinHwnd(pFrameWnd->GetClientWindow());
1164 DWORD dwStyle = ::GetWindowLong(hwndClient, GWL_EXSTYLE);
1165
1166 // we want to test whether there is a maximized child, so just set
1167 // dwThisStyle to 0 if there is no child at all
1168 DWORD dwThisStyle = pChild
1169 ? ::GetWindowLong(GetWinHwnd(pChild), GWL_STYLE) : 0;
1170 DWORD dwNewStyle = dwStyle;
1171 if ( dwThisStyle & WS_MAXIMIZE )
1172 dwNewStyle &= ~(WS_EX_CLIENTEDGE);
1173 else
1174 dwNewStyle |= WS_EX_CLIENTEDGE;
1175
1176 if (dwStyle != dwNewStyle)
1177 {
1178 // force update of everything
1179 ::RedrawWindow(hwndClient, NULL, NULL,
1180 RDW_INVALIDATE | RDW_ALLCHILDREN);
1181 ::SetWindowLong(hwndClient, GWL_EXSTYLE, dwNewStyle);
1182 ::SetWindowPos(hwndClient, NULL, 0, 0, 0, 0,
1183 SWP_FRAMECHANGED | SWP_NOACTIVATE |
1184 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
1185 SWP_NOCOPYBITS);
1186 if (rect)
1187 ::GetClientRect(hwndClient, rect);
1188
1189 return true;
1190 }
1191 }
1192 #endif // Win95
1193
1194 return false;
1195 }
1196
1197 // ===========================================================================
1198 // wxMDIClientWindow: the window of predefined (by Windows) class which
1199 // contains the child frames
1200 // ===========================================================================
1201
1202 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame *parent, long style)
1203 {
1204 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
1205
1206 CLIENTCREATESTRUCT ccs;
1207 m_windowStyle = style;
1208 m_parent = parent;
1209
1210 ccs.hWindowMenu = GetMDIWindowMenu(parent);
1211 ccs.idFirstChild = wxFIRST_MDI_CHILD;
1212
1213 DWORD msStyle = MDIS_ALLCHILDSTYLES | WS_VISIBLE | WS_CHILD |
1214 WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
1215
1216 if ( style & wxHSCROLL )
1217 msStyle |= WS_HSCROLL;
1218 if ( style & wxVSCROLL )
1219 msStyle |= WS_VSCROLL;
1220
1221 #if defined(__WIN95__)
1222 DWORD exStyle = WS_EX_CLIENTEDGE;
1223 #else
1224 DWORD exStyle = 0;
1225 #endif
1226
1227 wxWindowCreationHook hook(this);
1228 m_hWnd = (WXHWND)::CreateWindowEx
1229 (
1230 exStyle,
1231 wxT("MDICLIENT"),
1232 NULL,
1233 msStyle,
1234 0, 0, 0, 0,
1235 GetWinHwnd(parent),
1236 NULL,
1237 wxGetInstance(),
1238 (LPSTR)(LPCLIENTCREATESTRUCT)&ccs);
1239 if ( !m_hWnd )
1240 {
1241 wxLogLastError(wxT("CreateWindowEx(MDI client)"));
1242
1243 return false;
1244 }
1245
1246 SubclassWin(m_hWnd);
1247
1248 return true;
1249 }
1250
1251 // Explicitly call default scroll behaviour
1252 void wxMDIClientWindow::OnScroll(wxScrollEvent& event)
1253 {
1254 // Note: for client windows, the scroll position is not set in
1255 // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what
1256 // scroll position we're at.
1257 // This makes it hard to paint patterns or bitmaps in the background,
1258 // and have the client area scrollable as well.
1259
1260 if ( event.GetOrientation() == wxHORIZONTAL )
1261 m_scrollX = event.GetPosition(); // Always returns zero!
1262 else
1263 m_scrollY = event.GetPosition(); // Always returns zero!
1264
1265 event.Skip();
1266 }
1267
1268 void wxMDIClientWindow::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1269 {
1270 // Try to fix a problem whereby if you show an MDI child frame, then reposition the
1271 // client area, you can end up with a non-refreshed portion in the client window
1272 // (see OGL studio sample). So check if the position is changed and if so,
1273 // redraw the MDI child frames.
1274
1275 wxPoint oldPos = GetPosition();
1276
1277 wxWindow::DoSetSize(x, y, width, height, sizeFlags);
1278
1279 wxPoint newPos = GetPosition();
1280
1281 if ((newPos.x != oldPos.x) || (newPos.y != oldPos.y))
1282 {
1283 if (GetParent())
1284 {
1285 wxWindowList::compatibility_iterator node = GetParent()->GetChildren().GetFirst();
1286 while (node)
1287 {
1288 wxWindow *child = node->GetData();
1289 if (child->IsKindOf(CLASSINFO(wxMDIChildFrame)))
1290 {
1291 ::RedrawWindow(GetHwndOf(child),
1292 NULL,
1293 NULL,
1294 RDW_FRAME |
1295 RDW_ALLCHILDREN |
1296 RDW_INVALIDATE);
1297 }
1298 node = node->GetNext();
1299 }
1300 }
1301 }
1302 }
1303
1304 void wxMDIChildFrame::OnIdle(wxIdleEvent& event)
1305 {
1306 // MDI child frames get their WM_SIZE when they're constructed but at this
1307 // moment they don't have any children yet so all child windows will be
1308 // positioned incorrectly when they are added later - to fix this, we
1309 // generate an artificial size event here
1310 if ( m_needsResize )
1311 {
1312 m_needsResize = false; // avoid any possibility of recursion
1313
1314 SendSizeEvent();
1315 }
1316
1317 event.Skip();
1318 }
1319
1320 // ---------------------------------------------------------------------------
1321 // non member functions
1322 // ---------------------------------------------------------------------------
1323
1324 static void MDISetMenu(wxWindow *win, HMENU hmenuFrame, HMENU hmenuWindow)
1325 {
1326 ::SendMessage(GetWinHwnd(win), WM_MDISETMENU,
1327 #ifdef __WIN32__
1328 (WPARAM)hmenuFrame, (LPARAM)hmenuWindow
1329 #else
1330 0, MAKELPARAM(hmenuFrame, hmenuWindow)
1331 #endif
1332 );
1333
1334 // update menu bar of the parent window
1335 wxWindow *parent = win->GetParent();
1336 wxCHECK_RET( parent, wxT("MDI client without parent frame? weird...") );
1337
1338 ::SendMessage(GetWinHwnd(win), WM_MDIREFRESHMENU, 0, 0L);
1339
1340 ::DrawMenuBar(GetWinHwnd(parent));
1341 }
1342
1343 static void InsertWindowMenu(wxWindow *win, WXHMENU menu, HMENU subMenu)
1344 {
1345 // Try to insert Window menu in front of Help, otherwise append it.
1346 HMENU hmenu = (HMENU)menu;
1347
1348 if (subMenu)
1349 {
1350 int N = GetMenuItemCount(hmenu);
1351 bool success = false;
1352 for ( int i = 0; i < N; i++ )
1353 {
1354 wxChar buf[256];
1355 int chars = GetMenuString(hmenu, i, buf, WXSIZEOF(buf), MF_BYPOSITION);
1356 if ( chars == 0 )
1357 {
1358 wxLogLastError(wxT("GetMenuString"));
1359
1360 continue;
1361 }
1362
1363 if ( wxStripMenuCodes(wxString(buf)).IsSameAs(_("Help")) )
1364 {
1365 success = true;
1366 ::InsertMenu(hmenu, i, MF_BYPOSITION | MF_POPUP | MF_STRING,
1367 (UINT)subMenu, _("&Window"));
1368 break;
1369 }
1370 }
1371
1372 if ( !success )
1373 {
1374 ::AppendMenu(hmenu, MF_POPUP, (UINT)subMenu, _("&Window"));
1375 }
1376 }
1377
1378 MDISetMenu(win, hmenu, subMenu);
1379 }
1380
1381 static void RemoveWindowMenu(wxWindow *win, WXHMENU menu)
1382 {
1383 HMENU hMenu = (HMENU)menu;
1384
1385 if ( hMenu )
1386 {
1387 wxChar buf[1024];
1388
1389 int N = ::GetMenuItemCount(hMenu);
1390 for ( int i = 0; i < N; i++ )
1391 {
1392 if ( !::GetMenuString(hMenu, i, buf, WXSIZEOF(buf), MF_BYPOSITION) )
1393 {
1394 // Ignore successful read of menu string with length 0 which
1395 // occurs, for example, for a maximized MDI childs system menu
1396 if ( ::GetLastError() != 0 )
1397 {
1398 wxLogLastError(wxT("GetMenuString"));
1399 }
1400
1401 continue;
1402 }
1403
1404 if ( wxStrcmp(buf, _("&Window")) == 0 )
1405 {
1406 if ( !::RemoveMenu(hMenu, i, MF_BYPOSITION) )
1407 {
1408 wxLogLastError(wxT("RemoveMenu"));
1409 }
1410
1411 break;
1412 }
1413 }
1414 }
1415
1416 if ( win )
1417 {
1418 // we don't change the windows menu, but we update the main one
1419 MDISetMenu(win, hMenu, NULL);
1420 }
1421 }
1422
1423 static void UnpackMDIActivate(WXWPARAM wParam, WXLPARAM lParam,
1424 WXWORD *activate, WXHWND *hwndAct, WXHWND *hwndDeact)
1425 {
1426 *activate = true;
1427 *hwndAct = (WXHWND)lParam;
1428 *hwndDeact = (WXHWND)wParam;
1429 }
1430
1431 #endif // wxUSE_MDI && !defined(__WXUNIVERSAL__)
1432