From f02fa3b6e1f6c3b1c1325b999f101b5ca6e02558 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Wed, 27 Jan 2021 16:51:27 -0500 Subject: started warn on overwrite --- buffer.c | 6 ++++++ command.c | 2 +- main.c | 4 +++- menu.c | 41 +++++++++++++++++++++++++++++++++-------- ted-base.c | 2 +- ted.h | 1 + ui.c | 29 ++++++++++++++++++++++++++++- util.c | 2 ++ 8 files changed, 75 insertions(+), 12 deletions(-) diff --git a/buffer.c b/buffer.c index 5f6faec..3a808c7 100644 --- a/buffer.c +++ b/buffer.c @@ -94,6 +94,7 @@ void buffer_create(TextBuffer *buffer, Ted *ted) { buffer->ted = ted; } + void line_buffer_create(TextBuffer *buffer, Ted *ted) { buffer_create(buffer, ted); buffer->is_line_buffer = true; @@ -120,6 +121,11 @@ static bool buffer_pos_valid(TextBuffer *buffer, BufferPos p) { return p.line < buffer->nlines && p.index <= buffer->lines[p.line].len; } +// are there any unsaved changes? +bool buffer_unsaved_changes(TextBuffer *buffer) { + return buffer->modified; +} + // code point at position. // returns 0 if the position is invalid. note that it can also return 0 for a valid position, if there's a null character there char32_t buffer_char_at_pos(TextBuffer *buffer, BufferPos p) { diff --git a/command.c b/command.c index 0ec5059..eb2d0bf 100644 --- a/command.c +++ b/command.c @@ -186,7 +186,7 @@ void command_execute(Ted *ted, Command c, i64 argument) { // dismiss error box *ted->error_shown = '\0'; } else if (ted->menu) { - menu_close(ted, true); + menu_escape(ted); } else if (buffer) { buffer_disable_selection(buffer); } diff --git a/main.c b/main.c index 2adbbeb..f7ed8fa 100644 --- a/main.c +++ b/main.c @@ -1,8 +1,10 @@ // @TODO: // - warn on: // - overwrite (from save as menu) -// - unsaved changes +// - file selector: +// - tab to set search term to selected // - tabs, split +// - when closing tabs, warn on unsaved changes // - Windows installation #include "base.h" no_warn_start diff --git a/menu.c b/menu.c index 287b153..9a326de 100644 --- a/menu.c +++ b/menu.c @@ -2,6 +2,7 @@ static void menu_open(Ted *ted, Menu menu) { ted->menu = menu; ted->prev_active_buffer = ted->active_buffer; ted->active_buffer = NULL; + *ted->warn_overwrite = 0; // clear warn_overwrite switch (menu) { case MENU_NONE: assert(0); break; @@ -23,6 +24,16 @@ static void menu_close(Ted *ted, bool restore_prev_active_buffer) { buffer_clear(&ted->line_buffer); } +static void menu_escape(Ted *ted) { + if (*ted->warn_overwrite) { + // just close "are you sure you want to overwrite?" + *ted->warn_overwrite = 0; + ted->active_buffer = &ted->line_buffer; + } else { + menu_close(ted, true); + } +} + static float menu_get_width(Ted *ted) { Settings *settings = &ted->settings; return minf(settings->max_menu_width, ted->window_width - 2.0f * settings->padding); @@ -44,15 +55,23 @@ static void menu_update(Ted *ted, Menu menu) { switch (menu) { case MENU_NONE: break; case MENU_SAVE_AS: { - char *selected_file = file_selector_update(ted, &ted->file_selector); - if (selected_file) { - TextBuffer *buffer = ted->prev_active_buffer; - if (buffer) { - buffer_save_as(buffer, selected_file); - menu_close(ted, true); - file_selector_free(&ted->file_selector); + if (*ted->warn_overwrite) { + } else { + char *selected_file = file_selector_update(ted, &ted->file_selector); + if (selected_file) { + TextBuffer *buffer = ted->prev_active_buffer; + if (buffer) { + if (fs_path_type(selected_file) != FS_NON_EXISTENT) { + strbuf_cpy(ted->warn_overwrite, selected_file); + ted->active_buffer = NULL; + } else { + buffer_save_as(buffer, selected_file); + menu_close(ted, true); + file_selector_free(&ted->file_selector); + } + } + free(selected_file); } - free(selected_file); } } break; case MENU_OPEN: { @@ -82,6 +101,12 @@ static void menu_render(Ted *ted, Menu menu) { rect_render(rect(V2(0, 0), V2(window_width, window_height))); glEnd(); + if (*ted->warn_overwrite) { + char body[256] = {0}; + strbuf_printf(body, "Are you sure you want to overwrite %s?", ted->warn_overwrite); + popup_render(ted, "Save as...", body); + return; + } if (menu == MENU_OPEN || menu == MENU_SAVE_AS) { float padding = settings->padding; diff --git a/ted-base.c b/ted-base.c index b3a593e..066e537 100644 --- a/ted-base.c +++ b/ted-base.c @@ -102,4 +102,4 @@ static WarnUnusedResult TextBuffer *ted_open_file(Ted *ted, char const *filename } static void menu_open(Ted *ted, Menu menu); -static void menu_close(Ted *ted, bool restore_prev_active_buffer); +static void menu_escape(Ted *ted); diff --git a/ted.h b/ted.h index 18892ec..515a195 100644 --- a/ted.h +++ b/ted.h @@ -122,6 +122,7 @@ typedef struct Ted { double error_time; // time error box was opened (in seconds -- see time_get_seconds) KeyAction key_actions[KEY_COMBO_COUNT]; bool search_cwd; // should the working directory be searched for files? set to true if the executable isn't "installed" + char warn_overwrite[TED_PATH_MAX]; // file name user is trying to overwrite char local_data_dir[TED_PATH_MAX]; char global_data_dir[TED_PATH_MAX]; char home[TED_PATH_MAX]; diff --git a/ui.c b/ui.c index 065fb50..b09c8f6 100644 --- a/ui.c +++ b/ui.c @@ -306,7 +306,7 @@ static char *file_selector_update(Ted *ted, FileSelector *fs) { free(search_term); return str_dup(path); } else { - if (fs->selected && fs->selected < fs->n_entries) { + if (fs->selected < fs->n_entries) { FileEntry *entry = &fs->entries[fs->selected]; switch (entry->type) { case FS_FILE: @@ -461,3 +461,30 @@ static void file_selector_render(Ted *ted, FileSelector *fs) { } } +typedef enum { + POPUP_NONE, + POPUP_YES, + POPUP_NO, + POPUP_CANCEL +} PopupOption; + +static PopupOption popup_update(Ted *ted) { + (void)ted; + return POPUP_NONE; +} + +static void popup_render(Ted *ted, char const *title, char const *body) { + float window_width = ted->window_width; + float window_height = ted->window_height; + Rect r = rect_centered(V2(window_width * 0.5f, window_height * 0.5f), V2(300, 200)); + Settings const *settings = &ted->settings; + u32 const *colors = settings->colors; + + glBegin(GL_QUADS); + gl_color_rgba(colors[COLOR_MENU_BG]); + rect_render(r); + glEnd(); + + (void)title; + (void)body; +} diff --git a/util.c b/util.c index 651135e..b68efd3 100644 --- a/util.c +++ b/util.c @@ -110,6 +110,8 @@ static void str_cpy(char *dst, size_t dst_sz, char const *src) { dst[n] = 0; } +#define strbuf_cpy(dst, src) str_cpy(dst, sizeof dst, src) + // advances str to the start of the next UTF8 character static void utf8_next_char_const(char const **str) { if (**str) { -- cgit v1.2.3