1 /* Dia -- a diagram creation/manipulation program -*- c -*-
2 * Copyright (C) 1998 Alexander Larsson
4 * Property system for dia objects/shapes.
5 * Copyright (C) 2000 James Henstridge
6 * Copyright (C) 2001 Cyrille Chepelov
7 * Major restructuration done in August 2001 by C. Chepelov
9 * Property types for "textual" types (strings, texts, whatever).
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #define GTK_ENABLE_BROKEN /* GtkText */
31 #undef GTK_DISABLE_DEPRECATED /* for GtkText */
33 #define WIDGET GtkWidget
35 #include "properties.h"
36 #include "propinternals.h"
39 /*****************************************************/
40 /* The STRING, FILE and MULTISTRING property types. */
41 /*****************************************************/
43 static StringProperty
*
44 stringprop_new(const PropDescription
*pdesc
, PropDescToPropPredicate reason
)
46 StringProperty
*prop
= g_new0(StringProperty
,1);
47 initialize_property(&prop
->common
,pdesc
,reason
);
48 prop
->string_data
= NULL
;
53 static StringProperty
*
54 multistringprop_new(const PropDescription
*pdesc
,
55 PropDescToPropPredicate reason
)
57 StringProperty
*prop
= g_new0(StringProperty
,1);
58 initialize_property(&prop
->common
,pdesc
,reason
);
59 prop
->string_data
= NULL
;
60 prop
->num_lines
= GPOINTER_TO_INT(pdesc
->extra_data
);
64 static StringProperty
*
65 stringprop_copy(StringProperty
*src
)
67 StringProperty
*prop
=
68 (StringProperty
*)src
->common
.ops
->new_prop(src
->common
.descr
,
70 copy_init_property(&prop
->common
,&src
->common
);
72 prop
->string_data
= g_strdup(src
->string_data
);
74 prop
->string_data
= NULL
;
75 prop
->num_lines
= src
->num_lines
;
80 stringprop_free(StringProperty
*prop
)
82 g_free(prop
->string_data
);
87 stringprop_get_widget(StringProperty
*prop
, PropDialog
*dialog
)
89 GtkWidget
*ret
= gtk_entry_new();
90 prophandler_connect(&prop
->common
,GTK_OBJECT(ret
),"changed");
95 stringprop_reset_widget(StringProperty
*prop
, WIDGET
*widget
)
97 gtk_entry_set_text(GTK_ENTRY(widget
),
98 prop
->string_data
? prop
->string_data
: "");
102 stringprop_set_from_widget(StringProperty
*prop
, WIDGET
*widget
)
104 g_free(prop
->string_data
);
106 g_strdup (gtk_entry_get_text (GTK_ENTRY(widget
)));
110 multistringprop_get_widget(StringProperty
*prop
, PropDialog
*dialog
)
112 GtkWidget
*ret
= gtk_text_new(NULL
,NULL
);
113 gtk_text_set_editable(GTK_TEXT(ret
),TRUE
);
114 prophandler_connect(&prop
->common
,GTK_OBJECT(ret
),"changed");
119 multistringprop_reset_widget(StringProperty
*prop
, WIDGET
*widget
)
121 gtk_text_insert(GTK_TEXT(widget
), NULL
, NULL
, NULL
,
122 prop
->string_data
? prop
->string_data
: "", -1);
126 fileprop_get_widget(StringProperty
*prop
, PropDialog
*dialog
)
128 GtkWidget
*ret
= dia_file_selector_new();
129 prophandler_connect(&prop
->common
,GTK_OBJECT(ret
),"FIXME");
134 fileprop_reset_widget(StringProperty
*prop
, WIDGET
*widget
)
136 dia_file_selector_set_file(DIAFILESELECTOR(widget
),prop
->string_data
);
140 fileprop_set_from_widget(StringProperty
*prop
, WIDGET
*widget
)
142 g_free(prop
->string_data
);
144 g_strdup(dia_file_selector_get_file(DIAFILESELECTOR(widget
)));
148 stringprop_load(StringProperty
*prop
, AttributeNode attr
, DataNode data
)
150 g_free(prop
->string_data
);
151 prop
->string_data
= data_string(data
);
155 stringprop_save(StringProperty
*prop
, AttributeNode attr
)
157 data_add_string(attr
, prop
->string_data
);
161 stringprop_get_from_offset(StringProperty
*prop
,
162 void *base
, guint offset
, guint offset2
)
164 g_free(prop
->string_data
);
165 prop
->string_data
= g_strdup(struct_member(base
,offset
,gchar
*));
169 stringprop_set_from_offset(StringProperty
*prop
,
170 void *base
, guint offset
, guint offset2
)
172 g_free(struct_member(base
,offset
,gchar
*));
173 struct_member(base
,offset
,gchar
*) = g_strdup(prop
->string_data
);
176 static const PropertyOps stringprop_ops
= {
177 (PropertyType_New
) stringprop_new
,
178 (PropertyType_Free
) stringprop_free
,
179 (PropertyType_Copy
) stringprop_copy
,
180 (PropertyType_Load
) stringprop_load
,
181 (PropertyType_Save
) stringprop_save
,
182 (PropertyType_GetWidget
) stringprop_get_widget
,
183 (PropertyType_ResetWidget
) stringprop_reset_widget
,
184 (PropertyType_SetFromWidget
) stringprop_set_from_widget
,
186 (PropertyType_CanMerge
) noopprop_can_merge
,
187 (PropertyType_GetFromOffset
) stringprop_get_from_offset
,
188 (PropertyType_SetFromOffset
) stringprop_set_from_offset
191 static const PropertyOps multistringprop_ops
= {
192 (PropertyType_New
) multistringprop_new
,
193 (PropertyType_Free
) stringprop_free
,
194 (PropertyType_Copy
) stringprop_copy
,
195 (PropertyType_Load
) stringprop_load
,
196 (PropertyType_Save
) stringprop_save
,
197 (PropertyType_GetWidget
) multistringprop_get_widget
,
198 (PropertyType_ResetWidget
) multistringprop_reset_widget
,
199 (PropertyType_SetFromWidget
) stringprop_set_from_widget
,
201 (PropertyType_CanMerge
) noopprop_can_merge
,
202 (PropertyType_GetFromOffset
) stringprop_get_from_offset
,
203 (PropertyType_SetFromOffset
) stringprop_set_from_offset
206 static const PropertyOps fileprop_ops
= {
207 (PropertyType_New
) stringprop_new
,
208 (PropertyType_Free
) stringprop_free
,
209 (PropertyType_Copy
) stringprop_copy
,
210 (PropertyType_Load
) stringprop_load
,
211 (PropertyType_Save
) stringprop_save
,
212 (PropertyType_GetWidget
) fileprop_get_widget
,
213 (PropertyType_ResetWidget
) fileprop_reset_widget
,
214 (PropertyType_SetFromWidget
) fileprop_set_from_widget
,
216 (PropertyType_CanMerge
) noopprop_can_merge
,
217 (PropertyType_GetFromOffset
) stringprop_get_from_offset
,
218 (PropertyType_SetFromOffset
) stringprop_set_from_offset
221 /***************************/
222 /* The TEXT property type. */
223 /***************************/
225 static TextProperty
*
226 textprop_new(const PropDescription
*pdesc
, PropDescToPropPredicate reason
)
228 TextProperty
*prop
= g_new0(TextProperty
,1);
229 initialize_property(&prop
->common
,pdesc
,reason
);
230 prop
->text_data
= NULL
;
234 static TextProperty
*
235 textprop_copy(TextProperty
*src
)
238 (TextProperty
*)src
->common
.ops
->new_prop(src
->common
.descr
,
240 copy_init_property(&prop
->common
,&src
->common
);
242 prop
->text_data
= g_strdup(src
->text_data
);
244 prop
->text_data
= NULL
;
249 textprop_free(TextProperty
*prop
)
251 g_free(prop
->text_data
);
256 textprop_load(TextProperty
*prop
, AttributeNode attr
, DataNode data
)
259 g_free(prop
->text_data
);
260 text
= data_text(data
);
261 text_get_attributes(text
,&prop
->attr
);
262 prop
->text_data
= text_get_string_copy(text
);
267 textprop_save(TextProperty
*prop
, AttributeNode attr
)
269 Text
*text
= new_text(prop
->text_data
,
272 &prop
->attr
.position
,
274 prop
->attr
.alignment
);
275 data_add_text(attr
,text
);
280 textprop_get_from_offset(TextProperty
*prop
,
281 void *base
, guint offset
, guint offset2
)
283 Text
*text
= struct_member(base
,offset
,Text
*);
284 g_free(prop
->text_data
);
285 prop
->text_data
= text_get_string_copy(text
);
286 text_get_attributes(text
,&prop
->attr
);
290 textprop_set_from_offset(TextProperty
*prop
,
291 void *base
, guint offset
, guint offset2
)
293 Text
*text
= struct_member(base
,offset
,Text
*);
294 text_set_string(text
,prop
->text_data
);
295 text_set_attributes(text
,&prop
->attr
);
298 static const PropertyOps textprop_ops
= {
299 (PropertyType_New
) textprop_new
,
300 (PropertyType_Free
) textprop_free
,
301 (PropertyType_Copy
) textprop_copy
,
302 (PropertyType_Load
) textprop_load
,
303 (PropertyType_Save
) textprop_save
,
304 (PropertyType_GetWidget
) noopprop_get_widget
,
305 (PropertyType_ResetWidget
) noopprop_reset_widget
,
306 (PropertyType_SetFromWidget
) noopprop_set_from_widget
,
308 (PropertyType_CanMerge
) noopprop_can_merge
,
309 (PropertyType_GetFromOffset
) textprop_get_from_offset
,
310 (PropertyType_SetFromOffset
) textprop_set_from_offset
313 /* ************************************************************** */
316 prop_text_register(void)
318 prop_type_register(PROP_TYPE_STRING
,&stringprop_ops
);
319 prop_type_register(PROP_TYPE_MULTISTRING
,&multistringprop_ops
);
320 prop_type_register(PROP_TYPE_FILE
,&fileprop_ops
);
321 prop_type_register(PROP_TYPE_TEXT
,&textprop_ops
);