* src/maemo/modest-msg-edit-window.c:
[modest] / src / widgets / modest-recpt-editor.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/gtkscrolledwindow.h>
35 #include <gtk/gtktextview.h>
36 #include <gtk/gtkimage.h>
37 #include <gtk/gtkbutton.h>
38
39 #include <modest-text-utils.h>
40 #include <modest-recpt-editor.h>
41
42 static GObjectClass *parent_class = NULL;
43
44 /* /\* signals *\/ */
45 /* enum { */
46 /*      LAST_SIGNAL */
47 /* }; */
48
49 typedef struct _ModestRecptEditorPrivate ModestRecptEditorPrivate;
50
51 struct _ModestRecptEditorPrivate
52 {
53         GtkWidget *text_view;
54         GtkWidget *abook_button;
55         GtkWidget *scrolled_window;
56         gchar *recipients;
57 };
58
59 #define MODEST_RECPT_EDITOR_GET_PRIVATE(o)      \
60         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_RECPT_EDITOR, ModestRecptEditorPrivate))
61
62 /* static guint signals[LAST_SIGNAL] = {0}; */
63
64 /* static functions: GObject */
65 static void modest_recpt_editor_instance_init (GTypeInstance *instance, gpointer g_class);
66 static void modest_recpt_editor_finalize (GObject *object);
67 static void modest_recpt_editor_class_init (ModestRecptEditorClass *klass);
68
69 /**
70  * modest_recpt_editor_new:
71  *
72  * Return value: a new #ModestRecptEditor instance implemented for Gtk+
73  **/
74 GtkWidget*
75 modest_recpt_editor_new (void)
76 {
77         ModestRecptEditor *self = g_object_new (MODEST_TYPE_RECPT_EDITOR, 
78                                                 "homogeneous", FALSE,
79                                                 "spacing", 1,
80                                                 NULL);
81
82         return GTK_WIDGET (self);
83 }
84
85 void
86 modest_recpt_editor_set_recipients (ModestRecptEditor *recpt_editor, const gchar *recipients)
87 {
88         ModestRecptEditorPrivate *priv;
89         GtkTextBuffer *buffer = NULL;
90
91         g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
92         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
93
94         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
95
96         gtk_text_buffer_set_text (buffer, recipients, -1);
97         if (GTK_WIDGET_REALIZED (recpt_editor))
98                 gtk_widget_queue_resize (GTK_WIDGET (recpt_editor));
99
100 }
101
102 const gchar *
103 modest_recpt_editor_get_recipients (ModestRecptEditor *recpt_editor)
104 {
105         ModestRecptEditorPrivate *priv;
106         GtkTextBuffer *buffer = NULL;
107         GtkTextIter start, end;
108
109         g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), NULL);
110         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
111
112         if (priv->recipients != NULL) {
113                 g_free (priv->recipients);
114                 priv->recipients = NULL;
115         }
116
117         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
118
119         gtk_text_buffer_get_start_iter (buffer, &start);
120         gtk_text_buffer_get_end_iter (buffer, &end);
121
122         priv->recipients = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
123
124         return priv->recipients;
125
126 }
127
128 static void
129 modest_recpt_editor_instance_init (GTypeInstance *instance, gpointer g_class)
130 {
131         ModestRecptEditorPrivate *priv;
132         GtkWidget *abook_icon;
133
134         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (instance);
135
136         priv->abook_button = gtk_button_new ();
137         gtk_button_set_relief (GTK_BUTTON (priv->abook_button), GTK_RELIEF_NONE);
138         gtk_button_set_focus_on_click (GTK_BUTTON (priv->abook_button), FALSE);
139         gtk_button_set_alignment (GTK_BUTTON (priv->abook_button), 1.0, 1.0);
140         abook_icon = gtk_image_new_from_icon_name ("qgn_list_gene_contacts", GTK_ICON_SIZE_BUTTON);
141         gtk_container_add (GTK_CONTAINER (priv->abook_button), abook_icon);
142
143         priv->text_view = gtk_text_view_new ();
144         priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
145         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window), GTK_POLICY_NEVER, GTK_POLICY_NEVER);
146         gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window), GTK_SHADOW_IN);
147         gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->text_view);
148
149         gtk_box_pack_start (GTK_BOX (instance), priv->scrolled_window, TRUE, TRUE, 0);
150         gtk_box_pack_end (GTK_BOX (instance), priv->abook_button, FALSE, FALSE, 0);
151
152         gtk_text_view_set_accepts_tab (GTK_TEXT_VIEW (priv->text_view), FALSE);
153         gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (priv->text_view), TRUE);
154         gtk_text_view_set_editable (GTK_TEXT_VIEW (priv->text_view), TRUE);
155         gtk_text_view_set_justification (GTK_TEXT_VIEW (priv->text_view), GTK_JUSTIFY_LEFT);
156         gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (priv->text_view), GTK_WRAP_WORD_CHAR);
157
158         return;
159 }
160
161 void
162 modest_recpt_editor_set_field_size_group (ModestRecptEditor *recpt_editor, GtkSizeGroup *size_group)
163 {
164         ModestRecptEditorPrivate *priv;
165
166         g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
167         g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
168         priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
169
170         gtk_size_group_add_widget (size_group, priv->scrolled_window);
171 }
172
173 static void
174 modest_recpt_editor_finalize (GObject *object)
175 {
176         (*parent_class->finalize) (object);
177
178         return;
179 }
180
181 static void 
182 modest_recpt_editor_class_init (ModestRecptEditorClass *klass)
183 {
184         GObjectClass *object_class;
185         GtkWidgetClass *widget_class;
186
187         parent_class = g_type_class_peek_parent (klass);
188         object_class = (GObjectClass*) klass;
189         widget_class = GTK_WIDGET_CLASS (klass);
190
191         object_class->finalize = modest_recpt_editor_finalize;
192
193         g_type_class_add_private (object_class, sizeof (ModestRecptEditorPrivate));
194
195         return;
196 }
197
198 GType 
199 modest_recpt_editor_get_type (void)
200 {
201         static GType type = 0;
202
203         if (G_UNLIKELY(type == 0))
204         {
205                 static const GTypeInfo info = 
206                 {
207                   sizeof (ModestRecptEditorClass),
208                   NULL,   /* base_init */
209                   NULL,   /* base_finalize */
210                   (GClassInitFunc) modest_recpt_editor_class_init,   /* class_init */
211                   NULL,   /* class_finalize */
212                   NULL,   /* class_data */
213                   sizeof (ModestRecptEditor),
214                   0,      /* n_preallocs */
215                   modest_recpt_editor_instance_init    /* instance_init */
216                 };
217
218                 type = g_type_register_static (GTK_TYPE_HBOX,
219                         "ModestRecptEditor",
220                         &info, 0);
221
222         }
223
224         return type;
225 }