/* * zavai - simple interface to the OpenMoko (or to the FSO stack) * * Copyright (C) 2009 Enrico Zini * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ using GLib; public class FisheyeList : Gtk.DrawingArea { protected Gtk.TreeModel model; protected Gdk.Pixmap background; protected Gdk.Pixmap backing_store; // Pango layouts cached for speed protected const int steps = 5; protected Gtk.CellRendererText[] renderers; // Labels to show, extracted from the model protected string[] label_cache; protected bool base_layout_needed; protected int cur_el; // Layout information protected int focus_first; protected int focus_end; protected int[] focus_starts; protected bool focus_locked; protected bool focus_layout_needed; protected int _focus_size; public int focus_size { get { return _focus_size; } set { _focus_size = value; focus_starts = new int[value+1]; focus_layout_needed = true; queue_draw(); } } protected int _label_column; public int label_column { get { return _label_column; } set { _label_column = value; base_layout_needed = true; queue_draw(); } } //public virtual signal void cursor_changed (); public signal void row_activated(Gtk.TreePath path); public FisheyeList() { model = null; backing_store = null; label_cache = null; renderers = new Gtk.CellRendererText[steps]; // Defaults for properties focus_size = 21; label_column = 0; cur_el = 0; focus_locked = false; add_events(Gdk.EventMask.POINTER_MOTION_MASK | Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK); } public unowned Gtk.TreeModel get_model() { return model; } public void set_model (Gtk.TreeModel? model) { if (this.model != null) { this.model.row_changed -= on_row_changed; this.model.row_deleted -= on_row_deleted; this.model.row_has_child_toggled -= on_row_has_child_toggled; this.model.row_inserted -= on_row_inserted; this.model.rows_reordered -= on_rows_reordered; } this.model = model; this.model.row_changed += on_row_changed; this.model.row_deleted += on_row_deleted; this.model.row_has_child_toggled += on_row_has_child_toggled; this.model.row_inserted += on_row_inserted; this.model.rows_reordered += on_rows_reordered; base_layout_needed = true; queue_draw(); } private void on_row_changed(Gtk.TreePath path, Gtk.TreeIter iter) { base_layout_needed = true; } private void on_row_deleted(Gtk.TreePath path) { base_layout_needed = true; } private void on_row_has_child_toggled(Gtk.TreePath path, Gtk.TreeIter iter) { base_layout_needed = true; } private void on_row_inserted(Gtk.TreePath path, Gtk.TreeIter iter) { base_layout_needed = true; } private void on_rows_reordered(Gtk.TreePath path, Gtk.TreeIter iter, void* new_order) { base_layout_needed = true; } /* Mouse button got pressed over widget */ /* public override bool button_press_event(Gdk.EventButton event) { stderr.printf("Mouse pressed on %d %s\n", cur_el, label_cache[cur_el]); return false; } */ /* Mouse button got released */ public override bool button_release_event(Gdk.EventButton event) { stderr.printf("Mouse released on %d %s\n", cur_el, label_cache[cur_el]); // Emit row_activated if applicable if (model != null) { Gtk.TreeIter iter; if (model.iter_nth_child(out iter, null, cur_el)) { Gtk.TreePath path = model.get_path(iter); row_activated(path); } } return false; } /* Mouse pointer moved over widget */ public override bool motion_notify_event(Gdk.EventMotion event) { int old_cur_el = cur_el; int x = (int)event.x; int y = (int)event.y; focus_locked = !focus_layout_needed && x < allocation.width/2 && y >= focus_starts[0] && y < focus_starts[focus_end - focus_first]; if (focus_locked) { for (int idx = focus_first; idx < focus_end; ++idx) if (y < focus_starts[idx-focus_first+1]) { cur_el = idx; break; } } else { cur_el = y * label_cache.length / allocation.height; if (old_cur_el != cur_el) focus_layout_needed = true; } //stderr.printf("MOTION %f %f CE %d\n", event.x, event.y, cur_el); if (old_cur_el != cur_el) { queue_draw(); old_cur_el = cur_el; } return false; } public override bool configure_event (Gdk.EventConfigure event) { base_layout_needed = true; queue_draw(); return false; } /* Widget is asked to draw itself */ public override bool expose_event (Gdk.EventExpose event) { draw(); window.draw_drawable( get_style().fg_gc[Gtk.StateType.NORMAL], backing_store, event.area.x, event.area.y, event.area.x, event.area.y, event.area.width, event.area.height); return false; } public override void style_set(Gtk.Style? previous_style) { base_layout_needed = true; } public override void direction_changed(Gtk.TextDirection previous_direction) { base_layout_needed = true; } protected int el_y(int idx) { // Distorted position int pos = fisheye(idx); //stderr.printf("%d %f %f\n", idx, undy, pos); return pos; } protected Gtk.CellRendererText make_cell_renderer() { var res = new Gtk.CellRendererText(); res.font_desc = get_style().font_desc; return res; } protected void base_layout() { background = new Gdk.Pixmap(window, allocation.width, allocation.height, -1); backing_store = new Gdk.Pixmap(window, allocation.width, allocation.height, -1); // Recreate the renderers renderers[renderers.length-1] = make_cell_renderer(); renderers[renderers.length-1].set_fixed_height_from_font(1); renderers[0] = make_cell_renderer(); renderers[0].size = Pango.SCALE; renderers[0].set_fixed_height_from_font(1); for (int i = 1; i < renderers.length-1; ++i) { renderers[i] = make_cell_renderer(); renderers[i].scale = (double)i / renderers.length; renderers[i].set_fixed_height_from_font(1); } if (model == null) { label_cache = new string[0]; } else { Gtk.TreeIter iter; if (!model.get_iter_first(out iter)) { label_cache = new string[0]; } else { int count = model.iter_n_children(null); label_cache = new string[count]; int i = 0; do { string val; model.get(iter, _label_column, out val, -1); label_cache[i] = val; ++i; } while (model.iter_next(ref iter)); } } // Draw background draw_background(background); base_layout_needed = false; focus_layout_needed = true; } protected void focus_layout() { if (label_cache.length == 0) { focus_first = 0; focus_end = 0; focus_starts[0] = 0; } else { if (_focus_size >= label_cache.length) { focus_first = 0; focus_end = label_cache.length; } else { focus_first = cur_el -_focus_size/2; if (focus_first < 0) focus_first = 0; focus_end = focus_first + _focus_size; if (focus_end > label_cache.length) { focus_end = label_cache.length; focus_first = focus_end - _focus_size; } } // Compute starting positions for all items in focus for (int idx = focus_first; idx <= focus_end; ++idx) focus_starts[idx - focus_first] = el_y(idx); } focus_layout_needed = false; } protected void draw_background(Gdk.Drawable drawable) { Gtk.Style style = get_style(); // Background drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 0, 0, allocation.width, allocation.height); // Focus movement area drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true, allocation.width/2, 0, allocation.width, allocation.height); Gdk.Rectangle expose_area = Gdk.Rectangle(); expose_area.x = expose_area.y = 0; expose_area.width = allocation.width; expose_area.height = allocation.height; stderr.printf("BG %d %d\n", label_cache.length, allocation.height); if (label_cache.length >= allocation.height) { stderr.printf("LABELS\n"); Gdk.Rectangle cell_area = Gdk.Rectangle(); cell_area.x = 0; cell_area.width = allocation.width; cell_area.height = 1; for (int y = 0; y < allocation.height; ++y) { int idx = y * label_cache.length / allocation.height; cell_area.y = y; renderers[0].text = label_cache[idx]; renderers[0].render((Gdk.Window*)drawable, this, cell_area, cell_area, expose_area, 0); } } } protected void draw() { if (base_layout_needed) base_layout(); if (focus_layout_needed) focus_layout(); var drawable = backing_store; Gtk.Style style = get_style(); Gdk.Rectangle expose_area = Gdk.Rectangle(); expose_area.x = expose_area.y = 0; expose_area.width = allocation.width; expose_area.height = allocation.height; // Background drawable.draw_drawable( get_style().fg_gc[Gtk.StateType.NORMAL], background, 0, 0, 0, 0, allocation.width, allocation.height); // Focus lock area drawable.draw_rectangle(style.bg_gc[Gtk.StateType.ACTIVE], true, 0, focus_starts[0], allocation.width/2, focus_starts[focus_end - focus_first]-focus_starts[0]); // Paint items around focus Gdk.Rectangle cell_area = Gdk.Rectangle(); cell_area.x = 0; cell_area.width = allocation.width; for (int idx = 0; idx < focus_end-focus_first; ++idx) { int y0 = focus_starts[idx]; int y1 = focus_starts[idx + 1]; cell_area.y = y0; cell_area.height = y1-y0; Gtk.CellRendererState rflags = 0; if (idx + focus_first == cur_el) { rflags |= Gtk.CellRendererState.SELECTED | Gtk.CellRendererState.FOCUSED; drawable.draw_rectangle(style.bg_gc[Gtk.StateType.SELECTED], true, cell_area.x, cell_area.y, cell_area.width, cell_area.height); } int size = (y1-y0); if (size <= 0) size = 1; if (size >= renderers.length) size = renderers.length - 1; renderers[size].text = label_cache[idx + focus_first]; renderers[size].render((Gdk.Window*)drawable, this, cell_area, cell_area, expose_area, rflags); //Gdk.draw_line(drawable, style.fg_gc[itemState], 0, y0, allocation.width, y0); } } /* * The following function is adapted from Prefuse's FisheyeDistortion.java. * * A relevant annotation from Prefuse: * * For more details on this form of transformation, see Manojit Sarkar and * Marc H. Brown, "Graphical Fisheye Views of Graphs", in Proceedings of * CHI'92, Human Factors in Computing Systems, p. 83-91, 1992. Available * online at * http://citeseer.ist.psu.edu/sarkar92graphical.html. */ /* * Distorts an item's coordinate. * @param x the undistorted coordinate * @param coordinate of the anchor or focus point * @param d disortion factor * @param min the beginning of the display * @param max the end of the display * @return the distorted coordinate */ private int fisheye(int idx) { // Autocompute distortion factor // 20 is the pixel size of the item at centre of focus double d = label_cache.length * 20 / allocation.height; if ( d <= 1 ) return idx * allocation.height / label_cache.length; double a = (double)cur_el * allocation.height / label_cache.length; double x = (double)idx * allocation.height / label_cache.length; double max = (double)allocation.height; if (idx < cur_el) { double m = a; if ( m == 0 ) m = max; double v = (double)(a - x) / m; v = (double)(d+1)/(d+(1/v)); return (int)Math.round(a - m*v); } else { double m = max-a; if ( m == 0 ) m = max; double v = (double)(x - a) / m; v = (double)(d+1)/(d+(1/v)); return (int)Math.round(a + m*v); } } } public class Fisheye : Gtk.Window { public Fisheye() { title = "Fisheye"; destroy += Gtk.main_quit; var list = new FisheyeList(); add(list); var store = new Gtk.ListStore(1, typeof(string)); Gtk.TreeIter iter; var infd = FileStream.open("/tmp/names", "r"); if (infd == null) { for (int i = 0; i < 300; ++i) { store.append(out iter); store.set(iter, 0, "Antani %d".printf(i), -1); } } else { char buf[255]; while (true) { string line = infd.gets(buf); if (line == null) break; store.append(out iter); store.set(iter, 0, line, -1); } } list.set_model(store); } } static int main (string[] args) { Gtk.init (ref args); var fe = new Fisheye(); fe.set_size_request(200, 300); fe.show_all(); Gtk.main(); return 0; }