1 /* Dia -- an diagram creation/manipulation program
2  * Copyright (C) 1998 Alexander Larsson
3  *
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.
8  *
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.
13  *
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.
17  */
18 #include <config.h>
19 
20 #include "attributes.h"
21 #include "intl.h"
22 #include "persistence.h"
23 
24 static Color attributes_foreground = { 0.0f, 0.0f, 0.0f };
25 static Color attributes_background = { 1.0f, 1.0f, 1.0f };
26 
27 static real attributes_default_linewidth = 0.1;
28 
29 static Arrow attributes_start_arrow = { ARROW_NONE,
30 					DEFAULT_ARROW_SIZE,
31 					DEFAULT_ARROW_SIZE };
32 static Arrow attributes_end_arrow = { ARROW_NONE,
33 				      DEFAULT_ARROW_SIZE,
34 				      DEFAULT_ARROW_SIZE };
35 
36 static LineStyle attributes_linestyle = LINESTYLE_SOLID;
37 static real attributes_dash_length = 1.0;
38 
39 static DiaFont *attributes_font = NULL;
40 static real attributes_font_height = 0.8;
41 
42 /** Get the foreground color attribute (lines and text)
43  * @returns The current foreground color as set in the toolbox.
44  */
45 Color
attributes_get_foreground(void)46 attributes_get_foreground(void)
47 {
48   return attributes_foreground;
49 }
50 
51 /** Get the background color attribute (for box background and such)
52  * @returns The current background color as set in the toolbox.
53  */
54 Color
attributes_get_background(void)55 attributes_get_background(void)
56 {
57   return attributes_background;
58 }
59 
60 /** Set the default foreground color for new objects.
61  * @param color A color object to use for foreground color.  This object is
62  * not stored by ths function and can be freed afterwards.
63  */
64 void
attributes_set_foreground(Color * color)65 attributes_set_foreground(Color *color)
66 {
67   attributes_foreground = *color;
68   persistence_set_color("fg_color", color);
69 }
70 
71 /** Set the default background color for new objects.
72  * @param color A color object to use for background color.  This object is
73  * not stored by ths function and can be freed afterwards.
74  */
75 void
attributes_set_background(Color * color)76 attributes_set_background(Color *color)
77 {
78   attributes_background = *color;
79   persistence_set_color("bg_color", color);
80 }
81 
82 /** Swap the current foreground and background colors
83  */
84 void
attributes_swap_fgbg(void)85 attributes_swap_fgbg(void)
86 {
87   Color temp;
88   temp = attributes_foreground;
89   attributes_set_foreground(&attributes_background);
90   attributes_set_background(&temp);
91 }
92 
93 /** Set the default foreground and background colors to black and white.
94  */
95 void
attributes_default_fgbg(void)96 attributes_default_fgbg(void)
97 {
98   attributes_set_foreground(&color_black);
99   attributes_set_background(&color_white);
100 }
101 
102 /** Get the default line width as defined by the toolbox.
103  * @returns A linewidth (0.0 < linewidth < 10.0) that should be used as default
104  *          for new objects.
105  */
106 real
attributes_get_default_linewidth(void)107 attributes_get_default_linewidth(void)
108 {
109   return attributes_default_linewidth;
110 }
111 
112 /** Set the default line width.
113  * @param width The line width (0.0 < linewidth < 10.0) to use for new objects.
114  */
115 void
attributes_set_default_linewidth(real width)116 attributes_set_default_linewidth(real width)
117 {
118   attributes_default_linewidth = width;
119   persistence_set_real("linewidth", width);
120 }
121 
122 /** Get the default arrow type to put at the start (origin) of a connector.
123  * @returns An arrow object for the arrow type defined in the toolbox.
124  */
125 Arrow
attributes_get_default_start_arrow(void)126 attributes_get_default_start_arrow(void)
127 {
128   return attributes_start_arrow;
129 }
130 
131 /** Set the default arrow type that the toolbox will supply for new objects.
132  * @param arrow An arrow object to be used for the start of new connectors.
133  */
134 void
attributes_set_default_start_arrow(Arrow arrow)135 attributes_set_default_start_arrow(Arrow arrow)
136 {
137   attributes_start_arrow = arrow;
138   persistence_set_string("start-arrow-type",
139 			 arrow_get_name_from_type(arrow.type));
140   persistence_set_real("start-arrow-width", arrow.width);
141   persistence_set_real("start-arrow-length", arrow.length);
142 }
143 
144 /** Get the default arrow type to put at the end (target) of a connector.
145  * @returns An arrow object for the arrow type defined in the toolbox.
146  */
147 Arrow
attributes_get_default_end_arrow(void)148 attributes_get_default_end_arrow(void)
149 {
150   return attributes_end_arrow;
151 }
152 
153 /** Set the default arrow type that the toolbox will supply for new objects.
154  * @param arrow An arrow object to be used for the end of new connectors.
155  */
156 void
attributes_set_default_end_arrow(Arrow arrow)157 attributes_set_default_end_arrow(Arrow arrow)
158 {
159   attributes_end_arrow = arrow;
160   persistence_set_string("end-arrow-type",
161 			 arrow_get_name_from_type(arrow.type));
162   persistence_set_real("end-arrow-width", arrow.width);
163   persistence_set_real("end-arrow-length", arrow.length);
164 }
165 
166 /** Get the default line style (dashes, dots etc)
167  * @param style A place to return the style (number of dots and dashes)
168  * @param dash_length A place to return how long a dash is
169  *                    (0.0 < dash_length < ???)
170  * @see dia-enums.h for possible values for style.
171  */
172 void
attributes_get_default_line_style(LineStyle * style,real * dash_length)173 attributes_get_default_line_style(LineStyle *style, real *dash_length)
174 {
175   if (style)
176     *style = attributes_linestyle;
177   if (dash_length)
178     *dash_length = attributes_dash_length;
179 }
180 
181 /** Set the default line style (dashes, dots etc)
182  * @param style The style (number of dots and dashes)
183  * @param dash_length The length of a dash (0.0 < dash_length < ???)
184  * @see dia-enums.h for possible values for style.
185  */
186 void
attributes_set_default_line_style(LineStyle style,real dash_length)187 attributes_set_default_line_style(LineStyle style, real dash_length)
188 {
189   attributes_linestyle = style;
190   attributes_dash_length = dash_length;
191   persistence_set_integer("line-style", style);
192   persistence_set_real("dash-length", dash_length);
193 }
194 
195 /** Get the default font.
196  * Note that there is currently no way for the user to set these.
197  * @param font A place to return the default font description set by the user
198  * @param font_height A place to return the default font height set by the user
199  */
200 void
attributes_get_default_font(DiaFont ** font,real * font_height)201 attributes_get_default_font(DiaFont **font, real *font_height)
202 {
203 	if (!attributes_font) {
204 		attributes_font = dia_font_new_from_style(DIA_FONT_SANS,
205                                               attributes_font_height);
206 	}
207 	if (font)
208 		*font = dia_font_ref(attributes_font);
209 	if (font_height)
210 		*font_height = attributes_font_height;
211 }
212 
213 /** Set the default font.
214  * Note that this is not currently stored persistently.
215  * @param font The font to set as the new default.
216  *        This object will be ref'd by this call.
217  * @param font_height The font height to set as the new default.
218  */
219 void
attributes_set_default_font(DiaFont * font,real font_height)220 attributes_set_default_font(DiaFont *font, real font_height)
221 {
222   if (attributes_font != NULL)
223     dia_font_unref(attributes_font);
224   attributes_font = dia_font_ref(font);
225   attributes_font_height = font_height;
226 }
227