gui_status.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. /*
  2. * gui_status.c - GUI, status area
  3. *
  4. * Written 2009-2012 by Werner Almesberger
  5. * Copyright 2009-2012 by Werner Almesberger
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <stdarg.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <assert.h>
  17. #include <gtk/gtk.h>
  18. #include <gdk/gdkkeysyms.h>
  19. #include "util.h"
  20. #include "coord.h"
  21. #include "error.h"
  22. #include "unparse.h"
  23. #include "obj.h"
  24. #include "layer.h"
  25. #include "gui_util.h"
  26. #include "gui_style.h"
  27. #include "gui_canvas.h"
  28. #include "gui_frame.h"
  29. #include "gui.h"
  30. #include "gui_status.h"
  31. enum edit_status {
  32. es_unchanged,
  33. es_good,
  34. es_bad,
  35. };
  36. struct edit_ops {
  37. char *(*retrieve)(void *ctx);
  38. enum edit_status (*status)(const char *s, void *ctx);
  39. void (*store)(const char *s, void *ctx);
  40. };
  41. enum curr_unit curr_unit = curr_unit_mm;
  42. static GtkWidget *open_edits = NULL;
  43. static GtkWidget *last_edit = NULL;
  44. /* ----- setter functions -------------------------------------------------- */
  45. static GtkWidget *status_icon;
  46. static GtkWidget *status_name, *status_entry;
  47. static GtkWidget *status_type_x, *status_type_y, *status_type_entry;
  48. static GtkWidget *status_box_x, *status_entry_y;
  49. static GtkWidget *status_x, *status_y;
  50. static GtkWidget *status_r, *status_angle;
  51. static GtkWidget *status_sys_x, *status_sys_y;
  52. static GtkWidget *status_user_x, *status_user_y;
  53. static GtkWidget *status_zoom, *status_grid, *status_unit;
  54. static GtkWidget *status_msg;
  55. /* The x entry area serves multiple purposes */
  56. static GtkWidget *status_entry_x;
  57. static void set_label(GtkWidget *label, const char *tooltip,
  58. const char *fmt, va_list ap)
  59. {
  60. char *s;
  61. s = stralloc_vprintf(fmt, ap);
  62. gtk_label_set_text(GTK_LABEL(label), s);
  63. gtk_widget_set_tooltip_markup(label, tooltip);
  64. free(s);
  65. }
  66. #define SETTER(name) \
  67. void status_set_##name(const char *tooltip, const char *fmt, ...)\
  68. { \
  69. va_list ap; \
  70. \
  71. va_start(ap, fmt); \
  72. set_label(status_##name, tooltip, fmt, ap); \
  73. va_end(ap); \
  74. }
  75. SETTER(type_x)
  76. SETTER(type_y)
  77. SETTER(type_entry)
  78. SETTER(name)
  79. SETTER(x)
  80. SETTER(y)
  81. SETTER(r)
  82. SETTER(angle)
  83. SETTER(sys_x)
  84. SETTER(sys_y)
  85. SETTER(user_x)
  86. SETTER(user_y)
  87. SETTER(zoom)
  88. SETTER(grid)
  89. SETTER(unit)
  90. /* ----- set things with units --------------------------------------------- */
  91. void set_with_units(void (*set)(const char *tooltip, const char *fmt, ...),
  92. const char *prefix, unit_type u, const char *tooltip)
  93. {
  94. double n;
  95. int mm;
  96. switch (curr_unit) {
  97. case curr_unit_mm:
  98. n = units_to_mm(u);
  99. mm = 1;
  100. break;
  101. case curr_unit_mil:
  102. n = units_to_mil(u);
  103. mm = 0;
  104. break;
  105. case curr_unit_auto:
  106. n = units_to_best(u, &mm);
  107. break;
  108. default:
  109. abort();
  110. }
  111. if (mm) {
  112. /* -NNN.NNN mm */
  113. set(tooltip, "%s" MM_FORMAT_FIXED " mm", prefix, n);
  114. } else {
  115. /* -NNNN.N mil */
  116. set(tooltip, "%s" MIL_FORMAT_FIXED " mil", prefix, n);
  117. }
  118. }
  119. /* ----- complex status updates -------------------------------------------- */
  120. void status_set_icon(GtkWidget *image)
  121. {
  122. vacate_widget(status_icon);
  123. if (image)
  124. gtk_container_add(GTK_CONTAINER(status_icon), image);
  125. gtk_widget_show_all(status_icon);
  126. }
  127. void status_set_xy(struct coord coord)
  128. {
  129. /* do dX/dY etc. stuff later */
  130. status_set_type_x(NULL, "X =");
  131. status_set_type_y(NULL, "Y =");
  132. set_with_units(status_set_x, "", coord.x, "Width");
  133. set_with_units(status_set_y, "", coord.y, "Height");
  134. }
  135. void status_set_angle_xy(const char *tooltip, struct coord v)
  136. {
  137. if (!v.x && !v.y)
  138. status_set_angle(tooltip, "a = 0 deg");
  139. else
  140. status_set_angle(tooltip, "a = %3.1f deg", theta_vec(v));
  141. }
  142. static void entry_color(GtkWidget *widget, const char *color)
  143. {
  144. GdkColor col;
  145. col = get_color(color);
  146. gtk_widget_modify_base(widget, GTK_STATE_NORMAL, &col);
  147. }
  148. /* ----- pad type display and change --------------------------------------- */
  149. static enum pad_type *curr_pad_type;
  150. static GtkWidget *pad_type;
  151. static void show_pad_type(void)
  152. {
  153. gtk_label_set_text(GTK_LABEL(pad_type), pad_type_name(*curr_pad_type));
  154. }
  155. static gboolean pad_type_button_press_event(GtkWidget *widget,
  156. GdkEventButton *event, gpointer data)
  157. {
  158. switch (event->button) {
  159. case 1:
  160. *curr_pad_type = (*curr_pad_type+1) % pt_n;
  161. show_pad_type();
  162. break;
  163. }
  164. /*
  165. * We can't just redraw() here, because changing the pad type may also
  166. * affect the visual stacking. So we change the world and hope we end
  167. * up selecting the same pad afterwards.
  168. */
  169. change_world_reselect();
  170. return TRUE;
  171. }
  172. void edit_pad_type(enum pad_type *type)
  173. {
  174. vacate_widget(status_box_x);
  175. curr_pad_type = type;
  176. pad_type = label_in_box_new(NULL, "Pad type. Click to cycle.");
  177. gtk_container_add(GTK_CONTAINER(status_box_x), box_of_label(pad_type));
  178. label_in_box_bg(pad_type, COLOR_SELECTOR);
  179. g_signal_connect(G_OBJECT(box_of_label(pad_type)),
  180. "button_press_event", G_CALLBACK(pad_type_button_press_event),
  181. NULL);
  182. show_pad_type();
  183. gtk_widget_show_all(status_box_x);
  184. }
  185. /* ----- edit helper functions --------------------------------------------- */
  186. static void reset_edit(GtkWidget *widget)
  187. {
  188. struct edit_ops *ops;
  189. void *ctx;
  190. char *s;
  191. ops = gtk_object_get_data(GTK_OBJECT(widget), "edit-ops");
  192. ctx = gtk_object_get_data(GTK_OBJECT(widget), "edit-ctx");
  193. assert(ops);
  194. s = ops->retrieve(ctx);
  195. gtk_object_set_data(GTK_OBJECT(widget), "edit-ops", NULL);
  196. gtk_entry_set_text(GTK_ENTRY(widget), s);
  197. free(s);
  198. entry_color(widget, COLOR_EDIT_ASIS);
  199. gtk_object_set_data(GTK_OBJECT(widget), "edit-ops", ops);
  200. }
  201. static void reset_edits(void)
  202. {
  203. GtkWidget *edit;
  204. for (edit = open_edits; edit;
  205. edit = gtk_object_get_data(GTK_OBJECT(edit), "edit-next"))
  206. reset_edit(edit);
  207. gtk_widget_grab_focus(GTK_WIDGET(open_edits));
  208. }
  209. static gboolean edit_key_press_event(GtkWidget *widget, GdkEventKey *event,
  210. gpointer data)
  211. {
  212. GtkWidget *next = gtk_object_get_data(GTK_OBJECT(widget), "edit-next");
  213. switch (event->keyval) {
  214. case GDK_Tab:
  215. gtk_widget_grab_focus(GTK_WIDGET(next ? next : open_edits));
  216. return TRUE;
  217. case GDK_Escape:
  218. reset_edits();
  219. return TRUE;
  220. default:
  221. return FALSE;
  222. }
  223. }
  224. static void setup_edit(GtkWidget *widget, struct edit_ops *ops, void *ctx,
  225. const char *tooltip)
  226. {
  227. gtk_object_set_data(GTK_OBJECT(widget), "edit-ops", ops);
  228. gtk_object_set_data(GTK_OBJECT(widget), "edit-ctx", ctx);
  229. gtk_object_set_data(GTK_OBJECT(widget), "edit-next", NULL);
  230. reset_edit(widget);
  231. if (!open_edits)
  232. gtk_widget_grab_focus(GTK_WIDGET(widget));
  233. gtk_widget_show(widget);
  234. g_signal_connect(G_OBJECT(widget), "key_press_event",
  235. G_CALLBACK(edit_key_press_event), open_edits);
  236. if (last_edit)
  237. gtk_object_set_data(GTK_OBJECT(last_edit), "edit-next", widget);
  238. else
  239. open_edits = widget;
  240. last_edit = widget;
  241. gtk_widget_set_tooltip_markup(widget, tooltip);
  242. }
  243. /* ----- identifier fields ------------------------------------------------- */
  244. struct edit_unique_ctx {
  245. const char **s;
  246. int (*validate)(const char *s, void *ctx);
  247. void *ctx;
  248. };
  249. /*
  250. * Handle NULL so that we can also use it for unique_null
  251. */
  252. static char *unique_retrieve(void *ctx)
  253. {
  254. struct edit_unique_ctx *unique_ctx = ctx;
  255. return stralloc(*unique_ctx->s ? *unique_ctx->s : "");
  256. }
  257. static enum edit_status unique_status(const char *s, void *ctx)
  258. {
  259. const struct edit_unique_ctx *unique_ctx = ctx;
  260. if (!strcmp(s, *unique_ctx->s))
  261. return es_unchanged;
  262. return !unique_ctx->validate ||
  263. unique_ctx->validate(s, unique_ctx->ctx) ? es_good : es_bad;
  264. }
  265. static void unique_store(const char *s, void *ctx)
  266. {
  267. const struct edit_unique_ctx *unique_ctx = ctx;
  268. *unique_ctx->s = unique(s);
  269. }
  270. static struct edit_ops edit_ops_unique = {
  271. .retrieve = unique_retrieve,
  272. .status = unique_status,
  273. .store = unique_store,
  274. };
  275. void edit_unique(const char **s, int (*validate)(const char *s, void *ctx),
  276. void *ctx, const char *tooltip)
  277. {
  278. static struct edit_unique_ctx unique_ctx;
  279. unique_ctx.s = s;
  280. unique_ctx.validate = validate;
  281. unique_ctx.ctx = ctx;
  282. setup_edit(status_entry, &edit_ops_unique, &unique_ctx, tooltip);
  283. }
  284. /* ----- identifier fields with NULL --------------------------------------- */
  285. static enum edit_status unique_null_status(const char *s, void *ctx)
  286. {
  287. const struct edit_unique_ctx *unique_ctx = ctx;
  288. if (!strcmp(s, *unique_ctx->s ? *unique_ctx->s : ""))
  289. return es_unchanged;
  290. if (!*s)
  291. return es_good;
  292. return !unique_ctx->validate ||
  293. unique_ctx->validate(s, unique_ctx->ctx) ? es_good : es_bad;
  294. }
  295. static void unique_null_store(const char *s, void *ctx)
  296. {
  297. const struct edit_unique_ctx *unique_ctx = ctx;
  298. if (!*s)
  299. *unique_ctx->s = NULL;
  300. else
  301. *unique_ctx->s = unique(s);
  302. }
  303. static struct edit_ops edit_ops_null_unique = {
  304. .retrieve = unique_retrieve,
  305. .status = unique_null_status,
  306. .store = unique_null_store,
  307. };
  308. void edit_unique_null(const char **s,
  309. int (*validate)(const char *s, void *ctx), void *ctx, const char *tooltip)
  310. {
  311. static struct edit_unique_ctx unique_ctx;
  312. unique_ctx.s = s;
  313. unique_ctx.validate = validate;
  314. unique_ctx.ctx = ctx;
  315. setup_edit(status_entry, &edit_ops_null_unique, &unique_ctx, tooltip);
  316. }
  317. /* ----- unique field (variable) optionally followed by values ------------- */
  318. struct edit_unique_with_values_ctx {
  319. const char **s;
  320. int (*validate)(const char *s, void *ctx);
  321. void *ctx;
  322. void (*set_values)(void *user, const struct value *values,
  323. int n_values);
  324. void *user;
  325. int max_values;
  326. };
  327. static int unique_with_values_check(const char *s,
  328. const struct edit_unique_with_values_ctx *unique_ctx)
  329. {
  330. const char *id;
  331. struct value *values;
  332. int n;
  333. status_begin_reporting();
  334. n = parse_var(s, &id, &values, unique_ctx->max_values);
  335. if (n < 0)
  336. return 0;
  337. free_values(values, 0);
  338. return unique_ctx->validate ?
  339. unique_ctx->validate(id, unique_ctx->ctx) : 0;
  340. }
  341. static enum edit_status unique_with_values_status(const char *s, void *ctx)
  342. {
  343. const struct edit_unique_with_values_ctx *unique_ctx = ctx;
  344. if (!strcmp(s, *unique_ctx->s))
  345. return es_unchanged;
  346. return unique_with_values_check(s, unique_ctx) ? es_good : es_bad;
  347. }
  348. static void unique_with_values_store(const char *s, void *ctx)
  349. {
  350. const struct edit_unique_with_values_ctx *unique_ctx = ctx;
  351. struct value *values;
  352. int n;
  353. status_begin_reporting();
  354. n = parse_var(s, unique_ctx->s, &values, unique_ctx->max_values);
  355. if (!n)
  356. return;
  357. assert(n >= 0);
  358. assert(unique_ctx->max_values == -1 || n <= unique_ctx->max_values);
  359. unique_ctx->set_values(unique_ctx->user, values, n);
  360. free_values(values, 1);
  361. }
  362. static struct edit_ops edit_ops_unique_with_values = {
  363. .retrieve = unique_retrieve,
  364. .status = unique_with_values_status,
  365. .store = unique_with_values_store,
  366. };
  367. void edit_unique_with_values(const char **s,
  368. int (*validate)(const char *s, void *ctx), void *ctx,
  369. void (*set_values)(void *user, const struct value *values, int n_values),
  370. void *user, int max_values,
  371. const char *tooltip)
  372. {
  373. static struct edit_unique_with_values_ctx unique_ctx;
  374. unique_ctx.s = s;
  375. unique_ctx.validate = validate;
  376. unique_ctx.ctx = ctx;
  377. unique_ctx.set_values = set_values;
  378. unique_ctx.user = user;
  379. unique_ctx.max_values = max_values;
  380. setup_edit(status_entry, &edit_ops_unique_with_values, &unique_ctx,
  381. tooltip);
  382. }
  383. /* ----- variable type display and change ---------------------------------- */
  384. static struct var *curr_var;
  385. static GtkWidget *var_type;
  386. static void show_var_type(void)
  387. {
  388. gtk_label_set_text(GTK_LABEL(var_type),
  389. curr_var->key ? "key" : "assign");
  390. }
  391. /*
  392. * This is a bit of a layering violation. Note that we can't just try to
  393. * activate the edit and see what happens, because unique_with_values_status
  394. * would unconditionally accept the previous value, even if it is no longer
  395. * acceptable after the type change.
  396. */
  397. static int attempt_var_type_change(struct var *var)
  398. {
  399. const struct edit_unique_with_values_ctx *ctx;
  400. const char *s;
  401. ctx = gtk_object_get_data(GTK_OBJECT(status_entry), "edit-ctx");
  402. s = gtk_entry_get_text(GTK_ENTRY(status_entry));
  403. var->key = !var->key;
  404. if (unique_with_values_check(s, ctx))
  405. return 1;
  406. var->key = !var->key;
  407. return 0;
  408. }
  409. static gboolean do_activate(void);
  410. static gboolean var_type_button_press_event(GtkWidget *widget,
  411. GdkEventButton *event, gpointer data)
  412. {
  413. switch (event->button) {
  414. case 1:
  415. if (!attempt_var_type_change(curr_var))
  416. return TRUE;
  417. /* commit edit and change_world() */
  418. do_activate();
  419. reselect_var(curr_var);
  420. return TRUE;
  421. }
  422. return TRUE;
  423. }
  424. void edit_var_type(struct var *var)
  425. {
  426. vacate_widget(status_box_x);
  427. curr_var = var;
  428. var_type = label_in_box_new(NULL, "Variable type. Click to cycle.");
  429. gtk_container_add(GTK_CONTAINER(status_box_x), box_of_label(var_type));
  430. label_in_box_bg(var_type, COLOR_SELECTOR);
  431. g_signal_connect(G_OBJECT(box_of_label(var_type)),
  432. "button_press_event", G_CALLBACK(var_type_button_press_event),
  433. NULL);
  434. show_var_type();
  435. gtk_widget_show_all(status_box_x);
  436. }
  437. /* ----- string fields ----------------------------------------------------- */
  438. struct edit_name_ctx {
  439. char **s;
  440. int (*validate)(const char *s, void *ctx);
  441. void *ctx;
  442. };
  443. static char *name_retrieve(void *ctx)
  444. {
  445. struct edit_name_ctx *name_ctx = ctx;
  446. return stralloc(*name_ctx->s ? *name_ctx->s : "");
  447. }
  448. static enum edit_status name_status(const char *s, void *ctx)
  449. {
  450. const struct edit_name_ctx *name_ctx = ctx;
  451. if (!strcmp(s, *name_ctx->s))
  452. return es_unchanged;
  453. return !name_ctx->validate || name_ctx->validate(s, name_ctx->ctx) ?
  454. es_good : es_bad;
  455. }
  456. static void name_store(const char *s, void *ctx)
  457. {
  458. const struct edit_name_ctx *name_ctx = ctx;
  459. free(*name_ctx->s);
  460. *name_ctx->s = stralloc(s);
  461. }
  462. static struct edit_ops edit_ops_name = {
  463. .retrieve = name_retrieve,
  464. .status = name_status,
  465. .store = name_store,
  466. };
  467. void edit_name(char **s, int (*validate)(const char *s, void *ctx), void *ctx,
  468. const char *tooltip)
  469. {
  470. static struct edit_name_ctx name_ctx;
  471. name_ctx.s = s;
  472. name_ctx.validate = validate;
  473. name_ctx.ctx = ctx;
  474. setup_edit(status_entry, &edit_ops_name, &name_ctx, tooltip);
  475. }
  476. /* ----- expression fields ------------------------------------------------- */
  477. static struct expr *try_parse_expr(const char *s)
  478. {
  479. status_begin_reporting();
  480. return parse_expr(s);
  481. }
  482. static char *expr_retrieve(void *ctx)
  483. {
  484. struct expr **expr = ctx;
  485. return unparse(*expr);
  486. }
  487. static enum edit_status expr_status(const char *s, void *ctx)
  488. {
  489. struct expr *expr;
  490. expr = try_parse_expr(s);
  491. if (!expr)
  492. return es_bad;
  493. free_expr(expr);
  494. return es_good;
  495. }
  496. static void expr_store(const char *s, void *ctx)
  497. {
  498. struct expr **anchor = ctx;
  499. struct expr *expr;
  500. expr = try_parse_expr(s);
  501. assert(expr);
  502. if (*anchor)
  503. free_expr(*anchor);
  504. *anchor = expr;
  505. }
  506. static struct edit_ops edit_ops_expr = {
  507. .retrieve = expr_retrieve,
  508. .status = expr_status,
  509. .store = expr_store,
  510. };
  511. void edit_expr(struct expr **expr, const char *tooltip)
  512. {
  513. setup_edit(status_entry, &edit_ops_expr, expr, tooltip);
  514. }
  515. /* ----- distance expressions ---------------------------------------------- */
  516. static void dist_expr_store(const char *s, void *ctx)
  517. {
  518. struct expr **anchor = ctx;
  519. struct expr *expr;
  520. expr_store(s, ctx);
  521. expr = *anchor;
  522. if (expr->op == op_num && !expr->u.num.exponent && !expr->u.num.n)
  523. expr->u.num.exponent = 1;
  524. }
  525. static struct edit_ops edit_ops_dist_expr = {
  526. .retrieve = expr_retrieve,
  527. .status = expr_status,
  528. .store = dist_expr_store,
  529. };
  530. static void edit_any_dist_expr(GtkWidget *widget, struct expr **expr,
  531. const char *tooltip)
  532. {
  533. setup_edit(widget, &edit_ops_dist_expr, expr, tooltip);
  534. }
  535. void edit_dist_expr(struct expr **expr, const char *tooltip)
  536. {
  537. edit_any_dist_expr(status_entry, expr, tooltip);
  538. }
  539. void edit_x(struct expr **expr, const char *tooltip)
  540. {
  541. vacate_widget(status_box_x);
  542. gtk_container_add(GTK_CONTAINER(status_box_x), status_entry_x);
  543. gtk_widget_show(status_box_x);
  544. edit_any_dist_expr(status_entry_x, expr, tooltip);
  545. }
  546. void edit_y(struct expr **expr, const char *tooltip)
  547. {
  548. edit_any_dist_expr(status_entry_y, expr, tooltip);
  549. }
  550. /* ----- expression list --------------------------------------------------- */
  551. struct edit_expr_list_ctx {
  552. struct expr *expr;
  553. void (*set_values)(void *user, const struct value *values,
  554. int n_values);
  555. void *user;
  556. };
  557. static char *expr_list_retrieve(void *ctx)
  558. {
  559. struct edit_expr_list_ctx *expr_list_ctx = ctx;
  560. return unparse(expr_list_ctx->expr);
  561. }
  562. static enum edit_status expr_list_status(const char *s, void *ctx)
  563. {
  564. struct value *values;
  565. int n;
  566. status_begin_reporting();
  567. n = parse_values(s, &values);
  568. if (n < 0)
  569. return es_bad;
  570. free_values(values, 0);
  571. return es_good;
  572. }
  573. static void expr_list_store(const char *s, void *ctx)
  574. {
  575. struct edit_expr_list_ctx *expr_list_ctx = ctx;
  576. struct value *values;
  577. int n;
  578. status_begin_reporting();
  579. n = parse_values(s, &values);
  580. assert(n >= 0);
  581. expr_list_ctx->set_values(expr_list_ctx->user, values, n);
  582. free_values(values, 1);
  583. }
  584. static struct edit_ops edit_ops_expr_list = {
  585. .retrieve = expr_list_retrieve,
  586. .status = expr_list_status,
  587. .store = expr_list_store,
  588. };
  589. void edit_expr_list(struct expr *expr,
  590. void (*set_values)(void *user, const struct value *values, int n_values),
  591. void *user, const char *tooltip)
  592. {
  593. static struct edit_expr_list_ctx expr_list_ctx;
  594. expr_list_ctx.expr = expr;
  595. expr_list_ctx.set_values = set_values;
  596. expr_list_ctx.user = user;
  597. setup_edit(status_entry, &edit_ops_expr_list, &expr_list_ctx, tooltip);
  598. }
  599. /* ----- text entry -------------------------------------------------------- */
  600. static enum edit_status get_status(GtkWidget *widget)
  601. {
  602. struct edit_ops *ops;
  603. void *ctx;
  604. const char *s;
  605. ops = gtk_object_get_data(GTK_OBJECT(widget), "edit-ops");
  606. if (!ops)
  607. return es_unchanged;
  608. ctx = gtk_object_get_data(GTK_OBJECT(widget), "edit-ctx");
  609. s = gtk_entry_get_text(GTK_ENTRY(widget));
  610. return ops->status(s, ctx);
  611. }
  612. static void set_edit(GtkWidget *widget)
  613. {
  614. struct edit_ops *ops;
  615. void *ctx;
  616. const char *s;
  617. ops = gtk_object_get_data(GTK_OBJECT(widget), "edit-ops");
  618. if (!ops)
  619. return;
  620. ctx = gtk_object_get_data(GTK_OBJECT(widget), "edit-ctx");
  621. s = gtk_entry_get_text(GTK_ENTRY(widget));
  622. if (ops->store)
  623. ops->store(s, ctx);
  624. }
  625. static gboolean changed(GtkWidget *widget, GdkEventMotion *event,
  626. gpointer data)
  627. {
  628. switch (get_status(widget)) {
  629. case es_unchanged:
  630. entry_color(widget, COLOR_EDIT_ASIS);
  631. break;
  632. case es_good:
  633. entry_color(widget, COLOR_EDIT_GOOD);
  634. break;
  635. case es_bad:
  636. entry_color(widget, COLOR_EDIT_BAD);
  637. break;
  638. default:
  639. abort();
  640. }
  641. return TRUE;
  642. }
  643. static gboolean do_activate(void)
  644. {
  645. GtkWidget *edit;
  646. enum edit_status status;
  647. int unchanged = 1;
  648. for (edit = open_edits; edit;
  649. edit = gtk_object_get_data(GTK_OBJECT(edit), "edit-next")) {
  650. status = get_status(edit);
  651. if (status == es_bad)
  652. return TRUE;
  653. if (status == es_good)
  654. unchanged = 0;
  655. }
  656. if (unchanged) {
  657. inst_deselect();
  658. return TRUE;
  659. }
  660. for (edit = open_edits; edit;
  661. edit = gtk_object_get_data(GTK_OBJECT(edit), "edit-next"))
  662. if (get_status(edit) == es_good) {
  663. entry_color(edit, COLOR_EDIT_ASIS);
  664. set_edit(edit);
  665. }
  666. inst_deselect();
  667. change_world();
  668. return TRUE;
  669. }
  670. static gboolean activate(GtkWidget *widget, GdkEventMotion *event,
  671. gpointer data)
  672. {
  673. return do_activate();
  674. }
  675. void edit_nothing(void)
  676. {
  677. gtk_widget_hide(status_entry);
  678. gtk_widget_hide(status_box_x);
  679. gtk_widget_hide(status_entry_y);
  680. open_edits = NULL;
  681. last_edit = NULL;
  682. }
  683. /* ----- status reports ---------------------------------------------------- */
  684. static gint context_id;
  685. static int have_msg = 0;
  686. static void clear_status_msg(void)
  687. {
  688. if (have_msg) {
  689. gtk_statusbar_pop(GTK_STATUSBAR(status_msg), context_id);
  690. have_msg = 0;
  691. }
  692. }
  693. static void report_to_gui(const char *s)
  694. {
  695. if (!have_msg)
  696. gtk_statusbar_push(GTK_STATUSBAR(status_msg), context_id, s);
  697. have_msg = 1;
  698. }
  699. void status_begin_reporting(void)
  700. {
  701. clear_status_msg();
  702. reporter = report_to_gui;
  703. }
  704. /* ----- unit selection ---------------------------------------------------- */
  705. static void show_curr_unit(void)
  706. {
  707. static const char *tip = "Display unit. Click to cycle.";
  708. switch (curr_unit) {
  709. case curr_unit_mm:
  710. status_set_unit(tip, "mm");
  711. break;
  712. case curr_unit_mil:
  713. status_set_unit(tip, "mil");
  714. break;
  715. case curr_unit_auto:
  716. status_set_unit(tip, "auto");
  717. break;
  718. default:
  719. abort();
  720. }
  721. }
  722. static gboolean unit_button_press_event(GtkWidget *widget,
  723. GdkEventButton *event, gpointer data)
  724. {
  725. switch (event->button) {
  726. case 1:
  727. curr_unit = (curr_unit+1) % curr_unit_n;
  728. show_curr_unit();
  729. break;
  730. }
  731. refresh_pos();
  732. change_world();
  733. return TRUE;
  734. }
  735. /* ----- setup ------------------------------------------------------------- */
  736. static GtkWidget *add_vbox(GtkWidget *tab, int col, int row)
  737. {
  738. GtkWidget *vbox;
  739. vbox = gtk_vbox_new(FALSE, 0);
  740. gtk_table_attach_defaults(GTK_TABLE(tab), vbox,
  741. col, col+1, row, row+1);
  742. return vbox;
  743. }
  744. static GtkWidget *add_label_basic(GtkWidget *tab, int col, int row)
  745. {
  746. GtkWidget *label;
  747. label = label_in_box_new(NULL, NULL);
  748. gtk_table_attach(GTK_TABLE(tab), box_of_label(label),
  749. col, col+1, row, row+1,
  750. GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 1);
  751. /* 0 , 1 - padding */
  752. return label;
  753. }
  754. static GtkWidget *add_label(GtkWidget *tab, int col, int row)
  755. {
  756. GtkWidget *label;
  757. label = add_label_basic(tab, col, row);
  758. gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
  759. gtk_label_set_selectable(GTK_LABEL(label), TRUE);
  760. return label;
  761. }
  762. static GtkWidget *make_entry(void)
  763. {
  764. GtkWidget *entry;
  765. entry = gtk_entry_new();
  766. gtk_entry_set_has_frame(GTK_ENTRY(entry), FALSE);
  767. g_signal_connect(G_OBJECT(entry), "changed",
  768. G_CALLBACK(changed), entry);
  769. g_signal_connect(G_OBJECT(entry), "activate",
  770. G_CALLBACK(activate), entry);
  771. return entry;
  772. }
  773. static GtkWidget *add_entry(GtkWidget *tab, int col, int row)
  774. {
  775. GtkWidget *entry;
  776. entry = make_entry();
  777. gtk_table_attach_defaults(GTK_TABLE(tab), entry,
  778. col, col+1, row, row+1);
  779. return entry;
  780. }
  781. void make_status_area(GtkWidget *vbox)
  782. {
  783. GtkWidget *tab, *sep;
  784. GtkWidget *hbox, *vbox2;
  785. hbox = gtk_hbox_new(FALSE, 0);
  786. gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
  787. vbox2 = gtk_vbox_new(FALSE, 0);
  788. gtk_box_pack_start(GTK_BOX(hbox), vbox2, FALSE, FALSE, 1);
  789. status_icon = gtk_event_box_new();
  790. gtk_box_pack_start(GTK_BOX(vbox2), status_icon, FALSE, FALSE, 0);
  791. tab = gtk_table_new(7, 3, FALSE);
  792. gtk_box_pack_start(GTK_BOX(hbox), tab, TRUE, TRUE, 0);
  793. /* types */
  794. status_type_x = add_label(tab, 0, 0);
  795. status_type_y = add_label(tab, 0, 1);
  796. status_type_entry = add_label(tab, 0, 2);
  797. /* x / y */
  798. status_x = add_label(tab, 1, 0);
  799. status_box_x = add_vbox(tab, 2, 0);
  800. status_y = add_label(tab, 1, 1);
  801. status_entry_y = add_entry(tab, 2, 1);
  802. status_entry_x = gtk_widget_ref(make_entry());
  803. /* name and input */
  804. status_name = add_label(tab, 1, 2);
  805. status_entry = add_entry(tab, 2, 2);
  806. /* separator */
  807. sep = gtk_vseparator_new();
  808. gtk_table_attach_defaults(GTK_TABLE(tab), sep, 3, 4, 0, 3);
  809. /* sys / user pos */
  810. status_sys_x = add_label(tab, 4, 0);
  811. status_sys_y = add_label(tab, 4, 1);
  812. status_user_x = add_label(tab, 5, 0);
  813. status_user_y = add_label(tab, 5, 1);
  814. /* r / angle */
  815. status_r = add_label(tab, 4, 2);
  816. status_angle = add_label(tab, 5, 2);
  817. /* zoom / grid / unit */
  818. status_zoom = add_label(tab, 6, 0);
  819. status_grid = add_label(tab, 6, 1);
  820. status_unit = add_label_basic(tab, 6, 2);
  821. /* unit selection */
  822. label_in_box_bg(status_unit, COLOR_SELECTOR);
  823. show_curr_unit();
  824. g_signal_connect(G_OBJECT(box_of_label(status_unit)),
  825. "button_press_event", G_CALLBACK(unit_button_press_event), NULL);
  826. /* message bar */
  827. status_msg = gtk_statusbar_new();
  828. gtk_box_pack_start(GTK_BOX(vbox), status_msg, FALSE, FALSE, 0);
  829. context_id = gtk_statusbar_get_context_id(GTK_STATUSBAR(status_msg),
  830. "messages");
  831. }
  832. void cleanup_status_area(void)
  833. {
  834. gtk_widget_unref(status_entry_x);
  835. }