| 1 | // This file is part of SmallBASIC |
| 2 | // |
| 3 | // Copyright(C) 2001-2019 Chris Warren-Smith. |
| 4 | // |
| 5 | // This program is distributed under the terms of the GPL v2.0 or later |
| 6 | // Download the GNU Public License (GPL) from www.gnu.org |
| 7 | // |
| 8 | |
| 9 | #include <config.h> |
| 10 | #include <unistd.h> |
| 11 | #include <ctype.h> |
| 12 | #include "platform/fltk/Profile.h" |
| 13 | #include "platform/fltk/MainWindow.h" |
| 14 | #include "platform/fltk/utils.h" |
| 15 | |
| 16 | const char *configFile = "fl_config.txt" ; |
| 17 | const char *pathKey = "path" ; |
| 18 | const char *indentLevelKey = "indentLevel" ; |
| 19 | const char *fontNameKey = "fontName" ; |
| 20 | const char *fontSizeKey = "fontSize" ; |
| 21 | const char *windowPosKey = "windowPos" ; |
| 22 | const char *activeTabKey = "activeTab" ; |
| 23 | const char *createBackupsKey = "createBackups" ; |
| 24 | const char *lineNumbersKey = "lineNumbers" ; |
| 25 | const char *appPositionKey = "appPosition" ; |
| 26 | const char *themeIdKey = "themeId" ; |
| 27 | const char *helpThemeIdKey = "helpThemeId" ; |
| 28 | |
| 29 | // in BasicEditor.cxx |
| 30 | extern Fl_Text_Display::Style_Table_Entry styletable[]; |
| 31 | |
| 32 | static StyleField lastStyle = st_lineNumbers; |
| 33 | |
| 34 | // |
| 35 | // Profile constructor |
| 36 | // |
| 37 | Profile::Profile() : |
| 38 | _font(FL_COURIER), |
| 39 | _appPosition(0, 0, 640, 480), |
| 40 | _loaded(false), |
| 41 | _createBackups(true), |
| 42 | _lineNumbers(true), |
| 43 | _fontSize(DEF_FONT_SIZE), |
| 44 | _indentLevel(2), |
| 45 | _helpThemeId(0) { |
| 46 | loadEditTheme(0); |
| 47 | _helpTheme.setId(_helpThemeId); |
| 48 | } |
| 49 | |
| 50 | // |
| 51 | // setup the editor defaults |
| 52 | // |
| 53 | void Profile::loadConfig(EditorWidget *editWidget) { |
| 54 | editWidget->setIndentLevel(_indentLevel); |
| 55 | editWidget->setFont(_font); |
| 56 | editWidget->setFontSize(_fontSize); |
| 57 | editWidget->setTheme(&_theme); |
| 58 | editWidget->getEditor()->linenumber_width(_lineNumbers ? LINE_NUMBER_WIDTH : 1); |
| 59 | } |
| 60 | |
| 61 | // |
| 62 | // select the given theme |
| 63 | // |
| 64 | void Profile::loadEditTheme(int themeId) { |
| 65 | _theme.setId(themeId); |
| 66 | _themeId = themeId; |
| 67 | styletable[0].color = get_color(_theme._color); // A - plain |
| 68 | styletable[1].color = get_color(_theme._syntax_comments); // B - comments |
| 69 | styletable[2].color = get_color(_theme._syntax_text); // C - string |
| 70 | styletable[3].color = get_color(_theme._syntax_statement); // D - keywords |
| 71 | styletable[4].color = get_color(_theme._syntax_command); // E - functions |
| 72 | styletable[5].color = get_color(_theme._syntax_command); // F - procedures |
| 73 | styletable[6].color = get_color(_theme._match_background); // G - find matches |
| 74 | styletable[7].color = get_color(_theme._syntax_comments); // H - comments |
| 75 | styletable[8].color = get_color(_theme._syntax_digit); // I - numbers |
| 76 | styletable[9].color = get_color(_theme._syntax_command); // J - operators |
| 77 | styletable[10].color = get_color(_theme._selection_background); // K Selection Background |
| 78 | styletable[11].color = get_color(_theme._background); // L Background |
| 79 | styletable[12].color = get_color(_theme._number_color); // M Line numbers |
| 80 | } |
| 81 | |
| 82 | // |
| 83 | // restore saved settings |
| 84 | // |
| 85 | void Profile::restore(MainWindow *wnd) { |
| 86 | strlib::String buffer; |
| 87 | Properties<String *> profile; |
| 88 | |
| 89 | FILE *fp = wnd->openConfig(configFile, "r" ); |
| 90 | if (fp) { |
| 91 | fseek(fp, 0, SEEK_END); |
| 92 | long len = ftell(fp); |
| 93 | rewind(fp); |
| 94 | buffer.append(fp, len); |
| 95 | fclose(fp); |
| 96 | profile.load(buffer.c_str(), buffer.length()); |
| 97 | |
| 98 | restoreValue(&profile, indentLevelKey, &_indentLevel); |
| 99 | restoreValue(&profile, createBackupsKey, &_createBackups); |
| 100 | restoreValue(&profile, lineNumbersKey, &_lineNumbers); |
| 101 | restoreValue(&profile, themeIdKey, &_themeId); |
| 102 | restoreValue(&profile, helpThemeIdKey, &_helpThemeId); |
| 103 | restoreStyles(&profile); |
| 104 | |
| 105 | Fl_Rect rc; |
| 106 | rc = restoreRect(&profile, appPositionKey); |
| 107 | if (rc.w() && rc.h()) { |
| 108 | _appPosition = rc; |
| 109 | } |
| 110 | |
| 111 | rc = restoreRect(&profile, windowPosKey); |
| 112 | if (rc.w() && rc.h()) { |
| 113 | restoreWindowPos(wnd, rc); |
| 114 | } |
| 115 | |
| 116 | restoreTabs(wnd, &profile); |
| 117 | wnd->_runtime->setFontSize(_fontSize); |
| 118 | } |
| 119 | _loaded = true; |
| 120 | } |
| 121 | |
| 122 | // |
| 123 | // restore the standalone window position |
| 124 | // |
| 125 | void Profile::restoreAppPosition(Fl_Window *wnd) { |
| 126 | if (_appPosition.w() && _appPosition.h()) { |
| 127 | int x = _appPosition.x() != 0 ? _appPosition.x() : wnd->x(); |
| 128 | int y = _appPosition.y() != 0 ? _appPosition.y() : wnd->y(); |
| 129 | wnd->resize(x, y, _appPosition.w(), _appPosition.h()); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // |
| 134 | // set editor theme colors |
| 135 | // |
| 136 | void Profile::setEditTheme(EditorWidget *editWidget) { |
| 137 | editWidget->setTheme(&_theme); |
| 138 | editWidget->redraw(); |
| 139 | } |
| 140 | |
| 141 | // |
| 142 | // set help theme colors |
| 143 | // |
| 144 | void Profile::setHelpTheme(HelpWidget *helpWidget, int themeId) { |
| 145 | if (themeId != -1) { |
| 146 | _helpTheme.setId(themeId); |
| 147 | _helpThemeId = themeId; |
| 148 | } |
| 149 | helpWidget->setTheme(&_helpTheme); |
| 150 | helpWidget->damage(FL_DAMAGE_ALL); |
| 151 | } |
| 152 | |
| 153 | // |
| 154 | // persist profile values |
| 155 | // |
| 156 | void Profile::save(MainWindow *wnd) { |
| 157 | if (_loaded) { |
| 158 | // prevent overwriting config when not initially used |
| 159 | FILE *fp = wnd->openConfig(configFile); |
| 160 | if (fp) { |
| 161 | saveValue(fp, indentLevelKey, _indentLevel); |
| 162 | saveValue(fp, createBackupsKey, _createBackups); |
| 163 | saveValue(fp, lineNumbersKey, _lineNumbers); |
| 164 | saveValue(fp, themeIdKey, _themeId); |
| 165 | saveValue(fp, helpThemeIdKey, _helpThemeId); |
| 166 | saveStyles(fp); |
| 167 | saveTabs(fp, wnd); |
| 168 | saveRect(fp, appPositionKey, &_appPosition); |
| 169 | Fl_Rect wndRect(wnd->x(), wnd->y(), wnd->w(), wnd->h()); |
| 170 | saveRect(fp, windowPosKey, &wndRect); |
| 171 | fclose(fp); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // |
| 177 | // update the theme from the style table |
| 178 | // |
| 179 | void Profile::updateTheme() { |
| 180 | _theme._color = styletable[0].color >> 8; |
| 181 | _theme._syntax_comments = styletable[1].color >> 8; |
| 182 | _theme._syntax_text = styletable[2].color >> 8; |
| 183 | _theme._syntax_statement = styletable[3].color >> 8; |
| 184 | _theme._syntax_command = styletable[4].color >> 8; |
| 185 | _theme._syntax_command = styletable[5].color >> 8; |
| 186 | _theme._match_background = styletable[6].color >> 8; |
| 187 | _theme._syntax_comments = styletable[7].color >> 8; |
| 188 | _theme._syntax_digit = styletable[8].color >> 8; |
| 189 | _theme._syntax_command = styletable[9].color >> 8; |
| 190 | _theme._selection_background = styletable[10].color >> 8; |
| 191 | _theme._background = styletable[11].color >> 8; |
| 192 | _theme._number_color = styletable[12].color >> 8; |
| 193 | } |
| 194 | |
| 195 | // |
| 196 | // returns the next integer from the given string |
| 197 | // |
| 198 | int Profile::nextInteger(const char *s, int len, int &index) { |
| 199 | int result = 0; |
| 200 | while (index < len && isdigit(s[index])) { |
| 201 | result = (result * 10) + (s[index] - '0'); |
| 202 | index++; |
| 203 | } |
| 204 | if (s[index] == ';') { |
| 205 | index++; |
| 206 | } |
| 207 | return result; |
| 208 | } |
| 209 | |
| 210 | // |
| 211 | // restore a rectangle value with the given key |
| 212 | // |
| 213 | Fl_Rect Profile::restoreRect(Properties<String *> *profile, const char *key) { |
| 214 | Fl_Rect result(0, 0, 0, 0); |
| 215 | String *value = profile->get(key); |
| 216 | if (value != NULL) { |
| 217 | const char *buffer = value->c_str(); |
| 218 | int index = 0; |
| 219 | int len = strlen(buffer); |
| 220 | |
| 221 | result.x(nextInteger(buffer, len, index)); |
| 222 | result.y(nextInteger(buffer, len, index)); |
| 223 | result.w(nextInteger(buffer, len, index)); |
| 224 | result.h(nextInteger(buffer, len, index)); |
| 225 | } |
| 226 | return result; |
| 227 | } |
| 228 | |
| 229 | // |
| 230 | // load any stored font or color settings |
| 231 | // |
| 232 | void Profile::restoreStyles(Properties<String *> *profile) { |
| 233 | // restore size and face |
| 234 | loadEditTheme(_themeId); |
| 235 | _helpTheme.setId(_helpThemeId); |
| 236 | |
| 237 | restoreValue(profile, fontSizeKey, &_fontSize); |
| 238 | String *fontName = profile->get(fontNameKey); |
| 239 | if (fontName) { |
| 240 | _font = get_font(fontName->c_str()); |
| 241 | } |
| 242 | |
| 243 | for (int i = 0; i <= lastStyle; i++) { |
| 244 | char buffer[4]; |
| 245 | sprintf(buffer, "%02d" , i); |
| 246 | String *color = profile->get(buffer); |
| 247 | if (color) { |
| 248 | Fl_Color c = get_color(color->c_str(), NO_COLOR); |
| 249 | if (c != (Fl_Color)NO_COLOR) { |
| 250 | styletable[i].color = c; |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | updateTheme(); |
| 255 | } |
| 256 | |
| 257 | // |
| 258 | // restore the editor tabs |
| 259 | // |
| 260 | void Profile::restoreTabs(MainWindow *wnd, Properties<String *> *profile) { |
| 261 | bool usedEditor = false; |
| 262 | strlib::List<String *> paths; |
| 263 | profile->get(pathKey, &paths); |
| 264 | |
| 265 | List_each(String*, it, paths) { |
| 266 | String *nextString = (*it); |
| 267 | const char *buffer = nextString->c_str(); |
| 268 | int index = 0; |
| 269 | int len = strlen(buffer); |
| 270 | int logPrint = nextInteger(buffer, len, index); |
| 271 | int scrollLock = nextInteger(buffer, len, index); |
| 272 | int hideIde = nextInteger(buffer, len, index); |
| 273 | int gotoLine = nextInteger(buffer, len, index); |
| 274 | int insertPos = nextInteger(buffer, len, index); |
| 275 | int topLineNo = nextInteger(buffer, len, index); |
| 276 | |
| 277 | const char *path = buffer + index; |
| 278 | |
| 279 | EditorWidget *editWidget = 0; |
| 280 | if (usedEditor) { |
| 281 | // constructor will call loadConfig |
| 282 | Fl_Group *group = wnd->createEditor(path); |
| 283 | editWidget = wnd->getEditor(group); |
| 284 | } else { |
| 285 | // load into the initial buffer |
| 286 | editWidget = wnd->getEditor(true); |
| 287 | loadConfig(editWidget); |
| 288 | usedEditor = true; |
| 289 | } |
| 290 | |
| 291 | editWidget->loadFile(path); |
| 292 | editWidget->setHideIde(hideIde); |
| 293 | editWidget->setLogPrint(logPrint); |
| 294 | editWidget->setScrollLock(scrollLock); |
| 295 | editWidget->setBreakToLine(gotoLine); |
| 296 | editWidget->getEditor()->insert_position(insertPos); |
| 297 | editWidget->getEditor()->show_insert_position(); |
| 298 | editWidget->getEditor()->scroll(topLineNo, 0); |
| 299 | } |
| 300 | |
| 301 | // restore the active tab |
| 302 | String *activeTab = profile->get(activeTabKey); |
| 303 | if (activeTab != NULL) { |
| 304 | EditorWidget *editWidget = wnd->getEditor(activeTab->c_str()); |
| 305 | if (editWidget) { |
| 306 | wnd->showEditTab(editWidget); |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // |
| 312 | // restore the int value |
| 313 | // |
| 314 | void Profile::restoreValue(Properties<String *> *p, const char *key, int *value) { |
| 315 | String *s = p->get(key); |
| 316 | if (s) { |
| 317 | *value = s->toInteger(); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | // |
| 322 | // restore the main window position |
| 323 | // |
| 324 | void Profile::restoreWindowPos(MainWindow *wnd, Fl_Rect &rc) { |
| 325 | int x = rc.x(); |
| 326 | int y = rc.y(); |
| 327 | int w = rc.w(); |
| 328 | int h = rc.h(); |
| 329 | if (x > 0 && y > 0 && w > 100 && h > 100) { |
| 330 | if (x < Fl::w() && y < Fl::h()) { |
| 331 | wnd->resize(x, y, w, h); |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | // |
| 337 | // save the window position |
| 338 | // |
| 339 | void Profile::saveRect(FILE *fp, const char *key, Fl_Rect *rc) { |
| 340 | fprintf(fp, "%s=%d;%d;%d;%d\n" , key, rc->x(), rc->y(), rc->w(), rc->h()); |
| 341 | } |
| 342 | |
| 343 | // |
| 344 | // saves the current font size, face and colour configuration |
| 345 | // |
| 346 | void Profile::saveStyles(FILE *fp) { |
| 347 | uint8_t r, g, b; |
| 348 | |
| 349 | saveValue(fp, fontSizeKey, (int)styletable[0].size); |
| 350 | saveValue(fp, fontNameKey, styletable[0].font); |
| 351 | |
| 352 | for (int i = 0; i <= lastStyle; i++) { |
| 353 | Fl::get_color(styletable[i].color, r, g, b); |
| 354 | fprintf(fp, "%02d=#%02x%02x%02x\n" , i, r, g, b); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | // |
| 359 | // persist the editor tabs |
| 360 | // |
| 361 | void Profile::saveTabs(FILE *fp, MainWindow *wnd) { |
| 362 | int n = wnd->_tabGroup->children(); |
| 363 | for (int c = 0; c < n; c++) { |
| 364 | Fl_Group *group = (Fl_Group *) wnd->_tabGroup->child(c); |
| 365 | if (gw_editor == ((GroupWidgetEnum) (intptr_t) group->user_data())) { |
| 366 | EditorWidget *editWidget = (EditorWidget *) group->child(0); |
| 367 | |
| 368 | bool logPrint = editWidget->isLogPrint(); |
| 369 | bool scrollLock = editWidget->isScrollLock(); |
| 370 | bool hideIde = editWidget->isHideIDE(); |
| 371 | bool gotoLine = editWidget->isBreakToLine(); |
| 372 | int insertPos = editWidget->getEditor()->insert_position(); |
| 373 | int topLineNo = editWidget->top_line(); |
| 374 | |
| 375 | fprintf(fp, "%s='%d;%d;%d;%d;%d;%d;%s'\n" , pathKey, |
| 376 | logPrint, scrollLock, hideIde, gotoLine, insertPos, topLineNo, editWidget->getFilename()); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | // save the active tab |
| 381 | EditorWidget *editWidget = wnd->getEditor(false); |
| 382 | if (editWidget) { |
| 383 | saveValue(fp, activeTabKey, editWidget->getFilename()); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | // |
| 388 | // persist a single value |
| 389 | // |
| 390 | void Profile::saveValue(FILE *fp, const char *key, const char *value) { |
| 391 | fprintf(fp, "%s='%s'\n" , key, value); |
| 392 | } |
| 393 | |
| 394 | // |
| 395 | // persist a single value |
| 396 | // |
| 397 | void Profile::saveValue(FILE *fp, const char *key, int value) { |
| 398 | fprintf(fp, "%s=%d\n" , key, value); |
| 399 | } |
| 400 | |