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