genres.c 52.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"
44
#include "wine/unicode.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
45

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

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

59
res_t *grow_res(res_t *r, unsigned int add)
Alexandre Julliard's avatar
Alexandre Julliard committed
60 61
{
	r->allocsize += add;
62
	r->data = xrealloc(r->data, r->allocsize);
Alexandre Julliard's avatar
Alexandre Julliard committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
	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);
87
	res->data[res->size] = (char)c;
Alexandre Julliard's avatar
Alexandre Julliard committed
88 89 90 91 92 93 94
	res->size += sizeof(char);
}

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

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

void put_dword(res_t *res, unsigned d)
{
	if(res->allocsize - res->size < sizeof(DWORD))
		grow_res(res, RES_BLOCKSIZE);
120 121
	switch(byteorder)
	{
122 123
#ifdef WORDS_BIGENDIAN
	default:
124
#endif
125 126 127 128 129
	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));
130
		break;
131 132

#ifndef WORDS_BIGENDIAN
133
	default:
134 135 136 137 138 139
#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));
140 141
		break;
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
142 143 144
	res->size += sizeof(DWORD);
}

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

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
/*
 *****************************************************************************
 * 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	:
 *****************************************************************************
*/
167
static void set_word(res_t *res, int ofs, unsigned w)
168 169 170
{
	switch(byteorder)
	{
171 172
#ifdef WORDS_BIGENDIAN
	default:
173
#endif
174 175 176
	case WRC_BO_BIG:
		res->data[ofs+0] = HIBYTE(w);
		res->data[ofs+1] = LOBYTE(w);
177
		break;
178 179

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

189
static void set_dword(res_t *res, int ofs, unsigned d)
190 191 192
{
	switch(byteorder)
	{
193 194
#ifdef WORDS_BIGENDIAN
	default:
195
#endif
196 197 198 199 200
	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));
201
		break;
202 203

#ifndef WORDS_BIGENDIAN
204
	default:
205 206 207 208 209 210
#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));
211 212 213 214 215 216
		break;
	}
}

/*
 *****************************************************************************
217
 * Function	: get_dword
218 219 220 221 222 223 224 225 226
 * 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	:
 *****************************************************************************
*/
227
static DWORD get_dword(res_t *res, int ofs)
228 229 230
{
	switch(byteorder)
	{
231 232
#ifdef WORDS_BIGENDIAN
	default:
233
#endif
234 235 236 237 238 239 240
	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
241
	default:
242 243 244 245 246 247
#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];
248 249 250
	}
}

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

    if(str->type == str_char)
    {
        for (i = 0; i < str->size; i++) str->str.cstr[i] = toupper((unsigned char)str->str.cstr[i]);
    }
    else if(str->type == str_unicode)
    {
        for (i = 0; i < str->size; i++) str->str.wstr[i] = toupperW(str->str.wstr[i]);
    }
    else
    {
275
        internal_error(__FILE__, __LINE__, "Invalid string type %d\n", str->type);
276
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
277 278
}

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

    if(key->type == str_char)
    {
285
        if (key->str.cstr[0] == '#') return 0;  /* ignore message contexts */
286 287 288 289 290
	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 );
291
            error("VIRTKEY code is not equal to ascii value\n");
292 293 294 295 296
        }

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

Alexandre Julliard's avatar
Alexandre Julliard committed
341 342 343 344
/*
 *****************************************************************************
 * Function	: put_string
 * Syntax	: void put_string(res_t *res, string_t *str, enum str_e type,
345
 *				  int isterm, const language_t *lang)
Alexandre Julliard's avatar
Alexandre Julliard committed
346 347 348 349 350 351 352 353 354 355 356
 * 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	:
 *****************************************************************************
*/
357 358
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
359
{
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
    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 ))
377 378
            {
                print_location( &str->loc );
379
                error( "String %s does not convert identically to Unicode and back in codepage %d. "
380
                       "Try using a Unicode string instead\n", str->str.cstr, codepage );
381
            }
382
            if (check_valid_utf8( str, codepage ))
383 384
            {
                print_location( &str->loc );
385 386
                warning( "string \"%s\" seems to be UTF-8 but codepage %u is in use.\n",
                         str->str.cstr, codepage );
387
            }
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
        }
        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
410 411 412 413 414
}

/*
 *****************************************************************************
 * Function	: put_name_id
415
 * 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
416 417 418 419 420 421
 * Input	:
 * Output	:
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
422
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
423 424 425 426 427 428 429 430 431 432 433 434 435
{
	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);
436
		put_string(res, nid->name.s_name, win32 ? str_unicode : str_char, TRUE, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
437 438 439
	}
	else
	{
440
		internal_error(__FILE__, __LINE__, "Invalid name_id type %d\n", nid->type);
Alexandre Julliard's avatar
Alexandre Julliard committed
441 442 443 444 445 446 447 448 449 450 451 452 453
	}
}

/*
 *****************************************************************************
 * Function	: put_lvc
 * Syntax	: void put_lvc(res_t *res, lvc_t *lvc)
 * Input	:
 * Output	:
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
454
static void put_lvc(res_t *res, lvc_t *lvc)
Alexandre Julliard's avatar
Alexandre Julliard committed
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
{
	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	:
 *****************************************************************************
*/
480
static void put_raw_data(res_t *res, raw_data_t *raw, int offset)
Alexandre Julliard's avatar
Alexandre Julliard committed
481
{
482
	unsigned int wsize = raw->size - offset;
Alexandre Julliard's avatar
Alexandre Julliard committed
483 484 485 486 487 488 489 490 491
	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
492
 * Syntax	: input_res_header(res_t *res, int type, name_id_t *ntype,
Alexandre Julliard's avatar
Alexandre Julliard committed
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
 *				    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	:
 *****************************************************************************
*/
508 509
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
510 511 512 513 514 515 516 517 518 519 520
{
	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
521 522
			put_name_id(res, ntype, TRUE, lvc->language);
		put_name_id(res, name, TRUE, lvc->language); /* ResName */
Alexandre Julliard's avatar
Alexandre Julliard committed
523 524 525 526
		put_pad(res);
		put_dword(res, 0);		/* DataVersion */
		put_word(res, memopt);		/* Memory options */
		put_lvc(res, lvc);		/* Language, version and characts */
527 528
		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
529 530 531 532 533 534 535 536 537 538 539 540
		res->dataidx = res->size;
		return 0;
	}
	else /* win16 */
	{
		int tag;
		if(!ntype)
		{
			put_byte(res, 0xff);		/* ResType */
			put_word(res, type);
		}
		else
541 542
			put_name_id(res, ntype, TRUE, NULL);
		put_name_id(res, name, TRUE, NULL); /* ResName */
Alexandre Julliard's avatar
Alexandre Julliard committed
543 544 545
		put_word(res, memopt);		/* Memory options */
		tag = res->size;
		put_dword(res, 0);		/* ResSize overwritten later*/
546
		set_dword(res, tag, res->size);
Alexandre Julliard's avatar
Alexandre Julliard committed
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
		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	:
 *****************************************************************************
*/
564
static res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
Alexandre Julliard's avatar
Alexandre Julliard committed
565 566 567 568 569 570 571 572 573 574 575 576 577 578
{
	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)
		{
579 580
			int key = ev->key;
			if (ev->str) key = parse_accel_string( ev->str, ev->flags );
Alexandre Julliard's avatar
Alexandre Julliard committed
581
			put_word(res, ev->flags | (ev->next ? 0 : 0x80));
582
			put_word(res, key);
Alexandre Julliard's avatar
Alexandre Julliard committed
583 584 585 586 587 588 589 590 591 592 593
			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)
		{
594 595
			int key = ev->key;
			if (ev->str) key = parse_accel_string( ev->str, ev->flags );
Alexandre Julliard's avatar
Alexandre Julliard committed
596
			put_byte(res, ev->flags | (ev->next ? 0 : 0x80));
597
			put_word(res, key);
Alexandre Julliard's avatar
Alexandre Julliard committed
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
			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	:
 *****************************************************************************
*/
619
static res_t *dialog2res(name_id_t *name, dialog_t *dlg)
Alexandre Julliard's avatar
Alexandre Julliard committed
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
{
	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));

635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
		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
654 655 656 657 658 659 660
		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)
661
			put_name_id(res, dlg->menu, TRUE, dlg->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
662 663 664
		else
			put_word(res, 0);
		if(dlg->dlgclass)
665
			put_name_id(res, dlg->dlgclass, TRUE, dlg->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
666 667 668
		else
			put_word(res, 0);
		if(dlg->title)
669
			put_string(res, dlg->title, str_unicode, TRUE, dlg->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
670 671 672 673 674
		else
			put_word(res, 0);
		if(dlg->font)
		{
			put_word(res, dlg->font->size);
675 676 677 678 679 680 681 682 683
			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);
			}
684
			put_string(res, dlg->font->name, str_unicode, TRUE, dlg->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
685 686 687 688 689
		}

		put_pad(res);
		while(ctrl)
		{
690 691 692 693 694 695 696 697 698 699 700 701 702
			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
703 704 705 706
			put_word(res, ctrl->x);
			put_word(res, ctrl->y);
			put_word(res, ctrl->width);
			put_word(res, ctrl->height);
707 708 709 710
			if (dlg->is_ex)
				put_dword(res, ctrl->id);
			else
				put_word(res, ctrl->id);
Alexandre Julliard's avatar
Alexandre Julliard committed
711
			if(ctrl->ctlclass)
712
				put_name_id(res, ctrl->ctlclass, TRUE, dlg->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
713
			else
714
				internal_error(__FILE__, __LINE__, "Control has no control-class\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
715
			if(ctrl->title)
716
				put_name_id(res, ctrl->title, FALSE, dlg->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
			else
				put_word(res, 0);
			if(ctrl->extra)
			{
				put_word(res, ctrl->extra->size+2);
				put_pad(res);
				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 */
734
		set_word(res, tag_nctrl, (WORD)nctrl);
Alexandre Julliard's avatar
Alexandre Julliard committed
735 736 737 738 739
	}
	else /* win16 */
	{
		restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, NULL);

740
		put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
Alexandre Julliard's avatar
Alexandre Julliard committed
741 742 743 744 745 746 747
		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)
748
			put_name_id(res, dlg->menu, TRUE, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
749 750 751
		else
			put_byte(res, 0);
		if(dlg->dlgclass)
752
			put_name_id(res, dlg->dlgclass, TRUE, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
753 754 755
		else
			put_byte(res, 0);
		if(dlg->title)
756
			put_string(res, dlg->title, str_char, TRUE, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
757 758 759 760 761
		else
			put_byte(res, 0);
		if(dlg->font)
		{
			put_word(res, dlg->font->size);
762
			put_string(res, dlg->font->name, str_char, TRUE, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
763 764 765 766 767 768 769 770 771
		}

		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);
772
			put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
Alexandre Julliard's avatar
Alexandre Julliard committed
773 774 775 776 777 778 779
			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)
780
					put_name_id(res, ctrl->ctlclass, FALSE, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
781
				else
782
					error("Unknown control-class %04x\n", ctrl->ctlclass->name.i_name);
Alexandre Julliard's avatar
Alexandre Julliard committed
783 784
			}
			else
785
				internal_error(__FILE__, __LINE__, "Control has no control-class\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
786
			if(ctrl->title)
787
				put_name_id(res, ctrl->title, FALSE, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
788 789 790 791 792
			else
				put_byte(res, 0);

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

Alexandre Julliard's avatar
Alexandre Julliard committed
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
			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
 *****************************************************************************
*/
815
static void menuitem2res(res_t *res, menu_item_t *menitem, const language_t *lang)
Alexandre Julliard's avatar
Alexandre Julliard committed
816 817 818 819 820 821 822 823 824 825
{
	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)
826
				put_string(res, itm->name, str_unicode, TRUE, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
827 828 829
			else
				put_word(res, 0);
			if(itm->popup)
830
				menuitem2res(res, itm->popup, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
831 832 833 834 835 836 837 838 839 840 841
			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)
842
				put_string(res, itm->name, str_char, TRUE, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
843 844 845
			else
				put_byte(res, 0);
			if(itm->popup)
846
				menuitem2res(res, itm->popup, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
			itm = itm->next;
		}
	}

}

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

}

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

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

912 913 914 915 916 917 918 919 920 921 922 923 924
		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
925 926 927 928 929 930
		/* Set ResourceSize */
		SetResSize(res, restag);
		put_pad(res);
	}
	else /* win16 */
	{
931 932 933 934 935 936
		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
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
	}
	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	:
 *****************************************************************************
*/
953
static res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
Alexandre Julliard's avatar
Alexandre Julliard committed
954 955 956 957 958 959 960 961
{
	int restag;
	res_t *res;
	cursor_t *cur;
	assert(name != NULL);
	assert(curg != NULL);

	res = new_res();
Alexandre Julliard's avatar
Alexandre Julliard committed
962
	restag = put_res_header(res, WRC_RT_GROUP_CURSOR, NULL, name, curg->memopt, &(curg->lvc));
Alexandre Julliard's avatar
Alexandre Julliard committed
963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
	if(win32)
	{
		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);
#if 0
		for(cur = curg->cursorlist; cur; cur = cur->next)
#else
		cur = curg->cursorlist;
		while(cur->next)
			cur = cur->next;
		for(; cur; cur = cur->prev)
#endif
		{
			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.
Austin English's avatar
Austin English committed
998
			 * However, I could not find this in the documentation.
Alexandre Julliard's avatar
Alexandre Julliard committed
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
			 * The hotspot bytes must either be included or MS
			 * doesn't care.
			 */
			put_dword(res, cur->data->size +4);
			put_word(res, cur->id);
		}
	}
	else /* win16 */
	{
		put_word(res, 0);	/* Reserved */
		put_word(res, 2);	/* ResType */
		put_word(res, curg->ncursor);
#if 0
		for(cur = curg->cursorlist; cur; cur = cur->next)
#else
		cur = curg->cursorlist;
		while(cur->next)
			cur = cur->next;
		for(; cur; cur = cur->prev)
#endif
		{
			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.
Austin English's avatar
Austin English committed
1035
			 * However, I could not find this in the documentation.
Alexandre Julliard's avatar
Alexandre Julliard committed
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
			 * The hotspot bytes must either be included or MS
			 * doesn't care.
			 */
			put_dword(res, cur->data->size +4);
			put_word(res, cur->id);
		}
	}
	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	:
 *****************************************************************************
*/
1061
static res_t *cursor2res(cursor_t *cur)
Alexandre Julliard's avatar
Alexandre Julliard committed
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
{
	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
1072
	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
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
	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	:
 *****************************************************************************
*/
1096
static res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
Alexandre Julliard's avatar
Alexandre Julliard committed
1097 1098 1099 1100 1101 1102 1103 1104
{
	int restag;
	res_t *res;
	icon_t *ico;
	assert(name != NULL);
	assert(icog != NULL);

	res = new_res();
Alexandre Julliard's avatar
Alexandre Julliard committed
1105
	restag = put_res_header(res, WRC_RT_GROUP_ICON, NULL, name, icog->memopt, &(icog->lvc));
Alexandre Julliard's avatar
Alexandre Julliard committed
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 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
	if(win32)
	{
		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);
		}
	}
	else /* win16 */
	{
		put_word(res, 0);	/* Reserved */
		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);
		}
	}
	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	:
 *****************************************************************************
*/
1164
static res_t *icon2res(icon_t *ico)
Alexandre Julliard's avatar
Alexandre Julliard committed
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
{
	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
1175
	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
1176 1177 1178 1179 1180 1181 1182 1183 1184
	put_raw_data(res, ico->data, 0);

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

	return res;
}

1185 1186 1187 1188 1189 1190 1191 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
/*
 *****************************************************************************
 * 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
1219 1220 1221 1222 1223 1224 1225 1226 1227
/*
 *****************************************************************************
 * 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	:
1228
 * Remarks	: The endian of the bitmap structures have been converted
1229
 * 		  by the loader.
Alexandre Julliard's avatar
Alexandre Julliard committed
1230 1231
 *****************************************************************************
*/
1232
static res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
Alexandre Julliard's avatar
Alexandre Julliard committed
1233 1234 1235 1236 1237 1238 1239
{
	int restag;
	res_t *res;
	assert(name != NULL);
	assert(bmp != NULL);

	res = new_res();
1240
	restag = put_res_header(res, WRC_RT_BITMAP, NULL, name, bmp->memopt, &(bmp->data->lvc));
Alexandre Julliard's avatar
Alexandre Julliard committed
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 1267 1268
	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	:
1269
 * Remarks	: The data has been prepared just after parsing.
Alexandre Julliard's avatar
Alexandre Julliard committed
1270 1271
 *****************************************************************************
*/
1272
static res_t *font2res(name_id_t *name, font_t *fnt)
Alexandre Julliard's avatar
Alexandre Julliard committed
1273
{
1274 1275
	int restag;
	res_t *res;
Alexandre Julliard's avatar
Alexandre Julliard committed
1276 1277
	assert(name != NULL);
	assert(fnt != NULL);
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315

	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
1316 1317
}

1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
/*
 *****************************************************************************
 * 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
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
/*
 *****************************************************************************
 * 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	:
 *****************************************************************************
*/
1359
static res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
Alexandre Julliard's avatar
Alexandre Julliard committed
1360 1361 1362 1363 1364 1365 1366
{
	int restag;
	res_t *res;
	assert(name != NULL);
	assert(rdt != NULL);

	res = new_res();
1367
	restag = put_res_header(res, WRC_RT_RCDATA, NULL, name, rdt->memopt, &(rdt->data->lvc));
Alexandre Julliard's avatar
Alexandre Julliard committed
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
	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	:
1385
 * Remarks	: The data has been converted to the appropriate endian
1386
 *		  after it was parsed.
Alexandre Julliard's avatar
Alexandre Julliard committed
1387 1388
 *****************************************************************************
*/
1389
static res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
Alexandre Julliard's avatar
Alexandre Julliard committed
1390
{
1391 1392
	int restag;
	res_t *res;
Alexandre Julliard's avatar
Alexandre Julliard committed
1393 1394
	assert(name != NULL);
	assert(msg != NULL);
1395 1396 1397 1398 1399 1400 1401 1402 1403

	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
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
}

/*
 *****************************************************************************
 * Function	: stringtable2res
 * Syntax	: res_t *stringtable2res(stringtable_t *stt)
 * Input	:
 *	stt	- The stringtable descriptor
 * Output	: New .res format structure
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
1417
static res_t *stringtable2res(stringtable_t *stt)
Alexandre Julliard's avatar
Alexandre Julliard committed
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
{
	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)
		{
1432
			warning("Empty internal stringtable\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
1433 1434 1435 1436 1437 1438 1439
			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++)
		{
1440
			if(stt->entries[i].str && stt->entries[i].str->size)
Alexandre Julliard's avatar
Alexandre Julliard committed
1441
			{
1442 1443
				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
1444 1445 1446
			}
			else
			{
1447
				if (win32)
Alexandre Julliard's avatar
Alexandre Julliard committed
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
					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	:
 *****************************************************************************
*/
1474
static res_t *user2res(name_id_t *name, user_t *usr)
Alexandre Julliard's avatar
Alexandre Julliard committed
1475 1476 1477 1478 1479 1480 1481
{
	int restag;
	res_t *res;
	assert(name != NULL);
	assert(usr != NULL);

	res = new_res();
1482
	restag = put_res_header(res, 0, usr->type, name, usr->memopt, &(usr->data->lvc));
Alexandre Julliard's avatar
Alexandre Julliard committed
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
	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
 *****************************************************************************
*/
1503
static void versionblock2res(res_t *res, ver_block_t *blk, int level, const language_t *lang)
Alexandre Julliard's avatar
Alexandre Julliard committed
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
{
	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 ? */
1517
	put_string(res, blk->name, win32 ? str_unicode : str_char, TRUE, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
	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);
			}
1531
			put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
1532 1533
			put_pad(res);
			tag = res->size;
1534
			put_string(res, val->value.str, win32 ? str_unicode : str_char, TRUE, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
1535
			if(win32)
1536
				set_word(res, valvalsizetag, (WORD)((res->size - tag) >> 1));
Alexandre Julliard's avatar
Alexandre Julliard committed
1537
			else
1538 1539
				set_word(res, valvalsizetag, (WORD)(res->size - tag));
			set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
Alexandre Julliard's avatar
Alexandre Julliard committed
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
			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);
			}
1552
			put_string(res, val->key, win32 ? str_unicode : str_char, TRUE, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
1553 1554 1555 1556 1557 1558
			put_pad(res);
			tag = res->size;
			for(i = 0; i < val->value.words->nwords; i++)
			{
				put_word(res, val->value.words->words[i]);
			}
1559 1560
			set_word(res, valvalsizetag, (WORD)(res->size - tag));
			set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
Alexandre Julliard's avatar
Alexandre Julliard committed
1561 1562 1563 1564
			put_pad(res);
		}
		else if(val->type == val_block)
		{
1565
			versionblock2res(res, val->value.block, level+1, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
1566 1567 1568
		}
		else
		{
1569
			internal_error(__FILE__, __LINE__, "Invalid value indicator %d in VERSIONINFO\n", val->type);
Alexandre Julliard's avatar
Alexandre Julliard committed
1570 1571 1572 1573
		}
	}

	/* Set blocksize */
1574
	set_word(res, blksizetag, (WORD)(res->size - blksizetag));
Alexandre Julliard's avatar
Alexandre Julliard committed
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
}

/*
 *****************************************************************************
 * 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	:
 *****************************************************************************
*/
1589
static res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
Alexandre Julliard's avatar
Alexandre Julliard committed
1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
{
	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;
1603
	vsvi.str.cstr = xstrdup("VS_VERSION_INFO");
Alexandre Julliard's avatar
Alexandre Julliard committed
1604 1605 1606
	vsvi.size = 15; /* Excl. termination */

	res = new_res();
1607
	restag = put_res_header(res, WRC_RT_VERSION, NULL, name, ver->memopt, &(ver->lvc));
Alexandre Julliard's avatar
Alexandre Julliard committed
1608 1609 1610 1611 1612 1613
	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 ? */
1614 1615
	put_string(res, &vsvi, win32 ? str_unicode : str_char,
                   TRUE, win32 ? ver->lvc.language : NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
	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 */
1633
	set_word(res, valsizetag, (WORD)(res->size - tag));
Alexandre Julliard's avatar
Alexandre Julliard committed
1634 1635
	/* Descend into the blocks */
	for(blk = ver->blocks; blk; blk = blk->next)
1636
		versionblock2res(res, blk, 0, win32 ? ver->lvc.language : NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
1637
	/* Set root block's size */
1638
	set_word(res, rootblocksizetag, (WORD)(res->size - rootblocksizetag));
Alexandre Julliard's avatar
Alexandre Julliard committed
1639 1640 1641 1642 1643

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

1644
	free(vsvi.str.cstr);
Alexandre Julliard's avatar
Alexandre Julliard committed
1645 1646 1647
	return res;
}

1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
/*
 *****************************************************************************
 * Function	: toolbaritem2res
 * Syntax	: void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
 * Input	:
 * Output	: nop
 * Description	:
 * Remarks	: Self recursive
 *****************************************************************************
*/
1658
static void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
{
	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	:
 *****************************************************************************
*/
1682
static res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
{
	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?) */
1697
		put_word(res, toolbar->nitems);
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713
		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;
}

1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
/*
 *****************************************************************************
 * 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	:
 *****************************************************************************
*/
1726
static res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1727 1728 1729 1730 1731 1732 1733
{
	int restag;
	res_t *res;
	assert(name != NULL);
	assert(dit != NULL);

	res = new_res();
1734
	restag = put_res_header(res, WRC_RT_DLGINIT, NULL, name, dit->memopt, &(dit->data->lvc));
1735 1736 1737 1738 1739 1740 1741 1742
	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
1743 1744 1745
/*
 *****************************************************************************
 * Function	: prep_nid_for_label
1746
 * Syntax	: char *prep_nid_for_label(const name_id_t *nid)
Alexandre Julliard's avatar
Alexandre Julliard committed
1747 1748 1749 1750 1751 1752 1753 1754
 * Input	:
 * Output	:
 * Description	: Converts a resource name into the first 32 (or less)
 *		  characters of the name with conversions.
 * Remarks	:
 *****************************************************************************
*/
#define MAXNAMELEN	32
1755
char *prep_nid_for_label(const name_id_t *nid)
Alexandre Julliard's avatar
Alexandre Julliard committed
1756 1757 1758 1759 1760 1761 1762
{
	static char buf[MAXNAMELEN+1];

	assert(nid != NULL);

	if(nid->type == name_str && nid->name.s_name->type == str_unicode)
	{
1763
		WCHAR *sptr;
Alexandre Julliard's avatar
Alexandre Julliard committed
1764 1765 1766 1767 1768
		int i;
		sptr = nid->name.s_name->str.wstr;
		buf[0] = '\0';
		for(i = 0; *sptr && i < MAXNAMELEN; i++)
		{
1769
			if((unsigned)*sptr < 0x80 && isprint(*sptr & 0xff))
Alexandre Julliard's avatar
Alexandre Julliard committed
1770 1771
				buf[i] = *sptr++;
			else
1772
				warning("Resourcename (str_unicode) contain unprintable characters or invalid translation, ignored\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
		}
		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++)
		{
1784
			if((unsigned)*cptr < 0x80 && isprint(*cptr & 0xff))
Alexandre Julliard's avatar
Alexandre Julliard committed
1785 1786
				buf[i] = *cptr++;
			else
1787
				warning("Resourcename (str_char) contain unprintable characters, ignored\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
1788 1789 1790 1791 1792 1793 1794 1795 1796
		}
		buf[i] = '\0';
	}
	else if(nid->type == name_ord)
	{
		sprintf(buf, "%u", nid->name.i_name);
	}
	else
	{
1797
		internal_error(__FILE__, __LINE__, "Resource name_id with invalid type %d\n", nid->type);
Alexandre Julliard's avatar
Alexandre Julliard committed
1798 1799 1800 1801 1802 1803 1804 1805
	}
	return buf;
}
#undef MAXNAMELEN

/*
 *****************************************************************************
 * Function	: make_c_name
1806
 * Syntax	: char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
Alexandre Julliard's avatar
Alexandre Julliard committed
1807 1808 1809 1810 1811 1812 1813
 * Input	:
 * Output	:
 * Description	: Converts a resource name into a valid c-identifier in the
 *		  form "_base_nid".
 * Remarks	:
 *****************************************************************************
*/
1814
char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
Alexandre Julliard's avatar
Alexandre Julliard committed
1815
{
1816 1817
	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
1818 1819 1820 1821 1822
}

/*
 *****************************************************************************
 * Function	: get_c_typename
1823
 * Syntax	: const char *get_c_typename(enum res_e type)
Alexandre Julliard's avatar
Alexandre Julliard committed
1824 1825 1826 1827 1828 1829 1830
 * Input	:
 * Output	:
 * Description	: Convert resource enum to char string to be used in c-name
 *		  creation.
 * Remarks	:
 *****************************************************************************
*/
1831
const char *get_c_typename(enum res_e type)
Alexandre Julliard's avatar
Alexandre Julliard committed
1832 1833 1834 1835
{
	switch(type)
	{
	case res_acc:	return "Acc";
1836 1837
	case res_anicur:return "AniCur";
	case res_aniico:return "AniIco";
Alexandre Julliard's avatar
Alexandre Julliard committed
1838 1839 1840
	case res_bmp:	return "Bmp";
	case res_cur:	return "Cur";
	case res_curg:	return "CurGrp";
1841
	case res_dlg:	return "Dlg";
Alexandre Julliard's avatar
Alexandre Julliard committed
1842
	case res_fnt:	return "Fnt";
1843
	case res_fntdir:return "FntDir";
Alexandre Julliard's avatar
Alexandre Julliard committed
1844 1845
	case res_ico:	return "Ico";
	case res_icog:	return "IcoGrp";
1846
	case res_men:	return "Men";
Alexandre Julliard's avatar
Alexandre Julliard committed
1847 1848 1849 1850 1851
	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";
1852
	case res_toolbar:	return "TlBr";
1853
	case res_dlginit: return "DlgInit";
Alexandre Julliard's avatar
Alexandre Julliard committed
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898
	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;
1899 1900 1901 1902
		case res_fntdir:
			if(!top->binres)
				top->binres = fontdir2res(top->name, top->res.fnd);
			break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914
		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;
1915 1916 1917 1918
		case res_html:
			if(!top->binres)
				top->binres = html2res(top->name, top->res.html);
			break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938
		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;
1939 1940 1941 1942
		case res_toolbar:
			if(!top->binres)
				top->binres = toolbar2res(top->name, top->res.tbt);
			break;
1943 1944 1945 1946
		case res_dlginit:
			if(!top->binres)
			    top->binres = dlginit2res(top->name, top->res.dlgi);
			break;
1947 1948 1949 1950 1951
		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
1952
		default:
1953
			internal_error(__FILE__, __LINE__, "Unknown resource type encountered %d in binary res generation\n", top->type);
Alexandre Julliard's avatar
Alexandre Julliard committed
1954 1955 1956 1957 1958
		}
		top->c_name = make_c_name(get_c_typename(top->type), top->name, top->lan);
		top = top->next;
	}
}