SettingsWindow.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #include "PolycodeTextEditor.h"
  19. extern PolycodeFrame *globalFrame;
  20. extern UIGlobalMenu *globalMenu;
  21. extern SyntaxHighlightTheme *globalSyntaxTheme;
  22. SettingsWindow::SettingsWindow() : UIWindow(L"Settings", SETTINGS_WINDOW_WIDTH, SETTINGS_WINDOW_HEIGHT) {
  23. closeOnEscape = true;
  24. UILabel *label = new UILabel("TEXT EDITING", 22, "section", Label::ANTIALIAS_FULL);
  25. addChild(label);
  26. label->color.a = 1.0;
  27. label->setPosition(padding, 50);
  28. useExternalTextEditorBox = new UICheckBox("Use external text editor", false);
  29. addChild(useExternalTextEditorBox);
  30. useExternalTextEditorBox->setPosition(padding, 85);
  31. #define BUTTON_WIDTH 80
  32. #define BUTTON_PADDING 10
  33. #define EDITOR_BROWSE_POS 110
  34. #define TEXTBOX_HEIGHT 12
  35. externalTextEditorCommand = new UITextInput(false, SETTINGS_WINDOW_WIDTH - (padding*2 + BUTTON_WIDTH + BUTTON_PADDING/2), TEXTBOX_HEIGHT);
  36. addChild(externalTextEditorCommand);
  37. externalTextEditorCommand->setPosition(padding, EDITOR_BROWSE_POS);
  38. label = new UILabel("Syntax highlighting theme", 12);
  39. addChild(label);
  40. label->color.a = 1.0;
  41. label->setPosition(padding, EDITOR_BROWSE_POS + 35);
  42. syntaxThemeBox = new UIComboBox(globalMenu, 300);
  43. addChild(syntaxThemeBox);
  44. syntaxThemeBox->setPosition(padding, EDITOR_BROWSE_POS + 55);
  45. syntaxThemeBox->addEventListener(this, UIEvent::CHANGE_EVENT);
  46. std::vector<OSFileEntry> themes = OSBasics::parseFolder(CoreServices::getInstance()->getCore()->getDefaultWorkingDirectory() + "/SyntaxThemes", false);
  47. for(int i=0; i < themes.size(); i++) {
  48. if(themes[i].extension == "xml") {
  49. syntaxThemeBox->addComboItem(themes[i].nameWithoutExtension);
  50. }
  51. }
  52. browseButton = new UIButton("Browse...", BUTTON_WIDTH);
  53. browseButton->addEventListener(this, UIEvent::CLICK_EVENT);
  54. addChild(browseButton);
  55. browseButton->setPosition(SETTINGS_WINDOW_WIDTH - (2*padding + BUTTON_WIDTH/2), EDITOR_BROWSE_POS);
  56. label = new UILabel("GENERAL", 22, "section", Label::ANTIALIAS_FULL);
  57. addChild(label);
  58. label->color.a = 1.0;
  59. label->setPosition(padding, 200);
  60. label = new UILabel("UI theme (requires restart)", 12);
  61. addChild(label);
  62. label->color.a = 1.0;
  63. label->setPosition(padding, 235);
  64. uiThemeBox = new UIComboBox(globalMenu, 300);
  65. addChild(uiThemeBox);
  66. uiThemeBox->setPosition(padding, 255);
  67. uiThemeBox->addEventListener(this, UIEvent::CHANGE_EVENT);
  68. std::vector<OSFileEntry> uiThemes = OSBasics::parseFolder(CoreServices::getInstance()->getCore()->getDefaultWorkingDirectory() + "/UIThemes", false);
  69. for(int i=0; i < uiThemes.size(); i++) {
  70. if(uiThemes[i].type == OSFileEntry::TYPE_FOLDER) {
  71. uiThemeBox->addComboItem(uiThemes[i].name);
  72. if(uiThemes[i].name == CoreServices::getInstance()->getConfig()->getStringValue("Polycode", "uiTheme")) {
  73. uiThemeBox->setSelectedIndex(i);
  74. }
  75. }
  76. }
  77. cancelButton = new UIButton("Cancel", BUTTON_WIDTH);
  78. cancelButton->addEventListener(this, UIEvent::CLICK_EVENT);
  79. addChild(cancelButton);
  80. cancelButton->setPosition(SETTINGS_WINDOW_WIDTH - (2*padding + BUTTON_WIDTH*1.5 + BUTTON_PADDING), SETTINGS_WINDOW_HEIGHT - padding);
  81. okButton = new UIButton("OK", BUTTON_WIDTH);
  82. okButton->addEventListener(this, UIEvent::CLICK_EVENT);
  83. addChild(okButton);
  84. okButton->setPosition(SETTINGS_WINDOW_WIDTH - (2*padding + BUTTON_WIDTH/2), SETTINGS_WINDOW_HEIGHT - padding);
  85. }
  86. void SettingsWindow::handleEvent(Event *event) {
  87. if(event->getEventType() == "UIEvent") {
  88. if(event->getEventCode() == UIEvent::CHANGE_EVENT) {
  89. if(event->getDispatcher() == syntaxThemeBox) {
  90. if(syntaxThemeBox->getSelectedItem()->label != globalSyntaxTheme->name) {
  91. globalSyntaxTheme->loadFromFile(syntaxThemeBox->getSelectedItem()->label);
  92. }
  93. }
  94. } else if(event->getEventCode() == UIEvent::CLICK_EVENT) {
  95. if(event->getDispatcher() == okButton) {
  96. dispatchEvent(new UIEvent(), UIEvent::OK_EVENT);
  97. }
  98. if(event->getDispatcher() == cancelButton) {
  99. dispatchEvent(new UIEvent(), UIEvent::CLOSE_EVENT);
  100. }
  101. if(event->getDispatcher() == browseButton) {
  102. #ifdef USE_POLYCODEUI_FILE_DIALOGS
  103. std::vector<String> extensions;
  104. extensions.push_back("");
  105. globalFrame->showFileBrowser(CoreServices::getInstance()->getCore()->getUserHomeDirectory(), false, extensions, false);
  106. globalFrame->fileDialog->addEventListener(this, UIEvent::OK_EVENT);
  107. #else
  108. vector<CoreFileExtension> extensions;
  109. CoreFileExtension ext;
  110. #ifdef _WINDOWS
  111. ext.extension = "exe";
  112. #elif defined(__APPLE__) && defined(__MACH__)
  113. ext.extension = "app";
  114. #else
  115. ext.extension = "";
  116. #endif
  117. ext.description = "executable";
  118. extensions.push_back(ext);
  119. std::vector<String> path = CoreServices::getInstance()->getCore()->openFilePicker(extensions, false);
  120. if(path.size() == 0) {
  121. return;
  122. }
  123. if(path[0] != "") {
  124. externalTextEditorCommand->setText(path[0]);
  125. }
  126. #endif
  127. }
  128. }
  129. if(event->getDispatcher() == globalFrame->fileDialog && event->getEventCode() == UIEvent::OK_EVENT) {
  130. String path = globalFrame->fileDialog->getSelection();
  131. if (path != "") {
  132. externalTextEditorCommand->setText(path);
  133. }
  134. }
  135. }
  136. UIWindow::handleEvent(event);
  137. }
  138. void SettingsWindow::updateUI() {
  139. Config *config = CoreServices::getInstance()->getConfig();
  140. useExternalTextEditorBox->setChecked(config->getStringValue("Polycode", "useExternalTextEditor") == "true");
  141. externalTextEditorCommand->setText(config->getStringValue("Polycode", "externalTextEditorCommand"));
  142. for(int i=0; i < syntaxThemeBox->getNumItems(); i++) {
  143. if(globalSyntaxTheme->name == syntaxThemeBox->getItemAtIndex(i)->label) {
  144. syntaxThemeBox->setSelectedIndex(i);
  145. }
  146. }
  147. for(int i=0; i < uiThemeBox->getNumItems(); i++) {
  148. if(config->getStringValue("Polycode", "uiTheme") == uiThemeBox->getItemAtIndex(i)->label) {
  149. uiThemeBox->setSelectedIndex(i);
  150. }
  151. }
  152. }
  153. SettingsWindow::~SettingsWindow() {
  154. }