933fdcfba6fe5c11d5db1b6391d34adf2231504c
[hildon] / hildon / hildon-text-view.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2008, 2009 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: Text view within the Hildon framework.
20  *
21  * The #HildonTextView is a text
22  * view derived from the #GtkTextView widget that provides
23  * additional commodities specific to the Hildon framework.
24  *
25  * A #HildonTextView can also have a placeholder text. This text will
26  * be shown if the text view is empty and doesn't have the input
27  * focus, but it's otherwise ignored. Thus, calls to
28  * gtk_text_view_get_buffer() will never return the placeholder text,
29  * not even when it's being displayed.
30  *
31  * <example>
32  * <title>Creating a HildonTextView with a placeholder</title>
33  * <programlisting>
34  * GtkWidget *
35  * create_text_view (void)
36  * {
37  *     GtkWidget *text_view;
38  * <!-- -->
39  *     text_view = hildon_text_view_new ();
40  *     hildon_gtk_text_view_set_placeholder_text (GTK_TEXT_VIEW (text_view),
41  *                                                "Type some text here");
42  * <!-- -->
43  *     return text_view;
44  * }
45  * </programlisting>
46  * </example>
47  */
48
49 #include                                        "hildon-text-view.h"
50 #include <math.h>
51
52 #define HILDON_TEXT_VIEW_DRAG_THRESHOLD 16.0
53
54 G_DEFINE_TYPE                                   (HildonTextView, hildon_text_view, GTK_TYPE_TEXT_VIEW);
55
56 #define                                         HILDON_TEXT_VIEW_GET_PRIVATE(obj) \
57                                                 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
58                                                 HILDON_TYPE_TEXT_VIEW, HildonTextViewPrivate));
59
60 typedef struct                                  _HildonTextViewPrivate HildonTextViewPrivate;
61
62 struct                                          _HildonTextViewPrivate
63 {
64     gdouble x;                                                      /* tap x position */
65     gdouble y;                                                      /* tap y position */
66 };
67
68
69 /**
70  * hildon_text_view_set_buffer:
71  * @text_view: a #HildonTextView
72  * @buffer: a #GtkTextBuffer
73  *
74  * Sets @buffer as the buffer being displayed by @text_view. The
75  * previous buffer displayed by the text view is unreferenced, and a
76  * reference is added to @buffer. If you owned a reference to @buffer
77  * before passing it to this function, you must remove that reference
78  * yourself
79  *
80  * Since: 2.2
81  *
82  * Deprecated: use gtk_text_view_set_buffer() instead
83  */
84 void
85 hildon_text_view_set_buffer                     (HildonTextView *text_view,
86                                                  GtkTextBuffer  *buffer)
87 {
88     g_return_if_fail (HILDON_IS_TEXT_VIEW (text_view));
89     g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
90     gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), buffer);
91 }
92
93 /**
94  * hildon_text_view_get_buffer:
95  * @text_view: a #HildonTextView
96  *
97  * Returns the text buffer in @text_view. The reference count is not
98  * incremented; the caller of this function won't own a new reference.
99  *
100  * Note that the placeholder text (set using
101  * hildon_gtk_text_view_set_placeholder_text()) is never contained in
102  * this buffer.
103  *
104  * Returns: a #GtkTextBuffer
105  *
106  * Since: 2.2
107  *
108  * Deprecated: use gtk_text_view_get_buffer() instead
109  */
110 GtkTextBuffer *
111 hildon_text_view_get_buffer                     (HildonTextView *text_view)
112 {
113     g_return_val_if_fail (HILDON_IS_TEXT_VIEW (text_view), NULL);
114     return gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
115 }
116
117 /**
118  * hildon_text_view_set_placeholder:
119  * @text_view: a #HildonTextView
120  * @text: the new text
121  *
122  * Sets the placeholder text in @text_view to @text.
123  *
124  * Since: 2.2
125  *
126  * Deprecated: use hildon_gtk_text_view_set_placeholder_text() instead
127  */
128 void
129 hildon_text_view_set_placeholder                (HildonTextView *text_view,
130                                                  const gchar    *text)
131 {
132     g_return_if_fail (HILDON_IS_TEXT_VIEW (text_view) && text != NULL);
133     hildon_gtk_text_view_set_placeholder_text (GTK_TEXT_VIEW (text_view), text);
134 }
135
136 /**
137  * hildon_text_view_new:
138  *
139  * Creates a new text view.
140  *
141  * Returns: a new #HildonTextView
142  *
143  * Since: 2.2
144  */
145 GtkWidget *
146 hildon_text_view_new                            (void)
147 {
148     GtkWidget *entry = g_object_new (HILDON_TYPE_TEXT_VIEW, NULL);
149
150     return entry;
151 }
152
153 static gint
154 hildon_text_view_button_press_event             (GtkWidget        *widget,
155                                                  GdkEventButton   *event)
156 {
157     HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (widget);
158
159     gtk_widget_grab_focus (widget);
160
161     if (GTK_TEXT_VIEW (widget)->editable &&
162         hildon_gtk_im_context_filter_event (GTK_TEXT_VIEW (widget)->im_context, (GdkEvent*)event)) {
163         GTK_TEXT_VIEW (widget)->need_im_reset = TRUE;
164         return TRUE;
165     }
166
167     if (event->button == 1 && event->type == GDK_BUTTON_PRESS) {
168         priv->x = event->x;
169         priv->y = event->y;
170
171         return TRUE;
172     }
173
174     return FALSE;
175 }
176
177 static gint
178 hildon_text_view_button_release_event           (GtkWidget        *widget,
179                                                  GdkEventButton   *event)
180 {
181     GtkTextView *text_view = GTK_TEXT_VIEW (widget);
182     HildonTextViewPrivate *priv = HILDON_TEXT_VIEW_GET_PRIVATE (widget);
183     GtkTextIter iter;
184     gint x, y;
185
186     if (text_view->editable &&
187         hildon_gtk_im_context_filter_event (text_view->im_context, (GdkEvent*)event)) {
188         text_view->need_im_reset = TRUE;
189         return TRUE;
190     }
191
192     if (event->button == 1 && event->type == GDK_BUTTON_RELEASE) {
193         if (fabs (priv->x - event->x) < HILDON_TEXT_VIEW_DRAG_THRESHOLD &&
194             fabs (priv->y - event->y) < HILDON_TEXT_VIEW_DRAG_THRESHOLD) {
195             GtkTextWindowType window_type;
196             GtkTextBuffer *buffer;
197
198             window_type = gtk_text_view_get_window_type (text_view, event->window);
199             gtk_text_view_window_to_buffer_coords (text_view,
200                                                    window_type,
201                                                    event->x, event->y,
202                                                    &x, &y);
203             gtk_text_view_get_iter_at_location (text_view, &iter, x, y);
204             buffer = gtk_text_view_get_buffer (text_view);
205             if (gtk_text_buffer_get_char_count (buffer))
206                 gtk_text_buffer_place_cursor (buffer, &iter);
207
208             gtk_widget_grab_focus (GTK_WIDGET (text_view));
209
210             return TRUE;
211         }
212     }
213     return FALSE;
214 }
215
216 static void
217 hildon_text_view_class_init                     (HildonTextViewClass *klass)
218 {
219     GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
220
221     widget_class->motion_notify_event = NULL;
222     widget_class->button_press_event = hildon_text_view_button_press_event;
223     widget_class->button_release_event = hildon_text_view_button_release_event;
224
225     g_type_class_add_private (klass, sizeof (HildonTextViewPrivate));
226 }
227 static void
228 hildon_text_view_init                           (HildonTextView *self)
229 {
230 }