]> git.xonotic.org Git - xonotic/netradiant.git/blobdiff - radiant/main.cpp
Merge commit 'a59855266c41b68a8bfd063713c941eff7449181' into garux-merge
[xonotic/netradiant.git] / radiant / main.cpp
index 0a1eb3b1d3a709630a649ef68f75364cd7b4adfc..fec9894d9d13004ba07d508d958475ba52b82223 100644 (file)
@@ -49,7 +49,7 @@
    \link math/plane.h math/plane.h \endlink - Planes \n
    \link math/aabb.h math/aabb.h \endlink - AABBs \n
 
-   Callback MemberCaller FunctionCaller - callbacks similar to using boost::function with boost::bind \n
+   Callback MemberCaller0 FunctionCaller - callbacks similar to using boost::function with boost::bind \n
    SmartPointer SmartReference - smart-pointer and smart-reference similar to Loki's SmartPtr \n
 
    \link generic/bitfield.h generic/bitfield.h \endlink - Type-safe bitfield \n
@@ -62,6 +62,7 @@
  */
 
 #include "main.h"
+#include "globaldefs.h"
 
 #include "version.h"
 
@@ -69,7 +70,7 @@
 
 #include "iundo.h"
 
-#include <gtk/gtkmain.h>
+#include "uilib/uilib.h"
 
 #include "cmdlib.h"
 #include "os/file.h"
@@ -89,7 +90,7 @@
 #include "referencecache.h"
 #include "stacktrace.h"
 
-#ifdef WIN32
+#if GDEF_OS_WINDOWS
 #include <windows.h>
 #endif
 
@@ -208,19 +209,17 @@ void error_redirect( const gchar *domain, GLogLevelFlags log_level, const gchar
        // spam it...
        globalErrorStream() << buf << "\n";
 
-       // FIXME why are warnings is_fatal?
-#ifndef _DEBUG
-       if ( is_fatal )
-#endif
-       ERROR_MESSAGE( "GTK+ error: " << buf );
+       if (is_fatal) {
+           ERROR_MESSAGE( "GTK+ error: " << buf );
+    }
 }
 
-#if defined ( _DEBUG ) && defined ( WIN32 ) && defined ( _MSC_VER )
+#if GDEF_COMPILER_MSVC && GDEF_DEBUG
 #include "crtdbg.h"
 #endif
 
 void crt_init(){
-#if defined ( _DEBUG ) && defined ( WIN32 ) && defined ( _MSC_VER )
+#if GDEF_COMPILER_MSVC && GDEF_DEBUG
        _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
 #endif
 }
@@ -303,16 +302,16 @@ bool handleMessage(){
        globalErrorStream() << m_buffer.c_str();
        if ( !m_lock.locked() ) {
                ScopedLock lock( m_lock );
-#if defined _DEBUG
-               m_buffer << "Break into the debugger?\n";
-               bool handled = gtk_MessageBox( 0, m_buffer.c_str(), "Radiant - Runtime Error", eMB_YESNO, eMB_ICONERROR ) == eIDNO;
-               m_buffer.clear();
-               return handled;
-#else
-               m_buffer << "Please report this error to the developers\n";
-               gtk_MessageBox( 0, m_buffer.c_str(), "Radiant - Runtime Error", eMB_OK, eMB_ICONERROR );
-               m_buffer.clear();
-#endif
+        if (GDEF_DEBUG) {
+            m_buffer << "Break into the debugger?\n";
+            bool handled = ui::alert(ui::root, m_buffer.c_str(), "Radiant - Runtime Error", ui::alert_type::YESNO, ui::alert_icon::Error) == ui::alert_response::NO;
+            m_buffer.clear();
+            return handled;
+        } else {
+            m_buffer << "Please report this error to the developers\n";
+            ui::alert(ui::root, m_buffer.c_str(), "Radiant - Runtime Error", ui::alert_type::OK, ui::alert_icon::Error);
+            m_buffer.clear();
+        }
        }
        return true;
 }
@@ -326,14 +325,7 @@ void streams_init(){
 }
 
 void paths_init(){
-       const char* home = environment_get_home_path();
-       Q_mkdir( home );
-
-       {
-               StringOutputStream path( 256 );
-               path << home << "1." << RADIANT_MAJOR_VERSION "." << RADIANT_MINOR_VERSION << '/';
-               g_strSettingsPath = path.c_str();
-       }
+       g_strSettingsPath = environment_get_home_path();
 
        Q_mkdir( g_strSettingsPath.c_str() );
 
@@ -376,34 +368,31 @@ bool check_version(){
        // make something idiot proof and someone will make better idiots, this may be overkill
        // let's leave it disabled in debug mode in any case
        // http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=431
-#ifndef _DEBUG
-#define CHECK_VERSION
-#endif
-#ifdef CHECK_VERSION
-       // locate and open RADIANT_MAJOR and RADIANT_MINOR
-       bool bVerIsGood = true;
-       {
-               StringOutputStream ver_file_name( 256 );
-               ver_file_name << AppPath_get() << "RADIANT_MAJOR";
-               bVerIsGood = check_version_file( ver_file_name.c_str(), RADIANT_MAJOR_VERSION );
-       }
-       {
-               StringOutputStream ver_file_name( 256 );
-               ver_file_name << AppPath_get() << "RADIANT_MINOR";
-               bVerIsGood = check_version_file( ver_file_name.c_str(), RADIANT_MINOR_VERSION );
-       }
-
-       if ( !bVerIsGood ) {
-               StringOutputStream msg( 256 );
-               msg << "This editor binary (" RADIANT_VERSION ") doesn't match what the latest setup has configured in this directory\n"
-                                                                                                         "Make sure you run the right/latest editor binary you installed\n"
-                       << AppPath_get();
-               gtk_MessageBox( 0, msg.c_str(), "Radiant", eMB_OK, eMB_ICONDEFAULT );
-       }
-       return bVerIsGood;
-#else
-       return true;
-#endif
+    if (GDEF_DEBUG) {
+        return true;
+    }
+    // locate and open RADIANT_MAJOR and RADIANT_MINOR
+    bool bVerIsGood = true;
+    {
+        StringOutputStream ver_file_name(256);
+        ver_file_name << AppPath_get() << "RADIANT_MAJOR";
+        bVerIsGood = check_version_file(ver_file_name.c_str(), RADIANT_MAJOR_VERSION);
+    }
+    {
+        StringOutputStream ver_file_name(256);
+        ver_file_name << AppPath_get() << "RADIANT_MINOR";
+        bVerIsGood = check_version_file(ver_file_name.c_str(), RADIANT_MINOR_VERSION);
+    }
+
+    if (!bVerIsGood) {
+        StringOutputStream msg(256);
+        msg
+                << "This editor binary (" RADIANT_VERSION ") doesn't match what the latest setup has configured in this directory\n"
+                        "Make sure you run the right/latest editor binary you installed\n"
+                << AppPath_get();
+        ui::alert(ui::root, msg.c_str(), "Radiant", ui::alert_type::OK, ui::alert_icon::Default);
+    }
+    return bVerIsGood;
 }
 
 void create_global_pid(){
@@ -426,25 +415,26 @@ void create_global_pid(){
                if ( remove( g_pidFile.c_str() ) == -1 ) {
                        StringOutputStream msg( 256 );
                        msg << "WARNING: Could not delete " << g_pidFile.c_str();
-                       gtk_MessageBox( 0, msg.c_str(), "Radiant", eMB_OK, eMB_ICONERROR );
+                       ui::alert( ui::root, msg.c_str(), "Radiant", ui::alert_type::OK, ui::alert_icon::Error );
                }
 
                // in debug, never prompt to clean registry, turn console logging auto after a failed start
-#if !defined( _DEBUG )
-               StringOutputStream msg( 256 );
-               msg << "Radiant failed to start properly the last time it was run.\n"
-                          "The failure may be related to current global preferences.\n"
-                          "Do you want to reset global preferences to defaults?";
-
-               if ( gtk_MessageBox( 0, msg.c_str(), "Radiant - Startup Failure", eMB_YESNO, eMB_ICONQUESTION ) == eIDYES ) {
-                       g_GamesDialog.Reset();
-               }
+               if (!GDEF_DEBUG) {
+                       StringOutputStream msg(256);
+                       msg << "Radiant failed to start properly the last time it was run.\n"
+                                       "The failure may be related to current global preferences.\n"
+                                       "Do you want to reset global preferences to defaults?";
+
+                       if (ui::alert(ui::root, msg.c_str(), "Radiant - Startup Failure", ui::alert_type::YESNO, ui::alert_icon::Question) == ui::alert_response::YES) {
+                               g_GamesDialog.Reset();
+                       }
 
-               msg.clear();
-               msg << "Logging console output to " << SettingsPath_get() << "radiant.log\nRefer to the log if Radiant fails to start again.";
+                       msg.clear();
+                       msg << "Logging console output to " << SettingsPath_get()
+                               << "radiant.log\nRefer to the log if Radiant fails to start again.";
 
-               gtk_MessageBox( 0, msg.c_str(), "Radiant - Console Log", eMB_OK );
-#endif
+                       ui::alert(ui::root, msg.c_str(), "Radiant - Console Log", ui::alert_type::OK);
+               }
 
                // set without saving, the class is not in a coherent state yet
                // just do the value change and call to start logging, CGamesDialog will pickup when relevant
@@ -467,7 +457,7 @@ void remove_global_pid(){
        if ( remove( g_pidFile.c_str() ) == -1 ) {
                StringOutputStream msg( 256 );
                msg << "WARNING: Could not delete " << g_pidFile.c_str();
-               gtk_MessageBox( 0, msg.c_str(), "Radiant", eMB_OK, eMB_ICONERROR );
+               ui::alert( ui::root, msg.c_str(), "Radiant", ui::alert_type::OK, ui::alert_icon::Error );
        }
 }
 
@@ -485,25 +475,26 @@ void create_local_pid(){
                if ( remove( g_pidGameFile.c_str() ) == -1 ) {
                        StringOutputStream msg;
                        msg << "WARNING: Could not delete " << g_pidGameFile.c_str();
-                       gtk_MessageBox( 0, msg.c_str(), "Radiant", eMB_OK, eMB_ICONERROR );
+                       ui::alert( ui::root, msg.c_str(), "Radiant", ui::alert_type::OK, ui::alert_icon::Error );
                }
 
                // in debug, never prompt to clean registry, turn console logging auto after a failed start
-#if !defined( _DEBUG )
-               StringOutputStream msg;
-               msg << "Radiant failed to start properly the last time it was run.\n"
-                          "The failure may be caused by current preferences.\n"
-                          "Do you want to reset all preferences to defaults?";
-
-               if ( gtk_MessageBox( 0, msg.c_str(), "Radiant - Startup Failure", eMB_YESNO, eMB_ICONQUESTION ) == eIDYES ) {
-                       Preferences_Reset();
-               }
+               if (!GDEF_DEBUG) {
+                       StringOutputStream msg;
+                       msg << "Radiant failed to start properly the last time it was run.\n"
+                                       "The failure may be caused by current preferences.\n"
+                                       "Do you want to reset all preferences to defaults?";
 
-               msg.clear();
-               msg << "Logging console output to " << SettingsPath_get() << "radiant.log\nRefer to the log if Radiant fails to start again.";
+                       if (ui::alert(ui::root, msg.c_str(), "Radiant - Startup Failure", ui::alert_type::YESNO, ui::alert_icon::Question) == ui::alert_response::YES) {
+                               Preferences_Reset();
+                       }
 
-               gtk_MessageBox( 0, msg.c_str(), "Radiant - Console Log", eMB_OK );
-#endif
+                       msg.clear();
+                       msg << "Logging console output to " << SettingsPath_get()
+                               << "radiant.log\nRefer to the log if Radiant fails to start again.";
+
+                       ui::alert(ui::root, msg.c_str(), "Radiant - Console Log", ui::alert_type::OK);
+               }
 
                // force console logging on! (will go in prefs too)
                g_GamesDialog.m_bForceLogConsole = true;
@@ -548,7 +539,7 @@ int main( int argc, char* argv[] ){
 
        streams_init();
 
-#ifdef WIN32
+#if GDEF_OS_WINDOWS
        HMODULE lib;
        lib = LoadLibrary( "dwmapi.dll" );
        if ( lib != 0 ) {
@@ -572,8 +563,33 @@ int main( int argc, char* argv[] ){
        _setmaxstdio(2048);
 #endif
 
-       gtk_disable_setlocale();
-       gtk_init( &argc, &argv );
+       const char* mapname = NULL;
+    char const *error = NULL;
+       if ( !ui::init( &argc, &argv, "<filename.map>", &error) ) {
+               g_print( "%s\n", error );
+               return -1;
+       }
+
+       // Gtk already removed parsed `--options`
+       if (argc == 2) {
+               if ( strlen( argv[1] ) > 1 ) {
+                       if ( g_str_has_suffix( argv[1], ".map" ) ) {
+                               if ( g_path_is_absolute( argv[1] ) ) {
+                                       mapname = argv[1];
+                               }
+                               else {
+                                       mapname = g_build_filename( g_get_current_dir(), argv[1], NULL );
+                               }
+                       }
+                       else {
+                               g_print( "bad file name, will not load: %s\n", argv[1] );
+                       }
+               }
+       }
+       else if (argc > 2) {
+               g_print ( "%s\n", "too many arguments" );
+               return -1;
+       }
 
        // redirect Gtk warnings to the console
        g_log_set_handler( "Gdk", (GLogLevelFlags)( G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING |
@@ -589,7 +605,7 @@ int main( int argc, char* argv[] ){
 
        GlobalDebugMessageHandler::instance().setHandler( GlobalPopupDebugMessageHandler::instance() );
 
-       environment_init( argc, argv );
+       environment_init(argc, (char const **) argv);
 
        paths_init();
 
@@ -625,8 +641,6 @@ int main( int argc, char* argv[] ){
 
        Radiant_Initialise();
 
-       global_accel_init();
-
        user_shortcuts_init();
 
        g_pParentWnd = 0;
@@ -640,7 +654,10 @@ int main( int argc, char* argv[] ){
        }
        else
 #endif // WIN32
-       if ( g_bLoadLastMap && !g_strLastMap.empty() ) {
+       if ( mapname != NULL ) {
+               Map_LoadFile( mapname );
+       }
+       else if ( g_bLoadLastMap && !g_strLastMap.empty() ) {
                Map_LoadFile( g_strLastMap.c_str() );
        }
        else
@@ -655,7 +672,7 @@ int main( int argc, char* argv[] ){
 
        remove_local_pid();
 
-       gtk_main();
+       ui::main();
 
        // avoid saving prefs when the app is minimized
        if ( g_pParentWnd->IsSleeping() ) {
@@ -673,8 +690,6 @@ int main( int argc, char* argv[] ){
 
        user_shortcuts_save();
 
-       global_accel_destroy();
-
        Radiant_Shutdown();
 
        // close the log file if any