mouseredir.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include <precomp.h>
  2. #include "mouseredir.h"
  3. #include <api/skin/widgets/group.h>
  4. #include <api/script/scriptmgr.h>
  5. #include <api/script/objects/smap.h>
  6. #include <api/script/objects/sregion.h>
  7. const wchar_t mouseRedirXuiObjectStr[] = L"MouseRedir"; // This is the xml tag
  8. char mouseRedirXuiSvcName[] = "MouseRedir xui object"; // this is the name of the xuiservice
  9. XMLParamPair MouseRedir::params[] = {
  10. {MOUSEREDIR_TARGET, L"target"},
  11. };
  12. MouseRedir::MouseRedir()
  13. {
  14. getScriptObject()->vcpu_setInterface(mouseredirGuid, (void *)static_cast<MouseRedir *>(this));
  15. getScriptObject()->vcpu_setClassName(L"MouseRedir");
  16. getScriptObject()->vcpu_setController(mouseredirController);
  17. rgn = NULL;
  18. redirobject = NULL;
  19. getGuiObject()->guiobject_setClickThrough(0);
  20. xuihandle = newXuiHandle();
  21. CreateXMLParameters(xuihandle);
  22. }
  23. void MouseRedir::CreateXMLParameters(int master_handle)
  24. {
  25. //MOUSEREDIR_PARENT::CreateXMLParameters(master_handle);
  26. int numParams = sizeof(params) / sizeof(params[0]);
  27. hintNumberOfParams(xuihandle, numParams);
  28. for (int i = 0;i < numParams;i++)
  29. addParam(xuihandle, params[i], XUI_ATTRIBUTE_REQUIRED);
  30. }
  31. MouseRedir::~MouseRedir()
  32. {
  33. delete rgn;
  34. }
  35. int MouseRedir::setXuiParam(int _xuihandle, int xmlattributeid, const wchar_t *xmlattributename, const wchar_t *value)
  36. {
  37. if (_xuihandle == xuihandle)
  38. {
  39. switch (xmlattributeid)
  40. {
  41. case MOUSEREDIR_TARGET:
  42. setTarget(value);
  43. return 1;
  44. }
  45. }
  46. return MOUSEREDIR_PARENT::setXuiParam(_xuihandle, xmlattributeid, xmlattributename, value);
  47. }
  48. int MouseRedir::onInit()
  49. {
  50. MOUSEREDIR_PARENT::onInit();
  51. if (!deferedredirobjectid.isempty())
  52. doSetTarget(deferedredirobjectid);
  53. deferedredirobjectid = L"";
  54. return 1;
  55. }
  56. int MouseRedir::mouseInRegion(int x, int y)
  57. {
  58. if (!rgn) return 1;
  59. RECT cr;
  60. getClientRect(&cr);
  61. POINT pt = {x - cr.left, y - cr.top};
  62. return rgn->ptInRegion(&pt);
  63. }
  64. void MouseRedir::setRedirection(GuiObject *o)
  65. {
  66. redirobject = o;
  67. }
  68. GuiObject *MouseRedir::getRedirection()
  69. {
  70. return redirobject;
  71. }
  72. void MouseRedir::setTarget(const wchar_t *id)
  73. {
  74. if (!isInited())
  75. deferedredirobjectid = id;
  76. else
  77. doSetTarget(id);
  78. }
  79. void MouseRedir::doSetTarget(const wchar_t *id)
  80. {
  81. Group *g = getGuiObject()->guiobject_getParentGroup();
  82. if (!g) return ;
  83. GuiObject *o = g->getObject(id);
  84. if (!o) return ;
  85. redirobject = o;
  86. }
  87. MouseRedirScriptController _mouseredirController;
  88. MouseRedirScriptController *mouseredirController = &_mouseredirController;
  89. // -- Functions table -------------------------------------
  90. function_descriptor_struct MouseRedirScriptController::exportedFunction[] = {
  91. {L"setRegionFromMap", 1, (void*)MouseRedir::script_vcpu_setRegionFromMap},
  92. {L"setRegion", 1, (void*)MouseRedir::script_vcpu_setRegion },
  93. {L"setRedirection", 1, (void*)MouseRedir::script_vcpu_setRedirection},
  94. {L"getRedirection", 0, (void*)MouseRedir::script_vcpu_getRedirection},
  95. };
  96. // --------------------------------------------------------
  97. const wchar_t *MouseRedirScriptController::getClassName()
  98. {
  99. return L"MouseRedir";
  100. }
  101. const wchar_t *MouseRedirScriptController::getAncestorClassName()
  102. {
  103. return L"GuiObject";
  104. }
  105. ScriptObject *MouseRedirScriptController::instantiate()
  106. {
  107. MouseRedir *m = new MouseRedir;
  108. if (!m) return NULL;
  109. return m->getScriptObject();
  110. }
  111. void MouseRedirScriptController::destroy(ScriptObject *o)
  112. {
  113. MouseRedir *obj = static_cast<MouseRedir*>(o->vcpu_getInterface(mouseredirGuid));
  114. ASSERT(obj != NULL);
  115. delete obj;
  116. }
  117. void *MouseRedirScriptController::encapsulate(ScriptObject *o)
  118. {
  119. return NULL; // no encapsulation for mouseredir yet
  120. }
  121. void MouseRedirScriptController::deencapsulate(void *)
  122. {}
  123. int MouseRedirScriptController::getNumFunctions()
  124. {
  125. return sizeof(exportedFunction) / sizeof(function_descriptor_struct);
  126. }
  127. const function_descriptor_struct *MouseRedirScriptController::getExportedFunctions()
  128. {
  129. return exportedFunction;
  130. }
  131. GUID MouseRedirScriptController::getClassGuid()
  132. {
  133. return mouseredirGuid;
  134. }
  135. //---------------------------------------------------------------------------
  136. void MouseRedir::setRegionFromMap(SMap *map, int byte, int inversed)
  137. {
  138. if (rgn)
  139. {
  140. delete rgn;
  141. rgn = NULL;
  142. }
  143. rgn = new RegionI(map->getBitmap(), NULL, 0, 0, FALSE, 1, (unsigned char)byte, inversed);
  144. }
  145. void MouseRedir::setRegion(SRegion *reg)
  146. {
  147. if (rgn) { delete rgn; rgn = NULL; }
  148. if (!reg) { invalidate(); return ; }
  149. rgn = new RegionI();
  150. rgn->addRegion(reg->getRegion());
  151. }
  152. ifc_window *MouseRedir::getForwardWnd()
  153. {
  154. if (redirobject) return redirobject->guiobject_getRootWnd();
  155. return this;
  156. }
  157. scriptVar MouseRedir::script_vcpu_setRedirection(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar obj)
  158. {
  159. SCRIPT_FUNCTION_INIT;
  160. MouseRedir *mr = static_cast<MouseRedir *>(o->vcpu_getInterface(mouseredirGuid));
  161. if (mr) mr->setRedirection(static_cast<GuiObject *>(GET_SCRIPT_OBJECT_AS(obj, guiObjectGuid)));
  162. RETURN_SCRIPT_VOID;
  163. }
  164. scriptVar MouseRedir::script_vcpu_getRedirection(SCRIPT_FUNCTION_PARAMS, ScriptObject *o)
  165. {
  166. SCRIPT_FUNCTION_INIT;
  167. MouseRedir *mr = static_cast<MouseRedir *>(o->vcpu_getInterface(mouseredirGuid));
  168. return MAKE_SCRIPT_OBJECT(mr ? mr ->getRedirection()->guiobject_getScriptObject() : NULL);
  169. }
  170. scriptVar MouseRedir::script_vcpu_setRegionFromMap(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar map, scriptVar byte, scriptVar inv)
  171. {
  172. SCRIPT_FUNCTION_INIT;
  173. ASSERT(SOM::isNumeric(&byte));
  174. ASSERT(SOM::isNumeric(&inv));
  175. MouseRedir *mr = static_cast<MouseRedir *>(o->vcpu_getInterface(mouseredirGuid));
  176. SMap *sm = static_cast<SMap*>(GET_SCRIPT_OBJECT_AS(map, mapGuid));
  177. if (mr) mr->setRegionFromMap(sm, SOM::makeInt(&byte), SOM::makeInt(&inv));
  178. RETURN_SCRIPT_VOID;
  179. }
  180. scriptVar MouseRedir::script_vcpu_setRegion(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar reg)
  181. {
  182. SCRIPT_FUNCTION_INIT;
  183. MouseRedir *mr = static_cast<MouseRedir *>(o->vcpu_getInterface(mouseredirGuid));
  184. SRegion *r = static_cast<SRegion *>(GET_SCRIPT_OBJECT_AS(reg, regionGuid));
  185. if (mr) mr->setRegion(r);
  186. RETURN_SCRIPT_VOID;
  187. }