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 #undef SC_DLLIMPLEMENTATION
21 
22 #include <comphelper/string.hxx>
23 #include <svx/colorbox.hxx>
24 #include <unotools/useroptions.hxx>
25 #include <vcl/svapp.hxx>
26 #include <vcl/weld.hxx>
27 #include <unotools/localedatawrapper.hxx>
28 
29 #include <global.hxx>
30 #include <globstr.hrc>
31 #include <scresid.hxx>
32 #include <tabvwsh.hxx>
33 #include <viewdata.hxx>
34 #include <document.hxx>
35 #include <scendlg.hxx>
36 
ScNewScenarioDlg(weld::Window * pParent,const OUString & rName,bool bEdit,bool bSheetProtected)37 ScNewScenarioDlg::ScNewScenarioDlg(weld::Window* pParent, const OUString& rName, bool bEdit, bool bSheetProtected)
38     : GenericDialogController(pParent, "modules/scalc/ui/scenariodialog.ui", "ScenarioDialog")
39     , aDefScenarioName(rName)
40     , bIsEdit(bEdit)
41     , m_xEdName(m_xBuilder->weld_entry("name"))
42     , m_xEdComment(m_xBuilder->weld_text_view("comment"))
43     , m_xCbShowFrame(m_xBuilder->weld_check_button("showframe"))
44     , m_xLbColor(new ColorListBox(m_xBuilder->weld_menu_button("bordercolor"), pParent))
45     , m_xCbTwoWay(m_xBuilder->weld_check_button("copyback"))
46     , m_xCbCopyAll(m_xBuilder->weld_check_button("copysheet"))
47     , m_xCbProtect(m_xBuilder->weld_check_button("preventchanges"))
48     , m_xBtnOk(m_xBuilder->weld_button("ok"))
49     , m_xAltTitle(m_xBuilder->weld_label("alttitle"))
50     , m_xCreatedFt(m_xBuilder->weld_label("createdft"))
51     , m_xOnFt(m_xBuilder->weld_label("onft"))
52 {
53     m_xEdComment->set_size_request(m_xEdComment->get_approximate_digit_width() * 60,
54                                    m_xEdComment->get_height_rows(6));
55 
56     if (bIsEdit)
57         m_xDialog->set_title(m_xAltTitle->get_label());
58 
59     SvtUserOptions aUserOpt;
60 
61     OUString sCreatedBy(m_xCreatedFt->get_label());
62     OUString sOn(m_xOnFt->get_label());
63 
64     OUString aComment(sCreatedBy + " " + aUserOpt.GetFirstName() + " " +aUserOpt.GetLastName()
65               + ", " + sOn + " " + ScGlobal::GetpLocaleData()->getDate(Date(Date::SYSTEM))
66               + ", " + ScGlobal::GetpLocaleData()->getTime(tools::Time(tools::Time::SYSTEM)));
67 
68     m_xEdComment->set_text(aComment);
69     m_xEdName->set_text(rName);
70     m_xBtnOk->connect_clicked(LINK(this, ScNewScenarioDlg, OkHdl));
71     m_xCbShowFrame->connect_toggled(LINK(this, ScNewScenarioDlg, EnableHdl));
72 
73     m_xLbColor->SelectEntry( COL_LIGHTGRAY );
74     m_xCbShowFrame->set_active(true);
75     m_xCbTwoWay->set_active(true);
76     m_xCbCopyAll->set_active(false);
77     m_xCbProtect->set_active(true);
78 
79     if (bIsEdit)
80         m_xCbCopyAll->set_active(false);
81     // If the Sheet is protected then we disable the Scenario Protect input
82     // and default it to true above. Note we are in 'Add' mode here as: if
83     // Sheet && scenario protection are true, then we cannot edit this dialog.
84     if (bSheetProtected)
85         m_xCbProtect->set_active(false);
86 }
87 
~ScNewScenarioDlg()88 ScNewScenarioDlg::~ScNewScenarioDlg()
89 {
90 }
91 
GetScenarioData(OUString & rName,OUString & rComment,Color & rColor,ScScenarioFlags & rFlags) const92 void ScNewScenarioDlg::GetScenarioData( OUString& rName, OUString& rComment,
93                                         Color& rColor, ScScenarioFlags& rFlags ) const
94 {
95     rComment = m_xEdComment->get_text();
96     rName    = m_xEdName->get_text();
97 
98     if (rName.isEmpty())
99         rName = aDefScenarioName;
100 
101     rColor = m_xLbColor->GetSelectEntryColor();
102     ScScenarioFlags nBits = ScScenarioFlags::NONE;
103     if (m_xCbShowFrame->get_active())
104         nBits |= ScScenarioFlags::ShowFrame;
105     if (m_xCbTwoWay->get_active())
106         nBits |= ScScenarioFlags::TwoWay;
107     if (m_xCbCopyAll->get_active())
108         nBits |= ScScenarioFlags::CopyAll;
109     if (m_xCbProtect->get_active())
110         nBits |= ScScenarioFlags::Protected;
111     rFlags = nBits;
112 }
113 
SetScenarioData(const OUString & rName,const OUString & rComment,const Color & rColor,ScScenarioFlags nFlags)114 void ScNewScenarioDlg::SetScenarioData(const OUString& rName, const OUString& rComment,
115                                         const Color& rColor, ScScenarioFlags nFlags)
116 {
117     m_xEdComment->set_text(rComment);
118     m_xEdName->set_text(rName);
119     m_xLbColor->SelectEntry(rColor);
120 
121     m_xCbShowFrame->set_active( (nFlags & ScScenarioFlags::ShowFrame) != ScScenarioFlags::NONE );
122     EnableHdl(*m_xCbShowFrame);
123     m_xCbTwoWay->set_active( (nFlags & ScScenarioFlags::TwoWay)    != ScScenarioFlags::NONE );
124     //  not CopyAll
125     m_xCbProtect->set_active( (nFlags & ScScenarioFlags::Protected) != ScScenarioFlags::NONE );
126 }
127 
IMPL_LINK_NOARG(ScNewScenarioDlg,OkHdl,weld::Button &,void)128 IMPL_LINK_NOARG(ScNewScenarioDlg, OkHdl, weld::Button&, void)
129 {
130     OUString      aName = comphelper::string::strip(m_xEdName->get_text(), ' ');
131     ScDocument* pDoc    = static_cast<ScTabViewShell*>(SfxViewShell::Current())->GetViewData().GetDocument();
132 
133     m_xEdName->set_text(aName);
134 
135     if ( !ScDocument::ValidTabName( aName ) )
136     {
137         std::unique_ptr<weld::MessageDialog> xInfoBox(Application::CreateMessageDialog(m_xDialog.get(),
138                                                       VclMessageType::Info, VclButtonsType::Ok,
139                                                       ScResId(STR_INVALIDTABNAME)));
140         xInfoBox->run();
141         m_xEdName->grab_focus();
142     }
143     else if ( !bIsEdit && !pDoc->ValidNewTabName( aName ) )
144     {
145         std::unique_ptr<weld::MessageDialog> xInfoBox(Application::CreateMessageDialog(m_xDialog.get(),
146                                                       VclMessageType::Info, VclButtonsType::Ok,
147                                                       ScResId(STR_NEWTABNAMENOTUNIQUE)));
148         xInfoBox->run();
149         m_xEdName->grab_focus();
150     }
151     else
152         m_xDialog->response(RET_OK);
153 
154     //! when editing, test whether another table has the name!
155 }
156 
IMPL_LINK(ScNewScenarioDlg,EnableHdl,weld::ToggleButton &,rBox,void)157 IMPL_LINK(ScNewScenarioDlg, EnableHdl, weld::ToggleButton&, rBox, void)
158 {
159     if (&rBox == m_xCbShowFrame.get())
160         m_xLbColor->set_sensitive(m_xCbShowFrame->get_active());
161 }
162 
163 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
164