File: inf-text-user.c

package info (click to toggle)
libinfinity 0.7.2-5
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 13,768 kB
  • sloc: ansic: 107,626; sh: 4,475; xml: 1,852; makefile: 1,278
file content (321 lines) | stat: -rw-r--r-- 8,277 bytes parent folder | download | duplicates (4)
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
/* libinfinity - a GObject-based infinote implementation
 * Copyright (C) 2007-2015 Armin Burgmeier <armin@arbur.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

#include <libinftext/inf-text-user.h>

typedef struct _InfTextUserPrivate InfTextUserPrivate;
struct _InfTextUserPrivate {
  guint caret;
  gint selection;
  gdouble hue;
};

enum {
  PROP_0,

  PROP_CARET,
  PROP_SELECTION,
  PROP_HUE
};

enum {
  SELECTION_CHANGED,

  LAST_SIGNAL
};

#define INF_TEXT_USER_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), INF_TEXT_TYPE_USER, InfTextUserPrivate))

static guint user_signals[LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE_WITH_CODE(InfTextUser, inf_text_user, INF_ADOPTED_TYPE_USER,
  G_ADD_PRIVATE(InfTextUser))

static void
inf_text_user_init(InfTextUser* user)
{
  InfTextUserPrivate* priv;
  priv = INF_TEXT_USER_PRIVATE(user);

  priv->caret = 0;
  priv->selection = 0;
  priv->hue = 0.0;
}

static void
inf_text_user_set_property(GObject* object,
                           guint prop_id,
                           const GValue* value,
                           GParamSpec* pspec)
{
  InfTextUser* user;
  InfTextUserPrivate* priv;

  user = INF_TEXT_USER(object);
  priv = INF_TEXT_USER_PRIVATE(user);

  switch(prop_id)
  {
  case PROP_CARET:
    priv->caret = g_value_get_uint(value);
    break;
  case PROP_SELECTION:
    priv->selection = g_value_get_int(value);
    break;
  case PROP_HUE:
    priv->hue = g_value_get_double(value);
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
    break;
  }
}

static void
inf_text_user_get_property(GObject* object,
                           guint prop_id,
                           GValue* value,
                           GParamSpec* pspec)
{
  InfTextUser* user;
  InfTextUserPrivate* priv;

  user = INF_TEXT_USER(object);
  priv = INF_TEXT_USER_PRIVATE(user);

  switch(prop_id)
  {
  case PROP_CARET:
    g_value_set_uint(value, priv->caret);
    break;
  case PROP_SELECTION:
    g_value_set_int(value, priv->selection);
    break;
  case PROP_HUE:
    g_value_set_double(value, priv->hue);
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
    break;
  }
}

static void
inf_text_user_selection_changed(InfTextUser* user,
                                guint position,
                                guint length,
                                gboolean by_request)
{
  InfTextUserPrivate* priv;
  priv = INF_TEXT_USER_PRIVATE(user);

  priv->caret = position;
  priv->selection = length;

  g_object_notify(G_OBJECT(user), "caret-position");
  g_object_notify(G_OBJECT(user), "selection-length");
}

static void
inf_text_user_class_init(InfTextUserClass* user_class)
{
  GObjectClass* object_class;
  object_class = G_OBJECT_CLASS(user_class);

  object_class->set_property = inf_text_user_set_property;
  object_class->get_property = inf_text_user_get_property;

  user_class->selection_changed = inf_text_user_selection_changed;

  g_object_class_install_property(
    object_class,
    PROP_CARET,
    g_param_spec_uint(
      "caret-position",
      "Caret position",
      "The position of this user's caret",
      0,
      G_MAXUINT,
      0,
      G_PARAM_READWRITE | G_PARAM_CONSTRUCT
    )
  );

  g_object_class_install_property(
    object_class,
    PROP_SELECTION,
    g_param_spec_int(
      "selection-length",
      "Selection length",
      "The number of characters of this user's selection",
      G_MININT,
      G_MAXINT,
      0,
      G_PARAM_READWRITE | G_PARAM_CONSTRUCT
    )
  );

  g_object_class_install_property(
    object_class,
    PROP_HUE,
    g_param_spec_double(
      "hue",
      "Hue",
      "The hue value of the user's color. saturation and lightness are set "
      "by each client individually.",
      0.0,
      1.0,
      0.0,
      G_PARAM_READWRITE | G_PARAM_CONSTRUCT
    )
  );

  user_signals[SELECTION_CHANGED] = g_signal_new(
    "selection-changed",
    G_OBJECT_CLASS_TYPE(object_class),
    G_SIGNAL_RUN_LAST,
    G_STRUCT_OFFSET(InfTextUserClass, selection_changed),
    NULL, NULL,
    NULL,
    G_TYPE_NONE,
    3,
    G_TYPE_UINT,
    G_TYPE_INT,
    G_TYPE_BOOLEAN
  );
}

/**
 * inf_text_user_new: (constructor)
 * @id: The user ID for this user.
 * @name: The user's name.
 * @vector: (allow-none): The state at which the user is at, or %NULL.
 * @hue: The hue value of the user's color.
 *
 * Creates a new #InfTextUser. @id should be unique for all users working
 * together. #InfUserTable will refuse to add users with duplicate id. If
 * @vector is %NULL, then the vector with all components zero is used.
 *
 * Returns: (transfer full): A new #InfTextUser. Free with g_object_unref()
 * when no longer needed.
 */
InfTextUser*
inf_text_user_new(guint id,
                  const gchar* name,
                  InfAdoptedStateVector* vector,
                  double hue)
{
  g_return_val_if_fail(name != NULL, NULL);

  return INF_TEXT_USER(
    g_object_new(
      INF_TEXT_TYPE_USER,
      "id", id,
      "name", name,
      "vector", vector,
      "hue", hue,
      NULL
    )
  );
}

/**
 * inf_text_user_get_caret_position:
 * @user: A #InfTextUser.
 *
 * Returns the position of @user's caret.
 *
 * Returns: @user's caret position.
 **/
guint
inf_text_user_get_caret_position(InfTextUser* user)
{
  g_return_val_if_fail(INF_TEXT_IS_USER(user), 0);
  return INF_TEXT_USER_PRIVATE(user)->caret;
}

/**
 * inf_text_user_get_selection_length:
 * @user: A #InfTextUser.
 *
 * Returns the number of characters this user has selected, starting from
 * the caret position. Negative number mean selection towards the beginning
 * of the buffer.
 *
 * Returns: @user's selection length in characters.
 **/
gint
inf_text_user_get_selection_length(InfTextUser* user)
{
  g_return_val_if_fail(INF_TEXT_IS_USER(user), 0);
  return INF_TEXT_USER_PRIVATE(user)->selection;
}

/**
 * inf_text_user_set_selection:
 * @user: A #InfTextUser.
 * @position: The new position for the user's caret.
 * @length: The number of characters to select. Negative numbers mean
 * selection towards the beginning.
 * @by_request: %TRUE if explicitly requested or %FALSE when just an effect
 * of another operation.
 *
 * Changes @user's selection (i.e. caret position and selection length). The
 * @by_request parameter should be set to %TRUE if the selection change was
 * requested explicitly, for example by the user actively moving the cursor
 * using the mouse or the keyboard. If the cursor position changes only
 * because another user inserted text at a position before the user's cursor
 * and this results in a change of the selection position, the @by_request
 * parameter should be set to %FALSE.
 **/
void
inf_text_user_set_selection(InfTextUser* user,
                            guint position,
                            gint length,
                            gboolean by_request)
{
  g_return_if_fail(INF_TEXT_IS_USER(user));
  g_signal_emit(
    G_OBJECT(user),
    user_signals[SELECTION_CHANGED],
    0,
    position,
    length,
    by_request
  );
}

/**
 * inf_text_user_get_hue:
 * @user: A #InfTextUser.
 *
 * Returns the hue of the user's color as a double ranging from 0 to 1.
 * The other components (saturation and lightness) are not specific to the
 * user and may be chosen indivudually to optimize the actual visual display.
 *
 * Returns: The hue of the @user's color.
 **/
gdouble
inf_text_user_get_hue(InfTextUser* user)
{
  g_return_val_if_fail(INF_TEXT_IS_USER(user), 0.0);
  return INF_TEXT_USER_PRIVATE(user)->hue;
}

/* vim:set et sw=2 ts=2: */