summaryrefslogtreecommitdiff
path: root/champlain-gtk/gtk-champlain-embed.c
blob: fa2b76e080af8f1402b5a797b8b29cc549aa636b (plain)
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
 * Copyright (C) 2008-2009 Pierre-Luc Beaudoin <pierre-luc@pierlux.com>
 *
 * 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.1 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
 */

/**
 * SECTION:gtk-champlain-embed
 * @short_description: A Gtk+ Widget that embeds a #ChamplainView
 *
 * Since #ChamplainView is a #ClutterActor, you cannot embed it directly
 * into a Gtk+ application.  This widget solves this problem.  It creates
 * the #ChamplainView for you, you can get it with
 * #gtk_champlain_embed_get_view.
 */
#include "config.h"

#include <champlain/champlain.h>

#include <gtk/gtk.h>
#include <clutter/clutter.h>
#include <clutter-gtk/clutter-gtk.h>

#include "gtk-champlain-embed.h"

#if (GTK_MAJOR_VERSION == 2 && GTK_MINOR_VERSION <= 12)
#define gtk_widget_get_window(widget) ((widget)->window)
#endif

enum
{
  /* normal signals */
  LAST_SIGNAL
};

enum
{
  PROP_0,
  PROP_VIEW
};

/* static guint gtk_champlain_embed_embed_signals[LAST_SIGNAL] = { 0, }; */

struct _GtkChamplainEmbedPrivate
{
  GtkWidget *clutter_embed;
  ChamplainView *view;

  GdkCursor *cursor_hand_open;
  GdkCursor *cursor_hand_closed;

  guint width;
  guint height;
};


static void gtk_champlain_embed_get_property (GObject *object,
    guint prop_id,
    GValue *value,
    GParamSpec *pspec);
static void gtk_champlain_embed_set_property (GObject *object,
    guint prop_id,
    const GValue *value,
    GParamSpec *pspec);
static void gtk_champlain_embed_finalize (GObject *object);
static void gtk_champlain_embed_dispose (GObject *object);
static void gtk_champlain_embed_class_init (GtkChamplainEmbedClass *klass);
static void gtk_champlain_embed_init (GtkChamplainEmbed *view);
static void view_size_allocated_cb (GtkWidget *widget,
    GtkAllocation *allocation,
    GtkChamplainEmbed *view);
static gboolean mouse_button_cb (GtkWidget *widget,
    GdkEventButton *event,
    GtkChamplainEmbed *view);
static void view_size_allocated_cb (GtkWidget *widget,
    GtkAllocation *allocation,
    GtkChamplainEmbed *view);
static void view_realize_cb (GtkWidget *widget,
    GtkChamplainEmbed *view);
static gboolean embed_focus_cb (GtkChamplainEmbed *embed,
    GdkEvent *event);
static gboolean stage_key_press_cb (ClutterActor *actor,
    ClutterEvent *event,
    GtkChamplainEmbed *embed);

G_DEFINE_TYPE_WITH_PRIVATE (GtkChamplainEmbed, gtk_champlain_embed, GTK_TYPE_ALIGNMENT)

static void
gtk_champlain_embed_get_property (GObject *object,
    guint prop_id,
    GValue *value,
    GParamSpec *pspec)
{
  GtkChamplainEmbed *embed = GTK_CHAMPLAIN_EMBED (object);
  GtkChamplainEmbedPrivate *priv = embed->priv;

  switch (prop_id)
    {
    case PROP_VIEW:
      g_value_set_object (value, priv->view);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}


static void
gtk_champlain_embed_set_property (GObject *object,
    guint prop_id,
    const GValue *value,
    GParamSpec *pspec)
{
  switch (prop_id)
    {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}


static void
gtk_champlain_embed_dispose (GObject *object)
{
  GtkChamplainEmbed *embed = GTK_CHAMPLAIN_EMBED (object);
  GtkChamplainEmbedPrivate *priv = embed->priv;

  if (priv->cursor_hand_open != NULL)
    {
#if (GTK_MAJOR_VERSION == 2)
      gdk_cursor_unref (priv->cursor_hand_open);
#else
      g_object_unref (priv->cursor_hand_open);
#endif
      priv->cursor_hand_open = NULL;
    }

  if (priv->cursor_hand_closed != NULL)
    {
#if (GTK_MAJOR_VERSION == 2)
      gdk_cursor_unref (priv->cursor_hand_closed);
#else
      g_object_unref (priv->cursor_hand_closed);
#endif
      priv->cursor_hand_closed = NULL;
    }

  G_OBJECT_CLASS (gtk_champlain_embed_parent_class)->dispose (object);
}


static void
gtk_champlain_embed_finalize (GObject *object)
{
  G_OBJECT_CLASS (gtk_champlain_embed_parent_class)->finalize (object);
}


static void
gtk_champlain_embed_class_init (GtkChamplainEmbedClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  object_class->finalize = gtk_champlain_embed_finalize;
  object_class->dispose = gtk_champlain_embed_dispose;
  object_class->get_property = gtk_champlain_embed_get_property;
  object_class->set_property = gtk_champlain_embed_set_property;

  /**
   * GtkChamplainEmbed:champlain-view:
   *
   * The #ChamplainView to embed in the Gtk+ widget.
   *
   * Since: 0.4
   */
  g_object_class_install_property (object_class,
      PROP_VIEW,
      g_param_spec_object ("champlain-view",
          "Champlain view",
          "The ChamplainView to embed into the Gtk+ widget",
          CHAMPLAIN_TYPE_VIEW,
          G_PARAM_READABLE));
}


static void
set_view (GtkChamplainEmbed *embed,
    ChamplainView *view)
{
  GtkChamplainEmbedPrivate *priv = embed->priv;
  ClutterActor *stage;

  stage = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->clutter_embed));

  if (priv->view != NULL)
    clutter_actor_remove_child (stage, CLUTTER_ACTOR (priv->view));

  priv->view = view;
  clutter_actor_set_size (CLUTTER_ACTOR (priv->view), priv->width, priv->height);

  clutter_actor_add_child (stage, CLUTTER_ACTOR (priv->view));
}


static void
gtk_champlain_embed_init (GtkChamplainEmbed *embed)
{
  GtkChamplainEmbedPrivate *priv = gtk_champlain_embed_get_instance_private (embed);
  ClutterActor *stage;
  GdkDisplay *display;

  embed->priv = priv;

  priv->clutter_embed = gtk_clutter_embed_new ();

  g_signal_connect (priv->clutter_embed,
      "size-allocate",
      G_CALLBACK (view_size_allocated_cb),
      embed);
  g_signal_connect (priv->clutter_embed,
      "realize",
      G_CALLBACK (view_realize_cb),
      embed);
  g_signal_connect (priv->clutter_embed,
      "button-press-event",
      G_CALLBACK (mouse_button_cb),
      embed);
  g_signal_connect (priv->clutter_embed,
      "button-release-event",
      G_CALLBACK (mouse_button_cb),
      embed);
  /* Setup cursors */
  display = gdk_display_get_default ();
  priv->cursor_hand_open = gdk_cursor_new_for_display (display, GDK_HAND1);
  priv->cursor_hand_closed = gdk_cursor_new_for_display (display, GDK_FLEUR);

  priv->view = NULL;
  set_view (embed, CHAMPLAIN_VIEW (champlain_view_new ()));

  /* Setup focus/key-press events */
  g_signal_connect (embed, "focus-in-event",
                    G_CALLBACK (embed_focus_cb),
                    NULL);
  stage = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (priv->clutter_embed));
  g_signal_connect (stage, "key-press-event",
                    G_CALLBACK (stage_key_press_cb),
                    embed);
  gtk_widget_set_can_focus (GTK_WIDGET (embed), TRUE);

  gtk_container_add (GTK_CONTAINER (embed), priv->clutter_embed);
}


#if (GTK_MAJOR_VERSION == 2)
static void
gdk_to_clutter_color (GdkColor *gdk_color,
    ClutterColor *color)
{
  color->red = CLAMP ((gdk_color->red / 65535.0) * 255, 0, 255);
  color->green = CLAMP ((gdk_color->green / 65535.0) * 255, 0, 255);
  color->blue = CLAMP ((gdk_color->blue / 65535.0) * 255, 0, 255);
  color->alpha = 255;
}
#else
static void
gdk_rgba_to_clutter_color (GdkRGBA *gdk_rgba_color,
    ClutterColor *color)
{
  color->red = CLAMP (gdk_rgba_color->red * 255, 0, 255);
  color->green = CLAMP (gdk_rgba_color->green * 255, 0, 255);
  color->blue = CLAMP (gdk_rgba_color->blue * 255, 0, 255);
  color->alpha = CLAMP (gdk_rgba_color->alpha * 255, 0, 255);
}

static void
get_background_color (GtkStyleContext *context,
    GtkStateFlags state,
    GdkRGBA *color)
{
  GdkRGBA *c;

  gtk_style_context_get (context, state, "background-color", &c, NULL);
  *color = *c;
  gdk_rgba_free (c);
}
#endif


static void
view_realize_cb (GtkWidget *widget,
    GtkChamplainEmbed *view)
{
  ClutterColor color = { 0, 0, 0, };
  GtkChamplainEmbedPrivate *priv = view->priv;

#if (GTK_MAJOR_VERSION == 2)
  GtkStyle *style;

  /* Set selection color */
  style = gtk_widget_get_style (widget);

  gdk_to_clutter_color (&style->text[GTK_STATE_SELECTED], &color);
  if (color.alpha == 0 && color.red == 0 && color.green == 0 && color.blue == 0)
    {
      color.red = 255;
      color.green = 255;
      color.blue = 255;
    }
  champlain_marker_set_selection_text_color (&color);

  gdk_to_clutter_color (&style->bg[GTK_STATE_SELECTED], &color);
  if (color.alpha == 0)
    color.alpha = 255;
  if (color.red == 0 && color.green == 0 && color.blue == 0)
    {
      color.red = 75;
      color.green = 105;
      color.blue = 131;
    }
  champlain_marker_set_selection_color (&color);
#else
  GtkStyleContext *style;
  GdkRGBA gdk_rgba_color;

  /* Set selection color */
  style = gtk_widget_get_style_context (widget);
  gtk_style_context_save (style);
  gtk_style_context_set_state (style, GTK_STATE_FLAG_SELECTED);

  gtk_style_context_get_color (style, gtk_style_context_get_state (style),
                               &gdk_rgba_color);
  gdk_rgba_to_clutter_color (&gdk_rgba_color, &color);
  if (color.alpha == 0 && color.red == 0 && color.green == 0 && color.blue == 0)
    {
      color.red = 255;
      color.green = 255;
      color.blue = 255;
    }
  champlain_marker_set_selection_text_color (&color);

  get_background_color (style, gtk_style_context_get_state (style),
                        &gdk_rgba_color);
  gdk_rgba_to_clutter_color (&gdk_rgba_color, &color);
  if (color.alpha == 0)
    color.alpha = 255;
  if (color.red == 0 && color.green == 0 && color.blue == 0)
    {
      color.red = 75;
      color.green = 105;
      color.blue = 131;
    }
  champlain_marker_set_selection_color (&color);

  gtk_style_context_restore (style);
#endif

  /* Setup mouse cursor to a hand */
  gdk_window_set_cursor (gtk_widget_get_window (priv->clutter_embed), priv->cursor_hand_open);
}


static void
view_size_allocated_cb (GtkWidget *widget,
    GtkAllocation *allocation,
    GtkChamplainEmbed *view)
{
  GtkChamplainEmbedPrivate *priv = view->priv;

  if (priv->view != NULL)
    clutter_actor_set_size (CLUTTER_ACTOR (priv->view), allocation->width, allocation->height);

  priv->width = allocation->width;
  priv->height = allocation->height;
}


static gboolean
mouse_button_cb (GtkWidget *widget,
    GdkEventButton *event,
    GtkChamplainEmbed *view)
{
  GtkChamplainEmbedPrivate *priv = view->priv;

  if (event->type == GDK_BUTTON_PRESS)
    gdk_window_set_cursor (gtk_widget_get_window (priv->clutter_embed),
        priv->cursor_hand_closed);
  else
    gdk_window_set_cursor (gtk_widget_get_window (priv->clutter_embed),
        priv->cursor_hand_open);

  return FALSE;
}

static gboolean
embed_focus_cb (GtkChamplainEmbed *embed,
    GdkEvent *event)
{
  GtkChamplainEmbedPrivate *priv = embed->priv;

  gtk_widget_grab_focus (priv->clutter_embed);
  return TRUE;
}

static gboolean
stage_key_press_cb (ClutterActor *actor,
    ClutterEvent *event,
    GtkChamplainEmbed *embed)
{
  ChamplainView *view = gtk_champlain_embed_get_view (embed);

  clutter_actor_event (CLUTTER_ACTOR (view), event, FALSE);
  return TRUE;
}

/**
 * gtk_champlain_embed_new:
 *
 * Creates an instance of #GtkChamplainEmbed.
 *
 * Return value: a new #GtkChamplainEmbed ready to be used as a #GtkWidget.
 *
 * Since: 0.4
 */
GtkWidget *
gtk_champlain_embed_new ()
{
  return g_object_new (GTK_CHAMPLAIN_TYPE_EMBED, NULL);
}


/**
 * gtk_champlain_embed_get_view:
 * @embed: a #ChamplainView, the map view to embed
 *
 * Gets a #ChamplainView from the #GtkChamplainEmbed object.
 *
 * Return value: (transfer none): a #ChamplainView ready to be used
 *
 * Since: 0.4
 */
ChamplainView *
gtk_champlain_embed_get_view (GtkChamplainEmbed *embed)
{
  g_return_val_if_fail (GTK_CHAMPLAIN_IS_EMBED (embed), NULL);

  GtkChamplainEmbedPrivate *priv = embed->priv;
  return priv->view;
}