summaryrefslogtreecommitdiff
path: root/chart2/source/controller/dialogs/dlg_DataEditor.cxx
blob: ddba251a56d33e3d0f88c2f801a95d4920735ab3 (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
/* -*- 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/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include "dlg_DataEditor.hxx"
#include "Strings.hrc"
#include "DataBrowser.hxx"

#include "ResId.hxx"
#include <sfx2/dispatch.hxx>
#include <vcl/msgbox.hxx>
#include <vcl/taskpanelist.hxx>
#include <svtools/miscopt.hxx>
#include <unotools/pathoptions.hxx>

// for SfxBoolItem
#include <svl/eitem.hxx>

#include <vcl/edit.hxx>

#include <com/sun/star/frame/XStorable.hpp>
#include <com/sun/star/chart2/XChartDocument.hpp>

using namespace ::com::sun::star;
using ::com::sun::star::uno::Reference;

namespace chart
{

DataEditor::DataEditor(Window* pParent,
    const Reference< chart2::XChartDocument > & xChartDoc,
    const Reference< uno::XComponentContext > & xContext)
    : ModalDialog(pParent, "ChartDataDialog",
        "modules/schart/ui/chartdatadialog.ui")
    , m_bReadOnly(false)
    , m_xChartDoc(xChartDoc)
    , m_xContext(xContext)
{
    m_xBrwData.reset(new DataBrowser(get<Window>("datawindow"), WB_BORDER | WB_TABSTOP, true /* bLiveUpdate */));
    m_xBrwData->set_hexpand(true);
    m_xBrwData->set_vexpand(true);
    m_xBrwData->set_expand(true);
    Size aSize(m_xBrwData->LogicToPixel(Size(232, 121), MAP_APPFONT));
    m_xBrwData->set_width_request(aSize.Width());
    m_xBrwData->set_height_request(aSize.Height());
    m_xBrwData->Show();

    get(m_pTbxData, "toolbar");

    TBI_DATA_INSERT_ROW = m_pTbxData->GetItemId("InsertRow");
    TBI_DATA_INSERT_COL = m_pTbxData->GetItemId("InsertColumn");
    TBI_DATA_INSERT_TEXT_COL = m_pTbxData->GetItemId("InsertTextColumn");
    TBI_DATA_DELETE_ROW = m_pTbxData->GetItemId("RemoveRow");
    TBI_DATA_DELETE_COL = m_pTbxData->GetItemId("RemoveColumn");
    TBI_DATA_SWAP_COL = m_pTbxData->GetItemId("SwapColumn");
    TBI_DATA_SWAP_ROW = m_pTbxData->GetItemId("SwapRow");

    m_pTbxData->SetSelectHdl( LINK( this, DataEditor, ToolboxHdl ));

    m_xBrwData->SetCursorMovedHdl( LINK( this, DataEditor,   BrowserCursorMovedHdl ));
    m_xBrwData->SetCellModifiedHdl( LINK( this, DataEditor,  CellModified ));

    UpdateData();
    GrabFocus();
    m_xBrwData->GrabFocus();

    bool bReadOnly = true;
    Reference< frame::XStorable > xStor( m_xChartDoc, uno::UNO_QUERY );
    if( xStor.is())
        bReadOnly = xStor->isReadonly();
    SetReadOnly( bReadOnly );

    // change buttons to flat-look if set so by user
    SvtMiscOptions aMiscOptions;
    const sal_Int16 nStyle( aMiscOptions.GetToolboxStyle() );
    // react on changes
    aMiscOptions.AddListenerLink( LINK( this, DataEditor, MiscHdl ) );
    m_pTbxData->SetOutStyle( nStyle );

    // allow travelling to toolbar with F6
    notifySystemWindow( this, m_pTbxData, ::comphelper::mem_fun( & TaskPaneList::AddWindow ));
}

DataEditor::~DataEditor()
{
    notifySystemWindow( this, m_pTbxData, ::comphelper::mem_fun( & TaskPaneList::RemoveWindow ));

    SvtMiscOptions aMiscOptions;
    aMiscOptions.RemoveListenerLink( LINK( this, DataEditor, MiscHdl ) );

    OSL_TRACE( "DataEditor: DTOR" );
}

// react on click (or keypress) on toolbar icon
IMPL_LINK_NOARG(DataEditor, ToolboxHdl)
{
    sal_uInt16 nId = m_pTbxData->GetCurItemId();

    if (nId == TBI_DATA_INSERT_ROW)
        m_xBrwData->InsertRow();
    else if (nId == TBI_DATA_INSERT_COL)
        m_xBrwData->InsertColumn();
    else if (nId == TBI_DATA_INSERT_TEXT_COL)
        m_xBrwData->InsertTextColumn();
    else if (nId == TBI_DATA_DELETE_ROW)
        m_xBrwData->RemoveRow();
    else if (nId == TBI_DATA_DELETE_COL)
        m_xBrwData->RemoveColumn();
    else if (nId == TBI_DATA_SWAP_COL)
        m_xBrwData->SwapColumn();
    else if (nId == TBI_DATA_SWAP_ROW)
        m_xBrwData->SwapRow();

    return 0;
}

// refresh toolbar icons according to currently selected cell in brwose box
IMPL_LINK_NOARG(DataEditor, BrowserCursorMovedHdl)
{
    if( m_bReadOnly )
        return 0;

    bool bIsDataValid = m_xBrwData->IsEnableItem();

    m_pTbxData->EnableItem( TBI_DATA_INSERT_ROW, bIsDataValid && m_xBrwData->MayInsertRow() );
    m_pTbxData->EnableItem( TBI_DATA_INSERT_COL, bIsDataValid && m_xBrwData->MayInsertColumn() );
    m_pTbxData->EnableItem( TBI_DATA_INSERT_TEXT_COL, bIsDataValid && m_xBrwData->MayInsertColumn() );
    m_pTbxData->EnableItem( TBI_DATA_DELETE_ROW, m_xBrwData->MayDeleteRow() );
    m_pTbxData->EnableItem( TBI_DATA_DELETE_COL, m_xBrwData->MayDeleteColumn() );

    m_pTbxData->EnableItem( TBI_DATA_SWAP_COL,   bIsDataValid && m_xBrwData->MaySwapColumns() );
    m_pTbxData->EnableItem( TBI_DATA_SWAP_ROW,   bIsDataValid && m_xBrwData->MaySwapRows() );

    return 0;
}

// disable all modifying controls
void DataEditor::SetReadOnly( bool bReadOnly )
{
    m_bReadOnly = bReadOnly;
    if( m_bReadOnly )
    {
        m_pTbxData->EnableItem( TBI_DATA_INSERT_ROW, false );
        m_pTbxData->EnableItem( TBI_DATA_INSERT_COL, false );
        m_pTbxData->EnableItem( TBI_DATA_INSERT_TEXT_COL, false );
        m_pTbxData->EnableItem( TBI_DATA_DELETE_ROW, false );
        m_pTbxData->EnableItem( TBI_DATA_DELETE_COL, false );
        m_pTbxData->EnableItem( TBI_DATA_SWAP_COL, false );
        m_pTbxData->EnableItem( TBI_DATA_SWAP_ROW, false );
    }

    m_xBrwData->SetReadOnly( m_bReadOnly );
}

IMPL_LINK_NOARG(DataEditor, MiscHdl)
{
    SvtMiscOptions aMiscOptions;
    sal_Int16 nStyle( aMiscOptions.GetToolboxStyle() );

    m_pTbxData->SetOutStyle( nStyle );

    return 0L;
}

IMPL_LINK_NOARG(DataEditor, CellModified)
{
    return 0;
}

void DataEditor::UpdateData()
{
    m_xBrwData->SetDataFromModel( m_xChartDoc, m_xContext );
}

bool DataEditor::Close()
{
    if( ApplyChangesToModel() )
        return ModalDialog::Close();
    else
        return true;
}

bool DataEditor::ApplyChangesToModel()
{
    return m_xBrwData->EndEditing();
}

// add/remove a window (the toolbar) to/from the global list, so that F6
// travels/no longer travels over this window.  _rMemFunc may be
// TaskPaneList::AddWindow or TaskPaneList::RemoveWindow
void DataEditor::notifySystemWindow(
    Window* pWindow, Window* pToRegister,
    ::comphelper::mem_fun1_t< TaskPaneList, Window* > rMemFunc )
{
    OSL_ENSURE( pWindow, "Window must not be null!" );
    if( !pWindow )
        return;
    Window* pParent = pWindow->GetParent();
    while( pParent && ! pParent->IsSystemWindow() )
    {
        pParent = pParent->GetParent();
    }
    if ( pParent && pParent->IsSystemWindow())
    {
        SystemWindow* pSystemWindow = static_cast< SystemWindow* >( pParent );
        rMemFunc( pSystemWindow->GetTaskPaneList(),( pToRegister ));
    }
}

} // namespace chart

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