1 // -*- C++ -*-
2
3 /*
4 * GChemPaint library
5 * text-object.cc
6 *
7 * Copyright (C) 2002-2009 Jean Bréfort <jean.brefort@normalesup.org>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 3 of the
12 * License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
22 * USA
23 */
24
25 #include "config.h"
26 #include "text-object.h"
27 #include "text-editor.h"
28 #include "document.h"
29 #include "window.h"
30 #include <gcu/objprops.h>
31 #include <gcu/xml-utils.h>
32 #include <gccv/text.h>
33 #include <cstring>
34
35 using namespace gcu;
36 using namespace std;
37
38 namespace gcp {
39
40 extern xmlDocPtr pXmlDoc;
41
TextObject(TypeId Type)42 TextObject::TextObject (TypeId Type): Object (Type), gccv::TextClient (),
43 m_TextItem (NULL)
44 {
45 m_x = 0.;
46 m_y = 0.;
47 m_ascent = 0;
48 m_length = 5;
49 m_height = 15;
50 m_InsertOffset = -2;
51 m_bLoading = false;
52 m_RealSave = true;
53 m_StartSel = m_EndSel = 0;
54 m_Editor = NULL;
55 }
56
TextObject(double x,double y,TypeId Type)57 TextObject::TextObject (double x, double y, TypeId Type): Object (Type),
58 gccv::TextClient (),
59 m_TextItem (NULL)
60 {
61 m_x = x;
62 m_y = y;
63 m_ascent = 0;
64 m_length = 5;
65 m_height = 15;
66 m_InsertOffset = -2;
67 m_bLoading = false;
68 m_RealSave = true;
69 m_StartSel = m_EndSel = 0;
70 m_Editor = NULL;
71 }
72
~TextObject()73 TextObject::~TextObject ()
74 {
75 }
76
SaveSelected()77 xmlNodePtr TextObject::SaveSelected ()
78 {
79 m_RealSave = false;
80 xmlNodePtr node = Save (pXmlDoc);
81 m_RealSave = true;
82 if (!node)
83 return NULL;
84 gchar* buf = g_strdup_printf ("%u", m_StartSel);
85 xmlNewProp (node, (xmlChar*) "start-sel", (xmlChar*) buf);
86 g_free (buf);
87 buf = g_strdup_printf ("%u", m_EndSel);
88 xmlNewProp (node, (xmlChar*) "end-sel", (xmlChar*) buf);
89 g_free (buf);
90 return node;
91 }
92
LoadSelected(xmlNodePtr node)93 void TextObject::LoadSelected (xmlNodePtr node)
94 {
95 Load (node);
96 unsigned start = 0, end = 0;
97 char *buf = reinterpret_cast <char*> (xmlGetProp(node, (xmlChar*) "start-sel"));
98 if (buf) {
99 start = strtoul (buf, NULL, 10);
100 xmlFree (buf);
101 }
102 buf = reinterpret_cast <char*> (xmlGetProp(node, (xmlChar*) "end-sel"));
103 if (buf) {
104 end = strtoul (buf, NULL, 10);
105 xmlFree (buf);
106 }
107 reinterpret_cast <gccv::Text *> (m_TextItem)->SetSelectionBounds (start, end);
108 OnChanged (false);
109 }
110
SaveNode(xmlDocPtr xml,xmlNodePtr node) const111 bool TextObject::SaveNode (xmlDocPtr xml, xmlNodePtr node) const
112 {
113 SaveId (node);
114 return WritePosition (xml, node, NULL, m_x, m_y);
115 }
116
Load(xmlNodePtr node)117 bool TextObject::Load (xmlNodePtr node)
118 {
119 char* tmp, *endptr;
120 bool result;
121 tmp = (char*) xmlGetProp (node, (xmlChar*) "id");
122 if (tmp) {
123 SetId (tmp);
124 xmlFree (tmp);
125 }
126 if (ReadPosition (node, NULL, &m_x, &m_y))
127 return true;
128 tmp = (char*) xmlGetProp(node, (xmlChar*) "x");
129 if (!tmp)
130 return false;
131 m_x = strtod (tmp, &endptr);
132 result = *endptr;
133 xmlFree (tmp);
134 if (result)
135 return false;
136 tmp = (char*) xmlGetProp (node, (xmlChar*) "y");
137 if (!tmp)
138 return false;
139 m_y = strtod (tmp, &endptr);
140 result = *endptr;
141 xmlFree (tmp);
142 if (result)
143 return false;
144 return true;
145 }
146
Move(double x,double y,G_GNUC_UNUSED double z)147 void TextObject::Move (double x, double y, G_GNUC_UNUSED double z)
148 {
149 m_x += x;
150 m_y += y;
151 }
152
GetProperty(unsigned property) const153 string TextObject::GetProperty (unsigned property) const
154 {
155 switch (property) {
156 case GCU_PROP_TEXT_TEXT:
157 return m_buf;
158 default:
159 return Object::GetProperty (property);
160 }
161 }
162
SelectionChanged(unsigned start,unsigned cur)163 void TextObject::SelectionChanged (unsigned start, unsigned cur)
164 {
165 if (start <= cur) {
166 m_StartSel = start;
167 m_EndSel = cur;
168 } else {
169 m_EndSel = start;
170 m_StartSel = cur;
171 }
172 bool activate = m_EndSel > m_StartSel;
173 Document* Doc = dynamic_cast<Document*> (GetDocument ());
174 Window *Win = static_cast < Window * > (Doc->GetWindow ());
175 Win->ActivateActionWidget ("/MainMenu/EditMenu/Erase", activate);
176 Win->ActivateActionWidget ("/MainMenu/EditMenu/Copy", activate);
177 Win->ActivateActionWidget ("/MainMenu/EditMenu/Cut", activate);
178 if (m_Editor)
179 m_Editor->SelectionChanged ();
180 }
181
TextChanged(G_GNUC_UNUSED unsigned pos)182 void TextObject::TextChanged (G_GNUC_UNUSED unsigned pos)
183 {
184 OnChanged (true);
185 }
186
187 } // namespace gcp
188