xref: /AOO41X/main/sysui/source/win32/misc/resourceprovider.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 //------------------------------------------------------------------------
29 // includes
30 //------------------------------------------------------------------------
31 
32 #include <osl/diagnose.h>
33 #include <rtl/ustrbuf.hxx>
34 #include "resourceprovider.hxx"
35 #include <tools/resmgr.hxx>
36 #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
37 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
38 
39 #include <svtools/svtools.hrc>
40 
41 //------------------------------------------------------------
42 // namespace directives
43 //------------------------------------------------------------
44 
45 using rtl::OUString;
46 using namespace ::com::sun::star::ui::dialogs::ExtendedFilePickerElementIds;
47 using namespace ::com::sun::star::ui::dialogs::CommonFilePickerElementIds;
48 
49 //------------------------------------------------------------
50 //
51 //------------------------------------------------------------
52 
53 #define RES_NAME svt
54 
55 // because the label of a listbox is
56 // a control itself (static text) we
57 // have defined a control id for this
58 // label which is the listbox control
59 // id + 100
60 #define LB_LABEL_OFFSET 100
61 
62 const rtl::OUString TILDE = OUString::createFromAscii( "~" );
63 const sal_Unicode TILDE_SIGN = L'~';
64 
65 #define FOLDERPICKER_TITLE            500
66 #define FOLDER_PICKER_DEF_DESCRIPTION 501
67 
68 //------------------------------------------------------------
69 // we have to translate control ids to resource ids
70 //------------------------------------------------------------
71 
72 struct _Entry
73 {
74     sal_Int32 ctrlId;
75     sal_Int16 resId;
76 };
77 
78 _Entry CtrlIdToResIdTable[] = {
79     { CHECKBOX_AUTOEXTENSION,                   STR_SVT_FILEPICKER_AUTO_EXTENSION },
80     { CHECKBOX_PASSWORD,                        STR_SVT_FILEPICKER_PASSWORD },
81     { CHECKBOX_FILTEROPTIONS,                   STR_SVT_FILEPICKER_FILTER_OPTIONS },
82     { CHECKBOX_READONLY,                        STR_SVT_FILEPICKER_READONLY },
83     { CHECKBOX_LINK,                            STR_SVT_FILEPICKER_INSERT_AS_LINK },
84     { CHECKBOX_PREVIEW,                         STR_SVT_FILEPICKER_SHOW_PREVIEW },
85     { PUSHBUTTON_PLAY,                          STR_SVT_FILEPICKER_PLAY },
86     { LISTBOX_VERSION + LB_LABEL_OFFSET,        STR_SVT_FILEPICKER_VERSION },
87     { LISTBOX_TEMPLATE + LB_LABEL_OFFSET,       STR_SVT_FILEPICKER_TEMPLATES },
88     { LISTBOX_IMAGE_TEMPLATE + LB_LABEL_OFFSET, STR_SVT_FILEPICKER_IMAGE_TEMPLATE },
89     { CHECKBOX_SELECTION,                       STR_SVT_FILEPICKER_SELECTION },
90     { FOLDERPICKER_TITLE,                       STR_SVT_FOLDERPICKER_DEFAULT_TITLE },
91     { FOLDER_PICKER_DEF_DESCRIPTION,            STR_SVT_FOLDERPICKER_DEFAULT_DESCRIPTION }
92 };
93 
94 const sal_Int32 SIZE_TABLE = sizeof( CtrlIdToResIdTable ) / sizeof( _Entry );
95 
96 //------------------------------------------------------------
97 //
98 //------------------------------------------------------------
99 
100 sal_Int16 CtrlIdToResId( sal_Int32 aControlId )
101 {
102     sal_Int16 aResId = -1;
103 
104     for ( sal_Int32 i = 0; i < SIZE_TABLE; i++ )
105     {
106         if ( CtrlIdToResIdTable[i].ctrlId == aControlId )
107         {
108             aResId = CtrlIdToResIdTable[i].resId;
109             break;
110         }
111     }
112 
113     return aResId;
114 }
115 
116 //------------------------------------------------------------
117 //
118 //------------------------------------------------------------
119 
120 class CResourceProvider_Impl
121 {
122 public:
123 
124     //-------------------------------------
125     //
126     //-------------------------------------
127 
128     CResourceProvider_Impl( )
129     {
130         m_ResMgr = CREATEVERSIONRESMGR( RES_NAME );
131     }
132 
133     //-------------------------------------
134     //
135     //-------------------------------------
136 
137     ~CResourceProvider_Impl( )
138     {
139         delete m_ResMgr;
140     }
141 
142     //-------------------------------------
143     //
144     //-------------------------------------
145 
146     OUString getResString( sal_Int16 aId )
147     {
148         String   aResString;
149         OUString aResOUString;
150 
151         try
152         {
153             OSL_ASSERT( m_ResMgr );
154 
155             // translate the control id to a resource id
156             sal_Int16 aResId = CtrlIdToResId( aId );
157 
158             if ( aResId > -1 )
159             {
160                 aResString = String( ResId( aResId, m_ResMgr ) );
161                 aResOUString = OUString( aResString );
162 
163                 // remove '~' signs, if there are two '~' signs
164                 // in a row we remove only one of them
165                 if ( aResOUString.indexOf( TILDE ) > -1 )
166                 {
167                     sal_Int32 nStrLen = aResOUString.getLength( );
168                     rtl::OUStringBuffer aBuffer( nStrLen );
169                     sal_Int32 i = 0;
170                     const sal_Unicode* pPos  = aResOUString.getStr( );
171                     const sal_Unicode* pNext = aResOUString.getStr( ) + 1;
172                     const sal_Unicode* pEnd  = aResOUString.getStr( ) + nStrLen;
173 
174                     while( pPos < pEnd )
175                     {
176                         // we insert the next character only if the current character
177                         // in not a '~' or the following character is also a '~'
178                         if ( (*pPos != TILDE_SIGN) ||
179                              ((*pPos == TILDE_SIGN) && (pNext < pEnd) && (*pNext == TILDE_SIGN)) )
180                         {
181                             aBuffer.insert( i, *pPos );
182                             i++;
183                         }
184 
185                         pPos++;
186                         pNext++;
187                     }
188 
189                     aResOUString = aBuffer.makeStringAndClear( );
190                 }
191             }
192         }
193         catch(...)
194         {
195         }
196 
197         return aResOUString;
198     }
199 
200 public:
201     ResMgr* m_ResMgr;
202 };
203 
204 //------------------------------------------------------------
205 //
206 //------------------------------------------------------------
207 
208 CResourceProvider::CResourceProvider( ) :
209     m_pImpl( new CResourceProvider_Impl() )
210 {
211 }
212 
213 //------------------------------------------------------------
214 //
215 //------------------------------------------------------------
216 
217 CResourceProvider::~CResourceProvider( )
218 {
219     delete m_pImpl;
220 }
221 
222 //------------------------------------------------------------
223 //
224 //------------------------------------------------------------
225 
226 OUString CResourceProvider::getResString( sal_Int32 aId )
227 {
228    return m_pImpl->getResString( aId );
229 }
230