2 * This file is a part of hildon
4 * Copyright (C) 2008 Nokia Corporation, all rights reserved.
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.
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.
18 * SECTION:hildon-text-view
19 * @short_description: Widget representing a text view in the Hildon framework.
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.
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.
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.
39 * <title>Creating a HildonTextView with a placeholder</title>
42 * create_text_view (void)
44 * GtkWidget *text_view;
46 * text_view = hildon_text_view_new ();
47 * hildon_text_view_set_placeholder (HILDON_TEXT_VIEW (text_view),
48 * "Type some text here");
56 #include "hildon-text-view.h"
59 #define HILDON_TEXT_VIEW_DRAG_THRESHOLD 16.0
61 G_DEFINE_TYPE (HildonTextView, hildon_text_view, GTK_TYPE_TEXT_VIEW);
63 #define HILDON_TEXT_VIEW_GET_PRIVATE(obj) \
64 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
65 HILDON_TYPE_TEXT_VIEW, HildonTextViewPrivate));
67 typedef struct _HildonTextViewPrivate HildonTextViewPrivate;
69 struct _HildonTextViewPrivate
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 */
78 /* Function used to decide whether to show the placeholder or not */
80 hildon_text_view_refresh_contents (GtkWidget *text_view)
82 HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (text_view);
83 gint bufsize = gtk_text_buffer_get_char_count (priv->main_buffer);
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);
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);
97 * hildon_text_view_set_buffer:
98 * @text_view: a #HildonTextView
99 * @buffer: a #GtkTextBuffer
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
107 * Note that you must never use gtk_text_view_set_buffer() to set the
108 * buffer of a #HildonTextView.
113 hildon_text_view_set_buffer (HildonTextView *text_view,
114 GtkTextBuffer *buffer)
116 HildonTextViewPrivate *priv;
118 g_return_if_fail (HILDON_IS_TEXT_VIEW (text_view));
119 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
121 priv = HILDON_TEXT_VIEW_GET_PRIVATE (text_view);
123 /* If this is the same buffer, don't do anything */
124 if (buffer == priv->main_buffer)
127 /* Disconnect the signal handler from the old buffer */
128 g_signal_handler_disconnect (priv->main_buffer, priv->changed_id);
130 /* Replace the old buffer with the new one */
131 g_object_unref (priv->main_buffer);
132 priv->main_buffer = g_object_ref (buffer);
134 /* Attach a callback to the new text buffer */
136 g_signal_connect_swapped (priv->main_buffer, "changed",
137 G_CALLBACK (hildon_text_view_refresh_contents), text_view);
139 /* Refresh textview contents */
140 hildon_text_view_refresh_contents (GTK_WIDGET (text_view));
144 * hildon_text_view_get_buffer:
145 * @text_view: a #HildonTextView
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.
150 * Note that you must never use gtk_text_view_get_buffer() to get the
151 * buffer from a #HildonTextView.
153 * Also note that placeholder text (set using
154 * hildon_text_view_set_placeholder()) is never contained in this
157 * Returns: a #GtkTextBuffer
162 hildon_text_view_get_buffer (HildonTextView *text_view)
164 HildonTextViewPrivate *priv;
166 g_return_val_if_fail (HILDON_IS_TEXT_VIEW (text_view), NULL);
168 priv = HILDON_TEXT_VIEW_GET_PRIVATE (text_view);
170 /* Always return priv->main_buffer even if the placeholder is
172 return priv->main_buffer;
176 * hildon_text_view_set_placeholder:
177 * @text_view: a #HildonTextView
178 * @text: the new text
180 * Sets the placeholder text in @text_view to @text.
185 hildon_text_view_set_placeholder (HildonTextView *text_view,
188 HildonTextViewPrivate *priv;
190 g_return_if_fail (HILDON_IS_TEXT_VIEW (text_view) && text != NULL);
192 priv = HILDON_TEXT_VIEW_GET_PRIVATE (text_view);
194 gtk_text_buffer_set_text (priv->placeholder_buffer, text, -1);
198 * hildon_text_view_new:
200 * Creates a new text view.
202 * Returns: a new #HildonTextView
207 hildon_text_view_new (void)
209 GtkWidget *entry = g_object_new (HILDON_TYPE_TEXT_VIEW, NULL);
215 hildon_text_view_focus_in_event (GtkWidget *widget,
216 GdkEventFocus *event)
218 hildon_text_view_refresh_contents (widget);
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);
228 hildon_text_view_focus_out_event (GtkWidget *widget,
229 GdkEventFocus *event)
231 hildon_text_view_refresh_contents (widget);
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);
241 hildon_text_view_button_press_event (GtkWidget *widget,
242 GdkEventButton *event)
244 HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (widget);
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;
252 if (event->button == 1 && event->type == GDK_BUTTON_PRESS) {
263 hildon_text_view_button_release_event (GtkWidget *widget,
264 GdkEventButton *event)
266 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
267 HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (widget);
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;
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;
282 window_type = gtk_text_view_get_window_type (text_view, event->window);
283 gtk_text_view_window_to_buffer_coords (text_view,
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);
291 gtk_widget_grab_focus (GTK_WIDGET (text_view));
300 hildon_text_view_finalize (GObject *object)
302 HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (object);
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);
308 if (G_OBJECT_CLASS (hildon_text_view_parent_class)->finalize)
309 G_OBJECT_CLASS (hildon_text_view_parent_class)->finalize (object);
313 hildon_text_view_class_init (HildonTextViewClass *klass)
315 GObjectClass *gobject_class = (GObjectClass *)klass;
316 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
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;
325 g_type_class_add_private (klass, sizeof (HildonTextViewPrivate));
329 hildon_text_view_init (HildonTextView *self)
331 HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (self);
333 priv->main_buffer = gtk_text_buffer_new (NULL);
334 priv->placeholder_buffer = gtk_text_buffer_new (NULL);
336 hildon_text_view_refresh_contents (GTK_WIDGET (self));
339 g_signal_connect_swapped (priv->main_buffer, "changed",
340 G_CALLBACK (hildon_text_view_refresh_contents), self);