LibreOffice Module sw (master)  1
mailmergetoolbarcontrols.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
24 #include <vcl/button.hxx>
25 #include <vcl/edit.hxx>
26 #include <vcl/svapp.hxx>
27 #include <vcl/toolbox.hxx>
28 
29 #include <com/sun/star/lang/XServiceInfo.hpp>
30 
31 #include <dbui.hrc>
32 #include <strings.hrc>
33 #include <mmconfigitem.hxx>
34 #include <swmodule.hxx>
35 #include <view.hxx>
36 
37 using namespace css;
38 
39 namespace {
40 
42 class MMCurrentEntryController : public svt::ToolboxController, public lang::XServiceInfo
43 {
44  VclPtr<Edit> m_pCurrentEdit;
45 
46  DECL_LINK(CurrentEditUpdatedHdl, Edit&, void);
47 
48 public:
49  explicit MMCurrentEntryController(const uno::Reference<uno::XComponentContext>& rContext)
50  : svt::ToolboxController(rContext, uno::Reference<frame::XFrame>(), ".uno:MailMergeCurrentEntry")
51  , m_pCurrentEdit(nullptr)
52  {
53  }
54 
55  // XInterface
56  virtual uno::Any SAL_CALL queryInterface(const uno::Type& aType) override
57  {
58  uno::Any a(ToolboxController::queryInterface(aType));
59  if (a.hasValue())
60  return a;
61 
62  return ::cppu::queryInterface(aType, static_cast<lang::XServiceInfo*>(this));
63  }
64 
65  void SAL_CALL acquire() throw () override
66  {
67  ToolboxController::acquire();
68  }
69 
70  void SAL_CALL release() throw () override
71  {
72  ToolboxController::release();
73  }
74 
75  // XServiceInfo
76  virtual OUString SAL_CALL getImplementationName() override
77  {
78  return "lo.writer.MMCurrentEntryController";
79  }
80 
81  virtual sal_Bool SAL_CALL supportsService(const OUString& rServiceName) override
82  {
83  return cppu::supportsService(this, rServiceName);
84  }
85 
86  virtual uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
87  {
88  uno::Sequence<OUString> aServices { "com.sun.star.frame.ToolbarController" };
89  return aServices;
90  }
91 
92  // XComponent
93  virtual void SAL_CALL dispose() override;
94 
95  // XToolbarController
96  virtual uno::Reference<awt::XWindow> SAL_CALL createItemWindow(const uno::Reference<awt::XWindow>& rParent) override;
97 
98  // XStatusListener
99  virtual void SAL_CALL statusChanged(const frame::FeatureStateEvent& rEvent) override;
100 };
101 
103 class MMExcludeEntryController : public svt::ToolboxController, public lang::XServiceInfo
104 {
105  VclPtr<CheckBox> m_pExcludeCheckbox;
106 
107  DECL_STATIC_LINK(MMExcludeEntryController, ExcludeHdl, CheckBox&, void);
108 
109 public:
110  explicit MMExcludeEntryController(const uno::Reference<uno::XComponentContext>& rContext)
111  : svt::ToolboxController(rContext, uno::Reference<frame::XFrame>(), ".uno:MailMergeExcludeEntry")
112  , m_pExcludeCheckbox(nullptr)
113  {
114  }
115 
116  // XInterface
117  virtual uno::Any SAL_CALL queryInterface(const uno::Type& aType) override
118  {
119  uno::Any a(ToolboxController::queryInterface(aType));
120  if (a.hasValue())
121  return a;
122 
123  return ::cppu::queryInterface(aType, static_cast<lang::XServiceInfo*>(this));
124  }
125 
126  void SAL_CALL acquire() throw () override
127  {
128  ToolboxController::acquire();
129  }
130 
131  void SAL_CALL release() throw () override
132  {
133  ToolboxController::release();
134  }
135 
136  // XServiceInfo
137  virtual OUString SAL_CALL getImplementationName() override
138  {
139  return "lo.writer.MMExcludeEntryController";
140  }
141 
142  virtual sal_Bool SAL_CALL supportsService(const OUString& rServiceName) override
143  {
144  return cppu::supportsService(this, rServiceName);
145  }
146 
147  virtual uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
148  {
149  uno::Sequence<OUString> aServices { "com.sun.star.frame.ToolbarController" };
150  return aServices;
151  }
152 
153  // XComponent
154  virtual void SAL_CALL dispose() override;
155 
156  // XToolbarController
157  virtual uno::Reference<awt::XWindow> SAL_CALL createItemWindow(const uno::Reference<awt::XWindow>& rParent) override;
158 
159  // XStatusListener
160  virtual void SAL_CALL statusChanged(const frame::FeatureStateEvent& rEvent) override;
161 };
162 
164 {
165  SolarMutexGuard aSolarMutexGuard;
166 
168  m_pCurrentEdit.disposeAndClear();
169 }
170 
171 uno::Reference<awt::XWindow> MMCurrentEntryController::createItemWindow(const uno::Reference<awt::XWindow>& rParent)
172 {
173  VclPtr<vcl::Window> pParent = VCLUnoHelper::GetWindow(rParent);
174  ToolBox* pToolbar = dynamic_cast<ToolBox*>(pParent.get());
175  if (pToolbar)
176  {
177  // make it visible
178  m_pCurrentEdit = VclPtr<Edit>::Create(pToolbar);
179  m_pCurrentEdit->SetWidthInChars(4);
180  m_pCurrentEdit->SetSizePixel(m_pCurrentEdit->GetOptimalSize());
181 
182  m_pCurrentEdit->SetModifyHdl(LINK(this, MMCurrentEntryController, CurrentEditUpdatedHdl));
183  }
184 
185  return VCLUnoHelper::GetInterface(m_pCurrentEdit);
186 }
187 
188 IMPL_LINK(MMCurrentEntryController, CurrentEditUpdatedHdl, Edit&, rEdit, void)
189 {
190  SwView* pView = ::GetActiveView();
191  std::shared_ptr<SwMailMergeConfigItem> xConfigItem;
192  if (pView)
193  xConfigItem = pView->GetMailMergeConfigItem();
194 
195  if (!xConfigItem)
196  return;
197 
198  OUString aText(rEdit.GetText());
199  sal_Int32 nEntry = aText.toInt32();
200  if (!aText.isEmpty() && nEntry != xConfigItem->GetResultSetPosition())
201  {
202  xConfigItem->MoveResultSet(nEntry);
203  // notify about the change
204  dispatchCommand(".uno:MailMergeCurrentEntry", uno::Sequence<beans::PropertyValue>());
205  }
206 };
207 
208 void MMCurrentEntryController::statusChanged(const frame::FeatureStateEvent& rEvent)
209 {
210  if (!m_pCurrentEdit)
211  return;
212 
213  SwView* pView = ::GetActiveView();
214  std::shared_ptr<SwMailMergeConfigItem> xConfigItem;
215  if (pView)
216  xConfigItem = pView->GetMailMergeConfigItem();
217 
218  if (!xConfigItem || !rEvent.IsEnabled)
219  {
220  m_pCurrentEdit->Disable();
221  m_pCurrentEdit->SetText("");
222  }
223  else
224  {
225  sal_Int32 nEntry = m_pCurrentEdit->GetText().toInt32();
226  if (!m_pCurrentEdit->IsEnabled() || nEntry != xConfigItem->GetResultSetPosition())
227  {
228  m_pCurrentEdit->Enable();
229  m_pCurrentEdit->SetText(OUString::number(xConfigItem->GetResultSetPosition()));
230  }
231  }
232 }
233 
235 {
236  SolarMutexGuard aSolarMutexGuard;
237 
239  m_pExcludeCheckbox.disposeAndClear();
240 }
241 
242 uno::Reference<awt::XWindow> MMExcludeEntryController::createItemWindow(const uno::Reference<awt::XWindow>& rParent)
243 {
244  VclPtr<vcl::Window> pParent = VCLUnoHelper::GetWindow(rParent);
245  ToolBox* pToolbar = dynamic_cast<ToolBox*>(pParent.get());
246  if (pToolbar)
247  {
248  // make it visible
249  m_pExcludeCheckbox = VclPtr<CheckBox>::Create(pToolbar);
250  m_pExcludeCheckbox->SetText(SwResId(ST_EXCLUDE));
251  m_pExcludeCheckbox->SetSizePixel(m_pExcludeCheckbox->GetOptimalSize());
252 
253  m_pExcludeCheckbox->SetToggleHdl(LINK(this, MMExcludeEntryController, ExcludeHdl));
254  }
255 
256  return VCLUnoHelper::GetInterface(m_pExcludeCheckbox);
257 }
258 
259 IMPL_STATIC_LINK(MMExcludeEntryController, ExcludeHdl, CheckBox&, rCheckbox, void)
260 {
261  SwView* pView = ::GetActiveView();
262  std::shared_ptr<SwMailMergeConfigItem> xConfigItem;
263  if (pView)
264  xConfigItem = pView->GetMailMergeConfigItem();
265 
266  if (xConfigItem)
267  xConfigItem->ExcludeRecord(xConfigItem->GetResultSetPosition(), rCheckbox.IsChecked());
268 };
269 
270 void MMExcludeEntryController::statusChanged(const frame::FeatureStateEvent& rEvent)
271 {
272  if (!m_pExcludeCheckbox)
273  return;
274 
275  SwView* pView = ::GetActiveView();
276  std::shared_ptr<SwMailMergeConfigItem> xConfigItem;
277  if (pView)
278  xConfigItem = pView->GetMailMergeConfigItem();
279 
280  if (!xConfigItem || !rEvent.IsEnabled)
281  {
282  m_pExcludeCheckbox->Disable();
283  m_pExcludeCheckbox->Check(false);
284  }
285  else
286  {
287  m_pExcludeCheckbox->Enable();
288  m_pExcludeCheckbox->Check(xConfigItem->IsRecordExcluded(xConfigItem->GetResultSetPosition()));
289  }
290 }
291 
292 }
293 
294 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface *
296  uno::XComponentContext *context,
297  uno::Sequence<uno::Any> const &)
298 {
299  return cppu::acquire(new MMCurrentEntryController(context));
300 }
301 
302 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface *
304  uno::XComponentContext *context,
305  uno::Sequence<uno::Any> const &)
306 {
307  return cppu::acquire(new MMExcludeEntryController(context));
308 }
309 
310 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual Size GetOptimalSize() const override
virtual void SAL_CALL dispose() override
SwView * GetActiveView()
Definition: swmodul1.cxx:123
std::shared_ptr< SwMailMergeConfigItem > const & GetMailMergeConfigItem() const
Definition: view0.cxx:139
SAL_DLLPUBLIC_EXPORT uno::XInterface * lo_writer_MMExcludeEntryController_get_implementation(uno::XComponentContext *context, uno::Sequence< uno::Any > const &)
void disposeAndClear()
static VclPtr< vcl::Window > GetWindow(const css::uno::Reference< css::awt::XWindow > &rxWindow)
virtual void SetSizePixel(const Size &rNewSize)
Reference
OUString getImplementationName()
virtual void SetModifyHdl(const Link< Edit &, void > &rLink)
void Enable(bool bEnable=true, bool bChild=true)
bool dispatchCommand(const OUString &rCommand, const uno::Reference< css::frame::XFrame > &rFrame, const css::uno::Sequence< css::beans::PropertyValue > &rArguments, const uno::Reference< css::frame::XDispatchResultListener > &rListener)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
IMPL_STATIC_LINK(SwObjectFactory, MakeObject, SdrObjCreatorParams, aParams, SdrObject *)
Definition: dobjfac.cxx:26
SAL_DLLPUBLIC_EXPORT uno::XInterface * lo_writer_MMCurrentEntryController_get_implementation(uno::XComponentContext *context, uno::Sequence< uno::Any > const &)
css::uno::Sequence< OUString > getSupportedServiceNames()
virtual Size GetOptimalSize() const override
virtual void SetText(const OUString &rStr) override
virtual OUString GetText() const override
unsigned char sal_Bool
void SetToggleHdl(const Link< CheckBox &, void > &rLink)
OUString SwResId(const char *pId)
Definition: swmodule.cxx:191
void Check(bool bCheck=true)
IMPL_LINK(SwAccessibleDocument, WindowChildEventListener, VclWindowEvent &, rEvent, void)
Definition: accdoc.cxx:385
void Disable(bool bChild=true)
void SetWidthInChars(sal_Int32 nWidthInChars)
weld::Entry & rEdit
static VclPtr< reference_type > Create(Arg &&...arg)
vcl::Window * get() const
bool IsEnabled() const
virtual void SetText(const OUString &rStr) override
static css::uno::Reference< css::awt::XWindow > GetInterface(vcl::Window *pWindow)
css::uno::Any SAL_CALL queryInterface(const css::uno::Type &rType, Interface1 *p1)
Definition: view.hxx:146