genres.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  1. /*
  2. * Generate .res format from a resource-tree
  3. *
  4. * Copyright 1998 Bertho A. Stultiens
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. *
  20. * History:
  21. * 05-May-2000 BS - Added code to support endian conversions. The
  22. * extra functions also aid unaligned access, but
  23. * this is not yet implemented.
  24. * 25-May-1998 BS - Added simple unicode -> char conversion for resource
  25. * names in .s and .h files.
  26. */
  27. #include "config.h"
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdarg.h>
  32. #include <assert.h>
  33. #include <ctype.h>
  34. #include "wrc.h"
  35. #include "genres.h"
  36. #include "utils.h"
  37. #include "windef.h"
  38. #include "winbase.h"
  39. #include "wingdi.h"
  40. #include "winuser.h"
  41. res_t *new_res(void)
  42. {
  43. res_t *r;
  44. r = xmalloc(sizeof(res_t));
  45. r->allocsize = RES_BLOCKSIZE;
  46. r->size = 0;
  47. r->dataidx = 0;
  48. r->data = xmalloc(RES_BLOCKSIZE);
  49. return r;
  50. }
  51. res_t *grow_res(res_t *r, unsigned int add)
  52. {
  53. r->allocsize += add;
  54. r->data = xrealloc(r->data, r->allocsize);
  55. return r;
  56. }
  57. /*
  58. *****************************************************************************
  59. * Function : put_byte
  60. * put_word
  61. * put_dword
  62. * Syntax : void put_byte(res_t *res, unsigned c)
  63. * void put_word(res_t *res, unsigned w)
  64. * void put_dword(res_t *res, unsigned d)
  65. * Input :
  66. * res - Binary resource to put the data in
  67. * c, w, d - Data to put
  68. * Output : nop
  69. * Description : Put primitives that put an item in the binary resource.
  70. * The data array grows automatically.
  71. * Remarks :
  72. *****************************************************************************
  73. */
  74. void put_byte(res_t *res, unsigned c)
  75. {
  76. if(res->allocsize - res->size < sizeof(char))
  77. grow_res(res, RES_BLOCKSIZE);
  78. res->data[res->size] = (char)c;
  79. res->size += sizeof(char);
  80. }
  81. void put_word(res_t *res, unsigned w)
  82. {
  83. if(res->allocsize - res->size < sizeof(WORD))
  84. grow_res(res, RES_BLOCKSIZE);
  85. switch(byteorder)
  86. {
  87. #ifdef WORDS_BIGENDIAN
  88. default:
  89. #endif
  90. case WRC_BO_BIG:
  91. res->data[res->size+0] = HIBYTE(w);
  92. res->data[res->size+1] = LOBYTE(w);
  93. break;
  94. #ifndef WORDS_BIGENDIAN
  95. default:
  96. #endif
  97. case WRC_BO_LITTLE:
  98. res->data[res->size+1] = HIBYTE(w);
  99. res->data[res->size+0] = LOBYTE(w);
  100. break;
  101. }
  102. res->size += sizeof(WORD);
  103. }
  104. void put_dword(res_t *res, unsigned d)
  105. {
  106. if(res->allocsize - res->size < sizeof(DWORD))
  107. grow_res(res, RES_BLOCKSIZE);
  108. switch(byteorder)
  109. {
  110. #ifdef WORDS_BIGENDIAN
  111. default:
  112. #endif
  113. case WRC_BO_BIG:
  114. res->data[res->size+0] = HIBYTE(HIWORD(d));
  115. res->data[res->size+1] = LOBYTE(HIWORD(d));
  116. res->data[res->size+2] = HIBYTE(LOWORD(d));
  117. res->data[res->size+3] = LOBYTE(LOWORD(d));
  118. break;
  119. #ifndef WORDS_BIGENDIAN
  120. default:
  121. #endif
  122. case WRC_BO_LITTLE:
  123. res->data[res->size+3] = HIBYTE(HIWORD(d));
  124. res->data[res->size+2] = LOBYTE(HIWORD(d));
  125. res->data[res->size+1] = HIBYTE(LOWORD(d));
  126. res->data[res->size+0] = LOBYTE(LOWORD(d));
  127. break;
  128. }
  129. res->size += sizeof(DWORD);
  130. }
  131. static void put_pad(res_t *res)
  132. {
  133. while(res->size & 0x3)
  134. put_byte(res, 0);
  135. }
  136. /*
  137. *****************************************************************************
  138. * Function : set_word
  139. * set_dword
  140. * Syntax : void set_word(res_t *res, int ofs, unsigned w)
  141. * void set_dword(res_t *res, int ofs, unsigned d)
  142. * Input :
  143. * res - Binary resource to put the data in
  144. * ofs - Byte offset in data-array
  145. * w, d - Data to put
  146. * Output : nop
  147. * Description : Set the value of a binary resource data array to a
  148. * specific value.
  149. * Remarks :
  150. *****************************************************************************
  151. */
  152. static void set_word(res_t *res, int ofs, unsigned w)
  153. {
  154. switch(byteorder)
  155. {
  156. #ifdef WORDS_BIGENDIAN
  157. default:
  158. #endif
  159. case WRC_BO_BIG:
  160. res->data[ofs+0] = HIBYTE(w);
  161. res->data[ofs+1] = LOBYTE(w);
  162. break;
  163. #ifndef WORDS_BIGENDIAN
  164. default:
  165. #endif
  166. case WRC_BO_LITTLE:
  167. res->data[ofs+1] = HIBYTE(w);
  168. res->data[ofs+0] = LOBYTE(w);
  169. break;
  170. }
  171. }
  172. static void set_dword(res_t *res, int ofs, unsigned d)
  173. {
  174. switch(byteorder)
  175. {
  176. #ifdef WORDS_BIGENDIAN
  177. default:
  178. #endif
  179. case WRC_BO_BIG:
  180. res->data[ofs+0] = HIBYTE(HIWORD(d));
  181. res->data[ofs+1] = LOBYTE(HIWORD(d));
  182. res->data[ofs+2] = HIBYTE(LOWORD(d));
  183. res->data[ofs+3] = LOBYTE(LOWORD(d));
  184. break;
  185. #ifndef WORDS_BIGENDIAN
  186. default:
  187. #endif
  188. case WRC_BO_LITTLE:
  189. res->data[ofs+3] = HIBYTE(HIWORD(d));
  190. res->data[ofs+2] = LOBYTE(HIWORD(d));
  191. res->data[ofs+1] = HIBYTE(LOWORD(d));
  192. res->data[ofs+0] = LOBYTE(LOWORD(d));
  193. break;
  194. }
  195. }
  196. /*
  197. *****************************************************************************
  198. * Function : get_dword
  199. * Input :
  200. * res - Binary resource to put the data in
  201. * ofs - Byte offset in data-array
  202. * Output : The data in native endian
  203. * Description : Get the value of a binary resource data array in native
  204. * endian.
  205. * Remarks :
  206. *****************************************************************************
  207. */
  208. static DWORD get_dword(res_t *res, int ofs)
  209. {
  210. switch(byteorder)
  211. {
  212. #ifdef WORDS_BIGENDIAN
  213. default:
  214. #endif
  215. case WRC_BO_BIG:
  216. return (res->data[ofs+0] << 24)
  217. | (res->data[ofs+1] << 16)
  218. | (res->data[ofs+2] << 8)
  219. | res->data[ofs+3];
  220. #ifndef WORDS_BIGENDIAN
  221. default:
  222. #endif
  223. case WRC_BO_LITTLE:
  224. return (res->data[ofs+3] << 24)
  225. | (res->data[ofs+2] << 16)
  226. | (res->data[ofs+1] << 8)
  227. | res->data[ofs+0];
  228. }
  229. }
  230. static res_t *end_res(res_t *res, int ofs)
  231. {
  232. set_dword( res, ofs, res->size - get_dword( res, ofs ));
  233. if (win32) put_pad( res );
  234. return res;
  235. }
  236. /*
  237. *****************************************************************************
  238. * Function : string_to_upper
  239. * Syntax : void string_to_upper(string_t *str)
  240. * Input :
  241. * Output :
  242. * Description :
  243. * Remarks : FIXME: codepages...
  244. *****************************************************************************
  245. */
  246. static void string_to_upper(string_t *str)
  247. {
  248. int i;
  249. if(str->type == str_char)
  250. {
  251. for (i = 0; i < str->size; i++)
  252. if (str->str.cstr[i] >= 'a' && str->str.cstr[i] <= 'z') str->str.cstr[i] -= 32;
  253. }
  254. else if(str->type == str_unicode)
  255. {
  256. for (i = 0; i < str->size; i++)
  257. if (str->str.wstr[i] >= 'a' && str->str.wstr[i] <= 'z') str->str.wstr[i] -= 32;
  258. }
  259. else
  260. {
  261. internal_error(__FILE__, __LINE__, "Invalid string type %d\n", str->type);
  262. }
  263. }
  264. static int parse_accel_string( const string_t *key, int flags )
  265. {
  266. int keycode;
  267. if(key->type == str_char)
  268. {
  269. if (key->str.cstr[0] == '#') return 0; /* ignore message contexts */
  270. if((flags & WRC_AF_VIRTKEY) &&
  271. !((key->str.cstr[0] >= 'A' && key->str.cstr[0] <= 'Z') ||
  272. (key->str.cstr[0] >= '0' && key->str.cstr[0] <= '9')))
  273. {
  274. print_location( &key->loc );
  275. error("VIRTKEY code is not equal to ascii value\n");
  276. }
  277. if(key->str.cstr[0] == '^' && (flags & WRC_AF_CONTROL) != 0)
  278. {
  279. print_location( &key->loc );
  280. error("Cannot use both '^' and CONTROL modifier\n");
  281. }
  282. else if(key->str.cstr[0] == '^')
  283. {
  284. if (key->str.cstr[1] >= 'a' && key->str.cstr[1] <= 'z')
  285. keycode = key->str.cstr[1] - 'a' + 1;
  286. else if (key->str.cstr[1] >= 'A' && key->str.cstr[1] <= 'Z')
  287. keycode = key->str.cstr[1] - 'A' + 1;
  288. else
  289. {
  290. print_location( &key->loc );
  291. error("Control-code out of range\n");
  292. }
  293. }
  294. else
  295. keycode = key->str.cstr[0];
  296. }
  297. else
  298. {
  299. if (key->str.wstr[0] == '#') return 0; /* ignore message contexts */
  300. if((flags & WRC_AF_VIRTKEY) &&
  301. !((key->str.wstr[0] >= 'A' && key->str.wstr[0] <= 'Z') ||
  302. (key->str.wstr[0] >= '0' && key->str.wstr[0] <= '9')))
  303. {
  304. print_location( &key->loc );
  305. error("VIRTKEY code is not equal to ascii value\n");
  306. }
  307. if(key->str.wstr[0] == '^' && (flags & WRC_AF_CONTROL) != 0)
  308. {
  309. print_location( &key->loc );
  310. error("Cannot use both '^' and CONTROL modifier\n");
  311. }
  312. else if(key->str.wstr[0] == '^')
  313. {
  314. if (key->str.wstr[1] >= 'a' && key->str.wstr[1] <= 'z')
  315. keycode = key->str.wstr[1] - 'a' + 1;
  316. else if (key->str.wstr[1] >= 'A' && key->str.wstr[1] <= 'Z')
  317. keycode = key->str.wstr[1] - 'A' + 1;
  318. else
  319. {
  320. print_location( &key->loc );
  321. error("Control-code out of range\n");
  322. }
  323. }
  324. else
  325. keycode = key->str.wstr[0];
  326. }
  327. return keycode;
  328. }
  329. /*
  330. *****************************************************************************
  331. * Function : put_string
  332. * Input :
  333. * res - Binary resource to put the data in
  334. * str - String to put
  335. * isterm - The string is '\0' terminated (disregard the string's
  336. * size member)
  337. *****************************************************************************
  338. */
  339. static void put_string(res_t *res, const string_t *str, int isterm, const language_t *lang)
  340. {
  341. int cnt, codepage;
  342. if (win32)
  343. {
  344. string_t *newstr;
  345. if (lang) codepage = get_language_codepage( lang->id, lang->sub );
  346. else codepage = get_language_codepage( 0, 0 );
  347. assert( codepage != -1 );
  348. newstr = convert_string_unicode( str, codepage );
  349. if (str->type == str_char && check_valid_utf8( str, codepage ))
  350. {
  351. print_location( &str->loc );
  352. warning( "string \"%s\" seems to be UTF-8 but codepage %u is in use, maybe use --utf8?\n",
  353. str->str.cstr, codepage );
  354. }
  355. if (!isterm) put_word(res, newstr->size);
  356. for(cnt = 0; cnt < newstr->size; cnt++)
  357. {
  358. WCHAR c = newstr->str.wstr[cnt];
  359. if (isterm && !c) break;
  360. put_word(res, c);
  361. }
  362. if (isterm) put_word(res, 0);
  363. free_string(newstr);
  364. }
  365. else
  366. {
  367. if (str->type == str_unicode)
  368. internal_error(__FILE__, __LINE__, "Unicode string %s in 16-bit\n",
  369. convert_string_utf8( str, 0 ));
  370. if (!isterm) put_byte(res, str->size);
  371. for(cnt = 0; cnt < str->size; cnt++)
  372. {
  373. char c = str->str.cstr[cnt];
  374. if (isterm && !c) break;
  375. put_byte(res, c);
  376. }
  377. if (isterm) put_byte(res, 0);
  378. }
  379. }
  380. /*
  381. *****************************************************************************
  382. * Function : put_name_id
  383. * Syntax : void put_name_id(res_t *res, name_id_t *nid, int upcase, const language_t *lang)
  384. * Input :
  385. * Output :
  386. * Description :
  387. * Remarks :
  388. *****************************************************************************
  389. */
  390. static void put_name_id(res_t *res, name_id_t *nid, int upcase, const language_t *lang)
  391. {
  392. if(nid->type == name_ord)
  393. {
  394. if(win32)
  395. put_word(res, 0xffff);
  396. else
  397. put_byte(res, 0xff);
  398. put_word(res, (WORD)nid->name.i_name);
  399. }
  400. else if(nid->type == name_str)
  401. {
  402. if(upcase)
  403. string_to_upper(nid->name.s_name);
  404. put_string(res, nid->name.s_name, TRUE, lang);
  405. }
  406. else
  407. {
  408. internal_error(__FILE__, __LINE__, "Invalid name_id type %d\n", nid->type);
  409. }
  410. }
  411. /*
  412. *****************************************************************************
  413. * Function : put_lvc
  414. * Syntax : void put_lvc(res_t *res, lvc_t *lvc)
  415. * Input :
  416. * Output :
  417. * Description :
  418. * Remarks :
  419. *****************************************************************************
  420. */
  421. static void put_lvc(res_t *res, lvc_t *lvc)
  422. {
  423. if(lvc && lvc->language)
  424. put_word(res, MAKELANGID(lvc->language->id, lvc->language->sub));
  425. else
  426. put_word(res, 0); /* Neutral */
  427. if(lvc && lvc->version)
  428. put_dword(res, *(lvc->version));
  429. else
  430. put_dword(res, 0);
  431. if(lvc && lvc->characts)
  432. put_dword(res, *(lvc->characts));
  433. else
  434. put_dword(res, 0);
  435. }
  436. /*
  437. *****************************************************************************
  438. * Function : put_raw_data
  439. * Syntax : void put_raw_data(res_t *res, raw_data_t *raw, int offset)
  440. * Input :
  441. * Output :
  442. * Description :
  443. * Remarks :
  444. *****************************************************************************
  445. */
  446. static void put_raw_data(res_t *res, raw_data_t *raw, int offset)
  447. {
  448. unsigned int wsize = raw->size - offset;
  449. if(res->allocsize - res->size < wsize)
  450. grow_res(res, wsize);
  451. memcpy(&(res->data[res->size]), raw->data + offset, wsize);
  452. res->size += wsize;
  453. }
  454. /*
  455. *****************************************************************************
  456. * Function : put_res_header
  457. * Syntax : input_res_header(res_t *res, int type, name_id_t *ntype,
  458. * name_id_t *name, DWORD memopt, lvc_t *lvc)
  459. *
  460. * Input :
  461. * res - Binary resource descriptor to write to
  462. * type - Resource identifier (if ntype == NULL)
  463. * ntype - Name id of type
  464. * name - Resource's name
  465. * memopt - Resource's memory options to write
  466. * lvc - Language, version and characteristics (win32 only)
  467. * Output : An index to the resource size field. The resource size field
  468. * contains the header size upon exit.
  469. * Description :
  470. * Remarks :
  471. *****************************************************************************
  472. */
  473. static int put_res_header(res_t *res, int type, name_id_t *ntype, name_id_t *name,
  474. DWORD memopt, lvc_t *lvc)
  475. {
  476. if(win32)
  477. {
  478. put_dword(res, 0); /* We will overwrite these later */
  479. put_dword(res, 0);
  480. if(!ntype)
  481. {
  482. put_word(res, 0xffff); /* ResType */
  483. put_word(res, type);
  484. }
  485. else
  486. put_name_id(res, ntype, TRUE, lvc->language);
  487. put_name_id(res, name, TRUE, lvc->language); /* ResName */
  488. put_pad(res);
  489. put_dword(res, 0); /* DataVersion */
  490. put_word(res, memopt); /* Memory options */
  491. put_lvc(res, lvc); /* Language, version and characts */
  492. set_dword(res, 0*sizeof(DWORD), res->size); /* Set preliminary resource */
  493. set_dword(res, 1*sizeof(DWORD), res->size); /* Set HeaderSize */
  494. res->dataidx = res->size;
  495. return 0;
  496. }
  497. else /* win16 */
  498. {
  499. int tag;
  500. if(!ntype)
  501. {
  502. put_byte(res, 0xff); /* ResType */
  503. put_word(res, type);
  504. }
  505. else
  506. put_name_id(res, ntype, TRUE, NULL);
  507. put_name_id(res, name, TRUE, NULL); /* ResName */
  508. put_word(res, memopt); /* Memory options */
  509. tag = res->size;
  510. put_dword(res, 0); /* ResSize overwritten later*/
  511. set_dword(res, tag, res->size);
  512. res->dataidx = res->size;
  513. return tag;
  514. }
  515. }
  516. /*
  517. *****************************************************************************
  518. * Function : accelerator2res
  519. * Syntax : res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
  520. * Input :
  521. * name - Name/ordinal of the resource
  522. * acc - The accelerator descriptor
  523. * Output : New .res format structure
  524. * Description :
  525. * Remarks :
  526. *****************************************************************************
  527. */
  528. static res_t *accelerator2res(name_id_t *name, accelerator_t *acc)
  529. {
  530. int restag;
  531. res_t *res;
  532. event_t *ev;
  533. assert(name != NULL);
  534. assert(acc != NULL);
  535. ev = acc->events;
  536. res = new_res();
  537. if(win32)
  538. {
  539. restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, &(acc->lvc));
  540. while(ev)
  541. {
  542. int key = ev->key;
  543. if (ev->str) key = parse_accel_string( ev->str, ev->flags );
  544. put_word(res, ev->flags | (ev->next ? 0 : 0x80));
  545. put_word(res, key);
  546. put_word(res, ev->id);
  547. put_word(res, 0); /* Padding */
  548. ev = ev->next;
  549. }
  550. put_pad(res);
  551. }
  552. else /* win16 */
  553. {
  554. restag = put_res_header(res, WRC_RT_ACCELERATOR, NULL, name, acc->memopt, NULL);
  555. while(ev)
  556. {
  557. int key = ev->key;
  558. if (ev->str) key = parse_accel_string( ev->str, ev->flags );
  559. put_byte(res, ev->flags | (ev->next ? 0 : 0x80));
  560. put_word(res, key);
  561. put_word(res, ev->id);
  562. ev = ev->next;
  563. }
  564. }
  565. return end_res(res, restag);
  566. }
  567. /*
  568. *****************************************************************************
  569. * Function : dialog2res
  570. * Syntax : res_t *dialog2res(name_id_t *name, dialog_t *dlg)
  571. * Input :
  572. * name - Name/ordinal of the resource
  573. * dlg - The dialog descriptor
  574. * Output : New .res format structure
  575. * Description :
  576. * Remarks :
  577. *****************************************************************************
  578. */
  579. static res_t *dialog2res(name_id_t *name, dialog_t *dlg)
  580. {
  581. int restag;
  582. res_t *res;
  583. control_t *ctrl;
  584. int tag_nctrl;
  585. int nctrl = 0;
  586. assert(name != NULL);
  587. assert(dlg != NULL);
  588. ctrl = dlg->controls;
  589. res = new_res();
  590. if(win32)
  591. {
  592. restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, &(dlg->lvc));
  593. if (dlg->is_ex)
  594. {
  595. /* FIXME: MS doc says that the first word must contain 0xffff
  596. * and the second 0x0001 to signal a DLGTEMPLATEEX. Borland's
  597. * compiler reverses the two words.
  598. * I don't know which one to choose, but I write it as Mr. B
  599. * writes it.
  600. */
  601. put_word(res, 1); /* Signature */
  602. put_word(res, 0xffff); /* DlgVer */
  603. put_dword(res, dlg->gothelpid ? dlg->helpid : 0);
  604. put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
  605. put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
  606. }
  607. else
  608. {
  609. put_dword(res, dlg->style->or_mask);
  610. put_dword(res, dlg->gotexstyle ? dlg->exstyle->or_mask : 0);
  611. }
  612. tag_nctrl = res->size;
  613. put_word(res, 0); /* Number of controls */
  614. put_word(res, dlg->x);
  615. put_word(res, dlg->y);
  616. put_word(res, dlg->width);
  617. put_word(res, dlg->height);
  618. if(dlg->menu)
  619. put_name_id(res, dlg->menu, FALSE, dlg->lvc.language);
  620. else
  621. put_word(res, 0);
  622. if(dlg->dlgclass)
  623. put_name_id(res, dlg->dlgclass, FALSE, dlg->lvc.language);
  624. else
  625. put_word(res, 0);
  626. if(dlg->title)
  627. put_string(res, dlg->title, TRUE, dlg->lvc.language);
  628. else
  629. put_word(res, 0);
  630. if(dlg->font)
  631. {
  632. put_word(res, dlg->font->size);
  633. if (dlg->is_ex)
  634. {
  635. put_word(res, dlg->font->weight);
  636. /* FIXME: ? TRUE should be sufficient to say that it's
  637. * italic, but Borland's compiler says it's 0x0101.
  638. * I just copy it here, and hope for the best.
  639. */
  640. put_word(res, dlg->font->italic ? 0x0101 : 0);
  641. }
  642. put_string(res, dlg->font->name, TRUE, dlg->lvc.language);
  643. }
  644. else if (dlg->style->or_mask & DS_SETFONT) put_word( res, 0x7fff );
  645. put_pad(res);
  646. while(ctrl)
  647. {
  648. if (dlg->is_ex)
  649. {
  650. put_dword(res, ctrl->gothelpid ? ctrl->helpid : 0);
  651. put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
  652. /* FIXME: what is default control style? */
  653. put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask : WS_CHILD | WS_VISIBLE);
  654. }
  655. else
  656. {
  657. /* FIXME: what is default control style? */
  658. put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
  659. put_dword(res, ctrl->gotexstyle ? ctrl->exstyle->or_mask : 0);
  660. }
  661. put_word(res, ctrl->x);
  662. put_word(res, ctrl->y);
  663. put_word(res, ctrl->width);
  664. put_word(res, ctrl->height);
  665. if (dlg->is_ex)
  666. put_dword(res, ctrl->id);
  667. else
  668. put_word(res, ctrl->id);
  669. if(ctrl->ctlclass)
  670. put_name_id(res, ctrl->ctlclass, FALSE, dlg->lvc.language);
  671. else
  672. internal_error(__FILE__, __LINE__, "Control has no control-class\n");
  673. if(ctrl->title)
  674. put_name_id(res, ctrl->title, FALSE, dlg->lvc.language);
  675. else
  676. put_word(res, 0);
  677. if(ctrl->extra)
  678. {
  679. put_word(res, ctrl->extra->size);
  680. put_raw_data(res, ctrl->extra, 0);
  681. }
  682. else
  683. put_word(res, 0);
  684. if(ctrl->next)
  685. put_pad(res);
  686. nctrl++;
  687. ctrl = ctrl->next;
  688. }
  689. /* Set number of controls */
  690. set_word(res, tag_nctrl, (WORD)nctrl);
  691. }
  692. else /* win16 */
  693. {
  694. restag = put_res_header(res, WRC_RT_DIALOG, NULL, name, dlg->memopt, NULL);
  695. put_dword(res, dlg->gotstyle ? dlg->style->or_mask : WS_POPUPWINDOW);
  696. tag_nctrl = res->size;
  697. put_byte(res, 0); /* Number of controls */
  698. put_word(res, dlg->x);
  699. put_word(res, dlg->y);
  700. put_word(res, dlg->width);
  701. put_word(res, dlg->height);
  702. if(dlg->menu)
  703. put_name_id(res, dlg->menu, TRUE, NULL);
  704. else
  705. put_byte(res, 0);
  706. if(dlg->dlgclass)
  707. put_name_id(res, dlg->dlgclass, TRUE, NULL);
  708. else
  709. put_byte(res, 0);
  710. if(dlg->title)
  711. put_string(res, dlg->title, TRUE, NULL);
  712. else
  713. put_byte(res, 0);
  714. if(dlg->font)
  715. {
  716. put_word(res, dlg->font->size);
  717. put_string(res, dlg->font->name, TRUE, NULL);
  718. }
  719. while(ctrl)
  720. {
  721. put_word(res, ctrl->x);
  722. put_word(res, ctrl->y);
  723. put_word(res, ctrl->width);
  724. put_word(res, ctrl->height);
  725. put_word(res, ctrl->id);
  726. put_dword(res, ctrl->gotstyle ? ctrl->style->or_mask: WS_CHILD);
  727. if(ctrl->ctlclass)
  728. {
  729. if(ctrl->ctlclass->type == name_ord
  730. && ctrl->ctlclass->name.i_name >= 0x80
  731. && ctrl->ctlclass->name.i_name <= 0x85)
  732. put_byte(res, ctrl->ctlclass->name.i_name);
  733. else if(ctrl->ctlclass->type == name_str)
  734. put_name_id(res, ctrl->ctlclass, FALSE, NULL);
  735. else
  736. error("Unknown control-class %04x\n", ctrl->ctlclass->name.i_name);
  737. }
  738. else
  739. internal_error(__FILE__, __LINE__, "Control has no control-class\n");
  740. if(ctrl->title)
  741. put_name_id(res, ctrl->title, FALSE, NULL);
  742. else
  743. put_byte(res, 0);
  744. /* FIXME: What is this extra byte doing here? */
  745. put_byte(res, 0);
  746. nctrl++;
  747. ctrl = ctrl->next;
  748. }
  749. /* Set number of controls */
  750. ((char *)res->data)[tag_nctrl] = (char)nctrl;
  751. }
  752. return end_res(res, restag);
  753. }
  754. /*
  755. *****************************************************************************
  756. * Function : menuitem2res
  757. * Syntax : void menuitem2res(res_t *res, menu_item_t *item)
  758. * Input :
  759. * Output :
  760. * Description :
  761. * Remarks : Self recursive
  762. *****************************************************************************
  763. */
  764. static void menuitem2res(res_t *res, menu_item_t *menitem, const language_t *lang)
  765. {
  766. menu_item_t *itm = menitem;
  767. while(itm)
  768. {
  769. put_word(res, itm->state | (itm->popup ? MF_POPUP : 0) | (!itm->next ? MF_END : 0));
  770. if(!itm->popup)
  771. put_word(res, itm->id);
  772. if(itm->name)
  773. put_string(res, itm->name, TRUE, lang);
  774. else
  775. {
  776. if (win32) put_word(res, 0);
  777. else put_byte(res, 0);
  778. }
  779. if(itm->popup)
  780. menuitem2res(res, itm->popup, lang);
  781. itm = itm->next;
  782. }
  783. }
  784. /*
  785. *****************************************************************************
  786. * Function : menuexitem2res
  787. * Syntax : void menuexitem2res(res_t *res, menuex_item_t *item)
  788. * Input :
  789. * Output : nop
  790. * Description :
  791. * Remarks : Self recursive
  792. *****************************************************************************
  793. */
  794. static void menuexitem2res(res_t *res, menu_item_t *menitem, const language_t *lang)
  795. {
  796. menu_item_t *itm = menitem;
  797. assert(win32 != 0);
  798. while(itm)
  799. {
  800. put_dword(res, itm->gottype ? itm->type : 0);
  801. put_dword(res, itm->gotstate ? itm->state : 0);
  802. put_dword(res, itm->gotid ? itm->id : 0);
  803. put_word(res, (itm->popup ? 0x01 : 0) | (!itm->next ? MF_END : 0));
  804. if(itm->name)
  805. put_string(res, itm->name, TRUE, lang);
  806. else
  807. put_word(res, 0);
  808. put_pad(res);
  809. if(itm->popup)
  810. {
  811. put_dword(res, itm->gothelpid ? itm->helpid : 0);
  812. menuexitem2res(res, itm->popup, lang);
  813. }
  814. itm = itm->next;
  815. }
  816. }
  817. /*
  818. *****************************************************************************
  819. * Function : menu2res
  820. * Syntax : res_t *menu2res(name_id_t *name, menu_t *men)
  821. * Input :
  822. * name - Name/ordinal of the resource
  823. * menex - The menuex descriptor
  824. * Output : New .res format structure
  825. * Description :
  826. * Remarks :
  827. *****************************************************************************
  828. */
  829. static res_t *menu2res(name_id_t *name, menu_t *men)
  830. {
  831. int restag;
  832. res_t *res;
  833. assert(name != NULL);
  834. assert(men != NULL);
  835. res = new_res();
  836. restag = put_res_header(res, WRC_RT_MENU, NULL, name, men->memopt, &men->lvc);
  837. if(win32)
  838. {
  839. if (men->is_ex)
  840. {
  841. put_word(res, 1); /* Menuheader: Version */
  842. put_word(res, 4); /* Offset */
  843. put_dword(res, 0); /* HelpId */
  844. put_pad(res);
  845. menuexitem2res(res, men->items, men->lvc.language);
  846. }
  847. else
  848. {
  849. put_dword(res, 0); /* Menuheader: Version and HeaderSize */
  850. menuitem2res(res, men->items, men->lvc.language);
  851. }
  852. }
  853. else /* win16 */
  854. {
  855. put_dword(res, 0); /* Menuheader: Version and HeaderSize */
  856. menuitem2res(res, men->items, NULL);
  857. }
  858. return end_res(res, restag);
  859. }
  860. /*
  861. *****************************************************************************
  862. * Function : cursorgroup2res
  863. * Syntax : res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
  864. * Input :
  865. * name - Name/ordinal of the resource
  866. * curg - The cursor descriptor
  867. * Output : New .res format structure
  868. * Description :
  869. * Remarks :
  870. *****************************************************************************
  871. */
  872. static res_t *cursorgroup2res(name_id_t *name, cursor_group_t *curg)
  873. {
  874. int restag;
  875. res_t *res;
  876. cursor_t *cur;
  877. assert(name != NULL);
  878. assert(curg != NULL);
  879. res = new_res();
  880. restag = put_res_header(res, WRC_RT_GROUP_CURSOR, NULL, name, curg->memopt, &(curg->lvc));
  881. put_word(res, 0); /* Reserved */
  882. /* FIXME: The ResType in the NEWHEADER structure should
  883. * contain 14 according to the MS win32 doc. This is
  884. * not the case with the BRC compiler and I really doubt
  885. * the latter. Putting one here is compliant to win16 spec,
  886. * but who knows the true value?
  887. */
  888. put_word(res, 2); /* ResType */
  889. put_word(res, curg->ncursor);
  890. #if 0
  891. for(cur = curg->cursorlist; cur; cur = cur->next)
  892. #else
  893. cur = curg->cursorlist;
  894. while(cur->next)
  895. cur = cur->next;
  896. for(; cur; cur = cur->prev)
  897. #endif
  898. {
  899. put_word(res, cur->width);
  900. /* FIXME: The height of a cursor is half the size of
  901. * the bitmap's height. BRC puts the height from the
  902. * BITMAPINFOHEADER here instead of the cursorfile's
  903. * height. MS doesn't seem to care...
  904. */
  905. put_word(res, cur->height);
  906. /* FIXME: The next two are reversed in BRC and I don't
  907. * know why. Probably a bug. But, we can safely ignore
  908. * it because win16 does not support color cursors.
  909. * A warning should have been generated by the parser.
  910. */
  911. put_word(res, cur->planes);
  912. put_word(res, cur->bits);
  913. /* FIXME: The +4 is the hotspot in the cursor resource.
  914. * However, I could not find this in the documentation.
  915. * The hotspot bytes must either be included or MS
  916. * doesn't care.
  917. */
  918. put_dword(res, cur->data->size +4);
  919. put_word(res, cur->id);
  920. }
  921. return end_res(res, restag);
  922. }
  923. /*
  924. *****************************************************************************
  925. * Function : cursor2res
  926. * Syntax : res_t *cursor2res(cursor_t *cur)
  927. * Input :
  928. * cur - The cursor descriptor
  929. * Output : New .res format structure
  930. * Description :
  931. * Remarks :
  932. *****************************************************************************
  933. */
  934. static res_t *cursor2res(cursor_t *cur)
  935. {
  936. int restag;
  937. res_t *res;
  938. name_id_t name;
  939. assert(cur != NULL);
  940. res = new_res();
  941. name.type = name_ord;
  942. name.name.i_name = cur->id;
  943. restag = put_res_header(res, WRC_RT_CURSOR, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(cur->lvc));
  944. put_word(res, cur->xhot);
  945. put_word(res, cur->yhot);
  946. put_raw_data(res, cur->data, 0);
  947. return end_res(res, restag);
  948. }
  949. /*
  950. *****************************************************************************
  951. * Function : icongroup2res
  952. * Syntax : res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
  953. * Input :
  954. * name - Name/ordinal of the resource
  955. * icog - The icon group descriptor
  956. * Output : New .res format structure
  957. * Description :
  958. * Remarks :
  959. *****************************************************************************
  960. */
  961. static res_t *icongroup2res(name_id_t *name, icon_group_t *icog)
  962. {
  963. int restag;
  964. res_t *res;
  965. icon_t *ico;
  966. assert(name != NULL);
  967. assert(icog != NULL);
  968. res = new_res();
  969. restag = put_res_header(res, WRC_RT_GROUP_ICON, NULL, name, icog->memopt, &(icog->lvc));
  970. put_word(res, 0); /* Reserved */
  971. /* FIXME: The ResType in the NEWHEADER structure should
  972. * contain 14 according to the MS win32 doc. This is
  973. * not the case with the BRC compiler and I really doubt
  974. * the latter. Putting one here is compliant to win16 spec,
  975. * but who knows the true value?
  976. */
  977. put_word(res, 1); /* ResType */
  978. put_word(res, icog->nicon);
  979. for(ico = icog->iconlist; ico; ico = ico->next)
  980. {
  981. put_byte(res, ico->width);
  982. put_byte(res, ico->height);
  983. put_byte(res, ico->nclr);
  984. put_byte(res, 0); /* Reserved */
  985. put_word(res, ico->planes);
  986. put_word(res, ico->bits);
  987. put_dword(res, ico->data->size);
  988. put_word(res, ico->id);
  989. }
  990. return end_res(res, restag);
  991. }
  992. /*
  993. *****************************************************************************
  994. * Function : icon2res
  995. * Syntax : res_t *icon2res(icon_t *ico)
  996. * Input :
  997. * ico - The icon descriptor
  998. * Output : New .res format structure
  999. * Description :
  1000. * Remarks :
  1001. *****************************************************************************
  1002. */
  1003. static res_t *icon2res(icon_t *ico)
  1004. {
  1005. int restag;
  1006. res_t *res;
  1007. name_id_t name;
  1008. assert(ico != NULL);
  1009. res = new_res();
  1010. name.type = name_ord;
  1011. name.name.i_name = ico->id;
  1012. restag = put_res_header(res, WRC_RT_ICON, NULL, &name, WRC_MO_MOVEABLE | WRC_MO_DISCARDABLE, &(ico->lvc));
  1013. put_raw_data(res, ico->data, 0);
  1014. return end_res(res, restag);
  1015. }
  1016. /*
  1017. *****************************************************************************
  1018. * Function : anicurico2res
  1019. * Syntax : res_t *anicurico2res(name_id_t *name, ani_curico_t *ani)
  1020. * Input :
  1021. * name - Name/ordinal of the resource
  1022. * ani - The animated object descriptor
  1023. * Output : New .res format structure
  1024. * Description :
  1025. * Remarks : The endian of the object's structures have been converted
  1026. * by the loader.
  1027. * There are rumors that win311 could handle animated stuff.
  1028. * That is why they are generated for both win16 and win32
  1029. * compile.
  1030. *****************************************************************************
  1031. */
  1032. static res_t *anicurico2res(name_id_t *name, ani_curico_t *ani, enum res_e type)
  1033. {
  1034. int restag;
  1035. res_t *res;
  1036. assert(name != NULL);
  1037. assert(ani != NULL);
  1038. res = new_res();
  1039. restag = put_res_header(res, type == res_anicur ? WRC_RT_ANICURSOR : WRC_RT_ANIICON,
  1040. NULL, name, ani->memopt, NULL);
  1041. put_raw_data(res, ani->data, 0);
  1042. return end_res(res, restag);
  1043. }
  1044. /*
  1045. *****************************************************************************
  1046. * Function : bitmap2res
  1047. * Syntax : res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
  1048. * Input :
  1049. * name - Name/ordinal of the resource
  1050. * bmp - The bitmap descriptor
  1051. * Output : New .res format structure
  1052. * Description :
  1053. * Remarks : The endian of the bitmap structures have been converted
  1054. * by the loader.
  1055. *****************************************************************************
  1056. */
  1057. static res_t *bitmap2res(name_id_t *name, bitmap_t *bmp)
  1058. {
  1059. int restag;
  1060. res_t *res;
  1061. assert(name != NULL);
  1062. assert(bmp != NULL);
  1063. res = new_res();
  1064. restag = put_res_header(res, WRC_RT_BITMAP, NULL, name, bmp->memopt, &(bmp->data->lvc));
  1065. if(bmp->data->data[0] == 'B'
  1066. && bmp->data->data[1] == 'M'
  1067. && ((BITMAPFILEHEADER *)bmp->data->data)->bfSize == bmp->data->size
  1068. && bmp->data->size >= sizeof(BITMAPFILEHEADER))
  1069. {
  1070. /* The File header is still attached, don't write it */
  1071. put_raw_data(res, bmp->data, sizeof(BITMAPFILEHEADER));
  1072. }
  1073. else
  1074. {
  1075. put_raw_data(res, bmp->data, 0);
  1076. }
  1077. return end_res(res, restag);
  1078. }
  1079. /*
  1080. *****************************************************************************
  1081. * Function : font2res
  1082. * Syntax : res_t *font2res(name_id_t *name, font_t *fnt)
  1083. * Input :
  1084. * name - Name/ordinal of the resource
  1085. * fnt - The font descriptor
  1086. * Output : New .res format structure
  1087. * Description :
  1088. * Remarks : The data has been prepared just after parsing.
  1089. *****************************************************************************
  1090. */
  1091. static res_t *font2res(name_id_t *name, font_t *fnt)
  1092. {
  1093. int restag;
  1094. res_t *res;
  1095. assert(name != NULL);
  1096. assert(fnt != NULL);
  1097. res = new_res();
  1098. restag = put_res_header(res, WRC_RT_FONT, NULL, name, fnt->memopt, &(fnt->data->lvc));
  1099. put_raw_data(res, fnt->data, 0);
  1100. return end_res(res, restag);
  1101. }
  1102. /*
  1103. *****************************************************************************
  1104. * Function : fontdir2res
  1105. * Syntax : res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
  1106. * Input :
  1107. * name - Name/ordinal of the resource
  1108. * fntdir - The fontdir descriptor
  1109. * Output : New .res format structure
  1110. * Description :
  1111. * Remarks : The data has been prepared just after parsing.
  1112. *****************************************************************************
  1113. */
  1114. static res_t *fontdir2res(name_id_t *name, fontdir_t *fnd)
  1115. {
  1116. int restag;
  1117. res_t *res;
  1118. assert(name != NULL);
  1119. assert(fnd != NULL);
  1120. res = new_res();
  1121. restag = put_res_header(res, WRC_RT_FONTDIR, NULL, name, fnd->memopt, &(fnd->data->lvc));
  1122. put_raw_data(res, fnd->data, 0);
  1123. return end_res(res, restag);
  1124. }
  1125. /*
  1126. *****************************************************************************
  1127. * Function : html2res
  1128. * Syntax : res_t *html2res(name_id_t *name, html_t *html)
  1129. * Input :
  1130. * name - Name/ordinal of the resource
  1131. * rdt - The html descriptor
  1132. * Output : New .res format structure
  1133. * Description :
  1134. * Remarks :
  1135. *****************************************************************************
  1136. */
  1137. static res_t *html2res(name_id_t *name, html_t *html)
  1138. {
  1139. int restag;
  1140. res_t *res;
  1141. assert(name != NULL);
  1142. assert(html != NULL);
  1143. res = new_res();
  1144. restag = put_res_header(res, WRC_RT_HTML, NULL, name, html->memopt, &(html->data->lvc));
  1145. put_raw_data(res, html->data, 0);
  1146. return end_res(res, restag);
  1147. }
  1148. /*
  1149. *****************************************************************************
  1150. * Function : rcdata2res
  1151. * Syntax : res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
  1152. * Input :
  1153. * name - Name/ordinal of the resource
  1154. * rdt - The rcdata descriptor
  1155. * Output : New .res format structure
  1156. * Description :
  1157. * Remarks :
  1158. *****************************************************************************
  1159. */
  1160. static res_t *rcdata2res(name_id_t *name, rcdata_t *rdt)
  1161. {
  1162. int restag;
  1163. res_t *res;
  1164. assert(name != NULL);
  1165. assert(rdt != NULL);
  1166. res = new_res();
  1167. restag = put_res_header(res, WRC_RT_RCDATA, NULL, name, rdt->memopt, &(rdt->data->lvc));
  1168. put_raw_data(res, rdt->data, 0);
  1169. return end_res(res, restag);
  1170. }
  1171. /*
  1172. *****************************************************************************
  1173. * Function : messagetable2res
  1174. * Syntax : res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
  1175. * Input :
  1176. * name - Name/ordinal of the resource
  1177. * msg - The messagetable descriptor
  1178. * Output : New .res format structure
  1179. * Description :
  1180. * Remarks : The data has been converted to the appropriate endian
  1181. * after it was parsed.
  1182. *****************************************************************************
  1183. */
  1184. static res_t *messagetable2res(name_id_t *name, messagetable_t *msg)
  1185. {
  1186. int restag;
  1187. res_t *res;
  1188. assert(name != NULL);
  1189. assert(msg != NULL);
  1190. res = new_res();
  1191. restag = put_res_header(res, WRC_RT_MESSAGETABLE, NULL, name, msg->memopt, &(msg->data->lvc));
  1192. put_raw_data(res, msg->data, 0);
  1193. return end_res(res, restag);
  1194. }
  1195. /*
  1196. *****************************************************************************
  1197. * Function : stringtable2res
  1198. * Syntax : res_t *stringtable2res(stringtable_t *stt)
  1199. * Input :
  1200. * stt - The stringtable descriptor
  1201. * Output : New .res format structure
  1202. * Description :
  1203. * Remarks :
  1204. *****************************************************************************
  1205. */
  1206. static res_t *stringtable2res(stringtable_t *stt)
  1207. {
  1208. res_t *res;
  1209. name_id_t name;
  1210. int i;
  1211. int restag;
  1212. DWORD lastsize = 0;
  1213. assert(stt != NULL);
  1214. res = new_res();
  1215. for(; stt; stt = stt->next)
  1216. {
  1217. if(!stt->nentries)
  1218. {
  1219. warning("Empty internal stringtable\n");
  1220. continue;
  1221. }
  1222. name.type = name_ord;
  1223. name.name.i_name = (stt->idbase >> 4) + 1;
  1224. restag = put_res_header(res, WRC_RT_STRING, NULL, &name, stt->memopt, win32 ? &(stt->lvc) : NULL);
  1225. for(i = 0; i < stt->nentries; i++)
  1226. {
  1227. if(stt->entries[i].str && stt->entries[i].str->size)
  1228. {
  1229. put_string(res, stt->entries[i].str, FALSE, stt->lvc.language);
  1230. }
  1231. else
  1232. {
  1233. if (win32)
  1234. put_word(res, 0);
  1235. else
  1236. put_byte(res, 0);
  1237. }
  1238. }
  1239. end_res(res, restag - lastsize);
  1240. lastsize = res->size;
  1241. }
  1242. return res;
  1243. }
  1244. /*
  1245. *****************************************************************************
  1246. * Function : user2res
  1247. * Syntax : res_t *user2res(name_id_t *name, user_t *usr)
  1248. * Input :
  1249. * name - Name/ordinal of the resource
  1250. * usr - The userresource descriptor
  1251. * Output : New .res format structure
  1252. * Description :
  1253. * Remarks :
  1254. *****************************************************************************
  1255. */
  1256. static res_t *user2res(name_id_t *name, user_t *usr)
  1257. {
  1258. int restag;
  1259. res_t *res;
  1260. assert(name != NULL);
  1261. assert(usr != NULL);
  1262. res = new_res();
  1263. restag = put_res_header(res, 0, usr->type, name, usr->memopt, &(usr->data->lvc));
  1264. put_raw_data(res, usr->data, 0);
  1265. return end_res(res, restag);
  1266. }
  1267. /*
  1268. *****************************************************************************
  1269. * Function : versionblock2res
  1270. * Syntax : void versionblock2res(res_t *res, ver_block_t *blk)
  1271. * Input :
  1272. * res - Binary resource to write to
  1273. * blk - The version block to be written
  1274. * Output :
  1275. * Description :
  1276. * Remarks : Self recursive
  1277. *****************************************************************************
  1278. */
  1279. static void versionblock2res(res_t *res, ver_block_t *blk, int level, const language_t *lang)
  1280. {
  1281. ver_value_t *val;
  1282. int blksizetag;
  1283. int valblksizetag;
  1284. int valvalsizetag;
  1285. int tag;
  1286. int i;
  1287. blksizetag = res->size;
  1288. put_word(res, 0); /* Will be overwritten later */
  1289. put_word(res, 0);
  1290. if(win32)
  1291. put_word(res, 0); /* level ? */
  1292. put_string(res, blk->name, TRUE, NULL);
  1293. put_pad(res);
  1294. for(val = blk->values; val; val = val->next)
  1295. {
  1296. if(val->type == val_str)
  1297. {
  1298. valblksizetag = res->size;
  1299. put_word(res, 0); /* Will be overwritten later */
  1300. valvalsizetag = res->size;
  1301. put_word(res, 0); /* Will be overwritten later */
  1302. if(win32)
  1303. {
  1304. put_word(res, level);
  1305. }
  1306. put_string(res, val->key, TRUE, NULL);
  1307. put_pad(res);
  1308. tag = res->size;
  1309. put_string(res, val->value.str, TRUE, lang);
  1310. if(win32)
  1311. set_word(res, valvalsizetag, (WORD)((res->size - tag) >> 1));
  1312. else
  1313. set_word(res, valvalsizetag, (WORD)(res->size - tag));
  1314. set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
  1315. put_pad(res);
  1316. }
  1317. else if(val->type == val_words)
  1318. {
  1319. valblksizetag = res->size;
  1320. put_word(res, 0); /* Will be overwritten later */
  1321. valvalsizetag = res->size;
  1322. put_word(res, 0); /* Will be overwritten later */
  1323. if(win32)
  1324. {
  1325. put_word(res, level);
  1326. }
  1327. put_string(res, val->key, TRUE, NULL);
  1328. put_pad(res);
  1329. tag = res->size;
  1330. for(i = 0; i < val->value.words->nwords; i++)
  1331. {
  1332. put_word(res, val->value.words->words[i]);
  1333. }
  1334. set_word(res, valvalsizetag, (WORD)(res->size - tag));
  1335. set_word(res, valblksizetag, (WORD)(res->size - valblksizetag));
  1336. put_pad(res);
  1337. }
  1338. else if(val->type == val_block)
  1339. {
  1340. versionblock2res(res, val->value.block, level+1, lang);
  1341. }
  1342. else
  1343. {
  1344. internal_error(__FILE__, __LINE__, "Invalid value indicator %d in VERSIONINFO\n", val->type);
  1345. }
  1346. }
  1347. /* Set blocksize */
  1348. set_word(res, blksizetag, (WORD)(res->size - blksizetag));
  1349. }
  1350. /*
  1351. *****************************************************************************
  1352. * Function : versioninfo2res
  1353. * Syntax : res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
  1354. * Input :
  1355. * name - Name/ordinal of the resource
  1356. * ver - The versioninfo descriptor
  1357. * Output : New .res format structure
  1358. * Description :
  1359. * Remarks :
  1360. *****************************************************************************
  1361. */
  1362. static res_t *versioninfo2res(name_id_t *name, versioninfo_t *ver)
  1363. {
  1364. static const char info[] = "VS_VERSION_INFO";
  1365. unsigned int i;
  1366. int restag, rootblocksizetag, valsizetag, tag;
  1367. res_t *res;
  1368. ver_block_t *blk;
  1369. assert(name != NULL);
  1370. assert(ver != NULL);
  1371. res = new_res();
  1372. restag = put_res_header(res, WRC_RT_VERSION, NULL, name, ver->memopt, &(ver->lvc));
  1373. rootblocksizetag = res->size;
  1374. put_word(res, 0); /* BlockSize filled in later */
  1375. valsizetag = res->size;
  1376. put_word(res, 0); /* ValueSize filled in later*/
  1377. if(win32)
  1378. {
  1379. put_word(res, 0); /* Tree-level ? */
  1380. for (i = 0; i < sizeof(info); i++) put_word(res, info[i]);
  1381. put_pad(res);
  1382. }
  1383. else
  1384. {
  1385. for (i = 0; i < sizeof(info); i++) put_byte(res, info[i]);
  1386. }
  1387. tag = res->size;
  1388. put_dword(res, VS_FFI_SIGNATURE);
  1389. put_dword(res, VS_FFI_STRUCVERSION);
  1390. put_dword(res, (ver->filever_maj1 << 16) + (ver->filever_maj2 & 0xffff));
  1391. put_dword(res, (ver->filever_min1 << 16) + (ver->filever_min2 & 0xffff));
  1392. put_dword(res, (ver->prodver_maj1 << 16) + (ver->prodver_maj2 & 0xffff));
  1393. put_dword(res, (ver->prodver_min1 << 16) + (ver->prodver_min2 & 0xffff));
  1394. put_dword(res, ver->fileflagsmask);
  1395. put_dword(res, ver->fileflags);
  1396. put_dword(res, ver->fileos);
  1397. put_dword(res, ver->filetype);
  1398. put_dword(res, ver->filesubtype);
  1399. put_dword(res, 0); /* FileDateMS */
  1400. put_dword(res, 0); /* FileDateLS */
  1401. /* Set ValueSize */
  1402. set_word(res, valsizetag, (WORD)(res->size - tag));
  1403. /* Descend into the blocks */
  1404. for(blk = ver->blocks; blk; blk = blk->next)
  1405. versionblock2res(res, blk, 0, win32 ? ver->lvc.language : NULL);
  1406. /* Set root block's size */
  1407. set_word(res, rootblocksizetag, (WORD)(res->size - rootblocksizetag));
  1408. return end_res(res, restag);
  1409. }
  1410. /*
  1411. *****************************************************************************
  1412. * Function : toolbaritem2res
  1413. * Syntax : void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
  1414. * Input :
  1415. * Output : nop
  1416. * Description :
  1417. * Remarks : Self recursive
  1418. *****************************************************************************
  1419. */
  1420. static void toolbaritem2res(res_t *res, toolbar_item_t *tbitem)
  1421. {
  1422. toolbar_item_t *itm = tbitem;
  1423. assert(win32 != 0);
  1424. while(itm)
  1425. {
  1426. put_word(res, itm->id);
  1427. itm = itm->next;
  1428. }
  1429. }
  1430. /*
  1431. *****************************************************************************
  1432. * Function : toolbar2res
  1433. * Syntax : res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
  1434. * Input :
  1435. * name - Name/ordinal of the resource
  1436. * toolbar - The toolbar descriptor
  1437. * Output : New .res format structure
  1438. * Description :
  1439. * Remarks :
  1440. *****************************************************************************
  1441. */
  1442. static res_t *toolbar2res(name_id_t *name, toolbar_t *toolbar)
  1443. {
  1444. int restag;
  1445. res_t *res;
  1446. assert(name != NULL);
  1447. assert(toolbar != NULL);
  1448. if (!win32) return NULL;
  1449. res = new_res();
  1450. restag = put_res_header(res, WRC_RT_TOOLBAR, NULL, name, toolbar->memopt, &(toolbar->lvc));
  1451. put_word(res, 1); /* Menuheader: Version */
  1452. put_word(res, toolbar->button_width); /* (in pixels?) */
  1453. put_word(res, toolbar->button_height); /* (in pixels?) */
  1454. put_word(res, toolbar->nitems);
  1455. put_pad(res);
  1456. toolbaritem2res(res, toolbar->items);
  1457. return end_res(res, restag);
  1458. }
  1459. /*
  1460. *****************************************************************************
  1461. * Function : dlginit2res
  1462. * Syntax : res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
  1463. * Input :
  1464. * name - Name/ordinal of the resource
  1465. * rdt - The dlginit descriptor
  1466. * Output : New .res format structure
  1467. * Description :
  1468. * Remarks :
  1469. *****************************************************************************
  1470. */
  1471. static res_t *dlginit2res(name_id_t *name, dlginit_t *dit)
  1472. {
  1473. int restag;
  1474. res_t *res;
  1475. assert(name != NULL);
  1476. assert(dit != NULL);
  1477. res = new_res();
  1478. restag = put_res_header(res, WRC_RT_DLGINIT, NULL, name, dit->memopt, &(dit->data->lvc));
  1479. put_raw_data(res, dit->data, 0);
  1480. return end_res(res, restag);
  1481. }
  1482. /*
  1483. *****************************************************************************
  1484. * Function : prep_nid_for_label
  1485. * Syntax : char *prep_nid_for_label(const name_id_t *nid)
  1486. * Input :
  1487. * Output :
  1488. * Description : Converts a resource name into the first 32 (or less)
  1489. * characters of the name with conversions.
  1490. * Remarks :
  1491. *****************************************************************************
  1492. */
  1493. #define MAXNAMELEN 32
  1494. char *prep_nid_for_label(const name_id_t *nid)
  1495. {
  1496. static char buf[MAXNAMELEN+1];
  1497. assert(nid != NULL);
  1498. if(nid->type == name_str && nid->name.s_name->type == str_unicode)
  1499. {
  1500. WCHAR *sptr;
  1501. int i;
  1502. sptr = nid->name.s_name->str.wstr;
  1503. buf[0] = '\0';
  1504. for(i = 0; *sptr && i < MAXNAMELEN; i++)
  1505. {
  1506. if((unsigned)*sptr < 0x80 && isprint(*sptr & 0xff))
  1507. buf[i] = *sptr++;
  1508. else
  1509. warning("Resourcename (str_unicode) contain unprintable characters or invalid translation, ignored\n");
  1510. }
  1511. buf[i] = '\0';
  1512. }
  1513. else if(nid->type == name_str && nid->name.s_name->type == str_char)
  1514. {
  1515. char *cptr;
  1516. int i;
  1517. cptr = nid->name.s_name->str.cstr;
  1518. buf[0] = '\0';
  1519. for(i = 0; *cptr && i < MAXNAMELEN; i++)
  1520. {
  1521. if((unsigned)*cptr < 0x80 && isprint(*cptr & 0xff))
  1522. buf[i] = *cptr++;
  1523. else
  1524. warning("Resourcename (str_char) contain unprintable characters, ignored\n");
  1525. }
  1526. buf[i] = '\0';
  1527. }
  1528. else if(nid->type == name_ord)
  1529. {
  1530. sprintf(buf, "%u", nid->name.i_name);
  1531. }
  1532. else
  1533. {
  1534. internal_error(__FILE__, __LINE__, "Resource name_id with invalid type %d\n", nid->type);
  1535. }
  1536. return buf;
  1537. }
  1538. #undef MAXNAMELEN
  1539. /*
  1540. *****************************************************************************
  1541. * Function : make_c_name
  1542. * Syntax : char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
  1543. * Input :
  1544. * Output :
  1545. * Description : Converts a resource name into a valid c-identifier in the
  1546. * form "_base_nid".
  1547. * Remarks :
  1548. *****************************************************************************
  1549. */
  1550. char *make_c_name(const char *base, const name_id_t *nid, const language_t *lan)
  1551. {
  1552. char *buf = prep_nid_for_label(nid);
  1553. return strmake( "_%s_%s_%d", base, buf, lan ? MAKELANGID(lan->id, lan->sub) : 0);
  1554. }
  1555. /*
  1556. *****************************************************************************
  1557. * Function : get_c_typename
  1558. * Syntax : const char *get_c_typename(enum res_e type)
  1559. * Input :
  1560. * Output :
  1561. * Description : Convert resource enum to char string to be used in c-name
  1562. * creation.
  1563. * Remarks :
  1564. *****************************************************************************
  1565. */
  1566. const char *get_c_typename(enum res_e type)
  1567. {
  1568. switch(type)
  1569. {
  1570. case res_acc: return "Acc";
  1571. case res_anicur:return "AniCur";
  1572. case res_aniico:return "AniIco";
  1573. case res_bmp: return "Bmp";
  1574. case res_cur: return "Cur";
  1575. case res_curg: return "CurGrp";
  1576. case res_dlg: return "Dlg";
  1577. case res_fnt: return "Fnt";
  1578. case res_fntdir:return "FntDir";
  1579. case res_ico: return "Ico";
  1580. case res_icog: return "IcoGrp";
  1581. case res_men: return "Men";
  1582. case res_rdt: return "RCDat";
  1583. case res_stt: return "StrTab";
  1584. case res_usr: return "Usr";
  1585. case res_msg: return "MsgTab";
  1586. case res_ver: return "VerInf";
  1587. case res_toolbar: return "TlBr";
  1588. case res_dlginit: return "DlgInit";
  1589. default: return "Oops";
  1590. }
  1591. }
  1592. /*
  1593. *****************************************************************************
  1594. * Function : resources2res
  1595. * Syntax : void resources2res(resource_t *top)
  1596. * Input :
  1597. * top - The resource-tree to convert
  1598. * Output :
  1599. * Description : Convert logical resource descriptors into binary data
  1600. * Remarks :
  1601. *****************************************************************************
  1602. */
  1603. void resources2res(resource_t *top)
  1604. {
  1605. while(top)
  1606. {
  1607. switch(top->type)
  1608. {
  1609. case res_acc:
  1610. if(!top->binres)
  1611. top->binres = accelerator2res(top->name, top->res.acc);
  1612. break;
  1613. case res_bmp:
  1614. if(!top->binres)
  1615. top->binres = bitmap2res(top->name, top->res.bmp);
  1616. break;
  1617. case res_cur:
  1618. if(!top->binres)
  1619. top->binres = cursor2res(top->res.cur);
  1620. break;
  1621. case res_curg:
  1622. if(!top->binres)
  1623. top->binres = cursorgroup2res(top->name, top->res.curg);
  1624. break;
  1625. case res_dlg:
  1626. if(!top->binres)
  1627. top->binres = dialog2res(top->name, top->res.dlg);
  1628. break;
  1629. case res_fnt:
  1630. if(!top->binres)
  1631. top->binres = font2res(top->name, top->res.fnt);
  1632. break;
  1633. case res_fntdir:
  1634. if(!top->binres)
  1635. top->binres = fontdir2res(top->name, top->res.fnd);
  1636. break;
  1637. case res_ico:
  1638. if(!top->binres)
  1639. top->binres = icon2res(top->res.ico);
  1640. break;
  1641. case res_icog:
  1642. if(!top->binres)
  1643. top->binres = icongroup2res(top->name, top->res.icog);
  1644. break;
  1645. case res_men:
  1646. if(!top->binres)
  1647. top->binres = menu2res(top->name, top->res.men);
  1648. break;
  1649. case res_html:
  1650. if(!top->binres)
  1651. top->binres = html2res(top->name, top->res.html);
  1652. break;
  1653. case res_rdt:
  1654. if(!top->binres)
  1655. top->binres = rcdata2res(top->name, top->res.rdt);
  1656. break;
  1657. case res_stt:
  1658. if(!top->binres)
  1659. top->binres = stringtable2res(top->res.stt);
  1660. break;
  1661. case res_usr:
  1662. if(!top->binres)
  1663. top->binres = user2res(top->name, top->res.usr);
  1664. break;
  1665. case res_msg:
  1666. if(!top->binres)
  1667. top->binres = messagetable2res(top->name, top->res.msg);
  1668. break;
  1669. case res_ver:
  1670. if(!top->binres)
  1671. top->binres = versioninfo2res(top->name, top->res.ver);
  1672. break;
  1673. case res_toolbar:
  1674. if(!top->binres)
  1675. top->binres = toolbar2res(top->name, top->res.tbt);
  1676. break;
  1677. case res_dlginit:
  1678. if(!top->binres)
  1679. top->binres = dlginit2res(top->name, top->res.dlgi);
  1680. break;
  1681. case res_anicur:
  1682. case res_aniico:
  1683. if(!top->binres)
  1684. top->binres = anicurico2res(top->name, top->res.ani, top->type);
  1685. break;
  1686. default:
  1687. internal_error(__FILE__, __LINE__, "Unknown resource type encountered %d in binary res generation\n", top->type);
  1688. }
  1689. top->c_name = make_c_name(get_c_typename(top->type), top->name, top->lan);
  1690. top = top->next;
  1691. }
  1692. }