2009-01-13 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 #include <math.h>
58
59 #define HILDON_TEXT_VIEW_DRAG_THRESHOLD 16.0
60
61 G_DEFINE_TYPE                                   (HildonTextView, hildon_text_view, GTK_TYPE_TEXT_VIEW);
62
63 #define                                         HILDON_TEXT_VIEW_GET_PRIVATE(obj) \
64                                                 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
65                                                 HILDON_TYPE_TEXT_VIEW, HildonTextViewPrivate));
66
67 typedef struct                                  _HildonTextViewPrivate HildonTextViewPrivate;
68
69 struct                                          _HildonTextViewPrivate
70 {
71     GtkTextBuffer *main_buffer;                   /* Used to show the "real" contents */
72     GtkTextBuffer *placeholder_buffer;   /* Internal, used to display the placeholder */
73     gulong changed_id;               /* ID of the main_buffer::changed signal handler */
74     gdouble x;                                                      /* tap x position */
75     gdouble y;                                                      /* tap y position */
76 };
77
78 static const gchar *placeholder_widget_name     = "hildon-text-view-placeholder";
79
80 /* Function used to decide whether to show the placeholder or not */
81 static void
82 hildon_text_view_refresh_contents               (GtkWidget *text_view)
83 {
84     HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (text_view);
85     gint bufsize = gtk_text_buffer_get_char_count (priv->main_buffer);
86
87     if ((bufsize > 0) || GTK_WIDGET_HAS_FOCUS (text_view)) {
88         /* Display the main buffer if it contains text or the widget is focused */
89         gtk_widget_set_name (text_view, NULL);
90         gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), priv->main_buffer);
91     } else {
92         /* Otherwise, display the placeholder */
93         gtk_widget_set_name (text_view, placeholder_widget_name);
94         gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), priv->placeholder_buffer);
95     }
96 }
97
98 /**
99  * hildon_text_view_set_buffer:
100  * @text_view: a #HildonTextView
101  * @buffer: a #GtkTextBuffer
102  *
103  * Sets @buffer as the buffer being displayed by @text_view. The
104  * previous buffer displayed by the text view is unreferenced, and a
105  * reference is added to @buffer. If you owned a reference to @buffer
106  * before passing it to this function, you must remove that reference
107  * yourself
108  *
109  * Note that you must never use gtk_text_view_set_buffer() to set the
110  * buffer of a #HildonTextView.
111  *
112  * Since: 2.2
113  */
114 void
115 hildon_text_view_set_buffer                     (HildonTextView *text_view,
116                                                  GtkTextBuffer  *buffer)
117 {
118     HildonTextViewPrivate *priv;
119
120     g_return_if_fail (HILDON_IS_TEXT_VIEW (text_view));
121     g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
122
123     priv = HILDON_TEXT_VIEW_GET_PRIVATE (text_view);
124
125     /* If this is the same buffer, don't do anything */
126     if (buffer == priv->main_buffer)
127         return;
128
129     /* Disconnect the signal handler from the old buffer */
130     g_signal_handler_disconnect (priv->main_buffer, priv->changed_id);
131
132     /* Replace the old buffer with the new one */
133     g_object_unref (priv->main_buffer);
134     priv->main_buffer = g_object_ref (buffer);
135
136     /* Attach a callback to the new text buffer */
137     priv->changed_id =
138         g_signal_connect_swapped (priv->main_buffer, "changed",
139                                   G_CALLBACK (hildon_text_view_refresh_contents), text_view);
140
141     /* Refresh textview contents */
142     hildon_text_view_refresh_contents (GTK_WIDGET (text_view));
143 }
144
145 /**
146  * hildon_text_view_get_buffer:
147  * @text_view: a #HildonTextView
148  *
149  * Returns the text buffer in @text_view. The reference count is not
150  * incremented; the caller of this function won't own a new reference.
151  *
152  * Note that you must never use gtk_text_view_get_buffer() to get the
153  * buffer from a #HildonTextView.
154  *
155  * Also note that placeholder text (set using
156  * hildon_text_view_set_placeholder()) is never contained in this
157  * buffer.
158  *
159  * Returns: a #GtkTextBuffer
160  *
161  * Since: 2.2
162  */
163 GtkTextBuffer *
164 hildon_text_view_get_buffer                     (HildonTextView *text_view)
165 {
166     HildonTextViewPrivate *priv;
167
168     g_return_val_if_fail (HILDON_IS_TEXT_VIEW (text_view), NULL);
169
170     priv = HILDON_TEXT_VIEW_GET_PRIVATE (text_view);
171
172     /* Always return priv->main_buffer even if the placeholder is
173      * being displayed */
174     return priv->main_buffer;
175 }
176
177 /**
178  * hildon_text_view_set_placeholder:
179  * @text_view: a #HildonTextView
180  * @text: the new text
181  *
182  * Sets the placeholder text in @text_view to @text.
183  *
184  * Since: 2.2
185  */
186 void
187 hildon_text_view_set_placeholder                (HildonTextView *text_view,
188                                                  const gchar    *text)
189 {
190     HildonTextViewPrivate *priv;
191
192     g_return_if_fail (HILDON_IS_TEXT_VIEW (text_view) && text != NULL);
193
194     priv = HILDON_TEXT_VIEW_GET_PRIVATE (text_view);
195
196     gtk_text_buffer_set_text (priv->placeholder_buffer, text, -1);
197 }
198
199 /**
200  * hildon_text_view_new:
201  *
202  * Creates a new text view.
203  *
204  * Returns: a new #HildonTextView
205  *
206  * Since: 2.2 
207  */
208 GtkWidget *
209 hildon_text_view_new                            (void)
210 {
211     GtkWidget *entry = g_object_new (HILDON_TYPE_TEXT_VIEW, NULL);
212
213     return entry;
214 }
215
216 static gboolean
217 hildon_text_view_focus_in_event                 (GtkWidget     *widget,
218                                                  GdkEventFocus *event)
219 {
220     hildon_text_view_refresh_contents (widget);
221
222     if (GTK_WIDGET_CLASS (hildon_text_view_parent_class)->focus_in_event) {
223         return GTK_WIDGET_CLASS (hildon_text_view_parent_class)->focus_in_event (widget, event);
224     } else {
225         return FALSE;
226     }
227 }
228
229 static gboolean
230 hildon_text_view_focus_out_event                (GtkWidget     *widget,
231                                                  GdkEventFocus *event)
232 {
233     hildon_text_view_refresh_contents (widget);
234
235     if (GTK_WIDGET_CLASS (hildon_text_view_parent_class)->focus_out_event) {
236         return GTK_WIDGET_CLASS (hildon_text_view_parent_class)->focus_out_event (widget, event);
237     } else {
238         return FALSE;
239     }
240 }
241
242 static gint
243 hildon_text_view_button_press_event             (GtkWidget        *widget,
244                                                  GdkEventButton   *event)
245 {
246     HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (widget);
247
248     if (GTK_TEXT_VIEW (widget)->editable &&
249         hildon_gtk_im_context_filter_event (GTK_TEXT_VIEW (widget)->im_context, (GdkEvent*)event)) {
250         GTK_TEXT_VIEW (widget)->need_im_reset = TRUE;
251         return TRUE;
252     }
253
254     if (event->button == 1 && event->type == GDK_BUTTON_PRESS) {
255         priv->x = event->x;
256         priv->y = event->y;
257
258         return TRUE;
259     }
260
261     return FALSE;
262 }
263
264 static gint
265 hildon_text_view_button_release_event           (GtkWidget        *widget,
266                                                  GdkEventButton   *event)
267 {
268     GtkTextView *text_view = GTK_TEXT_VIEW (widget);
269     HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (widget);
270     GtkTextIter iter;
271     gint x, y;
272
273     if (text_view->editable &&
274         hildon_gtk_im_context_filter_event (text_view->im_context, (GdkEvent*)event)) {
275         text_view->need_im_reset = TRUE;
276         return TRUE;
277     }
278
279     if (event->button == 1 && event->type == GDK_BUTTON_RELEASE) {
280         if (fabs (priv->x - event->x) < HILDON_TEXT_VIEW_DRAG_THRESHOLD &&
281             fabs (priv->y - event->y) < HILDON_TEXT_VIEW_DRAG_THRESHOLD) {
282             GtkTextWindowType window_type;
283
284             window_type = gtk_text_view_get_window_type (text_view, event->window);
285             gtk_text_view_window_to_buffer_coords (text_view,
286                                                    window_type,
287                                                    event->x, event->y,
288                                                    &x, &y);
289             gtk_text_view_get_iter_at_location (text_view, &iter, x, y);
290             if (gtk_text_buffer_get_char_count (priv->main_buffer))
291                 gtk_text_buffer_place_cursor (priv->main_buffer, &iter);
292
293             gtk_widget_grab_focus (GTK_WIDGET (text_view));
294
295             return TRUE;
296         }
297     }
298     return FALSE;
299 }
300
301 static void
302 hildon_text_view_finalize                       (GObject *object)
303 {
304     HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (object);
305
306     g_signal_handler_disconnect (priv->main_buffer, priv->changed_id);
307     g_object_unref (priv->main_buffer);
308     g_object_unref (priv->placeholder_buffer);
309
310     if (G_OBJECT_CLASS (hildon_text_view_parent_class)->finalize)
311         G_OBJECT_CLASS (hildon_text_view_parent_class)->finalize (object);
312 }
313
314 static void
315 hildon_text_view_class_init                     (HildonTextViewClass *klass)
316 {
317     GObjectClass *gobject_class = (GObjectClass *)klass;
318     GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
319
320     gobject_class->finalize = hildon_text_view_finalize;
321     widget_class->focus_in_event = hildon_text_view_focus_in_event;
322     widget_class->focus_out_event = hildon_text_view_focus_out_event;
323     widget_class->motion_notify_event = NULL;
324     widget_class->button_press_event = hildon_text_view_button_press_event;
325     widget_class->button_release_event = hildon_text_view_button_release_event;
326
327     g_type_class_add_private (klass, sizeof (HildonTextViewPrivate));
328 }
329
330 static void
331 hildon_text_view_init                           (HildonTextView *self)
332 {
333     HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (self);
334
335     priv->main_buffer = gtk_text_buffer_new (NULL);
336     priv->placeholder_buffer = gtk_text_buffer_new (NULL);
337
338     hildon_text_view_refresh_contents (GTK_WIDGET (self));
339
340     priv->changed_id =
341         g_signal_connect_swapped (priv->main_buffer, "changed",
342                                   G_CALLBACK (hildon_text_view_refresh_contents), self);
343 }