Use GTK+ single includes
[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/gtk.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_end (GTK_TEXT_VIEW (text_view), &iter))
101                         break;
102                 else 
103                         gtk_text_view_forward_display_line (GTK_TEXT_VIEW (text_view), &iter);
104         }
105
106         /* Change the adjustment properties for one line per step behavior */
107         adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (widget));
108         if (adj != NULL) {
109                 g_object_set (G_OBJECT (adj), "page-increment", (gdouble) iter_rectangle.height + 1, "step-increment", (gdouble) iter_rectangle.height + 1, NULL);
110                 gtk_adjustment_changed (adj);
111         }
112
113         /* Set the requisition height to the get the limit of lines or less */
114         if (line > 0) {
115                 requisition->height = iter_rectangle.height * MIN (line + 1, line_limit);
116         } else {
117                 requisition->height = iter_rectangle.height;
118         }
119
120         if (gtk_scrolled_window_get_shadow_type (GTK_SCROLLED_WINDOW (widget)) != GTK_SHADOW_NONE) {
121                 requisition->height += GTK_WIDGET (widget)->style->ythickness * 2;
122         }
123                 
124         priv->line_height = iter_rectangle.height;
125
126         /* Put again the cursor in the first character. Also scroll to first line */
127         gtk_text_buffer_place_cursor (buffer, &insert_iter);
128         gtk_text_view_scroll_mark_onscreen (GTK_TEXT_VIEW (text_view), insert_mark);
129
130 }
131
132 static void
133 size_allocate (GtkWidget *widget,
134                GtkAllocation *allocation,
135                gpointer user_data)
136 {
137         GtkAdjustment *adj = NULL;
138         ModestScrollTextPriv *priv = MODEST_SCROLL_TEXT_GET_PRIVATE (widget);
139
140         adj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (widget));
141         if (adj != NULL) {              
142                 g_object_set (G_OBJECT (adj), "page-increment", (gdouble) priv->line_height, "step-increment", (gdouble) priv->line_height, NULL);
143         }
144         gtk_adjustment_changed (adj);
145 }
146
147 void 
148 modest_scroll_text_set_line_limit (ModestScrollText *scroll_text, guint line_limit)
149 {
150         ModestScrollTextPriv *priv = MODEST_SCROLL_TEXT_GET_PRIVATE (scroll_text);
151
152         if (line_limit == priv->line_limit)
153                 return;
154
155         priv->line_limit = line_limit;
156         if (GTK_WIDGET_REALIZED (scroll_text)) {
157                 gtk_widget_queue_resize (GTK_WIDGET (scroll_text));
158         }
159 }
160
161 const GtkWidget *
162 modest_scroll_text_get_text_view (ModestScrollText *scroll_text)
163 {
164         ModestScrollTextPriv *priv = MODEST_SCROLL_TEXT_GET_PRIVATE (scroll_text);
165
166         if (priv->text_view == NULL)
167                 modest_scroll_text_set_text_view (scroll_text, NULL);
168
169         return priv->text_view;
170 }
171
172 void
173 modest_scroll_text_set_text_view (ModestScrollText *scroll_text,
174                                   GtkTextView *text_view)
175 {
176         ModestScrollTextPriv *priv = MODEST_SCROLL_TEXT_GET_PRIVATE (scroll_text);
177         GtkStyle *style;
178
179         g_return_if_fail (MODEST_IS_SCROLL_TEXT (scroll_text));
180         if (text_view == NULL) {
181                 text_view = GTK_TEXT_VIEW(gtk_text_view_new ());
182                 gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (text_view), FALSE);
183                 gtk_text_view_set_editable (GTK_TEXT_VIEW (text_view), FALSE);
184         }
185
186         if (priv->text_view == GTK_WIDGET(text_view))
187                 return;
188
189         if (priv->text_view != NULL) {
190                 gtk_container_remove (GTK_CONTAINER(scroll_text), priv->text_view);
191                 priv->text_view = NULL;
192         }
193
194         priv->text_view = GTK_WIDGET(text_view);
195
196         gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (priv->text_view), GTK_WRAP_WORD_CHAR);
197         gtk_text_view_set_pixels_above_lines (GTK_TEXT_VIEW (priv->text_view), 0);
198         gtk_text_view_set_pixels_below_lines (GTK_TEXT_VIEW (priv->text_view), 0);
199         gtk_text_view_set_justification (GTK_TEXT_VIEW (priv->text_view), GTK_JUSTIFY_LEFT);
200         gtk_text_view_set_left_margin (GTK_TEXT_VIEW (priv->text_view), 0);
201         gtk_text_view_set_right_margin (GTK_TEXT_VIEW (priv->text_view), 0);
202
203         style = gtk_rc_get_style (GTK_WIDGET (scroll_text));
204         gtk_widget_modify_base (priv->text_view, GTK_STATE_NORMAL, & (style->bg[GTK_STATE_NORMAL]));
205
206         gtk_container_add (GTK_CONTAINER (scroll_text), priv->text_view);
207
208         if (GTK_WIDGET_REALIZED (scroll_text)) {
209                 gtk_widget_queue_resize (GTK_WIDGET (scroll_text));
210         }
211 }
212
213 static void
214 modest_scroll_text_instance_init (GTypeInstance *instance, gpointer g_class)
215 {
216         ModestScrollTextPriv *priv = MODEST_SCROLL_TEXT_GET_PRIVATE (instance);
217
218         gtk_scrolled_window_set_hadjustment (GTK_SCROLLED_WINDOW (instance), NULL);
219         gtk_scrolled_window_set_vadjustment (GTK_SCROLLED_WINDOW (instance), NULL);
220
221         priv->line_limit = MODEST_SCROLL_TEXT_DEFAULT_LINE_LIMIT;
222
223         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (instance), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
224
225         g_signal_connect (G_OBJECT (instance), "size-request", G_CALLBACK (size_request), NULL);
226         g_signal_connect (G_OBJECT (instance), "size-allocate", G_CALLBACK (size_allocate), NULL);
227
228         return;
229 }
230
231 static void
232 modest_scroll_text_finalize (GObject *object)
233 {
234         (*parent_class->finalize) (object);
235
236         return;
237 }
238
239 static void 
240 modest_scroll_text_class_init (ModestScrollTextClass *klass)
241 {
242         GObjectClass *object_class;
243
244         parent_class = g_type_class_peek_parent (klass);
245         object_class = (GObjectClass*) 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 }