home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / owlsrc.pak / MDICHILD.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  7.8 KB  |  285 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1991, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Implementation of TMDIChild. This defines the basic behavior of all MDI
  6. //   child windows
  7. //----------------------------------------------------------------------------
  8. #pragma hdrignore SECTION
  9. #include <owl/owlpch.h>
  10. #include <owl/mdichild.h>
  11. #include <owl/mdi.h>
  12. #include <owl/applicat.h>
  13.  
  14. DIAG_DECLARE_GROUP(OwlWin);  // diagnostic group for windows
  15.  
  16. // Define to work around here in the child, the 'creating MDI child maximized
  17. // corrupts the menu bar' bug 
  18. //
  19. #define MDIMAX_WORKAROUND
  20.  
  21. #if !defined(SECTION) || SECTION == 1
  22.  
  23. DEFINE_RESPONSE_TABLE1(TMDIChild, TFrameWindow)
  24.   EV_WM_MDIACTIVATE,
  25.   EV_WM_NCACTIVATE,
  26. END_RESPONSE_TABLE;
  27.  
  28. //
  29. // constructor for a TMDIChild
  30. //
  31. TMDIChild::TMDIChild(TMDIClient&     parent,
  32.                      const char far* title,
  33.                      TWindow*        clientWnd,
  34.                      bool            shrinkToClient,
  35.                      TModule*        module)
  36. {
  37.   //
  38.   // Initialize virtual bases, in case the derived-most used default ctor
  39.   //
  40.   TWindow::Init(&parent, title, module);
  41.   TFrameWindow::Init(clientWnd, shrinkToClient);
  42.  
  43.   Attr.Style = WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN |
  44.                WS_SYSMENU | WS_CAPTION | WS_THICKFRAME |
  45.                WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
  46.   Attr.Y = Attr.H = CW_USEDEFAULT;
  47. }
  48.  
  49. //
  50. // Constructor for a TMDIChild which is being used in a DLL as an alias
  51. // for a non-OWL window
  52. //
  53. TMDIChild::TMDIChild(HWND hWnd, TModule*   module)
  54. :
  55.   TFrameWindow(hWnd, module),
  56.   TWindow(hWnd, module)
  57. {
  58.   Attr.Style = WS_CLIPSIBLINGS;
  59. }
  60.  
  61. //
  62. // Don't allow NC Activation if we are only temporarily unhidden for window
  63. // menu maintainance
  64. //
  65. bool
  66. TMDIChild::EvNCActivate(bool active)
  67. {
  68.   return IsFlagSet(wfUnHidden) ? false : TFrameWindow::EvNCActivate(active);
  69. }
  70.  
  71. //
  72. // Perform special processing for showing MDI children to make sure that
  73. // the MDI client maintains the window list correctly
  74. //
  75. bool
  76. TMDIChild::ShowWindow(int cmdShow)
  77. {
  78.   int  retVal = TFrameWindow::ShowWindow(cmdShow);     // 0 if had been hidden
  79.  
  80.   // Process only if visible state has changed
  81.   //
  82.   if ((retVal != 0) != (cmdShow != SW_HIDE)) {
  83.     if ((HWND)Parent->HandleMessage(WM_MDIGETACTIVE) == *this) {
  84.       if (cmdShow == SW_HIDE)
  85.         Parent->HandleMessage(WM_MDINEXT, (uint)(HWND)*this);
  86.       else
  87.         HandleMessage(WM_NCACTIVATE, true); // resend suppressed message
  88.     }
  89. #if defined(BI_PLAT_WIN32)
  90.     Parent->HandleMessage(WM_MDIREFRESHMENU);
  91. #else
  92.     Parent->HandleMessage(WM_MDISETMENU, true);
  93. #endif
  94.   }
  95.   return retVal;
  96. }
  97.  
  98. //
  99. // Perform special processing for enabling MDI children to make sure that
  100. // the MDI client maintains the window list correctly
  101. //
  102. bool
  103. TMDIChild::EnableWindow(bool enable)
  104. {
  105.   int retVal = TFrameWindow::EnableWindow(enable);  // 0 if previously enabled
  106.  
  107.   // Process only if disabled state has actually changed
  108.   //
  109.   if ((retVal!=0) != (enable==0)) {
  110.     if (!enable && (HWND)Parent->HandleMessage(WM_MDIGETACTIVE) == *this)
  111.       Parent->HandleMessage(WM_MDINEXT, (uint)(HWND)*this);
  112. #if defined(BI_PLAT_WIN32)
  113.     Parent->HandleMessage(WM_MDIREFRESHMENU);
  114. #else
  115.     Parent->HandleMessage(WM_MDISETMENU, true);
  116. #endif
  117.   }
  118.   return retVal;
  119. }
  120.  
  121. bool
  122. TMDIChild::PreProcessMsg(MSG& msg)
  123. {
  124.   //
  125.   // if the MDI child has requested keyboard navigation then TFrameWindow's
  126.   // PreProcessMsg() member function will call ::IsDialogMessage() which will
  127.   // eat the event and the MDI client window won't get a chance to do MDI
  128.   // accelerator processing
  129.   //
  130.   // so, we will do it here to make sure it gets done
  131.   //
  132.   if (KeyboardHandling && Parent->PreProcessMsg(msg))
  133.     return true;
  134.  
  135.   if (hAccel && ::TranslateAccelerator(Parent->Parent->HWindow, hAccel, &msg))
  136.     return true;
  137.  
  138.   return TFrameWindow::PreProcessMsg(msg);
  139. }
  140.  
  141. //
  142. // Destroys the MS-Windows element associated with the TMDIChild
  143. //
  144. void
  145. TMDIChild::Destroy(int)
  146. {
  147.   if (HWindow) {
  148.     ForEach(DoEnableAutoCreate);  // use iterator function in window.cpp
  149.  
  150.     if (Parent) { // send destroy message to MDI client window
  151.       Parent->HandleMessage(WM_MDIDESTROY, (WPARAM)HWindow);
  152.       HWindow = 0;  // Assume success
  153.     }
  154.     else {
  155.       if (::DestroyWindow(HWindow))
  156.         HWindow = 0;
  157.       GetApplication()->ResumeThrow();
  158.       WARNX(OwlWin, HWindow, 0, "::DestroyWindow(" << (uint)HWindow << ") failed");
  159.     }
  160.   }
  161. }
  162.  
  163. //
  164. // An MDI Child creates its HWindow by sending an MDI Create packet to the MDI
  165. // client.
  166. //
  167. void
  168. TMDIChild::PerformCreate(int)
  169. {
  170.   PRECONDITION(Parent);
  171.  
  172.   MDICREATESTRUCT  createStruct;
  173.   createStruct.szClass = GetClassName();
  174.   createStruct.szTitle = Title;
  175.   createStruct.hOwner = *GetModule();
  176.   createStruct.x = Attr.X;
  177.   createStruct.y = Attr.Y;
  178.   createStruct.cx = Attr.W;
  179.   createStruct.cy = Attr.H;
  180.   createStruct.style = Attr.Style;
  181.   createStruct.lParam = (LPARAM)Attr.Param;
  182.  
  183.   // Work around a Windows MDI bug w/ bad menus if MDI child is created
  184.   // maximized, by hiding child now & maximizing later
  185.   //
  186. #if defined(MDIMAX_WORKAROUND)
  187.   uint32 origStyle = Attr.Style;
  188.   if (createStruct.style & WS_MAXIMIZE)
  189.     createStruct.style &= ~(WS_MAXIMIZE | WS_VISIBLE);
  190. #endif
  191.  
  192.   HWindow = (HWND)Parent->HandleMessage(WM_MDICREATE, 0, (LPARAM)&createStruct);
  193.  
  194.   // Finish up maximized MDI child workaround
  195.   //
  196. #if defined(MDIMAX_WORKAROUND)
  197.   if (HWindow && (origStyle & WS_MAXIMIZE)) {
  198.     Parent->HandleMessage(WM_MDIMAXIMIZE, WPARAM(HWindow));
  199. #if defined(BI_PLAT_WIN32)
  200.     Parent->HandleMessage(WM_MDIREFRESHMENU);
  201. #else
  202.     Parent->HandleMessage(WM_MDISETMENU, true);
  203. #endif
  204.   }
  205. #endif
  206. }
  207.  
  208. void
  209. TMDIChild::EvMDIActivate(HWND hWndActivated, HWND hWndDeactivated)
  210. {
  211.   if (HWindow == hWndActivated) {
  212.     // A bug in Windows MDI causes the first MDI child to not get a
  213.     // WM_SETFOCUS. Simulate it now
  214.     //
  215.     if (!GetWindow(GW_HWNDNEXT) && GetFocus() != HWindow)
  216.       HandleMessage(WM_SETFOCUS, WPARAM(HWindow));
  217.  
  218.     // Merge this windows menubar with the MDI frame's if there is a
  219.     // MenuDescr assigned
  220.     //
  221.     if (GetMenuDescr()) {
  222.       TFrameWindow* frame = TYPESAFE_DOWNCAST(Parent->Parent,TFrameWindow);
  223.       if (frame)
  224.         frame->MergeMenu(*GetMenuDescr());
  225.     }
  226.   }
  227.   else {
  228.     // Restore the MDI frame's menubar if there is no other MDI child being
  229.     // activated
  230.     //
  231.     if (GetMenuDescr() && !hWndActivated) {
  232.       TFrameWindow* frame = TYPESAFE_DOWNCAST(Parent->Parent,TFrameWindow);
  233.       if (frame)
  234.         frame->RestoreMenu();
  235.     }
  236.   }
  237.  
  238.   // Forward MDI child activation to our client (if we have one) so that it can
  239.   // perform any type of activate/deactivate processing that it needs
  240.   //
  241.   TWindow* w = GetClientWindow();
  242.   if (w && w->IsWindow())
  243. #if defined(BI_PLAT_WIN32)
  244.     w->HandleMessage(WM_MDIACTIVATE, WPARAM(hWndDeactivated), LPARAM(hWndActivated));
  245. #else
  246.     w->HandleMessage(WM_MDIACTIVATE, 0, MAKELPARAM(hWndActivated,hWndDeactivated));
  247. #endif
  248. }
  249.  
  250. //
  251. // override DefWindowProc to use DefMDIChildProc
  252. //
  253. LRESULT
  254. TMDIChild::DefWindowProc(uint msg, WPARAM wParam, LPARAM lParam)
  255. {
  256.   if (IsFlagSet(wfAlias))
  257.     return TWindow::DefWindowProc(msg, wParam, lParam);
  258.  
  259.   return ::DefMDIChildProc(HWindow, msg, wParam, lParam);
  260. }
  261.  
  262. #endif
  263. #if !defined(SECTION) || SECTION == 2
  264.  
  265.  
  266. IMPLEMENT_STREAMABLE2(TMDIChild, TFrameWindow, TWindow);
  267.  
  268. void*
  269. TMDIChild::Streamer::Read(ipstream& is, uint32 /*version*/) const
  270. {
  271.   ReadVirtualBase((TFrameWindow*)GetObject(), is);
  272.   return GetObject();
  273. }
  274.  
  275. //
  276. // writes data of the TMDIChild to the passed opstream
  277. //
  278. void
  279. TMDIChild::Streamer::Write(opstream& os) const
  280. {
  281.   WriteVirtualBase((TFrameWindow*)GetObject(), os);
  282. }
  283.  
  284. #endif
  285.