home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 11.ddi / OWLSRC.PAK / VBXCTL.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  6.0 KB  |  233 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1993 by Borland International
  3. //   source\owl\vbxctl.cpp
  4. //   Implementation of class TVbxControl.
  5. //----------------------------------------------------------------------------
  6. #include "owl\owlpch.h"
  7. #include "owl\vbxctl.h"
  8.  
  9. //----------------------------------------------------------------------------
  10.  
  11. const char BIVbxDllName[] = "bivbx10.dll";
  12. const char BIVbxClassPrefix[] = "THUNDER";
  13.  
  14.  
  15. TBIVbxLibrary::TBIVbxLibrary()
  16.   : TModule(BIVbxDllName, TRUE)
  17. {
  18.   ::VBXInit(_hInstance, BIVbxClassPrefix);
  19. }
  20.  
  21. TBIVbxLibrary::~TBIVbxLibrary()
  22. {
  23.   ::VBXTerm();
  24. }
  25.    
  26. //----------------------------------------------------------------------------
  27.  
  28. DEFINE_RESPONSE_TABLE2(TVbxControl, TControl, TVbxEventHandler)
  29.   EV_MESSAGE(WM_VSCROLL,EvDefaultProcessing),
  30.   EV_MESSAGE(WM_HSCROLL,EvDefaultProcessing),
  31. END_RESPONSE_TABLE;
  32.  
  33. IMPLEMENT_CASTABLE(TVbxControl);
  34.  
  35. //
  36. // constructor for a TVbxControl object
  37. //
  38. TVbxControl::TVbxControl(TWindow*        parent,
  39.                          int             id,
  40.                          const char far* vbxName,
  41.                          const char far* vbxClass,
  42.                          const char far* title,
  43.                          int             x,
  44.                          int             y,
  45.                          int             w,
  46.                          int             h,
  47.                          long            initLen,
  48.                          void far*       initData,
  49.                          TModule*        module)
  50.   : TControl(parent, id, title, x, y, w, h, module)
  51. {
  52.   VbxName = strnewdup(vbxName);
  53.   VbxClass = strnewdup(vbxClass);
  54.   HCtl = 0;
  55.   InitLen = initLen;
  56.   InitData = initData;
  57.   Attr.Style = 0;
  58. }
  59.  
  60. void
  61. TVbxControl::SetupWindow()
  62. {
  63.   TControl::SetupWindow();
  64.   //If we haven't bound to HCtl yet, do it here
  65.   if (!HCtl)
  66.     HCtl = ::VBXGetHctl(HWindow);
  67. }
  68.  
  69. //
  70. // constructor for a TVbxControl to be associated with a MS-Windows
  71. // interface element created by MS-Windows from a resource definition
  72. //
  73. TVbxControl::TVbxControl(TWindow*   parent,
  74.                          int        resourceId,
  75.                          TModule*   module)
  76.   : TControl(parent, resourceId, module)
  77. {
  78.   VbxName = 0;
  79.   VbxClass = 0;
  80.   HCtl = 0;
  81.   InitLen = 0;
  82.   InitData = 0;
  83. }
  84.  
  85. TVbxControl::~TVbxControl()
  86. {
  87.   delete VbxName;
  88.   delete VbxClass;
  89. }
  90.  
  91. //
  92. // Return name of VBX window class
  93. //
  94. char far*
  95. TVbxControl::GetClassName()
  96. {
  97.   return "VBCONTROL";
  98. }
  99.  
  100. //
  101. // Perform MS Windows window creation
  102. //
  103. void
  104. TVbxControl::PerformCreate(int menuOrId)
  105. {
  106.   HFORMFILE formFile = 0;
  107.  
  108.   if (InitData)
  109.     formFile = ::VBXCreateFormFile(InitLen,InitData);
  110.  
  111.   HCtl = ::VBXCreate(Parent->HWindow, menuOrId, VbxName, VbxClass, Title,
  112.                      Attr.Style, Attr.X, Attr.Y, Attr.W, Attr.H, formFile);
  113.   if (formFile)
  114.     ::VBXDeleteFormFile(formFile);
  115.  
  116.   HWindow = ::VBXGetHwnd(HCtl);
  117. }
  118.  
  119. //
  120. // Get a string property
  121. //
  122. BOOL
  123. TVbxControl::GetProp(int prop, string& value, int /*index*/)
  124. {
  125.   long strHandle;
  126.   if (!VBXGetProp(HCtl, prop, &strHandle))
  127.     return FALSE;
  128.  
  129.   LPSTR str = ::VBXGetCStringPtr(HSZ(strHandle));
  130.   value = str;
  131.   ::VBXDestroyCString(HSZ(strHandle));
  132.   return TRUE;
  133. }
  134.  
  135. //
  136. // Get a VBX property
  137. //
  138. BOOL
  139. TVbxControl::GetVBXProperty(int propIndex, void far* value, int arrayIndex)
  140. {
  141.   if (arrayIndex == -1)
  142.     return ::VBXGetProp(HCtl, propIndex, value);
  143.  
  144.   ELEMENTSTRUCT elem;
  145.   elem.Value            = 0;
  146.   elem.NumElems         = 1;
  147.   elem.Element[0].Type  = PTYPE_SHORT;
  148.   elem.Element[0].Index = arrayIndex;
  149.   return ::VBXGetProp(HCtl, propIndex, &elem);
  150. }
  151.  
  152. //
  153. // Set a VBX property
  154. //
  155. BOOL
  156. TVbxControl::SetVBXProperty(int propIndex, LONG value, int arrayIndex)
  157. {
  158.   if (arrayIndex == -1)
  159.     return ::VBXSetProp(HCtl, propIndex, value);
  160.  
  161.   ELEMENTSTRUCT elem;
  162.   elem.Value            = value;
  163.   elem.NumElems         = 1;
  164.   elem.Element[0].Type  = PTYPE_SHORT;
  165.   elem.Element[0].Index = arrayIndex;
  166.   return ::VBXSetProp(HCtl, propIndex, long(&elem));
  167. }
  168.  
  169.  
  170. //----------------------------------------------------------------------------
  171.  
  172. DEFINE_RESPONSE_TABLE(TVbxEventHandler)
  173.   EV_MESSAGE(WM_VBXFIREEVENT, EvVbxDispatch),
  174. END_RESPONSE_TABLE;
  175.  
  176. class TVbxEventInfo : public TEventHandler::TEventInfo {
  177.   public:
  178.     TVbxEventInfo(LPCSTR eventName, UINT msg, UINT id = 0)
  179.       : TEventHandler::TEventInfo(msg, id), EventName(eventName) {}
  180.  
  181.     LPCSTR EventName;
  182. };
  183.  
  184. //
  185. // Compare a response table entry to an eventinfo struct, looking for a
  186. // string version of an entry.
  187. // String is in dispatcher field of the entry, & the Entry field of the info.
  188. // If found, replace with i_LPARAM_Dispatch and insert the info's msg into
  189. // the msg field.
  190. //
  191. static BOOL
  192. VbxEqualOperator(TGenericTableEntry __RTFAR& entry,
  193.                  TEventHandler::TEventInfo& info)
  194. {
  195.   if (entry.Msg == WM_VBXNAME && entry.Id == info.Id &&
  196.       strcmpi((LPCSTR)(*(TVbxEventInfo*)&info).EventName,
  197.               (LPCSTR)entry.Dispatcher) == 0) {
  198.      entry.Msg = info.Msg;
  199.      entry.Dispatcher = (TAnyDispatcher)::i_LPARAM_Dispatch;
  200.      return TRUE;
  201.    }
  202.    return FALSE;
  203. }
  204.  
  205. //
  206. // Handle a VBX fire event message by forwarding to control and/or 
  207. // sub-dispatching to specific event handlers.
  208. //
  209. LRESULT
  210. TVbxEventHandler::EvVbxDispatch(WPARAM wp, LPARAM lp)
  211. {
  212.   VBXEVENT far* e = (VBXEVENT far*)lp;
  213.   TVbxEventInfo eventInfo(e->EventName, WM_VBXBASE + e->EventIndex,
  214.                           ::GetDlgCtrlID(e->Window));
  215.  
  216.   //
  217.   // If the control is not us, then send the fire event message to it to give
  218.   // it first crack.
  219.   //
  220.   TWindow* ctl = GetWindowPtr(e->Window);
  221.   if (ctl && 
  222.     STATIC_CAST(TEventHandler*, ctl) != STATIC_CAST(TEventHandler*, this))
  223.     if (ctl->HandleMessage(WM_VBXFIREEVENT, wp, lp))
  224.       return 1;
  225.  
  226.   //
  227.   // See if we have a handler for this event
  228.   //
  229.   if (Find(eventInfo) || Find(eventInfo, VbxEqualOperator))
  230.     return Dispatch(eventInfo, wp, lp);
  231.   return 0;
  232. }
  233.