Skip to content

Commit 74c46f5

Browse files
committed
UI: add file change check to editor
1 parent ce16f73 commit 74c46f5

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

src/ui/system.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This file is part of SmallBASIC
22
//
3-
// Copyright(C) 2001-2014 Chris Warren-Smith.
3+
// Copyright(C) 2001-2015 Chris Warren-Smith.
44
//
55
// This program is distributed under the terms of the GPL v2.0 or later
66
// Download the GNU Public License (GPL) from www.gnu.org
@@ -111,6 +111,7 @@ void System::editSource(strlib::String &loadPath) {
111111
TextEditInput *editWidget = new TextEditInput(_programSrc, charWidth, charHeight, 0, 0, w, h);
112112
TextEditHelpWidget *helpWidget = new TextEditHelpWidget(editWidget, charWidth, charHeight);
113113
TextEditInput *widget = editWidget;
114+
_modifiedTime = getModifiedTime();
114115

115116
editWidget->updateUI(NULL, NULL);
116117
editWidget->setLineNumbers();
@@ -144,6 +145,17 @@ void System::editSource(strlib::String &loadPath) {
144145
bool dirty = editWidget->isDirty();
145146
char *text;
146147

148+
if (_modifiedTime != 0 && _modifiedTime != getModifiedTime()) {
149+
const char *msg = "Do you want to reload the file?";
150+
if (ask("File has changed on disk", msg, false) == 0) {
151+
loadSource(loadPath.c_str());
152+
editWidget->reload(_programSrc);
153+
dirty = !editWidget->isDirty();
154+
}
155+
_modifiedTime = getModifiedTime();
156+
event.key = 0;
157+
}
158+
147159
switch (event.key) {
148160
case SB_KEY_F(2):
149161
case SB_KEY_F(3):
@@ -170,6 +182,7 @@ void System::editSource(strlib::String &loadPath) {
170182
if (!editWidget->save(loadPath)) {
171183
alert("", "Failed to save file");
172184
}
185+
_modifiedTime = getModifiedTime();
173186
break;
174187
case SB_KEY_CTRL('c'):
175188
case SB_KEY_CTRL('x'):
@@ -186,9 +199,14 @@ void System::editSource(strlib::String &loadPath) {
186199
helpWidget->show();
187200
break;
188201
case SB_KEY_CTRL('h'):
189-
_output->setStatus("Keystroke help. Esc=Close");
190-
widget = helpWidget;
191-
helpWidget->createHelp();
202+
if (widget == helpWidget) {
203+
_output->setStatus("Keyword Help. Esc=Close");
204+
helpWidget->createKeywordIndex();
205+
} else {
206+
_output->setStatus("Keystroke help. Esc=Close");
207+
widget = helpWidget;
208+
helpWidget->createHelp();
209+
}
192210
helpWidget->show();
193211
break;
194212
case SB_KEY_CTRL('l'):
@@ -954,7 +972,11 @@ void System::showSystemScreen(bool showSrc) {
954972

955973
void System::waitForBack() {
956974
while (!isBack() && !isClosing() && !isRestart()) {
957-
getNextEvent();
975+
MAEvent event = getNextEvent();
976+
if (event.type == EVENT_TYPE_KEY_PRESSED &&
977+
event.key == SB_KEY_BACKSPACE) {
978+
break;
979+
}
958980
}
959981
}
960982

src/ui/textedit.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,12 @@ void TextEditInput::gotoLine(const char *buffer) {
455455
}
456456
}
457457

458+
void TextEditInput::reload(const char *text) {
459+
_buf.clear();
460+
_buf.insertChars(0, text, strlen(text));
461+
stb_textedit_initialize_state(&_state, false);
462+
}
463+
458464
bool TextEditInput::save(const char *filePath) {
459465
bool result = true;
460466
FILE *fp = fopen(filePath, "w");

src/ui/textedit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ struct TextEditInput : public FormEditInput {
7474
const char *getText() const { return _buf._buffer; }
7575
int getTextLength() const { return _buf._len; }
7676
void gotoLine(const char *buffer);
77+
void reload(const char *text);
7778
bool save(const char *filePath);
7879
void setCursor(int pos);
7980
void setCursorRow(int row);

0 commit comments

Comments
 (0)