genres.c 52.6 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 18 19 20
 * 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * 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 33 34 35 36 37 38
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>

#include "wrc.h"
#include "genres.h"
#include "utils.h"
39
#include "windef.h"
40
#include "winbase.h"
41
#include "wingdi.h"
42
#include "winuser.h"
43
#include "wine/unicode.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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

res_t *new_res(void)
{
	res_t *r;
	r = (res_t *)xmalloc(sizeof(res_t));
	r->allocsize = RES_BLOCKSIZE;
	r->size = 0;
	r->data = (char *)xmalloc(RES_BLOCKSIZE);
	return r;
}

res_t *grow_res(res_t *r, int add)
{
	r->allocsize += add;
	r->data = (char *)xrealloc(r->data, r->allocsize);
	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);
85
	res->data[res->size] = (char)c;
Alexandre Julliard's avatar
Alexandre Julliard committed
86 87 88 89 90 91 92
	res->size += sizeof(char);
}

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

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

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

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

void put_pad(res_t *res)
{
	while(res->size & 0x3)
		put_byte(res, 0);
}

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

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

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

#ifndef WORDS_BIGENDIAN
202
	default:
203 204 205 206 207 208
#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));
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
		break;
	}
}

/*
 *****************************************************************************
 * Function	: get_word
 *		  get_dword
 * Syntax	: WORD get_word(res_t *res, int ofs)
 *		  DWORD get_dword(res_t *res, int ofs)
 * 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	:
 *****************************************************************************
*/
WORD get_word(res_t *res, int ofs)
{
	switch(byteorder)
	{
232 233
#ifdef WORDS_BIGENDIAN
	default:
234
#endif
235 236 237 238 239
	case WRC_BO_BIG:
		return   (res->data[ofs+0] << 8)
		       |  res->data[ofs+1];

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

DWORD get_dword(res_t *res, int ofs)
{
	switch(byteorder)
	{
252 253
#ifdef WORDS_BIGENDIAN
	default:
254
#endif
255 256 257 258 259 260 261
	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
262
	default:
263 264 265 266 267 268
#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];
269 270 271
	}
}

Alexandre Julliard's avatar
Alexandre Julliard committed
272 273 274 275 276 277 278 279 280 281
/*
 *****************************************************************************
 * Function	: string_to_upper
 * Syntax	: void string_to_upper(string_t *str)
 * Input	:
 * Output	:
 * Description	:
 * Remarks	: FIXME: codepages...
 *****************************************************************************
*/
282
static void string_to_upper(string_t *str)
Alexandre Julliard's avatar
Alexandre Julliard committed
283
{
284 285 286 287 288 289 290 291 292 293 294 295 296 297
    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
    {
        internal_error(__FILE__, __LINE__, "Invalid string type %d", str->type);
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
298 299 300 301 302 303
}

/*
 *****************************************************************************
 * Function	: put_string
 * Syntax	: void put_string(res_t *res, string_t *str, enum str_e type,
304
 *				  int isterm, const language_t *lang)
Alexandre Julliard's avatar
Alexandre Julliard committed
305 306 307 308 309 310 311 312 313 314 315
 * 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	:
 *****************************************************************************
*/
316 317
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
318
{
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
    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 ))
                error( "String %s does not convert identically to Unicode and back in codepage %d. "
                       "Try using a Unicode string instead.", str->str.cstr, codepage );
        }
        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
360 361 362 363 364
}

/*
 *****************************************************************************
 * Function	: put_name_id
365
 * 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
366 367 368 369 370 371
 * Input	:
 * Output	:
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
372
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
373 374 375 376 377 378 379 380 381 382 383 384 385
{
	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);
386
		put_string(res, nid->name.s_name, win32 ? str_unicode : str_char, TRUE, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
	}
	else
	{
		internal_error(__FILE__, __LINE__, "Invalid name_id type %d", nid->type);
	}
}

/*
 *****************************************************************************
 * Function	: put_lvc
 * Syntax	: void put_lvc(res_t *res, lvc_t *lvc)
 * Input	:
 * Output	:
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
404
static void put_lvc(res_t *res, lvc_t *lvc)
Alexandre Julliard's avatar
Alexandre Julliard committed
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
{
	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	:
 *****************************************************************************
*/
void put_raw_data(res_t *res, raw_data_t *raw, int offset)
{
	int wsize = raw->size - offset;
	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
 * Syntax	: intput_res_header(res_t *res, int type, name_id_t *ntype,
 *				    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	:
 *****************************************************************************
*/
int put_res_header(res_t *res, int type, name_id_t *ntype, name_id_t *name,
		      DWORD memopt, lvc_t *lvc)
{
	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
471 472
			put_name_id(res, ntype, TRUE, lvc->language);
		put_name_id(res, name, TRUE, lvc->language); /* ResName */
Alexandre Julliard's avatar
Alexandre Julliard committed
473 474 475 476
		put_pad(res);
		put_dword(res, 0);		/* DataVersion */
		put_word(res, memopt);		/* Memory options */
		put_lvc(res, lvc);		/* Language, version and characts */
477 478
		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
479 480 481 482 483 484 485 486 487 488 489 490
		res->dataidx = res->size;
		return 0;
	}
	else /* win16 */
	{
		int tag;
		if(!ntype)
		{
			put_byte(res, 0xff);		/* ResType */
			put_word(res, type);
		}
		else
491 492
			put_name_id(res, ntype, TRUE, NULL);
		put_name_id(res, name, TRUE, NULL); /* ResName */
Alexandre Julliard's avatar
Alexandre Julliard committed
493 494 495
		put_word(res, memopt);		/* Memory options */
		tag = res->size;
		put_dword(res, 0);		/* ResSize overwritten later*/
496
		set_dword(res, tag, res->size);
Alexandre Julliard's avatar
Alexandre Julliard committed
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
		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	:
 *****************************************************************************
*/
514
static res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
Alexandre Julliard's avatar
Alexandre Julliard committed
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
{
	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)
		{
			put_word(res, ev->flags | (ev->next ? 0 : 0x80));
			put_word(res, ev->key);
			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)
		{
			put_byte(res, ev->flags | (ev->next ? 0 : 0x80));
			put_word(res, ev->key);
			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	:
 *****************************************************************************
*/
565
static res_t *dialog2res(name_id_t *name, dialog_t *dlg)
Alexandre Julliard's avatar
Alexandre Julliard committed
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
{
	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));

581 582
		put_dword(res, dlg->style->or_mask);
		put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
583 584 585 586 587 588 589
		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)
590
			put_name_id(res, dlg->menu, TRUE, dlg->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
591 592 593
		else
			put_word(res, 0);
		if(dlg->dlgclass)
594
			put_name_id(res, dlg->dlgclass, TRUE, dlg->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
595 596 597
		else
			put_word(res, 0);
		if(dlg->title)
598
			put_string(res, dlg->title, str_unicode, TRUE, dlg->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
599 600 601 602 603
		else
			put_word(res, 0);
		if(dlg->font)
		{
			put_word(res, dlg->font->size);
604
			put_string(res, dlg->font->name, str_unicode, TRUE, dlg->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
605 606 607 608 609 610
		}

		put_pad(res);
		while(ctrl)
		{
			/* FIXME: what is default control style? */
611 612
			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
613 614 615 616 617 618
			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);
			if(ctrl->ctlclass)
619
				put_name_id(res, ctrl->ctlclass, TRUE, dlg->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
620 621 622
			else
				internal_error(__FILE__, __LINE__, "Control has no control-class");
			if(ctrl->title)
623
				put_name_id(res, ctrl->title, FALSE, dlg->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
			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 */
641
		set_word(res, tag_nctrl, (WORD)nctrl);
Alexandre Julliard's avatar
Alexandre Julliard committed
642 643 644 645 646
	}
	else /* win16 */
	{
		restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, NULL);

647
		put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
Alexandre Julliard's avatar
Alexandre Julliard committed
648 649 650 651 652 653 654
		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)
655
			put_name_id(res, dlg->menu, TRUE, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
656 657 658
		else
			put_byte(res, 0);
		if(dlg->dlgclass)
659
			put_name_id(res, dlg->dlgclass, TRUE, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
660 661 662
		else
			put_byte(res, 0);
		if(dlg->title)
663
			put_string(res, dlg->title, str_char, TRUE, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
664 665 666 667 668
		else
			put_byte(res, 0);
		if(dlg->font)
		{
			put_word(res, dlg->font->size);
669
			put_string(res, dlg->font->name, str_char, TRUE, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
670 671 672 673 674 675 676 677 678
		}

		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);
679
			put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
Alexandre Julliard's avatar
Alexandre Julliard committed
680 681 682 683 684 685 686
			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)
687
					put_name_id(res, ctrl->ctlclass, FALSE, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
688 689 690 691 692 693
				else
					error("Unknown control-class %04x", ctrl->ctlclass->name.i_name);
			}
			else
				internal_error(__FILE__, __LINE__, "Control has no control-class");
			if(ctrl->title)
694
				put_name_id(res, ctrl->title, FALSE, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
695 696 697 698 699
			else
				put_byte(res, 0);

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

Alexandre Julliard's avatar
Alexandre Julliard committed
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
			nctrl++;
			ctrl = ctrl->next;
		}
		/* Set number of controls */
		((char *)res->data)[tag_nctrl] = (char)nctrl;
	}
	/* Set ResourceSize */
	SetResSize(res, restag);
	return res;
}

/*
 *****************************************************************************
 * Function	: dialogex2res
 * Syntax	: res_t *dialogex2res(name_id_t *name, dialogex_t *dlgex)
 * Input	:
 *	name	- Name/ordinal of the resource
 *	dlgex	- The dialogex descriptor
 * Output	: New .res format structure
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
724
static res_t *dialogex2res(name_id_t *name, dialogex_t *dlgex)
Alexandre Julliard's avatar
Alexandre Julliard committed
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
{
	int restag;
	res_t *res;
	control_t *ctrl;
	int tag_nctrl;
	int nctrl = 0;
	assert(name != NULL);
	assert(dlgex != NULL);

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

		/* FIXME: MS doc says thet 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, dlgex->gothelpid ? dlgex->helpid : 0);
749 750
		put_dword(res, dlgex->gotexstyle ? dlgex->exstyle->or_mask : 0);
		put_dword(res, dlgex->gotstyle ? dlgex->style->or_mask : WS_POPUPWINDOW);
Alexandre Julliard's avatar
Alexandre Julliard committed
751 752 753 754 755 756 757
		tag_nctrl = res->size;
		put_word(res, 0);		/* Number of controls */
		put_word(res, dlgex->x);
		put_word(res, dlgex->y);
		put_word(res, dlgex->width);
		put_word(res, dlgex->height);
		if(dlgex->menu)
758
			put_name_id(res, dlgex->menu, TRUE, dlgex->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
759 760 761
		else
			put_word(res, 0);
		if(dlgex->dlgclass)
762
			put_name_id(res, dlgex->dlgclass, TRUE, dlgex->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
763 764 765
		else
			put_word(res, 0);
		if(dlgex->title)
766
			put_string(res, dlgex->title, str_unicode, TRUE, dlgex->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
767 768 769 770 771 772 773 774 775 776 777
		else
			put_word(res, 0);
		if(dlgex->font)
		{
			put_word(res, dlgex->font->size);
			put_word(res, dlgex->font->weight);
			/* FIXME: ? TRUE should be sufficient to say that its
			 * italic, but Borland's compiler says its 0x0101.
			 * I just copy it here, and hope for the best.
			 */
			put_word(res, dlgex->font->italic ? 0x0101 : 0);
778
			put_string(res, dlgex->font->name, str_unicode, TRUE, dlgex->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
779 780 781 782 783 784
		}

		put_pad(res);
		while(ctrl)
		{
			put_dword(res, ctrl->gothelpid ? ctrl->helpid : 0);
785
			put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
786
			/* FIXME: what is default control style? */
787
			put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask : WS_CHILD | WS_VISIBLE);
Alexandre Julliard's avatar
Alexandre Julliard committed
788 789 790 791 792 793 794 795
			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);
			/* FIXME: Pad is _NOT_ documented!?! */
			put_pad(res);
			if(ctrl->ctlclass)
796
				put_name_id(res, ctrl->ctlclass, TRUE, dlgex->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
797 798 799
			else
				internal_error(__FILE__, __LINE__, "Control has no control-class");
			if(ctrl->title)
800
				put_name_id(res, ctrl->title, FALSE, dlgex->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
			else
				put_word(res, 0);
			if(ctrl->extra)
			{
				put_pad(res);
				put_word(res, ctrl->extra->size);
				put_raw_data(res, ctrl->extra, 0);
			}
			else
				put_word(res, 0);

			put_pad(res);
			nctrl++;
			ctrl = ctrl->next;
		}
		/* Set number of controls */
817
		set_word(res, tag_nctrl, (WORD)nctrl);
Alexandre Julliard's avatar
Alexandre Julliard committed
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
		/* 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;
}

/*
 *****************************************************************************
 * Function	: menuitem2res
 * Syntax	: void menuitem2res(res_t *res, menu_item_t *item)
 * Input	:
 * Output	:
 * Description	:
 * Remarks	: Self recursive
 *****************************************************************************
*/
842
static void menuitem2res(res_t *res, menu_item_t *menitem, const language_t *lang)
Alexandre Julliard's avatar
Alexandre Julliard committed
843 844 845 846 847 848 849 850 851 852
{
	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)
853
				put_string(res, itm->name, str_unicode, TRUE, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
854 855 856
			else
				put_word(res, 0);
			if(itm->popup)
857
				menuitem2res(res, itm->popup, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
858 859 860 861 862 863 864 865 866 867 868
			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)
869
				put_string(res, itm->name, str_char, TRUE, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
870 871 872
			else
				put_byte(res, 0);
			if(itm->popup)
873
				menuitem2res(res, itm->popup, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
			itm = itm->next;
		}
	}

}

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

	res = new_res();
	restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, win32 ? &(men->lvc) : NULL);

	put_dword(res, 0);		/* Menuheader: Version and HeaderSize */
903
	menuitem2res(res, men->items, win32 ? men->lvc.language : NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
	/* Set ResourceSize */
	SetResSize(res, restag);
	if(win32)
		put_pad(res);
	return res;
}

/*
 *****************************************************************************
 * Function	: menuexitem2res
 * Syntax	: void menuexitem2res(res_t *res, menuex_item_t *item)
 * Input	:
 * Output	: nop
 * Description	:
 * Remarks	: Self recursive
 *****************************************************************************
*/
921
static void menuexitem2res(res_t *res, menuex_item_t *menitem, const language_t *lang)
Alexandre Julliard's avatar
Alexandre Julliard committed
922 923 924 925 926 927 928 929 930 931
{
	menuex_item_t *itm = menitem;
	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)
932
			put_string(res, itm->name, str_unicode, TRUE, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
933 934 935 936 937 938
		else
			put_word(res, 0);
		put_pad(res);
		if(itm->popup)
		{
			put_dword(res, itm->gothelpid ? itm->helpid : 0);
939
			menuexitem2res(res, itm->popup, lang);
Alexandre Julliard's avatar
Alexandre Julliard committed
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
		}
		itm = itm->next;
	}

}

/*
 *****************************************************************************
 * Function	: menuex2res
 * Syntax	: res_t *menuex2res(name_id_t *name, menuex_t *menex)
 * Input	:
 *	name	- Name/ordinal of the resource
 *	menex	- The menuex descriptor
 * Output	: New .res format structure
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
958
static res_t *menuex2res(name_id_t *name, menuex_t *menex)
Alexandre Julliard's avatar
Alexandre Julliard committed
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
{
	int restag;
	res_t *res;
	assert(name != NULL);
	assert(menex != NULL);

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

		put_word(res, 1);		/* Menuheader: Version */
		put_word(res, 4);		/* Offset */
		put_dword(res, 0);		/* HelpId */
		put_pad(res);
974
		menuexitem2res(res, menex->items, menex->lvc.language);
Alexandre Julliard's avatar
Alexandre Julliard committed
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
		/* 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;
}

/*
 *****************************************************************************
 * 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	:
 *****************************************************************************
*/
1001
static res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
Alexandre Julliard's avatar
Alexandre Julliard committed
1002 1003 1004 1005 1006 1007 1008 1009
{
	int restag;
	res_t *res;
	cursor_t *cur;
	assert(name != NULL);
	assert(curg != NULL);

	res = new_res();
Alexandre Julliard's avatar
Alexandre Julliard committed
1010
	restag = put_res_header(res, WRC_RT_GROUP_CURSOR, NULL, name, curg->memopt, &(curg->lvc));
Alexandre Julliard's avatar
Alexandre Julliard committed
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 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 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
	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.
			 * However, I cound 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);
		}
	}
	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.
			 * However, I cound 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);
		}
	}
	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	:
 *****************************************************************************
*/
1109
static res_t *cursor2res(cursor_t *cur)
Alexandre Julliard's avatar
Alexandre Julliard committed
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
{
	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
1120
	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
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
	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	:
 *****************************************************************************
*/
1144
static res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
Alexandre Julliard's avatar
Alexandre Julliard committed
1145 1146 1147 1148 1149 1150 1151 1152
{
	int restag;
	res_t *res;
	icon_t *ico;
	assert(name != NULL);
	assert(icog != NULL);

	res = new_res();
Alexandre Julliard's avatar
Alexandre Julliard committed
1153
	restag = put_res_header(res, WRC_RT_GROUP_ICON, NULL, name, icog->memopt, &(icog->lvc));
Alexandre Julliard's avatar
Alexandre Julliard committed
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 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
	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	:
 *****************************************************************************
*/
1212
static res_t *icon2res(icon_t *ico)
Alexandre Julliard's avatar
Alexandre Julliard committed
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
{
	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
1223
	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
1224 1225 1226 1227 1228 1229 1230 1231 1232
	put_raw_data(res, ico->data, 0);

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

	return res;
}

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
/*
 *****************************************************************************
 * 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
1267 1268 1269 1270 1271 1272 1273 1274 1275
/*
 *****************************************************************************
 * 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	:
1276
 * Remarks	: The endian of the bitmap structures have been converted
1277
 * 		  by the loader.
Alexandre Julliard's avatar
Alexandre Julliard committed
1278 1279
 *****************************************************************************
*/
1280
static res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
Alexandre Julliard's avatar
Alexandre Julliard committed
1281 1282 1283 1284 1285 1286 1287
{
	int restag;
	res_t *res;
	assert(name != NULL);
	assert(bmp != NULL);

	res = new_res();
1288
	restag = put_res_header(res, WRC_RT_BITMAP, NULL, name, bmp->memopt, &(bmp->data->lvc));
Alexandre Julliard's avatar
Alexandre Julliard committed
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 1316
	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	:
1317
 * Remarks	: The data has been prepared just after parsing.
Alexandre Julliard's avatar
Alexandre Julliard committed
1318 1319
 *****************************************************************************
*/
1320
static res_t *font2res(name_id_t *name, font_t *fnt)
Alexandre Julliard's avatar
Alexandre Julliard committed
1321
{
1322 1323
	int restag;
	res_t *res;
Alexandre Julliard's avatar
Alexandre Julliard committed
1324 1325
	assert(name != NULL);
	assert(fnt != NULL);
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363

	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
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
}

/*
 *****************************************************************************
 * 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	:
 *****************************************************************************
*/
1378
static res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
Alexandre Julliard's avatar
Alexandre Julliard committed
1379 1380 1381 1382 1383 1384 1385
{
	int restag;
	res_t *res;
	assert(name != NULL);
	assert(rdt != NULL);

	res = new_res();
1386
	restag = put_res_header(res, WRC_RT_RCDATA, NULL, name, rdt->memopt, &(rdt->data->lvc));
Alexandre Julliard's avatar
Alexandre Julliard committed
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
	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	:
1404 1405
 * Remarks	: The data has been converted to the appropriate endian
 *		  after is was parsed.
Alexandre Julliard's avatar
Alexandre Julliard committed
1406 1407
 *****************************************************************************
*/
1408
static res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
Alexandre Julliard's avatar
Alexandre Julliard committed
1409
{
1410 1411
	int restag;
	res_t *res;
Alexandre Julliard's avatar
Alexandre Julliard committed
1412 1413
	assert(name != NULL);
	assert(msg != NULL);
1414 1415 1416 1417 1418 1419 1420 1421 1422

	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
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
}

/*
 *****************************************************************************
 * Function	: stringtable2res
 * Syntax	: res_t *stringtable2res(stringtable_t *stt)
 * Input	:
 *	stt	- The stringtable descriptor
 * Output	: New .res format structure
 * Description	:
 * Remarks	:
 *****************************************************************************
*/
1436
static res_t *stringtable2res(stringtable_t *stt)
Alexandre Julliard's avatar
Alexandre Julliard committed
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
{
	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)
		{
			warning("Empty internal stringtable");
			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++)
		{
1459
			if(stt->entries[i].str && stt->entries[i].str->size)
Alexandre Julliard's avatar
Alexandre Julliard committed
1460
			{
1461 1462
				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
1463 1464 1465
			}
			else
			{
1466
				if (win32)
Alexandre Julliard's avatar
Alexandre Julliard committed
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
					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	:
 *****************************************************************************
*/
1493
static res_t *user2res(name_id_t *name, user_t *usr)
Alexandre Julliard's avatar
Alexandre Julliard committed
1494 1495 1496 1497 1498 1499 1500
{
	int restag;
	res_t *res;
	assert(name != NULL);
	assert(usr != NULL);

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

	/* Set blocksize */
1593
	set_word(res, blksizetag, (WORD)(res->size - blksizetag));
Alexandre Julliard's avatar
Alexandre Julliard committed
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
}

/*
 *****************************************************************************
 * 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	:
 *****************************************************************************
*/
1608
static res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
Alexandre Julliard's avatar
Alexandre Julliard committed
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
{
	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;
	vsvi.str.cstr = "VS_VERSION_INFO";
	vsvi.size = 15; /* Excl. termination */

	res = new_res();
1626
	restag = put_res_header(res, WRC_RT_VERSION, NULL, name, ver->memopt, &(ver->lvc));
Alexandre Julliard's avatar
Alexandre Julliard committed
1627 1628 1629 1630 1631 1632
	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 ? */
1633 1634
	put_string(res, &vsvi, win32 ? str_unicode : str_char,
                   TRUE, win32 ? ver->lvc.language : NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
	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 */
1652
	set_word(res, valsizetag, (WORD)(res->size - tag));
Alexandre Julliard's avatar
Alexandre Julliard committed
1653 1654
	/* Descend into the blocks */
	for(blk = ver->blocks; blk; blk = blk->next)
1655
		versionblock2res(res, blk, 0, win32 ? ver->lvc.language : NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
1656
	/* Set root block's size */
1657
	set_word(res, rootblocksizetag, (WORD)(res->size - rootblocksizetag));
Alexandre Julliard's avatar
Alexandre Julliard committed
1658 1659 1660 1661 1662 1663 1664 1665

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

	return res;
}

1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
/*
 *****************************************************************************
 * Function	: toolbaritem2res
 * Syntax	: void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
 * Input	:
 * Output	: nop
 * Description	:
 * Remarks	: Self recursive
 *****************************************************************************
*/
1676
static void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
{
	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	:
 *****************************************************************************
*/
1700
static res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
{
	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?) */
1715
		put_word(res, toolbar->nitems);
1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731
		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;
}

1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
/*
 *****************************************************************************
 * 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	:
 *****************************************************************************
*/
1744
static res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
1745 1746 1747 1748 1749 1750 1751
{
	int restag;
	res_t *res;
	assert(name != NULL);
	assert(dit != NULL);

	res = new_res();
1752
	restag = put_res_header(res, WRC_RT_DLGINIT, NULL, name, dit->memopt, &(dit->data->lvc));
1753 1754 1755 1756 1757 1758 1759 1760
	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
1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
/*
 *****************************************************************************
 * Function	: prep_nid_for_label
 * Syntax	: char *prep_nid_for_label(name_id_t *nid)
 * Input	:
 * Output	:
 * Description	: Converts a resource name into the first 32 (or less)
 *		  characters of the name with conversions.
 * Remarks	:
 *****************************************************************************
*/
#define MAXNAMELEN	32
char *prep_nid_for_label(name_id_t *nid)
{
	static char buf[MAXNAMELEN+1];

	assert(nid != NULL);

	if(nid->type == name_str && nid->name.s_name->type == str_unicode)
	{
		short *sptr;
		int i;
		sptr = nid->name.s_name->str.wstr;
		buf[0] = '\0';
		for(i = 0; *sptr && i < MAXNAMELEN; i++)
		{
1787
			if((unsigned)*sptr < 0x80 && isprint(*sptr & 0xff))
Alexandre Julliard's avatar
Alexandre Julliard committed
1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
				buf[i] = *sptr++;
			else
				warning("Resourcename (str_unicode) contain unprintable characters or invalid translation, ignored");
		}
		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++)
		{
1802
			if((unsigned)*cptr < 0x80 && isprint(*cptr & 0xff))
Alexandre Julliard's avatar
Alexandre Julliard committed
1803 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 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
				buf[i] = *cptr++;
			else
				warning("Resourcename (str_char) contain unprintable characters, ignored");
		}
		buf[i] = '\0';
	}
	else if(nid->type == name_ord)
	{
		sprintf(buf, "%u", nid->name.i_name);
	}
	else
	{
		internal_error(__FILE__, __LINE__, "Resource name_id with invalid type %d", nid->type);
	}
	return buf;
}
#undef MAXNAMELEN

/*
 *****************************************************************************
 * Function	: make_c_name
 * Syntax	: char *make_c_name(char *base, name_id_t *nid, language_t *lan)
 * Input	:
 * Output	:
 * Description	: Converts a resource name into a valid c-identifier in the
 *		  form "_base_nid".
 * Remarks	:
 *****************************************************************************
*/
char *make_c_name(char *base, name_id_t *nid, language_t *lan)
{
	int nlen;
	char *buf;
	char *ret;
	char lanbuf[6];

	sprintf(lanbuf, "%d", lan ? MAKELANGID(lan->id, lan->sub) : 0);
	buf = prep_nid_for_label(nid);
	nlen = strlen(buf) + strlen(lanbuf);
	nlen += strlen(base) + 4; /* three time '_' and '\0' */
	ret = (char *)xmalloc(nlen);
	strcpy(ret, "_");
	strcat(ret, base);
	strcat(ret, "_");
	strcat(ret, buf);
	strcat(ret, "_");
	strcat(ret, lanbuf);
	return ret;
}

/*
 *****************************************************************************
 * Function	: get_c_typename
 * Syntax	: char *get_c_typename(enum res_e type)
 * Input	:
 * Output	:
 * Description	: Convert resource enum to char string to be used in c-name
 *		  creation.
 * Remarks	:
 *****************************************************************************
*/
char *get_c_typename(enum res_e type)
{
	switch(type)
	{
	case res_acc:	return "Acc";
1869 1870
	case res_anicur:return "AniCur";
	case res_aniico:return "AniIco";
Alexandre Julliard's avatar
Alexandre Julliard committed
1871 1872 1873 1874 1875 1876
	case res_bmp:	return "Bmp";
	case res_cur:	return "Cur";
	case res_curg:	return "CurGrp";
	case res_dlg:
	case res_dlgex:	return "Dlg";
	case res_fnt:	return "Fnt";
1877
	case res_fntdir:return "FntDir";
Alexandre Julliard's avatar
Alexandre Julliard committed
1878 1879 1880 1881 1882 1883 1884 1885 1886
	case res_ico:	return "Ico";
	case res_icog:	return "IcoGrp";
	case res_men:
	case res_menex:	return "Men";
	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";
1887
	case res_toolbar:	return "TlBr";
1888
	case res_dlginit: return "DlgInit";
Alexandre Julliard's avatar
Alexandre Julliard committed
1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937
	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_dlgex:
			if(!top->binres)
				top->binres = dialogex2res(top->name, top->res.dlgex);
			break;
		case res_fnt:
			if(!top->binres)
				top->binres = font2res(top->name, top->res.fnt);
			break;
1938 1939 1940 1941
		case res_fntdir:
			if(!top->binres)
				top->binres = fontdir2res(top->name, top->res.fnd);
			break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977
		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;
		case res_menex:
			if(!top->binres)
				top->binres = menuex2res(top->name, top->res.menex);
			break;
		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;
1978 1979 1980 1981
		case res_toolbar:
			if(!top->binres)
				top->binres = toolbar2res(top->name, top->res.tbt);
			break;
1982 1983 1984 1985
		case res_dlginit:
			if(!top->binres)
			    top->binres = dlginit2res(top->name, top->res.dlgi);
			break;
1986 1987 1988 1989 1990
		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
1991 1992 1993 1994 1995 1996 1997
		default:
			internal_error(__FILE__, __LINE__, "Unknown resource type encountered %d in binary res generation", top->type);
		}
		top->c_name = make_c_name(get_c_typename(top->type), top->name, top->lan);
		top = top->next;
	}
}