genres.c 51.3 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4 5
/*
 * Generate .res format from a resource-tree
 *
 * Copyright 1998 Bertho A. Stultiens
 *
6 7 8 9 10 11 12 13 14 15 16 17
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
19 20
 *
 * History:
21 22 23
 * 05-May-2000 BS	- Added code to support endian conversions. The
 * 			  extra functions also aid unaligned access, but
 * 			  this is not yet implemented.
Alexandre Julliard's avatar
Alexandre Julliard committed
24
 * 25-May-1998 BS	- Added simple unicode -> char conversion for resource
25
 *			  names in .s and .h files.
Alexandre Julliard's avatar
Alexandre Julliard committed
26 27
 */

28 29
#include "config.h"

Alexandre Julliard's avatar
Alexandre Julliard committed
30 31 32
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
33
#include <stdarg.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
34 35 36 37 38 39
#include <assert.h>
#include <ctype.h>

#include "wrc.h"
#include "genres.h"
#include "utils.h"
40
#include "windef.h"
41
#include "winbase.h"
42
#include "wingdi.h"
43
#include "winuser.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
44

45
#define SetResSize(res, tag)	set_dword((res), (tag), (res)->size - get_dword((res), (tag)))
Alexandre Julliard's avatar
Alexandre Julliard committed
46 47 48 49

res_t *new_res(void)
{
	res_t *r;
50
	r = xmalloc(sizeof(res_t));
Alexandre Julliard's avatar
Alexandre Julliard committed
51 52
	r->allocsize = RES_BLOCKSIZE;
	r->size = 0;
53
	r->dataidx = 0;
54
	r->data = xmalloc(RES_BLOCKSIZE);
Alexandre Julliard's avatar
Alexandre Julliard committed
55 56 57
	return r;
}

58
res_t *grow_res(res_t *r, unsigned int add)
Alexandre Julliard's avatar
Alexandre Julliard committed
59 60
{
	r->allocsize += add;
61
	r->data = xrealloc(r->data, r->allocsize);
Alexandre Julliard's avatar
Alexandre Julliard committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	return r;
}

/*
 *****************************************************************************
 * Function	: put_byte
 *		  put_word
 *		  put_dword
 * Syntax	: void put_byte(res_t *res, unsigned c)
 *		  void put_word(res_t *res, unsigned w)
 *		  void put_dword(res_t *res, unsigned d)
 * Input	:
 *	res	- Binary resource to put the data in
 *	c, w, d	- Data to put
 * Output	: nop
 * Description	: Put primitives that put an item in the binary resource.
 *		  The data array grows automatically.
 * Remarks	:
 *****************************************************************************
*/
void put_byte(res_t *res, unsigned c)
{
	if(res->allocsize - res->size < sizeof(char))
		grow_res(res, RES_BLOCKSIZE);
86
	res->data[res->size] = (char)c;
Alexandre Julliard's avatar
Alexandre Julliard committed
87 88 89 90 91 92 93
	res->size += sizeof(char);
}

void put_word(res_t *res, unsigned w)
{
	if(res->allocsize - res->size < sizeof(WORD))
		grow_res(res, RES_BLOCKSIZE);
94 95
	switch(byteorder)
	{
96 97
#ifdef WORDS_BIGENDIAN
	default:
98
#endif
99 100 101
	case WRC_BO_BIG:
		res->data[res->size+0] = HIBYTE(w);
		res->data[res->size+1] = LOBYTE(w);
102
		break;
103 104

#ifndef WORDS_BIGENDIAN
105
	default:
106 107 108 109
#endif
	case WRC_BO_LITTLE:
		res->data[res->size+1] = HIBYTE(w);
		res->data[res->size+0] = LOBYTE(w);
110 111
		break;
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
112 113 114 115 116 117 118
	res->size += sizeof(WORD);
}

void put_dword(res_t *res, unsigned d)
{
	if(res->allocsize - res->size < sizeof(DWORD))
		grow_res(res, RES_BLOCKSIZE);
119 120
	switch(byteorder)
	{
121 122
#ifdef WORDS_BIGENDIAN
	default:
123
#endif
124 125 126 127 128
	case WRC_BO_BIG:
		res->data[res->size+0] = HIBYTE(HIWORD(d));
		res->data[res->size+1] = LOBYTE(HIWORD(d));
		res->data[res->size+2] = HIBYTE(LOWORD(d));
		res->data[res->size+3] = LOBYTE(LOWORD(d));
129
		break;
130 131

#ifndef WORDS_BIGENDIAN
132
	default:
133 134 135 136 137 138
#endif
	case WRC_BO_LITTLE:
		res->data[res->size+3] = HIBYTE(HIWORD(d));
		res->data[res->size+2] = LOBYTE(HIWORD(d));
		res->data[res->size+1] = HIBYTE(LOWORD(d));
		res->data[res->size+0] = LOBYTE(LOWORD(d));
139 140
		break;
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
141 142 143
	res->size += sizeof(DWORD);
}

144
static void put_pad(res_t *res)
Alexandre Julliard's avatar
Alexandre Julliard committed
145 146 147 148 149
{
	while(res->size & 0x3)
		put_byte(res, 0);
}

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
/*
 *****************************************************************************
 * Function	: set_word
 *		  set_dword
 * Syntax	: void set_word(res_t *res, int ofs, unsigned w)
 *		  void set_dword(res_t *res, int ofs, unsigned d)
 * Input	:
 *	res	- Binary resource to put the data in
 *	ofs	- Byte offset in data-array
 *	w, d	- Data to put
 * Output	: nop
 * Description	: Set the value of a binary resource data array to a
 * 		  specific value.
 * Remarks	:
 *****************************************************************************
*/
166
static void set_word(res_t *res, int ofs, unsigned w)
167 168 169
{
	switch(byteorder)
	{
170 171
#ifdef WORDS_BIGENDIAN
	default:
172
#endif
173 174 175
	case WRC_BO_BIG:
		res->data[ofs+0] = HIBYTE(w);
		res->data[ofs+1] = LOBYTE(w);
176
		break;
177 178

#ifndef WORDS_BIGENDIAN
179
	default:
180 181 182 183
#endif
	case WRC_BO_LITTLE:
		res->data[ofs+1] = HIBYTE(w);
		res->data[ofs+0] = LOBYTE(w);
184 185 186 187
		break;
	}
}

188
static void set_dword(res_t *res, int ofs, unsigned d)
189 190 191
{
	switch(byteorder)
	{
192 193
#ifdef WORDS_BIGENDIAN
	default:
194
#endif
195 196 197 198 199
	case WRC_BO_BIG:
		res->data[ofs+0] = HIBYTE(HIWORD(d));
		res->data[ofs+1] = LOBYTE(HIWORD(d));
		res->data[ofs+2] = HIBYTE(LOWORD(d));
		res->data[ofs+3] = LOBYTE(LOWORD(d));
200
		break;
201 202

#ifndef WORDS_BIGENDIAN
203
	default:
204 205 206 207 208 209
#endif
	case WRC_BO_LITTLE:
		res->data[ofs+3] = HIBYTE(HIWORD(d));
		res->data[ofs+2] = LOBYTE(HIWORD(d));
		res->data[ofs+1] = HIBYTE(LOWORD(d));
		res->data[ofs+0] = LOBYTE(LOWORD(d));
210 211 212 213 214 215
		break;
	}
}

/*
 *****************************************************************************
216
 * Function	: get_dword
217 218 219 220 221 222 223 224 225
 * Input	:
 *	res	- Binary resource to put the data in
 *	ofs	- Byte offset in data-array
 * Output	: The data in native endian
 * Description	: Get the value of a binary resource data array in native
 * 		  endian.
 * Remarks	:
 *****************************************************************************
*/
226
static DWORD get_dword(res_t *res, int ofs)
227 228 229
{
	switch(byteorder)
	{
230 231
#ifdef WORDS_BIGENDIAN
	default:
232
#endif
233 234 235 236 237 238 239
	case WRC_BO_BIG:
		return   (res->data[ofs+0] << 24)
		       | (res->data[ofs+1] << 16)
		       | (res->data[ofs+2] <<  8)
		       |  res->data[ofs+3];

#ifndef WORDS_BIGENDIAN
240
	default:
241 242 243 244 245 246
#endif
	case WRC_BO_LITTLE:
		return   (res->data[ofs+3] << 24)
		       | (res->data[ofs+2] << 16)
		       | (res->data[ofs+1] <<  8)
		       |  res->data[ofs+0];
247 248 249
	}
}

Alexandre Julliard's avatar
Alexandre Julliard committed
250 251 252 253 254 255 256 257 258 259
/*
 *****************************************************************************
 * Function	: string_to_upper
 * Syntax	: void string_to_upper(string_t *str)
 * Input	:
 * Output	:
 * Description	:
 * Remarks	: FIXME: codepages...
 *****************************************************************************
*/
260
static void string_to_upper(string_t *str)
Alexandre Julliard's avatar
Alexandre Julliard committed
261
{
262 263 264 265
    int i;

    if(str->type == str_char)
    {
266 267
        for (i = 0; i < str->size; i++)
            if (str->str.cstr[i] >= 'a' && str->str.cstr[i] <= 'z') str->str.cstr[i] -= 32;
268 269 270
    }
    else if(str->type == str_unicode)
    {
271 272
        for (i = 0; i < str->size; i++)
            if (str->str.wstr[i] >= 'a' && str->str.wstr[i] <= 'z') str->str.wstr[i] -= 32;
273 274 275
    }
    else
    {
276
        internal_error(__FILE__, __LINE__, "Invalid string type %d\n", str->type);
277
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
278 279
}

280 281 282 283 284 285
static int parse_accel_string( const string_t *key, int flags )
{
    int keycode;

    if(key->type == str_char)
    {
286
        if (key->str.cstr[0] == '#') return 0;  /* ignore message contexts */
287 288 289 290 291
	if((flags & WRC_AF_VIRTKEY) &&
           !((key->str.cstr[0] >= 'A' && key->str.cstr[0] <= 'Z') ||
             (key->str.cstr[0] >= '0' && key->str.cstr[0] <= '9')))
        {
            print_location( &key->loc );
292
            error("VIRTKEY code is not equal to ascii value\n");
293 294 295 296 297
        }

	if(key->str.cstr[0] == '^' && (flags & WRC_AF_CONTROL) != 0)
	{
            print_location( &key->loc );
298
            error("Cannot use both '^' and CONTROL modifier\n");
299 300 301
	}
	else if(key->str.cstr[0] == '^')
	{
302 303 304 305 306
            if (key->str.cstr[1] >= 'a' && key->str.cstr[1] <= 'z')
                keycode = key->str.cstr[1] - 'a' + 1;
            else if (key->str.cstr[1] >= 'A' && key->str.cstr[1] <= 'Z')
                keycode = key->str.cstr[1] - 'A' + 1;
            else
307 308
            {
                print_location( &key->loc );
309
                error("Control-code out of range\n");
310 311 312 313 314 315 316
            }
	}
	else
            keycode = key->str.cstr[0];
    }
    else
    {
317
        if (key->str.wstr[0] == '#') return 0;  /* ignore message contexts */
318 319 320 321 322
	if((flags & WRC_AF_VIRTKEY) &&
           !((key->str.wstr[0] >= 'A' && key->str.wstr[0] <= 'Z') ||
             (key->str.wstr[0] >= '0' && key->str.wstr[0] <= '9')))
        {
            print_location( &key->loc );
323
            error("VIRTKEY code is not equal to ascii value\n");
324 325 326 327
        }
	if(key->str.wstr[0] == '^' && (flags & WRC_AF_CONTROL) != 0)
	{
            print_location( &key->loc );
328
            error("Cannot use both '^' and CONTROL modifier\n");
329 330 331
	}
	else if(key->str.wstr[0] == '^')
	{
332 333 334 335 336
            if (key->str.wstr[1] >= 'a' && key->str.wstr[1] <= 'z')
                keycode = key->str.wstr[1] - 'a' + 1;
            else if (key->str.wstr[1] >= 'A' && key->str.wstr[1] <= 'Z')
                keycode = key->str.wstr[1] - 'A' + 1;
            else
337 338
            {
                print_location( &key->loc );
339
                error("Control-code out of range\n");
340 341 342 343 344 345 346 347
            }
	}
	else
            keycode = key->str.wstr[0];
    }
    return keycode;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
348 349 350 351
/*
 *****************************************************************************
 * Function	: put_string
 * Syntax	: void put_string(res_t *res, string_t *str, enum str_e type,
352
 *				  int isterm, const language_t *lang)
Alexandre Julliard's avatar
Alexandre Julliard committed
353 354 355 356 357 358 359 360 361 362 363
 * Input	:
 *	res	- Binary resource to put the data in
 *	str	- String to put
 *	type	- Data has to be written in either str_char or str_unicode
 *	isterm	- The string is '\0' terminated (disregard the string's
 *		  size member)
 * Output	: nop
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
364 365
static void put_string(res_t *res, const string_t *str, enum str_e type, int isterm,
                       const language_t *lang)
Alexandre Julliard's avatar
Alexandre Julliard committed
366
{
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
    int cnt, codepage;
    string_t *newstr;

    assert(res != NULL);
    assert(str != NULL);

    if (lang) codepage = get_language_codepage( lang->id, lang->sub );
    else codepage = get_language_codepage( 0, 0 );

    assert( codepage != -1 );

    newstr = convert_string(str, type, codepage);
    if (type == str_unicode)
    {
        if (str->type == str_char)
        {
            if (!check_unicode_conversion( str, newstr, codepage ))
384 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 );
388
            }
389
            if (check_valid_utf8( str, codepage ))
390 391
            {
                print_location( &str->loc );
392 393
                warning( "string \"%s\" seems to be UTF-8 but codepage %u is in use.\n",
                         str->str.cstr, codepage );
394
            }
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
        }
        if (!isterm) put_word(res, newstr->size);
        for(cnt = 0; cnt < newstr->size; cnt++)
        {
            WCHAR c = newstr->str.wstr[cnt];
            if (isterm && !c) break;
            put_word(res, c);
        }
        if (isterm) put_word(res, 0);
    }
    else  /* str_char */
    {
        if (!isterm) put_byte(res, newstr->size);
        for(cnt = 0; cnt < newstr->size; cnt++)
        {
            char c = newstr->str.cstr[cnt];
            if (isterm && !c) break;
            put_byte(res, c);
        }
        if (isterm) put_byte(res, 0);
    }
    free_string(newstr);
Alexandre Julliard's avatar
Alexandre Julliard committed
417 418 419 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)
Alexandre Julliard's avatar
Alexandre Julliard committed
423 424 425 426 427 428
 * Input	:
 * Output	:
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
429
static void put_name_id(res_t *res, name_id_t *nid, int upcase, const language_t *lang)
Alexandre Julliard's avatar
Alexandre Julliard committed
430 431 432 433 434 435 436 437 438 439 440 441 442
{
	if(nid->type == name_ord)
	{
		if(win32)
			put_word(res, 0xffff);
		else
			put_byte(res, 0xff);
		put_word(res, (WORD)nid->name.i_name);
	}
	else if(nid->type == name_str)
	{
		if(upcase)
			string_to_upper(nid->name.s_name);
443
		put_string(res, nid->name.s_name, win32 ? str_unicode : str_char, TRUE, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
444 445 446
	}
	else
	{
447
		internal_error(__FILE__, __LINE__, "Invalid name_id type %d\n", nid->type);
Alexandre Julliard's avatar
Alexandre Julliard committed
448 449 450 451 452 453 454 455 456 457 458 459 460
	}
}

/*
 *****************************************************************************
 * Function	: put_lvc
 * Syntax	: void put_lvc(res_t *res, lvc_t *lvc)
 * Input	:
 * Output	:
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
461
static void put_lvc(res_t *res, lvc_t *lvc)
Alexandre Julliard's avatar
Alexandre Julliard committed
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
{
	if(lvc && lvc->language)
		put_word(res, MAKELANGID(lvc->language->id, lvc->language->sub));
	else
		put_word(res, 0);	/* Neutral */
	if(lvc && lvc->version)
		put_dword(res, *(lvc->version));
	else
		put_dword(res, 0);
	if(lvc && lvc->characts)
		put_dword(res, *(lvc->characts));
	else
		put_dword(res, 0);
}

/*
 *****************************************************************************
 * Function	: put_raw_data
 * Syntax	: void put_raw_data(res_t *res, raw_data_t *raw, int offset)
 * Input	:
 * Output	:
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
487
static void put_raw_data(res_t *res, raw_data_t *raw, int offset)
Alexandre Julliard's avatar
Alexandre Julliard committed
488
{
489
	unsigned int wsize = raw->size - offset;
Alexandre Julliard's avatar
Alexandre Julliard committed
490 491 492 493 494 495 496 497 498
	if(res->allocsize - res->size < wsize)
		grow_res(res, wsize);
	memcpy(&(res->data[res->size]), raw->data + offset, wsize);
	res->size += wsize;
}

/*
 *****************************************************************************
 * Function	: put_res_header
499
 * Syntax	: input_res_header(res_t *res, int type, name_id_t *ntype,
Alexandre Julliard's avatar
Alexandre Julliard committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
 *				    name_id_t *name, DWORD memopt, lvc_t *lvc)
 *
 * Input	:
 *	res	- Binary resource descriptor to write to
 *	type	- Resource identifier (if ntype == NULL)
 *	ntype	- Name id of type
 *	name	- Resource's name
 *	memopt	- Resource's memory options to write
 *	lvc	- Language, version and characteristics (win32 only)
 * Output	: An index to the resource size field. The resource size field
 *		  contains the header size upon exit.
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
515 516
static int put_res_header(res_t *res, int type, name_id_t *ntype, name_id_t *name,
                          DWORD memopt, lvc_t *lvc)
Alexandre Julliard's avatar
Alexandre Julliard committed
517 518 519 520 521 522 523 524 525 526 527
{
	if(win32)
	{
		put_dword(res, 0);		/* We will overwrite these later */
		put_dword(res, 0);
		if(!ntype)
		{
			put_word(res, 0xffff);		/* ResType */
			put_word(res, type);
		}
		else
528 529
			put_name_id(res, ntype, TRUE, lvc->language);
		put_name_id(res, name, TRUE, lvc->language); /* ResName */
Alexandre Julliard's avatar
Alexandre Julliard committed
530 531 532 533
		put_pad(res);
		put_dword(res, 0);		/* DataVersion */
		put_word(res, memopt);		/* Memory options */
		put_lvc(res, lvc);		/* Language, version and characts */
534 535
		set_dword(res, 0*sizeof(DWORD), res->size);	/* Set preliminary resource */
		set_dword(res, 1*sizeof(DWORD), res->size);	/* Set HeaderSize */
Alexandre Julliard's avatar
Alexandre Julliard committed
536 537 538 539 540 541 542 543 544 545 546 547
		res->dataidx = res->size;
		return 0;
	}
	else /* win16 */
	{
		int tag;
		if(!ntype)
		{
			put_byte(res, 0xff);		/* ResType */
			put_word(res, type);
		}
		else
548 549
			put_name_id(res, ntype, TRUE, NULL);
		put_name_id(res, name, TRUE, NULL); /* ResName */
Alexandre Julliard's avatar
Alexandre Julliard committed
550 551 552
		put_word(res, memopt);		/* Memory options */
		tag = res->size;
		put_dword(res, 0);		/* ResSize overwritten later*/
553
		set_dword(res, tag, res->size);
Alexandre Julliard's avatar
Alexandre Julliard committed
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
		res->dataidx = res->size;
		return tag;
	}
}

/*
 *****************************************************************************
 * Function	: accelerator2res
 * Syntax	: res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
 * Input	:
 *	name	- Name/ordinal of the resource
 *	acc	- The accelerator descriptor
 * Output	: New .res format structure
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
571
static res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
Alexandre Julliard's avatar
Alexandre Julliard committed
572 573 574 575 576 577 578 579 580 581 582 583 584 585
{
	int restag;
	res_t *res;
	event_t *ev;
	assert(name != NULL);
	assert(acc != NULL);

	ev = acc->events;
	res = new_res();
	if(win32)
	{
		restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, &(acc->lvc));
		while(ev)
		{
586 587
			int key = ev->key;
			if (ev->str) key = parse_accel_string( ev->str, ev->flags );
Alexandre Julliard's avatar
Alexandre Julliard committed
588
			put_word(res, ev->flags | (ev->next ? 0 : 0x80));
589
			put_word(res, key);
Alexandre Julliard's avatar
Alexandre Julliard committed
590 591 592 593 594 595 596 597 598 599 600
			put_word(res, ev->id);
			put_word(res, 0);	/* Padding */
			ev = ev->next;
		}
		put_pad(res);
	}
	else /* win16 */
	{
		restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, NULL);
		while(ev)
		{
601 602
			int key = ev->key;
			if (ev->str) key = parse_accel_string( ev->str, ev->flags );
Alexandre Julliard's avatar
Alexandre Julliard committed
603
			put_byte(res, ev->flags | (ev->next ? 0 : 0x80));
604
			put_word(res, key);
Alexandre Julliard's avatar
Alexandre Julliard committed
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
			put_word(res, ev->id);
			ev = ev->next;
		}
	}
	/* Set ResourceSize */
	SetResSize(res, restag);
	return res;
}

/*
 *****************************************************************************
 * Function	: dialog2res
 * Syntax	: res_t *dialog2res(name_id_t *name, dialog_t *dlg)
 * Input	:
 *	name	- Name/ordinal of the resource
 *	dlg	- The dialog descriptor
 * Output	: New .res format structure
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
626
static res_t *dialog2res(name_id_t *name, dialog_t *dlg)
Alexandre Julliard's avatar
Alexandre Julliard committed
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
{
	int restag;
	res_t *res;
	control_t *ctrl;
	int tag_nctrl;
	int nctrl = 0;
	assert(name != NULL);
	assert(dlg != NULL);

	ctrl = dlg->controls;
	res = new_res();
	if(win32)
	{
		restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, &(dlg->lvc));

642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
		if (dlg->is_ex)
		{
			/* FIXME: MS doc says that the first word must contain 0xffff
			 * and the second 0x0001 to signal a DLGTEMPLATEEX. Borland's
			 * compiler reverses the two words.
			 * I don't know which one to choose, but I write it as Mr. B
			 * writes it.
			 */
			put_word(res, 1);		/* Signature */
			put_word(res, 0xffff);		/* DlgVer */
			put_dword(res, dlg->gothelpid ? dlg->helpid : 0);
			put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
			put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
		}
		else
		{
			put_dword(res, dlg->style->or_mask);
			put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
		}
Alexandre Julliard's avatar
Alexandre Julliard committed
661 662 663 664 665 666 667
		tag_nctrl = res->size;
		put_word(res, 0);		/* Number of controls */
		put_word(res, dlg->x);
		put_word(res, dlg->y);
		put_word(res, dlg->width);
		put_word(res, dlg->height);
		if(dlg->menu)
668
			put_name_id(res, dlg->menu, FALSE, dlg->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
669 670 671
		else
			put_word(res, 0);
		if(dlg->dlgclass)
672
			put_name_id(res, dlg->dlgclass, FALSE, dlg->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
673 674 675
		else
			put_word(res, 0);
		if(dlg->title)
676
			put_string(res, dlg->title, str_unicode, TRUE, dlg->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
677 678 679 680 681
		else
			put_word(res, 0);
		if(dlg->font)
		{
			put_word(res, dlg->font->size);
682 683 684 685 686 687 688 689 690
			if (dlg->is_ex)
			{
				put_word(res, dlg->font->weight);
				/* FIXME: ? TRUE should be sufficient to say that it's
				 * italic, but Borland's compiler says it's 0x0101.
				 * I just copy it here, and hope for the best.
				 */
				put_word(res, dlg->font->italic ? 0x0101 : 0);
			}
691
			put_string(res, dlg->font->name, str_unicode, TRUE, dlg->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
692
		}
693
                else if (dlg->style->or_mask & DS_SETFONT) put_word( res, 0x7fff );
Alexandre Julliard's avatar
Alexandre Julliard committed
694 695 696 697

		put_pad(res);
		while(ctrl)
		{
698 699 700 701 702 703 704 705 706 707 708 709 710
			if (dlg->is_ex)
			{
				put_dword(res, ctrl->gothelpid ? ctrl->helpid : 0);
				put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
				/* FIXME: what is default control style? */
				put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask : WS_CHILD | WS_VISIBLE);
			}
			else
			{
				/* FIXME: what is default control style? */
				put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
				put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
			}
Alexandre Julliard's avatar
Alexandre Julliard committed
711 712 713 714
			put_word(res, ctrl->x);
			put_word(res, ctrl->y);
			put_word(res, ctrl->width);
			put_word(res, ctrl->height);
715 716 717 718
			if (dlg->is_ex)
				put_dword(res, ctrl->id);
			else
				put_word(res, ctrl->id);
Alexandre Julliard's avatar
Alexandre Julliard committed
719
			if(ctrl->ctlclass)
720
				put_name_id(res, ctrl->ctlclass, FALSE, dlg->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
721
			else
722
				internal_error(__FILE__, __LINE__, "Control has no control-class\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
723
			if(ctrl->title)
724
				put_name_id(res, ctrl->title, FALSE, dlg->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
725 726 727 728
			else
				put_word(res, 0);
			if(ctrl->extra)
			{
729
				put_word(res, ctrl->extra->size);
Alexandre Julliard's avatar
Alexandre Julliard committed
730 731 732 733 734 735 736 737 738 739 740
				put_raw_data(res, ctrl->extra, 0);
			}
			else
				put_word(res, 0);

			if(ctrl->next)
				put_pad(res);
			nctrl++;
			ctrl = ctrl->next;
		}
		/* Set number of controls */
741
		set_word(res, tag_nctrl, (WORD)nctrl);
Alexandre Julliard's avatar
Alexandre Julliard committed
742 743 744 745 746
	}
	else /* win16 */
	{
		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);
Alexandre Julliard's avatar
Alexandre Julliard committed
748 749 750 751 752 753 754
		tag_nctrl = res->size;
		put_byte(res, 0);		/* Number of controls */
		put_word(res, dlg->x);
		put_word(res, dlg->y);
		put_word(res, dlg->width);
		put_word(res, dlg->height);
		if(dlg->menu)
755
			put_name_id(res, dlg->menu, TRUE, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
756 757 758
		else
			put_byte(res, 0);
		if(dlg->dlgclass)
759
			put_name_id(res, dlg->dlgclass, TRUE, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
760 761 762
		else
			put_byte(res, 0);
		if(dlg->title)
763
			put_string(res, dlg->title, str_char, TRUE, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
764 765 766 767 768
		else
			put_byte(res, 0);
		if(dlg->font)
		{
			put_word(res, dlg->font->size);
769
			put_string(res, dlg->font->name, str_char, TRUE, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
770 771 772 773 774 775 776 777 778
		}

		while(ctrl)
		{
			put_word(res, ctrl->x);
			put_word(res, ctrl->y);
			put_word(res, ctrl->width);
			put_word(res, ctrl->height);
			put_word(res, ctrl->id);
779
			put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
Alexandre Julliard's avatar
Alexandre Julliard committed
780 781 782 783 784 785 786
			if(ctrl->ctlclass)
			{
				if(ctrl->ctlclass->type == name_ord
				&& ctrl->ctlclass->name.i_name >= 0x80
				&& ctrl->ctlclass->name.i_name <= 0x85)
					put_byte(res, ctrl->ctlclass->name.i_name);
				else if(ctrl->ctlclass->type == name_str)
787
					put_name_id(res, ctrl->ctlclass, FALSE, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
788
				else
789
					error("Unknown control-class %04x\n", ctrl->ctlclass->name.i_name);
Alexandre Julliard's avatar
Alexandre Julliard committed
790 791
			}
			else
792
				internal_error(__FILE__, __LINE__, "Control has no control-class\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
793
			if(ctrl->title)
794
				put_name_id(res, ctrl->title, FALSE, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
795 796 797 798 799
			else
				put_byte(res, 0);

			/* FIXME: What is this extra byte doing here? */
			put_byte(res, 0);
800

Alexandre Julliard's avatar
Alexandre Julliard committed
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
			nctrl++;
			ctrl = ctrl->next;
		}
		/* Set number of controls */
		((char *)res->data)[tag_nctrl] = (char)nctrl;
	}
	/* Set ResourceSize */
	SetResSize(res, restag);
	return res;
}

/*
 *****************************************************************************
 * Function	: menuitem2res
 * Syntax	: void menuitem2res(res_t *res, menu_item_t *item)
 * Input	:
 * Output	:
 * Description	:
 * Remarks	: Self recursive
 *****************************************************************************
*/
822
static void menuitem2res(res_t *res, menu_item_t *menitem, const language_t *lang)
Alexandre Julliard's avatar
Alexandre Julliard committed
823 824 825 826 827 828 829 830 831 832
{
	menu_item_t *itm = menitem;
	if(win32)
	{
		while(itm)
		{
			put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
			if(!itm->popup)
				put_word(res, itm->id);
			if(itm->name)
833
				put_string(res, itm->name, str_unicode, TRUE, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
834 835 836
			else
				put_word(res, 0);
			if(itm->popup)
837
				menuitem2res(res, itm->popup, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
838 839 840 841 842 843 844 845 846 847 848
			itm = itm->next;
		}
	}
	else /* win16 */
	{
		while(itm)
		{
			put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
			if(!itm->popup)
				put_word(res, itm->id);
			if(itm->name)
849
				put_string(res, itm->name, str_char, TRUE, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
850 851 852
			else
				put_byte(res, 0);
			if(itm->popup)
853
				menuitem2res(res, itm->popup, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
			itm = itm->next;
		}
	}

}

/*
 *****************************************************************************
 * Function	: menuexitem2res
 * Syntax	: void menuexitem2res(res_t *res, menuex_item_t *item)
 * Input	:
 * Output	: nop
 * Description	:
 * Remarks	: Self recursive
 *****************************************************************************
*/
870
static void menuexitem2res(res_t *res, menu_item_t *menitem, const language_t *lang)
Alexandre Julliard's avatar
Alexandre Julliard committed
871
{
872
	menu_item_t *itm = menitem;
Alexandre Julliard's avatar
Alexandre Julliard committed
873 874 875 876 877
	assert(win32 != 0);
	while(itm)
	{
		put_dword(res, itm->gottype ? itm->type : 0);
		put_dword(res, itm->gotstate ? itm->state : 0);
878
		put_dword(res, itm->gotid ? itm->id : 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
879 880
		put_word(res, (itm->popup ? 0x01 : 0) | (!itm->next ? MF_END : 0));
		if(itm->name)
881
			put_string(res, itm->name, str_unicode, TRUE, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
882 883 884 885 886 887
		else
			put_word(res, 0);
		put_pad(res);
		if(itm->popup)
		{
			put_dword(res, itm->gothelpid ? itm->helpid : 0);
888
			menuexitem2res(res, itm->popup, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
889 890 891 892 893 894 895 896
		}
		itm = itm->next;
	}

}

/*
 *****************************************************************************
897 898
 * Function	: menu2res
 * Syntax	: res_t *menu2res(name_id_t *name, menu_t *men)
Alexandre Julliard's avatar
Alexandre Julliard committed
899 900 901 902 903 904 905 906
 * Input	:
 *	name	- Name/ordinal of the resource
 *	menex	- The menuex descriptor
 * Output	: New .res format structure
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
907
static res_t *menu2res(name_id_t *name, menu_t *men)
Alexandre Julliard's avatar
Alexandre Julliard committed
908 909 910 911
{
	int restag;
	res_t *res;
	assert(name != NULL);
912
	assert(men != NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
913 914 915 916

	res = new_res();
	if(win32)
	{
917
		restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, &(men->lvc));
Alexandre Julliard's avatar
Alexandre Julliard committed
918

919 920 921 922 923 924 925 926 927 928 929 930 931
		if (men->is_ex)
		{
			put_word(res, 1);		/* Menuheader: Version */
			put_word(res, 4);		/* Offset */
			put_dword(res, 0);		/* HelpId */
			put_pad(res);
			menuexitem2res(res, men->items, men->lvc.language);
		}
		else
		{
			put_dword(res, 0);		/* Menuheader: Version and HeaderSize */
			menuitem2res(res, men->items, men->lvc.language);
		}
Alexandre Julliard's avatar
Alexandre Julliard committed
932 933 934 935 936 937
		/* Set ResourceSize */
		SetResSize(res, restag);
		put_pad(res);
	}
	else /* win16 */
	{
938 939 940 941 942 943
		restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, NULL);

		put_dword(res, 0);		/* Menuheader: Version and HeaderSize */
		menuitem2res(res, men->items, NULL);
		/* Set ResourceSize */
		SetResSize(res, restag);
Alexandre Julliard's avatar
Alexandre Julliard committed
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
	}
	return res;
}

/*
 *****************************************************************************
 * Function	: cursorgroup2res
 * Syntax	: res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
 * Input	:
 *	name	- Name/ordinal of the resource
 *	curg	- The cursor descriptor
 * Output	: New .res format structure
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
960
static res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
Alexandre Julliard's avatar
Alexandre Julliard committed
961 962 963 964 965 966 967 968
{
	int restag;
	res_t *res;
	cursor_t *cur;
	assert(name != NULL);
	assert(curg != NULL);

	res = new_res();
Alexandre Julliard's avatar
Alexandre Julliard committed
969
	restag = put_res_header(res, WRC_RT_GROUP_CURSOR, NULL, name, curg->memopt, &(curg->lvc));
970 971 972 973 974 975 976 977 978 979

    put_word(res, 0);	/* Reserved */
    /* FIXME: The ResType in the NEWHEADER structure should
     * contain 14 according to the MS win32 doc. This is
     * not the case with the BRC compiler and I really doubt
     * the latter. Putting one here is compliant to win16 spec,
     * but who knows the true value?
     */
    put_word(res, 2);	/* ResType */
    put_word(res, curg->ncursor);
Alexandre Julliard's avatar
Alexandre Julliard committed
980
#if 0
981
    for(cur = curg->cursorlist; cur; cur = cur->next)
Alexandre Julliard's avatar
Alexandre Julliard committed
982
#else
983 984 985 986
    cur = curg->cursorlist;
    while(cur->next)
        cur = cur->next;
    for(; cur; cur = cur->prev)
Alexandre Julliard's avatar
Alexandre Julliard committed
987
#endif
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
    {
        put_word(res, cur->width);
        /* FIXME: The height of a cursor is half the size of
         * the bitmap's height. BRC puts the height from the
         * BITMAPINFOHEADER here instead of the cursorfile's
         * height. MS doesn't seem to care...
         */
        put_word(res, cur->height);
        /* FIXME: The next two are reversed in BRC and I don't
         * know why. Probably a bug. But, we can safely ignore
         * it because win16 does not support color cursors.
         * A warning should have been generated by the parser.
         */
        put_word(res, cur->planes);
        put_word(res, cur->bits);
        /* FIXME: The +4 is the hotspot in the cursor resource.
         * However, I could not find this in the documentation.
         * The hotspot bytes must either be included or MS
         * doesn't care.
         */
        put_dword(res, cur->data->size +4);
        put_word(res, cur->id);
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
	SetResSize(res, restag);	/* Set ResourceSize */
	if(win32)
		put_pad(res);

	return res;
}

/*
 *****************************************************************************
 * Function	: cursor2res
 * Syntax	: res_t *cursor2res(cursor_t *cur)
 * Input	:
 *	cur	- The cursor descriptor
 * Output	: New .res format structure
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
1030
static res_t *cursor2res(cursor_t *cur)
Alexandre Julliard's avatar
Alexandre Julliard committed
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
{
	int restag;
	res_t *res;
	name_id_t name;

	assert(cur != NULL);

	res = new_res();
	name.type = name_ord;
	name.name.i_name = cur->id;
Alexandre Julliard's avatar
Alexandre Julliard committed
1041
	restag = put_res_header(res, WRC_RT_CURSOR, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(cur->lvc));
Alexandre Julliard's avatar
Alexandre Julliard committed
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
	put_word(res, cur->xhot);
	put_word(res, cur->yhot);
	put_raw_data(res, cur->data, 0);

	SetResSize(res, restag);	/* Set ResourceSize */
	if(win32)
		put_pad(res);

	return res;
}

/*
 *****************************************************************************
 * Function	: icongroup2res
 * Syntax	: res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
 * Input	:
 *	name	- Name/ordinal of the resource
 *	icog	- The icon group descriptor
 * Output	: New .res format structure
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
1065
static res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
Alexandre Julliard's avatar
Alexandre Julliard committed
1066 1067 1068 1069 1070 1071 1072 1073
{
	int restag;
	res_t *res;
	icon_t *ico;
	assert(name != NULL);
	assert(icog != NULL);

	res = new_res();
Alexandre Julliard's avatar
Alexandre Julliard committed
1074
	restag = put_res_header(res, WRC_RT_GROUP_ICON, NULL, name, icog->memopt, &(icog->lvc));
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096

    put_word(res, 0);	/* Reserved */
    /* FIXME: The ResType in the NEWHEADER structure should
     * contain 14 according to the MS win32 doc. This is
     * not the case with the BRC compiler and I really doubt
     * the latter. Putting one here is compliant to win16 spec,
     * but who knows the true value?
     */
    put_word(res, 1);	/* ResType */
    put_word(res, icog->nicon);
    for(ico = icog->iconlist; ico; ico = ico->next)
    {
        put_byte(res, ico->width);
        put_byte(res, ico->height);
        put_byte(res, ico->nclr);
        put_byte(res, 0);	/* Reserved */
        put_word(res, ico->planes);
        put_word(res, ico->bits);
        put_dword(res, ico->data->size);
        put_word(res, ico->id);
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
	SetResSize(res, restag);	/* Set ResourceSize */
	if(win32)
		put_pad(res);

	return res;
}

/*
 *****************************************************************************
 * Function	: icon2res
 * Syntax	: res_t *icon2res(icon_t *ico)
 * Input	:
 *	ico	- The icon descriptor
 * Output	: New .res format structure
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
1115
static res_t *icon2res(icon_t *ico)
Alexandre Julliard's avatar
Alexandre Julliard committed
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
{
	int restag;
	res_t *res;
	name_id_t name;

	assert(ico != NULL);

	res = new_res();
	name.type = name_ord;
	name.name.i_name = ico->id;
Alexandre Julliard's avatar
Alexandre Julliard committed
1126
	restag = put_res_header(res, WRC_RT_ICON, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(ico->lvc));
Alexandre Julliard's avatar
Alexandre Julliard committed
1127 1128 1129 1130 1131 1132 1133 1134 1135
	put_raw_data(res, ico->data, 0);

	SetResSize(res, restag);	/* Set ResourceSize */
	if(win32)
		put_pad(res);

	return res;
}

1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
/*
 *****************************************************************************
 * Function	: anicurico2res
 * Syntax	: res_t *anicurico2res(name_id_t *name, ani_curico_t *ani)
 * Input	:
 *	name	- Name/ordinal of the resource
 *	ani	- The animated object descriptor
 * Output	: New .res format structure
 * Description	:
 * Remarks	: The endian of the object's structures have been converted
 * 		  by the loader.
 * 		  There are rumors that win311 could handle animated stuff.
 * 		  That is why they are generated for both win16 and win32
 * 		  compile.
 *****************************************************************************
*/
static res_t *anicurico2res(name_id_t *name, ani_curico_t *ani, enum res_e type)
{
	int restag;
	res_t *res;
	assert(name != NULL);
	assert(ani != NULL);

	res = new_res();
	restag = put_res_header(res, type == res_anicur ? WRC_RT_ANICURSOR : WRC_RT_ANIICON,
				NULL, name, ani->memopt, NULL);
	put_raw_data(res, ani->data, 0);
	/* Set ResourceSize */
	SetResSize(res, restag);
	if(win32)
		put_pad(res);
	return res;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1170 1171 1172 1173 1174 1175 1176 1177 1178
/*
 *****************************************************************************
 * Function	: bitmap2res
 * Syntax	: res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
 * Input	:
 *	name	- Name/ordinal of the resource
 *	bmp	- The bitmap descriptor
 * Output	: New .res format structure
 * Description	:
1179
 * Remarks	: The endian of the bitmap structures have been converted
1180
 * 		  by the loader.
Alexandre Julliard's avatar
Alexandre Julliard committed
1181 1182
 *****************************************************************************
*/
1183
static res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
Alexandre Julliard's avatar
Alexandre Julliard committed
1184 1185 1186 1187 1188 1189 1190
{
	int restag;
	res_t *res;
	assert(name != NULL);
	assert(bmp != NULL);

	res = new_res();
1191
	restag = put_res_header(res, WRC_RT_BITMAP, NULL, name, bmp->memopt, &(bmp->data->lvc));
Alexandre Julliard's avatar
Alexandre Julliard committed
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
	if(bmp->data->data[0] == 'B'
	&& bmp->data->data[1] == 'M'
	&& ((BITMAPFILEHEADER *)bmp->data->data)->bfSize == bmp->data->size
	&& bmp->data->size >= sizeof(BITMAPFILEHEADER))
	{
		/* The File header is still attached, don't write it */
		put_raw_data(res, bmp->data, sizeof(BITMAPFILEHEADER));
	}
	else
	{
		put_raw_data(res, bmp->data, 0);
	}
	/* Set ResourceSize */
	SetResSize(res, restag);
	if(win32)
		put_pad(res);
	return res;
}

/*
 *****************************************************************************
 * Function	: font2res
 * Syntax	: res_t *font2res(name_id_t *name, font_t *fnt)
 * Input	:
 *	name	- Name/ordinal of the resource
 *	fnt	- The font descriptor
 * Output	: New .res format structure
 * Description	:
1220
 * Remarks	: The data has been prepared just after parsing.
Alexandre Julliard's avatar
Alexandre Julliard committed
1221 1222
 *****************************************************************************
*/
1223
static res_t *font2res(name_id_t *name, font_t *fnt)
Alexandre Julliard's avatar
Alexandre Julliard committed
1224
{
1225 1226
	int restag;
	res_t *res;
Alexandre Julliard's avatar
Alexandre Julliard committed
1227 1228
	assert(name != NULL);
	assert(fnt != NULL);
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266

	res = new_res();
	restag = put_res_header(res, WRC_RT_FONT, NULL, name, fnt->memopt, &(fnt->data->lvc));
	put_raw_data(res, fnt->data, 0);
	/* Set ResourceSize */
	SetResSize(res, restag);
	if(win32)
		put_pad(res);
	return res;
}

/*
 *****************************************************************************
 * Function	: fontdir2res
 * Syntax	: res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
 * Input	:
 *	name	- Name/ordinal of the resource
 *	fntdir	- The fontdir descriptor
 * Output	: New .res format structure
 * Description	:
 * Remarks	: The data has been prepared just after parsing.
 *****************************************************************************
*/
static res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
{
	int restag;
	res_t *res;
	assert(name != NULL);
	assert(fnd != NULL);

	res = new_res();
	restag = put_res_header(res, WRC_RT_FONTDIR, NULL, name, fnd->memopt, &(fnd->data->lvc));
	put_raw_data(res, fnd->data, 0);
	/* Set ResourceSize */
	SetResSize(res, restag);
	if(win32)
		put_pad(res);
	return res;
Alexandre Julliard's avatar
Alexandre Julliard committed
1267 1268
}

1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
/*
 *****************************************************************************
 * Function	: html2res
 * Syntax	: res_t *html2res(name_id_t *name, html_t *html)
 * Input	:
 *	name	- Name/ordinal of the resource
 *	rdt	- The html descriptor
 * Output	: New .res format structure
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
static res_t *html2res(name_id_t *name, html_t *html)
{
	int restag;
	res_t *res;
	assert(name != NULL);
	assert(html != NULL);

	res = new_res();
	restag = put_res_header(res, WRC_RT_HTML, NULL, name, html->memopt, &(html->data->lvc));
	put_raw_data(res, html->data, 0);
	/* Set ResourceSize */
	SetResSize(res, restag);
	if(win32)
		put_pad(res);
	return res;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
/*
 *****************************************************************************
 * Function	: rcdata2res
 * Syntax	: res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
 * Input	:
 *	name	- Name/ordinal of the resource
 *	rdt	- The rcdata descriptor
 * Output	: New .res format structure
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
1310
static res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
Alexandre Julliard's avatar
Alexandre Julliard committed
1311 1312 1313 1314 1315 1316 1317
{
	int restag;
	res_t *res;
	assert(name != NULL);
	assert(rdt != NULL);

	res = new_res();
1318
	restag = put_res_header(res, WRC_RT_RCDATA, NULL, name, rdt->memopt, &(rdt->data->lvc));
Alexandre Julliard's avatar
Alexandre Julliard committed
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
	put_raw_data(res, rdt->data, 0);
	/* Set ResourceSize */
	SetResSize(res, restag);
	if(win32)
		put_pad(res);
	return res;
}

/*
 *****************************************************************************
 * Function	: messagetable2res
 * Syntax	: res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
 * Input	:
 *	name	- Name/ordinal of the resource
 *	msg	- The messagetable descriptor
 * Output	: New .res format structure
 * Description	:
1336
 * Remarks	: The data has been converted to the appropriate endian
1337
 *		  after it was parsed.
Alexandre Julliard's avatar
Alexandre Julliard committed
1338 1339
 *****************************************************************************
*/
1340
static res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
Alexandre Julliard's avatar
Alexandre Julliard committed
1341
{
1342 1343
	int restag;
	res_t *res;
Alexandre Julliard's avatar
Alexandre Julliard committed
1344 1345
	assert(name != NULL);
	assert(msg != NULL);
1346 1347 1348 1349 1350 1351 1352 1353 1354

	res = new_res();
	restag = put_res_header(res, WRC_RT_MESSAGETABLE, NULL, name, msg->memopt, &(msg->data->lvc));
	put_raw_data(res, msg->data, 0);
	/* Set ResourceSize */
	SetResSize(res, restag);
	if(win32)
		put_pad(res);
	return res;
Alexandre Julliard's avatar
Alexandre Julliard committed
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
}

/*
 *****************************************************************************
 * Function	: stringtable2res
 * Syntax	: res_t *stringtable2res(stringtable_t *stt)
 * Input	:
 *	stt	- The stringtable descriptor
 * Output	: New .res format structure
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
1368
static res_t *stringtable2res(stringtable_t *stt)
Alexandre Julliard's avatar
Alexandre Julliard committed
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
{
	res_t *res;
	name_id_t name;
	int i;
	int restag;
	DWORD lastsize = 0;

	assert(stt != NULL);
	res = new_res();

	for(; stt; stt = stt->next)
	{
		if(!stt->nentries)
		{
1383
			warning("Empty internal stringtable\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
1384 1385 1386 1387 1388 1389 1390
			continue;
		}
		name.type = name_ord;
		name.name.i_name = (stt->idbase >> 4) + 1;
		restag = put_res_header(res, WRC_RT_STRING, NULL, &name, stt->memopt, win32 ? &(stt->lvc) : NULL);
		for(i = 0; i < stt->nentries; i++)
		{
1391
			if(stt->entries[i].str && stt->entries[i].str->size)
Alexandre Julliard's avatar
Alexandre Julliard committed
1392
			{
1393 1394
				put_string(res, stt->entries[i].str, win32 ? str_unicode : str_char,
                                           FALSE, win32 ? stt->lvc.language : NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
1395 1396 1397
			}
			else
			{
1398
				if (win32)
Alexandre Julliard's avatar
Alexandre Julliard committed
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
					put_word(res, 0);
				else
					put_byte(res, 0);
			}
		}
		/* Set ResourceSize */
		SetResSize(res, restag - lastsize);
		if(win32)
			put_pad(res);
		lastsize = res->size;
	}
	return res;
}

/*
 *****************************************************************************
 * Function	: user2res
 * Syntax	: res_t *user2res(name_id_t *name, user_t *usr)
 * Input	:
 *	name	- Name/ordinal of the resource
 *	usr	- The userresource descriptor
 * Output	: New .res format structure
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
1425
static res_t *user2res(name_id_t *name, user_t *usr)
Alexandre Julliard's avatar
Alexandre Julliard committed
1426 1427 1428 1429 1430 1431 1432
{
	int restag;
	res_t *res;
	assert(name != NULL);
	assert(usr != NULL);

	res = new_res();
1433
	restag = put_res_header(res, 0, usr->type, name, usr->memopt, &(usr->data->lvc));
Alexandre Julliard's avatar
Alexandre Julliard committed
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
	put_raw_data(res, usr->data, 0);
	/* Set ResourceSize */
	SetResSize(res, restag);
	if(win32)
		put_pad(res);
	return res;
}

/*
 *****************************************************************************
 * Function	: versionblock2res
 * Syntax	: void versionblock2res(res_t *res, ver_block_t *blk)
 * Input	:
 *	res	- Binary resource to write to
 *	blk	- The version block to be written
 * Output	:
 * Description	:
 * Remarks	: Self recursive
 *****************************************************************************
*/
1454
static void versionblock2res(res_t *res, ver_block_t *blk, int level, const language_t *lang)
Alexandre Julliard's avatar
Alexandre Julliard committed
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
{
	ver_value_t *val;
	int blksizetag;
	int valblksizetag;
	int valvalsizetag;
	int tag;
	int i;

	blksizetag = res->size;
	put_word(res, 0);	/* Will be overwritten later */
	put_word(res, 0);
	if(win32)
		put_word(res, 0);	/* level ? */
1468
	put_string(res, blk->name, win32 ? str_unicode : str_char, TRUE, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
	put_pad(res);
	for(val = blk->values; val; val = val->next)
	{
		if(val->type == val_str)
		{
			valblksizetag = res->size;
			put_word(res, 0);	/* Will be overwritten later */
			valvalsizetag = res->size;
			put_word(res, 0);	/* Will be overwritten later */
			if(win32)
			{
				put_word(res, level);
			}
1482
			put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
1483 1484
			put_pad(res);
			tag = res->size;
1485
			put_string(res, val->value.str, win32 ? str_unicode : str_char, TRUE, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
1486
			if(win32)
1487
				set_word(res, valvalsizetag, (WORD)((res->size - tag) >> 1));
Alexandre Julliard's avatar
Alexandre Julliard committed
1488
			else
1489 1490
				set_word(res, valvalsizetag, (WORD)(res->size - tag));
			set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
Alexandre Julliard's avatar
Alexandre Julliard committed
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
			put_pad(res);
		}
		else if(val->type == val_words)
		{
			valblksizetag = res->size;
			put_word(res, 0);	/* Will be overwritten later */
			valvalsizetag = res->size;
			put_word(res, 0);	/* Will be overwritten later */
			if(win32)
			{
				put_word(res, level);
			}
1503
			put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
1504 1505 1506 1507 1508 1509
			put_pad(res);
			tag = res->size;
			for(i = 0; i < val->value.words->nwords; i++)
			{
				put_word(res, val->value.words->words[i]);
			}
1510 1511
			set_word(res, valvalsizetag, (WORD)(res->size - tag));
			set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
Alexandre Julliard's avatar
Alexandre Julliard committed
1512 1513 1514 1515
			put_pad(res);
		}
		else if(val->type == val_block)
		{
1516
			versionblock2res(res, val->value.block, level+1, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
1517 1518 1519
		}
		else
		{
1520
			internal_error(__FILE__, __LINE__, "Invalid value indicator %d in VERSIONINFO\n", val->type);
Alexandre Julliard's avatar
Alexandre Julliard committed
1521 1522 1523 1524
		}
	}

	/* Set blocksize */
1525
	set_word(res, blksizetag, (WORD)(res->size - blksizetag));
Alexandre Julliard's avatar
Alexandre Julliard committed
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
}

/*
 *****************************************************************************
 * Function	: versioninfo2res
 * Syntax	: res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
 * Input	:
 *	name	- Name/ordinal of the resource
 *	ver	- The versioninfo descriptor
 * Output	: New .res format structure
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
1540
static res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
Alexandre Julliard's avatar
Alexandre Julliard committed
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
{
	int restag;
	int rootblocksizetag;
	int valsizetag;
	int tag;
	res_t *res;
	string_t vsvi;
	ver_block_t *blk;

	assert(name != NULL);
	assert(ver != NULL);

	vsvi.type = str_char;
1554
	vsvi.str.cstr = xstrdup("VS_VERSION_INFO");
Alexandre Julliard's avatar
Alexandre Julliard committed
1555 1556 1557
	vsvi.size = 15; /* Excl. termination */

	res = new_res();
1558
	restag = put_res_header(res, WRC_RT_VERSION, NULL, name, ver->memopt, &(ver->lvc));
Alexandre Julliard's avatar
Alexandre Julliard committed
1559 1560 1561 1562 1563 1564
	rootblocksizetag = res->size;
	put_word(res, 0);	/* BlockSize filled in later */
	valsizetag = res->size;
	put_word(res, 0);	/* ValueSize filled in later*/
	if(win32)
		put_word(res, 0);	/* Tree-level ? */
1565
	put_string(res, &vsvi, win32 ? str_unicode : str_char, TRUE, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
	if(win32)
		put_pad(res);
	tag = res->size;
	put_dword(res, VS_FFI_SIGNATURE);
	put_dword(res, VS_FFI_STRUCVERSION);
	put_dword(res, (ver->filever_maj1 << 16) + (ver->filever_maj2 & 0xffff));
	put_dword(res, (ver->filever_min1 << 16) + (ver->filever_min2 & 0xffff));
	put_dword(res, (ver->prodver_maj1 << 16) + (ver->prodver_maj2 & 0xffff));
	put_dword(res, (ver->prodver_min1 << 16) + (ver->prodver_min2 & 0xffff));
	put_dword(res, ver->fileflagsmask);
	put_dword(res, ver->fileflags);
	put_dword(res, ver->fileos);
	put_dword(res, ver->filetype);
	put_dword(res, ver->filesubtype);
	put_dword(res, 0);		/* FileDateMS */
	put_dword(res, 0);		/* FileDateLS */
	/* Set ValueSize */
1583
	set_word(res, valsizetag, (WORD)(res->size - tag));
Alexandre Julliard's avatar
Alexandre Julliard committed
1584 1585
	/* Descend into the blocks */
	for(blk = ver->blocks; blk; blk = blk->next)
1586
		versionblock2res(res, blk, 0, win32 ? ver->lvc.language : NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
1587
	/* Set root block's size */
1588
	set_word(res, rootblocksizetag, (WORD)(res->size - rootblocksizetag));
Alexandre Julliard's avatar
Alexandre Julliard committed
1589 1590 1591 1592 1593

	SetResSize(res, restag);
	if(win32)
		put_pad(res);

1594
	free(vsvi.str.cstr);
Alexandre Julliard's avatar
Alexandre Julliard committed
1595 1596 1597
	return res;
}

1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
/*
 *****************************************************************************
 * Function	: toolbaritem2res
 * Syntax	: void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
 * Input	:
 * Output	: nop
 * Description	:
 * Remarks	: Self recursive
 *****************************************************************************
*/
1608
static void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
{
	toolbar_item_t *itm = tbitem;
	assert(win32 != 0);
	while(itm)
	{
		put_word(res, itm->id);
		itm = itm->next;
	}

}

/*
 *****************************************************************************
 * Function	: toolbar2res
 * Syntax	: res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
 * Input	:
 *	name	- Name/ordinal of the resource
 *	toolbar	- The toolbar descriptor
 * Output	: New .res format structure
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
1632
static res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
{
	int restag;
	res_t *res;
	assert(name != NULL);
	assert(toolbar != NULL);

	res = new_res();
	if(win32)
	{
		restag = put_res_header(res, WRC_RT_TOOLBAR, NULL, name, toolbar->memopt, &(toolbar->lvc));

		put_word(res, 1);		/* Menuheader: Version */
		put_word(res, toolbar->button_width); /* (in pixels?) */
		put_word(res, toolbar->button_height); /* (in pixels?) */
1647
		put_word(res, toolbar->nitems);
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663
		put_pad(res);
		toolbaritem2res(res, toolbar->items);
		/* Set ResourceSize */
		SetResSize(res, restag);
		put_pad(res);
	}
	else /* win16 */
	{
		/* Do not generate anything in 16-bit mode */
		free(res->data);
		free(res);
		return NULL;
	}
	return res;
}

1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
/*
 *****************************************************************************
 * Function	: dlginit2res
 * Syntax	: res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
 * Input	:
 *	name	- Name/ordinal of the resource
 *	rdt	- The dlginit descriptor
 * Output	: New .res format structure
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
1676
static res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1677 1678 1679 1680 1681 1682 1683
{
	int restag;
	res_t *res;
	assert(name != NULL);
	assert(dit != NULL);

	res = new_res();
1684
	restag = put_res_header(res, WRC_RT_DLGINIT, NULL, name, dit->memopt, &(dit->data->lvc));
1685 1686 1687 1688 1689 1690 1691 1692
	put_raw_data(res, dit->data, 0);
	/* Set ResourceSize */
	SetResSize(res, restag);
	if(win32)
		put_pad(res);
	return res;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1693 1694 1695
/*
 *****************************************************************************
 * Function	: prep_nid_for_label
1696
 * Syntax	: char *prep_nid_for_label(const name_id_t *nid)
Alexandre Julliard's avatar
Alexandre Julliard committed
1697 1698 1699 1700 1701 1702 1703 1704
 * Input	:
 * Output	:
 * Description	: Converts a resource name into the first 32 (or less)
 *		  characters of the name with conversions.
 * Remarks	:
 *****************************************************************************
*/
#define MAXNAMELEN	32
1705
char *prep_nid_for_label(const name_id_t *nid)
Alexandre Julliard's avatar
Alexandre Julliard committed
1706 1707 1708 1709 1710 1711 1712
{
	static char buf[MAXNAMELEN+1];

	assert(nid != NULL);

	if(nid->type == name_str && nid->name.s_name->type == str_unicode)
	{
1713
		WCHAR *sptr;
Alexandre Julliard's avatar
Alexandre Julliard committed
1714 1715 1716 1717 1718
		int i;
		sptr = nid->name.s_name->str.wstr;
		buf[0] = '\0';
		for(i = 0; *sptr && i < MAXNAMELEN; i++)
		{
1719
			if((unsigned)*sptr < 0x80 && isprint(*sptr & 0xff))
Alexandre Julliard's avatar
Alexandre Julliard committed
1720 1721
				buf[i] = *sptr++;
			else
1722
				warning("Resourcename (str_unicode) contain unprintable characters or invalid translation, ignored\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733
		}
		buf[i] = '\0';
	}
	else if(nid->type == name_str && nid->name.s_name->type == str_char)
	{
		char *cptr;
		int i;
		cptr = nid->name.s_name->str.cstr;
		buf[0] = '\0';
		for(i = 0; *cptr && i < MAXNAMELEN; i++)
		{
1734
			if((unsigned)*cptr < 0x80 && isprint(*cptr & 0xff))
Alexandre Julliard's avatar
Alexandre Julliard committed
1735 1736
				buf[i] = *cptr++;
			else
1737
				warning("Resourcename (str_char) contain unprintable characters, ignored\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
1738 1739 1740 1741 1742 1743 1744 1745 1746
		}
		buf[i] = '\0';
	}
	else if(nid->type == name_ord)
	{
		sprintf(buf, "%u", nid->name.i_name);
	}
	else
	{
1747
		internal_error(__FILE__, __LINE__, "Resource name_id with invalid type %d\n", nid->type);
Alexandre Julliard's avatar
Alexandre Julliard committed
1748 1749 1750 1751 1752 1753 1754 1755
	}
	return buf;
}
#undef MAXNAMELEN

/*
 *****************************************************************************
 * Function	: make_c_name
1756
 * Syntax	: char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
Alexandre Julliard's avatar
Alexandre Julliard committed
1757 1758 1759 1760 1761 1762 1763
 * Input	:
 * Output	:
 * Description	: Converts a resource name into a valid c-identifier in the
 *		  form "_base_nid".
 * Remarks	:
 *****************************************************************************
*/
1764
char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
Alexandre Julliard's avatar
Alexandre Julliard committed
1765
{
1766 1767
	char *buf = prep_nid_for_label(nid);
	return strmake( "_%s_%s_%d", base, buf, lan ? MAKELANGID(lan->id, lan->sub) : 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
1768 1769 1770 1771 1772
}

/*
 *****************************************************************************
 * Function	: get_c_typename
1773
 * Syntax	: const char *get_c_typename(enum res_e type)
Alexandre Julliard's avatar
Alexandre Julliard committed
1774 1775 1776 1777 1778 1779 1780
 * Input	:
 * Output	:
 * Description	: Convert resource enum to char string to be used in c-name
 *		  creation.
 * Remarks	:
 *****************************************************************************
*/
1781
const char *get_c_typename(enum res_e type)
Alexandre Julliard's avatar
Alexandre Julliard committed
1782 1783 1784 1785
{
	switch(type)
	{
	case res_acc:	return "Acc";
1786 1787
	case res_anicur:return "AniCur";
	case res_aniico:return "AniIco";
Alexandre Julliard's avatar
Alexandre Julliard committed
1788 1789 1790
	case res_bmp:	return "Bmp";
	case res_cur:	return "Cur";
	case res_curg:	return "CurGrp";
1791
	case res_dlg:	return "Dlg";
Alexandre Julliard's avatar
Alexandre Julliard committed
1792
	case res_fnt:	return "Fnt";
1793
	case res_fntdir:return "FntDir";
Alexandre Julliard's avatar
Alexandre Julliard committed
1794 1795
	case res_ico:	return "Ico";
	case res_icog:	return "IcoGrp";
1796
	case res_men:	return "Men";
Alexandre Julliard's avatar
Alexandre Julliard committed
1797 1798 1799 1800 1801
	case res_rdt:	return "RCDat";
	case res_stt:	return "StrTab";
	case res_usr:	return "Usr";
	case res_msg:	return "MsgTab";
	case res_ver:	return "VerInf";
1802
	case res_toolbar:	return "TlBr";
1803
	case res_dlginit: return "DlgInit";
Alexandre Julliard's avatar
Alexandre Julliard committed
1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848
	default:	return "Oops";
	}
}

/*
 *****************************************************************************
 * Function	: resources2res
 * Syntax	: void resources2res(resource_t *top)
 * Input	:
 *	top	- The resource-tree to convert
 * Output	:
 * Description	: Convert logical resource descriptors into binary data
 * Remarks	:
 *****************************************************************************
*/
void resources2res(resource_t *top)
{
	while(top)
	{
		switch(top->type)
		{
		case res_acc:
			if(!top->binres)
				top->binres = accelerator2res(top->name, top->res.acc);
			break;
		case res_bmp:
			if(!top->binres)
				top->binres = bitmap2res(top->name, top->res.bmp);
			break;
		case res_cur:
			if(!top->binres)
				top->binres = cursor2res(top->res.cur);
			break;
		case res_curg:
			if(!top->binres)
				top->binres = cursorgroup2res(top->name, top->res.curg);
			break;
		case res_dlg:
			if(!top->binres)
				top->binres = dialog2res(top->name, top->res.dlg);
			break;
		case res_fnt:
			if(!top->binres)
				top->binres = font2res(top->name, top->res.fnt);
			break;
1849 1850 1851 1852
		case res_fntdir:
			if(!top->binres)
				top->binres = fontdir2res(top->name, top->res.fnd);
			break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864
		case res_ico:
			if(!top->binres)
				top->binres = icon2res(top->res.ico);
			break;
		case res_icog:
			if(!top->binres)
				top->binres = icongroup2res(top->name, top->res.icog);
			break;
		case res_men:
			if(!top->binres)
				top->binres = menu2res(top->name, top->res.men);
			break;
1865 1866 1867 1868
		case res_html:
			if(!top->binres)
				top->binres = html2res(top->name, top->res.html);
			break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888
		case res_rdt:
			if(!top->binres)
				top->binres = rcdata2res(top->name, top->res.rdt);
			break;
		case res_stt:
			if(!top->binres)
				top->binres = stringtable2res(top->res.stt);
			break;
		case res_usr:
			if(!top->binres)
				top->binres = user2res(top->name, top->res.usr);
			break;
		case res_msg:
			if(!top->binres)
				top->binres = messagetable2res(top->name, top->res.msg);
			break;
		case res_ver:
			if(!top->binres)
				top->binres = versioninfo2res(top->name, top->res.ver);
			break;
1889 1890 1891 1892
		case res_toolbar:
			if(!top->binres)
				top->binres = toolbar2res(top->name, top->res.tbt);
			break;
1893 1894 1895 1896
		case res_dlginit:
			if(!top->binres)
			    top->binres = dlginit2res(top->name, top->res.dlgi);
			break;
1897 1898 1899 1900 1901
		case res_anicur:
		case res_aniico:
			if(!top->binres)
			    top->binres = anicurico2res(top->name, top->res.ani, top->type);
			break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1902
		default:
1903
			internal_error(__FILE__, __LINE__, "Unknown resource type encountered %d in binary res generation\n", top->type);
Alexandre Julliard's avatar
Alexandre Julliard committed
1904 1905 1906 1907 1908
		}
		top->c_name = make_c_name(get_c_typename(top->type), top->name, top->lan);
		top = top->next;
	}
}