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