* Added modest-scroll-text.[ch]
[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
84         text_view = modest_scroll_text_get_text_view (MODEST_SCROLL_TEXT (widget));
85         line_limit = priv->line_limit;
86
87         buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
88
89         /* get the first line and the height of the line */
90         gtk_text_buffer_get_start_iter (buffer, &iter);
91         gtk_text_view_get_iter_location (GTK_TEXT_VIEW (text_view), &iter, &iter_rectangle);
92
93         /* Count lines in text view */
94         for (line = 0; line < line_limit; line++) {
95                 if (!gtk_text_view_forward_display_line (GTK_TEXT_VIEW (text_view), &iter))
96                         break;
97         }
98
99         /* Put again the cursor in the first character. Also scroll to first line */
100         gtk_text_buffer_get_start_iter (buffer, &iter);
101         gtk_text_buffer_place_cursor (buffer, &iter);
102         gtk_text_view_place_cursor_onscreen (GTK_TEXT_VIEW (text_view));
103
104         /* Change the adjustment properties for one line per step behavior */
105         adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (widget));
106         if (adj != NULL) {
107                 g_object_set (G_OBJECT (adj), "page-increment", (gdouble) iter_rectangle.height, "step-increment", (gdouble) iter_rectangle.height, NULL);
108                 gtk_adjustment_changed (adj);
109         }
110
111         /* Set the requisition height to the get the limit of lines or less */
112         if (line > 0) {
113                 requisition->height = iter_rectangle.height * MAX (line, line_limit);
114         } else {
115                 requisition->height = iter_rectangle.height;
116         }
117
118         priv->line_height = iter_rectangle.height;
119
120 }
121
122 static void
123 size_allocate (GtkWidget *widget,
124                GtkAllocation *allocation,
125                gpointer user_data)
126 {
127         GtkAdjustment *adj = NULL;
128         ModestScrollTextPriv *priv = MODEST_SCROLL_TEXT_GET_PRIVATE (widget);
129
130         adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (widget));
131         if (adj != NULL) {              
132                 g_object_set (G_OBJECT (adj), "page-increment", (gdouble) priv->line_height, "step-increment", (gdouble) priv->line_height, NULL);
133         }
134         gtk_adjustment_changed (adj);
135 }
136
137 void 
138 modest_scroll_text_set_line_limit (ModestScrollText *scroll_text, guint line_limit)
139 {
140         ModestScrollTextPriv *priv = MODEST_SCROLL_TEXT_GET_PRIVATE (scroll_text);
141
142         if (line_limit == priv->line_limit)
143                 return;
144
145         priv->line_limit = line_limit;
146         if (GTK_WIDGET_REALIZED (scroll_text)) {
147                 gtk_widget_queue_resize (GTK_WIDGET (scroll_text));
148         }
149 }
150
151 const GtkWidget *
152 modest_scroll_text_get_text_view (ModestScrollText *scroll_text)
153 {
154         ModestScrollTextPriv *priv = MODEST_SCROLL_TEXT_GET_PRIVATE (scroll_text);
155
156         if (priv->text_view == NULL)
157                 modest_scroll_text_set_text_view (scroll_text, NULL);
158
159         return priv->text_view;
160 }
161
162 void
163 modest_scroll_text_set_text_view (ModestScrollText *scroll_text,
164                                   GtkTextView *text_view)
165 {
166         ModestScrollTextPriv *priv = MODEST_SCROLL_TEXT_GET_PRIVATE (scroll_text);
167
168         g_return_if_fail (MODEST_IS_SCROLL_TEXT (scroll_text));
169         if (text_view == NULL) {
170                 text_view = GTK_TEXT_VIEW(gtk_text_view_new ());
171                 gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (text_view), FALSE);
172                 gtk_text_view_set_editable (GTK_TEXT_VIEW (text_view), FALSE);
173         }
174
175         if (priv->text_view == GTK_WIDGET(text_view))
176                 return;
177
178         if (priv->text_view != NULL) {
179                 gtk_container_remove (GTK_CONTAINER(scroll_text), priv->text_view);
180                 priv->text_view = NULL;
181         }
182
183         priv->text_view = GTK_WIDGET(text_view);
184
185         gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (priv->text_view), GTK_WRAP_WORD_CHAR);
186         gtk_text_view_set_pixels_above_lines (GTK_TEXT_VIEW (priv->text_view), 0);
187         gtk_text_view_set_pixels_below_lines (GTK_TEXT_VIEW (priv->text_view), 0);
188         gtk_text_view_set_justification (GTK_TEXT_VIEW (priv->text_view), GTK_JUSTIFY_LEFT);
189         gtk_text_view_set_left_margin (GTK_TEXT_VIEW (priv->text_view), 0);
190         gtk_text_view_set_right_margin (GTK_TEXT_VIEW (priv->text_view), 0);
191
192         gtk_container_add (GTK_CONTAINER (scroll_text), priv->text_view);
193
194         if (GTK_WIDGET_REALIZED (scroll_text)) {
195                 gtk_widget_queue_resize (GTK_WIDGET (scroll_text));
196         }
197 }
198
199 static void
200 modest_scroll_text_instance_init (GTypeInstance *instance, gpointer g_class)
201 {
202         ModestScrollTextPriv *priv = MODEST_SCROLL_TEXT_GET_PRIVATE (instance);
203
204         gtk_scrolled_window_set_hadjustment (GTK_SCROLLED_WINDOW (instance), NULL);
205         gtk_scrolled_window_set_vadjustment (GTK_SCROLLED_WINDOW (instance), NULL);
206
207         priv->line_limit = MODEST_SCROLL_TEXT_DEFAULT_LINE_LIMIT;
208
209         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (instance), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
210
211         g_signal_connect (G_OBJECT (instance), "size-request", G_CALLBACK (size_request), NULL);
212         g_signal_connect (G_OBJECT (instance), "size-allocate", G_CALLBACK (size_allocate), NULL);
213
214         return;
215 }
216
217 static void
218 modest_scroll_text_finalize (GObject *object)
219 {
220         (*parent_class->finalize) (object);
221
222         return;
223 }
224
225 static void 
226 modest_scroll_text_class_init (ModestScrollTextClass *klass)
227 {
228         GObjectClass *object_class;
229         GtkWidgetClass *widget_class;
230
231         parent_class = g_type_class_peek_parent (klass);
232         object_class = (GObjectClass*) klass;
233         widget_class = GTK_WIDGET_CLASS (klass);
234
235         object_class->finalize = modest_scroll_text_finalize;
236
237         g_type_class_add_private (object_class, sizeof (ModestScrollTextPriv));
238
239         return;
240 }
241
242 GType 
243 modest_scroll_text_get_type (void)
244 {
245         static GType type = 0;
246
247         if (G_UNLIKELY(type == 0))
248         {
249                 static const GTypeInfo info = 
250                 {
251                   sizeof (ModestScrollTextClass),
252                   NULL,   /* base_init */
253                   NULL,   /* base_finalize */
254                   (GClassInitFunc) modest_scroll_text_class_init,   /* class_init */
255                   NULL,   /* class_finalize */
256                   NULL,   /* class_data */
257                   sizeof (ModestScrollText),
258                   0,      /* n_preallocs */
259                   modest_scroll_text_instance_init    /* instance_init */
260                 };
261
262                 type = g_type_register_static (GTK_TYPE_SCROLLED_WINDOW,
263                         "ModestScrollText",
264                         &info, 0);
265
266         }
267
268         return type;
269 }