Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License or as specified alternatively below. You may obtain a copy of
8 : * the License at http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * Major Contributor(s):
16 : * Copyright (C) 2012 Markus Mohrhard <markus.mohrhard@googlemail.com> (initial developer)
17 : *
18 : * All Rights Reserved.
19 : *
20 : * For minor contributions see the git repository.
21 : *
22 : * Alternatively, the contents of this file may be used under the terms of
23 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
24 : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
25 : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
26 : * instead of those above.
27 : */
28 :
29 : #include "condformatmgr.hxx"
30 : #include "condformatmgr.hrc"
31 : #include "scresid.hxx"
32 : #include "globstr.hrc"
33 : #include "condformatdlg.hxx"
34 : #include "vcl/msgbox.hxx"
35 :
36 : #define ITEMID_RANGE 1
37 : #define ITEMID_CONDITION 2
38 :
39 :
40 0 : ScCondFormatManagerWindow::ScCondFormatManagerWindow(Window* pParent, ScDocument* pDoc, ScConditionalFormatList* pFormatList):
41 : SvTabListBox(pParent, WB_SORT | WB_HSCROLL | WB_CLIPCHILDREN | WB_TABSTOP),
42 : maHeaderBar( pParent, WB_BUTTONSTYLE | WB_BOTTOMBORDER ),
43 : mpDoc(pDoc),
44 0 : mpFormatList(pFormatList)
45 : {
46 0 : Size aBoxSize( pParent->GetOutputSizePixel() );
47 :
48 0 : maHeaderBar.SetPosSizePixel( Point(0, 0), Size( aBoxSize.Width(), 16 ) );
49 :
50 0 : String aConditionStr(ScGlobal::GetRscString(STR_HEADER_COND));
51 0 : String aRangeStr(ScGlobal::GetRscString(STR_HEADER_RANGE));
52 :
53 0 : long nTabSize = aBoxSize.Width()/2;
54 0 : maHeaderBar.InsertItem( ITEMID_RANGE, aRangeStr, nTabSize, HIB_LEFT| HIB_VCENTER );
55 0 : maHeaderBar.InsertItem( ITEMID_CONDITION, aConditionStr, nTabSize, HIB_LEFT| HIB_VCENTER );
56 :
57 0 : static long nTabs[] = {2, 0, nTabSize };
58 0 : Size aHeadSize( maHeaderBar.GetSizePixel() );
59 :
60 : //pParent->SetFocusControl( this );
61 0 : SetPosSizePixel( Point( 0, aHeadSize.Height() ), Size( aBoxSize.Width(), aBoxSize.Height() - aHeadSize.Height() ) );
62 0 : SetTabs( &nTabs[0], MAP_PIXEL );
63 :
64 0 : maHeaderBar.SetEndDragHdl( LINK(this, ScCondFormatManagerWindow, HeaderEndDragHdl ) );
65 0 : HeaderEndDragHdl(NULL);
66 :
67 0 : Init();
68 0 : Show();
69 0 : maHeaderBar.Show();
70 0 : SetSelectionMode(MULTIPLE_SELECTION);
71 0 : }
72 :
73 0 : String ScCondFormatManagerWindow::createEntryString(const ScConditionalFormat& rFormat)
74 : {
75 0 : ScRangeList aRange = rFormat.GetRange();
76 0 : String aStr;
77 0 : aRange.Format(aStr, SCA_VALID, mpDoc, mpDoc->GetAddressConvention());
78 0 : aStr += '\t';
79 0 : aStr += ScCondFormatHelper::GetExpression(rFormat, aRange.GetTopLeftCorner());
80 0 : return aStr;
81 : }
82 :
83 0 : void ScCondFormatManagerWindow::Init()
84 : {
85 0 : SetUpdateMode(false);
86 :
87 0 : for(ScConditionalFormatList::iterator itr = mpFormatList->begin(); itr != mpFormatList->end(); ++itr)
88 : {
89 0 : SvTreeListEntry* pEntry = InsertEntryToColumn( createEntryString(*itr), LIST_APPEND, 0xffff );
90 0 : maMapLBoxEntryToCondIndex.insert(std::pair<SvTreeListEntry*,sal_Int32>(pEntry,itr->GetKey()));
91 : }
92 0 : SetUpdateMode(true);
93 0 : }
94 :
95 0 : void ScCondFormatManagerWindow::DeleteSelection()
96 : {
97 0 : if(GetSelectionCount())
98 : {
99 0 : for(SvTreeListEntry* pEntry = FirstSelected(); pEntry != NULL; pEntry = NextSelected(pEntry))
100 : {
101 0 : sal_Int32 nIndex = maMapLBoxEntryToCondIndex.find(pEntry)->second;
102 0 : mpFormatList->erase(nIndex);
103 : }
104 0 : RemoveSelection();
105 : }
106 0 : }
107 :
108 0 : ScConditionalFormat* ScCondFormatManagerWindow::GetSelection()
109 : {
110 0 : SvTreeListEntry* pEntry = FirstSelected();
111 0 : if(!pEntry)
112 0 : return NULL;
113 :
114 0 : sal_Int32 nIndex = maMapLBoxEntryToCondIndex.find(pEntry)->second;
115 0 : return mpFormatList->GetFormat(nIndex);
116 : }
117 :
118 0 : void ScCondFormatManagerWindow::Update()
119 : {
120 0 : Clear();
121 0 : maMapLBoxEntryToCondIndex.clear();
122 0 : Init();
123 0 : }
124 :
125 0 : IMPL_LINK_NOARG(ScCondFormatManagerWindow, HeaderEndDragHdl)
126 : {
127 0 : long aTableSize = maHeaderBar.GetSizePixel().Width();
128 0 : long aItemRangeSize = maHeaderBar.GetItemSize(ITEMID_RANGE);
129 :
130 : //calculate column size based on user input and minimum size
131 0 : long aItemCondSize = aTableSize - aItemRangeSize;
132 :
133 0 : Size aSz;
134 0 : aSz.Width() = aItemRangeSize;
135 0 : SetTab( ITEMID_RANGE, PixelToLogic( aSz, MapMode(MAP_APPFONT) ).Width(), MAP_APPFONT );
136 0 : maHeaderBar.SetItemSize(ITEMID_RANGE, aItemRangeSize);
137 0 : aSz.Width() += aItemCondSize;
138 0 : SetTab( ITEMID_CONDITION, PixelToLogic( aSz, MapMode(MAP_APPFONT) ).Width(), MAP_APPFONT );
139 0 : maHeaderBar.SetItemSize(ITEMID_CONDITION, aItemCondSize);
140 :
141 0 : return 0;
142 : }
143 :
144 0 : ScCondFormatManagerCtrl::ScCondFormatManagerCtrl(Window* pParent, ScDocument* pDoc, ScConditionalFormatList* pFormatList):
145 : Control(pParent, ScResId(CTRL_TABLE)),
146 0 : maWdManager(this, pDoc, pFormatList)
147 : {
148 0 : }
149 :
150 0 : ScConditionalFormat* ScCondFormatManagerCtrl::GetSelection()
151 : {
152 0 : return maWdManager.GetSelection();
153 : }
154 :
155 0 : void ScCondFormatManagerCtrl::DeleteSelection()
156 : {
157 0 : maWdManager.DeleteSelection();
158 0 : }
159 :
160 0 : void ScCondFormatManagerCtrl::Update()
161 : {
162 0 : maWdManager.Update();
163 0 : }
164 :
165 0 : ScCondFormatManagerDlg::ScCondFormatManagerDlg(Window* pParent, ScDocument* pDoc, const ScConditionalFormatList* pFormatList, const ScAddress& rPos):
166 : ModalDialog(pParent, ScResId(RID_SCDLG_COND_FORMAT_MANAGER)),
167 : maBtnAdd(this, ScResId(BTN_ADD)),
168 : maBtnRemove(this, ScResId(BTN_REMOVE)),
169 : maBtnEdit(this, ScResId(BTN_EDIT)),
170 : maBtnOk(this, ScResId(BTN_OK)),
171 : maBtnCancel(this, ScResId(BTN_CANCEL)),
172 : maFlLine(this, ScResId(FL_LINE)),
173 0 : mpFormatList( pFormatList ? new ScConditionalFormatList(*pFormatList) : NULL),
174 : maCtrlManager(this, pDoc, mpFormatList),
175 : mpDoc(pDoc),
176 : maPos(rPos),
177 0 : mbModified(false)
178 : {
179 0 : FreeResource();
180 :
181 0 : maBtnRemove.SetClickHdl(LINK(this, ScCondFormatManagerDlg, RemoveBtnHdl));
182 0 : maBtnEdit.SetClickHdl(LINK(this, ScCondFormatManagerDlg, EditBtnHdl));
183 0 : maBtnAdd.SetClickHdl(LINK(this, ScCondFormatManagerDlg, AddBtnHdl));
184 0 : maCtrlManager.GetListControl().SetDoubleClickHdl(LINK(this, ScCondFormatManagerDlg, EditBtnHdl));
185 0 : }
186 :
187 0 : ScCondFormatManagerDlg::~ScCondFormatManagerDlg()
188 : {
189 0 : delete mpFormatList;
190 0 : }
191 :
192 0 : bool ScCondFormatManagerDlg::IsInRefMode() const
193 : {
194 0 : return true;
195 : }
196 :
197 0 : ScConditionalFormatList* ScCondFormatManagerDlg::GetConditionalFormatList()
198 : {
199 0 : ScConditionalFormatList* pList = mpFormatList;
200 0 : mpFormatList = NULL;
201 0 : return pList;
202 : }
203 :
204 0 : bool ScCondFormatManagerDlg::CondFormatsChanged()
205 : {
206 0 : return mbModified;
207 : }
208 :
209 0 : IMPL_LINK_NOARG(ScCondFormatManagerDlg, RemoveBtnHdl)
210 : {
211 0 : maCtrlManager.DeleteSelection();
212 0 : mbModified = true;
213 0 : return 0;
214 : }
215 :
216 0 : IMPL_LINK_NOARG(ScCondFormatManagerDlg, EditBtnHdl)
217 : {
218 0 : ScConditionalFormat* pFormat = maCtrlManager.GetSelection();
219 :
220 0 : if(!pFormat)
221 0 : return 0;
222 :
223 0 : sal_uInt16 nId = 1;
224 0 : ScModule* pScMod = SC_MOD();
225 0 : pScMod->SetRefDialog( nId, true );
226 : boost::scoped_ptr<ScCondFormatDlg> pDlg(new ScCondFormatDlg(this, mpDoc, pFormat, pFormat->GetRange(),
227 0 : pFormat->GetRange().GetTopLeftCorner(), condformat::dialog::NONE));
228 0 : Show(false, 0);
229 0 : if(pDlg->Execute() == RET_OK)
230 : {
231 0 : sal_Int32 nKey = pFormat->GetKey();
232 0 : mpFormatList->erase(nKey);
233 0 : ScConditionalFormat* pNewFormat = pDlg->GetConditionalFormat();
234 0 : if(pNewFormat)
235 : {
236 0 : pNewFormat->SetKey(nKey);
237 0 : mpFormatList->InsertNew(pNewFormat);
238 : }
239 :
240 0 : maCtrlManager.Update();
241 0 : mbModified = true;
242 : }
243 0 : Show(true, 0);
244 :
245 0 : pScMod->SetRefDialog( nId, false );
246 :
247 0 : return 0;
248 : }
249 :
250 : namespace {
251 :
252 0 : sal_uInt32 FindKey(ScConditionalFormatList* pFormatList)
253 : {
254 0 : sal_uInt32 nKey = 0;
255 0 : for(ScConditionalFormatList::const_iterator itr = pFormatList->begin(), itrEnd = pFormatList->end();
256 : itr != itrEnd; ++itr)
257 : {
258 0 : if(itr->GetKey() > nKey)
259 0 : nKey = itr->GetKey();
260 : }
261 :
262 0 : return nKey + 1;
263 : }
264 :
265 : }
266 :
267 0 : IMPL_LINK_NOARG(ScCondFormatManagerDlg, AddBtnHdl)
268 : {
269 0 : sal_uInt16 nId = 1;
270 0 : ScModule* pScMod = SC_MOD();
271 0 : pScMod->SetRefDialog( nId, true );
272 : boost::scoped_ptr<ScCondFormatDlg> pDlg(new ScCondFormatDlg(this, mpDoc, NULL, ScRangeList(),
273 0 : maPos, condformat::dialog::CONDITION));
274 0 : Show(false, 0);
275 0 : if(pDlg->Execute() == RET_OK)
276 : {
277 0 : ScConditionalFormat* pNewFormat = pDlg->GetConditionalFormat();
278 0 : if(!pNewFormat)
279 0 : return 0;
280 :
281 0 : mpFormatList->InsertNew(pNewFormat);
282 0 : pNewFormat->SetKey(FindKey(mpFormatList));
283 0 : maCtrlManager.Update();
284 :
285 0 : mbModified = true;
286 : }
287 0 : Show(true, 0);
288 0 : pScMod->SetRefDialog( nId, false );
289 :
290 0 : return 0;
291 0 : }
292 :
293 :
294 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|