my homework was late :-)
[midnight-commander.git] / gnome / gdesktop-prefs.c
blob34a4ad9945339cb7b16856519fa2e292db6df041
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 "gdesktop.h"
10 #include <gnome.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 if (dp->rows_not_columns)
120 y = oy;
121 else
122 x = ox;
124 for (i = 0; i < 2; i++) {
125 if (dp->rows_not_columns) {
126 x = ox;
127 max_j = (i == 1) ? (ICON_POS_WIDTH / 2) : ICON_POS_WIDTH;
128 } else {
129 y = oy;
130 max_j = (i == 1) ? (ICON_POS_HEIGHT / 2) : ICON_POS_HEIGHT;
133 for (j = ICON_POS_INTERVAL; j < max_j; j += ICON_POS_INTERVAL) {
134 create_mini_icon (GNOME_CANVAS_GROUP (dp->group), x, y, color);
136 if (dp->rows_not_columns)
137 x += dx;
138 else
139 y += dy;
142 if (dp->rows_not_columns)
143 y += dy;
144 else
145 x += dx;
149 /* Handler for button presses on the icon position canvas */
150 static gint
151 button_press (GtkWidget *widget, GdkEventButton *event, gpointer data)
153 GDesktopPrefs *dp;
154 int r2l;
155 int b2t;
156 int dx, dy;
157 int rows;
159 dp = data;
161 if (!(event->type == GDK_BUTTON_PRESS && event->button == 1))
162 return FALSE;
164 if (event->x > ICON_POS_WIDTH / 2) {
165 r2l = TRUE;
166 dx = ICON_POS_WIDTH - event->x;
167 } else {
168 r2l = FALSE;
169 dx = event->x;
172 if (event->y > ICON_POS_HEIGHT / 2) {
173 b2t = TRUE;
174 dy = ICON_POS_HEIGHT - event->y;
175 } else {
176 b2t = FALSE;
177 dy = event->y;
180 if (dx < dy)
181 rows = FALSE;
182 else
183 rows = TRUE;
185 create_mini_icons (dp, r2l, b2t, rows);
186 gnome_property_box_changed (dp->pbox);
187 return TRUE;
190 /* Creates the canvas widget and items to configure icon position */
191 static GtkWidget *
192 create_icon_pos (GDesktopPrefs *dp)
194 dp->canvas = gnome_canvas_new ();
195 gtk_widget_set_usize (dp->canvas, ICON_POS_WIDTH, ICON_POS_HEIGHT);
196 gnome_canvas_set_scroll_region (GNOME_CANVAS (dp->canvas),
197 0.0, 0.0, ICON_POS_WIDTH, ICON_POS_HEIGHT);
199 gtk_signal_connect (GTK_OBJECT (dp->canvas), "button_press_event",
200 GTK_SIGNAL_FUNC (button_press),
201 dp);
203 create_mini_icons (dp, desktop_arr_r2l, desktop_arr_b2t, desktop_arr_rows);
205 return dp->canvas;
208 /* Callback used to notify a property box when a toggle button is toggled */
209 static void
210 toggled (GtkWidget *w, gpointer data)
212 gnome_property_box_changed (GNOME_PROPERTY_BOX (data));
215 /* Creates a check box that will notify a property box when it changes */
216 static GtkWidget *
217 create_check_box (char *text, int state, GnomePropertyBox *pbox)
219 GtkWidget *w;
221 w = gtk_check_button_new_with_label (text);
222 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), state);
223 gtk_signal_connect (GTK_OBJECT (w), "toggled",
224 GTK_SIGNAL_FUNC (toggled),
225 pbox);
227 return w;
230 /* Creates the widgets that are used to configure icon position and snapping */
231 static GtkWidget *
232 create_position_widgets (GDesktopPrefs *dp)
234 GtkWidget *vbox;
235 GtkWidget *w;
236 GtkWidget *frame;
238 vbox = gtk_vbox_new (FALSE, GNOME_PAD_SMALL);
240 /* Icon position */
242 w = gtk_label_new (_("Icon position"));
243 gtk_misc_set_alignment (GTK_MISC (w), 0.0, 0.5);
244 gtk_box_pack_start (GTK_BOX (vbox), w, FALSE, FALSE, 0);
246 w = gtk_alignment_new (0.0, 0.0, 0.0, 0.0);
247 gtk_box_pack_start (GTK_BOX (vbox), w, FALSE, FALSE, 0);
249 frame = gtk_frame_new (NULL);
250 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
251 gtk_container_add (GTK_CONTAINER (w), frame);
253 w = create_icon_pos (dp);
254 gtk_container_add (GTK_CONTAINER (frame), w);
256 /* Snap and placement */
258 dp->auto_placement = create_check_box (_("Automatic icon placement"),
259 desktop_auto_placement, dp->pbox);
260 gtk_box_pack_start (GTK_BOX (vbox), dp->auto_placement, FALSE, FALSE, 0);
262 dp->snap_icons = create_check_box (_("Snap icons to grid"),
263 desktop_snap_icons, dp->pbox);
264 gtk_box_pack_start (GTK_BOX (vbox), dp->snap_icons, FALSE, FALSE, 0);
266 return vbox;
269 /* Creates the widgets that are used to configure the icon shape */
270 static GtkWidget *
271 create_shape_widgets (GDesktopPrefs *dp)
273 GtkWidget *vbox;
275 vbox = gtk_vbox_new (FALSE, GNOME_PAD_SMALL);
277 dp->use_shaped_icons = create_check_box (_("Use shaped icons"),
278 desktop_use_shaped_icons, dp->pbox);
279 gtk_box_pack_start (GTK_BOX (vbox), dp->use_shaped_icons, FALSE, FALSE, 0);
281 dp->use_shaped_text = create_check_box (_("Use shaped text"),
282 desktop_use_shaped_text, dp->pbox);
283 gtk_box_pack_start (GTK_BOX (vbox), dp->use_shaped_text, FALSE, FALSE, 0);
285 return vbox;
288 /* Callback to destroy the desktop preferences data */
289 static void
290 destroy (GtkObject *object, gpointer data)
292 GDesktopPrefs *dp;
294 dp = data;
295 g_free (dp);
298 /* Creates all the widgets in the desktop preferences page */
299 static GtkWidget *
300 create_widgets (GDesktopPrefs *dp)
302 GtkWidget *hbox;
303 GtkWidget *w;
305 hbox = gtk_hbox_new (FALSE, GNOME_PAD_SMALL);
306 gtk_container_set_border_width (GTK_CONTAINER (hbox), GNOME_PAD_SMALL);
307 gtk_signal_connect (GTK_OBJECT (hbox), "destroy",
308 GTK_SIGNAL_FUNC (destroy),
309 dp);
311 w = create_position_widgets (dp);
312 gtk_box_pack_start (GTK_BOX (hbox), w, TRUE, TRUE, 0);
314 w = create_shape_widgets (dp);
315 gtk_box_pack_start (GTK_BOX (hbox), w, TRUE, TRUE, 0);
317 gtk_widget_show_all (hbox);
318 return hbox;
321 /* Creates the desktop preferences page */
322 GDesktopPrefs *
323 desktop_prefs_new (GnomePropertyBox *pbox)
325 GDesktopPrefs *dp;
326 GtkWidget *page;
328 g_return_val_if_fail (pbox != NULL, NULL);
330 dp = g_new0 (GDesktopPrefs, 1);
332 dp->pbox = pbox;
334 page = create_widgets (dp);
335 gnome_property_box_append_page (pbox, page, gtk_label_new (_("Desktop")));
337 return dp;
340 /* Applies the changes in the desktop preferences page */
341 void
342 desktop_prefs_apply (GDesktopPrefs *dp)
344 g_return_if_fail (dp != NULL);
346 desktop_arr_r2l = dp->right_to_left;
347 desktop_arr_b2t = dp->bottom_to_top;
348 desktop_arr_rows = dp->rows_not_columns;
350 desktop_auto_placement = gtk_toggle_button_get_active (
351 GTK_TOGGLE_BUTTON (dp->auto_placement));
353 desktop_snap_icons = gtk_toggle_button_get_active (
354 GTK_TOGGLE_BUTTON (dp->snap_icons));
356 desktop_use_shaped_icons = gtk_toggle_button_get_active (
357 GTK_TOGGLE_BUTTON (dp->use_shaped_icons));
359 desktop_use_shaped_text = gtk_toggle_button_get_active (
360 GTK_TOGGLE_BUTTON (dp->use_shaped_text));
362 desktop_reload_icons (FALSE, 0, 0);