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
24 #include "debugging/debugging.h"
28 #include "string/string.h"
29 #include "versionlib.h"
30 #include "gtkutil/accelerator.h"
31 #include "gtkutil/messagebox.h"
32 #include <gtk/gtktreeselection.h>
33 #include <gtk/gtkbutton.h>
36 typedef std::pair<Accelerator, int> ShortcutValue; // accelerator, isRegistered
37 typedef std::map<CopiedString, ShortcutValue> Shortcuts;
39 void Shortcuts_foreach( Shortcuts& shortcuts, CommandVisitor& visitor ){
40 for ( Shortcuts::iterator i = shortcuts.begin(); i != shortcuts.end(); ++i )
42 visitor.visit( ( *i ).first.c_str(), ( *i ).second.first );
46 Shortcuts g_shortcuts;
48 const Accelerator& GlobalShortcuts_insert( const char* name, const Accelerator& accelerator ){
49 return ( *g_shortcuts.insert( Shortcuts::value_type( name, ShortcutValue( accelerator, false ) ) ).first ).second.first;
52 void GlobalShortcuts_foreach( CommandVisitor& visitor ){
53 Shortcuts_foreach( g_shortcuts, visitor );
56 void GlobalShortcuts_register( const char* name, int type ){
57 Shortcuts::iterator i = g_shortcuts.find( name );
58 if ( i != g_shortcuts.end() ) {
59 ( *i ).second.second = type;
63 void GlobalShortcuts_reportUnregistered(){
64 for ( Shortcuts::iterator i = g_shortcuts.begin(); i != g_shortcuts.end(); ++i )
66 if ( ( *i ).second.first.key != 0 && !( *i ).second.second ) {
67 globalOutputStream() << "shortcut not registered: " << ( *i ).first.c_str() << "\n";
72 typedef std::map<CopiedString, Command> Commands;
76 void GlobalCommands_insert( const char* name, const Callback& callback, const Accelerator& accelerator ){
77 bool added = g_commands.insert( Commands::value_type( name, Command( callback, GlobalShortcuts_insert( name, accelerator ) ) ) ).second;
78 ASSERT_MESSAGE( added, "command already registered: " << makeQuoted( name ) );
81 const Command& GlobalCommands_find( const char* command ){
82 Commands::iterator i = g_commands.find( command );
83 ASSERT_MESSAGE( i != g_commands.end(), "failed to lookup command " << makeQuoted( command ) );
87 typedef std::map<CopiedString, Toggle> Toggles;
92 void GlobalToggles_insert( const char* name, const Callback& callback, const BoolExportCallback& exportCallback, const Accelerator& accelerator ){
93 bool added = g_toggles.insert( Toggles::value_type( name, Toggle( callback, GlobalShortcuts_insert( name, accelerator ), exportCallback ) ) ).second;
94 ASSERT_MESSAGE( added, "toggle already registered: " << makeQuoted( name ) );
96 const Toggle& GlobalToggles_find( const char* name ){
97 Toggles::iterator i = g_toggles.find( name );
98 ASSERT_MESSAGE( i != g_toggles.end(), "failed to lookup toggle " << makeQuoted( name ) );
102 typedef std::map<CopiedString, KeyEvent> KeyEvents;
105 KeyEvents g_keyEvents;
107 void GlobalKeyEvents_insert( const char* name, const Accelerator& accelerator, const Callback& keyDown, const Callback& keyUp ){
108 bool added = g_keyEvents.insert( KeyEvents::value_type( name, KeyEvent( GlobalShortcuts_insert( name, accelerator ), keyDown, keyUp ) ) ).second;
109 ASSERT_MESSAGE( added, "command already registered: " << makeQuoted( name ) );
111 const KeyEvent& GlobalKeyEvents_find( const char* name ){
112 KeyEvents::iterator i = g_keyEvents.find( name );
113 ASSERT_MESSAGE( i != g_keyEvents.end(), "failed to lookup keyEvent " << makeQuoted( name ) );
114 return ( *i ).second;
118 void disconnect_accelerator( const char *name ){
119 Shortcuts::iterator i = g_shortcuts.find( name );
120 if ( i != g_shortcuts.end() ) {
121 switch ( ( *i ).second.second )
125 command_disconnect_accelerator( name );
129 toggle_remove_accelerator( name );
135 void connect_accelerator( const char *name ){
136 Shortcuts::iterator i = g_shortcuts.find( name );
137 if ( i != g_shortcuts.end() ) {
138 switch ( ( *i ).second.second )
142 command_connect_accelerator( name );
146 toggle_add_accelerator( name );
155 #include <gtk/gtkbox.h>
156 #include <gtk/gtkliststore.h>
157 #include <gtk/gtktreemodel.h>
158 #include <gtk/gtktreeview.h>
159 #include <gtk/gtkcellrenderertext.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( NULL ), m_command_iter(), m_model( NULL ), m_waiting_for_key( false ){
173 ModalDialogButton m_close_button;
176 GtkTreeIter m_command_iter;
177 GtkTreeModel *m_model;
178 bool m_waiting_for_key;
181 void accelerator_clear_button_clicked( GtkButton *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( GTK_LIST_STORE( dialog.m_model ), &dialog.m_command_iter, 2, false, -1 );
188 gtk_widget_set_sensitive( GTK_WIDGET( dialog.m_list ), true );
189 dialog.m_model = NULL;
193 GtkTreeSelection *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( GTK_TREE_MODEL( 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( GTK_LIST_STORE( model ), &iter, 1, "", -1 );
216 g_value_unset( &val );
219 void accelerator_edit_button_clicked( GtkButton *btn, gpointer dialogptr ){
220 command_list_dialog_t &dialog = *(command_list_dialog_t *) dialogptr;
222 // 1. find selected row
223 GtkTreeSelection *sel = gtk_tree_view_get_selection( dialog.m_list );
226 if ( !gtk_tree_selection_get_selected( sel, &model, &iter ) ) {
229 dialog.m_command_iter = iter;
230 dialog.m_model = model;
232 // 2. disallow changing the row
233 //gtk_widget_set_sensitive(GTK_WIDGET(dialog.m_list), false);
235 // 3. highlight the row
236 gtk_list_store_set( GTK_LIST_STORE( model ), &iter, 2, true, -1 );
238 // 4. grab keyboard focus
239 dialog.m_waiting_for_key = true;
242 gboolean accelerator_window_key_press( GtkWidget *widget, GdkEventKey *event, gpointer dialogptr ){
243 command_list_dialog_t &dialog = *(command_list_dialog_t *) dialogptr;
245 if ( !dialog.m_waiting_for_key ) {
250 if ( event->is_modifier ) {
254 switch ( event->keyval )
274 dialog.m_waiting_for_key = false;
276 // 7. find the name of the accelerator
278 memset( &val, 0, sizeof( val ) );
279 gtk_tree_model_get_value( GTK_TREE_MODEL( dialog.m_model ), &dialog.m_command_iter, 0, &val );
280 const char *commandName = g_value_get_string( &val );;
281 Shortcuts::iterator thisShortcutIterator = g_shortcuts.find( commandName );
282 if ( thisShortcutIterator == g_shortcuts.end() ) {
283 gtk_list_store_set( GTK_LIST_STORE( dialog.m_model ), &dialog.m_command_iter, 2, false, -1 );
284 gtk_widget_set_sensitive( GTK_WIDGET( dialog.m_list ), true );
288 // 8. build an Accelerator
289 Accelerator newAccel( event->keyval, (GdkModifierType) event->state );
291 // 8. verify the key is still free, show a dialog to ask what to do if not
292 class VerifyAcceleratorNotTaken : public CommandVisitor
294 const char *commandName;
295 const Accelerator &newAccel;
300 VerifyAcceleratorNotTaken( const char *name, const Accelerator &accelerator, GtkWidget *w, GtkTreeModel *m ) : commandName( name ), newAccel( accelerator ), widget( w ), model( m ), allow( true ){
302 void visit( const char* name, Accelerator& accelerator ){
303 if ( !strcmp( name, commandName ) ) {
309 if ( accelerator.key == 0 ) {
312 if ( accelerator == newAccel ) {
313 StringOutputStream msg;
314 msg << "The command " << name << " is already assigned to the key " << accelerator << ".\n\n"
315 << "Do you want to unassign " << name << " first?";
316 EMessageBoxReturn r = gtk_MessageBox( widget, msg.c_str(), "Key already used", eMB_YESNOCANCEL );
318 // clear the ACTUAL accelerator too!
319 disconnect_accelerator( name );
320 // delete the modifier
321 accelerator = accelerator_null();
322 // empty the cell of the key binds dialog
324 if ( gtk_tree_model_get_iter_first( GTK_TREE_MODEL( model ), &i ) ) {
328 memset( &val, 0, sizeof( val ) );
329 gtk_tree_model_get_value( GTK_TREE_MODEL( model ), &i, 0, &val );
330 const char *thisName = g_value_get_string( &val );;
331 if ( !strcmp( thisName, name ) ) {
332 gtk_list_store_set( GTK_LIST_STORE( model ), &i, 1, "", -1 );
334 g_value_unset( &val );
335 if ( !gtk_tree_model_iter_next( GTK_TREE_MODEL( model ), &i ) ) {
341 else if ( r == eIDCANCEL ) {
347 } verify_visitor( commandName, newAccel, widget, dialog.m_model );
348 GlobalShortcuts_foreach( verify_visitor );
350 gtk_list_store_set( GTK_LIST_STORE( dialog.m_model ), &dialog.m_command_iter, 2, false, -1 );
351 gtk_widget_set_sensitive( GTK_WIDGET( dialog.m_list ), true );
353 if ( verify_visitor.allow ) {
354 // clear the ACTUAL accelerator first
355 disconnect_accelerator( commandName );
357 thisShortcutIterator->second.first = newAccel;
359 // write into the cell
360 StringOutputStream modifiers;
361 modifiers << newAccel;
362 gtk_list_store_set( GTK_LIST_STORE( dialog.m_model ), &dialog.m_command_iter, 1, modifiers.c_str(), -1 );
364 // set the ACTUAL accelerator too!
365 connect_accelerator( commandName );
368 g_value_unset( &val );
370 dialog.m_model = NULL;
378 if(!model) {g_error("Unable to get model from cell renderer");}
379 gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(model), &row, path_string);
381 gtk_tree_model_get_value(GTK_TREE_MODEL(model), &row, 0, &val);
382 const char *name = g_value_get_string(&val);
383 Shortcuts::iterator i = g_shortcuts.find(name);
384 if(i != g_shortcuts.end())
386 accelerator_parse(i->second.first, new_text);
387 StringOutputStream modifiers;
388 modifiers << i->second.first;
389 gtk_list_store_set(GTK_LIST_STORE(model), &row, 1, modifiers.c_str(), -1);
394 void DoCommandListDlg(){
395 command_list_dialog_t dialog;
397 GtkWindow* window = create_modal_dialog_window( MainFrame_getWindow(), "Mapped Commands", dialog, -1, 400 );
398 g_signal_connect( G_OBJECT( window ), "key-press-event", (GCallback) accelerator_window_key_press, &dialog );
400 GtkAccelGroup* accel = gtk_accel_group_new();
401 gtk_window_add_accel_group( window, accel );
403 GtkHBox* hbox = create_dialog_hbox( 4, 4 );
404 gtk_container_add( GTK_CONTAINER( window ), GTK_WIDGET( hbox ) );
407 GtkScrolledWindow* scr = create_scrolled_window( GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC );
408 gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( scr ), TRUE, TRUE, 0 );
411 GtkListStore* store = gtk_list_store_new( 4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_INT );
413 GtkWidget* view = gtk_tree_view_new_with_model( GTK_TREE_MODEL( store ) );
414 dialog.m_list = GTK_TREE_VIEW( view );
416 gtk_tree_view_set_enable_search( GTK_TREE_VIEW( view ), false ); // annoying
419 GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
420 GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes( "Command", renderer, "text", 0, "weight-set", 2, "weight", 3, NULL );
421 gtk_tree_view_append_column( GTK_TREE_VIEW( view ), column );
425 GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
426 GtkTreeViewColumn* column = gtk_tree_view_column_new_with_attributes( "Key", renderer, "text", 1, "weight-set", 2, "weight", 3, NULL );
427 gtk_tree_view_append_column( GTK_TREE_VIEW( view ), column );
430 gtk_widget_show( view );
431 gtk_container_add( GTK_CONTAINER( scr ), view );
435 StringOutputStream path( 256 );
436 path << SettingsPath_get() << "commandlist.txt";
437 globalOutputStream() << "Writing the command list to " << path.c_str() << "\n";
438 class BuildCommandList : public CommandVisitor
440 TextFileOutputStream m_commandList;
441 GtkListStore* m_store;
443 BuildCommandList( const char* filename, GtkListStore* store ) : m_commandList( filename ), m_store( store ){
445 void visit( const char* name, Accelerator& accelerator ){
446 StringOutputStream modifiers;
447 modifiers << accelerator;
451 gtk_list_store_append( m_store, &iter );
452 gtk_list_store_set( m_store, &iter, 0, name, 1, modifiers.c_str(), 2, false, 3, 800, -1 );
455 if ( !m_commandList.failed() ) {
456 int l = strlen( name );
457 m_commandList << name;
459 m_commandList << ' ';
460 m_commandList << modifiers.c_str() << '\n';
463 } visitor( path.c_str(), store );
465 GlobalShortcuts_foreach( visitor );
468 g_object_unref( G_OBJECT( store ) );
472 GtkVBox* vbox = create_dialog_vbox( 4 );
473 gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( vbox ), TRUE, TRUE, 0 );
475 GtkButton* editbutton = create_dialog_button( "Edit", (GCallback) accelerator_edit_button_clicked, &dialog );
476 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( editbutton ), FALSE, FALSE, 0 );
478 GtkButton* clearbutton = create_dialog_button( "Clear", (GCallback) accelerator_clear_button_clicked, &dialog );
479 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( clearbutton ), FALSE, FALSE, 0 );
481 GtkWidget *spacer = gtk_image_new();
482 gtk_widget_show( spacer );
483 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( spacer ), TRUE, TRUE, 0 );
485 GtkButton* button = create_modal_dialog_button( "Close", dialog.m_close_button );
486 gtk_box_pack_start( GTK_BOX( vbox ), GTK_WIDGET( button ), FALSE, FALSE, 0 );
487 widget_make_default( GTK_WIDGET( button ) );
488 gtk_widget_grab_default( GTK_WIDGET( button ) );
489 gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel, GDK_Return, (GdkModifierType)0, (GtkAccelFlags)0 );
490 gtk_widget_add_accelerator( GTK_WIDGET( button ), "clicked", accel, GDK_Escape, (GdkModifierType)0, (GtkAccelFlags)0 );
493 modal_dialog_show( window, dialog );
494 gtk_widget_destroy( GTK_WIDGET( window ) );
497 #include "profile/profile.h"
499 const char* const COMMANDS_VERSION = "1.0-gtk-accelnames";
501 void SaveCommandMap( const char* path ){
502 StringOutputStream strINI( 256 );
503 strINI << path << "shortcuts.ini";
505 TextFileOutputStream file( strINI.c_str() );
506 if ( !file.failed() ) {
507 file << "[Version]\n";
508 file << "number=" << COMMANDS_VERSION << "\n";
510 file << "[Commands]\n";
511 class WriteCommandMap : public CommandVisitor
513 TextFileOutputStream& m_file;
515 WriteCommandMap( TextFileOutputStream& file ) : m_file( file ){
517 void visit( const char* name, Accelerator& accelerator ){
518 m_file << name << "=";
520 const char* key = gtk_accelerator_name( accelerator.key, accelerator.modifiers );
525 GlobalShortcuts_foreach( visitor );
529 const char* stringrange_find( const char* first, const char* last, char c ){
530 const char* p = strchr( first, '+' );
537 class ReadCommandMap : public CommandVisitor
539 const char* m_filename;
542 ReadCommandMap( const char* filename ) : m_filename( filename ), m_count( 0 ){
544 void visit( const char* name, Accelerator& accelerator ){
546 if ( read_var( m_filename, "Commands", name, value ) ) {
547 if ( string_empty( value ) ) {
549 accelerator.modifiers = (GdkModifierType)0;
553 gtk_accelerator_parse( value, &accelerator.key, &accelerator.modifiers );
554 accelerator = accelerator; // fix modifiers
556 if ( accelerator.key != 0 ) {
561 globalOutputStream() << "WARNING: failed to parse user command " << makeQuoted( name ) << ": unknown key " << makeQuoted( value ) << "\n";
565 std::size_t count() const {
570 void LoadCommandMap( const char* path ){
571 StringOutputStream strINI( 256 );
572 strINI << path << "shortcuts.ini";
574 FILE* f = fopen( strINI.c_str(), "r" );
577 globalOutputStream() << "loading custom shortcuts list from " << makeQuoted( strINI.c_str() ) << "\n";
579 Version version = version_parse( COMMANDS_VERSION );
580 Version dataVersion = { 0, 0 };
584 if ( read_var( strINI.c_str(), "Version", "number", value ) ) {
585 dataVersion = version_parse( value );
589 if ( version_compatible( version, dataVersion ) ) {
590 globalOutputStream() << "commands import: data version " << dataVersion << " is compatible with code version " << version << "\n";
591 ReadCommandMap visitor( strINI.c_str() );
592 GlobalShortcuts_foreach( visitor );
593 globalOutputStream() << "parsed " << Unsigned( visitor.count() ) << " custom shortcuts\n";
597 globalOutputStream() << "commands import: data version " << dataVersion << " is not compatible with code version " << version << "\n";
602 globalOutputStream() << "failed to load custom shortcuts from " << makeQuoted( strINI.c_str() ) << "\n";