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.
30 #include "pagesetup.h"
32 #include "diapagelayout.h"
34 typedef struct _PageSetup PageSetup
;
42 static void pagesetup_changed (GtkWidget
*wid
, PageSetup
*ps
);
43 static void pagesetup_apply (GtkWidget
*wid
, PageSetup
*ps
);
46 pagesetup_respond(GtkWidget
*widget
,
50 PageSetup
*ps
= (PageSetup
*)data
;
52 if ( response_id
== GTK_RESPONSE_APPLY
53 || response_id
== GTK_RESPONSE_OK
) {
55 pagesetup_apply(widget
, ps
);
58 if (response_id
!= GTK_RESPONSE_APPLY
)
59 gtk_widget_destroy(ps
->window
);
65 create_page_setup_dlg(Diagram
*dia
)
70 ps
= g_new(PageSetup
, 1);
72 ps
->window
= gtk_dialog_new_with_buttons(
74 GTK_WINDOW (ddisplay_active()->shell
),
75 GTK_DIALOG_DESTROY_WITH_PARENT
,
76 GTK_STOCK_CLOSE
, GTK_RESPONSE_CLOSE
,
77 GTK_STOCK_APPLY
, GTK_RESPONSE_APPLY
,
78 GTK_STOCK_OK
, GTK_RESPONSE_OK
,
80 gtk_dialog_set_default_response (GTK_DIALOG(ps
->window
), GTK_RESPONSE_OK
);
81 vbox
= GTK_DIALOG(ps
->window
)->vbox
;
82 gtk_dialog_set_response_sensitive(GTK_DIALOG(ps
->window
), GTK_RESPONSE_APPLY
, FALSE
);
85 g_signal_connect(G_OBJECT (ps
->window
), "response",
86 G_CALLBACK (pagesetup_respond
), ps
);
88 /* destroy ps when the dialog is closed */
89 gtk_object_set_data_full(GTK_OBJECT(ps
->window
), "pagesetup", ps
,
90 (GtkDestroyNotify
)g_free
);
92 ps
->paper
= dia_page_layout_new();
93 dia_page_layout_set_paper(DIA_PAGE_LAYOUT(ps
->paper
), dia
->data
->paper
.name
);
94 dia_page_layout_set_margins(DIA_PAGE_LAYOUT(ps
->paper
),
95 dia
->data
->paper
.tmargin
,
96 dia
->data
->paper
.bmargin
,
97 dia
->data
->paper
.lmargin
,
98 dia
->data
->paper
.rmargin
);
99 dia_page_layout_set_orientation(DIA_PAGE_LAYOUT(ps
->paper
),
100 dia
->data
->paper
.is_portrait
? DIA_PAGE_ORIENT_PORTRAIT
:
101 DIA_PAGE_ORIENT_LANDSCAPE
);
102 dia_page_layout_set_scaling(DIA_PAGE_LAYOUT(ps
->paper
),
103 dia
->data
->paper
.scaling
);
104 dia_page_layout_set_fitto(DIA_PAGE_LAYOUT(ps
->paper
),
105 dia
->data
->paper
.fitto
);
106 dia_page_layout_set_fit_dims(DIA_PAGE_LAYOUT(ps
->paper
),
107 dia
->data
->paper
.fitwidth
,
108 dia
->data
->paper
.fitheight
);
110 gtk_container_set_border_width(GTK_CONTAINER(ps
->paper
), 5);
111 gtk_box_pack_start(GTK_BOX(vbox
), ps
->paper
, TRUE
, TRUE
, 0);
112 gtk_widget_show(ps
->paper
);
114 g_signal_connect(GTK_OBJECT(ps
->paper
), "changed",
115 G_CALLBACK(pagesetup_changed
), ps
);
117 gtk_widget_show(ps
->window
);
121 pagesetup_changed(GtkWidget
*wid
, PageSetup
*ps
)
123 gfloat dwidth
, dheight
;
124 gfloat pwidth
, pheight
;
125 gfloat xscale
, yscale
;
126 gint fitw
= 0, fith
= 0;
129 /* set sensitivity on apply button */
130 gtk_dialog_set_response_sensitive(GTK_DIALOG(ps
->window
), GTK_RESPONSE_APPLY
, TRUE
);
133 dwidth
= ps
->dia
->data
->extents
.right
- ps
->dia
->data
->extents
.left
;
134 dheight
= ps
->dia
->data
->extents
.bottom
- ps
->dia
->data
->extents
.top
;
136 if (dwidth
<= 0.0 || dheight
<= 0.0)
139 DIA_PAGE_LAYOUT(ps
->paper
)->block_changed
= TRUE
;
141 cur_scaling
= dia_page_layout_get_scaling(DIA_PAGE_LAYOUT(ps
->paper
));
142 dia_page_layout_get_effective_area(DIA_PAGE_LAYOUT(ps
->paper
),
144 pwidth
*= cur_scaling
;
145 pheight
*= cur_scaling
;
147 if (dia_page_layout_get_fitto(DIA_PAGE_LAYOUT(ps
->paper
))) {
148 dia_page_layout_get_fit_dims(DIA_PAGE_LAYOUT(ps
->paper
), &fitw
, &fith
);
149 xscale
= fitw
* pwidth
/ dwidth
;
150 yscale
= fith
* pheight
/ dheight
;
151 dia_page_layout_set_scaling(DIA_PAGE_LAYOUT(ps
->paper
),
152 MIN(xscale
, yscale
));
154 fitw
= ceil(dwidth
* cur_scaling
/ pwidth
);
155 fith
= ceil(dheight
* cur_scaling
/ pheight
);
156 dia_page_layout_set_fit_dims(DIA_PAGE_LAYOUT(ps
->paper
), fitw
, fith
);
159 DIA_PAGE_LAYOUT(ps
->paper
)->block_changed
= FALSE
;
162 /* This affects the actual setup. It should only be called when
163 something has in fact changed.
166 pagesetup_apply(GtkWidget
*wid
, PageSetup
*ps
)
168 g_free(ps
->dia
->data
->paper
.name
);
169 ps
->dia
->data
->paper
.name
=
170 g_strdup(dia_page_layout_get_paper(DIA_PAGE_LAYOUT(ps
->paper
)));
172 dia_page_layout_get_margins(DIA_PAGE_LAYOUT(ps
->paper
),
173 &ps
->dia
->data
->paper
.tmargin
,
174 &ps
->dia
->data
->paper
.bmargin
,
175 &ps
->dia
->data
->paper
.lmargin
,
176 &ps
->dia
->data
->paper
.rmargin
);
178 ps
->dia
->data
->paper
.is_portrait
=
179 dia_page_layout_get_orientation(DIA_PAGE_LAYOUT(ps
->paper
)) ==
180 DIA_PAGE_ORIENT_PORTRAIT
;
181 ps
->dia
->data
->paper
.scaling
=
182 dia_page_layout_get_scaling(DIA_PAGE_LAYOUT(ps
->paper
));
184 ps
->dia
->data
->paper
.fitto
= dia_page_layout_get_fitto(
185 DIA_PAGE_LAYOUT(ps
->paper
));
186 dia_page_layout_get_fit_dims(DIA_PAGE_LAYOUT(ps
->paper
),
187 &ps
->dia
->data
->paper
.fitwidth
,
188 &ps
->dia
->data
->paper
.fitheight
);
190 dia_page_layout_get_effective_area(DIA_PAGE_LAYOUT(ps
->paper
),
191 &ps
->dia
->data
->paper
.width
,
192 &ps
->dia
->data
->paper
.height
);
194 /* set sensitivity on apply button */
195 gtk_dialog_set_response_sensitive(GTK_DIALOG(ps
->window
), GTK_RESPONSE_APPLY
, FALSE
);
198 /* update diagram -- this is needed to reposition page boundaries */
199 diagram_set_modified(ps
->dia
, TRUE
);
200 diagram_add_update_all(ps
->dia
);
201 diagram_flush(ps
->dia
);