LibreOffice Module sw (master)  1
DateFormFieldButton.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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 
10 #include <DateFormFieldButton.hxx>
11 #include <edtwin.hxx>
12 #include <bookmrk.hxx>
13 #include <vcl/floatwin.hxx>
15 #include <tools/date.hxx>
16 #include <svl/zforlist.hxx>
17 
18 namespace
19 {
20 class SwCalendarBox final : public InterimItemWindow
21 {
22 private:
23  std::unique_ptr<weld::Calendar> m_xCalendar;
24 
25 public:
26  SwCalendarBox(vcl::Window* pParent)
27  : InterimItemWindow(pParent, "modules/swriter/ui/calendar.ui", "Calendar")
28  , m_xCalendar(m_xBuilder->weld_calendar("date"))
29  {
30  }
31  weld::Calendar& get_widget() { return *m_xCalendar; }
32  virtual ~SwCalendarBox() override { disposeOnce(); }
33  virtual void dispose() override
34  {
35  m_xCalendar.reset();
37  }
38 };
39 
40 class SwDatePickerDialog : public FloatingWindow
41 {
42 private:
43  VclPtr<SwCalendarBox> m_xCalendar;
45  SvNumberFormatter* m_pNumberFormatter;
46 
47  DECL_LINK(ImplSelectHdl, weld::Calendar&, void);
48 
49 public:
50  SwDatePickerDialog(SwEditWin* parent, sw::mark::DateFieldmark* pFieldmark,
51  SvNumberFormatter* pNumberFormatter);
52  virtual ~SwDatePickerDialog() override;
53  virtual void dispose() override;
54 };
55 }
56 
57 SwDatePickerDialog::SwDatePickerDialog(SwEditWin* parent, sw::mark::DateFieldmark* pFieldmark,
58  SvNumberFormatter* pNumberFormatter)
60  , m_xCalendar(VclPtr<SwCalendarBox>::Create(this))
61  , m_pFieldmark(pFieldmark)
62  , m_pNumberFormatter(pNumberFormatter)
63 {
64  weld::Calendar& rCalendar = m_xCalendar->get_widget();
65 
66  if (m_pFieldmark != nullptr)
67  {
68  std::pair<bool, double> aResult = m_pFieldmark->GetCurrentDate();
69  if (aResult.first)
70  {
71  const Date& rNullDate = m_pNumberFormatter->GetNullDate();
72  rCalendar.set_date(rNullDate + sal_Int32(aResult.second));
73  }
74  }
75 
76  Size lbSize(rCalendar.get_preferred_size());
77 
78  m_xCalendar->SetSizePixel(lbSize);
79  rCalendar.connect_activated(LINK(this, SwDatePickerDialog, ImplSelectHdl));
80  m_xCalendar->Show();
81 
82  rCalendar.grab_focus();
83 
84  SetSizePixel(lbSize);
85 }
86 
87 SwDatePickerDialog::~SwDatePickerDialog() { disposeOnce(); }
88 
90 {
91  m_xCalendar.disposeAndClear();
93 }
94 
95 IMPL_LINK(SwDatePickerDialog, ImplSelectHdl, weld::Calendar&, rCalendar, void)
96 {
97  if (m_pFieldmark != nullptr)
98  {
99  const Date& rNullDate = m_pNumberFormatter->GetNullDate();
100  double dDate = rCalendar.get_date() - rNullDate;
101  m_pFieldmark->SetCurrentDate(dDate);
102  }
103  EndPopupMode();
104 }
105 
107  SvNumberFormatter* pNumberFormatter)
108  : FormFieldButton(pEditWin, rFieldmark)
109  , m_pNumberFormatter(pNumberFormatter)
110 {
111 }
112 
114 
116 {
117  sw::mark::DateFieldmark* pDateFieldmark = dynamic_cast<sw::mark::DateFieldmark*>(&m_rFieldmark);
118  m_pFieldPopup = VclPtr<SwDatePickerDialog>::Create(static_cast<SwEditWin*>(GetParent()),
119  pDateFieldmark, m_pNumberFormatter);
120 }
121 
122 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
Fieldmark representing a date form field.
Definition: bookmrk.hxx:304
WinBits const WB_NOSHADOW
WinBits const WB_SYSTEMWINDOW
DateFormFieldButton(SwEditWin *pEditWin, sw::mark::DateFieldmark &rFieldMark, SvNumberFormatter *pNumberFormatter)
sw::mark::Fieldmark & m_rFieldmark
void Create(SwFormatVertOrient &rItem, SvStream &rStrm, sal_uInt16 nVersionAbusedAsSize)
Definition: legacyitem.cxx:32
virtual void SetSizePixel(const Size &rNewSize)
VclPtr< FloatingWindow > m_pFieldPopup
virtual Date get_date() const =0
virtual void set_date(const Date &rDate)=0
This button is shown when the cursor is on a form field with drop-down capability.
Window class for the Writer edit area, this is the one handling mouse and keyboard events and doing t...
Definition: edtwin.hxx:58
std::unique_ptr< Fieldmark > m_pFieldmark
Definition: docbm.cxx:1204
vcl::Window * GetParent() const
virtual Size get_preferred_size() const =0
IMPL_LINK(SwDatePickerDialog, ImplSelectHdl, weld::Calendar &, rCalendar, void)
SvNumberFormatter * m_pNumberFormatter
WinBits const WB_BORDER
virtual void InitPopup() override
virtual void grab_focus()=0
virtual void dispose() override
static VclPtr< reference_type > Create(Arg &&...arg)
void connect_activated(const Link< Calendar &, void > &rLink)
virtual ~DateFormFieldButton() override
virtual void dispose() override