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: Text view within the Hildon framework.
21 * The #HildonTextView is a text
22 * view derived from the #GtkTextView widget that 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"
57 #include "hildon-helper.h"
60 #define HILDON_TEXT_VIEW_DRAG_THRESHOLD 16.0
62 G_DEFINE_TYPE (HildonTextView, hildon_text_view, GTK_TYPE_TEXT_VIEW);
64 #define HILDON_TEXT_VIEW_GET_PRIVATE(obj) \
65 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
66 HILDON_TYPE_TEXT_VIEW, HildonTextViewPrivate));
68 typedef struct _HildonTextViewPrivate HildonTextViewPrivate;
70 struct _HildonTextViewPrivate
72 GtkTextBuffer *main_buffer; /* Used to show the "real" contents */
73 GtkTextBuffer *placeholder_buffer; /* Internal, used to display the placeholder */
74 gulong changed_id; /* ID of the main_buffer::changed signal handler */
75 gdouble x; /* tap x position */
76 gdouble y; /* tap y position */
79 /* Function used to decide whether to show the placeholder or not */
81 hildon_text_view_refresh_contents (GtkWidget *text_view)
83 HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (text_view);
84 gint bufsize = gtk_text_buffer_get_char_count (priv->main_buffer);
86 if ((bufsize > 0) || GTK_WIDGET_HAS_FOCUS (text_view)) {
87 /* Display the main buffer if it contains text or the widget is focused */
88 hildon_helper_set_logical_color (text_view, GTK_RC_TEXT, GTK_STATE_NORMAL, "ReversedTextColor");
89 gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), priv->main_buffer);
91 /* Otherwise, display the placeholder */
92 hildon_helper_set_logical_color (text_view, GTK_RC_TEXT, GTK_STATE_NORMAL, "ReversedSecondaryTextColor");
93 gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), priv->placeholder_buffer);
98 * hildon_text_view_set_buffer:
99 * @text_view: a #HildonTextView
100 * @buffer: a #GtkTextBuffer
102 * Sets @buffer as the buffer being displayed by @text_view. The
103 * previous buffer displayed by the text view is unreferenced, and a
104 * reference is added to @buffer. If you owned a reference to @buffer
105 * before passing it to this function, you must remove that reference
108 * Note that you must never use gtk_text_view_set_buffer() to set the
109 * buffer of a #HildonTextView.
114 hildon_text_view_set_buffer (HildonTextView *text_view,
115 GtkTextBuffer *buffer)
117 HildonTextViewPrivate *priv;
119 g_return_if_fail (HILDON_IS_TEXT_VIEW (text_view));
120 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
122 priv = HILDON_TEXT_VIEW_GET_PRIVATE (text_view);
124 /* If this is the same buffer, don't do anything */
125 if (buffer == priv->main_buffer)
128 /* Disconnect the signal handler from the old buffer */
129 g_signal_handler_disconnect (priv->main_buffer, priv->changed_id);
131 /* Replace the old buffer with the new one */
132 g_object_unref (priv->main_buffer);
133 priv->main_buffer = g_object_ref (buffer);
135 /* Attach a callback to the new text buffer */
137 g_signal_connect_swapped (priv->main_buffer, "changed",
138 G_CALLBACK (hildon_text_view_refresh_contents), text_view);
140 /* Refresh textview contents */
141 hildon_text_view_refresh_contents (GTK_WIDGET (text_view));
145 * hildon_text_view_get_buffer:
146 * @text_view: a #HildonTextView
148 * Returns the text buffer in @text_view. The reference count is not
149 * incremented; the caller of this function won't own a new reference.
151 * Note that you must never use gtk_text_view_get_buffer() to get the
152 * buffer from a #HildonTextView.
154 * Also note that placeholder text (set using
155 * hildon_text_view_set_placeholder()) is never contained in this
158 * Returns: a #GtkTextBuffer
163 hildon_text_view_get_buffer (HildonTextView *text_view)
165 HildonTextViewPrivate *priv;
167 g_return_val_if_fail (HILDON_IS_TEXT_VIEW (text_view), NULL);
169 priv = HILDON_TEXT_VIEW_GET_PRIVATE (text_view);
171 /* Always return priv->main_buffer even if the placeholder is
173 return priv->main_buffer;
177 * hildon_text_view_set_placeholder:
178 * @text_view: a #HildonTextView
179 * @text: the new text
181 * Sets the placeholder text in @text_view to @text.
186 hildon_text_view_set_placeholder (HildonTextView *text_view,
189 HildonTextViewPrivate *priv;
191 g_return_if_fail (HILDON_IS_TEXT_VIEW (text_view) && text != NULL);
193 priv = HILDON_TEXT_VIEW_GET_PRIVATE (text_view);
195 gtk_text_buffer_set_text (priv->placeholder_buffer, text, -1);
199 * hildon_text_view_new:
201 * Creates a new text view.
203 * Returns: a new #HildonTextView
208 hildon_text_view_new (void)
210 GtkWidget *entry = g_object_new (HILDON_TYPE_TEXT_VIEW, NULL);
216 hildon_text_view_focus_in_event (GtkWidget *widget,
217 GdkEventFocus *event)
219 hildon_text_view_refresh_contents (widget);
221 if (GTK_WIDGET_CLASS (hildon_text_view_parent_class)->focus_in_event) {
222 return GTK_WIDGET_CLASS (hildon_text_view_parent_class)->focus_in_event (widget, event);
229 hildon_text_view_focus_out_event (GtkWidget *widget,
230 GdkEventFocus *event)
232 hildon_text_view_refresh_contents (widget);
234 if (GTK_WIDGET_CLASS (hildon_text_view_parent_class)->focus_out_event) {
235 return GTK_WIDGET_CLASS (hildon_text_view_parent_class)->focus_out_event (widget, event);
242 hildon_text_view_button_press_event (GtkWidget *widget,
243 GdkEventButton *event)
245 HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (widget);
247 if (GTK_TEXT_VIEW (widget)->editable &&
248 hildon_gtk_im_context_filter_event (GTK_TEXT_VIEW (widget)->im_context, (GdkEvent*)event)) {
249 GTK_TEXT_VIEW (widget)->need_im_reset = TRUE;
253 if (event->button == 1 && event->type == GDK_BUTTON_PRESS) {
264 hildon_text_view_button_release_event (GtkWidget *widget,
265 GdkEventButton *event)
267 GtkTextView *text_view = GTK_TEXT_VIEW (widget);
268 HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (widget);
272 if (text_view->editable &&
273 hildon_gtk_im_context_filter_event (text_view->im_context, (GdkEvent*)event)) {
274 text_view->need_im_reset = TRUE;
278 if (event->button == 1 && event->type == GDK_BUTTON_RELEASE) {
279 if (fabs (priv->x - event->x) < HILDON_TEXT_VIEW_DRAG_THRESHOLD &&
280 fabs (priv->y - event->y) < HILDON_TEXT_VIEW_DRAG_THRESHOLD) {
281 GtkTextWindowType window_type;
283 window_type = gtk_text_view_get_window_type (text_view, event->window);
284 gtk_text_view_window_to_buffer_coords (text_view,
288 gtk_text_view_get_iter_at_location (text_view, &iter, x, y);
289 if (gtk_text_buffer_get_char_count (priv->main_buffer))
290 gtk_text_buffer_place_cursor (priv->main_buffer, &iter);
292 gtk_widget_grab_focus (GTK_WIDGET (text_view));
301 hildon_text_view_finalize (GObject *object)
303 HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (object);
305 g_signal_handler_disconnect (priv->main_buffer, priv->changed_id);
306 g_object_unref (priv->main_buffer);
307 g_object_unref (priv->placeholder_buffer);
309 if (G_OBJECT_CLASS (hildon_text_view_parent_class)->finalize)
310 G_OBJECT_CLASS (hildon_text_view_parent_class)->finalize (object);
314 hildon_text_view_class_init (HildonTextViewClass *klass)
316 GObjectClass *gobject_class = (GObjectClass *)klass;
317 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
319 gobject_class->finalize = hildon_text_view_finalize;
320 widget_class->focus_in_event = hildon_text_view_focus_in_event;
321 widget_class->focus_out_event = hildon_text_view_focus_out_event;
322 widget_class->motion_notify_event = NULL;
323 widget_class->button_press_event = hildon_text_view_button_press_event;
324 widget_class->button_release_event = hildon_text_view_button_release_event;
326 g_type_class_add_private (klass, sizeof (HildonTextViewPrivate));
330 hildon_text_view_init (HildonTextView *self)
332 HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (self);
334 priv->main_buffer = gtk_text_buffer_new (NULL);
335 priv->placeholder_buffer = gtk_text_buffer_new (NULL);
337 hildon_text_view_refresh_contents (GTK_WIDGET (self));
340 g_signal_connect_swapped (priv->main_buffer, "changed",
341 G_CALLBACK (hildon_text_view_refresh_contents), self);