2 * zavai - simple interface to the OpenMoko (or to the FSO stack)
4 * Copyright (C) 2009 Enrico Zini <enrico@enricozini.org>
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.
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.
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
23 protected struct FocusInfo
30 public class FisheyeList : Gtk.DrawingArea
32 protected Gtk.TreeModel model;
33 protected Gdk.Pixmap background;
34 protected Gdk.Pixmap backing_store;
36 // Pango layouts cached for speed
37 protected const int steps = 5;
38 protected Gtk.CellRendererText[] renderers;
39 protected int[] renderer_sizes;
40 protected int max_renderer_size;
42 // Labels to show, extracted from the model
43 protected string[] label_cache;
44 protected bool base_layout_needed;
49 protected int focus_first;
50 protected int focus_end;
51 protected FocusInfo[] focus_info;
52 protected bool focus_locked;
53 protected bool focus_layout_needed;
54 protected bool is_fisheye;
55 protected int focus_step_size;
57 // Number of elements in full focus
58 protected int _focus_size;
59 public int focus_size {
60 get { return _focus_size; }
63 focus_info = new FocusInfo[_focus_size+2*steps+1];
64 focus_layout_needed = true;
69 protected int _label_column;
70 public int label_column {
71 get { return _label_column; }
73 _label_column = value;
74 base_layout_needed = true;
79 //public virtual signal void cursor_changed ();
80 public signal void row_activated(Gtk.TreePath path);
89 renderers = new Gtk.CellRendererText[steps];
90 renderer_sizes = new int[steps];
92 // Defaults for properties
99 add_events(Gdk.EventMask.POINTER_MOTION_MASK
100 | Gdk.EventMask.BUTTON_PRESS_MASK
101 | Gdk.EventMask.BUTTON_RELEASE_MASK);
104 public unowned Gtk.TreeModel get_model() { return model; }
105 public void set_model (Gtk.TreeModel? model)
107 if (this.model != null)
109 this.model.row_changed -= on_row_changed;
110 this.model.row_deleted -= on_row_deleted;
111 this.model.row_has_child_toggled -= on_row_has_child_toggled;
112 this.model.row_inserted -= on_row_inserted;
113 this.model.rows_reordered -= on_rows_reordered;
116 this.model.row_changed += on_row_changed;
117 this.model.row_deleted += on_row_deleted;
118 this.model.row_has_child_toggled += on_row_has_child_toggled;
119 this.model.row_inserted += on_row_inserted;
120 this.model.rows_reordered += on_rows_reordered;
121 base_layout_needed = true;
125 private void on_row_changed(Gtk.TreePath path, Gtk.TreeIter iter) { base_layout_needed = true; }
126 private void on_row_deleted(Gtk.TreePath path) { base_layout_needed = true; }
127 private void on_row_has_child_toggled(Gtk.TreePath path, Gtk.TreeIter iter) { base_layout_needed = true; }
128 private void on_row_inserted(Gtk.TreePath path, Gtk.TreeIter iter) { base_layout_needed = true; }
129 private void on_rows_reordered(Gtk.TreePath path, Gtk.TreeIter iter, void* new_order) { base_layout_needed = true; }
131 /* Mouse button got pressed over widget */
133 public override bool button_press_event(Gdk.EventButton event)
135 stderr.printf("Mouse pressed on %d %s\n", cur_el, label_cache[cur_el]);
140 /* Mouse button got released */
141 public override bool button_release_event(Gdk.EventButton event)
143 stderr.printf("Mouse released on %d %s\n", cur_el, label_cache[cur_el]);
145 // Emit row_activated if applicable
149 if (model.iter_nth_child(out iter, null, cur_el))
151 Gtk.TreePath path = model.get_path(iter);
158 /* Mouse pointer moved over widget */
159 public override bool motion_notify_event(Gdk.EventMotion event)
161 int old_cur_el = cur_el;
162 int x = (int)event.x;
163 int y = (int)event.y;
167 focus_locked = !focus_layout_needed && x < allocation.width/2 && y >= focus_info[0].y && y < focus_info[focus_end - focus_first].y;
169 if (y < focus_info[0].y || y >= focus_info[focus_end - focus_first].y)
170 cur_el = y * (label_cache.length+1) / allocation.height;
172 for (int idx = focus_first; idx < focus_end; ++idx)
173 if (y < focus_info[idx-focus_first+1].y)
179 if (!focus_locked && old_cur_el != cur_el)
180 focus_layout_needed = true;
182 cur_el = y / max_renderer_size;
183 if (cur_el >= label_cache.length)
184 cur_el = label_cache.length - 1;
187 //stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
188 if (old_cur_el != cur_el)
196 public override bool configure_event (Gdk.EventConfigure event)
198 base_layout_needed = true;
203 /* Widget is asked to draw itself */
204 public override bool expose_event (Gdk.EventExpose event)
208 window.draw_drawable(
209 get_style().fg_gc[Gtk.StateType.NORMAL],
211 event.area.x, event.area.y,
212 event.area.x, event.area.y,
213 event.area.width, event.area.height);
218 public override void style_set(Gtk.Style? previous_style)
220 base_layout_needed = true;
222 public override void direction_changed(Gtk.TextDirection previous_direction)
224 base_layout_needed = true;
227 protected int el_y(int idx)
229 // Distorted position
230 int pos = fisheye(idx);
231 //stderr.printf("%d %f %f\n", idx, undy, pos);
235 protected Gtk.CellRendererText make_cell_renderer()
237 var res = new Gtk.CellRendererText();
238 res.font_desc = get_style().font_desc;
243 protected void base_layout()
245 // Rebuild label cache
248 label_cache = new string[0];
251 if (!model.get_iter_first(out iter))
253 label_cache = new string[0];
257 int count = model.iter_n_children(null);
258 label_cache = new string[count];
263 model.get(iter, _label_column, out val, -1);
264 label_cache[i] = val;
266 } while (model.iter_next(ref iter));
270 background = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
271 backing_store = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
273 // Recreate the renderers
274 renderers[renderers.length-1] = make_cell_renderer();
275 renderers[renderers.length-1].set_fixed_height_from_font(1);
276 renderers[renderers.length-1].get_size(this, null, null, null, null, out max_renderer_size);
277 renderer_sizes[renderers.length-1] = max_renderer_size;
279 is_fisheye = label_cache.length * max_renderer_size > allocation.height;
283 renderers[0] = make_cell_renderer();
284 renderers[0].size = Pango.SCALE;
285 renderers[0].set_fixed_height_from_font(1);
286 int min_renderer_size;
287 renderers[0].get_size(this, null, null, null, null, out min_renderer_size);
288 renderer_sizes[0] = min_renderer_size;
290 focus_step_size = 0; // Size of the diminishing area
291 for (int i = 1; i < renderers.length-1; ++i)
293 renderers[i] = make_cell_renderer();
294 renderers[i].scale = (double)i / renderers.length;
295 renderers[i].set_fixed_height_from_font(1);
297 renderers[i].get_size(this, null, null, null, null, out size);
298 renderer_sizes[i] = size;
299 focus_step_size += size;
304 draw_background(background);
306 base_layout_needed = false;
307 focus_layout_needed = true;
310 protected int el_renderer(int idx)
312 int fs2 = _focus_size/2;
315 renderer_idx = idx - (cur_el-fs2-steps);
317 renderer_idx = (cur_el+fs2+steps) - idx;
318 if (renderer_idx < 0)
320 if (renderer_idx >= renderer_sizes.length)
321 return renderer_sizes.length-1;
325 protected void focus_layout()
327 if (!is_fisheye || label_cache.length == 0)
331 focus_layout_needed = false;
335 int focus_area_size = 2 * focus_step_size + _focus_size * max_renderer_size;
336 int anchor_y = cur_el * allocation.height / (label_cache.length+1);
337 int focus_area_start = anchor_y - (focus_area_size - max_renderer_size) / 2;
338 if (focus_area_start < 0) focus_area_start = 0;
340 focus_first = cur_el - _focus_size/2 - steps;
341 if (focus_first < 0) focus_first = 0;
344 int full_focus_start = anchor_y - (_focus_size/2) * max_renderer_size;
345 int full_focus_end = anchor_y + (_focus_size/2) * max_renderer_size;
346 int steps_start = full_focus_start - focus_step_size;
349 int cur_pos = focus_area_start;
350 int cur_idx = focus_first;
351 while (cur_pos < allocation.height && cur_idx < label_cache.length && cur_idx - focus_first < focus_info.length)
353 focus_info[cur_idx - focus_first].y = cur_pos;
354 focus_info[cur_idx - focus_first].renderer = el_renderer(cur_idx);
355 // stderr.printf("LAYOUT [%d-%d-?] at %d/%d: pos %d rend %d rsz %d\n",
356 // focus_first, cur_idx, cur_idx - focus_first, focus_info.length, cur_pos, focus_info[cur_idx - focus_first].renderer, renderer_sizes[focus_info[cur_idx - focus_first].renderer]);
357 cur_pos += renderer_sizes[focus_info[cur_idx - focus_first].renderer];
362 focus_info[focus_end - focus_first].y = cur_pos;
363 focus_info[focus_end - focus_first].renderer = 0;
365 focus_layout_needed = false;
368 protected void draw_background(Gdk.Drawable drawable)
370 Gtk.Style style = get_style();
373 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height);
378 // Focus movement area
379 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true,
380 allocation.width/2, 0, allocation.width, allocation.height);
382 for (int y = 0; y < allocation.height/2 - 30; y += 30)
384 Gtk.paint_arrow(style, (Gdk.Window*)drawable, Gtk.StateType.INSENSITIVE, Gtk.ShadowType.NONE,
385 null, this, null, Gtk.ArrowType.UP, false,
386 allocation.width/2, y, allocation.width/2, 10);
387 Gtk.paint_arrow(style, (Gdk.Window*)drawable, Gtk.StateType.INSENSITIVE, Gtk.ShadowType.NONE,
388 null, this, null, Gtk.ArrowType.DOWN, false,
389 allocation.width/2, allocation.height-y-30, allocation.width/2, 10);
392 Gdk.Rectangle expose_area = Gdk.Rectangle();
393 expose_area.x = expose_area.y = 0;
394 expose_area.width = allocation.width;
395 expose_area.height = allocation.height;
397 Gdk.Rectangle cell_area = Gdk.Rectangle();
399 cell_area.width = allocation.width;
400 cell_area.height = renderer_sizes[0];
401 if (label_cache.length * cell_area.height >= allocation.height)
403 for (int y = 0; y < allocation.height; y += cell_area.height)
405 int idx = y * label_cache.length / allocation.height;
407 renderers[0].text = label_cache[idx];
408 renderers[0].render((Gdk.Window*)drawable, this,
415 int count = int.min(allocation.height/(2*cell_area.height), label_cache.length);
416 for (int idx = 0; idx < count; ++idx)
418 cell_area.y = idx * cell_area.height;
419 renderers[0].text = label_cache[idx];
420 renderers[0].render((Gdk.Window*)drawable, this,
426 cell_area.y = allocation.height-cell_area.y;
427 renderers[0].text = label_cache[label_cache.length-idx];
428 renderers[0].render((Gdk.Window*)drawable, this,
437 protected void draw()
439 if (base_layout_needed)
441 if (focus_layout_needed)
444 var drawable = backing_store;
445 Gtk.Style style = get_style();
446 Gdk.Rectangle expose_area = Gdk.Rectangle();
447 expose_area.x = expose_area.y = 0;
448 expose_area.width = allocation.width;
449 expose_area.height = allocation.height;
452 drawable.draw_drawable(
453 get_style().fg_gc[Gtk.StateType.NORMAL],
456 allocation.width, allocation.height);
461 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true,
462 0, focus_info[0].y, allocation.width/2, focus_info[focus_end - focus_first].y-focus_info[0].y);
464 // Paint items around focus
465 Gdk.Rectangle cell_area = Gdk.Rectangle();
467 cell_area.width = allocation.width;
468 for (int idx = 0; idx < focus_end-focus_first; ++idx)
470 var renderer = renderers[focus_info[idx].renderer];
471 cell_area.y = focus_info[idx].y;
472 cell_area.height = renderer_sizes[focus_info[idx].renderer];
474 Gtk.CellRendererState rflags = 0;
475 if (idx + focus_first == cur_el)
477 rflags |= Gtk.CellRendererState.SELECTED | Gtk.CellRendererState.FOCUSED;
478 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.SELECTED], true,
479 cell_area.x, cell_area.y, cell_area.width, cell_area.height);
482 renderer.text = label_cache[idx + focus_first];
483 renderer.render((Gdk.Window*)drawable, this,
489 //Gdk.draw_line(drawable, style.fg_gc[itemState], 0, y0, allocation.width, y0);
492 // Paint all items sequentially
493 var renderer = renderers[renderers.length-1];
494 Gdk.Rectangle cell_area = Gdk.Rectangle();
496 cell_area.width = allocation.width;
497 cell_area.height = max_renderer_size;
498 for (int idx = 0; idx < label_cache.length; ++idx)
500 cell_area.y = idx * cell_area.height;
502 Gtk.CellRendererState rflags = 0;
505 rflags |= Gtk.CellRendererState.SELECTED | Gtk.CellRendererState.FOCUSED;
506 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.SELECTED], true,
507 cell_area.x, cell_area.y, cell_area.width, cell_area.height);
510 renderer.text = label_cache[idx];
511 renderer.render((Gdk.Window*)drawable, this,
521 * The following function is adapted from Prefuse's FisheyeDistortion.java.
523 * A relevant annotation from Prefuse:
525 * For more details on this form of transformation, see Manojit Sarkar and
526 * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of
527 * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
528 * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
529 * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>.
533 * Distorts an item's coordinate.
534 * @param x the undistorted coordinate
535 * @param coordinate of the anchor or focus point
536 * @param d disortion factor
537 * @param min the beginning of the display
538 * @param max the end of the display
539 * @return the distorted coordinate
541 private int fisheye(int idx)
543 // Autocompute distortion factor
544 // 20 is the pixel size of the item at centre of focus
545 double d = label_cache.length * 20 / allocation.height;
547 return idx * allocation.height / label_cache.length;
549 double a = (double)cur_el * allocation.height / label_cache.length;
550 double x = (double)idx * allocation.height / label_cache.length;
551 double max = (double)allocation.height;
556 if ( m == 0 ) m = max;
557 double v = (double)(a - x) / m;
558 v = (double)(d+1)/(d+(1/v));
559 return (int)Math.round(a - m*v);
562 if ( m == 0 ) m = max;
563 double v = (double)(x - a) / m;
564 v = (double)(d+1)/(d+(1/v));
565 return (int)Math.round(a + m*v);
570 public class Fisheye : Gtk.Window
575 destroy += Gtk.main_quit;
577 var list = new FisheyeList();
580 var store = new Gtk.ListStore(1, typeof(string));
582 var infd = FileStream.open("/tmp/names", "r");
585 for (int i = 0; i < 300; ++i)
587 store.append(out iter);
588 store.set(iter, 0, "Antani %d".printf(i), -1);
594 string line = infd.gets(buf);
597 store.append(out iter);
598 store.set(iter, 0, line, -1);
602 list.set_model(store);
606 static int main (string[] args) {
609 var fe = new Fisheye();
610 fe.set_size_request(200, 300);