1
0

objdirwnd.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include <precomp.h>
  2. #include "objdirwnd.h"
  3. #include <api/wnd/contextmenu.h>
  4. #include <api/wnd/dragitemi.h>
  5. #include <api/skin/widgets/customobject.h>
  6. #include <bfc/parse/pathparse.h>
  7. ObjDirWnd::ObjDirWnd() :
  8. objdir(NULL), displaygroup(NULL) { }
  9. ObjDirWnd::~ObjDirWnd() {
  10. WASABI_API_SVC->service_release(objdir);
  11. if (displaygroup != NULL) WASABI_API_SKIN->group_destroy(displaygroup);
  12. }
  13. void ObjDirWnd::setTargetDirName(const wchar_t *dirname) {
  14. objectDirName = dirname;
  15. //FUCKO: reset here
  16. }
  17. void ObjDirWnd::setActionTarget(const wchar_t *targetname) {
  18. objectDirTarget = targetname;
  19. }
  20. void ObjDirWnd::setDisplayTarget(const wchar_t *name) {
  21. displayTarget = name;
  22. }
  23. void ObjDirWnd::setDefaultDisplay(const wchar_t *display) {
  24. defaultDisplay = display;
  25. }
  26. int ObjDirWnd::onInit() {
  27. OBJDIRWND_PARENT::onInit();
  28. setSorted(TRUE);
  29. ASSERT(!objectDirName.isempty());
  30. objdir = ObjectDirEnum(objectDirName).getNext();
  31. if (objdir != NULL) {
  32. setHilitedColor(L"pledit.currentOutline");//FUCKO from service
  33. viewer_addViewItem(objdir);
  34. int nobj = objdir->getNumObjects();
  35. for (int i = 0; i < nobj; i++) {
  36. ObjectHandle hand = objdir->enumObject(i);
  37. folderify(createObjItem(hand));
  38. }
  39. }
  40. return 1;
  41. }
  42. int ObjDirWnd::onContextMenu(int x, int y) {
  43. if (objdir != NULL) {
  44. DragItemT<svc_objectDir> di(objdir);
  45. ContextMenu(this, &di);
  46. }
  47. return 1;
  48. }
  49. int ObjDirWnd::onPreItemContextMenu(TreeItem *item, int x, int y) {
  50. BaseObjItem *boi = static_cast<BaseObjItem*>(item);
  51. if (boi->getParam() == OBJTYPE_OBJ) {
  52. ObjItem *oi = static_cast<ObjItem*>(item);
  53. int r = objdir->contextMenu(this, x, y, oi->getObjectHandle());
  54. if (r == -1) {
  55. editItemLabel(item);
  56. }
  57. } else {
  58. // handle other item types here
  59. }
  60. return 1;
  61. }
  62. int ObjDirWnd::compareItem(TreeItem *p1, TreeItem *p2) {
  63. BaseObjItem *b1 = static_cast<BaseObjItem*>(p1);
  64. BaseObjItem *b2 = static_cast<BaseObjItem*>(p2);
  65. int r = CMP3(b1->sortorder, b2->sortorder);
  66. if (r == 0) return OBJDIRWND_PARENT::compareItem(p1, p2);
  67. return r;
  68. }
  69. int ObjDirWnd::viewer_onEvent(svc_objectDir *item, int event, intptr_t param2, void *ptr, size_t ptrlen) {
  70. switch (event) {
  71. case svc_objectDir::Event_OBJECT_ADDED: {
  72. int sel_after = (enumAllItems(0) == NULL);
  73. ObjItem *oi = createObjItem(param2);
  74. folderify(oi);
  75. if (sel_after) selectItemDeferred(oi);
  76. }
  77. break;
  78. case svc_objectDir::Event_OBJECT_REMOVED: {
  79. ObjItem *pli = getObjItemForHandle(param2);
  80. delItemDeferred(pli);
  81. }
  82. break;
  83. case svc_objectDir::Event_OBJECT_LABELCHANGE: {
  84. ObjItem *pli = getObjItemForHandle(param2);
  85. const wchar_t *name = objdir->getObjectLabel(pli->getObjectHandle());
  86. pli->setLabel(name);
  87. }
  88. break;
  89. case svc_objectDir::Event_OBJECT_PATHCHANGE: {
  90. ObjItem *pli = getObjItemForHandle(param2);
  91. folderify(pli);
  92. }
  93. break;
  94. case svc_objectDir::Event_OBJECT_SORTORDERCHANGE: {
  95. ObjItem *pli = getObjItemForHandle(param2);
  96. ASSERT(pli != NULL);
  97. pli->sortorder = objdir->getObjectSortOrder(param2);
  98. sortTreeItems();
  99. }
  100. break;
  101. }
  102. return 1;
  103. }
  104. ObjItem *ObjDirWnd::createObjItem(ObjectHandle handle) {
  105. ObjItem *ret = new ObjItem(handle, objdir->getObjectLabel(handle));
  106. const wchar_t *nn = objdir->getObjectIcon(handle);
  107. if (nn && *nn) {
  108. SkinBitmap *ic = new SkinBitmap(nn);
  109. ret->setIcon(ic);
  110. }
  111. ret->hittable = objdir->getObjectSelectable(handle);
  112. ret->sortorder = objdir->getObjectSortOrder(handle);
  113. return ret;
  114. }
  115. ObjItem *ObjDirWnd::getObjItemForHandle(ObjectHandle handle) {
  116. for (int i = 0; ; i++) {
  117. TreeItem *it = enumAllItems(i);
  118. if (it == NULL) break;
  119. BaseObjItem *boi = static_cast<BaseObjItem*>(it);
  120. if (boi->getParam() != OBJTYPE_OBJ) continue;
  121. ObjItem *plit = static_cast<ObjItem *>(it);
  122. if (plit->getObjectHandle() == handle) return plit;
  123. }
  124. return NULL;
  125. }
  126. void ObjDirWnd::folderify(ObjItem *item) {
  127. if (item->getTree() != NULL)
  128. removeTreeItem(item);
  129. PathParserW pp(objdir->getObjectPath(item->getObjectHandle()));
  130. TreeItem *prevpar = NULL;
  131. for (int i = 0; i < pp.getNumStrings(); i++) {
  132. TreeItem *newpar = NULL;
  133. // check for already-existing folder
  134. for (int j = 0; ; j++) {
  135. if (prevpar == NULL) { // search top level
  136. newpar = enumRootItem(j);
  137. } else { // search prevpar children
  138. newpar = prevpar->getNthChild(j);
  139. }
  140. if (newpar == NULL) break; // out of thingies
  141. BaseObjItem *boi = static_cast<BaseObjItem*>(newpar);
  142. if (boi->getParam() != OBJTYPE_FOLDER) continue;
  143. ObjDirFolderItem *fi = static_cast<ObjDirFolderItem*>(newpar);
  144. if (fi->dying) continue;
  145. if (!wcscmp(boi->getLabel(), pp.enumString(i))) {
  146. break;
  147. }
  148. }
  149. if (newpar == NULL) {
  150. newpar = new ObjDirFolderItem(pp.enumString(i));
  151. addTreeItem(newpar, prevpar, FALSE, TRUE);
  152. expandItemDeferred(newpar);
  153. }
  154. prevpar = newpar;
  155. }
  156. // now attach it
  157. addTreeItem(item, prevpar);
  158. }
  159. void ObjDirWnd::onItemSelected(TreeItem *item) {
  160. BaseObjItem *boi = static_cast<BaseObjItem*>(item);
  161. if (boi->getParam() == OBJTYPE_OBJ) {
  162. ObjItem *objitem = static_cast<ObjItem *>(item);
  163. ObjectHandle handle = objitem->getObjectHandle();
  164. StringW display(objdir->getObjectDisplayGroup(handle));
  165. if (display.isempty()) display = defaultDisplay;
  166. if (!display.isempty())
  167. {
  168. GuiObject *go = findObject(displayTarget);
  169. if (go != NULL)
  170. {
  171. CustomObject *co = static_cast<CustomObject *>(go->guiobject_getScriptObject()->vcpu_getInterface(customObjectGuid));
  172. if (co != NULL) {
  173. if (displaygroupname != display) {
  174. co->customobject_setRootWnd(NULL);
  175. ifc_window *prev = displaygroup;
  176. if (prev != NULL) WASABI_API_SKIN->group_destroy(prev);
  177. displaygroup = WASABI_API_SKIN->group_create(display);
  178. co->customobject_setRootWnd(displaygroup);
  179. displaygroupname = display;
  180. }
  181. }
  182. }
  183. }
  184. // tell the objdir
  185. objdir->onAction(svc_objectDir::ODACTION_SELECTED, this, objectDirTarget, handle);
  186. } else {
  187. // handle other item types here
  188. }
  189. }
  190. void ObjDirWnd::onItemDeselected(TreeItem *item) {
  191. BaseObjItem *boi = static_cast<BaseObjItem*>(item);
  192. if (boi->getParam() == OBJTYPE_OBJ) {
  193. ObjItem *objitem = static_cast<ObjItem *>(item);
  194. ObjectHandle handle = objitem->getObjectHandle();
  195. objdir->onAction(svc_objectDir::ODACTION_DESELECTED, this, objectDirTarget, handle);
  196. } else {
  197. // handle other item types here
  198. }
  199. }