]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/gtkmisc.cpp
Merge commit '830125fad042fad35dc029b6eb57c8156ad7e176'
[xonotic/netradiant.git] / radiant / gtkmisc.cpp
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 // Small functions to help with GTK
33 //
34
35 #include "gtkmisc.h"
36
37 #include <gtk/gtkcolorseldialog.h>
38 #include <gtk/gtkentry.h>
39
40 #include "math/vector.h"
41 #include "os/path.h"
42
43 #include "gtkutil/dialog.h"
44 #include "gtkutil/filechooser.h"
45 #include "gtkutil/menu.h"
46 #include "gtkutil/toolbar.h"
47 #include "commands.h"
48
49
50 // =============================================================================
51 // Misc stuff
52
53 void command_connect_accelerator( const char* name ){
54         const Command& command = GlobalCommands_find( name );
55         GlobalShortcuts_register( name, 1 );
56         global_accel_group_connect( command.m_accelerator, command.m_callback );
57 }
58
59 void command_disconnect_accelerator( const char* name ){
60         const Command& command = GlobalCommands_find( name );
61         global_accel_group_disconnect( command.m_accelerator, command.m_callback );
62 }
63
64 void toggle_add_accelerator( const char* name ){
65         const Toggle& toggle = GlobalToggles_find( name );
66         GlobalShortcuts_register( name, 2 );
67         global_accel_group_connect( toggle.m_command.m_accelerator, toggle.m_command.m_callback );
68 }
69
70 void toggle_remove_accelerator( const char* name ){
71         const Toggle& toggle = GlobalToggles_find( name );
72         global_accel_group_disconnect( toggle.m_command.m_accelerator, toggle.m_command.m_callback );
73 }
74
75 GtkCheckMenuItem* create_check_menu_item_with_mnemonic( GtkMenu* menu, const char* mnemonic, const char* commandName ){
76         GlobalShortcuts_register( commandName, 2 );
77         const Toggle& toggle = GlobalToggles_find( commandName );
78         global_accel_group_connect( toggle.m_command.m_accelerator, toggle.m_command.m_callback );
79         return create_check_menu_item_with_mnemonic( menu, mnemonic, toggle );
80 }
81
82 GtkMenuItem* create_menu_item_with_mnemonic( GtkMenu* menu, const char *mnemonic, const char* commandName ){
83         GlobalShortcuts_register( commandName, 1 );
84         const Command& command = GlobalCommands_find( commandName );
85         global_accel_group_connect( command.m_accelerator, command.m_callback );
86         return create_menu_item_with_mnemonic( menu, mnemonic, command );
87 }
88
89 GtkButton* toolbar_append_button( GtkToolbar* toolbar, const char* description, const char* icon, const char* commandName ){
90         return toolbar_append_button( toolbar, description, icon, GlobalCommands_find( commandName ) );
91 }
92
93 GtkToggleButton* toolbar_append_toggle_button( GtkToolbar* toolbar, const char* description, const char* icon, const char* commandName ){
94         return toolbar_append_toggle_button( toolbar, description, icon, GlobalToggles_find( commandName ) );
95 }
96
97 // =============================================================================
98 // File dialog
99
100 bool color_dialog( GtkWidget *parent, Vector3& color, const char* title ){
101         GtkWidget* dlg;
102         double clr[3];
103         ModalDialog dialog;
104
105         clr[0] = color[0];
106         clr[1] = color[1];
107         clr[2] = color[2];
108
109         dlg = gtk_color_selection_dialog_new( title );
110         gtk_color_selection_set_color( GTK_COLOR_SELECTION( GTK_COLOR_SELECTION_DIALOG( dlg )->colorsel ), clr );
111         g_signal_connect( G_OBJECT( dlg ), "delete_event", G_CALLBACK( dialog_delete_callback ), &dialog );
112         g_signal_connect( G_OBJECT( GTK_COLOR_SELECTION_DIALOG( dlg )->ok_button ), "clicked", G_CALLBACK( dialog_button_ok ), &dialog );
113         g_signal_connect( G_OBJECT( GTK_COLOR_SELECTION_DIALOG( dlg )->cancel_button ), "clicked", G_CALLBACK( dialog_button_cancel ), &dialog );
114
115         if ( parent != 0 ) {
116                 gtk_window_set_transient_for( GTK_WINDOW( dlg ), GTK_WINDOW( parent ) );
117         }
118
119         bool ok = modal_dialog_show( GTK_WINDOW( dlg ), dialog ) == eIDOK;
120         if ( ok ) {
121                 GdkColor gdkcolor;
122                 gtk_color_selection_get_current_color( GTK_COLOR_SELECTION( GTK_COLOR_SELECTION_DIALOG( dlg )->colorsel ), &gdkcolor );
123                 clr[0] = gdkcolor.red / 65535.0;
124                 clr[1] = gdkcolor.green / 65535.0;
125                 clr[2] = gdkcolor.blue / 65535.0;
126
127                 color[0] = (float)clr[0];
128                 color[1] = (float)clr[1];
129                 color[2] = (float)clr[2];
130         }
131
132         gtk_widget_destroy( dlg );
133
134         return ok;
135 }
136
137 void button_clicked_entry_browse_file( GtkWidget* widget, GtkEntry* entry ){
138         const char *filename = file_dialog( gtk_widget_get_toplevel( widget ), TRUE, "Choose File", gtk_entry_get_text( entry ) );
139
140         if ( filename != 0 ) {
141                 gtk_entry_set_text( entry, filename );
142         }
143 }
144
145 void button_clicked_entry_browse_directory( GtkWidget* widget, GtkEntry* entry ){
146         const char* text = gtk_entry_get_text( entry );
147         char *dir = dir_dialog( gtk_widget_get_toplevel( widget ), "Choose Directory", path_is_absolute( text ) ? text : "" );
148
149         if ( dir != 0 ) {
150                 gchar* converted = g_filename_to_utf8( dir, -1, 0, 0, 0 );
151                 gtk_entry_set_text( entry, converted );
152                 g_free( dir );
153                 g_free( converted );
154         }
155 }