From 59360f770cd50bc1a5c67c2c8dff65bc0169fa99 Mon Sep 17 00:00:00 2001 From: Ryan Fleury Date: Tue, 27 Aug 2024 14:49:57 -0700 Subject: [PATCH] file drop support --- src/df/gfx/df_gfx.c | 23 ++++++++++++++++++++++- src/os/gfx/win32/os_gfx_win32.c | 25 +++++++++++++++++++++++-- src/ui/ui_core.h | 2 ++ 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/df/gfx/df_gfx.c b/src/df/gfx/df_gfx.c index 3999125b..f258c7c9 100644 --- a/src/df/gfx/df_gfx.c +++ b/src/df/gfx/df_gfx.c @@ -1341,12 +1341,14 @@ df_window_update_and_render(Arena *arena, DF_Window *ws, DF_CmdList *cmds) case OS_EventKind_MouseMove: {kind = UI_EventKind_MouseMove;}break; case OS_EventKind_Text: {kind = UI_EventKind_Text;}break; case OS_EventKind_Scroll: {kind = UI_EventKind_Scroll;}break; + case OS_EventKind_FileDrop: {kind = UI_EventKind_FileDrop;}break; } } ui_event.kind = kind; ui_event.key = os_event->key; ui_event.modifiers = os_event->flags; ui_event.string = os_event->character ? str8_from_32(ui_build_arena(), str32(&os_event->character, 1)) : str8_zero(); + ui_event.paths = str8_list_copy(ui_build_arena(), &os_event->strings); ui_event.pos = os_event->pos; ui_event.delta_2f32 = os_event->delta; ui_event.timestamp_us = os_event->timestamp_us; @@ -7377,7 +7379,7 @@ df_window_update_and_render(Arena *arena, DF_Window *ws, DF_CmdList *cmds) } } } - if((view->query_string_size != 0 || view->is_filtering) && ui_is_focus_active() && ui_slot_press(UI_EventActionSlot_Cancel)) + if(view->spec->info.flags & DF_ViewSpecFlag_CanFilter && (view->query_string_size != 0 || view->is_filtering) && ui_is_focus_active() && ui_slot_press(UI_EventActionSlot_Cancel)) { DF_CmdParams p = df_cmd_params_from_view(ws, panel, view); df_push_cmd__root(&p, df_cmd_spec_from_core_cmd_kind(DF_CoreCmdKind_ClearFilter)); @@ -7789,6 +7791,25 @@ df_window_update_and_render(Arena *arena, DF_Window *ws, DF_CmdList *cmds) } } } + + ////////////////////////// + //- rjf: accept file drops + // + for(UI_Event *evt = 0; ui_next_event(&evt);) + { + if(evt->kind == UI_EventKind_FileDrop && contains_2f32(content_rect, evt->pos)) + { + for(String8Node *n = evt->paths.first; n != 0; n = n->next) + { + Temp scratch = scratch_begin(0, 0); + DF_CmdParams p = df_cmd_params_from_panel(ws, panel); + p.file_path = path_normalized_from_string(scratch.arena, n->string); + df_push_cmd__root(&p, df_cmd_spec_from_core_cmd_kind(DF_CoreCmdKind_Open)); + scratch_end(scratch); + } + ui_eat_event(evt); + } + } } } diff --git a/src/os/gfx/win32/os_gfx_win32.c b/src/os/gfx/win32/os_gfx_win32.c index e53e951e..cd1cb87e 100644 --- a/src/os/gfx/win32/os_gfx_win32.c +++ b/src/os/gfx/win32/os_gfx_win32.c @@ -540,7 +540,27 @@ os_w32_wnd_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) F32 new_dpi = (F32)(wParam & 0xffff); window->dpi = new_dpi; }break; - + + //- rjf: [file drop] + case WM_DROPFILES: + { + HDROP drop = (HDROP)wParam; + POINT drop_pt = {0}; + DragQueryPoint(drop, &drop_pt); + ScreenToClient(window->hwnd, &drop_pt); + U64 num_files_dropped = DragQueryFile(drop, 0xffffffff, 0, 0); + OS_Event *event = os_w32_push_event(OS_EventKind_FileDrop, window); + event->pos = v2f32((F32)drop_pt.x, (F32)drop_pt.y); + for(U64 idx = 0; idx < num_files_dropped; idx += 1) + { + U64 name_size = DragQueryFile(drop, idx, 0, 0) + 1; + U8 *name_ptr = push_array(os_w32_event_arena, U8, name_size); + DragQueryFile(drop, idx, name_ptr, name_size); + str8_list_push(os_w32_event_arena, &event->strings, str8(name_ptr, name_size)); + } + DragFinish(drop); + }break; + //- rjf: [custom border] case WM_NCPAINT: { @@ -1014,7 +1034,8 @@ os_window_open(Vec2F32 resolution, OS_WindowFlags flags, String8 title) (int)resolution.y, 0, 0, os_w32_gfx_state->hInstance, - 0); + 0); + DragAcceptFiles(hwnd, 1); scratch_end(scratch); } diff --git a/src/ui/ui_core.h b/src/ui/ui_core.h index 2ba93d9b..a3a23d91 100644 --- a/src/ui/ui_core.h +++ b/src/ui/ui_core.h @@ -91,6 +91,7 @@ typedef enum UI_EventKind UI_EventKind_MouseMove, UI_EventKind_Scroll, UI_EventKind_AutocompleteHint, + UI_EventKind_FileDrop, UI_EventKind_COUNT } UI_EventKind; @@ -141,6 +142,7 @@ struct UI_Event OS_Key key; OS_EventFlags modifiers; String8 string; + String8List paths; Vec2F32 pos; Vec2F32 delta_2f32; Vec2S32 delta_2s32;