diff --git a/4coder_default.cpp b/4coder_default.cpp index cbe77bdf..ac9256e4 100644 --- a/4coder_default.cpp +++ b/4coder_default.cpp @@ -28,9 +28,7 @@ basic_seek(Application_Links *app, Command_ID seek_type, unsigned int flags){ } #define SEEK_COMMAND(n, dir, flags)\ -CUSTOM_COMMAND_SIG(seek_##n##_##dir){\ - basic_seek(app, cmdid_seek_##dir, flags);\ -} +CUSTOM_COMMAND_SIG(seek_##n##_##dir){ basic_seek(app, cmdid_seek_##dir, flags); } SEEK_COMMAND(whitespace, right, BoundryWhitespace) SEEK_COMMAND(whitespace, left, BoundryWhitespace) @@ -59,7 +57,6 @@ long_braces(Application_Links *app, char *text, int size){ push_parameter(app, par_range_start, pos); push_parameter(app, par_range_end, pos + size); push_parameter(app, par_clear_blank_lines, 0); - push_parameter(app, par_use_tabs, 1); exec_command(app, cmdid_auto_tab_range); } diff --git a/4coder_helper.h b/4coder_helper.h index 97494c20..db0cab60 100644 --- a/4coder_helper.h +++ b/4coder_helper.h @@ -308,7 +308,7 @@ query_user_general(Application_Links *app, Query_Bar *bar, int force_number){ } } } - + // NOTE(allen|a3.4.4): All we have to do to update the query bar is edit our // local Query_Bar struct! This is handy because it means our Query_Bar // is always correct for typical use without extra work updating the bar. @@ -317,7 +317,9 @@ query_user_general(Application_Links *app, Query_Bar *bar, int force_number){ break; } else if (in.key.keycode == key_back){ - --bar->string.size; + if (bar->string.size > 0){ + --bar->string.size; + } } else if (good_character){ append(&bar->string, in.key.character); diff --git a/4ed.cpp b/4ed.cpp index 943f5618..c30db684 100644 --- a/4ed.cpp +++ b/4ed.cpp @@ -748,6 +748,7 @@ COMMAND_DECL(delete_range){ range = make_range(view->cursor.pos, view->mark); if (range.start < range.end){ next_cursor_pos = range.start; + Assert(range.end <= buffer_size(&file->state.buffer)); view_replace_range(system, models, view, range.start, range.end, 0, 0, next_cursor_pos); view_cursor_move(view, next_cursor_pos); view->mark = range.start; @@ -2881,9 +2882,9 @@ app_hardcode_styles(Models *models){ style_set_name(style, make_lit_string("stb")); style->main.back_color = 0xFFD6D6D6; - style->main.margin_color = 0xFF9E9E9E; + style->main.margin_color = 0xFF5C5C5C; style->main.margin_hover_color = 0xFF7E7E7E; - style->main.margin_active_color = 0xFF5C5C5C; + style->main.margin_active_color = 0xFF9E9E9E; style->main.cursor_color = 0xFF000000; style->main.at_cursor_color = 0xFFD6D6D6; style->main.mark_color = 0xFF525252; @@ -3923,7 +3924,7 @@ App_Step_Sig(app_step){ // TOOD(allen): Deduplicate // TODO(allen): Allow a view to clean up however it wants after a command finishes, - // or after transfering to another view mid command. + // or after transfering to another view mid command? if (view != 0 && models->command_coroutine == 0){ init_query_set(&view->query_set); } @@ -3934,7 +3935,7 @@ App_Step_Sig(app_step){ update_command_data(vars, cmd); ProfileEnd(command_coroutine); - + ProfileStart(frame_hook); if (models->hooks[hook_frame]){ if ((models->hooks[hook_frame])(&app_links)){ @@ -3942,6 +3943,23 @@ App_Step_Sig(app_step){ } } ProfileStart(frame_hook); + + ProfileStart(fill_gui_command_buffers); + { + Panel *panel, *used_panels; + View *view; + b32 active; + + used_panels = &models->layout.used_sentinel; + for (dll_items(panel, used_panels)){ + view = panel->view; + active = (panel == cmd->panel); + if (step_file_view(view, active)){ + app_result.redraw = 1; + } + } + } + ProfileStart(fill_gui_command_buffers); // NOTE(allen): pass raw input to the panels ProfileStart(step); @@ -3998,7 +4016,7 @@ App_Step_Sig(app_step){ if (panel == mouse_panel && !mouse->out_of_window){ input.mouse = mouse_state; } - if (step_file_view(system, exchange, view, panel->inner, active, &input)){ + if (do_input_file_view(system, exchange, view, panel->inner, active, &input)){ app_result.redraw = 1; } } @@ -4431,7 +4449,6 @@ App_Step_Sig(app_step){ { if (!file && string.str){ file = working_set_lookup_file(working_set, string); - if (!file){ file = working_set_contains(system, working_set, string); } @@ -4450,7 +4467,6 @@ App_Step_Sig(app_step){ { if (!file && string.str){ file = working_set_lookup_file(working_set, string); - if (!file){ file = working_set_contains(system, working_set, string); } @@ -4475,7 +4491,6 @@ App_Step_Sig(app_step){ if (!file && string.str){ file = working_set_lookup_file(working_set, string); - if (!file){ file = working_set_contains(system, working_set, string); } @@ -4485,7 +4500,9 @@ App_Step_Sig(app_step){ if (buffer_needs_save(file)){ view_show_interactive(system, view, &models->map_ui, IAct_Sure_To_Kill, IInt_Sure_To_Kill, make_lit_string("Are you sure?")); +#if 0 copy(&view->dest, file->name.live_name); +#endif } else{ working_set_remove(system, working_set, file->name.source_path); @@ -4576,7 +4593,7 @@ App_Step_Sig(app_step){ draw_rectangle(target, full, back_color); draw_push_clip(target, panel->inner); - draw_file_view(system, exchange, view, cmd->view, panel->inner, active, target, &dead_input); + do_render_file_view(system, exchange, view, cmd->view, panel->inner, active, target, &dead_input); draw_pop_clip(target); u32 margin_color; diff --git a/4ed_file_view.cpp b/4ed_file_view.cpp index 9c0a387f..01958426 100644 --- a/4ed_file_view.cpp +++ b/4ed_file_view.cpp @@ -1,4232 +1,4386 @@ -/* - * Mr. 4th Dimention - Allen Webster - * - * 19.08.2015 - * - * File editing view for 4coder - * - */ - -// TOP - -enum Interactive_Action{ - IAct_Open, - IAct_Save_As, - IAct_New, - IAct_Switch, - IAct_Kill, - IAct_Sure_To_Kill, - IAct_Sure_To_Close -}; - -enum Interactive_Interaction{ - IInt_Sys_File_List, - IInt_Live_File_List, - IInt_Sure_To_Kill, - IInt_Sure_To_Close -}; - -struct View_Mode{ - i32 rewrite; -}; - -enum View_Widget_Type{ - FWIDG_NONE, - FWIDG_TIMELINES, - // never below this - FWIDG_TYPE_COUNT -}; - -struct View_Widget{ - UI_State state; - View_Widget_Type type; - i32 height_; - struct{ - b32 undo_line; - b32 history_line; - } timeline; -}; - -enum View_UI{ - VUI_None, - VUI_Theme, - VUI_Interactive, - VUI_Menu, - VUI_Config, -}; - -enum Color_View_Mode{ - CV_Mode_Library, - CV_Mode_Import_File, - CV_Mode_Export_File, - CV_Mode_Import, - CV_Mode_Export, - CV_Mode_Import_Wait, - CV_Mode_Adjusting -}; - -struct View{ - View *next, *prev; - b32 in_use; - i32 id; - - Models *models; - - Panel *panel; - Command_Map *map; - - Editing_File *file; - - UI_State ui_state; - View_UI showing_ui; - - // interactive stuff - Interactive_Interaction interaction; - Interactive_Action action; - b32 finished; - char query_[256]; - char dest_[256]; - String query; - String dest; - i32 user_action; - - // theme stuff - View *hot_file_view; - u32 *palette; - i32 palette_size; - Color_View_Mode color_mode; - Super_Color color; - Color_Highlight highlight; - b32 p4c_only; - Style_Library inspecting_styles; - b8 import_export_check[64]; - i32 import_file_id; - - // file stuff - i32 font_advance; - i32 font_height; - - Full_Cursor cursor; - i32 mark; - f32 scroll_y, target_y, prev_target_y; - f32 scroll_x, target_x, prev_target_x; - f32 preferred_x; - i32 scroll_i; - f32 scroll_min_limit; - - Full_Cursor temp_highlight; - i32 temp_highlight_end_pos; - b32 show_temp_highlight; - - View_Mode mode, next_mode; - View_Widget widget; - Query_Set query_set; - i32 scrub_max; - - b32 unwrapped_lines; - b32 show_whitespace; - b32 file_locked; - - i32 line_count, line_max; - f32 *line_wrap_y; - - Command_Map *map_for_file; - b32 reinit_scrolling; -}; - -struct View_And_ID{ - View *view; - i32 id; -}; - -#define LockLevel_Open 0 -#define LockLevel_NoWrite 1 -#define LockLevel_NoUpdate 2 - -inline i32 -view_lock_level(View *view){ - i32 result = LockLevel_Open; - if (view->showing_ui != VUI_None) result = LockLevel_NoUpdate; - else if (view->file_locked || - (view->file && view->file->settings.read_only)) result = LockLevel_NoWrite; - return(result); -} - -inline f32 -view_compute_width(View *view){ - Panel *panel = view->panel; - return (f32)(panel->inner.x1 - panel->inner.x0); -} - -inline f32 -view_compute_height(View *view){ - Panel *panel = view->panel; - return (f32)(panel->inner.y1 - panel->inner.y0); -} - -struct View_Iter{ - View *view; - - Editing_File *file; - View *skip; - Panel *used_panels; - Panel *panel; -}; - -internal View_Iter -file_view_iter_next(View_Iter iter){ - View *file_view; - - for (iter.panel = iter.panel->next; iter.panel != iter.used_panels; iter.panel = iter.panel->next){ - file_view = iter.panel->view; - if (file_view != iter.skip && (file_view->file == iter.file || iter.file == 0)){ - iter.view = file_view; - break; - } - } - - return(iter); -} - -internal View_Iter -file_view_iter_init(Editing_Layout *layout, Editing_File *file, View *skip){ - View_Iter result; - result.used_panels = &layout->used_sentinel; - result.panel = result.used_panels; - result.file = file; - result.skip = skip; - - result = file_view_iter_next(result); - - return(result); -} - -internal b32 -file_view_iter_good(View_Iter iter){ - b32 result = (iter.panel != iter.used_panels); - return(result); -} - -inline b32 -starts_new_line(u8 character){ - return (character == '\n'); -} - -inline void -file_init_strings(Editing_File *file){ - file->name.source_path = make_fixed_width_string(file->name.source_path_); - file->name.live_name = make_fixed_width_string(file->name.live_name_); - file->name.extension = make_fixed_width_string(file->name.extension_); -} - -inline void -file_set_name(Working_Set *working_set, Editing_File *file, char *filename){ - String f, ext; - - Assert(file->name.live_name.str != 0); - - f = make_string_slowly(filename); - copy_checked(&file->name.source_path, f); - - file->name.live_name.size = 0; - get_front_of_directory(&file->name.live_name, f); - - if (file->name.source_path.size == file->name.live_name.size){ - file->name.extension.size = 0; - } - else{ - ext = file_extension(f); - copy(&file->name.extension, ext); - } - - { - File_Node *node, *used_nodes; - Editing_File *file_ptr; - i32 file_x, original_len; - b32 hit_conflict; - - used_nodes = &working_set->used_sentinel; - original_len = file->name.live_name.size; - hit_conflict = 1; - file_x = 0; - while (hit_conflict){ - hit_conflict = 0; - for (dll_items(node, used_nodes)){ - file_ptr = (Editing_File*)node; - if (file_ptr != file && file_is_ready(file_ptr)){ - if (match(file->name.live_name, file_ptr->name.live_name)){ - ++file_x; - hit_conflict = 1; - break; - } - } - } - - if (hit_conflict){ - file->name.live_name.size = original_len; - append(&file->name.live_name, " <"); - append_int_to_str(file_x, &file->name.live_name); - append(&file->name.live_name, ">"); - } - } - } -} - -inline void -file_synchronize_times(System_Functions *system, Editing_File *file, char *filename){ - u64 stamp = system->file_time_stamp(filename); - if (stamp > 0){ - file->state.last_4ed_write_time = stamp; - file->state.last_4ed_edit_time = stamp; - file->state.last_sys_write_time = stamp; - } - file->state.sync = buffer_get_sync(file); -} - -internal i32 -file_save(System_Functions *system, Exchange *exchange, Mem_Options *mem, - Editing_File *file, char *filename){ - i32 result = 0; - - i32 max, size; - b32 dos_write_mode = file->settings.dos_write_mode; - char *data; - Buffer_Type *buffer = &file->state.buffer; - - if (dos_write_mode) - max = buffer_size(buffer) + buffer->line_count + 1; - else - max = buffer_size(buffer); - - data = (char*)general_memory_allocate(&mem->general, max, 0); - Assert(data); - - if (dos_write_mode) - size = buffer_convert_out(buffer, data, max); - else - buffer_stringify(buffer, 0, size = max, data); - - result = exchange_save_file(exchange, filename, str_size(filename), (byte*)data, size, max); - - if (result == 0){ - general_memory_free(&mem->general, data); - } - - file_synchronize_times(system, file, filename); - - return(result); -} - -inline b32 -file_save_and_set_names(System_Functions *system, Exchange *exchange, - Mem_Options *mem, Working_Set *working_set, Editing_File *file, - char *filename){ - b32 result = 0; - result = file_save(system, exchange, mem, file, filename); - if (result){ - file_set_name(working_set, file, filename); - } - return result; -} - -enum File_Bubble_Type{ - BUBBLE_BUFFER = 1, - BUBBLE_STARTS, - BUBBLE_WIDTHS, - BUBBLE_WRAPS, - BUBBLE_TOKENS, - BUBBLE_UNDO_STRING, - BUBBLE_UNDO, - BUBBLE_UNDO_CHILDREN, - // - FILE_BUBBLE_TYPE_END, -}; - -#define GROW_FAILED 0 -#define GROW_NOT_NEEDED 1 -#define GROW_SUCCESS 2 - -internal i32 -file_grow_starts_widths_as_needed(General_Memory *general, Buffer_Type *buffer, i32 additional_lines){ - b32 result = GROW_NOT_NEEDED; - i32 max = buffer->line_max; - i32 count = buffer->line_count; - i32 target_lines = count + additional_lines; - Assert(max == buffer->widths_max); - - if (target_lines > max || max == 0){ - max = LargeRoundUp(target_lines + max, Kbytes(1)); - - f32 *new_widths = (f32*)general_memory_reallocate( - general, buffer->line_widths, - sizeof(f32)*count, sizeof(f32)*max, BUBBLE_WIDTHS); - - i32 *new_lines = (i32*)general_memory_reallocate( - general, buffer->line_starts, - sizeof(i32)*count, sizeof(i32)*max, BUBBLE_STARTS); - - if (new_lines){ - buffer->line_starts = new_lines; - buffer->line_max = max; - } - if (new_widths){ - buffer->line_widths = new_widths; - buffer->widths_max = max; - } - if (new_lines && new_widths){ - result = GROW_SUCCESS; - } - else{ - result = GROW_FAILED; - } - } - - return(result); -} - -internal void -file_measure_starts_widths(System_Functions *system, General_Memory *general, - Buffer_Type *buffer, float *advance_data){ - ProfileMomentFunction(); - if (!buffer->line_starts){ - i32 max = buffer->line_max = Kbytes(1); - buffer->line_starts = (i32*)general_memory_allocate(general, max*sizeof(i32), BUBBLE_STARTS); - TentativeAssert(buffer->line_starts); - // TODO(allen): when unable to allocate? - } - if (!buffer->line_widths){ - i32 max = buffer->widths_max = Kbytes(1); - buffer->line_widths = (f32*)general_memory_allocate(general, max*sizeof(f32), BUBBLE_STARTS); - TentativeAssert(buffer->line_starts); - // TODO(allen): when unable to allocate? - } - - Buffer_Measure_Starts state = {}; - while (buffer_measure_starts_widths(&state, buffer, advance_data)){ - i32 count = state.count; - i32 max = buffer->line_max; - max = ((max + 1) << 1); - - { - i32 *new_lines = (i32*)general_memory_reallocate( - general, buffer->line_starts, sizeof(i32)*count, sizeof(i32)*max, BUBBLE_STARTS); - - // TODO(allen): when unable to grow? - TentativeAssert(new_lines); - buffer->line_starts = new_lines; - buffer->line_max = max; - } - - { - f32 *new_lines = (f32*) - general_memory_reallocate(general, buffer->line_widths, - sizeof(f32)*count, sizeof(f32)*max, BUBBLE_WIDTHS); - - // TODO(allen): when unable to grow? - TentativeAssert(new_lines); - buffer->line_widths = new_lines; - buffer->widths_max = max; - } - - } - buffer->line_count = state.count; - buffer->widths_count = state.count; -} - -struct Opaque_Font_Advance{ - void *data; - int stride; -}; - -inline Opaque_Font_Advance -get_opaque_font_advance(Render_Font *font){ - Opaque_Font_Advance result; - result.data = (char*)font->chardata + OffsetOfPtr(font->chardata, xadvance); - result.stride = sizeof(*font->chardata); - return result; -} - -internal void -file_remeasure_widths_(System_Functions *system, - General_Memory *general, Buffer_Type *buffer, Render_Font *font, - i32 line_start, i32 line_end, i32 line_shift){ - ProfileMomentFunction(); - file_grow_starts_widths_as_needed(general, buffer, line_shift); - buffer_remeasure_widths(buffer, font->advance_data, line_start, line_end, line_shift); -} - -inline i32 -view_wrapped_line_span(f32 line_width, f32 max_width){ - i32 line_count = CEIL32(line_width / max_width); - if (line_count == 0) line_count = 1; - return line_count; -} - -internal i32 -view_compute_lowest_line(View *view){ - i32 lowest_line = 0; - i32 last_line = view->line_count - 1; - if (last_line > 0){ - if (view->unwrapped_lines){ - lowest_line = last_line; - } - else{ - f32 wrap_y = view->line_wrap_y[last_line]; - lowest_line = FLOOR32(wrap_y / view->font_height); - f32 max_width = view_compute_width(view); - - Editing_File *file = view->file; - Assert(!file->state.is_dummy); - f32 width = file->state.buffer.line_widths[last_line]; - i32 line_span = view_wrapped_line_span(width, max_width); - lowest_line += line_span - 1; - } - } - return lowest_line; -} - -internal void -view_measure_wraps(System_Functions *system, - General_Memory *general, View *view){ - ProfileMomentFunction(); - Buffer_Type *buffer; - - buffer = &view->file->state.buffer; - i32 line_count = buffer->line_count; - - if (view->line_max < line_count){ - i32 max = view->line_max = LargeRoundUp(line_count, Kbytes(1)); - if (view->line_wrap_y){ - view->line_wrap_y = (real32*) - general_memory_reallocate_nocopy(general, view->line_wrap_y, sizeof(real32)*max, BUBBLE_WRAPS); - } - else{ - view->line_wrap_y = (real32*) - general_memory_allocate(general, sizeof(real32)*max, BUBBLE_WRAPS); - } - } - - f32 line_height = (f32)view->font_height; - f32 max_width = view_compute_width(view); - buffer_measure_wrap_y(buffer, view->line_wrap_y, line_height, max_width); - - view->line_count = line_count; -} - -internal void* -alloc_for_buffer(void *context, int *size){ - *size = LargeRoundUp(*size, Kbytes(4)); - void *data = general_memory_allocate((General_Memory*)context, *size, BUBBLE_BUFFER); - return data; -} - -internal void -file_create_from_string(System_Functions *system, Models *models, - Editing_File *file, char *filename, String val, b8 read_only = 0){ - - Font_Set *font_set = models->font_set; - Working_Set *working_set = &models->working_set; - General_Memory *general = &models->mem.general; - Partition *part = &models->mem.part; - Buffer_Init_Type init; - i32 page_size, scratch_size, init_success; - - file->state = {}; - - init = buffer_begin_init(&file->state.buffer, val.str, val.size); - for (; buffer_init_need_more(&init); ){ - page_size = buffer_init_page_size(&init); - page_size = LargeRoundUp(page_size, Kbytes(4)); - if (page_size < Kbytes(4)) page_size = Kbytes(4); - void *data = general_memory_allocate(general, page_size, BUBBLE_BUFFER); - buffer_init_provide_page(&init, data, page_size); - } - - scratch_size = partition_remaining(part); - Assert(scratch_size > 0); - init_success = buffer_end_init(&init, part->base + part->pos, scratch_size); - AllowLocal(init_success); - Assert(init_success); - - if (buffer_size(&file->state.buffer) < val.size){ - file->settings.dos_write_mode = 1; - } - - file_init_strings(file); - file_set_name(working_set, file, (char*)filename); - - file->state.font_id = models->global_font.font_id; - - file_synchronize_times(system, file, filename); - - Render_Font *font = get_font_info(font_set, file->state.font_id)->font; - float *advance_data = 0; - if (font) advance_data = font->advance_data; - file_measure_starts_widths(system, general, &file->state.buffer, advance_data); - - file->settings.read_only = read_only; - if (!read_only){ - i32 request_size = Kbytes(64); - file->state.undo.undo.max = request_size; - file->state.undo.undo.strings = (u8*)general_memory_allocate(general, request_size, BUBBLE_UNDO_STRING); - file->state.undo.undo.edit_max = request_size / sizeof(Edit_Step); - file->state.undo.undo.edits = (Edit_Step*)general_memory_allocate(general, request_size, BUBBLE_UNDO); - - file->state.undo.redo.max = request_size; - file->state.undo.redo.strings = (u8*)general_memory_allocate(general, request_size, BUBBLE_UNDO_STRING); - file->state.undo.redo.edit_max = request_size / sizeof(Edit_Step); - file->state.undo.redo.edits = (Edit_Step*)general_memory_allocate(general, request_size, BUBBLE_UNDO); - - file->state.undo.history.max = request_size; - file->state.undo.history.strings = (u8*)general_memory_allocate(general, request_size, BUBBLE_UNDO_STRING); - file->state.undo.history.edit_max = request_size / sizeof(Edit_Step); - file->state.undo.history.edits = (Edit_Step*)general_memory_allocate(general, request_size, BUBBLE_UNDO); - - file->state.undo.children.max = request_size; - file->state.undo.children.strings = (u8*)general_memory_allocate(general, request_size, BUBBLE_UNDO_STRING); - file->state.undo.children.edit_max = request_size / sizeof(Buffer_Edit); - file->state.undo.children.edits = (Buffer_Edit*)general_memory_allocate(general, request_size, BUBBLE_UNDO); - - file->state.undo.history_block_count = 1; - file->state.undo.history_head_block = 0; - file->state.undo.current_block_normal = 1; - } -} - -internal b32 -file_create_empty(System_Functions *system, - Models *models, Editing_File *file, char *filename){ - - file_create_from_string(system, models, file, filename, {}); - return (1); -} - -internal b32 -file_create_read_only(System_Functions *system, - Models *models, Editing_File *file, char *filename){ - - file_create_from_string(system, models, file, filename, {}, 1); - return (1); -} - -internal void -file_close(System_Functions *system, General_Memory *general, Editing_File *file){ - if (file->state.still_lexing){ - system->cancel_job(BACKGROUND_THREADS, file->state.lex_job); - if (file->state.swap_stack.tokens){ - general_memory_free(general, file->state.swap_stack.tokens); - file->state.swap_stack.tokens = 0; - } - } - if (file->state.token_stack.tokens){ - general_memory_free(general, file->state.token_stack.tokens); - } - - Buffer_Type *buffer = &file->state.buffer; - if (buffer->data){ - general_memory_free(general, buffer->data); - general_memory_free(general, buffer->line_starts); - general_memory_free(general, buffer->line_widths); - } - - if (file->state.undo.undo.edits){ - general_memory_free(general, file->state.undo.undo.strings); - general_memory_free(general, file->state.undo.undo.edits); - - general_memory_free(general, file->state.undo.redo.strings); - general_memory_free(general, file->state.undo.redo.edits); - - general_memory_free(general, file->state.undo.history.strings); - general_memory_free(general, file->state.undo.history.edits); - - general_memory_free(general, file->state.undo.children.strings); - general_memory_free(general, file->state.undo.children.edits); - } -} - -struct Shift_Information{ - i32 start, end, amount; -}; - -internal -Job_Callback_Sig(job_full_lex){ - Editing_File *file = (Editing_File*)data[0]; - General_Memory *general = (General_Memory*)data[1]; - - Cpp_File cpp_file; - cpp_file.data = file->state.buffer.data; - cpp_file.size = file->state.buffer.size; - - Cpp_Token_Stack tokens; - tokens.tokens = (Cpp_Token*)memory->data; - tokens.max_count = memory->size / sizeof(Cpp_Token); - tokens.count = 0; - - Cpp_Lex_Data status = cpp_lex_file_nonalloc(cpp_file, &tokens); - - while (!status.complete){ - system->grow_thread_memory(memory); - tokens.tokens = (Cpp_Token*)memory->data; - tokens.max_count = memory->size / sizeof(Cpp_Token); - status = cpp_lex_file_nonalloc(cpp_file, &tokens, status); - } - - i32 new_max = LargeRoundUp(tokens.count+1, Kbytes(1)); - - system->acquire_lock(FRAME_LOCK); - { - Assert(file->state.swap_stack.tokens == 0); - file->state.swap_stack.tokens = (Cpp_Token*) - general_memory_allocate(general, new_max*sizeof(Cpp_Token), BUBBLE_TOKENS); - } - system->release_lock(FRAME_LOCK); - - u8 *dest = (u8*)file->state.swap_stack.tokens; - u8 *src = (u8*)tokens.tokens; - - memcpy(dest, src, tokens.count*sizeof(Cpp_Token)); - - system->acquire_lock(FRAME_LOCK); - { - file->state.token_stack.count = tokens.count; - file->state.token_stack.max_count = new_max; - if (file->state.token_stack.tokens) - general_memory_free(general, file->state.token_stack.tokens); - file->state.token_stack.tokens = file->state.swap_stack.tokens; - file->state.swap_stack.tokens = 0; - } - system->release_lock(FRAME_LOCK); - - exchange->force_redraw = 1; - - // NOTE(allen): These are outside the locked section because I don't - // think getting these out of order will cause critical bugs, and I - // want to minimize what's done in locked sections. - file->state.tokens_complete = 1; - file->state.still_lexing = 0; -} - - -internal void -file_kill_tokens(System_Functions *system, - General_Memory *general, Editing_File *file){ - file->settings.tokens_exist = 0; - if (file->state.still_lexing){ - system->cancel_job(BACKGROUND_THREADS, file->state.lex_job); - if (file->state.swap_stack.tokens){ - general_memory_free(general, file->state.swap_stack.tokens); - file->state.swap_stack.tokens = 0; - } - } - if (file->state.token_stack.tokens){ - general_memory_free(general, file->state.token_stack.tokens); - } - file->state.tokens_complete = 0; - file->state.token_stack = {}; -} - -#if BUFFER_EXPERIMENT_SCALPEL <= 0 -internal void -file_first_lex_parallel(System_Functions *system, - General_Memory *general, Editing_File *file){ - file->settings.tokens_exist = 1; - - if (file->state.is_loading == 0 && file->state.still_lexing == 0){ - Assert(file->state.token_stack.tokens == 0); - - file->state.tokens_complete = 0; - file->state.still_lexing = 1; - - Job_Data job; - job.callback = job_full_lex; - job.data[0] = file; - job.data[1] = general; - job.memory_request = Kbytes(64); - file->state.lex_job = system->post_job(BACKGROUND_THREADS, job); -#endif - } -} - -internal void -file_relex_parallel(System_Functions *system, - Mem_Options *mem, Editing_File *file, - i32 start_i, i32 end_i, i32 amount){ - General_Memory *general = &mem->general; - Partition *part = &mem->part; - if (file->state.token_stack.tokens == 0){ - file_first_lex_parallel(system, general, file); - return; - } - - b32 inline_lex = !file->state.still_lexing; - if (inline_lex){ - Cpp_File cpp_file; - cpp_file.data = file->state.buffer.data; - cpp_file.size = file->state.buffer.size; - - Cpp_Token_Stack *stack = &file->state.token_stack; - - Cpp_Relex_State state = - cpp_relex_nonalloc_start(cpp_file, stack, - start_i, end_i, amount, 100); - - Temp_Memory temp = begin_temp_memory(part); - i32 relex_end; - Cpp_Token_Stack relex_space; - relex_space.count = 0; - relex_space.max_count = state.space_request; - relex_space.tokens = push_array(part, Cpp_Token, relex_space.max_count); - if (cpp_relex_nonalloc_main(&state, &relex_space, &relex_end)){ - inline_lex = 0; - } - else{ - i32 delete_amount = relex_end - state.start_token_i; - i32 shift_amount = relex_space.count - delete_amount; - - if (shift_amount != 0){ - int new_count = stack->count + shift_amount; - if (new_count > stack->max_count){ - int new_max = LargeRoundUp(new_count, Kbytes(1)); - stack->tokens = (Cpp_Token*) - general_memory_reallocate(general, stack->tokens, - stack->count*sizeof(Cpp_Token), - new_max*sizeof(Cpp_Token), BUBBLE_TOKENS); - stack->max_count = new_max; - } - - int shift_size = stack->count - relex_end; - if (shift_size > 0){ - Cpp_Token *old_base = stack->tokens + relex_end; - memmove(old_base + shift_amount, old_base, - sizeof(Cpp_Token)*shift_size); - } - - stack->count += shift_amount; - } - - memcpy(state.stack->tokens + state.start_token_i, relex_space.tokens, - sizeof(Cpp_Token)*relex_space.count); - } - - end_temp_memory(temp); - } - - if (!inline_lex){ - Cpp_Token_Stack *stack = &file->state.token_stack; - Cpp_Get_Token_Result get_token_result = cpp_get_token(stack, end_i); - i32 end_token_i = get_token_result.token_index; - - if (end_token_i < 0) end_token_i = 0; - else if (end_i > stack->tokens[end_token_i].start) ++end_token_i; - - cpp_shift_token_starts(stack, end_token_i, amount); - --end_token_i; - if (end_token_i >= 0){ - Cpp_Token *token = stack->tokens + end_token_i; - if (token->start < end_i && token->start + token->size > end_i){ - token->size += amount; - } - } - - file->state.still_lexing = 1; - - Job_Data job; - job.callback = job_full_lex; - job.data[0] = file; - job.data[1] = general; - job.memory_request = Kbytes(64); - file->state.lex_job = system->post_job(BACKGROUND_THREADS, job); - } -} - -internal void -undo_stack_grow_string(General_Memory *general, Edit_Stack *stack, i32 extra_size){ - i32 old_max = stack->max; - u8 *old_str = stack->strings; - i32 new_max = old_max*2 + extra_size; - u8 *new_str = (u8*) - general_memory_reallocate(general, old_str, old_max, new_max); - stack->strings = new_str; - stack->max = new_max; -} - -internal void -undo_stack_grow_edits(General_Memory *general, Edit_Stack *stack){ - i32 old_max = stack->edit_max; - Edit_Step *old_eds = stack->edits; - i32 new_max = old_max*2 + 2; - Edit_Step *new_eds = (Edit_Step*) - general_memory_reallocate(general, old_eds, old_max*sizeof(Edit_Step), new_max*sizeof(Edit_Step)); - stack->edits = new_eds; - stack->edit_max = new_max; -} - -internal void -child_stack_grow_string(General_Memory *general, Small_Edit_Stack *stack, i32 extra_size){ - i32 old_max = stack->max; - u8 *old_str = stack->strings; - i32 new_max = old_max*2 + extra_size; - u8 *new_str = (u8*) - general_memory_reallocate(general, old_str, old_max, new_max); - stack->strings = new_str; - stack->max = new_max; -} - -internal void -child_stack_grow_edits(General_Memory *general, Small_Edit_Stack *stack, i32 amount){ - i32 old_max = stack->edit_max; - Buffer_Edit *old_eds = stack->edits; - i32 new_max = old_max*2 + amount; - Buffer_Edit *new_eds = (Buffer_Edit*) - general_memory_reallocate(general, old_eds, old_max*sizeof(Buffer_Edit), new_max*sizeof(Buffer_Edit)); - stack->edits = new_eds; - stack->edit_max = new_max; -} - -internal i32 -undo_children_push(General_Memory *general, Small_Edit_Stack *children, - Buffer_Edit *edits, i32 edit_count, u8 *strings, i32 string_size){ - i32 result = children->edit_count; - if (children->edit_count + edit_count > children->edit_max) - child_stack_grow_edits(general, children, edit_count); - - if (children->size + string_size > children->max) - child_stack_grow_string(general, children, string_size); - - memcpy(children->edits + children->edit_count, edits, edit_count*sizeof(Buffer_Edit)); - memcpy(children->strings + children->size, strings, string_size); - - Buffer_Edit *edit = children->edits + children->edit_count; - i32 start_pos = children->size; - for (i32 i = 0; i < edit_count; ++i, ++edit){ - edit->str_start += start_pos; - } - - children->edit_count += edit_count; - children->size += string_size; - - return result; -} - -struct Edit_Spec{ - u8 *str; - Edit_Step step; -}; - -internal Edit_Step* -file_post_undo(General_Memory *general, Editing_File *file, - Edit_Step step, bool32 do_merge, bool32 can_merge){ - if (step.type == ED_NORMAL){ - file->state.undo.redo.size = 0; - file->state.undo.redo.edit_count = 0; - } - - Edit_Stack *undo = &file->state.undo.undo; - Edit_Step *result = 0; - - if (step.child_count == 0){ - if (step.edit.end - step.edit.start + undo->size > undo->max) - undo_stack_grow_string(general, undo, step.edit.end - step.edit.start); - - Buffer_Edit inv; - buffer_invert_edit(&file->state.buffer, step.edit, &inv, - (char*)undo->strings, &undo->size, undo->max); - - Edit_Step inv_step = {}; - inv_step.edit = inv; - inv_step.pre_pos = step.pre_pos; - inv_step.post_pos = step.post_pos; - inv_step.can_merge = (b8)can_merge; - inv_step.type = ED_UNDO; - - bool32 did_merge = 0; - if (do_merge && undo->edit_count > 0){ - Edit_Step prev = undo->edits[undo->edit_count-1]; - if (prev.can_merge && inv_step.edit.len == 0 && prev.edit.len == 0){ - if (prev.edit.end == inv_step.edit.start){ - did_merge = 1; - inv_step.edit.start = prev.edit.start; - inv_step.pre_pos = prev.pre_pos; - } - } - } - - if (did_merge){ - result = undo->edits + (undo->edit_count-1); - *result = inv_step; - } - else{ - if (undo->edit_count == undo->edit_max) - undo_stack_grow_edits(general, undo); - result = undo->edits + (undo->edit_count++); - *result = inv_step; - } - } - else{ - Edit_Step inv_step = {}; - inv_step.type = ED_UNDO; - inv_step.first_child = step.inverse_first_child; - inv_step.inverse_first_child = step.first_child; - inv_step.special_type = step.special_type; - inv_step.child_count = step.inverse_child_count; - inv_step.inverse_child_count = step.child_count; - - if (undo->edit_count == undo->edit_max) - undo_stack_grow_edits(general, undo); - result = undo->edits + (undo->edit_count++); - *result = inv_step; - } - return result; -} - -inline void -undo_stack_pop(Edit_Stack *stack){ - if (stack->edit_count > 0){ - Edit_Step *edit = stack->edits + (--stack->edit_count); - stack->size -= edit->edit.len; - } -} - -internal void -file_post_redo(General_Memory *general, Editing_File *file, Edit_Step step){ - Edit_Stack *redo = &file->state.undo.redo; - - if (step.child_count == 0){ - if (step.edit.end - step.edit.start + redo->size > redo->max) - undo_stack_grow_string(general, redo, step.edit.end - step.edit.start); - - Buffer_Edit inv; - buffer_invert_edit(&file->state.buffer, step.edit, &inv, - (char*)redo->strings, &redo->size, redo->max); - - Edit_Step inv_step = {}; - inv_step.edit = inv; - inv_step.pre_pos = step.pre_pos; - inv_step.post_pos = step.post_pos; - inv_step.type = ED_REDO; - - if (redo->edit_count == redo->edit_max) - undo_stack_grow_edits(general, redo); - redo->edits[redo->edit_count++] = inv_step; - } - else{ - Edit_Step inv_step = {}; - inv_step.type = ED_REDO; - inv_step.first_child = step.inverse_first_child; - inv_step.inverse_first_child = step.first_child; - inv_step.special_type = step.special_type; - inv_step.child_count = step.inverse_child_count; - inv_step.inverse_child_count = step.child_count; - - if (redo->edit_count == redo->edit_max) - undo_stack_grow_edits(general, redo); - redo->edits[redo->edit_count++] = inv_step; - } -} - -inline void -file_post_history_block(Editing_File *file, i32 pos){ - Assert(file->state.undo.history_head_block < pos); - Assert(pos < file->state.undo.history.edit_count); - - Edit_Step *history = file->state.undo.history.edits; - Edit_Step *step = history + file->state.undo.history_head_block; - step->next_block = pos; - step = history + pos; - step->prev_block = file->state.undo.history_head_block; - file->state.undo.history_head_block = pos; - ++file->state.undo.history_block_count; -} - -inline void -file_unpost_history_block(Editing_File *file){ - Assert(file->state.undo.history_block_count > 1); - --file->state.undo.history_block_count; - Edit_Step *old_head = file->state.undo.history.edits + file->state.undo.history_head_block; - file->state.undo.history_head_block = old_head->prev_block; -} - -internal Edit_Step* -file_post_history(General_Memory *general, Editing_File *file, - Edit_Step step, b32 do_merge, b32 can_merge){ - Edit_Stack *history = &file->state.undo.history; - Edit_Step *result = 0; - - persist Edit_Type reverse_types[4]; - if (reverse_types[ED_UNDO] == 0){ - reverse_types[ED_NORMAL] = ED_REVERSE_NORMAL; - reverse_types[ED_REVERSE_NORMAL] = ED_NORMAL; - reverse_types[ED_UNDO] = ED_REDO; - reverse_types[ED_REDO] = ED_UNDO; - } - - if (step.child_count == 0){ - if (step.edit.end - step.edit.start + history->size > history->max) - undo_stack_grow_string(general, history, step.edit.end - step.edit.start); - - Buffer_Edit inv; - buffer_invert_edit(&file->state.buffer, step.edit, &inv, - (char*)history->strings, &history->size, history->max); - - Edit_Step inv_step = {}; - inv_step.edit = inv; - inv_step.pre_pos = step.pre_pos; - inv_step.post_pos = step.post_pos; - inv_step.can_merge = (b8)can_merge; - inv_step.type = reverse_types[step.type]; - - bool32 did_merge = 0; - if (do_merge && history->edit_count > 0){ - Edit_Step prev = history->edits[history->edit_count-1]; - if (prev.can_merge && inv_step.edit.len == 0 && prev.edit.len == 0){ - if (prev.edit.end == inv_step.edit.start){ - did_merge = 1; - inv_step.edit.start = prev.edit.start; - inv_step.pre_pos = prev.pre_pos; - } - } - } - - if (did_merge){ - result = history->edits + (history->edit_count-1); - } - else{ - if (history->edit_count == history->edit_max) - undo_stack_grow_edits(general, history); - result = history->edits + (history->edit_count++); - } - - *result = inv_step; - } - else{ - Edit_Step inv_step = {}; - inv_step.type = reverse_types[step.type]; - inv_step.first_child = step.inverse_first_child; - inv_step.inverse_first_child = step.first_child; - inv_step.special_type = step.special_type; - inv_step.inverse_child_count = step.child_count; - inv_step.child_count = step.inverse_child_count; - - if (history->edit_count == history->edit_max) - undo_stack_grow_edits(general, history); - result = history->edits + (history->edit_count++); - *result = inv_step; - } - - return result; -} - -inline Full_Cursor -view_compute_cursor_from_pos(View *view, i32 pos){ - Editing_File *file = view->file; - Models *models = view->models; - Render_Font *font = get_font_info(models->font_set, models->global_font.font_id)->font; - - Full_Cursor result = {}; - if (font){ - f32 max_width = view_compute_width(view); - result = buffer_cursor_from_pos(&file->state.buffer, pos, view->line_wrap_y, - max_width, (f32)view->font_height, font->advance_data); - } - return result; -} - -inline Full_Cursor -view_compute_cursor_from_unwrapped_xy(View *view, f32 seek_x, f32 seek_y, b32 round_down = 0){ - Editing_File *file = view->file; - Models *models = view->models; - Render_Font *font = get_font_info(models->font_set, models->global_font.font_id)->font; - - Full_Cursor result = {}; - if (font){ - f32 max_width = view_compute_width(view); - result = buffer_cursor_from_unwrapped_xy(&file->state.buffer, seek_x, seek_y, - round_down, view->line_wrap_y, - max_width, (f32)view->font_height, font->advance_data); - } - - return result; -} - -internal Full_Cursor -view_compute_cursor_from_wrapped_xy(View *view, f32 seek_x, f32 seek_y, b32 round_down = 0){ - Editing_File *file = view->file; - Models *models = view->models; - Render_Font *font = get_font_info(models->font_set, models->global_font.font_id)->font; - - Full_Cursor result = {}; - if (font){ - f32 max_width = view_compute_width(view); - result = buffer_cursor_from_wrapped_xy(&file->state.buffer, seek_x, seek_y, - round_down, view->line_wrap_y, - max_width, (f32)view->font_height, font->advance_data); - } - - return (result); -} - -internal Full_Cursor -view_compute_cursor_from_line_pos(View *view, i32 line, i32 pos){ - Editing_File *file = view->file; - Models *models = view->models; - Render_Font *font = get_font_info(models->font_set, models->global_font.font_id)->font; - - Full_Cursor result = {}; - if (font){ - f32 max_width = view_compute_width(view); - result = buffer_cursor_from_line_character(&file->state.buffer, line, pos, - view->line_wrap_y, max_width, (f32)view->font_height, font->advance_data); - } - - return (result); -} - -inline Full_Cursor -view_compute_cursor(View *view, Buffer_Seek seek){ - Full_Cursor result = {}; - - switch(seek.type){ - case buffer_seek_pos: - result = view_compute_cursor_from_pos(view, seek.pos); - break; - - case buffer_seek_wrapped_xy: - result = view_compute_cursor_from_wrapped_xy(view, seek.x, seek.y); - break; - - case buffer_seek_unwrapped_xy: - result = view_compute_cursor_from_unwrapped_xy(view, seek.x, seek.y); - break; - - case buffer_seek_line_char: - result = view_compute_cursor_from_line_pos(view, seek.line, seek.character); - break; - } - - return (result); -} - -inline Full_Cursor -view_compute_cursor_from_xy(View *view, f32 seek_x, f32 seek_y){ - Full_Cursor result; - if (view->unwrapped_lines) result = view_compute_cursor_from_unwrapped_xy(view, seek_x, seek_y); - else result = view_compute_cursor_from_wrapped_xy(view, seek_x, seek_y); - return result; -} - -inline void -view_set_temp_highlight(View *view, i32 pos, i32 end_pos){ - view->temp_highlight = view_compute_cursor_from_pos(view, pos); - view->temp_highlight_end_pos = end_pos; - view->show_temp_highlight = 1; -} - -inline i32 -view_get_cursor_pos(View *view){ - i32 result; - if (view->show_temp_highlight){ - result = view->temp_highlight.pos; - } - else{ - result = view->cursor.pos; - } - return result; -} - -inline f32 -view_get_cursor_x(View *view){ - f32 result; - Full_Cursor *cursor; - if (view->show_temp_highlight){ - cursor = &view->temp_highlight; - } - else{ - cursor = &view->cursor; - } - if (view->unwrapped_lines){ - result = cursor->unwrapped_x; - } - else{ - result = cursor->wrapped_x; - } - return result; -} - -inline f32 -view_get_cursor_y(View *view){ - Full_Cursor *cursor; - f32 result; - - if (view->show_temp_highlight) cursor = &view->temp_highlight; - else cursor = &view->cursor; - - if (view->unwrapped_lines) result = cursor->unwrapped_y; - else result = cursor->wrapped_y; - - return result; -} - -internal void -view_set_file( - // NOTE(allen): These parameters are always meaningful - View *view, Editing_File *file, Models *models, - - // NOTE(allen): Necessary when file != 0 - System_Functions *system, Hook_Function *open_hook, Application_Links *app, - - // other - b32 set_vui = 1){ - - Font_Info *fnt_info; - - // TODO(allen): This belongs somewhere else. - fnt_info = get_font_info(models->font_set, models->global_font.font_id); - view->font_advance = fnt_info->advance; - view->font_height = fnt_info->height; - - // NOTE(allen): Stuff that doesn't assume file exists. - view->file = file; - view->cursor = {}; - - // NOTE(allen): Stuff that does assume file exists. - if (file){ - //view->locked = file->settings.super_locked; - view->unwrapped_lines = file->settings.unwrapped_lines; - - if (file_is_ready(file)){ - view_measure_wraps(system, &models->mem.general, view); - view->cursor = view_compute_cursor_from_pos(view, file->state.cursor_pos); - view->reinit_scrolling = 1; - } - } - - // TODO(allen): Bypass all this nonsense, it's a hack! Hooks need parameters! - // Just accept it and pass the file to the open hook when it is loaded. - if (file){ - if (open_hook && file->settings.is_initialized == 0){ - models->buffer_param_indices[models->buffer_param_count++] = file->id.id; - open_hook(app); - models->buffer_param_count = 0; - file->settings.is_initialized = 1; - } - } - - if (set_vui){ - // TODO(allen): Fix this! There should be a way to easily separate setting a file, - // and switching to file mode, so that they don't cross over eachother like this. - view->ui_state = {}; - view->showing_ui = VUI_None; - } -} - -struct Relative_Scrolling{ - f32 scroll_x, scroll_y; - f32 target_x, target_y; - f32 scroll_min_limit; -}; - -internal Relative_Scrolling -view_get_relative_scrolling(View *view){ - Relative_Scrolling result; - f32 cursor_y; - cursor_y = view_get_cursor_y(view); - result.scroll_y = cursor_y - view->scroll_y; - result.target_y = cursor_y - view->target_y; - result.scroll_min_limit = view->scroll_min_limit; - return result; -} - -internal void -view_set_relative_scrolling(View *view, Relative_Scrolling scrolling){ - f32 cursor_y; - cursor_y = view_get_cursor_y(view); - view->scroll_y = cursor_y - scrolling.scroll_y; - view->target_y = cursor_y - scrolling.target_y; - if (view->target_y < scrolling.scroll_min_limit) view->target_y = scrolling.scroll_min_limit; -} - -inline void -view_cursor_move(View *view, Full_Cursor cursor){ - view->cursor = cursor; - view->preferred_x = view_get_cursor_x(view); - view->file->state.cursor_pos = view->cursor.pos; - view->show_temp_highlight = 0; -} - -inline void -view_cursor_move(View *view, i32 pos){ - Full_Cursor cursor = view_compute_cursor_from_pos(view, pos); - view_cursor_move(view, cursor); -} - -inline void -view_cursor_move(View *view, f32 x, f32 y, b32 round_down = 0){ - Full_Cursor cursor; - if (view->unwrapped_lines){ - cursor = view_compute_cursor_from_unwrapped_xy(view, x, y, round_down); - } - else{ - cursor = view_compute_cursor_from_wrapped_xy(view, x, y, round_down); - } - view_cursor_move(view, cursor); -} - -inline void -view_cursor_move(View *view, i32 line, i32 pos){ - Full_Cursor cursor = view_compute_cursor_from_line_pos(view, line, pos); - view_cursor_move(view, cursor); -} - -inline void -view_set_widget(View *view, View_Widget_Type type){ - view->widget.type = type; -} - - -inline i32_Rect -view_widget_rect(View *view, i32 font_height){ - Panel *panel = view->panel; - i32_Rect result = panel->inner; - - if (view->file){ - result.y0 = result.y0 + font_height + 2; - } - - return(result); -} - -enum History_Mode{ - hist_normal, - hist_backward, - hist_forward -}; - -internal void -file_update_history_before_edit(Mem_Options *mem, Editing_File *file, Edit_Step step, u8 *str, - History_Mode history_mode){ - if (!file->state.undo.undo.edits) return; - General_Memory *general = &mem->general; - - b32 can_merge = 0, do_merge = 0; - switch (step.type){ - case ED_NORMAL: - { - if (step.edit.len == 1 && str && char_is_alpha_numeric(*str)) can_merge = 1; - if (step.edit.len == 1 && str && (can_merge || char_is_whitespace(*str))) do_merge = 1; - - if (history_mode != hist_forward) - file_post_history(general, file, step, do_merge, can_merge); - - file_post_undo(general, file, step, do_merge, can_merge); - }break; - - case ED_REVERSE_NORMAL: - { - if (history_mode != hist_forward) - file_post_history(general, file, step, do_merge, can_merge); - - undo_stack_pop(&file->state.undo.undo); - - b32 restore_redos = 0; - Edit_Step *redo_end = 0; - - if (history_mode == hist_backward && file->state.undo.edit_history_cursor > 0){ - restore_redos = 1; - redo_end = file->state.undo.history.edits + (file->state.undo.edit_history_cursor - 1); - } - else if (history_mode == hist_forward && file->state.undo.history.edit_count > 0){ - restore_redos = 1; - redo_end = file->state.undo.history.edits + (file->state.undo.history.edit_count - 1); - } - - if (restore_redos){ - Edit_Step *redo_start = redo_end; - i32 steps_of_redo = 0; - i32 strings_of_redo = 0; - i32 undo_count = 0; - while (redo_start->type == ED_REDO || redo_start->type == ED_UNDO){ - if (redo_start->type == ED_REDO){ - if (undo_count > 0) --undo_count; - else{ - ++steps_of_redo; - strings_of_redo += redo_start->edit.len; - } - } - else{ - ++undo_count; - } - --redo_start; - } - - if (redo_start < redo_end){ - ++redo_start; - ++redo_end; - - if (file->state.undo.redo.edit_count + steps_of_redo > file->state.undo.redo.edit_max) - undo_stack_grow_edits(general, &file->state.undo.redo); - - if (file->state.undo.redo.size + strings_of_redo > file->state.undo.redo.max) - undo_stack_grow_string(general, &file->state.undo.redo, strings_of_redo); - - u8 *str_src = file->state.undo.history.strings + redo_end->edit.str_start; - u8 *str_dest_base = file->state.undo.redo.strings; - i32 str_redo_pos = file->state.undo.redo.size + strings_of_redo; - - Edit_Step *edit_src = redo_end; - Edit_Step *edit_dest = - file->state.undo.redo.edits + file->state.undo.redo.edit_count + steps_of_redo; - - i32 undo_count = 0; - for (i32 i = 0; i < steps_of_redo;){ - --edit_src; - str_src -= edit_src->edit.len; - if (edit_src->type == ED_REDO){ - if (undo_count > 0){ - --undo_count; - } - else{ - ++i; - - --edit_dest; - *edit_dest = *edit_src; - - str_redo_pos -= edit_dest->edit.len; - edit_dest->edit.str_start = str_redo_pos; - - memcpy(str_dest_base + str_redo_pos, str_src, edit_dest->edit.len); - } - } - else{ - ++undo_count; - } - } - Assert(undo_count == 0); - - file->state.undo.redo.size += strings_of_redo; - file->state.undo.redo.edit_count += steps_of_redo; - } - } - }break; - - case ED_UNDO: - { - if (history_mode != hist_forward) - file_post_history(general, file, step, do_merge, can_merge); - file_post_redo(general, file, step); - undo_stack_pop(&file->state.undo.undo); - }break; - - case ED_REDO: - { - if (step.edit.len == 1 && str && char_is_alpha_numeric(*str)) can_merge = 1; - if (step.edit.len == 1 && str && (can_merge || char_is_whitespace(*str))) do_merge = 1; - - if (history_mode != hist_forward) - file_post_history(general, file, step, do_merge, can_merge); - - file_post_undo(general, file, step, do_merge, can_merge); - undo_stack_pop(&file->state.undo.redo); - }break; - } - - if (history_mode != hist_forward){ - if (step.type == ED_UNDO || step.type == ED_REDO){ - if (file->state.undo.current_block_normal){ - file_post_history_block(file, file->state.undo.history.edit_count - 1); - file->state.undo.current_block_normal = 0; - } - } - else{ - if (!file->state.undo.current_block_normal){ - file_post_history_block(file, file->state.undo.history.edit_count - 1); - file->state.undo.current_block_normal = 1; - } - } - } - else{ - if (file->state.undo.history_head_block == file->state.undo.history.edit_count){ - file_unpost_history_block(file); - file->state.undo.current_block_normal = !file->state.undo.current_block_normal; - } - } - - if (history_mode == hist_normal){ - file->state.undo.edit_history_cursor = file->state.undo.history.edit_count; - } -} - -inline void -file_pre_edit_maintenance(System_Functions *system, - General_Memory *general, - Editing_File *file){ - if (file->state.still_lexing){ - system->cancel_job(BACKGROUND_THREADS, file->state.lex_job); - if (file->state.swap_stack.tokens){ - general_memory_free(general, file->state.swap_stack.tokens); - file->state.swap_stack.tokens = 0; - } - } - file->state.last_4ed_edit_time = system->time(); -} - -struct Cursor_Fix_Descriptor{ - b32 is_batch; - union{ - struct{ - Buffer_Edit *batch; - i32 batch_size; - }; - struct{ - i32 start, end; - i32 shift_amount; - }; - }; -}; - -internal void -file_edit_cursor_fix(System_Functions *system, - Partition *part, General_Memory *general, - Editing_File *file, Editing_Layout *layout, - Cursor_Fix_Descriptor desc){ - - Full_Cursor temp_cursor; - Temp_Memory cursor_temp = begin_temp_memory(part); - i32 cursor_max = layout->panel_max_count * 2; - Cursor_With_Index *cursors = push_array(part, Cursor_With_Index, cursor_max); - - f32 y_offset = 0, y_position = 0; - i32 cursor_count = 0; - - View *view; - Panel *panel, *used_panels; - used_panels = &layout->used_sentinel; - - for (dll_items(panel, used_panels)){ - view = panel->view; - if (view->file == file){ - view_measure_wraps(system, general, view); - write_cursor_with_index(cursors, &cursor_count, view->cursor.pos); - write_cursor_with_index(cursors, &cursor_count, view->mark - 1); - write_cursor_with_index(cursors, &cursor_count, view->scroll_i - 1); - } - } - - if (cursor_count > 0){ - buffer_sort_cursors(cursors, cursor_count); - if (desc.is_batch){ - buffer_batch_edit_update_cursors(cursors, cursor_count, - desc.batch, desc.batch_size); - } - else{ - buffer_update_cursors(cursors, cursor_count, - desc.start, desc.end, - desc.shift_amount + (desc.end - desc.start)); - } - buffer_unsort_cursors(cursors, cursor_count); - - cursor_count = 0; - for (dll_items(panel, used_panels)){ - view = panel->view; - if (view && view->file == file){ - view_cursor_move(view, cursors[cursor_count++].pos); - view->preferred_x = view_get_cursor_x(view); - - view->mark = cursors[cursor_count++].pos + 1; - i32 new_scroll_i = cursors[cursor_count++].pos + 1; - if (view->scroll_i != new_scroll_i){ - view->scroll_i = new_scroll_i; - temp_cursor = view_compute_cursor_from_pos(view, view->scroll_i); - y_offset = MOD(view->scroll_y, view->font_height); - - if (view->unwrapped_lines){ - y_position = temp_cursor.unwrapped_y + y_offset; - view->target_y += (y_position - view->scroll_y); - view->scroll_y = y_position; - } - else{ - y_position = temp_cursor.wrapped_y + y_offset; - view->target_y += (y_position - view->scroll_y); - view->scroll_y = y_position; - } - } - } - } - } - - end_temp_memory(cursor_temp); -} - -internal void -file_do_single_edit(System_Functions *system, - Models *models, Editing_File *file, - Edit_Spec spec, History_Mode history_mode, b32 use_high_permission = 0){ - ProfileMomentFunction(); - if (!use_high_permission && file->settings.read_only) return; - - Mem_Options *mem = &models->mem; - Editing_Layout *layout = &models->layout; - - // NOTE(allen): fixing stuff beforewards???? - file_update_history_before_edit(mem, file, spec.step, spec.str, history_mode); - file_pre_edit_maintenance(system, &mem->general, file); - - // NOTE(allen): actual text replacement - i32 shift_amount = 0; - General_Memory *general = &mem->general; - Partition *part = &mem->part; - - char *str = (char*)spec.str; - i32 start = spec.step.edit.start; - i32 end = spec.step.edit.end; - i32 str_len = spec.step.edit.len; - - i32 scratch_size = partition_remaining(part); - - Assert(scratch_size > 0); - i32 request_amount = 0; - while (buffer_replace_range(&file->state.buffer, start, end, str, str_len, &shift_amount, - part->base + part->pos, scratch_size, &request_amount)){ - void *new_data = 0; - if (request_amount > 0){ - new_data = general_memory_allocate(general, request_amount, BUBBLE_BUFFER); - } - void *old_data = buffer_edit_provide_memory(&file->state.buffer, new_data, request_amount); - if (old_data) general_memory_free(general, old_data); - } - - Buffer_Type *buffer = &file->state.buffer; - i32 line_start = buffer_get_line_index(&file->state.buffer, start); - i32 line_end = buffer_get_line_index(&file->state.buffer, end); - i32 replaced_line_count = line_end - line_start; - i32 new_line_count = buffer_count_newlines(&file->state.buffer, start, start+str_len); - i32 line_shift = new_line_count - replaced_line_count; - - Render_Font *font = get_font_info(models->font_set, file->state.font_id)->font; - - file_grow_starts_widths_as_needed(general, buffer, line_shift); - buffer_remeasure_starts(buffer, line_start, line_end, line_shift, shift_amount); - buffer_remeasure_widths(buffer, font->advance_data, line_start, line_end, line_shift); - - Panel *panel, *used_panels; - used_panels = &layout->used_sentinel; - - for (dll_items(panel, used_panels)){ - View *view = panel->view; - if (view->file == file){ - view_measure_wraps(system, general, view); - } - } - -#if BUFFER_EXPERIMENT_SCALPEL <= 0 - // NOTE(allen): fixing stuff afterwards - if (file->settings.tokens_exist) - file_relex_parallel(system, mem, file, start, end, shift_amount); -#endif - - Cursor_Fix_Descriptor desc = {}; - desc.start = start; - desc.end = end; - desc.shift_amount = shift_amount; - - file_edit_cursor_fix(system, part, general, - file, layout, desc); -} - -internal void -file_do_white_batch_edit(System_Functions *system, Models *models, Editing_File *file, - Edit_Spec spec, History_Mode history_mode, b32 use_high_permission = 0){ - ProfileMomentFunction(); - if (!use_high_permission && file->settings.read_only) return; - - Mem_Options *mem = &models->mem; - Editing_Layout *layout = &models->layout; - - // NOTE(allen): fixing stuff "beforewards"??? - Assert(spec.str == 0); - file_update_history_before_edit(mem, file, spec.step, 0, history_mode); - file_pre_edit_maintenance(system, &mem->general, file); - - // NOTE(allen): actual text replacement - General_Memory *general = &mem->general; - Partition *part = &mem->part; - - u8 *str_base = file->state.undo.children.strings; - i32 batch_size = spec.step.child_count; - Buffer_Edit *batch = file->state.undo.children.edits + spec.step.first_child; - - Assert(spec.step.first_child < file->state.undo.children.edit_count); - Assert(batch_size >= 0); - - i32 scratch_size = partition_remaining(part); - Buffer_Batch_State state = {}; - i32 request_amount; - while (buffer_batch_edit_step(&state, &file->state.buffer, batch, - (char*)str_base, batch_size, part->base + part->pos, - scratch_size, &request_amount)){ - void *new_data = 0; - if (request_amount > 0){ - new_data = general_memory_allocate(general, request_amount, BUBBLE_BUFFER); - } - void *old_data = buffer_edit_provide_memory(&file->state.buffer, new_data, request_amount); - if (old_data) general_memory_free(general, old_data); - } - - // NOTE(allen): meta data - { - Buffer_Measure_Starts state = {}; - Render_Font *font = get_font_info(models->font_set, file->state.font_id)->font; - float *advance_data = 0; - if (font) advance_data = font->advance_data; - buffer_measure_starts_widths(&state, &file->state.buffer, advance_data); - } - - // NOTE(allen): cursor fixing - { - Cursor_Fix_Descriptor desc = {}; - desc.is_batch = 1; - desc.batch = batch; - desc.batch_size = batch_size; - - file_edit_cursor_fix(system, part, general, file, layout, desc); - } - - // NOTE(allen): token fixing - if (file->state.tokens_complete){ - Cpp_Token_Stack tokens = file->state.token_stack; - Cpp_Token *token = tokens.tokens; - Cpp_Token *end_token = tokens.tokens + tokens.count; - - Buffer_Edit *edit = batch; - Buffer_Edit *end_edit = batch + batch_size; - - i32 shift_amount = 0; - i32 local_shift = 0; - - for (; token < end_token && edit < end_edit; ++edit){ - local_shift = (edit->len - (edit->end - edit->start)); - for (; token->start < edit->start && edit->start < token->start + token->size && - token < end_token; ++token){ - token->size += local_shift; - } - for (; token->start < edit->start && token < end_token; ++token){ - token->start += shift_amount; - } - shift_amount += local_shift; - } - for (; token < end_token; ++token){ - token->start += shift_amount; - } - } -} - -inline void -file_replace_range(System_Functions *system, Models *models, Editing_File *file, - i32 start, i32 end, char *str, i32 len, i32 next_cursor, b32 use_high_permission = 0){ - Edit_Spec spec = {}; - spec.step.type = ED_NORMAL; - spec.step.edit.start = start; - spec.step.edit.end = end; - spec.step.edit.len = len; - spec.step.pre_pos = file->state.cursor_pos; - spec.step.post_pos = next_cursor; - spec.str = (u8*)str; - file_do_single_edit(system, models, file, spec, hist_normal, use_high_permission); -} - -inline void -file_clear(System_Functions *system, Models *models, Editing_File *file, b32 use_high_permission = 0){ - file_replace_range(system, models, file, 0, buffer_size(&file->state.buffer), 0, 0, 0, use_high_permission); -} - -inline void -view_replace_range(System_Functions *system, Models *models, View *view, - i32 start, i32 end, char *str, i32 len, i32 next_cursor){ - file_replace_range(system, models, view->file, start, end, str, len, next_cursor); -} - -inline void -view_post_paste_effect(View *view, i32 ticks, i32 start, i32 size, u32 color){ - Editing_File *file = view->file; - - file->state.paste_effect.start = start; - file->state.paste_effect.end = start + size; - file->state.paste_effect.color = color; - file->state.paste_effect.tick_down = ticks; - file->state.paste_effect.tick_max = ticks; -} - -internal void -view_undo_redo(System_Functions *system, - Models *models, View *view, - Edit_Stack *stack, Edit_Type expected_type){ - Editing_File *file = view->file; - - if (stack->edit_count > 0){ - Edit_Step step = stack->edits[stack->edit_count-1]; - - Assert(step.type == expected_type); - - Edit_Spec spec = {}; - spec.step = step; - - if (step.child_count == 0){ - spec.step.edit.str_start = 0; - spec.str = stack->strings + step.edit.str_start; - - file_do_single_edit(system, models, file, spec, hist_normal); - - if (expected_type == ED_UNDO) view_cursor_move(view, step.pre_pos); - else view_cursor_move(view, step.post_pos); - view->mark = view->cursor.pos; - - view_post_paste_effect(view, 10, step.edit.start, step.edit.len, - models->style.main.undo_color); - } - else{ - TentativeAssert(spec.step.special_type == 1); - file_do_white_batch_edit(system, models, view->file, spec, hist_normal); - } - } -} - -inline void -view_undo(System_Functions *system, Models *models, View *view){ - view_undo_redo(system, models, view, &view->file->state.undo.undo, ED_UNDO); -} - -inline void -view_redo(System_Functions *system, Models *models, View *view){ - view_undo_redo(system, models, view, &view->file->state.undo.redo, ED_REDO); -} - -inline u8* -write_data(u8 *ptr, void *x, i32 size){ - memcpy(ptr, x, size); - return (ptr + size); -} - -#define UseFileHistoryDump 0 - -#if UseFileHistoryDump -internal void -file_dump_history(System_Functions *system, Mem_Options *mem, Editing_File *file, char *filename){ - if (!file->state.undo.undo.edits) return; - - i32 size = 0; - - size += sizeof(i32); - size += file->state.undo.undo.edit_count*sizeof(Edit_Step); - size += sizeof(i32); - size += file->state.undo.redo.edit_count*sizeof(Edit_Step); - size += sizeof(i32); - size += file->state.undo.history.edit_count*sizeof(Edit_Step); - size += sizeof(i32); - size += file->state.undo.children.edit_count*sizeof(Buffer_Edit); - - size += sizeof(i32); - size += file->state.undo.undo.size; - size += sizeof(i32); - size += file->state.undo.redo.size; - size += sizeof(i32); - size += file->state.undo.history.size; - size += sizeof(i32); - size += file->state.undo.children.size; - - Partition *part = &mem->part; - i32 remaining = partition_remaining(part); - if (size < remaining){ - u8 *data, *curs; - data = (u8*)part->base + part->pos; - curs = data; - curs = write_data(curs, &file->state.undo.undo.edit_count, 4); - curs = write_data(curs, &file->state.undo.redo.edit_count, 4); - curs = write_data(curs, &file->state.undo.history.edit_count, 4); - curs = write_data(curs, &file->state.undo.children.edit_count, 4); - curs = write_data(curs, &file->state.undo.undo.size, 4); - curs = write_data(curs, &file->state.undo.redo.size, 4); - curs = write_data(curs, &file->state.undo.history.size, 4); - curs = write_data(curs, &file->state.undo.children.size, 4); - - curs = write_data(curs, file->state.undo.undo.edits, sizeof(Edit_Step)*file->state.undo.undo.edit_count); - curs = write_data(curs, file->state.undo.redo.edits, sizeof(Edit_Step)*file->state.undo.redo.edit_count); - curs = write_data(curs, file->state.undo.history.edits, sizeof(Edit_Step)*file->state.undo.history.edit_count); - curs = write_data(curs, file->state.undo.children.edits, sizeof(Buffer_Edit)*file->state.undo.children.edit_count); - - curs = write_data(curs, file->state.undo.undo.strings, file->state.undo.undo.size); - curs = write_data(curs, file->state.undo.redo.strings, file->state.undo.redo.size); - curs = write_data(curs, file->state.undo.history.strings, file->state.undo.history.size); - curs = write_data(curs, file->state.undo.children.strings, file->state.undo.children.size); - - Assert((i32)(curs - data) == size); - system->save_file(filename, data, size); - } -} -#endif - -internal void -view_history_step(System_Functions *system, Models *models, View *view, History_Mode history_mode){ - Assert(history_mode != hist_normal); - - Editing_File *file = view->file; - - b32 do_history_step = 0; - Edit_Step step = {}; - if (history_mode == hist_backward){ - if (file->state.undo.edit_history_cursor > 0){ - do_history_step = 1; - step = file->state.undo.history.edits[--file->state.undo.edit_history_cursor]; - } - } - else{ - if (file->state.undo.edit_history_cursor < file->state.undo.history.edit_count){ - Assert(((file->state.undo.history.edit_count - file->state.undo.edit_history_cursor) & 1) == 0); - step = file->state.undo.history.edits[--file->state.undo.history.edit_count]; - file->state.undo.history.size -= step.edit.len; - ++file->state.undo.edit_history_cursor; - do_history_step = 1; - } - } - - if (do_history_step){ - Edit_Spec spec; - spec.step = step; - - if (spec.step.child_count == 0){ - spec.step.edit.str_start = 0; - spec.str = file->state.undo.history.strings + step.edit.str_start; - - file_do_single_edit(system, models, file, spec, history_mode); - - switch (spec.step.type){ - case ED_NORMAL: - case ED_REDO: - view_cursor_move(view, step.post_pos); - break; - - case ED_REVERSE_NORMAL: - case ED_UNDO: - view_cursor_move(view, step.pre_pos); - break; - } - view->mark = view->cursor.pos; - } - else{ - TentativeAssert(spec.step.special_type == 1); - file_do_white_batch_edit(system, models, view->file, spec, history_mode); - } - } -} - -// TODO(allen): write these as streamed operations -internal i32 -view_find_end_of_line(View *view, i32 pos){ -#if BUFFER_EXPERIMENT_SCALPEL <= 0 - Editing_File *file = view->file; - char *data = file->state.buffer.data; - while (pos < file->state.buffer.size && data[pos] != '\n') ++pos; - if (pos > file->state.buffer.size) pos = file->state.buffer.size; -#endif - return pos; -} - -internal i32 -view_find_beginning_of_line(View *view, i32 pos){ -#if BUFFER_EXPERIMENT_SCALPEL <= 0 - Editing_File *file = view->file; - char *data = file->state.buffer.data; - if (pos > 0){ - --pos; - while (pos > 0 && data[pos] != '\n') --pos; - if (pos != 0) ++pos; - } -#endif - return pos; -} - -internal i32 -view_find_beginning_of_next_line(View *view, i32 pos){ -#if BUFFER_EXPERIMENT_SCALPEL <= 0 - Editing_File *file = view->file; - char *data = file->state.buffer.data; - while (pos < file->state.buffer.size && - !starts_new_line(data[pos])){ - ++pos; - } - if (pos < file->state.buffer.size){ - ++pos; - } -#endif - return pos; -} - -internal String* -working_set_next_clipboard_string(General_Memory *general, Working_Set *working, i32 str_size){ - String *result = 0; - i32 clipboard_current = working->clipboard_current; - if (working->clipboard_size == 0){ - clipboard_current = 0; - working->clipboard_size = 1; - } - else{ - ++clipboard_current; - if (clipboard_current >= working->clipboard_max_size){ - clipboard_current = 0; - } - else if (working->clipboard_size <= clipboard_current){ - working->clipboard_size = clipboard_current+1; - } - } - result = &working->clipboards[clipboard_current]; - working->clipboard_current = clipboard_current; - working->clipboard_rolling = clipboard_current; - char *new_str; - if (result->str){ - new_str = (char*)general_memory_reallocate(general, result->str, result->size, str_size); - } - else{ - new_str = (char*)general_memory_allocate(general, str_size+1); - } - // TODO(allen): What if new_str == 0? - *result = make_string(new_str, 0, str_size); - return result; -} - -internal String* -working_set_clipboard_head(Working_Set *working){ - String *result = 0; - if (working->clipboard_size > 0){ - i32 clipboard_index = working->clipboard_current; - working->clipboard_rolling = clipboard_index; - result = &working->clipboards[clipboard_index]; - } - return result; -} - -internal String* -working_set_clipboard_roll_down(Working_Set *working){ - String *result = 0; - if (working->clipboard_size > 0){ - i32 clipboard_index = working->clipboard_rolling; - --clipboard_index; - if (clipboard_index < 0){ - clipboard_index = working->clipboard_size-1; - } - working->clipboard_rolling = clipboard_index; - result = &working->clipboards[clipboard_index]; - } - return result; -} - -internal void -clipboard_copy(System_Functions *system, General_Memory *general, Working_Set *working, Range range, Editing_File *file){ - i32 size = range.end - range.start; - String *dest = working_set_next_clipboard_string(general, working, size); - buffer_stringify(&file->state.buffer, range.start, range.end, dest->str); - dest->size = size; - system->post_clipboard(*dest); -} - -internal Edit_Spec -file_compute_whitespace_edit(Mem_Options *mem, Editing_File *file, i32 cursor_pos, - Buffer_Edit *edits, char *str_base, i32 str_size, - Buffer_Edit *inverse_array, char *inv_str, i32 inv_max, - i32 edit_count){ - General_Memory *general = &mem->general; - - i32 inv_str_pos = 0; - Buffer_Invert_Batch state = {}; - if (buffer_invert_batch(&state, &file->state.buffer, edits, edit_count, - inverse_array, inv_str, &inv_str_pos, inv_max)) - Assert(0); - - i32 first_child = - undo_children_push(general, &file->state.undo.children, - edits, edit_count, (u8*)(str_base), str_size); - i32 inverse_first_child = - undo_children_push(general, &file->state.undo.children, - inverse_array, edit_count, (u8*)(inv_str), inv_str_pos); - - Edit_Spec spec = {}; - spec.step.type = ED_NORMAL; - spec.step.first_child = first_child; - spec.step.inverse_first_child = inverse_first_child; - spec.step.special_type = 1; - spec.step.child_count = edit_count; - spec.step.inverse_child_count = edit_count; - spec.step.pre_pos = cursor_pos; - spec.step.post_pos = cursor_pos; - - return spec; -} - -internal void -view_clean_whitespace(System_Functions *system, Models *models, View *view){ - Mem_Options *mem = &models->mem; - Editing_File *file = view->file; - - Partition *part = &mem->part; - i32 line_count = file->state.buffer.line_count; - i32 edit_max = line_count * 2; - i32 edit_count = 0; - - Assert(file && !file->state.is_dummy); - - Temp_Memory temp = begin_temp_memory(part); - Buffer_Edit *edits = push_array(part, Buffer_Edit, edit_max); - - char *str_base = (char*)part->base + part->pos; - i32 str_size = 0; - for (i32 line_i = 0; line_i < line_count; ++line_i){ - i32 start = file->state.buffer.line_starts[line_i]; - i32 preferred_indentation; - b32 all_whitespace = 0; - b32 all_space = 0; - i32 hard_start = - buffer_find_hard_start(&file->state.buffer, start, &all_whitespace, &all_space, - &preferred_indentation, 4); - - if (all_whitespace) preferred_indentation = 0; - - if ((all_whitespace && hard_start > start) || !all_space){ - Buffer_Edit new_edit; - new_edit.str_start = str_size; - str_size += preferred_indentation; - char *str = push_array(part, char, preferred_indentation); - for (i32 j = 0; j < preferred_indentation; ++j) str[j] = ' '; - new_edit.len = preferred_indentation; - new_edit.start = start; - new_edit.end = hard_start; - edits[edit_count++] = new_edit; - } - Assert(edit_count <= edit_max); - } - - if (edit_count > 0){ - Assert(buffer_batch_debug_sort_check(edits, edit_count)); - - // NOTE(allen): computing edit spec, doing batch edit - Buffer_Edit *inverse_array = push_array(part, Buffer_Edit, edit_count); - Assert(inverse_array); - - char *inv_str = (char*)part->base + part->pos; - Edit_Spec spec = - file_compute_whitespace_edit(mem, file, view->cursor.pos, edits, str_base, str_size, - inverse_array, inv_str, part->max - part->pos, edit_count); - - file_do_white_batch_edit(system, models, view->file, spec, hist_normal); - } - - end_temp_memory(temp); -} - -internal void -view_auto_tab_tokens(System_Functions *system, - Models *models, View *view, - i32 start, i32 end, b32 empty_blank_lines, b32 use_tabs){ -#if BUFFER_EXPERIMENT_SCALPEL <= 0 - Editing_File *file = view->file; - Mem_Options *mem = &models->mem; - Partition *part = &mem->part; - Buffer *buffer = &file->state.buffer; - - Assert(file && !file->state.is_dummy); - Cpp_Token_Stack tokens = file->state.token_stack; - Assert(tokens.tokens); - - i32 line_start = buffer_get_line_index(buffer, start); - i32 line_end = buffer_get_line_index(buffer, end) + 1; - - i32 edit_max = (line_end - line_start) * 2; - i32 edit_count = 0; - - i32 indent_mark_count = line_end - line_start; - - Temp_Memory temp = begin_temp_memory(part); - i32 *indent_marks = push_array(part, i32, indent_mark_count); - { - i32 current_indent = 0; - i32 token_i; - Cpp_Token *token, *self_token; - - { - i32 start_pos = file->state.buffer.line_starts[line_start]; - Cpp_Get_Token_Result result = cpp_get_token(&tokens, start_pos); - token_i = result.token_index; - if (result.in_whitespace) token_i += 1; - self_token = tokens.tokens + token_i; - } - - i32 line = line_start - 1; - for (; line >= 0; --line){ - i32 start = file->state.buffer.line_starts[line]; - b32 all_whitespace = 0; - b32 all_space = 0; - buffer_find_hard_start(&file->state.buffer, start, - &all_whitespace, &all_space, ¤t_indent, 4); - if (!all_whitespace) break; - } - - if (line < 0){ - token_i = 0; - token = tokens.tokens + token_i; - } - else{ - i32 start_pos = file->state.buffer.line_starts[line]; - Cpp_Get_Token_Result result = cpp_get_token(&tokens, start_pos); - token_i = result.token_index; - if (result.in_whitespace) token_i += 1; - token = tokens.tokens + token_i; - - while (token >= tokens.tokens && - token->flags & CPP_TFLAG_PP_DIRECTIVE || - token->flags & CPP_TFLAG_PP_BODY){ - --token; - } - - if (token < tokens.tokens){ - ++token; - current_indent = 0; - } - else if (token->start < start_pos){ - line = buffer_get_line_index(&file->state.buffer, token->start); - i32 start = file->state.buffer.line_starts[line]; - b32 all_whitespace = 0; - b32 all_space = 0; - buffer_find_hard_start(&file->state.buffer, start, - &all_whitespace, &all_space, ¤t_indent, 4); - Assert(!all_whitespace); - } - } - - indent_marks -= line_start; - i32 line_i = line_start; - i32 next_line_start = file->state.buffer.line_starts[line_i]; - switch (token->type){ - case CPP_TOKEN_BRACKET_OPEN: current_indent += 4; break; - case CPP_TOKEN_PARENTHESE_OPEN: current_indent += 4; break; - case CPP_TOKEN_BRACE_OPEN: current_indent += 4; break; - } - - Cpp_Token *prev_token = token; - ++token; - for (; line_i < line_end; ++token_i, ++token){ - for (; token->start >= next_line_start && line_i < line_end;){ - i32 this_line_start = next_line_start; - next_line_start = file->state.buffer.line_starts[line_i+1]; - i32 this_indent; - if (prev_token && prev_token->type == CPP_TOKEN_COMMENT && - prev_token->start <= this_line_start && prev_token->start + prev_token->size > this_line_start){ - this_indent = -1; - } - else{ - this_indent = current_indent; - if (token->start < next_line_start){ - if (token->flags & CPP_TFLAG_PP_DIRECTIVE) this_indent = 0; - else{ - switch (token->type){ - case CPP_TOKEN_BRACKET_CLOSE: this_indent -= 4; break; - case CPP_TOKEN_PARENTHESE_CLOSE: this_indent -= 4; break; - case CPP_TOKEN_BRACE_CLOSE: this_indent -= 4; break; - case CPP_TOKEN_BRACE_OPEN: break; - default: - if (current_indent > 0 && prev_token){ - if (!(prev_token->flags & CPP_TFLAG_PP_BODY || - prev_token->flags & CPP_TFLAG_PP_DIRECTIVE)){ - switch (prev_token->type){ - case CPP_TOKEN_BRACKET_OPEN: case CPP_TOKEN_PARENTHESE_OPEN: - case CPP_TOKEN_BRACE_OPEN: case CPP_TOKEN_BRACE_CLOSE: - case CPP_TOKEN_SEMICOLON: case CPP_TOKEN_COLON: break; - case CPP_TOKEN_COMMA: case CPP_TOKEN_COMMENT: break; - default: this_indent += 4; - } - } - } - } - } - } - if (this_indent < 0) this_indent = 0; - } - indent_marks[line_i] = this_indent; - ++line_i; - } - - switch (token->type){ - case CPP_TOKEN_BRACKET_OPEN: current_indent += 4; break; - case CPP_TOKEN_BRACKET_CLOSE: current_indent -= 4; break; - case CPP_TOKEN_PARENTHESE_OPEN: current_indent += 4; break; - case CPP_TOKEN_PARENTHESE_CLOSE: current_indent -= 4; break; - case CPP_TOKEN_BRACE_OPEN: current_indent += 4; break; - case CPP_TOKEN_BRACE_CLOSE: current_indent -= 4; break; - } - prev_token = token; - } - } - - Buffer_Edit *edits = push_array(part, Buffer_Edit, edit_max); - - char *str_base = (char*)part->base + part->pos; - i32 str_size = 0; - for (i32 line_i = line_start; line_i < line_end; ++line_i){ - i32 start = file->state.buffer.line_starts[line_i]; - i32 preferred_indentation; - i32 correct_indentation; - b32 all_whitespace = 0; - b32 all_space = 0; - i32 tab_width = 4; - i32 hard_start = - buffer_find_hard_start(&file->state.buffer, start, &all_whitespace, &all_space, - &preferred_indentation, tab_width); - - correct_indentation = indent_marks[line_i]; - if (all_whitespace && empty_blank_lines) correct_indentation = 0; - if (correct_indentation == -1) correct_indentation = preferred_indentation; - - if ((all_whitespace && hard_start > start) || !all_space || correct_indentation != preferred_indentation){ - Buffer_Edit new_edit; - new_edit.str_start = str_size; - str_size += correct_indentation; - char *str = push_array(part, char, correct_indentation); - i32 j = 0; - if (use_tabs){ - i32 i = 0; - for (; i + tab_width <= correct_indentation; i += tab_width) str[j++] = '\t'; - for (; i < correct_indentation; ++i) str[j++] = ' '; - } - else{ - for (; j < correct_indentation; ++j) str[j] = ' '; - } - new_edit.len = j; - new_edit.start = start; - new_edit.end = hard_start; - edits[edit_count++] = new_edit; - } - - Assert(edit_count <= edit_max); - } - - if (edit_count > 0){ - Assert(buffer_batch_debug_sort_check(edits, edit_count)); - - // NOTE(allen): computing edit spec, doing batch edit - Buffer_Edit *inverse_array = push_array(part, Buffer_Edit, edit_count); - Assert(inverse_array); - - char *inv_str = (char*)part->base + part->pos; - Edit_Spec spec = - file_compute_whitespace_edit(mem, file, view->cursor.pos, edits, str_base, str_size, - inverse_array, inv_str, part->max - part->pos, edit_count); - - file_do_white_batch_edit(system, models, view->file, spec, hist_normal); - } - - { - b32 all_whitespace = 0; - b32 all_space = 0; - i32 preferred_indentation; - i32 start = view->cursor.pos; - i32 hard_start = buffer_find_hard_start( - &file->state.buffer, start, &all_whitespace, &all_space, - &preferred_indentation, 4); - - view_cursor_move(view, hard_start); - } - - end_temp_memory(temp); -#endif -} - -struct Get_Link_Result{ - b32 in_link; - i32 index; -}; - -internal u32* -style_get_color(Style *style, Cpp_Token token){ - u32 *result; - if (token.flags & CPP_TFLAG_IS_KEYWORD){ - if (token.type == CPP_TOKEN_BOOLEAN_CONSTANT){ - result = &style->main.bool_constant_color; - } - else{ - result = &style->main.keyword_color; - } - } - else if(token.flags & CPP_TFLAG_PP_DIRECTIVE){ - result = &style->main.preproc_color; - } - else{ - switch (token.type){ - case CPP_TOKEN_COMMENT: - result = &style->main.comment_color; - break; - - case CPP_TOKEN_STRING_CONSTANT: - result = &style->main.str_constant_color; - break; - - case CPP_TOKEN_CHARACTER_CONSTANT: - result = &style->main.char_constant_color; - break; - - case CPP_TOKEN_INTEGER_CONSTANT: - result = &style->main.int_constant_color; - break; - - case CPP_TOKEN_FLOATING_CONSTANT: - result = &style->main.float_constant_color; - break; - - case CPP_TOKEN_INCLUDE_FILE: - result = &style->main.include_color; - break; - - default: - result = &style->main.default_color; - } - } - return result; -} - -inline f32 -view_compute_max_target_y(i32 lowest_line, i32 line_height, f32 view_height){ - real32 max_target_y = ((lowest_line+.5f)*line_height) - view_height*.5f; - return max_target_y; -} - -internal f32 -view_compute_max_target_y(View *view){ - i32 lowest_line = view_compute_lowest_line(view); - i32 line_height = view->font_height; - f32 view_height = view_compute_height(view); - f32 max_target_y = view_compute_max_target_y( - lowest_line, line_height, view_height); - return max_target_y; -} - -internal void -remeasure_file_view(System_Functions *system, View *view, i32_Rect rect){ - if (file_is_ready(view->file)){ - Relative_Scrolling relative = view_get_relative_scrolling(view); - view_measure_wraps(system, &view->models->mem.general, view); - view_cursor_move(view, view->cursor.pos); - view->preferred_x = view_get_cursor_x(view); - view_set_relative_scrolling(view, relative); - } -} - -internal void -undo_shit(System_Functions *system, View *view, UI_State *state, UI_Layout *layout, - i32 total_count, i32 undo_count, i32 scrub_max){ - - Editing_File *file = view->file; - - if (view->widget.timeline.undo_line){ - if (do_button(1, state, layout, "- Undo", 1)){ - view->widget.timeline.undo_line = 0; - } - - if (view->widget.timeline.undo_line){ - Widget_ID wid = make_id(state, 2); - i32 new_count; - if (do_undo_slider(wid, state, layout, total_count, undo_count, 0, &new_count)){ - for (i32 i = 0; i < scrub_max && new_count < undo_count; ++i){ - view_undo(system, view->models, view); - --undo_count; - } - for (i32 i = 0; i < scrub_max && new_count > undo_count; ++i){ - view_redo(system, view->models, view); - ++undo_count; - } - } - } - } - else{ - if (do_button(1, state, layout, "+ Undo", 1)){ - view->widget.timeline.undo_line = 1; - } - } - - if (view->widget.timeline.history_line){ - if (do_button(3, state, layout, "- History", 1)){ - view->widget.timeline.history_line = 0; - } - - Widget_ID wid = make_id(state, 4); - if (view->widget.timeline.history_line){ - i32 new_count; - i32 mid = ((file->state.undo.history.edit_count + file->state.undo.edit_history_cursor) >> 1); - i32 count = file->state.undo.edit_history_cursor; - if (do_undo_slider(wid, state, layout, mid, count, &file->state.undo, &new_count)){ - for (i32 i = 0; i < scrub_max && new_count < count; ++i){ - view_history_step(system, view->models, view, hist_backward); - } - for (i32 i = 0; i < scrub_max && new_count > count; ++i){ - view_history_step(system, view->models, view, hist_forward); - } - } - } - } - else{ - if (do_button(3, state, layout, "+ History", 1)){ - view->widget.timeline.history_line = 1; - } - } -} - -internal void -draw_file_view_queries(View *view, UI_State *state, UI_Layout *layout){ - Widget_ID wid; - Query_Slot *slot; - Query_Bar *bar; - i32 i = 1; - - for (slot = view->query_set.used_slot; slot != 0; slot = slot->next){ - wid = make_id(state, i++); - bar = slot->query_bar; - do_text_field(wid, state, layout, bar->prompt, bar->string); - } -} - -inline void -view_show_menu(View *fview, Command_Map *gui_map){ - fview->ui_state = {}; - fview->map_for_file = fview->map; - fview->map = gui_map; - fview->showing_ui = VUI_Menu; -} - -inline void -view_show_config(View *fview, Command_Map *gui_map){ - fview->ui_state = {}; - fview->map_for_file = fview->map; - fview->map = gui_map; - fview->showing_ui = VUI_Config; -} - -inline void -view_show_interactive(System_Functions *system, View *view, Command_Map *gui_map, - Interactive_Action action, Interactive_Interaction interaction, String query){ - - Models *models = view->models; - - view->ui_state = {}; - view->map_for_file = view->map; - view->map = gui_map; - view->showing_ui = VUI_Interactive; - view->action = action; - view->interaction = interaction; - view->finished = 0; - - copy(&view->query, query); - view->dest.str[0] = 0; - view->dest.size = 0; - - hot_directory_clean_end(&models->hot_directory); - hot_directory_reload(system, &models->hot_directory, &models->working_set); -} - -inline void -view_show_theme(View *view, Command_Map *gui_map){ - view->ui_state = {}; - view->map_for_file = view->map; - view->map = gui_map; - view->showing_ui = VUI_Theme; - view->color_mode = CV_Mode_Library; - view->color = super_color_create(0xFF000000); -} - -inline void -view_show_file(View *view, Command_Map *file_map){ - view->ui_state = {}; - if (file_map){ - view->map = file_map; - } - else{ - view->map = view->map_for_file; - } - view->showing_ui = VUI_None; -} - -internal void -interactive_view_complete(View *view){ - Models *models = view->models; - Panel *panel = view->panel; - Editing_File *old_file = view->file; - - switch (view->action){ - case IAct_Open: - delayed_open(&models->delay1, models->hot_directory.string, panel); - delayed_touch_file(&models->delay1, old_file); - break; - - case IAct_Save_As: - delayed_save_as(&models->delay1, models->hot_directory.string, panel); - break; - - case IAct_New: - if (models->hot_directory.string.size > 0 && - !char_is_slash(models->hot_directory.string.str[models->hot_directory.string.size-1])){ - delayed_new(&models->delay1, models->hot_directory.string, panel); - } - break; - - case IAct_Switch: - delayed_switch(&models->delay1, view->dest, panel); - delayed_touch_file(&models->delay1, old_file); - break; - - case IAct_Kill: - delayed_try_kill(&models->delay1, view->dest); - break; - - case IAct_Sure_To_Close: - switch (view->user_action){ - case 0: - delayed_close(&models->delay1); - break; - - case 1: - break; - - case 2: - // TODO(allen): Save all. - break; - } - break; - - case IAct_Sure_To_Kill: - switch (view->user_action){ - case 0: - delayed_kill(&models->delay1, view->dest); - break; - - case 1: - break; - - case 2: - // TODO(allen): This is fishy! What if the save doesn't happen this time around? - // We need to ensure delayed acts happen in order I think. - delayed_save(&models->delay1, view->dest); - delayed_kill(&models->delay1, view->dest); - break; - } - break; - } - view_show_file(view, 0); - - // TODO(allen): This is here to prevent the key press from being passed to the - // underlying file which is a giant pain. - view->file = 0; -} - -internal void -update_highlighting(View *view){ - View *file_view = view->hot_file_view; - if (!file_view){ - view->highlight = {}; - return; - } - - Editing_File *file = file_view->file; - if (!file || !file_is_ready(file)){ - view->highlight = {}; - return; - } - - Models *models = view->models; - - Style *style = &models->style; - i32 pos = view_get_cursor_pos(file_view); - char c = buffer_get_char(&file->state.buffer, pos); - - if (c == '\r'){ - view->highlight.ids[0] = - raw_ptr_dif(&style->main.special_character_color, style); - } - - else if (file->state.tokens_complete){ - Cpp_Token_Stack *tokens = &file->state.token_stack; - Cpp_Get_Token_Result result = cpp_get_token(tokens, pos); - Cpp_Token token = tokens->tokens[result.token_index]; - if (!result.in_whitespace){ - u32 *color = style_get_color(style, token); - view->highlight.ids[0] = raw_ptr_dif(color, style); - if (token.type == CPP_TOKEN_JUNK){ - view->highlight.ids[1] = - raw_ptr_dif(&style->main.highlight_junk_color, style); - } - else if (char_is_whitespace(c)){ - view->highlight.ids[1] = - raw_ptr_dif(&style->main.highlight_white_color, style); - } - else{ - view->highlight.ids[1] = 0; - } - } - else{ - view->highlight.ids[0] = 0; - view->highlight.ids[1] = - raw_ptr_dif(&style->main.highlight_white_color, style); - } - } - - else{ - if (char_is_whitespace(c)){ - view->highlight.ids[0] = 0; - view->highlight.ids[1] = - raw_ptr_dif(&style->main.highlight_white_color, style); - } - else{ - view->highlight.ids[0] = - raw_ptr_dif(&style->main.default_color, style); - view->highlight.ids[1] = 0; - } - } - - if (file_view->show_temp_highlight){ - view->highlight.ids[2] = - raw_ptr_dif(&style->main.highlight_color, style); - view->highlight.ids[3] = - raw_ptr_dif(&style->main.at_highlight_color, style); - } - else if (file->state.paste_effect.tick_down > 0){ - view->highlight.ids[2] = - raw_ptr_dif(&style->main.paste_color, style); - view->highlight.ids[3] = 0; - } - else{ - view->highlight.ids[2] = 0; - view->highlight.ids[3] = 0; - } -} - -internal b32 -theme_library_shit(System_Functions *system, Exchange *exchange, - View *view, UI_State *state, UI_Layout *layout){ - - Models *models = view->models; - Mem_Options *mem = &models->mem; - - i32 result = 0; - - Library_UI ui; - ui.state = state; - ui.layout = layout; - - ui.fonts = models->font_set; - ui.hot_directory = &models->hot_directory; - ui.styles = &models->styles; - - Color_View_Mode mode = view->color_mode; - - i32_Rect bar_rect = ui.layout->rect; - bar_rect.x0 = bar_rect.x1 - 20; - do_scroll_bar(ui.state, bar_rect); - - ui.layout->y -= FLOOR32(view->ui_state.view_y); - ui.layout->rect.x1 -= 20; - - b32 case_sensitive = 0; - - switch (mode){ - case CV_Mode_Library: - { - do_label(ui.state, ui.layout, literal("Current Theme - Click to Edit")); - if (do_style_preview(&ui, &models->style)){ - view->color_mode = CV_Mode_Adjusting; - view->ui_state.selected = {}; - ui.state->view_y = 0; - result = 1; - } - - begin_row(ui.layout, 3); - if (ui.state->style->name.size >= 1){ - if (do_button(-2, ui.state, ui.layout, "Save", 2)){ - //style_library_add(ui.styles, ui.state->style); - } - } - else{ - do_button(-2, ui.state, ui.layout, "~Need's Name~", 2); - } - if (do_button(-3, ui.state, ui.layout, "Import", 2)){ - view->color_mode = CV_Mode_Import_File; - hot_directory_clean_end(&models->hot_directory); - hot_directory_reload(system, &models->hot_directory, &models->working_set); - } - if (do_button(-4, ui.state, ui.layout, "Export", 2)){ - view->color_mode = CV_Mode_Export; - hot_directory_clean_end(&models->hot_directory); - hot_directory_reload(system, &models->hot_directory, &models->working_set); - memset(view->import_export_check, 0, sizeof(view->import_export_check)); - } - - do_label(ui.state, ui.layout, literal("Theme Library - Click to Select")); - - i32 style_count = models->styles.count; - Style *style = models->styles.styles; - for (i32 i = 0; i < style_count; ++i, ++style){ - if (do_style_preview(&ui, style)){ - style_copy(&models->style, style); - result = 1; - } - } - }break; - - case CV_Mode_Import_File: - { - do_label(ui.state, ui.layout, literal("Current Theme")); - do_style_preview(&ui, &models->style); - - b32 file_selected = 0; - - do_label(ui.state, ui.layout, literal("Import Which File?")); - begin_row(ui.layout, 2); - if (do_button(-2, ui.state, ui.layout, "*.p4c only", 2, 1, view->p4c_only)){ - view->p4c_only = !view->p4c_only; - } - if (do_button(-3, ui.state, ui.layout, "Cancel", 2)){ - view->color_mode = CV_Mode_Library; - } - - b32 new_dir = 0; - if (do_file_list_box(system, ui.state, ui.layout, - ui.hot_directory, view->p4c_only, 1, case_sensitive, - &new_dir, &file_selected, 0)){ - result = 1; - } - - if (new_dir){ - hot_directory_reload(system, ui.hot_directory, ui.state->working_set); - } - if (file_selected){ - memset(&view->inspecting_styles, 0, sizeof(Style_Library)); - memset(view->import_export_check, 1, - sizeof(view->import_export_check)); - - view->import_file_id = exchange_request_file(exchange, - models->hot_directory.string.str, - models->hot_directory.string.size); - view->color_mode = CV_Mode_Import_Wait; - - } - }break; - - case CV_Mode_Import_Wait: - { - Style *styles = view->inspecting_styles.styles; - Data file = {}; - i32 file_max = 0; - - i32 count = 0; - i32 max = ArrayCount(view->inspecting_styles.styles); - - AllowLocal(styles); - AllowLocal(max); - - if (exchange_file_ready(exchange, view->import_file_id, - &file.data, &file.size, &file_max)){ - if (file.data){ - if (0 /* && style_library_import(file, ui.fonts, styles, max, &count) */){ - view->color_mode = CV_Mode_Import; - } - else{ - view->color_mode = CV_Mode_Library; - } - view->inspecting_styles.count = count; - } - else{ - Assert(!"this shouldn't happen!"); - } - - exchange_free_file(exchange, view->import_file_id); - } - }break; - - case CV_Mode_Export_File: - { - do_label(ui.state, ui.layout, literal("Current Theme")); - do_style_preview(&ui, &models->style); - - b32 file_selected = 0; - - do_label(ui.state, ui.layout, literal("Export File Name?")); - begin_row(ui.layout, 2); - if (do_button(-2, ui.state, ui.layout, "Finish Export", 2)){ - file_selected = 1; - } - if (do_button(-3, ui.state, ui.layout, "Cancel", 2)){ - view->color_mode = CV_Mode_Library; - } - - b32 new_dir = 0; - if (do_file_list_box(system, ui.state, ui.layout, - ui.hot_directory, 1, 1, case_sensitive, - &new_dir, &file_selected, ".p4c")){ - result = 1; - } - - if (new_dir){ - hot_directory_reload(system, - ui.hot_directory, ui.state->working_set); - } - if (file_selected){ - i32 count = ui.styles->count; - Temp_Memory temp = begin_temp_memory(&mem->part); - Style **styles = push_array(&mem->part, Style*, sizeof(Style*)*count); - - Style *style = ui.styles->styles; - b8 *export_check = view->import_export_check; - i32 export_count = 0; - for (i32 i = 0; i < count; ++i, ++style){ - if (export_check[i]){ - styles[export_count++] = style; - } - } - char *data = push_array(&mem->part, char, ui.hot_directory->string.size + 5); - String str = make_string(data, 0, ui.hot_directory->string.size + 5); - copy(&str, ui.hot_directory->string); - append(&str, make_lit_string(".p4c")); - /*style_library_export(system, exchange, mem, &target->font_set, str.str, styles, export_count);*/ - - end_temp_memory(temp); - view->color_mode = CV_Mode_Library; - } - }break; - - case CV_Mode_Import: - { - do_label(ui.state, ui.layout, literal("Current Theme")); - do_style_preview(&ui, &models->style); - - i32 style_count = view->inspecting_styles.count; - Style *styles = view->inspecting_styles.styles; - b8 *import_check = view->import_export_check; - - do_label(ui.state, ui.layout, literal("Pack")); - begin_row(ui.layout, 2); - if (do_button(-2, ui.state, ui.layout, "Finish Import", 2)){ - Style *style = styles; - for (i32 i = 0; i < style_count; ++i, ++style){ - //if (import_check[i]) style_library_add(ui.styles, style); - } - view->color_mode = CV_Mode_Library; - } - if (do_button(-3, ui.state, ui.layout, "Cancel", 2)){ - view->color_mode = CV_Mode_Library; - } - - Style *style = styles; - for (i32 i = 0; i < style_count; ++i, ++style){ - if (do_style_preview(&ui, style, import_check[i])){ - import_check[i] = !import_check[i]; - result = 1; - } - } - }break; - - case CV_Mode_Export: - { - do_label(ui.state, ui.layout, literal("Current Theme")); - do_style_preview(&ui, &models->style); - - do_label(ui.state, ui.layout, literal("Export Which Themes?")); - begin_row(ui.layout, 2); - if (do_button(-2, ui.state, ui.layout, "Export", 2)){ - view->color_mode = CV_Mode_Export_File; - } - if (do_button(-3, ui.state, ui.layout, "Cancel", 2)){ - view->color_mode = CV_Mode_Library; - } - - i32 style_count = models->styles.count; - Style *style = models->styles.styles; - b8 *export_check = view->import_export_check; - for (i32 i = 0; i < style_count; ++i, ++style){ - if (do_style_preview(&ui, style, export_check[i])){ - export_check[i] = !export_check[i]; - result = 1; - } - } - }break; - } - - return (result); -} - -internal b32 -theme_adjusting_shit(View *view, UI_State *state, UI_Layout *layout, Super_Color *color){ - update_highlighting(view); - - Models *models = view->models; - - Style *style = &models->style; - i32 result = 0; - - Color_UI ui; - ui.state = state; - ui.layout = layout; - - ui.fonts = models->font_set; - ui.global_font = &models->global_font; - ui.highlight = view->highlight; - ui.color = view->color; - ui.has_hover_color = 0; - ui.state->sub_id1_change = 0; - ui.hex_advance = font_get_max_width(ui.fonts, ui.state->font_id, "0123456789abcdefx"); - ui.palette = models->palette; - ui.palette_size = models->palette_size; - - i32_Rect bar_rect = ui.layout->rect; - bar_rect.x0 = bar_rect.x1 - 20; - do_scroll_bar(ui.state, bar_rect); - - ui.layout->y -= FLOOR32(view->ui_state.view_y); - ui.layout->rect.x1 -= 20; - - if (do_button(-1, ui.state, ui.layout, "Back to Library", 2)){ - view->color_mode = CV_Mode_Library; - ui.state->view_y = 0; - } - - do_style_name(&ui); - do_font_switch(&ui); - - do_color_adjuster(&ui, &style->main.back_color, - style->main.default_color, style->main.back_color, - "Background"); - do_color_adjuster(&ui, &style->main.margin_color, - style->main.default_color, style->main.margin_color, - "Margin"); - do_color_adjuster(&ui, &style->main.margin_hover_color, - style->main.default_color, style->main.margin_hover_color, - "Margin Hover"); - do_color_adjuster(&ui, &style->main.margin_active_color, - style->main.default_color, style->main.margin_active_color, - "Margin Active"); - - do_color_adjuster(&ui, &style->main.cursor_color, - style->main.at_cursor_color, style->main.cursor_color, - "Cursor"); - do_color_adjuster(&ui, &style->main.at_cursor_color, - style->main.at_cursor_color, style->main.cursor_color, - "Text At Cursor"); - do_color_adjuster(&ui, &style->main.mark_color, - style->main.mark_color, style->main.back_color, - "Mark"); - - do_color_adjuster(&ui, &style->main.highlight_color, - style->main.at_highlight_color, style->main.highlight_color, - "Highlight"); - do_color_adjuster(&ui, &style->main.at_highlight_color, - style->main.at_highlight_color, style->main.highlight_color, - "Text At Highlight"); - - do_color_adjuster(&ui, &style->main.default_color, - style->main.default_color, style->main.back_color, - "Text Default"); - do_color_adjuster(&ui, &style->main.comment_color, - style->main.comment_color, style->main.back_color, - "Comment"); - do_color_adjuster(&ui, &style->main.keyword_color, - style->main.keyword_color, style->main.back_color, - "Keyword"); - do_color_adjuster(&ui, &style->main.str_constant_color, - style->main.str_constant_color, style->main.back_color, - "String Constant"); - do_color_adjuster(&ui, &style->main.char_constant_color, - style->main.char_constant_color, style->main.back_color, - "Character Constant"); - do_color_adjuster(&ui, &style->main.int_constant_color, - style->main.int_constant_color, style->main.back_color, - "Integer Constant"); - do_color_adjuster(&ui, &style->main.float_constant_color, - style->main.float_constant_color, style->main.back_color, - "Float Constant"); - do_color_adjuster(&ui, &style->main.bool_constant_color, - style->main.bool_constant_color, style->main.back_color, - "Boolean Constant"); - do_color_adjuster(&ui, &style->main.preproc_color, - style->main.preproc_color, style->main.back_color, - "Preprocessor"); - do_color_adjuster(&ui, &style->main.include_color, - style->main.include_color, style->main.back_color, - "Include Constant"); - do_color_adjuster(&ui, &style->main.special_character_color, - style->main.special_character_color, style->main.back_color, - "Special Character"); - - do_color_adjuster(&ui, &style->main.highlight_junk_color, - style->main.default_color, style->main.highlight_junk_color, - "Junk Highlight"); - do_color_adjuster(&ui, &style->main.highlight_white_color, - style->main.default_color, style->main.highlight_white_color, - "Whitespace Highlight"); - - do_color_adjuster(&ui, &style->main.paste_color, - style->main.paste_color, style->main.back_color, - "Paste Color"); - - Interactive_Style *bar_style = &style->main.file_info_style; - do_color_adjuster(&ui, &bar_style->bar_color, - bar_style->base_color, bar_style->bar_color, - "Bar"); - do_color_adjuster(&ui, &bar_style->base_color, - bar_style->base_color, bar_style->bar_color, - "Bar Text"); - do_color_adjuster(&ui, &bar_style->pop1_color, - bar_style->pop1_color, bar_style->bar_color, - "Bar Pop 1"); - do_color_adjuster(&ui, &bar_style->pop2_color, - bar_style->pop2_color, bar_style->bar_color, - "Bar Pop 2"); - - *color = ui.hover_color; - - return (result); -} - -internal b32 -theme_shit(System_Functions *system, Exchange *exchange, - View *view, View *active, UI_State *state, UI_Layout *layout, Super_Color *color){ - b32 result = 0; - - if (view != active){ - view->hot_file_view = active; - } - - switch (view->color_mode){ - case CV_Mode_Library: - case CV_Mode_Import_File: - case CV_Mode_Export_File: - case CV_Mode_Import: - case CV_Mode_Export: - case CV_Mode_Import_Wait: - if (theme_library_shit(system, exchange, view, state, layout)){ - result = 1; - } - break; - - case CV_Mode_Adjusting: - if (theme_adjusting_shit(view, state, layout, color)){ - result = 1; - } - break; - } - - return(result); -} - -internal b32 -interactive_shit(System_Functions *system, View *view, UI_State *state, UI_Layout *layout){ - b32 result = 0; - b32 new_dir = 0; - b32 complete = 0; - - Models *models = view->models; - - do_label(state, layout, view->query, 1.f); - - b32 case_sensitive = 0; - - b32 input_stage = state->input_stage; - Key_Summary *keys = state->keys; - - switch (view->interaction){ - case IInt_Sys_File_List: - { - b32 is_new = (view->action == IAct_New); - - if (do_file_list_box(system, state, layout, - &models->hot_directory, 0, !is_new, case_sensitive, - &new_dir, &complete, 0)){ - result = 1; - } - if (new_dir){ - hot_directory_reload(system, &models->hot_directory, &models->working_set); - } - }break; - - case IInt_Live_File_List: - { - if (do_live_file_list_box(system, state, layout, &models->working_set, &view->dest, &complete)){ - result = 1; - } - }break; - - case IInt_Sure_To_Close: - { - i32 action = -1; - char s_[256]; - String s; - s = make_fixed_width_string(s_); - append(&s, "There are unsaved changes in, close anyway?"); - do_label(state, layout, s, 1.f); - - i32 id = 0; - if (do_list_option(++id, state, layout, make_lit_string("(Y)es"))){ - action = 0; - } - - if (do_list_option(++id, state, layout, make_lit_string("(N)o"))){ - action = 1; - } - - if (action == -1 && input_stage){ - i32 key_count = keys->count; - for (i32 i = 0; i < key_count; ++i){ - Key_Event_Data key = keys->keys[i]; - switch (key.character){ - case 'y': case 'Y': action = 0; break; - case 'n': case 'N': action = 1; break; - } - if (action == -1 && key.keycode == key_esc) action = 1; - if (action != -1) break; - } - } - - if (action != -1){ - complete = 1; - view->user_action = action; - } - }break; - - case IInt_Sure_To_Kill: - { - i32 action = -1; - char s_[256]; - String s; - s = make_fixed_width_string(s_); - append(&s, view->dest); - append(&s, " has unsaved changes, kill it?"); - do_label(state, layout, s, 1.f); - - i32 id = 0; - if (do_list_option(++id, state, layout, make_lit_string("(Y)es"))){ - action = 0; - } - - if (do_list_option(++id, state, layout, make_lit_string("(N)o"))){ - action = 1; - } - - if (do_list_option(++id, state, layout, make_lit_string("(S)ave and kill"))){ - action = 2; - } - - if (action == -1 && input_stage){ - i32 key_count = keys->count; - for (i32 i = 0; i < key_count; ++i){ - Key_Event_Data key = keys->keys[i]; - switch (key.character){ - case 'y': case 'Y': action = 0; break; - case 'n': case 'N': action = 1; break; - case 's': case 'S': action = 2; break; - } - if (action == -1 && key.keycode == key_esc) action = 1; - if (action != -1) break; - } - } - - if (action != -1){ - complete = 1; - view->user_action = action; - } - }break; - } - - if (complete){ - view->finished = 1; - interactive_view_complete(view); - result= 1; - } - - return(result); -} - -internal void -menu_shit(View *view, UI_State *state, UI_Layout *layout){ - i32 id = 0; - - do_label(state, layout, literal("Menu"), 2.f); - - if (do_list_option(++id, state, layout, make_lit_string("Theme Options"))){ - view_show_theme(view, view->map); - } - - if (do_list_option(++id, state, layout, make_lit_string("Keyboard Layout Options"))){ - view_show_config(view, view->map); - } -} - -internal void -config_shit(View *view, UI_State *state, UI_Layout *layout){ - i32 id = 0; - Models *models = view->models; - - do_label(state, layout, literal("Config"), 2.f); - - if (do_checkbox_list_option(++id, state, layout, make_lit_string("Left Ctrl + Left Alt = AltGr"), - models->settings.lctrl_lalt_is_altgr)){ - models->settings.lctrl_lalt_is_altgr = !models->settings.lctrl_lalt_is_altgr; - } -} - -struct File_Bar{ - f32 pos_x, pos_y; - f32 text_shift_x, text_shift_y; - i32_Rect rect; - i16 font_id; -}; - -internal void -intbar_draw_string(Render_Target *target, File_Bar *bar, String str, u32 char_color){ - i16 font_id = bar->font_id; - - draw_string(target, font_id, str, - (i32)(bar->pos_x + bar->text_shift_x), - (i32)(bar->pos_y + bar->text_shift_y), - char_color); - bar->pos_x += font_string_width(target, font_id, str); -} - -internal void -do_file_bar(View *view, Editing_File *file, UI_Layout *layout, Render_Target *target){ - File_Bar bar; - Models *models = view->models; - Style_Font *font = &models->global_font; - i32 line_height = view->font_height; - Interactive_Style bar_style = models->style.main.file_info_style; - - u32 back_color = bar_style.bar_color; - u32 base_color = bar_style.base_color; - u32 pop1_color = bar_style.pop1_color; - u32 pop2_color = bar_style.pop2_color; - - bar.rect = layout_rect(layout, line_height + 2); - - if (target){ - bar.font_id = font->font_id; - bar.pos_x = (f32)bar.rect.x0; - bar.pos_y = (f32)bar.rect.y0; - bar.text_shift_y = 2; - bar.text_shift_x = 0; - - draw_rectangle(target, bar.rect, back_color); - intbar_draw_string(target, &bar, file->name.live_name, base_color); - intbar_draw_string(target, &bar, make_lit_string(" -"), base_color); - - if (file->state.is_loading){ - intbar_draw_string(target, &bar, make_lit_string(" loading"), base_color); - } - else{ - char line_number_space[30]; - String line_number = make_string(line_number_space, 0, 30); - append(&line_number, " L#"); - append_int_to_str(view->cursor.line, &line_number); - - intbar_draw_string(target, &bar, line_number, base_color); - - intbar_draw_string(target, &bar, make_lit_string(" -"), base_color); - - if (file->settings.dos_write_mode){ - intbar_draw_string(target, &bar, make_lit_string(" dos"), base_color); - } - else{ - intbar_draw_string(target, &bar, make_lit_string(" nix"), base_color); - } - - if (file->state.still_lexing){ - intbar_draw_string(target, &bar, make_lit_string(" parsing"), pop1_color); - } - - if (!file->settings.unimportant){ - switch (buffer_get_sync(file)){ - case SYNC_BEHIND_OS: - { - persist String out_of_sync = make_lit_string(" !"); - intbar_draw_string(target, &bar, out_of_sync, pop2_color); - }break; - - case SYNC_UNSAVED: - { - persist String out_of_sync = make_lit_string(" *"); - intbar_draw_string(target, &bar, out_of_sync, pop2_color); - }break; - } - } - } - } -} - -internal void -view_reinit_scrolling(View *view){ - Editing_File *file = view->file; - f32 w, h; - f32 cursor_x, cursor_y; - f32 target_x, target_y; - - view->reinit_scrolling = 0; - - target_x = 0; - target_y = 0; - - if (file && file_is_ready(file)){ - cursor_x = view_get_cursor_x(view); - cursor_y = view_get_cursor_y(view); - - w = view_compute_width(view); - h = view_compute_height(view); - - if (cursor_x >= target_x + w){ - target_x = (f32)(cursor_x - w*.5f); - } - - target_y = (f32)FLOOR32(cursor_y - h*.5f); - if (target_y < view->scroll_min_limit) target_y = view->scroll_min_limit; - } - - view->target_x = target_x; - view->target_y = target_y; - view->scroll_x = target_x; - view->scroll_y = target_y; - view->prev_target_x = -1000.f; - view->prev_target_y = -1000.f; -} - -internal i32 -step_file_view(System_Functions *system, Exchange *exchange, View *view, i32_Rect rect, - b32 is_active, Input_Summary *user_input){ - - Models *models = view->models; - i32 result = 0; - Editing_File *file = view->file; - - i32 widget_height = 0; - { - UI_State state = - ui_state_init(&view->widget.state, 0, user_input, - &models->style, models->global_font.font_id, models->font_set, 0, 1); - - UI_Layout layout; - begin_layout(&layout, rect); - - switch (view->widget.type){ - case FWIDG_NONE: - { - if (file && view->showing_ui == VUI_None){ - do_file_bar(view, file, &layout, 0); - } - - draw_file_view_queries(view, &state, &layout); - }break; - - case FWIDG_TIMELINES: - { - i32 scrub_max = view->scrub_max; - i32 undo_count = file->state.undo.undo.edit_count; - i32 redo_count = file->state.undo.redo.edit_count; - i32 total_count = undo_count + redo_count; - - undo_shit(system, view, &state, &layout, total_count, undo_count, scrub_max); - }break; - } - - widget_height = layout.y - rect.y0; - if (ui_finish_frame(&view->widget.state, &state, &layout, rect, 0, 0)){ - result = 1; - } - } - - view->scroll_min_limit = (f32)-widget_height; - if (view->reinit_scrolling){ - view_reinit_scrolling(view); - } - - // TODO(allen): Split this into passive step and step that depends on input - if (view->showing_ui == VUI_None && file && !file->state.is_loading){ - f32 line_height = (f32)view->font_height; - f32 cursor_y = view_get_cursor_y(view); - f32 target_y = view->target_y; - f32 max_y = view_compute_height(view) - line_height*2; - i32 lowest_line = view_compute_lowest_line(view); - f32 max_target_y = view_compute_max_target_y(lowest_line, (i32)line_height, max_y); - f32 delta_y = 3.f*line_height; - f32 extra_top = (f32)widget_height; - f32 taken_top_space = line_height + extra_top; - - if (user_input->mouse.wheel != 0){ - f32 wheel_multiplier = 3.f; - f32 delta_target_y = delta_y*user_input->mouse.wheel*wheel_multiplier; - target_y += delta_target_y; - - if (target_y < -taken_top_space) target_y = -taken_top_space; - if (target_y > max_target_y) target_y = max_target_y; - - f32 old_cursor_y = cursor_y; - if (cursor_y >= target_y + max_y) cursor_y = target_y + max_y; - if (cursor_y < target_y + taken_top_space) cursor_y = target_y + taken_top_space; - - if (cursor_y != old_cursor_y){ - view->cursor = - view_compute_cursor_from_xy(view, - view->preferred_x, - cursor_y); - } - - result = 1; - } - - if (cursor_y > target_y + max_y){ - target_y = cursor_y - max_y + delta_y; - } - if (cursor_y < target_y + taken_top_space){ - target_y = cursor_y - delta_y - taken_top_space; - } - - if (target_y > max_target_y) target_y = max_target_y; - if (target_y < -extra_top) target_y = -extra_top; - view->target_y = target_y; - - f32 cursor_x = view_get_cursor_x(view); - f32 target_x = view->target_x; - f32 max_x = view_compute_width(view); - if (cursor_x < target_x){ - target_x = (f32)Max(0, cursor_x - max_x/2); - } - else if (cursor_x >= target_x + max_x){ - target_x = (f32)(cursor_x - max_x/2); - } - - view->target_x = target_x; - - b32 is_new_target = 0; - if (view->target_x != view->prev_target_x) is_new_target = 1; - if (view->target_y != view->prev_target_y) is_new_target = 1; - - if (view->models->scroll_rule( - view->target_x, view->target_y, - &view->scroll_x, &view->scroll_y, - (view->id) + 1, is_new_target)){ - result = 1; - } - - view->prev_target_x = view->target_x; - view->prev_target_y = view->target_y; - - if (file->state.paste_effect.tick_down > 0){ - --file->state.paste_effect.tick_down; - result = 1; - } - - if (user_input->mouse.press_l && is_active){ - f32 max_y = view_compute_height(view); - f32 rx = (f32)(user_input->mouse.x - rect.x0); - f32 ry = (f32)(user_input->mouse.y - rect.y0); - - if (ry >= extra_top){ - view_set_widget(view, FWIDG_NONE); - if (rx >= 0 && rx < max_x && ry >= 0 && ry < max_y){ - view_cursor_move(view, rx + view->scroll_x, ry + view->scroll_y, 1); - view->mode = {}; - } - } - result = 1; - } - - if (!is_active) view_set_widget(view, FWIDG_NONE); - } - - { - UI_State state = - ui_state_init(&view->ui_state, 0, user_input, - &models->style, models->global_font.font_id, models->font_set, &models->working_set, 1); - - UI_Layout layout; - begin_layout(&layout, rect); - - Super_Color color = {}; - - switch (view->showing_ui){ - case VUI_None: break; - case VUI_Theme: - { - theme_shit(system, exchange, view, 0, &state, &layout, &color); - }break; - case VUI_Interactive: - { - if (interactive_shit(system, view, &state, &layout)){ - result = 1; - } - }break; - case VUI_Menu: - { - menu_shit(view, &state, &layout); - }break; - case VUI_Config: - { - config_shit(view, &state, &layout); - }break; - } - - i32 did_activation = 0; - if (ui_finish_frame(&view->ui_state, &state, &layout, rect, 0, &did_activation)){ - result = 1; - } - if (did_activation){ - if (view->showing_ui == VUI_Theme){ - view->color = color; - result = 1; - } - } - } - - return(result); -} - -internal i32 -draw_file_loaded(View *view, i32_Rect rect, b32 is_active, Render_Target *target){ - Models *models = view->models; - Editing_File *file = view->file; - Style *style = &models->style; - i32 line_height = view->font_height; - - i32 max_x = rect.x1 - rect.x0; - i32 max_y = rect.y1 - rect.y0 + line_height; - - Assert(file && !file->state.is_dummy && buffer_good(&file->state.buffer)); - - b32 tokens_use = 0; - Cpp_Token_Stack token_stack = {}; - if (file){ - tokens_use = file->state.tokens_complete && (file->state.token_stack.count > 0); - token_stack = file->state.token_stack; - } - - Partition *part = &models->mem.part; - - Temp_Memory temp = begin_temp_memory(part); - - partition_align(part, 4); - i32 max = partition_remaining(part) / sizeof(Buffer_Render_Item); - Buffer_Render_Item *items = push_array(part, Buffer_Render_Item, max); - - i16 font_id = models->global_font.font_id; - Render_Font *font = get_font_info(models->font_set, font_id)->font; - float *advance_data = 0; - if (font) advance_data = font->advance_data; - - i32 count; - Full_Cursor render_cursor; - Buffer_Render_Options opts = {}; - - f32 *wraps = view->line_wrap_y; - f32 scroll_x = view->scroll_x; - f32 scroll_y = view->scroll_y; - - { - render_cursor = buffer_get_start_cursor(&file->state.buffer, wraps, scroll_y, - !view->unwrapped_lines, (f32)max_x, advance_data, (f32)line_height); - - view->scroll_i = render_cursor.pos; - - buffer_get_render_data(&file->state.buffer, items, max, &count, - (f32)rect.x0, (f32)rect.y0, - scroll_x, scroll_y, render_cursor, - !view->unwrapped_lines, - (f32)max_x, (f32)max_y, - advance_data, (f32)line_height, - opts); - } - - Assert(count > 0); - - i32 cursor_begin, cursor_end; - u32 cursor_color, at_cursor_color; - if (view->show_temp_highlight){ - cursor_begin = view->temp_highlight.pos; - cursor_end = view->temp_highlight_end_pos; - cursor_color = style->main.highlight_color; - at_cursor_color = style->main.at_highlight_color; - } - else{ - cursor_begin = view->cursor.pos; - cursor_end = cursor_begin + 1; - cursor_color = style->main.cursor_color; - at_cursor_color = style->main.at_cursor_color; - } - - i32 token_i = 0; - u32 main_color = style->main.default_color; - u32 special_color = style->main.special_character_color; - if (tokens_use){ - Cpp_Get_Token_Result result = cpp_get_token(&token_stack, items->index); - main_color = *style_get_color(style, token_stack.tokens[result.token_index]); - token_i = result.token_index + 1; - } - - u32 mark_color = style->main.mark_color; - Buffer_Render_Item *item = items; - i32 prev_ind = -1; - u32 highlight_color = 0; - u32 highlight_this_color = 0; - - for (i32 i = 0; i < count; ++i, ++item){ - i32 ind = item->index; - highlight_this_color = 0; - if (tokens_use && ind != prev_ind){ - Cpp_Token current_token = token_stack.tokens[token_i-1]; - - if (token_i < token_stack.count){ - if (ind >= token_stack.tokens[token_i].start){ - main_color = - *style_get_color(style, token_stack.tokens[token_i]); - current_token = token_stack.tokens[token_i]; - ++token_i; - } - else if (ind >= current_token.start + current_token.size){ - main_color = 0xFFFFFFFF; - } - } - - if (current_token.type == CPP_TOKEN_JUNK && - i >= current_token.start && i < current_token.start + current_token.size){ - highlight_color = style->main.highlight_junk_color; - } - else{ - highlight_color = 0; - } - } - - u32 char_color = main_color; - if (item->flags & BRFlag_Special_Character) char_color = special_color; - - f32_Rect char_rect = f32R(item->x0, item->y0, item->x1, item->y1); - if (view->show_whitespace && highlight_color == 0 && - char_is_whitespace((char)item->glyphid)){ - highlight_this_color = style->main.highlight_white_color; - } - else{ - highlight_this_color = highlight_color; - } - - if (cursor_begin <= ind && ind < cursor_end && (ind != prev_ind || cursor_begin < ind)){ - if (is_active){ - draw_rectangle(target, char_rect, cursor_color); - char_color = at_cursor_color; - } - else{ - if (!view->show_temp_highlight){ - draw_rectangle_outline(target, char_rect, cursor_color); - } - } - } - else if (highlight_this_color){ - draw_rectangle(target, char_rect, highlight_this_color); - } - - u32 fade_color = 0xFFFF00FF; - f32 fade_amount = 0.f; - - if (file->state.paste_effect.tick_down > 0 && - file->state.paste_effect.start <= ind && - ind < file->state.paste_effect.end){ - fade_color = file->state.paste_effect.color; - fade_amount = (f32)(file->state.paste_effect.tick_down) / file->state.paste_effect.tick_max; - } - - char_color = color_blend(char_color, fade_amount, fade_color); - - if (ind == view->mark && prev_ind != ind){ - draw_rectangle_outline(target, char_rect, mark_color); - } - if (item->glyphid != 0){ - font_draw_glyph(target, font_id, (u8)item->glyphid, - item->x0, item->y0, char_color); - } - prev_ind = ind; - } - - end_temp_memory(temp); - - return(0); -} - -internal i32 -draw_file_view(System_Functions *system, Exchange *exchange, - View *view, View *active, i32_Rect rect, b32 is_active, - Render_Target *target, Input_Summary *user_input){ - - Models *models = view->models; - Editing_File *file = view->file; - i32 result = 0; - - i32 widget_height = 0; - { - UI_State state = - ui_state_init(&view->widget.state, target, 0, - &models->style, models->global_font.font_id, models->font_set, 0, 0); - - UI_Layout layout; - begin_layout(&layout, rect); - - switch (view->widget.type){ - case FWIDG_NONE: - { - if (file && view->showing_ui == VUI_None){ - do_file_bar(view, file, &layout, target); - } - - draw_file_view_queries(view, &state, &layout); - }break; - - case FWIDG_TIMELINES: - { - if (file){ - i32 undo_count = file->state.undo.undo.edit_count; - i32 redo_count = file->state.undo.redo.edit_count; - i32 total_count = undo_count + redo_count; - undo_shit(0, view, &state, &layout, total_count, undo_count, 0); - } - else{ - view->widget.type = FWIDG_NONE; - } - }break; - } - - widget_height = layout.y - rect.y0; - ui_finish_frame(&view->widget.state, &state, &layout, rect, 0, 0); - } - view->scroll_min_limit = (f32)-widget_height; - - { - rect.y0 += widget_height; - target->push_clip(target, rect); - - UI_State state = - ui_state_init(&view->ui_state, target, user_input, - &models->style, models->global_font.font_id, models->font_set, &models->working_set, 0); - - UI_Layout layout; - begin_layout(&layout, rect); - - rect.y0 -= widget_height; - - Super_Color color = {}; - - switch (view->showing_ui){ - case VUI_None: - { - if (file && file_is_ready(file)){ - if (view->reinit_scrolling){ - view_reinit_scrolling(view); - } - result = draw_file_loaded(view, rect, is_active, target); - } - }break; - - case VUI_Theme: - { - theme_shit(system, exchange, view, active, &state, &layout, &color); - }break; - - case VUI_Interactive: - { - interactive_shit(system, view, &state, &layout); - }break; - case VUI_Menu: - { - menu_shit(view, &state, &layout); - }break; - case VUI_Config: - { - config_shit(view, &state, &layout); - }break; - } - - ui_finish_frame(&view->ui_state, &state, &layout, rect, 0, 0); - - target->pop_clip(target); - } - - - return (result); -} - -// TODO(allen): Passing this hook and app pointer is a hack. It can go as soon as we start -// initializing files independently of setting them to views. -internal void -kill_file(System_Functions *system, Exchange *exchange, Models *models, Editing_File *file, - Hook_Function *open_hook, Application_Links *app){ - File_Node *node, *used; - - file_close(system, &models->mem.general, file); - working_set_free_file(&models->working_set, file); - - used = &models->working_set.used_sentinel; - node = used->next; - - for (View_Iter iter = file_view_iter_init(&models->layout, file, 0); - file_view_iter_good(iter); - iter = file_view_iter_next(iter)){ - if (node != used){ - iter.view->file = 0; - view_set_file(iter.view, (Editing_File*)node, models, system, open_hook, app, 0); - node = node->next; - } - else{ - iter.view->file = 0; - view_set_file(iter.view, 0, models, system, open_hook, app, 0); - } - } -} - -inline void -free_file_view(View *view){ - if (view->line_wrap_y) - general_memory_free(&view->models->mem.general, view->line_wrap_y); -} - -struct Search_Range{ - Buffer_Type *buffer; - i32 start, size; -}; - -struct Search_Set{ - Search_Range *ranges; - i32 count, max; -}; - -struct Search_Iter{ - String word; - i32 pos; - i32 i; -}; - -struct Search_Match{ - Buffer_Type *buffer; - i32 start, end; - b32 found_match; -}; - -internal void -search_iter_init(General_Memory *general, Search_Iter *iter, i32 size){ - i32 str_max; - - if (iter->word.str == 0){ - str_max = size*2; - iter->word.str = (char*)general_memory_allocate(general, str_max, 0); - iter->word.memory_size = str_max; - } - else if (iter->word.memory_size < size){ - str_max = size*2; - iter->word.str = (char*)general_memory_reallocate_nocopy(general, iter->word.str, str_max, 0); - iter->word.memory_size = str_max; - } - - iter->i = 0; - iter->pos = 0; -} - -internal void -search_set_init(General_Memory *general, Search_Set *set, i32 set_count){ - i32 max; - - if (set->ranges == 0){ - max = set_count*2; - set->ranges = (Search_Range*)general_memory_allocate(general, sizeof(Search_Range)*max, 0); - set->max = max; - } - else if (set->max < set_count){ - max = set_count*2; - set->ranges = (Search_Range*)general_memory_reallocate_nocopy( - general, set->ranges, sizeof(Search_Range)*max, 0); - set->max = max; - } - - set->count = set_count; -} - -internal void -search_hits_table_alloc(General_Memory *general, Table *hits, i32 table_size){ - void *mem; - i32 mem_size; - - mem_size = table_required_mem_size(table_size, sizeof(Offset_String)); - if (hits->hash_array == 0){ - mem = general_memory_allocate(general, mem_size, 0); - } - else{ - mem = general_memory_reallocate_nocopy(general, hits->hash_array, mem_size, 0); - } - table_init_memory(hits, mem, table_size, sizeof(Offset_String)); -} - -internal void -search_hits_init(General_Memory *general, Table *hits, String_Space *str, i32 table_size, i32 str_size){ - void *mem; - i32 mem_size; - - if (hits->hash_array == 0){ - search_hits_table_alloc(general, hits, table_size); - } - else if (hits->max < table_size){ - mem_size = table_required_mem_size(table_size, sizeof(Offset_String)); - mem = general_memory_reallocate_nocopy(general, hits->hash_array, mem_size, 0); - table_init_memory(hits, mem, table_size, sizeof(Offset_String)); - } - - if (str->space == 0){ - str->space = (char*)general_memory_allocate(general, str_size, 0); - str->max = str_size; - } - else if (str->max < str_size){ - str->space = (char*)general_memory_reallocate_nocopy(general, str->space, str_size, 0); - str->max = str_size; - } - - str->pos = str->new_pos = 0; - table_clear(hits); -} - -internal b32 -search_hit_add(General_Memory *general, Table *hits, String_Space *space, char *str, i32 len){ - b32 result; - i32 new_size; - Offset_String ostring; - Table new_hits; - - Assert(len != 0); - - ostring = strspace_append(space, str, len); - if (ostring.size == 0){ - new_size = Max(space->max*2, space->max + len); - space->space = (char*)general_memory_reallocate(general, space->space, space->new_pos, new_size, 0); - ostring = strspace_append(space, str, len); - } - - Assert(ostring.size != 0); - - if (table_at_capacity(hits)){ - search_hits_table_alloc(general, &new_hits, hits->max*2); - table_clear(&new_hits); - table_rehash(hits, &new_hits, space->space, tbl_offset_string_hash, tbl_offset_string_compare); - general_memory_free(general, hits->hash_array); - *hits = new_hits; - } - - if (!table_add(hits, &ostring, space->space, tbl_offset_string_hash, tbl_offset_string_compare)){ - result = 1; - strspace_keep_prev(space); - } - else{ - result = 0; - strspace_discard_prev(space); - } - - return(result); -} - -internal Search_Match -search_next_match(Partition *part, Search_Set *set, Search_Iter *iter_){ - Search_Match result = {}; - Search_Iter iter = *iter_; - Search_Range *range; - Temp_Memory temp; - char *spare; - i32 start_pos, end_pos, count; - - temp = begin_temp_memory(part); - spare = push_array(part, char, iter.word.size); - - count = set->count; - for (; iter.i < count;){ - range = set->ranges + iter.i; - - end_pos = range->start + range->size; - - if (iter.pos + iter.word.size < end_pos){ - start_pos = Max(iter.pos, range->start); - result.start = buffer_find_string(range->buffer, start_pos, end_pos, iter.word.str, iter.word.size, spare); - - if (result.start < end_pos){ - iter.pos = result.start + 1; - if (result.start == 0 || !char_is_alpha_numeric(buffer_get_char(range->buffer, result.start - 1))){ - result.end = buffer_seek_word_right_assume_on_word(range->buffer, result.start); - if (result.end < end_pos){ - result.found_match = 1; - result.buffer = range->buffer; - iter.pos = result.end; - break; - } - } - } - else{ - ++iter.i, iter.pos = 0; - } - } - else{ - ++iter.i, iter.pos = 0; - } - } - end_temp_memory(temp); - - *iter_ = iter; - - return(result); -} - -inline void -view_change_size(System_Functions *system, General_Memory *general, View *view){ - if (view->file){ - view_measure_wraps(system, general, view); - view->cursor = view_compute_cursor_from_pos(view, view->cursor.pos); - } -} - -struct Live_Views{ - View *views; - View free_sentinel; - i32 count, max; -}; - -internal View_And_ID -live_set_alloc_view(Live_Views *live_set, Panel *panel, Models *models){ - View_And_ID result = {}; - - Assert(live_set->count < live_set->max); - ++live_set->count; - - result.view = live_set->free_sentinel.next; - result.id = (i32)(result.view - live_set->views); - - dll_remove(result.view); - memset(result.view, 0, sizeof(View)); - result.view->id = result.id; - - result.view->in_use = 1; - panel->view = result.view; - result.view->panel = panel; - - result.view->models = models; - result.view->scrub_max = 1; - - // TODO(allen): Make "interactive" mode customizable just like the query bars! - result.view->query = make_fixed_width_string(result.view->query_); - result.view->dest = make_fixed_width_string(result.view->dest_); - - init_query_set(&result.view->query_set); - - return(result); -} - -inline void -live_set_free_view(System_Functions *system, Exchange *exchange, Live_Views *live_set, View *view){ - Assert(live_set->count > 0); - --live_set->count; - free_file_view(view); - dll_insert(&live_set->free_sentinel, view); - view->in_use = 0; -} - -// BOTTOM - +/* + * Mr. 4th Dimention - Allen Webster + * + * 19.08.2015 + * + * File editing view for 4coder + * + */ + +// TOP + +enum Interactive_Action{ + IAct_Open, + IAct_Save_As, + IAct_New, + IAct_Switch, + IAct_Kill, + IAct_Sure_To_Kill, + IAct_Sure_To_Close +}; + +enum Interactive_Interaction{ + IInt_Sys_File_List, + IInt_Live_File_List, + IInt_Sure_To_Kill, + IInt_Sure_To_Close +}; + +struct View_Mode{ + i32 rewrite; +}; + +enum View_Widget_Type{ + FWIDG_NONE, + FWIDG_TIMELINES, + // never below this + FWIDG_TYPE_COUNT +}; + +struct View_Widget{ + View_Widget_Type type; + i32 height_; + struct{ + b32 undo_line; + b32 history_line; + } timeline; +}; + +enum View_UI{ + VUI_None, + VUI_Theme, + VUI_Interactive, + VUI_Menu, + VUI_Config, +}; + +enum Color_View_Mode{ + CV_Mode_Library, + CV_Mode_Import_File, + CV_Mode_Export_File, + CV_Mode_Import, + CV_Mode_Export, + CV_Mode_Import_Wait, + CV_Mode_Adjusting +}; + +struct View{ + View *next, *prev; + b32 in_use; + i32 id; + + Models *models; + + Panel *panel; + Command_Map *map; + + Editing_File *file; + + View_UI showing_ui; + GUI_Target gui_target; + +#if 0 + // interactive stuff + Interactive_Interaction interaction; + Interactive_Action action; + b32 finished; + char query_[256]; + char dest_[256]; + String query; + String dest; + i32 user_action; + + // theme stuff + View *hot_file_view; + u32 *palette; + i32 palette_size; + Color_View_Mode color_mode; + b32 p4c_only; + Style_Library inspecting_styles; + b8 import_export_check[64]; + i32 import_file_id; +#endif + + // file stuff + i32 font_advance; + i32 font_height; + + Full_Cursor cursor; + i32 mark; + f32 scroll_y, target_y, prev_target_y; + f32 scroll_x, target_x, prev_target_x; + f32 preferred_x; + i32 scroll_i; + f32 scroll_min_limit; + + Full_Cursor temp_highlight; + i32 temp_highlight_end_pos; + b32 show_temp_highlight; + + View_Mode mode, next_mode; + View_Widget widget; + Query_Set query_set; + i32 scrub_max; + + b32 unwrapped_lines; + b32 show_whitespace; + b32 file_locked; + + i32 line_count, line_max; + f32 *line_wrap_y; + + Command_Map *map_for_file; + b32 reinit_scrolling; +}; + +struct View_And_ID{ + View *view; + i32 id; +}; + +#define LockLevel_Open 0 +#define LockLevel_NoWrite 1 +#define LockLevel_NoUpdate 2 + +inline i32 +view_lock_level(View *view){ + i32 result = LockLevel_Open; + if (view->showing_ui != VUI_None) result = LockLevel_NoUpdate; + else if (view->file_locked || + (view->file && view->file->settings.read_only)) result = LockLevel_NoWrite; + return(result); +} + +inline f32 +view_compute_width(View *view){ + Panel *panel = view->panel; + return (f32)(panel->inner.x1 - panel->inner.x0); +} + +inline f32 +view_compute_height(View *view){ + Panel *panel = view->panel; + return (f32)(panel->inner.y1 - panel->inner.y0); +} + +struct View_Iter{ + View *view; + + Editing_File *file; + View *skip; + Panel *used_panels; + Panel *panel; +}; + +internal View_Iter +file_view_iter_next(View_Iter iter){ + View *file_view; + + for (iter.panel = iter.panel->next; iter.panel != iter.used_panels; iter.panel = iter.panel->next){ + file_view = iter.panel->view; + if (file_view != iter.skip && (file_view->file == iter.file || iter.file == 0)){ + iter.view = file_view; + break; + } + } + + return(iter); +} + +internal View_Iter +file_view_iter_init(Editing_Layout *layout, Editing_File *file, View *skip){ + View_Iter result; + result.used_panels = &layout->used_sentinel; + result.panel = result.used_panels; + result.file = file; + result.skip = skip; + + result = file_view_iter_next(result); + + return(result); +} + +internal b32 +file_view_iter_good(View_Iter iter){ + b32 result = (iter.panel != iter.used_panels); + return(result); +} + +inline b32 +starts_new_line(u8 character){ + return (character == '\n'); +} + +inline void +file_init_strings(Editing_File *file){ + file->name.source_path = make_fixed_width_string(file->name.source_path_); + file->name.live_name = make_fixed_width_string(file->name.live_name_); + file->name.extension = make_fixed_width_string(file->name.extension_); +} + +inline void +file_set_name(Working_Set *working_set, Editing_File *file, char *filename){ + String f, ext; + + Assert(file->name.live_name.str != 0); + + f = make_string_slowly(filename); + copy_checked(&file->name.source_path, f); + + file->name.live_name.size = 0; + get_front_of_directory(&file->name.live_name, f); + + if (file->name.source_path.size == file->name.live_name.size){ + file->name.extension.size = 0; + } + else{ + ext = file_extension(f); + copy(&file->name.extension, ext); + } + + { + File_Node *node, *used_nodes; + Editing_File *file_ptr; + i32 file_x, original_len; + b32 hit_conflict; + + used_nodes = &working_set->used_sentinel; + original_len = file->name.live_name.size; + hit_conflict = 1; + file_x = 0; + while (hit_conflict){ + hit_conflict = 0; + for (dll_items(node, used_nodes)){ + file_ptr = (Editing_File*)node; + if (file_ptr != file && file_is_ready(file_ptr)){ + if (match(file->name.live_name, file_ptr->name.live_name)){ + ++file_x; + hit_conflict = 1; + break; + } + } + } + + if (hit_conflict){ + file->name.live_name.size = original_len; + append(&file->name.live_name, " <"); + append_int_to_str(file_x, &file->name.live_name); + append(&file->name.live_name, ">"); + } + } + } +} + +inline void +file_synchronize_times(System_Functions *system, Editing_File *file, char *filename){ + u64 stamp = system->file_time_stamp(filename); + if (stamp > 0){ + file->state.last_4ed_write_time = stamp; + file->state.last_4ed_edit_time = stamp; + file->state.last_sys_write_time = stamp; + } + file->state.sync = buffer_get_sync(file); +} + +internal i32 +file_save(System_Functions *system, Exchange *exchange, Mem_Options *mem, + Editing_File *file, char *filename){ + i32 result = 0; + + i32 max, size; + b32 dos_write_mode = file->settings.dos_write_mode; + char *data; + Buffer_Type *buffer = &file->state.buffer; + + if (dos_write_mode) + max = buffer_size(buffer) + buffer->line_count + 1; + else + max = buffer_size(buffer); + + data = (char*)general_memory_allocate(&mem->general, max, 0); + Assert(data); + + if (dos_write_mode) + size = buffer_convert_out(buffer, data, max); + else + buffer_stringify(buffer, 0, size = max, data); + + result = exchange_save_file(exchange, filename, str_size(filename), (byte*)data, size, max); + + if (result == 0){ + general_memory_free(&mem->general, data); + } + + file_synchronize_times(system, file, filename); + + return(result); +} + +inline b32 +file_save_and_set_names(System_Functions *system, Exchange *exchange, + Mem_Options *mem, Working_Set *working_set, Editing_File *file, + char *filename){ + b32 result = 0; + result = file_save(system, exchange, mem, file, filename); + if (result){ + file_set_name(working_set, file, filename); + } + return result; +} + +enum File_Bubble_Type{ + BUBBLE_BUFFER = 1, + BUBBLE_STARTS, + BUBBLE_WIDTHS, + BUBBLE_WRAPS, + BUBBLE_TOKENS, + BUBBLE_UNDO_STRING, + BUBBLE_UNDO, + BUBBLE_UNDO_CHILDREN, + // + FILE_BUBBLE_TYPE_END, +}; + +#define GROW_FAILED 0 +#define GROW_NOT_NEEDED 1 +#define GROW_SUCCESS 2 + +internal i32 +file_grow_starts_widths_as_needed(General_Memory *general, Buffer_Type *buffer, i32 additional_lines){ + b32 result = GROW_NOT_NEEDED; + i32 max = buffer->line_max; + i32 count = buffer->line_count; + i32 target_lines = count + additional_lines; + Assert(max == buffer->widths_max); + + if (target_lines > max || max == 0){ + max = LargeRoundUp(target_lines + max, Kbytes(1)); + + f32 *new_widths = (f32*)general_memory_reallocate( + general, buffer->line_widths, + sizeof(f32)*count, sizeof(f32)*max, BUBBLE_WIDTHS); + + i32 *new_lines = (i32*)general_memory_reallocate( + general, buffer->line_starts, + sizeof(i32)*count, sizeof(i32)*max, BUBBLE_STARTS); + + if (new_lines){ + buffer->line_starts = new_lines; + buffer->line_max = max; + } + if (new_widths){ + buffer->line_widths = new_widths; + buffer->widths_max = max; + } + if (new_lines && new_widths){ + result = GROW_SUCCESS; + } + else{ + result = GROW_FAILED; + } + } + + return(result); +} + +internal void +file_measure_starts_widths(System_Functions *system, General_Memory *general, + Buffer_Type *buffer, float *advance_data){ + ProfileMomentFunction(); + if (!buffer->line_starts){ + i32 max = buffer->line_max = Kbytes(1); + buffer->line_starts = (i32*)general_memory_allocate(general, max*sizeof(i32), BUBBLE_STARTS); + TentativeAssert(buffer->line_starts); + // TODO(allen): when unable to allocate? + } + if (!buffer->line_widths){ + i32 max = buffer->widths_max = Kbytes(1); + buffer->line_widths = (f32*)general_memory_allocate(general, max*sizeof(f32), BUBBLE_STARTS); + TentativeAssert(buffer->line_starts); + // TODO(allen): when unable to allocate? + } + + Buffer_Measure_Starts state = {}; + while (buffer_measure_starts_widths(&state, buffer, advance_data)){ + i32 count = state.count; + i32 max = buffer->line_max; + max = ((max + 1) << 1); + + { + i32 *new_lines = (i32*)general_memory_reallocate( + general, buffer->line_starts, sizeof(i32)*count, sizeof(i32)*max, BUBBLE_STARTS); + + // TODO(allen): when unable to grow? + TentativeAssert(new_lines); + buffer->line_starts = new_lines; + buffer->line_max = max; + } + + { + f32 *new_lines = (f32*) + general_memory_reallocate(general, buffer->line_widths, + sizeof(f32)*count, sizeof(f32)*max, BUBBLE_WIDTHS); + + // TODO(allen): when unable to grow? + TentativeAssert(new_lines); + buffer->line_widths = new_lines; + buffer->widths_max = max; + } + + } + buffer->line_count = state.count; + buffer->widths_count = state.count; +} + +struct Opaque_Font_Advance{ + void *data; + int stride; +}; + +inline Opaque_Font_Advance +get_opaque_font_advance(Render_Font *font){ + Opaque_Font_Advance result; + result.data = (char*)font->chardata + OffsetOfPtr(font->chardata, xadvance); + result.stride = sizeof(*font->chardata); + return result; +} + +internal void +file_remeasure_widths_(System_Functions *system, + General_Memory *general, Buffer_Type *buffer, Render_Font *font, + i32 line_start, i32 line_end, i32 line_shift){ + ProfileMomentFunction(); + file_grow_starts_widths_as_needed(general, buffer, line_shift); + buffer_remeasure_widths(buffer, font->advance_data, line_start, line_end, line_shift); +} + +inline i32 +view_wrapped_line_span(f32 line_width, f32 max_width){ + i32 line_count = CEIL32(line_width / max_width); + if (line_count == 0) line_count = 1; + return line_count; +} + +internal i32 +view_compute_lowest_line(View *view){ + i32 lowest_line = 0; + i32 last_line = view->line_count - 1; + if (last_line > 0){ + if (view->unwrapped_lines){ + lowest_line = last_line; + } + else{ + f32 wrap_y = view->line_wrap_y[last_line]; + lowest_line = FLOOR32(wrap_y / view->font_height); + f32 max_width = view_compute_width(view); + + Editing_File *file = view->file; + Assert(!file->state.is_dummy); + f32 width = file->state.buffer.line_widths[last_line]; + i32 line_span = view_wrapped_line_span(width, max_width); + lowest_line += line_span - 1; + } + } + return lowest_line; +} + +internal void +view_measure_wraps(System_Functions *system, + General_Memory *general, View *view){ + ProfileMomentFunction(); + Buffer_Type *buffer; + + buffer = &view->file->state.buffer; + i32 line_count = buffer->line_count; + + if (view->line_max < line_count){ + i32 max = view->line_max = LargeRoundUp(line_count, Kbytes(1)); + if (view->line_wrap_y){ + view->line_wrap_y = (real32*) + general_memory_reallocate_nocopy(general, view->line_wrap_y, sizeof(real32)*max, BUBBLE_WRAPS); + } + else{ + view->line_wrap_y = (real32*) + general_memory_allocate(general, sizeof(real32)*max, BUBBLE_WRAPS); + } + } + + f32 line_height = (f32)view->font_height; + f32 max_width = view_compute_width(view); + buffer_measure_wrap_y(buffer, view->line_wrap_y, line_height, max_width); + + view->line_count = line_count; +} + +internal void* +alloc_for_buffer(void *context, int *size){ + *size = LargeRoundUp(*size, Kbytes(4)); + void *data = general_memory_allocate((General_Memory*)context, *size, BUBBLE_BUFFER); + return data; +} + +internal void +file_create_from_string(System_Functions *system, Models *models, + Editing_File *file, char *filename, String val, b8 read_only = 0){ + + Font_Set *font_set = models->font_set; + Working_Set *working_set = &models->working_set; + General_Memory *general = &models->mem.general; + Partition *part = &models->mem.part; + Buffer_Init_Type init; + i32 page_size, scratch_size, init_success; + + file->state = {}; + + init = buffer_begin_init(&file->state.buffer, val.str, val.size); + for (; buffer_init_need_more(&init); ){ + page_size = buffer_init_page_size(&init); + page_size = LargeRoundUp(page_size, Kbytes(4)); + if (page_size < Kbytes(4)) page_size = Kbytes(4); + void *data = general_memory_allocate(general, page_size, BUBBLE_BUFFER); + buffer_init_provide_page(&init, data, page_size); + } + + scratch_size = partition_remaining(part); + Assert(scratch_size > 0); + init_success = buffer_end_init(&init, part->base + part->pos, scratch_size); + AllowLocal(init_success); + Assert(init_success); + + if (buffer_size(&file->state.buffer) < val.size){ + file->settings.dos_write_mode = 1; + } + + file_init_strings(file); + file_set_name(working_set, file, (char*)filename); + + file->state.font_id = models->global_font.font_id; + + file_synchronize_times(system, file, filename); + + Render_Font *font = get_font_info(font_set, file->state.font_id)->font; + float *advance_data = 0; + if (font) advance_data = font->advance_data; + file_measure_starts_widths(system, general, &file->state.buffer, advance_data); + + file->settings.read_only = read_only; + if (!read_only){ + i32 request_size = Kbytes(64); + file->state.undo.undo.max = request_size; + file->state.undo.undo.strings = (u8*)general_memory_allocate(general, request_size, BUBBLE_UNDO_STRING); + file->state.undo.undo.edit_max = request_size / sizeof(Edit_Step); + file->state.undo.undo.edits = (Edit_Step*)general_memory_allocate(general, request_size, BUBBLE_UNDO); + + file->state.undo.redo.max = request_size; + file->state.undo.redo.strings = (u8*)general_memory_allocate(general, request_size, BUBBLE_UNDO_STRING); + file->state.undo.redo.edit_max = request_size / sizeof(Edit_Step); + file->state.undo.redo.edits = (Edit_Step*)general_memory_allocate(general, request_size, BUBBLE_UNDO); + + file->state.undo.history.max = request_size; + file->state.undo.history.strings = (u8*)general_memory_allocate(general, request_size, BUBBLE_UNDO_STRING); + file->state.undo.history.edit_max = request_size / sizeof(Edit_Step); + file->state.undo.history.edits = (Edit_Step*)general_memory_allocate(general, request_size, BUBBLE_UNDO); + + file->state.undo.children.max = request_size; + file->state.undo.children.strings = (u8*)general_memory_allocate(general, request_size, BUBBLE_UNDO_STRING); + file->state.undo.children.edit_max = request_size / sizeof(Buffer_Edit); + file->state.undo.children.edits = (Buffer_Edit*)general_memory_allocate(general, request_size, BUBBLE_UNDO); + + file->state.undo.history_block_count = 1; + file->state.undo.history_head_block = 0; + file->state.undo.current_block_normal = 1; + } +} + +internal b32 +file_create_empty(System_Functions *system, + Models *models, Editing_File *file, char *filename){ + + file_create_from_string(system, models, file, filename, {}); + return (1); +} + +internal b32 +file_create_read_only(System_Functions *system, + Models *models, Editing_File *file, char *filename){ + + file_create_from_string(system, models, file, filename, {}, 1); + return (1); +} + +internal void +file_close(System_Functions *system, General_Memory *general, Editing_File *file){ + if (file->state.still_lexing){ + system->cancel_job(BACKGROUND_THREADS, file->state.lex_job); + if (file->state.swap_stack.tokens){ + general_memory_free(general, file->state.swap_stack.tokens); + file->state.swap_stack.tokens = 0; + } + } + if (file->state.token_stack.tokens){ + general_memory_free(general, file->state.token_stack.tokens); + } + + Buffer_Type *buffer = &file->state.buffer; + if (buffer->data){ + general_memory_free(general, buffer->data); + general_memory_free(general, buffer->line_starts); + general_memory_free(general, buffer->line_widths); + } + + if (file->state.undo.undo.edits){ + general_memory_free(general, file->state.undo.undo.strings); + general_memory_free(general, file->state.undo.undo.edits); + + general_memory_free(general, file->state.undo.redo.strings); + general_memory_free(general, file->state.undo.redo.edits); + + general_memory_free(general, file->state.undo.history.strings); + general_memory_free(general, file->state.undo.history.edits); + + general_memory_free(general, file->state.undo.children.strings); + general_memory_free(general, file->state.undo.children.edits); + } +} + +struct Shift_Information{ + i32 start, end, amount; +}; + +internal +Job_Callback_Sig(job_full_lex){ + Editing_File *file = (Editing_File*)data[0]; + General_Memory *general = (General_Memory*)data[1]; + + Cpp_File cpp_file; + cpp_file.data = file->state.buffer.data; + cpp_file.size = file->state.buffer.size; + + Cpp_Token_Stack tokens; + tokens.tokens = (Cpp_Token*)memory->data; + tokens.max_count = memory->size / sizeof(Cpp_Token); + tokens.count = 0; + + Cpp_Lex_Data status = cpp_lex_file_nonalloc(cpp_file, &tokens); + + while (!status.complete){ + system->grow_thread_memory(memory); + tokens.tokens = (Cpp_Token*)memory->data; + tokens.max_count = memory->size / sizeof(Cpp_Token); + status = cpp_lex_file_nonalloc(cpp_file, &tokens, status); + } + + i32 new_max = LargeRoundUp(tokens.count+1, Kbytes(1)); + + system->acquire_lock(FRAME_LOCK); + { + Assert(file->state.swap_stack.tokens == 0); + file->state.swap_stack.tokens = (Cpp_Token*) + general_memory_allocate(general, new_max*sizeof(Cpp_Token), BUBBLE_TOKENS); + } + system->release_lock(FRAME_LOCK); + + u8 *dest = (u8*)file->state.swap_stack.tokens; + u8 *src = (u8*)tokens.tokens; + + memcpy(dest, src, tokens.count*sizeof(Cpp_Token)); + + system->acquire_lock(FRAME_LOCK); + { + file->state.token_stack.count = tokens.count; + file->state.token_stack.max_count = new_max; + if (file->state.token_stack.tokens) + general_memory_free(general, file->state.token_stack.tokens); + file->state.token_stack.tokens = file->state.swap_stack.tokens; + file->state.swap_stack.tokens = 0; + } + system->release_lock(FRAME_LOCK); + + exchange->force_redraw = 1; + + // NOTE(allen): These are outside the locked section because I don't + // think getting these out of order will cause critical bugs, and I + // want to minimize what's done in locked sections. + file->state.tokens_complete = 1; + file->state.still_lexing = 0; +} + + +internal void +file_kill_tokens(System_Functions *system, + General_Memory *general, Editing_File *file){ + file->settings.tokens_exist = 0; + if (file->state.still_lexing){ + system->cancel_job(BACKGROUND_THREADS, file->state.lex_job); + if (file->state.swap_stack.tokens){ + general_memory_free(general, file->state.swap_stack.tokens); + file->state.swap_stack.tokens = 0; + } + } + if (file->state.token_stack.tokens){ + general_memory_free(general, file->state.token_stack.tokens); + } + file->state.tokens_complete = 0; + file->state.token_stack = {}; +} + +#if BUFFER_EXPERIMENT_SCALPEL <= 0 +internal void +file_first_lex_parallel(System_Functions *system, + General_Memory *general, Editing_File *file){ + file->settings.tokens_exist = 1; + + if (file->state.is_loading == 0 && file->state.still_lexing == 0){ + Assert(file->state.token_stack.tokens == 0); + + file->state.tokens_complete = 0; + file->state.still_lexing = 1; + + Job_Data job; + job.callback = job_full_lex; + job.data[0] = file; + job.data[1] = general; + job.memory_request = Kbytes(64); + file->state.lex_job = system->post_job(BACKGROUND_THREADS, job); +#endif + } +} + +internal void +file_relex_parallel(System_Functions *system, + Mem_Options *mem, Editing_File *file, + i32 start_i, i32 end_i, i32 amount){ + General_Memory *general = &mem->general; + Partition *part = &mem->part; + if (file->state.token_stack.tokens == 0){ + file_first_lex_parallel(system, general, file); + return; + } + + b32 inline_lex = !file->state.still_lexing; + if (inline_lex){ + Cpp_File cpp_file; + cpp_file.data = file->state.buffer.data; + cpp_file.size = file->state.buffer.size; + + Cpp_Token_Stack *stack = &file->state.token_stack; + + Cpp_Relex_State state = + cpp_relex_nonalloc_start(cpp_file, stack, + start_i, end_i, amount, 100); + + Temp_Memory temp = begin_temp_memory(part); + i32 relex_end; + Cpp_Token_Stack relex_space; + relex_space.count = 0; + relex_space.max_count = state.space_request; + relex_space.tokens = push_array(part, Cpp_Token, relex_space.max_count); + if (cpp_relex_nonalloc_main(&state, &relex_space, &relex_end)){ + inline_lex = 0; + } + else{ + i32 delete_amount = relex_end - state.start_token_i; + i32 shift_amount = relex_space.count - delete_amount; + + if (shift_amount != 0){ + int new_count = stack->count + shift_amount; + if (new_count > stack->max_count){ + int new_max = LargeRoundUp(new_count, Kbytes(1)); + stack->tokens = (Cpp_Token*) + general_memory_reallocate(general, stack->tokens, + stack->count*sizeof(Cpp_Token), + new_max*sizeof(Cpp_Token), BUBBLE_TOKENS); + stack->max_count = new_max; + } + + int shift_size = stack->count - relex_end; + if (shift_size > 0){ + Cpp_Token *old_base = stack->tokens + relex_end; + memmove(old_base + shift_amount, old_base, + sizeof(Cpp_Token)*shift_size); + } + + stack->count += shift_amount; + } + + memcpy(state.stack->tokens + state.start_token_i, relex_space.tokens, + sizeof(Cpp_Token)*relex_space.count); + } + + end_temp_memory(temp); + } + + if (!inline_lex){ + Cpp_Token_Stack *stack = &file->state.token_stack; + Cpp_Get_Token_Result get_token_result = cpp_get_token(stack, end_i); + i32 end_token_i = get_token_result.token_index; + + if (end_token_i < 0) end_token_i = 0; + else if (end_i > stack->tokens[end_token_i].start) ++end_token_i; + + cpp_shift_token_starts(stack, end_token_i, amount); + --end_token_i; + if (end_token_i >= 0){ + Cpp_Token *token = stack->tokens + end_token_i; + if (token->start < end_i && token->start + token->size > end_i){ + token->size += amount; + } + } + + file->state.still_lexing = 1; + + Job_Data job; + job.callback = job_full_lex; + job.data[0] = file; + job.data[1] = general; + job.memory_request = Kbytes(64); + file->state.lex_job = system->post_job(BACKGROUND_THREADS, job); + } +} + +internal void +undo_stack_grow_string(General_Memory *general, Edit_Stack *stack, i32 extra_size){ + i32 old_max = stack->max; + u8 *old_str = stack->strings; + i32 new_max = old_max*2 + extra_size; + u8 *new_str = (u8*) + general_memory_reallocate(general, old_str, old_max, new_max); + stack->strings = new_str; + stack->max = new_max; +} + +internal void +undo_stack_grow_edits(General_Memory *general, Edit_Stack *stack){ + i32 old_max = stack->edit_max; + Edit_Step *old_eds = stack->edits; + i32 new_max = old_max*2 + 2; + Edit_Step *new_eds = (Edit_Step*) + general_memory_reallocate(general, old_eds, old_max*sizeof(Edit_Step), new_max*sizeof(Edit_Step)); + stack->edits = new_eds; + stack->edit_max = new_max; +} + +internal void +child_stack_grow_string(General_Memory *general, Small_Edit_Stack *stack, i32 extra_size){ + i32 old_max = stack->max; + u8 *old_str = stack->strings; + i32 new_max = old_max*2 + extra_size; + u8 *new_str = (u8*) + general_memory_reallocate(general, old_str, old_max, new_max); + stack->strings = new_str; + stack->max = new_max; +} + +internal void +child_stack_grow_edits(General_Memory *general, Small_Edit_Stack *stack, i32 amount){ + i32 old_max = stack->edit_max; + Buffer_Edit *old_eds = stack->edits; + i32 new_max = old_max*2 + amount; + Buffer_Edit *new_eds = (Buffer_Edit*) + general_memory_reallocate(general, old_eds, old_max*sizeof(Buffer_Edit), new_max*sizeof(Buffer_Edit)); + stack->edits = new_eds; + stack->edit_max = new_max; +} + +internal i32 +undo_children_push(General_Memory *general, Small_Edit_Stack *children, + Buffer_Edit *edits, i32 edit_count, u8 *strings, i32 string_size){ + i32 result = children->edit_count; + if (children->edit_count + edit_count > children->edit_max) + child_stack_grow_edits(general, children, edit_count); + + if (children->size + string_size > children->max) + child_stack_grow_string(general, children, string_size); + + memcpy(children->edits + children->edit_count, edits, edit_count*sizeof(Buffer_Edit)); + memcpy(children->strings + children->size, strings, string_size); + + Buffer_Edit *edit = children->edits + children->edit_count; + i32 start_pos = children->size; + for (i32 i = 0; i < edit_count; ++i, ++edit){ + edit->str_start += start_pos; + } + + children->edit_count += edit_count; + children->size += string_size; + + return result; +} + +struct Edit_Spec{ + u8 *str; + Edit_Step step; +}; + +internal Edit_Step* +file_post_undo(General_Memory *general, Editing_File *file, + Edit_Step step, bool32 do_merge, bool32 can_merge){ + if (step.type == ED_NORMAL){ + file->state.undo.redo.size = 0; + file->state.undo.redo.edit_count = 0; + } + + Edit_Stack *undo = &file->state.undo.undo; + Edit_Step *result = 0; + + if (step.child_count == 0){ + if (step.edit.end - step.edit.start + undo->size > undo->max) + undo_stack_grow_string(general, undo, step.edit.end - step.edit.start); + + Buffer_Edit inv; + buffer_invert_edit(&file->state.buffer, step.edit, &inv, + (char*)undo->strings, &undo->size, undo->max); + + Edit_Step inv_step = {}; + inv_step.edit = inv; + inv_step.pre_pos = step.pre_pos; + inv_step.post_pos = step.post_pos; + inv_step.can_merge = (b8)can_merge; + inv_step.type = ED_UNDO; + + bool32 did_merge = 0; + if (do_merge && undo->edit_count > 0){ + Edit_Step prev = undo->edits[undo->edit_count-1]; + if (prev.can_merge && inv_step.edit.len == 0 && prev.edit.len == 0){ + if (prev.edit.end == inv_step.edit.start){ + did_merge = 1; + inv_step.edit.start = prev.edit.start; + inv_step.pre_pos = prev.pre_pos; + } + } + } + + if (did_merge){ + result = undo->edits + (undo->edit_count-1); + *result = inv_step; + } + else{ + if (undo->edit_count == undo->edit_max) + undo_stack_grow_edits(general, undo); + result = undo->edits + (undo->edit_count++); + *result = inv_step; + } + } + else{ + Edit_Step inv_step = {}; + inv_step.type = ED_UNDO; + inv_step.first_child = step.inverse_first_child; + inv_step.inverse_first_child = step.first_child; + inv_step.special_type = step.special_type; + inv_step.child_count = step.inverse_child_count; + inv_step.inverse_child_count = step.child_count; + + if (undo->edit_count == undo->edit_max) + undo_stack_grow_edits(general, undo); + result = undo->edits + (undo->edit_count++); + *result = inv_step; + } + return result; +} + +inline void +undo_stack_pop(Edit_Stack *stack){ + if (stack->edit_count > 0){ + Edit_Step *edit = stack->edits + (--stack->edit_count); + stack->size -= edit->edit.len; + } +} + +internal void +file_post_redo(General_Memory *general, Editing_File *file, Edit_Step step){ + Edit_Stack *redo = &file->state.undo.redo; + + if (step.child_count == 0){ + if (step.edit.end - step.edit.start + redo->size > redo->max) + undo_stack_grow_string(general, redo, step.edit.end - step.edit.start); + + Buffer_Edit inv; + buffer_invert_edit(&file->state.buffer, step.edit, &inv, + (char*)redo->strings, &redo->size, redo->max); + + Edit_Step inv_step = {}; + inv_step.edit = inv; + inv_step.pre_pos = step.pre_pos; + inv_step.post_pos = step.post_pos; + inv_step.type = ED_REDO; + + if (redo->edit_count == redo->edit_max) + undo_stack_grow_edits(general, redo); + redo->edits[redo->edit_count++] = inv_step; + } + else{ + Edit_Step inv_step = {}; + inv_step.type = ED_REDO; + inv_step.first_child = step.inverse_first_child; + inv_step.inverse_first_child = step.first_child; + inv_step.special_type = step.special_type; + inv_step.child_count = step.inverse_child_count; + inv_step.inverse_child_count = step.child_count; + + if (redo->edit_count == redo->edit_max) + undo_stack_grow_edits(general, redo); + redo->edits[redo->edit_count++] = inv_step; + } +} + +inline void +file_post_history_block(Editing_File *file, i32 pos){ + Assert(file->state.undo.history_head_block < pos); + Assert(pos < file->state.undo.history.edit_count); + + Edit_Step *history = file->state.undo.history.edits; + Edit_Step *step = history + file->state.undo.history_head_block; + step->next_block = pos; + step = history + pos; + step->prev_block = file->state.undo.history_head_block; + file->state.undo.history_head_block = pos; + ++file->state.undo.history_block_count; +} + +inline void +file_unpost_history_block(Editing_File *file){ + Assert(file->state.undo.history_block_count > 1); + --file->state.undo.history_block_count; + Edit_Step *old_head = file->state.undo.history.edits + file->state.undo.history_head_block; + file->state.undo.history_head_block = old_head->prev_block; +} + +internal Edit_Step* +file_post_history(General_Memory *general, Editing_File *file, + Edit_Step step, b32 do_merge, b32 can_merge){ + Edit_Stack *history = &file->state.undo.history; + Edit_Step *result = 0; + + persist Edit_Type reverse_types[4]; + if (reverse_types[ED_UNDO] == 0){ + reverse_types[ED_NORMAL] = ED_REVERSE_NORMAL; + reverse_types[ED_REVERSE_NORMAL] = ED_NORMAL; + reverse_types[ED_UNDO] = ED_REDO; + reverse_types[ED_REDO] = ED_UNDO; + } + + if (step.child_count == 0){ + if (step.edit.end - step.edit.start + history->size > history->max) + undo_stack_grow_string(general, history, step.edit.end - step.edit.start); + + Buffer_Edit inv; + buffer_invert_edit(&file->state.buffer, step.edit, &inv, + (char*)history->strings, &history->size, history->max); + + Edit_Step inv_step = {}; + inv_step.edit = inv; + inv_step.pre_pos = step.pre_pos; + inv_step.post_pos = step.post_pos; + inv_step.can_merge = (b8)can_merge; + inv_step.type = reverse_types[step.type]; + + bool32 did_merge = 0; + if (do_merge && history->edit_count > 0){ + Edit_Step prev = history->edits[history->edit_count-1]; + if (prev.can_merge && inv_step.edit.len == 0 && prev.edit.len == 0){ + if (prev.edit.end == inv_step.edit.start){ + did_merge = 1; + inv_step.edit.start = prev.edit.start; + inv_step.pre_pos = prev.pre_pos; + } + } + } + + if (did_merge){ + result = history->edits + (history->edit_count-1); + } + else{ + if (history->edit_count == history->edit_max) + undo_stack_grow_edits(general, history); + result = history->edits + (history->edit_count++); + } + + *result = inv_step; + } + else{ + Edit_Step inv_step = {}; + inv_step.type = reverse_types[step.type]; + inv_step.first_child = step.inverse_first_child; + inv_step.inverse_first_child = step.first_child; + inv_step.special_type = step.special_type; + inv_step.inverse_child_count = step.child_count; + inv_step.child_count = step.inverse_child_count; + + if (history->edit_count == history->edit_max) + undo_stack_grow_edits(general, history); + result = history->edits + (history->edit_count++); + *result = inv_step; + } + + return result; +} + +inline Full_Cursor +view_compute_cursor_from_pos(View *view, i32 pos){ + Editing_File *file = view->file; + Models *models = view->models; + Render_Font *font = get_font_info(models->font_set, models->global_font.font_id)->font; + + Full_Cursor result = {}; + if (font){ + f32 max_width = view_compute_width(view); + result = buffer_cursor_from_pos(&file->state.buffer, pos, view->line_wrap_y, + max_width, (f32)view->font_height, font->advance_data); + } + return result; +} + +inline Full_Cursor +view_compute_cursor_from_unwrapped_xy(View *view, f32 seek_x, f32 seek_y, b32 round_down = 0){ + Editing_File *file = view->file; + Models *models = view->models; + Render_Font *font = get_font_info(models->font_set, models->global_font.font_id)->font; + + Full_Cursor result = {}; + if (font){ + f32 max_width = view_compute_width(view); + result = buffer_cursor_from_unwrapped_xy(&file->state.buffer, seek_x, seek_y, + round_down, view->line_wrap_y, + max_width, (f32)view->font_height, font->advance_data); + } + + return result; +} + +internal Full_Cursor +view_compute_cursor_from_wrapped_xy(View *view, f32 seek_x, f32 seek_y, b32 round_down = 0){ + Editing_File *file = view->file; + Models *models = view->models; + Render_Font *font = get_font_info(models->font_set, models->global_font.font_id)->font; + + Full_Cursor result = {}; + if (font){ + f32 max_width = view_compute_width(view); + result = buffer_cursor_from_wrapped_xy(&file->state.buffer, seek_x, seek_y, + round_down, view->line_wrap_y, + max_width, (f32)view->font_height, font->advance_data); + } + + return (result); +} + +internal Full_Cursor +view_compute_cursor_from_line_pos(View *view, i32 line, i32 pos){ + Editing_File *file = view->file; + Models *models = view->models; + Render_Font *font = get_font_info(models->font_set, models->global_font.font_id)->font; + + Full_Cursor result = {}; + if (font){ + f32 max_width = view_compute_width(view); + result = buffer_cursor_from_line_character(&file->state.buffer, line, pos, + view->line_wrap_y, max_width, (f32)view->font_height, font->advance_data); + } + + return (result); +} + +inline Full_Cursor +view_compute_cursor(View *view, Buffer_Seek seek){ + Full_Cursor result = {}; + + switch(seek.type){ + case buffer_seek_pos: + result = view_compute_cursor_from_pos(view, seek.pos); + break; + + case buffer_seek_wrapped_xy: + result = view_compute_cursor_from_wrapped_xy(view, seek.x, seek.y); + break; + + case buffer_seek_unwrapped_xy: + result = view_compute_cursor_from_unwrapped_xy(view, seek.x, seek.y); + break; + + case buffer_seek_line_char: + result = view_compute_cursor_from_line_pos(view, seek.line, seek.character); + break; + } + + return (result); +} + +inline Full_Cursor +view_compute_cursor_from_xy(View *view, f32 seek_x, f32 seek_y){ + Full_Cursor result; + if (view->unwrapped_lines) result = view_compute_cursor_from_unwrapped_xy(view, seek_x, seek_y); + else result = view_compute_cursor_from_wrapped_xy(view, seek_x, seek_y); + return result; +} + +inline void +view_set_temp_highlight(View *view, i32 pos, i32 end_pos){ + view->temp_highlight = view_compute_cursor_from_pos(view, pos); + view->temp_highlight_end_pos = end_pos; + view->show_temp_highlight = 1; +} + +inline i32 +view_get_cursor_pos(View *view){ + i32 result; + if (view->show_temp_highlight){ + result = view->temp_highlight.pos; + } + else{ + result = view->cursor.pos; + } + return result; +} + +inline f32 +view_get_cursor_x(View *view){ + f32 result; + Full_Cursor *cursor; + if (view->show_temp_highlight){ + cursor = &view->temp_highlight; + } + else{ + cursor = &view->cursor; + } + if (view->unwrapped_lines){ + result = cursor->unwrapped_x; + } + else{ + result = cursor->wrapped_x; + } + return result; +} + +inline f32 +view_get_cursor_y(View *view){ + Full_Cursor *cursor; + f32 result; + + if (view->show_temp_highlight) cursor = &view->temp_highlight; + else cursor = &view->cursor; + + if (view->unwrapped_lines) result = cursor->unwrapped_y; + else result = cursor->wrapped_y; + + return result; +} + +internal void +view_set_file( + // NOTE(allen): These parameters are always meaningful + View *view, Editing_File *file, Models *models, + + // NOTE(allen): Necessary when file != 0 + System_Functions *system, Hook_Function *open_hook, Application_Links *app, + + // other + b32 set_vui = 1){ + + Font_Info *fnt_info; + + // TODO(allen): This belongs somewhere else. + fnt_info = get_font_info(models->font_set, models->global_font.font_id); + view->font_advance = fnt_info->advance; + view->font_height = fnt_info->height; + + // NOTE(allen): Stuff that doesn't assume file exists. + view->file = file; + view->cursor = {}; + + // NOTE(allen): Stuff that does assume file exists. + if (file){ + //view->locked = file->settings.super_locked; + view->unwrapped_lines = file->settings.unwrapped_lines; + + if (file_is_ready(file)){ + view_measure_wraps(system, &models->mem.general, view); + view->cursor = view_compute_cursor_from_pos(view, file->state.cursor_pos); + view->reinit_scrolling = 1; + } + } + + // TODO(allen): Bypass all this nonsense, it's a hack! Hooks need parameters! + // Just accept it and pass the file to the open hook when it is loaded. + if (file){ + if (open_hook && file->settings.is_initialized == 0){ + models->buffer_param_indices[models->buffer_param_count++] = file->id.id; + open_hook(app); + models->buffer_param_count = 0; + file->settings.is_initialized = 1; + } + } + + if (set_vui){ + // TODO(allen): Fix this! There should be a way to easily separate setting a file, + // and switching to file mode, so that they don't cross over eachother like this. + view->showing_ui = VUI_None; + } +} + +struct Relative_Scrolling{ + f32 scroll_x, scroll_y; + f32 target_x, target_y; + f32 scroll_min_limit; +}; + +internal Relative_Scrolling +view_get_relative_scrolling(View *view){ + Relative_Scrolling result; + f32 cursor_y; + cursor_y = view_get_cursor_y(view); + result.scroll_y = cursor_y - view->scroll_y; + result.target_y = cursor_y - view->target_y; + result.scroll_min_limit = view->scroll_min_limit; + return result; +} + +internal void +view_set_relative_scrolling(View *view, Relative_Scrolling scrolling){ + f32 cursor_y; + cursor_y = view_get_cursor_y(view); + view->scroll_y = cursor_y - scrolling.scroll_y; + view->target_y = cursor_y - scrolling.target_y; + if (view->target_y < scrolling.scroll_min_limit) view->target_y = scrolling.scroll_min_limit; +} + +inline void +view_cursor_move(View *view, Full_Cursor cursor){ + view->cursor = cursor; + view->preferred_x = view_get_cursor_x(view); + view->file->state.cursor_pos = view->cursor.pos; + view->show_temp_highlight = 0; +} + +inline void +view_cursor_move(View *view, i32 pos){ + Full_Cursor cursor = view_compute_cursor_from_pos(view, pos); + view_cursor_move(view, cursor); +} + +inline void +view_cursor_move(View *view, f32 x, f32 y, b32 round_down = 0){ + Full_Cursor cursor; + if (view->unwrapped_lines){ + cursor = view_compute_cursor_from_unwrapped_xy(view, x, y, round_down); + } + else{ + cursor = view_compute_cursor_from_wrapped_xy(view, x, y, round_down); + } + view_cursor_move(view, cursor); +} + +inline void +view_cursor_move(View *view, i32 line, i32 pos){ + Full_Cursor cursor = view_compute_cursor_from_line_pos(view, line, pos); + view_cursor_move(view, cursor); +} + +inline void +view_set_widget(View *view, View_Widget_Type type){ + view->widget.type = type; +} + + +inline i32_Rect +view_widget_rect(View *view, i32 font_height){ + Panel *panel = view->panel; + i32_Rect result = panel->inner; + + if (view->file){ + result.y0 = result.y0 + font_height + 2; + } + + return(result); +} + +enum History_Mode{ + hist_normal, + hist_backward, + hist_forward +}; + +internal void +file_update_history_before_edit(Mem_Options *mem, Editing_File *file, Edit_Step step, u8 *str, + History_Mode history_mode){ + if (!file->state.undo.undo.edits) return; + General_Memory *general = &mem->general; + + b32 can_merge = 0, do_merge = 0; + switch (step.type){ + case ED_NORMAL: + { + if (step.edit.len == 1 && str && char_is_alpha_numeric(*str)) can_merge = 1; + if (step.edit.len == 1 && str && (can_merge || char_is_whitespace(*str))) do_merge = 1; + + if (history_mode != hist_forward) + file_post_history(general, file, step, do_merge, can_merge); + + file_post_undo(general, file, step, do_merge, can_merge); + }break; + + case ED_REVERSE_NORMAL: + { + if (history_mode != hist_forward) + file_post_history(general, file, step, do_merge, can_merge); + + undo_stack_pop(&file->state.undo.undo); + + b32 restore_redos = 0; + Edit_Step *redo_end = 0; + + if (history_mode == hist_backward && file->state.undo.edit_history_cursor > 0){ + restore_redos = 1; + redo_end = file->state.undo.history.edits + (file->state.undo.edit_history_cursor - 1); + } + else if (history_mode == hist_forward && file->state.undo.history.edit_count > 0){ + restore_redos = 1; + redo_end = file->state.undo.history.edits + (file->state.undo.history.edit_count - 1); + } + + if (restore_redos){ + Edit_Step *redo_start = redo_end; + i32 steps_of_redo = 0; + i32 strings_of_redo = 0; + i32 undo_count = 0; + while (redo_start->type == ED_REDO || redo_start->type == ED_UNDO){ + if (redo_start->type == ED_REDO){ + if (undo_count > 0) --undo_count; + else{ + ++steps_of_redo; + strings_of_redo += redo_start->edit.len; + } + } + else{ + ++undo_count; + } + --redo_start; + } + + if (redo_start < redo_end){ + ++redo_start; + ++redo_end; + + if (file->state.undo.redo.edit_count + steps_of_redo > file->state.undo.redo.edit_max) + undo_stack_grow_edits(general, &file->state.undo.redo); + + if (file->state.undo.redo.size + strings_of_redo > file->state.undo.redo.max) + undo_stack_grow_string(general, &file->state.undo.redo, strings_of_redo); + + u8 *str_src = file->state.undo.history.strings + redo_end->edit.str_start; + u8 *str_dest_base = file->state.undo.redo.strings; + i32 str_redo_pos = file->state.undo.redo.size + strings_of_redo; + + Edit_Step *edit_src = redo_end; + Edit_Step *edit_dest = + file->state.undo.redo.edits + file->state.undo.redo.edit_count + steps_of_redo; + + i32 undo_count = 0; + for (i32 i = 0; i < steps_of_redo;){ + --edit_src; + str_src -= edit_src->edit.len; + if (edit_src->type == ED_REDO){ + if (undo_count > 0){ + --undo_count; + } + else{ + ++i; + + --edit_dest; + *edit_dest = *edit_src; + + str_redo_pos -= edit_dest->edit.len; + edit_dest->edit.str_start = str_redo_pos; + + memcpy(str_dest_base + str_redo_pos, str_src, edit_dest->edit.len); + } + } + else{ + ++undo_count; + } + } + Assert(undo_count == 0); + + file->state.undo.redo.size += strings_of_redo; + file->state.undo.redo.edit_count += steps_of_redo; + } + } + }break; + + case ED_UNDO: + { + if (history_mode != hist_forward) + file_post_history(general, file, step, do_merge, can_merge); + file_post_redo(general, file, step); + undo_stack_pop(&file->state.undo.undo); + }break; + + case ED_REDO: + { + if (step.edit.len == 1 && str && char_is_alpha_numeric(*str)) can_merge = 1; + if (step.edit.len == 1 && str && (can_merge || char_is_whitespace(*str))) do_merge = 1; + + if (history_mode != hist_forward) + file_post_history(general, file, step, do_merge, can_merge); + + file_post_undo(general, file, step, do_merge, can_merge); + undo_stack_pop(&file->state.undo.redo); + }break; + } + + if (history_mode != hist_forward){ + if (step.type == ED_UNDO || step.type == ED_REDO){ + if (file->state.undo.current_block_normal){ + file_post_history_block(file, file->state.undo.history.edit_count - 1); + file->state.undo.current_block_normal = 0; + } + } + else{ + if (!file->state.undo.current_block_normal){ + file_post_history_block(file, file->state.undo.history.edit_count - 1); + file->state.undo.current_block_normal = 1; + } + } + } + else{ + if (file->state.undo.history_head_block == file->state.undo.history.edit_count){ + file_unpost_history_block(file); + file->state.undo.current_block_normal = !file->state.undo.current_block_normal; + } + } + + if (history_mode == hist_normal){ + file->state.undo.edit_history_cursor = file->state.undo.history.edit_count; + } +} + +inline void +file_pre_edit_maintenance(System_Functions *system, + General_Memory *general, + Editing_File *file){ + if (file->state.still_lexing){ + system->cancel_job(BACKGROUND_THREADS, file->state.lex_job); + if (file->state.swap_stack.tokens){ + general_memory_free(general, file->state.swap_stack.tokens); + file->state.swap_stack.tokens = 0; + } + } + file->state.last_4ed_edit_time = system->time(); +} + +struct Cursor_Fix_Descriptor{ + b32 is_batch; + union{ + struct{ + Buffer_Edit *batch; + i32 batch_size; + }; + struct{ + i32 start, end; + i32 shift_amount; + }; + }; +}; + +internal void +file_edit_cursor_fix(System_Functions *system, + Partition *part, General_Memory *general, + Editing_File *file, Editing_Layout *layout, + Cursor_Fix_Descriptor desc){ + + Full_Cursor temp_cursor; + Temp_Memory cursor_temp = begin_temp_memory(part); + i32 cursor_max = layout->panel_max_count * 2; + Cursor_With_Index *cursors = push_array(part, Cursor_With_Index, cursor_max); + + f32 y_offset = 0, y_position = 0; + i32 cursor_count = 0; + + View *view; + Panel *panel, *used_panels; + used_panels = &layout->used_sentinel; + + for (dll_items(panel, used_panels)){ + view = panel->view; + if (view->file == file){ + view_measure_wraps(system, general, view); + write_cursor_with_index(cursors, &cursor_count, view->cursor.pos); + write_cursor_with_index(cursors, &cursor_count, view->mark - 1); + write_cursor_with_index(cursors, &cursor_count, view->scroll_i - 1); + } + } + + if (cursor_count > 0){ + buffer_sort_cursors(cursors, cursor_count); + if (desc.is_batch){ + buffer_batch_edit_update_cursors(cursors, cursor_count, + desc.batch, desc.batch_size); + } + else{ + buffer_update_cursors(cursors, cursor_count, + desc.start, desc.end, + desc.shift_amount + (desc.end - desc.start)); + } + buffer_unsort_cursors(cursors, cursor_count); + + cursor_count = 0; + for (dll_items(panel, used_panels)){ + view = panel->view; + if (view && view->file == file){ + view_cursor_move(view, cursors[cursor_count++].pos); + view->preferred_x = view_get_cursor_x(view); + + view->mark = cursors[cursor_count++].pos + 1; + i32 new_scroll_i = cursors[cursor_count++].pos + 1; + if (view->scroll_i != new_scroll_i){ + view->scroll_i = new_scroll_i; + temp_cursor = view_compute_cursor_from_pos(view, view->scroll_i); + y_offset = MOD(view->scroll_y, view->font_height); + + if (view->unwrapped_lines){ + y_position = temp_cursor.unwrapped_y + y_offset; + view->target_y += (y_position - view->scroll_y); + view->scroll_y = y_position; + } + else{ + y_position = temp_cursor.wrapped_y + y_offset; + view->target_y += (y_position - view->scroll_y); + view->scroll_y = y_position; + } + } + } + } + } + + end_temp_memory(cursor_temp); +} + +internal void +file_do_single_edit(System_Functions *system, + Models *models, Editing_File *file, + Edit_Spec spec, History_Mode history_mode, b32 use_high_permission = 0){ + ProfileMomentFunction(); + if (!use_high_permission && file->settings.read_only) return; + + Mem_Options *mem = &models->mem; + Editing_Layout *layout = &models->layout; + + // NOTE(allen): fixing stuff beforewards???? + file_update_history_before_edit(mem, file, spec.step, spec.str, history_mode); + file_pre_edit_maintenance(system, &mem->general, file); + + // NOTE(allen): actual text replacement + i32 shift_amount = 0; + General_Memory *general = &mem->general; + Partition *part = &mem->part; + + char *str = (char*)spec.str; + i32 start = spec.step.edit.start; + i32 end = spec.step.edit.end; + i32 str_len = spec.step.edit.len; + + i32 scratch_size = partition_remaining(part); + + Assert(scratch_size > 0); + i32 request_amount = 0; + Assert(end <= buffer_size(&file->state.buffer)); + while (buffer_replace_range(&file->state.buffer, start, end, str, str_len, &shift_amount, + part->base + part->pos, scratch_size, &request_amount)){ + void *new_data = 0; + if (request_amount > 0){ + new_data = general_memory_allocate(general, request_amount, BUBBLE_BUFFER); + } + void *old_data = buffer_edit_provide_memory(&file->state.buffer, new_data, request_amount); + if (old_data) general_memory_free(general, old_data); + } + + Buffer_Type *buffer = &file->state.buffer; + i32 line_start = buffer_get_line_index(&file->state.buffer, start); + i32 line_end = buffer_get_line_index(&file->state.buffer, end); + i32 replaced_line_count = line_end - line_start; + i32 new_line_count = buffer_count_newlines(&file->state.buffer, start, start+str_len); + i32 line_shift = new_line_count - replaced_line_count; + + Render_Font *font = get_font_info(models->font_set, file->state.font_id)->font; + + file_grow_starts_widths_as_needed(general, buffer, line_shift); + buffer_remeasure_starts(buffer, line_start, line_end, line_shift, shift_amount); + buffer_remeasure_widths(buffer, font->advance_data, line_start, line_end, line_shift); + + // NOTE(allen): update the views looking at this file + Panel *panel, *used_panels; + used_panels = &layout->used_sentinel; + + for (dll_items(panel, used_panels)){ + View *view = panel->view; + if (view->file == file){ + view_measure_wraps(system, general, view); + } + } + +#if BUFFER_EXPERIMENT_SCALPEL <= 0 + // NOTE(allen): fixing stuff afterwards + if (file->settings.tokens_exist) + file_relex_parallel(system, mem, file, start, end, shift_amount); +#endif + + Cursor_Fix_Descriptor desc = {}; + desc.start = start; + desc.end = end; + desc.shift_amount = shift_amount; + + file_edit_cursor_fix(system, part, general, file, layout, desc); +} + +internal void +file_do_white_batch_edit(System_Functions *system, Models *models, Editing_File *file, + Edit_Spec spec, History_Mode history_mode, b32 use_high_permission = 0){ + ProfileMomentFunction(); + if (!use_high_permission && file->settings.read_only) return; + + Mem_Options *mem = &models->mem; + Editing_Layout *layout = &models->layout; + + // NOTE(allen): fixing stuff "beforewards"??? + Assert(spec.str == 0); + file_update_history_before_edit(mem, file, spec.step, 0, history_mode); + file_pre_edit_maintenance(system, &mem->general, file); + + // NOTE(allen): actual text replacement + General_Memory *general = &mem->general; + Partition *part = &mem->part; + + u8 *str_base = file->state.undo.children.strings; + i32 batch_size = spec.step.child_count; + Buffer_Edit *batch = file->state.undo.children.edits + spec.step.first_child; + + Assert(spec.step.first_child < file->state.undo.children.edit_count); + Assert(batch_size >= 0); + + i32 scratch_size = partition_remaining(part); + Buffer_Batch_State state = {}; + i32 request_amount; + while (buffer_batch_edit_step(&state, &file->state.buffer, batch, + (char*)str_base, batch_size, part->base + part->pos, + scratch_size, &request_amount)){ + void *new_data = 0; + if (request_amount > 0){ + new_data = general_memory_allocate(general, request_amount, BUBBLE_BUFFER); + } + void *old_data = buffer_edit_provide_memory(&file->state.buffer, new_data, request_amount); + if (old_data) general_memory_free(general, old_data); + } + + // NOTE(allen): meta data + { + Buffer_Measure_Starts state = {}; + Render_Font *font = get_font_info(models->font_set, file->state.font_id)->font; + float *advance_data = 0; + if (font) advance_data = font->advance_data; + buffer_measure_starts_widths(&state, &file->state.buffer, advance_data); + } + + // NOTE(allen): cursor fixing + { + Cursor_Fix_Descriptor desc = {}; + desc.is_batch = 1; + desc.batch = batch; + desc.batch_size = batch_size; + + file_edit_cursor_fix(system, part, general, file, layout, desc); + } + + // NOTE(allen): token fixing + if (file->state.tokens_complete){ + Cpp_Token_Stack tokens = file->state.token_stack; + Cpp_Token *token = tokens.tokens; + Cpp_Token *end_token = tokens.tokens + tokens.count; + + Buffer_Edit *edit = batch; + Buffer_Edit *end_edit = batch + batch_size; + + i32 shift_amount = 0; + i32 local_shift = 0; + + for (; token < end_token && edit < end_edit; ++edit){ + local_shift = (edit->len - (edit->end - edit->start)); + for (; token->start < edit->start && edit->start < token->start + token->size && + token < end_token; ++token){ + token->size += local_shift; + } + for (; token->start < edit->start && token < end_token; ++token){ + token->start += shift_amount; + } + shift_amount += local_shift; + } + for (; token < end_token; ++token){ + token->start += shift_amount; + } + } +} + +inline void +file_replace_range(System_Functions *system, Models *models, Editing_File *file, + i32 start, i32 end, char *str, i32 len, i32 next_cursor, b32 use_high_permission = 0){ + Edit_Spec spec = {}; + spec.step.type = ED_NORMAL; + spec.step.edit.start = start; + spec.step.edit.end = end; + spec.step.edit.len = len; + spec.step.pre_pos = file->state.cursor_pos; + spec.step.post_pos = next_cursor; + spec.str = (u8*)str; + file_do_single_edit(system, models, file, spec, hist_normal, use_high_permission); +} + +inline void +file_clear(System_Functions *system, Models *models, Editing_File *file, b32 use_high_permission = 0){ + file_replace_range(system, models, file, 0, buffer_size(&file->state.buffer), 0, 0, 0, use_high_permission); +} + +inline void +view_replace_range(System_Functions *system, Models *models, View *view, + i32 start, i32 end, char *str, i32 len, i32 next_cursor){ + file_replace_range(system, models, view->file, start, end, str, len, next_cursor); +} + +inline void +view_post_paste_effect(View *view, i32 ticks, i32 start, i32 size, u32 color){ + Editing_File *file = view->file; + + file->state.paste_effect.start = start; + file->state.paste_effect.end = start + size; + file->state.paste_effect.color = color; + file->state.paste_effect.tick_down = ticks; + file->state.paste_effect.tick_max = ticks; +} + +internal void +view_undo_redo(System_Functions *system, + Models *models, View *view, + Edit_Stack *stack, Edit_Type expected_type){ + Editing_File *file = view->file; + + if (stack->edit_count > 0){ + Edit_Step step = stack->edits[stack->edit_count-1]; + + Assert(step.type == expected_type); + + Edit_Spec spec = {}; + spec.step = step; + + if (step.child_count == 0){ + spec.step.edit.str_start = 0; + spec.str = stack->strings + step.edit.str_start; + + file_do_single_edit(system, models, file, spec, hist_normal); + + if (expected_type == ED_UNDO) view_cursor_move(view, step.pre_pos); + else view_cursor_move(view, step.post_pos); + view->mark = view->cursor.pos; + + view_post_paste_effect(view, 10, step.edit.start, step.edit.len, + models->style.main.undo_color); + } + else{ + TentativeAssert(spec.step.special_type == 1); + file_do_white_batch_edit(system, models, view->file, spec, hist_normal); + } + } +} + +inline void +view_undo(System_Functions *system, Models *models, View *view){ + view_undo_redo(system, models, view, &view->file->state.undo.undo, ED_UNDO); +} + +inline void +view_redo(System_Functions *system, Models *models, View *view){ + view_undo_redo(system, models, view, &view->file->state.undo.redo, ED_REDO); +} + +inline u8* +write_data(u8 *ptr, void *x, i32 size){ + memcpy(ptr, x, size); + return (ptr + size); +} + +#define UseFileHistoryDump 0 + +#if UseFileHistoryDump +internal void +file_dump_history(System_Functions *system, Mem_Options *mem, Editing_File *file, char *filename){ + if (!file->state.undo.undo.edits) return; + + i32 size = 0; + + size += sizeof(i32); + size += file->state.undo.undo.edit_count*sizeof(Edit_Step); + size += sizeof(i32); + size += file->state.undo.redo.edit_count*sizeof(Edit_Step); + size += sizeof(i32); + size += file->state.undo.history.edit_count*sizeof(Edit_Step); + size += sizeof(i32); + size += file->state.undo.children.edit_count*sizeof(Buffer_Edit); + + size += sizeof(i32); + size += file->state.undo.undo.size; + size += sizeof(i32); + size += file->state.undo.redo.size; + size += sizeof(i32); + size += file->state.undo.history.size; + size += sizeof(i32); + size += file->state.undo.children.size; + + Partition *part = &mem->part; + i32 remaining = partition_remaining(part); + if (size < remaining){ + u8 *data, *curs; + data = (u8*)part->base + part->pos; + curs = data; + curs = write_data(curs, &file->state.undo.undo.edit_count, 4); + curs = write_data(curs, &file->state.undo.redo.edit_count, 4); + curs = write_data(curs, &file->state.undo.history.edit_count, 4); + curs = write_data(curs, &file->state.undo.children.edit_count, 4); + curs = write_data(curs, &file->state.undo.undo.size, 4); + curs = write_data(curs, &file->state.undo.redo.size, 4); + curs = write_data(curs, &file->state.undo.history.size, 4); + curs = write_data(curs, &file->state.undo.children.size, 4); + + curs = write_data(curs, file->state.undo.undo.edits, sizeof(Edit_Step)*file->state.undo.undo.edit_count); + curs = write_data(curs, file->state.undo.redo.edits, sizeof(Edit_Step)*file->state.undo.redo.edit_count); + curs = write_data(curs, file->state.undo.history.edits, sizeof(Edit_Step)*file->state.undo.history.edit_count); + curs = write_data(curs, file->state.undo.children.edits, sizeof(Buffer_Edit)*file->state.undo.children.edit_count); + + curs = write_data(curs, file->state.undo.undo.strings, file->state.undo.undo.size); + curs = write_data(curs, file->state.undo.redo.strings, file->state.undo.redo.size); + curs = write_data(curs, file->state.undo.history.strings, file->state.undo.history.size); + curs = write_data(curs, file->state.undo.children.strings, file->state.undo.children.size); + + Assert((i32)(curs - data) == size); + system->save_file(filename, data, size); + } +} +#endif + +internal void +view_history_step(System_Functions *system, Models *models, View *view, History_Mode history_mode){ + Assert(history_mode != hist_normal); + + Editing_File *file = view->file; + + b32 do_history_step = 0; + Edit_Step step = {}; + if (history_mode == hist_backward){ + if (file->state.undo.edit_history_cursor > 0){ + do_history_step = 1; + step = file->state.undo.history.edits[--file->state.undo.edit_history_cursor]; + } + } + else{ + if (file->state.undo.edit_history_cursor < file->state.undo.history.edit_count){ + Assert(((file->state.undo.history.edit_count - file->state.undo.edit_history_cursor) & 1) == 0); + step = file->state.undo.history.edits[--file->state.undo.history.edit_count]; + file->state.undo.history.size -= step.edit.len; + ++file->state.undo.edit_history_cursor; + do_history_step = 1; + } + } + + if (do_history_step){ + Edit_Spec spec; + spec.step = step; + + if (spec.step.child_count == 0){ + spec.step.edit.str_start = 0; + spec.str = file->state.undo.history.strings + step.edit.str_start; + + file_do_single_edit(system, models, file, spec, history_mode); + + switch (spec.step.type){ + case ED_NORMAL: + case ED_REDO: + view_cursor_move(view, step.post_pos); + break; + + case ED_REVERSE_NORMAL: + case ED_UNDO: + view_cursor_move(view, step.pre_pos); + break; + } + view->mark = view->cursor.pos; + } + else{ + TentativeAssert(spec.step.special_type == 1); + file_do_white_batch_edit(system, models, view->file, spec, history_mode); + } + } +} + +// TODO(allen): write these as streamed operations +internal i32 +view_find_end_of_line(View *view, i32 pos){ +#if BUFFER_EXPERIMENT_SCALPEL <= 0 + Editing_File *file = view->file; + char *data = file->state.buffer.data; + while (pos < file->state.buffer.size && data[pos] != '\n') ++pos; + if (pos > file->state.buffer.size) pos = file->state.buffer.size; +#endif + return pos; +} + +internal i32 +view_find_beginning_of_line(View *view, i32 pos){ +#if BUFFER_EXPERIMENT_SCALPEL <= 0 + Editing_File *file = view->file; + char *data = file->state.buffer.data; + if (pos > 0){ + --pos; + while (pos > 0 && data[pos] != '\n') --pos; + if (pos != 0) ++pos; + } +#endif + return pos; +} + +internal i32 +view_find_beginning_of_next_line(View *view, i32 pos){ +#if BUFFER_EXPERIMENT_SCALPEL <= 0 + Editing_File *file = view->file; + char *data = file->state.buffer.data; + while (pos < file->state.buffer.size && + !starts_new_line(data[pos])){ + ++pos; + } + if (pos < file->state.buffer.size){ + ++pos; + } +#endif + return pos; +} + +internal String* +working_set_next_clipboard_string(General_Memory *general, Working_Set *working, i32 str_size){ + String *result = 0; + i32 clipboard_current = working->clipboard_current; + if (working->clipboard_size == 0){ + clipboard_current = 0; + working->clipboard_size = 1; + } + else{ + ++clipboard_current; + if (clipboard_current >= working->clipboard_max_size){ + clipboard_current = 0; + } + else if (working->clipboard_size <= clipboard_current){ + working->clipboard_size = clipboard_current+1; + } + } + result = &working->clipboards[clipboard_current]; + working->clipboard_current = clipboard_current; + working->clipboard_rolling = clipboard_current; + char *new_str; + if (result->str){ + new_str = (char*)general_memory_reallocate(general, result->str, result->size, str_size); + } + else{ + new_str = (char*)general_memory_allocate(general, str_size+1); + } + // TODO(allen): What if new_str == 0? + *result = make_string(new_str, 0, str_size); + return result; +} + +internal String* +working_set_clipboard_head(Working_Set *working){ + String *result = 0; + if (working->clipboard_size > 0){ + i32 clipboard_index = working->clipboard_current; + working->clipboard_rolling = clipboard_index; + result = &working->clipboards[clipboard_index]; + } + return result; +} + +internal String* +working_set_clipboard_roll_down(Working_Set *working){ + String *result = 0; + if (working->clipboard_size > 0){ + i32 clipboard_index = working->clipboard_rolling; + --clipboard_index; + if (clipboard_index < 0){ + clipboard_index = working->clipboard_size-1; + } + working->clipboard_rolling = clipboard_index; + result = &working->clipboards[clipboard_index]; + } + return result; +} + +internal void +clipboard_copy(System_Functions *system, General_Memory *general, Working_Set *working, Range range, Editing_File *file){ + i32 size = range.end - range.start; + String *dest = working_set_next_clipboard_string(general, working, size); + buffer_stringify(&file->state.buffer, range.start, range.end, dest->str); + dest->size = size; + system->post_clipboard(*dest); +} + +internal Edit_Spec +file_compute_whitespace_edit(Mem_Options *mem, Editing_File *file, i32 cursor_pos, + Buffer_Edit *edits, char *str_base, i32 str_size, + Buffer_Edit *inverse_array, char *inv_str, i32 inv_max, + i32 edit_count){ + General_Memory *general = &mem->general; + + i32 inv_str_pos = 0; + Buffer_Invert_Batch state = {}; + if (buffer_invert_batch(&state, &file->state.buffer, edits, edit_count, + inverse_array, inv_str, &inv_str_pos, inv_max)) + Assert(0); + + i32 first_child = + undo_children_push(general, &file->state.undo.children, + edits, edit_count, (u8*)(str_base), str_size); + i32 inverse_first_child = + undo_children_push(general, &file->state.undo.children, + inverse_array, edit_count, (u8*)(inv_str), inv_str_pos); + + Edit_Spec spec = {}; + spec.step.type = ED_NORMAL; + spec.step.first_child = first_child; + spec.step.inverse_first_child = inverse_first_child; + spec.step.special_type = 1; + spec.step.child_count = edit_count; + spec.step.inverse_child_count = edit_count; + spec.step.pre_pos = cursor_pos; + spec.step.post_pos = cursor_pos; + + return spec; +} + +internal void +view_clean_whitespace(System_Functions *system, Models *models, View *view){ + Mem_Options *mem = &models->mem; + Editing_File *file = view->file; + + Partition *part = &mem->part; + i32 line_count = file->state.buffer.line_count; + i32 edit_max = line_count * 2; + i32 edit_count = 0; + + Assert(file && !file->state.is_dummy); + + Temp_Memory temp = begin_temp_memory(part); + Buffer_Edit *edits = push_array(part, Buffer_Edit, edit_max); + + char *str_base = (char*)part->base + part->pos; + i32 str_size = 0; + for (i32 line_i = 0; line_i < line_count; ++line_i){ + i32 start = file->state.buffer.line_starts[line_i]; + i32 preferred_indentation; + b32 all_whitespace = 0; + b32 all_space = 0; + i32 hard_start = + buffer_find_hard_start(&file->state.buffer, start, &all_whitespace, &all_space, + &preferred_indentation, 4); + + if (all_whitespace) preferred_indentation = 0; + + if ((all_whitespace && hard_start > start) || !all_space){ + Buffer_Edit new_edit; + new_edit.str_start = str_size; + str_size += preferred_indentation; + char *str = push_array(part, char, preferred_indentation); + for (i32 j = 0; j < preferred_indentation; ++j) str[j] = ' '; + new_edit.len = preferred_indentation; + new_edit.start = start; + new_edit.end = hard_start; + edits[edit_count++] = new_edit; + } + Assert(edit_count <= edit_max); + } + + if (edit_count > 0){ + Assert(buffer_batch_debug_sort_check(edits, edit_count)); + + // NOTE(allen): computing edit spec, doing batch edit + Buffer_Edit *inverse_array = push_array(part, Buffer_Edit, edit_count); + Assert(inverse_array); + + char *inv_str = (char*)part->base + part->pos; + Edit_Spec spec = + file_compute_whitespace_edit(mem, file, view->cursor.pos, edits, str_base, str_size, + inverse_array, inv_str, part->max - part->pos, edit_count); + + file_do_white_batch_edit(system, models, view->file, spec, hist_normal); + } + + end_temp_memory(temp); +} + +internal void +view_auto_tab_tokens(System_Functions *system, + Models *models, View *view, + i32 start, i32 end, b32 empty_blank_lines, b32 use_tabs){ +#if BUFFER_EXPERIMENT_SCALPEL <= 0 + Editing_File *file = view->file; + Mem_Options *mem = &models->mem; + Partition *part = &mem->part; + Buffer *buffer = &file->state.buffer; + + Assert(file && !file->state.is_dummy); + Cpp_Token_Stack tokens = file->state.token_stack; + Assert(tokens.tokens); + + i32 line_start = buffer_get_line_index(buffer, start); + i32 line_end = buffer_get_line_index(buffer, end) + 1; + + i32 edit_max = (line_end - line_start) * 2; + i32 edit_count = 0; + + i32 indent_mark_count = line_end - line_start; + + Temp_Memory temp = begin_temp_memory(part); + i32 *indent_marks = push_array(part, i32, indent_mark_count); + { + i32 current_indent = 0; + i32 token_i; + Cpp_Token *token, *self_token; + + { + i32 start_pos = file->state.buffer.line_starts[line_start]; + Cpp_Get_Token_Result result = cpp_get_token(&tokens, start_pos); + token_i = result.token_index; + if (result.in_whitespace) token_i += 1; + self_token = tokens.tokens + token_i; + } + + i32 line = line_start - 1; + for (; line >= 0; --line){ + i32 start = file->state.buffer.line_starts[line]; + b32 all_whitespace = 0; + b32 all_space = 0; + buffer_find_hard_start(&file->state.buffer, start, + &all_whitespace, &all_space, ¤t_indent, 4); + if (!all_whitespace) break; + } + + if (line < 0){ + token_i = 0; + token = tokens.tokens + token_i; + } + else{ + i32 start_pos = file->state.buffer.line_starts[line]; + Cpp_Get_Token_Result result = cpp_get_token(&tokens, start_pos); + token_i = result.token_index; + if (result.in_whitespace) token_i += 1; + token = tokens.tokens + token_i; + + while (token >= tokens.tokens && + token->flags & CPP_TFLAG_PP_DIRECTIVE || + token->flags & CPP_TFLAG_PP_BODY){ + --token; + } + + if (token < tokens.tokens){ + ++token; + current_indent = 0; + } + else if (token->start < start_pos){ + line = buffer_get_line_index(&file->state.buffer, token->start); + i32 start = file->state.buffer.line_starts[line]; + b32 all_whitespace = 0; + b32 all_space = 0; + buffer_find_hard_start(&file->state.buffer, start, + &all_whitespace, &all_space, ¤t_indent, 4); + Assert(!all_whitespace); + } + } + + indent_marks -= line_start; + i32 line_i = line_start; + i32 next_line_start = file->state.buffer.line_starts[line_i]; + switch (token->type){ + case CPP_TOKEN_BRACKET_OPEN: current_indent += 4; break; + case CPP_TOKEN_PARENTHESE_OPEN: current_indent += 4; break; + case CPP_TOKEN_BRACE_OPEN: current_indent += 4; break; + } + + Cpp_Token *prev_token = token; + ++token; + for (; line_i < line_end; ++token_i, ++token){ + for (; token->start >= next_line_start && line_i < line_end;){ + i32 this_line_start = next_line_start; + next_line_start = file->state.buffer.line_starts[line_i+1]; + i32 this_indent; + if (prev_token && prev_token->type == CPP_TOKEN_COMMENT && + prev_token->start <= this_line_start && prev_token->start + prev_token->size > this_line_start){ + this_indent = -1; + } + else{ + this_indent = current_indent; + if (token->start < next_line_start){ + if (token->flags & CPP_TFLAG_PP_DIRECTIVE) this_indent = 0; + else{ + switch (token->type){ + case CPP_TOKEN_BRACKET_CLOSE: this_indent -= 4; break; + case CPP_TOKEN_PARENTHESE_CLOSE: this_indent -= 4; break; + case CPP_TOKEN_BRACE_CLOSE: this_indent -= 4; break; + case CPP_TOKEN_BRACE_OPEN: break; + default: + if (current_indent > 0 && prev_token){ + if (!(prev_token->flags & CPP_TFLAG_PP_BODY || + prev_token->flags & CPP_TFLAG_PP_DIRECTIVE)){ + switch (prev_token->type){ + case CPP_TOKEN_BRACKET_OPEN: case CPP_TOKEN_PARENTHESE_OPEN: + case CPP_TOKEN_BRACE_OPEN: case CPP_TOKEN_BRACE_CLOSE: + case CPP_TOKEN_SEMICOLON: case CPP_TOKEN_COLON: break; + case CPP_TOKEN_COMMA: case CPP_TOKEN_COMMENT: break; + default: this_indent += 4; + } + } + } + } + } + } + if (this_indent < 0) this_indent = 0; + } + indent_marks[line_i] = this_indent; + ++line_i; + } + + switch (token->type){ + case CPP_TOKEN_BRACKET_OPEN: current_indent += 4; break; + case CPP_TOKEN_BRACKET_CLOSE: current_indent -= 4; break; + case CPP_TOKEN_PARENTHESE_OPEN: current_indent += 4; break; + case CPP_TOKEN_PARENTHESE_CLOSE: current_indent -= 4; break; + case CPP_TOKEN_BRACE_OPEN: current_indent += 4; break; + case CPP_TOKEN_BRACE_CLOSE: current_indent -= 4; break; + } + prev_token = token; + } + } + + Buffer_Edit *edits = push_array(part, Buffer_Edit, edit_max); + + char *str_base = (char*)part->base + part->pos; + i32 str_size = 0; + for (i32 line_i = line_start; line_i < line_end; ++line_i){ + i32 start = file->state.buffer.line_starts[line_i]; + i32 preferred_indentation; + i32 correct_indentation; + b32 all_whitespace = 0; + b32 all_space = 0; + i32 tab_width = 4; + i32 hard_start = + buffer_find_hard_start(&file->state.buffer, start, &all_whitespace, &all_space, + &preferred_indentation, tab_width); + + correct_indentation = indent_marks[line_i]; + if (all_whitespace && empty_blank_lines) correct_indentation = 0; + if (correct_indentation == -1) correct_indentation = preferred_indentation; + + if ((all_whitespace && hard_start > start) || !all_space || correct_indentation != preferred_indentation){ + Buffer_Edit new_edit; + new_edit.str_start = str_size; + str_size += correct_indentation; + char *str = push_array(part, char, correct_indentation); + i32 j = 0; + if (use_tabs){ + i32 i = 0; + for (; i + tab_width <= correct_indentation; i += tab_width) str[j++] = '\t'; + for (; i < correct_indentation; ++i) str[j++] = ' '; + } + else{ + for (; j < correct_indentation; ++j) str[j] = ' '; + } + new_edit.len = j; + new_edit.start = start; + new_edit.end = hard_start; + edits[edit_count++] = new_edit; + } + + Assert(edit_count <= edit_max); + } + + if (edit_count > 0){ + Assert(buffer_batch_debug_sort_check(edits, edit_count)); + + // NOTE(allen): computing edit spec, doing batch edit + Buffer_Edit *inverse_array = push_array(part, Buffer_Edit, edit_count); + Assert(inverse_array); + + char *inv_str = (char*)part->base + part->pos; + Edit_Spec spec = + file_compute_whitespace_edit(mem, file, view->cursor.pos, edits, str_base, str_size, + inverse_array, inv_str, part->max - part->pos, edit_count); + + file_do_white_batch_edit(system, models, view->file, spec, hist_normal); + } + + { + b32 all_whitespace = 0; + b32 all_space = 0; + i32 preferred_indentation; + i32 start = view->cursor.pos; + i32 hard_start = buffer_find_hard_start( + &file->state.buffer, start, &all_whitespace, &all_space, + &preferred_indentation, 4); + + view_cursor_move(view, hard_start); + } + + end_temp_memory(temp); +#endif +} + +struct Get_Link_Result{ + b32 in_link; + i32 index; +}; + +internal u32* +style_get_color(Style *style, Cpp_Token token){ + u32 *result; + if (token.flags & CPP_TFLAG_IS_KEYWORD){ + if (token.type == CPP_TOKEN_BOOLEAN_CONSTANT){ + result = &style->main.bool_constant_color; + } + else{ + result = &style->main.keyword_color; + } + } + else if(token.flags & CPP_TFLAG_PP_DIRECTIVE){ + result = &style->main.preproc_color; + } + else{ + switch (token.type){ + case CPP_TOKEN_COMMENT: + result = &style->main.comment_color; + break; + + case CPP_TOKEN_STRING_CONSTANT: + result = &style->main.str_constant_color; + break; + + case CPP_TOKEN_CHARACTER_CONSTANT: + result = &style->main.char_constant_color; + break; + + case CPP_TOKEN_INTEGER_CONSTANT: + result = &style->main.int_constant_color; + break; + + case CPP_TOKEN_FLOATING_CONSTANT: + result = &style->main.float_constant_color; + break; + + case CPP_TOKEN_INCLUDE_FILE: + result = &style->main.include_color; + break; + + default: + result = &style->main.default_color; + } + } + return result; +} + +inline f32 +view_compute_max_target_y(i32 lowest_line, i32 line_height, f32 view_height){ + real32 max_target_y = ((lowest_line+.5f)*line_height) - view_height*.5f; + return max_target_y; +} + +internal f32 +view_compute_max_target_y(View *view){ + i32 lowest_line = view_compute_lowest_line(view); + i32 line_height = view->font_height; + f32 view_height = view_compute_height(view); + f32 max_target_y = view_compute_max_target_y( + lowest_line, line_height, view_height); + return max_target_y; +} + +internal void +remeasure_file_view(System_Functions *system, View *view, i32_Rect rect){ + if (file_is_ready(view->file)){ + Relative_Scrolling relative = view_get_relative_scrolling(view); + view_measure_wraps(system, &view->models->mem.general, view); + view_cursor_move(view, view->cursor.pos); + view->preferred_x = view_get_cursor_x(view); + view_set_relative_scrolling(view, relative); + } +} + +#if 0 +internal void +undo_shit(System_Functions *system, View *view, UI_State *state, UI_Layout *layout, + i32 total_count, i32 undo_count, i32 scrub_max){ + + Editing_File *file = view->file; + + if (view->widget.timeline.undo_line){ + if (do_button(1, state, layout, "- Undo", 1)){ + view->widget.timeline.undo_line = 0; + } + + if (view->widget.timeline.undo_line){ + Widget_ID wid = make_id(state, 2); + i32 new_count; + if (do_undo_slider(wid, state, layout, total_count, undo_count, 0, &new_count)){ + for (i32 i = 0; i < scrub_max && new_count < undo_count; ++i){ + view_undo(system, view->models, view); + --undo_count; + } + for (i32 i = 0; i < scrub_max && new_count > undo_count; ++i){ + view_redo(system, view->models, view); + ++undo_count; + } + } + } + } + else{ + if (do_button(1, state, layout, "+ Undo", 1)){ + view->widget.timeline.undo_line = 1; + } + } + + if (view->widget.timeline.history_line){ + if (do_button(3, state, layout, "- History", 1)){ + view->widget.timeline.history_line = 0; + } + + Widget_ID wid = make_id(state, 4); + if (view->widget.timeline.history_line){ + i32 new_count; + i32 mid = ((file->state.undo.history.edit_count + file->state.undo.edit_history_cursor) >> 1); + i32 count = file->state.undo.edit_history_cursor; + if (do_undo_slider(wid, state, layout, mid, count, &file->state.undo, &new_count)){ + for (i32 i = 0; i < scrub_max && new_count < count; ++i){ + view_history_step(system, view->models, view, hist_backward); + } + for (i32 i = 0; i < scrub_max && new_count > count; ++i){ + view_history_step(system, view->models, view, hist_forward); + } + } + } + } + else{ + if (do_button(3, state, layout, "+ History", 1)){ + view->widget.timeline.history_line = 1; + } + } +} + +internal void +draw_file_view_queries(View *view, UI_State *state, UI_Layout *layout){ + Widget_ID wid; + Query_Slot *slot; + Query_Bar *bar; + i32 i = 1; + + for (slot = view->query_set.used_slot; slot != 0; slot = slot->next){ + wid = make_id(state, i++); + bar = slot->query_bar; + do_text_field(wid, state, layout, bar->prompt, bar->string); + } +} + +inline void +view_show_menu(View *view, Command_Map *gui_map){ + view->ui_state = {}; + view->map_for_file = view->map; + view->map = gui_map; + view->showing_ui = VUI_Menu; +} + +inline void +view_show_config(View *view, Command_Map *gui_map){ + view->ui_state = {}; + view->map_for_file = view->map; + view->map = gui_map; + view->showing_ui = VUI_Config; +} + +inline void +view_show_interactive(System_Functions *system, View *view, Command_Map *gui_map, + Interactive_Action action, Interactive_Interaction interaction, String query){ + + Models *models = view->models; + + view->ui_state = {}; + view->map_for_file = view->map; + view->map = gui_map; + view->showing_ui = VUI_Interactive; + view->action = action; + view->interaction = interaction; + view->finished = 0; + + copy(&view->query, query); + view->dest.str[0] = 0; + view->dest.size = 0; + + hot_directory_clean_end(&models->hot_directory); + hot_directory_reload(system, &models->hot_directory, &models->working_set); +} + +inline void +view_show_theme(View *view, Command_Map *gui_map){ + view->ui_state = {}; + view->map_for_file = view->map; + view->map = gui_map; + view->showing_ui = VUI_Theme; + view->color_mode = CV_Mode_Library; + view->color = super_color_create(0xFF000000); +} +#endif + + +inline void +view_show_menu(View *view, Command_Map *gui_map){} + +inline void +view_show_config(View *view, Command_Map *gui_map){} + +inline void +view_show_interactive(System_Functions *system, View *view, + Command_Map *gui_map, Interactive_Action action, + Interactive_Interaction interaction, String query){} + +inline void +view_show_theme(View *view, Command_Map *gui_map){} + + +inline void +view_show_file(View *view, Command_Map *file_map){ + if (file_map){ + view->map = file_map; + } + else{ + view->map = view->map_for_file; + } + view->showing_ui = VUI_None; +} + +#if 0 +internal void +interactive_view_complete(View *view){ + Models *models = view->models; + Panel *panel = view->panel; + Editing_File *old_file = view->file; + + switch (view->action){ + case IAct_Open: + delayed_open(&models->delay1, models->hot_directory.string, panel); + delayed_touch_file(&models->delay1, old_file); + break; + + case IAct_Save_As: + delayed_save_as(&models->delay1, models->hot_directory.string, panel); + break; + + case IAct_New: + if (models->hot_directory.string.size > 0 && + !char_is_slash(models->hot_directory.string.str[models->hot_directory.string.size-1])){ + delayed_new(&models->delay1, models->hot_directory.string, panel); + } + break; + + case IAct_Switch: + delayed_switch(&models->delay1, view->dest, panel); + delayed_touch_file(&models->delay1, old_file); + break; + + case IAct_Kill: + delayed_try_kill(&models->delay1, view->dest); + break; + + case IAct_Sure_To_Close: + switch (view->user_action){ + case 0: + delayed_close(&models->delay1); + break; + + case 1: + break; + + case 2: + // TODO(allen): Save all. + break; + } + break; + + case IAct_Sure_To_Kill: + switch (view->user_action){ + case 0: + delayed_kill(&models->delay1, view->dest); + break; + + case 1: + break; + + case 2: + // TODO(allen): This is fishy! What if the save doesn't happen this time around? + // We need to ensure delayed acts happen in order I think. + delayed_save(&models->delay1, view->dest); + delayed_kill(&models->delay1, view->dest); + break; + } + break; + } + view_show_file(view, 0); + + // TODO(allen): This is here to prevent the key press from being passed to the + // underlying file which is a giant pain. + view->file = 0; +} +#endif + +#if 0 +internal void +update_highlighting(View *view){ + View *file_view = view->hot_file_view; + if (!file_view){ + view->highlight = {}; + return; + } + + Editing_File *file = file_view->file; + if (!file || !file_is_ready(file)){ + view->highlight = {}; + return; + } + + Models *models = view->models; + + Style *style = &models->style; + i32 pos = view_get_cursor_pos(file_view); + char c = buffer_get_char(&file->state.buffer, pos); + + if (c == '\r'){ + view->highlight.ids[0] = + raw_ptr_dif(&style->main.special_character_color, style); + } + + else if (file->state.tokens_complete){ + Cpp_Token_Stack *tokens = &file->state.token_stack; + Cpp_Get_Token_Result result = cpp_get_token(tokens, pos); + Cpp_Token token = tokens->tokens[result.token_index]; + if (!result.in_whitespace){ + u32 *color = style_get_color(style, token); + view->highlight.ids[0] = raw_ptr_dif(color, style); + if (token.type == CPP_TOKEN_JUNK){ + view->highlight.ids[1] = + raw_ptr_dif(&style->main.highlight_junk_color, style); + } + else if (char_is_whitespace(c)){ + view->highlight.ids[1] = + raw_ptr_dif(&style->main.highlight_white_color, style); + } + else{ + view->highlight.ids[1] = 0; + } + } + else{ + view->highlight.ids[0] = 0; + view->highlight.ids[1] = + raw_ptr_dif(&style->main.highlight_white_color, style); + } + } + + else{ + if (char_is_whitespace(c)){ + view->highlight.ids[0] = 0; + view->highlight.ids[1] = + raw_ptr_dif(&style->main.highlight_white_color, style); + } + else{ + view->highlight.ids[0] = + raw_ptr_dif(&style->main.default_color, style); + view->highlight.ids[1] = 0; + } + } + + if (file_view->show_temp_highlight){ + view->highlight.ids[2] = + raw_ptr_dif(&style->main.highlight_color, style); + view->highlight.ids[3] = + raw_ptr_dif(&style->main.at_highlight_color, style); + } + else if (file->state.paste_effect.tick_down > 0){ + view->highlight.ids[2] = + raw_ptr_dif(&style->main.paste_color, style); + view->highlight.ids[3] = 0; + } + else{ + view->highlight.ids[2] = 0; + view->highlight.ids[3] = 0; + } +} + +internal b32 +theme_library_shit(System_Functions *system, Exchange *exchange, + View *view, UI_State *state, UI_Layout *layout){ + + Models *models = view->models; + Mem_Options *mem = &models->mem; + + i32 result = 0; + + Library_UI ui; + ui.state = state; + ui.layout = layout; + + ui.fonts = models->font_set; + ui.hot_directory = &models->hot_directory; + ui.styles = &models->styles; + + Color_View_Mode mode = view->color_mode; + + i32_Rect bar_rect = ui.layout->rect; + bar_rect.x0 = bar_rect.x1 - 20; + do_scroll_bar(ui.state, bar_rect); + + ui.layout->y -= FLOOR32(view->ui_state.view_y); + ui.layout->rect.x1 -= 20; + + b32 case_sensitive = 0; + + switch (mode){ + case CV_Mode_Library: + { + do_label(ui.state, ui.layout, literal("Current Theme - Click to Edit")); + if (do_style_preview(&ui, &models->style)){ + view->color_mode = CV_Mode_Adjusting; + view->ui_state.selected = {}; + ui.state->view_y = 0; + result = 1; + } + + begin_row(ui.layout, 3); + if (ui.state->style->name.size >= 1){ + if (do_button(-2, ui.state, ui.layout, "Save", 2)){ + //style_library_add(ui.styles, ui.state->style); + } + } + else{ + do_button(-2, ui.state, ui.layout, "~Need's Name~", 2); + } + if (do_button(-3, ui.state, ui.layout, "Import", 2)){ + view->color_mode = CV_Mode_Import_File; + hot_directory_clean_end(&models->hot_directory); + hot_directory_reload(system, &models->hot_directory, &models->working_set); + } + if (do_button(-4, ui.state, ui.layout, "Export", 2)){ + view->color_mode = CV_Mode_Export; + hot_directory_clean_end(&models->hot_directory); + hot_directory_reload(system, &models->hot_directory, &models->working_set); + memset(view->import_export_check, 0, sizeof(view->import_export_check)); + } + + do_label(ui.state, ui.layout, literal("Theme Library - Click to Select")); + + i32 style_count = models->styles.count; + Style *style = models->styles.styles; + for (i32 i = 0; i < style_count; ++i, ++style){ + if (do_style_preview(&ui, style)){ + style_copy(&models->style, style); + result = 1; + } + } + }break; + + case CV_Mode_Import_File: + { + do_label(ui.state, ui.layout, literal("Current Theme")); + do_style_preview(&ui, &models->style); + + b32 file_selected = 0; + + do_label(ui.state, ui.layout, literal("Import Which File?")); + begin_row(ui.layout, 2); + if (do_button(-2, ui.state, ui.layout, "*.p4c only", 2, 1, view->p4c_only)){ + view->p4c_only = !view->p4c_only; + } + if (do_button(-3, ui.state, ui.layout, "Cancel", 2)){ + view->color_mode = CV_Mode_Library; + } + + b32 new_dir = 0; + if (do_file_list_box(system, ui.state, ui.layout, + ui.hot_directory, view->p4c_only, 1, case_sensitive, + &new_dir, &file_selected, 0)){ + result = 1; + } + + if (new_dir){ + hot_directory_reload(system, ui.hot_directory, ui.state->working_set); + } + if (file_selected){ + memset(&view->inspecting_styles, 0, sizeof(Style_Library)); + memset(view->import_export_check, 1, + sizeof(view->import_export_check)); + + view->import_file_id = exchange_request_file(exchange, + models->hot_directory.string.str, + models->hot_directory.string.size); + view->color_mode = CV_Mode_Import_Wait; + + } + }break; + + case CV_Mode_Import_Wait: + { + Style *styles = view->inspecting_styles.styles; + Data file = {}; + i32 file_max = 0; + + i32 count = 0; + i32 max = ArrayCount(view->inspecting_styles.styles); + + AllowLocal(styles); + AllowLocal(max); + + if (exchange_file_ready(exchange, view->import_file_id, + &file.data, &file.size, &file_max)){ + if (file.data){ + if (0 /* && style_library_import(file, ui.fonts, styles, max, &count) */){ + view->color_mode = CV_Mode_Import; + } + else{ + view->color_mode = CV_Mode_Library; + } + view->inspecting_styles.count = count; + } + else{ + Assert(!"this shouldn't happen!"); + } + + exchange_free_file(exchange, view->import_file_id); + } + }break; + + case CV_Mode_Export_File: + { + do_label(ui.state, ui.layout, literal("Current Theme")); + do_style_preview(&ui, &models->style); + + b32 file_selected = 0; + + do_label(ui.state, ui.layout, literal("Export File Name?")); + begin_row(ui.layout, 2); + if (do_button(-2, ui.state, ui.layout, "Finish Export", 2)){ + file_selected = 1; + } + if (do_button(-3, ui.state, ui.layout, "Cancel", 2)){ + view->color_mode = CV_Mode_Library; + } + + b32 new_dir = 0; + if (do_file_list_box(system, ui.state, ui.layout, + ui.hot_directory, 1, 1, case_sensitive, + &new_dir, &file_selected, ".p4c")){ + result = 1; + } + + if (new_dir){ + hot_directory_reload(system, + ui.hot_directory, ui.state->working_set); + } + if (file_selected){ + i32 count = ui.styles->count; + Temp_Memory temp = begin_temp_memory(&mem->part); + Style **styles = push_array(&mem->part, Style*, sizeof(Style*)*count); + + Style *style = ui.styles->styles; + b8 *export_check = view->import_export_check; + i32 export_count = 0; + for (i32 i = 0; i < count; ++i, ++style){ + if (export_check[i]){ + styles[export_count++] = style; + } + } + char *data = push_array(&mem->part, char, ui.hot_directory->string.size + 5); + String str = make_string(data, 0, ui.hot_directory->string.size + 5); + copy(&str, ui.hot_directory->string); + append(&str, make_lit_string(".p4c")); + /*style_library_export(system, exchange, mem, &target->font_set, str.str, styles, export_count);*/ + + end_temp_memory(temp); + view->color_mode = CV_Mode_Library; + } + }break; + + case CV_Mode_Import: + { + do_label(ui.state, ui.layout, literal("Current Theme")); + do_style_preview(&ui, &models->style); + + i32 style_count = view->inspecting_styles.count; + Style *styles = view->inspecting_styles.styles; + b8 *import_check = view->import_export_check; + + do_label(ui.state, ui.layout, literal("Pack")); + begin_row(ui.layout, 2); + if (do_button(-2, ui.state, ui.layout, "Finish Import", 2)){ + Style *style = styles; + for (i32 i = 0; i < style_count; ++i, ++style){ + //if (import_check[i]) style_library_add(ui.styles, style); + } + view->color_mode = CV_Mode_Library; + } + if (do_button(-3, ui.state, ui.layout, "Cancel", 2)){ + view->color_mode = CV_Mode_Library; + } + + Style *style = styles; + for (i32 i = 0; i < style_count; ++i, ++style){ + if (do_style_preview(&ui, style, import_check[i])){ + import_check[i] = !import_check[i]; + result = 1; + } + } + }break; + + case CV_Mode_Export: + { + do_label(ui.state, ui.layout, literal("Current Theme")); + do_style_preview(&ui, &models->style); + + do_label(ui.state, ui.layout, literal("Export Which Themes?")); + begin_row(ui.layout, 2); + if (do_button(-2, ui.state, ui.layout, "Export", 2)){ + view->color_mode = CV_Mode_Export_File; + } + if (do_button(-3, ui.state, ui.layout, "Cancel", 2)){ + view->color_mode = CV_Mode_Library; + } + + i32 style_count = models->styles.count; + Style *style = models->styles.styles; + b8 *export_check = view->import_export_check; + for (i32 i = 0; i < style_count; ++i, ++style){ + if (do_style_preview(&ui, style, export_check[i])){ + export_check[i] = !export_check[i]; + result = 1; + } + } + }break; + } + + return (result); +} + +internal b32 +theme_adjusting_shit(View *view, UI_State *state, UI_Layout *layout, Super_Color *color){ + update_highlighting(view); + + Models *models = view->models; + + Style *style = &models->style; + i32 result = 0; + + Color_UI ui; + ui.state = state; + ui.layout = layout; + + ui.fonts = models->font_set; + ui.global_font = &models->global_font; + ui.highlight = view->highlight; + ui.color = view->color; + ui.has_hover_color = 0; + ui.state->sub_id1_change = 0; + ui.hex_advance = font_get_max_width(ui.fonts, ui.state->font_id, "0123456789abcdefx"); + ui.palette = models->palette; + ui.palette_size = models->palette_size; + + i32_Rect bar_rect = ui.layout->rect; + bar_rect.x0 = bar_rect.x1 - 20; + do_scroll_bar(ui.state, bar_rect); + + ui.layout->y -= FLOOR32(view->ui_state.view_y); + ui.layout->rect.x1 -= 20; + + if (do_button(-1, ui.state, ui.layout, "Back to Library", 2)){ + view->color_mode = CV_Mode_Library; + ui.state->view_y = 0; + } + + do_style_name(&ui); + do_font_switch(&ui); + + do_color_adjuster(&ui, &style->main.back_color, + style->main.default_color, style->main.back_color, + "Background"); + do_color_adjuster(&ui, &style->main.margin_color, + style->main.default_color, style->main.margin_color, + "Margin"); + do_color_adjuster(&ui, &style->main.margin_hover_color, + style->main.default_color, style->main.margin_hover_color, + "Margin Hover"); + do_color_adjuster(&ui, &style->main.margin_active_color, + style->main.default_color, style->main.margin_active_color, + "Margin Active"); + + do_color_adjuster(&ui, &style->main.cursor_color, + style->main.at_cursor_color, style->main.cursor_color, + "Cursor"); + do_color_adjuster(&ui, &style->main.at_cursor_color, + style->main.at_cursor_color, style->main.cursor_color, + "Text At Cursor"); + do_color_adjuster(&ui, &style->main.mark_color, + style->main.mark_color, style->main.back_color, + "Mark"); + + do_color_adjuster(&ui, &style->main.highlight_color, + style->main.at_highlight_color, style->main.highlight_color, + "Highlight"); + do_color_adjuster(&ui, &style->main.at_highlight_color, + style->main.at_highlight_color, style->main.highlight_color, + "Text At Highlight"); + + do_color_adjuster(&ui, &style->main.default_color, + style->main.default_color, style->main.back_color, + "Text Default"); + do_color_adjuster(&ui, &style->main.comment_color, + style->main.comment_color, style->main.back_color, + "Comment"); + do_color_adjuster(&ui, &style->main.keyword_color, + style->main.keyword_color, style->main.back_color, + "Keyword"); + do_color_adjuster(&ui, &style->main.str_constant_color, + style->main.str_constant_color, style->main.back_color, + "String Constant"); + do_color_adjuster(&ui, &style->main.char_constant_color, + style->main.char_constant_color, style->main.back_color, + "Character Constant"); + do_color_adjuster(&ui, &style->main.int_constant_color, + style->main.int_constant_color, style->main.back_color, + "Integer Constant"); + do_color_adjuster(&ui, &style->main.float_constant_color, + style->main.float_constant_color, style->main.back_color, + "Float Constant"); + do_color_adjuster(&ui, &style->main.bool_constant_color, + style->main.bool_constant_color, style->main.back_color, + "Boolean Constant"); + do_color_adjuster(&ui, &style->main.preproc_color, + style->main.preproc_color, style->main.back_color, + "Preprocessor"); + do_color_adjuster(&ui, &style->main.include_color, + style->main.include_color, style->main.back_color, + "Include Constant"); + do_color_adjuster(&ui, &style->main.special_character_color, + style->main.special_character_color, style->main.back_color, + "Special Character"); + + do_color_adjuster(&ui, &style->main.highlight_junk_color, + style->main.default_color, style->main.highlight_junk_color, + "Junk Highlight"); + do_color_adjuster(&ui, &style->main.highlight_white_color, + style->main.default_color, style->main.highlight_white_color, + "Whitespace Highlight"); + + do_color_adjuster(&ui, &style->main.paste_color, + style->main.paste_color, style->main.back_color, + "Paste Color"); + + Interactive_Style *bar_style = &style->main.file_info_style; + do_color_adjuster(&ui, &bar_style->bar_color, + bar_style->base_color, bar_style->bar_color, + "Bar"); + do_color_adjuster(&ui, &bar_style->base_color, + bar_style->base_color, bar_style->bar_color, + "Bar Text"); + do_color_adjuster(&ui, &bar_style->pop1_color, + bar_style->pop1_color, bar_style->bar_color, + "Bar Pop 1"); + do_color_adjuster(&ui, &bar_style->pop2_color, + bar_style->pop2_color, bar_style->bar_color, + "Bar Pop 2"); + + *color = ui.hover_color; + + return (result); +} + +internal b32 +theme_shit(System_Functions *system, Exchange *exchange, + View *view, View *active, UI_State *state, UI_Layout *layout, Super_Color *color){ + b32 result = 0; + + if (view != active){ + view->hot_file_view = active; + } + + switch (view->color_mode){ + case CV_Mode_Library: + case CV_Mode_Import_File: + case CV_Mode_Export_File: + case CV_Mode_Import: + case CV_Mode_Export: + case CV_Mode_Import_Wait: + if (theme_library_shit(system, exchange, view, state, layout)){ + result = 1; + } + break; + + case CV_Mode_Adjusting: + if (theme_adjusting_shit(view, state, layout, color)){ + result = 1; + } + break; + } + + return(result); +} + +internal b32 +interactive_shit(System_Functions *system, View *view, UI_State *state, UI_Layout *layout){ + b32 result = 0; + b32 new_dir = 0; + b32 complete = 0; + + Models *models = view->models; + + do_label(state, layout, view->query, 1.f); + + b32 case_sensitive = 0; + + b32 input_stage = state->input_stage; + Key_Summary *keys = state->keys; + + switch (view->interaction){ + case IInt_Sys_File_List: + { + b32 is_new = (view->action == IAct_New); + + if (do_file_list_box(system, state, layout, + &models->hot_directory, 0, !is_new, case_sensitive, + &new_dir, &complete, 0)){ + result = 1; + } + if (new_dir){ + hot_directory_reload(system, &models->hot_directory, &models->working_set); + } + }break; + + case IInt_Live_File_List: + { + if (do_live_file_list_box(system, state, layout, &models->working_set, &view->dest, &complete)){ + result = 1; + } + }break; + + case IInt_Sure_To_Close: + { + i32 action = -1; + char s_[256]; + String s; + s = make_fixed_width_string(s_); + append(&s, "There are unsaved changes in, close anyway?"); + do_label(state, layout, s, 1.f); + + i32 id = 0; + if (do_list_option(++id, state, layout, make_lit_string("(Y)es"))){ + action = 0; + } + + if (do_list_option(++id, state, layout, make_lit_string("(N)o"))){ + action = 1; + } + + if (action == -1 && input_stage){ + i32 key_count = keys->count; + for (i32 i = 0; i < key_count; ++i){ + Key_Event_Data key = keys->keys[i]; + switch (key.character){ + case 'y': case 'Y': action = 0; break; + case 'n': case 'N': action = 1; break; + } + if (action == -1 && key.keycode == key_esc) action = 1; + if (action != -1) break; + } + } + + if (action != -1){ + complete = 1; + view->user_action = action; + } + }break; + + case IInt_Sure_To_Kill: + { + i32 action = -1; + char s_[256]; + String s; + s = make_fixed_width_string(s_); + append(&s, view->dest); + append(&s, " has unsaved changes, kill it?"); + do_label(state, layout, s, 1.f); + + i32 id = 0; + if (do_list_option(++id, state, layout, make_lit_string("(Y)es"))){ + action = 0; + } + + if (do_list_option(++id, state, layout, make_lit_string("(N)o"))){ + action = 1; + } + + if (do_list_option(++id, state, layout, make_lit_string("(S)ave and kill"))){ + action = 2; + } + + if (action == -1 && input_stage){ + i32 key_count = keys->count; + for (i32 i = 0; i < key_count; ++i){ + Key_Event_Data key = keys->keys[i]; + switch (key.character){ + case 'y': case 'Y': action = 0; break; + case 'n': case 'N': action = 1; break; + case 's': case 'S': action = 2; break; + } + if (action == -1 && key.keycode == key_esc) action = 1; + if (action != -1) break; + } + } + + if (action != -1){ + complete = 1; + view->user_action = action; + } + }break; + } + + if (complete){ + view->finished = 1; + interactive_view_complete(view); + result= 1; + } + + return(result); +} + +internal void +menu_shit(View *view, UI_State *state, UI_Layout *layout){ + i32 id = 0; + + do_label(state, layout, literal("Menu"), 2.f); + + if (do_list_option(++id, state, layout, make_lit_string("Theme Options"))){ + view_show_theme(view, view->map); + } + + if (do_list_option(++id, state, layout, make_lit_string("Keyboard Layout Options"))){ + view_show_config(view, view->map); + } +} + +internal void +config_shit(View *view, UI_State *state, UI_Layout *layout){ + i32 id = 0; + Models *models = view->models; + + do_label(state, layout, literal("Config"), 2.f); + + if (do_checkbox_list_option(++id, state, layout, make_lit_string("Left Ctrl + Left Alt = AltGr"), + models->settings.lctrl_lalt_is_altgr)){ + models->settings.lctrl_lalt_is_altgr = !models->settings.lctrl_lalt_is_altgr; + } +} +#endif + +struct File_Bar{ + f32 pos_x, pos_y; + f32 text_shift_x, text_shift_y; + i32_Rect rect; + i16 font_id; +}; + +internal void +intbar_draw_string(Render_Target *target, File_Bar *bar, String str, u32 char_color){ + i16 font_id = bar->font_id; + draw_string(target, font_id, str, + (i32)(bar->pos_x + bar->text_shift_x), + (i32)(bar->pos_y + bar->text_shift_y), + char_color); + bar->pos_x += font_string_width(target, font_id, str); +} + +internal void +view_reinit_scrolling(View *view){ + Editing_File *file = view->file; + f32 w, h; + f32 cursor_x, cursor_y; + f32 target_x, target_y; + + view->reinit_scrolling = 0; + + target_x = 0; + target_y = 0; + + if (file && file_is_ready(file)){ + cursor_x = view_get_cursor_x(view); + cursor_y = view_get_cursor_y(view); + + w = view_compute_width(view); + h = view_compute_height(view); + + if (cursor_x >= target_x + w){ + target_x = (f32)(cursor_x - w*.5f); + } + + target_y = (f32)FLOOR32(cursor_y - h*.5f); + if (target_y < view->scroll_min_limit) target_y = view->scroll_min_limit; + } + + view->target_x = target_x; + view->target_y = target_y; + view->scroll_x = target_x; + view->scroll_y = target_y; + view->prev_target_x = -1000.f; + view->prev_target_y = -1000.f; +} + +internal i32 +file_step(View *view, i32_Rect region, Input_Summary *user_input, b32 is_active){ + i32 result = 0; + Editing_File *file = view->file; + if (file && !file->state.is_loading){ + f32 line_height = (f32)view->font_height; + f32 cursor_y = view_get_cursor_y(view); + f32 target_y = view->target_y; + i32 lowest_line = view_compute_lowest_line(view); + + f32 delta_y = 3.f*line_height; + + f32 max_y = (f32)(region.y1 - region.y0); + f32 max_x = (f32)(region.x1 - region.x0); + f32 max_target_y = view_compute_max_target_y(lowest_line, (i32)line_height, max_y); + + if (user_input->mouse.wheel != 0){ + f32 wheel_multiplier = 3.f; + f32 delta_target_y = delta_y*user_input->mouse.wheel*wheel_multiplier; + target_y += delta_target_y; + + if (target_y < view->scroll_min_limit) target_y = view->scroll_min_limit; + if (target_y > max_target_y) target_y = max_target_y; + + f32 old_cursor_y = cursor_y; + if (cursor_y >= target_y + max_y) cursor_y = target_y + max_y; + if (cursor_y < target_y - view->scroll_min_limit) cursor_y = target_y - view->scroll_min_limit; + + if (cursor_y != old_cursor_y){ + view->cursor = + view_compute_cursor_from_xy(view, view->preferred_x, cursor_y); + } + + result = 1; + } + + if (cursor_y > target_y + max_y){ + target_y = cursor_y - max_y + delta_y; + } + if (cursor_y < target_y - view->scroll_min_limit){ + target_y = cursor_y - delta_y + view->scroll_min_limit; + } + + if (target_y > max_target_y) target_y = max_target_y; + if (target_y < view->scroll_min_limit) target_y = view->scroll_min_limit; + view->target_y = target_y; + + f32 cursor_x = view_get_cursor_x(view); + f32 target_x = view->target_x; + if (cursor_x < target_x){ + target_x = (f32)Max(0, cursor_x - max_x/2); + } + else if (cursor_x >= target_x + max_x){ + target_x = (f32)(cursor_x - max_x/2); + } + + view->target_x = target_x; + + b32 is_new_target = 0; + if (view->target_x != view->prev_target_x) is_new_target = 1; + if (view->target_y != view->prev_target_y) is_new_target = 1; + + if (view->models->scroll_rule( + view->target_x, view->target_y, + &view->scroll_x, &view->scroll_y, + (view->id) + 1, is_new_target)){ + result = 1; + } + + view->prev_target_x = view->target_x; + view->prev_target_y = view->target_y; + + if (file->state.paste_effect.tick_down > 0){ + --file->state.paste_effect.tick_down; + result = 1; + } + + if (user_input->mouse.press_l && is_active){ + f32 rx = (f32)(user_input->mouse.x - region.x0); + f32 ry = (f32)(user_input->mouse.y - region.y0); + + if (ry >= -view->scroll_min_limit){ + view_set_widget(view, FWIDG_NONE); + if (rx >= 0 && rx < max_x && ry >= 0 && ry < max_y){ + view_cursor_move(view, rx + view->scroll_x, ry + view->scroll_y, 1); + view->mode = {}; + } + } + result = 1; + } + if (!is_active) view_set_widget(view, FWIDG_NONE); + } + + return(result); +} + +internal i32 +step_file_view(View *view, b32 is_active){ + gui_begin_top_level(&view->gui_target); + { + gui_do_top_bar(&view->gui_target); + + gui_begin_overlap(&view->gui_target); + { + gui_begin_serial_section(&view->gui_target); + { + // do widget + } + gui_end_serial_section(&view->gui_target); + + gui_begin_serial_section(&view->gui_target); + { + switch (view->showing_ui){ + case VUI_None: + gui_do_file(&view->gui_target); + break; + } + } + gui_end_serial_section(&view->gui_target); + } + gui_end_overlap(&view->gui_target); + } + gui_end_top_level(&view->gui_target); + + return(1); +} + +internal i32 +do_input_file_view(System_Functions *system, Exchange *exchange, + View *view, i32_Rect rect, b32 is_active, Input_Summary *user_input){ + + i32 result = 0; + + GUI_Session gui_session; + GUI_Header *h; + + gui_session_init(&gui_session, rect, view->font_height); + + for (h = (GUI_Header*)view->gui_target.push.base; + h->type; + h = NextHeader(h)){ + if (gui_interpret(&gui_session, h)){ + switch (h->type){ + case guicom_top_bar: break; + + case guicom_file: + { + view->scroll_min_limit = -(f32)(gui_session.clip_rect.y0 - gui_session.rect.y0); + if (view->reinit_scrolling){ + view_reinit_scrolling(view); + } + if (file_step(view, gui_session.rect, user_input, is_active)){ + result = 1; + } + }break; + } + } + } + + return(result); + +#if 0 + Models *models = view->models; + i32 result = 0; + + i32 widget_height = 0; + AllowLocal(models); + +#if 0 + { + UI_State state = + ui_state_init(&view->widget.state, 0, user_input, + &models->style, models->global_font.font_id, models->font_set, 0, 1); + + UI_Layout layout; + begin_layout(&layout, rect); + + switch (view->widget.type){ + case FWIDG_NONE: + { + if (file && view->showing_ui == VUI_None){ + do_file_bar(view, file, &layout, 0); + } + draw_file_view_queries(view, &state, &layout); + }break; + + case FWIDG_TIMELINES: + { + i32 scrub_max = view->scrub_max; + i32 undo_count = file->state.undo.undo.edit_count; + i32 redo_count = file->state.undo.redo.edit_count; + i32 total_count = undo_count + redo_count; + undo_shit(system, view, &state, &layout, total_count, undo_count, scrub_max); + }break; + } + + widget_height = layout.y - rect.y0; + if (ui_finish_frame(&view->widget.state, &state, &layout, rect, 0, 0)){ + result = 1; + } + } +#endif + + + view->scroll_min_limit = (f32)-widget_height; + if (view->reinit_scrolling){ + view_reinit_scrolling(view); + } + + if (view->showing_ui == VUI_None){ + if (file_step(view, rect, user_input, is_active)){ + result = 1; + } + } + +#if 0 + { + UI_State state = + ui_state_init(&view->ui_state, 0, user_input, + &models->style, models->global_font.font_id, models->font_set, &models->working_set, 1); + + UI_Layout layout; + begin_layout(&layout, rect); + + Super_Color color = {}; + + switch (view->showing_ui){ + case VUI_None: break; + case VUI_Theme: + { + theme_shit(system, exchange, view, 0, &state, &layout, &color); + }break; + case VUI_Interactive: + { + if (interactive_shit(system, view, &state, &layout)){ + result = 1; + } + }break; + case VUI_Menu: + { + menu_shit(view, &state, &layout); + }break; + case VUI_Config: + { + config_shit(view, &state, &layout); + }break; + } + + i32 did_activation = 0; + if (ui_finish_frame(&view->ui_state, &state, &layout, rect, 0, &did_activation)){ + result = 1; + } + if (did_activation){ + if (view->showing_ui == VUI_Theme){ + view->color = color; + result = 1; + } + } + } +#endif + + return(result); +#endif +} + +internal i32 +draw_file_loaded(View *view, i32_Rect rect, b32 is_active, Render_Target *target){ + Models *models = view->models; + Editing_File *file = view->file; + Style *style = &models->style; + i32 line_height = view->font_height; + + i32 max_x = rect.x1 - rect.x0; + i32 max_y = rect.y1 - rect.y0 + line_height; + + Assert(file && !file->state.is_dummy && buffer_good(&file->state.buffer)); + + b32 tokens_use = 0; + Cpp_Token_Stack token_stack = {}; + if (file){ + tokens_use = file->state.tokens_complete && (file->state.token_stack.count > 0); + token_stack = file->state.token_stack; + } + + Partition *part = &models->mem.part; + + Temp_Memory temp = begin_temp_memory(part); + + partition_align(part, 4); + i32 max = partition_remaining(part) / sizeof(Buffer_Render_Item); + Buffer_Render_Item *items = push_array(part, Buffer_Render_Item, max); + + i16 font_id = models->global_font.font_id; + Render_Font *font = get_font_info(models->font_set, font_id)->font; + float *advance_data = 0; + if (font) advance_data = font->advance_data; + + i32 count; + Full_Cursor render_cursor; + Buffer_Render_Options opts = {}; + + f32 *wraps = view->line_wrap_y; + f32 scroll_x = view->scroll_x; + f32 scroll_y = view->scroll_y; + + { + render_cursor = buffer_get_start_cursor(&file->state.buffer, wraps, scroll_y, + !view->unwrapped_lines, (f32)max_x, advance_data, (f32)line_height); + + view->scroll_i = render_cursor.pos; + + buffer_get_render_data(&file->state.buffer, items, max, &count, + (f32)rect.x0, (f32)rect.y0, + scroll_x, scroll_y, render_cursor, + !view->unwrapped_lines, + (f32)max_x, (f32)max_y, + advance_data, (f32)line_height, + opts); + } + + Assert(count > 0); + + i32 cursor_begin, cursor_end; + u32 cursor_color, at_cursor_color; + if (view->show_temp_highlight){ + cursor_begin = view->temp_highlight.pos; + cursor_end = view->temp_highlight_end_pos; + cursor_color = style->main.highlight_color; + at_cursor_color = style->main.at_highlight_color; + } + else{ + cursor_begin = view->cursor.pos; + cursor_end = cursor_begin + 1; + cursor_color = style->main.cursor_color; + at_cursor_color = style->main.at_cursor_color; + } + + i32 token_i = 0; + u32 main_color = style->main.default_color; + u32 special_color = style->main.special_character_color; + if (tokens_use){ + Cpp_Get_Token_Result result = cpp_get_token(&token_stack, items->index); + main_color = *style_get_color(style, token_stack.tokens[result.token_index]); + token_i = result.token_index + 1; + } + + u32 mark_color = style->main.mark_color; + Buffer_Render_Item *item = items; + i32 prev_ind = -1; + u32 highlight_color = 0; + u32 highlight_this_color = 0; + + for (i32 i = 0; i < count; ++i, ++item){ + i32 ind = item->index; + highlight_this_color = 0; + if (tokens_use && ind != prev_ind){ + Cpp_Token current_token = token_stack.tokens[token_i-1]; + + if (token_i < token_stack.count){ + if (ind >= token_stack.tokens[token_i].start){ + main_color = + *style_get_color(style, token_stack.tokens[token_i]); + current_token = token_stack.tokens[token_i]; + ++token_i; + } + else if (ind >= current_token.start + current_token.size){ + main_color = 0xFFFFFFFF; + } + } + + if (current_token.type == CPP_TOKEN_JUNK && + i >= current_token.start && i < current_token.start + current_token.size){ + highlight_color = style->main.highlight_junk_color; + } + else{ + highlight_color = 0; + } + } + + u32 char_color = main_color; + if (item->flags & BRFlag_Special_Character) char_color = special_color; + + f32_Rect char_rect = f32R(item->x0, item->y0, item->x1, item->y1); + if (view->show_whitespace && highlight_color == 0 && + char_is_whitespace((char)item->glyphid)){ + highlight_this_color = style->main.highlight_white_color; + } + else{ + highlight_this_color = highlight_color; + } + + if (cursor_begin <= ind && ind < cursor_end && (ind != prev_ind || cursor_begin < ind)){ + if (is_active){ + draw_rectangle(target, char_rect, cursor_color); + char_color = at_cursor_color; + } + else{ + if (!view->show_temp_highlight){ + draw_rectangle_outline(target, char_rect, cursor_color); + } + } + } + else if (highlight_this_color){ + draw_rectangle(target, char_rect, highlight_this_color); + } + + u32 fade_color = 0xFFFF00FF; + f32 fade_amount = 0.f; + + if (file->state.paste_effect.tick_down > 0 && + file->state.paste_effect.start <= ind && + ind < file->state.paste_effect.end){ + fade_color = file->state.paste_effect.color; + fade_amount = (f32)(file->state.paste_effect.tick_down) / file->state.paste_effect.tick_max; + } + + char_color = color_blend(char_color, fade_amount, fade_color); + + if (ind == view->mark && prev_ind != ind){ + draw_rectangle_outline(target, char_rect, mark_color); + } + if (item->glyphid != 0){ + font_draw_glyph(target, font_id, (u8)item->glyphid, + item->x0, item->y0, char_color); + } + prev_ind = ind; + } + + end_temp_memory(temp); + + return(0); +} + +internal void +do_render_file_bar(Render_Target *target, View *view, Editing_File *file, i32_Rect rect){ + File_Bar bar; + Models *models = view->models; + Style_Font *font = &models->global_font; + Interactive_Style bar_style = models->style.main.file_info_style; + + u32 back_color = bar_style.bar_color; + u32 base_color = bar_style.base_color; + u32 pop1_color = bar_style.pop1_color; + u32 pop2_color = bar_style.pop2_color; + + bar.rect = rect; + + if (target){ + bar.font_id = font->font_id; + bar.pos_x = (f32)bar.rect.x0; + bar.pos_y = (f32)bar.rect.y0; + bar.text_shift_y = 2; + bar.text_shift_x = 0; + + draw_rectangle(target, bar.rect, back_color); + if (file){ + intbar_draw_string(target, &bar, file->name.live_name, base_color); + } + else{ + intbar_draw_string(target, &bar, make_lit_string("*NULL*"), base_color); + } + intbar_draw_string(target, &bar, make_lit_string(" -"), base_color); + + if (file){ + if (file->state.is_loading){ + intbar_draw_string(target, &bar, make_lit_string(" loading"), base_color); + } + else{ + char line_number_space[30]; + String line_number = make_string(line_number_space, 0, 30); + append(&line_number, " L#"); + append_int_to_str(view->cursor.line, &line_number); + + intbar_draw_string(target, &bar, line_number, base_color); + + intbar_draw_string(target, &bar, make_lit_string(" -"), base_color); + + if (file->settings.dos_write_mode){ + intbar_draw_string(target, &bar, make_lit_string(" dos"), base_color); + } + else{ + intbar_draw_string(target, &bar, make_lit_string(" nix"), base_color); + } + + if (file->state.still_lexing){ + intbar_draw_string(target, &bar, make_lit_string(" parsing"), pop1_color); + } + + if (!file->settings.unimportant){ + switch (buffer_get_sync(file)){ + case SYNC_BEHIND_OS: + { + persist String out_of_sync = make_lit_string(" !"); + intbar_draw_string(target, &bar, out_of_sync, pop2_color); + }break; + + case SYNC_UNSAVED: + { + persist String out_of_sync = make_lit_string(" *"); + intbar_draw_string(target, &bar, out_of_sync, pop2_color); + }break; + } + } + } + } + } +} + +internal i32 +do_render_file_view(System_Functions *system, Exchange *exchange, + View *view, View *active, i32_Rect rect, b32 is_active, + Render_Target *target, Input_Summary *user_input){ + + Editing_File *file = view->file; + i32 result = 0; + + GUI_Session gui_session = {0}; + GUI_Header *h; + + gui_session_init(&gui_session, rect, view->font_height); + + for (h = (GUI_Header*)view->gui_target.push.base; + h->type; + h = NextHeader(h)){ + if (gui_interpret(&gui_session, h)){ + switch (h->type){ + case guicom_top_bar: + { + do_render_file_bar(target, view, file, gui_session.rect); + } + break; + + case guicom_file: + { + target->push_clip(target, gui_session.clip_rect); + view->scroll_min_limit = -(f32)(gui_session.clip_rect.y0 - gui_session.rect.y0); + if (view->reinit_scrolling){ + view_reinit_scrolling(view); + } + if (file && file_is_ready(file)){ + result = draw_file_loaded(view, gui_session.rect, is_active, target); + } + target->pop_clip(target); + }break; + } + } + } + + return(result); + +#if 0 + + Editing_File *file = view->file; + i32 result = 0; + + { + i32 line_height = view->font_height; + i32_Rect bar_rect = rect; + bar_rect.y1 = bar_rect.y0 + line_height + 2; + do_render_file_bar(target, view, file, bar_rect); + + rect.y0 = bar_rect.y1; + } + + target->push_clip(target, rect); + if (view->gui_target.show_file){ + view->scroll_min_limit = 0; + if (view->reinit_scrolling){ + view_reinit_scrolling(view); + } + + switch (view->showing_ui){ + case VUI_None: + { + if (file && file_is_ready(file)){ + result = draw_file_loaded(view, rect, is_active, target); + } + }break; + } + } + target->pop_clip(target); + + return(result); +#endif + +#if 0 + Models *models = view->models; + Editing_File *file = view->file; + i32 result = 0; + + i32 widget_height = 0; + AllowLocal(models); + +#if 0 + { + UI_State state = + ui_state_init(&view->widget.state, target, 0, + &models->style, models->global_font.font_id, models->font_set, 0, 0); + + UI_Layout layout; + begin_layout(&layout, rect); + + switch (view->widget.type){ + case FWIDG_NONE: + { + if (file && view->showing_ui == VUI_None){ + do_file_bar(view, file, &layout, target); + } + + draw_file_view_queries(view, &state, &layout); + }break; + + case FWIDG_TIMELINES: + { + if (file){ + i32 undo_count = file->state.undo.undo.edit_count; + i32 redo_count = file->state.undo.redo.edit_count; + i32 total_count = undo_count + redo_count; + undo_shit(0, view, &state, &layout, total_count, undo_count, 0); + } + else{ + view->widget.type = FWIDG_NONE; + } + }break; + } + + widget_height = layout.y - rect.y0; + ui_finish_frame(&view->widget.state, &state, &layout, rect, 0, 0); + } +#endif + + view->scroll_min_limit = (f32)-widget_height; + + { + rect.y0 += widget_height; + target->push_clip(target, rect); + + rect.y0 -= widget_height; + + switch (view->showing_ui){ + case VUI_None: + { + if (file && file_is_ready(file)){ + result = draw_file_loaded(view, rect, is_active, target); + } + }break; + } + + target->pop_clip(target); + } + + return (result); +#endif +} + +// TODO(allen): Passing this hook and app pointer is a hack. It can go as soon as we start +// initializing files independently of setting them to views. +internal void +kill_file(System_Functions *system, Exchange *exchange, Models *models, Editing_File *file, + Hook_Function *open_hook, Application_Links *app){ + File_Node *node, *used; + + file_close(system, &models->mem.general, file); + working_set_free_file(&models->working_set, file); + + used = &models->working_set.used_sentinel; + node = used->next; + + for (View_Iter iter = file_view_iter_init(&models->layout, file, 0); + file_view_iter_good(iter); + iter = file_view_iter_next(iter)){ + if (node != used){ + iter.view->file = 0; + view_set_file(iter.view, (Editing_File*)node, models, system, open_hook, app, 0); + node = node->next; + } + else{ + iter.view->file = 0; + view_set_file(iter.view, 0, models, system, open_hook, app, 0); + } + } +} + +inline void +free_file_view(View *view){ + if (view->line_wrap_y) + general_memory_free(&view->models->mem.general, view->line_wrap_y); +} + +struct Search_Range{ + Buffer_Type *buffer; + i32 start, size; +}; + +struct Search_Set{ + Search_Range *ranges; + i32 count, max; +}; + +struct Search_Iter{ + String word; + i32 pos; + i32 i; +}; + +struct Search_Match{ + Buffer_Type *buffer; + i32 start, end; + b32 found_match; +}; + +internal void +search_iter_init(General_Memory *general, Search_Iter *iter, i32 size){ + i32 str_max; + + if (iter->word.str == 0){ + str_max = size*2; + iter->word.str = (char*)general_memory_allocate(general, str_max, 0); + iter->word.memory_size = str_max; + } + else if (iter->word.memory_size < size){ + str_max = size*2; + iter->word.str = (char*)general_memory_reallocate_nocopy(general, iter->word.str, str_max, 0); + iter->word.memory_size = str_max; + } + + iter->i = 0; + iter->pos = 0; +} + +internal void +search_set_init(General_Memory *general, Search_Set *set, i32 set_count){ + i32 max; + + if (set->ranges == 0){ + max = set_count*2; + set->ranges = (Search_Range*)general_memory_allocate(general, sizeof(Search_Range)*max, 0); + set->max = max; + } + else if (set->max < set_count){ + max = set_count*2; + set->ranges = (Search_Range*)general_memory_reallocate_nocopy( + general, set->ranges, sizeof(Search_Range)*max, 0); + set->max = max; + } + + set->count = set_count; +} + +internal void +search_hits_table_alloc(General_Memory *general, Table *hits, i32 table_size){ + void *mem; + i32 mem_size; + + mem_size = table_required_mem_size(table_size, sizeof(Offset_String)); + if (hits->hash_array == 0){ + mem = general_memory_allocate(general, mem_size, 0); + } + else{ + mem = general_memory_reallocate_nocopy(general, hits->hash_array, mem_size, 0); + } + table_init_memory(hits, mem, table_size, sizeof(Offset_String)); +} + +internal void +search_hits_init(General_Memory *general, Table *hits, String_Space *str, i32 table_size, i32 str_size){ + void *mem; + i32 mem_size; + + if (hits->hash_array == 0){ + search_hits_table_alloc(general, hits, table_size); + } + else if (hits->max < table_size){ + mem_size = table_required_mem_size(table_size, sizeof(Offset_String)); + mem = general_memory_reallocate_nocopy(general, hits->hash_array, mem_size, 0); + table_init_memory(hits, mem, table_size, sizeof(Offset_String)); + } + + if (str->space == 0){ + str->space = (char*)general_memory_allocate(general, str_size, 0); + str->max = str_size; + } + else if (str->max < str_size){ + str->space = (char*)general_memory_reallocate_nocopy(general, str->space, str_size, 0); + str->max = str_size; + } + + str->pos = str->new_pos = 0; + table_clear(hits); +} + +internal b32 +search_hit_add(General_Memory *general, Table *hits, String_Space *space, char *str, i32 len){ + b32 result; + i32 new_size; + Offset_String ostring; + Table new_hits; + + Assert(len != 0); + + ostring = strspace_append(space, str, len); + if (ostring.size == 0){ + new_size = Max(space->max*2, space->max + len); + space->space = (char*)general_memory_reallocate(general, space->space, space->new_pos, new_size, 0); + ostring = strspace_append(space, str, len); + } + + Assert(ostring.size != 0); + + if (table_at_capacity(hits)){ + search_hits_table_alloc(general, &new_hits, hits->max*2); + table_clear(&new_hits); + table_rehash(hits, &new_hits, space->space, tbl_offset_string_hash, tbl_offset_string_compare); + general_memory_free(general, hits->hash_array); + *hits = new_hits; + } + + if (!table_add(hits, &ostring, space->space, tbl_offset_string_hash, tbl_offset_string_compare)){ + result = 1; + strspace_keep_prev(space); + } + else{ + result = 0; + strspace_discard_prev(space); + } + + return(result); +} + +internal Search_Match +search_next_match(Partition *part, Search_Set *set, Search_Iter *iter_){ + Search_Match result = {}; + Search_Iter iter = *iter_; + Search_Range *range; + Temp_Memory temp; + char *spare; + i32 start_pos, end_pos, count; + + temp = begin_temp_memory(part); + spare = push_array(part, char, iter.word.size); + + count = set->count; + for (; iter.i < count;){ + range = set->ranges + iter.i; + + end_pos = range->start + range->size; + + if (iter.pos + iter.word.size < end_pos){ + start_pos = Max(iter.pos, range->start); + result.start = buffer_find_string(range->buffer, start_pos, end_pos, iter.word.str, iter.word.size, spare); + + if (result.start < end_pos){ + iter.pos = result.start + 1; + if (result.start == 0 || !char_is_alpha_numeric(buffer_get_char(range->buffer, result.start - 1))){ + result.end = buffer_seek_word_right_assume_on_word(range->buffer, result.start); + if (result.end < end_pos){ + result.found_match = 1; + result.buffer = range->buffer; + iter.pos = result.end; + break; + } + } + } + else{ + ++iter.i, iter.pos = 0; + } + } + else{ + ++iter.i, iter.pos = 0; + } + } + end_temp_memory(temp); + + *iter_ = iter; + + return(result); +} + +inline void +view_change_size(System_Functions *system, General_Memory *general, View *view){ + if (view->file){ + view_measure_wraps(system, general, view); + view->cursor = view_compute_cursor_from_pos(view, view->cursor.pos); + } +} + +struct Live_Views{ + View *views; + View free_sentinel; + i32 count, max; +}; + +internal View_And_ID +live_set_alloc_view(Live_Views *live_set, Panel *panel, Models *models){ + View_And_ID result = {}; + + Assert(live_set->count < live_set->max); + ++live_set->count; + + result.view = live_set->free_sentinel.next; + result.id = (i32)(result.view - live_set->views); + + dll_remove(result.view); + memset(result.view, 0, sizeof(*result.view)); + result.view->id = result.id; + + result.view->in_use = 1; + panel->view = result.view; + result.view->panel = panel; + + result.view->models = models; + result.view->scrub_max = 1; + +#if 0 + // TODO(allen): Make "interactive" mode customizable just like the query bars! + result.view->query = make_fixed_width_string(result.view->query_); + result.view->dest = make_fixed_width_string(result.view->dest_); +#endif + + init_query_set(&result.view->query_set); + + { + i32 gui_mem_size = Kbytes(32); + void *gui_mem = general_memory_allocate(&models->mem.general, gui_mem_size, 0); + result.view->gui_target.push = partition_open(gui_mem, gui_mem_size); + } + + return(result); +} + +inline void +live_set_free_view(System_Functions *system, Exchange *exchange, Live_Views *live_set, View *view){ + Assert(live_set->count > 0); + --live_set->count; + free_file_view(view); + dll_insert(&live_set->free_sentinel, view); + view->in_use = 0; +} + +// BOTTOM + diff --git a/4ed_gui.cpp b/4ed_gui.cpp index d672e4b1..256b0e4e 100644 --- a/4ed_gui.cpp +++ b/4ed_gui.cpp @@ -64,2164 +64,236 @@ free_query_slot(Query_Set *set, Query_Bar *match_bar){ } } -struct Single_Line_Input_Step{ - b8 hit_newline; - b8 hit_ctrl_newline; - b8 hit_a_character; - b8 hit_backspace; - b8 hit_esc; - b8 made_a_change; - b8 did_command; - b8 no_file_match; -}; - -enum Single_Line_Input_Type{ - SINGLE_LINE_STRING, - SINGLE_LINE_FILE -}; - -struct Single_Line_Mode{ - Single_Line_Input_Type type; - String *string; - Hot_Directory *hot_directory; - b32 fast_folder_select; - b32 try_to_match; - b32 case_sensitive; -}; - -internal Single_Line_Input_Step -app_single_line_input_core(System_Functions *system, Working_Set *working_set, - Key_Event_Data key, Single_Line_Mode mode){ - Single_Line_Input_Step result = {}; - - if (key.keycode == key_back){ - result.hit_backspace = 1; - if (mode.string->size > 0){ - result.made_a_change = 1; - --mode.string->size; - switch (mode.type){ - case SINGLE_LINE_STRING: - mode.string->str[mode.string->size] = 0; break; - - case SINGLE_LINE_FILE: - { - char end_character = mode.string->str[mode.string->size]; - if (char_is_slash(end_character)){ - mode.string->size = reverse_seek_slash(*mode.string) + 1; - mode.string->str[mode.string->size] = 0; - hot_directory_set(system, mode.hot_directory, *mode.string, working_set); - } - else{ - mode.string->str[mode.string->size] = 0; - } - }break; - } - } - } - - else if (key.character == '\n' || key.character == '\t'){ - result.made_a_change = 1; - if (key.modifiers[MDFR_CONTROL_INDEX] || - key.modifiers[MDFR_ALT_INDEX]){ - result.hit_ctrl_newline = 1; - } - else{ - result.hit_newline = 1; - if (mode.fast_folder_select){ - Hot_Directory_Match match; - char front_name_space[256]; - String front_name = make_fixed_width_string(front_name_space); - get_front_of_directory(&front_name, *mode.string); - - match = - hot_directory_first_match(mode.hot_directory, front_name, 1, 1, mode.case_sensitive); - - if (mode.try_to_match && !match.filename.str){ - match = hot_directory_first_match(mode.hot_directory, front_name, 1, 0, mode.case_sensitive); - } - if (match.filename.str){ - if (match.is_folder){ - set_last_folder(mode.string, match.filename, mode.hot_directory->slash); - hot_directory_set(system, mode.hot_directory, *mode.string, working_set); - result.hit_newline = 0; - } - else{ - if (mode.try_to_match){ - mode.string->size = reverse_seek_slash(*mode.string) + 1; - append(mode.string, match.filename); - } - } - } - else{ - if (mode.try_to_match){ - result.no_file_match = 1; - } - } - } - } - } - - else if (key.keycode == key_esc){ - result.hit_esc = 1; - result.made_a_change = 1; - } - - else if (key.character){ - result.hit_a_character = 1; - if (!key.modifiers[MDFR_CONTROL_INDEX] && - !key.modifiers[MDFR_ALT_INDEX]){ - if (mode.string->size+1 < mode.string->memory_size){ - u8 new_character = (u8)key.character; - mode.string->str[mode.string->size] = new_character; - mode.string->size++; - mode.string->str[mode.string->size] = 0; - if (mode.type == SINGLE_LINE_FILE && char_is_slash(new_character)){ - hot_directory_set(system, mode.hot_directory, *mode.string, working_set); - } - result.made_a_change = 1; - } - } - else{ - result.did_command = 1; - result.made_a_change = 1; - } - } - - return result; -} - -inline Single_Line_Input_Step -app_single_line_input_step(System_Functions *system, Key_Event_Data key, String *string){ - Single_Line_Mode mode = {}; - mode.type = SINGLE_LINE_STRING; - mode.string = string; - return app_single_line_input_core(system, 0, key, mode); -} - -inline Single_Line_Input_Step -app_single_file_input_step(System_Functions *system, - Working_Set *working_set, Key_Event_Data key, - String *string, Hot_Directory *hot_directory, - b32 fast_folder_select, b32 try_to_match, b32 case_sensitive){ - Single_Line_Mode mode = {}; - mode.type = SINGLE_LINE_FILE; - mode.string = string; - mode.hot_directory = hot_directory; - mode.fast_folder_select = fast_folder_select; - mode.try_to_match = try_to_match; - mode.case_sensitive = case_sensitive; - return app_single_line_input_core(system, working_set, key, mode); -} - -inline Single_Line_Input_Step -app_single_number_input_step(System_Functions *system, Key_Event_Data key, String *string){ - Single_Line_Input_Step result = {}; - Single_Line_Mode mode = {}; - mode.type = SINGLE_LINE_STRING; - mode.string = string; - - char c = (char)key.character; - if (c == 0 || c == '\n' || char_is_numeric(c)) - result = app_single_line_input_core(system, 0, key, mode); - return result; -} - -struct Widget_ID{ - i32 id; - i32 sub_id0; - i32 sub_id1; - i32 sub_id2; -}; - -inline b32 -widget_match(Widget_ID s1, Widget_ID s2){ - return (s1.id == s2.id && s1.sub_id0 == s2.sub_id0 && - s1.sub_id1 == s2.sub_id1 && s1.sub_id2 == s2.sub_id2); -} - -struct UI_State{ - Render_Target *target; - Style *style; - Font_Set *font_set; - Mouse_State *mouse; - Key_Summary *keys; - Working_Set *working_set; - i16 font_id; - - Widget_ID selected, hover, hot; - b32 activate_me; - b32 redraw; - b32 input_stage; - i32 sub_id1_change; - - f32 height, view_y; -}; - -inline Widget_ID -make_id(UI_State *state, i32 id){ - Widget_ID r = state->selected; - r.id = id; - return r; -} - -inline Widget_ID -make_sub0(UI_State *state, i32 id){ - Widget_ID r = state->selected; - r.sub_id0 = id; - return r; -} - -inline Widget_ID -make_sub1(UI_State *state, i32 id){ - Widget_ID r = state->selected; - r.sub_id1 = id; - return r; -} - -inline Widget_ID -make_sub2(UI_State *state, i32 id){ - Widget_ID r = state->selected; - r.sub_id2 = id; - return r; -} - -inline b32 -is_selected(UI_State *state, Widget_ID id){ - return widget_match(state->selected, id); -} - -inline b32 -is_hot(UI_State *state, Widget_ID id){ - return widget_match(state->hot, id); -} - -inline b32 -is_hover(UI_State *state, Widget_ID id){ - return widget_match(state->hover, id); -} - -struct UI_Layout{ - i32 row_count; - i32 row_item_width; - i32 row_max_item_height; - - i32_Rect rect; - i32 x, y; -}; - -struct UI_Layout_Restore{ - UI_Layout layout; - UI_Layout *dest; -}; - -inline void -begin_layout(UI_Layout *layout, i32_Rect rect){ - layout->rect = rect; - layout->x = rect.x0; - layout->y = rect.y0; - layout->row_count = 0; - layout->row_max_item_height = 0; -} - -inline void -begin_row(UI_Layout *layout, i32 count){ - layout->row_count = count; - layout->row_item_width = (layout->rect.x1 - layout->x) / count; -} - -inline i32_Rect -layout_rect(UI_Layout *layout, i32 height){ - i32_Rect rect; - rect.x0 = layout->x; - rect.y0 = layout->y; - rect.x1 = rect.x0; - rect.y1 = rect.y0 + height; - if (layout->row_count > 0){ - --layout->row_count; - rect.x1 = rect.x0 + layout->row_item_width; - layout->x += layout->row_item_width; - layout->row_max_item_height = Max(height, layout->row_max_item_height); - } - if (layout->row_count == 0){ - rect.x1 = layout->rect.x1; - layout->row_max_item_height = Max(height, layout->row_max_item_height); - layout->y += layout->row_max_item_height; - layout->x = layout->rect.x0; - layout->row_max_item_height = 0; - } - return rect; -} - -inline UI_Layout_Restore -begin_sub_layout(UI_Layout *layout, i32_Rect area){ - UI_Layout_Restore restore; - restore.layout = *layout; - restore.dest = layout; - begin_layout(layout, area); - return restore; -} - -inline void -end_sub_layout(UI_Layout_Restore restore){ - *restore.dest = restore.layout; -} - -struct UI_Style{ - u32 dark, dim, bright; - u32 base, pop1, pop2; -}; - -internal UI_Style -get_ui_style(Style *style){ - UI_Style ui_style; - ui_style.dark = style->main.back_color; - ui_style.dim = style->main.margin_color; - ui_style.bright = style->main.margin_active_color; - ui_style.base = style->main.default_color; - ui_style.pop1 = style->main.file_info_style.pop1_color; - ui_style.pop2 = style->main.file_info_style.pop2_color; - return ui_style; -} - -internal UI_Style -get_ui_style_upper(Style *style){ - UI_Style ui_style; - ui_style.dark = style->main.margin_color; - ui_style.dim = style->main.margin_hover_color; - ui_style.bright = style->main.margin_active_color; - ui_style.base = style->main.default_color; - ui_style.pop1 = style->main.file_info_style.pop1_color; - ui_style.pop2 = style->main.file_info_style.pop2_color; - return ui_style; -} - -inline void -get_colors(UI_State *state, u32 *back, u32 *fore, Widget_ID wid, UI_Style style){ - bool32 hover = is_hover(state, wid); - bool32 hot = is_hot(state, wid); - i32 level = hot + hover; - switch (level){ - case 2: - *back = style.bright; - *fore = style.dark; - break; - case 1: - *back = style.dim; - *fore = style.bright; - break; - case 0: - *back = style.dark; - *fore = style.bright; - break; - } -} - -inline void -get_pop_color(UI_State *state, u32 *pop, Widget_ID wid, UI_Style style){ - bool32 hover = is_hover(state, wid); - bool32 hot = is_hot(state, wid); - i32 level = hot + hover; - switch (level){ - case 2: - *pop = style.pop1; - break; - case 1: - *pop = style.pop1; - break; - case 0: - *pop = style.pop1; - break; - } -} - -internal UI_State -ui_state_init(UI_State *state_in, Render_Target *target, Input_Summary *user_input, - Style *style, i16 font_id, Font_Set *font_set, Working_Set *working_set, - b32 input_stage){ - UI_State state = {}; - state.target = target; - state.style = style; - state.font_set = font_set; - state.font_id = font_id; - state.working_set = working_set; - if (user_input){ - state.mouse = &user_input->mouse; - state.keys = &user_input->keys; - } - state.selected = state_in->selected; - state.hot = state_in->hot; - if (input_stage) state.hover = {}; - else state.hover = state_in->hover; - state.redraw = 0; - state.activate_me = 0; - state.input_stage = input_stage; - state.height = state_in->height; - state.view_y = state_in->view_y; - return state; -} - -inline b32 -ui_state_match(UI_State a, UI_State b){ - return (widget_match(a.selected, b.selected) && - widget_match(a.hot, b.hot) && - widget_match(a.hover, b.hover)); -} - -internal b32 -ui_finish_frame(UI_State *persist_state, UI_State *state, UI_Layout *layout, i32_Rect rect, - b32 do_wheel, b32 *did_activation){ - b32 result = 0; - f32 h = layout->y + persist_state->view_y - rect.y0; - f32 max_y = h - (rect.y1 - rect.y0); - - persist_state->height = h; - persist_state->view_y = state->view_y; - - if (state->input_stage){ - Mouse_State *mouse = state->mouse; - Font_Set *font_set = state->font_set; - - if (mouse->wheel != 0 && do_wheel){ - i32 height = get_font_info(font_set, state->font_id)->height; - persist_state->view_y += mouse->wheel*height; - result = 1; - } - if (mouse->release_l && widget_match(state->hot, state->hover)){ - if (did_activation) *did_activation = 1; - if (state->activate_me){ - state->selected = state->hot; - } - } - if (!mouse->l && !mouse->r){ - state->hot = {}; - } - - if (!ui_state_match(*persist_state, *state) || state->redraw){ - result = 1; - } - - *persist_state = *state; - } - - if (persist_state->view_y >= max_y) persist_state->view_y = max_y; - if (persist_state->view_y < 0) persist_state->view_y = 0; - - return result; -} - -internal b32 -ui_do_button_input(UI_State *state, i32_Rect rect, Widget_ID id, bool32 activate, bool32 *right = 0){ - b32 result = 0; - Mouse_State *mouse = state->mouse; - b32 hover = hit_check(mouse->x, mouse->y, rect); - if (hover){ - state->hover = id; - if (activate) state->activate_me = 1; - if (mouse->press_l || (mouse->press_r && right)) state->hot = id; - if (mouse->l && mouse->r) state->hot = {}; - } - bool32 is_match = is_hot(state, id); - if (mouse->release_l && is_match){ - if (hover) result = 1; - state->redraw = 1; - } - if (right && mouse->release_r && is_match){ - if (hover) *right = 1; - state->redraw = 1; - } - return result; -} - -internal bool32 -ui_do_subdivided_button_input(UI_State *state, i32_Rect rect, i32 parts, Widget_ID id, bool32 activate, i32 *indx_out, bool32 *right = 0){ - bool32 result = 0; - real32 x0, x1; - i32_Rect sub_rect; - Widget_ID sub_widg = id; - real32 sub_width = (rect.x1 - rect.x0) / (real32)parts; - sub_rect.y0 = rect.y0; - sub_rect.y1 = rect.y1; - x1 = (real32)rect.x0; - - for (i32 i = 0; i < parts; ++i){ - x0 = x1; - x1 = x1 + sub_width; - sub_rect.x0 = TRUNC32(x0); - sub_rect.x1 = TRUNC32(x1); - sub_widg.sub_id2 = i; - if (ui_do_button_input(state, sub_rect, sub_widg, activate, right)){ - *indx_out = i; - break; - } - } - - return result; -} - -internal real32 -ui_do_vscroll_input(UI_State *state, i32_Rect top, i32_Rect bottom, i32_Rect slider, - Widget_ID id, real32 val, real32 step_amount, - real32 smin, real32 smax, real32 vmin, real32 vmax){ - Mouse_State *mouse = state->mouse; - i32 mx = mouse->x; - i32 my = mouse->y; - if (hit_check(mx, my, top)){ - state->hover = id; - state->hover.sub_id2 = 1; - } - if (hit_check(mx, my, bottom)){ - state->hover = id; - state->hover.sub_id2 = 2; - } - if (hit_check(mx, my, slider)){ - state->hover = id; - state->hover.sub_id2 = 3; - } - if (mouse->press_l) state->hot = state->hover; - if (id.id == state->hot.id){ - if (mouse->release_l){ - Widget_ID wid1, wid2; - wid1 = wid2 = id; - wid1.sub_id2 = 1; - wid2.sub_id2 = 2; - if (state->hot.sub_id2 == 1 && is_hover(state, wid1)) val -= step_amount; - if (state->hot.sub_id2 == 2 && is_hover(state, wid2)) val += step_amount; - state->redraw = 1; - } - if (state->hot.sub_id2 == 3){ - f32 S, L; - S = (f32)mouse->y - (slider.y1 - slider.y0) / 2; - if (S < smin) S = smin; - if (S > smax) S = smax; - L = unlerp(smin, S, smax); - val = lerp(vmin, L, vmax); - state->redraw = 1; - } - } - return val; -} - -internal b32 -ui_do_text_field_input(UI_State *state, String *str){ - b32 result = 0; - Key_Summary *keys = state->keys; - for (i32 key_i = 0; key_i < keys->count; ++key_i){ - Key_Event_Data key = get_single_key(keys, key_i); - char c = (char)key.character; - if (char_is_basic(c) && str->size < str->memory_size-1){ - str->str[str->size++] = c; - str->str[str->size] = 0; - } - else if (c == '\n'){ - result = 1; - } - else if (key.keycode == key_back && str->size > 0){ - str->str[--str->size] = 0; - } - } - return result; -} - -internal b32 -ui_do_file_field_input(System_Functions *system, UI_State *state, - Hot_Directory *hot_dir, b32 try_to_match, b32 case_sensitive){ - Key_Event_Data key; - Single_Line_Input_Step step; - String *str = &hot_dir->string; - Key_Summary *keys = state->keys; - i32 key_i; - b32 result = 0; - - terminate_with_null(str); - - for (key_i = 0; key_i < keys->count; ++key_i){ - key = get_single_key(keys, key_i); - step = - app_single_file_input_step(system, state->working_set, key, str, - hot_dir, 1, try_to_match, case_sensitive); - if ((step.hit_newline || step.hit_ctrl_newline) && !step.no_file_match) result = 1; - } - return result; -} - -internal b32 -ui_do_line_field_input(System_Functions *system, - UI_State *state, String *string){ - b32 result = 0; - Key_Summary *keys = state->keys; - for (i32 key_i = 0; key_i < keys->count; ++key_i){ - Key_Event_Data key = get_single_key(keys, key_i); - terminate_with_null(string); - Single_Line_Input_Step step = - app_single_line_input_step(system, key, string); - if (step.hit_newline || step.hit_ctrl_newline) result = 1; - } - return result; -} - -internal b32 -ui_do_slider_input(UI_State *state, i32_Rect rect, Widget_ID wid, - real32 min, real32 max, real32 *v){ - b32 result = 0; - ui_do_button_input(state, rect, wid, 0); - Mouse_State *mouse = state->mouse; - if (is_hot(state, wid)){ - result = 1; - *v = unlerp(min, (f32)mouse->x, max); - state->redraw = 1; - } - return result; -} - -internal bool32 -do_text_field(Widget_ID wid, UI_State *state, UI_Layout *layout, String prompt, String dest){ - b32 result = 0; - i32 character_h = get_font_info(state->font_set, state->font_id)->height; - - i32_Rect rect = layout_rect(layout, character_h); - - if (state->input_stage){ - ui_do_button_input(state, rect, wid, 1); - if (is_selected(state, wid)){ - if (ui_do_text_field_input(state, &dest)){ - result = 1; - } - } - } - else{ - Render_Target *target = state->target; - UI_Style ui_style = get_ui_style_upper(state->style); - u32 back, fore, prompt_pop; - get_colors(state, &back, &fore, wid, ui_style); - get_pop_color(state, &prompt_pop, wid, ui_style); - - draw_rectangle(target, rect, back); - - i32 x = draw_string(target, state->font_id, prompt, rect.x0, rect.y0 + 1, prompt_pop); - draw_string(target, state->font_id, dest, x, rect.y0 + 1, ui_style.base); - } - - return result; -} - -internal b32 -do_button(i32 id, UI_State *state, UI_Layout *layout, char *text, i32 height_mult, - b32 is_toggle = 0, b32 on = 0){ - b32 result = 0; - i16 font_id = state->font_id; - i32 character_h = get_font_info(state->font_set, font_id)->height; - - i32_Rect btn_rect = layout_rect(layout, character_h * height_mult); - if (height_mult > 1) btn_rect = get_inner_rect(btn_rect, 2); - else{ - btn_rect.x0 += 2; - btn_rect.x1 -= 2; - } - - Widget_ID wid = make_id(state, id); - - if (state->input_stage){ - if (ui_do_button_input(state, btn_rect, wid, 0)){ - result = 1; - } - } - else{ - Render_Target *target = state->target; - UI_Style ui_style = get_ui_style(state->style); - u32 back, fore, outline; - outline = ui_style.bright; - get_colors(state, &back, &fore, wid, ui_style); - - draw_rectangle(target, btn_rect, back); - draw_rectangle_outline(target, btn_rect, outline); - real32 text_width = font_string_width(target, font_id, text); - i32 box_width = btn_rect.x1 - btn_rect.x0; - i32 box_height = btn_rect.y1 - btn_rect.y0; - i32 x_pos = TRUNC32(btn_rect.x0 + (box_width - text_width)*.5f); - draw_string(target, font_id, text, x_pos, btn_rect.y0 + (box_height - character_h) / 2, fore); - - if (is_toggle){ - i32_Rect on_box = get_inner_rect(btn_rect, character_h/2); - on_box.x1 = on_box.x0 + (on_box.y1 - on_box.y0); - - if (on) draw_rectangle(target, on_box, fore); - else draw_rectangle(target, on_box, back); - draw_rectangle_outline(target, on_box, fore); - } - } - - return result; -} - -internal b32 -do_undo_slider(Widget_ID wid, UI_State *state, UI_Layout *layout, i32 max, i32 v, Undo_Data *undo, i32 *out){ - b32 result = 0; - i16 font_id = state->font_id; - i32 character_h = get_font_info(state->font_set, font_id)->height; - - i32_Rect containing_rect = layout_rect(layout, character_h); - - i32_Rect click_rect; - click_rect.x0 = containing_rect.x0 + character_h - 1; - click_rect.x1 = containing_rect.x1 - character_h + 1; - click_rect.y0 = containing_rect.y0 + 2; - click_rect.y1 = containing_rect.y1 - 2; - - if (state->input_stage){ - real32 l; - if (ui_do_slider_input(state, click_rect, wid, (real32)click_rect.x0, (real32)click_rect.x1, &l)){ - real32 v_new = lerp(0.f, l, (real32)max); - v = ROUND32(v_new); - result = 1; - if (out) *out = v; - } - } - else{ - Render_Target *target = state->target; - if (max > 0){ - UI_Style ui_style = get_ui_style_upper(state->style); - - real32 L = unlerp(0.f, (real32)v, (real32)max); - i32 x = FLOOR32(lerp((real32)click_rect.x0, L, (real32)click_rect.x1)); - - i32 bar_top = ((click_rect.y0 + click_rect.y1) >> 1) - 1; - i32 bar_bottom = bar_top + 2; - - bool32 show_bar = 1; - real32 tick_step = (click_rect.x1 - click_rect.x0) / (real32)max; - bool32 show_ticks = 1; - if (tick_step <= 5.f) show_ticks = 0; - - if (undo == 0){ - if (show_bar){ - i32_Rect slider_rect; - slider_rect.x0 = click_rect.x0; - slider_rect.x1 = x; - slider_rect.y0 = bar_top; - slider_rect.y1 = bar_bottom; - - draw_rectangle(target, slider_rect, ui_style.dim); - - slider_rect.x0 = x; - slider_rect.x1 = click_rect.x1; - draw_rectangle(target, slider_rect, ui_style.pop1); - } - - if (show_ticks){ - f32_Rect tick; - tick.x0 = (real32)click_rect.x0 - 1; - tick.x1 = (real32)click_rect.x0 + 1; - tick.y0 = (real32)bar_top - 3; - tick.y1 = (real32)bar_bottom + 3; - - for (i32 i = 0; i < v; ++i){ - draw_rectangle(target, tick, ui_style.dim); - tick.x0 += tick_step; - tick.x1 += tick_step; - } - - for (i32 i = v; i <= max; ++i){ - draw_rectangle(target, tick, ui_style.pop1); - tick.x0 += tick_step; - tick.x1 += tick_step; - } - } - } - else{ - if (show_bar){ - i32_Rect slider_rect; - slider_rect.x0 = click_rect.x0; - slider_rect.y0 = bar_top; - slider_rect.y1 = bar_bottom; - - Edit_Step *history = undo->history.edits; - i32 block_count = undo->history_block_count; - Edit_Step *step = history; - for (i32 i = 0; i < block_count; ++i){ - u32 color; - if (step->type == ED_REDO || - step->type == ED_UNDO) color = ui_style.pop1; - else color = ui_style.dim; - - real32 L; - if (i + 1 == block_count){ - L = 1.f; - }else{ - step = history + step->next_block; - L = unlerp(0.f, (real32)(step - history), (real32)max); - } - if (L > 1.f) L = 1.f; - i32 x = FLOOR32(lerp((real32)click_rect.x0, L, (real32)click_rect.x1)); - - slider_rect.x1 = x; - draw_rectangle(target, slider_rect, color); - slider_rect.x0 = slider_rect.x1; - - if (L == 1.f) break; - } - } - - if (show_ticks){ - f32_Rect tick; - tick.x0 = (real32)click_rect.x0 - 1; - tick.x1 = (real32)click_rect.x0 + 1; - tick.y0 = (real32)bar_top - 3; - tick.y1 = (real32)bar_bottom + 3; - - Edit_Step *history = undo->history.edits; - u32 color = ui_style.dim; - for (i32 i = 0; i <= max; ++i){ - if (i != max){ - if (history[i].type == ED_REDO) color = ui_style.pop1; - else if (history[i].type == ED_UNDO || - history[i].type == ED_NORMAL) color = ui_style.pop2; - else color = ui_style.dim; - } - draw_rectangle(target, tick, color); - tick.x0 += tick_step; - tick.x1 += tick_step; - } - } - } - - i32_Rect slider_handle; - slider_handle.x0 = x - 2; - slider_handle.x1 = x + 2; - slider_handle.y0 = click_rect.y0; - slider_handle.y1 = click_rect.y1; - - draw_rectangle(target, slider_handle, ui_style.bright); - } - } - - return result; -} - -internal void -do_label(UI_State *state, UI_Layout *layout, char *text, int text_size, f32 height = 2.f){ - Style *style = state->style; - i16 font_id = state->font_id; - i32 line_height = get_font_info(state->font_set, font_id)->height; - i32_Rect label = layout_rect(layout, FLOOR32(line_height * height)); - - if (!state->input_stage){ - Render_Target *target = state->target; - u32 back = style->main.margin_color; - u32 fore = style->main.default_color; - draw_rectangle(target, label, back); - i32 height = label.y1 - label.y0; - - String textstr = make_string(text, text_size); - draw_string(target, font_id, textstr, label.x0, - label.y0 + (height - line_height)/2, fore); - } -} - -inline void -do_label(UI_State *state, UI_Layout *layout, String text, f32 height = 2.f){ - do_label(state, layout, text.str, text.size, height); -} - -internal void -do_scroll_bar(UI_State *state, i32_Rect rect){ - i32 id = 1; - i32 w = (rect.x1 - rect.x0); - i32 h = (rect.y1 - rect.y0); - - i32_Rect top_arrow, bottom_arrow; - top_arrow.x0 = rect.x0; - top_arrow.x1 = rect.x1; - top_arrow.y0 = rect.y0; - top_arrow.y1 = top_arrow.y0 + w; - - bottom_arrow.x0 = rect.x0; - bottom_arrow.x1 = rect.x1; - bottom_arrow.y1 = rect.y1; - bottom_arrow.y0 = bottom_arrow.y1 - w; - - f32 space_h = (f32)(bottom_arrow.y0 - top_arrow.y1); - if (space_h <= w) return; - - i32 slider_h = w; - - f32 view_hmin = 0; - f32 view_hmax = state->height - h; - f32 L = unlerp(view_hmin, state->view_y, view_hmax); - - f32 slider_hmin = (f32)top_arrow.y1; - f32 slider_hmax = (f32)bottom_arrow.y0 - slider_h; - f32 S = lerp(slider_hmin, L, slider_hmax); - - i32_Rect slider; - slider.x0 = rect.x0; - slider.x1 = rect.x1; - slider.y0 = FLOOR32(S); - slider.y1 = slider.y0 + slider_h; - - Widget_ID wid = make_id(state, id); - - if (state->input_stage){ - state->view_y = - ui_do_vscroll_input(state, top_arrow, bottom_arrow, slider, wid, state->view_y, - (f32)(get_font_info(state->font_set, state->font_id)->height), - slider_hmin, slider_hmax, view_hmin, view_hmax); - } - else{ - Render_Target *target = state->target; - - f32 x0, y0, x1, y1, x2, y2; - f32 w_1_2 = w*.5f; - f32 w_1_3 = w*.333333f; - f32 w_2_3 = w*.666667f; - - - UI_Style ui_style = get_ui_style(state->style); - u32 outline, back, fore; - - outline = ui_style.bright; - - wid.sub_id2 = 0; - - x0 = (w_1_2 + top_arrow.x0); - x1 = (w_1_3 + top_arrow.x0); - x2 = (w_2_3 + top_arrow.x0); - - ++wid.sub_id2; - y0 = (w_1_3 + top_arrow.y0); - y1 = (w_2_3 + top_arrow.y0); - y2 = (w_2_3 + top_arrow.y0); - get_colors(state, &back, &fore, wid, ui_style); - draw_rectangle(target, top_arrow, back); - draw_rectangle_outline(target, top_arrow, outline); - - ++wid.sub_id2; - y0 = (w_2_3 + bottom_arrow.y0); - y1 = (w_1_3 + bottom_arrow.y0); - y2 = (w_1_3 + bottom_arrow.y0); - get_colors(state, &back, &fore, wid, ui_style); - draw_rectangle(target, bottom_arrow, back); - draw_rectangle_outline(target, bottom_arrow, outline); - - ++wid.sub_id2; - get_colors(state, &back, &fore, wid, ui_style); - draw_rectangle(target, slider, back); - draw_rectangle_outline(target, slider, outline); - - draw_rectangle_outline(target, rect, outline); - } -} - -internal void -draw_gradient_slider(Render_Target *target, Vec4 base, i32 channel, - i32 steps, f32 top, f32_Rect slider, b32 hsla){ - Vec4 low, high; - f32 *lowv, *highv; - f32 x; - f32 next_x; - f32 x_step; - f32 v_step; - f32 m; - - x = (real32)slider.x0; - x_step = (real32)(slider.x1 - slider.x0) / steps; - v_step = top / steps; - m = 1.f / top; - lowv = &low.v[channel]; - highv = &high.v[channel]; - - if (hsla){ - for (i32 i = 0; i < steps; ++i){ - low = high = base; - *lowv = (i * v_step); - *highv = *lowv + v_step; - *lowv *= m; - *highv *= m; - low = hsla_to_rgba(low); - high = hsla_to_rgba(high); - - next_x = x + x_step; - draw_gradient_2corner_clipped( - target, x, slider.y0, next_x, slider.y1, - low, high); - x = next_x; - } - } - else{ - for (i32 i = 0; i < steps; ++i){ - low = high = base; - *lowv = (i * v_step); - *highv = *lowv + v_step; - *lowv *= m; - *highv *= m; - - next_x = x + x_step; - draw_gradient_2corner_clipped( - target, x, slider.y0, next_x, slider.y1, - low, high); - x = next_x; - } - } -} - -inline void -draw_hsl_slider(Render_Target *target, Vec4 base, i32 channel, - i32 steps, f32 top, f32_Rect slider){ - draw_gradient_slider(target, base, channel, steps, top, slider, 1); -} - -inline void -draw_rgb_slider(Render_Target *target, Vec4 base, i32 channel, - i32 steps, f32 top, f32_Rect slider){ - draw_gradient_slider(target, base, channel, steps, top, slider, 0); -} - -internal b32 -do_main_file_box(System_Functions *system, UI_State *state, UI_Layout *layout, - Hot_Directory *hot_directory, b32 try_to_match, b32 case_sensitive, char *end){ - b32 result = 0; - Style *style = state->style; - String *string = &hot_directory->string; - - i16 font_id = state->font_id; - i32 line_height = get_font_info(state->font_set, font_id)->height; - i32_Rect box = layout_rect(layout, line_height + 2); - - if (state->input_stage){ - if (ui_do_file_field_input(system, state, hot_directory, try_to_match, case_sensitive)){ - result = 1; - } - } - else{ - Render_Target *target = state->target; - u32 back = style->main.margin_color; - u32 fore = style->main.default_color; - u32 special = style->main.special_character_color; - draw_rectangle(target, box, back); - i32 x = box.x0; - x = draw_string(target, font_id, string->str, x, box.y0, fore); - if (end) draw_string(target, font_id, end, x, box.y0, special); - } - - layout->y = box.y1; - return result; -} - -internal b32 -do_main_string_box(System_Functions *system, UI_State *state, UI_Layout *layout, String *string){ - b32 result = 0; - Style *style = state->style; - - i16 font_id = state->font_id; - i32 line_height = get_font_info(state->font_set, font_id)->height; - i32_Rect box = layout_rect(layout, line_height + 2); - - if (state->input_stage){ - if (ui_do_line_field_input(system, state, string)){ - result = 1; - } - } - else{ - Render_Target *target = state->target; - u32 back = style->main.margin_color; - u32 fore = style->main.default_color; - draw_rectangle(target, box, back); - i32 x = box.x0; - x = draw_string(target, font_id, string->str, x, box.y0, fore); - } - - layout->y = box.y1; - return result; -} - -internal b32 -do_list_option(i32 id, UI_State *state, UI_Layout *layout, String text){ - b32 result = 0; - Style *style = state->style; - - i16 font_id = state->font_id; - i32 character_h = get_font_info(state->font_set, font_id)->height; - - i32_Rect box = layout_rect(layout, character_h*2); - Widget_ID wid = make_id(state, id); - - if (state->input_stage){ - if (ui_do_button_input(state, box, wid, 0)){ - result = 1; - } - } - else{ - Render_Target *target = state->target; - i32_Rect inner = get_inner_rect(box, 3); - u32 back, outline, fore, pop; - back = style->main.back_color; - fore = style->main.default_color; - pop = style->main.file_info_style.pop2_color; - if (is_hover(state, wid)) outline = style->main.margin_active_color; - else outline = style->main.margin_color; - - draw_rectangle(target, inner, back); - i32 x = inner.x0, y = box.y0 + character_h/2; - x = draw_string(target, font_id, text, x, y, fore); - draw_margin(target, box, inner, outline); - } - - layout->y = box.y1; - return result; -} - -internal b32 -do_checkbox_list_option(i32 id, UI_State *state, UI_Layout *layout, String text, b32 is_on){ - b32 result = 0; - Style *style = state->style; - - i16 font_id = state->font_id; - i32 character_h = get_font_info(state->font_set, font_id)->height; - - i32_Rect box = layout_rect(layout, character_h*2); - Widget_ID wid = make_id(state, id); - - if (state->input_stage){ - if (ui_do_button_input(state, box, wid, 0)){ - result = 1; - } - } - else{ - Render_Target *target = state->target; - i32_Rect inner = get_inner_rect(box, 3); - u32 back, outline, fore, pop, box_color; - back = style->main.back_color; - fore = style->main.default_color; - pop = style->main.file_info_style.pop2_color; - if (is_hover(state, wid)) outline = style->main.margin_active_color; - else outline = style->main.margin_color; - box_color = style->main.margin_active_color; - - draw_rectangle(target, inner, back); - - i32_Rect square; - square = get_inner_rect(inner, character_h/3); - square.x1 = square.x0 + (square.y1 - square.y0); - if (is_on) draw_rectangle(target, square, box_color); - else draw_margin(target, square, 1, box_color); - - i32 x = square.x1 + 3; - i32 y = box.y0 + character_h/2; - x = draw_string(target, font_id, text, x, y, fore); - draw_margin(target, box, inner, outline); - } - - layout->y = box.y1; - return result; -} - - -internal b32 -do_file_option(i32 id, UI_State *state, UI_Layout *layout, String filename, b32 is_folder, String extra, char slash){ - b32 result = 0; - Style *style = state->style; - i16 font_id = state->font_id; - i32 character_h = get_font_info(state->font_set, font_id)->height; - char slash_buf[2] = { slash, 0 }; - - i32_Rect box = layout_rect(layout, character_h*2); - Widget_ID wid = make_id(state, id); - - if (state->input_stage){ - if (ui_do_button_input(state, box, wid, 0)){ - result = 1; - } - } - else{ - Render_Target *target = state->target; - i32_Rect inner = get_inner_rect(box, 3); - u32 back, outline, fore, pop; - back = style->main.back_color; - fore = style->main.default_color; - pop = style->main.file_info_style.pop2_color; - if (is_hover(state, wid)) outline = style->main.margin_active_color; - else outline = style->main.margin_color; - - draw_rectangle(target, inner, back); - i32 x = inner.x0, y = box.y0 + character_h/2; - x = draw_string(target, font_id, filename, x, y, fore); - if (is_folder) x = draw_string(target, font_id, slash_buf, x, y, fore); - draw_string(target, font_id, extra, x, y, pop); - draw_margin(target, box, inner, outline); - } - - layout->y = box.y1; - return result; -} - -internal b32 -do_file_list_box(System_Functions *system, UI_State *state, UI_Layout *layout, - Hot_Directory *hot_dir, b32 has_filter, b32 try_to_match, b32 case_sensitive, - b32 *new_dir, b32 *selected, char *end){ - b32 result = 0; - File_List *files = &hot_dir->file_list; - - if (do_main_file_box(system, state, layout, hot_dir, try_to_match, case_sensitive, end)){ - *selected = 1; - terminate_with_null(&hot_dir->string); - } - else{ - persist String p4c_extension = make_lit_string("p4c"); - persist String message_loaded = make_lit_string(" LOADED"); - persist String message_unsaved = make_lit_string(" LOADED *"); - persist String message_unsynced = make_lit_string(" LOADED !"); - persist String message_nothing = {}; - - char front_name_space[256]; - String front_name = make_fixed_width_string(front_name_space); - get_front_of_directory(&front_name, hot_dir->string); - - Absolutes absolutes; - get_absolutes(front_name, &absolutes, 1, 1); - - char full_path_[256]; - String full_path = make_fixed_width_string(full_path_); - get_path_of_directory(&full_path, hot_dir->string); - i32 restore_size = full_path.size; - - i32 i; - File_Info *info, *end; - end = files->infos + files->count; - for (info = files->infos, i = 0; info != end; ++info, ++i){ - String filename = info->filename; - - append(&full_path, filename); - terminate_with_null(&full_path); - - Editing_File *file = working_set_contains(system, state->working_set, full_path); - full_path.size = restore_size; - - b8 is_folder = (info->folder != 0); - b8 ext_match = (match(file_extension(filename), p4c_extension) != 0); - b8 name_match = (filename_match(front_name, &absolutes, filename, case_sensitive) != 0); - b8 is_loaded = (file != 0 && file_is_ready(file)); - - String message = message_nothing; - if (is_loaded){ - switch (buffer_get_sync(file)){ - case SYNC_GOOD: message = message_loaded; break; - case SYNC_BEHIND_OS: message = message_unsynced; break; - case SYNC_UNSAVED: message = message_unsaved; break; - } - } - - if ((is_folder || !has_filter || ext_match) && name_match){ - if (do_file_option(100+i, state, layout, filename, is_folder, message, system->slash)){ - result = 1; - hot_directory_clean_end(hot_dir); - append(&hot_dir->string, filename); - if (is_folder){ - *new_dir = 1; - append(&hot_dir->string, system->slash); - } - else{ - *selected = 1; - } - terminate_with_null(&hot_dir->string); - } - } - } - } - - return result; -} - -internal b32 -do_live_file_list_box(System_Functions *system, UI_State *state, UI_Layout *layout, - Working_Set *working_set, String *string, b32 *selected){ - b32 result = 0; - - if (do_main_string_box(system, state, layout, string)){ - *selected = 1; - terminate_with_null(string); - } - else{ - persist String message_unsaved = make_lit_string(" *"); - persist String message_unsynced = make_lit_string(" !"); - persist String message_nothing = {}; - - Absolutes absolutes; - get_absolutes(*string, &absolutes, 1, 1); - - Editing_File *file; - File_Node *node, *used_nodes; - i32 i = 0; - used_nodes = &working_set->used_sentinel; - - for (dll_items(node, used_nodes)){ - file = (Editing_File*)node; - Assert(!file->state.is_dummy); - - String message = message_nothing; - switch (buffer_get_sync(file)){ - case SYNC_BEHIND_OS: message = message_unsynced; break; - case SYNC_UNSAVED: message = message_unsaved; break; - } - - if (filename_match(*string, &absolutes, file->name.live_name, 1)){ - if (do_file_option(100+i, state, layout, file->name.live_name, 0, message, system->slash)){ - result = 1; - *selected = 1; - copy(string, file->name.source_path); - terminate_with_null(string); - } - } - - ++i; - } - } - - return result; -} - struct Super_Color{ Vec4 hsla; Vec4 rgba; u32 *out; }; -internal Super_Color -super_color_create(u32 packed){ - Super_Color result = {}; - result.rgba = unpack_color4(packed); - result.hsla = rgba_to_hsla(result.rgba); - return result; -} - -internal void -super_color_post_hsla(Super_Color *color, Vec4 hsla){ - color->hsla = hsla; - if (hsla.h == 1.f) - hsla.h = 0.f; - color->rgba = hsla_to_rgba(hsla); - *color->out = pack_color4(color->rgba); -} - -internal void -super_color_post_rgba(Super_Color *color, Vec4 rgba){ - color->rgba = rgba; - color->hsla = rgba_to_hsla(rgba); - *color->out = pack_color4(rgba); -} - -internal void -super_color_post_packed(Super_Color *color, u32 packed){ - color->rgba = unpack_color4(packed); - color->hsla = rgba_to_hsla(color->rgba); - *color->out = packed; -} - -u32 super_color_clear_masks[] = {0xFF00FFFF, 0xFFFF00FF, 0xFFFFFF00}; -u32 super_color_shifts[] = {16, 8, 0}; - -internal u32 -super_color_post_byte(Super_Color *color, i32 channel, u8 byte){ - u32 packed = *color->out; - packed &= super_color_clear_masks[channel]; - packed |= (byte << super_color_shifts[channel]); - super_color_post_packed(color, packed); - return packed; -} - -struct Color_Highlight{ - i32 ids[4]; +struct GUI_Target{ + Partition push; + b32 show_file; }; -struct Library_UI{ - UI_State *state; - UI_Layout *layout; - - Font_Set *fonts; - - Style_Library *styles; - Hot_Directory *hot_directory; +struct GUI_Header{ + i32 type; + i32 size; }; -struct Color_UI{ - UI_State *state; - UI_Layout *layout; - - Font_Set *fonts; - Style_Font *global_font; - - f32 hex_advance; - u32 *palette; - i32 palette_size; - - Color_Highlight highlight; - Super_Color color; - - b32 has_hover_color; - Super_Color hover_color; +#define NextHeader(h) ((GUI_Header*)((char*)(h) + (h)->size)) + +enum GUI_Command_Type{ + guicom_null, + guicom_begin_overlap, + guicom_end_overlap, + guicom_begin_serial, + guicom_end_serial, + guicom_top_bar, + guicom_file, }; -enum Channel_Field_Type{ - CF_DEC, - CF_HEX -}; - -internal void -do_single_slider(i32 sub_id, Color_UI *ui, i32 channel, b32 is_rgba, - i32 grad_steps, f32 top, f32_Rect slider, f32 v_handle, - i32_Rect rect){ - f32_Rect click_box = slider; - click_box.y0 -= v_handle; - - if (ui->state->input_stage){ - real32 v; - if (ui_do_slider_input(ui->state, i32R(click_box), make_sub1(ui->state, sub_id), slider.x0, slider.x1, &v)){ - Vec4 new_color; - if (is_rgba) new_color = ui->color.rgba; - else new_color = ui->color.hsla; - new_color.v[channel] = clamp(0.f, v, 1.f); - if (is_rgba) super_color_post_rgba(&ui->color, new_color); - else super_color_post_hsla(&ui->color, new_color); - } - } - else{ - Render_Target *target = ui->state->target; - Vec4 color; - real32 x; - if (is_rgba){ - color = ui->color.rgba; - draw_rgb_slider(target, V4(0,0,0,1.f), channel, 10, 100.f, slider); - } - else{ - i32 steps; - real32 top; - if (channel == 0){ - steps = 45; - top = 360.f; - } - else{ - steps = 10; - top = 100.f; - } - color = ui->color.hsla; - draw_hsl_slider(target, color, channel, steps, top, slider); - } - - x = lerp(slider.x0, color.v[channel], slider.x1); - draw_rectangle( - target, f32R(x, slider.y0, x + 1, slider.y1), 0xff000000); - - draw_rectangle( - target, f32R(x-2, click_box.y0, x+3, slider.y0-4), 0xff777777); - } -} - -internal void -do_hsl_sliders(Color_UI *ui, i32_Rect rect){ - real32 bar_width = (real32)(rect.x1 - rect.x0 - 20); - if (bar_width > 45){ - f32_Rect slider; - real32 y; - i32 sub_id; - - real32 v_full_space = 30.f; - real32 v_half_space = 15.f; - real32 v_quarter_space = 12.f; - real32 v_handle = 9.f; - - slider.x0 = rect.x0 + 10.f; - slider.x1 = slider.x0 + bar_width; - - sub_id = 0; - - i32 step_count[] = {45, 10, 10}; - real32 tops[] = {360.f, 100.f, 100.f}; - - y = rect.y0 + v_quarter_space; - for (i32 i = 0; i < 3; ++i){ - ++sub_id; - slider.y0 = y; - slider.y1 = slider.y0 + v_half_space; - do_single_slider(sub_id, ui, i, 0, step_count[i], tops[i], slider, v_handle, rect); - y += v_full_space; - } - } -} - -internal void -fill_buffer_color_channel(char *buffer, u8 x, Channel_Field_Type ftype){ - if (ftype == CF_DEC){ - u8 x0; - x0 = x / 10; - buffer[2] = (x - (10*x0)) + '0'; - x = x0; - x0 = x / 10; - buffer[1] = (x - (10*x0)) + '0'; - x = x0; - x0 = x / 10; - buffer[0] = (x - (10*x0)) + '0'; - } - else{ - u8 n; - n = x & 0xF; - buffer[1] = int_to_hexchar(n); - x >>= 4; - n = x & 0xF; - buffer[0] = int_to_hexchar(n); +internal b32 +gui_push_command(GUI_Target *target, void *item, i32 size){ + b32 result = 0; + void *dest = partition_allocate(&target->push, size); + if (dest){ + memcpy(dest, item, size); + result = 1; } + return(result); } internal b32 -do_channel_field(i32 sub_id, Color_UI *ui, u8 *channel, Channel_Field_Type ftype, - i32 y, u32 color, u32 back, i32 x0, i32 x1){ - b32 result = 0; - - i16 font_id = ui->state->font_id; - i32 line_height = get_font_info(ui->state->font_set, font_id)->height; - i32_Rect hit_region; - hit_region.x0 = x0; - hit_region.x1 = x1; - hit_region.y0 = y; - hit_region.y1 = y + line_height; - - i32 digit_count; - if (ftype == CF_DEC) digit_count = 3; - else digit_count = 2; - - Render_Target *target = ui->state->target; - - if (ui->state->input_stage){ - i32 indx; - ui_do_subdivided_button_input(ui->state, hit_region, digit_count, - make_sub1(ui->state, sub_id), 1, &indx); - } - else{ - if (ui->state->hover.sub_id1 == sub_id && ui->state->selected.sub_id1 != sub_id){ - draw_rectangle(target, hit_region, back); - } - } - - char string_buffer[4]; - string_buffer[digit_count] = 0; - fill_buffer_color_channel(string_buffer, *channel, ftype); - - if (ui->state->selected.sub_id1 == sub_id){ - i32 indx = ui->state->selected.sub_id2; - if (ui->state->input_stage){ - Key_Summary *keys = ui->state->keys; - for (i32 key_i = 0; key_i < keys->count; ++key_i){ - Key_Event_Data key = get_single_key(keys, key_i); - - if (key.keycode == key_right){ - ++indx; - if (indx > digit_count-1) indx = 0; - } - if (key.keycode == key_left){ - --indx; - if (indx < 0) indx = digit_count-1; - } - - i32 new_value = *channel; - if (key.keycode == key_up || key.keycode == key_down){ - i32 place = digit_count-1-indx; - i32 base = (ftype == CF_DEC)?10:0x10; - i32 step_amount = 1; - while (place > 0){ - step_amount *= base; - --place; - } - if (key.keycode == key_down){ - step_amount = 0 - step_amount; - } - new_value += step_amount; - } - - u8 c = (u8)key.character; - bool32 is_good = (ftype == CF_DEC)?char_is_numeric(c):char_is_hex(c); - if (is_good){ - string_buffer[indx] = c; - if (ftype == CF_DEC) - new_value = str_to_int(make_string(string_buffer, 3)); - else - new_value = hexstr_to_int(make_string(string_buffer, 2)); - ++indx; - if (indx > digit_count-1) indx = 0; - } - - if (c == '\n'){ - switch (sub_id){ - case 1: case 2: - case 4: case 5: - ui->state->sub_id1_change = sub_id + 3; break; - - case 7: case 8: - ui->state->sub_id1_change = sub_id - 6; break; - } - } - - if (new_value != *channel){ - if (new_value > 255){ - *channel = 255; - } - else if (new_value < 0){ - *channel = 0; - } - else{ - *channel = (u8)new_value; - } - fill_buffer_color_channel(string_buffer, *channel, ftype); - result = 1; - } - ui->state->selected.sub_id2 = indx; - } - } - else{ - f32_Rect r = f32R(hit_region); - r.x0 += indx*ui->hex_advance+1; - r.x1 = r.x0+ui->hex_advance+1; - draw_rectangle(target, r, back); - } - } - - if (!ui->state->input_stage) - draw_string_mono(target, font_id, string_buffer, - (real32)x0 + 1, (real32)y, ui->hex_advance, - color); - - return result; +gui_push_simple_command(GUI_Target *target, i32 type){ + b32 result; + GUI_Header item; + item.type = type; + item.size = sizeof(item); + result = gui_push_command(target, &item, item.size); + return(result); } internal void -do_rgb_sliders(Color_UI *ui, i32_Rect rect){ - i32 dec_x0, dec_x1; - dec_x0 = rect.x0 + 10; - dec_x1 = TRUNC32(dec_x0 + ui->hex_advance*3 + 2); - - i32 hex_x0, hex_x1; - hex_x0 = dec_x1 + 10; - hex_x1 = TRUNC32(hex_x0 + ui->hex_advance*2 + 2); - - rect.x0 = hex_x1; - real32 bar_width = (real32)(rect.x1 - rect.x0 - 20); - - f32_Rect slider; - f32 y; - i32 sub_id; - u8 channel; - - real32 v_full_space = 30.f; - real32 v_half_space = 15.f; - real32 v_quarter_space = 12.f; - real32 v_handle = 9.f; - - u32 packed_color = *ui->color.out; - - y = rect.y0 + v_quarter_space; - slider.x0 = rect.x0 + 10.f; - slider.x1 = slider.x0 + bar_width; - - sub_id = 0; - - persist i32 shifts[3] = { 16, 8, 0 }; - persist u32 fore_colors[3] = { 0xFFFF0000, 0xFF00FF00, 0xFF1919FF }; - persist u32 back_colors[3] = { 0xFF222222, 0xFF222222, 0xFF131313 }; - - for (i32 i = 0; i < 3; ++i){ - i32 shift = shifts[i]; - u32 fore = fore_colors[i]; - u32 back = back_colors[i]; - - ++sub_id; - channel = (packed_color >> shift) & 0xFF; - if (do_channel_field(sub_id, ui, &channel, CF_DEC, - (i32)y, fore, back, dec_x0, dec_x1)) - super_color_post_byte(&ui->color, i, channel); - - ++sub_id; - channel = (packed_color >> shift) & 0xFF; - if (do_channel_field(sub_id, ui, &channel, CF_HEX, - (i32)y, fore, back, hex_x0, hex_x1)) - super_color_post_byte(&ui->color, i, channel); - - ++sub_id; - slider.y0 = y; - slider.y1 = slider.y0 + v_half_space; - if (bar_width > 45.f) - do_single_slider(sub_id, ui, i, 1, 10, 100.f, slider, v_handle, rect); - y += v_full_space; - } +gui_begin_top_level(GUI_Target *target){ + target->show_file = 0; } -struct Blob_Layout{ +internal void +gui_end_top_level(GUI_Target *target){ + gui_push_simple_command(target, guicom_null); +} + +internal void +gui_begin_overlap(GUI_Target *target){ + gui_push_simple_command(target, guicom_begin_overlap); +} + +internal void +gui_end_overlap(GUI_Target *target){ + gui_push_simple_command(target, guicom_end_overlap); +} + +internal void +gui_begin_serial_section(GUI_Target *target){ + gui_push_simple_command(target, guicom_begin_serial); +} + +internal void +gui_end_serial_section(GUI_Target *target){ + gui_push_simple_command(target, guicom_end_serial); +} + +internal void +gui_do_top_bar(GUI_Target *target){ + gui_push_simple_command(target, guicom_top_bar); +} + +internal void +gui_do_file(GUI_Target *target){ + gui_push_simple_command(target, guicom_file); + target->show_file = 1; +} + + +struct GUI_Section{ + b32 overlapped; + i32 v; + i32 max_v; +}; + +struct GUI_Session{ + i32_Rect full_rect; i32_Rect rect; - i32 x, y; - i32 size, space; + i32_Rect clip_rect; + + i32 line_height; + + GUI_Section sections[64]; + i32 t; }; internal void -begin_layout(Blob_Layout *layout, i32_Rect rect){ - layout->rect = rect; - layout->x = rect.x0 + 10; - layout->y = rect.y0; - layout->size = 20; - layout->space = 5; +gui_session_init(GUI_Session *session, i32_Rect full_rect, i32 line_height){ + GUI_Section *section; + + *session = {0}; + session->full_rect = full_rect; + session->line_height = line_height; + + section = &session->sections[0]; + section->v = full_rect.y0; + section->max_v = full_rect.y0; } internal void -do_blob(Color_UI *ui, Blob_Layout *layout, u32 color, bool32 *set_me, i32 sub_id){ - i32_Rect rect = layout->rect; - f32_Rect blob; - blob.x0 = (real32)layout->x; - blob.y0 = (real32)layout->y; - blob.x1 = blob.x0 + layout->size; - blob.y1 = blob.y0 + layout->size; - - layout->y += layout->size + layout->space; - if (layout->y + layout->size + layout->space*2 > rect.y1){ - layout->y = rect.y0; - layout->x += layout->size + layout->space; - } - - if (ui->state->input_stage){ - bool32 right = 0; - if (ui_do_button_input(ui->state, i32R(blob), make_sub1(ui->state, sub_id), 0, &right)){ - super_color_post_packed(&ui->color, color); - } - else if (right) *set_me = 1; - } - else{ - Render_Target *target = ui->state->target; - draw_rectangle(target, blob, color); - persist u32 silver = 0xFFa0a0a0; - draw_rectangle_outline(target, blob, silver); - } -} - -inline void -do_blob(Color_UI *ui, Blob_Layout *layout, u32 *color, bool32 *set_me){ - i32 sub_id = (i32)((char*)color - (char*)ui->state->style); - do_blob(ui, layout, *color, set_me, sub_id); -} - -internal void -do_v_divide(Color_UI *ui, Blob_Layout *layout, i32 width){ - i32_Rect rect = layout->rect; - if (layout->y > rect.y0){ - layout->x += layout->size + layout->space; - } - layout->x += width; - layout->y = rect.y0; -} - -internal void -do_palette(Color_UI *ui, i32_Rect rect){ - Style *style = ui->state->style; - Blob_Layout layout; - begin_layout(&layout, rect); - bool32 set_me; - - do_blob(ui, &layout, &style->main.back_color, &set_me); - do_blob(ui, &layout, &style->main.margin_color, &set_me); - do_blob(ui, &layout, &style->main.margin_active_color, &set_me); - - do_blob(ui, &layout, &style->main.cursor_color, &set_me); - do_blob(ui, &layout, &style->main.at_cursor_color, &set_me); - do_blob(ui, &layout, &style->main.mark_color, &set_me); - - do_blob(ui, &layout, &style->main.highlight_color, &set_me); - do_blob(ui, &layout, &style->main.at_highlight_color, &set_me); - - do_blob(ui, &layout, &style->main.default_color, &set_me); - do_blob(ui, &layout, &style->main.comment_color, &set_me); - do_blob(ui, &layout, &style->main.keyword_color, &set_me); - do_blob(ui, &layout, &style->main.str_constant_color, &set_me); - do_blob(ui, &layout, &style->main.char_constant_color, &set_me); - do_blob(ui, &layout, &style->main.int_constant_color, &set_me); - do_blob(ui, &layout, &style->main.float_constant_color, &set_me); - do_blob(ui, &layout, &style->main.bool_constant_color, &set_me); - do_blob(ui, &layout, &style->main.include_color, &set_me); - do_blob(ui, &layout, &style->main.preproc_color, &set_me); - do_blob(ui, &layout, &style->main.special_character_color, &set_me); - - do_blob(ui, &layout, &style->main.highlight_junk_color, &set_me); - do_blob(ui, &layout, &style->main.highlight_white_color, &set_me); - - do_blob(ui, &layout, &style->main.paste_color, &set_me); - - do_blob(ui, &layout, &style->main.file_info_style.bar_color, &set_me); - do_blob(ui, &layout, &style->main.file_info_style.base_color, &set_me); - do_blob(ui, &layout, &style->main.file_info_style.pop1_color, &set_me); - do_blob(ui, &layout, &style->main.file_info_style.pop2_color, &set_me); - - do_v_divide(ui, &layout, 20); - - if (!ui->state->input_stage){ - Render_Target *target = ui->state->target; - draw_string(target, ui->state->font_id, "Global Palette: right click to save color", - layout.x, layout.rect.y0, style->main.default_color); - } - - layout.rect.y0 += layout.size + layout.space; - layout.y = layout.rect.y0; - i32 palette_size = ui->palette_size + 1000; - u32 *color = ui->palette; - for (i32 i = 1000; i < palette_size; ++i, ++color){ - set_me = 0; - do_blob(ui, &layout, *color, &set_me, i); - if (set_me){ - *color = *ui->color.out; - ui->state->redraw = 1; - } - } -} - -internal void -do_sub_button(i32 id, Color_UI *ui, char *text){ - i16 font_id = ui->state->font_id; - i32 line_height = get_font_info(ui->state->font_set, font_id)->height; - i32_Rect rect = layout_rect(ui->layout, line_height + 2); - - if (ui->state->input_stage){ - ui_do_button_input(ui->state, rect, make_sub0(ui->state, id), 1); - } - else{ - Render_Target *target = ui->state->target; - - u32 back_color, text_color; - text_color = 0xFFDDDDDD; - if (ui->state->selected.sub_id0 == id){ - back_color = 0xFF444444; - } - else if (ui->state->hover.sub_id0 == id){ - back_color = 0xFF222222; - } - else{ - back_color = 0xFF111111; - } - - draw_rectangle(target, rect, back_color); - draw_string(target, font_id, text, rect.x0, rect.y0 + 1, - text_color); - } -} - -internal void -do_color_adjuster(Color_UI *ui, u32 *color, - u32 text_color, u32 back_color, char *name){ - i32 id = raw_ptr_dif(color, ui->state->style); - i16 font_id = ui->state->font_id; - i32 character_h = get_font_info(ui->state->font_set, font_id)->height; - u32 text = 0, back = 0; - - i32_Rect bar = layout_rect(ui->layout, character_h); - - if (ui->state->input_stage){ - if (ui_do_button_input(ui->state, bar, make_id(ui->state, id), 1)){ - ui->has_hover_color = 1; - ui->hover_color = super_color_create(*color); - } - } - - else{ - Render_Target *target = ui->state->target; - u32 text_hover = 0xFF101010; - u32 back_hover = 0xFF999999; - if (ui->state->selected.id != id && ui->state->hover.id == id){ - text = text_hover; - back = back_hover; - } - else{ - text = text_color; - back = back_color; - } - - draw_rectangle(target, bar, back); - i32 end_pos = draw_string(target, font_id, name, bar.x0, bar.y0, text); - - real32 x_spacing = ui->hex_advance; - i32_Rect temp_rect = bar; - temp_rect.x0 = temp_rect.x1 - CEIL32(x_spacing * 9.f); - if (temp_rect.x0 >= end_pos + x_spacing){ - u32 n = *color; - char full_hex_string[] = "0x000000"; - for (i32 i = 7; i >= 2; --i){ - i32 m = (n & 0xF); - n >>= 4; - full_hex_string[i] = int_to_hexchar(m); - } - draw_string_mono(target, font_id, full_hex_string, - (f32)temp_rect.x0, (f32)bar.y0, - x_spacing, text); - } - - for (i32 i = 0; i < ArrayCount(ui->highlight.ids); ++i){ - if (ui->highlight.ids[i] == id){ - draw_rectangle_outline(target, f32R(bar), text_color); - break; - } - } - } - - if (ui->state->selected.id == id){ - Render_Target *target = ui->state->target; - i32_Rect expanded = layout_rect(ui->layout, 115 + (character_h + 2)); - UI_Layout_Restore restore = begin_sub_layout(ui->layout, expanded); - - ui->color.out = color; - - if (ui->state->input_stage){ - if (ui->state->selected.sub_id0 == 0){ - ui->state->selected.sub_id0 = 1; - } - } - else{ - draw_rectangle(target, expanded, 0xff000000); - } - - begin_row(ui->layout, 3); - do_sub_button(1, ui, "HSL"); - do_sub_button(2, ui, "RGB"); - do_sub_button(3, ui, "Palette"); - - i32_Rect sub_rect; - sub_rect = expanded; - sub_rect.y0 += 10 + character_h; - - switch (ui->state->selected.sub_id0){ - case 1: do_hsl_sliders(ui, sub_rect); break; - case 2: do_rgb_sliders(ui, sub_rect); break; - case 3: do_palette(ui, sub_rect); break; - } - - end_sub_layout(restore); - } -} - -internal void -do_style_name(Color_UI *ui){ - i32 id = -3; - - i16 font_id = ui->state->font_id; - i32 line_height = get_font_info(ui->state->font_set, font_id)->height; - - i32_Rect srect = layout_rect(ui->layout, line_height); - - Widget_ID wid = make_id(ui->state, id); - b32 selected = is_selected(ui->state, wid); - - if (ui->state->input_stage){ - if (!selected){ - ui_do_button_input(ui->state, srect, wid, 1); - } - else{ - Style *style = ui->state->style; - if (ui_do_text_field_input(ui->state, &style->name)){ - ui->state->selected = {}; - } - } - } - else{ - Render_Target *target = ui->state->target; - Style *style = ui->state->style; - u32 back, fore_text, fore_label; - if (selected){ - back = 0xFF000000; - fore_label = 0xFF808080; - fore_text = 0xFFFFFFFF; - } - else if (is_hover(ui->state, wid)){ - back = 0xFF999999; - fore_text = fore_label = 0xFF101010; - } - else{ - back = style->main.back_color; - fore_text = fore_label = style->main.default_color; - } - - draw_rectangle(target, srect, back); - i32 x = srect.x0; - x = draw_string(target, font_id, "NAME: ", - x, srect.y0, fore_label); - x = draw_string(target, font_id, style->name.str, - x, srect.y0, fore_text); - } +gui_section_end_item(GUI_Section *section, i32 v){ + if (!section->overlapped){ + section->v = v; + } + section->max_v = v; } internal b32 -do_font_option(Color_UI *ui, i16 font_id){ - b32 result = 0; - Font_Info *info = get_font_info(ui->state->font_set, font_id); +gui_interpret(GUI_Session *session, GUI_Header *h){ + GUI_Section *section = 0; + GUI_Section *new_section = 0; + GUI_Section *prev_section = 0; + GUI_Section *end_section = 0; + b32 give_to_user = 0; + i32_Rect rect = {0}; + i32 y = 0; + i32 end_v = -1; - i32 sub_id = (i32)(i64)(info); - i32_Rect orect = layout_rect(ui->layout, info->height); + Assert(session->t < ArrayCount(session->sections)); + section = session->sections + session->t; + y = section->v; - Widget_ID wid = make_sub0(ui->state, sub_id); - if (ui->state->input_stage){ - if (ui_do_button_input(ui->state, orect, wid, 0)){ - result = 1; - } - } - else{ - Render_Target *target = ui->state->target; - u32 back, fore; - if (is_hover(ui->state, wid)){ - back = 0xFF999999; - fore = 0xFF101010; - } - else{ - back = 0xFF000000; - fore = 0xFFFFFFFF; - } - draw_rectangle(target, orect, back); - i32 x = orect.x0; - x = draw_string(target, font_id, "->", x, orect.y0, fore); - draw_string(target, font_id, info->name.str, x, orect.y0, fore); - } - - return result; -} - -internal void -do_font_switch(Color_UI *ui){ - i32 id = -2; - Render_Target *target = ui->state->target; - Font_Set *font_set = ui->state->font_set; - - i16 font_id = ui->state->font_id; - Font_Info *info = get_font_info(font_set, font_id); - i32 character_h = info->height; - - i32_Rect srect = layout_rect(ui->layout, character_h); - Widget_ID wid = make_id(ui->state, id); - - if (ui->state->input_stage){ - ui_do_button_input(ui->state, srect, wid, 1); - } - else{ - Style *style = ui->state->style; - u32 back, fore; - if (is_hover(ui->state, wid) && !is_selected(ui->state, wid)){ - back = 0xFF999999; - fore = 0xFF101010; - } - else{ - back = style->main.back_color; - fore = style->main.default_color; - } - draw_rectangle(target, srect, back); - i32 x = srect.x0; - x = draw_string(target, font_id, "FONT: ", - x, srect.y0, fore); - x = draw_string(target, font_id, info->name.str, - x, srect.y0, fore); - } - - if (is_selected(ui->state, wid)){ - srect = layout_rect(ui->layout, character_h/2); - if (!ui->state->input_stage) - draw_rectangle(target, srect, 0xFF000000); - - i32 count = font_set->count + 1; - - for (i16 i = 1; i < count; ++i){ - if (i == font_id) continue; - if (do_font_option(ui, i)){ - ui->global_font->font_id = i; - ui->global_font->font_changed = 1; - } - } - - srect = layout_rect(ui->layout, character_h/2); - if (!ui->state->input_stage) - draw_rectangle(target, srect, 0xFF000000); - } -} - -internal b32 -do_style_preview(Library_UI *ui, Style *style, i32 toggle = -1){ - b32 result = 0; - i32 id; - if (style == ui->state->style) id = 2; - else id = raw_ptr_dif(style, ui->styles->styles) + 100; - - i16 font_id = ui->state->font_id; - Font_Info *info = get_font_info(ui->state->font_set, font_id); - - i32_Rect prect = layout_rect(ui->layout, info->height*3 + 6); - - Widget_ID wid = make_id(ui->state, id); - - if (ui->state->input_stage){ - if (ui_do_button_input(ui->state, prect, wid, 0)){ - result = 1; - } - } - else{ - Render_Target *target = ui->state->target; - u32 margin_color = style->main.margin_color; - if (is_hover(ui->state, wid)){ - margin_color = style->main.margin_active_color; - } - - i32_Rect inner; - if (toggle != -1){ - i32_Rect toggle_box = prect; - toggle_box.x1 = toggle_box.x0 + info->height*2 + 6; - prect.x0 = toggle_box.x1; + if (y < session->full_rect.y1){ + switch (h->type){ + case guicom_null: Assert(0); break; - inner = get_inner_rect(toggle_box, 3); - draw_margin(target, toggle_box, inner, margin_color); - draw_rectangle(target, inner, style->main.back_color); + case guicom_begin_overlap: + ++session->t; + Assert(session->t < ArrayCount(session->sections)); + new_section = &session->sections[session->t]; + new_section->overlapped = 1; + new_section->v = y; + break; - i32 d; - d = info->height/2; - i32_Rect b; - b.x0 = (inner.x1 + inner.x0)/2 - d; - b.y0 = (inner.y1 + inner.y0)/2 - d; - b.x1 = b.x0 + info->height; - b.y1 = b.y0 + info->height; - if (toggle) draw_rectangle(target, b, margin_color); - else draw_rectangle_outline(target, b, margin_color); + case guicom_end_overlap: + Assert(session->t > 0); + Assert(section->overlapped); + prev_section = &session->sections[--session->t]; + end_v = section->max_v; + end_section = prev_section; + break; + + case guicom_begin_serial: + ++session->t; + Assert(session->t < ArrayCount(session->sections)); + new_section = &session->sections[session->t]; + new_section->overlapped = 0; + new_section->v = y; + break; + + case guicom_end_serial: + Assert(session->t > 0); + Assert(!section->overlapped); + prev_section = &session->sections[--session->t]; + end_v = section->max_v; + end_section = prev_section; + break; + + case guicom_top_bar: + give_to_user = 1; + rect.y0 = y; + rect.y1 = rect.y0 + session->line_height + 2; + rect.x0 = session->full_rect.x0; + rect.x1 = session->full_rect.x1; + end_v = rect.y1; + end_section = section; + break; + + case guicom_file: + give_to_user = 1; + rect.y0 = y; + rect.y1 = session->full_rect.y1; + rect.x0 = session->full_rect.x0; + rect.x1 = session->full_rect.x1; + end_v = rect.y1; + end_section = section; + break; } - inner = get_inner_rect(prect, 3); - draw_margin(target, prect, inner, margin_color); - draw_rectangle(target, inner, style->main.back_color); + if (give_to_user){ + GUI_Section *section = session->sections; + i32 max_v = 0; + i32 i = 0; + + for (i = 0; i <= session->t; ++i, ++section){ + if (section->overlapped){ + max_v = Max(max_v, section->max_v); + } + } + + session->rect = rect; + + if (rect.y0 < max_v){ + rect.y0 = max_v; + } + + session->clip_rect = rect; + } - i32 text_y = inner.y0; - i32 text_x = inner.x0; - text_x = draw_string(target, font_id, style->name.str, - text_x, text_y, style->main.default_color); - i32 font_x = (i32)(inner.x1 - font_string_width(target, font_id, info->name.str)); - if (font_x > text_x + 10) - draw_string(target, font_id, info->name.str, - font_x, text_y, style->main.default_color); - - text_x = inner.x0; - text_y += info->height; - text_x = draw_string(target, font_id, "if ", text_x, text_y, - style->main.keyword_color); - text_x = draw_string(target, font_id, "(x < ", text_x, text_y, - style->main.default_color); - text_x = draw_string(target, font_id, "0", text_x, text_y, - style->main.int_constant_color); - text_x = draw_string(target, font_id, ") { x = ", text_x, text_y, - style->main.default_color); - text_x = draw_string(target, font_id, "0", text_x, text_y, - style->main.int_constant_color); - text_x = draw_string(target, font_id, "; } ", text_x, text_y, - style->main.default_color); - text_x = draw_string(target, font_id, "// comment", text_x, text_y, - style->main.comment_color); - - text_x = inner.x0; - text_y += info->height; - text_x = draw_string(target, font_id, "[] () {}; * -> +-/ <>= ! && || % ^", - text_x, text_y, style->main.default_color); + if (end_section){ + gui_section_end_item(end_section, end_v); + } } - - ui->layout->y = prect.y1; - return result; + + return(give_to_user); } + // BOTTOM diff --git a/4ed_gui_old.cpp b/4ed_gui_old.cpp new file mode 100644 index 00000000..9b223f0b --- /dev/null +++ b/4ed_gui_old.cpp @@ -0,0 +1,2166 @@ +/* + * Mr. 4th Dimention - Allen Webster + * + * 20.02.2016 + * + * GUI system for 4coder + * + */ + +// TOP + +struct Single_Line_Input_Step{ + b8 hit_newline; + b8 hit_ctrl_newline; + b8 hit_a_character; + b8 hit_backspace; + b8 hit_esc; + b8 made_a_change; + b8 did_command; + b8 no_file_match; +}; + +enum Single_Line_Input_Type{ + SINGLE_LINE_STRING, + SINGLE_LINE_FILE +}; + +struct Single_Line_Mode{ + Single_Line_Input_Type type; + String *string; + Hot_Directory *hot_directory; + b32 fast_folder_select; + b32 try_to_match; + b32 case_sensitive; +}; + +internal Single_Line_Input_Step +app_single_line_input_core(System_Functions *system, Working_Set *working_set, + Key_Event_Data key, Single_Line_Mode mode){ + Single_Line_Input_Step result = {}; + + if (key.keycode == key_back){ + result.hit_backspace = 1; + if (mode.string->size > 0){ + result.made_a_change = 1; + --mode.string->size; + switch (mode.type){ + case SINGLE_LINE_STRING: + mode.string->str[mode.string->size] = 0; break; + + case SINGLE_LINE_FILE: + { + char end_character = mode.string->str[mode.string->size]; + if (char_is_slash(end_character)){ + mode.string->size = reverse_seek_slash(*mode.string) + 1; + mode.string->str[mode.string->size] = 0; + hot_directory_set(system, mode.hot_directory, *mode.string, working_set); + } + else{ + mode.string->str[mode.string->size] = 0; + } + }break; + } + } + } + + else if (key.character == '\n' || key.character == '\t'){ + result.made_a_change = 1; + if (key.modifiers[MDFR_CONTROL_INDEX] || + key.modifiers[MDFR_ALT_INDEX]){ + result.hit_ctrl_newline = 1; + } + else{ + result.hit_newline = 1; + if (mode.fast_folder_select){ + Hot_Directory_Match match; + char front_name_space[256]; + String front_name = make_fixed_width_string(front_name_space); + get_front_of_directory(&front_name, *mode.string); + + match = + hot_directory_first_match(mode.hot_directory, front_name, 1, 1, mode.case_sensitive); + + if (mode.try_to_match && !match.filename.str){ + match = hot_directory_first_match(mode.hot_directory, front_name, 1, 0, mode.case_sensitive); + } + if (match.filename.str){ + if (match.is_folder){ + set_last_folder(mode.string, match.filename, mode.hot_directory->slash); + hot_directory_set(system, mode.hot_directory, *mode.string, working_set); + result.hit_newline = 0; + } + else{ + if (mode.try_to_match){ + mode.string->size = reverse_seek_slash(*mode.string) + 1; + append(mode.string, match.filename); + } + } + } + else{ + if (mode.try_to_match){ + result.no_file_match = 1; + } + } + } + } + } + + else if (key.keycode == key_esc){ + result.hit_esc = 1; + result.made_a_change = 1; + } + + else if (key.character){ + result.hit_a_character = 1; + if (!key.modifiers[MDFR_CONTROL_INDEX] && + !key.modifiers[MDFR_ALT_INDEX]){ + if (mode.string->size+1 < mode.string->memory_size){ + u8 new_character = (u8)key.character; + mode.string->str[mode.string->size] = new_character; + mode.string->size++; + mode.string->str[mode.string->size] = 0; + if (mode.type == SINGLE_LINE_FILE && char_is_slash(new_character)){ + hot_directory_set(system, mode.hot_directory, *mode.string, working_set); + } + result.made_a_change = 1; + } + } + else{ + result.did_command = 1; + result.made_a_change = 1; + } + } + + return result; +} + +inline Single_Line_Input_Step +app_single_line_input_step(System_Functions *system, Key_Event_Data key, String *string){ + Single_Line_Mode mode = {}; + mode.type = SINGLE_LINE_STRING; + mode.string = string; + return app_single_line_input_core(system, 0, key, mode); +} + +inline Single_Line_Input_Step +app_single_file_input_step(System_Functions *system, + Working_Set *working_set, Key_Event_Data key, + String *string, Hot_Directory *hot_directory, + b32 fast_folder_select, b32 try_to_match, b32 case_sensitive){ + Single_Line_Mode mode = {}; + mode.type = SINGLE_LINE_FILE; + mode.string = string; + mode.hot_directory = hot_directory; + mode.fast_folder_select = fast_folder_select; + mode.try_to_match = try_to_match; + mode.case_sensitive = case_sensitive; + return app_single_line_input_core(system, working_set, key, mode); +} + +inline Single_Line_Input_Step +app_single_number_input_step(System_Functions *system, Key_Event_Data key, String *string){ + Single_Line_Input_Step result = {}; + Single_Line_Mode mode = {}; + mode.type = SINGLE_LINE_STRING; + mode.string = string; + + char c = (char)key.character; + if (c == 0 || c == '\n' || char_is_numeric(c)) + result = app_single_line_input_core(system, 0, key, mode); + return result; +} + +struct Widget_ID{ + i32 id; + i32 sub_id0; + i32 sub_id1; + i32 sub_id2; +}; + +inline b32 +widget_match(Widget_ID s1, Widget_ID s2){ + return (s1.id == s2.id && s1.sub_id0 == s2.sub_id0 && + s1.sub_id1 == s2.sub_id1 && s1.sub_id2 == s2.sub_id2); +} + +struct UI_State{ + Render_Target *target; + Style *style; + Font_Set *font_set; + Mouse_State *mouse; + Key_Summary *keys; + Working_Set *working_set; + i16 font_id; + + Widget_ID selected, hover, hot; + b32 activate_me; + b32 redraw; + b32 input_stage; + i32 sub_id1_change; + + f32 height, view_y; +}; + +inline Widget_ID +make_id(UI_State *state, i32 id){ + Widget_ID r = state->selected; + r.id = id; + return r; +} + +inline Widget_ID +make_sub0(UI_State *state, i32 id){ + Widget_ID r = state->selected; + r.sub_id0 = id; + return r; +} + +inline Widget_ID +make_sub1(UI_State *state, i32 id){ + Widget_ID r = state->selected; + r.sub_id1 = id; + return r; +} + +inline Widget_ID +make_sub2(UI_State *state, i32 id){ + Widget_ID r = state->selected; + r.sub_id2 = id; + return r; +} + +inline b32 +is_selected(UI_State *state, Widget_ID id){ + return widget_match(state->selected, id); +} + +inline b32 +is_hot(UI_State *state, Widget_ID id){ + return widget_match(state->hot, id); +} + +inline b32 +is_hover(UI_State *state, Widget_ID id){ + return widget_match(state->hover, id); +} + +struct UI_Layout{ + i32 row_count; + i32 row_item_width; + i32 row_max_item_height; + + i32_Rect rect; + i32 x, y; +}; + +struct UI_Layout_Restore{ + UI_Layout layout; + UI_Layout *dest; +}; + +inline void +begin_layout(UI_Layout *layout, i32_Rect rect){ + layout->rect = rect; + layout->x = rect.x0; + layout->y = rect.y0; + layout->row_count = 0; + layout->row_max_item_height = 0; +} + +inline void +begin_row(UI_Layout *layout, i32 count){ + layout->row_count = count; + layout->row_item_width = (layout->rect.x1 - layout->x) / count; +} + +inline i32_Rect +layout_rect(UI_Layout *layout, i32 height){ + i32_Rect rect; + rect.x0 = layout->x; + rect.y0 = layout->y; + rect.x1 = rect.x0; + rect.y1 = rect.y0 + height; + if (layout->row_count > 0){ + --layout->row_count; + rect.x1 = rect.x0 + layout->row_item_width; + layout->x += layout->row_item_width; + layout->row_max_item_height = Max(height, layout->row_max_item_height); + } + if (layout->row_count == 0){ + rect.x1 = layout->rect.x1; + layout->row_max_item_height = Max(height, layout->row_max_item_height); + layout->y += layout->row_max_item_height; + layout->x = layout->rect.x0; + layout->row_max_item_height = 0; + } + return rect; +} + +inline UI_Layout_Restore +begin_sub_layout(UI_Layout *layout, i32_Rect area){ + UI_Layout_Restore restore; + restore.layout = *layout; + restore.dest = layout; + begin_layout(layout, area); + return restore; +} + +inline void +end_sub_layout(UI_Layout_Restore restore){ + *restore.dest = restore.layout; +} + +struct UI_Style{ + u32 dark, dim, bright; + u32 base, pop1, pop2; +}; + +internal UI_Style +get_ui_style(Style *style){ + UI_Style ui_style; + ui_style.dark = style->main.back_color; + ui_style.dim = style->main.margin_color; + ui_style.bright = style->main.margin_active_color; + ui_style.base = style->main.default_color; + ui_style.pop1 = style->main.file_info_style.pop1_color; + ui_style.pop2 = style->main.file_info_style.pop2_color; + return ui_style; +} + +internal UI_Style +get_ui_style_upper(Style *style){ + UI_Style ui_style; + ui_style.dark = style->main.margin_color; + ui_style.dim = style->main.margin_hover_color; + ui_style.bright = style->main.margin_active_color; + ui_style.base = style->main.default_color; + ui_style.pop1 = style->main.file_info_style.pop1_color; + ui_style.pop2 = style->main.file_info_style.pop2_color; + return ui_style; +} + +inline void +get_colors(UI_State *state, u32 *back, u32 *fore, Widget_ID wid, UI_Style style){ + bool32 hover = is_hover(state, wid); + bool32 hot = is_hot(state, wid); + i32 level = hot + hover; + switch (level){ + case 2: + *back = style.bright; + *fore = style.dark; + break; + case 1: + *back = style.dim; + *fore = style.bright; + break; + case 0: + *back = style.dark; + *fore = style.bright; + break; + } +} + +inline void +get_pop_color(UI_State *state, u32 *pop, Widget_ID wid, UI_Style style){ + bool32 hover = is_hover(state, wid); + bool32 hot = is_hot(state, wid); + i32 level = hot + hover; + switch (level){ + case 2: + *pop = style.pop1; + break; + case 1: + *pop = style.pop1; + break; + case 0: + *pop = style.pop1; + break; + } +} + +internal UI_State +ui_state_init(UI_State *state_in, Render_Target *target, Input_Summary *user_input, + Style *style, i16 font_id, Font_Set *font_set, Working_Set *working_set, + b32 input_stage){ + UI_State state = {}; + state.target = target; + state.style = style; + state.font_set = font_set; + state.font_id = font_id; + state.working_set = working_set; + if (user_input){ + state.mouse = &user_input->mouse; + state.keys = &user_input->keys; + } + state.selected = state_in->selected; + state.hot = state_in->hot; + if (input_stage) state.hover = {}; + else state.hover = state_in->hover; + state.redraw = 0; + state.activate_me = 0; + state.input_stage = input_stage; + state.height = state_in->height; + state.view_y = state_in->view_y; + return state; +} + +inline b32 +ui_state_match(UI_State a, UI_State b){ + return (widget_match(a.selected, b.selected) && + widget_match(a.hot, b.hot) && + widget_match(a.hover, b.hover)); +} + +internal b32 +ui_finish_frame(UI_State *persist_state, UI_State *state, UI_Layout *layout, i32_Rect rect, + b32 do_wheel, b32 *did_activation){ + b32 result = 0; + f32 h = layout->y + persist_state->view_y - rect.y0; + f32 max_y = h - (rect.y1 - rect.y0); + + persist_state->height = h; + persist_state->view_y = state->view_y; + + if (state->input_stage){ + Mouse_State *mouse = state->mouse; + Font_Set *font_set = state->font_set; + + if (mouse->wheel != 0 && do_wheel){ + i32 height = get_font_info(font_set, state->font_id)->height; + persist_state->view_y += mouse->wheel*height; + result = 1; + } + if (mouse->release_l && widget_match(state->hot, state->hover)){ + if (did_activation) *did_activation = 1; + if (state->activate_me){ + state->selected = state->hot; + } + } + if (!mouse->l && !mouse->r){ + state->hot = {}; + } + + if (!ui_state_match(*persist_state, *state) || state->redraw){ + result = 1; + } + + *persist_state = *state; + } + + if (persist_state->view_y >= max_y) persist_state->view_y = max_y; + if (persist_state->view_y < 0) persist_state->view_y = 0; + + return result; +} + +internal b32 +ui_do_button_input(UI_State *state, i32_Rect rect, Widget_ID id, bool32 activate, bool32 *right = 0){ + b32 result = 0; + Mouse_State *mouse = state->mouse; + b32 hover = hit_check(mouse->x, mouse->y, rect); + if (hover){ + state->hover = id; + if (activate) state->activate_me = 1; + if (mouse->press_l || (mouse->press_r && right)) state->hot = id; + if (mouse->l && mouse->r) state->hot = {}; + } + bool32 is_match = is_hot(state, id); + if (mouse->release_l && is_match){ + if (hover) result = 1; + state->redraw = 1; + } + if (right && mouse->release_r && is_match){ + if (hover) *right = 1; + state->redraw = 1; + } + return result; +} + +internal bool32 +ui_do_subdivided_button_input(UI_State *state, i32_Rect rect, i32 parts, Widget_ID id, bool32 activate, i32 *indx_out, bool32 *right = 0){ + bool32 result = 0; + real32 x0, x1; + i32_Rect sub_rect; + Widget_ID sub_widg = id; + real32 sub_width = (rect.x1 - rect.x0) / (real32)parts; + sub_rect.y0 = rect.y0; + sub_rect.y1 = rect.y1; + x1 = (real32)rect.x0; + + for (i32 i = 0; i < parts; ++i){ + x0 = x1; + x1 = x1 + sub_width; + sub_rect.x0 = TRUNC32(x0); + sub_rect.x1 = TRUNC32(x1); + sub_widg.sub_id2 = i; + if (ui_do_button_input(state, sub_rect, sub_widg, activate, right)){ + *indx_out = i; + break; + } + } + + return result; +} + +internal real32 +ui_do_vscroll_input(UI_State *state, i32_Rect top, i32_Rect bottom, i32_Rect slider, + Widget_ID id, real32 val, real32 step_amount, + real32 smin, real32 smax, real32 vmin, real32 vmax){ + Mouse_State *mouse = state->mouse; + i32 mx = mouse->x; + i32 my = mouse->y; + if (hit_check(mx, my, top)){ + state->hover = id; + state->hover.sub_id2 = 1; + } + if (hit_check(mx, my, bottom)){ + state->hover = id; + state->hover.sub_id2 = 2; + } + if (hit_check(mx, my, slider)){ + state->hover = id; + state->hover.sub_id2 = 3; + } + if (mouse->press_l) state->hot = state->hover; + if (id.id == state->hot.id){ + if (mouse->release_l){ + Widget_ID wid1, wid2; + wid1 = wid2 = id; + wid1.sub_id2 = 1; + wid2.sub_id2 = 2; + if (state->hot.sub_id2 == 1 && is_hover(state, wid1)) val -= step_amount; + if (state->hot.sub_id2 == 2 && is_hover(state, wid2)) val += step_amount; + state->redraw = 1; + } + if (state->hot.sub_id2 == 3){ + f32 S, L; + S = (f32)mouse->y - (slider.y1 - slider.y0) / 2; + if (S < smin) S = smin; + if (S > smax) S = smax; + L = unlerp(smin, S, smax); + val = lerp(vmin, L, vmax); + state->redraw = 1; + } + } + return val; +} + +internal b32 +ui_do_text_field_input(UI_State *state, String *str){ + b32 result = 0; + Key_Summary *keys = state->keys; + for (i32 key_i = 0; key_i < keys->count; ++key_i){ + Key_Event_Data key = get_single_key(keys, key_i); + char c = (char)key.character; + if (char_is_basic(c) && str->size < str->memory_size-1){ + str->str[str->size++] = c; + str->str[str->size] = 0; + } + else if (c == '\n'){ + result = 1; + } + else if (key.keycode == key_back && str->size > 0){ + str->str[--str->size] = 0; + } + } + return result; +} + +internal b32 +ui_do_file_field_input(System_Functions *system, UI_State *state, + Hot_Directory *hot_dir, b32 try_to_match, b32 case_sensitive){ + Key_Event_Data key; + Single_Line_Input_Step step; + String *str = &hot_dir->string; + Key_Summary *keys = state->keys; + i32 key_i; + b32 result = 0; + + terminate_with_null(str); + + for (key_i = 0; key_i < keys->count; ++key_i){ + key = get_single_key(keys, key_i); + step = + app_single_file_input_step(system, state->working_set, key, str, + hot_dir, 1, try_to_match, case_sensitive); + if ((step.hit_newline || step.hit_ctrl_newline) && !step.no_file_match) result = 1; + } + return result; +} + +internal b32 +ui_do_line_field_input(System_Functions *system, + UI_State *state, String *string){ + b32 result = 0; + Key_Summary *keys = state->keys; + for (i32 key_i = 0; key_i < keys->count; ++key_i){ + Key_Event_Data key = get_single_key(keys, key_i); + terminate_with_null(string); + Single_Line_Input_Step step = + app_single_line_input_step(system, key, string); + if (step.hit_newline || step.hit_ctrl_newline) result = 1; + } + return result; +} + +internal b32 +ui_do_slider_input(UI_State *state, i32_Rect rect, Widget_ID wid, + real32 min, real32 max, real32 *v){ + b32 result = 0; + ui_do_button_input(state, rect, wid, 0); + Mouse_State *mouse = state->mouse; + if (is_hot(state, wid)){ + result = 1; + *v = unlerp(min, (f32)mouse->x, max); + state->redraw = 1; + } + return result; +} + +internal bool32 +do_text_field(Widget_ID wid, UI_State *state, UI_Layout *layout, String prompt, String dest){ + b32 result = 0; + i32 character_h = get_font_info(state->font_set, state->font_id)->height; + + i32_Rect rect = layout_rect(layout, character_h); + + if (state->input_stage){ + ui_do_button_input(state, rect, wid, 1); + if (is_selected(state, wid)){ + if (ui_do_text_field_input(state, &dest)){ + result = 1; + } + } + } + else{ + Render_Target *target = state->target; + UI_Style ui_style = get_ui_style_upper(state->style); + u32 back, fore, prompt_pop; + get_colors(state, &back, &fore, wid, ui_style); + get_pop_color(state, &prompt_pop, wid, ui_style); + + draw_rectangle(target, rect, back); + + i32 x = draw_string(target, state->font_id, prompt, rect.x0, rect.y0 + 1, prompt_pop); + draw_string(target, state->font_id, dest, x, rect.y0 + 1, ui_style.base); + } + + return result; +} + +internal b32 +do_button(i32 id, UI_State *state, UI_Layout *layout, char *text, i32 height_mult, + b32 is_toggle = 0, b32 on = 0){ + b32 result = 0; + i16 font_id = state->font_id; + i32 character_h = get_font_info(state->font_set, font_id)->height; + + i32_Rect btn_rect = layout_rect(layout, character_h * height_mult); + if (height_mult > 1) btn_rect = get_inner_rect(btn_rect, 2); + else{ + btn_rect.x0 += 2; + btn_rect.x1 -= 2; + } + + Widget_ID wid = make_id(state, id); + + if (state->input_stage){ + if (ui_do_button_input(state, btn_rect, wid, 0)){ + result = 1; + } + } + else{ + Render_Target *target = state->target; + UI_Style ui_style = get_ui_style(state->style); + u32 back, fore, outline; + outline = ui_style.bright; + get_colors(state, &back, &fore, wid, ui_style); + + draw_rectangle(target, btn_rect, back); + draw_rectangle_outline(target, btn_rect, outline); + real32 text_width = font_string_width(target, font_id, text); + i32 box_width = btn_rect.x1 - btn_rect.x0; + i32 box_height = btn_rect.y1 - btn_rect.y0; + i32 x_pos = TRUNC32(btn_rect.x0 + (box_width - text_width)*.5f); + draw_string(target, font_id, text, x_pos, btn_rect.y0 + (box_height - character_h) / 2, fore); + + if (is_toggle){ + i32_Rect on_box = get_inner_rect(btn_rect, character_h/2); + on_box.x1 = on_box.x0 + (on_box.y1 - on_box.y0); + + if (on) draw_rectangle(target, on_box, fore); + else draw_rectangle(target, on_box, back); + draw_rectangle_outline(target, on_box, fore); + } + } + + return result; +} + +internal b32 +do_undo_slider(Widget_ID wid, UI_State *state, UI_Layout *layout, i32 max, i32 v, Undo_Data *undo, i32 *out){ + b32 result = 0; + i16 font_id = state->font_id; + i32 character_h = get_font_info(state->font_set, font_id)->height; + + i32_Rect containing_rect = layout_rect(layout, character_h); + + i32_Rect click_rect; + click_rect.x0 = containing_rect.x0 + character_h - 1; + click_rect.x1 = containing_rect.x1 - character_h + 1; + click_rect.y0 = containing_rect.y0 + 2; + click_rect.y1 = containing_rect.y1 - 2; + + if (state->input_stage){ + real32 l; + if (ui_do_slider_input(state, click_rect, wid, (real32)click_rect.x0, (real32)click_rect.x1, &l)){ + real32 v_new = lerp(0.f, l, (real32)max); + v = ROUND32(v_new); + result = 1; + if (out) *out = v; + } + } + else{ + Render_Target *target = state->target; + if (max > 0){ + UI_Style ui_style = get_ui_style_upper(state->style); + + real32 L = unlerp(0.f, (real32)v, (real32)max); + i32 x = FLOOR32(lerp((real32)click_rect.x0, L, (real32)click_rect.x1)); + + i32 bar_top = ((click_rect.y0 + click_rect.y1) >> 1) - 1; + i32 bar_bottom = bar_top + 2; + + bool32 show_bar = 1; + real32 tick_step = (click_rect.x1 - click_rect.x0) / (real32)max; + bool32 show_ticks = 1; + if (tick_step <= 5.f) show_ticks = 0; + + if (undo == 0){ + if (show_bar){ + i32_Rect slider_rect; + slider_rect.x0 = click_rect.x0; + slider_rect.x1 = x; + slider_rect.y0 = bar_top; + slider_rect.y1 = bar_bottom; + + draw_rectangle(target, slider_rect, ui_style.dim); + + slider_rect.x0 = x; + slider_rect.x1 = click_rect.x1; + draw_rectangle(target, slider_rect, ui_style.pop1); + } + + if (show_ticks){ + f32_Rect tick; + tick.x0 = (real32)click_rect.x0 - 1; + tick.x1 = (real32)click_rect.x0 + 1; + tick.y0 = (real32)bar_top - 3; + tick.y1 = (real32)bar_bottom + 3; + + for (i32 i = 0; i < v; ++i){ + draw_rectangle(target, tick, ui_style.dim); + tick.x0 += tick_step; + tick.x1 += tick_step; + } + + for (i32 i = v; i <= max; ++i){ + draw_rectangle(target, tick, ui_style.pop1); + tick.x0 += tick_step; + tick.x1 += tick_step; + } + } + } + else{ + if (show_bar){ + i32_Rect slider_rect; + slider_rect.x0 = click_rect.x0; + slider_rect.y0 = bar_top; + slider_rect.y1 = bar_bottom; + + Edit_Step *history = undo->history.edits; + i32 block_count = undo->history_block_count; + Edit_Step *step = history; + for (i32 i = 0; i < block_count; ++i){ + u32 color; + if (step->type == ED_REDO || + step->type == ED_UNDO) color = ui_style.pop1; + else color = ui_style.dim; + + real32 L; + if (i + 1 == block_count){ + L = 1.f; + }else{ + step = history + step->next_block; + L = unlerp(0.f, (real32)(step - history), (real32)max); + } + if (L > 1.f) L = 1.f; + i32 x = FLOOR32(lerp((real32)click_rect.x0, L, (real32)click_rect.x1)); + + slider_rect.x1 = x; + draw_rectangle(target, slider_rect, color); + slider_rect.x0 = slider_rect.x1; + + if (L == 1.f) break; + } + } + + if (show_ticks){ + f32_Rect tick; + tick.x0 = (real32)click_rect.x0 - 1; + tick.x1 = (real32)click_rect.x0 + 1; + tick.y0 = (real32)bar_top - 3; + tick.y1 = (real32)bar_bottom + 3; + + Edit_Step *history = undo->history.edits; + u32 color = ui_style.dim; + for (i32 i = 0; i <= max; ++i){ + if (i != max){ + if (history[i].type == ED_REDO) color = ui_style.pop1; + else if (history[i].type == ED_UNDO || + history[i].type == ED_NORMAL) color = ui_style.pop2; + else color = ui_style.dim; + } + draw_rectangle(target, tick, color); + tick.x0 += tick_step; + tick.x1 += tick_step; + } + } + } + + i32_Rect slider_handle; + slider_handle.x0 = x - 2; + slider_handle.x1 = x + 2; + slider_handle.y0 = click_rect.y0; + slider_handle.y1 = click_rect.y1; + + draw_rectangle(target, slider_handle, ui_style.bright); + } + } + + return result; +} + +internal void +do_label(UI_State *state, UI_Layout *layout, char *text, int text_size, f32 height = 2.f){ + Style *style = state->style; + i16 font_id = state->font_id; + i32 line_height = get_font_info(state->font_set, font_id)->height; + i32_Rect label = layout_rect(layout, FLOOR32(line_height * height)); + + if (!state->input_stage){ + Render_Target *target = state->target; + u32 back = style->main.margin_color; + u32 fore = style->main.default_color; + draw_rectangle(target, label, back); + i32 height = label.y1 - label.y0; + + String textstr = make_string(text, text_size); + draw_string(target, font_id, textstr, label.x0, + label.y0 + (height - line_height)/2, fore); + } +} + +inline void +do_label(UI_State *state, UI_Layout *layout, String text, f32 height = 2.f){ + do_label(state, layout, text.str, text.size, height); +} + +internal void +do_scroll_bar(UI_State *state, i32_Rect rect){ + i32 id = 1; + i32 w = (rect.x1 - rect.x0); + i32 h = (rect.y1 - rect.y0); + + i32_Rect top_arrow, bottom_arrow; + top_arrow.x0 = rect.x0; + top_arrow.x1 = rect.x1; + top_arrow.y0 = rect.y0; + top_arrow.y1 = top_arrow.y0 + w; + + bottom_arrow.x0 = rect.x0; + bottom_arrow.x1 = rect.x1; + bottom_arrow.y1 = rect.y1; + bottom_arrow.y0 = bottom_arrow.y1 - w; + + f32 space_h = (f32)(bottom_arrow.y0 - top_arrow.y1); + if (space_h <= w) return; + + i32 slider_h = w; + + f32 view_hmin = 0; + f32 view_hmax = state->height - h; + f32 L = unlerp(view_hmin, state->view_y, view_hmax); + + f32 slider_hmin = (f32)top_arrow.y1; + f32 slider_hmax = (f32)bottom_arrow.y0 - slider_h; + f32 S = lerp(slider_hmin, L, slider_hmax); + + i32_Rect slider; + slider.x0 = rect.x0; + slider.x1 = rect.x1; + slider.y0 = FLOOR32(S); + slider.y1 = slider.y0 + slider_h; + + Widget_ID wid = make_id(state, id); + + if (state->input_stage){ + state->view_y = + ui_do_vscroll_input(state, top_arrow, bottom_arrow, slider, wid, state->view_y, + (f32)(get_font_info(state->font_set, state->font_id)->height), + slider_hmin, slider_hmax, view_hmin, view_hmax); + } + else{ + Render_Target *target = state->target; + + f32 x0, y0, x1, y1, x2, y2; + f32 w_1_2 = w*.5f; + f32 w_1_3 = w*.333333f; + f32 w_2_3 = w*.666667f; + + + UI_Style ui_style = get_ui_style(state->style); + u32 outline, back, fore; + + outline = ui_style.bright; + + wid.sub_id2 = 0; + + x0 = (w_1_2 + top_arrow.x0); + x1 = (w_1_3 + top_arrow.x0); + x2 = (w_2_3 + top_arrow.x0); + + ++wid.sub_id2; + y0 = (w_1_3 + top_arrow.y0); + y1 = (w_2_3 + top_arrow.y0); + y2 = (w_2_3 + top_arrow.y0); + get_colors(state, &back, &fore, wid, ui_style); + draw_rectangle(target, top_arrow, back); + draw_rectangle_outline(target, top_arrow, outline); + + ++wid.sub_id2; + y0 = (w_2_3 + bottom_arrow.y0); + y1 = (w_1_3 + bottom_arrow.y0); + y2 = (w_1_3 + bottom_arrow.y0); + get_colors(state, &back, &fore, wid, ui_style); + draw_rectangle(target, bottom_arrow, back); + draw_rectangle_outline(target, bottom_arrow, outline); + + ++wid.sub_id2; + get_colors(state, &back, &fore, wid, ui_style); + draw_rectangle(target, slider, back); + draw_rectangle_outline(target, slider, outline); + + draw_rectangle_outline(target, rect, outline); + } +} + +internal void +draw_gradient_slider(Render_Target *target, Vec4 base, i32 channel, + i32 steps, f32 top, f32_Rect slider, b32 hsla){ + Vec4 low, high; + f32 *lowv, *highv; + f32 x; + f32 next_x; + f32 x_step; + f32 v_step; + f32 m; + + x = (real32)slider.x0; + x_step = (real32)(slider.x1 - slider.x0) / steps; + v_step = top / steps; + m = 1.f / top; + lowv = &low.v[channel]; + highv = &high.v[channel]; + + if (hsla){ + for (i32 i = 0; i < steps; ++i){ + low = high = base; + *lowv = (i * v_step); + *highv = *lowv + v_step; + *lowv *= m; + *highv *= m; + low = hsla_to_rgba(low); + high = hsla_to_rgba(high); + + next_x = x + x_step; + draw_gradient_2corner_clipped( + target, x, slider.y0, next_x, slider.y1, + low, high); + x = next_x; + } + } + else{ + for (i32 i = 0; i < steps; ++i){ + low = high = base; + *lowv = (i * v_step); + *highv = *lowv + v_step; + *lowv *= m; + *highv *= m; + + next_x = x + x_step; + draw_gradient_2corner_clipped( + target, x, slider.y0, next_x, slider.y1, + low, high); + x = next_x; + } + } +} + +inline void +draw_hsl_slider(Render_Target *target, Vec4 base, i32 channel, + i32 steps, f32 top, f32_Rect slider){ + draw_gradient_slider(target, base, channel, steps, top, slider, 1); +} + +inline void +draw_rgb_slider(Render_Target *target, Vec4 base, i32 channel, + i32 steps, f32 top, f32_Rect slider){ + draw_gradient_slider(target, base, channel, steps, top, slider, 0); +} + +internal b32 +do_main_file_box(System_Functions *system, UI_State *state, UI_Layout *layout, + Hot_Directory *hot_directory, b32 try_to_match, b32 case_sensitive, char *end){ + b32 result = 0; + Style *style = state->style; + String *string = &hot_directory->string; + + i16 font_id = state->font_id; + i32 line_height = get_font_info(state->font_set, font_id)->height; + i32_Rect box = layout_rect(layout, line_height + 2); + + if (state->input_stage){ + if (ui_do_file_field_input(system, state, hot_directory, try_to_match, case_sensitive)){ + result = 1; + } + } + else{ + Render_Target *target = state->target; + u32 back = style->main.margin_color; + u32 fore = style->main.default_color; + u32 special = style->main.special_character_color; + draw_rectangle(target, box, back); + i32 x = box.x0; + x = draw_string(target, font_id, string->str, x, box.y0, fore); + if (end) draw_string(target, font_id, end, x, box.y0, special); + } + + layout->y = box.y1; + return result; +} + +internal b32 +do_main_string_box(System_Functions *system, UI_State *state, UI_Layout *layout, String *string){ + b32 result = 0; + Style *style = state->style; + + i16 font_id = state->font_id; + i32 line_height = get_font_info(state->font_set, font_id)->height; + i32_Rect box = layout_rect(layout, line_height + 2); + + if (state->input_stage){ + if (ui_do_line_field_input(system, state, string)){ + result = 1; + } + } + else{ + Render_Target *target = state->target; + u32 back = style->main.margin_color; + u32 fore = style->main.default_color; + draw_rectangle(target, box, back); + i32 x = box.x0; + x = draw_string(target, font_id, string->str, x, box.y0, fore); + } + + layout->y = box.y1; + return result; +} + +internal b32 +do_list_option(i32 id, UI_State *state, UI_Layout *layout, String text){ + b32 result = 0; + Style *style = state->style; + + i16 font_id = state->font_id; + i32 character_h = get_font_info(state->font_set, font_id)->height; + + i32_Rect box = layout_rect(layout, character_h*2); + Widget_ID wid = make_id(state, id); + + if (state->input_stage){ + if (ui_do_button_input(state, box, wid, 0)){ + result = 1; + } + } + else{ + Render_Target *target = state->target; + i32_Rect inner = get_inner_rect(box, 3); + u32 back, outline, fore, pop; + back = style->main.back_color; + fore = style->main.default_color; + pop = style->main.file_info_style.pop2_color; + if (is_hover(state, wid)) outline = style->main.margin_active_color; + else outline = style->main.margin_color; + + draw_rectangle(target, inner, back); + i32 x = inner.x0, y = box.y0 + character_h/2; + x = draw_string(target, font_id, text, x, y, fore); + draw_margin(target, box, inner, outline); + } + + layout->y = box.y1; + return result; +} + +internal b32 +do_checkbox_list_option(i32 id, UI_State *state, UI_Layout *layout, String text, b32 is_on){ + b32 result = 0; + Style *style = state->style; + + i16 font_id = state->font_id; + i32 character_h = get_font_info(state->font_set, font_id)->height; + + i32_Rect box = layout_rect(layout, character_h*2); + Widget_ID wid = make_id(state, id); + + if (state->input_stage){ + if (ui_do_button_input(state, box, wid, 0)){ + result = 1; + } + } + else{ + Render_Target *target = state->target; + i32_Rect inner = get_inner_rect(box, 3); + u32 back, outline, fore, pop, box_color; + back = style->main.back_color; + fore = style->main.default_color; + pop = style->main.file_info_style.pop2_color; + if (is_hover(state, wid)) outline = style->main.margin_active_color; + else outline = style->main.margin_color; + box_color = style->main.margin_active_color; + + draw_rectangle(target, inner, back); + + i32_Rect square; + square = get_inner_rect(inner, character_h/3); + square.x1 = square.x0 + (square.y1 - square.y0); + if (is_on) draw_rectangle(target, square, box_color); + else draw_margin(target, square, 1, box_color); + + i32 x = square.x1 + 3; + i32 y = box.y0 + character_h/2; + x = draw_string(target, font_id, text, x, y, fore); + draw_margin(target, box, inner, outline); + } + + layout->y = box.y1; + return result; +} + + +internal b32 +do_file_option(i32 id, UI_State *state, UI_Layout *layout, String filename, b32 is_folder, String extra, char slash){ + b32 result = 0; + Style *style = state->style; + i16 font_id = state->font_id; + i32 character_h = get_font_info(state->font_set, font_id)->height; + char slash_buf[2] = { slash, 0 }; + + i32_Rect box = layout_rect(layout, character_h*2); + Widget_ID wid = make_id(state, id); + + if (state->input_stage){ + if (ui_do_button_input(state, box, wid, 0)){ + result = 1; + } + } + else{ + Render_Target *target = state->target; + i32_Rect inner = get_inner_rect(box, 3); + u32 back, outline, fore, pop; + back = style->main.back_color; + fore = style->main.default_color; + pop = style->main.file_info_style.pop2_color; + if (is_hover(state, wid)) outline = style->main.margin_active_color; + else outline = style->main.margin_color; + + draw_rectangle(target, inner, back); + i32 x = inner.x0, y = box.y0 + character_h/2; + x = draw_string(target, font_id, filename, x, y, fore); + if (is_folder) x = draw_string(target, font_id, slash_buf, x, y, fore); + draw_string(target, font_id, extra, x, y, pop); + draw_margin(target, box, inner, outline); + } + + layout->y = box.y1; + return result; +} + +internal b32 +do_file_list_box(System_Functions *system, UI_State *state, UI_Layout *layout, + Hot_Directory *hot_dir, b32 has_filter, b32 try_to_match, b32 case_sensitive, + b32 *new_dir, b32 *selected, char *end){ + b32 result = 0; + File_List *files = &hot_dir->file_list; + + if (do_main_file_box(system, state, layout, hot_dir, try_to_match, case_sensitive, end)){ + *selected = 1; + terminate_with_null(&hot_dir->string); + } + else{ + persist String p4c_extension = make_lit_string("p4c"); + persist String message_loaded = make_lit_string(" LOADED"); + persist String message_unsaved = make_lit_string(" LOADED *"); + persist String message_unsynced = make_lit_string(" LOADED !"); + persist String message_nothing = {}; + + char front_name_space[256]; + String front_name = make_fixed_width_string(front_name_space); + get_front_of_directory(&front_name, hot_dir->string); + + Absolutes absolutes; + get_absolutes(front_name, &absolutes, 1, 1); + + char full_path_[256]; + String full_path = make_fixed_width_string(full_path_); + get_path_of_directory(&full_path, hot_dir->string); + i32 restore_size = full_path.size; + + i32 i; + File_Info *info, *end; + end = files->infos + files->count; + for (info = files->infos, i = 0; info != end; ++info, ++i){ + String filename = info->filename; + + append(&full_path, filename); + terminate_with_null(&full_path); + + Editing_File *file = working_set_contains(system, state->working_set, full_path); + full_path.size = restore_size; + + b8 is_folder = (info->folder != 0); + b8 ext_match = (match(file_extension(filename), p4c_extension) != 0); + b8 name_match = (filename_match(front_name, &absolutes, filename, case_sensitive) != 0); + b8 is_loaded = (file != 0 && file_is_ready(file)); + + String message = message_nothing; + if (is_loaded){ + switch (buffer_get_sync(file)){ + case SYNC_GOOD: message = message_loaded; break; + case SYNC_BEHIND_OS: message = message_unsynced; break; + case SYNC_UNSAVED: message = message_unsaved; break; + } + } + + if ((is_folder || !has_filter || ext_match) && name_match){ + if (do_file_option(100+i, state, layout, filename, is_folder, message, system->slash)){ + result = 1; + hot_directory_clean_end(hot_dir); + append(&hot_dir->string, filename); + if (is_folder){ + *new_dir = 1; + append(&hot_dir->string, system->slash); + } + else{ + *selected = 1; + } + terminate_with_null(&hot_dir->string); + } + } + } + } + + return result; +} + +internal b32 +do_live_file_list_box(System_Functions *system, UI_State *state, UI_Layout *layout, + Working_Set *working_set, String *string, b32 *selected){ + b32 result = 0; + + if (do_main_string_box(system, state, layout, string)){ + *selected = 1; + terminate_with_null(string); + } + else{ + persist String message_unsaved = make_lit_string(" *"); + persist String message_unsynced = make_lit_string(" !"); + persist String message_nothing = {}; + + Absolutes absolutes; + get_absolutes(*string, &absolutes, 1, 1); + + Editing_File *file; + File_Node *node, *used_nodes; + i32 i = 0; + used_nodes = &working_set->used_sentinel; + + for (dll_items(node, used_nodes)){ + file = (Editing_File*)node; + Assert(!file->state.is_dummy); + + String message = message_nothing; + switch (buffer_get_sync(file)){ + case SYNC_BEHIND_OS: message = message_unsynced; break; + case SYNC_UNSAVED: message = message_unsaved; break; + } + + if (filename_match(*string, &absolutes, file->name.live_name, 1)){ + if (do_file_option(100+i, state, layout, file->name.live_name, 0, message, system->slash)){ + result = 1; + *selected = 1; + copy(string, file->name.source_path); + terminate_with_null(string); + } + } + + ++i; + } + } + + return result; +} + +internal Super_Color +super_color_create(u32 packed){ + Super_Color result = {}; + result.rgba = unpack_color4(packed); + result.hsla = rgba_to_hsla(result.rgba); + return result; +} + +internal void +super_color_post_hsla(Super_Color *color, Vec4 hsla){ + color->hsla = hsla; + if (hsla.h == 1.f) + hsla.h = 0.f; + color->rgba = hsla_to_rgba(hsla); + *color->out = pack_color4(color->rgba); +} + +internal void +super_color_post_rgba(Super_Color *color, Vec4 rgba){ + color->rgba = rgba; + color->hsla = rgba_to_hsla(rgba); + *color->out = pack_color4(rgba); +} + +internal void +super_color_post_packed(Super_Color *color, u32 packed){ + color->rgba = unpack_color4(packed); + color->hsla = rgba_to_hsla(color->rgba); + *color->out = packed; +} + +u32 super_color_clear_masks[] = {0xFF00FFFF, 0xFFFF00FF, 0xFFFFFF00}; +u32 super_color_shifts[] = {16, 8, 0}; + +internal u32 +super_color_post_byte(Super_Color *color, i32 channel, u8 byte){ + u32 packed = *color->out; + packed &= super_color_clear_masks[channel]; + packed |= (byte << super_color_shifts[channel]); + super_color_post_packed(color, packed); + return packed; +} + +struct Color_Highlight{ + i32 ids[4]; +}; + +struct Library_UI{ + UI_State *state; + UI_Layout *layout; + + Font_Set *fonts; + + Style_Library *styles; + Hot_Directory *hot_directory; +}; + +struct Color_UI{ + UI_State *state; + UI_Layout *layout; + + Font_Set *fonts; + Style_Font *global_font; + + f32 hex_advance; + u32 *palette; + i32 palette_size; + + Color_Highlight highlight; + Super_Color color; + + b32 has_hover_color; + Super_Color hover_color; +}; + +enum Channel_Field_Type{ + CF_DEC, + CF_HEX +}; + +internal void +do_single_slider(i32 sub_id, Color_UI *ui, i32 channel, b32 is_rgba, + i32 grad_steps, f32 top, f32_Rect slider, f32 v_handle, + i32_Rect rect){ + f32_Rect click_box = slider; + click_box.y0 -= v_handle; + + if (ui->state->input_stage){ + real32 v; + if (ui_do_slider_input(ui->state, i32R(click_box), make_sub1(ui->state, sub_id), slider.x0, slider.x1, &v)){ + Vec4 new_color; + if (is_rgba) new_color = ui->color.rgba; + else new_color = ui->color.hsla; + new_color.v[channel] = clamp(0.f, v, 1.f); + if (is_rgba) super_color_post_rgba(&ui->color, new_color); + else super_color_post_hsla(&ui->color, new_color); + } + } + else{ + Render_Target *target = ui->state->target; + Vec4 color; + real32 x; + if (is_rgba){ + color = ui->color.rgba; + draw_rgb_slider(target, V4(0,0,0,1.f), channel, 10, 100.f, slider); + } + else{ + i32 steps; + real32 top; + if (channel == 0){ + steps = 45; + top = 360.f; + } + else{ + steps = 10; + top = 100.f; + } + color = ui->color.hsla; + draw_hsl_slider(target, color, channel, steps, top, slider); + } + + x = lerp(slider.x0, color.v[channel], slider.x1); + draw_rectangle( + target, f32R(x, slider.y0, x + 1, slider.y1), 0xff000000); + + draw_rectangle( + target, f32R(x-2, click_box.y0, x+3, slider.y0-4), 0xff777777); + } +} + +internal void +do_hsl_sliders(Color_UI *ui, i32_Rect rect){ + real32 bar_width = (real32)(rect.x1 - rect.x0 - 20); + if (bar_width > 45){ + f32_Rect slider; + real32 y; + i32 sub_id; + + real32 v_full_space = 30.f; + real32 v_half_space = 15.f; + real32 v_quarter_space = 12.f; + real32 v_handle = 9.f; + + slider.x0 = rect.x0 + 10.f; + slider.x1 = slider.x0 + bar_width; + + sub_id = 0; + + i32 step_count[] = {45, 10, 10}; + real32 tops[] = {360.f, 100.f, 100.f}; + + y = rect.y0 + v_quarter_space; + for (i32 i = 0; i < 3; ++i){ + ++sub_id; + slider.y0 = y; + slider.y1 = slider.y0 + v_half_space; + do_single_slider(sub_id, ui, i, 0, step_count[i], tops[i], slider, v_handle, rect); + y += v_full_space; + } + } +} + +internal void +fill_buffer_color_channel(char *buffer, u8 x, Channel_Field_Type ftype){ + if (ftype == CF_DEC){ + u8 x0; + x0 = x / 10; + buffer[2] = (x - (10*x0)) + '0'; + x = x0; + x0 = x / 10; + buffer[1] = (x - (10*x0)) + '0'; + x = x0; + x0 = x / 10; + buffer[0] = (x - (10*x0)) + '0'; + } + else{ + u8 n; + n = x & 0xF; + buffer[1] = int_to_hexchar(n); + x >>= 4; + n = x & 0xF; + buffer[0] = int_to_hexchar(n); + } +} + +internal b32 +do_channel_field(i32 sub_id, Color_UI *ui, u8 *channel, Channel_Field_Type ftype, + i32 y, u32 color, u32 back, i32 x0, i32 x1){ + b32 result = 0; + + i16 font_id = ui->state->font_id; + i32 line_height = get_font_info(ui->state->font_set, font_id)->height; + i32_Rect hit_region; + hit_region.x0 = x0; + hit_region.x1 = x1; + hit_region.y0 = y; + hit_region.y1 = y + line_height; + + i32 digit_count; + if (ftype == CF_DEC) digit_count = 3; + else digit_count = 2; + + Render_Target *target = ui->state->target; + + if (ui->state->input_stage){ + i32 indx; + ui_do_subdivided_button_input(ui->state, hit_region, digit_count, + make_sub1(ui->state, sub_id), 1, &indx); + } + else{ + if (ui->state->hover.sub_id1 == sub_id && ui->state->selected.sub_id1 != sub_id){ + draw_rectangle(target, hit_region, back); + } + } + + char string_buffer[4]; + string_buffer[digit_count] = 0; + fill_buffer_color_channel(string_buffer, *channel, ftype); + + if (ui->state->selected.sub_id1 == sub_id){ + i32 indx = ui->state->selected.sub_id2; + if (ui->state->input_stage){ + Key_Summary *keys = ui->state->keys; + for (i32 key_i = 0; key_i < keys->count; ++key_i){ + Key_Event_Data key = get_single_key(keys, key_i); + + if (key.keycode == key_right){ + ++indx; + if (indx > digit_count-1) indx = 0; + } + if (key.keycode == key_left){ + --indx; + if (indx < 0) indx = digit_count-1; + } + + i32 new_value = *channel; + if (key.keycode == key_up || key.keycode == key_down){ + i32 place = digit_count-1-indx; + i32 base = (ftype == CF_DEC)?10:0x10; + i32 step_amount = 1; + while (place > 0){ + step_amount *= base; + --place; + } + if (key.keycode == key_down){ + step_amount = 0 - step_amount; + } + new_value += step_amount; + } + + u8 c = (u8)key.character; + bool32 is_good = (ftype == CF_DEC)?char_is_numeric(c):char_is_hex(c); + if (is_good){ + string_buffer[indx] = c; + if (ftype == CF_DEC) + new_value = str_to_int(make_string(string_buffer, 3)); + else + new_value = hexstr_to_int(make_string(string_buffer, 2)); + ++indx; + if (indx > digit_count-1) indx = 0; + } + + if (c == '\n'){ + switch (sub_id){ + case 1: case 2: + case 4: case 5: + ui->state->sub_id1_change = sub_id + 3; break; + + case 7: case 8: + ui->state->sub_id1_change = sub_id - 6; break; + } + } + + if (new_value != *channel){ + if (new_value > 255){ + *channel = 255; + } + else if (new_value < 0){ + *channel = 0; + } + else{ + *channel = (u8)new_value; + } + fill_buffer_color_channel(string_buffer, *channel, ftype); + result = 1; + } + ui->state->selected.sub_id2 = indx; + } + } + else{ + f32_Rect r = f32R(hit_region); + r.x0 += indx*ui->hex_advance+1; + r.x1 = r.x0+ui->hex_advance+1; + draw_rectangle(target, r, back); + } + } + + if (!ui->state->input_stage) + draw_string_mono(target, font_id, string_buffer, + (real32)x0 + 1, (real32)y, ui->hex_advance, + color); + + return result; +} + +internal void +do_rgb_sliders(Color_UI *ui, i32_Rect rect){ + i32 dec_x0, dec_x1; + dec_x0 = rect.x0 + 10; + dec_x1 = TRUNC32(dec_x0 + ui->hex_advance*3 + 2); + + i32 hex_x0, hex_x1; + hex_x0 = dec_x1 + 10; + hex_x1 = TRUNC32(hex_x0 + ui->hex_advance*2 + 2); + + rect.x0 = hex_x1; + real32 bar_width = (real32)(rect.x1 - rect.x0 - 20); + + f32_Rect slider; + f32 y; + i32 sub_id; + u8 channel; + + real32 v_full_space = 30.f; + real32 v_half_space = 15.f; + real32 v_quarter_space = 12.f; + real32 v_handle = 9.f; + + u32 packed_color = *ui->color.out; + + y = rect.y0 + v_quarter_space; + slider.x0 = rect.x0 + 10.f; + slider.x1 = slider.x0 + bar_width; + + sub_id = 0; + + persist i32 shifts[3] = { 16, 8, 0 }; + persist u32 fore_colors[3] = { 0xFFFF0000, 0xFF00FF00, 0xFF1919FF }; + persist u32 back_colors[3] = { 0xFF222222, 0xFF222222, 0xFF131313 }; + + for (i32 i = 0; i < 3; ++i){ + i32 shift = shifts[i]; + u32 fore = fore_colors[i]; + u32 back = back_colors[i]; + + ++sub_id; + channel = (packed_color >> shift) & 0xFF; + if (do_channel_field(sub_id, ui, &channel, CF_DEC, + (i32)y, fore, back, dec_x0, dec_x1)) + super_color_post_byte(&ui->color, i, channel); + + ++sub_id; + channel = (packed_color >> shift) & 0xFF; + if (do_channel_field(sub_id, ui, &channel, CF_HEX, + (i32)y, fore, back, hex_x0, hex_x1)) + super_color_post_byte(&ui->color, i, channel); + + ++sub_id; + slider.y0 = y; + slider.y1 = slider.y0 + v_half_space; + if (bar_width > 45.f) + do_single_slider(sub_id, ui, i, 1, 10, 100.f, slider, v_handle, rect); + y += v_full_space; + } +} + +struct Blob_Layout{ + i32_Rect rect; + i32 x, y; + i32 size, space; +}; + +internal void +begin_layout(Blob_Layout *layout, i32_Rect rect){ + layout->rect = rect; + layout->x = rect.x0 + 10; + layout->y = rect.y0; + layout->size = 20; + layout->space = 5; +} + +internal void +do_blob(Color_UI *ui, Blob_Layout *layout, u32 color, bool32 *set_me, i32 sub_id){ + i32_Rect rect = layout->rect; + f32_Rect blob; + blob.x0 = (real32)layout->x; + blob.y0 = (real32)layout->y; + blob.x1 = blob.x0 + layout->size; + blob.y1 = blob.y0 + layout->size; + + layout->y += layout->size + layout->space; + if (layout->y + layout->size + layout->space*2 > rect.y1){ + layout->y = rect.y0; + layout->x += layout->size + layout->space; + } + + if (ui->state->input_stage){ + bool32 right = 0; + if (ui_do_button_input(ui->state, i32R(blob), make_sub1(ui->state, sub_id), 0, &right)){ + super_color_post_packed(&ui->color, color); + } + else if (right) *set_me = 1; + } + else{ + Render_Target *target = ui->state->target; + draw_rectangle(target, blob, color); + persist u32 silver = 0xFFa0a0a0; + draw_rectangle_outline(target, blob, silver); + } +} + +inline void +do_blob(Color_UI *ui, Blob_Layout *layout, u32 *color, bool32 *set_me){ + i32 sub_id = (i32)((char*)color - (char*)ui->state->style); + do_blob(ui, layout, *color, set_me, sub_id); +} + +internal void +do_v_divide(Color_UI *ui, Blob_Layout *layout, i32 width){ + i32_Rect rect = layout->rect; + if (layout->y > rect.y0){ + layout->x += layout->size + layout->space; + } + layout->x += width; + layout->y = rect.y0; +} + +internal void +do_palette(Color_UI *ui, i32_Rect rect){ + Style *style = ui->state->style; + Blob_Layout layout; + begin_layout(&layout, rect); + bool32 set_me; + + do_blob(ui, &layout, &style->main.back_color, &set_me); + do_blob(ui, &layout, &style->main.margin_color, &set_me); + do_blob(ui, &layout, &style->main.margin_active_color, &set_me); + + do_blob(ui, &layout, &style->main.cursor_color, &set_me); + do_blob(ui, &layout, &style->main.at_cursor_color, &set_me); + do_blob(ui, &layout, &style->main.mark_color, &set_me); + + do_blob(ui, &layout, &style->main.highlight_color, &set_me); + do_blob(ui, &layout, &style->main.at_highlight_color, &set_me); + + do_blob(ui, &layout, &style->main.default_color, &set_me); + do_blob(ui, &layout, &style->main.comment_color, &set_me); + do_blob(ui, &layout, &style->main.keyword_color, &set_me); + do_blob(ui, &layout, &style->main.str_constant_color, &set_me); + do_blob(ui, &layout, &style->main.char_constant_color, &set_me); + do_blob(ui, &layout, &style->main.int_constant_color, &set_me); + do_blob(ui, &layout, &style->main.float_constant_color, &set_me); + do_blob(ui, &layout, &style->main.bool_constant_color, &set_me); + do_blob(ui, &layout, &style->main.include_color, &set_me); + do_blob(ui, &layout, &style->main.preproc_color, &set_me); + do_blob(ui, &layout, &style->main.special_character_color, &set_me); + + do_blob(ui, &layout, &style->main.highlight_junk_color, &set_me); + do_blob(ui, &layout, &style->main.highlight_white_color, &set_me); + + do_blob(ui, &layout, &style->main.paste_color, &set_me); + + do_blob(ui, &layout, &style->main.file_info_style.bar_color, &set_me); + do_blob(ui, &layout, &style->main.file_info_style.base_color, &set_me); + do_blob(ui, &layout, &style->main.file_info_style.pop1_color, &set_me); + do_blob(ui, &layout, &style->main.file_info_style.pop2_color, &set_me); + + do_v_divide(ui, &layout, 20); + + if (!ui->state->input_stage){ + Render_Target *target = ui->state->target; + draw_string(target, ui->state->font_id, "Global Palette: right click to save color", + layout.x, layout.rect.y0, style->main.default_color); + } + + layout.rect.y0 += layout.size + layout.space; + layout.y = layout.rect.y0; + i32 palette_size = ui->palette_size + 1000; + u32 *color = ui->palette; + for (i32 i = 1000; i < palette_size; ++i, ++color){ + set_me = 0; + do_blob(ui, &layout, *color, &set_me, i); + if (set_me){ + *color = *ui->color.out; + ui->state->redraw = 1; + } + } +} + +internal void +do_sub_button(i32 id, Color_UI *ui, char *text){ + i16 font_id = ui->state->font_id; + i32 line_height = get_font_info(ui->state->font_set, font_id)->height; + i32_Rect rect = layout_rect(ui->layout, line_height + 2); + + if (ui->state->input_stage){ + ui_do_button_input(ui->state, rect, make_sub0(ui->state, id), 1); + } + else{ + Render_Target *target = ui->state->target; + + u32 back_color, text_color; + text_color = 0xFFDDDDDD; + if (ui->state->selected.sub_id0 == id){ + back_color = 0xFF444444; + } + else if (ui->state->hover.sub_id0 == id){ + back_color = 0xFF222222; + } + else{ + back_color = 0xFF111111; + } + + draw_rectangle(target, rect, back_color); + draw_string(target, font_id, text, rect.x0, rect.y0 + 1, + text_color); + } +} + +internal void +do_color_adjuster(Color_UI *ui, u32 *color, + u32 text_color, u32 back_color, char *name){ + i32 id = raw_ptr_dif(color, ui->state->style); + i16 font_id = ui->state->font_id; + i32 character_h = get_font_info(ui->state->font_set, font_id)->height; + u32 text = 0, back = 0; + + i32_Rect bar = layout_rect(ui->layout, character_h); + + if (ui->state->input_stage){ + if (ui_do_button_input(ui->state, bar, make_id(ui->state, id), 1)){ + ui->has_hover_color = 1; + ui->hover_color = super_color_create(*color); + } + } + + else{ + Render_Target *target = ui->state->target; + u32 text_hover = 0xFF101010; + u32 back_hover = 0xFF999999; + if (ui->state->selected.id != id && ui->state->hover.id == id){ + text = text_hover; + back = back_hover; + } + else{ + text = text_color; + back = back_color; + } + + draw_rectangle(target, bar, back); + i32 end_pos = draw_string(target, font_id, name, bar.x0, bar.y0, text); + + real32 x_spacing = ui->hex_advance; + i32_Rect temp_rect = bar; + temp_rect.x0 = temp_rect.x1 - CEIL32(x_spacing * 9.f); + if (temp_rect.x0 >= end_pos + x_spacing){ + u32 n = *color; + char full_hex_string[] = "0x000000"; + for (i32 i = 7; i >= 2; --i){ + i32 m = (n & 0xF); + n >>= 4; + full_hex_string[i] = int_to_hexchar(m); + } + draw_string_mono(target, font_id, full_hex_string, + (f32)temp_rect.x0, (f32)bar.y0, + x_spacing, text); + } + + for (i32 i = 0; i < ArrayCount(ui->highlight.ids); ++i){ + if (ui->highlight.ids[i] == id){ + draw_rectangle_outline(target, f32R(bar), text_color); + break; + } + } + } + + if (ui->state->selected.id == id){ + Render_Target *target = ui->state->target; + i32_Rect expanded = layout_rect(ui->layout, 115 + (character_h + 2)); + UI_Layout_Restore restore = begin_sub_layout(ui->layout, expanded); + + ui->color.out = color; + + if (ui->state->input_stage){ + if (ui->state->selected.sub_id0 == 0){ + ui->state->selected.sub_id0 = 1; + } + } + else{ + draw_rectangle(target, expanded, 0xff000000); + } + + begin_row(ui->layout, 3); + do_sub_button(1, ui, "HSL"); + do_sub_button(2, ui, "RGB"); + do_sub_button(3, ui, "Palette"); + + i32_Rect sub_rect; + sub_rect = expanded; + sub_rect.y0 += 10 + character_h; + + switch (ui->state->selected.sub_id0){ + case 1: do_hsl_sliders(ui, sub_rect); break; + case 2: do_rgb_sliders(ui, sub_rect); break; + case 3: do_palette(ui, sub_rect); break; + } + + end_sub_layout(restore); + } +} + +internal void +do_style_name(Color_UI *ui){ + i32 id = -3; + + i16 font_id = ui->state->font_id; + i32 line_height = get_font_info(ui->state->font_set, font_id)->height; + + i32_Rect srect = layout_rect(ui->layout, line_height); + + Widget_ID wid = make_id(ui->state, id); + b32 selected = is_selected(ui->state, wid); + + if (ui->state->input_stage){ + if (!selected){ + ui_do_button_input(ui->state, srect, wid, 1); + } + else{ + Style *style = ui->state->style; + if (ui_do_text_field_input(ui->state, &style->name)){ + ui->state->selected = {}; + } + } + } + else{ + Render_Target *target = ui->state->target; + Style *style = ui->state->style; + u32 back, fore_text, fore_label; + if (selected){ + back = 0xFF000000; + fore_label = 0xFF808080; + fore_text = 0xFFFFFFFF; + } + else if (is_hover(ui->state, wid)){ + back = 0xFF999999; + fore_text = fore_label = 0xFF101010; + } + else{ + back = style->main.back_color; + fore_text = fore_label = style->main.default_color; + } + + draw_rectangle(target, srect, back); + i32 x = srect.x0; + x = draw_string(target, font_id, "NAME: ", + x, srect.y0, fore_label); + x = draw_string(target, font_id, style->name.str, + x, srect.y0, fore_text); + } +} + +internal b32 +do_font_option(Color_UI *ui, i16 font_id){ + b32 result = 0; + Font_Info *info = get_font_info(ui->state->font_set, font_id); + + i32 sub_id = (i32)(i64)(info); + i32_Rect orect = layout_rect(ui->layout, info->height); + + Widget_ID wid = make_sub0(ui->state, sub_id); + if (ui->state->input_stage){ + if (ui_do_button_input(ui->state, orect, wid, 0)){ + result = 1; + } + } + else{ + Render_Target *target = ui->state->target; + u32 back, fore; + if (is_hover(ui->state, wid)){ + back = 0xFF999999; + fore = 0xFF101010; + } + else{ + back = 0xFF000000; + fore = 0xFFFFFFFF; + } + draw_rectangle(target, orect, back); + i32 x = orect.x0; + x = draw_string(target, font_id, "->", x, orect.y0, fore); + draw_string(target, font_id, info->name.str, x, orect.y0, fore); + } + + return result; +} + +internal void +do_font_switch(Color_UI *ui){ + i32 id = -2; + Render_Target *target = ui->state->target; + Font_Set *font_set = ui->state->font_set; + + i16 font_id = ui->state->font_id; + Font_Info *info = get_font_info(font_set, font_id); + i32 character_h = info->height; + + i32_Rect srect = layout_rect(ui->layout, character_h); + Widget_ID wid = make_id(ui->state, id); + + if (ui->state->input_stage){ + ui_do_button_input(ui->state, srect, wid, 1); + } + else{ + Style *style = ui->state->style; + u32 back, fore; + if (is_hover(ui->state, wid) && !is_selected(ui->state, wid)){ + back = 0xFF999999; + fore = 0xFF101010; + } + else{ + back = style->main.back_color; + fore = style->main.default_color; + } + draw_rectangle(target, srect, back); + i32 x = srect.x0; + x = draw_string(target, font_id, "FONT: ", + x, srect.y0, fore); + x = draw_string(target, font_id, info->name.str, + x, srect.y0, fore); + } + + if (is_selected(ui->state, wid)){ + srect = layout_rect(ui->layout, character_h/2); + if (!ui->state->input_stage) + draw_rectangle(target, srect, 0xFF000000); + + i32 count = font_set->count + 1; + + for (i16 i = 1; i < count; ++i){ + if (i == font_id) continue; + if (do_font_option(ui, i)){ + ui->global_font->font_id = i; + ui->global_font->font_changed = 1; + } + } + + srect = layout_rect(ui->layout, character_h/2); + if (!ui->state->input_stage) + draw_rectangle(target, srect, 0xFF000000); + } +} + +internal b32 +do_style_preview(Library_UI *ui, Style *style, i32 toggle = -1){ + b32 result = 0; + i32 id; + if (style == ui->state->style) id = 2; + else id = raw_ptr_dif(style, ui->styles->styles) + 100; + + i16 font_id = ui->state->font_id; + Font_Info *info = get_font_info(ui->state->font_set, font_id); + + i32_Rect prect = layout_rect(ui->layout, info->height*3 + 6); + + Widget_ID wid = make_id(ui->state, id); + + if (ui->state->input_stage){ + if (ui_do_button_input(ui->state, prect, wid, 0)){ + result = 1; + } + } + else{ + Render_Target *target = ui->state->target; + u32 margin_color = style->main.margin_color; + if (is_hover(ui->state, wid)){ + margin_color = style->main.margin_active_color; + } + + i32_Rect inner; + if (toggle != -1){ + i32_Rect toggle_box = prect; + toggle_box.x1 = toggle_box.x0 + info->height*2 + 6; + prect.x0 = toggle_box.x1; + + inner = get_inner_rect(toggle_box, 3); + draw_margin(target, toggle_box, inner, margin_color); + draw_rectangle(target, inner, style->main.back_color); + + i32 d; + d = info->height/2; + i32_Rect b; + b.x0 = (inner.x1 + inner.x0)/2 - d; + b.y0 = (inner.y1 + inner.y0)/2 - d; + b.x1 = b.x0 + info->height; + b.y1 = b.y0 + info->height; + if (toggle) draw_rectangle(target, b, margin_color); + else draw_rectangle_outline(target, b, margin_color); + } + + inner = get_inner_rect(prect, 3); + draw_margin(target, prect, inner, margin_color); + draw_rectangle(target, inner, style->main.back_color); + + i32 text_y = inner.y0; + i32 text_x = inner.x0; + text_x = draw_string(target, font_id, style->name.str, + text_x, text_y, style->main.default_color); + i32 font_x = (i32)(inner.x1 - font_string_width(target, font_id, info->name.str)); + if (font_x > text_x + 10) + draw_string(target, font_id, info->name.str, + font_x, text_y, style->main.default_color); + + text_x = inner.x0; + text_y += info->height; + text_x = draw_string(target, font_id, "if ", text_x, text_y, + style->main.keyword_color); + text_x = draw_string(target, font_id, "(x < ", text_x, text_y, + style->main.default_color); + text_x = draw_string(target, font_id, "0", text_x, text_y, + style->main.int_constant_color); + text_x = draw_string(target, font_id, ") { x = ", text_x, text_y, + style->main.default_color); + text_x = draw_string(target, font_id, "0", text_x, text_y, + style->main.int_constant_color); + text_x = draw_string(target, font_id, "; } ", text_x, text_y, + style->main.default_color); + text_x = draw_string(target, font_id, "// comment", text_x, text_y, + style->main.comment_color); + + text_x = inner.x0; + text_y += info->height; + text_x = draw_string(target, font_id, "[] () {}; * -> +-/ <>= ! && || % ^", + text_x, text_y, style->main.default_color); + } + + ui->layout->y = prect.y1; + return result; +} + + +// BOTTOM + diff --git a/buffer/4coder_golden_array.cpp b/buffer/4coder_golden_array.cpp index 32019cb5..9d7cdf19 100644 --- a/buffer/4coder_golden_array.cpp +++ b/buffer/4coder_golden_array.cpp @@ -162,30 +162,31 @@ buffer_backify_next(Buffer_Backify_Loop *loop){ internal_4tech int buffer_replace_range(Buffer *buffer, int start, int end, char *str, int len, int *shift_amount, - void *scratch, int scratch_size, int *request_amount){ + void *scratch, int scratch_size, int *request_amount){ + char *data; int result; int size; - + size = buffer_size(buffer); assert_4tech(0 <= start); assert_4tech(start <= end); assert_4tech(end <= size); - + *shift_amount = (len - (end - start)); if (*shift_amount + size <= buffer->max){ data = (char*)buffer->data; memmove_4tech(data + end + *shift_amount, data + end, buffer->size - end); buffer->size += *shift_amount; if (len != 0) memcpy_4tech(data + start, str, len); - + result = 0; } else{ *request_amount = round_up_4tech(2*(*shift_amount + size), 4 << 10); result = 1; } - + return(result); } @@ -199,16 +200,17 @@ buffer_batch_edit_step(Buffer_Batch_State *state, Buffer *buffer, Buffer_Edit *s result = 0; shift_total = state->shift_total; i = state->i; - + edit = sorted_edits + i; for (; i < edit_count; ++i, ++edit){ + assert(edit->end + shift_total < buffer_size(buffer)); result = buffer_replace_range(buffer, edit->start + shift_total, edit->end + shift_total, - strings + edit->str_start, edit->len, &shift_amount, - scratch, scratch_size, request_amount); + strings + edit->str_start, edit->len, &shift_amount, + scratch, scratch_size, request_amount); if (result) break; shift_total += shift_amount; } - + state->shift_total = shift_total; state->i = i; diff --git a/build.bat b/build.bat index 64dca135..de341b76 100644 --- a/build.bat +++ b/build.bat @@ -1,5 +1,5 @@ @echo off -"w:\4ed\misc\build_exp.bat" /Zi -REM "w:\4ed\misc\build_all.bat" /DFRED_SUPER /DFRED_NOT_PACKAGE /Zi +REM "w:\4ed\misc\build_exp.bat" /Zi +"w:\4ed\misc\build_all.bat" /DFRED_SUPER /DFRED_NOT_PACKAGE /Zi REM "w:\4ed\misc\build_all.bat" /O2 /Zi diff --git a/test/4cpp_lexer_fsms.h b/test/4cpp_lexer_fsms.h index 9c1c153e..4bb73af6 100644 --- a/test/4cpp_lexer_fsms.h +++ b/test/4cpp_lexer_fsms.h @@ -58,7 +58,9 @@ enum Lex_Int_State{ LSINT_ul, LSINT_uL, LSINT_ll, - LSINT_extra + LSINT_extra, + // + LSINT_count }; enum Lex_INC_State{ diff --git a/test/4cpp_lexer_tables.c b/test/4cpp_lexer_tables.c index 889ba4af..c7944363 100644 --- a/test/4cpp_lexer_tables.c +++ b/test/4cpp_lexer_tables.c @@ -10,6 +10,19 @@ unsigned char whitespace_fsm_table[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, }; +unsigned short int_fsm_eq_classes[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0,16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,24, 0, 0, 0, 0, 0, 0, 0, 0,16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +}; + +const int num_int_fsm_eq_classes = 4; + +unsigned char int_fsm_table[] = { + 8, 9,10,11,12,13,14,15, + 3, 5,10, 6,12, 7,14,15, + 1, 9, 7, 7,12,13, 7,15, + 2, 4, 6,11, 7,13,14,15, +}; + unsigned char multiline_state_table[] = { 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; diff --git a/test/4cpp_new_lexer.h b/test/4cpp_new_lexer.h index 96183564..db49a4b2 100644 --- a/test/4cpp_new_lexer.h +++ b/test/4cpp_new_lexer.h @@ -4,13 +4,9 @@ #ifndef FCPP_NEW_LEXER_INC #define FCPP_NEW_LEXER_INC -#include "../4cpp_lexer_types.h" #include "4cpp_lexer_fsms.h" #include "4cpp_lexer_tables.c" -namespace new_lex{ -// - #define lexer_link static // TODO(allen): revisit this keyword data declaration system @@ -336,7 +332,8 @@ cpp_shift_token_starts(Cpp_Token_Stack *stack, int from_token_i, int shift_amoun enum Pos_Update_Rule{ PUR_none, - PUR_unget_whitespace + PUR_back_one, + PUR_unget_whitespace, }; lexer_link Lex_PP_State @@ -439,470 +436,6 @@ struct Lex_Data{ int __pc__; }; -Whitespace_FSM -whitespace_skip_fsm(Whitespace_FSM wfsm, char c){ - if (wfsm.pp_state != LSPP_default){ - if (c == '\n') wfsm.pp_state = LSPP_default; - } - if (!(c == ' ' || c == '\n' || c == '\t' || c == '\r' || c == '\f' || c == '\v')){ - wfsm.white_done = 1; - } - return(wfsm); -} - -Lex_FSM -int_fsm(Lex_FSM fsm, char c){ - switch (fsm.int_state){ - case LSINT_default: - switch (c){ - case 'u': case 'U': fsm.int_state = LSINT_u; break; - case 'l': fsm.int_state = LSINT_l; break; - case 'L': fsm.int_state = LSINT_L; break; - default: fsm.emit_token = 1; break; - } - break; - - case LSINT_u: - switch (c){ - case 'l': fsm.int_state = LSINT_ul; break; - case 'L': fsm.int_state = LSINT_uL; break; - default: fsm.emit_token = 1; break; - } - break; - - case LSINT_l: - switch (c){ - case 'l': fsm.int_state = LSINT_ll; break; - case 'U': case 'u': fsm.int_state = LSINT_extra; break; - default: fsm.emit_token = 1; break; - } - break; - - case LSINT_L: - switch (c){ - case 'L': fsm.int_state = LSINT_ll; break; - case 'U': case 'u': fsm.int_state = LSINT_extra; break; - default: fsm.emit_token = 1; break; - } - break; - - case LSINT_ul: - switch (c){ - case 'l': fsm.int_state = LSINT_extra; break; - default: fsm.emit_token = 1; break; - } - break; - - case LSINT_uL: - switch (c){ - case 'L': fsm.int_state = LSINT_extra; break; - default: fsm.emit_token = 1; break; - } - break; - - case LSINT_ll: - switch (c){ - case 'u': case 'U': fsm.int_state = LSINT_extra; break; - default: fsm.emit_token = 1; break; - } - break; - - case LSINT_extra: - fsm.emit_token = 1; - break; - } - return(fsm); -} - -Lex_FSM -main_fsm(Lex_FSM fsm, unsigned char pp_state, unsigned char c){ - if (c == 0) fsm.emit_token = 1; - else - switch (pp_state){ - case LSPP_error: - fsm.state = LS_error_message; - if (c == '\n') fsm.emit_token = 1; - break; - - case LSPP_include: - switch (fsm.state){ - case LSINC_default: - switch (c){ - case '"': fsm.state = LSINC_quotes; break; - case '<': fsm.state = LSINC_pointy; break; - default: fsm.state = LSINC_junk; break; - } - break; - - case LSINC_quotes: - if (c == '"') fsm.emit_token = 1; - break; - - case LSINC_pointy: - if (c == '>') fsm.emit_token = 1; - break; - - case LSINC_junk: - if (c == '\n') fsm.emit_token = 1; - break; - } - break; - - default: - switch (fsm.state){ - case LS_default: - if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'){ - fsm.state = LS_identifier; - } - else if (c >= '1' && c <= '9'){ - fsm.state = LS_number; - } - else if (c == '0'){ - fsm.state = LS_number0; - } - else switch (c){ - case '\'': fsm.state = LS_char; break; - case '"': fsm.state = LS_string; break; - - case '/': fsm.state = LS_comment_pre; break; - - case '.': fsm.state = LS_dot; break; - - case '<': fsm.state = LS_less; break; - case '>': fsm.state = LS_more; break; - - case '-': fsm.state = LS_minus; break; - - case '&': fsm.state = LS_and; break; - case '|': fsm.state = LS_or; break; - - case '+': fsm.state = LS_plus; break; - - case ':': fsm.state = LS_colon; break; - - case '*': fsm.state = LS_star; break; - - case '%': fsm.state = LS_modulo; break; - case '^': fsm.state = LS_caret; break; - - case '=': fsm.state = LS_eq; break; - case '!': fsm.state = LS_bang; break; - - case '#': fsm.state = LS_pound; break; - -#define OperCase(op,type) case op: fsm.emit_token = 1; break; - OperCase('{', CPP_TOKEN_BRACE_OPEN); - OperCase('}', CPP_TOKEN_BRACE_CLOSE); - - OperCase('[', CPP_TOKEN_BRACKET_OPEN); - OperCase(']', CPP_TOKEN_BRACKET_CLOSE); - - OperCase('(', CPP_TOKEN_PARENTHESE_OPEN); - OperCase(')', CPP_TOKEN_PARENTHESE_CLOSE); - - OperCase('~', CPP_TOKEN_TILDE); - OperCase(',', CPP_TOKEN_COMMA); - OperCase(';', CPP_TOKEN_SEMICOLON); - OperCase('?', CPP_TOKEN_TERNARY_QMARK); - - OperCase('@', CPP_TOKEN_JUNK); - OperCase('$', CPP_TOKEN_JUNK); - OperCase('\\', CPP_TOKEN_JUNK); -#undef OperCase - } - break; - - case LS_identifier: - if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_')){ - fsm.emit_token = 1; - } - break; - - case LS_pound: - if (pp_state == LSPP_default){ - if (c == ' ' || c == '\t' || c == '\r' || c == '\f' || c == '\v'){ - fsm.state = LS_pound; - } - else if (c == '\n'){ - fsm.emit_token = 1; - } - else{ - fsm.state = LS_pp; - } - } - else{ - switch (c){ - case '#': fsm.emit_token = 1; break; - default: fsm.emit_token = 1; break; - } - } - break; - - case LS_pp: - if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_')){ - fsm.emit_token = 1; - } - break; - - case LS_char: - switch(c){ - case '\'': fsm.emit_token = 1; break; - case '\\': fsm.state = LS_char_slashed; break; - } - break; - - case LS_char_slashed: - switch (c){ - case '\r': case '\f': case '\v': break; - case '\n': fsm.state = LS_string; fsm.multi_line |= 1; break; - default: fsm.state = LS_char; break; - } - break; - - case LS_string: - switch(c){ - case '\"': fsm.emit_token = 1; break; - case '\\': fsm.state = LS_string_slashed; break; - } - break; - - case LS_string_slashed: - switch (c){ - case '\r': case '\f': case '\v': break; - case '\n': fsm.state = LS_string; fsm.multi_line |= 1; break; - default: fsm.state = LS_string; break; - } - break; - - case LS_number: - if (c >= '0' && c <= '9'){ - fsm.state = LS_number; - } - else{ - switch (c){ - case '.': fsm.state = LS_float; break; - default: fsm.emit_token = 1; break; - } - } - break; - - case LS_number0: - if (c >= '0' && c <= '9'){ - fsm.state = LS_number; - } - else if (c == 'x'){ - fsm.state = LS_hex; - } - else if (c == '.'){ - fsm.state = LS_float; - } - else{ - fsm.emit_token = 1; - } - break; - - case LS_float: - if (!(c >= '0' && c <= '9')){ - switch (c){ - case 'e': fsm.state = LS_crazy_float0; break; - default: fsm.emit_token = 1; break; - } - } - break; - - case LS_crazy_float0: - { - if ((c >= '0' && c <= '9') || c == '-'){ - fsm.state = LS_crazy_float1; - } - else{ - fsm.emit_token = 1; - } - } - break; - - case LS_crazy_float1: - { - if (!(c >= '0' && c <= '9')){ - fsm.emit_token = 1; - } - } - break; - - case LS_hex: - if (!(c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F')){ - fsm.emit_token = 1; - } - break; - - case LS_dot: - if (c >= '0' && c <= '9'){ - fsm.state = LS_float; - } - else - switch (c){ - case '.': fsm.state = LS_ellipsis; break; - case '*': fsm.emit_token = 1; break; - default: fsm.emit_token = 1; break; - } - break; - - case LS_ellipsis: fsm.emit_token = 1; break; - - case LS_less: - switch (c){ - case '<': fsm.state = LS_less_less; break; - case '=': fsm.emit_token = 1; break; - default: fsm.emit_token = 1; break; - } - break; - - case LS_less_less: - switch (c){ - case '=': fsm.emit_token = 1; break; - default: fsm.emit_token = 1; break; - } - break; - - case LS_more: - switch (c){ - case '>': fsm.state = LS_more_more; break; - case '=': fsm.emit_token = 1; break; - default: fsm.emit_token = 1; break; - } - break; - - case LS_more_more: - switch (c){ - case '=': fsm.emit_token = 1; break; - default: fsm.emit_token = 1; break; - } - break; - - case LS_comment_pre: - switch (c){ - case '/': fsm.state = LS_comment; break; - case '*': fsm.state = LS_comment_block; break; - case '=': fsm.emit_token = 1; break; - default: fsm.emit_token = 1; break; - } - break; - - case LS_comment: - switch (c){ - case '\\': fsm.state = LS_comment_slashed; break; - case '\n': fsm.emit_token = 1; break; - } - break; - - case LS_comment_slashed: - switch (c){ - case '\r': case '\f': case '\v': break; - default: fsm.state = LS_comment; break; - } - break; - - case LS_comment_block: - switch (c){ - case '*': fsm.state = LS_comment_block_ending; break; - } - break; - - case LS_comment_block_ending: - switch (c){ - case '*': fsm.state = LS_comment_block_ending; break; - case '/': fsm.emit_token = 1; break; - default: fsm.state = LS_comment_block; break; - } - break; - - case LS_minus: - switch (c){ - case '>': fsm.state = LS_arrow; break; - case '-': fsm.emit_token = 1; break; - case '=': fsm.emit_token = 1; break; - default: fsm.emit_token = 1; break; - } - break; - - case LS_arrow: - switch (c){ - case '*': fsm.emit_token = 1; break; - default: fsm.emit_token = 1; break; - } - break; - - case LS_and: - switch (c){ - case '&': fsm.emit_token = 1; break; - case '=': fsm.emit_token = 1; break; - default: fsm.emit_token = 1; break; - } - break; - - case LS_or: - switch (c){ - case '|': fsm.emit_token = 1; break; - case '=': fsm.emit_token = 1; break; - default: fsm.emit_token = 1; break; - } - break; - - case LS_plus: - switch (c){ - case '+': fsm.emit_token = 1; break; - case '=': fsm.emit_token = 1; break; - default: fsm.emit_token = 1; break; - } - break; - - case LS_colon: - switch (c){ - case ':': fsm.emit_token = 1; break; - default: fsm.emit_token = 1; break; - } - break; - - case LS_star: - switch (c){ - case '=': fsm.emit_token = 1; break; - default: fsm.emit_token = 1; break; - } - break; - - case LS_modulo: - switch (c){ - case '=': fsm.emit_token = 1; break; - default: fsm.emit_token = 1; break; - } - break; - - case LS_caret: - switch (c){ - case '=': fsm.emit_token = 1; break; - default: fsm.emit_token = 1; break; - } - break; - - case LS_eq: - switch (c){ - case '=': fsm.emit_token = 1; break; - default: fsm.emit_token = 1; break; - } - break; - - case LS_bang: - switch (c){ - case '=': fsm.emit_token = 1; break; - default: fsm.emit_token = 1; break; - } - break; - } - break; - } - return(fsm); -} - #define DrCase(PC) case PC: goto resumespot_##PC #define DrYield(PC, n) {\ @@ -1002,463 +535,472 @@ cpp_lex_nonalloc(Lex_Data *S_ptr, char *chunk, int size, Cpp_Token_Stack *token_ break; } } - else switch (S.fsm.state){ - case LS_default: - switch (c){ + else{ + switch (S.fsm.state){ + case LS_default: + switch (c){ #define OperCase(op,t) case op: S.token.type = t; break; - OperCase('{', CPP_TOKEN_BRACE_OPEN); - OperCase('}', CPP_TOKEN_BRACE_CLOSE); + OperCase('{', CPP_TOKEN_BRACE_OPEN); + OperCase('}', CPP_TOKEN_BRACE_CLOSE); - OperCase('[', CPP_TOKEN_BRACKET_OPEN); - OperCase(']', CPP_TOKEN_BRACKET_CLOSE); + OperCase('[', CPP_TOKEN_BRACKET_OPEN); + OperCase(']', CPP_TOKEN_BRACKET_CLOSE); - OperCase('(', CPP_TOKEN_PARENTHESE_OPEN); - OperCase(')', CPP_TOKEN_PARENTHESE_CLOSE); + OperCase('(', CPP_TOKEN_PARENTHESE_OPEN); + OperCase(')', CPP_TOKEN_PARENTHESE_CLOSE); - OperCase('~', CPP_TOKEN_TILDE); - OperCase(',', CPP_TOKEN_COMMA); - OperCase(';', CPP_TOKEN_SEMICOLON); - OperCase('?', CPP_TOKEN_TERNARY_QMARK); + OperCase('~', CPP_TOKEN_TILDE); + OperCase(',', CPP_TOKEN_COMMA); + OperCase(';', CPP_TOKEN_SEMICOLON); + OperCase('?', CPP_TOKEN_TERNARY_QMARK); - OperCase('@', CPP_TOKEN_JUNK); - OperCase('$', CPP_TOKEN_JUNK); + OperCase('@', CPP_TOKEN_JUNK); + OperCase('$', CPP_TOKEN_JUNK); #undef OperCase - case '\\': - if (S.pp_state == LSPP_default){ - S.token.type = CPP_TOKEN_JUNK; - } - else{ - S.pos_overide = S.pos; - S.wfsm.white_done = 0; - for (;;){ - for (; S.wfsm.white_done == 0 && S.pos < end_pos;){ - c = chunk[S.pos++]; - if (!(c == ' ' || c == '\t' || c == '\r' || c == '\v' || c == '\f')) S.wfsm.white_done = 1; - } - - if (S.wfsm.white_done == 0){ - DrYield(1, 1); - } - else break; - } - - if (c == '\n'){ - S.fsm.emit_token = 0; - S.pos_overide = 0; - } - else{ + case '\\': + if (S.pp_state == LSPP_default){ S.token.type = CPP_TOKEN_JUNK; - } - } - break; - } - if (c != '@' && c != '$' && c != '\\'){ - S.token.flags = CPP_TFLAG_IS_OPERATOR; - } - break; - - case LS_identifier: - { - --S.pos; - - int word_size = S.pos - S.token_start; - - if (S.pp_state == LSPP_body_if){ - if (match(make_string(S.tb, word_size), make_lit_string("defined"))){ - S.token.type = CPP_TOKEN_DEFINED; - S.token.flags = CPP_TFLAG_IS_OPERATOR | CPP_TFLAG_IS_KEYWORD; - break; } - } + else{ + S.pos_overide = S.pos; + S.wfsm.white_done = 0; + for (;;){ + for (; S.wfsm.white_done == 0 && S.pos < end_pos;){ + c = chunk[S.pos++]; + if (!(c == ' ' || c == '\t' || c == '\r' || c == '\v' || c == '\f')) S.wfsm.white_done = 1; + } - Sub_Match_List_Result sub_match; - sub_match = sub_match_list(S.tb, S.tb_pos, 0, bool_lits, word_size); + if (S.wfsm.white_done == 0){ + DrYield(1, 1); + } + else break; + } - if (sub_match.index != -1){ - S.token.type = CPP_TOKEN_BOOLEAN_CONSTANT; - S.token.flags = CPP_TFLAG_IS_KEYWORD; + if (c == '\n'){ + S.fsm.emit_token = 0; + S.pos_overide = 0; + } + else{ + S.token.type = CPP_TOKEN_JUNK; + } + } + break; } - else{ - sub_match = sub_match_list(S.tb, S.tb_pos, 0, keywords, word_size); + if (c != '@' && c != '$' && c != '\\'){ + S.token.flags = CPP_TFLAG_IS_OPERATOR; + } + break; + + case LS_identifier: + { + --S.pos; + + int word_size = S.pos - S.token_start; + + if (S.pp_state == LSPP_body_if){ + if (match(make_string(S.tb, word_size), make_lit_string("defined"))){ + S.token.type = CPP_TOKEN_DEFINED; + S.token.flags = CPP_TFLAG_IS_OPERATOR | CPP_TFLAG_IS_KEYWORD; + break; + } + } + + Sub_Match_List_Result sub_match; + sub_match = sub_match_list(S.tb, S.tb_pos, 0, bool_lits, word_size); if (sub_match.index != -1){ - String_And_Flag data = keywords.data[sub_match.index]; - S.token.type = (Cpp_Token_Type)data.flags; + S.token.type = CPP_TOKEN_BOOLEAN_CONSTANT; S.token.flags = CPP_TFLAG_IS_KEYWORD; } else{ - S.token.type = CPP_TOKEN_IDENTIFIER; + sub_match = sub_match_list(S.tb, S.tb_pos, 0, keywords, word_size); + + if (sub_match.index != -1){ + String_And_Flag data = keywords.data[sub_match.index]; + S.token.type = (Cpp_Token_Type)data.flags; + S.token.flags = CPP_TFLAG_IS_KEYWORD; + } + else{ + S.token.type = CPP_TOKEN_IDENTIFIER; + S.token.flags = 0; + } + } + }break; + + case LS_pound: + S.token.flags = 0; + switch (c){ + case '#': S.token.type = CPP_PP_CONCAT; break; + default: + S.token.type = CPP_PP_STRINGIFY; + pos_update_rule = PUR_back_one; + break; + } + break; + + case LS_pp: + { + --S.pos; + int start = 1; + + c = S.tb[start]; + while (start < S.tb_pos && (c == ' ' || c == '\t' || c == '\r' || c == '\v' || c == '\f')){ + ++start; + c = S.tb[start]; + } + + int word_size = S.tb_pos - start - 1; + Sub_Match_List_Result match; + match = sub_match_list(S.tb, S.tb_pos, start, preprops, word_size); + + if (match.index != -1){ + String_And_Flag data = preprops.data[match.index]; + S.token.type = (Cpp_Token_Type)data.flags; + S.token.flags = CPP_TFLAG_PP_DIRECTIVE; + S.pp_state = (unsigned char)cpp_pp_directive_to_state(S.token.type); + } + else{ + S.token.type = CPP_TOKEN_JUNK; S.token.flags = 0; } - } - }break; + }break; - case LS_pound: - S.token.flags = 0; - switch (c){ - case '#': S.token.type = CPP_PP_CONCAT; break; - default: - S.token.type = CPP_PP_STRINGIFY; - --S.pos; - break; - } - break; + case LS_number: + case LS_number0: + case LS_hex: + { + S.fsm.int_state = LSINT_default; + S.fsm.emit_token = 0; + --S.pos; + for (;;){ + for (; S.fsm.int_state < LSINT_count && S.pos < end_pos;){ + c = chunk[S.pos++]; + S.fsm.int_state = int_fsm_table[S.fsm.int_state + int_fsm_eq_classes[c]]; + } + S.fsm.emit_token = (S.fsm.int_state >= LSINT_count); - case LS_pp: - { - --S.pos; - int start = 1; - - c = S.tb[start]; - while (start < S.tb_pos && (c == ' ' || c == '\t' || c == '\r' || c == '\v' || c == '\f')){ - ++start; - c = S.tb[start]; - } - - int word_size = S.tb_pos - start - 1; - Sub_Match_List_Result match; - match = sub_match_list(S.tb, S.tb_pos, start, preprops, word_size); - - if (match.index != -1){ - String_And_Flag data = preprops.data[match.index]; - S.token.type = (Cpp_Token_Type)data.flags; - S.token.flags = CPP_TFLAG_PP_DIRECTIVE; - S.pp_state = (unsigned char)cpp_pp_directive_to_state(S.token.type); - } - else{ - S.token.type = CPP_TOKEN_JUNK; - S.token.flags = 0; - } - }break; - - case LS_number: - case LS_number0: - case LS_hex: - S.fsm.int_state = LSINT_default; - - { - S.fsm.emit_token = 0; - --S.pos; - for (;;){ - for (; S.fsm.emit_token == 0 && S.pos < end_pos;){ - c = chunk[S.pos++]; - S.fsm = int_fsm(S.fsm, c); + if (S.fsm.emit_token == 0){ + DrYield(5, 1); + } + else break; } - if (S.fsm.emit_token == 0){ - DrYield(5, 1); - } - else break; - } - --S.pos; - } - - S.token.type = CPP_TOKEN_INTEGER_CONSTANT; - S.token.flags = 0; - break; + --S.pos; + } - case LS_float: - case LS_crazy_float0: - case LS_crazy_float1: - S.token.type = CPP_TOKEN_FLOATING_CONSTANT; - S.token.flags = 0; - switch (c){ - case 'f': case 'F': - case 'l': case 'L':break; - default: --S.pos; break; - } - break; - - case LS_char: - S.token.type = CPP_TOKEN_CHARACTER_CONSTANT; - S.token.flags = 0; - break; - - case LS_char_multiline: - S.token.type = CPP_TOKEN_CHARACTER_CONSTANT; - S.token.flags = CPP_TFLAG_MULTILINE; - break; - - case LS_string: - S.token.type = CPP_TOKEN_STRING_CONSTANT; - S.token.flags = 0; - break; - - case LS_string_multiline: - S.token.type = CPP_TOKEN_STRING_CONSTANT; - S.token.flags = CPP_TFLAG_MULTILINE; - break; - - case LS_comment_pre: - S.token.flags = CPP_TFLAG_IS_OPERATOR; - switch (c){ - case '=': S.token.type = CPP_TOKEN_DIVEQ; break; - default: - S.token.type = CPP_TOKEN_DIV; - --S.pos; + S.token.type = CPP_TOKEN_INTEGER_CONSTANT; + S.token.flags = 0; break; - } - break; - case LS_comment: case LS_comment_block_ending: - S.token.type = CPP_TOKEN_COMMENT; - S.token.flags = 0; - pos_update_rule = PUR_unget_whitespace; - break; - - case LS_error_message: - S.token.type = CPP_TOKEN_ERROR_MESSAGE; - S.token.flags = 0; - pos_update_rule = PUR_unget_whitespace; - break; - - case LS_dot: - S.token.flags = CPP_TFLAG_IS_OPERATOR; - switch (c){ - case '*': S.token.type = CPP_TOKEN_PTRDOT; break; - default: - S.token.type = CPP_TOKEN_DOT; - --S.pos; + case LS_float: + case LS_crazy_float0: + case LS_crazy_float1: + S.token.type = CPP_TOKEN_FLOATING_CONSTANT; + S.token.flags = 0; + switch (c){ + case 'f': case 'F': + case 'l': case 'L':break; + default: + pos_update_rule = PUR_back_one; + break; + } break; - } - break; - case LS_ellipsis: - switch (c){ - case '.': + case LS_char: + S.token.type = CPP_TOKEN_CHARACTER_CONSTANT; + S.token.flags = 0; + break; + + case LS_char_multiline: + S.token.type = CPP_TOKEN_CHARACTER_CONSTANT; + S.token.flags = CPP_TFLAG_MULTILINE; + break; + + case LS_string: + S.token.type = CPP_TOKEN_STRING_CONSTANT; + S.token.flags = 0; + break; + + case LS_string_multiline: + S.token.type = CPP_TOKEN_STRING_CONSTANT; + S.token.flags = CPP_TFLAG_MULTILINE; + break; + + case LS_comment_pre: S.token.flags = CPP_TFLAG_IS_OPERATOR; - S.token.type = CPP_TOKEN_ELLIPSIS; + switch (c){ + case '=': S.token.type = CPP_TOKEN_DIVEQ; break; + default: + S.token.type = CPP_TOKEN_DIV; + pos_update_rule = PUR_back_one; + break; + } break; - default: - S.token.type = CPP_TOKEN_JUNK; - --S.pos; + case LS_comment: case LS_comment_block_ending: + S.token.type = CPP_TOKEN_COMMENT; + S.token.flags = 0; + pos_update_rule = PUR_unget_whitespace; break; - } - break; - case LS_less: - S.token.flags = CPP_TFLAG_IS_OPERATOR; - switch (c){ - case '=': S.token.type = CPP_TOKEN_LESSEQ; break; - default: - S.token.type = CPP_TOKEN_LESS; - --S.pos; + case LS_error_message: + S.token.type = CPP_TOKEN_ERROR_MESSAGE; + S.token.flags = 0; + pos_update_rule = PUR_unget_whitespace; break; - } - break; - case LS_less_less: - S.token.flags = CPP_TFLAG_IS_OPERATOR; - switch (c){ - case '=': S.token.type = CPP_TOKEN_LSHIFTEQ; break; - default: - S.token.type = CPP_TOKEN_LSHIFT; - --S.pos; + case LS_dot: + S.token.flags = CPP_TFLAG_IS_OPERATOR; + switch (c){ + case '*': S.token.type = CPP_TOKEN_PTRDOT; break; + default: + S.token.type = CPP_TOKEN_DOT; + pos_update_rule = PUR_back_one; + break; + } break; - } - break; - case LS_more: - S.token.flags = CPP_TFLAG_IS_OPERATOR; - switch (c){ - case '=': S.token.type = CPP_TOKEN_GRTREQ; break; - default: - S.token.type = CPP_TOKEN_GRTR; - --S.pos; - break; - } - break; + case LS_ellipsis: + switch (c){ + case '.': + S.token.flags = CPP_TFLAG_IS_OPERATOR; + S.token.type = CPP_TOKEN_ELLIPSIS; + break; - case LS_more_more: - S.token.flags = CPP_TFLAG_IS_OPERATOR; - switch (c){ - case '=': S.token.type = CPP_TOKEN_RSHIFTEQ; break; - default: - S.token.type = CPP_TOKEN_RSHIFT; - --S.pos; + default: + S.token.type = CPP_TOKEN_JUNK; + pos_update_rule = PUR_back_one; + break; + } break; - } - break; - case LS_minus: - S.token.flags = CPP_TFLAG_IS_OPERATOR; - switch (c){ - case '-': S.token.type = CPP_TOKEN_DECREMENT; break; - case '=': S.token.type = CPP_TOKEN_SUBEQ; break; - default: - S.token.type = CPP_TOKEN_MINUS; - --S.pos; + case LS_less: + S.token.flags = CPP_TFLAG_IS_OPERATOR; + switch (c){ + case '=': S.token.type = CPP_TOKEN_LESSEQ; break; + default: + S.token.type = CPP_TOKEN_LESS; + pos_update_rule = PUR_back_one; + break; + } break; - } - break; - case LS_arrow: - S.token.flags = CPP_TFLAG_IS_OPERATOR; - switch (c){ - case '*': S.token.type = CPP_TOKEN_PTRARROW; break; - default: - S.token.type = CPP_TOKEN_ARROW; - --S.pos; + case LS_less_less: + S.token.flags = CPP_TFLAG_IS_OPERATOR; + switch (c){ + case '=': S.token.type = CPP_TOKEN_LSHIFTEQ; break; + default: + S.token.type = CPP_TOKEN_LSHIFT; + pos_update_rule = PUR_back_one; + break; + } break; - } - break; - case LS_and: - S.token.flags = CPP_TFLAG_IS_OPERATOR; - switch (c){ - case '&': S.token.type = CPP_TOKEN_AND; break; - case '=': S.token.type = CPP_TOKEN_ANDEQ; break; - default: - S.token.type = CPP_TOKEN_AMPERSAND; - --S.pos; + case LS_more: + S.token.flags = CPP_TFLAG_IS_OPERATOR; + switch (c){ + case '=': S.token.type = CPP_TOKEN_GRTREQ; break; + default: + S.token.type = CPP_TOKEN_GRTR; + pos_update_rule = PUR_back_one; + break; + } break; - } - break; - case LS_or: - S.token.flags = CPP_TFLAG_IS_OPERATOR; - switch (c){ - case '|': S.token.type = CPP_TOKEN_OR; break; - case '=': S.token.type = CPP_TOKEN_OREQ; break; - default: - S.token.type = CPP_TOKEN_BIT_OR; - --S.pos; + case LS_more_more: + S.token.flags = CPP_TFLAG_IS_OPERATOR; + switch (c){ + case '=': S.token.type = CPP_TOKEN_RSHIFTEQ; break; + default: + S.token.type = CPP_TOKEN_RSHIFT; + pos_update_rule = PUR_back_one; + break; + } break; - } - break; - case LS_plus: - S.token.flags = CPP_TFLAG_IS_OPERATOR; - switch (c){ - case '+': S.token.type = CPP_TOKEN_INCREMENT; break; - case '=': S.token.type = CPP_TOKEN_ADDEQ; break; - default: - S.token.type = CPP_TOKEN_PLUS; - --S.pos; + case LS_minus: + S.token.flags = CPP_TFLAG_IS_OPERATOR; + switch (c){ + case '-': S.token.type = CPP_TOKEN_DECREMENT; break; + case '=': S.token.type = CPP_TOKEN_SUBEQ; break; + default: + S.token.type = CPP_TOKEN_MINUS; + pos_update_rule = PUR_back_one; + break; + } break; - } - break; - case LS_colon: - S.token.flags = CPP_TFLAG_IS_OPERATOR; - switch (c){ - case ':': S.token.type = CPP_TOKEN_SCOPE; break; - default: - S.token.type = CPP_TOKEN_COLON; - --S.pos; + case LS_arrow: + S.token.flags = CPP_TFLAG_IS_OPERATOR; + switch (c){ + case '*': S.token.type = CPP_TOKEN_PTRARROW; break; + default: + S.token.type = CPP_TOKEN_ARROW; + pos_update_rule = PUR_back_one; + break; + } break; - } - break; - case LS_star: - S.token.flags = CPP_TFLAG_IS_OPERATOR; - switch (c){ - case '=': S.token.type = CPP_TOKEN_MULEQ; break; - default: - S.token.type = CPP_TOKEN_STAR; - --S.pos; + case LS_and: + S.token.flags = CPP_TFLAG_IS_OPERATOR; + switch (c){ + case '&': S.token.type = CPP_TOKEN_AND; break; + case '=': S.token.type = CPP_TOKEN_ANDEQ; break; + default: + S.token.type = CPP_TOKEN_AMPERSAND; + pos_update_rule = PUR_back_one; + break; + } break; - } - break; - case LS_modulo: - S.token.flags = CPP_TFLAG_IS_OPERATOR; - switch (c){ - case '=': S.token.type = CPP_TOKEN_MODEQ; break; - default: - S.token.type = CPP_TOKEN_MOD; - --S.pos; + case LS_or: + S.token.flags = CPP_TFLAG_IS_OPERATOR; + switch (c){ + case '|': S.token.type = CPP_TOKEN_OR; break; + case '=': S.token.type = CPP_TOKEN_OREQ; break; + default: + S.token.type = CPP_TOKEN_BIT_OR; + pos_update_rule = PUR_back_one; + break; + } break; - } - break; - case LS_caret: - S.token.flags = CPP_TFLAG_IS_OPERATOR; - switch (c){ - case '=': S.token.type = CPP_TOKEN_XOREQ; break; - default: - S.token.type = CPP_TOKEN_BIT_XOR; - --S.pos; + case LS_plus: + S.token.flags = CPP_TFLAG_IS_OPERATOR; + switch (c){ + case '+': S.token.type = CPP_TOKEN_INCREMENT; break; + case '=': S.token.type = CPP_TOKEN_ADDEQ; break; + default: + S.token.type = CPP_TOKEN_PLUS; + pos_update_rule = PUR_back_one; + break; + } break; - } - break; - case LS_eq: - S.token.flags = CPP_TFLAG_IS_OPERATOR; - switch (c){ - case '=': S.token.type = CPP_TOKEN_EQEQ; break; - default: - S.token.type = CPP_TOKEN_EQ; - --S.pos; + case LS_colon: + S.token.flags = CPP_TFLAG_IS_OPERATOR; + switch (c){ + case ':': S.token.type = CPP_TOKEN_SCOPE; break; + default: + S.token.type = CPP_TOKEN_COLON; + pos_update_rule = PUR_back_one; + break; + } break; - } - break; - case LS_bang: - S.token.flags = CPP_TFLAG_IS_OPERATOR; - switch (c){ - case '=': S.token.type = CPP_TOKEN_NOTEQ; break; - default: - S.token.type = CPP_TOKEN_NOT; - --S.pos; + case LS_star: + S.token.flags = CPP_TFLAG_IS_OPERATOR; + switch (c){ + case '=': S.token.type = CPP_TOKEN_MULEQ; break; + default: + S.token.type = CPP_TOKEN_STAR; + pos_update_rule = PUR_back_one; + break; + } + break; + + case LS_modulo: + S.token.flags = CPP_TFLAG_IS_OPERATOR; + switch (c){ + case '=': S.token.type = CPP_TOKEN_MODEQ; break; + default: + S.token.type = CPP_TOKEN_MOD; + pos_update_rule = PUR_back_one; + break; + } + break; + + case LS_caret: + S.token.flags = CPP_TFLAG_IS_OPERATOR; + switch (c){ + case '=': S.token.type = CPP_TOKEN_XOREQ; break; + default: + S.token.type = CPP_TOKEN_BIT_XOR; + pos_update_rule = PUR_back_one; + break; + } + break; + + case LS_eq: + S.token.flags = CPP_TFLAG_IS_OPERATOR; + switch (c){ + case '=': S.token.type = CPP_TOKEN_EQEQ; break; + default: + S.token.type = CPP_TOKEN_EQ; + pos_update_rule = PUR_back_one; + break; + } + break; + + case LS_bang: + S.token.flags = CPP_TFLAG_IS_OPERATOR; + switch (c){ + case '=': S.token.type = CPP_TOKEN_NOTEQ; break; + default: + S.token.type = CPP_TOKEN_NOT; + pos_update_rule = PUR_back_one; + break; + } break; } - break; - } - - switch (pos_update_rule){ - case PUR_unget_whitespace: - c = chunk[--S.pos]; - while (c == ' ' || c == '\n' || c == '\t' || c == '\r' || c == '\v' || c == '\f'){ + + switch (pos_update_rule){ + case PUR_back_one: + --S.pos; + break; + + case PUR_unget_whitespace: c = chunk[--S.pos]; - } - ++S.pos; - break; - } - - if ((S.token.flags & CPP_TFLAG_PP_DIRECTIVE) == 0){ - switch (S.pp_state){ - case LSPP_include: - if (S.token.type != CPP_TOKEN_INCLUDE_FILE){ - S.token.type = CPP_TOKEN_JUNK; + while (c == ' ' || c == '\n' || c == '\t' || c == '\r' || c == '\v' || c == '\f'){ + c = chunk[--S.pos]; } - S.pp_state = LSPP_junk; - break; - - case LSPP_macro_identifier: - if (S.fsm.state != LS_identifier){ - S.token.type = CPP_TOKEN_JUNK; - S.pp_state = LSPP_junk; - } - else{ - S.pp_state = LSPP_body; - } - break; - - case LSPP_identifier: - if (S.fsm.state != LS_identifier){ - S.token.type = CPP_TOKEN_JUNK; - } - S.pp_state = LSPP_junk; - break; - - case LSPP_number: - if (S.token.type != CPP_TOKEN_INTEGER_CONSTANT){ - S.token.type = CPP_TOKEN_JUNK; - S.pp_state = LSPP_junk; - } - else{ - S.pp_state = LSPP_include; - } - break; - - case LSPP_junk: - S.token.type = CPP_TOKEN_JUNK; + ++S.pos; break; } + + if ((S.token.flags & CPP_TFLAG_PP_DIRECTIVE) == 0){ + switch (S.pp_state){ + case LSPP_include: + if (S.token.type != CPP_TOKEN_INCLUDE_FILE){ + S.token.type = CPP_TOKEN_JUNK; + } + S.pp_state = LSPP_junk; + break; + + case LSPP_macro_identifier: + if (S.fsm.state != LS_identifier){ + S.token.type = CPP_TOKEN_JUNK; + S.pp_state = LSPP_junk; + } + else{ + S.pp_state = LSPP_body; + } + break; + + case LSPP_identifier: + if (S.fsm.state != LS_identifier){ + S.token.type = CPP_TOKEN_JUNK; + } + S.pp_state = LSPP_junk; + break; + + case LSPP_number: + if (S.token.type != CPP_TOKEN_INTEGER_CONSTANT){ + S.token.type = CPP_TOKEN_JUNK; + S.pp_state = LSPP_junk; + } + else{ + S.pp_state = LSPP_include; + } + break; + + case LSPP_junk: + S.token.type = CPP_TOKEN_JUNK; + break; + } + } } - + if (S.fsm.emit_token){ S.token.start = S.token_start; if (S.pos_overide){ @@ -1493,8 +1035,6 @@ cpp_lex_nonalloc(Lex_Data *S_ptr, char *chunk, int size, Cpp_Token_Stack *token_ #undef DrReturn #undef DrCase -} - #endif // BOTTOM diff --git a/test/experiment.cpp b/test/experiment.cpp index f850b67e..14426e4c 100644 --- a/test/experiment.cpp +++ b/test/experiment.cpp @@ -17,7 +17,10 @@ #include "../4cpp_lexer_types.h" #define FCPP_LEXER_IMPLEMENTATION #include "../4cpp_lexer.h" -#include "4cpp_new_lexer.h" + +namespace new_lex{ +# include "4cpp_new_lexer.h" +} #include #include diff --git a/test/fsm_table_generator.cpp b/test/fsm_table_generator.cpp index 786984ad..23b32bfa 100644 --- a/test/fsm_table_generator.cpp +++ b/test/fsm_table_generator.cpp @@ -528,6 +528,40 @@ struct FSM_Tables{ unsigned short state_count; }; +void +do_table_reduction(FSM_Tables *table, unsigned short state_count){ + { + table->eq_class_counter = 0; + unsigned char *c_line = table->full_transition_table; + for (unsigned short c = 0; c < 256; ++c){ + if (table->marks[c] == 0){ + table->eq_class[c] = table->eq_class_counter; + table->eq_class_rep[table->eq_class_counter] = (unsigned char)c; + unsigned char *c2_line = c_line + state_count; + for (unsigned short c2 = c + 1; c2 < 256; ++c2){ + if (memcmp(c_line, c2_line, state_count) == 0){ + table->marks[c2] = 1; + table->eq_class[c2] = table->eq_class_counter; + } + c2_line += state_count; + } + ++table->eq_class_counter; + } + c_line += state_count; + } + } + + table->reduced_transition_table = (unsigned char*)malloc(state_count * table->eq_class_counter); + { + unsigned char *r_line = table->reduced_transition_table; + for (unsigned short eq = 0; eq < table->eq_class_counter; ++eq){ + unsigned char *u_line = table->full_transition_table + state_count * table->eq_class_rep[eq]; + memcpy(r_line, u_line, state_count); + r_line += state_count; + } + } +} + FSM_Tables generate_whitespace_skip_table(){ unsigned char state_count = LSPP_count; @@ -551,36 +585,36 @@ generate_whitespace_skip_table(){ } } - table.eq_class_counter = 0; - unsigned char *c_line = table.full_transition_table; - for (unsigned short c = 0; c < 256; ++c){ - if (table.marks[c] == 0){ - table.eq_class[c] = table.eq_class_counter; - table.eq_class_rep[table.eq_class_counter] = (unsigned char)c; - unsigned char *c2_line = c_line + state_count; - for (unsigned short c2 = c + 1; c2 < 256; ++c2){ - if (memcmp(c_line, c2_line, state_count) == 0){ - table.marks[c2] = 1; - table.eq_class[c2] = table.eq_class_counter; - } - c2_line += state_count; - } - ++table.eq_class_counter; - } - c_line += state_count; - } + do_table_reduction(&table, state_count); - table.reduced_transition_table = (unsigned char*)malloc(state_count * table.eq_class_counter); - i = 0; - for (unsigned short eq = 0; eq < table.eq_class_counter; ++eq){ + return(table); +} + +FSM_Tables +generate_int_table(){ + unsigned char state_count = LSINT_count; + FSM_Tables table; + table.full_transition_table = (unsigned char*)malloc(state_count * 256); + table.marks = (unsigned char*)malloc(state_count * 256); + table.eq_class = (unsigned char*)malloc(state_count * 256); + table.eq_class_rep = (unsigned char*)malloc(state_count * 256); + table.state_count = state_count; + memset(table.marks, 0, 256); + + int i = 0; + Lex_FSM fsm = {0}; + Lex_FSM new_fsm; + for (unsigned short c = 0; c < 256; ++c){ for (unsigned char state = 0; state < state_count; ++state){ - wfsm.pp_state = state; - wfsm.white_done = 0; - new_wfsm = whitespace_skip_fsm(wfsm, table.eq_class_rep[eq]); - table.reduced_transition_table[i++] = new_wfsm.pp_state + state_count*new_wfsm.white_done; + fsm.int_state = state; + fsm.emit_token = 0; + new_fsm = int_fsm(fsm, (unsigned char)c); + table.full_transition_table[i++] = new_fsm.int_state + state_count*new_fsm.emit_token; } } - + + do_table_reduction(&table, state_count); + return(table); } @@ -607,35 +641,7 @@ generate_fsm_table(unsigned char pp_state){ } } - table.eq_class_counter = 0; - unsigned char *c_line = table.full_transition_table; - for (unsigned short c = 0; c < 256; ++c){ - if (table.marks[c] == 0){ - table.eq_class[c] = table.eq_class_counter; - table.eq_class_rep[table.eq_class_counter] = (unsigned char)c; - unsigned char *c2_line = c_line + state_count; - for (unsigned short c2 = c + 1; c2 < 256; ++c2){ - if (memcmp(c_line, c2_line, state_count) == 0){ - table.marks[c2] = 1; - table.eq_class[c2] = table.eq_class_counter; - } - c2_line += state_count; - } - ++table.eq_class_counter; - } - c_line += state_count; - } - - table.reduced_transition_table = (unsigned char*)malloc(state_count * table.eq_class_counter); - i = 0; - for (unsigned short eq = 0; eq < table.eq_class_counter; ++eq){ - for (unsigned char state = 0; state < state_count; ++state){ - fsm.state = state; - fsm.emit_token = 0; - new_fsm = main_fsm(fsm, pp_state, table.eq_class_rep[eq]); - table.reduced_transition_table[i++] = new_fsm.state + state_count*new_fsm.emit_token; - } - } + do_table_reduction(&table, state_count); return(table); } @@ -686,13 +692,16 @@ int main(){ FSM_Tables wtables = generate_whitespace_skip_table(); render_fsm_table(file, wtables, "whitespace_fsm"); + FSM_Tables itables = generate_int_table(); + render_fsm_table(file, itables, "int_fsm"); + begin_table(file, "char", "multiline_state_table"); for (unsigned char state = 0; state < LS_count; ++state){ do_table_item(file, (state == LS_string_multiline || state == LS_char_multiline)); } end_row(file); end_table(file); - + for (int i = 0; i < ArrayCount(pp_names); ++i){ assert(i == pp_names[i].pp_state); FSM_Tables tables = generate_fsm_table(pp_names[i].pp_state);