1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: fltdlg.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 //_________________________________________________________________________________________________________________
33 //_________________________________________________________________________________________________________________
41 #ifndef UUI_FLTDLG_HRC
45 //_________________________________________________________________________________________________________________
46 // includes of other projects
47 //_________________________________________________________________________________________________________________
48 #include <com/sun/star/util/XStringWidth.hpp>
49 #include <cppuhelper/implbase1.hxx>
50 #include <unotools/localfilehelper.hxx>
51 #include <tools/list.hxx>
52 #include <tools/urlobj.hxx>
54 #ifndef _BUTTON_HXX //autogen
55 #include <vcl/button.hxx>
57 #include <vos/mutex.hxx>
58 #include <vcl/svapp.hxx>
63 /*-************************************************************************************************************//**
64 @short initialize filter dialog with start values
65 @descr We set some neccessary informations on these instance for later working and create internal structures.
66 After construction user should call "SetFilters()" and "SetURL()" to fill listbox with selectable filter
67 names and set file name of file, which should be used for selected filter.
69 @seealso method SetFilters()
70 @seealso method SetURL()
72 @param "pParentWindow" , parent window for dialog
73 @param "pResMgr" , ressource manager
78 *//*-*************************************************************************************************************/
79 FilterDialog::FilterDialog( Window
* pParentWindow
,
81 : ModalDialog ( pParentWindow
, ResId( DLG_FILTER_SELECT
, *pResMgr
) )
82 , m_ftURL ( this, ResId( FT_URL
, *pResMgr
) )
83 , m_lbFilters ( this, ResId( LB_FILTERS
, *pResMgr
) )
84 , m_btnOK ( this, ResId( BTN_OK
, *pResMgr
) )
85 , m_btnCancel ( this, ResId( BTN_CANCEL
, *pResMgr
) )
86 , m_btnHelp ( this, ResId( BTN_HELP
, *pResMgr
) )
91 /*-************************************************************************************************************//**
92 @short set file name on dialog control
93 @descr We convert given URL (it must be an URL!) into valid file name and show it on our dialog.
97 @param "sURL", URL for showing
102 *//*-*************************************************************************************************************/
103 void FilterDialog::SetURL( const String
& sURL
)
105 // convert it and use given pure string as fallback if convertion failed
106 m_ftURL
.SetText( impl_buildUIFileName(sURL
) );
109 /*-************************************************************************************************************//**
110 @short change list of filter names
111 @descr We save given pointer internal and use it to fill our listbox with given names.
112 Saved list pointer is used on method "AskForFilter()" too, to find user selected item
113 and return pointer into these list as result of operation.
114 So it's possible to call dialog again and again for different or same filter list
115 and ask user for his decision by best performance!
117 @attention Don't free memory of given list after this call till object will die ... or
118 you call "ChangeFilters( NULL )"! Then we forget it too.
120 @seealso method AskForFilter()
122 @param "pFilterNames", pointer to list of filter names, which should be used for later operations.
125 @onerror We clear list box and forget our currently set filter informations completly!
127 *//*-*************************************************************************************************************/
128 void FilterDialog::ChangeFilters( const FilterNameList
* pFilterNames
)
130 m_pFilterNames
= pFilterNames
;
132 if( m_pFilterNames
!= NULL
)
134 for( FilterNameListPtr pItem
= m_pFilterNames
->begin();
135 pItem
!= m_pFilterNames
->end() ;
138 m_lbFilters
.InsertEntry( pItem
->sUI
);
143 /*-************************************************************************************************************//**
144 @short ask user for his decision
145 @descr We show the dialog and if user finish it with "OK" - we try to find selected item in internal saved
146 name list (which you must set in "ChangeFilters()"!). If we return TRUE as result, you can use out
147 parameter "pSelectedItem" as pointer into your FilterNameList to get selected item realy ...
148 but if we return FALSE ... user hsa cancel the dialog ... you shouldnt do that. pSelectedItem isnt
149 set to any valid value then. We don't change them ...
151 @seealso method ChangeFilters()
153 @param "pSelectedItem", returns result of selection as pointer into set list of filter names
154 (valid for function return TRUE only!)
155 @return true => pSelectedItem parameter points into name list and represent use decision
156 false => use has cancelled dialog (pSelectedItem isnt valid then!)
158 @onerror We return false ... but don't change pSelectedItem!
160 *//*-*************************************************************************************************************/
161 bool FilterDialog::AskForFilter( FilterNameListPtr
& pSelectedItem
)
163 bool bSelected
= sal_False
;
165 if( m_pFilterNames
!= NULL
)
167 if( ModalDialog::Execute() == RET_OK
)
169 String sEntry
= m_lbFilters
.GetSelectEntry();
170 if( sEntry
.Len() > 0 )
172 int nPos
= m_lbFilters
.GetSelectEntryPos();
173 if( nPos
< (int)(m_pFilterNames
->size()) )
175 pSelectedItem
= m_pFilterNames
->begin();
176 pSelectedItem
+= nPos
;
177 bSelected
= ( pSelectedItem
!= m_pFilterNames
->end() );
186 /*-************************************************************************************************************//**
187 @short helper class to calculate length of given string
188 @descr Instances of it can be used as callback for INetURLObject::getAbbreviated() method to build
189 short URLs to show it on GUI. We use in ctor set OutputDevice to call special VCL method ...
191 @seealso method OutputDevice::GetTextWidth()
192 @seealso method InetURLObject::getAbbreviated()
199 *//*-*************************************************************************************************************/
200 class StringCalculator
: public ::cppu::WeakImplHelper1
< ::com::sun::star::util::XStringWidth
>
203 StringCalculator( const OutputDevice
* pDevice
)
204 : m_pDevice( pDevice
)
208 sal_Int32 SAL_CALL
queryStringWidth( const ::rtl::OUString
& sString
) throw( ::com::sun::star::uno::RuntimeException
)
210 return (sal_Int32
)(m_pDevice
->GetTextWidth(String(sString
)));
214 const OutputDevice
* m_pDevice
;
217 /*-************************************************************************************************************//**
218 @short try to build short name of given URL to show it n GUI
219 @descr We detect type of given URL automaticly and build this short name depend on this type ...
220 If we couldnt make it right we return full given string without any changes ...
222 @seealso class LocalFileHelper
223 @seealso method InetURLObject::getAbbreviated()
225 @param "sName", file name
226 @return A short file name ...
228 @onerror We return given name without any changes.
230 *//*-*************************************************************************************************************/
231 String
FilterDialog::impl_buildUIFileName( const String
& sName
)
233 String
sShortName( sName
);
235 if( ::utl::LocalFileHelper::ConvertURLToSystemPath( sName
, sShortName
) == sal_True
)
237 // its a system file ... build short name by using osl functionality
241 // otherwise its realy a url ... build short name by using INetURLObject
242 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XStringWidth
> xStringCalculator( new StringCalculator(&m_ftURL
) );
243 if( xStringCalculator
.is() == sal_True
)
245 INetURLObject
aBuilder ( sName
);
246 Size aSize
= m_ftURL
.GetOutputSize();
247 sShortName
= aBuilder
.getAbbreviated( xStringCalculator
, aSize
.Width(), INetURLObject::DECODE_UNAMBIGUOUS
);