diff options
-rw-r--r-- | main.c | 29 | ||||
-rw-r--r-- | ted-base.c | 46 | ||||
-rw-r--r-- | ted.h | 16 |
3 files changed, 78 insertions, 13 deletions
@@ -236,9 +236,16 @@ int main(int argc, char **argv) { { - TextBuffer *buffer = &ted->main_buffer; + u16 buffer_index = (u16)ted_new_buffer(ted); + assert(buffer_index == 0); + TextBuffer *buffer = &ted->buffers[buffer_index]; buffer_create(buffer, ted); ted->active_buffer = buffer; + u16 node_index = (u16)ted_new_node(ted); + assert(node_index == 0); + Node *node = ted->active_node = &ted->nodes[node_index]; + node->tabs = NULL; + arr_add(node->tabs, 0); if (fs_file_exists(starting_filename)) { @@ -452,7 +459,7 @@ int main(int argc, char **argv) { { float x1 = 50, y1 = 50, x2 = window_width-50, y2 = window_height-50; - buffer_render(&ted->main_buffer, x1, y1, x2, y2); + buffer_render(&ted->buffers[ted->active_node->tabs[0]], x1, y1, x2, y2); if (text_has_err()) { ted_seterr(ted, "Couldn't render text: %s", text_get_err()); } @@ -463,9 +470,12 @@ int main(int argc, char **argv) { menu_render(ted, menu); } - if (buffer_haserr(&ted->main_buffer)) { - ted_seterr_to_buferr(ted, &ted->main_buffer); - buffer_clearerr(&ted->main_buffer); + for (u16 i = 0; i < TED_MAX_BUFFERS; ++i) { + TextBuffer *buffer = &ted->buffers[i]; + if (buffer_haserr(buffer)) { + ted_seterr_to_buferr(ted, buffer); + buffer_clearerr(buffer); + } } // check if there's a new error @@ -519,7 +529,9 @@ int main(int argc, char **argv) { #if DEBUG - buffer_check_valid(&ted->main_buffer); + for (u16 i = 0; i < TED_MAX_BUFFERS; ++i) + if (ted->buffers_used[i]) + buffer_check_valid(&ted->buffers[i]); buffer_check_valid(&ted->line_buffer); #endif @@ -535,7 +547,10 @@ int main(int argc, char **argv) { SDL_GL_DeleteContext(glctx); SDL_DestroyWindow(window); SDL_Quit(); - buffer_free(&ted->main_buffer); + for (u16 i = 0; i < TED_MAX_BUFFERS; ++i) + buffer_free(&ted->buffers[i]); + for (u16 i = 0; i < TED_MAX_NODES; ++i) + node_free(&ted->nodes[i]); buffer_free(&ted->line_buffer); text_font_free(ted->font); text_font_free(ted->font_bold); @@ -89,14 +89,50 @@ static void ted_load_fonts(Ted *ted) { ted_load_font(ted, "assets/font-bold.ttf", &ted->font_bold); } +// returns the index of an available buffer, or -1 if none are available +static i32 ted_new_buffer(Ted *ted) { + bool *buffers_used = ted->buffers_used; + for (i32 i = 0; i < TED_MAX_BUFFERS; ++i) { + if (!buffers_used[i]) { + buffers_used[i] = true; + return i; + } + } + return -1; +} + +// returns the index of an available node, or -1 if none are available +static i32 ted_new_node(Ted *ted) { + bool *nodes_used = ted->nodes_used; + for (i32 i = 0; i < TED_MAX_NODES; ++i) { + if (!nodes_used[i]) { + nodes_used[i] = true; + return i; + } + } + return -1; + +} + +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) { - TextBuffer *open_to = &ted->main_buffer; - if (buffer_load_file(open_to, filename)) { - ted->active_buffer = open_to; - return open_to; - } else { + i32 new_buffer_index = ted_new_buffer(ted); + if (new_buffer_index < 0) { + ted_seterr(ted, "Too many buffers open!"); return NULL; + } else { + arr_add(ted->active_node->tabs, (u16)new_buffer_index); + TextBuffer *new_buffer = &ted->buffers[new_buffer_index]; + if (buffer_load_file(new_buffer, filename)) { + ted->active_buffer = new_buffer; + return new_buffer; + } else { + return NULL; + } } } @@ -100,6 +100,15 @@ typedef struct { bool create_menu; // this is for creating files, not opening files } FileSelector; +// a node is a collection of tabs OR a split of two nodes +typedef struct Node { + u16 *tabs; // dynamic array of indices into ted->buffers, or NULL if this is a split + u16 left; // index into ted->nodes + u16 right; +} Node; + +#define TED_MAX_BUFFERS 256 +#define TED_MAX_NODES 256 typedef struct Ted { Font *font_bold; Font *font; @@ -109,6 +118,8 @@ typedef struct Ted { // while a menu or something is open, there is no active buffer. when the menu is closed, // the old active buffer needs to be restored. that's what this stores. TextBuffer *prev_active_buffer; + Node *root; + Node *active_node; Settings settings; float window_width, window_height; v2 mouse_pos; @@ -118,7 +129,6 @@ typedef struct Ted { Menu menu; FileSelector file_selector; TextBuffer line_buffer; // general-purpose line buffer for inputs -- used for menus - TextBuffer main_buffer; 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" @@ -127,6 +137,10 @@ typedef struct Ted { char global_data_dir[TED_PATH_MAX]; char home[TED_PATH_MAX]; char cwd[TED_PATH_MAX]; // current working directory + bool nodes_used[TED_MAX_NODES]; + Node nodes[TED_MAX_NODES]; + bool buffers_used[TED_MAX_BUFFERS]; + TextBuffer buffers[TED_MAX_BUFFERS]; char error[512]; char error_shown[512]; // error display in box on screen } Ted; |