Cache Pango layouts
[gregoa/zavai.git] / src / fisheye.vala
1 /*
2  * zavai - simple interface to the OpenMoko (or to the FSO stack)
3  *
4  * Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 using GLib;
22
23 public class FisheyeList : Gtk.DrawingArea
24 {
25         protected Gdk.Pixmap backing_store;
26
27         private string[] list;
28         protected int cur_el;
29         protected double distortion_factor;
30
31         protected Pango.Layout[] pango_cache;
32
33         // Number of items shown before and after the focus element
34         protected int focus_first;
35         protected int focus_end;
36         protected int focus_size;
37         protected int[] focus_starts;
38         protected bool focus_locked;
39         protected int focus_centre;
40
41         public FisheyeList()
42         {
43                 backing_store = null;
44
45                 list = new string[300];
46                 for (int i = 0; i < 300; ++i)
47                         list[i] = "Lorem Ipsum %d".printf(i);
48
49                 pango_cache = new Pango.Layout[30];
50                 for (int i = 0; i < 30; ++i)
51                         pango_cache[i] = null;
52
53                 cur_el = 0;
54                 focus_centre = 0;
55                 focus_size = 20;
56                 focus_starts = new int[focus_size + 1];
57                 focus_locked = false;
58                 distortion_factor = 30;
59                 focus_layout();
60
61                 add_events(Gdk.EventMask.POINTER_MOTION_MASK
62                          | Gdk.EventMask.BUTTON_PRESS_MASK
63                          | Gdk.EventMask.BUTTON_RELEASE_MASK);
64         }
65
66         /* Mouse button got pressed over widget */
67         public override bool button_press_event(Gdk.EventButton event)
68         {
69                 stderr.printf("Mouse pressed on %d %s\n", cur_el, list[cur_el]);
70                 return false;
71         }
72
73         /* Mouse button got released */
74         public override bool button_release_event(Gdk.EventButton event)
75         {
76                 stderr.printf("Mouse released on %d %s\n", cur_el, list[cur_el]);
77                 // ...
78                 return false;
79         }
80
81         /* Mouse pointer moved over widget */
82         public override bool motion_notify_event(Gdk.EventMotion event)
83         {
84                 int old_cur_el = cur_el;
85                 int x = (int)event.x;
86                 int y = (int)event.y;
87
88                 focus_locked = x < allocation.width/2 && y >= focus_starts[0] && y < focus_starts[focus_end - focus_first];
89
90                 if (focus_locked)
91                 {
92                         for (int idx = focus_first; idx < focus_end; ++idx)
93                                 if (y < focus_starts[idx-focus_first+1])
94                                 {
95                                         cur_el = idx;
96                                         break;
97                                 }
98
99                 } else {
100                         cur_el = y * list.length / allocation.height;
101                         if (old_cur_el != cur_el)
102                                 focus_layout();
103                 }
104
105                 //stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
106                 if (old_cur_el != cur_el)
107                 {
108                         queue_draw();
109                         old_cur_el = cur_el;
110                 }
111                 return false;
112         }
113
114         public override bool configure_event (Gdk.EventConfigure event)
115         {
116                 backing_store = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
117                 focus_layout();
118                 queue_draw();
119                 return false;
120         }
121
122         /* Widget is asked to draw itself */
123         public override bool expose_event (Gdk.EventExpose event)
124         {
125                 if (backing_store == null)
126                         return false;
127
128                 draw(backing_store);
129
130                 window.draw_drawable(
131                         get_style().fg_gc[Gtk.StateType.NORMAL],
132                         backing_store,
133                         event.area.x, event.area.y,
134                         event.area.x, event.area.y,
135                         event.area.width, event.area.height);
136
137                 return false;
138         }
139
140         protected int el_y(int idx)
141         {
142                 // Undistorted Y
143                 int undy = idx * allocation.height / list.length;
144                 // Distorted position
145                 int pos = fisheye(undy, focus_centre, distortion_factor, 0, allocation.height);
146                 //stderr.printf("%d %f %f\n", idx, undy, pos);
147                 return pos;
148         }
149
150         protected void focus_layout()
151         {
152                 // Anchor point
153                 focus_centre = cur_el*allocation.height/list.length;
154
155                 focus_first = cur_el > focus_size/2 ? cur_el-focus_size/2 : 0;
156                 focus_end = focus_first + focus_size;
157                 if (focus_end >= list.length) focus_end = list.length;
158
159                 // Compute starting positions for all items in focus
160                 for (int idx = focus_first; idx < focus_end; ++idx)
161                 {
162                         int posprev = idx == 0 ? 0 : el_y(idx-1);
163                         int pos = el_y(idx);
164                         int posnext = idx == list.length-1 ? 1 : el_y(idx+1);
165                         int y0 = (pos+posprev)/2;
166                         int y1 = (pos+posnext)/2;
167
168                         focus_starts[idx - focus_first] = y0;
169                         focus_starts[idx - focus_first + 1] = y1;
170                 }
171         }
172
173         protected void draw(Gdk.Drawable drawable)
174         {
175                 Gtk.Style style = get_style();
176
177                 // Background
178                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height);
179
180                 // Focus lock area
181                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true,
182                         0, focus_starts[0], allocation.width/2, focus_starts[focus_end - focus_first]);
183
184                 // Focus movement area
185                 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true,
186                         allocation.width/2, 0, allocation.width, allocation.height);
187
188                 // Create a Cairo context
189                 //var context = Gdk.cairo_create (drawable);
190
191                 // Paint items around focus
192                 //context.select_font_face(style.font_desc.get_family(), Cairo.FontSlant.NORMAL, Cairo.FontWeight.NORMAL);
193                 for (int idx = focus_first; idx < focus_end; ++idx)
194                 {
195                         int y0 = focus_starts[idx - focus_first];
196                         int y1 = focus_starts[idx - focus_first + 1];
197
198                         Gtk.StateType itemState = Gtk.StateType.NORMAL;
199                         if (idx == cur_el)
200                         {
201                                 itemState = Gtk.StateType.SELECTED;
202                                 drawable.draw_rectangle(style.bg_gc[itemState], true,
203                                         0, y0, allocation.width, y1-y0);
204                         }
205
206                 
207                         // TODO: cache pango contexts instead of fontdescs
208                         int size = (y1-y0)*80/100;
209                         if (size <= 0) size = 1;
210                         if (size >= pango_cache.length) size = pango_cache.length - 1;
211                         if (pango_cache[size] == null)
212                         {
213                                 var fd = style.font_desc.copy();
214                                 fd.set_absolute_size(size*Pango.SCALE);
215                                 var pc = create_pango_context();
216                                 pc.set_font_description(fd);
217                                 pango_cache[size] = new Pango.Layout(pc);
218                         }
219                         pango_cache[size].set_text(list[idx], -1);
220                         var layout = pango_cache[size];
221                         //stderr.printf("AZAZA %p\n", layout.get_attributes());
222                         //var attrlist = layout.get_attributes().copy();
223                         //stderr.printf("AL %p\n", attrlist);
224                         //var attrlist = new Pango.AttrList();
225                         //stderr.printf("SIZE %d\n", y1-y0);
226                         //attrlist.insert(new Pango.AttrSize(y1-y0));
227                         //var attrlist = layout.get_attributes();
228                         //attrlist.change(new Pango.AttrSize(y1-y0));
229                         //layout.set_attributes(attrlist);
230                         //layout.set_height(y1-y0);
231                         //int w, h;
232                         //layout.get_pixel_size(out w, out h);
233                         Gdk.draw_layout(drawable, style.fg_gc[itemState], 0, y0, layout);
234                 }
235         }
236
237     /*
238      * The following function is adapted from Prefuse's FisheyeDistortion.java.
239      *
240      * A relevant annotation from Prefuse:
241      *
242      * For more details on this form of transformation, see Manojit Sarkar and 
243      * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of 
244      * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
245      * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
246      * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>. 
247      */
248
249     /*
250      * Distorts an item's coordinate.
251      * @param x the undistorted coordinate
252      * @param coordinate of the anchor or focus point
253      * @param d disortion factor
254      * @param min the beginning of the display
255      * @param max the end of the display
256      * @return the distorted coordinate
257      */
258     private int fisheye(int x, int a, double d, int min, int max)
259     {
260         if ( d != 0 ) {
261             bool left = x<a;
262             double v;
263             int m = (left ? a-min : max-a);
264             if ( m == 0 ) m = max-min;
265             v = (double)(x - a).abs() / m;
266             v = (d+1)/(d+(1/v));
267             return (int)Math.round((left?-1:1)*m*v + a);
268         } else {
269             return x;
270         }
271     }
272 }
273
274 public class Fisheye : Gtk.Window
275 {
276         public Fisheye()
277         {
278                 title = "Fisheye";
279                 destroy += Gtk.main_quit;
280
281                 var list = new FisheyeList();
282                 add(list);
283         }
284 }
285
286 static int main (string[] args) {
287         Gtk.init (ref args);
288
289         var fe = new Fisheye();
290         fe.set_size_request(200, 300);
291         fe.show_all();
292
293         Gtk.main();
294
295         return 0;
296 }