2 * Create dynamic new structures of various types
3 * and some utils in that trend.
5 * Copyright 1998 Bertho A. Stultiens
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
36 #include "wingdi.h" /* for BITMAPINFOHEADER */
42 unsigned short biWidth
;
43 unsigned short biHeight
;
44 unsigned short biPlanes
;
45 unsigned short biBitCount
;
49 /* New instances for all types of structures */
50 /* Very inefficient (in size), but very functional :-]
51 * Especially for type-checking.
54 dialog_t
*new_dialog(void)
56 dialog_t
*ret
= xmalloc( sizeof(*ret
) );
57 memset( ret
, 0, sizeof(*ret
) );
61 name_id_t
*new_name_id(void)
63 name_id_t
*ret
= xmalloc( sizeof(*ret
) );
64 memset( ret
, 0, sizeof(*ret
) );
68 menu_t
*new_menu(void)
70 menu_t
*ret
= xmalloc( sizeof(*ret
) );
71 memset( ret
, 0, sizeof(*ret
) );
75 menu_item_t
*new_menu_item(void)
77 menu_item_t
*ret
= xmalloc( sizeof(*ret
) );
78 memset( ret
, 0, sizeof(*ret
) );
82 control_t
*new_control(void)
84 control_t
*ret
= xmalloc( sizeof(*ret
) );
85 memset( ret
, 0, sizeof(*ret
) );
89 icon_t
*new_icon(void)
91 icon_t
*ret
= xmalloc( sizeof(*ret
) );
92 memset( ret
, 0, sizeof(*ret
) );
96 cursor_t
*new_cursor(void)
98 cursor_t
*ret
= xmalloc( sizeof(*ret
) );
99 memset( ret
, 0, sizeof(*ret
) );
103 versioninfo_t
*new_versioninfo(void)
105 versioninfo_t
*ret
= xmalloc( sizeof(*ret
) );
106 memset( ret
, 0, sizeof(*ret
) );
110 ver_value_t
*new_ver_value(void)
112 ver_value_t
*ret
= xmalloc( sizeof(*ret
) );
113 memset( ret
, 0, sizeof(*ret
) );
117 ver_block_t
*new_ver_block(void)
119 ver_block_t
*ret
= xmalloc( sizeof(*ret
) );
120 memset( ret
, 0, sizeof(*ret
) );
124 stt_entry_t
*new_stt_entry(void)
126 stt_entry_t
*ret
= xmalloc( sizeof(*ret
) );
127 memset( ret
, 0, sizeof(*ret
) );
131 accelerator_t
*new_accelerator(void)
133 accelerator_t
*ret
= xmalloc( sizeof(*ret
) );
134 memset( ret
, 0, sizeof(*ret
) );
138 event_t
*new_event(void)
140 event_t
*ret
= xmalloc( sizeof(*ret
) );
141 memset( ret
, 0, sizeof(*ret
) );
145 raw_data_t
*new_raw_data(void)
147 raw_data_t
*ret
= xmalloc( sizeof(*ret
) );
148 memset( ret
, 0, sizeof(*ret
) );
154 lvc_t
*ret
= xmalloc( sizeof(*ret
) );
155 memset( ret
, 0, sizeof(*ret
) );
159 res_count_t
*new_res_count(void)
161 res_count_t
*ret
= xmalloc( sizeof(*ret
) );
162 memset( ret
, 0, sizeof(*ret
) );
166 string_t
*new_string(void)
168 string_t
*ret
= xmalloc( sizeof(*ret
) );
169 memset( ret
, 0, sizeof(*ret
) );
170 set_location( &ret
->loc
);
174 toolbar_item_t
*new_toolbar_item(void)
176 toolbar_item_t
*ret
= xmalloc( sizeof(*ret
) );
177 memset( ret
, 0, sizeof(*ret
) );
181 ani_any_t
*new_ani_any(void)
183 ani_any_t
*ret
= xmalloc( sizeof(*ret
) );
184 memset( ret
, 0, sizeof(*ret
) );
188 resource_t
*new_resource(enum res_e t
, void *res
, int memopt
, language_t lan
)
190 resource_t
*r
= xmalloc(sizeof(resource_t
));
191 memset( r
, 0, sizeof(*r
) );
193 r
->res
.overlay
= res
;
199 html_t
*new_html(raw_data_t
*rd
, int *memopt
)
201 html_t
*html
= xmalloc(sizeof(html_t
));
205 html
->memopt
= *memopt
;
209 html
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
;
213 rcdata_t
*new_rcdata(raw_data_t
*rd
, int *memopt
)
215 rcdata_t
*rc
= xmalloc(sizeof(rcdata_t
));
219 rc
->memopt
= *memopt
;
223 rc
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
;
227 font_id_t
*new_font_id(int size
, string_t
*face
, int weight
, int italic
)
229 font_id_t
*fid
= xmalloc(sizeof(font_id_t
));
232 fid
->weight
= weight
;
233 fid
->italic
= italic
;
237 user_t
*new_user(name_id_t
*type
, raw_data_t
*rd
, int *memopt
)
239 user_t
*usr
= xmalloc(sizeof(user_t
));
243 usr
->memopt
= *memopt
;
247 usr
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
;
252 font_t
*new_font(raw_data_t
*rd
, int *memopt
)
254 font_t
*fnt
= xmalloc(sizeof(font_t
));
258 fnt
->memopt
= *memopt
;
262 fnt
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_DISCARDABLE
;
266 fontdir_t
*new_fontdir(raw_data_t
*rd
, int *memopt
)
268 fontdir_t
*fnd
= xmalloc(sizeof(fontdir_t
));
272 fnd
->memopt
= *memopt
;
276 fnd
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_DISCARDABLE
;
281 static int convert_bitmap(char *data
, int size
)
283 if (size
> sizeof(BITMAPFILEHEADER
) && data
[0] == 'B' && data
[1] == 'M')
285 memmove(data
, data
+sizeof(BITMAPFILEHEADER
), size
- sizeof(BITMAPFILEHEADER
));
286 return sizeof(BITMAPFILEHEADER
);
292 * Cursor and icon splitter functions used when allocating
293 * cursor- and icon-groups.
300 static int get_new_id(id_alloc_t
**list
, int *n
, language_t lan
)
303 assert(list
!= NULL
);
308 *list
= xmalloc(sizeof(id_alloc_t
));
310 (*list
)[0].lan
= lan
;
315 for(i
= 0; i
< *n
; i
++)
317 if((*list
)[i
].lan
== lan
)
318 return ++((*list
)[i
].id
);
321 *list
= xrealloc(*list
, sizeof(id_alloc_t
) * (*n
+1));
322 (*list
)[*n
].lan
= lan
;
328 static int alloc_icon_id(language_t lan
)
330 static id_alloc_t
*idlist
= NULL
;
333 return get_new_id(&idlist
, &nid
, lan
);
336 static int alloc_cursor_id(language_t lan
)
338 static id_alloc_t
*idlist
= NULL
;
341 return get_new_id(&idlist
, &nid
, lan
);
344 static void split_icons(raw_data_t
*rd
, icon_group_t
*icog
, int *nico
)
350 icon_header_t
*ih
= (icon_header_t
*)rd
->data
;
352 if(GET_WORD(&ih
->type
) != 1)
353 parser_error("Icon resource data has invalid type id %d", ih
->type
);
355 cnt
= GET_WORD(&ih
->count
);
356 for(i
= 0; i
< cnt
; i
++)
358 icon_dir_entry_t ide
;
360 BITMAPINFOHEADER info
;
361 memcpy(&ide
, rd
->data
+ sizeof(icon_header_t
)
362 + i
*sizeof(icon_dir_entry_t
), sizeof(ide
));
365 ico
->id
= alloc_icon_id(icog
->lvc
.language
);
366 ico
->lvc
= icog
->lvc
;
367 offset
= GET_DWORD(&ide
.offset
);
368 size
= GET_DWORD(&ide
.ressize
);
369 if(offset
> rd
->size
|| offset
+ size
> rd
->size
)
370 parser_error("Icon resource data corrupt");
371 ico
->width
= ide
.width
;
372 ico
->height
= ide
.height
;
373 ico
->nclr
= ide
.nclr
;
374 ico
->planes
= GET_WORD(&ide
.planes
);
375 ico
->bits
= GET_WORD(&ide
.bits
);
376 memcpy(&info
, rd
->data
+ offset
, sizeof(info
));
377 convert_bitmap((char *) &info
, 0);
378 memcpy(rd
->data
+ offset
, &info
, sizeof(info
));
379 if(!ico
->planes
) ico
->planes
= GET_WORD(&info
.biPlanes
);
380 if(!ico
->bits
) ico
->bits
= GET_WORD(&info
.biBitCount
);
381 ico
->data
= new_raw_data();
382 copy_raw_data(ico
->data
, rd
, offset
, size
);
394 icog
->iconlist
= list
;
398 static void split_cursors(raw_data_t
*rd
, cursor_group_t
*curg
, int *ncur
)
403 cursor_t
*list
= NULL
;
404 cursor_header_t
*ch
= (cursor_header_t
*)rd
->data
;
406 if(GET_WORD(&ch
->type
) != 2) parser_error("Cursor resource data has invalid type id %d", ch
->type
);
407 cnt
= GET_WORD(&ch
->count
);
408 for(i
= 0; i
< cnt
; i
++)
410 cursor_dir_entry_t cde
;
412 BITMAPINFOHEADER info
;
413 memcpy(&cde
, rd
->data
+ sizeof(cursor_header_t
)
414 + i
*sizeof(cursor_dir_entry_t
), sizeof(cde
));
417 cur
->id
= alloc_cursor_id(curg
->lvc
.language
);
418 cur
->lvc
= curg
->lvc
;
419 offset
= GET_DWORD(&cde
.offset
);
420 size
= GET_DWORD(&cde
.ressize
);
421 if(offset
> rd
->size
|| offset
+ size
> rd
->size
)
422 parser_error("Cursor resource data corrupt");
423 cur
->width
= cde
.width
;
424 cur
->height
= cde
.height
;
425 cur
->nclr
= cde
.nclr
;
426 memcpy(&info
, rd
->data
+ offset
, sizeof(info
));
427 convert_bitmap((char *)&info
, 0);
428 memcpy(rd
->data
+ offset
, &info
, sizeof(info
));
429 cur
->planes
= GET_WORD(&info
.biPlanes
);
430 cur
->bits
= GET_WORD(&info
.biBitCount
);
431 if(!win32
&& (cur
->planes
!= 1 || cur
->bits
!= 1))
432 parser_warning("Win16 cursor contains colors\n");
433 cur
->xhot
= GET_WORD(&cde
.xhot
);
434 cur
->yhot
= GET_WORD(&cde
.yhot
);
435 cur
->data
= new_raw_data();
436 copy_raw_data(cur
->data
, rd
, offset
, size
);
448 curg
->cursorlist
= list
;
453 icon_group_t
*new_icon_group(raw_data_t
*rd
, int *memopt
)
455 icon_group_t
*icog
= xmalloc(sizeof(icon_group_t
));
458 icog
->memopt
= *memopt
;
462 icog
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
| WRC_MO_DISCARDABLE
;
464 split_icons(rd
, icog
, &(icog
->nicon
));
470 cursor_group_t
*new_cursor_group(raw_data_t
*rd
, int *memopt
)
472 cursor_group_t
*curg
= xmalloc(sizeof(cursor_group_t
));
475 curg
->memopt
= *memopt
;
479 curg
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
| WRC_MO_DISCARDABLE
;
481 split_cursors(rd
, curg
, &(curg
->ncursor
));
487 ani_curico_t
*new_ani_curico(enum res_e type
, raw_data_t
*rd
, int *memopt
)
489 ani_curico_t
*ani
= xmalloc(sizeof(ani_curico_t
));
491 assert(type
== res_anicur
|| type
== res_aniico
);
496 ani
->memopt
= *memopt
;
500 ani
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_DISCARDABLE
;
505 bitmap_t
*new_bitmap(raw_data_t
*rd
, int *memopt
)
507 bitmap_t
*bmp
= xmalloc(sizeof(bitmap_t
));
512 bmp
->memopt
= *memopt
;
516 bmp
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
;
517 rd
->size
-= convert_bitmap(rd
->data
, rd
->size
);
521 ver_words_t
*new_ver_words(int i
)
523 ver_words_t
*w
= xmalloc(sizeof(ver_words_t
));
524 w
->words
= xmalloc(sizeof(unsigned short));
530 ver_words_t
*add_ver_words(ver_words_t
*w
, int i
)
532 w
->words
= xrealloc(w
->words
, (w
->nwords
+1) * sizeof(unsigned short));
533 w
->words
[w
->nwords
] = i
;
538 #define MSGTAB_BAD_PTR(p, b, l, r) (((l) - ((char *)(p) - (char *)(b))) > (r))
539 messagetable_t
*new_messagetable(raw_data_t
*rd
, int *memopt
)
541 messagetable_t
*msg
= xmalloc(sizeof(messagetable_t
));
546 msg
->memopt
= *memopt
;
550 msg
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
;
552 if(rd
->size
< sizeof(unsigned int))
553 parser_error("Invalid messagetable, size too small");
557 #undef MSGTAB_BAD_PTR
559 void copy_raw_data(raw_data_t
*dst
, raw_data_t
*src
, unsigned int offs
, int len
)
561 assert(offs
<= src
->size
);
562 assert(offs
+ len
<= src
->size
);
565 dst
->data
= xmalloc(len
);
569 dst
->data
= xrealloc(dst
->data
, dst
->size
+ len
);
570 /* dst->size holds the offset to copy to */
571 memcpy(dst
->data
+ dst
->size
, src
->data
+ offs
, len
);
577 int *ip
= xmalloc(sizeof(int));
582 stringtable_t
*new_stringtable(lvc_t
*lvc
)
584 stringtable_t
*stt
= xmalloc(sizeof(stringtable_t
));
586 memset( stt
, 0, sizeof(*stt
) );
593 toolbar_t
*new_toolbar(int button_width
, int button_height
, toolbar_item_t
*items
, int nitems
)
595 toolbar_t
*tb
= xmalloc(sizeof(toolbar_t
));
596 memset( tb
, 0, sizeof(*tb
) );
597 tb
->button_width
= button_width
;
598 tb
->button_height
= button_height
;
604 dlginit_t
*new_dlginit(raw_data_t
*rd
, int *memopt
)
606 dlginit_t
*di
= xmalloc(sizeof(dlginit_t
));
610 di
->memopt
= *memopt
;
614 di
->memopt
= WRC_MO_MOVEABLE
| WRC_MO_PURE
| WRC_MO_DISCARDABLE
;
619 style_pair_t
*new_style_pair(style_t
*style
, style_t
*exstyle
)
621 style_pair_t
*sp
= xmalloc(sizeof(style_pair_t
));
623 sp
->exstyle
= exstyle
;
627 style_t
*new_style(unsigned int or_mask
, unsigned int and_mask
)
629 style_t
*st
= xmalloc(sizeof(style_t
));
630 st
->or_mask
= or_mask
;
631 st
->and_mask
= and_mask
;