2008-12-12 Claudio Saavedra <csaavedra@igalia.com>
[hildon] / src / hildon-text-view.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser Public License as published by
8  * the Free Software Foundation; version 2 of the license.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser Public License for more details.
14  *
15  */
16
17 /**
18  * SECTION:hildon-text-view
19  * @short_description: Widget representing a text view in the Hildon framework.
20  *
21  * The #HildonTextView is a GTK widget which represents a text
22  * view. It is derived from the #GtkTextView widget and provides
23  * additional commodities specific to the Hildon framework.
24  *
25  * Besides all the features inherited from #GtkTextView, a
26  * #HildonTextView can also have a placeholder text. This text will be
27  * shown if the text view is empty and doesn't have the input focus,
28  * but it's otherwise ignored. Thus, calls to
29  * hildon_text_view_get_buffer() will never return the placeholder
30  * text, not even when it's being displayed.
31  *
32  * Although #HildonTextView is derived from #GtkTextView,
33  * gtk_text_view_get_buffer() and gtk_text_view_set_buffer() must
34  * never be used to get/set the buffer in this
35  * widget. hildon_text_view_get_buffer() and
36  * hildon_text_view_set_buffer() must be used instead.
37  *
38  * <example>
39  * <title>Creating a HildonTextView with a placeholder</title>
40  * <programlisting>
41  * GtkWidget *
42  * create_text_view (void)
43  * {
44  *     GtkWidget *text_view;
45  * <!-- -->
46  *     text_view = hildon_text_view_new ();
47  *     hildon_text_view_set_placeholder (HILDON_TEXT_VIEW (text_view),
48  *                                       "Type some text here");
49  * <!-- -->
50  *     return text_view;
51  * }
52  * </programlisting>
53  * </example>
54  */
55
56 #include                                        "hildon-text-view.h"
57
58 G_DEFINE_TYPE                                   (HildonTextView, hildon_text_view, GTK_TYPE_TEXT_VIEW);
59
60 #define                                         HILDON_TEXT_VIEW_GET_PRIVATE(obj) \
61                                                 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
62                                                 HILDON_TYPE_TEXT_VIEW, HildonTextViewPrivate));
63
64 typedef struct                                  _HildonTextViewPrivate HildonTextViewPrivate;
65
66 struct                                          _HildonTextViewPrivate
67 {
68     GtkTextBuffer *main_buffer;                   /* Used to show the "real" contents */
69     GtkTextBuffer *placeholder_buffer;   /* Internal, used to display the placeholder */
70     gulong changed_id;               /* ID of the main_buffer::changed signal handler */
71 };
72
73 static const gchar *placeholder_widget_name     = "hildon-text-view-placeholder";
74
75 /* Function used to decide whether to show the placeholder or not */
76 static void
77 hildon_text_view_refresh_contents               (GtkWidget *text_view)
78 {
79     HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (text_view);
80     gint bufsize = gtk_text_buffer_get_char_count (priv->main_buffer);
81
82     if ((bufsize > 0) || GTK_WIDGET_HAS_FOCUS (text_view)) {
83         /* Display the main buffer if it contains text or the widget is focused */
84         gtk_widget_set_name (text_view, NULL);
85         gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), priv->main_buffer);
86     } else {
87         /* Otherwise, display the placeholder */
88         gtk_widget_set_name (text_view, placeholder_widget_name);
89         gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), priv->placeholder_buffer);
90     }
91 }
92
93 /**
94  * hildon_text_view_set_buffer:
95  * @text_view: a #HildonTextView
96  * @buffer: a #GtkTextBuffer
97  *
98  * Sets @buffer as the buffer being displayed by @text_view. The
99  * previous buffer displayed by the text view is unreferenced, and a
100  * reference is added to @buffer. If you owned a reference to @buffer
101  * before passing it to this function, you must remove that reference
102  * yourself
103  *
104  * Note that you must never use gtk_text_view_set_buffer() to set the
105  * buffer of a #HildonTextView.
106  *
107  * Since: 2.2
108  */
109 void
110 hildon_text_view_set_buffer                     (HildonTextView *text_view,
111                                                  GtkTextBuffer  *buffer)
112 {
113     HildonTextViewPrivate *priv;
114
115     g_return_if_fail (HILDON_IS_TEXT_VIEW (text_view));
116     g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
117
118     priv = HILDON_TEXT_VIEW_GET_PRIVATE (text_view);
119
120     /* If this is the same buffer, don't do anything */
121     if (buffer == priv->main_buffer)
122         return;
123
124     /* Disconnect the signal handler from the old buffer */
125     g_signal_handler_disconnect (priv->main_buffer, priv->changed_id);
126
127     /* Replace the old buffer with the new one */
128     g_object_unref (priv->main_buffer);
129     priv->main_buffer = g_object_ref (buffer);
130
131     /* Attach a callback to the new text buffer */
132     priv->changed_id =
133         g_signal_connect_swapped (priv->main_buffer, "changed",
134                                   G_CALLBACK (hildon_text_view_refresh_contents), text_view);
135
136     /* Refresh textview contents */
137     hildon_text_view_refresh_contents (GTK_WIDGET (text_view));
138 }
139
140 /**
141  * hildon_text_view_get_buffer:
142  * @text_view: a #HildonTextView
143  *
144  * Returns the text buffer in @text_view. The reference count is not
145  * incremented; the caller of this function won't own a new reference.
146  *
147  * Note that you must never use gtk_text_view_get_buffer() to get the
148  * buffer from a #HildonTextView.
149  *
150  * Also note that placeholder text (set using
151  * hildon_text_view_set_placeholder()) is never contained in this
152  * buffer.
153  *
154  * Returns: a #GtkTextBuffer
155  *
156  * Since: 2.2
157  */
158 GtkTextBuffer *
159 hildon_text_view_get_buffer                     (HildonTextView *text_view)
160 {
161     HildonTextViewPrivate *priv;
162
163     g_return_val_if_fail (HILDON_IS_TEXT_VIEW (text_view), NULL);
164
165     priv = HILDON_TEXT_VIEW_GET_PRIVATE (text_view);
166
167     /* Always return priv->main_buffer even if the placeholder is
168      * being displayed */
169     return priv->main_buffer;
170 }
171
172 /**
173  * hildon_text_view_set_placeholder:
174  * @text_view: a #HildonTextView
175  * @text: the new text
176  *
177  * Sets the placeholder text in @text_view to @text.
178  *
179  * Since: 2.2
180  */
181 void
182 hildon_text_view_set_placeholder                (HildonTextView *text_view,
183                                                  const gchar    *text)
184 {
185     HildonTextViewPrivate *priv;
186
187     g_return_if_fail (HILDON_IS_TEXT_VIEW (text_view) && text != NULL);
188
189     priv = HILDON_TEXT_VIEW_GET_PRIVATE (text_view);
190
191     gtk_text_buffer_set_text (priv->placeholder_buffer, text, -1);
192 }
193
194 /**
195  * hildon_text_view_new:
196  *
197  * Creates a new text view.
198  *
199  * Returns: a new #HildonTextView
200  *
201  * Since: 2.2 
202  */
203 GtkWidget *
204 hildon_text_view_new                            (void)
205 {
206     GtkWidget *entry = g_object_new (HILDON_TYPE_TEXT_VIEW, NULL);
207
208     return entry;
209 }
210
211 static gboolean
212 hildon_text_view_focus_in_event                 (GtkWidget     *widget,
213                                                  GdkEventFocus *event)
214 {
215     hildon_text_view_refresh_contents (widget);
216
217     if (GTK_WIDGET_CLASS (hildon_text_view_parent_class)->focus_in_event) {
218         return GTK_WIDGET_CLASS (hildon_text_view_parent_class)->focus_in_event (widget, event);
219     } else {
220         return FALSE;
221     }
222 }
223
224 static gboolean
225 hildon_text_view_focus_out_event                (GtkWidget     *widget,
226                                                  GdkEventFocus *event)
227 {
228     hildon_text_view_refresh_contents (widget);
229
230     if (GTK_WIDGET_CLASS (hildon_text_view_parent_class)->focus_out_event) {
231         return GTK_WIDGET_CLASS (hildon_text_view_parent_class)->focus_out_event (widget, event);
232     } else {
233         return FALSE;
234     }
235 }
236
237 static void
238 hildon_text_view_finalize                       (GObject *object)
239 {
240     HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (object);
241
242     g_signal_handler_disconnect (priv->main_buffer, priv->changed_id);
243     g_object_unref (priv->main_buffer);
244     g_object_unref (priv->placeholder_buffer);
245
246     if (G_OBJECT_CLASS (hildon_text_view_parent_class)->finalize)
247         G_OBJECT_CLASS (hildon_text_view_parent_class)->finalize (object);
248 }
249
250 static void
251 hildon_text_view_class_init                     (HildonTextViewClass *klass)
252 {
253     GObjectClass *gobject_class = (GObjectClass *)klass;
254     GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
255
256     gobject_class->finalize = hildon_text_view_finalize;
257     widget_class->focus_in_event = hildon_text_view_focus_in_event;
258     widget_class->focus_out_event = hildon_text_view_focus_out_event;
259
260     g_type_class_add_private (klass, sizeof (HildonTextViewPrivate));
261 }
262
263 static void
264 hildon_text_view_init                           (HildonTextView *self)
265 {
266     HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (self);
267
268     priv->main_buffer = gtk_text_buffer_new (NULL);
269     priv->placeholder_buffer = gtk_text_buffer_new (NULL);
270
271     hildon_text_view_refresh_contents (GTK_WIDGET (self));
272
273     priv->changed_id =
274         g_signal_connect_swapped (priv->main_buffer, "changed",
275                                   G_CALLBACK (hildon_text_view_refresh_contents), self);
276 }