]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - radiant/patchmanip.cpp
Merge commit 'dce6730b39a2e6484015e483c46edbe93054f6a0' into garux-merge
[xonotic/netradiant.git] / radiant / patchmanip.cpp
index 9ea7c5d81cf015b14e99761bcf3c6139a68fafcd..7a764976f4395ff34850c725bb19305707923533 100644 (file)
@@ -21,6 +21,9 @@
 
 #include "patchmanip.h"
 
+#include <gtk/gtk.h>
+#include <gdk/gdkkeysyms.h>
+
 #include "debugging/debugging.h"
 
 
@@ -67,6 +70,32 @@ void Scene_PatchConstructPrefab( scene::Graph& graph, const AABB aabb, const cha
        }
 }
 
+void PatchAutoCapTexture( Patch& patch ) {
+
+       AABB box = patch.localAABB();
+       float x = box.extents.x();
+       float y = box.extents.y();
+       float z = box.extents.z();
+
+       int cap_direction = -1;
+       if ( x < y  && x < z )
+               cap_direction = 0;
+       else if ( y < x  && y < z )
+               cap_direction = 1;
+       else if ( z < x  && z < x )
+               cap_direction = 2;
+
+       if ( cap_direction >= 0 )
+               patch.ProjectTexture(cap_direction);
+       else
+               patch.NaturalTexture();
+}
+
+void Patch_AutoCapTexture(){
+       UndoableCommand command( "patchAutoCapTexture" );
+       Scene_forEachVisibleSelectedPatch( &PatchAutoCapTexture );
+       SceneChangeNotify();
+}
 
 void Patch_makeCaps( Patch& patch, scene::Instance& instance, EPatchCap type, const char* shader ){
        if ( ( type == eCapEndCap || type == eCapIEndCap )
@@ -89,8 +118,10 @@ void Patch_makeCaps( Patch& patch, scene::Instance& instance, EPatchCap type, co
                NodeSmartReference cap( g_patchCreator->createPatch() );
                Node_getTraversable( instance.path().parent() )->insert( cap );
 
-               patch.MakeCap( Node_getPatch( cap ), type, ROW, true );
-               Node_getPatch( cap )->SetShader( shader );
+               Patch* cap_patch = Node_getPatch( cap );
+               patch.MakeCap( cap_patch, type, ROW, true );
+               cap_patch->SetShader( shader );
+               PatchAutoCapTexture(*cap_patch);
 
                scene::Path path( instance.path() );
                path.pop();
@@ -102,8 +133,10 @@ void Patch_makeCaps( Patch& patch, scene::Instance& instance, EPatchCap type, co
                NodeSmartReference cap( g_patchCreator->createPatch() );
                Node_getTraversable( instance.path().parent() )->insert( cap );
 
-               patch.MakeCap( Node_getPatch( cap ), type, ROW, false );
-               Node_getPatch( cap )->SetShader( shader );
+               Patch* cap_patch = Node_getPatch( cap );
+               patch.MakeCap( cap_patch, type, ROW, false );
+               cap_patch->SetShader( shader );
+               PatchAutoCapTexture(*cap_patch);
 
                scene::Path path( instance.path() );
                path.pop();
@@ -115,17 +148,6 @@ void Patch_makeCaps( Patch& patch, scene::Instance& instance, EPatchCap type, co
 
 typedef std::vector<scene::Instance*> InstanceVector;
 
-class PatchStoreInstance
-{
-InstanceVector& m_instances;
-public:
-PatchStoreInstance( InstanceVector& instances ) : m_instances( instances ){
-}
-void operator()( PatchInstance& patch ) const {
-       m_instances.push_back( &patch );
-}
-};
-
 enum ECapDialog {
        PATCHCAP_BEVEL = 0,
        PATCHCAP_ENDCAP,
@@ -164,7 +186,9 @@ void Scene_PatchDoCap_Selected( scene::Graph& graph, const char* shader ){
                }
 
                InstanceVector instances;
-               Scene_forEachVisibleSelectedPatchInstance( PatchStoreInstance( instances ) );
+               Scene_forEachVisibleSelectedPatchInstance([&](PatchInstance &patch) {
+                       instances.push_back(&patch);
+               });
                for ( InstanceVector::const_iterator i = instances.begin(); i != instances.end(); ++i )
                {
                        Patch_makeCaps( *Node_getPatch( ( *i )->path().top() ), *( *i ), eType, shader );
@@ -188,7 +212,9 @@ void Patch_deform( Patch& patch, scene::Instance& instance, const int deform ){
 void Scene_PatchDeform( scene::Graph& graph, const int deform )
 {
        InstanceVector instances;
-       Scene_forEachVisibleSelectedPatchInstance( PatchStoreInstance( instances ) );
+       Scene_forEachVisibleSelectedPatchInstance([&](PatchInstance &patch) {
+                       instances.push_back(&patch);
+       });
        for ( InstanceVector::const_iterator i = instances.begin(); i != instances.end(); ++i )
        {
                Patch_deform( *Node_getPatch( ( *i )->path().top() ), *( *i ), deform );
@@ -196,6 +222,85 @@ void Scene_PatchDeform( scene::Graph& graph, const int deform )
 
 }
 
+void Patch_thicken( Patch& patch, scene::Instance& instance, const float thickness, bool seams, const int axis ){
+
+               // Create a new patch node
+               NodeSmartReference node( g_patchCreator->createPatch() );
+               // Insert the node into worldspawn
+               Node_getTraversable( Map_FindOrInsertWorldspawn( g_map ) )->insert( node );
+
+               // Retrieve the contained patch from the node
+               Patch* targetPatch = Node_getPatch( node );
+
+               // Create the opposite patch with the given thickness = distance
+               bool no12 = true;
+               bool no34 = true;
+               targetPatch->createThickenedOpposite( patch, thickness, axis, no12, no34 );
+
+               // Now select the newly created patches
+               {
+                       scene::Path patchpath( makeReference( GlobalSceneGraph().root() ) );
+                       patchpath.push( makeReference( *Map_GetWorldspawn( g_map ) ) );
+                       patchpath.push( makeReference( node.get() ) );
+                       Instance_getSelectable( *GlobalSceneGraph().find( patchpath ) )->setSelected( true );
+               }
+
+               if( seams && thickness != 0.0f){
+                       int i = 0;
+                       if ( no12 ){
+                               i = 2;
+                       }
+                       int iend = 4;
+                       if ( no34 ){
+                               iend = 2;
+                       }
+                       // Now create the four walls
+                       for ( ; i < iend; i++ ){
+                               // Allocate new patch
+                               NodeSmartReference node = NodeSmartReference( g_patchCreator->createPatch() );
+                               // Insert each node into worldspawn
+                               Node_getTraversable( Map_FindOrInsertWorldspawn( g_map ) )->insert( node );
+
+                               // Retrieve the contained patch from the node
+                               Patch* wallPatch = Node_getPatch( node );
+
+                               // Create the wall patch by passing i as wallIndex
+                               wallPatch->createThickenedWall( patch, *targetPatch, i );
+
+                               if( ( wallPatch->localAABB().extents[0] <= 0.00005 && wallPatch->localAABB().extents[1] <= 0.00005 ) ||
+                                       ( wallPatch->localAABB().extents[1] <= 0.00005 && wallPatch->localAABB().extents[2] <= 0.00005 ) ||
+                                       ( wallPatch->localAABB().extents[0] <= 0.00005 && wallPatch->localAABB().extents[2] <= 0.00005 ) ){
+                                       //globalOutputStream() << "Thicken: Discarding degenerate patch.\n";
+                                       Node_getTraversable( Map_FindOrInsertWorldspawn( g_map ) )->erase( node );
+                               }
+                               else
+                               // Now select the newly created patches
+                               {
+                                       scene::Path patchpath( makeReference( GlobalSceneGraph().root() ) );
+                                       patchpath.push( makeReference( *Map_GetWorldspawn(g_map) ) );
+                                       patchpath.push( makeReference( node.get() ) );
+                                       Instance_getSelectable( *GlobalSceneGraph().find( patchpath ) )->setSelected( true );
+                               }
+                       }
+               }
+
+               // Invert the target patch so that it faces the opposite direction
+               targetPatch->InvertMatrix();
+}
+
+void Scene_PatchThicken( scene::Graph& graph, const int thickness, bool seams, const int axis )
+{
+       InstanceVector instances;
+       Scene_forEachVisibleSelectedPatchInstance([&](PatchInstance &patch) {
+               instances.push_back(&patch);
+       });
+       for ( InstanceVector::const_iterator i = instances.begin(); i != instances.end(); ++i )
+       {
+               Patch_thicken( *Node_getPatch( ( *i )->path().top() ), *( *i ), thickness, seams, axis );
+       }
+
+}
+
 Patch* Scene_GetUltimateSelectedVisiblePatch(){
        if ( GlobalSelectionSystem().countSelected() != 0 ) {
                scene::Node& node = GlobalSelectionSystem().ultimateSelected().path().top();
@@ -207,132 +312,62 @@ Patch* Scene_GetUltimateSelectedVisiblePatch(){
 }
 
 
-class PatchCapTexture
-{
-public:
-void operator()( Patch& patch ) const {
-       patch.ProjectTexture( Patch::m_CycleCapIndex );
-}
-};
-
 void Scene_PatchCapTexture_Selected( scene::Graph& graph ){
-       Scene_forEachVisibleSelectedPatch( PatchCapTexture() );
+       Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
+               patch.ProjectTexture(Patch::m_CycleCapIndex);
+       });
        Patch::m_CycleCapIndex = ( Patch::m_CycleCapIndex == 0 ) ? 1 : ( Patch::m_CycleCapIndex == 1 ) ? 2 : 0;
        SceneChangeNotify();
 }
 
-class PatchFlipTexture
-{
-int m_axis;
-public:
-PatchFlipTexture( int axis ) : m_axis( axis ){
-}
-void operator()( Patch& patch ) const {
-       patch.FlipTexture( m_axis );
-}
-};
-
 void Scene_PatchFlipTexture_Selected( scene::Graph& graph, int axis ){
-       Scene_forEachVisibleSelectedPatch( PatchFlipTexture( axis ) );
+       Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
+               patch.FlipTexture(axis);
+       });
 }
 
-class PatchNaturalTexture
-{
-public:
-void operator()( Patch& patch ) const {
-       patch.NaturalTexture();
-}
-};
-
 void Scene_PatchNaturalTexture_Selected( scene::Graph& graph ){
-       Scene_forEachVisibleSelectedPatch( PatchNaturalTexture() );
+       Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
+               patch.NaturalTexture();
+       });
        SceneChangeNotify();
 }
 
 
-class PatchInsertRemove
-{
-bool m_insert, m_column, m_first;
-public:
-PatchInsertRemove( bool insert, bool column, bool first ) : m_insert( insert ), m_column( column ), m_first( first ){
-}
-void operator()( Patch& patch ) const {
-       patch.InsertRemove( m_insert, m_column, m_first );
-}
-};
-
 void Scene_PatchInsertRemove_Selected( scene::Graph& graph, bool bInsert, bool bColumn, bool bFirst ){
-       Scene_forEachVisibleSelectedPatch( PatchInsertRemove( bInsert, bColumn, bFirst ) );
+       Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
+               patch.InsertRemove(bInsert, bColumn, bFirst);
+       });
 }
 
-class PatchInvertMatrix
-{
-public:
-void operator()( Patch& patch ) const {
-       patch.InvertMatrix();
-}
-};
-
 void Scene_PatchInvert_Selected( scene::Graph& graph ){
-       Scene_forEachVisibleSelectedPatch( PatchInvertMatrix() );
+       Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
+               patch.InvertMatrix();
+       });
 }
 
-class PatchRedisperse
-{
-EMatrixMajor m_major;
-public:
-PatchRedisperse( EMatrixMajor major ) : m_major( major ){
-}
-void operator()( Patch& patch ) const {
-       patch.Redisperse( m_major );
-}
-};
-
 void Scene_PatchRedisperse_Selected( scene::Graph& graph, EMatrixMajor major ){
-       Scene_forEachVisibleSelectedPatch( PatchRedisperse( major ) );
+       Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
+               patch.Redisperse(major);
+       });
 }
 
-class PatchSmooth
-{
-EMatrixMajor m_major;
-public:
-PatchSmooth( EMatrixMajor major ) : m_major( major ){
-}
-void operator()( Patch& patch ) const {
-       patch.Smooth( m_major );
-}
-};
-
 void Scene_PatchSmooth_Selected( scene::Graph& graph, EMatrixMajor major ){
-       Scene_forEachVisibleSelectedPatch( PatchSmooth( major ) );
+       Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
+               patch.Smooth(major);
+       });
 }
 
-class PatchTransposeMatrix
-{
-public:
-void operator()( Patch& patch ) const {
-       patch.TransposeMatrix();
-}
-};
-
 void Scene_PatchTranspose_Selected( scene::Graph& graph ){
-       Scene_forEachVisibleSelectedPatch( PatchTransposeMatrix() );
-}
-
-class PatchSetShader
-{
-const char* m_name;
-public:
-PatchSetShader( const char* name )
-       : m_name( name ){
-}
-void operator()( Patch& patch ) const {
-       patch.SetShader( m_name );
+       Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
+               patch.TransposeMatrix();
+       });
 }
-};
 
 void Scene_PatchSetShader_Selected( scene::Graph& graph, const char* name ){
-       Scene_forEachVisibleSelectedPatch( PatchSetShader( name ) );
+       Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
+               patch.SetShader(name);
+       });
        SceneChangeNotify();
 }
 
@@ -358,7 +393,11 @@ void operator()( PatchInstance& patch ) const {
 };
 
 void Scene_PatchSelectByShader( scene::Graph& graph, const char* name ){
-       Scene_forEachVisiblePatchInstance( PatchSelectByShader( name ) );
+       Scene_forEachVisiblePatchInstance([&](PatchInstance &patch) {
+               if (shader_equal(patch.getPatch().GetShader(), name)) {
+                       patch.setSelected(true);
+               }
+       });
 }
 
 
@@ -376,12 +415,28 @@ void operator()( Patch& patch ) const {
 }
 };
 
+namespace{
+bool DoingSearch( const char *repl ){
+       return ( repl == NULL || ( strcmp( "textures/", repl ) == 0 ) );
+}
+}
 void Scene_PatchFindReplaceShader( scene::Graph& graph, const char* find, const char* replace ){
-       Scene_forEachVisiblePatch( PatchFindReplaceShader( find, replace ) );
+       if( DoingSearch( replace ) ){
+               Scene_forEachVisiblePatchInstance( PatchSelectByShader( find ) );
+       }
+       else{
+               Scene_forEachVisiblePatch( PatchFindReplaceShader( find, replace ) );
+       }
 }
 
 void Scene_PatchFindReplaceShader_Selected( scene::Graph& graph, const char* find, const char* replace ){
-       Scene_forEachVisibleSelectedPatch( PatchFindReplaceShader( find, replace ) );
+       if( DoingSearch( replace ) ){
+               //do nothing, because alternative is replacing to notex
+               //perhaps deselect ones with not matching shaders here?
+       }
+       else{
+               Scene_forEachVisibleSelectedPatch( PatchFindReplaceShader( find, replace ) );
+       }
 }
 
 
@@ -610,6 +665,26 @@ void Patch_NaturalTexture(){
        Scene_PatchNaturalTexture_Selected( GlobalSceneGraph() );
 }
 
+void Patch_CapTexture(){
+       UndoableCommand command( "patchCapTexture" );
+
+       Scene_PatchCapTexture_Selected( GlobalSceneGraph() );
+}
+
+void Patch_ResetTexture(){
+       float fx, fy;
+       if ( DoTextureLayout( &fx, &fy ) == eIDOK ) {
+               UndoableCommand command( "patchTileTexture" );
+               Scene_PatchTileTexture_Selected( GlobalSceneGraph(), fx, fy );
+       }
+}
+
+void Patch_FitTexture(){
+       UndoableCommand command( "patchFitTexture" );
+
+       Scene_PatchTileTexture_Selected( GlobalSceneGraph(), 1, 1 );
+}
+
 void DoPatchDeformDlg();
 
 void Patch_Deform(){
@@ -618,7 +693,13 @@ void Patch_Deform(){
        DoPatchDeformDlg();
 }
 
+void DoPatchThickenDlg();
+
+void Patch_Thicken(){
+       UndoableCommand undo( "patchThicken" );
 
+       DoPatchThickenDlg();
+}
 
 
 #include "ifilter.h"
@@ -678,70 +759,71 @@ void Patch_constructPage( PreferenceGroup& group ){
        Patch_constructPreferences( page );
 }
 void Patch_registerPreferencesPage(){
-       PreferencesDialog_addDisplayPage( FreeCaller1<PreferenceGroup&, Patch_constructPage>() );
+       PreferencesDialog_addDisplayPage( makeCallbackF(Patch_constructPage) );
 }
 
 
 #include "preferencesystem.h"
 
 void PatchPreferences_construct(){
-       GlobalPreferenceSystem().registerPreference( "Subdivisions", IntImportStringCaller( g_PatchSubdivideThreshold ), IntExportStringCaller( g_PatchSubdivideThreshold ) );
+       GlobalPreferenceSystem().registerPreference( "Subdivisions", make_property_string( g_PatchSubdivideThreshold ) );
 }
 
 
 #include "generic/callback.h"
 
 void Patch_registerCommands(){
-       GlobalCommands_insert( "InvertCurveTextureX", FreeCaller<Patch_FlipTextureX>(), Accelerator( 'I', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
-       GlobalCommands_insert( "InvertCurveTextureY", FreeCaller<Patch_FlipTextureY>(), Accelerator( 'I', (GdkModifierType)GDK_SHIFT_MASK ) );
-       GlobalCommands_insert( "IncPatchColumn", FreeCaller<Patch_InsertInsertColumn>(), Accelerator( GDK_KP_Add, (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
-       GlobalCommands_insert( "IncPatchRow", FreeCaller<Patch_InsertInsertRow>(), Accelerator( GDK_KP_Add, (GdkModifierType)GDK_CONTROL_MASK ) );
-       GlobalCommands_insert( "DecPatchColumn", FreeCaller<Patch_DeleteLastColumn>(), Accelerator( GDK_KP_Subtract, (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
-       GlobalCommands_insert( "DecPatchRow", FreeCaller<Patch_DeleteLastRow>(), Accelerator( GDK_KP_Subtract, (GdkModifierType)GDK_CONTROL_MASK ) );
-       GlobalCommands_insert( "NaturalizePatch", FreeCaller<Patch_NaturalTexture>(), Accelerator( 'N', (GdkModifierType)GDK_CONTROL_MASK ) );
-       GlobalCommands_insert( "PatchCylinder", FreeCaller<Patch_Cylinder>() );
-       GlobalCommands_insert( "PatchDenseCylinder", FreeCaller<Patch_DenseCylinder>() );
-       GlobalCommands_insert( "PatchVeryDenseCylinder", FreeCaller<Patch_VeryDenseCylinder>() );
-       GlobalCommands_insert( "PatchSquareCylinder", FreeCaller<Patch_SquareCylinder>() );
-       GlobalCommands_insert( "PatchXactCylinder", FreeCaller<Patch_XactCylinder>() );
-       GlobalCommands_insert( "PatchXactSphere", FreeCaller<Patch_XactSphere>() );
-       GlobalCommands_insert( "PatchXactCone", FreeCaller<Patch_XactCone>() );
-       GlobalCommands_insert( "PatchEndCap", FreeCaller<Patch_Endcap>() );
-       GlobalCommands_insert( "PatchBevel", FreeCaller<Patch_Bevel>() );
-       GlobalCommands_insert( "PatchSquareBevel", FreeCaller<Patch_SquareBevel>() );
-       GlobalCommands_insert( "PatchSquareEndcap", FreeCaller<Patch_SquareEndcap>() );
-       GlobalCommands_insert( "PatchCone", FreeCaller<Patch_Cone>() );
-       GlobalCommands_insert( "PatchSphere", FreeCaller<Patch_Sphere>() );
-       GlobalCommands_insert( "SimplePatchMesh", FreeCaller<Patch_Plane>(), Accelerator( 'P', (GdkModifierType)GDK_SHIFT_MASK ) );
-       GlobalCommands_insert( "PatchInsertInsertColumn", FreeCaller<Patch_InsertInsertColumn>() );
-       GlobalCommands_insert( "PatchInsertAddColumn", FreeCaller<Patch_InsertAddColumn>() );
-       GlobalCommands_insert( "PatchInsertInsertRow", FreeCaller<Patch_InsertInsertRow>() );
-       GlobalCommands_insert( "PatchInsertAddRow", FreeCaller<Patch_InsertAddRow>() );
-       GlobalCommands_insert( "PatchDeleteFirstColumn", FreeCaller<Patch_DeleteFirstColumn>() );
-       GlobalCommands_insert( "PatchDeleteLastColumn", FreeCaller<Patch_DeleteLastColumn>() );
-       GlobalCommands_insert( "PatchDeleteFirstRow", FreeCaller<Patch_DeleteFirstRow>() );
-       GlobalCommands_insert( "PatchDeleteLastRow", FreeCaller<Patch_DeleteLastRow>() );
-       GlobalCommands_insert( "InvertCurve", FreeCaller<Patch_Invert>(), Accelerator( 'I', (GdkModifierType)GDK_CONTROL_MASK ) );
-       GlobalCommands_insert( "RedisperseRows", FreeCaller<Patch_RedisperseRows>(), Accelerator( 'E', (GdkModifierType)GDK_CONTROL_MASK ) );
-       GlobalCommands_insert( "RedisperseCols", FreeCaller<Patch_RedisperseCols>(), Accelerator( 'E', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
-       GlobalCommands_insert( "SmoothRows", FreeCaller<Patch_SmoothRows>(), Accelerator( 'W', (GdkModifierType)GDK_CONTROL_MASK ) );
-       GlobalCommands_insert( "SmoothCols", FreeCaller<Patch_SmoothCols>(), Accelerator( 'W', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
-       GlobalCommands_insert( "MatrixTranspose", FreeCaller<Patch_Transpose>(), Accelerator( 'M', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
-       GlobalCommands_insert( "CapCurrentCurve", FreeCaller<Patch_Cap>(), Accelerator( 'C', (GdkModifierType)GDK_SHIFT_MASK ) );
-       GlobalCommands_insert( "CycleCapTexturePatch", FreeCaller<Patch_CycleProjection>(), Accelerator( 'N', (GdkModifierType)GDK_SHIFT_MASK ) );
-       GlobalCommands_insert( "MakeOverlayPatch", FreeCaller<Patch_OverlayOn>(), Accelerator( 'Y' ) );
-       GlobalCommands_insert( "ClearPatchOverlays", FreeCaller<Patch_OverlayOff>(), Accelerator( 'L', (GdkModifierType)GDK_CONTROL_MASK ) );
-       GlobalCommands_insert( "PatchDeform", FreeCaller<Patch_Deform>() );
-}
-
-void Patch_constructToolbar( GtkToolbar* toolbar ){
-       toolbar_append_button( toolbar, "Put caps on the current patch (SHIFT + C)", "curve_cap.bmp", "CapCurrentCurve" );
-}
-
-void Patch_constructMenu( GtkMenu* menu ){
+       GlobalCommands_insert( "InvertCurveTextureX", makeCallbackF(Patch_FlipTextureX), Accelerator( 'I', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
+       GlobalCommands_insert( "InvertCurveTextureY", makeCallbackF(Patch_FlipTextureY), Accelerator( 'I', (GdkModifierType)GDK_SHIFT_MASK ) );
+       GlobalCommands_insert( "IncPatchColumn", makeCallbackF(Patch_InsertInsertColumn), Accelerator( GDK_KP_Add, (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
+       GlobalCommands_insert( "IncPatchRow", makeCallbackF(Patch_InsertInsertRow), Accelerator( GDK_KP_Add, (GdkModifierType)GDK_CONTROL_MASK ) );
+       GlobalCommands_insert( "DecPatchColumn", makeCallbackF(Patch_DeleteLastColumn), Accelerator( GDK_KP_Subtract, (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
+       GlobalCommands_insert( "DecPatchRow", makeCallbackF(Patch_DeleteLastRow), Accelerator( GDK_KP_Subtract, (GdkModifierType)GDK_CONTROL_MASK ) );
+       GlobalCommands_insert( "NaturalizePatch", makeCallbackF(Patch_NaturalTexture), Accelerator( 'N', (GdkModifierType)GDK_CONTROL_MASK ) );
+       GlobalCommands_insert( "PatchCylinder", makeCallbackF(Patch_Cylinder) );
+       GlobalCommands_insert( "PatchDenseCylinder", makeCallbackF(Patch_DenseCylinder) );
+       GlobalCommands_insert( "PatchVeryDenseCylinder", makeCallbackF(Patch_VeryDenseCylinder) );
+       GlobalCommands_insert( "PatchSquareCylinder", makeCallbackF(Patch_SquareCylinder) );
+       GlobalCommands_insert( "PatchXactCylinder", makeCallbackF(Patch_XactCylinder) );
+       GlobalCommands_insert( "PatchXactSphere", makeCallbackF(Patch_XactSphere) );
+       GlobalCommands_insert( "PatchXactCone", makeCallbackF(Patch_XactCone) );
+       GlobalCommands_insert( "PatchEndCap", makeCallbackF(Patch_Endcap) );
+       GlobalCommands_insert( "PatchBevel", makeCallbackF(Patch_Bevel) );
+       GlobalCommands_insert( "PatchSquareBevel", makeCallbackF(Patch_SquareBevel) );
+       GlobalCommands_insert( "PatchSquareEndcap", makeCallbackF(Patch_SquareEndcap) );
+       GlobalCommands_insert( "PatchCone", makeCallbackF(Patch_Cone) );
+       GlobalCommands_insert( "PatchSphere", makeCallbackF(Patch_Sphere) );
+       GlobalCommands_insert( "SimplePatchMesh", makeCallbackF(Patch_Plane), Accelerator( 'P', (GdkModifierType)GDK_SHIFT_MASK ) );
+       GlobalCommands_insert( "PatchInsertInsertColumn", makeCallbackF(Patch_InsertInsertColumn), Accelerator( GDK_KEY_KP_Add, (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
+       GlobalCommands_insert( "PatchInsertAddColumn", makeCallbackF(Patch_InsertAddColumn) );
+       GlobalCommands_insert( "PatchInsertInsertRow", makeCallbackF(Patch_InsertInsertRow), Accelerator( GDK_KEY_KP_Add, (GdkModifierType)GDK_CONTROL_MASK ) );
+       GlobalCommands_insert( "PatchInsertAddRow", makeCallbackF(Patch_InsertAddRow) );
+       GlobalCommands_insert( "PatchDeleteFirstColumn", makeCallbackF(Patch_DeleteFirstColumn) );
+       GlobalCommands_insert( "PatchDeleteLastColumn", makeCallbackF(Patch_DeleteLastColumn), Accelerator( GDK_KEY_KP_Subtract, (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
+       GlobalCommands_insert( "PatchDeleteFirstRow", makeCallbackF(Patch_DeleteFirstRow), Accelerator( GDK_KEY_KP_Subtract, (GdkModifierType)GDK_CONTROL_MASK ) );
+       GlobalCommands_insert( "PatchDeleteLastRow", makeCallbackF(Patch_DeleteLastRow) );
+       GlobalCommands_insert( "InvertCurve", makeCallbackF(Patch_Invert), Accelerator( 'I', (GdkModifierType)GDK_CONTROL_MASK ) );
+       GlobalCommands_insert( "RedisperseRows", makeCallbackF(Patch_RedisperseRows), Accelerator( 'E', (GdkModifierType)GDK_CONTROL_MASK ) );
+       GlobalCommands_insert( "RedisperseCols", makeCallbackF(Patch_RedisperseCols), Accelerator( 'E', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
+       GlobalCommands_insert( "SmoothRows", makeCallbackF(Patch_SmoothRows), Accelerator( 'W', (GdkModifierType)GDK_CONTROL_MASK ) );
+       GlobalCommands_insert( "SmoothCols", makeCallbackF(Patch_SmoothCols), Accelerator( 'W', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
+       GlobalCommands_insert( "MatrixTranspose", makeCallbackF(Patch_Transpose), Accelerator( 'M', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
+       GlobalCommands_insert( "CapCurrentCurve", makeCallbackF(Patch_Cap), Accelerator( 'C', (GdkModifierType)GDK_SHIFT_MASK ) );
+       GlobalCommands_insert( "CycleCapTexturePatch", makeCallbackF(Patch_CycleProjection), Accelerator( 'N', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
+       GlobalCommands_insert( "MakeOverlayPatch", makeCallbackF(Patch_OverlayOn), Accelerator( 'Y' ) );
+       GlobalCommands_insert( "ClearPatchOverlays", makeCallbackF(Patch_OverlayOff), Accelerator( 'L', (GdkModifierType)GDK_CONTROL_MASK ) );
+       GlobalCommands_insert( "PatchDeform", makeCallbackF(Patch_Deform) );
+       GlobalCommands_insert( "PatchThicken", makeCallbackF(Patch_Thicken) );
+}
+
+void Patch_constructToolbar( ui::Toolbar toolbar ){
+       toolbar_append_button( toolbar, "Put caps on the current patch (SHIFT + C)", "cap_curve.png", "CapCurrentCurve" );
+}
+
+void Patch_constructMenu( ui::Menu menu ){
        create_menu_item_with_mnemonic( menu, "Cylinder", "PatchCylinder" );
        {
-               GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "More Cylinders" );
+               auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "More Cylinders" );
                if ( g_Layout_enableDetachableMenus.m_value ) {
                        menu_tearoff( menu_in_menu );
                }
@@ -754,7 +836,7 @@ void Patch_constructMenu( GtkMenu* menu ){
        create_menu_item_with_mnemonic( menu, "End cap", "PatchEndCap" );
        create_menu_item_with_mnemonic( menu, "Bevel", "PatchBevel" );
        {
-//             GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "More End caps, Bevels" );
+//             auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "More End caps, Bevels" );
 //             if ( g_Layout_enableDetachableMenus.m_value ) {
 //                     menu_tearoff( menu_in_menu );
 //             }
@@ -771,18 +853,18 @@ void Patch_constructMenu( GtkMenu* menu ){
        create_menu_item_with_mnemonic( menu, "Simple Patch Mesh...", "SimplePatchMesh" );
        menu_separator( menu );
        {
-               GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "Insert" );
+               auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Insert" );
                if ( g_Layout_enableDetachableMenus.m_value ) {
                        menu_tearoff( menu_in_menu );
                }
-               create_menu_item_with_mnemonic( menu_in_menu, "Insert (2) Columns", "PatchInsertInsertColumn" );
                create_menu_item_with_mnemonic( menu_in_menu, "Add (2) Columns", "PatchInsertAddColumn" );
+               create_menu_item_with_mnemonic( menu_in_menu, "Insert (2) Columns", "PatchInsertInsertColumn" );
                menu_separator( menu_in_menu );
-               create_menu_item_with_mnemonic( menu_in_menu, "Insert (2) Rows", "PatchInsertInsertRow" );
                create_menu_item_with_mnemonic( menu_in_menu, "Add (2) Rows", "PatchInsertAddRow" );
+               create_menu_item_with_mnemonic( menu_in_menu, "Insert (2) Rows", "PatchInsertInsertRow" );
        }
        {
-               GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "Delete" );
+               auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Delete" );
                if ( g_Layout_enableDetachableMenus.m_value ) {
                        menu_tearoff( menu_in_menu );
                }
@@ -794,32 +876,33 @@ void Patch_constructMenu( GtkMenu* menu ){
        }
        menu_separator( menu );
        {
-               GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "Matrix" );
+               auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Matrix" );
                if ( g_Layout_enableDetachableMenus.m_value ) {
                        menu_tearoff( menu_in_menu );
                }
                create_menu_item_with_mnemonic( menu_in_menu, "Invert", "InvertCurve" );
-               create_menu_item_with_mnemonic( menu_in_menu, "Transpose", "MatrixTranspose" );
-//             GtkMenu* menu_3 = create_sub_menu_with_mnemonic( menu_in_menu, "Re-disperse" );
+//             auto menu_3 = create_sub_menu_with_mnemonic( menu_in_menu, "Re-disperse" );
 //             if ( g_Layout_enableDetachableMenus.m_value ) {
 //                     menu_tearoff( menu_3 );
 //             }
                menu_separator( menu_in_menu );
-               create_menu_item_with_mnemonic( menu_in_menu, "Re-disperse Rows", "RedisperseRows" );
-               create_menu_item_with_mnemonic( menu_in_menu, "Re-disperse Columns", "RedisperseCols" );
-//             GtkMenu* menu_4 = create_sub_menu_with_mnemonic( menu_in_menu, "Smooth" );
+               create_menu_item_with_mnemonic( menu, "Rows", "RedisperseRows" );
+               create_menu_item_with_mnemonic( menu, "Columns", "RedisperseCols" );
+//             auto menu_4 = create_sub_menu_with_mnemonic( menu_in_menu, "Smooth" );
 //             if ( g_Layout_enableDetachableMenus.m_value ) {
 //                     menu_tearoff( menu_4 );
 //             }
-               menu_separator( menu_in_menu );
-               create_menu_item_with_mnemonic( menu_in_menu, "Smooth Rows", "SmoothRows" );
-               create_menu_item_with_mnemonic( menu_in_menu, "Smooth Columns", "SmoothCols" );
+               create_menu_item_with_mnemonic( menu, "Rows", "SmoothRows" );
+               create_menu_item_with_mnemonic( menu, "Columns", "SmoothCols" );
+               create_menu_item_with_mnemonic( menu_in_menu, "Transpose", "MatrixTranspose" );
+
        }
        menu_separator( menu );
        create_menu_item_with_mnemonic( menu, "Cap Selection", "CapCurrentCurve" );
+       create_menu_item_with_mnemonic( menu, "Cycle Cap Texture", "CycleCapTexturePatch" );
        menu_separator( menu );
        {
-               GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "Texture" );
+               auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Texture" );
                if ( g_Layout_enableDetachableMenus.m_value ) {
                        menu_tearoff( menu_in_menu );
                }
@@ -831,7 +914,7 @@ void Patch_constructMenu( GtkMenu* menu ){
        }
        menu_separator( menu );
        {
-               GtkMenu* menu_in_menu = create_sub_menu_with_mnemonic( menu, "Overlay" );
+               auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Overlay" );
                if ( g_Layout_enableDetachableMenus.m_value ) {
                        menu_tearoff( menu_in_menu );
                }
@@ -840,54 +923,44 @@ void Patch_constructMenu( GtkMenu* menu ){
        }
        menu_separator( menu );
        create_menu_item_with_mnemonic( menu, "Deform...", "PatchDeform" );
+       create_menu_item_with_mnemonic( menu, "Thicken...", "PatchThicken" );
 }
 
 
-#include <gtk/gtkbox.h>
-#include <gtk/gtktable.h>
-#include <gtk/gtktogglebutton.h>
-#include <gtk/gtkradiobutton.h>
-#include <gtk/gtkcombobox.h>
-#include <gtk/gtklabel.h>
 #include "gtkutil/dialog.h"
 #include "gtkutil/widget.h"
 
 void DoNewPatchDlg( EPatchPrefab prefab, int minrows, int mincols, int defrows, int defcols, int maxrows, int maxcols ){
        ModalDialog dialog;
-       GtkComboBox* width;
-       GtkComboBox* height;
-
-       GtkWindow* window = create_dialog_window( MainFrame_getWindow(), "Patch density", G_CALLBACK( dialog_delete_callback ), &dialog );
 
-       GtkAccelGroup* accel = gtk_accel_group_new();
-       gtk_window_add_accel_group( window, accel );
+       ui::Window window = MainFrame_getWindow().create_dialog_window("Patch density", G_CALLBACK(dialog_delete_callback ), &dialog );
 
+       auto accel = ui::AccelGroup(ui::New);
+       window.add_accel_group( accel );
+       auto width = ui::ComboBoxText(ui::New);
+       auto height = ui::ComboBoxText(ui::New);
        {
-               GtkHBox* hbox = create_dialog_hbox( 4, 4 );
-               gtk_container_add( GTK_CONTAINER( window ), GTK_WIDGET( hbox ) );
+               auto hbox = create_dialog_hbox( 4, 4 );
+               window.add(hbox);
                {
-                       GtkTable* table = create_dialog_table( 2, 2, 4, 4 );
-                       gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
+                       auto table = create_dialog_table( 2, 2, 4, 4 );
+                       hbox.pack_start( table, TRUE, TRUE, 0 );
                        {
-                               GtkLabel* label = GTK_LABEL( gtk_label_new( "Width:" ) );
-                               gtk_widget_show( GTK_WIDGET( label ) );
-                               gtk_table_attach( table, GTK_WIDGET( label ), 0, 1, 0, 1,
-                                                                 (GtkAttachOptions) ( GTK_FILL ),
-                                                                 (GtkAttachOptions) ( 0 ), 0, 0 );
+                               auto label = ui::Label( "Width:" );
+                               label.show();
+                table.attach(label, {0, 1, 0, 1}, {GTK_FILL, 0});
                                gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
                        }
                        {
-                               GtkLabel* label = GTK_LABEL( gtk_label_new( "Height:" ) );
-                               gtk_widget_show( GTK_WIDGET( label ) );
-                               gtk_table_attach( table, GTK_WIDGET( label ), 0, 1, 1, 2,
-                                                                 (GtkAttachOptions) ( GTK_FILL ),
-                                                                 (GtkAttachOptions) ( 0 ), 0, 0 );
+                               auto label = ui::Label( "Height:" );
+                               label.show();
+                table.attach(label, {0, 1, 1, 2}, {GTK_FILL, 0});
                                gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
                        }
 
                        {
-                               GtkComboBox* combo = GTK_COMBO_BOX( gtk_combo_box_new_text() );
-#define D_ITEM( x ) if ( x >= mincols && ( !maxcols || x <= maxcols ) ) gtk_combo_box_append_text( combo, # x )
+                               auto combo = width;
+#define D_ITEM( x ) if ( x >= mincols && ( !maxcols || x <= maxcols ) ) gtk_combo_box_text_append_text( combo, #x )
                                D_ITEM( 3 );
                                D_ITEM( 5 );
                                D_ITEM( 7 );
@@ -904,16 +977,12 @@ void DoNewPatchDlg( EPatchPrefab prefab, int minrows, int mincols, int defrows,
                                D_ITEM( 29 );
                                D_ITEM( 31 ); // MAX_PATCH_SIZE is 32, so we should be able to do 31...
 #undef D_ITEM
-                               gtk_widget_show( GTK_WIDGET( combo ) );
-                               gtk_table_attach( table, GTK_WIDGET( combo ), 1, 2, 0, 1,
-                                                                 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
-                                                                 (GtkAttachOptions) ( 0 ), 0, 0 );
-
-                               width = combo;
+                               combo.show();
+                table.attach(combo, {1, 2, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
                        }
                        {
-                               GtkComboBox* combo = GTK_COMBO_BOX( gtk_combo_box_new_text() );
-#define D_ITEM( x ) if ( x >= minrows && ( !maxrows || x <= maxrows ) ) gtk_combo_box_append_text( combo, # x )
+                               auto combo = height;
+#define D_ITEM( x ) if ( x >= minrows && ( !maxrows || x <= maxrows ) ) gtk_combo_box_text_append_text( combo, #x )
                                D_ITEM( 3 );
                                D_ITEM( 5 );
                                D_ITEM( 7 );
@@ -930,29 +999,25 @@ void DoNewPatchDlg( EPatchPrefab prefab, int minrows, int mincols, int defrows,
                                D_ITEM( 29 );
                                D_ITEM( 31 ); // MAX_PATCH_SIZE is 32, so we should be able to do 31...
 #undef D_ITEM
-                               gtk_widget_show( GTK_WIDGET( combo ) );
-                               gtk_table_attach( table, GTK_WIDGET( combo ), 1, 2, 1, 2,
-                                                                 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
-                                                                 (GtkAttachOptions) ( 0 ), 0, 0 );
-
-                               height = combo;
+                               combo.show();
+                table.attach(combo, {1, 2, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
                        }
                }
 
                {
-                       GtkVBox* vbox = create_dialog_vbox( 4 );
-                       gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( vbox ), TRUE, TRUE, 0 );
+                       auto vbox = create_dialog_vbox( 4 );
+                       hbox.pack_start( vbox, TRUE, TRUE, 0 );
                        {
-                               GtkButton* button = create_dialog_button( "OK", G_CALLBACK( dialog_button_ok ), &dialog );
-                               gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
-                               widget_make_default( GTK_WIDGET( button ) );
-                               gtk_widget_grab_focus( GTK_WIDGET( button ) );
-                               gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel, GDK_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
+                               auto button = create_dialog_button( "OK", G_CALLBACK( dialog_button_ok ), &dialog );
+                               vbox.pack_start( button, FALSE, FALSE, 0 );
+                               widget_make_default( button );
+                               gtk_widget_grab_focus( button  );
+                               gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
                        }
                        {
-                               GtkButton* button = create_dialog_button( "Cancel", G_CALLBACK( dialog_button_cancel ), &dialog );
-                               gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
-                               gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel, GDK_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
+                               auto button = create_dialog_button( "Cancel", G_CALLBACK( dialog_button_cancel ), &dialog );
+                               vbox.pack_start( button, FALSE, FALSE, 0 );
+                               gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
                        }
                }
        }
@@ -968,7 +1033,7 @@ void DoNewPatchDlg( EPatchPrefab prefab, int minrows, int mincols, int defrows,
                Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), prefab, GlobalXYWnd_getCurrentViewType(), w, h );
        }
 
-       gtk_widget_destroy( GTK_WIDGET( window ) );
+       window.destroy();
 }
 
 
@@ -976,16 +1041,16 @@ void DoPatchDeformDlg(){
        ModalDialog dialog;
        GtkWidget* deformW;
 
-       GtkWindow* window = create_dialog_window( MainFrame_getWindow(), "Patch deform", G_CALLBACK( dialog_delete_callback ), &dialog );
+       ui::Window window = create_dialog_window( MainFrame_getWindow(), "Patch deform", G_CALLBACK( dialog_delete_callback ), &dialog );
 
        GtkAccelGroup* accel = gtk_accel_group_new();
        gtk_window_add_accel_group( window, accel );
 
        {
-               GtkHBox* hbox = create_dialog_hbox( 4, 4 );
+               auto hbox = create_dialog_hbox( 4, 4 );
                gtk_container_add( GTK_CONTAINER( window ), GTK_WIDGET( hbox ) );
                {
-                       GtkTable* table = create_dialog_table( 2, 2, 4, 4 );
+                       auto table = create_dialog_table( 2, 2, 4, 4 );
                        gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
                        {
                                GtkLabel* label = GTK_LABEL( gtk_label_new( "Max deform:" ) );
@@ -1007,19 +1072,19 @@ void DoPatchDeformDlg(){
                        }
                }
                {
-                       GtkVBox* vbox = create_dialog_vbox( 4 );
-                       gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( vbox ), TRUE, TRUE, 0 );
+                       auto vbox = create_dialog_vbox( 4 );
+                       hbox.pack_start( vbox, FALSE, FALSE, 0 );
                        {
-                               GtkButton* button = create_dialog_button( "OK", G_CALLBACK( dialog_button_ok ), &dialog );
-                               gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
-                               widget_make_default( GTK_WIDGET( button ) );
-                               gtk_widget_grab_focus( GTK_WIDGET( button ) );
-                               gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel, GDK_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
+                               auto button = create_dialog_button( "OK", G_CALLBACK( dialog_button_ok ), &dialog );
+                               vbox.pack_start( button, FALSE, FALSE, 0 );
+                               widget_make_default( button );
+                               gtk_widget_grab_focus( button  );
+                               gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
                        }
                        {
-                               GtkButton* button = create_dialog_button( "Cancel", G_CALLBACK( dialog_button_cancel ), &dialog );
-                               gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
-                               gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel, GDK_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
+                               auto button = create_dialog_button( "Cancel", G_CALLBACK( dialog_button_cancel ), &dialog );
+                               vbox.pack_start( button, FALSE, FALSE, 0 );
+                               gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
                        }
                }
        }
@@ -1038,117 +1103,97 @@ EMessageBoxReturn DoCapDlg( ECapDialog* type ){
        ModalDialog dialog;
        ModalDialogButton ok_button( dialog, eIDOK );
        ModalDialogButton cancel_button( dialog, eIDCANCEL );
-       GtkWidget* bevel;
-       GtkWidget* ibevel;
-       GtkWidget* endcap;
-       GtkWidget* iendcap;
-       GtkWidget* cylinder;
+       ui::Widget bevel{ui::null};
+       ui::Widget ibevel{ui::null};
+       ui::Widget endcap{ui::null};
+       ui::Widget iendcap{ui::null};
+       ui::Widget cylinder{ui::null};
 
-       GtkWindow* window = create_modal_dialog_window( MainFrame_getWindow(), "Cap", dialog );
+       ui::Window window = MainFrame_getWindow().create_modal_dialog_window( "Cap", dialog );
 
-       GtkAccelGroup *accel_group = gtk_accel_group_new();
-       gtk_window_add_accel_group( window, accel_group );
+       auto accel_group = ui::AccelGroup(ui::New);
+       window.add_accel_group( accel_group );
 
        {
-               GtkHBox* hbox = create_dialog_hbox( 4, 4 );
-               gtk_container_add( GTK_CONTAINER( window ), GTK_WIDGET( hbox ) );
+               auto hbox = create_dialog_hbox( 4, 4 );
+               window.add(hbox);
 
                {
                        // Gef: Added a vbox to contain the toggle buttons
-                       GtkVBox* radio_vbox = create_dialog_vbox( 4 );
-                       gtk_container_add( GTK_CONTAINER( hbox ), GTK_WIDGET( radio_vbox ) );
+                       auto radio_vbox = create_dialog_vbox( 4 );
+                       hbox.add(radio_vbox);
 
                        {
-                               GtkTable* table = GTK_TABLE( gtk_table_new( 5, 2, FALSE ) );
-                               gtk_widget_show( GTK_WIDGET( table ) );
-                               gtk_box_pack_start( GTK_BOX( radio_vbox ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
+                               auto table = ui::Table( 5, 2, FALSE );
+                               table.show();
+                               radio_vbox.pack_start( table, TRUE, TRUE, 0 );
                                gtk_table_set_row_spacings( table, 5 );
                                gtk_table_set_col_spacings( table, 5 );
 
                                {
-                                       GtkImage* image = new_local_image( "cap_bevel.bmp" );
-                                       gtk_widget_show( GTK_WIDGET( image ) );
-                                       gtk_table_attach( table, GTK_WIDGET( image ), 0, 1, 0, 1,
-                                                                         (GtkAttachOptions) ( GTK_FILL ),
-                                                                         (GtkAttachOptions) ( 0 ), 0, 0 );
+                                       auto image = new_local_image( "cap_bevel.png" );
+                                       image.show();
+                    table.attach(image, {0, 1, 0, 1}, {GTK_FILL, 0});
                                }
                                {
-                                       GtkImage* image = new_local_image( "cap_endcap.bmp" );
-                                       gtk_widget_show( GTK_WIDGET( image ) );
-                                       gtk_table_attach( table, GTK_WIDGET( image ), 0, 1, 1, 2,
-                                                                         (GtkAttachOptions) ( GTK_FILL ),
-                                                                         (GtkAttachOptions) ( 0 ), 0, 0 );
+                                       auto image = new_local_image( "cap_endcap.png" );
+                                       image.show();
+                    table.attach(image, {0, 1, 1, 2}, {GTK_FILL, 0});
                                }
                                {
-                                       GtkImage* image = new_local_image( "cap_ibevel.bmp" );
-                                       gtk_widget_show( GTK_WIDGET( image ) );
-                                       gtk_table_attach( table, GTK_WIDGET( image ), 0, 1, 2, 3,
-                                                                         (GtkAttachOptions) ( GTK_FILL ),
-                                                                         (GtkAttachOptions) ( 0 ), 0, 0 );
+                                       auto image = new_local_image( "cap_ibevel.png" );
+                                       image.show();
+                    table.attach(image, {0, 1, 2, 3}, {GTK_FILL, 0});
                                }
                                {
-                                       GtkImage* image = new_local_image( "cap_iendcap.bmp" );
-                                       gtk_widget_show( GTK_WIDGET( image ) );
-                                       gtk_table_attach( table, GTK_WIDGET( image ), 0, 1, 3, 4,
-                                                                         (GtkAttachOptions) ( GTK_FILL ),
-                                                                         (GtkAttachOptions) ( 0 ), 0, 0 );
+                                       auto image = new_local_image( "cap_iendcap.png" );
+                                       image.show();
+                    table.attach(image, {0, 1, 3, 4}, {GTK_FILL, 0});
                                }
                                {
-                                       GtkImage* image = new_local_image( "cap_cylinder.bmp" );
-                                       gtk_widget_show( GTK_WIDGET( image ) );
-                                       gtk_table_attach( table, GTK_WIDGET( image ), 0, 1, 4, 5,
-                                                                         (GtkAttachOptions) ( GTK_FILL ),
-                                                                         (GtkAttachOptions) ( 0 ), 0, 0 );
+                                       auto image = new_local_image( "cap_cylinder.png" );
+                                       image.show();
+                    table.attach(image, {0, 1, 4, 5}, {GTK_FILL, 0});
                                }
 
                                GSList* group = 0;
                                {
-                                       GtkWidget* button = gtk_radio_button_new_with_label( group, "Bevel" );
-                                       gtk_widget_show( button );
-                                       gtk_table_attach( table, button, 1, 2, 0, 1,
-                                                                         (GtkAttachOptions) ( GTK_FILL | GTK_EXPAND ),
-                                                                         (GtkAttachOptions) ( 0 ), 0, 0 );
-                                       group = gtk_radio_button_group( GTK_RADIO_BUTTON( button ) );
+                                       ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Bevel" ));
+                                       button.show();
+                    table.attach(button, {1, 2, 0, 1}, {GTK_FILL | GTK_EXPAND, 0});
+                                       group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
 
                                        bevel = button;
                                }
                                {
-                                       GtkWidget* button = gtk_radio_button_new_with_label( group, "Endcap" );
-                                       gtk_widget_show( button );
-                                       gtk_table_attach( table, button, 1, 2, 1, 2,
-                                                                         (GtkAttachOptions) ( GTK_FILL | GTK_EXPAND ),
-                                                                         (GtkAttachOptions) ( 0 ), 0, 0 );
-                                       group = gtk_radio_button_group( GTK_RADIO_BUTTON( button ) );
+                                       ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Endcap" ));
+                                       button.show();
+                    table.attach(button, {1, 2, 1, 2}, {GTK_FILL | GTK_EXPAND, 0});
+                                       group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
 
                                        endcap = button;
                                }
                                {
-                                       GtkWidget* button = gtk_radio_button_new_with_label( group, "Inverted Bevel" );
-                                       gtk_widget_show( button );
-                                       gtk_table_attach( table, button, 1, 2, 2, 3,
-                                                                         (GtkAttachOptions) ( GTK_FILL | GTK_EXPAND ),
-                                                                         (GtkAttachOptions) ( 0 ), 0, 0 );
-                                       group = gtk_radio_button_group( GTK_RADIO_BUTTON( button ) );
+                                       ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Inverted Bevel" ));
+                                       button.show();
+                    table.attach(button, {1, 2, 2, 3}, {GTK_FILL | GTK_EXPAND, 0});
+                                       group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
 
                                        ibevel = button;
                                }
                                {
-                                       GtkWidget* button = gtk_radio_button_new_with_label( group, "Inverted Endcap" );
-                                       gtk_widget_show( button );
-                                       gtk_table_attach( table, button, 1, 2, 3, 4,
-                                                                         (GtkAttachOptions) ( GTK_FILL | GTK_EXPAND ),
-                                                                         (GtkAttachOptions) ( 0 ), 0, 0 );
-                                       group = gtk_radio_button_group( GTK_RADIO_BUTTON( button ) );
+                                       ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Inverted Endcap" ));
+                                       button.show();
+                    table.attach(button, {1, 2, 3, 4}, {GTK_FILL | GTK_EXPAND, 0});
+                                       group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
 
                                        iendcap = button;
                                }
                                {
-                                       GtkWidget* button = gtk_radio_button_new_with_label( group, "Cylinder" );
-                                       gtk_widget_show( button );
-                                       gtk_table_attach( table, button, 1, 2, 4, 5,
-                                                                         (GtkAttachOptions) ( GTK_FILL | GTK_EXPAND ),
-                                                                         (GtkAttachOptions) ( 0 ), 0, 0 );
-                                       group = gtk_radio_button_group( GTK_RADIO_BUTTON( button ) );
+                                       ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Cylinder" ));
+                                       button.show();
+                    table.attach(button, {1, 2, 4, 5}, {GTK_FILL | GTK_EXPAND, 0});
+                                       group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
 
                                        cylinder = button;
                                }
@@ -1156,18 +1201,18 @@ EMessageBoxReturn DoCapDlg( ECapDialog* type ){
                }
 
                {
-                       GtkVBox* vbox = create_dialog_vbox( 4 );
-                       gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( vbox ), FALSE, FALSE, 0 );
+                       auto vbox = create_dialog_vbox( 4 );
+                       hbox.pack_start( vbox, FALSE, FALSE, 0 );
                        {
-                               GtkButton* button = create_modal_dialog_button( "OK", ok_button );
-                               gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
-                               widget_make_default( GTK_WIDGET( button ) );
-                               gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel_group, GDK_Return, (GdkModifierType)0, GTK_ACCEL_VISIBLE );
+                               auto button = create_modal_dialog_button( "OK", ok_button );
+                               vbox.pack_start( button, FALSE, FALSE, 0 );
+                               widget_make_default( button );
+                               gtk_widget_add_accelerator( button , "clicked", accel_group, GDK_KEY_Return, (GdkModifierType)0, GTK_ACCEL_VISIBLE );
                        }
                        {
-                               GtkButton* button = create_modal_dialog_button( "Cancel", cancel_button );
-                               gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
-                               gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel_group, GDK_Escape, (GdkModifierType)0, GTK_ACCEL_VISIBLE );
+                               auto button = create_modal_dialog_button( "Cancel", cancel_button );
+                               vbox.pack_start( button, FALSE, FALSE, 0 );
+                               gtk_widget_add_accelerator( button , "clicked", accel_group, GDK_KEY_Escape, (GdkModifierType)0, GTK_ACCEL_VISIBLE );
                        }
                }
        }
@@ -1194,7 +1239,132 @@ EMessageBoxReturn DoCapDlg( ECapDialog* type ){
                }
        }
 
-       gtk_widget_destroy( GTK_WIDGET( window ) );
+       window.destroy();
 
        return ret;
 }
+
+
+void DoPatchThickenDlg(){
+       ModalDialog dialog;
+       GtkWidget* thicknessW;
+       GtkWidget* seamsW;
+       GtkWidget* radX;
+       GtkWidget* radY;
+       GtkWidget* radZ;
+       GtkWidget* radNormals;
+
+       ui::Window window = create_dialog_window( MainFrame_getWindow(), "Patch thicken", G_CALLBACK( dialog_delete_callback ), &dialog );
+
+       GtkAccelGroup* accel = gtk_accel_group_new();
+       gtk_window_add_accel_group( window, accel );
+
+       {
+               auto hbox = create_dialog_hbox( 4, 4 );
+               gtk_container_add( GTK_CONTAINER( window ), GTK_WIDGET( hbox ) );
+               {
+                       auto table = create_dialog_table( 2, 4, 4, 4 );
+                       gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
+                       {
+                               GtkLabel* label = GTK_LABEL( gtk_label_new( "Thickness:" ) );
+                               gtk_widget_show( GTK_WIDGET( label ) );
+                               gtk_table_attach( table, GTK_WIDGET( label ), 0, 1, 0, 1,
+                                                                 (GtkAttachOptions) ( GTK_FILL ),
+                                                                 (GtkAttachOptions) ( 0 ), 0, 0 );
+                               gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
+                       }
+                       {
+                               GtkWidget* entry = gtk_entry_new();
+                               gtk_entry_set_text( GTK_ENTRY( entry ), "16" );
+                               gtk_widget_set_size_request( entry, 40, -1 );
+                               gtk_widget_show( entry );
+                               gtk_table_attach( table, entry, 1, 2, 0, 1,
+                                                                 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
+                                                                 (GtkAttachOptions) ( 0 ), 0, 0 );
+
+                               thicknessW = entry;
+                       }
+                       {
+                               // Create the "create seams" label
+                               GtkWidget* _seamsCheckBox = gtk_check_button_new_with_label( "Side walls" );
+                               gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( _seamsCheckBox ), TRUE );
+                               gtk_widget_show( _seamsCheckBox );
+                               gtk_table_attach( table, _seamsCheckBox, 3, 4, 0, 1,
+                                                                 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
+                                                                 (GtkAttachOptions) ( 0 ), 0, 0 );
+                               seamsW = _seamsCheckBox;
+
+                       }
+                       {
+                               // Create the radio button group for choosing the extrude axis
+                               GtkWidget* _radNormals = gtk_radio_button_new_with_label( NULL, "Normal" );
+                               GtkWidget* _radX = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON(_radNormals), "X" );
+                               GtkWidget* _radY = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON(_radNormals), "Y" );
+                               GtkWidget* _radZ = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON(_radNormals), "Z" );
+                               gtk_widget_show( _radNormals );
+                               gtk_widget_show( _radX );
+                               gtk_widget_show( _radY );
+                               gtk_widget_show( _radZ );
+
+
+                               // Pack the buttons into the table
+                               gtk_table_attach( table, _radNormals, 0, 1, 1, 2,
+                                                                 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
+                                                                 (GtkAttachOptions) ( 0 ), 0, 0 );
+                               gtk_table_attach( table, _radX, 1, 2, 1, 2,
+                                                                 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
+                                                                 (GtkAttachOptions) ( 0 ), 0, 0 );
+                               gtk_table_attach( table, _radY, 2, 3, 1, 2,
+                                                                 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
+                                                                 (GtkAttachOptions) ( 0 ), 0, 0 );
+                               gtk_table_attach( table, _radZ, 3, 4, 1, 2,
+                                                                 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
+                                                                 (GtkAttachOptions) ( 0 ), 0, 0 );
+                               radX = _radX;
+                               radY = _radY;
+                               radZ = _radZ;
+                               radNormals = _radNormals;
+                       }
+               }
+               {
+                       auto vbox = create_dialog_vbox( 4 );
+                       hbox.pack_start( vbox, FALSE, FALSE, 0 );
+                       {
+                               auto button = create_dialog_button( "OK", G_CALLBACK( dialog_button_ok ), &dialog );
+                               vbox.pack_start( button, FALSE, FALSE, 0 );
+                               widget_make_default( button );
+                               gtk_widget_grab_focus( button  );
+                               gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
+                       }
+                       {
+                               auto button = create_dialog_button( "Cancel", G_CALLBACK( dialog_button_cancel ), &dialog );
+                               vbox.pack_start( button, FALSE, FALSE, 0 );
+                               gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
+                       }
+               }
+       }
+
+       if ( modal_dialog_show( window, dialog ) == eIDOK ) {
+               int axis;
+               bool seams;
+               float thickness = static_cast<float>( atoi( gtk_entry_get_text( GTK_ENTRY( thicknessW ) ) ) );
+               seams = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON( seamsW )) ? true : false;
+
+               if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radX))) {
+                       axis = 0;
+               }
+               else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radY))) {
+                       axis = 1;
+               }
+               else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radZ))) {
+                       axis = 2;
+               }
+               else  {
+                       // Extrude along normals
+                       axis = 3;
+               }
+               Scene_PatchThicken( GlobalSceneGraph(), thickness, seams, axis );
+       }
+
+       gtk_widget_destroy( GTK_WIDGET( window ) );
+}