-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from tryphotino/debug-dialogs
Debug dialogs
- Loading branch information
Showing
17 changed files
with
1,156 additions
and
309 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.