LCOV - code coverage report
Current view: top level - vcl/source/window - mnemonic.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 117 167 70.1 %
Date: 2014-11-03 Functions: 9 9 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <string.h>
      21             : #include <vcl/svapp.hxx>
      22             : #include <vcl/settings.hxx>
      23             : #include <vcl/mnemonic.hxx>
      24             : 
      25             : #include <vcl/unohelp.hxx>
      26             : #include <com/sun/star/i18n/XCharacterClassification.hpp>
      27             : #include <i18nlangtag/mslangid.hxx>
      28             : 
      29             : using namespace ::com::sun::star;
      30             : 
      31         148 : MnemonicGenerator::MnemonicGenerator()
      32             : {
      33         148 :     memset( maMnemonics, 1, sizeof( maMnemonics ) );
      34         148 : }
      35             : 
      36        1836 : sal_uInt16 MnemonicGenerator::ImplGetMnemonicIndex( sal_Unicode c )
      37             : {
      38             :     static sal_uInt16 const aImplMnemonicRangeTab[MNEMONIC_RANGES*2] =
      39             :     {
      40             :         MNEMONIC_RANGE_1_START, MNEMONIC_RANGE_1_END,
      41             :         MNEMONIC_RANGE_2_START, MNEMONIC_RANGE_2_END,
      42             :         MNEMONIC_RANGE_3_START, MNEMONIC_RANGE_3_END,
      43             :         MNEMONIC_RANGE_4_START, MNEMONIC_RANGE_4_END
      44             :     };
      45             : 
      46        1836 :     sal_uInt16 nMnemonicIndex = 0;
      47        4062 :     for ( sal_uInt16 i = 0; i < MNEMONIC_RANGES; i++ )
      48             :     {
      49        7344 :         if ( (c >= aImplMnemonicRangeTab[i*2]) &&
      50        3412 :              (c <= aImplMnemonicRangeTab[i*2+1]) )
      51        1706 :             return nMnemonicIndex+c-aImplMnemonicRangeTab[i*2];
      52             : 
      53        2226 :         nMnemonicIndex += aImplMnemonicRangeTab[i*2+1]-aImplMnemonicRangeTab[i*2];
      54             :     }
      55             : 
      56         130 :     return MNEMONIC_INDEX_NOTFOUND;
      57             : }
      58             : 
      59        1868 : sal_Unicode MnemonicGenerator::ImplFindMnemonic( const OUString& rKey )
      60             : {
      61        1868 :     sal_Int32 nIndex = 0;
      62        3736 :     while ( (nIndex = rKey.indexOf( MNEMONIC_CHAR, nIndex )) != -1 )
      63             :     {
      64         714 :         sal_Unicode cMnemonic = rKey[ nIndex+1 ];
      65         714 :         if ( cMnemonic != MNEMONIC_CHAR )
      66         714 :             return cMnemonic;
      67           0 :         nIndex += 2;
      68             :     }
      69             : 
      70        1154 :     return 0;
      71             : }
      72             : 
      73        1402 : void MnemonicGenerator::RegisterMnemonic( const OUString& rKey )
      74             : {
      75        1402 :     const ::com::sun::star::lang::Locale& rLocale = Application::GetSettings().GetUILanguageTag().getLocale();
      76        1402 :     uno::Reference < i18n::XCharacterClassification > xCharClass = GetCharClass();
      77             : 
      78             :     // Don't crash even when we don't have access to i18n service
      79        1402 :     if ( !xCharClass.is() )
      80        1402 :         return;
      81             : 
      82        2804 :     OUString aKey = xCharClass->toUpper( rKey, 0, rKey.getLength(), rLocale );
      83             : 
      84             :     // If we find a Mnemonic, set the flag. In other case count the
      85             :     // characters, because we need this to set most as possible
      86             :     // Mnemonics
      87        1402 :     sal_Unicode cMnemonic = ImplFindMnemonic( aKey );
      88        1402 :     if ( cMnemonic )
      89             :     {
      90         364 :         sal_uInt16 nMnemonicIndex = ImplGetMnemonicIndex( cMnemonic );
      91         364 :         if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
      92         354 :             maMnemonics[nMnemonicIndex] = 0;
      93             :     }
      94             :     else
      95             :     {
      96        1038 :         sal_Int32 nIndex = 0;
      97        1038 :         sal_Int32 nLen = aKey.getLength();
      98        3146 :         while ( nIndex < nLen )
      99             :         {
     100        1070 :             sal_Unicode c = aKey[ nIndex ];
     101             : 
     102        1070 :             sal_uInt16 nMnemonicIndex = ImplGetMnemonicIndex( c );
     103        1070 :             if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
     104             :             {
     105         968 :                 if ( maMnemonics[nMnemonicIndex] && (maMnemonics[nMnemonicIndex] < 0xFF) )
     106         718 :                     maMnemonics[nMnemonicIndex]++;
     107             :             }
     108             : 
     109        1070 :             nIndex++;
     110             :         }
     111        1402 :     }
     112             : }
     113             : 
     114         550 : OUString MnemonicGenerator::CreateMnemonic( const OUString& _rKey )
     115             : {
     116         550 :     if ( _rKey.isEmpty() || ImplFindMnemonic( _rKey ) )
     117         434 :         return _rKey;
     118             : 
     119         116 :     const ::com::sun::star::lang::Locale& rLocale = Application::GetSettings().GetUILanguageTag().getLocale();
     120         116 :     uno::Reference < i18n::XCharacterClassification > xCharClass = GetCharClass();
     121             : 
     122             :     // Don't crash even when we don't have access to i18n service
     123         116 :     if ( !xCharClass.is() )
     124           0 :         return _rKey;
     125             : 
     126         232 :     OUString aKey = xCharClass->toUpper( _rKey, 0, _rKey.getLength(), rLocale );
     127             : 
     128         116 :     bool bChanged = false;
     129         116 :     sal_Int32 nLen = aKey.getLength();
     130             : 
     131         116 :     bool bCJK = MsLangId::isCJK(Application::GetSettings().GetUILanguageTag().getLanguageType());
     132             : 
     133             :     // #107889# in CJK versions ALL strings (even those that contain latin characters)
     134             :     // will get mnemonics in the form: xyz (M)
     135             :     // thus steps 1) and 2) are skipped for CJK locales
     136             : 
     137             :     // #110720#, avoid CJK-style mnemonics for latin-only strings that do not contain useful mnemonic chars
     138         116 :     if( bCJK )
     139             :     {
     140           0 :         bool bLatinOnly = true;
     141           0 :         bool bMnemonicIndexFound = false;
     142             :         sal_Unicode     c;
     143             :         sal_Int32       nIndex;
     144             : 
     145           0 :         for( nIndex=0; nIndex < nLen; nIndex++ )
     146             :         {
     147           0 :             c = aKey[ nIndex ];
     148           0 :             if ( ((c >= 0x3000) && (c <= 0xD7FF)) ||    // cjk
     149           0 :                  ((c >= 0xFF61) && (c <= 0xFFDC)) )     // halfwidth forms
     150             :             {
     151           0 :                 bLatinOnly = false;
     152           0 :                 break;
     153             :             }
     154           0 :             if( ImplGetMnemonicIndex( c ) != MNEMONIC_INDEX_NOTFOUND )
     155           0 :                 bMnemonicIndexFound = true;
     156             :         }
     157           0 :         if( bLatinOnly && !bMnemonicIndexFound )
     158           0 :             return _rKey;
     159             :     }
     160             : 
     161         232 :     OUString        rKey(_rKey);
     162         116 :     int             nCJK = 0;
     163             :     sal_uInt16      nMnemonicIndex;
     164             :     sal_Unicode     c;
     165         116 :     sal_Int32       nIndex = 0;
     166         116 :     if( !bCJK )
     167             :     {
     168             :         // 1) first try the first character of a word
     169          44 :         do
     170             :         {
     171         124 :             c = aKey[ nIndex ];
     172             : 
     173         124 :             if ( nCJK != 2 )
     174             :             {
     175         116 :                 if ( ((c >= 0x3000) && (c <= 0xD7FF)) ||    // cjk
     176           0 :                     ((c >= 0xFF61) && (c <= 0xFFDC)) )     // halfwidth forms
     177           0 :                     nCJK = 1;
     178         116 :                 else if ( ((c >= 0x0030) && (c <= 0x0039)) || // digits
     179         116 :                         ((c >= 0x0041) && (c <= 0x005A)) || // latin capitals
     180           0 :                         ((c >= 0x0061) && (c <= 0x007A)) || // latin small
     181           0 :                         ((c >= 0x0370) && (c <= 0x037F)) || // greek numeral signs
     182           0 :                         ((c >= 0x0400) && (c <= 0x04FF)) )  // cyrillic
     183         116 :                     nCJK = 2;
     184             :             }
     185             : 
     186         124 :             nMnemonicIndex = ImplGetMnemonicIndex( c );
     187         124 :             if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
     188             :             {
     189         124 :                 if ( maMnemonics[nMnemonicIndex] )
     190             :                 {
     191          80 :                     maMnemonics[nMnemonicIndex] = 0;
     192          80 :                     rKey = rKey.replaceAt( nIndex, 0, OUString(MNEMONIC_CHAR) );
     193          80 :                     bChanged = true;
     194          80 :                     break;
     195             :                 }
     196             :             }
     197             : 
     198             :             // Search for next word
     199          44 :             nIndex++;
     200         324 :             while ( nIndex < nLen )
     201             :             {
     202         244 :                 c = aKey[ nIndex ];
     203         244 :                 if ( c == ' ' )
     204           8 :                     break;
     205         236 :                 nIndex++;
     206             :             }
     207          44 :             nIndex++;
     208             :         }
     209             :         while ( nIndex < nLen );
     210             : 
     211             :         // 2) search for a unique/uncommon character
     212         116 :         if ( !bChanged )
     213             :         {
     214          36 :             sal_uInt16      nBestCount = 0xFFFF;
     215          36 :             sal_uInt16      nBestMnemonicIndex = 0;
     216          36 :             sal_Int32       nBestIndex = 0;
     217          36 :             nIndex = 0;
     218         274 :             do
     219             :             {
     220         278 :                 c = aKey[ nIndex ];
     221         278 :                 nMnemonicIndex = ImplGetMnemonicIndex( c );
     222         278 :                 if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
     223             :                 {
     224         260 :                     if ( maMnemonics[nMnemonicIndex] )
     225             :                     {
     226          44 :                         if ( maMnemonics[nMnemonicIndex] < nBestCount )
     227             :                         {
     228          26 :                             nBestCount = maMnemonics[nMnemonicIndex];
     229          26 :                             nBestIndex = nIndex;
     230          26 :                             nBestMnemonicIndex = nMnemonicIndex;
     231          26 :                             if ( nBestCount == 2 )
     232           4 :                                 break;
     233             :                         }
     234             :                     }
     235             :                 }
     236             : 
     237         274 :                 nIndex++;
     238             :             }
     239             :             while ( nIndex < nLen );
     240             : 
     241          36 :             if ( nBestCount != 0xFFFF )
     242             :             {
     243          16 :                 maMnemonics[nBestMnemonicIndex] = 0;
     244          16 :                 rKey = rKey.replaceAt( nBestIndex, 0, OUString(MNEMONIC_CHAR) );
     245          16 :                 bChanged = true;
     246             :             }
     247             :         }
     248             :     }
     249             :     else
     250           0 :         nCJK = 1;
     251             : 
     252             :     // 3) Add English Mnemonic for CJK Text
     253         116 :     if ( !bChanged && (nCJK == 1) && !rKey.isEmpty() )
     254             :     {
     255             :         // Append Ascii Mnemonic
     256           0 :         for ( c = MNEMONIC_RANGE_2_START; c <= MNEMONIC_RANGE_2_END; c++ )
     257             :         {
     258           0 :             nMnemonicIndex = ImplGetMnemonicIndex( c );
     259           0 :             if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
     260             :             {
     261           0 :                 if ( maMnemonics[nMnemonicIndex] )
     262             :                 {
     263           0 :                     maMnemonics[nMnemonicIndex] = 0;
     264             :                     OUString aStr = OUStringBuffer().
     265           0 :                         append('(').append(MNEMONIC_CHAR).append(c).
     266           0 :                         append(')').makeStringAndClear();
     267           0 :                     nIndex = rKey.getLength();
     268           0 :                     if( nIndex >= 2 )
     269             :                     {
     270           0 :                         if ( ( rKey[nIndex-2] == '>'    && rKey[nIndex-1] == '>' ) ||
     271           0 :                              ( rKey[nIndex-2] == 0xFF1E && rKey[nIndex-1] == 0xFF1E ) )
     272           0 :                             nIndex -= 2;
     273             :                     }
     274           0 :                     if( nIndex >= 3 )
     275             :                     {
     276           0 :                         if ( ( rKey[nIndex-3] == '.'    && rKey[nIndex-2] == '.'    && rKey[nIndex-1] == '.' ) ||
     277           0 :                              ( rKey[nIndex-3] == 0xFF0E && rKey[nIndex-2] == 0xFF0E && rKey[nIndex-1] == 0xFF0E ) )
     278           0 :                             nIndex -= 3;
     279             :                     }
     280           0 :                     if( nIndex >= 1)
     281             :                     {
     282           0 :                         sal_Unicode cLastChar = rKey[ nIndex-1 ];
     283           0 :                         if ( (cLastChar == ':') || (cLastChar == 0xFF1A) ||
     284           0 :                             (cLastChar == '.') || (cLastChar == 0xFF0E) ||
     285           0 :                             (cLastChar == '?') || (cLastChar == 0xFF1F) ||
     286             :                             (cLastChar == ' ') )
     287           0 :                             nIndex--;
     288             :                     }
     289           0 :                     rKey = rKey.replaceAt( nIndex, 0, aStr );
     290           0 :                     bChanged = true;
     291           0 :                     break;
     292             :                 }
     293             :             }
     294             :         }
     295             :     }
     296             : 
     297             : // #i87415# Duplicates mnemonics are bad for consistent keyboard accessibility
     298             : // It's probably better to not have mnemonics for some widgets, than to have ambiguous ones.
     299             : //    if( ! bChanged )
     300             : //    {
     301             : //        /*
     302             : //         *  #97809# if all else fails use the first character of a word
     303             : //         *  anyway and live with duplicate mnemonics
     304             : //         */
     305             : //        nIndex = 0;
     306             : //        do
     307             : //        {
     308             : //            c = aKey.GetChar( nIndex );
     309             : 
     310             : //            nMnemonicIndex = ImplGetMnemonicIndex( c );
     311             : //            if ( nMnemonicIndex != MNEMONIC_INDEX_NOTFOUND )
     312             : //            {
     313             : //                maMnemonics[nMnemonicIndex] = 0;
     314             : //                rKey.Insert( MNEMONIC_CHAR, nIndex );
     315             : //                bChanged = true;
     316             : //                break;
     317             : //            }
     318             : 
     319             : //            // Search for next word
     320             : //            do
     321             : //            {
     322             : //                nIndex++;
     323             : //                c = aKey.GetChar( nIndex );
     324             : //                if ( c == ' ' )
     325             : //                    break;
     326             : //            }
     327             : //            while ( nIndex < nLen );
     328             : //            nIndex++;
     329             : //        }
     330             : //        while ( nIndex < nLen );
     331             : //    }
     332             : 
     333         232 :     return rKey;
     334             : }
     335             : 
     336        1518 : uno::Reference< i18n::XCharacterClassification > MnemonicGenerator::GetCharClass()
     337             : {
     338        1518 :     if ( !mxCharClass.is() )
     339         116 :         mxCharClass = vcl::unohelper::CreateCharacterClassification();
     340        1518 :     return mxCharClass;
     341             : }
     342             : 
     343      294175 : OUString MnemonicGenerator::EraseAllMnemonicChars( const OUString& rStr )
     344             : {
     345      294175 :     OUString    aStr = rStr;
     346      294175 :     sal_Int32   nLen = aStr.getLength();
     347      294175 :     sal_Int32   i    = 0;
     348             : 
     349     4873335 :     while ( i < nLen )
     350             :     {
     351     4284985 :         if ( aStr[ i ] == '~' )
     352             :         {
     353             :             // check for CJK-style mnemonic
     354        4701 :             if( i > 0 && (i+2) < nLen )
     355             :             {
     356        2464 :                 sal_Unicode c = aStr[i+1];
     357        4928 :                 if( aStr[ i-1 ] == '(' &&
     358           0 :                     aStr[ i+2 ] == ')' &&
     359        2464 :                     c >= MNEMONIC_RANGE_2_START && c <= MNEMONIC_RANGE_2_END )
     360             :                 {
     361           0 :                     aStr = aStr.replaceAt( i-1, 4, "" );
     362           0 :                     nLen -= 4;
     363           0 :                     i--;
     364           0 :                     continue;
     365             :                 }
     366             :             }
     367             : 
     368             :             // remove standard mnemonics
     369        4701 :             aStr = aStr.replaceAt( i, 1, "" );
     370        4701 :             nLen--;
     371             :         }
     372             :         else
     373     4280284 :             i++;
     374             :     }
     375             : 
     376      294175 :     return aStr;
     377        1233 : }
     378             : 
     379             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10