tdf#162326 remove character formats and styles on style apply
[LibreOffice.git] / uui / source / fltdlg.cxx
blob0aeee3a221f632485bb34287d0583f91f3f08fde
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 <com/sun/star/util/XStringWidth.hpp>
23 #include <cppuhelper/implbase.hxx>
24 #include <tools/urlobj.hxx>
26 #include <osl/file.hxx>
28 namespace uui
30 /*-************************************************************************************************************
31 @short initialize filter dialog with start values
32 @descr We set some necessary information on these instance for later working and create internal structures.
33 After construction user should call "SetFilters()" and "SetURL()" to fill listbox with selectable filter
34 names and set file name of file, which should be used for selected filter.
36 @seealso method SetFilters()
37 @seealso method SetURL()
39 @param "pParentWindow" , parent window for dialog
40 @threadsafe no
41 */ /*-*************************************************************************************************************/
42 FilterDialog::FilterDialog(weld::Window* pParentWindow)
43 : GenericDialogController(pParentWindow, u"uui/ui/filterselect.ui"_ustr,
44 u"FilterSelectDialog"_ustr)
45 , m_pFilterNames(nullptr)
46 , m_xFtURL(m_xBuilder->weld_label(u"url"_ustr))
47 , m_xLbFilters(m_xBuilder->weld_tree_view(u"filters"_ustr))
49 m_xLbFilters->set_size_request(m_xLbFilters->get_approximate_digit_width() * 42,
50 m_xLbFilters->get_height_rows(15));
53 FilterDialog::~FilterDialog() {}
55 /*-************************************************************************************************************
56 @short set file name on dialog control
57 @descr We convert given URL (it must be a URL!) into valid file name and show it on our dialog.
58 @param "sURL", URL for showing
59 @threadsafe no
60 */ /*-*************************************************************************************************************/
61 void FilterDialog::SetURL(const OUString& sURL)
63 // convert it and use given pure string as fallback if conversion failed
64 m_xFtURL->set_label(impl_buildUIFileName(sURL));
67 /*-************************************************************************************************************
68 @short change list of filter names
69 @descr We save given pointer internal and use it to fill our listbox with given names.
70 Saved list pointer is used on method "AskForFilter()" too, to find user selected item
71 and return pointer into these list as result of operation.
72 So it's possible to call dialog again and again for different or same filter list
73 and ask user for his decision by best performance!
75 @attention Don't free memory of given list after this call till object will die ... or
76 you call "ChangeFilters( NULL )"! Then we forget it too.
78 @seealso method AskForFilter()
80 @param "pFilterNames", pointer to list of filter names, which should be used for later operations.
81 @onerror We clear list box and forget our currently set filter information completely!
82 @threadsafe no
83 */ /*-*************************************************************************************************************/
84 void FilterDialog::ChangeFilters(const FilterNameList* pFilterNames)
86 m_pFilterNames = pFilterNames;
87 m_xLbFilters->clear();
88 if (m_pFilterNames != nullptr)
90 for (const auto& rItem : *m_pFilterNames)
92 m_xLbFilters->append_text(rItem.sUI);
97 /*-************************************************************************************************************
98 @short ask user for his decision
99 @descr We show the dialog and if user finish it with "OK" - we try to find selected item in internal saved
100 name list (which you must set in "ChangeFilters()"!). If we return sal_True as result, you can use out
101 parameter "pSelectedItem" as pointer into your FilterNameList to get selected item really ...
102 but if we return sal_False ... user has cancel the dialog ... you should not do that. pSelectedItem is not
103 set to any valid value then. We don't change them ...
105 @seealso method ChangeFilters()
107 @param "pSelectedItem", returns result of selection as pointer into set list of filter names
108 (valid for function return sal_True only!)
109 @return true => pSelectedItem parameter points into name list and represent use decision
110 false => use has cancelled dialog (pSelectedItem is not valid then!)
112 @onerror We return false ... but don't change pSelectedItem!
113 @threadsafe no
114 */ /*-*************************************************************************************************************/
115 bool FilterDialog::AskForFilter(FilterNameListPtr& pSelectedItem)
117 bool bSelected = false;
119 if (m_pFilterNames != nullptr)
121 if (m_xDialog->run() == RET_OK)
123 OUString sEntry = m_xLbFilters->get_selected_text();
124 if (!sEntry.isEmpty())
126 int nPos = m_xLbFilters->get_selected_index();
127 if (nPos < static_cast<int>(m_pFilterNames->size()))
129 pSelectedItem = m_pFilterNames->begin();
130 pSelectedItem += nPos;
131 bSelected = (pSelectedItem != m_pFilterNames->end());
137 return bSelected;
140 namespace
142 /*-************************************************************************************************************
143 @short helper class to calculate length of given string
144 @descr Instances of it can be used as callback for INetURLObject::getAbbreviated() method to build
145 short URLs to show it on GUI. We use in ctor set OutputDevice to call special VCL method ...
147 @seealso method OutputDevice::GetTextWidth()
148 @seealso method InetURLObject::getAbbreviated()
149 @threadsafe no
150 */ /*-*************************************************************************************************************/
151 class StringCalculator : public ::cppu::WeakImplHelper<css::util::XStringWidth>
153 public:
154 explicit StringCalculator(weld::Widget* pDevice)
155 : m_pDevice(pDevice)
159 sal_Int32 SAL_CALL queryStringWidth(const OUString& sString) override
161 return static_cast<sal_Int32>(m_pDevice->get_pixel_size(sString).Width());
164 private:
165 weld::Widget* m_pDevice;
169 /*-************************************************************************************************************
170 @short try to build short name of given URL to show it n GUI
171 @descr We detect type of given URL automatically and build this short name depend on this type ...
172 If we couldn't make it right we return full given string without any changes ...
174 @seealso method InetURLObject::getAbbreviated()
176 @param "sName", file name
177 @return A short file name ...
179 @onerror We return given name without any changes.
180 @threadsafe no
181 */ /*-*************************************************************************************************************/
182 OUString FilterDialog::impl_buildUIFileName(const OUString& sName)
184 OUString sShortName(sName);
186 if (osl::FileBase::getSystemPathFromFileURL(sName, sShortName) == osl::FileBase::E_None)
189 // it's a system file... build short name by using osl functionality
191 else
193 // otherwise it's really a URL... build short name by using INetURLObject
194 css::uno::Reference<css::util::XStringWidth> xStringCalculator(
195 new StringCalculator(m_xFtURL.get()));
196 if (xStringCalculator.is())
198 INetURLObject aBuilder(sName);
199 Size aSize = m_xLbFilters->get_preferred_size();
200 sShortName = aBuilder.getAbbreviated(xStringCalculator, aSize.Width(),
201 INetURLObject::DecodeMechanism::Unambiguous);
205 return sShortName;
208 } // namespace uui
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */