diff --git a/source/libublsettingsui-gtk3-config-chooser.c b/source/libublsettingsui-gtk3-config-chooser.c new file mode 100644 index 0000000..ab670f0 --- /dev/null +++ b/source/libublsettingsui-gtk3-config-chooser.c @@ -0,0 +1,65 @@ +#include "libublsettingsui-gtk3.h" + +typedef struct { + filechooser_window *file_chooser_window; + GtkFileFilter *ini_filter; + GtkFileFilter *yaml_filter; + GtkFileFilter *json_filter; +} config_file_chooser_window; + +void on_config_file_chooser_destroy(GtkWidget*, config_file_chooser_window *window){ + printf("fewrgege\n"); +} + +void on_config_file_chooser_accept(GtkWidget *, config_file_chooser_window *window){ + switch (gtk_file_chooser_get_action(GTK_FILE_CHOOSER(window->file_chooser_window->MainFileChooser))){ + case GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER: + case GTK_FILE_CHOOSER_ACTION_SAVE: + if (yon_char_is_empty(window->file_chooser_window->last_success_selection)){ + yon_ubl_status_box_spawn(GTK_CONTAINER(window->file_chooser_window->StatusBox),yon_char_get_localised_from_lib(NOTHING_CHOSEN_LABEL),5,BACKGROUND_IMAGE_FAIL_TYPE); + return; + } + window->file_chooser_window->responce=GTK_RESPONSE_APPLY; + break; + + case GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER: + case GTK_FILE_CHOOSER_ACTION_OPEN: + window->file_chooser_window->responce = GTK_RESPONSE_ACCEPT; + break; + } + + char *path = NULL; + const char *filter_name = gtk_file_filter_get_name(gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(window->file_chooser_window->MainFileChooser))); + if (window->file_chooser_window->last_success_selection){ + int ext_pos = yon_char_find_last(window->file_chooser_window->last_success_selection,'.'); + if (ext_pos==-1||strcmp(window->file_chooser_window->last_success_selection+ext_pos+1,filter_name)){ + path = yon_char_unite(window->file_chooser_window->last_success_selection,filter_name,NULL); + free(window->file_chooser_window->last_success_selection); + window->file_chooser_window->last_success_selection = path; + } + } + + + gtk_widget_destroy(window->file_chooser_window->Window); + gtk_main_quit(); +} + +filechooser_window *yon_config_file_chooser_window_new(){ + config_file_chooser_window *window = malloc(sizeof(config_file_chooser_window)); + window->file_chooser_window = yon_file_chooser_window_new(GTK_FILE_CHOOSER_ACTION_SAVE); + window->ini_filter = gtk_file_filter_new(); + gtk_file_filter_add_pattern(window->ini_filter,"*.ini"); + gtk_file_filter_set_name(window->ini_filter,".ini"); + gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(window->file_chooser_window->MainFileChooser),window->ini_filter); + window->yaml_filter = gtk_file_filter_new(); + gtk_file_filter_add_pattern(window->yaml_filter,"*.yaml"); + gtk_file_filter_set_name(window->yaml_filter,".yaml"); + gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(window->file_chooser_window->MainFileChooser),window->yaml_filter); + window->json_filter = gtk_file_filter_new(); + gtk_file_filter_add_pattern(window->json_filter,"*.json"); + gtk_file_filter_set_name(window->json_filter,".json"); + gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(window->file_chooser_window->MainFileChooser),window->json_filter); + yon_file_chooser_remove_accept_function(window->file_chooser_window); + g_signal_connect(G_OBJECT(window->file_chooser_window->SaveButton),"clicked",G_CALLBACK(on_config_file_chooser_accept),window); + return window->file_chooser_window; +} \ No newline at end of file diff --git a/source/libublsettingsui-gtk3-filechooser.c b/source/libublsettingsui-gtk3-filechooser.c index d287a79..b94a6da 100644 --- a/source/libublsettingsui-gtk3-filechooser.c +++ b/source/libublsettingsui-gtk3-filechooser.c @@ -87,7 +87,7 @@ void yon_file_chooser_set_root(filechooser_window *window, char *root_path){ g_signal_connect(G_OBJECT(window->MainFileChooser),"current-folder-changed",G_CALLBACK(on_file_chooser_check_root),window); } -void yon_file_chooser_remove_root(filechooser_window *window, char *root_path){ +void yon_file_chooser_remove_root(filechooser_window *window){ if (!yon_char_is_empty(window->root)) free(window->root); window->root=NULL; g_signal_handlers_disconnect_by_func(G_OBJECT(window->MainFileChooser),G_CALLBACK(on_file_chooser_check_root),window); @@ -151,4 +151,9 @@ GtkResponseType yon_file_chooser_start(filechooser_window *window){ gtk_main(); return window->responce; +} + +void yon_file_chooser_remove_accept_function(filechooser_window *window){ + g_signal_handlers_disconnect_by_func(G_OBJECT(window->SaveButton),G_CALLBACK(on_file_chooser_accept),window); + } \ No newline at end of file diff --git a/source/libublsettingsui-gtk3.h b/source/libublsettingsui-gtk3.h index bc7d345..2d1cd3d 100644 --- a/source/libublsettingsui-gtk3.h +++ b/source/libublsettingsui-gtk3.h @@ -245,11 +245,36 @@ typedef struct { char *root; } filechooser_window; +/// @brief Create a file chooser window for specific action +/// @param action File chooser's action, a mode for window to work at +/// @return A newly allocated file chooser window; filechooser_window *yon_file_chooser_window_new(GtkFileChooserAction action); + +/// @brief Start file chooser window as dialog window +/// @param window File chooser window to start +/// @return GtkResponceType of pressed button GtkResponseType yon_file_chooser_start(filechooser_window *window); + +/// @brief Sets a a new label for "Accept" button +/// @param label void yon_file_chooser_set_button_label(char *label); + +/// @brief Set a root for file chooser. File chooser cannot escape root folder +/// @param window +/// @param root_path void yon_file_chooser_set_root(filechooser_window *window, char *root_path); -void yon_file_chooser_remove_root(filechooser_window *window, char *root_path); + +/// @brief Remove root from file chooser. +/// @param window +/// @param root_path +void yon_file_chooser_remove_root(filechooser_window *window); + + +/// @brief Create file chooser window, configured to select supported configuration files. +/// @return A newly allocated file chooser window; +filechooser_window *yon_config_file_chooser_window_new(); + +void yon_file_chooser_remove_accept_function(filechooser_window *window); dialog_confirmation_data *yon_confirmation_dialog_data_new();