controls.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654
  1. /*
  2. * controls.c: routines to self-manage the controls in a dialog
  3. * box.
  4. */
  5. /*
  6. * Possible TODO in new cross-platform config box stuff:
  7. *
  8. * - When lining up two controls alongside each other, I wonder if
  9. * we could conveniently arrange to centre them vertically?
  10. * Particularly ugly in the current setup is the `Add new
  11. * forwarded port:' static next to the rather taller `Remove'
  12. * button.
  13. */
  14. #include <assert.h>
  15. #include <ctype.h>
  16. #include "putty.h"
  17. #include "misc.h"
  18. #include "dialog.h"
  19. #include <commctrl.h>
  20. #define GAPBETWEEN 3
  21. #define GAPWITHIN 1
  22. #define GAPXBOX 7
  23. #define GAPYBOX 4
  24. #define DLGWIDTH 168
  25. #define STATICHEIGHT 8
  26. #define TITLEHEIGHT 12
  27. #define CHECKBOXHEIGHT 8
  28. #define RADIOHEIGHT 8
  29. #define EDITHEIGHT 12
  30. #define LISTHEIGHT 11
  31. #define LISTINCREMENT 8
  32. #define COMBOHEIGHT 12
  33. #define PUSHBTNHEIGHT 14
  34. #define PROGBARHEIGHT 14
  35. DECL_WINDOWS_FUNCTION(static, void, InitCommonControls, (void));
  36. DECL_WINDOWS_FUNCTION(static, BOOL, MakeDragList, (HWND));
  37. DECL_WINDOWS_FUNCTION(static, int, LBItemFromPt, (HWND, POINT, BOOL));
  38. DECL_WINDOWS_FUNCTION(static, void, DrawInsert, (HWND, HWND, int));
  39. void init_common_controls(void)
  40. {
  41. HMODULE comctl32_module = load_system32_dll("comctl32.dll");
  42. GET_WINDOWS_FUNCTION(comctl32_module, InitCommonControls);
  43. GET_WINDOWS_FUNCTION(comctl32_module, MakeDragList);
  44. GET_WINDOWS_FUNCTION(comctl32_module, LBItemFromPt);
  45. GET_WINDOWS_FUNCTION(comctl32_module, DrawInsert);
  46. p_InitCommonControls();
  47. }
  48. void ctlposinit(struct ctlpos *cp, HWND hwnd,
  49. int leftborder, int rightborder, int topborder)
  50. {
  51. RECT r, r2;
  52. cp->hwnd = hwnd;
  53. cp->font = SendMessage(hwnd, WM_GETFONT, 0, 0);
  54. cp->ypos = topborder;
  55. GetClientRect(hwnd, &r);
  56. r2.left = r2.top = 0;
  57. r2.right = 4;
  58. r2.bottom = 8;
  59. MapDialogRect(hwnd, &r2);
  60. cp->dlu4inpix = r2.right;
  61. cp->width = (r.right * 4) / (r2.right) - 2 * GAPBETWEEN;
  62. cp->xoff = leftborder;
  63. cp->width -= leftborder + rightborder;
  64. }
  65. HWND doctl(struct ctlpos *cp, RECT r, const char *wclass, int wstyle,
  66. int exstyle, const char *wtext, int wid)
  67. {
  68. HWND ctl;
  69. /*
  70. * Note nonstandard use of RECT. This is deliberate: by
  71. * transforming the width and height directly we arrange to
  72. * have all supposedly same-sized controls really same-sized.
  73. */
  74. r.left += cp->xoff;
  75. MapDialogRect(cp->hwnd, &r);
  76. /*
  77. * We can pass in cp->hwnd == NULL, to indicate a dry run
  78. * without creating any actual controls.
  79. */
  80. if (cp->hwnd) {
  81. ctl = CreateWindowEx(exstyle, wclass, wtext, wstyle,
  82. r.left, r.top, r.right, r.bottom,
  83. cp->hwnd, (HMENU)(ULONG_PTR)wid, hinst, NULL);
  84. SendMessage(ctl, WM_SETFONT, cp->font, MAKELPARAM(true, 0));
  85. if (!strcmp(wclass, "LISTBOX")) {
  86. /*
  87. * Bizarre Windows bug: the list box calculates its
  88. * number of lines based on the font it has at creation
  89. * time, but sending it WM_SETFONT doesn't cause it to
  90. * recalculate. So now, _after_ we've sent it
  91. * WM_SETFONT, we explicitly resize it (to the same
  92. * size it was already!) to force it to reconsider.
  93. */
  94. SetWindowPos(ctl, NULL, 0, 0, r.right, r.bottom,
  95. SWP_NOACTIVATE | SWP_NOCOPYBITS |
  96. SWP_NOMOVE | SWP_NOZORDER);
  97. }
  98. } else
  99. ctl = NULL;
  100. return ctl;
  101. }
  102. /*
  103. * A title bar across the top of a sub-dialog.
  104. */
  105. void bartitle(struct ctlpos *cp, const char *name, int id)
  106. {
  107. RECT r;
  108. r.left = GAPBETWEEN;
  109. r.right = cp->width;
  110. r.top = cp->ypos;
  111. r.bottom = STATICHEIGHT;
  112. cp->ypos += r.bottom + GAPBETWEEN;
  113. doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, name, id);
  114. }
  115. /*
  116. * Begin a grouping box, with or without a group title.
  117. */
  118. void beginbox(struct ctlpos *cp, const char *name, int idbox)
  119. {
  120. cp->boxystart = cp->ypos;
  121. if (!name)
  122. cp->boxystart -= STATICHEIGHT / 2;
  123. if (name)
  124. cp->ypos += STATICHEIGHT;
  125. cp->ypos += GAPYBOX;
  126. cp->width -= 2 * GAPXBOX;
  127. cp->xoff += GAPXBOX;
  128. cp->boxid = idbox;
  129. cp->boxtext = name;
  130. }
  131. /*
  132. * End a grouping box.
  133. */
  134. void endbox(struct ctlpos *cp)
  135. {
  136. RECT r;
  137. cp->xoff -= GAPXBOX;
  138. cp->width += 2 * GAPXBOX;
  139. cp->ypos += GAPYBOX - GAPBETWEEN;
  140. r.left = GAPBETWEEN;
  141. r.right = cp->width;
  142. r.top = cp->boxystart;
  143. r.bottom = cp->ypos - cp->boxystart;
  144. doctl(cp, r, "BUTTON", BS_GROUPBOX | WS_CHILD | WS_VISIBLE, 0,
  145. cp->boxtext ? cp->boxtext : "", cp->boxid);
  146. cp->ypos += GAPYBOX;
  147. }
  148. /*
  149. * A static line, followed by a full-width edit box.
  150. */
  151. void editboxfw(struct ctlpos *cp, bool password, bool readonly,
  152. const char *text, int staticid, int editid)
  153. {
  154. RECT r;
  155. r.left = GAPBETWEEN;
  156. r.right = cp->width;
  157. if (text) {
  158. r.top = cp->ypos;
  159. r.bottom = STATICHEIGHT;
  160. doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, staticid);
  161. cp->ypos += STATICHEIGHT + GAPWITHIN;
  162. }
  163. r.top = cp->ypos;
  164. r.bottom = EDITHEIGHT;
  165. doctl(cp, r, "EDIT",
  166. WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL |
  167. (password ? ES_PASSWORD : 0) |
  168. (readonly ? ES_READONLY : 0),
  169. WS_EX_CLIENTEDGE, "", editid);
  170. cp->ypos += EDITHEIGHT + GAPBETWEEN;
  171. }
  172. /*
  173. * A static line, followed by a full-width combo box.
  174. */
  175. void combobox(struct ctlpos *cp, const char *text, int staticid, int listid)
  176. {
  177. RECT r;
  178. r.left = GAPBETWEEN;
  179. r.right = cp->width;
  180. if (text) {
  181. r.top = cp->ypos;
  182. r.bottom = STATICHEIGHT;
  183. doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, staticid);
  184. cp->ypos += STATICHEIGHT + GAPWITHIN;
  185. }
  186. r.top = cp->ypos;
  187. r.bottom = COMBOHEIGHT * 10;
  188. doctl(cp, r, "COMBOBOX",
  189. WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
  190. CBS_DROPDOWN | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", listid);
  191. cp->ypos += COMBOHEIGHT + GAPBETWEEN;
  192. }
  193. struct radio { const char *text; int id; };
  194. static void radioline_common(struct ctlpos *cp, const char *text, int id,
  195. int nacross, struct radio *buttons, int nbuttons)
  196. {
  197. RECT r;
  198. int group;
  199. int i;
  200. int j;
  201. r.left = GAPBETWEEN;
  202. r.top = cp->ypos;
  203. if (text) {
  204. r.right = cp->width;
  205. r.bottom = STATICHEIGHT;
  206. cp->ypos += r.bottom + GAPWITHIN;
  207. doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, id);
  208. } else {
  209. r.right = r.bottom = 0;
  210. }
  211. group = WS_GROUP;
  212. i = 0;
  213. for (j = 0; j < nbuttons; j++) {
  214. const char *btext = buttons[j].text;
  215. int bid = buttons[j].id;
  216. if (i == nacross) {
  217. cp->ypos += r.bottom + (nacross > 1 ? GAPBETWEEN : GAPWITHIN);
  218. i = 0;
  219. }
  220. r.left = GAPBETWEEN + i * (cp->width + GAPBETWEEN) / nacross;
  221. if (j < nbuttons-1)
  222. r.right =
  223. (i + 1) * (cp->width + GAPBETWEEN) / nacross - r.left;
  224. else
  225. r.right = cp->width - r.left;
  226. r.top = cp->ypos;
  227. r.bottom = RADIOHEIGHT;
  228. doctl(cp, r, "BUTTON",
  229. BS_NOTIFY | BS_AUTORADIOBUTTON | WS_CHILD |
  230. WS_VISIBLE | WS_TABSTOP | group, 0, btext, bid);
  231. group = 0;
  232. i++;
  233. }
  234. cp->ypos += r.bottom + GAPBETWEEN;
  235. }
  236. /*
  237. * A set of radio buttons on the same line, with a static above
  238. * them. `nacross' dictates how many parts the line is divided into
  239. * (you might want this not to equal the number of buttons if you
  240. * needed to line up some 2s and some 3s to look good in the same
  241. * panel).
  242. *
  243. * There's a bit of a hack in here to ensure that if nacross
  244. * exceeds the actual number of buttons, the rightmost button
  245. * really does get all the space right to the edge of the line, so
  246. * you can do things like
  247. *
  248. * (*) Button1 (*) Button2 (*) ButtonWithReallyLongTitle
  249. */
  250. void radioline(struct ctlpos *cp, const char *text, int id, int nacross, ...)
  251. {
  252. va_list ap;
  253. struct radio *buttons;
  254. int i, nbuttons;
  255. va_start(ap, nacross);
  256. nbuttons = 0;
  257. while (1) {
  258. const char *btext = va_arg(ap, const char *);
  259. if (!btext)
  260. break;
  261. (void) va_arg(ap, int); /* id */
  262. nbuttons++;
  263. }
  264. va_end(ap);
  265. buttons = snewn(nbuttons, struct radio);
  266. va_start(ap, nacross);
  267. for (i = 0; i < nbuttons; i++) {
  268. buttons[i].text = va_arg(ap, const char *);
  269. buttons[i].id = va_arg(ap, int);
  270. }
  271. va_end(ap);
  272. radioline_common(cp, text, id, nacross, buttons, nbuttons);
  273. sfree(buttons);
  274. }
  275. /*
  276. * A set of radio buttons on the same line, without a static above
  277. * them. Otherwise just like radioline.
  278. */
  279. void bareradioline(struct ctlpos *cp, int nacross, ...)
  280. {
  281. va_list ap;
  282. struct radio *buttons;
  283. int i, nbuttons;
  284. va_start(ap, nacross);
  285. nbuttons = 0;
  286. while (1) {
  287. const char *btext = va_arg(ap, const char *);
  288. if (!btext)
  289. break;
  290. (void) va_arg(ap, int); /* id */
  291. nbuttons++;
  292. }
  293. va_end(ap);
  294. buttons = snewn(nbuttons, struct radio);
  295. va_start(ap, nacross);
  296. for (i = 0; i < nbuttons; i++) {
  297. buttons[i].text = va_arg(ap, const char *);
  298. buttons[i].id = va_arg(ap, int);
  299. }
  300. va_end(ap);
  301. radioline_common(cp, NULL, 0, nacross, buttons, nbuttons);
  302. sfree(buttons);
  303. }
  304. /*
  305. * A set of radio buttons on multiple lines, with a static above
  306. * them.
  307. */
  308. void radiobig(struct ctlpos *cp, const char *text, int id, ...)
  309. {
  310. va_list ap;
  311. struct radio *buttons;
  312. int i, nbuttons;
  313. va_start(ap, id);
  314. nbuttons = 0;
  315. while (1) {
  316. const char *btext = va_arg(ap, const char *);
  317. if (!btext)
  318. break;
  319. (void) va_arg(ap, int); /* id */
  320. nbuttons++;
  321. }
  322. va_end(ap);
  323. buttons = snewn(nbuttons, struct radio);
  324. va_start(ap, id);
  325. for (i = 0; i < nbuttons; i++) {
  326. buttons[i].text = va_arg(ap, const char *);
  327. buttons[i].id = va_arg(ap, int);
  328. }
  329. va_end(ap);
  330. radioline_common(cp, text, id, 1, buttons, nbuttons);
  331. sfree(buttons);
  332. }
  333. /*
  334. * A single standalone checkbox.
  335. */
  336. void checkbox(struct ctlpos *cp, const char *text, int id)
  337. {
  338. RECT r;
  339. r.left = GAPBETWEEN;
  340. r.top = cp->ypos;
  341. r.right = cp->width;
  342. r.bottom = CHECKBOXHEIGHT;
  343. cp->ypos += r.bottom + GAPBETWEEN;
  344. doctl(cp, r, "BUTTON",
  345. BS_NOTIFY | BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0,
  346. text, id);
  347. }
  348. /*
  349. * Wrap a piece of text for a static text control. Returns the
  350. * wrapped text (a malloc'ed string containing \ns), and also
  351. * returns the number of lines required.
  352. */
  353. char *staticwrap(struct ctlpos *cp, HWND hwnd, const char *text, int *lines)
  354. {
  355. HDC hdc = GetDC(hwnd);
  356. int width, nlines, j;
  357. INT *pwidths, nfit;
  358. SIZE size;
  359. const char *p;
  360. RECT r;
  361. HFONT oldfont, newfont;
  362. strbuf *sb = strbuf_new();
  363. p = text;
  364. pwidths = snewn(1+strlen(text), INT);
  365. /*
  366. * Work out the width the text will need to fit in, by doing
  367. * the same adjustment that the `statictext' function itself
  368. * will perform.
  369. */
  370. SetMapMode(hdc, MM_TEXT); /* ensure logical units == pixels */
  371. r.left = r.top = r.bottom = 0;
  372. r.right = cp->width;
  373. MapDialogRect(hwnd, &r);
  374. width = r.right;
  375. nlines = 1;
  376. /*
  377. * We must select the correct font into the HDC before calling
  378. * GetTextExtent*, or silly things will happen.
  379. */
  380. newfont = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0);
  381. oldfont = SelectObject(hdc, newfont);
  382. while (*p) {
  383. if (!GetTextExtentExPoint(hdc, p, strlen(p), width,
  384. &nfit, pwidths, &size) ||
  385. (size_t)nfit >= strlen(p)) {
  386. /*
  387. * Either GetTextExtentExPoint returned failure, or the
  388. * whole of the rest of the text fits on this line.
  389. * Either way, we stop wrapping, copy the remainder of
  390. * the input string unchanged to the output, and leave.
  391. */
  392. put_datapl(sb, ptrlen_from_asciz(p));
  393. break;
  394. }
  395. /*
  396. * Now we search backwards along the string from `nfit',
  397. * looking for a space at which to break the line. If we
  398. * don't find one at all, that's fine - we'll just break
  399. * the line at `nfit'.
  400. */
  401. for (j = nfit; j > 0; j--) {
  402. if (isspace((unsigned char)p[j])) {
  403. nfit = j;
  404. break;
  405. }
  406. }
  407. put_data(sb, p, nfit);
  408. put_byte(sb, '\n');
  409. p += nfit;
  410. while (*p && isspace((unsigned char)*p))
  411. p++;
  412. nlines++;
  413. }
  414. SelectObject(hdc, oldfont);
  415. ReleaseDC(cp->hwnd, hdc);
  416. if (lines) *lines = nlines;
  417. sfree(pwidths);
  418. return strbuf_to_str(sb);
  419. }
  420. /*
  421. * A single standalone static text control.
  422. */
  423. void statictext(struct ctlpos *cp, const char *text, int lines, int id)
  424. {
  425. RECT r;
  426. r.left = GAPBETWEEN;
  427. r.top = cp->ypos;
  428. r.right = cp->width;
  429. r.bottom = STATICHEIGHT * lines;
  430. cp->ypos += r.bottom + GAPBETWEEN;
  431. doctl(cp, r, "STATIC",
  432. WS_CHILD | WS_VISIBLE | SS_LEFTNOWORDWRAP,
  433. 0, text, id);
  434. }
  435. /*
  436. * An owner-drawn static text control for a panel title.
  437. */
  438. void paneltitle(struct ctlpos *cp, int id)
  439. {
  440. RECT r;
  441. r.left = GAPBETWEEN;
  442. r.top = cp->ypos;
  443. r.right = cp->width;
  444. r.bottom = TITLEHEIGHT;
  445. cp->ypos += r.bottom + GAPBETWEEN;
  446. doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_OWNERDRAW,
  447. 0, NULL, id);
  448. }
  449. /*
  450. * A button on the right hand side, with a static to its left.
  451. */
  452. void staticbtn(struct ctlpos *cp, const char *stext, int sid,
  453. const char *btext, int bid)
  454. {
  455. const int height = (PUSHBTNHEIGHT > STATICHEIGHT ?
  456. PUSHBTNHEIGHT : STATICHEIGHT);
  457. RECT r;
  458. int lwid, rwid, rpos;
  459. rpos = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
  460. lwid = rpos - 2 * GAPBETWEEN;
  461. rwid = cp->width + GAPBETWEEN - rpos;
  462. r.left = GAPBETWEEN;
  463. r.top = cp->ypos + (height - STATICHEIGHT) / 2;
  464. r.right = lwid;
  465. r.bottom = STATICHEIGHT;
  466. doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
  467. r.left = rpos;
  468. r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
  469. r.right = rwid;
  470. r.bottom = PUSHBTNHEIGHT;
  471. doctl(cp, r, "BUTTON",
  472. BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
  473. 0, btext, bid);
  474. cp->ypos += height + GAPBETWEEN;
  475. }
  476. /*
  477. * A simple push button.
  478. */
  479. void button(struct ctlpos *cp, const char *btext, int bid, bool defbtn)
  480. {
  481. RECT r;
  482. r.left = GAPBETWEEN;
  483. r.top = cp->ypos;
  484. r.right = cp->width;
  485. r.bottom = PUSHBTNHEIGHT;
  486. /* Q67655: the _dialog box_ must know which button is default
  487. * as well as the button itself knowing */
  488. if (defbtn && cp->hwnd)
  489. SendMessage(cp->hwnd, DM_SETDEFID, bid, 0);
  490. doctl(cp, r, "BUTTON",
  491. BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP |
  492. (defbtn ? BS_DEFPUSHBUTTON : 0) | BS_PUSHBUTTON,
  493. 0, btext, bid);
  494. cp->ypos += PUSHBTNHEIGHT + GAPBETWEEN;
  495. }
  496. /*
  497. * Like staticbtn, but two buttons.
  498. */
  499. void static2btn(struct ctlpos *cp, const char *stext, int sid,
  500. const char *btext1, int bid1, const char *btext2, int bid2)
  501. {
  502. const int height = (PUSHBTNHEIGHT > STATICHEIGHT ?
  503. PUSHBTNHEIGHT : STATICHEIGHT);
  504. RECT r;
  505. int lwid, rwid1, rwid2, rpos1, rpos2;
  506. rpos1 = GAPBETWEEN + (cp->width + GAPBETWEEN) / 2;
  507. rpos2 = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
  508. lwid = rpos1 - 2 * GAPBETWEEN;
  509. rwid1 = rpos2 - rpos1 - GAPBETWEEN;
  510. rwid2 = cp->width + GAPBETWEEN - rpos2;
  511. r.left = GAPBETWEEN;
  512. r.top = cp->ypos + (height - STATICHEIGHT) / 2;
  513. r.right = lwid;
  514. r.bottom = STATICHEIGHT;
  515. doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
  516. r.left = rpos1;
  517. r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
  518. r.right = rwid1;
  519. r.bottom = PUSHBTNHEIGHT;
  520. doctl(cp, r, "BUTTON",
  521. BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
  522. 0, btext1, bid1);
  523. r.left = rpos2;
  524. r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
  525. r.right = rwid2;
  526. r.bottom = PUSHBTNHEIGHT;
  527. doctl(cp, r, "BUTTON",
  528. BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
  529. 0, btext2, bid2);
  530. cp->ypos += height + GAPBETWEEN;
  531. }
  532. /*
  533. * An edit control on the right hand side, with a static to its left.
  534. */
  535. static void staticedit_internal(struct ctlpos *cp, const char *stext,
  536. int sid, int eid, int percentedit,
  537. int style)
  538. {
  539. const int height = (EDITHEIGHT > STATICHEIGHT ?
  540. EDITHEIGHT : STATICHEIGHT);
  541. RECT r;
  542. int lwid, rwid, rpos;
  543. rpos =
  544. GAPBETWEEN + (100 - percentedit) * (cp->width + GAPBETWEEN) / 100;
  545. lwid = rpos - 2 * GAPBETWEEN;
  546. rwid = cp->width + GAPBETWEEN - rpos;
  547. r.left = GAPBETWEEN;
  548. r.top = cp->ypos + (height - STATICHEIGHT) / 2;
  549. r.right = lwid;
  550. r.bottom = STATICHEIGHT;
  551. doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
  552. r.left = rpos;
  553. r.top = cp->ypos + (height - EDITHEIGHT) / 2;
  554. r.right = rwid;
  555. r.bottom = EDITHEIGHT;
  556. doctl(cp, r, "EDIT",
  557. WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL | style,
  558. WS_EX_CLIENTEDGE, "", eid);
  559. cp->ypos += height + GAPBETWEEN;
  560. }
  561. void staticedit(struct ctlpos *cp, const char *stext,
  562. int sid, int eid, int percentedit)
  563. {
  564. staticedit_internal(cp, stext, sid, eid, percentedit, 0);
  565. }
  566. void staticpassedit(struct ctlpos *cp, const char *stext,
  567. int sid, int eid, int percentedit)
  568. {
  569. staticedit_internal(cp, stext, sid, eid, percentedit, ES_PASSWORD);
  570. }
  571. /*
  572. * A drop-down list box on the right hand side, with a static to
  573. * its left.
  574. */
  575. void staticddl(struct ctlpos *cp, const char *stext,
  576. int sid, int lid, int percentlist)
  577. {
  578. const int height = (COMBOHEIGHT > STATICHEIGHT ?
  579. COMBOHEIGHT : STATICHEIGHT);
  580. RECT r;
  581. int lwid, rwid, rpos;
  582. rpos =
  583. GAPBETWEEN + (100 - percentlist) * (cp->width + GAPBETWEEN) / 100;
  584. lwid = rpos - 2 * GAPBETWEEN;
  585. rwid = cp->width + GAPBETWEEN - rpos;
  586. r.left = GAPBETWEEN;
  587. r.top = cp->ypos + (height - STATICHEIGHT) / 2;
  588. r.right = lwid;
  589. r.bottom = STATICHEIGHT;
  590. doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
  591. r.left = rpos;
  592. r.top = cp->ypos + (height - EDITHEIGHT) / 2;
  593. r.right = rwid;
  594. r.bottom = COMBOHEIGHT*4;
  595. doctl(cp, r, "COMBOBOX",
  596. WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
  597. CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
  598. cp->ypos += height + GAPBETWEEN;
  599. }
  600. /*
  601. * A combo box on the right hand side, with a static to its left.
  602. */
  603. void staticcombo(struct ctlpos *cp, const char *stext,
  604. int sid, int lid, int percentlist)
  605. {
  606. const int height = (COMBOHEIGHT > STATICHEIGHT ?
  607. COMBOHEIGHT : STATICHEIGHT);
  608. RECT r;
  609. int lwid, rwid, rpos;
  610. rpos =
  611. GAPBETWEEN + (100 - percentlist) * (cp->width + GAPBETWEEN) / 100;
  612. lwid = rpos - 2 * GAPBETWEEN;
  613. rwid = cp->width + GAPBETWEEN - rpos;
  614. r.left = GAPBETWEEN;
  615. r.top = cp->ypos + (height - STATICHEIGHT) / 2;
  616. r.right = lwid;
  617. r.bottom = STATICHEIGHT;
  618. doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
  619. r.left = rpos;
  620. r.top = cp->ypos + (height - EDITHEIGHT) / 2;
  621. r.right = rwid;
  622. r.bottom = COMBOHEIGHT*10;
  623. doctl(cp, r, "COMBOBOX",
  624. WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
  625. CBS_DROPDOWN | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
  626. cp->ypos += height + GAPBETWEEN;
  627. }
  628. /*
  629. * A static, with a full-width drop-down list box below it.
  630. */
  631. void staticddlbig(struct ctlpos *cp, const char *stext,
  632. int sid, int lid)
  633. {
  634. RECT r;
  635. if (stext) {
  636. r.left = GAPBETWEEN;
  637. r.top = cp->ypos;
  638. r.right = cp->width;
  639. r.bottom = STATICHEIGHT;
  640. doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
  641. cp->ypos += STATICHEIGHT;
  642. }
  643. r.left = GAPBETWEEN;
  644. r.top = cp->ypos;
  645. r.right = cp->width;
  646. r.bottom = COMBOHEIGHT*4;
  647. doctl(cp, r, "COMBOBOX",
  648. WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
  649. CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
  650. cp->ypos += COMBOHEIGHT + GAPBETWEEN;
  651. }
  652. /*
  653. * A big multiline edit control with a static labelling it.
  654. */
  655. void bigeditctrl(struct ctlpos *cp, const char *stext,
  656. int sid, int eid, int lines)
  657. {
  658. RECT r;
  659. if (stext) {
  660. r.left = GAPBETWEEN;
  661. r.top = cp->ypos;
  662. r.right = cp->width;
  663. r.bottom = STATICHEIGHT;
  664. cp->ypos += r.bottom + GAPWITHIN;
  665. doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
  666. }
  667. r.left = GAPBETWEEN;
  668. r.top = cp->ypos;
  669. r.right = cp->width;
  670. r.bottom = EDITHEIGHT + (lines - 1) * STATICHEIGHT;
  671. cp->ypos += r.bottom + GAPBETWEEN;
  672. doctl(cp, r, "EDIT",
  673. WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | ES_MULTILINE,
  674. WS_EX_CLIENTEDGE, "", eid);
  675. }
  676. /*
  677. * A list box with a static labelling it.
  678. */
  679. void listbox(struct ctlpos *cp, const char *stext,
  680. int sid, int lid, int lines, bool multi)
  681. {
  682. RECT r;
  683. if (stext != NULL) {
  684. r.left = GAPBETWEEN;
  685. r.top = cp->ypos;
  686. r.right = cp->width;
  687. r.bottom = STATICHEIGHT;
  688. cp->ypos += r.bottom + GAPWITHIN;
  689. doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
  690. }
  691. r.left = GAPBETWEEN;
  692. r.top = cp->ypos;
  693. r.right = cp->width;
  694. r.bottom = LISTHEIGHT + (lines - 1) * LISTINCREMENT;
  695. cp->ypos += r.bottom + GAPBETWEEN;
  696. doctl(cp, r, "LISTBOX",
  697. WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
  698. LBS_NOTIFY | LBS_HASSTRINGS | LBS_USETABSTOPS |
  699. (multi ? LBS_MULTIPLESEL : 0),
  700. WS_EX_CLIENTEDGE, "", lid);
  701. }
  702. /*
  703. * A tab-control substitute when a real tab control is unavailable.
  704. */
  705. void ersatztab(struct ctlpos *cp, const char *stext, int sid, int lid,
  706. int s2id)
  707. {
  708. const int height = (COMBOHEIGHT > STATICHEIGHT ?
  709. COMBOHEIGHT : STATICHEIGHT);
  710. RECT r;
  711. int bigwid, lwid, rwid, rpos;
  712. static const int BIGGAP = 15;
  713. static const int MEDGAP = 3;
  714. bigwid = cp->width + 2 * GAPBETWEEN - 2 * BIGGAP;
  715. cp->ypos += MEDGAP;
  716. rpos = BIGGAP + (bigwid + BIGGAP) / 2;
  717. lwid = rpos - 2 * BIGGAP;
  718. rwid = bigwid + BIGGAP - rpos;
  719. r.left = BIGGAP;
  720. r.top = cp->ypos + (height - STATICHEIGHT) / 2;
  721. r.right = lwid;
  722. r.bottom = STATICHEIGHT;
  723. doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
  724. r.left = rpos;
  725. r.top = cp->ypos + (height - COMBOHEIGHT) / 2;
  726. r.right = rwid;
  727. r.bottom = COMBOHEIGHT * 10;
  728. doctl(cp, r, "COMBOBOX",
  729. WS_CHILD | WS_VISIBLE | WS_TABSTOP |
  730. CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
  731. cp->ypos += height + MEDGAP + GAPBETWEEN;
  732. r.left = GAPBETWEEN;
  733. r.top = cp->ypos;
  734. r.right = cp->width;
  735. r.bottom = 2;
  736. doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_ETCHEDHORZ,
  737. 0, "", s2id);
  738. }
  739. /*
  740. * A static line, followed by an edit control on the left hand side
  741. * and a button on the right.
  742. */
  743. void editbutton(struct ctlpos *cp, const char *stext, int sid,
  744. int eid, const char *btext, int bid)
  745. {
  746. const int height = (EDITHEIGHT > PUSHBTNHEIGHT ?
  747. EDITHEIGHT : PUSHBTNHEIGHT);
  748. RECT r;
  749. int lwid, rwid, rpos;
  750. r.left = GAPBETWEEN;
  751. r.top = cp->ypos;
  752. r.right = cp->width;
  753. r.bottom = STATICHEIGHT;
  754. cp->ypos += r.bottom + GAPWITHIN;
  755. doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
  756. rpos = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
  757. lwid = rpos - 2 * GAPBETWEEN;
  758. rwid = cp->width + GAPBETWEEN - rpos;
  759. r.left = GAPBETWEEN;
  760. r.top = cp->ypos + (height - EDITHEIGHT) / 2;
  761. r.right = lwid;
  762. r.bottom = EDITHEIGHT;
  763. doctl(cp, r, "EDIT",
  764. WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
  765. WS_EX_CLIENTEDGE, "", eid);
  766. r.left = rpos;
  767. r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
  768. r.right = rwid;
  769. r.bottom = PUSHBTNHEIGHT;
  770. doctl(cp, r, "BUTTON",
  771. BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
  772. 0, btext, bid);
  773. cp->ypos += height + GAPBETWEEN;
  774. }
  775. /*
  776. * A special control for manipulating an ordered preference list
  777. * (eg. for cipher selection).
  778. * XXX: this is a rough hack and could be improved.
  779. */
  780. void prefslist(struct prefslist *hdl, struct ctlpos *cp, int lines,
  781. const char *stext, int sid, int listid, int upbid, int dnbid)
  782. {
  783. const static int percents[] = { 5, 75, 20 };
  784. RECT r;
  785. int xpos, percent = 0, i;
  786. int listheight = LISTHEIGHT + (lines - 1) * LISTINCREMENT;
  787. const int BTNSHEIGHT = 2*PUSHBTNHEIGHT + GAPBETWEEN;
  788. int totalheight, buttonpos;
  789. /* Squirrel away IDs. */
  790. hdl->listid = listid;
  791. hdl->upbid = upbid;
  792. hdl->dnbid = dnbid;
  793. /* The static label. */
  794. if (stext != NULL) {
  795. r.left = GAPBETWEEN;
  796. r.top = cp->ypos;
  797. r.right = cp->width;
  798. r.bottom = STATICHEIGHT;
  799. cp->ypos += r.bottom + GAPWITHIN;
  800. doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
  801. }
  802. if (listheight > BTNSHEIGHT) {
  803. totalheight = listheight;
  804. buttonpos = (listheight - BTNSHEIGHT) / 2;
  805. } else {
  806. totalheight = BTNSHEIGHT;
  807. buttonpos = 0;
  808. }
  809. for (i=0; i<3; i++) {
  810. int left, wid;
  811. xpos = (cp->width + GAPBETWEEN) * percent / 100;
  812. left = xpos + GAPBETWEEN;
  813. percent += percents[i];
  814. xpos = (cp->width + GAPBETWEEN) * percent / 100;
  815. wid = xpos - left;
  816. switch (i) {
  817. case 1: {
  818. /* The drag list box. */
  819. r.left = left; r.right = wid;
  820. r.top = cp->ypos; r.bottom = listheight;
  821. HWND ctl = doctl(cp, r, "LISTBOX",
  822. WS_CHILD | WS_VISIBLE | WS_TABSTOP |
  823. WS_VSCROLL | LBS_HASSTRINGS | LBS_USETABSTOPS,
  824. WS_EX_CLIENTEDGE,
  825. "", listid);
  826. p_MakeDragList(ctl);
  827. break;
  828. }
  829. case 2:
  830. /* The "Up" and "Down" buttons. */
  831. /* XXX worry about accelerators if we have more than one
  832. * prefslist on a panel */
  833. r.left = left; r.right = wid;
  834. r.top = cp->ypos + buttonpos; r.bottom = PUSHBTNHEIGHT;
  835. doctl(cp, r, "BUTTON",
  836. BS_NOTIFY | WS_CHILD | WS_VISIBLE |
  837. WS_TABSTOP | BS_PUSHBUTTON,
  838. 0, "&Up", upbid);
  839. r.left = left; r.right = wid;
  840. r.top = cp->ypos + buttonpos + PUSHBTNHEIGHT + GAPBETWEEN;
  841. r.bottom = PUSHBTNHEIGHT;
  842. doctl(cp, r, "BUTTON",
  843. BS_NOTIFY | WS_CHILD | WS_VISIBLE |
  844. WS_TABSTOP | BS_PUSHBUTTON,
  845. 0, "&Down", dnbid);
  846. break;
  847. }
  848. }
  849. cp->ypos += totalheight + GAPBETWEEN;
  850. }
  851. /*
  852. * Helper function for prefslist: move item in list box.
  853. */
  854. static void pl_moveitem(HWND hwnd, int listid, int src, int dst)
  855. {
  856. int tlen, val;
  857. char *txt;
  858. /* Get the item's data. */
  859. tlen = SendDlgItemMessage (hwnd, listid, LB_GETTEXTLEN, src, 0);
  860. txt = snewn(tlen+1, char);
  861. SendDlgItemMessage (hwnd, listid, LB_GETTEXT, src, (LPARAM) txt);
  862. val = SendDlgItemMessage (hwnd, listid, LB_GETITEMDATA, src, 0);
  863. /* Deselect old location. */
  864. SendDlgItemMessage (hwnd, listid, LB_SETSEL, false, src);
  865. /* Delete it at the old location. */
  866. SendDlgItemMessage (hwnd, listid, LB_DELETESTRING, src, 0);
  867. /* Insert it at new location. */
  868. SendDlgItemMessage (hwnd, listid, LB_INSERTSTRING, dst,
  869. (LPARAM) txt);
  870. SendDlgItemMessage (hwnd, listid, LB_SETITEMDATA, dst,
  871. (LPARAM) val);
  872. /* Set selection. */
  873. SendDlgItemMessage (hwnd, listid, LB_SETCURSEL, dst, 0);
  874. sfree (txt);
  875. }
  876. int pl_itemfrompt(HWND hwnd, POINT cursor, bool scroll)
  877. {
  878. int ret;
  879. POINT uppoint, downpoint;
  880. int updist, downdist, upitem, downitem, i;
  881. /*
  882. * Ghastly hackery to try to figure out not which
  883. * _item_, but which _gap between items_, the user
  884. * is pointing at. We do this by first working out
  885. * which list item is under the cursor, and then
  886. * working out how far the cursor would have to
  887. * move up or down before the answer was different.
  888. * Then we put the insertion point _above_ the
  889. * current item if the upper edge is closer than
  890. * the lower edge, or _below_ it if vice versa.
  891. */
  892. ret = p_LBItemFromPt(hwnd, cursor, scroll);
  893. if (ret == -1)
  894. return ret;
  895. ret = p_LBItemFromPt(hwnd, cursor, false);
  896. updist = downdist = 0;
  897. for (i = 1; i < 4096 && (!updist || !downdist); i++) {
  898. uppoint = downpoint = cursor;
  899. uppoint.y -= i;
  900. downpoint.y += i;
  901. upitem = p_LBItemFromPt(hwnd, uppoint, false);
  902. downitem = p_LBItemFromPt(hwnd, downpoint, false);
  903. if (!updist && upitem != ret)
  904. updist = i;
  905. if (!downdist && downitem != ret)
  906. downdist = i;
  907. }
  908. if (downdist < updist)
  909. ret++;
  910. return ret;
  911. }
  912. /*
  913. * Handler for prefslist above.
  914. *
  915. * Return value has bit 0 set if the dialog box procedure needs to
  916. * return true from handling this message; it has bit 1 set if a
  917. * change may have been made in the contents of the list.
  918. */
  919. int handle_prefslist(struct prefslist *hdl,
  920. int *array, int maxmemb,
  921. bool is_dlmsg, HWND hwnd,
  922. WPARAM wParam, LPARAM lParam)
  923. {
  924. int i;
  925. int ret = 0;
  926. if (is_dlmsg) {
  927. if ((int)wParam == hdl->listid) {
  928. DRAGLISTINFO *dlm = (DRAGLISTINFO *)lParam;
  929. int dest = 0; /* initialise to placate gcc */
  930. switch (dlm->uNotification) {
  931. case DL_BEGINDRAG:
  932. /* Add a dummy item to make pl_itemfrompt() work
  933. * better.
  934. * FIXME: this causes scrollbar glitches if the count of
  935. * listbox contains >= its height. */
  936. hdl->dummyitem =
  937. SendDlgItemMessage(hwnd, hdl->listid,
  938. LB_ADDSTRING, 0, (LPARAM) "");
  939. hdl->srcitem = p_LBItemFromPt(dlm->hWnd, dlm->ptCursor, true);
  940. hdl->dragging = false;
  941. /* XXX hack Q183115 */
  942. SetWindowLongPtr(hwnd, DWLP_MSGRESULT, true);
  943. ret |= 1; break;
  944. case DL_CANCELDRAG:
  945. p_DrawInsert(hwnd, dlm->hWnd, -1); /* Clear arrow */
  946. SendDlgItemMessage(hwnd, hdl->listid,
  947. LB_DELETESTRING, hdl->dummyitem, 0);
  948. hdl->dragging = false;
  949. ret |= 1; break;
  950. case DL_DRAGGING:
  951. hdl->dragging = true;
  952. dest = pl_itemfrompt(dlm->hWnd, dlm->ptCursor, true);
  953. if (dest > hdl->dummyitem) dest = hdl->dummyitem;
  954. p_DrawInsert (hwnd, dlm->hWnd, dest);
  955. if (dest >= 0)
  956. SetWindowLongPtr(hwnd, DWLP_MSGRESULT, DL_MOVECURSOR);
  957. else
  958. SetWindowLongPtr(hwnd, DWLP_MSGRESULT, DL_STOPCURSOR);
  959. ret |= 1; break;
  960. case DL_DROPPED:
  961. if (hdl->dragging) {
  962. dest = pl_itemfrompt(dlm->hWnd, dlm->ptCursor, true);
  963. if (dest > hdl->dummyitem) dest = hdl->dummyitem;
  964. p_DrawInsert (hwnd, dlm->hWnd, -1);
  965. }
  966. SendDlgItemMessage(hwnd, hdl->listid,
  967. LB_DELETESTRING, hdl->dummyitem, 0);
  968. if (hdl->dragging) {
  969. hdl->dragging = false;
  970. if (dest >= 0) {
  971. /* Correct for "missing" item. */
  972. if (dest > hdl->srcitem) dest--;
  973. pl_moveitem(hwnd, hdl->listid, hdl->srcitem, dest);
  974. }
  975. ret |= 2;
  976. }
  977. ret |= 1; break;
  978. }
  979. }
  980. } else {
  981. if (((LOWORD(wParam) == hdl->upbid) ||
  982. (LOWORD(wParam) == hdl->dnbid)) &&
  983. ((HIWORD(wParam) == BN_CLICKED) ||
  984. (HIWORD(wParam) == BN_DOUBLECLICKED))) {
  985. /* Move an item up or down the list. */
  986. /* Get the current selection, if any. */
  987. int selection = SendDlgItemMessage (hwnd, hdl->listid, LB_GETCURSEL, 0, 0);
  988. if (selection == LB_ERR) {
  989. MessageBeep(0);
  990. } else {
  991. int nitems;
  992. /* Get the total number of items. */
  993. nitems = SendDlgItemMessage (hwnd, hdl->listid, LB_GETCOUNT, 0, 0);
  994. /* Should we do anything? */
  995. if (LOWORD(wParam) == hdl->upbid && (selection > 0))
  996. pl_moveitem(hwnd, hdl->listid, selection, selection - 1);
  997. else if (LOWORD(wParam) == hdl->dnbid && (selection < nitems - 1))
  998. pl_moveitem(hwnd, hdl->listid, selection, selection + 1);
  999. ret |= 2;
  1000. }
  1001. }
  1002. }
  1003. if (array) {
  1004. /* Update array to match the list box. */
  1005. for (i=0; i < maxmemb; i++)
  1006. array[i] = SendDlgItemMessage (hwnd, hdl->listid, LB_GETITEMDATA,
  1007. i, 0);
  1008. }
  1009. return ret;
  1010. }
  1011. /*
  1012. * A progress bar (from Common Controls). We like our progress bars
  1013. * to be smooth and unbroken, without those ugly divisions; some
  1014. * older compilers may not support that, but that's life.
  1015. */
  1016. void progressbar(struct ctlpos *cp, int id)
  1017. {
  1018. RECT r;
  1019. r.left = GAPBETWEEN;
  1020. r.top = cp->ypos;
  1021. r.right = cp->width;
  1022. r.bottom = PROGBARHEIGHT;
  1023. cp->ypos += r.bottom + GAPBETWEEN;
  1024. doctl(cp, r, PROGRESS_CLASS, WS_CHILD | WS_VISIBLE
  1025. #ifdef PBS_SMOOTH
  1026. | PBS_SMOOTH
  1027. #endif
  1028. , WS_EX_CLIENTEDGE, "", id);
  1029. }
  1030. /* ----------------------------------------------------------------------
  1031. * Platform-specific side of portable dialog-box mechanism.
  1032. */
  1033. /*
  1034. * This function takes a string, escapes all the ampersands, and
  1035. * places a single (unescaped) ampersand in front of the first
  1036. * occurrence of the given shortcut character (which may be
  1037. * NO_SHORTCUT).
  1038. *
  1039. * Return value is a malloc'ed copy of the processed version of the
  1040. * string.
  1041. */
  1042. static char *shortcut_escape(const char *text, char shortcut)
  1043. {
  1044. if (!text)
  1045. return NULL; /* sfree won't choke on this */
  1046. strbuf *sb = strbuf_new();
  1047. shortcut = tolower((unsigned char)shortcut);
  1048. const char *p = text;
  1049. while (*p) {
  1050. if (shortcut != NO_SHORTCUT &&
  1051. tolower((unsigned char)*p) == shortcut) {
  1052. put_byte(sb, '&');
  1053. shortcut = NO_SHORTCUT; /* stop it happening twice */
  1054. } else if (*p == '&') {
  1055. put_byte(sb, '&');
  1056. }
  1057. put_byte(sb, *p++);
  1058. }
  1059. return strbuf_to_str(sb);
  1060. }
  1061. void winctrl_add_shortcuts(struct dlgparam *dp, struct winctrl *c)
  1062. {
  1063. int i;
  1064. for (i = 0; i < lenof(c->shortcuts); i++)
  1065. if (c->shortcuts[i] != NO_SHORTCUT) {
  1066. unsigned char s = tolower((unsigned char)c->shortcuts[i]);
  1067. assert(!dp->shortcuts[s]);
  1068. dp->shortcuts[s] = true;
  1069. }
  1070. }
  1071. void winctrl_rem_shortcuts(struct dlgparam *dp, struct winctrl *c)
  1072. {
  1073. int i;
  1074. for (i = 0; i < lenof(c->shortcuts); i++)
  1075. if (c->shortcuts[i] != NO_SHORTCUT) {
  1076. unsigned char s = tolower((unsigned char)c->shortcuts[i]);
  1077. assert(dp->shortcuts[s]);
  1078. dp->shortcuts[s] = false;
  1079. }
  1080. }
  1081. static int winctrl_cmp_byctrl(void *av, void *bv)
  1082. {
  1083. struct winctrl *a = (struct winctrl *)av;
  1084. struct winctrl *b = (struct winctrl *)bv;
  1085. if (a->ctrl < b->ctrl)
  1086. return -1;
  1087. else if (a->ctrl > b->ctrl)
  1088. return +1;
  1089. else
  1090. return 0;
  1091. }
  1092. static int winctrl_cmp_byid(void *av, void *bv)
  1093. {
  1094. struct winctrl *a = (struct winctrl *)av;
  1095. struct winctrl *b = (struct winctrl *)bv;
  1096. if (a->base_id < b->base_id)
  1097. return -1;
  1098. else if (a->base_id > b->base_id)
  1099. return +1;
  1100. else
  1101. return 0;
  1102. }
  1103. static int winctrl_cmp_byctrl_find(void *av, void *bv)
  1104. {
  1105. dlgcontrol *a = (dlgcontrol *)av;
  1106. struct winctrl *b = (struct winctrl *)bv;
  1107. if (a < b->ctrl)
  1108. return -1;
  1109. else if (a > b->ctrl)
  1110. return +1;
  1111. else
  1112. return 0;
  1113. }
  1114. static int winctrl_cmp_byid_find(void *av, void *bv)
  1115. {
  1116. int *a = (int *)av;
  1117. struct winctrl *b = (struct winctrl *)bv;
  1118. if (*a < b->base_id)
  1119. return -1;
  1120. else if (*a >= b->base_id + b->num_ids)
  1121. return +1;
  1122. else
  1123. return 0;
  1124. }
  1125. void winctrl_init(struct winctrls *wc)
  1126. {
  1127. wc->byctrl = newtree234(winctrl_cmp_byctrl);
  1128. wc->byid = newtree234(winctrl_cmp_byid);
  1129. }
  1130. void winctrl_cleanup(struct winctrls *wc)
  1131. {
  1132. struct winctrl *c;
  1133. while ((c = index234(wc->byid, 0)) != NULL) {
  1134. winctrl_remove(wc, c);
  1135. sfree(c->data);
  1136. sfree(c);
  1137. }
  1138. freetree234(wc->byctrl);
  1139. freetree234(wc->byid);
  1140. wc->byctrl = wc->byid = NULL;
  1141. }
  1142. void winctrl_add(struct winctrls *wc, struct winctrl *c)
  1143. {
  1144. struct winctrl *ret;
  1145. if (c->ctrl) {
  1146. ret = add234(wc->byctrl, c);
  1147. assert(ret == c);
  1148. }
  1149. ret = add234(wc->byid, c);
  1150. assert(ret == c);
  1151. }
  1152. void winctrl_remove(struct winctrls *wc, struct winctrl *c)
  1153. {
  1154. struct winctrl *ret;
  1155. ret = del234(wc->byctrl, c);
  1156. ret = del234(wc->byid, c);
  1157. assert(ret == c);
  1158. }
  1159. struct winctrl *winctrl_findbyctrl(struct winctrls *wc, dlgcontrol *ctrl)
  1160. {
  1161. return find234(wc->byctrl, ctrl, winctrl_cmp_byctrl_find);
  1162. }
  1163. struct winctrl *winctrl_findbyid(struct winctrls *wc, int id)
  1164. {
  1165. return find234(wc->byid, &id, winctrl_cmp_byid_find);
  1166. }
  1167. struct winctrl *winctrl_findbyindex(struct winctrls *wc, int index)
  1168. {
  1169. return index234(wc->byid, index);
  1170. }
  1171. static void move_windows(HWND hwnd, int base_id, int num_ids, LONG dy)
  1172. {
  1173. if (!dy)
  1174. return;
  1175. for (int i = 0; i < num_ids; i++) {
  1176. HWND win = GetDlgItem(hwnd, base_id + i);
  1177. RECT rect;
  1178. if (!GetWindowRect(win, &rect))
  1179. continue;
  1180. POINT p;
  1181. p.x = rect.left;
  1182. p.y = rect.top + dy;
  1183. if (!ScreenToClient(hwnd, &p))
  1184. continue;
  1185. SetWindowPos(win, NULL, p.x, p.y, 0, 0,
  1186. SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
  1187. }
  1188. }
  1189. void winctrl_layout(struct dlgparam *dp, struct winctrls *wc,
  1190. struct ctlpos *cp, struct controlset *s, int *id)
  1191. {
  1192. struct ctlpos columns[16];
  1193. int ncols, colstart, colspan;
  1194. struct ctlpos tabdelays[16];
  1195. dlgcontrol *tabdelayed[16];
  1196. int ntabdelays;
  1197. struct ctlpos pos;
  1198. char shortcuts[MAX_SHORTCUTS_PER_CTRL];
  1199. int nshortcuts;
  1200. char *escaped;
  1201. int i, actual_base_id, base_id, num_ids, align_id_relative;
  1202. void *data;
  1203. base_id = *id;
  1204. ctrlset_normalise_aligns(s);
  1205. /* Start a containing box, if we have a boxname. */
  1206. if (s->boxname && *s->boxname) {
  1207. struct winctrl *c = snew(struct winctrl);
  1208. c->ctrl = NULL;
  1209. c->base_id = c->align_id = base_id;
  1210. c->num_ids = 1;
  1211. c->data = NULL;
  1212. memset(c->shortcuts, NO_SHORTCUT, lenof(c->shortcuts));
  1213. winctrl_add(wc, c);
  1214. beginbox(cp, s->boxtitle, base_id);
  1215. base_id++;
  1216. }
  1217. /* Draw a title, if we have one. */
  1218. if (!s->boxname && s->boxtitle) {
  1219. struct winctrl *c = snew(struct winctrl);
  1220. c->ctrl = NULL;
  1221. c->base_id = c->align_id = base_id;
  1222. c->num_ids = 1;
  1223. c->data = dupstr(s->boxtitle);
  1224. memset(c->shortcuts, NO_SHORTCUT, lenof(c->shortcuts));
  1225. winctrl_add(wc, c);
  1226. paneltitle(cp, base_id);
  1227. base_id++;
  1228. }
  1229. /* Initially we have just one column. */
  1230. ncols = 1;
  1231. columns[0] = *cp; /* structure copy */
  1232. /* And initially, there are no pending tab-delayed controls. */
  1233. ntabdelays = 0;
  1234. /* Loop over each control in the controlset. */
  1235. for (i = 0; i < s->ncontrols; i++) {
  1236. dlgcontrol *ctrl = s->ctrls[i];
  1237. /*
  1238. * Generic processing that pertains to all control types.
  1239. * At the end of this if statement, we'll have produced
  1240. * `ctrl' (a pointer to the control we have to create, or
  1241. * think about creating, in this iteration of the loop),
  1242. * `pos' (a suitable ctlpos with which to position it), and
  1243. * `c' (a winctrl structure to receive details of the
  1244. * dialog IDs). Or we'll have done a `continue', if it was
  1245. * CTRL_COLUMNS and doesn't require any control creation at
  1246. * all.
  1247. */
  1248. if (ctrl->type == CTRL_COLUMNS) {
  1249. assert((ctrl->columns.ncols == 1) ^ (ncols == 1));
  1250. if (ncols == 1) {
  1251. /*
  1252. * We're splitting into multiple columns.
  1253. */
  1254. int lpercent, rpercent, lx, rx, i;
  1255. ncols = ctrl->columns.ncols;
  1256. assert(ncols <= lenof(columns));
  1257. for (i = 1; i < ncols; i++)
  1258. columns[i] = columns[0]; /* structure copy */
  1259. lpercent = 0;
  1260. for (i = 0; i < ncols; i++) {
  1261. rpercent = lpercent + ctrl->columns.percentages[i];
  1262. lx = columns[i].xoff + lpercent *
  1263. (columns[i].width + GAPBETWEEN) / 100;
  1264. rx = columns[i].xoff + rpercent *
  1265. (columns[i].width + GAPBETWEEN) / 100;
  1266. columns[i].xoff = lx;
  1267. columns[i].width = rx - lx - GAPBETWEEN;
  1268. lpercent = rpercent;
  1269. }
  1270. } else {
  1271. /*
  1272. * We're recombining the various columns into one.
  1273. */
  1274. int maxy = columns[0].ypos;
  1275. int i;
  1276. for (i = 1; i < ncols; i++)
  1277. if (maxy < columns[i].ypos)
  1278. maxy = columns[i].ypos;
  1279. ncols = 1;
  1280. columns[0] = *cp; /* structure copy */
  1281. columns[0].ypos = maxy;
  1282. }
  1283. continue;
  1284. } else if (ctrl->type == CTRL_TABDELAY) {
  1285. int i;
  1286. assert(!ctrl->delay_taborder);
  1287. ctrl = ctrl->tabdelay.ctrl;
  1288. for (i = 0; i < ntabdelays; i++)
  1289. if (tabdelayed[i] == ctrl)
  1290. break;
  1291. assert(i < ntabdelays); /* we have to have found it */
  1292. pos = tabdelays[i]; /* structure copy */
  1293. colstart = colspan = -1; /* indicate this was tab-delayed */
  1294. } else {
  1295. /*
  1296. * If it wasn't one of those, it's a genuine control;
  1297. * so we'll have to compute a position for it now, by
  1298. * checking its column span.
  1299. */
  1300. int col;
  1301. colstart = COLUMN_START(ctrl->column);
  1302. colspan = COLUMN_SPAN(ctrl->column);
  1303. pos = columns[colstart]; /* structure copy */
  1304. pos.width = columns[colstart+colspan-1].width +
  1305. (columns[colstart+colspan-1].xoff - columns[colstart].xoff);
  1306. for (col = colstart; col < colstart+colspan; col++)
  1307. if (pos.ypos < columns[col].ypos)
  1308. pos.ypos = columns[col].ypos;
  1309. /*
  1310. * If this control is to be tabdelayed, add it to the
  1311. * tabdelay list, and unset pos.hwnd to inhibit actual
  1312. * control creation.
  1313. */
  1314. if (ctrl->delay_taborder) {
  1315. assert(ntabdelays < lenof(tabdelays));
  1316. tabdelays[ntabdelays] = pos; /* structure copy */
  1317. tabdelayed[ntabdelays] = ctrl;
  1318. ntabdelays++;
  1319. pos.hwnd = NULL;
  1320. }
  1321. }
  1322. /* Most controls don't need anything in c->data. */
  1323. data = NULL;
  1324. /* And they all start off with no shortcuts registered. */
  1325. memset(shortcuts, NO_SHORTCUT, lenof(shortcuts));
  1326. nshortcuts = 0;
  1327. /* Almost all controls start at base_id. */
  1328. actual_base_id = base_id;
  1329. /* For vertical alignment purposes, the most relevant control
  1330. * in a group is usually the last one. But that can be
  1331. * overridden occasionally. */
  1332. align_id_relative = -1;
  1333. /*
  1334. * Now we're ready to actually create the control, by
  1335. * switching on its type.
  1336. */
  1337. switch (ctrl->type) {
  1338. case CTRL_TEXT:
  1339. if (ctrl->text.wrap) {
  1340. char *wrapped, *escaped;
  1341. int lines;
  1342. num_ids = 1;
  1343. wrapped = staticwrap(&pos, cp->hwnd,
  1344. ctrl->label, &lines);
  1345. escaped = shortcut_escape(wrapped, NO_SHORTCUT);
  1346. statictext(&pos, escaped, lines, base_id);
  1347. sfree(escaped);
  1348. sfree(wrapped);
  1349. } else {
  1350. num_ids = 1;
  1351. editboxfw(&pos, false, true, NULL, 0, base_id);
  1352. SetDlgItemText(pos.hwnd, base_id, ctrl->label);
  1353. MakeDlgItemBorderless(pos.hwnd, base_id);
  1354. }
  1355. break;
  1356. case CTRL_EDITBOX:
  1357. num_ids = 2; /* static, edit */
  1358. escaped = shortcut_escape(ctrl->label,
  1359. ctrl->editbox.shortcut);
  1360. shortcuts[nshortcuts++] = ctrl->editbox.shortcut;
  1361. if (ctrl->editbox.percentwidth == 100) {
  1362. if (ctrl->editbox.has_list)
  1363. combobox(&pos, escaped,
  1364. base_id, base_id+1);
  1365. else
  1366. editboxfw(&pos, ctrl->editbox.password, false, escaped,
  1367. base_id, base_id+1);
  1368. } else {
  1369. if (ctrl->editbox.has_list) {
  1370. staticcombo(&pos, escaped, base_id, base_id+1,
  1371. ctrl->editbox.percentwidth);
  1372. } else {
  1373. (ctrl->editbox.password ? staticpassedit : staticedit)
  1374. (&pos, escaped, base_id, base_id+1,
  1375. ctrl->editbox.percentwidth);
  1376. }
  1377. }
  1378. sfree(escaped);
  1379. break;
  1380. case CTRL_RADIO: {
  1381. num_ids = ctrl->radio.nbuttons + 1; /* label as well */
  1382. struct radio *buttons;
  1383. int i;
  1384. escaped = shortcut_escape(ctrl->label,
  1385. ctrl->radio.shortcut);
  1386. shortcuts[nshortcuts++] = ctrl->radio.shortcut;
  1387. buttons = snewn(ctrl->radio.nbuttons, struct radio);
  1388. for (i = 0; i < ctrl->radio.nbuttons; i++) {
  1389. buttons[i].text =
  1390. shortcut_escape(ctrl->radio.buttons[i],
  1391. (char)(ctrl->radio.shortcuts ?
  1392. ctrl->radio.shortcuts[i] :
  1393. NO_SHORTCUT));
  1394. buttons[i].id = base_id + 1 + i;
  1395. if (ctrl->radio.shortcuts) {
  1396. assert(nshortcuts < MAX_SHORTCUTS_PER_CTRL);
  1397. shortcuts[nshortcuts++] = ctrl->radio.shortcuts[i];
  1398. }
  1399. }
  1400. radioline_common(&pos, escaped, base_id,
  1401. ctrl->radio.ncolumns,
  1402. buttons, ctrl->radio.nbuttons);
  1403. for (i = 0; i < ctrl->radio.nbuttons; i++) {
  1404. sfree((char *)buttons[i].text);
  1405. }
  1406. sfree(buttons);
  1407. sfree(escaped);
  1408. break;
  1409. }
  1410. case CTRL_CHECKBOX:
  1411. num_ids = 1;
  1412. escaped = shortcut_escape(ctrl->label,
  1413. ctrl->checkbox.shortcut);
  1414. shortcuts[nshortcuts++] = ctrl->checkbox.shortcut;
  1415. checkbox(&pos, escaped, base_id);
  1416. sfree(escaped);
  1417. break;
  1418. case CTRL_BUTTON:
  1419. escaped = shortcut_escape(ctrl->label,
  1420. ctrl->button.shortcut);
  1421. shortcuts[nshortcuts++] = ctrl->button.shortcut;
  1422. if (ctrl->button.iscancel)
  1423. actual_base_id = IDCANCEL;
  1424. num_ids = 1;
  1425. button(&pos, escaped, actual_base_id, ctrl->button.isdefault);
  1426. sfree(escaped);
  1427. break;
  1428. case CTRL_LISTBOX:
  1429. num_ids = 2;
  1430. escaped = shortcut_escape(ctrl->label,
  1431. ctrl->listbox.shortcut);
  1432. shortcuts[nshortcuts++] = ctrl->listbox.shortcut;
  1433. if (ctrl->listbox.draglist) {
  1434. data = snew(struct prefslist);
  1435. num_ids = 4;
  1436. prefslist(data, &pos, ctrl->listbox.height, escaped,
  1437. base_id, base_id+1, base_id+2, base_id+3);
  1438. shortcuts[nshortcuts++] = 'u'; /* Up */
  1439. shortcuts[nshortcuts++] = 'd'; /* Down */
  1440. } else if (ctrl->listbox.height == 0) {
  1441. /* Drop-down list. */
  1442. if (ctrl->listbox.percentwidth == 100) {
  1443. staticddlbig(&pos, escaped,
  1444. base_id, base_id+1);
  1445. } else {
  1446. staticddl(&pos, escaped, base_id,
  1447. base_id+1, ctrl->listbox.percentwidth);
  1448. }
  1449. } else {
  1450. /* Ordinary list. */
  1451. listbox(&pos, escaped, base_id, base_id+1,
  1452. ctrl->listbox.height, ctrl->listbox.multisel);
  1453. }
  1454. if (ctrl->listbox.ncols) {
  1455. /*
  1456. * This method of getting the box width is a bit of
  1457. * a hack; we'd do better to try to retrieve the
  1458. * actual width in dialog units from doctl() just
  1459. * before MapDialogRect. But that's going to be no
  1460. * fun, and this should be good enough accuracy.
  1461. */
  1462. int width = cp->width * ctrl->listbox.percentwidth;
  1463. int *tabarray;
  1464. int i, percent;
  1465. tabarray = snewn(ctrl->listbox.ncols-1, int);
  1466. percent = 0;
  1467. for (i = 0; i < ctrl->listbox.ncols-1; i++) {
  1468. percent += ctrl->listbox.percentages[i];
  1469. tabarray[i] = width * percent / 10000;
  1470. }
  1471. SendDlgItemMessage(cp->hwnd, base_id+1, LB_SETTABSTOPS,
  1472. ctrl->listbox.ncols-1, (LPARAM)tabarray);
  1473. sfree(tabarray);
  1474. }
  1475. sfree(escaped);
  1476. break;
  1477. case CTRL_FILESELECT:
  1478. escaped = shortcut_escape(ctrl->label, ctrl->fileselect.shortcut);
  1479. shortcuts[nshortcuts++] = ctrl->fileselect.shortcut;
  1480. num_ids = 3;
  1481. if (!ctrl->fileselect.just_button) {
  1482. editbutton(&pos, escaped, base_id, base_id+1,
  1483. "Browse...", base_id+2);
  1484. } else {
  1485. button(&pos, escaped, base_id+2, false);
  1486. }
  1487. sfree(escaped);
  1488. break;
  1489. case CTRL_FONTSELECT:
  1490. num_ids = 3;
  1491. escaped = shortcut_escape(ctrl->label,
  1492. ctrl->fontselect.shortcut);
  1493. shortcuts[nshortcuts++] = ctrl->fontselect.shortcut;
  1494. statictext(&pos, escaped, 1, base_id);
  1495. staticbtn(&pos, "", base_id+1, "Change...", base_id+2);
  1496. data = fontspec_new_default();
  1497. sfree(escaped);
  1498. break;
  1499. default:
  1500. unreachable("bad control type in winctrl_layout");
  1501. }
  1502. /* Translate the original align_id_relative of -1 into n-1 */
  1503. if (align_id_relative < 0)
  1504. align_id_relative += num_ids;
  1505. /*
  1506. * Create a `struct winctrl' for this control, and advance
  1507. * the dialog ID counter, if it's actually been created
  1508. * (and isn't tabdelayed).
  1509. */
  1510. if (pos.hwnd) {
  1511. struct winctrl *c = snew(struct winctrl);
  1512. c->ctrl = ctrl;
  1513. c->base_id = actual_base_id;
  1514. c->align_id = c->base_id + align_id_relative;
  1515. c->num_ids = num_ids;
  1516. c->data = data;
  1517. memcpy(c->shortcuts, shortcuts, sizeof(shortcuts));
  1518. winctrl_add(wc, c);
  1519. winctrl_add_shortcuts(dp, c);
  1520. if (actual_base_id == base_id)
  1521. base_id += num_ids;
  1522. if (ctrl->align_next_to) {
  1523. /*
  1524. * Implement align_next_to by looking at the y extents
  1525. * of the two controls now that both are created, and
  1526. * moving one or the other downwards so that they're
  1527. * centred on a common horizontal line.
  1528. */
  1529. LONG mid2 = 0;
  1530. for (dlgcontrol *thisctrl = ctrl; thisctrl;
  1531. thisctrl = thisctrl->align_next_to) {
  1532. struct winctrl *thisc = winctrl_findbyctrl(wc, thisctrl);
  1533. assert(thisc);
  1534. HWND win = GetDlgItem(pos.hwnd, thisc->align_id);
  1535. assert(win);
  1536. RECT rect;
  1537. if (!GetWindowRect(win, &rect))
  1538. continue;
  1539. if (mid2 < rect.top + rect.bottom)
  1540. mid2 = rect.top + rect.bottom;
  1541. }
  1542. for (dlgcontrol *thisctrl = ctrl; thisctrl;
  1543. thisctrl = thisctrl->align_next_to) {
  1544. struct winctrl *thisc = winctrl_findbyctrl(wc, thisctrl);
  1545. assert(thisc);
  1546. HWND win = GetDlgItem(pos.hwnd, thisc->align_id);
  1547. assert(win);
  1548. RECT rect;
  1549. if (!GetWindowRect(win, &rect))
  1550. continue;
  1551. LONG dy = (mid2 - (rect.top + rect.bottom)) / 2;
  1552. move_windows(pos.hwnd, thisc->base_id, thisc->num_ids, dy);
  1553. }
  1554. }
  1555. } else {
  1556. sfree(data);
  1557. }
  1558. if (colstart >= 0) {
  1559. /*
  1560. * Update the ypos in all columns crossed by this
  1561. * control.
  1562. */
  1563. int i;
  1564. for (i = colstart; i < colstart+colspan; i++)
  1565. columns[i].ypos = pos.ypos;
  1566. }
  1567. }
  1568. /*
  1569. * We've now finished laying out the controls; so now update
  1570. * the ctlpos and control ID that were passed in, terminate
  1571. * any containing box, and return.
  1572. */
  1573. for (i = 0; i < ncols; i++)
  1574. if (cp->ypos < columns[i].ypos)
  1575. cp->ypos = columns[i].ypos;
  1576. *id = base_id;
  1577. if (s->boxname && *s->boxname)
  1578. endbox(cp);
  1579. }
  1580. static void winctrl_set_focus(dlgcontrol *ctrl, struct dlgparam *dp,
  1581. bool has_focus)
  1582. {
  1583. if (has_focus) {
  1584. if (dp->focused)
  1585. dp->lastfocused = dp->focused;
  1586. dp->focused = ctrl;
  1587. } else if (!has_focus && dp->focused == ctrl) {
  1588. dp->lastfocused = dp->focused;
  1589. dp->focused = NULL;
  1590. }
  1591. }
  1592. dlgcontrol *dlg_last_focused(dlgcontrol *ctrl, dlgparam *dp)
  1593. {
  1594. return dp->focused == ctrl ? dp->lastfocused : dp->focused;
  1595. }
  1596. /*
  1597. * The dialog-box procedure calls this function to handle Windows
  1598. * messages on a control we manage.
  1599. */
  1600. bool winctrl_handle_command(struct dlgparam *dp, UINT msg,
  1601. WPARAM wParam, LPARAM lParam)
  1602. {
  1603. struct winctrl *c;
  1604. dlgcontrol *ctrl;
  1605. int i, id;
  1606. bool ret;
  1607. static UINT draglistmsg = WM_NULL;
  1608. /*
  1609. * Filter out pointless window messages. Our interest is in
  1610. * WM_COMMAND and the drag list message, and nothing else.
  1611. */
  1612. if (draglistmsg == WM_NULL)
  1613. draglistmsg = RegisterWindowMessage (DRAGLISTMSGSTRING);
  1614. if (msg != draglistmsg && msg != WM_COMMAND && msg != WM_DRAWITEM)
  1615. return false;
  1616. /*
  1617. * Look up the control ID in our data.
  1618. */
  1619. c = NULL;
  1620. for (i = 0; i < dp->nctrltrees; i++) {
  1621. c = winctrl_findbyid(dp->controltrees[i], LOWORD(wParam));
  1622. if (c)
  1623. break;
  1624. }
  1625. if (!c)
  1626. return false; /* we have nothing to do */
  1627. if (msg == WM_DRAWITEM) {
  1628. /*
  1629. * Owner-draw request for a panel title.
  1630. */
  1631. LPDRAWITEMSTRUCT di = (LPDRAWITEMSTRUCT) lParam;
  1632. HDC hdc = di->hDC;
  1633. RECT r = di->rcItem;
  1634. SIZE s;
  1635. SetMapMode(hdc, MM_TEXT); /* ensure logical units == pixels */
  1636. GetTextExtentPoint32(hdc, (char *)c->data,
  1637. strlen((char *)c->data), &s);
  1638. DrawEdge(hdc, &r, EDGE_ETCHED, BF_ADJUST | BF_RECT);
  1639. TextOut(hdc,
  1640. r.left + (r.right-r.left-s.cx)/2,
  1641. r.top + (r.bottom-r.top-s.cy)/2,
  1642. (char *)c->data, strlen((char *)c->data));
  1643. return true;
  1644. }
  1645. ctrl = c->ctrl;
  1646. id = LOWORD(wParam) - c->base_id;
  1647. if (!ctrl || !ctrl->handler)
  1648. return false; /* nothing we can do here */
  1649. /*
  1650. * From here on we do not issue `return' statements until the
  1651. * very end of the dialog box: any event handler is entitled to
  1652. * ask for a colour selector, so we _must_ always allow control
  1653. * to reach the end of this switch statement so that the
  1654. * subsequent code can test dp->coloursel_wanted().
  1655. */
  1656. ret = false;
  1657. dp->coloursel_wanted = false;
  1658. /*
  1659. * Now switch on the control type and the message.
  1660. */
  1661. switch (ctrl->type) {
  1662. case CTRL_EDITBOX:
  1663. if (msg == WM_COMMAND && !ctrl->editbox.has_list &&
  1664. (HIWORD(wParam) == EN_SETFOCUS || HIWORD(wParam) == EN_KILLFOCUS))
  1665. winctrl_set_focus(ctrl, dp, HIWORD(wParam) == EN_SETFOCUS);
  1666. if (msg == WM_COMMAND && ctrl->editbox.has_list &&
  1667. (HIWORD(wParam)==CBN_SETFOCUS || HIWORD(wParam)==CBN_KILLFOCUS))
  1668. winctrl_set_focus(ctrl, dp, HIWORD(wParam) == CBN_SETFOCUS);
  1669. if (msg == WM_COMMAND && !ctrl->editbox.has_list &&
  1670. HIWORD(wParam) == EN_CHANGE)
  1671. ctrl->handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
  1672. if (msg == WM_COMMAND &&
  1673. ctrl->editbox.has_list) {
  1674. if (HIWORD(wParam) == CBN_SELCHANGE) {
  1675. int index, len;
  1676. char *text;
  1677. index = SendDlgItemMessage(dp->hwnd, c->base_id+1,
  1678. CB_GETCURSEL, 0, 0);
  1679. len = SendDlgItemMessage(dp->hwnd, c->base_id+1,
  1680. CB_GETLBTEXTLEN, index, 0);
  1681. text = snewn(len+1, char);
  1682. SendDlgItemMessage(dp->hwnd, c->base_id+1, CB_GETLBTEXT,
  1683. index, (LPARAM)text);
  1684. SetDlgItemText(dp->hwnd, c->base_id+1, text);
  1685. sfree(text);
  1686. ctrl->handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
  1687. } else if (HIWORD(wParam) == CBN_EDITCHANGE) {
  1688. ctrl->handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
  1689. } else if (HIWORD(wParam) == CBN_KILLFOCUS) {
  1690. ctrl->handler(ctrl, dp, dp->data, EVENT_REFRESH);
  1691. }
  1692. }
  1693. break;
  1694. case CTRL_RADIO:
  1695. if (msg == WM_COMMAND &&
  1696. (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
  1697. winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
  1698. /*
  1699. * We sometimes get spurious BN_CLICKED messages for the
  1700. * radio button that is just about to _lose_ selection, if
  1701. * we're switching using the arrow keys. Therefore we
  1702. * double-check that the button in wParam is actually
  1703. * checked before generating an event.
  1704. */
  1705. if (msg == WM_COMMAND &&
  1706. (HIWORD(wParam) == BN_CLICKED ||
  1707. HIWORD(wParam) == BN_DOUBLECLICKED) &&
  1708. IsDlgButtonChecked(dp->hwnd, LOWORD(wParam))) {
  1709. ctrl->handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
  1710. }
  1711. break;
  1712. case CTRL_CHECKBOX:
  1713. if (msg == WM_COMMAND &&
  1714. (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
  1715. winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
  1716. if (msg == WM_COMMAND &&
  1717. (HIWORD(wParam) == BN_CLICKED ||
  1718. HIWORD(wParam) == BN_DOUBLECLICKED)) {
  1719. ctrl->handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
  1720. }
  1721. break;
  1722. case CTRL_BUTTON:
  1723. if (msg == WM_COMMAND &&
  1724. (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
  1725. winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
  1726. if (msg == WM_COMMAND &&
  1727. (HIWORD(wParam) == BN_CLICKED ||
  1728. HIWORD(wParam) == BN_DOUBLECLICKED)) {
  1729. ctrl->handler(ctrl, dp, dp->data, EVENT_ACTION);
  1730. }
  1731. break;
  1732. case CTRL_LISTBOX:
  1733. if (msg == WM_COMMAND && ctrl->listbox.height != 0 &&
  1734. (HIWORD(wParam)==LBN_SETFOCUS || HIWORD(wParam)==LBN_KILLFOCUS))
  1735. winctrl_set_focus(ctrl, dp, HIWORD(wParam) == LBN_SETFOCUS);
  1736. if (msg == WM_COMMAND && ctrl->listbox.height == 0 &&
  1737. (HIWORD(wParam)==CBN_SETFOCUS || HIWORD(wParam)==CBN_KILLFOCUS))
  1738. winctrl_set_focus(ctrl, dp, HIWORD(wParam) == CBN_SETFOCUS);
  1739. if (msg == WM_COMMAND && id >= 2 &&
  1740. (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
  1741. winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
  1742. if (ctrl->listbox.draglist) {
  1743. int pret;
  1744. pret = handle_prefslist(c->data, NULL, 0, (msg != WM_COMMAND),
  1745. dp->hwnd, wParam, lParam);
  1746. if (pret & 2)
  1747. ctrl->handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
  1748. ret = pret & 1;
  1749. } else {
  1750. if (msg == WM_COMMAND && HIWORD(wParam) == LBN_DBLCLK) {
  1751. SetCapture(dp->hwnd);
  1752. ctrl->handler(ctrl, dp, dp->data, EVENT_ACTION);
  1753. } else if (msg == WM_COMMAND && HIWORD(wParam) == LBN_SELCHANGE) {
  1754. ctrl->handler(ctrl, dp, dp->data, EVENT_SELCHANGE);
  1755. }
  1756. }
  1757. break;
  1758. case CTRL_FILESELECT:
  1759. if (msg == WM_COMMAND && id == 1 &&
  1760. (HIWORD(wParam) == EN_SETFOCUS || HIWORD(wParam) == EN_KILLFOCUS))
  1761. winctrl_set_focus(ctrl, dp, HIWORD(wParam) == EN_SETFOCUS);
  1762. if (msg == WM_COMMAND && id == 2 &&
  1763. (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
  1764. winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
  1765. if (msg == WM_COMMAND && id == 1 && HIWORD(wParam) == EN_CHANGE)
  1766. ctrl->handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
  1767. if (id == 2 &&
  1768. (msg == WM_COMMAND &&
  1769. (HIWORD(wParam) == BN_CLICKED ||
  1770. HIWORD(wParam) == BN_DOUBLECLICKED))) {
  1771. OPENFILENAMEW of;
  1772. wchar_t filename[FILENAME_MAX];
  1773. wchar_t *title_to_free = NULL;
  1774. memset(&of, 0, sizeof(of));
  1775. of.hwndOwner = dp->hwnd;
  1776. if (ctrl->fileselect.filter)
  1777. of.lpstrFilter = ctrl->fileselect.filter;
  1778. else
  1779. of.lpstrFilter = L"All Files (*.*)\0*\0\0\0";
  1780. of.lpstrCustomFilter = NULL;
  1781. of.nFilterIndex = 1;
  1782. of.lpstrFile = filename;
  1783. if (!ctrl->fileselect.just_button) {
  1784. GetDlgItemTextW(dp->hwnd, c->base_id+1,
  1785. filename, lenof(filename));
  1786. filename[lenof(filename)-1] = L'\0';
  1787. } else {
  1788. *filename = L'\0';
  1789. }
  1790. of.nMaxFile = lenof(filename);
  1791. of.lpstrFileTitle = NULL;
  1792. of.lpstrTitle = title_to_free = dup_mb_to_wc(
  1793. DEFAULT_CODEPAGE, 0, ctrl->fileselect.title);
  1794. of.Flags = 0;
  1795. if (request_file_w(NULL, &of, false,
  1796. ctrl->fileselect.for_writing)) {
  1797. if (!ctrl->fileselect.just_button) {
  1798. SetDlgItemTextW(dp->hwnd, c->base_id + 1, filename);
  1799. ctrl->handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
  1800. } else {
  1801. assert(!c->data);
  1802. c->data = filename;
  1803. ctrl->handler(ctrl, dp, dp->data, EVENT_ACTION);
  1804. c->data = NULL;
  1805. }
  1806. }
  1807. sfree(title_to_free);
  1808. }
  1809. break;
  1810. case CTRL_FONTSELECT:
  1811. if (msg == WM_COMMAND && id == 2 &&
  1812. (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
  1813. winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
  1814. if (id == 2 &&
  1815. (msg == WM_COMMAND &&
  1816. (HIWORD(wParam) == BN_CLICKED ||
  1817. HIWORD(wParam) == BN_DOUBLECLICKED))) {
  1818. CHOOSEFONT cf;
  1819. LOGFONT lf;
  1820. HDC hdc;
  1821. FontSpec *fs = (FontSpec *)c->data;
  1822. hdc = GetDC(0);
  1823. lf.lfHeight = -MulDiv(fs->height,
  1824. GetDeviceCaps(hdc, LOGPIXELSY), 72);
  1825. ReleaseDC(0, hdc);
  1826. lf.lfWidth = lf.lfEscapement = lf.lfOrientation = 0;
  1827. lf.lfItalic = lf.lfUnderline = lf.lfStrikeOut = 0;
  1828. lf.lfWeight = (fs->isbold ? FW_BOLD : 0);
  1829. lf.lfCharSet = fs->charset;
  1830. lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
  1831. lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  1832. lf.lfQuality = DEFAULT_QUALITY;
  1833. lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
  1834. strncpy(lf.lfFaceName, fs->name,
  1835. sizeof(lf.lfFaceName) - 1);
  1836. lf.lfFaceName[sizeof(lf.lfFaceName) - 1] = '\0';
  1837. cf.lStructSize = sizeof(cf);
  1838. cf.hwndOwner = dp->hwnd;
  1839. cf.lpLogFont = &lf;
  1840. cf.Flags = (dp->fixed_pitch_fonts ? CF_FIXEDPITCHONLY : 0) |
  1841. CF_FORCEFONTEXIST | CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS;
  1842. if (ChooseFont(&cf)) {
  1843. fs = fontspec_new(lf.lfFaceName, (lf.lfWeight == FW_BOLD),
  1844. cf.iPointSize / 10, lf.lfCharSet);
  1845. dlg_fontsel_set(ctrl, dp, fs);
  1846. fontspec_free(fs);
  1847. ctrl->handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
  1848. }
  1849. }
  1850. break;
  1851. }
  1852. /*
  1853. * If the above event handler has asked for a colour selector,
  1854. * now is the time to generate one.
  1855. */
  1856. if (dp->coloursel_wanted) {
  1857. static CHOOSECOLOR cc;
  1858. static DWORD custom[16] = { 0 }; /* zero initialisers */
  1859. cc.lStructSize = sizeof(cc);
  1860. cc.hwndOwner = dp->hwnd;
  1861. cc.hInstance = (HWND) hinst;
  1862. cc.lpCustColors = custom;
  1863. cc.rgbResult = RGB(dp->coloursel_result.r,
  1864. dp->coloursel_result.g,
  1865. dp->coloursel_result.b);
  1866. cc.Flags = CC_FULLOPEN | CC_RGBINIT;
  1867. if (ChooseColor(&cc)) {
  1868. dp->coloursel_result.r =
  1869. (unsigned char) (cc.rgbResult & 0xFF);
  1870. dp->coloursel_result.g =
  1871. (unsigned char) (cc.rgbResult >> 8) & 0xFF;
  1872. dp->coloursel_result.b =
  1873. (unsigned char) (cc.rgbResult >> 16) & 0xFF;
  1874. dp->coloursel_result.ok = true;
  1875. } else
  1876. dp->coloursel_result.ok = false;
  1877. ctrl->handler(ctrl, dp, dp->data, EVENT_CALLBACK);
  1878. }
  1879. return ret;
  1880. }
  1881. /*
  1882. * This function can be called to produce context help on a
  1883. * control. Returns true if it has actually launched some help.
  1884. */
  1885. bool winctrl_context_help(struct dlgparam *dp, HWND hwnd, int id)
  1886. {
  1887. int i;
  1888. struct winctrl *c;
  1889. /*
  1890. * Look up the control ID in our data.
  1891. */
  1892. c = NULL;
  1893. for (i = 0; i < dp->nctrltrees; i++) {
  1894. c = winctrl_findbyid(dp->controltrees[i], id);
  1895. if (c)
  1896. break;
  1897. }
  1898. if (!c)
  1899. return false; /* we have nothing to do */
  1900. /*
  1901. * This is the Windows front end, so we're allowed to assume
  1902. * `helpctx' is a context string.
  1903. */
  1904. if (!c->ctrl || !c->ctrl->helpctx)
  1905. return false; /* no help available for this ctrl */
  1906. launch_help(hwnd, c->ctrl->helpctx);
  1907. return true;
  1908. }
  1909. /*
  1910. * Now the various functions that the platform-independent
  1911. * mechanism can call to access the dialog box entries.
  1912. */
  1913. static struct winctrl *dlg_findbyctrl(struct dlgparam *dp, dlgcontrol *ctrl)
  1914. {
  1915. int i;
  1916. for (i = 0; i < dp->nctrltrees; i++) {
  1917. struct winctrl *c = winctrl_findbyctrl(dp->controltrees[i], ctrl);
  1918. if (c)
  1919. return c;
  1920. }
  1921. return NULL;
  1922. }
  1923. bool dlg_is_visible(dlgcontrol *ctrl, dlgparam *dp)
  1924. {
  1925. /*
  1926. * In this implementation of the dialog box, we physically
  1927. * uncreate controls that aren't in a visible panel of the config
  1928. * box. So we can tell if a control is visible just by checking if
  1929. * it _exists_.
  1930. */
  1931. return dlg_findbyctrl(dp, ctrl) != NULL;
  1932. }
  1933. void dlg_radiobutton_set(dlgcontrol *ctrl, dlgparam *dp, int whichbutton)
  1934. {
  1935. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  1936. assert(c && c->ctrl->type == CTRL_RADIO);
  1937. CheckRadioButton(dp->hwnd,
  1938. c->base_id + 1,
  1939. c->base_id + c->ctrl->radio.nbuttons,
  1940. c->base_id + 1 + whichbutton);
  1941. }
  1942. int dlg_radiobutton_get(dlgcontrol *ctrl, dlgparam *dp)
  1943. {
  1944. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  1945. int i;
  1946. assert(c && c->ctrl->type == CTRL_RADIO);
  1947. for (i = 0; i < c->ctrl->radio.nbuttons; i++)
  1948. if (IsDlgButtonChecked(dp->hwnd, c->base_id + 1 + i))
  1949. return i;
  1950. unreachable("no radio button was checked");
  1951. }
  1952. void dlg_checkbox_set(dlgcontrol *ctrl, dlgparam *dp, bool checked)
  1953. {
  1954. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  1955. assert(c && c->ctrl->type == CTRL_CHECKBOX);
  1956. CheckDlgButton(dp->hwnd, c->base_id, checked);
  1957. }
  1958. bool dlg_checkbox_get(dlgcontrol *ctrl, dlgparam *dp)
  1959. {
  1960. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  1961. assert(c && c->ctrl->type == CTRL_CHECKBOX);
  1962. return 0 != IsDlgButtonChecked(dp->hwnd, c->base_id);
  1963. }
  1964. void dlg_editbox_set(dlgcontrol *ctrl, dlgparam *dp, char const *text)
  1965. {
  1966. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  1967. assert(c && c->ctrl->type == CTRL_EDITBOX);
  1968. SetDlgItemText(dp->hwnd, c->base_id+1, text);
  1969. }
  1970. char *dlg_editbox_get(dlgcontrol *ctrl, dlgparam *dp)
  1971. {
  1972. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  1973. assert(c && c->ctrl->type == CTRL_EDITBOX);
  1974. return GetDlgItemText_alloc(dp->hwnd, c->base_id+1);
  1975. }
  1976. void dlg_editbox_select_range(dlgcontrol *ctrl, dlgparam *dp,
  1977. size_t start, size_t len)
  1978. {
  1979. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  1980. assert(c && c->ctrl->type == CTRL_EDITBOX);
  1981. SendDlgItemMessage(dp->hwnd, c->base_id+1, EM_SETSEL, start, start+len);
  1982. }
  1983. /* The `listbox' functions can also apply to combo boxes. */
  1984. void dlg_listbox_clear(dlgcontrol *ctrl, dlgparam *dp)
  1985. {
  1986. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  1987. int msg;
  1988. assert(c &&
  1989. (c->ctrl->type == CTRL_LISTBOX ||
  1990. (c->ctrl->type == CTRL_EDITBOX &&
  1991. c->ctrl->editbox.has_list)));
  1992. msg = (c->ctrl->type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
  1993. LB_RESETCONTENT : CB_RESETCONTENT);
  1994. SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, 0);
  1995. }
  1996. void dlg_listbox_del(dlgcontrol *ctrl, dlgparam *dp, int index)
  1997. {
  1998. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  1999. int msg;
  2000. assert(c &&
  2001. (c->ctrl->type == CTRL_LISTBOX ||
  2002. (c->ctrl->type == CTRL_EDITBOX &&
  2003. c->ctrl->editbox.has_list)));
  2004. msg = (c->ctrl->type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
  2005. LB_DELETESTRING : CB_DELETESTRING);
  2006. SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
  2007. }
  2008. void dlg_listbox_add(dlgcontrol *ctrl, dlgparam *dp, char const *text)
  2009. {
  2010. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  2011. int msg;
  2012. assert(c &&
  2013. (c->ctrl->type == CTRL_LISTBOX ||
  2014. (c->ctrl->type == CTRL_EDITBOX &&
  2015. c->ctrl->editbox.has_list)));
  2016. msg = (c->ctrl->type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
  2017. LB_ADDSTRING : CB_ADDSTRING);
  2018. SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, (LPARAM)text);
  2019. }
  2020. /*
  2021. * Each listbox entry may have a numeric id associated with it.
  2022. * Note that some front ends only permit a string to be stored at
  2023. * each position, which means that _if_ you put two identical
  2024. * strings in any listbox then you MUST not assign them different
  2025. * IDs and expect to get meaningful results back.
  2026. */
  2027. void dlg_listbox_addwithid(dlgcontrol *ctrl, dlgparam *dp,
  2028. char const *text, int id)
  2029. {
  2030. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  2031. int msg, msg2, index;
  2032. assert(c &&
  2033. (c->ctrl->type == CTRL_LISTBOX ||
  2034. (c->ctrl->type == CTRL_EDITBOX &&
  2035. c->ctrl->editbox.has_list)));
  2036. msg = (c->ctrl->type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
  2037. LB_ADDSTRING : CB_ADDSTRING);
  2038. msg2 = (c->ctrl->type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
  2039. LB_SETITEMDATA : CB_SETITEMDATA);
  2040. index = SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, (LPARAM)text);
  2041. SendDlgItemMessage(dp->hwnd, c->base_id+1, msg2, index, (LPARAM)id);
  2042. }
  2043. int dlg_listbox_getid(dlgcontrol *ctrl, dlgparam *dp, int index)
  2044. {
  2045. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  2046. int msg;
  2047. assert(c && c->ctrl->type == CTRL_LISTBOX);
  2048. msg = (c->ctrl->listbox.height != 0 ? LB_GETITEMDATA : CB_GETITEMDATA);
  2049. return
  2050. SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
  2051. }
  2052. /* dlg_listbox_index returns <0 if no single element is selected. */
  2053. int dlg_listbox_index(dlgcontrol *ctrl, dlgparam *dp)
  2054. {
  2055. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  2056. int msg, ret;
  2057. assert(c && c->ctrl->type == CTRL_LISTBOX);
  2058. if (c->ctrl->listbox.multisel) {
  2059. assert(c->ctrl->listbox.height != 0); /* not combo box */
  2060. ret = SendDlgItemMessage(dp->hwnd, c->base_id+1, LB_GETSELCOUNT, 0, 0);
  2061. if (ret == LB_ERR || ret > 1)
  2062. return -1;
  2063. }
  2064. msg = (c->ctrl->listbox.height != 0 ? LB_GETCURSEL : CB_GETCURSEL);
  2065. ret = SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, 0);
  2066. if (ret == LB_ERR)
  2067. return -1;
  2068. else
  2069. return ret;
  2070. }
  2071. bool dlg_listbox_issel(dlgcontrol *ctrl, dlgparam *dp, int index)
  2072. {
  2073. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  2074. assert(c && c->ctrl->type == CTRL_LISTBOX &&
  2075. c->ctrl->listbox.multisel &&
  2076. c->ctrl->listbox.height != 0);
  2077. return
  2078. SendDlgItemMessage(dp->hwnd, c->base_id+1, LB_GETSEL, index, 0);
  2079. }
  2080. void dlg_listbox_select(dlgcontrol *ctrl, dlgparam *dp, int index)
  2081. {
  2082. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  2083. int msg;
  2084. assert(c && c->ctrl->type == CTRL_LISTBOX &&
  2085. !c->ctrl->listbox.multisel);
  2086. msg = (c->ctrl->listbox.height != 0 ? LB_SETCURSEL : CB_SETCURSEL);
  2087. SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
  2088. }
  2089. void dlg_text_set(dlgcontrol *ctrl, dlgparam *dp, char const *text)
  2090. {
  2091. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  2092. assert(c && c->ctrl->type == CTRL_TEXT);
  2093. SetDlgItemText(dp->hwnd, c->base_id, text);
  2094. }
  2095. void dlg_label_change(dlgcontrol *ctrl, dlgparam *dp, char const *text)
  2096. {
  2097. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  2098. char *escaped = NULL;
  2099. int id = -1;
  2100. assert(c);
  2101. switch (c->ctrl->type) {
  2102. case CTRL_EDITBOX:
  2103. escaped = shortcut_escape(text, c->ctrl->editbox.shortcut);
  2104. id = c->base_id;
  2105. break;
  2106. case CTRL_RADIO:
  2107. escaped = shortcut_escape(text, c->ctrl->radio.shortcut);
  2108. id = c->base_id;
  2109. break;
  2110. case CTRL_CHECKBOX:
  2111. escaped = shortcut_escape(text, ctrl->checkbox.shortcut);
  2112. id = c->base_id;
  2113. break;
  2114. case CTRL_BUTTON:
  2115. escaped = shortcut_escape(text, ctrl->button.shortcut);
  2116. id = c->base_id;
  2117. break;
  2118. case CTRL_LISTBOX:
  2119. escaped = shortcut_escape(text, ctrl->listbox.shortcut);
  2120. id = c->base_id;
  2121. break;
  2122. case CTRL_FILESELECT:
  2123. escaped = shortcut_escape(text, ctrl->fileselect.shortcut);
  2124. if (ctrl->fileselect.just_button)
  2125. id = c->base_id + 2; /* the button */
  2126. else
  2127. id = c->base_id; /* the label */
  2128. break;
  2129. case CTRL_FONTSELECT:
  2130. escaped = shortcut_escape(text, ctrl->fontselect.shortcut);
  2131. id = c->base_id;
  2132. break;
  2133. default:
  2134. unreachable("bad control type in label_change");
  2135. }
  2136. if (escaped) {
  2137. SetDlgItemText(dp->hwnd, id, escaped);
  2138. sfree(escaped);
  2139. }
  2140. }
  2141. void dlg_filesel_set(dlgcontrol *ctrl, dlgparam *dp, Filename *fn)
  2142. {
  2143. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  2144. assert(c);
  2145. assert(c->ctrl->type == CTRL_FILESELECT);
  2146. assert(!c->ctrl->fileselect.just_button);
  2147. SetDlgItemTextW(dp->hwnd, c->base_id+1, fn->wpath);
  2148. }
  2149. Filename *dlg_filesel_get(dlgcontrol *ctrl, dlgparam *dp)
  2150. {
  2151. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  2152. assert(c);
  2153. assert(c->ctrl->type == CTRL_FILESELECT);
  2154. if (!c->ctrl->fileselect.just_button) {
  2155. wchar_t *tmp = GetDlgItemTextW_alloc(dp->hwnd, c->base_id+1);
  2156. Filename *ret = filename_from_wstr(tmp);
  2157. sfree(tmp);
  2158. return ret;
  2159. } else {
  2160. return filename_from_str(c->data);
  2161. }
  2162. }
  2163. void dlg_fontsel_set(dlgcontrol *ctrl, dlgparam *dp, FontSpec *fs)
  2164. {
  2165. char *buf, *boldstr;
  2166. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  2167. assert(c && c->ctrl->type == CTRL_FONTSELECT);
  2168. fontspec_free((FontSpec *)c->data);
  2169. c->data = fontspec_copy(fs);
  2170. boldstr = (fs->isbold ? "bold, " : "");
  2171. if (fs->height == 0)
  2172. buf = dupprintf("Font: %s, %sdefault height", fs->name, boldstr);
  2173. else
  2174. buf = dupprintf("Font: %s, %s%d-%s", fs->name, boldstr,
  2175. (fs->height < 0 ? -fs->height : fs->height),
  2176. (fs->height < 0 ? "pixel" : "point"));
  2177. SetDlgItemText(dp->hwnd, c->base_id+1, buf);
  2178. sfree(buf);
  2179. dlg_auto_set_fixed_pitch_flag(dp);
  2180. }
  2181. FontSpec *dlg_fontsel_get(dlgcontrol *ctrl, dlgparam *dp)
  2182. {
  2183. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  2184. assert(c && c->ctrl->type == CTRL_FONTSELECT);
  2185. return fontspec_copy((FontSpec *)c->data);
  2186. }
  2187. /*
  2188. * Bracketing a large set of updates in these two functions will
  2189. * cause the front end (if possible) to delay updating the screen
  2190. * until it's all complete, thus avoiding flicker.
  2191. */
  2192. void dlg_update_start(dlgcontrol *ctrl, dlgparam *dp)
  2193. {
  2194. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  2195. if (c && c->ctrl->type == CTRL_LISTBOX) {
  2196. SendDlgItemMessage(dp->hwnd, c->base_id+1, WM_SETREDRAW, false, 0);
  2197. }
  2198. }
  2199. void dlg_update_done(dlgcontrol *ctrl, dlgparam *dp)
  2200. {
  2201. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  2202. if (c && c->ctrl->type == CTRL_LISTBOX) {
  2203. HWND hw = GetDlgItem(dp->hwnd, c->base_id+1);
  2204. SendMessage(hw, WM_SETREDRAW, true, 0);
  2205. InvalidateRect(hw, NULL, true);
  2206. }
  2207. }
  2208. void dlg_set_focus(dlgcontrol *ctrl, dlgparam *dp)
  2209. {
  2210. struct winctrl *c = dlg_findbyctrl(dp, ctrl);
  2211. int id;
  2212. HWND ctl;
  2213. if (!c)
  2214. return;
  2215. switch (ctrl->type) {
  2216. case CTRL_EDITBOX: id = c->base_id + 1; break;
  2217. case CTRL_RADIO:
  2218. for (id = c->base_id + ctrl->radio.nbuttons; id > 1; id--)
  2219. if (IsDlgButtonChecked(dp->hwnd, id))
  2220. break;
  2221. /*
  2222. * In the theoretically-unlikely case that no button was
  2223. * selected, id should come out of this as 1, which is a
  2224. * reasonable enough choice.
  2225. */
  2226. break;
  2227. case CTRL_CHECKBOX: id = c->base_id; break;
  2228. case CTRL_BUTTON: id = c->base_id; break;
  2229. case CTRL_LISTBOX: id = c->base_id + 1; break;
  2230. case CTRL_FILESELECT: id = c->base_id + 1; break;
  2231. case CTRL_FONTSELECT: id = c->base_id + 2; break;
  2232. default: id = c->base_id; break;
  2233. }
  2234. ctl = GetDlgItem(dp->hwnd, id);
  2235. SetFocus(ctl);
  2236. }
  2237. /*
  2238. * During event processing, you might well want to give an error
  2239. * indication to the user. dlg_beep() is a quick and easy generic
  2240. * error; dlg_error() puts up a message-box or equivalent.
  2241. */
  2242. void dlg_beep(dlgparam *dp)
  2243. {
  2244. MessageBeep(0);
  2245. }
  2246. void dlg_error_msg(dlgparam *dp, const char *msg)
  2247. {
  2248. MessageBox(dp->hwnd, msg,
  2249. dp->errtitle ? dp->errtitle : NULL,
  2250. MB_OK | MB_ICONERROR);
  2251. }
  2252. /*
  2253. * This function signals to the front end that the dialog's
  2254. * processing is completed, and passes an integer value (typically
  2255. * a success status).
  2256. */
  2257. void dlg_end(dlgparam *dp, int value)
  2258. {
  2259. dp->ended = true;
  2260. dp->endresult = value;
  2261. }
  2262. void dlg_refresh(dlgcontrol *ctrl, dlgparam *dp)
  2263. {
  2264. int i, j;
  2265. struct winctrl *c;
  2266. if (!ctrl) {
  2267. /*
  2268. * Send EVENT_REFRESH to absolutely everything.
  2269. */
  2270. for (j = 0; j < dp->nctrltrees; j++) {
  2271. for (i = 0;
  2272. (c = winctrl_findbyindex(dp->controltrees[j], i)) != NULL;
  2273. i++) {
  2274. if (c->ctrl && c->ctrl->handler != NULL)
  2275. c->ctrl->handler(c->ctrl, dp,
  2276. dp->data, EVENT_REFRESH);
  2277. }
  2278. }
  2279. } else {
  2280. /*
  2281. * Send EVENT_REFRESH to a specific control.
  2282. */
  2283. if (ctrl->handler != NULL)
  2284. ctrl->handler(ctrl, dp, dp->data, EVENT_REFRESH);
  2285. }
  2286. }
  2287. void dlg_coloursel_start(dlgcontrol *ctrl, dlgparam *dp, int r, int g, int b)
  2288. {
  2289. dp->coloursel_wanted = true;
  2290. dp->coloursel_result.r = r;
  2291. dp->coloursel_result.g = g;
  2292. dp->coloursel_result.b = b;
  2293. }
  2294. bool dlg_coloursel_results(dlgcontrol *ctrl, dlgparam *dp,
  2295. int *r, int *g, int *b)
  2296. {
  2297. if (dp->coloursel_result.ok) {
  2298. *r = dp->coloursel_result.r;
  2299. *g = dp->coloursel_result.g;
  2300. *b = dp->coloursel_result.b;
  2301. return true;
  2302. } else
  2303. return false;
  2304. }
  2305. void dlg_auto_set_fixed_pitch_flag(dlgparam *dp)
  2306. {
  2307. Conf *conf = (Conf *)dp->data;
  2308. FontSpec *fs;
  2309. int quality;
  2310. HFONT hfont;
  2311. HDC hdc;
  2312. TEXTMETRIC tm;
  2313. bool is_var;
  2314. /*
  2315. * Attempt to load the current font, and see if it's
  2316. * variable-pitch. If so, start off the fixed-pitch flag for the
  2317. * dialog box as false.
  2318. *
  2319. * We assume here that any client of the dlg_* mechanism which is
  2320. * using font selectors at all is also using a normal 'Conf *'
  2321. * as dp->data.
  2322. */
  2323. quality = conf_get_int(conf, CONF_font_quality);
  2324. fs = conf_get_fontspec(conf, CONF_font);
  2325. hfont = CreateFont(0, 0, 0, 0, FW_DONTCARE, false, false, false,
  2326. DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
  2327. CLIP_DEFAULT_PRECIS, FONT_QUALITY(quality),
  2328. FIXED_PITCH | FF_DONTCARE, fs->name);
  2329. hdc = GetDC(NULL);
  2330. if (hdc && SelectObject(hdc, hfont) && GetTextMetrics(hdc, &tm)) {
  2331. /* Note that the TMPF_FIXED_PITCH bit is defined upside down :-( */
  2332. is_var = (tm.tmPitchAndFamily & TMPF_FIXED_PITCH);
  2333. } else {
  2334. is_var = false; /* assume it's basically normal */
  2335. }
  2336. if (hdc)
  2337. ReleaseDC(NULL, hdc);
  2338. if (hfont)
  2339. DeleteObject(hfont);
  2340. if (is_var)
  2341. dp->fixed_pitch_fonts = false;
  2342. }
  2343. bool dlg_get_fixed_pitch_flag(dlgparam *dp)
  2344. {
  2345. return dp->fixed_pitch_fonts;
  2346. }
  2347. void dlg_set_fixed_pitch_flag(dlgparam *dp, bool flag)
  2348. {
  2349. dp->fixed_pitch_fonts = flag;
  2350. }
  2351. void dp_init(struct dlgparam *dp)
  2352. {
  2353. dp->nctrltrees = 0;
  2354. dp->data = NULL;
  2355. dp->ended = false;
  2356. dp->focused = dp->lastfocused = NULL;
  2357. memset(dp->shortcuts, 0, sizeof(dp->shortcuts));
  2358. dp->hwnd = NULL;
  2359. dp->wintitle = dp->errtitle = NULL;
  2360. dp->fixed_pitch_fonts = true;
  2361. }
  2362. void dp_add_tree(struct dlgparam *dp, struct winctrls *wc)
  2363. {
  2364. assert(dp->nctrltrees < lenof(dp->controltrees));
  2365. dp->controltrees[dp->nctrltrees++] = wc;
  2366. }
  2367. void dp_cleanup(struct dlgparam *dp)
  2368. {
  2369. sfree(dp->wintitle);
  2370. sfree(dp->errtitle);
  2371. }