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