]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/patchmanip.cpp
Merge commit 'ee4b663ef7227a42b5eb051698265bed514ab3c0' into master-merge
[xonotic/netradiant.git] / radiant / patchmanip.cpp
1 /*
2    Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #include "patchmanip.h"
23
24 #include <gtk/gtk.h>
25 #include <gdk/gdkkeysyms.h>
26
27 #include "debugging/debugging.h"
28
29
30 #include "iselection.h"
31 #include "ipatch.h"
32
33 #include "math/vector.h"
34 #include "math/aabb.h"
35 #include "generic/callback.h"
36
37 #include "gtkutil/menu.h"
38 #include "gtkutil/image.h"
39 #include "map.h"
40 #include "mainframe.h"
41 #include "commands.h"
42 #include "gtkmisc.h"
43 #include "gtkdlgs.h"
44 #include "texwindow.h"
45 #include "xywindow.h"
46 #include "select.h"
47 #include "patch.h"
48 #include "grid.h"
49
50 PatchCreator* g_patchCreator = 0;
51
52 void Scene_PatchConstructPrefab( scene::Graph& graph, const AABB aabb, const char* shader, EPatchPrefab eType, int axis, std::size_t width = 3, std::size_t height = 3 ){
53         Select_Delete();
54         GlobalSelectionSystem().setSelectedAll( false );
55
56         NodeSmartReference node( g_patchCreator->createPatch() );
57         Node_getTraversable( Map_FindOrInsertWorldspawn( g_map ) )->insert( node );
58
59         Patch* patch = Node_getPatch( node );
60         patch->SetShader( shader );
61
62         patch->ConstructPrefab( aabb, eType, axis, width, height );
63         patch->controlPointsChanged();
64
65         {
66                 scene::Path patchpath( makeReference( GlobalSceneGraph().root() ) );
67                 patchpath.push( makeReference( *Map_GetWorldspawn( g_map ) ) );
68                 patchpath.push( makeReference( node.get() ) );
69                 Instance_getSelectable( *graph.find( patchpath ) )->setSelected( true );
70         }
71 }
72
73 void PatchAutoCapTexture( Patch& patch ) {
74
75         AABB box = patch.localAABB();
76         float x = box.extents.x();
77         float y = box.extents.y();
78         float z = box.extents.z();
79
80         int cap_direction = -1;
81         if ( x < y  && x < z )
82                 cap_direction = 0;
83         else if ( y < x  && y < z )
84                 cap_direction = 1;
85         else if ( z < x  && z < x )
86                 cap_direction = 2;
87
88         if ( cap_direction >= 0 )
89                 patch.ProjectTexture(cap_direction);
90         else
91                 patch.NaturalTexture();
92 }
93
94 void Patch_AutoCapTexture(){
95         UndoableCommand command( "patchAutoCapTexture" );
96         Scene_forEachVisibleSelectedPatch( &PatchAutoCapTexture );
97         SceneChangeNotify();
98 }
99
100 void Patch_makeCaps( Patch& patch, scene::Instance& instance, EPatchCap type, const char* shader ){
101         if ( ( type == eCapEndCap || type == eCapIEndCap )
102                  && patch.getWidth() != 5 ) {
103                 globalErrorStream() << "cannot create end-cap - patch width != 5\n";
104                 return;
105         }
106         if ( ( type == eCapBevel || type == eCapIBevel )
107                  && patch.getWidth() != 3 && patch.getWidth() != 5 ) {
108                 globalErrorStream() << "cannot create bevel-cap - patch width != 3\n";
109                 return;
110         }
111         if ( type == eCapCylinder
112                  && patch.getWidth() != 9 ) {
113                 globalErrorStream() << "cannot create cylinder-cap - patch width != 9\n";
114                 return;
115         }
116
117         {
118                 NodeSmartReference cap( g_patchCreator->createPatch() );
119                 Node_getTraversable( instance.path().parent() )->insert( cap );
120
121                 Patch* cap_patch = Node_getPatch( cap );
122                 patch.MakeCap( cap_patch, type, ROW, true );
123                 cap_patch->SetShader( shader );
124                 PatchAutoCapTexture(*cap_patch);
125
126                 scene::Path path( instance.path() );
127                 path.pop();
128                 path.push( makeReference( cap.get() ) );
129                 selectPath( path, true );
130         }
131
132         {
133                 NodeSmartReference cap( g_patchCreator->createPatch() );
134                 Node_getTraversable( instance.path().parent() )->insert( cap );
135
136                 Patch* cap_patch = Node_getPatch( cap );
137                 patch.MakeCap( cap_patch, type, ROW, false );
138                 cap_patch->SetShader( shader );
139                 PatchAutoCapTexture(*cap_patch);
140
141                 scene::Path path( instance.path() );
142                 path.pop();
143                 path.push( makeReference( cap.get() ) );
144                 selectPath( path, true );
145         }
146 }
147
148
149 typedef std::vector<scene::Instance*> InstanceVector;
150
151 enum ECapDialog {
152         PATCHCAP_BEVEL = 0,
153         PATCHCAP_ENDCAP,
154         PATCHCAP_INVERTED_BEVEL,
155         PATCHCAP_INVERTED_ENDCAP,
156         PATCHCAP_CYLINDER
157 };
158
159 EMessageBoxReturn DoCapDlg( ECapDialog *type );
160
161 void Scene_PatchDoCap_Selected( scene::Graph& graph, const char* shader ){
162         ECapDialog nType;
163
164         if ( DoCapDlg( &nType ) == eIDOK ) {
165                 EPatchCap eType;
166                 switch ( nType )
167                 {
168                 case PATCHCAP_INVERTED_BEVEL:
169                         eType = eCapIBevel;
170                         break;
171                 case PATCHCAP_BEVEL:
172                         eType = eCapBevel;
173                         break;
174                 case PATCHCAP_INVERTED_ENDCAP:
175                         eType = eCapIEndCap;
176                         break;
177                 case PATCHCAP_ENDCAP:
178                         eType = eCapEndCap;
179                         break;
180                 case PATCHCAP_CYLINDER:
181                         eType = eCapCylinder;
182                         break;
183                 default:
184                         ERROR_MESSAGE( "invalid patch cap type" );
185                         return;
186                 }
187
188                 InstanceVector instances;
189                 Scene_forEachVisibleSelectedPatchInstance([&](PatchInstance &patch) {
190                         instances.push_back(&patch);
191                 });
192                 for ( InstanceVector::const_iterator i = instances.begin(); i != instances.end(); ++i )
193                 {
194                         Patch_makeCaps( *Node_getPatch( ( *i )->path().top() ), *( *i ), eType, shader );
195                 }
196         }
197 }
198
199 void Patch_deform( Patch& patch, scene::Instance& instance, const int deform ){
200         patch.undoSave();
201
202         for (PatchControlIter i = patch.begin(); i != patch.end(); ++i)
203         {
204                 PatchControl& control = *i;
205                 int randomNumber = int( deform * (float(std::rand()) / float(RAND_MAX)));
206                 control.m_vertex[2] += randomNumber;
207         }
208
209         patch.controlPointsChanged();
210 }
211
212 void Scene_PatchDeform( scene::Graph& graph, const int deform )
213 {
214         InstanceVector instances;
215         Scene_forEachVisibleSelectedPatchInstance([&](PatchInstance &patch) {
216                         instances.push_back(&patch);
217         });
218         for ( InstanceVector::const_iterator i = instances.begin(); i != instances.end(); ++i )
219         {
220                 Patch_deform( *Node_getPatch( ( *i )->path().top() ), *( *i ), deform );
221         }
222
223 }
224
225 Patch* Scene_GetUltimateSelectedVisiblePatch(){
226         if ( GlobalSelectionSystem().countSelected() != 0 ) {
227                 scene::Node& node = GlobalSelectionSystem().ultimateSelected().path().top();
228                 if ( node.visible() ) {
229                         return Node_getPatch( node );
230                 }
231         }
232         return 0;
233 }
234
235
236 void Scene_PatchCapTexture_Selected( scene::Graph& graph ){
237         Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
238                 patch.ProjectTexture(Patch::m_CycleCapIndex);
239         });
240         Patch::m_CycleCapIndex = ( Patch::m_CycleCapIndex == 0 ) ? 1 : ( Patch::m_CycleCapIndex == 1 ) ? 2 : 0;
241         SceneChangeNotify();
242 }
243
244 void Scene_PatchFlipTexture_Selected( scene::Graph& graph, int axis ){
245         Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
246                 patch.FlipTexture(axis);
247         });
248 }
249
250 void Scene_PatchNaturalTexture_Selected( scene::Graph& graph ){
251         Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
252                 patch.NaturalTexture();
253         });
254         SceneChangeNotify();
255 }
256
257
258 void Scene_PatchInsertRemove_Selected( scene::Graph& graph, bool bInsert, bool bColumn, bool bFirst ){
259         Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
260                 patch.InsertRemove(bInsert, bColumn, bFirst);
261         });
262 }
263
264 void Scene_PatchInvert_Selected( scene::Graph& graph ){
265         Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
266                 patch.InvertMatrix();
267         });
268 }
269
270 void Scene_PatchRedisperse_Selected( scene::Graph& graph, EMatrixMajor major ){
271         Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
272                 patch.Redisperse(major);
273         });
274 }
275
276 void Scene_PatchSmooth_Selected( scene::Graph& graph, EMatrixMajor major ){
277         Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
278                 patch.Smooth(major);
279         });
280 }
281
282 void Scene_PatchTranspose_Selected( scene::Graph& graph ){
283         Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
284                 patch.TransposeMatrix();
285         });
286 }
287
288 void Scene_PatchSetShader_Selected( scene::Graph& graph, const char* name ){
289         Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
290                 patch.SetShader(name);
291         });
292         SceneChangeNotify();
293 }
294
295 void Scene_PatchGetShader_Selected( scene::Graph& graph, CopiedString& name ){
296         Patch* patch = Scene_GetUltimateSelectedVisiblePatch();
297         if ( patch != 0 ) {
298                 name = patch->GetShader();
299         }
300 }
301
302 void Scene_PatchSelectByShader( scene::Graph& graph, const char* name ){
303         Scene_forEachVisiblePatchInstance([&](PatchInstance &patch) {
304                 if (shader_equal(patch.getPatch().GetShader(), name)) {
305                         patch.setSelected(true);
306                 }
307         });
308 }
309
310
311 void Scene_PatchFindReplaceShader( scene::Graph& graph, const char* find, const char* replace ){
312         Scene_forEachVisiblePatch([&](Patch &patch) {
313                 if (shader_equal(patch.GetShader(), find)) {
314                         patch.SetShader(replace);
315                 }
316         });
317 }
318
319 void Scene_PatchFindReplaceShader_Selected( scene::Graph& graph, const char* find, const char* replace ){
320         Scene_forEachVisibleSelectedPatch([&](Patch &patch) {
321                 if (shader_equal(patch.GetShader(), find)) {
322                         patch.SetShader(replace);
323                 }
324         });
325 }
326
327
328 AABB PatchCreator_getBounds(){
329         AABB aabb( aabb_for_minmax( Select_getWorkZone().d_work_min, Select_getWorkZone().d_work_max ) );
330
331         float gridSize = GetGridSize();
332
333         if ( aabb.extents[0] == 0 ) {
334                 aabb.extents[0] = gridSize;
335         }
336         if ( aabb.extents[1] == 0 ) {
337                 aabb.extents[1] = gridSize;
338         }
339         if ( aabb.extents[2] == 0 ) {
340                 aabb.extents[2] = gridSize;
341         }
342
343         if ( aabb_valid( aabb ) ) {
344                 return aabb;
345         }
346         return AABB( Vector3( 0, 0, 0 ), Vector3( 64, 64, 64 ) );
347 }
348
349 void DoNewPatchDlg( EPatchPrefab prefab, int minrows, int mincols, int defrows, int defcols, int maxrows, int maxcols );
350
351 void Patch_XactCylinder(){
352         UndoableCommand undo( "patchCreateXactCylinder" );
353
354         DoNewPatchDlg( eXactCylinder, 3, 7, 3, 13, 0, 0 );
355 }
356
357 void Patch_XactSphere(){
358         UndoableCommand undo( "patchCreateXactSphere" );
359
360         DoNewPatchDlg( eXactSphere, 5, 7, 7, 13, 0, 0 );
361 }
362
363 void Patch_XactCone(){
364         UndoableCommand undo( "patchCreateXactCone" );
365
366         DoNewPatchDlg( eXactCone, 3, 7, 3, 13, 0, 0 );
367 }
368
369 void Patch_Cylinder(){
370         UndoableCommand undo( "patchCreateCylinder" );
371
372         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eCylinder, GlobalXYWnd_getCurrentViewType() );
373 }
374
375 void Patch_DenseCylinder(){
376         UndoableCommand undo( "patchCreateDenseCylinder" );
377
378         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eDenseCylinder, GlobalXYWnd_getCurrentViewType() );
379 }
380
381 void Patch_VeryDenseCylinder(){
382         UndoableCommand undo( "patchCreateVeryDenseCylinder" );
383
384         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eVeryDenseCylinder, GlobalXYWnd_getCurrentViewType() );
385 }
386
387 void Patch_SquareCylinder(){
388         UndoableCommand undo( "patchCreateSquareCylinder" );
389
390         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eSqCylinder, GlobalXYWnd_getCurrentViewType() );
391 }
392
393 void Patch_Endcap(){
394         UndoableCommand undo( "patchCreateCaps" );
395
396         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eEndCap, GlobalXYWnd_getCurrentViewType() );
397 }
398
399 void Patch_Bevel(){
400         UndoableCommand undo( "patchCreateBevel" );
401
402         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eBevel, GlobalXYWnd_getCurrentViewType() );
403 }
404
405 void Patch_Sphere(){
406         UndoableCommand undo( "patchCreateSphere" );
407
408         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eSphere, GlobalXYWnd_getCurrentViewType() );
409 }
410
411 void Patch_SquareBevel(){
412 }
413
414 void Patch_SquareEndcap(){
415 }
416
417 void Patch_Cone(){
418         UndoableCommand undo( "patchCreateCone" );
419
420         Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), eCone, GlobalXYWnd_getCurrentViewType() );
421 }
422
423 void Patch_Plane(){
424         UndoableCommand undo( "patchCreatePlane" );
425
426         DoNewPatchDlg( ePlane, 3, 3, 3, 3, 0, 0 );
427 }
428
429 void Patch_InsertInsertColumn(){
430         UndoableCommand undo( "patchInsertColumns" );
431
432         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), true, true, false );
433 }
434
435 void Patch_InsertAddColumn(){
436         UndoableCommand undo( "patchAddColumns" );
437
438         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), true, true, true );
439 }
440
441 void Patch_InsertInsertRow(){
442         UndoableCommand undo( "patchInsertRows" );
443
444         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), true, false, false );
445 }
446
447 void Patch_InsertAddRow(){
448         UndoableCommand undo( "patchAddRows" );
449
450         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), true, false, true );
451 }
452
453 void Patch_DeleteFirstColumn(){
454         UndoableCommand undo( "patchDeleteFirstColumns" );
455
456         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), false, true, true );
457 }
458
459 void Patch_DeleteLastColumn(){
460         UndoableCommand undo( "patchDeleteLastColumns" );
461
462         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), false, true, false );
463 }
464
465 void Patch_DeleteFirstRow(){
466         UndoableCommand undo( "patchDeleteFirstRows" );
467
468         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), false, false, true );
469 }
470
471 void Patch_DeleteLastRow(){
472         UndoableCommand undo( "patchDeleteLastRows" );
473
474         Scene_PatchInsertRemove_Selected( GlobalSceneGraph(), false, false, false );
475 }
476
477 void Patch_Invert(){
478         UndoableCommand undo( "patchInvert" );
479
480         Scene_PatchInvert_Selected( GlobalSceneGraph() );
481 }
482
483 void Patch_RedisperseRows(){
484         UndoableCommand undo( "patchRedisperseRows" );
485
486         Scene_PatchRedisperse_Selected( GlobalSceneGraph(), ROW );
487 }
488
489 void Patch_RedisperseCols(){
490         UndoableCommand undo( "patchRedisperseColumns" );
491
492         Scene_PatchRedisperse_Selected( GlobalSceneGraph(), COL );
493 }
494
495 void Patch_SmoothRows(){
496         UndoableCommand undo( "patchSmoothRows" );
497
498         Scene_PatchSmooth_Selected( GlobalSceneGraph(), ROW );
499 }
500
501 void Patch_SmoothCols(){
502         UndoableCommand undo( "patchSmoothColumns" );
503
504         Scene_PatchSmooth_Selected( GlobalSceneGraph(), COL );
505 }
506
507 void Patch_Transpose(){
508         UndoableCommand undo( "patchTranspose" );
509
510         Scene_PatchTranspose_Selected( GlobalSceneGraph() );
511 }
512
513 void Patch_Cap(){
514         // FIXME: add support for patch cap creation
515         // Patch_CapCurrent();
516         UndoableCommand undo( "patchCreateCaps" );
517
518         Scene_PatchDoCap_Selected( GlobalSceneGraph(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
519 }
520
521 void Patch_CycleProjection(){
522         UndoableCommand undo( "patchCycleUVProjectionAxis" );
523
524         Scene_PatchCapTexture_Selected( GlobalSceneGraph() );
525 }
526
527 ///\todo Unfinished.
528 void Patch_OverlayOn(){
529 }
530
531 ///\todo Unfinished.
532 void Patch_OverlayOff(){
533 }
534
535 void Patch_FlipTextureX(){
536         UndoableCommand undo( "patchFlipTextureU" );
537
538         Scene_PatchFlipTexture_Selected( GlobalSceneGraph(), 0 );
539 }
540
541 void Patch_FlipTextureY(){
542         UndoableCommand undo( "patchFlipTextureV" );
543
544         Scene_PatchFlipTexture_Selected( GlobalSceneGraph(), 1 );
545 }
546
547 void Patch_NaturalTexture(){
548         UndoableCommand undo( "patchNaturalTexture" );
549
550         Scene_PatchNaturalTexture_Selected( GlobalSceneGraph() );
551 }
552
553 void Patch_CapTexture(){
554         UndoableCommand command( "patchCapTexture" );
555
556         Scene_PatchCapTexture_Selected( GlobalSceneGraph() );
557 }
558
559 void Patch_ResetTexture(){
560         float fx, fy;
561         if ( DoTextureLayout( &fx, &fy ) == eIDOK ) {
562                 UndoableCommand command( "patchTileTexture" );
563                 Scene_PatchTileTexture_Selected( GlobalSceneGraph(), fx, fy );
564         }
565 }
566
567 void Patch_FitTexture(){
568         UndoableCommand command( "patchFitTexture" );
569
570         Scene_PatchTileTexture_Selected( GlobalSceneGraph(), 1, 1 );
571 }
572
573 void DoPatchDeformDlg();
574
575 void Patch_Deform(){
576         UndoableCommand undo( "patchDeform" );
577
578         DoPatchDeformDlg();
579 }
580
581 #include "ifilter.h"
582
583
584 class filter_patch_all : public PatchFilter
585 {
586 public:
587 bool filter( const Patch& patch ) const {
588         return true;
589 }
590 };
591
592 class filter_patch_shader : public PatchFilter
593 {
594 const char* m_shader;
595 public:
596 filter_patch_shader( const char* shader ) : m_shader( shader ){
597 }
598 bool filter( const Patch& patch ) const {
599         return shader_equal( patch.GetShader(), m_shader );
600 }
601 };
602
603 class filter_patch_flags : public PatchFilter
604 {
605 int m_flags;
606 public:
607 filter_patch_flags( int flags ) : m_flags( flags ){
608 }
609 bool filter( const Patch& patch ) const {
610         return ( patch.getShaderFlags() & m_flags ) != 0;
611 }
612 };
613
614
615 filter_patch_all g_filter_patch_all;
616 filter_patch_shader g_filter_patch_clip( "textures/common/clip" );
617 filter_patch_shader g_filter_patch_weapclip( "textures/common/weapclip" );
618 filter_patch_flags g_filter_patch_translucent( QER_TRANS );
619
620 void PatchFilters_construct(){
621         add_patch_filter( g_filter_patch_all, EXCLUDE_CURVES );
622         add_patch_filter( g_filter_patch_clip, EXCLUDE_CLIP );
623         add_patch_filter( g_filter_patch_weapclip, EXCLUDE_CLIP );
624         add_patch_filter( g_filter_patch_translucent, EXCLUDE_TRANSLUCENT );
625 }
626
627
628 #include "preferences.h"
629
630 void Patch_constructPreferences( PreferencesPage& page ){
631         page.appendEntry( "Patch Subdivide Threshold", g_PatchSubdivideThreshold );
632 }
633 void Patch_constructPage( PreferenceGroup& group ){
634         PreferencesPage page( group.createPage( "Patches", "Patch Display Preferences" ) );
635         Patch_constructPreferences( page );
636 }
637 void Patch_registerPreferencesPage(){
638         PreferencesDialog_addDisplayPage( makeCallbackF(Patch_constructPage) );
639 }
640
641
642 #include "preferencesystem.h"
643
644 void PatchPreferences_construct(){
645         GlobalPreferenceSystem().registerPreference( "Subdivisions", make_property_string( g_PatchSubdivideThreshold ) );
646 }
647
648
649 #include "generic/callback.h"
650
651 void Patch_registerCommands(){
652         GlobalCommands_insert( "InvertCurveTextureX", makeCallbackF(Patch_FlipTextureX), Accelerator( 'I', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
653         GlobalCommands_insert( "InvertCurveTextureY", makeCallbackF(Patch_FlipTextureY), Accelerator( 'I', (GdkModifierType)GDK_SHIFT_MASK ) );
654         GlobalCommands_insert( "IncPatchColumn", makeCallbackF(Patch_InsertInsertColumn), Accelerator( GDK_KP_Add, (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
655         GlobalCommands_insert( "IncPatchRow", makeCallbackF(Patch_InsertInsertRow), Accelerator( GDK_KP_Add, (GdkModifierType)GDK_CONTROL_MASK ) );
656         GlobalCommands_insert( "DecPatchColumn", makeCallbackF(Patch_DeleteLastColumn), Accelerator( GDK_KP_Subtract, (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
657         GlobalCommands_insert( "DecPatchRow", makeCallbackF(Patch_DeleteLastRow), Accelerator( GDK_KP_Subtract, (GdkModifierType)GDK_CONTROL_MASK ) );
658         GlobalCommands_insert( "NaturalizePatch", makeCallbackF(Patch_NaturalTexture), Accelerator( 'N', (GdkModifierType)GDK_CONTROL_MASK ) );
659         GlobalCommands_insert( "PatchCylinder", makeCallbackF(Patch_Cylinder) );
660         GlobalCommands_insert( "PatchDenseCylinder", makeCallbackF(Patch_DenseCylinder) );
661         GlobalCommands_insert( "PatchVeryDenseCylinder", makeCallbackF(Patch_VeryDenseCylinder) );
662         GlobalCommands_insert( "PatchSquareCylinder", makeCallbackF(Patch_SquareCylinder) );
663         GlobalCommands_insert( "PatchXactCylinder", makeCallbackF(Patch_XactCylinder) );
664         GlobalCommands_insert( "PatchXactSphere", makeCallbackF(Patch_XactSphere) );
665         GlobalCommands_insert( "PatchXactCone", makeCallbackF(Patch_XactCone) );
666         GlobalCommands_insert( "PatchEndCap", makeCallbackF(Patch_Endcap) );
667         GlobalCommands_insert( "PatchBevel", makeCallbackF(Patch_Bevel) );
668         GlobalCommands_insert( "PatchSquareBevel", makeCallbackF(Patch_SquareBevel) );
669         GlobalCommands_insert( "PatchSquareEndcap", makeCallbackF(Patch_SquareEndcap) );
670         GlobalCommands_insert( "PatchCone", makeCallbackF(Patch_Cone) );
671         GlobalCommands_insert( "PatchSphere", makeCallbackF(Patch_Sphere) );
672         GlobalCommands_insert( "SimplePatchMesh", makeCallbackF(Patch_Plane), Accelerator( 'P', (GdkModifierType)GDK_SHIFT_MASK ) );
673         GlobalCommands_insert( "PatchInsertInsertColumn", makeCallbackF(Patch_InsertInsertColumn), Accelerator( GDK_KEY_KP_Add, (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
674         GlobalCommands_insert( "PatchInsertAddColumn", makeCallbackF(Patch_InsertAddColumn) );
675         GlobalCommands_insert( "PatchInsertInsertRow", makeCallbackF(Patch_InsertInsertRow), Accelerator( GDK_KEY_KP_Add, (GdkModifierType)GDK_CONTROL_MASK ) );
676         GlobalCommands_insert( "PatchInsertAddRow", makeCallbackF(Patch_InsertAddRow) );
677         GlobalCommands_insert( "PatchDeleteFirstColumn", makeCallbackF(Patch_DeleteFirstColumn) );
678         GlobalCommands_insert( "PatchDeleteLastColumn", makeCallbackF(Patch_DeleteLastColumn), Accelerator( GDK_KEY_KP_Subtract, (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
679         GlobalCommands_insert( "PatchDeleteFirstRow", makeCallbackF(Patch_DeleteFirstRow), Accelerator( GDK_KEY_KP_Subtract, (GdkModifierType)GDK_CONTROL_MASK ) );
680         GlobalCommands_insert( "PatchDeleteLastRow", makeCallbackF(Patch_DeleteLastRow) );
681         GlobalCommands_insert( "InvertCurve", makeCallbackF(Patch_Invert), Accelerator( 'I', (GdkModifierType)GDK_CONTROL_MASK ) );
682         GlobalCommands_insert( "RedisperseRows", makeCallbackF(Patch_RedisperseRows), Accelerator( 'E', (GdkModifierType)GDK_CONTROL_MASK ) );
683         GlobalCommands_insert( "RedisperseCols", makeCallbackF(Patch_RedisperseCols), Accelerator( 'E', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
684         GlobalCommands_insert( "SmoothRows", makeCallbackF(Patch_SmoothRows), Accelerator( 'W', (GdkModifierType)GDK_CONTROL_MASK ) );
685         GlobalCommands_insert( "SmoothCols", makeCallbackF(Patch_SmoothCols), Accelerator( 'W', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
686         GlobalCommands_insert( "MatrixTranspose", makeCallbackF(Patch_Transpose), Accelerator( 'M', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
687         GlobalCommands_insert( "CapCurrentCurve", makeCallbackF(Patch_Cap), Accelerator( 'C', (GdkModifierType)GDK_SHIFT_MASK ) );
688         GlobalCommands_insert( "CycleCapTexturePatch", makeCallbackF(Patch_CycleProjection), Accelerator( 'N', (GdkModifierType)( GDK_SHIFT_MASK | GDK_CONTROL_MASK ) ) );
689         GlobalCommands_insert( "MakeOverlayPatch", makeCallbackF(Patch_OverlayOn), Accelerator( 'Y' ) );
690         GlobalCommands_insert( "ClearPatchOverlays", makeCallbackF(Patch_OverlayOff), Accelerator( 'L', (GdkModifierType)GDK_CONTROL_MASK ) );
691         GlobalCommands_insert( "PatchDeform", makeCallbackF(Patch_Deform) );
692 }
693
694 void Patch_constructToolbar( ui::Toolbar toolbar ){
695         toolbar_append_button( toolbar, "Put caps on the current patch (SHIFT + C)", "cap_curve.png", "CapCurrentCurve" );
696 }
697
698 void Patch_constructMenu( ui::Menu menu ){
699         create_menu_item_with_mnemonic( menu, "Cylinder", "PatchCylinder" );
700         {
701                 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "More Cylinders" );
702                 if ( g_Layout_enableDetachableMenus.m_value ) {
703                         menu_tearoff( menu_in_menu );
704                 }
705                 create_menu_item_with_mnemonic( menu_in_menu, "Dense Cylinder", "PatchDenseCylinder" );
706                 create_menu_item_with_mnemonic( menu_in_menu, "Very Dense Cylinder", "PatchVeryDenseCylinder" );
707                 create_menu_item_with_mnemonic( menu_in_menu, "Square Cylinder", "PatchSquareCylinder" );
708                 create_menu_item_with_mnemonic( menu_in_menu, "Exact Cylinder...", "PatchXactCylinder" );
709         }
710         menu_separator( menu );
711         create_menu_item_with_mnemonic( menu, "End cap", "PatchEndCap" );
712         create_menu_item_with_mnemonic( menu, "Bevel", "PatchBevel" );
713         {
714 //              auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "More End caps, Bevels" );
715 //              if ( g_Layout_enableDetachableMenus.m_value ) {
716 //                      menu_tearoff( menu_in_menu );
717 //              }
718                 create_menu_item_with_mnemonic( menu, "Square Endcap", "PatchSquareBevel" );
719                 create_menu_item_with_mnemonic( menu, "Square Bevel", "PatchSquareEndcap" );
720         }
721         menu_separator( menu );
722         create_menu_item_with_mnemonic( menu, "Cone", "PatchCone" );
723         create_menu_item_with_mnemonic( menu, "Exact Cone...", "PatchXactCone" );
724         menu_separator( menu );
725         create_menu_item_with_mnemonic( menu, "Sphere", "PatchSphere" );
726         create_menu_item_with_mnemonic( menu, "Exact Sphere...", "PatchXactSphere" );
727         menu_separator( menu );
728         create_menu_item_with_mnemonic( menu, "Simple Patch Mesh...", "SimplePatchMesh" );
729         menu_separator( menu );
730         {
731                 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Insert" );
732                 if ( g_Layout_enableDetachableMenus.m_value ) {
733                         menu_tearoff( menu_in_menu );
734                 }
735                 create_menu_item_with_mnemonic( menu_in_menu, "Insert (2) Columns", "PatchInsertInsertColumn" );
736                 create_menu_item_with_mnemonic( menu_in_menu, "Add (2) Columns", "PatchInsertAddColumn" );
737                 menu_separator( menu_in_menu );
738                 create_menu_item_with_mnemonic( menu_in_menu, "Insert (2) Rows", "PatchInsertInsertRow" );
739                 create_menu_item_with_mnemonic( menu_in_menu, "Add (2) Rows", "PatchInsertAddRow" );
740         }
741         {
742                 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Delete" );
743                 if ( g_Layout_enableDetachableMenus.m_value ) {
744                         menu_tearoff( menu_in_menu );
745                 }
746                 create_menu_item_with_mnemonic( menu_in_menu, "First (2) Columns", "PatchDeleteFirstColumn" );
747                 create_menu_item_with_mnemonic( menu_in_menu, "Last (2) Columns", "PatchDeleteLastColumn" );
748                 menu_separator( menu_in_menu );
749                 create_menu_item_with_mnemonic( menu_in_menu, "First (2) Rows", "PatchDeleteFirstRow" );
750                 create_menu_item_with_mnemonic( menu_in_menu, "Last (2) Rows", "PatchDeleteLastRow" );
751         }
752         menu_separator( menu );
753         {
754                 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Matrix" );
755                 if ( g_Layout_enableDetachableMenus.m_value ) {
756                         menu_tearoff( menu_in_menu );
757                 }
758                 create_menu_item_with_mnemonic( menu_in_menu, "Invert", "InvertCurve" );
759 //              auto menu_3 = create_sub_menu_with_mnemonic( menu_in_menu, "Re-disperse" );
760 //              if ( g_Layout_enableDetachableMenus.m_value ) {
761 //                      menu_tearoff( menu_3 );
762 //              }
763                 menu_separator( menu_in_menu );
764                 create_menu_item_with_mnemonic( menu, "Rows", "RedisperseRows" );
765                 create_menu_item_with_mnemonic( menu, "Columns", "RedisperseCols" );
766 //              auto menu_4 = create_sub_menu_with_mnemonic( menu_in_menu, "Smooth" );
767 //              if ( g_Layout_enableDetachableMenus.m_value ) {
768 //                      menu_tearoff( menu_4 );
769 //              }
770                 create_menu_item_with_mnemonic( menu, "Rows", "SmoothRows" );
771                 create_menu_item_with_mnemonic( menu, "Columns", "SmoothCols" );
772                 create_menu_item_with_mnemonic( menu_in_menu, "Transpose", "MatrixTranspose" );
773
774         }
775         menu_separator( menu );
776         create_menu_item_with_mnemonic( menu, "Cap Selection", "CapCurrentCurve" );
777         create_menu_item_with_mnemonic( menu, "Cycle Cap Texture", "CycleCapTexturePatch" );
778         menu_separator( menu );
779         {
780                 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Texture" );
781                 if ( g_Layout_enableDetachableMenus.m_value ) {
782                         menu_tearoff( menu_in_menu );
783                 }
784                 create_menu_item_with_mnemonic( menu_in_menu, "Cycle Projection", "CycleCapTexturePatch" );
785                 create_menu_item_with_mnemonic( menu_in_menu, "Naturalize", "NaturalizePatch" );
786                 create_menu_item_with_mnemonic( menu_in_menu, "Invert X", "InvertCurveTextureX" );
787                 create_menu_item_with_mnemonic( menu_in_menu, "Invert Y", "InvertCurveTextureY" );
788
789         }
790         menu_separator( menu );
791         {
792                 auto menu_in_menu = create_sub_menu_with_mnemonic( menu, "Overlay" );
793                 if ( g_Layout_enableDetachableMenus.m_value ) {
794                         menu_tearoff( menu_in_menu );
795                 }
796                 create_menu_item_with_mnemonic( menu_in_menu, "Set", "MakeOverlayPatch" );
797                 create_menu_item_with_mnemonic( menu_in_menu, "Clear", "ClearPatchOverlays" );
798         }
799         menu_separator( menu );
800         create_menu_item_with_mnemonic( menu, "Deform...", "PatchDeform" );
801 }
802
803
804 #include "gtkutil/dialog.h"
805 #include "gtkutil/widget.h"
806
807 void DoNewPatchDlg( EPatchPrefab prefab, int minrows, int mincols, int defrows, int defcols, int maxrows, int maxcols ){
808         ModalDialog dialog;
809
810         ui::Window window = MainFrame_getWindow().create_dialog_window("Patch density", G_CALLBACK(dialog_delete_callback ), &dialog );
811
812         auto accel = ui::AccelGroup(ui::New);
813         window.add_accel_group( accel );
814         auto width = ui::ComboBoxText(ui::New);
815         auto height = ui::ComboBoxText(ui::New);
816         {
817                 auto hbox = create_dialog_hbox( 4, 4 );
818                 window.add(hbox);
819                 {
820                         auto table = create_dialog_table( 2, 2, 4, 4 );
821                         hbox.pack_start( table, TRUE, TRUE, 0 );
822                         {
823                                 auto label = ui::Label( "Width:" );
824                                 label.show();
825                 table.attach(label, {0, 1, 0, 1}, {GTK_FILL, 0});
826                                 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
827                         }
828                         {
829                                 auto label = ui::Label( "Height:" );
830                                 label.show();
831                 table.attach(label, {0, 1, 1, 2}, {GTK_FILL, 0});
832                                 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
833                         }
834
835                         {
836                                 auto combo = width;
837 #define D_ITEM( x ) if ( x >= mincols && ( !maxcols || x <= maxcols ) ) gtk_combo_box_text_append_text( combo, #x )
838                                 D_ITEM( 3 );
839                                 D_ITEM( 5 );
840                                 D_ITEM( 7 );
841                                 D_ITEM( 9 );
842                                 D_ITEM( 11 );
843                                 D_ITEM( 13 );
844                                 D_ITEM( 15 );
845                                 D_ITEM( 17 );
846                                 D_ITEM( 19 );
847                                 D_ITEM( 21 );
848                                 D_ITEM( 23 );
849                                 D_ITEM( 25 );
850                                 D_ITEM( 27 );
851                                 D_ITEM( 29 );
852                                 D_ITEM( 31 ); // MAX_PATCH_SIZE is 32, so we should be able to do 31...
853 #undef D_ITEM
854                                 combo.show();
855                 table.attach(combo, {1, 2, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
856                         }
857                         {
858                                 auto combo = height;
859 #define D_ITEM( x ) if ( x >= minrows && ( !maxrows || x <= maxrows ) ) gtk_combo_box_text_append_text( combo, #x )
860                                 D_ITEM( 3 );
861                                 D_ITEM( 5 );
862                                 D_ITEM( 7 );
863                                 D_ITEM( 9 );
864                                 D_ITEM( 11 );
865                                 D_ITEM( 13 );
866                                 D_ITEM( 15 );
867                                 D_ITEM( 17 );
868                                 D_ITEM( 19 );
869                                 D_ITEM( 21 );
870                                 D_ITEM( 23 );
871                                 D_ITEM( 25 );
872                                 D_ITEM( 27 );
873                                 D_ITEM( 29 );
874                                 D_ITEM( 31 ); // MAX_PATCH_SIZE is 32, so we should be able to do 31...
875 #undef D_ITEM
876                                 combo.show();
877                 table.attach(combo, {1, 2, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
878                         }
879                 }
880
881                 {
882                         auto vbox = create_dialog_vbox( 4 );
883                         hbox.pack_start( vbox, TRUE, TRUE, 0 );
884                         {
885                                 auto button = create_dialog_button( "OK", G_CALLBACK( dialog_button_ok ), &dialog );
886                                 vbox.pack_start( button, FALSE, FALSE, 0 );
887                                 widget_make_default( button );
888                                 gtk_widget_grab_focus( button  );
889                                 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
890                         }
891                         {
892                                 auto button = create_dialog_button( "Cancel", G_CALLBACK( dialog_button_cancel ), &dialog );
893                                 vbox.pack_start( button, FALSE, FALSE, 0 );
894                                 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
895                         }
896                 }
897         }
898
899         // Initialize dialog
900         gtk_combo_box_set_active( width, ( defcols - mincols ) / 2 );
901         gtk_combo_box_set_active( height, ( defrows - minrows ) / 2 );
902
903         if ( modal_dialog_show( window, dialog ) == eIDOK ) {
904                 int w = gtk_combo_box_get_active( width ) * 2 + mincols;
905                 int h = gtk_combo_box_get_active( height ) * 2 + minrows;
906
907                 Scene_PatchConstructPrefab( GlobalSceneGraph(), PatchCreator_getBounds(), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), prefab, GlobalXYWnd_getCurrentViewType(), w, h );
908         }
909
910         window.destroy();
911 }
912
913
914 void DoPatchDeformDlg(){
915         ModalDialog dialog;
916         GtkWidget* deformW;
917
918         ui::Window window = create_dialog_window( MainFrame_getWindow(), "Patch deform", G_CALLBACK( dialog_delete_callback ), &dialog );
919
920         GtkAccelGroup* accel = gtk_accel_group_new();
921         gtk_window_add_accel_group( window, accel );
922
923         {
924                 GtkHBox* hbox = create_dialog_hbox( 4, 4 );
925                 gtk_container_add( GTK_CONTAINER( window ), GTK_WIDGET( hbox ) );
926                 {
927                         GtkTable* table = create_dialog_table( 2, 2, 4, 4 );
928                         gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
929                         {
930                                 GtkLabel* label = GTK_LABEL( gtk_label_new( "Max deform:" ) );
931                                 gtk_widget_show( GTK_WIDGET( label ) );
932                                 gtk_table_attach( table, GTK_WIDGET( label ), 0, 1, 0, 1,
933                                                                   (GtkAttachOptions) ( GTK_FILL ),
934                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
935                                 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
936                         }
937                         {
938                                 GtkWidget* entry = gtk_entry_new();
939                                 gtk_entry_set_text( GTK_ENTRY( entry ), "16" );
940                                 gtk_widget_show( entry );
941                                 gtk_table_attach( table, entry, 1, 2, 0, 1,
942                                                                   (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
943                                                                   (GtkAttachOptions) ( 0 ), 0, 0 );
944
945                                 deformW = entry;
946                         }
947                 }
948                 {
949                         auto vbox = create_dialog_vbox( 4 );
950                         gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( vbox ), TRUE, TRUE, 0 );
951                         {
952                                 auto button = create_dialog_button( "OK", G_CALLBACK( dialog_button_ok ), &dialog );
953                                 vbox.pack_start( button, FALSE, FALSE, 0 );
954                                 widget_make_default( button );
955                                 gtk_widget_grab_focus( button  );
956                                 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
957                         }
958                         {
959                                 auto button = create_dialog_button( "Cancel", G_CALLBACK( dialog_button_cancel ), &dialog );
960                                 vbox.pack_start( button, FALSE, FALSE, 0 );
961                                 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
962                         }
963                 }
964         }
965
966         if ( modal_dialog_show( window, dialog ) == eIDOK ) {
967                 int deform = static_cast<int>( atoi( gtk_entry_get_text( GTK_ENTRY( deformW ) ) ) );
968                 Scene_PatchDeform( GlobalSceneGraph(), deform );
969         }
970
971         gtk_widget_destroy( GTK_WIDGET( window ) );
972 }
973
974
975
976 EMessageBoxReturn DoCapDlg( ECapDialog* type ){
977         ModalDialog dialog;
978         ModalDialogButton ok_button( dialog, eIDOK );
979         ModalDialogButton cancel_button( dialog, eIDCANCEL );
980         ui::Widget bevel{ui::null};
981         ui::Widget ibevel{ui::null};
982         ui::Widget endcap{ui::null};
983         ui::Widget iendcap{ui::null};
984         ui::Widget cylinder{ui::null};
985
986         ui::Window window = MainFrame_getWindow().create_modal_dialog_window( "Cap", dialog );
987
988         auto accel_group = ui::AccelGroup(ui::New);
989         window.add_accel_group( accel_group );
990
991         {
992                 auto hbox = create_dialog_hbox( 4, 4 );
993                 window.add(hbox);
994
995                 {
996                         // Gef: Added a vbox to contain the toggle buttons
997                         auto radio_vbox = create_dialog_vbox( 4 );
998                         hbox.add(radio_vbox);
999
1000                         {
1001                                 auto table = ui::Table( 5, 2, FALSE );
1002                                 table.show();
1003                                 radio_vbox.pack_start( table, TRUE, TRUE, 0 );
1004                                 gtk_table_set_row_spacings( table, 5 );
1005                                 gtk_table_set_col_spacings( table, 5 );
1006
1007                                 {
1008                                         auto image = new_local_image( "cap_bevel.png" );
1009                                         image.show();
1010                     table.attach(image, {0, 1, 0, 1}, {GTK_FILL, 0});
1011                                 }
1012                                 {
1013                                         auto image = new_local_image( "cap_endcap.png" );
1014                                         image.show();
1015                     table.attach(image, {0, 1, 1, 2}, {GTK_FILL, 0});
1016                                 }
1017                                 {
1018                                         auto image = new_local_image( "cap_ibevel.png" );
1019                                         image.show();
1020                     table.attach(image, {0, 1, 2, 3}, {GTK_FILL, 0});
1021                                 }
1022                                 {
1023                                         auto image = new_local_image( "cap_iendcap.png" );
1024                                         image.show();
1025                     table.attach(image, {0, 1, 3, 4}, {GTK_FILL, 0});
1026                                 }
1027                                 {
1028                                         auto image = new_local_image( "cap_cylinder.png" );
1029                                         image.show();
1030                     table.attach(image, {0, 1, 4, 5}, {GTK_FILL, 0});
1031                                 }
1032
1033                                 GSList* group = 0;
1034                                 {
1035                                         ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Bevel" ));
1036                                         button.show();
1037                     table.attach(button, {1, 2, 0, 1}, {GTK_FILL | GTK_EXPAND, 0});
1038                                         group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
1039
1040                                         bevel = button;
1041                                 }
1042                                 {
1043                                         ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Endcap" ));
1044                                         button.show();
1045                     table.attach(button, {1, 2, 1, 2}, {GTK_FILL | GTK_EXPAND, 0});
1046                                         group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
1047
1048                                         endcap = button;
1049                                 }
1050                                 {
1051                                         ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Inverted Bevel" ));
1052                                         button.show();
1053                     table.attach(button, {1, 2, 2, 3}, {GTK_FILL | GTK_EXPAND, 0});
1054                                         group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
1055
1056                                         ibevel = button;
1057                                 }
1058                                 {
1059                                         ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Inverted Endcap" ));
1060                                         button.show();
1061                     table.attach(button, {1, 2, 3, 4}, {GTK_FILL | GTK_EXPAND, 0});
1062                                         group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
1063
1064                                         iendcap = button;
1065                                 }
1066                                 {
1067                                         ui::Widget button = ui::Widget::from(gtk_radio_button_new_with_label( group, "Cylinder" ));
1068                                         button.show();
1069                     table.attach(button, {1, 2, 4, 5}, {GTK_FILL | GTK_EXPAND, 0});
1070                                         group = gtk_radio_button_get_group( GTK_RADIO_BUTTON( button ) );
1071
1072                                         cylinder = button;
1073                                 }
1074                         }
1075                 }
1076
1077                 {
1078                         auto vbox = create_dialog_vbox( 4 );
1079                         hbox.pack_start( vbox, FALSE, FALSE, 0 );
1080                         {
1081                                 auto button = create_modal_dialog_button( "OK", ok_button );
1082                                 vbox.pack_start( button, FALSE, FALSE, 0 );
1083                                 widget_make_default( button );
1084                                 gtk_widget_add_accelerator( button , "clicked", accel_group, GDK_KEY_Return, (GdkModifierType)0, GTK_ACCEL_VISIBLE );
1085                         }
1086                         {
1087                                 auto button = create_modal_dialog_button( "Cancel", cancel_button );
1088                                 vbox.pack_start( button, FALSE, FALSE, 0 );
1089                                 gtk_widget_add_accelerator( button , "clicked", accel_group, GDK_KEY_Escape, (GdkModifierType)0, GTK_ACCEL_VISIBLE );
1090                         }
1091                 }
1092         }
1093
1094         // Initialize dialog
1095         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( bevel ), TRUE );
1096
1097         EMessageBoxReturn ret = modal_dialog_show( window, dialog );
1098         if ( ret == eIDOK ) {
1099                 if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( bevel ) ) ) {
1100                         *type = PATCHCAP_BEVEL;
1101                 }
1102                 else if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( endcap ) ) ) {
1103                         *type = PATCHCAP_ENDCAP;
1104                 }
1105                 else if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( ibevel ) ) ) {
1106                         *type = PATCHCAP_INVERTED_BEVEL;
1107                 }
1108                 else if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( iendcap ) ) ) {
1109                         *type = PATCHCAP_INVERTED_ENDCAP;
1110                 }
1111                 else if ( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( cylinder ) ) ) {
1112                         *type = PATCHCAP_CYLINDER;
1113                 }
1114         }
1115
1116         window.destroy();
1117
1118         return ret;
1119 }