diff --git a/build_file.cpp b/build_file.cpp index 404d8cf..e247182 100644 --- a/build_file.cpp +++ b/build_file.cpp @@ -153,10 +153,15 @@ int CompileTextEditor() { // cmd.add("opengl32.lib gdi32.lib winmm.lib shell32.lib user32.lib msvcrt.lib /NODEFAULTLIB:LIBCMT"); For2(lib, libs) For(lib.link) cmd.add(it); For(libs) For2(o, it.objects) cmd.add(o); + cmd.add("icon.res"); OS_DeleteFile("te.pdb"); // For(cmd) IO_Printf("%.*s\n", S8_Expand(it)); - + if (!OS_FileExists("icon.res")) { + OS_CopyFile("../data/icon.ico", "icon.ico", true); + OS_CopyFile("../data/icon.rc", "icon.rc", true); + Run("rc icon.rc"); + } result += Run(cmd); return result; } diff --git a/data/icon.ico b/data/icon.ico new file mode 100644 index 0000000..6069fc9 Binary files /dev/null and b/data/icon.ico differ diff --git a/data/icon.rc b/data/icon.rc new file mode 100644 index 0000000..ff5fdbe --- /dev/null +++ b/data/icon.rc @@ -0,0 +1 @@ +id ICON "icon.ico" \ No newline at end of file diff --git a/src/text_editor/commands.cpp b/src/text_editor/commands.cpp index ee1460b..fe14334 100644 --- a/src/text_editor/commands.cpp +++ b/src/text_editor/commands.cpp @@ -17,9 +17,9 @@ enum EventKind { EVENT_KEY_PRESS, EVENT_TEXT_INPUT, -}; -bool IsMouseEvent(EventKind kind) { return kind >= EVENT_MOUSE_LEFT && kind <= EVENT_MOUSE_MOVE; } + EVENT_DROP_FILE, +}; struct Event { EventKind kind; @@ -39,6 +39,8 @@ struct Event { const char *text; }; +inline bool IsMouseEvent(EventKind kind) { return kind >= EVENT_MOUSE_LEFT && kind <= EVENT_MOUSE_MOVE; } + const int DIR_RIGHT = 0; const int DIR_LEFT = 1; const int DIR_DOWN = 2; diff --git a/src/text_editor/commands_window.cpp b/src/text_editor/commands_window.cpp index 80e02e8..e0023a3 100644 --- a/src/text_editor/commands_window.cpp +++ b/src/text_editor/commands_window.cpp @@ -595,6 +595,10 @@ void WindowCommand(Event event, Window *window, View *view) { } } + if (event.kind == EVENT_DROP_FILE) { + WindowOpenBufferView(window, event.text); + } + if (CtrlAlt(SDLK_DOWN)) { Command_DuplicateLine(view, DIR_DOWN); } else if (AltShift(SDLK_DOWN)) { @@ -732,7 +736,7 @@ void WindowCommand(Event event, Window *window, View *view) { search = true; } - if (event.text) { + if (event.kind == EVENT_TEXT_INPUT) { Scratch scratch; String string = event.text; String16 string16 = ToString16(scratch, string); diff --git a/src/text_editor/process.cpp b/src/text_editor/process.cpp index b4cf3c7..d0f458b 100644 --- a/src/text_editor/process.cpp +++ b/src/text_editor/process.cpp @@ -1,5 +1,11 @@ Array ActiveProcesses = {}; +// WARNING: seems that this maybe can't work reliably? +// in case of 'git grep a' it's possible that child process spawns it's own +// child process and then it won't print anything because it won't have +// the appropriate handles. This happens in this case when git grep calls +// 'less' program which errors out and doesn't print anything +// @todo: maybe I should ask someone smarter about this! void UpdateProcesses() { Scratch scratch; int64_t buffer_size = 4096; diff --git a/src/text_editor/text_editor.cpp b/src/text_editor/text_editor.cpp index ba746c3..4567133 100644 --- a/src/text_editor/text_editor.cpp +++ b/src/text_editor/text_editor.cpp @@ -80,9 +80,10 @@ Event TranslateSDLEvent(SDL_Event *input_event) { } break; case SDL_EVENT_TEXT_INPUT: { - event.kind = EVENT_TEXT_INPUT; - SDL_TextInputEvent &b = input_event->text; - event.text = b.text; + event.kind = EVENT_TEXT_INPUT; + SDL_TextInputEvent &b = input_event->text; + String string = b.text; + event.text = Copy(Perm, string).data; // @leak: @todo: intern table } break; case SDL_EVENT_MOUSE_BUTTON_DOWN: { @@ -131,6 +132,21 @@ Event TranslateSDLEvent(SDL_Event *input_event) { // SDL_MouseMotionEvent &b = input_event->motion; } break; + case SDL_EVENT_DROP_FILE: { + event.kind = EVENT_DROP_FILE; + SDL_DropEvent &b = input_event->drop; + String string = b.data; + event.text = Copy(Perm, string).data; // @leak + } break; + + // SDL_SetEventEnabled(SDL_EVENT_DROP_TEXT, true); + // case SDL_EVENT_DROP_TEXT: { + // SDL_DropEvent &b = input_event->drop; + // event.kind = EVENT_DROP_TEXT; + // String string = b.data; + // event.text = Copy(Perm, string).data; // @leak + // } break; + default: { }; } @@ -218,9 +234,13 @@ Array GetEventsForFrame(Allocator allocator) { #if _WIN32 int WinMain(void *hInstance, void *hPrevInstance, const char *lpCmdLine, int nShowCmd) #else -int main() +int main(int argc, char **argv) #endif { +#if _WIN32 + int argc = __argc; + char **argv = __argv; +#endif BeginProfiler(); InitScratch(); @@ -291,6 +311,7 @@ int main() } SDL_StartTextInput(SDLWindow); + SDL_SetEventEnabled(SDL_EVENT_DROP_FILE, true); SDL_GL_SetSwapInterval(1); // vsync { float scale = SDL_GetWindowDisplayScale(SDLWindow); @@ -304,6 +325,12 @@ int main() InitLuaConfig(); InitWindows(); + for (int i = 1; i < argc; i += 1) { + String it = argv[i]; + Window *window = GetWindow({0}); + WindowOpenBufferView(window, it); + } + while (AppIsRunning) { FrameID += 1; diff --git a/src/text_editor/todo.txt b/src/text_editor/todo.txt index 69e8866..898c335 100644 --- a/src/text_editor/todo.txt +++ b/src/text_editor/todo.txt @@ -1,15 +1,11 @@ - I guess it's pretty dangerous passing pointers everywhere? -- kill process using command in window +- text in events aren't we using invalid memory for text? We need an intern table? - kill all processes on exit https://devblogs.microsoft.com/oldnewthing/20131209-00/?p=2433 - if exec is prepended with '!' symbol then run a shell command - try using git grep for search for now, combine with fuzzy search buffer - Test stdin writing code - Implement shell interaction (last line should have a '$'' symbols, if you press enter it should send that line to stdin of a running shell) -- open file from cmd -- detach from console when executing the program from the console to make sure that the user won't have to keep the cmd alive -- win32 open file with text editor from desktop -- drag and drop file into the window - exe icon - search as a command to execute which is going to be in the title bar