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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
/* -*- 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 <align.hxx>

#include <editeng/svxenum.hxx>
#include <svx/svxids.hrc>
#include <svx/strings.hrc>
#include <svx/dialmgr.hxx>
#include <bitmaps.hlst>
#include <svx/rotmodit.hxx>
#include <svx/sdangitm.hxx>

#include <editeng/frmdiritem.hxx>
#include <editeng/justifyitem.hxx>
#include <svl/cjkoptions.hxx>
#include <svl/intitem.hxx>
#include <vcl/image.hxx>

#define IID_BOTTOMLOCK 1
#define IID_TOPLOCK    2
#define IID_CELLLOCK   3

namespace svx {

const sal_uInt16 AlignmentTabPage::s_pRanges[] =
{
    SID_ATTR_ALIGN_HOR_JUSTIFY,SID_ATTR_ALIGN_VER_JUSTIFY,
    SID_ATTR_ALIGN_STACKED,SID_ATTR_ALIGN_LINEBREAK,
    SID_ATTR_ALIGN_INDENT,SID_ATTR_ALIGN_INDENT,
    SID_ATTR_ALIGN_DEGREES,SID_ATTR_ALIGN_DEGREES,
    SID_ATTR_ALIGN_LOCKPOS,SID_ATTR_ALIGN_LOCKPOS,
    SID_ATTR_ALIGN_HYPHENATION,SID_ATTR_ALIGN_HYPHENATION,
    SID_ATTR_ALIGN_ASIANVERTICAL,SID_ATTR_ALIGN_ASIANVERTICAL,
    SID_ATTR_FRAMEDIRECTION,SID_ATTR_FRAMEDIRECTION,
    SID_ATTR_ALIGN_SHRINKTOFIT,SID_ATTR_ALIGN_SHRINKTOFIT,
    0
};


namespace {

template<typename JustContainerType, typename JustEnumType>
void lcl_MaybeResetAlignToDistro(
    weld::ComboBox& rLB, sal_uInt16 nListPos, const SfxItemSet& rCoreAttrs, sal_uInt16 nWhichAlign, sal_uInt16 nWhichJM, JustEnumType eBlock)
{
    const SfxPoolItem* pItem;
    if (rCoreAttrs.GetItemState(nWhichAlign, true, &pItem) != SfxItemState::SET)
        // alignment not set.
        return;

    const SfxEnumItemInterface* p = static_cast<const SfxEnumItemInterface*>(pItem);
    JustContainerType eVal = static_cast<JustContainerType>(p->GetEnumValue());
    if (eVal != eBlock)
        // alignment is not 'justify'.  No need to go further.
        return;

    if (rCoreAttrs.GetItemState(nWhichJM, true, &pItem) != SfxItemState::SET)
        // justification method is not set.
        return;

    p = static_cast<const SfxEnumItemInterface*>(pItem);
    SvxCellJustifyMethod eMethod = static_cast<SvxCellJustifyMethod>(p->GetEnumValue());
    if (eMethod == SvxCellJustifyMethod::Distribute)
    {
        // Select the 'distribute' entry in the specified list box.
        rLB.set_active(nListPos);
    }
}

void lcl_SetJustifyMethodToItemSet(SfxItemSet& rSet, const SfxItemSet& rOldSet, sal_uInt16 nWhichJM, const weld::ComboBox& rLB, sal_uInt16 nListPos)
{
    SvxCellJustifyMethod eJM = SvxCellJustifyMethod::Auto;
    if (rLB.get_active() == nListPos)
        eJM = SvxCellJustifyMethod::Distribute;

    // tdf#129300 If it would create no change, don't force it
    const SvxJustifyMethodItem& rOldItem = static_cast<const SvxJustifyMethodItem&>(rOldSet.Get(nWhichJM));
    if (rOldItem.GetValue() == eJM)
    {
        rSet.InvalidateItem(nWhichJM);
        return;
    }

    SvxJustifyMethodItem aItem(eJM, nWhichJM);
    rSet.Put(aItem);
}

}//namespace

AlignmentTabPage::AlignmentTabPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rCoreAttrs)
    : SfxTabPage(pPage, pController, "cui/ui/cellalignment.ui", "CellAlignPage", &rCoreAttrs)
    , m_aVsRefEdge(nullptr)
    // text alignment
    , m_xLbHorAlign(m_xBuilder->weld_combo_box("comboboxHorzAlign"))
    , m_xFtIndent(m_xBuilder->weld_label("labelIndent"))
    , m_xEdIndent(m_xBuilder->weld_metric_spin_button("spinIndentFrom", FieldUnit::POINT))
    , m_xFtVerAlign(m_xBuilder->weld_label("labelVertAlign"))
    , m_xLbVerAlign(m_xBuilder->weld_combo_box("comboboxVertAlign"))
    //text rotation
    , m_xFtRotate(m_xBuilder->weld_label("labelDegrees"))
    , m_xNfRotate(m_xBuilder->weld_metric_spin_button("spinDegrees", FieldUnit::DEGREE))
    , m_xFtRefEdge(m_xBuilder->weld_label("labelRefEdge"))
    //Asian mode
    , m_xCbStacked(m_xBuilder->weld_check_button("checkVertStack"))
    , m_xCbAsianMode(m_xBuilder->weld_check_button("checkAsianMode"))
    // Properties
    , m_xBoxDirection(m_xBuilder->weld_widget("boxDirection"))
    , m_xBtnWrap(m_xBuilder->weld_check_button("checkWrapTextAuto"))
    , m_xBtnHyphen(m_xBuilder->weld_check_button("checkHyphActive"))
    , m_xBtnShrink(m_xBuilder->weld_check_button("checkShrinkFitCellSize"))
    , m_xLbFrameDir(new svx::FrameDirectionListBox(m_xBuilder->weld_combo_box("comboTextDirBox")))
    //ValueSet hover strings
    , m_xFtBotLock(m_xBuilder->weld_label("labelSTR_BOTTOMLOCK"))
    , m_xFtTopLock(m_xBuilder->weld_label("labelSTR_TOPLOCK"))
    , m_xFtCelLock(m_xBuilder->weld_label("labelSTR_CELLLOCK"))
    , m_xFtABCD(m_xBuilder->weld_label("labelABCD"))
    , m_xAlignmentFrame(m_xBuilder->weld_widget("alignment"))
    , m_xOrientFrame(m_xBuilder->weld_widget("orientation"))
    , m_xPropertiesFrame(m_xBuilder->weld_widget("properties"))
    , m_xVsRefEdge(new weld::CustomWeld(*m_xBuilder, "references", m_aVsRefEdge))
    , m_xCtrlDial(new DialControl)
    , m_xCtrlDialWin(new weld::CustomWeld(*m_xBuilder, "dialcontrol", *m_xCtrlDial))
{
    m_xCtrlDial->SetLinkedField(m_xNfRotate.get());
    m_xCtrlDial->SetText(m_xFtABCD->get_label());

    InitVsRefEgde();

    m_xLbHorAlign->connect_changed(LINK(this, AlignmentTabPage, UpdateEnableHdl));

    m_xCbStacked->connect_toggled(LINK(this, AlignmentTabPage, StackedClickHdl));
    m_xCbAsianMode->connect_toggled(LINK(this, AlignmentTabPage, AsianModeClickHdl));
    m_xBtnWrap->connect_toggled(LINK(this, AlignmentTabPage, WrapClickHdl));
    m_xBtnHyphen->connect_toggled(LINK(this, AlignmentTabPage, HyphenClickHdl));
    m_xBtnShrink->connect_toggled(LINK(this, AlignmentTabPage, ShrinkClickHdl));

    // Asian vertical mode
    m_xCbAsianMode->set_visible(SvtCJKOptions().IsVerticalTextEnabled());

    m_xLbFrameDir->append(SvxFrameDirection::Horizontal_LR_TB, SvxResId(RID_SVXSTR_FRAMEDIR_LTR));
    m_xLbFrameDir->append(SvxFrameDirection::Horizontal_RL_TB, SvxResId(RID_SVXSTR_FRAMEDIR_RTL));
    m_xLbFrameDir->append(SvxFrameDirection::Environment, SvxResId(RID_SVXSTR_FRAMEDIR_SUPER));

    // This page needs ExchangeSupport.
    SetExchangeSupport();
}

AlignmentTabPage::~AlignmentTabPage()
{
    m_xCtrlDialWin.reset();
    m_xCtrlDial.reset();
    m_xVsRefEdge.reset();
    m_xLbFrameDir.reset();
}

std::unique_ptr<SfxTabPage> AlignmentTabPage::Create(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* rAttrSet)
{
    return std::make_unique<AlignmentTabPage>(pPage, pController, *rAttrSet);
}

bool AlignmentTabPage::FillItemSet( SfxItemSet* rSet )
{
    const SfxItemSet& rOldSet = GetItemSet();

    bool bChanged = SfxTabPage::FillItemSet(rSet);

    sal_uInt16 nWhich = GetWhich(SID_ATTR_ALIGN_HOR_JUSTIFY);
    if (m_xLbHorAlign->get_value_changed_from_saved())
    {
        SvxCellHorJustify eJustify(SvxCellHorJustify::Standard);
        switch (m_xLbHorAlign->get_active_id().toInt32())
        {
            case ALIGNDLG_HORALIGN_STD:
                eJustify = SvxCellHorJustify::Standard;
                break;
            case ALIGNDLG_HORALIGN_LEFT:
                eJustify = SvxCellHorJustify::Left;
                break;
            case ALIGNDLG_HORALIGN_CENTER:
                eJustify = SvxCellHorJustify::Center;
                break;
            case ALIGNDLG_HORALIGN_RIGHT:
                eJustify = SvxCellHorJustify::Right;
                break;
            case ALIGNDLG_HORALIGN_BLOCK:
            case ALIGNDLG_HORALIGN_DISTRIBUTED:
                eJustify = SvxCellHorJustify::Block;
                break;
            case ALIGNDLG_HORALIGN_FILL:
                eJustify = SvxCellHorJustify::Repeat;
                break;
        }
        rSet->Put(SvxHorJustifyItem(eJustify, nWhich));
        bChanged = true;
    }
    else if (SfxItemState::DEFAULT == rOldSet.GetItemState(nWhich, false))
        rSet->InvalidateItem(nWhich);

    nWhich = GetWhich(SID_ATTR_ALIGN_INDENT);
    if (m_xEdIndent->get_value_changed_from_saved())
    {
        const SfxUInt16Item* pIndentItem = static_cast<const SfxUInt16Item*>(GetOldItem(
                                                *rSet, SID_ATTR_ALIGN_INDENT));
        assert(pIndentItem);
        std::unique_ptr<SfxUInt16Item> pNewIndentItem(pIndentItem->Clone());
        pNewIndentItem->SetValue(m_xEdIndent->get_value(FieldUnit::TWIP));
        rSet->Put(*pNewIndentItem);
        bChanged = true;
    }
    else if (SfxItemState::DEFAULT == rOldSet.GetItemState(nWhich, false))
        rSet->InvalidateItem(nWhich);

    nWhich = GetWhich(SID_ATTR_ALIGN_VER_JUSTIFY);
    if (m_xLbVerAlign->get_value_changed_from_saved())
    {
        SvxCellVerJustify eJustify(SvxCellVerJustify::Standard);
        switch (m_xLbVerAlign->get_active_id().toInt32())
        {
            case ALIGNDLG_VERALIGN_STD:
                eJustify = SvxCellVerJustify::Standard;
                break;
            case ALIGNDLG_VERALIGN_TOP:
                eJustify = SvxCellVerJustify::Top;
                break;
            case ALIGNDLG_VERALIGN_MID:
                eJustify = SvxCellVerJustify::Center;
                break;
            case ALIGNDLG_VERALIGN_BOTTOM:
                eJustify = SvxCellVerJustify::Bottom;
                break;
            case ALIGNDLG_VERALIGN_BLOCK:
            case ALIGNDLG_VERALIGN_DISTRIBUTED:
                eJustify = SvxCellVerJustify::Block;
                break;
        }
        rSet->Put(SvxVerJustifyItem(eJustify, nWhich));
        bChanged = true;
    }
    else if (SfxItemState::DEFAULT == rOldSet.GetItemState(nWhich, false))
        rSet->InvalidateItem(nWhich);

    nWhich = GetWhich(SID_ATTR_ALIGN_DEGREES);
    if (m_xNfRotate->get_value_changed_from_saved())
    {
        const SdrAngleItem* pAngleItem = static_cast<const SdrAngleItem*>(GetOldItem(
                                            *rSet, SID_ATTR_ALIGN_DEGREES));
        assert(pAngleItem);
        std::unique_ptr<SdrAngleItem> pNewAngleItem(pAngleItem->Clone());
        pNewAngleItem->SetValue(m_xCtrlDial->GetRotation());
        rSet->Put(*pNewAngleItem);
        bChanged = true;
    }
    else if (SfxItemState::DEFAULT == rOldSet.GetItemState(nWhich, false))
        rSet->InvalidateItem(nWhich);

    nWhich = GetWhich(SID_ATTR_ALIGN_LOCKPOS);
    if (m_aVsRefEdge.IsValueChangedFromSaved())
    {
        switch (m_aVsRefEdge.GetSelectedItemId())
        {
            case IID_CELLLOCK:
                rSet->Put(SvxRotateModeItem(SvxRotateMode::SVX_ROTATE_MODE_STANDARD, nWhich));
                break;
            case IID_TOPLOCK:
                rSet->Put(SvxRotateModeItem(SvxRotateMode::SVX_ROTATE_MODE_TOP, nWhich));
                break;
            case IID_BOTTOMLOCK:
                rSet->Put(SvxRotateModeItem(SvxRotateMode::SVX_ROTATE_MODE_BOTTOM, nWhich));
                break;
            default:
                m_aVsRefEdge.SetNoSelection();
                break;
        }
        bChanged = true;
    }
    else if (SfxItemState::DEFAULT == rOldSet.GetItemState(nWhich, false))
        rSet->InvalidateItem(nWhich);

    nWhich = GetWhich(SID_ATTR_ALIGN_STACKED);
    if (m_xCbStacked->get_state_changed_from_saved())
    {
        const SfxBoolItem* pStackItem = static_cast<const SfxBoolItem*>(GetOldItem(
                                            *rSet, SID_ATTR_ALIGN_STACKED));
        assert(pStackItem);
        std::unique_ptr<SfxBoolItem> pNewStackItem(pStackItem->Clone());
        pNewStackItem->SetValue(m_xCbStacked->get_active());
        rSet->Put(*pNewStackItem);
        bChanged = true;
    }
    else if (SfxItemState::DEFAULT == rOldSet.GetItemState(nWhich, false))
        rSet->InvalidateItem(nWhich);

    nWhich = GetWhich(SID_ATTR_ALIGN_ASIANVERTICAL);
    if (m_xCbAsianMode->get_state_changed_from_saved())
    {
        rSet->Put(SfxBoolItem(nWhich, m_xCbAsianMode->get_active()));
        bChanged = true;
    }
    else if (SfxItemState::DEFAULT == rOldSet.GetItemState(nWhich, false))
        rSet->InvalidateItem(nWhich);

    nWhich = GetWhich(SID_ATTR_ALIGN_LINEBREAK);
    if (m_xBtnWrap->get_state_changed_from_saved())
    {
        const SfxBoolItem* pWrapItem = static_cast<const SfxBoolItem*>(GetOldItem(
                                            *rSet, SID_ATTR_ALIGN_LINEBREAK));
        assert(pWrapItem);
        std::unique_ptr<SfxBoolItem> pNewWrapItem(pWrapItem->Clone());
        pNewWrapItem->SetValue(m_xBtnWrap->get_active());
        rSet->Put(*pNewWrapItem);
        bChanged = true;
    }
    else if (SfxItemState::DEFAULT == rOldSet.GetItemState(nWhich, false))
        rSet->InvalidateItem(nWhich);

    nWhich = GetWhich(SID_ATTR_ALIGN_HYPHENATION);
    if (m_xBtnHyphen->get_state_changed_from_saved())
    {
        const SfxBoolItem* pHyphItem = static_cast<const SfxBoolItem*>(GetOldItem(
                                            *rSet, SID_ATTR_ALIGN_HYPHENATION));
        assert(pHyphItem);
        std::unique_ptr<SfxBoolItem> pNewHyphItem(pHyphItem->Clone());
        pNewHyphItem->SetValue(m_xBtnHyphen->get_active());
        rSet->Put(*pNewHyphItem);
        bChanged = true;
    }
    else if (SfxItemState::DEFAULT == rOldSet.GetItemState(nWhich, false))
        rSet->InvalidateItem(nWhich);

    nWhich = GetWhich(SID_ATTR_ALIGN_SHRINKTOFIT);
    if (m_xBtnShrink->get_state_changed_from_saved())
    {
        const SfxBoolItem* pShrinkItem = static_cast<const SfxBoolItem*>(GetOldItem(
                                            *rSet, SID_ATTR_ALIGN_SHRINKTOFIT));
        assert(pShrinkItem);
        std::unique_ptr<SfxBoolItem> pNewShrinkItem(pShrinkItem->Clone());
        pNewShrinkItem->SetValue(m_xBtnShrink->get_active());
        rSet->Put(*pNewShrinkItem);
        bChanged = true;
    }
    else if (SfxItemState::DEFAULT == rOldSet.GetItemState(nWhich, false))
        rSet->InvalidateItem(nWhich);

    if (m_xLbFrameDir->get_visible())
    {
        nWhich = GetWhich(SID_ATTR_FRAMEDIRECTION);
        if (m_xLbFrameDir->get_value_changed_from_saved())
        {
            SvxFrameDirection eDir = m_xLbFrameDir->get_active_id();
            rSet->Put(SvxFrameDirectionItem(eDir, nWhich));
            bChanged = true;
        }
        else if (SfxItemState::DEFAULT == rOldSet.GetItemState(nWhich, false))
            rSet->InvalidateItem(nWhich);
    }

    // Special treatment for distributed alignment; we need to set the justify
    // method to 'distribute' to distinguish from the normal justification.
    sal_uInt16 nWhichHorJM = GetWhich(SID_ATTR_ALIGN_HOR_JUSTIFY_METHOD);
    lcl_SetJustifyMethodToItemSet(*rSet, rOldSet, nWhichHorJM, *m_xLbHorAlign, ALIGNDLG_HORALIGN_DISTRIBUTED);
    if (!bChanged)
        bChanged = HasAlignmentChanged(*rSet, nWhichHorJM);

    sal_uInt16 nWhichVerJM = GetWhich(SID_ATTR_ALIGN_VER_JUSTIFY_METHOD);
    lcl_SetJustifyMethodToItemSet(*rSet, rOldSet, nWhichVerJM, *m_xLbVerAlign, ALIGNDLG_VERALIGN_DISTRIBUTED);
    if (!bChanged)
        bChanged = HasAlignmentChanged(*rSet, nWhichVerJM);

    return bChanged;
}

namespace
{
    void ResetBool(sal_uInt16 nWhich, const SfxItemSet* pSet, weld::CheckButton& rBtn, weld::TriStateEnabled& rTriState)
    {
        SfxItemState eState = pSet->GetItemState(nWhich);
        switch (eState)
        {
            case SfxItemState::UNKNOWN:
                rBtn.hide();
                rTriState.bTriStateEnabled = false;
                break;
            case SfxItemState::DISABLED:
            case SfxItemState::READONLY:
                rBtn.set_sensitive(false);
                rTriState.bTriStateEnabled = false;
                break;
            case SfxItemState::DONTCARE:
                rBtn.set_state(TRISTATE_INDET);
                rTriState.bTriStateEnabled = true;
                break;
            case SfxItemState::DEFAULT:
            case SfxItemState::SET:
            {
                const SfxBoolItem& rItem = static_cast<const SfxBoolItem&>(pSet->Get(nWhich));
                rBtn.set_state(static_cast<TriState>(rItem.GetValue()));
                rTriState.bTriStateEnabled = false;
                break;
            }
        }
        rBtn.save_state();
    }
}

void AlignmentTabPage::Reset(const SfxItemSet* pCoreAttrs)
{
    SfxTabPage::Reset(pCoreAttrs);

    ResetBool(GetWhich(SID_ATTR_ALIGN_STACKED), pCoreAttrs, *m_xCbStacked, m_aStackedState);
    ResetBool(GetWhich(SID_ATTR_ALIGN_ASIANVERTICAL), pCoreAttrs, *m_xCbAsianMode, m_aAsianModeState);
    ResetBool(GetWhich(SID_ATTR_ALIGN_LINEBREAK), pCoreAttrs, *m_xBtnWrap, m_aWrapState);
    ResetBool(GetWhich(SID_ATTR_ALIGN_HYPHENATION), pCoreAttrs, *m_xBtnHyphen, m_aHyphenState);
    ResetBool(GetWhich(SID_ATTR_ALIGN_SHRINKTOFIT), pCoreAttrs, *m_xBtnShrink, m_aShrinkState);

    sal_uInt16 nWhich = GetWhich(SID_ATTR_ALIGN_HOR_JUSTIFY);
    SfxItemState eState = pCoreAttrs->GetItemState(nWhich);
    switch (eState)
    {
        case SfxItemState::UNKNOWN:
            m_xLbHorAlign->hide();
            break;
        case SfxItemState::DISABLED:
        case SfxItemState::READONLY:
            m_xLbHorAlign->set_sensitive(false);
            break;
        case SfxItemState::DONTCARE:
            m_xLbHorAlign->set_active(-1);
            break;
        case SfxItemState::DEFAULT:
        case SfxItemState::SET:
        {
            const SvxHorJustifyItem& rJustifyItem = static_cast<const SvxHorJustifyItem&>(pCoreAttrs->Get(nWhich));
            switch (rJustifyItem.GetValue())
            {
                case SvxCellHorJustify::Standard:
                    m_xLbHorAlign->set_active_id(OUString::number(ALIGNDLG_HORALIGN_STD));
                    break;
                case SvxCellHorJustify::Left:
                    m_xLbHorAlign->set_active_id(OUString::number(ALIGNDLG_HORALIGN_LEFT));
                    break;
                case SvxCellHorJustify::Center:
                    m_xLbHorAlign->set_active_id(OUString::number(ALIGNDLG_HORALIGN_CENTER));
                    break;
                case SvxCellHorJustify::Right:
                    m_xLbHorAlign->set_active_id(OUString::number(ALIGNDLG_HORALIGN_RIGHT));
                    break;
                case SvxCellHorJustify::Block:
                    m_xLbHorAlign->set_active_id(OUString::number(ALIGNDLG_HORALIGN_BLOCK));
                    break;
                case SvxCellHorJustify::Repeat:
                    m_xLbHorAlign->set_active_id(OUString::number(ALIGNDLG_HORALIGN_FILL));
                    break;
            }
            break;
        }
    }

    nWhich = GetWhich(SID_ATTR_ALIGN_INDENT);
    eState = pCoreAttrs->GetItemState(nWhich);
    switch (eState)
    {
        case SfxItemState::UNKNOWN:
            m_xEdIndent->hide();
            m_xFtIndent->hide();
            break;
        case SfxItemState::DISABLED:
        case SfxItemState::READONLY:
            m_xEdIndent->set_sensitive(false);
            break;
        case SfxItemState::DONTCARE:
            m_xEdIndent->set_text("");
            break;
        case SfxItemState::DEFAULT:
        case SfxItemState::SET:
        {
            const SfxUInt16Item& rIndentItem = static_cast<const SfxUInt16Item&>(pCoreAttrs->Get(nWhich));
            m_xEdIndent->set_value(rIndentItem.GetValue(), FieldUnit::TWIP);
            break;
        }
    }

    nWhich = GetWhich(SID_ATTR_ALIGN_VER_JUSTIFY);
    eState = pCoreAttrs->GetItemState(nWhich);
    switch (eState)
    {
        case SfxItemState::UNKNOWN:
            m_xLbVerAlign->hide();
            m_xFtVerAlign->hide();
            break;
        case SfxItemState::DISABLED:
        case SfxItemState::READONLY:
            m_xLbVerAlign->set_sensitive(false);
            break;
        case SfxItemState::DONTCARE:
            m_xLbVerAlign->set_active(-1);
            break;
        case SfxItemState::DEFAULT:
        case SfxItemState::SET:
        {
            const SvxVerJustifyItem& rJustifyItem = static_cast<const SvxVerJustifyItem&>(pCoreAttrs->Get(nWhich));
            switch (rJustifyItem.GetValue())
            {
                case SvxCellVerJustify::Standard:
                    m_xLbVerAlign->set_active_id(OUString::number(ALIGNDLG_VERALIGN_STD));
                    break;
                case SvxCellVerJustify::Top:
                    m_xLbVerAlign->set_active_id(OUString::number(ALIGNDLG_VERALIGN_TOP));
                    break;
                case SvxCellVerJustify::Center:
                    m_xLbVerAlign->set_active_id(OUString::number(ALIGNDLG_VERALIGN_MID));
                    break;
                case SvxCellVerJustify::Bottom:
                    m_xLbVerAlign->set_active_id(OUString::number(ALIGNDLG_VERALIGN_BOTTOM));
                    break;
                case SvxCellVerJustify::Block:
                    m_xLbVerAlign->set_active_id(OUString::number(ALIGNDLG_VERALIGN_BLOCK));
                    break;
            }
            break;
        }
    }

    nWhich = GetWhich(SID_ATTR_ALIGN_DEGREES);
    eState = pCoreAttrs->GetItemState(nWhich);
    switch (eState)
    {
        case SfxItemState::UNKNOWN:
            m_xNfRotate->hide();
            m_xCtrlDialWin->hide();
            break;
        case SfxItemState::DISABLED:
        case SfxItemState::READONLY:
            m_xNfRotate->set_sensitive(false);
            m_xCtrlDialWin->set_sensitive(false);
            break;
        case SfxItemState::DONTCARE:
            m_xCtrlDial->SetNoRotation();
            break;
        case SfxItemState::DEFAULT:
        case SfxItemState::SET:
        {
            const SdrAngleItem& rAlignItem = static_cast<const SdrAngleItem&>(pCoreAttrs->Get(nWhich));
            m_xCtrlDial->SetRotation(rAlignItem.GetValue());
            break;
        }
    }

    nWhich = GetWhich(SID_ATTR_ALIGN_LOCKPOS);
    eState = pCoreAttrs->GetItemState(nWhich);
    switch (eState)
    {
        case SfxItemState::UNKNOWN:
            m_xVsRefEdge->hide();
            break;
        case SfxItemState::DISABLED:
        case SfxItemState::READONLY:
            m_xVsRefEdge->set_sensitive(false);
            break;
        case SfxItemState::DONTCARE:
            m_aVsRefEdge.SetNoSelection();
            break;
        case SfxItemState::DEFAULT:
        case SfxItemState::SET:
        {
            const SvxRotateModeItem& rRotateModeItem = static_cast<const SvxRotateModeItem&>(pCoreAttrs->Get(nWhich));
            switch (rRotateModeItem.GetValue())
            {
                case SvxRotateMode::SVX_ROTATE_MODE_STANDARD:
                    m_aVsRefEdge.SelectItem(IID_CELLLOCK);
                    break;
                case SvxRotateMode::SVX_ROTATE_MODE_TOP:
                    m_aVsRefEdge.SelectItem(IID_TOPLOCK);
                    break;
                case SvxRotateMode::SVX_ROTATE_MODE_BOTTOM:
                    m_aVsRefEdge.SelectItem(IID_BOTTOMLOCK);
                    break;
                default:
                    m_aVsRefEdge.SetNoSelection();
                    break;
            }
            break;
        }
    }
    m_aVsRefEdge.SaveValue();

    //text direction
    nWhich = GetWhich(SID_ATTR_FRAMEDIRECTION);
    eState = pCoreAttrs->GetItemState(nWhich);
    switch (eState)
    {
        case SfxItemState::UNKNOWN:
            m_xLbFrameDir->hide();
            break;
        case SfxItemState::DISABLED:
        case SfxItemState::READONLY:
            m_xLbFrameDir->set_sensitive(false);
            break;
        case SfxItemState::DONTCARE:
            m_xLbFrameDir->set_active(-1);
            break;
        case SfxItemState::DEFAULT:
        case SfxItemState::SET:
        {
            const SvxFrameDirectionItem& rFrameDirItem = static_cast<const SvxFrameDirectionItem&>(pCoreAttrs->Get(nWhich));
            m_xLbFrameDir->set_active_id(rFrameDirItem.GetValue());
            break;
        }
    }


    // Special treatment for distributed alignment; we need to set the justify
    // method to 'distribute' to distinguish from the normal justification.

    lcl_MaybeResetAlignToDistro<SvxCellHorJustify, SvxCellHorJustify>(
        *m_xLbHorAlign, ALIGNDLG_HORALIGN_DISTRIBUTED, *pCoreAttrs,
        GetWhich(SID_ATTR_ALIGN_HOR_JUSTIFY), GetWhich(SID_ATTR_ALIGN_HOR_JUSTIFY_METHOD),
        SvxCellHorJustify::Block);

    lcl_MaybeResetAlignToDistro<SvxCellVerJustify, SvxCellVerJustify>(
        *m_xLbVerAlign, ALIGNDLG_VERALIGN_DISTRIBUTED, *pCoreAttrs,
        GetWhich(SID_ATTR_ALIGN_VER_JUSTIFY), GetWhich(SID_ATTR_ALIGN_VER_JUSTIFY_METHOD),
        SvxCellVerJustify::Block);

    m_xLbHorAlign->save_value();
    m_xLbFrameDir->save_value();
    m_xLbVerAlign->save_value();
    m_xNfRotate->save_value();
    m_xEdIndent->save_value();

    UpdateEnableControls();
}

DeactivateRC AlignmentTabPage::DeactivatePage( SfxItemSet* _pSet )
{
    if( _pSet )
        FillItemSet( _pSet );
    return DeactivateRC::LeavePage;
}

void AlignmentTabPage::InitVsRefEgde()
{
    // remember selection - is deleted in call to ValueSet::Clear()
    sal_uInt16 nSel = m_aVsRefEdge.GetSelectedItemId();

    Image aBottomLock(StockImage::Yes, RID_SVXBMP_BOTTOMLOCK);
    Image aTopLock(StockImage::Yes, RID_SVXBMP_TOPLOCK);
    Image aCellLock(StockImage::Yes, RID_SVXBMP_CELLLOCK);

    m_aVsRefEdge.Clear();
    m_aVsRefEdge.SetStyle(m_aVsRefEdge.GetStyle() | WB_ITEMBORDER | WB_DOUBLEBORDER);

    m_aVsRefEdge.SetColCount(3);
    m_aVsRefEdge.InsertItem(IID_BOTTOMLOCK, aBottomLock,  m_xFtBotLock->get_label());
    m_aVsRefEdge.InsertItem(IID_TOPLOCK,    aTopLock,     m_xFtTopLock->get_label());
    m_aVsRefEdge.InsertItem(IID_CELLLOCK,   aCellLock,    m_xFtCelLock->get_label());
    m_aVsRefEdge.SetOptimalSize();

    m_aVsRefEdge.SelectItem( nSel );
}

void AlignmentTabPage::UpdateEnableControls()
{
    const sal_Int32 nHorAlign = m_xLbHorAlign->get_active();
    bool bHorLeft  = (nHorAlign == ALIGNDLG_HORALIGN_LEFT);
    bool bHorBlock = (nHorAlign == ALIGNDLG_HORALIGN_BLOCK);
    bool bHorFill  = (nHorAlign == ALIGNDLG_HORALIGN_FILL);
    bool bHorDist  = (nHorAlign == ALIGNDLG_HORALIGN_DISTRIBUTED);

    // indent edit field only for left alignment
    m_xFtIndent->set_sensitive( bHorLeft );
    m_xEdIndent->set_sensitive( bHorLeft );

    // stacked disabled for fill alignment
    m_xCbStacked->set_sensitive(!bHorFill);

    // hyphenation only for automatic line breaks or for block alignment
    m_xBtnHyphen->set_sensitive( m_xBtnWrap->get_active() || bHorBlock );

    // shrink only without automatic line break, and not for block, fill or distribute.
    m_xBtnShrink->set_sensitive( (m_xBtnWrap->get_state() == TRISTATE_FALSE) && !bHorBlock && !bHorFill && !bHorDist );

    // visibility of frames
    m_xAlignmentFrame->set_visible(m_xLbHorAlign->get_visible() || m_xEdIndent->get_visible() ||
        m_xLbVerAlign->get_visible());
    m_xOrientFrame->set_visible(m_xCtrlDialWin->get_visible() || m_xVsRefEdge->get_visible() ||
        m_xCbStacked->get_visible() || m_xCbAsianMode->get_visible());
    m_xPropertiesFrame->set_visible(m_xBtnWrap->get_visible() || m_xBtnHyphen->get_visible() ||
        m_xBtnShrink->get_visible() || m_xLbFrameDir->get_visible());

    bool bStackedText = m_xCbStacked->get_active();
    // windows to be disabled, if stacked text is turned ON
    m_xFtRotate->set_sensitive(!bStackedText);
    m_xFtRefEdge->set_sensitive(!bStackedText);
    m_xVsRefEdge->set_sensitive(!bStackedText);
    // windows to be disabled, if stacked text is turned OFF
    m_xCbAsianMode->set_sensitive(bStackedText);
    // rotation/stacked disabled for fill alignment/stacked
    m_xCtrlDialWin->set_sensitive(!bHorFill && !bStackedText);
    m_xNfRotate->set_sensitive(!bHorFill && !bStackedText);
}

bool AlignmentTabPage::HasAlignmentChanged( const SfxItemSet& rNew, sal_uInt16 nWhich ) const
{
    const SfxItemSet& rOld = GetItemSet();
    const SfxPoolItem* pItem;
    SvxCellJustifyMethod eMethodOld = SvxCellJustifyMethod::Auto;
    SvxCellJustifyMethod eMethodNew = SvxCellJustifyMethod::Auto;
    if (rOld.GetItemState(nWhich, true, &pItem) == SfxItemState::SET)
    {
        const SfxEnumItemInterface* p = static_cast<const SfxEnumItemInterface*>(pItem);
        eMethodOld = static_cast<SvxCellJustifyMethod>(p->GetEnumValue());
    }

    if (rNew.GetItemState(nWhich, true, &pItem) == SfxItemState::SET)
    {
        const SfxEnumItemInterface* p = static_cast<const SfxEnumItemInterface*>(pItem);
        eMethodNew = static_cast<SvxCellJustifyMethod>(p->GetEnumValue());
    }

    return eMethodOld != eMethodNew;
}

IMPL_LINK(AlignmentTabPage, StackedClickHdl, weld::ToggleButton&, rToggle, void)<--- syntax error
{
    m_aStackedState.ButtonToggled(rToggle);
    UpdateEnableControls();
}

IMPL_LINK(AlignmentTabPage, AsianModeClickHdl, weld::ToggleButton&, rToggle, void)
{
    m_aAsianModeState.ButtonToggled(rToggle);
}

IMPL_LINK(AlignmentTabPage, WrapClickHdl, weld::ToggleButton&, rToggle, void)
{
    m_aWrapState.ButtonToggled(rToggle);
    UpdateEnableControls();
}

IMPL_LINK(AlignmentTabPage, HyphenClickHdl, weld::ToggleButton&, rToggle, void)
{
    m_aHyphenState.ButtonToggled(rToggle);
}

IMPL_LINK(AlignmentTabPage, ShrinkClickHdl, weld::ToggleButton&, rToggle, void)
{
    m_aShrinkState.ButtonToggled(rToggle);
}

IMPL_LINK_NOARG(AlignmentTabPage, UpdateEnableHdl, weld::ComboBox&, void)
{
    UpdateEnableControls();
}

}

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