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