pathpicker.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <precomp.h>
  2. #include "pathpicker.h"
  3. #include <api/script/objects/guiobject.h>
  4. #include <api/script/scriptguid.h>
  5. #include <api/script/objects/c_script/c_text.h>
  6. #include <api/util/selectfile.h>
  7. #define PATHPICKER_MAIN_GROUP L"wasabi.pathpicker.main.group"
  8. #define PATHPICKER_BUTTON L"pathpicker.button"
  9. #define PATHPICKER_TEXT L"pathpicker.text"
  10. // -----------------------------------------------------------------------
  11. PathPicker::PathPicker()
  12. {
  13. abstract_setAllowDeferredContent(1);
  14. abstract_setContent(PATHPICKER_MAIN_GROUP);
  15. clicks_button = NULL;
  16. disable_cfg_event = 0;
  17. }
  18. PathPicker::~PathPicker() {
  19. }
  20. int PathPicker::onInit() {
  21. int rt = PATHPICKER_PARENT::onInit();
  22. return rt;
  23. }
  24. void PathPicker::abstract_onNewContent() {
  25. PATHPICKER_PARENT::abstract_onNewContent();
  26. trapControls();
  27. updatePathInControl();
  28. }
  29. #ifdef WASABI_COMPILE_CONFIG
  30. int PathPicker::onReloadConfig() {
  31. int r = PATHPICKER_PARENT::onReloadConfig();
  32. disable_cfg_event = 1;
  33. updatePathFromConfig(); // triggers onSelect
  34. disable_cfg_event = 0;
  35. return r;
  36. }
  37. void PathPicker::updatePathFromConfig() {
  38. const wchar_t *val = getGuiObject()->guiobject_getCfgString();
  39. const wchar_t *old = getPath();
  40. if (old && val && !_wcsicmp(val, old)) return;
  41. setPath(val);
  42. }
  43. #endif
  44. void PathPicker::trapControls() {
  45. delete clicks_button;
  46. clicks_button = NULL;
  47. GuiObject *butGuiObj = getGuiObject()->guiobject_findObject(PATHPICKER_BUTTON);
  48. if (butGuiObj) clicks_button = new PPClicksCallback(*butGuiObj, this);
  49. }
  50. void PathPicker::clickCallback() {
  51. SelectFile sf(this,0,0);
  52. sf.setDefaultDir(getPath());
  53. if (sf.runSelector(L"directory",0,0)) {
  54. StringW p = sf.getDirectory();
  55. if (p[wcslen(p-1)] != '/' && p[wcslen(p-1)] != '\\')
  56. p += L"\\";
  57. setPath(p);
  58. }
  59. }
  60. void PathPicker::setDefault() {
  61. #ifdef WASABI_COMPILE_CONFIG
  62. onReloadConfig();
  63. #endif
  64. }
  65. void PathPicker::setPath(const wchar_t *path)
  66. {
  67. if (WCSCASEEQLSAFE(curpath, path)) return;
  68. curpath = path;
  69. onPathChanged(path);
  70. }
  71. void PathPicker::onPathChanged(const wchar_t *newpath) {
  72. updatePathInControl();
  73. if (!disable_cfg_event) {
  74. #ifdef WASABI_COMPILE_CONFIG
  75. if (newpath == NULL)
  76. getGuiObject()->guiobject_setCfgString(L"");
  77. else
  78. getGuiObject()->guiobject_setCfgString(newpath);
  79. #endif
  80. }
  81. }
  82. void PathPicker::updatePathInControl() {
  83. GuiObject *content = getContent();
  84. if (content != NULL) {
  85. GuiObject *text = content->guiobject_findObject(PATHPICKER_TEXT);
  86. if (text != NULL) {
  87. C_Text t(*text);
  88. if (curpath.isempty())
  89. t.setText(L"");
  90. else
  91. t.setText(curpath);
  92. }
  93. }
  94. }