xref: /aoo41x/main/sc/source/core/data/sheetevents.cxx (revision b3f79822)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 
27 // INCLUDE ---------------------------------------------------------------
28 
29 #include "sheetevents.hxx"
30 #include <com/sun/star/script/vba/VBAEventId.hpp>
31 #include <tools/debug.hxx>
32 
33 // -----------------------------------------------------------------------
34 
35 // static
GetEventName(sal_Int32 nEvent)36 rtl::OUString ScSheetEvents::GetEventName(sal_Int32 nEvent)
37 {
38     if (nEvent<0 || nEvent>=SC_SHEETEVENT_COUNT)
39     {
40         DBG_ERRORFILE("invalid event number");
41         return rtl::OUString();
42     }
43 
44     static const sal_Char* aEventNames[] =
45 	{
46         "OnFocus",                  // SC_SHEETEVENT_FOCUS
47         "OnUnfocus",                // SC_SHEETEVENT_UNFOCUS
48         "OnSelect",                 // SC_SHEETEVENT_SELECT
49         "OnDoubleClick",            // SC_SHEETEVENT_DOUBLECLICK
50         "OnRightClick",             // SC_SHEETEVENT_RIGHTCLICK
51         "OnChange",                 // SC_SHEETEVENT_CHANGE
52         "OnCalculate"               // SC_SHEETEVENT_CALCULATE
53 	};
54     return rtl::OUString::createFromAscii(aEventNames[nEvent]);
55 }
56 
57 // static
GetVbaSheetEventId(sal_Int32 nEvent)58 sal_Int32 ScSheetEvents::GetVbaSheetEventId(sal_Int32 nEvent)
59 {
60     using namespace ::com::sun::star::script::vba::VBAEventId;
61     if (nEvent<0 || nEvent>=SC_SHEETEVENT_COUNT)
62     {
63         DBG_ERRORFILE("invalid event number");
64         return NO_EVENT;
65     }
66 
67     static const sal_Int32 nVbaEventIds[] =
68 	{
69         WORKSHEET_ACTIVATE,             // SC_SHEETEVENT_FOCUS
70         WORKSHEET_DEACTIVATE,           // SC_SHEETEVENT_UNFOCUS
71         WORKSHEET_SELECTIONCHANGE,      // SC_SHEETEVENT_SELECT
72         WORKSHEET_BEFOREDOUBLECLICK,    // SC_SHEETEVENT_DOUBLECLICK
73         WORKSHEET_BEFORERIGHTCLICK,     // SC_SHEETEVENT_RIGHTCLICK
74         WORKSHEET_CHANGE,               // SC_SHEETEVENT_CHANGE
75         WORKSHEET_CALCULATE             // SC_SHEETEVENT_CALCULATE
76 	};
77     return nVbaEventIds[nEvent];
78 }
79 
80 // static
GetVbaDocumentEventId(sal_Int32 nEvent)81 sal_Int32 ScSheetEvents::GetVbaDocumentEventId(sal_Int32 nEvent)
82 {
83     using namespace ::com::sun::star::script::vba::VBAEventId;
84     sal_Int32 nSheetEventId = GetVbaSheetEventId(nEvent);
85     return (nSheetEventId != NO_EVENT) ? (nSheetEventId + USERDEFINED_START) : NO_EVENT;
86 }
87 
88 // -----------------------------------------------------------------------
89 
ScSheetEvents()90 ScSheetEvents::ScSheetEvents() :
91     mpScriptNames(NULL)
92 {
93 }
94 
~ScSheetEvents()95 ScSheetEvents::~ScSheetEvents()
96 {
97     Clear();
98 }
99 
Clear()100 void ScSheetEvents::Clear()
101 {
102     if (mpScriptNames)
103     {
104         for (sal_Int32 nEvent=0; nEvent<SC_SHEETEVENT_COUNT; ++nEvent)
105             delete mpScriptNames[nEvent];
106         delete[] mpScriptNames;
107         mpScriptNames = NULL;
108     }
109 }
110 
ScSheetEvents(const ScSheetEvents & rOther)111 ScSheetEvents::ScSheetEvents(const ScSheetEvents& rOther) :
112     mpScriptNames(NULL)
113 {
114     *this = rOther;
115 }
116 
operator =(const ScSheetEvents & rOther)117 const ScSheetEvents& ScSheetEvents::operator=(const ScSheetEvents& rOther)
118 {
119     Clear();
120     if (rOther.mpScriptNames)
121     {
122         mpScriptNames = new rtl::OUString*[SC_SHEETEVENT_COUNT];
123         for (sal_Int32 nEvent=0; nEvent<SC_SHEETEVENT_COUNT; ++nEvent)
124             if (rOther.mpScriptNames[nEvent])
125                 mpScriptNames[nEvent] = new rtl::OUString(*rOther.mpScriptNames[nEvent]);
126             else
127                 mpScriptNames[nEvent] = NULL;
128     }
129     return *this;
130 }
131 
GetScript(sal_Int32 nEvent) const132 const rtl::OUString* ScSheetEvents::GetScript(sal_Int32 nEvent) const
133 {
134     if (nEvent<0 || nEvent>=SC_SHEETEVENT_COUNT)
135     {
136         DBG_ERRORFILE("invalid event number");
137         return NULL;
138     }
139 
140     if (mpScriptNames)
141         return mpScriptNames[nEvent];
142     return NULL;
143 }
144 
SetScript(sal_Int32 nEvent,const rtl::OUString * pNew)145 void ScSheetEvents::SetScript(sal_Int32 nEvent, const rtl::OUString* pNew)
146 {
147     if (nEvent<0 || nEvent>=SC_SHEETEVENT_COUNT)
148     {
149         DBG_ERRORFILE("invalid event number");
150         return;
151     }
152 
153     if (!mpScriptNames)
154     {
155         mpScriptNames = new rtl::OUString*[SC_SHEETEVENT_COUNT];
156         for (sal_Int32 nEventIdx=0; nEventIdx<SC_SHEETEVENT_COUNT; ++nEventIdx)
157             mpScriptNames[nEventIdx] = NULL;
158     }
159     delete mpScriptNames[nEvent];
160     if (pNew)
161         mpScriptNames[nEvent] = new rtl::OUString(*pNew);
162     else
163         mpScriptNames[nEvent] = NULL;
164 }
165 
166