From 90ffcaad047caa55e65a6bf858a7b976e062007a Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Fri, 29 Jan 2021 18:33:56 -0500 Subject: tabs working pretty well (not done yet) --- Untitled | 1 + command.c | 3 +++ command.h | 9 +++++++++ main.c | 55 ++++++++++++++++++++++++++++++++++++++++++------------- menu.c | 6 ++++++ ted-base.c | 34 ++++++++++++++++++++++++++-------- ted.cfg | 7 +++++++ 7 files changed, 94 insertions(+), 21 deletions(-) diff --git a/Untitled b/Untitled index 74aaf7e..3af9812 100644 --- a/Untitled +++ b/Untitled @@ -1,2 +1,3 @@ aa 1 2 3 4 56 78 910 1112 +testing diff --git a/command.c b/command.c index 1acca2f..1c5f28b 100644 --- a/command.c +++ b/command.c @@ -140,6 +140,9 @@ void command_execute(Ted *ted, Command c, i64 argument) { case CMD_OPEN: menu_open(ted, MENU_OPEN); break; + case CMD_NEW: + ted_new_file(ted); + break; case CMD_SAVE: if (buffer) { if (buffer->filename && streq(buffer->filename, "Untitled")) { diff --git a/command.h b/command.h index 9793d2a..04a24f8 100644 --- a/command.h +++ b/command.h @@ -41,12 +41,17 @@ ENUM_U16 { CMD_OPEN, // open a file CMD_SAVE, // save current buffer CMD_SAVE_AS, + CMD_NEW, CMD_UNDO, CMD_REDO, CMD_COPY, CMD_CUT, CMD_PASTE, + CMD_TAB_CLOSE, + CMD_TAB_NEXT, + CMD_TAB_PREV, + CMD_TEXT_SIZE_INCREASE, CMD_TEXT_SIZE_DECREASE, @@ -92,6 +97,7 @@ static CommandName const command_names[CMD_COUNT] = { {"backspace-word", CMD_BACKSPACE_WORD}, {"delete-word", CMD_DELETE_WORD}, {"open", CMD_OPEN}, + {"new", CMD_NEW}, {"save", CMD_SAVE}, {"save-as", CMD_SAVE_AS}, {"undo", CMD_UNDO}, @@ -99,6 +105,9 @@ static CommandName const command_names[CMD_COUNT] = { {"copy", CMD_COPY}, {"cut", CMD_CUT}, {"paste", CMD_PASTE}, + {"tab-close", CMD_TAB_CLOSE}, + {"tab-next", CMD_TAB_NEXT}, + {"tab-prev", CMD_TAB_PREV}, {"increase-text-size", CMD_TEXT_SIZE_INCREASE}, {"decrease-text-size", CMD_TEXT_SIZE_DECREASE}, {"escape", CMD_ESCAPE} diff --git a/main.c b/main.c index eab78ba..dfe314b 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,7 @@ // @TODO: -// - tabs, split // - when closing tabs/window, warn on unsaved changes +// - try opening a file you don't have read permission for +// - split // - Windows installation #include "base.h" no_warn_start @@ -73,7 +74,17 @@ static Rect error_box_rect(Ted *ted) { V2(menu_get_width(ted), 3 * char_height + 2 * padding)); } -static void node_render(Ted *ted, Node *node, Rect r) { +static void node_switch_to_tab(Ted *ted, Node *node, u16 new_tab_index) { + node->active_tab = new_tab_index; + if (node == ted->active_node) { + // switch active buffer + assert(node->tabs); + u16 buffer_idx = node->tabs[new_tab_index]; + ted->active_buffer = &ted->buffers[buffer_idx]; + } +} + +static void node_frame(Ted *ted, Node *node, Rect r) { if (node->tabs) { bool is_active = node == ted->active_node; Settings const *settings = &ted->settings; @@ -89,6 +100,13 @@ static void node_render(Ted *ted, Node *node, Rect r) { { // tab bar u16 ntabs = (u16)arr_len(node->tabs); float tab_width = r.size.x / ntabs; + for (u16 c = 0; c < ted->nmouse_clicks[SDL_BUTTON_LEFT]; ++c) { + v2 click = ted->mouse_clicks[SDL_BUTTON_LEFT][c]; + if (rect_contains_point(tab_bar_rect, click)) { + u16 tab_index = (u16)((click.x - r.pos.x) / tab_width); + node_switch_to_tab(ted, node, tab_index); + } + } for (u16 i = 0; i < ntabs; ++i) { TextBuffer *buffer = &ted->buffers[node->tabs[i]]; char tab_title[256]; @@ -116,6 +134,7 @@ static void node_render(Ted *ted, Node *node, Rect r) { TextRenderState text_state = text_render_state_default; text_state.max_x = rect_x2(tab_rect); text_render_with_state(font, &text_state, tab_title, tab_rect.pos.x, tab_rect.pos.y); + } } @@ -349,13 +368,6 @@ int main(int argc, char **argv) { //printf("\033[H\033[2J"); #endif - { - int window_width_int = 0, window_height_int = 0; - SDL_GetWindowSize(window, &window_width_int, &window_height_int); - ted->window_width = (float)window_width_int; - ted->window_height = (float)window_height_int; - } - float window_width = ted->window_width, window_height = ted->window_height; SDL_Event event; Uint8 const *keyboard_state = SDL_GetKeyboardState(NULL); @@ -374,7 +386,17 @@ int main(int argc, char **argv) { //printf("%p\n",(void *)ted->drag_buffer); + float window_width = 0, window_height = 0; + { + int window_width_int = 0, window_height_int = 0; + SDL_GetWindowSize(window, &window_width_int, &window_height_int); + ted->window_width = (float)window_width_int; + ted->window_height = (float)window_height_int; + } + window_width = ted->window_width, window_height = ted->window_height; + while (SDL_PollEvent(&event)) { + TextBuffer *buffer = ted->active_buffer; u32 key_modifier = (u32)ctrl_down << KEY_MODIFIER_CTRL_BIT | (u32)shift_down << KEY_MODIFIER_SHIFT_BIT @@ -489,7 +511,6 @@ int main(int argc, char **argv) { ted->drag_buffer = NULL; } - menu_update(ted, ted->menu); u32 key_modifier = (u32)ctrl_down << KEY_MODIFIER_CTRL_BIT | (u32)shift_down << KEY_MODIFIER_SHIFT_BIT @@ -517,7 +538,15 @@ int main(int argc, char **argv) { if (keyboard_state[SDL_SCANCODE_RIGHT]) buffer_scroll(active_buffer, +scroll_amount_x, 0); } - + + // update window dimensions + { + int window_width_int = 0, window_height_int = 0; + SDL_GetWindowSize(window, &window_width_int, &window_height_int); + ted->window_width = (float)window_width_int; + ted->window_height = (float)window_height_int; + } + window_width = ted->window_width, window_height = ted->window_height; // set up GL @@ -540,12 +569,12 @@ int main(int argc, char **argv) { { float x1 = 50, y1 = 50, x2 = window_width-50, y2 = window_height-50; Node *node = ted->root; - node_render(ted, node, rect4(x1, y1, x2, y2)); + node_frame(ted, node, rect4(x1, y1, x2, y2)); } Menu menu = ted->menu; if (menu) { - menu_render(ted, menu); + menu_frame(ted, menu); } diff --git a/menu.c b/menu.c index 891d97e..415b547 100644 --- a/menu.c +++ b/menu.c @@ -168,3 +168,9 @@ static void menu_render(Ted *ted, Menu menu) { file_selector_render(ted, fs); } } + +static void menu_frame(Ted *ted, Menu menu) { + menu_update(ted, menu); + if (ted->menu) + menu_render(ted, ted->menu); +} diff --git a/ted-base.c b/ted-base.c index 371096f..4cc1bee 100644 --- a/ted-base.c +++ b/ted-base.c @@ -111,6 +111,7 @@ static i32 ted_new_node(Ted *ted) { return i; } } + ted_seterr(ted, "Too many buffers open!"); return -1; } @@ -119,13 +120,10 @@ static void node_free(Node *node) { arr_free(node->tabs); } -// returns buffer of new file, or NULL on failure -static WarnUnusedResult TextBuffer *ted_open_file(Ted *ted, char const *filename) { +// returns true on success +static bool ted_open_file(Ted *ted, char const *filename) { i32 new_buffer_index = ted_new_buffer(ted); - if (new_buffer_index < 0) { - ted_seterr(ted, "Too many buffers open!"); - return NULL; - } else { + if (new_buffer_index >= 0) { Node *node = ted->active_node; if (arr_len(node->tabs) < TED_MAX_TABS) { arr_add(node->tabs, (u16)new_buffer_index); @@ -133,12 +131,32 @@ static WarnUnusedResult TextBuffer *ted_open_file(Ted *ted, char const *filename if (node->tabs && buffer_load_file(new_buffer, filename)) { ted->active_buffer = new_buffer; node->active_tab = (u16)(arr_len(node->tabs) - 1); - return new_buffer; + return true; + } + } else { + ted_seterr(ted, "Too many tabs."); + } + } + return false; +} + +static void ted_new_file(Ted *ted) { + i32 new_buffer_index = ted_new_buffer(ted); + if (new_buffer_index >= 0) { + Node *node = ted->active_node; + if (arr_len(node->tabs) < TED_MAX_TABS) { + arr_add(node->tabs, (u16)new_buffer_index); + TextBuffer *new_buffer = &ted->buffers[new_buffer_index]; + if (node->tabs) { + buffer_new_file(new_buffer, "Untitled"); + if (!buffer_haserr(new_buffer)) { + ted->active_buffer = new_buffer; + node->active_tab = (u16)(arr_len(node->tabs) - 1); + } } } else { ted_seterr(ted, "Too many tabs."); } - return NULL; } } diff --git a/ted.cfg b/ted.cfg index 0906df4..bdc8c59 100644 --- a/ted.cfg +++ b/ted.cfg @@ -64,6 +64,7 @@ Ctrl+PageUp = 10 :page-up Ctrl+PageDown = 10 :page-down Ctrl+o = :open +Ctrl+n = :new Ctrl+s = :save Ctrl+Shift+s = :save-as Ctrl+z = :undo @@ -72,6 +73,12 @@ Ctrl+c = :copy Ctrl+x = :cut Ctrl+v = :paste +Ctrl+w = :tab-close +Ctrl+PageUp = :tab-prev +Ctrl+Left = :tab-prev +Ctrl+PageDown = :tab-next +Ctrl+Right = :tab-next + Ctrl++ = 3 :increase-text-size Ctrl+- = 3 :decrease-text-size -- cgit v1.2.3