diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2021-01-27 16:51:27 -0500 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2021-01-27 16:51:27 -0500 |
commit | f02fa3b6e1f6c3b1c1325b999f101b5ca6e02558 (patch) | |
tree | 19372b99cef17f6312454688d5e115c10aff3ac9 | |
parent | 710ea4ef8ca812aa4f0334f93c25fd9995a8e364 (diff) |
started warn on overwrite
-rw-r--r-- | buffer.c | 6 | ||||
-rw-r--r-- | command.c | 2 | ||||
-rw-r--r-- | main.c | 4 | ||||
-rw-r--r-- | menu.c | 41 | ||||
-rw-r--r-- | ted-base.c | 2 | ||||
-rw-r--r-- | ted.h | 1 | ||||
-rw-r--r-- | ui.c | 29 | ||||
-rw-r--r-- | util.c | 2 |
8 files changed, 75 insertions, 12 deletions
@@ -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) { @@ -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); } @@ -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 @@ -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; @@ -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); @@ -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]; @@ -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; +} @@ -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) { |