selectfile.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <precomp.h>
  2. #include "selectfile.h"
  3. #include <api/wnd/popup.h>
  4. #include <api/service/svcs/svc_filesel.h>
  5. SelectFile::SelectFile(ifc_window *parent, const wchar_t *menu_prefix, const wchar_t *menu_suffix)
  6. : parentWnd(parent),
  7. prefix_str(menu_prefix),
  8. suffix_str(menu_suffix)
  9. {
  10. svc = NULL;
  11. ASSERT(parent != NULL);
  12. xpos = ypos = 0;
  13. pos_set = 0;
  14. }
  15. SelectFile::~SelectFile()
  16. {
  17. if (svc) WASABI_API_SVC->service_release(svc);
  18. types.deleteAll();
  19. }
  20. void SelectFile::setDefaultDir(const wchar_t *dir)
  21. {
  22. default_dir = dir;
  23. }
  24. const wchar_t *SelectFile::getDirectory()
  25. {
  26. if (svc) return svc->getDirectory();
  27. return NULL;
  28. }
  29. void SelectFile::setIdent(const wchar_t *id)
  30. {
  31. ident = id;
  32. }
  33. void SelectFile::setPopPosition(int x, int y)
  34. {
  35. xpos = x;
  36. ypos = y;
  37. pos_set = 1;
  38. }
  39. int SelectFile::runSelector(const wchar_t *type, int allow_multiple, const wchar_t *extlist)
  40. {
  41. if (svc) WASABI_API_SVC->service_release(svc); svc = NULL;
  42. types.deleteAll();
  43. if (type == NULL)
  44. {
  45. int pos = 0;
  46. for (;;)
  47. {
  48. waServiceFactory *wasvc = WASABI_API_SVC->service_enumService(WaSvc::FILESELECTOR, pos++);
  49. if (wasvc == NULL) break;
  50. svc_fileSelector *sfs = castService<svc_fileSelector>(wasvc);
  51. const wchar_t *pref = sfs->getPrefix();
  52. if (pref != NULL)
  53. types.addItem(new StringW(pref));
  54. WASABI_API_SVC->service_release(sfs);
  55. }
  56. if (types.getNumItems() <= 0) return 0; // none?!
  57. PopupMenu *pop = new PopupMenu(parentWnd);
  58. for (int i = 0; i < types.getNumItems(); i++)
  59. {
  60. StringW str;
  61. str += prefix_str;
  62. str += types[i]->getValue();
  63. str += suffix_str;
  64. pop->addCommand(str, i);
  65. }
  66. int cmd = pos_set ? pop->popAtXY(xpos, ypos) : pop->popAtMouse();
  67. StringW *s = types[cmd];
  68. if (s == NULL) return 0;
  69. type = *s;
  70. }
  71. ASSERT(type != NULL);
  72. saved_type = type;
  73. svc = FileSelectorEnum(type).getFirst();
  74. ASSERT(svc != NULL); // we just enumed it
  75. if (extlist != NULL) svc->setExtList(extlist);
  76. //FUCKO : need to set open vs. save as
  77. WASABI_API_WND->pushModalWnd();
  78. int r = svc->runSelector(parentWnd, FileSel::OPEN, allow_multiple,
  79. ident.isempty() ? type : ident.getValue(),
  80. default_dir);
  81. WASABI_API_WND->popModalWnd();
  82. ASSERT(svc != NULL); // we just enumed it
  83. return r;
  84. }
  85. const wchar_t *SelectFile::getType()
  86. {
  87. return saved_type;
  88. }
  89. int SelectFile::getNumFiles()
  90. {
  91. return svc ? svc->getNumFilesSelected() : 0;
  92. }
  93. const wchar_t *SelectFile::enumFilename(int n)
  94. {
  95. return svc ? svc->enumFilename(n) : NULL;
  96. }