* editmenu.c (edit_wrap_cmd): Use g_free() on the result
[midnight-commander.git] / gnome / gdesktop-prefs.c
blob2d20e26cc067cf37a36cb78b16e3891da7a70999
1 /* Desktop preferences box for the Midnight Commander
3 * Copyright (C) 1999 The Free Software Foundation
5 * Author: Federico Mena <federico@nuclecu.unam.mx>
6 */
8 #include <config.h>
9 #include "global.h"
10 #include "gdesktop.h"
11 #include "gdesktop-prefs.h"
14 /* Size of the icon position box */
15 #define ICON_POS_WIDTH 120
16 #define ICON_POS_HEIGHT 90
17 #define ICON_POS_INTERVAL 10
20 struct point {
21 int x, y;
24 /* Structure to hold preferences information */
25 struct _GDesktopPrefs {
26 /* Property box we are attached to */
27 GnomePropertyBox *pbox;
29 /* Canvas for the miniature icons */
30 GtkWidget *canvas;
32 /* Group that holds the miniature icons */
33 GnomeCanvasItem *group;
35 /* Icon position flags */
36 guint right_to_left : 1;
37 guint bottom_to_top : 1;
38 guint rows_not_columns : 1;
40 /* Icon positioning options */
41 GtkWidget *auto_placement;
42 GtkWidget *snap_icons;
44 /* Icon shape options */
45 GtkWidget *use_shaped_icons;
46 GtkWidget *use_shaped_text;
50 /* Creates the canvas items that represent a mini-icon */
51 static void
52 create_mini_icon (GnomeCanvasGroup *group, int x, int y, guint color)
54 gnome_canvas_item_new (group,
55 gnome_canvas_rect_get_type (),
56 "x1", (double) (x - 1),
57 "y1", (double) (y - 2),
58 "x2", (double) (x + 1),
59 "y2", (double) y,
60 "fill_color_rgba", color,
61 NULL);
63 gnome_canvas_item_new (group,
64 gnome_canvas_rect_get_type (),
65 "x1", (double) (x - 2),
66 "y1", (double) (y + 2),
67 "x2", (double) (x + 2),
68 "y2", (double) (y + 3),
69 "fill_color_rgba", color,
70 NULL);
73 /* Re-create the icon position mini-icons */
74 static void
75 create_mini_icons (GDesktopPrefs *dp, int r2l, int b2t, int rows)
77 GtkStyle *style;
78 GdkColor *c;
79 guint color;
80 int i, j;
81 int max_j;
82 int x, y;
83 int dx, dy;
84 int ox, oy;
86 dp->right_to_left = r2l ? TRUE : FALSE;
87 dp->bottom_to_top = b2t ? TRUE : FALSE;
88 dp->rows_not_columns = rows ? TRUE : FALSE;
90 /* Compute color for mini icons */
92 style = gtk_widget_get_style (GTK_WIDGET (dp->canvas));
93 c = &style->fg[GTK_STATE_NORMAL];
94 color = ((c->red & 0xff00) << 8) | (c->green & 0xff00) | (c->blue >> 8) | 0xff;
96 if (dp->group)
97 gtk_object_destroy (GTK_OBJECT (dp->group));
99 dp->group = gnome_canvas_item_new (gnome_canvas_root (GNOME_CANVAS (dp->canvas)),
100 gnome_canvas_group_get_type (),
101 NULL);
103 if (dp->right_to_left) {
104 ox = ICON_POS_WIDTH - ICON_POS_INTERVAL;
105 dx = -ICON_POS_INTERVAL;
106 } else {
107 ox = ICON_POS_INTERVAL;
108 dx = ICON_POS_INTERVAL;
111 if (dp->bottom_to_top) {
112 oy = ICON_POS_HEIGHT - ICON_POS_INTERVAL;
113 dy = -ICON_POS_INTERVAL;
114 } else {
115 oy = ICON_POS_INTERVAL;
116 dy = ICON_POS_INTERVAL;
119 x = ox;
120 y = oy;
122 for (i = 0; i < 2; i++) {
123 if (dp->rows_not_columns) {
124 x = ox;
125 max_j = (i == 1) ? (ICON_POS_WIDTH / 2) : ICON_POS_WIDTH;
126 } else {
127 y = oy;
128 max_j = (i == 1) ? (ICON_POS_HEIGHT / 2) : ICON_POS_HEIGHT;
131 for (j = ICON_POS_INTERVAL; j < max_j; j += ICON_POS_INTERVAL) {
132 create_mini_icon (GNOME_CANVAS_GROUP (dp->group), x, y, color);
134 if (dp->rows_not_columns)
135 x += dx;
136 else
137 y += dy;
140 if (dp->rows_not_columns)
141 y += dy;
142 else
143 x += dx;
147 /* Handler for button presses on the icon position canvas */
148 static gint
149 button_press (GtkWidget *widget, GdkEventButton *event, gpointer data)
151 GDesktopPrefs *dp;
152 int r2l;
153 int b2t;
154 int dx, dy;
155 int rows;
157 dp = data;
159 if (!(event->type == GDK_BUTTON_PRESS && event->button == 1))
160 return FALSE;
162 if (event->x > ICON_POS_WIDTH / 2) {
163 r2l = TRUE;
164 dx = ICON_POS_WIDTH - event->x;
165 } else {
166 r2l = FALSE;
167 dx = event->x;
170 if (event->y > ICON_POS_HEIGHT / 2) {
171 b2t = TRUE;
172 dy = ICON_POS_HEIGHT - event->y;
173 } else {
174 b2t = FALSE;
175 dy = event->y;
178 if (dx < dy)
179 rows = FALSE;
180 else
181 rows = TRUE;
183 create_mini_icons (dp, r2l, b2t, rows);
184 gnome_property_box_changed (dp->pbox);
185 return TRUE;
188 /* Creates the canvas widget and items to configure icon position */
189 static GtkWidget *
190 create_icon_pos (GDesktopPrefs *dp)
192 dp->canvas = gnome_canvas_new ();
193 gtk_widget_set_usize (dp->canvas, ICON_POS_WIDTH, ICON_POS_HEIGHT);
194 gnome_canvas_set_scroll_region (GNOME_CANVAS (dp->canvas),
195 0.0, 0.0, ICON_POS_WIDTH, ICON_POS_HEIGHT);
197 gtk_signal_connect (GTK_OBJECT (dp->canvas), "button_press_event",
198 GTK_SIGNAL_FUNC (button_press),
199 dp);
201 create_mini_icons (dp, desktop_arr_r2l, desktop_arr_b2t, desktop_arr_rows);
203 return dp->canvas;
206 /* Callback used to notify a property box when a toggle button is toggled */
207 static void
208 toggled (GtkWidget *w, gpointer data)
210 gnome_property_box_changed (GNOME_PROPERTY_BOX (data));
213 /* Creates a check box that will notify a property box when it changes */
214 static GtkWidget *
215 create_check_box (char *text, int state, GnomePropertyBox *pbox)
217 GtkWidget *w;
219 w = gtk_check_button_new_with_label (text);
220 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), state);
221 gtk_signal_connect (GTK_OBJECT (w), "toggled",
222 GTK_SIGNAL_FUNC (toggled),
223 pbox);
225 return w;
228 /* Creates the widgets that are used to configure icon position and snapping */
229 static GtkWidget *
230 create_position_widgets (GDesktopPrefs *dp)
232 GtkWidget *vbox;
233 GtkWidget *w;
234 GtkWidget *frame;
236 vbox = gtk_vbox_new (FALSE, GNOME_PAD_SMALL);
238 /* Icon position */
240 w = gtk_label_new (_("Icon position"));
241 gtk_misc_set_alignment (GTK_MISC (w), 0.0, 0.5);
242 gtk_box_pack_start (GTK_BOX (vbox), w, FALSE, FALSE, 0);
244 w = gtk_alignment_new (0.0, 0.0, 0.0, 0.0);
245 gtk_box_pack_start (GTK_BOX (vbox), w, FALSE, FALSE, 0);
247 frame = gtk_frame_new (NULL);
248 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
249 gtk_container_add (GTK_CONTAINER (w), frame);
251 w = create_icon_pos (dp);
252 gtk_container_add (GTK_CONTAINER (frame), w);
254 /* Snap and placement */
256 dp->auto_placement = create_check_box (_("Automatic icon placement"),
257 desktop_auto_placement, dp->pbox);
258 gtk_box_pack_start (GTK_BOX (vbox), dp->auto_placement, FALSE, FALSE, 0);
260 dp->snap_icons = create_check_box (_("Snap icons to grid"),
261 desktop_snap_icons, dp->pbox);
262 gtk_box_pack_start (GTK_BOX (vbox), dp->snap_icons, FALSE, FALSE, 0);
264 return vbox;
267 /* Creates the widgets that are used to configure the icon shape */
268 static GtkWidget *
269 create_shape_widgets (GDesktopPrefs *dp)
271 GtkWidget *vbox;
273 vbox = gtk_vbox_new (FALSE, GNOME_PAD_SMALL);
275 dp->use_shaped_icons = create_check_box (_("Use shaped icons"),
276 desktop_use_shaped_icons, dp->pbox);
277 gtk_box_pack_start (GTK_BOX (vbox), dp->use_shaped_icons, FALSE, FALSE, 0);
279 dp->use_shaped_text = create_check_box (_("Use shaped text"),
280 desktop_use_shaped_text, dp->pbox);
281 gtk_box_pack_start (GTK_BOX (vbox), dp->use_shaped_text, FALSE, FALSE, 0);
283 return vbox;
286 /* Callback to destroy the desktop preferences data */
287 static void
288 destroy (GtkObject *object, gpointer data)
290 GDesktopPrefs *dp;
292 dp = data;
293 g_free (dp);
296 /* Creates all the widgets in the desktop preferences page */
297 static GtkWidget *
298 create_widgets (GDesktopPrefs *dp)
300 GtkWidget *hbox;
301 GtkWidget *w;
303 hbox = gtk_hbox_new (FALSE, GNOME_PAD_SMALL);
304 gtk_container_set_border_width (GTK_CONTAINER (hbox), GNOME_PAD_SMALL);
305 gtk_signal_connect (GTK_OBJECT (hbox), "destroy",
306 GTK_SIGNAL_FUNC (destroy),
307 dp);
309 w = create_position_widgets (dp);
310 gtk_box_pack_start (GTK_BOX (hbox), w, TRUE, TRUE, 0);
312 w = create_shape_widgets (dp);
313 gtk_box_pack_start (GTK_BOX (hbox), w, TRUE, TRUE, 0);
315 gtk_widget_show_all (hbox);
316 return hbox;
319 /* Creates the desktop preferences page */
320 GDesktopPrefs *
321 desktop_prefs_new (GnomePropertyBox *pbox)
323 GDesktopPrefs *dp;
324 GtkWidget *page;
326 g_return_val_if_fail (pbox != NULL, NULL);
328 dp = g_new0 (GDesktopPrefs, 1);
330 dp->pbox = pbox;
332 page = create_widgets (dp);
333 gnome_property_box_append_page (pbox, page, gtk_label_new (_("Desktop")));
335 return dp;
338 /* Applies the changes in the desktop preferences page */
339 void
340 desktop_prefs_apply (GDesktopPrefs *dp)
342 g_return_if_fail (dp != NULL);
344 desktop_arr_r2l = dp->right_to_left;
345 desktop_arr_b2t = dp->bottom_to_top;
346 desktop_arr_rows = dp->rows_not_columns;
348 desktop_auto_placement = gtk_toggle_button_get_active (
349 GTK_TOGGLE_BUTTON (dp->auto_placement));
351 desktop_snap_icons = gtk_toggle_button_get_active (
352 GTK_TOGGLE_BUTTON (dp->snap_icons));
354 desktop_use_shaped_icons = gtk_toggle_button_get_active (
355 GTK_TOGGLE_BUTTON (dp->use_shaped_icons));
357 desktop_use_shaped_text = gtk_toggle_button_get_active (
358 GTK_TOGGLE_BUTTON (dp->use_shaped_text));
360 desktop_reload_icons (FALSE, 0, 0);