summaryrefslogtreecommitdiffstats
path: root/sw/source/uibase/sidebar/PageHeaderPanel.cxx
blob: e0706f9c7698788c08eb04a6bfa81ae848319d3e (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
/* -*- 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 <sal/config.h>
#include <svl/intitem.hxx>
#include <svl/eitem.hxx>
#include <svl/itemset.hxx>
#include <svx/dlgutil.hxx>
#include <svx/rulritem.hxx>
#include <svx/svdtrans.hxx>
#include <svx/spacinglistbox.hxx>
#include <svx/samecontentlistbox.hxx>
#include "PageHeaderPanel.hxx"
#include <sfx2/dispatch.hxx>
#include <sfx2/bindings.hxx>
#include <cmdid.h>

#include <com/sun/star/lang/IllegalArgumentException.hpp>

namespace sw::sidebar{

std::unique_ptr<PanelLayout> PageHeaderPanel::Create(
    weld::Widget* pParent,
    SfxBindings* pBindings)
{
    if( pParent == nullptr )
        throw ::com::sun::star::lang::IllegalArgumentException("no parent window given to PageHeaderPanel::Create", nullptr, 0);
    if( pBindings == nullptr )
        throw ::com::sun::star::lang::IllegalArgumentException("no SfxBindings given to PageHeaderPanel::Create", nullptr, 0);

    return std::make_unique<PageHeaderPanel>(pParent, pBindings);
}

void PageHeaderPanel::SetMarginsAndSpacingFieldUnit()
{
    SpacingListBox::Fill(IsInch(meFUnit) ? SpacingType::SPACING_INCH : SpacingType::SPACING_CM, *mxHeaderSpacingLB);
    SpacingListBox::Fill(IsInch(meFUnit) ? SpacingType::MARGINS_INCH : SpacingType::MARGINS_CM, *mxHeaderMarginPresetLB);
}

PageHeaderPanel::PageHeaderPanel(
    weld::Widget* pParent,
    SfxBindings* pBindings
    ) :
    PanelLayout(pParent, "PageHeaderPanel", "modules/swriter/ui/pageheaderpanel.ui"),
    mpBindings( pBindings ),
    maHFToggleController(SID_ATTR_PAGE_HEADER, *pBindings, *this),
    maMetricController(SID_ATTR_METRIC, *pBindings,*this),
    maHeaderLRMarginController(SID_ATTR_PAGE_HEADER_LRMARGIN, *pBindings, *this),
    maHeaderSpacingController(SID_ATTR_PAGE_HEADER_SPACING, *pBindings, *this),
    maHeaderLayoutController(SID_ATTR_PAGE_HEADER_LAYOUT, *pBindings, *this),
    meFUnit(GetModuleFieldUnit()),
    mpHeaderItem( new SfxBoolItem(SID_ATTR_PAGE_HEADER) ),
    mpHeaderLRMarginItem( new SvxLongLRSpaceItem(0, 0, SID_ATTR_PAGE_HEADER_LRMARGIN)),
    mpHeaderSpacingItem( new SvxLongULSpaceItem(0, 0, SID_ATTR_PAGE_HEADER_SPACING)),
    mpHeaderLayoutItem( new SfxInt16Item(SID_ATTR_PAGE_HEADER_LAYOUT)),
    mxHeaderToggle(m_xBuilder->weld_check_button("headertoggle")),
    mxHeaderSpacingLB(m_xBuilder->weld_combo_box("spacingpreset")),
    mxHeaderMarginPresetLB(m_xBuilder->weld_combo_box("headermarginpreset")),
    mxHeaderLayoutLB(m_xBuilder->weld_combo_box("samecontentLB")),
    mxCustomEntry(m_xBuilder->weld_label("customlabel"))
{
    Initialize();
}

PageHeaderPanel::~PageHeaderPanel()
{
    mxHeaderToggle.reset();
    mxHeaderSpacingLB.reset();
    mxHeaderLayoutLB.reset();
    mxHeaderMarginPresetLB.reset();
    mxCustomEntry.reset();
}

FieldUnit PageHeaderPanel::GetCurrentUnit(SfxItemState eState, const SfxPoolItem* pState)
{
    FieldUnit eUnit;

    if (pState && eState >= SfxItemState::DEFAULT)
        eUnit = static_cast<FieldUnit>(static_cast<const SfxUInt16Item*>(pState)->GetValue());
    else
        eUnit = GetModuleFieldUnit();

    return eUnit;
}

void PageHeaderPanel::Initialize()
{
    SameContentListBox::Fill(*mxHeaderLayoutLB);

    SetMarginsAndSpacingFieldUnit();

    m_aCustomEntry = mxCustomEntry->get_label();
    mxHeaderToggle->connect_toggled( LINK(this, PageHeaderPanel, HeaderToggleHdl) );
    mxHeaderMarginPresetLB->connect_changed( LINK(this, PageHeaderPanel, HeaderLRMarginHdl));
    mxHeaderSpacingLB->connect_changed( LINK(this, PageHeaderPanel, HeaderSpacingHdl));
    mxHeaderLayoutLB->connect_changed( LINK(this, PageHeaderPanel, HeaderLayoutHdl));

    mpBindings->Invalidate(SID_ATTR_METRIC);
    mpBindings->Invalidate(SID_ATTR_PAGE_HEADER);
    mpBindings->Invalidate(SID_ATTR_PAGE_HEADER_LRMARGIN);
    mpBindings->Invalidate(SID_ATTR_PAGE_HEADER_SPACING);
    mpBindings->Invalidate(SID_ATTR_PAGE_HEADER_LAYOUT);
}

void PageHeaderPanel::UpdateHeaderCheck()
{
    if (mxHeaderToggle->get_active())
    {
        mxHeaderSpacingLB->set_sensitive(true);
        mxHeaderLayoutLB->set_sensitive(true);
        mxHeaderMarginPresetLB->set_sensitive(true);
    }
    else
    {
        mxHeaderSpacingLB->set_sensitive(false);
        mxHeaderLayoutLB->set_sensitive(false);
        mxHeaderMarginPresetLB->set_sensitive(false);
    }
}

void PageHeaderPanel::UpdateMarginControl()
{
    sal_uInt16 nLeft = mpHeaderLRMarginItem->GetLeft();
    sal_uInt16 nRight = mpHeaderLRMarginItem->GetRight();
    sal_uInt16 nCount = mxHeaderMarginPresetLB->get_count();
    if(nLeft == nRight)
    {
        for (sal_uInt16 i = 0; i < nCount; ++i)
        {
            if (mxHeaderMarginPresetLB->get_id(i).toUInt32() == nLeft)
            {
                mxHeaderMarginPresetLB->set_active(i);
                int nCustomEntry = mxHeaderMarginPresetLB->find_text(m_aCustomEntry);
                if (nCustomEntry != -1)
                    mxHeaderMarginPresetLB->remove(nCustomEntry);
                return;
            }
        }
    }
    mxHeaderMarginPresetLB->append_text(m_aCustomEntry);
    mxHeaderMarginPresetLB->set_active_text(m_aCustomEntry);
}

void PageHeaderPanel::UpdateSpacingControl()
{
    sal_uInt16 nBottom = mpHeaderSpacingItem->GetLower();
    sal_uInt16 nCount = mxHeaderSpacingLB->get_count();
    for (sal_uInt16 i = 0; i < nCount; ++i)
    {
        if (mxHeaderSpacingLB->get_id(i).toUInt32() == nBottom)
        {
            mxHeaderSpacingLB->set_active(i);
            int nCustomEntry = mxHeaderSpacingLB->find_text(m_aCustomEntry);
            if (nCustomEntry != -1)
                mxHeaderSpacingLB->remove(nCustomEntry);
            return;
        }
    }
    mxHeaderSpacingLB->append_text(m_aCustomEntry);
    mxHeaderSpacingLB->set_active_text(m_aCustomEntry);
}

void PageHeaderPanel::UpdateLayoutControl()
{
    sal_uInt16 nLayout = mpHeaderLayoutItem->GetValue();
    mxHeaderLayoutLB->set_active(nLayout);
}

void PageHeaderPanel::NotifyItemUpdate(
    const sal_uInt16 nSid,
    const SfxItemState eState,
    const SfxPoolItem* pState)
{
    if (!mxHeaderToggle) //disposed
        return;

    switch(nSid)
    {
        case SID_ATTR_PAGE_HEADER:
        {
            if(eState >= SfxItemState::DEFAULT &&
                dynamic_cast<const SfxBoolItem*>( pState) )
            {
                mpHeaderItem.reset( static_cast<SfxBoolItem*>(pState->Clone()) );
                mxHeaderToggle->set_active(mpHeaderItem->GetValue());
                UpdateHeaderCheck();
            }
        }
        break;
        case SID_ATTR_PAGE_HEADER_LRMARGIN:
        {
            if(eState >= SfxItemState::DEFAULT &&
                dynamic_cast<const SvxLongLRSpaceItem*>( pState) )
            {
                mpHeaderLRMarginItem.reset( static_cast<SvxLongLRSpaceItem*>(pState->Clone()) );
                UpdateMarginControl();
            }
        }
        break;
        case SID_ATTR_PAGE_HEADER_SPACING:
        {
            if(eState >= SfxItemState::DEFAULT &&
                dynamic_cast<const SvxLongULSpaceItem*>( pState) )
            {
                mpHeaderSpacingItem.reset(static_cast<SvxLongULSpaceItem*>(pState->Clone()) );
                UpdateSpacingControl();
            }
        }
        break;
        case SID_ATTR_PAGE_HEADER_LAYOUT:
        {
            if(eState >= SfxItemState::DEFAULT &&
                dynamic_cast<const SfxInt16Item*>( pState) )
            {
                mpHeaderLayoutItem.reset(static_cast<SfxInt16Item*>(pState->Clone()) );
                UpdateLayoutControl();
            }
        }
        break;
        case SID_ATTR_METRIC:
        {
            FieldUnit eFUnit = GetCurrentUnit(eState, pState);
            if (meFUnit != eFUnit)
            {
                meFUnit = eFUnit;
                SetMarginsAndSpacingFieldUnit();
                UpdateSpacingControl();
                UpdateMarginControl();
            }
        }
        break;
        default:
            break;
    }
}

IMPL_LINK_NOARG( PageHeaderPanel, HeaderToggleHdl, weld::Toggleable&, void )
{
    bool IsChecked = mxHeaderToggle->get_active();
    mpHeaderItem->SetValue(IsChecked);
    GetBindings()->GetDispatcher()->ExecuteList( SID_ATTR_PAGE_HEADER, SfxCallMode::RECORD, { mpHeaderItem.get() } );
    UpdateHeaderCheck();
}

IMPL_LINK_NOARG( PageHeaderPanel, HeaderLRMarginHdl, weld::ComboBox&, void )
{
    sal_uInt16 nVal = mxHeaderMarginPresetLB->get_active_id().toUInt32();
    mpHeaderLRMarginItem->SetLeft(nVal);
    mpHeaderLRMarginItem->SetRight(nVal);
    GetBindings()->GetDispatcher()->ExecuteList( SID_ATTR_PAGE_HEADER_LRMARGIN,
                                                 SfxCallMode::RECORD, { mpHeaderLRMarginItem.get() } );
}

IMPL_LINK_NOARG( PageHeaderPanel, HeaderSpacingHdl, weld::ComboBox&, void )
{
    sal_uInt16 nVal = mxHeaderSpacingLB->get_active_id().toUInt32();
    mpHeaderSpacingItem->SetLower(nVal);
    GetBindings()->GetDispatcher()->ExecuteList( SID_ATTR_PAGE_HEADER_SPACING,
                                                 SfxCallMode::RECORD, { mpHeaderSpacingItem.get() } );
}
IMPL_LINK_NOARG( PageHeaderPanel, HeaderLayoutHdl, weld::ComboBox&, void )
{
    sal_uInt16 nVal = mxHeaderLayoutLB->get_active();
    mpHeaderLayoutItem->SetValue(nVal);
    GetBindings()->GetDispatcher()->ExecuteList( SID_ATTR_PAGE_HEADER_LAYOUT,
                                                 SfxCallMode::RECORD, { mpHeaderLayoutItem.get() } );
}


}

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