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