1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <tools/lineend.hxx>
22 #include <vcl/print.hxx>
23 #include <vcl/settings.hxx>
26 #include <viewopt.hxx>
34 #include <poolfmt.hxx>
37 #include <comphelper/string.hxx>
39 using namespace ::com::sun::star::lang
;
40 using namespace ::com::sun::star::container
;
41 using namespace ::com::sun::star::uno
;
42 using namespace ::com::sun::star
;
44 SwEnvPreview::SwEnvPreview()
49 void SwEnvPreview::SetDrawingArea(weld::DrawingArea
* pDrawingArea
)
51 CustomWidgetController::SetDrawingArea(pDrawingArea
);
52 pDrawingArea
->set_size_request(pDrawingArea
->get_approximate_digit_width() * 20,
53 pDrawingArea
->get_text_height() * 8);
56 void SwEnvPreview::Paint(vcl::RenderContext
& rRenderContext
, const tools::Rectangle
&)
58 const StyleSettings
& rSettings
= rRenderContext
.GetSettings().GetStyleSettings();
59 rRenderContext
.SetBackground(rRenderContext
.GetSettings().GetStyleSettings().GetDialogColor());
60 rRenderContext
.Erase();
62 const SwEnvItem
& rItem
= m_pDialog
->m_aEnvItem
;
64 const tools::Long nPageW
= std::max(rItem
.m_nWidth
, rItem
.m_nHeight
);
65 const tools::Long nPageH
= std::min(rItem
.m_nWidth
, rItem
.m_nHeight
);
67 Size
aSize(GetOutputSizePixel());
69 const double f
= 0.8 * std::min(
70 double(aSize
.Width()) / double(nPageW
),
71 double(aSize
.Height()) / double(nPageH
));
73 Color aBack
= rSettings
.GetWindowColor();
74 const Color
& rFront
= SwViewOption::GetCurrentViewOptions().GetFontColor();
75 Color
aMedium((aBack
.GetRed() + rFront
.GetRed()) / 2,
76 (aBack
.GetGreen() + rFront
.GetGreen()) / 2,
77 (aBack
.GetBlue() + rFront
.GetBlue()) / 2);
79 rRenderContext
.SetLineColor(rFront
);
82 const tools::Long nW
= static_cast<tools::Long
>(f
* nPageW
);
83 const tools::Long nH
= static_cast<tools::Long
>(f
* nPageH
);
84 const tools::Long nX
= (aSize
.Width () - nW
) / 2;
85 const tools::Long nY
= (aSize
.Height() - nH
) / 2;
86 rRenderContext
.SetFillColor(aBack
);
87 rRenderContext
.DrawRect(tools::Rectangle(Point(nX
, nY
), Size(nW
, nH
)));
92 const tools::Long nSendX
= nX
+ static_cast<tools::Long
>(f
* rItem
.m_nSendFromLeft
);
93 const tools::Long nSendY
= nY
+ static_cast<tools::Long
>(f
* rItem
.m_nSendFromTop
);
94 const tools::Long nSendW
= static_cast<tools::Long
>(f
* (rItem
.m_nAddrFromLeft
- rItem
.m_nSendFromLeft
));
95 const tools::Long nSendH
= static_cast<tools::Long
>(f
* (rItem
.m_nAddrFromTop
- rItem
.m_nSendFromTop
- 566));
96 rRenderContext
.SetFillColor(aMedium
);
98 rRenderContext
.DrawRect(tools::Rectangle(Point(nSendX
, nSendY
), Size(nSendW
, nSendH
)));
102 const tools::Long nAddrX
= nX
+ static_cast<tools::Long
>(f
* rItem
.m_nAddrFromLeft
);
103 const tools::Long nAddrY
= nY
+ static_cast<tools::Long
>(f
* rItem
.m_nAddrFromTop
);
104 const tools::Long nAddrW
= static_cast<tools::Long
>(f
* (nPageW
- rItem
.m_nAddrFromLeft
- 566));
105 const tools::Long nAddrH
= static_cast<tools::Long
>(f
* (nPageH
- rItem
.m_nAddrFromTop
- 566));
106 rRenderContext
.SetFillColor(aMedium
);
107 rRenderContext
.DrawRect(tools::Rectangle(Point(nAddrX
, nAddrY
), Size(nAddrW
, nAddrH
)));
110 const tools::Long nStmpW
= static_cast<tools::Long
>(f
* 1417 /* 2,5 cm */);
111 const tools::Long nStmpH
= static_cast<tools::Long
>(f
* 1701 /* 3,0 cm */);
112 const tools::Long nStmpX
= nX
+ nW
- static_cast<tools::Long
>(f
* 566) - nStmpW
;
113 const tools::Long nStmpY
= nY
+ static_cast<tools::Long
>(f
* 566);
115 rRenderContext
.SetFillColor(aBack
);
116 rRenderContext
.DrawRect(tools::Rectangle(Point(nStmpX
, nStmpY
), Size(nStmpW
, nStmpH
)));
119 SwEnvDlg::SwEnvDlg(weld::Window
* pParent
, const SfxItemSet
& rSet
,
120 SwWrtShell
* pWrtSh
, Printer
* pPrt
, bool bInsert
)
121 : SfxTabDialogController(pParent
, "modules/swriter/ui/envdialog.ui", "EnvDialog", &rSet
)
122 , m_aEnvItem(static_cast<const SwEnvItem
&>( rSet
.Get(FN_ENVELOP
)))
125 , m_xModify(m_xBuilder
->weld_button("modify"))
129 GetUserButton()->set_label(m_xModify
->get_label());
132 AddTabPage("envelope", SwEnvPage::Create
, nullptr);
133 AddTabPage("format", SwEnvFormatPage::Create
, nullptr);
134 AddTabPage("printer", SwEnvPrtPage::Create
, nullptr);
137 SwEnvDlg::~SwEnvDlg()
139 m_pAddresseeSet
.reset();
140 m_pSenderSet
.reset();
143 void SwEnvDlg::PageCreated(const OUString
& rId
, SfxTabPage
&rPage
)
145 if (rId
== "printer")
147 static_cast<SwEnvPrtPage
*>(&rPage
)->SetPrt(m_pPrinter
);
149 else if (rId
== "envelope")
151 static_cast<SwEnvPage
*>(&rPage
)->Init(this);
153 else if (rId
== "format")
155 static_cast<SwEnvFormatPage
*>(&rPage
)->Init(this);
161 short nRet
= SfxTabDialogController::Ok();
163 if (nRet
== RET_OK
|| nRet
== RET_USER
)
167 SwTextFormatColl
* pColl
= m_pSh
->GetTextCollFromPool(RES_POOLCOLL_ENVELOPE_ADDRESS
);
168 pColl
->SetFormatAttr(*m_pAddresseeSet
);
172 SwTextFormatColl
* pColl
= m_pSh
->GetTextCollFromPool(RES_POOLCOLL_SEND_ADDRESS
);
173 pColl
->SetFormatAttr(*m_pSenderSet
);
180 SwEnvPage::SwEnvPage(weld::Container
* pPage
, weld::DialogController
* pController
, const SfxItemSet
& rSet
)
181 : SfxTabPage(pPage
, pController
, "modules/swriter/ui/envaddresspage.ui", "EnvAddressPage", &rSet
)
184 , m_xAddrEdit(m_xBuilder
->weld_text_view("addredit"))
185 , m_xDatabaseLB(m_xBuilder
->weld_combo_box("database"))
186 , m_xTableLB(m_xBuilder
->weld_combo_box("table"))
187 , m_xDBFieldLB(m_xBuilder
->weld_combo_box("field"))
188 , m_xInsertBT(m_xBuilder
->weld_button("insert"))
189 , m_xSenderBox(m_xBuilder
->weld_check_button("sender"))
190 , m_xSenderEdit(m_xBuilder
->weld_text_view("senderedit"))
191 , m_xPreview(new weld::CustomWeld(*m_xBuilder
, "preview", m_aPreview
))
193 auto nTextBoxHeight(m_xAddrEdit
->get_height_rows(10));
194 auto nTextBoxWidth(m_xAddrEdit
->get_approximate_digit_width() * 25);
196 m_xAddrEdit
->set_size_request(nTextBoxWidth
, nTextBoxHeight
);
197 m_xSenderEdit
->set_size_request(nTextBoxWidth
, nTextBoxHeight
);
199 auto nListBoxWidth
= m_xTableLB
->get_approximate_digit_width() * 25;
200 m_xTableLB
->set_size_request(nListBoxWidth
, -1);
201 m_xDatabaseLB
->set_size_request(nListBoxWidth
, -1);
202 m_xDBFieldLB
->set_size_request(nListBoxWidth
, -1);
204 SetExchangeSupport();
207 void SwEnvPage::Init(SwEnvDlg
* pDialog
)
210 m_pSh
= m_pDialog
->m_pSh
;
211 m_aPreview
.SetDialog(pDialog
);
214 m_xDatabaseLB
->connect_changed(LINK(this, SwEnvPage
, DatabaseHdl
));
215 m_xTableLB
->connect_changed(LINK(this, SwEnvPage
, DatabaseHdl
));
216 m_xInsertBT
->connect_clicked(LINK(this, SwEnvPage
, FieldHdl
));
217 m_xSenderBox
->connect_toggled(LINK(this, SwEnvPage
, SenderHdl
));
219 SwDBData aData
= m_pSh
->GetDBData();
220 m_sActDBName
= aData
.sDataSource
+ OUStringChar(DB_DELIM
) + aData
.sCommand
;
224 SwEnvPage::~SwEnvPage()
228 IMPL_LINK( SwEnvPage
, DatabaseHdl
, weld::ComboBox
&, rListBox
, void )
230 SwWait
aWait( *m_pSh
->GetView().GetDocShell(), true );
232 if (&rListBox
== m_xDatabaseLB
.get())
234 m_sActDBName
= rListBox
.get_active_text();
235 m_pSh
->GetDBManager()->GetTableNames(*m_xTableLB
, m_sActDBName
);
236 m_sActDBName
+= OUStringChar(DB_DELIM
);
240 m_sActDBName
= comphelper::string::setToken(m_sActDBName
, 1, DB_DELIM
, m_xTableLB
->get_active_text());
242 m_pSh
->GetDBManager()->GetColumnNames(*m_xDBFieldLB
, m_xDatabaseLB
->get_active_text(),
243 m_xTableLB
->get_active_text());
246 IMPL_LINK_NOARG(SwEnvPage
, FieldHdl
, weld::Button
&, void)
248 OUString
aStr("<" + m_xDatabaseLB
->get_active_text() + "." +
249 m_xTableLB
->get_active_text() + "." +
250 m_xTableLB
->get_active_id() + "." +
251 m_xDBFieldLB
->get_active_text() + ">");
252 m_xAddrEdit
->replace_selection(aStr
);
253 int nStartPos
, nEndPos
;
254 m_xAddrEdit
->get_selection_bounds(nStartPos
, nEndPos
);
255 m_xAddrEdit
->grab_focus();
256 m_xAddrEdit
->select_region(nStartPos
, nEndPos
);
259 IMPL_LINK_NOARG(SwEnvPage
, SenderHdl
, weld::Toggleable
&, void)
261 const bool bEnable
= m_xSenderBox
->get_active();
262 GetParentSwEnvDlg()->m_aEnvItem
.m_bSend
= bEnable
;
263 m_xSenderEdit
->set_sensitive(bEnable
);
266 m_xSenderEdit
->grab_focus();
267 if (m_xSenderEdit
->get_text().isEmpty())
268 m_xSenderEdit
->set_text(MakeSender());
270 m_xPreview
->queue_draw();
273 void SwEnvPage::InitDatabaseBox()
275 if (!m_pSh
->GetDBManager())
278 m_xDatabaseLB
->clear();
279 const Sequence
<OUString
> aDataNames
= SwDBManager::GetExistingDatabaseNames();
281 for (const OUString
& rDataName
: aDataNames
)
282 m_xDatabaseLB
->append_text(rDataName
);
285 OUString sDBName
= m_sActDBName
.getToken( 0, DB_DELIM
, nIdx
);
286 OUString sTableName
= m_sActDBName
.getToken( 0, DB_DELIM
, nIdx
);
287 m_xDatabaseLB
->set_active_text(sDBName
);
288 if (m_pSh
->GetDBManager()->GetTableNames(*m_xTableLB
, sDBName
))
290 m_xTableLB
->append_text(sTableName
);
291 m_pSh
->GetDBManager()->GetColumnNames(*m_xDBFieldLB
, sDBName
, sTableName
);
294 m_xDBFieldLB
->clear();
297 std::unique_ptr
<SfxTabPage
> SwEnvPage::Create(weld::Container
* pPage
, weld::DialogController
* pController
, const SfxItemSet
* rSet
)
299 return std::make_unique
<SwEnvPage
>(pPage
, pController
, *rSet
);
302 void SwEnvPage::ActivatePage(const SfxItemSet
& rSet
)
304 SfxItemSet
aSet(rSet
);
305 aSet
.Put(GetParentSwEnvDlg()->m_aEnvItem
);
309 DeactivateRC
SwEnvPage::DeactivatePage(SfxItemSet
* _pSet
)
311 FillItem(GetParentSwEnvDlg()->m_aEnvItem
);
314 return DeactivateRC::LeavePage
;
317 void SwEnvPage::FillItem(SwEnvItem
& rItem
)
319 rItem
.m_aAddrText
= m_xAddrEdit
->get_text();
320 rItem
.m_bSend
= m_xSenderBox
->get_active();
321 rItem
.m_aSendText
= m_xSenderEdit
->get_text();
324 bool SwEnvPage::FillItemSet(SfxItemSet
* rSet
)
326 FillItem(GetParentSwEnvDlg()->m_aEnvItem
);
327 rSet
->Put(GetParentSwEnvDlg()->m_aEnvItem
);
331 void SwEnvPage::Reset(const SfxItemSet
* rSet
)
333 SwEnvItem aItem
= static_cast<const SwEnvItem
&>( rSet
->Get(FN_ENVELOP
));
334 m_xAddrEdit
->set_text(convertLineEnd(aItem
.m_aAddrText
, GetSystemLineEnd()));
335 m_xSenderEdit
->set_text(convertLineEnd(aItem
.m_aSendText
, GetSystemLineEnd()));
336 m_xSenderBox
->set_active(aItem
.m_bSend
);
337 SenderHdl(*m_xSenderBox
);
340 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */