msvcp: Fixed complex division.
[wine.git] / tools / wrc / genres.c
blob61b2b29097de804fd2408ec8d7493cc9b4299052
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"
45 #define SetResSize(res, tag) set_dword((res), (tag), (res)->size - get_dword((res), (tag)))
47 res_t *new_res(void)
49 res_t *r;
50 r = xmalloc(sizeof(res_t));
51 r->allocsize = RES_BLOCKSIZE;
52 r->size = 0;
53 r->dataidx = 0;
54 r->data = xmalloc(RES_BLOCKSIZE);
55 return r;
58 res_t *grow_res(res_t *r, unsigned int add)
60 r->allocsize += add;
61 r->data = xrealloc(r->data, r->allocsize);
62 return r;
66 *****************************************************************************
67 * Function : put_byte
68 * put_word
69 * put_dword
70 * Syntax : void put_byte(res_t *res, unsigned c)
71 * void put_word(res_t *res, unsigned w)
72 * void put_dword(res_t *res, unsigned d)
73 * Input :
74 * res - Binary resource to put the data in
75 * c, w, d - Data to put
76 * Output : nop
77 * Description : Put primitives that put an item in the binary resource.
78 * The data array grows automatically.
79 * Remarks :
80 *****************************************************************************
82 void put_byte(res_t *res, unsigned c)
84 if(res->allocsize - res->size < sizeof(char))
85 grow_res(res, RES_BLOCKSIZE);
86 res->data[res->size] = (char)c;
87 res->size += sizeof(char);
90 void put_word(res_t *res, unsigned w)
92 if(res->allocsize - res->size < sizeof(WORD))
93 grow_res(res, RES_BLOCKSIZE);
94 switch(byteorder)
96 #ifdef WORDS_BIGENDIAN
97 default:
98 #endif
99 case WRC_BO_BIG:
100 res->data[res->size+0] = HIBYTE(w);
101 res->data[res->size+1] = LOBYTE(w);
102 break;
104 #ifndef WORDS_BIGENDIAN
105 default:
106 #endif
107 case WRC_BO_LITTLE:
108 res->data[res->size+1] = HIBYTE(w);
109 res->data[res->size+0] = LOBYTE(w);
110 break;
112 res->size += sizeof(WORD);
115 void put_dword(res_t *res, unsigned d)
117 if(res->allocsize - res->size < sizeof(DWORD))
118 grow_res(res, RES_BLOCKSIZE);
119 switch(byteorder)
121 #ifdef WORDS_BIGENDIAN
122 default:
123 #endif
124 case WRC_BO_BIG:
125 res->data[res->size+0] = HIBYTE(HIWORD(d));
126 res->data[res->size+1] = LOBYTE(HIWORD(d));
127 res->data[res->size+2] = HIBYTE(LOWORD(d));
128 res->data[res->size+3] = LOBYTE(LOWORD(d));
129 break;
131 #ifndef WORDS_BIGENDIAN
132 default:
133 #endif
134 case WRC_BO_LITTLE:
135 res->data[res->size+3] = HIBYTE(HIWORD(d));
136 res->data[res->size+2] = LOBYTE(HIWORD(d));
137 res->data[res->size+1] = HIBYTE(LOWORD(d));
138 res->data[res->size+0] = LOBYTE(LOWORD(d));
139 break;
141 res->size += sizeof(DWORD);
144 static void put_pad(res_t *res)
146 while(res->size & 0x3)
147 put_byte(res, 0);
151 *****************************************************************************
152 * Function : set_word
153 * set_dword
154 * Syntax : void set_word(res_t *res, int ofs, unsigned w)
155 * void set_dword(res_t *res, int ofs, unsigned d)
156 * Input :
157 * res - Binary resource to put the data in
158 * ofs - Byte offset in data-array
159 * w, d - Data to put
160 * Output : nop
161 * Description : Set the value of a binary resource data array to a
162 * specific value.
163 * Remarks :
164 *****************************************************************************
166 static void set_word(res_t *res, int ofs, unsigned w)
168 switch(byteorder)
170 #ifdef WORDS_BIGENDIAN
171 default:
172 #endif
173 case WRC_BO_BIG:
174 res->data[ofs+0] = HIBYTE(w);
175 res->data[ofs+1] = LOBYTE(w);
176 break;
178 #ifndef WORDS_BIGENDIAN
179 default:
180 #endif
181 case WRC_BO_LITTLE:
182 res->data[ofs+1] = HIBYTE(w);
183 res->data[ofs+0] = LOBYTE(w);
184 break;
188 static void set_dword(res_t *res, int ofs, unsigned d)
190 switch(byteorder)
192 #ifdef WORDS_BIGENDIAN
193 default:
194 #endif
195 case WRC_BO_BIG:
196 res->data[ofs+0] = HIBYTE(HIWORD(d));
197 res->data[ofs+1] = LOBYTE(HIWORD(d));
198 res->data[ofs+2] = HIBYTE(LOWORD(d));
199 res->data[ofs+3] = LOBYTE(LOWORD(d));
200 break;
202 #ifndef WORDS_BIGENDIAN
203 default:
204 #endif
205 case WRC_BO_LITTLE:
206 res->data[ofs+3] = HIBYTE(HIWORD(d));
207 res->data[ofs+2] = LOBYTE(HIWORD(d));
208 res->data[ofs+1] = HIBYTE(LOWORD(d));
209 res->data[ofs+0] = LOBYTE(LOWORD(d));
210 break;
215 *****************************************************************************
216 * Function : get_dword
217 * Input :
218 * res - Binary resource to put the data in
219 * ofs - Byte offset in data-array
220 * Output : The data in native endian
221 * Description : Get the value of a binary resource data array in native
222 * endian.
223 * Remarks :
224 *****************************************************************************
226 static DWORD get_dword(res_t *res, int ofs)
228 switch(byteorder)
230 #ifdef WORDS_BIGENDIAN
231 default:
232 #endif
233 case WRC_BO_BIG:
234 return (res->data[ofs+0] << 24)
235 | (res->data[ofs+1] << 16)
236 | (res->data[ofs+2] << 8)
237 | res->data[ofs+3];
239 #ifndef WORDS_BIGENDIAN
240 default:
241 #endif
242 case WRC_BO_LITTLE:
243 return (res->data[ofs+3] << 24)
244 | (res->data[ofs+2] << 16)
245 | (res->data[ofs+1] << 8)
246 | res->data[ofs+0];
251 *****************************************************************************
252 * Function : string_to_upper
253 * Syntax : void string_to_upper(string_t *str)
254 * Input :
255 * Output :
256 * Description :
257 * Remarks : FIXME: codepages...
258 *****************************************************************************
260 static void string_to_upper(string_t *str)
262 int i;
264 if(str->type == str_char)
266 for (i = 0; i < str->size; i++) str->str.cstr[i] = toupper((unsigned char)str->str.cstr[i]);
268 else if(str->type == str_unicode)
270 for (i = 0; i < str->size; i++) str->str.wstr[i] = toupperW(str->str.wstr[i]);
272 else
274 internal_error(__FILE__, __LINE__, "Invalid string type %d\n", str->type);
278 static int parse_accel_string( const string_t *key, int flags )
280 int keycode;
282 if(key->type == str_char)
284 if (key->str.cstr[0] == '#') return 0; /* ignore message contexts */
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\n");
293 if(key->str.cstr[0] == '^' && (flags & WRC_AF_CONTROL) != 0)
295 print_location( &key->loc );
296 error("Cannot use both '^' and CONTROL modifier\n");
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\n");
307 else
308 keycode = key->str.cstr[0];
310 else
312 if (key->str.wstr[0] == '#') return 0; /* ignore message contexts */
313 if((flags & WRC_AF_VIRTKEY) &&
314 !((key->str.wstr[0] >= 'A' && key->str.wstr[0] <= 'Z') ||
315 (key->str.wstr[0] >= '0' && key->str.wstr[0] <= '9')))
317 print_location( &key->loc );
318 error("VIRTKEY code is not equal to ascii value\n");
320 if(key->str.wstr[0] == '^' && (flags & WRC_AF_CONTROL) != 0)
322 print_location( &key->loc );
323 error("Cannot use both '^' and CONTROL modifier\n");
325 else if(key->str.wstr[0] == '^')
327 keycode = toupperW(key->str.wstr[1]) - '@';
328 if(keycode >= ' ')
330 print_location( &key->loc );
331 error("Control-code out of range\n");
334 else
335 keycode = key->str.wstr[0];
337 return keycode;
341 *****************************************************************************
342 * Function : put_string
343 * Syntax : void put_string(res_t *res, string_t *str, enum str_e type,
344 * int isterm, const language_t *lang)
345 * Input :
346 * res - Binary resource to put the data in
347 * str - String to put
348 * type - Data has to be written in either str_char or str_unicode
349 * isterm - The string is '\0' terminated (disregard the string's
350 * size member)
351 * Output : nop
352 * Description :
353 * Remarks :
354 *****************************************************************************
356 static void put_string(res_t *res, const string_t *str, enum str_e type, int isterm,
357 const language_t *lang)
359 int cnt, codepage;
360 string_t *newstr;
362 assert(res != NULL);
363 assert(str != NULL);
365 if (lang) codepage = get_language_codepage( lang->id, lang->sub );
366 else codepage = get_language_codepage( 0, 0 );
368 assert( codepage != -1 );
370 newstr = convert_string(str, type, codepage);
371 if (type == str_unicode)
373 if (str->type == str_char)
375 if (!check_unicode_conversion( str, newstr, codepage ))
377 print_location( &str->loc );
378 error( "String %s does not convert identically to Unicode and back in codepage %d. "
379 "Try using a Unicode string instead\n", str->str.cstr, codepage );
381 if (check_valid_utf8( str, codepage ))
383 print_location( &str->loc );
384 warning( "string \"%s\" seems to be UTF-8 but codepage %u is in use.\n",
385 str->str.cstr, codepage );
388 if (!isterm) put_word(res, newstr->size);
389 for(cnt = 0; cnt < newstr->size; cnt++)
391 WCHAR c = newstr->str.wstr[cnt];
392 if (isterm && !c) break;
393 put_word(res, c);
395 if (isterm) put_word(res, 0);
397 else /* str_char */
399 if (!isterm) put_byte(res, newstr->size);
400 for(cnt = 0; cnt < newstr->size; cnt++)
402 char c = newstr->str.cstr[cnt];
403 if (isterm && !c) break;
404 put_byte(res, c);
406 if (isterm) put_byte(res, 0);
408 free_string(newstr);
412 *****************************************************************************
413 * Function : put_name_id
414 * Syntax : void put_name_id(res_t *res, name_id_t *nid, int upcase, const language_t *lang)
415 * Input :
416 * Output :
417 * Description :
418 * Remarks :
419 *****************************************************************************
421 static void put_name_id(res_t *res, name_id_t *nid, int upcase, const language_t *lang)
423 if(nid->type == name_ord)
425 if(win32)
426 put_word(res, 0xffff);
427 else
428 put_byte(res, 0xff);
429 put_word(res, (WORD)nid->name.i_name);
431 else if(nid->type == name_str)
433 if(upcase)
434 string_to_upper(nid->name.s_name);
435 put_string(res, nid->name.s_name, win32 ? str_unicode : str_char, TRUE, lang);
437 else
439 internal_error(__FILE__, __LINE__, "Invalid name_id type %d\n", nid->type);
444 *****************************************************************************
445 * Function : put_lvc
446 * Syntax : void put_lvc(res_t *res, lvc_t *lvc)
447 * Input :
448 * Output :
449 * Description :
450 * Remarks :
451 *****************************************************************************
453 static void put_lvc(res_t *res, lvc_t *lvc)
455 if(lvc && lvc->language)
456 put_word(res, MAKELANGID(lvc->language->id, lvc->language->sub));
457 else
458 put_word(res, 0); /* Neutral */
459 if(lvc && lvc->version)
460 put_dword(res, *(lvc->version));
461 else
462 put_dword(res, 0);
463 if(lvc && lvc->characts)
464 put_dword(res, *(lvc->characts));
465 else
466 put_dword(res, 0);
470 *****************************************************************************
471 * Function : put_raw_data
472 * Syntax : void put_raw_data(res_t *res, raw_data_t *raw, int offset)
473 * Input :
474 * Output :
475 * Description :
476 * Remarks :
477 *****************************************************************************
479 static void put_raw_data(res_t *res, raw_data_t *raw, int offset)
481 unsigned int wsize = raw->size - offset;
482 if(res->allocsize - res->size < wsize)
483 grow_res(res, wsize);
484 memcpy(&(res->data[res->size]), raw->data + offset, wsize);
485 res->size += wsize;
489 *****************************************************************************
490 * Function : put_res_header
491 * Syntax : input_res_header(res_t *res, int type, name_id_t *ntype,
492 * name_id_t *name, DWORD memopt, lvc_t *lvc)
494 * Input :
495 * res - Binary resource descriptor to write to
496 * type - Resource identifier (if ntype == NULL)
497 * ntype - Name id of type
498 * name - Resource's name
499 * memopt - Resource's memory options to write
500 * lvc - Language, version and characteristics (win32 only)
501 * Output : An index to the resource size field. The resource size field
502 * contains the header size upon exit.
503 * Description :
504 * Remarks :
505 *****************************************************************************
507 static int put_res_header(res_t *res, int type, name_id_t *ntype, name_id_t *name,
508 DWORD memopt, lvc_t *lvc)
510 if(win32)
512 put_dword(res, 0); /* We will overwrite these later */
513 put_dword(res, 0);
514 if(!ntype)
516 put_word(res, 0xffff); /* ResType */
517 put_word(res, type);
519 else
520 put_name_id(res, ntype, TRUE, lvc->language);
521 put_name_id(res, name, TRUE, lvc->language); /* ResName */
522 put_pad(res);
523 put_dword(res, 0); /* DataVersion */
524 put_word(res, memopt); /* Memory options */
525 put_lvc(res, lvc); /* Language, version and characts */
526 set_dword(res, 0*sizeof(DWORD), res->size); /* Set preliminary resource */
527 set_dword(res, 1*sizeof(DWORD), res->size); /* Set HeaderSize */
528 res->dataidx = res->size;
529 return 0;
531 else /* win16 */
533 int tag;
534 if(!ntype)
536 put_byte(res, 0xff); /* ResType */
537 put_word(res, type);
539 else
540 put_name_id(res, ntype, TRUE, NULL);
541 put_name_id(res, name, TRUE, NULL); /* ResName */
542 put_word(res, memopt); /* Memory options */
543 tag = res->size;
544 put_dword(res, 0); /* ResSize overwritten later*/
545 set_dword(res, tag, res->size);
546 res->dataidx = res->size;
547 return tag;
552 *****************************************************************************
553 * Function : accelerator2res
554 * Syntax : res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
555 * Input :
556 * name - Name/ordinal of the resource
557 * acc - The accelerator descriptor
558 * Output : New .res format structure
559 * Description :
560 * Remarks :
561 *****************************************************************************
563 static res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
565 int restag;
566 res_t *res;
567 event_t *ev;
568 assert(name != NULL);
569 assert(acc != NULL);
571 ev = acc->events;
572 res = new_res();
573 if(win32)
575 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, &(acc->lvc));
576 while(ev)
578 int key = ev->key;
579 if (ev->str) key = parse_accel_string( ev->str, ev->flags );
580 put_word(res, ev->flags | (ev->next ? 0 : 0x80));
581 put_word(res, key);
582 put_word(res, ev->id);
583 put_word(res, 0); /* Padding */
584 ev = ev->next;
586 put_pad(res);
588 else /* win16 */
590 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, NULL);
591 while(ev)
593 int key = ev->key;
594 if (ev->str) key = parse_accel_string( ev->str, ev->flags );
595 put_byte(res, ev->flags | (ev->next ? 0 : 0x80));
596 put_word(res, key);
597 put_word(res, ev->id);
598 ev = ev->next;
601 /* Set ResourceSize */
602 SetResSize(res, restag);
603 return res;
607 *****************************************************************************
608 * Function : dialog2res
609 * Syntax : res_t *dialog2res(name_id_t *name, dialog_t *dlg)
610 * Input :
611 * name - Name/ordinal of the resource
612 * dlg - The dialog descriptor
613 * Output : New .res format structure
614 * Description :
615 * Remarks :
616 *****************************************************************************
618 static res_t *dialog2res(name_id_t *name, dialog_t *dlg)
620 int restag;
621 res_t *res;
622 control_t *ctrl;
623 int tag_nctrl;
624 int nctrl = 0;
625 assert(name != NULL);
626 assert(dlg != NULL);
628 ctrl = dlg->controls;
629 res = new_res();
630 if(win32)
632 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, &(dlg->lvc));
634 if (dlg->is_ex)
636 /* FIXME: MS doc says that the first word must contain 0xffff
637 * and the second 0x0001 to signal a DLGTEMPLATEEX. Borland's
638 * compiler reverses the two words.
639 * I don't know which one to choose, but I write it as Mr. B
640 * writes it.
642 put_word(res, 1); /* Signature */
643 put_word(res, 0xffff); /* DlgVer */
644 put_dword(res, dlg->gothelpid ? dlg->helpid : 0);
645 put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
646 put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
648 else
650 put_dword(res, dlg->style->or_mask);
651 put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
653 tag_nctrl = res->size;
654 put_word(res, 0); /* Number of controls */
655 put_word(res, dlg->x);
656 put_word(res, dlg->y);
657 put_word(res, dlg->width);
658 put_word(res, dlg->height);
659 if(dlg->menu)
660 put_name_id(res, dlg->menu, TRUE, dlg->lvc.language);
661 else
662 put_word(res, 0);
663 if(dlg->dlgclass)
664 put_name_id(res, dlg->dlgclass, TRUE, dlg->lvc.language);
665 else
666 put_word(res, 0);
667 if(dlg->title)
668 put_string(res, dlg->title, str_unicode, TRUE, dlg->lvc.language);
669 else
670 put_word(res, 0);
671 if(dlg->font)
673 put_word(res, dlg->font->size);
674 if (dlg->is_ex)
676 put_word(res, dlg->font->weight);
677 /* FIXME: ? TRUE should be sufficient to say that it's
678 * italic, but Borland's compiler says it's 0x0101.
679 * I just copy it here, and hope for the best.
681 put_word(res, dlg->font->italic ? 0x0101 : 0);
683 put_string(res, dlg->font->name, str_unicode, TRUE, dlg->lvc.language);
686 put_pad(res);
687 while(ctrl)
689 if (dlg->is_ex)
691 put_dword(res, ctrl->gothelpid ? ctrl->helpid : 0);
692 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
693 /* FIXME: what is default control style? */
694 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask : WS_CHILD | WS_VISIBLE);
696 else
698 /* FIXME: what is default control style? */
699 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
700 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
702 put_word(res, ctrl->x);
703 put_word(res, ctrl->y);
704 put_word(res, ctrl->width);
705 put_word(res, ctrl->height);
706 if (dlg->is_ex)
707 put_dword(res, ctrl->id);
708 else
709 put_word(res, ctrl->id);
710 if(ctrl->ctlclass)
711 put_name_id(res, ctrl->ctlclass, TRUE, dlg->lvc.language);
712 else
713 internal_error(__FILE__, __LINE__, "Control has no control-class\n");
714 if(ctrl->title)
715 put_name_id(res, ctrl->title, FALSE, dlg->lvc.language);
716 else
717 put_word(res, 0);
718 if(ctrl->extra)
720 put_word(res, ctrl->extra->size+2);
721 put_pad(res);
722 put_raw_data(res, ctrl->extra, 0);
724 else
725 put_word(res, 0);
727 if(ctrl->next)
728 put_pad(res);
729 nctrl++;
730 ctrl = ctrl->next;
732 /* Set number of controls */
733 set_word(res, tag_nctrl, (WORD)nctrl);
735 else /* win16 */
737 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, NULL);
739 put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
740 tag_nctrl = res->size;
741 put_byte(res, 0); /* Number of controls */
742 put_word(res, dlg->x);
743 put_word(res, dlg->y);
744 put_word(res, dlg->width);
745 put_word(res, dlg->height);
746 if(dlg->menu)
747 put_name_id(res, dlg->menu, TRUE, NULL);
748 else
749 put_byte(res, 0);
750 if(dlg->dlgclass)
751 put_name_id(res, dlg->dlgclass, TRUE, NULL);
752 else
753 put_byte(res, 0);
754 if(dlg->title)
755 put_string(res, dlg->title, str_char, TRUE, NULL);
756 else
757 put_byte(res, 0);
758 if(dlg->font)
760 put_word(res, dlg->font->size);
761 put_string(res, dlg->font->name, str_char, TRUE, NULL);
764 while(ctrl)
766 put_word(res, ctrl->x);
767 put_word(res, ctrl->y);
768 put_word(res, ctrl->width);
769 put_word(res, ctrl->height);
770 put_word(res, ctrl->id);
771 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
772 if(ctrl->ctlclass)
774 if(ctrl->ctlclass->type == name_ord
775 && ctrl->ctlclass->name.i_name >= 0x80
776 && ctrl->ctlclass->name.i_name <= 0x85)
777 put_byte(res, ctrl->ctlclass->name.i_name);
778 else if(ctrl->ctlclass->type == name_str)
779 put_name_id(res, ctrl->ctlclass, FALSE, NULL);
780 else
781 error("Unknown control-class %04x\n", ctrl->ctlclass->name.i_name);
783 else
784 internal_error(__FILE__, __LINE__, "Control has no control-class\n");
785 if(ctrl->title)
786 put_name_id(res, ctrl->title, FALSE, NULL);
787 else
788 put_byte(res, 0);
790 /* FIXME: What is this extra byte doing here? */
791 put_byte(res, 0);
793 nctrl++;
794 ctrl = ctrl->next;
796 /* Set number of controls */
797 ((char *)res->data)[tag_nctrl] = (char)nctrl;
799 /* Set ResourceSize */
800 SetResSize(res, restag);
801 return res;
805 *****************************************************************************
806 * Function : menuitem2res
807 * Syntax : void menuitem2res(res_t *res, menu_item_t *item)
808 * Input :
809 * Output :
810 * Description :
811 * Remarks : Self recursive
812 *****************************************************************************
814 static void menuitem2res(res_t *res, menu_item_t *menitem, const language_t *lang)
816 menu_item_t *itm = menitem;
817 if(win32)
819 while(itm)
821 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
822 if(!itm->popup)
823 put_word(res, itm->id);
824 if(itm->name)
825 put_string(res, itm->name, str_unicode, TRUE, lang);
826 else
827 put_word(res, 0);
828 if(itm->popup)
829 menuitem2res(res, itm->popup, lang);
830 itm = itm->next;
833 else /* win16 */
835 while(itm)
837 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
838 if(!itm->popup)
839 put_word(res, itm->id);
840 if(itm->name)
841 put_string(res, itm->name, str_char, TRUE, lang);
842 else
843 put_byte(res, 0);
844 if(itm->popup)
845 menuitem2res(res, itm->popup, lang);
846 itm = itm->next;
853 *****************************************************************************
854 * Function : menuexitem2res
855 * Syntax : void menuexitem2res(res_t *res, menuex_item_t *item)
856 * Input :
857 * Output : nop
858 * Description :
859 * Remarks : Self recursive
860 *****************************************************************************
862 static void menuexitem2res(res_t *res, menu_item_t *menitem, const language_t *lang)
864 menu_item_t *itm = menitem;
865 assert(win32 != 0);
866 while(itm)
868 put_dword(res, itm->gottype ? itm->type : 0);
869 put_dword(res, itm->gotstate ? itm->state : 0);
870 put_dword(res, itm->gotid ? itm->id : 0); /* FIXME: Docu. says word */
871 put_word(res, (itm->popup ? 0x01 : 0) | (!itm->next ? MF_END : 0));
872 if(itm->name)
873 put_string(res, itm->name, str_unicode, TRUE, lang);
874 else
875 put_word(res, 0);
876 put_pad(res);
877 if(itm->popup)
879 put_dword(res, itm->gothelpid ? itm->helpid : 0);
880 menuexitem2res(res, itm->popup, lang);
882 itm = itm->next;
888 *****************************************************************************
889 * Function : menu2res
890 * Syntax : res_t *menu2res(name_id_t *name, menu_t *men)
891 * Input :
892 * name - Name/ordinal of the resource
893 * menex - The menuex descriptor
894 * Output : New .res format structure
895 * Description :
896 * Remarks :
897 *****************************************************************************
899 static res_t *menu2res(name_id_t *name, menu_t *men)
901 int restag;
902 res_t *res;
903 assert(name != NULL);
904 assert(men != NULL);
906 res = new_res();
907 if(win32)
909 restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, &(men->lvc));
911 if (men->is_ex)
913 put_word(res, 1); /* Menuheader: Version */
914 put_word(res, 4); /* Offset */
915 put_dword(res, 0); /* HelpId */
916 put_pad(res);
917 menuexitem2res(res, men->items, men->lvc.language);
919 else
921 put_dword(res, 0); /* Menuheader: Version and HeaderSize */
922 menuitem2res(res, men->items, men->lvc.language);
924 /* Set ResourceSize */
925 SetResSize(res, restag);
926 put_pad(res);
928 else /* win16 */
930 restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, NULL);
932 put_dword(res, 0); /* Menuheader: Version and HeaderSize */
933 menuitem2res(res, men->items, NULL);
934 /* Set ResourceSize */
935 SetResSize(res, restag);
937 return res;
941 *****************************************************************************
942 * Function : cursorgroup2res
943 * Syntax : res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
944 * Input :
945 * name - Name/ordinal of the resource
946 * curg - The cursor descriptor
947 * Output : New .res format structure
948 * Description :
949 * Remarks :
950 *****************************************************************************
952 static res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
954 int restag;
955 res_t *res;
956 cursor_t *cur;
957 assert(name != NULL);
958 assert(curg != NULL);
960 res = new_res();
961 restag = put_res_header(res, WRC_RT_GROUP_CURSOR, NULL, name, curg->memopt, &(curg->lvc));
962 if(win32)
964 put_word(res, 0); /* Reserved */
965 /* FIXME: The ResType in the NEWHEADER structure should
966 * contain 14 according to the MS win32 doc. This is
967 * not the case with the BRC compiler and I really doubt
968 * the latter. Putting one here is compliant to win16 spec,
969 * but who knows the true value?
971 put_word(res, 2); /* ResType */
972 put_word(res, curg->ncursor);
973 #if 0
974 for(cur = curg->cursorlist; cur; cur = cur->next)
975 #else
976 cur = curg->cursorlist;
977 while(cur->next)
978 cur = cur->next;
979 for(; cur; cur = cur->prev)
980 #endif
982 put_word(res, cur->width);
983 /* FIXME: The height of a cursor is half the size of
984 * the bitmap's height. BRC puts the height from the
985 * BITMAPINFOHEADER here instead of the cursorfile's
986 * height. MS doesn't seem to care...
988 put_word(res, cur->height);
989 /* FIXME: The next two are reversed in BRC and I don't
990 * know why. Probably a bug. But, we can safely ignore
991 * it because win16 does not support color cursors.
992 * A warning should have been generated by the parser.
994 put_word(res, cur->planes);
995 put_word(res, cur->bits);
996 /* FIXME: The +4 is the hotspot in the cursor resource.
997 * However, I could not find this in the documentation.
998 * The hotspot bytes must either be included or MS
999 * doesn't care.
1001 put_dword(res, cur->data->size +4);
1002 put_word(res, cur->id);
1005 else /* win16 */
1007 put_word(res, 0); /* Reserved */
1008 put_word(res, 2); /* ResType */
1009 put_word(res, curg->ncursor);
1010 #if 0
1011 for(cur = curg->cursorlist; cur; cur = cur->next)
1012 #else
1013 cur = curg->cursorlist;
1014 while(cur->next)
1015 cur = cur->next;
1016 for(; cur; cur = cur->prev)
1017 #endif
1019 put_word(res, cur->width);
1020 /* FIXME: The height of a cursor is half the size of
1021 * the bitmap's height. BRC puts the height from the
1022 * BITMAPINFOHEADER here instead of the cursorfile's
1023 * height. MS doesn't seem to care...
1025 put_word(res, cur->height);
1026 /* FIXME: The next two are reversed in BRC and I don't
1027 * know why. Probably a bug. But, we can safely ignore
1028 * it because win16 does not support color cursors.
1029 * A warning should have been generated by the parser.
1031 put_word(res, cur->planes);
1032 put_word(res, cur->bits);
1033 /* FIXME: The +4 is the hotspot in the cursor resource.
1034 * However, I could not find this in the documentation.
1035 * The hotspot bytes must either be included or MS
1036 * doesn't care.
1038 put_dword(res, cur->data->size +4);
1039 put_word(res, cur->id);
1042 SetResSize(res, restag); /* Set ResourceSize */
1043 if(win32)
1044 put_pad(res);
1046 return res;
1050 *****************************************************************************
1051 * Function : cursor2res
1052 * Syntax : res_t *cursor2res(cursor_t *cur)
1053 * Input :
1054 * cur - The cursor descriptor
1055 * Output : New .res format structure
1056 * Description :
1057 * Remarks :
1058 *****************************************************************************
1060 static res_t *cursor2res(cursor_t *cur)
1062 int restag;
1063 res_t *res;
1064 name_id_t name;
1066 assert(cur != NULL);
1068 res = new_res();
1069 name.type = name_ord;
1070 name.name.i_name = cur->id;
1071 restag = put_res_header(res, WRC_RT_CURSOR, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(cur->lvc));
1072 put_word(res, cur->xhot);
1073 put_word(res, cur->yhot);
1074 put_raw_data(res, cur->data, 0);
1076 SetResSize(res, restag); /* Set ResourceSize */
1077 if(win32)
1078 put_pad(res);
1080 return res;
1084 *****************************************************************************
1085 * Function : icongroup2res
1086 * Syntax : res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1087 * Input :
1088 * name - Name/ordinal of the resource
1089 * icog - The icon group descriptor
1090 * Output : New .res format structure
1091 * Description :
1092 * Remarks :
1093 *****************************************************************************
1095 static res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1097 int restag;
1098 res_t *res;
1099 icon_t *ico;
1100 assert(name != NULL);
1101 assert(icog != NULL);
1103 res = new_res();
1104 restag = put_res_header(res, WRC_RT_GROUP_ICON, NULL, name, icog->memopt, &(icog->lvc));
1105 if(win32)
1107 put_word(res, 0); /* Reserved */
1108 /* FIXME: The ResType in the NEWHEADER structure should
1109 * contain 14 according to the MS win32 doc. This is
1110 * not the case with the BRC compiler and I really doubt
1111 * the latter. Putting one here is compliant to win16 spec,
1112 * but who knows the true value?
1114 put_word(res, 1); /* ResType */
1115 put_word(res, icog->nicon);
1116 for(ico = icog->iconlist; ico; ico = ico->next)
1118 put_byte(res, ico->width);
1119 put_byte(res, ico->height);
1120 put_byte(res, ico->nclr);
1121 put_byte(res, 0); /* Reserved */
1122 put_word(res, ico->planes);
1123 put_word(res, ico->bits);
1124 put_dword(res, ico->data->size);
1125 put_word(res, ico->id);
1128 else /* win16 */
1130 put_word(res, 0); /* Reserved */
1131 put_word(res, 1); /* ResType */
1132 put_word(res, icog->nicon);
1133 for(ico = icog->iconlist; ico; ico = ico->next)
1135 put_byte(res, ico->width);
1136 put_byte(res, ico->height);
1137 put_byte(res, ico->nclr);
1138 put_byte(res, 0); /* Reserved */
1139 put_word(res, ico->planes);
1140 put_word(res, ico->bits);
1141 put_dword(res, ico->data->size);
1142 put_word(res, ico->id);
1145 SetResSize(res, restag); /* Set ResourceSize */
1146 if(win32)
1147 put_pad(res);
1149 return res;
1153 *****************************************************************************
1154 * Function : icon2res
1155 * Syntax : res_t *icon2res(icon_t *ico)
1156 * Input :
1157 * ico - The icon descriptor
1158 * Output : New .res format structure
1159 * Description :
1160 * Remarks :
1161 *****************************************************************************
1163 static res_t *icon2res(icon_t *ico)
1165 int restag;
1166 res_t *res;
1167 name_id_t name;
1169 assert(ico != NULL);
1171 res = new_res();
1172 name.type = name_ord;
1173 name.name.i_name = ico->id;
1174 restag = put_res_header(res, WRC_RT_ICON, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(ico->lvc));
1175 put_raw_data(res, ico->data, 0);
1177 SetResSize(res, restag); /* Set ResourceSize */
1178 if(win32)
1179 put_pad(res);
1181 return res;
1185 *****************************************************************************
1186 * Function : anicurico2res
1187 * Syntax : res_t *anicurico2res(name_id_t *name, ani_curico_t *ani)
1188 * Input :
1189 * name - Name/ordinal of the resource
1190 * ani - The animated object descriptor
1191 * Output : New .res format structure
1192 * Description :
1193 * Remarks : The endian of the object's structures have been converted
1194 * by the loader.
1195 * There are rumors that win311 could handle animated stuff.
1196 * That is why they are generated for both win16 and win32
1197 * compile.
1198 *****************************************************************************
1200 static res_t *anicurico2res(name_id_t *name, ani_curico_t *ani, enum res_e type)
1202 int restag;
1203 res_t *res;
1204 assert(name != NULL);
1205 assert(ani != NULL);
1207 res = new_res();
1208 restag = put_res_header(res, type == res_anicur ? WRC_RT_ANICURSOR : WRC_RT_ANIICON,
1209 NULL, name, ani->memopt, NULL);
1210 put_raw_data(res, ani->data, 0);
1211 /* Set ResourceSize */
1212 SetResSize(res, restag);
1213 if(win32)
1214 put_pad(res);
1215 return res;
1219 *****************************************************************************
1220 * Function : bitmap2res
1221 * Syntax : res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1222 * Input :
1223 * name - Name/ordinal of the resource
1224 * bmp - The bitmap descriptor
1225 * Output : New .res format structure
1226 * Description :
1227 * Remarks : The endian of the bitmap structures have been converted
1228 * by the loader.
1229 *****************************************************************************
1231 static res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1233 int restag;
1234 res_t *res;
1235 assert(name != NULL);
1236 assert(bmp != NULL);
1238 res = new_res();
1239 restag = put_res_header(res, WRC_RT_BITMAP, NULL, name, bmp->memopt, &(bmp->data->lvc));
1240 if(bmp->data->data[0] == 'B'
1241 && bmp->data->data[1] == 'M'
1242 && ((BITMAPFILEHEADER *)bmp->data->data)->bfSize == bmp->data->size
1243 && bmp->data->size >= sizeof(BITMAPFILEHEADER))
1245 /* The File header is still attached, don't write it */
1246 put_raw_data(res, bmp->data, sizeof(BITMAPFILEHEADER));
1248 else
1250 put_raw_data(res, bmp->data, 0);
1252 /* Set ResourceSize */
1253 SetResSize(res, restag);
1254 if(win32)
1255 put_pad(res);
1256 return res;
1260 *****************************************************************************
1261 * Function : font2res
1262 * Syntax : res_t *font2res(name_id_t *name, font_t *fnt)
1263 * Input :
1264 * name - Name/ordinal of the resource
1265 * fnt - The font descriptor
1266 * Output : New .res format structure
1267 * Description :
1268 * Remarks : The data has been prepared just after parsing.
1269 *****************************************************************************
1271 static res_t *font2res(name_id_t *name, font_t *fnt)
1273 int restag;
1274 res_t *res;
1275 assert(name != NULL);
1276 assert(fnt != NULL);
1278 res = new_res();
1279 restag = put_res_header(res, WRC_RT_FONT, NULL, name, fnt->memopt, &(fnt->data->lvc));
1280 put_raw_data(res, fnt->data, 0);
1281 /* Set ResourceSize */
1282 SetResSize(res, restag);
1283 if(win32)
1284 put_pad(res);
1285 return res;
1289 *****************************************************************************
1290 * Function : fontdir2res
1291 * Syntax : res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1292 * Input :
1293 * name - Name/ordinal of the resource
1294 * fntdir - The fontdir descriptor
1295 * Output : New .res format structure
1296 * Description :
1297 * Remarks : The data has been prepared just after parsing.
1298 *****************************************************************************
1300 static res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1302 int restag;
1303 res_t *res;
1304 assert(name != NULL);
1305 assert(fnd != NULL);
1307 res = new_res();
1308 restag = put_res_header(res, WRC_RT_FONTDIR, NULL, name, fnd->memopt, &(fnd->data->lvc));
1309 put_raw_data(res, fnd->data, 0);
1310 /* Set ResourceSize */
1311 SetResSize(res, restag);
1312 if(win32)
1313 put_pad(res);
1314 return res;
1318 *****************************************************************************
1319 * Function : html2res
1320 * Syntax : res_t *html2res(name_id_t *name, html_t *html)
1321 * Input :
1322 * name - Name/ordinal of the resource
1323 * rdt - The html descriptor
1324 * Output : New .res format structure
1325 * Description :
1326 * Remarks :
1327 *****************************************************************************
1329 static res_t *html2res(name_id_t *name, html_t *html)
1331 int restag;
1332 res_t *res;
1333 assert(name != NULL);
1334 assert(html != NULL);
1336 res = new_res();
1337 restag = put_res_header(res, WRC_RT_HTML, NULL, name, html->memopt, &(html->data->lvc));
1338 put_raw_data(res, html->data, 0);
1339 /* Set ResourceSize */
1340 SetResSize(res, restag);
1341 if(win32)
1342 put_pad(res);
1343 return res;
1347 *****************************************************************************
1348 * Function : rcdata2res
1349 * Syntax : res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1350 * Input :
1351 * name - Name/ordinal of the resource
1352 * rdt - The rcdata descriptor
1353 * Output : New .res format structure
1354 * Description :
1355 * Remarks :
1356 *****************************************************************************
1358 static res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1360 int restag;
1361 res_t *res;
1362 assert(name != NULL);
1363 assert(rdt != NULL);
1365 res = new_res();
1366 restag = put_res_header(res, WRC_RT_RCDATA, NULL, name, rdt->memopt, &(rdt->data->lvc));
1367 put_raw_data(res, rdt->data, 0);
1368 /* Set ResourceSize */
1369 SetResSize(res, restag);
1370 if(win32)
1371 put_pad(res);
1372 return res;
1376 *****************************************************************************
1377 * Function : messagetable2res
1378 * Syntax : res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1379 * Input :
1380 * name - Name/ordinal of the resource
1381 * msg - The messagetable descriptor
1382 * Output : New .res format structure
1383 * Description :
1384 * Remarks : The data has been converted to the appropriate endian
1385 * after it was parsed.
1386 *****************************************************************************
1388 static res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1390 int restag;
1391 res_t *res;
1392 assert(name != NULL);
1393 assert(msg != NULL);
1395 res = new_res();
1396 restag = put_res_header(res, WRC_RT_MESSAGETABLE, NULL, name, msg->memopt, &(msg->data->lvc));
1397 put_raw_data(res, msg->data, 0);
1398 /* Set ResourceSize */
1399 SetResSize(res, restag);
1400 if(win32)
1401 put_pad(res);
1402 return res;
1406 *****************************************************************************
1407 * Function : stringtable2res
1408 * Syntax : res_t *stringtable2res(stringtable_t *stt)
1409 * Input :
1410 * stt - The stringtable descriptor
1411 * Output : New .res format structure
1412 * Description :
1413 * Remarks :
1414 *****************************************************************************
1416 static res_t *stringtable2res(stringtable_t *stt)
1418 res_t *res;
1419 name_id_t name;
1420 int i;
1421 int restag;
1422 DWORD lastsize = 0;
1424 assert(stt != NULL);
1425 res = new_res();
1427 for(; stt; stt = stt->next)
1429 if(!stt->nentries)
1431 warning("Empty internal stringtable\n");
1432 continue;
1434 name.type = name_ord;
1435 name.name.i_name = (stt->idbase >> 4) + 1;
1436 restag = put_res_header(res, WRC_RT_STRING, NULL, &name, stt->memopt, win32 ? &(stt->lvc) : NULL);
1437 for(i = 0; i < stt->nentries; i++)
1439 if(stt->entries[i].str && stt->entries[i].str->size)
1441 put_string(res, stt->entries[i].str, win32 ? str_unicode : str_char,
1442 FALSE, win32 ? stt->lvc.language : NULL);
1444 else
1446 if (win32)
1447 put_word(res, 0);
1448 else
1449 put_byte(res, 0);
1452 /* Set ResourceSize */
1453 SetResSize(res, restag - lastsize);
1454 if(win32)
1455 put_pad(res);
1456 lastsize = res->size;
1458 return res;
1462 *****************************************************************************
1463 * Function : user2res
1464 * Syntax : res_t *user2res(name_id_t *name, user_t *usr)
1465 * Input :
1466 * name - Name/ordinal of the resource
1467 * usr - The userresource descriptor
1468 * Output : New .res format structure
1469 * Description :
1470 * Remarks :
1471 *****************************************************************************
1473 static res_t *user2res(name_id_t *name, user_t *usr)
1475 int restag;
1476 res_t *res;
1477 assert(name != NULL);
1478 assert(usr != NULL);
1480 res = new_res();
1481 restag = put_res_header(res, 0, usr->type, name, usr->memopt, &(usr->data->lvc));
1482 put_raw_data(res, usr->data, 0);
1483 /* Set ResourceSize */
1484 SetResSize(res, restag);
1485 if(win32)
1486 put_pad(res);
1487 return res;
1491 *****************************************************************************
1492 * Function : versionblock2res
1493 * Syntax : void versionblock2res(res_t *res, ver_block_t *blk)
1494 * Input :
1495 * res - Binary resource to write to
1496 * blk - The version block to be written
1497 * Output :
1498 * Description :
1499 * Remarks : Self recursive
1500 *****************************************************************************
1502 static void versionblock2res(res_t *res, ver_block_t *blk, int level, const language_t *lang)
1504 ver_value_t *val;
1505 int blksizetag;
1506 int valblksizetag;
1507 int valvalsizetag;
1508 int tag;
1509 int i;
1511 blksizetag = res->size;
1512 put_word(res, 0); /* Will be overwritten later */
1513 put_word(res, 0);
1514 if(win32)
1515 put_word(res, 0); /* level ? */
1516 put_string(res, blk->name, win32 ? str_unicode : str_char, TRUE, lang);
1517 put_pad(res);
1518 for(val = blk->values; val; val = val->next)
1520 if(val->type == val_str)
1522 valblksizetag = res->size;
1523 put_word(res, 0); /* Will be overwritten later */
1524 valvalsizetag = res->size;
1525 put_word(res, 0); /* Will be overwritten later */
1526 if(win32)
1528 put_word(res, level);
1530 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
1531 put_pad(res);
1532 tag = res->size;
1533 put_string(res, val->value.str, win32 ? str_unicode : str_char, TRUE, lang);
1534 if(win32)
1535 set_word(res, valvalsizetag, (WORD)((res->size - tag) >> 1));
1536 else
1537 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1538 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1539 put_pad(res);
1541 else if(val->type == val_words)
1543 valblksizetag = res->size;
1544 put_word(res, 0); /* Will be overwritten later */
1545 valvalsizetag = res->size;
1546 put_word(res, 0); /* Will be overwritten later */
1547 if(win32)
1549 put_word(res, level);
1551 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
1552 put_pad(res);
1553 tag = res->size;
1554 for(i = 0; i < val->value.words->nwords; i++)
1556 put_word(res, val->value.words->words[i]);
1558 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1559 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1560 put_pad(res);
1562 else if(val->type == val_block)
1564 versionblock2res(res, val->value.block, level+1, lang);
1566 else
1568 internal_error(__FILE__, __LINE__, "Invalid value indicator %d in VERSIONINFO\n", val->type);
1572 /* Set blocksize */
1573 set_word(res, blksizetag, (WORD)(res->size - blksizetag));
1577 *****************************************************************************
1578 * Function : versioninfo2res
1579 * Syntax : res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1580 * Input :
1581 * name - Name/ordinal of the resource
1582 * ver - The versioninfo descriptor
1583 * Output : New .res format structure
1584 * Description :
1585 * Remarks :
1586 *****************************************************************************
1588 static res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1590 int restag;
1591 int rootblocksizetag;
1592 int valsizetag;
1593 int tag;
1594 res_t *res;
1595 string_t vsvi;
1596 ver_block_t *blk;
1598 assert(name != NULL);
1599 assert(ver != NULL);
1601 vsvi.type = str_char;
1602 vsvi.str.cstr = xstrdup("VS_VERSION_INFO");
1603 vsvi.size = 15; /* Excl. termination */
1605 res = new_res();
1606 restag = put_res_header(res, WRC_RT_VERSION, NULL, name, ver->memopt, &(ver->lvc));
1607 rootblocksizetag = res->size;
1608 put_word(res, 0); /* BlockSize filled in later */
1609 valsizetag = res->size;
1610 put_word(res, 0); /* ValueSize filled in later*/
1611 if(win32)
1612 put_word(res, 0); /* Tree-level ? */
1613 put_string(res, &vsvi, win32 ? str_unicode : str_char,
1614 TRUE, win32 ? ver->lvc.language : NULL);
1615 if(win32)
1616 put_pad(res);
1617 tag = res->size;
1618 put_dword(res, VS_FFI_SIGNATURE);
1619 put_dword(res, VS_FFI_STRUCVERSION);
1620 put_dword(res, (ver->filever_maj1 << 16) + (ver->filever_maj2 & 0xffff));
1621 put_dword(res, (ver->filever_min1 << 16) + (ver->filever_min2 & 0xffff));
1622 put_dword(res, (ver->prodver_maj1 << 16) + (ver->prodver_maj2 & 0xffff));
1623 put_dword(res, (ver->prodver_min1 << 16) + (ver->prodver_min2 & 0xffff));
1624 put_dword(res, ver->fileflagsmask);
1625 put_dword(res, ver->fileflags);
1626 put_dword(res, ver->fileos);
1627 put_dword(res, ver->filetype);
1628 put_dword(res, ver->filesubtype);
1629 put_dword(res, 0); /* FileDateMS */
1630 put_dword(res, 0); /* FileDateLS */
1631 /* Set ValueSize */
1632 set_word(res, valsizetag, (WORD)(res->size - tag));
1633 /* Descend into the blocks */
1634 for(blk = ver->blocks; blk; blk = blk->next)
1635 versionblock2res(res, blk, 0, win32 ? ver->lvc.language : NULL);
1636 /* Set root block's size */
1637 set_word(res, rootblocksizetag, (WORD)(res->size - rootblocksizetag));
1639 SetResSize(res, restag);
1640 if(win32)
1641 put_pad(res);
1643 free(vsvi.str.cstr);
1644 return res;
1648 *****************************************************************************
1649 * Function : toolbaritem2res
1650 * Syntax : void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1651 * Input :
1652 * Output : nop
1653 * Description :
1654 * Remarks : Self recursive
1655 *****************************************************************************
1657 static void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1659 toolbar_item_t *itm = tbitem;
1660 assert(win32 != 0);
1661 while(itm)
1663 put_word(res, itm->id);
1664 itm = itm->next;
1670 *****************************************************************************
1671 * Function : toolbar2res
1672 * Syntax : res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1673 * Input :
1674 * name - Name/ordinal of the resource
1675 * toolbar - The toolbar descriptor
1676 * Output : New .res format structure
1677 * Description :
1678 * Remarks :
1679 *****************************************************************************
1681 static res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1683 int restag;
1684 res_t *res;
1685 assert(name != NULL);
1686 assert(toolbar != NULL);
1688 res = new_res();
1689 if(win32)
1691 restag = put_res_header(res, WRC_RT_TOOLBAR, NULL, name, toolbar->memopt, &(toolbar->lvc));
1693 put_word(res, 1); /* Menuheader: Version */
1694 put_word(res, toolbar->button_width); /* (in pixels?) */
1695 put_word(res, toolbar->button_height); /* (in pixels?) */
1696 put_word(res, toolbar->nitems);
1697 put_pad(res);
1698 toolbaritem2res(res, toolbar->items);
1699 /* Set ResourceSize */
1700 SetResSize(res, restag);
1701 put_pad(res);
1703 else /* win16 */
1705 /* Do not generate anything in 16-bit mode */
1706 free(res->data);
1707 free(res);
1708 return NULL;
1710 return res;
1714 *****************************************************************************
1715 * Function : dlginit2res
1716 * Syntax : res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1717 * Input :
1718 * name - Name/ordinal of the resource
1719 * rdt - The dlginit descriptor
1720 * Output : New .res format structure
1721 * Description :
1722 * Remarks :
1723 *****************************************************************************
1725 static res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1727 int restag;
1728 res_t *res;
1729 assert(name != NULL);
1730 assert(dit != NULL);
1732 res = new_res();
1733 restag = put_res_header(res, WRC_RT_DLGINIT, NULL, name, dit->memopt, &(dit->data->lvc));
1734 put_raw_data(res, dit->data, 0);
1735 /* Set ResourceSize */
1736 SetResSize(res, restag);
1737 if(win32)
1738 put_pad(res);
1739 return res;
1743 *****************************************************************************
1744 * Function : prep_nid_for_label
1745 * Syntax : char *prep_nid_for_label(const name_id_t *nid)
1746 * Input :
1747 * Output :
1748 * Description : Converts a resource name into the first 32 (or less)
1749 * characters of the name with conversions.
1750 * Remarks :
1751 *****************************************************************************
1753 #define MAXNAMELEN 32
1754 char *prep_nid_for_label(const name_id_t *nid)
1756 static char buf[MAXNAMELEN+1];
1758 assert(nid != NULL);
1760 if(nid->type == name_str && nid->name.s_name->type == str_unicode)
1762 WCHAR *sptr;
1763 int i;
1764 sptr = nid->name.s_name->str.wstr;
1765 buf[0] = '\0';
1766 for(i = 0; *sptr && i < MAXNAMELEN; i++)
1768 if((unsigned)*sptr < 0x80 && isprint(*sptr & 0xff))
1769 buf[i] = *sptr++;
1770 else
1771 warning("Resourcename (str_unicode) contain unprintable characters or invalid translation, ignored\n");
1773 buf[i] = '\0';
1775 else if(nid->type == name_str && nid->name.s_name->type == str_char)
1777 char *cptr;
1778 int i;
1779 cptr = nid->name.s_name->str.cstr;
1780 buf[0] = '\0';
1781 for(i = 0; *cptr && i < MAXNAMELEN; i++)
1783 if((unsigned)*cptr < 0x80 && isprint(*cptr & 0xff))
1784 buf[i] = *cptr++;
1785 else
1786 warning("Resourcename (str_char) contain unprintable characters, ignored\n");
1788 buf[i] = '\0';
1790 else if(nid->type == name_ord)
1792 sprintf(buf, "%u", nid->name.i_name);
1794 else
1796 internal_error(__FILE__, __LINE__, "Resource name_id with invalid type %d\n", nid->type);
1798 return buf;
1800 #undef MAXNAMELEN
1803 *****************************************************************************
1804 * Function : make_c_name
1805 * Syntax : char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
1806 * Input :
1807 * Output :
1808 * Description : Converts a resource name into a valid c-identifier in the
1809 * form "_base_nid".
1810 * Remarks :
1811 *****************************************************************************
1813 char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
1815 char *buf = prep_nid_for_label(nid);
1816 return strmake( "_%s_%s_%d", base, buf, lan ? MAKELANGID(lan->id, lan->sub) : 0);
1820 *****************************************************************************
1821 * Function : get_c_typename
1822 * Syntax : const char *get_c_typename(enum res_e type)
1823 * Input :
1824 * Output :
1825 * Description : Convert resource enum to char string to be used in c-name
1826 * creation.
1827 * Remarks :
1828 *****************************************************************************
1830 const char *get_c_typename(enum res_e type)
1832 switch(type)
1834 case res_acc: return "Acc";
1835 case res_anicur:return "AniCur";
1836 case res_aniico:return "AniIco";
1837 case res_bmp: return "Bmp";
1838 case res_cur: return "Cur";
1839 case res_curg: return "CurGrp";
1840 case res_dlg: return "Dlg";
1841 case res_fnt: return "Fnt";
1842 case res_fntdir:return "FntDir";
1843 case res_ico: return "Ico";
1844 case res_icog: return "IcoGrp";
1845 case res_men: return "Men";
1846 case res_rdt: return "RCDat";
1847 case res_stt: return "StrTab";
1848 case res_usr: return "Usr";
1849 case res_msg: return "MsgTab";
1850 case res_ver: return "VerInf";
1851 case res_toolbar: return "TlBr";
1852 case res_dlginit: return "DlgInit";
1853 default: return "Oops";
1858 *****************************************************************************
1859 * Function : resources2res
1860 * Syntax : void resources2res(resource_t *top)
1861 * Input :
1862 * top - The resource-tree to convert
1863 * Output :
1864 * Description : Convert logical resource descriptors into binary data
1865 * Remarks :
1866 *****************************************************************************
1868 void resources2res(resource_t *top)
1870 while(top)
1872 switch(top->type)
1874 case res_acc:
1875 if(!top->binres)
1876 top->binres = accelerator2res(top->name, top->res.acc);
1877 break;
1878 case res_bmp:
1879 if(!top->binres)
1880 top->binres = bitmap2res(top->name, top->res.bmp);
1881 break;
1882 case res_cur:
1883 if(!top->binres)
1884 top->binres = cursor2res(top->res.cur);
1885 break;
1886 case res_curg:
1887 if(!top->binres)
1888 top->binres = cursorgroup2res(top->name, top->res.curg);
1889 break;
1890 case res_dlg:
1891 if(!top->binres)
1892 top->binres = dialog2res(top->name, top->res.dlg);
1893 break;
1894 case res_fnt:
1895 if(!top->binres)
1896 top->binres = font2res(top->name, top->res.fnt);
1897 break;
1898 case res_fntdir:
1899 if(!top->binres)
1900 top->binres = fontdir2res(top->name, top->res.fnd);
1901 break;
1902 case res_ico:
1903 if(!top->binres)
1904 top->binres = icon2res(top->res.ico);
1905 break;
1906 case res_icog:
1907 if(!top->binres)
1908 top->binres = icongroup2res(top->name, top->res.icog);
1909 break;
1910 case res_men:
1911 if(!top->binres)
1912 top->binres = menu2res(top->name, top->res.men);
1913 break;
1914 case res_html:
1915 if(!top->binres)
1916 top->binres = html2res(top->name, top->res.html);
1917 break;
1918 case res_rdt:
1919 if(!top->binres)
1920 top->binres = rcdata2res(top->name, top->res.rdt);
1921 break;
1922 case res_stt:
1923 if(!top->binres)
1924 top->binres = stringtable2res(top->res.stt);
1925 break;
1926 case res_usr:
1927 if(!top->binres)
1928 top->binres = user2res(top->name, top->res.usr);
1929 break;
1930 case res_msg:
1931 if(!top->binres)
1932 top->binres = messagetable2res(top->name, top->res.msg);
1933 break;
1934 case res_ver:
1935 if(!top->binres)
1936 top->binres = versioninfo2res(top->name, top->res.ver);
1937 break;
1938 case res_toolbar:
1939 if(!top->binres)
1940 top->binres = toolbar2res(top->name, top->res.tbt);
1941 break;
1942 case res_dlginit:
1943 if(!top->binres)
1944 top->binres = dlginit2res(top->name, top->res.dlgi);
1945 break;
1946 case res_anicur:
1947 case res_aniico:
1948 if(!top->binres)
1949 top->binres = anicurico2res(top->name, top->res.ani, top->type);
1950 break;
1951 default:
1952 internal_error(__FILE__, __LINE__, "Unknown resource type encountered %d in binary res generation\n", top->type);
1954 top->c_name = make_c_name(get_c_typename(top->type), top->name, top->lan);
1955 top = top->next;