SettingsWindow.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright (C) 2013 by Isak Andersson
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  12. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  13. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  14. THE SOFTWARE.
  15. */
  16. #include "SettingsWindow.h"
  17. #include "PolycodeFrame.h"
  18. extern PolycodeFrame *globalFrame;
  19. SettingsWindow::SettingsWindow() : UIWindow(L"Settings", SETTINGS_WINDOW_WIDTH, SETTINGS_WINDOW_HEIGHT) {
  20. closeOnEscape = true;
  21. ScreenLabel *label = new ScreenLabel("MISC", 22, "section", Label::ANTIALIAS_FULL);
  22. addChild(label);
  23. label->color.a = 0.4;
  24. label->setPosition(padding, 50);
  25. useExternalTextEditorBox = new UICheckBox("Use external text editor", false);
  26. addChild(useExternalTextEditorBox);
  27. useExternalTextEditorBox->setPosition(padding, 85);
  28. #define BUTTON_WIDTH 80
  29. #define BUTTON_PADDING 10
  30. #define EDITOR_BROWSE_POS 110
  31. #define TEXTBOX_HEIGHT 12
  32. externalTextEditorCommand = new UITextInput(false, SETTINGS_WINDOW_WIDTH - (padding*2 + BUTTON_WIDTH + BUTTON_PADDING/2), TEXTBOX_HEIGHT);
  33. addChild(externalTextEditorCommand);
  34. externalTextEditorCommand->setPosition(padding, EDITOR_BROWSE_POS);
  35. browseButton = new UIButton("Browse...", BUTTON_WIDTH);
  36. browseButton->addEventListener(this, UIEvent::CLICK_EVENT);
  37. addChild(browseButton);
  38. browseButton->setPosition(SETTINGS_WINDOW_WIDTH - (2*padding + BUTTON_WIDTH/2), EDITOR_BROWSE_POS);
  39. cancelButton = new UIButton("Cancel", BUTTON_WIDTH);
  40. cancelButton->addEventListener(this, UIEvent::CLICK_EVENT);
  41. addChild(cancelButton);
  42. cancelButton->setPosition(SETTINGS_WINDOW_WIDTH - (2*padding + BUTTON_WIDTH*1.5 + BUTTON_PADDING), SETTINGS_WINDOW_HEIGHT - padding);
  43. okButton = new UIButton("OK", BUTTON_WIDTH);
  44. okButton->addEventListener(this, UIEvent::CLICK_EVENT);
  45. addChild(okButton);
  46. okButton->setPosition(SETTINGS_WINDOW_WIDTH - (2*padding + BUTTON_WIDTH/2), SETTINGS_WINDOW_HEIGHT - padding);
  47. }
  48. void SettingsWindow::handleEvent(Event *event) {
  49. if(event->getEventType() == "UIEvent") {
  50. if(event->getEventCode() == UIEvent::CLICK_EVENT) {
  51. if(event->getDispatcher() == okButton) {
  52. dispatchEvent(new UIEvent(), UIEvent::OK_EVENT);
  53. }
  54. if(event->getDispatcher() == cancelButton) {
  55. dispatchEvent(new UIEvent(), UIEvent::CLOSE_EVENT);
  56. }
  57. if(event->getDispatcher() == browseButton) {
  58. #ifdef USE_POLYCODEUI_FILE_DIALOGS
  59. std::vector<String> extensions;
  60. extensions.push_back("");
  61. globalFrame->showFileBrowser(CoreServices::getInstance()->getCore()->getUserHomeDirectory(), false, extensions, false);
  62. globalFrame->fileDialog->addEventListener(this, UIEvent::OK_EVENT);
  63. #else
  64. vector<CoreFileExtension> extensions;
  65. CoreFileExtension ext;
  66. #ifdef _WINDOWS
  67. ext.extension = "exe";
  68. #elif defined(__APPLE__) && defined(__MACH__)
  69. ext.extension = "app";
  70. #else
  71. ext.extension = "";
  72. #endif
  73. ext.description = "executable";
  74. extensions.push_back(ext);
  75. std::vector<String> path = CoreServices::getInstance()->getCore()->openFilePicker(extensions, false);
  76. if(path.size() == 0) {
  77. return;
  78. }
  79. if(path[0] != "") {
  80. externalTextEditorCommand->setText(path[0]);
  81. }
  82. #endif
  83. }
  84. }
  85. if(event->getDispatcher() == globalFrame->fileDialog && event->getEventCode() == UIEvent::OK_EVENT) {
  86. String path = globalFrame->fileDialog->getSelection();
  87. if (path != "") {
  88. externalTextEditorCommand->setText(path);
  89. }
  90. }
  91. }
  92. UIWindow::handleEvent(event);
  93. }
  94. void SettingsWindow::updateUI() {
  95. Config *config = CoreServices::getInstance()->getConfig();
  96. useExternalTextEditorBox->setChecked(config->getStringValue("Polycode", "useExternalTextEditor") == "true");
  97. externalTextEditorCommand->setText(config->getStringValue("Polycode", "externalTextEditorCommand"));
  98. }
  99. SettingsWindow::~SettingsWindow() {
  100. }