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 "condformatdlg.hxx"
30 : #include "condformatdlg.hrc"
31 :
32 : #include <vcl/vclevent.hxx>
33 : #include <svl/style.hxx>
34 : #include <sfx2/dispatch.hxx>
35 : #include <svl/stritem.hxx>
36 : #include <svl/intitem.hxx>
37 : #include <svx/xtable.hxx>
38 : #include <svx/drawitem.hxx>
39 : #include <vcl/msgbox.hxx>
40 :
41 : #include "anyrefdg.hxx"
42 : #include "document.hxx"
43 : #include "conditio.hxx"
44 : #include "stlpool.hxx"
45 : #include "tabvwsh.hxx"
46 : #include "conditio.hxx"
47 : #include "colorscale.hxx"
48 : #include "colorformat.hxx"
49 : #include "reffact.hxx"
50 : #include "docsh.hxx"
51 : #include "docfunc.hxx"
52 : #include "condformatdlgentry.hxx"
53 :
54 : #include "globstr.hrc"
55 :
56 0 : ScCondFormatList::ScCondFormatList(Window* pParent, const ResId& rResId, ScDocument* pDoc, const ScConditionalFormat* pFormat,
57 : const ScRangeList& rRanges, const ScAddress& rPos, condformat::dialog::ScCondFormatDialogType eType):
58 : Control(pParent, rResId),
59 : mbHasScrollBar(false),
60 0 : mpScrollBar(new ScrollBar(this, WB_VERT )),
61 : mpDoc(pDoc),
62 : maPos(rPos),
63 0 : maRanges(rRanges)
64 : {
65 0 : mpScrollBar->SetScrollHdl( LINK( this, ScCondFormatList, ScrollHdl ) );
66 0 : mpScrollBar->EnableDrag();
67 :
68 0 : if(pFormat)
69 : {
70 0 : size_t nCount = pFormat->size();
71 0 : for (size_t nIndex = 0; nIndex < nCount; ++nIndex)
72 : {
73 0 : const ScFormatEntry* pEntry = pFormat->GetEntry(nIndex);
74 0 : switch(pEntry->GetType())
75 : {
76 : case condformat::CONDITION:
77 : {
78 0 : const ScCondFormatEntry* pConditionEntry = static_cast<const ScCondFormatEntry*>( pEntry );
79 0 : if(pConditionEntry->GetOperation() != SC_COND_DIRECT)
80 0 : maEntries.push_back(new ScConditionFrmtEntry( this, mpDoc, maPos, pConditionEntry ) );
81 : else
82 0 : maEntries.push_back(new ScFormulaFrmtEntry( this, mpDoc, maPos, pConditionEntry ) );
83 :
84 : }
85 0 : break;
86 : case condformat::COLORSCALE:
87 : {
88 0 : const ScColorScaleFormat* pColorScale = static_cast<const ScColorScaleFormat*>( pEntry );
89 0 : if( pColorScale->size() == 2 )
90 0 : maEntries.push_back(new ScColorScale2FrmtEntry( this, mpDoc, maPos, pColorScale ) );
91 : else
92 0 : maEntries.push_back(new ScColorScale3FrmtEntry( this, mpDoc, maPos, pColorScale ) );
93 : }
94 0 : break;
95 : case condformat::DATABAR:
96 0 : maEntries.push_back(new ScDataBarFrmtEntry( this, mpDoc, maPos, static_cast<const ScDataBarFormat*>( pEntry ) ) );
97 0 : break;
98 : case condformat::ICONSET:
99 0 : maEntries.push_back(new ScIconSetFrmtEntry( this, mpDoc, maPos, static_cast<const ScIconSetFormat*>( pEntry ) ) );
100 0 : break;
101 : case condformat::DATE:
102 0 : maEntries.push_back(new ScDateFrmtEntry( this, mpDoc, static_cast<const ScCondDateFormatEntry*>( pEntry ) ) );
103 0 : break;
104 : }
105 : }
106 : }
107 : else
108 : {
109 0 : switch(eType)
110 : {
111 : case condformat::dialog::CONDITION:
112 0 : maEntries.push_back(new ScConditionFrmtEntry( this, mpDoc, maPos ));
113 0 : break;
114 : case condformat::dialog::COLORSCALE:
115 0 : maEntries.push_back(new ScColorScale3FrmtEntry( this, mpDoc, maPos ));
116 0 : break;
117 : case condformat::dialog::DATABAR:
118 0 : maEntries.push_back(new ScDataBarFrmtEntry( this, mpDoc, maPos ));
119 0 : break;
120 : default:
121 0 : break;
122 : }
123 : }
124 0 : RecalcAll();
125 0 : if (!maEntries.empty())
126 0 : maEntries.begin()->SetActive();
127 :
128 0 : RecalcAll();
129 0 : FreeResource();
130 0 : }
131 :
132 0 : ScConditionalFormat* ScCondFormatList::GetConditionalFormat() const
133 : {
134 0 : if(maEntries.empty())
135 0 : return NULL;
136 :
137 0 : ScConditionalFormat* pFormat = new ScConditionalFormat(0, mpDoc);
138 0 : for(EntryContainer::const_iterator itr = maEntries.begin(); itr != maEntries.end(); ++itr)
139 : {
140 0 : ScFormatEntry* pEntry = itr->GetEntry();
141 0 : if(pEntry)
142 0 : pFormat->AddEntry(pEntry);
143 : }
144 :
145 0 : pFormat->AddRange(maRanges);
146 :
147 0 : return pFormat;
148 : }
149 :
150 0 : void ScCondFormatList::RecalcAll()
151 : {
152 0 : sal_Int32 nTotalHeight = 0;
153 0 : sal_Int32 nIndex = 1;
154 0 : for(EntryContainer::iterator itr = maEntries.begin(); itr != maEntries.end(); ++itr)
155 : {
156 0 : nTotalHeight += itr->GetSizePixel().Height();
157 0 : itr->SetIndex( nIndex );
158 0 : ++nIndex;
159 : }
160 :
161 0 : Size aCtrlSize = GetOutputSize();
162 0 : long nSrcBarSize = GetSettings().GetStyleSettings().GetScrollBarSize();
163 0 : if(nTotalHeight > GetSizePixel().Height())
164 : {
165 0 : mbHasScrollBar = true;
166 0 : mpScrollBar->SetPosSizePixel(Point(aCtrlSize.Width() -nSrcBarSize, 0),
167 0 : Size(nSrcBarSize, aCtrlSize.Height()) );
168 0 : mpScrollBar->SetRangeMax(nTotalHeight);
169 0 : mpScrollBar->SetVisibleSize(aCtrlSize.Height());
170 0 : mpScrollBar->Show();
171 : }
172 : else
173 : {
174 0 : mbHasScrollBar = false;
175 0 : mpScrollBar->Hide();
176 : }
177 :
178 0 : Point aPoint(0,-1*mpScrollBar->GetThumbPos());
179 0 : for(EntryContainer::iterator itr = maEntries.begin(); itr != maEntries.end(); ++itr)
180 : {
181 0 : itr->SetPosPixel(aPoint);
182 0 : Size aSize = itr->GetSizePixel();
183 0 : if(mbHasScrollBar)
184 0 : aSize.Width() = aCtrlSize.Width() - nSrcBarSize;
185 : else
186 0 : aSize.Width() = aCtrlSize.Width();
187 0 : itr->SetSizePixel(aSize);
188 :
189 0 : aPoint.Y() += itr->GetSizePixel().Height();
190 : }
191 0 : }
192 :
193 0 : void ScCondFormatList::DoScroll(long nDelta)
194 : {
195 0 : Point aNewPoint = mpScrollBar->GetPosPixel();
196 0 : Rectangle aRect(Point(), GetOutputSize());
197 0 : aRect.Right() -= mpScrollBar->GetSizePixel().Width();
198 0 : Scroll( 0, -nDelta, aRect );
199 0 : mpScrollBar->SetPosPixel(aNewPoint);
200 0 : }
201 :
202 0 : IMPL_LINK(ScCondFormatList, ColFormatTypeHdl, ListBox*, pBox)
203 : {
204 0 : EntryContainer::iterator itr = maEntries.begin();
205 0 : for(; itr != maEntries.end(); ++itr)
206 : {
207 0 : if(itr->IsSelected())
208 0 : break;
209 : }
210 0 : if(itr == maEntries.end())
211 0 : return 0;
212 :
213 0 : sal_Int32 nPos = pBox->GetSelectEntryPos();
214 0 : switch(nPos)
215 : {
216 : case 0:
217 0 : if(itr->GetType() == condformat::entry::COLORSCALE2)
218 0 : return 0;
219 :
220 0 : maEntries.replace( itr, new ScColorScale2FrmtEntry( this, mpDoc, maPos ) );
221 0 : break;
222 : case 1:
223 0 : if(itr->GetType() == condformat::entry::COLORSCALE3)
224 0 : return 0;
225 :
226 0 : maEntries.replace( itr, new ScColorScale3FrmtEntry( this, mpDoc, maPos ) );
227 0 : break;
228 : case 2:
229 0 : if(itr->GetType() == condformat::entry::DATABAR)
230 0 : return 0;
231 :
232 0 : maEntries.replace( itr, new ScDataBarFrmtEntry( this, mpDoc, maPos ) );
233 0 : break;
234 : case 3:
235 0 : if(itr->GetType() == condformat::entry::ICONSET)
236 0 : return 0;
237 :
238 0 : maEntries.replace( itr, new ScIconSetFrmtEntry( this, mpDoc, maPos ) );
239 0 : break;
240 : default:
241 0 : break;
242 : }
243 0 : static_cast<ScCondFormatDlg*>(GetParent())->InvalidateRefData();
244 0 : itr->SetActive();
245 0 : RecalcAll();
246 0 : return 0;
247 : }
248 :
249 0 : IMPL_LINK(ScCondFormatList, TypeListHdl, ListBox*, pBox)
250 : {
251 0 : EntryContainer::iterator itr = maEntries.begin();
252 0 : for(; itr != maEntries.end(); ++itr)
253 : {
254 0 : if(itr->IsSelected())
255 0 : break;
256 : }
257 0 : if(itr == maEntries.end())
258 0 : return 0;;
259 :
260 0 : sal_Int32 nPos = pBox->GetSelectEntryPos();
261 0 : switch(nPos)
262 : {
263 : case 0:
264 0 : switch(itr->GetType())
265 : {
266 : case condformat::entry::FORMULA:
267 : case condformat::entry::CONDITION:
268 : case condformat::entry::DATE:
269 0 : break;
270 : case condformat::entry::COLORSCALE2:
271 : case condformat::entry::COLORSCALE3:
272 : case condformat::entry::DATABAR:
273 : case condformat::entry::ICONSET:
274 0 : return 0;
275 : }
276 0 : maEntries.replace( itr, new ScColorScale3FrmtEntry(this, mpDoc, maPos));
277 0 : static_cast<ScCondFormatDlg*>(GetParent())->InvalidateRefData();
278 0 : itr->SetActive();
279 0 : break;
280 : case 1:
281 0 : if(itr->GetType() == condformat::entry::CONDITION)
282 0 : return 0;
283 :
284 0 : maEntries.replace( itr, new ScConditionFrmtEntry(this, mpDoc, maPos));
285 0 : static_cast<ScCondFormatDlg*>(GetParent())->InvalidateRefData();
286 0 : itr->SetActive();
287 0 : break;
288 : case 2:
289 0 : if(itr->GetType() == condformat::entry::FORMULA)
290 0 : return 0;
291 :
292 0 : maEntries.replace( itr, new ScFormulaFrmtEntry(this, mpDoc, maPos));
293 0 : static_cast<ScCondFormatDlg*>(GetParent())->InvalidateRefData();
294 0 : itr->SetActive();
295 0 : break;
296 : case 3:
297 0 : if(itr->GetType() == condformat::entry::DATE)
298 0 : return 0;
299 :
300 0 : maEntries.replace( itr, new ScDateFrmtEntry( this, mpDoc ));
301 0 : static_cast<ScCondFormatDlg*>(GetParent())->InvalidateRefData();
302 0 : itr->SetActive();
303 0 : break;
304 :
305 : }
306 0 : RecalcAll();
307 0 : return 0;
308 : }
309 :
310 0 : IMPL_LINK_NOARG( ScCondFormatList, AddBtnHdl )
311 : {
312 0 : ScCondFrmtEntry* pNewEntry = new ScConditionFrmtEntry(this, mpDoc, maPos);
313 0 : maEntries.push_back( pNewEntry );
314 0 : for(EntryContainer::iterator itr = maEntries.begin(); itr != maEntries.end(); ++itr)
315 : {
316 0 : itr->SetInactive();
317 : }
318 0 : static_cast<ScCondFormatDlg*>(GetParent())->InvalidateRefData();
319 0 : pNewEntry->SetActive();
320 0 : RecalcAll();
321 0 : return 0;
322 : }
323 :
324 0 : IMPL_LINK_NOARG( ScCondFormatList, RemoveBtnHdl )
325 : {
326 0 : for(EntryContainer::iterator itr = maEntries.begin(); itr != maEntries.end(); ++itr)
327 : {
328 0 : if(itr->IsSelected())
329 : {
330 0 : maEntries.erase(itr);
331 0 : break;
332 : }
333 : }
334 0 : static_cast<ScCondFormatDlg*>(GetParent())->InvalidateRefData();
335 0 : RecalcAll();
336 0 : return 0;
337 : }
338 :
339 0 : IMPL_LINK( ScCondFormatList, EntrySelectHdl, ScCondFrmtEntry*, pEntry )
340 : {
341 0 : if(pEntry->IsSelected())
342 0 : return 0;
343 :
344 0 : for(EntryContainer::iterator itr = maEntries.begin(); itr != maEntries.end(); ++itr)
345 : {
346 0 : itr->SetInactive();
347 : }
348 0 : static_cast<ScCondFormatDlg*>(GetParent())->InvalidateRefData();
349 0 : pEntry->SetActive();
350 0 : RecalcAll();
351 0 : return 0;
352 : }
353 :
354 0 : IMPL_LINK_NOARG( ScCondFormatList, ScrollHdl )
355 : {
356 0 : DoScroll(mpScrollBar->GetDelta());
357 0 : return 0;
358 : }
359 :
360 : //---------------------------------------------------
361 : //ScCondFormatDlg
362 : //---------------------------------------------------
363 :
364 0 : ScCondFormatDlg::ScCondFormatDlg(Window* pParent, ScDocument* pDoc, const ScConditionalFormat* pFormat, const ScRangeList& rRange,
365 : const ScAddress& rPos, condformat::dialog::ScCondFormatDialogType eType):
366 : ScAnyRefModalDlg(pParent, ScResId(RID_SCDLG_CONDFORMAT) ),
367 : maBtnAdd( this, ScResId( BTN_ADD ) ),
368 : maBtnRemove( this, ScResId( BTN_REMOVE ) ),
369 : maBtnOk( this, ScResId( BTN_OK ) ),
370 : maBtnCancel( this, ScResId( BTN_CANCEL ) ),
371 : maFtRange( this, ScResId( FT_RANGE ) ),
372 : maEdRange( this, this, ScResId( ED_RANGE ) ),
373 : maRbRange( this, ScResId( RB_RANGE ), &maEdRange, this ),
374 : maCondFormList( this, ScResId( CTRL_LIST ), pDoc, pFormat, rRange, rPos, eType ),
375 : maPos(rPos),
376 : mpDoc(pDoc),
377 0 : mpLastEdit(NULL)
378 : {
379 0 : rtl::OUStringBuffer aTitle( GetText() );
380 0 : aTitle.append(rtl::OUString(" "));
381 0 : rtl::OUString aRangeString;
382 0 : rRange.Format(aRangeString, SCA_VALID, pDoc, pDoc->GetAddressConvention());
383 0 : aTitle.append(aRangeString);
384 0 : SetText(aTitle.makeStringAndClear());
385 0 : maBtnAdd.SetClickHdl( LINK( &maCondFormList, ScCondFormatList, AddBtnHdl ) );
386 0 : maBtnRemove.SetClickHdl( LINK( &maCondFormList, ScCondFormatList, RemoveBtnHdl ) );
387 0 : maEdRange.SetModifyHdl( LINK( this, ScCondFormatDlg, EdRangeModifyHdl ) );
388 0 : maEdRange.SetGetFocusHdl( LINK( this, ScCondFormatDlg, RangeGetFocusHdl ) );
389 0 : maEdRange.SetLoseFocusHdl( LINK( this, ScCondFormatDlg, RangeLoseFocusHdl ) );
390 0 : FreeResource();
391 :
392 0 : maEdRange.SetText(aRangeString);
393 :
394 0 : SC_MOD()->PushNewAnyRefDlg(this);
395 0 : }
396 :
397 0 : ScCondFormatDlg::~ScCondFormatDlg()
398 : {
399 0 : SC_MOD()->PopAnyRefDlg();
400 0 : }
401 :
402 0 : void ScCondFormatDlg::SetActive()
403 : {
404 0 : if(mpLastEdit)
405 0 : mpLastEdit->GrabFocus();
406 : else
407 0 : maEdRange.GrabFocus();
408 :
409 0 : RefInputDone();
410 0 : }
411 :
412 0 : void ScCondFormatDlg::RefInputDone( sal_Bool bForced )
413 : {
414 0 : ScAnyRefModalDlg::RefInputDone(bForced);
415 0 : }
416 :
417 0 : sal_Bool ScCondFormatDlg::IsTableLocked() const
418 : {
419 0 : if(mpLastEdit && mpLastEdit != &maEdRange)
420 0 : return sal_False;
421 :
422 0 : return sal_True;
423 : }
424 :
425 0 : sal_Bool ScCondFormatDlg::IsRefInputMode() const
426 : {
427 0 : return maEdRange.IsEnabled();
428 : }
429 :
430 : #define ABS_SREF SCA_VALID \
431 : | SCA_COL_ABSOLUTE | SCA_ROW_ABSOLUTE | SCA_TAB_ABSOLUTE
432 : #define ABS_DREF ABS_SREF \
433 : | SCA_COL2_ABSOLUTE | SCA_ROW2_ABSOLUTE | SCA_TAB2_ABSOLUTE
434 : #define ABS_DREF3D ABS_DREF | SCA_TAB_3D
435 :
436 0 : void ScCondFormatDlg::SetReference(const ScRange& rRef, ScDocument*)
437 : {
438 0 : formula::RefEdit* pEdit = mpLastEdit;
439 0 : if(!mpLastEdit)
440 0 : pEdit = &maEdRange;
441 :
442 0 : if( pEdit->IsEnabled() )
443 : {
444 0 : if(rRef.aStart != rRef.aEnd)
445 0 : RefInputStart(pEdit);
446 :
447 0 : rtl::OUString aRefStr;
448 0 : sal_uInt16 n = 0;
449 0 : if(mpLastEdit && mpLastEdit != &maEdRange)
450 0 : n = ABS_DREF3D;
451 : else
452 0 : n = ABS_DREF;
453 :
454 0 : rRef.Format( aRefStr, n, mpDoc, ScAddress::Details(mpDoc->GetAddressConvention(), 0, 0) );
455 0 : pEdit->SetRefString( aRefStr );
456 : }
457 0 : }
458 :
459 0 : ScConditionalFormat* ScCondFormatDlg::GetConditionalFormat() const
460 : {
461 0 : rtl::OUString aRangeStr = maEdRange.GetText();
462 0 : ScRangeList aRange;
463 0 : sal_uInt16 nFlags = aRange.Parse(aRangeStr, mpDoc, SCA_VALID, mpDoc->GetAddressConvention(), maPos.Tab());
464 0 : ScConditionalFormat* pFormat = maCondFormList.GetConditionalFormat();
465 :
466 0 : if(nFlags & SCA_VALID && !aRange.empty() && pFormat)
467 0 : pFormat->AddRange(aRange);
468 :
469 0 : return pFormat;
470 : }
471 :
472 0 : void ScCondFormatDlg::InvalidateRefData()
473 : {
474 0 : mpLastEdit = NULL;
475 0 : }
476 :
477 0 : IMPL_LINK( ScCondFormatDlg, EdRangeModifyHdl, Edit*, pEdit )
478 : {
479 0 : rtl::OUString aRangeStr = pEdit->GetText();
480 0 : ScRangeList aRange;
481 0 : sal_uInt16 nFlags = aRange.Parse(aRangeStr, mpDoc, SCA_VALID, mpDoc->GetAddressConvention());
482 0 : if(nFlags & SCA_VALID)
483 0 : pEdit->SetControlBackground(GetSettings().GetStyleSettings().GetWindowColor());
484 : else
485 0 : pEdit->SetControlBackground(COL_LIGHTRED);
486 0 : return 0;
487 : }
488 :
489 0 : IMPL_LINK( ScCondFormatDlg, RangeGetFocusHdl, formula::RefEdit*, pEdit )
490 : {
491 0 : mpLastEdit = pEdit;
492 0 : return 0;
493 : }
494 :
495 0 : IMPL_LINK_NOARG( ScCondFormatDlg, RangeLoseFocusHdl )
496 : {
497 : //mpLastEdit = NULL;
498 0 : return 0;
499 102 : }
500 :
501 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|