2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
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.
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.
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
25 #include "debugging/debugging.h"
29 #include "string/string.h"
30 #include "versionlib.h"
31 #include "gtkutil/messagebox.h"
34 #define NETRADIANT_CUSTOM_FULLY_MERGED 0
35 #if NETRADIANT_CUSTOM_FULLY_MERGED
36 // For deleting old shortcuts.ini file
37 #include "preferences.h"
39 #endif // NETRADIANT_CUSTOM_FULLY_MERGED
41 typedef std::pair<Accelerator, int> ShortcutValue; // accelerator, isRegistered
42 typedef std::map<CopiedString, ShortcutValue> Shortcuts;
44 void Shortcuts_foreach( Shortcuts& shortcuts, CommandVisitor& visitor ){
45 for ( Shortcuts::iterator i = shortcuts.begin(); i != shortcuts.end(); ++i )
47 visitor.visit( ( *i ).first.c_str(), ( *i ).second.first );
51 Shortcuts g_shortcuts;
53 const Accelerator& GlobalShortcuts_insert( const char* name, const Accelerator& accelerator ){
54 return ( *g_shortcuts.insert( Shortcuts::value_type( name, ShortcutValue( accelerator, false ) ) ).first ).second.first;
57 void GlobalShortcuts_foreach( CommandVisitor& visitor ){
58 Shortcuts_foreach( g_shortcuts, visitor );
61 void GlobalShortcuts_register( const char* name, int type ){
62 Shortcuts::iterator i = g_shortcuts.find( name );
63 if ( i != g_shortcuts.end() ) {
64 ( *i ).second.second = type;
68 void GlobalShortcuts_reportUnregistered(){
69 for ( Shortcuts::iterator i = g_shortcuts.begin(); i != g_shortcuts.end(); ++i )
71 if ( ( *i ).second.first.key != 0 && !( *i ).second.second ) {
72 globalOutputStream() << "shortcut not registered: " << ( *i ).first.c_str() << "\n";
77 typedef std::map<CopiedString, Command> Commands;
81 void GlobalCommands_insert( const char* name, const Callback<void()>& callback, const Accelerator& accelerator ){
82 bool added = g_commands.insert( Commands::value_type( name, Command( callback, GlobalShortcuts_insert( name, accelerator ) ) ) ).second;
83 ASSERT_MESSAGE( added, "command already registered: " << makeQuoted( name ) );
86 const Command& GlobalCommands_find( const char* command ){
87 Commands::iterator i = g_commands.find( command );
88 ASSERT_MESSAGE( i != g_commands.end(), "failed to lookup command " << makeQuoted( command ) );
92 typedef std::map<CopiedString, Toggle> Toggles;
97 void GlobalToggles_insert( const char* name, const Callback<void()>& callback, const Callback<void(const Callback<void(bool)> &)>& exportCallback, const Accelerator& accelerator ){
98 bool added = g_toggles.insert( Toggles::value_type( name, Toggle( callback, GlobalShortcuts_insert( name, accelerator ), exportCallback ) ) ).second;
99 ASSERT_MESSAGE( added, "toggle already registered: " << makeQuoted( name ) );
101 const Toggle& GlobalToggles_find( const char* name ){
102 Toggles::iterator i = g_toggles.find( name );
103 ASSERT_MESSAGE( i != g_toggles.end(), "failed to lookup toggle " << makeQuoted( name ) );
104 return ( *i ).second;
107 typedef std::map<CopiedString, KeyEvent> KeyEvents;
110 KeyEvents g_keyEvents;
112 void GlobalKeyEvents_insert( const char* name, const Accelerator& accelerator, const Callback<void()>& keyDown, const Callback<void()>& keyUp ){
113 bool added = g_keyEvents.insert( KeyEvents::value_type( name, KeyEvent( GlobalShortcuts_insert( name, accelerator ), keyDown, keyUp ) ) ).second;
114 ASSERT_MESSAGE( added, "command already registered: " << makeQuoted( name ) );
116 const KeyEvent& GlobalKeyEvents_find( const char* name ){
117 KeyEvents::iterator i = g_keyEvents.find( name );
118 ASSERT_MESSAGE( i != g_keyEvents.end(), "failed to lookup keyEvent " << makeQuoted( name ) );
119 return ( *i ).second;
123 void disconnect_accelerator( const char *name ){
124 Shortcuts::iterator i = g_shortcuts.find( name );
125 if ( i != g_shortcuts.end() ) {
126 switch ( ( *i ).second.second )
130 command_disconnect_accelerator( name );
134 toggle_remove_accelerator( name );
140 void connect_accelerator( const char *name ){
141 Shortcuts::iterator i = g_shortcuts.find( name );
142 if ( i != g_shortcuts.end() ) {
143 switch ( ( *i ).second.second )
147 command_connect_accelerator( name );
151 toggle_add_accelerator( name );
158 #include <uilib/uilib.h>
159 #include <gdk/gdkkeysyms.h>
161 #include "gtkutil/dialog.h"
162 #include "mainframe.h"
164 #include "stream/textfilestream.h"
165 #include "stream/stringstream.h"
168 struct command_list_dialog_t : public ModalDialog
170 command_list_dialog_t()
171 : m_close_button( *this, eIDCANCEL ), m_list( ui::null ), m_command_iter(), m_model( ui::null ), m_waiting_for_key( false ){
173 ModalDialogButton m_close_button;
176 GtkTreeIter m_command_iter;
177 ui::TreeModel m_model;
178 bool m_waiting_for_key;
181 void accelerator_clear_button_clicked( ui::Button btn, gpointer dialogptr ){
182 command_list_dialog_t &dialog = *(command_list_dialog_t *) dialogptr;
184 if ( dialog.m_waiting_for_key ) {
185 // just unhighlight, user wanted to cancel
186 dialog.m_waiting_for_key = false;
187 gtk_list_store_set( ui::ListStore::from( dialog.m_model ), &dialog.m_command_iter, 2, false, -1 );
188 gtk_widget_set_sensitive( dialog.m_list , true );
189 dialog.m_model = ui::TreeModel(ui::null);
193 auto sel = gtk_tree_view_get_selection( dialog.m_list );
196 if ( !gtk_tree_selection_get_selected( sel, &model, &iter ) ) {
201 memset( &val, 0, sizeof( val ) );
202 gtk_tree_model_get_value(model, &iter, 0, &val );
203 const char *commandName = g_value_get_string( &val );;
205 // clear the ACTUAL accelerator too!
206 disconnect_accelerator( commandName );
208 Shortcuts::iterator thisShortcutIterator = g_shortcuts.find( commandName );
209 if ( thisShortcutIterator == g_shortcuts.end() ) {
212 thisShortcutIterator->second.first = accelerator_null();
214 gtk_list_store_set( ui::ListStore::from( model ), &iter, 1, "", -1 );
216 g_value_unset( &val );
219 void accelerator_edit_button_clicked( ui::Button btn, gpointer dialogptr ){
220 command_list_dialog_t &dialog = *(command_list_dialog_t *) dialogptr;
222 // 1. find selected row
223 auto sel = gtk_tree_view_get_selection( dialog.m_list );
226 if ( !gtk_tree_selection_get_selected( sel, &model, &iter ) ) {
229 if ( dialog.m_waiting_for_key ) {
230 // unhighlight highlit
231 dialog.m_waiting_for_key = false;
232 gtk_list_store_set( GTK_LIST_STORE( dialog.m_model ), &dialog.m_command_iter, 2, false, -1 );
234 dialog.m_command_iter = iter;
235 dialog.m_model = ui::TreeModel::from(model);
237 // 2. disallow changing the row
238 //gtk_widget_set_sensitive(dialog.m_list, false);
240 // 3. highlight the row
241 gtk_list_store_set( ui::ListStore::from( model ), &iter, 2, true, -1 );
243 // 4. grab keyboard focus
244 dialog.m_waiting_for_key = true;
247 gboolean accelerator_tree_butt_press( GtkWidget* widget, GdkEventButton* event, gpointer dialogptr ){
248 if ( event->type == GDK_2BUTTON_PRESS && event->button == 1 ) {
249 accelerator_edit_button_clicked( ui::Button( ui::null ), dialogptr );
255 bool accelerator_window_key_press( ui::Window widget, GdkEventKey *event, gpointer dialogptr ){
256 command_list_dialog_t &dialog = *(command_list_dialog_t *) dialogptr;
258 if ( !dialog.m_waiting_for_key ) {
263 if ( event->is_modifier ) {
267 switch ( event->keyval )
269 case GDK_KEY_Shift_L:
270 case GDK_KEY_Shift_R:
271 case GDK_KEY_Control_L:
272 case GDK_KEY_Control_R:
273 case GDK_KEY_Caps_Lock:
274 case GDK_KEY_Shift_Lock:
279 case GDK_KEY_Super_L:
280 case GDK_KEY_Super_R:
281 case GDK_KEY_Hyper_L:
282 case GDK_KEY_Hyper_R:
287 dialog.m_waiting_for_key = false;
289 // 7. find the name of the accelerator
291 memset( &val, 0, sizeof( val ) );
292 gtk_tree_model_get_value(dialog.m_model, &dialog.m_command_iter, 0, &val );
293 const char *commandName = g_value_get_string( &val );;
294 Shortcuts::iterator thisShortcutIterator = g_shortcuts.find( commandName );
295 if ( thisShortcutIterator == g_shortcuts.end() ) {
296 gtk_list_store_set( ui::ListStore::from( dialog.m_model ), &dialog.m_command_iter, 2, false, -1 );
297 gtk_widget_set_sensitive( dialog.m_list , true );
301 // 8. build an Accelerator
302 Accelerator newAccel( event->keyval, (GdkModifierType) event->state );
304 // 8. verify the key is still free, show a dialog to ask what to do if not
305 class VerifyAcceleratorNotTaken : public CommandVisitor
307 const char *commandName;
308 const Accelerator &newAccel;
313 VerifyAcceleratorNotTaken( const char *name, const Accelerator &accelerator, ui::Widget w, ui::TreeModel m ) : commandName( name ), newAccel( accelerator ), widget( w ), model( m ), allow( true ){
315 void visit( const char* name, Accelerator& accelerator ){
316 if ( !strcmp( name, commandName ) ) {
322 if ( accelerator.key == 0 ) {
325 if ( accelerator == newAccel ) {
326 StringOutputStream msg;
327 msg << "The command " << name << " is already assigned to the key " << accelerator << ".\n\n"
328 << "Do you want to unassign " << name << " first?";
329 auto r = ui::alert( widget.window(), msg.c_str(), "Key already used", ui::alert_type::YESNOCANCEL );
330 if ( r == ui::alert_response::YES ) {
331 // clear the ACTUAL accelerator too!
332 disconnect_accelerator( name );
333 // delete the modifier
334 accelerator = accelerator_null();
335 // empty the cell of the key binds dialog
337 if ( gtk_tree_model_get_iter_first(model, &i ) ) {
341 memset( &val, 0, sizeof( val ) );
342 gtk_tree_model_get_value(model, &i, 0, &val );
343 const char *thisName = g_value_get_string( &val );;
344 if ( !strcmp( thisName, name ) ) {
345 gtk_list_store_set( ui::ListStore::from( model ), &i, 1, "", -1 );
347 g_value_unset( &val );
348 if ( !gtk_tree_model_iter_next(model, &i ) ) {
354 else if ( r == ui::alert_response::CANCEL ) {
360 } verify_visitor( commandName, newAccel, widget, dialog.m_model );
361 GlobalShortcuts_foreach( verify_visitor );
363 gtk_list_store_set( ui::ListStore::from( dialog.m_model ), &dialog.m_command_iter, 2, false, -1 );
364 gtk_widget_set_sensitive( dialog.m_list , true );
366 if ( verify_visitor.allow ) {
367 // clear the ACTUAL accelerator first
368 disconnect_accelerator( commandName );
370 thisShortcutIterator->second.first = newAccel;
372 // write into the cell
373 StringOutputStream modifiers;
374 modifiers << newAccel;
375 gtk_list_store_set( ui::ListStore::from( dialog.m_model ), &dialog.m_command_iter, 1, modifiers.c_str(), -1 );
377 // set the ACTUAL accelerator too!
378 connect_accelerator( commandName );
381 g_value_unset( &val );
383 dialog.m_model = ui::TreeModel(ui::null);
391 if(!model) {g_error("Unable to get model from cell renderer");}
392 gtk_tree_model_get_iter_from_string(model, &row, path_string);
394 gtk_tree_model_get_value(model, &row, 0, &val);
395 const char *name = g_value_get_string(&val);
396 Shortcuts::iterator i = g_shortcuts.find(name);
397 if(i != g_shortcuts.end())
399 accelerator_parse(i->second.first, new_text);
400 StringOutputStream modifiers;
401 modifiers << i->second.first;
402 gtk_list_store_set(ui::ListStore::from(model), &row, 1, modifiers.c_str(), -1);
407 void DoCommandListDlg(){
408 command_list_dialog_t dialog;
410 ui::Window window = MainFrame_getWindow().create_modal_dialog_window("Mapped Commands", dialog, -1, 400);
411 window.on_key_press([](ui::Widget widget, GdkEventKey *event, gpointer dialogptr) {
412 return accelerator_window_key_press(ui::Window::from(widget), event, dialogptr);
415 auto accel = ui::AccelGroup(ui::New);
416 window.add_accel_group( accel );
418 auto hbox = create_dialog_hbox( 4, 4 );
422 auto scr = create_scrolled_window( ui::Policy::NEVER, ui::Policy::AUTOMATIC );
423 hbox.pack_start( scr, TRUE, TRUE, 0 );
426 auto store = ui::ListStore::from(gtk_list_store_new( 4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_INT ));
428 auto view = ui::TreeView(ui::TreeModel::from(store._handle));
429 dialog.m_list = view;
431 //gtk_tree_view_set_enable_search( GTK_TREE_VIEW( view ), false ); // annoying
433 g_signal_connect( G_OBJECT( view ), "button_press_event", G_CALLBACK( accelerator_tree_butt_press ), &dialog );
436 auto renderer = ui::CellRendererText(ui::New);
437 auto column = ui::TreeViewColumn( "Command", renderer, {{"text", 0}, {"weight-set", 2}, {"weight", 3}} );
438 gtk_tree_view_append_column(view, column );
442 auto renderer = ui::CellRendererText(ui::New);
443 auto column = ui::TreeViewColumn( "Key", renderer, {{"text", 1}, {"weight-set", 2}, {"weight", 3}} );
444 gtk_tree_view_append_column(view, column );
452 class BuildCommandList : public CommandVisitor
454 ui::ListStore m_store;
456 BuildCommandList( ui::ListStore store ) : m_store( store ){
458 void visit( const char* name, Accelerator& accelerator ){
459 StringOutputStream modifiers;
460 modifiers << accelerator;
462 m_store.append(0, name, 1, modifiers.c_str(), 2, false, 3, 800);
466 GlobalShortcuts_foreach( visitor );
473 auto vbox = create_dialog_vbox( 4 );
474 hbox.pack_start( vbox, TRUE, TRUE, 0 );
476 auto editbutton = create_dialog_button( "Edit", (GCallback) accelerator_edit_button_clicked, &dialog );
477 vbox.pack_start( editbutton, FALSE, FALSE, 0 );
479 auto clearbutton = create_dialog_button( "Clear", (GCallback) accelerator_clear_button_clicked, &dialog );
480 vbox.pack_start( clearbutton, FALSE, FALSE, 0 );
482 ui::Widget spacer = ui::Image(ui::New);
484 vbox.pack_start( spacer, TRUE, TRUE, 0 );
486 auto button = create_modal_dialog_button( "Close", dialog.m_close_button );
487 vbox.pack_start( button, FALSE, FALSE, 0 );
488 widget_make_default( button );
489 gtk_widget_grab_default( button );
490 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
491 gtk_widget_add_accelerator( button , "clicked", accel, GDK_KEY_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
494 modal_dialog_show( window, dialog );
498 #include "profile/profile.h"
500 const char* const COMMANDS_VERSION = "1.1-gtk-accelnames";
502 void DeleteOldCommandMap(){
503 #if NETRADIANT_CUSTOM_FULLY_MERGED
504 // To enable when NetRadiant and NetRadiant-custom are fully merged together.
505 StringOutputStream path( 256 );
506 path << SettingsPath_get() << g_pGameDescription->mGameFile.c_str() << '/';
507 path << "shortcuts.ini";
508 unlink( path.c_str() );
512 void SaveCommandMap(){
513 StringOutputStream strINI( 256 );
514 strINI << SettingsPath_get() << "shortcuts.ini";
516 TextFileOutputStream file( strINI.c_str() );
517 if ( !file.failed() ) {
518 file << "[Version]\n";
519 file << "number=" << COMMANDS_VERSION << "\n";
521 file << "[Commands]\n";
522 class WriteCommandMap : public CommandVisitor
524 TextFileOutputStream& m_file;
526 WriteCommandMap( TextFileOutputStream& file ) : m_file( file ){
528 void visit( const char* name, Accelerator& accelerator ){
529 m_file << name << "=";
531 const char* key = gtk_accelerator_name( accelerator.key, accelerator.modifiers );
536 GlobalShortcuts_foreach( visitor );
539 DeleteOldCommandMap();
542 const char* stringrange_find( const char* first, const char* last, char c ){
543 const char* p = strchr( first, '+' );
550 class ReadCommandMap : public CommandVisitor
552 const char* m_filename;
555 ReadCommandMap( const char* filename ) : m_filename( filename ), m_count( 0 ){
557 void visit( const char* name, Accelerator& accelerator ){
559 if ( read_var( m_filename, "Commands", name, value ) ) {
560 if ( string_empty( value ) ) {
562 accelerator.modifiers = (GdkModifierType)0;
566 gtk_accelerator_parse( value, &accelerator.key, &accelerator.modifiers );
567 accelerator = accelerator; // fix modifiers
569 if ( accelerator.key != 0 ) {
574 globalOutputStream() << "WARNING: failed to parse user command " << makeQuoted( name ) << ": unknown key " << makeQuoted( value ) << "\n";
578 std::size_t count() const {
583 void LoadCommandMap(){
584 StringOutputStream strINI( 256 );
585 strINI << SettingsPath_get() << "shortcuts.ini";
587 FILE* f = fopen( strINI.c_str(), "r" );
590 globalOutputStream() << "loading custom shortcuts list from " << makeQuoted( strINI.c_str() ) << "\n";
592 Version version = version_parse( COMMANDS_VERSION );
593 Version dataVersion = { 0, 0 };
597 if ( read_var( strINI.c_str(), "Version", "number", value ) ) {
598 dataVersion = version_parse( value );
602 if ( version_compatible( version, dataVersion ) ) {
603 globalOutputStream() << "commands import: data version " << dataVersion << " is compatible with code version " << version << "\n";
604 ReadCommandMap visitor( strINI.c_str() );
605 GlobalShortcuts_foreach( visitor );
606 globalOutputStream() << "parsed " << Unsigned( visitor.count() ) << " custom shortcuts\n";
610 globalOutputStream() << "commands import: data version " << dataVersion << " is not compatible with code version " << version << "\n";
615 globalOutputStream() << "failed to load custom shortcuts from " << makeQuoted( strINI.c_str() ) << "\n";