gui_frame_drag.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*
  2. * gui_frame_drag.c - GUI, dragging of frame items
  3. *
  4. * Written 2010 by Werner Almesberger
  5. * Copyright 2010 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 <assert.h>
  13. #include <gtk/gtk.h>
  14. #include "util.h"
  15. #include "obj.h"
  16. #include "gui_util.h"
  17. #include "gui.h"
  18. #include "gui_canvas.h"
  19. #include "gui_frame_drag.h"
  20. #if 0
  21. #include "icons/frame.xpm"
  22. #endif
  23. enum {
  24. target_id_var,
  25. target_id_value,
  26. target_id_frame,
  27. target_id_canvas,
  28. };
  29. static GtkTargetEntry target_var = {
  30. .target = "var",
  31. .flags = GTK_TARGET_SAME_APP,
  32. .info = target_id_var,
  33. };
  34. static GtkTargetEntry target_value = {
  35. .target = "value",
  36. .flags = GTK_TARGET_SAME_APP,
  37. .info = target_id_value,
  38. };
  39. static GtkTargetEntry target_frame = {
  40. .target = "frame",
  41. .flags = GTK_TARGET_SAME_APP,
  42. .info = target_id_frame,
  43. };
  44. /* ----- dragging status --------------------------------------------------- */
  45. /*
  46. * Pointer to whatever it is we're dragging. NULL if not dragging.
  47. */
  48. static void *dragging;
  49. int is_dragging(void *this)
  50. {
  51. return this == dragging;
  52. }
  53. int is_dragging_anything(void)
  54. {
  55. return !!dragging;
  56. }
  57. /* ----- helper functions for indexed list --------------------------------- */
  58. #define NDX(first, item) \
  59. ({ typeof(first) NDX_walk; \
  60. int NDX_n = 0; \
  61. for (NDX_walk = (first); NDX_walk != (item); \
  62. NDX_walk = NDX_walk->next) \
  63. NDX_n++; \
  64. NDX_n; })
  65. #define NTH(first, n) \
  66. ({ typeof(first) *NTH_walk; \
  67. int NTH_n = (n); \
  68. for (NTH_walk = &(first); NTH_n; NTH_n--) \
  69. NTH_walk = &(*NTH_walk)->next; \
  70. NTH_walk; })
  71. #define FOR_UNORDERED(var, a, b) \
  72. for (var = (a) < (b) ? (a) : (b); var != ((a) < (b) ? (b) : (a)); \
  73. var++)
  74. /* ----- generic helper functions. maybe move to gui_util later ------------ */
  75. static void get_cell_coords(GtkWidget *widget, guint res[4])
  76. {
  77. GtkWidget *tab;
  78. tab = gtk_widget_get_ancestor(widget, GTK_TYPE_TABLE);
  79. gtk_container_child_get(GTK_CONTAINER(tab), widget,
  80. "left-attach", res,
  81. "right-attach", res+1,
  82. "top-attach", res+2,
  83. "bottom-attach", res+3, NULL);
  84. }
  85. static void swap_table_cells(GtkWidget *a, GtkWidget *b)
  86. {
  87. GtkWidget *tab_a, *tab_b;
  88. guint pos_a[4], pos_b[4];
  89. tab_a = gtk_widget_get_ancestor(a, GTK_TYPE_TABLE);
  90. tab_b = gtk_widget_get_ancestor(b, GTK_TYPE_TABLE);
  91. get_cell_coords(a, pos_a);
  92. get_cell_coords(b, pos_b);
  93. g_object_ref(a);
  94. g_object_ref(b);
  95. gtk_container_remove(GTK_CONTAINER(tab_a), a);
  96. gtk_container_remove(GTK_CONTAINER(tab_b), b);
  97. gtk_table_attach_defaults(GTK_TABLE(tab_a), b,
  98. pos_a[0], pos_a[1], pos_a[2], pos_a[3]);
  99. gtk_table_attach_defaults(GTK_TABLE(tab_b), a,
  100. pos_b[0], pos_b[1], pos_b[2], pos_b[3]);
  101. g_object_unref(a);
  102. g_object_unref(b);
  103. }
  104. static GtkWidget *pick_table_cell(GtkWidget *table, int x, int y)
  105. {
  106. GList *children, *walk;
  107. GtkWidget *child;
  108. guint pos[4];
  109. children = gtk_container_get_children(GTK_CONTAINER(table));
  110. for (walk = children; walk; walk = g_list_next(walk)) {
  111. child = g_list_nth_data(walk, 0);
  112. assert(child);
  113. get_cell_coords(child, pos);
  114. if (pos[0] == x && pos[2] == y)
  115. break;
  116. }
  117. g_list_free(children);
  118. return walk ? child : NULL;
  119. }
  120. static void swap_table_cells_by_coord(GtkWidget *table_a,
  121. int a_col, int a_row, GtkWidget *table_b, int b_col, int b_row)
  122. {
  123. GtkWidget *a, *b;
  124. a = pick_table_cell(table_a, a_col, a_row);
  125. b = pick_table_cell(table_b, b_col, b_row);
  126. if (a) {
  127. g_object_ref(a);
  128. gtk_container_remove(GTK_CONTAINER(table_a), a);
  129. }
  130. if (b) {
  131. g_object_ref(b);
  132. gtk_container_remove(GTK_CONTAINER(table_b), b);
  133. }
  134. if (a)
  135. gtk_table_attach_defaults(GTK_TABLE(table_b), a,
  136. b_col, b_col+1, b_row, b_row+1);
  137. if (b)
  138. gtk_table_attach_defaults(GTK_TABLE(table_a), b,
  139. a_col, a_col+1, a_row, a_row+1);
  140. if (a)
  141. g_object_unref(a);
  142. if (b)
  143. g_object_unref(b);
  144. }
  145. static void swap_table_rows(GtkWidget *table, int a, int b)
  146. {
  147. guint cols;
  148. int i;
  149. g_object_get(table, "n-columns", &cols, NULL);
  150. for (i = 0; i != cols; i++)
  151. swap_table_cells_by_coord(table, i, a, table, i, b);
  152. }
  153. /* ----- swap table items -------------------------------------------------- */
  154. static void swap_vars(struct table *table, int a, int b)
  155. {
  156. struct var **var_a, **var_b;
  157. var_a = NTH(table->vars, a);
  158. var_b = NTH(table->vars, b);
  159. swap_table_cells(box_of_label((*var_a)->widget),
  160. box_of_label((*var_b)->widget));
  161. SWAP(*var_a, *var_b);
  162. SWAP((*var_a)->next, (*var_b)->next);
  163. }
  164. static void swap_values(struct row *row, int a, int b)
  165. {
  166. struct value **value_a, **value_b;
  167. value_a = NTH(row->values, a);
  168. value_b = NTH(row->values, b);
  169. swap_table_cells(box_of_label((*value_a)->widget),
  170. box_of_label((*value_b)->widget));
  171. SWAP(*value_a, *value_b);
  172. SWAP((*value_a)->next, (*value_b)->next);
  173. }
  174. static void swap_cols(struct table *table, int a, int b)
  175. {
  176. struct row *row;
  177. swap_vars(table, a, b);
  178. for (row = table->rows; row; row = row->next)
  179. swap_values(row, a, b);
  180. }
  181. static void swap_rows(struct row **a, struct row **b)
  182. {
  183. struct value *value_a, *value_b;
  184. value_a = (*a)->values;
  185. value_b = (*b)->values;
  186. while (value_a) {
  187. swap_table_cells(box_of_label(value_a->widget),
  188. box_of_label(value_b->widget));
  189. value_a = value_a->next;
  190. value_b = value_b->next;
  191. }
  192. SWAP(*a, *b);
  193. SWAP((*a)->next, (*b)->next);
  194. }
  195. /* ----- swap frames ------------------------------------------------------- */
  196. static void swap_frames(GtkWidget *table, int a, int b)
  197. {
  198. struct frame **frame_a = NTH(frames, a);
  199. struct frame **frame_b = NTH(frames, b);
  200. swap_table_rows(table, 2*a+1, 2*b+1);
  201. swap_table_rows(table, 2*a+2, 2*b+2);
  202. SWAP(*frame_a, *frame_b);
  203. SWAP((*frame_a)->next, (*frame_b)->next);
  204. }
  205. /* ----- common functions -------------------------------------------------- */
  206. /*
  207. * according to
  208. * http://www.pubbs.net/201004/gtk/22819-re-drag-and-drop-drag-motion-cursor-lockup-fixed-.html
  209. * http://www.cryingwolf.org/articles/gtk-dnd.html
  210. */
  211. static int has_target(GtkWidget *widget, GdkDragContext *drag_context,
  212. const char *name)
  213. {
  214. GdkAtom target;
  215. target = gtk_drag_dest_find_target(widget, drag_context, NULL);
  216. /*
  217. * Force allocation so that we don't have to check for GDK_NONE.
  218. */
  219. return target == gdk_atom_intern(name, FALSE);
  220. }
  221. static void drag_begin(GtkWidget *widget,
  222. GtkTextDirection previous_direction, gpointer user_data)
  223. {
  224. GdkPixbuf *pixbuf;
  225. /*
  226. * Suppress the icon. PixBufs can't be zero-sized, but nobody will
  227. * notice a lone pixel either :-)
  228. */
  229. pixbuf =
  230. gdk_pixbuf_get_from_drawable(NULL, DA, NULL, 0, 0, 0, 0, 1, 1);
  231. gtk_drag_source_set_icon_pixbuf(widget, pixbuf);
  232. g_object_unref(pixbuf);
  233. dragging = user_data;
  234. }
  235. static void drag_end(GtkWidget *widget, GdkDragContext *drag_context,
  236. gpointer user_data)
  237. {
  238. dragging = NULL;
  239. }
  240. static void setup_drag_common(GtkWidget *widget, void *user_arg)
  241. {
  242. g_signal_connect(G_OBJECT(widget), "drag-begin",
  243. G_CALLBACK(drag_begin), user_arg);
  244. g_signal_connect(G_OBJECT(widget), "drag-end",
  245. G_CALLBACK(drag_end), user_arg);
  246. }
  247. /* ----- drag variables ---------------------------------------------------- */
  248. static gboolean drag_var_motion(GtkWidget *widget,
  249. GdkDragContext *drag_context, gint x, gint y, guint time_,
  250. gpointer user_data)
  251. {
  252. struct var *from = dragging;
  253. struct var *to = user_data;
  254. int from_n, to_n, i;
  255. if (!has_target(widget, drag_context, "var"))
  256. return FALSE;
  257. if (from == to || from->table != to->table)
  258. return FALSE;
  259. from_n = NDX(from->table->vars, from);
  260. to_n = NDX(to->table->vars, to);
  261. FOR_UNORDERED(i, from_n, to_n)
  262. swap_cols(from->table, i, i+1);
  263. return FALSE;
  264. }
  265. void setup_var_drag(struct var *var)
  266. {
  267. GtkWidget *box;
  268. box = box_of_label(var->widget);
  269. gtk_drag_source_set(box, GDK_BUTTON1_MASK,
  270. &target_var, 1, GDK_ACTION_PRIVATE);
  271. gtk_drag_dest_set(box, GTK_DEST_DEFAULT_MOTION,
  272. &target_var, 1, GDK_ACTION_PRIVATE);
  273. setup_drag_common(box, var);
  274. g_signal_connect(G_OBJECT(box), "drag-motion",
  275. G_CALLBACK(drag_var_motion), var);
  276. }
  277. /* ----- drag values ------------------------------------------------------- */
  278. static gboolean drag_value_motion(GtkWidget *widget,
  279. GdkDragContext *drag_context, gint x, gint y, guint time_,
  280. gpointer user_data)
  281. {
  282. struct value *from = dragging;
  283. struct value *to = user_data;
  284. struct table *table;
  285. struct row **row, *end;
  286. int from_n, to_n, i;
  287. if (!has_target(widget, drag_context, "value"))
  288. return FALSE;
  289. table = from->row->table;
  290. if (table != to->row->table)
  291. return FALSE;
  292. /* columns */
  293. from_n = NDX(from->row->values, from);
  294. to_n = NDX(to->row->values, to);
  295. FOR_UNORDERED(i, from_n, to_n)
  296. swap_cols(table, i, i+1);
  297. /* rows */
  298. if (from->row == to->row)
  299. return FALSE;
  300. row = &table->rows;
  301. while (1) {
  302. if (*row == from->row) {
  303. end = to->row;
  304. break;
  305. }
  306. if (*row == to->row) {
  307. end = from->row;
  308. break;
  309. }
  310. row = &(*row)->next;
  311. }
  312. while (1) {
  313. swap_rows(row, &(*row)->next);
  314. if (*row == end)
  315. break;
  316. row = &(*row)->next;
  317. }
  318. return FALSE;
  319. }
  320. void setup_value_drag(struct value *value)
  321. {
  322. GtkWidget *box;
  323. box = box_of_label(value->widget);
  324. gtk_drag_source_set(box, GDK_BUTTON1_MASK,
  325. &target_value, 1, GDK_ACTION_PRIVATE);
  326. gtk_drag_dest_set(box, GTK_DEST_DEFAULT_MOTION,
  327. &target_value, 1, GDK_ACTION_PRIVATE);
  328. setup_drag_common(box, value);
  329. g_signal_connect(G_OBJECT(box), "drag-motion",
  330. G_CALLBACK(drag_value_motion), value);
  331. }
  332. /* ----- frame to canvas helper functions ---------------------------------- */
  333. static int frame_on_canvas = 0;
  334. static void leave_canvas(void)
  335. {
  336. if (frame_on_canvas)
  337. canvas_frame_end();
  338. frame_on_canvas = 0;
  339. }
  340. /* ----- drag frame labels ------------------------------------------------- */
  341. #if 0
  342. /*
  343. * Setting our own icon looks nice but it slows things down to the point where
  344. * cursor movements can lag noticeable and it adds yet another element to an
  345. * already crowded cursor.
  346. */
  347. static void drag_frame_begin(GtkWidget *widget,
  348. GtkTextDirection previous_direction, gpointer user_data)
  349. {
  350. GdkPixmap *pixmap;
  351. GdkBitmap *mask;
  352. GdkColormap *cmap;
  353. pixmap = gdk_pixmap_create_from_xpm_d(DA, &mask, NULL, xpm_frame);
  354. cmap = gdk_drawable_get_colormap(root->window);
  355. gtk_drag_source_set_icon(widget, cmap, pixmap, mask);
  356. g_object_unref(pixmap);
  357. g_object_unref(mask);
  358. dragging = user_data;
  359. }
  360. #endif
  361. static gboolean drag_frame_motion(GtkWidget *widget,
  362. GdkDragContext *drag_context, gint x, gint y, guint time_,
  363. gpointer user_data)
  364. {
  365. struct frame *from = dragging;
  366. struct frame *to = user_data;
  367. int from_n, to_n, i;
  368. if (!has_target(widget, drag_context, "frame"))
  369. return FALSE;
  370. assert(from != frames);
  371. assert(to != frames);
  372. from_n = NDX(frames, from);
  373. to_n = NDX(frames, to);
  374. FOR_UNORDERED(i, from_n, to_n)
  375. swap_frames(gtk_widget_get_ancestor(widget, GTK_TYPE_TABLE),
  376. i, i+1);
  377. return FALSE;
  378. }
  379. static void drag_frame_end(GtkWidget *widget, GdkDragContext *drag_context,
  380. gpointer user_data)
  381. {
  382. leave_canvas();
  383. drag_end(widget, drag_context, user_data);
  384. }
  385. void setup_frame_drag(struct frame *frame)
  386. {
  387. GtkWidget *box;
  388. box = box_of_label(frame->label);
  389. gtk_drag_source_set(box, GDK_BUTTON1_MASK,
  390. &target_frame, 1, GDK_ACTION_COPY | GDK_ACTION_MOVE);
  391. gtk_drag_dest_set(box, GTK_DEST_DEFAULT_MOTION,
  392. &target_frame, 1, GDK_ACTION_MOVE);
  393. setup_drag_common(box, frame);
  394. /* override */
  395. #if 0
  396. g_signal_connect(G_OBJECT(box), "drag-begin",
  397. G_CALLBACK(drag_frame_begin), frame);
  398. #endif
  399. g_signal_connect(G_OBJECT(box), "drag-end",
  400. G_CALLBACK(drag_frame_end), frame);
  401. g_signal_connect(G_OBJECT(box), "drag-motion",
  402. G_CALLBACK(drag_frame_motion), frame);
  403. }
  404. /* ----- drag to the canvas ------------------------------------------------ */
  405. static gboolean drag_canvas_motion(GtkWidget *widget,
  406. GdkDragContext *drag_context, gint x, gint y, guint time_,
  407. gpointer user_data)
  408. {
  409. if (!has_target(widget, drag_context, "frame"))
  410. return FALSE;
  411. if (!frame_on_canvas) {
  412. frame_on_canvas = 1;
  413. canvas_frame_begin(dragging);
  414. }
  415. if (canvas_frame_motion(dragging, x, y)) {
  416. gdk_drag_status(drag_context, GDK_ACTION_COPY, time_);
  417. return TRUE;
  418. } else {
  419. gdk_drag_status(drag_context, 0, time_);
  420. return FALSE;
  421. }
  422. }
  423. static void drag_canvas_leave(GtkWidget *widget, GdkDragContext *drag_context,
  424. guint time_, gpointer user_data)
  425. {
  426. leave_canvas();
  427. }
  428. static gboolean drag_canvas_drop(GtkWidget *widget,
  429. GdkDragContext *drag_context, gint x, gint y, guint time_,
  430. gpointer user_data)
  431. {
  432. if (!has_target(widget, drag_context, "frame"))
  433. return FALSE;
  434. if (!canvas_frame_drop(dragging, x, y))
  435. return FALSE;
  436. gtk_drag_finish(drag_context, TRUE, FALSE, time_);
  437. drag_end(widget, drag_context, user_data);
  438. return TRUE;
  439. }
  440. void setup_canvas_drag(GtkWidget *canvas)
  441. {
  442. gtk_drag_dest_set(canvas,
  443. GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
  444. &target_frame, 1, GDK_ACTION_COPY);
  445. g_signal_connect(G_OBJECT(canvas), "drag-motion",
  446. G_CALLBACK(drag_canvas_motion), NULL);
  447. g_signal_connect(G_OBJECT(canvas), "drag-leave",
  448. G_CALLBACK(drag_canvas_leave), NULL);
  449. g_signal_connect(G_OBJECT(canvas), "drag-drop",
  450. G_CALLBACK(drag_canvas_drop), NULL);
  451. }