merge the formfield patch from ooo-build
[ooovba.git] / starmath / source / utility.cxx
blobfea4e39f61f56ebef46d9445c009274f3b53cc8d
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: utility.cxx,v $
10 * $Revision: 1.20 $
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_starmath.hxx"
35 #include <sfx2/app.hxx>
36 #include <vcl/virdev.hxx>
37 #include <tools/string.hxx>
38 #include <tools/tenccvt.hxx>
39 #include <osl/thread.h>
41 #include <tools/stream.hxx>
43 #include "starmath.hrc"
45 #include "utility.hxx"
46 #include "dialog.hxx"
47 #include "view.hxx"
48 #include "smdll.hxx"
51 // return pointer to active SmViewShell, if this is not possible
52 // return 0 instead.
53 //!! Since this method is based on the current focus it is somewhat
54 //!! unreliable and may return unexpected 0 pointers!
55 SmViewShell * SmGetActiveView()
57 SfxViewShell *pView = SfxViewShell::Current();
58 return PTR_CAST(SmViewShell, pView);
62 ////////////////////////////////////////////////////////////
65 /**************************************************************************/
67 SmPickList::SmPickList(USHORT nInitSize, USHORT nMaxSize) :
68 SfxPtrArr((BYTE) nInitSize, 1)
70 nSize = nMaxSize;
74 SmPickList::~SmPickList()
76 Clear();
80 SmPickList& SmPickList::operator=(const SmPickList& rList)
82 USHORT nPos;
84 Clear();
85 nSize = rList.nSize;
86 for (nPos = 0; nPos < rList.Count(); nPos++)
87 InsertPtr(nPos, CreateItem(rList.Get(nPos)));
89 return *this;
93 void SmPickList::Insert(const void *pItem)
95 Remove(pItem);
96 InsertPtr(0, CreateItem(pItem));
98 if (Count() > nSize)
100 DestroyItem(GetPtr(nSize));
101 RemovePtr(nSize, 1);
106 void SmPickList::Update(const void *pItem, const void *pNewItem)
108 USHORT nPos;
110 for (nPos = 0; nPos < Count(); nPos++)
111 if (CompareItem(GetPtr(nPos), pItem))
113 DestroyItem(GetPtr(nPos));
114 GetPtr(nPos) = CreateItem(pNewItem);
115 break;
119 void SmPickList::Remove(const void *pItem)
121 USHORT nPos;
123 for (nPos = 0; nPos < Count(); nPos++)
124 if (CompareItem(GetPtr(nPos), pItem))
126 DestroyItem(GetPtr(nPos));
127 RemovePtr(nPos, 1);
128 break;
132 void SmPickList::Clear()
134 USHORT nPos;
136 for (nPos = 0; nPos < Count(); nPos++)
137 DestroyItem(GetPtr(nPos));
139 RemovePtr(0, Count());
143 /**************************************************************************/
144 /**************************************************************************/
146 void * SmFontPickList::CreateItem(const String& /*rString*/)
148 return new Font();
151 void * SmFontPickList::CreateItem(const void *pItem)
153 return new Font(*((Font *) pItem));
156 void SmFontPickList::DestroyItem(void *pItem)
158 delete (Font *)pItem;
161 BOOL SmFontPickList::CompareItem(const void *pFirstItem, const void *pSecondItem) const
163 Font *pFirstFont, *pSecondFont;
165 pFirstFont = (Font *)pFirstItem;
166 pSecondFont = (Font *)pSecondItem;
168 if (pFirstFont->GetName() == pSecondFont->GetName())
169 if ((pFirstFont->GetFamily() == pSecondFont->GetFamily()) &&
170 (pFirstFont->GetCharSet() == pSecondFont->GetCharSet()) &&
171 (pFirstFont->GetWeight() == pSecondFont->GetWeight()) &&
172 (pFirstFont->GetItalic() == pSecondFont->GetItalic()))
173 return (TRUE);
175 return FALSE;
178 String SmFontPickList::GetStringItem(void *pItem)
180 Font *pFont;
181 String aString;
182 const sal_Char *pDelim = ", ";
184 pFont = (Font *)pItem;
186 aString = pFont->GetName();
188 if (IsItalic( *pFont ))
190 aString.AppendAscii( pDelim );
191 aString += String(SmResId(RID_FONTITALIC));
193 if (IsBold( *pFont )) // bold?
195 aString.AppendAscii( pDelim );
196 aString += String(SmResId(RID_FONTBOLD));
199 return (aString);
202 void SmFontPickList::Insert(const Font &rFont)
204 SmPickList::Insert((void *)&rFont);
207 void SmFontPickList::Update(const Font &rFont, const Font &rNewFont)
209 SmPickList::Update((void *)&rFont, (void *)&rNewFont);
212 void SmFontPickList::Remove(const Font &rFont)
214 SmPickList::Remove((void *)&rFont);
218 void SmFontPickList::ReadFrom(const SmFontDialog& rDialog)
220 Insert(rDialog.GetFont());
223 void SmFontPickList::WriteTo(SmFontDialog& rDialog) const
225 rDialog.SetFont(Get());
229 /**************************************************************************/
232 /**************************************************************************/
234 IMPL_LINK( SmFontPickListBox, SelectHdl, ListBox *, /*pListBox*/ )
236 USHORT nPos;
237 String aString;
239 nPos = GetSelectEntryPos();
241 if (nPos != 0)
243 SmFontPickList::Insert(Get(nPos));
244 aString = GetEntry(nPos);
245 RemoveEntry(nPos);
246 InsertEntry(aString, 0);
249 SelectEntryPos(0);
251 return 0;
255 SmFontPickListBox::SmFontPickListBox(Window* pParent, const ResId& rResId, USHORT nMax) :
256 SmFontPickList(nMax, nMax),
257 ListBox(pParent, rResId)
259 SetSelectHdl(LINK(this, SmFontPickListBox, SelectHdl));
263 SmFontPickListBox& SmFontPickListBox::operator=(const SmFontPickList& rList)
265 USHORT nPos;
267 *(SmFontPickList *)this = rList;
269 for (nPos = 0; nPos < Count(); nPos++)
270 InsertEntry(GetStringItem(GetPtr(nPos)), nPos);
272 if (Count() > 0)
273 SelectEntry(GetStringItem(GetPtr(0)));
275 return *this;
278 void SmFontPickListBox::Insert(const Font &rFont)
280 SmFontPickList::Insert(rFont);
282 RemoveEntry(GetStringItem(GetPtr(0)));
283 InsertEntry(GetStringItem(GetPtr(0)), 0);
284 SelectEntry(GetStringItem(GetPtr(0)));
286 while (GetEntryCount() > nSize)
287 RemoveEntry(GetEntryCount() - 1);
289 return;
293 void SmFontPickListBox::Update(const Font &rFont, const Font &rNewFont)
295 SmFontPickList::Update(rFont, rNewFont);
297 // ********************** hier fehlt noch was
299 return;
303 void SmFontPickListBox::Remove(const Font &rFont)
305 SmFontPickList::Remove(rFont);
307 // ********************** hier fehlt noch was
309 return;
312 ////////////////////////////////////////
314 BOOL IsItalic( const Font &rFont )
316 FontItalic eItalic = rFont.GetItalic();
317 // the code below leaves only _NONE and _DONTKNOW as not italic
318 return eItalic == ITALIC_OBLIQUE || eItalic == ITALIC_NORMAL;
322 BOOL IsBold( const Font &rFont )
324 FontWeight eWeight = rFont.GetWeight();
325 return eWeight != WEIGHT_DONTKNOW && eWeight > WEIGHT_NORMAL;
329 void SmFace::Impl_Init()
331 SetSize( GetSize() );
332 SetTransparent( TRUE );
333 SetAlign( ALIGN_BASELINE );
334 SetColor( COL_AUTO );
337 void SmFace::SetSize(const Size& rSize)
339 Size aSize (rSize);
341 // check the requested size against minimum value
342 static int __READONLY_DATA nMinVal = SmPtsTo100th_mm(2);
344 if (aSize.Height() < nMinVal)
345 aSize.Height() = nMinVal;
347 //! we don't force a maximum value here because this may prevent eg the
348 //! parentheses in "left ( ... right )" from matching up with large
349 //! bodies (eg stack{...} with many entries).
350 //! Of course this is holds only if characters are used and not polygons.
352 Font::SetSize(aSize);
356 long SmFace::GetBorderWidth() const
358 if (nBorderWidth < 0)
359 return GetDefaultBorderWidth();
360 else
361 return nBorderWidth;
364 SmFace & SmFace::operator = (const SmFace &rFace)
366 Font::operator = (rFace);
367 nBorderWidth = -1;
368 return *this;
372 SmFace & operator *= (SmFace &rFace, const Fraction &rFrac)
373 // scales the width and height of 'rFace' by 'rFrac' and returns a
374 // reference to 'rFace'.
375 // It's main use is to make scaling fonts look easier.
376 { const Size &rFaceSize = rFace.GetSize();
378 rFace.SetSize(Size(Fraction(rFaceSize.Width()) *= rFrac,
379 Fraction(rFaceSize.Height()) *= rFrac));
380 return rFace;