]> git.saurik.com Git - wxWidgets.git/blob - src/msw/mdi.cpp
return begining of the selection for GetInsertionPoint to match what
[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 void wxMDIParentFrame::Tile(wxOrientation orient)
388 {
389 wxASSERT_MSG( orient == wxHORIZONTAL || orient == wxVERTICAL,
390 _T("invalid orientation value") );
391
392 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDITILE,
393 orient == wxHORIZONTAL ? MDITILE_HORIZONTAL
394 : MDITILE_VERTICAL, 0);
395 }
396
397 void wxMDIParentFrame::ArrangeIcons()
398 {
399 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDIICONARRANGE, 0, 0);
400 }
401
402 void wxMDIParentFrame::ActivateNext()
403 {
404 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT, 0, 0);
405 }
406
407 void wxMDIParentFrame::ActivatePrevious()
408 {
409 ::SendMessage(GetWinHwnd(GetClientWindow()), WM_MDINEXT, 0, 1);
410 }
411
412 // ---------------------------------------------------------------------------
413 // the MDI parent frame window proc
414 // ---------------------------------------------------------------------------
415
416 WXLRESULT wxMDIParentFrame::MSWWindowProc(WXUINT message,
417 WXWPARAM wParam,
418 WXLPARAM lParam)
419 {
420 WXLRESULT rc = 0;
421 bool processed = false;
422
423 switch ( message )
424 {
425 case WM_ACTIVATE:
426 {
427 WXWORD state, minimized;
428 WXHWND hwnd;
429 UnpackActivate(wParam, lParam, &state, &minimized, &hwnd);
430
431 processed = HandleActivate(state, minimized != 0, hwnd);
432 }
433 break;
434
435 case WM_COMMAND:
436 {
437 WXWORD id, cmd;
438 WXHWND hwnd;
439 UnpackCommand(wParam, lParam, &id, &hwnd, &cmd);
440
441 (void)HandleCommand(id, cmd, hwnd);
442
443 // even if the frame didn't process it, there is no need to try it
444 // once again (i.e. call wxFrame::HandleCommand()) - we just did it,
445 // so pretend we processed the message anyhow
446 processed = true;
447 }
448
449 // always pass this message DefFrameProc(), otherwise MDI menu
450 // commands (and sys commands - more surprisingly!) won't work
451 MSWDefWindowProc(message, wParam, lParam);
452 break;
453
454 case WM_CREATE:
455 m_clientWindow = OnCreateClient();
456 // Uses own style for client style
457 if ( !m_clientWindow->CreateClient(this, GetWindowStyleFlag()) )
458 {
459 wxLogMessage(_("Failed to create MDI parent frame."));
460
461 rc = -1;
462 }
463
464 processed = true;
465 break;
466
467 case WM_ERASEBKGND:
468 processed = true;
469
470 // we erase background ourselves
471 rc = true;
472 break;
473
474 case WM_MENUSELECT:
475 {
476 WXWORD item, flags;
477 WXHMENU hmenu;
478 UnpackMenuSelect(wParam, lParam, &item, &flags, &hmenu);
479
480 if ( m_parentFrameActive )
481 {
482 processed = HandleMenuSelect(item, flags, hmenu);
483 }
484 else if (m_currentChild)
485 {
486 processed = m_currentChild->
487 HandleMenuSelect(item, flags, hmenu);
488 }
489 }
490 break;
491
492 case WM_SIZE:
493 // though we don't (usually) resize the MDI client to exactly fit the
494 // client area we need to pass this one to DefFrameProc to allow the children to show
495 break;
496 }
497
498 if ( !processed )
499 rc = wxFrame::MSWWindowProc(message, wParam, lParam);
500
501 return rc;
502 }
503
504 bool wxMDIParentFrame::HandleActivate(int state, bool minimized, WXHWND activate)
505 {
506 bool processed = false;
507
508 if ( wxWindow::HandleActivate(state, minimized, activate) )
509 {
510 // already processed
511 processed = true;
512 }
513
514 // If this window is an MDI parent, we must also send an OnActivate message
515 // to the current child.
516 if ( (m_currentChild != NULL) &&
517 ((state == WA_ACTIVE) || (state == WA_CLICKACTIVE)) )
518 {
519 wxActivateEvent event(wxEVT_ACTIVATE, true, m_currentChild->GetId());
520 event.SetEventObject( m_currentChild );
521 if ( m_currentChild->GetEventHandler()->ProcessEvent(event) )
522 processed = true;
523 }
524
525 return processed;
526 }
527
528 bool wxMDIParentFrame::HandleCommand(WXWORD id, WXWORD cmd, WXHWND hwnd)
529 {
530 // In case it's e.g. a toolbar.
531 if ( hwnd )
532 {
533 wxWindow *win = wxFindWinFromHandle(hwnd);
534 if ( win )
535 return win->MSWCommand(cmd, id);
536 }
537
538 // is it one of standard MDI commands?
539 WXWPARAM wParam = 0;
540 WXLPARAM lParam = 0;
541 int msg;
542 switch ( id )
543 {
544 case IDM_WINDOWCASCADE:
545 msg = WM_MDICASCADE;
546 wParam = MDITILE_SKIPDISABLED;
547 break;
548
549 case IDM_WINDOWTILEHOR:
550 wParam |= MDITILE_HORIZONTAL;
551 // fall through
552
553 case IDM_WINDOWTILEVERT:
554 if ( !wParam )
555 wParam = MDITILE_VERTICAL;
556 msg = WM_MDITILE;
557 wParam |= MDITILE_SKIPDISABLED;
558 break;
559
560 case IDM_WINDOWICONS:
561 msg = WM_MDIICONARRANGE;
562 break;
563
564 case IDM_WINDOWNEXT:
565 msg = WM_MDINEXT;
566 lParam = 0; // next child
567 break;
568
569 case IDM_WINDOWPREV:
570 msg = WM_MDINEXT;
571 lParam = 1; // previous child
572 break;
573
574 default:
575 msg = 0;
576 }
577
578 if ( msg )
579 {
580 ::SendMessage(GetWinHwnd(GetClientWindow()), msg, wParam, lParam);
581
582 return true;
583 }
584
585 // FIXME VZ: what does this test do??
586 if (id >= 0xF000)
587 {
588 return false; // Get WndProc to call default proc
589 }
590
591 if ( IsMdiCommandId(id) )
592 {
593 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
594 while ( node )
595 {
596 wxWindow *child = node->GetData();
597 if ( child->GetHWND() )
598 {
599 long childId = wxGetWindowId(child->GetHWND());
600 if (childId == (long)id)
601 {
602 ::SendMessage( GetWinHwnd(GetClientWindow()),
603 WM_MDIACTIVATE,
604 (WPARAM)child->GetHWND(), 0);
605 return true;
606 }
607 }
608 node = node->GetNext();
609 }
610 }
611 else if ( m_parentFrameActive )
612 {
613 return ProcessCommand(id);
614 }
615 else if ( m_currentChild )
616 {
617 return m_currentChild->HandleCommand(id, cmd, hwnd);
618 }
619 else
620 {
621 // this shouldn't happen because it means that our messages are being
622 // lost (they're not sent to the parent frame nor to the children)
623 wxFAIL_MSG(wxT("MDI parent frame is not active, yet there is no active MDI child?"));
624 }
625
626 return false;
627 }
628
629 WXLRESULT wxMDIParentFrame::MSWDefWindowProc(WXUINT message,
630 WXWPARAM wParam,
631 WXLPARAM lParam)
632 {
633 WXHWND clientWnd;
634 if ( GetClientWindow() )
635 clientWnd = GetClientWindow()->GetHWND();
636 else
637 clientWnd = 0;
638
639 return DefFrameProc(GetHwnd(), (HWND)clientWnd, message, wParam, lParam);
640 }
641
642 bool wxMDIParentFrame::MSWTranslateMessage(WXMSG* msg)
643 {
644 MSG *pMsg = (MSG *)msg;
645
646 // first let the current child get it
647 if ( m_currentChild && m_currentChild->GetHWND() &&
648 m_currentChild->MSWTranslateMessage(msg) )
649 {
650 return true;
651 }
652
653 // then try out accel table (will also check the menu accels)
654 if ( wxFrame::MSWTranslateMessage(msg) )
655 {
656 return true;
657 }
658
659 // finally, check for MDI specific built in accel keys
660 if ( pMsg->message == WM_KEYDOWN || pMsg->message == WM_SYSKEYDOWN )
661 {
662 if ( ::TranslateMDISysAccel(GetWinHwnd(GetClientWindow()), pMsg))
663 return true;
664 }
665
666 return false;
667 }
668
669 // ===========================================================================
670 // wxMDIChildFrame
671 // ===========================================================================
672
673 void wxMDIChildFrame::Init()
674 {
675 m_needsResize = true;
676 m_needsInitialShow = true;
677 }
678
679 bool wxMDIChildFrame::Create(wxMDIParentFrame *parent,
680 wxWindowID id,
681 const wxString& title,
682 const wxPoint& pos,
683 const wxSize& size,
684 long style,
685 const wxString& name)
686 {
687 SetName(name);
688
689 if ( id != wxID_ANY )
690 m_windowId = id;
691 else
692 m_windowId = (int)NewControlId();
693
694 if ( parent )
695 {
696 parent->AddChild(this);
697 }
698
699 int x = pos.x;
700 int y = pos.y;
701 int width = size.x;
702 int height = size.y;
703
704 MDICREATESTRUCT mcs;
705
706 mcs.szClass = style & wxFULL_REPAINT_ON_RESIZE
707 ? wxMDIChildFrameClassName
708 : wxMDIChildFrameClassNameNoRedraw;
709 mcs.szTitle = title;
710 mcs.hOwner = wxGetInstance();
711 if (x != wxDefaultCoord)
712 mcs.x = x;
713 else
714 mcs.x = CW_USEDEFAULT;
715
716 if (y != wxDefaultCoord)
717 mcs.y = y;
718 else
719 mcs.y = CW_USEDEFAULT;
720
721 if (width != wxDefaultCoord)
722 mcs.cx = width;
723 else
724 mcs.cx = CW_USEDEFAULT;
725
726 if (height != wxDefaultCoord)
727 mcs.cy = height;
728 else
729 mcs.cy = CW_USEDEFAULT;
730
731 DWORD msflags = WS_OVERLAPPED | WS_CLIPCHILDREN;
732 if (style & wxMINIMIZE_BOX)
733 msflags |= WS_MINIMIZEBOX;
734 if (style & wxMAXIMIZE_BOX)
735 msflags |= WS_MAXIMIZEBOX;
736 if (style & wxTHICK_FRAME)
737 msflags |= WS_THICKFRAME;
738 if (style & wxSYSTEM_MENU)
739 msflags |= WS_SYSMENU;
740 if ((style & wxMINIMIZE) || (style & wxICONIZE))
741 msflags |= WS_MINIMIZE;
742 if (style & wxMAXIMIZE)
743 msflags |= WS_MAXIMIZE;
744 if (style & wxCAPTION)
745 msflags |= WS_CAPTION;
746
747 mcs.style = msflags;
748
749 mcs.lParam = 0;
750
751 wxWindowCreationHook hook(this);
752
753 m_hWnd = (WXHWND)::SendMessage(GetWinHwnd(parent->GetClientWindow()),
754 WM_MDICREATE, 0, (LONG)(LPSTR)&mcs);
755
756 wxAssociateWinWithHandle((HWND) GetHWND(), this);
757
758 return true;
759 }
760
761 wxMDIChildFrame::~wxMDIChildFrame()
762 {
763 // will be destroyed by DestroyChildren() but reset them before calling it
764 // to avoid using dangling pointers if a callback comes in the meanwhile
765 #if wxUSE_TOOLBAR
766 m_frameToolBar = NULL;
767 #endif
768 #if wxUSE_STATUSBAR
769 m_frameStatusBar = NULL;
770 #endif // wxUSE_STATUSBAR
771
772 DestroyChildren();
773
774 RemoveWindowMenu(NULL, m_hMenu);
775
776 MSWDestroyWindow();
777 }
778
779 bool wxMDIChildFrame::Show(bool show)
780 {
781 m_needsInitialShow = false;
782 return wxFrame::Show(show);
783 }
784
785 // Set the client size (i.e. leave the calculation of borders etc.
786 // to wxWidgets)
787 void wxMDIChildFrame::DoSetClientSize(int width, int height)
788 {
789 HWND hWnd = GetHwnd();
790
791 RECT rect;
792 ::GetClientRect(hWnd, &rect);
793
794 RECT rect2;
795 GetWindowRect(hWnd, &rect2);
796
797 // Find the difference between the entire window (title bar and all)
798 // and the client area; add this to the new client size to move the
799 // window
800 int actual_width = rect2.right - rect2.left - rect.right + width;
801 int actual_height = rect2.bottom - rect2.top - rect.bottom + height;
802
803 #if wxUSE_STATUSBAR
804 if (GetStatusBar() && GetStatusBar()->IsShown())
805 {
806 int sx, sy;
807 GetStatusBar()->GetSize(&sx, &sy);
808 actual_height += sy;
809 }
810 #endif // wxUSE_STATUSBAR
811
812 POINT point;
813 point.x = rect2.left;
814 point.y = rect2.top;
815
816 // If there's an MDI parent, must subtract the parent's top left corner
817 // since MoveWindow moves relative to the parent
818 wxMDIParentFrame *mdiParent = (wxMDIParentFrame *)GetParent();
819 ::ScreenToClient((HWND) mdiParent->GetClientWindow()->GetHWND(), &point);
820
821 MoveWindow(hWnd, point.x, point.y, actual_width, actual_height, (BOOL)true);
822
823 wxSize size(width, height);
824 wxSizeEvent event(size, m_windowId);
825 event.SetEventObject( this );
826 GetEventHandler()->ProcessEvent(event);
827 }
828
829 void wxMDIChildFrame::DoGetPosition(int *x, int *y) const
830 {
831 RECT rect;
832 GetWindowRect(GetHwnd(), &rect);
833 POINT point;
834 point.x = rect.left;
835 point.y = rect.top;
836
837 // Since we now have the absolute screen coords,
838 // if there's a parent we must subtract its top left corner
839 wxMDIParentFrame *mdiParent = (wxMDIParentFrame *)GetParent();
840 ::ScreenToClient((HWND) mdiParent->GetClientWindow()->GetHWND(), &point);
841
842 *x = point.x;
843 *y = point.y;
844 }
845
846 void wxMDIChildFrame::InternalSetMenuBar()
847 {
848 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
849
850 InsertWindowMenu(parent->GetClientWindow(),
851 m_hMenu, GetMDIWindowMenu(parent));
852
853 parent->m_parentFrameActive = false;
854 }
855
856 WXHICON wxMDIChildFrame::GetDefaultIcon() const
857 {
858 // we don't have any standard icons (any more)
859 return (WXHICON)0;
860 }
861
862 // ---------------------------------------------------------------------------
863 // MDI operations
864 // ---------------------------------------------------------------------------
865
866 void wxMDIChildFrame::Maximize(bool maximize)
867 {
868 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
869 if ( parent && parent->GetClientWindow() )
870 {
871 ::SendMessage(GetWinHwnd(parent->GetClientWindow()),
872 maximize ? WM_MDIMAXIMIZE : WM_MDIRESTORE,
873 (WPARAM)GetHwnd(), 0);
874 }
875 }
876
877 void wxMDIChildFrame::Restore()
878 {
879 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
880 if ( parent && parent->GetClientWindow() )
881 {
882 ::SendMessage(GetWinHwnd(parent->GetClientWindow()), WM_MDIRESTORE,
883 (WPARAM) GetHwnd(), 0);
884 }
885 }
886
887 void wxMDIChildFrame::Activate()
888 {
889 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
890 if ( parent && parent->GetClientWindow() )
891 {
892 ::SendMessage(GetWinHwnd(parent->GetClientWindow()), WM_MDIACTIVATE,
893 (WPARAM) GetHwnd(), 0);
894 }
895 }
896
897 // ---------------------------------------------------------------------------
898 // MDI window proc and message handlers
899 // ---------------------------------------------------------------------------
900
901 WXLRESULT wxMDIChildFrame::MSWWindowProc(WXUINT message,
902 WXWPARAM wParam,
903 WXLPARAM lParam)
904 {
905 WXLRESULT rc = 0;
906 bool processed = false;
907
908 switch ( message )
909 {
910 case WM_COMMAND:
911 {
912 WORD id, cmd;
913 WXHWND hwnd;
914 UnpackCommand((WXWPARAM)wParam, (WXLPARAM)lParam,
915 &id, &hwnd, &cmd);
916
917 processed = HandleCommand(id, cmd, (WXHWND)hwnd);
918 }
919 break;
920
921 case WM_GETMINMAXINFO:
922 processed = HandleGetMinMaxInfo((MINMAXINFO *)lParam);
923 break;
924
925 case WM_MDIACTIVATE:
926 {
927 WXWORD act;
928 WXHWND hwndAct, hwndDeact;
929 UnpackMDIActivate(wParam, lParam, &act, &hwndAct, &hwndDeact);
930
931 processed = HandleMDIActivate(act, hwndAct, hwndDeact);
932 }
933 // fall through
934
935 case WM_MOVE:
936 // must pass WM_MOVE to DefMDIChildProc() to recalculate MDI client
937 // scrollbars if necessary
938
939 // fall through
940
941 case WM_SIZE:
942 // must pass WM_SIZE to DefMDIChildProc(), otherwise many weird
943 // things happen
944 MSWDefWindowProc(message, wParam, lParam);
945 break;
946
947 case WM_SYSCOMMAND:
948 // DefMDIChildProc handles SC_{NEXT/PREV}WINDOW here, so pass it
949 // the message (the base class version does not)
950 return MSWDefWindowProc(message, wParam, lParam);
951
952 case WM_WINDOWPOSCHANGING:
953 processed = HandleWindowPosChanging((LPWINDOWPOS)lParam);
954 break;
955 }
956
957 if ( !processed )
958 rc = wxFrame::MSWWindowProc(message, wParam, lParam);
959
960 return rc;
961 }
962
963 bool wxMDIChildFrame::HandleCommand(WXWORD id, WXWORD cmd, WXHWND hwnd)
964 {
965 // In case it's e.g. a toolbar.
966 if ( hwnd )
967 {
968 wxWindow *win = wxFindWinFromHandle(hwnd);
969 if (win)
970 return win->MSWCommand(cmd, id);
971 }
972
973 if (wxCurrentPopupMenu)
974 {
975 wxMenu *popupMenu = wxCurrentPopupMenu;
976 wxCurrentPopupMenu = NULL;
977 if (popupMenu->MSWCommand(cmd, id))
978 return true;
979 }
980
981 bool processed;
982 if (GetMenuBar() && GetMenuBar()->FindItem(id))
983 {
984 processed = ProcessCommand(id);
985 }
986 else
987 {
988 processed = false;
989 }
990
991 return processed;
992 }
993
994 bool wxMDIChildFrame::HandleMDIActivate(long WXUNUSED(activate),
995 WXHWND hwndAct,
996 WXHWND hwndDeact)
997 {
998 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
999
1000 HMENU menuToSet = 0;
1001
1002 bool activated;
1003
1004 if ( m_hWnd == hwndAct )
1005 {
1006 activated = true;
1007 parent->m_currentChild = this;
1008
1009 HMENU child_menu = (HMENU)GetWinMenu();
1010 if ( child_menu )
1011 {
1012 parent->m_parentFrameActive = false;
1013
1014 menuToSet = child_menu;
1015 }
1016 }
1017 else if ( m_hWnd == hwndDeact )
1018 {
1019 wxASSERT_MSG( parent->m_currentChild == this,
1020 wxT("can't deactivate MDI child which wasn't active!") );
1021
1022 activated = false;
1023 parent->m_currentChild = NULL;
1024
1025 HMENU parent_menu = (HMENU)parent->GetWinMenu();
1026
1027 // activate the the parent menu only when there is no other child
1028 // that has been activated
1029 if ( parent_menu && !hwndAct )
1030 {
1031 parent->m_parentFrameActive = true;
1032
1033 menuToSet = parent_menu;
1034 }
1035 }
1036 else
1037 {
1038 // we have nothing to do with it
1039 return false;
1040 }
1041
1042 if ( menuToSet )
1043 {
1044 MDISetMenu(parent->GetClientWindow(),
1045 menuToSet, GetMDIWindowMenu(parent));
1046 }
1047
1048 wxActivateEvent event(wxEVT_ACTIVATE, activated, m_windowId);
1049 event.SetEventObject( this );
1050
1051 ResetWindowStyle((void *)NULL);
1052
1053 return GetEventHandler()->ProcessEvent(event);
1054 }
1055
1056 bool wxMDIChildFrame::HandleWindowPosChanging(void *pos)
1057 {
1058 WINDOWPOS *lpPos = (WINDOWPOS *)pos;
1059 #if defined(__WIN95__)
1060 if (!(lpPos->flags & SWP_NOSIZE))
1061 {
1062 RECT rectClient;
1063 DWORD dwExStyle = ::GetWindowLong(GetHwnd(), GWL_EXSTYLE);
1064 DWORD dwStyle = ::GetWindowLong(GetHwnd(), GWL_STYLE);
1065 if (ResetWindowStyle((void *) & rectClient) && (dwStyle & WS_MAXIMIZE))
1066 {
1067 ::AdjustWindowRectEx(&rectClient, dwStyle, false, dwExStyle);
1068 lpPos->x = rectClient.left;
1069 lpPos->y = rectClient.top;
1070 lpPos->cx = rectClient.right - rectClient.left;
1071 lpPos->cy = rectClient.bottom - rectClient.top;
1072 }
1073 #if wxUSE_TOOLBAR
1074 wxMDIParentFrame* pFrameWnd = (wxMDIParentFrame *)GetParent();
1075 if (pFrameWnd && pFrameWnd->GetToolBar() && pFrameWnd->GetToolBar()->IsShown())
1076 {
1077 pFrameWnd->GetToolBar()->Refresh();
1078 }
1079 #endif
1080 }
1081 #endif // Win95
1082
1083 return false;
1084 }
1085
1086 bool wxMDIChildFrame::HandleGetMinMaxInfo(void *mmInfo)
1087 {
1088 MINMAXINFO *info = (MINMAXINFO *)mmInfo;
1089
1090 // let the default window proc calculate the size of MDI children
1091 // frames because it is based on the size of the MDI client window,
1092 // not on the values specified in wxWindow m_max variables
1093 bool processed = MSWDefWindowProc(WM_GETMINMAXINFO, 0, (LPARAM)mmInfo) != 0;
1094
1095 int minWidth = GetMinWidth(),
1096 minHeight = GetMinHeight();
1097
1098 // but allow GetSizeHints() to set the min size
1099 if ( minWidth != -1 )
1100 {
1101 info->ptMinTrackSize.x = minWidth;
1102
1103 processed = true;
1104 }
1105
1106 if ( minHeight != -1 )
1107 {
1108 info->ptMinTrackSize.y = minHeight;
1109
1110 processed = true;
1111 }
1112
1113 return processed;
1114 }
1115
1116 // ---------------------------------------------------------------------------
1117 // MDI specific message translation/preprocessing
1118 // ---------------------------------------------------------------------------
1119
1120 WXLRESULT wxMDIChildFrame::MSWDefWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
1121 {
1122 return DefMDIChildProc(GetHwnd(),
1123 (UINT)message, (WPARAM)wParam, (LPARAM)lParam);
1124 }
1125
1126 bool wxMDIChildFrame::MSWTranslateMessage(WXMSG* msg)
1127 {
1128 return wxFrame::MSWTranslateMessage(msg);
1129 }
1130
1131 // ---------------------------------------------------------------------------
1132 // misc
1133 // ---------------------------------------------------------------------------
1134
1135 void wxMDIChildFrame::MSWDestroyWindow()
1136 {
1137 invalidHandle = GetHwnd();
1138
1139 wxMDIParentFrame *parent = (wxMDIParentFrame *)GetParent();
1140
1141 // Must make sure this handle is invalidated (set to NULL) since all sorts
1142 // of things could happen after the child client is destroyed, but before
1143 // the wxFrame is destroyed.
1144
1145 HWND oldHandle = (HWND)GetHWND();
1146 SendMessage(GetWinHwnd(parent->GetClientWindow()), WM_MDIDESTROY,
1147 (WPARAM)oldHandle, 0);
1148
1149 if (parent->GetActiveChild() == (wxMDIChildFrame*) NULL)
1150 ResetWindowStyle((void*) NULL);
1151
1152 invalidHandle = 0;
1153
1154 if (m_hMenu)
1155 {
1156 ::DestroyMenu((HMENU) m_hMenu);
1157 m_hMenu = 0;
1158 }
1159 wxRemoveHandleAssociation(this);
1160 m_hWnd = 0;
1161 }
1162
1163 // Change the client window's extended style so we don't get a client edge
1164 // style when a child is maximised (a double border looks silly.)
1165 bool wxMDIChildFrame::ResetWindowStyle(void *vrect)
1166 {
1167 #if defined(__WIN95__)
1168 RECT *rect = (RECT *)vrect;
1169 wxMDIParentFrame* pFrameWnd = (wxMDIParentFrame *)GetParent();
1170 wxMDIChildFrame* pChild = pFrameWnd->GetActiveChild();
1171 if (!pChild || (pChild == this))
1172 {
1173 HWND hwndClient = GetWinHwnd(pFrameWnd->GetClientWindow());
1174 DWORD dwStyle = ::GetWindowLong(hwndClient, GWL_EXSTYLE);
1175
1176 // we want to test whether there is a maximized child, so just set
1177 // dwThisStyle to 0 if there is no child at all
1178 DWORD dwThisStyle = pChild
1179 ? ::GetWindowLong(GetWinHwnd(pChild), GWL_STYLE) : 0;
1180 DWORD dwNewStyle = dwStyle;
1181 if ( dwThisStyle & WS_MAXIMIZE )
1182 dwNewStyle &= ~(WS_EX_CLIENTEDGE);
1183 else
1184 dwNewStyle |= WS_EX_CLIENTEDGE;
1185
1186 if (dwStyle != dwNewStyle)
1187 {
1188 // force update of everything
1189 ::RedrawWindow(hwndClient, NULL, NULL,
1190 RDW_INVALIDATE | RDW_ALLCHILDREN);
1191 ::SetWindowLong(hwndClient, GWL_EXSTYLE, dwNewStyle);
1192 ::SetWindowPos(hwndClient, NULL, 0, 0, 0, 0,
1193 SWP_FRAMECHANGED | SWP_NOACTIVATE |
1194 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
1195 SWP_NOCOPYBITS);
1196 if (rect)
1197 ::GetClientRect(hwndClient, rect);
1198
1199 return true;
1200 }
1201 }
1202 #endif // Win95
1203
1204 return false;
1205 }
1206
1207 // ===========================================================================
1208 // wxMDIClientWindow: the window of predefined (by Windows) class which
1209 // contains the child frames
1210 // ===========================================================================
1211
1212 bool wxMDIClientWindow::CreateClient(wxMDIParentFrame *parent, long style)
1213 {
1214 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_APPWORKSPACE);
1215
1216 CLIENTCREATESTRUCT ccs;
1217 m_windowStyle = style;
1218 m_parent = parent;
1219
1220 ccs.hWindowMenu = GetMDIWindowMenu(parent);
1221 ccs.idFirstChild = wxFIRST_MDI_CHILD;
1222
1223 DWORD msStyle = MDIS_ALLCHILDSTYLES | WS_VISIBLE | WS_CHILD |
1224 WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
1225
1226 if ( style & wxHSCROLL )
1227 msStyle |= WS_HSCROLL;
1228 if ( style & wxVSCROLL )
1229 msStyle |= WS_VSCROLL;
1230
1231 #if defined(__WIN95__)
1232 DWORD exStyle = WS_EX_CLIENTEDGE;
1233 #else
1234 DWORD exStyle = 0;
1235 #endif
1236
1237 wxWindowCreationHook hook(this);
1238 m_hWnd = (WXHWND)::CreateWindowEx
1239 (
1240 exStyle,
1241 wxT("MDICLIENT"),
1242 NULL,
1243 msStyle,
1244 0, 0, 0, 0,
1245 GetWinHwnd(parent),
1246 NULL,
1247 wxGetInstance(),
1248 (LPSTR)(LPCLIENTCREATESTRUCT)&ccs);
1249 if ( !m_hWnd )
1250 {
1251 wxLogLastError(wxT("CreateWindowEx(MDI client)"));
1252
1253 return false;
1254 }
1255
1256 SubclassWin(m_hWnd);
1257
1258 return true;
1259 }
1260
1261 // Explicitly call default scroll behaviour
1262 void wxMDIClientWindow::OnScroll(wxScrollEvent& event)
1263 {
1264 // Note: for client windows, the scroll position is not set in
1265 // WM_HSCROLL, WM_VSCROLL, so we can't easily determine what
1266 // scroll position we're at.
1267 // This makes it hard to paint patterns or bitmaps in the background,
1268 // and have the client area scrollable as well.
1269
1270 if ( event.GetOrientation() == wxHORIZONTAL )
1271 m_scrollX = event.GetPosition(); // Always returns zero!
1272 else
1273 m_scrollY = event.GetPosition(); // Always returns zero!
1274
1275 event.Skip();
1276 }
1277
1278 void wxMDIClientWindow::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1279 {
1280 // Try to fix a problem whereby if you show an MDI child frame, then reposition the
1281 // client area, you can end up with a non-refreshed portion in the client window
1282 // (see OGL studio sample). So check if the position is changed and if so,
1283 // redraw the MDI child frames.
1284
1285 wxPoint oldPos = GetPosition();
1286
1287 wxWindow::DoSetSize(x, y, width, height, sizeFlags);
1288
1289 wxPoint newPos = GetPosition();
1290
1291 if ((newPos.x != oldPos.x) || (newPos.y != oldPos.y))
1292 {
1293 if (GetParent())
1294 {
1295 wxWindowList::compatibility_iterator node = GetParent()->GetChildren().GetFirst();
1296 while (node)
1297 {
1298 wxWindow *child = node->GetData();
1299 if (child->IsKindOf(CLASSINFO(wxMDIChildFrame)))
1300 {
1301 ::RedrawWindow(GetHwndOf(child),
1302 NULL,
1303 NULL,
1304 RDW_FRAME |
1305 RDW_ALLCHILDREN |
1306 RDW_INVALIDATE);
1307 }
1308 node = node->GetNext();
1309 }
1310 }
1311 }
1312 }
1313
1314 void wxMDIChildFrame::OnIdle(wxIdleEvent& event)
1315 {
1316 // wxMSW prior to 2.5.3 created MDI child frames as visible, which resulted
1317 // in flicker e.g. when the frame contained controls with non-trivial
1318 // layout. Since 2.5.3, the frame is created hidden as all other top level
1319 // windows. In order to maintain backward compatibility, the frame is shown
1320 // in OnIdle, unless Show(false) was called by the programmer before.
1321 if ( m_needsInitialShow )
1322 {
1323 Show(true);
1324 }
1325
1326 // MDI child frames get their WM_SIZE when they're constructed but at this
1327 // moment they don't have any children yet so all child windows will be
1328 // positioned incorrectly when they are added later - to fix this, we
1329 // generate an artificial size event here
1330 if ( m_needsResize )
1331 {
1332 m_needsResize = false; // avoid any possibility of recursion
1333
1334 SendSizeEvent();
1335 }
1336
1337 event.Skip();
1338 }
1339
1340 // ---------------------------------------------------------------------------
1341 // non member functions
1342 // ---------------------------------------------------------------------------
1343
1344 static void MDISetMenu(wxWindow *win, HMENU hmenuFrame, HMENU hmenuWindow)
1345 {
1346 ::SendMessage(GetWinHwnd(win), WM_MDISETMENU,
1347 #ifdef __WIN32__
1348 (WPARAM)hmenuFrame, (LPARAM)hmenuWindow
1349 #else
1350 0, MAKELPARAM(hmenuFrame, hmenuWindow)
1351 #endif
1352 );
1353
1354 // update menu bar of the parent window
1355 wxWindow *parent = win->GetParent();
1356 wxCHECK_RET( parent, wxT("MDI client without parent frame? weird...") );
1357
1358 ::SendMessage(GetWinHwnd(win), WM_MDIREFRESHMENU, 0, 0L);
1359
1360 ::DrawMenuBar(GetWinHwnd(parent));
1361 }
1362
1363 static void InsertWindowMenu(wxWindow *win, WXHMENU menu, HMENU subMenu)
1364 {
1365 // Try to insert Window menu in front of Help, otherwise append it.
1366 HMENU hmenu = (HMENU)menu;
1367
1368 if (subMenu)
1369 {
1370 int N = GetMenuItemCount(hmenu);
1371 bool success = false;
1372 for ( int i = 0; i < N; i++ )
1373 {
1374 wxChar buf[256];
1375 int chars = GetMenuString(hmenu, i, buf, WXSIZEOF(buf), MF_BYPOSITION);
1376 if ( chars == 0 )
1377 {
1378 wxLogLastError(wxT("GetMenuString"));
1379
1380 continue;
1381 }
1382
1383 if ( wxStripMenuCodes(wxString(buf)).IsSameAs(_("Help")) )
1384 {
1385 success = true;
1386 ::InsertMenu(hmenu, i, MF_BYPOSITION | MF_POPUP | MF_STRING,
1387 (UINT)subMenu, _("&Window"));
1388 break;
1389 }
1390 }
1391
1392 if ( !success )
1393 {
1394 ::AppendMenu(hmenu, MF_POPUP, (UINT)subMenu, _("&Window"));
1395 }
1396 }
1397
1398 MDISetMenu(win, hmenu, subMenu);
1399 }
1400
1401 static void RemoveWindowMenu(wxWindow *win, WXHMENU menu)
1402 {
1403 HMENU hMenu = (HMENU)menu;
1404
1405 if ( hMenu )
1406 {
1407 wxChar buf[1024];
1408
1409 int N = ::GetMenuItemCount(hMenu);
1410 for ( int i = 0; i < N; i++ )
1411 {
1412 if ( !::GetMenuString(hMenu, i, buf, WXSIZEOF(buf), MF_BYPOSITION) )
1413 {
1414 // Ignore successful read of menu string with length 0 which
1415 // occurs, for example, for a maximized MDI childs system menu
1416 if ( ::GetLastError() != 0 )
1417 {
1418 wxLogLastError(wxT("GetMenuString"));
1419 }
1420
1421 continue;
1422 }
1423
1424 if ( wxStrcmp(buf, _("&Window")) == 0 )
1425 {
1426 if ( !::RemoveMenu(hMenu, i, MF_BYPOSITION) )
1427 {
1428 wxLogLastError(wxT("RemoveMenu"));
1429 }
1430
1431 break;
1432 }
1433 }
1434 }
1435
1436 if ( win )
1437 {
1438 // we don't change the windows menu, but we update the main one
1439 MDISetMenu(win, hMenu, NULL);
1440 }
1441 }
1442
1443 static void UnpackMDIActivate(WXWPARAM wParam, WXLPARAM lParam,
1444 WXWORD *activate, WXHWND *hwndAct, WXHWND *hwndDeact)
1445 {
1446 *activate = true;
1447 *hwndAct = (WXHWND)lParam;
1448 *hwndDeact = (WXHWND)wParam;
1449 }
1450
1451 #endif // wxUSE_MDI && !defined(__WXUNIVERSAL__)
1452