gui_tool.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. /*
  2. * gui_tool.c - GUI, tool bar
  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 <stdlib.h>
  13. #include <math.h>
  14. #include <assert.h>
  15. #include <gtk/gtk.h>
  16. #include "util.h"
  17. #include "inst.h"
  18. #include "meas.h"
  19. #include "obj.h"
  20. #include "gui_util.h"
  21. #include "gui_style.h"
  22. #include "gui_inst.h"
  23. #include "gui_over.h"
  24. #include "gui_canvas.h"
  25. #include "gui_status.h"
  26. #include "gui.h"
  27. #include "gui_meas.h"
  28. #include "gui_tool.h"
  29. #include "icons/arc.xpm"
  30. #include "icons/circ.xpm"
  31. #include "icons/frame.xpm"
  32. #include "icons/line.xpm"
  33. #include "icons/meas.xpm"
  34. #include "icons/meas_x.xpm"
  35. #include "icons/meas_y.xpm"
  36. #include "icons/pad.xpm"
  37. #include "icons/rpad.xpm"
  38. #include "icons/hole.xpm"
  39. #include "icons/point.xpm"
  40. #include "icons/delete.xpm"
  41. #include "icons/delete_off.xpm"
  42. #include "icons/rect.xpm"
  43. #include "icons/vec.xpm"
  44. static GtkWidget *ev_point, *ev_delete;
  45. static GtkWidget *active_tool;
  46. static struct tool_ops *active_ops = NULL;
  47. static struct inst *hover_inst = NULL;
  48. static GtkWidget *delete_image[2];
  49. static struct drag_state {
  50. struct inst *inst; /* non-NULL if dragging an existing object */
  51. struct inst *new; /* non-NULL if dragging a new object */
  52. int anchors_n; /* number of anchors, 0 if no moving */
  53. int anchor_i; /* current anchor */
  54. struct vec **anchors[3];
  55. } drag = {
  56. .new = NULL,
  57. .anchors_n = 0,
  58. };
  59. static struct coord last_canvas_pos;
  60. static struct vec *new_vec(struct inst *base)
  61. {
  62. struct vec *vec, **walk;
  63. vec = alloc_type(struct vec);
  64. vec->nul_tag = 0;
  65. vec->name = NULL;
  66. vec->base = inst_get_vec(base);
  67. vec->next = NULL;
  68. vec->frame = active_frame;
  69. for (walk = &active_frame->vecs; *walk; walk = &(*walk)->next);
  70. *walk = vec;
  71. return vec;
  72. }
  73. struct obj *new_obj_unconnected(enum obj_type type, struct inst *base)
  74. {
  75. struct obj *obj;
  76. obj = alloc_type(struct obj);
  77. obj->type = type;
  78. obj->name = NULL;
  79. obj->frame = active_frame;
  80. obj->base = inst_get_vec(base);
  81. obj->next = NULL;
  82. obj->lineno = 0;
  83. return obj;
  84. }
  85. void connect_obj(struct frame *frame, struct obj *obj)
  86. {
  87. struct obj **walk;
  88. obj->frame = frame;
  89. for (walk = &frame->objs; *walk; walk = &(*walk)->next);
  90. *walk = obj;
  91. }
  92. static struct obj *new_obj(enum obj_type type, struct inst *base)
  93. {
  94. struct obj *obj;
  95. obj = new_obj_unconnected(type, base);
  96. connect_obj(active_frame, obj);
  97. return obj;
  98. }
  99. /* ----- shared functions -------------------------------------------------- */
  100. struct pix_buf *draw_move_line_common(struct inst *inst,
  101. struct coord end, struct coord pos, int i)
  102. {
  103. struct coord from, to;
  104. struct pix_buf *buf;
  105. from = translate(inst->base);
  106. to = translate(end);
  107. pos = translate(pos);
  108. switch (i) {
  109. case 0:
  110. from = pos;
  111. break;
  112. case 1:
  113. to = pos;
  114. break;
  115. default:
  116. abort();
  117. }
  118. buf = save_pix_buf(DA, from.x, from.y, to.x, to.y, 1);
  119. gdk_draw_line(DA, gc_drag, from.x, from.y, to.x, to.y);
  120. return buf;
  121. }
  122. static struct pix_buf *draw_move_rect_common(struct inst *inst,
  123. struct coord other, struct coord pos, int i)
  124. {
  125. struct coord min, max;
  126. struct pix_buf *buf;
  127. min = translate(inst->base);
  128. max = translate(other);
  129. pos = translate(pos);
  130. switch (i) {
  131. case 0:
  132. min = pos;
  133. break;
  134. case 1:
  135. max = pos;
  136. break;
  137. default:
  138. abort();
  139. }
  140. sort_coord(&min, &max);
  141. buf = save_pix_buf(DA, min.x, min.y, max.x, max.y, 1);
  142. gdk_draw_rectangle(DA, gc_drag, FALSE,
  143. min.x, min.y, max.x-min.x, max.y-min.y);
  144. return buf;
  145. }
  146. static struct pix_buf *hover_common(GdkGC *gc, struct coord center, unit_type r)
  147. {
  148. struct pix_buf *buf;
  149. center = translate(center);
  150. buf = save_pix_buf(DA,
  151. center.x-r, center.y-r, center.x+r, center.y+r, 2);
  152. draw_circle(DA, gc, FALSE, center.x, center.y, VEC_EYE_R);
  153. return buf;
  154. }
  155. /* ----- delete ------------------------------------------------------------ */
  156. static void tool_selected_delete(void)
  157. {
  158. if (selected_inst) {
  159. tool_dehover();
  160. inst_delete(selected_inst);
  161. change_world();
  162. }
  163. tool_reset();
  164. }
  165. static struct tool_ops delete_ops = {
  166. .tool_selected = tool_selected_delete,
  167. };
  168. void tool_selected_inst(struct inst *inst)
  169. {
  170. set_image(ev_delete, delete_image[inst != NULL]);
  171. }
  172. /* ----- vec --------------------------------------------------------------- */
  173. static struct coord gridify(struct coord base, struct coord pos)
  174. {
  175. struct coord new;
  176. unit_type unit;
  177. switch (curr_unit) {
  178. case curr_unit_mm:
  179. case curr_unit_auto:
  180. unit = mm_to_units(0.1);
  181. break;
  182. case curr_unit_mil:
  183. unit = mil_to_units(10);
  184. break;
  185. default:
  186. abort();
  187. }
  188. new.x = pos.x-((pos.x-base.x) % unit);
  189. new.y = pos.y-((pos.y-base.y) % unit);
  190. if (new.x != base.x || new.y != base.y)
  191. return new;
  192. if (fabs(pos.x-base.x) > fabs(pos.y-base.y))
  193. new.x += pos.x > base.x ? unit : -unit;
  194. else
  195. new.y += pos.y > base.y ? unit : -unit;
  196. return new;
  197. }
  198. static struct pix_buf *drag_new_vec(struct inst *from, struct coord to)
  199. {
  200. struct coord pos;
  201. struct pix_buf *buf;
  202. pos = inst_get_point(from);
  203. to = gridify(pos, to);
  204. status_set_type_x(NULL, "dX =");
  205. status_set_type_y(NULL, "dX =");
  206. /* @@@ use status_set_xy */
  207. switch (curr_unit) {
  208. case curr_unit_mm:
  209. case curr_unit_auto:
  210. status_set_x(NULL, "%lg mm", units_to_mm(to.x-pos.x));
  211. status_set_y(NULL, "%lg mm", units_to_mm(to.y-pos.y));
  212. break;
  213. case curr_unit_mil:
  214. status_set_x(NULL, "%lg mil", units_to_mil(to.x-pos.x));
  215. status_set_y(NULL, "%lg mil", units_to_mil(to.y-pos.y));
  216. break;
  217. default:
  218. abort();
  219. }
  220. pos = translate(pos);
  221. to = translate(to);
  222. buf = save_pix_buf(DA, pos.x, pos.y, to.x, to.y, 1);
  223. gdk_draw_line(DA, gc_drag, pos.x, pos.y, to.x, to.y);
  224. return buf;
  225. }
  226. struct pix_buf *draw_move_vec(struct inst *inst, struct coord pos, int i)
  227. {
  228. return draw_move_line_common(inst,
  229. add_vec(sub_vec(inst->u.vec.end, inst->base), pos), pos, i);
  230. }
  231. struct pix_buf *gui_hover_vec(struct inst *self)
  232. {
  233. return hover_common(gc_vec[mode_hover],
  234. self->u.vec.end, VEC_EYE_R);
  235. }
  236. static int end_new_raw_vec(struct inst *from, struct coord to)
  237. {
  238. struct vec *vec;
  239. struct coord pos;
  240. vec = new_vec(from);
  241. pos = inst_get_point(from);
  242. to = gridify(pos, to);
  243. switch (curr_unit) {
  244. case curr_unit_mm:
  245. case curr_unit_auto:
  246. vec->x = new_num(make_mm(units_to_mm(to.x-pos.x)));
  247. vec->y = new_num(make_mm(units_to_mm(to.y-pos.y)));
  248. break;
  249. case curr_unit_mil:
  250. vec->x = new_num(make_mil(units_to_mil(to.x-pos.x)));
  251. vec->y = new_num(make_mil(units_to_mil(to.y-pos.y)));
  252. break;
  253. default:
  254. abort();
  255. }
  256. return 1;
  257. }
  258. static struct tool_ops vec_ops = {
  259. .drag_new = drag_new_vec,
  260. .end_new_raw = end_new_raw_vec,
  261. };
  262. /* ----- line -------------------------------------------------------------- */
  263. struct pix_buf *drag_new_line(struct inst *from, struct coord to)
  264. {
  265. struct coord pos;
  266. struct pix_buf *buf;
  267. pos = translate(inst_get_point(from));
  268. to = translate(to);
  269. buf = save_pix_buf(DA, pos.x, pos.y, to.x, to.y, 1);
  270. gdk_draw_line(DA, gc_drag, pos.x, pos.y, to.x, to.y);
  271. return buf;
  272. }
  273. struct pix_buf *draw_move_line(struct inst *inst, struct coord pos, int i)
  274. {
  275. return draw_move_line_common(inst, inst->u.rect.end, pos, i);
  276. }
  277. static int end_new_line(struct inst *from, struct inst *to)
  278. {
  279. struct obj *obj;
  280. if (from == to)
  281. return 0;
  282. obj = new_obj(ot_line, from);
  283. obj->u.line.other = inst_get_vec(to);
  284. obj->u.line.width = NULL;
  285. return 1;
  286. }
  287. static struct tool_ops line_ops = {
  288. .drag_new = drag_new_line,
  289. .end_new = end_new_line,
  290. };
  291. /* ----- rect -------------------------------------------------------------- */
  292. static struct pix_buf *drag_new_rect(struct inst *from, struct coord to)
  293. {
  294. struct coord pos;
  295. struct pix_buf *buf;
  296. pos = translate(inst_get_point(from));
  297. to = translate(to);
  298. sort_coord(&pos, &to);
  299. buf = save_pix_buf(DA, pos.x, pos.y, to.x, to.y, 1);
  300. gdk_draw_rectangle(DA, gc_drag, FALSE,
  301. pos.x, pos.y, to.x-pos.x, to.y-pos.y);
  302. return buf;
  303. }
  304. struct pix_buf *draw_move_rect(struct inst *inst, struct coord pos, int i)
  305. {
  306. return draw_move_rect_common(inst, inst->u.rect.end, pos, i);
  307. }
  308. static int end_new_rect(struct inst *from, struct inst *to)
  309. {
  310. struct obj *obj;
  311. if (from == to)
  312. return 0;
  313. obj = new_obj(ot_rect, from);
  314. obj->u.rect.other = inst_get_vec(to);
  315. obj->u.rect.width = NULL;
  316. return 1;
  317. }
  318. static struct tool_ops rect_ops = {
  319. .drag_new = drag_new_rect,
  320. .end_new = end_new_rect,
  321. };
  322. /* ----- pad --------------------------------------------------------------- */
  323. static int end_new_pad(struct inst *from, struct inst *to)
  324. {
  325. struct obj *obj;
  326. if (from == to)
  327. return 0;
  328. obj = new_obj(ot_pad, from);
  329. obj->u.pad.other = inst_get_vec(to);
  330. obj->u.pad.name = stralloc("?");
  331. obj->u.pad.rounded = 0;
  332. obj->u.pad.type = pt_normal;
  333. return 1;
  334. }
  335. struct pix_buf *draw_move_pad(struct inst *inst, struct coord pos, int i)
  336. {
  337. return draw_move_rect_common(inst, inst->u.pad.other, pos, i);
  338. }
  339. static struct tool_ops pad_ops = {
  340. .drag_new = drag_new_rect,
  341. .end_new = end_new_pad,
  342. };
  343. /* ----- rounded pad ------------------------------------------------------- */
  344. static int end_new_rpad(struct inst *from, struct inst *to)
  345. {
  346. struct obj *obj;
  347. if (from == to)
  348. return 0;
  349. obj = new_obj(ot_pad, from);
  350. obj->u.pad.other = inst_get_vec(to);
  351. obj->u.pad.name = stralloc("?");
  352. obj->u.pad.rounded = 1;
  353. obj->u.pad.type = pt_normal;
  354. return 1;
  355. }
  356. struct pix_buf *draw_move_rpad(struct inst *inst, struct coord pos, int i)
  357. {
  358. return draw_move_rect_common(inst, inst->u.pad.other, pos, i);
  359. }
  360. static struct tool_ops rpad_ops = {
  361. .drag_new = drag_new_rect,
  362. .end_new = end_new_rpad,
  363. };
  364. /* ----- hole -------------------------------------------------------------- */
  365. static int end_new_hole(struct inst *from, struct inst *to)
  366. {
  367. struct obj *obj;
  368. if (from == to)
  369. return 0;
  370. obj = new_obj(ot_hole, from);
  371. obj->u.hole.other = inst_get_vec(to);
  372. return 1;
  373. }
  374. struct pix_buf *draw_move_hole(struct inst *inst, struct coord pos, int i)
  375. {
  376. return draw_move_rect_common(inst, inst->u.hole.other, pos, i);
  377. }
  378. static struct tool_ops hole_ops = {
  379. .drag_new = drag_new_rect,
  380. .end_new = end_new_hole,
  381. };
  382. /* ----- circ -------------------------------------------------------------- */
  383. static struct pix_buf *drag_new_circ(struct inst *from, struct coord to)
  384. {
  385. struct coord pos;
  386. struct pix_buf *buf;
  387. double r;
  388. pos = translate(inst_get_point(from));
  389. to = translate(to);
  390. r = hypot(to.x-pos.x, to.y-pos.y);
  391. buf = save_pix_buf(DA, pos.x-r, pos.y-r, pos.x+r, pos.y+r, 1);
  392. draw_circle(DA, gc_drag, FALSE, pos.x, pos.y, r);
  393. return buf;
  394. }
  395. static int end_new_circ(struct inst *from, struct inst *to)
  396. {
  397. struct obj *obj;
  398. if (from == to)
  399. return 0;
  400. obj = new_obj(ot_arc, from);
  401. obj->u.arc.start = inst_get_vec(to);
  402. obj->u.arc.end = inst_get_vec(to);
  403. obj->u.arc.width = NULL;
  404. return 1;
  405. }
  406. struct pix_buf *draw_move_arc(struct inst *inst, struct coord pos, int i)
  407. {
  408. struct coord c, from, to, end;
  409. double r, r_save, a1, a2;
  410. struct pix_buf *buf;
  411. c = translate(inst->base);
  412. from =
  413. translate(rotate_r(inst->base, inst->u.arc.r, inst->u.arc.a1));
  414. to =
  415. translate(rotate_r(inst->base, inst->u.arc.r, inst->u.arc.a2));
  416. pos = translate(pos);
  417. switch (i) {
  418. case 0:
  419. c = pos;
  420. break;
  421. case 2:
  422. from = pos;
  423. break;
  424. case 1:
  425. to = pos;
  426. break;
  427. default:
  428. abort();
  429. }
  430. r = hypot(from.x-c.x, from.y-c.y);
  431. /*
  432. * the screen coordinate system is reversed with y growing downward,
  433. * so we have to negate the angles.
  434. */
  435. a1 = -theta(c, from);
  436. a2 = -theta(c, to);
  437. if (a2 < a1)
  438. a2 += 360.0;
  439. if (i != 2) {
  440. r_save = r;
  441. } else {
  442. r_save = hypot(to.x-c.x, to.y-c.y);
  443. if (r > r_save)
  444. r_save = r;
  445. }
  446. buf = save_pix_buf(DA,
  447. c.x-r_save, c.y-r_save, c.x+r_save, c.y+r_save, 1);
  448. draw_arc(DA, gc_drag, FALSE, c.x, c.y, r, a1, a2);
  449. if (i == 1) {
  450. end = rotate_r(c, r_save, -a2);
  451. gdk_draw_line(DA, gc_drag, c.x, c.y, end.x, end.y);
  452. }
  453. return buf;
  454. }
  455. void do_move_to_arc(struct inst *inst, struct inst *to, int i)
  456. {
  457. struct vec *vec = inst_get_vec(to);
  458. struct obj *obj = inst->obj;
  459. switch (i) {
  460. case 0:
  461. obj->base = vec;
  462. break;
  463. case 2:
  464. obj->u.arc.start = vec;
  465. break;
  466. case 1:
  467. obj->u.arc.end = vec;
  468. break;
  469. default:
  470. abort();
  471. }
  472. }
  473. static struct tool_ops circ_ops = {
  474. .drag_new = drag_new_circ,
  475. .end_new = end_new_circ,
  476. };
  477. /* ----- frame helper ------------------------------------------------------ */
  478. int is_parent_of(const struct frame *p, const struct frame *c)
  479. {
  480. const struct obj *obj;
  481. if (p == c)
  482. return 1;
  483. for (obj = p->objs; obj; obj = obj->next)
  484. if (obj->type == ot_frame)
  485. if (is_parent_of(obj->u.frame.ref, c))
  486. return 1;
  487. return 0;
  488. }
  489. /* ----- frame ------------------------------------------------------------- */
  490. static struct frame *locked_frame = NULL;
  491. struct pix_buf *draw_move_frame(struct inst *inst, struct coord pos, int i)
  492. {
  493. struct pix_buf *buf;
  494. int r = FRAME_EYE_R2;
  495. pos = translate(pos);
  496. buf = save_pix_buf(DA, pos.x-r, pos.y-r, pos.x+r, pos.y+r, 1);
  497. draw_arc(DA, gc_drag, FALSE, pos.x, pos.y, r, 0, 360);
  498. return buf;
  499. }
  500. struct pix_buf *gui_hover_frame(struct inst *self)
  501. {
  502. return hover_common(gc_frame[mode_hover],
  503. self->base, FRAME_EYE_R2);
  504. }
  505. static int end_new_frame(struct inst *from, struct inst *to)
  506. {
  507. struct obj *obj;
  508. if (!locked_frame || is_parent_of(locked_frame, active_frame))
  509. return 0;
  510. obj = new_obj(ot_frame, from);
  511. obj->u.frame.ref = locked_frame;
  512. obj->u.frame.lineno = 0;
  513. if (!locked_frame->active_ref)
  514. locked_frame->active_ref = obj;
  515. locked_frame = NULL;
  516. tool_reset();
  517. return 1;
  518. }
  519. static struct tool_ops frame_ops = {
  520. .tool_selected = NULL,
  521. .drag_new = NULL,
  522. .end_new = end_new_frame,
  523. };
  524. /* ----- moving references ------------------------------------------------- */
  525. #if 0
  526. static int may_move(struct inst *curr)
  527. {
  528. if (!selected_inst)
  529. return 0;
  530. if (drag.anchors_n)
  531. return 0; /* already moving something else */
  532. drag.anchors_n = inst_anchors(selected_inst, drag.anchors);
  533. for (drag.anchor_i = 0; drag.anchor_i != drag.anchors_n;
  534. drag.anchor_i++)
  535. if (*drag.anchors[drag.anchor_i] == inst_get_vec(curr))
  536. return 1;
  537. drag.anchors_n = 0;
  538. return 0;
  539. }
  540. #endif
  541. static int would_be_equal(const struct drag_state *state,
  542. int a, int b, struct inst *curr)
  543. {
  544. const struct vec *va;
  545. const struct vec *vb;
  546. va = a == state->anchor_i ? inst_get_vec(curr) : *state->anchors[a];
  547. vb = b == state->anchor_i ? inst_get_vec(curr) : *state->anchors[b];
  548. return va == vb;
  549. }
  550. static int may_move_to(const struct drag_state *state, struct inst *curr)
  551. {
  552. assert(drag.inst);
  553. assert(state->anchors_n);
  554. switch (state->anchors_n) {
  555. case 3:
  556. if (would_be_equal(state, 0, 2, curr))
  557. return 0;
  558. /* fall through */
  559. case 2:
  560. if (would_be_equal(state, 0, 1, curr))
  561. return 0;
  562. /* fall through */
  563. case 1:
  564. return 1;
  565. default:
  566. abort();
  567. }
  568. }
  569. static void do_move_to(struct drag_state *state, struct inst *curr)
  570. {
  571. assert(state->inst->ops->find_point || may_move_to(state, curr));
  572. *state->anchors[state->anchor_i] = inst_get_vec(curr);
  573. }
  574. /* ----- hover ------------------------------------------------------------- */
  575. static struct pix_buf *hover_save_and_draw(void *user)
  576. {
  577. return inst_hover(hover_inst);
  578. }
  579. void tool_dehover(void)
  580. {
  581. if (!hover_inst)
  582. return;
  583. over_leave();
  584. hover_inst = NULL;
  585. }
  586. /*
  587. * When hovering, we have to consider the following states:
  588. *
  589. * selected (selected_inst != NULL)
  590. * | dragging new (drag.new != NULL)
  591. * | | dragging existing (drag.anchors_n != 0)
  592. * | | | tool selected (active_ops)
  593. * | | | |
  594. * Y N N N highlight if inst_find_point_selected else don't
  595. * Y N N Y highlight if inst_find_point_selected else fall over to tool
  596. * - Y N - highlight if inst_find_point / active_ops->find_point else don't
  597. * - N Y - highlight if may_move_to else don't
  598. * - Y Y - invalid state
  599. * N N N Y highlight if inst_find_point / active_ops->find_point else don't
  600. * N N N N don't highlight
  601. */
  602. static struct inst *get_hover_inst(struct coord pos)
  603. {
  604. struct inst *inst;
  605. int i;
  606. if (drag.new) {
  607. if (active_ops->find_point)
  608. return active_ops->find_point(pos);
  609. return inst_find_point(pos);
  610. }
  611. if (drag.anchors_n) {
  612. if (drag.inst->ops->find_point)
  613. return drag.inst->ops->find_point(drag.inst, pos);
  614. inst = inst_find_point(pos);
  615. if (!inst)
  616. return NULL;
  617. return may_move_to(&drag, inst) ? inst : NULL;
  618. }
  619. if (selected_inst) {
  620. i = inst_find_point_selected(pos, &inst);
  621. if (i != -1)
  622. return inst;
  623. }
  624. if (!active_ops)
  625. return NULL;
  626. if (active_ops->find_point)
  627. return active_ops->find_point(pos);
  628. return inst_find_point(pos);
  629. }
  630. int tool_hover(struct coord pos)
  631. {
  632. struct inst *curr;
  633. curr = get_hover_inst(pos);
  634. #if 0
  635. if ((drag.new && curr == drag.new) || (drag.inst && curr == drag.inst))
  636. return;
  637. if (curr && !active_ops) {
  638. if (drag.anchors_n) {
  639. if (!may_move_to(&drag, curr))
  640. curr = NULL;
  641. } else {
  642. if (!may_move(curr))
  643. curr = NULL;
  644. drag.anchors_n = 0;
  645. }
  646. }
  647. got:
  648. #endif
  649. if (curr == hover_inst)
  650. return !!curr;
  651. if (hover_inst) {
  652. over_leave();
  653. hover_inst = NULL;
  654. }
  655. if (!curr)
  656. return 0;
  657. hover_inst = curr;
  658. over_enter(hover_save_and_draw, NULL);
  659. return 1;
  660. }
  661. /* ----- frame drag and drop ----------------------------------------------- */
  662. /*
  663. * When dragging a frame, we temporarily replace the selected tool (if any)
  664. * with the frame tool.
  665. */
  666. static struct tool_ops *pushed_ops;
  667. void tool_push_frame(struct frame *frame)
  668. {
  669. pushed_ops = active_ops;
  670. locked_frame = frame;
  671. active_ops = &frame_ops;
  672. /*
  673. * We don't need to call tool_selected since, with drag and drop, the
  674. * frame tools doesn't need activation anymore.
  675. */
  676. }
  677. static int do_place_frame(struct frame *frame, struct coord pos)
  678. {
  679. if (!get_hover_inst(pos))
  680. return 0;
  681. tool_consider_drag(pos);
  682. return 1;
  683. }
  684. /*
  685. * Gtk calls drag-leave, drag-end, and only then drag-drop. So we'll already
  686. * have cleaned up in tool_pop_frame before we get here. In order to place the
  687. * frame, we need to activate the frame tool again.
  688. *
  689. * @@@ bug: there's a tool_reset in this path, so we'll lose the widget of the
  690. * tool that's really active. This problem will vanish when scrapping the
  691. * old-style frame referenes.
  692. */
  693. int tool_place_frame(struct frame *frame, struct coord pos)
  694. {
  695. int ok;
  696. active_ops = &frame_ops;
  697. ok = do_place_frame(frame, pos);
  698. active_ops = pushed_ops;
  699. return ok;
  700. }
  701. void tool_pop_frame(void)
  702. {
  703. if (!active_tool)
  704. return;
  705. active_ops = pushed_ops;
  706. /*
  707. * We don't need to call tool_selected since the only tool that could
  708. * use this would be the delete tool, and there the semantics would be
  709. * undesirable. Also, the delete tool never stays active, so it can't
  710. * appear together with drag and drop anyway.
  711. */
  712. }
  713. /* ----- tooltip ----------------------------------------------------------- */
  714. const char *tool_tip(struct coord pos)
  715. {
  716. struct inst *inst;
  717. inst = get_hover_inst(pos);
  718. if (!inst)
  719. return NULL;
  720. /*
  721. * The logic below follows exactly what happens in get_hover_inst.
  722. */
  723. if (drag.new)
  724. return "End here";
  725. if (drag.anchors_n)
  726. return "Move here";
  727. if (selected_inst)
  728. return "Move this point";
  729. return "Start here";
  730. }
  731. /* ----- mouse actions ----------------------------------------------------- */
  732. static struct pix_buf *drag_save_and_draw(void *user, struct coord to)
  733. {
  734. if (drag.new) {
  735. assert(active_ops);
  736. return active_ops->drag_new(drag.new, to);
  737. } else {
  738. return inst_draw_move(drag.inst, to, drag.anchor_i);
  739. }
  740. }
  741. /*
  742. * When considering dragging, we have the following states:
  743. *
  744. * selected (selected_inst != NULL)
  745. * | tool selected (active_ops)
  746. * | |
  747. * N N don't
  748. * Y - if we could drag, drag_new/end_new, else fall over to tool
  749. * N Y else single-click creation, else drag_new/end_new
  750. */
  751. int tool_consider_drag(struct coord pos)
  752. {
  753. struct inst *curr;
  754. assert(!drag.new);
  755. assert(!drag.anchors_n);
  756. last_canvas_pos = translate(pos);
  757. if (!selected_inst && !active_ops)
  758. return 0;
  759. if (selected_inst) {
  760. drag.anchor_i = inst_find_point_selected(pos, NULL);
  761. if (drag.anchor_i != -1) {
  762. tool_dehover();
  763. drag.inst = selected_inst;
  764. drag.new = NULL;
  765. drag.anchors_n =
  766. inst_anchors(selected_inst, drag.anchors);
  767. over_begin(drag_save_and_draw, NULL, pos);
  768. inst_begin_drag_move(selected_inst, drag.anchor_i);
  769. return 1;
  770. }
  771. }
  772. if (!active_ops)
  773. return 0;
  774. curr = get_hover_inst(pos);
  775. if (!curr)
  776. return 0;
  777. tool_dehover();
  778. if (active_ops->drag_new) {
  779. if (active_ops->begin_drag_new)
  780. active_ops->begin_drag_new(curr);
  781. drag.inst = NULL;
  782. drag.new = curr;
  783. over_begin(drag_save_and_draw, NULL, pos);
  784. return 1;
  785. }
  786. /* object is created without dragging */
  787. if (active_ops->end_new(curr, NULL))
  788. return -1;
  789. return 0;
  790. }
  791. void tool_drag(struct coord to)
  792. {
  793. last_canvas_pos = translate(to);
  794. over_move(to);
  795. }
  796. void tool_cancel_drag(void)
  797. {
  798. if (drag.anchors_n && drag.inst->ops->end_drag_move)
  799. drag.inst->ops->end_drag_move();
  800. drag.new = NULL;
  801. active_ops = NULL;
  802. drag.anchors_n = 0;
  803. over_end();
  804. tool_dehover();
  805. tool_reset();
  806. }
  807. int tool_end_drag(struct coord to)
  808. {
  809. struct drag_state state = drag;
  810. struct inst *end;
  811. struct tool_ops *ops = active_ops;
  812. tool_cancel_drag();
  813. if (state.new && ops->end_new_raw)
  814. return ops->end_new_raw(state.new, to);
  815. if (state.new && ops->find_point) {
  816. end = ops->find_point(to);
  817. } else {
  818. if (state.inst && state.inst->ops->find_point)
  819. end = state.inst->ops->find_point(state.inst, to);
  820. else
  821. end = inst_find_point(to);
  822. }
  823. if (!end) {
  824. if (state.new && ops->cancel_drag_new)
  825. ops->cancel_drag_new();
  826. return 0;
  827. }
  828. if (state.new)
  829. return ops->end_new(state.new, end);
  830. /* if we got the point from find_point, it's authoritative */
  831. if (!state.inst->ops->find_point && !may_move_to(&state, end))
  832. return 0;
  833. if (!inst_do_move_to(state.inst, end, state.anchor_i))
  834. do_move_to(&state, end);
  835. return 1;
  836. }
  837. void tool_redraw(void)
  838. {
  839. struct coord pos;
  840. over_reset();
  841. hover_inst = NULL;
  842. if (!drag.new && !drag.anchors_n)
  843. return;
  844. pos = canvas_to_coord(last_canvas_pos.x, last_canvas_pos.y);
  845. tool_hover(pos);
  846. over_begin(drag_save_and_draw, NULL, pos);
  847. }
  848. /* ----- Retrieve icons by instance characteristics ------------------------ */
  849. GtkWidget *get_icon_by_inst(const struct inst *inst)
  850. {
  851. char **image;
  852. switch (inst->prio) {
  853. case ip_frame:
  854. image = xpm_frame;
  855. break;
  856. case ip_pad_copper:
  857. case ip_pad_special:
  858. image = inst->obj->u.pad.rounded ? xpm_rpad : xpm_pad;
  859. break;
  860. case ip_hole:
  861. image = xpm_hole;
  862. break;
  863. case ip_circ:
  864. image = xpm_circ;
  865. break;
  866. case ip_arc:
  867. image = xpm_arc;
  868. break;
  869. case ip_rect:
  870. image = xpm_rect;
  871. break;
  872. case ip_meas:
  873. switch (inst->obj->u.meas.type) {
  874. case mt_xy_next:
  875. case mt_xy_max:
  876. image = xpm_meas;
  877. break;
  878. case mt_x_next:
  879. case mt_x_max:
  880. image = xpm_meas_x;
  881. break;
  882. case mt_y_next:
  883. case mt_y_max:
  884. image = xpm_meas_y;
  885. break;
  886. default:
  887. abort();
  888. }
  889. break;
  890. case ip_line:
  891. image = xpm_line;
  892. break;
  893. case ip_vec:
  894. image = xpm_vec;
  895. break;
  896. default:
  897. abort();
  898. }
  899. return make_transparent_image(DA, image, NULL);
  900. }
  901. /* ----- tool bar creation ------------------------------------------------- */
  902. static void tool_select(GtkWidget *evbox, struct tool_ops *ops)
  903. {
  904. GdkColor col;
  905. if (active_tool) {
  906. if (active_ops && active_ops->tool_deselected)
  907. active_ops->tool_deselected();
  908. col = get_color(COLOR_TOOL_UNSELECTED);
  909. gtk_widget_modify_bg(active_tool, GTK_STATE_NORMAL, &col);
  910. active_tool = NULL;
  911. }
  912. col = get_color(COLOR_TOOL_SELECTED);
  913. gtk_widget_modify_bg(evbox, GTK_STATE_NORMAL, &col);
  914. active_tool = evbox;
  915. active_ops = ops;
  916. if (ops && ops->tool_selected)
  917. ops->tool_selected();
  918. }
  919. void tool_reset(void)
  920. {
  921. over_reset();
  922. hover_inst = NULL;
  923. tool_select(ev_point, NULL);
  924. }
  925. static gboolean tool_button_press_event(GtkWidget *widget,
  926. GdkEventButton *event, gpointer data)
  927. {
  928. switch (event->button) {
  929. case 1:
  930. tool_select(widget, data);
  931. break;
  932. }
  933. return TRUE;
  934. }
  935. static void tool_separator(GtkWidget *bar)
  936. {
  937. GtkToolItem *item;
  938. item = gtk_separator_tool_item_new();
  939. gtk_separator_tool_item_set_draw(GTK_SEPARATOR_TOOL_ITEM(item), FALSE);
  940. gtk_toolbar_insert(GTK_TOOLBAR(bar), item, -1);
  941. }
  942. GtkWidget *gui_setup_tools(GdkDrawable *drawable)
  943. {
  944. GtkWidget *bar;
  945. bar = gtk_toolbar_new();
  946. gtk_toolbar_set_style(GTK_TOOLBAR(bar), GTK_TOOLBAR_ICONS);
  947. gtk_toolbar_set_orientation(GTK_TOOLBAR(bar),
  948. GTK_ORIENTATION_VERTICAL);
  949. ev_point = tool_button(bar, drawable, xpm_point,
  950. "Select and move items",
  951. tool_button_press_event, NULL);
  952. ev_delete = tool_button(bar, drawable, NULL, NULL,
  953. tool_button_press_event, &delete_ops);
  954. tool_separator(bar);
  955. tool_button(bar, drawable, xpm_vec,
  956. "Add a vector",
  957. tool_button_press_event, &vec_ops);
  958. tool_button(bar, drawable, xpm_pad,
  959. "Add a rectangular pad",
  960. tool_button_press_event, &pad_ops);
  961. tool_button(bar, drawable, xpm_rpad,
  962. "Add a rounded pad",
  963. tool_button_press_event, &rpad_ops);
  964. tool_button(bar, drawable, xpm_hole,
  965. "Add a hole",
  966. tool_button_press_event, &hole_ops);
  967. tool_button(bar, drawable, xpm_line,
  968. "Add a silk screen line",
  969. tool_button_press_event, &line_ops);
  970. tool_button(bar, drawable, xpm_rect,
  971. "Add a silk screen rectangle",
  972. tool_button_press_event, &rect_ops);
  973. tool_button(bar, drawable, xpm_circ,
  974. "Add a silk screen circle or arc",
  975. tool_button_press_event, &circ_ops);
  976. tool_separator(bar);
  977. tool_button(bar, drawable, xpm_meas,
  978. "Add a measurement",
  979. tool_button_press_event, &tool_meas_ops);
  980. tool_button(bar, drawable, xpm_meas_x,
  981. "Add a horizontal measurement",
  982. tool_button_press_event, &tool_meas_ops_x);
  983. tool_button(bar, drawable, xpm_meas_y,
  984. "Add a vertical measurement",
  985. tool_button_press_event, &tool_meas_ops_y);
  986. delete_image[0] = gtk_widget_ref(make_image(drawable, xpm_delete_off,
  987. NULL));
  988. delete_image[1] = gtk_widget_ref(make_image(drawable, xpm_delete,
  989. "Delete the selected item"));
  990. set_image(ev_delete, delete_image[0]);
  991. tool_reset();
  992. return bar;
  993. }
  994. void gui_cleanup_tools(void)
  995. {
  996. g_object_unref(delete_image[0]);
  997. g_object_unref(delete_image[1]);
  998. }