wrc: Add support for translating accelerators through po files.
[wine/testsucceed.git] / tools / wrc / genres.c
blobd6e641fc4f4c4606bab16409324d76122bd48c56
1 /*
2 * Generate .res format from a resource-tree
4 * Copyright 1998 Bertho A. Stultiens
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * History:
21 * 05-May-2000 BS - Added code to support endian conversions. The
22 * extra functions also aid unaligned access, but
23 * this is not yet implemented.
24 * 25-May-1998 BS - Added simple unicode -> char conversion for resource
25 * names in .s and .h files.
28 #include "config.h"
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdarg.h>
34 #include <assert.h>
35 #include <ctype.h>
37 #include "wrc.h"
38 #include "genres.h"
39 #include "utils.h"
40 #include "windef.h"
41 #include "winbase.h"
42 #include "wingdi.h"
43 #include "winuser.h"
44 #include "wine/unicode.h"
46 #define SetResSize(res, tag) set_dword((res), (tag), (res)->size - get_dword((res), (tag)))
48 res_t *new_res(void)
50 res_t *r;
51 r = xmalloc(sizeof(res_t));
52 r->allocsize = RES_BLOCKSIZE;
53 r->size = 0;
54 r->dataidx = 0;
55 r->data = xmalloc(RES_BLOCKSIZE);
56 return r;
59 res_t *grow_res(res_t *r, unsigned int add)
61 r->allocsize += add;
62 r->data = xrealloc(r->data, r->allocsize);
63 return r;
67 *****************************************************************************
68 * Function : put_byte
69 * put_word
70 * put_dword
71 * Syntax : void put_byte(res_t *res, unsigned c)
72 * void put_word(res_t *res, unsigned w)
73 * void put_dword(res_t *res, unsigned d)
74 * Input :
75 * res - Binary resource to put the data in
76 * c, w, d - Data to put
77 * Output : nop
78 * Description : Put primitives that put an item in the binary resource.
79 * The data array grows automatically.
80 * Remarks :
81 *****************************************************************************
83 void put_byte(res_t *res, unsigned c)
85 if(res->allocsize - res->size < sizeof(char))
86 grow_res(res, RES_BLOCKSIZE);
87 res->data[res->size] = (char)c;
88 res->size += sizeof(char);
91 void put_word(res_t *res, unsigned w)
93 if(res->allocsize - res->size < sizeof(WORD))
94 grow_res(res, RES_BLOCKSIZE);
95 switch(byteorder)
97 #ifdef WORDS_BIGENDIAN
98 default:
99 #endif
100 case WRC_BO_BIG:
101 res->data[res->size+0] = HIBYTE(w);
102 res->data[res->size+1] = LOBYTE(w);
103 break;
105 #ifndef WORDS_BIGENDIAN
106 default:
107 #endif
108 case WRC_BO_LITTLE:
109 res->data[res->size+1] = HIBYTE(w);
110 res->data[res->size+0] = LOBYTE(w);
111 break;
113 res->size += sizeof(WORD);
116 void put_dword(res_t *res, unsigned d)
118 if(res->allocsize - res->size < sizeof(DWORD))
119 grow_res(res, RES_BLOCKSIZE);
120 switch(byteorder)
122 #ifdef WORDS_BIGENDIAN
123 default:
124 #endif
125 case WRC_BO_BIG:
126 res->data[res->size+0] = HIBYTE(HIWORD(d));
127 res->data[res->size+1] = LOBYTE(HIWORD(d));
128 res->data[res->size+2] = HIBYTE(LOWORD(d));
129 res->data[res->size+3] = LOBYTE(LOWORD(d));
130 break;
132 #ifndef WORDS_BIGENDIAN
133 default:
134 #endif
135 case WRC_BO_LITTLE:
136 res->data[res->size+3] = HIBYTE(HIWORD(d));
137 res->data[res->size+2] = LOBYTE(HIWORD(d));
138 res->data[res->size+1] = HIBYTE(LOWORD(d));
139 res->data[res->size+0] = LOBYTE(LOWORD(d));
140 break;
142 res->size += sizeof(DWORD);
145 static void put_pad(res_t *res)
147 while(res->size & 0x3)
148 put_byte(res, 0);
152 *****************************************************************************
153 * Function : set_word
154 * set_dword
155 * Syntax : void set_word(res_t *res, int ofs, unsigned w)
156 * void set_dword(res_t *res, int ofs, unsigned d)
157 * Input :
158 * res - Binary resource to put the data in
159 * ofs - Byte offset in data-array
160 * w, d - Data to put
161 * Output : nop
162 * Description : Set the value of a binary resource data array to a
163 * specific value.
164 * Remarks :
165 *****************************************************************************
167 static void set_word(res_t *res, int ofs, unsigned w)
169 switch(byteorder)
171 #ifdef WORDS_BIGENDIAN
172 default:
173 #endif
174 case WRC_BO_BIG:
175 res->data[ofs+0] = HIBYTE(w);
176 res->data[ofs+1] = LOBYTE(w);
177 break;
179 #ifndef WORDS_BIGENDIAN
180 default:
181 #endif
182 case WRC_BO_LITTLE:
183 res->data[ofs+1] = HIBYTE(w);
184 res->data[ofs+0] = LOBYTE(w);
185 break;
189 static void set_dword(res_t *res, int ofs, unsigned d)
191 switch(byteorder)
193 #ifdef WORDS_BIGENDIAN
194 default:
195 #endif
196 case WRC_BO_BIG:
197 res->data[ofs+0] = HIBYTE(HIWORD(d));
198 res->data[ofs+1] = LOBYTE(HIWORD(d));
199 res->data[ofs+2] = HIBYTE(LOWORD(d));
200 res->data[ofs+3] = LOBYTE(LOWORD(d));
201 break;
203 #ifndef WORDS_BIGENDIAN
204 default:
205 #endif
206 case WRC_BO_LITTLE:
207 res->data[ofs+3] = HIBYTE(HIWORD(d));
208 res->data[ofs+2] = LOBYTE(HIWORD(d));
209 res->data[ofs+1] = HIBYTE(LOWORD(d));
210 res->data[ofs+0] = LOBYTE(LOWORD(d));
211 break;
216 *****************************************************************************
217 * Function : get_dword
218 * Input :
219 * res - Binary resource to put the data in
220 * ofs - Byte offset in data-array
221 * Output : The data in native endian
222 * Description : Get the value of a binary resource data array in native
223 * endian.
224 * Remarks :
225 *****************************************************************************
227 static DWORD get_dword(res_t *res, int ofs)
229 switch(byteorder)
231 #ifdef WORDS_BIGENDIAN
232 default:
233 #endif
234 case WRC_BO_BIG:
235 return (res->data[ofs+0] << 24)
236 | (res->data[ofs+1] << 16)
237 | (res->data[ofs+2] << 8)
238 | res->data[ofs+3];
240 #ifndef WORDS_BIGENDIAN
241 default:
242 #endif
243 case WRC_BO_LITTLE:
244 return (res->data[ofs+3] << 24)
245 | (res->data[ofs+2] << 16)
246 | (res->data[ofs+1] << 8)
247 | res->data[ofs+0];
252 *****************************************************************************
253 * Function : string_to_upper
254 * Syntax : void string_to_upper(string_t *str)
255 * Input :
256 * Output :
257 * Description :
258 * Remarks : FIXME: codepages...
259 *****************************************************************************
261 static void string_to_upper(string_t *str)
263 int i;
265 if(str->type == str_char)
267 for (i = 0; i < str->size; i++) str->str.cstr[i] = toupper((unsigned char)str->str.cstr[i]);
269 else if(str->type == str_unicode)
271 for (i = 0; i < str->size; i++) str->str.wstr[i] = toupperW(str->str.wstr[i]);
273 else
275 internal_error(__FILE__, __LINE__, "Invalid string type %d\n", str->type);
279 static int parse_accel_string( const string_t *key, int flags )
281 int keycode;
283 if(key->type == str_char)
285 if((flags & WRC_AF_VIRTKEY) &&
286 !((key->str.cstr[0] >= 'A' && key->str.cstr[0] <= 'Z') ||
287 (key->str.cstr[0] >= '0' && key->str.cstr[0] <= '9')))
289 print_location( &key->loc );
290 error("VIRTKEY code is not equal to ascii value");
293 if(key->str.cstr[0] == '^' && (flags & WRC_AF_CONTROL) != 0)
295 print_location( &key->loc );
296 error("Cannot use both '^' and CONTROL modifier");
298 else if(key->str.cstr[0] == '^')
300 keycode = toupper((unsigned char)key->str.cstr[1]) - '@';
301 if(keycode >= ' ')
303 print_location( &key->loc );
304 error("Control-code out of range");
307 else
308 keycode = key->str.cstr[0];
310 else
312 if((flags & WRC_AF_VIRTKEY) &&
313 !((key->str.wstr[0] >= 'A' && key->str.wstr[0] <= 'Z') ||
314 (key->str.wstr[0] >= '0' && key->str.wstr[0] <= '9')))
316 print_location( &key->loc );
317 error("VIRTKEY code is not equal to ascii value");
319 if(key->str.wstr[0] == '^' && (flags & WRC_AF_CONTROL) != 0)
321 print_location( &key->loc );
322 error("Cannot use both '^' and CONTROL modifier");
324 else if(key->str.wstr[0] == '^')
326 keycode = toupperW(key->str.wstr[1]) - '@';
327 if(keycode >= ' ')
329 print_location( &key->loc );
330 error("Control-code out of range");
333 else
334 keycode = key->str.wstr[0];
336 return keycode;
340 *****************************************************************************
341 * Function : put_string
342 * Syntax : void put_string(res_t *res, string_t *str, enum str_e type,
343 * int isterm, const language_t *lang)
344 * Input :
345 * res - Binary resource to put the data in
346 * str - String to put
347 * type - Data has to be written in either str_char or str_unicode
348 * isterm - The string is '\0' terminated (disregard the string's
349 * size member)
350 * Output : nop
351 * Description :
352 * Remarks :
353 *****************************************************************************
355 static void put_string(res_t *res, const string_t *str, enum str_e type, int isterm,
356 const language_t *lang)
358 int cnt, codepage;
359 string_t *newstr;
361 assert(res != NULL);
362 assert(str != NULL);
364 if (lang) codepage = get_language_codepage( lang->id, lang->sub );
365 else codepage = get_language_codepage( 0, 0 );
367 assert( codepage != -1 );
369 newstr = convert_string(str, type, codepage);
370 if (type == str_unicode)
372 if (str->type == str_char)
374 if (!check_unicode_conversion( str, newstr, codepage ))
376 print_location( &str->loc );
377 error( "String %s does not convert identically to Unicode and back in codepage %d. "
378 "Try using a Unicode string instead\n", str->str.cstr, codepage );
380 if (check_valid_utf8( str, codepage ))
382 print_location( &str->loc );
383 warning( "string \"%s\" seems to be UTF-8 but codepage %u is in use.\n",
384 str->str.cstr, codepage );
387 if (!isterm) put_word(res, newstr->size);
388 for(cnt = 0; cnt < newstr->size; cnt++)
390 WCHAR c = newstr->str.wstr[cnt];
391 if (isterm && !c) break;
392 put_word(res, c);
394 if (isterm) put_word(res, 0);
396 else /* str_char */
398 if (!isterm) put_byte(res, newstr->size);
399 for(cnt = 0; cnt < newstr->size; cnt++)
401 char c = newstr->str.cstr[cnt];
402 if (isterm && !c) break;
403 put_byte(res, c);
405 if (isterm) put_byte(res, 0);
407 free_string(newstr);
411 *****************************************************************************
412 * Function : put_name_id
413 * Syntax : void put_name_id(res_t *res, name_id_t *nid, int upcase, const language_t *lang)
414 * Input :
415 * Output :
416 * Description :
417 * Remarks :
418 *****************************************************************************
420 static void put_name_id(res_t *res, name_id_t *nid, int upcase, const language_t *lang)
422 if(nid->type == name_ord)
424 if(win32)
425 put_word(res, 0xffff);
426 else
427 put_byte(res, 0xff);
428 put_word(res, (WORD)nid->name.i_name);
430 else if(nid->type == name_str)
432 if(upcase)
433 string_to_upper(nid->name.s_name);
434 put_string(res, nid->name.s_name, win32 ? str_unicode : str_char, TRUE, lang);
436 else
438 internal_error(__FILE__, __LINE__, "Invalid name_id type %d\n", nid->type);
443 *****************************************************************************
444 * Function : put_lvc
445 * Syntax : void put_lvc(res_t *res, lvc_t *lvc)
446 * Input :
447 * Output :
448 * Description :
449 * Remarks :
450 *****************************************************************************
452 static void put_lvc(res_t *res, lvc_t *lvc)
454 if(lvc && lvc->language)
455 put_word(res, MAKELANGID(lvc->language->id, lvc->language->sub));
456 else
457 put_word(res, 0); /* Neutral */
458 if(lvc && lvc->version)
459 put_dword(res, *(lvc->version));
460 else
461 put_dword(res, 0);
462 if(lvc && lvc->characts)
463 put_dword(res, *(lvc->characts));
464 else
465 put_dword(res, 0);
469 *****************************************************************************
470 * Function : put_raw_data
471 * Syntax : void put_raw_data(res_t *res, raw_data_t *raw, int offset)
472 * Input :
473 * Output :
474 * Description :
475 * Remarks :
476 *****************************************************************************
478 static void put_raw_data(res_t *res, raw_data_t *raw, int offset)
480 unsigned int wsize = raw->size - offset;
481 if(res->allocsize - res->size < wsize)
482 grow_res(res, wsize);
483 memcpy(&(res->data[res->size]), raw->data + offset, wsize);
484 res->size += wsize;
488 *****************************************************************************
489 * Function : put_res_header
490 * Syntax : input_res_header(res_t *res, int type, name_id_t *ntype,
491 * name_id_t *name, DWORD memopt, lvc_t *lvc)
493 * Input :
494 * res - Binary resource descriptor to write to
495 * type - Resource identifier (if ntype == NULL)
496 * ntype - Name id of type
497 * name - Resource's name
498 * memopt - Resource's memory options to write
499 * lvc - Language, version and characteristics (win32 only)
500 * Output : An index to the resource size field. The resource size field
501 * contains the header size upon exit.
502 * Description :
503 * Remarks :
504 *****************************************************************************
506 static int put_res_header(res_t *res, int type, name_id_t *ntype, name_id_t *name,
507 DWORD memopt, lvc_t *lvc)
509 if(win32)
511 put_dword(res, 0); /* We will overwrite these later */
512 put_dword(res, 0);
513 if(!ntype)
515 put_word(res, 0xffff); /* ResType */
516 put_word(res, type);
518 else
519 put_name_id(res, ntype, TRUE, lvc->language);
520 put_name_id(res, name, TRUE, lvc->language); /* ResName */
521 put_pad(res);
522 put_dword(res, 0); /* DataVersion */
523 put_word(res, memopt); /* Memory options */
524 put_lvc(res, lvc); /* Language, version and characts */
525 set_dword(res, 0*sizeof(DWORD), res->size); /* Set preliminary resource */
526 set_dword(res, 1*sizeof(DWORD), res->size); /* Set HeaderSize */
527 res->dataidx = res->size;
528 return 0;
530 else /* win16 */
532 int tag;
533 if(!ntype)
535 put_byte(res, 0xff); /* ResType */
536 put_word(res, type);
538 else
539 put_name_id(res, ntype, TRUE, NULL);
540 put_name_id(res, name, TRUE, NULL); /* ResName */
541 put_word(res, memopt); /* Memory options */
542 tag = res->size;
543 put_dword(res, 0); /* ResSize overwritten later*/
544 set_dword(res, tag, res->size);
545 res->dataidx = res->size;
546 return tag;
551 *****************************************************************************
552 * Function : accelerator2res
553 * Syntax : res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
554 * Input :
555 * name - Name/ordinal of the resource
556 * acc - The accelerator descriptor
557 * Output : New .res format structure
558 * Description :
559 * Remarks :
560 *****************************************************************************
562 static res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
564 int restag;
565 res_t *res;
566 event_t *ev;
567 assert(name != NULL);
568 assert(acc != NULL);
570 ev = acc->events;
571 res = new_res();
572 if(win32)
574 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, &(acc->lvc));
575 while(ev)
577 int key = ev->key;
578 if (ev->str) key = parse_accel_string( ev->str, ev->flags );
579 put_word(res, ev->flags | (ev->next ? 0 : 0x80));
580 put_word(res, key);
581 put_word(res, ev->id);
582 put_word(res, 0); /* Padding */
583 ev = ev->next;
585 put_pad(res);
587 else /* win16 */
589 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, NULL);
590 while(ev)
592 int key = ev->key;
593 if (ev->str) key = parse_accel_string( ev->str, ev->flags );
594 put_byte(res, ev->flags | (ev->next ? 0 : 0x80));
595 put_word(res, key);
596 put_word(res, ev->id);
597 ev = ev->next;
600 /* Set ResourceSize */
601 SetResSize(res, restag);
602 return res;
606 *****************************************************************************
607 * Function : dialog2res
608 * Syntax : res_t *dialog2res(name_id_t *name, dialog_t *dlg)
609 * Input :
610 * name - Name/ordinal of the resource
611 * dlg - The dialog descriptor
612 * Output : New .res format structure
613 * Description :
614 * Remarks :
615 *****************************************************************************
617 static res_t *dialog2res(name_id_t *name, dialog_t *dlg)
619 int restag;
620 res_t *res;
621 control_t *ctrl;
622 int tag_nctrl;
623 int nctrl = 0;
624 assert(name != NULL);
625 assert(dlg != NULL);
627 ctrl = dlg->controls;
628 res = new_res();
629 if(win32)
631 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, &(dlg->lvc));
633 if (dlg->is_ex)
635 /* FIXME: MS doc says that the first word must contain 0xffff
636 * and the second 0x0001 to signal a DLGTEMPLATEEX. Borland's
637 * compiler reverses the two words.
638 * I don't know which one to choose, but I write it as Mr. B
639 * writes it.
641 put_word(res, 1); /* Signature */
642 put_word(res, 0xffff); /* DlgVer */
643 put_dword(res, dlg->gothelpid ? dlg->helpid : 0);
644 put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
645 put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
647 else
649 put_dword(res, dlg->style->or_mask);
650 put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
652 tag_nctrl = res->size;
653 put_word(res, 0); /* Number of controls */
654 put_word(res, dlg->x);
655 put_word(res, dlg->y);
656 put_word(res, dlg->width);
657 put_word(res, dlg->height);
658 if(dlg->menu)
659 put_name_id(res, dlg->menu, TRUE, dlg->lvc.language);
660 else
661 put_word(res, 0);
662 if(dlg->dlgclass)
663 put_name_id(res, dlg->dlgclass, TRUE, dlg->lvc.language);
664 else
665 put_word(res, 0);
666 if(dlg->title)
667 put_string(res, dlg->title, str_unicode, TRUE, dlg->lvc.language);
668 else
669 put_word(res, 0);
670 if(dlg->font)
672 put_word(res, dlg->font->size);
673 if (dlg->is_ex)
675 put_word(res, dlg->font->weight);
676 /* FIXME: ? TRUE should be sufficient to say that it's
677 * italic, but Borland's compiler says it's 0x0101.
678 * I just copy it here, and hope for the best.
680 put_word(res, dlg->font->italic ? 0x0101 : 0);
682 put_string(res, dlg->font->name, str_unicode, TRUE, dlg->lvc.language);
685 put_pad(res);
686 while(ctrl)
688 if (dlg->is_ex)
690 put_dword(res, ctrl->gothelpid ? ctrl->helpid : 0);
691 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
692 /* FIXME: what is default control style? */
693 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask : WS_CHILD | WS_VISIBLE);
695 else
697 /* FIXME: what is default control style? */
698 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
699 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
701 put_word(res, ctrl->x);
702 put_word(res, ctrl->y);
703 put_word(res, ctrl->width);
704 put_word(res, ctrl->height);
705 if (dlg->is_ex)
706 put_dword(res, ctrl->id);
707 else
708 put_word(res, ctrl->id);
709 if(ctrl->ctlclass)
710 put_name_id(res, ctrl->ctlclass, TRUE, dlg->lvc.language);
711 else
712 internal_error(__FILE__, __LINE__, "Control has no control-class\n");
713 if(ctrl->title)
714 put_name_id(res, ctrl->title, FALSE, dlg->lvc.language);
715 else
716 put_word(res, 0);
717 if(ctrl->extra)
719 put_word(res, ctrl->extra->size+2);
720 put_pad(res);
721 put_raw_data(res, ctrl->extra, 0);
723 else
724 put_word(res, 0);
726 if(ctrl->next)
727 put_pad(res);
728 nctrl++;
729 ctrl = ctrl->next;
731 /* Set number of controls */
732 set_word(res, tag_nctrl, (WORD)nctrl);
734 else /* win16 */
736 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, NULL);
738 put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
739 tag_nctrl = res->size;
740 put_byte(res, 0); /* Number of controls */
741 put_word(res, dlg->x);
742 put_word(res, dlg->y);
743 put_word(res, dlg->width);
744 put_word(res, dlg->height);
745 if(dlg->menu)
746 put_name_id(res, dlg->menu, TRUE, NULL);
747 else
748 put_byte(res, 0);
749 if(dlg->dlgclass)
750 put_name_id(res, dlg->dlgclass, TRUE, NULL);
751 else
752 put_byte(res, 0);
753 if(dlg->title)
754 put_string(res, dlg->title, str_char, TRUE, NULL);
755 else
756 put_byte(res, 0);
757 if(dlg->font)
759 put_word(res, dlg->font->size);
760 put_string(res, dlg->font->name, str_char, TRUE, NULL);
763 while(ctrl)
765 put_word(res, ctrl->x);
766 put_word(res, ctrl->y);
767 put_word(res, ctrl->width);
768 put_word(res, ctrl->height);
769 put_word(res, ctrl->id);
770 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
771 if(ctrl->ctlclass)
773 if(ctrl->ctlclass->type == name_ord
774 && ctrl->ctlclass->name.i_name >= 0x80
775 && ctrl->ctlclass->name.i_name <= 0x85)
776 put_byte(res, ctrl->ctlclass->name.i_name);
777 else if(ctrl->ctlclass->type == name_str)
778 put_name_id(res, ctrl->ctlclass, FALSE, NULL);
779 else
780 error("Unknown control-class %04x\n", ctrl->ctlclass->name.i_name);
782 else
783 internal_error(__FILE__, __LINE__, "Control has no control-class\n");
784 if(ctrl->title)
785 put_name_id(res, ctrl->title, FALSE, NULL);
786 else
787 put_byte(res, 0);
789 /* FIXME: What is this extra byte doing here? */
790 put_byte(res, 0);
792 nctrl++;
793 ctrl = ctrl->next;
795 /* Set number of controls */
796 ((char *)res->data)[tag_nctrl] = (char)nctrl;
798 /* Set ResourceSize */
799 SetResSize(res, restag);
800 return res;
804 *****************************************************************************
805 * Function : menuitem2res
806 * Syntax : void menuitem2res(res_t *res, menu_item_t *item)
807 * Input :
808 * Output :
809 * Description :
810 * Remarks : Self recursive
811 *****************************************************************************
813 static void menuitem2res(res_t *res, menu_item_t *menitem, const language_t *lang)
815 menu_item_t *itm = menitem;
816 if(win32)
818 while(itm)
820 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
821 if(!itm->popup)
822 put_word(res, itm->id);
823 if(itm->name)
824 put_string(res, itm->name, str_unicode, TRUE, lang);
825 else
826 put_word(res, 0);
827 if(itm->popup)
828 menuitem2res(res, itm->popup, lang);
829 itm = itm->next;
832 else /* win16 */
834 while(itm)
836 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
837 if(!itm->popup)
838 put_word(res, itm->id);
839 if(itm->name)
840 put_string(res, itm->name, str_char, TRUE, lang);
841 else
842 put_byte(res, 0);
843 if(itm->popup)
844 menuitem2res(res, itm->popup, lang);
845 itm = itm->next;
852 *****************************************************************************
853 * Function : menuexitem2res
854 * Syntax : void menuexitem2res(res_t *res, menuex_item_t *item)
855 * Input :
856 * Output : nop
857 * Description :
858 * Remarks : Self recursive
859 *****************************************************************************
861 static void menuexitem2res(res_t *res, menu_item_t *menitem, const language_t *lang)
863 menu_item_t *itm = menitem;
864 assert(win32 != 0);
865 while(itm)
867 put_dword(res, itm->gottype ? itm->type : 0);
868 put_dword(res, itm->gotstate ? itm->state : 0);
869 put_dword(res, itm->gotid ? itm->id : 0); /* FIXME: Docu. says word */
870 put_word(res, (itm->popup ? 0x01 : 0) | (!itm->next ? MF_END : 0));
871 if(itm->name)
872 put_string(res, itm->name, str_unicode, TRUE, lang);
873 else
874 put_word(res, 0);
875 put_pad(res);
876 if(itm->popup)
878 put_dword(res, itm->gothelpid ? itm->helpid : 0);
879 menuexitem2res(res, itm->popup, lang);
881 itm = itm->next;
887 *****************************************************************************
888 * Function : menu2res
889 * Syntax : res_t *menu2res(name_id_t *name, menu_t *men)
890 * Input :
891 * name - Name/ordinal of the resource
892 * menex - The menuex descriptor
893 * Output : New .res format structure
894 * Description :
895 * Remarks :
896 *****************************************************************************
898 static res_t *menu2res(name_id_t *name, menu_t *men)
900 int restag;
901 res_t *res;
902 assert(name != NULL);
903 assert(men != NULL);
905 res = new_res();
906 if(win32)
908 restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, &(men->lvc));
910 if (men->is_ex)
912 put_word(res, 1); /* Menuheader: Version */
913 put_word(res, 4); /* Offset */
914 put_dword(res, 0); /* HelpId */
915 put_pad(res);
916 menuexitem2res(res, men->items, men->lvc.language);
918 else
920 put_dword(res, 0); /* Menuheader: Version and HeaderSize */
921 menuitem2res(res, men->items, men->lvc.language);
923 /* Set ResourceSize */
924 SetResSize(res, restag);
925 put_pad(res);
927 else /* win16 */
929 restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, NULL);
931 put_dword(res, 0); /* Menuheader: Version and HeaderSize */
932 menuitem2res(res, men->items, NULL);
933 /* Set ResourceSize */
934 SetResSize(res, restag);
936 return res;
940 *****************************************************************************
941 * Function : cursorgroup2res
942 * Syntax : res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
943 * Input :
944 * name - Name/ordinal of the resource
945 * curg - The cursor descriptor
946 * Output : New .res format structure
947 * Description :
948 * Remarks :
949 *****************************************************************************
951 static res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
953 int restag;
954 res_t *res;
955 cursor_t *cur;
956 assert(name != NULL);
957 assert(curg != NULL);
959 res = new_res();
960 restag = put_res_header(res, WRC_RT_GROUP_CURSOR, NULL, name, curg->memopt, &(curg->lvc));
961 if(win32)
963 put_word(res, 0); /* Reserved */
964 /* FIXME: The ResType in the NEWHEADER structure should
965 * contain 14 according to the MS win32 doc. This is
966 * not the case with the BRC compiler and I really doubt
967 * the latter. Putting one here is compliant to win16 spec,
968 * but who knows the true value?
970 put_word(res, 2); /* ResType */
971 put_word(res, curg->ncursor);
972 #if 0
973 for(cur = curg->cursorlist; cur; cur = cur->next)
974 #else
975 cur = curg->cursorlist;
976 while(cur->next)
977 cur = cur->next;
978 for(; cur; cur = cur->prev)
979 #endif
981 put_word(res, cur->width);
982 /* FIXME: The height of a cursor is half the size of
983 * the bitmap's height. BRC puts the height from the
984 * BITMAPINFOHEADER here instead of the cursorfile's
985 * height. MS doesn't seem to care...
987 put_word(res, cur->height);
988 /* FIXME: The next two are reversed in BRC and I don't
989 * know why. Probably a bug. But, we can safely ignore
990 * it because win16 does not support color cursors.
991 * A warning should have been generated by the parser.
993 put_word(res, cur->planes);
994 put_word(res, cur->bits);
995 /* FIXME: The +4 is the hotspot in the cursor resource.
996 * However, I could not find this in the documentation.
997 * The hotspot bytes must either be included or MS
998 * doesn't care.
1000 put_dword(res, cur->data->size +4);
1001 put_word(res, cur->id);
1004 else /* win16 */
1006 put_word(res, 0); /* Reserved */
1007 put_word(res, 2); /* ResType */
1008 put_word(res, curg->ncursor);
1009 #if 0
1010 for(cur = curg->cursorlist; cur; cur = cur->next)
1011 #else
1012 cur = curg->cursorlist;
1013 while(cur->next)
1014 cur = cur->next;
1015 for(; cur; cur = cur->prev)
1016 #endif
1018 put_word(res, cur->width);
1019 /* FIXME: The height of a cursor is half the size of
1020 * the bitmap's height. BRC puts the height from the
1021 * BITMAPINFOHEADER here instead of the cursorfile's
1022 * height. MS doesn't seem to care...
1024 put_word(res, cur->height);
1025 /* FIXME: The next two are reversed in BRC and I don't
1026 * know why. Probably a bug. But, we can safely ignore
1027 * it because win16 does not support color cursors.
1028 * A warning should have been generated by the parser.
1030 put_word(res, cur->planes);
1031 put_word(res, cur->bits);
1032 /* FIXME: The +4 is the hotspot in the cursor resource.
1033 * However, I could not find this in the documentation.
1034 * The hotspot bytes must either be included or MS
1035 * doesn't care.
1037 put_dword(res, cur->data->size +4);
1038 put_word(res, cur->id);
1041 SetResSize(res, restag); /* Set ResourceSize */
1042 if(win32)
1043 put_pad(res);
1045 return res;
1049 *****************************************************************************
1050 * Function : cursor2res
1051 * Syntax : res_t *cursor2res(cursor_t *cur)
1052 * Input :
1053 * cur - The cursor descriptor
1054 * Output : New .res format structure
1055 * Description :
1056 * Remarks :
1057 *****************************************************************************
1059 static res_t *cursor2res(cursor_t *cur)
1061 int restag;
1062 res_t *res;
1063 name_id_t name;
1065 assert(cur != NULL);
1067 res = new_res();
1068 name.type = name_ord;
1069 name.name.i_name = cur->id;
1070 restag = put_res_header(res, WRC_RT_CURSOR, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(cur->lvc));
1071 put_word(res, cur->xhot);
1072 put_word(res, cur->yhot);
1073 put_raw_data(res, cur->data, 0);
1075 SetResSize(res, restag); /* Set ResourceSize */
1076 if(win32)
1077 put_pad(res);
1079 return res;
1083 *****************************************************************************
1084 * Function : icongroup2res
1085 * Syntax : res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1086 * Input :
1087 * name - Name/ordinal of the resource
1088 * icog - The icon group descriptor
1089 * Output : New .res format structure
1090 * Description :
1091 * Remarks :
1092 *****************************************************************************
1094 static res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1096 int restag;
1097 res_t *res;
1098 icon_t *ico;
1099 assert(name != NULL);
1100 assert(icog != NULL);
1102 res = new_res();
1103 restag = put_res_header(res, WRC_RT_GROUP_ICON, NULL, name, icog->memopt, &(icog->lvc));
1104 if(win32)
1106 put_word(res, 0); /* Reserved */
1107 /* FIXME: The ResType in the NEWHEADER structure should
1108 * contain 14 according to the MS win32 doc. This is
1109 * not the case with the BRC compiler and I really doubt
1110 * the latter. Putting one here is compliant to win16 spec,
1111 * but who knows the true value?
1113 put_word(res, 1); /* ResType */
1114 put_word(res, icog->nicon);
1115 for(ico = icog->iconlist; ico; ico = ico->next)
1117 put_byte(res, ico->width);
1118 put_byte(res, ico->height);
1119 put_byte(res, ico->nclr);
1120 put_byte(res, 0); /* Reserved */
1121 put_word(res, ico->planes);
1122 put_word(res, ico->bits);
1123 put_dword(res, ico->data->size);
1124 put_word(res, ico->id);
1127 else /* win16 */
1129 put_word(res, 0); /* Reserved */
1130 put_word(res, 1); /* ResType */
1131 put_word(res, icog->nicon);
1132 for(ico = icog->iconlist; ico; ico = ico->next)
1134 put_byte(res, ico->width);
1135 put_byte(res, ico->height);
1136 put_byte(res, ico->nclr);
1137 put_byte(res, 0); /* Reserved */
1138 put_word(res, ico->planes);
1139 put_word(res, ico->bits);
1140 put_dword(res, ico->data->size);
1141 put_word(res, ico->id);
1144 SetResSize(res, restag); /* Set ResourceSize */
1145 if(win32)
1146 put_pad(res);
1148 return res;
1152 *****************************************************************************
1153 * Function : icon2res
1154 * Syntax : res_t *icon2res(icon_t *ico)
1155 * Input :
1156 * ico - The icon descriptor
1157 * Output : New .res format structure
1158 * Description :
1159 * Remarks :
1160 *****************************************************************************
1162 static res_t *icon2res(icon_t *ico)
1164 int restag;
1165 res_t *res;
1166 name_id_t name;
1168 assert(ico != NULL);
1170 res = new_res();
1171 name.type = name_ord;
1172 name.name.i_name = ico->id;
1173 restag = put_res_header(res, WRC_RT_ICON, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(ico->lvc));
1174 put_raw_data(res, ico->data, 0);
1176 SetResSize(res, restag); /* Set ResourceSize */
1177 if(win32)
1178 put_pad(res);
1180 return res;
1184 *****************************************************************************
1185 * Function : anicurico2res
1186 * Syntax : res_t *anicurico2res(name_id_t *name, ani_curico_t *ani)
1187 * Input :
1188 * name - Name/ordinal of the resource
1189 * ani - The animated object descriptor
1190 * Output : New .res format structure
1191 * Description :
1192 * Remarks : The endian of the object's structures have been converted
1193 * by the loader.
1194 * There are rumors that win311 could handle animated stuff.
1195 * That is why they are generated for both win16 and win32
1196 * compile.
1197 *****************************************************************************
1199 static res_t *anicurico2res(name_id_t *name, ani_curico_t *ani, enum res_e type)
1201 int restag;
1202 res_t *res;
1203 assert(name != NULL);
1204 assert(ani != NULL);
1206 res = new_res();
1207 restag = put_res_header(res, type == res_anicur ? WRC_RT_ANICURSOR : WRC_RT_ANIICON,
1208 NULL, name, ani->memopt, NULL);
1209 put_raw_data(res, ani->data, 0);
1210 /* Set ResourceSize */
1211 SetResSize(res, restag);
1212 if(win32)
1213 put_pad(res);
1214 return res;
1218 *****************************************************************************
1219 * Function : bitmap2res
1220 * Syntax : res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1221 * Input :
1222 * name - Name/ordinal of the resource
1223 * bmp - The bitmap descriptor
1224 * Output : New .res format structure
1225 * Description :
1226 * Remarks : The endian of the bitmap structures have been converted
1227 * by the loader.
1228 *****************************************************************************
1230 static res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1232 int restag;
1233 res_t *res;
1234 assert(name != NULL);
1235 assert(bmp != NULL);
1237 res = new_res();
1238 restag = put_res_header(res, WRC_RT_BITMAP, NULL, name, bmp->memopt, &(bmp->data->lvc));
1239 if(bmp->data->data[0] == 'B'
1240 && bmp->data->data[1] == 'M'
1241 && ((BITMAPFILEHEADER *)bmp->data->data)->bfSize == bmp->data->size
1242 && bmp->data->size >= sizeof(BITMAPFILEHEADER))
1244 /* The File header is still attached, don't write it */
1245 put_raw_data(res, bmp->data, sizeof(BITMAPFILEHEADER));
1247 else
1249 put_raw_data(res, bmp->data, 0);
1251 /* Set ResourceSize */
1252 SetResSize(res, restag);
1253 if(win32)
1254 put_pad(res);
1255 return res;
1259 *****************************************************************************
1260 * Function : font2res
1261 * Syntax : res_t *font2res(name_id_t *name, font_t *fnt)
1262 * Input :
1263 * name - Name/ordinal of the resource
1264 * fnt - The font descriptor
1265 * Output : New .res format structure
1266 * Description :
1267 * Remarks : The data has been prepared just after parsing.
1268 *****************************************************************************
1270 static res_t *font2res(name_id_t *name, font_t *fnt)
1272 int restag;
1273 res_t *res;
1274 assert(name != NULL);
1275 assert(fnt != NULL);
1277 res = new_res();
1278 restag = put_res_header(res, WRC_RT_FONT, NULL, name, fnt->memopt, &(fnt->data->lvc));
1279 put_raw_data(res, fnt->data, 0);
1280 /* Set ResourceSize */
1281 SetResSize(res, restag);
1282 if(win32)
1283 put_pad(res);
1284 return res;
1288 *****************************************************************************
1289 * Function : fontdir2res
1290 * Syntax : res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1291 * Input :
1292 * name - Name/ordinal of the resource
1293 * fntdir - The fontdir descriptor
1294 * Output : New .res format structure
1295 * Description :
1296 * Remarks : The data has been prepared just after parsing.
1297 *****************************************************************************
1299 static res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1301 int restag;
1302 res_t *res;
1303 assert(name != NULL);
1304 assert(fnd != NULL);
1306 res = new_res();
1307 restag = put_res_header(res, WRC_RT_FONTDIR, NULL, name, fnd->memopt, &(fnd->data->lvc));
1308 put_raw_data(res, fnd->data, 0);
1309 /* Set ResourceSize */
1310 SetResSize(res, restag);
1311 if(win32)
1312 put_pad(res);
1313 return res;
1317 *****************************************************************************
1318 * Function : html2res
1319 * Syntax : res_t *html2res(name_id_t *name, html_t *html)
1320 * Input :
1321 * name - Name/ordinal of the resource
1322 * rdt - The html descriptor
1323 * Output : New .res format structure
1324 * Description :
1325 * Remarks :
1326 *****************************************************************************
1328 static res_t *html2res(name_id_t *name, html_t *html)
1330 int restag;
1331 res_t *res;
1332 assert(name != NULL);
1333 assert(html != NULL);
1335 res = new_res();
1336 restag = put_res_header(res, WRC_RT_HTML, NULL, name, html->memopt, &(html->data->lvc));
1337 put_raw_data(res, html->data, 0);
1338 /* Set ResourceSize */
1339 SetResSize(res, restag);
1340 if(win32)
1341 put_pad(res);
1342 return res;
1346 *****************************************************************************
1347 * Function : rcdata2res
1348 * Syntax : res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1349 * Input :
1350 * name - Name/ordinal of the resource
1351 * rdt - The rcdata descriptor
1352 * Output : New .res format structure
1353 * Description :
1354 * Remarks :
1355 *****************************************************************************
1357 static res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1359 int restag;
1360 res_t *res;
1361 assert(name != NULL);
1362 assert(rdt != NULL);
1364 res = new_res();
1365 restag = put_res_header(res, WRC_RT_RCDATA, NULL, name, rdt->memopt, &(rdt->data->lvc));
1366 put_raw_data(res, rdt->data, 0);
1367 /* Set ResourceSize */
1368 SetResSize(res, restag);
1369 if(win32)
1370 put_pad(res);
1371 return res;
1375 *****************************************************************************
1376 * Function : messagetable2res
1377 * Syntax : res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1378 * Input :
1379 * name - Name/ordinal of the resource
1380 * msg - The messagetable descriptor
1381 * Output : New .res format structure
1382 * Description :
1383 * Remarks : The data has been converted to the appropriate endian
1384 * after it was parsed.
1385 *****************************************************************************
1387 static res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1389 int restag;
1390 res_t *res;
1391 assert(name != NULL);
1392 assert(msg != NULL);
1394 res = new_res();
1395 restag = put_res_header(res, WRC_RT_MESSAGETABLE, NULL, name, msg->memopt, &(msg->data->lvc));
1396 put_raw_data(res, msg->data, 0);
1397 /* Set ResourceSize */
1398 SetResSize(res, restag);
1399 if(win32)
1400 put_pad(res);
1401 return res;
1405 *****************************************************************************
1406 * Function : stringtable2res
1407 * Syntax : res_t *stringtable2res(stringtable_t *stt)
1408 * Input :
1409 * stt - The stringtable descriptor
1410 * Output : New .res format structure
1411 * Description :
1412 * Remarks :
1413 *****************************************************************************
1415 static res_t *stringtable2res(stringtable_t *stt)
1417 res_t *res;
1418 name_id_t name;
1419 int i;
1420 int restag;
1421 DWORD lastsize = 0;
1423 assert(stt != NULL);
1424 res = new_res();
1426 for(; stt; stt = stt->next)
1428 if(!stt->nentries)
1430 warning("Empty internal stringtable\n");
1431 continue;
1433 name.type = name_ord;
1434 name.name.i_name = (stt->idbase >> 4) + 1;
1435 restag = put_res_header(res, WRC_RT_STRING, NULL, &name, stt->memopt, win32 ? &(stt->lvc) : NULL);
1436 for(i = 0; i < stt->nentries; i++)
1438 if(stt->entries[i].str && stt->entries[i].str->size)
1440 put_string(res, stt->entries[i].str, win32 ? str_unicode : str_char,
1441 FALSE, win32 ? stt->lvc.language : NULL);
1443 else
1445 if (win32)
1446 put_word(res, 0);
1447 else
1448 put_byte(res, 0);
1451 /* Set ResourceSize */
1452 SetResSize(res, restag - lastsize);
1453 if(win32)
1454 put_pad(res);
1455 lastsize = res->size;
1457 return res;
1461 *****************************************************************************
1462 * Function : user2res
1463 * Syntax : res_t *user2res(name_id_t *name, user_t *usr)
1464 * Input :
1465 * name - Name/ordinal of the resource
1466 * usr - The userresource descriptor
1467 * Output : New .res format structure
1468 * Description :
1469 * Remarks :
1470 *****************************************************************************
1472 static res_t *user2res(name_id_t *name, user_t *usr)
1474 int restag;
1475 res_t *res;
1476 assert(name != NULL);
1477 assert(usr != NULL);
1479 res = new_res();
1480 restag = put_res_header(res, 0, usr->type, name, usr->memopt, &(usr->data->lvc));
1481 put_raw_data(res, usr->data, 0);
1482 /* Set ResourceSize */
1483 SetResSize(res, restag);
1484 if(win32)
1485 put_pad(res);
1486 return res;
1490 *****************************************************************************
1491 * Function : versionblock2res
1492 * Syntax : void versionblock2res(res_t *res, ver_block_t *blk)
1493 * Input :
1494 * res - Binary resource to write to
1495 * blk - The version block to be written
1496 * Output :
1497 * Description :
1498 * Remarks : Self recursive
1499 *****************************************************************************
1501 static void versionblock2res(res_t *res, ver_block_t *blk, int level, const language_t *lang)
1503 ver_value_t *val;
1504 int blksizetag;
1505 int valblksizetag;
1506 int valvalsizetag;
1507 int tag;
1508 int i;
1510 blksizetag = res->size;
1511 put_word(res, 0); /* Will be overwritten later */
1512 put_word(res, 0);
1513 if(win32)
1514 put_word(res, 0); /* level ? */
1515 put_string(res, blk->name, win32 ? str_unicode : str_char, TRUE, lang);
1516 put_pad(res);
1517 for(val = blk->values; val; val = val->next)
1519 if(val->type == val_str)
1521 valblksizetag = res->size;
1522 put_word(res, 0); /* Will be overwritten later */
1523 valvalsizetag = res->size;
1524 put_word(res, 0); /* Will be overwritten later */
1525 if(win32)
1527 put_word(res, level);
1529 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
1530 put_pad(res);
1531 tag = res->size;
1532 put_string(res, val->value.str, win32 ? str_unicode : str_char, TRUE, lang);
1533 if(win32)
1534 set_word(res, valvalsizetag, (WORD)((res->size - tag) >> 1));
1535 else
1536 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1537 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1538 put_pad(res);
1540 else if(val->type == val_words)
1542 valblksizetag = res->size;
1543 put_word(res, 0); /* Will be overwritten later */
1544 valvalsizetag = res->size;
1545 put_word(res, 0); /* Will be overwritten later */
1546 if(win32)
1548 put_word(res, level);
1550 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
1551 put_pad(res);
1552 tag = res->size;
1553 for(i = 0; i < val->value.words->nwords; i++)
1555 put_word(res, val->value.words->words[i]);
1557 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1558 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1559 put_pad(res);
1561 else if(val->type == val_block)
1563 versionblock2res(res, val->value.block, level+1, lang);
1565 else
1567 internal_error(__FILE__, __LINE__, "Invalid value indicator %d in VERSIONINFO\n", val->type);
1571 /* Set blocksize */
1572 set_word(res, blksizetag, (WORD)(res->size - blksizetag));
1576 *****************************************************************************
1577 * Function : versioninfo2res
1578 * Syntax : res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1579 * Input :
1580 * name - Name/ordinal of the resource
1581 * ver - The versioninfo descriptor
1582 * Output : New .res format structure
1583 * Description :
1584 * Remarks :
1585 *****************************************************************************
1587 static res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1589 int restag;
1590 int rootblocksizetag;
1591 int valsizetag;
1592 int tag;
1593 res_t *res;
1594 string_t vsvi;
1595 ver_block_t *blk;
1597 assert(name != NULL);
1598 assert(ver != NULL);
1600 vsvi.type = str_char;
1601 vsvi.str.cstr = xstrdup("VS_VERSION_INFO");
1602 vsvi.size = 15; /* Excl. termination */
1604 res = new_res();
1605 restag = put_res_header(res, WRC_RT_VERSION, NULL, name, ver->memopt, &(ver->lvc));
1606 rootblocksizetag = res->size;
1607 put_word(res, 0); /* BlockSize filled in later */
1608 valsizetag = res->size;
1609 put_word(res, 0); /* ValueSize filled in later*/
1610 if(win32)
1611 put_word(res, 0); /* Tree-level ? */
1612 put_string(res, &vsvi, win32 ? str_unicode : str_char,
1613 TRUE, win32 ? ver->lvc.language : NULL);
1614 if(win32)
1615 put_pad(res);
1616 tag = res->size;
1617 put_dword(res, VS_FFI_SIGNATURE);
1618 put_dword(res, VS_FFI_STRUCVERSION);
1619 put_dword(res, (ver->filever_maj1 << 16) + (ver->filever_maj2 & 0xffff));
1620 put_dword(res, (ver->filever_min1 << 16) + (ver->filever_min2 & 0xffff));
1621 put_dword(res, (ver->prodver_maj1 << 16) + (ver->prodver_maj2 & 0xffff));
1622 put_dword(res, (ver->prodver_min1 << 16) + (ver->prodver_min2 & 0xffff));
1623 put_dword(res, ver->fileflagsmask);
1624 put_dword(res, ver->fileflags);
1625 put_dword(res, ver->fileos);
1626 put_dword(res, ver->filetype);
1627 put_dword(res, ver->filesubtype);
1628 put_dword(res, 0); /* FileDateMS */
1629 put_dword(res, 0); /* FileDateLS */
1630 /* Set ValueSize */
1631 set_word(res, valsizetag, (WORD)(res->size - tag));
1632 /* Descend into the blocks */
1633 for(blk = ver->blocks; blk; blk = blk->next)
1634 versionblock2res(res, blk, 0, win32 ? ver->lvc.language : NULL);
1635 /* Set root block's size */
1636 set_word(res, rootblocksizetag, (WORD)(res->size - rootblocksizetag));
1638 SetResSize(res, restag);
1639 if(win32)
1640 put_pad(res);
1642 free(vsvi.str.cstr);
1643 return res;
1647 *****************************************************************************
1648 * Function : toolbaritem2res
1649 * Syntax : void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1650 * Input :
1651 * Output : nop
1652 * Description :
1653 * Remarks : Self recursive
1654 *****************************************************************************
1656 static void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1658 toolbar_item_t *itm = tbitem;
1659 assert(win32 != 0);
1660 while(itm)
1662 put_word(res, itm->id);
1663 itm = itm->next;
1669 *****************************************************************************
1670 * Function : toolbar2res
1671 * Syntax : res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1672 * Input :
1673 * name - Name/ordinal of the resource
1674 * toolbar - The toolbar descriptor
1675 * Output : New .res format structure
1676 * Description :
1677 * Remarks :
1678 *****************************************************************************
1680 static res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1682 int restag;
1683 res_t *res;
1684 assert(name != NULL);
1685 assert(toolbar != NULL);
1687 res = new_res();
1688 if(win32)
1690 restag = put_res_header(res, WRC_RT_TOOLBAR, NULL, name, toolbar->memopt, &(toolbar->lvc));
1692 put_word(res, 1); /* Menuheader: Version */
1693 put_word(res, toolbar->button_width); /* (in pixels?) */
1694 put_word(res, toolbar->button_height); /* (in pixels?) */
1695 put_word(res, toolbar->nitems);
1696 put_pad(res);
1697 toolbaritem2res(res, toolbar->items);
1698 /* Set ResourceSize */
1699 SetResSize(res, restag);
1700 put_pad(res);
1702 else /* win16 */
1704 /* Do not generate anything in 16-bit mode */
1705 free(res->data);
1706 free(res);
1707 return NULL;
1709 return res;
1713 *****************************************************************************
1714 * Function : dlginit2res
1715 * Syntax : res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1716 * Input :
1717 * name - Name/ordinal of the resource
1718 * rdt - The dlginit descriptor
1719 * Output : New .res format structure
1720 * Description :
1721 * Remarks :
1722 *****************************************************************************
1724 static res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1726 int restag;
1727 res_t *res;
1728 assert(name != NULL);
1729 assert(dit != NULL);
1731 res = new_res();
1732 restag = put_res_header(res, WRC_RT_DLGINIT, NULL, name, dit->memopt, &(dit->data->lvc));
1733 put_raw_data(res, dit->data, 0);
1734 /* Set ResourceSize */
1735 SetResSize(res, restag);
1736 if(win32)
1737 put_pad(res);
1738 return res;
1742 *****************************************************************************
1743 * Function : prep_nid_for_label
1744 * Syntax : char *prep_nid_for_label(const name_id_t *nid)
1745 * Input :
1746 * Output :
1747 * Description : Converts a resource name into the first 32 (or less)
1748 * characters of the name with conversions.
1749 * Remarks :
1750 *****************************************************************************
1752 #define MAXNAMELEN 32
1753 char *prep_nid_for_label(const name_id_t *nid)
1755 static char buf[MAXNAMELEN+1];
1757 assert(nid != NULL);
1759 if(nid->type == name_str && nid->name.s_name->type == str_unicode)
1761 WCHAR *sptr;
1762 int i;
1763 sptr = nid->name.s_name->str.wstr;
1764 buf[0] = '\0';
1765 for(i = 0; *sptr && i < MAXNAMELEN; i++)
1767 if((unsigned)*sptr < 0x80 && isprint(*sptr & 0xff))
1768 buf[i] = *sptr++;
1769 else
1770 warning("Resourcename (str_unicode) contain unprintable characters or invalid translation, ignored\n");
1772 buf[i] = '\0';
1774 else if(nid->type == name_str && nid->name.s_name->type == str_char)
1776 char *cptr;
1777 int i;
1778 cptr = nid->name.s_name->str.cstr;
1779 buf[0] = '\0';
1780 for(i = 0; *cptr && i < MAXNAMELEN; i++)
1782 if((unsigned)*cptr < 0x80 && isprint(*cptr & 0xff))
1783 buf[i] = *cptr++;
1784 else
1785 warning("Resourcename (str_char) contain unprintable characters, ignored\n");
1787 buf[i] = '\0';
1789 else if(nid->type == name_ord)
1791 sprintf(buf, "%u", nid->name.i_name);
1793 else
1795 internal_error(__FILE__, __LINE__, "Resource name_id with invalid type %d\n", nid->type);
1797 return buf;
1799 #undef MAXNAMELEN
1802 *****************************************************************************
1803 * Function : make_c_name
1804 * Syntax : char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
1805 * Input :
1806 * Output :
1807 * Description : Converts a resource name into a valid c-identifier in the
1808 * form "_base_nid".
1809 * Remarks :
1810 *****************************************************************************
1812 char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
1814 char *buf = prep_nid_for_label(nid);
1815 return strmake( "_%s_%s_%d", base, buf, lan ? MAKELANGID(lan->id, lan->sub) : 0);
1819 *****************************************************************************
1820 * Function : get_c_typename
1821 * Syntax : const char *get_c_typename(enum res_e type)
1822 * Input :
1823 * Output :
1824 * Description : Convert resource enum to char string to be used in c-name
1825 * creation.
1826 * Remarks :
1827 *****************************************************************************
1829 const char *get_c_typename(enum res_e type)
1831 switch(type)
1833 case res_acc: return "Acc";
1834 case res_anicur:return "AniCur";
1835 case res_aniico:return "AniIco";
1836 case res_bmp: return "Bmp";
1837 case res_cur: return "Cur";
1838 case res_curg: return "CurGrp";
1839 case res_dlg: return "Dlg";
1840 case res_fnt: return "Fnt";
1841 case res_fntdir:return "FntDir";
1842 case res_ico: return "Ico";
1843 case res_icog: return "IcoGrp";
1844 case res_men: return "Men";
1845 case res_rdt: return "RCDat";
1846 case res_stt: return "StrTab";
1847 case res_usr: return "Usr";
1848 case res_msg: return "MsgTab";
1849 case res_ver: return "VerInf";
1850 case res_toolbar: return "TlBr";
1851 case res_dlginit: return "DlgInit";
1852 default: return "Oops";
1857 *****************************************************************************
1858 * Function : resources2res
1859 * Syntax : void resources2res(resource_t *top)
1860 * Input :
1861 * top - The resource-tree to convert
1862 * Output :
1863 * Description : Convert logical resource descriptors into binary data
1864 * Remarks :
1865 *****************************************************************************
1867 void resources2res(resource_t *top)
1869 while(top)
1871 switch(top->type)
1873 case res_acc:
1874 if(!top->binres)
1875 top->binres = accelerator2res(top->name, top->res.acc);
1876 break;
1877 case res_bmp:
1878 if(!top->binres)
1879 top->binres = bitmap2res(top->name, top->res.bmp);
1880 break;
1881 case res_cur:
1882 if(!top->binres)
1883 top->binres = cursor2res(top->res.cur);
1884 break;
1885 case res_curg:
1886 if(!top->binres)
1887 top->binres = cursorgroup2res(top->name, top->res.curg);
1888 break;
1889 case res_dlg:
1890 if(!top->binres)
1891 top->binres = dialog2res(top->name, top->res.dlg);
1892 break;
1893 case res_fnt:
1894 if(!top->binres)
1895 top->binres = font2res(top->name, top->res.fnt);
1896 break;
1897 case res_fntdir:
1898 if(!top->binres)
1899 top->binres = fontdir2res(top->name, top->res.fnd);
1900 break;
1901 case res_ico:
1902 if(!top->binres)
1903 top->binres = icon2res(top->res.ico);
1904 break;
1905 case res_icog:
1906 if(!top->binres)
1907 top->binres = icongroup2res(top->name, top->res.icog);
1908 break;
1909 case res_men:
1910 if(!top->binres)
1911 top->binres = menu2res(top->name, top->res.men);
1912 break;
1913 case res_html:
1914 if(!top->binres)
1915 top->binres = html2res(top->name, top->res.html);
1916 break;
1917 case res_rdt:
1918 if(!top->binres)
1919 top->binres = rcdata2res(top->name, top->res.rdt);
1920 break;
1921 case res_stt:
1922 if(!top->binres)
1923 top->binres = stringtable2res(top->res.stt);
1924 break;
1925 case res_usr:
1926 if(!top->binres)
1927 top->binres = user2res(top->name, top->res.usr);
1928 break;
1929 case res_msg:
1930 if(!top->binres)
1931 top->binres = messagetable2res(top->name, top->res.msg);
1932 break;
1933 case res_ver:
1934 if(!top->binres)
1935 top->binres = versioninfo2res(top->name, top->res.ver);
1936 break;
1937 case res_toolbar:
1938 if(!top->binres)
1939 top->binres = toolbar2res(top->name, top->res.tbt);
1940 break;
1941 case res_dlginit:
1942 if(!top->binres)
1943 top->binres = dlginit2res(top->name, top->res.dlgi);
1944 break;
1945 case res_anicur:
1946 case res_aniico:
1947 if(!top->binres)
1948 top->binres = anicurico2res(top->name, top->res.ani, top->type);
1949 break;
1950 default:
1951 internal_error(__FILE__, __LINE__, "Unknown resource type encountered %d in binary res generation\n", top->type);
1953 top->c_name = make_c_name(get_c_typename(top->type), top->name, top->lan);
1954 top = top->next;