Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / ui / fldui / javaedit.cxx
blobb13c9828d8c50b630d99825ef1b0ee73db508b79
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
21 #include <svl/urihelper.hxx>
22 #include <view.hxx>
23 #include <sfx2/docfile.hxx>
24 #include <sfx2/filedlghelper.hxx>
25 #include <docsh.hxx>
26 #include <wrtsh.hxx>
27 #include <fldbas.hxx>
28 #include <fldmgr.hxx>
29 #include <docufld.hxx>
30 #include <javaedit.hxx>
32 #include <strings.hrc>
34 using namespace ::com::sun::star;
36 SwJavaEditDialog::SwJavaEditDialog(weld::Window* pParent, SwWrtShell* pWrtSh)
37 : GenericDialogController(pParent, "modules/swriter/ui/insertscript.ui", "InsertScriptDialog")
38 , m_bNew(true)
39 , m_bIsUrl(false)
40 , m_pSh(pWrtSh)
41 , m_xTypeED(m_xBuilder->weld_entry("scripttype"))
42 , m_xUrlRB(m_xBuilder->weld_radio_button("url"))
43 , m_xEditRB(m_xBuilder->weld_radio_button("text"))
44 , m_xUrlPB(m_xBuilder->weld_button("browse"))
45 , m_xUrlED(m_xBuilder->weld_entry("urlentry"))
46 , m_xEditED(m_xBuilder->weld_text_view("textentry"))
47 , m_xOKBtn(m_xBuilder->weld_button("ok"))
48 , m_xPrevBtn(m_xBuilder->weld_button("previous"))
49 , m_xNextBtn(m_xBuilder->weld_button("next"))
51 // install handler
52 m_xPrevBtn->connect_clicked( LINK( this, SwJavaEditDialog, PrevHdl ) );
53 m_xNextBtn->connect_clicked( LINK( this, SwJavaEditDialog, NextHdl ) );
54 m_xOKBtn->connect_clicked( LINK( this, SwJavaEditDialog, OKHdl ) );
56 Link<weld::Toggleable&,void> aLk = LINK(this, SwJavaEditDialog, RadioButtonHdl);
57 m_xUrlRB->connect_toggled(aLk);
58 m_xEditRB->connect_toggled(aLk);
59 m_xUrlPB->connect_clicked(LINK(this, SwJavaEditDialog, InsertFileHdl));
61 m_pMgr.reset(new SwFieldMgr(m_pSh));
62 m_pField = static_cast<SwScriptField*>(m_pMgr->GetCurField());
64 m_bNew = !(m_pField && m_pField->GetTyp()->Which() == SwFieldIds::Script);
66 CheckTravel();
68 if (!m_bNew)
69 m_xDialog->set_title(SwResId(STR_JAVA_EDIT));
71 UpdateFromRadioButtons();
74 SwJavaEditDialog::~SwJavaEditDialog()
76 m_pSh->EnterStdMode();
77 m_pMgr.reset();
78 m_pFileDlg.reset();
81 IMPL_LINK_NOARG(SwJavaEditDialog, PrevHdl, weld::Button&, void)
83 m_pSh->EnterStdMode();
85 SetField();
86 m_pMgr->GoPrev();
87 m_pField = static_cast<SwScriptField*>(m_pMgr->GetCurField());
88 CheckTravel();
89 UpdateFromRadioButtons();
92 IMPL_LINK_NOARG(SwJavaEditDialog, NextHdl, weld::Button&, void)
94 m_pSh->EnterStdMode();
96 SetField();
97 m_pMgr->GoNext();
98 m_pField = static_cast<SwScriptField*>(m_pMgr->GetCurField());
99 CheckTravel();
100 UpdateFromRadioButtons();
103 IMPL_LINK_NOARG(SwJavaEditDialog, OKHdl, weld::Button&, void)
105 SetField();
106 m_xDialog->response(RET_OK);
109 void SwJavaEditDialog::CheckTravel()
111 bool bTravel = false;
112 bool bNext(false), bPrev(false);
114 if (!m_bNew)
116 // Traveling only when more than one field
117 m_pSh->StartAction();
118 m_pSh->CreateCursor();
120 bNext = m_pMgr->GoNext();
121 if( bNext )
122 m_pMgr->GoPrev();
124 bPrev = m_pMgr->GoPrev();
125 if( bPrev )
126 m_pMgr->GoNext();
127 bTravel |= bNext || bPrev;
129 m_pSh->DestroyCursor();
130 m_pSh->EndAction();
132 if (m_pField->IsCodeURL())
134 OUString sURL(m_pField->GetPar2());
135 if(!sURL.isEmpty())
137 INetURLObject aINetURL(sURL);
138 if(INetProtocol::File == aINetURL.GetProtocol())
139 sURL = aINetURL.PathToFileName();
141 m_xUrlED->set_text(sURL);
142 m_xEditED->set_text(OUString());
143 m_xUrlRB->set_active(true);
145 else
147 m_xEditED->set_text(m_pField->GetPar2());
148 m_xUrlED->set_text(OUString());
149 m_xEditRB->set_active(true);
151 m_xTypeED->set_text(m_pField->GetPar1());
154 if ( !bTravel )
156 m_xPrevBtn->hide();
157 m_xNextBtn->hide();
159 else
161 m_xPrevBtn->set_sensitive(bPrev);
162 m_xNextBtn->set_sensitive(bNext);
166 void SwJavaEditDialog::SetField()
168 if( !m_xOKBtn->get_sensitive() )
169 return ;
171 m_aType = m_xTypeED->get_text();
172 m_bIsUrl = m_xUrlRB->get_active();
174 if (m_bIsUrl)
176 m_aText = m_xUrlED->get_text();
177 if (!m_aText.isEmpty())
179 SfxMedium* pMedium = m_pSh->GetView().GetDocShell()->GetMedium();
180 INetURLObject aAbs;
181 if( pMedium )
182 aAbs = pMedium->GetURLObject();
184 m_aText = URIHelper::SmartRel2Abs(
185 aAbs, m_aText, URIHelper::GetMaybeFileHdl());
188 else
189 m_aText = m_xEditED->get_text();
191 if (m_aType.isEmpty())
192 m_aType = "JavaScript";
195 bool SwJavaEditDialog::IsUpdate() const
197 return m_pField && ( sal_uInt32(m_bIsUrl ? 1 : 0) != m_pField->GetFormat() || m_pField->GetPar2() != m_aType || m_pField->GetPar1() != m_aText );
200 IMPL_LINK(SwJavaEditDialog, RadioButtonHdl, weld::Toggleable&, rButton, void)
202 if (!rButton.get_active())
203 return;
204 UpdateFromRadioButtons();
207 void SwJavaEditDialog::UpdateFromRadioButtons()
209 bool bEnable = m_xUrlRB->get_active();
210 m_xUrlPB->set_sensitive(bEnable);
211 m_xUrlED->set_sensitive(bEnable);
212 m_xEditED->set_sensitive(!bEnable);
214 if (!m_bNew)
216 bEnable = !m_pSh->IsReadOnlyAvailable() || !m_pSh->HasReadonlySel();
217 m_xOKBtn->set_sensitive(bEnable);
218 m_xUrlED->set_editable(bEnable);
219 m_xEditED->set_editable(bEnable);
220 m_xTypeED->set_editable(bEnable);
221 if( m_xUrlPB->get_sensitive() && !bEnable )
222 m_xUrlPB->set_sensitive( false );
226 IMPL_LINK_NOARG( SwJavaEditDialog, InsertFileHdl, weld::Button&, void )
228 if (!m_pFileDlg)
230 m_pFileDlg.reset(new ::sfx2::FileDialogHelper(
231 ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE,
232 FileDialogFlags::Insert, "swriter", SfxFilterFlags::NONE, SfxFilterFlags::NONE, m_xDialog.get()));
234 m_pFileDlg->SetContext(sfx2::FileDialogHelper::WriterInsertScript);
235 m_pFileDlg->StartExecuteModal( LINK( this, SwJavaEditDialog, DlgClosedHdl ) );
238 IMPL_LINK_NOARG(SwJavaEditDialog, DlgClosedHdl, sfx2::FileDialogHelper *, void)
240 if (m_pFileDlg->GetError() == ERRCODE_NONE)
242 OUString sFileName = m_pFileDlg->GetPath();
243 if ( !sFileName.isEmpty() )
245 INetURLObject aINetURL( sFileName );
246 if ( INetProtocol::File == aINetURL.GetProtocol() )
247 sFileName = aINetURL.PathToFileName();
249 m_xUrlED->set_text(sFileName);
253 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */