1 /* Dia -- an diagram creation/manipulation program
2 * Copyright (C) 1998 Alexander Larsson
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "linewidth_area.h"
24 #include "attributes.h"
29 # define rint(x) floor ((x) + 0.5)
32 #define BASE_WIDTH 0.05
33 #define PIXELS_BETWEEN_LINES 6
36 #define X_OFFSET(i) (PIXELS_BETWEEN_LINES*(i)+((i)-1)*(i)/2)
38 #define AREA_WIDTH X_OFFSET(NUMLINES+1)
39 #define AREA_HEIGHT 42
41 static void linewidth_create_dialog(void);
43 static int active_linewidth
= 2;
44 static GdkGC
*linewidth_area_gc
= NULL
;
45 static GdkPixmap
*linewidth_area_pixmap
= NULL
;
47 static GtkWidget
*linewidth_area_widget
= NULL
;
48 static GtkWidget
*linewidth_dialog
= NULL
;
49 static GtkWidget
*linewidth_button
= NULL
;
52 linewidth_area_target (int x
, int y
)
56 for (i
=1;i
<=NUMLINES
;i
++) {
58 if ((x
>=x_offs
-PIXELS_BETWEEN_LINES
/2) &&
59 (x
<x_offs
+i
+PIXELS_BETWEEN_LINES
/2))
66 linewidth_area_draw (GtkWidget
*linewidth_area
)
74 if (!linewidth_area_pixmap
) /* we haven't gotten initial expose yet,
75 * no point in drawing anything */
79 if (!linewidth_area_gc
) {
80 linewidth_area_gc
= gdk_gc_new (linewidth_area_pixmap
);
81 gdk_gc_set_line_attributes(linewidth_area_gc
, 1,
87 gdk_window_get_size (linewidth_area_pixmap
, &width
, &height
);
89 win_bg
= &(linewidth_area
->style
->bg
[GTK_STATE_NORMAL
]);
90 line
= color_gdk_black
;
92 gdk_gc_set_foreground (linewidth_area_gc
, win_bg
);
93 gdk_draw_rectangle (linewidth_area_pixmap
, linewidth_area_gc
, 1,
96 gdk_gc_set_foreground (linewidth_area_gc
, &line
);
98 for (i
=0;i
<=NUMLINES
;i
++) {
101 gdk_draw_rectangle (linewidth_area_pixmap
, linewidth_area_gc
, 1,
102 x_offs
, 2, i
, height
-4);
106 if (active_linewidth
!= 0) {
107 gdk_draw_rectangle (linewidth_area_pixmap
, linewidth_area_gc
, 0,
108 X_OFFSET(active_linewidth
)-2, 0,
109 active_linewidth
+4, height
-1);
112 gdk_draw_pixmap (linewidth_area
->window
, linewidth_area_gc
, linewidth_area_pixmap
,
113 0, 0, 0, 0, width
, height
);
117 linewidth_area_events (GtkWidget
*widget
,
120 GdkEventButton
*bevent
;
121 GdkEventConfigure
*cevent
;
127 cevent
= (GdkEventConfigure
*) event
;
128 if (cevent
->width
> 1) {
129 linewidth_area_pixmap
= gdk_pixmap_new (widget
->window
,
135 linewidth_area_draw (widget
);
138 case GDK_BUTTON_PRESS
:
139 bevent
= (GdkEventButton
*) event
;
140 if (bevent
->button
== 1) {
141 target
= linewidth_area_target (bevent
->x
, bevent
->y
);
144 active_linewidth
= target
;
145 linewidth_area_draw(widget
);
146 attributes_set_default_linewidth(BASE_WIDTH
*(target
-1));
151 case GDK_2BUTTON_PRESS
:
152 if (linewidth_dialog
== NULL
)
153 linewidth_create_dialog();
154 gtk_spin_button_set_value(GTK_SPIN_BUTTON(linewidth_button
), attributes_get_default_linewidth());
156 gtk_widget_show(linewidth_dialog
);
168 linewidth_area_create (void)
170 GtkWidget
*linewidth_area
;
172 linewidth_area
= gtk_drawing_area_new ();
173 gtk_drawing_area_size (GTK_DRAWING_AREA (linewidth_area
),
174 AREA_WIDTH
, AREA_HEIGHT
);
175 gtk_widget_set_events (linewidth_area
, GDK_EXPOSURE_MASK
| GDK_BUTTON_PRESS_MASK
);
176 gtk_signal_connect (GTK_OBJECT (linewidth_area
), "event",
177 (GtkSignalFunc
) linewidth_area_events
,
180 attributes_set_default_linewidth(BASE_WIDTH
*active_linewidth
);
182 linewidth_area_widget
= linewidth_area
;
183 return linewidth_area
;
187 linewidth_dialog_ok(GtkWidget
*widget
, gpointer data
) {
188 float newvalue
= gtk_spin_button_get_value_as_float(GTK_SPIN_BUTTON(linewidth_button
));
189 if (fabs(newvalue
/BASE_WIDTH
-rint(newvalue
/BASE_WIDTH
)) > 0.0005 ||
190 (newvalue
/BASE_WIDTH
> NUMLINES
)) {
191 active_linewidth
= 0;
193 active_linewidth
= newvalue
/BASE_WIDTH
+1.0005;
195 linewidth_area_draw(GTK_WIDGET(linewidth_area_widget
));
196 attributes_set_default_linewidth(newvalue
);
197 gtk_widget_hide(linewidth_dialog
);
201 linewidth_dialog_cancel(GtkWidget
*widget
, gpointer data
)
203 gtk_widget_hide(linewidth_dialog
);
207 linewidth_create_dialog()
214 linewidth_dialog
= gtk_dialog_new();
216 gtk_window_set_title (GTK_WINDOW (linewidth_dialog
), _("Line width"));
217 gtk_window_set_wmclass (GTK_WINDOW (linewidth_dialog
),
218 "linewidth_window", "Dia");
219 gtk_window_set_policy (GTK_WINDOW (linewidth_dialog
),
221 gtk_container_set_border_width (GTK_CONTAINER (linewidth_dialog
), 2);
223 hbox
= gtk_hbox_new(FALSE
, 5);
224 label
= gtk_label_new(_("Line width:"));
225 gtk_box_pack_start (GTK_BOX (hbox
), label
, FALSE
, TRUE
, 0);
226 gtk_widget_show (label
);
227 adj
= (GtkAdjustment
*) gtk_adjustment_new(0.1, 0.00, 10.0, 0.01, 0.05, 0.0);
228 linewidth_button
= gtk_spin_button_new(adj
, attributes_get_default_linewidth(), 2);
229 gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(linewidth_button
), TRUE
);
230 gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(linewidth_button
), TRUE
);
231 gtk_box_pack_start(GTK_BOX (hbox
), linewidth_button
, TRUE
, TRUE
, 0);
232 gtk_widget_show (linewidth_button
);
233 gtk_widget_show(hbox
);
234 gtk_box_pack_start (GTK_BOX(GTK_DIALOG (linewidth_dialog
)->vbox
), hbox
, TRUE
, TRUE
, 0);
236 gtk_widget_show (linewidth_button
);
237 button
= gtk_button_new_with_label (_("OK"));
238 GTK_WIDGET_SET_FLAGS (button
, GTK_CAN_DEFAULT
);
239 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (linewidth_dialog
)->action_area
),
240 button
, TRUE
, TRUE
, 0);
241 gtk_signal_connect (GTK_OBJECT (button
), "clicked",
242 GTK_SIGNAL_FUNC(linewidth_dialog_ok
),
244 gtk_widget_show (button
);
246 button
= gtk_button_new_with_label (_("Cancel"));
247 GTK_WIDGET_SET_FLAGS (button
, GTK_CAN_DEFAULT
);
248 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (linewidth_dialog
)->action_area
),
249 button
, TRUE
, TRUE
, 0);
250 gtk_signal_connect (GTK_OBJECT (button
), "clicked",
251 GTK_SIGNAL_FUNC(linewidth_dialog_cancel
),
253 gtk_widget_show (button
);
255 gtk_signal_connect (GTK_OBJECT (linewidth_dialog
), "delete_event",
256 GTK_SIGNAL_FUNC(gtk_widget_hide
), NULL
);