summaryrefslogtreecommitdiff
path: root/patches/vba/sc-source-ui-vba-vbaworkbook-cxx.diff
blob: b22beea0e811e9a213e8a4d84b8fd87150d7eb86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
--- /dev/null	
+++ sc/source/ui/vba/vbaworkbook.cxx	
@@ -0,0 +1,228 @@
+#include <tools/urlobj.hxx>
+
+#include <com/sun/star/util/XModifiable.hpp>
+#include <com/sun/star/util/XProtectable.hpp>
+#include <com/sun/star/sheet/XSpreadsheetView.hpp>
+#include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
+#include <com/sun/star/frame/XStorable.hpp>
+#include <com/sun/star/frame/XFrame.hpp>
+#include <com/sun/star/beans/XPropertySet.hpp>
+
+#include "vbaworksheet.hxx"
+#include "vbaworksheets.hxx"
+#include "vbaworkbook.hxx"
+#include "vbawindows.hxx"
+#include "vbahelper.hxx"
+
+#include <stdio.h>
+
+// Much of the impl. for the equivalend UNO module is
+// sc/source/ui/unoobj/docuno.cxx, viewuno.cxx
+
+using namespace ::org::openoffice;
+using namespace ::com::sun::star;
+
+class ActiveSheet : public ScVbaWorksheet
+{
+protected:
+	virtual uno::Reference< frame::XModel > getModel()
+	{ 	
+		return getCurrentDocument(); 
+	}
+	virtual uno::Reference< sheet::XSpreadsheet > getSheet()
+	{ 
+		uno::Reference< frame::XModel > xModel = getModel();
+		uno::Reference< sheet::XSpreadsheet > xSheet;
+		if ( xModel.is() )
+		{
+			uno::Reference< sheet::XSpreadsheetView > xSpreadsheet(
+                        	xModel->getCurrentController(), uno::UNO_QUERY );
+			if ( xSpreadsheet.is() )
+				xSheet = xSpreadsheet->getActiveSheet(); 
+		}
+		return xSheet;
+	}
+public:
+	ActiveSheet( uno::Reference< uno::XComponentContext >& xContext ) : ScVbaWorksheet( xContext ) {}
+		
+};
+
+
+::rtl::OUString
+ScVbaWorkbook::getName() throw (uno::RuntimeException)
+{
+	rtl::OUString sName = getModel()->getURL();
+	if ( sName.getLength() )
+	{
+
+		INetURLObject aURL( getModel()->getURL() );
+		sName = aURL.GetLastName();
+	}
+	else
+	{
+		const static rtl::OUString sTitle( RTL_CONSTASCII_USTRINGPARAM("Title" ) );
+		// process "UntitledX - $(PRODUCTNAME)"
+		uno::Reference< frame::XFrame > xFrame( getModel()->getCurrentController()->getFrame(), uno::UNO_QUERY_THROW );
+		uno::Reference< beans::XPropertySet > xProps( xFrame, uno::UNO_QUERY_THROW );
+		xProps->getPropertyValue(sTitle ) >>= sName;
+		sal_Int32 pos = 0;
+		sName = sName.getToken(0,' ',pos);	
+	}
+	return sName;
+}
+::rtl::OUString
+ScVbaWorkbook::getPath() throw (uno::RuntimeException)
+{
+	INetURLObject aURL( getModel()->getURL() );
+	aURL.CutLastName();
+	return aURL.GetURLPath();
+}
+
+::rtl::OUString
+ScVbaWorkbook::getFullName() throw (uno::RuntimeException)
+{
+	INetURLObject aURL( getModel()->getURL() );
+	return aURL.GetURLPath();
+}
+uno::Reference< vba::XWorksheet >
+ScVbaWorkbook::getActiveSheet() throw (uno::RuntimeException)
+{
+	return new ActiveSheet( m_xContext );
+}
+uno::Any SAL_CALL
+ScVbaWorkbook::Worksheets( const uno::Any& aIndex ) throw (uno::RuntimeException)
+{
+	uno::Reference< frame::XModel > xModel( getModel() );	
+	uno::Reference <sheet::XSpreadsheetDocument> xSpreadDoc( xModel, uno::UNO_QUERY_THROW );
+	uno::Reference<sheet::XSpreadsheets > xSheets( xSpreadDoc->getSheets(), uno::UNO_QUERY_THROW );
+	uno::Reference< vba::XCollection > xWorkSheets( uno::Reference< vba::XWorksheets >( new ScVbaWorksheets(m_xContext, xSheets, xModel ) ), uno::UNO_QUERY_THROW );
+	if (  aIndex.getValueTypeClass() == uno::TypeClass_VOID )
+	{
+		return uno::Any( xWorkSheets );	
+	}
+	return uno::Any( xWorkSheets->Item( aIndex ) );
+}
+uno::Any SAL_CALL
+ScVbaWorkbook::Windows( const uno::Any& aIndex ) throw (uno::RuntimeException)
+{
+	uno::Reference< vba::XCollection >  xWindows = ScVbaWindows::Windows( m_xContext );
+	if ( aIndex.getValueTypeClass() == uno::TypeClass_VOID )
+		return uno::Any( xWindows );
+	return uno::Any( xWindows->Item( aIndex ) );
+}
+void
+ScVbaWorkbook::Close( const uno::Any &rSaveArg, const uno::Any &rFileArg,
+					  const uno::Any &rRouteArg ) throw (uno::RuntimeException)
+{
+	sal_Bool bSaveChanges = sal_False;
+	rtl::OUString aFileName;
+	sal_Bool bRouteWorkbook = sal_True;
+
+	rSaveArg >>= bSaveChanges;
+	sal_Bool bFileName =  ( rFileArg >>= aFileName );
+	rRouteArg >>= bRouteWorkbook;
+	uno::Reference< frame::XStorable > xStorable( getModel(), uno::UNO_QUERY_THROW );
+	uno::Reference< util::XModifiable > xModifiable( getModel(), uno::UNO_QUERY_THROW );
+
+	if( bSaveChanges )
+	{
+		if( xStorable->isReadonly() )
+		{	
+			throw uno::RuntimeException(::rtl::OUString( 
+				RTL_CONSTASCII_USTRINGPARAM( "Unable to save to a read only file ") ),
+                        	uno::Reference< XInterface >() );
+		}
+		if( bFileName )
+			xStorable->storeAsURL( aFileName, uno::Sequence< beans::PropertyValue >(0) ); 
+		else
+			xStorable->store();
+	}	
+	else
+		xModifiable->setModified( false );		
+
+	uno::Reference< util::XCloseable > xCloseable( getModel(), uno::UNO_QUERY );
+
+	if( xCloseable.is() )
+		// use close(boolean DeliverOwnership)
+	
+		// The boolean parameter DeliverOwnership tells objects vetoing the close process that they may
+		// assume ownership if they object the closure by throwing a CloseVetoException
+		// Here we give up ownership. To be on the safe side, catch possible veto exception anyway.
+		xCloseable->close(sal_True);
+	// If close is not supported by this model - try to dispose it.
+	// But if the model disagree with a reset request for the modify state
+	// we shouldn't do so. Otherwhise some strange things can happen.
+	else
+	{
+		uno::Reference< lang::XComponent > xDisposable ( getCurrentDocument(), uno::UNO_QUERY );
+		if ( xDisposable.is() )
+			xDisposable->dispose();
+	}
+}
+
+void
+ScVbaWorkbook::Protect( const uno::Any &aPassword ) throw (uno::RuntimeException)
+{
+	rtl::OUString rPassword;
+	uno::Reference< util::XProtectable > xProt( getModel(), uno::UNO_QUERY_THROW );
+	SC_VBA_FIXME(("Workbook::Protect stub"));
+	if(  aPassword >>= rPassword )
+		xProt->protect( rPassword );
+	else
+		xProt->protect( rtl::OUString() );
+}
+
+void 
+ScVbaWorkbook::Unprotect( const uno::Any &aPassword ) throw (uno::RuntimeException)
+{
+	rtl::OUString rPassword;
+	uno::Reference< util::XProtectable > xProt( getModel(), uno::UNO_QUERY_THROW );
+	if( !getProtectStructure() )
+		throw uno::RuntimeException(::rtl::OUString(
+			RTL_CONSTASCII_USTRINGPARAM( "File is already unprotected" ) ),
+			uno::Reference< XInterface >() );
+	else
+	{
+		if( aPassword >>= rPassword )
+			xProt->unprotect( rPassword );
+		else
+			xProt->unprotect( rtl::OUString() );
+	}
+}
+		
+::sal_Bool
+ScVbaWorkbook::getProtectStructure() throw (uno::RuntimeException)
+{
+	uno::Reference< util::XProtectable > xProt( getModel(), uno::UNO_QUERY_THROW );
+	return xProt->isProtected();
+}
+
+void 
+ScVbaWorkbook::setSaved( sal_Bool bSave ) throw (uno::RuntimeException)
+{
+	uno::Reference< util::XModifiable > xModifiable( getModel(), uno::UNO_QUERY_THROW );
+	xModifiable->setModified( bSave );
+}
+
+sal_Bool
+ScVbaWorkbook::getSaved() throw (uno::RuntimeException)
+{
+	uno::Reference< util::XModifiable > xModifiable( getModel(), uno::UNO_QUERY_THROW );
+	return xModifiable->isModified();
+}
+
+void
+ScVbaWorkbook::Save() throw (uno::RuntimeException)
+{
+	rtl::OUString url = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(".uno:Save"));
+	uno::Reference< frame::XModel > xModel = getModel();
+	dispatchRequests(xModel,url);
+}
+
+void 
+ScVbaWorkbook::Activate() throw (uno::RuntimeException)
+{
+	uno::Reference< frame::XFrame > xFrame( getModel()->getCurrentController()->getFrame(), uno::UNO_QUERY_THROW );
+	xFrame->activate();
+}	
+