aboutsummaryrefslogtreecommitdiff
path: root/examples/hildon-touch-selector-example.c
blob: 996c5fe065b7c070a7736c14ca17100c8f1fd3a9 (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
/*
 * This file is a part of hildon examples
 *
 * Copyright (C) 2008 Nokia Corporation, all rights reserved.
 *
 *
 * 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; 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
 *
 */

#include                                        <glib.h>
#include                                        <gtk/gtk.h>
#include                                        <hildon/hildon.h>
#include                                        <stdlib.h>

static GtkWidget *create_selector ();
static GtkWidget *get_visible_content (GtkWidget * window);

static GtkWindow *parent_window = NULL;

static GtkWidget *label = NULL;

static HildonTouchSelectorSelectionMode mode;

static void
value_changed (HildonPickerButton * button,
               gpointer user_data)
{
  HildonTouchSelector *selector;
  gchar *aux_string = NULL;

  selector = hildon_picker_button_get_selector (button);
  aux_string = hildon_touch_selector_get_current_text (selector);
  gtk_label_set_text (GTK_LABEL (label), aux_string);
  g_free (aux_string);
}

static GtkWidget *
create_selector ()
{
  GtkWidget *selector = NULL;
  GSList *icon_list = NULL;
  GtkListStore *store_icons = NULL;
  GSList *item = NULL;
  GtkCellRenderer *renderer = NULL;
  HildonTouchSelectorColumn *column = NULL;

  selector = hildon_touch_selector_new ();

  icon_list = gtk_stock_list_ids ();

  store_icons = gtk_list_store_new (1, G_TYPE_STRING);
  for (item = icon_list; item; item = g_slist_next (item)) {
    GtkTreeIter iter;
    gchar *label = item->data;

    gtk_list_store_append (store_icons, &iter);
    gtk_list_store_set (store_icons, &iter, 0, label, -1);
    g_free (label);
  }
  g_slist_free (icon_list);

  renderer = gtk_cell_renderer_pixbuf_new ();
  gtk_cell_renderer_set_fixed_size (renderer, -1, 100);

  column = hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector),
                                                GTK_TREE_MODEL (store_icons),
                                                renderer, "stock-id", 0, NULL);
  hildon_touch_selector_column_set_text_column (column, 0);

  g_object_unref (store_icons);

  hildon_touch_selector_set_column_selection_mode (HILDON_TOUCH_SELECTOR (selector),
                                                   mode);

  return selector;
}

static GtkWidget *
get_visible_content (GtkWidget * window)
{
  GtkWidget *result = NULL;
  GtkWidget *button = NULL;
  GtkWidget *selector;

  label = gtk_label_new ("Here we are going to put the selection");

  button = hildon_picker_button_new (HILDON_SIZE_AUTO, HILDON_BUTTON_ARRANGEMENT_VERTICAL);
  hildon_button_set_title (HILDON_BUTTON (button), "Click me..");
  selector = create_selector ();
  hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (button),
                                     HILDON_TOUCH_SELECTOR (selector));

  g_signal_connect (G_OBJECT (button), "value-changed",
                    G_CALLBACK (value_changed), window);

  result = gtk_vbox_new (FALSE, 6);

  gtk_container_add (GTK_CONTAINER (result), button);
  gtk_container_add (GTK_CONTAINER (result), label);

  return result;
}

int
main (int argc, char **argv)
{
  HildonProgram *program = NULL;
  GtkWidget *window = NULL;

  hildon_gtk_init (&argc, &argv);

  program = hildon_program_get_instance ();
  g_set_application_name
    ("hildon-touch-selector cell renderer example program");

  mode = (argc >= 2 && atoi (argv[1]) == 1) ?
    HILDON_TOUCH_SELECTOR_SELECTION_MODE_SINGLE:
    HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE;

  window = hildon_stackable_window_new ();
  parent_window = GTK_WINDOW (window);
  hildon_program_add_window (program, HILDON_WINDOW (window));

  gtk_container_set_border_width (GTK_CONTAINER (window), 6);

  GtkWidget *vbox = get_visible_content (window);

  gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (vbox));

  g_signal_connect (G_OBJECT (window), "destroy",
                    G_CALLBACK (gtk_main_quit), NULL);
  gtk_widget_show_all (GTK_WIDGET (window));

  gtk_main ();

  return 0;
}