Rewrote the collapsing of . and .. in RtlGetFullPathName_U for better
[wine/multimedia.git] / tools / wrc / genres.c
blob069778d87f1d59678d6442495eb47edd4c0a354e
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * History:
21 * 05-May-2000 BS - Added code to support endian conversions. The
22 * extra functions also aid unaligned access, but
23 * this is not yet implemented.
24 * 25-May-1998 BS - Added simple unicode -> char conversion for resource
25 * names in .s and .h files.
28 #include "config.h"
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdarg.h>
34 #include <assert.h>
35 #include <ctype.h>
37 #include "wrc.h"
38 #include "genres.h"
39 #include "utils.h"
40 #include "windef.h"
41 #include "winbase.h"
42 #include "wingdi.h"
43 #include "winuser.h"
44 #include "wine/unicode.h"
46 #define SetResSize(res, tag) set_dword((res), (tag), (res)->size - get_dword((res), (tag)))
48 res_t *new_res(void)
50 res_t *r;
51 r = (res_t *)xmalloc(sizeof(res_t));
52 r->allocsize = RES_BLOCKSIZE;
53 r->size = 0;
54 r->data = (char *)xmalloc(RES_BLOCKSIZE);
55 return r;
58 res_t *grow_res(res_t *r, int add)
60 r->allocsize += add;
61 r->data = (char *)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 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 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 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_word
217 * get_dword
218 * Syntax : WORD get_word(res_t *res, int ofs)
219 * DWORD get_dword(res_t *res, int ofs)
220 * Input :
221 * res - Binary resource to put the data in
222 * ofs - Byte offset in data-array
223 * Output : The data in native endian
224 * Description : Get the value of a binary resource data array in native
225 * endian.
226 * Remarks :
227 *****************************************************************************
229 WORD get_word(res_t *res, int ofs)
231 switch(byteorder)
233 #ifdef WORDS_BIGENDIAN
234 default:
235 #endif
236 case WRC_BO_BIG:
237 return (res->data[ofs+0] << 8)
238 | res->data[ofs+1];
240 #ifndef WORDS_BIGENDIAN
241 default:
242 #endif
243 case WRC_BO_LITTLE:
244 return (res->data[ofs+1] << 8)
245 | res->data[ofs+0];
249 DWORD get_dword(res_t *res, int ofs)
251 switch(byteorder)
253 #ifdef WORDS_BIGENDIAN
254 default:
255 #endif
256 case WRC_BO_BIG:
257 return (res->data[ofs+0] << 24)
258 | (res->data[ofs+1] << 16)
259 | (res->data[ofs+2] << 8)
260 | res->data[ofs+3];
262 #ifndef WORDS_BIGENDIAN
263 default:
264 #endif
265 case WRC_BO_LITTLE:
266 return (res->data[ofs+3] << 24)
267 | (res->data[ofs+2] << 16)
268 | (res->data[ofs+1] << 8)
269 | res->data[ofs+0];
274 *****************************************************************************
275 * Function : string_to_upper
276 * Syntax : void string_to_upper(string_t *str)
277 * Input :
278 * Output :
279 * Description :
280 * Remarks : FIXME: codepages...
281 *****************************************************************************
283 static void string_to_upper(string_t *str)
285 int i;
287 if(str->type == str_char)
289 for (i = 0; i < str->size; i++) str->str.cstr[i] = toupper((unsigned char)str->str.cstr[i]);
291 else if(str->type == str_unicode)
293 for (i = 0; i < str->size; i++) str->str.wstr[i] = toupperW(str->str.wstr[i]);
295 else
297 internal_error(__FILE__, __LINE__, "Invalid string type %d", str->type);
302 *****************************************************************************
303 * Function : put_string
304 * Syntax : void put_string(res_t *res, string_t *str, enum str_e type,
305 * int isterm, const language_t *lang)
306 * Input :
307 * res - Binary resource to put the data in
308 * str - String to put
309 * type - Data has to be written in either str_char or str_unicode
310 * isterm - The string is '\0' terminated (disregard the string's
311 * size member)
312 * Output : nop
313 * Description :
314 * Remarks :
315 *****************************************************************************
317 static void put_string(res_t *res, const string_t *str, enum str_e type, int isterm,
318 const language_t *lang)
320 int cnt, codepage;
321 string_t *newstr;
323 assert(res != NULL);
324 assert(str != NULL);
326 if (lang) codepage = get_language_codepage( lang->id, lang->sub );
327 else codepage = get_language_codepage( 0, 0 );
329 assert( codepage != -1 );
331 newstr = convert_string(str, type, codepage);
332 if (type == str_unicode)
334 if (str->type == str_char)
336 if (!check_unicode_conversion( str, newstr, codepage ))
337 error( "String %s does not convert identically to Unicode and back in codepage %d. "
338 "Try using a Unicode string instead.", str->str.cstr, codepage );
340 if (!isterm) put_word(res, newstr->size);
341 for(cnt = 0; cnt < newstr->size; cnt++)
343 WCHAR c = newstr->str.wstr[cnt];
344 if (isterm && !c) break;
345 put_word(res, c);
347 if (isterm) put_word(res, 0);
349 else /* str_char */
351 if (!isterm) put_byte(res, newstr->size);
352 for(cnt = 0; cnt < newstr->size; cnt++)
354 char c = newstr->str.cstr[cnt];
355 if (isterm && !c) break;
356 put_byte(res, c);
358 if (isterm) put_byte(res, 0);
360 free_string(newstr);
364 *****************************************************************************
365 * Function : put_name_id
366 * Syntax : void put_name_id(res_t *res, name_id_t *nid, int upcase, const language_t *lang)
367 * Input :
368 * Output :
369 * Description :
370 * Remarks :
371 *****************************************************************************
373 static void put_name_id(res_t *res, name_id_t *nid, int upcase, const language_t *lang)
375 if(nid->type == name_ord)
377 if(win32)
378 put_word(res, 0xffff);
379 else
380 put_byte(res, 0xff);
381 put_word(res, (WORD)nid->name.i_name);
383 else if(nid->type == name_str)
385 if(upcase)
386 string_to_upper(nid->name.s_name);
387 put_string(res, nid->name.s_name, win32 ? str_unicode : str_char, TRUE, lang);
389 else
391 internal_error(__FILE__, __LINE__, "Invalid name_id type %d", nid->type);
396 *****************************************************************************
397 * Function : put_lvc
398 * Syntax : void put_lvc(res_t *res, lvc_t *lvc)
399 * Input :
400 * Output :
401 * Description :
402 * Remarks :
403 *****************************************************************************
405 static void put_lvc(res_t *res, lvc_t *lvc)
407 if(lvc && lvc->language)
408 put_word(res, MAKELANGID(lvc->language->id, lvc->language->sub));
409 else
410 put_word(res, 0); /* Neutral */
411 if(lvc && lvc->version)
412 put_dword(res, *(lvc->version));
413 else
414 put_dword(res, 0);
415 if(lvc && lvc->characts)
416 put_dword(res, *(lvc->characts));
417 else
418 put_dword(res, 0);
422 *****************************************************************************
423 * Function : put_raw_data
424 * Syntax : void put_raw_data(res_t *res, raw_data_t *raw, int offset)
425 * Input :
426 * Output :
427 * Description :
428 * Remarks :
429 *****************************************************************************
431 void put_raw_data(res_t *res, raw_data_t *raw, int offset)
433 int wsize = raw->size - offset;
434 if(res->allocsize - res->size < wsize)
435 grow_res(res, wsize);
436 memcpy(&(res->data[res->size]), raw->data + offset, wsize);
437 res->size += wsize;
441 *****************************************************************************
442 * Function : put_res_header
443 * Syntax : intput_res_header(res_t *res, int type, name_id_t *ntype,
444 * name_id_t *name, DWORD memopt, lvc_t *lvc)
446 * Input :
447 * res - Binary resource descriptor to write to
448 * type - Resource identifier (if ntype == NULL)
449 * ntype - Name id of type
450 * name - Resource's name
451 * memopt - Resource's memory options to write
452 * lvc - Language, version and characteristics (win32 only)
453 * Output : An index to the resource size field. The resource size field
454 * contains the header size upon exit.
455 * Description :
456 * Remarks :
457 *****************************************************************************
459 int put_res_header(res_t *res, int type, name_id_t *ntype, name_id_t *name,
460 DWORD memopt, lvc_t *lvc)
462 if(win32)
464 put_dword(res, 0); /* We will overwrite these later */
465 put_dword(res, 0);
466 if(!ntype)
468 put_word(res, 0xffff); /* ResType */
469 put_word(res, type);
471 else
472 put_name_id(res, ntype, TRUE, lvc->language);
473 put_name_id(res, name, TRUE, lvc->language); /* ResName */
474 put_pad(res);
475 put_dword(res, 0); /* DataVersion */
476 put_word(res, memopt); /* Memory options */
477 put_lvc(res, lvc); /* Language, version and characts */
478 set_dword(res, 0*sizeof(DWORD), res->size); /* Set preliminary resource */
479 set_dword(res, 1*sizeof(DWORD), res->size); /* Set HeaderSize */
480 res->dataidx = res->size;
481 return 0;
483 else /* win16 */
485 int tag;
486 if(!ntype)
488 put_byte(res, 0xff); /* ResType */
489 put_word(res, type);
491 else
492 put_name_id(res, ntype, TRUE, NULL);
493 put_name_id(res, name, TRUE, NULL); /* ResName */
494 put_word(res, memopt); /* Memory options */
495 tag = res->size;
496 put_dword(res, 0); /* ResSize overwritten later*/
497 set_dword(res, tag, res->size);
498 res->dataidx = res->size;
499 return tag;
504 *****************************************************************************
505 * Function : accelerator2res
506 * Syntax : res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
507 * Input :
508 * name - Name/ordinal of the resource
509 * acc - The accelerator descriptor
510 * Output : New .res format structure
511 * Description :
512 * Remarks :
513 *****************************************************************************
515 static res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
517 int restag;
518 res_t *res;
519 event_t *ev;
520 assert(name != NULL);
521 assert(acc != NULL);
523 ev = acc->events;
524 res = new_res();
525 if(win32)
527 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, &(acc->lvc));
528 while(ev)
530 put_word(res, ev->flags | (ev->next ? 0 : 0x80));
531 put_word(res, ev->key);
532 put_word(res, ev->id);
533 put_word(res, 0); /* Padding */
534 ev = ev->next;
536 put_pad(res);
538 else /* win16 */
540 restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, NULL);
541 while(ev)
543 put_byte(res, ev->flags | (ev->next ? 0 : 0x80));
544 put_word(res, ev->key);
545 put_word(res, ev->id);
546 ev = ev->next;
549 /* Set ResourceSize */
550 SetResSize(res, restag);
551 return res;
555 *****************************************************************************
556 * Function : dialog2res
557 * Syntax : res_t *dialog2res(name_id_t *name, dialog_t *dlg)
558 * Input :
559 * name - Name/ordinal of the resource
560 * dlg - The dialog descriptor
561 * Output : New .res format structure
562 * Description :
563 * Remarks :
564 *****************************************************************************
566 static res_t *dialog2res(name_id_t *name, dialog_t *dlg)
568 int restag;
569 res_t *res;
570 control_t *ctrl;
571 int tag_nctrl;
572 int nctrl = 0;
573 assert(name != NULL);
574 assert(dlg != NULL);
576 ctrl = dlg->controls;
577 res = new_res();
578 if(win32)
580 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, &(dlg->lvc));
582 put_dword(res, dlg->style->or_mask);
583 put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
584 tag_nctrl = res->size;
585 put_word(res, 0); /* Number of controls */
586 put_word(res, dlg->x);
587 put_word(res, dlg->y);
588 put_word(res, dlg->width);
589 put_word(res, dlg->height);
590 if(dlg->menu)
591 put_name_id(res, dlg->menu, TRUE, dlg->lvc.language);
592 else
593 put_word(res, 0);
594 if(dlg->dlgclass)
595 put_name_id(res, dlg->dlgclass, TRUE, dlg->lvc.language);
596 else
597 put_word(res, 0);
598 if(dlg->title)
599 put_string(res, dlg->title, str_unicode, TRUE, dlg->lvc.language);
600 else
601 put_word(res, 0);
602 if(dlg->font)
604 put_word(res, dlg->font->size);
605 put_string(res, dlg->font->name, str_unicode, TRUE, dlg->lvc.language);
608 put_pad(res);
609 while(ctrl)
611 /* FIXME: what is default control style? */
612 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
613 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
614 put_word(res, ctrl->x);
615 put_word(res, ctrl->y);
616 put_word(res, ctrl->width);
617 put_word(res, ctrl->height);
618 put_word(res, ctrl->id);
619 if(ctrl->ctlclass)
620 put_name_id(res, ctrl->ctlclass, TRUE, dlg->lvc.language);
621 else
622 internal_error(__FILE__, __LINE__, "Control has no control-class");
623 if(ctrl->title)
624 put_name_id(res, ctrl->title, FALSE, dlg->lvc.language);
625 else
626 put_word(res, 0);
627 if(ctrl->extra)
629 put_word(res, ctrl->extra->size+2);
630 put_pad(res);
631 put_raw_data(res, ctrl->extra, 0);
633 else
634 put_word(res, 0);
636 if(ctrl->next)
637 put_pad(res);
638 nctrl++;
639 ctrl = ctrl->next;
641 /* Set number of controls */
642 set_word(res, tag_nctrl, (WORD)nctrl);
644 else /* win16 */
646 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, NULL);
648 put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
649 tag_nctrl = res->size;
650 put_byte(res, 0); /* Number of controls */
651 put_word(res, dlg->x);
652 put_word(res, dlg->y);
653 put_word(res, dlg->width);
654 put_word(res, dlg->height);
655 if(dlg->menu)
656 put_name_id(res, dlg->menu, TRUE, NULL);
657 else
658 put_byte(res, 0);
659 if(dlg->dlgclass)
660 put_name_id(res, dlg->dlgclass, TRUE, NULL);
661 else
662 put_byte(res, 0);
663 if(dlg->title)
664 put_string(res, dlg->title, str_char, TRUE, NULL);
665 else
666 put_byte(res, 0);
667 if(dlg->font)
669 put_word(res, dlg->font->size);
670 put_string(res, dlg->font->name, str_char, TRUE, NULL);
673 while(ctrl)
675 put_word(res, ctrl->x);
676 put_word(res, ctrl->y);
677 put_word(res, ctrl->width);
678 put_word(res, ctrl->height);
679 put_word(res, ctrl->id);
680 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
681 if(ctrl->ctlclass)
683 if(ctrl->ctlclass->type == name_ord
684 && ctrl->ctlclass->name.i_name >= 0x80
685 && ctrl->ctlclass->name.i_name <= 0x85)
686 put_byte(res, ctrl->ctlclass->name.i_name);
687 else if(ctrl->ctlclass->type == name_str)
688 put_name_id(res, ctrl->ctlclass, FALSE, NULL);
689 else
690 error("Unknown control-class %04x", ctrl->ctlclass->name.i_name);
692 else
693 internal_error(__FILE__, __LINE__, "Control has no control-class");
694 if(ctrl->title)
695 put_name_id(res, ctrl->title, FALSE, NULL);
696 else
697 put_byte(res, 0);
699 /* FIXME: What is this extra byte doing here? */
700 put_byte(res, 0);
702 nctrl++;
703 ctrl = ctrl->next;
705 /* Set number of controls */
706 ((char *)res->data)[tag_nctrl] = (char)nctrl;
708 /* Set ResourceSize */
709 SetResSize(res, restag);
710 return res;
714 *****************************************************************************
715 * Function : dialogex2res
716 * Syntax : res_t *dialogex2res(name_id_t *name, dialogex_t *dlgex)
717 * Input :
718 * name - Name/ordinal of the resource
719 * dlgex - The dialogex descriptor
720 * Output : New .res format structure
721 * Description :
722 * Remarks :
723 *****************************************************************************
725 static res_t *dialogex2res(name_id_t *name, dialogex_t *dlgex)
727 int restag;
728 res_t *res;
729 control_t *ctrl;
730 int tag_nctrl;
731 int nctrl = 0;
732 assert(name != NULL);
733 assert(dlgex != NULL);
735 ctrl = dlgex->controls;
736 res = new_res();
737 if(win32)
739 restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlgex->memopt, &(dlgex->lvc));
741 /* FIXME: MS doc says thet the first word must contain 0xffff
742 * and the second 0x0001 to signal a DLGTEMPLATEEX. Borland's
743 * compiler reverses the two words.
744 * I don't know which one to choose, but I write it as Mr. B
745 * writes it.
747 put_word(res, 1); /* Signature */
748 put_word(res, 0xffff); /* DlgVer */
749 put_dword(res, dlgex->gothelpid ? dlgex->helpid : 0);
750 put_dword(res, dlgex->gotexstyle ? dlgex->exstyle->or_mask : 0);
751 put_dword(res, dlgex->gotstyle ? dlgex->style->or_mask : WS_POPUPWINDOW);
752 tag_nctrl = res->size;
753 put_word(res, 0); /* Number of controls */
754 put_word(res, dlgex->x);
755 put_word(res, dlgex->y);
756 put_word(res, dlgex->width);
757 put_word(res, dlgex->height);
758 if(dlgex->menu)
759 put_name_id(res, dlgex->menu, TRUE, dlgex->lvc.language);
760 else
761 put_word(res, 0);
762 if(dlgex->dlgclass)
763 put_name_id(res, dlgex->dlgclass, TRUE, dlgex->lvc.language);
764 else
765 put_word(res, 0);
766 if(dlgex->title)
767 put_string(res, dlgex->title, str_unicode, TRUE, dlgex->lvc.language);
768 else
769 put_word(res, 0);
770 if(dlgex->font)
772 put_word(res, dlgex->font->size);
773 put_word(res, dlgex->font->weight);
774 /* FIXME: ? TRUE should be sufficient to say that its
775 * italic, but Borland's compiler says its 0x0101.
776 * I just copy it here, and hope for the best.
778 put_word(res, dlgex->font->italic ? 0x0101 : 0);
779 put_string(res, dlgex->font->name, str_unicode, TRUE, dlgex->lvc.language);
782 put_pad(res);
783 while(ctrl)
785 put_dword(res, ctrl->gothelpid ? ctrl->helpid : 0);
786 put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
787 /* FIXME: what is default control style? */
788 put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask : WS_CHILD | WS_VISIBLE);
789 put_word(res, ctrl->x);
790 put_word(res, ctrl->y);
791 put_word(res, ctrl->width);
792 put_word(res, ctrl->height);
793 put_word(res, ctrl->id);
794 /* FIXME: Pad is _NOT_ documented!?! */
795 put_pad(res);
796 if(ctrl->ctlclass)
797 put_name_id(res, ctrl->ctlclass, TRUE, dlgex->lvc.language);
798 else
799 internal_error(__FILE__, __LINE__, "Control has no control-class");
800 if(ctrl->title)
801 put_name_id(res, ctrl->title, FALSE, dlgex->lvc.language);
802 else
803 put_word(res, 0);
804 if(ctrl->extra)
806 put_pad(res);
807 put_word(res, ctrl->extra->size);
808 put_raw_data(res, ctrl->extra, 0);
810 else
811 put_word(res, 0);
813 put_pad(res);
814 nctrl++;
815 ctrl = ctrl->next;
817 /* Set number of controls */
818 set_word(res, tag_nctrl, (WORD)nctrl);
819 /* Set ResourceSize */
820 SetResSize(res, restag);
821 put_pad(res);
823 else /* win16 */
825 /* Do not generate anything in 16-bit mode */
826 free(res->data);
827 free(res);
828 return NULL;
830 return res;
834 *****************************************************************************
835 * Function : menuitem2res
836 * Syntax : void menuitem2res(res_t *res, menu_item_t *item)
837 * Input :
838 * Output :
839 * Description :
840 * Remarks : Self recursive
841 *****************************************************************************
843 static void menuitem2res(res_t *res, menu_item_t *menitem, const language_t *lang)
845 menu_item_t *itm = menitem;
846 if(win32)
848 while(itm)
850 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
851 if(!itm->popup)
852 put_word(res, itm->id);
853 if(itm->name)
854 put_string(res, itm->name, str_unicode, TRUE, lang);
855 else
856 put_word(res, 0);
857 if(itm->popup)
858 menuitem2res(res, itm->popup, lang);
859 itm = itm->next;
862 else /* win16 */
864 while(itm)
866 put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
867 if(!itm->popup)
868 put_word(res, itm->id);
869 if(itm->name)
870 put_string(res, itm->name, str_char, TRUE, lang);
871 else
872 put_byte(res, 0);
873 if(itm->popup)
874 menuitem2res(res, itm->popup, lang);
875 itm = itm->next;
882 *****************************************************************************
883 * Function : menu2res
884 * Syntax : res_t *menu2res(name_id_t *name, menu_t *men)
885 * Input :
886 * name - Name/ordinal of the resource
887 * men - The menu descriptor
888 * Output : New .res format structure
889 * Description :
890 * Remarks :
891 *****************************************************************************
893 static res_t *menu2res(name_id_t *name, menu_t *men)
895 int restag;
896 res_t *res;
897 assert(name != NULL);
898 assert(men != NULL);
900 res = new_res();
901 restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, win32 ? &(men->lvc) : NULL);
903 put_dword(res, 0); /* Menuheader: Version and HeaderSize */
904 menuitem2res(res, men->items, win32 ? men->lvc.language : NULL);
905 /* Set ResourceSize */
906 SetResSize(res, restag);
907 if(win32)
908 put_pad(res);
909 return res;
913 *****************************************************************************
914 * Function : menuexitem2res
915 * Syntax : void menuexitem2res(res_t *res, menuex_item_t *item)
916 * Input :
917 * Output : nop
918 * Description :
919 * Remarks : Self recursive
920 *****************************************************************************
922 static void menuexitem2res(res_t *res, menuex_item_t *menitem, const language_t *lang)
924 menuex_item_t *itm = menitem;
925 assert(win32 != 0);
926 while(itm)
928 put_dword(res, itm->gottype ? itm->type : 0);
929 put_dword(res, itm->gotstate ? itm->state : 0);
930 put_dword(res, itm->gotid ? itm->id : 0); /* FIXME: Docu. says word */
931 put_word(res, (itm->popup ? 0x01 : 0) | (!itm->next ? MF_END : 0));
932 if(itm->name)
933 put_string(res, itm->name, str_unicode, TRUE, lang);
934 else
935 put_word(res, 0);
936 put_pad(res);
937 if(itm->popup)
939 put_dword(res, itm->gothelpid ? itm->helpid : 0);
940 menuexitem2res(res, itm->popup, lang);
942 itm = itm->next;
948 *****************************************************************************
949 * Function : menuex2res
950 * Syntax : res_t *menuex2res(name_id_t *name, menuex_t *menex)
951 * Input :
952 * name - Name/ordinal of the resource
953 * menex - The menuex descriptor
954 * Output : New .res format structure
955 * Description :
956 * Remarks :
957 *****************************************************************************
959 static res_t *menuex2res(name_id_t *name, menuex_t *menex)
961 int restag;
962 res_t *res;
963 assert(name != NULL);
964 assert(menex != NULL);
966 res = new_res();
967 if(win32)
969 restag = put_res_header(res, WRC_RT_MENU, NULL, name, menex->memopt, &(menex->lvc));
971 put_word(res, 1); /* Menuheader: Version */
972 put_word(res, 4); /* Offset */
973 put_dword(res, 0); /* HelpId */
974 put_pad(res);
975 menuexitem2res(res, menex->items, menex->lvc.language);
976 /* Set ResourceSize */
977 SetResSize(res, restag);
978 put_pad(res);
980 else /* win16 */
982 /* Do not generate anything in 16-bit mode */
983 free(res->data);
984 free(res);
985 return NULL;
987 return res;
991 *****************************************************************************
992 * Function : cursorgroup2res
993 * Syntax : res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
994 * Input :
995 * name - Name/ordinal of the resource
996 * curg - The cursor descriptor
997 * Output : New .res format structure
998 * Description :
999 * Remarks :
1000 *****************************************************************************
1002 static res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
1004 int restag;
1005 res_t *res;
1006 cursor_t *cur;
1007 assert(name != NULL);
1008 assert(curg != NULL);
1010 res = new_res();
1011 restag = put_res_header(res, WRC_RT_GROUP_CURSOR, NULL, name, curg->memopt, &(curg->lvc));
1012 if(win32)
1014 put_word(res, 0); /* Reserved */
1015 /* FIXME: The ResType in the NEWHEADER structure should
1016 * contain 14 according to the MS win32 doc. This is
1017 * not the case with the BRC compiler and I really doubt
1018 * the latter. Putting one here is compliant to win16 spec,
1019 * but who knows the true value?
1021 put_word(res, 2); /* ResType */
1022 put_word(res, curg->ncursor);
1023 #if 0
1024 for(cur = curg->cursorlist; cur; cur = cur->next)
1025 #else
1026 cur = curg->cursorlist;
1027 while(cur->next)
1028 cur = cur->next;
1029 for(; cur; cur = cur->prev)
1030 #endif
1032 put_word(res, cur->width);
1033 /* FIXME: The height of a cursor is half the size of
1034 * the bitmap's height. BRC puts the height from the
1035 * BITMAPINFOHEADER here instead of the cursorfile's
1036 * height. MS doesn't seem to care...
1038 put_word(res, cur->height);
1039 /* FIXME: The next two are reversed in BRC and I don't
1040 * know why. Probably a bug. But, we can safely ignore
1041 * it because win16 does not support color cursors.
1042 * A warning should have been generated by the parser.
1044 put_word(res, cur->planes);
1045 put_word(res, cur->bits);
1046 /* FIXME: The +4 is the hotspot in the cursor resource.
1047 * However, I cound not find this in the documentation.
1048 * The hotspot bytes must either be included or MS
1049 * doesn't care.
1051 put_dword(res, cur->data->size +4);
1052 put_word(res, cur->id);
1055 else /* win16 */
1057 put_word(res, 0); /* Reserved */
1058 put_word(res, 2); /* ResType */
1059 put_word(res, curg->ncursor);
1060 #if 0
1061 for(cur = curg->cursorlist; cur; cur = cur->next)
1062 #else
1063 cur = curg->cursorlist;
1064 while(cur->next)
1065 cur = cur->next;
1066 for(; cur; cur = cur->prev)
1067 #endif
1069 put_word(res, cur->width);
1070 /* FIXME: The height of a cursor is half the size of
1071 * the bitmap's height. BRC puts the height from the
1072 * BITMAPINFOHEADER here instead of the cursorfile's
1073 * height. MS doesn't seem to care...
1075 put_word(res, cur->height);
1076 /* FIXME: The next two are reversed in BRC and I don't
1077 * know why. Probably a bug. But, we can safely ignore
1078 * it because win16 does not support color cursors.
1079 * A warning should have been generated by the parser.
1081 put_word(res, cur->planes);
1082 put_word(res, cur->bits);
1083 /* FIXME: The +4 is the hotspot in the cursor resource.
1084 * However, I cound not find this in the documentation.
1085 * The hotspot bytes must either be included or MS
1086 * doesn't care.
1088 put_dword(res, cur->data->size +4);
1089 put_word(res, cur->id);
1092 SetResSize(res, restag); /* Set ResourceSize */
1093 if(win32)
1094 put_pad(res);
1096 return res;
1100 *****************************************************************************
1101 * Function : cursor2res
1102 * Syntax : res_t *cursor2res(cursor_t *cur)
1103 * Input :
1104 * cur - The cursor descriptor
1105 * Output : New .res format structure
1106 * Description :
1107 * Remarks :
1108 *****************************************************************************
1110 static res_t *cursor2res(cursor_t *cur)
1112 int restag;
1113 res_t *res;
1114 name_id_t name;
1116 assert(cur != NULL);
1118 res = new_res();
1119 name.type = name_ord;
1120 name.name.i_name = cur->id;
1121 restag = put_res_header(res, WRC_RT_CURSOR, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(cur->lvc));
1122 put_word(res, cur->xhot);
1123 put_word(res, cur->yhot);
1124 put_raw_data(res, cur->data, 0);
1126 SetResSize(res, restag); /* Set ResourceSize */
1127 if(win32)
1128 put_pad(res);
1130 return res;
1134 *****************************************************************************
1135 * Function : icongroup2res
1136 * Syntax : res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1137 * Input :
1138 * name - Name/ordinal of the resource
1139 * icog - The icon group descriptor
1140 * Output : New .res format structure
1141 * Description :
1142 * Remarks :
1143 *****************************************************************************
1145 static res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
1147 int restag;
1148 res_t *res;
1149 icon_t *ico;
1150 assert(name != NULL);
1151 assert(icog != NULL);
1153 res = new_res();
1154 restag = put_res_header(res, WRC_RT_GROUP_ICON, NULL, name, icog->memopt, &(icog->lvc));
1155 if(win32)
1157 put_word(res, 0); /* Reserved */
1158 /* FIXME: The ResType in the NEWHEADER structure should
1159 * contain 14 according to the MS win32 doc. This is
1160 * not the case with the BRC compiler and I really doubt
1161 * the latter. Putting one here is compliant to win16 spec,
1162 * but who knows the true value?
1164 put_word(res, 1); /* ResType */
1165 put_word(res, icog->nicon);
1166 for(ico = icog->iconlist; ico; ico = ico->next)
1168 put_byte(res, ico->width);
1169 put_byte(res, ico->height);
1170 put_byte(res, ico->nclr);
1171 put_byte(res, 0); /* Reserved */
1172 put_word(res, ico->planes);
1173 put_word(res, ico->bits);
1174 put_dword(res, ico->data->size);
1175 put_word(res, ico->id);
1178 else /* win16 */
1180 put_word(res, 0); /* Reserved */
1181 put_word(res, 1); /* ResType */
1182 put_word(res, icog->nicon);
1183 for(ico = icog->iconlist; ico; ico = ico->next)
1185 put_byte(res, ico->width);
1186 put_byte(res, ico->height);
1187 put_byte(res, ico->nclr);
1188 put_byte(res, 0); /* Reserved */
1189 put_word(res, ico->planes);
1190 put_word(res, ico->bits);
1191 put_dword(res, ico->data->size);
1192 put_word(res, ico->id);
1195 SetResSize(res, restag); /* Set ResourceSize */
1196 if(win32)
1197 put_pad(res);
1199 return res;
1203 *****************************************************************************
1204 * Function : icon2res
1205 * Syntax : res_t *icon2res(icon_t *ico)
1206 * Input :
1207 * ico - The icon descriptor
1208 * Output : New .res format structure
1209 * Description :
1210 * Remarks :
1211 *****************************************************************************
1213 static res_t *icon2res(icon_t *ico)
1215 int restag;
1216 res_t *res;
1217 name_id_t name;
1219 assert(ico != NULL);
1221 res = new_res();
1222 name.type = name_ord;
1223 name.name.i_name = ico->id;
1224 restag = put_res_header(res, WRC_RT_ICON, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(ico->lvc));
1225 put_raw_data(res, ico->data, 0);
1227 SetResSize(res, restag); /* Set ResourceSize */
1228 if(win32)
1229 put_pad(res);
1231 return res;
1235 *****************************************************************************
1236 * Function : anicurico2res
1237 * Syntax : res_t *anicurico2res(name_id_t *name, ani_curico_t *ani)
1238 * Input :
1239 * name - Name/ordinal of the resource
1240 * ani - The animated object descriptor
1241 * Output : New .res format structure
1242 * Description :
1243 * Remarks : The endian of the object's structures have been converted
1244 * by the loader.
1245 * There are rumors that win311 could handle animated stuff.
1246 * That is why they are generated for both win16 and win32
1247 * compile.
1248 *****************************************************************************
1250 static res_t *anicurico2res(name_id_t *name, ani_curico_t *ani, enum res_e type)
1252 int restag;
1253 res_t *res;
1254 assert(name != NULL);
1255 assert(ani != NULL);
1257 res = new_res();
1258 restag = put_res_header(res, type == res_anicur ? WRC_RT_ANICURSOR : WRC_RT_ANIICON,
1259 NULL, name, ani->memopt, NULL);
1260 put_raw_data(res, ani->data, 0);
1261 /* Set ResourceSize */
1262 SetResSize(res, restag);
1263 if(win32)
1264 put_pad(res);
1265 return res;
1269 *****************************************************************************
1270 * Function : bitmap2res
1271 * Syntax : res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1272 * Input :
1273 * name - Name/ordinal of the resource
1274 * bmp - The bitmap descriptor
1275 * Output : New .res format structure
1276 * Description :
1277 * Remarks : The endian of the bitmap structures have been converted
1278 * by the loader.
1279 *****************************************************************************
1281 static res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
1283 int restag;
1284 res_t *res;
1285 assert(name != NULL);
1286 assert(bmp != NULL);
1288 res = new_res();
1289 restag = put_res_header(res, WRC_RT_BITMAP, NULL, name, bmp->memopt, &(bmp->data->lvc));
1290 if(bmp->data->data[0] == 'B'
1291 && bmp->data->data[1] == 'M'
1292 && ((BITMAPFILEHEADER *)bmp->data->data)->bfSize == bmp->data->size
1293 && bmp->data->size >= sizeof(BITMAPFILEHEADER))
1295 /* The File header is still attached, don't write it */
1296 put_raw_data(res, bmp->data, sizeof(BITMAPFILEHEADER));
1298 else
1300 put_raw_data(res, bmp->data, 0);
1302 /* Set ResourceSize */
1303 SetResSize(res, restag);
1304 if(win32)
1305 put_pad(res);
1306 return res;
1310 *****************************************************************************
1311 * Function : font2res
1312 * Syntax : res_t *font2res(name_id_t *name, font_t *fnt)
1313 * Input :
1314 * name - Name/ordinal of the resource
1315 * fnt - The font descriptor
1316 * Output : New .res format structure
1317 * Description :
1318 * Remarks : The data has been prepared just after parsing.
1319 *****************************************************************************
1321 static res_t *font2res(name_id_t *name, font_t *fnt)
1323 int restag;
1324 res_t *res;
1325 assert(name != NULL);
1326 assert(fnt != NULL);
1328 res = new_res();
1329 restag = put_res_header(res, WRC_RT_FONT, NULL, name, fnt->memopt, &(fnt->data->lvc));
1330 put_raw_data(res, fnt->data, 0);
1331 /* Set ResourceSize */
1332 SetResSize(res, restag);
1333 if(win32)
1334 put_pad(res);
1335 return res;
1339 *****************************************************************************
1340 * Function : fontdir2res
1341 * Syntax : res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1342 * Input :
1343 * name - Name/ordinal of the resource
1344 * fntdir - The fontdir descriptor
1345 * Output : New .res format structure
1346 * Description :
1347 * Remarks : The data has been prepared just after parsing.
1348 *****************************************************************************
1350 static res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
1352 int restag;
1353 res_t *res;
1354 assert(name != NULL);
1355 assert(fnd != NULL);
1357 res = new_res();
1358 restag = put_res_header(res, WRC_RT_FONTDIR, NULL, name, fnd->memopt, &(fnd->data->lvc));
1359 put_raw_data(res, fnd->data, 0);
1360 /* Set ResourceSize */
1361 SetResSize(res, restag);
1362 if(win32)
1363 put_pad(res);
1364 return res;
1368 *****************************************************************************
1369 * Function : rcdata2res
1370 * Syntax : res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1371 * Input :
1372 * name - Name/ordinal of the resource
1373 * rdt - The rcdata descriptor
1374 * Output : New .res format structure
1375 * Description :
1376 * Remarks :
1377 *****************************************************************************
1379 static res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
1381 int restag;
1382 res_t *res;
1383 assert(name != NULL);
1384 assert(rdt != NULL);
1386 res = new_res();
1387 restag = put_res_header(res, WRC_RT_RCDATA, NULL, name, rdt->memopt, &(rdt->data->lvc));
1388 put_raw_data(res, rdt->data, 0);
1389 /* Set ResourceSize */
1390 SetResSize(res, restag);
1391 if(win32)
1392 put_pad(res);
1393 return res;
1397 *****************************************************************************
1398 * Function : messagetable2res
1399 * Syntax : res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1400 * Input :
1401 * name - Name/ordinal of the resource
1402 * msg - The messagetable descriptor
1403 * Output : New .res format structure
1404 * Description :
1405 * Remarks : The data has been converted to the appropriate endian
1406 * after is was parsed.
1407 *****************************************************************************
1409 static res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
1411 int restag;
1412 res_t *res;
1413 assert(name != NULL);
1414 assert(msg != NULL);
1416 res = new_res();
1417 restag = put_res_header(res, WRC_RT_MESSAGETABLE, NULL, name, msg->memopt, &(msg->data->lvc));
1418 put_raw_data(res, msg->data, 0);
1419 /* Set ResourceSize */
1420 SetResSize(res, restag);
1421 if(win32)
1422 put_pad(res);
1423 return res;
1427 *****************************************************************************
1428 * Function : stringtable2res
1429 * Syntax : res_t *stringtable2res(stringtable_t *stt)
1430 * Input :
1431 * stt - The stringtable descriptor
1432 * Output : New .res format structure
1433 * Description :
1434 * Remarks :
1435 *****************************************************************************
1437 static res_t *stringtable2res(stringtable_t *stt)
1439 res_t *res;
1440 name_id_t name;
1441 int i;
1442 int restag;
1443 DWORD lastsize = 0;
1445 assert(stt != NULL);
1446 res = new_res();
1448 for(; stt; stt = stt->next)
1450 if(!stt->nentries)
1452 warning("Empty internal stringtable");
1453 continue;
1455 name.type = name_ord;
1456 name.name.i_name = (stt->idbase >> 4) + 1;
1457 restag = put_res_header(res, WRC_RT_STRING, NULL, &name, stt->memopt, win32 ? &(stt->lvc) : NULL);
1458 for(i = 0; i < stt->nentries; i++)
1460 if(stt->entries[i].str && stt->entries[i].str->size)
1462 put_string(res, stt->entries[i].str, win32 ? str_unicode : str_char,
1463 FALSE, win32 ? stt->lvc.language : NULL);
1465 else
1467 if (win32)
1468 put_word(res, 0);
1469 else
1470 put_byte(res, 0);
1473 /* Set ResourceSize */
1474 SetResSize(res, restag - lastsize);
1475 if(win32)
1476 put_pad(res);
1477 lastsize = res->size;
1479 return res;
1483 *****************************************************************************
1484 * Function : user2res
1485 * Syntax : res_t *user2res(name_id_t *name, user_t *usr)
1486 * Input :
1487 * name - Name/ordinal of the resource
1488 * usr - The userresource descriptor
1489 * Output : New .res format structure
1490 * Description :
1491 * Remarks :
1492 *****************************************************************************
1494 static res_t *user2res(name_id_t *name, user_t *usr)
1496 int restag;
1497 res_t *res;
1498 assert(name != NULL);
1499 assert(usr != NULL);
1501 res = new_res();
1502 restag = put_res_header(res, 0, usr->type, name, usr->memopt, &(usr->data->lvc));
1503 put_raw_data(res, usr->data, 0);
1504 /* Set ResourceSize */
1505 SetResSize(res, restag);
1506 if(win32)
1507 put_pad(res);
1508 return res;
1512 *****************************************************************************
1513 * Function : versionblock2res
1514 * Syntax : void versionblock2res(res_t *res, ver_block_t *blk)
1515 * Input :
1516 * res - Binary resource to write to
1517 * blk - The version block to be written
1518 * Output :
1519 * Description :
1520 * Remarks : Self recursive
1521 *****************************************************************************
1523 static void versionblock2res(res_t *res, ver_block_t *blk, int level, const language_t *lang)
1525 ver_value_t *val;
1526 int blksizetag;
1527 int valblksizetag;
1528 int valvalsizetag;
1529 int tag;
1530 int i;
1532 blksizetag = res->size;
1533 put_word(res, 0); /* Will be overwritten later */
1534 put_word(res, 0);
1535 if(win32)
1536 put_word(res, 0); /* level ? */
1537 put_string(res, blk->name, win32 ? str_unicode : str_char, TRUE, lang);
1538 put_pad(res);
1539 for(val = blk->values; val; val = val->next)
1541 if(val->type == val_str)
1543 valblksizetag = res->size;
1544 put_word(res, 0); /* Will be overwritten later */
1545 valvalsizetag = res->size;
1546 put_word(res, 0); /* Will be overwritten later */
1547 if(win32)
1549 put_word(res, level);
1551 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
1552 put_pad(res);
1553 tag = res->size;
1554 put_string(res, val->value.str, win32 ? str_unicode : str_char, TRUE, lang);
1555 if(win32)
1556 set_word(res, valvalsizetag, (WORD)((res->size - tag) >> 1));
1557 else
1558 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1559 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1560 put_pad(res);
1562 else if(val->type == val_words)
1564 valblksizetag = res->size;
1565 put_word(res, 0); /* Will be overwritten later */
1566 valvalsizetag = res->size;
1567 put_word(res, 0); /* Will be overwritten later */
1568 if(win32)
1570 put_word(res, level);
1572 put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
1573 put_pad(res);
1574 tag = res->size;
1575 for(i = 0; i < val->value.words->nwords; i++)
1577 put_word(res, val->value.words->words[i]);
1579 set_word(res, valvalsizetag, (WORD)(res->size - tag));
1580 set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
1581 put_pad(res);
1583 else if(val->type == val_block)
1585 versionblock2res(res, val->value.block, level+1, lang);
1587 else
1589 internal_error(__FILE__, __LINE__, "Invalid value indicator %d in VERSIONINFO", val->type);
1593 /* Set blocksize */
1594 set_word(res, blksizetag, (WORD)(res->size - blksizetag));
1598 *****************************************************************************
1599 * Function : versioninfo2res
1600 * Syntax : res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1601 * Input :
1602 * name - Name/ordinal of the resource
1603 * ver - The versioninfo descriptor
1604 * Output : New .res format structure
1605 * Description :
1606 * Remarks :
1607 *****************************************************************************
1609 static res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
1611 int restag;
1612 int rootblocksizetag;
1613 int valsizetag;
1614 int tag;
1615 res_t *res;
1616 string_t vsvi;
1617 ver_block_t *blk;
1619 assert(name != NULL);
1620 assert(ver != NULL);
1622 vsvi.type = str_char;
1623 vsvi.str.cstr = xstrdup("VS_VERSION_INFO");
1624 vsvi.size = 15; /* Excl. termination */
1626 res = new_res();
1627 restag = put_res_header(res, WRC_RT_VERSION, NULL, name, ver->memopt, &(ver->lvc));
1628 rootblocksizetag = res->size;
1629 put_word(res, 0); /* BlockSize filled in later */
1630 valsizetag = res->size;
1631 put_word(res, 0); /* ValueSize filled in later*/
1632 if(win32)
1633 put_word(res, 0); /* Tree-level ? */
1634 put_string(res, &vsvi, win32 ? str_unicode : str_char,
1635 TRUE, win32 ? ver->lvc.language : NULL);
1636 if(win32)
1637 put_pad(res);
1638 tag = res->size;
1639 put_dword(res, VS_FFI_SIGNATURE);
1640 put_dword(res, VS_FFI_STRUCVERSION);
1641 put_dword(res, (ver->filever_maj1 << 16) + (ver->filever_maj2 & 0xffff));
1642 put_dword(res, (ver->filever_min1 << 16) + (ver->filever_min2 & 0xffff));
1643 put_dword(res, (ver->prodver_maj1 << 16) + (ver->prodver_maj2 & 0xffff));
1644 put_dword(res, (ver->prodver_min1 << 16) + (ver->prodver_min2 & 0xffff));
1645 put_dword(res, ver->fileflagsmask);
1646 put_dword(res, ver->fileflags);
1647 put_dword(res, ver->fileos);
1648 put_dword(res, ver->filetype);
1649 put_dword(res, ver->filesubtype);
1650 put_dword(res, 0); /* FileDateMS */
1651 put_dword(res, 0); /* FileDateLS */
1652 /* Set ValueSize */
1653 set_word(res, valsizetag, (WORD)(res->size - tag));
1654 /* Descend into the blocks */
1655 for(blk = ver->blocks; blk; blk = blk->next)
1656 versionblock2res(res, blk, 0, win32 ? ver->lvc.language : NULL);
1657 /* Set root block's size */
1658 set_word(res, rootblocksizetag, (WORD)(res->size - rootblocksizetag));
1660 SetResSize(res, restag);
1661 if(win32)
1662 put_pad(res);
1664 free(vsvi.str.cstr);
1665 return res;
1669 *****************************************************************************
1670 * Function : toolbaritem2res
1671 * Syntax : void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1672 * Input :
1673 * Output : nop
1674 * Description :
1675 * Remarks : Self recursive
1676 *****************************************************************************
1678 static void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1680 toolbar_item_t *itm = tbitem;
1681 assert(win32 != 0);
1682 while(itm)
1684 put_word(res, itm->id);
1685 itm = itm->next;
1691 *****************************************************************************
1692 * Function : toolbar2res
1693 * Syntax : res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1694 * Input :
1695 * name - Name/ordinal of the resource
1696 * toolbar - The toolbar descriptor
1697 * Output : New .res format structure
1698 * Description :
1699 * Remarks :
1700 *****************************************************************************
1702 static res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1704 int restag;
1705 res_t *res;
1706 assert(name != NULL);
1707 assert(toolbar != NULL);
1709 res = new_res();
1710 if(win32)
1712 restag = put_res_header(res, WRC_RT_TOOLBAR, NULL, name, toolbar->memopt, &(toolbar->lvc));
1714 put_word(res, 1); /* Menuheader: Version */
1715 put_word(res, toolbar->button_width); /* (in pixels?) */
1716 put_word(res, toolbar->button_height); /* (in pixels?) */
1717 put_word(res, toolbar->nitems);
1718 put_pad(res);
1719 toolbaritem2res(res, toolbar->items);
1720 /* Set ResourceSize */
1721 SetResSize(res, restag);
1722 put_pad(res);
1724 else /* win16 */
1726 /* Do not generate anything in 16-bit mode */
1727 free(res->data);
1728 free(res);
1729 return NULL;
1731 return res;
1735 *****************************************************************************
1736 * Function : dlginit2res
1737 * Syntax : res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1738 * Input :
1739 * name - Name/ordinal of the resource
1740 * rdt - The dlginit descriptor
1741 * Output : New .res format structure
1742 * Description :
1743 * Remarks :
1744 *****************************************************************************
1746 static res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1748 int restag;
1749 res_t *res;
1750 assert(name != NULL);
1751 assert(dit != NULL);
1753 res = new_res();
1754 restag = put_res_header(res, WRC_RT_DLGINIT, NULL, name, dit->memopt, &(dit->data->lvc));
1755 put_raw_data(res, dit->data, 0);
1756 /* Set ResourceSize */
1757 SetResSize(res, restag);
1758 if(win32)
1759 put_pad(res);
1760 return res;
1764 *****************************************************************************
1765 * Function : prep_nid_for_label
1766 * Syntax : char *prep_nid_for_label(const name_id_t *nid)
1767 * Input :
1768 * Output :
1769 * Description : Converts a resource name into the first 32 (or less)
1770 * characters of the name with conversions.
1771 * Remarks :
1772 *****************************************************************************
1774 #define MAXNAMELEN 32
1775 char *prep_nid_for_label(const name_id_t *nid)
1777 static char buf[MAXNAMELEN+1];
1779 assert(nid != NULL);
1781 if(nid->type == name_str && nid->name.s_name->type == str_unicode)
1783 short *sptr;
1784 int i;
1785 sptr = nid->name.s_name->str.wstr;
1786 buf[0] = '\0';
1787 for(i = 0; *sptr && i < MAXNAMELEN; i++)
1789 if((unsigned)*sptr < 0x80 && isprint(*sptr & 0xff))
1790 buf[i] = *sptr++;
1791 else
1792 warning("Resourcename (str_unicode) contain unprintable characters or invalid translation, ignored");
1794 buf[i] = '\0';
1796 else if(nid->type == name_str && nid->name.s_name->type == str_char)
1798 char *cptr;
1799 int i;
1800 cptr = nid->name.s_name->str.cstr;
1801 buf[0] = '\0';
1802 for(i = 0; *cptr && i < MAXNAMELEN; i++)
1804 if((unsigned)*cptr < 0x80 && isprint(*cptr & 0xff))
1805 buf[i] = *cptr++;
1806 else
1807 warning("Resourcename (str_char) contain unprintable characters, ignored");
1809 buf[i] = '\0';
1811 else if(nid->type == name_ord)
1813 sprintf(buf, "%u", nid->name.i_name);
1815 else
1817 internal_error(__FILE__, __LINE__, "Resource name_id with invalid type %d", nid->type);
1819 return buf;
1821 #undef MAXNAMELEN
1824 *****************************************************************************
1825 * Function : make_c_name
1826 * Syntax : char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
1827 * Input :
1828 * Output :
1829 * Description : Converts a resource name into a valid c-identifier in the
1830 * form "_base_nid".
1831 * Remarks :
1832 *****************************************************************************
1834 char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
1836 int nlen;
1837 char *buf;
1838 char *ret;
1839 char lanbuf[6];
1841 sprintf(lanbuf, "%d", lan ? MAKELANGID(lan->id, lan->sub) : 0);
1842 buf = prep_nid_for_label(nid);
1843 nlen = strlen(buf) + strlen(lanbuf);
1844 nlen += strlen(base) + 4; /* three time '_' and '\0' */
1845 ret = (char *)xmalloc(nlen);
1846 strcpy(ret, "_");
1847 strcat(ret, base);
1848 strcat(ret, "_");
1849 strcat(ret, buf);
1850 strcat(ret, "_");
1851 strcat(ret, lanbuf);
1852 return ret;
1856 *****************************************************************************
1857 * Function : get_c_typename
1858 * Syntax : const char *get_c_typename(enum res_e type)
1859 * Input :
1860 * Output :
1861 * Description : Convert resource enum to char string to be used in c-name
1862 * creation.
1863 * Remarks :
1864 *****************************************************************************
1866 const char *get_c_typename(enum res_e type)
1868 switch(type)
1870 case res_acc: return "Acc";
1871 case res_anicur:return "AniCur";
1872 case res_aniico:return "AniIco";
1873 case res_bmp: return "Bmp";
1874 case res_cur: return "Cur";
1875 case res_curg: return "CurGrp";
1876 case res_dlg:
1877 case res_dlgex: return "Dlg";
1878 case res_fnt: return "Fnt";
1879 case res_fntdir:return "FntDir";
1880 case res_ico: return "Ico";
1881 case res_icog: return "IcoGrp";
1882 case res_men:
1883 case res_menex: return "Men";
1884 case res_rdt: return "RCDat";
1885 case res_stt: return "StrTab";
1886 case res_usr: return "Usr";
1887 case res_msg: return "MsgTab";
1888 case res_ver: return "VerInf";
1889 case res_toolbar: return "TlBr";
1890 case res_dlginit: return "DlgInit";
1891 default: return "Oops";
1896 *****************************************************************************
1897 * Function : resources2res
1898 * Syntax : void resources2res(resource_t *top)
1899 * Input :
1900 * top - The resource-tree to convert
1901 * Output :
1902 * Description : Convert logical resource descriptors into binary data
1903 * Remarks :
1904 *****************************************************************************
1906 void resources2res(resource_t *top)
1908 while(top)
1910 switch(top->type)
1912 case res_acc:
1913 if(!top->binres)
1914 top->binres = accelerator2res(top->name, top->res.acc);
1915 break;
1916 case res_bmp:
1917 if(!top->binres)
1918 top->binres = bitmap2res(top->name, top->res.bmp);
1919 break;
1920 case res_cur:
1921 if(!top->binres)
1922 top->binres = cursor2res(top->res.cur);
1923 break;
1924 case res_curg:
1925 if(!top->binres)
1926 top->binres = cursorgroup2res(top->name, top->res.curg);
1927 break;
1928 case res_dlg:
1929 if(!top->binres)
1930 top->binres = dialog2res(top->name, top->res.dlg);
1931 break;
1932 case res_dlgex:
1933 if(!top->binres)
1934 top->binres = dialogex2res(top->name, top->res.dlgex);
1935 break;
1936 case res_fnt:
1937 if(!top->binres)
1938 top->binres = font2res(top->name, top->res.fnt);
1939 break;
1940 case res_fntdir:
1941 if(!top->binres)
1942 top->binres = fontdir2res(top->name, top->res.fnd);
1943 break;
1944 case res_ico:
1945 if(!top->binres)
1946 top->binres = icon2res(top->res.ico);
1947 break;
1948 case res_icog:
1949 if(!top->binres)
1950 top->binres = icongroup2res(top->name, top->res.icog);
1951 break;
1952 case res_men:
1953 if(!top->binres)
1954 top->binres = menu2res(top->name, top->res.men);
1955 break;
1956 case res_menex:
1957 if(!top->binres)
1958 top->binres = menuex2res(top->name, top->res.menex);
1959 break;
1960 case res_rdt:
1961 if(!top->binres)
1962 top->binres = rcdata2res(top->name, top->res.rdt);
1963 break;
1964 case res_stt:
1965 if(!top->binres)
1966 top->binres = stringtable2res(top->res.stt);
1967 break;
1968 case res_usr:
1969 if(!top->binres)
1970 top->binres = user2res(top->name, top->res.usr);
1971 break;
1972 case res_msg:
1973 if(!top->binres)
1974 top->binres = messagetable2res(top->name, top->res.msg);
1975 break;
1976 case res_ver:
1977 if(!top->binres)
1978 top->binres = versioninfo2res(top->name, top->res.ver);
1979 break;
1980 case res_toolbar:
1981 if(!top->binres)
1982 top->binres = toolbar2res(top->name, top->res.tbt);
1983 break;
1984 case res_dlginit:
1985 if(!top->binres)
1986 top->binres = dlginit2res(top->name, top->res.dlgi);
1987 break;
1988 case res_anicur:
1989 case res_aniico:
1990 if(!top->binres)
1991 top->binres = anicurico2res(top->name, top->res.ani, top->type);
1992 break;
1993 default:
1994 internal_error(__FILE__, __LINE__, "Unknown resource type encountered %d in binary res generation", top->type);
1996 top->c_name = make_c_name(get_c_typename(top->type), top->name, top->lan);
1997 top = top->next;