xref: /aoo42x/main/starmath/inc/utility.hxx (revision f6a9d5ca)
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 #ifndef UTILITY_HXX
24 #define UTILITY_HXX
25 
26 #include <sfx2/minarray.hxx>
27 #ifndef _FONT_HXX //autogen
28 #include <vcl/font.hxx>
29 #endif
30 #include <vcl/fixed.hxx>
31 #include <vcl/combobox.hxx>
32 #include <vcl/lstbox.hxx>
33 #include <tools/fract.hxx>
34 
35 
36 class SmRect;
37 class String;
38 
39 #define C2S(cChar) String::CreateFromAscii(RTL_CONSTASCII_STRINGPARAM(cChar))
40 
41 
42 /////////////////////////////////////////////////////////////////
43 
SmPtsTo100th_mm(long nNumPts)44 inline long SmPtsTo100th_mm(long nNumPts)
45 	// returns the length (in 100th of mm) that corresponds to the length
46 	// 'nNumPts' (in units points).
47 	// 72.27 [pt] = 1 [inch] = 2,54 [cm] = 2540 [100th of mm].
48 	// result is being rounded to the nearest integer.
49 {
50 	DBG_ASSERT(nNumPts >= 0, "Sm : Ooops...");
51 	// broken into multiple and fraction of 'nNumPts' to reduce chance
52 	// of overflow
53 	// (7227 / 2) is added in order to round to the nearest integer
54 	return 35 * nNumPts + (nNumPts * 1055L + (7227 / 2)) / 7227L;
55 }
56 
57 
SmPtsTo100th_mm(const Fraction & rNumPts)58 inline long SmPtsTo100th_mm(const Fraction &rNumPts)
59 	// as above but with argument 'rNumPts' as 'Fraction'
60 {
61 	Fraction  aTmp (254000L, 7227L);
62 	return aTmp *= rNumPts;
63 }
64 
65 
Sm100th_mmToPts(long nNum100th_mm)66 inline Fraction Sm100th_mmToPts(long nNum100th_mm)
67 	// returns the length (in points) that corresponds to the length
68 	// 'nNum100th_mm' (in 100th of mm).
69 {
70 	DBG_ASSERT(nNum100th_mm >= 0, "Sm : Ooops...");
71 	Fraction  aTmp (7227L, 254000L);
72 	return aTmp *= Fraction(nNum100th_mm);
73 }
74 
75 
SmRoundFraction(const Fraction & rFrac)76 inline long SmRoundFraction(const Fraction &rFrac)
77 {
78 	DBG_ASSERT(rFrac > Fraction(), "Sm : Ooops...");
79 	return (rFrac.GetNumerator() + rFrac.GetDenominator() / 2) / rFrac.GetDenominator();
80 }
81 
82 
83 class SmViewShell;
84 SmViewShell * SmGetActiveView();
85 
86 
87 ////////////////////////////////////////////////////////////
88 //
89 // SmFace
90 //
91 
92 sal_Bool    IsItalic( const Font &rFont );
93 sal_Bool    IsBold( const Font &rFont );
94 
95 class SmFace : public Font
96 {
97 	long	nBorderWidth;
98 
99     void    Impl_Init();
100 
101 public:
SmFace()102 	SmFace() :
103         Font(), nBorderWidth(-1) { Impl_Init(); }
SmFace(const Font & rFont)104 	SmFace(const Font& rFont) :
105         Font(rFont), nBorderWidth(-1) { Impl_Init(); }
SmFace(const String & rName,const Size & rSize)106 	SmFace(const String& rName, const Size& rSize) :
107         Font(rName, rSize), nBorderWidth(-1) { Impl_Init(); }
SmFace(FontFamily eFamily,const Size & rSize)108 	SmFace( FontFamily eFamily, const Size& rSize) :
109         Font(eFamily, rSize), nBorderWidth(-1) { Impl_Init(); }
110 
SmFace(const SmFace & rFace)111 	SmFace(const SmFace &rFace) :
112         Font(rFace), nBorderWidth(-1) { Impl_Init(); }
113 
114 	// overloaded version in order to supply a min value
115 	// for font size (height). (Also used in ctor's to do so.)
116 	void 	SetSize(const Size& rSize);
117 
SetBorderWidth(long nWidth)118 	void	SetBorderWidth(long nWidth)		{ nBorderWidth = nWidth; }
119 	long	GetBorderWidth() const;
GetDefaultBorderWidth() const120 	long	GetDefaultBorderWidth() const	{ return GetSize().Height() / 20 ; }
FreezeBorderWidth()121 	void	FreezeBorderWidth()		{ nBorderWidth = GetDefaultBorderWidth(); }
122 
123 	SmFace & operator = (const SmFace &rFace);
124 };
125 
126 SmFace & operator *= (SmFace &rFace, const Fraction &rFrac);
127 
128 
129 #ifdef NEVER
130 ////////////////////////////////////////////////////////////
131 //
132 // SmInfoText
133 //
134 
135 class SmInfoText : public FixedText
136 {
137 protected:
138 	sal_uInt16	nMaxLen;
139 	String	aText;
140 
141 public:
142 	SmInfoText(Window* pParent, WinBits nWinStyle = 0, sal_uInt16 nMax = 128);
143 	SmInfoText(Window* pParent, const ResId& rResId, sal_uInt16 nMax = 128);
144 
145 	void	SetText(const String& rStr);
146 
GetText() const147 	XubString GetText() const { return (aText); }
148 };
149 #endif
150 
151 
152 ////////////////////////////////////////////////////////////
153 //
154 // SmPickList
155 //
156 
157 class SmPickList : public SfxPtrArr
158 {
159 protected:
160 	sal_uInt16	nSize;
161 
162 	virtual void   *CreateItem(const String& rString) = 0;
163 	virtual void   *CreateItem(const void *pItem) = 0;
164 	virtual void	DestroyItem(void *pItem) = 0;
165 
166 	virtual sal_Bool	CompareItem(const void *pFirstItem, const void *pSecondItem) const = 0;
167 
168 	virtual String	GetStringItem(void *pItem) = 0;
169 
GetPtr(sal_uInt16 nPos) const170 	void	   *GetPtr(sal_uInt16 nPos) const { return SfxPtrArr::GetObject(nPos); }
GetPtr(sal_uInt16 nPos)171 	void	  *&GetPtr(sal_uInt16 nPos) { return SfxPtrArr::GetObject(nPos); }
InsertPtr(sal_uInt16 nPos,void * pItem)172 	void		InsertPtr(sal_uInt16 nPos, void *pItem) { SfxPtrArr::Insert(nPos, pItem); }
RemovePtr(sal_uInt16 nPos,sal_uInt16 nCount=1)173 	void		RemovePtr(sal_uInt16 nPos, sal_uInt16 nCount = 1) { SfxPtrArr::Remove(nPos, nCount); }
174 
175 public:
176 	SmPickList(sal_uInt16 nInitSize = 0, sal_uInt16 nMaxSize = 5);
177     virtual ~SmPickList();
178 
179 	SmPickList&   operator = (const SmPickList& rList);
180 
Get(sal_uInt16 nPos=0) const181 	void	   *Get(sal_uInt16 nPos = 0) const { return GetPtr(nPos); }
182     using   SfxPtrArr::Insert;
183 	void		Insert(const void* pItem);
184 	void		Update(const void* pItem, const void *pNewItem);
185     using   SfxPtrArr::Remove;
186 	void		Remove(const void* pItem);
187 
188     using   SfxPtrArr::operator [];
operator [](sal_uInt16 nPos) const189 	void	   *operator [] (sal_uInt16 nPos) const { return GetPtr(nPos); }
190 
GetSize() const191 	sal_uInt16		GetSize() const { return nSize; }
Count() const192 	sal_uInt16		Count() const { return SfxPtrArr::Count(); }
193 
194 	void		Clear();
195 };
196 
197 
198 ////////////////////////////////////////////////////////////
199 //
200 // SmStringPickList
201 //
202 #ifdef NEVER
203 class SmStringPickList : public SmPickList
204 {
205 protected:
206 	virtual void   *CreateItem(const String& rString);
207 	virtual void   *CreateItem(const void *pItem);
208 	virtual void	DestroyItem(void *pItem);
209 
210 	virtual sal_Bool	CompareItem(const void *pFirstItem, const void *pSecondItem) const;
211 
212 	virtual String	GetStringItem(void *pItem);
213 
214 public:
SmStringPickList()215 	SmStringPickList()
216 		: SmPickList(0, 5) {}
SmStringPickList(sal_uInt16 nInitSize,sal_uInt16 nMaxSize)217 	SmStringPickList(sal_uInt16 nInitSize, sal_uInt16 nMaxSize)
218 		: SmPickList(nInitSize, nMaxSize) {}
SmStringPickList(const SmPickList & rOrig)219 	SmStringPickList(const SmPickList& rOrig )
220 		: SmPickList(rOrig) {}
~SmStringPickList()221     virtual ~SmStringPickList() { Clear(); }
222 
223 	virtual void	Insert(const String &rString);
224 	virtual void	Update(const String &rString, const String &rNewString);
225 	virtual void	Remove(const String &rString);
226 
227 	inline sal_Bool		Contains(const String &rString) const;
228 	inline String	Get(sal_uInt16 nPos = 0) const;
229 
230 	inline SmStringPickList& operator = (const SmStringPickList& rList);
231 	inline String			 operator [] (sal_uInt16 nPos) const;
232 };
233 
operator =(const SmStringPickList & rList)234 inline SmStringPickList& SmStringPickList::operator = (const SmStringPickList& rList)
235 {
236 	*(SmPickList *)this = *(SmPickList *)&rList; return *this;
237 }
238 
operator [](sal_uInt16 nPos) const239 inline String SmStringPickList::operator [] (sal_uInt16 nPos) const
240 {
241 	return *((String *)SmPickList::operator[](nPos));
242 }
243 
Get(sal_uInt16 nPos) const244 inline String SmStringPickList::Get(sal_uInt16 nPos) const
245 {
246 	return nPos < Count() ? *((String *)SmPickList::Get(nPos)) : String();
247 }
248 
Contains(const String & rString) const249 inline sal_Bool	SmStringPickList::Contains(const String &rString) const
250 {
251 	return SmPickList::Contains((void *)&rString);
252 }
253 #endif
254 
255 ////////////////////////////////////////////////////////////
256 //
257 // SmFontPickList
258 //
259 
260 class SmFontDialog;
261 
262 class SmFontPickList : public SmPickList
263 {
264 protected:
265 	virtual void   *CreateItem(const String& rString);
266 	virtual void   *CreateItem(const void *pItem);
267 	virtual void	DestroyItem(void *pItem);
268 
269 	virtual sal_Bool	CompareItem(const void *pFirstItem, const void *pSecondItem) const;
270 
271 	virtual String	GetStringItem(void *pItem);
272 
273 public:
SmFontPickList()274 	SmFontPickList()
275 		: SmPickList(0, 5) {}
SmFontPickList(sal_uInt16 nInitSize,sal_uInt16 nMaxSize)276 	SmFontPickList(sal_uInt16 nInitSize, sal_uInt16 nMaxSize)
277 		: SmPickList(nInitSize, nMaxSize) {}
SmFontPickList(const SmPickList & rOrig)278 	SmFontPickList(const SmPickList& rOrig )
279 		: SmPickList(rOrig) {}
~SmFontPickList()280     virtual ~SmFontPickList() { Clear(); }
281 
282     using   SfxPtrArr::Insert;
283 	virtual void	Insert(const Font &rFont);
284     using   SmPickList::Update;
285 	virtual void	Update(const Font &rFont, const Font &rNewFont);
286     using   SfxPtrArr::Remove;
287 	virtual void	Remove(const Font &rFont);
288 
289     using   SmPickList::Contains;
290 	inline sal_Bool		Contains(const Font &rFont) const;
291 	inline Font		Get(sal_uInt16 nPos = 0) const;
292 
293 	inline SmFontPickList& 	operator = (const SmFontPickList& rList);
294     using   SfxPtrArr::operator [];
295 	inline Font				operator [] (sal_uInt16 nPos) const;
296 
297 	void 			ReadFrom(const SmFontDialog& rDialog);
298 	void 			WriteTo(SmFontDialog& rDialog) const;
299 };
300 
operator =(const SmFontPickList & rList)301 inline SmFontPickList& SmFontPickList::operator = (const SmFontPickList& rList)
302 {
303 	*(SmPickList *)this = *(SmPickList *)&rList; return *this;
304 }
305 
operator [](sal_uInt16 nPos) const306 inline Font	SmFontPickList::operator [] (sal_uInt16 nPos) const
307 {
308 	return *((Font *)SmPickList::operator[](nPos));
309 }
310 
Get(sal_uInt16 nPos) const311 inline Font SmFontPickList::Get(sal_uInt16 nPos) const
312 {
313 	return nPos < Count() ? *((Font *)SmPickList::Get(nPos)) : Font();
314 }
315 
Contains(const Font & rFont) const316 inline sal_Bool	SmFontPickList::Contains(const Font &rFont) const
317 {
318 	return SmPickList::Contains((void *)&rFont);
319 }
320 
321 
322 ////////////////////////////////////////////////////////////
323 //
324 // SmStringPickComboBox
325 //
326 #ifdef NEVER
327 class SmStringPickComboBox : public SmStringPickList, public ComboBox
328 {
329 protected:
330 	virtual void LoseFocus();
331 
332 	DECL_LINK(SelectHdl, ComboBox *);
333 
334 public:
335 	SmStringPickComboBox(Window* pParent, WinBits nWinStyle = 0, sal_uInt16 nMax = 4);
336 	SmStringPickComboBox(Window* pParent, const ResId& rResId, sal_uInt16 nMax = 4);
337 
338 	SmStringPickComboBox& operator = (const SmStringPickList& rList);
339 
340 	void			SetText(const String& rStr);
341 
342 	virtual void	Insert(const String &rString);
343 	virtual void	Update(const String &rString, const String &rNewString);
344 	virtual void	Remove(const String &rString);
345 };
346 #endif
347 
348 ////////////////////////////////////////////////////////////
349 //
350 //	SmFontPickListBox
351 //
352 
353 class SmFontPickListBox : public SmFontPickList, public ListBox
354 {
355 protected:
356 	DECL_LINK(SelectHdl, ListBox *);
357 
358 public:
359 	SmFontPickListBox(Window* pParent, const ResId& rResId, sal_uInt16 nMax = 4);
360 
361 	SmFontPickListBox& operator = (const SmFontPickList& rList);
362 
363     using   SfxPtrArr::Insert;
364 	virtual void	Insert(const Font &rFont);
365     using   Window::Update;
366 	virtual void	Update(const Font &rFont, const Font &rNewFont);
367     using   SfxPtrArr::Remove;
368 	virtual void	Remove(const Font &rFont);
369 };
370 
371 #endif
372 
373