Fix view destruction upon shutdown.
[libchamplain:potyl-perl.git] / demos / launcher.c
1 /*
2  * Copyright (C) 2008 Pierre-Luc Beaudoin <pierre-luc@pierlux.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #include <champlain/champlain.h>
20 #include "markers.h"
21
22 #define PADDING 10
23
24 static gboolean
25 map_view_button_release_cb (ClutterActor *actor,
26     ClutterButtonEvent *event,
27     ChamplainView * view)
28 {
29   gdouble lat, lon;
30
31   if (event->button != 1 || event->click_count > 1)
32     return FALSE;
33
34   if (champlain_view_get_coords_from_event (view, (ClutterEvent*)event, &lat,
35          &lon))
36     g_print("Map clicked at %f, %f \n", lat, lon);
37
38   return TRUE;
39 }
40
41 static gboolean
42 zoom_in (ClutterActor *actor,
43     ClutterButtonEvent *event,
44     ChamplainView * view)
45 {
46   champlain_view_zoom_in (view);
47   return TRUE;
48 }
49
50 static gboolean
51 zoom_out (ClutterActor *actor,
52     ClutterButtonEvent *event,
53     ChamplainView * view)
54 {
55   champlain_view_zoom_out (view);
56   return TRUE;
57 }
58
59 static ClutterActor *
60 make_button (char *text)
61 {
62   ClutterActor *button, *button_bg, *button_text;
63   ClutterColor white = { 0xff, 0xff, 0xff, 0xff };
64   ClutterColor black = { 0x00, 0x00, 0x00, 0xff };
65   gfloat width, height;
66
67   button = clutter_group_new ();
68
69   button_bg = clutter_rectangle_new_with_color (&white);
70   clutter_container_add_actor (CLUTTER_CONTAINER (button), button_bg);
71   clutter_actor_set_opacity (button_bg, 0xcc);
72
73   button_text = clutter_text_new_full ("Sans 10", text, &black);
74   clutter_container_add_actor (CLUTTER_CONTAINER (button), button_text);
75   clutter_actor_get_size (button_text, &width, &height);
76
77   clutter_actor_set_size (button_bg, width + PADDING * 2, height + PADDING * 2);
78   clutter_actor_set_position (button_bg, 0, 0);
79   clutter_actor_set_position (button_text, PADDING, PADDING);
80
81   return button;
82 }
83
84 int
85 main (int argc,
86     char *argv[])
87 {
88   ClutterActor* actor, *stage, *buttons, *button;
89   ChamplainLayer *layer;
90   gfloat width, total_width = 0;;
91
92   g_thread_init (NULL);
93   clutter_init (&argc, &argv);
94
95   stage = clutter_stage_get_default ();
96   clutter_actor_set_size (stage, 800, 600);
97
98   /* Create the map view */
99   actor = champlain_view_new ();
100   clutter_actor_set_size (CLUTTER_ACTOR (actor), 800, 600);
101   clutter_container_add_actor (CLUTTER_CONTAINER (stage), actor);
102
103   /* Create the buttons */
104   buttons = clutter_group_new ();
105   clutter_actor_set_position (buttons, PADDING, PADDING);
106
107   button = make_button ("Zoom in");
108   clutter_container_add_actor (CLUTTER_CONTAINER (buttons), button);
109   clutter_actor_set_reactive (button, TRUE);
110   clutter_actor_get_size (button, &width, NULL);
111   total_width += width + PADDING;
112   g_signal_connect (button, "button-release-event",
113       G_CALLBACK (zoom_in),
114       actor);
115
116   button = make_button ("Zoom out");
117   clutter_container_add_actor (CLUTTER_CONTAINER (buttons), button);
118   clutter_actor_set_reactive (button, TRUE);
119   clutter_actor_set_position (button, total_width, 0);
120   clutter_actor_get_size (button, &width, NULL);
121   total_width += width + PADDING;
122   g_signal_connect (button, "button-release-event",
123       G_CALLBACK (zoom_out),
124       actor);
125
126   clutter_container_add_actor (CLUTTER_CONTAINER (stage), buttons);
127
128   /* Create the markers and marker layer */
129   layer = create_marker_layer (CHAMPLAIN_VIEW (actor));
130   champlain_view_add_layer (CHAMPLAIN_VIEW (actor), layer);
131
132   /* Connect to the click event */
133   clutter_actor_set_reactive (actor, TRUE);
134   g_signal_connect (actor, "button-release-event",
135       G_CALLBACK (map_view_button_release_cb),
136       actor);
137
138   /* Finish initialising the map view */
139   g_object_set (G_OBJECT (actor), "zoom-level", 12,
140       "scroll-mode", CHAMPLAIN_SCROLL_MODE_KINETIC, NULL);
141   champlain_view_center_on(CHAMPLAIN_VIEW(actor), 45.466, -73.75);
142
143   clutter_actor_show (stage);
144   clutter_main ();
145
146   clutter_actor_destroy (actor);
147   return 0;
148 }