~ubuntu-branches/debian/sid/link-monitor-applet/sid

« back to all changes in this revision

Viewing changes to src/lm-line-graph.gob

  • Committer: Bazaar Package Importer
  • Author(s): Adriaan Peeters
  • Date: 2008-03-30 22:26:13 UTC
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20080330222613-5aubcuo9mgg2n7st
Tags: upstream-3.0
ImportĀ upstreamĀ versionĀ 3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Link Monitor Applet
 
3
 * Copyright (C) 2004-2008 Jean-Yves Lefort <jylefort@brutele.be>
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 3 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License along
 
16
 * with this program; if not, write to the Free Software Foundation, Inc.,
 
17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
18
 */
 
19
 
 
20
%headertop{
 
21
#include <gtk/gtk.h>
 
22
#include "lm-data-set.h"
 
23
#include "lm-util.h"
 
24
%}
 
25
 
 
26
%h{
 
27
/*
 
28
 * Make sure that the resulting area width is divisible by the x
 
29
 * subdivisions, and that the resulting area height is divisible by
 
30
 * the y subdivisions.
 
31
 */
 
32
#define LM_LINE_GRAPH_WIDTH             494
 
33
#define LM_LINE_GRAPH_HEIGHT            202
 
34
 
 
35
#define LM_LINE_GRAPH_AREA_WIDTH        (LM_LINE_GRAPH_WIDTH - LM_BOX_BORDER_WIDTH * 2)
 
36
#define LM_LINE_GRAPH_AREA_HEIGHT       (LM_LINE_GRAPH_HEIGHT - LM_BOX_BORDER_WIDTH * 2)
 
37
%}
 
38
 
 
39
%privateheader{
 
40
/*
 
41
 * Have integral subdivisions (100 ms vertical and 10 seconds
 
42
 * horizontal) at the default scale and span.
 
43
 */
 
44
 
 
45
#define X_MAJOR_SUBDIVISIONS            6
 
46
#define X_MINOR_SUBDIVISIONS            12
 
47
#define X_LABELS                        (X_MAJOR_SUBDIVISIONS + 1)
 
48
 
 
49
#define Y_MAJOR_SUBDIVISIONS            5
 
50
#define Y_MINOR_SUBDIVISIONS            10
 
51
#define Y_LABELS                        (Y_MAJOR_SUBDIVISIONS + 1)
 
52
 
 
53
typedef struct
 
54
{
 
55
  PangoLayout   *layout;
 
56
  int           x;
 
57
  int           y;
 
58
} LabelInfo;
 
59
%}
 
60
 
 
61
%{
 
62
#include <time.h>
 
63
#include <glib/gi18n.h>
 
64
#include "lm-util.h"
 
65
#include "lm-applet.h"
 
66
#include "lm-host-frontend.h"
 
67
 
 
68
#define LINE_WIDTH                      1.0
 
69
 
 
70
#define XALIGN                          0.5
 
71
#define YALIGN                          0.5
 
72
 
 
73
/* spacing between graph and labels */
 
74
#define GRAPH_MARGIN                    3
 
75
%}
 
76
 
 
77
class LM:Line:Graph from Gtk:Widget
 
78
{
 
79
  private LMApplet *applet;
 
80
  property POINTER applet (link, flags = CONSTRUCT_ONLY);
 
81
 
 
82
  private unsigned int update_timeout_id;
 
83
  private unsigned int update_time_timeout_id;
 
84
  private unsigned int update_not_done_timeout_id;
 
85
 
 
86
  private LabelInfo x_labels[X_LABELS];
 
87
  private LabelInfo y_labels[Y_LABELS];
 
88
 
 
89
  /* total widget size */
 
90
  private int width;
 
91
  private int height;
 
92
 
 
93
  /* relative offset of graph within widget */
 
94
  private int graph_x;
 
95
  private int graph_y;
 
96
 
 
97
  private GdkGC *subdivision_gc unrefwith g_object_unref;
 
98
 
 
99
  class_init (class)
 
100
  {
 
101
    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(class);
 
102
 
 
103
    gtk_widget_class_install_style_property(widget_class,
 
104
                                            g_param_spec_boxed("minor-subdivision-color",
 
105
                                                               NULL,
 
106
                                                               NULL,
 
107
                                                               GDK_TYPE_COLOR,
 
108
                                                               G_PARAM_READABLE));
 
109
    gtk_widget_class_install_style_property(widget_class,
 
110
                                            g_param_spec_boxed("major-subdivision-color",
 
111
                                                               NULL,
 
112
                                                               NULL,
 
113
                                                               GDK_TYPE_COLOR,
 
114
                                                               G_PARAM_READABLE));
 
115
  }
 
116
 
 
117
  constructor (self)
 
118
  {
 
119
    int i;
 
120
 
 
121
    GTK_WIDGET_SET_FLAGS(self, GTK_NO_WINDOW);
 
122
 
 
123
    g_object_connect(self,
 
124
                     "signal::style-set", self_clear_labels_and_queue_resize, self,
 
125
                     "signal::direction-changed", self_clear_labels_and_queue_resize, self,
 
126
                     NULL);
 
127
 
 
128
    lm_g_object_connect(self, selfp->applet,
 
129
                        "swapped-signal::notify::scale", self_clear_labels_and_queue_resize, self,
 
130
                        "swapped-signal::notify::tooltip-graph-span", self_tooltip_graph_span_changed, self,
 
131
                        NULL);
 
132
 
 
133
    LM_ARRAY_FOREACH(i, selfp->applet->hosts)
 
134
      {
 
135
        LMHostFrontend *host = g_ptr_array_index(selfp->applet->hosts, i);
 
136
 
 
137
        lm_g_object_connect(self, host,
 
138
                            "swapped-signal::notify::color", gtk_widget_queue_draw, self,
 
139
                            NULL);
 
140
      }
 
141
  }
 
142
 
 
143
  dispose (self)
 
144
  {
 
145
    lm_source_clear(&selfp->update_timeout_id);
 
146
    lm_source_clear(&selfp->update_time_timeout_id);
 
147
    lm_source_clear(&selfp->update_not_done_timeout_id);
 
148
 
 
149
    self_clear_labels(self);
 
150
  }
 
151
 
 
152
  private void
 
153
    tooltip_graph_span_changed (self)
 
154
  {
 
155
    /*
 
156
     * We must clear the update timeout since a new one is needed
 
157
     * (expose_event() will install it).
 
158
     */
 
159
    lm_source_clear(&selfp->update_timeout_id);
 
160
 
 
161
    self_clear_labels_and_queue_resize(self);
 
162
  }
 
163
 
 
164
  override (Gtk:Widget) void
 
165
    size_request (GtkWidget *widget, GtkRequisition *requisition)
 
166
  {
 
167
    Self *self = SELF(widget);
 
168
 
 
169
    self_ensure_labels(self);
 
170
 
 
171
    requisition->width = selfp->width;
 
172
    requisition->height = selfp->height;
 
173
  }
 
174
 
 
175
  private void
 
176
    clear_labels (self)
 
177
  {
 
178
    if (! selfp->x_labels[0].layout)
 
179
      return;
 
180
 
 
181
    self_clear_labels_real(self, selfp->x_labels, X_LABELS);
 
182
    self_clear_labels_real(self, selfp->y_labels, Y_LABELS);
 
183
  }
 
184
 
 
185
  private void
 
186
    clear_labels_real (self, LabelInfo *infos, int num_infos)
 
187
  {
 
188
    int i;
 
189
 
 
190
    for (i = 0; i < num_infos; i++)
 
191
      {
 
192
        LabelInfo *info = &infos[i];
 
193
 
 
194
        g_object_unref(info->layout);
 
195
        info->layout = NULL;
 
196
      }
 
197
  }
 
198
 
 
199
  private void
 
200
    clear_labels_and_queue_resize (self)
 
201
  {
 
202
    self_clear_labels(self);
 
203
    gtk_widget_queue_resize(GTK_WIDGET(self));
 
204
  }
 
205
 
 
206
  private void
 
207
    ensure_labels (self)
 
208
  {
 
209
    int i;
 
210
    GTimeVal now;
 
211
    double step;
 
212
    const char *time_format;
 
213
    int step_usec;
 
214
    int y_precision;
 
215
    int x_label_width = 0;
 
216
    int x_label_height = 0;
 
217
    int y_label_width = 0;
 
218
    int y_label_height = 0;
 
219
    PangoRectangle rect;
 
220
    int x_left_margin;
 
221
    int y_left_margin;
 
222
    double step_pixels;
 
223
 
 
224
    if (selfp->x_labels[0].layout)
 
225
      return;
 
226
 
 
227
    /* create horizontal labels */
 
228
 
 
229
    g_get_current_time(&now);
 
230
 
 
231
    step = (double) selfp->applet->tooltip_graph_span / X_MAJOR_SUBDIVISIONS;
 
232
 
 
233
    if (selfp->applet->tooltip_graph_span < 3600)
 
234
      time_format = "%T";
 
235
    else if (selfp->applet->tooltip_graph_span < 86400)
 
236
      time_format = "%R";
 
237
    else
 
238
      time_format = "%a %R";
 
239
 
 
240
    for (i = 0; i < X_LABELS; i++)
 
241
      {
 
242
        LabelInfo *info = &selfp->x_labels[i];
 
243
        time_t label_time;
 
244
        struct tm *tm;
 
245
        char *str;
 
246
 
 
247
        label_time = now.tv_sec - step * (X_MAJOR_SUBDIVISIONS - i);
 
248
        tm = localtime(&label_time);
 
249
 
 
250
        str = lm_strftime(time_format, tm);
 
251
        info->layout = self_create_pango_layout(self, str, &x_label_width, &x_label_height);
 
252
        g_free(str);
 
253
      }
 
254
 
 
255
    /* create vertical labels */
 
256
 
 
257
    step = (double) selfp->applet->scale / Y_MAJOR_SUBDIVISIONS;
 
258
 
 
259
    /* display a suitable number of decimals */
 
260
    step_usec = selfp->applet->scale * 1000 / Y_MAJOR_SUBDIVISIONS;
 
261
    if (step_usec % 1000 == 0)
 
262
      y_precision = 0;
 
263
    else if (step_usec % 100 == 0)
 
264
      y_precision = 1;
 
265
    else
 
266
      y_precision = 2;
 
267
 
 
268
    for (i = 0; i < Y_LABELS; i++)
 
269
      {
 
270
        LabelInfo *info = &selfp->y_labels[i];
 
271
        char *str;
 
272
 
 
273
        str = g_strdup_printf(_("%.*f ms"), y_precision, step * (Y_MAJOR_SUBDIVISIONS - i));
 
274
        info->layout = self_create_pango_layout(self, str, &y_label_width, &y_label_height);
 
275
        g_free(str);
 
276
      }
 
277
 
 
278
    /* compute margins, graph size and position */
 
279
 
 
280
    pango_layout_get_pixel_extents(selfp->x_labels[0].layout, NULL, &rect);
 
281
 
 
282
    x_left_margin = rect.width / 2;
 
283
    y_left_margin = y_label_width + GRAPH_MARGIN;
 
284
 
 
285
    selfp->graph_x = MAX(x_left_margin, y_left_margin);
 
286
    selfp->graph_y = y_label_height / 2;
 
287
 
 
288
    pango_layout_get_pixel_extents(selfp->x_labels[X_LABELS - 1].layout, NULL, &rect);
 
289
 
 
290
    selfp->width = selfp->graph_x + LM_LINE_GRAPH_WIDTH + rect.width / 2;
 
291
    selfp->height = selfp->graph_y + LM_LINE_GRAPH_HEIGHT + GRAPH_MARGIN + x_label_height;
 
292
 
 
293
    /* compute horizontal labels positions */
 
294
 
 
295
    step_pixels = LM_LINE_GRAPH_WIDTH / X_MAJOR_SUBDIVISIONS;
 
296
 
 
297
    for (i = 0; i < X_LABELS; i++)
 
298
      {
 
299
        LabelInfo *info = &selfp->x_labels[i];
 
300
 
 
301
        pango_layout_get_pixel_extents(info->layout, NULL, &rect);
 
302
 
 
303
        info->x = selfp->graph_x + step_pixels * i - rect.width / 2;
 
304
        info->y = selfp->graph_y + LM_LINE_GRAPH_HEIGHT + GRAPH_MARGIN + y_label_height - rect.height;
 
305
      }
 
306
 
 
307
    /* compute vertical labels positions */
 
308
 
 
309
    step_pixels = LM_LINE_GRAPH_HEIGHT / Y_MAJOR_SUBDIVISIONS;
 
310
 
 
311
    for (i = 0; i < Y_LABELS; i++)
 
312
      {
 
313
        LabelInfo *info = &selfp->y_labels[i];
 
314
 
 
315
        pango_layout_get_pixel_extents(info->layout, NULL, &rect);
 
316
 
 
317
        info->x = selfp->graph_x - GRAPH_MARGIN - rect.width;
 
318
        info->y = selfp->graph_y + step_pixels * i - rect.height / 2;
 
319
      }
 
320
 
 
321
    /* update the time labels when the second will change */
 
322
 
 
323
    if (selfp->update_time_timeout_id)
 
324
      g_source_remove(selfp->update_time_timeout_id);
 
325
 
 
326
    selfp->update_time_timeout_id = gdk_threads_add_timeout((1000 - now.tv_usec / 1000) + 1, self_update_time_cb, self);
 
327
  }
 
328
 
 
329
  private PangoLayout *
 
330
    create_pango_layout (self,
 
331
                         const char *text (check null),
 
332
                         int *max_width (check null),
 
333
                         int *max_height (check null))
 
334
  {
 
335
    PangoLayout *layout;
 
336
    char *markup;
 
337
    PangoRectangle rect;
 
338
 
 
339
    layout = gtk_widget_create_pango_layout(GTK_WIDGET(self), NULL);
 
340
 
 
341
    markup = g_strdup_printf("<span size=\"smaller\">%s</span>", text);
 
342
    pango_layout_set_markup(layout, markup, -1);
 
343
    g_free(markup);
 
344
 
 
345
    pango_layout_get_pixel_extents(layout, NULL, &rect);
 
346
 
 
347
    if (rect.width > *max_width)
 
348
      *max_width = rect.width;
 
349
    if (rect.height > *max_height)
 
350
      *max_height = rect.height;
 
351
 
 
352
    return layout;
 
353
  }
 
354
 
 
355
  override (Gtk:Widget) gboolean
 
356
    expose_event (GtkWidget *widget, GdkEventExpose *event)
 
357
  {
 
358
    Self *self = SELF(widget);
 
359
    int x;
 
360
    int y;
 
361
    int graph_x;
 
362
    int graph_y;
 
363
    int graph_area_x;
 
364
    int graph_area_y;
 
365
 
 
366
    if (! GTK_WIDGET_DRAWABLE(widget))
 
367
      return FALSE;
 
368
 
 
369
    lm_source_clear(&selfp->update_not_done_timeout_id);
 
370
 
 
371
    self_ensure_labels(self);
 
372
 
 
373
    lm_widget_get_origin(widget, XALIGN, YALIGN, &x, &y);
 
374
 
 
375
    self_draw_labels(self, event, x, y);
 
376
 
 
377
    graph_x = x + selfp->graph_x;
 
378
    graph_y = y + selfp->graph_y;
 
379
 
 
380
    lm_paint_box(widget->window,
 
381
                 GTK_WIDGET_STATE(widget),
 
382
                 GTK_SHADOW_IN,
 
383
                 &event->area,
 
384
                 widget,
 
385
                 widget->style->base_gc[GTK_WIDGET_STATE(widget)],
 
386
                 graph_x,
 
387
                 graph_y,
 
388
                 LM_LINE_GRAPH_WIDTH,
 
389
                 LM_LINE_GRAPH_HEIGHT);
 
390
 
 
391
    graph_area_x = graph_x + LM_BOX_BORDER_WIDTH;
 
392
    graph_area_y = graph_y + LM_BOX_BORDER_WIDTH;
 
393
 
 
394
    self_draw_subdivisions(self, graph_area_x, graph_area_y);
 
395
 
 
396
    if (selfp->applet->hosts->len != 0)
 
397
      {
 
398
        cairo_t *cr;
 
399
        LMTimeSpan now;
 
400
        LMTimeSpan scale;
 
401
        LMTimeSpan span;
 
402
        int i;
 
403
 
 
404
        cr = gdk_cairo_create(widget->window);
 
405
 
 
406
        /*
 
407
         * Setup a Cairo clip rectangle of size width,height at
 
408
         * (1,1). We need this rectangle because the line that
 
409
         * connects the first data point (which lies outside of the
 
410
         * graph) and the second (which lies inside) crosses the graph
 
411
         * edge.
 
412
         *
 
413
         * Another purpose is to make sure that Cairo will not draw on
 
414
         * the widget border (because of line width).
 
415
         */
 
416
        cairo_rectangle(cr, graph_area_x, graph_area_y, LM_LINE_GRAPH_AREA_WIDTH, LM_LINE_GRAPH_AREA_HEIGHT);
 
417
        cairo_clip(cr);
 
418
 
 
419
        /*
 
420
         * Add an offset so that 0,0 is located at the upper-left
 
421
         * corner of our clip rectangle, not at the upper-left corner
 
422
         * of the GdkWindow.
 
423
         */
 
424
        cairo_translate(cr, graph_area_x, graph_area_y);
 
425
 
 
426
        cairo_set_line_width(cr, LINE_WIDTH);
 
427
        cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
 
428
        cairo_set_line_join(cr, CAIRO_LINE_JOIN_MITER);
 
429
 
 
430
        now = lm_get_ticks();
 
431
        scale = (LMTimeSpan) selfp->applet->scale * 1000;
 
432
        span = (LMTimeSpan) selfp->applet->tooltip_graph_span * 1000000;
 
433
 
 
434
        /* iterate in reverse order, to plot hosts listed first above */
 
435
        LM_ARRAY_REVERSE_FOREACH(i, selfp->applet->hosts)
 
436
          {
 
437
            LMHostFrontend *host = g_ptr_array_index(selfp->applet->hosts, i);
 
438
 
 
439
            self_plot_host(self, host, cr, now, scale, span);
 
440
          }
 
441
 
 
442
        cairo_destroy(cr);
 
443
 
 
444
        if (! selfp->update_timeout_id)
 
445
          {
 
446
            int timeout;
 
447
 
 
448
            /*
 
449
             * Compute the update interval required for scrolling
 
450
             * pixel by pixel. Note that if we updated more often than
 
451
             * that, lines would wobble, since rasterisation could
 
452
             * cause the number of horizontal pixels between two data
 
453
             * points to vary from one frame to the next.
 
454
             */
 
455
            timeout = selfp->applet->tooltip_graph_span * 1000 / LM_LINE_GRAPH_AREA_WIDTH;
 
456
 
 
457
            selfp->update_timeout_id = gdk_threads_add_timeout(timeout, self_update_cb, self);
 
458
          }
 
459
      }
 
460
    else
 
461
      lm_source_clear(&selfp->update_timeout_id);
 
462
 
 
463
    return FALSE;
 
464
  }
 
465
 
 
466
  private void
 
467
    draw_labels (self, GdkEventExpose *event, int x, int y)
 
468
  {
 
469
    self_draw_labels_real(self, selfp->x_labels, X_LABELS, event, x, y);
 
470
    self_draw_labels_real(self, selfp->y_labels, Y_LABELS, event, x, y);
 
471
  }
 
472
 
 
473
  private void
 
474
    draw_labels_real (self,
 
475
                      const LabelInfo *infos,
 
476
                      int num_infos,
 
477
                      GdkEventExpose *event (check null),
 
478
                      int x,
 
479
                      int y)
 
480
  {
 
481
    GtkWidget *widget = GTK_WIDGET(self);
 
482
    int i;
 
483
 
 
484
    for (i = 0; i < num_infos; i++)
 
485
      {
 
486
        const LabelInfo *info = &infos[i];
 
487
 
 
488
        gtk_paint_layout(widget->style,
 
489
                         widget->window,
 
490
                         GTK_WIDGET_STATE(widget),
 
491
                         FALSE,
 
492
                         &event->area,
 
493
                         widget,
 
494
                         NULL,
 
495
                         x + info->x,
 
496
                         y + info->y,
 
497
                         info->layout);
 
498
      }
 
499
  }
 
500
 
 
501
  private void
 
502
    draw_subdivisions (self, int graph_area_x, int graph_area_y)
 
503
  {
 
504
    GtkWidget *widget = GTK_WIDGET(self);
 
505
    GdkColor minor_color = { 0, 0xe000, 0xe000, 0xe000 };
 
506
    GdkColor major_color = { 0, 0xff00, 0xcc00, 0xcc00 };
 
507
    GdkColor *custom_minor_color;
 
508
    GdkColor *custom_major_color;
 
509
    int minor_subdivision;
 
510
    int major_subdivision;
 
511
    int line_end;
 
512
    int graph_end;
 
513
    int line;
 
514
 
 
515
    if (! selfp->subdivision_gc)
 
516
      selfp->subdivision_gc = gdk_gc_new(widget->window);
 
517
 
 
518
    gtk_widget_style_get(widget,
 
519
                         "minor-subdivision-color", &custom_minor_color,
 
520
                         "major-subdivision-color", &custom_major_color,
 
521
                         NULL);
 
522
 
 
523
    if (custom_minor_color)
 
524
      {
 
525
        minor_color = *custom_minor_color;
 
526
        gdk_color_free(custom_minor_color);
 
527
      }
 
528
    if (custom_major_color)
 
529
      {
 
530
        major_color = *custom_major_color;
 
531
        gdk_color_free(custom_major_color);
 
532
      }
 
533
 
 
534
    /* horizontal */
 
535
 
 
536
    minor_subdivision = LM_LINE_GRAPH_AREA_WIDTH / X_MINOR_SUBDIVISIONS;
 
537
    major_subdivision = LM_LINE_GRAPH_AREA_WIDTH / X_MAJOR_SUBDIVISIONS;
 
538
 
 
539
    line_end = graph_area_y + LM_LINE_GRAPH_AREA_HEIGHT - 1;
 
540
    graph_end = graph_area_x + LM_LINE_GRAPH_AREA_WIDTH;
 
541
 
 
542
    gdk_gc_set_rgb_fg_color(selfp->subdivision_gc, &minor_color);
 
543
 
 
544
    for (line = minor_subdivision; line < LM_LINE_GRAPH_AREA_WIDTH; line += minor_subdivision)
 
545
      if (line % major_subdivision != 0)
 
546
        {
 
547
          int x = graph_area_x + line;
 
548
          gdk_draw_line(widget->window, selfp->subdivision_gc, x, graph_area_y, x, line_end);
 
549
        }
 
550
 
 
551
    gdk_gc_set_rgb_fg_color(selfp->subdivision_gc, &major_color);
 
552
 
 
553
    for (line = graph_area_x + major_subdivision; line < graph_end; line += major_subdivision)
 
554
      gdk_draw_line(widget->window, selfp->subdivision_gc, line, graph_area_y, line, line_end);
 
555
 
 
556
    /* vertical */
 
557
 
 
558
    minor_subdivision = LM_LINE_GRAPH_AREA_HEIGHT / Y_MINOR_SUBDIVISIONS;
 
559
    major_subdivision = LM_LINE_GRAPH_AREA_HEIGHT / Y_MAJOR_SUBDIVISIONS;
 
560
 
 
561
    line_end = graph_area_x + LM_LINE_GRAPH_AREA_WIDTH - 1;
 
562
    graph_end = graph_area_y + LM_LINE_GRAPH_AREA_HEIGHT;
 
563
 
 
564
    gdk_gc_set_rgb_fg_color(selfp->subdivision_gc, &minor_color);
 
565
 
 
566
    for (line = minor_subdivision; line < LM_LINE_GRAPH_AREA_HEIGHT; line += minor_subdivision)
 
567
      if (line % major_subdivision != 0)
 
568
        {
 
569
          int y = graph_area_y + line;
 
570
          gdk_draw_line(widget->window, selfp->subdivision_gc, graph_area_x, y, line_end, y);
 
571
        }
 
572
 
 
573
    gdk_gc_set_rgb_fg_color(selfp->subdivision_gc, &major_color);
 
574
 
 
575
    for (line = graph_area_y + major_subdivision; line < graph_end; line += major_subdivision)
 
576
      gdk_draw_line(widget->window, selfp->subdivision_gc, graph_area_x, line, line_end, line);
 
577
  }
 
578
 
 
579
  private void
 
580
    plot_host (self,
 
581
               LM:Host:Frontend *host (check null type),
 
582
               cairo_t *cr (check null),
 
583
               LMTimeSpan now,
 
584
               LMTimeSpan scale,
 
585
               LMTimeSpan span)
 
586
  {
 
587
    GArray *display_data;
 
588
    gboolean line_ended = TRUE;
 
589
    int i;
 
590
 
 
591
    cairo_new_path(cr);
 
592
    gdk_cairo_set_source_color(cr, &host->color);
 
593
 
 
594
    display_data = lm_data_set_get_display_data(host->data_set, now);
 
595
 
 
596
    for (i = 0; i < display_data->len; i++)
 
597
      {
 
598
        const LMDataPoint *dp = &g_array_index(display_data, LMDataPoint, i);
 
599
        LMTimeSpan age;
 
600
        double x;
 
601
        double y;
 
602
 
 
603
        if (dp->roundtrip_time < 0)
 
604
          {
 
605
            if (! line_ended)
 
606
              {
 
607
                line_ended = TRUE;
 
608
                cairo_stroke(cr);
 
609
              }
 
610
            continue;
 
611
          }
 
612
 
 
613
        age = now - dp->timestamp;
 
614
 
 
615
        x = (double) (span - age) / span * LM_LINE_GRAPH_AREA_WIDTH;
 
616
        y = (double) LM_LINE_GRAPH_AREA_HEIGHT - ((double) dp->roundtrip_time / scale * LM_LINE_GRAPH_AREA_HEIGHT);
 
617
 
 
618
        /*
 
619
         * Constrain high pings inside the graph, since no plot means
 
620
         * no reply. Note that we use 1 and not 0 because of the line
 
621
         * width: if we used 0, the point would be centered on the
 
622
         * edge of the clip rectangle, and half of it would be
 
623
         * invisible.
 
624
         */
 
625
        if (y < 1)
 
626
          y = 1;
 
627
 
 
628
        if (line_ended)
 
629
          {
 
630
            cairo_move_to(cr, x, y);
 
631
            line_ended = FALSE;
 
632
          }
 
633
        else
 
634
          cairo_line_to(cr, x, y);
 
635
      }
 
636
 
 
637
    if (! line_ended)
 
638
      cairo_stroke(cr);
 
639
  }
 
640
 
 
641
  private gboolean
 
642
    update_cb (gpointer data)
 
643
  {
 
644
    Self *self = data;
 
645
 
 
646
    gtk_widget_queue_draw(GTK_WIDGET(self));
 
647
 
 
648
    /*
 
649
     * If the update did not occur in a couple of seconds, it normally
 
650
     * means that the graph is no longer visible (for instance because
 
651
     * its parent tooltip was hidden). We can then save some
 
652
     * resources.
 
653
     */
 
654
    if (! selfp->update_not_done_timeout_id)
 
655
      selfp->update_not_done_timeout_id = g_timeout_add_seconds(10, self_update_not_done_cb, self);
 
656
 
 
657
    return TRUE;                /* continue */
 
658
  }
 
659
 
 
660
  private gboolean
 
661
    update_time_cb (gpointer data)
 
662
  {
 
663
    Self *self = data;
 
664
 
 
665
    self_clear_labels_and_queue_resize(self);
 
666
 
 
667
    selfp->update_time_timeout_id = 0;
 
668
    return FALSE;               /* remove */
 
669
  }
 
670
 
 
671
  private gboolean
 
672
    update_not_done_cb (gpointer data)
 
673
  {
 
674
    Self *self = data;
 
675
    int i;
 
676
 
 
677
    lm_source_clear(&selfp->update_timeout_id);
 
678
    lm_source_clear(&selfp->update_time_timeout_id);
 
679
 
 
680
    LM_ARRAY_FOREACH(i, selfp->applet->hosts)
 
681
      {
 
682
        LMHostFrontend *host = g_ptr_array_index(selfp->applet->hosts, i);
 
683
 
 
684
        lm_data_set_clear_display_data(host->data_set);
 
685
      }
 
686
 
 
687
    selfp->update_not_done_timeout_id = 0;
 
688
    return FALSE;
 
689
  }
 
690
 
 
691
  public GtkWidget *
 
692
    new (LM:Applet *applet (check null type))
 
693
  {
 
694
    return GTK_WIDGET(GET_NEW_VARG(LM_LINE_GRAPH_PROP_APPLET(applet), NULL));
 
695
  }
 
696
}