aboutsummaryrefslogtreecommitdiff
path: root/examples/hildon-text-view-example.c
blob: 4e2a16b5f09e1318188f29337ad1bff291bfa2e2 (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
/*
 * This file is a part of hildon examples
 *
 * Copyright (C) 2008, 2009 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                                        <hildon/hildon.h>

GtkTextView *textview;
GtkTextBuffer *buffer;

static void
set_text_button_clicked                         (GtkButton *button,
                                                 GtkEntry  *entry)
{
    gtk_text_buffer_set_text (buffer, gtk_entry_get_text (entry), -1);
}

static void
set_placeholder_button_clicked                  (GtkButton *button,
                                                 GtkEntry  *entry)
{
    hildon_gtk_text_view_set_placeholder_text (textview, gtk_entry_get_text (entry));
}

static void
text_changed                                    (GtkTextBuffer *buffer,
                                                 GtkLabel      *label)
{
    const gchar *text;
    GtkTextIter start, end;

    gtk_text_buffer_get_start_iter (buffer, &start);
    gtk_text_buffer_get_end_iter (buffer, &end);

    text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);

    if (text != NULL && *text != '\0') {
        gtk_label_set_text (label, text);
    } else {
        gtk_label_set_text (label, "(empty)");
    }
}

int
main                                            (int    argc,
                                                 char **argv)
{
    GtkWidget *win;
    GtkWidget *label;
    GtkWidget *textentry, *textbutton, *texthbox;
    GtkWidget *placeholderentry, *placeholderbutton, *placeholderhbox;
    GtkWidget *textviewframe;
    GtkBox *vbox;

    hildon_gtk_init (&argc, &argv);

    /* Window and vbox to pack everything */
    win = hildon_stackable_window_new ();
    vbox = GTK_BOX (gtk_vbox_new (FALSE, 10));

    /* Entry to modify the text of the main HildonEntry */
    textentry = hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
    textbutton = hildon_gtk_button_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
    gtk_button_set_label (GTK_BUTTON (textbutton), "Set text view text");
    texthbox = gtk_hbox_new (FALSE, 10);

    /* Entry to modify the placeholder of the main HildonEntry */
    placeholderentry = hildon_entry_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
    placeholderbutton = hildon_gtk_button_new (HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
    gtk_button_set_label (GTK_BUTTON (placeholderbutton), "Set text view placeholder");
    placeholderhbox = gtk_hbox_new (FALSE, 10);

    /* The text view */
    textview = GTK_TEXT_VIEW (hildon_text_view_new ());
    buffer = gtk_text_view_get_buffer (textview);
    hildon_gtk_text_view_set_placeholder_text (textview, "This is a placeholder - change using the buttons above");
    textviewframe = gtk_frame_new (NULL);

    /* This label is used to show the contents -not the placeholder- of the HildonTextView */
    label = gtk_label_new (NULL);

    /* Pack all widgets */
    gtk_box_pack_start (GTK_BOX (texthbox), textentry, TRUE, TRUE, 0);
    gtk_box_pack_start (GTK_BOX (texthbox), textbutton, FALSE, FALSE, 0);

    gtk_box_pack_start (GTK_BOX (placeholderhbox), placeholderentry, TRUE, TRUE, 0);
    gtk_box_pack_start (GTK_BOX (placeholderhbox), placeholderbutton, FALSE, FALSE, 0);

    gtk_container_add (GTK_CONTAINER (textviewframe), GTK_WIDGET (textview));

    gtk_box_pack_start (vbox, texthbox, FALSE, FALSE, 0);
    gtk_box_pack_start (vbox, placeholderhbox, FALSE, FALSE, 0);
    gtk_box_pack_start (vbox, textviewframe, TRUE, TRUE, 0);
    gtk_box_pack_start (vbox, gtk_label_new ("Contents of the text view:"), TRUE, TRUE, 0);
    gtk_box_pack_start (vbox, label, TRUE, TRUE, 0);

    gtk_container_set_border_width (GTK_CONTAINER (win), 20);
    gtk_container_add (GTK_CONTAINER (win), GTK_WIDGET (vbox));

    /* Connect signals */
    g_signal_connect (win, "destroy", G_CALLBACK (gtk_main_quit), NULL);
    g_signal_connect (buffer, "changed", G_CALLBACK (text_changed), label);
    g_signal_connect (textbutton, "clicked",
                      G_CALLBACK (set_text_button_clicked), textentry);
    g_signal_connect (placeholderbutton, "clicked",
                      G_CALLBACK (set_placeholder_button_clicked), placeholderentry);

    /* Set the initial state of the label */
    text_changed (buffer, GTK_LABEL (label));

    /* Run example */
    gtk_widget_show_all (win);
    gtk_main ();

    return 0;
}