xref: /aoo42x/main/starmath/source/utility.cxx (revision d107581f)
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_starmath.hxx"
26 
27 
28 #include <sfx2/app.hxx>
29 #include <vcl/virdev.hxx>
30 #include <tools/string.hxx>
31 #include <tools/tenccvt.hxx>
32 #include <osl/thread.h>
33 
34 #include <tools/stream.hxx>
35 
36 #include "starmath.hrc"
37 
38 #include "utility.hxx"
39 #include "dialog.hxx"
40 #include "view.hxx"
41 #include "smdll.hxx"
42 
43 
44 ////////////////////////////////////////////////////////////
45 
46 // return pointer to active SmViewShell, if this is not possible
47 // return 0 instead.
48 //!! Since this method is based on the current focus it is somewhat
49 //!! unreliable and may return unexpected 0 pointers!
SmGetActiveView()50 SmViewShell * SmGetActiveView()
51 {
52 	SfxViewShell *pView = SfxViewShell::Current();
53 	return PTR_CAST(SmViewShell, pView);
54 }
55 
56 
57 ////////////////////////////////////////////////////////////
58 
59 
60 /**************************************************************************/
61 
SmPickList(sal_uInt16 nInitSize,sal_uInt16 nMaxSize)62 SmPickList::SmPickList(sal_uInt16 nInitSize, sal_uInt16 nMaxSize) :
63 	SfxPtrArr((sal_uInt8) nInitSize, 1)
64 {
65 	nSize = nMaxSize;
66 }
67 
68 
~SmPickList()69 SmPickList::~SmPickList()
70 {
71 	Clear();
72 }
73 
74 
operator =(const SmPickList & rList)75 SmPickList& SmPickList::operator=(const SmPickList& rList)
76 {
77 	sal_uInt16	nPos;
78 
79 	Clear();
80 	nSize = rList.nSize;
81 	for (nPos = 0; nPos < rList.Count(); nPos++)
82 		InsertPtr(nPos, CreateItem(rList.Get(nPos)));
83 
84 	return *this;
85 }
86 
87 
Insert(const void * pItem)88 void SmPickList::Insert(const void *pItem)
89 {
90 	Remove(pItem);
91 	InsertPtr(0, CreateItem(pItem));
92 
93 	if (Count() > nSize)
94 	{
95 		DestroyItem(GetPtr(nSize));
96 		RemovePtr(nSize, 1);
97 	}
98 }
99 
100 
Update(const void * pItem,const void * pNewItem)101 void SmPickList::Update(const void *pItem, const void *pNewItem)
102 {
103 	sal_uInt16	nPos;
104 
105 	for (nPos = 0; nPos < Count(); nPos++)
106 		if (CompareItem(GetPtr(nPos), pItem))
107 		{
108 			DestroyItem(GetPtr(nPos));
109 			GetPtr(nPos) = CreateItem(pNewItem);
110 			break;
111 		}
112 }
113 
Remove(const void * pItem)114 void SmPickList::Remove(const void *pItem)
115 {
116 	sal_uInt16	nPos;
117 
118 	for (nPos = 0; nPos < Count(); nPos++)
119 		if (CompareItem(GetPtr(nPos), pItem))
120 		{
121 			DestroyItem(GetPtr(nPos));
122 			RemovePtr(nPos, 1);
123 			break;
124 		}
125 }
126 
Clear()127 void SmPickList::Clear()
128 {
129 	sal_uInt16	nPos;
130 
131 	for (nPos = 0; nPos < Count(); nPos++)
132 		DestroyItem(GetPtr(nPos));
133 
134 	RemovePtr(0, Count());
135 }
136 
137 
138 /**************************************************************************/
139 /**************************************************************************/
140 
CreateItem(const String &)141 void * SmFontPickList::CreateItem(const String& /*rString*/)
142 {
143 	return new Font();
144 }
145 
CreateItem(const void * pItem)146 void * SmFontPickList::CreateItem(const void *pItem)
147 {
148 	return new Font(*((Font *) pItem));
149 }
150 
DestroyItem(void * pItem)151 void SmFontPickList::DestroyItem(void *pItem)
152 {
153 	delete (Font *)pItem;
154 }
155 
CompareItem(const void * pFirstItem,const void * pSecondItem) const156 sal_Bool SmFontPickList::CompareItem(const void *pFirstItem, const void *pSecondItem) const
157 {
158 	Font	*pFirstFont, *pSecondFont;
159 
160 	pFirstFont	= (Font *)pFirstItem;
161 	pSecondFont = (Font *)pSecondItem;
162 
163 	if (pFirstFont->GetName() == pSecondFont->GetName())
164 		if ((pFirstFont->GetFamily()  == pSecondFont->GetFamily())	&&
165 			(pFirstFont->GetCharSet() == pSecondFont->GetCharSet()) &&
166 			(pFirstFont->GetWeight()  == pSecondFont->GetWeight())	&&
167 			(pFirstFont->GetItalic()  == pSecondFont->GetItalic()))
168 			return (sal_True);
169 
170 	return sal_False;
171 }
172 
GetStringItem(void * pItem)173 String SmFontPickList::GetStringItem(void *pItem)
174 {
175 	Font   *pFont;
176 	String	aString;
177     const sal_Char *pDelim = ", ";
178 
179 	pFont = (Font *)pItem;
180 
181 	aString = pFont->GetName();
182 
183     if (IsItalic( *pFont ))
184     {
185         aString.AppendAscii( pDelim );
186 		aString += String(SmResId(RID_FONTITALIC));
187     }
188     if (IsBold( *pFont ))    // bold?
189     {
190         aString.AppendAscii( pDelim );
191 		aString += String(SmResId(RID_FONTBOLD));
192     }
193 
194 	return (aString);
195 }
196 
Insert(const Font & rFont)197 void SmFontPickList::Insert(const Font &rFont)
198 {
199 	SmPickList::Insert((void *)&rFont);
200 }
201 
Update(const Font & rFont,const Font & rNewFont)202 void SmFontPickList::Update(const Font &rFont, const Font &rNewFont)
203 {
204 	SmPickList::Update((void *)&rFont, (void *)&rNewFont);
205 }
206 
Remove(const Font & rFont)207 void SmFontPickList::Remove(const Font &rFont)
208 {
209 	SmPickList::Remove((void *)&rFont);
210 }
211 
212 
ReadFrom(const SmFontDialog & rDialog)213 void SmFontPickList::ReadFrom(const SmFontDialog& rDialog)
214 {
215 	Insert(rDialog.GetFont());
216 }
217 
WriteTo(SmFontDialog & rDialog) const218 void SmFontPickList::WriteTo(SmFontDialog& rDialog) const
219 {
220 	rDialog.SetFont(Get());
221 }
222 
223 
224 /**************************************************************************/
225 
226 
227 /**************************************************************************/
228 
229 IMPL_LINK( SmFontPickListBox, SelectHdl, ListBox *, /*pListBox*/ )
230 {
231 	sal_uInt16	nPos;
232 	String	aString;
233 
234 	nPos = GetSelectEntryPos();
235 
236 	if (nPos != 0)
237 	{
238 		SmFontPickList::Insert(Get(nPos));
239 		aString = GetEntry(nPos);
240 		RemoveEntry(nPos);
241 		InsertEntry(aString, 0);
242 	}
243 
244 	SelectEntryPos(0);
245 
246 	return 0;
247 }
248 
249 
SmFontPickListBox(Window * pParent,const ResId & rResId,sal_uInt16 nMax)250 SmFontPickListBox::SmFontPickListBox(Window* pParent, const ResId& rResId, sal_uInt16 nMax) :
251 	SmFontPickList(nMax, nMax),
252 	ListBox(pParent, rResId)
253 {
254 	SetSelectHdl(LINK(this, SmFontPickListBox, SelectHdl));
255 }
256 
257 
operator =(const SmFontPickList & rList)258 SmFontPickListBox& SmFontPickListBox::operator=(const SmFontPickList& rList)
259 {
260 	sal_uInt16 nPos;
261 
262 	*(SmFontPickList *)this = rList;
263 
264 	for (nPos = 0; nPos < Count(); nPos++)
265 		InsertEntry(GetStringItem(GetPtr(nPos)), nPos);
266 
267 	if (Count() > 0)
268 		SelectEntry(GetStringItem(GetPtr(0)));
269 
270 	return *this;
271 }
272 
Insert(const Font & rFont)273 void SmFontPickListBox::Insert(const Font &rFont)
274 {
275 	SmFontPickList::Insert(rFont);
276 
277 	RemoveEntry(GetStringItem(GetPtr(0)));
278 	InsertEntry(GetStringItem(GetPtr(0)), 0);
279 	SelectEntry(GetStringItem(GetPtr(0)));
280 
281 	while (GetEntryCount() > nSize)
282 		RemoveEntry(GetEntryCount() - 1);
283 
284 	return;
285 }
286 
287 
Update(const Font & rFont,const Font & rNewFont)288 void SmFontPickListBox::Update(const Font &rFont, const Font &rNewFont)
289 {
290 	SmFontPickList::Update(rFont, rNewFont);
291 
292 	// ********************** hier fehlt noch was
293 
294 	return;
295 }
296 
297 
Remove(const Font & rFont)298 void SmFontPickListBox::Remove(const Font &rFont)
299 {
300 	SmFontPickList::Remove(rFont);
301 
302 	// ********************** hier fehlt noch was
303 
304 	return;
305 }
306 
307 ////////////////////////////////////////
308 
IsItalic(const Font & rFont)309 sal_Bool IsItalic( const Font &rFont )
310 {
311     FontItalic eItalic = rFont.GetItalic();
312     // the code below leaves only _NONE and _DONTKNOW as not italic
313     return eItalic == ITALIC_OBLIQUE  ||  eItalic == ITALIC_NORMAL;
314 }
315 
316 
IsBold(const Font & rFont)317 sal_Bool IsBold( const Font &rFont )
318 {
319     FontWeight eWeight = rFont.GetWeight();
320     return eWeight != WEIGHT_DONTKNOW && eWeight > WEIGHT_NORMAL;
321 }
322 
323 
Impl_Init()324 void SmFace::Impl_Init()
325 {
326     SetSize( GetSize() );
327     SetTransparent( sal_True );
328     SetAlign( ALIGN_BASELINE );
329     SetColor( COL_AUTO );
330 }
331 
SetSize(const Size & rSize)332 void SmFace::SetSize(const Size& rSize)
333 {
334 	Size  aSize (rSize);
335 
336 	// check the requested size against minimum value
337 	static int __READONLY_DATA	nMinVal = SmPtsTo100th_mm(2);
338 
339 	if (aSize.Height() < nMinVal)
340 		aSize.Height() = nMinVal;
341 
342 	//! we don't force a maximum value here because this may prevent eg the
343 	//! parentheses in "left ( ... right )" from matching up with large
344 	//! bodies (eg stack{...} with many entries).
345 	//! Of course this is holds only if characters are used and not polygons.
346 
347 	Font::SetSize(aSize);
348 }
349 
350 
GetBorderWidth() const351 long SmFace::GetBorderWidth() const
352 {
353 	if (nBorderWidth < 0)
354 		return GetDefaultBorderWidth();
355 	else
356 		return nBorderWidth;
357 }
358 
operator =(const SmFace & rFace)359 SmFace & SmFace::operator = (const SmFace &rFace)
360 {
361 	Font::operator = (rFace);
362 	nBorderWidth = -1;
363 	return *this;
364 }
365 
366 
operator *=(SmFace & rFace,const Fraction & rFrac)367 SmFace & operator *= (SmFace &rFace, const Fraction &rFrac)
368 	// scales the width and height of 'rFace' by 'rFrac' and returns a
369 	// reference to 'rFace'.
370 	// It's main use is to make scaling fonts look easier.
371 {	const Size &rFaceSize = rFace.GetSize();
372 
373 	rFace.SetSize(Size(Fraction(rFaceSize.Width())	*= rFrac,
374 					   Fraction(rFaceSize.Height()) *= rFrac));
375 	return rFace;
376 }
377 
378 
379 
380