updated on Thu Jan 19 20:01:47 UTC 2012
[aur-mirror.git] / xfce4-corner-plugin / xfce4-corner-plugin.c
blobc6d547561213523bc71ca285d0e7b72e0923d447
1 #include <gtk/gtk.h>
2 #include <libxfce4panel/xfce-panel-plugin.h>
3 #include <unistd.h>
5 typedef enum
7 // dir & 1 = LEFT or RIGHT
8 // dir & 2 = TOP or BOTTOM
9 TOP_LEFT = 0,
10 TOP_RIGHT = 1,
11 BOTTOM_LEFT = 2,
12 BOTTOM_RIGHT = 3
14 CornerDir;
16 const int CORNER_DIR_COUNT = 4;
18 typedef struct {
19 XfcePanelPlugin* plugin;
20 // Core Data
21 CornerDir corner_dir;
22 // Widgets
23 GtkWidget* eventbox;
24 GtkWidget* drawing;
26 CornerImg;
28 static void cornerimg_construct (XfcePanelPlugin *plugin);
29 XFCE_PANEL_PLUGIN_REGISTER_EXTERNAL (cornerimg_construct);
31 static void gdk_color_to_doubles (GdkColor color, double* output)
33 const int R=0, G=1, B=2;
34 output[R] = color.red / 65535.0;
35 output[G] = color.green / 65535.0;
36 output[B] = color.blue / 65535.0;
39 static void drawing_update(CornerImg* cimg, cairo_t* cr)
41 double width = cimg->drawing->allocation.width;
42 double height = cimg->drawing->allocation.height;
43 double pc[3];
44 gdk_color_to_doubles(cimg->drawing->style->bg[GTK_STATE_NORMAL], pc);
45 cairo_matrix_t matrix = {width, 0.0, 0.0, height, 0.0, 0.0};
46 if (cimg->corner_dir & 1) // left or right?
48 matrix.x0 = width;
49 matrix.xx *= -1;
51 if (cimg->corner_dir & 2)
53 matrix.y0 = height;
54 matrix.yy *= -1;
57 cairo_set_matrix(cr, &matrix);
58 cairo_set_antialias(cr, CAIRO_ANTIALIAS_GRAY);
60 cairo_set_source_rgb(cr, pc[0], pc[1], pc[2]);
61 cairo_rectangle(cr, 0.0, 0.0, 1.0, 1.0);
62 cairo_fill(cr);
64 cairo_move_to(cr, 1.0, 0.0);
65 cairo_line_to(cr, 0.0, 0.0);
66 cairo_line_to(cr, 0.0, 0.5);
67 cairo_curve_to(cr, 0.0, 0.3, 0.2, 0.0, 1.0, 0.0);
69 cairo_close_path(cr);
70 cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
71 cairo_fill(cr);
74 static gboolean drawing_on_expose(GtkWidget* widget,
75 GdkEventExpose* event,
76 gpointer my_data)
78 CornerImg* cimg = (CornerImg*) my_data;
79 cairo_t* cr = gdk_cairo_create(widget->window);
80 cairo_rectangle(cr,
81 event->area.x, event->area.y,
82 event->area.width, event->area.height);
83 cairo_clip(cr);
84 drawing_update(cimg, cr);
85 cairo_destroy(cr);
86 return FALSE;
89 void cornerimg_load_from_rc(CornerImg* cimg)
91 char* file;
92 if (!(file = xfce_panel_plugin_lookup_rc_file(cimg->plugin))) return;
93 XfceRc* rc = xfce_rc_simple_open(file, TRUE);
94 g_free(file);
95 if (!rc) return;
97 cimg->corner_dir = xfce_rc_read_int_entry(rc, "CornerDir", 0);
99 xfce_rc_close (rc);
102 void cornerimg_write_rc(CornerImg* cimg)
104 char* file;
105 if (!(file = xfce_panel_plugin_save_location(cimg->plugin, TRUE))) return;
106 unlink(file);
107 XfceRc* rc = xfce_rc_simple_open(file, FALSE);
108 g_free(file);
109 if (!rc) return;
111 xfce_rc_write_int_entry(rc, "CornerDir", cimg->corner_dir);
113 xfce_rc_close (rc);
116 static void cornerimg_set_size(XfcePanelPlugin *plugin,
117 int size,
118 CornerImg* cimg)
120 gtk_widget_set_size_request(GTK_WIDGET(plugin), size/2, size);
123 static void cornerimg_free_data(XfcePanelPlugin *plugin,
124 CornerImg* cimg)
126 panel_slice_free(CornerImg, cimg);
129 static gboolean cornerimg_toggle_dir(GtkWidget* widget, GdkEventButton* event,
130 CornerImg* cimg)
132 if (event->button == 1)
134 cimg->corner_dir ++;
135 if (cimg->corner_dir >= CORNER_DIR_COUNT)
136 cimg->corner_dir = 0;
137 cairo_t* cr = gdk_cairo_create (cimg->drawing->window);
138 drawing_update(cimg, cr);
139 cairo_destroy (cr);
140 cornerimg_write_rc(cimg);
142 return FALSE;
145 static void cornerimg_construct(XfcePanelPlugin* plugin)
147 CornerImg *cimg = panel_slice_new0 (CornerImg);
148 cimg->plugin = plugin;
149 cornerimg_load_from_rc(cimg);
151 gtk_container_set_border_width(GTK_CONTAINER(plugin), 0);
153 cimg->eventbox = gtk_event_box_new();
154 gtk_container_add(GTK_CONTAINER(plugin), cimg->eventbox);
155 g_signal_connect(cimg->eventbox, "button-press-event",
156 G_CALLBACK(cornerimg_toggle_dir), cimg);
158 cimg->drawing = gtk_drawing_area_new();
159 g_signal_connect (cimg->drawing, "expose-event",
160 G_CALLBACK (drawing_on_expose), cimg);
161 gtk_container_add(GTK_CONTAINER(cimg->eventbox), cimg->drawing);
163 g_signal_connect(plugin, "size-changed",
164 G_CALLBACK(cornerimg_set_size), NULL);
165 g_signal_connect(plugin, "free-data",
166 G_CALLBACK (cornerimg_free_data), cimg);
168 gtk_widget_show_all(cimg->eventbox);
170 xfce_panel_plugin_add_action_widget (plugin, GTK_WIDGET(cimg->eventbox));
171 cornerimg_set_size(plugin, xfce_panel_plugin_get_size(plugin), cimg);