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

« back to all changes in this revision

Viewing changes to src/lm-non-linear-range.c

  • 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
#include <math.h>
 
21
#include "lm-non-linear-range.h"
 
22
#include "lm-util.h"
 
23
 
 
24
typedef struct
 
25
{
 
26
  const LMNonLinearRangeBlock   *blocks;
 
27
  int                           num_blocks;
 
28
} RangeInfo;
 
29
 
 
30
static GQuark info_quark = 0;
 
31
 
 
32
static void
 
33
global_init (void)
 
34
{
 
35
  if (! info_quark)
 
36
    info_quark = g_quark_from_static_string("lm-non-linear-range-info");
 
37
}
 
38
 
 
39
static RangeInfo *
 
40
get_info (GtkRange *range)
 
41
{
 
42
  RangeInfo *info;
 
43
 
 
44
  g_return_val_if_fail(GTK_IS_RANGE(range), NULL);
 
45
  g_return_val_if_fail(info_quark != 0, NULL);
 
46
 
 
47
  info = g_object_get_qdata(G_OBJECT(range), info_quark);
 
48
  g_return_val_if_fail(info != NULL, NULL);
 
49
 
 
50
  return info;
 
51
}
 
52
 
 
53
static int
 
54
get_block_len (const LMNonLinearRangeBlock *block)
 
55
{
 
56
  g_return_val_if_fail(block != NULL, -1);
 
57
 
 
58
  return ((block->max - block->min) / block->step) + 1;
 
59
}
 
60
 
 
61
void
 
62
lm_non_linear_range_setup_static (GtkRange *range,
 
63
                                  const LMNonLinearRangeBlock *blocks,
 
64
                                  int num_blocks)
 
65
{
 
66
  RangeInfo *info;
 
67
  int i;
 
68
  int num_values = 0;
 
69
 
 
70
  g_return_if_fail(GTK_IS_RANGE(range));
 
71
  g_return_if_fail(blocks != NULL);
 
72
  g_return_if_fail(num_blocks > 0);
 
73
 
 
74
  global_init();
 
75
 
 
76
  info = g_new0(RangeInfo, 1);
 
77
  info->blocks = blocks;
 
78
  info->num_blocks = num_blocks;
 
79
 
 
80
  g_object_set_qdata_full(G_OBJECT(range), info_quark, info, g_free);
 
81
 
 
82
  for (i = 0; i < num_blocks; i++)
 
83
    num_values += get_block_len(&blocks[i]);
 
84
 
 
85
  gtk_range_set_range(range, 0, num_values - 1);
 
86
}
 
87
 
 
88
int
 
89
lm_non_linear_range_get_value (GtkRange *range)
 
90
{
 
91
  RangeInfo *info;
 
92
  int raw;
 
93
  int offset = 0;
 
94
  int i;
 
95
 
 
96
  g_return_val_if_fail(GTK_IS_RANGE(range), -1);
 
97
 
 
98
  info = get_info(range);
 
99
 
 
100
  raw = (int) gtk_range_get_value(range);
 
101
 
 
102
  for (i = 0; i < info->num_blocks; i++)
 
103
    {
 
104
      const LMNonLinearRangeBlock *block = &info->blocks[i];
 
105
      int next_offset;
 
106
 
 
107
      next_offset = offset + get_block_len(block);
 
108
 
 
109
      if (raw >= offset && raw < next_offset)
 
110
        return block->min + (raw - offset) * block->step;
 
111
 
 
112
      offset = next_offset;
 
113
    }
 
114
 
 
115
  g_assert_not_reached();
 
116
  return -1;
 
117
}
 
118
 
 
119
static int
 
120
value_to_index (RangeInfo *info, int value)
 
121
{
 
122
  int offset = 0;
 
123
  int i;
 
124
 
 
125
  /* if smaller than the first value, use the first value */
 
126
  if (value < info->blocks[0].min)
 
127
    return 0;
 
128
 
 
129
  for (i = 0; i < info->num_blocks; i++)
 
130
    {
 
131
      const LMNonLinearRangeBlock *block = &info->blocks[i];
 
132
 
 
133
      if (value >= block->min && value <= block->max)
 
134
        {
 
135
          int rounded;
 
136
          int index;
 
137
          int j;
 
138
 
 
139
          /* round the value to the nearest step */
 
140
          rounded = lround((double) value / block->step) * block->step;
 
141
 
 
142
          for (j = block->min, index = 0; j <= block->max; j += block->step, index++)
 
143
            if (j == rounded)
 
144
              return offset + index;
 
145
 
 
146
          g_assert_not_reached();
 
147
        }
 
148
 
 
149
      offset += get_block_len(block);
 
150
    }
 
151
 
 
152
  /* block not found: fallback to the maximum value */
 
153
  return offset - 1;
 
154
}
 
155
 
 
156
/*
 
157
 * If the value is not found in the blocks, the nearest existing value
 
158
 * will be used.
 
159
 */
 
160
void
 
161
lm_non_linear_range_set_value (GtkRange *range, int value)
 
162
{
 
163
  g_return_if_fail(GTK_IS_RANGE(range));
 
164
 
 
165
  gtk_range_set_value(range, value_to_index(get_info(range), value));
 
166
}