xref: /trunk/main/sc/source/ui/miscdlgs/simpref.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sc.hxx"
30 
31 // System - Includes ---------------------------------------------------------
32 
33 
34 
35 // INCLUDE -------------------------------------------------------------------
36 
37 #include <vcl/msgbox.hxx>
38 #include <sfx2/app.hxx>
39 
40 #include "reffact.hxx"
41 #include "document.hxx"
42 #include "scresid.hxx"
43 #include "globstr.hrc"
44 #include "simpref.hrc"
45 #include "rangenam.hxx"     // IsNameValid
46 #include "simpref.hxx"
47 #include "scmod.hxx"
48 
49 //============================================================================
50 
51 #define ABS_SREF          SCA_VALID \
52                         | SCA_COL_ABSOLUTE | SCA_ROW_ABSOLUTE | SCA_TAB_ABSOLUTE
53 #define ABS_DREF          ABS_SREF \
54                         | SCA_COL2_ABSOLUTE | SCA_ROW2_ABSOLUTE | SCA_TAB2_ABSOLUTE
55 #define ABS_SREF3D      ABS_SREF | SCA_TAB_3D
56 #define ABS_DREF3D      ABS_DREF | SCA_TAB_3D
57 
58 //----------------------------------------------------------------------------
59 
60 #define ERRORBOX(s) ErrorBox(this,WinBits(WB_OK|WB_DEF_OK),s).Execute()
61 #define QUERYBOX(m) QueryBox(this,WinBits(WB_YES_NO|WB_DEF_YES),m).Execute()
62 
63 //============================================================================
64 //  class ScSimpleRefDlg
65 
66 //----------------------------------------------------------------------------
67 ScSimpleRefDlg::ScSimpleRefDlg( SfxBindings* pB, SfxChildWindow* pCW, Window* pParent,
68                           ScViewData*   ptrViewData )
69 
70     :   ScAnyRefDlg ( pB, pCW, pParent, RID_SCDLG_SIMPLEREF ),
71         //
72         aFtAssign       ( this, ScResId( FT_ASSIGN ) ),
73         aEdAssign       ( this, this, ScResId( ED_ASSIGN ) ),
74         aRbAssign       ( this, ScResId( RB_ASSIGN ), &aEdAssign, this ),
75 
76         aBtnOk          ( this, ScResId( BTN_OK ) ),
77         aBtnCancel      ( this, ScResId( BTN_CANCEL ) ),
78         aBtnHelp        ( this, ScResId( BTN_HELP ) ),
79 
80         //
81         pViewData       ( ptrViewData ),
82         pDoc            ( ptrViewData->GetDocument() ),
83         bRefInputMode   ( sal_False ),
84         bAutoReOpen     ( sal_True ),
85         bCloseOnButtonUp( sal_False ),
86         bSingleCell     ( sal_False ),
87         bMultiSelection ( sal_False )
88 {
89     //  damit die Strings in der Resource bei den FixedTexten bleiben koennen:
90     Init();
91     FreeResource();
92     SetDispatcherLock( sal_True ); // Modal-Modus einschalten
93 }
94 
95 //----------------------------------------------------------------------------
96 __EXPORT ScSimpleRefDlg::~ScSimpleRefDlg()
97 {
98     SetDispatcherLock( sal_False ); // Modal-Modus einschalten
99 }
100 
101 //----------------------------------------------------------------------------
102 void ScSimpleRefDlg::FillInfo(SfxChildWinInfo& rWinInfo) const
103 {
104     ScAnyRefDlg::FillInfo(rWinInfo);
105     rWinInfo.bVisible=bAutoReOpen;
106 }
107 
108 //----------------------------------------------------------------------------
109 void ScSimpleRefDlg::SetRefString(const String &rStr)
110 {
111     aEdAssign.SetText(rStr);
112 }
113 
114 //----------------------------------------------------------------------------
115 void ScSimpleRefDlg::Init()
116 {
117     aBtnOk.SetClickHdl      ( LINK( this, ScSimpleRefDlg, OkBtnHdl ) );
118     aBtnCancel.SetClickHdl  ( LINK( this, ScSimpleRefDlg, CancelBtnHdl ) );
119     bCloseFlag=sal_False;
120 }
121 
122 //----------------------------------------------------------------------------
123 // Uebergabe eines mit der Maus selektierten Tabellenbereiches, der dann als
124 //  neue Selektion im Referenz-Fenster angezeigt wird.
125 void ScSimpleRefDlg::SetReference( const ScRange& rRef, ScDocument* pDocP )
126 {
127     if ( aEdAssign.IsEnabled() )
128     {
129         if ( rRef.aStart != rRef.aEnd )
130             RefInputStart( &aEdAssign );
131 
132         theCurArea = rRef;
133         String aRefStr;
134         if ( bSingleCell )
135         {
136             ScAddress aAdr = rRef.aStart;
137             aAdr.Format( aRefStr, SCA_ABS_3D, pDocP, pDocP->GetAddressConvention() );
138         }
139         else
140             theCurArea.Format( aRefStr, ABS_DREF3D, pDocP, pDocP->GetAddressConvention() );
141 
142         if ( bMultiSelection )
143         {
144             String aVal = aEdAssign.GetText();
145             Selection aSel = aEdAssign.GetSelection();
146             aSel.Justify();
147             aVal.Erase( (xub_StrLen)aSel.Min(), (xub_StrLen)aSel.Len() );
148             aVal.Insert( aRefStr, (xub_StrLen)aSel.Min() );
149             Selection aNewSel( aSel.Min(), aSel.Min()+aRefStr.Len() );
150             aEdAssign.SetRefString( aVal );
151             aEdAssign.SetSelection( aNewSel );
152         }
153         else
154             aEdAssign.SetRefString( aRefStr );
155 
156         aChangeHdl.Call( &aRefStr );
157     }
158 }
159 
160 
161 //----------------------------------------------------------------------------
162 sal_Bool __EXPORT ScSimpleRefDlg::Close()
163 {
164     CancelBtnHdl(&aBtnCancel);
165     return sal_True;
166 }
167 
168 //------------------------------------------------------------------------
169 void ScSimpleRefDlg::SetActive()
170 {
171     aEdAssign.GrabFocus();
172 
173     //  kein NameModifyHdl, weil sonst Bereiche nicht geaendert werden koennen
174     //  (nach dem Aufziehen der Referenz wuerde der alte Inhalt wieder angezeigt)
175     //  (der ausgewaehlte DB-Name hat sich auch nicht veraendert)
176 
177     RefInputDone();
178 }
179 //------------------------------------------------------------------------
180 sal_Bool ScSimpleRefDlg::IsRefInputMode() const
181 {
182     return sal_True;
183 }
184 
185 String ScSimpleRefDlg::GetRefString() const
186 {
187     return aEdAssign.GetText();
188 }
189 
190 void ScSimpleRefDlg::SetCloseHdl( const Link& rLink )
191 {
192     aCloseHdl=rLink;
193 }
194 
195 void ScSimpleRefDlg::SetUnoLinks( const Link& rDone, const Link& rAbort,
196                                     const Link& rChange )
197 {
198     aDoneHdl    = rDone;
199     aAbortedHdl = rAbort;
200     aChangeHdl  = rChange;
201 }
202 
203 void ScSimpleRefDlg::SetFlags( sal_Bool bSetCloseOnButtonUp, sal_Bool bSetSingleCell, sal_Bool bSetMultiSelection )
204 {
205     bCloseOnButtonUp = bSetCloseOnButtonUp;
206     bSingleCell = bSetSingleCell;
207     bMultiSelection = bSetMultiSelection;
208 }
209 
210 void ScSimpleRefDlg::StartRefInput()
211 {
212     if ( bMultiSelection )
213     {
214         // initially select the whole string, so it gets replaced by default
215         aEdAssign.SetSelection( Selection( 0, aEdAssign.GetText().Len() ) );
216     }
217 
218     aRbAssign.DoRef();
219     bCloseFlag=sal_True;
220 }
221 
222 void ScSimpleRefDlg::RefInputDone( sal_Bool bForced)
223 {
224     ScAnyRefDlg::RefInputDone(bForced);
225     if ( (bForced || bCloseOnButtonUp) && bCloseFlag )
226         OkBtnHdl(&aBtnOk);
227 }
228 //------------------------------------------------------------------------
229 // Handler:
230 // ========
231 IMPL_LINK( ScSimpleRefDlg, OkBtnHdl, void *, EMPTYARG )
232 {
233     bAutoReOpen=sal_False;
234     String aResult=aEdAssign.GetText();
235     aCloseHdl.Call(&aResult);
236     Link aUnoLink = aDoneHdl;       // stack var because this is deleted in DoClose
237     DoClose( ScSimpleRefDlgWrapper::GetChildWindowId() );
238     aUnoLink.Call( &aResult );
239     return 0;
240 }
241 
242 //------------------------------------------------------------------------
243 IMPL_LINK( ScSimpleRefDlg, CancelBtnHdl, void *, EMPTYARG )
244 {
245     bAutoReOpen=sal_False;
246     String aResult=aEdAssign.GetText();
247     aCloseHdl.Call(NULL);
248     Link aUnoLink = aAbortedHdl;    // stack var because this is deleted in DoClose
249     DoClose( ScSimpleRefDlgWrapper::GetChildWindowId() );
250     aUnoLink.Call( &aResult );
251     return 0;
252 }
253 
254 
255 
256 //------------------------------------------------------------------------
257 
258