99cb97ab0522c48fef0a52b5ae9cf7888fa4ed7c
[modest] / src / widgets / modest-recpt-view.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <config.h>
31
32 #include <glib/gi18n-lib.h>
33
34 #include <gtk/gtkwidget.h>
35
36 #include <modest-text-utils.h>
37 #include <modest-recpt-view.h>
38
39 #define RECPT_VIEW_CLICK_AREA_THRESHOLD 32
40
41 static GObjectClass *parent_class = NULL;
42
43 /* signals */
44 enum {
45         ACTIVATE_SIGNAL,
46         LAST_SIGNAL
47 };
48
49 typedef struct _ModestRecptViewPriv ModestRecptViewPriv;
50
51 struct _ModestRecptViewPriv
52 {
53         gboolean button_pressed;
54         gdouble pressed_x, pressed_y;
55 };
56
57 #define MODEST_RECPT_VIEW_GET_PRIVATE(o)        \
58         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_RECPT_VIEW, ModestRecptViewPriv))
59
60 static guint signals[LAST_SIGNAL] = {0};
61
62 /* static functions: GObject */
63 static void modest_recpt_view_instance_init (GTypeInstance *instance, gpointer g_class);
64 static void modest_recpt_view_finalize (GObject *object);
65 static void modest_recpt_view_class_init (ModestRecptViewClass *klass);
66 /* static functions: GtkWidget */
67 static gint button_press_event (GtkWidget *widget, GdkEventButton *event, gpointer user_data);
68 static gint button_release_event (GtkWidget *widget, GdkEventButton *event, gpointer user_data);
69
70 /**
71  * modest_recpt_view_new:
72  *
73  * Return value: a new #ModestRecptView instance implemented for Gtk+
74  **/
75 GtkWidget*
76 modest_recpt_view_new (void)
77 {
78         ModestRecptView *self = g_object_new (MODEST_TYPE_RECPT_VIEW, NULL);
79
80         return GTK_WIDGET (self);
81 }
82
83 void
84 modest_recpt_view_set_recipients (ModestRecptView *recpt_view, const gchar *recipients)
85 {
86         const GtkWidget *text_view = NULL;
87         GtkTextBuffer *buffer = NULL;
88
89         text_view = modest_scroll_text_get_text_view (MODEST_SCROLL_TEXT (recpt_view));
90         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
91
92         gtk_text_buffer_set_text (buffer, recipients, -1);
93         if (GTK_WIDGET_REALIZED (recpt_view))
94                 gtk_widget_queue_resize (GTK_WIDGET (recpt_view));
95
96 }
97
98 static gint
99 button_press_event (GtkWidget *widget,
100                     GdkEventButton *event,
101                     gpointer user_data)
102 {
103         ModestRecptViewPriv *priv = MODEST_RECPT_VIEW_GET_PRIVATE (MODEST_RECPT_VIEW (user_data));
104
105         if (event->type == GDK_BUTTON_PRESS && event->button == 1) {
106                 priv->button_pressed = TRUE;
107                 priv->pressed_x = event->x;
108                 priv->pressed_y = event->y;
109         }
110         return TRUE;
111 }
112
113 static gint
114 button_release_event (GtkWidget *widget,
115                       GdkEventButton *event,
116                       gpointer user_data)
117 {
118         ModestRecptViewPriv *priv = MODEST_RECPT_VIEW_GET_PRIVATE (MODEST_RECPT_VIEW (user_data));
119         const GtkWidget *text_view = NULL;
120
121         text_view = modest_scroll_text_get_text_view (MODEST_SCROLL_TEXT (user_data));
122
123         if (event->type != GDK_BUTTON_RELEASE)
124                 return TRUE;
125
126         if ((priv->button_pressed) &&
127             (event->type == GDK_BUTTON_RELEASE) &&
128             ((event->x >= priv->pressed_x - RECPT_VIEW_CLICK_AREA_THRESHOLD)&&
129              (event->x <= priv->pressed_x + RECPT_VIEW_CLICK_AREA_THRESHOLD)) &&
130             ((event->y >= priv->pressed_y - RECPT_VIEW_CLICK_AREA_THRESHOLD)&&
131              (event->y <= priv->pressed_y + RECPT_VIEW_CLICK_AREA_THRESHOLD))) {
132                 priv->button_pressed = FALSE;
133                 if (event->button == 1) {
134                         gint buffer_x, buffer_y;
135                         int index;
136                         GtkTextIter iter;
137                         gtk_widget_grab_focus (GTK_WIDGET (text_view));
138                         gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (text_view), GTK_TEXT_WINDOW_WIDGET,
139                                                                event->x, event->y, &buffer_x, &buffer_y);
140                         gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (text_view), &iter, buffer_x, buffer_y);
141                         index = gtk_text_iter_get_offset (&iter);
142                         
143                         if (!gtk_text_iter_is_end (&iter)) {
144                                 int selection_start, selection_end;
145                                 gboolean selected = FALSE;
146                                 GtkTextIter start_iter, end_iter;
147                                 GtkTextBuffer *buffer;
148
149                                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
150                                 if (gtk_text_buffer_get_selection_bounds (buffer, &start_iter, &end_iter) &&
151                                     gtk_text_iter_in_range (&iter, &start_iter, &end_iter)) {
152                                         selected = TRUE;
153                                 } else {
154                                         gchar *text = NULL;
155                                         GtkTextIter start_iter, end_iter;
156
157                                         gtk_text_buffer_get_start_iter (buffer, &start_iter);
158                                         gtk_text_buffer_get_end_iter (buffer, &end_iter);
159                                         text = gtk_text_buffer_get_text (buffer, &start_iter, &end_iter, FALSE);
160
161                                         /* text will not be NULL, but source code checkers should be satisfied */
162                                         if (text) {
163                                                 modest_text_utils_address_range_at_position (text,
164                                                                                              index,
165                                                                                              &selection_start, &selection_end);
166                                                 /* TODO: now gtk label tries to select more than the label as usual,
167                                                  *  and we force it to recover the selected region for the defined area.
168                                                  *  It should be fixed (maybe preventing gtklabel to manage selections
169                                                  *  in parallel with us
170                                                  */
171                                                 gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, selection_start);
172                                                 gtk_text_buffer_get_iter_at_offset (buffer, &end_iter, selection_end);
173                                                 gtk_text_buffer_select_range (buffer, &start_iter, &end_iter);
174                                                 
175                                                 g_free (text);
176                                         }                     
177                                 }
178                                 
179                                 if (selected) {
180                                         gchar *selection;
181
182                                         gtk_text_buffer_get_selection_bounds (buffer, &start_iter, &end_iter);
183                                         selection = gtk_text_buffer_get_text (buffer, &start_iter, &end_iter, FALSE);
184                                         g_signal_emit (G_OBJECT (user_data), signals[ACTIVATE_SIGNAL], 0, selection);
185                                         g_free (selection);
186                                 }
187
188                         }
189                         return TRUE;
190                 }
191         }
192         priv->button_pressed = FALSE;
193         return TRUE;
194 }
195
196 static void
197 modest_recpt_view_instance_init (GTypeInstance *instance, gpointer g_class)
198 {
199         const GtkTextView *text_view = NULL;
200
201         text_view = GTK_TEXT_VIEW(modest_scroll_text_get_text_view (MODEST_SCROLL_TEXT (instance)));
202
203         g_signal_connect (G_OBJECT (text_view), "button-press-event", G_CALLBACK (button_press_event), instance);
204         g_signal_connect_after (G_OBJECT (text_view), "button-release-event", G_CALLBACK (button_release_event), instance);
205
206         return;
207 }
208
209 static void
210 modest_recpt_view_finalize (GObject *object)
211 {
212         (*parent_class->finalize) (object);
213
214         return;
215 }
216
217 static void 
218 modest_recpt_view_class_init (ModestRecptViewClass *klass)
219 {
220         GObjectClass *object_class;
221         GtkWidgetClass *widget_class;
222
223         parent_class = g_type_class_peek_parent (klass);
224         object_class = (GObjectClass*) klass;
225         widget_class = GTK_WIDGET_CLASS (klass);
226
227         object_class->finalize = modest_recpt_view_finalize;
228
229         klass->activate = NULL;
230
231         g_type_class_add_private (object_class, sizeof (ModestRecptViewPriv));
232
233         signals[ACTIVATE_SIGNAL] =
234                 g_signal_new ("activate",
235                               G_TYPE_FROM_CLASS (object_class),
236                               G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
237                               G_STRUCT_OFFSET(ModestRecptViewClass, activate),
238                               NULL, NULL,
239                               g_cclosure_marshal_VOID__STRING,
240                               G_TYPE_NONE, 1, G_TYPE_STRING);
241         
242         return;
243 }
244
245 GType 
246 modest_recpt_view_get_type (void)
247 {
248         static GType type = 0;
249
250         if (G_UNLIKELY(type == 0))
251         {
252                 static const GTypeInfo info = 
253                 {
254                   sizeof (ModestRecptViewClass),
255                   NULL,   /* base_init */
256                   NULL,   /* base_finalize */
257                   (GClassInitFunc) modest_recpt_view_class_init,   /* class_init */
258                   NULL,   /* class_finalize */
259                   NULL,   /* class_data */
260                   sizeof (ModestRecptView),
261                   0,      /* n_preallocs */
262                   modest_recpt_view_instance_init    /* instance_init */
263                 };
264
265                 type = g_type_register_static (MODEST_TYPE_SCROLL_TEXT,
266                         "ModestRecptView",
267                         &info, 0);
268
269         }
270
271         return type;
272 }