Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / condformat / condformatmgr.cxx
blob3990cfdc1f96304ba3d54b59a2fd67f07cc78290
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/.
8 */
10 #include "condformatmgr.hxx"
11 #include "condformatmgr.hrc"
12 #include "scresid.hxx"
13 #include "globstr.hrc"
14 #include "condformatdlg.hxx"
15 #include "vcl/msgbox.hxx"
16 #include "document.hxx"
18 #define ITEMID_RANGE 1
19 #define ITEMID_CONDITION 2
22 ScCondFormatManagerWindow::ScCondFormatManagerWindow(Window* pParent, ScDocument* pDoc, ScConditionalFormatList* pFormatList):
23 SvTabListBox(pParent, WB_SORT | WB_HSCROLL | WB_CLIPCHILDREN | WB_TABSTOP),
24 maHeaderBar( pParent, WB_BUTTONSTYLE | WB_BOTTOMBORDER ),
25 mpDoc(pDoc),
26 mpFormatList(pFormatList)
28 Size aBoxSize( pParent->GetOutputSizePixel() );
30 maHeaderBar.SetPosSizePixel( Point(0, 0), Size( aBoxSize.Width(), 16 ) );
32 OUString aConditionStr(ScGlobal::GetRscString(STR_HEADER_COND));
33 OUString aRangeStr(ScGlobal::GetRscString(STR_HEADER_RANGE));
35 long nTabSize = aBoxSize.Width()/2;
36 maHeaderBar.InsertItem( ITEMID_RANGE, aRangeStr, nTabSize, HIB_LEFT| HIB_VCENTER );
37 maHeaderBar.InsertItem( ITEMID_CONDITION, aConditionStr, nTabSize, HIB_LEFT| HIB_VCENTER );
39 static long nTabs[] = {2, 0, nTabSize };
40 Size aHeadSize( maHeaderBar.GetSizePixel() );
42 //pParent->SetFocusControl( this );
43 SetPosSizePixel( Point( 0, aHeadSize.Height() ), Size( aBoxSize.Width(), aBoxSize.Height() - aHeadSize.Height() ) );
44 SetTabs( &nTabs[0], MAP_PIXEL );
46 maHeaderBar.SetEndDragHdl( LINK(this, ScCondFormatManagerWindow, HeaderEndDragHdl ) );
47 HeaderEndDragHdl(NULL);
49 Init();
50 Show();
51 maHeaderBar.Show();
52 SetSelectionMode(MULTIPLE_SELECTION);
55 OUString ScCondFormatManagerWindow::createEntryString(const ScConditionalFormat& rFormat)
57 ScRangeList aRange = rFormat.GetRange();
58 OUString aStr;
59 aRange.Format(aStr, SCA_VALID, mpDoc, mpDoc->GetAddressConvention());
60 aStr += "\t";
61 aStr += ScCondFormatHelper::GetExpression(rFormat, aRange.GetTopLeftCorner());
62 return aStr;
65 void ScCondFormatManagerWindow::Init()
67 SetUpdateMode(false);
69 for(ScConditionalFormatList::iterator itr = mpFormatList->begin(); itr != mpFormatList->end(); ++itr)
71 SvTreeListEntry* pEntry = InsertEntryToColumn( createEntryString(*itr), LIST_APPEND, 0xffff );
72 maMapLBoxEntryToCondIndex.insert(std::pair<SvTreeListEntry*,sal_Int32>(pEntry,itr->GetKey()));
74 SetUpdateMode(true);
77 void ScCondFormatManagerWindow::DeleteSelection()
79 if(GetSelectionCount())
81 for(SvTreeListEntry* pEntry = FirstSelected(); pEntry != NULL; pEntry = NextSelected(pEntry))
83 sal_Int32 nIndex = maMapLBoxEntryToCondIndex.find(pEntry)->second;
84 mpFormatList->erase(nIndex);
86 RemoveSelection();
90 ScConditionalFormat* ScCondFormatManagerWindow::GetSelection()
92 SvTreeListEntry* pEntry = FirstSelected();
93 if(!pEntry)
94 return NULL;
96 sal_Int32 nIndex = maMapLBoxEntryToCondIndex.find(pEntry)->second;
97 return mpFormatList->GetFormat(nIndex);
100 void ScCondFormatManagerWindow::Update()
102 Clear();
103 maMapLBoxEntryToCondIndex.clear();
104 Init();
107 IMPL_LINK_NOARG(ScCondFormatManagerWindow, HeaderEndDragHdl)
109 long aTableSize = maHeaderBar.GetSizePixel().Width();
110 long aItemRangeSize = maHeaderBar.GetItemSize(ITEMID_RANGE);
112 //calculate column size based on user input and minimum size
113 long aItemCondSize = aTableSize - aItemRangeSize;
115 Size aSz;
116 aSz.Width() = aItemRangeSize;
117 SetTab( ITEMID_RANGE, PixelToLogic( aSz, MapMode(MAP_APPFONT) ).Width(), MAP_APPFONT );
118 maHeaderBar.SetItemSize(ITEMID_RANGE, aItemRangeSize);
119 aSz.Width() += aItemCondSize;
120 SetTab( ITEMID_CONDITION, PixelToLogic( aSz, MapMode(MAP_APPFONT) ).Width(), MAP_APPFONT );
121 maHeaderBar.SetItemSize(ITEMID_CONDITION, aItemCondSize);
123 return 0;
126 ScCondFormatManagerCtrl::ScCondFormatManagerCtrl(Window* pParent, ScDocument* pDoc, ScConditionalFormatList* pFormatList):
127 Control(pParent, ScResId(CTRL_TABLE)),
128 maWdManager(this, pDoc, pFormatList)
132 ScConditionalFormat* ScCondFormatManagerCtrl::GetSelection()
134 return maWdManager.GetSelection();
137 void ScCondFormatManagerCtrl::DeleteSelection()
139 maWdManager.DeleteSelection();
142 void ScCondFormatManagerCtrl::Update()
144 maWdManager.Update();
147 ScCondFormatManagerDlg::ScCondFormatManagerDlg(Window* pParent, ScDocument* pDoc, const ScConditionalFormatList* pFormatList, const ScAddress& rPos):
148 ModalDialog(pParent, ScResId(RID_SCDLG_COND_FORMAT_MANAGER)),
149 maBtnAdd(this, ScResId(BTN_ADD)),
150 maBtnRemove(this, ScResId(BTN_REMOVE)),
151 maBtnEdit(this, ScResId(BTN_EDIT)),
152 maBtnOk(this, ScResId(BTN_OK)),
153 maBtnCancel(this, ScResId(BTN_CANCEL)),
154 maFlLine(this, ScResId(FL_LINE)),
155 mpFormatList( pFormatList ? new ScConditionalFormatList(*pFormatList) : NULL),
156 maCtrlManager(this, pDoc, mpFormatList),
157 mpDoc(pDoc),
158 maPos(rPos),
159 mbModified(false)
161 FreeResource();
163 maBtnRemove.SetClickHdl(LINK(this, ScCondFormatManagerDlg, RemoveBtnHdl));
164 maBtnEdit.SetClickHdl(LINK(this, ScCondFormatManagerDlg, EditBtnHdl));
165 maBtnAdd.SetClickHdl(LINK(this, ScCondFormatManagerDlg, AddBtnHdl));
166 maCtrlManager.GetListControl().SetDoubleClickHdl(LINK(this, ScCondFormatManagerDlg, EditBtnHdl));
169 ScCondFormatManagerDlg::~ScCondFormatManagerDlg()
171 delete mpFormatList;
174 bool ScCondFormatManagerDlg::IsInRefMode() const
176 return true;
179 ScConditionalFormatList* ScCondFormatManagerDlg::GetConditionalFormatList()
181 ScConditionalFormatList* pList = mpFormatList;
182 mpFormatList = NULL;
183 return pList;
186 bool ScCondFormatManagerDlg::CondFormatsChanged()
188 return mbModified;
191 IMPL_LINK_NOARG(ScCondFormatManagerDlg, RemoveBtnHdl)
193 maCtrlManager.DeleteSelection();
194 mbModified = true;
195 return 0;
198 IMPL_LINK_NOARG(ScCondFormatManagerDlg, EditBtnHdl)
200 ScConditionalFormat* pFormat = maCtrlManager.GetSelection();
202 if(!pFormat)
203 return 0;
205 sal_uInt16 nId = 1;
206 ScModule* pScMod = SC_MOD();
207 pScMod->SetRefDialog( nId, true );
208 boost::scoped_ptr<ScCondFormatDlg> pDlg(new ScCondFormatDlg(this, mpDoc, pFormat, pFormat->GetRange(),
209 pFormat->GetRange().GetTopLeftCorner(), condformat::dialog::NONE));
210 Show(false, 0);
211 if(pDlg->Execute() == RET_OK)
213 sal_Int32 nKey = pFormat->GetKey();
214 mpFormatList->erase(nKey);
215 ScConditionalFormat* pNewFormat = pDlg->GetConditionalFormat();
216 if(pNewFormat)
218 pNewFormat->SetKey(nKey);
219 mpFormatList->InsertNew(pNewFormat);
222 maCtrlManager.Update();
223 mbModified = true;
225 Show(true, 0);
227 pScMod->SetRefDialog( nId, false );
229 return 0;
232 namespace {
234 sal_uInt32 FindKey(ScConditionalFormatList* pFormatList)
236 sal_uInt32 nKey = 0;
237 for(ScConditionalFormatList::const_iterator itr = pFormatList->begin(), itrEnd = pFormatList->end();
238 itr != itrEnd; ++itr)
240 if(itr->GetKey() > nKey)
241 nKey = itr->GetKey();
244 return nKey + 1;
249 IMPL_LINK_NOARG(ScCondFormatManagerDlg, AddBtnHdl)
251 sal_uInt16 nId = 1;
252 ScModule* pScMod = SC_MOD();
253 pScMod->SetRefDialog( nId, true );
254 boost::scoped_ptr<ScCondFormatDlg> pDlg(new ScCondFormatDlg(this, mpDoc, NULL, ScRangeList(),
255 maPos, condformat::dialog::CONDITION));
256 Show(false, 0);
257 if(pDlg->Execute() == RET_OK)
259 ScConditionalFormat* pNewFormat = pDlg->GetConditionalFormat();
260 if(pNewFormat)
262 mpFormatList->InsertNew(pNewFormat);
263 pNewFormat->SetKey(FindKey(mpFormatList));
264 maCtrlManager.Update();
266 mbModified = true;
269 Show(true, 0);
270 pScMod->SetRefDialog( nId, false );
272 return 0;
276 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */