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;
56 protected int focus_area_start;
58 // Number of elements in full focus
59 protected int _focus_size;
60 public int focus_size {
61 get { return _focus_size; }
64 focus_info = new FocusInfo[_focus_size+2*steps+1];
65 focus_layout_needed = true;
70 protected int _label_column;
71 public int label_column {
72 get { return _label_column; }
74 _label_column = value;
75 base_layout_needed = true;
80 //public virtual signal void cursor_changed ();
81 public signal void row_activated(Gtk.TreePath path);
90 renderers = new Gtk.CellRendererText[steps];
91 renderer_sizes = new int[steps];
93 // Defaults for properties
100 add_events(Gdk.EventMask.POINTER_MOTION_MASK
101 | Gdk.EventMask.BUTTON_PRESS_MASK
102 | Gdk.EventMask.BUTTON_RELEASE_MASK);
105 public unowned Gtk.TreeModel get_model() { return model; }
106 public void set_model (Gtk.TreeModel? model)
108 if (this.model != null)
110 this.model.row_changed -= on_row_changed;
111 this.model.row_deleted -= on_row_deleted;
112 this.model.row_has_child_toggled -= on_row_has_child_toggled;
113 this.model.row_inserted -= on_row_inserted;
114 this.model.rows_reordered -= on_rows_reordered;
117 this.model.row_changed += on_row_changed;
118 this.model.row_deleted += on_row_deleted;
119 this.model.row_has_child_toggled += on_row_has_child_toggled;
120 this.model.row_inserted += on_row_inserted;
121 this.model.rows_reordered += on_rows_reordered;
122 base_layout_needed = true;
126 private void on_row_changed(Gtk.TreePath path, Gtk.TreeIter iter) { base_layout_needed = true; }
127 private void on_row_deleted(Gtk.TreePath path) { base_layout_needed = true; }
128 private void on_row_has_child_toggled(Gtk.TreePath path, Gtk.TreeIter iter) { base_layout_needed = true; }
129 private void on_row_inserted(Gtk.TreePath path, Gtk.TreeIter iter) { base_layout_needed = true; }
130 private void on_rows_reordered(Gtk.TreePath path, Gtk.TreeIter iter, void* new_order) { base_layout_needed = true; }
132 /* Mouse button got pressed over widget */
134 public override bool button_press_event(Gdk.EventButton event)
136 stderr.printf("Mouse pressed on %d %s\n", cur_el, label_cache[cur_el]);
141 /* Mouse button got released */
142 public override bool button_release_event(Gdk.EventButton event)
144 stderr.printf("Mouse released on %d %s\n", cur_el, label_cache[cur_el]);
146 // Emit row_activated if applicable
150 if (model.iter_nth_child(out iter, null, cur_el))
152 Gtk.TreePath path = model.get_path(iter);
159 /* Mouse pointer moved over widget */
160 public override bool motion_notify_event(Gdk.EventMotion event)
162 int old_cur_el = cur_el;
163 int x = (int)event.x;
164 int y = (int)event.y;
168 focus_locked = !focus_layout_needed && x < allocation.width/2 && y >= focus_area_start+focus_info[0].y && y < focus_area_start+focus_info[focus_end - focus_first].y;
170 if (y < focus_area_start+focus_info[0].y || y >= focus_area_start+focus_info[focus_end - focus_first].y)
171 cur_el = y * (label_cache.length+1) / allocation.height;
173 for (int idx = 0; idx < focus_info.length; ++idx)
174 if (y - focus_area_start < focus_info[idx].y + renderer_sizes[focus_info[idx].renderer])
176 cur_el = idx + focus_first;
180 if (!focus_locked && old_cur_el != cur_el)
181 focus_layout_needed = true;
183 cur_el = y / max_renderer_size;
184 if (cur_el >= label_cache.length)
185 cur_el = label_cache.length - 1;
188 //stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el);
189 if (old_cur_el != cur_el)
197 public override bool configure_event (Gdk.EventConfigure event)
199 base_layout_needed = true;
204 /* Widget is asked to draw itself */
205 public override bool expose_event (Gdk.EventExpose event)
209 window.draw_drawable(
210 get_style().fg_gc[Gtk.StateType.NORMAL],
212 event.area.x, event.area.y,
213 event.area.x, event.area.y,
214 event.area.width, event.area.height);
219 public override void style_set(Gtk.Style? previous_style)
221 base_layout_needed = true;
223 public override void direction_changed(Gtk.TextDirection previous_direction)
225 base_layout_needed = true;
228 protected int el_y(int idx)
230 // Distorted position
231 int pos = fisheye(idx);
232 //stderr.printf("%d %f %f\n", idx, undy, pos);
236 protected Gtk.CellRendererText make_cell_renderer()
238 var res = new Gtk.CellRendererText();
239 res.font_desc = get_style().font_desc;
244 protected void base_layout()
246 // Rebuild label cache
249 label_cache = new string[0];
252 if (!model.get_iter_first(out iter))
254 label_cache = new string[0];
258 int count = model.iter_n_children(null);
259 label_cache = new string[count];
264 model.get(iter, _label_column, out val, -1);
265 label_cache[i] = val;
267 } while (model.iter_next(ref iter));
271 background = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
272 backing_store = new Gdk.Pixmap(window, allocation.width, allocation.height, -1);
274 // Recreate the renderers
275 renderers[renderers.length-1] = make_cell_renderer();
276 renderers[renderers.length-1].set_fixed_height_from_font(1);
277 renderers[renderers.length-1].get_size(this, null, null, null, null, out max_renderer_size);
278 renderer_sizes[renderers.length-1] = max_renderer_size;
280 is_fisheye = label_cache.length * max_renderer_size > allocation.height;
284 renderers[0] = make_cell_renderer();
285 renderers[0].size = Pango.SCALE;
286 renderers[0].set_fixed_height_from_font(1);
287 int min_renderer_size;
288 renderers[0].get_size(this, null, null, null, null, out min_renderer_size);
289 renderer_sizes[0] = min_renderer_size;
291 focus_step_size = 0; // Size of the diminishing area
292 for (int i = 1; i < renderers.length-1; ++i)
294 renderers[i] = make_cell_renderer();
295 renderers[i].scale = (double)i / renderers.length;
296 renderers[i].set_fixed_height_from_font(1);
298 renderers[i].get_size(this, null, null, null, null, out size);
299 renderer_sizes[i] = size;
300 focus_step_size += size;
305 draw_background(background);
307 base_layout_needed = false;
308 focus_layout_needed = true;
311 protected int el_renderer(int idx)
313 int fs2 = _focus_size/2;
316 renderer_idx = idx - (cur_el-fs2-steps);
318 renderer_idx = (cur_el+fs2+steps) - idx;
319 if (renderer_idx < 0)
321 if (renderer_idx >= renderer_sizes.length)
322 return renderer_sizes.length-1;
326 protected void focus_layout()
328 if (!is_fisheye || label_cache.length == 0)
332 focus_layout_needed = false;
336 focus_first = cur_el - _focus_size/2 - steps;
337 if (focus_first < 0) focus_first = 0;
338 if (focus_first + focus_info.length > label_cache.length)
339 focus_first = label_cache.length - focus_info.length;
342 int full_focus_start = anchor_y - (_focus_size/2) * max_renderer_size;
343 int full_focus_end = anchor_y + (_focus_size/2) * max_renderer_size;
344 int steps_start = full_focus_start - focus_step_size;
349 int focus_area_pre = 0;
350 while (cur_pos < allocation.height && cur_idx < focus_info.length)
352 if (focus_first + cur_idx == cur_el)
353 focus_area_pre = cur_pos;
354 focus_info[cur_idx].y = cur_pos;
355 focus_info[cur_idx].renderer = el_renderer(focus_first + cur_idx);
356 // stderr.printf("LAYOUT %d+[0-%d-%d] item %d/%d: pos %d rend %d rsz %d\n",
357 // focus_first, cur_idx, focus_info.length, focus_first + cur_idx, label_cache.length,
358 // cur_pos, focus_info[cur_idx].renderer, renderer_sizes[focus_info[cur_idx].renderer]);
359 cur_pos += renderer_sizes[focus_info[cur_idx].renderer];
363 focus_info[cur_idx].y = cur_pos;
364 focus_info[cur_idx].renderer = 0;
365 focus_end = focus_first + cur_idx;
367 int anchor_y = cur_el * allocation.height / (label_cache.length+1);
368 int focus_area_size = cur_pos;
370 focus_area_start = anchor_y - focus_area_pre;
371 if (focus_area_start < 0) focus_area_start = 0;
372 if (focus_area_start + focus_area_size > allocation.height)
373 focus_area_start = allocation.height - focus_area_size;
375 // stderr.printf("FA [0 [%d+%d=%d] %d]\n", focus_area_start, focus_area_size, focus_area_start+focus_area_size, allocation.height);
377 focus_layout_needed = false;
380 protected void draw_background(Gdk.Drawable drawable)
382 Gtk.Style style = get_style();
385 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height);
390 // Focus movement area
391 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true,
392 allocation.width/2, 0, allocation.width, allocation.height);
394 for (int y = 0; y < allocation.height/2 - 30; y += 30)
396 Gtk.paint_arrow(style, (Gdk.Window*)drawable, Gtk.StateType.INSENSITIVE, Gtk.ShadowType.NONE,
397 null, this, null, Gtk.ArrowType.UP, false,
398 allocation.width/2, y, allocation.width/2, 10);
399 Gtk.paint_arrow(style, (Gdk.Window*)drawable, Gtk.StateType.INSENSITIVE, Gtk.ShadowType.NONE,
400 null, this, null, Gtk.ArrowType.DOWN, false,
401 allocation.width/2, allocation.height-y-30, allocation.width/2, 10);
404 Gdk.Rectangle expose_area = Gdk.Rectangle();
405 expose_area.x = expose_area.y = 0;
406 expose_area.width = allocation.width;
407 expose_area.height = allocation.height;
409 Gdk.Rectangle cell_area = Gdk.Rectangle();
411 cell_area.width = allocation.width;
412 cell_area.height = renderer_sizes[0];
413 if (label_cache.length * cell_area.height >= allocation.height)
415 for (int y = 0; y < allocation.height; y += cell_area.height)
417 int idx = y * label_cache.length / allocation.height;
419 renderers[0].text = label_cache[idx];
420 renderers[0].render((Gdk.Window*)drawable, this,
427 int count = int.min(allocation.height/(2*cell_area.height), label_cache.length);
428 for (int idx = 0; idx < count; ++idx)
430 cell_area.y = idx * cell_area.height;
431 renderers[0].text = label_cache[idx];
432 renderers[0].render((Gdk.Window*)drawable, this,
438 cell_area.y = allocation.height-cell_area.y;
439 renderers[0].text = label_cache[label_cache.length-idx];
440 renderers[0].render((Gdk.Window*)drawable, this,
449 protected void draw()
451 if (base_layout_needed)
453 if (focus_layout_needed)
456 var drawable = backing_store;
457 Gtk.Style style = get_style();
458 Gdk.Rectangle expose_area = Gdk.Rectangle();
459 expose_area.x = expose_area.y = 0;
460 expose_area.width = allocation.width;
461 expose_area.height = allocation.height;
464 drawable.draw_drawable(
465 get_style().fg_gc[Gtk.StateType.NORMAL],
468 allocation.width, allocation.height);
473 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true,
474 0, focus_area_start + focus_info[0].y, allocation.width/2, focus_info[focus_end - focus_first].y);
476 // Paint items around focus
477 Gdk.Rectangle cell_area = Gdk.Rectangle();
479 cell_area.width = allocation.width;
480 for (int idx = 0; idx < focus_info.length; ++idx)
482 var renderer = renderers[focus_info[idx].renderer];
483 cell_area.y = focus_area_start + focus_info[idx].y;
484 cell_area.height = renderer_sizes[focus_info[idx].renderer];
486 Gtk.CellRendererState rflags = 0;
487 if (idx + focus_first == cur_el)
489 rflags |= Gtk.CellRendererState.SELECTED | Gtk.CellRendererState.FOCUSED;
490 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.SELECTED], true,
491 cell_area.x, cell_area.y, cell_area.width, cell_area.height);
494 renderer.text = label_cache[idx + focus_first];
495 renderer.render((Gdk.Window*)drawable, this,
501 //Gdk.draw_line(drawable, style.fg_gc[itemState], 0, y0, allocation.width, y0);
504 // Paint all items sequentially
505 var renderer = renderers[renderers.length-1];
506 Gdk.Rectangle cell_area = Gdk.Rectangle();
508 cell_area.width = allocation.width;
509 cell_area.height = max_renderer_size;
510 for (int idx = 0; idx < label_cache.length; ++idx)
512 cell_area.y = idx * cell_area.height;
514 Gtk.CellRendererState rflags = 0;
517 rflags |= Gtk.CellRendererState.SELECTED | Gtk.CellRendererState.FOCUSED;
518 drawable.draw_rectangle(style.bg_gc[Gtk.StateType.SELECTED], true,
519 cell_area.x, cell_area.y, cell_area.width, cell_area.height);
522 renderer.text = label_cache[idx];
523 renderer.render((Gdk.Window*)drawable, this,
533 * The following function is adapted from Prefuse's FisheyeDistortion.java.
535 * A relevant annotation from Prefuse:
537 * For more details on this form of transformation, see Manojit Sarkar and
538 * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of
539 * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available
540 * online at <a href="http://citeseer.ist.psu.edu/sarkar92graphical.html">
541 * http://citeseer.ist.psu.edu/sarkar92graphical.html</a>.
545 * Distorts an item's coordinate.
546 * @param x the undistorted coordinate
547 * @param coordinate of the anchor or focus point
548 * @param d disortion factor
549 * @param min the beginning of the display
550 * @param max the end of the display
551 * @return the distorted coordinate
553 private int fisheye(int idx)
555 // Autocompute distortion factor
556 // 20 is the pixel size of the item at centre of focus
557 double d = label_cache.length * 20 / allocation.height;
559 return idx * allocation.height / label_cache.length;
561 double a = (double)cur_el * allocation.height / label_cache.length;
562 double x = (double)idx * allocation.height / label_cache.length;
563 double max = (double)allocation.height;
568 if ( m == 0 ) m = max;
569 double v = (double)(a - x) / m;
570 v = (double)(d+1)/(d+(1/v));
571 return (int)Math.round(a - m*v);
574 if ( m == 0 ) m = max;
575 double v = (double)(x - a) / m;
576 v = (double)(d+1)/(d+(1/v));
577 return (int)Math.round(a + m*v);
582 public class Fisheye : Gtk.Window
587 destroy += Gtk.main_quit;
589 var list = new FisheyeList();
592 var store = new Gtk.ListStore(1, typeof(string));
594 var infd = FileStream.open("/tmp/names", "r");
597 for (int i = 0; i < 300; ++i)
599 store.append(out iter);
600 store.set(iter, 0, "Antani %d".printf(i), -1);
606 string line = infd.gets(buf);
609 store.append(out iter);
610 store.set(iter, 0, line, -1);
614 list.set_model(store);
618 static int main (string[] args) {
621 var fe = new Fisheye();
622 fe.set_size_request(200, 300);