File: tooltips.c

package info (click to toggle)
gurlchecker 0.13.1-2.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 4,872 kB
  • sloc: ansic: 16,629; sh: 10,243; makefile: 346
file content (390 lines) | stat: -rw-r--r-- 9,266 bytes parent folder | download | duplicates (3)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
 * Copyright (C) 2002-2010
 * Emmanuel Saracco <esaracco@users.labs.libre-entreprise.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "utils.h"
#include "check.h"
#include "application.h"
#include "lists.h"
#include "project.h"

#include "tooltips.h"

/**
 * UCTTMain:
 * @x: X mouse coord.
 * @y: Y mouse coord.
 * @active: %TRUE if tooltip is being displayed.
 * @out: %TRUE is the mouse is out of the tooltip window.
 * @last_id: #UCLinkProperties Id of the last row for which we displayed 
 *           tooltip in the main tree view.
 * @current_id: #UCLinkProperties Id of the current row for which we are
 *              displaying tooltip.
 *
 * Structure for the tooltips object.
 */
typedef struct _UCTTMain UCTTMain;
struct _UCTTMain
{
  gint32 x;
  gint32 y;
  gboolean active;
  gboolean out;
  guint32 last_id;
  guint32 current_id;
};

static UCTTMain uc_tooltips_main;
static GtkWidget *treeview_tooltip = NULL;
static guint timer_id = 0;
static gboolean uc_tooltips_display_tooltips_value = TRUE;
static gboolean uc_tooltips_main_cb (gpointer data);

/**
 * uc_tooltips_main_set_display:
 * @value: %TRUE if we must display tooltips.
 *
 * Set it user want that tooltips be displayed or not.
 */
void
uc_tooltips_main_set_display (const gboolean value)
{
  uc_tooltips_display_tooltips_value = value;
}

/**
 * uc_tooltips_main_get_display:
 *
 * Check if we can display tooltips.
 * 
 * Returns: %TRUE if we must display tooltips.
 */
gboolean
uc_tooltips_main_get_display (void)
{
  return uc_tooltips_display_tooltips_value;
}

/**
 * uc_tooltips_init:
 *
 * Initialize tooltips object.
 */
void
uc_tooltips_init (void)
{
  if (timer_id != 0)
    return;

  timer_id = g_timeout_add (UC_TOOLTIPS_DELAY_DEFAULT,
			    &uc_tooltips_main_cb, NULL);

  uc_tooltips_main_set_mouse_coord (0, 0);
  uc_tooltips_main_set_active (FALSE);
  uc_tooltips_main_set_frozen (FALSE);
  uc_tooltips_main_set_last_id (0);
  uc_tooltips_main_set_current_id (0);
  uc_tooltips_main_set_display (FALSE);
}

/**
 * uc_tooltips_main_cb:
 * @data: Always %NULL.
 *
 * Callback to manage tooltip show/hide.
 *
 * Returns: Always %TRUE.
 */
static gboolean
uc_tooltips_main_cb (gpointer data)
{
  GtkWidget *widget = NULL;
  GtkTreePath *treepath = NULL;
  GValue *value = NULL;
  gchar *str = NULL;
  UCLinkProperties *prop = NULL;
  gint32 x = 0;
  gint32 y = 0;
  GtkTreeIter iter;

  if (treeview == NULL ||
      !uc_tooltips_main_get_display () ||
      uc_tooltips_main_get_frozen () ||
      uc_lists_checked_links_is_empty () ||
      (uc_project_get_url () == NULL &&
       !uc_project_get_check_is_bookmarks ()))
    return TRUE;

  uc_tooltips_main_get_mouse_coord (&x, &y);
  gtk_tree_view_get_path_at_pos (treeview, x, y, &treepath, NULL, NULL, NULL);

  if (treepath == NULL)
    {
      uc_tooltips_main_destroy ();

      return TRUE;
    }

  gtk_tree_model_get_iter (GTK_TREE_MODEL (treestore), &iter, treepath);
  gtk_tree_path_free (treepath), treepath = NULL;

  value = g_new0 (GValue, 1);
  gtk_tree_model_get_value (GTK_TREE_MODEL (treestore), &iter, 0, value);
  uc_tooltips_main_set_current_id (g_value_get_int (value));
  g_value_unset (value);

  prop =
    uc_lists_checked_links_lookup_by_uid (uc_tooltips_main_get_current_id ());

  if (prop == NULL || prop->link_type == LINK_TYPE_BOOKMARK_FOLDER)
    {
      uc_tooltips_main_destroy ();

      return TRUE;
    }

  if (!uc_tooltips_main_get_active ())
    {

      uc_tooltips_main_set_active (TRUE);
      uc_tooltips_main_set_last_id (uc_tooltips_main_get_current_id ());

      treeview_tooltip = WGET ("treeview_tooltip");

      if (!uc_tooltips_main_get_frozen ())
	{
	  gint32 wx = 0;
	  gint32 wy = 0;
	  gint32 ph = 0;
	  gint32 pw = 0;
	  gchar *lm_date = NULL;
	  gchar *parent_url = NULL;
	  gchar *url = NULL;
	  GtkWidget *w = treeview_tooltip;
	  UCStatusCode *sc = NULL;
	  GdkColor color;

	  parent_url =
	    (prop->parent != NULL &&
	     prop->parent->link_type != LINK_TYPE_BOOKMARK_FOLDER) ?
	    uc_utils_replace (prop->parent->url, "&",
			      "&amp;") : g_strdup ("-");
	  url = uc_utils_replace (prop->url, "&", "&amp;");
	  lm_date = g_hash_table_lookup (prop->header, UC_LAST_MODIFIED);
	  if (lm_date == NULL)
	    lm_date = "-";

	  sc =
	    uc_application_get_status_code_properties (g_hash_table_lookup
						       (prop->header,
							UC_HEADER_STATUS));

	  gdk_color_parse (sc->color, &color);
	  gtk_widget_modify_bg (w, GTK_STATE_NORMAL, &color);

	  widget = WGET ("tt_image");
	  gtk_image_set_from_file (GTK_IMAGE (widget),
				   uc_check_get_link_type_icon_path
				   (prop->link_type, prop->protocol));

	  widget = WGET ("tt_type");
	  str = g_strdup_printf ("<b>%s</b>",
				 uc_check_get_link_type_label
				 (prop->link_type));
	  gtk_label_set_markup (GTK_LABEL (widget), str);
	  g_free (str), str = NULL;

	  widget = WGET ("tt_label");
	  str = g_strdup_printf (_("<b>Status</b>: %s\n"
				   "<b>Location</b>: %s\n"
				   "<b>Parent location</b>: %s\n"
				   "<b>Last modified</b>: %s"),
				 sc->message, url, parent_url, lm_date);
	  gtk_label_set_markup (GTK_LABEL (widget), str);

	  g_free (str), str = NULL;
	  g_free (parent_url), parent_url = NULL;
	  g_free (url), url = NULL;

	  gtk_window_get_size (GTK_WINDOW (w), &pw, &ph);
	  gtk_window_get_position (GTK_WINDOW (w), &wx, &wy);
	  gtk_window_move (GTK_WINDOW (w), wx, wy + (ph / 2) + 10);

	  gtk_widget_show_all (w);
	}
    }
  else if (uc_tooltips_main_get_last_id () !=
	   uc_tooltips_main_get_current_id ())
    {
      uc_tooltips_main_set_active (FALSE);
      uc_tooltips_main_destroy ();
    }

  return TRUE;
}

/**
 * uc_tooltips_main_get_active:
 *
 * Check if a tooltip is being displayed.
 *
 * Returns: %TRUE if a tooltip is being displayed.
 */
gboolean
uc_tooltips_main_get_active (void)
{
  return uc_tooltips_main.active;
}

/**
 * uc_tooltips_main_set_active:
 * @value: %TRUE if a tooltip is active.
 *
 * Tell if a tooltip is active or not.
 */
void
uc_tooltips_main_set_active (const gboolean value)
{
  uc_tooltips_main.active = value;
}

/**
 * uc_tooltips_main_get_current_id:
 *
 * Get the current id of a #UCLinkProperties object currently displayed
 * in a tooltip.
 * 
 * Returns: Id of the #UCLinkProperties object currently displayed
 *          in the tooltip.
 */
guint32
uc_tooltips_main_get_current_id (void)
{
  return uc_tooltips_main.current_id;
}

/**
 * uc_tooltips_main_set_current_id:
 * @id: Id of the #UCLinkProperties object currently displayed in the tooltip.
 *
 * Tell the id of the #UCLinkProperties object currently displayed in 
 * the tooltip.
 */
void
uc_tooltips_main_set_current_id (const guint32 id)
{
  uc_tooltips_main.current_id = id;
}

/**
 * uc_tooltips_main_get_last_id:
 *
 * Get the id of the #UCLinkProperties object previously displayed in
 * the tooltip.
 * 
 * Returns: The last #UCLinkProperties id.
 */
guint32
uc_tooltips_main_get_last_id (void)
{
  return uc_tooltips_main.last_id;
}

/**
 * uc_tooltips_main_set_last_id:
 * @id: #UCLinkProperties id to set.
 *
 * Set the id of the #UCLinkProperties object previously displayed in
 * the tooltip.
 */
void
uc_tooltips_main_set_last_id (const guint32 id)
{
  uc_tooltips_main.last_id = id;
}

/**
 * uc_tooltips_main_set_mouse_coord:
 * @x: X mouse coord.
 * @y: Y mouse coord.
 *
 * Set the current mouse coord.
 */
void
uc_tooltips_main_set_mouse_coord (const gint32 x, const gint32 y)
{
  uc_tooltips_main.x = x;
  uc_tooltips_main.y = y;
}

/**
 * uc_tooltips_main_destroy:
 *
 * Destroy a tooltip window.
 */
void
uc_tooltips_main_destroy (void)
{
  if (treeview_tooltip != NULL && GTK_IS_WIDGET (treeview_tooltip))
    gtk_widget_hide (treeview_tooltip);
}

/**
 * uc_tooltips_main_get_mouse_coord:
 * @x: X mouse coord. to be set.
 * @y: Y mouse coord. to be set.
 *
 * Get the mouse coord. @X and @Y parameters are modified.
 */
void
uc_tooltips_main_get_mouse_coord (gint32 * x, gint32 * y)
{
  if (x != NULL)
    *x = uc_tooltips_main.x;

  if (y != NULL)
    *y = uc_tooltips_main.y;
}

/**
 * uc_tooltips_main_get_frozen:
 *
 * The tooltips display is "frozen" when the mouse is out of the main
 * tree view.
 * 
 * Returns: %TRUE if tooltips display must be "frozen".
 */
gboolean
uc_tooltips_main_get_frozen (void)
{
  return uc_tooltips_main.out;
}

/**
 * uc_tooltips_main_set_frozen:
 * @value: %TRUE is the mouse is out of the main tree view.
 *
 * The tooltips display is "frozen" when the mouse is out of the main
* tree view.
 */
void
uc_tooltips_main_set_frozen (const gboolean value)
{
  uc_tooltips_main.out = value;
}