Add ModestScrollable interface, which will wrap pannable area and scrolled
[modest] / src / widgets / modest-scrollable.c
1 /* Copyright (c) 2009, Igalia
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 <modest-scrollable.h>
33
34 /**
35  * modest_scrollable_add_with_viewport:
36  * @scrollable: a #ModestScrollable instance
37  * @widget: a #GtkWidget
38  *
39  * adds a widget to the @scrollable, wrapping it into a viewport.
40  * This is useful if @widget does not implement scroll_adjustments
41  * interface.
42  */
43 void            
44 modest_scrollable_add_with_viewport (ModestScrollable *scrollable,
45                                      GtkWidget *widget)
46 {
47         if (MODEST_SCROLLABLE_GET_IFACE (scrollable)->add_with_viewport) {
48                 MODEST_SCROLLABLE_GET_IFACE (scrollable)->add_with_viewport (scrollable, widget);
49         }
50 }
51
52 /**
53  * modest_scrollable_get_vadjustment:
54  * @self: a #ModestScrollable
55  *
56  * obtains the vertical adjustment for @self
57  *
58  * Returns: a #GtkAdjustment
59  */
60 GtkAdjustment *
61 modest_scrollable_get_vadjustment (ModestScrollable *self)
62 {
63         if (MODEST_SCROLLABLE_GET_IFACE (self)->get_vadjustment) {
64                 return MODEST_SCROLLABLE_GET_IFACE (self)->get_vadjustment (self);
65         } else {
66                 return NULL;
67         }
68 }
69
70 /**
71  * modest_scrollable_get_hadjustment:
72  * @self: a #ModestScrollable
73  *
74  * obtains the horizontal adjustment for @self
75  *
76  * Returns: a #GtkAdjustment
77  */
78 GtkAdjustment *
79 modest_scrollable_get_hadjustment (ModestScrollable *self)
80 {
81         if (MODEST_SCROLLABLE_GET_IFACE (self)->get_hadjustment) {
82                 return MODEST_SCROLLABLE_GET_IFACE (self)->get_hadjustment (self);
83         } else {
84                 return NULL;
85         }
86 }
87
88 /**
89  * modest_scrollable_scroll_to:
90  * @scrollable: a #ModestScrollable instance
91  * @x: the X coordinate to scroll to
92  * @y: the Y coordinate to scroll to
93  *
94  * scrolls softly visible area of @scrollable, to show the coordinates
95  * (@x, @y).
96  */
97 void            
98 modest_scrollable_scroll_to (ModestScrollable *scrollable,
99                              const gint x, const gint y)
100 {
101         if (MODEST_SCROLLABLE_GET_IFACE (scrollable)->scroll_to) {
102                 MODEST_SCROLLABLE_GET_IFACE (scrollable)->scroll_to (scrollable, x, y);
103         }
104 }
105
106 /**
107  * modest_scrollable_jump_to:
108  * @scrollable: a #ModestScrollable instance
109  * @x: the X coordinate to jump to
110  * @y: the Y coordinate to jump to
111  *
112  * scrolls visible area of @scrollable, to show the coordinates
113  * (@x, @y). Movement is immediate.
114  */
115 void            
116 modest_scrollable_jump_to (ModestScrollable *scrollable,
117                              const gint x, const gint y)
118 {
119         if (MODEST_SCROLLABLE_GET_IFACE (scrollable)->jump_to) {
120                 MODEST_SCROLLABLE_GET_IFACE (scrollable)->jump_to (scrollable, x, y);
121         }
122 }
123
124
125
126 static void
127 modest_scrollable_base_init (gpointer g_iface)
128 {
129         static gboolean initialized = FALSE;
130
131         if (!initialized) {
132                 /* create interface signals here. */
133                 initialized = TRUE;
134
135                 g_object_interface_install_property (g_iface,
136                                                      g_param_spec_enum ("vscrollbar_policy",
137                                                                         "vscrollbar policy",
138                                                                         "Visual policy of the vertical scrollbar",
139                                                                         GTK_TYPE_POLICY_TYPE,
140                                                                         GTK_POLICY_AUTOMATIC,
141                                                                         G_PARAM_READWRITE |
142                                                                         G_PARAM_CONSTRUCT));
143
144                 g_object_interface_install_property (g_iface,
145                                                      g_param_spec_enum ("hscrollbar_policy",
146                                                                         "hscrollbar policy",
147                                                                         "Visual policy of the horizontal scrollbar",
148                                                                         GTK_TYPE_POLICY_TYPE,
149                                                                         GTK_POLICY_AUTOMATIC,
150                                                                         G_PARAM_READWRITE |
151                                                                         G_PARAM_CONSTRUCT));
152
153
154         }
155 }
156
157 GType
158 modest_scrollable_get_type (void)
159 {
160         static GType type = 0;
161
162         if (G_UNLIKELY(type == 0)) 
163         {
164                 static const GTypeInfo info = 
165                 {
166                   sizeof (ModestScrollableIface),
167                   modest_scrollable_base_init,   /* base_init */
168                   NULL,   /* base_finalize */
169                   NULL,   /* class_init */
170                   NULL,   /* class_finalize */
171                   NULL,   /* class_data */
172                   0,
173                   0,      /* n_preallocs */
174                   NULL,   /* instance_init */
175                   NULL
176                 };
177
178                 type = g_type_register_static (G_TYPE_INTERFACE,
179                         "ModestScrollable", &info, 0);
180
181         }
182
183         return type;
184 }