Sync to trunk (r44371)
[reactos.git] / reactos / dll / win32 / gdiplus / stringformat.c
1 /*
2 *
3 * Copyright (C) 2007 Google (Evan Stade)
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20 #include <stdarg.h>
21
22 #include "windef.h"
23 #include "winbase.h"
24 #include "wingdi.h"
25 #include "winnls.h"
26
27 #include "objbase.h"
28
29 #include "gdiplus.h"
30 #include "gdiplus_private.h"
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
34
35 GpStatus WINGDIPAPI GdipCreateStringFormat(INT attr, LANGID lang,
36 GpStringFormat **format)
37 {
38 TRACE("(%i, %x, %p)\n", attr, lang, format);
39
40 if(!format)
41 return InvalidParameter;
42
43 *format = GdipAlloc(sizeof(GpStringFormat));
44 if(!*format) return OutOfMemory;
45
46 (*format)->attr = attr;
47 (*format)->lang = lang;
48 (*format)->digitlang = LANG_NEUTRAL;
49 (*format)->trimming = StringTrimmingCharacter;
50 (*format)->digitsub = StringDigitSubstituteUser;
51 /* tabstops */
52 (*format)->tabcount = 0;
53 (*format)->firsttab = 0.0;
54 (*format)->tabs = NULL;
55
56 return Ok;
57 }
58
59 GpStatus WINGDIPAPI GdipDeleteStringFormat(GpStringFormat *format)
60 {
61 if(!format)
62 return InvalidParameter;
63
64 GdipFree(format->tabs);
65 GdipFree(format);
66
67 return Ok;
68 }
69
70 GpStatus WINGDIPAPI GdipStringFormatGetGenericDefault(GpStringFormat **format)
71 {
72 GpStatus stat;
73
74 if (!format)
75 return InvalidParameter;
76
77 stat = GdipCreateStringFormat(0, LANG_NEUTRAL, format);
78 if(stat != Ok)
79 return stat;
80
81 (*format)->align = StringAlignmentNear;
82 (*format)->vertalign = StringAlignmentNear;
83
84 return Ok;
85 }
86
87 GpStatus WINGDIPAPI GdipGetStringFormatAlign(GpStringFormat *format,
88 StringAlignment *align)
89 {
90 if(!format || !align)
91 return InvalidParameter;
92
93 *align = format->align;
94
95 return Ok;
96 }
97
98 GpStatus WINGDIPAPI GdipGetStringFormatDigitSubstitution(GDIPCONST GpStringFormat *format,
99 LANGID *language, StringDigitSubstitute *substitute)
100 {
101 if(!format)
102 return InvalidParameter;
103
104 if(language) *language = format->digitlang;
105 if(substitute) *substitute = format->digitsub;
106
107 return Ok;
108 }
109
110 GpStatus WINGDIPAPI GdipGetStringFormatFlags(GDIPCONST GpStringFormat* format,
111 INT* flags)
112 {
113 if (!(format && flags))
114 return InvalidParameter;
115
116 *flags = format->attr;
117
118 return Ok;
119 }
120
121 GpStatus WINGDIPAPI GdipGetStringFormatHotkeyPrefix(GDIPCONST GpStringFormat
122 *format, INT *hkpx)
123 {
124 if(!format || !hkpx)
125 return InvalidParameter;
126
127 *hkpx = (INT)format->hkprefix;
128
129 return Ok;
130 }
131
132 GpStatus WINGDIPAPI GdipGetStringFormatLineAlign(GpStringFormat *format,
133 StringAlignment *align)
134 {
135 if(!format || !align)
136 return InvalidParameter;
137
138 *align = format->vertalign;
139
140 return Ok;
141 }
142
143 GpStatus WINGDIPAPI GdipGetStringFormatMeasurableCharacterRangeCount(
144 GDIPCONST GpStringFormat* format, INT* count)
145 {
146 if (!(format && count))
147 return InvalidParameter;
148
149 FIXME("stub: %p %p\n", format, count);
150
151 return NotImplemented;
152 }
153
154 GpStatus WINGDIPAPI GdipGetStringFormatTabStopCount(GDIPCONST GpStringFormat *format,
155 INT *count)
156 {
157 if(!format || !count)
158 return InvalidParameter;
159
160 *count = format->tabcount;
161
162 return Ok;
163 }
164
165 GpStatus WINGDIPAPI GdipGetStringFormatTabStops(GDIPCONST GpStringFormat *format, INT count,
166 REAL *firsttab, REAL *tabs)
167 {
168 if(!format || !firsttab || !tabs)
169 return InvalidParameter;
170
171 /* native simply crashes on count < 0 */
172 if(count != 0)
173 memcpy(tabs, format->tabs, sizeof(REAL)*count);
174
175 *firsttab = format->firsttab;
176
177 return Ok;
178 }
179
180 GpStatus WINGDIPAPI GdipGetStringFormatTrimming(GpStringFormat *format,
181 StringTrimming *trimming)
182 {
183 if(!format || !trimming)
184 return InvalidParameter;
185
186 *trimming = format->trimming;
187
188 return Ok;
189 }
190
191 GpStatus WINGDIPAPI GdipSetStringFormatAlign(GpStringFormat *format,
192 StringAlignment align)
193 {
194 TRACE("(%p, %i)\n", format, align);
195
196 if(!format)
197 return InvalidParameter;
198
199 format->align = align;
200
201 return Ok;
202 }
203
204 /*FIXME: digit substitution actually not implemented, get/set only */
205 GpStatus WINGDIPAPI GdipSetStringFormatDigitSubstitution(GpStringFormat *format,
206 LANGID language, StringDigitSubstitute substitute)
207 {
208 TRACE("(%p, %x, %i)\n", format, language, substitute);
209
210 if(!format)
211 return InvalidParameter;
212
213 format->digitlang = language;
214 format->digitsub = substitute;
215
216 return Ok;
217 }
218
219 GpStatus WINGDIPAPI GdipSetStringFormatHotkeyPrefix(GpStringFormat *format,
220 INT hkpx)
221 {
222 TRACE("(%p, %i)\n", format, hkpx);
223
224 if(!format || hkpx < 0 || hkpx > 2)
225 return InvalidParameter;
226
227 format->hkprefix = (HotkeyPrefix) hkpx;
228
229 return Ok;
230 }
231
232 GpStatus WINGDIPAPI GdipSetStringFormatLineAlign(GpStringFormat *format,
233 StringAlignment align)
234 {
235 TRACE("(%p, %i)\n", format, align);
236
237 if(!format)
238 return InvalidParameter;
239
240 format->vertalign = align;
241
242 return Ok;
243 }
244
245 GpStatus WINGDIPAPI GdipSetStringFormatMeasurableCharacterRanges(GpStringFormat*
246 format, INT rangeCount, GDIPCONST CharacterRange* ranges)
247 {
248 if (!(format && rangeCount && ranges))
249 return InvalidParameter;
250
251 FIXME("stub: %p, %d, %p\n", format, rangeCount, ranges);
252
253 return NotImplemented;
254 }
255
256 GpStatus WINGDIPAPI GdipSetStringFormatTabStops(GpStringFormat *format, REAL firsttab,
257 INT count, GDIPCONST REAL *tabs)
258 {
259 TRACE("(%p, %0.2f, %i, %p)\n", format, firsttab, count, tabs);
260
261 if(!format || !tabs)
262 return InvalidParameter;
263
264 if(count > 0){
265 if(firsttab < 0.0) return NotImplemented;
266 /* first time allocation */
267 if(format->tabcount == 0){
268 format->tabs = GdipAlloc(sizeof(REAL)*count);
269 if(!format->tabs)
270 return OutOfMemory;
271 }
272 /* reallocation */
273 if((format->tabcount < count) && (format->tabcount > 0)){
274 REAL *ptr;
275 ptr = HeapReAlloc(GetProcessHeap(), 0, format->tabs, sizeof(REAL)*count);
276 if(!ptr)
277 return OutOfMemory;
278 format->tabs = ptr;
279 }
280 format->firsttab = firsttab;
281 format->tabcount = count;
282 memcpy(format->tabs, tabs, sizeof(REAL)*count);
283 }
284
285 return Ok;
286 }
287
288 GpStatus WINGDIPAPI GdipSetStringFormatTrimming(GpStringFormat *format,
289 StringTrimming trimming)
290 {
291 TRACE("(%p, %i)\n", format, trimming);
292
293 if(!format)
294 return InvalidParameter;
295
296 format->trimming = trimming;
297
298 return Ok;
299 }
300
301 GpStatus WINGDIPAPI GdipSetStringFormatFlags(GpStringFormat *format, INT flags)
302 {
303 TRACE("(%p, %x)\n", format, flags);
304
305 if(!format)
306 return InvalidParameter;
307
308 format->attr = flags;
309
310 return Ok;
311 }
312
313 GpStatus WINGDIPAPI GdipCloneStringFormat(GDIPCONST GpStringFormat *format, GpStringFormat **newFormat)
314 {
315 if(!format || !newFormat)
316 return InvalidParameter;
317
318 *newFormat = GdipAlloc(sizeof(GpStringFormat));
319 if(!*newFormat) return OutOfMemory;
320
321 **newFormat = *format;
322
323 if(format->tabcount > 0){
324 (*newFormat)->tabs = GdipAlloc(sizeof(REAL) * format->tabcount);
325 if(!(*newFormat)->tabs){
326 GdipFree(*newFormat);
327 return OutOfMemory;
328 }
329 memcpy((*newFormat)->tabs, format->tabs, sizeof(REAL) * format->tabcount);
330 }
331 else
332 (*newFormat)->tabs = NULL;
333
334 TRACE("%p %p\n",format,newFormat);
335
336 return Ok;
337 }
338
339 GpStatus WINGDIPAPI GdipStringFormatGetGenericTypographic(GpStringFormat **format)
340 {
341 GpStatus stat;
342
343 if(!format)
344 return InvalidParameter;
345
346 stat = GdipCreateStringFormat(StringFormatFlagsNoFitBlackBox |
347 StringFormatFlagsLineLimit |
348 StringFormatFlagsNoClip, LANG_NEUTRAL, format);
349 if(stat != Ok)
350 return stat;
351
352 (*format)->digitlang = LANG_NEUTRAL;
353 (*format)->digitsub = StringDigitSubstituteUser;
354 (*format)->trimming = StringTrimmingNone;
355 (*format)->hkprefix = HotkeyPrefixNone;
356 (*format)->align = StringAlignmentNear;
357 (*format)->vertalign = StringAlignmentNear;
358
359 return Ok;
360 }