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

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1991, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Implementation of class TMDIFrame.  This defines the basic behavior of
  6. //   all MDI frame windows.
  7. //----------------------------------------------------------------------------
  8. #pragma hdrignore SECTION
  9. #include <owl/owlpch.h>
  10. #include <owl/mdi.h>
  11. #include <owl/applicat.h>
  12.  
  13. #if !defined(SECTION) || SECTION == 1
  14.  
  15. DEFINE_RESPONSE_TABLE1(TMDIFrame, TFrameWindow)
  16. END_RESPONSE_TABLE;
  17.  
  18. //
  19. // constructor for a TMDIFrame
  20. //
  21. TMDIFrame::TMDIFrame(const char far* title,
  22.                      TResId          menuResId,
  23.                      TMDIClient&     clientWnd,
  24.                      TModule*        module)
  25. {
  26.   //
  27.   // Initialize virtual bases, in case the derived-most used default ctor
  28.   //
  29.   TWindow::Init(0, title, module);
  30.   TFrameWindow::Init(&clientWnd, false);
  31.  
  32.   if (menuResId)
  33.     AssignMenu(menuResId);
  34. }
  35.  
  36. //
  37. // constructor for a TMDIFrame which is being used as an alias for a
  38. // non-OWL window
  39. //
  40. TMDIFrame::TMDIFrame(HWND       hWnd,
  41.                      HWND       clientHWnd,
  42.                      TModule*   module)
  43. :
  44.   TFrameWindow(hWnd, module),
  45.   TWindow(hWnd, module)
  46. {
  47.   CHECK(::GetParent(clientHWnd) == hWnd);
  48.  
  49.   //
  50.   // NOTE: Attr.Menu set in TWindow's constructor
  51.   //
  52.   ClientWnd = new TMDIClient(clientHWnd);
  53.   ClientWnd->Parent = this;
  54. }
  55.  
  56. //
  57. //  an MDI frame must have a menu.  Give it an empty one if none supplied.
  58. //
  59. void
  60. TMDIFrame::PerformCreate(int menuOrId)
  61. {
  62.   TFrameWindow::PerformCreate(menuOrId ? menuOrId : (int)::CreateMenu());
  63. }
  64.  
  65. //
  66. // look for the MDI submenu in a menubar by looking for the normal
  67. // MDI commands, and return pos if found. Scan from right to
  68. // left since the Window menu is usually near the right.
  69. //
  70. HMENU
  71. TMDIFrame::FindChildMenu(HMENU menu)
  72. {
  73.   if (menu) {
  74.     int   numItems = ::GetMenuItemCount(menu);
  75.     for (int i = numItems-1; i >= 0; i--) {
  76.       HMENU childMenu = ::GetSubMenu(menu, i);
  77.       if (childMenu &&
  78.           (::GetMenuState(childMenu, CM_CASCADECHILDREN, MF_BYCOMMAND) != (uint)-1 ||
  79.            ::GetMenuState(childMenu, CM_TILECHILDREN, MF_BYCOMMAND) != (uint)-1 ||
  80.            ::GetMenuState(childMenu, CM_ARRANGEICONS, MF_BYCOMMAND) != (uint)-1)) {
  81.         return childMenu;
  82.       }
  83.     }
  84.   }
  85.   return 0;
  86. }
  87.  
  88. //
  89. // MDI specific version of SetMenu uses WM_MDISETMENU to set a new
  90. // menu bar and childMenu within it.
  91. //
  92. bool
  93. TMDIFrame::SetMenu(HMENU newMenu)
  94. {
  95.   PRECONDITION(newMenu);
  96.  
  97.   if (IsFlagSet(wfMainWindow))
  98.     GetApplication()->PreProcessMenu(newMenu);
  99.  
  100.   if (HWindow) {
  101.     HMENU childMenu = FindChildMenu(newMenu);
  102.     #if defined(BI_PLAT_WIN32)
  103.       HMENU oldMenu = (HMENU)ClientWnd->HandleMessage(WM_MDISETMENU,
  104.                                                       (WPARAM)newMenu,
  105.                                                       (LPARAM)childMenu);
  106.     #else
  107.       HMENU oldMenu = (HMENU)ClientWnd->HandleMessage(WM_MDISETMENU,
  108.                                                       false,
  109.                                                       MAKELPARAM(newMenu, childMenu));
  110.     #endif
  111.     DrawMenuBar();
  112.     if (!oldMenu)
  113.       return false;
  114.   }
  115.   return true;
  116. }
  117.  
  118. TMDIClient*
  119. TMDIFrame::GetClientWindow()
  120. {
  121.   return TYPESAFE_DOWNCAST(ClientWnd,TMDIClient);
  122. }
  123.  
  124. //
  125. // Locate and return the child window that is the target of command and command
  126. // enable messages. Pass this to our active mdi child, if any, to let it
  127. // locate its active command target.
  128. //
  129. HWND
  130. TMDIFrame::GetCommandTarget()
  131. {
  132.   TFrameWindow* mdiChild = GetClientWindow()->GetActiveMDIChild();
  133.  
  134.   return mdiChild ? mdiChild->GetCommandTarget() : TFrameWindow::GetCommandTarget();
  135. }
  136.  
  137. //
  138. // override TWindow method and call ::DefFrameProc() instead of
  139. // ::DefWindowProc()
  140. //
  141. LRESULT
  142. TMDIFrame::DefWindowProc(uint message, WPARAM wParam, LPARAM lParam)
  143. {
  144.   return ::DefFrameProc(HWindow, ClientWnd ? ClientWnd->HWindow : 0,
  145.                         message, wParam, lParam);
  146. }
  147.  
  148. #endif
  149. #if !defined(SECTION) || SECTION == 2
  150.  
  151. IMPLEMENT_STREAMABLE2(TMDIFrame, TFrameWindow, TWindow);
  152.  
  153. //
  154. // reads an instance of TMDIFrame from the passed ipstream
  155. //
  156. void*
  157. TMDIFrame::Streamer::Read(ipstream& is, uint32 /*version*/) const
  158. {
  159.   ReadVirtualBase((TFrameWindow*)GetObject(), is);
  160.   GetObject()->AssignMenu(GetObject()->Attr.Menu);
  161.   return GetObject();
  162. }
  163.  
  164. //
  165. // writes the TMDIFrame to the passed opstream
  166. //
  167. void
  168. TMDIFrame::Streamer::Write(opstream& os) const
  169. {
  170.   WriteVirtualBase((TFrameWindow*)GetObject(), os);
  171. }
  172.  
  173. #endif
  174.