summaryrefslogtreecommitdiff
path: root/cui/source/dialogs/zoom.cxx
blob: a93d18ff01f80653540e24e801b95f294e7c90c9 (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
453
454
455
456
/* -*- 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 <svl/itemset.hxx>
#include <svl/itempool.hxx>
#include <sfx2/objsh.hxx>
#include <vcl/layout.hxx>
#include <vcl/msgbox.hxx>

#include <cuires.hrc>

#include "zoom.hxx"
#include <sfx2/zoomitem.hxx>
#include <svx/viewlayoutitem.hxx>
#include <dialmgr.hxx>
#include <svx/zoom_def.hxx>

namespace
{

const sal_uInt16 SPECIAL_FACTOR = 0xFFFF;

} // anonymous namespace

sal_uInt16 SvxZoomDialog::GetFactor() const
{
    if (m_p100Btn->IsChecked())
        return 100;

    if (m_pUserBtn->IsChecked())
        return static_cast<sal_uInt16>(m_pUserEdit->GetValue());
    else
        return SPECIAL_FACTOR;
}

void SvxZoomDialog::SetFactor(sal_uInt16 nNewFactor, ZoomButtonId nButtonId)
{
    m_pUserEdit->Disable();

    if (nButtonId == ZoomButtonId::NONE)
    {
        if ( nNewFactor == 100 )
        {
            m_p100Btn->Check();
            m_p100Btn->GrabFocus();
        }
        else
        {
            m_pUserBtn->Check();
            m_pUserEdit->Enable();
            m_pUserEdit->SetValue(static_cast<long>(nNewFactor));
            m_pUserEdit->GrabFocus();
        }
    }
    else
    {
        m_pUserEdit->SetValue(static_cast<long>(nNewFactor));
        switch(nButtonId)
        {
            case ZoomButtonId::OPTIMAL:
            {
                m_pOptimalBtn->Check();
                m_pOptimalBtn->GrabFocus();
                break;
            }
            case ZoomButtonId::PAGEWIDTH:
            {
                m_pPageWidthBtn->Check();
                m_pPageWidthBtn->GrabFocus();
                break;
            }
            case ZoomButtonId::WHOLEPAGE:
            {
                m_pWholePageBtn->Check();
                m_pWholePageBtn->GrabFocus();
                break;
            }
            default: break;
        }
    }
}

void SvxZoomDialog::HideButton(ZoomButtonId nButtonId)
{
    switch (nButtonId)
    {
        case ZoomButtonId::OPTIMAL:
            m_pOptimalBtn->Hide();
            break;

        case ZoomButtonId::PAGEWIDTH:
            m_pPageWidthBtn->Hide();
            break;

        case ZoomButtonId::WHOLEPAGE:
            m_pWholePageBtn->Hide();
            break;

        default:
            OSL_FAIL("Wrong button number!" );
    }
}

void SvxZoomDialog::SetLimits(sal_uInt16 nMin, sal_uInt16 nMax)
{
    DBG_ASSERT(nMin < nMax, "invalid limits");
    m_pUserEdit->SetMin(nMin);
    m_pUserEdit->SetFirst(nMin);
    m_pUserEdit->SetMax(nMax);
    m_pUserEdit->SetLast(nMax);
}

const SfxItemSet* SvxZoomDialog::GetOutputItemSet() const
{
    return mpOutSet.get();
}

SvxZoomDialog::SvxZoomDialog( vcl::Window* pParent, const SfxItemSet& rCoreSet )
    : SfxModalDialog(pParent, "ZoomDialog", "cui/ui/zoomdialog.ui")
    , mrSet(rCoreSet)
    , mpOutSet()
    , mbModified(false)

{
    get(m_pOptimalBtn, "optimal");
    get(m_pWholePageBtn, "fitwandh");
    get(m_pPageWidthBtn, "fitw");
    get(m_p100Btn, "100pc");
    get(m_pUserBtn, "variable");
    get(m_pUserEdit, "zoomsb");
    get(m_pViewFrame, "viewframe");
    get(m_pAutomaticBtn, "automatic");
    get(m_pSingleBtn, "singlepage");
    get(m_pColumnsBtn, "columns");
    get(m_pColumnsEdit, "columnssb");
    get(m_pBookModeChk, "bookmode");
    get(m_pOKBtn, "ok");
    Link<Button*,void> aLink = LINK(this, SvxZoomDialog, UserHdl);
    m_p100Btn->SetClickHdl(aLink);
    m_pOptimalBtn->SetClickHdl(aLink);
    m_pPageWidthBtn->SetClickHdl(aLink);
    m_pWholePageBtn->SetClickHdl(aLink);
    m_pUserBtn->SetClickHdl(aLink);

    Link<Button*,void> aViewLayoutLink = LINK(this, SvxZoomDialog, ViewLayoutUserHdl);
    m_pAutomaticBtn->SetClickHdl(aViewLayoutLink);
    m_pSingleBtn->SetClickHdl(aViewLayoutLink);
    m_pColumnsBtn->SetClickHdl(aViewLayoutLink);

    Link<Edit&,void> aViewLayoutSpinLink = LINK(this, SvxZoomDialog, ViewLayoutSpinHdl);
    m_pColumnsEdit->SetModifyHdl(aViewLayoutSpinLink);

    Link<Button*,void> aViewLayoutCheckLink = LINK(this, SvxZoomDialog, ViewLayoutCheckHdl);
    m_pBookModeChk->SetClickHdl(aViewLayoutCheckLink);

    m_pOKBtn->SetClickHdl(LINK(this, SvxZoomDialog, OKHdl));
    m_pUserEdit->SetModifyHdl(LINK(this, SvxZoomDialog, SpinHdl));

    // default values
    sal_uInt16 nValue = 100;
    sal_uInt16 nMin = 10;
    sal_uInt16 nMax = 1000;

    // maybe get the old value first
    const SfxUInt16Item* pOldUserItem = nullptr;
    SfxObjectShell* pShell = SfxObjectShell::Current();

    if (pShell)
        pOldUserItem = static_cast<const SfxUInt16Item*>(pShell->GetItem(SID_ATTR_ZOOM_USER));

    if (pOldUserItem)
        nValue = pOldUserItem->GetValue();

    // initialize UserEdit
    if (nMin > nValue)
        nMin = nValue;
    if (nMax < nValue)
        nMax = nValue;

    SetLimits(nMin, nMax);
    m_pUserEdit->SetValue(nValue);

    const SfxPoolItem& rItem = mrSet.Get(mrSet.GetPool()->GetWhich(SID_ATTR_ZOOM));

    if (nullptr != dynamic_cast<const SvxZoomItem*>( &rItem))
    {
        const SvxZoomItem& rZoomItem = static_cast<const SvxZoomItem&>(rItem);
        const sal_uInt16 nZoom = rZoomItem.GetValue();
        const SvxZoomType eType = rZoomItem.GetType();
        const SvxZoomEnableFlags nValSet = rZoomItem.GetValueSet();
        ZoomButtonId nButtonId = ZoomButtonId::NONE;

        switch (eType)
        {
            case SvxZoomType::OPTIMAL:
                nButtonId = ZoomButtonId::OPTIMAL;
                break;
            case SvxZoomType::PAGEWIDTH:
                nButtonId = ZoomButtonId::PAGEWIDTH;
                break;
            case SvxZoomType::WHOLEPAGE:
                nButtonId = ZoomButtonId::WHOLEPAGE;
                break;
            case SvxZoomType::PERCENT:
                break;
            case SvxZoomType::PAGEWIDTH_NOBORDER:
                break;
        }

        if (!(SvxZoomEnableFlags::N100 & nValSet))
            m_p100Btn->Disable();
        if (!(SvxZoomEnableFlags::OPTIMAL & nValSet))
            m_pOptimalBtn->Disable();
        if (!(SvxZoomEnableFlags::PAGEWIDTH & nValSet))
            m_pPageWidthBtn->Disable();
        if (!(SvxZoomEnableFlags::WHOLEPAGE & nValSet))
            m_pWholePageBtn->Disable();

        SetFactor(nZoom, nButtonId);
    }
    else
    {
        const sal_uInt16 nZoom = static_cast<const SfxUInt16Item&>(rItem).GetValue();
        SetFactor(nZoom);
    }

    const SfxPoolItem* pPoolViewLayoutItem = nullptr;
    if (SfxItemState::SET == mrSet.GetItemState(SID_ATTR_VIEWLAYOUT, false, &pPoolViewLayoutItem))
    {
        const SvxViewLayoutItem* pViewLayoutItem = static_cast<const SvxViewLayoutItem*>(pPoolViewLayoutItem);
        const sal_uInt16 nColumns = pViewLayoutItem->GetValue();
        const bool bBookMode  = pViewLayoutItem->IsBookMode();

        if (0 == nColumns)
        {
            m_pAutomaticBtn->Check();
            m_pColumnsEdit->SetValue(2);
            m_pColumnsEdit->Disable();
            m_pBookModeChk->Disable();
        }
        else if (1 == nColumns)
        {
            m_pSingleBtn->Check();
            m_pColumnsEdit->SetValue(2);
            m_pColumnsEdit->Disable();
            m_pBookModeChk->Disable();
        }
        else
        {
            m_pColumnsBtn->Check();
            if (!bBookMode)
            {
                m_pColumnsEdit->SetValue(nColumns);
                if (nColumns % 2 != 0)
                    m_pBookModeChk->Disable();
            }
            else
            {
                m_pColumnsEdit->SetValue(nColumns);
                m_pBookModeChk->Check();
            }
        }
    }
    else
    {
        // hide view layout related controls:
        m_pViewFrame->Disable();
    }
}

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

void SvxZoomDialog::dispose()
{
    mpOutSet.reset();
    m_pOptimalBtn.clear();
    m_pWholePageBtn.clear();
    m_pPageWidthBtn.clear();
    m_p100Btn.clear();
    m_pUserBtn.clear();
    m_pUserEdit.clear();
    m_pViewFrame.clear();
    m_pAutomaticBtn.clear();
    m_pSingleBtn.clear();
    m_pColumnsBtn.clear();
    m_pColumnsEdit.clear();
    m_pBookModeChk.clear();
    m_pOKBtn.clear();
    SfxModalDialog::dispose();
}

IMPL_LINK(SvxZoomDialog, UserHdl, Button *, pButton, void)
{
    mbModified = true;

    if (pButton == m_pUserBtn)
    {
        m_pUserEdit->Enable();
        m_pUserEdit->GrabFocus();
    }
    else
    {
        m_pUserEdit->Disable();
    }
}

IMPL_LINK_NOARG(SvxZoomDialog, SpinHdl, Edit&, void)
{
    if (!m_pUserBtn->IsChecked())
        return;

    mbModified = true;
}

IMPL_LINK(SvxZoomDialog, ViewLayoutUserHdl, Button*, pButton, void)
{
    mbModified = true;

    if (pButton == m_pAutomaticBtn)
    {
        m_pColumnsEdit->Disable();
        m_pBookModeChk->Disable();
    }
    else if (pButton == m_pSingleBtn)
    {
        m_pColumnsEdit->Disable();
        m_pBookModeChk->Disable();
    }
    else if (pButton == m_pColumnsBtn)
    {
        m_pColumnsEdit->Enable();
        m_pColumnsEdit->GrabFocus();
        if (m_pColumnsEdit->GetValue() % 2 == 0)
            m_pBookModeChk->Enable();
    }
    else
    {
        OSL_FAIL("Wrong Button");
    }
}

IMPL_LINK(SvxZoomDialog, ViewLayoutSpinHdl, Edit&, rEdit, void)
{
    if (&rEdit == m_pColumnsEdit && !m_pColumnsBtn->IsChecked())
        return;

    if (m_pColumnsEdit->GetValue() % 2 == 0)
    {
        m_pBookModeChk->Enable();
    }
    else
    {
        m_pBookModeChk->Check(false);
        m_pBookModeChk->Disable();
    }

    mbModified = true;
}

IMPL_LINK(SvxZoomDialog, ViewLayoutCheckHdl, Button*, pCheckBox, void)
{
    if (pCheckBox == m_pBookModeChk && !m_pColumnsBtn->IsChecked())
        return;

    mbModified = true;
}

IMPL_LINK(SvxZoomDialog, OKHdl, Button*, pButton, void)
{
    if (mbModified || m_pOKBtn != pButton)
    {
        SvxZoomItem aZoomItem(SvxZoomType::PERCENT, 0, mrSet.GetPool()->GetWhich(SID_ATTR_ZOOM));
        SvxViewLayoutItem aViewLayoutItem(0, false, mrSet.GetPool()->GetWhich(SID_ATTR_VIEWLAYOUT));

        if (m_pOKBtn == pButton)
        {
            sal_uInt16 nFactor = GetFactor();

            if (SPECIAL_FACTOR == nFactor)
            {
                if (m_pOptimalBtn->IsChecked())
                    aZoomItem.SetType(SvxZoomType::OPTIMAL);
                else if (m_pPageWidthBtn->IsChecked())
                    aZoomItem.SetType(SvxZoomType::PAGEWIDTH);
                else if (m_pWholePageBtn->IsChecked())
                    aZoomItem.SetType(SvxZoomType::WHOLEPAGE);
            }
            else
            {
                aZoomItem.SetValue(nFactor);
            }

            if (m_pAutomaticBtn->IsChecked())
            {
                aViewLayoutItem.SetValue(0);
                aViewLayoutItem.SetBookMode(false);
            }
            if (m_pSingleBtn->IsChecked())
            {
                aViewLayoutItem.SetValue(1);
                aViewLayoutItem.SetBookMode(false);
            }
            else if (m_pColumnsBtn->IsChecked())
            {
                aViewLayoutItem.SetValue(static_cast<sal_uInt16>(m_pColumnsEdit->GetValue()));
                aViewLayoutItem.SetBookMode(m_pBookModeChk->IsChecked());
            }
        }
        else
        {
            OSL_FAIL("Wrong Button");
            return;
        }
        mpOutSet.reset(new SfxItemSet(mrSet));
        mpOutSet->Put(aZoomItem);

        // don't set attribute in case the whole viewlayout stuff is disabled:
        if (m_pViewFrame->IsEnabled())
            mpOutSet->Put(aViewLayoutItem);

        // memorize value from the UserEdit beyond the dialog
        SfxObjectShell* pShell = SfxObjectShell::Current();

        if (pShell)
        {
            sal_uInt16 nZoomValue = static_cast<sal_uInt16>(m_pUserEdit->GetValue());
            pShell->PutItem(SfxUInt16Item(SID_ATTR_ZOOM_USER, nZoomValue));
        }
        EndDialog( RET_OK );
    }
    else
    {
        EndDialog();
    }
}

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