oleaut32/tests: Test more return values.
[wine.git] / tools / wrc / genres.c
blob8c2f280ac5ba601209ef25cf1c76e46fddbd6825
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++)
267 if (str->str.cstr[i] >= 'a' && str->str.cstr[i] <= 'z') str->str.cstr[i] -= 32;
269 else if(str->type == str_unicode)
271 for (i = 0; i < str->size; i++)
272 if (str->str.wstr[i] >= 'a' && str->str.wstr[i] <= 'z') str->str.wstr[i] -= 32;
274 else
276 internal_error(__FILE__, __LINE__, "Invalid string type %d\n", str->type);
280 static int parse_accel_string( const string_t *key, int flags )
282 int keycode;
284 if(key->type == str_char)
286 if (key->str.cstr[0] == '#') return 0; /* ignore message contexts */
287 if((flags & WRC_AF_VIRTKEY) &&
288 !((key->str.cstr[0] >= 'A' && key->str.cstr[0] <= 'Z') ||
289 (key->str.cstr[0] >= '0' && key->str.cstr[0] <= '9')))
291 print_location( &key->loc );
292 error("VIRTKEY code is not equal to ascii value\n");
295 if(key->str.cstr[0] == '^' && (flags & WRC_AF_CONTROL) != 0)
297 print_location( &key->loc );
298 error("Cannot use both '^' and CONTROL modifier\n");
300 else if(key->str.cstr[0] == '^')
302 if (key->str.cstr[1] >= 'a' && key->str.cstr[1] <= 'z')
303 keycode = key->str.cstr[1] - 'a' + 1;
304 else if (key->str.cstr[1] >= 'A' && key->str.cstr[1] <= 'Z')
305 keycode = key->str.cstr[1] - 'A' + 1;
306 else
308 print_location( &key->loc );
309 error("Control-code out of range\n");
312 else
313 keycode = key->str.cstr[0];
315 else
317 if (key->str.wstr[0] == '#') return 0; /* ignore message contexts */
318 if((flags & WRC_AF_VIRTKEY) &&
319 !((key->str.wstr[0] >= 'A' && key->str.wstr[0] <= 'Z') ||
320 (key->str.wstr[0] >= '0' && key->str.wstr[0] <= '9')))
322 print_location( &key->loc );
323 error("VIRTKEY code is not equal to ascii value\n");
325 if(key->str.wstr[0] == '^' && (flags & WRC_AF_CONTROL) != 0)
327 print_location( &key->loc );
328 error("Cannot use both '^' and CONTROL modifier\n");
330 else if(key->str.wstr[0] == '^')
332 if (key->str.wstr[1] >= 'a' && key->str.wstr[1] <= 'z')
333 keycode = key->str.wstr[1] - 'a' + 1;
334 else if (key->str.wstr[1] >= 'A' && key->str.wstr[1] <= 'Z')
335 keycode = key->str.wstr[1] - 'A' + 1;
336 else
338 print_location( &key->loc );
339 error("Control-code out of range\n");
342 else
343 keycode = key->str.wstr[0];
345 return keycode;
349 *****************************************************************************
350 * Function : put_string
351 * Syntax : void put_string(res_t *res, string_t *str, enum str_e type,
352 * int isterm, const language_t *lang)
353 * Input :
354 * res - Binary resource to put the data in
355 * str - String to put
356 * type - Data has to be written in either str_char or str_unicode
357 * isterm - The string is '\0' terminated (disregard the string's
358 * size member)
359 * Output : nop
360 * Description :
361 * Remarks :
362 *****************************************************************************
364 static void put_string(res_t *res, const string_t *str, enum str_e type, int isterm,
365 const language_t *lang)
367 int cnt, codepage;
368 string_t *newstr;
370 assert(res != NULL);
371 assert(str != NULL);
373 if (lang) codepage = get_language_codepage( lang->id, lang->sub );
374 else codepage = get_language_codepage( 0, 0 );
376 assert( codepage != -1 );
378 newstr = convert_string(str, type, codepage);
379 if (type == str_unicode)
381 if (str->type == str_char)
383 if (!check_unicode_conversion( str, newstr, codepage ))
385 print_location( &str->loc );
386 error( "String %s does not convert identically to Unicode and back in codepage %d. "
387 "Try using a Unicode string instead\n", str->str.cstr, codepage );
389 if (check_valid_utf8( str, codepage ))
391 print_location( &str->loc );
392 warning( "string \"%s\" seems to be UTF-8 but codepage %u is in use.\n",
393 str->str.cstr, codepage );
396 if (!isterm) put_word(res, newstr->size);
397 for(cnt = 0; cnt < newstr->size; cnt++)
399 WCHAR c = newstr->str.wstr[cnt];
400 if (isterm && !c) break;
401 put_word(res, c);
403 if (isterm) put_word(res, 0);
405 else /* str_char */
407 if (!isterm) put_byte(res, newstr->size);
408 for(cnt = 0; cnt < newstr->size; cnt++)
410 char c = newstr->str.cstr[cnt];
411 if (isterm && !c) break;
412 put_byte(res, c);
414 if (isterm) put_byte(res, 0);
416 free_string(newstr);
420 *****************************************************************************
421 * Function : put_name_id
422 * Syntax : void put_name_id(res_t *res, name_id_t *nid, int upcase, const language_t *lang)
423 * Input :
424 * Output :
425 * Description :
426 * Remarks :
427 *****************************************************************************
429 static void put_name_id(res_t *res, name_id_t *nid, int upcase, const language_t *lang)
431 if(nid->type == name_ord)
433 if(win32)
434 put_word(res, 0xffff);
435 else
436 put_byte(res, 0xff);
437 put_word(res, (WORD)nid->name.i_name);
439 else if(nid->type == name_str)
441 if(upcase)
442 string_to_upper(nid->name.s_name);
443 put_string(res, nid->name.s_name, win32 ? str_unicode : str_char, TRUE, lang);
445 else
447 internal_error(__FILE__, __LINE__, "Invalid name_id type %d\n", nid->type);
452 *****************************************************************************
453 * Function : put_lvc
454 * Syntax : void put_lvc(res_t *res, lvc_t *lvc)
455 * Input :
456 * Output :
457 * Description :
458 * Remarks :
459 *****************************************************************************
461 static void put_lvc(res_t *res, lvc_t *lvc)
463 if(lvc && lvc->language)
464 put_word(res, MAKELANGID(lvc->language->id, lvc->language->sub));
465 else
466 put_word(res, 0); /* Neutral */
467 if(lvc && lvc->version)
468 put_dword(res, *(lvc->version));
469 else
470 put_dword(res, 0);
471 if(lvc && lvc->characts)
472 put_dword(res, *(lvc->characts));
473 else
474 put_dword(res, 0);
478 *****************************************************************************
479 * Function : put_raw_data
480 * Syntax : void put_raw_data(res_t *res, raw_data_t *raw, int offset)
481 * Input :
482 * Output :
483 * Description :
484 * Remarks :
485 *****************************************************************************
487 static void put_raw_data(res_t *res, raw_data_t *raw, int offset)
489 unsigned int wsize = raw->size - offset;
490 if(res->allocsize - res->size < wsize)
491 grow_res(res, wsize);
492 memcpy(&(res->data[res->size]), raw->data + offset, wsize);
493 res->size += wsize;
497 *****************************************************************************
498 * Function : put_res_header
499 * Syntax : input_res_header(res_t *res, int type, name_id_t *ntype,
500 * name_id_t *name, DWORD memopt, lvc_t *lvc)
502 * Input :
503 * res - Binary resource descriptor to write to
504 * type - Resource identifier (if ntype == NULL)
505 * ntype - Name id of type
506 * name - Resource's name
507 * memopt - Resource's memory options to write
508 * lvc - Language, version and characteristics (win32 only)
509 * Output : An index to the resource size field. The resource size field
510 * contains the header size upon exit.
511 * Description :
512 * Remarks :
513 *****************************************************************************
515 static int put_res_header(res_t *res, int type, name_id_t *ntype, name_id_t *name,
516 DWORD memopt, lvc_t *lvc)
518 if(win32)
520 put_dword(res, 0); /* We will overwrite these later */
521 put_dword(res, 0);
522 if(!ntype)
524 put_word(res, 0xffff); /* ResType */
525 put_word(res, type);
527 else
528 put_name_id(res, ntype, TRUE, lvc->language);
529 put_name_id(res, name, TRUE, lvc->language); /* ResName */
530 put_pad(res);
531 put_dword(res, 0); /* DataVersion */
532 put_word(res, memopt); /* Memory options */
533 put_lvc(res, lvc); /* Language, version and characts */
534 set_dword(res, 0*sizeof(DWORD), res->size); /* Set preliminary resource */
535 set_dword(res, 1*sizeof(DWORD), res->size); /* Set HeaderSize */
536 res->dataidx = res->size;
537 return 0;
539 else /* win16 */
541 int tag;
542 if(!ntype)
544 put_byte(res, 0xff); /* ResType */
545 put_word(res, type);
547 else
548 put_name_id(res, ntype, TRUE, NULL);
549 put_name_id(res, name, TRUE, NULL); /* ResName */
550 put_word(res, memopt); /* Memory options */
551 tag = res->size;
552 put_dword(res, 0); /* ResSize overwritten later*/
553 set_dword(res, tag, res->size);
554 res->dataidx = res->size;
555 return tag;
560 *****************************************************************************
561 * Function : accelerator2res
562 * Syntax : res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
563 * Input :
564 * name - Name/ordinal of the resource
565 * acc - The accelerator descriptor
566 * Output : New .res format structure
567 * Description :
568 * Remarks :
569 *****************************************************************************
571 static res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
573 int restag;
574 res_t *res;
575 event_t *ev;
576 assert(name != NULL);
577 assert(acc != NULL);
579 ev = acc->events;
580 res = new_res();
581 if(win32)
583 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, &(acc->lvc));
584 while(ev)
586 int key = ev->key;
587 if (ev->str) key = parse_accel_string( ev->str, ev->flags );
588 put_word(res, ev->flags | (ev->next ? 0 : 0x80));
589 put_word(res, key);
590 put_word(res, ev->id);
591 put_word(res, 0); /* Padding */
592 ev = ev->next;
594 put_pad(res);
596 else /* win16 */
598 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, NULL);
599 while(ev)
601 int key = ev->key;
602 if (ev->str) key = parse_accel_string( ev->str, ev->flags );
603 put_byte(res, ev->flags | (ev->next ? 0 : 0x80));
604 put_word(res, key);
605 put_word(res, ev->id);
606 ev = ev->next;
609 /* Set ResourceSize */
610 SetResSize(res, restag);
611 return res;
615 *****************************************************************************
616 * Function : dialog2res
617 * Syntax : res_t *dialog2res(name_id_t *name, dialog_t *dlg)
618 * Input :
619 * name - Name/ordinal of the resource
620 * dlg - The dialog descriptor
621 * Output : New .res format structure
622 * Description :
623 * Remarks :
624 *****************************************************************************
626 static res_t *dialog2res(name_id_t *name, dialog_t *dlg)
628 int restag;
629 res_t *res;
630 control_t *ctrl;
631 int tag_nctrl;
632 int nctrl = 0;
633 assert(name != NULL);
634 assert(dlg != NULL);
636 ctrl = dlg->controls;
637 res = new_res();
638 if(win32)
640 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, &(dlg->lvc));
642 if (dlg->is_ex)
644 /* FIXME: MS doc says that the first word must contain 0xffff
645 * and the second 0x0001 to signal a DLGTEMPLATEEX. Borland's
646 * compiler reverses the two words.
647 * I don't know which one to choose, but I write it as Mr. B
648 * writes it.
650 put_word(res, 1); /* Signature */
651 put_word(res, 0xffff); /* DlgVer */
652 put_dword(res, dlg->gothelpid ? dlg->helpid : 0);
653 put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
654 put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
656 else
658 put_dword(res, dlg->style->or_mask);
659 put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
661 tag_nctrl = res->size;
662 put_word(res, 0); /* Number of controls */
663 put_word(res, dlg->x);
664 put_word(res, dlg->y);
665 put_word(res, dlg->width);
666 put_word(res, dlg->height);
667 if(dlg->menu)
668 put_name_id(res, dlg->menu, FALSE, dlg->lvc.language);
669 else
670 put_word(res, 0);
671 if(dlg->dlgclass)
672 put_name_id(res, dlg->dlgclass, FALSE, dlg->lvc.language);
673 else
674 put_word(res, 0);
675 if(dlg->title)
676 put_string(res, dlg->title, str_unicode, TRUE, dlg->lvc.language);
677 else
678 put_word(res, 0);
679 if(dlg->font)
681 put_word(res, dlg->font->size);
682 if (dlg->is_ex)
684 put_word(res, dlg->font->weight);
685 /* FIXME: ? TRUE should be sufficient to say that it's
686 * italic, but Borland's compiler says it's 0x0101.
687 * I just copy it here, and hope for the best.
689 put_word(res, dlg->font->italic ? 0x0101 : 0);
691 put_string(res, dlg->font->name, str_unicode, TRUE, dlg->lvc.language);
693 else if (dlg->style->or_mask & DS_SETFONT) put_word( res, 0x7fff );
695 put_pad(res);
696 while(ctrl)
698 if (dlg->is_ex)
700 put_dword(res, ctrl->gothelpid ? ctrl->helpid : 0);
701 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
702 /* FIXME: what is default control style? */
703 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask : WS_CHILD | WS_VISIBLE);
705 else
707 /* FIXME: what is default control style? */
708 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
709 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
711 put_word(res, ctrl->x);
712 put_word(res, ctrl->y);
713 put_word(res, ctrl->width);
714 put_word(res, ctrl->height);
715 if (dlg->is_ex)
716 put_dword(res, ctrl->id);
717 else
718 put_word(res, ctrl->id);
719 if(ctrl->ctlclass)
720 put_name_id(res, ctrl->ctlclass, FALSE, dlg->lvc.language);
721 else
722 internal_error(__FILE__, __LINE__, "Control has no control-class\n");
723 if(ctrl->title)
724 put_name_id(res, ctrl->title, FALSE, dlg->lvc.language);
725 else
726 put_word(res, 0);
727 if(ctrl->extra)
729 put_word(res, ctrl->extra->size+2);
730 put_pad(res);
731 put_raw_data(res, ctrl->extra, 0);
733 else
734 put_word(res, 0);
736 if(ctrl->next)
737 put_pad(res);
738 nctrl++;
739 ctrl = ctrl->next;
741 /* Set number of controls */
742 set_word(res, tag_nctrl, (WORD)nctrl);
744 else /* win16 */
746 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, NULL);
748 put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
749 tag_nctrl = res->size;
750 put_byte(res, 0); /* Number of controls */
751 put_word(res, dlg->x);
752 put_word(res, dlg->y);
753 put_word(res, dlg->width);
754 put_word(res, dlg->height);
755 if(dlg->menu)
756 put_name_id(res, dlg->menu, TRUE, NULL);
757 else
758 put_byte(res, 0);
759 if(dlg->dlgclass)
760 put_name_id(res, dlg->dlgclass, TRUE, NULL);
761 else
762 put_byte(res, 0);
763 if(dlg->title)
764 put_string(res, dlg->title, str_char, TRUE, NULL);
765 else
766 put_byte(res, 0);
767 if(dlg->font)
769 put_word(res, dlg->font->size);
770 put_string(res, dlg->font->name, str_char, TRUE, NULL);
773 while(ctrl)
775 put_word(res, ctrl->x);
776 put_word(res, ctrl->y);
777 put_word(res, ctrl->width);
778 put_word(res, ctrl->height);
779 put_word(res, ctrl->id);
780 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
781 if(ctrl->ctlclass)
783 if(ctrl->ctlclass->type == name_ord
784 && ctrl->ctlclass->name.i_name >= 0x80
785 && ctrl->ctlclass->name.i_name <= 0x85)
786 put_byte(res, ctrl->ctlclass->name.i_name);
787 else if(ctrl->ctlclass->type == name_str)
788 put_name_id(res, ctrl->ctlclass, FALSE, NULL);
789 else
790 error("Unknown control-class %04x\n", ctrl->ctlclass->name.i_name);
792 else
793 internal_error(__FILE__, __LINE__, "Control has no control-class\n");
794 if(ctrl->title)
795 put_name_id(res, ctrl->title, FALSE, NULL);
796 else
797 put_byte(res, 0);
799 /* FIXME: What is this extra byte doing here? */
800 put_byte(res, 0);
802 nctrl++;
803 ctrl = ctrl->next;
805 /* Set number of controls */
806 ((char *)res->data)[tag_nctrl] = (char)nctrl;
808 /* Set ResourceSize */
809 SetResSize(res, restag);
810 return res;
814 *****************************************************************************
815 * Function : menuitem2res
816 * Syntax : void menuitem2res(res_t *res, menu_item_t *item)
817 * Input :
818 * Output :
819 * Description :
820 * Remarks : Self recursive
821 *****************************************************************************
823 static void menuitem2res(res_t *res, menu_item_t *menitem, const language_t *lang)
825 menu_item_t *itm = menitem;
826 if(win32)
828 while(itm)
830 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
831 if(!itm->popup)
832 put_word(res, itm->id);
833 if(itm->name)
834 put_string(res, itm->name, str_unicode, TRUE, lang);
835 else
836 put_word(res, 0);
837 if(itm->popup)
838 menuitem2res(res, itm->popup, lang);
839 itm = itm->next;
842 else /* win16 */
844 while(itm)
846 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
847 if(!itm->popup)
848 put_word(res, itm->id);
849 if(itm->name)
850 put_string(res, itm->name, str_char, TRUE, lang);
851 else
852 put_byte(res, 0);
853 if(itm->popup)
854 menuitem2res(res, itm->popup, lang);
855 itm = itm->next;
862 *****************************************************************************
863 * Function : menuexitem2res
864 * Syntax : void menuexitem2res(res_t *res, menuex_item_t *item)
865 * Input :
866 * Output : nop
867 * Description :
868 * Remarks : Self recursive
869 *****************************************************************************
871 static void menuexitem2res(res_t *res, menu_item_t *menitem, const language_t *lang)
873 menu_item_t *itm = menitem;
874 assert(win32 != 0);
875 while(itm)
877 put_dword(res, itm->gottype ? itm->type : 0);
878 put_dword(res, itm->gotstate ? itm->state : 0);
879 put_dword(res, itm->gotid ? itm->id : 0);
880 put_word(res, (itm->popup ? 0x01 : 0) | (!itm->next ? MF_END : 0));
881 if(itm->name)
882 put_string(res, itm->name, str_unicode, TRUE, lang);
883 else
884 put_word(res, 0);
885 put_pad(res);
886 if(itm->popup)
888 put_dword(res, itm->gothelpid ? itm->helpid : 0);
889 menuexitem2res(res, itm->popup, lang);
891 itm = itm->next;
897 *****************************************************************************
898 * Function : menu2res
899 * Syntax : res_t *menu2res(name_id_t *name, menu_t *men)
900 * Input :
901 * name - Name/ordinal of the resource
902 * menex - The menuex descriptor
903 * Output : New .res format structure
904 * Description :
905 * Remarks :
906 *****************************************************************************
908 static res_t *menu2res(name_id_t *name, menu_t *men)
910 int restag;
911 res_t *res;
912 assert(name != NULL);
913 assert(men != NULL);
915 res = new_res();
916 if(win32)
918 restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, &(men->lvc));
920 if (men->is_ex)
922 put_word(res, 1); /* Menuheader: Version */
923 put_word(res, 4); /* Offset */
924 put_dword(res, 0); /* HelpId */
925 put_pad(res);
926 menuexitem2res(res, men->items, men->lvc.language);
928 else
930 put_dword(res, 0); /* Menuheader: Version and HeaderSize */
931 menuitem2res(res, men->items, men->lvc.language);
933 /* Set ResourceSize */
934 SetResSize(res, restag);
935 put_pad(res);
937 else /* win16 */
939 restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, NULL);
941 put_dword(res, 0); /* Menuheader: Version and HeaderSize */
942 menuitem2res(res, men->items, NULL);
943 /* Set ResourceSize */
944 SetResSize(res, restag);
946 return res;
950 *****************************************************************************
951 * Function : cursorgroup2res
952 * Syntax : res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
953 * Input :
954 * name - Name/ordinal of the resource
955 * curg - The cursor descriptor
956 * Output : New .res format structure
957 * Description :
958 * Remarks :
959 *****************************************************************************
961 static res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
963 int restag;
964 res_t *res;
965 cursor_t *cur;
966 assert(name != NULL);
967 assert(curg != NULL);
969 res = new_res();
970 restag = put_res_header(res, WRC_RT_GROUP_CURSOR, NULL, name, curg->memopt, &(curg->lvc));
972 put_word(res, 0); /* Reserved */
973 /* FIXME: The ResType in the NEWHEADER structure should
974 * contain 14 according to the MS win32 doc. This is
975 * not the case with the BRC compiler and I really doubt
976 * the latter. Putting one here is compliant to win16 spec,
977 * but who knows the true value?
979 put_word(res, 2); /* ResType */
980 put_word(res, curg->ncursor);
981 #if 0
982 for(cur = curg->cursorlist; cur; cur = cur->next)
983 #else
984 cur = curg->cursorlist;
985 while(cur->next)
986 cur = cur->next;
987 for(; cur; cur = cur->prev)
988 #endif
990 put_word(res, cur->width);
991 /* FIXME: The height of a cursor is half the size of
992 * the bitmap's height. BRC puts the height from the
993 * BITMAPINFOHEADER here instead of the cursorfile's
994 * height. MS doesn't seem to care...
996 put_word(res, cur->height);
997 /* FIXME: The next two are reversed in BRC and I don't
998 * know why. Probably a bug. But, we can safely ignore
999 * it because win16 does not support color cursors.
1000 * A warning should have been generated by the parser.
1002 put_word(res, cur->planes);
1003 put_word(res, cur->bits);
1004 /* FIXME: The +4 is the hotspot in the cursor resource.
1005 * However, I could not find this in the documentation.
1006 * The hotspot bytes must either be included or MS
1007 * doesn't care.
1009 put_dword(res, cur->data->size +4);
1010 put_word(res, cur->id);
1013 SetResSize(res, restag); /* Set ResourceSize */
1014 if(win32)
1015 put_pad(res);
1017 return res;
1021 *****************************************************************************
1022 * Function : cursor2res
1023 * Syntax : res_t *cursor2res(cursor_t *cur)
1024 * Input :
1025 * cur - The cursor descriptor
1026 * Output : New .res format structure
1027 * Description :
1028 * Remarks :
1029 *****************************************************************************
1031 static res_t *cursor2res(cursor_t *cur)
1033 int restag;
1034 res_t *res;
1035 name_id_t name;
1037 assert(cur != NULL);
1039 res = new_res();
1040 name.type = name_ord;
1041 name.name.i_name = cur->id;
1042 restag = put_res_header(res, WRC_RT_CURSOR, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(cur->lvc));
1043 put_word(res, cur->xhot);
1044 put_word(res, cur->yhot);
1045 put_raw_data(res, cur->data, 0);
1047 SetResSize(res, restag); /* Set ResourceSize */
1048 if(win32)
1049 put_pad(res);
1051 return res;
1055 *****************************************************************************
1056 * Function : icongroup2res
1057 * Syntax : res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1058 * Input :
1059 * name - Name/ordinal of the resource
1060 * icog - The icon group descriptor
1061 * Output : New .res format structure
1062 * Description :
1063 * Remarks :
1064 *****************************************************************************
1066 static res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1068 int restag;
1069 res_t *res;
1070 icon_t *ico;
1071 assert(name != NULL);
1072 assert(icog != NULL);
1074 res = new_res();
1075 restag = put_res_header(res, WRC_RT_GROUP_ICON, NULL, name, icog->memopt, &(icog->lvc));
1077 put_word(res, 0); /* Reserved */
1078 /* FIXME: The ResType in the NEWHEADER structure should
1079 * contain 14 according to the MS win32 doc. This is
1080 * not the case with the BRC compiler and I really doubt
1081 * the latter. Putting one here is compliant to win16 spec,
1082 * but who knows the true value?
1084 put_word(res, 1); /* ResType */
1085 put_word(res, icog->nicon);
1086 for(ico = icog->iconlist; ico; ico = ico->next)
1088 put_byte(res, ico->width);
1089 put_byte(res, ico->height);
1090 put_byte(res, ico->nclr);
1091 put_byte(res, 0); /* Reserved */
1092 put_word(res, ico->planes);
1093 put_word(res, ico->bits);
1094 put_dword(res, ico->data->size);
1095 put_word(res, ico->id);
1098 SetResSize(res, restag); /* Set ResourceSize */
1099 if(win32)
1100 put_pad(res);
1102 return res;
1106 *****************************************************************************
1107 * Function : icon2res
1108 * Syntax : res_t *icon2res(icon_t *ico)
1109 * Input :
1110 * ico - The icon descriptor
1111 * Output : New .res format structure
1112 * Description :
1113 * Remarks :
1114 *****************************************************************************
1116 static res_t *icon2res(icon_t *ico)
1118 int restag;
1119 res_t *res;
1120 name_id_t name;
1122 assert(ico != NULL);
1124 res = new_res();
1125 name.type = name_ord;
1126 name.name.i_name = ico->id;
1127 restag = put_res_header(res, WRC_RT_ICON, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(ico->lvc));
1128 put_raw_data(res, ico->data, 0);
1130 SetResSize(res, restag); /* Set ResourceSize */
1131 if(win32)
1132 put_pad(res);
1134 return res;
1138 *****************************************************************************
1139 * Function : anicurico2res
1140 * Syntax : res_t *anicurico2res(name_id_t *name, ani_curico_t *ani)
1141 * Input :
1142 * name - Name/ordinal of the resource
1143 * ani - The animated object descriptor
1144 * Output : New .res format structure
1145 * Description :
1146 * Remarks : The endian of the object's structures have been converted
1147 * by the loader.
1148 * There are rumors that win311 could handle animated stuff.
1149 * That is why they are generated for both win16 and win32
1150 * compile.
1151 *****************************************************************************
1153 static res_t *anicurico2res(name_id_t *name, ani_curico_t *ani, enum res_e type)
1155 int restag;
1156 res_t *res;
1157 assert(name != NULL);
1158 assert(ani != NULL);
1160 res = new_res();
1161 restag = put_res_header(res, type == res_anicur ? WRC_RT_ANICURSOR : WRC_RT_ANIICON,
1162 NULL, name, ani->memopt, NULL);
1163 put_raw_data(res, ani->data, 0);
1164 /* Set ResourceSize */
1165 SetResSize(res, restag);
1166 if(win32)
1167 put_pad(res);
1168 return res;
1172 *****************************************************************************
1173 * Function : bitmap2res
1174 * Syntax : res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1175 * Input :
1176 * name - Name/ordinal of the resource
1177 * bmp - The bitmap descriptor
1178 * Output : New .res format structure
1179 * Description :
1180 * Remarks : The endian of the bitmap structures have been converted
1181 * by the loader.
1182 *****************************************************************************
1184 static res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1186 int restag;
1187 res_t *res;
1188 assert(name != NULL);
1189 assert(bmp != NULL);
1191 res = new_res();
1192 restag = put_res_header(res, WRC_RT_BITMAP, NULL, name, bmp->memopt, &(bmp->data->lvc));
1193 if(bmp->data->data[0] == 'B'
1194 && bmp->data->data[1] == 'M'
1195 && ((BITMAPFILEHEADER *)bmp->data->data)->bfSize == bmp->data->size
1196 && bmp->data->size >= sizeof(BITMAPFILEHEADER))
1198 /* The File header is still attached, don't write it */
1199 put_raw_data(res, bmp->data, sizeof(BITMAPFILEHEADER));
1201 else
1203 put_raw_data(res, bmp->data, 0);
1205 /* Set ResourceSize */
1206 SetResSize(res, restag);
1207 if(win32)
1208 put_pad(res);
1209 return res;
1213 *****************************************************************************
1214 * Function : font2res
1215 * Syntax : res_t *font2res(name_id_t *name, font_t *fnt)
1216 * Input :
1217 * name - Name/ordinal of the resource
1218 * fnt - The font descriptor
1219 * Output : New .res format structure
1220 * Description :
1221 * Remarks : The data has been prepared just after parsing.
1222 *****************************************************************************
1224 static res_t *font2res(name_id_t *name, font_t *fnt)
1226 int restag;
1227 res_t *res;
1228 assert(name != NULL);
1229 assert(fnt != NULL);
1231 res = new_res();
1232 restag = put_res_header(res, WRC_RT_FONT, NULL, name, fnt->memopt, &(fnt->data->lvc));
1233 put_raw_data(res, fnt->data, 0);
1234 /* Set ResourceSize */
1235 SetResSize(res, restag);
1236 if(win32)
1237 put_pad(res);
1238 return res;
1242 *****************************************************************************
1243 * Function : fontdir2res
1244 * Syntax : res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1245 * Input :
1246 * name - Name/ordinal of the resource
1247 * fntdir - The fontdir descriptor
1248 * Output : New .res format structure
1249 * Description :
1250 * Remarks : The data has been prepared just after parsing.
1251 *****************************************************************************
1253 static res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1255 int restag;
1256 res_t *res;
1257 assert(name != NULL);
1258 assert(fnd != NULL);
1260 res = new_res();
1261 restag = put_res_header(res, WRC_RT_FONTDIR, NULL, name, fnd->memopt, &(fnd->data->lvc));
1262 put_raw_data(res, fnd->data, 0);
1263 /* Set ResourceSize */
1264 SetResSize(res, restag);
1265 if(win32)
1266 put_pad(res);
1267 return res;
1271 *****************************************************************************
1272 * Function : html2res
1273 * Syntax : res_t *html2res(name_id_t *name, html_t *html)
1274 * Input :
1275 * name - Name/ordinal of the resource
1276 * rdt - The html descriptor
1277 * Output : New .res format structure
1278 * Description :
1279 * Remarks :
1280 *****************************************************************************
1282 static res_t *html2res(name_id_t *name, html_t *html)
1284 int restag;
1285 res_t *res;
1286 assert(name != NULL);
1287 assert(html != NULL);
1289 res = new_res();
1290 restag = put_res_header(res, WRC_RT_HTML, NULL, name, html->memopt, &(html->data->lvc));
1291 put_raw_data(res, html->data, 0);
1292 /* Set ResourceSize */
1293 SetResSize(res, restag);
1294 if(win32)
1295 put_pad(res);
1296 return res;
1300 *****************************************************************************
1301 * Function : rcdata2res
1302 * Syntax : res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1303 * Input :
1304 * name - Name/ordinal of the resource
1305 * rdt - The rcdata descriptor
1306 * Output : New .res format structure
1307 * Description :
1308 * Remarks :
1309 *****************************************************************************
1311 static res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1313 int restag;
1314 res_t *res;
1315 assert(name != NULL);
1316 assert(rdt != NULL);
1318 res = new_res();
1319 restag = put_res_header(res, WRC_RT_RCDATA, NULL, name, rdt->memopt, &(rdt->data->lvc));
1320 put_raw_data(res, rdt->data, 0);
1321 /* Set ResourceSize */
1322 SetResSize(res, restag);
1323 if(win32)
1324 put_pad(res);
1325 return res;
1329 *****************************************************************************
1330 * Function : messagetable2res
1331 * Syntax : res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1332 * Input :
1333 * name - Name/ordinal of the resource
1334 * msg - The messagetable descriptor
1335 * Output : New .res format structure
1336 * Description :
1337 * Remarks : The data has been converted to the appropriate endian
1338 * after it was parsed.
1339 *****************************************************************************
1341 static res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1343 int restag;
1344 res_t *res;
1345 assert(name != NULL);
1346 assert(msg != NULL);
1348 res = new_res();
1349 restag = put_res_header(res, WRC_RT_MESSAGETABLE, NULL, name, msg->memopt, &(msg->data->lvc));
1350 put_raw_data(res, msg->data, 0);
1351 /* Set ResourceSize */
1352 SetResSize(res, restag);
1353 if(win32)
1354 put_pad(res);
1355 return res;
1359 *****************************************************************************
1360 * Function : stringtable2res
1361 * Syntax : res_t *stringtable2res(stringtable_t *stt)
1362 * Input :
1363 * stt - The stringtable descriptor
1364 * Output : New .res format structure
1365 * Description :
1366 * Remarks :
1367 *****************************************************************************
1369 static res_t *stringtable2res(stringtable_t *stt)
1371 res_t *res;
1372 name_id_t name;
1373 int i;
1374 int restag;
1375 DWORD lastsize = 0;
1377 assert(stt != NULL);
1378 res = new_res();
1380 for(; stt; stt = stt->next)
1382 if(!stt->nentries)
1384 warning("Empty internal stringtable\n");
1385 continue;
1387 name.type = name_ord;
1388 name.name.i_name = (stt->idbase >> 4) + 1;
1389 restag = put_res_header(res, WRC_RT_STRING, NULL, &name, stt->memopt, win32 ? &(stt->lvc) : NULL);
1390 for(i = 0; i < stt->nentries; i++)
1392 if(stt->entries[i].str && stt->entries[i].str->size)
1394 put_string(res, stt->entries[i].str, win32 ? str_unicode : str_char,
1395 FALSE, win32 ? stt->lvc.language : NULL);
1397 else
1399 if (win32)
1400 put_word(res, 0);
1401 else
1402 put_byte(res, 0);
1405 /* Set ResourceSize */
1406 SetResSize(res, restag - lastsize);
1407 if(win32)
1408 put_pad(res);
1409 lastsize = res->size;
1411 return res;
1415 *****************************************************************************
1416 * Function : user2res
1417 * Syntax : res_t *user2res(name_id_t *name, user_t *usr)
1418 * Input :
1419 * name - Name/ordinal of the resource
1420 * usr - The userresource descriptor
1421 * Output : New .res format structure
1422 * Description :
1423 * Remarks :
1424 *****************************************************************************
1426 static res_t *user2res(name_id_t *name, user_t *usr)
1428 int restag;
1429 res_t *res;
1430 assert(name != NULL);
1431 assert(usr != NULL);
1433 res = new_res();
1434 restag = put_res_header(res, 0, usr->type, name, usr->memopt, &(usr->data->lvc));
1435 put_raw_data(res, usr->data, 0);
1436 /* Set ResourceSize */
1437 SetResSize(res, restag);
1438 if(win32)
1439 put_pad(res);
1440 return res;
1444 *****************************************************************************
1445 * Function : versionblock2res
1446 * Syntax : void versionblock2res(res_t *res, ver_block_t *blk)
1447 * Input :
1448 * res - Binary resource to write to
1449 * blk - The version block to be written
1450 * Output :
1451 * Description :
1452 * Remarks : Self recursive
1453 *****************************************************************************
1455 static void versionblock2res(res_t *res, ver_block_t *blk, int level, const language_t *lang)
1457 ver_value_t *val;
1458 int blksizetag;
1459 int valblksizetag;
1460 int valvalsizetag;
1461 int tag;
1462 int i;
1464 blksizetag = res->size;
1465 put_word(res, 0); /* Will be overwritten later */
1466 put_word(res, 0);
1467 if(win32)
1468 put_word(res, 0); /* level ? */
1469 put_string(res, blk->name, win32 ? str_unicode : str_char, TRUE, lang);
1470 put_pad(res);
1471 for(val = blk->values; val; val = val->next)
1473 if(val->type == val_str)
1475 valblksizetag = res->size;
1476 put_word(res, 0); /* Will be overwritten later */
1477 valvalsizetag = res->size;
1478 put_word(res, 0); /* Will be overwritten later */
1479 if(win32)
1481 put_word(res, level);
1483 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
1484 put_pad(res);
1485 tag = res->size;
1486 put_string(res, val->value.str, win32 ? str_unicode : str_char, TRUE, lang);
1487 if(win32)
1488 set_word(res, valvalsizetag, (WORD)((res->size - tag) >> 1));
1489 else
1490 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1491 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1492 put_pad(res);
1494 else if(val->type == val_words)
1496 valblksizetag = res->size;
1497 put_word(res, 0); /* Will be overwritten later */
1498 valvalsizetag = res->size;
1499 put_word(res, 0); /* Will be overwritten later */
1500 if(win32)
1502 put_word(res, level);
1504 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
1505 put_pad(res);
1506 tag = res->size;
1507 for(i = 0; i < val->value.words->nwords; i++)
1509 put_word(res, val->value.words->words[i]);
1511 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1512 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1513 put_pad(res);
1515 else if(val->type == val_block)
1517 versionblock2res(res, val->value.block, level+1, lang);
1519 else
1521 internal_error(__FILE__, __LINE__, "Invalid value indicator %d in VERSIONINFO\n", val->type);
1525 /* Set blocksize */
1526 set_word(res, blksizetag, (WORD)(res->size - blksizetag));
1530 *****************************************************************************
1531 * Function : versioninfo2res
1532 * Syntax : res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1533 * Input :
1534 * name - Name/ordinal of the resource
1535 * ver - The versioninfo descriptor
1536 * Output : New .res format structure
1537 * Description :
1538 * Remarks :
1539 *****************************************************************************
1541 static res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1543 int restag;
1544 int rootblocksizetag;
1545 int valsizetag;
1546 int tag;
1547 res_t *res;
1548 string_t vsvi;
1549 ver_block_t *blk;
1551 assert(name != NULL);
1552 assert(ver != NULL);
1554 vsvi.type = str_char;
1555 vsvi.str.cstr = xstrdup("VS_VERSION_INFO");
1556 vsvi.size = 15; /* Excl. termination */
1558 res = new_res();
1559 restag = put_res_header(res, WRC_RT_VERSION, NULL, name, ver->memopt, &(ver->lvc));
1560 rootblocksizetag = res->size;
1561 put_word(res, 0); /* BlockSize filled in later */
1562 valsizetag = res->size;
1563 put_word(res, 0); /* ValueSize filled in later*/
1564 if(win32)
1565 put_word(res, 0); /* Tree-level ? */
1566 put_string(res, &vsvi, win32 ? str_unicode : str_char,
1567 TRUE, win32 ? ver->lvc.language : NULL);
1568 if(win32)
1569 put_pad(res);
1570 tag = res->size;
1571 put_dword(res, VS_FFI_SIGNATURE);
1572 put_dword(res, VS_FFI_STRUCVERSION);
1573 put_dword(res, (ver->filever_maj1 << 16) + (ver->filever_maj2 & 0xffff));
1574 put_dword(res, (ver->filever_min1 << 16) + (ver->filever_min2 & 0xffff));
1575 put_dword(res, (ver->prodver_maj1 << 16) + (ver->prodver_maj2 & 0xffff));
1576 put_dword(res, (ver->prodver_min1 << 16) + (ver->prodver_min2 & 0xffff));
1577 put_dword(res, ver->fileflagsmask);
1578 put_dword(res, ver->fileflags);
1579 put_dword(res, ver->fileos);
1580 put_dword(res, ver->filetype);
1581 put_dword(res, ver->filesubtype);
1582 put_dword(res, 0); /* FileDateMS */
1583 put_dword(res, 0); /* FileDateLS */
1584 /* Set ValueSize */
1585 set_word(res, valsizetag, (WORD)(res->size - tag));
1586 /* Descend into the blocks */
1587 for(blk = ver->blocks; blk; blk = blk->next)
1588 versionblock2res(res, blk, 0, win32 ? ver->lvc.language : NULL);
1589 /* Set root block's size */
1590 set_word(res, rootblocksizetag, (WORD)(res->size - rootblocksizetag));
1592 SetResSize(res, restag);
1593 if(win32)
1594 put_pad(res);
1596 free(vsvi.str.cstr);
1597 return res;
1601 *****************************************************************************
1602 * Function : toolbaritem2res
1603 * Syntax : void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1604 * Input :
1605 * Output : nop
1606 * Description :
1607 * Remarks : Self recursive
1608 *****************************************************************************
1610 static void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1612 toolbar_item_t *itm = tbitem;
1613 assert(win32 != 0);
1614 while(itm)
1616 put_word(res, itm->id);
1617 itm = itm->next;
1623 *****************************************************************************
1624 * Function : toolbar2res
1625 * Syntax : res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1626 * Input :
1627 * name - Name/ordinal of the resource
1628 * toolbar - The toolbar descriptor
1629 * Output : New .res format structure
1630 * Description :
1631 * Remarks :
1632 *****************************************************************************
1634 static res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1636 int restag;
1637 res_t *res;
1638 assert(name != NULL);
1639 assert(toolbar != NULL);
1641 res = new_res();
1642 if(win32)
1644 restag = put_res_header(res, WRC_RT_TOOLBAR, NULL, name, toolbar->memopt, &(toolbar->lvc));
1646 put_word(res, 1); /* Menuheader: Version */
1647 put_word(res, toolbar->button_width); /* (in pixels?) */
1648 put_word(res, toolbar->button_height); /* (in pixels?) */
1649 put_word(res, toolbar->nitems);
1650 put_pad(res);
1651 toolbaritem2res(res, toolbar->items);
1652 /* Set ResourceSize */
1653 SetResSize(res, restag);
1654 put_pad(res);
1656 else /* win16 */
1658 /* Do not generate anything in 16-bit mode */
1659 free(res->data);
1660 free(res);
1661 return NULL;
1663 return res;
1667 *****************************************************************************
1668 * Function : dlginit2res
1669 * Syntax : res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1670 * Input :
1671 * name - Name/ordinal of the resource
1672 * rdt - The dlginit descriptor
1673 * Output : New .res format structure
1674 * Description :
1675 * Remarks :
1676 *****************************************************************************
1678 static res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1680 int restag;
1681 res_t *res;
1682 assert(name != NULL);
1683 assert(dit != NULL);
1685 res = new_res();
1686 restag = put_res_header(res, WRC_RT_DLGINIT, NULL, name, dit->memopt, &(dit->data->lvc));
1687 put_raw_data(res, dit->data, 0);
1688 /* Set ResourceSize */
1689 SetResSize(res, restag);
1690 if(win32)
1691 put_pad(res);
1692 return res;
1696 *****************************************************************************
1697 * Function : prep_nid_for_label
1698 * Syntax : char *prep_nid_for_label(const name_id_t *nid)
1699 * Input :
1700 * Output :
1701 * Description : Converts a resource name into the first 32 (or less)
1702 * characters of the name with conversions.
1703 * Remarks :
1704 *****************************************************************************
1706 #define MAXNAMELEN 32
1707 char *prep_nid_for_label(const name_id_t *nid)
1709 static char buf[MAXNAMELEN+1];
1711 assert(nid != NULL);
1713 if(nid->type == name_str && nid->name.s_name->type == str_unicode)
1715 WCHAR *sptr;
1716 int i;
1717 sptr = nid->name.s_name->str.wstr;
1718 buf[0] = '\0';
1719 for(i = 0; *sptr && i < MAXNAMELEN; i++)
1721 if((unsigned)*sptr < 0x80 && isprint(*sptr & 0xff))
1722 buf[i] = *sptr++;
1723 else
1724 warning("Resourcename (str_unicode) contain unprintable characters or invalid translation, ignored\n");
1726 buf[i] = '\0';
1728 else if(nid->type == name_str && nid->name.s_name->type == str_char)
1730 char *cptr;
1731 int i;
1732 cptr = nid->name.s_name->str.cstr;
1733 buf[0] = '\0';
1734 for(i = 0; *cptr && i < MAXNAMELEN; i++)
1736 if((unsigned)*cptr < 0x80 && isprint(*cptr & 0xff))
1737 buf[i] = *cptr++;
1738 else
1739 warning("Resourcename (str_char) contain unprintable characters, ignored\n");
1741 buf[i] = '\0';
1743 else if(nid->type == name_ord)
1745 sprintf(buf, "%u", nid->name.i_name);
1747 else
1749 internal_error(__FILE__, __LINE__, "Resource name_id with invalid type %d\n", nid->type);
1751 return buf;
1753 #undef MAXNAMELEN
1756 *****************************************************************************
1757 * Function : make_c_name
1758 * Syntax : char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
1759 * Input :
1760 * Output :
1761 * Description : Converts a resource name into a valid c-identifier in the
1762 * form "_base_nid".
1763 * Remarks :
1764 *****************************************************************************
1766 char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
1768 char *buf = prep_nid_for_label(nid);
1769 return strmake( "_%s_%s_%d", base, buf, lan ? MAKELANGID(lan->id, lan->sub) : 0);
1773 *****************************************************************************
1774 * Function : get_c_typename
1775 * Syntax : const char *get_c_typename(enum res_e type)
1776 * Input :
1777 * Output :
1778 * Description : Convert resource enum to char string to be used in c-name
1779 * creation.
1780 * Remarks :
1781 *****************************************************************************
1783 const char *get_c_typename(enum res_e type)
1785 switch(type)
1787 case res_acc: return "Acc";
1788 case res_anicur:return "AniCur";
1789 case res_aniico:return "AniIco";
1790 case res_bmp: return "Bmp";
1791 case res_cur: return "Cur";
1792 case res_curg: return "CurGrp";
1793 case res_dlg: return "Dlg";
1794 case res_fnt: return "Fnt";
1795 case res_fntdir:return "FntDir";
1796 case res_ico: return "Ico";
1797 case res_icog: return "IcoGrp";
1798 case res_men: return "Men";
1799 case res_rdt: return "RCDat";
1800 case res_stt: return "StrTab";
1801 case res_usr: return "Usr";
1802 case res_msg: return "MsgTab";
1803 case res_ver: return "VerInf";
1804 case res_toolbar: return "TlBr";
1805 case res_dlginit: return "DlgInit";
1806 default: return "Oops";
1811 *****************************************************************************
1812 * Function : resources2res
1813 * Syntax : void resources2res(resource_t *top)
1814 * Input :
1815 * top - The resource-tree to convert
1816 * Output :
1817 * Description : Convert logical resource descriptors into binary data
1818 * Remarks :
1819 *****************************************************************************
1821 void resources2res(resource_t *top)
1823 while(top)
1825 switch(top->type)
1827 case res_acc:
1828 if(!top->binres)
1829 top->binres = accelerator2res(top->name, top->res.acc);
1830 break;
1831 case res_bmp:
1832 if(!top->binres)
1833 top->binres = bitmap2res(top->name, top->res.bmp);
1834 break;
1835 case res_cur:
1836 if(!top->binres)
1837 top->binres = cursor2res(top->res.cur);
1838 break;
1839 case res_curg:
1840 if(!top->binres)
1841 top->binres = cursorgroup2res(top->name, top->res.curg);
1842 break;
1843 case res_dlg:
1844 if(!top->binres)
1845 top->binres = dialog2res(top->name, top->res.dlg);
1846 break;
1847 case res_fnt:
1848 if(!top->binres)
1849 top->binres = font2res(top->name, top->res.fnt);
1850 break;
1851 case res_fntdir:
1852 if(!top->binres)
1853 top->binres = fontdir2res(top->name, top->res.fnd);
1854 break;
1855 case res_ico:
1856 if(!top->binres)
1857 top->binres = icon2res(top->res.ico);
1858 break;
1859 case res_icog:
1860 if(!top->binres)
1861 top->binres = icongroup2res(top->name, top->res.icog);
1862 break;
1863 case res_men:
1864 if(!top->binres)
1865 top->binres = menu2res(top->name, top->res.men);
1866 break;
1867 case res_html:
1868 if(!top->binres)
1869 top->binres = html2res(top->name, top->res.html);
1870 break;
1871 case res_rdt:
1872 if(!top->binres)
1873 top->binres = rcdata2res(top->name, top->res.rdt);
1874 break;
1875 case res_stt:
1876 if(!top->binres)
1877 top->binres = stringtable2res(top->res.stt);
1878 break;
1879 case res_usr:
1880 if(!top->binres)
1881 top->binres = user2res(top->name, top->res.usr);
1882 break;
1883 case res_msg:
1884 if(!top->binres)
1885 top->binres = messagetable2res(top->name, top->res.msg);
1886 break;
1887 case res_ver:
1888 if(!top->binres)
1889 top->binres = versioninfo2res(top->name, top->res.ver);
1890 break;
1891 case res_toolbar:
1892 if(!top->binres)
1893 top->binres = toolbar2res(top->name, top->res.tbt);
1894 break;
1895 case res_dlginit:
1896 if(!top->binres)
1897 top->binres = dlginit2res(top->name, top->res.dlgi);
1898 break;
1899 case res_anicur:
1900 case res_aniico:
1901 if(!top->binres)
1902 top->binres = anicurico2res(top->name, top->res.ani, top->type);
1903 break;
1904 default:
1905 internal_error(__FILE__, __LINE__, "Unknown resource type encountered %d in binary res generation\n", top->type);
1907 top->c_name = make_c_name(get_c_typename(top->type), top->name, top->lan);
1908 top = top->next;