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 
25 #include <vcl/svapp.hxx>
26 #include <vcl/toolbox.hxx>
27 
28 #include <com/sun/star/lang/XServiceInfo.hpp>
29 
30 #include <strings.hrc>
31 #include <mmconfigitem.hxx>
32 #include <swmodule.hxx>
33 #include <view.hxx>
34 
35 using namespace css;
36 
37 namespace {
38 
39 class CurrentEdit final : public InterimItemWindow
40 {
41 private:
42  std::unique_ptr<weld::Entry> m_xWidget;
43 
44  DECL_LINK(KeyInputHdl, const KeyEvent&, bool);
45 public:
46  CurrentEdit(vcl::Window* pParent)
47  : InterimItemWindow(pParent, "modules/swriter/ui/editbox.ui", "EditBox")
48  , m_xWidget(m_xBuilder->weld_entry("entry"))
49  {
50  m_xWidget->connect_key_press(LINK(this, CurrentEdit, KeyInputHdl));
51  SetSizePixel(m_xWidget->get_preferred_size());
52  }
53 
54  virtual void dispose() override
55  {
56  m_xWidget.reset();
58  }
59 
60  virtual void GetFocus() override
61  {
62  if (m_xWidget)
63  m_xWidget->grab_focus();
65  }
66 
67  void set_sensitive(bool bSensitive)
68  {
69  Enable(bSensitive);
70  m_xWidget->set_sensitive(bSensitive);
71  }
72 
73  bool get_sensitive() const
74  {
75  return m_xWidget->get_sensitive();
76  }
77 
78  void set_text(const OUString& rText)
79  {
80  m_xWidget->set_text(rText);
81  }
82 
83  OUString get_text() const
84  {
85  return m_xWidget->get_text();
86  }
87 
88  void connect_activate(const Link<weld::Entry&, bool>& rLink)
89  {
90  m_xWidget->connect_activate(rLink);
91  }
92 
93  virtual ~CurrentEdit() override
94  {
95  disposeOnce();
96  }
97 };
98 
99 IMPL_LINK(CurrentEdit, KeyInputHdl, const KeyEvent&, rKEvt, bool)
100 {
101  return ChildKeyInput(rKEvt);
102 }
103 
105 class MMCurrentEntryController : public svt::ToolboxController, public lang::XServiceInfo
106 {
107  VclPtr<CurrentEdit> m_xCurrentEdit;
108 
109  DECL_LINK(CurrentEditUpdatedHdl, weld::Entry&, bool);
110 
111 public:
112  explicit MMCurrentEntryController(const uno::Reference<uno::XComponentContext>& rContext)
113  : svt::ToolboxController(rContext, uno::Reference<frame::XFrame>(), ".uno:MailMergeCurrentEntry")
114  , m_xCurrentEdit(nullptr)
115  {
116  }
117 
118  // XInterface
119  virtual uno::Any SAL_CALL queryInterface(const uno::Type& aType) override
120  {
121  uno::Any a(ToolboxController::queryInterface(aType));
122  if (a.hasValue())
123  return a;
124 
125  return ::cppu::queryInterface(aType, static_cast<lang::XServiceInfo*>(this));
126  }
127 
128  void SAL_CALL acquire() throw () override
129  {
130  ToolboxController::acquire();
131  }
132 
133  void SAL_CALL release() throw () override
134  {
135  ToolboxController::release();
136  }
137 
138  // XServiceInfo
139  virtual OUString SAL_CALL getImplementationName() override
140  {
141  return "lo.writer.MMCurrentEntryController";
142  }
143 
144  virtual sal_Bool SAL_CALL supportsService(const OUString& rServiceName) override
145  {
146  return cppu::supportsService(this, rServiceName);
147  }
148 
149  virtual uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
150  {
151  return { "com.sun.star.frame.ToolbarController" };
152  }
153 
154  // XComponent
155  virtual void SAL_CALL dispose() override;
156 
157  // XToolbarController
158  virtual uno::Reference<awt::XWindow> SAL_CALL createItemWindow(const uno::Reference<awt::XWindow>& rParent) override;
159 
160  // XStatusListener
161  virtual void SAL_CALL statusChanged(const frame::FeatureStateEvent& rEvent) override;
162 };
163 
164 class ExcludeCheckBox final : public InterimItemWindow
165 {
166 private:
167  std::unique_ptr<weld::CheckButton> m_xWidget;
168 
169  DECL_LINK(KeyInputHdl, const KeyEvent&, bool);
170 public:
171  ExcludeCheckBox(vcl::Window* pParent)
172  : InterimItemWindow(pParent, "modules/swriter/ui/checkbox.ui", "CheckBox")
173  , m_xWidget(m_xBuilder->weld_check_button("checkbutton"))
174  {
175  m_xWidget->set_label(SwResId(ST_EXCLUDE));
176  m_xWidget->connect_key_press(LINK(this, ExcludeCheckBox, KeyInputHdl));
177  SetSizePixel(m_xWidget->get_preferred_size());
178  }
179 
180  virtual void dispose() override
181  {
182  m_xWidget.reset();
184  }
185 
186  virtual void GetFocus() override
187  {
188  if (m_xWidget)
189  m_xWidget->grab_focus();
191  }
192 
193  void set_sensitive(bool bSensitive)
194  {
195  Enable(bSensitive);
196  m_xWidget->set_sensitive(bSensitive);
197  }
198 
199  void set_active(bool bActive)
200  {
201  m_xWidget->set_active(bActive);
202  }
203 
204  void connect_toggled(const Link<weld::ToggleButton&, void>& rLink)
205  {
206  m_xWidget->connect_toggled(rLink);
207  }
208 
209  virtual ~ExcludeCheckBox() override
210  {
211  disposeOnce();
212  }
213 };
214 
215 IMPL_LINK(ExcludeCheckBox, KeyInputHdl, const KeyEvent&, rKEvt, bool)
216 {
217  return ChildKeyInput(rKEvt);
218 }
219 
221 class MMExcludeEntryController : public svt::ToolboxController, public lang::XServiceInfo
222 {
223  VclPtr<ExcludeCheckBox> m_xExcludeCheckbox;
224 
225  DECL_STATIC_LINK(MMExcludeEntryController, ExcludeHdl, weld::ToggleButton&, void);
226 
227 public:
228  explicit MMExcludeEntryController(const uno::Reference<uno::XComponentContext>& rContext)
229  : svt::ToolboxController(rContext, uno::Reference<frame::XFrame>(), ".uno:MailMergeExcludeEntry")
230  , m_xExcludeCheckbox(nullptr)
231  {
232  }
233 
234  // XInterface
235  virtual uno::Any SAL_CALL queryInterface(const uno::Type& aType) override
236  {
237  uno::Any a(ToolboxController::queryInterface(aType));
238  if (a.hasValue())
239  return a;
240 
241  return ::cppu::queryInterface(aType, static_cast<lang::XServiceInfo*>(this));
242  }
243 
244  void SAL_CALL acquire() throw () override
245  {
246  ToolboxController::acquire();
247  }
248 
249  void SAL_CALL release() throw () override
250  {
251  ToolboxController::release();
252  }
253 
254  // XServiceInfo
255  virtual OUString SAL_CALL getImplementationName() override
256  {
257  return "lo.writer.MMExcludeEntryController";
258  }
259 
260  virtual sal_Bool SAL_CALL supportsService(const OUString& rServiceName) override
261  {
262  return cppu::supportsService(this, rServiceName);
263  }
264 
265  virtual uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override
266  {
267  return { "com.sun.star.frame.ToolbarController" };
268  }
269 
270  // XComponent
271  virtual void SAL_CALL dispose() override;
272 
273  // XToolbarController
274  virtual uno::Reference<awt::XWindow> SAL_CALL createItemWindow(const uno::Reference<awt::XWindow>& rParent) override;
275 
276  // XStatusListener
277  virtual void SAL_CALL statusChanged(const frame::FeatureStateEvent& rEvent) override;
278 };
279 
281 {
282  SolarMutexGuard aSolarMutexGuard;
283 
285  m_xCurrentEdit.disposeAndClear();
286 }
287 
288 uno::Reference<awt::XWindow> MMCurrentEntryController::createItemWindow(const uno::Reference<awt::XWindow>& rParent)
289 {
290  VclPtr<vcl::Window> pParent = VCLUnoHelper::GetWindow(rParent);
291  ToolBox* pToolbar = dynamic_cast<ToolBox*>(pParent.get());
292  if (pToolbar)
293  {
294  // make it visible
295  m_xCurrentEdit = VclPtr<CurrentEdit>::Create(pToolbar);
296  m_xCurrentEdit->connect_activate(LINK(this, MMCurrentEntryController, CurrentEditUpdatedHdl));
297  }
298 
299  return VCLUnoHelper::GetInterface(m_xCurrentEdit);
300 }
301 
302 IMPL_LINK(MMCurrentEntryController, CurrentEditUpdatedHdl, weld::Entry&, rEdit, bool)
303 {
304  SwView* pView = ::GetActiveView();
305  std::shared_ptr<SwMailMergeConfigItem> xConfigItem;
306  if (pView)
307  xConfigItem = pView->GetMailMergeConfigItem();
308 
309  if (!xConfigItem)
310  return true;
311 
312  OUString aText(rEdit.get_text());
313  sal_Int32 nEntry = aText.toInt32();
314  if (!aText.isEmpty() && nEntry != xConfigItem->GetResultSetPosition())
315  {
316  xConfigItem->MoveResultSet(nEntry);
317  // notify about the change
318  dispatchCommand(".uno:MailMergeCurrentEntry", uno::Sequence<beans::PropertyValue>());
319  }
320  return true;
321 };
322 
323 void MMCurrentEntryController::statusChanged(const frame::FeatureStateEvent& rEvent)
324 {
325  if (!m_xCurrentEdit)
326  return;
327 
328  SwView* pView = ::GetActiveView();
329  std::shared_ptr<SwMailMergeConfigItem> xConfigItem;
330  if (pView)
331  xConfigItem = pView->GetMailMergeConfigItem();
332 
333  if (!xConfigItem || !rEvent.IsEnabled)
334  {
335  m_xCurrentEdit->set_sensitive(false);
336  m_xCurrentEdit->set_text("");
337  }
338  else
339  {
340  sal_Int32 nEntry = m_xCurrentEdit->get_text().toInt32();
341  if (!m_xCurrentEdit->get_sensitive() || nEntry != xConfigItem->GetResultSetPosition())
342  {
343  m_xCurrentEdit->set_sensitive(true);
344  m_xCurrentEdit->set_text(OUString::number(xConfigItem->GetResultSetPosition()));
345  }
346  }
347 }
348 
350 {
351  SolarMutexGuard aSolarMutexGuard;
352 
354  m_xExcludeCheckbox.disposeAndClear();
355 }
356 
357 uno::Reference<awt::XWindow> MMExcludeEntryController::createItemWindow(const uno::Reference<awt::XWindow>& rParent)
358 {
359  VclPtr<vcl::Window> pParent = VCLUnoHelper::GetWindow(rParent);
360  ToolBox* pToolbar = dynamic_cast<ToolBox*>(pParent.get());
361  if (pToolbar)
362  {
363  // make it visible
364  m_xExcludeCheckbox = VclPtr<ExcludeCheckBox>::Create(pToolbar);
365  m_xExcludeCheckbox->connect_toggled(LINK(this, MMExcludeEntryController, ExcludeHdl));
366  }
367 
368  return VCLUnoHelper::GetInterface(m_xExcludeCheckbox);
369 }
370 
371 IMPL_STATIC_LINK(MMExcludeEntryController, ExcludeHdl, weld::ToggleButton&, rCheckbox, void)
372 {
373  SwView* pView = ::GetActiveView();
374  std::shared_ptr<SwMailMergeConfigItem> xConfigItem;
375  if (pView)
376  xConfigItem = pView->GetMailMergeConfigItem();
377 
378  if (xConfigItem)
379  xConfigItem->ExcludeRecord(xConfigItem->GetResultSetPosition(), rCheckbox.get_active());
380 };
381 
382 void MMExcludeEntryController::statusChanged(const frame::FeatureStateEvent& rEvent)
383 {
384  if (!m_xExcludeCheckbox)
385  return;
386 
387  SwView* pView = ::GetActiveView();
388  std::shared_ptr<SwMailMergeConfigItem> xConfigItem;
389  if (pView)
390  xConfigItem = pView->GetMailMergeConfigItem();
391 
392  if (!xConfigItem || !rEvent.IsEnabled)
393  {
394  m_xExcludeCheckbox->set_sensitive(false);
395  m_xExcludeCheckbox->set_active(false);
396  }
397  else
398  {
399  m_xExcludeCheckbox->set_sensitive(false);
400  m_xExcludeCheckbox->set_active(xConfigItem->IsRecordExcluded(xConfigItem->GetResultSetPosition()));
401  }
402 }
403 
404 }
405 
406 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface *
408  uno::XComponentContext *context,
409  uno::Sequence<uno::Any> const &)
410 {
411  return cppu::acquire(new MMCurrentEntryController(context));
412 }
413 
414 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface *
416  uno::XComponentContext *context,
417  uno::Sequence<uno::Any> const &)
418 {
419  return cppu::acquire(new MMExcludeEntryController(context));
420 }
421 
422 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
rEdit set_text(aStr)
virtual void SAL_CALL dispose() override
SwView * GetActiveView()
Definition: swmodul1.cxx:115
std::shared_ptr< SwMailMergeConfigItem > const & GetMailMergeConfigItem() const
Definition: view0.cxx:128
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)
Reference
OUString getImplementationName()
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)
virtual void GetFocus() override
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
IMPL_STATIC_LINK(SwObjectFactory, MakeObject, SdrObjCreatorParams, aParams, SdrObject *)
Definition: dobjfac.cxx:25
SAL_DLLPUBLIC_EXPORT uno::XInterface * lo_writer_MMCurrentEntryController_get_implementation(uno::XComponentContext *context, uno::Sequence< uno::Any > const &)
css::uno::Sequence< OUString > getSupportedServiceNames()
uno_Any a
unsigned char sal_Bool
OUString SwResId(const char *pId)
Definition: swmodule.cxx:178
IMPL_LINK(SwAccessibleDocument, WindowChildEventListener, VclWindowEvent &, rEvent, void)
Definition: accdoc.cxx:386
virtual OUString get_text() const =0
weld::Entry & rEdit
virtual void dispose() override
static VclPtr< reference_type > Create(Arg &&...arg)
vcl::Window * get() const
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