evolution-3.6.4/addressbook/gui/widgets/e-minicard-label.c

No issues found

Incomplete coverage

Tool Failure ID Location Function Message Data
clang-analyzer no-output-found e-minicard-label.c Message(text='Unable to locate XML output from invoke-clang-analyzer') None
clang-analyzer no-output-found e-minicard-label.c Message(text='Unable to locate XML output from invoke-clang-analyzer') None
Failure running clang-analyzer ('no-output-found')
Message
Unable to locate XML output from invoke-clang-analyzer
Failure running clang-analyzer ('no-output-found')
Message
Unable to locate XML output from invoke-clang-analyzer
  1 /*
  2  * This program is free software; you can redistribute it and/or
  3  * modify it under the terms of the GNU Lesser General Public
  4  * License as published by the Free Software Foundation; either
  5  * version 2 of the License, or (at your option) version 3.
  6  *
  7  * This program is distributed in the hope that it will be useful,
  8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 10  * Lesser General Public License for more details.
 11  *
 12  * You should have received a copy of the GNU Lesser General Public
 13  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 14  *
 15  *
 16  * Authors:
 17  *		Chris Lahey <clahey@ximian.com>
 18  *
 19  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 20  *
 21  */
 22 
 23 #ifdef HAVE_CONFIG_H
 24 #include <config.h>
 25 #endif
 26 
 27 #include "e-minicard-label.h"
 28 
 29 #include <gtk/gtk.h>
 30 #include <libgnomecanvas/libgnomecanvas.h>
 31 #include <glib/gi18n.h>
 32 #include <e-util/e-util.h>
 33 #include <text/e-text.h>
 34 #include <misc/e-canvas.h>
 35 #include <misc/e-canvas-utils.h>
 36 #include <gdk/gdkkeysyms.h>
 37 
 38 static void e_minicard_label_set_property  (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec);
 39 static void e_minicard_label_get_property  (GObject *object, guint property_id, GValue *value, GParamSpec *pspec);
 40 static gboolean e_minicard_label_event (GnomeCanvasItem *item, GdkEvent *event);
 41 static void e_minicard_label_realize (GnomeCanvasItem *item);
 42 static void e_minicard_label_reflow (GnomeCanvasItem *item, gint flags);
 43 static void e_minicard_label_style_set (EMinicardLabel *label, GtkStyle *previous_style);
 44 
 45 static void e_minicard_label_resize_children (EMinicardLabel *e_minicard_label);
 46 
 47 static void set_colors (EMinicardLabel *label);
 48 
 49 enum {
 50 	PROP_0,
 51 	PROP_WIDTH,
 52 	PROP_HEIGHT,
 53 	PROP_HAS_FOCUS,
 54 	PROP_FIELD,
 55 	PROP_FIELDNAME,
 56 	PROP_TEXT_MODEL,
 57 	PROP_MAX_FIELD_NAME_WIDTH,
 58 	PROP_EDITABLE
 59 };
 60 
 61 enum {
 62 	STYLE_SET,
 63 	LAST_SIGNAL
 64 };
 65 
 66 static guint e_minicard_label_signals[LAST_SIGNAL] = {0, };
 67 
 68 G_DEFINE_TYPE (
 69 	EMinicardLabel,
 70 	e_minicard_label,
 71 	GNOME_TYPE_CANVAS_GROUP)
 72 
 73 static void
 74 e_minicard_label_class_init (EMinicardLabelClass *class)
 75 {
 76 	GObjectClass *object_class;
 77 	GnomeCanvasItemClass *item_class;
 78 
 79 	object_class = G_OBJECT_CLASS (class);
 80 	item_class = (GnomeCanvasItemClass *) class;
 81 
 82 	class->style_set = e_minicard_label_style_set;
 83 
 84 	object_class->set_property = e_minicard_label_set_property;
 85 	object_class->get_property = e_minicard_label_get_property;
 86 
 87 	g_object_class_install_property (
 88 		object_class,
 89 		PROP_WIDTH,
 90 		g_param_spec_double (
 91 			"width",
 92 			"Width",
 93 			NULL,
 94 			0.0, G_MAXDOUBLE, 10.0,
 95 			G_PARAM_READWRITE));
 96 
 97 	g_object_class_install_property (
 98 		object_class,
 99 		PROP_HEIGHT,
100 		g_param_spec_double (
101 			"height",
102 			"Height",
103 			NULL,
104 			0.0, G_MAXDOUBLE, 10.0,
105 			G_PARAM_READWRITE));
106 
107 	g_object_class_install_property (
108 		object_class,
109 		PROP_HAS_FOCUS,
110 		g_param_spec_boolean (
111 			"has_focus",
112 			"Has Focus",
113 			NULL,
114 			FALSE,
115 			G_PARAM_READWRITE));
116 
117 	g_object_class_install_property (
118 		object_class,
119 		PROP_FIELD,
120 		g_param_spec_string (
121 			"field",
122 			"Field",
123 			NULL,
124 			NULL,
125 			G_PARAM_READWRITE));
126 
127 	g_object_class_install_property (
128 		object_class,
129 		PROP_FIELDNAME,
130 		g_param_spec_string (
131 			"fieldname",
132 			"Field Name",
133 			NULL,
134 			NULL,
135 			G_PARAM_READWRITE));
136 
137 	g_object_class_install_property (
138 		object_class,
139 		PROP_TEXT_MODEL,
140 		g_param_spec_object (
141 			"text_model",
142 			"Text Model",
143 			NULL,
144 			E_TYPE_TEXT_MODEL,
145 			G_PARAM_READWRITE));
146 
147 	g_object_class_install_property (
148 		object_class,
149 		PROP_MAX_FIELD_NAME_WIDTH,
150 		g_param_spec_double (
151 			"max_field_name_length",
152 			"Max field name length",
153 			NULL,
154 			-1.0, G_MAXDOUBLE, -1.0,
155 			G_PARAM_READWRITE));
156 
157 	g_object_class_install_property (
158 		object_class,
159 		PROP_EDITABLE,
160 		g_param_spec_boolean (
161 			"editable",
162 			"Editable",
163 			NULL,
164 			FALSE,
165 			G_PARAM_READWRITE));
166 
167 	e_minicard_label_signals[STYLE_SET] = g_signal_new (
168 		"style_set",
169 		G_TYPE_FROM_CLASS (object_class),
170 		G_SIGNAL_RUN_FIRST,
171 		G_STRUCT_OFFSET (EMinicardLabelClass, style_set),
172 		NULL, NULL,
173 		g_cclosure_marshal_VOID__OBJECT,
174 		G_TYPE_NONE, 1,
175 		GTK_TYPE_STYLE);
176 
177 	/* GnomeCanvasItem method overrides */
178 	item_class->realize     = e_minicard_label_realize;
179 	item_class->event       = e_minicard_label_event;
180 }
181 
182 static void
183 e_minicard_label_init (EMinicardLabel *minicard_label)
184 {
185 	minicard_label->width = 10;
186 	minicard_label->height = 10;
187 	minicard_label->rect = NULL;
188 	minicard_label->fieldname = NULL;
189 	minicard_label->field = NULL;
190 
191 	minicard_label->max_field_name_length = -1;
192 
193 	e_canvas_item_set_reflow_callback (
194 		GNOME_CANVAS_ITEM (minicard_label),
195 		e_minicard_label_reflow);
196 }
197 
198 static void
199 e_minicard_label_set_property (GObject *object,
200                                guint property_id,
201                                const GValue *value,
202                                GParamSpec *pspec)
203 {
204 	EMinicardLabel *e_minicard_label;
205 	GnomeCanvasItem *item;
206 
207 	e_minicard_label = E_MINICARD_LABEL (object);
208 	item = GNOME_CANVAS_ITEM (object);
209 
210 	switch (property_id) {
211 	case PROP_WIDTH:
212 		e_minicard_label->width = g_value_get_double (value);
213 		e_minicard_label_resize_children (e_minicard_label);
214 		e_canvas_item_request_reflow (item);
215 		break;
216 	case PROP_HAS_FOCUS:
217 		if (e_minicard_label->field && (g_value_get_boolean (value) != E_FOCUS_NONE))
218 			e_canvas_item_grab_focus (e_minicard_label->field, FALSE);
219 		break;
220 	case PROP_FIELD:
221 		gnome_canvas_item_set (e_minicard_label->field, "text", g_value_get_string (value), NULL);
222 		break;
223 	case PROP_FIELDNAME:
224 		gnome_canvas_item_set (e_minicard_label->fieldname, "text", g_value_get_string (value), NULL);
225 		break;
226 	case PROP_TEXT_MODEL:
227 		gnome_canvas_item_set (e_minicard_label->field, "model", g_value_get_object (value), NULL);
228 		break;
229 	case PROP_MAX_FIELD_NAME_WIDTH:
230 		e_minicard_label->max_field_name_length = g_value_get_double (value);
231 		break;
232 	case PROP_EDITABLE:
233 		e_minicard_label->editable = g_value_get_boolean (value);
234 		g_object_set (e_minicard_label->field, "editable", FALSE /* e_minicard_label->editable */, NULL);
235 		break;
236 	default:
237 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
238 		break;
239 	}
240 }
241 
242 static void
243 e_minicard_label_get_property (GObject *object,
244                                guint property_id,
245                                GValue *value,
246                                GParamSpec *pspec)
247 {
248 	EMinicardLabel *e_minicard_label;
249 
250 	e_minicard_label = E_MINICARD_LABEL (object);
251 
252 	switch (property_id) {
253 	case PROP_WIDTH:
254 		g_value_set_double (value, e_minicard_label->width);
255 		break;
256 	case PROP_HEIGHT:
257 		g_value_set_double (value, e_minicard_label->height);
258 		break;
259 	case PROP_HAS_FOCUS:
260 		g_value_set_boolean (value, e_minicard_label->has_focus ? E_FOCUS_CURRENT : E_FOCUS_NONE);
261 		break;
262 	case PROP_FIELD:
263 		g_object_get_property (
264 			G_OBJECT (e_minicard_label->field),
265 			"text", value);
266 		break;
267 	case PROP_FIELDNAME:
268 		g_object_get_property (
269 			G_OBJECT (e_minicard_label->fieldname),
270 			"text", value);
271 		break;
272 	case PROP_TEXT_MODEL:
273 		g_object_get_property (
274 			G_OBJECT (e_minicard_label->field),
275 			"model", value);
276 		break;
277 	case PROP_MAX_FIELD_NAME_WIDTH:
278 		g_value_set_double (value, e_minicard_label->max_field_name_length);
279 		break;
280 	case PROP_EDITABLE:
281 		g_value_set_boolean (value, e_minicard_label->editable);
282 		break;
283 	default:
284 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
285 		break;
286 	}
287 }
288 
289 static void
290 e_minicard_label_realize (GnomeCanvasItem *item)
291 {
292 	EMinicardLabel *e_minicard_label;
293 	GnomeCanvasGroup *group;
294 
295 	e_minicard_label = E_MINICARD_LABEL (item);
296 	group = GNOME_CANVAS_GROUP (item);
297 
298 	GNOME_CANVAS_ITEM_CLASS (e_minicard_label_parent_class)->realize (item);
299 
300 	e_canvas_item_request_reflow (item);
301 
302 	e_minicard_label->rect = gnome_canvas_item_new (
303 		group,
304 		gnome_canvas_rect_get_type (),
305 		"x1", (gdouble) 0,
306 		"y1", (gdouble) 0,
307 		"x2", (gdouble) e_minicard_label->width - 1,
308 		"y2", (gdouble) e_minicard_label->height - 1,
309 		"outline_color", NULL,
310 		NULL);
311 
312 	e_minicard_label->fieldname = gnome_canvas_item_new (
313 		group,
314 		e_text_get_type (),
315 		"clip_width", (gdouble) (e_minicard_label->width / 2 - 4),
316 		"clip", TRUE,
317 		"use_ellipsis", TRUE,
318 		"fill_color", "black",
319 		"im_context", E_CANVAS (item->canvas)->im_context,
320 		NULL);
321 
322 	e_canvas_item_move_absolute (e_minicard_label->fieldname, 2, 1);
323 
324 	e_minicard_label->field = gnome_canvas_item_new (
325 		group,
326 		e_text_get_type (),
327 		"clip_width", (gdouble) ((e_minicard_label->width + 1) / 2 - 4),
328 		"clip", TRUE,
329 		"use_ellipsis", TRUE,
330 		"fill_color", "black",
331 		"editable", FALSE, /* e_minicard_label->editable, */
332 		"im_context", E_CANVAS (item->canvas)->im_context,
333 		NULL);
334 
335 	e_canvas_item_move_absolute (
336 		e_minicard_label->field,
337 		(e_minicard_label->width / 2 + 2), 1);
338 
339 	set_colors (e_minicard_label);
340 
341 	e_canvas_item_request_reflow (item);
342 }
343 
344 static gboolean
345 e_minicard_label_event (GnomeCanvasItem *item,
346                         GdkEvent *event)
347 {
348 	EMinicardLabel *e_minicard_label;
349 
350 	e_minicard_label = E_MINICARD_LABEL (item);
351 
352 	switch (event->type) {
353 	case GDK_KEY_PRESS:
354 		if (event->key.keyval == GDK_KEY_Escape) {
355 			GnomeCanvasItem *parent;
356 
357 			e_text_cancel_editing (E_TEXT (e_minicard_label->field));
358 
359 			parent = GNOME_CANVAS_ITEM (e_minicard_label)->parent;
360 			if (parent)
361 				e_canvas_item_grab_focus (parent, FALSE);
362 		}
363 		break;
364 	case GDK_FOCUS_CHANGE: {
365 		GdkEventFocus *focus_event = (GdkEventFocus *) event;
366 
367 		e_minicard_label->has_focus = focus_event->in;
368 		set_colors (e_minicard_label);
369 
370 		g_object_set (
371 			e_minicard_label->field,
372 			"handle_popup", e_minicard_label->has_focus,
373 			NULL);
374 		break;
375 	}
376 	case GDK_BUTTON_PRESS:
377 	case GDK_BUTTON_RELEASE:
378 	case GDK_MOTION_NOTIFY:
379 	case GDK_ENTER_NOTIFY:
380 	case GDK_LEAVE_NOTIFY: {
381 		gboolean return_val;
382 		g_signal_emit_by_name (e_minicard_label->field, "event", event, &return_val);
383 		return return_val;
384 	}
385 	default:
386 		break;
387 	}
388 
389 	return GNOME_CANVAS_ITEM_CLASS (e_minicard_label_parent_class)->
390 		event (item, event);
391 }
392 
393 static void
394 e_minicard_label_resize_children (EMinicardLabel *e_minicard_label)
395 {
396 	gdouble left_width;
397 	gdouble fieldnamewidth;
398 	gdouble fieldwidth;
399 	gboolean is_rtl = (gtk_widget_get_default_direction () == GTK_TEXT_DIR_RTL);
400 	if (e_minicard_label->max_field_name_length != -1 && ((e_minicard_label->width / 2) - 4 > e_minicard_label->max_field_name_length))
401 		left_width = e_minicard_label->max_field_name_length;
402 	else
403 		left_width = e_minicard_label->width / 2 - 4;
404 
405 	fieldnamewidth = (gdouble) MAX (left_width, 0);
406 	fieldwidth = (gdouble) MAX (e_minicard_label->width - 8 - left_width, 0);
407 	gnome_canvas_item_set (
408 		e_minicard_label->fieldname,
409 		"clip_width", is_rtl ? fieldwidth : fieldnamewidth,
410 		NULL);
411 	gnome_canvas_item_set (
412 		e_minicard_label->field,
413 		"clip_width", is_rtl ? fieldnamewidth : fieldwidth,
414 		NULL);
415 }
416 
417 static void
418 set_colors (EMinicardLabel *label)
419 {
420 	GnomeCanvasItem *item = GNOME_CANVAS_ITEM (label);
421 
422 	if ((item->flags & GNOME_CANVAS_ITEM_REALIZED)) {
423 		GnomeCanvas *canvas;
424 		GtkStyle *style;
425 
426 		canvas = GNOME_CANVAS_ITEM (label)->canvas;
427 		style = gtk_widget_get_style (GTK_WIDGET (canvas));
428 
429 		if (label->has_focus) {
430 			gnome_canvas_item_set (
431 				label->rect,
432 				"outline_color_gdk", &style->mid[GTK_STATE_SELECTED],
433 				"fill_color_gdk", &style->bg[GTK_STATE_NORMAL],
434 				NULL);
435 
436 			gnome_canvas_item_set (
437 				label->field,
438 				"fill_color_gdk", &style->text[GTK_STATE_NORMAL],
439 				NULL);
440 
441 			gnome_canvas_item_set (
442 				label->fieldname,
443 				"fill_color_gdk", &style->text[GTK_STATE_NORMAL],
444 				NULL);
445 		}
446 		else {
447 			gnome_canvas_item_set (
448 				label->rect,
449 				"outline_color_gdk", NULL,
450 				"fill_color_gdk", NULL,
451 				NULL);
452 
453 			gnome_canvas_item_set (
454 				label->field,
455 				"fill_color_gdk", &style->text[GTK_STATE_NORMAL],
456 				NULL);
457 
458 			gnome_canvas_item_set (
459 				label->fieldname,
460 				"fill_color_gdk", &style->text[GTK_STATE_NORMAL],
461 				NULL);
462 		}
463 	}
464 }
465 
466 static void
467 e_minicard_label_style_set (EMinicardLabel *label,
468                             GtkStyle *previous_style)
469 {
470 	set_colors (label);
471 }
472 
473 static void
474 e_minicard_label_reflow (GnomeCanvasItem *item,
475                          gint flags)
476 {
477 	EMinicardLabel *e_minicard_label = E_MINICARD_LABEL (item);
478 
479 	gint old_height;
480 	gdouble text_height;
481 	gdouble left_width;
482 
483 	old_height = e_minicard_label->height;
484 
485 	g_object_get (
486 		e_minicard_label->fieldname,
487 		"text_height", &text_height,
488 		NULL);
489 
490 	e_minicard_label->height = text_height;
491 
492 	g_object_get (
493 		e_minicard_label->field,
494 		"text_height", &text_height,
495 		NULL);
496 
497 	if (e_minicard_label->height < text_height)
498 		e_minicard_label->height = text_height;
499 	e_minicard_label->height += 3;
500 
501 	gnome_canvas_item_set (
502 		e_minicard_label->rect,
503 		"x2", (gdouble) e_minicard_label->width - 1,
504 		"y2", (gdouble) e_minicard_label->height - 1,
505 		NULL);
506 
507 	gnome_canvas_item_set (
508 		e_minicard_label->fieldname,
509 		"clip_height", (gdouble) e_minicard_label->height - 3,
510 		NULL);
511 
512 	if (e_minicard_label->max_field_name_length != -1 && ((e_minicard_label->width / 2) - 4 > e_minicard_label->max_field_name_length))
513 		left_width = e_minicard_label->max_field_name_length;
514 	else
515 		left_width = e_minicard_label->width / 2 - 4;
516 
517 	e_canvas_item_move_absolute (e_minicard_label->field, left_width + 6, 1);
518 
519 	if (old_height != e_minicard_label->height)
520 		e_canvas_item_request_parent_reflow (item);
521 }
522 
523 GnomeCanvasItem *
524 e_minicard_label_new (GnomeCanvasGroup *parent)
525 {
526 	return gnome_canvas_item_new (
527 		parent, e_minicard_label_get_type (), NULL);
528 }