Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / uui / source / fltdlg.cxx
blob2911acb03b22cbf04aa57e8cee51180784ea3557
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "fltdlg.hxx"
22 #include <ids.hxx>
24 #include <com/sun/star/util/XStringWidth.hpp>
25 #include <cppuhelper/implbase.hxx>
26 #include <tools/urlobj.hxx>
28 #include <vcl/button.hxx>
29 #include <osl/file.hxx>
30 #include <vcl/svapp.hxx>
32 namespace uui
35 /*-************************************************************************************************************
36 @short initialize filter dialog with start values
37 @descr We set some necessary information on these instance for later working and create internal structures.
38 After construction user should call "SetFilters()" and "SetURL()" to fill listbox with selectable filter
39 names and set file name of file, which should be used for selected filter.
41 @seealso method SetFilters()
42 @seealso method SetURL()
44 @param "pParentWindow" , parent window for dialog
45 @threadsafe no
46 *//*-*************************************************************************************************************/
47 FilterDialog::FilterDialog(weld::Window* pParentWindow)
48 : GenericDialogController(pParentWindow, "uui/ui/filterselect.ui", "FilterSelectDialog")
49 , m_pFilterNames(nullptr)
50 , m_xFtURL(m_xBuilder->weld_label("url"))
51 , m_xLbFilters(m_xBuilder->weld_tree_view("filters"))
53 m_xLbFilters->set_size_request(m_xLbFilters->get_approximate_digit_width() * 42,
54 m_xLbFilters->get_height_rows(15));
57 FilterDialog::~FilterDialog()
61 /*-************************************************************************************************************
62 @short set file name on dialog control
63 @descr We convert given URL (it must be an URL!) into valid file name and show it on our dialog.
64 @param "sURL", URL for showing
65 @threadsafe no
66 *//*-*************************************************************************************************************/
67 void FilterDialog::SetURL( const OUString& sURL )
69 // convert it and use given pure string as fallback if conversion failed
70 m_xFtURL->set_label(impl_buildUIFileName(sURL));
73 /*-************************************************************************************************************
74 @short change list of filter names
75 @descr We save given pointer internal and use it to fill our listbox with given names.
76 Saved list pointer is used on method "AskForFilter()" too, to find user selected item
77 and return pointer into these list as result of operation.
78 So it's possible to call dialog again and again for different or same filter list
79 and ask user for his decision by best performance!
81 @attention Don't free memory of given list after this call till object will die ... or
82 you call "ChangeFilters( NULL )"! Then we forget it too.
84 @seealso method AskForFilter()
86 @param "pFilterNames", pointer to list of filter names, which should be used for later operations.
87 @onerror We clear list box and forget our currently set filter information completely!
88 @threadsafe no
89 *//*-*************************************************************************************************************/
90 void FilterDialog::ChangeFilters( const FilterNameList* pFilterNames )
92 m_pFilterNames = pFilterNames;
93 m_xLbFilters->clear();
94 if( m_pFilterNames != nullptr )
96 for( FilterNameListPtr pItem = m_pFilterNames->begin();
97 pItem != m_pFilterNames->end() ;
98 ++pItem )
100 m_xLbFilters->append_text(pItem->sUI);
105 /*-************************************************************************************************************
106 @short ask user for his decision
107 @descr We show the dialog and if user finish it with "OK" - we try to find selected item in internal saved
108 name list (which you must set in "ChangeFilters()"!). If we return sal_True as result, you can use out
109 parameter "pSelectedItem" as pointer into your FilterNameList to get selected item really ...
110 but if we return sal_False ... user has cancel the dialog ... you should not do that. pSelectedItem is not
111 set to any valid value then. We don't change them ...
113 @seealso method ChangeFilters()
115 @param "pSelectedItem", returns result of selection as pointer into set list of filter names
116 (valid for function return sal_True only!)
117 @return true => pSelectedItem parameter points into name list and represent use decision
118 false => use has cancelled dialog (pSelectedItem is not valid then!)
120 @onerror We return false ... but don't change pSelectedItem!
121 @threadsafe no
122 *//*-*************************************************************************************************************/
123 bool FilterDialog::AskForFilter( FilterNameListPtr& pSelectedItem )
125 bool bSelected = false;
127 if( m_pFilterNames != nullptr )
129 if (m_xDialog->run() == RET_OK)
131 OUString sEntry = m_xLbFilters->get_selected_text();
132 if( !sEntry.isEmpty() )
134 int nPos = m_xLbFilters->get_selected_index();
135 if( nPos < static_cast<int>(m_pFilterNames->size()) )
137 pSelectedItem = m_pFilterNames->begin();
138 pSelectedItem += nPos;
139 bSelected = ( pSelectedItem != m_pFilterNames->end() );
145 return bSelected;
148 /*-************************************************************************************************************
149 @short helper class to calculate length of given string
150 @descr Instances of it can be used as callback for INetURLObject::getAbbreviated() method to build
151 short URLs to show it on GUI. We use in ctor set OutputDevice to call special VCL method ...
153 @seealso method OutputDevice::GetTextWidth()
154 @seealso method InetURLObject::getAbbreviated()
155 @threadsafe no
156 *//*-*************************************************************************************************************/
157 class StringCalculator : public ::cppu::WeakImplHelper< css::util::XStringWidth >
159 public:
160 explicit StringCalculator(weld::Widget* pDevice)
161 : m_pDevice(pDevice)
165 sal_Int32 SAL_CALL queryStringWidth( const OUString& sString ) override
167 return static_cast<sal_Int32>(m_pDevice->get_pixel_size(sString).Width());
170 private:
171 weld::Widget* m_pDevice;
174 /*-************************************************************************************************************
175 @short try to build short name of given URL to show it n GUI
176 @descr We detect type of given URL automatically and build this short name depend on this type ...
177 If we couldn't make it right we return full given string without any changes ...
179 @seealso method InetURLObject::getAbbreviated()
181 @param "sName", file name
182 @return A short file name ...
184 @onerror We return given name without any changes.
185 @threadsafe no
186 *//*-*************************************************************************************************************/
187 OUString FilterDialog::impl_buildUIFileName( const OUString& sName )
189 OUString sShortName( sName );
191 if (osl::FileBase::getSystemPathFromFileURL(sName, sShortName) == osl::FileBase::E_None)
194 // it's a system file ... build short name by using osl functionality
196 else
198 // otherwise its really a url ... build short name by using INetURLObject
199 css::uno::Reference< css::util::XStringWidth > xStringCalculator(new StringCalculator(m_xFtURL.get()));
200 if( xStringCalculator.is() )
202 INetURLObject aBuilder ( sName );
203 Size aSize = m_xLbFilters->get_preferred_size();
204 sShortName = aBuilder.getAbbreviated( xStringCalculator, aSize.Width(), INetURLObject::DecodeMechanism::Unambiguous );
208 return sShortName;
211 } // namespace uui
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */