merge the formfield patch from ooo-build
[ooovba.git] / vcl / source / helper / smartid.cxx
blobad9e9e23bfdbf6986f5824ceb32783ce3fb49380
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: smartid.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_vcl.hxx"
33 #include <vcl/smartid.hxx>
35 struct ImplSmartIdData
37 String aUId;
38 ULONG nUId;
39 BOOL bHasStringId;
40 BOOL bHasNumericId;
44 ImplSmartIdData* SmartId::GetSmartIdData()
46 if ( !mpData )
48 mpData = new ImplSmartIdData;
49 // mpData->aUId = "";
50 mpData->nUId = 0;
51 mpData->bHasStringId = FALSE;
52 mpData->bHasNumericId = FALSE;
54 return mpData;
58 SmartId::SmartId( const String& rId )
59 : mpData( NULL )
61 GetSmartIdData()->aUId = rId;
62 GetSmartIdData()->bHasStringId = TRUE;
65 SmartId::SmartId( ULONG nId )
66 : mpData( NULL )
68 GetSmartIdData()->nUId = nId;
69 GetSmartIdData()->bHasNumericId = TRUE;
72 SmartId::SmartId( const String& rId, ULONG nId )
73 : mpData( NULL )
75 GetSmartIdData()->aUId = rId;
76 GetSmartIdData()->bHasStringId = TRUE;
77 GetSmartIdData()->nUId = nId;
78 GetSmartIdData()->bHasNumericId = TRUE;
81 SmartId::SmartId()
82 : mpData( NULL )
85 SmartId::SmartId( const SmartId& rId )
86 : mpData( NULL )
88 if ( rId.mpData )
90 GetSmartIdData();
91 mpData->aUId = rId.mpData->aUId;
92 mpData->bHasStringId = rId.mpData->bHasStringId;
93 mpData->nUId = rId.mpData->nUId;
94 mpData->bHasNumericId = rId.mpData->bHasNumericId;
98 SmartId& SmartId::operator = ( const SmartId& rId )
100 if ( rId.mpData )
101 GetSmartIdData();
102 else
104 delete mpData;
105 mpData = NULL;
107 if ( mpData && rId.mpData )
109 mpData->aUId = rId.mpData->aUId;
110 mpData->bHasStringId = rId.mpData->bHasStringId;
111 mpData->nUId = rId.mpData->nUId;
112 mpData->bHasNumericId = rId.mpData->bHasNumericId;
114 return *this;
117 SmartId::~SmartId()
119 if ( mpData )
120 delete mpData;
121 #ifdef DBG_UTIL
122 if ( mpData )
123 mpData = (ImplSmartIdData*)0xDeadBeef;
124 #endif
127 void SmartId::UpdateId( const SmartId& rId, SmartIdUpdateMode aMode )
129 // Check if ImplData is needed
130 if ( aMode != SMART_SET_SMART || ( rId.HasString() || rId.HasNumeric() ) )
131 GetSmartIdData();
133 if ( aMode == SMART_SET_STR || aMode == SMART_SET_ALL || ( aMode == SMART_SET_SMART && rId.HasString() ) )
135 GetSmartIdData()->aUId = rId.GetStr();
136 GetSmartIdData()->bHasStringId = rId.HasString();
138 if ( aMode == SMART_SET_NUM || aMode == SMART_SET_ALL || ( aMode == SMART_SET_SMART && rId.HasNumeric() ) )
140 GetSmartIdData()->nUId = rId.GetNum();
141 GetSmartIdData()->bHasNumericId = rId.HasNumeric();
144 // remove ImplData when no IDs are set. This is Important because Implementation of Equals() Matches and HasAny relies on it
145 if ( mpData && !mpData->bHasStringId && !mpData->bHasNumericId )
147 delete mpData;
148 mpData = NULL;
152 BOOL SmartId::HasNumeric() const
154 if ( !mpData )
155 return FALSE;
156 else
157 return mpData->bHasNumericId;
160 BOOL SmartId::HasString() const
162 if ( !mpData )
163 return FALSE;
164 else
165 return mpData->bHasStringId;
168 BOOL SmartId::HasAny() const
170 return mpData != NULL;
173 ULONG SmartId::GetNum() const
175 if ( !mpData )
176 return 0;
177 else
178 return mpData->nUId;
181 String SmartId::GetStr() const
183 if ( !mpData )
184 return String();
185 else
186 return mpData->aUId;
190 String SmartId::GetText() const // return String for UI usage
192 String aRes;
193 if ( HasNumeric() )
194 aRes = String::CreateFromInt64( GetNum() );
195 if ( HasString() )
197 if ( HasNumeric() )
198 aRes.AppendAscii( "/" );
199 aRes.Append( GetStr() );
201 return aRes;
204 BOOL SmartId::Matches( const String &rId )const
206 if ( HasString() )
207 return GetStr().EqualsIgnoreCaseAscii( rId );
208 else
209 return FALSE;
212 BOOL SmartId::Matches( const ULONG nId ) const
214 if ( HasNumeric() )
215 return GetNum() == nId;
216 else
217 return FALSE;
220 /******************************************************************************
221 If Both Ids have nither Strings nor Numbers they don't match
222 If both Ids have Strings the result of Matching these is returned.
223 Numbers are then Ignored.
224 Else Matching Numbers is attempted.
225 ******************************************************************************/
226 BOOL SmartId::Matches( const SmartId &rId ) const
228 if ( !mpData || !rId.mpData )
229 return FALSE;
230 else if ( HasString() && rId.HasString() )
231 return Matches( rId.GetStr() );
232 else
233 return rId.HasNumeric() && Matches( rId.GetNum() );
236 BOOL SmartId::Equals( const SmartId &rId ) const
238 if ( mpData && rId.mpData )
239 return mpData->aUId.EqualsIgnoreCaseAscii( rId.mpData->aUId )
240 && mpData->bHasStringId == rId.mpData->bHasStringId
241 && mpData->nUId == rId.mpData->nUId
242 && mpData->bHasNumericId == rId.mpData->bHasNumericId;
243 else if ( !mpData && !rId.mpData )
244 return TRUE;
245 else
246 return FALSE;
249 BOOL SmartId::operator == ( const SmartId& rRight ) const
251 return Equals( rRight );
254 BOOL SmartId::operator < ( const SmartId& rRight ) const
256 if ( HasString() && rRight.HasString() && GetStr() != rRight.GetStr() )
257 return GetStr() < rRight.GetStr();
258 else if ( HasNumeric() && rRight.HasNumeric() && GetNum() != rRight.GetNum() )
259 return GetNum() < rRight.GetNum();
260 else
261 { // Sort Strings to Front
262 if ( HasString() )
263 return rRight.HasString() && rRight.HasNumeric();
264 else
265 return rRight.HasString() || (!HasNumeric() && rRight.HasNumeric());