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
/* -*- 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 <osl/file.hxx>
#include <sfx2/new.hxx>
#include <vcl/idle.hxx>
#include <vcl/gdimtf.hxx>
#include <vcl/outdev.hxx>
#include <svl/itemset.hxx>
#include <svl/eitem.hxx>
#include <svtools/sfxecode.hxx>
#include <svtools/ehdl.hxx>
#include <tools/urlobj.hxx>
#include <tools/debug.hxx>

#include <sfx2/strings.hrc>
#include <sfx2/sfxsids.hrc>
#include <sfx2/app.hxx>
#include <sfx2/objsh.hxx>
#include <sfx2/sfxresid.hxx>
#include <sfx2/docfile.hxx>
#include <preview.hxx>
#include <unotools/viewoptions.hxx>

void SfxPreviewWin_Impl::SetObjectShell(SfxObjectShell const * pObj)
{
    std::shared_ptr<GDIMetaFile> xFile = pObj
        ? pObj->GetPreviewMetaFile()
        : std::shared_ptr<GDIMetaFile>();
    xMetaFile = xFile;
    Invalidate();
}

SfxPreviewWin_Impl::SfxPreviewWin_Impl()
{
}

void SfxPreviewWin_Impl::ImpPaint(vcl::RenderContext& rRenderContext, GDIMetaFile* pFile)
{
    rRenderContext.SetLineColor();
    rRenderContext.SetFillColor(COL_LIGHTGRAY);
    rRenderContext.DrawRect(tools::Rectangle(Point(0,0), rRenderContext.GetOutputSize()));

    Size aTmpSize = pFile ? pFile->GetPrefSize() : Size(1, 1);
    DBG_ASSERT(!aTmpSize.IsEmpty(), "size of first page is 0, override GetFirstPageSize or set visible-area!");

#define FRAME 4

    long nWidth = rRenderContext.GetOutputSize().Width() - 2 * FRAME;
    long nHeight = rRenderContext.GetOutputSize().Height() - 2 * FRAME;
    if (nWidth <= 0 || nHeight <= 0)
        return;

    double dRatio = aTmpSize.Height() ? (double(aTmpSize.Width()) / aTmpSize.Height()) : 1;
    double dRatioPreV = double(nWidth) / nHeight;
    Size aSize;
    Point aPoint;
    if (dRatio > dRatioPreV)
    {
        aSize = Size(nWidth, sal_uInt16(nWidth / dRatio));
        aPoint = Point(0, sal_uInt16((nHeight - aSize.Height()) / 2));
    }
    else
    {
        aSize = Size(sal_uInt16(nHeight * dRatio), nHeight);
        aPoint = Point(sal_uInt16((nWidth - aSize.Width()) / 2), 0);
    }
    Point bPoint = Point(nWidth, nHeight) - aPoint;

    if (pFile)
    {
        rRenderContext.SetLineColor(COL_BLACK);
        rRenderContext.SetFillColor(COL_WHITE);
        rRenderContext.DrawRect(tools::Rectangle(aPoint + Point(FRAME, FRAME), bPoint + Point(FRAME, FRAME)));
        pFile->WindStart();
        pFile->Play(&rRenderContext, aPoint + Point(FRAME, FRAME), aSize);
    }
}

void SfxPreviewWin_Impl::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle&)
{
    ImpPaint(rRenderContext, xMetaFile.get());
}

IMPL_LINK_NOARG(SfxNewFileDialog, Update, Timer*, void)
{
    if (m_xDocShell.Is())
    {
        if (m_xDocShell->GetProgress())
            return;
        m_xDocShell.Clear();
    }

    const sal_uInt16 nEntry = GetSelectedTemplatePos();
    if (!nEntry)
    {
        m_xPreviewController->Invalidate();
        m_xPreviewController->SetObjectShell(nullptr);
        return;
    }

    if (!m_xMoreBt->get_expanded() || (m_nFlags != SfxNewFileDialogMode::Preview))
        return;

    OUString aFileName = m_aTemplates.GetPath(m_xRegionLb->get_selected_index(), nEntry - 1);
    INetURLObject aTestObj(aFileName);
    if (aTestObj.GetProtocol() == INetProtocol::NotValid)
    {
        // temp. fix until Templates are managed by UCB compatible service
        // does NOT work with locally cached components !
        OUString aTemp;
        osl::FileBase::getFileURLFromSystemPath( aFileName, aTemp );
        aFileName = aTemp;
    }

    INetURLObject aObj(aFileName);
    for (SfxObjectShell* pTmp = SfxObjectShell::GetFirst(); pTmp; pTmp = SfxObjectShell::GetNext(*pTmp))
    {
        //! fsys bug op==
        if (pTmp->GetMedium())
            // ??? HasName() MM
            if (INetURLObject( pTmp->GetMedium()->GetName() ) == aObj)
            {
                m_xDocShell = pTmp;
                break;
            }
    }

    if (!m_xDocShell.Is())
    {
        SfxErrorContext eEC(ERRCTX_SFX_LOADTEMPLATE, m_xDialog.get());
        SfxApplication *pSfxApp = SfxGetpApp();
        std::unique_ptr<SfxItemSet> pSet(new SfxAllItemSet(pSfxApp->GetPool()));
        pSet->Put(SfxBoolItem(SID_TEMPLATE, true));
        pSet->Put(SfxBoolItem(SID_PREVIEW, true));
        ErrCode lErr = pSfxApp->LoadTemplate(m_xDocShell, aFileName, std::move(pSet));
        if (lErr)
            ErrorHandler::HandleError(lErr);
        if (!m_xDocShell.Is())
        {
            m_xPreviewController->SetObjectShell(nullptr);
            return;
        }
    }

    m_xPreviewController->SetObjectShell(m_xDocShell);
}

IMPL_LINK( SfxNewFileDialog, RegionSelect, weld::TreeView&, rBox, void )
{
    if (m_xDocShell.Is() && m_xDocShell->GetProgress())
        return;

    const sal_uInt16 nRegion = rBox.get_selected_index();
    const sal_uInt16 nCount = m_aTemplates.GetRegionCount() ? m_aTemplates.GetCount(nRegion): 0;
    m_xTemplateLb->freeze();
    m_xTemplateLb->clear();
    OUString aSel = m_xRegionLb->get_selected_text();
    sal_Int32 nc = aSel.indexOf('(');
    if (nc != -1 && nc != 0)
        aSel = aSel.replaceAt(nc-1, 1, "");
    if ( aSel.compareToIgnoreAsciiCase( SfxResId(STR_STANDARD) ) == 0 )
        m_xTemplateLb->append_text(SfxResId(STR_NONE));
    for (sal_uInt16 i = 0; i < nCount; ++i)
        m_xTemplateLb->append_text(m_aTemplates.GetName(nRegion, i));
    m_xTemplateLb->thaw();
    if (nCount)
        m_xTemplateLb->select(0);
    TemplateSelect(*m_xTemplateLb);
}

IMPL_LINK_NOARG(SfxNewFileDialog, Expand, weld::Expander&, void)
{
    TemplateSelect(*m_xTemplateLb);
}

IMPL_LINK_NOARG(SfxNewFileDialog, TemplateSelect, weld::TreeView&, void)
{
    // Still loading
    if (m_xDocShell && m_xDocShell->GetProgress())
        return;

    if (!m_xMoreBt->get_expanded())
    {
        // Dialog is not opened
        return;
    }

    m_aPrevIdle.Start();
}

IMPL_LINK_NOARG( SfxNewFileDialog, DoubleClick, weld::TreeView&, bool )
{
    // Still loading
    if (!m_xDocShell.Is() || !m_xDocShell->GetProgress())
        m_xDialog->response(RET_OK);
    return true;
}

sal_uInt16  SfxNewFileDialog::GetSelectedTemplatePos() const
{
    int nEntry = m_xTemplateLb->get_selected_index();
    if (nEntry == -1)
        return 0;
    OUString aSel = m_xRegionLb->get_selected_text();
    sal_Int32 nc = aSel.indexOf('(');
    if (nc != -1 && nc != 0)
        aSel = aSel.replaceAt(nc-1, 1, "");
    if ( aSel.compareToIgnoreAsciiCase(SfxResId(STR_STANDARD)) != 0 )
        nEntry++;
    return nEntry;
}

SfxNewFileDialog::SfxNewFileDialog(weld::Window *pParent, SfxNewFileDialogMode nFlags)
    : SfxDialogController(pParent, "sfx/ui/loadtemplatedialog.ui", "LoadTemplateDialog")
    , m_nFlags(nFlags)
    , m_xPreviewController(new SfxPreviewWin_Impl)
    , m_xRegionLb(m_xBuilder->weld_tree_view("categories"))
    , m_xTemplateLb(m_xBuilder->weld_tree_view("templates"))
    , m_xTextStyleCB(m_xBuilder->weld_check_button("text"))
    , m_xFrameStyleCB(m_xBuilder->weld_check_button("frame"))
    , m_xPageStyleCB(m_xBuilder->weld_check_button("pages"))
    , m_xNumStyleCB(m_xBuilder->weld_check_button("numbering"))
    , m_xMergeStyleCB(m_xBuilder->weld_check_button("overwrite"))
    , m_xLoadFilePB(m_xBuilder->weld_button("fromfile"))
    , m_xMoreBt(m_xBuilder->weld_expander("expander"))
    , m_xPreviewWin(new weld::CustomWeld(*m_xBuilder, "image", *m_xPreviewController))
    , m_xAltTitleFt(m_xBuilder->weld_label("alttitle"))
{
    const int nWidth = m_xRegionLb->get_approximate_digit_width() * 32;
    const int nHeight = m_xRegionLb->get_height_rows(8);
    m_xRegionLb->set_size_request(nWidth, nHeight);
    m_xTemplateLb->set_size_request(nWidth, nHeight);
    m_xPreviewWin->set_size_request(nWidth, nWidth);

    if (nFlags == SfxNewFileDialogMode::NONE)
        m_xMoreBt->hide();
    else if(SfxNewFileDialogMode::LoadTemplate == nFlags)
    {
        m_xLoadFilePB->show();
        m_xTextStyleCB->show();
        m_xFrameStyleCB->show();
        m_xPageStyleCB->show();
        m_xNumStyleCB->show();
        m_xMergeStyleCB->show();
        m_xMoreBt->hide();
        m_xTextStyleCB->set_active(true);
        m_xDialog->set_title(m_xAltTitleFt->get_label());
    }
    else
    {
        m_xMoreBt->connect_expanded(LINK(this, SfxNewFileDialog, Expand));
        m_xPreviewWin->show();
    }

    OUString sExtraData;
    SvtViewOptions aDlgOpt(EViewType::Dialog, OStringToOUString(m_xDialog->get_help_id(), RTL_TEXTENCODING_UTF8));
    if (aDlgOpt.Exists())
    {
        css::uno::Any aUserItem = aDlgOpt.GetUserItem("UserItem");<--- Variable 'aUserItem' is assigned a value that is never used.
        aUserItem >>= sExtraData;<--- Variable 'aUserItem' is assigned a value that is never used.
    }

    bool bExpand = !sExtraData.isEmpty() && sExtraData[0] == 'Y';
    m_xMoreBt->set_expanded(bExpand && (nFlags != SfxNewFileDialogMode::NONE));

    m_xTemplateLb->connect_changed(LINK(this, SfxNewFileDialog, TemplateSelect));
    m_xTemplateLb->connect_row_activated(LINK(this, SfxNewFileDialog, DoubleClick));

    // update the template configuration if necessary
    {
        weld::WaitObject aWaitCursor(m_xDialog.get());
        m_aTemplates.Update();
    }
    // fill the list boxes
    const sal_uInt16 nCount = m_aTemplates.GetRegionCount();
    if (nCount)
    {
        for(sal_uInt16 i = 0; i < nCount; ++i)
            m_xRegionLb->append_text(m_aTemplates.GetFullRegionName(i));
        m_xRegionLb->connect_changed(LINK(this, SfxNewFileDialog, RegionSelect));
    }

    m_aPrevIdle.SetPriority( TaskPriority::LOWEST );
    m_aPrevIdle.SetInvokeHandler( LINK( this, SfxNewFileDialog, Update));

    m_xRegionLb->select(0);
    RegionSelect(*m_xRegionLb);
}

SfxNewFileDialog::~SfxNewFileDialog()
{
    SvtViewOptions aDlgOpt(EViewType::Dialog, OStringToOUString(m_xDialog->get_help_id(), RTL_TEXTENCODING_UTF8));
    aDlgOpt.SetUserItem("UserItem", css::uno::makeAny(m_xMoreBt->get_expanded() ? OUString("Y") : OUString("N")));
}

bool SfxNewFileDialog::IsTemplate() const
{
    return GetSelectedTemplatePos()!=0;
}

OUString SfxNewFileDialog::GetTemplateFileName() const
{
    if (!IsTemplate() || !m_aTemplates.GetRegionCount())
        return OUString();
    return m_aTemplates.GetPath(m_xRegionLb->get_selected_index(),
                              GetSelectedTemplatePos()-1);
}

SfxTemplateFlags SfxNewFileDialog::GetTemplateFlags()const
{
    SfxTemplateFlags nRet = m_xTextStyleCB->get_active() ? SfxTemplateFlags::LOAD_TEXT_STYLES : SfxTemplateFlags::NONE;
    if(m_xFrameStyleCB->get_active())
        nRet |= SfxTemplateFlags::LOAD_FRAME_STYLES;
    if(m_xPageStyleCB->get_active())
        nRet |= SfxTemplateFlags::LOAD_PAGE_STYLES;
    if(m_xNumStyleCB->get_active())
        nRet |= SfxTemplateFlags::LOAD_NUM_STYLES;
    if(m_xMergeStyleCB->get_active())
        nRet |= SfxTemplateFlags::MERGE_STYLES;
    return nRet;
}

void SfxNewFileDialog::SetTemplateFlags(SfxTemplateFlags nSet)
{
    m_xTextStyleCB->set_active(  bool(nSet & SfxTemplateFlags::LOAD_TEXT_STYLES ));
    m_xFrameStyleCB->set_active( bool(nSet & SfxTemplateFlags::LOAD_FRAME_STYLES));
    m_xPageStyleCB->set_active(  bool(nSet & SfxTemplateFlags::LOAD_PAGE_STYLES ));
    m_xNumStyleCB->set_active(   bool(nSet & SfxTemplateFlags::LOAD_NUM_STYLES  ));
    m_xMergeStyleCB->set_active( bool(nSet & SfxTemplateFlags::MERGE_STYLES     ));
}

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