1 /* AbiWord
2 * Copyright (C) 2001 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 "ut_string_class.h"
21 #include "ut_units.h"
22
23 #include "xap_Dlg_Image.h"
24 #include "xap_Preview_Zoom.h"
25 #include "xap_Frame.h"
26 #include "xap_App.h"
27
XAP_Dialog_Image(XAP_DialogFactory * pDlgFactory,XAP_Dialog_Id id)28 XAP_Dialog_Image::XAP_Dialog_Image(XAP_DialogFactory * pDlgFactory, XAP_Dialog_Id id)
29 : XAP_Dialog_NonPersistent (pDlgFactory, id, "interface/dialogimageproperties"),
30 m_bAspect(true),
31 m_width(0),
32 m_height(0),
33 m_maxWidth (0),
34 m_maxHeight(0),
35 m_answer(a_OK),
36 m_HeightString("0.0in"),
37 m_WidthString("0.0in"),
38 m_bHeightChanged(false),
39 m_bWidthChanged(false),
40 m_PreferedUnits(DIM_IN),
41 m_iWrappingType(WRAP_INLINE),
42 m_iPositionTo(POSITION_TO_PARAGRAPH),
43 m_bInHdrFtr(false),
44 m_bTightWrap(false)
45 {
46 }
47
~XAP_Dialog_Image()48 XAP_Dialog_Image::~XAP_Dialog_Image ()
49 {
50 }
51
52
53 /*!
54 * Returns the dimensioned string that defines the height of the
55 * Image.
56 \returns const char * dimensioned string which is the height of the column.
57 */
getHeightString(void)58 const char * XAP_Dialog_Image::getHeightString(void)
59 {
60 return m_HeightString.c_str();
61 }
62
isInHdrFtr(void)63 bool XAP_Dialog_Image::isInHdrFtr(void)
64 {
65 return m_bInHdrFtr;
66 }
67 /*!
68 * Returns the dimensioned string that defines the height of the
69 * Image.
70 \returns const char * dimensioned string which is the height of the column.
71 */
getWidthString(void)72 const char * XAP_Dialog_Image::getWidthString(void)
73 {
74 return m_WidthString.c_str();
75 }
76
77
78 /*!
79 * Returns the increment associated with the dimension defined in the string.
80 \param const char * sz the dimensioned string.
81 \returns double - the increment associated with the dimension in sz
82 */
getIncrement(const char * sz)83 double XAP_Dialog_Image::getIncrement(const char * sz)
84 {
85 double inc = 0.1;
86 UT_Dimension dim = UT_determineDimension(sz);
87 switch( dim )
88 {
89 default:
90 case DIM_IN:
91 inc = 0.02;
92 break;
93 case DIM_CM:
94 inc = 0.1;
95 break;
96 case DIM_MM:
97 case DIM_PI:
98 case DIM_PT:
99 case DIM_PX:
100 inc = 1.0;
101 break;
102 }
103 return inc;
104 }
105
106
107 /*!
108 * Increment the member variable UT_String defining the dimensioned string
109 * for the image height.
110 */
incrementHeight(bool bIncrement)111 void XAP_Dialog_Image::incrementHeight(bool bIncrement)
112 {
113 double inc = getIncrement(m_HeightString.c_str());
114 if(!bIncrement)
115 {
116 inc = -inc;
117 }
118
119 m_HeightString = UT_incrementDimString(m_HeightString.c_str(),inc);
120 setPreferedUnits( UT_determineDimension(getHeightString(), DIM_none) );
121 setHeight( UT_convertToInches(getHeightString()), true );
122 }
123
124 /*!
125 * Increment the member variable UT_String defining the dimensioned string
126 * for the image width.
127 */
incrementWidth(bool bIncrement)128 void XAP_Dialog_Image::incrementWidth(bool bIncrement)
129 {
130 double inc = getIncrement(m_WidthString.c_str());
131 if(!bIncrement)
132 {
133 inc = -inc;
134 }
135
136 m_WidthString = UT_incrementDimString(m_WidthString.c_str(),inc);
137 setPreferedUnits( UT_determineDimension(getWidthString(), DIM_none) );
138 setWidth( UT_convertToInches(getWidthString()), true );
139 }
140
141
142 /*!
143 * Set the member string variable m_HeightString
144 \param const char * szHeight is the string containing the new value
145 */
setHeight(const char * szHeight)146 void XAP_Dialog_Image::setHeight(const char * szHeight)
147 {
148 UT_Dimension dim = UT_determineDimension(szHeight, DIM_none);
149 if(dim != DIM_none)
150 {
151 m_bHeightChanged = true;
152 m_HeightString = szHeight;
153 setPreferedUnits( dim );
154 setHeight( UT_convertToInches(getHeightString()), true );
155 }
156 }
157
158 /*!
159 * Set the member string variable m_HeightString from the a double value of
160 * the image.
161 \param double dHeight is the height of the image in inches.
162 */
setHeight(double dHeight,bool checkaspect)163 void XAP_Dialog_Image::setHeight(double dHeight, bool checkaspect)
164 {
165 if(checkaspect && m_bAspect && m_height!=0.0)
166 setWidthAndHeight(dHeight, false);
167 else
168 {
169 m_height = dHeight*72.0;
170 if(m_height < 0.0)
171 {
172 m_height = 0.1;
173 dHeight = 0.1;
174 }
175 else if(m_height > m_maxHeight)
176 {
177 m_height = m_maxHeight;
178 dHeight = (m_maxHeight - 1)/72.0;
179 }
180
181 m_HeightString = UT_convertInchesToDimensionString(getPreferedUnits(),dHeight);
182 }
183 }
184
185 /*!
186 * Set the member string variable m_HeightString
187 \param const char * szWidth is the string containing the new value
188 */
setWidth(const char * szWidth)189 void XAP_Dialog_Image::setWidth(const char * szWidth)
190 {
191 UT_Dimension dim = UT_determineDimension(szWidth, DIM_none);
192 if(dim != DIM_none)
193 {
194 m_bWidthChanged = true;
195 m_WidthString = szWidth;
196 setPreferedUnits( dim );
197 setWidth( UT_convertToInches(getWidthString()), true );
198 }
199 }
200
201 /*!
202 * Set the member string variable m_WidthString from the a double value of
203 * the image.
204 \param double dWidth is the width of the image in inches.
205 */
setWidth(double dWidth,bool checkaspect)206 void XAP_Dialog_Image::setWidth(double dWidth, bool checkaspect)
207 {
208 if(checkaspect && m_bAspect && m_width!=0.0)
209 setWidthAndHeight(dWidth, true);
210 else
211 {
212 m_width = dWidth*72.0;
213 if(m_width < 0.0)
214 {
215 m_width = 0.1;
216 dWidth = 0.1;
217 }
218 else if(m_width > m_maxWidth)
219 {
220 m_width = m_maxWidth;
221 dWidth = (m_maxWidth - 1)/72.0;
222 }
223 m_WidthString = UT_convertInchesToDimensionString(getPreferedUnits(),dWidth);
224
225 }
226 }
227
228
setWidthAndHeight(double wh,bool iswidth)229 void XAP_Dialog_Image::setWidthAndHeight(double wh, bool iswidth)
230 {
231 double orig_width,orig_height;
232
233 orig_width = m_width;
234 orig_height = m_height;
235
236 if (wh < 0.1) wh=0.1;
237 if (orig_width < 1.) orig_width = 1.;
238 if (orig_height < 1.) orig_height = 1.;
239
240 if (iswidth)
241 {
242 m_width = wh*72.0;
243 m_height = m_width*orig_height/orig_width;
244 }
245 else
246 {
247 m_height = wh*72.0;
248 m_width = m_height*orig_width/orig_height;
249 }
250
251 if (m_width > m_maxWidth)
252 {
253 m_width = m_maxWidth;
254 m_height = m_width*orig_height/orig_width;
255 }
256
257 if (m_height > m_maxHeight)
258 {
259 m_height = m_maxHeight;
260 m_width = m_height*orig_width/orig_height;
261 }
262
263
264 m_WidthString = UT_convertInchesToDimensionString(getPreferedUnits(),m_width/72.0);
265 m_HeightString = UT_convertInchesToDimensionString(getPreferedUnits(),m_height/72.0);
266
267 }
268
269
270 /*!
271 * Set the member string variable m_WidthString from the pixel value of
272 * the image. This is to set the initial value if it's not defined.
273 \param UT_sint32 iWidth is the pixel width of the string.
274 */
setWidth(UT_sint32 iWidth)275 void XAP_Dialog_Image::setWidth(UT_sint32 iWidth)
276 {
277 setWidth( ((double) iWidth)/72.0, false );
278 }
279
setWrapping(WRAPPING_TYPE iWrap)280 void XAP_Dialog_Image::setWrapping(WRAPPING_TYPE iWrap)
281 {
282 m_iWrappingType = iWrap;
283 }
284
setPositionTo(POSITION_TO iPos)285 void XAP_Dialog_Image::setPositionTo(POSITION_TO iPos)
286 {
287 m_iPositionTo = iPos;
288 }
289
290 /*!
291 * Set the member string variable m_HeightString from the pixel value of
292 * the image. This is to set the initial value if it's not defined.
293 \param UT_sint32 iHeight is the pixels height of the image.
294 */
setHeight(UT_sint32 iHeight)295 void XAP_Dialog_Image::setHeight(UT_sint32 iHeight)
296 {
297 setHeight( ((double) iHeight)/72.0, false );
298 }
299
300 /*!
301 * Converts the string sz into the units seleced for the ruler.
302 \param const char * sz is the strinewheightng containing the old value
303 \param UT_String & pRet is the string to which the new value is copied.
304 */
_convertToPreferredUnits(const char * sz,UT_String & pRet)305 void XAP_Dialog_Image::_convertToPreferredUnits(const char *sz, UT_String & pRet)
306 {
307 UT_Dimension PreferedUnits = getPreferedUnits();
308 pRet = (const gchar *) UT_reformatDimensionString(PreferedUnits,sz);
309 }
310
311 /*!
312 * Sets the prefered units for the dialog.
313 */
setPreferedUnits(UT_Dimension dim)314 void XAP_Dialog_Image::setPreferedUnits(UT_Dimension dim)
315 {
316 m_PreferedUnits = dim;
317 }
318
319 /*!
320 * Returns the current units used for the rulers.
321 */
getPreferedUnits(void)322 UT_Dimension XAP_Dialog_Image::getPreferedUnits(void)
323 {
324 return m_PreferedUnits;
325 }
326
327
328