2 * Generate .res format from a resource-tree
4 * Copyright 1998 Bertho A. Stultiens
6 * 25-May-1998 BS - Added simple unicode -> char conversion for resource
7 * names in .s and .h files.
25 #define SetResSize(res, tag) *(DWORD *)&((res)->data[(tag)]) = \
26 (res)->size - *(DWORD *)&((res)->data[(tag)])
31 r
= (res_t
*)xmalloc(sizeof(res_t
));
32 r
->allocsize
= RES_BLOCKSIZE
;
34 r
->data
= (char *)xmalloc(RES_BLOCKSIZE
);
38 res_t
*grow_res(res_t
*r
, int add
)
41 r
->data
= (char *)xrealloc(r
->data
, r
->allocsize
);
46 *****************************************************************************
50 * Syntax : void put_byte(res_t *res, unsigned c)
51 * void put_word(res_t *res, unsigned w)
52 * void put_dword(res_t *res, unsigned d)
54 * res - Binary resource to put the data in
55 * c, w, d - Data to put
57 * Description : Put primitives that put an item in the binary resource.
58 * The data array grows automatically.
60 *****************************************************************************
62 void put_byte(res_t
*res
, unsigned c
)
64 if(res
->allocsize
- res
->size
< sizeof(char))
65 grow_res(res
, RES_BLOCKSIZE
);
66 *(char *)&(res
->data
[res
->size
]) = (char)c
;
67 res
->size
+= sizeof(char);
70 void put_word(res_t
*res
, unsigned w
)
72 if(res
->allocsize
- res
->size
< sizeof(WORD
))
73 grow_res(res
, RES_BLOCKSIZE
);
74 *(WORD
*)&(res
->data
[res
->size
]) = (WORD
)w
;
75 res
->size
+= sizeof(WORD
);
78 void put_dword(res_t
*res
, unsigned d
)
80 if(res
->allocsize
- res
->size
< sizeof(DWORD
))
81 grow_res(res
, RES_BLOCKSIZE
);
82 *(DWORD
*)&(res
->data
[res
->size
]) = (DWORD
)d
;
83 res
->size
+= sizeof(DWORD
);
86 void put_pad(res_t
*res
)
88 while(res
->size
& 0x3)
93 *****************************************************************************
94 * Function : string_to_upper
95 * Syntax : void string_to_upper(string_t *str)
99 * Remarks : FIXME: codepages...
100 *****************************************************************************
102 void string_to_upper(string_t
*str
)
104 if(str
->type
== str_char
)
106 char *cptr
= str
->str
.cstr
;
108 *cptr
= (char)toupper(*cptr
);
110 else if(str
->type
== str_unicode
)
112 short *sptr
= str
->str
.wstr
;
115 *sptr
= (short)toupper(*sptr
);
119 internal_error(__FILE__
, __LINE__
, "Invalid string type %d", str
->type
);
124 *****************************************************************************
125 * Function : put_string
126 * Syntax : void put_string(res_t *res, string_t *str, enum str_e type,
129 * res - Binary resource to put the data in
130 * str - String to put
131 * type - Data has to be written in either str_char or str_unicode
132 * isterm - The string is '\0' terminated (disregard the string's
137 *****************************************************************************
139 void put_string(res_t
*res
, string_t
*str
, enum str_e type
, int isterm
)
145 if(!isterm
&& str
->size
== 0)
147 warning("String length is zero, not written");
151 if(str
->type
== str_unicode
&& type
== str_unicode
)
153 for(cnt
= 0; cnt
< str
->size
; cnt
++)
155 c
= str
->str
.wstr
[cnt
];
160 if(isterm
&& (str
->size
== 0 || (cnt
== str
->size
&& c
)))
163 else if(str
->type
== str_char
&& type
== str_char
)
165 for(cnt
= 0; cnt
< str
->size
; cnt
++)
167 c
= str
->str
.cstr
[cnt
];
172 if(isterm
&& (str
->size
== 0 || (cnt
== str
->size
&& c
)))
175 else if(str
->type
== str_unicode
&& type
== str_char
)
177 for(cnt
= 0; cnt
< str
->size
; cnt
++)
179 c
= str
->str
.wstr
[cnt
];
184 if(isterm
&& (str
->size
== 0 || (cnt
== str
->size
&& c
)))
187 else /* str->type == str_char && type == str_unicode */
189 for(cnt
= 0; cnt
< str
->size
; cnt
++)
191 c
= str
->str
.cstr
[cnt
];
192 put_word(res
, c
& 0xff);
196 if(isterm
&& (str
->size
== 0 || (cnt
== str
->size
&& c
)))
202 *****************************************************************************
203 * Function : put_name_id
204 * Syntax : void put_name_id(res_t *res, name_id_t *nid, int upcase)
209 *****************************************************************************
211 void put_name_id(res_t
*res
, name_id_t
*nid
, int upcase
)
213 if(nid
->type
== name_ord
)
216 put_word(res
, 0xffff);
219 put_word(res
, (WORD
)nid
->name
.i_name
);
221 else if(nid
->type
== name_str
)
224 string_to_upper(nid
->name
.s_name
);
225 put_string(res
, nid
->name
.s_name
, win32
? str_unicode
: str_char
, TRUE
);
229 internal_error(__FILE__
, __LINE__
, "Invalid name_id type %d", nid
->type
);
234 *****************************************************************************
236 * Syntax : void put_lvc(res_t *res, lvc_t *lvc)
241 *****************************************************************************
243 void put_lvc(res_t
*res
, lvc_t
*lvc
)
245 if(lvc
&& lvc
->language
)
246 put_word(res
, MAKELANGID(lvc
->language
->id
, lvc
->language
->sub
));
248 put_word(res
, 0); /* Neutral */
249 if(lvc
&& lvc
->version
)
250 put_dword(res
, *(lvc
->version
));
253 if(lvc
&& lvc
->characts
)
254 put_dword(res
, *(lvc
->characts
));
260 *****************************************************************************
261 * Function : put_raw_data
262 * Syntax : void put_raw_data(res_t *res, raw_data_t *raw, int offset)
267 *****************************************************************************
269 void put_raw_data(res_t
*res
, raw_data_t
*raw
, int offset
)
271 int wsize
= raw
->size
- offset
;
272 if(res
->allocsize
- res
->size
< wsize
)
273 grow_res(res
, wsize
);
274 memcpy(&(res
->data
[res
->size
]), raw
->data
+ offset
, wsize
);
279 *****************************************************************************
280 * Function : put_res_header
281 * Syntax : intput_res_header(res_t *res, int type, name_id_t *ntype,
282 * name_id_t *name, DWORD memopt, lvc_t *lvc)
285 * res - Binary resource descriptor to write to
286 * type - Resource identifier (if ntype == NULL)
287 * ntype - Name id of type
288 * name - Resource's name
289 * memopt - Resource's memory options to write
290 * lvc - Language, version and characteristics (win32 only)
291 * Output : An index to the resource size field. The resource size field
292 * contains the header size upon exit.
295 *****************************************************************************
297 int put_res_header(res_t
*res
, int type
, name_id_t
*ntype
, name_id_t
*name
,
298 DWORD memopt
, lvc_t
*lvc
)
302 put_dword(res
, 0); /* We will overwrite these later */
306 put_word(res
, 0xffff); /* ResType */
310 put_name_id(res
, ntype
, TRUE
);
311 put_name_id(res
, name
, TRUE
); /* ResName */
313 put_dword(res
, 0); /* DataVersion */
314 put_word(res
, memopt
); /* Memory options */
315 put_lvc(res
, lvc
); /* Language, version and characts */
316 ((DWORD
*)res
->data
)[0] = res
->size
; /* Set preliminary resource */
317 ((DWORD
*)res
->data
)[1] = res
->size
; /* Set HeaderSize */
318 res
->dataidx
= res
->size
;
326 put_byte(res
, 0xff); /* ResType */
330 put_name_id(res
, ntype
, TRUE
);
331 put_name_id(res
, name
, TRUE
); /* ResName */
332 put_word(res
, memopt
); /* Memory options */
334 put_dword(res
, 0); /* ResSize overwritten later*/
335 *(DWORD
*)&(res
->data
[tag
]) = res
->size
;
336 res
->dataidx
= res
->size
;
342 *****************************************************************************
343 * Function : accelerator2res
344 * Syntax : res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
346 * name - Name/ordinal of the resource
347 * acc - The accelerator descriptor
348 * Output : New .res format structure
351 *****************************************************************************
353 res_t
*accelerator2res(name_id_t
*name
, accelerator_t
*acc
)
358 assert(name
!= NULL
);
365 restag
= put_res_header(res
, WRC_RT_ACCELERATOR
, NULL
, name
, acc
->memopt
, &(acc
->lvc
));
368 put_word(res
, ev
->flags
| (ev
->next
? 0 : 0x80));
369 put_word(res
, ev
->key
);
370 put_word(res
, ev
->id
);
371 put_word(res
, 0); /* Padding */
378 restag
= put_res_header(res
, WRC_RT_ACCELERATOR
, NULL
, name
, acc
->memopt
, NULL
);
381 put_byte(res
, ev
->flags
| (ev
->next
? 0 : 0x80));
382 put_word(res
, ev
->key
);
383 put_word(res
, ev
->id
);
387 /* Set ResourceSize */
388 SetResSize(res
, restag
);
393 *****************************************************************************
394 * Function : dialog2res
395 * Syntax : res_t *dialog2res(name_id_t *name, dialog_t *dlg)
397 * name - Name/ordinal of the resource
398 * dlg - The dialog descriptor
399 * Output : New .res format structure
402 *****************************************************************************
404 res_t
*dialog2res(name_id_t
*name
, dialog_t
*dlg
)
411 assert(name
!= NULL
);
414 ctrl
= dlg
->controls
;
418 restag
= put_res_header(res
, WRC_RT_DIALOG
, NULL
, name
, dlg
->memopt
, &(dlg
->lvc
));
420 put_dword(res
, dlg
->style
->or_mask
);
421 put_dword(res
, dlg
->gotexstyle
? dlg
->exstyle
->or_mask
: 0);
422 tag_nctrl
= res
->size
;
423 put_word(res
, 0); /* Number of controls */
424 put_word(res
, dlg
->x
);
425 put_word(res
, dlg
->y
);
426 put_word(res
, dlg
->width
);
427 put_word(res
, dlg
->height
);
429 put_name_id(res
, dlg
->menu
, TRUE
);
433 put_name_id(res
, dlg
->dlgclass
, TRUE
);
437 put_string(res
, dlg
->title
, str_unicode
, TRUE
);
442 put_word(res
, dlg
->font
->size
);
443 put_string(res
, dlg
->font
->name
, str_unicode
, TRUE
);
449 /* FIXME: what is default control style? */
450 put_dword(res
, ctrl
->gotstyle
? ctrl
->style
->or_mask
: WS_CHILD
);
451 put_dword(res
, ctrl
->gotexstyle
? ctrl
->exstyle
->or_mask
: 0);
452 put_word(res
, ctrl
->x
);
453 put_word(res
, ctrl
->y
);
454 put_word(res
, ctrl
->width
);
455 put_word(res
, ctrl
->height
);
456 put_word(res
, ctrl
->id
);
458 put_name_id(res
, ctrl
->ctlclass
, TRUE
);
460 internal_error(__FILE__
, __LINE__
, "Control has no control-class");
462 put_name_id(res
, ctrl
->title
, FALSE
);
467 put_word(res
, ctrl
->extra
->size
+2);
469 put_raw_data(res
, ctrl
->extra
, 0);
479 /* Set number of controls */
480 *(WORD
*)&((char *)res
->data
)[tag_nctrl
] = (WORD
)nctrl
;
484 restag
= put_res_header(res
, WRC_RT_DIALOG
, NULL
, name
, dlg
->memopt
, NULL
);
486 put_dword(res
, dlg
->gotstyle
? dlg
->style
->or_mask
: WS_POPUPWINDOW
);
487 tag_nctrl
= res
->size
;
488 put_byte(res
, 0); /* Number of controls */
489 put_word(res
, dlg
->x
);
490 put_word(res
, dlg
->y
);
491 put_word(res
, dlg
->width
);
492 put_word(res
, dlg
->height
);
494 put_name_id(res
, dlg
->menu
, TRUE
);
498 put_name_id(res
, dlg
->dlgclass
, TRUE
);
502 put_string(res
, dlg
->title
, str_char
, TRUE
);
507 put_word(res
, dlg
->font
->size
);
508 put_string(res
, dlg
->font
->name
, str_char
, TRUE
);
513 put_word(res
, ctrl
->x
);
514 put_word(res
, ctrl
->y
);
515 put_word(res
, ctrl
->width
);
516 put_word(res
, ctrl
->height
);
517 put_word(res
, ctrl
->id
);
518 put_dword(res
, ctrl
->gotstyle
? ctrl
->style
->or_mask
: WS_CHILD
);
521 if(ctrl
->ctlclass
->type
== name_ord
522 && ctrl
->ctlclass
->name
.i_name
>= 0x80
523 && ctrl
->ctlclass
->name
.i_name
<= 0x85)
524 put_byte(res
, ctrl
->ctlclass
->name
.i_name
);
525 else if(ctrl
->ctlclass
->type
== name_str
)
526 put_name_id(res
, ctrl
->ctlclass
, FALSE
);
528 error("Unknown control-class %04x", ctrl
->ctlclass
->name
.i_name
);
531 internal_error(__FILE__
, __LINE__
, "Control has no control-class");
533 put_name_id(res
, ctrl
->title
, FALSE
);
537 /* FIXME: What is this extra byte doing here? */
543 /* Set number of controls */
544 ((char *)res
->data
)[tag_nctrl
] = (char)nctrl
;
546 /* Set ResourceSize */
547 SetResSize(res
, restag
);
552 *****************************************************************************
553 * Function : dialogex2res
554 * Syntax : res_t *dialogex2res(name_id_t *name, dialogex_t *dlgex)
556 * name - Name/ordinal of the resource
557 * dlgex - The dialogex descriptor
558 * Output : New .res format structure
561 *****************************************************************************
563 res_t
*dialogex2res(name_id_t
*name
, dialogex_t
*dlgex
)
570 assert(name
!= NULL
);
571 assert(dlgex
!= NULL
);
573 ctrl
= dlgex
->controls
;
577 restag
= put_res_header(res
, WRC_RT_DIALOG
, NULL
, name
, dlgex
->memopt
, &(dlgex
->lvc
));
579 /* FIXME: MS doc says thet the first word must contain 0xffff
580 * and the second 0x0001 to signal a DLGTEMPLATEEX. Borland's
581 * compiler reverses the two words.
582 * I don't know which one to choose, but I write it as Mr. B
585 put_word(res
, 1); /* Signature */
586 put_word(res
, 0xffff); /* DlgVer */
587 put_dword(res
, dlgex
->gothelpid
? dlgex
->helpid
: 0);
588 put_dword(res
, dlgex
->gotexstyle
? dlgex
->exstyle
->or_mask
: 0);
589 put_dword(res
, dlgex
->gotstyle
? dlgex
->style
->or_mask
: WS_POPUPWINDOW
);
590 tag_nctrl
= res
->size
;
591 put_word(res
, 0); /* Number of controls */
592 put_word(res
, dlgex
->x
);
593 put_word(res
, dlgex
->y
);
594 put_word(res
, dlgex
->width
);
595 put_word(res
, dlgex
->height
);
597 put_name_id(res
, dlgex
->menu
, TRUE
);
601 put_name_id(res
, dlgex
->dlgclass
, TRUE
);
605 put_string(res
, dlgex
->title
, str_unicode
, TRUE
);
610 put_word(res
, dlgex
->font
->size
);
611 put_word(res
, dlgex
->font
->weight
);
612 /* FIXME: ? TRUE should be sufficient to say that its
613 * italic, but Borland's compiler says its 0x0101.
614 * I just copy it here, and hope for the best.
616 put_word(res
, dlgex
->font
->italic
? 0x0101 : 0);
617 put_string(res
, dlgex
->font
->name
, str_unicode
, TRUE
);
623 put_dword(res
, ctrl
->gothelpid
? ctrl
->helpid
: 0);
624 put_dword(res
, ctrl
->gotexstyle
? ctrl
->exstyle
->or_mask
: 0);
625 /* FIXME: what is default control style? */
626 put_dword(res
, ctrl
->gotstyle
? ctrl
->style
->or_mask
: WS_CHILD
| WS_VISIBLE
);
627 put_word(res
, ctrl
->x
);
628 put_word(res
, ctrl
->y
);
629 put_word(res
, ctrl
->width
);
630 put_word(res
, ctrl
->height
);
631 put_word(res
, ctrl
->id
);
632 /* FIXME: Pad is _NOT_ documented!?! */
635 put_name_id(res
, ctrl
->ctlclass
, TRUE
);
637 internal_error(__FILE__
, __LINE__
, "Control has no control-class");
639 put_name_id(res
, ctrl
->title
, FALSE
);
645 put_word(res
, ctrl
->extra
->size
);
646 put_raw_data(res
, ctrl
->extra
, 0);
655 /* Set number of controls */
656 *(WORD
*)&((char *)res
->data
)[tag_nctrl
] = (WORD
)nctrl
;
657 /* Set ResourceSize */
658 SetResSize(res
, restag
);
663 /* Do not generate anything in 16-bit mode */
672 *****************************************************************************
673 * Function : menuitem2res
674 * Syntax : void menuitem2res(res_t *res, menu_item_t *item)
678 * Remarks : Self recursive
679 *****************************************************************************
681 void menuitem2res(res_t
*res
, menu_item_t
*menitem
)
683 menu_item_t
*itm
= menitem
;
688 put_word(res
, itm
->state
| (itm
->popup
? MF_POPUP
: 0) | (!itm
->next
? MF_END
: 0));
690 put_word(res
, itm
->id
);
692 put_string(res
, itm
->name
, str_unicode
, TRUE
);
696 menuitem2res(res
, itm
->popup
);
704 put_word(res
, itm
->state
| (itm
->popup
? MF_POPUP
: 0) | (!itm
->next
? MF_END
: 0));
706 put_word(res
, itm
->id
);
708 put_string(res
, itm
->name
, str_char
, TRUE
);
712 menuitem2res(res
, itm
->popup
);
720 *****************************************************************************
721 * Function : menu2res
722 * Syntax : res_t *menu2res(name_id_t *name, menu_t *men)
724 * name - Name/ordinal of the resource
725 * men - The menu descriptor
726 * Output : New .res format structure
729 *****************************************************************************
731 res_t
*menu2res(name_id_t
*name
, menu_t
*men
)
735 assert(name
!= NULL
);
739 restag
= put_res_header(res
, WRC_RT_MENU
, NULL
, name
, men
->memopt
, win32
? &(men
->lvc
) : NULL
);
741 put_dword(res
, 0); /* Menuheader: Version and HeaderSize */
742 menuitem2res(res
, men
->items
);
743 /* Set ResourceSize */
744 SetResSize(res
, restag
);
751 *****************************************************************************
752 * Function : menuexitem2res
753 * Syntax : void menuexitem2res(res_t *res, menuex_item_t *item)
757 * Remarks : Self recursive
758 *****************************************************************************
760 void menuexitem2res(res_t
*res
, menuex_item_t
*menitem
)
762 menuex_item_t
*itm
= menitem
;
766 put_dword(res
, itm
->gottype
? itm
->type
: 0);
767 put_dword(res
, itm
->gotstate
? itm
->state
: 0);
768 put_dword(res
, itm
->gotid
? itm
->id
: 0); /* FIXME: Docu. says word */
769 put_word(res
, (itm
->popup
? 0x01 : 0) | (!itm
->next
? MF_END
: 0));
771 put_string(res
, itm
->name
, str_unicode
, TRUE
);
777 put_dword(res
, itm
->gothelpid
? itm
->helpid
: 0);
778 menuexitem2res(res
, itm
->popup
);
786 *****************************************************************************
787 * Function : menuex2res
788 * Syntax : res_t *menuex2res(name_id_t *name, menuex_t *menex)
790 * name - Name/ordinal of the resource
791 * menex - The menuex descriptor
792 * Output : New .res format structure
795 *****************************************************************************
797 res_t
*menuex2res(name_id_t
*name
, menuex_t
*menex
)
801 assert(name
!= NULL
);
802 assert(menex
!= NULL
);
807 restag
= put_res_header(res
, WRC_RT_MENU
, NULL
, name
, menex
->memopt
, &(menex
->lvc
));
809 put_word(res
, 1); /* Menuheader: Version */
810 put_word(res
, 4); /* Offset */
811 put_dword(res
, 0); /* HelpId */
813 menuexitem2res(res
, menex
->items
);
814 /* Set ResourceSize */
815 SetResSize(res
, restag
);
820 /* Do not generate anything in 16-bit mode */
829 *****************************************************************************
830 * Function : cursorgroup2res
831 * Syntax : res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
833 * name - Name/ordinal of the resource
834 * curg - The cursor descriptor
835 * Output : New .res format structure
838 *****************************************************************************
840 res_t
*cursorgroup2res(name_id_t
*name
, cursor_group_t
*curg
)
845 assert(name
!= NULL
);
846 assert(curg
!= NULL
);
849 restag
= put_res_header(res
, WRC_RT_GROUP_CURSOR
, NULL
, name
, curg
->memopt
, &(curg
->lvc
));
852 put_word(res
, 0); /* Reserved */
853 /* FIXME: The ResType in the NEWHEADER structure should
854 * contain 14 according to the MS win32 doc. This is
855 * not the case with the BRC compiler and I really doubt
856 * the latter. Putting one here is compliant to win16 spec,
857 * but who knows the true value?
859 put_word(res
, 2); /* ResType */
860 put_word(res
, curg
->ncursor
);
862 for(cur
= curg
->cursorlist
; cur
; cur
= cur
->next
)
864 cur
= curg
->cursorlist
;
867 for(; cur
; cur
= cur
->prev
)
870 put_word(res
, cur
->width
);
871 /* FIXME: The height of a cursor is half the size of
872 * the bitmap's height. BRC puts the height from the
873 * BITMAPINFOHEADER here instead of the cursorfile's
874 * height. MS doesn't seem to care...
876 put_word(res
, cur
->height
);
877 /* FIXME: The next two are reversed in BRC and I don't
878 * know why. Probably a bug. But, we can safely ignore
879 * it because win16 does not support color cursors.
880 * A warning should have been generated by the parser.
882 put_word(res
, cur
->planes
);
883 put_word(res
, cur
->bits
);
884 /* FIXME: The +4 is the hotspot in the cursor resource.
885 * However, I cound not find this in the documentation.
886 * The hotspot bytes must either be included or MS
889 put_dword(res
, cur
->data
->size
+4);
890 put_word(res
, cur
->id
);
895 put_word(res
, 0); /* Reserved */
896 put_word(res
, 2); /* ResType */
897 put_word(res
, curg
->ncursor
);
899 for(cur
= curg
->cursorlist
; cur
; cur
= cur
->next
)
901 cur
= curg
->cursorlist
;
904 for(; cur
; cur
= cur
->prev
)
907 put_word(res
, cur
->width
);
908 /* FIXME: The height of a cursor is half the size of
909 * the bitmap's height. BRC puts the height from the
910 * BITMAPINFOHEADER here instead of the cursorfile's
911 * height. MS doesn't seem to care...
913 put_word(res
, cur
->height
);
914 /* FIXME: The next two are reversed in BRC and I don't
915 * know why. Probably a bug. But, we can safely ignore
916 * it because win16 does not support color cursors.
917 * A warning should have been generated by the parser.
919 put_word(res
, cur
->planes
);
920 put_word(res
, cur
->bits
);
921 /* FIXME: The +4 is the hotspot in the cursor resource.
922 * However, I cound not find this in the documentation.
923 * The hotspot bytes must either be included or MS
926 put_dword(res
, cur
->data
->size
+4);
927 put_word(res
, cur
->id
);
930 SetResSize(res
, restag
); /* Set ResourceSize */
938 *****************************************************************************
939 * Function : cursor2res
940 * Syntax : res_t *cursor2res(cursor_t *cur)
942 * cur - The cursor descriptor
943 * Output : New .res format structure
946 *****************************************************************************
948 res_t
*cursor2res(cursor_t
*cur
)
957 name
.type
= name_ord
;
958 name
.name
.i_name
= cur
->id
;
959 restag
= put_res_header(res
, WRC_RT_CURSOR
, NULL
, &name
, WRC_MO_MOVEABLE
| WRC_MO_DISCARDABLE
, &(cur
->lvc
));
960 put_word(res
, cur
->xhot
);
961 put_word(res
, cur
->yhot
);
962 put_raw_data(res
, cur
->data
, 0);
964 SetResSize(res
, restag
); /* Set ResourceSize */
972 *****************************************************************************
973 * Function : icongroup2res
974 * Syntax : res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
976 * name - Name/ordinal of the resource
977 * icog - The icon group descriptor
978 * Output : New .res format structure
981 *****************************************************************************
983 res_t
*icongroup2res(name_id_t
*name
, icon_group_t
*icog
)
988 assert(name
!= NULL
);
989 assert(icog
!= NULL
);
992 restag
= put_res_header(res
, WRC_RT_GROUP_ICON
, NULL
, name
, icog
->memopt
, &(icog
->lvc
));
995 put_word(res
, 0); /* Reserved */
996 /* FIXME: The ResType in the NEWHEADER structure should
997 * contain 14 according to the MS win32 doc. This is
998 * not the case with the BRC compiler and I really doubt
999 * the latter. Putting one here is compliant to win16 spec,
1000 * but who knows the true value?
1002 put_word(res
, 1); /* ResType */
1003 put_word(res
, icog
->nicon
);
1004 for(ico
= icog
->iconlist
; ico
; ico
= ico
->next
)
1006 put_byte(res
, ico
->width
);
1007 put_byte(res
, ico
->height
);
1008 put_byte(res
, ico
->nclr
);
1009 put_byte(res
, 0); /* Reserved */
1010 put_word(res
, ico
->planes
);
1011 put_word(res
, ico
->bits
);
1012 put_dword(res
, ico
->data
->size
);
1013 put_word(res
, ico
->id
);
1018 put_word(res
, 0); /* Reserved */
1019 put_word(res
, 1); /* ResType */
1020 put_word(res
, icog
->nicon
);
1021 for(ico
= icog
->iconlist
; ico
; ico
= ico
->next
)
1023 put_byte(res
, ico
->width
);
1024 put_byte(res
, ico
->height
);
1025 put_byte(res
, ico
->nclr
);
1026 put_byte(res
, 0); /* Reserved */
1027 put_word(res
, ico
->planes
);
1028 put_word(res
, ico
->bits
);
1029 put_dword(res
, ico
->data
->size
);
1030 put_word(res
, ico
->id
);
1033 SetResSize(res
, restag
); /* Set ResourceSize */
1041 *****************************************************************************
1042 * Function : icon2res
1043 * Syntax : res_t *icon2res(icon_t *ico)
1045 * ico - The icon descriptor
1046 * Output : New .res format structure
1049 *****************************************************************************
1051 res_t
*icon2res(icon_t
*ico
)
1057 assert(ico
!= NULL
);
1060 name
.type
= name_ord
;
1061 name
.name
.i_name
= ico
->id
;
1062 restag
= put_res_header(res
, WRC_RT_ICON
, NULL
, &name
, WRC_MO_MOVEABLE
| WRC_MO_DISCARDABLE
, &(ico
->lvc
));
1063 put_raw_data(res
, ico
->data
, 0);
1065 SetResSize(res
, restag
); /* Set ResourceSize */
1073 *****************************************************************************
1074 * Function : bitmap2res
1075 * Syntax : res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1077 * name - Name/ordinal of the resource
1078 * bmp - The bitmap descriptor
1079 * Output : New .res format structure
1082 *****************************************************************************
1084 res_t
*bitmap2res(name_id_t
*name
, bitmap_t
*bmp
)
1088 assert(name
!= NULL
);
1089 assert(bmp
!= NULL
);
1094 restag
= put_res_header(res
, WRC_RT_BITMAP
, NULL
, name
, bmp
->memopt
, NULL
);
1096 if(bmp
->data
->data
[0] == 'B'
1097 && bmp
->data
->data
[1] == 'M'
1098 && ((BITMAPFILEHEADER
*)bmp
->data
->data
)->bfSize
== bmp
->data
->size
1099 && bmp
->data
->size
>= sizeof(BITMAPFILEHEADER
))
1101 /* The File header is still attached, don't write it */
1102 put_raw_data(res
, bmp
->data
, sizeof(BITMAPFILEHEADER
));
1106 put_raw_data(res
, bmp
->data
, 0);
1109 /* Set ResourceSize */
1110 SetResSize(res
, restag
);
1119 *****************************************************************************
1120 * Function : font2res
1121 * Syntax : res_t *font2res(name_id_t *name, font_t *fnt)
1123 * name - Name/ordinal of the resource
1124 * fnt - The font descriptor
1125 * Output : New .res format structure
1128 *****************************************************************************
1130 res_t
*font2res(name_id_t
*name
, font_t
*fnt
)
1132 assert(name
!= NULL
);
1133 assert(fnt
!= NULL
);
1134 warning("Fonts not yet implemented");
1139 *****************************************************************************
1140 * Function : rcdata2res
1141 * Syntax : res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1143 * name - Name/ordinal of the resource
1144 * rdt - The rcdata descriptor
1145 * Output : New .res format structure
1148 *****************************************************************************
1150 res_t
*rcdata2res(name_id_t
*name
, rcdata_t
*rdt
)
1154 assert(name
!= NULL
);
1155 assert(rdt
!= NULL
);
1158 restag
= put_res_header(res
, WRC_RT_RCDATA
, NULL
, name
, rdt
->memopt
, NULL
);
1159 put_raw_data(res
, rdt
->data
, 0);
1160 /* Set ResourceSize */
1161 SetResSize(res
, restag
);
1168 *****************************************************************************
1169 * Function : messagetable2res
1170 * Syntax : res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1172 * name - Name/ordinal of the resource
1173 * msg - The messagetable descriptor
1174 * Output : New .res format structure
1177 *****************************************************************************
1179 res_t
*messagetable2res(name_id_t
*name
, messagetable_t
*msg
)
1181 assert(name
!= NULL
);
1182 assert(msg
!= NULL
);
1183 warning("Messagetable not yet implemented");
1188 *****************************************************************************
1189 * Function : stringtable2res
1190 * Syntax : res_t *stringtable2res(stringtable_t *stt)
1192 * stt - The stringtable descriptor
1193 * Output : New .res format structure
1196 *****************************************************************************
1198 res_t
*stringtable2res(stringtable_t
*stt
)
1206 assert(stt
!= NULL
);
1209 for(; stt
; stt
= stt
->next
)
1213 warning("Empty internal stringtable");
1216 name
.type
= name_ord
;
1217 name
.name
.i_name
= (stt
->idbase
>> 4) + 1;
1218 restag
= put_res_header(res
, WRC_RT_STRING
, NULL
, &name
, stt
->memopt
, win32
? &(stt
->lvc
) : NULL
);
1219 for(i
= 0; i
< stt
->nentries
; i
++)
1221 if(stt
->entries
[i
].str
)
1224 put_word(res
, stt
->entries
[i
].str
->size
);
1226 put_byte(res
, stt
->entries
[i
].str
->size
);
1227 put_string(res
, stt
->entries
[i
].str
, win32
? str_unicode
: str_char
, FALSE
);
1237 /* Set ResourceSize */
1238 SetResSize(res
, restag
- lastsize
);
1241 lastsize
= res
->size
;
1247 *****************************************************************************
1248 * Function : user2res
1249 * Syntax : res_t *user2res(name_id_t *name, user_t *usr)
1251 * name - Name/ordinal of the resource
1252 * usr - The userresource descriptor
1253 * Output : New .res format structure
1256 *****************************************************************************
1258 res_t
*user2res(name_id_t
*name
, user_t
*usr
)
1262 assert(name
!= NULL
);
1263 assert(usr
!= NULL
);
1266 restag
= put_res_header(res
, 0, usr
->type
, name
, usr
->memopt
, NULL
);
1267 put_raw_data(res
, usr
->data
, 0);
1268 /* Set ResourceSize */
1269 SetResSize(res
, restag
);
1276 *****************************************************************************
1277 * Function : versionblock2res
1278 * Syntax : void versionblock2res(res_t *res, ver_block_t *blk)
1280 * res - Binary resource to write to
1281 * blk - The version block to be written
1284 * Remarks : Self recursive
1285 *****************************************************************************
1287 void versionblock2res(res_t
*res
, ver_block_t
*blk
, int level
)
1296 blksizetag
= res
->size
;
1297 put_word(res
, 0); /* Will be overwritten later */
1300 put_word(res
, 0); /* level ? */
1301 put_string(res
, blk
->name
, win32
? str_unicode
: str_char
, TRUE
);
1303 for(val
= blk
->values
; val
; val
= val
->next
)
1305 if(val
->type
== val_str
)
1307 valblksizetag
= res
->size
;
1308 put_word(res
, 0); /* Will be overwritten later */
1309 valvalsizetag
= res
->size
;
1310 put_word(res
, 0); /* Will be overwritten later */
1313 put_word(res
, level
);
1315 put_string(res
, val
->key
, win32
? str_unicode
: str_char
, TRUE
);
1318 put_string(res
, val
->value
.str
, win32
? str_unicode
: str_char
, TRUE
);
1320 *(WORD
*)&(res
->data
[valvalsizetag
]) = (WORD
)((res
->size
- tag
) >> 1);
1322 *(WORD
*)&(res
->data
[valvalsizetag
]) = (WORD
)(res
->size
- tag
);
1323 *(WORD
*)&(res
->data
[valblksizetag
]) = (WORD
)(res
->size
- valblksizetag
);
1326 else if(val
->type
== val_words
)
1328 valblksizetag
= res
->size
;
1329 put_word(res
, 0); /* Will be overwritten later */
1330 valvalsizetag
= res
->size
;
1331 put_word(res
, 0); /* Will be overwritten later */
1334 put_word(res
, level
);
1336 put_string(res
, val
->key
, win32
? str_unicode
: str_char
, TRUE
);
1339 for(i
= 0; i
< val
->value
.words
->nwords
; i
++)
1341 put_word(res
, val
->value
.words
->words
[i
]);
1343 *(WORD
*)&(res
->data
[valvalsizetag
]) = (WORD
)(res
->size
- tag
);
1344 *(WORD
*)&(res
->data
[valblksizetag
]) = (WORD
)(res
->size
- valblksizetag
);
1347 else if(val
->type
== val_block
)
1349 versionblock2res(res
, val
->value
.block
, level
+1);
1353 internal_error(__FILE__
, __LINE__
, "Invalid value indicator %d in VERSIONINFO", val
->type
);
1358 *(WORD
*)&(res
->data
[blksizetag
]) = (WORD
)(res
->size
- blksizetag
);
1362 *****************************************************************************
1363 * Function : versioninfo2res
1364 * Syntax : res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1366 * name - Name/ordinal of the resource
1367 * ver - The versioninfo descriptor
1368 * Output : New .res format structure
1371 *****************************************************************************
1373 res_t
*versioninfo2res(name_id_t
*name
, versioninfo_t
*ver
)
1376 int rootblocksizetag
;
1383 assert(name
!= NULL
);
1384 assert(ver
!= NULL
);
1386 vsvi
.type
= str_char
;
1387 vsvi
.str
.cstr
= "VS_VERSION_INFO";
1388 vsvi
.size
= 15; /* Excl. termination */
1391 restag
= put_res_header(res
, WRC_RT_VERSION
, NULL
, name
, WRC_MO_MOVEABLE
| WRC_MO_PURE
, NULL
);
1392 rootblocksizetag
= res
->size
;
1393 put_word(res
, 0); /* BlockSize filled in later */
1394 valsizetag
= res
->size
;
1395 put_word(res
, 0); /* ValueSize filled in later*/
1397 put_word(res
, 0); /* Tree-level ? */
1398 put_string(res
, &vsvi
, win32
? str_unicode
: str_char
, TRUE
);
1402 put_dword(res
, VS_FFI_SIGNATURE
);
1403 put_dword(res
, VS_FFI_STRUCVERSION
);
1404 put_dword(res
, (ver
->filever_maj1
<< 16) + (ver
->filever_maj2
& 0xffff));
1405 put_dword(res
, (ver
->filever_min1
<< 16) + (ver
->filever_min2
& 0xffff));
1406 put_dword(res
, (ver
->prodver_maj1
<< 16) + (ver
->prodver_maj2
& 0xffff));
1407 put_dword(res
, (ver
->prodver_min1
<< 16) + (ver
->prodver_min2
& 0xffff));
1408 put_dword(res
, ver
->fileflagsmask
);
1409 put_dword(res
, ver
->fileflags
);
1410 put_dword(res
, ver
->fileos
);
1411 put_dword(res
, ver
->filetype
);
1412 put_dword(res
, ver
->filesubtype
);
1413 put_dword(res
, 0); /* FileDateMS */
1414 put_dword(res
, 0); /* FileDateLS */
1416 *(WORD
*)&(res
->data
[valsizetag
]) = (WORD
)(res
->size
- tag
);
1417 /* Descend into the blocks */
1418 for(blk
= ver
->blocks
; blk
; blk
= blk
->next
)
1419 versionblock2res(res
, blk
, 0);
1420 /* Set root block's size */
1421 *(WORD
*)&(res
->data
[rootblocksizetag
]) = (WORD
)(res
->size
- rootblocksizetag
);
1423 SetResSize(res
, restag
);
1431 *****************************************************************************
1432 * Function : toolbaritem2res
1433 * Syntax : void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1437 * Remarks : Self recursive
1438 *****************************************************************************
1440 void toolbaritem2res(res_t
*res
, toolbar_item_t
*tbitem
)
1442 toolbar_item_t
*itm
= tbitem
;
1446 put_word(res
, itm
->id
);
1453 *****************************************************************************
1454 * Function : toolbar2res
1455 * Syntax : res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1457 * name - Name/ordinal of the resource
1458 * toolbar - The toolbar descriptor
1459 * Output : New .res format structure
1462 *****************************************************************************
1464 res_t
*toolbar2res(name_id_t
*name
, toolbar_t
*toolbar
)
1468 assert(name
!= NULL
);
1469 assert(toolbar
!= NULL
);
1474 restag
= put_res_header(res
, WRC_RT_TOOLBAR
, NULL
, name
, toolbar
->memopt
, &(toolbar
->lvc
));
1476 put_word(res
, 1); /* Menuheader: Version */
1477 put_word(res
, toolbar
->button_width
); /* (in pixels?) */
1478 put_word(res
, toolbar
->button_height
); /* (in pixels?) */
1479 put_word(res
, toolbar
->nitems
);
1481 toolbaritem2res(res
, toolbar
->items
);
1482 /* Set ResourceSize */
1483 SetResSize(res
, restag
);
1488 /* Do not generate anything in 16-bit mode */
1497 *****************************************************************************
1498 * Function : dlginit2res
1499 * Syntax : res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1501 * name - Name/ordinal of the resource
1502 * rdt - The dlginit descriptor
1503 * Output : New .res format structure
1506 *****************************************************************************
1508 res_t
*dlginit2res(name_id_t
*name
, dlginit_t
*dit
)
1512 assert(name
!= NULL
);
1513 assert(dit
!= NULL
);
1516 restag
= put_res_header(res
, WRC_RT_DLGINIT
, NULL
, name
, dit
->memopt
, &(dit
->lvc
));
1517 put_raw_data(res
, dit
->data
, 0);
1518 /* Set ResourceSize */
1519 SetResSize(res
, restag
);
1526 *****************************************************************************
1527 * Function : prep_nid_for_label
1528 * Syntax : char *prep_nid_for_label(name_id_t *nid)
1531 * Description : Converts a resource name into the first 32 (or less)
1532 * characters of the name with conversions.
1534 *****************************************************************************
1536 #define MAXNAMELEN 32
1537 char *prep_nid_for_label(name_id_t
*nid
)
1539 static char buf
[MAXNAMELEN
+1];
1541 assert(nid
!= NULL
);
1543 if(nid
->type
== name_str
&& nid
->name
.s_name
->type
== str_unicode
)
1547 sptr
= nid
->name
.s_name
->str
.wstr
;
1549 for(i
= 0; *sptr
&& i
< MAXNAMELEN
; i
++)
1551 if((unsigned)*sptr
< 0x80 && isprint((char)*sptr
))
1554 warning("Resourcename (str_unicode) contain unprintable characters or invalid translation, ignored");
1558 else if(nid
->type
== name_str
&& nid
->name
.s_name
->type
== str_char
)
1562 cptr
= nid
->name
.s_name
->str
.cstr
;
1564 for(i
= 0; *cptr
&& i
< MAXNAMELEN
; i
++)
1566 if((unsigned)*cptr
< 0x80 && isprint(*cptr
))
1569 warning("Resourcename (str_char) contain unprintable characters, ignored");
1573 else if(nid
->type
== name_ord
)
1575 sprintf(buf
, "%u", nid
->name
.i_name
);
1579 internal_error(__FILE__
, __LINE__
, "Resource name_id with invalid type %d", nid
->type
);
1586 *****************************************************************************
1587 * Function : make_c_name
1588 * Syntax : char *make_c_name(char *base, name_id_t *nid, language_t *lan)
1591 * Description : Converts a resource name into a valid c-identifier in the
1594 *****************************************************************************
1596 char *make_c_name(char *base
, name_id_t
*nid
, language_t
*lan
)
1603 sprintf(lanbuf
, "%d", lan
? MAKELANGID(lan
->id
, lan
->sub
) : 0);
1604 buf
= prep_nid_for_label(nid
);
1605 nlen
= strlen(buf
) + strlen(lanbuf
);
1606 nlen
+= strlen(base
) + 4; /* three time '_' and '\0' */
1607 ret
= (char *)xmalloc(nlen
);
1613 strcat(ret
, lanbuf
);
1618 *****************************************************************************
1619 * Function : get_c_typename
1620 * Syntax : char *get_c_typename(enum res_e type)
1623 * Description : Convert resource enum to char string to be used in c-name
1626 *****************************************************************************
1628 char *get_c_typename(enum res_e type
)
1632 case res_acc
: return "Acc";
1633 case res_bmp
: return "Bmp";
1634 case res_cur
: return "Cur";
1635 case res_curg
: return "CurGrp";
1637 case res_dlgex
: return "Dlg";
1638 case res_fnt
: return "Fnt";
1639 case res_ico
: return "Ico";
1640 case res_icog
: return "IcoGrp";
1642 case res_menex
: return "Men";
1643 case res_rdt
: return "RCDat";
1644 case res_stt
: return "StrTab";
1645 case res_usr
: return "Usr";
1646 case res_msg
: return "MsgTab";
1647 case res_ver
: return "VerInf";
1648 case res_toolbar
: return "TlBr";
1649 case res_dlginit
: return "DlgInit";
1650 default: return "Oops";
1655 *****************************************************************************
1656 * Function : resources2res
1657 * Syntax : void resources2res(resource_t *top)
1659 * top - The resource-tree to convert
1661 * Description : Convert logical resource descriptors into binary data
1663 *****************************************************************************
1665 void resources2res(resource_t
*top
)
1673 top
->binres
= accelerator2res(top
->name
, top
->res
.acc
);
1677 top
->binres
= bitmap2res(top
->name
, top
->res
.bmp
);
1681 top
->binres
= cursor2res(top
->res
.cur
);
1685 top
->binres
= cursorgroup2res(top
->name
, top
->res
.curg
);
1689 top
->binres
= dialog2res(top
->name
, top
->res
.dlg
);
1693 top
->binres
= dialogex2res(top
->name
, top
->res
.dlgex
);
1697 top
->binres
= font2res(top
->name
, top
->res
.fnt
);
1701 top
->binres
= icon2res(top
->res
.ico
);
1705 top
->binres
= icongroup2res(top
->name
, top
->res
.icog
);
1709 top
->binres
= menu2res(top
->name
, top
->res
.men
);
1713 top
->binres
= menuex2res(top
->name
, top
->res
.menex
);
1717 top
->binres
= rcdata2res(top
->name
, top
->res
.rdt
);
1721 top
->binres
= stringtable2res(top
->res
.stt
);
1725 top
->binres
= user2res(top
->name
, top
->res
.usr
);
1729 top
->binres
= messagetable2res(top
->name
, top
->res
.msg
);
1733 top
->binres
= versioninfo2res(top
->name
, top
->res
.ver
);
1737 top
->binres
= toolbar2res(top
->name
, top
->res
.tbt
);
1741 top
->binres
= dlginit2res(top
->name
, top
->res
.dlgi
);
1745 internal_error(__FILE__
, __LINE__
, "Unknown resource type encountered %d in binary res generation", top
->type
);
1747 top
->c_name
= make_c_name(get_c_typename(top
->type
), top
->name
, top
->lan
);