Text editor object management change

This commit is contained in:
Krzosa Karol
2025-05-04 09:57:34 +02:00
parent 101e844ac5
commit 542b0f0a0a
10 changed files with 161 additions and 186 deletions

View File

@@ -180,17 +180,6 @@ Shader CreateShader(char *glsl_vshader, char *glsl_fshader) {
return result; return result;
} }
#define SLL_QUEUE_ADD_MOD(f, l, n, next) \
do { \
(n)->next = 0; \
if ((f) == 0) { \
(f) = (l) = (n); \
} else { \
(l) = (l)->next = (n); \
} \
} while (0)
#define SLL_QUEUE_ADD(f, l, n) SLL_QUEUE_ADD_MOD(f, l, n, next)
VertexNode2D *AllocVertexNode2D(Allocator allocator, VertexList2D *list) { VertexNode2D *AllocVertexNode2D(Allocator allocator, VertexList2D *list) {
VertexNode2D *node = AllocType(allocator, VertexNode2D); VertexNode2D *node = AllocType(allocator, VertexNode2D);
SLL_QUEUE_ADD(list->first, list->last, node); SLL_QUEUE_ADD(list->first, list->last, node);

View File

@@ -16,6 +16,8 @@ struct HistoryEntry {
struct Buffer { struct Buffer {
BufferID id; BufferID id;
Buffer *next;
Buffer *prev;
String name; String name;
Int change_id; Int change_id;
Int user_change_id; Int user_change_id;

View File

@@ -82,9 +82,8 @@ void Command_ListBuffers() {
Scratch scratch; Scratch scratch;
Array<String> strings = {scratch}; Array<String> strings = {scratch};
For(Buffers) { for (Buffer *it = FirstBuffer; it; it = it->next) {
Buffer *buffer = GetBuffer(it); String string = Format(scratch, "%.*s", FmtString(it->name));
String string = Format(scratch, "%.*s", FmtString(buffer->name));
Add(&strings, string); Add(&strings, string);
} }
String result = Merge(scratch, strings, "\n"); String result = Merge(scratch, strings, "\n");
@@ -145,8 +144,7 @@ void Command_Append(View *view, String16 string, bool scroll_to_end_if_cursor_on
}; };
Array<ViewInfo> view_info = {scratch}; Array<ViewInfo> view_info = {scratch};
For (Views) { for (View *it_view = FirstView; it_view; it_view = it_view->next) {
View *it_view = GetView(it);
if (it_view->active_buffer != buffer->id) { if (it_view->active_buffer != buffer->id) {
continue; continue;
} }
@@ -200,7 +198,7 @@ void ReportErrorf(const char *fmt, ...) {
// Set console view as active // Set console view as active
{ {
Window *w = GetWindowWithView(NullViewID); Window *w = GetWindow(NullWindowID);
if (!w) { if (!w) {
BSet bset = GetActiveMainSet(); BSet bset = GetActiveMainSet();
w = GetNextLayoutWindow(bset.window); w = GetNextLayoutWindow(bset.window);
@@ -229,7 +227,7 @@ void ReportWarningf(const char *fmt, ...) {
// Set console view as active // Set console view as active
{ {
Window *w = GetWindowWithView(NullViewID); Window *w = GetWindow(NullWindowID);
if (!w) { if (!w) {
BSet bset = GetActiveMainSet(); BSet bset = GetActiveMainSet();
w = GetNextLayoutWindow(bset.window); w = GetNextLayoutWindow(bset.window);

View File

@@ -109,7 +109,7 @@ void OnCommand(Event event) {
// Window cursor setting // Window cursor setting
// //
Scratch scratch; Scratch scratch;
Array<Int> order = GetWindowZOrder(scratch); Array<Window *> order = GetWindowZOrder(scratch);
{ {
static SDL_Cursor *SDL_MouseCursor; static SDL_Cursor *SDL_MouseCursor;
if (SDL_MouseCursor) { if (SDL_MouseCursor) {
@@ -119,10 +119,9 @@ void OnCommand(Event event) {
Vec2I mouse = MouseVec2I(); Vec2I mouse = MouseVec2I();
For(order) { For(order) {
Window *window = Windows[it].o; if (!it->visible) continue;
if (!window->visible) continue; bool mouse_in_total = CheckCollisionPointRec(mouse, it->total_rect);
bool mouse_in_total = CheckCollisionPointRec(mouse, window->total_rect); bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, it->scrollbar_rect);
bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, window->scrollbar_rect);
if (!IsDocumentSelectionValid() && (mouse_in_scrollbar || IsScrollbarSelectionValid())) { if (!IsDocumentSelectionValid() && (mouse_in_scrollbar || IsScrollbarSelectionValid())) {
SDL_MouseCursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_DEFAULT); SDL_MouseCursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_DEFAULT);
@@ -146,12 +145,11 @@ void OnCommand(Event event) {
Vec2I mouse = MouseVec2I(); Vec2I mouse = MouseVec2I();
For(order) { For(order) {
Window *window = Windows[it].o; if (!it->visible) continue;
if (!window->visible) continue;
bool mouse_in_window = CheckCollisionPointRec(mouse, window->total_rect); bool mouse_in_window = CheckCollisionPointRec(mouse, it->total_rect);
if (mouse_in_window) { if (mouse_in_window) {
View *view = GetView(window->active_view); View *view = GetView(it->active_view);
view->scroll.y -= (Int)(event.ywheel * 48); view->scroll.y -= (Int)(event.ywheel * 48);
view->scroll.x += (Int)(event.xwheel * 48); view->scroll.x += (Int)(event.xwheel * 48);
break; break;
@@ -212,11 +210,12 @@ void OnCommand(Event event) {
if (MousePress()) { if (MousePress()) {
Vec2I mouse = MouseVec2I(); Vec2I mouse = MouseVec2I();
For(order) { For(order) {
Window *window = Windows[it].o; if (!it->visible) {
if (!window->visible) continue; continue;
bool mouse_in_document = CheckCollisionPointRec(mouse, window->document_rect); }
bool mouse_in_document = CheckCollisionPointRec(mouse, it->document_rect);
if (mouse_in_document) { if (mouse_in_document) {
ActiveWindow = window->id; ActiveWindow = it->id;
break; break;
} }
} }
@@ -313,22 +312,21 @@ void OnCommand(Event event) {
// @todo: it generally works ok but it moves the scrollbar a bit on click // @todo: it generally works ok but it moves the scrollbar a bit on click
// when mouse is not even moving // when mouse is not even moving
For(order) { For(order) {
Window *window = Windows[it].o; if (!it->visible) continue;
if (!window->visible) continue; bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, it->scrollbar_rect);
bool mouse_in_scrollbar = CheckCollisionPointRec(mouse, window->scrollbar_rect);
if (mouse_in_scrollbar) { if (mouse_in_scrollbar) {
ScrollbarSelected = window->id; ScrollbarSelected = it->id;
View *view = GetView(window->active_view); View *view = GetView(it->active_view);
Vec2 mouse_vec2 = MouseVec2(); Vec2 mouse_vec2 = MouseVec2();
Scroller s = ComputeScrollerRect(window); Scroller s = ComputeScrollerRect(it);
double size_y = (double)GetSize(window->scrollbar_rect).y; double size_y = (double)GetSize(it->scrollbar_rect).y;
double p = mouse_vec2.y - window->scrollbar_rect.min.y; double p = mouse_vec2.y - it->scrollbar_rect.min.y;
if (mouse_vec2.y < s.rect.min.y || mouse_vec2.y > s.rect.max.y) { if (mouse_vec2.y < s.rect.min.y || mouse_vec2.y > s.rect.max.y) {
view->scroll.y = (Int)(p / size_y * (double)s.line_count * (double)FontLineSpacing); view->scroll.y = (Int)(p / size_y * (double)s.line_count * (double)FontLineSpacing);
window->mouse_scroller_offset = -(double)GetSize(s.rect).y / 2.0 / size_y; it->mouse_scroller_offset = -(double)GetSize(s.rect).y / 2.0 / size_y;
} else { } else {
window->mouse_scroller_offset = (s.rect.min.y - p) / size_y; it->mouse_scroller_offset = (s.rect.min.y - p) / size_y;
} }
break; break;
} }

View File

@@ -286,12 +286,12 @@ int Lua_ListBuffers(lua_State *L) {
} }
int Lua_GetBufferList(lua_State *L) { int Lua_GetBufferList(lua_State *L) {
lua_createtable(L, 0, (int)Buffers.len); lua_createtable(L, 0, (int)BufferCount);
int i = 1; int i = 1;
For(Buffers) { for (Buffer *it = FirstBuffer; it; it = it->next) {
lua_pushinteger(L, i++); lua_pushinteger(L, i++);
lua_pushlstring(L, it.o->name.data, it.o->name.len); lua_pushlstring(L, it->name.data, it->name.len);
lua_settable(L, -3); /* 3rd element from the stack top */ lua_settable(L, -3); /* 3rd element from the stack top */
} }
/* We still have table left on top of the Lua stack. */ /* We still have table left on top of the Lua stack. */
@@ -301,7 +301,7 @@ int Lua_GetBufferList(lua_State *L) {
int Lua_BufferExists(lua_State *L) { int Lua_BufferExists(lua_State *L) {
String string = lua_tostring(L, 1); String string = lua_tostring(L, 1);
lua_pop(L, 1); lua_pop(L, 1);
Buffer *buffer = BufferNameExists(string); Buffer *buffer = GetBuffer(string);
lua_pushboolean(L, buffer != NULL); lua_pushboolean(L, buffer != NULL);
return 1; return 1;
} }
@@ -531,11 +531,12 @@ void ReloadLuaConfigs() {
ReloadStyle(); ReloadStyle();
ReloadFont(); ReloadFont();
For(Windows) { for (Window *it = FirstWindow; it; it = it->next) {
Window *window = it.o; if (!it->visible || it->absolute_position || it->is_title_bar || it->is_search_bar) {
if (!window->visible || window->absolute_position || window->is_title_bar || window->is_search_bar) continue; continue;
window->draw_scrollbar = StyleDrawScrollbar; }
window->draw_line_numbers = StyleDrawLineNumbers; it->draw_scrollbar = StyleDrawScrollbar;
it->draw_line_numbers = StyleDrawLineNumbers;
} }
} }

View File

@@ -2,9 +2,17 @@ WindowID WindowIDs;
BufferID BufferIDs; BufferID BufferIDs;
ViewID ViewIDs; ViewID ViewIDs;
Array<BufferID> Buffers; Window *FirstWindow;
Array<ViewID> Views; Window *LastWindow;
Array<WindowID> Windows; Int WindowCount;
View *FirstView;
View *LastView;
Int ViewCount;
Buffer *FirstBuffer;
Buffer *LastBuffer;
Int BufferCount;
BufferID NullBufferID; // +buffer BufferID NullBufferID; // +buffer
ViewID NullViewID; ViewID NullViewID;
@@ -68,38 +76,45 @@ inline WindowID AllocWindowID(Window *window) { return {WindowIDs.id++, window};
inline BufferID AllocBufferID(Buffer *buffer) { return {BufferIDs.id++, buffer}; } inline BufferID AllocBufferID(Buffer *buffer) { return {BufferIDs.id++, buffer}; }
inline Window *GetWindow(WindowID id) { inline Window *GetWindow(WindowID id) {
For(Windows) if (it == id) return it.o; for (Window *it = FirstWindow; it; it = it->next) {
return Windows[0].o; if (it->id == id) return it;
}
return FirstWindow;
} }
inline Buffer *GetBuffer(BufferID id) { inline Buffer *GetBuffer(BufferID id) {
For(Buffers) if (it == id) return it.o; for (Buffer *it = FirstBuffer; it; it = it->next) {
return Buffers[0].o; if (it->id == id) return it;
}
return FirstBuffer;
} }
inline Buffer *GetBufferStrict(BufferID id) { inline Buffer *GetBufferStrict(BufferID id) {
For(Buffers) if (it == id) return it.o; for (Buffer *it = FirstBuffer; it; it = it->next) {
if (it->id == id) return it;
}
return NULL; return NULL;
} }
inline Buffer *GetBuffer(String name) { inline Buffer *GetBuffer(String name) {
For(Buffers) if (it.o->name == name) return it.o; for (Buffer *it = FirstBuffer; it; it = it->next) {
return Buffers[0].o; if (it->name == name) return it;
}
return FirstBuffer;
} }
inline Buffer *GetBufferStrict(String name) { inline Buffer *GetBufferStrict(String name) {
For(Buffers) if (it.o->name == name) return it.o; for (Buffer *it = FirstBuffer; it; it = it->next) {
return NULL; if (it->name == name) return it;
} }
inline Buffer *BufferNameExists(String name) {
For(Buffers) if (it.o->name == name) return it.o;
return NULL; return NULL;
} }
inline View *GetView(ViewID id) { inline View *GetView(ViewID id) {
For(Views) if (it == id) return it.o; for (View *it = FirstView; it; it = it->next) {
return Views[0].o; if (it->id == id) return it;
}
return FirstView;
} }
inline bool IsNull(Buffer *buffer) { return buffer->id.id == NullBufferID.id; } inline bool IsNull(Buffer *buffer) { return buffer->id.id == NullBufferID.id; }
@@ -119,7 +134,8 @@ void InitBuffer(Allocator allocator, Buffer *buffer, String name, Int size = 409
Buffer *CreateBuffer(Allocator allocator, String name, Int size) { Buffer *CreateBuffer(Allocator allocator, String name, Int size) {
Buffer *result = AllocType(allocator, Buffer); Buffer *result = AllocType(allocator, Buffer);
InitBuffer(allocator, result, name, size); InitBuffer(allocator, result, name, size);
Add(&Buffers, result->id); DLL_QUEUE_ADD(FirstBuffer, LastBuffer, result);
BufferCount += 1;
return result; return result;
} }
@@ -131,13 +147,14 @@ Buffer *CreateTempBuffer(Allocator allocator, Int size = 4096) {
return result; return result;
} }
Window *CreateWindow(bool insert_into_windows = true) { Window *CreateWindow() {
Window *w = AllocType(Perm, Window); Window *w = AllocType(Perm, Window);
w->visible = true; w->visible = true;
w->draw_scrollbar = StyleDrawScrollbar; w->draw_scrollbar = StyleDrawScrollbar;
w->draw_line_numbers = StyleDrawLineNumbers; w->draw_line_numbers = StyleDrawLineNumbers;
w->id = AllocWindowID(w); w->id = AllocWindowID(w);
if (insert_into_windows) Add(&Windows, w->id); DLL_QUEUE_ADD(FirstWindow, LastWindow, w);
WindowCount += 1;
return w; return w;
} }
@@ -148,49 +165,56 @@ View *CreateView(BufferID active_buffer) {
view->active_buffer = active_buffer; view->active_buffer = active_buffer;
view->carets.allocator = al; view->carets.allocator = al;
Add(&view->carets, {0, 0}); Add(&view->carets, {0, 0});
Add(&Views, view->id); DLL_QUEUE_ADD(FirstView, LastView, view);
ViewCount += 1;
return view; return view;
} }
View *FindView(BufferID buffer_id) { View *FindView(BufferID buffer_id, View *default_view = NULL) {
For(Views) { for (View *it = FirstView; it; it = it->next) {
View *view = it.o; if (it->active_buffer == buffer_id) {
if (view->active_buffer == buffer_id) { return it;
return view;
} }
} }
return NULL; return default_view;
} }
Window *GetWindowWithView(ViewID view_id) { Window *FindWindow(ViewID view_id, Window *default_window = NULL) {
For(Windows) { for (Window *it = FirstWindow; it; it = it->next) {
Window *window = it.o; if (it->active_view == view_id) {
if (window->active_view.id == view_id.id) { return it;
return window;
} }
} }
return NULL; return default_window;
} }
Window *GetWindowWithView(String buffer_name) { Window *FindWindow(String buffer_name, Window *default_window = NULL) {
For(Windows) { for (Window *it = FirstWindow; it; it = it->next) {
View *it_view = GetView(it.o->active_view); View *it_view = GetView(it->active_view);
Buffer *it_buffer = GetBuffer(it_view->active_buffer); Buffer *it_buffer = GetBuffer(it_view->active_buffer);
if (it_buffer->name == buffer_name) { if (it_buffer->name == buffer_name) {
return it.o; return it;
} }
} }
return default_window;
}
Window *FindWindow(BufferID buffer_id) {
for (Window *it = FirstWindow; it; it = it->next) {
View *view = GetView(it->active_view);
if (view->active_buffer == buffer_id) return it;
}
return NULL; return NULL;
} }
View *FindViewWithBufferName(String name) { View *FindView(String name, View *default_view = NULL) {
For(Views) { for (View *it = FirstView; it; it = it->next) {
Buffer *buffer = GetBuffer(it.o->active_buffer); Buffer *buffer = GetBuffer(it->active_buffer);
if (buffer->name == name) { if (buffer->name == name) {
return it.o; return it;
} }
} }
return NULL; return default_view;
} }
Window *GetTitlebarWindow(WindowID id) { Window *GetTitlebarWindow(WindowID id) {
@@ -379,14 +403,14 @@ View *OpenBufferView(String name) {
} }
View *WindowOpenBufferView(Window *new_parent_window, String name) { View *WindowOpenBufferView(Window *new_parent_window, String name) {
View *view = FindViewWithBufferName(name); View *view = FindView(name);
if (!view) { if (!view) {
View *result = OpenBufferView(name); View *result = OpenBufferView(name);
new_parent_window->active_view = result->id; new_parent_window->active_view = result->id;
return result; return result;
} }
Window *window = GetWindowWithView(view->id); Window *window = FindWindow(view->id);
if (!window) { if (!window) {
new_parent_window->active_view = view->id; new_parent_window->active_view = view->id;
return view; return view;
@@ -402,34 +426,18 @@ View *WindowOpenBufferView(Window *new_parent_window, String name) {
} }
Window *ViewIsReferenced(ViewID view) { Window *ViewIsReferenced(ViewID view) {
For(Windows) { for (Window *it = FirstWindow; it; it = it->next) {
Window *window = it.o; if (it->active_view == view || it->active_goto_list == view) return it;
if (window->active_view == view) return window;
if (window->active_goto_list == view) return window;
} }
return NULL; return NULL;
} }
View *BufferIsReferenced(BufferID buffer_id) { View *BufferIsReferenced(BufferID buffer_id) {
For(Views) { return FindView(buffer_id);
View *view = it.o;
if (view->active_buffer == buffer_id) return view;
}
return NULL;
}
Window *BufferIsInWindow(BufferID buffer_id) {
For(Windows) {
Window *window = it.o;
View *view = GetView(window->active_view);
if (view->active_buffer == buffer_id) return window;
}
return NULL;
} }
Window *BufferIsCrumb(BufferID buffer_id) { Window *BufferIsCrumb(BufferID buffer_id) {
For(Windows) { for (Window *window = FirstWindow; window; window = window->next) {
Window *window = it.o;
For(window->goto_history) if (it.buffer_id == buffer_id) return window; For(window->goto_history) if (it.buffer_id == buffer_id) return window;
For(window->goto_redo) if (it.buffer_id == buffer_id) return window; For(window->goto_redo) if (it.buffer_id == buffer_id) return window;
} }
@@ -458,12 +466,11 @@ void GarbageCollect() {
// } // }
// } // }
For(Buffers) { for (Buffer *it = FirstBuffer; it; it = it->next) {
Buffer *buffer = GetBuffer(it); if (it->file_mod_time) {
if (buffer->file_mod_time) { int64_t new_file_mod_time = GetFileModTime(it->name);
int64_t new_file_mod_time = GetFileModTime(buffer->name); if (it->file_mod_time != new_file_mod_time) {
if (buffer->file_mod_time != new_file_mod_time) { it->changed_on_disk = true;
buffer->changed_on_disk = true;
} }
} }
} }

View File

@@ -1,5 +1,6 @@
#include <math.h> #include <math.h>
#define BASIC_IMPL #define BASIC_IMPL
#include "basic/basic.h" #include "basic/basic.h"
#include "basic/filesystem.h" #include "basic/filesystem.h"
@@ -7,6 +8,7 @@
#include "basic/math_int.cpp" #include "basic/math_int.cpp"
#include "basic/math.cpp" #include "basic/math.cpp"
#include "profiler/profiler.cpp" #include "profiler/profiler.cpp"
#include "build_tool/standalone_libraries/linked_list.h"
#include "SDL3/SDL.h" #include "SDL3/SDL.h"
#include "external/glad/glad.h" #include "external/glad/glad.h"
@@ -163,17 +165,16 @@ void Update(Event event) {
LayoutWindows(event.xwindow, event.ywindow); LayoutWindows(event.xwindow, event.ywindow);
Scratch scratch; Scratch scratch;
Array<Int> order = GetWindowZOrder(scratch); Array<Window *> order = GetWindowZOrder(scratch);
For(order) { For(order) {
Window *window = Windows[it].o; if (!it->visible) continue;
if (!window->visible) continue; View *view = GetView(it->active_view);
View *view = GetView(window->active_view);
view->main_caret_on_begin_frame = view->carets[0]; view->main_caret_on_begin_frame = view->carets[0];
view->update_scroll = true; view->update_scroll = true;
} }
OnCommand(event); OnCommand(event);
For(Windows) if (it.o->is_title_bar) ReplaceTitleBarData(it.o); for (Window *it = FirstWindow; it; it = it->next) if (it->is_title_bar) ReplaceTitleBarData(it);
UpdateProcesses(); UpdateProcesses();
UpdateCo(&event); UpdateCo(&event);
ReloadLuaConfigs(); ReloadLuaConfigs();
@@ -182,11 +183,10 @@ void Update(Event event) {
GarbageCollect(); GarbageCollect();
For(IterateInReverse(&order)) { For(IterateInReverse(&order)) {
Window *window = Windows[it].o; if (!it->visible) continue;
if (!window->visible) continue;
View *view = GetView(window->active_view); View *view = GetView(it->active_view);
UpdateScroll(window, !AreEqual(view->main_caret_on_begin_frame, view->carets[0]) && view->update_scroll); UpdateScroll(it, !AreEqual(view->main_caret_on_begin_frame, view->carets[0]) && view->update_scroll);
} }
} }
@@ -408,11 +408,10 @@ int main(int argc, char **argv)
Event *event = GetLast(frame_events); Event *event = GetLast(frame_events);
LayoutWindows(event->xwindow, event->ywindow); // This is here to render changes in title bar size without a frame of delay LayoutWindows(event->xwindow, event->ywindow); // This is here to render changes in title bar size without a frame of delay
BeginFrameRender(event->xwindow, event->ywindow); BeginFrameRender(event->xwindow, event->ywindow);
Array<Int> order = GetWindowZOrder(scratch); Array<Window *> order = GetWindowZOrder(scratch);
For(IterateInReverse(&order)) { For(IterateInReverse(&order)) {
Window *window = Windows[it].o; if (!it->visible) continue;
if (!window->visible) continue; DrawWindow(it, *GetLast(frame_events));
DrawWindow(window, *GetLast(frame_events));
} }
EndFrameRender(event->xwindow, event->ywindow, ColorBackground); EndFrameRender(event->xwindow, event->ywindow, ColorBackground);
SDL_GL_SwapWindow(SDLWindow); SDL_GL_SwapWindow(SDLWindow);

View File

@@ -5,6 +5,8 @@ struct WindowID { Int id; Window *o; }; // @todo: change name of object to somet
struct View { struct View {
ViewID id; ViewID id;
BufferID active_buffer; BufferID active_buffer;
View *next;
View *prev;
Vec2I scroll; Vec2I scroll;
Array<Caret> carets; Array<Caret> carets;
@@ -21,6 +23,8 @@ struct GotoCrumb {
struct Window { struct Window {
WindowID id; WindowID id;
ViewID active_view; ViewID active_view;
Window *next;
Window *prev;
WindowID title_bar_window; WindowID title_bar_window;
Int title_bar_last_buffer_change_id; // @todo: bring back the changes to title bar? Int title_bar_last_buffer_change_id; // @todo: bring back the changes to title bar?

View File

@@ -15,7 +15,6 @@ void UpdateDebugBuffer() {
float xmouse, ymouse; float xmouse, ymouse;
SDL_GetMouseState(&xmouse, &ymouse); SDL_GetMouseState(&xmouse, &ymouse);
RawAppendf(buffer, "mouse: [%f, %f]\n", xmouse, ymouse); RawAppendf(buffer, "mouse: [%f, %f]\n", xmouse, ymouse);
RawAppendf(buffer, "window count: %d view count: %d buffer count: %d\n", (int)Windows.len, (int)Views.len, (int)Buffers.len);
RawAppendf(buffer, "C:/Work/text_editor/src/text_editor/text_editor.cpp\n"); RawAppendf(buffer, "C:/Work/text_editor/src/text_editor/text_editor.cpp\n");
RawAppendf(buffer, "working dir: %.*s\n", FmtString(WorkingDir)); RawAppendf(buffer, "working dir: %.*s\n", FmtString(WorkingDir));
RawAppendf(buffer, "exe dir: %.*s\n", FmtString(ExeDir)); RawAppendf(buffer, "exe dir: %.*s\n", FmtString(ExeDir));

View File

@@ -5,17 +5,18 @@ struct VisualColumn {
Array<VisualColumn> GetVisualColumns(Allocator allocator) { Array<VisualColumn> GetVisualColumns(Allocator allocator) {
Array<VisualColumn> columns = {allocator}; Array<VisualColumn> columns = {allocator};
For(Windows) { for (Window *it = FirstWindow; it; it = it->next) {
Window *window = it.o; if (!it->visible || it->absolute_position || it->is_title_bar || it->is_search_bar) {
if (!window->visible || window->absolute_position || window->is_title_bar || window->is_search_bar) continue; continue;
}
if (window->is_column) { if (it->is_column) {
Add(&columns, {window, {allocator}}); Add(&columns, {it, {allocator}});
VisualColumn *col = GetLast(columns); VisualColumn *col = GetLast(columns);
Add(&col->rows, window); Add(&col->rows, it);
} else if (columns.len) { } else if (columns.len) {
VisualColumn *col = GetLast(columns); VisualColumn *col = GetLast(columns);
Add(&col->rows, window); Add(&col->rows, it);
} }
} }
return columns; return columns;
@@ -23,11 +24,10 @@ Array<VisualColumn> GetVisualColumns(Allocator allocator) {
Window *GetLayoutWindow(int n) { Window *GetLayoutWindow(int n) {
int i = 0; int i = 0;
For(Windows) { for (Window *it = FirstWindow; it; it = it->next) {
Window *window = it.o; if (!it->is_column || !it->visible || it->absolute_position || it->is_title_bar || it->is_search_bar) continue;
if (!window->is_column || !window->visible || window->absolute_position || window->is_title_bar || window->is_search_bar) continue;
if (n == i) return window; if (n == i) return it;
i += 1; i += 1;
} }
return NULL; return NULL;
@@ -35,20 +35,19 @@ Window *GetLayoutWindow(int n) {
Window *GetNextLayoutWindow(Window *input_window) { Window *GetNextLayoutWindow(Window *input_window) {
bool found = false; bool found = false;
For(Windows) { for (Window *it = FirstWindow; it; it = it->next) {
Window *window = it.o; if (!it->is_column || !it->visible || it->absolute_position || it->is_title_bar || it->is_search_bar) continue;
if (!window->is_column || !window->visible || window->absolute_position || window->is_title_bar || window->is_search_bar) continue; if (found) return it;
if (found) return window; if (it->id == input_window->id) found = true;
if (window->id == input_window->id) found = true;
} }
return input_window; return input_window;
} }
Array<Int> GetWindowZOrder(Allocator allocator) { Array<Window *> GetWindowZOrder(Allocator allocator) {
Array<Int> order = {allocator}; Array<Window *> order = {allocator};
For(Windows) if (it.o->z == 2) Add(&order, GetIndex(Windows, it)); for (Window *it = FirstWindow; it; it = it->next) if (it->z == 2) Add(&order, it);
For(Windows) if (it.o->z == 1) Add(&order, GetIndex(Windows, it)); for (Window *it = FirstWindow; it; it = it->next) if (it->z == 1) Add(&order, it);
For(Windows) if (it.o->z == 0) Add(&order, GetIndex(Windows, it)); for (Window *it = FirstWindow; it; it = it->next) if (it->z == 0) Add(&order, it);
return order; return order;
} }
@@ -115,15 +114,6 @@ void AddColumnWindow() {
CreateSearchBar(window->id); CreateSearchBar(window->id);
} }
Int GetWindowIndex(WindowID window_id) {
Int i = 0;
For(Windows) {
if (window_id == it) return i;
i += 1;
}
return 0;
}
void AddRowWindow() { void AddRowWindow() {
Window *active_window = GetActiveWindow(); Window *active_window = GetActiveWindow();
Window *window = CreateWindow(); Window *window = CreateWindow();
@@ -131,12 +121,6 @@ void AddRowWindow() {
window->active_view = view->id; window->active_view = view->id;
CreateTitlebar(window->id); CreateTitlebar(window->id);
CreateSearchBar(window->id); CreateSearchBar(window->id);
Int i0 = GetWindowIndex(window->id);
RemoveByIndex(&Windows, i0);
Int active_window_index = GetWindowIndex(active_window->id);
Insert(&Windows, window->id, active_window_index + 1);
} }
void SetVisibility(WindowID window_id, bool v) { void SetVisibility(WindowID window_id, bool v) {
@@ -207,11 +191,6 @@ https://www.lua.org/manual/5.4/
void InitWindows() { void InitWindows() {
Allocator sys_allocator = Perm; Allocator sys_allocator = Perm;
#if !ARRAY_DEBUG
Reserve(&Windows, 64);
Reserve(&Buffers, 256);
Reserve(&Views, 256);
#endif
{ {
Window *window = CreateWindow(); Window *window = CreateWindow();
@@ -315,23 +294,22 @@ void LayoutWindows(int16_t wx, int16_t wy) {
} }
} }
For(Windows) { for (Window *it = FirstWindow; it; it = it->next) {
Window *window = it.o; if (!it->visible || it->absolute_position || it->is_title_bar || it->is_search_bar) continue;
if (!window->visible || window->absolute_position || window->is_title_bar || window->is_search_bar) continue;
Window *title_bar_window = GetWindow(window->title_bar_window); Window *title_bar_window = GetWindow(it->title_bar_window);
title_bar_window->total_rect = CutBottom(&window->total_rect, GetTitleBarSize(title_bar_window)); title_bar_window->total_rect = CutBottom(&it->total_rect, GetTitleBarSize(title_bar_window));
title_bar_window->document_rect = title_bar_window->total_rect; title_bar_window->document_rect = title_bar_window->total_rect;
Rect2I save_rect = window->document_rect; Rect2I save_rect = it->document_rect;
CutLeft(&save_rect, GetSize(save_rect).x/2); CutLeft(&save_rect, GetSize(save_rect).x/2);
Window *search_bar_window = GetWindow(window->search_bar_window); Window *search_bar_window = GetWindow(it->search_bar_window);
search_bar_window->total_rect = CutTop(&save_rect, GetTitleBarSize(search_bar_window)); search_bar_window->total_rect = CutTop(&save_rect, GetTitleBarSize(search_bar_window));
search_bar_window->document_rect = search_bar_window->total_rect; search_bar_window->document_rect = search_bar_window->total_rect;
window->document_rect = window->total_rect; it->document_rect = it->total_rect;
if (window->draw_scrollbar) window->scrollbar_rect = CutRight(&window->document_rect, (Int)ScrollBarSize); if (it->draw_scrollbar) it->scrollbar_rect = CutRight(&it->document_rect, (Int)ScrollBarSize);
if (window->draw_line_numbers) window->line_numbers_rect = CutLeft(&window->document_rect, (Int)line_numbers_size); if (it->draw_line_numbers) it->line_numbers_rect = CutLeft(&it->document_rect, (Int)line_numbers_size);
} }
{ {