25f64265c324139486032602274c5c8c590b2fc9
[modest] / src / widgets / modest-scroll-text.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/gtktextview.h>
35
36 #include <modest-scroll-text.h>
37
38 #define MODEST_SCROLL_TEXT_DEFAULT_LINE_LIMIT 2
39
40 static GObjectClass *parent_class = NULL;
41
42 typedef struct _ModestScrollTextPriv ModestScrollTextPriv;
43
44 struct _ModestScrollTextPriv
45 {
46         GtkWidget *text_view;
47         gint line_height;
48         guint line_limit;
49 };
50
51 #define MODEST_SCROLL_TEXT_GET_PRIVATE(o)       \
52         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_SCROLL_TEXT, ModestScrollTextPriv))
53
54
55 /**
56  * modest_scroll_text_new:
57  *
58  * Return value: a new #ModestScrollText instance implemented for Gtk+
59  **/
60 GtkWidget*
61 modest_scroll_text_new (GtkTextView *text_view, guint line_limit)
62 {
63         ModestScrollText *self = g_object_new (MODEST_TYPE_SCROLL_TEXT, NULL);
64         modest_scroll_text_set_line_limit (self, line_limit);
65         modest_scroll_text_set_text_view (self, text_view);
66
67         return GTK_WIDGET (self);
68 }
69
70 static void
71 size_request (GtkWidget *widget,
72               GtkRequisition *requisition,
73               gpointer user_data)
74 {
75         ModestScrollTextPriv *priv = MODEST_SCROLL_TEXT_GET_PRIVATE (widget);
76         const GtkWidget *text_view = NULL;
77         GtkTextBuffer *buffer = NULL;
78         GtkTextIter iter;
79         guint line;
80         guint line_limit;
81         GdkRectangle iter_rectangle;
82         GtkAdjustment *adj = NULL;
83         GtkTextMark *insert_mark;
84         GtkTextIter insert_iter;
85
86         text_view = modest_scroll_text_get_text_view (MODEST_SCROLL_TEXT (widget));
87         line_limit = priv->line_limit;
88
89         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
90
91         insert_mark = gtk_text_buffer_get_insert (buffer);
92         gtk_text_buffer_get_iter_at_mark (buffer, &insert_iter, insert_mark);
93
94         /* get the first line and the height of the line */
95         gtk_text_buffer_get_start_iter (buffer, &iter);
96         gtk_text_view_get_iter_location (GTK_TEXT_VIEW (text_view), &iter, &iter_rectangle);
97
98         /* Count lines in text view */
99         for (line = 0; line < line_limit; line++) {
100                 if (!gtk_text_view_forward_display_line (GTK_TEXT_VIEW (text_view), &iter))
101                         break;
102         }
103
104         /* Put again the cursor in the first character. Also scroll to first line */
105         gtk_text_buffer_place_cursor (buffer, &insert_iter);
106         gtk_text_view_place_cursor_onscreen (GTK_TEXT_VIEW (text_view));
107
108         /* Change the adjustment properties for one line per step behavior */
109         adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (widget));
110         if (adj != NULL) {
111                 g_object_set (G_OBJECT (adj), "page-increment", (gdouble) iter_rectangle.height + 1, "step-increment", (gdouble) iter_rectangle.height + 1, NULL);
112                 gtk_adjustment_changed (adj);
113         }
114
115         /* Set the requisition height to the get the limit of lines or less */
116         if (line > 0) {
117                 requisition->height = iter_rectangle.height * MIN (line + 1, line_limit);
118         } else {
119                 requisition->height = iter_rectangle.height;
120         }
121
122         if (gtk_scrolled_window_get_shadow_type (GTK_SCROLLED_WINDOW (widget)) != GTK_SHADOW_NONE) {
123                 requisition->height += GTK_WIDGET (widget)->style->ythickness * 2;
124         }
125                 
126         priv->line_height = iter_rectangle.height;
127
128 }
129
130 static void
131 size_allocate (GtkWidget *widget,
132                GtkAllocation *allocation,
133                gpointer user_data)
134 {
135         GtkAdjustment *adj = NULL;
136         ModestScrollTextPriv *priv = MODEST_SCROLL_TEXT_GET_PRIVATE (widget);
137
138         adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (widget));
139         if (adj != NULL) {              
140                 g_object_set (G_OBJECT (adj), "page-increment", (gdouble) priv->line_height, "step-increment", (gdouble) priv->line_height, NULL);
141         }
142         gtk_adjustment_changed (adj);
143 }
144
145 void 
146 modest_scroll_text_set_line_limit (ModestScrollText *scroll_text, guint line_limit)
147 {
148         ModestScrollTextPriv *priv = MODEST_SCROLL_TEXT_GET_PRIVATE (scroll_text);
149
150         if (line_limit == priv->line_limit)
151                 return;
152
153         priv->line_limit = line_limit;
154         if (GTK_WIDGET_REALIZED (scroll_text)) {
155                 gtk_widget_queue_resize (GTK_WIDGET (scroll_text));
156         }
157 }
158
159 const GtkWidget *
160 modest_scroll_text_get_text_view (ModestScrollText *scroll_text)
161 {
162         ModestScrollTextPriv *priv = MODEST_SCROLL_TEXT_GET_PRIVATE (scroll_text);
163
164         if (priv->text_view == NULL)
165                 modest_scroll_text_set_text_view (scroll_text, NULL);
166
167         return priv->text_view;
168 }
169
170 void
171 modest_scroll_text_set_text_view (ModestScrollText *scroll_text,
172                                   GtkTextView *text_view)
173 {
174         ModestScrollTextPriv *priv = MODEST_SCROLL_TEXT_GET_PRIVATE (scroll_text);
175         GtkStyle *style;
176
177         g_return_if_fail (MODEST_IS_SCROLL_TEXT (scroll_text));
178         if (text_view == NULL) {
179                 text_view = GTK_TEXT_VIEW(gtk_text_view_new ());
180                 gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (text_view), FALSE);
181                 gtk_text_view_set_editable (GTK_TEXT_VIEW (text_view), FALSE);
182         }
183
184         if (priv->text_view == GTK_WIDGET(text_view))
185                 return;
186
187         if (priv->text_view != NULL) {
188                 gtk_container_remove (GTK_CONTAINER(scroll_text), priv->text_view);
189                 priv->text_view = NULL;
190         }
191
192         priv->text_view = GTK_WIDGET(text_view);
193
194         gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (priv->text_view), GTK_WRAP_WORD_CHAR);
195         gtk_text_view_set_pixels_above_lines (GTK_TEXT_VIEW (priv->text_view), 0);
196         gtk_text_view_set_pixels_below_lines (GTK_TEXT_VIEW (priv->text_view), 0);
197         gtk_text_view_set_justification (GTK_TEXT_VIEW (priv->text_view), GTK_JUSTIFY_LEFT);
198         gtk_text_view_set_left_margin (GTK_TEXT_VIEW (priv->text_view), 0);
199         gtk_text_view_set_right_margin (GTK_TEXT_VIEW (priv->text_view), 0);
200
201         style = gtk_rc_get_style (GTK_WIDGET (scroll_text));
202         gtk_widget_modify_base (priv->text_view, GTK_STATE_NORMAL, & (style->bg[GTK_STATE_NORMAL]));
203
204         gtk_container_add (GTK_CONTAINER (scroll_text), priv->text_view);
205
206         if (GTK_WIDGET_REALIZED (scroll_text)) {
207                 gtk_widget_queue_resize (GTK_WIDGET (scroll_text));
208         }
209 }
210
211 static void
212 modest_scroll_text_instance_init (GTypeInstance *instance, gpointer g_class)
213 {
214         ModestScrollTextPriv *priv = MODEST_SCROLL_TEXT_GET_PRIVATE (instance);
215
216         gtk_scrolled_window_set_hadjustment (GTK_SCROLLED_WINDOW (instance), NULL);
217         gtk_scrolled_window_set_vadjustment (GTK_SCROLLED_WINDOW (instance), NULL);
218
219         priv->line_limit = MODEST_SCROLL_TEXT_DEFAULT_LINE_LIMIT;
220
221         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (instance), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
222
223         g_signal_connect (G_OBJECT (instance), "size-request", G_CALLBACK (size_request), NULL);
224         g_signal_connect (G_OBJECT (instance), "size-allocate", G_CALLBACK (size_allocate), NULL);
225
226         return;
227 }
228
229 static void
230 modest_scroll_text_finalize (GObject *object)
231 {
232         (*parent_class->finalize) (object);
233
234         return;
235 }
236
237 static void 
238 modest_scroll_text_class_init (ModestScrollTextClass *klass)
239 {
240         GObjectClass *object_class;
241         GtkWidgetClass *widget_class;
242
243         parent_class = g_type_class_peek_parent (klass);
244         object_class = (GObjectClass*) klass;
245         widget_class = GTK_WIDGET_CLASS (klass);
246
247         object_class->finalize = modest_scroll_text_finalize;
248
249         g_type_class_add_private (object_class, sizeof (ModestScrollTextPriv));
250
251         return;
252 }
253
254 GType 
255 modest_scroll_text_get_type (void)
256 {
257         static GType type = 0;
258
259         if (G_UNLIKELY(type == 0))
260         {
261                 static const GTypeInfo info = 
262                 {
263                   sizeof (ModestScrollTextClass),
264                   NULL,   /* base_init */
265                   NULL,   /* base_finalize */
266                   (GClassInitFunc) modest_scroll_text_class_init,   /* class_init */
267                   NULL,   /* class_finalize */
268                   NULL,   /* class_data */
269                   sizeof (ModestScrollText),
270                   0,      /* n_preallocs */
271                   modest_scroll_text_instance_init    /* instance_init */
272                 };
273
274                 type = g_type_register_static (GTK_TYPE_SCROLLED_WINDOW,
275                         "ModestScrollText",
276                         &info, 0);
277
278         }
279
280         return type;
281 }