tdestdaccel.cpp
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 1997 Stefan Taferner (taferner@alpin.or.at) 00003 Copyright (C) 2000 Nicolas Hadacek (haadcek@kde.org) 00004 Copyright (C) 2001,2002 Ellis Whitehead (ellis@kde.org) 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License version 2 as published by the Free Software Foundation. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 #define __KSTDACCEL_CPP_ 1 00021 00022 #include "tdestdaccel.h" 00023 00024 #include "tdeaccelaction.h" 00025 #include "tdeaccelbase.h" 00026 #include <tdeconfig.h> 00027 #include <kdebug.h> 00028 #include <tdeglobal.h> 00029 #include <tdelocale.h> 00030 #include <tdeshortcut.h> 00031 #include <tdeshortcutlist.h> 00032 00033 namespace TDEStdAccel 00034 { 00035 00036 struct TDEStdAccelInfo 00037 { 00038 StdAccel id; 00039 const char* psName; 00040 const char* psDesc; 00041 int cutDefault, cutDefault4, cutDefault3B, cutDefault4B; 00042 TDEShortcut cut; 00043 bool bInitialized; 00044 }; 00045 00050 static TDEStdAccelInfo g_infoStdAccel[] = 00051 { 00052 {AccelNone, "Group:File", I18N_NOOP("File"), 0, 0, 0, 0, TDEShortcut(), false }, 00053 { Open, I18N_NOOP("Open"), 0, Qt::CTRL+Qt::Key_O, 0, 0, 0, TDEShortcut(), false }, 00054 { New, I18N_NOOP("New"), 0, Qt::CTRL+Qt::Key_N, 0, 0, 0, TDEShortcut(), false }, 00055 { Close, I18N_NOOP("Close"), 0, Qt::CTRL+Qt::Key_W, Qt::CTRL+Qt::Key_Escape, 0, Qt::CTRL+Qt::Key_W, TDEShortcut(), false }, 00056 { Save, I18N_NOOP("Save"), 0, Qt::CTRL+Qt::Key_S, 0, 0, 0, TDEShortcut(), false }, 00057 { Print, I18N_NOOP("Print"), 0, Qt::CTRL+Qt::Key_P, 0, 0, 0, TDEShortcut(), false }, 00058 { Quit, I18N_NOOP("Quit"), 0, Qt::CTRL+Qt::Key_Q, 0, 0, 0, TDEShortcut(), false }, 00059 {AccelNone, "Group:Edit", I18N_NOOP("Edit"), 0, 0, 0, 0, TDEShortcut(), false }, 00060 { Undo, I18N_NOOP("Undo"), 0, Qt::CTRL+Qt::Key_Z, 0, 0, 0, TDEShortcut(), false }, 00061 { Redo, I18N_NOOP("Redo"), 0, Qt::CTRL+Qt::SHIFT+Qt::Key_Z, 0, 0, 0, TDEShortcut(), false }, 00062 { Cut, I18N_NOOP("Cut"), 0, Qt::CTRL+Qt::Key_X, 0, Qt::SHIFT+Qt::Key_Delete, 0, TDEShortcut(), false }, 00063 { Copy, I18N_NOOP("Copy"), 0, Qt::CTRL+Qt::Key_C, 0, Qt::CTRL+Qt::Key_Insert, 0, TDEShortcut(), false }, 00064 { Paste, I18N_NOOP("Paste"), 0, Qt::CTRL+Qt::Key_V, 0, Qt::SHIFT+Qt::Key_Insert, 0, TDEShortcut(), false }, 00065 { PasteSelection, I18N_NOOP("Paste Selection"), 0, 0, 0, Qt::CTRL+Qt::SHIFT+Qt::Key_Insert, 0, TDEShortcut(), false }, 00066 { SelectAll, "SelectAll", I18N_NOOP("Select All"), Qt::CTRL+Qt::Key_A, 0, 0, 0, TDEShortcut(), false }, 00067 { Deselect, I18N_NOOP("Deselect"), 0, Qt::CTRL+Qt::SHIFT+Qt::Key_A, 0, 0, 0, TDEShortcut(), false }, 00068 { DeleteWordBack, "DeleteWordBack", I18N_NOOP("Delete Word Backwards"), Qt::CTRL+Qt::Key_Backspace, 0, 0, 0, TDEShortcut(), false }, 00069 { DeleteWordForward, "DeleteWordForward", I18N_NOOP("Delete Word Forward"), Qt::CTRL+Qt::Key_Delete, 0, 0, 0, TDEShortcut(), false }, 00070 { Find, I18N_NOOP("Find"), 0, Qt::CTRL+Qt::Key_F, 0, 0, 0, TDEShortcut(), false }, 00071 { FindNext, "FindNext", I18N_NOOP("Find Next"), Qt::Key_F3, 0, 0, 0, TDEShortcut(), false }, 00072 { FindPrev, "FindPrev", I18N_NOOP("Find Prev"), Qt::SHIFT+Qt::Key_F3, 0, 0, 0, TDEShortcut(), false }, 00073 { Replace, I18N_NOOP("Replace"), 0, Qt::CTRL+Qt::Key_R, 0, 0, 0, TDEShortcut(), false }, 00074 {AccelNone, "Group:Navigation", I18N_NOOP("Navigation"), 0, 0, 0, 0, TDEShortcut(), false }, 00075 { Home, I18N_NOOP2("Opposite to End","Home"), 0, Qt::CTRL+Qt::Key_Home, 0, Qt::Key_HomePage, 0, TDEShortcut(), false }, 00076 { End, I18N_NOOP("End"), 0, Qt::CTRL+Qt::Key_End, 0, 0, 0, TDEShortcut(), false }, 00077 { BeginningOfLine, "BeginningOfLine", I18N_NOOP("Beginning of Line"), Qt::Key_Home, 0, 0, 0, TDEShortcut(), false}, 00078 { EndOfLine, "EndOfLine", I18N_NOOP("End of Line"), Qt::Key_End, 0, 0, 0, TDEShortcut(), false}, 00079 { Prior, I18N_NOOP("Prior"), 0, TQt::Key_Prior, 0, 0, 0, TDEShortcut(), false }, 00080 { Next, I18N_NOOP2("Opposite to Prior","Next"), 0, TQt::Key_Next, 0, 0, 0, TDEShortcut(), false }, 00081 { GotoLine, "GotoLine", I18N_NOOP("Go to Line"), Qt::CTRL+Qt::Key_G, 0, 0, 0, TDEShortcut(), false }, 00082 { AddBookmark, "AddBookmark", I18N_NOOP("Add Bookmark"), Qt::CTRL+Qt::Key_B, 0, 0, 0, TDEShortcut(), false }, 00083 { ZoomIn, "ZoomIn", I18N_NOOP("Zoom In"), Qt::CTRL+Qt::Key_Plus, 0, 0, 0, TDEShortcut(), false }, 00084 { ZoomOut, "ZoomOut", I18N_NOOP("Zoom Out"), Qt::CTRL+Qt::Key_Minus, 0, 0, 0, TDEShortcut(), false }, 00085 { Up, I18N_NOOP("Up"), 0, Qt::ALT+Qt::Key_Up, 0, 0, 0, TDEShortcut(), false }, 00086 { Back, I18N_NOOP("Back"), 0, Qt::ALT+Qt::Key_Left, 0, Qt::Key_Back, 0, TDEShortcut(), false }, 00087 { Forward, I18N_NOOP("Forward"), 0, Qt::ALT+Qt::Key_Right, 0, Qt::Key_Forward, 0, TDEShortcut(), false }, 00088 { Reload, I18N_NOOP("Reload"), 0, Qt::Key_F5, 0, Qt::Key_Refresh, 0, TDEShortcut(), false }, 00089 { PopupMenuContext, "PopupMenuContext", I18N_NOOP("Popup Menu Context"), Qt::Key_Menu, 0, 0, 0, TDEShortcut(), false }, 00090 { ShowMenubar, "ShowMenubar", I18N_NOOP("Show Menu Bar"), Qt::CTRL+Qt::Key_M, 0, 0, 0, TDEShortcut(), false }, 00091 { BackwardWord, "BackwardWord", I18N_NOOP("Backward Word"), Qt::CTRL+Qt::Key_Left, 0, 0, 0, TDEShortcut(), false }, 00092 { ForwardWord, "ForwardWord", I18N_NOOP("Forward Word"), Qt::CTRL+Qt::Key_Right, 0, 0, 0, TDEShortcut(), false }, 00093 { TabNext, I18N_NOOP("Activate Next Tab"), 0, Qt::CTRL+Qt::Key_Period, 0, Qt::CTRL+Qt::Key_BracketRight, 0, TDEShortcut(), false }, 00094 { TabPrev, I18N_NOOP("Activate Previous Tab"), 0, Qt::CTRL+Qt::Key_Comma, 0, Qt::CTRL+Qt::Key_BracketLeft, 0, TDEShortcut(), false }, 00095 { FullScreen, "FullScreen", I18N_NOOP("Full Screen Mode"), Qt::CTRL+Qt::SHIFT+Qt::Key_F, 0, 0, 0, TDEShortcut(), false }, 00096 {AccelNone, "Group:Help", I18N_NOOP("Help"), 0, 0, 0, 0, TDEShortcut(), false }, 00097 { Help, I18N_NOOP("Help"), 0, Qt::Key_F1, 0, 0, 0, TDEShortcut(), false }, 00098 { WhatsThis, "WhatsThis", I18N_NOOP("What's This"), Qt::SHIFT+Qt::Key_F1, 0, 0, 0, TDEShortcut(), false }, 00099 {AccelNone, "Group:TextCompletion", I18N_NOOP("Text Completion"), 0, 0, 0, 0, TDEShortcut(), false }, 00100 { TextCompletion, "TextCompletion", I18N_NOOP("Text Completion"), Qt::CTRL+Qt::Key_E, 0, 0, 0, TDEShortcut(), false }, 00101 { PrevCompletion, "PrevCompletion", I18N_NOOP("Previous Completion Match"), Qt::CTRL+Qt::Key_Up, 0, 0, 0, TDEShortcut(), false }, 00102 { NextCompletion, "NextCompletion", I18N_NOOP("Next Completion Match"), Qt::CTRL+Qt::Key_Down, 0, 0, 0, TDEShortcut(), false }, 00103 { SubstringCompletion, "SubstringCompletion", I18N_NOOP("Substring Completion"), Qt::CTRL+Qt::Key_T, 0, 0, 0, TDEShortcut(), false }, 00104 { RotateUp, "RotateUp", I18N_NOOP("Previous Item in List"), Qt::Key_Up, 0, 0, 0, TDEShortcut(), false }, 00105 { RotateDown, "RotateDown", I18N_NOOP("Next Item in List"), Qt::Key_Down, 0, 0, 0, TDEShortcut(), false }, 00106 { AccelNone, 0, 0, 0, 0, 0, 0, TDEShortcut(), false } 00107 }; 00108 00110 static TDEStdAccelInfo* infoPtr( StdAccel id ) 00111 { 00112 if( id != AccelNone ) { 00113 // Linear search. Changing the data structure doesn't seem possible 00114 // (since we need groups for the config stuff), but maybe a little 00115 // additional hashtable wouldn't hurt. 00116 for( uint i = 0; g_infoStdAccel[i].psName != 0; i++ ) { 00117 if( g_infoStdAccel[i].id == id ) 00118 return &g_infoStdAccel[i]; 00119 } 00120 } 00121 return 0; 00122 } 00123 00127 static void initialize( StdAccel id ) 00128 { 00129 TDEConfigGroupSaver saver( TDEGlobal::config(), "Shortcuts" ); 00130 TDEStdAccelInfo* pInfo = infoPtr( id ); 00131 00132 if( !pInfo ) { 00133 kdWarning(125) << "TDEStdAccel: id not found!" << endl; // -- ellis 00134 return; 00135 } 00136 00137 if( saver.config()->hasKey( pInfo->psName ) ) { 00138 TQString s = saver.config()->readEntry( pInfo->psName ); 00139 if( s != "none" ) 00140 pInfo->cut.init( s ); 00141 else 00142 pInfo->cut.clear(); 00143 } else 00144 pInfo->cut = shortcutDefault( id ); 00145 pInfo->bInitialized = true; 00146 } 00147 00148 TQString name( StdAccel id ) 00149 { 00150 TDEStdAccelInfo* pInfo = infoPtr( id ); 00151 if( !pInfo ) 00152 return TQString::null; 00153 return pInfo->psName; 00154 } 00155 00156 TQString label( StdAccel id ) 00157 { 00158 TDEStdAccelInfo* pInfo = infoPtr( id ); 00159 if( !pInfo ) 00160 return TQString::null; 00161 return i18n((pInfo->psDesc) ? pInfo->psDesc : pInfo->psName); 00162 } 00163 00164 // TODO: Add psWhatsThis entry to TDEStdAccelInfo 00165 TQString whatsThis( StdAccel /*id*/ ) 00166 { 00167 // TDEStdAccelInfo* pInfo = infoPtr( id ); 00168 // if( pInfo && pInfo->psWhatsThis ) 00169 // return i18n(pInfo->psWhatsThis); 00170 // else 00171 return TQString::null; 00172 } 00173 00174 const TDEShortcut& shortcut( StdAccel id ) 00175 { 00176 TDEStdAccelInfo* pInfo = infoPtr( id ); 00177 if( !pInfo ) 00178 return TDEShortcut::null(); 00179 00180 if( !pInfo->bInitialized ) 00181 initialize( id ); 00182 00183 return pInfo->cut; 00184 } 00185 00186 StdAccel findStdAccel( const KKeySequence& seq ) 00187 { 00188 if( !seq.isNull() ) { 00189 for( uint i = 0; g_infoStdAccel[i].psName != 0; i++ ) { 00190 StdAccel id = g_infoStdAccel[i].id; 00191 if( id != AccelNone ) { 00192 if( !g_infoStdAccel[i].bInitialized ) 00193 initialize( id ); 00194 if( g_infoStdAccel[i].cut.contains( seq ) ) 00195 return id; 00196 } 00197 } 00198 } 00199 return AccelNone; 00200 } 00201 00202 TDEShortcut shortcutDefault( StdAccel id ) 00203 { 00204 return (TDEAccelAction::useFourModifierKeys()) 00205 ? shortcutDefault4(id) : shortcutDefault3(id); 00206 } 00207 00208 TDEShortcut shortcutDefault3( StdAccel id ) 00209 { 00210 TDEShortcut cut; 00211 00212 TDEStdAccelInfo* pInfo = infoPtr( id ); 00213 if( pInfo ) { 00214 if( pInfo->cutDefault ) 00215 cut.init( pInfo->cutDefault ); 00216 // FIXME: if there is no cutDefault, then this we be made the primary 00217 // instead of alternate shortcut. 00218 if( pInfo->cutDefault3B ) 00219 cut.append( KKey(pInfo->cutDefault3B) ); 00220 } 00221 00222 return cut; 00223 } 00224 00225 TDEShortcut shortcutDefault4( StdAccel id ) 00226 { 00227 TDEShortcut cut; 00228 00229 TDEStdAccelInfo* pInfo = infoPtr( id ); 00230 if( pInfo ) { 00231 TDEStdAccelInfo& info = *pInfo; 00232 KKeySequence key2; 00233 00234 cut.init( (info.cutDefault4) ? 00235 TQKeySequence(info.cutDefault) : TQKeySequence(info.cutDefault4) ); 00236 00237 if( info.cutDefault4B ) 00238 key2.init( TQKeySequence(info.cutDefault4B) ); 00239 else if( info.cutDefault3B ) 00240 key2.init( TQKeySequence(info.cutDefault3B) ); 00241 00242 if( key2.count() ) 00243 cut.append( key2 ); 00244 } 00245 00246 return cut; 00247 } 00248 00249 #if 0 // unused 00250 void createAccelActions( TDEAccelActions& actions ) 00251 { 00252 actions.clear(); 00253 00254 for( uint i = 0; g_infoStdAccel[i].psName != 0; i++ ) { 00255 StdAccel id = g_infoStdAccel[i].id; 00256 TDEStdAccelInfo* pInfo = &g_infoStdAccel[i]; 00257 00258 if( id != AccelNone ) { 00259 actions.insert( pInfo->psName, 00260 i18n((pInfo->psDesc) ? pInfo->psDesc : pInfo->psName), 00261 TQString::null, // pInfo->psWhatsThis, 00262 shortcutDefault3(id), 00263 shortcutDefault4(id) ); 00264 } else 00265 actions.insert( pInfo->psName, i18n(pInfo->psDesc) ); 00266 } 00267 } 00268 #endif 00269 00270 const TDEShortcut& open() { return shortcut( Open ); } 00271 const TDEShortcut& openNew() { return shortcut( New ); } 00272 const TDEShortcut& close() { return shortcut( Close ); } 00273 const TDEShortcut& save() { return shortcut( Save ); } 00274 const TDEShortcut& print() { return shortcut( Print ); } 00275 const TDEShortcut& quit() { return shortcut( Quit ); } 00276 const TDEShortcut& cut() { return shortcut( Cut ); } 00277 const TDEShortcut& copy() { return shortcut( Copy ); } 00278 const TDEShortcut& paste() { return shortcut( Paste ); } 00279 const TDEShortcut& pasteSelection() { return shortcut( PasteSelection ); } 00280 const TDEShortcut& deleteWordBack() { return shortcut( DeleteWordBack ); } 00281 const TDEShortcut& deleteWordForward() { return shortcut( DeleteWordForward ); } 00282 const TDEShortcut& undo() { return shortcut( Undo ); } 00283 const TDEShortcut& redo() { return shortcut( Redo ); } 00284 const TDEShortcut& find() { return shortcut( Find ); } 00285 const TDEShortcut& findNext() { return shortcut( FindNext ); } 00286 const TDEShortcut& findPrev() { return shortcut( FindPrev ); } 00287 const TDEShortcut& replace() { return shortcut( Replace ); } 00288 const TDEShortcut& home() { return shortcut( Home ); } 00289 const TDEShortcut& end() { return shortcut( End ); } 00290 const TDEShortcut& beginningOfLine() { return shortcut( BeginningOfLine ); } 00291 const TDEShortcut& endOfLine() { return shortcut( EndOfLine ); } 00292 const TDEShortcut& prior() { return shortcut( Prior ); } 00293 const TDEShortcut& next() { return shortcut( Next ); } 00294 const TDEShortcut& backwardWord() { return shortcut( BackwardWord ); } 00295 const TDEShortcut& forwardWord() { return shortcut( ForwardWord ); } 00296 const TDEShortcut& gotoLine() { return shortcut( GotoLine ); } 00297 const TDEShortcut& addBookmark() { return shortcut( AddBookmark ); } 00298 const TDEShortcut& tabNext() { return shortcut( TabNext ); } 00299 const TDEShortcut& tabPrev() { return shortcut( TabPrev ); } 00300 const TDEShortcut& fullScreen() { return shortcut( FullScreen ); } 00301 const TDEShortcut& zoomIn() { return shortcut( ZoomIn ); } 00302 const TDEShortcut& zoomOut() { return shortcut( ZoomOut ); } 00303 const TDEShortcut& help() { return shortcut( Help ); } 00304 const TDEShortcut& completion() { return shortcut( TextCompletion ); } 00305 const TDEShortcut& prevCompletion() { return shortcut( PrevCompletion ); } 00306 const TDEShortcut& nextCompletion() { return shortcut( NextCompletion ); } 00307 const TDEShortcut& rotateUp() { return shortcut( RotateUp ); } 00308 const TDEShortcut& rotateDown() { return shortcut( RotateDown ); } 00309 const TDEShortcut& substringCompletion() { return shortcut( SubstringCompletion ); } 00310 const TDEShortcut& popupMenuContext() { return shortcut( PopupMenuContext ); } 00311 const TDEShortcut& whatsThis() { return shortcut( WhatsThis ); } 00312 const TDEShortcut& reload() { return shortcut( Reload ); } 00313 const TDEShortcut& selectAll() { return shortcut( SelectAll ); } 00314 const TDEShortcut& up() { return shortcut( Up ); } 00315 const TDEShortcut& back() { return shortcut( Back ); } 00316 const TDEShortcut& forward() { return shortcut( Forward ); } 00317 const TDEShortcut& showMenubar() { return shortcut( ShowMenubar ); } 00318 00319 //--------------------------------------------------------------------- 00320 // ShortcutList 00321 //--------------------------------------------------------------------- 00322 00323 ShortcutList::ShortcutList() 00324 { } 00325 00326 ShortcutList::~ShortcutList() 00327 { } 00328 00329 uint ShortcutList::count() const 00330 { 00331 static uint g_nAccels = 0; 00332 if( g_nAccels == 0 ) { 00333 for( ; g_infoStdAccel[g_nAccels].psName != 0; g_nAccels++ ) 00334 ; 00335 } 00336 return g_nAccels; 00337 } 00338 00339 TQString ShortcutList::name( uint i ) const 00340 { return g_infoStdAccel[i].psName; } 00341 00342 TQString ShortcutList::label( uint i ) const 00343 { return i18n((g_infoStdAccel[i].psDesc) ? g_infoStdAccel[i].psDesc : g_infoStdAccel[i].psName); } 00344 00345 TQString ShortcutList::whatsThis( uint ) const 00346 { return TQString::null; } 00347 00348 const TDEShortcut& ShortcutList::shortcut( uint i ) const 00349 { 00350 if( !g_infoStdAccel[i].bInitialized ) 00351 initialize( g_infoStdAccel[i].id ); 00352 return g_infoStdAccel[i].cut; 00353 } 00354 00355 const TDEShortcut& ShortcutList::shortcutDefault( uint i ) const 00356 { 00357 static TDEShortcut cut; 00358 cut = TDEStdAccel::shortcutDefault( g_infoStdAccel[i].id ); 00359 return cut; 00360 } 00361 00362 bool ShortcutList::isConfigurable( uint i ) const 00363 { return (g_infoStdAccel[i].id != AccelNone); } 00364 00365 bool ShortcutList::setShortcut( uint i, const TDEShortcut& cut ) 00366 { g_infoStdAccel[i].cut = cut; return true; } 00367 00368 TQVariant ShortcutList::getOther( Other, uint ) const 00369 { return TQVariant(); } 00370 00371 bool ShortcutList::setOther( Other, uint, TQVariant ) 00372 { return false; } 00373 00374 bool ShortcutList::save() const 00375 { 00376 return writeSettings( TQString::null, 0, false, true ); 00377 } 00378 00379 TDECORE_EXPORT TQString action(StdAccel id) 00380 { return name(id); } 00381 TDECORE_EXPORT TQString description(StdAccel id) 00382 { return label(id); } 00383 TDECORE_EXPORT int key(StdAccel id) 00384 { return shortcut(id).keyCodeQt(); } 00385 TDECORE_EXPORT int defaultKey(StdAccel id) 00386 { return shortcutDefault(id).keyCodeQt(); } 00387 00388 TDECORE_EXPORT bool isEqual(const TQKeyEvent* ev, int keyQt) 00389 { 00390 KKey key1( ev ), key2( keyQt ); 00391 return key1 == key2; 00392 } 00393 00394 } 00395 00396 #undef __KSTDACCEL_CPP_