1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_basctl.hxx" 30 31 #include <ide_pch.hxx> 32 33 34 #include <basidesh.hrc> 35 #include <basidesh.hxx> 36 #include <basobj.hxx> 37 38 #include <basicbox.hxx> 39 #include <iderid.hxx> 40 #include <iderdll.hxx> 41 #include <bastypes.hxx> 42 #include "bastype2.hxx" 43 #include "basdoc.hxx" 44 45 #include "localizationmgr.hxx" 46 #include "managelang.hxx" 47 #include "dlgresid.hrc" 48 #include <editeng/unolingu.hxx> 49 50 #include <svtools/langtab.hxx> 51 52 53 using namespace ::com::sun::star; 54 using namespace ::com::sun::star::lang; 55 using namespace ::com::sun::star::uno; 56 57 SFX_IMPL_TOOLBOX_CONTROL( LibBoxControl, SfxStringItem ); 58 59 LibBoxControl::LibBoxControl( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx ) 60 : SfxToolBoxControl( nSlotId, nId, rTbx ) 61 { 62 } 63 64 65 66 LibBoxControl::~LibBoxControl() 67 { 68 } 69 70 71 72 void LibBoxControl::StateChanged( sal_uInt16, SfxItemState eState, const SfxPoolItem* pState ) 73 { 74 BasicLibBox* pBox = (BasicLibBox*) GetToolBox().GetItemWindow( GetId() ); 75 76 DBG_ASSERT( pBox, "Box not found" ); 77 if ( !pBox ) 78 return; 79 80 if ( eState != SFX_ITEM_AVAILABLE ) 81 pBox->Disable(); 82 else 83 { 84 pBox->Enable(); 85 86 if ( pState->ISA(SfxStringItem) ) 87 pBox->Update( (const SfxStringItem*)pState ); 88 else 89 pBox->Update( NULL ); 90 } 91 } 92 93 94 95 Window* LibBoxControl::CreateItemWindow( Window *pParent ) 96 { 97 return new BasicLibBox( pParent, m_xFrame ); 98 } 99 100 //============================================================================= 101 //= DocListenerBox 102 //============================================================================= 103 104 DocListenerBox::DocListenerBox( Window* pParent ) 105 :ListBox( pParent, WinBits( WB_BORDER | WB_DROPDOWN ) ) 106 ,m_aNotifier( *this ) 107 { 108 } 109 110 DocListenerBox::~DocListenerBox() 111 { 112 m_aNotifier.dispose(); 113 } 114 115 void DocListenerBox::onDocumentCreated( const ScriptDocument& /*_rDocument*/ ) 116 { 117 FillBox(); 118 } 119 120 void DocListenerBox::onDocumentOpened( const ScriptDocument& /*_rDocument*/ ) 121 { 122 FillBox(); 123 } 124 125 void DocListenerBox::onDocumentSave( const ScriptDocument& /*_rDocument*/ ) 126 { 127 // not interested in 128 } 129 130 void DocListenerBox::onDocumentSaveDone( const ScriptDocument& /*_rDocument*/ ) 131 { 132 // not interested in 133 } 134 135 void DocListenerBox::onDocumentSaveAs( const ScriptDocument& /*_rDocument*/ ) 136 { 137 // not interested in 138 } 139 140 void DocListenerBox::onDocumentSaveAsDone( const ScriptDocument& /*_rDocument*/ ) 141 { 142 FillBox(); 143 } 144 145 void DocListenerBox::onDocumentClosed( const ScriptDocument& /*_rDocument*/ ) 146 { 147 FillBox(); 148 } 149 150 void DocListenerBox::onDocumentTitleChanged( const ScriptDocument& /*_rDocument*/ ) 151 { 152 // not interested in 153 } 154 155 void DocListenerBox::onDocumentModeChanged( const ScriptDocument& /*_rDocument*/ ) 156 { 157 // not interested in 158 } 159 160 //============================================================================= 161 //= BasicLibBox 162 //============================================================================= 163 BasicLibBox::BasicLibBox( Window* pParent, const uno::Reference< frame::XFrame >& rFrame ) : 164 DocListenerBox( pParent ), 165 m_xFrame( rFrame ) 166 { 167 FillBox(); 168 bIgnoreSelect = sal_True; // Select von 0 noch nicht weiterleiten 169 bFillBox = sal_True; 170 SelectEntryPos( 0 ); 171 aCurText = GetEntry( 0 ); 172 SetSizePixel( Size( 250, 200 ) ); 173 bIgnoreSelect = sal_False; 174 } 175 176 177 178 __EXPORT BasicLibBox::~BasicLibBox() 179 { 180 ClearBox(); 181 } 182 183 void __EXPORT BasicLibBox::Update( const SfxStringItem* pItem ) 184 { 185 // Immer auf dem laufenden sein... 186 // if ( !pItem || !pItem->GetValue().Len() ) 187 FillBox(); 188 189 if ( pItem ) 190 { 191 aCurText = pItem->GetValue(); 192 if ( aCurText.Len() == 0 ) 193 aCurText = String( IDEResId( RID_STR_ALL ) ); 194 } 195 196 if ( GetSelectEntry() != aCurText ) 197 SelectEntry( aCurText ); 198 } 199 200 void __EXPORT BasicLibBox::ReleaseFocus() 201 { 202 SfxViewShell* pCurSh = SfxViewShell::Current(); 203 DBG_ASSERT( pCurSh, "Current ViewShell not found!" ); 204 205 if ( pCurSh ) 206 { 207 Window* pShellWin = pCurSh->GetWindow(); 208 if ( !pShellWin ) // sonst werde ich ihn nicht los 209 pShellWin = Application::GetDefDialogParent(); 210 211 pShellWin->GrabFocus(); 212 } 213 } 214 215 void BasicLibBox::FillBox() 216 { 217 SetUpdateMode( sal_False ); 218 bIgnoreSelect = sal_True; 219 220 aCurText = GetSelectEntry(); 221 222 SelectEntryPos( 0 ); 223 ClearBox(); 224 225 // create list box entries 226 sal_uInt16 nPos = InsertEntry( String( IDEResId( RID_STR_ALL ) ), LISTBOX_APPEND ); 227 SetEntryData( nPos, new BasicLibEntry( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_UNKNOWN, String() ) ); 228 InsertEntries( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_USER ); 229 InsertEntries( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_SHARE ); 230 231 ScriptDocuments aDocuments( ScriptDocument::getAllScriptDocuments( ScriptDocument::DocumentsSorted ) ); 232 for ( ScriptDocuments::const_iterator doc = aDocuments.begin(); 233 doc != aDocuments.end(); 234 ++doc 235 ) 236 { 237 InsertEntries( *doc, LIBRARY_LOCATION_DOCUMENT ); 238 } 239 240 SetUpdateMode( sal_True ); 241 242 SelectEntry( aCurText ); 243 if ( !GetSelectEntryCount() ) 244 { 245 SelectEntryPos( GetEntryCount() ); // gibst es nicht => leer? 246 aCurText = GetSelectEntry(); 247 } 248 bIgnoreSelect = sal_False; 249 } 250 251 void BasicLibBox::InsertEntries( const ScriptDocument& rDocument, LibraryLocation eLocation ) 252 { 253 // get a sorted list of library names 254 Sequence< ::rtl::OUString > aLibNames = rDocument.getLibraryNames(); 255 sal_Int32 nLibCount = aLibNames.getLength(); 256 const ::rtl::OUString* pLibNames = aLibNames.getConstArray(); 257 258 for ( sal_Int32 i = 0 ; i < nLibCount ; ++i ) 259 { 260 String aLibName = pLibNames[ i ]; 261 if ( eLocation == rDocument.getLibraryLocation( aLibName ) ) 262 { 263 String aName( rDocument.getTitle( eLocation ) ); 264 String aEntryText( CreateMgrAndLibStr( aName, aLibName ) ); 265 sal_uInt16 nPos = InsertEntry( aEntryText, LISTBOX_APPEND ); 266 SetEntryData( nPos, new BasicLibEntry( rDocument, eLocation, aLibName ) ); 267 } 268 } 269 } 270 271 long BasicLibBox::PreNotify( NotifyEvent& rNEvt ) 272 { 273 long nDone = 0; 274 if( rNEvt.GetType() == EVENT_KEYINPUT ) 275 { 276 KeyEvent aKeyEvt = *rNEvt.GetKeyEvent(); 277 sal_uInt16 nKeyCode = aKeyEvt.GetKeyCode().GetCode(); 278 switch( nKeyCode ) 279 { 280 case KEY_RETURN: 281 { 282 NotifyIDE(); 283 nDone = 1; 284 } 285 break; 286 287 case KEY_ESCAPE: 288 { 289 SelectEntry( aCurText ); 290 ReleaseFocus(); 291 nDone = 1; 292 } 293 break; 294 } 295 } 296 else if( rNEvt.GetType() == EVENT_GETFOCUS ) 297 { 298 if ( bFillBox ) 299 { 300 FillBox(); 301 bFillBox = sal_False; 302 } 303 } 304 else if( rNEvt.GetType() == EVENT_LOSEFOCUS ) 305 { 306 if ( !HasChildPathFocus( sal_True ) ) 307 { 308 bIgnoreSelect = sal_True; 309 bFillBox = sal_True; 310 } 311 } 312 313 return nDone ? nDone : ListBox::PreNotify( rNEvt ); 314 } 315 316 void __EXPORT BasicLibBox::Select() 317 { 318 if ( !IsTravelSelect() ) 319 { 320 if ( !bIgnoreSelect ) 321 NotifyIDE(); 322 else 323 SelectEntry( aCurText ); // Seit 306... (Select nach Escape) 324 } 325 } 326 327 void BasicLibBox::NotifyIDE() 328 { 329 sal_uInt16 nSelPos = GetSelectEntryPos(); 330 BasicLibEntry* pEntry = (BasicLibEntry*)GetEntryData( nSelPos ); 331 if ( pEntry ) 332 { 333 ScriptDocument aDocument( pEntry->GetDocument() ); 334 SfxUsrAnyItem aDocumentItem( SID_BASICIDE_ARG_DOCUMENT_MODEL, uno::makeAny( aDocument.getDocumentOrNull() ) ); 335 String aLibName = pEntry->GetLibName(); 336 SfxStringItem aLibNameItem( SID_BASICIDE_ARG_LIBNAME, aLibName ); 337 BasicIDEShell* pIDEShell = IDE_DLL()->GetShell(); 338 SfxViewFrame* pViewFrame = pIDEShell ? pIDEShell->GetViewFrame() : NULL; 339 SfxDispatcher* pDispatcher = pViewFrame ? pViewFrame->GetDispatcher() : NULL; 340 if ( pDispatcher ) 341 { 342 pDispatcher->Execute( SID_BASICIDE_LIBSELECTED, 343 SFX_CALLMODE_SYNCHRON, &aDocumentItem, &aLibNameItem, 0L ); 344 } 345 } 346 ReleaseFocus(); 347 } 348 349 void BasicLibBox::ClearBox() 350 { 351 sal_uInt16 nCount = GetEntryCount(); 352 for ( sal_uInt16 i = 0; i < nCount; ++i ) 353 { 354 BasicLibEntry* pEntry = (BasicLibEntry*)GetEntryData( i ); 355 delete pEntry; 356 } 357 ListBox::Clear(); 358 } 359 360 // class LanguageBoxControl ---------------------------------------------- 361 362 SFX_IMPL_TOOLBOX_CONTROL( LanguageBoxControl, SfxStringItem ); 363 364 LanguageBoxControl::LanguageBoxControl( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx ) 365 : SfxToolBoxControl( nSlotId, nId, rTbx ) 366 { 367 } 368 369 LanguageBoxControl::~LanguageBoxControl() 370 { 371 } 372 373 void LanguageBoxControl::StateChanged( sal_uInt16 _nID, SfxItemState _eState, const SfxPoolItem* _pItem ) 374 { 375 (void)_nID; 376 377 BasicLanguageBox* pBox = (BasicLanguageBox*)( GetToolBox().GetItemWindow( GetId() ) ); 378 379 if ( pBox ) 380 { 381 if ( _eState != SFX_ITEM_AVAILABLE ) 382 pBox->Disable(); 383 else 384 { 385 pBox->Enable(); 386 if ( _pItem->ISA(SfxStringItem) ) 387 pBox->Update( (const SfxStringItem*)_pItem ); 388 else 389 pBox->Update( NULL ); 390 } 391 } 392 } 393 394 Window* LanguageBoxControl::CreateItemWindow( Window *pParent ) 395 { 396 return new BasicLanguageBox( pParent ); 397 } 398 399 // class BasicLanguageBox ------------------------------------------------ 400 401 BasicLanguageBox::BasicLanguageBox( Window* pParent ) : 402 403 DocListenerBox( pParent ), 404 405 m_sNotLocalizedStr( IDEResId( RID_STR_TRANSLATION_NOTLOCALIZED ) ), 406 m_sDefaultLanguageStr( IDEResId( RID_STR_TRANSLATION_DEFAULT ) ), 407 408 m_bIgnoreSelect( false ) 409 410 { 411 SetSizePixel( Size( 210, 200 ) ); 412 413 FillBox(); 414 } 415 416 BasicLanguageBox::~BasicLanguageBox() 417 { 418 ClearBox(); 419 } 420 421 void BasicLanguageBox::FillBox() 422 { 423 SetUpdateMode( sal_False ); 424 m_bIgnoreSelect = true; 425 m_sCurrentText = GetSelectEntry(); 426 ClearBox(); 427 428 LocalizationMgr* pCurMgr = IDE_DLL()->GetShell()->GetCurLocalizationMgr(); 429 if ( pCurMgr->isLibraryLocalized() ) 430 { 431 Enable(); 432 SvtLanguageTable aLangTable; 433 Locale aDefaultLocale = pCurMgr->getStringResourceManager()->getDefaultLocale(); 434 Locale aCurrentLocale = pCurMgr->getStringResourceManager()->getCurrentLocale(); 435 Sequence< Locale > aLocaleSeq = pCurMgr->getStringResourceManager()->getLocales(); 436 const Locale* pLocale = aLocaleSeq.getConstArray(); 437 sal_Int32 i, nCount = aLocaleSeq.getLength(); 438 sal_uInt16 nSelPos = LISTBOX_ENTRY_NOTFOUND; 439 for ( i = 0; i < nCount; ++i ) 440 { 441 bool bIsDefault = localesAreEqual( aDefaultLocale, pLocale[i] ); 442 bool bIsCurrent = localesAreEqual( aCurrentLocale, pLocale[i] ); 443 LanguageType eLangType = SvxLocaleToLanguage( pLocale[i] ); 444 String sLanguage = aLangTable.GetString( eLangType ); 445 if ( bIsDefault ) 446 { 447 sLanguage += ' '; 448 sLanguage += m_sDefaultLanguageStr; 449 } 450 sal_uInt16 nPos = InsertEntry( sLanguage ); 451 SetEntryData( nPos, new LanguageEntry( sLanguage, pLocale[i], bIsDefault ) ); 452 453 if ( bIsCurrent ) 454 nSelPos = nPos; 455 } 456 457 if ( nSelPos != LISTBOX_ENTRY_NOTFOUND ) 458 { 459 SelectEntryPos( nSelPos ); 460 m_sCurrentText = GetSelectEntry(); 461 } 462 } 463 else 464 { 465 InsertEntry( m_sNotLocalizedStr ); 466 SelectEntryPos(0); 467 Disable(); 468 } 469 470 SetUpdateMode( sal_True ); 471 m_bIgnoreSelect = false; 472 } 473 474 void BasicLanguageBox::ClearBox() 475 { 476 sal_uInt16 nCount = GetEntryCount(); 477 for ( sal_uInt16 i = 0; i < nCount; ++i ) 478 { 479 LanguageEntry* pEntry = (LanguageEntry*)GetEntryData(i); 480 delete pEntry; 481 } 482 ListBox::Clear(); 483 } 484 485 void BasicLanguageBox::SetLanguage() 486 { 487 LanguageEntry* pEntry = (LanguageEntry*)GetEntryData( GetSelectEntryPos() ); 488 if ( pEntry ) 489 IDE_DLL()->GetShell()->GetCurLocalizationMgr()->handleSetCurrentLocale( pEntry->m_aLocale ); 490 } 491 492 void BasicLanguageBox::Select() 493 { 494 if ( !m_bIgnoreSelect ) 495 SetLanguage(); 496 else 497 SelectEntry( m_sCurrentText ); // Select after Escape 498 } 499 500 long BasicLanguageBox::PreNotify( NotifyEvent& rNEvt ) 501 { 502 long nDone = 0; 503 if( rNEvt.GetType() == EVENT_KEYINPUT ) 504 { 505 sal_uInt16 nKeyCode = rNEvt.GetKeyEvent()->GetKeyCode().GetCode(); 506 switch( nKeyCode ) 507 { 508 case KEY_RETURN: 509 { 510 SetLanguage(); 511 nDone = 1; 512 } 513 break; 514 515 case KEY_ESCAPE: 516 { 517 SelectEntry( m_sCurrentText ); 518 nDone = 1; 519 } 520 break; 521 } 522 } 523 else if( rNEvt.GetType() == EVENT_GETFOCUS ) 524 { 525 } 526 else if( rNEvt.GetType() == EVENT_LOSEFOCUS ) 527 { 528 } 529 530 return nDone ? nDone : ListBox::PreNotify( rNEvt ); 531 } 532 533 void BasicLanguageBox::Update( const SfxStringItem* pItem ) 534 { 535 FillBox(); 536 537 if ( pItem && pItem->GetValue().Len() > 0 ) 538 { 539 m_sCurrentText = pItem->GetValue(); 540 if ( GetSelectEntry() != m_sCurrentText ) 541 SelectEntry( m_sCurrentText ); 542 } 543 } 544 545