From 2994bf93de60930af3855db7a230c27be77de4ef Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Sat, 30 Jan 2021 13:36:03 -0500 Subject: middle click to close tab --- main.c | 1 - menu.c | 8 +++++--- node.c | 28 ++++++++++++++++++++++++++-- ted.c | 2 +- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/main.c b/main.c index 1157ce4..cfabb85 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,4 @@ // @TODO: -// - middle click to close tab // - try opening a file you don't have read permission for -- check for memory leaks! // - show something informative when there's no nodes open (i.e. ted->active_node == NULL). diff --git a/menu.c b/menu.c index 5cdf83f..8fd0a3b 100644 --- a/menu.c +++ b/menu.c @@ -128,12 +128,14 @@ static void menu_update(Ted *ted, Menu menu) { case POPUP_YES: // save changes switch (ted->warn_unsaved) { - case CMD_TAB_CLOSE: + case CMD_TAB_CLOSE: { menu_close(ted, true); - if (buffer_save(ted->prev_active_buffer)) { + TextBuffer *buffer = ted->active_buffer; + command_execute(ted, CMD_SAVE, 1); + if (!buffer_unsaved_changes(buffer)) { command_execute(ted, CMD_TAB_CLOSE, 1); } - break; + } break; case CMD_QUIT: menu_close(ted, true); if (ted_save_all(ted)) { diff --git a/node.c b/node.c index f927f70..a767c7a 100644 --- a/node.c +++ b/node.c @@ -45,12 +45,14 @@ static void node_close(Ted *ted, Node *node) { } // close tab, WITHOUT checking for unsaved changes! -static void node_tab_close(Ted *ted, Node *node, u16 index) { +// returns true if the node is still open +static bool node_tab_close(Ted *ted, Node *node, u16 index) { u16 ntabs = (u16)arr_len(node->tabs); assert(index < ntabs); if (ntabs == 1) { // only 1 tab left, just close the node node_close(ted, node); + return false; } else { u16 buffer_index = node->tabs[index]; // remove tab from array @@ -66,6 +68,7 @@ static void node_tab_close(Ted *ted, Node *node, u16 index) { // fix active buffer if necessary ted->active_buffer = &ted->buffers[node->tabs[node->active_tab]]; } + return true; } } @@ -76,7 +79,6 @@ static void node_frame(Ted *ted, Node *node, Rect r) { u32 const *colors = settings->colors; Font *font = ted->font; - u16 buffer_index = node->tabs[node->active_tab]; float tab_bar_height = 20; Rect tab_bar_rect = r; @@ -92,6 +94,27 @@ static void node_frame(Ted *ted, Node *node, Rect r) { node_switch_to_tab(ted, node, tab_index); } } + for (u16 c = 0; c < ted->nmouse_clicks[SDL_BUTTON_MIDDLE]; ++c) { + v2 click = ted->mouse_clicks[SDL_BUTTON_MIDDLE][c]; + if (rect_contains_point(tab_bar_rect, click)) { + u16 tab_index = (u16)((click.x - r.pos.x) / tab_width); + u16 buffer_idx = node->tabs[tab_index]; + TextBuffer *buffer = &ted->buffers[buffer_idx]; + // close that tab + if (buffer_unsaved_changes(buffer)) { + // make sure unsaved changes dialog is opened + ted_switch_to_buffer(ted, buffer_idx); + command_execute(ted, CMD_TAB_CLOSE, 1); + } else { + if (!node_tab_close(ted, node, tab_index)) { + return; // node closed + } + } + ntabs = (u16)arr_len(node->tabs); + tab_width = r.size.x / ntabs; + } + } + for (u16 i = 0; i < ntabs; ++i) { TextBuffer *buffer = &ted->buffers[node->tabs[i]]; char tab_title[256]; @@ -123,6 +146,7 @@ static void node_frame(Ted *ted, Node *node, Rect r) { } } + u16 buffer_index = node->tabs[node->active_tab]; TextBuffer *buffer = &ted->buffers[buffer_index]; assert(ted->buffers_used[buffer_index]); Rect buffer_rect = rect_translate(r, V2(0, tab_bar_height)); diff --git a/ted.c b/ted.c index 0e8be05..bbb65d0 100644 --- a/ted.c +++ b/ted.c @@ -128,7 +128,7 @@ static i32 ted_new_node(Ted *ted) { } -static void node_tab_close(Ted *ted, Node *node, u16 index); +static bool node_tab_close(Ted *ted, Node *node, u16 index); // Open a new buffer. Fills out *tab to the index of the tab used, and *buffer_idx to the index of the buffer. // Returns true on success. -- cgit v1.2.3