sync master with lastest vba changes
[ooovba.git] / sc / source / ui / miscdlgs / datafdlg.cxx
blob619355c19ba9b047d71bd9c0b9a05a040cdde380
1 /*************************************************************************
3 * OpenOffice.org - a multi-platform office productivity suite
5 * $RCSfile: datafdlg.cxx,v $
7 * $Revision: 1.00 $
9 * last change: $Author: kz $ $Date: 2006/07/21 14:04:33 $
11 * The Contents of this file are made available subject to
12 * the terms of GNU Lesser General Public License Version 2.1.
15 * GNU Lesser General Public License Version 2.1
16 * =============================================
17 * Copyright 2005 by Sun Microsystems, Inc.
18 * 901 San Antonio Road, Palo Alto, CA 94303, USA
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License version 2.1, as published by the Free Software Foundation.
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * Lesser General Public License for more details.
29 * You should have received a copy of the GNU Lesser General Public
30 * License along with this library; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 * MA 02111-1307 USA
34 ************************************************************************/
36 // MARKER(update_precomp.py): autogen include statement, do not remove
37 #include "precompiled_sc.hxx"
39 #undef SC_DLLIMPLEMENTATION
41 //------------------------------------------------------------------
43 #include "datafdlg.hxx"
44 #include "scresid.hxx"
45 #include "datafdlg.hrc"
46 #include "viewdata.hxx"
47 //#include "document.hxx"
48 #include "docsh.hxx"
49 #include "refundo.hxx"
50 #include "undodat.hxx"
52 #define HDL(hdl) LINK( this, ScDataFormDlg, hdl )
54 //zhangyun
55 ScDataFormDlg::ScDataFormDlg( Window* pParent, ScTabViewShell* pTabViewShellOri) :
56 ModalDialog ( pParent, ScResId( RID_SCDLG_DATAFORM ) ),
58 aBtnNew ( this, ScResId( BTN_DATAFORM_NEW ) ),
59 aBtnDelete ( this, ScResId( BTN_DATAFORM_DELETE ) ),
60 aBtnRestore ( this, ScResId( BTN_DATAFORM_RESTORE ) ),
61 aBtnLast ( this, ScResId( BTN_DATAFORM_LAST ) ),
62 aBtnNext ( this, ScResId( BTN_DATAFORM_NEXT ) ),
63 aBtnClose ( this, ScResId( BTN_DATAFORM_CLOSE ) ),
64 aSlider ( this, ScResId( WND_DATAFORM_SCROLLBAR ) ),
65 aFixedText ( this, ScResId( LAB_DATAFORM_RECORDNO ) )
67 pTabViewShell = pTabViewShellOri;
68 FreeResource();
69 //read header form current document, and add new controls
70 DBG_ASSERT( pTabViewShell, "pTabViewShell is NULL! :-/" );
71 ScViewData* pViewData = pTabViewShell->GetViewData();
73 pDoc = pViewData->GetDocument();
74 if (pDoc)
76 ScRange aRange;
77 pViewData->GetSimpleArea( aRange );
78 ScAddress aStart = aRange.aStart;
79 ScAddress aEnd = aRange.aEnd;
81 nStartCol = aStart.Col();
82 nEndCol = aEnd.Col();
83 nStartRow = aStart.Row();
84 nEndRow = aEnd.Row();
86 nTab = pViewData->GetTabNo();
87 //if there is no selection
88 if ((nStartCol == nEndCol) && (nStartRow == nEndRow))
89 bNoSelection = TRUE;
91 if (bNoSelection)
93 //find last not blank cell in row
94 for (int i=1;i<=MAX_DATAFORM_COLS;i++)
96 String aColName;
97 nEndCol++;
98 pDoc->GetString( nEndCol, nStartRow, nTab, aColName );
99 int nColWidth = pDoc->GetColWidth( nEndCol, nTab );
100 if ( aColName.Len() == 0 && nColWidth)
102 nEndCol--;
103 break;
107 //find first not blank cell in row
108 for (int i=1;i<=MAX_DATAFORM_COLS;i++)
110 String aColName;
111 if (nStartCol <= 0)
112 break;
113 nStartCol--;
115 pDoc->GetString( nStartCol, nStartRow, nTab, aColName );
116 int nColWidth = pDoc->GetColWidth( nEndCol, nTab );
117 if ( aColName.Len() == 0 && nColWidth)
119 nStartCol++;
120 break;
124 //skip leading hide column
125 for (int i=1;i<=MAX_DATAFORM_COLS;i++)
127 String aColName;
128 int nColWidth = pDoc->GetColWidth( nStartCol, nTab );
129 if (nColWidth)
130 break;
131 nStartCol++;
134 if (nEndCol < nStartCol)
135 nEndCol = nStartCol;
137 //find last not blank cell in row
138 for (int i=1;i<=MAX_DATAFORM_ROWS;i++)
140 String aColName;
141 nEndRow++;
142 pDoc->GetString( nStartCol, nEndRow, nTab, aColName );
143 if ( aColName.Len() == 0 )
145 nEndRow--;
146 break;
150 //find first not blank cell in row
151 for (int i=1;i<=MAX_DATAFORM_ROWS;i++)
153 String aColName;
154 if (nStartRow <= 0)
155 break;
156 nStartRow--;
158 pDoc->GetString( nStartCol, nStartRow, nTab, aColName );
159 if ( aColName.Len() == 0 )
161 nStartRow++;
162 break;
166 if (nEndRow < nStartRow)
167 nEndRow = nStartRow;
170 aCurrentRow = nStartRow + 1;
172 String aFieldName;
174 int nTop = 12;
176 Size nFixedSize(FIXED_WIDTH, CTRL_HEIGHT );
177 Size nEditSize(EDIT_WIDTH, CTRL_HEIGHT );
179 //pFtArray = new FixedText(this);
180 aColLength = nEndCol - nStartCol + 1;
182 //new the controls
183 pFixedTexts = new FixedText*[aColLength];
184 pEdits = new Edit*[aColLength];
186 for(sal_uInt16 nIndex = 0; nIndex < aColLength; nIndex++)
188 pDoc->GetString( nIndex + nStartCol, nStartRow, nTab, aFieldName );
189 int nColWidth = pDoc->GetColWidth( nIndex + nStartCol, nTab );
190 if (nColWidth)
192 pFixedTexts[nIndex] = new FixedText(this);
193 pEdits[nIndex] = new Edit(this, WB_BORDER);
195 pFixedTexts[nIndex]->SetSizePixel(nFixedSize);
196 pEdits[nIndex]->SetSizePixel(nEditSize);
197 pFixedTexts[nIndex]->SetPosPixel(Point(FIXED_LEFT, nTop));
198 pEdits[nIndex]->SetPosPixel(Point(EDIT_LEFT, nTop));
199 //pFixedTexts[nIndex]->SetText(String::CreateFromAscii("W4W-Filter Nr. "));
200 pFixedTexts[nIndex]->SetText(aFieldName);
201 pFixedTexts[nIndex]->Show();
202 pEdits[nIndex]->Show();
204 nTop += LINE_HEIGHT;
206 else
208 pFixedTexts[nIndex] = NULL;
209 pEdits[nIndex] = NULL;
211 pEdits[nIndex]->SetModifyHdl( HDL(Impl_DataModifyHdl) );
214 Size nDialogSize = this->GetSizePixel();
215 if (nTop > nDialogSize.Height())
217 nDialogSize.setHeight(nTop);
218 this->SetSizePixel(nDialogSize);
220 Size nScrollSize = aSlider.GetSizePixel();
221 nScrollSize.setHeight(nDialogSize.Height()-20);
222 aSlider.SetSizePixel(nScrollSize);
225 FillCtrls(aCurrentRow);
227 aSlider.SetPageSize( 10 );
228 aSlider.SetVisibleSize( 1 );
229 aSlider.SetLineSize( 1 );
230 aSlider.SetRange( Range( 0, nEndRow - nStartRow + 1) );
231 aSlider.Show();
233 aBtnNew.SetClickHdl ( HDL(Impl_NewHdl) );
234 aBtnLast.SetClickHdl ( HDL(Impl_LastHdl) );
235 aBtnNext.SetClickHdl ( HDL(Impl_NextHdl) );
237 aBtnRestore.SetClickHdl ( HDL(Impl_RestoreHdl) );
238 aBtnDelete.SetClickHdl ( HDL(Impl_DeleteHdl) );
239 aBtnClose.SetClickHdl ( HDL(Impl_CloseHdl) );
241 aSlider.SetEndScrollHdl( HDL( Impl_ScrollHdl ) );
243 SetButtonState();
245 //end
246 //FreeResource();
249 ScDataFormDlg::~ScDataFormDlg()
251 for(sal_uInt16 i = 0; i < aColLength; i++)
253 if (pEdits[i])
254 delete pEdits[i];
255 if (pFixedTexts[i])
256 delete pFixedTexts[i];
258 if (pEdits)
259 delete pEdits;
260 if (pFixedTexts)
261 delete pFixedTexts;
264 void ScDataFormDlg::FillCtrls(SCROW /*nCurrentRow*/)
266 //ScViewData* pViewData = pTabViewShell->GetViewData();
268 //pDoc = pViewData->GetDocument();
269 String aFieldName;
270 int nRecordNum = nEndRow - nStartRow;
271 for(sal_uInt16 i = 0; i < aColLength; i++)
273 if (pEdits[i])
275 if (aCurrentRow<=nEndRow)
277 pDoc->GetString( i + nStartCol, aCurrentRow, nTab, aFieldName );
278 pEdits[i]->SetText(aFieldName);
280 else
281 pEdits[i]->SetText(String());
284 char sRecordStr[256];
285 if (aCurrentRow<=nEndRow)
286 aFixedText.SetText(String::CreateFromAscii(sRecordStr));
287 else
288 aFixedText.SetText(String::CreateFromAscii("New Record"));
290 aSlider.SetThumbPos(aCurrentRow-nStartRow-1);
293 IMPL_LINK( ScDataFormDlg, Impl_DataModifyHdl, Edit*, pEdit)
295 if ( pEdit->IsModified() )
296 aBtnRestore.Enable( TRUE );
297 return 0;
300 IMPL_LINK( ScDataFormDlg, Impl_NewHdl, PushButton*, EMPTYARG )
302 ScViewData* pViewData = pTabViewShell->GetViewData();
303 ScDocShell* pDocSh = pViewData->GetDocShell();
304 if ( pDoc )
306 sal_Bool bHasData = sal_False;
307 for(sal_uInt16 i = 0; i < aColLength; i++)
308 if (pEdits[i])
309 if ( pEdits[i]->GetText().Len() != 0 )
311 bHasData = sal_True;
312 break;
315 if ( bHasData )
317 pTabViewShell->DataFormPutData( aCurrentRow , nStartRow , nStartCol , nEndRow , nEndCol , pEdits , aColLength );
318 aCurrentRow++;
319 if (aCurrentRow >= nEndRow + 2)
321 nEndRow ++ ;
322 aSlider.SetRange( Range( 0, nEndRow - nStartRow + 1) );
324 SetButtonState();
325 FillCtrls(aCurrentRow);
326 pDocSh->SetDocumentModified();
327 pDocSh->PostPaintGridAll();
330 return 0;
333 IMPL_LINK( ScDataFormDlg, Impl_LastHdl, PushButton*, EMPTYARG )
335 if (pDoc)
337 if ( aCurrentRow > nStartRow +1 )
338 aCurrentRow--;
340 SetButtonState();
341 FillCtrls(aCurrentRow);
343 return 0;
346 IMPL_LINK( ScDataFormDlg, Impl_NextHdl, PushButton*, EMPTYARG )
348 if (pDoc)
350 if ( aCurrentRow <= nEndRow)
351 aCurrentRow++;
353 SetButtonState();
354 FillCtrls(aCurrentRow);
356 return 0;
359 IMPL_LINK( ScDataFormDlg, Impl_RestoreHdl, PushButton*, EMPTYARG )
361 if (pDoc)
363 FillCtrls(aCurrentRow);
365 return 0;
368 IMPL_LINK( ScDataFormDlg, Impl_DeleteHdl, PushButton*, EMPTYARG )
370 ScViewData* pViewData = pTabViewShell->GetViewData();
371 ScDocShell* pDocSh = pViewData->GetDocShell();
372 if (pDoc)
374 ScRange aRange(nStartCol, aCurrentRow, nTab, nEndCol, aCurrentRow, nTab);
375 pDoc->DeleteRow(aRange);
376 nEndRow--;
378 SetButtonState();
379 pDocSh->GetUndoManager()->Clear();
381 FillCtrls(aCurrentRow);
382 pDocSh->SetDocumentModified();
383 pDocSh->PostPaintGridAll();
385 return 0;
388 IMPL_LINK( ScDataFormDlg, Impl_CloseHdl, PushButton*, EMPTYARG )
390 EndDialog( );
391 return 0;
394 IMPL_LINK( ScDataFormDlg, Impl_ScrollHdl, ScrollBar*, EMPTYARG )
396 long nOffset = aSlider.GetThumbPos();
397 aCurrentRow = nStartRow + nOffset + 1;
398 SetButtonState();
399 FillCtrls(aCurrentRow);
400 return 0;
403 void ScDataFormDlg::SetButtonState()
405 if ( aCurrentRow > nEndRow )
407 aBtnDelete.Enable( FALSE );
408 aBtnLast.Enable( TRUE );
409 aBtnNext.Enable( FALSE );
411 else
413 aBtnDelete.Enable( TRUE );
414 aBtnNext.Enable( TRUE );
416 if ( 1 == aCurrentRow )
417 aBtnLast.Enable( FALSE );
419 aBtnRestore.Enable( FALSE );
420 if ( pEdits )
421 pEdits[0]->GrabFocus();