| 1 |
/* AbiSource Application Framework |
| 2 |
* Copyright (C) 1998 AbiSource, Inc. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU General Public License |
| 6 |
* as published by the Free Software Foundation; either version 2 |
| 7 |
* of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 17 |
* 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
|
| 21 |
#include <stdlib.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include "ut_assert.h" |
| 24 |
#include "ut_types.h" |
| 25 |
#include "ut_string.h" |
| 26 |
#include "gr_Graphics.h" |
| 27 |
#include "gr_Painter.h" |
| 28 |
#include "xap_Preview_Zoom.h" |
| 29 |
|
| 30 |
XAP_Preview_Zoom::XAP_Preview_Zoom(GR_Graphics * gc) |
| 31 |
: XAP_Preview(gc) |
| 32 |
{ |
| 33 |
m_string = NULL; |
| 34 |
m_pFont = NULL; |
| 35 |
|
| 36 |
// m_gc is set in base class, so set up defaults |
| 37 |
m_zoomPercent = 100; |
| 38 |
setFont(XAP_Preview_Zoom::font_NORMAL); |
| 39 |
setDrawAtPosition(XAP_Preview_Zoom::pos_CENTER); |
| 40 |
} |
| 41 |
|
| 42 |
XAP_Preview_Zoom::~XAP_Preview_Zoom() |
| 43 |
{ |
| 44 |
FREEP(m_string); |
| 45 |
} |
| 46 |
|
| 47 |
void XAP_Preview_Zoom::setDrawAtPosition(XAP_Preview_Zoom::tPos pos) |
| 48 |
{ |
| 49 |
UT_ASSERT(m_gc); |
| 50 |
m_pos = pos; |
| 51 |
} |
| 52 |
|
| 53 |
void XAP_Preview_Zoom::setFont(XAP_Preview_Zoom::tFont f) |
| 54 |
{ |
| 55 |
UT_ASSERT(m_gc); |
| 56 |
UT_ASSERT(m_zoomPercent > 0); |
| 57 |
char fontString [10]; |
| 58 |
|
| 59 |
// when searching for fonts, we use these |
| 60 |
GR_Font * found = NULL; |
| 61 |
|
| 62 |
switch (f) |
| 63 |
{ |
| 64 |
case XAP_Preview_Zoom::font_NORMAL: |
| 65 |
sprintf (fontString, "%dpt", (10 * m_zoomPercent / 100)); |
| 66 |
found = m_gc->findFont("Times New Roman", |
| 67 |
"normal", "", "normal", |
| 68 |
"", fontString, |
| 69 |
NULL); |
| 70 |
|
| 71 |
if (found) |
| 72 |
{ |
| 73 |
m_gc->setFont(found); |
| 74 |
m_pFont = found; |
| 75 |
} |
| 76 |
else { |
| 77 |
UT_ASSERT(UT_SHOULD_NOT_HAPPEN); |
| 78 |
} |
| 79 |
break; |
| 80 |
default: |
| 81 |
UT_ASSERT(UT_SHOULD_NOT_HAPPEN); |
| 82 |
} |
| 83 |
|
| 84 |
// store it for next time |
| 85 |
m_previewFont = f; |
| 86 |
} |
| 87 |
|
| 88 |
void XAP_Preview_Zoom::setZoomPercent(UT_uint32 percent) |
| 89 |
{ |
| 90 |
UT_ASSERT(m_gc); |
| 91 |
UT_ASSERT(percent > 0 || percent < 1000); |
| 92 |
|
| 93 |
m_zoomPercent = percent; |
| 94 |
|
| 95 |
// re-calculate the GC's font using the new zoom size |
| 96 |
setFont(m_previewFont); |
| 97 |
} |
| 98 |
|
| 99 |
bool XAP_Preview_Zoom::setString(const char * string) |
| 100 |
{ |
| 101 |
UT_ASSERT(m_gc); |
| 102 |
UT_ASSERT(string); |
| 103 |
|
| 104 |
FREEP(m_string); |
| 105 |
|
| 106 |
bool foo = UT_UCS4_cloneString_char(&m_string, string); |
| 107 |
return foo; |
| 108 |
} |
| 109 |
|
| 110 |
bool XAP_Preview_Zoom::setString(UT_UCSChar * string) |
| 111 |
{ |
| 112 |
UT_ASSERT(m_gc); |
| 113 |
UT_ASSERT(string); |
| 114 |
|
| 115 |
FREEP(m_string); |
| 116 |
|
| 117 |
bool foo = UT_UCS4_cloneString(&m_string, string); |
| 118 |
return foo; |
| 119 |
} |
| 120 |
|
| 121 |
void XAP_Preview_Zoom::draw(const UT_Rect *clip) |
| 122 |
{ |
| 123 |
UT_UNUSED(clip); |
| 124 |
UT_ASSERT(m_gc); |
| 125 |
UT_ASSERT(m_string); |
| 126 |
|
| 127 |
GR_Painter painter(m_gc); |
| 128 |
|
| 129 |
UT_sint32 iWidth = m_gc->tlu (getWindowWidth()); |
| 130 |
UT_sint32 iHeight = m_gc->tlu (getWindowHeight()); |
| 131 |
UT_Rect pageRect(m_gc->tlu(7), m_gc->tlu(7), iWidth - m_gc->tlu(14), iHeight - m_gc->tlu(14)); |
| 132 |
|
| 133 |
painter.fillRect(GR_Graphics::CLR3D_Background, 0, 0, iWidth, iHeight); |
| 134 |
painter.clearArea(pageRect.left, pageRect.top, pageRect.width, pageRect.height); |
| 135 |
|
| 136 |
pageRect.left += m_gc->tlu(5); |
| 137 |
pageRect.top += m_gc->tlu(5); |
| 138 |
pageRect.width -= m_gc->tlu(10); |
| 139 |
pageRect.height -= m_gc->tlu(10); |
| 140 |
|
| 141 |
// set the cliprect to our page size, so we don't draw over our borders |
| 142 |
m_gc->setClipRect(&pageRect); |
| 143 |
|
| 144 |
painter.drawChars(m_string, 0, UT_UCS4_strlen(m_string), pageRect.left, pageRect.top); |
| 145 |
|
| 146 |
UT_Rect rr (0, 0, iWidth, iHeight); |
| 147 |
m_gc->setClipRect(&rr); |
| 148 |
} |