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