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