kernel32: Implement GetCurrentThreadStackLimits.
[wine.git] / tools / wrc / genres.c
blob939d2655b2771c2dd98192f9f9305874fef56806
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);
730 put_raw_data(res, ctrl->extra, 0);
732 else
733 put_word(res, 0);
735 if(ctrl->next)
736 put_pad(res);
737 nctrl++;
738 ctrl = ctrl->next;
740 /* Set number of controls */
741 set_word(res, tag_nctrl, (WORD)nctrl);
743 else /* win16 */
745 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, NULL);
747 put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
748 tag_nctrl = res->size;
749 put_byte(res, 0); /* Number of controls */
750 put_word(res, dlg->x);
751 put_word(res, dlg->y);
752 put_word(res, dlg->width);
753 put_word(res, dlg->height);
754 if(dlg->menu)
755 put_name_id(res, dlg->menu, TRUE, NULL);
756 else
757 put_byte(res, 0);
758 if(dlg->dlgclass)
759 put_name_id(res, dlg->dlgclass, TRUE, NULL);
760 else
761 put_byte(res, 0);
762 if(dlg->title)
763 put_string(res, dlg->title, str_char, TRUE, NULL);
764 else
765 put_byte(res, 0);
766 if(dlg->font)
768 put_word(res, dlg->font->size);
769 put_string(res, dlg->font->name, str_char, TRUE, NULL);
772 while(ctrl)
774 put_word(res, ctrl->x);
775 put_word(res, ctrl->y);
776 put_word(res, ctrl->width);
777 put_word(res, ctrl->height);
778 put_word(res, ctrl->id);
779 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
780 if(ctrl->ctlclass)
782 if(ctrl->ctlclass->type == name_ord
783 && ctrl->ctlclass->name.i_name >= 0x80
784 && ctrl->ctlclass->name.i_name <= 0x85)
785 put_byte(res, ctrl->ctlclass->name.i_name);
786 else if(ctrl->ctlclass->type == name_str)
787 put_name_id(res, ctrl->ctlclass, FALSE, NULL);
788 else
789 error("Unknown control-class %04x\n", ctrl->ctlclass->name.i_name);
791 else
792 internal_error(__FILE__, __LINE__, "Control has no control-class\n");
793 if(ctrl->title)
794 put_name_id(res, ctrl->title, FALSE, NULL);
795 else
796 put_byte(res, 0);
798 /* FIXME: What is this extra byte doing here? */
799 put_byte(res, 0);
801 nctrl++;
802 ctrl = ctrl->next;
804 /* Set number of controls */
805 ((char *)res->data)[tag_nctrl] = (char)nctrl;
807 /* Set ResourceSize */
808 SetResSize(res, restag);
809 return res;
813 *****************************************************************************
814 * Function : menuitem2res
815 * Syntax : void menuitem2res(res_t *res, menu_item_t *item)
816 * Input :
817 * Output :
818 * Description :
819 * Remarks : Self recursive
820 *****************************************************************************
822 static void menuitem2res(res_t *res, menu_item_t *menitem, const language_t *lang)
824 menu_item_t *itm = menitem;
825 if(win32)
827 while(itm)
829 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
830 if(!itm->popup)
831 put_word(res, itm->id);
832 if(itm->name)
833 put_string(res, itm->name, str_unicode, TRUE, lang);
834 else
835 put_word(res, 0);
836 if(itm->popup)
837 menuitem2res(res, itm->popup, lang);
838 itm = itm->next;
841 else /* win16 */
843 while(itm)
845 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
846 if(!itm->popup)
847 put_word(res, itm->id);
848 if(itm->name)
849 put_string(res, itm->name, str_char, TRUE, lang);
850 else
851 put_byte(res, 0);
852 if(itm->popup)
853 menuitem2res(res, itm->popup, lang);
854 itm = itm->next;
861 *****************************************************************************
862 * Function : menuexitem2res
863 * Syntax : void menuexitem2res(res_t *res, menuex_item_t *item)
864 * Input :
865 * Output : nop
866 * Description :
867 * Remarks : Self recursive
868 *****************************************************************************
870 static void menuexitem2res(res_t *res, menu_item_t *menitem, const language_t *lang)
872 menu_item_t *itm = menitem;
873 assert(win32 != 0);
874 while(itm)
876 put_dword(res, itm->gottype ? itm->type : 0);
877 put_dword(res, itm->gotstate ? itm->state : 0);
878 put_dword(res, itm->gotid ? itm->id : 0);
879 put_word(res, (itm->popup ? 0x01 : 0) | (!itm->next ? MF_END : 0));
880 if(itm->name)
881 put_string(res, itm->name, str_unicode, TRUE, lang);
882 else
883 put_word(res, 0);
884 put_pad(res);
885 if(itm->popup)
887 put_dword(res, itm->gothelpid ? itm->helpid : 0);
888 menuexitem2res(res, itm->popup, lang);
890 itm = itm->next;
896 *****************************************************************************
897 * Function : menu2res
898 * Syntax : res_t *menu2res(name_id_t *name, menu_t *men)
899 * Input :
900 * name - Name/ordinal of the resource
901 * menex - The menuex descriptor
902 * Output : New .res format structure
903 * Description :
904 * Remarks :
905 *****************************************************************************
907 static res_t *menu2res(name_id_t *name, menu_t *men)
909 int restag;
910 res_t *res;
911 assert(name != NULL);
912 assert(men != NULL);
914 res = new_res();
915 if(win32)
917 restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, &(men->lvc));
919 if (men->is_ex)
921 put_word(res, 1); /* Menuheader: Version */
922 put_word(res, 4); /* Offset */
923 put_dword(res, 0); /* HelpId */
924 put_pad(res);
925 menuexitem2res(res, men->items, men->lvc.language);
927 else
929 put_dword(res, 0); /* Menuheader: Version and HeaderSize */
930 menuitem2res(res, men->items, men->lvc.language);
932 /* Set ResourceSize */
933 SetResSize(res, restag);
934 put_pad(res);
936 else /* win16 */
938 restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, NULL);
940 put_dword(res, 0); /* Menuheader: Version and HeaderSize */
941 menuitem2res(res, men->items, NULL);
942 /* Set ResourceSize */
943 SetResSize(res, restag);
945 return res;
949 *****************************************************************************
950 * Function : cursorgroup2res
951 * Syntax : res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
952 * Input :
953 * name - Name/ordinal of the resource
954 * curg - The cursor descriptor
955 * Output : New .res format structure
956 * Description :
957 * Remarks :
958 *****************************************************************************
960 static res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
962 int restag;
963 res_t *res;
964 cursor_t *cur;
965 assert(name != NULL);
966 assert(curg != NULL);
968 res = new_res();
969 restag = put_res_header(res, WRC_RT_GROUP_CURSOR, NULL, name, curg->memopt, &(curg->lvc));
971 put_word(res, 0); /* Reserved */
972 /* FIXME: The ResType in the NEWHEADER structure should
973 * contain 14 according to the MS win32 doc. This is
974 * not the case with the BRC compiler and I really doubt
975 * the latter. Putting one here is compliant to win16 spec,
976 * but who knows the true value?
978 put_word(res, 2); /* ResType */
979 put_word(res, curg->ncursor);
980 #if 0
981 for(cur = curg->cursorlist; cur; cur = cur->next)
982 #else
983 cur = curg->cursorlist;
984 while(cur->next)
985 cur = cur->next;
986 for(; cur; cur = cur->prev)
987 #endif
989 put_word(res, cur->width);
990 /* FIXME: The height of a cursor is half the size of
991 * the bitmap's height. BRC puts the height from the
992 * BITMAPINFOHEADER here instead of the cursorfile's
993 * height. MS doesn't seem to care...
995 put_word(res, cur->height);
996 /* FIXME: The next two are reversed in BRC and I don't
997 * know why. Probably a bug. But, we can safely ignore
998 * it because win16 does not support color cursors.
999 * A warning should have been generated by the parser.
1001 put_word(res, cur->planes);
1002 put_word(res, cur->bits);
1003 /* FIXME: The +4 is the hotspot in the cursor resource.
1004 * However, I could not find this in the documentation.
1005 * The hotspot bytes must either be included or MS
1006 * doesn't care.
1008 put_dword(res, cur->data->size +4);
1009 put_word(res, cur->id);
1012 SetResSize(res, restag); /* Set ResourceSize */
1013 if(win32)
1014 put_pad(res);
1016 return res;
1020 *****************************************************************************
1021 * Function : cursor2res
1022 * Syntax : res_t *cursor2res(cursor_t *cur)
1023 * Input :
1024 * cur - The cursor descriptor
1025 * Output : New .res format structure
1026 * Description :
1027 * Remarks :
1028 *****************************************************************************
1030 static res_t *cursor2res(cursor_t *cur)
1032 int restag;
1033 res_t *res;
1034 name_id_t name;
1036 assert(cur != NULL);
1038 res = new_res();
1039 name.type = name_ord;
1040 name.name.i_name = cur->id;
1041 restag = put_res_header(res, WRC_RT_CURSOR, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(cur->lvc));
1042 put_word(res, cur->xhot);
1043 put_word(res, cur->yhot);
1044 put_raw_data(res, cur->data, 0);
1046 SetResSize(res, restag); /* Set ResourceSize */
1047 if(win32)
1048 put_pad(res);
1050 return res;
1054 *****************************************************************************
1055 * Function : icongroup2res
1056 * Syntax : res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1057 * Input :
1058 * name - Name/ordinal of the resource
1059 * icog - The icon group descriptor
1060 * Output : New .res format structure
1061 * Description :
1062 * Remarks :
1063 *****************************************************************************
1065 static res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1067 int restag;
1068 res_t *res;
1069 icon_t *ico;
1070 assert(name != NULL);
1071 assert(icog != NULL);
1073 res = new_res();
1074 restag = put_res_header(res, WRC_RT_GROUP_ICON, NULL, name, icog->memopt, &(icog->lvc));
1076 put_word(res, 0); /* Reserved */
1077 /* FIXME: The ResType in the NEWHEADER structure should
1078 * contain 14 according to the MS win32 doc. This is
1079 * not the case with the BRC compiler and I really doubt
1080 * the latter. Putting one here is compliant to win16 spec,
1081 * but who knows the true value?
1083 put_word(res, 1); /* ResType */
1084 put_word(res, icog->nicon);
1085 for(ico = icog->iconlist; ico; ico = ico->next)
1087 put_byte(res, ico->width);
1088 put_byte(res, ico->height);
1089 put_byte(res, ico->nclr);
1090 put_byte(res, 0); /* Reserved */
1091 put_word(res, ico->planes);
1092 put_word(res, ico->bits);
1093 put_dword(res, ico->data->size);
1094 put_word(res, ico->id);
1097 SetResSize(res, restag); /* Set ResourceSize */
1098 if(win32)
1099 put_pad(res);
1101 return res;
1105 *****************************************************************************
1106 * Function : icon2res
1107 * Syntax : res_t *icon2res(icon_t *ico)
1108 * Input :
1109 * ico - The icon descriptor
1110 * Output : New .res format structure
1111 * Description :
1112 * Remarks :
1113 *****************************************************************************
1115 static res_t *icon2res(icon_t *ico)
1117 int restag;
1118 res_t *res;
1119 name_id_t name;
1121 assert(ico != NULL);
1123 res = new_res();
1124 name.type = name_ord;
1125 name.name.i_name = ico->id;
1126 restag = put_res_header(res, WRC_RT_ICON, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(ico->lvc));
1127 put_raw_data(res, ico->data, 0);
1129 SetResSize(res, restag); /* Set ResourceSize */
1130 if(win32)
1131 put_pad(res);
1133 return res;
1137 *****************************************************************************
1138 * Function : anicurico2res
1139 * Syntax : res_t *anicurico2res(name_id_t *name, ani_curico_t *ani)
1140 * Input :
1141 * name - Name/ordinal of the resource
1142 * ani - The animated object descriptor
1143 * Output : New .res format structure
1144 * Description :
1145 * Remarks : The endian of the object's structures have been converted
1146 * by the loader.
1147 * There are rumors that win311 could handle animated stuff.
1148 * That is why they are generated for both win16 and win32
1149 * compile.
1150 *****************************************************************************
1152 static res_t *anicurico2res(name_id_t *name, ani_curico_t *ani, enum res_e type)
1154 int restag;
1155 res_t *res;
1156 assert(name != NULL);
1157 assert(ani != NULL);
1159 res = new_res();
1160 restag = put_res_header(res, type == res_anicur ? WRC_RT_ANICURSOR : WRC_RT_ANIICON,
1161 NULL, name, ani->memopt, NULL);
1162 put_raw_data(res, ani->data, 0);
1163 /* Set ResourceSize */
1164 SetResSize(res, restag);
1165 if(win32)
1166 put_pad(res);
1167 return res;
1171 *****************************************************************************
1172 * Function : bitmap2res
1173 * Syntax : res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1174 * Input :
1175 * name - Name/ordinal of the resource
1176 * bmp - The bitmap descriptor
1177 * Output : New .res format structure
1178 * Description :
1179 * Remarks : The endian of the bitmap structures have been converted
1180 * by the loader.
1181 *****************************************************************************
1183 static res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1185 int restag;
1186 res_t *res;
1187 assert(name != NULL);
1188 assert(bmp != NULL);
1190 res = new_res();
1191 restag = put_res_header(res, WRC_RT_BITMAP, NULL, name, bmp->memopt, &(bmp->data->lvc));
1192 if(bmp->data->data[0] == 'B'
1193 && bmp->data->data[1] == 'M'
1194 && ((BITMAPFILEHEADER *)bmp->data->data)->bfSize == bmp->data->size
1195 && bmp->data->size >= sizeof(BITMAPFILEHEADER))
1197 /* The File header is still attached, don't write it */
1198 put_raw_data(res, bmp->data, sizeof(BITMAPFILEHEADER));
1200 else
1202 put_raw_data(res, bmp->data, 0);
1204 /* Set ResourceSize */
1205 SetResSize(res, restag);
1206 if(win32)
1207 put_pad(res);
1208 return res;
1212 *****************************************************************************
1213 * Function : font2res
1214 * Syntax : res_t *font2res(name_id_t *name, font_t *fnt)
1215 * Input :
1216 * name - Name/ordinal of the resource
1217 * fnt - The font descriptor
1218 * Output : New .res format structure
1219 * Description :
1220 * Remarks : The data has been prepared just after parsing.
1221 *****************************************************************************
1223 static res_t *font2res(name_id_t *name, font_t *fnt)
1225 int restag;
1226 res_t *res;
1227 assert(name != NULL);
1228 assert(fnt != NULL);
1230 res = new_res();
1231 restag = put_res_header(res, WRC_RT_FONT, NULL, name, fnt->memopt, &(fnt->data->lvc));
1232 put_raw_data(res, fnt->data, 0);
1233 /* Set ResourceSize */
1234 SetResSize(res, restag);
1235 if(win32)
1236 put_pad(res);
1237 return res;
1241 *****************************************************************************
1242 * Function : fontdir2res
1243 * Syntax : res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1244 * Input :
1245 * name - Name/ordinal of the resource
1246 * fntdir - The fontdir descriptor
1247 * Output : New .res format structure
1248 * Description :
1249 * Remarks : The data has been prepared just after parsing.
1250 *****************************************************************************
1252 static res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1254 int restag;
1255 res_t *res;
1256 assert(name != NULL);
1257 assert(fnd != NULL);
1259 res = new_res();
1260 restag = put_res_header(res, WRC_RT_FONTDIR, NULL, name, fnd->memopt, &(fnd->data->lvc));
1261 put_raw_data(res, fnd->data, 0);
1262 /* Set ResourceSize */
1263 SetResSize(res, restag);
1264 if(win32)
1265 put_pad(res);
1266 return res;
1270 *****************************************************************************
1271 * Function : html2res
1272 * Syntax : res_t *html2res(name_id_t *name, html_t *html)
1273 * Input :
1274 * name - Name/ordinal of the resource
1275 * rdt - The html descriptor
1276 * Output : New .res format structure
1277 * Description :
1278 * Remarks :
1279 *****************************************************************************
1281 static res_t *html2res(name_id_t *name, html_t *html)
1283 int restag;
1284 res_t *res;
1285 assert(name != NULL);
1286 assert(html != NULL);
1288 res = new_res();
1289 restag = put_res_header(res, WRC_RT_HTML, NULL, name, html->memopt, &(html->data->lvc));
1290 put_raw_data(res, html->data, 0);
1291 /* Set ResourceSize */
1292 SetResSize(res, restag);
1293 if(win32)
1294 put_pad(res);
1295 return res;
1299 *****************************************************************************
1300 * Function : rcdata2res
1301 * Syntax : res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1302 * Input :
1303 * name - Name/ordinal of the resource
1304 * rdt - The rcdata descriptor
1305 * Output : New .res format structure
1306 * Description :
1307 * Remarks :
1308 *****************************************************************************
1310 static res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1312 int restag;
1313 res_t *res;
1314 assert(name != NULL);
1315 assert(rdt != NULL);
1317 res = new_res();
1318 restag = put_res_header(res, WRC_RT_RCDATA, NULL, name, rdt->memopt, &(rdt->data->lvc));
1319 put_raw_data(res, rdt->data, 0);
1320 /* Set ResourceSize */
1321 SetResSize(res, restag);
1322 if(win32)
1323 put_pad(res);
1324 return res;
1328 *****************************************************************************
1329 * Function : messagetable2res
1330 * Syntax : res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1331 * Input :
1332 * name - Name/ordinal of the resource
1333 * msg - The messagetable descriptor
1334 * Output : New .res format structure
1335 * Description :
1336 * Remarks : The data has been converted to the appropriate endian
1337 * after it was parsed.
1338 *****************************************************************************
1340 static res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1342 int restag;
1343 res_t *res;
1344 assert(name != NULL);
1345 assert(msg != NULL);
1347 res = new_res();
1348 restag = put_res_header(res, WRC_RT_MESSAGETABLE, NULL, name, msg->memopt, &(msg->data->lvc));
1349 put_raw_data(res, msg->data, 0);
1350 /* Set ResourceSize */
1351 SetResSize(res, restag);
1352 if(win32)
1353 put_pad(res);
1354 return res;
1358 *****************************************************************************
1359 * Function : stringtable2res
1360 * Syntax : res_t *stringtable2res(stringtable_t *stt)
1361 * Input :
1362 * stt - The stringtable descriptor
1363 * Output : New .res format structure
1364 * Description :
1365 * Remarks :
1366 *****************************************************************************
1368 static res_t *stringtable2res(stringtable_t *stt)
1370 res_t *res;
1371 name_id_t name;
1372 int i;
1373 int restag;
1374 DWORD lastsize = 0;
1376 assert(stt != NULL);
1377 res = new_res();
1379 for(; stt; stt = stt->next)
1381 if(!stt->nentries)
1383 warning("Empty internal stringtable\n");
1384 continue;
1386 name.type = name_ord;
1387 name.name.i_name = (stt->idbase >> 4) + 1;
1388 restag = put_res_header(res, WRC_RT_STRING, NULL, &name, stt->memopt, win32 ? &(stt->lvc) : NULL);
1389 for(i = 0; i < stt->nentries; i++)
1391 if(stt->entries[i].str && stt->entries[i].str->size)
1393 put_string(res, stt->entries[i].str, win32 ? str_unicode : str_char,
1394 FALSE, win32 ? stt->lvc.language : NULL);
1396 else
1398 if (win32)
1399 put_word(res, 0);
1400 else
1401 put_byte(res, 0);
1404 /* Set ResourceSize */
1405 SetResSize(res, restag - lastsize);
1406 if(win32)
1407 put_pad(res);
1408 lastsize = res->size;
1410 return res;
1414 *****************************************************************************
1415 * Function : user2res
1416 * Syntax : res_t *user2res(name_id_t *name, user_t *usr)
1417 * Input :
1418 * name - Name/ordinal of the resource
1419 * usr - The userresource descriptor
1420 * Output : New .res format structure
1421 * Description :
1422 * Remarks :
1423 *****************************************************************************
1425 static res_t *user2res(name_id_t *name, user_t *usr)
1427 int restag;
1428 res_t *res;
1429 assert(name != NULL);
1430 assert(usr != NULL);
1432 res = new_res();
1433 restag = put_res_header(res, 0, usr->type, name, usr->memopt, &(usr->data->lvc));
1434 put_raw_data(res, usr->data, 0);
1435 /* Set ResourceSize */
1436 SetResSize(res, restag);
1437 if(win32)
1438 put_pad(res);
1439 return res;
1443 *****************************************************************************
1444 * Function : versionblock2res
1445 * Syntax : void versionblock2res(res_t *res, ver_block_t *blk)
1446 * Input :
1447 * res - Binary resource to write to
1448 * blk - The version block to be written
1449 * Output :
1450 * Description :
1451 * Remarks : Self recursive
1452 *****************************************************************************
1454 static void versionblock2res(res_t *res, ver_block_t *blk, int level, const language_t *lang)
1456 ver_value_t *val;
1457 int blksizetag;
1458 int valblksizetag;
1459 int valvalsizetag;
1460 int tag;
1461 int i;
1463 blksizetag = res->size;
1464 put_word(res, 0); /* Will be overwritten later */
1465 put_word(res, 0);
1466 if(win32)
1467 put_word(res, 0); /* level ? */
1468 put_string(res, blk->name, win32 ? str_unicode : str_char, TRUE, lang);
1469 put_pad(res);
1470 for(val = blk->values; val; val = val->next)
1472 if(val->type == val_str)
1474 valblksizetag = res->size;
1475 put_word(res, 0); /* Will be overwritten later */
1476 valvalsizetag = res->size;
1477 put_word(res, 0); /* Will be overwritten later */
1478 if(win32)
1480 put_word(res, level);
1482 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
1483 put_pad(res);
1484 tag = res->size;
1485 put_string(res, val->value.str, win32 ? str_unicode : str_char, TRUE, lang);
1486 if(win32)
1487 set_word(res, valvalsizetag, (WORD)((res->size - tag) >> 1));
1488 else
1489 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1490 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1491 put_pad(res);
1493 else if(val->type == val_words)
1495 valblksizetag = res->size;
1496 put_word(res, 0); /* Will be overwritten later */
1497 valvalsizetag = res->size;
1498 put_word(res, 0); /* Will be overwritten later */
1499 if(win32)
1501 put_word(res, level);
1503 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
1504 put_pad(res);
1505 tag = res->size;
1506 for(i = 0; i < val->value.words->nwords; i++)
1508 put_word(res, val->value.words->words[i]);
1510 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1511 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1512 put_pad(res);
1514 else if(val->type == val_block)
1516 versionblock2res(res, val->value.block, level+1, lang);
1518 else
1520 internal_error(__FILE__, __LINE__, "Invalid value indicator %d in VERSIONINFO\n", val->type);
1524 /* Set blocksize */
1525 set_word(res, blksizetag, (WORD)(res->size - blksizetag));
1529 *****************************************************************************
1530 * Function : versioninfo2res
1531 * Syntax : res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1532 * Input :
1533 * name - Name/ordinal of the resource
1534 * ver - The versioninfo descriptor
1535 * Output : New .res format structure
1536 * Description :
1537 * Remarks :
1538 *****************************************************************************
1540 static res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1542 int restag;
1543 int rootblocksizetag;
1544 int valsizetag;
1545 int tag;
1546 res_t *res;
1547 string_t vsvi;
1548 ver_block_t *blk;
1550 assert(name != NULL);
1551 assert(ver != NULL);
1553 vsvi.type = str_char;
1554 vsvi.str.cstr = xstrdup("VS_VERSION_INFO");
1555 vsvi.size = 15; /* Excl. termination */
1557 res = new_res();
1558 restag = put_res_header(res, WRC_RT_VERSION, NULL, name, ver->memopt, &(ver->lvc));
1559 rootblocksizetag = res->size;
1560 put_word(res, 0); /* BlockSize filled in later */
1561 valsizetag = res->size;
1562 put_word(res, 0); /* ValueSize filled in later*/
1563 if(win32)
1564 put_word(res, 0); /* Tree-level ? */
1565 put_string(res, &vsvi, win32 ? str_unicode : str_char,
1566 TRUE, win32 ? ver->lvc.language : NULL);
1567 if(win32)
1568 put_pad(res);
1569 tag = res->size;
1570 put_dword(res, VS_FFI_SIGNATURE);
1571 put_dword(res, VS_FFI_STRUCVERSION);
1572 put_dword(res, (ver->filever_maj1 << 16) + (ver->filever_maj2 & 0xffff));
1573 put_dword(res, (ver->filever_min1 << 16) + (ver->filever_min2 & 0xffff));
1574 put_dword(res, (ver->prodver_maj1 << 16) + (ver->prodver_maj2 & 0xffff));
1575 put_dword(res, (ver->prodver_min1 << 16) + (ver->prodver_min2 & 0xffff));
1576 put_dword(res, ver->fileflagsmask);
1577 put_dword(res, ver->fileflags);
1578 put_dword(res, ver->fileos);
1579 put_dword(res, ver->filetype);
1580 put_dword(res, ver->filesubtype);
1581 put_dword(res, 0); /* FileDateMS */
1582 put_dword(res, 0); /* FileDateLS */
1583 /* Set ValueSize */
1584 set_word(res, valsizetag, (WORD)(res->size - tag));
1585 /* Descend into the blocks */
1586 for(blk = ver->blocks; blk; blk = blk->next)
1587 versionblock2res(res, blk, 0, win32 ? ver->lvc.language : NULL);
1588 /* Set root block's size */
1589 set_word(res, rootblocksizetag, (WORD)(res->size - rootblocksizetag));
1591 SetResSize(res, restag);
1592 if(win32)
1593 put_pad(res);
1595 free(vsvi.str.cstr);
1596 return res;
1600 *****************************************************************************
1601 * Function : toolbaritem2res
1602 * Syntax : void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1603 * Input :
1604 * Output : nop
1605 * Description :
1606 * Remarks : Self recursive
1607 *****************************************************************************
1609 static void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1611 toolbar_item_t *itm = tbitem;
1612 assert(win32 != 0);
1613 while(itm)
1615 put_word(res, itm->id);
1616 itm = itm->next;
1622 *****************************************************************************
1623 * Function : toolbar2res
1624 * Syntax : res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1625 * Input :
1626 * name - Name/ordinal of the resource
1627 * toolbar - The toolbar descriptor
1628 * Output : New .res format structure
1629 * Description :
1630 * Remarks :
1631 *****************************************************************************
1633 static res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1635 int restag;
1636 res_t *res;
1637 assert(name != NULL);
1638 assert(toolbar != NULL);
1640 res = new_res();
1641 if(win32)
1643 restag = put_res_header(res, WRC_RT_TOOLBAR, NULL, name, toolbar->memopt, &(toolbar->lvc));
1645 put_word(res, 1); /* Menuheader: Version */
1646 put_word(res, toolbar->button_width); /* (in pixels?) */
1647 put_word(res, toolbar->button_height); /* (in pixels?) */
1648 put_word(res, toolbar->nitems);
1649 put_pad(res);
1650 toolbaritem2res(res, toolbar->items);
1651 /* Set ResourceSize */
1652 SetResSize(res, restag);
1653 put_pad(res);
1655 else /* win16 */
1657 /* Do not generate anything in 16-bit mode */
1658 free(res->data);
1659 free(res);
1660 return NULL;
1662 return res;
1666 *****************************************************************************
1667 * Function : dlginit2res
1668 * Syntax : res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1669 * Input :
1670 * name - Name/ordinal of the resource
1671 * rdt - The dlginit descriptor
1672 * Output : New .res format structure
1673 * Description :
1674 * Remarks :
1675 *****************************************************************************
1677 static res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1679 int restag;
1680 res_t *res;
1681 assert(name != NULL);
1682 assert(dit != NULL);
1684 res = new_res();
1685 restag = put_res_header(res, WRC_RT_DLGINIT, NULL, name, dit->memopt, &(dit->data->lvc));
1686 put_raw_data(res, dit->data, 0);
1687 /* Set ResourceSize */
1688 SetResSize(res, restag);
1689 if(win32)
1690 put_pad(res);
1691 return res;
1695 *****************************************************************************
1696 * Function : prep_nid_for_label
1697 * Syntax : char *prep_nid_for_label(const name_id_t *nid)
1698 * Input :
1699 * Output :
1700 * Description : Converts a resource name into the first 32 (or less)
1701 * characters of the name with conversions.
1702 * Remarks :
1703 *****************************************************************************
1705 #define MAXNAMELEN 32
1706 char *prep_nid_for_label(const name_id_t *nid)
1708 static char buf[MAXNAMELEN+1];
1710 assert(nid != NULL);
1712 if(nid->type == name_str && nid->name.s_name->type == str_unicode)
1714 WCHAR *sptr;
1715 int i;
1716 sptr = nid->name.s_name->str.wstr;
1717 buf[0] = '\0';
1718 for(i = 0; *sptr && i < MAXNAMELEN; i++)
1720 if((unsigned)*sptr < 0x80 && isprint(*sptr & 0xff))
1721 buf[i] = *sptr++;
1722 else
1723 warning("Resourcename (str_unicode) contain unprintable characters or invalid translation, ignored\n");
1725 buf[i] = '\0';
1727 else if(nid->type == name_str && nid->name.s_name->type == str_char)
1729 char *cptr;
1730 int i;
1731 cptr = nid->name.s_name->str.cstr;
1732 buf[0] = '\0';
1733 for(i = 0; *cptr && i < MAXNAMELEN; i++)
1735 if((unsigned)*cptr < 0x80 && isprint(*cptr & 0xff))
1736 buf[i] = *cptr++;
1737 else
1738 warning("Resourcename (str_char) contain unprintable characters, ignored\n");
1740 buf[i] = '\0';
1742 else if(nid->type == name_ord)
1744 sprintf(buf, "%u", nid->name.i_name);
1746 else
1748 internal_error(__FILE__, __LINE__, "Resource name_id with invalid type %d\n", nid->type);
1750 return buf;
1752 #undef MAXNAMELEN
1755 *****************************************************************************
1756 * Function : make_c_name
1757 * Syntax : char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
1758 * Input :
1759 * Output :
1760 * Description : Converts a resource name into a valid c-identifier in the
1761 * form "_base_nid".
1762 * Remarks :
1763 *****************************************************************************
1765 char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
1767 char *buf = prep_nid_for_label(nid);
1768 return strmake( "_%s_%s_%d", base, buf, lan ? MAKELANGID(lan->id, lan->sub) : 0);
1772 *****************************************************************************
1773 * Function : get_c_typename
1774 * Syntax : const char *get_c_typename(enum res_e type)
1775 * Input :
1776 * Output :
1777 * Description : Convert resource enum to char string to be used in c-name
1778 * creation.
1779 * Remarks :
1780 *****************************************************************************
1782 const char *get_c_typename(enum res_e type)
1784 switch(type)
1786 case res_acc: return "Acc";
1787 case res_anicur:return "AniCur";
1788 case res_aniico:return "AniIco";
1789 case res_bmp: return "Bmp";
1790 case res_cur: return "Cur";
1791 case res_curg: return "CurGrp";
1792 case res_dlg: return "Dlg";
1793 case res_fnt: return "Fnt";
1794 case res_fntdir:return "FntDir";
1795 case res_ico: return "Ico";
1796 case res_icog: return "IcoGrp";
1797 case res_men: return "Men";
1798 case res_rdt: return "RCDat";
1799 case res_stt: return "StrTab";
1800 case res_usr: return "Usr";
1801 case res_msg: return "MsgTab";
1802 case res_ver: return "VerInf";
1803 case res_toolbar: return "TlBr";
1804 case res_dlginit: return "DlgInit";
1805 default: return "Oops";
1810 *****************************************************************************
1811 * Function : resources2res
1812 * Syntax : void resources2res(resource_t *top)
1813 * Input :
1814 * top - The resource-tree to convert
1815 * Output :
1816 * Description : Convert logical resource descriptors into binary data
1817 * Remarks :
1818 *****************************************************************************
1820 void resources2res(resource_t *top)
1822 while(top)
1824 switch(top->type)
1826 case res_acc:
1827 if(!top->binres)
1828 top->binres = accelerator2res(top->name, top->res.acc);
1829 break;
1830 case res_bmp:
1831 if(!top->binres)
1832 top->binres = bitmap2res(top->name, top->res.bmp);
1833 break;
1834 case res_cur:
1835 if(!top->binres)
1836 top->binres = cursor2res(top->res.cur);
1837 break;
1838 case res_curg:
1839 if(!top->binres)
1840 top->binres = cursorgroup2res(top->name, top->res.curg);
1841 break;
1842 case res_dlg:
1843 if(!top->binres)
1844 top->binres = dialog2res(top->name, top->res.dlg);
1845 break;
1846 case res_fnt:
1847 if(!top->binres)
1848 top->binres = font2res(top->name, top->res.fnt);
1849 break;
1850 case res_fntdir:
1851 if(!top->binres)
1852 top->binres = fontdir2res(top->name, top->res.fnd);
1853 break;
1854 case res_ico:
1855 if(!top->binres)
1856 top->binres = icon2res(top->res.ico);
1857 break;
1858 case res_icog:
1859 if(!top->binres)
1860 top->binres = icongroup2res(top->name, top->res.icog);
1861 break;
1862 case res_men:
1863 if(!top->binres)
1864 top->binres = menu2res(top->name, top->res.men);
1865 break;
1866 case res_html:
1867 if(!top->binres)
1868 top->binres = html2res(top->name, top->res.html);
1869 break;
1870 case res_rdt:
1871 if(!top->binres)
1872 top->binres = rcdata2res(top->name, top->res.rdt);
1873 break;
1874 case res_stt:
1875 if(!top->binres)
1876 top->binres = stringtable2res(top->res.stt);
1877 break;
1878 case res_usr:
1879 if(!top->binres)
1880 top->binres = user2res(top->name, top->res.usr);
1881 break;
1882 case res_msg:
1883 if(!top->binres)
1884 top->binres = messagetable2res(top->name, top->res.msg);
1885 break;
1886 case res_ver:
1887 if(!top->binres)
1888 top->binres = versioninfo2res(top->name, top->res.ver);
1889 break;
1890 case res_toolbar:
1891 if(!top->binres)
1892 top->binres = toolbar2res(top->name, top->res.tbt);
1893 break;
1894 case res_dlginit:
1895 if(!top->binres)
1896 top->binres = dlginit2res(top->name, top->res.dlgi);
1897 break;
1898 case res_anicur:
1899 case res_aniico:
1900 if(!top->binres)
1901 top->binres = anicurico2res(top->name, top->res.ani, top->type);
1902 break;
1903 default:
1904 internal_error(__FILE__, __LINE__, "Unknown resource type encountered %d in binary res generation\n", top->type);
1906 top->c_name = make_c_name(get_c_typename(top->type), top->name, top->lan);
1907 top = top->next;