Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / miscdlgs / datafdlg.cxx
blob62fcaafa718770bbce9e70bd53ef3e3c6461b0f8
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 #undef SC_DLLIMPLEMENTATION
12 //------------------------------------------------------------------
14 #include "datafdlg.hxx"
15 #include "scresid.hxx"
16 #include "viewdata.hxx"
17 #include "docsh.hxx"
18 #include "refundo.hxx"
19 #include "undodat.hxx"
21 #include "rtl/ustrbuf.hxx"
23 #define HDL(hdl) LINK( this, ScDataFormDlg, hdl )
26 ScDataFormDlg::ScDataFormDlg(Window* pParent, ScTabViewShell* pTabViewShellOri)
27 : ModalDialog(pParent, "DataFormDialog", "modules/scalc/ui/dataform.ui")
28 , pTabViewShell(pTabViewShellOri)
30 get(m_pBtnNew, "new");
31 get(m_pBtnDelete, "delete");
32 get(m_pBtnRestore, "restore");
33 get(m_pBtnPrev, "prev");
34 get(m_pBtnNext, "next");
35 get(m_pBtnClose, "close");
36 get(m_pFixedText, "label");
37 sNewRecord = m_pFixedText->GetText();
38 get(m_pSlider, "scrollbar");
39 get(m_pGrid, "grid");
41 //read header form current document, and add new controls
42 OSL_ENSURE( pTabViewShell, "pTabViewShell is NULL! :-/" );
43 ScViewData* pViewData = pTabViewShell->GetViewData();
45 pDoc = pViewData->GetDocument();
46 if (pDoc)
48 ScRange aRange;
49 pViewData->GetSimpleArea( aRange );
50 ScAddress aStart = aRange.aStart;
51 ScAddress aEnd = aRange.aEnd;
53 nStartCol = aStart.Col();
54 nEndCol = aEnd.Col();
55 nStartRow = aStart.Row();
56 nEndRow = aEnd.Row();
58 nTab = pViewData->GetTabNo();
59 //if there is no selection
60 if ((nStartCol == nEndCol) && (nStartRow == nEndRow))
61 bNoSelection = true;
63 if (bNoSelection)
65 //find last not blank cell in row
66 for (int i=1;i<=MAX_DATAFORM_COLS;i++)
68 nEndCol++;
69 OUString aColName = pDoc->GetString(nEndCol, nStartRow, nTab);
70 int nColWidth = pDoc->GetColWidth( nEndCol, nTab );
71 if (aColName.isEmpty() && nColWidth)
73 nEndCol--;
74 break;
78 //find first not blank cell in row
79 for (int i=1;i<=MAX_DATAFORM_COLS;i++)
81 if (nStartCol <= 0)
82 break;
83 nStartCol--;
85 OUString aColName = pDoc->GetString(nStartCol, nStartRow, nTab);
86 int nColWidth = pDoc->GetColWidth( nEndCol, nTab );
87 if (aColName.isEmpty() && nColWidth)
89 nStartCol++;
90 break;
94 //skip leading hide column
95 for (int i=1;i<=MAX_DATAFORM_COLS;i++)
97 int nColWidth = pDoc->GetColWidth( nStartCol, nTab );
98 if (nColWidth)
99 break;
100 nStartCol++;
103 if (nEndCol < nStartCol)
104 nEndCol = nStartCol;
106 //find last not blank cell in row
107 for (int i=1;i<=MAX_DATAFORM_ROWS;i++)
109 nEndRow++;
110 OUString aColName = pDoc->GetString(nStartCol, nEndRow, nTab);
111 if (aColName.isEmpty())
113 nEndRow--;
114 break;
118 //find first not blank cell in row
119 for (int i=1;i<=MAX_DATAFORM_ROWS;i++)
121 if (nStartRow <= 0)
122 break;
123 nStartRow--;
125 OUString aColName = pDoc->GetString(nStartCol, nStartRow, nTab);
126 if (aColName.isEmpty())
128 nStartRow++;
129 break;
133 if (nEndRow < nStartRow)
134 nEndRow = nStartRow;
137 nCurrentRow = nStartRow + 1;
139 aColLength = nEndCol - nStartCol + 1;
141 //new the controls
142 maFixedTexts.reserve(aColLength);
143 maEdits.reserve(aColLength);
145 sal_Int32 nGridRow = 0;
146 for(sal_uInt16 nIndex = 0; nIndex < aColLength; ++nIndex)
148 OUString aFieldName = pDoc->GetString(nIndex + nStartCol, nStartRow, nTab);
149 int nColWidth = pDoc->GetColWidth( nIndex + nStartCol, nTab );
150 if (nColWidth)
152 maFixedTexts.push_back( new FixedText(m_pGrid) );
153 maEdits.push_back( new Edit(m_pGrid, WB_BORDER) );
155 maFixedTexts[nIndex].set_grid_left_attach(0);
156 maEdits[nIndex].set_grid_left_attach(1);
157 maFixedTexts[nIndex].set_grid_top_attach(nGridRow);
158 maEdits[nIndex].set_grid_top_attach(nGridRow);
160 maEdits[nIndex].SetWidthInChars(32);
161 maEdits[nIndex].set_hexpand(true);
163 ++nGridRow;
165 maFixedTexts[nIndex].SetText(aFieldName);
166 maFixedTexts[nIndex].Show();
167 maEdits[nIndex].Show();
169 else
171 maFixedTexts.push_back( NULL );
172 maEdits.push_back( NULL );
174 if (!maEdits.is_null(nIndex))
175 maEdits[nIndex].SetModifyHdl( HDL(Impl_DataModifyHdl) );
179 FillCtrls(nCurrentRow);
181 m_pSlider->SetPageSize( 10 );
182 m_pSlider->SetVisibleSize( 1 );
183 m_pSlider->SetLineSize( 1 );
184 m_pSlider->SetRange( Range( 0, nEndRow - nStartRow + 1) );
185 m_pSlider->Show();
187 m_pBtnNew->SetClickHdl(HDL(Impl_NewHdl));
188 m_pBtnPrev->SetClickHdl(HDL(Impl_PrevHdl));
189 m_pBtnNext->SetClickHdl(HDL(Impl_NextHdl));
191 m_pBtnRestore->SetClickHdl(HDL(Impl_RestoreHdl));
192 m_pBtnDelete->SetClickHdl(HDL(Impl_DeleteHdl));
193 m_pBtnClose->SetClickHdl(HDL(Impl_CloseHdl));
195 m_pSlider->SetEndScrollHdl(HDL(Impl_ScrollHdl));
197 SetButtonState();
200 ScDataFormDlg::~ScDataFormDlg()
205 void ScDataFormDlg::FillCtrls(SCROW /*nCurrentRow*/)
207 OUString aFieldName;
208 for (sal_uInt16 i = 0; i < aColLength; ++i)
210 if (!maEdits.is_null(i))
212 if (nCurrentRow<=nEndRow)
214 aFieldName = pDoc->GetString(i + nStartCol, nCurrentRow, nTab);
215 maEdits[i].SetText(aFieldName);
217 else
218 maEdits[i].SetText(OUString());
222 if (nCurrentRow <= nEndRow)
224 OUStringBuffer aBuf;
225 aBuf.append(static_cast<sal_Int32>(nCurrentRow - nStartRow));
226 aBuf.appendAscii(" / ");
227 aBuf.append(static_cast<sal_Int32>(nEndRow - nStartRow));
228 m_pFixedText->SetText(aBuf.makeStringAndClear());
230 else
231 m_pFixedText->SetText(sNewRecord);
233 m_pSlider->SetThumbPos(nCurrentRow-nStartRow-1);
236 IMPL_LINK( ScDataFormDlg, Impl_DataModifyHdl, Edit*, pEdit)
238 if ( pEdit->IsModified() )
239 m_pBtnRestore->Enable( true );
240 return 0;
243 IMPL_LINK_NOARG(ScDataFormDlg, Impl_NewHdl)
245 ScViewData* pViewData = pTabViewShell->GetViewData();
246 ScDocShell* pDocSh = pViewData->GetDocShell();
247 if ( pDoc )
249 bool bHasData = false;
250 boost::ptr_vector<Edit>::iterator itr = maEdits.begin(), itrEnd = maEdits.end();
251 for(; itr != itrEnd; ++itr)
252 if (!boost::is_null(itr))
253 if ( !(*itr).GetText().isEmpty() )
255 bHasData = true;
256 break;
259 if ( bHasData )
261 pTabViewShell->DataFormPutData( nCurrentRow , nStartRow , nStartCol , nEndRow , nEndCol , maEdits , aColLength );
262 nCurrentRow++;
263 if (nCurrentRow >= nEndRow + 2)
265 nEndRow ++ ;
266 m_pSlider->SetRange( Range( 0, nEndRow - nStartRow + 1) );
268 SetButtonState();
269 FillCtrls(nCurrentRow);
270 pDocSh->SetDocumentModified();
271 pDocSh->PostPaintGridAll();
274 return 0;
277 IMPL_LINK_NOARG(ScDataFormDlg, Impl_PrevHdl)
279 if (pDoc)
281 if ( nCurrentRow > nStartRow +1 )
282 nCurrentRow--;
284 SetButtonState();
285 FillCtrls(nCurrentRow);
287 return 0;
290 IMPL_LINK_NOARG(ScDataFormDlg, Impl_NextHdl)
292 if (pDoc)
294 if ( nCurrentRow <= nEndRow)
295 nCurrentRow++;
297 SetButtonState();
298 FillCtrls(nCurrentRow);
300 return 0;
303 IMPL_LINK_NOARG(ScDataFormDlg, Impl_RestoreHdl)
305 if (pDoc)
307 FillCtrls(nCurrentRow);
309 return 0;
312 IMPL_LINK_NOARG(ScDataFormDlg, Impl_DeleteHdl)
314 ScViewData* pViewData = pTabViewShell->GetViewData();
315 ScDocShell* pDocSh = pViewData->GetDocShell();
316 if (pDoc)
318 ScRange aRange(nStartCol, nCurrentRow, nTab, nEndCol, nCurrentRow, nTab);
319 pDoc->DeleteRow(aRange);
320 nEndRow--;
322 SetButtonState();
323 pDocSh->GetUndoManager()->Clear();
325 FillCtrls(nCurrentRow);
326 pDocSh->SetDocumentModified();
327 pDocSh->PostPaintGridAll();
329 return 0;
332 IMPL_LINK_NOARG(ScDataFormDlg, Impl_CloseHdl)
334 EndDialog( );
335 return 0;
338 IMPL_LINK_NOARG(ScDataFormDlg, Impl_ScrollHdl)
340 long nOffset = m_pSlider->GetThumbPos();
341 nCurrentRow = nStartRow + nOffset + 1;
342 SetButtonState();
343 FillCtrls(nCurrentRow);
344 return 0;
347 void ScDataFormDlg::SetButtonState()
349 if (nCurrentRow > nEndRow)
351 m_pBtnDelete->Enable( false );
352 m_pBtnNext->Enable( false );
354 else
356 m_pBtnDelete->Enable( true );
357 m_pBtnNext->Enable( true );
360 if (nCurrentRow == nStartRow + 1)
361 m_pBtnPrev->Enable( false );
362 else
363 m_pBtnPrev->Enable( true );
365 m_pBtnRestore->Enable( false );
366 if ( maEdits.size()>=1 && !maEdits.is_null(0) )
367 maEdits[0].GrabFocus();
370 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */