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