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