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::uno
;
40 using namespace ::com::sun::star
;
42 SwEnvPreview::SwEnvPreview()
47 void SwEnvPreview::SetDrawingArea(weld::DrawingArea
* pDrawingArea
)
49 CustomWidgetController::SetDrawingArea(pDrawingArea
);
50 pDrawingArea
->set_size_request(pDrawingArea
->get_approximate_digit_width() * 20,
51 pDrawingArea
->get_text_height() * 8);
54 void SwEnvPreview::Paint(vcl::RenderContext
& rRenderContext
, const tools::Rectangle
&)
56 const StyleSettings
& rSettings
= rRenderContext
.GetSettings().GetStyleSettings();
57 rRenderContext
.SetBackground(rRenderContext
.GetSettings().GetStyleSettings().GetDialogColor());
58 rRenderContext
.Erase();
60 const SwEnvItem
& rItem
= m_pDialog
->m_aEnvItem
;
62 const tools::Long nPageW
= std::max(rItem
.m_nWidth
, rItem
.m_nHeight
);
63 const tools::Long nPageH
= std::min(rItem
.m_nWidth
, rItem
.m_nHeight
);
65 Size
aSize(GetOutputSizePixel());
67 const double f
= 0.8 * std::min(
68 double(aSize
.Width()) / double(nPageW
),
69 double(aSize
.Height()) / double(nPageH
));
71 Color aBack
= rSettings
.GetWindowColor();
72 const Color
& rFront
= SwViewOption::GetCurrentViewOptions().GetFontColor();
73 Color
aMedium((aBack
.GetRed() + rFront
.GetRed()) / 2,
74 (aBack
.GetGreen() + rFront
.GetGreen()) / 2,
75 (aBack
.GetBlue() + rFront
.GetBlue()) / 2);
77 rRenderContext
.SetLineColor(rFront
);
80 const tools::Long nW
= static_cast<tools::Long
>(f
* nPageW
);
81 const tools::Long nH
= static_cast<tools::Long
>(f
* nPageH
);
82 const tools::Long nX
= (aSize
.Width () - nW
) / 2;
83 const tools::Long nY
= (aSize
.Height() - nH
) / 2;
84 rRenderContext
.SetFillColor(aBack
);
85 rRenderContext
.DrawRect(tools::Rectangle(Point(nX
, nY
), Size(nW
, nH
)));
90 const tools::Long nSendX
= nX
+ static_cast<tools::Long
>(f
* rItem
.m_nSendFromLeft
);
91 const tools::Long nSendY
= nY
+ static_cast<tools::Long
>(f
* rItem
.m_nSendFromTop
);
92 const tools::Long nSendW
= static_cast<tools::Long
>(f
* (rItem
.m_nAddrFromLeft
- rItem
.m_nSendFromLeft
));
93 const tools::Long nSendH
= static_cast<tools::Long
>(f
* (rItem
.m_nAddrFromTop
- rItem
.m_nSendFromTop
- 566));
94 rRenderContext
.SetFillColor(aMedium
);
96 rRenderContext
.DrawRect(tools::Rectangle(Point(nSendX
, nSendY
), Size(nSendW
, nSendH
)));
100 const tools::Long nAddrX
= nX
+ static_cast<tools::Long
>(f
* rItem
.m_nAddrFromLeft
);
101 const tools::Long nAddrY
= nY
+ static_cast<tools::Long
>(f
* rItem
.m_nAddrFromTop
);
102 const tools::Long nAddrW
= static_cast<tools::Long
>(f
* (nPageW
- rItem
.m_nAddrFromLeft
- 566));
103 const tools::Long nAddrH
= static_cast<tools::Long
>(f
* (nPageH
- rItem
.m_nAddrFromTop
- 566));
104 rRenderContext
.SetFillColor(aMedium
);
105 rRenderContext
.DrawRect(tools::Rectangle(Point(nAddrX
, nAddrY
), Size(nAddrW
, nAddrH
)));
108 const tools::Long nStmpW
= static_cast<tools::Long
>(f
* 1417 /* 2,5 cm */);
109 const tools::Long nStmpH
= static_cast<tools::Long
>(f
* 1701 /* 3,0 cm */);
110 const tools::Long nStmpX
= nX
+ nW
- static_cast<tools::Long
>(f
* 566) - nStmpW
;
111 const tools::Long nStmpY
= nY
+ static_cast<tools::Long
>(f
* 566);
113 rRenderContext
.SetFillColor(aBack
);
114 rRenderContext
.DrawRect(tools::Rectangle(Point(nStmpX
, nStmpY
), Size(nStmpW
, nStmpH
)));
117 SwEnvDlg::SwEnvDlg(weld::Window
* pParent
, const SfxItemSet
& rSet
,
118 SwWrtShell
* pWrtSh
, Printer
* pPrt
, bool bInsert
)
119 : SfxTabDialogController(pParent
, u
"modules/swriter/ui/envdialog.ui"_ustr
, u
"EnvDialog"_ustr
, &rSet
)
120 , m_aEnvItem(static_cast<const SwEnvItem
&>( rSet
.Get(FN_ENVELOP
)))
123 , m_xModify(m_xBuilder
->weld_button(u
"modify"_ustr
))
127 GetUserButton()->set_label(m_xModify
->get_label());
130 AddTabPage(u
"envelope"_ustr
, SwEnvPage::Create
, nullptr);
131 AddTabPage(u
"format"_ustr
, SwEnvFormatPage::Create
, nullptr);
132 AddTabPage(u
"printer"_ustr
, SwEnvPrtPage::Create
, nullptr);
135 SwEnvDlg::~SwEnvDlg()
137 m_pAddresseeSet
.reset();
138 m_pSenderSet
.reset();
141 void SwEnvDlg::PageCreated(const OUString
& rId
, SfxTabPage
&rPage
)
143 if (rId
== "printer")
145 static_cast<SwEnvPrtPage
*>(&rPage
)->SetPrt(m_pPrinter
);
147 else if (rId
== "envelope")
149 static_cast<SwEnvPage
*>(&rPage
)->Init(this);
151 else if (rId
== "format")
153 static_cast<SwEnvFormatPage
*>(&rPage
)->Init(this);
159 short nRet
= SfxTabDialogController::Ok();
161 if (nRet
== RET_OK
|| nRet
== RET_USER
)
165 SwTextFormatColl
* pColl
= m_pSh
->GetTextCollFromPool(RES_POOLCOLL_ENVELOPE_ADDRESS
);
166 pColl
->SetFormatAttr(*m_pAddresseeSet
);
170 SwTextFormatColl
* pColl
= m_pSh
->GetTextCollFromPool(RES_POOLCOLL_SEND_ADDRESS
);
171 pColl
->SetFormatAttr(*m_pSenderSet
);
178 SwEnvPage::SwEnvPage(weld::Container
* pPage
, weld::DialogController
* pController
, const SfxItemSet
& rSet
)
179 : SfxTabPage(pPage
, pController
, u
"modules/swriter/ui/envaddresspage.ui"_ustr
, u
"EnvAddressPage"_ustr
, &rSet
)
182 , m_xAddrEdit(m_xBuilder
->weld_text_view(u
"addredit"_ustr
))
183 , m_xDatabaseLB(m_xBuilder
->weld_combo_box(u
"database"_ustr
))
184 , m_xTableLB(m_xBuilder
->weld_combo_box(u
"table"_ustr
))
185 , m_xDBFieldLB(m_xBuilder
->weld_combo_box(u
"field"_ustr
))
186 , m_xInsertBT(m_xBuilder
->weld_button(u
"insert"_ustr
))
187 , m_xSenderBox(m_xBuilder
->weld_check_button(u
"sender"_ustr
))
188 , m_xSenderEdit(m_xBuilder
->weld_text_view(u
"senderedit"_ustr
))
189 , m_xPreview(new weld::CustomWeld(*m_xBuilder
, u
"preview"_ustr
, m_aPreview
))
191 auto nTextBoxHeight(m_xAddrEdit
->get_height_rows(10));
192 auto nTextBoxWidth(m_xAddrEdit
->get_approximate_digit_width() * 25);
194 m_xAddrEdit
->set_size_request(nTextBoxWidth
, nTextBoxHeight
);
195 m_xSenderEdit
->set_size_request(nTextBoxWidth
, nTextBoxHeight
);
197 auto nListBoxWidth
= m_xTableLB
->get_approximate_digit_width() * 25;
198 m_xTableLB
->set_size_request(nListBoxWidth
, -1);
199 m_xDatabaseLB
->set_size_request(nListBoxWidth
, -1);
200 m_xDBFieldLB
->set_size_request(nListBoxWidth
, -1);
202 SetExchangeSupport();
205 void SwEnvPage::Init(SwEnvDlg
* pDialog
)
208 m_pSh
= m_pDialog
->m_pSh
;
209 m_aPreview
.SetDialog(pDialog
);
212 m_xDatabaseLB
->connect_changed(LINK(this, SwEnvPage
, DatabaseHdl
));
213 m_xTableLB
->connect_changed(LINK(this, SwEnvPage
, DatabaseHdl
));
214 m_xInsertBT
->connect_clicked(LINK(this, SwEnvPage
, FieldHdl
));
215 m_xSenderBox
->connect_toggled(LINK(this, SwEnvPage
, SenderHdl
));
217 SwDBData aData
= m_pSh
->GetDBData();
218 m_sActDBName
= aData
.sDataSource
+ OUStringChar(DB_DELIM
) + aData
.sCommand
;
222 SwEnvPage::~SwEnvPage()
226 IMPL_LINK( SwEnvPage
, DatabaseHdl
, weld::ComboBox
&, rListBox
, void )
228 SwWait
aWait( *m_pSh
->GetView().GetDocShell(), true );
230 if (&rListBox
== m_xDatabaseLB
.get())
232 m_sActDBName
= rListBox
.get_active_text();
233 m_pSh
->GetDBManager()->GetTableNames(*m_xTableLB
, m_sActDBName
);
234 m_sActDBName
+= OUStringChar(DB_DELIM
);
238 m_sActDBName
= comphelper::string::setToken(m_sActDBName
, 1, DB_DELIM
, m_xTableLB
->get_active_text());
240 m_pSh
->GetDBManager()->GetColumnNames(*m_xDBFieldLB
, m_xDatabaseLB
->get_active_text(),
241 m_xTableLB
->get_active_text());
244 IMPL_LINK_NOARG(SwEnvPage
, FieldHdl
, weld::Button
&, void)
246 OUString
aStr("<" + m_xDatabaseLB
->get_active_text() + "." +
247 m_xTableLB
->get_active_text() + "." +
248 m_xTableLB
->get_active_id() + "." +
249 m_xDBFieldLB
->get_active_text() + ">");
250 m_xAddrEdit
->replace_selection(aStr
);
251 int nStartPos
, nEndPos
;
252 m_xAddrEdit
->get_selection_bounds(nStartPos
, nEndPos
);
253 m_xAddrEdit
->grab_focus();
254 m_xAddrEdit
->select_region(nStartPos
, nEndPos
);
257 IMPL_LINK_NOARG(SwEnvPage
, SenderHdl
, weld::Toggleable
&, void)
259 const bool bEnable
= m_xSenderBox
->get_active();
260 GetParentSwEnvDlg()->m_aEnvItem
.m_bSend
= bEnable
;
261 m_xSenderEdit
->set_sensitive(bEnable
);
264 m_xSenderEdit
->grab_focus();
265 if (m_xSenderEdit
->get_text().isEmpty())
266 m_xSenderEdit
->set_text(MakeSender());
268 m_xPreview
->queue_draw();
271 void SwEnvPage::InitDatabaseBox()
273 if (!m_pSh
->GetDBManager())
276 m_xDatabaseLB
->clear();
277 const Sequence
<OUString
> aDataNames
= SwDBManager::GetExistingDatabaseNames();
279 for (const OUString
& rDataName
: aDataNames
)
280 m_xDatabaseLB
->append_text(rDataName
);
283 OUString sDBName
= m_sActDBName
.getToken( 0, DB_DELIM
, nIdx
);
284 OUString sTableName
= m_sActDBName
.getToken( 0, DB_DELIM
, nIdx
);
285 m_xDatabaseLB
->set_active_text(sDBName
);
286 if (m_pSh
->GetDBManager()->GetTableNames(*m_xTableLB
, sDBName
))
288 m_xTableLB
->append_text(sTableName
);
289 m_pSh
->GetDBManager()->GetColumnNames(*m_xDBFieldLB
, sDBName
, sTableName
);
292 m_xDBFieldLB
->clear();
295 std::unique_ptr
<SfxTabPage
> SwEnvPage::Create(weld::Container
* pPage
, weld::DialogController
* pController
, const SfxItemSet
* rSet
)
297 return std::make_unique
<SwEnvPage
>(pPage
, pController
, *rSet
);
300 void SwEnvPage::ActivatePage(const SfxItemSet
& rSet
)
302 SfxItemSet
aSet(rSet
);
303 aSet
.Put(GetParentSwEnvDlg()->m_aEnvItem
);
307 DeactivateRC
SwEnvPage::DeactivatePage(SfxItemSet
* _pSet
)
309 FillItem(GetParentSwEnvDlg()->m_aEnvItem
);
312 return DeactivateRC::LeavePage
;
315 void SwEnvPage::FillItem(SwEnvItem
& rItem
)
317 rItem
.m_aAddrText
= m_xAddrEdit
->get_text();
318 rItem
.m_bSend
= m_xSenderBox
->get_active();
319 rItem
.m_aSendText
= m_xSenderEdit
->get_text();
322 bool SwEnvPage::FillItemSet(SfxItemSet
* rSet
)
324 FillItem(GetParentSwEnvDlg()->m_aEnvItem
);
325 rSet
->Put(GetParentSwEnvDlg()->m_aEnvItem
);
329 void SwEnvPage::Reset(const SfxItemSet
* rSet
)
331 SwEnvItem aItem
= static_cast<const SwEnvItem
&>( rSet
->Get(FN_ENVELOP
));
332 m_xAddrEdit
->set_text(convertLineEnd(aItem
.m_aAddrText
, GetSystemLineEnd()));
333 m_xSenderEdit
->set_text(convertLineEnd(aItem
.m_aSendText
, GetSystemLineEnd()));
334 m_xSenderBox
->set_active(aItem
.m_bSend
);
335 SenderHdl(*m_xSenderBox
);
338 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */