* widgets/:
[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 static GObjectClass *parent_class = NULL;
40
41 /* signals */
42 enum {
43         ACTIVATE_SIGNAL,
44         LAST_SIGNAL
45 };
46
47 typedef struct _ModestRecptViewPriv ModestRecptViewPriv;
48
49 struct _ModestRecptViewPriv
50 {
51         gboolean button_pressed;
52         gdouble pressed_x, pressed_y;
53 };
54
55 #define MODEST_RECPT_VIEW_GET_PRIVATE(o)        \
56         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_RECPT_VIEW, ModestRecptViewPriv))
57
58 static guint signals[LAST_SIGNAL] = {0};
59
60
61 /**
62  * modest_recpt_view_new:
63  *
64  * Return value: a new #ModestRecptView instance implemented for Gtk+
65  **/
66 GtkWidget*
67 modest_recpt_view_new (void)
68 {
69         ModestRecptView *self = g_object_new (MODEST_TYPE_RECPT_VIEW, NULL);
70
71         return GTK_WIDGET (self);
72 }
73
74 void
75 modest_recpt_view_set_recipients (ModestRecptView *recpt_view, const gchar *recipients)
76 {
77         const GtkWidget *text_view = NULL;
78         GtkTextBuffer *buffer = NULL;
79
80         text_view = modest_scroll_text_get_text_view (MODEST_SCROLL_TEXT (recpt_view));
81         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
82
83         gtk_text_buffer_set_text (buffer, recipients, -1);
84         if (GTK_WIDGET_REALIZED (recpt_view))
85                 gtk_widget_queue_resize (GTK_WIDGET (recpt_view));
86
87 }
88
89 static gint
90 button_press_event (GtkWidget *widget,
91                     GdkEventButton *event,
92                     gpointer user_data)
93 {
94         ModestRecptViewPriv *priv = MODEST_RECPT_VIEW_GET_PRIVATE (MODEST_RECPT_VIEW (user_data));
95
96         if (event->type == GDK_BUTTON_PRESS && event->button == 1) {
97                 priv->button_pressed = TRUE;
98                 priv->pressed_x = event->x;
99                 priv->pressed_y = event->y;
100         }
101         return TRUE;
102 }
103
104 static gint
105 button_release_event (GtkWidget *widget,
106                       GdkEventButton *event,
107                       gpointer user_data)
108 {
109         ModestRecptViewPriv *priv = MODEST_RECPT_VIEW_GET_PRIVATE (MODEST_RECPT_VIEW (user_data));
110         const GtkWidget *text_view = NULL;
111
112         text_view = modest_scroll_text_get_text_view (MODEST_SCROLL_TEXT (user_data));
113
114         if (event->type != GDK_BUTTON_RELEASE)
115                 return TRUE;
116
117         if ((priv->button_pressed) &&
118             (event->type == GDK_BUTTON_RELEASE) &&
119             (priv->pressed_x == event->x) &&
120             (priv->pressed_y == event->y)) {
121                 priv->button_pressed = FALSE;
122                 if (event->button == 1) {
123                         gint buffer_x, buffer_y;
124                         int index;
125                         GtkTextIter iter;
126                         gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (text_view), GTK_TEXT_WINDOW_WIDGET,
127                                                                event->x, event->y, &buffer_x, &buffer_y);
128                         gtk_text_view_get_iter_at_location (GTK_TEXT_VIEW (text_view), &iter, buffer_x, buffer_y);
129                         index = gtk_text_iter_get_offset (&iter);
130                         
131                         if (!gtk_text_iter_is_end (&iter)) {
132                                 int selection_start, selection_end;
133                                 gboolean selected = FALSE;
134                                 GtkTextIter start_iter, end_iter;
135                                 GtkTextBuffer *buffer;
136
137                                 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
138                                 if (gtk_text_buffer_get_selection_bounds (buffer, &start_iter, &end_iter) &&
139                                     gtk_text_iter_in_range (&iter, &start_iter, &end_iter)) {
140                                         selected = TRUE;
141                                 } else {
142                                         gchar *text = NULL;
143                                         GtkTextIter start_iter, end_iter;
144
145                                         gtk_text_buffer_get_start_iter (buffer, &start_iter);
146                                         gtk_text_buffer_get_end_iter (buffer, &end_iter);
147                                         text = gtk_text_buffer_get_text (buffer, &start_iter, &end_iter, FALSE);
148
149                                         modest_text_utils_address_range_at_position (text,
150                                                                                      index,
151                                                                                      &selection_start, &selection_end);
152                                         /* TODO: now gtk label tries to select more than the label as usual,
153                                          *  and we force it to recover the selected region for the defined area.
154                                          *  It should be fixed (maybe preventing gtklabel to manage selections
155                                          *  in parallel with us
156                                          */
157                                         gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, selection_start);
158                                         gtk_text_buffer_get_iter_at_offset (buffer, &end_iter, selection_end);
159                                         gtk_text_buffer_select_range (buffer, &start_iter, &end_iter);
160                                         
161                                         if (text)
162                                                 g_free (text);
163                                                                       
164                                 }
165
166                                 if (selected) {
167                                         gchar *selection;
168
169                                         gtk_text_buffer_get_selection_bounds (buffer, &start_iter, &end_iter);
170                                         selection = gtk_text_buffer_get_text (buffer, &start_iter, &end_iter, FALSE);
171                                         g_signal_emit (G_OBJECT (user_data), signals[ACTIVATE_SIGNAL], 0, selection);
172                                         g_free (selection);
173                                 }
174
175                         }
176                         return TRUE;
177                 }
178         }
179         priv->button_pressed = FALSE;
180         return TRUE;
181 }
182
183 static void
184 modest_recpt_view_instance_init (GTypeInstance *instance, gpointer g_class)
185 {
186         const GtkTextView *text_view = NULL;
187
188         text_view = GTK_TEXT_VIEW(modest_scroll_text_get_text_view (MODEST_SCROLL_TEXT (instance)));
189
190         g_signal_connect (G_OBJECT (text_view), "button-press-event", G_CALLBACK (button_press_event), instance);
191         g_signal_connect (G_OBJECT (text_view), "button-release-event", G_CALLBACK (button_release_event), instance);
192
193         return;
194 }
195
196 static void
197 modest_recpt_view_finalize (GObject *object)
198 {
199         (*parent_class->finalize) (object);
200
201         return;
202 }
203
204 static void 
205 modest_recpt_view_class_init (ModestRecptViewClass *klass)
206 {
207         GObjectClass *object_class;
208         GtkWidgetClass *widget_class;
209
210         parent_class = g_type_class_peek_parent (klass);
211         object_class = (GObjectClass*) klass;
212         widget_class = GTK_WIDGET_CLASS (klass);
213
214         object_class->finalize = modest_recpt_view_finalize;
215
216         klass->activate = NULL;
217
218         g_type_class_add_private (object_class, sizeof (ModestRecptViewPriv));
219
220         signals[ACTIVATE_SIGNAL] =
221                 g_signal_new ("activate",
222                               G_TYPE_FROM_CLASS (object_class),
223                               G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
224                               G_STRUCT_OFFSET(ModestRecptViewClass, activate),
225                               NULL, NULL,
226                               g_cclosure_marshal_VOID__STRING,
227                               G_TYPE_NONE, 1, G_TYPE_STRING);
228         
229         return;
230 }
231
232 GType 
233 modest_recpt_view_get_type (void)
234 {
235         static GType type = 0;
236
237         if (G_UNLIKELY(type == 0))
238         {
239                 static const GTypeInfo info = 
240                 {
241                   sizeof (ModestRecptViewClass),
242                   NULL,   /* base_init */
243                   NULL,   /* base_finalize */
244                   (GClassInitFunc) modest_recpt_view_class_init,   /* class_init */
245                   NULL,   /* class_finalize */
246                   NULL,   /* class_data */
247                   sizeof (ModestRecptView),
248                   0,      /* n_preallocs */
249                   modest_recpt_view_instance_init    /* instance_init */
250                 };
251
252                 type = g_type_register_static (MODEST_TYPE_SCROLL_TEXT,
253                         "ModestRecptView",
254                         &info, 0);
255
256         }
257
258         return type;
259 }