home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / BORLAND TURBO / OWLSRC.PAK / VBXCTL.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  8.4 KB  |  375 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1993, 1997 by Borland International, All Rights Reserved
  4. //
  5. // $Revision:   10.12  $
  6. //
  7. // Implementation of classes TVbxControl and TBIVbxLibrary.
  8. //----------------------------------------------------------------------------
  9. #include <owl/pch.h>
  10. #if !defined(OWL_VBXCTL_H)
  11. # include <owl/vbxctl.h>
  12. #endif
  13.  
  14. OWL_DIAGINFO;
  15.  
  16. //----------------------------------------------------------------------------
  17.  
  18. const char BIVbxClassPrefix[] = "THUNDER";
  19.  
  20. //
  21. //
  22. //
  23. TBIVbxLibrary::TBIVbxLibrary()
  24. {
  25.   // Initialize the VBX support
  26.   //
  27.   if (!::VBXInit(_hInstance, BIVbxClassPrefix))
  28.     TXVbxLibrary::Raise();
  29. }
  30.  
  31. //
  32. //
  33. //
  34. TBIVbxLibrary::~TBIVbxLibrary()
  35. {
  36.   ::VBXTerm();
  37. }
  38.  
  39. //----------------------------------------------------------------------------
  40.  
  41. //
  42. // Exception class for VBX errors other than initial DLL load
  43. //
  44.  
  45. //
  46. //
  47. //
  48. TXVbxLibrary::TXVbxLibrary()
  49. :
  50.   TXOwl(IDS_VBXLIBRARYFAIL)
  51. {
  52. }
  53.  
  54. #if defined(BI_NO_COVAR_RET)
  55. TXBase*
  56. #else
  57. TXVbxLibrary*
  58. #endif
  59. TXVbxLibrary::Clone()
  60. {
  61.   return new TXVbxLibrary(*this);
  62. }
  63.  
  64. //
  65. //
  66. //
  67. void
  68. TXVbxLibrary::Throw()
  69. {
  70.   THROW( *this );
  71. }
  72.  
  73. //
  74. //
  75. //
  76. void
  77. TXVbxLibrary::Raise()
  78. {
  79.   TXVbxLibrary().Throw();
  80. }
  81.  
  82. //----------------------------------------------------------------------------
  83.  
  84. DEFINE_RESPONSE_TABLE2(TVbxControl, TControl, TVbxEventHandler)
  85.   EV_MESSAGE(WM_VSCROLL,EvDefaultProcessing),
  86.   EV_MESSAGE(WM_HSCROLL,EvDefaultProcessing),
  87.   EV_MESSAGE(WM_COMPAREITEM,EvDefaultProcessing),
  88.   EV_MESSAGE(WM_DELETEITEM,EvDefaultProcessing),
  89.   EV_MESSAGE(WM_DRAWITEM,EvDefaultProcessing),
  90.   EV_MESSAGE(WM_MEASUREITEM,EvDefaultProcessing),
  91. END_RESPONSE_TABLE;
  92.  
  93. IMPLEMENT_CASTABLE(TVbxControl);
  94.  
  95. //
  96. // Constructor for a TVbxControl object
  97. //
  98. TVbxControl::TVbxControl(TWindow*        parent,
  99.                          int             id,
  100.                          const char far* vbxName,
  101.                          const char far* vbxClass,
  102.                          const char far* title,
  103.                          int             x,
  104.                          int             y,
  105.                          int             w,
  106.                          int             h,
  107.                          long            initLen,
  108.                          void far*       initData,
  109.                          TModule*        module)
  110. :
  111.   TControl(parent, id, title, x, y, w, h, module)
  112. {
  113.   VbxName = strnewdup(vbxName);
  114.   VbxClass = strnewdup(vbxClass);
  115.   HCtl = 0;
  116.   InitLen = initLen;
  117.   InitData = initData;
  118.   Attr.Style = 0;
  119. }
  120.  
  121. //
  122. // Constructor for a TVbxControl to be associated with a MS-Windows
  123. // interface element created by MS-Windows from a resource definition
  124. //
  125. TVbxControl::TVbxControl(TWindow*   parent,
  126.                          int        resourceId,
  127.                          TModule*   module)
  128. :
  129.   TControl(parent, resourceId, module)
  130. {
  131.   VbxName = 0;
  132.   VbxClass = 0;
  133.   HCtl = 0;
  134.   InitLen = 0;
  135.   InitData = 0;
  136. }
  137.  
  138. //
  139. //
  140. //
  141. TVbxControl::~TVbxControl()
  142. {
  143.   delete[] VbxName;
  144.   delete[] VbxClass;
  145. }
  146.  
  147. //
  148. //
  149. //
  150. void
  151. TVbxControl::SetupWindow()
  152. {
  153.   TControl::SetupWindow();
  154.  
  155.   // If we haven't bound to HCtl yet, do it here
  156.   //
  157.   if (!HCtl)
  158.     HCtl = ::VBXGetHctl(GetHandle());
  159. }
  160.  
  161. //
  162. //
  163. //
  164. TResult
  165. TVbxControl::EvDefaultProcessing(TParam1, TParam2)
  166. {
  167.   return DefaultProcessing();
  168. }
  169.  
  170. //
  171. // Return name of VBX window class
  172. //
  173. char far*
  174. TVbxControl::GetClassName()
  175. {
  176.   return "VBCONTROL";
  177. }
  178.  
  179. //
  180. // Perform MS Windows window creation
  181. //
  182. void
  183. TVbxControl::PerformCreate(int menuOrId)
  184. {
  185.   HFORMFILE formFile = 0;
  186.  
  187.   if (InitData)
  188.     formFile = ::VBXCreateFormFile(InitLen,InitData);
  189.  
  190.   HCtl = ::VBXCreate(Parent->GetHandle(), menuOrId, VbxName, VbxClass, Title,
  191.                      Attr.Style, Attr.X, Attr.Y, Attr.W, Attr.H, formFile);
  192.   if (formFile)
  193.     ::VBXDeleteFormFile(formFile);
  194.  
  195.   SetHandle(::VBXGetHwnd(HCtl));
  196. }
  197.  
  198. //
  199. // Get a string property
  200. //
  201. bool
  202. TVbxControl::GetProp(int prop, string& value, int /*index*/)
  203. {
  204. #if defined(BI_PLAT_WIN32)
  205.   uint32 strHandle;
  206.   if (!VBXGetProp(HCtl, prop, &strHandle))
  207.     return false;
  208.  
  209.   char buf[256];
  210.   *buf = 0xff;
  211.   if (!::VBXGetCStringBuf(HSZ(strHandle), buf, sizeof(buf))
  212.    && *buf == char(0xff))
  213.     return false;
  214.   value = buf;
  215.   ::VBXDestroyCString(HSZ(strHandle));
  216.   return true;
  217. #else
  218.   uint32 strHandle;
  219.   if (!VBXGetProp(HCtl, prop, &strHandle))
  220.     return false;
  221.  
  222.   char far* str = ::VBXGetCStringPtr(HSZ(strHandle));
  223.   value = str;
  224.   ::VBXDestroyCString(HSZ(strHandle));
  225.   return true;
  226. #endif
  227. }
  228.  
  229. //
  230. // Get a VBX property
  231. //
  232. bool
  233. TVbxControl::GetVBXProperty(int propIndex, void far* value, int arrayIndex)
  234. {
  235.   if (arrayIndex == -1)
  236.     return ::VBXGetProp(HCtl, propIndex, value);
  237.   else
  238.     return ::VBXGetArrayProp(HCtl, propIndex, arrayIndex, value);
  239. }
  240.  
  241. //
  242. // Set a VBX property
  243. //
  244. bool
  245. TVbxControl::SetVBXProperty(int propIndex, int32 value, int arrayIndex)
  246. {
  247.   if (arrayIndex == -1)
  248.     return ::VBXSetProp(HCtl, propIndex, value);
  249.   else
  250.     return ::VBXSetArrayProp(HCtl, propIndex, arrayIndex, value);
  251. }
  252.  
  253.  
  254. //----------------------------------------------------------------------------
  255.  
  256. DEFINE_RESPONSE_TABLE(TVbxEventHandler)
  257.   EV_MESSAGE(WM_VBXFIREEVENT, EvVbxDispatch),
  258.   EV_MESSAGE(WM_VBXINITFORM, EvVbxInitForm),
  259. END_RESPONSE_TABLE;
  260.  
  261. class TVbxEventInfo : public TEventHandler::TEventInfo {
  262.   public:
  263.     TVbxEventInfo(const char far* eventName, uint msg, uint id = 0)
  264.       : TEventHandler::TEventInfo(msg, id), EventName(eventName) {}
  265.  
  266.     const char far* EventName;
  267. };
  268.  
  269. //
  270. // Compare a response table entry to an eventinfo struct, looking for a
  271. // string version of an entry.
  272. // String is in dispatcher field of the entry, & the Entry field of the info.
  273. // If found, replace with i_LPARAM_Dispatch and insert the info's msg into
  274. // the msg field.
  275. //
  276. static bool
  277. VbxEqualOperator(TGenericTableEntry __RTFAR& entry,
  278.                  TEventHandler::TEventInfo& info)
  279. {
  280.   if (entry.Msg == WM_VBXNAME && entry.Id == info.Id &&
  281.       strcmpi((const char far*)(*(TVbxEventInfo*)&info).EventName,
  282.               (const char far*)entry.Dispatcher) == 0) {
  283.      entry.Msg = info.Msg;
  284.      entry.Dispatcher = (TAnyDispatcher)::i_LPARAM_Dispatch;
  285.      return true;
  286.    }
  287.    return false;
  288. }
  289.  
  290. #if defined(BI_PLAT_WIN32)
  291. //
  292. // Find which VBX sent this event.
  293. //
  294. void
  295. TVbxEventHandler::FindVBX(TWindow *c, LPPARAM p)
  296. {
  297.   if(!p->vbx) {
  298.     TVbxControl *vbx = TYPESAFE_DOWNCAST(c, TVbxControl);
  299.     if(vbx && HWND16TO32(vbx->GetHandle()) == p->hWnd)
  300.       p->vbx = vbx;
  301.   }
  302. }
  303.  
  304. //
  305. // This function determines which environment the
  306. // application is running under, NT or other versions of Windows, and
  307. // get the object pointer accordingly.
  308. //
  309. TWindow*
  310. TVbxEventHandler::GetWindowPtr( HWND Window )
  311. {
  312.   if (!TSystem::IsNT()) {
  313. #if defined(BI_NAMESPACE)
  314.     return OWL::GetWindowPtr(Window);
  315. #else
  316.     return ::GetWindowPtr(Window);
  317. #endif
  318.   }
  319.  
  320.   TWindow *Dlg = TYPESAFE_DOWNCAST(this, TWindow);
  321.  
  322.   if(Dlg)  {
  323.     PARAM p = {Window};
  324.     Dlg->ForEach(LPFNFOREACH(FindVBX), &p);
  325.     return p.vbx;
  326.   }
  327.  
  328.   return NULL;
  329. #pragma warn -aus
  330. }
  331. #pragma warn .aus
  332. #endif BI_PLAT_WIN32
  333.  
  334. //
  335. // Handle a VBX fire event message by forwarding to control and/or
  336. // sub-dispatching to specific event handlers.
  337. //
  338. TResult
  339. TVbxEventHandler::EvVbxDispatch(TParam1 p1, TParam2 p2)
  340. {
  341.   VBXEVENT far* e = (VBXEVENT far*)p2;
  342.   TVbxEventInfo eventInfo(e->EventName, WM_VBXBASE + e->EventIndex,
  343.                           ::GetDlgCtrlID(e->Window));
  344.  
  345.   // If the control is not us, then send the fire event message to it to give
  346.   // it first crack.
  347.   //
  348.   TWindow* ctl = GetWindowPtr( e->Window );
  349.   if (ctl &&
  350.     STATIC_CAST(TEventHandler*, ctl) != STATIC_CAST(TEventHandler*, this))
  351.     if (ctl->HandleMessage(WM_VBXFIREEVENT, p1, p2))
  352.       return 1;
  353.  
  354.   // See if we have a handler for this event
  355.   //
  356.   if (Find(eventInfo) || Find(eventInfo, VbxEqualOperator))
  357.     return Dispatch(eventInfo, p1, p2);
  358.   return 0;
  359. }
  360.  
  361. //
  362. //
  363. //
  364. TResult
  365. TVbxEventHandler::EvVbxInitForm(TParam1 p1, TParam2 p2)
  366. {
  367.   HWND hWnd = HWND(p1);
  368. #if defined(BI_PLAT_WIN32)
  369.   HINSTANCE hInst = HINSTANCE(::GetWindowLong(hWnd, GWL_HINSTANCE));
  370. #else
  371.   HINSTANCE hInst = HINSTANCE(::GetWindowWord(hWnd, GWW_HINSTANCE));
  372. #endif
  373.   return ::VBXInitDialog(hWnd, hInst, (char far*)p2) ? 1 : -1;
  374. }
  375.