Debian Codegraph @

radiant/gtkdlgs.cpp

raw symbols SCIP
1 /*
2 Copyright (c) 2001, Loki software, inc.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this list
9 of conditions and the following disclaimer.
10
11 Redistributions in binary form must reproduce the above copyright notice, this
12 list of conditions and the following disclaimer in the documentation and/or
13 other materials provided with the distribution.
14
15 Neither the name of Loki software nor the names of its contributors may be used
16 to endorse or promote products derived from this software without specific prior
17 written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
23 DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 //
32 // Some small dialogs that don't need much
33 //
34 // Leonardo Zide ([email protected])
35 //
36
37 #include "gtkdlgs.h"
38 #include "globaldefs.h"
39
40 #include <gtk/gtk.h>
41
42 #include "debugging/debugging.h"
43 #include "aboutmsg.h"
44
45 #include "igl.h"
46 #include "iscenegraph.h"
47 #include "iselection.h"
48
49 #include <gdk/gdkkeysyms.h>
50 #include <uilib/uilib.h>
51 #include <gtk/gtkspinbutton.h>
52
53 #include "os/path.h"
54 #include "math/aabb.h"
55 #include "container/array.h"
56 #include "generic/static.h"
57 #include "stream/stringstream.h"
58 #include "convert.h"
59 #include "gtkutil/messagebox.h"
60 #include "gtkutil/image.h"
61
62 #include "gtkmisc.h"
63 #include "brushmanip.h"
64 #include "build.h"
65 #include "qe3.h"
66 #include "texwindow.h"
67 #include "xywindow.h"
68 #include "mainframe.h"
69 #include "preferences.h"
70 #include "url.h"
71 #include "cmdlib.h"
72
73 #include "qerplugin.h"
74 #include "os/file.h"
75
76
77
78 // =============================================================================
79 // Project settings dialog
80
81 class GameComboConfiguration
82 {
83 public:
84 const char* basegame_dir;
85 const char* basegame;
86 const char* known_dir;
87 const char* known;
88 const char* custom;
89
90 GameComboConfiguration() :
91 basegame_dir( g_pGameDescription->getRequiredKeyValue( "basegame" ) ),
92 basegame( g_pGameDescription->getRequiredKeyValue( "basegamename" ) ),
93 known_dir( g_pGameDescription->getKeyValue( "knowngame" ) ),
94 known( g_pGameDescription->getKeyValue( "knowngamename" ) ),
95 custom( g_pGameDescription->getRequiredKeyValue( "unknowngamename" ) ){
96 }
97 };
98
99 typedef LazyStatic<GameComboConfiguration> LazyStaticGameComboConfiguration;
100
101 inline GameComboConfiguration& globalGameComboConfiguration(){
102 return LazyStaticGameComboConfiguration::instance();
103 }
104
105
106 struct gamecombo_t
107 {
108 gamecombo_t( int _game, const char* _fs_game, bool _sensitive )
109 : game( _game ), fs_game( _fs_game ), sensitive( _sensitive )
110 {}
111 int game;
112 const char* fs_game;
113 bool sensitive;
114 };
115
116 gamecombo_t gamecombo_for_dir( const char* dir ){
117 if ( string_equal( dir, globalGameComboConfiguration().basegame_dir ) ) {
118 return gamecombo_t( 0, "", false );
119 }
120 else if ( string_equal( dir, globalGameComboConfiguration().known_dir ) ) {
121 return gamecombo_t( 1, dir, false );
122 }
123 else
124 {
125 return gamecombo_t( string_empty( globalGameComboConfiguration().known_dir ) ? 1 : 2, dir, true );
126 }
127 }
128
129 gamecombo_t gamecombo_for_gamename( const char* gamename ){
130 if ( ( strlen( gamename ) == 0 ) || !strcmp( gamename, globalGameComboConfiguration().basegame ) ) {
131 return gamecombo_t( 0, "", false );
132 }
133 else if ( !strcmp( gamename, globalGameComboConfiguration().known ) ) {
134 return gamecombo_t( 1, globalGameComboConfiguration().known_dir, false );
135 }
136 else
137 {
138 return gamecombo_t( string_empty( globalGameComboConfiguration().known_dir ) ? 1 : 2, "", true );
139 }
140 }
141
142 inline void path_copy_clean( char* destination, const char* source ){
143 char* i = destination;
144
145 while ( *source != '\0' )
146 {
147 *i++ = ( *source == '\\' ) ? '/' : *source;
148 ++source;
149 }
150
151 if ( i != destination && *( i - 1 ) != '/' ) {
152 *( i++ ) = '/';
153 }
154
155 *i = '\0';
156 }
157
158
159 struct GameCombo
160 {
161 ui::ComboBoxText game_select{ui::null};
162 ui::Entry fsgame_entry{ui::null};
163 };
164
165 gboolean OnSelchangeComboWhatgame( ui::Widget widget, GameCombo* combo ){
166 const char *gamename;
167 {
168 GtkTreeIter iter;
169 gtk_combo_box_get_active_iter( combo->game_select, &iter );
170 gtk_tree_model_get( gtk_combo_box_get_model( combo->game_select ), &iter, 0, (gpointer*)&gamename, -1 );
171 }
172
173 gamecombo_t gamecombo = gamecombo_for_gamename( gamename );
174
175 combo->fsgame_entry.text( gamecombo.fs_game );
176 gtk_widget_set_sensitive( combo->fsgame_entry , gamecombo.sensitive );
177
178 return FALSE;
179 }
180
181 class MappingMode
182 {
183 public:
184 bool do_mapping_mode;
185 const char* sp_mapping_mode;
186 const char* mp_mapping_mode;
187
188 MappingMode() :
189 do_mapping_mode( !string_empty( g_pGameDescription->getKeyValue( "show_gamemode" ) ) ),
190 sp_mapping_mode( "Single Player mapping mode" ),
191 mp_mapping_mode( "Multiplayer mapping mode" ){
192 }
193 };
194
195 typedef LazyStatic<MappingMode> LazyStaticMappingMode;
196
197 inline MappingMode& globalMappingMode(){
198 return LazyStaticMappingMode::instance();
199 }
200
201 class ProjectSettingsDialog
202 {
203 public:
204 GameCombo game_combo;
205 ui::ComboBox gamemode_combo{ui::null};
206 };
207
208 ui::Window ProjectSettingsDialog_construct( ProjectSettingsDialog& dialog, ModalDialog& modal ){
209 auto window = MainFrame_getWindow().create_dialog_window("Project Settings", G_CALLBACK(dialog_delete_callback ), &modal );
210
211 {
212 auto table1 = create_dialog_table( 1, 2, 4, 4, 4 );
213 window.add(table1);
214 {
215 auto vbox = create_dialog_vbox( 4 );
216 table1.attach(vbox, {1, 2, 0, 1}, {GTK_FILL, GTK_FILL});
217 {
218 auto button = create_dialog_button( "OK", G_CALLBACK( dialog_button_ok ), &modal );
219 vbox.pack_start( button, FALSE, FALSE, 0 );
220 }
221 {
222 auto button = create_dialog_button( "Cancel", G_CALLBACK( dialog_button_cancel ), &modal );
223 vbox.pack_start( button, FALSE, FALSE, 0 );
224 }
225 }
226 {
227 auto frame = create_dialog_frame( "Project settings" );
228 table1.attach(frame, {0, 1, 0, 1}, {GTK_EXPAND | GTK_FILL, GTK_FILL});
229 {
230 auto table2 = create_dialog_table( ( globalMappingMode().do_mapping_mode ) ? 4 : 3, 2, 4, 4, 4 );
231 frame.add(table2);
232
233 {
234 auto label = ui::Label( "Select mod" );
235 label.show();
236 table2.attach(label, {0, 1, 0, 1}, {GTK_FILL, 0});
237 gtk_misc_set_alignment( GTK_MISC( label ), 1, 0.5 );
238 }
239 {
240 dialog.game_combo.game_select = ui::ComboBoxText(ui::New);
241
242 gtk_combo_box_text_append_text( dialog.game_combo.game_select, globalGameComboConfiguration().basegame );
243 if ( globalGameComboConfiguration().known[0] != '\0' ) {
244 gtk_combo_box_text_append_text( dialog.game_combo.game_select, globalGameComboConfiguration().known );
245 }
246 gtk_combo_box_text_append_text( dialog.game_combo.game_select, globalGameComboConfiguration().custom );
247
248 dialog.game_combo.game_select.show();
249 table2.attach(dialog.game_combo.game_select, {1, 2, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
250
251 dialog.game_combo.game_select.connect( "changed", G_CALLBACK( OnSelchangeComboWhatgame ), &dialog.game_combo );
252 }
253
254 {
255 auto label = ui::Label( "fs_game" );
256 label.show();
257 table2.attach(label, {0, 1, 1, 2}, {GTK_FILL, 0});
258 gtk_misc_set_alignment( GTK_MISC( label ), 1, 0.5 );
259 }
260 {
261 auto entry = ui::Entry(ui::New);
262 entry.show();
263 table2.attach(entry, {1, 2, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
264
265 dialog.game_combo.fsgame_entry = entry;
266 }
267
268 if ( globalMappingMode().do_mapping_mode ) {
269 auto label = ui::Label( "Mapping mode" );
270 label.show();
271 table2.attach(label, {0, 1, 3, 4}, {GTK_FILL, 0});
272 gtk_misc_set_alignment( GTK_MISC( label ), 1, 0.5 );
273
274 auto combo = ui::ComboBoxText(ui::New);
275 gtk_combo_box_text_append_text( combo, globalMappingMode().sp_mapping_mode );
276 gtk_combo_box_text_append_text( combo, globalMappingMode().mp_mapping_mode );
277
278 combo.show();
279 table2.attach(combo, {1, 2, 3, 4}, {GTK_EXPAND | GTK_FILL, 0});
280
281 dialog.gamemode_combo = combo;
282 }
283 }
284 }
285 }
286
287 // initialise the fs_game selection from the project settings into the dialog
288 const char* dir = gamename_get();
289 gamecombo_t gamecombo = gamecombo_for_dir( dir );
290
291 gtk_combo_box_set_active( dialog.game_combo.game_select, gamecombo.game );
292 dialog.game_combo.fsgame_entry.text( gamecombo.fs_game );
293 gtk_widget_set_sensitive( dialog.game_combo.fsgame_entry , gamecombo.sensitive );
294
295 if ( globalMappingMode().do_mapping_mode ) {
296 const char *gamemode = gamemode_get();
297 if ( string_empty( gamemode ) || string_equal( gamemode, "sp" ) ) {
298 gtk_combo_box_set_active( dialog.gamemode_combo, 0 );
299 }
300 else
301 {
302 gtk_combo_box_set_active( dialog.gamemode_combo, 1 );
303 }
304 }
305
306 return window;
307 }
308
309 void ProjectSettingsDialog_ok( ProjectSettingsDialog& dialog ){
310 const char* dir = gtk_entry_get_text( dialog.game_combo.fsgame_entry );
311
312 const char* new_gamename = path_equal( dir, globalGameComboConfiguration().basegame_dir )
313 ? ""
314 : dir;
315
316 if ( !path_equal( new_gamename, gamename_get() ) ) {
317 ScopeDisableScreenUpdates disableScreenUpdates( "Processing...", "Changing Game Name" );
318
319 EnginePath_Unrealise();
320
321 gamename_set( new_gamename );
322
323 EnginePath_Realise();
324 }
325
326 if ( globalMappingMode().do_mapping_mode ) {
327 // read from gamemode_combo
328 int active = gtk_combo_box_get_active( dialog.gamemode_combo );
329 if ( active == -1 || active == 0 ) {
330 gamemode_set( "sp" );
331 }
332 else
333 {
334 gamemode_set( "mp" );
335 }
336 }
337 }
338
339 void DoProjectSettings(){
340 //if ( ConfirmModified( "Edit Project Settings" ) ) {
341 ModalDialog modal;
342 ProjectSettingsDialog dialog;
343
344 ui::Window window = ProjectSettingsDialog_construct( dialog, modal );
345
346 if ( modal_dialog_show( window, modal ) == eIDOK ) {
347 ProjectSettingsDialog_ok( dialog );
348 }
349
350 window.destroy();
351 //}
352 }
353
354 // =============================================================================
355 // Arbitrary Sides dialog
356
357 void DoSides( int type, int axis ){
358 ModalDialog dialog;
359 //GtkEntry* sides_entry;
360 GtkWidget* sides_spin;
361
362 auto window = MainFrame_getWindow().create_dialog_window("Arbitrary sides", G_CALLBACK(dialog_delete_callback ), &dialog );
363
364 auto accel = ui::AccelGroup(ui::New);
365 window.add_accel_group( accel );
366
367 {
368 auto hbox = create_dialog_hbox( 4, 4 );
369 window.add(hbox);
370 {
371 auto label = ui::Label( "Sides:" );
372 label.show();
373 hbox.pack_start( label, FALSE, FALSE, 0 );
374 }
375 // {
376 // auto entry = sides_entry;
377 // entry.show();
378 // hbox.pack_start( entry, FALSE, FALSE, 0 );
379 // gtk_widget_grab_focus( entry );
380 // }
381 {
382 GtkAdjustment* adj;
383 EBrushPrefab BrushPrefabType = (EBrushPrefab)type;
384 switch ( BrushPrefabType )
385 {
386 case eBrushPrism :
387 case eBrushCone :
388 adj = GTK_ADJUSTMENT( gtk_adjustment_new( 8, 3, 1022, 1, 10, 0 ) );
389 break;
390 case eBrushSphere :
391 adj = GTK_ADJUSTMENT( gtk_adjustment_new( 8, 3, 31, 1, 10, 0 ) );
392 break;
393 case eBrushRock :
394 adj = GTK_ADJUSTMENT( gtk_adjustment_new( 32, 10, 1000, 1, 10, 0 ) );
395 break;
396 default:
397 adj = GTK_ADJUSTMENT( gtk_adjustment_new( 8, 3, 31, 1, 10, 0 ) );
398 break;
399 }
400
401 GtkWidget* spin = gtk_spin_button_new( adj, 1, 0 );
402 gtk_widget_show( spin );
403 gtk_box_pack_start( GTK_BOX( hbox ), spin, FALSE, FALSE, 0 );
404 gtk_widget_set_size_request( spin, 64, -1 );
405 gtk_spin_button_set_numeric( GTK_SPIN_BUTTON( spin ), TRUE );
406
407 sides_spin = spin;
408 }
409 {
410 auto vbox = create_dialog_vbox( 4 );
411 hbox.pack_start( vbox, TRUE, TRUE, 0 );
412 {
413 auto button = create_dialog_button( "OK", G_CALLBACK( dialog_button_ok ), &dialog );
414 vbox.pack_start( button, FALSE, FALSE, 0 );
415 widget_make_default( button );
416 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
417 }
418 {
419 auto button = create_dialog_button( "Cancel", G_CALLBACK( dialog_button_cancel ), &dialog );
420 vbox.pack_start( button, FALSE, FALSE, 0 );
421 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
422 }
423 }
424 }
425
426 if ( modal_dialog_show( window, dialog ) == eIDOK ) {
427 // const char *str = gtk_entry_get_text( sides_entry );
428
429 // Scene_BrushConstructPrefab( GlobalSceneGraph(), (EBrushPrefab)type, atoi( str ), TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
430 gtk_spin_button_update ( GTK_SPIN_BUTTON( sides_spin ) );
431 int sides = static_cast<int>( gtk_spin_button_get_value( GTK_SPIN_BUTTON( sides_spin ) ) );
432 Scene_BrushConstructPrefab( GlobalSceneGraph(), (EBrushPrefab)type, sides, TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ) );
433 }
434
435 window.destroy();
436 }
437
438 // =============================================================================
439 // About dialog (no program is complete without one)
440
441 void about_button_changelog( ui::Widget widget, gpointer data ){
442 StringOutputStream log( 256 );
443 log << "https://gitlab.com/xonotic/netradiant/-/commits/master";
444 OpenURL( log.c_str() );
445 }
446
447 void about_button_credits( ui::Widget widget, gpointer data ){
448 StringOutputStream cred( 256 );
449 cred << "https://gitlab.com/xonotic/netradiant/-/graphs/master";
450 OpenURL( cred.c_str() );
451 }
452
453 void about_button_issues( ui::Widget widget, gpointer data ){
454 StringOutputStream cred( 256 );
455 cred << "https://gitlab.com/xonotic/netradiant/-/issues";
456 OpenURL( cred.c_str() );
457 }
458
459 static void AddParagraph( ui::VBox vbox, const char* text, bool use_markup ){
460 auto label = ui::Label( text );
461 gtk_label_set_use_markup( GTK_LABEL( label ), use_markup );
462 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0 );
463 gtk_label_set_justify( label, GTK_JUSTIFY_LEFT );
464 label.show();
465 vbox.pack_start( label, TRUE, TRUE, 0 );
466 }
467
468 static void AddParagraph( ui::VBox vbox, const char* text ){
469 AddParagraph( vbox, text, false );
470 }
471
472 void DoAbout(){
473 ModalDialog dialog;
474 ModalDialogButton ok_button( dialog, eIDOK );
475
476 auto window = MainFrame_getWindow().create_modal_dialog_window("About " RADIANT_NAME, dialog );
477
478 {
479 auto vbox = create_dialog_vbox( 4, 4 );
480 window.add(vbox);
481
482 {
483 auto hbox = create_dialog_hbox( 4 );
484 vbox.pack_start( hbox, FALSE, FALSE, 0 );
485
486 {
487 auto vbox2 = create_dialog_vbox( 4 );
488 hbox.pack_start( vbox2, FALSE, FALSE, 5 );
489 {
490 auto frame = create_dialog_frame( 0, ui::Shadow::IN );
491 vbox2.pack_start( frame, FALSE, FALSE, 0 );
492 {
493 auto image = new_local_image( "logo.png" );
494 image.show();
495 frame.add(image);
496 }
497 }
498 }
499
500 {
501 // HACK: that may not be related to font size
502 auto about_vbox = ui::VBox( FALSE, 5 );
503 about_vbox.show();
504 hbox.pack_start( about_vbox, FALSE, FALSE, 0 );
505
506 AddParagraph( about_vbox,
507 RADIANT_NAME " " RADIANT_VERSION_STRING " (" __DATE__ ")\n"
508 RADIANT_ABOUTMSG );
509 AddParagraph( about_vbox,
510 "Get news and latest build at "
511 "<a href='https://netradiant.gitlab.io/'>"
512 "netradiant.gitlab.io"
513 "</a>\n"
514 "Please report your issues at "
515 "<a href='https://gitlab.com/xonotic/netradiant/issues'>"
516 "gitlab.com/xonotic/netradiant/issues"
517 "</a>\n"
518 "The team cannot provide support for custom builds.", true );
519 AddParagraph( about_vbox,
520 RADIANT_NAME " is a community project maintained by "
521 "<a href='https://xonotic.org'>"
522 "Xonotic"
523 "</a>\n"
524 "and developed with help from "
525 "<a href='https://netradiant.gitlab.io/page/about/'>"
526 "other game projects"
527 "</a> and individuals. ", true );
528 AddParagraph( about_vbox,
529 "This program is free software licensed under the GNU GPL." );
530 }
531
532 {
533 auto vbox2 = create_dialog_vbox( 4 );
534 hbox.pack_start( vbox2, TRUE, TRUE, 0 );
535 {
536 auto button = create_modal_dialog_button( "OK", ok_button );
537 vbox2.pack_start( button, FALSE, FALSE, 0 );
538 gtk_widget_grab_focus( GTK_WIDGET( button ) );
539 }
540 {
541 auto button = create_dialog_button( "Credits", G_CALLBACK( about_button_credits ), 0 );
542 vbox2.pack_start( button, FALSE, FALSE, 0 );
543 }
544 {
545 auto button = create_dialog_button( "Changes", G_CALLBACK( about_button_changelog ), 0 );
546 vbox2.pack_start( button, FALSE, FALSE, 0 );
547 }
548 {
549 auto button = create_dialog_button( "Issues", G_CALLBACK( about_button_issues ), 0 );
550 vbox2.pack_start( button, FALSE, FALSE, 0 );
551 }
552 }
553 }
554 {
555 auto frame = create_dialog_frame( "OpenGL Properties" );
556 vbox.pack_start( frame, FALSE, FALSE, 0 );
557 {
558 auto table = create_dialog_table( 3, 2, 4, 4, 4 );
559 frame.add(table);
560 {
561 auto label = ui::Label( "Vendor:" );
562 label.show();
563 table.attach(label, {0, 1, 0, 1}, {GTK_FILL, 0});
564 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
565 }
566 {
567 auto label = ui::Label( "Version:" );
568 label.show();
569 table.attach(label, {0, 1, 1, 2}, {GTK_FILL, 0});
570 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
571 }
572 {
573 auto label = ui::Label( "Renderer:" );
574 label.show();
575 table.attach(label, {0, 1, 2, 3}, {GTK_FILL, 0});
576 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
577 }
578 {
579 auto label = ui::Label( reinterpret_cast<const char*>( glGetString( GL_VENDOR ) ) );
580 label.show();
581 table.attach(label, {1, 2, 0, 1}, {GTK_FILL, 0});
582 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
583 }
584 {
585 auto label = ui::Label( reinterpret_cast<const char*>( glGetString( GL_VERSION ) ) );
586 label.show();
587 table.attach(label, {1, 2, 1, 2}, {GTK_FILL, 0});
588 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
589 }
590 {
591 auto label = ui::Label( reinterpret_cast<const char*>( glGetString( GL_RENDERER ) ) );
592 label.show();
593 table.attach(label, {1, 2, 2, 3}, {GTK_FILL, 0});
594 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
595 }
596 }
597 {
598 auto frame = create_dialog_frame( "OpenGL Extensions" );
599 vbox.pack_start( frame, TRUE, TRUE, 0 );
600 {
601 auto sc_extensions = create_scrolled_window( ui::Policy::AUTOMATIC, ui::Policy::ALWAYS, 4 );
602 frame.add(sc_extensions);
603 {
604 auto text_extensions = ui::TextView(ui::New);
605 gtk_text_view_set_editable( text_extensions, FALSE );
606 sc_extensions.add(text_extensions);
607 text_extensions.text(reinterpret_cast<const char *>(glGetString(GL_EXTENSIONS)));
608 gtk_text_view_set_wrap_mode( text_extensions, GTK_WRAP_WORD );
609 text_extensions.show();
610 }
611 }
612 }
613 }
614 }
615
616 modal_dialog_show( window, dialog );
617
618 window.destroy();
619 }
620
621 // =============================================================================
622 // TextureLayout dialog
623
624 // Last used texture scale values
625 static float last_used_texture_layout_scale_x = 4.0;
626 static float last_used_texture_layout_scale_y = 4.0;
627
628 EMessageBoxReturn DoTextureLayout( float *fx, float *fy ){
629 ModalDialog dialog;
630 ModalDialogButton ok_button( dialog, eIDOK );
631 ModalDialogButton cancel_button( dialog, eIDCANCEL );
632 ui::Entry x{ui::null};
633 ui::Entry y{ui::null};
634
635 auto window = MainFrame_getWindow().create_modal_dialog_window("Patch texture layout", dialog );
636
637 auto accel = ui::AccelGroup(ui::New);
638 window.add_accel_group( accel );
639
640 {
641 auto hbox = create_dialog_hbox( 4, 4 );
642 window.add(hbox);
643 {
644 auto vbox = create_dialog_vbox( 4 );
645 hbox.pack_start( vbox, TRUE, TRUE, 0 );
646 {
647 auto label = ui::Label( "Texture will be fit across the patch based\n"
648 "on the x and y values given. Values of 1x1\n"
649 "will \"fit\" the texture. 2x2 will repeat\n"
650 "it twice, etc." );
651 label.show();
652 vbox.pack_start( label, TRUE, TRUE, 0 );
653 gtk_label_set_justify( label, GTK_JUSTIFY_LEFT );
654 }
655 {
656 auto table = create_dialog_table( 2, 2, 4, 4 );
657 table.show();
658 vbox.pack_start( table, TRUE, TRUE, 0 );
659 {
660 auto label = ui::Label( "Texture x:" );
661 label.show();
662 table.attach(label, {0, 1, 0, 1}, {GTK_FILL, 0});
663 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
664 }
665 {
666 auto label = ui::Label( "Texture y:" );
667 label.show();
668 table.attach(label, {0, 1, 1, 2}, {GTK_FILL, 0});
669 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
670 }
671 {
672 auto entry = ui::Entry(ui::New);
673 entry.show();
674 table.attach(entry, {1, 2, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
675
676 x = entry;
677 }
678 {
679 auto entry = ui::Entry(ui::New);
680 entry.show();
681 table.attach(entry, {1, 2, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
682
683 y = entry;
684 }
685 }
686 }
687 {
688 auto vbox = create_dialog_vbox( 4 );
689 hbox.pack_start( vbox, FALSE, FALSE, 0 );
690 {
691 auto button = create_modal_dialog_button( "OK", ok_button );
692 vbox.pack_start( button, FALSE, FALSE, 0 );
693 widget_make_default( button );
694 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
695 }
696 {
697 auto button = create_modal_dialog_button( "Cancel", cancel_button );
698 vbox.pack_start( button, FALSE, FALSE, 0 );
699 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
700 }
701 }
702 }
703
704 // Initialize with last used values
705 char buf[16];
706
707 sprintf( buf, "%f", last_used_texture_layout_scale_x );
708 x.text( buf );
709
710 sprintf( buf, "%f", last_used_texture_layout_scale_y );
711 y.text( buf );
712
713 // Set focus after intializing the values
714 gtk_widget_grab_focus( x );
715
716 EMessageBoxReturn ret = modal_dialog_show( window, dialog );
717 if ( ret == eIDOK ) {
718 *fx = static_cast<float>( atof( gtk_entry_get_text( x ) ) );
719 *fy = static_cast<float>( atof( gtk_entry_get_text( y ) ) );
720
721 // Remember last used values
722 last_used_texture_layout_scale_x = *fx;
723 last_used_texture_layout_scale_y = *fy;
724 }
725
726 window.destroy();
727
728 return ret;
729 }
730
731 // =============================================================================
732 // Text Editor dialog
733
734 // master window widget
735 static ui::Window text_editor{ui::null};
736 static ui::Widget text_widget{ui::null}; // slave, text widget from the gtk editor
737 static GtkTextBuffer* text_buffer_;
738
739 static gint editor_delete( ui::Widget widget, gpointer data ){
740 /* if ( ui::alert( widget.window(), "Close the shader editor ?", "Radiant", ui::alert_type::YESNO, ui::alert_icon::Question ) == ui::alert_response::NO ) {
741 return TRUE;
742 }
743 */
744 text_editor.hide();
745
746 return TRUE;
747 }
748
749 static void editor_save( ui::Widget widget, gpointer data ){
750 FILE *f = fopen( (char*)g_object_get_data( G_OBJECT( data ), "filename" ), "w" );
751 //gpointer text = g_object_get_data( G_OBJECT( data ), "text" );
752
753 if ( f == 0 ) {
754 ui::alert( ui::Widget::from(data).window(), "Error saving file !" );
755 return;
756 }
757
758 /* Obtain iters for the start and end of points of the buffer */
759 GtkTextIter start;
760 GtkTextIter end;
761 gtk_text_buffer_get_start_iter (text_buffer_, &start);
762 gtk_text_buffer_get_end_iter (text_buffer_, &end);
763
764 /* Get the entire buffer text. */
765 char *str = gtk_text_buffer_get_text (text_buffer_, &start, &end, FALSE);
766
767 //char *str = gtk_editable_get_chars( GTK_EDITABLE( text ), 0, -1 );
768 fwrite( str, 1, strlen( str ), f );
769 fclose( f );
770 g_free (str);
771 }
772
773 static void editor_close( ui::Widget widget, gpointer data ){
774 /* if ( ui::alert( text_editor.window(), "Close the shader editor ?", "Radiant", ui::alert_type::YESNO, ui::alert_icon::Question ) == ui::alert_response::NO ) {
775 return;
776 }
777 */
778 text_editor.hide();
779 }
780
781 static void CreateGtkTextEditor(){
782 auto dlg = ui::Window( ui::window_type::TOP );
783
784 dlg.connect( "", G_CALLBACK( editor_delete ), 0 );
785 gtk_window_set_default_size( dlg, 400, 600 );
786
787 auto vbox = ui::VBox( FALSE, 5 );
788 vbox.show();
789 dlg.add(vbox);
790 gtk_container_set_border_width( GTK_CONTAINER( vbox ), 5 );
791
792 auto scr = ui::ScrolledWindow(ui::New);
793 scr.show();
794 vbox.pack_start( scr, TRUE, TRUE, 0 );
795 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scr ), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
796 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW( scr ), GTK_SHADOW_IN );
797
798 auto text = ui::TextView(ui::New);
799 scr.add(text);
800 text.show();
801 g_object_set_data( G_OBJECT( dlg ), "text", (gpointer) text );
802 gtk_text_view_set_editable( text, TRUE );
803
804 auto hbox = ui::HBox( FALSE, 5 );
805 hbox.show();
806 vbox.pack_start( hbox, FALSE, TRUE, 0 );
807
808 auto button = ui::Button( "Close" );
809 button.show();
810 hbox.pack_end(button, FALSE, FALSE, 0);
811 button.connect( "clicked",
812 G_CALLBACK( editor_close ), dlg );
813 button.dimensions(60, -1);
814
815 button = ui::Button( "Save" );
816 button.show();
817 hbox.pack_end(button, FALSE, FALSE, 0);
818 button.connect( "clicked",
819 G_CALLBACK( editor_save ), dlg );
820 button.dimensions(60, -1);
821
822 text_editor = dlg;
823 text_widget = text;
824 }
825
826 static void DoGtkTextEditor( const char* filename, guint cursorpos, int length ){
827 if ( !text_editor ) {
828 CreateGtkTextEditor(); // build it the first time we need it
829
830 }
831 // Load file
832 FILE *f = fopen( filename, "r" );
833
834 if ( f == 0 ) {
835 globalOutputStream() << "Unable to load file " << filename << " in shader editor.\n";
836 text_editor.hide();
837 }
838 else
839 {
840 fseek( f, 0, SEEK_END );
841 int len = ftell( f );
842 void *buf = malloc( len );
843 void *old_filename;
844
845 rewind( f );
846 fread( buf, 1, len, f );
847
848 gtk_window_set_title( text_editor, filename );
849
850 auto text_buffer = gtk_text_view_get_buffer(ui::TextView::from(text_widget));
851 gtk_text_buffer_set_text( text_buffer, (char*)buf, length );
852
853 old_filename = g_object_get_data( G_OBJECT( text_editor ), "filename" );
854 if ( old_filename ) {
855 free( old_filename );
856 }
857 g_object_set_data( G_OBJECT( text_editor ), "filename", strdup( filename ) );
858
859 // trying to show later
860 text_editor.show();
861 gtk_window_present( GTK_WINDOW( text_editor ) );
862
863 //#if GDEF_OS_WINDOWS
864 ui::process();
865 //#endif
866
867 // only move the cursor if it's not exceeding the size..
868 // NOTE: this is erroneous, cursorpos is the offset in bytes, not in characters
869 // len is the max size in bytes, not in characters either, but the character count is below that limit..
870 // thinking .. the difference between character count and byte count would be only because of CR/LF?
871 {
872 GtkTextIter text_iter;
873 // character offset, not byte offset
874 gtk_text_buffer_get_iter_at_offset( text_buffer, &text_iter, cursorpos );
875 gtk_text_buffer_place_cursor( text_buffer, &text_iter );
876 gtk_text_view_scroll_to_iter( GTK_TEXT_VIEW( text_widget ), &text_iter, 0, TRUE, 0, 0);
877 }
878
879 //#if GDEF_OS_WINDOWS
880 gtk_widget_queue_draw( text_widget );
881 //#endif
882
883 text_buffer_ = text_buffer;
884 free( buf );
885 fclose( f );
886 }
887 }
888
889 // =============================================================================
890 // Light Intensity dialog
891
892 EMessageBoxReturn DoLightIntensityDlg( int *intensity ){
893 ModalDialog dialog;
894 ui::Entry intensity_entry{ui::null};
895 ModalDialogButton ok_button( dialog, eIDOK );
896 ModalDialogButton cancel_button( dialog, eIDCANCEL );
897
898 ui::Window window = MainFrame_getWindow().create_modal_dialog_window("Light intensity", dialog, -1, -1 );
899
900 auto accel_group = ui::AccelGroup(ui::New);
901 window.add_accel_group( accel_group );
902
903 {
904 auto hbox = create_dialog_hbox( 4, 4 );
905 window.add(hbox);
906 {
907 auto vbox = create_dialog_vbox( 4 );
908 hbox.pack_start( vbox, TRUE, TRUE, 0 );
909 {
910 auto label = ui::Label( "ESC for default, ENTER to validate" );
911 label.show();
912 vbox.pack_start( label, FALSE, FALSE, 0 );
913 }
914 {
915 auto entry = ui::Entry(ui::New);
916 entry.show();
917 vbox.pack_start( entry, TRUE, TRUE, 0 );
918
919 gtk_widget_grab_focus( entry );
920
921 intensity_entry = entry;
922 }
923 }
924 {
925 auto vbox = create_dialog_vbox( 4 );
926 hbox.pack_start( vbox, FALSE, FALSE, 0 );
927
928 {
929 auto button = create_modal_dialog_button( "OK", ok_button );
930 vbox.pack_start( button, FALSE, FALSE, 0 );
931 widget_make_default( button );
932 gtk_widget_add_accelerator( button , "clicked", accel_group, GDK_KEY_Return, (GdkModifierType)0, GTK_ACCEL_VISIBLE );
933 }
934 {
935 auto button = create_modal_dialog_button( "Cancel", cancel_button );
936 vbox.pack_start( button, FALSE, FALSE, 0 );
937 gtk_widget_add_accelerator( button , "clicked", accel_group, GDK_KEY_Escape, (GdkModifierType)0, GTK_ACCEL_VISIBLE );
938 }
939 }
940 }
941
942 char buf[16];
943 sprintf( buf, "%d", *intensity );
944 intensity_entry.text(buf);
945
946 EMessageBoxReturn ret = modal_dialog_show( window, dialog );
947 if ( ret == eIDOK ) {
948 *intensity = atoi( gtk_entry_get_text( intensity_entry ) );
949 }
950
951 window.destroy();
952
953 return ret;
954 }
955
956 // =============================================================================
957 // Add new shader tag dialog
958
959 EMessageBoxReturn DoShaderTagDlg( CopiedString* tag, const char* title ){
960 ModalDialog dialog;
961 ModalDialogButton ok_button( dialog, eIDOK );
962 ModalDialogButton cancel_button( dialog, eIDCANCEL );
963
964 auto window = MainFrame_getWindow().create_modal_dialog_window(title, dialog, -1, -1 );
965
966 auto accel_group = ui::AccelGroup(ui::New);
967 window.add_accel_group( accel_group );
968
969 auto textentry = ui::Entry(ui::New);
970 {
971 auto hbox = create_dialog_hbox( 4, 4 );
972 window.add(hbox);
973 {
974 auto vbox = create_dialog_vbox( 4 );
975 hbox.pack_start( vbox, TRUE, TRUE, 0 );
976 {
977 //GtkLabel* label = GTK_LABEL(gtk_label_new("Enter one ore more tags separated by spaces"));
978 auto label = ui::Label( "ESC to cancel, ENTER to validate" );
979 label.show();
980 vbox.pack_start( label, FALSE, FALSE, 0 );
981 }
982 {
983 auto entry = textentry;
984 entry.show();
985 vbox.pack_start( entry, TRUE, TRUE, 0 );
986
987 gtk_widget_grab_focus( entry );
988 }
989 }
990 {
991 auto vbox = create_dialog_vbox( 4 );
992 hbox.pack_start( vbox, FALSE, FALSE, 0 );
993
994 {
995 auto button = create_modal_dialog_button( "OK", ok_button );
996 vbox.pack_start( button, FALSE, FALSE, 0 );
997 widget_make_default( button );
998 gtk_widget_add_accelerator( button , "clicked", accel_group, GDK_KEY_Return, (GdkModifierType)0, GTK_ACCEL_VISIBLE );
999 }
1000 {
1001 auto button = create_modal_dialog_button( "Cancel", cancel_button );
1002 vbox.pack_start( button, FALSE, FALSE, 0 );
1003 gtk_widget_add_accelerator( button , "clicked", accel_group, GDK_KEY_Escape, (GdkModifierType)0, GTK_ACCEL_VISIBLE );
1004 }
1005 }
1006 }
1007
1008 EMessageBoxReturn ret = modal_dialog_show( window, dialog );
1009 if ( ret == eIDOK ) {
1010 *tag = gtk_entry_get_text( textentry );
1011 }
1012
1013 window.destroy();
1014
1015 return ret;
1016 }
1017
1018 EMessageBoxReturn DoShaderInfoDlg( const char* name, const char* filename, const char* title ){
1019 ModalDialog dialog;
1020 ModalDialogButton ok_button( dialog, eIDOK );
1021
1022 auto window = MainFrame_getWindow().create_modal_dialog_window(title, dialog, -1, -1 );
1023
1024 auto accel_group = ui::AccelGroup(ui::New);
1025 window.add_accel_group( accel_group );
1026
1027 {
1028 auto hbox = create_dialog_hbox( 4, 4 );
1029 window.add(hbox);
1030 {
1031 auto vbox = create_dialog_vbox( 4 );
1032 hbox.pack_start( vbox, FALSE, FALSE, 0 );
1033 {
1034 auto label = ui::Label( "The selected shader" );
1035 label.show();
1036 vbox.pack_start( label, FALSE, FALSE, 0 );
1037 }
1038 {
1039 auto label = ui::Label( name );
1040 label.show();
1041 vbox.pack_start( label, FALSE, FALSE, 0 );
1042 }
1043 {
1044 auto label = ui::Label( "is located in file" );
1045 label.show();
1046 vbox.pack_start( label, FALSE, FALSE, 0 );
1047 }
1048 {
1049 auto label = ui::Label( filename );
1050 label.show();
1051 vbox.pack_start( label, FALSE, FALSE, 0 );
1052 }
1053 {
1054 auto button = create_modal_dialog_button( "OK", ok_button );
1055 vbox.pack_start( button, FALSE, FALSE, 0 );
1056 widget_make_default( button );
1057 gtk_widget_add_accelerator( button , "clicked", accel_group, GDK_KEY_Return, (GdkModifierType)0, GTK_ACCEL_VISIBLE );
1058 }
1059 }
1060 }
1061
1062 EMessageBoxReturn ret = modal_dialog_show( window, dialog );
1063
1064 window.destroy();
1065
1066 return ret;
1067 }
1068
1069
1070
1071 #if GDEF_OS_WINDOWS
1072 #include <gdk/gdkwin32.h>
1073 #endif
1074
1075 CopiedString g_TextEditor_editorCommand( "" );
1076
1077 void DoTextEditor( const char* filename, int cursorpos, int length, bool external_editor ){
1078 //StringOutputStream paths[4]( 256 );
1079 StringOutputStream paths[4] = { StringOutputStream(256) };
1080 StringOutputStream* goodpath = 0;
1081
1082 const char* gamename = GlobalRadiant().getGameName();
1083 const char* basegame = GlobalRadiant().getRequiredGameDescriptionKeyValue( "basegame" );
1084 const char* enginePath = GlobalRadiant().getEnginePath();
1085 const char* homeEnginePath = g_qeglobals.m_userEnginePath.c_str();
1086
1087 paths[0] << homeEnginePath << gamename << '/' << filename;
1088 paths[1] << enginePath << gamename << '/' << filename;
1089 paths[2] << homeEnginePath << basegame << '/' << filename;
1090 paths[3] << enginePath << basegame << '/' << filename;
1091
1092 for ( std::size_t i = 0; i < 4; ++i ){
1093 if ( file_exists( paths[i].c_str() ) ){
1094 goodpath = &paths[i];
1095 break;
1096 }
1097 }
1098
1099 if( goodpath ){
1100 globalOutputStream() << "opening file '" << goodpath->c_str() << "' (line " << cursorpos << " info ignored)\n";
1101 if( external_editor ){
1102 if( g_TextEditor_editorCommand.empty() ){
1103 #ifdef WIN32
1104 ShellExecute( (HWND)GDK_WINDOW_HWND( GTK_WIDGET( MainFrame_getWindow() )->window ), 0, goodpath->c_str(), 0, 0, SW_SHOWNORMAL );
1105 // SHELLEXECUTEINFO ShExecInfo;
1106 // ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
1107 // ShExecInfo.fMask = 0;
1108 // ShExecInfo.hwnd = (HWND)GDK_WINDOW_HWND( GTK_WIDGET( MainFrame_getWindow() )->window );
1109 // ShExecInfo.lpVerb = NULL;
1110 // ShExecInfo.lpFile = goodpath->c_str();
1111 // ShExecInfo.lpParameters = NULL;
1112 // ShExecInfo.lpDirectory = NULL;
1113 // ShExecInfo.nShow = SW_SHOWNORMAL;
1114 // ShExecInfo.hInstApp = NULL;
1115 // ShellExecuteEx(&ShExecInfo);
1116 #else
1117 globalOutputStream() << "Failed to open '" << goodpath->c_str() << "'\nSet Shader Editor Command in preferences\n";
1118 #endif
1119 }
1120 else{
1121 StringOutputStream strEditCommand( 256 );
1122 strEditCommand << g_TextEditor_editorCommand.c_str() << " \"" << goodpath->c_str() << "\"";
1123
1124 globalOutputStream() << "Launching: " << strEditCommand.c_str() << "\n";
1125 // note: linux does not return false if the command failed so it will assume success
1126 if ( Q_Exec( 0, const_cast<char*>( strEditCommand.c_str() ), 0, true, false ) == false ) {
1127 globalOutputStream() << "Failed to execute " << strEditCommand.c_str() << "\n";
1128 }
1129 else
1130 {
1131 // the command (appeared) to run successfully, no need to do anything more
1132 return;
1133 }
1134 }
1135 }
1136 else{
1137 DoGtkTextEditor( goodpath->c_str(), cursorpos, length );
1138 }
1139 }
1140 else{
1141 globalOutputStream() << "Failed to open '" << filename << "'\nOne sits in .pk3 most likely!\n";
1142 }
1143 }