grouplist.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include <precomp.h>
  2. #include <api/skin/widgets/group.h>
  3. #include <api/skin/groupmgr.h>
  4. #include "grouplist.h"
  5. #include <api/config/items/cfgitem.h>
  6. #include <api/wnd/notifmsg.h>
  7. const wchar_t groupListXuiObjectStr[] = L"GroupList"; // This is the xml tag
  8. char groupListXuiSvcName[] = "GroupList xui object"; // this is the name of the xuiservice
  9. GroupList::GroupList() {
  10. getScriptObject()->vcpu_setInterface(grouplistGuid, (void *)static_cast<GroupList *>(this));
  11. getScriptObject()->vcpu_setClassName(L"GroupList");
  12. getScriptObject()->vcpu_setController(grouplistController);
  13. scrollY = 0;
  14. maxheight = 0;
  15. maxwidth = 0;
  16. redraw=1;
  17. }
  18. GroupList::~GroupList() {
  19. // todo: unload group scripts
  20. for (int i=0;i<groups.getNumItems();i++)
  21. GroupMgr::destroy(groups.enumItem(i));
  22. groups.removeAll();
  23. }
  24. Group *GroupList::instantiate(const wchar_t *id, int n) {
  25. Group *last=NULL;
  26. RECT r;
  27. getClientRect(&r);
  28. for (int i=0;i<n;i++) {
  29. last = GroupMgr::instantiate(id);
  30. last->setParent(this);
  31. last->init(this);
  32. groups.addItem(last);
  33. }
  34. reposChildren();
  35. notifyParent(ChildNotify::AUTOWHCHANGED);
  36. invalidate();
  37. return last;
  38. }
  39. int GroupList::onResize() {
  40. int r = GROUPLIST_PARENT::onResize();
  41. reposChildren();
  42. return r;
  43. }
  44. Group *GroupList::enumItem(int n) {
  45. return groups.enumItem(n);
  46. }
  47. void GroupList::removeAll() {
  48. for (int i=0;i<groups.getNumItems();i++)
  49. GroupMgr::destroy(groups.enumItem(i));
  50. groups.removeAll();
  51. notifyParent(ChildNotify::AUTOWHCHANGED);
  52. if (!redraw) return;
  53. invalidate();
  54. }
  55. int GroupList::getNumItems() {
  56. return groups.getNumItems();
  57. }
  58. void GroupList::scrollToPercent(int p) {
  59. RECT r;
  60. getClientRect(&r);
  61. int height = r.bottom - r.top;
  62. if (height > maxheight) return;
  63. scrollTo((int)((float)(maxheight - height) * (float)p / 100.0f));
  64. }
  65. void GroupList::scrollTo(int y) {
  66. scrollY = y;
  67. if (!redraw) return;
  68. invalidate();
  69. }
  70. void GroupList::reposChildren() {
  71. if (!redraw) return;
  72. RECT r;
  73. getClientRect(&r);
  74. int ch = -scrollY+r.top;
  75. maxheight = 0;
  76. for (int i=0;i<getNumItems();i++) {
  77. Group *g = enumItem(i);
  78. int h = g->getPreferences(SUGGESTED_H);
  79. int w = g->getPreferences(SUGGESTED_W);
  80. RECT cr;
  81. getClientRect(&cr);
  82. cr.top = ch;
  83. cr.bottom = cr.top + h;
  84. g->resize(&cr);
  85. maxheight += h;
  86. if (maxwidth < w) maxwidth = w;
  87. ch += h;
  88. }
  89. }
  90. void GroupList::setRedraw(int i) {
  91. if (redraw == i) return;
  92. redraw=i;
  93. if (redraw) {
  94. notifyParent(ChildNotify::AUTOWHCHANGED);
  95. reposChildren();
  96. invalidate();
  97. }
  98. }
  99. int GroupList::getPreferences(int what) {
  100. if (what == SUGGESTED_H) { reposChildren(); return maxheight; }
  101. if (what == SUGGESTED_W) { reposChildren(); return maxwidth; }
  102. return GROUPLIST_PARENT::getPreferences(what);
  103. }
  104. GroupListScriptController _grouplistController;
  105. GroupListScriptController *grouplistController = &_grouplistController;
  106. // -- Functions table -------------------------------------
  107. function_descriptor_struct GroupListScriptController ::exportedFunction[] = {
  108. {L"instantiate", 2, (void*)GroupList::script_vcpu_instantiate },
  109. {L"getNumItems", 0, (void*)GroupList::script_vcpu_getNumItems },
  110. {L"enumItem", 1, (void*)GroupList::script_vcpu_enumItem },
  111. {L"removeAll", 0, (void*)GroupList::script_vcpu_removeAll },
  112. {L"scrollToPercent", 1, (void*)GroupList::script_vcpu_scrollToPercent },
  113. {L"setRedraw", 1, (void*)GroupList::script_vcpu_setRedraw},
  114. };
  115. const wchar_t *GroupListScriptController ::getClassName() {
  116. return L"GroupList";
  117. }
  118. const wchar_t *GroupListScriptController ::getAncestorClassName() {
  119. return L"GuiObject";
  120. }
  121. ScriptObject *GroupListScriptController::instantiate() {
  122. GroupList *gl = new GroupList;
  123. ASSERT(gl != NULL);
  124. return gl->getScriptObject();
  125. }
  126. void GroupListScriptController::destroy(ScriptObject *o) {
  127. GroupList *gl = static_cast<GroupList *>(o->vcpu_getInterface(grouplistGuid));
  128. ASSERT(gl != NULL);
  129. delete gl;
  130. }
  131. void *GroupListScriptController::encapsulate(ScriptObject *o) {
  132. return NULL; // no encapsulation for grouplist yet
  133. }
  134. void GroupListScriptController::deencapsulate(void *o) {
  135. }
  136. int GroupListScriptController ::getNumFunctions() {
  137. return sizeof(exportedFunction) / sizeof(function_descriptor_struct);
  138. }
  139. const function_descriptor_struct *GroupListScriptController ::getExportedFunctions() {
  140. return exportedFunction;
  141. }
  142. GUID GroupListScriptController ::getClassGuid() {
  143. return grouplistGuid;
  144. }
  145. scriptVar GroupList::script_vcpu_instantiate(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar id, scriptVar n) {
  146. SCRIPT_FUNCTION_INIT
  147. GroupList *gl = static_cast<GroupList *>(o->vcpu_getInterface(grouplistGuid));
  148. Group *g = NULL;
  149. if (gl) g = gl->instantiate(GET_SCRIPT_STRING(id), GET_SCRIPT_INT(n));
  150. return MAKE_SCRIPT_OBJECT(g ? g->getScriptObject() : NULL);
  151. }
  152. scriptVar GroupList::script_vcpu_getNumItems(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  153. SCRIPT_FUNCTION_INIT
  154. GroupList *gl = static_cast<GroupList *>(o->vcpu_getInterface(grouplistGuid));
  155. if (gl) return MAKE_SCRIPT_INT(gl->getNumItems());
  156. RETURN_SCRIPT_ZERO;
  157. }
  158. scriptVar GroupList::script_vcpu_enumItem(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar n) {
  159. SCRIPT_FUNCTION_INIT
  160. GroupList *gl = static_cast<GroupList *>(o->vcpu_getInterface(grouplistGuid));
  161. Group *g = NULL;
  162. if (gl) g = gl->enumItem(GET_SCRIPT_INT(n));
  163. return MAKE_SCRIPT_OBJECT(g ? g->getScriptObject() : NULL);
  164. }
  165. scriptVar GroupList::script_vcpu_removeAll(SCRIPT_FUNCTION_PARAMS, ScriptObject *o) {
  166. SCRIPT_FUNCTION_INIT
  167. GroupList *gl = static_cast<GroupList *>(o->vcpu_getInterface(grouplistGuid));
  168. if (gl) gl->removeAll();
  169. RETURN_SCRIPT_VOID;
  170. }
  171. scriptVar GroupList::script_vcpu_scrollToPercent(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar n) {
  172. SCRIPT_FUNCTION_INIT
  173. GroupList *gl = static_cast<GroupList *>(o->vcpu_getInterface(grouplistGuid));
  174. if (gl) gl->scrollToPercent(GET_SCRIPT_INT(n));
  175. RETURN_SCRIPT_VOID;
  176. }
  177. scriptVar GroupList::script_vcpu_setRedraw(SCRIPT_FUNCTION_PARAMS, ScriptObject *o, scriptVar n) {
  178. SCRIPT_FUNCTION_INIT
  179. GroupList *gl = static_cast<GroupList *>(o->vcpu_getInterface(grouplistGuid));
  180. if (gl) gl->setRedraw(GET_SCRIPT_INT(n));
  181. RETURN_SCRIPT_VOID;
  182. }