Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / uui / source / fltdlg.cxx
blob124471716771082256863f1f21bb1e62f439ebca
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 <osl/file.hxx>
29 #include <vcl/svapp.hxx>
31 namespace uui
34 /*-************************************************************************************************************
35 @short initialize filter dialog with start values
36 @descr We set some necessary information on these instance for later working and create internal structures.
37 After construction user should call "SetFilters()" and "SetURL()" to fill listbox with selectable filter
38 names and set file name of file, which should be used for selected filter.
40 @seealso method SetFilters()
41 @seealso method SetURL()
43 @param "pParentWindow" , parent window for dialog
44 @threadsafe no
45 *//*-*************************************************************************************************************/
46 FilterDialog::FilterDialog(weld::Window* pParentWindow)
47 : GenericDialogController(pParentWindow, "uui/ui/filterselect.ui", "FilterSelectDialog")
48 , m_pFilterNames(nullptr)
49 , m_xFtURL(m_xBuilder->weld_label("url"))
50 , m_xLbFilters(m_xBuilder->weld_tree_view("filters"))
52 m_xLbFilters->set_size_request(m_xLbFilters->get_approximate_digit_width() * 42,
53 m_xLbFilters->get_height_rows(15));
56 FilterDialog::~FilterDialog()
60 /*-************************************************************************************************************
61 @short set file name on dialog control
62 @descr We convert given URL (it must be a URL!) into valid file name and show it on our dialog.
63 @param "sURL", URL for showing
64 @threadsafe no
65 *//*-*************************************************************************************************************/
66 void FilterDialog::SetURL( const OUString& sURL )
68 // convert it and use given pure string as fallback if conversion failed
69 m_xFtURL->set_label(impl_buildUIFileName(sURL));
72 /*-************************************************************************************************************
73 @short change list of filter names
74 @descr We save given pointer internal and use it to fill our listbox with given names.
75 Saved list pointer is used on method "AskForFilter()" too, to find user selected item
76 and return pointer into these list as result of operation.
77 So it's possible to call dialog again and again for different or same filter list
78 and ask user for his decision by best performance!
80 @attention Don't free memory of given list after this call till object will die ... or
81 you call "ChangeFilters( NULL )"! Then we forget it too.
83 @seealso method AskForFilter()
85 @param "pFilterNames", pointer to list of filter names, which should be used for later operations.
86 @onerror We clear list box and forget our currently set filter information completely!
87 @threadsafe no
88 *//*-*************************************************************************************************************/
89 void FilterDialog::ChangeFilters( const FilterNameList* pFilterNames )
91 m_pFilterNames = pFilterNames;
92 m_xLbFilters->clear();
93 if( m_pFilterNames != nullptr )
95 for( const auto& rItem : *m_pFilterNames )
97 m_xLbFilters->append_text(rItem.sUI);
102 /*-************************************************************************************************************
103 @short ask user for his decision
104 @descr We show the dialog and if user finish it with "OK" - we try to find selected item in internal saved
105 name list (which you must set in "ChangeFilters()"!). If we return sal_True as result, you can use out
106 parameter "pSelectedItem" as pointer into your FilterNameList to get selected item really ...
107 but if we return sal_False ... user has cancel the dialog ... you should not do that. pSelectedItem is not
108 set to any valid value then. We don't change them ...
110 @seealso method ChangeFilters()
112 @param "pSelectedItem", returns result of selection as pointer into set list of filter names
113 (valid for function return sal_True only!)
114 @return true => pSelectedItem parameter points into name list and represent use decision
115 false => use has cancelled dialog (pSelectedItem is not valid then!)
117 @onerror We return false ... but don't change pSelectedItem!
118 @threadsafe no
119 *//*-*************************************************************************************************************/
120 bool FilterDialog::AskForFilter( FilterNameListPtr& pSelectedItem )
122 bool bSelected = false;
124 if( m_pFilterNames != nullptr )
126 if (m_xDialog->run() == RET_OK)
128 OUString sEntry = m_xLbFilters->get_selected_text();
129 if( !sEntry.isEmpty() )
131 int nPos = m_xLbFilters->get_selected_index();
132 if( nPos < static_cast<int>(m_pFilterNames->size()) )
134 pSelectedItem = m_pFilterNames->begin();
135 pSelectedItem += nPos;
136 bSelected = ( pSelectedItem != m_pFilterNames->end() );
142 return bSelected;
145 /*-************************************************************************************************************
146 @short helper class to calculate length of given string
147 @descr Instances of it can be used as callback for INetURLObject::getAbbreviated() method to build
148 short URLs to show it on GUI. We use in ctor set OutputDevice to call special VCL method ...
150 @seealso method OutputDevice::GetTextWidth()
151 @seealso method InetURLObject::getAbbreviated()
152 @threadsafe no
153 *//*-*************************************************************************************************************/
154 class StringCalculator : public ::cppu::WeakImplHelper< css::util::XStringWidth >
156 public:
157 explicit StringCalculator(weld::Widget* pDevice)
158 : m_pDevice(pDevice)
162 sal_Int32 SAL_CALL queryStringWidth( const OUString& sString ) override
164 return static_cast<sal_Int32>(m_pDevice->get_pixel_size(sString).Width());
167 private:
168 weld::Widget* const m_pDevice;
171 /*-************************************************************************************************************
172 @short try to build short name of given URL to show it n GUI
173 @descr We detect type of given URL automatically and build this short name depend on this type ...
174 If we couldn't make it right we return full given string without any changes ...
176 @seealso method InetURLObject::getAbbreviated()
178 @param "sName", file name
179 @return A short file name ...
181 @onerror We return given name without any changes.
182 @threadsafe no
183 *//*-*************************************************************************************************************/
184 OUString FilterDialog::impl_buildUIFileName( const OUString& sName )
186 OUString sShortName( sName );
188 if (osl::FileBase::getSystemPathFromFileURL(sName, sShortName) == osl::FileBase::E_None)
191 // it's a system file... build short name by using osl functionality
193 else
195 // otherwise it's really a URL... build short name by using INetURLObject
196 css::uno::Reference< css::util::XStringWidth > xStringCalculator(new StringCalculator(m_xFtURL.get()));
197 if( xStringCalculator.is() )
199 INetURLObject aBuilder ( sName );
200 Size aSize = m_xLbFilters->get_preferred_size();
201 sShortName = aBuilder.getAbbreviated( xStringCalculator, aSize.Width(), INetURLObject::DecodeMechanism::Unambiguous );
205 return sShortName;
208 } // namespace uui
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */