1 /* Dia -- an diagram creation/manipulation program
2 * Copyright (C) 1998, 1999 Alexander Larsson
4 * pagesetup.[ch] -- code for the page setup dialog
5 * Copyright (C) 1999 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.
34 #include "pagesetup.h"
36 #include "diapagelayout.h"
38 typedef struct _PageSetup PageSetup
;
43 GtkWidget
*ok
, *apply
, *close
;
46 static void pagesetup_changed (GtkWidget
*wid
, PageSetup
*ps
);
47 static void pagesetup_ok (GtkWidget
*wid
, PageSetup
*ps
);
48 static void pagesetup_apply (GtkWidget
*wid
, PageSetup
*ps
);
49 static void pagesetup_close (GtkWidget
*wid
, PageSetup
*ps
);
52 create_page_setup_dlg(Diagram
*dia
)
57 ps
= g_new(PageSetup
, 1);
60 ps
->window
= gnome_dialog_new(_("Page Setup"), GNOME_STOCK_BUTTON_OK
,
61 GNOME_STOCK_BUTTON_APPLY
,
62 GNOME_STOCK_BUTTON_CLOSE
, NULL
);
63 vbox
= GNOME_DIALOG(ps
->window
)->vbox
;
64 gnome_dialog_set_sensitive(GNOME_DIALOG(ps
->window
), 1, FALSE
);
65 gnome_dialog_set_default(GNOME_DIALOG(ps
->window
), 2);
67 ps
->window
= gtk_dialog_new();
68 gtk_window_set_title(GTK_WINDOW(ps
->window
), _("Page Setup"));
69 vbox
= GTK_DIALOG(ps
->window
)->vbox
;
72 ps
->ok
= gtk_button_new_with_label(_("OK"));
73 GTK_WIDGET_SET_FLAGS(ps
->ok
, GTK_CAN_DEFAULT
);
74 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(ps
->window
)->action_area
),
76 gtk_widget_show(ps
->ok
);
77 ps
->apply
= gtk_button_new_with_label(_("Apply"));
78 GTK_WIDGET_SET_FLAGS(ps
->apply
, GTK_CAN_DEFAULT
);
79 gtk_widget_set_sensitive(ps
->apply
, FALSE
);
80 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(ps
->window
)->action_area
),
82 gtk_widget_show(ps
->apply
);
83 ps
->close
= gtk_button_new_with_label(_("Close"));
84 GTK_WIDGET_SET_FLAGS(ps
->close
, GTK_CAN_DEFAULT
);
85 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(ps
->window
)->action_area
),
87 gtk_widget_grab_default(ps
->close
);
88 gtk_widget_show(ps
->close
);
91 /* destroy ps when the dialog is closed */
92 gtk_object_set_data_full(GTK_OBJECT(ps
->window
), "pagesetup", ps
,
93 (GtkDestroyNotify
)g_free
);
95 ps
->paper
= dia_page_layout_new();
96 dia_page_layout_set_paper(DIA_PAGE_LAYOUT(ps
->paper
), dia
->data
->paper
.name
);
97 dia_page_layout_set_margins(DIA_PAGE_LAYOUT(ps
->paper
),
98 dia
->data
->paper
.tmargin
,
99 dia
->data
->paper
.bmargin
,
100 dia
->data
->paper
.lmargin
,
101 dia
->data
->paper
.rmargin
);
102 dia_page_layout_set_orientation(DIA_PAGE_LAYOUT(ps
->paper
),
103 dia
->data
->paper
.is_portrait
? DIA_PAGE_ORIENT_PORTRAIT
:
104 DIA_PAGE_ORIENT_LANDSCAPE
);
105 dia_page_layout_set_scaling(DIA_PAGE_LAYOUT(ps
->paper
),
106 dia
->data
->paper
.scaling
);
107 dia_page_layout_set_fitto(DIA_PAGE_LAYOUT(ps
->paper
),
108 dia
->data
->paper
.fitto
);
109 dia_page_layout_set_fit_dims(DIA_PAGE_LAYOUT(ps
->paper
),
110 dia
->data
->paper
.fitwidth
,
111 dia
->data
->paper
.fitheight
);
113 gtk_container_set_border_width(GTK_CONTAINER(ps
->paper
), 5);
114 gtk_box_pack_start(GTK_BOX(vbox
), ps
->paper
, TRUE
, TRUE
, 0);
115 gtk_widget_show(ps
->paper
);
117 gtk_signal_connect(GTK_OBJECT(ps
->paper
), "changed",
118 GTK_SIGNAL_FUNC(pagesetup_changed
), ps
);
120 gnome_dialog_button_connect(GNOME_DIALOG(ps
->window
), 0,
121 GTK_SIGNAL_FUNC(pagesetup_ok
), ps
);
122 gnome_dialog_button_connect(GNOME_DIALOG(ps
->window
), 1,
123 GTK_SIGNAL_FUNC(pagesetup_apply
), ps
);
124 gnome_dialog_button_connect(GNOME_DIALOG(ps
->window
), 2,
125 GTK_SIGNAL_FUNC(pagesetup_close
), ps
);
127 gtk_signal_connect(GTK_OBJECT(ps
->ok
), "clicked",
128 GTK_SIGNAL_FUNC(pagesetup_ok
), ps
);
129 gtk_signal_connect(GTK_OBJECT(ps
->apply
), "clicked",
130 GTK_SIGNAL_FUNC(pagesetup_apply
), ps
);
131 gtk_signal_connect(GTK_OBJECT(ps
->close
), "clicked",
132 GTK_SIGNAL_FUNC(pagesetup_close
), ps
);
135 gtk_widget_show(ps
->window
);
139 pagesetup_changed(GtkWidget
*wid
, PageSetup
*ps
)
141 gfloat dwidth
, dheight
;
142 gfloat pwidth
, pheight
;
143 gfloat xscale
, yscale
;
144 gint fitw
= 0, fith
= 0;
147 /* set sensitivity on apply button */
149 gnome_dialog_set_sensitive(GNOME_DIALOG(ps
->window
), 1, TRUE
);
151 gtk_widget_set_sensitive(ps
->apply
, TRUE
);
154 dwidth
= ps
->dia
->data
->extents
.right
- ps
->dia
->data
->extents
.left
;
155 dheight
= ps
->dia
->data
->extents
.bottom
- ps
->dia
->data
->extents
.top
;
157 if (dwidth
<= 0.0 || dheight
<= 0.0)
160 DIA_PAGE_LAYOUT(ps
->paper
)->block_changed
= TRUE
;
162 cur_scaling
= dia_page_layout_get_scaling(DIA_PAGE_LAYOUT(ps
->paper
));
163 dia_page_layout_get_effective_area(DIA_PAGE_LAYOUT(ps
->paper
),
165 pwidth
*= cur_scaling
;
166 pheight
*= cur_scaling
;
168 if (dia_page_layout_get_fitto(DIA_PAGE_LAYOUT(ps
->paper
))) {
169 dia_page_layout_get_fit_dims(DIA_PAGE_LAYOUT(ps
->paper
), &fitw
, &fith
);
170 xscale
= fitw
* pwidth
/ dwidth
;
171 yscale
= fith
* pheight
/ dheight
;
172 dia_page_layout_set_scaling(DIA_PAGE_LAYOUT(ps
->paper
),
173 MIN(xscale
, yscale
));
175 fitw
= ceil(dwidth
* cur_scaling
/ pwidth
);
176 fith
= ceil(dheight
* cur_scaling
/ pheight
);
177 dia_page_layout_set_fit_dims(DIA_PAGE_LAYOUT(ps
->paper
), fitw
, fith
);
180 DIA_PAGE_LAYOUT(ps
->paper
)->block_changed
= FALSE
;
184 pagesetup_ok(GtkWidget
*wid
, PageSetup
*ps
)
186 pagesetup_apply(wid
, ps
);
187 pagesetup_close(wid
, ps
);
191 pagesetup_apply(GtkWidget
*wid
, PageSetup
*ps
)
193 g_free(ps
->dia
->data
->paper
.name
);
194 ps
->dia
->data
->paper
.name
=
195 g_strdup(dia_page_layout_get_paper(DIA_PAGE_LAYOUT(ps
->paper
)));
196 dia_page_layout_get_margins(DIA_PAGE_LAYOUT(ps
->paper
),
197 &ps
->dia
->data
->paper
.tmargin
,
198 &ps
->dia
->data
->paper
.bmargin
,
199 &ps
->dia
->data
->paper
.lmargin
,
200 &ps
->dia
->data
->paper
.rmargin
);
201 ps
->dia
->data
->paper
.is_portrait
=
202 dia_page_layout_get_orientation(DIA_PAGE_LAYOUT(ps
->paper
)) ==
203 DIA_PAGE_ORIENT_PORTRAIT
;
204 ps
->dia
->data
->paper
.scaling
=
205 dia_page_layout_get_scaling(DIA_PAGE_LAYOUT(ps
->paper
));
207 ps
->dia
->data
->paper
.fitto
= dia_page_layout_get_fitto(
208 DIA_PAGE_LAYOUT(ps
->paper
));
209 dia_page_layout_get_fit_dims(DIA_PAGE_LAYOUT(ps
->paper
),
210 &ps
->dia
->data
->paper
.fitwidth
,
211 &ps
->dia
->data
->paper
.fitheight
);
213 dia_page_layout_get_effective_area(DIA_PAGE_LAYOUT(ps
->paper
),
214 &ps
->dia
->data
->paper
.width
,
215 &ps
->dia
->data
->paper
.height
);
217 /* set sensitivity on apply button */
219 gnome_dialog_set_sensitive(GNOME_DIALOG(ps
->window
), 1, FALSE
);
221 gtk_widget_set_sensitive(ps
->apply
, FALSE
);
223 /* update diagram -- this is needed to reposition page boundaries */
224 diagram_add_update_all(ps
->dia
);
225 diagram_flush(ps
->dia
);
229 pagesetup_close(GtkWidget
*wid
, PageSetup
*ps
)
231 gtk_widget_destroy(ps
->window
);