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