Skip to content

Commit

Permalink
remove duplicate dialog code
Browse files Browse the repository at this point in the history
  • Loading branch information
tsl0922 committed Jan 31, 2024
1 parent 3e64acc commit cb17d92
Showing 1 changed file with 42 additions and 64 deletions.
106 changes: 42 additions & 64 deletions src/dialog.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2023-2024 tsl0922. All rights reserved.
// SPDX-License-Identifier: GPL-2.0-only

#include <assert.h>
#include <windows.h>
#include <shobjidl.h>
#include <mpv/client.h>
Expand Down Expand Up @@ -96,21 +97,12 @@ static void add_options(IFileDialog *pfd, DWORD options) {
}
}

// single file open dialog
char *open_dialog(void *talloc_ctx, plugin_ctx *ctx) {
IFileOpenDialog *pfd = NULL;
if (FAILED(CoCreateInstance(&CLSID_FileOpenDialog, NULL,
CLSCTX_INPROC_SERVER, &IID_IFileDialog,
(void **)&pfd)))
return NULL;

add_filters(ctx->mpv, (IFileDialog *)pfd);
set_default_path(ctx->mpv, (IFileDialog *)pfd);
add_options((IFileDialog *)pfd, FOS_FORCEFILESYSTEM);

// file open/save dialog with single result
static char *show_dialog(void *talloc_ctx, HWND hwnd, IFileDialog *pfd) {
assert(pfd);
char *path = NULL;

if (SUCCEEDED(pfd->lpVtbl->Show(pfd, ctx->hwnd))) {
if (SUCCEEDED(pfd->lpVtbl->Show(pfd, hwnd))) {
IShellItem *psi;
if (SUCCEEDED(pfd->lpVtbl->GetResult(pfd, &psi))) {
wchar_t *w_path;
Expand All @@ -124,25 +116,16 @@ char *open_dialog(void *talloc_ctx, plugin_ctx *ctx) {
}

pfd->lpVtbl->Release(pfd);

return path;
}

// multiple file open dialog
char **open_dialog_multi(void *talloc_ctx, plugin_ctx *ctx) {
IFileOpenDialog *pfd = NULL;
if (FAILED(CoCreateInstance(&CLSID_FileOpenDialog, NULL,
CLSCTX_INPROC_SERVER, &IID_IFileOpenDialog,
(void **)&pfd)))
return NULL;

add_filters(ctx->mpv, (IFileDialog *)pfd);
set_default_path(ctx->mpv, (IFileDialog *)pfd);
add_options((IFileDialog *)pfd, FOS_FORCEFILESYSTEM | FOS_ALLOWMULTISELECT);

// file open dialog with multiple result
static char **show_dialog_multi(void *talloc_ctx, HWND hwnd,
IFileOpenDialog *pfd) {
assert(pfd);
char **paths = NULL;

if (SUCCEEDED(pfd->lpVtbl->Show(pfd, ctx->hwnd))) {
if (SUCCEEDED(pfd->lpVtbl->Show(pfd, hwnd))) {
IShellItemArray *psia;
if (SUCCEEDED(pfd->lpVtbl->GetResults(pfd, &psia))) {
DWORD count;
Expand All @@ -167,39 +150,51 @@ char **open_dialog_multi(void *talloc_ctx, plugin_ctx *ctx) {
}

pfd->lpVtbl->Release(pfd);

return paths;
}

// folder open dialog
char *open_folder(void *talloc_ctx, plugin_ctx *ctx) {
// single file open dialog
char *open_dialog(void *talloc_ctx, plugin_ctx *ctx) {
IFileOpenDialog *pfd = NULL;
if (FAILED(CoCreateInstance(&CLSID_FileOpenDialog, NULL,
CLSCTX_INPROC_SERVER, &IID_IFileDialog,
(void **)&pfd)))
return NULL;

add_filters(ctx->mpv, (IFileDialog *)pfd);
set_default_path(ctx->mpv, (IFileDialog *)pfd);
add_options((IFileDialog *)pfd, FOS_FORCEFILESYSTEM);

return show_dialog(talloc_ctx, ctx->hwnd, (IFileDialog *)pfd);
}

// multiple file open dialog
char **open_dialog_multi(void *talloc_ctx, plugin_ctx *ctx) {
IFileOpenDialog *pfd = NULL;
if (FAILED(CoCreateInstance(&CLSID_FileOpenDialog, NULL,
CLSCTX_INPROC_SERVER, &IID_IFileOpenDialog,
(void **)&pfd)))
return NULL;

add_filters(ctx->mpv, (IFileDialog *)pfd);
set_default_path(ctx->mpv, (IFileDialog *)pfd);
add_options((IFileDialog *)pfd, FOS_FORCEFILESYSTEM | FOS_PICKFOLDERS);
add_options((IFileDialog *)pfd, FOS_FORCEFILESYSTEM | FOS_ALLOWMULTISELECT);

char *path = NULL;
return show_dialog_multi(talloc_ctx, ctx->hwnd, pfd);
}

if (SUCCEEDED(pfd->lpVtbl->Show(pfd, ctx->hwnd))) {
IShellItem *psi;
if (SUCCEEDED(pfd->lpVtbl->GetResult(pfd, &psi))) {
wchar_t *w_path;
if (SUCCEEDED(psi->lpVtbl->GetDisplayName(psi, SIGDN_FILESYSPATH,
&w_path))) {
path = mp_to_utf8(talloc_ctx, w_path);
CoTaskMemFree(w_path);
}
psi->lpVtbl->Release(psi);
}
}
// folder open dialog
char *open_folder(void *talloc_ctx, plugin_ctx *ctx) {
IFileOpenDialog *pfd = NULL;
if (FAILED(CoCreateInstance(&CLSID_FileOpenDialog, NULL,
CLSCTX_INPROC_SERVER, &IID_IFileOpenDialog,
(void **)&pfd)))
return NULL;

pfd->lpVtbl->Release(pfd);
set_default_path(ctx->mpv, (IFileDialog *)pfd);
add_options((IFileDialog *)pfd, FOS_FORCEFILESYSTEM | FOS_PICKFOLDERS);

return path;
return show_dialog(talloc_ctx, ctx->hwnd, (IFileDialog *)pfd);
}

// save dialog
Expand All @@ -215,22 +210,5 @@ char *save_dialog(void *talloc_ctx, plugin_ctx *ctx) {
set_default_name(ctx->mpv, (IFileDialog *)pfd);
add_options((IFileDialog *)pfd, FOS_FORCEFILESYSTEM);

char *path = NULL;

if (SUCCEEDED(pfd->lpVtbl->Show(pfd, ctx->hwnd))) {
IShellItem *psi;
if (SUCCEEDED(pfd->lpVtbl->GetResult(pfd, &psi))) {
wchar_t *w_path;
if (SUCCEEDED(psi->lpVtbl->GetDisplayName(psi, SIGDN_FILESYSPATH,
&w_path))) {
path = mp_to_utf8(talloc_ctx, w_path);
CoTaskMemFree(w_path);
}
psi->lpVtbl->Release(psi);
}
}

pfd->lpVtbl->Release(pfd);

return path;
return show_dialog(talloc_ctx, ctx->hwnd, (IFileDialog *)pfd);
}

0 comments on commit cb17d92

Please sign in to comment.