Branch data 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. You may obtain a copy of the License at
8 : : * 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 : : * The Initial Developer of the Original Code is
16 : : * Novell Inc.
17 : : * Portions created by the Initial Developer are Copyright (C) 2010 the
18 : : * Initial Developer. All Rights Reserved.
19 : : *
20 : : * Contributor(s): Amelia Wang <amwang@novell.com>
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 : :
30 : : #undef SC_DLLIMPLEMENTATION
31 : :
32 : : //------------------------------------------------------------------
33 : :
34 : : #include "datafdlg.hxx"
35 : : #include "scresid.hxx"
36 : : #include "datafdlg.hrc"
37 : : #include "viewdata.hxx"
38 : : #include "docsh.hxx"
39 : : #include "refundo.hxx"
40 : : #include "undodat.hxx"
41 : :
42 : : #include "rtl/ustrbuf.hxx"
43 : :
44 : : #define HDL(hdl) LINK( this, ScDataFormDlg, hdl )
45 : :
46 : : using ::rtl::OUStringBuffer;
47 : :
48 : : //zhangyun
49 : 0 : ScDataFormDlg::ScDataFormDlg( Window* pParent, ScTabViewShell* pTabViewShellOri) :
50 : : ModalDialog ( pParent, ScResId( RID_SCDLG_DATAFORM ) ),
51 : : aBtnNew ( this, ScResId( BTN_DATAFORM_NEW ) ),
52 : : aBtnDelete ( this, ScResId( BTN_DATAFORM_DELETE ) ),
53 : : aBtnRestore ( this, ScResId( BTN_DATAFORM_RESTORE ) ),
54 : : aBtnPrev ( this, ScResId( BTN_DATAFORM_PREV ) ),
55 : : aBtnNext ( this, ScResId( BTN_DATAFORM_NEXT ) ),
56 : : aBtnClose ( this, ScResId( BTN_DATAFORM_CLOSE ) ),
57 : : aSlider ( this, ScResId( WND_DATAFORM_SCROLLBAR ) ),
58 : 0 : aFixedText ( this, ScResId( LAB_DATAFORM_RECORDNO ) )
59 : : {
60 : 0 : pTabViewShell = pTabViewShellOri;
61 : 0 : FreeResource();
62 : : //read header form current document, and add new controls
63 : : OSL_ENSURE( pTabViewShell, "pTabViewShell is NULL! :-/" );
64 : 0 : ScViewData* pViewData = pTabViewShell->GetViewData();
65 : :
66 : 0 : pDoc = pViewData->GetDocument();
67 : 0 : if (pDoc)
68 : : {
69 : 0 : ScRange aRange;
70 : 0 : pViewData->GetSimpleArea( aRange );
71 : 0 : ScAddress aStart = aRange.aStart;
72 : 0 : ScAddress aEnd = aRange.aEnd;
73 : :
74 : 0 : nStartCol = aStart.Col();
75 : 0 : nEndCol = aEnd.Col();
76 : 0 : nStartRow = aStart.Row();
77 : 0 : nEndRow = aEnd.Row();
78 : :
79 : 0 : nTab = pViewData->GetTabNo();
80 : : //if there is no selection
81 : 0 : if ((nStartCol == nEndCol) && (nStartRow == nEndRow))
82 : 0 : bNoSelection = true;
83 : :
84 : 0 : if (bNoSelection)
85 : : {
86 : : //find last not blank cell in row
87 : 0 : for (int i=1;i<=MAX_DATAFORM_COLS;i++)
88 : : {
89 : 0 : String aColName;
90 : 0 : nEndCol++;
91 : 0 : pDoc->GetString( nEndCol, nStartRow, nTab, aColName );
92 : 0 : int nColWidth = pDoc->GetColWidth( nEndCol, nTab );
93 : 0 : if ( aColName.Len() == 0 && nColWidth)
94 : : {
95 : 0 : nEndCol--;
96 : : break;
97 : : }
98 : 0 : }
99 : :
100 : : //find first not blank cell in row
101 : 0 : for (int i=1;i<=MAX_DATAFORM_COLS;i++)
102 : : {
103 : 0 : String aColName;
104 : 0 : if (nStartCol <= 0)
105 : : break;
106 : 0 : nStartCol--;
107 : :
108 : 0 : pDoc->GetString( nStartCol, nStartRow, nTab, aColName );
109 : 0 : int nColWidth = pDoc->GetColWidth( nEndCol, nTab );
110 : 0 : if ( aColName.Len() == 0 && nColWidth)
111 : : {
112 : 0 : nStartCol++;
113 : : break;
114 : : }
115 : 0 : }
116 : :
117 : : //skip leading hide column
118 : 0 : for (int i=1;i<=MAX_DATAFORM_COLS;i++)
119 : : {
120 : 0 : int nColWidth = pDoc->GetColWidth( nStartCol, nTab );
121 : 0 : if (nColWidth)
122 : 0 : break;
123 : 0 : nStartCol++;
124 : : }
125 : :
126 : 0 : if (nEndCol < nStartCol)
127 : 0 : nEndCol = nStartCol;
128 : :
129 : : //find last not blank cell in row
130 : 0 : for (int i=1;i<=MAX_DATAFORM_ROWS;i++)
131 : : {
132 : 0 : String aColName;
133 : 0 : nEndRow++;
134 : 0 : pDoc->GetString( nStartCol, nEndRow, nTab, aColName );
135 : 0 : if ( aColName.Len() == 0 )
136 : : {
137 : 0 : nEndRow--;
138 : : break;
139 : : }
140 : 0 : }
141 : :
142 : : //find first not blank cell in row
143 : 0 : for (int i=1;i<=MAX_DATAFORM_ROWS;i++)
144 : : {
145 : 0 : String aColName;
146 : 0 : if (nStartRow <= 0)
147 : : break;
148 : 0 : nStartRow--;
149 : :
150 : 0 : pDoc->GetString( nStartCol, nStartRow, nTab, aColName );
151 : 0 : if ( aColName.Len() == 0 )
152 : : {
153 : 0 : nStartRow++;
154 : : break;
155 : : }
156 : 0 : }
157 : :
158 : 0 : if (nEndRow < nStartRow)
159 : 0 : nEndRow = nStartRow;
160 : : }
161 : :
162 : 0 : nCurrentRow = nStartRow + 1;
163 : :
164 : 0 : String aFieldName;
165 : :
166 : : //align with LAB_DATAFORM_RECORDNO
167 : 0 : int nTop = LogicToPixel( Size(1,6), MapMode(MAP_APPFONT) ).getHeight();
168 : 0 : const int nOne = LogicToPixel( Size(1,1), MapMode(MAP_APPFONT) ).getHeight();
169 : 0 : const int nLineHeight = LogicToPixel( Size(1, LINE_HEIGHT), MapMode(MAP_APPFONT) ).getHeight();
170 : 0 : const int nFixedLeft = LogicToPixel( Size(FIXED_LEFT, 1), MapMode(MAP_APPFONT) ).getWidth();
171 : 0 : const int nEditLeft = LogicToPixel( Size(EDIT_LEFT, 1), MapMode(MAP_APPFONT) ).getWidth();
172 : :
173 : 0 : Size nFixedSize(LogicToPixel( Size(FIXED_WIDTH, FIXED_HEIGHT), MapMode(MAP_APPFONT) ));
174 : 0 : Size nEditSize(LogicToPixel( Size(EDIT_WIDTH, EDIT_HEIGHT), MapMode(MAP_APPFONT) ));
175 : :
176 : 0 : aColLength = nEndCol - nStartCol + 1;
177 : :
178 : : //new the controls
179 : 0 : maFixedTexts.reserve(aColLength);
180 : 0 : maEdits.reserve(aColLength);
181 : :
182 : 0 : for(sal_uInt16 nIndex = 0; nIndex < aColLength; nIndex++)
183 : : {
184 : 0 : pDoc->GetString( nIndex + nStartCol, nStartRow, nTab, aFieldName );
185 : 0 : int nColWidth = pDoc->GetColWidth( nIndex + nStartCol, nTab );
186 : 0 : if (nColWidth)
187 : : {
188 : 0 : maFixedTexts.push_back( new FixedText(this) );
189 : 0 : maEdits.push_back( new Edit(this, WB_BORDER) );
190 : :
191 : 0 : maFixedTexts[nIndex].SetSizePixel(nFixedSize);
192 : 0 : maEdits[nIndex].SetSizePixel(nEditSize);
193 : 0 : maFixedTexts[nIndex].SetPosPixel(Point(nFixedLeft, nTop + nOne));
194 : 0 : maEdits[nIndex].SetPosPixel(Point(nEditLeft, nTop));
195 : 0 : maFixedTexts[nIndex].SetText(aFieldName);
196 : 0 : maFixedTexts[nIndex].Show();
197 : 0 : maEdits[nIndex].Show();
198 : :
199 : 0 : nTop += nLineHeight;
200 : : }
201 : : else
202 : : {
203 : 0 : maFixedTexts.push_back( NULL );
204 : 0 : maEdits.push_back( NULL );
205 : : }
206 : 0 : if (!maEdits.is_null(nIndex))
207 : 0 : maEdits[nIndex].SetModifyHdl( HDL(Impl_DataModifyHdl) );
208 : : }
209 : :
210 : 0 : Size nDialogSize = this->GetSizePixel();
211 : 0 : if (nTop > nDialogSize.Height())
212 : : {
213 : 0 : nDialogSize.setHeight(nTop);
214 : 0 : this->SetSizePixel(nDialogSize);
215 : : }
216 : 0 : Size nScrollSize = aSlider.GetSizePixel();
217 : 0 : nScrollSize.setHeight(nDialogSize.Height()-20);
218 : 0 : aSlider.SetSizePixel(nScrollSize);
219 : : }
220 : :
221 : 0 : FillCtrls(nCurrentRow);
222 : :
223 : 0 : aSlider.SetPageSize( 10 );
224 : 0 : aSlider.SetVisibleSize( 1 );
225 : 0 : aSlider.SetLineSize( 1 );
226 : 0 : aSlider.SetRange( Range( 0, nEndRow - nStartRow + 1) );
227 : 0 : aSlider.Show();
228 : :
229 : 0 : aBtnNew.SetClickHdl ( HDL(Impl_NewHdl) );
230 : 0 : aBtnPrev.SetClickHdl ( HDL(Impl_PrevHdl) );
231 : 0 : aBtnNext.SetClickHdl ( HDL(Impl_NextHdl) );
232 : :
233 : 0 : aBtnRestore.SetClickHdl ( HDL(Impl_RestoreHdl) );
234 : 0 : aBtnDelete.SetClickHdl ( HDL(Impl_DeleteHdl) );
235 : 0 : aBtnClose.SetClickHdl ( HDL(Impl_CloseHdl) );
236 : :
237 : 0 : aSlider.SetEndScrollHdl( HDL( Impl_ScrollHdl ) );
238 : :
239 : 0 : SetButtonState();
240 : 0 : }
241 : :
242 : 0 : ScDataFormDlg::~ScDataFormDlg()
243 : : {
244 : :
245 : 0 : }
246 : :
247 : 0 : void ScDataFormDlg::FillCtrls(SCROW /*nCurrentRow*/)
248 : : {
249 : 0 : String aFieldName;
250 : 0 : for (sal_uInt16 i = 0; i < aColLength; ++i)
251 : : {
252 : 0 : if (!maEdits.is_null(i))
253 : : {
254 : 0 : if (nCurrentRow<=nEndRow)
255 : : {
256 : 0 : pDoc->GetString( i + nStartCol, nCurrentRow, nTab, aFieldName );
257 : 0 : maEdits[i].SetText(aFieldName);
258 : : }
259 : : else
260 : 0 : maEdits[i].SetText(String());
261 : : }
262 : : }
263 : :
264 : 0 : if (nCurrentRow <= nEndRow)
265 : : {
266 : 0 : OUStringBuffer aBuf;
267 : 0 : aBuf.append(static_cast<sal_Int32>(nCurrentRow - nStartRow));
268 : 0 : aBuf.appendAscii(" / ");
269 : 0 : aBuf.append(static_cast<sal_Int32>(nEndRow - nStartRow));
270 : 0 : aFixedText.SetText(aBuf.makeStringAndClear());
271 : : }
272 : : else
273 : 0 : aFixedText.SetText(String(ScResId(STR_NEW_RECORD)));
274 : :
275 : 0 : aSlider.SetThumbPos(nCurrentRow-nStartRow-1);
276 : 0 : }
277 : :
278 : 0 : IMPL_LINK( ScDataFormDlg, Impl_DataModifyHdl, Edit*, pEdit)
279 : : {
280 : 0 : if ( pEdit->IsModified() )
281 : 0 : aBtnRestore.Enable( true );
282 : 0 : return 0;
283 : : }
284 : :
285 : 0 : IMPL_LINK_NOARG(ScDataFormDlg, Impl_NewHdl)
286 : : {
287 : 0 : ScViewData* pViewData = pTabViewShell->GetViewData();
288 : 0 : ScDocShell* pDocSh = pViewData->GetDocShell();
289 : 0 : if ( pDoc )
290 : : {
291 : 0 : bool bHasData = false;
292 : 0 : boost::ptr_vector<Edit>::iterator itr = maEdits.begin(), itrEnd = maEdits.end();
293 : 0 : for(; itr != itrEnd; ++itr)
294 : 0 : if (!boost::is_null(itr))
295 : 0 : if ( (*itr).GetText().Len() != 0 )
296 : : {
297 : 0 : bHasData = true;
298 : 0 : break;
299 : : }
300 : :
301 : 0 : if ( bHasData )
302 : : {
303 : 0 : pTabViewShell->DataFormPutData( nCurrentRow , nStartRow , nStartCol , nEndRow , nEndCol , maEdits , aColLength );
304 : 0 : nCurrentRow++;
305 : 0 : if (nCurrentRow >= nEndRow + 2)
306 : : {
307 : 0 : nEndRow ++ ;
308 : 0 : aSlider.SetRange( Range( 0, nEndRow - nStartRow + 1) );
309 : : }
310 : 0 : SetButtonState();
311 : 0 : FillCtrls(nCurrentRow);
312 : 0 : pDocSh->SetDocumentModified();
313 : 0 : pDocSh->PostPaintGridAll();
314 : : }
315 : : }
316 : 0 : return 0;
317 : : }
318 : :
319 : 0 : IMPL_LINK_NOARG(ScDataFormDlg, Impl_PrevHdl)
320 : : {
321 : 0 : if (pDoc)
322 : : {
323 : 0 : if ( nCurrentRow > nStartRow +1 )
324 : 0 : nCurrentRow--;
325 : :
326 : 0 : SetButtonState();
327 : 0 : FillCtrls(nCurrentRow);
328 : : }
329 : 0 : return 0;
330 : : }
331 : :
332 : 0 : IMPL_LINK_NOARG(ScDataFormDlg, Impl_NextHdl)
333 : : {
334 : 0 : if (pDoc)
335 : : {
336 : 0 : if ( nCurrentRow <= nEndRow)
337 : 0 : nCurrentRow++;
338 : :
339 : 0 : SetButtonState();
340 : 0 : FillCtrls(nCurrentRow);
341 : : }
342 : 0 : return 0;
343 : : }
344 : :
345 : 0 : IMPL_LINK_NOARG(ScDataFormDlg, Impl_RestoreHdl)
346 : : {
347 : 0 : if (pDoc)
348 : : {
349 : 0 : FillCtrls(nCurrentRow);
350 : : }
351 : 0 : return 0;
352 : : }
353 : :
354 : 0 : IMPL_LINK_NOARG(ScDataFormDlg, Impl_DeleteHdl)
355 : : {
356 : 0 : ScViewData* pViewData = pTabViewShell->GetViewData();
357 : 0 : ScDocShell* pDocSh = pViewData->GetDocShell();
358 : 0 : if (pDoc)
359 : : {
360 : 0 : ScRange aRange(nStartCol, nCurrentRow, nTab, nEndCol, nCurrentRow, nTab);
361 : 0 : pDoc->DeleteRow(aRange);
362 : 0 : nEndRow--;
363 : :
364 : 0 : SetButtonState();
365 : 0 : pDocSh->GetUndoManager()->Clear();
366 : :
367 : 0 : FillCtrls(nCurrentRow);
368 : 0 : pDocSh->SetDocumentModified();
369 : 0 : pDocSh->PostPaintGridAll();
370 : : }
371 : 0 : return 0;
372 : : }
373 : :
374 : 0 : IMPL_LINK_NOARG(ScDataFormDlg, Impl_CloseHdl)
375 : : {
376 : 0 : EndDialog( );
377 : 0 : return 0;
378 : : }
379 : :
380 : 0 : IMPL_LINK_NOARG(ScDataFormDlg, Impl_ScrollHdl)
381 : : {
382 : 0 : long nOffset = aSlider.GetThumbPos();
383 : 0 : nCurrentRow = nStartRow + nOffset + 1;
384 : 0 : SetButtonState();
385 : 0 : FillCtrls(nCurrentRow);
386 : 0 : return 0;
387 : : }
388 : :
389 : 0 : void ScDataFormDlg::SetButtonState()
390 : : {
391 : 0 : if (nCurrentRow > nEndRow)
392 : : {
393 : 0 : aBtnDelete.Enable( false );
394 : 0 : aBtnNext.Enable( false );
395 : : }
396 : : else
397 : : {
398 : 0 : aBtnDelete.Enable( true );
399 : 0 : aBtnNext.Enable( true );
400 : : }
401 : :
402 : 0 : if (nCurrentRow == nStartRow + 1)
403 : 0 : aBtnPrev.Enable( false );
404 : : else
405 : 0 : aBtnPrev.Enable( true );
406 : :
407 : 0 : aBtnRestore.Enable( false );
408 : 0 : if ( maEdits.size()>=1 && !maEdits.is_null(0) )
409 : 0 : maEdits[0].GrabFocus();
410 : 0 : }
411 : :
412 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|