LibreOffice Module sw (master)  1
javaedit.cxx
Go to the documentation of this file.
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 <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>
31 
32 #include <strings.hrc>
33 
34 using namespace ::com::sun::star;
35 
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"))
50 {
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 ) );
55 
56  Link<weld::Button&,void> aLk = LINK(this, SwJavaEditDialog, RadioButtonHdl);
57  m_xUrlRB->connect_clicked(aLk);
58  m_xEditRB->connect_clicked(aLk);
59  m_xUrlPB->connect_clicked(LINK(this, SwJavaEditDialog, InsertFileHdl));
60 
61  m_pMgr.reset(new SwFieldMgr(m_pSh));
62  m_pField = static_cast<SwScriptField*>(m_pMgr->GetCurField());
63 
65 
66  CheckTravel();
67 
68  if (!m_bNew)
69  m_xDialog->set_title(SwResId(STR_JAVA_EDIT));
70 
71  RadioButtonHdl(*m_xUrlRB);
72 }
73 
75 {
77  m_pMgr.reset();
78  m_pFileDlg.reset();
79 }
80 
82 {
83  m_pSh->EnterStdMode();
84 
85  SetField();
86  m_pMgr->GoPrev();
87  m_pField = static_cast<SwScriptField*>(m_pMgr->GetCurField());
88  CheckTravel();
89  RadioButtonHdl(*m_xUrlRB);
90 }
91 
93 {
94  m_pSh->EnterStdMode();
95 
96  SetField();
97  m_pMgr->GoNext();
98  m_pField = static_cast<SwScriptField*>(m_pMgr->GetCurField());
99  CheckTravel();
100  RadioButtonHdl(*m_xUrlRB);
101 }
102 
104 {
105  SetField();
106  m_xDialog->response(RET_OK);
107 }
108 
110 {
111  bool bTravel = false;
112  bool bNext(false), bPrev(false);
113 
114  if (!m_bNew)
115  {
116  // Traveling only when more than one field
117  m_pSh->StartAction();
118  m_pSh->CreateCursor();
119 
120  bNext = m_pMgr->GoNext();
121  if( bNext )
122  m_pMgr->GoPrev();
123 
124  bPrev = m_pMgr->GoPrev();
125  if( bPrev )
126  m_pMgr->GoNext();
127  bTravel |= bNext || bPrev;
128 
129  m_pSh->DestroyCursor();
130  m_pSh->EndAction();
131 
132  if (m_pField->IsCodeURL())
133  {
134  OUString sURL(m_pField->GetPar2());
135  if(!sURL.isEmpty())
136  {
137  INetURLObject aINetURL(sURL);
138  if(INetProtocol::File == aINetURL.GetProtocol())
139  sURL = aINetURL.PathToFileName();
140  }
141  m_xUrlED->set_text(sURL);
142  m_xEditED->set_text(OUString());
143  m_xUrlRB->set_active(true);
144  }
145  else
146  {
147  m_xEditED->set_text(m_pField->GetPar2());
148  m_xUrlED->set_text(OUString());
149  m_xEditRB->set_active(true);
150  }
151  m_xTypeED->set_text(m_pField->GetPar1());
152  }
153 
154  if ( !bTravel )
155  {
156  m_xPrevBtn->hide();
157  m_xNextBtn->hide();
158  }
159  else
160  {
161  m_xPrevBtn->set_sensitive(bPrev);
162  m_xNextBtn->set_sensitive(bNext);
163  }
164 }
165 
167 {
168  if( !m_xOKBtn->get_sensitive() )
169  return ;
170 
171  m_aType = m_xTypeED->get_text();
172  m_bIsUrl = m_xUrlRB->get_active();
173 
174  if (m_bIsUrl)
175  {
176  m_aText = m_xUrlED->get_text();
177  if (!m_aText.isEmpty())
178  {
179  SfxMedium* pMedium = m_pSh->GetView().GetDocShell()->GetMedium();
180  INetURLObject aAbs;
181  if( pMedium )
182  aAbs = pMedium->GetURLObject();
183 
186  }
187  }
188  else
189  m_aText = m_xEditED->get_text();
190 
191  if (m_aType.isEmpty())
192  m_aType = "JavaScript";
193 }
194 
196 {
197  return m_pField && ( sal_uInt32(m_bIsUrl ? 1 : 0) != m_pField->GetFormat() || m_pField->GetPar2() != m_aType || m_pField->GetPar1() != m_aText );
198 }
199 
201 {
202  bool bEnable = m_xUrlRB->get_active();
203  m_xUrlPB->set_sensitive(bEnable);
204  m_xUrlED->set_sensitive(bEnable);
205  m_xEditED->set_sensitive(!bEnable);
206 
207  if (!m_bNew)
208  {
209  bEnable = !m_pSh->IsReadOnlyAvailable() || !m_pSh->HasReadonlySel();
210  m_xOKBtn->set_sensitive(bEnable);
211  m_xUrlED->set_editable(bEnable);
212  m_xEditED->set_editable(bEnable);
213  m_xTypeED->set_editable(bEnable);
214  if( m_xUrlPB->get_sensitive() && !bEnable )
215  m_xUrlPB->set_sensitive( false );
216  }
217 }
218 
220 {
221  if (!m_pFileDlg)
222  {
223  m_pFileDlg.reset(new ::sfx2::FileDialogHelper(
224  ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE,
225  FileDialogFlags::Insert, "swriter", SfxFilterFlags::NONE, SfxFilterFlags::NONE, m_xDialog.get()));
226  }
227 
228  m_pFileDlg->StartExecuteModal( LINK( this, SwJavaEditDialog, DlgClosedHdl ) );
229 }
230 
232 {
233  if (m_pFileDlg->GetError() == ERRCODE_NONE)
234  {
235  OUString sFileName = m_pFileDlg->GetPath();
236  if ( !sFileName.isEmpty() )
237  {
238  INetURLObject aINetURL( sFileName );
239  if ( INetProtocol::File == aINetURL.GetProtocol() )
240  sFileName = aINetURL.PathToFileName();
241  }
242  m_xUrlED->set_text(sFileName);
243  }
244 }
245 
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::shared_ptr< weld::Dialog > m_xDialog
std::unique_ptr< weld::Button > m_xNextBtn
Definition: javaedit.hxx:52
std::unique_ptr< SwFieldMgr > m_pMgr
Definition: javaedit.hxx:40
bool IsUpdate() const
Definition: javaedit.cxx:195
std::unique_ptr< weld::RadioButton > m_xUrlRB
Definition: javaedit.hxx:45
sal_uInt32 GetFormat() const
Query parameters for dialog and for BASIC.
Definition: fldbas.hxx:401
std::unique_ptr< weld::Entry > m_xUrlED
Definition: javaedit.hxx:48
bool IsCodeURL() const
Definition: docufld.hxx:712
std::unique_ptr< weld::Button > m_xUrlPB
Definition: javaedit.hxx:47
void EnterStdMode()
Definition: select.cxx:550
void DestroyCursor()
transform TableCursor to normal cursor, nullify Tablemode
Definition: crsrsh.cxx:146
virtual OUString GetPar1() const override
Type.
Definition: scrptfld.cxx:65
OUString PathToFileName() const
Used by the UI to modify the document model.
Definition: wrtsh.hxx:90
std::unique_ptr< sfx2::FileDialogHelper > m_pFileDlg
Definition: javaedit.hxx:42
std::unique_ptr< weld::Button > m_xPrevBtn
Definition: javaedit.hxx:51
const SwView & GetView() const
Definition: wrtsh.hxx:428
std::unique_ptr< weld::TextView > m_xEditED
Definition: javaedit.hxx:49
void CheckTravel()
Definition: javaedit.cxx:109
SwPaM * CreateCursor()
delete the current cursor and make the following into the current
Definition: crsrsh.cxx:122
void EndAction(const bool bIdleEnd=false, const bool DoSetPosX=false)
Definition: crsrsh.cxx:239
SVL_DLLPUBLIC Link< OUString *, bool > const & GetMaybeFileHdl()
SwJavaEditDialog(weld::Window *pParent, SwWrtShell *pWrtSh)
Definition: javaedit.cxx:36
virtual OUString GetPar2() const override
Text.
Definition: scrptfld.cxx:76
std::unique_ptr< weld::Entry > m_xTypeED
Definition: javaedit.hxx:44
OUString SwResId(const char *pId)
Definition: swmodule.cxx:178
SwDocShell * GetDocShell()
Definition: view.cxx:1107
SwFieldType * GetTyp() const
Definition: fldbas.hxx:396
void StartAction()
Definition: crsrsh.cxx:222
OUString m_aText
Definition: javaedit.hxx:33
#define ERRCODE_NONE
INetProtocol GetProtocol() const
std::unique_ptr< weld::RadioButton > m_xEditRB
Definition: javaedit.hxx:46
RET_OK
Reference< XExecutableDialog > m_xDialog
const INetURLObject & GetURLObject() const
virtual ~SwJavaEditDialog() override
Definition: javaedit.cxx:74
SwScriptField * m_pField
Definition: javaedit.hxx:39
SwWrtShell * m_pSh
Definition: javaedit.hxx:41
IMPL_LINK_NOARG(SwJavaEditDialog, PrevHdl, weld::Button &, void)
Definition: javaedit.cxx:81
OUString m_aType
Definition: javaedit.hxx:34
SwFieldIds Which() const
Definition: fldbas.hxx:272
std::unique_ptr< weld::Button > m_xOKBtn
Definition: javaedit.hxx:50
SVL_DLLPUBLIC OUString SmartRel2Abs(INetURLObject const &rTheBaseURIRef, OUString const &rTheRelURIRef, Link< OUString *, bool > const &rMaybeFileHdl=Link< OUString *, bool >(), bool bCheckFileExists=true, bool bIgnoreFragment=false, INetURLObject::EncodeMechanism eEncodeMechanism=INetURLObject::EncodeMechanism::WasEncoded, INetURLObject::DecodeMechanism eDecodeMechanism=INetURLObject::DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8, FSysStyle eStyle=FSysStyle::Detect)
SfxMedium * GetMedium() const