Coverage Report

Created: 2026-10-14 09:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sfx2/source/doc/new.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
#include <osl/file.hxx>
21
#include <sfx2/new.hxx>
22
#include <vcl/idle.hxx>
23
#include <vcl/gdimtf.hxx>
24
#include <vcl/outdev.hxx>
25
#include <svl/itemset.hxx>
26
#include <svl/eitem.hxx>
27
#include <svtools/sfxecode.hxx>
28
#include <svtools/ehdl.hxx>
29
#include <svtools/viewoptions.hxx>
30
#include <tools/urlobj.hxx>
31
#include <tools/debug.hxx>
32
33
#include <sfx2/strings.hrc>
34
#include <sfx2/sfxsids.hrc>
35
#include <sfx2/app.hxx>
36
#include <sfx2/objsh.hxx>
37
#include <sfx2/sfxresid.hxx>
38
#include <sfx2/docfile.hxx>
39
#include <preview.hxx>
40
#include <vcl/vclenum.hxx>
41
#include <vcl/weld/Builder.hxx>
42
#include <vcl/weld/Button.hxx>
43
#include <vcl/weld/CheckButton.hxx>
44
#include <vcl/weld/Dialog.hxx>
45
#include <vcl/weld/Expander.hxx>
46
#include <vcl/weld/Label.hxx>
47
#include <vcl/weld/TreeView.hxx>
48
#include <vcl/weld/WaitObject.hxx>
49
50
void SfxPreviewWin_Impl::SetObjectShell(SfxObjectShell const * pObj)
51
0
{
52
0
    xMetaFile = pObj
53
0
        ? pObj->GetPreviewMetaFile()
54
0
        : std::shared_ptr<GDIMetaFile>();
55
0
    Invalidate();
56
0
}
57
58
SfxPreviewWin_Impl::SfxPreviewWin_Impl()
59
0
{
60
0
}
61
62
void SfxPreviewWin_Impl::ImpPaint(vcl::RenderContext& rRenderContext, GDIMetaFile* pFile)
63
0
{
64
0
    rRenderContext.SetLineColor();
65
0
    rRenderContext.SetFillColor(COL_LIGHTGRAY);
66
0
    rRenderContext.DrawRect(tools::Rectangle(Point(0,0), rRenderContext.GetOutputSize()));
67
68
0
    Size aTmpSize = pFile ? pFile->GetPrefSize() : Size(1, 1);
69
0
    DBG_ASSERT(!aTmpSize.IsEmpty(), "size of first page is 0, override GetFirstPageSize or set visible-area!");
70
71
0
#define FRAME 4
72
73
0
    tools::Long nWidth = rRenderContext.GetOutputSize().Width() - 2 * FRAME;
74
0
    tools::Long nHeight = rRenderContext.GetOutputSize().Height() - 2 * FRAME;
75
0
    if (nWidth <= 0 || nHeight <= 0)
76
0
        return;
77
78
0
    double dRatio = aTmpSize.Height() ? (double(aTmpSize.Width()) / aTmpSize.Height()) : 1;
79
0
    double dRatioPreV = double(nWidth) / nHeight;
80
0
    Size aSize;
81
0
    Point aPoint;
82
0
    if (dRatio > dRatioPreV)
83
0
    {
84
0
        aSize = Size(nWidth, sal_uInt16(nWidth / dRatio));
85
0
        aPoint = Point(0, sal_uInt16((nHeight - aSize.Height()) / 2));
86
0
    }
87
0
    else
88
0
    {
89
0
        aSize = Size(sal_uInt16(nHeight * dRatio), nHeight);
90
0
        aPoint = Point(sal_uInt16((nWidth - aSize.Width()) / 2), 0);
91
0
    }
92
0
    Point bPoint = Point(nWidth, nHeight) - aPoint;
93
94
0
    if (pFile)
95
0
    {
96
0
        rRenderContext.SetLineColor(COL_BLACK);
97
0
        rRenderContext.SetFillColor(COL_WHITE);
98
0
        rRenderContext.DrawRect(tools::Rectangle(aPoint + Point(FRAME, FRAME), bPoint + Point(FRAME, FRAME)));
99
0
        pFile->WindStart();
100
0
        pFile->Play(rRenderContext, aPoint + Point(FRAME, FRAME), aSize);
101
0
    }
102
0
}
103
104
void SfxPreviewWin_Impl::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle&)
105
0
{
106
0
    ImpPaint(rRenderContext, xMetaFile.get());
107
0
}
108
109
IMPL_LINK_NOARG(SfxNewFileDialog, Update, Timer*, void)
110
0
{
111
0
    if (m_xDocShell.Is())
112
0
    {
113
0
        if (m_xDocShell->GetProgress())
114
0
            return;
115
0
        m_xDocShell.Clear();
116
0
    }
117
118
0
    const sal_uInt16 nEntry = GetSelectedTemplatePos();
119
0
    if (!nEntry)
120
0
    {
121
0
        m_xPreviewController->Invalidate();
122
0
        m_xPreviewController->SetObjectShell(nullptr);
123
0
        return;
124
0
    }
125
126
0
    if (!m_xMoreBt->get_expanded() || (m_nFlags != SfxNewFileDialogMode::Preview))
127
0
        return;
128
129
0
    OUString aFileName = m_aTemplates.GetPath(m_xRegionLb->get_selected_index(), nEntry - 1);
130
0
    INetURLObject aTestObj(aFileName);
131
0
    if (aTestObj.GetProtocol() == INetProtocol::NotValid)
132
0
    {
133
        // temp. fix until Templates are managed by UCB compatible service
134
        // does NOT work with locally cached components !
135
0
        OUString aTemp;
136
0
        osl::FileBase::getFileURLFromSystemPath( aFileName, aTemp );
137
0
        aFileName = aTemp;
138
0
    }
139
140
0
    INetURLObject aObj(aFileName);
141
0
    for (SfxObjectShell* pTmp = SfxObjectShell::GetFirst(); pTmp; pTmp = SfxObjectShell::GetNext(*pTmp))
142
0
    {
143
        //! fsys bug op==
144
0
        if (pTmp->GetMedium())
145
            // ??? HasName() MM
146
0
            if (INetURLObject( pTmp->GetMedium()->GetName() ) == aObj)
147
0
            {
148
0
                m_xDocShell = pTmp;
149
0
                break;
150
0
            }
151
0
    }
152
153
0
    if (!m_xDocShell.Is())
154
0
    {
155
0
        SfxErrorContext eEC(ERRCTX_SFX_LOADTEMPLATE, m_xDialog.get());
156
0
        SfxApplication *pSfxApp = SfxGetpApp();
157
0
        std::unique_ptr<SfxItemSet> pSet(new SfxAllItemSet(pSfxApp->GetPool()));
158
0
        pSet->Put(SfxBoolItem(SID_TEMPLATE, true));
159
0
        pSet->Put(SfxBoolItem(SID_PREVIEW, true));
160
0
        ErrCodeMsg lErr = pSfxApp->LoadTemplate(m_xDocShell, aFileName, std::move(pSet));
161
0
        if (lErr)
162
0
            ErrorHandler::HandleError(lErr);
163
0
        if (!m_xDocShell.Is())
164
0
        {
165
0
            m_xPreviewController->SetObjectShell(nullptr);
166
0
            return;
167
0
        }
168
0
    }
169
170
0
    m_xPreviewController->SetObjectShell(m_xDocShell);
171
0
}
172
173
IMPL_LINK(SfxNewFileDialog, RegionSelect, weld::ItemView&, rBox, void)
174
0
{
175
0
    if (m_xDocShell.Is() && m_xDocShell->GetProgress())
176
0
        return;
177
178
0
    const sal_uInt16 nRegion = rBox.get_selected_index();
179
0
    const sal_uInt16 nCount = m_aTemplates.GetRegionCount() ? m_aTemplates.GetCount(nRegion): 0;
180
0
    m_xTemplateLb->freeze();
181
0
    m_xTemplateLb->clear();
182
0
    OUString aSel = m_xRegionLb->get_selected_text();
183
0
    sal_Int32 nc = aSel.indexOf('(');
184
0
    if (nc != -1 && nc != 0)
185
0
        aSel = aSel.replaceAt(nc-1, 1, u"");
186
0
    if ( aSel.compareToIgnoreAsciiCase( SfxResId(STR_STANDARD) ) == 0 )
187
0
        m_xTemplateLb->append_text(SfxResId(STR_NONE));
188
0
    for (sal_uInt16 i = 0; i < nCount; ++i)
189
0
        m_xTemplateLb->append_text(m_aTemplates.GetName(nRegion, i));
190
0
    m_xTemplateLb->thaw();
191
0
    if (nCount)
192
0
        m_xTemplateLb->select(0);
193
0
    TemplateSelect(*m_xTemplateLb);
194
0
}
195
196
IMPL_LINK_NOARG(SfxNewFileDialog, Expand, weld::Expander&, void)
197
0
{
198
0
    TemplateSelect(*m_xTemplateLb);
199
0
}
200
201
IMPL_LINK_NOARG(SfxNewFileDialog, TemplateSelect, weld::ItemView&, void)
202
0
{
203
    // Still loading
204
0
    if (m_xDocShell && m_xDocShell->GetProgress())
205
0
        return;
206
207
0
    if (!m_xMoreBt->get_expanded())
208
0
    {
209
        // Dialog is not opened
210
0
        return;
211
0
    }
212
213
0
    m_aPrevIdle.Start();
214
0
}
215
216
IMPL_LINK_NOARG( SfxNewFileDialog, DoubleClick, const weld::TreeIter&, bool )
217
0
{
218
    // Still loading
219
0
    if (!m_xDocShell.Is() || !m_xDocShell->GetProgress())
220
0
        m_xDialog->response(RET_OK);
221
0
    return true;
222
0
}
223
224
sal_uInt16  SfxNewFileDialog::GetSelectedTemplatePos() const
225
0
{
226
0
    int nEntry = m_xTemplateLb->get_selected_index();
227
0
    if (nEntry == -1)
228
0
        return 0;
229
0
    OUString aSel = m_xRegionLb->get_selected_text();
230
0
    sal_Int32 nc = aSel.indexOf('(');
231
0
    if (nc != -1 && nc != 0)
232
0
        aSel = aSel.replaceAt(nc-1, 1, u"");
233
0
    if ( aSel.compareToIgnoreAsciiCase(SfxResId(STR_STANDARD)) != 0 )
234
0
        nEntry++;
235
0
    return nEntry;
236
0
}
237
238
SfxNewFileDialog::SfxNewFileDialog(weld::Window *pParent, SfxNewFileDialogMode nFlags)
239
0
    : SfxDialogController(pParent, u"sfx/ui/loadtemplatedialog.ui"_ustr, u"LoadTemplateDialog"_ustr)
240
0
    , m_aPrevIdle("SfxNewFileDialog m_aPrevIdle")
241
0
    , m_nFlags(nFlags)
242
0
    , m_xPreviewController(new SfxPreviewWin_Impl)
243
0
    , m_xRegionLb(m_xBuilder->weld_tree_view(u"categories"_ustr))
244
0
    , m_xTemplateLb(m_xBuilder->weld_tree_view(u"templates"_ustr))
245
0
    , m_xTextStyleCB(m_xBuilder->weld_check_button(u"text"_ustr))
246
0
    , m_xFrameStyleCB(m_xBuilder->weld_check_button(u"frame"_ustr))
247
0
    , m_xPageStyleCB(m_xBuilder->weld_check_button(u"pages"_ustr))
248
0
    , m_xNumStyleCB(m_xBuilder->weld_check_button(u"numbering"_ustr))
249
0
    , m_xMergeStyleCB(m_xBuilder->weld_check_button(u"overwrite"_ustr))
250
0
    , m_xLoadFilePB(m_xBuilder->weld_button(u"fromfile"_ustr))
251
0
    , m_xMoreBt(m_xBuilder->weld_expander(u"expander"_ustr))
252
0
    , m_xPreviewWin(new weld::CustomWeld(*m_xBuilder, u"image"_ustr, *m_xPreviewController))
253
0
    , m_xAltTitleFt(m_xBuilder->weld_label(u"alttitle"_ustr))
254
0
{
255
0
    const int nWidth = m_xRegionLb->get_approximate_digit_width() * 32;
256
0
    const int nHeight = m_xRegionLb->get_height_rows(8);
257
0
    m_xRegionLb->set_size_request(nWidth, nHeight);
258
0
    m_xTemplateLb->set_size_request(nWidth, nHeight);
259
0
    m_xPreviewWin->set_size_request(nWidth, nWidth);
260
261
0
    if (nFlags == SfxNewFileDialogMode::NONE)
262
0
        m_xMoreBt->hide();
263
0
    else if(SfxNewFileDialogMode::LoadTemplate == nFlags)
264
0
    {
265
0
        m_xLoadFilePB->show();
266
0
        m_xTextStyleCB->show();
267
0
        m_xFrameStyleCB->show();
268
0
        m_xPageStyleCB->show();
269
0
        m_xNumStyleCB->show();
270
0
        m_xMergeStyleCB->show();
271
0
        m_xMoreBt->hide();
272
0
        m_xTextStyleCB->set_active(true);
273
0
        m_xDialog->set_title(m_xAltTitleFt->get_label());
274
0
    }
275
0
    else
276
0
    {
277
0
        m_xMoreBt->connect_expanded(LINK(this, SfxNewFileDialog, Expand));
278
0
        m_xPreviewWin->show();
279
0
    }
280
281
0
    OUString sExtraData;
282
0
    SvtViewOptions aDlgOpt(EViewType::Dialog, m_xDialog->get_help_id());
283
0
    if (aDlgOpt.Exists())
284
0
    {
285
0
        css::uno::Any aUserItem = aDlgOpt.GetUserItem(u"UserItem"_ustr);
286
0
        aUserItem >>= sExtraData;
287
0
    }
288
289
0
    bool bExpand = !sExtraData.isEmpty() && sExtraData[0] == 'Y';
290
0
    m_xMoreBt->set_expanded(bExpand && (nFlags != SfxNewFileDialogMode::NONE));
291
292
0
    m_xTemplateLb->connect_selection_changed(LINK(this, SfxNewFileDialog, TemplateSelect));
293
0
    m_xTemplateLb->connect_item_activated(LINK(this, SfxNewFileDialog, DoubleClick));
294
295
    // update the template configuration if necessary
296
0
    {
297
0
        weld::WaitObject aWaitCursor(m_xDialog.get());
298
0
        m_aTemplates.Update();
299
0
    }
300
    // fill the list boxes
301
0
    const sal_uInt16 nCount = m_aTemplates.GetRegionCount();
302
0
    if (nCount)
303
0
    {
304
0
        for(sal_uInt16 i = 0; i < nCount; ++i)
305
0
            m_xRegionLb->append_text(m_aTemplates.GetFullRegionName(i));
306
0
        m_xRegionLb->connect_selection_changed(LINK(this, SfxNewFileDialog, RegionSelect));
307
0
    }
308
309
0
    m_aPrevIdle.SetPriority( TaskPriority::LOWEST );
310
0
    m_aPrevIdle.SetInvokeHandler( LINK( this, SfxNewFileDialog, Update));
311
312
0
    m_xRegionLb->select(0);
313
0
    RegionSelect(*m_xRegionLb);
314
0
}
315
316
SfxNewFileDialog::~SfxNewFileDialog()
317
0
{
318
0
    SvtViewOptions aDlgOpt(EViewType::Dialog, m_xDialog->get_help_id());
319
0
    aDlgOpt.SetUserItem(u"UserItem"_ustr, css::uno::Any(m_xMoreBt->get_expanded() ? u"Y"_ustr : u"N"_ustr));
320
0
}
321
322
bool SfxNewFileDialog::IsTemplate() const
323
0
{
324
0
    return GetSelectedTemplatePos()!=0;
325
0
}
326
327
OUString SfxNewFileDialog::GetTemplateFileName() const
328
0
{
329
0
    if (!IsTemplate() || !m_aTemplates.GetRegionCount())
330
0
        return OUString();
331
0
    return m_aTemplates.GetPath(m_xRegionLb->get_selected_index(),
332
0
                              GetSelectedTemplatePos()-1);
333
0
}
334
335
SfxTemplateFlags SfxNewFileDialog::GetTemplateFlags()const
336
0
{
337
0
    SfxTemplateFlags nRet = m_xTextStyleCB->get_active() ? SfxTemplateFlags::LOAD_TEXT_STYLES : SfxTemplateFlags::NONE;
338
0
    if(m_xFrameStyleCB->get_active())
339
0
        nRet |= SfxTemplateFlags::LOAD_FRAME_STYLES;
340
0
    if(m_xPageStyleCB->get_active())
341
0
        nRet |= SfxTemplateFlags::LOAD_PAGE_STYLES;
342
0
    if(m_xNumStyleCB->get_active())
343
0
        nRet |= SfxTemplateFlags::LOAD_NUM_STYLES;
344
0
    if(m_xMergeStyleCB->get_active())
345
0
        nRet |= SfxTemplateFlags::MERGE_STYLES;
346
0
    return nRet;
347
0
}
348
349
void SfxNewFileDialog::SetTemplateFlags(SfxTemplateFlags nSet)
350
0
{
351
0
    m_xTextStyleCB->set_active(  bool(nSet & SfxTemplateFlags::LOAD_TEXT_STYLES ));
352
0
    m_xFrameStyleCB->set_active( bool(nSet & SfxTemplateFlags::LOAD_FRAME_STYLES));
353
0
    m_xPageStyleCB->set_active(  bool(nSet & SfxTemplateFlags::LOAD_PAGE_STYLES ));
354
0
    m_xNumStyleCB->set_active(   bool(nSet & SfxTemplateFlags::LOAD_NUM_STYLES  ));
355
0
    m_xMergeStyleCB->set_active( bool(nSet & SfxTemplateFlags::MERGE_STYLES     ));
356
0
}
357
358
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */