1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #undef SC_DLLIMPLEMENTATION
12 //------------------------------------------------------------------
14 #include "datafdlg.hxx"
15 #include "scresid.hxx"
16 #include "viewdata.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");
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();
49 pViewData
->GetSimpleArea( aRange
);
50 ScAddress aStart
= aRange
.aStart
;
51 ScAddress aEnd
= aRange
.aEnd
;
53 nStartCol
= aStart
.Col();
55 nStartRow
= aStart
.Row();
58 nTab
= pViewData
->GetTabNo();
59 //if there is no selection
60 if ((nStartCol
== nEndCol
) && (nStartRow
== nEndRow
))
65 //find last not blank cell in row
66 for (int i
=1;i
<=MAX_DATAFORM_COLS
;i
++)
69 OUString aColName
= pDoc
->GetString(nEndCol
, nStartRow
, nTab
);
70 int nColWidth
= pDoc
->GetColWidth( nEndCol
, nTab
);
71 if (aColName
.isEmpty() && nColWidth
)
78 //find first not blank cell in row
79 for (int i
=1;i
<=MAX_DATAFORM_COLS
;i
++)
85 OUString aColName
= pDoc
->GetString(nStartCol
, nStartRow
, nTab
);
86 int nColWidth
= pDoc
->GetColWidth( nEndCol
, nTab
);
87 if (aColName
.isEmpty() && nColWidth
)
94 //skip leading hide column
95 for (int i
=1;i
<=MAX_DATAFORM_COLS
;i
++)
97 int nColWidth
= pDoc
->GetColWidth( nStartCol
, nTab
);
103 if (nEndCol
< nStartCol
)
106 //find last not blank cell in row
107 for (int i
=1;i
<=MAX_DATAFORM_ROWS
;i
++)
110 OUString aColName
= pDoc
->GetString(nStartCol
, nEndRow
, nTab
);
111 if (aColName
.isEmpty())
118 //find first not blank cell in row
119 for (int i
=1;i
<=MAX_DATAFORM_ROWS
;i
++)
125 OUString aColName
= pDoc
->GetString(nStartCol
, nStartRow
, nTab
);
126 if (aColName
.isEmpty())
133 if (nEndRow
< nStartRow
)
137 nCurrentRow
= nStartRow
+ 1;
139 aColLength
= nEndCol
- nStartCol
+ 1;
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
);
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);
165 maFixedTexts
[nIndex
].SetText(aFieldName
);
166 maFixedTexts
[nIndex
].Show();
167 maEdits
[nIndex
].Show();
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) );
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
));
200 ScDataFormDlg::~ScDataFormDlg()
205 void ScDataFormDlg::FillCtrls(SCROW
/*nCurrentRow*/)
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
);
218 maEdits
[i
].SetText(OUString());
222 if (nCurrentRow
<= nEndRow
)
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());
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 );
243 IMPL_LINK_NOARG(ScDataFormDlg
, Impl_NewHdl
)
245 ScViewData
* pViewData
= pTabViewShell
->GetViewData();
246 ScDocShell
* pDocSh
= pViewData
->GetDocShell();
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() )
261 pTabViewShell
->DataFormPutData( nCurrentRow
, nStartRow
, nStartCol
, nEndRow
, nEndCol
, maEdits
, aColLength
);
263 if (nCurrentRow
>= nEndRow
+ 2)
266 m_pSlider
->SetRange( Range( 0, nEndRow
- nStartRow
+ 1) );
269 FillCtrls(nCurrentRow
);
270 pDocSh
->SetDocumentModified();
271 pDocSh
->PostPaintGridAll();
277 IMPL_LINK_NOARG(ScDataFormDlg
, Impl_PrevHdl
)
281 if ( nCurrentRow
> nStartRow
+1 )
285 FillCtrls(nCurrentRow
);
290 IMPL_LINK_NOARG(ScDataFormDlg
, Impl_NextHdl
)
294 if ( nCurrentRow
<= nEndRow
)
298 FillCtrls(nCurrentRow
);
303 IMPL_LINK_NOARG(ScDataFormDlg
, Impl_RestoreHdl
)
307 FillCtrls(nCurrentRow
);
312 IMPL_LINK_NOARG(ScDataFormDlg
, Impl_DeleteHdl
)
314 ScViewData
* pViewData
= pTabViewShell
->GetViewData();
315 ScDocShell
* pDocSh
= pViewData
->GetDocShell();
318 ScRange
aRange(nStartCol
, nCurrentRow
, nTab
, nEndCol
, nCurrentRow
, nTab
);
319 pDoc
->DeleteRow(aRange
);
323 pDocSh
->GetUndoManager()->Clear();
325 FillCtrls(nCurrentRow
);
326 pDocSh
->SetDocumentModified();
327 pDocSh
->PostPaintGridAll();
332 IMPL_LINK_NOARG(ScDataFormDlg
, Impl_CloseHdl
)
338 IMPL_LINK_NOARG(ScDataFormDlg
, Impl_ScrollHdl
)
340 long nOffset
= m_pSlider
->GetThumbPos();
341 nCurrentRow
= nStartRow
+ nOffset
+ 1;
343 FillCtrls(nCurrentRow
);
347 void ScDataFormDlg::SetButtonState()
349 if (nCurrentRow
> nEndRow
)
351 m_pBtnDelete
->Enable( false );
352 m_pBtnNext
->Enable( false );
356 m_pBtnDelete
->Enable( true );
357 m_pBtnNext
->Enable( true );
360 if (nCurrentRow
== nStartRow
+ 1)
361 m_pBtnPrev
->Enable( false );
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: */