LCOV - code coverage report
Current view: top level - libreoffice/basctl/source/dlged - managelang.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 212 0.0 %
Date: 2012-12-27 Functions: 0 23 0.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 "basidesh.hxx"
      21             : #include "basobj.hxx"
      22             : #include "iderdll.hxx"
      23             : #include "iderid.hxx"
      24             : #include "localizationmgr.hxx"
      25             : #include "managelang.hxx"
      26             : 
      27             : #include "dlgresid.hrc"
      28             : #include "helpid.hrc"
      29             : #include "managelang.hrc"
      30             : 
      31             : #include <com/sun/star/i18n/Boundary.hpp>
      32             : #include <com/sun/star/i18n/WordType.hpp>
      33             : #include <com/sun/star/i18n/XBreakIterator.hpp>
      34             : #include <editeng/unolingu.hxx>
      35             : #include <sfx2/bindings.hxx>
      36             : #include <svtools/langtab.hxx>
      37             : #include <svx/langbox.hxx>
      38             : #include <vcl/unohelp.hxx>
      39             : #include <vcl/svapp.hxx>
      40             : #include <vcl/msgbox.hxx>
      41             : 
      42             : namespace basctl
      43             : {
      44             : 
      45             : using namespace ::com::sun::star::i18n;
      46             : using namespace ::com::sun::star::lang;
      47             : using namespace ::com::sun::star::resource;
      48             : using namespace ::com::sun::star::uno;
      49             : 
      50           0 : bool localesAreEqual( const Locale& rLocaleLeft, const Locale& rLocaleRight )
      51             : {
      52           0 :     bool bRet = ( rLocaleLeft.Language.equals( rLocaleRight.Language ) &&
      53           0 :                   rLocaleLeft.Country.equals( rLocaleRight.Country ) &&
      54           0 :                   rLocaleLeft.Variant.equals( rLocaleRight.Variant ) );
      55           0 :     return bRet;
      56             : }
      57             : 
      58             : namespace {
      59           0 :     long getLongestWordWidth( const OUString& rText, const Window& rWin )
      60             :     {
      61           0 :         long nWidth = 0;
      62           0 :         Reference< XBreakIterator > xBreakIter( vcl::unohelper::CreateBreakIterator() );
      63           0 :         sal_Int32 nStartPos = 0;
      64           0 :         const Locale aLocale = Application::GetSettings().GetUILanguageTag().getLocale();
      65           0 :         Boundary aBoundary = xBreakIter->getWordBoundary(
      66           0 :             rText, nStartPos, aLocale, WordType::ANYWORD_IGNOREWHITESPACES, true );
      67             : 
      68           0 :         while ( aBoundary.startPos != aBoundary.endPos )
      69             :         {
      70           0 :             nStartPos = aBoundary.endPos;
      71           0 :             OUString sWord(rText.copy(aBoundary.startPos, aBoundary.endPos - aBoundary.startPos));
      72           0 :             long nTemp = rWin.GetCtrlTextWidth( sWord );
      73           0 :             if ( nTemp > nWidth )
      74           0 :                 nWidth = nTemp;
      75           0 :             aBoundary = xBreakIter->nextWord(
      76           0 :                 rText, nStartPos, aLocale, WordType::ANYWORD_IGNOREWHITESPACES );
      77           0 :         }
      78             : 
      79           0 :         return nWidth;
      80             :     }
      81             : }
      82             : 
      83           0 : ManageLanguageDialog::ManageLanguageDialog( Window* pParent, boost::shared_ptr<LocalizationMgr> _pLMgr ) :
      84             :     ModalDialog( pParent, IDEResId( RID_DLG_MANAGE_LANGUAGE ) ),
      85             :     m_aLanguageFT       ( this, IDEResId( FT_LANGUAGE ) ),
      86             :     m_aLanguageLB       ( this, IDEResId( LB_LANGUAGE ) ),
      87             :     m_aAddPB            ( this, IDEResId( PB_ADD_LANG ) ),
      88             :     m_aDeletePB         ( this, IDEResId( PB_DEL_LANG ) ),
      89             :     m_aMakeDefPB        ( this, IDEResId( PB_MAKE_DEFAULT ) ),
      90             :     m_aInfoFT           ( this, IDEResId( FT_INFO ) ),
      91             :     m_aBtnLine          ( this, IDEResId( FL_BUTTONS ) ),
      92             :     m_aHelpBtn          ( this, IDEResId( PB_HELP ) ),
      93             :     m_aCloseBtn         ( this, IDEResId( PB_CLOSE ) ),
      94             :     m_pLocalizationMgr  ( _pLMgr ),
      95             :     m_sDefLangStr       (IDE_RESSTR(STR_DEF_LANG)),
      96             :     m_sDeleteStr        (IDE_RESSTR(STR_DELETE)),
      97           0 :     m_sCreateLangStr    (IDE_RESSTR(STR_CREATE_LANG))
      98             : {
      99           0 :     FreeResource();
     100             : 
     101           0 :     Init();
     102           0 :     FillLanguageBox();
     103           0 :     SelectHdl( NULL );
     104           0 : }
     105             : 
     106           0 : ManageLanguageDialog::~ManageLanguageDialog()
     107             : {
     108           0 :     ClearLanguageBox();
     109           0 : }
     110             : 
     111           0 : void ManageLanguageDialog::Init()
     112             : {
     113             :     // get current IDE
     114           0 :     Shell* pShell = GetShell();
     115           0 :     OUString sLibName = pShell->GetCurLibName();
     116             :     // set dialog title with library name
     117           0 :     OUString sText = GetText();
     118           0 :     sText = sText.replaceAll("$1", sLibName);
     119           0 :     SetText( sText );
     120             :     // set handler
     121           0 :     m_aAddPB.SetClickHdl( LINK( this, ManageLanguageDialog, AddHdl ) );
     122           0 :     m_aDeletePB.SetClickHdl( LINK( this, ManageLanguageDialog, DeleteHdl ) );
     123           0 :     m_aMakeDefPB.SetClickHdl( LINK( this, ManageLanguageDialog, MakeDefHdl ) );
     124           0 :     m_aLanguageLB.SetSelectHdl( LINK( this, ManageLanguageDialog, SelectHdl ) );
     125             : 
     126           0 :     m_aLanguageLB.EnableMultiSelection( true );
     127           0 :     CalcInfoSize();
     128           0 : }
     129             : 
     130           0 : void ManageLanguageDialog::CalcInfoSize()
     131             : {
     132           0 :     OUString sInfoStr = m_aInfoFT.GetText();
     133           0 :     long nInfoWidth = m_aInfoFT.GetSizePixel().Width();
     134           0 :     long nLongWord = getLongestWordWidth( sInfoStr, m_aInfoFT );
     135           0 :     long nTxtWidth = m_aInfoFT.GetCtrlTextWidth( sInfoStr ) + nLongWord;
     136           0 :     long nLines = ( nTxtWidth / nInfoWidth ) + 1;
     137           0 :     if ( nLines > INFO_LINES_COUNT )
     138             :     {
     139           0 :         Size aFTSize = m_aLanguageFT.GetSizePixel();
     140           0 :         Size aSize = m_aInfoFT.GetSizePixel();
     141           0 :         long nNewHeight = aFTSize.Height() * nLines;
     142           0 :         long nDelta = nNewHeight - aSize.Height();
     143           0 :         aSize.Height() = nNewHeight;
     144           0 :         m_aInfoFT.SetSizePixel( aSize );
     145             : 
     146           0 :         aSize = m_aLanguageLB.GetSizePixel();
     147           0 :         aSize.Height() -= nDelta;
     148           0 :         m_aLanguageLB.SetSizePixel( aSize );
     149             : 
     150           0 :         Point aNewPos = m_aInfoFT.GetPosPixel();
     151           0 :         aNewPos.Y() -= nDelta;
     152           0 :         m_aInfoFT.SetPosPixel( aNewPos );
     153           0 :         aNewPos = m_aMakeDefPB.GetPosPixel();
     154           0 :         aNewPos.Y() -= nDelta;
     155           0 :         m_aMakeDefPB.SetPosPixel( aNewPos );
     156           0 :     }
     157           0 : }
     158             : 
     159           0 : void ManageLanguageDialog::FillLanguageBox()
     160             : {
     161             :     DBG_ASSERT( m_pLocalizationMgr, "ManageLanguageDialog::FillLanguageBox(): no localization manager" );
     162             : 
     163           0 :     if ( m_pLocalizationMgr->isLibraryLocalized() )
     164             :     {
     165           0 :         SvtLanguageTable aLangTable;
     166           0 :         Locale aDefaultLocale = m_pLocalizationMgr->getStringResourceManager()->getDefaultLocale();
     167           0 :         Sequence< Locale > aLocaleSeq = m_pLocalizationMgr->getStringResourceManager()->getLocales();
     168           0 :         const Locale* pLocale = aLocaleSeq.getConstArray();
     169           0 :         sal_Int32 i, nCount = aLocaleSeq.getLength();
     170           0 :         for ( i = 0;  i < nCount;  ++i )
     171             :         {
     172           0 :             bool bIsDefault = localesAreEqual( aDefaultLocale, pLocale[i] );
     173           0 :             LanguageType eLangType = LanguageTag( pLocale[i] ).getLanguageType();
     174           0 :             OUStringBuffer sLanguageBuf(aLangTable.GetString( eLangType ));
     175           0 :             if ( bIsDefault )
     176             :             {
     177           0 :                 sLanguageBuf.append(' ');
     178           0 :                 sLanguageBuf.append(m_sDefLangStr);
     179             :             }
     180           0 :             OUString sLanguage(sLanguageBuf.makeStringAndClear());
     181           0 :             sal_uInt16 nPos = m_aLanguageLB.InsertEntry( sLanguage );
     182           0 :             m_aLanguageLB.SetEntryData( nPos, new LanguageEntry( sLanguage, pLocale[i], bIsDefault ) );
     183           0 :         }
     184             :     }
     185             :     else
     186           0 :         m_aLanguageLB.InsertEntry( m_sCreateLangStr );
     187           0 : }
     188             : 
     189           0 : void ManageLanguageDialog::ClearLanguageBox()
     190             : {
     191           0 :     sal_uInt16 i, nCount = m_aLanguageLB.GetEntryCount();
     192           0 :     for ( i = 0; i < nCount; ++i )
     193             :     {
     194           0 :         LanguageEntry* pEntry = (LanguageEntry*)( m_aLanguageLB.GetEntryData(i) );
     195           0 :         delete pEntry;
     196             :     }
     197           0 :     m_aLanguageLB.Clear();
     198           0 : }
     199             : 
     200           0 : IMPL_LINK_NOARG(ManageLanguageDialog, AddHdl)
     201             : {
     202           0 :     SetDefaultLanguageDialog aDlg( this, m_pLocalizationMgr );
     203           0 :     if ( RET_OK == aDlg.Execute() )
     204             :     {
     205             :         // add new locales
     206           0 :         Sequence< Locale > aLocaleSeq = aDlg.GetLocales();
     207           0 :         m_pLocalizationMgr->handleAddLocales( aLocaleSeq );
     208             :         // update listbox
     209           0 :         ClearLanguageBox();
     210           0 :         FillLanguageBox();
     211             : 
     212           0 :         if (SfxBindings* pBindings = GetBindingsPtr())
     213           0 :             pBindings->Invalidate( SID_BASICIDE_CURRENT_LANG );
     214             :     }
     215           0 :     return 1;
     216             : }
     217             : 
     218           0 : IMPL_LINK_NOARG(ManageLanguageDialog, DeleteHdl)
     219             : {
     220           0 :     QueryBox aQBox( this, IDEResId( RID_QRYBOX_LANGUAGE ) );
     221           0 :     aQBox.SetButtonText( RET_OK, m_sDeleteStr );
     222           0 :     if ( aQBox.Execute() == RET_OK )
     223             :     {
     224           0 :         sal_uInt16 i, nCount = m_aLanguageLB.GetSelectEntryCount();
     225           0 :         sal_uInt16 nPos = m_aLanguageLB.GetSelectEntryPos();
     226             :         // remove locales
     227           0 :         Sequence< Locale > aLocaleSeq( nCount );
     228           0 :         for ( i = 0; i < nCount; ++i )
     229             :         {
     230           0 :             sal_uInt16 nSelPos = m_aLanguageLB.GetSelectEntryPos(i);
     231           0 :             LanguageEntry* pEntry = (LanguageEntry*)( m_aLanguageLB.GetEntryData( nSelPos ) );
     232           0 :             if ( pEntry )
     233           0 :                 aLocaleSeq[i] = pEntry->m_aLocale;
     234             :         }
     235           0 :         m_pLocalizationMgr->handleRemoveLocales( aLocaleSeq );
     236             :         // update listbox
     237           0 :         ClearLanguageBox();
     238           0 :         FillLanguageBox();
     239             :         // reset selection
     240           0 :         nCount = m_aLanguageLB.GetEntryCount();
     241           0 :         if ( nCount <= nPos )
     242           0 :             nPos = nCount - 1;
     243           0 :         m_aLanguageLB.SelectEntryPos( nPos );
     244           0 :         SelectHdl( NULL );
     245             :     }
     246           0 :     return 1;
     247             : }
     248             : 
     249           0 : IMPL_LINK_NOARG(ManageLanguageDialog, MakeDefHdl)
     250             : {
     251           0 :     sal_uInt16 nPos = m_aLanguageLB.GetSelectEntryPos();
     252           0 :     LanguageEntry* pSelectEntry = (LanguageEntry*)( m_aLanguageLB.GetEntryData( nPos ) );
     253           0 :     if ( pSelectEntry && !pSelectEntry->m_bIsDefault )
     254             :     {
     255             :         // set new default entry
     256           0 :         m_pLocalizationMgr->handleSetDefaultLocale( pSelectEntry->m_aLocale );
     257             :         // update Listbox
     258           0 :         ClearLanguageBox();
     259           0 :         FillLanguageBox();
     260             :         // reset selection
     261           0 :         m_aLanguageLB.SelectEntryPos( nPos );
     262           0 :         SelectHdl( NULL );
     263             :     }
     264             : 
     265           0 :     return 1;
     266             : }
     267             : 
     268           0 : IMPL_LINK_NOARG(ManageLanguageDialog, SelectHdl)
     269             : {
     270           0 :     sal_uInt16 nCount = m_aLanguageLB.GetEntryCount();
     271             :     bool bEmpty = ( !nCount ||
     272           0 :                     m_aLanguageLB.GetEntryPos( m_sCreateLangStr ) != LISTBOX_ENTRY_NOTFOUND );
     273           0 :     bool bSelect = ( m_aLanguageLB.GetSelectEntryPos() != LISTBOX_ENTRY_NOTFOUND );
     274           0 :     bool bEnable = ( !bEmpty && bSelect != false );
     275             : 
     276           0 :     m_aDeletePB.Enable( bEnable != false );
     277           0 :     m_aMakeDefPB.Enable( bEnable != false && nCount > 1 && m_aLanguageLB.GetSelectEntryCount() == 1 );
     278             : 
     279           0 :     return 1;
     280             : }
     281             : 
     282             : // class SetDefaultLanguageDialog -----------------------------------------------
     283             : 
     284           0 : SetDefaultLanguageDialog::SetDefaultLanguageDialog( Window* pParent, boost::shared_ptr<LocalizationMgr> _pLMgr ) :
     285             : 
     286             :     ModalDialog( pParent, IDEResId( RID_DLG_SETDEF_LANGUAGE ) ),
     287             :     m_aLanguageFT   ( this, IDEResId( FT_DEF_LANGUAGE ) ),
     288           0 :     m_pLanguageLB   ( new SvxLanguageBox( this, IDEResId( LB_DEF_LANGUAGE ) ) ),
     289             :     m_pCheckLangLB  ( NULL ),
     290             :     m_aInfoFT       ( this, IDEResId( FT_DEF_INFO ) ),
     291             :     m_aBtnLine      ( this, IDEResId( FL_DEF_BUTTONS ) ),
     292             :     m_aOKBtn        ( this, IDEResId( PB_DEF_OK ) ),
     293             :     m_aCancelBtn    ( this, IDEResId( PB_DEF_CANCEL ) ),
     294             :     m_aHelpBtn      ( this, IDEResId( PB_DEF_HELP ) ),
     295           0 :     m_pLocalizationMgr( _pLMgr )
     296             : {
     297           0 :     if ( m_pLocalizationMgr->isLibraryLocalized() )
     298             :     {
     299             :         // change to "Add Interface Language" mode
     300           0 :         SetHelpId( HID_BASICIDE_ADDNEW_LANGUAGE );
     301           0 :         m_pCheckLangLB = new SvxCheckListBox( this, IDEResId( LB_ADD_LANGUAGE ) );
     302           0 :         SetText( IDE_RESSTR(STR_ADDLANG_TITLE) );
     303           0 :         m_aLanguageFT.SetText( IDE_RESSTR(STR_ADDLANG_LABEL) );
     304           0 :         m_aInfoFT.SetText( IDE_RESSTR(STR_ADDLANG_INFO) );
     305             :     }
     306             : 
     307           0 :     FreeResource();
     308             : 
     309           0 :     FillLanguageBox();
     310           0 :     CalcInfoSize();
     311           0 : }
     312             : 
     313           0 : SetDefaultLanguageDialog::~SetDefaultLanguageDialog()
     314             : {
     315           0 :     delete m_pLanguageLB;
     316           0 :     delete m_pCheckLangLB;
     317           0 : }
     318             : 
     319           0 : void SetDefaultLanguageDialog::FillLanguageBox()
     320             : {
     321             :     // fill list with all languages
     322           0 :     m_pLanguageLB->SetLanguageList( LANG_LIST_ALL, false );
     323             :     // remove the already localized languages
     324           0 :     Sequence< Locale > aLocaleSeq = m_pLocalizationMgr->getStringResourceManager()->getLocales();
     325           0 :     const Locale* pLocale = aLocaleSeq.getConstArray();
     326           0 :     sal_Int32 i, nCount = aLocaleSeq.getLength();
     327           0 :     for ( i = 0;  i < nCount;  ++i )
     328           0 :         m_pLanguageLB->RemoveLanguage( LanguageTag( pLocale[i] ).getLanguageType() );
     329             : 
     330             :     // fill checklistbox if not in default mode
     331           0 :     if ( m_pLocalizationMgr->isLibraryLocalized() )
     332             :     {
     333           0 :         sal_uInt16 j, nCount_ = m_pLanguageLB->GetEntryCount();
     334           0 :         for ( j = 0;  j < nCount_;  ++j )
     335             :         {
     336             :             m_pCheckLangLB->InsertEntry(
     337           0 :                 m_pLanguageLB->GetEntry(j), LISTBOX_APPEND, m_pLanguageLB->GetEntryData(j) );
     338             :         }
     339           0 :         delete m_pLanguageLB;
     340           0 :         m_pLanguageLB = NULL;
     341             :     }
     342             :     else
     343             :         // preselect current UI language
     344           0 :         m_pLanguageLB->SelectLanguage( Application::GetSettings().GetUILanguageTag().getLanguageType() );
     345           0 : }
     346             : 
     347           0 : void SetDefaultLanguageDialog::CalcInfoSize()
     348             : {
     349           0 :     OUString sInfoStr = m_aInfoFT.GetText();
     350           0 :     long nInfoWidth = m_aInfoFT.GetSizePixel().Width();
     351           0 :     long nLongWord = getLongestWordWidth( sInfoStr, m_aInfoFT );
     352           0 :     long nTxtWidth = m_aInfoFT.GetCtrlTextWidth( sInfoStr ) + nLongWord;
     353           0 :     long nLines = ( nTxtWidth / nInfoWidth ) + 1;
     354           0 :     if ( nLines > INFO_LINES_COUNT )
     355             :     {
     356           0 :         Size aFTSize = m_aLanguageFT.GetSizePixel();
     357           0 :         Size aSize = m_aInfoFT.GetSizePixel();
     358           0 :         long nNewHeight = aFTSize.Height() * nLines;
     359           0 :         long nDelta = nNewHeight - aSize.Height();
     360           0 :         aSize.Height() = nNewHeight;
     361           0 :         m_aInfoFT.SetSizePixel( aSize );
     362             : 
     363             :         Window* pWin = ( m_pLanguageLB != NULL ) ? dynamic_cast< Window* >( m_pLanguageLB )
     364           0 :                                                  : dynamic_cast< Window* >( m_pCheckLangLB );
     365           0 :         aSize = pWin->GetSizePixel();
     366           0 :         aSize.Height() -= nDelta;
     367           0 :         pWin->SetSizePixel( aSize );
     368             : 
     369           0 :         Point aNewPos = m_aInfoFT.GetPosPixel();
     370           0 :         aNewPos.Y() -= nDelta;
     371           0 :         m_aInfoFT.SetPosPixel( aNewPos );
     372           0 :     }
     373           0 : }
     374             : 
     375           0 : Sequence< Locale > SetDefaultLanguageDialog::GetLocales() const
     376             : {
     377           0 :     bool bNotLocalized = !m_pLocalizationMgr->isLibraryLocalized();
     378           0 :     sal_Int32 nSize = bNotLocalized ? 1 : m_pCheckLangLB->GetCheckedEntryCount();
     379           0 :     Sequence< Locale > aLocaleSeq( nSize );
     380           0 :     if ( bNotLocalized )
     381             :     {
     382           0 :         aLocaleSeq[0] = LanguageTag( m_pLanguageLB->GetSelectLanguage() ).getLocale();
     383             :     }
     384             :     else
     385             :     {
     386           0 :         sal_uInt16 i, nCount = static_cast< sal_uInt16 >( m_pCheckLangLB->GetEntryCount() );
     387           0 :         sal_Int32 j = 0;
     388           0 :         for ( i = 0; i < nCount; ++i )
     389             :         {
     390           0 :             if ( m_pCheckLangLB->IsChecked(i) )
     391             :             {
     392           0 :                 LanguageType eType = LanguageType( (sal_uLong)m_pCheckLangLB->GetEntryData(i) );
     393           0 :                 aLocaleSeq[j++] = LanguageTag( eType ).getLocale();
     394             :             }
     395             :         }
     396             :         DBG_ASSERT( nSize == j, "SetDefaultLanguageDialog::GetLocales(): invalid indexes" );
     397             :     }
     398           0 :     return aLocaleSeq;
     399             : }
     400             : 
     401             : } // namespace basctl
     402             : 
     403             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10