summaryrefslogtreecommitdiff
path: root/sc/source/ui/miscdlgs/datafdlg.cxx
blob: 9dd8cdbb105983a7c67b676ce3664a79ccbf6157 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#undef SC_DLLIMPLEMENTATION

//------------------------------------------------------------------

#include "datafdlg.hxx"
#include "scresid.hxx"
#include "viewdata.hxx"
#include "docsh.hxx"
#include "refundo.hxx"
#include "undodat.hxx"

#include "rtl/ustrbuf.hxx"

#define HDL(hdl)            LINK( this, ScDataFormDlg, hdl )


ScDataFormDlg::ScDataFormDlg(Window* pParent, ScTabViewShell* pTabViewShellOri)
    : ModalDialog(pParent, "DataFormDialog", "modules/scalc/ui/dataform.ui")
    , pTabViewShell(pTabViewShellOri)
{
    get(m_pBtnNew, "new");
    get(m_pBtnDelete, "delete");
    get(m_pBtnRestore, "restore");
    get(m_pBtnPrev, "prev");
    get(m_pBtnNext, "next");
    get(m_pBtnClose, "close");
    get(m_pFixedText, "label");
    sNewRecord = m_pFixedText->GetText();
    get(m_pSlider, "scrollbar");
    get(m_pGrid, "grid");

    //read header form current document, and add new controls
    OSL_ENSURE( pTabViewShell, "pTabViewShell is NULL! :-/" );
    ScViewData* pViewData = pTabViewShell->GetViewData();

    pDoc = pViewData->GetDocument();
    if (pDoc)
    {
        ScRange aRange;
        pViewData->GetSimpleArea( aRange );
        ScAddress aStart = aRange.aStart;
        ScAddress aEnd = aRange.aEnd;

        nStartCol = aStart.Col();
        nEndCol = aEnd.Col();
        nStartRow   = aStart.Row();
        nEndRow = aEnd.Row();

        nTab = pViewData->GetTabNo();
        //if there is no selection
        if ((nStartCol == nEndCol) && (nStartRow == nEndRow))
            bNoSelection = true;

        if (bNoSelection)
        {
            //find last not blank cell in row
            for (int i=1;i<=MAX_DATAFORM_COLS;i++)
            {
                nEndCol++;
                OUString aColName = pDoc->GetString(nEndCol, nStartRow, nTab);
                int nColWidth = pDoc->GetColWidth( nEndCol, nTab );
                if (aColName.isEmpty() && nColWidth)
                {
                    nEndCol--;
                    break;
                }
            }

            //find first not blank cell in row
            for (int i=1;i<=MAX_DATAFORM_COLS;i++)
            {
                if (nStartCol <= 0)
                    break;
                nStartCol--;

                OUString aColName = pDoc->GetString(nStartCol, nStartRow, nTab);
                int nColWidth = pDoc->GetColWidth( nEndCol, nTab );
                if (aColName.isEmpty() && nColWidth)
                {
                    nStartCol++;
                    break;
                }
            }

            //skip leading hide column
            for (int i=1;i<=MAX_DATAFORM_COLS;i++)
            {
                int nColWidth = pDoc->GetColWidth( nStartCol, nTab );
                if (nColWidth)
                    break;
                nStartCol++;
            }

            if (nEndCol < nStartCol)
                nEndCol = nStartCol;

            //find last not blank cell in row
            for (int i=1;i<=MAX_DATAFORM_ROWS;i++)
            {
                nEndRow++;
                OUString aColName = pDoc->GetString(nStartCol, nEndRow, nTab);
                if (aColName.isEmpty())
                {
                    nEndRow--;
                    break;
                }
            }

            //find first not blank cell in row
            for (int i=1;i<=MAX_DATAFORM_ROWS;i++)
            {
                if (nStartRow <= 0)
                    break;
                nStartRow--;

                OUString aColName = pDoc->GetString(nStartCol, nStartRow, nTab);
                if (aColName.isEmpty())
                {
                    nStartRow++;
                    break;
                }
            }

            if (nEndRow < nStartRow)
                nEndRow = nStartRow;
        }

        nCurrentRow = nStartRow + 1;

        aColLength = nEndCol - nStartCol + 1;

        //new the controls
        maFixedTexts.reserve(aColLength);
        maEdits.reserve(aColLength);

        sal_Int32 nGridRow = 0;
        for(sal_uInt16 nIndex = 0; nIndex < aColLength; ++nIndex)
        {
            OUString aFieldName = pDoc->GetString(nIndex + nStartCol, nStartRow, nTab);
            int nColWidth = pDoc->GetColWidth( nIndex + nStartCol, nTab );
            if (nColWidth)
            {
                maFixedTexts.push_back( new FixedText(m_pGrid) );
                maEdits.push_back( new Edit(m_pGrid, WB_BORDER) );

                maFixedTexts[nIndex].set_grid_left_attach(0);
                maEdits[nIndex].set_grid_left_attach(1);
                maFixedTexts[nIndex].set_grid_top_attach(nGridRow);
                maEdits[nIndex].set_grid_top_attach(nGridRow);

                maEdits[nIndex].SetWidthInChars(32);
                maEdits[nIndex].set_hexpand(true);

                ++nGridRow;

                maFixedTexts[nIndex].SetText(aFieldName);
                maFixedTexts[nIndex].Show();
                maEdits[nIndex].Show();
            }
            else
            {
                maFixedTexts.push_back( NULL );
                maEdits.push_back( NULL );
            }
            if (!maEdits.is_null(nIndex))
                maEdits[nIndex].SetModifyHdl( HDL(Impl_DataModifyHdl) );
        }
    }

    FillCtrls(nCurrentRow);

    m_pSlider->SetPageSize( 10 );
    m_pSlider->SetVisibleSize( 1 );
    m_pSlider->SetLineSize( 1 );
    m_pSlider->SetRange( Range( 0, nEndRow - nStartRow + 1) );
    m_pSlider->Show();

    m_pBtnNew->SetClickHdl(HDL(Impl_NewHdl));
    m_pBtnPrev->SetClickHdl(HDL(Impl_PrevHdl));
    m_pBtnNext->SetClickHdl(HDL(Impl_NextHdl));

    m_pBtnRestore->SetClickHdl(HDL(Impl_RestoreHdl));
    m_pBtnDelete->SetClickHdl(HDL(Impl_DeleteHdl));
    m_pBtnClose->SetClickHdl(HDL(Impl_CloseHdl));

    m_pSlider->SetEndScrollHdl(HDL(Impl_ScrollHdl));

    SetButtonState();
}

ScDataFormDlg::~ScDataFormDlg()
{

}

void ScDataFormDlg::FillCtrls(SCROW /*nCurrentRow*/)
{
    String  aFieldName;
    for (sal_uInt16 i = 0; i < aColLength; ++i)
    {
        if (!maEdits.is_null(i))
        {
            if (nCurrentRow<=nEndRow)
            {
                aFieldName = pDoc->GetString(i + nStartCol, nCurrentRow, nTab);
                maEdits[i].SetText(aFieldName);
            }
            else
                maEdits[i].SetText(String());
        }
    }

    if (nCurrentRow <= nEndRow)
    {
        OUStringBuffer aBuf;
        aBuf.append(static_cast<sal_Int32>(nCurrentRow - nStartRow));
        aBuf.appendAscii(" / ");
        aBuf.append(static_cast<sal_Int32>(nEndRow - nStartRow));
        m_pFixedText->SetText(aBuf.makeStringAndClear());
    }
    else
        m_pFixedText->SetText(sNewRecord);

    m_pSlider->SetThumbPos(nCurrentRow-nStartRow-1);
}

IMPL_LINK( ScDataFormDlg, Impl_DataModifyHdl, Edit*, pEdit)
{
    if ( pEdit->IsModified() )
        m_pBtnRestore->Enable( true );
    return 0;
}

IMPL_LINK_NOARG(ScDataFormDlg, Impl_NewHdl)
{
    ScViewData* pViewData = pTabViewShell->GetViewData();
    ScDocShell* pDocSh = pViewData->GetDocShell();
    if ( pDoc )
    {
        bool bHasData = false;
        boost::ptr_vector<Edit>::iterator itr = maEdits.begin(), itrEnd = maEdits.end();
        for(; itr != itrEnd; ++itr)
            if (!boost::is_null(itr))
                if ( !(*itr).GetText().isEmpty() )
                {
                    bHasData = true;
                    break;
                }

        if ( bHasData )
        {
            pTabViewShell->DataFormPutData( nCurrentRow , nStartRow , nStartCol , nEndRow , nEndCol , maEdits , aColLength );
            nCurrentRow++;
            if (nCurrentRow >= nEndRow + 2)
            {
                    nEndRow ++ ;
                    m_pSlider->SetRange( Range( 0, nEndRow - nStartRow + 1) );
            }
            SetButtonState();
            FillCtrls(nCurrentRow);
            pDocSh->SetDocumentModified();
            pDocSh->PostPaintGridAll();
            }
    }
    return 0;
}

IMPL_LINK_NOARG(ScDataFormDlg, Impl_PrevHdl)
{
    if (pDoc)
    {
        if ( nCurrentRow > nStartRow +1 )
            nCurrentRow--;

        SetButtonState();
        FillCtrls(nCurrentRow);
    }
    return 0;
}

IMPL_LINK_NOARG(ScDataFormDlg, Impl_NextHdl)
{
    if (pDoc)
    {
        if ( nCurrentRow <= nEndRow)
            nCurrentRow++;

        SetButtonState();
        FillCtrls(nCurrentRow);
    }
    return 0;
}

IMPL_LINK_NOARG(ScDataFormDlg, Impl_RestoreHdl)
{
    if (pDoc)
    {
        FillCtrls(nCurrentRow);
    }
    return 0;
}

IMPL_LINK_NOARG(ScDataFormDlg, Impl_DeleteHdl)
{
    ScViewData* pViewData = pTabViewShell->GetViewData();
    ScDocShell* pDocSh = pViewData->GetDocShell();
    if (pDoc)
    {
        ScRange aRange(nStartCol, nCurrentRow, nTab, nEndCol, nCurrentRow, nTab);
        pDoc->DeleteRow(aRange);
        nEndRow--;

        SetButtonState();
        pDocSh->GetUndoManager()->Clear();

        FillCtrls(nCurrentRow);
        pDocSh->SetDocumentModified();
        pDocSh->PostPaintGridAll();
    }
    return 0;
}

IMPL_LINK_NOARG(ScDataFormDlg, Impl_CloseHdl)
{
    EndDialog( );
    return 0;
}

IMPL_LINK_NOARG(ScDataFormDlg, Impl_ScrollHdl)
{
    long nOffset = m_pSlider->GetThumbPos();
    nCurrentRow = nStartRow + nOffset + 1;
    SetButtonState();
    FillCtrls(nCurrentRow);
    return 0;
}

void ScDataFormDlg::SetButtonState()
{
    if (nCurrentRow > nEndRow)
    {
        m_pBtnDelete->Enable( false );
        m_pBtnNext->Enable( false );
    }
    else
    {
        m_pBtnDelete->Enable( true );
        m_pBtnNext->Enable( true );
    }

    if (nCurrentRow == nStartRow + 1)
        m_pBtnPrev->Enable( false );
    else
        m_pBtnPrev->Enable( true );

    m_pBtnRestore->Enable( false );
    if ( maEdits.size()>=1 && !maEdits.is_null(0) )
        maEdits[0].GrabFocus();
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */