bump product version to 4.1.6.2
[LibreOffice.git] / sw / source / ui / envelp / envlop1.cxx
blob29ab6f8bc9934eea79064c24be69f6a405b1c237
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include "dbmgr.hxx"
21 #include <sfx2/app.hxx>
22 #include <vcl/msgbox.hxx>
23 #include <swwait.hxx>
24 #include <viewopt.hxx>
26 #include "wrtsh.hxx"
27 #include "cmdid.h"
28 #include "helpid.h"
29 #include "envfmt.hxx"
30 #include "envlop.hxx"
31 #include "envprt.hxx"
32 #include "fmtcol.hxx"
33 #include "poolfmt.hxx"
34 #include "view.hxx"
36 #include "envlop.hrc"
37 #include <comphelper/processfactory.hxx>
39 #include <unomid.h>
41 using namespace ::com::sun::star::lang;
42 using namespace ::com::sun::star::container;
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star;
45 using namespace ::rtl;
48 //impl in envimg.cxx
49 extern SW_DLLPUBLIC String MakeSender();
51 SwEnvPreview::SwEnvPreview(SfxTabPage* pParent, const ResId& rResID) :
53 Window(pParent, rResID)
56 SetMapMode(MapMode(MAP_PIXEL));
59 SwEnvPreview::~SwEnvPreview()
63 void SwEnvPreview::DataChanged( const DataChangedEvent& rDCEvt )
65 Window::DataChanged( rDCEvt );
66 if ( DATACHANGED_SETTINGS == rDCEvt.GetType() )
67 SetBackground( GetSettings().GetStyleSettings().GetDialogColor() );
70 void SwEnvPreview::Paint(const Rectangle &)
72 const StyleSettings& rSettings = GetSettings().GetStyleSettings();
74 const SwEnvItem& rItem =
75 ((SwEnvDlg*) GetParentDialog())->aEnvItem;
77 sal_uInt16 nPageW = (sal_uInt16) std::max(rItem.lWidth, rItem.lHeight),
78 nPageH = (sal_uInt16) std::min(rItem.lWidth, rItem.lHeight);
80 float fx = (float)GetOutputSizePixel().Width () / (float)nPageW,
81 fy = (float)GetOutputSizePixel().Height() / (float)nPageH,
82 f = 0.8f * ( fx < fy ? fx : fy );
84 Color aBack = rSettings.GetWindowColor( );
85 Color aFront = SwViewOption::GetFontColor();
86 Color aMedium = Color( ( aBack.GetRed() + aFront.GetRed() ) / 2,
87 ( aBack.GetGreen() + aFront.GetGreen() ) / 2,
88 ( aBack.GetBlue() + aFront.GetBlue() ) / 2
91 SetLineColor( aFront );
93 // Envelope
94 long nW = (sal_uInt16) (f * nPageW),
95 nH = (sal_uInt16) (f * nPageH),
96 nX = (GetOutputSizePixel().Width () - nW) / 2,
97 nY = (GetOutputSizePixel().Height() - nH) / 2;
98 SetFillColor( aBack );
99 DrawRect(Rectangle(Point(nX, nY), Size(nW, nH)));
101 // Sender
102 if (rItem.bSend)
104 long nSendX = nX + (sal_uInt16) (f * rItem.lSendFromLeft),
105 nSendY = nY + (sal_uInt16) (f * rItem.lSendFromTop ),
106 nSendW = (sal_uInt16) (f * (rItem.lAddrFromLeft - rItem.lSendFromLeft)),
107 nSendH = (sal_uInt16) (f * (rItem.lAddrFromTop - rItem.lSendFromTop - 566));
108 SetFillColor( aMedium );
110 DrawRect(Rectangle(Point(nSendX, nSendY), Size(nSendW, nSendH)));
113 // Addressee
114 long nAddrX = nX + (sal_uInt16) (f * rItem.lAddrFromLeft),
115 nAddrY = nY + (sal_uInt16) (f * rItem.lAddrFromTop ),
116 nAddrW = (sal_uInt16) (f * (nPageW - rItem.lAddrFromLeft - 566)),
117 nAddrH = (sal_uInt16) (f * (nPageH - rItem.lAddrFromTop - 566));
118 SetFillColor( aMedium );
119 DrawRect(Rectangle(Point(nAddrX, nAddrY), Size(nAddrW, nAddrH)));
121 // Stamp
122 long nStmpW = (sal_uInt16) (f * 1417 /* 2,5 cm */),
123 nStmpH = (sal_uInt16) (f * 1701 /* 3,0 cm */),
124 nStmpX = nX + nW - (sal_uInt16) (f * 566) - nStmpW,
125 nStmpY = nY + (sal_uInt16) (f * 566);
127 SetFillColor( aBack );
128 DrawRect(Rectangle(Point(nStmpX, nStmpY), Size(nStmpW, nStmpH)));
131 SwEnvDlg::SwEnvDlg(Window* pParent, const SfxItemSet& rSet,
132 SwWrtShell* pWrtSh, Printer* pPrt, sal_Bool bInsert) :
134 SfxTabDialog(pParent, SW_RES(DLG_ENV), &rSet, sal_False, &aEmptyStr),
135 sInsert(SW_RES(ST_INSERT)),
136 sChange(SW_RES(ST_CHANGE)),
137 aEnvItem((const SwEnvItem&) rSet.Get(FN_ENVELOP)),
138 pSh(pWrtSh),
139 pPrinter(pPrt),
140 pAddresseeSet(0),
141 pSenderSet(0)
143 FreeResource();
145 GetOKButton().SetText(String(SW_RES(STR_BTN_NEWDOC)));
146 GetOKButton().SetHelpId(HID_ENVELOP_PRINT);
147 GetOKButton().SetHelpText(aEmptyStr); // in order for generated help text to get used
148 if (GetUserButton())
150 GetUserButton()->SetText(bInsert ? sInsert : sChange);
151 GetUserButton()->SetHelpId(HID_ENVELOP_INSERT);
154 AddTabPage(TP_ENV_ENV, SwEnvPage ::Create, 0);
155 AddTabPage(TP_ENV_FMT, SwEnvFmtPage::Create, 0);
156 AddTabPage(TP_ENV_PRT, SwEnvPrtPage::Create, 0);
159 SwEnvDlg::~SwEnvDlg()
161 delete pAddresseeSet;
162 delete pSenderSet;
165 void SwEnvDlg::PageCreated(sal_uInt16 nId, SfxTabPage &rPage)
167 if (nId == TP_ENV_PRT)
169 ((SwEnvPrtPage*)&rPage)->SetPrt(pPrinter);
173 short SwEnvDlg::Ok()
175 short nRet = SfxTabDialog::Ok();
177 if (nRet == RET_OK || nRet == RET_USER)
179 if (pAddresseeSet)
181 SwTxtFmtColl* pColl = pSh->GetTxtCollFromPool(RES_POOLCOLL_JAKETADRESS);
182 pColl->SetFmtAttr(*pAddresseeSet);
184 if (pSenderSet)
186 SwTxtFmtColl* pColl = pSh->GetTxtCollFromPool(RES_POOLCOLL_SENDADRESS);
187 pColl->SetFmtAttr(*pSenderSet);
191 return nRet;
194 SwEnvPage::SwEnvPage(Window* pParent, const SfxItemSet& rSet) :
196 SfxTabPage(pParent, SW_RES(TP_ENV_ENV), rSet),
198 aAddrText (this, SW_RES(TXT_ADDR )),
199 aAddrEdit (this, SW_RES(EDT_ADDR )),
200 aDatabaseFT (this, SW_RES(FT_DATABASE)),
201 aDatabaseLB (this, SW_RES(LB_DATABASE)),
202 aTableFT (this, SW_RES(FT_TABLE )),
203 aTableLB (this, SW_RES(LB_TABLE )),
204 aInsertBT (this, SW_RES(BTN_INSERT )),
205 aDBFieldFT (this, SW_RES(FT_DBFIELD )),
206 aDBFieldLB (this, SW_RES(LB_DBFIELD )),
207 aSenderBox (this, SW_RES(BOX_SEND )),
208 aSenderEdit (this, SW_RES(EDT_SEND )),
209 aPreview (this, SW_RES(WIN_PREVIEW))
212 FreeResource();
213 SetExchangeSupport();
214 pSh = GetParentSwEnvDlg()->pSh;
216 // Install handlers
217 aDatabaseLB .SetSelectHdl(LINK(this, SwEnvPage, DatabaseHdl ));
218 aTableLB .SetSelectHdl(LINK(this, SwEnvPage, DatabaseHdl ));
219 aInsertBT .SetClickHdl (LINK(this, SwEnvPage, FieldHdl ));
220 aSenderBox .SetClickHdl (LINK(this, SwEnvPage, SenderHdl ));
221 aPreview.SetBorderStyle( WINDOW_BORDER_MONO );
223 SwDBData aData = pSh->GetDBData();
224 sActDBName = aData.sDataSource;
225 sActDBName += DB_DELIM;
226 sActDBName += (String)aData.sCommand;
227 InitDatabaseBox();
230 SwEnvPage::~SwEnvPage()
234 IMPL_LINK( SwEnvPage, DatabaseHdl, ListBox *, pListBox )
236 SwWait aWait( *pSh->GetView().GetDocShell(), sal_True );
238 if (pListBox == &aDatabaseLB)
240 sActDBName = pListBox->GetSelectEntry();
241 pSh->GetNewDBMgr()->GetTableNames(&aTableLB, sActDBName);
242 sActDBName += DB_DELIM;
244 else
245 sActDBName.SetToken(1, DB_DELIM, aTableLB.GetSelectEntry());
246 pSh->GetNewDBMgr()->GetColumnNames(&aDBFieldLB, aDatabaseLB.GetSelectEntry(),
247 aTableLB.GetSelectEntry());
248 return 0;
251 IMPL_LINK_NOARG(SwEnvPage, FieldHdl)
253 OUString aStr("<" + aDatabaseLB.GetSelectEntry() + "." +
254 aTableLB.GetSelectEntry() + "." +
255 OUString(aTableLB.GetEntryData(aTableLB.GetSelectEntryPos()) == 0 ? '0' : '1') + "." +
256 aDBFieldLB.GetSelectEntry() + ">");
257 aAddrEdit.ReplaceSelected(aStr);
258 Selection aSel = aAddrEdit.GetSelection();
259 aAddrEdit.GrabFocus();
260 aAddrEdit.SetSelection(aSel);
261 return 0;
264 IMPL_LINK_NOARG(SwEnvPage, SenderHdl)
266 const sal_Bool bEnable = aSenderBox.IsChecked();
267 GetParentSwEnvDlg()->aEnvItem.bSend = bEnable;
268 aSenderEdit.Enable(bEnable);
269 if ( bEnable )
271 aSenderEdit.GrabFocus();
272 if(aSenderEdit.GetText().isEmpty())
273 aSenderEdit.SetText(MakeSender());
275 aPreview.Invalidate();
276 return 0;
279 void SwEnvPage::InitDatabaseBox()
281 if (pSh->GetNewDBMgr())
283 aDatabaseLB.Clear();
284 Sequence<OUString> aDataNames = SwNewDBMgr::GetExistingDatabaseNames();
285 const OUString* pDataNames = aDataNames.getConstArray();
287 for (long i = 0; i < aDataNames.getLength(); i++)
288 aDatabaseLB.InsertEntry(pDataNames[i]);
290 String sDBName = sActDBName.GetToken( 0, DB_DELIM );
291 String sTableName = sActDBName.GetToken( 1, DB_DELIM );
292 aDatabaseLB.SelectEntry(sDBName);
293 if (pSh->GetNewDBMgr()->GetTableNames(&aTableLB, sDBName))
295 aTableLB.SelectEntry(sTableName);
296 pSh->GetNewDBMgr()->GetColumnNames(&aDBFieldLB, sDBName, sTableName);
298 else
299 aDBFieldLB.Clear();
304 SfxTabPage* SwEnvPage::Create(Window* pParent, const SfxItemSet& rSet)
306 return new SwEnvPage(pParent, rSet);
309 void SwEnvPage::ActivatePage(const SfxItemSet& rSet)
311 SfxItemSet aSet(rSet);
312 aSet.Put(GetParentSwEnvDlg()->aEnvItem);
313 Reset(aSet);
316 int SwEnvPage::DeactivatePage(SfxItemSet* _pSet)
318 FillItem(GetParentSwEnvDlg()->aEnvItem);
319 if( _pSet )
320 FillItemSet(*_pSet);
321 return SfxTabPage::LEAVE_PAGE;
324 void SwEnvPage::FillItem(SwEnvItem& rItem)
326 rItem.aAddrText = aAddrEdit .GetText();
327 rItem.bSend = aSenderBox .IsChecked();
328 rItem.aSendText = aSenderEdit.GetText();
331 sal_Bool SwEnvPage::FillItemSet(SfxItemSet& rSet)
333 FillItem(GetParentSwEnvDlg()->aEnvItem);
334 rSet.Put(GetParentSwEnvDlg()->aEnvItem);
335 return sal_True;
338 void SwEnvPage::Reset(const SfxItemSet& rSet)
340 SwEnvItem aItem = (const SwEnvItem&) rSet.Get(FN_ENVELOP);
341 aAddrEdit .SetText(convertLineEnd(aItem.aAddrText, GetSystemLineEnd()));
342 aSenderEdit.SetText(convertLineEnd(aItem.aSendText, GetSystemLineEnd()));
343 aSenderBox .Check (aItem.bSend);
344 aSenderBox.GetClickHdl().Call(&aSenderBox);
348 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */