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 static const gchar *placeholder_widget_name = "hildon-text-view-placeholder";
80 /* Function used to decide whether to show the placeholder or not */
82 hildon_text_view_refresh_contents (GtkWidget *text_view)
84 HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (text_view);
85 gint bufsize = gtk_text_buffer_get_char_count (priv->main_buffer);
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);
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);
99 * hildon_text_view_set_buffer:
100 * @text_view: a #HildonTextView
101 * @buffer: a #GtkTextBuffer
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
109 * Note that you must never use gtk_text_view_set_buffer() to set the
110 * buffer of a #HildonTextView.
115 hildon_text_view_set_buffer (HildonTextView *text_view,
116 GtkTextBuffer *buffer)
118 HildonTextViewPrivate *priv;
120 g_return_if_fail (HILDON_IS_TEXT_VIEW (text_view));
121 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
123 priv = HILDON_TEXT_VIEW_GET_PRIVATE (text_view);
125 /* If this is the same buffer, don't do anything */
126 if (buffer == priv->main_buffer)
129 /* Disconnect the signal handler from the old buffer */
130 g_signal_handler_disconnect (priv->main_buffer, priv->changed_id);
132 /* Replace the old buffer with the new one */
133 g_object_unref (priv->main_buffer);
134 priv->main_buffer = g_object_ref (buffer);
136 /* Attach a callback to the new text buffer */
138 g_signal_connect_swapped (priv->main_buffer, "changed",
139 G_CALLBACK (hildon_text_view_refresh_contents), text_view);
141 /* Refresh textview contents */
142 hildon_text_view_refresh_contents (GTK_WIDGET (text_view));
146 * hildon_text_view_get_buffer:
147 * @text_view: a #HildonTextView
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.
152 * Note that you must never use gtk_text_view_get_buffer() to get the
153 * buffer from a #HildonTextView.
155 * Also note that placeholder text (set using
156 * hildon_text_view_set_placeholder()) is never contained in this
159 * Returns: a #GtkTextBuffer
164 hildon_text_view_get_buffer (HildonTextView *text_view)
166 HildonTextViewPrivate *priv;
168 g_return_val_if_fail (HILDON_IS_TEXT_VIEW (text_view), NULL);
170 priv = HILDON_TEXT_VIEW_GET_PRIVATE (text_view);
172 /* Always return priv->main_buffer even if the placeholder is
174 return priv->main_buffer;
178 * hildon_text_view_set_placeholder:
179 * @text_view: a #HildonTextView
180 * @text: the new text
182 * Sets the placeholder text in @text_view to @text.
187 hildon_text_view_set_placeholder (HildonTextView *text_view,
190 HildonTextViewPrivate *priv;
192 g_return_if_fail (HILDON_IS_TEXT_VIEW (text_view) && text != NULL);
194 priv = HILDON_TEXT_VIEW_GET_PRIVATE (text_view);
196 gtk_text_buffer_set_text (priv->placeholder_buffer, text, -1);
200 * hildon_text_view_new:
202 * Creates a new text view.
204 * Returns: a new #HildonTextView
209 hildon_text_view_new (void)
211 GtkWidget *entry = g_object_new (HILDON_TYPE_TEXT_VIEW, NULL);
217 hildon_text_view_focus_in_event (GtkWidget *widget,
218 GdkEventFocus *event)
220 hildon_text_view_refresh_contents (widget);
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);
230 hildon_text_view_focus_out_event (GtkWidget *widget,
231 GdkEventFocus *event)
233 hildon_text_view_refresh_contents (widget);
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);
243 hildon_text_view_button_press_event (GtkWidget *widget,
244 GdkEventButton *event)
246 HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (widget);
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;
254 if (event->button == 1 && event->type == GDK_BUTTON_PRESS) {
265 hildon_text_view_button_release_event (GtkWidget *widget,
266 GdkEventButton *event)
268 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
269 HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (widget);
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;
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;
284 window_type = gtk_text_view_get_window_type (text_view, event->window);
285 gtk_text_view_window_to_buffer_coords (text_view,
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);
293 gtk_widget_grab_focus (GTK_WIDGET (text_view));
302 hildon_text_view_finalize (GObject *object)
304 HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (object);
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);
310 if (G_OBJECT_CLASS (hildon_text_view_parent_class)->finalize)
311 G_OBJECT_CLASS (hildon_text_view_parent_class)->finalize (object);
315 hildon_text_view_class_init (HildonTextViewClass *klass)
317 GObjectClass *gobject_class = (GObjectClass *)klass;
318 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
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;
327 g_type_class_add_private (klass, sizeof (HildonTextViewPrivate));
331 hildon_text_view_init (HildonTextView *self)
333 HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (self);
335 priv->main_buffer = gtk_text_buffer_new (NULL);
336 priv->placeholder_buffer = gtk_text_buffer_new (NULL);
338 hildon_text_view_refresh_contents (GTK_WIDGET (self));
341 g_signal_connect_swapped (priv->main_buffer, "changed",
342 G_CALLBACK (hildon_text_view_refresh_contents), self);