xref: /trunk/main/cui/source/customize/cfg.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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_cui.hxx"
30 
31 #include <stdlib.h>
32 #include <time.h>
33 
34 #ifndef _HELP_HXX //autogen
35 #include <vcl/help.hxx>
36 #endif
37 #ifndef _MSGBOX_HXX //autogen
38 #include <vcl/msgbox.hxx>
39 #endif
40 #include <vcl/decoview.hxx>
41 #include <vcl/toolbox.hxx>
42 #include <vcl/scrbar.hxx>
43 
44 //added for issue73355
45 //#ifndef _SV_SVDATA_HXX
46 //#include <vcl/svdata.hxx>
47 //#endif
48 //issue73355 ends
49 
50 #include <sfx2/app.hxx>
51 #include <sfx2/sfxdlg.hxx>
52 #include <sfx2/viewfrm.hxx>
53 #include <sfx2/viewsh.hxx>
54 #include <sfx2/msg.hxx>
55 #include <sfx2/msgpool.hxx>
56 #include <sfx2/mnumgr.hxx>
57 #include <sfx2/minfitem.hxx>
58 #include <sfx2/objsh.hxx>
59 #include <sfx2/request.hxx>
60 #include <sfx2/filedlghelper.hxx>
61 #include <svl/stritem.hxx>
62 #include <svtools/miscopt.hxx>
63 #include <tools/diagnose_ex.h>
64 #include <toolkit/unohlp.hxx>
65 
66 #include <algorithm>
67 //add
68 #include <cuires.hrc>
69 #include "cfg.hrc"
70 #include "helpid.hrc"
71 
72 #include "acccfg.hxx"
73 #include "cfg.hxx"
74 #include "eventdlg.hxx"
75 #include <dialmgr.hxx>
76 
77 #include <comphelper/documentinfo.hxx>
78 #include <comphelper/processfactory.hxx>
79 #ifndef _UNOTOOLS_CONFIGMGR_HXX_
80 #include <unotools/configmgr.hxx>
81 #endif
82 #include <com/sun/star/ui/ItemType.hpp>
83 #include <com/sun/star/ui/ItemStyle.hpp>
84 #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
85 #include <com/sun/star/frame/XController.hpp>
86 #include <com/sun/star/frame/XDesktop.hpp>
87 #include <com/sun/star/ui/XUIConfiguration.hpp>
88 #include <com/sun/star/ui/XUIConfigurationListener.hpp>
89 #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
90 #include <com/sun/star/ui/XUIConfigurationPersistence.hpp>
91 #include <com/sun/star/ui/XUIConfigurationStorage.hpp>
92 #include <com/sun/star/ui/XModuleUIConfigurationManager.hpp>
93 #include <com/sun/star/ui/XUIElement.hpp>
94 #ifndef _COM_SUN_STAR_UI_UIElementType_HPP_
95 #include <com/sun/star/ui/UIElementType.hpp>
96 #endif
97 #include <com/sun/star/ui/ImageType.hpp>
98 #include <com/sun/star/frame/XLayoutManager.hpp>
99 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
100 #include "com/sun/star/ui/dialogs/TemplateDescription.hpp"
101 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
102 #include <com/sun/star/frame/XFramesSupplier.hpp>
103 #include <com/sun/star/frame/XFrames.hpp>
104 #include <com/sun/star/frame/FrameSearchFlag.hpp>
105 #include <com/sun/star/embed/ElementModes.hpp>
106 
107 #include "dlgname.hxx"
108 
109 #define PRTSTR(x) rtl::OUStringToOString(x, RTL_TEXTENCODING_ASCII_US).pData->buffer
110 
111 #define ENTRY_HEIGHT 16
112 
113 static const char ITEM_DESCRIPTOR_COMMANDURL[]  = "CommandURL";
114 static const char ITEM_DESCRIPTOR_CONTAINER[]   = "ItemDescriptorContainer";
115 static const char ITEM_DESCRIPTOR_LABEL[]       = "Label";
116 static const char ITEM_DESCRIPTOR_TYPE[]        = "Type";
117 static const char ITEM_DESCRIPTOR_STYLE[]       = "Style";
118 static const char ITEM_DESCRIPTOR_ISVISIBLE[]   = "IsVisible";
119 static const char ITEM_DESCRIPTOR_RESOURCEURL[] = "ResourceURL";
120 static const char ITEM_DESCRIPTOR_UINAME[]      = "UIName";
121 
122 static const char ITEM_MENUBAR_URL[] = "private:resource/menubar/menubar";
123 static const char ITEM_TOOLBAR_URL[] = "private:resource/toolbar/";
124 
125 static const char CUSTOM_TOOLBAR_STR[] = "custom_toolbar_";
126 static const char CUSTOM_MENU_STR[] = "vnd.openoffice.org:CustomMenu";
127 
128 static const char __FAR_DATA pSeparatorStr[] =
129     "----------------------------------";
130 static const char __FAR_DATA pMenuSeparatorStr[]    = " | ";
131 
132 #ifdef _MSC_VER
133 #pragma warning (disable:4355)
134 #endif
135 
136 using rtl::OUString;
137 namespace css = com::sun::star;
138 namespace uno = com::sun::star::uno;
139 namespace frame = com::sun::star::frame;
140 namespace lang = com::sun::star::lang;
141 namespace container = com::sun::star::container;
142 namespace beans = com::sun::star::beans;
143 namespace graphic = com::sun::star::graphic;
144 
145 #if OSL_DEBUG_LEVEL > 1
146 
147 void printPropertySet(
148     const OUString& prefix,
149     const uno::Reference< beans::XPropertySet >& xPropSet )
150 {
151     uno::Reference< beans::XPropertySetInfo > xPropSetInfo =
152         xPropSet->getPropertySetInfo();
153 
154     uno::Sequence< beans::Property > aPropDetails =
155         xPropSetInfo->getProperties();
156 
157     OSL_TRACE("printPropertySet: %d properties", aPropDetails.getLength());
158 
159     for ( sal_Int32 i = 0; i < aPropDetails.getLength(); i++ )
160     {
161         OUString tmp;
162         sal_Int32 ival;
163 
164         uno::Any a = xPropSet->getPropertyValue( aPropDetails[i].Name );
165 
166         if ( ( a >>= tmp ) /* && tmp.getLength() != 0 */ )
167         {
168             OSL_TRACE("%s: Got property: %s = %s",
169                 PRTSTR(prefix), PRTSTR(aPropDetails[i].Name), PRTSTR(tmp));
170         }
171         else if ( ( a >>= ival ) )
172         {
173             OSL_TRACE("%s: Got property: %s = %d",
174                 PRTSTR(prefix), PRTSTR(aPropDetails[i].Name), PRTSTR(tmp));
175         }
176         else
177         {
178             OSL_TRACE("%s: Got property: %s of type %s",
179                 PRTSTR(prefix), PRTSTR(aPropDetails[i].Name), PRTSTR(a.getValueTypeName()));
180         }
181     }
182 }
183 
184 void printProperties(
185     const OUString& prefix,
186     const uno::Sequence< beans::PropertyValue >& aProp )
187 {
188     for ( sal_Int32 i = 0; i < aProp.getLength(); i++ )
189     {
190         OUString tmp;
191 
192         aProp[i].Value >>= tmp;
193 
194         OSL_TRACE("%s: Got property: %s = %s",
195             PRTSTR(prefix), PRTSTR(aProp[i].Name), PRTSTR(tmp));
196     }
197 }
198 
199 void printEntries(SvxEntries* entries)
200 {
201     SvxEntries::const_iterator iter = entries->begin();
202 
203     for ( ; iter != entries->end(); iter++ )
204     {
205         SvxConfigEntry* entry = *iter;
206 
207         OSL_TRACE("printEntries: %s", PRTSTR(entry->GetName()));
208     }
209 }
210 
211 #endif
212 
213 OUString
214 stripHotKey( const OUString& str )
215 {
216     sal_Int32 index = str.indexOf( '~' );
217     if ( index == -1 )
218     {
219         return str;
220     }
221     else
222     {
223         return str.replaceAt( index, 1, OUString() );
224     }
225 }
226 
227 OUString replaceSaveInName(
228     const OUString& rMessage,
229     const OUString& rSaveInName )
230 {
231     OUString name;
232     OUString placeholder = OUString::createFromAscii( "%SAVE IN SELECTION%" );
233 
234     sal_Int32 pos = rMessage.indexOf( placeholder );
235 
236     if ( pos != -1 )
237     {
238         name = rMessage.replaceAt(
239             pos, placeholder.getLength(), rSaveInName );
240     }
241     else
242     {
243         // don't change the message
244     }
245 
246     return name;
247 }
248 
249 OUString
250 replaceSixteen( const OUString& str, sal_Int32 nReplacement )
251 {
252     OUString result( str );
253     OUString sixteen = OUString::valueOf( (sal_Int32)16 );
254     OUString expected = OUString::valueOf( nReplacement );
255 
256     sal_Int32 len = sixteen.getLength();
257     sal_Int32 index = result.indexOf( sixteen );
258 
259     while ( index != -1 )
260     {
261         result = result.replaceAt( index, len, expected );
262         index = result.indexOf( sixteen, index );
263     }
264 
265     return result;
266 }
267 
268 OUString
269 generateCustomName(
270     const OUString& prefix,
271     SvxEntries* entries,
272     sal_Int32 suffix = 1 )
273 {
274     // find and replace the %n placeholder in the prefix string
275     OUString name;
276     OUString placeholder = OUString::createFromAscii( "%n" );
277 
278     sal_Int32 pos = prefix.indexOf(
279         OUString::createFromAscii( "%n" ) );
280 
281     if ( pos != -1 )
282     {
283         name = prefix.replaceAt(
284             pos, placeholder.getLength(), OUString::valueOf( suffix ) );
285     }
286     else
287     {
288         // no placeholder found so just append the suffix
289         name = prefix + OUString::valueOf( suffix );
290     }
291 
292     // now check is there is an already existing entry with this name
293     SvxEntries::const_iterator iter = entries->begin();
294 
295     SvxConfigEntry* pEntry;
296     while ( iter != entries->end() )
297     {
298         pEntry = *iter;
299 
300         if ( name.equals( pEntry->GetName() ) )
301         {
302             break;
303         }
304         iter++;
305     }
306 
307     if ( iter != entries->end() )
308     {
309         // name already exists so try the next number up
310         return generateCustomName( prefix, entries, ++suffix );
311     }
312 
313     return name;
314 }
315 
316 sal_uInt32 generateRandomValue()
317 {
318     srand( unsigned( time( NULL ) ));
319     return sal_uInt32( rand() );
320 }
321 
322 OUString
323 generateCustomURL(
324     SvxEntries* entries )
325 {
326     OUString url = OUString::createFromAscii( ITEM_TOOLBAR_URL );
327     url += OUString::createFromAscii( CUSTOM_TOOLBAR_STR );
328 
329     // use a random number to minimize possible clash with existing custom toolbars
330     url += OUString::valueOf( sal_Int64( generateRandomValue() ), 16 );
331 
332     // now check is there is an already existing entry with this url
333     SvxEntries::const_iterator iter = entries->begin();
334 
335     SvxConfigEntry* pEntry;
336     while ( iter != entries->end() )
337     {
338         pEntry = *iter;
339 
340         if ( url.equals( pEntry->GetCommand() ) )
341         {
342             break;
343         }
344         iter++;
345     }
346 
347     if ( iter != entries->end() )
348     {
349         // url already exists so try the next number up
350         return generateCustomURL( entries );
351     }
352 
353     return url;
354 }
355 
356 OUString
357 generateCustomMenuURL(
358     SvxEntries* entries,
359     sal_Int32 suffix = 1 )
360 {
361     OUString url = OUString::createFromAscii( CUSTOM_MENU_STR );
362     url += OUString::valueOf( suffix );
363 
364     // now check is there is an already existing entry with this url
365     SvxEntries::const_iterator iter = entries->begin();
366 
367     SvxConfigEntry* pEntry;
368     while ( iter != entries->end() )
369     {
370         pEntry = *iter;
371 
372         if ( url.equals( pEntry->GetCommand() ) )
373         {
374             break;
375         }
376         iter++;
377     }
378 
379     if ( iter != entries->end() )
380     {
381         // url already exists so try the next number up
382         return generateCustomMenuURL( entries, ++suffix );
383     }
384 
385     return url;
386 }
387 
388 static sal_Int16 theImageType =
389     css::ui::ImageType::COLOR_NORMAL |
390     css::ui::ImageType::SIZE_DEFAULT;
391 
392 void InitImageType()
393 {
394     theImageType =
395         css::ui::ImageType::COLOR_NORMAL |
396         css::ui::ImageType::SIZE_DEFAULT;
397 
398     if ( SvtMiscOptions().AreCurrentSymbolsLarge() )
399     {
400         theImageType |= css::ui::ImageType::SIZE_LARGE;
401     }
402 
403     Window* topwin = Application::GetActiveTopWindow();
404     if ( topwin != NULL &&
405          topwin->GetSettings().GetStyleSettings().GetHighContrastMode() )
406     {
407         theImageType |= css::ui::ImageType::COLOR_HIGHCONTRAST;
408     }
409 }
410 
411 sal_Int16 GetImageType()
412 {
413     return theImageType;
414 }
415 
416 void RemoveEntry( SvxEntries* pEntries, SvxConfigEntry* pChildEntry )
417 {
418     SvxEntries::iterator iter = pEntries->begin();
419 
420     while ( iter != pEntries->end() )
421     {
422         if ( pChildEntry == *iter )
423         {
424             pEntries->erase( iter );
425             break;
426         }
427         iter++;
428     }
429 }
430 
431 bool
432 SvxConfigPage::CanConfig( const OUString& aModuleId )
433 {
434     OSL_TRACE("SupportsDocumentConfig: %s", PRTSTR(aModuleId));
435 
436     if  (  aModuleId.equalsAscii( "com.sun.star.script.BasicIDE" )
437         || aModuleId.equalsAscii( "com.sun.star.frame.Bibliography" )
438         )
439     {
440         return sal_False;
441     }
442     return sal_True;
443 }
444 
445 OUString GetModuleName( const OUString& aModuleId )
446 {
447     if ( aModuleId.equalsAscii( "com.sun.star.text.TextDocument" ) ||
448          aModuleId.equalsAscii( "com.sun.star.text.GlobalDocument" ) )
449         return OUString::createFromAscii("Writer");
450     else if ( aModuleId.equalsAscii( "com.sun.star.text.WebDocument" ) )
451         return OUString::createFromAscii("Writer/Web");
452     else if ( aModuleId.equalsAscii( "com.sun.star.drawing.DrawingDocument" ) )
453         return OUString::createFromAscii("Draw");
454     else if ( aModuleId.equalsAscii( "com.sun.star.presentation.PresentationDocument" ) )
455         return OUString::createFromAscii("Impress");
456     else if ( aModuleId.equalsAscii( "com.sun.star.sheet.SpreadsheetDocument" ) )
457         return OUString::createFromAscii("Calc");
458     else if ( aModuleId.equalsAscii( "com.sun.star.script.BasicIDE" ) )
459         return OUString::createFromAscii("Basic");
460     else if ( aModuleId.equalsAscii( "com.sun.star.formula.FormulaProperties" ) )
461         return OUString::createFromAscii("Math");
462     else if ( aModuleId.equalsAscii( "com.sun.star.sdb.RelationDesign" ) )
463         return OUString::createFromAscii("Relation Design");
464     else if ( aModuleId.equalsAscii( "com.sun.star.sdb.QueryDesign" ) )
465         return OUString::createFromAscii("Query Design");
466     else if ( aModuleId.equalsAscii( "com.sun.star.sdb.TableDesign" ) )
467         return OUString::createFromAscii("Table Design");
468     else if ( aModuleId.equalsAscii( "com.sun.star.sdb.DataSourceBrowser" ) )
469         return OUString::createFromAscii("Data Source Browser" );
470     else if ( aModuleId.equalsAscii( "com.sun.star.sdb.DatabaseDocument" ) )
471         return OUString::createFromAscii("Database" );
472 
473     return ::rtl::OUString();
474 }
475 
476 OUString GetUIModuleName( const OUString& aModuleId, const uno::Reference< css::frame::XModuleManager >& rModuleManager )
477 {
478     OUString aModuleUIName;
479 
480     if ( rModuleManager.is() )
481     {
482         uno::Reference< css::container::XNameAccess > xNameAccess( rModuleManager, uno::UNO_QUERY );
483         if ( xNameAccess.is() )
484         {
485             try
486             {
487                 uno::Any a = xNameAccess->getByName( aModuleId );
488                 uno::Sequence< beans::PropertyValue > aSeq;
489 
490                 if ( a >>= aSeq )
491                 {
492                     OUString aUIName;
493                     for ( sal_Int32 i = 0; i < aSeq.getLength(); i++ )
494                     {
495                         if ( aSeq[i].Name.equalsAscii( "ooSetupFactoryUIName" ))
496                         {
497                             aSeq[i].Value >>= aModuleUIName;
498                             break;
499                         }
500                     }
501                 }
502             }
503             catch ( uno::RuntimeException& e )
504             {
505                 throw e;
506             }
507             catch ( uno::Exception& )
508             {
509             }
510         }
511     }
512 
513     if ( aModuleUIName.getLength() == 0 )
514         aModuleUIName = GetModuleName( aModuleId );
515 
516     return aModuleUIName;
517 }
518 
519 bool GetMenuItemData(
520     const uno::Reference< container::XIndexAccess >& rItemContainer,
521     sal_Int32 nIndex,
522     OUString& rCommandURL,
523     OUString& rLabel,
524     sal_uInt16& rType,
525     uno::Reference< container::XIndexAccess >& rSubMenu )
526 {
527     try
528     {
529         uno::Sequence< beans::PropertyValue > aProp;
530         if ( rItemContainer->getByIndex( nIndex ) >>= aProp )
531         {
532             for ( sal_Int32 i = 0; i < aProp.getLength(); i++ )
533             {
534                 if ( aProp[i].Name.equalsAscii( ITEM_DESCRIPTOR_COMMANDURL ))
535                 {
536                     aProp[i].Value >>= rCommandURL;
537                 }
538                 else if ( aProp[i].Name.equalsAscii( ITEM_DESCRIPTOR_CONTAINER ))
539                 {
540                     aProp[i].Value >>= rSubMenu;
541                 }
542                 else if ( aProp[i].Name.equalsAscii( ITEM_DESCRIPTOR_LABEL ))
543                 {
544                     aProp[i].Value >>= rLabel;
545                 }
546                 else if ( aProp[i].Name.equalsAscii( ITEM_DESCRIPTOR_TYPE ))
547                 {
548                     aProp[i].Value >>= rType;
549                 }
550             }
551 
552             return sal_True;
553         }
554     }
555     catch ( ::com::sun::star::lang::IndexOutOfBoundsException& )
556     {
557     }
558 
559     return sal_False;
560 }
561 
562 bool GetToolbarItemData(
563     const uno::Reference< container::XIndexAccess >& rItemContainer,
564     sal_Int32 nIndex,
565     OUString& rCommandURL,
566     OUString& rLabel,
567     sal_uInt16& rType,
568     sal_Bool& rIsVisible,
569     sal_Int32& rStyle,
570     uno::Reference< container::XIndexAccess >& rSubMenu )
571 {
572     try
573     {
574         uno::Sequence< beans::PropertyValue > aProp;
575         if ( rItemContainer->getByIndex( nIndex ) >>= aProp )
576         {
577             for ( sal_Int32 i = 0; i < aProp.getLength(); i++ )
578             {
579                 if ( aProp[i].Name.equalsAscii( ITEM_DESCRIPTOR_COMMANDURL ))
580                 {
581                     aProp[i].Value >>= rCommandURL;
582                 }
583                 if ( aProp[i].Name.equalsAscii( ITEM_DESCRIPTOR_STYLE ))
584                 {
585                     aProp[i].Value >>= rStyle;
586                 }
587                 else if (aProp[i].Name.equalsAscii(ITEM_DESCRIPTOR_CONTAINER))
588                 {
589                     aProp[i].Value >>= rSubMenu;
590                 }
591                 else if ( aProp[i].Name.equalsAscii( ITEM_DESCRIPTOR_LABEL ))
592                 {
593                     aProp[i].Value >>= rLabel;
594                 }
595                 else if ( aProp[i].Name.equalsAscii( ITEM_DESCRIPTOR_TYPE ))
596                 {
597                     aProp[i].Value >>= rType;
598                 }
599                 else if (aProp[i].Name.equalsAscii(ITEM_DESCRIPTOR_ISVISIBLE))
600                 {
601                     aProp[i].Value >>= rIsVisible;
602                 }
603             }
604 
605             return sal_True;
606         }
607     }
608     catch ( ::com::sun::star::lang::IndexOutOfBoundsException& )
609     {
610     }
611 
612     return sal_False;
613 }
614 
615 uno::Sequence< beans::PropertyValue >
616 ConvertSvxConfigEntry(
617     const uno::Reference< container::XNameAccess >& xCommandToLabelMap,
618     const SvxConfigEntry* pEntry )
619 {
620     static const OUString aDescriptorCommandURL (
621         RTL_CONSTASCII_USTRINGPARAM( ITEM_DESCRIPTOR_COMMANDURL ) );
622 
623     static const OUString aDescriptorType(
624             RTL_CONSTASCII_USTRINGPARAM( ITEM_DESCRIPTOR_TYPE ) );
625 
626     static const OUString aDescriptorLabel(
627             RTL_CONSTASCII_USTRINGPARAM( ITEM_DESCRIPTOR_LABEL ) );
628 
629     static const OUString aDescriptorContainer(
630             RTL_CONSTASCII_USTRINGPARAM( ITEM_DESCRIPTOR_CONTAINER ) );
631 
632     uno::Sequence< beans::PropertyValue > aPropSeq( 3 );
633 
634     aPropSeq[0].Name = aDescriptorCommandURL;
635     aPropSeq[0].Value <<= rtl::OUString( pEntry->GetCommand() );
636 
637     aPropSeq[1].Name = aDescriptorType;
638     aPropSeq[1].Value <<= css::ui::ItemType::DEFAULT;
639 
640     // If the name has not been changed and the name is the same as
641     // in the default command to label map then the label can be stored
642     // as an empty string.
643     // It will be initialised again later using the command to label map.
644     aPropSeq[2].Name = aDescriptorLabel;
645     if ( pEntry->HasChangedName() == sal_False && pEntry->GetCommand().getLength() )
646     {
647         sal_Bool isDefaultName = sal_False;
648         try
649         {
650             uno::Any a( xCommandToLabelMap->getByName( pEntry->GetCommand() ) );
651             uno::Sequence< beans::PropertyValue > tmpPropSeq;
652             if ( a >>= tmpPropSeq )
653             {
654                 for ( sal_Int32 i = 0; i < tmpPropSeq.getLength(); i++ )
655                 {
656                     if ( tmpPropSeq[i].Name.equals( aDescriptorLabel ) )
657                     {
658                         OUString tmpLabel;
659                         tmpPropSeq[i].Value >>= tmpLabel;
660 
661                         if ( tmpLabel.equals( pEntry->GetName() ) )
662                         {
663                             isDefaultName = sal_True;
664                         }
665 
666                         break;
667                     }
668                 }
669             }
670         }
671         catch ( container::NoSuchElementException& )
672         {
673             // isDefaultName is left as FALSE
674         }
675 
676         if ( isDefaultName )
677         {
678             aPropSeq[2].Value <<= rtl::OUString();
679         }
680         else
681         {
682             aPropSeq[2].Value <<= rtl::OUString( pEntry->GetName() );
683         }
684     }
685     else
686     {
687         aPropSeq[2].Value <<= rtl::OUString( pEntry->GetName() );
688     }
689 
690     return aPropSeq;
691 }
692 
693 uno::Sequence< beans::PropertyValue >
694 ConvertToolbarEntry(
695     const uno::Reference< container::XNameAccess >& xCommandToLabelMap,
696     const SvxConfigEntry* pEntry )
697 {
698     static const OUString aDescriptorCommandURL (
699         RTL_CONSTASCII_USTRINGPARAM( ITEM_DESCRIPTOR_COMMANDURL ) );
700 
701     static const OUString aDescriptorType(
702             RTL_CONSTASCII_USTRINGPARAM( ITEM_DESCRIPTOR_TYPE ) );
703 
704     static const OUString aDescriptorLabel(
705             RTL_CONSTASCII_USTRINGPARAM( ITEM_DESCRIPTOR_LABEL ) );
706 
707     static const OUString aDescriptorContainer(
708             RTL_CONSTASCII_USTRINGPARAM( ITEM_DESCRIPTOR_CONTAINER ) );
709 
710     static const OUString aIsVisible(
711             RTL_CONSTASCII_USTRINGPARAM( ITEM_DESCRIPTOR_ISVISIBLE ) );
712 
713     uno::Sequence< beans::PropertyValue > aPropSeq( 4 );
714 
715     aPropSeq[0].Name = aDescriptorCommandURL;
716     aPropSeq[0].Value <<= rtl::OUString( pEntry->GetCommand() );
717 
718     aPropSeq[1].Name = aDescriptorType;
719     aPropSeq[1].Value <<= css::ui::ItemType::DEFAULT;
720 
721     // If the name has not been changed and the name is the same as
722     // in the default command to label map then the label can be stored
723     // as an empty string.
724     // It will be initialised again later using the command to label map.
725     aPropSeq[2].Name = aDescriptorLabel;
726     if ( pEntry->HasChangedName() == sal_False && pEntry->GetCommand().getLength() )
727     {
728         sal_Bool isDefaultName = sal_False;
729         try
730         {
731             uno::Any a( xCommandToLabelMap->getByName( pEntry->GetCommand() ) );
732             uno::Sequence< beans::PropertyValue > tmpPropSeq;
733             if ( a >>= tmpPropSeq )
734             {
735                 for ( sal_Int32 i = 0; i < tmpPropSeq.getLength(); i++ )
736                 {
737                     if ( tmpPropSeq[i].Name.equals( aDescriptorLabel ) )
738                     {
739                         OUString tmpLabel;
740                         tmpPropSeq[i].Value >>= tmpLabel;
741 
742                         if ( tmpLabel.equals( pEntry->GetName() ) )
743                         {
744                             isDefaultName = sal_True;
745                         }
746 
747                         break;
748                     }
749                 }
750             }
751         }
752         catch ( container::NoSuchElementException& )
753         {
754             // isDefaultName is left as FALSE
755         }
756 
757         if ( isDefaultName )
758         {
759             aPropSeq[2].Value <<= rtl::OUString();
760         }
761         else
762         {
763             aPropSeq[2].Value <<= rtl::OUString( pEntry->GetName() );
764         }
765     }
766     else
767     {
768         aPropSeq[2].Value <<= rtl::OUString( pEntry->GetName() );
769     }
770 
771     aPropSeq[3].Name = aIsVisible;
772     aPropSeq[3].Value <<= pEntry->IsVisible();
773 
774     return aPropSeq;
775 }
776 
777 SfxTabPage *CreateSvxMenuConfigPage( Window *pParent, const SfxItemSet& rSet )
778 {
779     return new SvxMenuConfigPage( pParent, rSet );
780 }
781 
782 SfxTabPage *CreateKeyboardConfigPage( Window *pParent, const SfxItemSet& rSet )
783 {
784     return new SfxAcceleratorConfigPage( pParent, rSet );
785 }
786 
787 SfxTabPage *CreateSvxToolbarConfigPage( Window *pParent, const SfxItemSet& rSet )
788 {
789     return new SvxToolbarConfigPage( pParent, rSet );
790 }
791 
792 SfxTabPage *CreateSvxEventConfigPage( Window *pParent, const SfxItemSet& rSet )
793 {
794     return new SvxEventConfigPage( pParent, rSet, SvxEventConfigPage::EarlyInit() );
795 }
796 
797 sal_Bool impl_showKeyConfigTabPage( const css::uno::Reference< css::frame::XFrame >& xFrame )
798 {
799     static ::rtl::OUString SERVICENAME_MODULEMANAGER = ::rtl::OUString::createFromAscii("com.sun.star.frame.ModuleManager");
800     static ::rtl::OUString SERVICENAME_DESKTOP       = ::rtl::OUString::createFromAscii("com.sun.star.frame.Desktop"             );
801     static ::rtl::OUString MODULEID_STARTMODULE      = ::rtl::OUString::createFromAscii("com.sun.star.frame.StartModule"         );
802 
803     try
804     {
805         css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR   = ::comphelper::getProcessServiceFactory();
806         css::uno::Reference< css::frame::XFramesSupplier >     xDesktop(xSMGR->createInstance(SERVICENAME_DESKTOP), css::uno::UNO_QUERY_THROW);
807         css::uno::Reference< css::frame::XModuleManager >     xMM     (xSMGR->createInstance(SERVICENAME_MODULEMANAGER), css::uno::UNO_QUERY_THROW);
808 
809         if (xMM.is() && xFrame.is())
810         {
811             ::rtl::OUString sModuleId = xMM->identify(xFrame);
812             if (
813                 ( sModuleId.getLength()                 ) &&
814                 (!sModuleId.equals(MODULEID_STARTMODULE))
815                )
816                return sal_True;
817         }
818     }
819     catch(const css::uno::Exception&)
820         {}
821 
822     return sal_False;
823 }
824 
825 /******************************************************************************
826  *
827  * SvxConfigDialog is the configuration dialog which is brought up from the
828  * Tools menu. It includes tabs for customizing menus, toolbars, events and
829  * key bindings.
830  *
831  *****************************************************************************/
832 SvxConfigDialog::SvxConfigDialog(
833     Window * pParent, const SfxItemSet* pSet_ )
834     :
835         SfxTabDialog( pParent,
836             CUI_RES( RID_SVXDLG_CUSTOMIZE ), pSet_ )
837 {
838     FreeResource();
839 
840     InitImageType();
841 
842     AddTabPage( RID_SVXPAGE_MENUS, CreateSvxMenuConfigPage, NULL );
843     AddTabPage( RID_SVXPAGE_KEYBOARD, CreateKeyboardConfigPage, NULL );
844     AddTabPage( RID_SVXPAGE_TOOLBARS, CreateSvxToolbarConfigPage, NULL );
845     AddTabPage( RID_SVXPAGE_EVENTS, CreateSvxEventConfigPage, NULL );
846 
847     const SfxPoolItem* pItem =
848         pSet_->GetItem( pSet_->GetPool()->GetWhich( SID_CONFIG ) );
849 
850     if ( pItem )
851     {
852         OUString text = ((const SfxStringItem*)pItem)->GetValue();
853 
854         if (text.indexOf(OUString::createFromAscii(ITEM_TOOLBAR_URL)) == 0)
855         {
856             SetCurPageId( RID_SVXPAGE_TOOLBARS );
857         }
858     }
859 }
860 
861 void SvxConfigDialog::SetFrame(const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& xFrame)
862 {
863     m_xFrame = xFrame;
864 
865     if (!impl_showKeyConfigTabPage( xFrame ))
866         RemoveTabPage( RID_SVXPAGE_KEYBOARD );
867 }
868 
869 SvxConfigDialog::~SvxConfigDialog()
870 {
871 }
872 
873 short SvxConfigDialog::Ok()
874 {
875     return SfxTabDialog::Ok();
876 }
877 
878 void SvxConfigDialog::PageCreated( sal_uInt16 nId, SfxTabPage& rPage )
879 {
880     (void)rPage;
881 
882     switch ( nId )
883     {
884         case RID_SVXPAGE_MENUS:
885         case RID_SVXPAGE_TOOLBARS:
886         case RID_SVXPAGE_KEYBOARD:
887             {
888                 rPage.SetFrame(m_xFrame);
889             }
890             break;
891         case RID_SVXPAGE_EVENTS:
892             {
893                 dynamic_cast< SvxEventConfigPage& >( rPage ).LateInit( m_xFrame );
894             };
895             break;
896         default:
897             break;
898     }
899 }
900 
901 /******************************************************************************
902  *
903  * The SaveInData class is used to hold data for entries in the Save In
904  * ListBox controls in the menu and toolbar tabs
905  *
906  ******************************************************************************/
907 
908 // Initialize static variable which holds default XImageManager
909 uno::Reference< css::ui::XImageManager>* SaveInData::xDefaultImgMgr = NULL;
910 
911 SaveInData::SaveInData(
912     const uno::Reference< css::ui::XUIConfigurationManager >& xCfgMgr,
913     const uno::Reference< css::ui::XUIConfigurationManager >& xParentCfgMgr,
914     const OUString& aModuleId,
915     bool isDocConfig )
916         :
917             bModified( sal_False ),
918             bDocConfig( isDocConfig ),
919             bReadOnly( sal_False ),
920             m_xCfgMgr( xCfgMgr ),
921             m_xParentCfgMgr( xParentCfgMgr )
922 {
923     uno::Reference< beans::XPropertySet > xProps(
924         ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY );
925 
926     xProps->getPropertyValue(
927         OUString::createFromAscii( "DefaultContext" ))
928             >>= m_xComponentContext;
929 
930     m_aSeparatorSeq.realloc( 1 );
931     m_aSeparatorSeq[0].Name  = OUString(
932         RTL_CONSTASCII_USTRINGPARAM( ITEM_DESCRIPTOR_TYPE ) );
933     m_aSeparatorSeq[0].Value <<= css::ui::ItemType::SEPARATOR_LINE;
934 
935     if ( bDocConfig )
936     {
937         uno::Reference< css::ui::XUIConfigurationPersistence >
938             xDocPersistence( GetConfigManager(), uno::UNO_QUERY );
939 
940         bReadOnly = xDocPersistence->isReadOnly();
941     }
942 
943     m_xServiceManager = uno::Reference< lang::XMultiServiceFactory >(
944         ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
945 
946     uno::Reference< container::XNameAccess > xNameAccess(
947         m_xServiceManager->createInstance(
948             OUString( RTL_CONSTASCII_USTRINGPARAM(
949                 "com.sun.star.frame.UICommandDescription" ) ) ),
950         uno::UNO_QUERY );
951 
952     if ( xNameAccess.is() )
953         xNameAccess->getByName( aModuleId ) >>= m_xCommandToLabelMap;
954 
955     if ( !m_xImgMgr.is() )
956     {
957         m_xImgMgr = uno::Reference< css::ui::XImageManager >(
958             GetConfigManager()->getImageManager(), uno::UNO_QUERY );
959     }
960 
961     if ( !IsDocConfig() )
962     {
963         // If this is not a document configuration then it is the settings
964         // for the module (writer, calc, impress etc.) Use this as the default
965         // XImageManager instance
966         xDefaultImgMgr = &m_xImgMgr;
967     }
968     else
969     {
970         // If this is a document configuration then use the module image manager
971         // as default.
972         if ( m_xParentCfgMgr.is() )
973         {
974             m_xParentImgMgr = uno::Reference< css::ui::XImageManager >(
975                 m_xParentCfgMgr->getImageManager(), uno::UNO_QUERY );
976             xDefaultImgMgr = &m_xParentImgMgr;
977         }
978     }
979 }
980 
981 uno::Reference< graphic::XGraphic > GetGraphic(
982     const uno::Reference< css::ui::XImageManager >& xImageManager,
983     const OUString& rCommandURL )
984 {
985     uno::Reference< graphic::XGraphic > result;
986 
987     if ( xImageManager.is() )
988     {
989         // TODO handle large and high contrast graphics
990         uno::Sequence< uno::Reference< graphic::XGraphic > > aGraphicSeq;
991 
992         uno::Sequence< OUString > aImageCmdSeq( 1 );
993         aImageCmdSeq[0] = rCommandURL;
994 
995         try
996         {
997             aGraphicSeq =
998                 xImageManager->getImages( GetImageType(), aImageCmdSeq );
999 
1000             if ( aGraphicSeq.getLength() > 0 )
1001             {
1002                 result =  aGraphicSeq[0];
1003             }
1004         }
1005         catch ( uno::Exception& )
1006         {
1007             // will return empty XGraphic
1008         }
1009     }
1010 
1011     return result;
1012 }
1013 
1014 Image SaveInData::GetImage( const OUString& rCommandURL )
1015 {
1016     Image aImage;
1017 
1018     uno::Reference< graphic::XGraphic > xGraphic =
1019         GetGraphic( m_xImgMgr, rCommandURL );
1020 
1021     if ( xGraphic.is() )
1022     {
1023         aImage = Image( xGraphic );
1024     }
1025     else if ( xDefaultImgMgr != NULL && (*xDefaultImgMgr).is() )
1026     {
1027         xGraphic = GetGraphic( (*xDefaultImgMgr), rCommandURL );
1028 
1029         if ( xGraphic.is() )
1030         {
1031             aImage = Image( xGraphic );
1032         }
1033     }
1034 
1035     return aImage;
1036 }
1037 
1038 bool SaveInData::PersistChanges(
1039     const uno::Reference< uno::XInterface >& xManager )
1040 {
1041     bool result = sal_True;
1042 
1043     try
1044     {
1045         if ( xManager.is() && !IsReadOnly() )
1046         {
1047             uno::Reference< css::ui::XUIConfigurationPersistence >
1048                 xConfigPersistence( xManager, uno::UNO_QUERY );
1049 
1050             if ( xConfigPersistence->isModified() )
1051             {
1052                 xConfigPersistence->store();
1053             }
1054         }
1055     }
1056     catch ( com::sun::star::io::IOException& )
1057     {
1058         result = sal_False;
1059     }
1060 
1061     return result;
1062 }
1063 
1064 /******************************************************************************
1065  *
1066  * The MenuSaveInData class extends SaveInData and provides menu specific
1067  * load and store functionality.
1068  *
1069  ******************************************************************************/
1070 
1071 // Initialize static variable which holds default Menu data
1072 MenuSaveInData* MenuSaveInData::pDefaultData = NULL;
1073 
1074 MenuSaveInData::MenuSaveInData(
1075     const uno::Reference< css::ui::XUIConfigurationManager >& cfgmgr,
1076     const uno::Reference< css::ui::XUIConfigurationManager >& xParentCfgMgr,
1077     const OUString& aModuleId,
1078     bool isDocConfig )
1079     :
1080         SaveInData( cfgmgr, xParentCfgMgr, aModuleId, isDocConfig ),
1081         m_aMenuResourceURL(
1082             RTL_CONSTASCII_USTRINGPARAM( ITEM_MENUBAR_URL ) ),
1083         m_aDescriptorContainer(
1084             RTL_CONSTASCII_USTRINGPARAM( ITEM_DESCRIPTOR_CONTAINER ) ),
1085         pRootEntry( 0 )
1086 {
1087     try
1088     {
1089         OUString url( RTL_CONSTASCII_USTRINGPARAM( ITEM_MENUBAR_URL ) );
1090         m_xMenuSettings = GetConfigManager()->getSettings( url, sal_False );
1091     }
1092     catch ( container::NoSuchElementException& )
1093     {
1094         // will use menu settings for the module
1095     }
1096 
1097     // If this is not a document configuration then it is the settings
1098     // for the module (writer, calc, impress etc.). These settings should
1099     // be set as the default to be used for SaveIn locations that do not
1100     // have custom settings
1101     if ( !IsDocConfig() )
1102     {
1103         SetDefaultData( this );
1104     }
1105 }
1106 
1107 MenuSaveInData::~MenuSaveInData()
1108 {
1109     if ( pRootEntry != NULL )
1110     {
1111         delete pRootEntry;
1112     }
1113 }
1114 
1115 SvxEntries*
1116 MenuSaveInData::GetEntries()
1117 {
1118     if ( pRootEntry == NULL )
1119     {
1120         pRootEntry = new SvxConfigEntry(
1121             String::CreateFromAscii("MainMenus"), String(), sal_True );
1122 
1123         if ( m_xMenuSettings.is() )
1124         {
1125             LoadSubMenus( m_xMenuSettings, String(), pRootEntry );
1126         }
1127         else if ( GetDefaultData() != NULL )
1128         {
1129             // If the doc has no config settings use module config settings
1130             LoadSubMenus( GetDefaultData()->m_xMenuSettings, String(), pRootEntry );
1131         }
1132     }
1133 
1134     return pRootEntry->GetEntries();
1135 }
1136 
1137 void
1138 MenuSaveInData::SetEntries( SvxEntries* pNewEntries )
1139 {
1140     // delete old menu hierarchy first
1141     if ( pRootEntry != NULL )
1142     {
1143         delete pRootEntry->GetEntries();
1144     }
1145 
1146     // now set new menu hierarchy
1147     pRootEntry->SetEntries( pNewEntries );
1148 }
1149 
1150 bool MenuSaveInData::LoadSubMenus(
1151     const uno::Reference< container::XIndexAccess >& xMenuSettings,
1152     const OUString& rBaseTitle,
1153     SvxConfigEntry* pParentData )
1154 {
1155     SvxEntries* pEntries = pParentData->GetEntries();
1156 
1157     // Don't access non existing menu configuration!
1158     if ( !xMenuSettings.is() )
1159         return true;
1160 
1161     for ( sal_Int32 nIndex = 0; nIndex < xMenuSettings->getCount(); nIndex++ )
1162     {
1163         uno::Reference< container::XIndexAccess >   xSubMenu;
1164         OUString                aCommandURL;
1165         OUString                aLabel;
1166         bool                    bIsUserDefined = sal_True;
1167 
1168         sal_uInt16 nType( css::ui::ItemType::DEFAULT );
1169 
1170         bool bItem = GetMenuItemData( xMenuSettings, nIndex,
1171             aCommandURL, aLabel, nType, xSubMenu );
1172 
1173         if ( bItem )
1174         {
1175             if ( nType == css::ui::ItemType::DEFAULT )
1176             {
1177                 uno::Any a;
1178                 try
1179                 {
1180                     a = m_xCommandToLabelMap->getByName( aCommandURL );
1181                     bIsUserDefined = sal_False;
1182                 }
1183                 catch ( container::NoSuchElementException& )
1184                 {
1185                     bIsUserDefined = sal_True;
1186                 }
1187 
1188                 // If custom label not set retrieve it from the command
1189                 // to info service
1190                 if ( aLabel.equals( OUString() ) )
1191                 {
1192                     uno::Sequence< beans::PropertyValue > aPropSeq;
1193                     if ( a >>= aPropSeq )
1194                     {
1195                         for ( sal_Int32 i = 0; i < aPropSeq.getLength(); i++ )
1196                         {
1197                             if ( aPropSeq[i].Name.equalsAscii( ITEM_DESCRIPTOR_LABEL ) )
1198                             {
1199                                 aPropSeq[i].Value >>= aLabel;
1200                                 break;
1201                             }
1202                         }
1203                     }
1204                 }
1205 
1206                 if ( xSubMenu.is() )
1207                 {
1208                     // popup menu
1209                     SvxConfigEntry* pEntry = new SvxConfigEntry(
1210                         aLabel, aCommandURL, sal_True );
1211 
1212                     pEntry->SetUserDefined( bIsUserDefined );
1213 
1214                     pEntries->push_back( pEntry );
1215 
1216                     OUString subMenuTitle( rBaseTitle );
1217 
1218                     if ( subMenuTitle.getLength() != 0 )
1219                     {
1220                         subMenuTitle +=
1221                             OUString::createFromAscii(pMenuSeparatorStr);
1222                     }
1223                     else
1224                     {
1225                         pEntry->SetMain( sal_True );
1226                     }
1227 
1228                     subMenuTitle += stripHotKey( aLabel );
1229 
1230                     LoadSubMenus( xSubMenu, subMenuTitle, pEntry );
1231                 }
1232                 else
1233                 {
1234                     SvxConfigEntry* pEntry = new SvxConfigEntry(
1235                         aLabel, aCommandURL, sal_False );
1236                     pEntry->SetUserDefined( bIsUserDefined );
1237                     pEntries->push_back( pEntry );
1238                 }
1239             }
1240             else
1241             {
1242                 SvxConfigEntry* pEntry = new SvxConfigEntry;
1243                 pEntry->SetUserDefined( bIsUserDefined );
1244                 pEntries->push_back( pEntry );
1245             }
1246         }
1247     }
1248     return true;
1249 }
1250 
1251 bool MenuSaveInData::Apply()
1252 {
1253     bool result = sal_False;
1254 
1255     if ( IsModified() )
1256     {
1257         // Apply new menu bar structure to our settings container
1258         m_xMenuSettings = uno::Reference< container::XIndexAccess >(
1259             GetConfigManager()->createSettings(), uno::UNO_QUERY );
1260 
1261         uno::Reference< container::XIndexContainer > xIndexContainer (
1262             m_xMenuSettings, uno::UNO_QUERY );
1263 
1264         uno::Reference< lang::XSingleComponentFactory > xFactory (
1265             m_xMenuSettings, uno::UNO_QUERY );
1266 
1267         Apply( pRootEntry, xIndexContainer, xFactory, NULL );
1268 
1269         try
1270         {
1271             if ( GetConfigManager()->hasSettings( m_aMenuResourceURL ) )
1272             {
1273                 GetConfigManager()->replaceSettings(
1274                     m_aMenuResourceURL, m_xMenuSettings );
1275             }
1276             else
1277             {
1278                 GetConfigManager()->insertSettings(
1279                     m_aMenuResourceURL, m_xMenuSettings );
1280             }
1281         }
1282         catch ( container::NoSuchElementException& )
1283         {
1284             OSL_TRACE("caught container::NoSuchElementException saving settings");
1285         }
1286         catch ( com::sun::star::io::IOException& )
1287         {
1288             OSL_TRACE("caught IOException saving settings");
1289         }
1290         catch ( com::sun::star::uno::Exception& )
1291         {
1292             OSL_TRACE("caught some other exception saving settings");
1293         }
1294 
1295         SetModified( sal_False );
1296 
1297         result = PersistChanges( GetConfigManager() );
1298     }
1299 
1300     return result;
1301 }
1302 
1303 void MenuSaveInData::Apply(
1304     SvxConfigEntry* pRootEntry_,
1305     uno::Reference< container::XIndexContainer >& rMenuBar,
1306     uno::Reference< lang::XSingleComponentFactory >& rFactory,
1307     SvLBoxEntry *pParentEntry )
1308 {
1309     (void)pRootEntry_;
1310     (void)pParentEntry;
1311 
1312     SvxEntries::const_iterator iter = GetEntries()->begin();
1313     SvxEntries::const_iterator end = GetEntries()->end();
1314 
1315     for ( ; iter != end; iter++ )
1316     {
1317         SvxConfigEntry* pEntryData = *iter;
1318 
1319         uno::Sequence< beans::PropertyValue > aPropValueSeq =
1320             ConvertSvxConfigEntry( m_xCommandToLabelMap, pEntryData );
1321 
1322         uno::Reference< container::XIndexContainer > xSubMenuBar(
1323             rFactory->createInstanceWithContext( m_xComponentContext ),
1324             uno::UNO_QUERY );
1325 
1326         sal_Int32 nIndex = aPropValueSeq.getLength();
1327         aPropValueSeq.realloc( nIndex + 1 );
1328         aPropValueSeq[nIndex].Name = m_aDescriptorContainer;
1329         aPropValueSeq[nIndex].Value <<= xSubMenuBar;
1330         rMenuBar->insertByIndex(
1331             rMenuBar->getCount(), uno::makeAny( aPropValueSeq ));
1332         ApplyMenu( xSubMenuBar, rFactory, pEntryData );
1333     }
1334 }
1335 
1336 void MenuSaveInData::ApplyMenu(
1337     uno::Reference< container::XIndexContainer >& rMenuBar,
1338     uno::Reference< lang::XSingleComponentFactory >& rFactory,
1339     SvxConfigEntry* pMenuData )
1340 {
1341     SvxEntries::const_iterator iter = pMenuData->GetEntries()->begin();
1342     SvxEntries::const_iterator end = pMenuData->GetEntries()->end();
1343 
1344     for ( ; iter != end; iter++ )
1345     {
1346         SvxConfigEntry* pEntry = *iter;
1347 
1348         if ( pEntry->IsPopup() )
1349         {
1350             uno::Sequence< beans::PropertyValue > aPropValueSeq =
1351                 ConvertSvxConfigEntry( m_xCommandToLabelMap, pEntry );
1352 
1353             uno::Reference< container::XIndexContainer > xSubMenuBar(
1354                 rFactory->createInstanceWithContext( m_xComponentContext ),
1355                     uno::UNO_QUERY );
1356 
1357             sal_Int32 nIndex = aPropValueSeq.getLength();
1358             aPropValueSeq.realloc( nIndex + 1 );
1359             aPropValueSeq[nIndex].Name = m_aDescriptorContainer;
1360             aPropValueSeq[nIndex].Value <<= xSubMenuBar;
1361 
1362             rMenuBar->insertByIndex(
1363                 rMenuBar->getCount(), uno::makeAny( aPropValueSeq ));
1364 
1365             ApplyMenu( xSubMenuBar, rFactory, pEntry );
1366         }
1367         else if ( pEntry->IsSeparator() )
1368         {
1369             rMenuBar->insertByIndex(
1370                 rMenuBar->getCount(), uno::makeAny( m_aSeparatorSeq ));
1371         }
1372         else
1373         {
1374             uno::Sequence< beans::PropertyValue > aPropValueSeq =
1375                 ConvertSvxConfigEntry( m_xCommandToLabelMap, pEntry );
1376             rMenuBar->insertByIndex(
1377                 rMenuBar->getCount(), uno::makeAny( aPropValueSeq ));
1378         }
1379     }
1380 }
1381 
1382 void
1383 MenuSaveInData::Reset()
1384 {
1385     GetConfigManager()->reset();
1386 
1387     delete pRootEntry;
1388     pRootEntry = NULL;
1389 
1390     try
1391     {
1392         m_xMenuSettings = GetConfigManager()->getSettings(
1393             m_aMenuResourceURL, sal_False );
1394     }
1395     catch ( container::NoSuchElementException& )
1396     {
1397         // will use default settings
1398     }
1399 }
1400 
1401 class PopupPainter : public SvLBoxString
1402 {
1403 public:
1404     PopupPainter( SvLBoxEntry* pEntry, const String& rStr )
1405         : SvLBoxString( pEntry, 0, rStr )
1406     { }
1407 
1408     ~PopupPainter() { }
1409 
1410     void Paint( const Point& rPos, SvLBox& rOutDev,
1411         sal_uInt16 nViewDataEntryFlags, SvLBoxEntry* pEntry )
1412     {
1413         SvLBoxString::Paint( rPos, rOutDev, nViewDataEntryFlags, pEntry );
1414 
1415         Color aOldFillColor = rOutDev.GetFillColor();
1416 
1417         SvTreeListBox* pTreeBox = static_cast< SvTreeListBox* >( &rOutDev );
1418         long nX = pTreeBox->GetSizePixel().Width();
1419 
1420         ScrollBar* pVScroll = pTreeBox->GetVScroll();
1421         if ( pVScroll->IsVisible() )
1422         {
1423             nX -= pVScroll->GetSizePixel().Width();
1424         }
1425 
1426         SvViewDataItem* pItem = rOutDev.GetViewDataItem( pEntry, this );
1427         nX -= pItem->aSize.Height();
1428 
1429         long nSize = pItem->aSize.Height() / 2;
1430         long nHalfSize = nSize / 2;
1431         long nY = rPos.Y() + nHalfSize;
1432 
1433         if ( aOldFillColor == COL_WHITE )
1434         {
1435             rOutDev.SetFillColor( Color( COL_BLACK ) );
1436         }
1437         else
1438         {
1439             rOutDev.SetFillColor( Color( COL_WHITE ) );
1440         }
1441 
1442         long n = 0;
1443         while ( n <= nHalfSize )
1444         {
1445             rOutDev.DrawRect( Rectangle( nX+n, nY+n, nX+n, nY+nSize-n ) );
1446             n++;
1447         }
1448 
1449         rOutDev.SetFillColor( aOldFillColor );
1450     }
1451 };
1452 
1453 /******************************************************************************
1454  *
1455  * SvxMenuEntriesListBox is the listbox in which the menu items for a
1456  * particular menu are shown. We have a custom listbox because we need
1457  * to add drag'n'drop support from the Macro Selector and within the
1458  * listbox
1459  *
1460  *****************************************************************************/
1461 SvxMenuEntriesListBox::SvxMenuEntriesListBox(
1462     Window* pParent, const ResId& rResId)
1463     : SvTreeListBox( pParent, rResId )
1464     , pPage( (SvxMenuConfigPage*) pParent )
1465     , m_bIsInternalDrag( sal_False )
1466 {
1467     SetStyle(
1468         GetStyle() | WB_CLIPCHILDREN | WB_HSCROLL | WB_HIDESELECTION );
1469 
1470     SetSpaceBetweenEntries( 3 );
1471     SetEntryHeight( ENTRY_HEIGHT );
1472 
1473     SetHighlightRange();
1474     SetSelectionMode(SINGLE_SELECTION);
1475 
1476     SetDragDropMode( SV_DRAGDROP_CTRL_MOVE  |
1477                      SV_DRAGDROP_APP_COPY   |
1478                      SV_DRAGDROP_ENABLE_TOP |
1479                      SV_DRAGDROP_APP_DROP);
1480 }
1481 
1482 SvxMenuEntriesListBox::~SvxMenuEntriesListBox()
1483 {
1484     // do nothing
1485 }
1486 
1487 // drag and drop support
1488 DragDropMode SvxMenuEntriesListBox::NotifyStartDrag(
1489     TransferDataContainer& aTransferDataContainer, SvLBoxEntry* pEntry )
1490 {
1491     (void)aTransferDataContainer;
1492     (void)pEntry;
1493 
1494     m_bIsInternalDrag = sal_True;
1495     return GetDragDropMode();
1496 }
1497 
1498 void SvxMenuEntriesListBox::DragFinished( sal_Int8 nDropAction )
1499 {
1500     (void)nDropAction;
1501     m_bIsInternalDrag = sal_False;
1502 }
1503 
1504 sal_Int8 SvxMenuEntriesListBox::AcceptDrop( const AcceptDropEvent& rEvt )
1505 {
1506     if ( m_bIsInternalDrag )
1507     {
1508         // internal copy isn't allowed!
1509         if ( rEvt.mnAction == DND_ACTION_COPY )
1510             return DND_ACTION_NONE;
1511         else
1512             return SvTreeListBox::AcceptDrop( rEvt );
1513     }
1514 
1515     // Always do COPY instead of MOVE if D&D comes from outside!
1516     AcceptDropEvent aNewAcceptDropEvent( rEvt );
1517     aNewAcceptDropEvent.mnAction = DND_ACTION_COPY;
1518     return SvTreeListBox::AcceptDrop( aNewAcceptDropEvent );
1519 }
1520 
1521 sal_Bool SvxMenuEntriesListBox::NotifyAcceptDrop( SvLBoxEntry* )
1522 {
1523     return sal_True;
1524 }
1525 
1526 sal_Bool SvxMenuEntriesListBox::NotifyMoving(
1527     SvLBoxEntry* pTarget, SvLBoxEntry* pSource,
1528     SvLBoxEntry*& rpNewParent, sal_uLong& rNewChildPos)
1529 {
1530     // only try to do a move if we are dragging within the list box
1531     if ( m_bIsInternalDrag )
1532     {
1533         if ( pPage->MoveEntryData( pSource, pTarget ) == sal_True )
1534         {
1535             SvTreeListBox::NotifyMoving(
1536                 pTarget, pSource, rpNewParent, rNewChildPos );
1537             return sal_True;
1538         }
1539         else
1540         {
1541             return sal_False;
1542         }
1543     }
1544     else
1545     {
1546         return NotifyCopying( pTarget, pSource, rpNewParent, rNewChildPos );
1547     }
1548 }
1549 
1550 sal_Bool SvxMenuEntriesListBox::NotifyCopying(
1551     SvLBoxEntry* pTarget, SvLBoxEntry* pSource,
1552     SvLBoxEntry*& rpNewParent, sal_uLong& rNewChildPos)
1553 {
1554     (void)pSource;
1555     (void)rpNewParent;
1556     (void)rNewChildPos;
1557 
1558     if ( !m_bIsInternalDrag )
1559     {
1560         // if the target is NULL then add function to the start of the list
1561         pPage->AddFunction( pTarget, pTarget == NULL );
1562 
1563         // AddFunction already adds the listbox entry so return FALSE
1564         // to stop another listbox entry being added
1565         return sal_False;
1566     }
1567 
1568     // Copying is only allowed from external controls, not within the listbox
1569     return sal_False;
1570 }
1571 
1572 void SvxMenuEntriesListBox::KeyInput( const KeyEvent& rKeyEvent )
1573 {
1574     KeyCode keycode = rKeyEvent.GetKeyCode();
1575 
1576     // support DELETE for removing the current entry
1577     if ( keycode == KEY_DELETE )
1578     {
1579         pPage->DeleteSelectedContent();
1580     }
1581     // support CTRL+UP and CTRL+DOWN for moving selected entries
1582     else if ( keycode.GetCode() == KEY_UP && keycode.IsMod1() )
1583     {
1584         pPage->MoveEntry( sal_True );
1585     }
1586     else if ( keycode.GetCode() == KEY_DOWN && keycode.IsMod1() )
1587     {
1588         pPage->MoveEntry( sal_False );
1589     }
1590     else
1591     {
1592         // pass on to superclass
1593         SvTreeListBox::KeyInput( rKeyEvent );
1594     }
1595 }
1596 
1597 // class SvxDescriptionEdit ----------------------------------------------
1598 
1599 SvxDescriptionEdit::SvxDescriptionEdit( Window* pParent, const ResId& _rId ) :
1600 
1601     ExtMultiLineEdit( pParent, _rId )
1602 
1603 {
1604     // calculate the available space for help text
1605     m_aRealRect = Rectangle( Point(), GetSizePixel() );
1606     if ( GetVScrollBar() )
1607         m_aRealRect.Right() -= ( GetVScrollBar()->GetSizePixel().Width() + 4 );
1608 
1609     SetLeftMargin(2);
1610     SetBorderStyle( WINDOW_BORDER_MONO );
1611 }
1612 
1613 // -----------------------------------------------------------------------
1614 
1615 void SvxDescriptionEdit::SetNewText( const String& _rText )
1616 {
1617     String sTemp( _rText );
1618     sal_Bool bShow = sal_False;
1619     if ( sTemp.Len() > 0 )
1620     {
1621         // detect if a scrollbar is necessary
1622         Rectangle aRect = GetTextRect( m_aRealRect, sTemp, TEXT_DRAW_WORDBREAK | TEXT_DRAW_MULTILINE );
1623         bShow = ( aRect.Bottom() > m_aRealRect.Bottom() );
1624     }
1625 
1626     if ( GetVScrollBar() )
1627         GetVScrollBar()->Show( bShow );
1628 
1629     if ( bShow )
1630         sTemp += '\n';
1631 
1632     SetText( sTemp );
1633 }
1634 
1635 /******************************************************************************
1636  *
1637  * SvxConfigPage is the abstract base class on which the Menu and Toolbar
1638  * configuration tabpages are based. It includes methods which are common to
1639  * both tabpages to add, delete, move and rename items etc.
1640  *
1641  *****************************************************************************/
1642 SvxConfigPage::SvxConfigPage(
1643     Window *pParent, const SfxItemSet& rSet )
1644     :
1645     SfxTabPage( pParent, CUI_RES( RID_SVXPAGE_MENUS ), rSet ),
1646     bInitialised( sal_False ),
1647     pCurrentSaveInData( 0 ),
1648     aTopLevelSeparator( this, CUI_RES( GRP_MENUS ) ),
1649     aTopLevelLabel( this, CUI_RES( FT_MENUS ) ),
1650     aTopLevelListBox( this, CUI_RES( LB_MENUS ) ),
1651     aNewTopLevelButton( this, CUI_RES( BTN_NEW ) ),
1652     aModifyTopLevelButton( this, CUI_RES( BTN_CHANGE ) ),
1653     aContentsSeparator( this, CUI_RES( GRP_MENU_SEPARATOR ) ),
1654     aContentsLabel( this, CUI_RES( GRP_MENU_ENTRIES ) ),
1655     aContentsListBox( 0 ),
1656     aAddCommandsButton( this, CUI_RES( BTN_ADD_COMMANDS ) ),
1657     aModifyCommandButton( this, CUI_RES( BTN_CHANGE_ENTRY ) ),
1658     aMoveUpButton( this, CUI_RES( BTN_UP ) ),
1659     aMoveDownButton( this, CUI_RES( BTN_DOWN ) ),
1660     aSaveInText( this, CUI_RES( TXT_SAVEIN ) ),
1661     aSaveInListBox( this, CUI_RES( LB_SAVEIN ) ),
1662     aDescriptionLabel( this, CUI_RES( FT_DESCRIPTION ) ),
1663     aDescriptionField( this, CUI_RES( ED_DESCRIPTION ) ),
1664     pSelectorDlg( 0 )
1665 {
1666     aDescriptionField.SetControlBackground( GetSettings().GetStyleSettings().GetDialogColor() );
1667     aDescriptionField.SetAutoScroll( sal_True );
1668     aDescriptionField.EnableCursor( sal_False );
1669 
1670     aMoveUpButton.SetAccessibleName(String(CUI_RES(BUTTON_STR_UP)));
1671     aMoveDownButton.SetAccessibleName(String(CUI_RES(BUTTON_STR_DOWN)));
1672     aMoveUpButton.SetAccessibleRelationMemberOf(&aContentsSeparator);
1673     aMoveDownButton.SetAccessibleRelationMemberOf(&aContentsSeparator);
1674     aNewTopLevelButton.SetAccessibleRelationMemberOf(&aTopLevelSeparator);
1675     aModifyTopLevelButton.SetAccessibleRelationMemberOf(&aTopLevelSeparator);
1676     aAddCommandsButton.SetAccessibleRelationMemberOf(&aContentsSeparator);
1677     aModifyCommandButton.SetAccessibleRelationMemberOf(&aContentsSeparator);
1678 }
1679 
1680 SvxConfigPage::~SvxConfigPage()
1681 {
1682 }
1683 
1684 void SvxConfigPage::Reset( const SfxItemSet& )
1685 {
1686     // If we haven't initialised our XMultiServiceFactory reference
1687     // then Reset is being called at the opening of the dialog.
1688     //
1689     // Load menu configuration data for the module of the currently
1690     // selected document, for the currently selected document, and for
1691     // all other open documents of the same module type
1692     if ( !bInitialised )
1693     {
1694         sal_uInt16 nPos = 0;
1695         uno::Reference < css::ui::XUIConfigurationManager > xCfgMgr;
1696         uno::Reference < css::ui::XUIConfigurationManager > xDocCfgMgr;
1697 
1698         uno::Reference< lang::XMultiServiceFactory > xServiceManager(
1699             ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
1700 
1701         m_xFrame = GetFrame();
1702         OUString aModuleId = GetFrameWithDefaultAndIdentify( m_xFrame );
1703 
1704         // replace %MODULENAME in the label with the correct module name
1705         uno::Reference< css::frame::XModuleManager > xModuleManager(
1706             xServiceManager->createInstance(
1707                 OUString( RTL_CONSTASCII_USTRINGPARAM(
1708                     "com.sun.star.frame.ModuleManager" ) ) ),
1709             uno::UNO_QUERY_THROW );
1710         OUString aModuleName = GetUIModuleName( aModuleId, xModuleManager );
1711 
1712         OUString title = aTopLevelSeparator.GetText();
1713         OUString aSearchString = OUString::createFromAscii( "%MODULENAME" );
1714         sal_Int32 index = title.indexOf( aSearchString );
1715 
1716         if ( index != -1 )
1717         {
1718             title = title.replaceAt(
1719                 index, aSearchString.getLength(), aModuleName );
1720             aTopLevelSeparator.SetText( title );
1721         }
1722 
1723         uno::Reference< css::ui::XModuleUIConfigurationManagerSupplier >
1724             xModuleCfgSupplier( xServiceManager->createInstance(
1725                 OUString( RTL_CONSTASCII_USTRINGPARAM(
1726             "com.sun.star.ui.ModuleUIConfigurationManagerSupplier" ))),
1727             uno::UNO_QUERY );
1728 
1729         // Set up data for module specific menus
1730         SaveInData* pModuleData = NULL;
1731 
1732         try
1733         {
1734             xCfgMgr =
1735                 xModuleCfgSupplier->getUIConfigurationManager( aModuleId );
1736 
1737             pModuleData = CreateSaveInData( xCfgMgr,
1738                                             uno::Reference< css::ui::XUIConfigurationManager >(),
1739                                             aModuleId,
1740                                             sal_False );
1741         }
1742         catch ( container::NoSuchElementException& )
1743         {
1744         }
1745 
1746         if ( pModuleData != NULL )
1747         {
1748             OUString label;
1749             utl::ConfigManager::GetDirectConfigProperty(
1750                 utl::ConfigManager::PRODUCTNAME ) >>= label;
1751             label += OUString::createFromAscii( " " );
1752             label += aModuleName;
1753 
1754             nPos = aSaveInListBox.InsertEntry( label );
1755             aSaveInListBox.SetEntryData( nPos, pModuleData );
1756         }
1757 
1758         // try to retrieve the document based ui configuration manager
1759         OUString aTitle;
1760         uno::Reference< frame::XController > xController =
1761             m_xFrame->getController();
1762         if ( CanConfig( aModuleId ) && xController.is() )
1763         {
1764             uno::Reference< frame::XModel > xModel( xController->getModel() );
1765             if ( xModel.is() )
1766             {
1767                 uno::Reference< css::ui::XUIConfigurationManagerSupplier >
1768                     xCfgSupplier( xModel, uno::UNO_QUERY );
1769 
1770                 if ( xCfgSupplier.is() )
1771                 {
1772                     xDocCfgMgr = xCfgSupplier->getUIConfigurationManager();
1773                 }
1774                 aTitle = ::comphelper::DocumentInfo::getDocumentTitle( xModel );
1775             }
1776         }
1777 
1778         SaveInData* pDocData = NULL;
1779         if ( xDocCfgMgr.is() )
1780         {
1781             pDocData = CreateSaveInData( xDocCfgMgr, xCfgMgr, aModuleId, sal_True );
1782 
1783             if ( !pDocData->IsReadOnly() )
1784             {
1785                 nPos = aSaveInListBox.InsertEntry( aTitle );
1786                 aSaveInListBox.SetEntryData( nPos, pDocData );
1787             }
1788         }
1789 
1790         // if an item to select has been passed in (eg. the ResourceURL for a
1791         // toolbar) then try to select the SaveInData entry that has that item
1792         bool bURLToSelectFound = sal_False;
1793         if ( m_aURLToSelect.getLength() != 0 )
1794         {
1795             if ( pDocData != NULL && pDocData->HasURL( m_aURLToSelect ) )
1796             {
1797                 aSaveInListBox.SelectEntryPos( nPos, sal_True );
1798                 pCurrentSaveInData = pDocData;
1799                 bURLToSelectFound = sal_True;
1800             }
1801             else if ( pModuleData->HasURL( m_aURLToSelect ) )
1802             {
1803                 aSaveInListBox.SelectEntryPos( 0, sal_True );
1804                 pCurrentSaveInData = pModuleData;
1805                 bURLToSelectFound = sal_True;
1806             }
1807         }
1808 
1809         if ( bURLToSelectFound == sal_False )
1810         {
1811             // if the document has menu configuration settings select it
1812             // it the SaveIn listbox, otherwise select the module data
1813             if ( pDocData != NULL && pDocData->HasSettings() )
1814             {
1815                 aSaveInListBox.SelectEntryPos( nPos, sal_True );
1816                 pCurrentSaveInData = pDocData;
1817             }
1818             else
1819             {
1820                 aSaveInListBox.SelectEntryPos( 0, sal_True );
1821                 pCurrentSaveInData = pModuleData;
1822             }
1823         }
1824 
1825 #ifdef DBG_UTIL
1826         DBG_ASSERT( pCurrentSaveInData, "SvxConfigPage::Reset(): no SaveInData" );
1827 #endif
1828 
1829         if ( CanConfig( aModuleId ) )
1830         {
1831             // Load configuration for other open documents which have
1832             // same module type
1833             uno::Sequence< uno::Reference< frame::XFrame > > aFrameList;
1834             try
1835             {
1836                 uno::Reference< frame::XFramesSupplier > xFramesSupplier(
1837                     xServiceManager->createInstance(
1838                         OUString( RTL_CONSTASCII_USTRINGPARAM(
1839                             "com.sun.star.frame.Desktop" ) ) ),
1840                     uno::UNO_QUERY_THROW );
1841 
1842                 uno::Reference< frame::XFrames > xFrames =
1843                     xFramesSupplier->getFrames();
1844 
1845                 aFrameList = xFrames->queryFrames(
1846                     frame::FrameSearchFlag::ALL & ~frame::FrameSearchFlag::SELF );
1847 
1848             }
1849             catch( const uno::Exception& )
1850             {
1851                 DBG_UNHANDLED_EXCEPTION();
1852             }
1853 
1854             for ( sal_Int32 i = 0; i < aFrameList.getLength(); i++ )
1855             {
1856                 SaveInData* pData = NULL;
1857                 uno::Reference < frame::XFrame > xf = aFrameList[i];
1858 
1859                 if ( xf.is() && xf != m_xFrame )
1860                 {
1861                     OUString aCheckId;
1862                     try{
1863                         aCheckId = xModuleManager->identify( xf );
1864                     } catch(const uno::Exception&)
1865                         { aCheckId = ::rtl::OUString(); }
1866 
1867                     if ( aModuleId.equals( aCheckId ) )
1868                     {
1869                         // try to get the document based ui configuration manager
1870                         OUString aTitle2;
1871                         uno::Reference< frame::XController > xController_ =
1872                             xf->getController();
1873 
1874                         if ( xController_.is() )
1875                         {
1876                             uno::Reference< frame::XModel > xModel(
1877                                 xController_->getModel() );
1878 
1879                             if ( xModel.is() )
1880                             {
1881                                 uno::Reference<
1882                                     css::ui::XUIConfigurationManagerSupplier >
1883                                         xCfgSupplier( xModel, uno::UNO_QUERY );
1884 
1885                                 if ( xCfgSupplier.is() )
1886                                 {
1887                                     xDocCfgMgr =
1888                                         xCfgSupplier->getUIConfigurationManager();
1889                                 }
1890                                 aTitle2 = ::comphelper::DocumentInfo::getDocumentTitle( xModel );
1891                             }
1892                         }
1893 
1894                         if ( xDocCfgMgr.is() )
1895                         {
1896                             pData = CreateSaveInData( xDocCfgMgr, xCfgMgr, aModuleId, sal_True );
1897 
1898                             if ( pData && !pData->IsReadOnly() )
1899                             {
1900                                 nPos = aSaveInListBox.InsertEntry( aTitle2 );
1901                                 aSaveInListBox.SetEntryData( nPos, pData );
1902                             }
1903                         }
1904                     }
1905                 }
1906             }
1907         }
1908 
1909         aSaveInListBox.SetSelectHdl(
1910             LINK( this, SvxConfigPage, SelectSaveInLocation ) );
1911 
1912         bInitialised = sal_True;
1913 
1914         Init();
1915     }
1916     else
1917     {
1918         if ( QueryReset() == RET_YES )
1919         {
1920             // Reset menu configuration for currently selected SaveInData
1921             GetSaveInData()->Reset();
1922 
1923             Init();
1924         }
1925     }
1926 }
1927 
1928 ::rtl::OUString SvxConfigPage::GetFrameWithDefaultAndIdentify( uno::Reference< frame::XFrame >& _inout_rxFrame )
1929 {
1930     ::rtl::OUString sModuleID;
1931     try
1932     {
1933         uno::Reference< lang::XMultiServiceFactory > xServiceManager(
1934             ::comphelper::getProcessServiceFactory(), uno::UNO_QUERY_THROW );
1935 
1936         uno::Reference< frame::XFramesSupplier > xFramesSupplier(
1937             xServiceManager->createInstance(
1938                 OUString( RTL_CONSTASCII_USTRINGPARAM(
1939                     "com.sun.star.frame.Desktop" ) ) ),
1940             uno::UNO_QUERY_THROW );
1941 
1942         if ( !_inout_rxFrame.is() )
1943             _inout_rxFrame = xFramesSupplier->getActiveFrame();
1944 
1945         if ( !_inout_rxFrame.is() )
1946         {
1947             uno::Reference< frame::XDesktop > xDesktop( xFramesSupplier, uno::UNO_QUERY_THROW );
1948             _inout_rxFrame = xDesktop->getCurrentFrame();
1949         }
1950 
1951         if ( !_inout_rxFrame.is() && SfxViewFrame::Current() )
1952             _inout_rxFrame = SfxViewFrame::Current()->GetFrame().GetFrameInterface();
1953 
1954         if ( !_inout_rxFrame.is() )
1955         {
1956             DBG_ERRORFILE( "SvxConfigPage::GetFrameWithDefaultAndIdentify(): no frame found!" );
1957             return sModuleID;
1958         }
1959 
1960         uno::Reference< css::frame::XModuleManager > xModuleManager(
1961             xServiceManager->createInstance(
1962                 OUString( RTL_CONSTASCII_USTRINGPARAM(
1963                     "com.sun.star.frame.ModuleManager" ) ) ),
1964             uno::UNO_QUERY_THROW );
1965 
1966         try
1967         {
1968             sModuleID = xModuleManager->identify( _inout_rxFrame );
1969         }
1970         catch ( const frame::UnknownModuleException& )
1971         {
1972         }
1973 
1974     }
1975     catch( const uno::Exception& )
1976     {
1977         DBG_UNHANDLED_EXCEPTION();
1978     }
1979 
1980     return sModuleID;
1981 }
1982 
1983 sal_Bool SvxConfigPage::FillItemSet( SfxItemSet& )
1984 {
1985     bool result = sal_False;
1986 
1987     for ( sal_uInt16 i = 0 ; i < aSaveInListBox.GetEntryCount(); i++ )
1988     {
1989         SaveInData* pData =
1990             (SaveInData*) aSaveInListBox.GetEntryData( i );
1991 
1992         result = pData->Apply();
1993     }
1994     return result;
1995 }
1996 
1997 void SvxConfigPage::PositionContentsListBox()
1998 {
1999     if ( aContentsListBox == NULL )
2000     {
2001         return;
2002     }
2003 
2004     Point p, newp;
2005     Size s, news;
2006     long x, y, width, height;
2007 
2008     // x and width is same as aTopLevelListBox
2009     x = aTopLevelListBox.GetPosPixel().X();
2010     width = aTopLevelListBox.GetSizePixel().Width();
2011 
2012     // y is same as aAddCommandsButton
2013     y = aAddCommandsButton.GetPosPixel().Y();
2014 
2015     // get gap between aAddCommandsButton and aContentsSeparator
2016     p = aContentsSeparator.GetPosPixel();
2017     s = aContentsSeparator.GetSizePixel();
2018     long gap = y - ( p.Y() + s.Height() );
2019 
2020     height = aSaveInListBox.GetPosPixel().Y() - y - gap;
2021 
2022     aContentsListBox->SetPosPixel( Point( x, y ) );
2023     aContentsListBox->SetSizePixel( Size( width, height ) );
2024 }
2025 
2026 IMPL_LINK( SvxConfigPage, SelectSaveInLocation, ListBox *, pBox )
2027 {
2028     (void)pBox;
2029 
2030     pCurrentSaveInData = (SaveInData*) aSaveInListBox.GetEntryData(
2031             aSaveInListBox.GetSelectEntryPos());
2032 
2033     Init();
2034     return 1;
2035 }
2036 
2037 void SvxConfigPage::ReloadTopLevelListBox( SvxConfigEntry* pToSelect )
2038 {
2039     sal_uInt16 nSelectionPos = aTopLevelListBox.GetSelectEntryPos();
2040     aTopLevelListBox.Clear();
2041 
2042     if ( GetSaveInData() && GetSaveInData()->GetEntries() )
2043     {
2044         SvxEntries::const_iterator iter = GetSaveInData()->GetEntries()->begin();
2045         SvxEntries::const_iterator end = GetSaveInData()->GetEntries()->end();
2046 
2047         for ( ; iter != end; iter++ )
2048         {
2049             SvxConfigEntry* pEntryData = *iter;
2050             sal_uInt16 nPos = aTopLevelListBox.InsertEntry( stripHotKey( pEntryData->GetName() ) );
2051             aTopLevelListBox.SetEntryData( nPos, pEntryData );
2052 
2053             if ( pEntryData == pToSelect )
2054                 nSelectionPos = nPos;
2055 
2056             AddSubMenusToUI( stripHotKey( pEntryData->GetName() ), pEntryData );
2057         }
2058     }
2059 #ifdef DBG_UTIL
2060     else
2061     {
2062         DBG_ASSERT( GetSaveInData(), "SvxConfigPage::ReloadTopLevelListBox(): no SaveInData" );
2063         DBG_ASSERT( GetSaveInData()->GetEntries() ,
2064             "SvxConfigPage::ReloadTopLevelListBox(): no SaveInData entries" );
2065     }
2066 #endif
2067 
2068     nSelectionPos = nSelectionPos < aTopLevelListBox.GetEntryCount() ?
2069         nSelectionPos : aTopLevelListBox.GetEntryCount() - 1;
2070 
2071     aTopLevelListBox.SelectEntryPos( nSelectionPos, sal_True );
2072     aTopLevelListBox.GetSelectHdl().Call( this );
2073 }
2074 
2075 void SvxConfigPage::AddSubMenusToUI(
2076     const String& rBaseTitle, SvxConfigEntry* pParentData )
2077 {
2078     SvxEntries::const_iterator iter = pParentData->GetEntries()->begin();
2079     SvxEntries::const_iterator end = pParentData->GetEntries()->end();
2080 
2081     for ( ; iter != end; iter++ )
2082     {
2083         SvxConfigEntry* pEntryData = *iter;
2084 
2085         if ( pEntryData->IsPopup() )
2086         {
2087             OUString subMenuTitle( rBaseTitle );
2088             subMenuTitle += OUString::createFromAscii( pMenuSeparatorStr );
2089             subMenuTitle += stripHotKey( pEntryData->GetName() );
2090 
2091             sal_uInt16 nPos = aTopLevelListBox.InsertEntry( subMenuTitle );
2092             aTopLevelListBox.SetEntryData( nPos, pEntryData );
2093 
2094             AddSubMenusToUI( subMenuTitle, pEntryData );
2095         }
2096     }
2097 }
2098 
2099 SvxEntries* SvxConfigPage::FindParentForChild(
2100     SvxEntries* pRootEntries, SvxConfigEntry* pChildData )
2101 {
2102     SvxEntries::const_iterator iter = pRootEntries->begin();
2103     SvxEntries::const_iterator end = pRootEntries->end();
2104 
2105     for ( ; iter != end; iter++ )
2106     {
2107         SvxConfigEntry* pEntryData = *iter;
2108 
2109         if ( pEntryData == pChildData )
2110         {
2111             return pRootEntries;
2112         }
2113         else if ( pEntryData->IsPopup() )
2114         {
2115             SvxEntries* result =
2116                 FindParentForChild( pEntryData->GetEntries(), pChildData );
2117 
2118             if ( result != NULL )
2119             {
2120                 return result;
2121             }
2122         }
2123     }
2124     return NULL;
2125 }
2126 
2127 SvLBoxEntry* SvxConfigPage::AddFunction(
2128     SvLBoxEntry* pTarget, bool bFront, bool bAllowDuplicates )
2129 {
2130     String aDisplayName = pSelectorDlg->GetSelectedDisplayName();
2131     String aHelpText = pSelectorDlg->GetSelectedHelpText();
2132     String aURL = pSelectorDlg->GetScriptURL();
2133 
2134     if ( !aURL.Len() )
2135     {
2136         return NULL;
2137     }
2138 
2139     SvxConfigEntry* pNewEntryData =
2140         new SvxConfigEntry( aDisplayName, aURL, sal_False );
2141     pNewEntryData->SetUserDefined( sal_True );
2142 
2143     // check that this function is not already in the menu
2144     SvxConfigEntry* pParent = GetTopLevelSelection();
2145 
2146     SvxEntries::const_iterator iter = pParent->GetEntries()->begin();
2147     SvxEntries::const_iterator end = pParent->GetEntries()->end();
2148 
2149     if ( !bAllowDuplicates )
2150     {
2151         while ( iter != end )
2152         {
2153             SvxConfigEntry *pCurEntry = *iter;
2154 
2155             if ( pCurEntry->GetCommand() == pNewEntryData->GetCommand() )
2156             {
2157                 // asynchronous error message, because of MsgBoxes
2158                 PostUserEvent(
2159                     LINK( this, SvxConfigPage, AsyncInfoMsg ) );
2160                 return NULL;
2161             }
2162 
2163             iter++;
2164         }
2165     }
2166 
2167     return InsertEntry( pNewEntryData, pTarget, bFront );
2168 }
2169 
2170 SvLBoxEntry* SvxConfigPage::InsertEntry(
2171     SvxConfigEntry* pNewEntryData,
2172     SvLBoxEntry* pTarget,
2173     bool bFront )
2174 {
2175     // Grab the entries list for the currently selected menu
2176     SvxEntries* pEntries = GetTopLevelSelection()->GetEntries();
2177 
2178     SvLBoxEntry* pNewEntry = NULL;
2179     SvLBoxEntry* pCurEntry =
2180         pTarget != NULL ? pTarget : aContentsListBox->GetCurEntry();
2181 
2182     if ( bFront )
2183     {
2184         pEntries->insert( pEntries->begin(), pNewEntryData );
2185         pNewEntry = InsertEntryIntoUI( pNewEntryData, 0 );
2186     }
2187     else if ( pCurEntry == NULL || pCurEntry == aContentsListBox->Last() )
2188     {
2189         pEntries->push_back( pNewEntryData );
2190         pNewEntry = InsertEntryIntoUI( pNewEntryData );
2191     }
2192     else
2193     {
2194         SvxConfigEntry* pEntryData =
2195             (SvxConfigEntry*) pCurEntry->GetUserData();
2196 
2197         SvxEntries::iterator iter = pEntries->begin();
2198         SvxEntries::const_iterator end = pEntries->end();
2199 
2200         // Advance the iterator to the data for currently selected entry
2201         sal_uInt16 nPos = 0;
2202         while (*iter != pEntryData && ++iter != end)
2203         {
2204             nPos++;
2205         }
2206 
2207         // Now step past it to the entry after the currently selected one
2208         iter++;
2209         nPos++;
2210 
2211         // Now add the new entry to the UI and to the parent's list
2212         if ( iter != end )
2213         {
2214             pEntries->insert( iter, pNewEntryData );
2215             pNewEntry = InsertEntryIntoUI( pNewEntryData, nPos );
2216         }
2217     }
2218 
2219     if ( pNewEntry != NULL )
2220     {
2221         aContentsListBox->Select( pNewEntry );
2222         aContentsListBox->MakeVisible( pNewEntry );
2223 
2224         GetSaveInData()->SetModified( sal_True );
2225     }
2226 
2227     return pNewEntry;
2228 }
2229 
2230 SvLBoxEntry* SvxConfigPage::InsertEntryIntoUI(
2231     SvxConfigEntry* pNewEntryData, sal_uLong nPos )
2232 {
2233     SvLBoxEntry* pNewEntry = NULL;
2234 
2235     if (pNewEntryData->IsSeparator())
2236     {
2237         pNewEntry = aContentsListBox->InsertEntry(
2238             String::CreateFromAscii( pSeparatorStr ),
2239                 0, sal_False, nPos, pNewEntryData );
2240     }
2241     else
2242     {
2243         OUString aName = stripHotKey( pNewEntryData->GetName() );
2244 
2245         Image aImage = GetSaveInData()->GetImage(
2246             pNewEntryData->GetCommand());
2247 
2248         if ( !!aImage )
2249         {
2250             pNewEntry = aContentsListBox->InsertEntry(
2251                 aName, aImage, aImage, 0, sal_False, nPos, pNewEntryData );
2252         }
2253         else
2254         {
2255             pNewEntry = aContentsListBox->InsertEntry(
2256                 aName, 0, sal_False, nPos, pNewEntryData );
2257         }
2258 
2259         if ( pNewEntryData->IsPopup() ||
2260              pNewEntryData->GetStyle() & css::ui::ItemStyle::DROP_DOWN )
2261         {
2262             // add new popup painter, it gets destructed by the entry
2263             pNewEntry->ReplaceItem(
2264                 new PopupPainter( pNewEntry, aName ),
2265                 pNewEntry->ItemCount() - 1 );
2266         }
2267     }
2268 
2269     return pNewEntry;
2270 }
2271 
2272 IMPL_LINK( SvxConfigPage, AsyncInfoMsg, String*, pMsg )
2273 {
2274     (void)pMsg;
2275 
2276     // Asynchronous msg because of D&D
2277     InfoBox( this, CUI_RES(
2278         IBX_MNUCFG_ALREADY_INCLUDED ) ).Execute();
2279 
2280     return 0;
2281 }
2282 
2283 IMPL_LINK( SvxConfigPage, MoveHdl, Button *, pButton )
2284 {
2285     MoveEntry( pButton == &aMoveUpButton );
2286     return 0;
2287 }
2288 
2289 void SvxConfigPage::MoveEntry( bool bMoveUp )
2290 {
2291     SvLBoxEntry *pSourceEntry = aContentsListBox->FirstSelected();
2292     SvLBoxEntry *pTargetEntry = NULL;
2293     SvLBoxEntry *pToSelect = NULL;
2294 
2295     if ( !pSourceEntry )
2296     {
2297         return;
2298     }
2299 
2300     if ( bMoveUp )
2301     {
2302         // Move Up is just a Move Down with the source and target reversed
2303         pTargetEntry = pSourceEntry;
2304         pSourceEntry = aContentsListBox->PrevSibling( pTargetEntry );
2305         pToSelect = pTargetEntry;
2306     }
2307     else
2308     {
2309         pTargetEntry = aContentsListBox->NextSibling( pSourceEntry );
2310         pToSelect = pSourceEntry;
2311     }
2312 
2313     if ( MoveEntryData( pSourceEntry, pTargetEntry ) )
2314     {
2315         aContentsListBox->GetModel()->Move( pSourceEntry, pTargetEntry );
2316         aContentsListBox->Select( pToSelect );
2317         aContentsListBox->MakeVisible( pToSelect );
2318 
2319         UpdateButtonStates();
2320     }
2321 }
2322 
2323 bool SvxConfigPage::MoveEntryData(
2324     SvLBoxEntry* pSourceEntry, SvLBoxEntry* pTargetEntry )
2325 {
2326     //modified by shizhoubo for issue53677
2327     if ( NULL == pSourceEntry || NULL == pTargetEntry )
2328     {
2329         return sal_False;
2330     }
2331 
2332     // Grab the entries list for the currently selected menu
2333     SvxEntries* pEntries = GetTopLevelSelection()->GetEntries();
2334 
2335     SvxConfigEntry* pSourceData =
2336         (SvxConfigEntry*) pSourceEntry->GetUserData();
2337 
2338     if ( pTargetEntry == NULL )
2339     {
2340         RemoveEntry( pEntries, pSourceData );
2341         pEntries->insert(
2342             pEntries->begin(), pSourceData );
2343 
2344         GetSaveInData()->SetModified( sal_True );
2345 
2346         return sal_True;
2347     }
2348     else
2349     {
2350         SvxConfigEntry* pTargetData =
2351             (SvxConfigEntry*) pTargetEntry->GetUserData();
2352 
2353         if ( pSourceData != NULL && pTargetData != NULL )
2354         {
2355             // remove the source entry from our list
2356             RemoveEntry( pEntries, pSourceData );
2357 
2358             SvxEntries::iterator iter = pEntries->begin();
2359             SvxEntries::const_iterator end = pEntries->end();
2360 
2361             // advance the iterator to the position of the target entry
2362             while (*iter != pTargetData && ++iter != end) ;
2363 
2364             // insert the source entry at the position after the target
2365             pEntries->insert( ++iter, pSourceData );
2366 
2367             GetSaveInData()->SetModified( sal_True );
2368 
2369             return sal_True;
2370         }
2371     }
2372 
2373     return sal_False;
2374 }
2375 
2376 SvxMenuConfigPage::SvxMenuConfigPage(
2377     Window *pParent, const SfxItemSet& rSet )
2378     :
2379     SvxConfigPage( pParent, rSet )
2380 {
2381     aContentsListBox = new SvxMenuEntriesListBox( this, CUI_RES( BOX_ENTRIES ) );
2382     FreeResource();
2383 
2384     PositionContentsListBox();
2385     aContentsListBox->SetZOrder( &aAddCommandsButton, WINDOW_ZORDER_BEFOR );
2386 
2387     aTopLevelListBox.SetSelectHdl(
2388         LINK( this, SvxMenuConfigPage, SelectMenu ) );
2389 
2390     aContentsListBox->SetSelectHdl(
2391         LINK( this, SvxMenuConfigPage, SelectMenuEntry ) );
2392 
2393     aMoveUpButton.SetClickHdl ( LINK( this, SvxConfigPage, MoveHdl) );
2394     aMoveDownButton.SetClickHdl ( LINK( this, SvxConfigPage, MoveHdl) );
2395 
2396     aNewTopLevelButton.SetClickHdl  (
2397         LINK( this, SvxMenuConfigPage, NewMenuHdl ) );
2398 
2399     aAddCommandsButton.SetClickHdl  (
2400         LINK( this, SvxMenuConfigPage, AddCommandsHdl ) );
2401 
2402     PopupMenu* pMenu = new PopupMenu( CUI_RES( MODIFY_MENU ) );
2403     pMenu->SetMenuFlags(
2404         pMenu->GetMenuFlags() | MENU_FLAG_ALWAYSSHOWDISABLEDENTRIES );
2405 
2406     aModifyTopLevelButton.SetPopupMenu( pMenu );
2407     aModifyTopLevelButton.SetSelectHdl(
2408         LINK( this, SvxMenuConfigPage, MenuSelectHdl ) );
2409 
2410     PopupMenu* pEntry = new PopupMenu( CUI_RES( MODIFY_ENTRY ) );
2411     pEntry->SetMenuFlags(
2412         pEntry->GetMenuFlags() | MENU_FLAG_ALWAYSSHOWDISABLEDENTRIES );
2413 
2414     aModifyCommandButton.SetPopupMenu( pEntry );
2415     aModifyCommandButton.SetSelectHdl(
2416         LINK( this, SvxMenuConfigPage, EntrySelectHdl ) );
2417 }
2418 
2419 // Populates the Menu combo box
2420 void SvxMenuConfigPage::Init()
2421 {
2422     // ensure that the UI is cleared before populating it
2423     aTopLevelListBox.Clear();
2424     aContentsListBox->Clear();
2425 
2426     ReloadTopLevelListBox();
2427 
2428     aTopLevelListBox.SelectEntryPos(0, sal_True);
2429     aTopLevelListBox.GetSelectHdl().Call(this);
2430 }
2431 
2432 SvxMenuConfigPage::~SvxMenuConfigPage()
2433 {
2434     for ( sal_uInt16 i = 0 ; i < aSaveInListBox.GetEntryCount(); i++ )
2435     {
2436         MenuSaveInData* pData =
2437             (MenuSaveInData*) aSaveInListBox.GetEntryData( i );
2438 
2439         delete pData;
2440     }
2441 
2442     if ( pSelectorDlg != NULL )
2443     {
2444         delete pSelectorDlg;
2445     }
2446 
2447     delete aContentsListBox;
2448 }
2449 
2450 IMPL_LINK( SvxMenuConfigPage, SelectMenuEntry, Control *, pBox )
2451 {
2452     (void)pBox;
2453 
2454     UpdateButtonStates();
2455 
2456     return 1;
2457 }
2458 
2459 void SvxMenuConfigPage::UpdateButtonStates()
2460 {
2461     PopupMenu* pPopup = aModifyCommandButton.GetPopupMenu();
2462 
2463     // Disable Up and Down buttons depending on current selection
2464     SvLBoxEntry* selection = aContentsListBox->GetCurEntry();
2465 
2466     if ( aContentsListBox->GetEntryCount() == 0 || selection == NULL )
2467     {
2468         aMoveUpButton.Enable( sal_False );
2469         aMoveDownButton.Enable( sal_False );
2470 
2471         pPopup->EnableItem( ID_BEGIN_GROUP, sal_True );
2472         pPopup->EnableItem( ID_RENAME, sal_False );
2473         pPopup->EnableItem( ID_DELETE, sal_False );
2474 
2475         aDescriptionField.Clear();
2476 
2477         return;
2478     }
2479 
2480     SvLBoxEntry* first = aContentsListBox->First();
2481     SvLBoxEntry* last = aContentsListBox->Last();
2482 
2483     aMoveUpButton.Enable( selection != first );
2484     aMoveDownButton.Enable( selection != last );
2485 
2486     SvxConfigEntry* pEntryData =
2487         (SvxConfigEntry*) selection->GetUserData();
2488 
2489     if ( pEntryData->IsSeparator() )
2490     {
2491         pPopup->EnableItem( ID_DELETE, sal_True );
2492         pPopup->EnableItem( ID_BEGIN_GROUP, sal_False );
2493         pPopup->EnableItem( ID_RENAME, sal_False );
2494 
2495         aDescriptionField.Clear();
2496     }
2497     else
2498     {
2499         pPopup->EnableItem( ID_BEGIN_GROUP, sal_True );
2500         pPopup->EnableItem( ID_DELETE, sal_True );
2501         pPopup->EnableItem( ID_RENAME, sal_True );
2502 
2503         aDescriptionField.SetNewText( pEntryData->GetHelpText() );
2504     }
2505 }
2506 
2507 void SvxMenuConfigPage::DeleteSelectedTopLevel()
2508 {
2509     SvxConfigEntry* pMenuData = GetTopLevelSelection();
2510 
2511     SvxEntries* pParentEntries =
2512         FindParentForChild( GetSaveInData()->GetEntries(), pMenuData );
2513 
2514     RemoveEntry( pParentEntries, pMenuData );
2515     delete pMenuData;
2516 
2517     ReloadTopLevelListBox();
2518 
2519     GetSaveInData()->SetModified( sal_True );
2520 }
2521 
2522 bool SvxMenuConfigPage::DeleteSelectedContent()
2523 {
2524     SvLBoxEntry *pActEntry = aContentsListBox->FirstSelected();
2525 
2526     if ( pActEntry != NULL )
2527     {
2528         // get currently selected menu entry
2529         SvxConfigEntry* pMenuEntry =
2530             (SvxConfigEntry*) pActEntry->GetUserData();
2531 
2532         // get currently selected menu
2533         SvxConfigEntry* pMenu = GetTopLevelSelection();
2534 
2535         // remove menu entry from the list for this menu
2536         RemoveEntry( pMenu->GetEntries(), pMenuEntry );
2537 
2538         // remove menu entry from UI
2539         aContentsListBox->GetModel()->Remove( pActEntry );
2540 
2541         // if this is a submenu entry, redraw the menus list box
2542         if ( pMenuEntry->IsPopup() )
2543         {
2544             ReloadTopLevelListBox();
2545         }
2546 
2547         // delete data for menu entry
2548         delete pMenuEntry;
2549 
2550         GetSaveInData()->SetModified( sal_True );
2551 
2552         return sal_True;
2553     }
2554     return sal_False;
2555 }
2556 
2557 short SvxMenuConfigPage::QueryReset()
2558 {
2559     String msg =
2560         String( CUI_RES( RID_SVXSTR_CONFIRM_MENU_RESET ) );
2561 
2562     String saveInName = aSaveInListBox.GetEntry(
2563         aSaveInListBox.GetSelectEntryPos() );
2564 
2565     OUString label = replaceSaveInName( msg, saveInName );
2566 
2567     QueryBox qbox( this, WB_YES_NO, label );
2568 
2569     return qbox.Execute();
2570 }
2571 
2572 IMPL_LINK( SvxMenuConfigPage, SelectMenu, ListBox *, pBox )
2573 {
2574     (void)pBox;
2575 
2576     aContentsListBox->Clear();
2577 
2578     SvxConfigEntry* pMenuData = GetTopLevelSelection();
2579 
2580     PopupMenu* pPopup = aModifyTopLevelButton.GetPopupMenu();
2581     if ( pMenuData )
2582     {
2583         pPopup->EnableItem( ID_DELETE, pMenuData->IsDeletable() );
2584         pPopup->EnableItem( ID_RENAME, pMenuData->IsRenamable() );
2585         pPopup->EnableItem( ID_MOVE, pMenuData->IsMovable() );
2586 
2587         SvxEntries* pEntries = pMenuData->GetEntries();
2588         SvxEntries::const_iterator iter = pEntries->begin();
2589 
2590         for ( ; iter != pEntries->end(); iter++ )
2591         {
2592             SvxConfigEntry* pEntry = *iter;
2593             InsertEntryIntoUI( pEntry );
2594         }
2595     }
2596 
2597     UpdateButtonStates();
2598 
2599     return 0;
2600 }
2601 
2602 IMPL_LINK( SvxMenuConfigPage, MenuSelectHdl, MenuButton *, pButton )
2603 {
2604     switch( pButton->GetCurItemId() )
2605     {
2606         case ID_DELETE:
2607         {
2608             DeleteSelectedTopLevel();
2609             break;
2610         }
2611         case ID_RENAME:
2612         {
2613             SvxConfigEntry* pMenuData = GetTopLevelSelection();
2614 
2615             String aNewName( stripHotKey( pMenuData->GetName() ) );
2616             String aDesc = CUI_RESSSTR( RID_SVXSTR_LABEL_NEW_NAME );
2617 
2618             SvxNameDialog* pNameDialog = new SvxNameDialog( this, aNewName, aDesc );
2619             pNameDialog->SetHelpId( HID_SVX_CONFIG_RENAME_MENU );
2620             pNameDialog->SetText( CUI_RESSTR( RID_SVXSTR_RENAME_MENU ) );
2621 
2622             bool ret = pNameDialog->Execute();
2623 
2624             if ( ret == RET_OK ) {
2625                 pNameDialog->GetName( aNewName );
2626                 pMenuData->SetName( aNewName );
2627 
2628                 ReloadTopLevelListBox();
2629 
2630                 GetSaveInData()->SetModified( sal_True );
2631             }
2632 
2633             // #i68101# Moemory leak (!)
2634             delete pNameDialog;
2635 
2636             break;
2637         }
2638         case ID_MOVE:
2639         {
2640             SvxConfigEntry* pMenuData = GetTopLevelSelection();
2641 
2642             SvxMainMenuOrganizerDialog* pDialog =
2643                 new SvxMainMenuOrganizerDialog( this,
2644                     GetSaveInData()->GetEntries(), pMenuData );
2645 
2646             bool ret = pDialog->Execute();
2647 
2648             if ( ret == RET_OK )
2649             {
2650                 GetSaveInData()->SetEntries( pDialog->GetEntries() );
2651 
2652                 ReloadTopLevelListBox( pDialog->GetSelectedEntry() );
2653 
2654                 GetSaveInData()->SetModified( sal_True );
2655             }
2656 
2657             delete pDialog;
2658 
2659             break;
2660         }
2661         default:
2662             return sal_False;
2663     }
2664     return sal_True;
2665 }
2666 
2667 IMPL_LINK( SvxMenuConfigPage, EntrySelectHdl, MenuButton *, pButton )
2668 {
2669     switch( pButton->GetCurItemId() )
2670     {
2671         case ID_ADD_SUBMENU:
2672         {
2673             String aNewName;
2674             String aDesc = CUI_RESSSTR( RID_SVXSTR_SUBMENU_NAME );
2675 
2676             SvxNameDialog* pNameDialog = new SvxNameDialog( this, aNewName, aDesc );
2677             pNameDialog->SetHelpId( HID_SVX_CONFIG_NAME_SUBMENU );
2678             pNameDialog->SetText( CUI_RESSTR( RID_SVXSTR_ADD_SUBMENU ) );
2679 
2680             bool ret = pNameDialog->Execute();
2681 
2682             if ( ret == RET_OK ) {
2683                 pNameDialog->GetName(aNewName);
2684 
2685                 SvxConfigEntry* pNewEntryData =
2686                     new SvxConfigEntry( aNewName, aNewName, sal_True );
2687                 pNewEntryData->SetUserDefined( sal_True );
2688 
2689                 InsertEntry( pNewEntryData );
2690 
2691                 ReloadTopLevelListBox();
2692 
2693                 GetSaveInData()->SetModified( sal_True );
2694             }
2695 
2696             delete pNameDialog;
2697 
2698             break;
2699         }
2700         case ID_BEGIN_GROUP:
2701         {
2702             SvxConfigEntry* pNewEntryData = new SvxConfigEntry;
2703             pNewEntryData->SetUserDefined( sal_True );
2704             InsertEntry( pNewEntryData );
2705 
2706             break;
2707         }
2708         case ID_DELETE:
2709         {
2710             DeleteSelectedContent();
2711             break;
2712         }
2713         case ID_RENAME:
2714         {
2715             SvLBoxEntry* pActEntry = aContentsListBox->GetCurEntry();
2716             SvxConfigEntry* pEntry =
2717                 (SvxConfigEntry*) pActEntry->GetUserData();
2718 
2719             String aNewName( stripHotKey( pEntry->GetName() ) );
2720             String aDesc = CUI_RESSSTR( RID_SVXSTR_LABEL_NEW_NAME );
2721 
2722             SvxNameDialog* pNameDialog = new SvxNameDialog( this, aNewName, aDesc );
2723             pNameDialog->SetHelpId( HID_SVX_CONFIG_RENAME_MENU_ITEM );
2724             pNameDialog->SetText( CUI_RESSTR( RID_SVXSTR_RENAME_MENU ) );
2725 
2726             bool ret = pNameDialog->Execute();
2727 
2728             if ( ret == RET_OK ) {
2729                 pNameDialog->GetName(aNewName);
2730 
2731                 pEntry->SetName( aNewName );
2732                 aContentsListBox->SetEntryText( pActEntry, aNewName );
2733 
2734                 GetSaveInData()->SetModified( sal_True );
2735             }
2736 
2737             delete pNameDialog;
2738 
2739             break;
2740         }
2741         default:
2742         {
2743             return sal_False;
2744         }
2745     }
2746 
2747     if ( GetSaveInData()->IsModified() )
2748     {
2749         UpdateButtonStates();
2750     }
2751 
2752     return sal_True;
2753 }
2754 
2755 IMPL_LINK( SvxMenuConfigPage, AddFunctionHdl,
2756     SvxScriptSelectorDialog *, pDialog )
2757 {
2758     (void)pDialog;
2759 
2760     AddFunction();
2761 
2762     return 0;
2763 }
2764 
2765 IMPL_LINK( SvxMenuConfigPage, NewMenuHdl, Button *, pButton )
2766 {
2767     (void)pButton;
2768 
2769     SvxMainMenuOrganizerDialog* pDialog =
2770         new SvxMainMenuOrganizerDialog( 0,
2771             GetSaveInData()->GetEntries(), NULL, sal_True );
2772 
2773     bool ret = pDialog->Execute();
2774 
2775     if ( ret == RET_OK )
2776     {
2777         GetSaveInData()->SetEntries( pDialog->GetEntries() );
2778         ReloadTopLevelListBox( pDialog->GetSelectedEntry() );
2779         GetSaveInData()->SetModified( sal_True );
2780     }
2781 
2782     delete pDialog;
2783 
2784     return 0;
2785 }
2786 
2787 IMPL_LINK( SvxMenuConfigPage, AddCommandsHdl, Button *, pButton )
2788 {
2789     (void)pButton;
2790 
2791     if ( pSelectorDlg == NULL )
2792     {
2793         // Create Script Selector which also shows builtin commands
2794         pSelectorDlg = new SvxScriptSelectorDialog( this, sal_True, m_xFrame );
2795 
2796         pSelectorDlg->SetAddHdl(
2797             LINK( this, SvxMenuConfigPage, AddFunctionHdl ) );
2798 
2799         pSelectorDlg->SetDialogDescription( String(
2800             CUI_RES( RID_SVXSTR_MENU_ADDCOMMANDS_DESCRIPTION ) ) );
2801     }
2802 
2803     // Position the Script Selector over the Add button so it is
2804     // beside the menu contents list and does not obscure it
2805     pSelectorDlg->SetPosPixel( aAddCommandsButton.GetPosPixel() );
2806 
2807     pSelectorDlg->SetImageProvider(
2808         static_cast< ImageProvider* >( GetSaveInData() ) );
2809 
2810     pSelectorDlg->Show();
2811     return 1;
2812 }
2813 
2814 SaveInData* SvxMenuConfigPage::CreateSaveInData(
2815     const uno::Reference< css::ui::XUIConfigurationManager >& xCfgMgr,
2816     const uno::Reference< css::ui::XUIConfigurationManager >& xParentCfgMgr,
2817     const OUString& aModuleId,
2818     bool bDocConfig )
2819 {
2820     return static_cast< SaveInData* >(
2821         new MenuSaveInData( xCfgMgr, xParentCfgMgr, aModuleId, bDocConfig ));
2822 }
2823 
2824 SvxMainMenuOrganizerDialog::SvxMainMenuOrganizerDialog(
2825     Window* pParent, SvxEntries* entries,
2826     SvxConfigEntry* selection, bool bCreateMenu )
2827     :
2828     ModalDialog( pParent, CUI_RES( MD_MENU_ORGANISER ) ),
2829     aMenuNameText( this, CUI_RES( TXT_MENU_NAME ) ),
2830     aMenuNameEdit( this, CUI_RES( EDIT_MENU_NAME ) ),
2831     aMenuListText( this, CUI_RES( TXT_MENU ) ),
2832     aMenuListBox( this, CUI_RES( BOX_MAIN_MENUS ) ),
2833     aMoveUpButton( this, CUI_RES( BTN_MENU_UP ) ),
2834     aMoveDownButton( this, CUI_RES( BTN_MENU_DOWN ) ),
2835     aOKButton( this, CUI_RES( BTN_MENU_ADD ) ),
2836     aCloseButton( this, CUI_RES( BTN_MENU_CLOSE ) ),
2837     aHelpButton( this, CUI_RES( BTN_MENU_HELP ) ),
2838     bModified( sal_False )
2839 {
2840     FreeResource();
2841 
2842     // Copy the entries list passed in
2843     if ( entries != NULL )
2844     {
2845         SvxConfigEntry* pEntry;
2846         SvLBoxEntry* pLBEntry;
2847 
2848         pEntries = new SvxEntries();
2849         SvxEntries::const_iterator iter = entries->begin();
2850 
2851         while ( iter != entries->end() )
2852         {
2853             pEntry = *iter;
2854             pLBEntry =
2855                 aMenuListBox.InsertEntry( stripHotKey( pEntry->GetName() ) );
2856             pLBEntry->SetUserData( pEntry );
2857             pEntries->push_back( pEntry );
2858 
2859             if ( pEntry == selection )
2860             {
2861                 aMenuListBox.Select( pLBEntry );
2862             }
2863             iter++;
2864         }
2865     }
2866 
2867     if ( bCreateMenu )
2868     {
2869         // Generate custom name for new menu
2870         String prefix =
2871             String( CUI_RES( RID_SVXSTR_NEW_MENU ) );
2872 
2873         OUString newname = generateCustomName( prefix, entries );
2874         OUString newurl = generateCustomMenuURL( pEntries );
2875 
2876         SvxConfigEntry* pNewEntryData =
2877             new SvxConfigEntry( newname, newurl, sal_True );
2878         pNewEntryData->SetUserDefined( sal_True );
2879         pNewEntryData->SetMain( sal_True );
2880 
2881         pNewMenuEntry =
2882             aMenuListBox.InsertEntry( stripHotKey( pNewEntryData->GetName() ) );
2883         aMenuListBox.Select( pNewMenuEntry );
2884 
2885         pNewMenuEntry->SetUserData( pNewEntryData );
2886 
2887         pEntries->push_back( pNewEntryData );
2888 
2889         aMenuNameEdit.SetText( newname );
2890         aMenuNameEdit.SetModifyHdl(
2891             LINK( this, SvxMainMenuOrganizerDialog, ModifyHdl ) );
2892     }
2893     else
2894     {
2895         Point p, newp;
2896         Size s, news;
2897 
2898         // get offset to bottom of name textfield from top of dialog
2899         p = aMenuNameEdit.GetPosPixel();
2900         s = aMenuNameEdit.GetSizePixel();
2901         long offset = p.Y() + s.Height();
2902 
2903         // reposition menu list and label
2904         aMenuListText.SetPosPixel( aMenuNameText.GetPosPixel() );
2905         aMenuListBox.SetPosPixel( aMenuNameEdit.GetPosPixel() );
2906 
2907         // reposition up and down buttons
2908         p = aMoveUpButton.GetPosPixel();
2909         newp = Point( p.X(), p.Y() - offset );
2910         aMoveUpButton.SetPosPixel( newp );
2911 
2912         p = aMoveDownButton.GetPosPixel();
2913         newp = Point( p.X(), p.Y() - offset );
2914         aMoveDownButton.SetPosPixel( newp );
2915 
2916         // change size of dialog
2917         s = GetSizePixel();
2918         news = Size( s.Width(), s.Height() - offset );
2919         SetSizePixel( news );
2920 
2921         // hide name label and textfield
2922         aMenuNameText.Hide();
2923         aMenuNameEdit.Hide();
2924 
2925         // change the title
2926         SetText( CUI_RES( RID_SVXSTR_MOVE_MENU ) );
2927     }
2928 
2929     aMenuListBox.SetSelectHdl(
2930         LINK( this, SvxMainMenuOrganizerDialog, SelectHdl ) );
2931 
2932     aMoveUpButton.SetClickHdl (
2933         LINK( this, SvxMainMenuOrganizerDialog, MoveHdl) );
2934     aMoveDownButton.SetClickHdl (
2935         LINK( this, SvxMainMenuOrganizerDialog, MoveHdl) );
2936 
2937     aMoveUpButton.SetAccessibleName(String(CUI_RES(BUTTON_STR_UP)));
2938     aMoveDownButton.SetAccessibleName(String(CUI_RES(BUTTON_STR_DOWN)));
2939 }
2940 
2941 IMPL_LINK(SvxMainMenuOrganizerDialog, ModifyHdl, Edit*, pEdit)
2942 {
2943     (void)pEdit;
2944 
2945     // if the Edit control is empty do not change the name
2946     if ( aMenuNameEdit.GetText().Equals( String() ) )
2947     {
2948         return 0;
2949     }
2950 
2951     SvxConfigEntry* pNewEntryData =
2952         (SvxConfigEntry*) pNewMenuEntry->GetUserData();
2953 
2954     pNewEntryData->SetName( aMenuNameEdit.GetText() );
2955 
2956     aMenuListBox.SetEntryText( pNewMenuEntry, pNewEntryData->GetName() );
2957 
2958     return 0;
2959 }
2960 
2961 SvxMainMenuOrganizerDialog::~SvxMainMenuOrganizerDialog()
2962 {
2963 }
2964 
2965 IMPL_LINK( SvxMainMenuOrganizerDialog, SelectHdl, Control*, pCtrl )
2966 {
2967     (void)pCtrl;
2968     UpdateButtonStates();
2969     return 1;
2970 }
2971 
2972 void SvxMainMenuOrganizerDialog::UpdateButtonStates()
2973 {
2974     // Disable Up and Down buttons depending on current selection
2975     SvLBoxEntry* selection = aMenuListBox.GetCurEntry();
2976     SvLBoxEntry* first = aMenuListBox.First();
2977     SvLBoxEntry* last = aMenuListBox.Last();
2978 
2979     aMoveUpButton.Enable( selection != first );
2980     aMoveDownButton.Enable( selection != last );
2981 }
2982 
2983 IMPL_LINK( SvxMainMenuOrganizerDialog, MoveHdl, Button *, pButton )
2984 {
2985     SvLBoxEntry *pSourceEntry = aMenuListBox.FirstSelected();
2986     SvLBoxEntry *pTargetEntry = NULL;
2987 
2988     if ( !pSourceEntry )
2989     {
2990         return 0;
2991     }
2992 
2993     if ( pButton == &aMoveDownButton )
2994     {
2995         pTargetEntry = aMenuListBox.NextSibling( pSourceEntry );
2996     }
2997     else if ( pButton == &aMoveUpButton )
2998     {
2999         // Move Up is just a Move Down with the source and target reversed
3000         pTargetEntry = pSourceEntry;
3001         pSourceEntry = aMenuListBox.PrevSibling( pTargetEntry );
3002     }
3003 
3004     if ( pSourceEntry != NULL && pTargetEntry != NULL )
3005     {
3006         SvxConfigEntry* pSourceData =
3007             (SvxConfigEntry*) pSourceEntry->GetUserData();
3008         SvxConfigEntry* pTargetData =
3009             (SvxConfigEntry*) pTargetEntry->GetUserData();
3010 
3011         SvxEntries::iterator iter1 = GetEntries()->begin();
3012         SvxEntries::iterator iter2 = GetEntries()->begin();
3013         SvxEntries::const_iterator end = GetEntries()->end();
3014 
3015         // Advance the iterators to the positions of the source and target
3016         while (*iter1 != pSourceData && ++iter1 != end) ;
3017         while (*iter2 != pTargetData && ++iter2 != end) ;
3018 
3019         // Now swap the entries in the menu list and in the UI
3020         if ( iter1 != end && iter2 != end )
3021         {
3022             std::swap( *iter1, *iter2 );
3023             aMenuListBox.GetModel()->Move( pSourceEntry, pTargetEntry );
3024             aMenuListBox.MakeVisible( pSourceEntry );
3025 
3026             bModified = sal_True;
3027         }
3028     }
3029 
3030     if ( bModified )
3031     {
3032         UpdateButtonStates();
3033     }
3034 
3035     return 0;
3036 }
3037 
3038 SvxEntries* SvxMainMenuOrganizerDialog::GetEntries()
3039 {
3040     return pEntries;
3041 }
3042 
3043 SvxConfigEntry* SvxMainMenuOrganizerDialog::GetSelectedEntry()
3044 {
3045     return (SvxConfigEntry*)aMenuListBox.FirstSelected()->GetUserData();
3046 }
3047 
3048 const OUString&
3049 SvxConfigEntry::GetHelpText()
3050 {
3051     if ( aHelpText.getLength() == 0 )
3052     {
3053         if ( aCommand.getLength() )
3054         {
3055             aHelpText = Application::GetHelp()->GetHelpText( aCommand, NULL );
3056         }
3057     }
3058 
3059     return aHelpText;
3060 }
3061 
3062 SvxConfigEntry::SvxConfigEntry( const OUString& rDisplayName,
3063                                 const OUString& rCommandURL, bool bPopup, bool bParentData )
3064     : nId( 1 )
3065     , aLabel(rDisplayName)
3066     , aCommand(rCommandURL)
3067     , bPopUp(bPopup)
3068     , bStrEdited( sal_False )
3069     , bIsUserDefined( sal_False )
3070     , bIsMain( sal_False )
3071     , bIsParentData( bParentData )
3072     , bIsVisible( sal_True )
3073     , nStyle( 0 )
3074     , pEntries( 0 )
3075 {
3076     if (bPopUp)
3077     {
3078         pEntries = new SvxEntries();
3079     }
3080 }
3081 
3082 SvxConfigEntry::~SvxConfigEntry()
3083 {
3084     if ( pEntries != NULL )
3085     {
3086         SvxEntries::const_iterator iter = pEntries->begin();
3087 
3088         for ( ; iter != pEntries->end(); iter++ )
3089         {
3090             delete *iter;
3091         }
3092         delete pEntries;
3093     }
3094 }
3095 
3096 bool SvxConfigEntry::IsMovable()
3097 {
3098     if ( IsPopup() && !IsMain() )
3099     {
3100         return sal_False;
3101     }
3102     return sal_True;
3103 }
3104 
3105 bool SvxConfigEntry::IsDeletable()
3106 {
3107     if ( IsMain() && !IsUserDefined() )
3108     {
3109         return sal_False;
3110     }
3111     return sal_True;
3112 }
3113 
3114 bool SvxConfigEntry::IsRenamable()
3115 {
3116     if ( IsMain() && !IsUserDefined() )
3117     {
3118         return sal_False;
3119     }
3120     return sal_True;
3121 }
3122 
3123 SvxToolbarConfigPage::SvxToolbarConfigPage(
3124     Window *pParent, const SfxItemSet& rSet )
3125     :
3126     SvxConfigPage( pParent, rSet )
3127 {
3128     SetHelpId( HID_SVX_CONFIG_TOOLBAR );
3129 
3130     aContentsListBox = new SvxToolbarEntriesListBox(this, CUI_RES(BOX_ENTRIES));
3131     FreeResource();
3132     PositionContentsListBox();
3133     aContentsListBox->SetZOrder( &aAddCommandsButton, WINDOW_ZORDER_BEFOR );
3134 
3135     aContentsListBox->SetHelpId( HID_SVX_CONFIG_TOOLBAR_CONTENTS );
3136     aNewTopLevelButton.SetHelpId( HID_SVX_NEW_TOOLBAR );
3137     aModifyTopLevelButton.SetHelpId( HID_SVX_MODIFY_TOOLBAR );
3138     aAddCommandsButton.SetHelpId( HID_SVX_NEW_TOOLBAR_ITEM );
3139     aModifyCommandButton.SetHelpId( HID_SVX_MODIFY_TOOLBAR_ITEM );
3140     aSaveInListBox.SetHelpId( HID_SVX_SAVE_IN );
3141 
3142     aTopLevelSeparator.SetText(
3143         CUI_RES ( RID_SVXSTR_PRODUCTNAME_TOOLBARS ) );
3144 
3145     aTopLevelLabel.SetText( CUI_RES( RID_SVXSTR_TOOLBAR ) );
3146     aModifyTopLevelButton.SetText( CUI_RES( RID_SVXSTR_TOOLBAR ) );
3147     aContentsSeparator.SetText( CUI_RES( RID_SVXSTR_TOOLBAR_CONTENT ) );
3148     aContentsLabel.SetText( CUI_RES( RID_SVXSTR_COMMANDS ) );
3149 
3150     aTopLevelListBox.SetSelectHdl(
3151         LINK( this, SvxToolbarConfigPage, SelectToolbar ) );
3152     aContentsListBox->SetSelectHdl(
3153         LINK( this, SvxToolbarConfigPage, SelectToolbarEntry ) );
3154 
3155     aNewTopLevelButton.SetClickHdl  (
3156         LINK( this, SvxToolbarConfigPage, NewToolbarHdl ) );
3157 
3158     aAddCommandsButton.SetClickHdl  (
3159         LINK( this, SvxToolbarConfigPage, AddCommandsHdl ) );
3160 
3161     aMoveUpButton.SetClickHdl ( LINK( this, SvxToolbarConfigPage, MoveHdl) );
3162     aMoveDownButton.SetClickHdl ( LINK( this, SvxToolbarConfigPage, MoveHdl) );
3163     // Always enable Up and Down buttons
3164     // added for issue i53677 by shizhoubo
3165     aMoveDownButton.Enable( sal_True );
3166     aMoveUpButton.Enable( sal_True );
3167 
3168     PopupMenu* pMenu = new PopupMenu( CUI_RES( MODIFY_TOOLBAR ) );
3169     pMenu->SetMenuFlags(
3170         pMenu->GetMenuFlags() | MENU_FLAG_ALWAYSSHOWDISABLEDENTRIES );
3171 
3172     aModifyTopLevelButton.SetPopupMenu( pMenu );
3173     aModifyTopLevelButton.SetSelectHdl(
3174         LINK( this, SvxToolbarConfigPage, ToolbarSelectHdl ) );
3175 
3176     PopupMenu* pEntry = new PopupMenu(
3177         CUI_RES( MODIFY_TOOLBAR_CONTENT ) );
3178     pEntry->SetMenuFlags(
3179         pEntry->GetMenuFlags() | MENU_FLAG_ALWAYSSHOWDISABLEDENTRIES );
3180 
3181     aModifyCommandButton.SetPopupMenu( pEntry );
3182     aModifyCommandButton.SetSelectHdl(
3183         LINK( this, SvxToolbarConfigPage, EntrySelectHdl ) );
3184 
3185     // default toolbar to select is standardbar unless a different one
3186     // has been passed in
3187     m_aURLToSelect = OUString::createFromAscii( ITEM_TOOLBAR_URL );
3188     m_aURLToSelect += OUString::createFromAscii( "standardbar" );
3189 
3190     const SfxPoolItem* pItem =
3191         rSet.GetItem( rSet.GetPool()->GetWhich( SID_CONFIG ) );
3192 
3193     if ( pItem )
3194     {
3195         OUString text = ((const SfxStringItem*)pItem)->GetValue();
3196         if (text.indexOf(OUString::createFromAscii(ITEM_TOOLBAR_URL)) == 0)
3197         {
3198             m_aURLToSelect = text.copy( 0 );
3199         }
3200     }
3201 
3202     long nTxtW = aTopLevelLabel.GetCtrlTextWidth( aTopLevelLabel.GetText() );
3203     long nCtrlW = aTopLevelLabel.GetSizePixel().Width();
3204     if ( nTxtW >= nCtrlW )
3205     {
3206         long nDelta = Max( (long)10, nTxtW - nCtrlW );
3207         Size aNewSz = aTopLevelLabel.GetSizePixel();
3208         aNewSz.Width() += nDelta;
3209         aTopLevelLabel.SetSizePixel( aNewSz );
3210         aNewSz = aTopLevelListBox.GetSizePixel();
3211         aNewSz.Width() -= nDelta;
3212         Point aNewPt = aTopLevelListBox.GetPosPixel();
3213         aNewPt.X() += nDelta;
3214         aTopLevelListBox.SetPosSizePixel( aNewPt, aNewSz );
3215     }
3216 }
3217 
3218 SvxToolbarConfigPage::~SvxToolbarConfigPage()
3219 {
3220     for ( sal_uInt16 i = 0 ; i < aSaveInListBox.GetEntryCount(); i++ )
3221     {
3222         ToolbarSaveInData* pData =
3223             (ToolbarSaveInData*) aSaveInListBox.GetEntryData( i );
3224 
3225         delete pData;
3226     }
3227 
3228     if ( pSelectorDlg != NULL )
3229     {
3230         delete pSelectorDlg;
3231     }
3232 
3233 
3234     delete aContentsListBox;
3235 }
3236 
3237 void SvxToolbarConfigPage::DeleteSelectedTopLevel()
3238 {
3239     sal_uInt16 nSelectionPos = aTopLevelListBox.GetSelectEntryPos();
3240     ToolbarSaveInData* pSaveInData = (ToolbarSaveInData*) GetSaveInData();
3241     pSaveInData->RemoveToolbar( GetTopLevelSelection() );
3242 
3243     if ( aTopLevelListBox.GetEntryCount() > 1 )
3244     {
3245         // select next entry after the one being deleted
3246         // selection position is indexed from 0 so need to
3247         // subtract one from the entry count
3248         if ( nSelectionPos != aTopLevelListBox.GetEntryCount() - 1 )
3249         {
3250             aTopLevelListBox.SelectEntryPos( nSelectionPos + 1, sal_True );
3251         }
3252         else
3253         {
3254             aTopLevelListBox.SelectEntryPos( nSelectionPos - 1, sal_True );
3255         }
3256         aTopLevelListBox.GetSelectHdl().Call( this );
3257 
3258         // and now remove the entry
3259         aTopLevelListBox.RemoveEntry( nSelectionPos );
3260     }
3261     else
3262     {
3263         ReloadTopLevelListBox();
3264     }
3265 }
3266 
3267 bool SvxToolbarConfigPage::DeleteSelectedContent()
3268 {
3269     SvLBoxEntry *pActEntry = aContentsListBox->FirstSelected();
3270 
3271     if ( pActEntry != NULL )
3272     {
3273         // get currently selected entry
3274         SvxConfigEntry* pEntry =
3275             (SvxConfigEntry*) pActEntry->GetUserData();
3276 
3277         SvxConfigEntry* pToolbar = GetTopLevelSelection();
3278 
3279         // remove entry from the list for this toolbar
3280         RemoveEntry( pToolbar->GetEntries(), pEntry );
3281 
3282         // remove toolbar entry from UI
3283         aContentsListBox->GetModel()->Remove( pActEntry );
3284 
3285         // delete data for toolbar entry
3286         delete pEntry;
3287 
3288         (( ToolbarSaveInData* ) GetSaveInData())->ApplyToolbar( pToolbar );
3289         UpdateButtonStates();
3290 
3291         // if this is the last entry in the toolbar and it is a user
3292         // defined toolbar pop up a dialog asking the user if they
3293         // want to delete the toolbar
3294         if ( aContentsListBox->GetEntryCount() == 0 &&
3295              GetTopLevelSelection()->IsDeletable() )
3296         {
3297             QueryBox qbox( this,
3298                 CUI_RES( QBX_CONFIRM_DELETE_TOOLBAR ) );
3299 
3300             if ( qbox.Execute() == RET_YES )
3301             {
3302                 DeleteSelectedTopLevel();
3303             }
3304         }
3305 
3306         return sal_True;
3307     }
3308 
3309     return sal_False;
3310 }
3311 
3312 IMPL_LINK( SvxToolbarConfigPage, MoveHdl, Button *, pButton )
3313 {
3314     MoveEntry( pButton == &aMoveUpButton );
3315     return 0;
3316 }
3317 
3318 void SvxToolbarConfigPage::MoveEntry( bool bMoveUp )
3319 {
3320     SvxConfigPage::MoveEntry( bMoveUp );
3321 
3322     // Apply change to currently selected toolbar
3323     SvxConfigEntry* pToolbar = GetTopLevelSelection();
3324     if ( pToolbar )
3325         ((ToolbarSaveInData*)GetSaveInData())->ApplyToolbar( pToolbar );
3326     else
3327     {
3328         DBG_ERRORFILE( "SvxToolbarConfigPage::MoveEntry(): no entry" );
3329         UpdateButtonStates();
3330     }
3331 }
3332 
3333 IMPL_LINK( SvxToolbarConfigPage, ToolbarSelectHdl, MenuButton *, pButton )
3334 {
3335     sal_uInt16 nSelectionPos = aTopLevelListBox.GetSelectEntryPos();
3336 
3337     SvxConfigEntry* pToolbar =
3338         (SvxConfigEntry*)aTopLevelListBox.GetEntryData( nSelectionPos );
3339 
3340     ToolbarSaveInData* pSaveInData = (ToolbarSaveInData*) GetSaveInData();
3341 
3342     switch( pButton->GetCurItemId() )
3343     {
3344         case ID_DELETE:
3345         {
3346             DeleteSelectedTopLevel();
3347             UpdateButtonStates();
3348             break;
3349         }
3350         case ID_RENAME:
3351         {
3352             String aNewName( stripHotKey( pToolbar->GetName() ) );
3353             String aDesc = CUI_RESSSTR( RID_SVXSTR_LABEL_NEW_NAME );
3354 
3355             SvxNameDialog* pNameDialog = new SvxNameDialog( this, aNewName, aDesc );
3356             pNameDialog->SetHelpId( HID_SVX_CONFIG_RENAME_TOOLBAR );
3357             pNameDialog->SetText( CUI_RESSTR( RID_SVXSTR_RENAME_TOOLBAR ) );
3358 
3359             bool ret = pNameDialog->Execute();
3360 
3361             if ( ret == RET_OK )
3362             {
3363                 pNameDialog->GetName(aNewName);
3364 
3365                 pToolbar->SetName( aNewName );
3366                 pSaveInData->ApplyToolbar( pToolbar );
3367 
3368                 // have to use remove and insert to change the name
3369                 aTopLevelListBox.RemoveEntry( nSelectionPos );
3370                 nSelectionPos =
3371                     aTopLevelListBox.InsertEntry( aNewName, nSelectionPos );
3372                 aTopLevelListBox.SetEntryData( nSelectionPos, pToolbar );
3373                 aTopLevelListBox.SelectEntryPos( nSelectionPos );
3374             }
3375 
3376             delete pNameDialog;
3377 
3378             break;
3379         }
3380         case ID_DEFAULT_STYLE:
3381         {
3382             QueryBox qbox( this,
3383                 CUI_RES( QBX_CONFIRM_RESTORE_DEFAULT ) );
3384 
3385             if ( qbox.Execute() == RET_YES )
3386             {
3387                 ToolbarSaveInData* pSaveInData_ =
3388                     (ToolbarSaveInData*) GetSaveInData();
3389 
3390                 pSaveInData_->RestoreToolbar( pToolbar );
3391 
3392                 aTopLevelListBox.GetSelectHdl().Call( this );
3393             }
3394 
3395             break;
3396         }
3397         case ID_ICONS_ONLY:
3398         {
3399             pToolbar->SetStyle( 0 );
3400             pSaveInData->SetSystemStyle( m_xFrame, pToolbar->GetCommand(), 0 );
3401 
3402             aTopLevelListBox.GetSelectHdl().Call( this );
3403 
3404             break;
3405         }
3406         case ID_TEXT_ONLY:
3407         {
3408             pToolbar->SetStyle( 1 );
3409             pSaveInData->SetSystemStyle( m_xFrame, pToolbar->GetCommand(), 1 );
3410 
3411             aTopLevelListBox.GetSelectHdl().Call( this );
3412 
3413             break;
3414         }
3415         case ID_ICONS_AND_TEXT:
3416         {
3417             pToolbar->SetStyle( 2 );
3418             pSaveInData->SetSystemStyle( m_xFrame, pToolbar->GetCommand(), 2 );
3419 
3420             aTopLevelListBox.GetSelectHdl().Call( this );
3421 
3422             break;
3423         }
3424     }
3425     return 1;
3426 }
3427 
3428 IMPL_LINK( SvxToolbarConfigPage, EntrySelectHdl, MenuButton *, pButton )
3429 {
3430     bool bNeedsApply = sal_False;
3431 
3432     // get currently selected toolbar
3433     SvxConfigEntry* pToolbar = GetTopLevelSelection();
3434 
3435     switch( pButton->GetCurItemId() )
3436     {
3437         case ID_RENAME:
3438         {
3439             SvLBoxEntry* pActEntry = aContentsListBox->GetCurEntry();
3440             SvxConfigEntry* pEntry =
3441                 (SvxConfigEntry*) pActEntry->GetUserData();
3442 
3443             String aNewName( stripHotKey( pEntry->GetName() ) );
3444             String aDesc = CUI_RESSSTR( RID_SVXSTR_LABEL_NEW_NAME );
3445 
3446             SvxNameDialog* pNameDialog = new SvxNameDialog( this, aNewName, aDesc );
3447             pNameDialog->SetHelpId( HID_SVX_CONFIG_RENAME_TOOLBAR_ITEM );
3448             pNameDialog->SetText( CUI_RESSTR( RID_SVXSTR_RENAME_TOOLBAR ) );
3449 
3450             bool ret = pNameDialog->Execute();
3451 
3452             if ( ret == RET_OK ) {
3453                 pNameDialog->GetName(aNewName);
3454 
3455                 pEntry->SetName( aNewName );
3456                 aContentsListBox->SetEntryText( pActEntry, aNewName );
3457 
3458                 bNeedsApply = sal_True;
3459             }
3460 
3461             delete pNameDialog;
3462             break;
3463         }
3464         case ID_DEFAULT_COMMAND:
3465         {
3466             SvLBoxEntry* pActEntry = aContentsListBox->GetCurEntry();
3467             SvxConfigEntry* pEntry =
3468                 (SvxConfigEntry*) pActEntry->GetUserData();
3469 
3470             sal_uInt16 nSelectionPos = 0;
3471 
3472             // find position of entry within the list
3473             for ( sal_uInt16 i = 0; i < aContentsListBox->GetEntryCount(); i++ )
3474             {
3475                 if ( aContentsListBox->GetEntry( 0, i ) == pActEntry )
3476                 {
3477                     nSelectionPos = i;
3478                     break;
3479                 }
3480             }
3481 
3482             ToolbarSaveInData* pSaveInData =
3483                 (ToolbarSaveInData*) GetSaveInData();
3484 
3485             OUString aSystemName =
3486                 pSaveInData->GetSystemUIName( pEntry->GetCommand() );
3487 
3488             if ( !pEntry->GetName().equals( aSystemName ) )
3489             {
3490                 pEntry->SetName( aSystemName );
3491                 aContentsListBox->SetEntryText(
3492                     pActEntry, stripHotKey( aSystemName ) );
3493                 bNeedsApply = sal_True;
3494             }
3495 
3496             uno::Sequence< OUString > aURLSeq( 1 );
3497             aURLSeq[ 0 ] = pEntry->GetCommand();
3498 
3499             try
3500             {
3501                 GetSaveInData()->GetImageManager()->removeImages(
3502                     GetImageType(), aURLSeq );
3503 
3504                 // reset backup in entry
3505                 pEntry->SetBackupGraphic(
3506                     uno::Reference< graphic::XGraphic >() );
3507 
3508                 GetSaveInData()->PersistChanges(
3509                     GetSaveInData()->GetImageManager() );
3510 
3511                 aContentsListBox->GetModel()->Remove( pActEntry );
3512 
3513                 SvLBoxEntry* pNewLBEntry =
3514                     InsertEntryIntoUI( pEntry, nSelectionPos );
3515 
3516                 aContentsListBox->SetCheckButtonState( pNewLBEntry,
3517                     pEntry->IsVisible() ?
3518                         SV_BUTTON_CHECKED : SV_BUTTON_UNCHECKED );
3519 
3520                 aContentsListBox->Select( pNewLBEntry );
3521                 aContentsListBox->MakeVisible( pNewLBEntry );
3522 
3523                 bNeedsApply = sal_True;
3524             }
3525             catch ( uno::Exception& )
3526             {
3527                 OSL_TRACE("Error restoring image");
3528             }
3529             break;
3530         }
3531         case ID_BEGIN_GROUP:
3532         {
3533             SvxConfigEntry* pNewEntryData = new SvxConfigEntry;
3534             pNewEntryData->SetUserDefined( sal_True );
3535 
3536             SvLBoxEntry* pNewLBEntry = InsertEntry( pNewEntryData );
3537 
3538             aContentsListBox->SetCheckButtonState(
3539                 pNewLBEntry, SV_BUTTON_TRISTATE );
3540 
3541             bNeedsApply = sal_True;
3542             break;
3543         }
3544         case ID_DELETE:
3545         {
3546             DeleteSelectedContent();
3547             break;
3548         }
3549         case ID_ICON_ONLY:
3550         {
3551             break;
3552         }
3553         case ID_TEXT_ONLY:
3554         {
3555             break;
3556         }
3557         case ID_ICON_AND_TEXT:
3558         {
3559             break;
3560         }
3561         case ID_CHANGE_SYMBOL:
3562         {
3563             SvLBoxEntry* pActEntry = aContentsListBox->GetCurEntry();
3564             SvxConfigEntry* pEntry =
3565                 (SvxConfigEntry*) pActEntry->GetUserData();
3566 
3567             sal_uInt16 nSelectionPos = 0;
3568 
3569             // find position of entry within the list
3570             for ( sal_uInt16 i = 0; i < aContentsListBox->GetEntryCount(); i++ )
3571             {
3572                 if ( aContentsListBox->GetEntry( 0, i ) == pActEntry )
3573                 {
3574                     nSelectionPos = i;
3575                     break;
3576                 }
3577             }
3578 
3579             SvxIconSelectorDialog* pIconDialog =
3580                 new SvxIconSelectorDialog( 0,
3581                     GetSaveInData()->GetImageManager(),
3582                     GetSaveInData()->GetParentImageManager() );
3583 
3584             bool ret = pIconDialog->Execute();
3585 
3586             if ( ret == RET_OK )
3587             {
3588                 uno::Reference< graphic::XGraphic > newgraphic =
3589                     pIconDialog->GetSelectedIcon();
3590 
3591                 if ( newgraphic.is() )
3592                 {
3593                     uno::Sequence< uno::Reference< graphic::XGraphic > >
3594                         aGraphicSeq( 1 );
3595 
3596                     uno::Sequence< OUString > aURLSeq( 1 );
3597                     aURLSeq[ 0 ] = pEntry->GetCommand();
3598 
3599                     if ( !pEntry->GetBackupGraphic().is() )
3600                     {
3601                         uno::Reference< graphic::XGraphic > backup;
3602                         backup = GetGraphic(
3603                             GetSaveInData()->GetImageManager(), aURLSeq[ 0 ] );
3604 
3605                         if ( backup.is() )
3606                         {
3607                             pEntry->SetBackupGraphic( backup );
3608                         }
3609                     }
3610 
3611                     aGraphicSeq[ 0 ] = newgraphic;
3612                     try
3613                     {
3614                         GetSaveInData()->GetImageManager()->replaceImages(
3615                             GetImageType(), aURLSeq, aGraphicSeq );
3616 
3617                         Image aImage( newgraphic );
3618 
3619                         aContentsListBox->GetModel()->Remove( pActEntry );
3620                         SvLBoxEntry* pNewLBEntry =
3621                             InsertEntryIntoUI( pEntry, nSelectionPos );
3622 
3623                         aContentsListBox->SetCheckButtonState( pNewLBEntry,
3624                             pEntry->IsVisible() ?
3625                                 SV_BUTTON_CHECKED : SV_BUTTON_UNCHECKED );
3626 
3627                         aContentsListBox->Select( pNewLBEntry );
3628                         aContentsListBox->MakeVisible( pNewLBEntry );
3629 
3630                         GetSaveInData()->PersistChanges(
3631                             GetSaveInData()->GetImageManager() );
3632                     }
3633                     catch ( uno::Exception& )
3634                     {
3635                         OSL_TRACE("Error replacing image");
3636                     }
3637                 }
3638             }
3639 
3640             delete pIconDialog;
3641 
3642             break;
3643         }
3644         case ID_RESET_SYMBOL:
3645         {
3646             SvLBoxEntry* pActEntry = aContentsListBox->GetCurEntry();
3647             SvxConfigEntry* pEntry =
3648                 (SvxConfigEntry*) pActEntry->GetUserData();
3649 
3650             sal_uInt16 nSelectionPos = 0;
3651 
3652             // find position of entry within the list
3653             for ( sal_uInt16 i = 0; i < aContentsListBox->GetEntryCount(); i++ )
3654             {
3655                 if ( aContentsListBox->GetEntry( 0, i ) == pActEntry )
3656                 {
3657                     nSelectionPos = i;
3658                     break;
3659                 }
3660             }
3661 
3662             uno::Reference< graphic::XGraphic > backup =
3663                 pEntry->GetBackupGraphic();
3664 
3665             uno::Sequence< uno::Reference< graphic::XGraphic > >
3666                 aGraphicSeq( 1 );
3667             aGraphicSeq[ 0 ] = backup;
3668 
3669             uno::Sequence< OUString > aURLSeq( 1 );
3670             aURLSeq[ 0 ] = pEntry->GetCommand();
3671 
3672             try
3673             {
3674                 GetSaveInData()->GetImageManager()->replaceImages(
3675                     GetImageType(), aURLSeq, aGraphicSeq );
3676 
3677                 Image aImage( backup );
3678                 aContentsListBox->GetModel()->Remove( pActEntry );
3679 
3680                 SvLBoxEntry* pNewLBEntry =
3681                     InsertEntryIntoUI( pEntry, nSelectionPos );
3682 
3683                 aContentsListBox->SetCheckButtonState( pNewLBEntry,
3684                     pEntry->IsVisible() ?
3685                         SV_BUTTON_CHECKED : SV_BUTTON_UNCHECKED );
3686 
3687                 aContentsListBox->Select( pNewLBEntry );
3688                 aContentsListBox->MakeVisible( pNewLBEntry );
3689 
3690                 // reset backup in entry
3691                 pEntry->SetBackupGraphic(
3692                     uno::Reference< graphic::XGraphic >() );
3693 
3694                 GetSaveInData()->PersistChanges(
3695                     GetSaveInData()->GetImageManager() );
3696             }
3697             catch ( uno::Exception& )
3698             {
3699                 OSL_TRACE("Error resetting image");
3700             }
3701             break;
3702         }
3703     }
3704 
3705     if ( bNeedsApply == sal_True )
3706     {
3707         (( ToolbarSaveInData* ) GetSaveInData())->ApplyToolbar( pToolbar );
3708         UpdateButtonStates();
3709     }
3710 
3711     return 1;
3712 }
3713 
3714 void SvxToolbarConfigPage::Init()
3715 {
3716     // ensure that the UI is cleared before populating it
3717     aTopLevelListBox.Clear();
3718     aContentsListBox->Clear();
3719 
3720     ReloadTopLevelListBox();
3721 
3722     sal_uInt16 nPos = 0;
3723     if ( m_aURLToSelect.getLength() != 0 )
3724     {
3725         for ( sal_uInt16 i = 0 ; i < aTopLevelListBox.GetEntryCount(); i++ )
3726         {
3727             SvxConfigEntry* pData =
3728                 (SvxConfigEntry*) aTopLevelListBox.GetEntryData( i );
3729 
3730             if ( pData->GetCommand().equals( m_aURLToSelect ) )
3731             {
3732                 nPos = i;
3733                 break;
3734             }
3735         }
3736 
3737         // in future select the default toolbar: Standard
3738         m_aURLToSelect = OUString::createFromAscii( ITEM_TOOLBAR_URL );
3739         m_aURLToSelect += OUString::createFromAscii( "standardbar" );
3740     }
3741 
3742     aTopLevelListBox.SelectEntryPos(nPos, sal_True);
3743     aTopLevelListBox.GetSelectHdl().Call(this);
3744 }
3745 
3746 SaveInData* SvxToolbarConfigPage::CreateSaveInData(
3747     const uno::Reference< css::ui::XUIConfigurationManager >& xCfgMgr,
3748     const uno::Reference< css::ui::XUIConfigurationManager >& xParentCfgMgr,
3749     const OUString& aModuleId,
3750     bool bDocConfig )
3751 {
3752     return static_cast< SaveInData* >(
3753         new ToolbarSaveInData( xCfgMgr, xParentCfgMgr, aModuleId, bDocConfig ));
3754 }
3755 
3756 ToolbarSaveInData::ToolbarSaveInData(
3757     const uno::Reference < css::ui::XUIConfigurationManager >& xCfgMgr,
3758     const uno::Reference < css::ui::XUIConfigurationManager >& xParentCfgMgr,
3759     const OUString& aModuleId,
3760     bool docConfig ) :
3761 
3762     SaveInData              ( xCfgMgr, xParentCfgMgr, aModuleId, docConfig ),
3763     pRootEntry              ( NULL ),
3764     m_aDescriptorContainer  ( RTL_CONSTASCII_USTRINGPARAM( ITEM_DESCRIPTOR_CONTAINER ) )
3765 
3766 {
3767     // Initialize the m_xPersistentWindowState variable which is used
3768     // to get the default properties of system toolbars such as name
3769     uno::Reference< container::XNameAccess > xPWSS(
3770         m_xServiceManager->createInstance(
3771             OUString( RTL_CONSTASCII_USTRINGPARAM(
3772                 "com.sun.star.ui.WindowStateConfiguration" ) ) ),
3773         uno::UNO_QUERY );
3774 
3775     if ( xPWSS.is() )
3776         xPWSS->getByName( aModuleId ) >>= m_xPersistentWindowState;
3777 }
3778 
3779 ToolbarSaveInData::~ToolbarSaveInData()
3780 {
3781     delete pRootEntry;
3782 }
3783 
3784 void ToolbarSaveInData::SetSystemStyle(
3785     uno::Reference< frame::XFrame > xFrame,
3786     const OUString& rResourceURL,
3787     sal_Int32 nStyle )
3788 {
3789     // change the style using the API
3790     SetSystemStyle( rResourceURL, nStyle );
3791 
3792     // this code is a temporary hack as the UI is not updating after
3793     // changing the toolbar style via the API
3794     uno::Reference< css::frame::XLayoutManager > xLayoutManager;
3795     Window *window = NULL;
3796 
3797     uno::Reference< beans::XPropertySet > xPropSet( xFrame, uno::UNO_QUERY );
3798     if ( xPropSet.is() )
3799     {
3800         uno::Any a = xPropSet->getPropertyValue(
3801             OUString( RTL_CONSTASCII_USTRINGPARAM( "LayoutManager" ) ) );
3802         a >>= xLayoutManager;
3803     }
3804 
3805     if ( xLayoutManager.is() )
3806     {
3807         uno::Reference< css::ui::XUIElement > xUIElement =
3808             xLayoutManager->getElement( rResourceURL );
3809 
3810         // check reference before we call getRealInterface. The layout manager
3811         // can only provide references for elements that have been created
3812         // before. It's possible that the current element is not available.
3813         uno::Reference< com::sun::star::awt::XWindow > xWindow;
3814         if ( xUIElement.is() )
3815             xWindow = uno::Reference< com::sun::star::awt::XWindow >(
3816                         xUIElement->getRealInterface(), uno::UNO_QUERY );
3817 
3818         window = VCLUnoHelper::GetWindow( xWindow );
3819     }
3820 
3821     if ( window != NULL && window->GetType() == WINDOW_TOOLBOX )
3822     {
3823         ToolBox* toolbox = (ToolBox*)window;
3824 
3825         if ( nStyle == 0 )
3826         {
3827             toolbox->SetButtonType( BUTTON_SYMBOL );
3828         }
3829         else if ( nStyle == 1 )
3830         {
3831             toolbox->SetButtonType( BUTTON_TEXT );
3832         }
3833         if ( nStyle == 2 )
3834         {
3835             toolbox->SetButtonType( BUTTON_SYMBOLTEXT );
3836         }
3837     }
3838 }
3839 
3840 void ToolbarSaveInData::SetSystemStyle(
3841     const OUString& rResourceURL,
3842     sal_Int32 nStyle )
3843 {
3844     if ( rResourceURL.indexOf( OUString::createFromAscii( "private" ) ) == 0 &&
3845          m_xPersistentWindowState.is() &&
3846          m_xPersistentWindowState->hasByName( rResourceURL ) )
3847     {
3848         try
3849         {
3850             uno::Sequence< beans::PropertyValue > aProps;
3851 
3852             uno::Any a( m_xPersistentWindowState->getByName( rResourceURL ) );
3853 
3854             if ( a >>= aProps )
3855             {
3856                 for ( sal_Int32 i = 0; i < aProps.getLength(); i++ )
3857                 {
3858                     if ( aProps[ i ].Name.equalsAscii( ITEM_DESCRIPTOR_STYLE) )
3859                     {
3860                         aProps[ i ].Value = uno::makeAny( nStyle );
3861                         break;
3862                     }
3863                 }
3864             }
3865 
3866             uno::Reference< container::XNameReplace >
3867                 xNameReplace( m_xPersistentWindowState, uno::UNO_QUERY );
3868 
3869             xNameReplace->replaceByName( rResourceURL, uno::makeAny( aProps ) );
3870         }
3871         catch ( uno::Exception& )
3872         {
3873             // do nothing, a default value is returned
3874             OSL_TRACE("Exception setting toolbar style");
3875         }
3876     }
3877 }
3878 
3879 sal_Int32 ToolbarSaveInData::GetSystemStyle( const OUString& rResourceURL )
3880 {
3881     sal_Int32 result = 0;
3882 
3883     if ( rResourceURL.indexOf( OUString::createFromAscii( "private" ) ) == 0 &&
3884          m_xPersistentWindowState.is() &&
3885          m_xPersistentWindowState->hasByName( rResourceURL ) )
3886     {
3887         try
3888         {
3889             uno::Sequence< beans::PropertyValue > aProps;
3890             uno::Any a( m_xPersistentWindowState->getByName( rResourceURL ) );
3891 
3892             if ( a >>= aProps )
3893             {
3894                 for ( sal_Int32 i = 0; i < aProps.getLength(); i++ )
3895                 {
3896                     if ( aProps[ i ].Name.equalsAscii( ITEM_DESCRIPTOR_STYLE) )
3897                     {
3898                         aProps[i].Value >>= result;
3899                         break;
3900                     }
3901                 }
3902             }
3903         }
3904         catch ( uno::Exception& )
3905         {
3906             // do nothing, a default value is returned
3907         }
3908     }
3909 
3910     return result;
3911 }
3912 
3913 OUString ToolbarSaveInData::GetSystemUIName( const OUString& rResourceURL )
3914 {
3915     OUString result;
3916 
3917     if ( rResourceURL.indexOf( OUString::createFromAscii( "private" ) ) == 0 &&
3918          m_xPersistentWindowState.is() &&
3919          m_xPersistentWindowState->hasByName( rResourceURL ) )
3920     {
3921         try
3922         {
3923             uno::Sequence< beans::PropertyValue > aProps;
3924             uno::Any a( m_xPersistentWindowState->getByName( rResourceURL ) );
3925 
3926             if ( a >>= aProps )
3927             {
3928                 for ( sal_Int32 i = 0; i < aProps.getLength(); i++ )
3929                 {
3930                     if ( aProps[ i ].Name.equalsAscii( ITEM_DESCRIPTOR_UINAME) )
3931                     {
3932                         aProps[ i ].Value >>= result;
3933                     }
3934                 }
3935             }
3936         }
3937         catch ( uno::Exception& )
3938         {
3939             // do nothing, an empty UIName will be returned
3940         }
3941     }
3942 
3943     if ( rResourceURL.indexOf( OUString::createFromAscii( ".uno" ) ) == 0 &&
3944          m_xCommandToLabelMap.is() &&
3945          m_xCommandToLabelMap->hasByName( rResourceURL ) )
3946     {
3947         uno::Any a;
3948         try
3949         {
3950             a = m_xCommandToLabelMap->getByName( rResourceURL );
3951 
3952             uno::Sequence< beans::PropertyValue > aPropSeq;
3953             if ( a >>= aPropSeq )
3954             {
3955                 for ( sal_Int32 i = 0; i < aPropSeq.getLength(); i++ )
3956                 {
3957                     if ( aPropSeq[i].Name.equalsAscii( ITEM_DESCRIPTOR_LABEL ) )
3958                     {
3959                         aPropSeq[i].Value >>= result;
3960                     }
3961                 }
3962             }
3963         }
3964         catch ( uno::Exception& )
3965         {
3966             // not a system command name
3967         }
3968     }
3969 
3970     return result;
3971 }
3972 
3973 bool EntrySort( SvxConfigEntry* a, SvxConfigEntry* b )
3974 {
3975     return a->GetName().compareTo( b->GetName() ) < 0;
3976 }
3977 
3978 SvxEntries* ToolbarSaveInData::GetEntries()
3979 {
3980     typedef ::std::hash_map< ::rtl::OUString,
3981                              bool,
3982                              ::rtl::OUStringHash,
3983                              ::std::equal_to< ::rtl::OUString > > ToolbarInfo;
3984 
3985     ToolbarInfo aToolbarInfo;
3986 
3987     if ( pRootEntry == NULL )
3988     {
3989 
3990         pRootEntry = new SvxConfigEntry(
3991             String::CreateFromAscii("MainToolbars"), String(), sal_True );
3992 
3993         uno::Sequence< uno::Sequence < beans::PropertyValue > > info =
3994             GetConfigManager()->getUIElementsInfo(
3995                 css::ui::UIElementType::TOOLBAR );
3996 
3997         for ( sal_Int32 i = 0; i < info.getLength(); i++ )
3998         {
3999             uno::Sequence< beans::PropertyValue > props = info[ i ];
4000 
4001             OUString url;
4002             OUString systemname;
4003             OUString uiname;
4004 
4005             for ( sal_Int32 j = 0; j < props.getLength(); j++ )
4006             {
4007                 if ( props[ j ].Name.equalsAscii( ITEM_DESCRIPTOR_RESOURCEURL) )
4008                 {
4009                     props[ j ].Value >>= url;
4010                     systemname = url.copy( url.lastIndexOf( '/' ) + 1 );
4011                 }
4012                 else if ( props[ j ].Name.equalsAscii( ITEM_DESCRIPTOR_UINAME) )
4013                 {
4014                     props[ j ].Value >>= uiname;
4015                 }
4016             }
4017 
4018             try
4019             {
4020                 uno::Reference< container::XIndexAccess > xToolbarSettings =
4021                     GetConfigManager()->getSettings( url, sal_False );
4022 
4023                 if ( uiname.getLength() == 0 )
4024                 {
4025                     // try to get the name from m_xPersistentWindowState
4026                     uiname = GetSystemUIName( url );
4027 
4028                     if ( uiname.getLength() == 0 )
4029                     {
4030                         uiname = systemname;
4031                     }
4032                 }
4033 
4034                 SvxConfigEntry* pEntry = new SvxConfigEntry(
4035                     uiname, url, sal_True );
4036 
4037                 pEntry->SetMain( sal_True );
4038                 pEntry->SetStyle( GetSystemStyle( url ) );
4039 
4040 
4041                 // insert into hash_map to filter duplicates from the parent
4042                 aToolbarInfo.insert( ToolbarInfo::value_type( systemname, true ));
4043 
4044                 OUString custom = OUString::createFromAscii(CUSTOM_TOOLBAR_STR);
4045                 if ( systemname.indexOf( custom ) == 0 )
4046                 {
4047                     pEntry->SetUserDefined( sal_True );
4048                 }
4049                 else
4050                 {
4051                     pEntry->SetUserDefined( sal_False );
4052                 }
4053 
4054                 pRootEntry->GetEntries()->push_back( pEntry );
4055 
4056                 LoadToolbar( xToolbarSettings, pEntry );
4057             }
4058             catch ( container::NoSuchElementException& )
4059             {
4060                 // TODO, handle resourceURL with no settings
4061             }
4062         }
4063 
4064         uno::Reference< css::ui::XUIConfigurationManager > xParentCfgMgr = GetParentConfigManager();
4065         if ( xParentCfgMgr.is() )
4066         {
4067             // Retrieve also the parent toolbars to make it possible
4068             // to configure module toolbars and save them into the document
4069             // config manager.
4070             uno::Sequence< uno::Sequence < beans::PropertyValue > > info_ =
4071                 xParentCfgMgr->getUIElementsInfo(
4072                     css::ui::UIElementType::TOOLBAR );
4073 
4074             for ( sal_Int32 i = 0; i < info_.getLength(); i++ )
4075             {
4076                 uno::Sequence< beans::PropertyValue > props = info_[ i ];
4077 
4078                 OUString url;
4079                 OUString systemname;
4080                 OUString uiname;
4081 
4082                 for ( sal_Int32 j = 0; j < props.getLength(); j++ )
4083                 {
4084                     if ( props[ j ].Name.equalsAscii( ITEM_DESCRIPTOR_RESOURCEURL) )
4085                     {
4086                         props[ j ].Value >>= url;
4087                         systemname = url.copy( url.lastIndexOf( '/' ) + 1 );
4088                     }
4089                     else if ( props[ j ].Name.equalsAscii( ITEM_DESCRIPTOR_UINAME) )
4090                     {
4091                         props[ j ].Value >>= uiname;
4092                     }
4093                 }
4094 
4095                 // custom toolbars of the parent are not visible in the document layer
4096                 OUString custom = OUString::createFromAscii(CUSTOM_TOOLBAR_STR);
4097                 if ( systemname.indexOf( custom ) == 0 )
4098                     continue;
4099 
4100                 // check if toolbar is already in the document layer
4101                 ToolbarInfo::const_iterator pIter = aToolbarInfo.find( systemname );
4102                 if ( pIter == aToolbarInfo.end() )
4103                 {
4104                     aToolbarInfo.insert( ToolbarInfo::value_type( systemname, true ));
4105 
4106                     try
4107                     {
4108                         uno::Reference< container::XIndexAccess > xToolbarSettings =
4109                             xParentCfgMgr->getSettings( url, sal_False );
4110 
4111                         if ( uiname.getLength() == 0 )
4112                         {
4113                             // try to get the name from m_xPersistentWindowState
4114                             uiname = GetSystemUIName( url );
4115 
4116                             if ( uiname.getLength() == 0 )
4117                             {
4118                                 uiname = systemname;
4119                             }
4120                         }
4121 
4122                         SvxConfigEntry* pEntry = new SvxConfigEntry(
4123                             uiname, url, sal_True, sal_True );
4124 
4125                         pEntry->SetMain( sal_True );
4126                         pEntry->SetStyle( GetSystemStyle( url ) );
4127 
4128                         if ( systemname.indexOf( custom ) == 0 )
4129                         {
4130                             pEntry->SetUserDefined( sal_True );
4131                         }
4132                         else
4133                         {
4134                             pEntry->SetUserDefined( sal_False );
4135                         }
4136 
4137                         pRootEntry->GetEntries()->push_back( pEntry );
4138 
4139                         LoadToolbar( xToolbarSettings, pEntry );
4140                     }
4141                     catch ( container::NoSuchElementException& )
4142                     {
4143                         // TODO, handle resourceURL with no settings
4144                     }
4145                 }
4146             }
4147         }
4148 
4149         std::sort( GetEntries()->begin(), GetEntries()->end(), EntrySort );
4150     }
4151 
4152     return pRootEntry->GetEntries();
4153 }
4154 
4155 void
4156 ToolbarSaveInData::SetEntries( SvxEntries* pNewEntries )
4157 {
4158     // delete old menu hierarchy first
4159     if ( pRootEntry != NULL && pRootEntry->GetEntries() != NULL )
4160     {
4161         delete pRootEntry->GetEntries();
4162     }
4163 
4164     // now set new menu hierarchy
4165     pRootEntry->SetEntries( pNewEntries );
4166 }
4167 
4168 bool
4169 ToolbarSaveInData::HasURL( const OUString& rURL )
4170 {
4171     SvxEntries::const_iterator iter = GetEntries()->begin();
4172     SvxEntries::const_iterator end = GetEntries()->end();
4173 
4174     while ( iter != end )
4175     {
4176         SvxConfigEntry* pEntry = *iter;
4177 
4178         if ( pEntry->GetCommand().equals( rURL ) )
4179         {
4180             if ( pEntry->IsParentData() )
4181                 return sal_False;
4182             else
4183                 return sal_True;
4184         }
4185 
4186         iter++;
4187     }
4188     return sal_False;
4189 }
4190 
4191 bool ToolbarSaveInData::HasSettings()
4192 {
4193     // return true if there is at least one toolbar entry
4194     if ( GetEntries()->size() > 0 )
4195     {
4196         return sal_True;
4197     }
4198     return sal_False;
4199 }
4200 
4201 void ToolbarSaveInData::Reset()
4202 {
4203     SvxEntries::const_iterator toolbars = GetEntries()->begin();
4204     SvxEntries::const_iterator end = GetEntries()->end();
4205 
4206     // reset each toolbar by calling removeSettings for it's toolbar URL
4207     for ( ; toolbars != end; toolbars++ )
4208     {
4209         SvxConfigEntry* pToolbar = *toolbars;
4210 
4211         try
4212         {
4213             OUString url = pToolbar->GetCommand();
4214             GetConfigManager()->removeSettings( url );
4215         }
4216         catch ( uno::Exception& )
4217         {
4218             // error occured removing the settings
4219             // TODO - add error dialog in future?
4220         }
4221     }
4222 
4223     // persist changes to toolbar storage
4224     PersistChanges( GetConfigManager() );
4225 
4226     // now delete the root SvxConfigEntry the next call to GetEntries()
4227     // causes it to be reinitialised
4228     delete pRootEntry;
4229     pRootEntry = NULL;
4230 
4231     // reset all icons to default
4232     try
4233     {
4234         GetImageManager()->reset();
4235         PersistChanges( GetImageManager() );
4236     }
4237     catch ( uno::Exception& )
4238     {
4239         OSL_TRACE("Error resetting all icons when resetting toolbars");
4240     }
4241 }
4242 
4243 bool ToolbarSaveInData::Apply()
4244 {
4245     // toolbar changes are instantly applied
4246     return sal_False;
4247 }
4248 
4249 void ToolbarSaveInData::ApplyToolbar(
4250     uno::Reference< container::XIndexContainer >& rToolbarBar,
4251     uno::Reference< lang::XSingleComponentFactory >& rFactory,
4252     SvxConfigEntry* pToolbarData )
4253 {
4254     SvxEntries::const_iterator iter = pToolbarData->GetEntries()->begin();
4255     SvxEntries::const_iterator end = pToolbarData->GetEntries()->end();
4256 
4257     for ( ; iter != end; iter++ )
4258     {
4259         SvxConfigEntry* pEntry = *iter;
4260 
4261         if ( pEntry->IsPopup() )
4262         {
4263             uno::Sequence< beans::PropertyValue > aPropValueSeq =
4264                 ConvertToolbarEntry( m_xCommandToLabelMap, pEntry );
4265 
4266             uno::Reference< container::XIndexContainer > xSubMenuBar(
4267                 rFactory->createInstanceWithContext( m_xComponentContext ),
4268                     uno::UNO_QUERY );
4269 
4270             sal_Int32 nIndex = aPropValueSeq.getLength();
4271             aPropValueSeq.realloc( nIndex + 1 );
4272             aPropValueSeq[nIndex].Name = m_aDescriptorContainer;
4273             aPropValueSeq[nIndex].Value <<= xSubMenuBar;
4274             rToolbarBar->insertByIndex(
4275                 rToolbarBar->getCount(), uno::makeAny( aPropValueSeq ));
4276 
4277             ApplyToolbar( xSubMenuBar, rFactory, pEntry );
4278         }
4279         else if ( pEntry->IsSeparator() )
4280         {
4281             rToolbarBar->insertByIndex(
4282                 rToolbarBar->getCount(), uno::makeAny( m_aSeparatorSeq ));
4283         }
4284         else
4285         {
4286             uno::Sequence< beans::PropertyValue > aPropValueSeq =
4287                 ConvertToolbarEntry( m_xCommandToLabelMap, pEntry );
4288 
4289             rToolbarBar->insertByIndex(
4290                 rToolbarBar->getCount(), uno::makeAny( aPropValueSeq ));
4291         }
4292     }
4293 }
4294 
4295 void ToolbarSaveInData::ApplyToolbar( SvxConfigEntry* pToolbar )
4296 {
4297     // Apply new toolbar structure to our settings container
4298     uno::Reference< container::XIndexAccess > xSettings(
4299         GetConfigManager()->createSettings(), uno::UNO_QUERY );
4300 
4301     uno::Reference< container::XIndexContainer > xIndexContainer (
4302         xSettings, uno::UNO_QUERY );
4303 
4304     uno::Reference< lang::XSingleComponentFactory > xFactory (
4305         xSettings, uno::UNO_QUERY );
4306 
4307     ApplyToolbar( xIndexContainer, xFactory, pToolbar );
4308 
4309     uno::Reference< beans::XPropertySet > xProps(
4310         xSettings, uno::UNO_QUERY );
4311 
4312     if ( pToolbar->IsUserDefined() )
4313     {
4314         xProps->setPropertyValue(
4315             OUString::createFromAscii( ITEM_DESCRIPTOR_UINAME ),
4316             uno::makeAny( OUString( pToolbar->GetName() ) ) );
4317     }
4318 
4319     try
4320     {
4321         if ( GetConfigManager()->hasSettings( pToolbar->GetCommand() ) )
4322         {
4323             GetConfigManager()->replaceSettings(
4324                 pToolbar->GetCommand(), xSettings );
4325         }
4326         else
4327         {
4328             GetConfigManager()->insertSettings(
4329                 pToolbar->GetCommand(), xSettings );
4330             if ( pToolbar->IsParentData() )
4331                 pToolbar->SetParentData( false );
4332         }
4333     }
4334     catch ( container::NoSuchElementException& )
4335     {
4336         OSL_TRACE("caught container::NoSuchElementException saving settings");
4337     }
4338     catch ( com::sun::star::io::IOException& )
4339     {
4340         OSL_TRACE("caught IOException saving settings");
4341     }
4342     catch ( com::sun::star::uno::Exception& )
4343     {
4344         OSL_TRACE("caught some other exception saving settings");
4345     }
4346 
4347     PersistChanges( GetConfigManager() );
4348 }
4349 
4350 void ToolbarSaveInData::CreateToolbar( SvxConfigEntry* pToolbar )
4351 {
4352     // show the new toolbar in the UI also
4353     uno::Reference< container::XIndexAccess >
4354         xSettings( GetConfigManager()->createSettings(), uno::UNO_QUERY );
4355 
4356     uno::Reference< container::XIndexContainer >
4357         xIndexContainer ( xSettings, uno::UNO_QUERY );
4358 
4359     uno::Reference< beans::XPropertySet >
4360         xPropertySet( xSettings, uno::UNO_QUERY );
4361 
4362     xPropertySet->setPropertyValue(
4363         OUString::createFromAscii( ITEM_DESCRIPTOR_UINAME ),
4364             uno::makeAny( pToolbar->GetName() ) );
4365 
4366     try
4367     {
4368         GetConfigManager()->insertSettings( pToolbar->GetCommand(), xSettings );
4369     }
4370     catch ( container::ElementExistException& )
4371     {
4372         OSL_TRACE("caught ElementExistsException saving settings");
4373     }
4374     catch ( com::sun::star::lang::IllegalArgumentException& )
4375     {
4376         OSL_TRACE("caught IOException saving settings");
4377     }
4378     catch ( com::sun::star::lang::IllegalAccessException& )
4379     {
4380         OSL_TRACE("caught IOException saving settings");
4381     }
4382     catch ( com::sun::star::uno::Exception& )
4383     {
4384         OSL_TRACE("caught some other exception saving settings");
4385     }
4386 
4387     GetEntries()->push_back( pToolbar );
4388 
4389     PersistChanges( GetConfigManager() );
4390 }
4391 
4392 void ToolbarSaveInData::RemoveToolbar( SvxConfigEntry* pToolbar )
4393 {
4394     try
4395     {
4396         OUString url = pToolbar->GetCommand();
4397         GetConfigManager()->removeSettings( url );
4398         RemoveEntry( GetEntries(), pToolbar );
4399         delete pToolbar;
4400 
4401         PersistChanges( GetConfigManager() );
4402 
4403         // remove the persistent window state data
4404         css::uno::Reference< css::container::XNameContainer > xNameContainer(
4405             m_xPersistentWindowState, css::uno::UNO_QUERY_THROW );
4406 
4407         xNameContainer->removeByName( url );
4408     }
4409     catch ( uno::Exception& )
4410     {
4411         // error occured removing the settings
4412     }
4413 }
4414 
4415 void ToolbarSaveInData::RestoreToolbar( SvxConfigEntry* pToolbar )
4416 {
4417     OUString url = pToolbar->GetCommand();
4418 
4419     // Restore of toolbar is done by removing it from
4420     // it's configuration manager and then getting it again
4421     bool bParentToolbar = pToolbar->IsParentData();
4422 
4423     // Cannot restore parent toolbar
4424     if ( bParentToolbar )
4425         return;
4426 
4427     try
4428     {
4429         GetConfigManager()->removeSettings( url );
4430         pToolbar->GetEntries()->clear();
4431         PersistChanges( GetConfigManager() );
4432     }
4433     catch ( uno::Exception& )
4434     {
4435         // if an error occurs removing the settings then just return
4436         return;
4437     }
4438 
4439     // Now reload the toolbar settings
4440     try
4441     {
4442         uno::Reference< container::XIndexAccess > xToolbarSettings;
4443         if ( IsDocConfig() )
4444         {
4445             xToolbarSettings = GetParentConfigManager()->getSettings( url, sal_False );
4446             pToolbar->SetParentData( true );
4447         }
4448         else
4449             xToolbarSettings = GetConfigManager()->getSettings( url, sal_False );
4450 
4451         LoadToolbar( xToolbarSettings, pToolbar );
4452 
4453         // After reloading, ensure that the icon is reset of each entry
4454         // in the toolbar
4455         SvxEntries::const_iterator iter = pToolbar->GetEntries()->begin();
4456         uno::Sequence< OUString > aURLSeq( 1 );
4457         for ( ; iter != pToolbar->GetEntries()->end(); iter++ )
4458         {
4459             SvxConfigEntry* pEntry = *iter;
4460             aURLSeq[ 0 ] = pEntry->GetCommand();
4461 
4462             try
4463             {
4464                 GetImageManager()->removeImages( GetImageType(), aURLSeq );
4465             }
4466             catch ( uno::Exception& )
4467             {
4468                 OSL_TRACE("Error restoring icon when resetting toolbar");
4469             }
4470         }
4471         PersistChanges( GetImageManager() );
4472     }
4473     catch ( container::NoSuchElementException& )
4474     {
4475         // cannot find the resource URL after removing it
4476         // so no entry will appear in the toolbar list
4477     }
4478 }
4479 
4480 bool ToolbarSaveInData::LoadToolbar(
4481     const uno::Reference< container::XIndexAccess >& xToolbarSettings,
4482     SvxConfigEntry* pParentData )
4483 {
4484     SvxEntries*         pEntries            = pParentData->GetEntries();
4485 
4486     for ( sal_Int32 nIndex = 0; nIndex < xToolbarSettings->getCount(); nIndex++ )
4487     {
4488         uno::Reference< container::XIndexAccess >   xSubMenu;
4489         OUString                aCommandURL;
4490         OUString                aLabel;
4491         bool                    bIsUserDefined = sal_True;
4492         sal_Bool                bIsVisible;
4493         sal_Int32               nStyle;
4494 
4495         sal_uInt16 nType( css::ui::ItemType::DEFAULT );
4496 
4497         bool bItem = GetToolbarItemData( xToolbarSettings, nIndex, aCommandURL,
4498             aLabel, nType, bIsVisible, nStyle, xSubMenu );
4499 
4500         if ( bItem )
4501         {
4502             if ( nType == css::ui::ItemType::DEFAULT )
4503             {
4504                 uno::Any a;
4505                 try
4506                 {
4507                     a = m_xCommandToLabelMap->getByName( aCommandURL );
4508                     bIsUserDefined = sal_False;
4509                 }
4510                 catch ( container::NoSuchElementException& )
4511                 {
4512                     bIsUserDefined = sal_True;
4513                 }
4514 
4515                 // If custom label not set retrieve it from the command
4516                 // to info service
4517                 if ( aLabel.equals( OUString() ) )
4518                 {
4519                     uno::Sequence< beans::PropertyValue > aPropSeq;
4520                     if ( a >>= aPropSeq )
4521                     {
4522                         for ( sal_Int32 i = 0; i < aPropSeq.getLength(); i++ )
4523                         {
4524                             if ( aPropSeq[i].Name.equalsAscii( ITEM_DESCRIPTOR_LABEL ) )
4525                             {
4526                                 aPropSeq[i].Value >>= aLabel;
4527                                 break;
4528                             }
4529                         }
4530                     }
4531                 }
4532 
4533                 if ( xSubMenu.is() )
4534                 {
4535                     SvxConfigEntry* pEntry = new SvxConfigEntry(
4536                         aLabel, aCommandURL, sal_True );
4537 
4538                     pEntry->SetUserDefined( bIsUserDefined );
4539                     pEntry->SetVisible( bIsVisible );
4540 
4541                     pEntries->push_back( pEntry );
4542 
4543                     LoadToolbar( xSubMenu, pEntry );
4544                 }
4545                 else
4546                 {
4547                     SvxConfigEntry* pEntry = new SvxConfigEntry(
4548                         aLabel, aCommandURL, sal_False );
4549                     pEntry->SetUserDefined( bIsUserDefined );
4550                     pEntry->SetVisible( bIsVisible );
4551                     pEntry->SetStyle( nStyle );
4552                     pEntries->push_back( pEntry );
4553                 }
4554             }
4555             else
4556             {
4557                 SvxConfigEntry* pEntry = new SvxConfigEntry;
4558                 pEntry->SetUserDefined( bIsUserDefined );
4559                 pEntries->push_back( pEntry );
4560             }
4561         }
4562     }
4563 
4564     return true;
4565 }
4566 
4567 IMPL_LINK( SvxToolbarConfigPage, SelectToolbarEntry, Control *, pBox )
4568 {
4569     (void)pBox;
4570     UpdateButtonStates();
4571     return 1;
4572 }
4573 
4574 void SvxToolbarConfigPage::UpdateButtonStates()
4575 {
4576     PopupMenu* pPopup = aModifyCommandButton.GetPopupMenu();
4577     pPopup->EnableItem( ID_RENAME, sal_False );
4578     pPopup->EnableItem( ID_DELETE, sal_False );
4579     pPopup->EnableItem( ID_BEGIN_GROUP, sal_False );
4580     pPopup->EnableItem( ID_DEFAULT_COMMAND, sal_False );
4581     pPopup->EnableItem( ID_ICON_ONLY, sal_False );
4582     pPopup->EnableItem( ID_ICON_AND_TEXT, sal_False );
4583     pPopup->EnableItem( ID_TEXT_ONLY, sal_False );
4584     pPopup->EnableItem( ID_CHANGE_SYMBOL, sal_False );
4585     pPopup->EnableItem( ID_RESET_SYMBOL, sal_False );
4586 
4587     aDescriptionField.Clear();
4588 
4589     SvLBoxEntry* selection = aContentsListBox->GetCurEntry();
4590     if ( aContentsListBox->GetEntryCount() == 0 || selection == NULL )
4591     {
4592         return;
4593     }
4594 
4595     SvxConfigEntry* pEntryData = (SvxConfigEntry*) selection->GetUserData();
4596     if ( pEntryData->IsSeparator() )
4597         pPopup->EnableItem( ID_DELETE, sal_True );
4598     else
4599     {
4600         pPopup->EnableItem( ID_BEGIN_GROUP, sal_True );
4601         pPopup->EnableItem( ID_DELETE, sal_True );
4602         pPopup->EnableItem( ID_RENAME, sal_True );
4603         pPopup->EnableItem( ID_ICON_ONLY, sal_True );
4604         pPopup->EnableItem( ID_ICON_AND_TEXT, sal_True );
4605         pPopup->EnableItem( ID_TEXT_ONLY, sal_True );
4606         pPopup->EnableItem( ID_CHANGE_SYMBOL, sal_True );
4607 
4608         if ( !pEntryData->IsUserDefined() )
4609             pPopup->EnableItem( ID_DEFAULT_COMMAND, sal_True );
4610 
4611         if ( pEntryData->IsIconModified() )
4612             pPopup->EnableItem( ID_RESET_SYMBOL, sal_True );
4613 
4614         aDescriptionField.SetNewText( pEntryData->GetHelpText() );
4615     }
4616 }
4617 
4618 short SvxToolbarConfigPage::QueryReset()
4619 {
4620     String msg =
4621         String( CUI_RES( RID_SVXSTR_CONFIRM_TOOLBAR_RESET ) );
4622 
4623     String saveInName = aSaveInListBox.GetEntry(
4624         aSaveInListBox.GetSelectEntryPos() );
4625 
4626     OUString label = replaceSaveInName( msg, saveInName );
4627 
4628     QueryBox qbox( this, WB_YES_NO, label );
4629 
4630     return qbox.Execute();
4631 }
4632 
4633 IMPL_LINK( SvxToolbarConfigPage, SelectToolbar, ListBox *, pBox )
4634 {
4635     (void)pBox;
4636 
4637     aContentsListBox->Clear();
4638 
4639     SvxConfigEntry* pToolbar = GetTopLevelSelection();
4640     if ( pToolbar == NULL )
4641     {
4642         aModifyTopLevelButton.Enable( sal_False );
4643         aModifyCommandButton.Enable( sal_False );
4644         aAddCommandsButton.Enable( sal_False );
4645 
4646         return 0;
4647     }
4648 
4649     aModifyTopLevelButton.Enable( sal_True );
4650     aModifyCommandButton.Enable( sal_True );
4651     aAddCommandsButton.Enable( sal_True );
4652 
4653     PopupMenu* pPopup = aModifyTopLevelButton.GetPopupMenu();
4654 
4655     pPopup->EnableItem( ID_DELETE, pToolbar->IsDeletable() );
4656     pPopup->EnableItem( ID_RENAME, pToolbar->IsRenamable() );
4657     pPopup->EnableItem( ID_DEFAULT_STYLE, !pToolbar->IsRenamable() );
4658 
4659     switch( pToolbar->GetStyle() )
4660     {
4661         case 0:
4662         {
4663             pPopup->CheckItem( ID_ICONS_ONLY );
4664             break;
4665         }
4666         case 1:
4667         {
4668             pPopup->CheckItem( ID_TEXT_ONLY );
4669             break;
4670         }
4671         case 2:
4672         {
4673             pPopup->CheckItem( ID_ICONS_AND_TEXT );
4674             break;
4675         }
4676     }
4677 
4678     SvxEntries* pEntries = pToolbar->GetEntries();
4679     SvxEntries::const_iterator iter = pEntries->begin();
4680 
4681     for ( ; iter != pEntries->end(); iter++ )
4682     {
4683         SvxConfigEntry* pEntry = *iter;
4684 
4685         SvLBoxEntry* pNewLBEntry = InsertEntryIntoUI( pEntry );
4686 
4687         if (pEntry->IsBinding())
4688         {
4689             aContentsListBox->SetCheckButtonState( pNewLBEntry,
4690                 pEntry->IsVisible() ? SV_BUTTON_CHECKED : SV_BUTTON_UNCHECKED );
4691         }
4692         else
4693         {
4694             aContentsListBox->SetCheckButtonState(
4695                 pNewLBEntry, SV_BUTTON_TRISTATE );
4696         }
4697     }
4698 
4699     UpdateButtonStates();
4700 
4701     return 0;
4702 }
4703 
4704 IMPL_LINK( SvxToolbarConfigPage, NewToolbarHdl, Button *, pButton )
4705 {
4706     (void)pButton;
4707 
4708     String prefix =
4709         String( CUI_RES( RID_SVXSTR_NEW_TOOLBAR ) );
4710 
4711     OUString aNewName =
4712         generateCustomName( prefix, GetSaveInData()->GetEntries() );
4713 
4714     OUString aNewURL =
4715         generateCustomURL( GetSaveInData()->GetEntries() );
4716 
4717     SvxNewToolbarDialog* pNameDialog = new SvxNewToolbarDialog( 0, aNewName );
4718 
4719     sal_uInt16 nInsertPos;
4720     for ( sal_uInt16 i = 0 ; i < aSaveInListBox.GetEntryCount(); i++ )
4721     {
4722         SaveInData* pData =
4723             (SaveInData*) aSaveInListBox.GetEntryData( i );
4724 
4725         nInsertPos = pNameDialog->aSaveInListBox.InsertEntry(
4726             aSaveInListBox.GetEntry( i ) );
4727 
4728         pNameDialog->aSaveInListBox.SetEntryData( nInsertPos, pData );
4729     }
4730 
4731     pNameDialog->aSaveInListBox.SelectEntryPos(
4732         aSaveInListBox.GetSelectEntryPos(), sal_True );
4733 
4734     bool ret = pNameDialog->Execute();
4735     if ( ret == RET_OK )
4736     {
4737         pNameDialog->GetName( aNewName );
4738 
4739         nInsertPos = pNameDialog->aSaveInListBox.GetSelectEntryPos();
4740 
4741         ToolbarSaveInData* pData = (ToolbarSaveInData*)
4742             pNameDialog->aSaveInListBox.GetEntryData( nInsertPos );
4743 
4744         if ( GetSaveInData() != pData )
4745         {
4746             aSaveInListBox.SelectEntryPos( nInsertPos, sal_True );
4747             aSaveInListBox.GetSelectHdl().Call(this);
4748         }
4749 
4750         SvxConfigEntry* pToolbar =
4751             new SvxConfigEntry( aNewName, aNewURL, sal_True );
4752 
4753         pToolbar->SetUserDefined( sal_True );
4754         pToolbar->SetMain( sal_True );
4755 
4756         pData->CreateToolbar( pToolbar );
4757 
4758         nInsertPos = aTopLevelListBox.InsertEntry( pToolbar->GetName() );
4759         aTopLevelListBox.SetEntryData( nInsertPos, pToolbar );
4760         aTopLevelListBox.SelectEntryPos( nInsertPos, sal_True );
4761         aTopLevelListBox.GetSelectHdl().Call(this);
4762 
4763         pData->SetModified( sal_True );
4764     }
4765 
4766     delete pNameDialog;
4767 
4768     return 0;
4769 }
4770 
4771 IMPL_LINK( SvxToolbarConfigPage, AddCommandsHdl, Button *, pButton )
4772 {
4773     (void)pButton;
4774 
4775     if ( pSelectorDlg == NULL )
4776     {
4777         // Create Script Selector which shows slot commands
4778         pSelectorDlg = new SvxScriptSelectorDialog( this, sal_True, m_xFrame );
4779 
4780         // Position the Script Selector over the Add button so it is
4781         // beside the menu contents list and does not obscure it
4782         pSelectorDlg->SetPosPixel( aAddCommandsButton.GetPosPixel() );
4783 
4784         pSelectorDlg->SetAddHdl(
4785             LINK( this, SvxToolbarConfigPage, AddFunctionHdl ) );
4786     }
4787 
4788     pSelectorDlg->SetImageProvider(
4789         static_cast< ImageProvider* >( GetSaveInData() ) );
4790 
4791     pSelectorDlg->Show();
4792     return 1;
4793 }
4794 
4795 IMPL_LINK( SvxToolbarConfigPage, AddFunctionHdl,
4796     SvxScriptSelectorDialog *, pDialog )
4797 {
4798     (void)pDialog;
4799 
4800     AddFunction();
4801 
4802     return 0;
4803 }
4804 
4805 SvLBoxEntry* SvxToolbarConfigPage::AddFunction(
4806     SvLBoxEntry* pTarget, bool bFront, bool bAllowDuplicates )
4807 {
4808     SvLBoxEntry* pNewLBEntry =
4809         SvxConfigPage::AddFunction( pTarget, bFront, bAllowDuplicates );
4810 
4811     SvxConfigEntry* pEntry = (SvxConfigEntry*) pNewLBEntry->GetUserData();
4812 
4813     if ( pEntry->IsBinding() )
4814     {
4815         pEntry->SetVisible( sal_True );
4816         aContentsListBox->SetCheckButtonState(
4817             pNewLBEntry, SV_BUTTON_CHECKED );
4818     }
4819     else
4820     {
4821         aContentsListBox->SetCheckButtonState(
4822             pNewLBEntry, SV_BUTTON_TRISTATE );
4823     }
4824 
4825     // get currently selected toolbar and apply change
4826     SvxConfigEntry* pToolbar = GetTopLevelSelection();
4827 
4828     if ( pToolbar != NULL )
4829     {
4830         ( ( ToolbarSaveInData* ) GetSaveInData() )->ApplyToolbar( pToolbar );
4831     }
4832 
4833     return pNewLBEntry;
4834 }
4835 
4836 // -----------------------------------------------------------------------
4837 
4838 SvxToolbarEntriesListBox::SvxToolbarEntriesListBox(
4839     Window* pParent, const ResId& aResId )
4840     :
4841         SvxMenuEntriesListBox( pParent, aResId ),
4842         pPage( ( SvxToolbarConfigPage* ) pParent )
4843 {
4844     m_pButtonData = new SvLBoxButtonData( this );
4845     BuildCheckBoxButtonImages( m_pButtonData );
4846     EnableCheckButton( m_pButtonData );
4847 
4848     m_bHiContrastMode = GetSettings().GetStyleSettings().GetHighContrastMode();
4849 }
4850 
4851 // --------------------------------------------------------
4852 
4853 SvxToolbarEntriesListBox::~SvxToolbarEntriesListBox()
4854 {
4855     delete m_pButtonData;
4856 }
4857 
4858 // --------------------------------------------------------
4859 
4860 void SvxToolbarEntriesListBox::BuildCheckBoxButtonImages( SvLBoxButtonData* pData )
4861 {
4862     // Build checkbox images according to the current application
4863     // settings. This is necessary to be able to have correct colors
4864     // in all color modes, like high contrast.
4865     const AllSettings& rSettings = Application::GetSettings();
4866 
4867     VirtualDevice   aDev;
4868     Size            aSize( 26, 20 );
4869 
4870     aDev.SetOutputSizePixel( aSize );
4871 
4872     Image aImage = GetSizedImage( aDev, aSize,
4873         CheckBox::GetCheckImage( rSettings, BUTTON_DRAW_DEFAULT ));
4874 
4875     // Fill button data struct with new images
4876     pData->aBmps[SV_BMP_UNCHECKED]      = aImage;
4877     pData->aBmps[SV_BMP_CHECKED]        = GetSizedImage( aDev, aSize, CheckBox::GetCheckImage( rSettings, BUTTON_DRAW_CHECKED ));
4878     pData->aBmps[SV_BMP_HICHECKED]      = GetSizedImage( aDev, aSize, CheckBox::GetCheckImage( rSettings, BUTTON_DRAW_CHECKED | BUTTON_DRAW_PRESSED ));
4879     pData->aBmps[SV_BMP_HIUNCHECKED]    = GetSizedImage( aDev, aSize, CheckBox::GetCheckImage( rSettings, BUTTON_DRAW_DEFAULT | BUTTON_DRAW_PRESSED));
4880     pData->aBmps[SV_BMP_TRISTATE]       = GetSizedImage( aDev, aSize, Image() ); // Use tristate bitmaps to have no checkbox for separator entries
4881     pData->aBmps[SV_BMP_HITRISTATE]     = GetSizedImage( aDev, aSize, Image() );
4882 
4883     // Get image size
4884     m_aCheckBoxImageSizePixel = aImage.GetSizePixel();
4885 }
4886 
4887 Image SvxToolbarEntriesListBox::GetSizedImage(
4888     VirtualDevice& aDev, const Size& aNewSize, const Image& aImage )
4889 {
4890     // Create new checkbox images for treelistbox. They must have a
4891     // decent width to have a clear column for the visibility checkbox.
4892 
4893     // Standard transparent color is light magenta as is won't be
4894     // used for other things
4895     Color   aFillColor( COL_LIGHTMAGENTA );
4896 
4897     // Position image at the center of (width-2),(height) rectangle.
4898     // We need 2 pixels to have a bigger border to the next button image
4899     sal_uInt16  nPosX = std::max( (sal_uInt16) (((( aNewSize.Width() - 2 ) - aImage.GetSizePixel().Width() ) / 2 ) - 1), (sal_uInt16) 0 );
4900     sal_uInt16  nPosY = std::max( (sal_uInt16) (((( aNewSize.Height() - 2 ) - aImage.GetSizePixel().Height() ) / 2 ) + 1), (sal_uInt16) 0 );
4901     Point   aPos( nPosX > 0 ? nPosX : 0, nPosY > 0 ? nPosY : 0 );
4902     aDev.SetFillColor( aFillColor );
4903     aDev.SetLineColor( aFillColor );
4904     aDev.DrawRect( Rectangle( Point(), aNewSize ));
4905     aDev.DrawImage( aPos, aImage );
4906 
4907     // Draw separator line 2 pixels left from the right border
4908     Color aLineColor = GetDisplayBackground().GetColor().IsDark() ? Color( COL_WHITE ) : Color( COL_BLACK );
4909     aDev.SetLineColor( aLineColor );
4910     aDev.DrawLine( Point( aNewSize.Width()-3, 0 ), Point( aNewSize.Width()-3, aNewSize.Height()-1 ));
4911 
4912     // Create new image that uses the fillcolor as transparent
4913     return Image( aDev.GetBitmap( Point(), aNewSize ), aFillColor );
4914 }
4915 
4916 void SvxToolbarEntriesListBox::DataChanged( const DataChangedEvent& rDCEvt )
4917 {
4918     SvTreeListBox::DataChanged( rDCEvt );
4919 
4920     if (( rDCEvt.GetType() == DATACHANGED_SETTINGS ) &&
4921         ( rDCEvt.GetFlags() & SETTINGS_STYLE ))
4922     {
4923         // We have to reset all images because we change to/from high contrast mode
4924         m_bHiContrastMode = GetSettings().GetStyleSettings().GetHighContrastMode();
4925 
4926         BuildCheckBoxButtonImages( m_pButtonData );
4927         Invalidate();
4928     }
4929 }
4930 
4931 // --------------------------------------------------------
4932 
4933 void SvxToolbarEntriesListBox::ChangeVisibility( SvLBoxEntry* pEntry )
4934 {
4935     if ( pEntry != NULL )
4936     {
4937         SvxConfigEntry* pEntryData =
4938             (SvxConfigEntry*) pEntry->GetUserData();
4939 
4940         if ( pEntryData->IsBinding() )
4941         {
4942             pEntryData->SetVisible( !pEntryData->IsVisible() );
4943 
4944             SvxConfigEntry* pToolbar = pPage->GetTopLevelSelection();
4945 
4946             ToolbarSaveInData* pToolbarSaveInData = ( ToolbarSaveInData* )
4947                 pPage->GetSaveInData();
4948 
4949             pToolbarSaveInData->ApplyToolbar( pToolbar );
4950 
4951             SetCheckButtonState( pEntry, pEntryData->IsVisible() ?
4952                 SV_BUTTON_CHECKED : SV_BUTTON_UNCHECKED );
4953         }
4954     }
4955 }
4956 
4957 void SvxToolbarEntriesListBox::CheckButtonHdl()
4958 {
4959     ChangeVisibility( GetHdlEntry() );
4960 }
4961 
4962 void SvxToolbarEntriesListBox::KeyInput( const KeyEvent& rKeyEvent )
4963 {
4964     // space key will change visibility of toolbar items
4965     if ( rKeyEvent.GetKeyCode() == KEY_SPACE )
4966     {
4967         ChangeVisibility( GetCurEntry() );
4968     }
4969     else
4970     {
4971         // pass on to superclass
4972         SvxMenuEntriesListBox::KeyInput( rKeyEvent );
4973     }
4974 }
4975 
4976 sal_Bool SvxToolbarEntriesListBox::NotifyMoving(
4977     SvLBoxEntry* pTarget, SvLBoxEntry* pSource,
4978     SvLBoxEntry*& rpNewParent, sal_uLong& rNewChildPos)
4979 {
4980     bool result = SvxMenuEntriesListBox::NotifyMoving(
4981         pTarget, pSource, rpNewParent, rNewChildPos );
4982 
4983     if ( result == sal_True )
4984     {
4985         // Instant Apply changes to UI
4986         SvxConfigEntry* pToolbar = pPage->GetTopLevelSelection();
4987         if ( pToolbar != NULL )
4988         {
4989             ToolbarSaveInData* pSaveInData =
4990                 ( ToolbarSaveInData*) pPage->GetSaveInData();
4991             pSaveInData->ApplyToolbar( pToolbar );
4992         }
4993     }
4994 
4995     return result;
4996 }
4997 
4998 sal_Bool SvxToolbarEntriesListBox::NotifyCopying(
4999     SvLBoxEntry*  pTarget,
5000     SvLBoxEntry*  pSource,
5001     SvLBoxEntry*& rpNewParent,
5002     sal_uLong&      rNewChildPos)
5003 {
5004     (void)pSource;
5005     (void)rpNewParent;
5006     (void)rNewChildPos;
5007 
5008     if ( !m_bIsInternalDrag )
5009     {
5010         // if the target is NULL then add function to the start of the list
5011         ((SvxToolbarConfigPage*)pPage)->AddFunction( pTarget, pTarget == NULL );
5012 
5013         // Instant Apply changes to UI
5014         SvxConfigEntry* pToolbar = pPage->GetTopLevelSelection();
5015         if ( pToolbar != NULL )
5016         {
5017             ToolbarSaveInData* pSaveInData =
5018                 ( ToolbarSaveInData*) pPage->GetSaveInData();
5019             pSaveInData->ApplyToolbar( pToolbar );
5020         }
5021 
5022         // AddFunction already adds the listbox entry so return FALSE
5023         // to stop another listbox entry being added
5024         return sal_False;
5025     }
5026 
5027     // Copying is only allowed from external controls, not within the listbox
5028     return sal_False;
5029 }
5030 
5031 SvxNewToolbarDialog::SvxNewToolbarDialog(
5032     Window* pWindow, const String& rName )
5033     :
5034     ModalDialog     ( pWindow, CUI_RES( MD_NEW_TOOLBAR ) ),
5035     aFtDescription  ( this, CUI_RES( FT_NAME ) ),
5036     aEdtName        ( this, CUI_RES( EDT_STRING ) ),
5037     aSaveInText     ( this, CUI_RES( TXT_SAVEIN ) ),
5038     aBtnOK          ( this, CUI_RES( BTN_OK ) ),
5039     aBtnCancel      ( this, CUI_RES( BTN_CANCEL ) ),
5040     aBtnHelp        ( this, CUI_RES( BTN_HELP ) ),
5041     aSaveInListBox  ( this, CUI_RES( LB_SAVEIN ) )
5042 {
5043     FreeResource();
5044 
5045     aEdtName.SetText( rName );
5046     aEdtName.SetSelection(Selection(SELECTION_MIN, SELECTION_MAX));
5047     ModifyHdl(&aEdtName);
5048     aEdtName.SetModifyHdl(LINK(this, SvxNewToolbarDialog, ModifyHdl));
5049 }
5050 
5051 IMPL_LINK(SvxNewToolbarDialog, ModifyHdl, Edit*, pEdit)
5052 {
5053     (void)pEdit;
5054 
5055     if(aCheckNameHdl.IsSet())
5056         aBtnOK.Enable(aCheckNameHdl.Call(this) > 0);
5057 
5058     return 0;
5059 }
5060 
5061 /*******************************************************************************
5062 *
5063 * The SvxIconSelectorDialog class
5064 *
5065 *******************************************************************************/
5066 SvxIconSelectorDialog::SvxIconSelectorDialog( Window *pWindow,
5067     const uno::Reference< css::ui::XImageManager >& rXImageManager,
5068     const uno::Reference< css::ui::XImageManager >& rXParentImageManager )
5069     :
5070     ModalDialog          ( pWindow, CUI_RES( MD_ICONSELECTOR ) ),
5071     aFtDescription       ( this, CUI_RES( FT_SYMBOLS ) ),
5072     aTbSymbol            ( this, CUI_RES( TB_SYMBOLS ) ),
5073     aFtNote              ( this, CUI_RES( FT_NOTE ) ),
5074     aBtnOK               ( this, CUI_RES( BTN_OK ) ),
5075     aBtnCancel           ( this, CUI_RES( BTN_CANCEL ) ),
5076     aBtnHelp             ( this, CUI_RES( BTN_HELP ) ),
5077     aBtnImport           ( this, CUI_RES( BTN_IMPORT ) ),
5078     aBtnDelete           ( this, CUI_RES( BTN_DELETE ) ),
5079     aFlSeparator         ( this, CUI_RES( FL_SEPARATOR ) ),
5080     m_nNextId            ( 0 ),
5081     m_xImageManager      ( rXImageManager ),
5082     m_xParentImageManager( rXParentImageManager )
5083 {
5084     FreeResource();
5085 
5086     typedef ::std::hash_map< ::rtl::OUString,
5087                              bool,
5088                              ::rtl::OUStringHash,
5089                              ::std::equal_to< ::rtl::OUString > > ImageInfo;
5090 
5091     aTbSymbol.SetPageScroll( sal_True );
5092 
5093     bool bLargeIcons = GetImageType() & css::ui::ImageType::SIZE_LARGE;
5094     m_nExpectedSize = bLargeIcons ? 26 : 16;
5095 
5096     if ( m_nExpectedSize != 16 )
5097     {
5098         aFtNote.SetText( replaceSixteen( aFtNote.GetText(), m_nExpectedSize ) );
5099     }
5100 
5101     uno::Reference< lang::XMultiServiceFactory > xServiceManager =
5102         ::comphelper::getProcessServiceFactory();
5103 
5104     if ( xServiceManager.is() )
5105     {
5106         m_xGraphProvider = uno::Reference< graphic::XGraphicProvider >(
5107             xServiceManager->createInstance(
5108                 ::rtl::OUString::createFromAscii(
5109                     "com.sun.star.graphic.GraphicProvider" ) ),
5110             uno::UNO_QUERY );
5111     }
5112 
5113     if ( !m_xGraphProvider.is() )
5114     {
5115         aBtnImport.Enable( sal_False );
5116     }
5117 
5118     uno::Reference< beans::XPropertySet > xPropSet(
5119         xServiceManager->createInstance( ::rtl::OUString::createFromAscii( "com.sun.star.util.PathSettings" ) ),
5120         uno::UNO_QUERY );
5121 
5122     uno::Any aAny = xPropSet->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "UserConfig" ) ) );
5123 
5124     ::rtl::OUString aDirectory;
5125 
5126     aAny >>= aDirectory;
5127 
5128     sal_Int32 aCount = aDirectory.getLength();
5129 
5130     if ( aCount > 0 )
5131     {
5132         sal_Unicode aChar = aDirectory[ aCount-1 ];
5133         if ( aChar != '/')
5134         {
5135             aDirectory += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/" ) );
5136         }
5137     }
5138     else
5139     {
5140         aBtnImport.Enable( sal_False );
5141     }
5142 
5143     aDirectory += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "soffice.cfg/import" ) );
5144 
5145     uno::Reference< lang::XSingleServiceFactory > xStorageFactory(
5146         xServiceManager->createInstance(
5147         ::rtl::OUString::createFromAscii( "com.sun.star.embed.FileSystemStorageFactory" )),
5148         uno::UNO_QUERY );
5149 
5150     uno::Sequence< uno::Any > aArgs( 2 );
5151     aArgs[ 0 ] <<= aDirectory;
5152     aArgs[ 1 ] <<= com::sun::star::embed::ElementModes::READWRITE;
5153 
5154     uno::Reference< com::sun::star::embed::XStorage > xStorage(
5155         xStorageFactory->createInstanceWithArguments( aArgs ), uno::UNO_QUERY );
5156 
5157     uno::Sequence< uno::Any > aProp( 2 );
5158     beans::PropertyValue aPropValue;
5159 
5160     aPropValue.Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "UserConfigStorage" ) );
5161     aPropValue.Value <<= xStorage;
5162     aProp[ 0 ] <<= aPropValue;
5163 
5164     aPropValue.Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "OpenMode" ) );
5165     aPropValue.Value <<= com::sun::star::embed::ElementModes::READWRITE;
5166     aProp[ 1 ] <<= aPropValue;
5167 
5168     m_xImportedImageManager = uno::Reference< com::sun::star::ui::XImageManager >(
5169         xServiceManager->createInstanceWithArguments(
5170         ::rtl::OUString::createFromAscii( "com.sun.star.ui.ImageManager" ), aProp ),
5171         uno::UNO_QUERY );
5172 
5173     ImageInfo mImageInfo;
5174     uno::Sequence< OUString > names;
5175     if ( m_xImportedImageManager.is() )
5176     {
5177         names = m_xImportedImageManager->getAllImageNames( GetImageType() );
5178         for ( sal_Int32 n = 0; n < names.getLength(); n++ )
5179             mImageInfo.insert( ImageInfo::value_type( names[n], false ));
5180     }
5181     sal_uInt16 nId = 1;
5182     ImageInfo::const_iterator pConstIter = mImageInfo.begin();
5183     uno::Sequence< OUString > name( 1 );
5184     while ( pConstIter != mImageInfo.end() )
5185     {
5186         name[ 0 ] = pConstIter->first;
5187         uno::Sequence< uno::Reference< graphic::XGraphic> > graphics = m_xImportedImageManager->getImages( GetImageType(), name );
5188         if ( graphics.getLength() > 0 )
5189         {
5190             Image img = Image( graphics[ 0 ] );
5191             aTbSymbol.InsertItem( nId, img, pConstIter->first );
5192 
5193             graphics[ 0 ]->acquire();
5194 
5195             aTbSymbol.SetItemData(
5196                 nId, static_cast< void * > ( graphics[ 0 ].get() ) );
5197 
5198             ++nId;
5199         }
5200         ++pConstIter;
5201     }
5202 
5203     ImageInfo                 aImageInfo;
5204 
5205     if ( m_xParentImageManager.is() )
5206     {
5207         names = m_xParentImageManager->getAllImageNames( GetImageType() );
5208         for ( sal_Int32 n = 0; n < names.getLength(); n++ )
5209             aImageInfo.insert( ImageInfo::value_type( names[n], false ));
5210     }
5211 
5212     names = m_xImageManager->getAllImageNames( GetImageType() );
5213     for ( sal_Int32 n = 0; n < names.getLength(); n++ )
5214     {
5215         ImageInfo::iterator pIter = aImageInfo.find( names[n] );
5216         if ( pIter != aImageInfo.end() )
5217             pIter->second = true;
5218         else
5219             aImageInfo.insert( ImageInfo::value_type( names[n], true ));
5220     }
5221 
5222     // large growth factor, expecting many entries
5223     pConstIter = aImageInfo.begin();
5224     while ( pConstIter != aImageInfo.end() )
5225     {
5226         name[ 0 ] = pConstIter->first;
5227 
5228         uno::Sequence< uno::Reference< graphic::XGraphic> > graphics;
5229         try
5230         {
5231             if ( pConstIter->second )
5232                 graphics = m_xImageManager->getImages( GetImageType(), name );
5233             else
5234                 graphics = m_xParentImageManager->getImages( GetImageType(), name );
5235         }
5236         catch ( uno::Exception& )
5237         {
5238             // can't get sequence for this name so it will not be
5239             // added to the list
5240         }
5241 
5242         if ( graphics.getLength() > 0 )
5243         {
5244             Image img = Image( graphics[ 0 ] );
5245             aTbSymbol.InsertItem( nId, img, pConstIter->first );
5246 
5247             uno::Reference< graphic::XGraphic > xGraphic = graphics[ 0 ];
5248 
5249             if ( xGraphic.is() )
5250                 xGraphic->acquire();
5251 
5252             aTbSymbol.SetItemData(
5253                 nId, static_cast< void * > ( xGraphic.get() ) );
5254 
5255             ++nId;
5256         }
5257 
5258         ++pConstIter;
5259     }
5260 
5261     aBtnDelete.Enable( sal_False );
5262     aTbSymbol.SetSelectHdl( LINK(this, SvxIconSelectorDialog, SelectHdl) );
5263     aBtnImport.SetClickHdl( LINK(this, SvxIconSelectorDialog, ImportHdl) );
5264     aBtnDelete.SetClickHdl( LINK(this, SvxIconSelectorDialog, DeleteHdl) );
5265 
5266     m_nNextId = aTbSymbol.GetItemCount()+1;
5267 }
5268 
5269 SvxIconSelectorDialog::~SvxIconSelectorDialog()
5270 {
5271     sal_uInt16 nCount = aTbSymbol.GetItemCount();
5272 
5273     for (sal_uInt16 n = 0; n < nCount; n++ )
5274     {
5275         sal_uInt16 nId = aTbSymbol.GetItemId(n);
5276 
5277         uno::XInterface* xi = static_cast< uno::XInterface* >(
5278             aTbSymbol.GetItemData( nId ) );
5279 
5280         if ( xi != NULL )
5281         {
5282             xi->release();
5283         }
5284     }
5285 }
5286 
5287 uno::Reference< graphic::XGraphic> SvxIconSelectorDialog::GetSelectedIcon()
5288 {
5289     uno::Reference< graphic::XGraphic > result;
5290 
5291     sal_uInt16 nId;
5292     for ( sal_uInt16 n = 0; n < aTbSymbol.GetItemCount(); n++ )
5293     {
5294         nId = aTbSymbol.GetItemId( n );
5295         if ( aTbSymbol.IsItemChecked( nId ) )
5296         {
5297             result = uno::Reference< graphic::XGraphic >(
5298                 reinterpret_cast< graphic::XGraphic* >(
5299                     aTbSymbol.GetItemData( nId ) ) );
5300         }
5301     }
5302 
5303     return result;
5304 }
5305 
5306 IMPL_LINK( SvxIconSelectorDialog, SelectHdl, ToolBox *, pToolBox )
5307 {
5308     (void)pToolBox;
5309 
5310     sal_uInt16 nCount = aTbSymbol.GetItemCount();
5311 
5312     for (sal_uInt16 n = 0; n < nCount; n++ )
5313     {
5314         sal_uInt16 nId = aTbSymbol.GetItemId( n );
5315 
5316         if ( aTbSymbol.IsItemChecked( nId ) )
5317         {
5318             aTbSymbol.CheckItem( nId, sal_False );
5319         }
5320     }
5321 
5322     sal_uInt16 nId = aTbSymbol.GetCurItemId();
5323     aTbSymbol.CheckItem( nId );
5324 
5325     ::rtl::OUString aSelImageText = aTbSymbol.GetItemText( nId );
5326     if ( m_xImportedImageManager->hasImage( GetImageType(), aSelImageText ) )
5327     {
5328         aBtnDelete.Enable( sal_True );
5329     }
5330     else
5331     {
5332         aBtnDelete.Enable( sal_False );
5333     }
5334 
5335     return 0;
5336 }
5337 
5338 IMPL_LINK( SvxIconSelectorDialog, ImportHdl, PushButton *, pButton )
5339 {
5340     (void)pButton;
5341 
5342     sfx2::FileDialogHelper aImportDialog(
5343         css::ui::dialogs::TemplateDescription::FILEOPEN_LINK_PREVIEW,
5344         SFXWB_GRAPHIC | SFXWB_MULTISELECTION );
5345 
5346     // disable the link checkbox in the dialog
5347     uno::Reference< css::ui::dialogs::XFilePickerControlAccess >
5348         xController( aImportDialog.GetFilePicker(), uno::UNO_QUERY);
5349     if ( xController.is() )
5350     {
5351         xController->enableControl(
5352             css::ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_LINK,
5353             sal_False);
5354     }
5355 
5356     aImportDialog.SetCurrentFilter(
5357         String::CreateFromAscii( "PNG - Portable Network Graphic" ) );
5358 
5359     if ( ERRCODE_NONE == aImportDialog.Execute() )
5360     {
5361         uno::Sequence< OUString > paths = aImportDialog.GetMPath();
5362         ImportGraphics ( paths );
5363     }
5364 
5365     return 0;
5366 }
5367 
5368 IMPL_LINK( SvxIconSelectorDialog, DeleteHdl, PushButton *, pButton )
5369 {
5370     (void)pButton;
5371 
5372     OUString message = String( CUI_RES( RID_SVXSTR_DELETE_ICON_CONFIRM ) );
5373     bool ret = WarningBox( this, WinBits(WB_OK_CANCEL), message ).Execute();
5374 
5375     if ( ret == RET_OK )
5376     {
5377         sal_uInt16 nCount = aTbSymbol.GetItemCount();
5378 
5379         for (sal_uInt16 n = 0; n < nCount; n++ )
5380         {
5381             sal_uInt16 nId = aTbSymbol.GetItemId( n );
5382 
5383             if ( aTbSymbol.IsItemChecked( nId ) )
5384             {
5385                 ::rtl::OUString aSelImageText = aTbSymbol.GetItemText( nId );
5386                 uno::Sequence< OUString > URLs(1);
5387                 URLs[0] = aSelImageText;
5388                 aTbSymbol.RemoveItem( aTbSymbol.GetItemPos( nId ) );
5389                 m_xImportedImageManager->removeImages( GetImageType(), URLs );
5390                 uno::Reference< css::ui::XUIConfigurationPersistence >
5391                     xConfigPersistence( m_xImportedImageManager, uno::UNO_QUERY );
5392                 if ( xConfigPersistence.is() && xConfigPersistence->isModified() )
5393                 {
5394                     xConfigPersistence->store();
5395                 }
5396                 break;
5397             }
5398         }
5399     }
5400     return 0;
5401 }
5402 
5403 bool SvxIconSelectorDialog::ReplaceGraphicItem(
5404     const ::rtl::OUString& aURL )
5405 {
5406     uno::Sequence< OUString > URLs(1);
5407     uno::Sequence< uno::Reference<graphic::XGraphic > > aImportGraph( 1 );
5408     uno::Reference< css::ui::XUIConfigurationPersistence >
5409         xConfigPer( m_xImportedImageManager, uno::UNO_QUERY );
5410 
5411     uno::Reference< graphic::XGraphic > xGraphic;
5412     uno::Sequence< beans::PropertyValue > aMediaProps( 1 );
5413     aMediaProps[0].Name = ::rtl::OUString::createFromAscii("URL");
5414     aMediaProps[0].Value <<= aURL;
5415 
5416     com::sun::star::awt::Size aSize;
5417     bool bOK = sal_False;
5418     try
5419     {
5420         xGraphic = m_xGraphProvider->queryGraphic( aMediaProps );
5421 
5422         uno::Reference< beans::XPropertySet > props =
5423             m_xGraphProvider->queryGraphicDescriptor( aMediaProps );
5424         uno::Any a = props->getPropertyValue(
5425             OUString::createFromAscii("SizePixel") );
5426         a >>= aSize;
5427         if (0 == aSize.Width || 0 == aSize.Height)
5428             return sal_False;
5429         else
5430             bOK = sal_True;
5431     }
5432     catch ( uno::Exception& )
5433     {
5434         return false;
5435     }
5436 
5437     bool   bResult( false );
5438     sal_uInt16 nCount = aTbSymbol.GetItemCount();
5439     for (sal_uInt16 n = 0; n < nCount; n++ )
5440     {
5441         sal_uInt16 nId = aTbSymbol.GetItemId( n );
5442 
5443         if ( OUString( aTbSymbol.GetItemText( nId ) ) == aURL )
5444         {
5445             try
5446             {
5447                 // replace/insert image with provided URL
5448                 aTbSymbol.RemoveItem( aTbSymbol.GetItemPos( nId ) );
5449                 aMediaProps[0].Value <<= aURL;
5450 
5451                 Image aImage( xGraphic );
5452                 if ( bOK && ((aSize.Width != m_nExpectedSize) || (aSize.Height != m_nExpectedSize)) )
5453                 {
5454                     BitmapEx aBitmap = aImage.GetBitmapEx();
5455                     BitmapEx aBitmapex = AutoScaleBitmap(aBitmap, m_nExpectedSize);
5456                     aImage = Image( aBitmapex);
5457                 }
5458                 aTbSymbol.InsertItem( nId,aImage, aURL, 0, 0 ); //modify
5459 
5460                 xGraphic = aImage.GetXGraphic();
5461 
5462                 URLs[0] = aURL;
5463                 aImportGraph[ 0 ] = xGraphic;
5464                 m_xImportedImageManager->replaceImages( GetImageType(), URLs, aImportGraph );
5465                 xConfigPer->store();
5466 
5467                 bResult = true;
5468                 break;
5469             }
5470             catch ( ::com::sun::star::uno::Exception& )
5471             {
5472                 break;
5473             }
5474         }
5475     }
5476 
5477     return bResult;
5478 }
5479 
5480 void SvxIconSelectorDialog::ImportGraphics(
5481     const uno::Sequence< OUString >& rPaths )
5482 {
5483     uno::Sequence< OUString > rejected( rPaths.getLength() );
5484     sal_Int32 rejectedCount = 0;
5485 
5486     sal_uInt16 ret = 0;
5487     sal_Int32 aIndex;
5488     OUString aIconName;
5489     uno::Sequence< OUString > URLs(1);
5490     uno::Sequence< uno::Reference<graphic::XGraphic > > aImportGraph( 1 );
5491     uno::Sequence< beans::PropertyValue > aMediaProps( 1 );
5492     aMediaProps[0].Name = ::rtl::OUString::createFromAscii("URL");
5493     uno::Reference< css::ui::XUIConfigurationPersistence >
5494         xConfigPer( m_xImportedImageManager, uno::UNO_QUERY );
5495 
5496     if ( rPaths.getLength() == 1 )
5497     {
5498         if ( m_xImportedImageManager->hasImage( GetImageType(), rPaths[0] ) )
5499         {
5500             aIndex = rPaths[0].lastIndexOf( '/' );
5501             aIconName = rPaths[0].copy( aIndex+1 );
5502             ret = SvxIconReplacementDialog( this, aIconName ).ShowDialog();
5503             if ( ret == 2 )
5504             {
5505                 ReplaceGraphicItem( rPaths[0] );
5506             }
5507         }
5508         else
5509         {
5510             if ( ImportGraphic( rPaths[0] ) == sal_False )
5511             {
5512                 rejected[0] = rPaths[0];
5513                 rejectedCount = 1;
5514             }
5515         }
5516     }
5517     else
5518     {
5519         ::rtl::OUString aSourcePath( rPaths[0] );
5520         if ( rPaths[0].lastIndexOf( '/' ) != rPaths[0].getLength() -1 )
5521             aSourcePath = rPaths[0] + ::rtl::OUString::createFromAscii( "/" );
5522 
5523         for ( sal_Int32 i = 1; i < rPaths.getLength(); i++ )
5524         {
5525             ::rtl::OUString aPath = aSourcePath + rPaths[i];
5526             if ( m_xImportedImageManager->hasImage( GetImageType(), aPath ) )
5527             {
5528                 aIndex = rPaths[i].lastIndexOf( '/' );
5529                 aIconName = rPaths[i].copy( aIndex+1 );
5530                 ret = SvxIconReplacementDialog( this, aIconName, sal_True ).ShowDialog();
5531                 if ( ret == 2 )
5532                 {
5533                     ReplaceGraphicItem( aPath );
5534                 }
5535                 else if ( ret == 5 )
5536                 {
5537                     for ( sal_Int32 k = i; k < rPaths.getLength(); k++ )
5538                     {
5539                         aPath = aSourcePath + rPaths[k];
5540                         bool bHasReplaced = ReplaceGraphicItem( aPath );
5541 
5542                         if ( !bHasReplaced )
5543                         {
5544                             bool result = ImportGraphic( aPath );
5545                             if ( result == sal_False )
5546                             {
5547                                 rejected[ rejectedCount ] = rPaths[i];
5548                                 rejectedCount++;
5549                             }
5550                         }
5551                     }
5552                     break;
5553                 }
5554             }
5555             else
5556             {
5557                 bool result = ImportGraphic( aSourcePath + rPaths[i] );
5558                 if ( result == sal_False )
5559                 {
5560                     rejected[ rejectedCount ] = rPaths[i];
5561                     rejectedCount++;
5562                 }
5563             }
5564         }
5565     }
5566 
5567     if ( rejectedCount != 0 )
5568     {
5569         OUString message =OUString::createFromAscii("");
5570         OUString newLine = OUString::createFromAscii("\n");
5571         rtl::OUString fPath = OUString::createFromAscii("");
5572         if (rejectedCount > 1)
5573             fPath = rPaths[0].copy(8) + ::rtl::OUString::createFromAscii( "/" );
5574         for ( sal_Int32 i = 0; i < rejectedCount; i++ )
5575         {
5576             message += fPath + rejected[i];
5577             message += newLine;
5578         }
5579 
5580         SvxIconChangeDialog aDialog(this, message);
5581         aDialog.Execute();
5582     }
5583 }
5584 
5585 bool SvxIconSelectorDialog::ImportGraphic( const OUString& aURL )
5586 {
5587     bool result = sal_False;
5588 
5589     sal_uInt16 nId = m_nNextId;
5590     ++m_nNextId;
5591 
5592     uno::Sequence< beans::PropertyValue > aMediaProps( 1 );
5593     aMediaProps[0].Name = ::rtl::OUString::createFromAscii("URL");
5594 
5595     uno::Reference< graphic::XGraphic > xGraphic;
5596     com::sun::star::awt::Size aSize;
5597     bool bOK = sal_True;
5598     aMediaProps[0].Value <<= aURL;
5599     try
5600     {
5601         uno::Reference< beans::XPropertySet > props =
5602             m_xGraphProvider->queryGraphicDescriptor( aMediaProps );
5603 
5604         uno::Any a = props->getPropertyValue(
5605             OUString::createFromAscii("SizePixel") );
5606 
5607             xGraphic = m_xGraphProvider->queryGraphic( aMediaProps );
5608             if ( xGraphic.is() )
5609             {
5610                 a >>= aSize;
5611                 if ( 0 == aSize.Width || 0 == aSize.Height )
5612                     bOK = sal_False;
5613 
5614                 Image aImage( xGraphic );
5615 
5616                 if ( bOK && ((aSize.Width != m_nExpectedSize) || (aSize.Height != m_nExpectedSize)) )
5617                 {
5618                     BitmapEx aBitmap = aImage.GetBitmapEx();
5619                     BitmapEx aBitmapex = AutoScaleBitmap(aBitmap, m_nExpectedSize);
5620                     aImage = Image( aBitmapex);
5621                 }
5622                 if ( bOK && !!aImage )
5623                 {
5624                     aTbSymbol.InsertItem( nId, aImage, aURL, 0, 0 );
5625 
5626                     xGraphic = aImage.GetXGraphic();
5627                     xGraphic->acquire();
5628 
5629                     aTbSymbol.SetItemData(
5630                         nId, static_cast< void * > ( xGraphic.get() ) );
5631                     uno::Sequence< OUString > aImportURL( 1 );
5632                     aImportURL[ 0 ] = aURL;
5633                     uno::Sequence< uno::Reference<graphic::XGraphic > > aImportGraph( 1 );
5634                     aImportGraph[ 0 ] = xGraphic;
5635                     m_xImportedImageManager->insertImages( GetImageType(), aImportURL, aImportGraph );
5636                     uno::Reference< css::ui::XUIConfigurationPersistence >
5637                     xConfigPersistence( m_xImportedImageManager, uno::UNO_QUERY );
5638 
5639                     if ( xConfigPersistence.is() && xConfigPersistence->isModified() )
5640                     {
5641                         xConfigPersistence->store();
5642                     }
5643 
5644                     result = sal_True;
5645                 }
5646                 else
5647                 {
5648                     OSL_TRACE("could not create Image from XGraphic");
5649                 }
5650             }
5651             else
5652             {
5653                 OSL_TRACE("could not get query XGraphic");
5654             }
5655     }
5656     catch( uno::Exception& e )
5657     {
5658         OSL_TRACE("Caught exception importing XGraphic: %s", PRTSTR(e.Message));
5659     }
5660     return result;
5661 }
5662 
5663 /*******************************************************************************
5664 *
5665 * The SvxIconReplacementDialog class
5666 *
5667 *******************************************************************************/
5668 SvxIconReplacementDialog :: SvxIconReplacementDialog(
5669     Window *pWindow, const rtl::OUString& aMessage, bool /*bYestoAll*/ )
5670     :
5671 MessBox( pWindow, WB_DEF_YES, String( CUI_RES( RID_SVXSTR_REPLACE_ICON_CONFIRM ) ),  String( CUI_RES( RID_SVXSTR_REPLACE_ICON_WARNING ) ) )
5672 
5673 {
5674     SetImage( WarningBox::GetStandardImage() );
5675     SetMessText( ReplaceIconName( aMessage ) );
5676     RemoveButton( 1 );
5677     AddButton( BUTTON_YES, 2, 0 );
5678     AddButton( String( CUI_RES( RID_SVXSTR_YESTOALL ) ), 5, 0 );
5679     AddButton( BUTTON_NO, 3, 0 );
5680     AddButton( BUTTON_CANCEL, 4, 0 );
5681 }
5682 
5683 SvxIconReplacementDialog :: SvxIconReplacementDialog(
5684     Window *pWindow, const rtl::OUString& aMessage )
5685     :
5686 MessBox( pWindow, WB_YES_NO_CANCEL, String( CUI_RES( RID_SVXSTR_REPLACE_ICON_CONFIRM ) ),  String( CUI_RES( RID_SVXSTR_REPLACE_ICON_WARNING ) ) )
5687 {
5688     SetImage( WarningBox::GetStandardImage() );
5689     SetMessText( ReplaceIconName( aMessage ));
5690 }
5691 
5692 rtl::OUString SvxIconReplacementDialog :: ReplaceIconName( const OUString& rMessage )
5693 {
5694     rtl::OUString name;
5695     rtl::OUString message = String( CUI_RES( RID_SVXSTR_REPLACE_ICON_WARNING ) );
5696     rtl::OUString placeholder = OUString::createFromAscii( "%ICONNAME" );
5697     sal_Int32 pos = message.indexOf( placeholder );
5698     if ( pos != -1 )
5699     {
5700         name = message.replaceAt(
5701             pos, placeholder.getLength(), rMessage );
5702     }
5703     return name;
5704 }
5705 
5706 sal_uInt16 SvxIconReplacementDialog :: ShowDialog()
5707 {
5708     this->Execute();
5709     return ( this->GetCurButtonId() );
5710 }
5711 /*******************************************************************************
5712 *
5713 * The SvxIconChangeDialog class added for issue83555
5714 *
5715 *******************************************************************************/
5716 SvxIconChangeDialog::SvxIconChangeDialog(
5717     Window *pWindow, const rtl::OUString& aMessage)
5718     :
5719     ModalDialog            ( pWindow, CUI_RES( MD_ICONCHANGE ) ),
5720     aFImageInfo            (this, CUI_RES( FI_INFO ) ),
5721     aBtnOK                 (this, CUI_RES(MD_BTN_OK)),
5722     aDescriptionLabel      (this, CUI_RES(FTCHGE_DESCRIPTION)),
5723     aLineEditDescription   (this, CUI_RES(EDT_ADDR))
5724 {
5725     FreeResource();
5726     aFImageInfo.SetImage(InfoBox::GetStandardImage());
5727     aLineEditDescription.SetControlBackground( GetSettings().GetStyleSettings().GetDialogColor() );
5728     aLineEditDescription.SetAutoScroll( sal_True );
5729     aLineEditDescription.EnableCursor( sal_False );
5730     aLineEditDescription.SetText(aMessage);
5731 }
5732 
5733 BitmapEx SvxIconSelectorDialog::AutoScaleBitmap(BitmapEx & aBitmap, const long aStandardSize)
5734 {
5735     Point aEmptyPoint(0,0);
5736     sal_Int32 imgNewWidth = 0;
5737     sal_Int32 imgNewHeight = 0;
5738     double imgposX = 0;
5739     double imgposY = 0;
5740     BitmapEx  aRet = aBitmap;
5741     double imgOldWidth = aRet.GetSizePixel().Width();
5742     double imgOldHeight =aRet.GetSizePixel().Height();
5743 
5744     Size aScaledSize;
5745     if (imgOldWidth >= aStandardSize || imgOldHeight >= aStandardSize)
5746     {
5747         if (imgOldWidth >= imgOldHeight)
5748         {
5749             imgNewWidth = aStandardSize;
5750             imgNewHeight = sal_Int32(imgOldHeight / (imgOldWidth / aStandardSize) + 0.5);
5751             imgposX = 0;
5752             imgposY = (aStandardSize - (imgOldHeight / (imgOldWidth / aStandardSize) + 0.5)) / 2 + 0.5;
5753         }
5754         else
5755         {
5756             imgNewHeight = aStandardSize;
5757             imgNewWidth = sal_Int32(imgOldWidth / (imgOldHeight / aStandardSize) + 0.5);
5758             imgposY = 0;
5759             imgposX = (aStandardSize - (imgOldWidth / (imgOldHeight / aStandardSize) + 0.5)) / 2 + 0.5;
5760         }
5761 
5762         aScaledSize = Size( imgNewWidth, imgNewHeight );
5763         aRet.Scale( aScaledSize, BMP_SCALE_INTERPOLATE );
5764     }
5765     else
5766     {
5767         imgposX = (aStandardSize - imgOldWidth) / 2 + 0.5;
5768         imgposY = (aStandardSize - imgOldHeight) / 2 + 0.5;
5769     }
5770 
5771     Size aBmpSize = aRet.GetSizePixel();
5772     Size aStdSize( aStandardSize, aStandardSize );
5773     Rectangle aRect(aEmptyPoint, aStdSize );
5774 
5775     VirtualDevice aVirDevice( *Application::GetDefaultDevice(), 0, 1 );
5776     aVirDevice.SetOutputSizePixel( aStdSize );
5777     aVirDevice.SetFillColor( COL_TRANSPARENT );
5778     aVirDevice.SetLineColor( COL_TRANSPARENT );
5779 
5780     //draw a rect into virDevice
5781     aVirDevice.DrawRect( aRect );
5782     Point aPointPixel( (long)imgposX, (long)imgposY );
5783     aVirDevice.DrawBitmapEx( aPointPixel, aRet );
5784     aRet = aVirDevice.GetBitmapEx( aEmptyPoint, aStdSize );
5785 
5786     return aRet;
5787 }
5788