summaryrefslogtreecommitdiff
path: root/sw/source/ui/utlui/swrenamexnameddlg.cxx
blob: 582c3b8bb69823515880286d5a429a9f1d092598 (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
/* -*- 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 <swtypes.hxx>
#include <globals.hrc>

#include <utlui.hrc>
#include <../../uibase/utlui/unotools.hrc>
#include <unoprnms.hxx>
#include <osl/diagnose.h>
#include <vcl/msgbox.hxx>
#include <com/sun/star/text/XTextViewCursorSupplier.hpp>
#include <com/sun/star/view/XScreenCursor.hpp>
#include <com/sun/star/view/DocumentZoomType.hpp>
#include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
#include <com/sun/star/style/XStyle.hpp>
#include <com/sun/star/frame/XFrame.hpp>
#include <com/sun/star/text/XText.hpp>
#include <com/sun/star/text/XTextDocument.hpp>
#include <com/sun/star/awt/PosSize.hpp>
#include <com/sun/star/view/XViewSettingsSupplier.hpp>
#include <com/sun/star/container/XNameContainer.hpp>
#include <comphelper/processfactory.hxx>
#include <comphelper/string.hxx>
#include <sfx2/dispatch.hxx>
#include <svl/stritem.hxx>
#include <shellio.hxx>
#include <docsh.hxx>
#include <view.hxx>
#include <wrtsh.hxx>
#include <swmodule.hxx>
#include <unocrsr.hxx>

#include "swrenamexnameddlg.hxx"

using namespace ::com::sun::star;

SwRenameXNamedDlg::SwRenameXNamedDlg( Window* pWin,
            uno::Reference< container::XNamed > & xN,
            uno::Reference< container::XNameAccess > & xNA )
    : ModalDialog(pWin, "RenameObjectDialog",
        "modules/swriter/ui/renameobjectdialog.ui")
    , m_sRemoveWarning(SW_RESSTR(STR_REMOVE_WARNING))
    , xNamed(xN)
    , xNameAccess(xNA)
{
    get(m_pNewNameED, "entry");
    get(m_pOk, "ok");

    OUString sTmp(GetText());
    m_pNewNameED->SetText(xNamed->getName());
    m_pNewNameED->SetSelection(Selection(SELECTION_MIN, SELECTION_MAX));
    sTmp += xNamed->getName();
    SetText(sTmp);

    m_pOk->SetClickHdl(LINK(this, SwRenameXNamedDlg, OkHdl));
    m_pNewNameED->SetModifyHdl(LINK(this, SwRenameXNamedDlg, ModifyHdl));
    m_pOk->Enable(false);
}

IMPL_LINK_NOARG(SwRenameXNamedDlg, OkHdl)
{
    try
    {
        xNamed->setName(m_pNewNameED->GetText());
    }
    catch (const uno::RuntimeException&)
    {
        OSL_FAIL("name wasn't changed");
    }
    EndDialog(RET_OK);
    return 0;
}

IMPL_LINK(SwRenameXNamedDlg, ModifyHdl, NoSpaceEdit*, pEdit)
{
    OUString sTmp(pEdit->GetText());

    // prevent from pasting illegal characters
    const sal_Int32 nLen = sTmp.getLength();
    OUString sMsg;
    for(sal_Int32 i = 0; i < pEdit->GetForbiddenChars().getLength(); ++i)
    {
        const sal_Int32 nTmpLen = sTmp.getLength();
        sTmp = comphelper::string::remove(sTmp, pEdit->GetForbiddenChars()[i]);
        if(sTmp.getLength() != nTmpLen)
            sMsg += OUString(pEdit->GetForbiddenChars()[i]);
    }
    if(sTmp.getLength() != nLen)
    {
        pEdit->SetText(sTmp);
        InfoBox(this, m_sRemoveWarning + sMsg).Execute();
    }

    m_pOk->Enable(!sTmp.isEmpty()
                  && !xNameAccess->hasByName(sTmp)
                  && (!xSecondAccess.is() || !xSecondAccess->hasByName(sTmp))
                  && (!xThirdAccess.is() || !xThirdAccess->hasByName(sTmp))
                 );
    return 0;
}

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