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 <tools/lineend.hxx>
21 #include <unotools/charclass.hxx>
22 #include <editeng/unolingu.hxx>
23 #include <wrtsh.hxx>
24 #include <fldbas.hxx>
25 #include <expfld.hxx>
26 #include <usrfld.hxx>
27 #include <inpdlg.hxx>
28 #include <fldmgr.hxx>
29 
30 // edit field-insert
SwFieldInputDlg(weld::Widget * pParent,SwWrtShell & rS,SwField * pField,bool bPrevButton,bool bNextButton)31 SwFieldInputDlg::SwFieldInputDlg(weld::Widget *pParent, SwWrtShell &rS,
32                                  SwField* pField, bool bPrevButton, bool bNextButton)
33     : GenericDialogController(pParent, "modules/swriter/ui/inputfielddialog.ui", "InputFieldDialog")
34     , rSh( rS )
35     , pInpField(nullptr)
36     , pSetField(nullptr)
37     , pUsrType(nullptr)
38     , m_pPressedButton(nullptr)
39     , m_xLabelED(m_xBuilder->weld_entry("name"))
40     , m_xEditED(m_xBuilder->weld_text_view("text"))
41     , m_xPrevBT(m_xBuilder->weld_button("prev"))
42     , m_xNextBT(m_xBuilder->weld_button("next"))
43     , m_xOKBT(m_xBuilder->weld_button("ok"))
44 {
45     m_xEditED->set_size_request(-1, m_xEditED->get_height_rows(8));
46 
47     if( bPrevButton || bNextButton )
48     {
49         m_xPrevBT->show();
50         m_xPrevBT->connect_clicked(LINK(this, SwFieldInputDlg, PrevHdl));
51         m_xPrevBT->set_sensitive(bPrevButton);
52 
53         m_xNextBT->show();
54         m_xNextBT->connect_clicked(LINK(this, SwFieldInputDlg, NextHdl));
55         m_xNextBT->set_sensitive(bNextButton);
56     }
57 
58     // evaluation here
59     OUString aStr;
60     if( SwFieldIds::Input == pField->GetTyp()->Which() )
61     {   // it is an input field
62 
63         pInpField = static_cast<SwInputField*>(pField);
64         m_xLabelED->set_text(pInpField->GetPar2());
65         sal_uInt16 nSubType = pInpField->GetSubType();
66 
67         switch(nSubType & 0xff)
68         {
69             case INP_TXT:
70                 aStr = pInpField->GetPar1();
71                 break;
72 
73             case INP_USR:
74                 // user field
75                 if( nullptr != ( pUsrType = static_cast<SwUserFieldType*>(rSh.GetFieldType(
76                             SwFieldIds::User, pInpField->GetPar1() ) )  ) )
77                     aStr = pUsrType->GetContent();
78                 break;
79         }
80     }
81     else
82     {
83         // it is a SetExpression
84         pSetField = static_cast<SwSetExpField*>(pField);
85         OUString sFormula(pSetField->GetFormula());
86         //values are formatted - formulas are not
87         CharClass aCC( LanguageTag( pSetField->GetLanguage() ));
88         if( aCC.isNumeric( sFormula ))
89         {
90             aStr = pSetField->ExpandField(true, rS.GetLayout());
91         }
92         else
93             aStr = sFormula;
94         m_xLabelED->set_text(pSetField->GetPromptText());
95     }
96 
97     // JP 31.3.00: Inputfields in readonly regions must be allowed to
98     //              input any content. - 74639
99     bool bEnable = !rSh.IsCursorReadonly();
100 
101     m_xOKBT->set_sensitive( bEnable );
102     m_xEditED->set_editable( bEnable );
103 
104     if( !aStr.isEmpty() )
105         m_xEditED->set_text(convertLineEnd(aStr, GetSystemLineEnd()));
106     m_xEditED->grab_focus();
107 
108     // preselect all text to allow quickly changing the content
109     if (bEnable)
110         m_xEditED->select_region(0, -1);
111 }
112 
~SwFieldInputDlg()113 SwFieldInputDlg::~SwFieldInputDlg()
114 {
115 }
116 
117 // Close
Apply()118 void SwFieldInputDlg::Apply()
119 {
120     OUString aTmp = m_xEditED->get_text().replaceAll("\r", "");
121     rSh.StartAllAction();
122     bool bModified = false;
123     if(pInpField)
124     {
125         if(pUsrType)
126         {
127             if( aTmp != pUsrType->GetContent() )
128             {
129                 pUsrType->SetContent(aTmp);
130                 pUsrType->UpdateFields();
131                 bModified = true;
132             }
133         }
134         else if( aTmp != pInpField->GetPar1() )
135         {
136             pInpField->SetPar1(aTmp);
137             rSh.SwEditShell::UpdateOneField(*pInpField);
138             bModified = true;
139         }
140     }
141     else if( aTmp != pSetField->GetPar2())
142     {
143         pSetField->SetPar2(aTmp);
144         rSh.SwEditShell::UpdateOneField(*pSetField);
145         bModified = true;
146     }
147 
148     if( bModified )
149         rSh.SetUndoNoResetModified();
150 
151     rSh.EndAllAction();
152 }
153 
PrevButtonPressed() const154 bool SwFieldInputDlg::PrevButtonPressed() const
155 {
156     return m_pPressedButton == m_xPrevBT.get();
157 }
158 
NextButtonPressed() const159 bool SwFieldInputDlg::NextButtonPressed() const
160 {
161     return m_pPressedButton == m_xNextBT.get();
162 }
163 
IMPL_LINK_NOARG(SwFieldInputDlg,PrevHdl,weld::Button &,void)164 IMPL_LINK_NOARG(SwFieldInputDlg, PrevHdl, weld::Button&, void)
165 {
166     m_pPressedButton = m_xPrevBT.get();
167     m_xDialog->response(RET_OK);
168 }
169 
IMPL_LINK_NOARG(SwFieldInputDlg,NextHdl,weld::Button &,void)170 IMPL_LINK_NOARG(SwFieldInputDlg, NextHdl, weld::Button&, void)
171 {
172     m_pPressedButton = m_xNextBT.get();
173     m_xDialog->response(RET_OK);
174 }
175 
176 
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
178