1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <cppuhelper/implbase.hxx>
#include <svtools/toolboxcontroller.hxx>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/frame/XFrame.hpp>
#include <vcl/vclptr.hxx>
#include <vcl/lstbox.hxx>
#include <vcl/svapp.hxx>
#include <toolkit/helper/vclunohelper.hxx>
#include <vcl/toolbox.hxx>
#include <vcl/fixed.hxx>
#include <sfx2/viewfrm.hxx>
#include <sfx2/classificationhelper.hxx>
#include <sfx2/sfx.hrc>
#include <sfx2/sfxresid.hxx>
#include <cppuhelper/supportsservice.hxx>
#include <comphelper/propertysequence.hxx>
#include <comphelper/dispatchcommand.hxx>
#include <comphelper/configurationlistener.hxx>
using namespace com::sun::star;
namespace sfx2
{
class ClassificationCategoriesController;
using ClassificationPropertyListenerBase = comphelper::ConfigurationListenerProperty<OUString>;
/// Listens to configuration changes, so no restart is needed after setting the classification path.
class ClassificationPropertyListener : public ClassificationPropertyListenerBase
{
ClassificationCategoriesController& m_rController;
public:
ClassificationPropertyListener(const rtl::Reference<comphelper::ConfigurationListener>& xListener, ClassificationCategoriesController& rController);
void setProperty(const uno::Any& rProperty) override;
};
using ClassificationCategoriesControllerBase = cppu::ImplInheritanceHelper<svt::ToolboxController, lang::XServiceInfo>;
class ClassificationControl;
/// Controller for .uno:ClassificationApply.
class ClassificationCategoriesController : public ClassificationCategoriesControllerBase
{
VclPtr<ClassificationControl> m_pClassification;
rtl::Reference<comphelper::ConfigurationListener> m_xListener;
ClassificationPropertyListener m_aPropertyListener;
DECL_LINK(SelectHdl, ListBox&, void);
public:
explicit ClassificationCategoriesController(const uno::Reference<uno::XComponentContext>& rContext);
// XServiceInfo
OUString SAL_CALL getImplementationName() override;
sal_Bool SAL_CALL supportsService(const OUString& rServiceName) override;
uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
// XComponent
void SAL_CALL dispose() override;
// XToolbarController
uno::Reference<awt::XWindow> SAL_CALL createItemWindow(const uno::Reference<awt::XWindow>& rParent) override;
// XStatusListener
void SAL_CALL statusChanged(const frame::FeatureStateEvent& rEvent) override;
void removeEntries();
};
/// Classification control is the parent of all widgets that belongs to ClassificationCategoriesController.
class SAL_WARN_UNUSED ClassificationControl : public vcl::Window
{
std::map<SfxClassificationPolicyType, VclPtr<FixedText>> m_pLabels;
std::map<SfxClassificationPolicyType, VclPtr<ListBox>> m_pCategories;
void SetOptimalSize();
void DataChanged(const DataChangedEvent& rEvent) override;
public:
explicit ClassificationControl(vcl::Window* pParent);
~ClassificationControl() override;
void dispose() override;
void Resize() override;
const VclPtr<ListBox>& getCategories(SfxClassificationPolicyType eType);
std::size_t getLabelsSize();
OUString getCategoryType(ListBox& rCategory);
};
ClassificationPropertyListener::ClassificationPropertyListener(const rtl::Reference<comphelper::ConfigurationListener>& xListener, ClassificationCategoriesController& rController)
: ClassificationPropertyListenerBase(xListener, "WritePath")
, m_rController(rController)
{
}
void ClassificationPropertyListener::setProperty(const uno::Any& /*rProperty*/)
{
// So that its gets re-filled with entries from the new policy.
m_rController.removeEntries();
}
ClassificationCategoriesController::ClassificationCategoriesController(const uno::Reference<uno::XComponentContext>& rContext)
: ClassificationCategoriesControllerBase(rContext, uno::Reference<frame::XFrame>(), OUString(".uno:ClassificationApply"))
, m_pClassification(nullptr)
, m_xListener(new comphelper::ConfigurationListener("/org.openoffice.Office.Paths/Paths/Classification"))
, m_aPropertyListener(m_xListener, *this)
{
}
OUString ClassificationCategoriesController::getImplementationName()
{
return OUString("com.sun.star.comp.sfx2.ClassificationCategoriesController");
}
sal_Bool ClassificationCategoriesController::supportsService(const OUString& rServiceName)
{
return cppu::supportsService(this, rServiceName);
}
uno::Sequence<OUString> ClassificationCategoriesController::getSupportedServiceNames()
{
uno::Sequence<OUString> aServices
{
"com.sun.star.frame.ToolbarController"
};
return aServices;
}
void ClassificationCategoriesController::dispose()
{
SolarMutexGuard aSolarMutexGuard;
svt::ToolboxController::dispose();
m_pClassification.disposeAndClear();
m_aPropertyListener.dispose();
m_xListener->dispose();
}
uno::Reference<awt::XWindow> ClassificationCategoriesController::createItemWindow(const uno::Reference<awt::XWindow>& rParent)
{
VclPtr<vcl::Window> pParent = VCLUnoHelper::GetWindow(rParent);
auto pToolbar = dynamic_cast<ToolBox*>(pParent.get());
if (pToolbar)
{
m_pClassification = VclPtr<ClassificationControl>::Create(pToolbar);
for (size_t i = m_pClassification->getLabelsSize(); i > 0; --i)
{
auto eType = static_cast<SfxClassificationPolicyType>(i);
m_pClassification->getCategories(eType)->SetSelectHdl(LINK(this, ClassificationCategoriesController, SelectHdl));
}
}
return uno::Reference<awt::XWindow>(VCLUnoHelper::GetInterface(m_pClassification));
}
IMPL_LINK(ClassificationCategoriesController, SelectHdl, ListBox&, rCategory, void)
{
OUString aEntry = rCategory.GetSelectEntry();
OUString aType = m_pClassification->getCategoryType(rCategory);
uno::Sequence<beans::PropertyValue> aPropertyValues(comphelper::InitPropertySequence(
{
{"Name", uno::makeAny(aEntry)},
{"Type", uno::makeAny(aType)},
}));
comphelper::dispatchCommand(".uno:ClassificationApply", aPropertyValues);
}
void ClassificationCategoriesController::statusChanged(const frame::FeatureStateEvent& /*rEvent*/)
{
if (!m_pClassification)
return;
SfxObjectShell* pObjectShell = SfxObjectShell::Current();
if (!pObjectShell)
return;
SfxClassificationHelper aHelper(pObjectShell->getDocProperties());
for (size_t i = m_pClassification->getLabelsSize(); i > 0; --i)
{
auto eType = static_cast<SfxClassificationPolicyType>(i);
VclPtr<ListBox> pCategories = m_pClassification->getCategories(eType);
if (pCategories->GetEntryCount() == 0)
{
std::vector<OUString> aNames = aHelper.GetBACNames();
for (const OUString& rName : aNames)
pCategories->InsertEntry(rName);
// Normally VclBuilder::makeObject() does this.
pCategories->EnableAutoSize(true);
}
}
// Restore state based on the doc. model.
for (size_t i = m_pClassification->getLabelsSize(); i > 0; --i)
{
auto eType = static_cast<SfxClassificationPolicyType>(i);
const OUString& rCategoryName = aHelper.GetBACName(eType);
if (!rCategoryName.isEmpty())
{
VclPtr<ListBox> pCategories = m_pClassification->getCategories(eType);
pCategories->SelectEntry(rCategoryName);
}
}
}
void ClassificationCategoriesController::removeEntries()
{
if (m_pClassification)
{
for (size_t i = m_pClassification->getLabelsSize(); i > 0; --i)
{
auto eType = static_cast<SfxClassificationPolicyType>(i);
VclPtr<ListBox> pCategories = m_pClassification->getCategories(eType);
pCategories->Clear();
}
}
}
// WB_NOLABEL means here that the control won't be replaced with a label
// when it wouldn't fit the available space.
ClassificationControl::ClassificationControl(vcl::Window* pParent)
: Window(pParent, WB_DIALOGCONTROL | WB_NOLABEL)
{
m_pLabels[SfxClassificationPolicyType::IntellectualProperty] = VclPtr<FixedText>::Create(this, WB_CENTER);
m_pLabels[SfxClassificationPolicyType::NationalSecurity] = VclPtr<FixedText>::Create(this, WB_CENTER);
m_pLabels[SfxClassificationPolicyType::ExportControl] = VclPtr<FixedText>::Create(this, WB_CENTER);
for (auto& rPair : m_pLabels)
{
OUString aText;
switch (rPair.first)
{
case SfxClassificationPolicyType::IntellectualProperty:
aText = SfxResId(STR_CLASSIFIED_INTELLECTUAL_PROPERTY);
break;
case SfxClassificationPolicyType::NationalSecurity:
aText = SfxResId(STR_CLASSIFIED_NATIONAL_SECURITY);
break;
case SfxClassificationPolicyType::ExportControl:
aText = SfxResId(STR_CLASSIFIED_EXPORT_CONTROL);
break;
}
auto& pLabel = rPair.second;
Size aTextSize(pLabel->GetTextWidth(aText), pLabel->GetTextHeight());
// Padding.
aTextSize.Width() += 12;
pLabel->SetText(aText);
pLabel->SetSizePixel(aTextSize);
pLabel->Show();
}
m_pCategories[SfxClassificationPolicyType::IntellectualProperty] = VclPtr<ListBox>::Create(this, WB_CLIPCHILDREN|WB_LEFT|WB_VCENTER|WB_3DLOOK|WB_DROPDOWN|WB_SIMPLEMODE);
m_pCategories[SfxClassificationPolicyType::NationalSecurity] = VclPtr<ListBox>::Create(this, WB_CLIPCHILDREN|WB_LEFT|WB_VCENTER|WB_3DLOOK|WB_DROPDOWN|WB_SIMPLEMODE);
m_pCategories[SfxClassificationPolicyType::ExportControl] = VclPtr<ListBox>::Create(this, WB_CLIPCHILDREN|WB_LEFT|WB_VCENTER|WB_3DLOOK|WB_DROPDOWN|WB_SIMPLEMODE);
for (auto& rPair : m_pCategories)
rPair.second->Show();
SetOptimalSize();
}
ClassificationControl::~ClassificationControl()
{
disposeOnce();
}
void ClassificationControl::dispose()
{
for (auto& rPair : m_pLabels)
rPair.second.disposeAndClear();
for (auto& rPair : m_pCategories)
rPair.second.disposeAndClear();
vcl::Window::dispose();
}
void ClassificationControl::Resize()
{
// Give the label what it wants, and the remaining size to the listbox.
Size aSize(GetOutputSizePixel());
long nXPosition = 0;
for (size_t i = m_pLabels.size(); i > 0; --i)
{
auto eType = static_cast<SfxClassificationPolicyType>(i);
auto& pLabel = m_pLabels[eType];
long nWLabel = pLabel->GetOutputSizePixel().Width();
long nW = aSize.Width() / m_pLabels.size();
long nH = aSize.Height();
long nPrefHeight = pLabel->get_preferred_size().Height();
long nOffset = (nH - nPrefHeight) / 2;
pLabel->SetPosSizePixel(Point(nXPosition, nOffset), Size(nWLabel, nPrefHeight));
auto& pCategories = m_pCategories[eType];
nPrefHeight = pCategories->get_preferred_size().Height();
nOffset = (nH - nPrefHeight) / 2;
pCategories->SetPosSizePixel(Point(nXPosition + nWLabel, nOffset), Size(nW - nWLabel, nPrefHeight));
nXPosition += nW;
}
}
const VclPtr<ListBox>& ClassificationControl::getCategories(SfxClassificationPolicyType eType)
{
return m_pCategories[eType];
}
std::size_t ClassificationControl::getLabelsSize()
{
return m_pLabels.size();
}
OUString ClassificationControl::getCategoryType(ListBox& rCategory)
{
OUString aRet;
auto it = std::find_if(m_pCategories.begin(), m_pCategories.end(), [&rCategory](const std::pair<SfxClassificationPolicyType, VclPtr<ListBox>>& rPair)
{
return rPair.second.get() == &rCategory;
});
if (it != m_pCategories.end())
aRet = SfxClassificationHelper::policyTypeToString(it->first);
return aRet;
}
void ClassificationControl::SetOptimalSize()
{
// Same as SvxColorDockingWindow.
const Size aLogicalAttrSize(150 * m_pLabels.size(), 0);
Size aSize(LogicToPixel(aLogicalAttrSize,MapUnit::MapAppFont));
auto& pLabel = m_pLabels[SfxClassificationPolicyType::IntellectualProperty];
auto& pCategories = m_pCategories[SfxClassificationPolicyType::IntellectualProperty];
Point aPosition = pCategories->GetPosPixel();
aSize.Height() = std::max(aSize.Height(), pLabel->get_preferred_size().Height());
aSize.Height() = std::max(aSize.Height(), pCategories->get_preferred_size().Height());
aSize.Width() = aPosition.X() + aSize.Width();
SetSizePixel(aSize);
}
void ClassificationControl::DataChanged(const DataChangedEvent& rEvent)
{
if ((rEvent.GetType() == DataChangedEventType::SETTINGS) && (rEvent.GetFlags() & AllSettingsFlags::STYLE))
SetOptimalSize();
Window::DataChanged(rEvent);
}
} // namespace sfx2
extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface* SAL_CALL com_sun_star_sfx2_ClassificationCategoriesController_get_implementation(uno::XComponentContext* pContext, const uno::Sequence<uno::Any>&)
{
return cppu::acquire(new sfx2::ClassificationCategoriesController(pContext));
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|