xref: /trunk/main/uui/source/fltdlg.cxx (revision 870c3792)
1859212d1SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3859212d1SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4859212d1SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5859212d1SAndrew Rist  * distributed with this work for additional information
6859212d1SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7859212d1SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8859212d1SAndrew Rist  * "License"); you may not use this file except in compliance
9859212d1SAndrew Rist  * with the License.  You may obtain a copy of the License at
10859212d1SAndrew Rist  *
11859212d1SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12859212d1SAndrew Rist  *
13859212d1SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14859212d1SAndrew Rist  * software distributed under the License is distributed on an
15859212d1SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16859212d1SAndrew Rist  * KIND, either express or implied.  See the License for the
17859212d1SAndrew Rist  * specific language governing permissions and limitations
18859212d1SAndrew Rist  * under the License.
19859212d1SAndrew Rist  *
20859212d1SAndrew Rist  *************************************************************/
21859212d1SAndrew Rist 
22859212d1SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir //_________________________________________________________________________________________________________________
25cdf0e10cSrcweir //	my own includes
26cdf0e10cSrcweir //_________________________________________________________________________________________________________________
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include "fltdlg.hxx"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #ifndef UUI_IDS_HRC
31cdf0e10cSrcweir #include "ids.hrc"
32cdf0e10cSrcweir #endif
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #ifndef UUI_FLTDLG_HRC
35cdf0e10cSrcweir #include "fltdlg.hrc"
36cdf0e10cSrcweir #endif
37cdf0e10cSrcweir 
38cdf0e10cSrcweir //_________________________________________________________________________________________________________________
39cdf0e10cSrcweir //	includes of other projects
40cdf0e10cSrcweir //_________________________________________________________________________________________________________________
41cdf0e10cSrcweir #include <com/sun/star/util/XStringWidth.hpp>
42cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
43cdf0e10cSrcweir #include <unotools/localfilehelper.hxx>
44cdf0e10cSrcweir #include <tools/list.hxx>
45cdf0e10cSrcweir #include <tools/urlobj.hxx>
46cdf0e10cSrcweir 
47cdf0e10cSrcweir #ifndef _BUTTON_HXX //autogen
48cdf0e10cSrcweir #include <vcl/button.hxx>
49cdf0e10cSrcweir #endif
50cdf0e10cSrcweir #include <vos/mutex.hxx>
51cdf0e10cSrcweir #include <vcl/svapp.hxx>
52cdf0e10cSrcweir 
53cdf0e10cSrcweir namespace uui
54cdf0e10cSrcweir {
55cdf0e10cSrcweir 
56cdf0e10cSrcweir /*-************************************************************************************************************//**
57cdf0e10cSrcweir     @short      initialize filter dialog with start values
5886e1cf34SPedro Giffuni     @descr      We set some necessary informations on these instance for later working and create internal structures.
59cdf0e10cSrcweir                 After construction user should call "SetFilters()" and "SetURL()" to fill listbox with selectable filter
60cdf0e10cSrcweir                 names and set file name of file, which should be used for selected filter.
61cdf0e10cSrcweir 
62cdf0e10cSrcweir     @seealso    method SetFilters()
63cdf0e10cSrcweir     @seealso    method SetURL()
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     @param      "pParentWindow"  , parent window for dialog
66*870c3792Smseidel     @param      "pResMgr"        , resource manager
67cdf0e10cSrcweir     @return     -
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     @onerror    -
70cdf0e10cSrcweir     @threadsafe no
71cdf0e10cSrcweir *//*-*************************************************************************************************************/
FilterDialog(Window * pParentWindow,ResMgr * pResMgr)72cdf0e10cSrcweir FilterDialog::FilterDialog( Window* pParentWindow ,
73cdf0e10cSrcweir                             ResMgr* pResMgr       )
74cdf0e10cSrcweir     :   ModalDialog  ( pParentWindow, ResId( DLG_FILTER_SELECT, *pResMgr ) )
75cdf0e10cSrcweir     ,   m_ftURL      ( this, ResId( FT_URL, *pResMgr            )                   )
76cdf0e10cSrcweir     ,   m_lbFilters  ( this, ResId( LB_FILTERS, *pResMgr        )                   )
77cdf0e10cSrcweir     ,   m_btnOK      ( this, ResId( BTN_OK, *pResMgr            )                   )
78cdf0e10cSrcweir     ,   m_btnCancel  ( this, ResId( BTN_CANCEL, *pResMgr        )                   )
79cdf0e10cSrcweir     ,   m_btnHelp    ( this, ResId( BTN_HELP, *pResMgr          )                   )
80cdf0e10cSrcweir {
81cdf0e10cSrcweir     FreeResource();
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir /*-************************************************************************************************************//**
85cdf0e10cSrcweir     @short      set file name on dialog control
86cdf0e10cSrcweir     @descr      We convert given URL (it must be an URL!) into valid file name and show it on our dialog.
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     @seealso    -
89cdf0e10cSrcweir 
90cdf0e10cSrcweir     @param      "sURL", URL for showing
91cdf0e10cSrcweir     @return     -
92cdf0e10cSrcweir 
93cdf0e10cSrcweir     @onerror    -
94cdf0e10cSrcweir     @threadsafe no
95cdf0e10cSrcweir *//*-*************************************************************************************************************/
SetURL(const String & sURL)96cdf0e10cSrcweir void FilterDialog::SetURL( const String& sURL )
97cdf0e10cSrcweir {
9886e1cf34SPedro Giffuni     // convert it and use given pure string as fallback if conversion failed
99cdf0e10cSrcweir     m_ftURL.SetText( impl_buildUIFileName(sURL) );
100cdf0e10cSrcweir }
101cdf0e10cSrcweir 
102cdf0e10cSrcweir /*-************************************************************************************************************//**
103cdf0e10cSrcweir     @short      change list of filter names
104cdf0e10cSrcweir     @descr      We save given pointer internal and use it to fill our listbox with given names.
105cdf0e10cSrcweir                 Saved list pointer is used on method "AskForFilter()" too, to find user selected item
106cdf0e10cSrcweir                 and return pointer into these list as result of operation.
107cdf0e10cSrcweir                 So it's possible to call dialog again and again for different or same filter list
108cdf0e10cSrcweir                 and ask user for his decision by best performance!
109cdf0e10cSrcweir 
110cdf0e10cSrcweir     @attention  Don't free memory of given list after this call till object will die ... or
111cdf0e10cSrcweir                 you call "ChangeFilters( NULL )"! Then we forget it too.
112cdf0e10cSrcweir 
113cdf0e10cSrcweir     @seealso    method AskForFilter()
114cdf0e10cSrcweir 
115cdf0e10cSrcweir     @param      "pFilterNames", pointer to list of filter names, which should be used for later operations.
116cdf0e10cSrcweir     @return     -
117cdf0e10cSrcweir 
118*870c3792Smseidel     @onerror    We clear list box and forget our currently set filter informations completely!
119cdf0e10cSrcweir     @threadsafe no
120cdf0e10cSrcweir *//*-*************************************************************************************************************/
ChangeFilters(const FilterNameList * pFilterNames)121cdf0e10cSrcweir void FilterDialog::ChangeFilters( const FilterNameList* pFilterNames )
122cdf0e10cSrcweir {
123cdf0e10cSrcweir     m_pFilterNames = pFilterNames;
124cdf0e10cSrcweir     m_lbFilters.Clear();
125cdf0e10cSrcweir     if( m_pFilterNames != NULL )
126cdf0e10cSrcweir     {
127cdf0e10cSrcweir         for( FilterNameListPtr pItem  = m_pFilterNames->begin();
128cdf0e10cSrcweir                                pItem != m_pFilterNames->end()  ;
129cdf0e10cSrcweir                                ++pItem                         )
130cdf0e10cSrcweir         {
131cdf0e10cSrcweir             m_lbFilters.InsertEntry( pItem->sUI );
132cdf0e10cSrcweir         }
133cdf0e10cSrcweir     }
134cdf0e10cSrcweir }
135cdf0e10cSrcweir 
136cdf0e10cSrcweir /*-************************************************************************************************************//**
137cdf0e10cSrcweir     @short      ask user for his decision
138cdf0e10cSrcweir     @descr      We show the dialog and if user finish it with "OK" - we try to find selected item in internal saved
139cdf0e10cSrcweir                 name list (which you must set in "ChangeFilters()"!). If we return sal_True as result, you can use out
14086e1cf34SPedro Giffuni                 parameter "pSelectedItem" as pointer into your FilterNameList to get selected item really ...
1419f813b30Smseidel                 but if we return sal_False ... user has canceled the dialog ... you shouldn't do that. pSelectedItem isn't
142cdf0e10cSrcweir                 set to any valid value then. We don't change them ...
143cdf0e10cSrcweir 
144cdf0e10cSrcweir     @seealso    method ChangeFilters()
145cdf0e10cSrcweir 
146cdf0e10cSrcweir     @param      "pSelectedItem", returns result of selection as pointer into set list of filter names
147cdf0e10cSrcweir                                  (valid for function return sal_True only!)
148cdf0e10cSrcweir     @return     true  => pSelectedItem parameter points into name list and represent use decision
1499f813b30Smseidel                 false => use has canceled dialog (pSelectedItem isn't valid then!)
150cdf0e10cSrcweir 
151cdf0e10cSrcweir     @onerror    We return false ... but don't change pSelectedItem!
152cdf0e10cSrcweir     @threadsafe no
153cdf0e10cSrcweir *//*-*************************************************************************************************************/
AskForFilter(FilterNameListPtr & pSelectedItem)154cdf0e10cSrcweir bool FilterDialog::AskForFilter( FilterNameListPtr& pSelectedItem )
155cdf0e10cSrcweir {
156cdf0e10cSrcweir     bool bSelected = sal_False;
157cdf0e10cSrcweir 
158cdf0e10cSrcweir     if( m_pFilterNames != NULL )
159cdf0e10cSrcweir     {
160cdf0e10cSrcweir         if( ModalDialog::Execute() == RET_OK )
161cdf0e10cSrcweir         {
162cdf0e10cSrcweir             String sEntry = m_lbFilters.GetSelectEntry();
163cdf0e10cSrcweir             if( sEntry.Len() > 0 )
164cdf0e10cSrcweir             {
165cdf0e10cSrcweir                 int nPos = m_lbFilters.GetSelectEntryPos();
166cdf0e10cSrcweir                 if( nPos < (int)(m_pFilterNames->size()) )
167cdf0e10cSrcweir                 {
168cdf0e10cSrcweir                     pSelectedItem  = m_pFilterNames->begin();
169cdf0e10cSrcweir                     pSelectedItem += nPos;
170cdf0e10cSrcweir                     bSelected      = ( pSelectedItem != m_pFilterNames->end() );
171cdf0e10cSrcweir                 }
172cdf0e10cSrcweir             }
173cdf0e10cSrcweir         }
174cdf0e10cSrcweir     }
175cdf0e10cSrcweir 
176cdf0e10cSrcweir     return bSelected;
177cdf0e10cSrcweir }
178cdf0e10cSrcweir 
179cdf0e10cSrcweir /*-************************************************************************************************************//**
180cdf0e10cSrcweir     @short      helper class to calculate length of given string
181cdf0e10cSrcweir     @descr      Instances of it can be used as callback for INetURLObject::getAbbreviated() method to build
182cdf0e10cSrcweir                 short URLs to show it on GUI. We use in ctor set OutputDevice to call special VCL method ...
183cdf0e10cSrcweir 
184cdf0e10cSrcweir     @seealso    method OutputDevice::GetTextWidth()
185cdf0e10cSrcweir     @seealso    method InetURLObject::getAbbreviated()
186cdf0e10cSrcweir 
187cdf0e10cSrcweir     @param      -
188cdf0e10cSrcweir     @return     -
189cdf0e10cSrcweir 
190cdf0e10cSrcweir     @onerror    -
191cdf0e10cSrcweir     @threadsafe no
192cdf0e10cSrcweir *//*-*************************************************************************************************************/
193cdf0e10cSrcweir class StringCalculator : public ::cppu::WeakImplHelper1< ::com::sun::star::util::XStringWidth >
194cdf0e10cSrcweir {
195cdf0e10cSrcweir 	public:
StringCalculator(const OutputDevice * pDevice)196cdf0e10cSrcweir         StringCalculator( const OutputDevice* pDevice )
197cdf0e10cSrcweir             : m_pDevice( pDevice )
198cdf0e10cSrcweir         {
199cdf0e10cSrcweir         }
200cdf0e10cSrcweir 
queryStringWidth(const::rtl::OUString & sString)201cdf0e10cSrcweir         sal_Int32 SAL_CALL queryStringWidth( const ::rtl::OUString& sString ) throw( ::com::sun::star::uno::RuntimeException )
202cdf0e10cSrcweir 		{
203cdf0e10cSrcweir             return (sal_Int32)(m_pDevice->GetTextWidth(String(sString)));
204cdf0e10cSrcweir 		}
205cdf0e10cSrcweir 
206cdf0e10cSrcweir     private:
207cdf0e10cSrcweir         const OutputDevice* m_pDevice;
208cdf0e10cSrcweir };
209cdf0e10cSrcweir 
210cdf0e10cSrcweir /*-************************************************************************************************************//**
2119f813b30Smseidel     @short      try to build short name of given URL to show it in GUI
21286e1cf34SPedro Giffuni     @descr      We detect type of given URL automatically and build this short name depend on this type ...
2139f813b30Smseidel                 If we couldn't make it right we return full given string without any changes ...
214cdf0e10cSrcweir 
215cdf0e10cSrcweir     @seealso    class LocalFileHelper
216cdf0e10cSrcweir     @seealso    method InetURLObject::getAbbreviated()
217cdf0e10cSrcweir 
218cdf0e10cSrcweir     @param      "sName", file name
219cdf0e10cSrcweir     @return     A short file name ...
220cdf0e10cSrcweir 
221cdf0e10cSrcweir     @onerror    We return given name without any changes.
222cdf0e10cSrcweir     @threadsafe no
223cdf0e10cSrcweir *//*-*************************************************************************************************************/
impl_buildUIFileName(const String & sName)224cdf0e10cSrcweir String FilterDialog::impl_buildUIFileName( const String& sName )
225cdf0e10cSrcweir {
226cdf0e10cSrcweir     String sShortName( sName );
227cdf0e10cSrcweir 
228cdf0e10cSrcweir     if( ::utl::LocalFileHelper::ConvertURLToSystemPath( sName, sShortName ) == sal_True )
229cdf0e10cSrcweir     {
2309f813b30Smseidel         // it's a system file ... build short name by using osl functionality
231cdf0e10cSrcweir     }
232cdf0e10cSrcweir     else
233cdf0e10cSrcweir     {
23486e1cf34SPedro Giffuni         // otherwise its really a url ... build short name by using INetURLObject
235cdf0e10cSrcweir         ::com::sun::star::uno::Reference< ::com::sun::star::util::XStringWidth > xStringCalculator( new StringCalculator(&m_ftURL) );
236cdf0e10cSrcweir         if( xStringCalculator.is() == sal_True )
237cdf0e10cSrcweir         {
238cdf0e10cSrcweir             INetURLObject aBuilder   ( sName );
239cdf0e10cSrcweir             Size          aSize      = m_ftURL.GetOutputSize();
240cdf0e10cSrcweir                           sShortName = aBuilder.getAbbreviated( xStringCalculator, aSize.Width(), INetURLObject::DECODE_UNAMBIGUOUS );
241cdf0e10cSrcweir         }
242cdf0e10cSrcweir     }
243cdf0e10cSrcweir 
244cdf0e10cSrcweir     return sShortName;
245cdf0e10cSrcweir }
246cdf0e10cSrcweir 
247cdf0e10cSrcweir }   // namespace uui
248