Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug dialogs #106

Merged
merged 8 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions Photino.Native/Exports.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "Photino.Dialog.h"
#include "Photino.h"

#ifdef _WIN32
Expand Down Expand Up @@ -204,12 +205,7 @@ extern "C"
{
instance->SetZoom(zoom);
}

EXPORTED void Photino_ShowMessage(Photino* instance, AutoString title, AutoString body, unsigned int type)
{
instance->ShowMessage(title, body, type);
}


EXPORTED void Photino_ShowNotification(Photino* instance, AutoString title, AutoString body)
{
instance->ShowNotification(title, body);
Expand All @@ -222,6 +218,22 @@ extern "C"



//Dialog
EXPORTED AutoString* Photino_ShowOpenFile(Photino* inst, AutoString title, AutoString defaultPath, bool multiSelect, AutoString* filters, int filterCount, int* resultCount) {
return inst->GetDialog()->ShowOpenFile(title, defaultPath, multiSelect, filters, filterCount, resultCount);
}
EXPORTED AutoString* Photino_ShowOpenFolder(Photino* inst, AutoString title, AutoString defaultPath, bool multiSelect, int* resultCount) {
return inst->GetDialog()->ShowOpenFolder(title, defaultPath, multiSelect, resultCount);
}
EXPORTED AutoString Photino_ShowSaveFile(Photino* inst, AutoString title, AutoString defaultPath, AutoString* filters, int filterCount) {
return inst->GetDialog()->ShowSaveFile(title, defaultPath, filters, filterCount);
}
EXPORTED DialogResult Photino_ShowMessage(Photino* inst, AutoString title, AutoString text, DialogButtons buttons, DialogIcon icon) {
return inst->GetDialog()->ShowMessage(title, text, buttons, icon);
}



//Callbacks
EXPORTED void Photino_AddCustomSchemeName(Photino* instance, AutoString scheme)
{
Expand Down
67 changes: 67 additions & 0 deletions Photino.Native/Photino.Dialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#pragma once

#ifndef PHOTINO_DIALOG_H
#define PHOTINO_DIALOG_H

#include "Photino.h"

#ifdef __APPLE__
#include <Cocoa/Cocoa.h>
#endif

enum class DialogResult
{
Cancel = -1,
Ok,
Yes,
No,
Abort,
Retry,
Ignore,
};

enum class DialogButtons
{
Ok,
OkCancel,
YesNo,
YesNoCancel,
RetryCancel,
AbortRetryIgnore,
};

enum class DialogIcon
{
Info,
Warning,
Error,
Question,
};

class PhotinoDialog
{
public:
#ifdef _WIN32
PhotinoDialog(Photino *window);
#else
PhotinoDialog();
#endif
~PhotinoDialog();

AutoString *ShowOpenFile(AutoString title, AutoString defaultPath, bool multiSelect, AutoString *filters, int filterCount, int *resultCount);
AutoString *ShowOpenFolder(AutoString title, AutoString defaultPath, bool multiSelect, int *resultCount);
AutoString ShowSaveFile(AutoString title, AutoString defaultPath, AutoString *filters, int filterCount);
DialogResult ShowMessage(AutoString title, AutoString text, DialogButtons buttons, DialogIcon icon);

protected:
#ifdef __APPLE__
NSImage *_errorIcon;
NSImage *_infoIcon;
NSImage *_questionIcon;
NSImage *_warningIcon;
#elif _WIN32
Photino *_window;
#endif
};

#endif
192 changes: 192 additions & 0 deletions Photino.Native/Photino.Linux.Dialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
#include "Photino.Dialog.h"

enum DialogType {
OpenFile,
OpenFolder,
SaveFile
};

void AddFilters(GtkWidget* dialog, AutoString* filters, int filterCount)
{
for (int i = 0; i < filterCount; i++) {
GtkFileFilter *filter = gtk_file_filter_new();
char *name = strtok(filters[i], "|");
gtk_file_filter_set_name(filter, name);
char *patterns = strtok(NULL, "|");
while (patterns != NULL) {
gtk_file_filter_add_pattern(filter, patterns);
patterns = strtok(NULL, ";");
}
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
}
}

AutoString* ShowDialog(DialogType type, AutoString title, AutoString defaultPath, bool multiSelect, AutoString* filters, int filterCount, int* resultCount) {
GtkFileChooserAction action;
const char* buttonText;
switch (type) {
case OpenFile:
action = GTK_FILE_CHOOSER_ACTION_OPEN;
buttonText = "_Open";
break;
case OpenFolder:
action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
buttonText = "_Select";
break;
case SaveFile:
action = GTK_FILE_CHOOSER_ACTION_SAVE;
buttonText = "_Save";
break;
}

GtkWidget* dialog = gtk_file_chooser_dialog_new(
title, NULL, action,
"_Cancel", GTK_RESPONSE_CANCEL,
buttonText, GTK_RESPONSE_ACCEPT,
NULL);

if (defaultPath != NULL) {
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), defaultPath);
}
if (type == OpenFile || type == OpenFolder) {
gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), multiSelect);
}
if (type == SaveFile) {
gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
}
if (type == OpenFile || type == SaveFile) {
AddFilters(dialog, filters, filterCount);
}

gint res = gtk_dialog_run(GTK_DIALOG(dialog));

if (res != GTK_RESPONSE_ACCEPT) {
*resultCount = 0;
gtk_widget_destroy(dialog);
return NULL;
}

if (type == OpenFile || type == OpenFolder) {
GSList* pathList = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
int count = g_slist_length(pathList);
char** results = new char*[count];
for (int i = 0; i < count; i++) {
results[i] = g_strdup((char*)g_slist_nth_data(pathList, i));
}
g_slist_free(pathList);
*resultCount = count;
gtk_widget_destroy(dialog);
return results;
}
else {
char* result = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
gtk_widget_destroy(dialog);
return new char*[1] { result };
}
}

PhotinoDialog::PhotinoDialog() {}

PhotinoDialog::~PhotinoDialog() {}

AutoString* PhotinoDialog::ShowOpenFile(AutoString title, AutoString defaultPath, bool multiSelect, AutoString* filters, int filterCount, int* resultCount)
{
return ShowDialog(OpenFile, title, defaultPath, multiSelect, filters, filterCount, resultCount);
}

AutoString* PhotinoDialog::ShowOpenFolder(AutoString title, AutoString defaultPath, bool multiSelect, int* resultCount)
{
return ShowDialog(OpenFolder, title, defaultPath, multiSelect, NULL, 0, resultCount);
}

AutoString PhotinoDialog::ShowSaveFile(AutoString title, AutoString defaultPath, AutoString* filters, int filterCount)
{
return ShowDialog(SaveFile, title, defaultPath, false, filters, filterCount, NULL)[0];
}

DialogResult PhotinoDialog::ShowMessage(AutoString title, AutoString text, DialogButtons buttons, DialogIcon icon)
{
GtkWidget* dialog;
GtkMessageType type;

switch (icon) {
case DialogIcon::Info:
type = GTK_MESSAGE_INFO;
break;
case DialogIcon::Warning:
type = GTK_MESSAGE_WARNING;
break;
case DialogIcon::Error:
type = GTK_MESSAGE_ERROR;
break;
case DialogIcon::Question:
type = GTK_MESSAGE_QUESTION;
break;
default:
type = GTK_MESSAGE_OTHER;
break;
}

dialog = gtk_message_dialog_new(nullptr,
GTK_DIALOG_MODAL,
type,
GTK_BUTTONS_NONE,
"%s",
title);
gtk_message_dialog_set_markup(GTK_MESSAGE_DIALOG(dialog), text);

switch (buttons) {
case DialogButtons::Ok:
gtk_dialog_add_button(GTK_DIALOG(dialog), "_Ok", (gint)DialogResult::Ok);
break;
case DialogButtons::OkCancel:
gtk_dialog_add_button(GTK_DIALOG(dialog), "_Ok", (gint)DialogResult::Ok);
gtk_dialog_add_button(GTK_DIALOG(dialog), "_Cancel", (gint)DialogResult::Cancel);
break;
case DialogButtons::YesNo:
gtk_dialog_add_button(GTK_DIALOG(dialog), "_Yes", (gint)DialogResult::Yes);
gtk_dialog_add_button(GTK_DIALOG(dialog), "_No", (gint)DialogResult::No);
break;
case DialogButtons::YesNoCancel:
gtk_dialog_add_button(GTK_DIALOG(dialog), "_Yes", (gint)DialogResult::Yes);
gtk_dialog_add_button(GTK_DIALOG(dialog), "_No", (gint)DialogResult::No);
gtk_dialog_add_button(GTK_DIALOG(dialog), "_Cancel", (gint)DialogResult::Cancel);
break;
case DialogButtons::RetryCancel:
gtk_dialog_add_button(GTK_DIALOG(dialog), "_Retry", (gint)DialogResult::Retry);
gtk_dialog_add_button(GTK_DIALOG(dialog), "_Cancel", (gint)DialogResult::Cancel);
break;
case DialogButtons::AbortRetryIgnore:
gtk_dialog_add_button(GTK_DIALOG(dialog), "_Abort", (gint)DialogResult::Abort);
gtk_dialog_add_button(GTK_DIALOG(dialog), "_Retry", (gint)DialogResult::Retry);
gtk_dialog_add_button(GTK_DIALOG(dialog), "_Ignore", (gint)DialogResult::Ignore);
break;
default:
gtk_dialog_add_button(GTK_DIALOG(dialog), "_Ok", (gint)DialogResult::Ok);
break;
}

gint result = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);

switch (result) {
case GTK_RESPONSE_CLOSE:
return DialogResult::Cancel;
case (gint)DialogResult::Ok:
return DialogResult::Ok;
case (gint)DialogResult::Yes:
return DialogResult::Yes;
case (gint)DialogResult::No:
return DialogResult::No;
case (gint)DialogResult::Cancel:
return DialogResult::Cancel;
case (gint)DialogResult::Abort:
return DialogResult::Abort;
case (gint)DialogResult::Retry:
return DialogResult::Retry;
case (gint)DialogResult::Ignore:
return DialogResult::Ignore;
default:
return DialogResult::Cancel;
}
}
Loading