]> git.xonotic.org Git - xonotic/netradiant.git/blobdiff - libs/gtkutil/filechooser.cpp
Merge commit '6291935031c28286b34cbf4b483181e51c1d8ea6' into master-merge
[xonotic/netradiant.git] / libs / gtkutil / filechooser.cpp
index d65d310ca567dc3eac3d47da95ccb6b946f37b56..abfc225dcc4b05136bf91fff90ec924d52ccffe4 100644 (file)
 
 #include <list>
 #include <vector>
-#include <gtk/gtkwidget.h>
-#include <gtk/gtkwindow.h>
-#include <gtk/gtkfilechooser.h>
-#include <gtk/gtkfilechooserdialog.h>
-#include <gtk/gtkstock.h>
+#include <gtk/gtk.h>
+#include <uilib/uilib.h>
 
 #include "string/string.h"
 #include "stream/stringstream.h"
@@ -127,7 +124,7 @@ filetype_pair_t GetTypeForGTKMask( const char *mask ) const {
 
 static char g_file_dialog_file[1024];
 
-const char* file_dialog_show( GtkWidget* parent, bool open, const char* title, const char* path, const char* pattern, bool want_load, bool want_import, bool want_save ){
+const char* file_dialog_show( ui::Window parent, bool open, const char* title, const char* path, const char* pattern, bool want_load, bool want_import, bool want_save ){
        filetype_t type;
 
        if ( pattern == 0 ) {
@@ -143,54 +140,61 @@ const char* file_dialog_show( GtkWidget* parent, bool open, const char* title, c
                title = open ? "Open File" : "Save File";
        }
 
-       GtkWidget* dialog;
+       ui::Dialog dialog{ui::null};
        if ( open ) {
-               dialog = gtk_file_chooser_dialog_new( title,
-                                                                                         GTK_WINDOW( parent ),
+               dialog = ui::Dialog::from(gtk_file_chooser_dialog_new( title,
+                                                                                         parent,
                                                                                          GTK_FILE_CHOOSER_ACTION_OPEN,
                                                                                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                                                                          GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
-                                                                                         NULL );
+                                                                                         NULL ));
        }
        else
        {
-               dialog = gtk_file_chooser_dialog_new( title,
-                                                                                         GTK_WINDOW( parent ),
+               dialog = ui::Dialog::from(gtk_file_chooser_dialog_new( title,
+                                                                                         parent,
                                                                                          GTK_FILE_CHOOSER_ACTION_SAVE,
                                                                                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                                                                          GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
-                                                                                         NULL );
+                                                                                         NULL ));
                gtk_file_chooser_set_current_name( GTK_FILE_CHOOSER( dialog ), "unnamed" );
        }
 
-       gtk_window_set_modal( GTK_WINDOW( dialog ), TRUE );
-       gtk_window_set_position( GTK_WINDOW( dialog ), GTK_WIN_POS_CENTER_ON_PARENT );
+       gtk_window_set_modal( dialog, TRUE );
+       gtk_window_set_position( dialog, GTK_WIN_POS_CENTER_ON_PARENT );
 
        // we expect an actual path below, if the path is 0 we might crash
-       if ( path != 0 && !string_empty( path ) ) {
+       if ( path != nullptr && !string_empty( path ) ) {
                ASSERT_MESSAGE( path_is_absolute( path ), "file_dialog_show: path not absolute: " << makeQuoted( path ) );
 
-               Array<char> new_path( strlen( path ) + 1 );
+               std::string new_path( path );
 
-               // copy path, replacing dir separators as appropriate
-               Array<char>::iterator w = new_path.begin();
-               for ( const char* r = path; *r != '\0'; ++r )
-               {
-                       *w++ = ( *r == '/' ) ? G_DIR_SEPARATOR : *r;
+               // replacing dir separators as appropriate
+               for ( char &c : new_path ) {
+                       if ( c == '/' ) {
+                              c = G_DIR_SEPARATOR;
+                       }
                }
                // remove separator from end of path if required
-               if ( *( w - 1 ) == G_DIR_SEPARATOR ) {
-                       --w;
+               if ( new_path.back() == G_DIR_SEPARATOR ) {
+                       new_path.pop_back();
                }
-               // terminate string
-               *w = '\0';
 
-               gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER( dialog ), new_path.data() );
+               gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER( dialog ), new_path.c_str() );
        }
 
        // we should add all important paths as shortcut folder...
        // gtk_file_chooser_add_shortcut_folder(GTK_FILE_CHOOSER(dialog), "/tmp/", NULL);
 
+       if ( open && masks.m_filters.size() > 1 ){
+               GtkFileFilter* filter = gtk_file_filter_new();
+               gtk_file_filter_set_name( filter, "Supported formats" );
+               for ( std::size_t i = 0; i < masks.m_filters.size(); ++i )
+               {
+                       gtk_file_filter_add_pattern( filter, masks.m_filters[i].c_str() );
+               }
+               gtk_file_chooser_add_filter( GTK_FILE_CHOOSER( dialog ), filter );
+       }
 
        for ( std::size_t i = 0; i < masks.m_filters.size(); ++i )
        {
@@ -205,7 +209,7 @@ const char* file_dialog_show( GtkWidget* parent, bool open, const char* title, c
 
                if ( !string_equal( pattern, "*" ) ) {
                        GtkFileFilter* filter = gtk_file_chooser_get_filter( GTK_FILE_CHOOSER( dialog ) );
-                       if ( filter != 0 ) { // no filter set? some file-chooser implementations may allow the user to set no filter, which we treat as 'all files'
+                       if ( filter != 0 && !string_equal( gtk_file_filter_get_name( filter ), "Supported formats" ) ) { // no filter set? some file-chooser implementations may allow the user to set no filter, which we treat as 'all files'
                                type = masks.GetTypeForGTKMask( gtk_file_filter_get_name( filter ) ).m_type;
                                // last ext separator
                                const char* extension = path_get_extension( g_file_dialog_file );
@@ -233,7 +237,7 @@ const char* file_dialog_show( GtkWidget* parent, bool open, const char* title, c
                g_file_dialog_file[0] = '\0';
        }
 
-       gtk_widget_destroy( dialog );
+       ui::Widget(dialog).destroy();
 
        // don't return an empty filename
        if ( g_file_dialog_file[0] == '\0' ) {
@@ -243,16 +247,16 @@ const char* file_dialog_show( GtkWidget* parent, bool open, const char* title, c
        return g_file_dialog_file;
 }
 
-char* dir_dialog( GtkWidget* parent, const char* title, const char* path ){
-       GtkWidget* dialog = gtk_file_chooser_dialog_new( title,
-                                                                                                        GTK_WINDOW( parent ),
+char* dir_dialog( ui::Window parent, const char* title, const char* path ){
+       auto dialog = ui::Dialog::from(gtk_file_chooser_dialog_new( title,
+                                                                                                        parent,
                                                                                                         GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
                                                                                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                                                                                         GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
-                                                                                                        NULL );
+                                                                                                        NULL ));
 
-       gtk_window_set_modal( GTK_WINDOW( dialog ), TRUE );
-       gtk_window_set_position( GTK_WINDOW( dialog ), GTK_WIN_POS_CENTER_ON_PARENT );
+       gtk_window_set_modal( dialog, TRUE );
+       gtk_window_set_position( dialog, GTK_WIN_POS_CENTER_ON_PARENT );
 
        if ( !string_empty( path ) ) {
                gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER( dialog ), path );
@@ -263,20 +267,20 @@ char* dir_dialog( GtkWidget* parent, const char* title, const char* path ){
                filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( dialog ) );
        }
 
-       gtk_widget_destroy( dialog );
+       dialog.destroy();
 
        return filename;
 }
 
-const char* file_dialog( GtkWidget* parent, bool open, const char* title, const char* path, const char* pattern, bool want_load, bool want_import, bool want_save ){
+const char* file_dialog( ui::Window parent, bool open, const char* title, const char* path, const char* pattern, bool want_load, bool want_import, bool want_save ){
        for (;; )
        {
                const char* file = file_dialog_show( parent, open, title, path, pattern, want_load, want_import, want_save );
 
                if ( open
-                        || file == 0
+                        || !file
                         || !file_exists( file )
-                        || gtk_MessageBox( parent, "The file specified already exists.\nDo you want to replace it?", title, eMB_NOYES, eMB_ICONQUESTION ) == eIDYES ) {
+                        || ui::alert(parent, "The file specified already exists.\nDo you want to replace it?", title, ui::alert_type::NOYES, ui::alert_icon::Question ) == ui::alert_response::YES ) {
                        return file;
                }
        }