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

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1993, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Implementation of class TVbxControl.
  6. //----------------------------------------------------------------------------
  7. #include <owl/owlpch.h>
  8. #include <owl/vbxctl.h>
  9.  
  10. //----------------------------------------------------------------------------
  11.  
  12. const char BIVbxDllName[] = "bivbx11.dll";
  13. const char BIVbxClassPrefix[] = "THUNDER";
  14.  
  15.  
  16. TBIVbxLibrary::TBIVbxLibrary()
  17. :
  18. #if defined(BI_PLAT_WIN32)
  19.   TModule(BIVbxDllName, false)
  20. #else
  21.   TModule(BIVbxDllName, true)
  22. #endif
  23. {
  24.   if (!::VBXInit(_hInstance, BIVbxClassPrefix))
  25.     THROW(TXVbxLibrary());
  26. }
  27.  
  28. TBIVbxLibrary::~TBIVbxLibrary()
  29. {
  30.   ::VBXTerm();
  31. }
  32.  
  33. //----------------------------------------------------------------------------
  34. //
  35. // Exception class
  36. //
  37.  
  38. TBIVbxLibrary::TXVbxLibrary::TXVbxLibrary() : TXOwl(IDS_VBXLIBRARYFAIL)
  39. {
  40. }
  41.  
  42. TXOwl*
  43. TBIVbxLibrary::TXVbxLibrary::Clone()
  44. {
  45.   return new TXVbxLibrary(*this);
  46. }
  47.  
  48. void
  49. TBIVbxLibrary::TXVbxLibrary::Throw()
  50. {
  51.   THROW( *this );
  52. }
  53.  
  54. //----------------------------------------------------------------------------
  55.  
  56. DEFINE_RESPONSE_TABLE2(TVbxControl, TControl, TVbxEventHandler)
  57.   EV_MESSAGE(WM_VSCROLL,EvDefaultProcessing),
  58.   EV_MESSAGE(WM_HSCROLL,EvDefaultProcessing),
  59.   EV_MESSAGE(WM_COMPAREITEM,EvDefaultProcessing),
  60.   EV_MESSAGE(WM_DELETEITEM,EvDefaultProcessing),
  61.   EV_MESSAGE(WM_DRAWITEM,EvDefaultProcessing),
  62.   EV_MESSAGE(WM_MEASUREITEM,EvDefaultProcessing),
  63. END_RESPONSE_TABLE;
  64.  
  65. IMPLEMENT_CASTABLE(TVbxControl);
  66.  
  67. //
  68. // constructor for a TVbxControl object
  69. //
  70. TVbxControl::TVbxControl(TWindow*        parent,
  71.                          int             id,
  72.                          const char far* vbxName,
  73.                          const char far* vbxClass,
  74.                          const char far* title,
  75.                          int             x,
  76.                          int             y,
  77.                          int             w,
  78.                          int             h,
  79.                          long            initLen,
  80.                          void far*       initData,
  81.                          TModule*        module)
  82. :
  83.   TControl(parent, id, title, x, y, w, h, module)
  84. {
  85.   VbxName = strnewdup(vbxName);
  86.   VbxClass = strnewdup(vbxClass);
  87.   HCtl = 0;
  88.   InitLen = initLen;
  89.   InitData = initData;
  90.   Attr.Style = 0;
  91. }
  92.  
  93. void
  94. TVbxControl::SetupWindow()
  95. {
  96.   TControl::SetupWindow();
  97.  
  98.   // If we haven't bound to HCtl yet, do it here
  99.   //
  100.   if (!HCtl)
  101.     HCtl = ::VBXGetHctl(HWindow);
  102. }
  103.  
  104. //
  105. // constructor for a TVbxControl to be associated with a MS-Windows
  106. // interface element created by MS-Windows from a resource definition
  107. //
  108. TVbxControl::TVbxControl(TWindow*   parent,
  109.                          int        resourceId,
  110.                          TModule*   module)
  111.   : TControl(parent, resourceId, module)
  112. {
  113.   VbxName = 0;
  114.   VbxClass = 0;
  115.   HCtl = 0;
  116.   InitLen = 0;
  117.   InitData = 0;
  118. }
  119.  
  120. TVbxControl::~TVbxControl()
  121. {
  122.   delete VbxName;
  123.   delete VbxClass;
  124. }
  125.  
  126. LRESULT TVbxControl::EvDefaultProcessing(WPARAM, LPARAM)
  127. {
  128.   return DefaultProcessing();
  129. }
  130.  
  131. //
  132. // Return name of VBX window class
  133. //
  134. char far*
  135. TVbxControl::GetClassName()
  136. {
  137.   return "VBCONTROL";
  138. }
  139.  
  140. //
  141. // Perform MS Windows window creation
  142. //
  143. void
  144. TVbxControl::PerformCreate(int menuOrId)
  145. {
  146.   HFORMFILE formFile = 0;
  147.  
  148.   if (InitData)
  149.     formFile = ::VBXCreateFormFile(InitLen,InitData);
  150.  
  151.   HCtl = ::VBXCreate(Parent->HWindow, menuOrId, VbxName, VbxClass, Title,
  152.                      Attr.Style, Attr.X, Attr.Y, Attr.W, Attr.H, formFile);
  153.   if (formFile)
  154.     ::VBXDeleteFormFile(formFile);
  155.  
  156.   HWindow = ::VBXGetHwnd(HCtl);
  157. }
  158.  
  159. //
  160. // Get a string property
  161. //
  162. bool
  163. TVbxControl::GetProp(int prop, string& value, int /*index*/)
  164. {
  165. #if defined(BI_PLAT_WIN32)
  166.   uint32 strHandle;
  167.   if (!VBXGetProp(HCtl, prop, &strHandle))
  168.     return false;
  169.  
  170.   char buf[256];
  171.   if (!::VBXGetCStringBuf(HSZ(strHandle), buf, sizeof(buf)))
  172.     return false;
  173.   value = buf;
  174.   ::VBXDestroyCString(HSZ(strHandle));
  175.   return true;
  176. #else
  177.   uint32 strHandle;
  178.   if (!VBXGetProp(HCtl, prop, &strHandle))
  179.     return false;
  180.  
  181.   char far* str = ::VBXGetCStringPtr(HSZ(strHandle));
  182.   value = str;
  183.   ::VBXDestroyCString(HSZ(strHandle));
  184.   return true;
  185. #endif
  186. }
  187.  
  188. //
  189. // Get a VBX property
  190. //
  191. bool
  192. TVbxControl::GetVBXProperty(int propIndex, void far* value, int arrayIndex)
  193. {
  194.   if (arrayIndex == -1)
  195.     return ::VBXGetProp(HCtl, propIndex, value);
  196.   else
  197.     return ::VBXGetArrayProp(HCtl, propIndex, arrayIndex, value);
  198. }
  199.  
  200. //
  201. // Set a VBX property
  202. //
  203. bool
  204. TVbxControl::SetVBXProperty(int propIndex, int32 value, int arrayIndex)
  205. {
  206.   if (arrayIndex == -1)
  207.     return ::VBXSetProp(HCtl, propIndex, value);
  208.   else
  209.     return ::VBXSetArrayProp(HCtl, propIndex, arrayIndex, value);
  210. }
  211.  
  212.  
  213. //----------------------------------------------------------------------------
  214.  
  215. DEFINE_RESPONSE_TABLE(TVbxEventHandler)
  216.   EV_MESSAGE(WM_VBXFIREEVENT, EvVbxDispatch),
  217.   EV_MESSAGE(WM_VBXINITFORM, EvVbxInitForm),
  218. END_RESPONSE_TABLE;
  219.  
  220. class TVbxEventInfo : public TEventHandler::TEventInfo {
  221.   public:
  222.     TVbxEventInfo(const char far* eventName, uint msg, uint id = 0)
  223.       : TEventHandler::TEventInfo(msg, id), EventName(eventName) {}
  224.  
  225.     const char far* EventName;
  226. };
  227.  
  228. //
  229. // Compare a response table entry to an eventinfo struct, looking for a
  230. // string version of an entry.
  231. // String is in dispatcher field of the entry, & the Entry field of the info.
  232. // If found, replace with i_LPARAM_Dispatch and insert the info's msg into
  233. // the msg field.
  234. //
  235. static bool
  236. VbxEqualOperator(TGenericTableEntry __RTFAR& entry,
  237.                  TEventHandler::TEventInfo& info)
  238. {
  239.   if (entry.Msg == WM_VBXNAME && entry.Id == info.Id &&
  240.       strcmpi((const char far*)(*(TVbxEventInfo*)&info).EventName,
  241.               (const char far*)entry.Dispatcher) == 0) {
  242.      entry.Msg = info.Msg;
  243.      entry.Dispatcher = (TAnyDispatcher)::i_LPARAM_Dispatch;
  244.      return true;
  245.    }
  246.    return false;
  247. }
  248.  
  249. //
  250. // Handle a VBX fire event message by forwarding to control and/or
  251. // sub-dispatching to specific event handlers.
  252. //
  253. LRESULT
  254. TVbxEventHandler::EvVbxDispatch(WPARAM wp, LPARAM lp)
  255. {
  256.   VBXEVENT far* e = (VBXEVENT far*)lp;
  257.   TVbxEventInfo eventInfo(e->EventName, WM_VBXBASE + e->EventIndex,
  258.                           ::GetDlgCtrlID(e->Window));
  259.  
  260.   //
  261.   // If the control is not us, then send the fire event message to it to give
  262.   // it first crack.
  263.   //
  264.   TWindow* ctl = GetWindowPtr(e->Window);
  265.   if (ctl &&
  266.     STATIC_CAST(TEventHandler*, ctl) != STATIC_CAST(TEventHandler*, this))
  267.     if (ctl->HandleMessage(WM_VBXFIREEVENT, wp, lp))
  268.       return 1;
  269.  
  270.   //
  271.   // See if we have a handler for this event
  272.   //
  273.   if (Find(eventInfo) || Find(eventInfo, VbxEqualOperator))
  274.     return Dispatch(eventInfo, wp, lp);
  275.   return 0;
  276. }
  277.  
  278. LRESULT
  279. TVbxEventHandler::EvVbxInitForm(WPARAM wp, LPARAM lp)
  280. {
  281.   HWND hWnd = HWND(wp);
  282. #if defined(BI_PLAT_WIN32)
  283.   HINSTANCE hInst = (HINSTANCE)::GetWindowLong(hWnd, GWL_HINSTANCE);
  284. #else
  285.   HINSTANCE hInst = (HINSTANCE)::GetWindowWord(hWnd, GWW_HINSTANCE);
  286. #endif
  287.   return ::VBXInitDialog(hWnd, hInst, (char far*)lp) ? 1 : -1;
  288. }
  289.