ba19dbed2d49c70f4d041bda775b09d200c98928
[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         gchar *std_recipients;
89
90         text_view = modest_scroll_text_get_text_view (MODEST_SCROLL_TEXT (recpt_view));
91         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
92
93         if (recipients == NULL) {
94                 std_recipients = NULL;
95         } else {
96                 std_recipients = modest_text_utils_address_with_standard_length (recipients);
97         }
98
99         gtk_text_buffer_set_text (buffer, std_recipients, -1);
100         g_free (std_recipients);
101         if (GTK_WIDGET_REALIZED (recpt_view))
102                 gtk_widget_queue_resize (GTK_WIDGET (recpt_view));
103
104 }
105
106 static gint
107 button_press_event (GtkWidget *widget,
108                     GdkEventButton *event,
109                     gpointer user_data)
110 {
111         ModestRecptViewPriv *priv = MODEST_RECPT_VIEW_GET_PRIVATE (MODEST_RECPT_VIEW (user_data));
112
113         if (event->type == GDK_BUTTON_PRESS && event->button == 1) {
114                 priv->button_pressed = TRUE;
115                 priv->pressed_x = event->x;
116                 priv->pressed_y = event->y;
117         }
118         return TRUE;
119 }
120
121 static gint
122 button_release_event (GtkWidget *widget,
123                       GdkEventButton *event,
124                       gpointer user_data)
125 {
126         ModestRecptViewPriv *priv = MODEST_RECPT_VIEW_GET_PRIVATE (MODEST_RECPT_VIEW (user_data));
127         const GtkWidget *text_view = NULL;
128
129         text_view = modest_scroll_text_get_text_view (MODEST_SCROLL_TEXT (user_data));
130
131         if (event->type != GDK_BUTTON_RELEASE)
132                 return TRUE;
133
134         if ((priv->button_pressed) &&
135             (event->type == GDK_BUTTON_RELEASE) &&
136             ((event->x >= priv->pressed_x - RECPT_VIEW_CLICK_AREA_THRESHOLD)&&
137              (event->x <= priv->pressed_x + RECPT_VIEW_CLICK_AREA_THRESHOLD)) &&
138             ((event->y >= priv->pressed_y - RECPT_VIEW_CLICK_AREA_THRESHOLD)&&
139              (event->y <= priv->pressed_y + RECPT_VIEW_CLICK_AREA_THRESHOLD))) {
140                 priv->button_pressed = FALSE;
141                 if (event->button == 1) {
142                         gint buffer_x, buffer_y;
143                         int index;
144                         GtkTextIter iter;
145                         gtk_widget_grab_focus (GTK_WIDGET (text_view));
146                         gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (text_view), GTK_TEXT_WINDOW_WIDGET,
147                                                                event->x, event->y, &buffer_x, &buffer_y);
148                         gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (text_view), &iter, buffer_x, buffer_y);
149                         index = gtk_text_iter_get_offset (&iter);
150                         
151                         if (!gtk_text_iter_is_end (&iter)) {
152                                 guint selection_start, selection_end;
153                                 gboolean selected = FALSE;
154                                 GtkTextIter start_iter, end_iter;
155                                 GtkTextBuffer *buffer;
156
157                                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
158                                 if (gtk_text_buffer_get_selection_bounds (buffer, &start_iter, &end_iter) &&
159                                     gtk_text_iter_in_range (&iter, &start_iter, &end_iter)) {
160                                         selected = TRUE;
161                                 } else {
162                                         gchar *text = NULL;
163                                         GtkTextIter start_iter, end_iter;
164
165                                         gtk_text_buffer_get_start_iter (buffer, &start_iter);
166                                         gtk_text_buffer_get_end_iter (buffer, &end_iter);
167                                         text = gtk_text_buffer_get_text (buffer, &start_iter, &end_iter, FALSE);
168
169                                         /* text will not be NULL, but source code checkers should be satisfied */
170                                         if (text) {
171                                                 modest_text_utils_address_range_at_position (text,
172                                                                                              index,
173                                                                                              &selection_start, &selection_end);
174                                                 /* TODO: now gtk label tries to select more than the label as usual,
175                                                  *  and we force it to recover the selected region for the defined area.
176                                                  *  It should be fixed (maybe preventing gtklabel to manage selections
177                                                  *  in parallel with us
178                                                  */
179                                                 gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, selection_start);
180                                                 gtk_text_buffer_get_iter_at_offset (buffer, &end_iter, selection_end);
181                                                 gtk_text_buffer_select_range (buffer, &start_iter, &end_iter);
182                                                 
183                                                 g_free (text);
184                                         }                     
185                                 }
186                                 
187                                 if (selected) {
188                                         gchar *selection;
189
190                                         gtk_text_buffer_get_selection_bounds (buffer, &start_iter, &end_iter);
191                                         selection = gtk_text_buffer_get_text (buffer, &start_iter, &end_iter, FALSE);
192                                         g_signal_emit (G_OBJECT (user_data), signals[ACTIVATE_SIGNAL], 0, selection);
193                                         g_free (selection);
194                                 }
195
196                         }
197                         return TRUE;
198                 }
199         }
200         priv->button_pressed = FALSE;
201         return TRUE;
202 }
203
204 static void
205 modest_recpt_view_instance_init (GTypeInstance *instance, gpointer g_class)
206 {
207         const GtkTextView *text_view = NULL;
208
209         text_view = GTK_TEXT_VIEW(modest_scroll_text_get_text_view (MODEST_SCROLL_TEXT (instance)));
210
211         g_signal_connect (G_OBJECT (text_view), "button-press-event", G_CALLBACK (button_press_event), instance);
212         g_signal_connect_after (G_OBJECT (text_view), "button-release-event", G_CALLBACK (button_release_event), instance);
213
214         return;
215 }
216
217 static void
218 modest_recpt_view_finalize (GObject *object)
219 {
220         (*parent_class->finalize) (object);
221
222         return;
223 }
224
225 static void 
226 modest_recpt_view_class_init (ModestRecptViewClass *klass)
227 {
228         GObjectClass *object_class;
229
230         parent_class = g_type_class_peek_parent (klass);
231         object_class = (GObjectClass*) klass;
232
233         object_class->finalize = modest_recpt_view_finalize;
234
235         klass->activate = NULL;
236
237         g_type_class_add_private (object_class, sizeof (ModestRecptViewPriv));
238
239         signals[ACTIVATE_SIGNAL] =
240                 g_signal_new ("activate",
241                               G_TYPE_FROM_CLASS (object_class),
242                               G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
243                               G_STRUCT_OFFSET(ModestRecptViewClass, activate),
244                               NULL, NULL,
245                               g_cclosure_marshal_VOID__STRING,
246                               G_TYPE_NONE, 1, G_TYPE_STRING);
247         
248         return;
249 }
250
251 GType 
252 modest_recpt_view_get_type (void)
253 {
254         static GType type = 0;
255
256         if (G_UNLIKELY(type == 0))
257         {
258                 static const GTypeInfo info = 
259                 {
260                   sizeof (ModestRecptViewClass),
261                   NULL,   /* base_init */
262                   NULL,   /* base_finalize */
263                   (GClassInitFunc) modest_recpt_view_class_init,   /* class_init */
264                   NULL,   /* class_finalize */
265                   NULL,   /* class_data */
266                   sizeof (ModestRecptView),
267                   0,      /* n_preallocs */
268                   modest_recpt_view_instance_init    /* instance_init */
269                 };
270
271                 type = g_type_register_static (MODEST_TYPE_SCROLL_TEXT,
272                         "ModestRecptView",
273                         &info, 0);
274
275         }
276
277         return type;
278 }