1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: utility.cxx,v $
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"
51 // return pointer to active SmViewShell, if this is not possible
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)
74 SmPickList::~SmPickList()
80 SmPickList
& SmPickList::operator=(const SmPickList
& rList
)
86 for (nPos
= 0; nPos
< rList
.Count(); nPos
++)
87 InsertPtr(nPos
, CreateItem(rList
.Get(nPos
)));
93 void SmPickList::Insert(const void *pItem
)
96 InsertPtr(0, CreateItem(pItem
));
100 DestroyItem(GetPtr(nSize
));
106 void SmPickList::Update(const void *pItem
, const void *pNewItem
)
110 for (nPos
= 0; nPos
< Count(); nPos
++)
111 if (CompareItem(GetPtr(nPos
), pItem
))
113 DestroyItem(GetPtr(nPos
));
114 GetPtr(nPos
) = CreateItem(pNewItem
);
119 void SmPickList::Remove(const void *pItem
)
123 for (nPos
= 0; nPos
< Count(); nPos
++)
124 if (CompareItem(GetPtr(nPos
), pItem
))
126 DestroyItem(GetPtr(nPos
));
132 void SmPickList::Clear()
136 for (nPos
= 0; nPos
< Count(); nPos
++)
137 DestroyItem(GetPtr(nPos
));
139 RemovePtr(0, Count());
143 /**************************************************************************/
144 /**************************************************************************/
146 void * SmFontPickList::CreateItem(const String
& /*rString*/)
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()))
178 String
SmFontPickList::GetStringItem(void *pItem
)
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
));
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*/ )
239 nPos
= GetSelectEntryPos();
243 SmFontPickList::Insert(Get(nPos
));
244 aString
= GetEntry(nPos
);
246 InsertEntry(aString
, 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
)
267 *(SmFontPickList
*)this = rList
;
269 for (nPos
= 0; nPos
< Count(); nPos
++)
270 InsertEntry(GetStringItem(GetPtr(nPos
)), nPos
);
273 SelectEntry(GetStringItem(GetPtr(0)));
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);
293 void SmFontPickListBox::Update(const Font
&rFont
, const Font
&rNewFont
)
295 SmFontPickList::Update(rFont
, rNewFont
);
297 // ********************** hier fehlt noch was
303 void SmFontPickListBox::Remove(const Font
&rFont
)
305 SmFontPickList::Remove(rFont
);
307 // ********************** hier fehlt noch was
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
)
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();
364 SmFace
& SmFace::operator = (const SmFace
&rFace
)
366 Font::operator = (rFace
);
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
));