1 /* AbiWord
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 #include <stdio.h>
21 #include <stdlib.h>
22 #include "ut_string.h"
23 #include "gr_Painter.h"
24 #include "ap_Dialog_PageNumbers.h"
25 
AP_Dialog_PageNumbers(XAP_DialogFactory * pDlgFactory,XAP_Dialog_Id id)26 AP_Dialog_PageNumbers::AP_Dialog_PageNumbers (XAP_DialogFactory * pDlgFactory,
27 					      XAP_Dialog_Id id)
28 	: XAP_Dialog_NonPersistent(pDlgFactory,id, "interface/dialogpagenumbers")
29 {
30 	m_align   = id_CALIGN;
31 	m_control = id_FTR;
32 
33 	m_answer  = a_OK;
34 	m_pFrame  = NULL;
35 	m_preview = NULL;
36 }
37 
~AP_Dialog_PageNumbers(void)38 AP_Dialog_PageNumbers::~AP_Dialog_PageNumbers(void)
39 {
40 	DELETEP(m_preview);
41 }
42 
getAnswer(void)43 AP_Dialog_PageNumbers::tAnswer AP_Dialog_PageNumbers::getAnswer(void)
44 {
45 	return m_answer;
46 }
47 
getAlignment(void)48 AP_Dialog_PageNumbers::tAlign AP_Dialog_PageNumbers::getAlignment(void)
49 {
50 	return m_align;
51 }
52 
isHeader(void)53 bool AP_Dialog_PageNumbers::isHeader(void)
54 {
55 	return (m_control == id_HDR);
56 }
57 
isFooter(void)58 bool AP_Dialog_PageNumbers::isFooter(void)
59 {
60 	return (m_control == id_FTR);
61 }
62 
_updatePreview(AP_Dialog_PageNumbers::tAlign align,AP_Dialog_PageNumbers::tControl ctrl)63 void AP_Dialog_PageNumbers::_updatePreview(AP_Dialog_PageNumbers::tAlign align,
64 					   AP_Dialog_PageNumbers::tControl ctrl)
65 {
66 	UT_return_if_fail (m_preview);
67 	m_preview->setHdrFtr (ctrl);
68 	m_preview->setAlign (align);
69 	m_preview->queueDraw ();
70 }
71 
_createPreviewFromGC(GR_Graphics * gc,UT_uint32 width,UT_uint32 height)72 void AP_Dialog_PageNumbers::_createPreviewFromGC(GR_Graphics * gc,
73 						 UT_uint32 width,
74 						 UT_uint32 height)
75 {
76 	UT_return_if_fail (gc);
77 	m_preview = new AP_Preview_PageNumbers (gc);
78 	UT_return_if_fail (m_preview);
79 
80 	m_preview->setWindowSize (width, height);
81 }
82 
83 /**************************************************************************/
84 
AP_Preview_PageNumbers(GR_Graphics * gc)85 AP_Preview_PageNumbers::AP_Preview_PageNumbers (GR_Graphics * gc)
86   : XAP_Preview (gc)
87 {
88 	char fontString [10];
89 	sprintf(fontString, "%dpt", 8);
90 
91 	GR_Font * found =  m_gc->findFont("Times New Roman", "normal",
92 									  "", "normal", "", fontString,
93 									  NULL);
94 
95 	m_gc->setFont(found);
96 
97 	UT_UCS4_cloneString_char (&m_str, "1");
98 }
99 
~AP_Preview_PageNumbers(void)100 AP_Preview_PageNumbers::~AP_Preview_PageNumbers(void)
101 {
102 	FREEP(m_str);
103 }
104 
setHdrFtr(AP_Dialog_PageNumbers::tControl control)105 void AP_Preview_PageNumbers::setHdrFtr(AP_Dialog_PageNumbers::tControl control)
106 {
107 	m_control = control;
108 }
109 
setAlign(AP_Dialog_PageNumbers::tAlign align)110 void AP_Preview_PageNumbers::setAlign(AP_Dialog_PageNumbers::tAlign align)
111 {
112 	m_align = align;
113 }
114 
draw(const UT_Rect * clip)115 void AP_Preview_PageNumbers::draw (const UT_Rect *clip)
116 {
117 	UT_UNUSED(clip);
118 	GR_Painter painter(m_gc);
119 
120 	int x = 0, y = 0;
121 
122 	UT_sint32 iWidth = m_gc->tlu (getWindowWidth());
123 	UT_sint32 iHeight = m_gc->tlu (getWindowHeight());
124 	UT_Rect pageRect(m_gc->tlu(7), m_gc->tlu(7), iWidth - m_gc->tlu(14), iHeight - m_gc->tlu(14));
125 
126 	painter.fillRect(GR_Graphics::CLR3D_Background, 0, 0, iWidth, iHeight);
127 	painter.clearArea(pageRect.left, pageRect.top, pageRect.width, pageRect.height);
128 
129 	// actually draw some "text" on the preview for a more realistic appearance
130 
131 	m_gc->setLineWidth(m_gc->tlu(1));
132 	m_gc->setColor3D(GR_Graphics::CLR3D_Foreground);
133 
134 	UT_sint32 iFontHeight = m_gc->getFontHeight ();
135 	UT_sint32 step = m_gc->tlu(4);
136 
137 	for (int txty = pageRect.top + (2 * iFontHeight); txty < pageRect.top + pageRect.height - (2 * iFontHeight); txty += step)
138 	{
139 		painter.drawLine (pageRect.left + m_gc->tlu(5), txty, pageRect.left + pageRect.width - m_gc->tlu(5), txty);
140 	}
141 
142 	// draw in the page number as a header or footer, properly aligned
143 
144 	switch (m_align)
145 	{
146 		case AP_Dialog_PageNumbers::id_RALIGN : x = pageRect.left + pageRect.width - (2 * m_gc->measureUnRemappedChar(*m_str)); break;
147 		case AP_Dialog_PageNumbers::id_CALIGN : x = pageRect.left + (int)(pageRect.width / 2); break;
148 		case AP_Dialog_PageNumbers::id_LALIGN : x = pageRect.left + m_gc->measureUnRemappedChar(*m_str); break;
149 	}
150 
151 	switch (m_control)
152 	{
153 		case AP_Dialog_PageNumbers::id_HDR : y = pageRect.top + (int)(iFontHeight / 2); break;
154 		case AP_Dialog_PageNumbers::id_FTR : y = pageRect.top + pageRect.height - (int)(1.5 * iFontHeight); break;
155 	}
156 
157 	//m_gc->setColor3D(GR_Graphics::CLR3D_Foreground);
158 	painter.drawChars (m_str, 0, UT_UCS4_strlen(m_str), x, y);
159 }
160