offset fixes, admit to textline, proper updating of height/font at attr set,
[dia.git] / app / dia-props.c
blob86d8af8d79165720dac353c30d8fc5815167323e
1 /* Dia -- an diagram creation/manipulation program
2 * Copyright (C) 1998 Alexander Larsson
4 * dia-props.c - a dialog for the diagram properties
5 * Copyright (C) 2000 James Henstridge
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.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
26 #include "dia-props.h"
28 #include <gtk/gtk.h>
30 #include "intl.h"
31 #include "display.h"
32 #include "widgets.h"
33 #include "display.h"
35 static GtkWidget *dialog = NULL;
36 static GtkWidget *dynamic_check;
37 static GtkWidget *width_x_entry, *width_y_entry;
38 static GtkWidget *visible_x_entry, *visible_y_entry;
39 static GtkWidget *bg_colour, *grid_colour, *pagebreak_colour;
40 static GtkWidget *hex_check, *hex_size_entry;
42 static void diagram_properties_respond(GtkWidget *widget,
43 gint response_id,
44 gpointer user_data);
46 static void
47 diagram_properties_update_sensitivity(GtkToggleButton *widget,
48 gpointer userdata)
50 Diagram *dia = ddisplay_active_diagram();
51 gboolean dyn_grid, square_grid, hex_grid;
53 if (!dia)
54 return; /* safety first */
56 dia->grid.dynamic =
57 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dynamic_check));
58 dyn_grid = dia->grid.dynamic;
59 if (!dyn_grid)
60 dia->grid.hex =
61 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(hex_check));
63 square_grid = !dyn_grid && !dia->grid.hex;
64 hex_grid = !dyn_grid && dia->grid.hex;
67 gtk_widget_set_sensitive(width_x_entry, square_grid);
68 gtk_widget_set_sensitive(width_y_entry, square_grid);
69 gtk_widget_set_sensitive(visible_x_entry, square_grid);
70 gtk_widget_set_sensitive(visible_y_entry, square_grid);
71 gtk_widget_set_sensitive(hex_check, !dyn_grid);
72 gtk_widget_set_sensitive(hex_size_entry, hex_grid);
75 static void
76 create_diagram_properties_dialog(Diagram *dia)
78 GtkWidget *dialog_vbox;
79 GtkWidget *notebook;
80 GtkWidget *table;
81 GtkWidget *label;
82 GtkAdjustment *adj;
84 dialog = gtk_dialog_new_with_buttons(
85 _("Diagram Properties"),
86 GTK_WINDOW(ddisplay_active()->shell),
87 GTK_DIALOG_DESTROY_WITH_PARENT,
88 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL,
89 GTK_STOCK_APPLY, GTK_RESPONSE_APPLY,
90 GTK_STOCK_OK, GTK_RESPONSE_OK,
91 NULL);
93 gtk_dialog_set_default_response (GTK_DIALOG(dialog), GTK_RESPONSE_OK);
95 dialog_vbox = GTK_DIALOG(dialog)->vbox;
97 gtk_window_set_role(GTK_WINDOW(dialog), "diagram_properties");
99 g_signal_connect(G_OBJECT(dialog), "response",
100 G_CALLBACK(diagram_properties_respond),
101 NULL);
102 g_signal_connect(G_OBJECT(dialog), "delete_event",
103 G_CALLBACK(gtk_widget_hide), NULL);
104 g_signal_connect(G_OBJECT(dialog), "destroy",
105 G_CALLBACK(gtk_widget_destroyed), &dialog);
107 notebook = gtk_notebook_new();
108 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_TOP);
109 gtk_box_pack_start(GTK_BOX(dialog_vbox), notebook, TRUE, TRUE, 0);
110 gtk_container_set_border_width(GTK_CONTAINER(notebook), 2);
111 gtk_widget_show(notebook);
113 /* the grid page */
114 table = gtk_table_new(3,3,FALSE);
115 gtk_container_set_border_width(GTK_CONTAINER(table), 2);
116 gtk_table_set_row_spacings(GTK_TABLE(table), 1);
117 gtk_table_set_col_spacings(GTK_TABLE(table), 2);
119 dynamic_check = gtk_check_button_new_with_label(_("Dynamic grid"));
120 gtk_table_attach(GTK_TABLE(table), dynamic_check, 1,2, 0,1,
121 GTK_FILL, GTK_FILL, 0, 0);
122 g_signal_connect(G_OBJECT(dynamic_check), "toggled",
123 G_CALLBACK(diagram_properties_update_sensitivity), NULL);
125 gtk_widget_show(dynamic_check);
127 label = gtk_label_new(_("x"));
128 gtk_table_attach(GTK_TABLE(table), label, 1,2, 1,2,
129 GTK_FILL, GTK_FILL, 0, 0);
130 gtk_widget_show(label);
131 label = gtk_label_new(_("y"));
132 gtk_table_attach(GTK_TABLE(table), label, 2,3, 1,2,
133 GTK_FILL, GTK_FILL, 0, 0);
134 gtk_widget_show(label);
136 label = gtk_label_new(_("Spacing"));
137 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
138 gtk_table_attach(GTK_TABLE(table), label, 0,1, 2,3,
139 GTK_FILL, GTK_FILL, 0, 0);
140 gtk_widget_show(label);
142 adj = GTK_ADJUSTMENT(gtk_adjustment_new(1.0, 0.0, 10.0, 0.1, 10.0, 10.0));
143 width_x_entry = gtk_spin_button_new(adj, 1.0, 3);
144 gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(width_x_entry), TRUE);
145 gtk_table_attach(GTK_TABLE(table), width_x_entry, 1,2, 2,3,
146 GTK_FILL|GTK_EXPAND, GTK_FILL, 0, 0);
147 gtk_widget_show(width_x_entry);
149 adj = GTK_ADJUSTMENT(gtk_adjustment_new(1.0, 0.0, 10.0, 0.1, 10.0, 10.0));
150 width_y_entry = gtk_spin_button_new(adj, 1.0, 3);
151 gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(width_y_entry), TRUE);
152 gtk_table_attach(GTK_TABLE(table), width_y_entry, 2,3, 2,3,
153 GTK_FILL|GTK_EXPAND, GTK_FILL, 0, 0);
154 gtk_widget_show(width_y_entry);
156 label = gtk_label_new(_("Visible spacing"));
157 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
158 gtk_table_attach(GTK_TABLE(table), label, 0,1, 3,4,
159 GTK_FILL, GTK_FILL, 0, 0);
160 gtk_widget_show(label);
162 adj = GTK_ADJUSTMENT(gtk_adjustment_new(1.0, 0.0, 100.0, 1.0, 10.0, 10.0));
163 visible_x_entry = gtk_spin_button_new(adj, 1.0, 0);
164 gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(visible_x_entry), TRUE);
165 gtk_table_attach(GTK_TABLE(table), visible_x_entry, 1,2, 3,4,
166 GTK_FILL|GTK_EXPAND, GTK_FILL, 0, 0);
167 gtk_widget_show(visible_x_entry);
169 adj = GTK_ADJUSTMENT(gtk_adjustment_new(1.0, 0.0, 100.0, 1.0, 10.0, 10.0));
170 visible_y_entry = gtk_spin_button_new(adj, 1.0, 0);
171 gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(visible_y_entry), TRUE);
172 gtk_table_attach(GTK_TABLE(table), visible_y_entry, 2,3, 3,4,
173 GTK_FILL|GTK_EXPAND, GTK_FILL, 0, 0);
174 gtk_widget_show(visible_y_entry);
176 /* Hexes! */
177 hex_check = gtk_check_button_new_with_label(_("Hex grid"));
178 gtk_table_attach(GTK_TABLE(table), hex_check, 1,2, 4,5,
179 GTK_FILL, GTK_FILL, 0, 0);
180 g_signal_connect(G_OBJECT(hex_check), "toggled",
181 G_CALLBACK(diagram_properties_update_sensitivity), NULL);
183 gtk_widget_show(hex_check);
185 label = gtk_label_new(_("Hex grid size"));
186 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
187 gtk_table_attach(GTK_TABLE(table), label, 0,1, 5,6,
188 GTK_FILL, GTK_FILL, 0, 0);
189 gtk_widget_show(label);
191 adj = GTK_ADJUSTMENT(gtk_adjustment_new(1.0, 0.0, 100.0, 1.0, 10.0, 10.0));
192 hex_size_entry = gtk_spin_button_new(adj, 1.0, 0);
193 gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(hex_size_entry), TRUE);
194 gtk_table_attach(GTK_TABLE(table), hex_size_entry, 1,2, 5,6,
195 GTK_FILL|GTK_EXPAND, GTK_FILL, 0, 0);
196 gtk_widget_show(hex_size_entry);
198 label = gtk_label_new(_("Grid"));
199 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), table, label);
200 gtk_widget_show(table);
201 gtk_widget_show(label);
203 /* The background page */
204 table = gtk_table_new(1,2, FALSE);
205 gtk_container_set_border_width(GTK_CONTAINER(table), 2);
206 gtk_table_set_row_spacings(GTK_TABLE(table), 1);
207 gtk_table_set_col_spacings(GTK_TABLE(table), 2);
209 label = gtk_label_new(_("Background"));
210 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
211 gtk_table_attach(GTK_TABLE(table), label, 0,1, 0,1,
212 GTK_FILL, GTK_FILL, 0, 0);
213 gtk_widget_show(label);
215 bg_colour = dia_color_selector_new();
216 gtk_table_attach(GTK_TABLE(table), bg_colour, 1,2, 0,1,
217 GTK_FILL|GTK_EXPAND, GTK_FILL, 0, 0);
218 gtk_widget_show(bg_colour);
220 label = gtk_label_new(_("Grid Lines"));
221 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
222 gtk_table_attach(GTK_TABLE(table), label, 0,1, 1,2,
223 GTK_FILL, GTK_FILL, 0, 0);
224 gtk_widget_show(label);
226 grid_colour = dia_color_selector_new();
227 gtk_table_attach(GTK_TABLE(table), grid_colour, 1,2, 1,2,
228 GTK_FILL|GTK_EXPAND, GTK_FILL, 0, 0);
229 gtk_widget_show(grid_colour);
231 label = gtk_label_new(_("Page Breaks"));
232 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
233 gtk_table_attach(GTK_TABLE(table), label, 0,1, 2,3,
234 GTK_FILL, GTK_FILL, 0, 0);
235 gtk_widget_show(label);
237 pagebreak_colour = dia_color_selector_new();
238 gtk_table_attach(GTK_TABLE(table), pagebreak_colour, 1,2, 2,3,
239 GTK_FILL|GTK_EXPAND, GTK_FILL, 0, 0);
240 gtk_widget_show(pagebreak_colour);
242 label = gtk_label_new(_("Colors"));
243 gtk_notebook_append_page(GTK_NOTEBOOK(notebook), table, label);
244 gtk_widget_show(table);
245 gtk_widget_show(label);
248 /* diagram_properties_retrieve
249 * Retrieves properties of a diagram *dia and sets the values in the
250 * diagram properties dialog.
252 static void
253 diagram_properties_retrieve(Diagram *dia)
255 gchar *title;
256 gchar *name = dia ? diagram_get_name(dia) : "?";
258 g_return_if_fail(dia != NULL);
259 if (!dialog)
260 return;
262 /* Can we be sure that the filename is the 'proper title'? */
263 title = g_strdup_printf(_("Diagram Properties: %s"), name ? name : "??");
264 gtk_window_set_title(GTK_WINDOW(dialog), title);
265 g_free(name);
266 g_free(title);
267 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dynamic_check),
268 dia->grid.dynamic);
269 gtk_spin_button_set_value(GTK_SPIN_BUTTON(width_x_entry),
270 dia->grid.width_x);
271 gtk_spin_button_set_value(GTK_SPIN_BUTTON(width_y_entry),
272 dia->grid.width_y);
273 gtk_spin_button_set_value(GTK_SPIN_BUTTON(visible_x_entry),
274 dia->grid.visible_x);
275 gtk_spin_button_set_value(GTK_SPIN_BUTTON(visible_y_entry),
276 dia->grid.visible_y);
277 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(hex_check),
278 dia->grid.hex);
279 gtk_spin_button_set_value(GTK_SPIN_BUTTON(hex_size_entry),
280 dia->grid.hex_size);
282 dia_color_selector_set_color(bg_colour,
283 &dia->data->bg_color);
284 dia_color_selector_set_color(grid_colour,
285 &dia->grid.colour);
286 dia_color_selector_set_color(pagebreak_colour,
287 &dia->pagebreak_color);
289 diagram_properties_update_sensitivity(GTK_TOGGLE_BUTTON(dynamic_check), dia);
293 void
294 diagram_properties_show(Diagram *dia)
296 if (dialog) {
297 /* This makes the dialog a child of the newer diagram */
298 gtk_widget_destroy(dialog);
299 dialog = NULL;
302 create_diagram_properties_dialog(dia);
304 diagram_properties_retrieve(dia);
306 gtk_window_set_transient_for(GTK_WINDOW(dialog),
307 GTK_WINDOW (ddisplay_active()->shell));
308 gtk_widget_show(dialog);
311 static void
312 diagram_properties_respond(GtkWidget *widget,
313 gint response_id,
314 gpointer user_data)
316 Diagram *active_diagram = ddisplay_active_diagram();
318 if (response_id == GTK_RESPONSE_OK ||
319 response_id == GTK_RESPONSE_APPLY) {
320 if (active_diagram) {
321 active_diagram->grid.dynamic =
322 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dynamic_check));
323 active_diagram->grid.width_x =
324 gtk_spin_button_get_value(GTK_SPIN_BUTTON(width_x_entry));
325 active_diagram->grid.width_y =
326 gtk_spin_button_get_value(GTK_SPIN_BUTTON(width_y_entry));
327 active_diagram->grid.visible_x =
328 gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(visible_x_entry));
329 active_diagram->grid.visible_y =
330 gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(visible_y_entry));
331 active_diagram->grid.hex =
332 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(hex_check));
333 active_diagram->grid.hex_size =
334 gtk_spin_button_get_value(GTK_SPIN_BUTTON(hex_size_entry));
335 dia_color_selector_get_color(bg_colour,
336 &active_diagram->data->bg_color);
337 dia_color_selector_get_color(grid_colour,
338 &active_diagram->grid.colour);
339 dia_color_selector_get_color(pagebreak_colour,
340 &active_diagram->pagebreak_color);
341 diagram_add_update_all(active_diagram);
342 diagram_flush(active_diagram);
343 diagram_set_modified(active_diagram, TRUE);
346 if (response_id != GTK_RESPONSE_APPLY)
347 gtk_widget_hide(dialog);
350 /* diagram_properties_set_diagram
351 * Called when the active diagram is changed. It updates the contents
352 * of the diagram properties dialog
354 void
355 diagram_properties_set_diagram(Diagram *dia)
357 if (dialog && dia != NULL)
359 diagram_properties_retrieve(dia);