]> git.xonotic.org Git - xonotic/netradiant.git/blobdiff - contrib/sunplug/sunplug.cpp
Merge commit '9c4c8b725fdca551dc9379b77ebd9c498d0c6f28' into master-merge
[xonotic/netradiant.git] / contrib / sunplug / sunplug.cpp
index 50fb2cd8ee681f8abd93d02f2b3596c13251d966..11aca066381717c90fa67ebc983945af20120824 100644 (file)
@@ -19,6 +19,7 @@
  */
 
 #include "sunplug.h"
+#include "globaldefs.h"
 
 #include "debugging/debugging.h"
 
 
 #include <gtk/gtk.h>     // to display something with gtk (windows, buttons etc.), the whole package might not be necessary
 
-void about_plugin_window();
+#define CMD_ABOUT "About..."
+
 void MapCoordinator();
 
-#ifndef _WIN32
+#if !GDEF_OS_WINDOWS
 // linux itoa implementation
 char* itoa( int value, char* result, int base ){
        // check that the base if valid
@@ -72,7 +74,7 @@ char* itoa( int value, char* result, int base ){
 #endif
 
 typedef struct _mapcoord_setting_packet {
-       GtkSpinButton *spinner1, *spinner2, *spinner3, *spinner4;
+    ui::SpinButton spinner1{ui::null}, spinner2{ui::null}, spinner3{ui::null}, spinner4{ui::null};
        Entity* worldspawn;
 } mapcoord_setting_packet;
 
@@ -113,7 +115,7 @@ Entity* Scene_FindEntityByClass( const char* name ){
 // ** GTK callback functions **
 //  **************************
 
-static gboolean delete_event( GtkWidget *widget, GdkEvent *event, gpointer data ){
+static gboolean delete_event(ui::Widget widget, GdkEvent *event, gpointer data ){
        /* If you return FALSE in the "delete_event" signal handler,
         * GTK will emit the "destroy" signal. Returning TRUE means
         * you don't want the window to be destroyed.
@@ -124,17 +126,17 @@ static gboolean delete_event( GtkWidget *widget, GdkEvent *event, gpointer data
 }
 
 // destroy widget if destroy signal is passed to widget
-static void destroy( GtkWidget *widget, gpointer data ){
-       gtk_widget_destroy( widget );
+static void destroy( ui::Widget widget, gpointer data ){
+       widget.destroy();
 }
 
 // function for close button to destroy the toplevel widget
-static void close_window( GtkWidget *widget, gpointer data ){
-       gtk_widget_destroy( gtk_widget_get_toplevel( widget ) );
+static void close_window( ui::Widget widget, gpointer data ){
+       widget.window().destroy();
 }
 
 // callback function to assign the optimal mapcoords to the spinboxes
-static void input_optimal( GtkWidget *widget, gpointer data ){
+static void input_optimal(ui::Widget widget, gpointer data ){
        gtk_spin_button_set_value( msp.spinner1, minX );
        gtk_spin_button_set_value( msp.spinner2, maxY );
        gtk_spin_button_set_value( msp.spinner3, maxX );
@@ -142,12 +144,12 @@ static void input_optimal( GtkWidget *widget, gpointer data ){
 }
 
 // Spinner return value function
-gint grab_int_value( GtkSpinButton *a_spinner, gpointer user_data ) {
+gint grab_int_value(ui::SpinButton a_spinner, gpointer user_data ) {
        return gtk_spin_button_get_value_as_int( a_spinner );
 }
 
 // write the values of the Spinner-Boxes to the worldspawn
-static void set_coordinates( GtkWidget *widget, gpointer data ){
+static void set_coordinates( ui::Widget widget, gpointer data ){
        //Str str_min, str_max;
        char buffer[10], str_min[20], str_max[20];
 
@@ -184,18 +186,18 @@ SunPlugPluginDependencies() :
 //  *************************
 namespace SunPlug
 {
-GtkWindow* main_window;
+ui::Window main_window{ui::null};
 char MenuList[100] = "";
 
 const char* init( void* hApp, void* pMainWidget ){
-       main_window = GTK_WINDOW( pMainWidget );
-       return "Initializing SunPlug for GTKRadiant";
+       main_window = ui::Window::from(pMainWidget);
+       return "Initializing " PLUGIN_NAME " for " RADIANT_NAME;
 }
 const char* getName(){
-       return "SunPlug"; // name that is shown in the menue
+       return PLUGIN_NAME; // name that is shown in the menue
 }
 const char* getCommandList(){
-       const char about[] = "About...";
+       const char about[] = CMD_ABOUT;
        const char etMapCoordinator[] = ";ET-MapCoordinator";
 
        strcat( MenuList, about );
@@ -208,8 +210,19 @@ const char* getCommandTitleList(){
        return "";
 }
 void dispatch( const char* command, float* vMin, float* vMax, bool bSingleBrush ){ // message processing
-       if ( string_equal( command, "About..." ) ) {
-               about_plugin_window();
+       if ( string_equal( command, CMD_ABOUT ) ) {
+               char const *label_text = 
+                       PLUGIN_NAME " " PLUGIN_VERSION " for "
+                       RADIANT_NAME " " RADIANT_VERSION "\n\n"
+                       "Written by Topsun\n\n"
+                       "Built against "
+                       RADIANT_NAME " " RADIANT_VERSION_STRING "\n"
+                       __DATE__;
+
+               GlobalRadiant().m_pfnMessageBox( main_window, label_text,
+                                                                               "About " PLUGIN_NAME,
+                                                                               eMB_OK,
+                                                                               eMB_ICONDEFAULT );
        }
        if ( string_equal( command, "ET-MapCoordinator" ) ) {
                MapCoordinator();
@@ -222,7 +235,7 @@ class SunPlugModule : public TypeSystemRef
 _QERPluginTable m_plugin;
 public:
 typedef _QERPluginTable Type;
-STRING_CONSTANT( Name, "SunPlug" );
+STRING_CONSTANT( Name, PLUGIN_NAME );
 
 SunPlugModule(){
        m_plugin.m_pfnQERPlug_Init = &SunPlug::init;
@@ -251,35 +264,6 @@ extern "C" void RADIANT_DLLEXPORT Radiant_RegisterModules( ModuleServer& server
 // ** my stuff **
 //  ************
 
-// About dialog
-void about_plugin_window(){
-       GtkWidget *window, *vbox, *label, *button;
-
-       window = gtk_window_new( GTK_WINDOW_TOPLEVEL ); // create a window
-       gtk_window_set_transient_for( GTK_WINDOW( window ), SunPlug::main_window ); // make the window to stay in front of the main window
-       g_signal_connect( G_OBJECT( window ), "delete_event", G_CALLBACK( delete_event ), NULL ); // connect the delete event
-       g_signal_connect( G_OBJECT( window ), "destroy", G_CALLBACK( destroy ), NULL ); // connect the destroy event for the window
-       gtk_window_set_title( GTK_WINDOW( window ), "About SunPlug" ); // set the title of the window for the window
-       gtk_window_set_resizable( GTK_WINDOW( window ), FALSE ); // don't let the user resize the window
-       gtk_window_set_modal( GTK_WINDOW( window ), TRUE ); // force the user not to do something with the other windows
-       gtk_container_set_border_width( GTK_CONTAINER( window ), 10 ); // set the border of the window
-
-       vbox = gtk_vbox_new( FALSE, 10 ); // create a box to arrange new objects vertically
-       gtk_container_add( GTK_CONTAINER( window ), vbox ); // add the box to the window
-
-       label = gtk_label_new( "SunPlug v1.0 for NetRadiant 1.5\nby Topsun" ); // create a label
-       gtk_label_set_justify( GTK_LABEL( label ), GTK_JUSTIFY_LEFT ); // text align left
-       gtk_box_pack_start( GTK_BOX( vbox ), label, FALSE, FALSE, 2 ); // insert the label in the box
-
-       button = gtk_button_new_with_label( "OK" ); // create a button with text
-       g_signal_connect_swapped( G_OBJECT( button ), "clicked", G_CALLBACK( gtk_widget_destroy ), window ); // connect the click event to close the window
-       gtk_box_pack_start( GTK_BOX( vbox ), button, FALSE, FALSE, 2 ); // insert the button in the box
-
-       gtk_window_set_position( GTK_WINDOW( window ), GTK_WIN_POS_CENTER ); // center the window on screen
-
-       gtk_widget_show_all( window ); // show the window and all subelements
-}
-
 // get the current bounding box and return the optimal coordinates
 void GetOptimalCoordinates( AABB *levelBoundingBox ){
        int half_width, half_heigth, center_x, center_y;
@@ -314,24 +298,22 @@ void GetOptimalCoordinates( AABB *levelBoundingBox ){
 
 // MapCoordinator dialog window
 void MapCoordinator(){
-       GtkWidget *window, *vbox, *table, *label, *spinnerMinX, *spinnerMinY, *spinnerMaxX, *spinnerMaxY, *button;
-       GtkAdjustment *spinner_adj_MinX, *spinner_adj_MinY, *spinner_adj_MaxX, *spinner_adj_MaxY;
        Entity *theWorldspawn = NULL;
        const char *buffer;
        char line[20];
 
        // in any case we need a window to show the user what to do
-       window = gtk_window_new( GTK_WINDOW_TOPLEVEL ); // create the window
-       gtk_window_set_transient_for( GTK_WINDOW( window ), SunPlug::main_window ); // make the window to stay in front of the main window
-       g_signal_connect( G_OBJECT( window ), "delete_event", G_CALLBACK( delete_event ), NULL ); // connect the delete event for the window
-       g_signal_connect( G_OBJECT( window ), "destroy", G_CALLBACK( destroy ), NULL ); // connect the destroy event for the window
-       gtk_window_set_title( GTK_WINDOW( window ), "ET-MapCoordinator" ); // set the title of the window for the window
-       gtk_window_set_resizable( GTK_WINDOW( window ), FALSE ); // don't let the user resize the window
-       gtk_window_set_modal( GTK_WINDOW( window ), TRUE ); // force the user not to do something with the other windows
+       auto window = ui::Window( ui::window_type::TOP ); // create the window
+       gtk_window_set_transient_for( window, SunPlug::main_window ); // make the window to stay in front of the main window
+       window.connect( "delete_event", G_CALLBACK( delete_event ), NULL ); // connect the delete event for the window
+       window.connect( "destroy", G_CALLBACK( destroy ), NULL ); // connect the destroy event for the window
+       gtk_window_set_title( window, "ET-MapCoordinator" ); // set the title of the window for the window
+       gtk_window_set_resizable( window, FALSE ); // don't let the user resize the window
+       gtk_window_set_modal( window, TRUE ); // force the user not to do something with the other windows
        gtk_container_set_border_width( GTK_CONTAINER( window ), 10 ); // set the border of the window
 
-       vbox = gtk_vbox_new( FALSE, 10 ); // create a box to arrange new objects vertically
-       gtk_container_add( GTK_CONTAINER( window ), vbox ); // add the box to the window
+       auto vbox = ui::VBox( FALSE, 10 ); // create a box to arrange new objects vertically
+       window.add(vbox);
 
        scene::Path path = makeReference( GlobalSceneGraph().root() ); // get the path to the root element of the graph
        scene::Instance* instance = GlobalSceneGraph().find( path ); // find the instance to the given path
@@ -366,77 +348,77 @@ void MapCoordinator(){
                globalOutputStream() << "SunPlug: adviced mapcoordsmins=" << minX << " " << maxY << "\n"; // console info about mapcoordsmins
                globalOutputStream() << "SunPlug: adviced mapcoordsmaxs=" << maxX << " " << minY << "\n"; // console info about mapcoordsmaxs
 
-               spinner_adj_MinX = (GtkAdjustment *)gtk_adjustment_new( map_minX, -65536.0, 65536.0, 1.0, 5.0, 0 ); // create adjustment for value and range of minimum x value
-               spinner_adj_MinY = (GtkAdjustment *)gtk_adjustment_new( map_minY, -65536.0, 65536.0, 1.0, 5.0, 0 ); // create adjustment for value and range of minimum y value
-               spinner_adj_MaxX = (GtkAdjustment *)gtk_adjustment_new( map_maxX, -65536.0, 65536.0, 1.0, 5.0, 0 ); // create adjustment for value and range of maximum x value
-               spinner_adj_MaxY = (GtkAdjustment *)gtk_adjustment_new( map_maxY, -65536.0, 65536.0, 1.0, 5.0, 0 ); // create adjustment for value and range of maximum y value
+               auto spinner_adj_MinX = ui::Adjustment( map_minX, -65536.0, 65536.0, 1.0, 5.0, 0 ); // create adjustment for value and range of minimum x value
+               auto spinner_adj_MinY = ui::Adjustment( map_minY, -65536.0, 65536.0, 1.0, 5.0, 0 ); // create adjustment for value and range of minimum y value
+               auto spinner_adj_MaxX = ui::Adjustment( map_maxX, -65536.0, 65536.0, 1.0, 5.0, 0 ); // create adjustment for value and range of maximum x value
+               auto spinner_adj_MaxY = ui::Adjustment( map_maxY, -65536.0, 65536.0, 1.0, 5.0, 0 ); // create adjustment for value and range of maximum y value
 
-               button = gtk_button_new_with_label( "Get optimal mapcoords" ); // create button with text
-               g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( input_optimal ), NULL ); // connect button with callback function
-               gtk_box_pack_start( GTK_BOX( vbox ), button, FALSE, FALSE, 2 ); // insert button into vbox
+               auto button = ui::Button( "Get optimal mapcoords" ); // create button with text
+               button.connect( "clicked", G_CALLBACK( input_optimal ), NULL ); // connect button with callback function
+               vbox.pack_start( button, FALSE, FALSE, 2 ); // insert button into vbox
 
-               gtk_box_pack_start( GTK_BOX( vbox ), gtk_hseparator_new(), FALSE, FALSE, 2 ); // insert separator into vbox
+               vbox.pack_start( ui::Widget::from(gtk_hseparator_new()), FALSE, FALSE, 2 ); // insert separator into vbox
 
-               table = gtk_table_new( 4, 3, TRUE ); // create table
-               gtk_table_set_row_spacings( GTK_TABLE( table ), 8 ); // set row spacings
-               gtk_table_set_col_spacings( GTK_TABLE( table ), 8 ); // set column spacings
-               gtk_box_pack_start( GTK_BOX( vbox ), table, FALSE, FALSE, 2 ); // insert table into vbox
+               auto table = ui::Table( 4, 3, TRUE ); // create table
+        gtk_table_set_row_spacings(table, 8); // set row spacings
+        gtk_table_set_col_spacings(table, 8); // set column spacings
+               vbox.pack_start( table, FALSE, FALSE, 2 ); // insert table into vbox
 
-               label = gtk_label_new( "x" ); // create label
+               auto label = ui::Label( "x" ); // create label
                gtk_label_set_justify( GTK_LABEL( label ), GTK_JUSTIFY_LEFT ); // align text to the left side
-               gtk_table_attach_defaults( GTK_TABLE( table ), label, 1, 2, 0, 1 ); // insert label into table
+        table.attach(label, {1, 2, 0, 1}); // insert label into table
 
-               label = gtk_label_new( "y" ); // create label
+               label = ui::Label( "y" ); // create label
                gtk_label_set_justify( GTK_LABEL( label ), GTK_JUSTIFY_LEFT ); // align text to the left side
-               gtk_table_attach_defaults( GTK_TABLE( table ), label, 2, 3, 0, 1 ); // insert label into table
+        table.attach(label, {2, 3, 0, 1}); // insert label into table
 
-               label = gtk_label_new( "mapcoordsmins" ); // create label
+               label = ui::Label( "mapcoordsmins" ); // create label
                gtk_label_set_justify( GTK_LABEL( label ), GTK_JUSTIFY_LEFT ); // align text to the left side
-               gtk_table_attach_defaults( GTK_TABLE( table ), label, 0, 1, 1, 2 ); // insert label into table
+        table.attach(label, {0, 1, 1, 2}); // insert label into table
 
-               spinnerMinX = gtk_spin_button_new( spinner_adj_MinX, 1.0, 0 ); // create textbox wiht value spin, value and value range
-               gtk_table_attach_defaults( GTK_TABLE( table ), spinnerMinX, 1, 2, 1, 2 ); // insert spinbox into table
+        auto spinnerMinX = ui::SpinButton(spinner_adj_MinX, 1.0, 0); // create textbox wiht value spin, value and value range
+        table.attach(spinnerMinX, {1, 2, 1, 2}); // insert spinbox into table
 
-               spinnerMinY = gtk_spin_button_new( spinner_adj_MinY, 1.0, 0 ); // create textbox wiht value spin, value and value range
-               gtk_table_attach_defaults( GTK_TABLE( table ), spinnerMinY, 2, 3, 1, 2 ); // insert spinbox into table
+        auto spinnerMinY = ui::SpinButton(spinner_adj_MinY, 1.0, 0); // create textbox wiht value spin, value and value range
+        table.attach(spinnerMinY, {2, 3, 1, 2}); // insert spinbox into table
 
-               label = gtk_label_new( "mapcoordsmaxs" ); // create label
+               label = ui::Label( "mapcoordsmaxs" ); // create label
                gtk_label_set_justify( GTK_LABEL( label ), GTK_JUSTIFY_LEFT ); // align text to the left side
-               gtk_table_attach_defaults( GTK_TABLE( table ), label, 0, 1, 2, 3 ); // insert label into table
+        table.attach(label, {0, 1, 2, 3}); // insert label into table
 
-               spinnerMaxX = gtk_spin_button_new( spinner_adj_MaxX, 1.0, 0 ); // create textbox wiht value spin, value and value range
-               gtk_table_attach_defaults( GTK_TABLE( table ), spinnerMaxX, 1, 2, 2, 3 ); // insert spinbox into table
+        auto spinnerMaxX = ui::SpinButton(spinner_adj_MaxX, 1.0, 0); // create textbox wiht value spin, value and value range
+        table.attach(spinnerMaxX, {1, 2, 2, 3}); // insert spinbox into table
 
-               spinnerMaxY = gtk_spin_button_new( spinner_adj_MaxY, 1.0, 0 ); // create textbox wiht value spin, value and value range
-               gtk_table_attach_defaults( GTK_TABLE( table ), spinnerMaxY, 2, 3, 2, 3 ); // insert spinbox into table
+        auto spinnerMaxY = ui::SpinButton(spinner_adj_MaxY, 1.0, 0); // create textbox wiht value spin, value and value range
+        table.attach(spinnerMaxY, {2, 3, 2, 3}); // insert spinbox into table
 
                // put the references to the spinboxes and the worldspawn into the global exchange
-               msp.spinner1 = GTK_SPIN_BUTTON( spinnerMinX );
-               msp.spinner2 = GTK_SPIN_BUTTON( spinnerMinY );
-               msp.spinner3 = GTK_SPIN_BUTTON( spinnerMaxX );
-               msp.spinner4 = GTK_SPIN_BUTTON( spinnerMaxY );
+        msp.spinner1 = spinnerMinX;
+        msp.spinner2 = spinnerMinY;
+        msp.spinner3 = spinnerMaxX;
+        msp.spinner4 = spinnerMaxY;
                msp.worldspawn = theWorldspawn;
 
-               button = gtk_button_new_with_label( "Set" ); // create button with text
-               g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( set_coordinates ), NULL ); // connect button with callback function
-               gtk_table_attach_defaults( GTK_TABLE( table ), button, 1, 2, 3, 4 ); // insert button into table
+               button = ui::Button( "Set" ); // create button with text
+               button.connect( "clicked", G_CALLBACK( set_coordinates ), NULL ); // connect button with callback function
+        table.attach(button, {1, 2, 3, 4}); // insert button into table
 
-               button = gtk_button_new_with_label( "Cancel" ); // create button with text
-               g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( close_window ), NULL ); // connect button with callback function
-               gtk_table_attach_defaults( GTK_TABLE( table ), button, 2, 3, 3, 4 ); // insert button into table
+               button = ui::Button( "Cancel" ); // create button with text
+               button.connect( "clicked", G_CALLBACK( close_window ), NULL ); // connect button with callback function
+        table.attach(button, {2, 3, 3, 4}); // insert button into table
        }
        else {
                globalOutputStream() << "SunPlug: no worldspawn found!\n"; // output error to console
 
-               label = gtk_label_new( "ERROR: No worldspawn was found in the map!\nIn order to use this tool the map must have at least one brush in the worldspawn. " ); // create a label
+               auto label = ui::Label( "ERROR: No worldspawn was found in the map!\nIn order to use this tool the map must have at least one brush in the worldspawn. " ); // create a label
                gtk_label_set_justify( GTK_LABEL( label ), GTK_JUSTIFY_LEFT ); // text align left
-               gtk_box_pack_start( GTK_BOX( vbox ), label, FALSE, FALSE, 2 ); // insert the label in the box
+               vbox.pack_start( label, FALSE, FALSE, 2 ); // insert the label in the box
 
-               button = gtk_button_new_with_label( "OK" ); // create a button with text
-               g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( close_window ), NULL ); // connect the click event to close the window
-               gtk_box_pack_start( GTK_BOX( vbox ), button, FALSE, FALSE, 2 ); // insert the button in the box
+               auto button = ui::Button( "OK" ); // create a button with text
+               button.connect( "clicked", G_CALLBACK( close_window ), NULL ); // connect the click event to close the window
+               vbox.pack_start( button, FALSE, FALSE, 2 ); // insert the button in the box
        }
 
-       gtk_window_set_position( GTK_WINDOW( window ), GTK_WIN_POS_CENTER ); // center the window
+       gtk_window_set_position( window, GTK_WIN_POS_CENTER ); // center the window
        gtk_widget_show_all( window ); // show the window and all subelements
 }