| 1 | /* |
| 2 | Copyright (C) 2001-2006, William Joseph. |
| 3 | All Rights Reserved. |
| 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 "commands.h" |
| 23 | |
| 24 | #include "gtk/gtk.h" |
| 25 | #include "debugging/debugging.h" |
| 26 | #include "warnings.h" |
| 27 | |
| 28 | #include <map> |
| 29 | #include "string/string.h" |
| 30 | #include "versionlib.h" |
| 31 | #include "gtkutil/messagebox.h" |
| 32 | #include "gtkmisc.h" |
| 33 | |
| 34 | #define NETRADIANT_CUSTOM_FULLY_MERGED 0 |
| 35 | #if NETRADIANT_CUSTOM_FULLY_MERGED |
| 36 | // For deleting old shortcuts.ini file |
| 37 | #include "preferences.h" |
| 38 | #include "unistd.h" |
| 39 | #endif // NETRADIANT_CUSTOM_FULLY_MERGED |
| 40 | |
| 41 | typedef std::pair<Accelerator, int> ShortcutValue; // accelerator, isRegistered |
| 42 | typedef std::map<CopiedString, ShortcutValue> Shortcuts; |
| 43 | |
| 44 | void Shortcuts_foreach( Shortcuts& shortcuts, CommandVisitor& visitor ){ |
| 45 | for ( Shortcuts::iterator i = shortcuts.begin(); i != shortcuts.end(); ++i ) |
| 46 | { |
| 47 | visitor.visit( ( *i ).first.c_str(), ( *i ).second.first ); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | Shortcuts g_shortcuts; |
| 52 | |
| 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; |
| 55 | } |
| 56 | |
| 57 | void GlobalShortcuts_foreach( CommandVisitor& visitor ){ |
| 58 | Shortcuts_foreach( g_shortcuts, visitor ); |
| 59 | } |
| 60 | |
| 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; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void GlobalShortcuts_reportUnregistered(){ |
| 69 | for ( Shortcuts::iterator i = g_shortcuts.begin(); i != g_shortcuts.end(); ++i ) |
| 70 | { |
| 71 | if ( ( *i ).second.first.key != 0 && !( *i ).second.second ) { |
| 72 | globalOutputStream() << "shortcut not registered: " << ( *i ).first.c_str() << "\n"; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | typedef std::map<CopiedString, Command> Commands; |
| 78 | |
| 79 | Commands g_commands; |
| 80 | |
| 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 ) ); |
| 84 | } |
| 85 | |
| 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 ) ); |
| 89 | return ( *i ).second; |
| 90 | } |
| 91 | |
| 92 | typedef std::map<CopiedString, Toggle> Toggles; |
| 93 | |
| 94 | |
| 95 | Toggles g_toggles; |
| 96 | |
| 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 ) ); |
| 100 | } |
| 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; |
| 105 | } |
| 106 | |
| 107 | typedef std::map<CopiedString, KeyEvent> KeyEvents; |
| 108 | |
| 109 | |
| 110 | KeyEvents g_keyEvents; |
| 111 | |
| 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 ) ); |
| 115 | } |
| 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; |
| 120 | } |
| 121 | |
| 122 | |
| 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 ) |
| 127 | { |
| 128 | case 1: |
| 129 | // command |
| 130 | command_disconnect_accelerator( name ); |
| 131 | break; |
| 132 | case 2: |
| 133 | // toggle |
| 134 | toggle_remove_accelerator( name ); |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 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 ) |
| 144 | { |
| 145 | case 1: |
| 146 | // command |
| 147 | command_connect_accelerator( name ); |
| 148 | break; |
| 149 | case 2: |
| 150 | // toggle |
| 151 | toggle_add_accelerator( name ); |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | |
| 158 | #include <uilib/uilib.h> |
| 159 | #include <gdk/gdkkeysyms.h> |
| 160 | |
| 161 | #include "gtkutil/dialog.h" |
| 162 | #include "mainframe.h" |
| 163 | |
| 164 | #include "stream/textfilestream.h" |
| 165 | #include "stream/stringstream.h" |
| 166 | |
| 167 | |
| 168 | struct command_list_dialog_t : public ModalDialog |
| 169 | { |
| 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 ){ |
| 172 | } |
| 173 | ModalDialogButton m_close_button; |
| 174 | |
| 175 | ui::TreeView m_list; |
| 176 | GtkTreeIter m_command_iter; |
| 177 | ui::TreeModel m_model; |
| 178 | bool m_waiting_for_key; |
| 179 | }; |
| 180 | |
| 181 | void accelerator_clear_button_clicked( ui::Button btn, gpointer dialogptr ){ |
| 182 | command_list_dialog_t &dialog = *(command_list_dialog_t *) dialogptr; |
| 183 | |
| 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); |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | auto sel = gtk_tree_view_get_selection( dialog.m_list ); |
| 194 | GtkTreeModel *model; |
| 195 | GtkTreeIter iter; |
| 196 | if ( !gtk_tree_selection_get_selected( sel, &model, &iter ) ) { |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | GValue val; |
| 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 );; |
| 204 | |
| 205 | // clear the ACTUAL accelerator too! |
| 206 | disconnect_accelerator( commandName ); |
| 207 | |
| 208 | Shortcuts::iterator thisShortcutIterator = g_shortcuts.find( commandName ); |
| 209 | if ( thisShortcutIterator == g_shortcuts.end() ) { |
| 210 | return; |
| 211 | } |
| 212 | thisShortcutIterator->second.first = accelerator_null(); |
| 213 | |
| 214 | gtk_list_store_set( ui::ListStore::from( model ), &iter, 1, "", -1 ); |
| 215 | |
| 216 | g_value_unset( &val ); |
| 217 | } |
| 218 | |
| 219 | void accelerator_edit_button_clicked( ui::Button btn, gpointer dialogptr ){ |
| 220 | command_list_dialog_t &dialog = *(command_list_dialog_t *) dialogptr; |
| 221 | |
| 222 | // 1. find selected row |
| 223 | auto sel = gtk_tree_view_get_selection( dialog.m_list ); |
| 224 | GtkTreeModel *model; |
| 225 | GtkTreeIter iter; |
| 226 | if ( !gtk_tree_selection_get_selected( sel, &model, &iter ) ) { |
| 227 | return; |
| 228 | } |
| 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 ); |
| 233 | } |
| 234 | dialog.m_command_iter = iter; |
| 235 | dialog.m_model = ui::TreeModel::from(model); |
| 236 | |
| 237 | // 2. disallow changing the row |
| 238 | //gtk_widget_set_sensitive(dialog.m_list, false); |
| 239 | |
| 240 | // 3. highlight the row |
| 241 | gtk_list_store_set( ui::ListStore::from( model ), &iter, 2, true, -1 ); |
| 242 | |
| 243 | // 4. grab keyboard focus |
| 244 | dialog.m_waiting_for_key = true; |
| 245 | } |
| 246 | |
| 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 ); |
| 250 | return TRUE; |
| 251 | } |
| 252 | return FALSE; |
| 253 | } |
| 254 | |
| 255 | bool accelerator_window_key_press( ui::Window widget, GdkEventKey *event, gpointer dialogptr ){ |
| 256 | command_list_dialog_t &dialog = *(command_list_dialog_t *) dialogptr; |
| 257 | |
| 258 | if ( !dialog.m_waiting_for_key ) { |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | #if 0 |
| 263 | if ( event->is_modifier ) { |
| 264 | return false; |
| 265 | } |
| 266 | #else |
| 267 | switch ( event->keyval ) |
| 268 | { |
| 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: |
| 275 | case GDK_KEY_Meta_L: |
| 276 | case GDK_KEY_Meta_R: |
| 277 | case GDK_KEY_Alt_L: |
| 278 | case GDK_KEY_Alt_R: |
| 279 | case GDK_KEY_Super_L: |
| 280 | case GDK_KEY_Super_R: |
| 281 | case GDK_KEY_Hyper_L: |
| 282 | case GDK_KEY_Hyper_R: |
| 283 | return false; |
| 284 | } |
| 285 | #endif |
| 286 | |
| 287 | dialog.m_waiting_for_key = false; |
| 288 | |
| 289 | // 7. find the name of the accelerator |
| 290 | GValue val; |
| 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 ); |
| 298 | return true; |
| 299 | } |
| 300 | |
| 301 | // 8. build an Accelerator |
| 302 | Accelerator newAccel( event->keyval, (GdkModifierType) event->state ); |
| 303 | |
| 304 | // 8. verify the key is still free, show a dialog to ask what to do if not |
| 305 | class VerifyAcceleratorNotTaken : public CommandVisitor |
| 306 | { |
| 307 | const char *commandName; |
| 308 | const Accelerator &newAccel; |
| 309 | ui::Widget widget; |
| 310 | ui::TreeModel model; |
| 311 | public: |
| 312 | bool allow; |
| 313 | VerifyAcceleratorNotTaken( const char *name, const Accelerator &accelerator, ui::Widget w, ui::TreeModel m ) : commandName( name ), newAccel( accelerator ), widget( w ), model( m ), allow( true ){ |
| 314 | } |
| 315 | void visit( const char* name, Accelerator& accelerator ){ |
| 316 | if ( !strcmp( name, commandName ) ) { |
| 317 | return; |
| 318 | } |
| 319 | if ( !allow ) { |
| 320 | return; |
| 321 | } |
| 322 | if ( accelerator.key == 0 ) { |
| 323 | return; |
| 324 | } |
| 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 |
| 336 | GtkTreeIter i; |
| 337 | if ( gtk_tree_model_get_iter_first(model, &i ) ) { |
| 338 | for (;; ) |
| 339 | { |
| 340 | GValue val; |
| 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 ); |
| 346 | } |
| 347 | g_value_unset( &val ); |
| 348 | if ( !gtk_tree_model_iter_next(model, &i ) ) { |
| 349 | break; |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | else if ( r == ui::alert_response::CANCEL ) { |
| 355 | // aborted |
| 356 | allow = false; |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | } verify_visitor( commandName, newAccel, widget, dialog.m_model ); |
| 361 | GlobalShortcuts_foreach( verify_visitor ); |
| 362 | |
| 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 ); |
| 365 | |
| 366 | if ( verify_visitor.allow ) { |
| 367 | // clear the ACTUAL accelerator first |
| 368 | disconnect_accelerator( commandName ); |
| 369 | |
| 370 | thisShortcutIterator->second.first = newAccel; |
| 371 | |
| 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 ); |
| 376 | |
| 377 | // set the ACTUAL accelerator too! |
| 378 | connect_accelerator( commandName ); |
| 379 | } |
| 380 | |
| 381 | g_value_unset( &val ); |
| 382 | |
| 383 | dialog.m_model = ui::TreeModel(ui::null); |
| 384 | |
| 385 | return true; |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | GtkTreeIter row; |
| 390 | GValue val; |
| 391 | if(!model) {g_error("Unable to get model from cell renderer");} |
| 392 | gtk_tree_model_get_iter_from_string(model, &row, path_string); |
| 393 | |
| 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()) |
| 398 | { |
| 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); |
| 403 | } |
| 404 | }; |
| 405 | */ |
| 406 | |
| 407 | void DoCommandListDlg(){ |
| 408 | command_list_dialog_t dialog; |
| 409 | |
| 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); |
| 413 | }, &dialog); |
| 414 | |
| 415 | auto accel = ui::AccelGroup(ui::New); |
| 416 | window.add_accel_group( accel ); |
| 417 | |
| 418 | auto hbox = create_dialog_hbox( 4, 4 ); |
| 419 | window.add(hbox); |
| 420 | |
| 421 | { |
| 422 | auto scr = create_scrolled_window( ui::Policy::NEVER, ui::Policy::AUTOMATIC ); |
| 423 | hbox.pack_start( scr, TRUE, TRUE, 0 ); |
| 424 | |
| 425 | { |
| 426 | auto store = ui::ListStore::from(gtk_list_store_new( 4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_INT )); |
| 427 | |
| 428 | auto view = ui::TreeView(ui::TreeModel::from(store._handle)); |
| 429 | dialog.m_list = view; |
| 430 | |
| 431 | //gtk_tree_view_set_enable_search( GTK_TREE_VIEW( view ), false ); // annoying |
| 432 | |
| 433 | g_signal_connect( G_OBJECT( view ), "button_press_event", G_CALLBACK( accelerator_tree_butt_press ), &dialog ); |
| 434 | |
| 435 | { |
| 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 ); |
| 439 | } |
| 440 | |
| 441 | { |
| 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 ); |
| 445 | } |
| 446 | |
| 447 | view.show(); |
| 448 | scr.add(view); |
| 449 | |
| 450 | { |
| 451 | // Initialize dialog |
| 452 | class BuildCommandList : public CommandVisitor |
| 453 | { |
| 454 | ui::ListStore m_store; |
| 455 | public: |
| 456 | BuildCommandList( ui::ListStore store ) : m_store( store ){ |
| 457 | } |
| 458 | void visit( const char* name, Accelerator& accelerator ){ |
| 459 | StringOutputStream modifiers; |
| 460 | modifiers << accelerator; |
| 461 | |
| 462 | m_store.append(0, name, 1, modifiers.c_str(), 2, false, 3, 800); |
| 463 | } |
| 464 | } visitor( store ); |
| 465 | |
| 466 | GlobalShortcuts_foreach( visitor ); |
| 467 | } |
| 468 | |
| 469 | store.unref(); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | auto vbox = create_dialog_vbox( 4 ); |
| 474 | hbox.pack_start( vbox, TRUE, TRUE, 0 ); |
| 475 | { |
| 476 | auto editbutton = create_dialog_button( "Edit", (GCallback) accelerator_edit_button_clicked, &dialog ); |
| 477 | vbox.pack_start( editbutton, FALSE, FALSE, 0 ); |
| 478 | |
| 479 | auto clearbutton = create_dialog_button( "Clear", (GCallback) accelerator_clear_button_clicked, &dialog ); |
| 480 | vbox.pack_start( clearbutton, FALSE, FALSE, 0 ); |
| 481 | |
| 482 | ui::Widget spacer = ui::Image(ui::New); |
| 483 | spacer.show(); |
| 484 | vbox.pack_start( spacer, TRUE, TRUE, 0 ); |
| 485 | |
| 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 ); |
| 492 | } |
| 493 | |
| 494 | modal_dialog_show( window, dialog ); |
| 495 | window.destroy(); |
| 496 | } |
| 497 | |
| 498 | #include "profile/profile.h" |
| 499 | |
| 500 | const char* const COMMANDS_VERSION = "1.1-gtk-accelnames"; |
| 501 | |
| 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() ); |
| 509 | #endif |
| 510 | } |
| 511 | |
| 512 | void SaveCommandMap(){ |
| 513 | StringOutputStream strINI( 256 ); |
| 514 | strINI << SettingsPath_get() << "shortcuts.ini"; |
| 515 | |
| 516 | TextFileOutputStream file( strINI.c_str() ); |
| 517 | if ( !file.failed() ) { |
| 518 | file << "[Version]\n"; |
| 519 | file << "number=" << COMMANDS_VERSION << "\n"; |
| 520 | file << "\n"; |
| 521 | file << "[Commands]\n"; |
| 522 | class WriteCommandMap : public CommandVisitor |
| 523 | { |
| 524 | TextFileOutputStream& m_file; |
| 525 | public: |
| 526 | WriteCommandMap( TextFileOutputStream& file ) : m_file( file ){ |
| 527 | } |
| 528 | void visit( const char* name, Accelerator& accelerator ){ |
| 529 | m_file << name << "="; |
| 530 | |
| 531 | const char* key = gtk_accelerator_name( accelerator.key, accelerator.modifiers ); |
| 532 | m_file << key; |
| 533 | m_file << "\n"; |
| 534 | } |
| 535 | } visitor( file ); |
| 536 | GlobalShortcuts_foreach( visitor ); |
| 537 | } |
| 538 | |
| 539 | DeleteOldCommandMap(); |
| 540 | } |
| 541 | |
| 542 | const char* stringrange_find( const char* first, const char* last, char c ){ |
| 543 | const char* p = strchr( first, '+' ); |
| 544 | if ( p == 0 ) { |
| 545 | return last; |
| 546 | } |
| 547 | return p; |
| 548 | } |
| 549 | |
| 550 | class ReadCommandMap : public CommandVisitor |
| 551 | { |
| 552 | const char* m_filename; |
| 553 | std::size_t m_count; |
| 554 | public: |
| 555 | ReadCommandMap( const char* filename ) : m_filename( filename ), m_count( 0 ){ |
| 556 | } |
| 557 | void visit( const char* name, Accelerator& accelerator ){ |
| 558 | char value[1024]; |
| 559 | if ( read_var( m_filename, "Commands", name, value ) ) { |
| 560 | if ( string_empty( value ) ) { |
| 561 | accelerator.key = 0; |
| 562 | accelerator.modifiers = (GdkModifierType)0; |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | gtk_accelerator_parse( value, &accelerator.key, &accelerator.modifiers ); |
| 567 | accelerator = accelerator; // fix modifiers |
| 568 | |
| 569 | if ( accelerator.key != 0 ) { |
| 570 | ++m_count; |
| 571 | } |
| 572 | else |
| 573 | { |
| 574 | globalOutputStream() << "WARNING: failed to parse user command " << makeQuoted( name ) << ": unknown key " << makeQuoted( value ) << "\n"; |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | std::size_t count() const { |
| 579 | return m_count; |
| 580 | } |
| 581 | }; |
| 582 | |
| 583 | void LoadCommandMap(){ |
| 584 | StringOutputStream strINI( 256 ); |
| 585 | strINI << SettingsPath_get() << "shortcuts.ini"; |
| 586 | |
| 587 | FILE* f = fopen( strINI.c_str(), "r" ); |
| 588 | if ( f != 0 ) { |
| 589 | fclose( f ); |
| 590 | globalOutputStream() << "loading custom shortcuts list from " << makeQuoted( strINI.c_str() ) << "\n"; |
| 591 | |
| 592 | Version version = version_parse( COMMANDS_VERSION ); |
| 593 | Version dataVersion = { 0, 0 }; |
| 594 | |
| 595 | { |
| 596 | char value[1024]; |
| 597 | if ( read_var( strINI.c_str(), "Version", "number", value ) ) { |
| 598 | dataVersion = version_parse( value ); |
| 599 | } |
| 600 | } |
| 601 | |
| 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"; |
| 607 | } |
| 608 | else |
| 609 | { |
| 610 | globalOutputStream() << "commands import: data version " << dataVersion << " is not compatible with code version " << version << "\n"; |
| 611 | } |
| 612 | } |
| 613 | else |
| 614 | { |
| 615 | globalOutputStream() << "failed to load custom shortcuts from " << makeQuoted( strINI.c_str() ) << "\n"; |
| 616 | } |
| 617 | } |