gtkdlg.c 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780
  1. /*
  2. * gtkdlg.c - GTK implementation of the PuTTY configuration box.
  3. */
  4. /*
  5. * TODO when porting to GTK 2.0:
  6. *
  7. * - GtkTree is apparently deprecated and we should switch to
  8. * GtkTreeView instead.
  9. * - GtkLabel has a built-in mnemonic scheme, so we should at
  10. * least consider switching to that from the current adhockery.
  11. */
  12. #include <assert.h>
  13. #include <stdarg.h>
  14. #include <ctype.h>
  15. #include <time.h>
  16. #include <gtk/gtk.h>
  17. #include <gdk/gdkkeysyms.h>
  18. #include <gdk/gdkx.h>
  19. #include <X11/Xlib.h>
  20. #include <X11/Xutil.h>
  21. #include "gtkcols.h"
  22. #include "gtkpanel.h"
  23. #ifdef TESTMODE
  24. #define PUTTY_DO_GLOBALS /* actually _define_ globals */
  25. #endif
  26. #include "putty.h"
  27. #include "storage.h"
  28. #include "dialog.h"
  29. #include "tree234.h"
  30. struct Shortcut {
  31. GtkWidget *widget;
  32. struct uctrl *uc;
  33. int action;
  34. };
  35. struct Shortcuts {
  36. struct Shortcut sc[128];
  37. };
  38. struct uctrl {
  39. union control *ctrl;
  40. GtkWidget *toplevel;
  41. void *privdata;
  42. int privdata_needs_free;
  43. GtkWidget **buttons; int nbuttons; /* for radio buttons */
  44. GtkWidget *entry; /* for editbox, combobox, filesel, fontsel */
  45. GtkWidget *button; /* for filesel, fontsel */
  46. GtkWidget *list; /* for combobox, listbox */
  47. GtkWidget *menu; /* for optionmenu (==droplist) */
  48. GtkWidget *optmenu; /* also for optionmenu */
  49. GtkWidget *text; /* for text */
  50. GtkAdjustment *adj; /* for the scrollbar in a list box */
  51. guint textsig;
  52. };
  53. struct dlgparam {
  54. tree234 *byctrl, *bywidget;
  55. void *data;
  56. struct { unsigned char r, g, b, ok; } coloursel_result; /* 0-255 */
  57. /* `flags' are set to indicate when a GTK signal handler is being called
  58. * due to automatic processing and should not flag a user event. */
  59. int flags;
  60. struct Shortcuts *shortcuts;
  61. GtkWidget *window, *cancelbutton, *currtreeitem, **treeitems;
  62. union control *currfocus, *lastfocus;
  63. int ntreeitems;
  64. int retval;
  65. };
  66. #define FLAG_UPDATING_COMBO_LIST 1
  67. enum { /* values for Shortcut.action */
  68. SHORTCUT_EMPTY, /* no shortcut on this key */
  69. SHORTCUT_TREE, /* focus a tree item */
  70. SHORTCUT_FOCUS, /* focus the supplied widget */
  71. SHORTCUT_UCTRL, /* do something sane with uctrl */
  72. SHORTCUT_UCTRL_UP, /* uctrl is a draglist, move Up */
  73. SHORTCUT_UCTRL_DOWN, /* uctrl is a draglist, move Down */
  74. };
  75. /*
  76. * Forward references.
  77. */
  78. static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event,
  79. gpointer data);
  80. static void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw,
  81. int chr, int action, void *ptr);
  82. static int listitem_single_key(GtkWidget *item, GdkEventKey *event,
  83. gpointer data);
  84. static int listitem_multi_key(GtkWidget *item, GdkEventKey *event,
  85. gpointer data);
  86. static int listitem_button(GtkWidget *item, GdkEventButton *event,
  87. gpointer data);
  88. static void menuitem_activate(GtkMenuItem *item, gpointer data);
  89. static void coloursel_ok(GtkButton *button, gpointer data);
  90. static void coloursel_cancel(GtkButton *button, gpointer data);
  91. static void window_destroy(GtkWidget *widget, gpointer data);
  92. static int uctrl_cmp_byctrl(void *av, void *bv)
  93. {
  94. struct uctrl *a = (struct uctrl *)av;
  95. struct uctrl *b = (struct uctrl *)bv;
  96. if (a->ctrl < b->ctrl)
  97. return -1;
  98. else if (a->ctrl > b->ctrl)
  99. return +1;
  100. return 0;
  101. }
  102. static int uctrl_cmp_byctrl_find(void *av, void *bv)
  103. {
  104. union control *a = (union control *)av;
  105. struct uctrl *b = (struct uctrl *)bv;
  106. if (a < b->ctrl)
  107. return -1;
  108. else if (a > b->ctrl)
  109. return +1;
  110. return 0;
  111. }
  112. static int uctrl_cmp_bywidget(void *av, void *bv)
  113. {
  114. struct uctrl *a = (struct uctrl *)av;
  115. struct uctrl *b = (struct uctrl *)bv;
  116. if (a->toplevel < b->toplevel)
  117. return -1;
  118. else if (a->toplevel > b->toplevel)
  119. return +1;
  120. return 0;
  121. }
  122. static int uctrl_cmp_bywidget_find(void *av, void *bv)
  123. {
  124. GtkWidget *a = (GtkWidget *)av;
  125. struct uctrl *b = (struct uctrl *)bv;
  126. if (a < b->toplevel)
  127. return -1;
  128. else if (a > b->toplevel)
  129. return +1;
  130. return 0;
  131. }
  132. static void dlg_init(struct dlgparam *dp)
  133. {
  134. dp->byctrl = newtree234(uctrl_cmp_byctrl);
  135. dp->bywidget = newtree234(uctrl_cmp_bywidget);
  136. dp->coloursel_result.ok = FALSE;
  137. dp->treeitems = NULL;
  138. dp->window = dp->cancelbutton = dp->currtreeitem = NULL;
  139. dp->flags = 0;
  140. dp->currfocus = NULL;
  141. }
  142. static void dlg_cleanup(struct dlgparam *dp)
  143. {
  144. struct uctrl *uc;
  145. freetree234(dp->byctrl); /* doesn't free the uctrls inside */
  146. dp->byctrl = NULL;
  147. while ( (uc = index234(dp->bywidget, 0)) != NULL) {
  148. del234(dp->bywidget, uc);
  149. if (uc->privdata_needs_free)
  150. sfree(uc->privdata);
  151. sfree(uc->buttons);
  152. sfree(uc);
  153. }
  154. freetree234(dp->bywidget);
  155. dp->bywidget = NULL;
  156. sfree(dp->treeitems);
  157. }
  158. static void dlg_add_uctrl(struct dlgparam *dp, struct uctrl *uc)
  159. {
  160. add234(dp->byctrl, uc);
  161. add234(dp->bywidget, uc);
  162. }
  163. static struct uctrl *dlg_find_byctrl(struct dlgparam *dp, union control *ctrl)
  164. {
  165. if (!dp->byctrl)
  166. return NULL;
  167. return find234(dp->byctrl, ctrl, uctrl_cmp_byctrl_find);
  168. }
  169. static struct uctrl *dlg_find_bywidget(struct dlgparam *dp, GtkWidget *w)
  170. {
  171. struct uctrl *ret = NULL;
  172. if (!dp->bywidget)
  173. return NULL;
  174. do {
  175. ret = find234(dp->bywidget, w, uctrl_cmp_bywidget_find);
  176. if (ret)
  177. return ret;
  178. w = w->parent;
  179. } while (w);
  180. return ret;
  181. }
  182. void *dlg_get_privdata(union control *ctrl, void *dlg)
  183. {
  184. struct dlgparam *dp = (struct dlgparam *)dlg;
  185. struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
  186. return uc->privdata;
  187. }
  188. void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr)
  189. {
  190. struct dlgparam *dp = (struct dlgparam *)dlg;
  191. struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
  192. uc->privdata = ptr;
  193. uc->privdata_needs_free = FALSE;
  194. }
  195. void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size)
  196. {
  197. struct dlgparam *dp = (struct dlgparam *)dlg;
  198. struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
  199. /*
  200. * This is an internal allocation routine, so it's allowed to
  201. * use smalloc directly.
  202. */
  203. uc->privdata = smalloc(size);
  204. uc->privdata_needs_free = FALSE;
  205. return uc->privdata;
  206. }
  207. union control *dlg_last_focused(union control *ctrl, void *dlg)
  208. {
  209. struct dlgparam *dp = (struct dlgparam *)dlg;
  210. if (dp->currfocus != ctrl)
  211. return dp->currfocus;
  212. else
  213. return dp->lastfocus;
  214. }
  215. void dlg_radiobutton_set(union control *ctrl, void *dlg, int which)
  216. {
  217. struct dlgparam *dp = (struct dlgparam *)dlg;
  218. struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
  219. assert(uc->ctrl->generic.type == CTRL_RADIO);
  220. assert(uc->buttons != NULL);
  221. gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->buttons[which]), TRUE);
  222. }
  223. int dlg_radiobutton_get(union control *ctrl, void *dlg)
  224. {
  225. struct dlgparam *dp = (struct dlgparam *)dlg;
  226. struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
  227. int i;
  228. assert(uc->ctrl->generic.type == CTRL_RADIO);
  229. assert(uc->buttons != NULL);
  230. for (i = 0; i < uc->nbuttons; i++)
  231. if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->buttons[i])))
  232. return i;
  233. return 0; /* got to return something */
  234. }
  235. void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
  236. {
  237. struct dlgparam *dp = (struct dlgparam *)dlg;
  238. struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
  239. assert(uc->ctrl->generic.type == CTRL_CHECKBOX);
  240. gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(uc->toplevel), checked);
  241. }
  242. int dlg_checkbox_get(union control *ctrl, void *dlg)
  243. {
  244. struct dlgparam *dp = (struct dlgparam *)dlg;
  245. struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
  246. assert(uc->ctrl->generic.type == CTRL_CHECKBOX);
  247. return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(uc->toplevel));
  248. }
  249. void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
  250. {
  251. struct dlgparam *dp = (struct dlgparam *)dlg;
  252. struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
  253. assert(uc->ctrl->generic.type == CTRL_EDITBOX);
  254. assert(uc->entry != NULL);
  255. gtk_entry_set_text(GTK_ENTRY(uc->entry), text);
  256. }
  257. void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length)
  258. {
  259. struct dlgparam *dp = (struct dlgparam *)dlg;
  260. struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
  261. assert(uc->ctrl->generic.type == CTRL_EDITBOX);
  262. assert(uc->entry != NULL);
  263. strncpy(buffer, gtk_entry_get_text(GTK_ENTRY(uc->entry)),
  264. length);
  265. buffer[length-1] = '\0';
  266. }
  267. static void container_remove_and_destroy(GtkWidget *w, gpointer data)
  268. {
  269. GtkContainer *cont = GTK_CONTAINER(data);
  270. /* gtk_container_remove will unref the widget for us; we need not. */
  271. gtk_container_remove(cont, w);
  272. }
  273. /* The `listbox' functions can also apply to combo boxes. */
  274. void dlg_listbox_clear(union control *ctrl, void *dlg)
  275. {
  276. struct dlgparam *dp = (struct dlgparam *)dlg;
  277. struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
  278. assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
  279. uc->ctrl->generic.type == CTRL_LISTBOX);
  280. assert(uc->menu != NULL || uc->list != NULL);
  281. if (uc->menu) {
  282. gtk_container_foreach(GTK_CONTAINER(uc->menu),
  283. container_remove_and_destroy,
  284. GTK_CONTAINER(uc->menu));
  285. } else {
  286. gtk_list_clear_items(GTK_LIST(uc->list), 0, -1);
  287. }
  288. }
  289. void dlg_listbox_del(union control *ctrl, void *dlg, int index)
  290. {
  291. struct dlgparam *dp = (struct dlgparam *)dlg;
  292. struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
  293. assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
  294. uc->ctrl->generic.type == CTRL_LISTBOX);
  295. assert(uc->menu != NULL || uc->list != NULL);
  296. if (uc->menu) {
  297. gtk_container_remove
  298. (GTK_CONTAINER(uc->menu),
  299. g_list_nth_data(GTK_MENU_SHELL(uc->menu)->children, index));
  300. } else {
  301. gtk_list_clear_items(GTK_LIST(uc->list), index, index+1);
  302. }
  303. }
  304. void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
  305. {
  306. dlg_listbox_addwithid(ctrl, dlg, text, 0);
  307. }
  308. /*
  309. * Each listbox entry may have a numeric id associated with it.
  310. * Note that some front ends only permit a string to be stored at
  311. * each position, which means that _if_ you put two identical
  312. * strings in any listbox then you MUST not assign them different
  313. * IDs and expect to get meaningful results back.
  314. */
  315. void dlg_listbox_addwithid(union control *ctrl, void *dlg,
  316. char const *text, int id)
  317. {
  318. struct dlgparam *dp = (struct dlgparam *)dlg;
  319. struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
  320. assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
  321. uc->ctrl->generic.type == CTRL_LISTBOX);
  322. assert(uc->menu != NULL || uc->list != NULL);
  323. dp->flags |= FLAG_UPDATING_COMBO_LIST;
  324. if (uc->menu) {
  325. /*
  326. * List item in a drop-down (but non-combo) list. Tabs are
  327. * ignored; we just provide a standard menu item with the
  328. * text.
  329. */
  330. GtkWidget *menuitem = gtk_menu_item_new_with_label(text);
  331. gtk_container_add(GTK_CONTAINER(uc->menu), menuitem);
  332. gtk_widget_show(menuitem);
  333. gtk_object_set_data(GTK_OBJECT(menuitem), "user-data",
  334. GINT_TO_POINTER(id));
  335. gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
  336. GTK_SIGNAL_FUNC(menuitem_activate), dp);
  337. } else if (!uc->entry) {
  338. /*
  339. * List item in a non-combo-box list box. We make all of
  340. * these Columns containing GtkLabels. This allows us to do
  341. * the nasty force_left hack irrespective of whether there
  342. * are tabs in the thing.
  343. */
  344. GtkWidget *listitem = gtk_list_item_new();
  345. GtkWidget *cols = columns_new(10);
  346. gint *percents;
  347. int i, ncols;
  348. /* Count the tabs in the text, and hence determine # of columns. */
  349. ncols = 1;
  350. for (i = 0; text[i]; i++)
  351. if (text[i] == '\t')
  352. ncols++;
  353. assert(ncols <=
  354. (uc->ctrl->listbox.ncols ? uc->ctrl->listbox.ncols : 1));
  355. percents = snewn(ncols, gint);
  356. percents[ncols-1] = 100;
  357. for (i = 0; i < ncols-1; i++) {
  358. percents[i] = uc->ctrl->listbox.percentages[i];
  359. percents[ncols-1] -= percents[i];
  360. }
  361. columns_set_cols(COLUMNS(cols), ncols, percents);
  362. sfree(percents);
  363. for (i = 0; i < ncols; i++) {
  364. int len = strcspn(text, "\t");
  365. char *dup = dupprintf("%.*s", len, text);
  366. GtkWidget *label;
  367. text += len;
  368. if (*text) text++;
  369. label = gtk_label_new(dup);
  370. sfree(dup);
  371. columns_add(COLUMNS(cols), label, i, 1);
  372. columns_force_left_align(COLUMNS(cols), label);
  373. gtk_widget_show(label);
  374. }
  375. gtk_container_add(GTK_CONTAINER(listitem), cols);
  376. gtk_widget_show(cols);
  377. gtk_container_add(GTK_CONTAINER(uc->list), listitem);
  378. gtk_widget_show(listitem);
  379. if (ctrl->listbox.multisel) {
  380. gtk_signal_connect(GTK_OBJECT(listitem), "key_press_event",
  381. GTK_SIGNAL_FUNC(listitem_multi_key), uc->adj);
  382. } else {
  383. gtk_signal_connect(GTK_OBJECT(listitem), "key_press_event",
  384. GTK_SIGNAL_FUNC(listitem_single_key), uc->adj);
  385. }
  386. gtk_signal_connect(GTK_OBJECT(listitem), "focus_in_event",
  387. GTK_SIGNAL_FUNC(widget_focus), dp);
  388. gtk_signal_connect(GTK_OBJECT(listitem), "button_press_event",
  389. GTK_SIGNAL_FUNC(listitem_button), dp);
  390. gtk_object_set_data(GTK_OBJECT(listitem), "user-data",
  391. GINT_TO_POINTER(id));
  392. } else {
  393. /*
  394. * List item in a combo-box list, which means the sensible
  395. * thing to do is make it a perfectly normal label. Hence
  396. * tabs are disregarded.
  397. */
  398. GtkWidget *listitem = gtk_list_item_new_with_label(text);
  399. gtk_container_add(GTK_CONTAINER(uc->list), listitem);
  400. gtk_widget_show(listitem);
  401. gtk_object_set_data(GTK_OBJECT(listitem), "user-data",
  402. GINT_TO_POINTER(id));
  403. }
  404. dp->flags &= ~FLAG_UPDATING_COMBO_LIST;
  405. }
  406. int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
  407. {
  408. struct dlgparam *dp = (struct dlgparam *)dlg;
  409. struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
  410. GList *children;
  411. GtkObject *item;
  412. assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
  413. uc->ctrl->generic.type == CTRL_LISTBOX);
  414. assert(uc->menu != NULL || uc->list != NULL);
  415. children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
  416. uc->list));
  417. item = GTK_OBJECT(g_list_nth_data(children, index));
  418. g_list_free(children);
  419. return GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item), "user-data"));
  420. }
  421. /* dlg_listbox_index returns <0 if no single element is selected. */
  422. int dlg_listbox_index(union control *ctrl, void *dlg)
  423. {
  424. struct dlgparam *dp = (struct dlgparam *)dlg;
  425. struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
  426. GList *children;
  427. GtkWidget *item, *activeitem;
  428. int i;
  429. int selected = -1;
  430. assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
  431. uc->ctrl->generic.type == CTRL_LISTBOX);
  432. assert(uc->menu != NULL || uc->list != NULL);
  433. if (uc->menu)
  434. activeitem = gtk_menu_get_active(GTK_MENU(uc->menu));
  435. else
  436. activeitem = NULL; /* unnecessarily placate gcc */
  437. children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
  438. uc->list));
  439. for (i = 0; children!=NULL && (item = GTK_WIDGET(children->data))!=NULL;
  440. i++, children = children->next) {
  441. if (uc->menu ? activeitem == item :
  442. GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED) {
  443. if (selected == -1)
  444. selected = i;
  445. else
  446. selected = -2;
  447. }
  448. }
  449. g_list_free(children);
  450. return selected < 0 ? -1 : selected;
  451. }
  452. int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
  453. {
  454. struct dlgparam *dp = (struct dlgparam *)dlg;
  455. struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
  456. GList *children;
  457. GtkWidget *item, *activeitem;
  458. assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
  459. uc->ctrl->generic.type == CTRL_LISTBOX);
  460. assert(uc->menu != NULL || uc->list != NULL);
  461. children = gtk_container_children(GTK_CONTAINER(uc->menu ? uc->menu :
  462. uc->list));
  463. item = GTK_WIDGET(g_list_nth_data(children, index));
  464. g_list_free(children);
  465. if (uc->menu) {
  466. activeitem = gtk_menu_get_active(GTK_MENU(uc->menu));
  467. return item == activeitem;
  468. } else {
  469. return GTK_WIDGET_STATE(item) == GTK_STATE_SELECTED;
  470. }
  471. }
  472. void dlg_listbox_select(union control *ctrl, void *dlg, int index)
  473. {
  474. struct dlgparam *dp = (struct dlgparam *)dlg;
  475. struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
  476. assert(uc->ctrl->generic.type == CTRL_EDITBOX ||
  477. uc->ctrl->generic.type == CTRL_LISTBOX);
  478. assert(uc->optmenu != NULL || uc->list != NULL);
  479. if (uc->optmenu) {
  480. gtk_option_menu_set_history(GTK_OPTION_MENU(uc->optmenu), index);
  481. } else {
  482. gtk_list_select_item(GTK_LIST(uc->list), index);
  483. }
  484. }
  485. void dlg_text_set(union control *ctrl, void *dlg, char const *text)
  486. {
  487. struct dlgparam *dp = (struct dlgparam *)dlg;
  488. struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
  489. assert(uc->ctrl->generic.type == CTRL_TEXT);
  490. assert(uc->text != NULL);
  491. gtk_label_set_text(GTK_LABEL(uc->text), text);
  492. }
  493. void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn)
  494. {
  495. struct dlgparam *dp = (struct dlgparam *)dlg;
  496. struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
  497. assert(uc->ctrl->generic.type == CTRL_FILESELECT);
  498. assert(uc->entry != NULL);
  499. gtk_entry_set_text(GTK_ENTRY(uc->entry), fn.path);
  500. }
  501. void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn)
  502. {
  503. struct dlgparam *dp = (struct dlgparam *)dlg;
  504. struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
  505. assert(uc->ctrl->generic.type == CTRL_FILESELECT);
  506. assert(uc->entry != NULL);
  507. strncpy(fn->path, gtk_entry_get_text(GTK_ENTRY(uc->entry)),
  508. lenof(fn->path));
  509. fn->path[lenof(fn->path)-1] = '\0';
  510. }
  511. void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fs)
  512. {
  513. struct dlgparam *dp = (struct dlgparam *)dlg;
  514. struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
  515. assert(uc->ctrl->generic.type == CTRL_FONTSELECT);
  516. assert(uc->entry != NULL);
  517. gtk_entry_set_text(GTK_ENTRY(uc->entry), fs.name);
  518. }
  519. void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fs)
  520. {
  521. struct dlgparam *dp = (struct dlgparam *)dlg;
  522. struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
  523. assert(uc->ctrl->generic.type == CTRL_FONTSELECT);
  524. assert(uc->entry != NULL);
  525. strncpy(fs->name, gtk_entry_get_text(GTK_ENTRY(uc->entry)),
  526. lenof(fs->name));
  527. fs->name[lenof(fs->name)-1] = '\0';
  528. }
  529. /*
  530. * Bracketing a large set of updates in these two functions will
  531. * cause the front end (if possible) to delay updating the screen
  532. * until it's all complete, thus avoiding flicker.
  533. */
  534. void dlg_update_start(union control *ctrl, void *dlg)
  535. {
  536. /*
  537. * Apparently we can't do this at all in GTK. GtkCList supports
  538. * freeze and thaw, but not GtkList. Bah.
  539. */
  540. }
  541. void dlg_update_done(union control *ctrl, void *dlg)
  542. {
  543. /*
  544. * Apparently we can't do this at all in GTK. GtkCList supports
  545. * freeze and thaw, but not GtkList. Bah.
  546. */
  547. }
  548. void dlg_set_focus(union control *ctrl, void *dlg)
  549. {
  550. struct dlgparam *dp = (struct dlgparam *)dlg;
  551. struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
  552. switch (ctrl->generic.type) {
  553. case CTRL_CHECKBOX:
  554. case CTRL_BUTTON:
  555. /* Check boxes and buttons get the focus _and_ get toggled. */
  556. gtk_widget_grab_focus(uc->toplevel);
  557. break;
  558. case CTRL_FILESELECT:
  559. case CTRL_FONTSELECT:
  560. case CTRL_EDITBOX:
  561. /* Anything containing an edit box gets that focused. */
  562. gtk_widget_grab_focus(uc->entry);
  563. break;
  564. case CTRL_RADIO:
  565. /*
  566. * Radio buttons: we find the currently selected button and
  567. * focus it.
  568. */
  569. {
  570. int i;
  571. for (i = 0; i < ctrl->radio.nbuttons; i++)
  572. if (gtk_toggle_button_get_active
  573. (GTK_TOGGLE_BUTTON(uc->buttons[i]))) {
  574. gtk_widget_grab_focus(uc->buttons[i]);
  575. }
  576. }
  577. break;
  578. case CTRL_LISTBOX:
  579. /*
  580. * If the list is really an option menu, we focus it.
  581. * Otherwise we tell it to focus one of its children, which
  582. * appears to do the Right Thing.
  583. */
  584. if (uc->optmenu) {
  585. gtk_widget_grab_focus(uc->optmenu);
  586. } else {
  587. assert(uc->list != NULL);
  588. gtk_container_focus(GTK_CONTAINER(uc->list), GTK_DIR_TAB_FORWARD);
  589. }
  590. break;
  591. }
  592. }
  593. /*
  594. * During event processing, you might well want to give an error
  595. * indication to the user. dlg_beep() is a quick and easy generic
  596. * error; dlg_error() puts up a message-box or equivalent.
  597. */
  598. void dlg_beep(void *dlg)
  599. {
  600. gdk_beep();
  601. }
  602. static void errmsg_button_clicked(GtkButton *button, gpointer data)
  603. {
  604. gtk_widget_destroy(GTK_WIDGET(data));
  605. }
  606. static void set_transient_window_pos(GtkWidget *parent, GtkWidget *child)
  607. {
  608. gint x, y, w, h, dx, dy;
  609. GtkRequisition req;
  610. gtk_window_set_position(GTK_WINDOW(child), GTK_WIN_POS_NONE);
  611. gtk_widget_size_request(GTK_WIDGET(child), &req);
  612. gdk_window_get_origin(GTK_WIDGET(parent)->window, &x, &y);
  613. gdk_window_get_size(GTK_WIDGET(parent)->window, &w, &h);
  614. /*
  615. * One corner of the transient will be offset inwards, by 1/4
  616. * of the parent window's size, from the corresponding corner
  617. * of the parent window. The corner will be chosen so as to
  618. * place the transient closer to the centre of the screen; this
  619. * should avoid transients going off the edge of the screen on
  620. * a regular basis.
  621. */
  622. if (x + w/2 < gdk_screen_width() / 2)
  623. dx = x + w/4; /* work from left edges */
  624. else
  625. dx = x + 3*w/4 - req.width; /* work from right edges */
  626. if (y + h/2 < gdk_screen_height() / 2)
  627. dy = y + h/4; /* work from top edges */
  628. else
  629. dy = y + 3*h/4 - req.height; /* work from bottom edges */
  630. gtk_widget_set_uposition(GTK_WIDGET(child), dx, dy);
  631. }
  632. void dlg_error_msg(void *dlg, char *msg)
  633. {
  634. struct dlgparam *dp = (struct dlgparam *)dlg;
  635. GtkWidget *window, *hbox, *text, *ok;
  636. window = gtk_dialog_new();
  637. text = gtk_label_new(msg);
  638. gtk_misc_set_alignment(GTK_MISC(text), 0.0, 0.0);
  639. hbox = gtk_hbox_new(FALSE, 0);
  640. gtk_box_pack_start(GTK_BOX(hbox), text, FALSE, FALSE, 20);
  641. gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
  642. hbox, FALSE, FALSE, 20);
  643. gtk_widget_show(text);
  644. gtk_widget_show(hbox);
  645. gtk_window_set_title(GTK_WINDOW(window), "Error");
  646. gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
  647. ok = gtk_button_new_with_label("OK");
  648. gtk_box_pack_end(GTK_BOX(GTK_DIALOG(window)->action_area),
  649. ok, FALSE, FALSE, 0);
  650. gtk_widget_show(ok);
  651. GTK_WIDGET_SET_FLAGS(ok, GTK_CAN_DEFAULT);
  652. gtk_window_set_default(GTK_WINDOW(window), ok);
  653. gtk_signal_connect(GTK_OBJECT(ok), "clicked",
  654. GTK_SIGNAL_FUNC(errmsg_button_clicked), window);
  655. gtk_signal_connect(GTK_OBJECT(window), "destroy",
  656. GTK_SIGNAL_FUNC(window_destroy), NULL);
  657. gtk_window_set_modal(GTK_WINDOW(window), TRUE);
  658. gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(dp->window));
  659. set_transient_window_pos(dp->window, window);
  660. gtk_widget_show(window);
  661. gtk_main();
  662. }
  663. /*
  664. * This function signals to the front end that the dialog's
  665. * processing is completed, and passes an integer value (typically
  666. * a success status).
  667. */
  668. void dlg_end(void *dlg, int value)
  669. {
  670. struct dlgparam *dp = (struct dlgparam *)dlg;
  671. dp->retval = value;
  672. gtk_widget_destroy(dp->window);
  673. }
  674. void dlg_refresh(union control *ctrl, void *dlg)
  675. {
  676. struct dlgparam *dp = (struct dlgparam *)dlg;
  677. struct uctrl *uc;
  678. if (ctrl) {
  679. if (ctrl->generic.handler != NULL)
  680. ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH);
  681. } else {
  682. int i;
  683. for (i = 0; (uc = index234(dp->byctrl, i)) != NULL; i++) {
  684. assert(uc->ctrl != NULL);
  685. if (uc->ctrl->generic.handler != NULL)
  686. uc->ctrl->generic.handler(uc->ctrl, dp,
  687. dp->data, EVENT_REFRESH);
  688. }
  689. }
  690. }
  691. void dlg_coloursel_start(union control *ctrl, void *dlg, int r, int g, int b)
  692. {
  693. struct dlgparam *dp = (struct dlgparam *)dlg;
  694. struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
  695. gdouble cvals[4];
  696. GtkWidget *coloursel =
  697. gtk_color_selection_dialog_new("Select a colour");
  698. GtkColorSelectionDialog *ccs = GTK_COLOR_SELECTION_DIALOG(coloursel);
  699. dp->coloursel_result.ok = FALSE;
  700. gtk_window_set_modal(GTK_WINDOW(coloursel), TRUE);
  701. gtk_color_selection_set_opacity(GTK_COLOR_SELECTION(ccs->colorsel), FALSE);
  702. cvals[0] = r / 255.0;
  703. cvals[1] = g / 255.0;
  704. cvals[2] = b / 255.0;
  705. cvals[3] = 1.0; /* fully opaque! */
  706. gtk_color_selection_set_color(GTK_COLOR_SELECTION(ccs->colorsel), cvals);
  707. gtk_object_set_data(GTK_OBJECT(ccs->ok_button), "user-data",
  708. (gpointer)coloursel);
  709. gtk_object_set_data(GTK_OBJECT(ccs->cancel_button), "user-data",
  710. (gpointer)coloursel);
  711. gtk_object_set_data(GTK_OBJECT(coloursel), "user-data", (gpointer)uc);
  712. gtk_signal_connect(GTK_OBJECT(ccs->ok_button), "clicked",
  713. GTK_SIGNAL_FUNC(coloursel_ok), (gpointer)dp);
  714. gtk_signal_connect(GTK_OBJECT(ccs->cancel_button), "clicked",
  715. GTK_SIGNAL_FUNC(coloursel_cancel), (gpointer)dp);
  716. gtk_signal_connect_object(GTK_OBJECT(ccs->ok_button), "clicked",
  717. GTK_SIGNAL_FUNC(gtk_widget_destroy),
  718. (gpointer)coloursel);
  719. gtk_signal_connect_object(GTK_OBJECT(ccs->cancel_button), "clicked",
  720. GTK_SIGNAL_FUNC(gtk_widget_destroy),
  721. (gpointer)coloursel);
  722. gtk_widget_show(coloursel);
  723. }
  724. int dlg_coloursel_results(union control *ctrl, void *dlg,
  725. int *r, int *g, int *b)
  726. {
  727. struct dlgparam *dp = (struct dlgparam *)dlg;
  728. if (dp->coloursel_result.ok) {
  729. *r = dp->coloursel_result.r;
  730. *g = dp->coloursel_result.g;
  731. *b = dp->coloursel_result.b;
  732. return 1;
  733. } else
  734. return 0;
  735. }
  736. /* ----------------------------------------------------------------------
  737. * Signal handlers while the dialog box is active.
  738. */
  739. static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event,
  740. gpointer data)
  741. {
  742. struct dlgparam *dp = (struct dlgparam *)data;
  743. struct uctrl *uc = dlg_find_bywidget(dp, widget);
  744. union control *focus;
  745. if (uc && uc->ctrl)
  746. focus = uc->ctrl;
  747. else
  748. focus = NULL;
  749. if (focus != dp->currfocus) {
  750. dp->lastfocus = dp->currfocus;
  751. dp->currfocus = focus;
  752. }
  753. return FALSE;
  754. }
  755. static void button_clicked(GtkButton *button, gpointer data)
  756. {
  757. struct dlgparam *dp = (struct dlgparam *)data;
  758. struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
  759. uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
  760. }
  761. static void button_toggled(GtkToggleButton *tb, gpointer data)
  762. {
  763. struct dlgparam *dp = (struct dlgparam *)data;
  764. struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(tb));
  765. uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
  766. }
  767. static int editbox_key(GtkWidget *widget, GdkEventKey *event, gpointer data)
  768. {
  769. /*
  770. * GtkEntry has a nasty habit of eating the Return key, which
  771. * is unhelpful since it doesn't actually _do_ anything with it
  772. * (it calls gtk_widget_activate, but our edit boxes never need
  773. * activating). So I catch Return before GtkEntry sees it, and
  774. * pass it straight on to the parent widget. Effect: hitting
  775. * Return in an edit box will now activate the default button
  776. * in the dialog just like it will everywhere else.
  777. */
  778. if (event->keyval == GDK_Return && widget->parent != NULL) {
  779. gint return_val;
  780. gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event");
  781. gtk_signal_emit_by_name(GTK_OBJECT(widget->parent), "key_press_event",
  782. event, &return_val);
  783. return return_val;
  784. }
  785. return FALSE;
  786. }
  787. static void editbox_changed(GtkEditable *ed, gpointer data)
  788. {
  789. struct dlgparam *dp = (struct dlgparam *)data;
  790. if (!(dp->flags & FLAG_UPDATING_COMBO_LIST)) {
  791. struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed));
  792. uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
  793. }
  794. }
  795. static void editbox_lostfocus(GtkWidget *ed, GdkEventFocus *event,
  796. gpointer data)
  797. {
  798. struct dlgparam *dp = (struct dlgparam *)data;
  799. struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed));
  800. uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_REFRESH);
  801. }
  802. static int listitem_key(GtkWidget *item, GdkEventKey *event, gpointer data,
  803. int multiple)
  804. {
  805. GtkAdjustment *adj = GTK_ADJUSTMENT(data);
  806. if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up ||
  807. event->keyval == GDK_Down || event->keyval == GDK_KP_Down ||
  808. event->keyval == GDK_Page_Up || event->keyval == GDK_KP_Page_Up ||
  809. event->keyval == GDK_Page_Down || event->keyval == GDK_KP_Page_Down) {
  810. /*
  811. * Up, Down, PgUp or PgDn have been pressed on a ListItem
  812. * in a list box. So, if the list box is single-selection:
  813. *
  814. * - if the list item in question isn't already selected,
  815. * we simply select it.
  816. * - otherwise, we find the next one (or next
  817. * however-far-away) in whichever direction we're going,
  818. * and select that.
  819. * + in this case, we must also fiddle with the
  820. * scrollbar to ensure the newly selected item is
  821. * actually visible.
  822. *
  823. * If it's multiple-selection, we do all of the above
  824. * except actually selecting anything, so we move the focus
  825. * and fiddle the scrollbar to follow it.
  826. */
  827. GtkWidget *list = item->parent;
  828. gtk_signal_emit_stop_by_name(GTK_OBJECT(item), "key_press_event");
  829. if (!multiple &&
  830. GTK_WIDGET_STATE(item) != GTK_STATE_SELECTED) {
  831. gtk_list_select_child(GTK_LIST(list), item);
  832. } else {
  833. int direction =
  834. (event->keyval==GDK_Up || event->keyval==GDK_KP_Up ||
  835. event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up)
  836. ? -1 : +1;
  837. int step =
  838. (event->keyval==GDK_Page_Down ||
  839. event->keyval==GDK_KP_Page_Down ||
  840. event->keyval==GDK_Page_Up || event->keyval==GDK_KP_Page_Up)
  841. ? 2 : 1;
  842. int i, n;
  843. GList *children, *chead;
  844. chead = children = gtk_container_children(GTK_CONTAINER(list));
  845. n = g_list_length(children);
  846. if (step == 2) {
  847. /*
  848. * Figure out how many list items to a screenful,
  849. * and adjust the step appropriately.
  850. */
  851. step = 0.5 + adj->page_size * n / (adj->upper - adj->lower);
  852. step--; /* go by one less than that */
  853. }
  854. i = 0;
  855. while (children != NULL) {
  856. if (item == children->data)
  857. break;
  858. children = children->next;
  859. i++;
  860. }
  861. while (step > 0) {
  862. if (direction < 0 && i > 0)
  863. children = children->prev, i--;
  864. else if (direction > 0 && i < n-1)
  865. children = children->next, i++;
  866. step--;
  867. }
  868. if (children && children->data) {
  869. if (!multiple)
  870. gtk_list_select_child(GTK_LIST(list),
  871. GTK_WIDGET(children->data));
  872. gtk_widget_grab_focus(GTK_WIDGET(children->data));
  873. gtk_adjustment_clamp_page
  874. (adj,
  875. adj->lower + (adj->upper-adj->lower) * i / n,
  876. adj->lower + (adj->upper-adj->lower) * (i+1) / n);
  877. }
  878. g_list_free(chead);
  879. }
  880. return TRUE;
  881. }
  882. return FALSE;
  883. }
  884. static int listitem_single_key(GtkWidget *item, GdkEventKey *event,
  885. gpointer data)
  886. {
  887. return listitem_key(item, event, data, FALSE);
  888. }
  889. static int listitem_multi_key(GtkWidget *item, GdkEventKey *event,
  890. gpointer data)
  891. {
  892. return listitem_key(item, event, data, TRUE);
  893. }
  894. static int listitem_button(GtkWidget *item, GdkEventButton *event,
  895. gpointer data)
  896. {
  897. struct dlgparam *dp = (struct dlgparam *)data;
  898. if (event->type == GDK_2BUTTON_PRESS ||
  899. event->type == GDK_3BUTTON_PRESS) {
  900. struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(item));
  901. uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_ACTION);
  902. return TRUE;
  903. }
  904. return FALSE;
  905. }
  906. static void list_selchange(GtkList *list, gpointer data)
  907. {
  908. struct dlgparam *dp = (struct dlgparam *)data;
  909. struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(list));
  910. if (!uc) return;
  911. uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
  912. }
  913. static void menuitem_activate(GtkMenuItem *item, gpointer data)
  914. {
  915. struct dlgparam *dp = (struct dlgparam *)data;
  916. GtkWidget *menushell = GTK_WIDGET(item)->parent;
  917. gpointer optmenu = gtk_object_get_data(GTK_OBJECT(menushell), "user-data");
  918. struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(optmenu));
  919. uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_SELCHANGE);
  920. }
  921. static void draglist_move(struct dlgparam *dp, struct uctrl *uc, int direction)
  922. {
  923. int index = dlg_listbox_index(uc->ctrl, dp);
  924. GList *children = gtk_container_children(GTK_CONTAINER(uc->list));
  925. GtkWidget *child;
  926. if ((index < 0) ||
  927. (index == 0 && direction < 0) ||
  928. (index == g_list_length(children)-1 && direction > 0)) {
  929. gdk_beep();
  930. return;
  931. }
  932. child = g_list_nth_data(children, index);
  933. gtk_widget_ref(child);
  934. gtk_list_clear_items(GTK_LIST(uc->list), index, index+1);
  935. g_list_free(children);
  936. children = NULL;
  937. children = g_list_append(children, child);
  938. gtk_list_insert_items(GTK_LIST(uc->list), children, index + direction);
  939. gtk_list_select_item(GTK_LIST(uc->list), index + direction);
  940. uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
  941. }
  942. static void draglist_up(GtkButton *button, gpointer data)
  943. {
  944. struct dlgparam *dp = (struct dlgparam *)data;
  945. struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
  946. draglist_move(dp, uc, -1);
  947. }
  948. static void draglist_down(GtkButton *button, gpointer data)
  949. {
  950. struct dlgparam *dp = (struct dlgparam *)data;
  951. struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
  952. draglist_move(dp, uc, +1);
  953. }
  954. static void filesel_ok(GtkButton *button, gpointer data)
  955. {
  956. /* struct dlgparam *dp = (struct dlgparam *)data; */
  957. gpointer filesel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
  958. struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(filesel), "user-data");
  959. char *name = gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel));
  960. gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
  961. }
  962. static void fontsel_ok(GtkButton *button, gpointer data)
  963. {
  964. /* struct dlgparam *dp = (struct dlgparam *)data; */
  965. gpointer fontsel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
  966. struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(fontsel), "user-data");
  967. char *name = gtk_font_selection_dialog_get_font_name
  968. (GTK_FONT_SELECTION_DIALOG(fontsel));
  969. gtk_entry_set_text(GTK_ENTRY(uc->entry), name);
  970. }
  971. static void coloursel_ok(GtkButton *button, gpointer data)
  972. {
  973. struct dlgparam *dp = (struct dlgparam *)data;
  974. gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
  975. struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data");
  976. gdouble cvals[4];
  977. gtk_color_selection_get_color
  978. (GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(coloursel)->colorsel),
  979. cvals);
  980. dp->coloursel_result.r = (int) (255 * cvals[0]);
  981. dp->coloursel_result.g = (int) (255 * cvals[1]);
  982. dp->coloursel_result.b = (int) (255 * cvals[2]);
  983. dp->coloursel_result.ok = TRUE;
  984. uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
  985. }
  986. static void coloursel_cancel(GtkButton *button, gpointer data)
  987. {
  988. struct dlgparam *dp = (struct dlgparam *)data;
  989. gpointer coloursel = gtk_object_get_data(GTK_OBJECT(button), "user-data");
  990. struct uctrl *uc = gtk_object_get_data(GTK_OBJECT(coloursel), "user-data");
  991. dp->coloursel_result.ok = FALSE;
  992. uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_CALLBACK);
  993. }
  994. static void filefont_clicked(GtkButton *button, gpointer data)
  995. {
  996. struct dlgparam *dp = (struct dlgparam *)data;
  997. struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(button));
  998. if (uc->ctrl->generic.type == CTRL_FILESELECT) {
  999. GtkWidget *filesel =
  1000. gtk_file_selection_new(uc->ctrl->fileselect.title);
  1001. gtk_window_set_modal(GTK_WINDOW(filesel), TRUE);
  1002. gtk_object_set_data
  1003. (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "user-data",
  1004. (gpointer)filesel);
  1005. gtk_object_set_data(GTK_OBJECT(filesel), "user-data", (gpointer)uc);
  1006. gtk_signal_connect
  1007. (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
  1008. GTK_SIGNAL_FUNC(filesel_ok), (gpointer)dp);
  1009. gtk_signal_connect_object
  1010. (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked",
  1011. GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
  1012. gtk_signal_connect_object
  1013. (GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked",
  1014. GTK_SIGNAL_FUNC(gtk_widget_destroy), (gpointer)filesel);
  1015. gtk_widget_show(filesel);
  1016. }
  1017. if (uc->ctrl->generic.type == CTRL_FONTSELECT) {
  1018. gchar *spacings[] = { "c", "m", NULL };
  1019. gchar *fontname = gtk_entry_get_text(GTK_ENTRY(uc->entry));
  1020. GtkWidget *fontsel =
  1021. gtk_font_selection_dialog_new("Select a font");
  1022. gtk_window_set_modal(GTK_WINDOW(fontsel), TRUE);
  1023. gtk_font_selection_dialog_set_filter
  1024. (GTK_FONT_SELECTION_DIALOG(fontsel),
  1025. GTK_FONT_FILTER_BASE, GTK_FONT_ALL,
  1026. NULL, NULL, NULL, NULL, spacings, NULL);
  1027. if (!gtk_font_selection_dialog_set_font_name
  1028. (GTK_FONT_SELECTION_DIALOG(fontsel), fontname)) {
  1029. /*
  1030. * If the font name wasn't found as it was, try opening
  1031. * it and extracting its FONT property. This should
  1032. * have the effect of mapping short aliases into true
  1033. * XLFDs.
  1034. */
  1035. GdkFont *font = gdk_font_load(fontname);
  1036. if (font) {
  1037. XFontStruct *xfs = GDK_FONT_XFONT(font);
  1038. Display *disp = GDK_FONT_XDISPLAY(font);
  1039. Atom fontprop = XInternAtom(disp, "FONT", False);
  1040. unsigned long ret;
  1041. if (XGetFontProperty(xfs, fontprop, &ret)) {
  1042. char *name = XGetAtomName(disp, (Atom)ret);
  1043. if (name)
  1044. gtk_font_selection_dialog_set_font_name
  1045. (GTK_FONT_SELECTION_DIALOG(fontsel), name);
  1046. }
  1047. gdk_font_unref(font);
  1048. }
  1049. }
  1050. gtk_object_set_data
  1051. (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
  1052. "user-data", (gpointer)fontsel);
  1053. gtk_object_set_data(GTK_OBJECT(fontsel), "user-data", (gpointer)uc);
  1054. gtk_signal_connect
  1055. (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
  1056. "clicked", GTK_SIGNAL_FUNC(fontsel_ok), (gpointer)dp);
  1057. gtk_signal_connect_object
  1058. (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->ok_button),
  1059. "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
  1060. (gpointer)fontsel);
  1061. gtk_signal_connect_object
  1062. (GTK_OBJECT(GTK_FONT_SELECTION_DIALOG(fontsel)->cancel_button),
  1063. "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy),
  1064. (gpointer)fontsel);
  1065. gtk_widget_show(fontsel);
  1066. }
  1067. }
  1068. static void label_sizealloc(GtkWidget *widget, GtkAllocation *alloc,
  1069. gpointer data)
  1070. {
  1071. struct dlgparam *dp = (struct dlgparam *)data;
  1072. struct uctrl *uc = dlg_find_bywidget(dp, widget);
  1073. gtk_widget_set_usize(uc->text, alloc->width, -1);
  1074. gtk_label_set_text(GTK_LABEL(uc->text), uc->ctrl->generic.label);
  1075. gtk_signal_disconnect(GTK_OBJECT(uc->text), uc->textsig);
  1076. }
  1077. /* ----------------------------------------------------------------------
  1078. * This function does the main layout work: it reads a controlset,
  1079. * it creates the relevant GTK controls, and returns a GtkWidget
  1080. * containing the result. (This widget might be a title of some
  1081. * sort, it might be a Columns containing many controls, or it
  1082. * might be a GtkFrame containing a Columns; whatever it is, it's
  1083. * definitely a GtkWidget and should probably be added to a
  1084. * GtkVbox.)
  1085. *
  1086. * `listitemheight' is used to calculate a usize for list boxes: it
  1087. * should be the height from the size request of a GtkListItem.
  1088. *
  1089. * `win' is required for setting the default button. If it is
  1090. * non-NULL, all buttons created will be default-capable (so they
  1091. * have extra space round them for the default highlight).
  1092. */
  1093. GtkWidget *layout_ctrls(struct dlgparam *dp, struct Shortcuts *scs,
  1094. struct controlset *s, int listitemheight,
  1095. GtkWindow *win)
  1096. {
  1097. Columns *cols;
  1098. GtkWidget *ret;
  1099. int i;
  1100. if (!s->boxname && s->boxtitle) {
  1101. /* This controlset is a panel title. */
  1102. return gtk_label_new(s->boxtitle);
  1103. }
  1104. /*
  1105. * Otherwise, we expect to be laying out actual controls, so
  1106. * we'll start by creating a Columns for the purpose.
  1107. */
  1108. cols = COLUMNS(columns_new(4));
  1109. ret = GTK_WIDGET(cols);
  1110. gtk_widget_show(ret);
  1111. /*
  1112. * Create a containing frame if we have a box name.
  1113. */
  1114. if (*s->boxname) {
  1115. ret = gtk_frame_new(s->boxtitle); /* NULL is valid here */
  1116. gtk_container_set_border_width(GTK_CONTAINER(cols), 4);
  1117. gtk_container_add(GTK_CONTAINER(ret), GTK_WIDGET(cols));
  1118. gtk_widget_show(ret);
  1119. }
  1120. /*
  1121. * Now iterate through the controls themselves, create them,
  1122. * and add them to the Columns.
  1123. */
  1124. for (i = 0; i < s->ncontrols; i++) {
  1125. union control *ctrl = s->ctrls[i];
  1126. struct uctrl *uc;
  1127. int left = FALSE;
  1128. GtkWidget *w = NULL;
  1129. switch (ctrl->generic.type) {
  1130. case CTRL_COLUMNS:
  1131. {
  1132. static const int simplecols[1] = { 100 };
  1133. columns_set_cols(cols, ctrl->columns.ncols,
  1134. (ctrl->columns.percentages ?
  1135. ctrl->columns.percentages : simplecols));
  1136. }
  1137. continue; /* no actual control created */
  1138. case CTRL_TABDELAY:
  1139. {
  1140. struct uctrl *uc = dlg_find_byctrl(dp, ctrl->tabdelay.ctrl);
  1141. if (uc)
  1142. columns_taborder_last(cols, uc->toplevel);
  1143. }
  1144. continue; /* no actual control created */
  1145. }
  1146. uc = snew(struct uctrl);
  1147. uc->ctrl = ctrl;
  1148. uc->privdata = NULL;
  1149. uc->privdata_needs_free = FALSE;
  1150. uc->buttons = NULL;
  1151. uc->entry = uc->list = uc->menu = NULL;
  1152. uc->button = uc->optmenu = uc->text = NULL;
  1153. switch (ctrl->generic.type) {
  1154. case CTRL_BUTTON:
  1155. w = gtk_button_new_with_label(ctrl->generic.label);
  1156. if (win) {
  1157. GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
  1158. if (ctrl->button.isdefault)
  1159. gtk_window_set_default(win, w);
  1160. if (ctrl->button.iscancel)
  1161. dp->cancelbutton = w;
  1162. }
  1163. gtk_signal_connect(GTK_OBJECT(w), "clicked",
  1164. GTK_SIGNAL_FUNC(button_clicked), dp);
  1165. gtk_signal_connect(GTK_OBJECT(w), "focus_in_event",
  1166. GTK_SIGNAL_FUNC(widget_focus), dp);
  1167. shortcut_add(scs, GTK_BIN(w)->child, ctrl->button.shortcut,
  1168. SHORTCUT_UCTRL, uc);
  1169. break;
  1170. case CTRL_CHECKBOX:
  1171. w = gtk_check_button_new_with_label(ctrl->generic.label);
  1172. gtk_signal_connect(GTK_OBJECT(w), "toggled",
  1173. GTK_SIGNAL_FUNC(button_toggled), dp);
  1174. gtk_signal_connect(GTK_OBJECT(w), "focus_in_event",
  1175. GTK_SIGNAL_FUNC(widget_focus), dp);
  1176. shortcut_add(scs, GTK_BIN(w)->child, ctrl->checkbox.shortcut,
  1177. SHORTCUT_UCTRL, uc);
  1178. left = TRUE;
  1179. break;
  1180. case CTRL_RADIO:
  1181. /*
  1182. * Radio buttons get to go inside their own Columns, no
  1183. * matter what.
  1184. */
  1185. {
  1186. gint i, *percentages;
  1187. GSList *group;
  1188. w = columns_new(0);
  1189. if (ctrl->generic.label) {
  1190. GtkWidget *label = gtk_label_new(ctrl->generic.label);
  1191. columns_add(COLUMNS(w), label, 0, 1);
  1192. columns_force_left_align(COLUMNS(w), label);
  1193. gtk_widget_show(label);
  1194. shortcut_add(scs, label, ctrl->radio.shortcut,
  1195. SHORTCUT_UCTRL, uc);
  1196. }
  1197. percentages = g_new(gint, ctrl->radio.ncolumns);
  1198. for (i = 0; i < ctrl->radio.ncolumns; i++) {
  1199. percentages[i] =
  1200. ((100 * (i+1) / ctrl->radio.ncolumns) -
  1201. 100 * i / ctrl->radio.ncolumns);
  1202. }
  1203. columns_set_cols(COLUMNS(w), ctrl->radio.ncolumns,
  1204. percentages);
  1205. g_free(percentages);
  1206. group = NULL;
  1207. uc->nbuttons = ctrl->radio.nbuttons;
  1208. uc->buttons = snewn(uc->nbuttons, GtkWidget *);
  1209. for (i = 0; i < ctrl->radio.nbuttons; i++) {
  1210. GtkWidget *b;
  1211. gint colstart;
  1212. b = (gtk_radio_button_new_with_label
  1213. (group, ctrl->radio.buttons[i]));
  1214. uc->buttons[i] = b;
  1215. group = gtk_radio_button_group(GTK_RADIO_BUTTON(b));
  1216. colstart = i % ctrl->radio.ncolumns;
  1217. columns_add(COLUMNS(w), b, colstart,
  1218. (i == ctrl->radio.nbuttons-1 ?
  1219. ctrl->radio.ncolumns - colstart : 1));
  1220. columns_force_left_align(COLUMNS(w), b);
  1221. gtk_widget_show(b);
  1222. gtk_signal_connect(GTK_OBJECT(b), "toggled",
  1223. GTK_SIGNAL_FUNC(button_toggled), dp);
  1224. gtk_signal_connect(GTK_OBJECT(b), "focus_in_event",
  1225. GTK_SIGNAL_FUNC(widget_focus), dp);
  1226. if (ctrl->radio.shortcuts) {
  1227. shortcut_add(scs, GTK_BIN(b)->child,
  1228. ctrl->radio.shortcuts[i],
  1229. SHORTCUT_UCTRL, uc);
  1230. }
  1231. }
  1232. }
  1233. break;
  1234. case CTRL_EDITBOX:
  1235. {
  1236. GtkRequisition req;
  1237. if (ctrl->editbox.has_list) {
  1238. w = gtk_combo_new();
  1239. gtk_combo_set_value_in_list(GTK_COMBO(w), FALSE, TRUE);
  1240. uc->entry = GTK_COMBO(w)->entry;
  1241. uc->list = GTK_COMBO(w)->list;
  1242. } else {
  1243. w = gtk_entry_new();
  1244. if (ctrl->editbox.password)
  1245. gtk_entry_set_visibility(GTK_ENTRY(w), FALSE);
  1246. uc->entry = w;
  1247. }
  1248. gtk_signal_connect(GTK_OBJECT(uc->entry), "changed",
  1249. GTK_SIGNAL_FUNC(editbox_changed), dp);
  1250. gtk_signal_connect(GTK_OBJECT(uc->entry), "key_press_event",
  1251. GTK_SIGNAL_FUNC(editbox_key), dp);
  1252. gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_in_event",
  1253. GTK_SIGNAL_FUNC(widget_focus), dp);
  1254. /*
  1255. * Edit boxes, for some strange reason, have a minimum
  1256. * width of 150 in GTK 1.2. We don't want this - we'd
  1257. * rather the edit boxes acquired their natural width
  1258. * from the column layout of the rest of the box.
  1259. *
  1260. * Also, while we're here, we'll squirrel away the
  1261. * edit box height so we can use that to centre its
  1262. * label vertically beside it.
  1263. */
  1264. gtk_widget_size_request(w, &req);
  1265. gtk_widget_set_usize(w, 10, req.height);
  1266. if (ctrl->generic.label) {
  1267. GtkWidget *label, *container;
  1268. label = gtk_label_new(ctrl->generic.label);
  1269. shortcut_add(scs, label, ctrl->editbox.shortcut,
  1270. SHORTCUT_FOCUS, uc->entry);
  1271. container = columns_new(4);
  1272. if (ctrl->editbox.percentwidth == 100) {
  1273. columns_add(COLUMNS(container), label, 0, 1);
  1274. columns_force_left_align(COLUMNS(container), label);
  1275. columns_add(COLUMNS(container), w, 0, 1);
  1276. } else {
  1277. gint percentages[2];
  1278. percentages[1] = ctrl->editbox.percentwidth;
  1279. percentages[0] = 100 - ctrl->editbox.percentwidth;
  1280. columns_set_cols(COLUMNS(container), 2, percentages);
  1281. columns_add(COLUMNS(container), label, 0, 1);
  1282. columns_force_left_align(COLUMNS(container), label);
  1283. columns_add(COLUMNS(container), w, 1, 1);
  1284. /* Centre the label vertically. */
  1285. gtk_widget_set_usize(label, -1, req.height);
  1286. gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
  1287. }
  1288. gtk_widget_show(label);
  1289. gtk_widget_show(w);
  1290. w = container;
  1291. }
  1292. gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_out_event",
  1293. GTK_SIGNAL_FUNC(editbox_lostfocus), dp);
  1294. }
  1295. break;
  1296. case CTRL_FILESELECT:
  1297. case CTRL_FONTSELECT:
  1298. {
  1299. GtkWidget *ww;
  1300. GtkRequisition req;
  1301. char *browsebtn =
  1302. (ctrl->generic.type == CTRL_FILESELECT ?
  1303. "Browse..." : "Change...");
  1304. gint percentages[] = { 75, 25 };
  1305. w = columns_new(4);
  1306. columns_set_cols(COLUMNS(w), 2, percentages);
  1307. if (ctrl->generic.label) {
  1308. ww = gtk_label_new(ctrl->generic.label);
  1309. columns_add(COLUMNS(w), ww, 0, 2);
  1310. columns_force_left_align(COLUMNS(w), ww);
  1311. gtk_widget_show(ww);
  1312. shortcut_add(scs, ww,
  1313. (ctrl->generic.type == CTRL_FILESELECT ?
  1314. ctrl->fileselect.shortcut :
  1315. ctrl->fontselect.shortcut),
  1316. SHORTCUT_UCTRL, uc);
  1317. }
  1318. uc->entry = ww = gtk_entry_new();
  1319. gtk_widget_size_request(ww, &req);
  1320. gtk_widget_set_usize(ww, 10, req.height);
  1321. columns_add(COLUMNS(w), ww, 0, 1);
  1322. gtk_widget_show(ww);
  1323. uc->button = ww = gtk_button_new_with_label(browsebtn);
  1324. columns_add(COLUMNS(w), ww, 1, 1);
  1325. gtk_widget_show(ww);
  1326. gtk_signal_connect(GTK_OBJECT(uc->entry), "key_press_event",
  1327. GTK_SIGNAL_FUNC(editbox_key), dp);
  1328. gtk_signal_connect(GTK_OBJECT(uc->entry), "changed",
  1329. GTK_SIGNAL_FUNC(editbox_changed), dp);
  1330. gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_in_event",
  1331. GTK_SIGNAL_FUNC(widget_focus), dp);
  1332. gtk_signal_connect(GTK_OBJECT(uc->button), "focus_in_event",
  1333. GTK_SIGNAL_FUNC(widget_focus), dp);
  1334. gtk_signal_connect(GTK_OBJECT(ww), "clicked",
  1335. GTK_SIGNAL_FUNC(filefont_clicked), dp);
  1336. }
  1337. break;
  1338. case CTRL_LISTBOX:
  1339. if (ctrl->listbox.height == 0) {
  1340. uc->optmenu = w = gtk_option_menu_new();
  1341. uc->menu = gtk_menu_new();
  1342. gtk_option_menu_set_menu(GTK_OPTION_MENU(w), uc->menu);
  1343. gtk_object_set_data(GTK_OBJECT(uc->menu), "user-data",
  1344. (gpointer)uc->optmenu);
  1345. gtk_signal_connect(GTK_OBJECT(uc->optmenu), "focus_in_event",
  1346. GTK_SIGNAL_FUNC(widget_focus), dp);
  1347. } else {
  1348. uc->list = gtk_list_new();
  1349. if (ctrl->listbox.multisel == 2) {
  1350. gtk_list_set_selection_mode(GTK_LIST(uc->list),
  1351. GTK_SELECTION_EXTENDED);
  1352. } else if (ctrl->listbox.multisel == 1) {
  1353. gtk_list_set_selection_mode(GTK_LIST(uc->list),
  1354. GTK_SELECTION_MULTIPLE);
  1355. } else {
  1356. gtk_list_set_selection_mode(GTK_LIST(uc->list),
  1357. GTK_SELECTION_SINGLE);
  1358. }
  1359. w = gtk_scrolled_window_new(NULL, NULL);
  1360. gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(w),
  1361. uc->list);
  1362. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w),
  1363. GTK_POLICY_NEVER,
  1364. GTK_POLICY_AUTOMATIC);
  1365. uc->adj = gtk_scrolled_window_get_vadjustment
  1366. (GTK_SCROLLED_WINDOW(w));
  1367. gtk_widget_show(uc->list);
  1368. gtk_signal_connect(GTK_OBJECT(uc->list), "selection-changed",
  1369. GTK_SIGNAL_FUNC(list_selchange), dp);
  1370. gtk_signal_connect(GTK_OBJECT(uc->list), "focus_in_event",
  1371. GTK_SIGNAL_FUNC(widget_focus), dp);
  1372. /*
  1373. * Adjust the height of the scrolled window to the
  1374. * minimum given by the height parameter.
  1375. *
  1376. * This piece of guesswork is a horrid hack based
  1377. * on looking inside the GTK 1.2 sources
  1378. * (specifically gtkviewport.c, which appears to be
  1379. * the widget which provides the border around the
  1380. * scrolling area). Anyone lets me know how I can
  1381. * do this in a way which isn't at risk from GTK
  1382. * upgrades, I'd be grateful.
  1383. */
  1384. {
  1385. int edge = GTK_WIDGET(uc->list)->style->klass->ythickness;
  1386. gtk_widget_set_usize(w, 10,
  1387. 2*edge + (ctrl->listbox.height *
  1388. listitemheight));
  1389. }
  1390. if (ctrl->listbox.draglist) {
  1391. /*
  1392. * GTK doesn't appear to make it easy to
  1393. * implement a proper draggable list; so
  1394. * instead I'm just going to have to put an Up
  1395. * and a Down button to the right of the actual
  1396. * list box. Ah well.
  1397. */
  1398. GtkWidget *cols, *button;
  1399. static const gint percentages[2] = { 80, 20 };
  1400. cols = columns_new(4);
  1401. columns_set_cols(COLUMNS(cols), 2, percentages);
  1402. columns_add(COLUMNS(cols), w, 0, 1);
  1403. gtk_widget_show(w);
  1404. button = gtk_button_new_with_label("Up");
  1405. columns_add(COLUMNS(cols), button, 1, 1);
  1406. gtk_widget_show(button);
  1407. gtk_signal_connect(GTK_OBJECT(button), "clicked",
  1408. GTK_SIGNAL_FUNC(draglist_up), dp);
  1409. gtk_signal_connect(GTK_OBJECT(button), "focus_in_event",
  1410. GTK_SIGNAL_FUNC(widget_focus), dp);
  1411. button = gtk_button_new_with_label("Down");
  1412. columns_add(COLUMNS(cols), button, 1, 1);
  1413. gtk_widget_show(button);
  1414. gtk_signal_connect(GTK_OBJECT(button), "clicked",
  1415. GTK_SIGNAL_FUNC(draglist_down), dp);
  1416. gtk_signal_connect(GTK_OBJECT(button), "focus_in_event",
  1417. GTK_SIGNAL_FUNC(widget_focus), dp);
  1418. w = cols;
  1419. }
  1420. }
  1421. if (ctrl->generic.label) {
  1422. GtkWidget *label, *container;
  1423. label = gtk_label_new(ctrl->generic.label);
  1424. container = columns_new(4);
  1425. if (ctrl->listbox.percentwidth == 100) {
  1426. columns_add(COLUMNS(container), label, 0, 1);
  1427. columns_force_left_align(COLUMNS(container), label);
  1428. columns_add(COLUMNS(container), w, 0, 1);
  1429. } else {
  1430. gint percentages[2];
  1431. percentages[1] = ctrl->listbox.percentwidth;
  1432. percentages[0] = 100 - ctrl->listbox.percentwidth;
  1433. columns_set_cols(COLUMNS(container), 2, percentages);
  1434. columns_add(COLUMNS(container), label, 0, 1);
  1435. columns_force_left_align(COLUMNS(container), label);
  1436. columns_add(COLUMNS(container), w, 1, 1);
  1437. }
  1438. gtk_widget_show(label);
  1439. gtk_widget_show(w);
  1440. shortcut_add(scs, label, ctrl->listbox.shortcut,
  1441. SHORTCUT_UCTRL, uc);
  1442. w = container;
  1443. }
  1444. break;
  1445. case CTRL_TEXT:
  1446. /*
  1447. * Wrapping text widgets don't sit well with the GTK
  1448. * layout model, in which widgets state a minimum size
  1449. * and the whole window then adjusts to the smallest
  1450. * size it can sensibly take given its contents. A
  1451. * wrapping text widget _has_ no clear minimum size;
  1452. * instead it has a range of possibilities. It can be
  1453. * one line deep but 2000 wide, or two lines deep and
  1454. * 1000 pixels, or three by 867, or four by 500 and so
  1455. * on. It can be as short as you like provided you
  1456. * don't mind it being wide, or as narrow as you like
  1457. * provided you don't mind it being tall.
  1458. *
  1459. * Therefore, it fits very badly into the layout model.
  1460. * Hence the only thing to do is pick a width and let
  1461. * it choose its own number of lines. To do this I'm
  1462. * going to cheat a little. All new wrapping text
  1463. * widgets will be created with a minimal text content
  1464. * "X"; then, after the rest of the dialog box is set
  1465. * up and its size calculated, the text widgets will be
  1466. * told their width and given their real text, which
  1467. * will cause the size to be recomputed in the y
  1468. * direction (because many of them will expand to more
  1469. * than one line).
  1470. */
  1471. uc->text = w = gtk_label_new("X");
  1472. gtk_misc_set_alignment(GTK_MISC(w), 0.0, 0.0);
  1473. gtk_label_set_line_wrap(GTK_LABEL(w), TRUE);
  1474. uc->textsig =
  1475. gtk_signal_connect(GTK_OBJECT(w), "size-allocate",
  1476. GTK_SIGNAL_FUNC(label_sizealloc), dp);
  1477. break;
  1478. }
  1479. assert(w != NULL);
  1480. columns_add(cols, w,
  1481. COLUMN_START(ctrl->generic.column),
  1482. COLUMN_SPAN(ctrl->generic.column));
  1483. if (left)
  1484. columns_force_left_align(cols, w);
  1485. gtk_widget_show(w);
  1486. uc->toplevel = w;
  1487. dlg_add_uctrl(dp, uc);
  1488. }
  1489. return ret;
  1490. }
  1491. struct selparam {
  1492. struct dlgparam *dp;
  1493. Panels *panels;
  1494. GtkWidget *panel, *treeitem;
  1495. struct Shortcuts shortcuts;
  1496. };
  1497. static void treeitem_sel(GtkItem *item, gpointer data)
  1498. {
  1499. struct selparam *sp = (struct selparam *)data;
  1500. panels_switch_to(sp->panels, sp->panel);
  1501. sp->dp->shortcuts = &sp->shortcuts;
  1502. sp->dp->currtreeitem = sp->treeitem;
  1503. }
  1504. static void window_destroy(GtkWidget *widget, gpointer data)
  1505. {
  1506. gtk_main_quit();
  1507. }
  1508. static int tree_grab_focus(struct dlgparam *dp)
  1509. {
  1510. int i, f;
  1511. /*
  1512. * See if any of the treeitems has the focus.
  1513. */
  1514. f = -1;
  1515. for (i = 0; i < dp->ntreeitems; i++)
  1516. if (GTK_WIDGET_HAS_FOCUS(dp->treeitems[i])) {
  1517. f = i;
  1518. break;
  1519. }
  1520. if (f >= 0)
  1521. return FALSE;
  1522. else {
  1523. gtk_widget_grab_focus(dp->currtreeitem);
  1524. return TRUE;
  1525. }
  1526. }
  1527. gint tree_focus(GtkContainer *container, GtkDirectionType direction,
  1528. gpointer data)
  1529. {
  1530. struct dlgparam *dp = (struct dlgparam *)data;
  1531. gtk_signal_emit_stop_by_name(GTK_OBJECT(container), "focus");
  1532. /*
  1533. * If there's a focused treeitem, we return FALSE to cause the
  1534. * focus to move on to some totally other control. If not, we
  1535. * focus the selected one.
  1536. */
  1537. return tree_grab_focus(dp);
  1538. }
  1539. int win_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
  1540. {
  1541. struct dlgparam *dp = (struct dlgparam *)data;
  1542. if (event->keyval == GDK_Escape && dp->cancelbutton) {
  1543. gtk_signal_emit_by_name(GTK_OBJECT(dp->cancelbutton), "clicked");
  1544. return TRUE;
  1545. }
  1546. if ((event->state & GDK_MOD1_MASK) &&
  1547. (unsigned char)event->string[0] > 0 &&
  1548. (unsigned char)event->string[0] <= 127) {
  1549. int schr = (unsigned char)event->string[0];
  1550. struct Shortcut *sc = &dp->shortcuts->sc[schr];
  1551. switch (sc->action) {
  1552. case SHORTCUT_TREE:
  1553. tree_grab_focus(dp);
  1554. break;
  1555. case SHORTCUT_FOCUS:
  1556. gtk_widget_grab_focus(sc->widget);
  1557. break;
  1558. case SHORTCUT_UCTRL:
  1559. /*
  1560. * We must do something sensible with a uctrl.
  1561. * Precisely what this is depends on the type of
  1562. * control.
  1563. */
  1564. switch (sc->uc->ctrl->generic.type) {
  1565. case CTRL_CHECKBOX:
  1566. case CTRL_BUTTON:
  1567. /* Check boxes and buttons get the focus _and_ get toggled. */
  1568. gtk_widget_grab_focus(sc->uc->toplevel);
  1569. gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->toplevel),
  1570. "clicked");
  1571. break;
  1572. case CTRL_FILESELECT:
  1573. case CTRL_FONTSELECT:
  1574. /* File/font selectors have their buttons pressed (ooer),
  1575. * and focus transferred to the edit box. */
  1576. gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->button),
  1577. "clicked");
  1578. gtk_widget_grab_focus(sc->uc->entry);
  1579. break;
  1580. case CTRL_RADIO:
  1581. /*
  1582. * Radio buttons are fun, because they have
  1583. * multiple shortcuts. We must find whether the
  1584. * activated shortcut is the shortcut for the whole
  1585. * group, or for a particular button. In the former
  1586. * case, we find the currently selected button and
  1587. * focus it; in the latter, we focus-and-click the
  1588. * button whose shortcut was pressed.
  1589. */
  1590. if (schr == sc->uc->ctrl->radio.shortcut) {
  1591. int i;
  1592. for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++)
  1593. if (gtk_toggle_button_get_active
  1594. (GTK_TOGGLE_BUTTON(sc->uc->buttons[i]))) {
  1595. gtk_widget_grab_focus(sc->uc->buttons[i]);
  1596. }
  1597. } else if (sc->uc->ctrl->radio.shortcuts) {
  1598. int i;
  1599. for (i = 0; i < sc->uc->ctrl->radio.nbuttons; i++)
  1600. if (schr == sc->uc->ctrl->radio.shortcuts[i]) {
  1601. gtk_widget_grab_focus(sc->uc->buttons[i]);
  1602. gtk_signal_emit_by_name
  1603. (GTK_OBJECT(sc->uc->buttons[i]), "clicked");
  1604. }
  1605. }
  1606. break;
  1607. case CTRL_LISTBOX:
  1608. /*
  1609. * If the list is really an option menu, we focus
  1610. * and click it. Otherwise we tell it to focus one
  1611. * of its children, which appears to do the Right
  1612. * Thing.
  1613. */
  1614. if (sc->uc->optmenu) {
  1615. GdkEventButton bev;
  1616. gint returnval;
  1617. gtk_widget_grab_focus(sc->uc->optmenu);
  1618. /* Option menus don't work using the "clicked" signal.
  1619. * We need to manufacture a button press event :-/ */
  1620. bev.type = GDK_BUTTON_PRESS;
  1621. bev.button = 1;
  1622. gtk_signal_emit_by_name(GTK_OBJECT(sc->uc->optmenu),
  1623. "button_press_event",
  1624. &bev, &returnval);
  1625. } else {
  1626. assert(sc->uc->list != NULL);
  1627. gtk_container_focus(GTK_CONTAINER(sc->uc->list),
  1628. GTK_DIR_TAB_FORWARD);
  1629. }
  1630. break;
  1631. }
  1632. break;
  1633. }
  1634. }
  1635. return FALSE;
  1636. }
  1637. int tree_key_press(GtkWidget *widget, GdkEventKey *event, gpointer data)
  1638. {
  1639. struct dlgparam *dp = (struct dlgparam *)data;
  1640. if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up ||
  1641. event->keyval == GDK_Down || event->keyval == GDK_KP_Down) {
  1642. int dir, i, j = -1;
  1643. for (i = 0; i < dp->ntreeitems; i++)
  1644. if (widget == dp->treeitems[i])
  1645. break;
  1646. if (i < dp->ntreeitems) {
  1647. if (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
  1648. dir = -1;
  1649. else
  1650. dir = +1;
  1651. while (1) {
  1652. i += dir;
  1653. if (i < 0 || i >= dp->ntreeitems)
  1654. break; /* nothing in that dir to select */
  1655. /*
  1656. * Determine if this tree item is visible.
  1657. */
  1658. {
  1659. GtkWidget *w = dp->treeitems[i];
  1660. int vis = TRUE;
  1661. while (w && (GTK_IS_TREE_ITEM(w) || GTK_IS_TREE(w))) {
  1662. if (!GTK_WIDGET_VISIBLE(w)) {
  1663. vis = FALSE;
  1664. break;
  1665. }
  1666. w = w->parent;
  1667. }
  1668. if (vis) {
  1669. j = i; /* got one */
  1670. break;
  1671. }
  1672. }
  1673. }
  1674. }
  1675. gtk_signal_emit_stop_by_name(GTK_OBJECT(widget),
  1676. "key_press_event");
  1677. if (j >= 0) {
  1678. gtk_signal_emit_by_name(GTK_OBJECT(dp->treeitems[j]), "toggle");
  1679. gtk_widget_grab_focus(dp->treeitems[j]);
  1680. }
  1681. return TRUE;
  1682. }
  1683. /*
  1684. * It's nice for Left and Right to expand and collapse tree
  1685. * branches.
  1686. */
  1687. if (event->keyval == GDK_Left || event->keyval == GDK_KP_Left) {
  1688. gtk_signal_emit_stop_by_name(GTK_OBJECT(widget),
  1689. "key_press_event");
  1690. gtk_tree_item_collapse(GTK_TREE_ITEM(widget));
  1691. return TRUE;
  1692. }
  1693. if (event->keyval == GDK_Right || event->keyval == GDK_KP_Right) {
  1694. gtk_signal_emit_stop_by_name(GTK_OBJECT(widget),
  1695. "key_press_event");
  1696. gtk_tree_item_expand(GTK_TREE_ITEM(widget));
  1697. return TRUE;
  1698. }
  1699. return FALSE;
  1700. }
  1701. void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw,
  1702. int chr, int action, void *ptr)
  1703. {
  1704. GtkLabel *label = GTK_LABEL(labelw);
  1705. gchar *currstr, *pattern;
  1706. int i;
  1707. if (chr == NO_SHORTCUT)
  1708. return;
  1709. chr = tolower((unsigned char)chr);
  1710. assert(scs->sc[chr].action == SHORTCUT_EMPTY);
  1711. scs->sc[chr].action = action;
  1712. if (action == SHORTCUT_FOCUS) {
  1713. scs->sc[chr].uc = NULL;
  1714. scs->sc[chr].widget = (GtkWidget *)ptr;
  1715. } else {
  1716. scs->sc[chr].widget = NULL;
  1717. scs->sc[chr].uc = (struct uctrl *)ptr;
  1718. }
  1719. gtk_label_get(label, &currstr);
  1720. for (i = 0; currstr[i]; i++)
  1721. if (tolower((unsigned char)currstr[i]) == chr) {
  1722. GtkRequisition req;
  1723. pattern = dupprintf("%*s_", i, "");
  1724. gtk_widget_size_request(GTK_WIDGET(label), &req);
  1725. gtk_label_set_pattern(label, pattern);
  1726. gtk_widget_set_usize(GTK_WIDGET(label), -1, req.height);
  1727. sfree(pattern);
  1728. break;
  1729. }
  1730. }
  1731. int get_listitemheight(void)
  1732. {
  1733. GtkWidget *listitem = gtk_list_item_new_with_label("foo");
  1734. GtkRequisition req;
  1735. gtk_widget_size_request(listitem, &req);
  1736. gtk_widget_unref(listitem);
  1737. return req.height;
  1738. }
  1739. int do_config_box(const char *title, Config *cfg, int midsession)
  1740. {
  1741. GtkWidget *window, *hbox, *vbox, *cols, *label,
  1742. *tree, *treescroll, *panels, *panelvbox;
  1743. int index, level, listitemheight;
  1744. struct controlbox *ctrlbox;
  1745. char *path;
  1746. GtkTreeItem *treeitemlevels[8];
  1747. GtkTree *treelevels[8];
  1748. struct dlgparam dp;
  1749. struct sesslist sl;
  1750. struct Shortcuts scs;
  1751. struct selparam *selparams = NULL;
  1752. int nselparams = 0, selparamsize = 0;
  1753. dlg_init(&dp);
  1754. get_sesslist(&sl, TRUE);
  1755. listitemheight = get_listitemheight();
  1756. for (index = 0; index < lenof(scs.sc); index++) {
  1757. scs.sc[index].action = SHORTCUT_EMPTY;
  1758. }
  1759. window = gtk_dialog_new();
  1760. ctrlbox = ctrl_new_box();
  1761. setup_config_box(ctrlbox, &sl, midsession, cfg->protocol);
  1762. unix_setup_config_box(ctrlbox, midsession, window);
  1763. gtk_window_set_title(GTK_WINDOW(window), title);
  1764. hbox = gtk_hbox_new(FALSE, 4);
  1765. gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox), hbox, TRUE, TRUE, 0);
  1766. gtk_container_set_border_width(GTK_CONTAINER(hbox), 10);
  1767. gtk_widget_show(hbox);
  1768. vbox = gtk_vbox_new(FALSE, 4);
  1769. gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
  1770. gtk_widget_show(vbox);
  1771. cols = columns_new(4);
  1772. gtk_box_pack_start(GTK_BOX(vbox), cols, FALSE, FALSE, 0);
  1773. gtk_widget_show(cols);
  1774. label = gtk_label_new("Category:");
  1775. columns_add(COLUMNS(cols), label, 0, 1);
  1776. columns_force_left_align(COLUMNS(cols), label);
  1777. gtk_widget_show(label);
  1778. treescroll = gtk_scrolled_window_new(NULL, NULL);
  1779. tree = gtk_tree_new();
  1780. gtk_signal_connect(GTK_OBJECT(tree), "focus_in_event",
  1781. GTK_SIGNAL_FUNC(widget_focus), &dp);
  1782. shortcut_add(&scs, label, 'g', SHORTCUT_TREE, tree);
  1783. gtk_tree_set_view_mode(GTK_TREE(tree), GTK_TREE_VIEW_ITEM);
  1784. gtk_tree_set_selection_mode(GTK_TREE(tree), GTK_SELECTION_BROWSE);
  1785. gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(treescroll),
  1786. tree);
  1787. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(treescroll),
  1788. GTK_POLICY_NEVER,
  1789. GTK_POLICY_AUTOMATIC);
  1790. gtk_signal_connect(GTK_OBJECT(tree), "focus",
  1791. GTK_SIGNAL_FUNC(tree_focus), &dp);
  1792. gtk_widget_show(tree);
  1793. gtk_widget_show(treescroll);
  1794. gtk_box_pack_start(GTK_BOX(vbox), treescroll, TRUE, TRUE, 0);
  1795. panels = panels_new();
  1796. gtk_box_pack_start(GTK_BOX(hbox), panels, TRUE, TRUE, 0);
  1797. gtk_widget_show(panels);
  1798. panelvbox = NULL;
  1799. path = NULL;
  1800. level = 0;
  1801. for (index = 0; index < ctrlbox->nctrlsets; index++) {
  1802. struct controlset *s = ctrlbox->ctrlsets[index];
  1803. GtkWidget *w;
  1804. if (!*s->pathname) {
  1805. w = layout_ctrls(&dp, &scs, s, listitemheight, GTK_WINDOW(window));
  1806. gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->action_area),
  1807. w, TRUE, TRUE, 0);
  1808. } else {
  1809. int j = path ? ctrl_path_compare(s->pathname, path) : 0;
  1810. if (j != INT_MAX) { /* add to treeview, start new panel */
  1811. char *c;
  1812. GtkWidget *treeitem;
  1813. int first;
  1814. /*
  1815. * We expect never to find an implicit path
  1816. * component. For example, we expect never to see
  1817. * A/B/C followed by A/D/E, because that would
  1818. * _implicitly_ create A/D. All our path prefixes
  1819. * are expected to contain actual controls and be
  1820. * selectable in the treeview; so we would expect
  1821. * to see A/D _explicitly_ before encountering
  1822. * A/D/E.
  1823. */
  1824. assert(j == ctrl_path_elements(s->pathname) - 1);
  1825. c = strrchr(s->pathname, '/');
  1826. if (!c)
  1827. c = s->pathname;
  1828. else
  1829. c++;
  1830. treeitem = gtk_tree_item_new_with_label(c);
  1831. assert(j-1 < level);
  1832. if (j > 0) {
  1833. if (!treelevels[j-1]) {
  1834. treelevels[j-1] = GTK_TREE(gtk_tree_new());
  1835. gtk_tree_item_set_subtree
  1836. (treeitemlevels[j-1],
  1837. GTK_WIDGET(treelevels[j-1]));
  1838. gtk_tree_item_expand(treeitemlevels[j-1]);
  1839. }
  1840. gtk_tree_append(treelevels[j-1], treeitem);
  1841. } else {
  1842. gtk_tree_append(GTK_TREE(tree), treeitem);
  1843. }
  1844. treeitemlevels[j] = GTK_TREE_ITEM(treeitem);
  1845. treelevels[j] = NULL;
  1846. level = j+1;
  1847. gtk_signal_connect(GTK_OBJECT(treeitem), "key_press_event",
  1848. GTK_SIGNAL_FUNC(tree_key_press), &dp);
  1849. gtk_signal_connect(GTK_OBJECT(treeitem), "focus_in_event",
  1850. GTK_SIGNAL_FUNC(widget_focus), &dp);
  1851. gtk_widget_show(treeitem);
  1852. path = s->pathname;
  1853. first = (panelvbox == NULL);
  1854. panelvbox = gtk_vbox_new(FALSE, 4);
  1855. gtk_container_add(GTK_CONTAINER(panels), panelvbox);
  1856. if (first) {
  1857. panels_switch_to(PANELS(panels), panelvbox);
  1858. gtk_tree_select_child(GTK_TREE(tree), treeitem);
  1859. }
  1860. if (nselparams >= selparamsize) {
  1861. selparamsize += 16;
  1862. selparams = sresize(selparams, selparamsize,
  1863. struct selparam);
  1864. }
  1865. selparams[nselparams].dp = &dp;
  1866. selparams[nselparams].panels = PANELS(panels);
  1867. selparams[nselparams].panel = panelvbox;
  1868. selparams[nselparams].shortcuts = scs; /* structure copy */
  1869. selparams[nselparams].treeitem = treeitem;
  1870. nselparams++;
  1871. }
  1872. w = layout_ctrls(&dp,
  1873. &selparams[nselparams-1].shortcuts,
  1874. s, listitemheight, NULL);
  1875. gtk_box_pack_start(GTK_BOX(panelvbox), w, FALSE, FALSE, 0);
  1876. gtk_widget_show(w);
  1877. }
  1878. }
  1879. dp.ntreeitems = nselparams;
  1880. dp.treeitems = snewn(dp.ntreeitems, GtkWidget *);
  1881. for (index = 0; index < nselparams; index++) {
  1882. gtk_signal_connect(GTK_OBJECT(selparams[index].treeitem), "select",
  1883. GTK_SIGNAL_FUNC(treeitem_sel),
  1884. &selparams[index]);
  1885. dp.treeitems[index] = selparams[index].treeitem;
  1886. }
  1887. dp.data = cfg;
  1888. dlg_refresh(NULL, &dp);
  1889. dp.shortcuts = &selparams[0].shortcuts;
  1890. dp.currtreeitem = dp.treeitems[0];
  1891. dp.lastfocus = NULL;
  1892. dp.retval = 0;
  1893. dp.window = window;
  1894. gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  1895. gtk_widget_show(window);
  1896. /*
  1897. * Set focus into the first available control.
  1898. */
  1899. for (index = 0; index < ctrlbox->nctrlsets; index++) {
  1900. struct controlset *s = ctrlbox->ctrlsets[index];
  1901. int done = 0;
  1902. int j;
  1903. if (*s->pathname) {
  1904. for (j = 0; j < s->ncontrols; j++)
  1905. if (s->ctrls[j]->generic.type != CTRL_TABDELAY &&
  1906. s->ctrls[j]->generic.type != CTRL_COLUMNS &&
  1907. s->ctrls[j]->generic.type != CTRL_TEXT) {
  1908. dlg_set_focus(s->ctrls[j], &dp);
  1909. dp.lastfocus = s->ctrls[j];
  1910. done = 1;
  1911. break;
  1912. }
  1913. }
  1914. if (done)
  1915. break;
  1916. }
  1917. gtk_signal_connect(GTK_OBJECT(window), "destroy",
  1918. GTK_SIGNAL_FUNC(window_destroy), NULL);
  1919. gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
  1920. GTK_SIGNAL_FUNC(win_key_press), &dp);
  1921. gtk_main();
  1922. get_sesslist(&sl, FALSE);
  1923. dlg_cleanup(&dp);
  1924. sfree(selparams);
  1925. return dp.retval;
  1926. }
  1927. static void messagebox_handler(union control *ctrl, void *dlg,
  1928. void *data, int event)
  1929. {
  1930. if (event == EVENT_ACTION)
  1931. dlg_end(dlg, ctrl->generic.context.i);
  1932. }
  1933. int messagebox(GtkWidget *parentwin, char *title, char *msg, int minwid, ...)
  1934. {
  1935. GtkWidget *window, *w0, *w1;
  1936. struct controlbox *ctrlbox;
  1937. struct controlset *s0, *s1;
  1938. union control *c;
  1939. struct dlgparam dp;
  1940. struct Shortcuts scs;
  1941. int index, ncols;
  1942. va_list ap;
  1943. dlg_init(&dp);
  1944. for (index = 0; index < lenof(scs.sc); index++) {
  1945. scs.sc[index].action = SHORTCUT_EMPTY;
  1946. }
  1947. ctrlbox = ctrl_new_box();
  1948. ncols = 0;
  1949. va_start(ap, minwid);
  1950. while (va_arg(ap, char *) != NULL) {
  1951. ncols++;
  1952. (void) va_arg(ap, int); /* shortcut */
  1953. (void) va_arg(ap, int); /* normal/default/cancel */
  1954. (void) va_arg(ap, int); /* end value */
  1955. }
  1956. va_end(ap);
  1957. s0 = ctrl_getset(ctrlbox, "", "", "");
  1958. c = ctrl_columns(s0, 2, 50, 50);
  1959. c->columns.ncols = s0->ncolumns = ncols;
  1960. c->columns.percentages = sresize(c->columns.percentages, ncols, int);
  1961. for (index = 0; index < ncols; index++)
  1962. c->columns.percentages[index] = (index+1)*100/ncols - index*100/ncols;
  1963. va_start(ap, minwid);
  1964. index = 0;
  1965. while (1) {
  1966. char *title = va_arg(ap, char *);
  1967. int shortcut, type, value;
  1968. if (title == NULL)
  1969. break;
  1970. shortcut = va_arg(ap, int);
  1971. type = va_arg(ap, int);
  1972. value = va_arg(ap, int);
  1973. c = ctrl_pushbutton(s0, title, shortcut, HELPCTX(no_help),
  1974. messagebox_handler, I(value));
  1975. c->generic.column = index++;
  1976. if (type > 0)
  1977. c->button.isdefault = TRUE;
  1978. else if (type < 0)
  1979. c->button.iscancel = TRUE;
  1980. }
  1981. va_end(ap);
  1982. s1 = ctrl_getset(ctrlbox, "x", "", "");
  1983. ctrl_text(s1, msg, HELPCTX(no_help));
  1984. window = gtk_dialog_new();
  1985. gtk_window_set_title(GTK_WINDOW(window), title);
  1986. w0 = layout_ctrls(&dp, &scs, s0, 0, GTK_WINDOW(window));
  1987. gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->action_area),
  1988. w0, TRUE, TRUE, 0);
  1989. gtk_widget_show(w0);
  1990. w1 = layout_ctrls(&dp, &scs, s1, 0, GTK_WINDOW(window));
  1991. gtk_container_set_border_width(GTK_CONTAINER(w1), 10);
  1992. gtk_widget_set_usize(w1, minwid+20, -1);
  1993. gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
  1994. w1, TRUE, TRUE, 0);
  1995. gtk_widget_show(w1);
  1996. dp.shortcuts = &scs;
  1997. dp.lastfocus = NULL;
  1998. dp.retval = 0;
  1999. dp.window = window;
  2000. gtk_window_set_modal(GTK_WINDOW(window), TRUE);
  2001. if (parentwin) {
  2002. set_transient_window_pos(parentwin, window);
  2003. gtk_window_set_transient_for(GTK_WINDOW(window),
  2004. GTK_WINDOW(parentwin));
  2005. } else
  2006. gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  2007. gtk_widget_show(window);
  2008. gtk_signal_connect(GTK_OBJECT(window), "destroy",
  2009. GTK_SIGNAL_FUNC(window_destroy), NULL);
  2010. gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
  2011. GTK_SIGNAL_FUNC(win_key_press), &dp);
  2012. gtk_main();
  2013. dlg_cleanup(&dp);
  2014. ctrl_free_box(ctrlbox);
  2015. return dp.retval;
  2016. }
  2017. static int string_width(char *text)
  2018. {
  2019. GtkWidget *label = gtk_label_new(text);
  2020. GtkRequisition req;
  2021. gtk_widget_size_request(label, &req);
  2022. gtk_widget_unref(label);
  2023. return req.width;
  2024. }
  2025. int reallyclose(void *frontend)
  2026. {
  2027. char *title = dupcat(appname, " Exit Confirmation", NULL);
  2028. int ret = messagebox(GTK_WIDGET(get_window(frontend)),
  2029. title, "Are you sure you want to close this session?",
  2030. string_width("Most of the width of the above text"),
  2031. "Yes", 'y', +1, 1,
  2032. "No", 'n', -1, 0,
  2033. NULL);
  2034. sfree(title);
  2035. return ret;
  2036. }
  2037. void verify_ssh_host_key(void *frontend, char *host, int port, char *keytype,
  2038. char *keystr, char *fingerprint)
  2039. {
  2040. static const char absenttxt[] =
  2041. "The server's host key is not cached. You have no guarantee "
  2042. "that the server is the computer you think it is.\n"
  2043. "The server's %s key fingerprint is:\n"
  2044. "%s\n"
  2045. "If you trust this host, press \"Accept\" to add the key to "
  2046. "PuTTY's cache and carry on connecting.\n"
  2047. "If you want to carry on connecting just once, without "
  2048. "adding the key to the cache, press \"Connect Once\".\n"
  2049. "If you do not trust this host, press \"Cancel\" to abandon the "
  2050. "connection.";
  2051. static const char wrongtxt[] =
  2052. "WARNING - POTENTIAL SECURITY BREACH!\n"
  2053. "The server's host key does not match the one PuTTY has "
  2054. "cached. This means that either the server administrator "
  2055. "has changed the host key, or you have actually connected "
  2056. "to another computer pretending to be the server.\n"
  2057. "The new %s key fingerprint is:\n"
  2058. "%s\n"
  2059. "If you were expecting this change and trust the new key, "
  2060. "press \"Accept\" to update PuTTY's cache and continue connecting.\n"
  2061. "If you want to carry on connecting but without updating "
  2062. "the cache, press \"Connect Once\".\n"
  2063. "If you want to abandon the connection completely, press "
  2064. "\"Cancel\" to cancel. Pressing \"Cancel\" is the ONLY guaranteed "
  2065. "safe choice.";
  2066. char *text;
  2067. int ret;
  2068. /*
  2069. * Verify the key.
  2070. */
  2071. ret = verify_host_key(host, port, keytype, keystr);
  2072. if (ret == 0) /* success - key matched OK */
  2073. return;
  2074. text = dupprintf((ret == 2 ? wrongtxt : absenttxt), keytype, fingerprint);
  2075. ret = messagebox(GTK_WIDGET(get_window(frontend)),
  2076. "PuTTY Security Alert", text,
  2077. string_width(fingerprint),
  2078. "Accept", 'a', 0, 2,
  2079. "Connect Once", 'o', 0, 1,
  2080. "Cancel", 'c', -1, 0,
  2081. NULL);
  2082. sfree(text);
  2083. if (ret == 0)
  2084. cleanup_exit(0);
  2085. else if (ret == 2)
  2086. store_host_key(host, port, keytype, keystr);
  2087. }
  2088. /*
  2089. * Ask whether the selected cipher is acceptable (since it was
  2090. * below the configured 'warn' threshold).
  2091. * cs: 0 = both ways, 1 = client->server, 2 = server->client
  2092. */
  2093. void askcipher(void *frontend, char *ciphername, int cs)
  2094. {
  2095. static const char msg[] =
  2096. "The first %scipher supported by the server is "
  2097. "%s, which is below the configured warning threshold.\n"
  2098. "Continue with connection?";
  2099. char *text;
  2100. int ret;
  2101. text = dupprintf(msg, (cs == 0) ? "" :
  2102. (cs == 1) ? "client-to-server " : "server-to-client ",
  2103. ciphername);
  2104. ret = messagebox(GTK_WIDGET(get_window(frontend)),
  2105. "PuTTY Security Alert", text,
  2106. string_width("Continue with connection?"),
  2107. "Yes", 'y', 0, 1,
  2108. "No", 'n', 0, 0,
  2109. NULL);
  2110. sfree(text);
  2111. if (ret) {
  2112. return;
  2113. } else {
  2114. cleanup_exit(0);
  2115. }
  2116. }
  2117. void old_keyfile_warning(void)
  2118. {
  2119. /*
  2120. * This should never happen on Unix. We hope.
  2121. */
  2122. }
  2123. void fatal_message_box(void *window, char *msg)
  2124. {
  2125. messagebox(window, "PuTTY Fatal Error", msg,
  2126. string_width("REASONABLY LONG LINE OF TEXT FOR BASIC SANITY"),
  2127. "OK", 'o', 1, 1, NULL);
  2128. }
  2129. void fatalbox(char *p, ...)
  2130. {
  2131. va_list ap;
  2132. char *msg;
  2133. va_start(ap, p);
  2134. msg = dupvprintf(p, ap);
  2135. va_end(ap);
  2136. fatal_message_box(NULL, msg);
  2137. sfree(msg);
  2138. cleanup_exit(1);
  2139. }
  2140. static GtkWidget *aboutbox = NULL;
  2141. static void about_close_clicked(GtkButton *button, gpointer data)
  2142. {
  2143. gtk_widget_destroy(aboutbox);
  2144. aboutbox = NULL;
  2145. }
  2146. static void licence_clicked(GtkButton *button, gpointer data)
  2147. {
  2148. char *title;
  2149. char *licence =
  2150. "Copyright 1997-2004 Simon Tatham.\n\n"
  2151. "Portions copyright Robert de Bath, Joris van Rantwijk, Delian "
  2152. "Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas "
  2153. "Barry, Justin Bradford, Ben Harris, Malcolm Smith, and CORE SDI S.A.\n\n"
  2154. "Permission is hereby granted, free of charge, to any person "
  2155. "obtaining a copy of this software and associated documentation "
  2156. "files (the ""Software""), to deal in the Software without restriction, "
  2157. "including without limitation the rights to use, copy, modify, merge, "
  2158. "publish, distribute, sublicense, and/or sell copies of the Software, "
  2159. "and to permit persons to whom the Software is furnished to do so, "
  2160. "subject to the following conditions:\n\n"
  2161. "The above copyright notice and this permission notice shall be "
  2162. "included in all copies or substantial portions of the Software.\n\n"
  2163. "THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT "
  2164. "WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, "
  2165. "INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
  2166. "MERCHANTABILITY, FITNESS FOR A PARTICULAR "
  2167. "PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE "
  2168. "COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES "
  2169. "OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, "
  2170. "TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN "
  2171. "CONNECTION WITH THE SOFTWARE OR THE USE OR "
  2172. "OTHER DEALINGS IN THE SOFTWARE.";
  2173. title = dupcat(appname, " Licence", NULL);
  2174. assert(aboutbox != NULL);
  2175. messagebox(aboutbox, title, licence,
  2176. string_width("LONGISH LINE OF TEXT SO THE LICENCE"
  2177. " BOX ISN'T EXCESSIVELY TALL AND THIN"),
  2178. "OK", 'o', 1, 1, NULL);
  2179. sfree(title);
  2180. }
  2181. void about_box(void *window)
  2182. {
  2183. GtkWidget *w;
  2184. char *title;
  2185. if (aboutbox) {
  2186. gtk_widget_grab_focus(aboutbox);
  2187. return;
  2188. }
  2189. aboutbox = gtk_dialog_new();
  2190. gtk_container_set_border_width(GTK_CONTAINER(aboutbox), 10);
  2191. title = dupcat("About ", appname, NULL);
  2192. gtk_window_set_title(GTK_WINDOW(aboutbox), title);
  2193. sfree(title);
  2194. w = gtk_button_new_with_label("Close");
  2195. GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
  2196. gtk_window_set_default(GTK_WINDOW(aboutbox), w);
  2197. gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox)->action_area),
  2198. w, FALSE, FALSE, 0);
  2199. gtk_signal_connect(GTK_OBJECT(w), "clicked",
  2200. GTK_SIGNAL_FUNC(about_close_clicked), NULL);
  2201. gtk_widget_show(w);
  2202. w = gtk_button_new_with_label("View Licence");
  2203. GTK_WIDGET_SET_FLAGS(w, GTK_CAN_DEFAULT);
  2204. gtk_box_pack_end(GTK_BOX(GTK_DIALOG(aboutbox)->action_area),
  2205. w, FALSE, FALSE, 0);
  2206. gtk_signal_connect(GTK_OBJECT(w), "clicked",
  2207. GTK_SIGNAL_FUNC(licence_clicked), NULL);
  2208. gtk_widget_show(w);
  2209. w = gtk_label_new(appname);
  2210. gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox)->vbox),
  2211. w, FALSE, FALSE, 0);
  2212. gtk_widget_show(w);
  2213. w = gtk_label_new(ver);
  2214. gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox)->vbox),
  2215. w, FALSE, FALSE, 5);
  2216. gtk_widget_show(w);
  2217. w = gtk_label_new("Copyright 1997-2004 Simon Tatham. All rights reserved");
  2218. gtk_box_pack_start(GTK_BOX(GTK_DIALOG(aboutbox)->vbox),
  2219. w, FALSE, FALSE, 5);
  2220. gtk_widget_show(w);
  2221. set_transient_window_pos(GTK_WIDGET(window), aboutbox);
  2222. gtk_widget_show(aboutbox);
  2223. }
  2224. struct eventlog_stuff {
  2225. GtkWidget *parentwin, *window;
  2226. struct controlbox *eventbox;
  2227. struct Shortcuts scs;
  2228. struct dlgparam dp;
  2229. union control *listctrl;
  2230. char **events;
  2231. int nevents, negsize;
  2232. char *seldata;
  2233. int sellen;
  2234. int ignore_selchange;
  2235. };
  2236. static void eventlog_destroy(GtkWidget *widget, gpointer data)
  2237. {
  2238. struct eventlog_stuff *es = (struct eventlog_stuff *)data;
  2239. es->window = NULL;
  2240. sfree(es->seldata);
  2241. dlg_cleanup(&es->dp);
  2242. ctrl_free_box(es->eventbox);
  2243. }
  2244. static void eventlog_ok_handler(union control *ctrl, void *dlg,
  2245. void *data, int event)
  2246. {
  2247. if (event == EVENT_ACTION)
  2248. dlg_end(dlg, 0);
  2249. }
  2250. static void eventlog_list_handler(union control *ctrl, void *dlg,
  2251. void *data, int event)
  2252. {
  2253. struct eventlog_stuff *es = (struct eventlog_stuff *)data;
  2254. if (event == EVENT_REFRESH) {
  2255. int i;
  2256. dlg_update_start(ctrl, dlg);
  2257. dlg_listbox_clear(ctrl, dlg);
  2258. for (i = 0; i < es->nevents; i++) {
  2259. dlg_listbox_add(ctrl, dlg, es->events[i]);
  2260. }
  2261. dlg_update_done(ctrl, dlg);
  2262. } else if (event == EVENT_SELCHANGE) {
  2263. int i;
  2264. int selsize = 0;
  2265. /*
  2266. * If this SELCHANGE event is happening as a result of
  2267. * deliberate deselection because someone else has grabbed
  2268. * the selection, the last thing we want to do is pre-empt
  2269. * them.
  2270. */
  2271. if (es->ignore_selchange)
  2272. return;
  2273. /*
  2274. * Construct the data to use as the selection.
  2275. */
  2276. sfree(es->seldata);
  2277. es->seldata = NULL;
  2278. es->sellen = 0;
  2279. for (i = 0; i < es->nevents; i++) {
  2280. if (dlg_listbox_issel(ctrl, dlg, i)) {
  2281. int extralen = strlen(es->events[i]);
  2282. if (es->sellen + extralen + 2 > selsize) {
  2283. selsize = es->sellen + extralen + 512;
  2284. es->seldata = sresize(es->seldata, selsize, char);
  2285. }
  2286. strcpy(es->seldata + es->sellen, es->events[i]);
  2287. es->sellen += extralen;
  2288. es->seldata[es->sellen++] = '\n';
  2289. }
  2290. }
  2291. if (gtk_selection_owner_set(es->window, GDK_SELECTION_PRIMARY,
  2292. GDK_CURRENT_TIME)) {
  2293. extern GdkAtom compound_text_atom;
  2294. gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY,
  2295. GDK_SELECTION_TYPE_STRING, 1);
  2296. gtk_selection_add_target(es->window, GDK_SELECTION_PRIMARY,
  2297. compound_text_atom, 1);
  2298. }
  2299. }
  2300. }
  2301. void eventlog_selection_get(GtkWidget *widget, GtkSelectionData *seldata,
  2302. guint info, guint time_stamp, gpointer data)
  2303. {
  2304. struct eventlog_stuff *es = (struct eventlog_stuff *)data;
  2305. gtk_selection_data_set(seldata, seldata->target, 8,
  2306. es->seldata, es->sellen);
  2307. }
  2308. gint eventlog_selection_clear(GtkWidget *widget, GdkEventSelection *seldata,
  2309. gpointer data)
  2310. {
  2311. struct eventlog_stuff *es = (struct eventlog_stuff *)data;
  2312. struct uctrl *uc;
  2313. /*
  2314. * Deselect everything in the list box.
  2315. */
  2316. uc = dlg_find_byctrl(&es->dp, es->listctrl);
  2317. es->ignore_selchange = 1;
  2318. gtk_list_unselect_all(GTK_LIST(uc->list));
  2319. es->ignore_selchange = 0;
  2320. sfree(es->seldata);
  2321. es->sellen = 0;
  2322. es->seldata = NULL;
  2323. return TRUE;
  2324. }
  2325. void showeventlog(void *estuff, void *parentwin)
  2326. {
  2327. struct eventlog_stuff *es = (struct eventlog_stuff *)estuff;
  2328. GtkWidget *window, *w0, *w1;
  2329. GtkWidget *parent = GTK_WIDGET(parentwin);
  2330. struct controlset *s0, *s1;
  2331. union control *c;
  2332. int listitemheight, index;
  2333. char *title;
  2334. if (es->window) {
  2335. gtk_widget_grab_focus(es->window);
  2336. return;
  2337. }
  2338. dlg_init(&es->dp);
  2339. for (index = 0; index < lenof(es->scs.sc); index++) {
  2340. es->scs.sc[index].action = SHORTCUT_EMPTY;
  2341. }
  2342. es->eventbox = ctrl_new_box();
  2343. s0 = ctrl_getset(es->eventbox, "", "", "");
  2344. ctrl_columns(s0, 3, 33, 34, 33);
  2345. c = ctrl_pushbutton(s0, "Close", 'c', HELPCTX(no_help),
  2346. eventlog_ok_handler, P(NULL));
  2347. c->button.column = 1;
  2348. c->button.isdefault = TRUE;
  2349. s1 = ctrl_getset(es->eventbox, "x", "", "");
  2350. es->listctrl = c = ctrl_listbox(s1, NULL, NO_SHORTCUT, HELPCTX(no_help),
  2351. eventlog_list_handler, P(es));
  2352. c->listbox.height = 10;
  2353. c->listbox.multisel = 2;
  2354. c->listbox.ncols = 3;
  2355. c->listbox.percentages = snewn(3, int);
  2356. c->listbox.percentages[0] = 25;
  2357. c->listbox.percentages[1] = 10;
  2358. c->listbox.percentages[2] = 65;
  2359. listitemheight = get_listitemheight();
  2360. es->window = window = gtk_dialog_new();
  2361. title = dupcat(appname, " Event Log", NULL);
  2362. gtk_window_set_title(GTK_WINDOW(window), title);
  2363. sfree(title);
  2364. w0 = layout_ctrls(&es->dp, &es->scs, s0,
  2365. listitemheight, GTK_WINDOW(window));
  2366. gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->action_area),
  2367. w0, TRUE, TRUE, 0);
  2368. gtk_widget_show(w0);
  2369. w1 = layout_ctrls(&es->dp, &es->scs, s1,
  2370. listitemheight, GTK_WINDOW(window));
  2371. gtk_container_set_border_width(GTK_CONTAINER(w1), 10);
  2372. gtk_widget_set_usize(w1, 20 +
  2373. string_width("LINE OF TEXT GIVING WIDTH OF EVENT LOG"
  2374. " IS QUITE LONG 'COS SSH LOG ENTRIES"
  2375. " ARE WIDE"), -1);
  2376. gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),
  2377. w1, TRUE, TRUE, 0);
  2378. gtk_widget_show(w1);
  2379. es->dp.data = es;
  2380. es->dp.shortcuts = &es->scs;
  2381. es->dp.lastfocus = NULL;
  2382. es->dp.retval = 0;
  2383. es->dp.window = window;
  2384. dlg_refresh(NULL, &es->dp);
  2385. if (parent) {
  2386. set_transient_window_pos(parent, window);
  2387. gtk_window_set_transient_for(GTK_WINDOW(window),
  2388. GTK_WINDOW(parent));
  2389. } else
  2390. gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  2391. gtk_widget_show(window);
  2392. gtk_signal_connect(GTK_OBJECT(window), "destroy",
  2393. GTK_SIGNAL_FUNC(eventlog_destroy), es);
  2394. gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
  2395. GTK_SIGNAL_FUNC(win_key_press), &es->dp);
  2396. gtk_signal_connect(GTK_OBJECT(window), "selection_get",
  2397. GTK_SIGNAL_FUNC(eventlog_selection_get), es);
  2398. gtk_signal_connect(GTK_OBJECT(window), "selection_clear_event",
  2399. GTK_SIGNAL_FUNC(eventlog_selection_clear), es);
  2400. }
  2401. void *eventlogstuff_new(void)
  2402. {
  2403. struct eventlog_stuff *es;
  2404. es = snew(struct eventlog_stuff);
  2405. memset(es, 0, sizeof(*es));
  2406. return es;
  2407. }
  2408. void logevent_dlg(void *estuff, const char *string)
  2409. {
  2410. struct eventlog_stuff *es = (struct eventlog_stuff *)estuff;
  2411. char timebuf[40];
  2412. time_t t;
  2413. if (es->nevents >= es->negsize) {
  2414. es->negsize += 64;
  2415. es->events = sresize(es->events, es->negsize, char *);
  2416. }
  2417. time(&t);
  2418. strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S\t",
  2419. localtime(&t));
  2420. es->events[es->nevents] = snewn(strlen(timebuf) + strlen(string) + 1, char);
  2421. strcpy(es->events[es->nevents], timebuf);
  2422. strcat(es->events[es->nevents], string);
  2423. if (es->window) {
  2424. dlg_listbox_add(es->listctrl, &es->dp, es->events[es->nevents]);
  2425. }
  2426. es->nevents++;
  2427. }
  2428. int askappend(void *frontend, Filename filename)
  2429. {
  2430. static const char msgtemplate[] =
  2431. "The session log file \"%.*s\" already exists. "
  2432. "You can overwrite it with a new session log, "
  2433. "append your session log to the end of it, "
  2434. "or disable session logging for this session.";
  2435. char *message;
  2436. char *mbtitle;
  2437. int mbret;
  2438. message = dupprintf(msgtemplate, FILENAME_MAX, filename.path);
  2439. mbtitle = dupprintf("%s Log to File", appname);
  2440. mbret = messagebox(get_window(frontend), mbtitle, message,
  2441. string_width("LINE OF TEXT SUITABLE FOR THE"
  2442. " ASKAPPEND WIDTH"),
  2443. "Overwrite", 'o', 1, 2,
  2444. "Append", 'a', 0, 1,
  2445. "Disable", 'd', -1, 0,
  2446. NULL);
  2447. sfree(message);
  2448. sfree(mbtitle);
  2449. return mbret;
  2450. }