2
0
Эх сурвалжийг харах

Merge pull request #1313 from rlofc/next

Added GTK-based displayFileDialog implementation for Linux
Sean Taylor 12 жил өмнө
parent
commit
ce9fabb636

+ 9 - 0
gameplay/CMakeLists.txt

@@ -622,6 +622,15 @@ include_directories(
     ../external-deps/glew/include
 )
 
+option(WITH_GTK_EXTENSIONS "Add GTK extensions to GamePlay" ON)
+if(WITH_GTK_EXTENSIONS)
+   add_definitions(-DUSE_GTK_EXTENSIONS=1)
+   find_package(PkgConfig REQUIRED)
+   pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
+   include_directories(${GTK3_INCLUDE_DIRS})
+   add_definitions(${GTK3_CFLAGS_OTHER})
+endif()
+
 add_definitions(-D__linux__)
 add_definitions(-lstdc++)
 

+ 1 - 1
gameplay/src/FileSystem.h

@@ -104,7 +104,7 @@ public:
      * @param filterDescription The file filter description. (Ex. All Files or Image Files)
      * @param filterExtensions The extensions to filter on. (Ex. png;bmp)
      * @param initialDirectory The initial directory to start. NULL runs from the executable directory.
-     * @return The file that is opened or saved.
+     * @return The file that is opened or saved, or an empty string if canceled.
      *
      * @script{ignore}
      */

+ 1 - 1
gameplay/src/Platform.h

@@ -406,7 +406,7 @@ public:
      * @param filterDescription The file filter description. (Ex. Image Files)
      * @param filterExtensions The semi-colon delimited list of filtered file extensions. (Ex. png;jpg;bmp)
      * @param initialDirectory The initial directory to open or save files from. (Ex. "res") If NULL this will use the executable directory.
-     * @return The file that is opened or saved.
+     * @return The file that is opened or saved, or an empty string if canceled.
      *
      * @script{ignore}
      */

+ 72 - 5
gameplay/src/PlatformLinux.cpp

@@ -20,6 +20,12 @@
 #include <errno.h>
 #include <fstream>
 
+#ifdef USE_GTK_EXTENSIONS
+#include <glib.h>
+#include <glib/gi18n.h>
+#include <gtk/gtk.h>
+#endif
+
 #define TOUCH_COUNT_MAX     4
 #define MAX_GAMEPADS 4
 
@@ -550,10 +556,6 @@ extern void print(const char* format, ...)
         return strcasecmp(s1, s2);
     }
 
-    Platform::Platform(Game* game) : _game(game)
-    {
-    }
-
 Platform::Platform(Game* game) : _game(game)
 {
 }
@@ -1672,7 +1674,72 @@ bool Platform::launchURL(const char* url)
 
 std::string Platform::displayFileDialog(size_t mode, const char* title, const char* filterDescription, const char* filterExtensions, const char* initialDirectory)
 {
-    return "";
+    std::string filename="";
+
+#ifdef USE_GTK_EXTENSIONS
+    if (!gtk_init_check(NULL,NULL))
+        return "";
+
+    // Create the dialog in one of two modes, SAVE or OPEN
+    GtkWidget *dialog;
+    dialog = gtk_file_chooser_dialog_new (title,
+        NULL,
+        mode == FileSystem::SAVE ? GTK_FILE_CHOOSER_ACTION_SAVE : GTK_FILE_CHOOSER_ACTION_OPEN,
+        _("_Cancel"), GTK_RESPONSE_CANCEL,
+        mode == FileSystem::SAVE ? _("_Save") : _("_Open"),
+        GTK_RESPONSE_ACCEPT,
+        NULL);
+
+    // Filter on extensions
+    GtkFileFilter *filter = gtk_file_filter_new();
+    std::istringstream f(filterExtensions);
+    std::string s;
+    std::string descStr = filterDescription;
+    std::string extStr;
+    while (std::getline(f, s, ';'))
+    {
+        extStr = "*.";
+        extStr += s;
+        gtk_file_filter_add_pattern( filter, extStr.c_str() );
+    }
+    gtk_file_chooser_set_filter( GTK_FILE_CHOOSER (dialog), filter );
+
+    // Set initial directory
+    std::string initialDirectoryStr;
+    if (initialDirectory == NULL)
+    {
+        char* currentDir = g_get_current_dir();
+        initialDirectoryStr = currentDir;
+        g_free (currentDir);
+    }
+    else
+    {
+        initialDirectoryStr = initialDirectory;
+    }
+    gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), initialDirectoryStr.c_str());
+
+    if (mode==FileSystem::SAVE)
+    {
+        gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);
+        gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), "untitled");
+    }
+
+    // Finally, show the dialog
+    if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
+    {
+        char *szFilename;
+        szFilename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
+        filename = szFilename;
+        g_free (szFilename);
+    }
+
+    gtk_widget_destroy (dialog);
+
+    // Since we are not using gtk_main(), this will let the dialog close
+    while (gtk_events_pending ()) gtk_main_iteration ();
+#endif
+
+    return filename;
 }
 
 }

+ 4 - 1
samples/CMakeLists.txt

@@ -42,9 +42,12 @@ set(GAMEPLAY_LIBRARIES
     dl
     X11
     pthread
+    gtk-3
+    glib-2.0
+    gobject-2.0
 ) 
 
-add_definitions(-lstdc++ -lgameplay -lm -llua -lz -lpng -lvorbis -logg -lBulletCollision -lBulletDynamics -lLinearMath -lopenal -LGLEW -lGL -lrt -ldl -lX11 -lpthread)
+add_definitions(-lstdc++ -lgameplay -lm -llua -lz -lpng -lvorbis -logg -lBulletCollision -lBulletDynamics -lLinearMath -lopenal -LGLEW -lGL -lrt -ldl -lX11 -lpthread -lgtk-3 -lglib-2.0 -lgobject-2.0)
 
 add_subdirectory(browser)
 add_subdirectory(character)

+ 4 - 1
samples/browser/CMakeLists.txt

@@ -41,9 +41,12 @@ set(GAMEPLAY_LIBRARIES
     dl
     X11
     pthread
+    gtk-3
+    glib-2.0
+    gobject-2.0
 ) 
 
-add_definitions(-lstdc++ -lgameplay -lm -llua -lz -lpng -lvorbis -logg -lBulletCollision -lBulletDynamics -lLinearMath -lopenal -LGLEW -lGL -lrt -ldl -lX11 -lpthread)
+add_definitions(-lstdc++ -lgameplay -lm -llua -lz -lpng -lvorbis -logg -lBulletCollision -lBulletDynamics -lLinearMath -lopenal -LGLEW -lGL -lrt -ldl -lX11 -lpthread -lgtk-3 -lglib-2.0 -lgobject-2.0)
 
 set( GAME_NAME sample-browser)
 

+ 4 - 0
template/template-CMakeLists.txt

@@ -118,6 +118,10 @@ IF (TARGET_OS STREQUAL "LINUX")
 	append_gameplay_ext_lib(GAMEPLAY_LIBRARIES "dl" "")
 	append_gameplay_ext_lib(GAMEPLAY_LIBRARIES "rt" "" )
 	append_gameplay_ext_lib(GAMEPLAY_LIBRARIES "pthread" "" )
+	append_gameplay_ext_lib(GAMEPLAY_LIBRARIES "gtk-3" "" )
+	append_gameplay_ext_lib(GAMEPLAY_LIBRARIES "gobject-2.0" "" )
+	append_gameplay_ext_lib(GAMEPLAY_LIBRARIES "glib-2.0" "" )
+
 ELSEIF (TARGET_OS STREQUAL "WINDOWS")
 	set(GAMEPLAY_LIBRARIES ${GAMEPLAY_LIBRARIES} "OpenGL32")
 	set(GAMEPLAY_LIBRARIES ${GAMEPLAY_LIBRARIES} "GLU32")