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