File: dia-canvas-text.c

package info (click to toggle)
dia-newcanvas 0.6.10-5.4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 1,900 kB
  • ctags: 1,102
  • sloc: ansic: 7,865; sh: 7,322; makefile: 254
file content (309 lines) | stat: -rw-r--r-- 8,825 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* dia-canvas-text.c
 * Copyright (C) 2001 Xavier Ordoquy
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "dia-canvas-text.h"
#include "dia-canvas-group.h"

#include <string.h>

#define _(s) (s)
#define sqr(x) ((x)*(x))

/* properties */
enum {
  PROP_NONE,
  PROP_X,
  PROP_Y,
  PROP_TEXT,
  PROP_TEXT_COLOR,
  PROP_TEXT_WIDTH,
  PROP_LAST
};

static void dia_canvas_text_init         (DiaCanvasText *text);
static void dia_canvas_text_class_init   (DiaCanvasTextClass *klass);

static void   dia_canvas_text_set_property (GObject *object,
					    guint property_id,
					    const GValue *value,
					    GParamSpec *pspec);
static void   dia_canvas_text_get_property (GObject *object,
					    guint property_id,
					    GValue *value,
					    GParamSpec *pspec);

static void    dia_canvas_text_real_render    (DiaCanvasItem *item,
					       DiaRenderer *renderer,
					       DiaRectangle *rect);
static gdouble dia_canvas_text_real_distance  (DiaCanvasItem *item,
					       gdouble x, gdouble y);
static void    dia_canvas_text_real_update    (DiaCanvasItem *item);
static void    dia_canvas_text_real_move      (DiaCanvasItem *item,
					       gdouble dx, gdouble dy);

static GObjectClass *parent_class = NULL;

GType
dia_canvas_text_get_type (void)
{
  static GtkType canvas_text_type = 0;

  if (!canvas_text_type)
    {
      static const GTypeInfo canvas_text_info =
      {
        sizeof (DiaCanvasTextClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) dia_canvas_text_class_init,
        (GClassFinalizeFunc) NULL,
        NULL,
        
        sizeof (DiaCanvasText),
        0, /* n_preallocs */
        (GInstanceInitFunc) dia_canvas_text_init,
      };

      canvas_text_type = g_type_register_static (DIA_TYPE_CANVAS_ITEM,
						 "DiaCanvasText",
						 &canvas_text_info,
						 0);
    }
  return canvas_text_type;
}

static void
dia_canvas_text_class_init (DiaCanvasTextClass *klass)
{
  GObjectClass *object_class;
  DiaCanvasItemClass *item_class;

  object_class = G_OBJECT_CLASS(klass);
  item_class   = DIA_CANVAS_ITEM_CLASS(klass);
  parent_class = g_type_class_ref(DIA_TYPE_CANVAS_ITEM);

  object_class->set_property = dia_canvas_text_set_property;
  object_class->get_property = dia_canvas_text_get_property;

  item_class->render     = dia_canvas_text_real_render;
  item_class->update     = dia_canvas_text_real_update;
  item_class->distance   = dia_canvas_text_real_distance;
  item_class->move       = dia_canvas_text_real_move;

  g_object_class_install_property (object_class,
				   PROP_X,
				   g_param_spec_double ("x", _("Position"),
							_("The position of the text"),
							-G_MAXDOUBLE,
							G_MAXDOUBLE,
							0.0,
							G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_Y,
				   g_param_spec_double ("y", _("Position"),
							_("The position of the text"),
							-G_MAXDOUBLE,
							G_MAXDOUBLE,
							0,
							G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_TEXT,
				   g_param_spec_string ("text", _("The text"),
							_("The text"),
							"",
							G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_TEXT_COLOR,
				   g_param_spec_boxed ("text_color", _("Text color"),
						       _("The text color of the text"),
						       GDK_TYPE_COLOR,
						       G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
				   PROP_TEXT_WIDTH,
				   g_param_spec_double ("text_width", _("Text width"),
							_("The thickness of text"),
							0, G_MAXDOUBLE, 0,
							G_PARAM_READWRITE));
}

static void
dia_canvas_text_init (DiaCanvasText *text)
{
  GdkColor red   = { 0, 0xffff, 0, 0 };

  text->text_color = red;
  text->text_width = 1;
}

static void
dia_canvas_text_set_property (GObject *object, guint property_id,
			      const GValue *value, GParamSpec *pspec)
{
  DiaCanvasText *text = DIA_CANVAS_TEXT(object);
  gdouble   val;
  GdkColor *color;

  switch (property_id)
    {
    case PROP_X:
      val = g_value_get_double(value);
      if (val != text->point.x) {
	text->point.x = val;
	dia_canvas_item_request_update(DIA_CANVAS_ITEM(text));
      }
      break;
    case PROP_Y:
      val = g_value_get_double(value);
      if (val != text->point.y) {
	text->point.y = val;
	dia_canvas_item_request_update(DIA_CANVAS_ITEM(text));
      }
      break;
    case PROP_TEXT:
      if( text->text != NULL ) {
	g_free(text->text);
      }
      text->text = (guchar *) strdup(g_value_get_string(value));
      dia_canvas_item_request_update(DIA_CANVAS_ITEM(text));
      break;
    case PROP_TEXT_COLOR:
      color = g_value_get_boxed(value);
      text->text_color = *color;
      dia_canvas_item_request_update(DIA_CANVAS_ITEM(text));
      break;
    case PROP_TEXT_WIDTH:
      val = g_value_get_double(value);
      text->text_width = val;
      dia_canvas_item_request_update(DIA_CANVAS_ITEM(text));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
dia_canvas_text_get_property (GObject *object, guint property_id,
				   GValue *value, GParamSpec *pspec)
{
  DiaCanvasText *text = DIA_CANVAS_TEXT(object);

  switch (property_id)
    {
    case PROP_X:
      g_value_set_double(value, text->point.x);
      break;
    case PROP_Y:
      g_value_set_double(value, text->point.y);
      break;
    case PROP_TEXT:
      g_value_set_string(value, (gchar *) text->text);
      break;
    case PROP_TEXT_COLOR:
      g_value_set_boxed(value, &text->text_color);
      break;
    case PROP_TEXT_WIDTH:
      g_value_set_double(value, text->text_width);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
dia_canvas_text_real_render (DiaCanvasItem *item, DiaRenderer *renderer,
			     DiaRectangle *rect)
{
  DiaPoint        point;
  DiaCanvasText *text = DIA_CANVAS_TEXT(item);

  dia_renderer_get_text_size(renderer, &point, (gchar *) text->text);

  item->bounds.left   = text->point.x;
  item->bounds.top    = text->point.y;
  item->bounds.right  = text->point.x + point.x;
  item->bounds.bottom = text->point.y + point.y;

  dia_renderer_set_color(renderer, &text->text_color);
  dia_renderer_set_line_width(renderer, text->text_width);
  dia_renderer_draw_text(renderer, &text->point, (gchar *) text->text);
}

static void
dia_canvas_text_real_update (DiaCanvasItem *item)
{
  DiaPoint        point;
  DiaCanvasText  *text = DIA_CANVAS_TEXT(item);

  /* the text may have been moved, so dirty its former location */
  dia_canvas_dirty_region(item->canvas, &item->bounds);

  item->bounds.left   = text->point.x;
  item->bounds.top    = text->point.y;
  item->bounds.right  = text->point.x+1;
  item->bounds.bottom = text->point.y+1;

  if( (item->canvas->focus_view != NULL) &&
      (DIA_IS_RENDERER(item->canvas->focus_view)) ) {

    dia_renderer_get_text_size(DIA_RENDERER(item->canvas->focus_view),
			       &point, (gchar *) text->text);

    item->bounds.left   = text->point.x;
    item->bounds.top    = text->point.y;
    item->bounds.right  = text->point.x + point.x;
    item->bounds.bottom = text->point.y + point.y;
  }

  /* dirty the new location to redraw ... */
  dia_canvas_dirty_region(item->canvas, &item->bounds);
}

static gdouble
dia_canvas_text_real_distance (DiaCanvasItem *item, gdouble x,gdouble y)
{
  //DiaCanvasText *text = DIA_CANVAS_TEXT(item);
  double dx = 0.0, dy = 0.0;

  /* not the exact distance, but close enough for our checks */
  if (x < item->bounds.left)
    dx = item->bounds.left - x;
  else if (x > item->bounds.right)
    dx = x - item->bounds.right;

  if (y < item->bounds.top)
    dy = item->bounds.top - y;
  else if (y > item->bounds.bottom)
    dy = y - item->bounds.bottom;

  return dx*dx + dy*dy;
}

static void
dia_canvas_text_real_move (DiaCanvasItem *item,
			   gdouble dx, gdouble dy)
{
  DiaCanvasText *text = DIA_CANVAS_TEXT(item);

  text->point.x += dx;
  text->point.y += dy;

  dia_canvas_item_request_update(DIA_CANVAS_ITEM(text));
}