gui_util.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * gui_util.c - GUI helper functions
  3. *
  4. * Written 2009, 2010 by Werner Almesberger
  5. * Copyright 2009, 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 <stdlib.h>
  13. #include <math.h>
  14. #include <gtk/gtk.h>
  15. #include "util.h"
  16. #include "gui_style.h"
  17. #include "gui.h"
  18. #include "gui_util.h"
  19. struct draw_ctx draw_ctx;
  20. /* ----- look up a color --------------------------------------------------- */
  21. GdkColor get_color(const char *spec)
  22. {
  23. GdkColormap *cmap;
  24. GdkColor color;
  25. cmap = gdk_drawable_get_colormap(root->window);
  26. if (!gdk_color_parse(spec, &color))
  27. abort();
  28. if (!gdk_colormap_alloc_color(cmap, &color, FALSE, TRUE))
  29. abort();
  30. return color;
  31. }
  32. /* ----- lines with a width ------------------------------------------------ */
  33. void set_width(GdkGC *gc, int width)
  34. {
  35. gdk_gc_set_line_attributes(gc, width < 1 ? 1 : width,
  36. GDK_LINE_SOLID, GDK_CAP_ROUND, GDK_JOIN_ROUND);
  37. }
  38. /* ----- backing store ----------------------------------------------------- */
  39. void free_pix_buf(struct pix_buf *buf)
  40. {
  41. g_object_unref(G_OBJECT(buf->buf));
  42. free(buf);
  43. }
  44. struct pix_buf *save_pix_buf(GdkDrawable *da, int xa, int ya, int xb, int yb,
  45. int border)
  46. {
  47. struct pix_buf *buf;
  48. int w, h;
  49. if (xa > xb)
  50. SWAP(xa, xb);
  51. if (ya > yb)
  52. SWAP(ya, yb);
  53. buf = alloc_type(struct pix_buf);
  54. buf->da = da;
  55. buf->x = xa-border;
  56. buf->y = ya-border;
  57. w = xb-xa+1+2*border;
  58. h = yb-ya+1+2*border;
  59. if (buf->x < 0) {
  60. w += buf->x;
  61. buf->x = 0;
  62. }
  63. if (buf->y < 0) {
  64. h += buf->y;
  65. buf->y = 0;
  66. }
  67. buf->buf = gdk_pixbuf_get_from_drawable(NULL, da, NULL,
  68. buf->x, buf->y, 0, 0, w, h);
  69. return buf;
  70. }
  71. void restore_pix_buf(struct pix_buf *buf)
  72. {
  73. gdk_draw_pixbuf(buf->da, NULL, buf->buf, 0, 0, buf->x, buf->y, -1, -1,
  74. GDK_RGB_DITHER_NORMAL, 0, 0);
  75. free_pix_buf(buf);
  76. }
  77. /* ----- arcs and circles -------------------------------------------------- */
  78. void draw_arc(GdkDrawable *da, GdkGC *gc, int fill,
  79. int x, int y, int r, double a1, double a2)
  80. {
  81. /*
  82. * This adjustment handles two distinct cases:
  83. * - if a1 == a2, we make sure we draw a full circle
  84. * - the end angle a2 must always be greater than the start angle a1
  85. */
  86. if (a2 <= a1)
  87. a2 += 360;
  88. gdk_draw_arc(da, gc, fill, x-r, y-r, 2*r, 2*r, a1*64, (a2-a1)*64);
  89. }
  90. void draw_circle(GdkDrawable *da, GdkGC *gc, int fill,
  91. int x, int y, int r)
  92. {
  93. draw_arc(da, gc, fill, x, y, r, 0, 360);
  94. }
  95. /* ----- labels in a box --------------------------------------------------- */
  96. GtkWidget *label_in_box_new(const char *s, const char *tooltip)
  97. {
  98. GtkWidget *evbox, *label;
  99. evbox = gtk_event_box_new();
  100. label = gtk_label_new(s);
  101. gtk_misc_set_padding(GTK_MISC(label), 1, 1);
  102. gtk_container_add(GTK_CONTAINER(evbox), label);
  103. if (tooltip)
  104. gtk_widget_set_tooltip_markup(evbox, tooltip);
  105. return label;
  106. }
  107. GtkWidget *box_of_label(GtkWidget *label)
  108. {
  109. return gtk_widget_get_ancestor(label, GTK_TYPE_EVENT_BOX);
  110. }
  111. void label_in_box_fg(GtkWidget *label, const char *color)
  112. {
  113. GdkColor col = get_color(color);
  114. gtk_widget_modify_fg(label, GTK_STATE_NORMAL, &col);
  115. }
  116. void label_in_box_bg(GtkWidget *label, const char *color)
  117. {
  118. GtkWidget *box;
  119. GdkColor col = get_color(color);
  120. box = box_of_label(label);
  121. gtk_widget_modify_bg(box, GTK_STATE_NORMAL, &col);
  122. }
  123. /* ----- generate a tool button with an XPM image -------------------------- */
  124. GtkWidget *make_image(GdkDrawable *drawable, char **xpm, const char *tooltip)
  125. {
  126. GdkPixmap *pixmap;
  127. GtkWidget *image;
  128. GdkColor white = get_color("white");
  129. pixmap = gdk_pixmap_create_from_xpm_d(drawable, NULL, &white, xpm);
  130. image = gtk_image_new_from_pixmap(pixmap, NULL);
  131. gtk_misc_set_padding(GTK_MISC(image), 1, 1);
  132. if (tooltip)
  133. gtk_widget_set_tooltip_markup(image, tooltip);
  134. return image;
  135. }
  136. GtkWidget *make_transparent_image(GdkDrawable *drawable, char **xpm,
  137. const char *tooltip)
  138. {
  139. GdkPixmap *pixmap;
  140. GdkBitmap *mask;
  141. GtkWidget *image;
  142. pixmap = gdk_pixmap_create_from_xpm_d(drawable, &mask, NULL, xpm);
  143. image = gtk_image_new_from_pixmap(pixmap, mask);
  144. gtk_misc_set_padding(GTK_MISC(image), 1, 1);
  145. if (tooltip)
  146. gtk_widget_set_tooltip_markup(image, tooltip);
  147. return image;
  148. }
  149. static void remove_child(GtkWidget *widget, gpointer data)
  150. {
  151. gtk_container_remove(GTK_CONTAINER(data), widget);
  152. }
  153. void vacate_widget(GtkWidget *widget)
  154. {
  155. gtk_container_foreach(GTK_CONTAINER(widget), remove_child, widget);
  156. }
  157. void set_image(GtkWidget *widget, GtkWidget *image)
  158. {
  159. vacate_widget(widget);
  160. gtk_container_add(GTK_CONTAINER(widget), image);
  161. gtk_widget_show_all(widget);
  162. }
  163. GtkWidget *tool_button(GtkWidget *bar, GdkDrawable *drawable,
  164. char **xpm, const char *tooltip,
  165. gboolean (*cb)(GtkWidget *widget, GdkEventButton *event, gpointer data),
  166. gpointer data)
  167. {
  168. GtkWidget *image, *evbox;
  169. GtkToolItem *item;
  170. /*
  171. * gtk_radio_tool_button_new_from_widget is *huge*. We try to do things
  172. * in a
  173. * more compact way.
  174. */
  175. evbox = gtk_event_box_new();
  176. if (xpm) {
  177. image = make_image(drawable, xpm, tooltip);
  178. gtk_container_add(GTK_CONTAINER(evbox), image);
  179. }
  180. g_signal_connect(G_OBJECT(evbox), "button_press_event",
  181. G_CALLBACK(cb), data);
  182. item = gtk_tool_item_new();
  183. gtk_container_add(GTK_CONTAINER(item), evbox);
  184. gtk_container_set_border_width(GTK_CONTAINER(item), 0);
  185. gtk_toolbar_insert(GTK_TOOLBAR(bar), item, -1);
  186. return evbox;
  187. }
  188. /* ----- render a text string ---------------------------------------------- */
  189. void render_text(GdkDrawable *da, GdkGC *gc, int x, int y, double angle,
  190. const char *s, const char *font, double xalign, double yalign,
  191. int xmax, int ymax)
  192. {
  193. GdkScreen *screen;
  194. PangoRenderer *renderer;
  195. PangoContext *context;
  196. PangoLayout *layout;
  197. PangoFontDescription *desc;
  198. int width, height;
  199. PangoMatrix m = PANGO_MATRIX_INIT;
  200. double f_min, f;
  201. /* set up the renderer */
  202. screen = gdk_drawable_get_screen(da);
  203. renderer = gdk_pango_renderer_get_default(screen);
  204. gdk_pango_renderer_set_drawable(GDK_PANGO_RENDERER(renderer), da);
  205. gdk_pango_renderer_set_gc(GDK_PANGO_RENDERER(renderer), gc);
  206. /* start preparing the layout */
  207. context = gdk_pango_context_get_for_screen(screen);
  208. layout = pango_layout_new(context);
  209. pango_layout_set_text(layout, s, -1);
  210. /* apply the font */
  211. desc = pango_font_description_from_string(font);
  212. pango_layout_set_font_description(layout, desc);
  213. pango_font_description_free(desc);
  214. /* align and position the text */
  215. pango_layout_get_size(layout, &width, &height);
  216. f_min = 1.0;
  217. if (xmax) {
  218. f = xmax/((double) width/PANGO_SCALE);
  219. if (f < f_min)
  220. f_min = f;
  221. }
  222. if (ymax) {
  223. f = ymax/((double) height/PANGO_SCALE);
  224. if (f < f_min)
  225. f_min = f;
  226. }
  227. if (f_min < MIN_FONT_SCALE)
  228. f_min = MIN_FONT_SCALE;
  229. pango_matrix_translate(&m, x, y);
  230. pango_matrix_rotate(&m, angle);
  231. pango_matrix_translate(&m,
  232. -xalign*f_min*width/PANGO_SCALE,
  233. (yalign-1)*f_min*height/PANGO_SCALE);
  234. pango_matrix_scale(&m, f_min, f_min);
  235. pango_context_set_matrix(context, &m);
  236. pango_layout_context_changed(layout);
  237. pango_renderer_draw_layout(renderer, layout, 0, 0);
  238. /* clean up renderer */
  239. gdk_pango_renderer_set_drawable(GDK_PANGO_RENDERER(renderer), NULL);
  240. gdk_pango_renderer_set_gc(GDK_PANGO_RENDERER(renderer), NULL);
  241. /* free objects */
  242. g_object_unref(layout);
  243. g_object_unref(context);
  244. }
  245. /* ----- Debugging support ------------------------------------------------- */
  246. /*
  247. * View with make montage or something like
  248. *
  249. * montage -label %f -frame 3 __dbg????.png png:- | display -
  250. */
  251. void debug_save_pixbuf(GdkPixbuf *buf)
  252. {
  253. static int buf_num = 0;
  254. char name[20]; /* plenty */
  255. sprintf(name, "__dbg%04d.png", buf_num++);
  256. gdk_pixbuf_save(buf, name, "png", NULL, NULL);
  257. fprintf(stderr, "saved to %s\n", name);
  258. }
  259. /*
  260. * gtk_widget_get_snapshot seems to use an expose event to do the drawing. This
  261. * means that we can't call debug_save_widget from the expose event handler of
  262. * the widget being dumped.
  263. */
  264. #if GTK_CHECK_VERSION(2, 14, 0)
  265. void debug_save_widget(GtkWidget *widget)
  266. {
  267. GdkPixmap *pixmap;
  268. GdkPixbuf *pixbuf;
  269. gint w, h;
  270. pixmap = gtk_widget_get_snapshot(widget, NULL);
  271. gdk_drawable_get_size(GDK_DRAWABLE(pixmap), &w, &h);
  272. pixbuf = gdk_pixbuf_get_from_drawable(NULL, GDK_DRAWABLE(pixmap),
  273. NULL, 0, 0, 0, 0, w, h);
  274. debug_save_pixbuf(pixbuf);
  275. gdk_pixmap_unref(pixmap);
  276. g_object_unref(pixbuf);
  277. }
  278. #endif /* GTK_CHECK_VERSION(2, 14, 0) */
  279. /* ----- kill the content of a container ----------------------------------- */
  280. static void destroy_callback(GtkWidget *widget, gpointer data)
  281. {
  282. gtk_widget_destroy(widget);
  283. }
  284. void destroy_all_children(GtkContainer *container)
  285. {
  286. gtk_container_foreach(container, destroy_callback, NULL);
  287. }
  288. /* ----- get a widget's desired width -------------------------------------- */
  289. int get_widget_width(GtkWidget *widget)
  290. {
  291. GtkRequisition req;
  292. gtk_widget_show_all(widget);
  293. gtk_widget_size_request(widget, &req);
  294. return req.width;
  295. }