summaryrefslogtreecommitdiff
path: root/svx/source/sidebar/paragraph/ParaLineSpacingControl.cxx
blob: b5e54801dde711034a04e05189a32fa0f7385b55 (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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* -*- 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 "ParaLineSpacingControl.hxx"

#include <editeng/editids.hrc>
#include <editeng/kernitem.hxx>
#include <editeng/lspcitem.hxx>
#include <sfx2/dispatch.hxx>
#include <svtools/unitconv.hxx>

#define DEFAULT_LINE_SPACING  200
#define FIX_DIST_DEF          283
#define LINESPACE_1           100
#define LINESPACE_15          150
#define LINESPACE_2           200
#define LINESPACE_115         115

// values of the mpLineDist listbox
#define LLINESPACE_1          0
#define LLINESPACE_115        1
#define LLINESPACE_15         2
#define LLINESPACE_2          3
#define LLINESPACE_PROP       4
#define LLINESPACE_MIN        5
#define LLINESPACE_DURCH      6
#define LLINESPACE_FIX        7

#define MIN_FIXED_DISTANCE    28

using namespace svx;

ParaLineSpacingControl::ParaLineSpacingControl(sal_uInt16 nId, vcl::Window* pParent)
    : SfxPopupWindow(nId, pParent, "ParaLineSpacingControl", "svx/ui/paralinespacingcontrol.ui")
{
    mpSpacing1Button = get<PushButton>("spacing_1");
    mpSpacing115Button = get<PushButton>("spacing_115");
    mpSpacing15Button = get<PushButton>("spacing_15");
    mpSpacing2Button = get<PushButton>("spacing_2");

    mpLineDist = get<ListBox>("line_dist");

    mpLineDistLabel = get<FixedText>("value_label");
    mpLineDistAtPercentBox = get<MetricField>("percent_box");
    mpLineDistAtMetricBox = get<MetricField>("metric_box");

    mpActLineDistFld = mpLineDistAtPercentBox.get();

    meLNSpaceUnit = MapUnit::Map100thMM;

    Link<Button*,void> aLink = LINK(this, ParaLineSpacingControl, PredefinedValuesHandler);
    mpSpacing1Button->SetClickHdl(aLink);
    mpSpacing115Button->SetClickHdl(aLink);
    mpSpacing15Button->SetClickHdl(aLink);
    mpSpacing2Button->SetClickHdl(aLink);

    Link<ListBox&,void> aLink3 = LINK( this, ParaLineSpacingControl, LineSPDistHdl_Impl );
    mpLineDist->SetSelectHdl(aLink3);
    SelectEntryPos(LLINESPACE_1);

    Link<Edit&,void> aLink2 = LINK( this, ParaLineSpacingControl, LineSPDistAtHdl_Impl );
    mpLineDistAtPercentBox->SetModifyHdl( aLink2 );
    mpLineDistAtMetricBox->SetModifyHdl( aLink2 );

    FieldUnit eUnit = FUNIT_INCH;
    const SfxPoolItem* pItem = nullptr;
    if (SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState(SID_ATTR_METRIC, pItem) >= SfxItemState::DEFAULT)
        eUnit = static_cast<FieldUnit>(static_cast<const SfxUInt16Item*>(pItem)->GetValue());
    else
        eUnit = SfxModule::GetCurrentFieldUnit();

    SetFieldUnit(*mpLineDistAtMetricBox, eUnit);

    Initialize();
}

ParaLineSpacingControl::~ParaLineSpacingControl()
{
    disposeOnce();
}

void ParaLineSpacingControl::dispose()
{
    mpActLineDistFld.clear();
    mpSpacing1Button.clear();
    mpSpacing115Button.clear();
    mpSpacing15Button.clear();
    mpSpacing2Button.clear();
    mpLineDist.clear();
    mpLineDistLabel.clear();
    mpLineDistAtPercentBox.clear();
    mpLineDistAtMetricBox.clear();
    SfxPopupWindow::dispose();
}

void ParaLineSpacingControl::Initialize()
{
    const SfxPoolItem* pItem;
    SfxItemState eState = SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState(SID_ATTR_PARA_LINESPACE, pItem);

    const SvxLineSpacingItem* currSPItem = static_cast<const SvxLineSpacingItem*>(pItem);

    mpLineDist->Enable();

    if( eState >= SfxItemState::DEFAULT )
    {
        MapUnit eUnit = MapUnit::Map100thMM;
        meLNSpaceUnit = eUnit;

        switch( currSPItem->GetLineSpaceRule() )
        {
        case SvxLineSpaceRule::Auto:
            {
                SvxInterLineSpaceRule eInter = currSPItem->GetInterLineSpaceRule();

                switch( eInter )
                {
                case SvxInterLineSpaceRule::Off:
                    SelectEntryPos(LLINESPACE_1);
                    break;

                case SvxInterLineSpaceRule::Prop:
                    {
                        if ( LINESPACE_1 == currSPItem->GetPropLineSpace() )
                        {
                            SelectEntryPos(LLINESPACE_1);
                        }
                        else if ( LINESPACE_115 == currSPItem->GetPropLineSpace() )
                        {
                            SelectEntryPos(LLINESPACE_115);
                        }
                        else if ( LINESPACE_15 == currSPItem->GetPropLineSpace() )
                        {
                            SelectEntryPos(LLINESPACE_15);
                        }
                        else if ( LINESPACE_2 == currSPItem->GetPropLineSpace() )
                        {
                            SelectEntryPos(LLINESPACE_2);
                        }
                        else
                        {
                            SelectEntryPos(LLINESPACE_PROP);
                            mpLineDistAtPercentBox->SetValue(mpLineDistAtPercentBox->Normalize(currSPItem->GetPropLineSpace()));
                        }
                    }
                    break;

                case SvxInterLineSpaceRule::Fix:
                    {
                        SelectEntryPos(LLINESPACE_DURCH);
                        SetMetricValue(*mpLineDistAtMetricBox, currSPItem->GetInterLineSpace(), eUnit);
                    }
                    break;
                default:
                    break;
                }
            }
            break;
        case SvxLineSpaceRule::Fix:
            {
                SelectEntryPos(LLINESPACE_FIX);
                SetMetricValue(*mpLineDistAtMetricBox, currSPItem->GetLineHeight(), eUnit);
            }
            break;

        case SvxLineSpaceRule::Min:
            {
                SelectEntryPos(LLINESPACE_MIN);
                SetMetricValue(*mpLineDistAtMetricBox, currSPItem->GetLineHeight(), eUnit);
            }
            break;
        default:
            break;
        }
    }
    else if( eState == SfxItemState::DISABLED )
    {
        mpLineDist->Disable();
        mpLineDistLabel->Disable();
        mpActLineDistFld->Disable();
        mpActLineDistFld->SetText("");

    }
    else
    {
        mpLineDistLabel->Disable();
        mpActLineDistFld->Disable();
        mpActLineDistFld->SetText("");
        mpLineDist->SetNoSelection();
    }

    mpLineDist->SaveValue();

    /* TODO
    const sal_uInt16 uCount = mpLineDist->GetEntryCount();
    if( uCount == LLINESPACE_FIX + 1 )
    {
        switch (currentContext.GetCombinedContext_DI())
        {
        case CombinedEnumContext(Application::DrawImpress, Context::Table):
        case CombinedEnumContext(Application::DrawImpress, Context::DrawText):
        case CombinedEnumContext(Application::DrawImpress, Context::Draw):
        case CombinedEnumContext(Application::DrawImpress, Context::TextObject):
        case CombinedEnumContext(Application::DrawImpress, Context::Graphic):
        case CombinedEnumContext(Application::Calc, Context::DrawText):
        case CombinedEnumContext(Application::WriterVariants, Context::DrawText):
        case CombinedEnumContext(Application::WriterVariants, Context::Annotation):
            {
                mpLineDist->RemoveEntry(LLINESPACE_FIX);
            }
        }
    }
    else if( uCount == LLINESPACE_FIX)
    {
        switch (currentContext.GetCombinedContext_DI())
        {
            case CombinedEnumContext(Application::WriterVariants, Context::Default):
            case CombinedEnumContext(Application::WriterVariants, Context::Text):
            case CombinedEnumContext(Application::WriterVariants, Context::Table):
            {
                mpLineDist->InsertEntry(OUString("Fixed"), LLINESPACE_FIX);
            }
        }
    }
    */
}

void ParaLineSpacingControl::UpdateMetricFields()
{
    switch (mpLineDist->GetSelectedEntryPos())
    {
        case LLINESPACE_1:
        case LLINESPACE_115:
        case LLINESPACE_15:
        case LLINESPACE_2:
            if (mpActLineDistFld == mpLineDistAtPercentBox)
                mpLineDistAtMetricBox->Hide();
            else
                mpLineDistAtPercentBox->Hide();

            mpLineDistLabel->Disable();
            mpActLineDistFld->Show();
            mpActLineDistFld->Disable();
            mpActLineDistFld->SetText("");
            break;

        case LLINESPACE_DURCH:
            mpLineDistAtPercentBox->Hide();

            mpActLineDistFld = mpLineDistAtMetricBox.get();
            mpLineDistAtMetricBox->SetMin(0);

            if (mpLineDistAtMetricBox->GetText().isEmpty())
                mpLineDistAtMetricBox->SetValue(mpLineDistAtMetricBox->Normalize(0));

            mpLineDistLabel->Enable();
            mpActLineDistFld->Show();
            mpActLineDistFld->Enable();
            break;

        case LLINESPACE_MIN:
            mpLineDistAtPercentBox->Hide();

            mpActLineDistFld = mpLineDistAtMetricBox.get();
            mpLineDistAtMetricBox->SetMin(0);

            if (mpLineDistAtMetricBox->GetText().isEmpty())
                mpLineDistAtMetricBox->SetValue(mpLineDistAtMetricBox->Normalize(0), FUNIT_TWIP);

            mpLineDistLabel->Enable();
            mpActLineDistFld->Show();
            mpActLineDistFld->Enable();
            break;

        case LLINESPACE_PROP:
            mpLineDistAtMetricBox->Hide();

            mpActLineDistFld = mpLineDistAtPercentBox.get();

            if (mpLineDistAtPercentBox->GetText().isEmpty())
                mpLineDistAtPercentBox->SetValue(mpLineDistAtPercentBox->Normalize(100), FUNIT_TWIP);

            mpLineDistLabel->Enable();
            mpActLineDistFld->Show();
            mpActLineDistFld->Enable();
            break;

        case LLINESPACE_FIX:
            mpLineDistAtPercentBox->Hide();

            mpActLineDistFld = mpLineDistAtMetricBox.get();
            sal_Int64 nTemp = mpLineDistAtMetricBox->GetValue();
            mpLineDistAtMetricBox->SetMin(mpLineDistAtMetricBox->Normalize(MIN_FIXED_DISTANCE), FUNIT_TWIP);

            if (mpLineDistAtMetricBox->GetValue() != nTemp)
                SetMetricValue(*mpLineDistAtMetricBox, FIX_DIST_DEF, MapUnit::MapTwip);

            mpLineDistLabel->Enable();
            mpActLineDistFld->Show();
            mpActLineDistFld->Enable();
            break;
    }
}

void ParaLineSpacingControl::SelectEntryPos(sal_Int32 nPos)
{
    mpLineDist->SelectEntryPos(nPos);
    UpdateMetricFields();
}

IMPL_LINK_NOARG(ParaLineSpacingControl, LineSPDistHdl_Impl, ListBox&, void)
{
    UpdateMetricFields();
    ExecuteLineSpace();
}

IMPL_LINK_NOARG( ParaLineSpacingControl, LineSPDistAtHdl_Impl, Edit&, void )
{
    ExecuteLineSpace();
}

void ParaLineSpacingControl::ExecuteLineSpace()
{
    mpLineDist->SaveValue();

    SvxLineSpacingItem aSpacing(DEFAULT_LINE_SPACING, SID_ATTR_PARA_LINESPACE);
    const sal_Int32 nPos = mpLineDist->GetSelectedEntryPos();

    switch ( nPos )
    {
        case LLINESPACE_1:
        case LLINESPACE_115:
        case LLINESPACE_15:
        case LLINESPACE_2:
            SetLineSpace(aSpacing, nPos);
            break;

        case LLINESPACE_PROP:
            SetLineSpace(aSpacing, nPos, mpLineDistAtPercentBox->Denormalize((long)mpLineDistAtPercentBox->GetValue()));
            break;

        case LLINESPACE_MIN:
        case LLINESPACE_DURCH:
        case LLINESPACE_FIX:
            SetLineSpace(aSpacing, nPos, GetCoreValue(*mpLineDistAtMetricBox, meLNSpaceUnit));
            break;

        default:
            break;
    }

    SfxViewFrame::Current()->GetBindings().GetDispatcher()->ExecuteList(
            SID_ATTR_PARA_LINESPACE, SfxCallMode::RECORD, { &aSpacing });
}

void ParaLineSpacingControl::SetLineSpace(SvxLineSpacingItem& rLineSpace, sal_Int32 eSpace, long lValue)
{
    switch ( eSpace )
    {
        case LLINESPACE_1:
            rLineSpace.SetLineSpaceRule( SvxLineSpaceRule::Auto );
            rLineSpace.SetInterLineSpaceRule( SvxInterLineSpaceRule::Off );
            break;

        case LLINESPACE_115:
            rLineSpace.SetLineSpaceRule( SvxLineSpaceRule::Auto );
            rLineSpace.SetPropLineSpace( LINESPACE_115 );
            break;

        case LLINESPACE_15:
            rLineSpace.SetLineSpaceRule( SvxLineSpaceRule::Auto );
            rLineSpace.SetPropLineSpace( LINESPACE_15 );
            break;

        case LLINESPACE_2:
            rLineSpace.SetLineSpaceRule( SvxLineSpaceRule::Auto );
            rLineSpace.SetPropLineSpace( LINESPACE_2 );
            break;

        case LLINESPACE_PROP:
            rLineSpace.SetLineSpaceRule( SvxLineSpaceRule::Auto );
            rLineSpace.SetPropLineSpace( (sal_uInt8)lValue );
            break;

        case LLINESPACE_MIN:
            rLineSpace.SetLineHeight( (sal_uInt16)lValue );
            rLineSpace.SetInterLineSpaceRule( SvxInterLineSpaceRule::Off );
            break;

        case LLINESPACE_DURCH:
            rLineSpace.SetLineSpaceRule( SvxLineSpaceRule::Auto );
            rLineSpace.SetInterLineSpace( (sal_uInt16)lValue );
            break;

        case LLINESPACE_FIX:
            rLineSpace.SetLineHeight((sal_uInt16)lValue);
            rLineSpace.SetLineSpaceRule( SvxLineSpaceRule::Fix );
            rLineSpace.SetInterLineSpaceRule( SvxInterLineSpaceRule::Off );
        break;
    }
}

IMPL_LINK(ParaLineSpacingControl, PredefinedValuesHandler, Button*, pControl, void)
{
    if (pControl == mpSpacing1Button)
    {
        ExecuteLineSpacing(LLINESPACE_1);
    }
    else if (pControl == mpSpacing115Button)
    {
        ExecuteLineSpacing(LLINESPACE_115);
    }
    else if (pControl == mpSpacing15Button)
    {
        ExecuteLineSpacing(LLINESPACE_15);
    }
    else if (pControl == mpSpacing2Button)
    {
        ExecuteLineSpacing(LLINESPACE_2);
    }
}

void ParaLineSpacingControl::ExecuteLineSpacing(sal_Int32 nEntry)
{
    SvxLineSpacingItem aSpacing(DEFAULT_LINE_SPACING, SID_ATTR_PARA_LINESPACE);

    SetLineSpace(aSpacing, nEntry);

    SfxViewFrame::Current()->GetBindings().GetDispatcher()->ExecuteList(
            SID_ATTR_PARA_LINESPACE, SfxCallMode::RECORD, { &aSpacing });

    // close when the user used the buttons
    EndPopupMode();
}

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