/* * 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 backing_store; private string[] list; protected int cur_el; protected int _distortion_factor; public int distortion_factor { get { return _distortion_factor; } set { _distortion_factor = value; focus_layout_needed = true; } } // Pango layouts cached for speed // TODO: If you create and keep a PangoLayout using this context, you // must deal with changes to the context by calling // pango_layout_context_changed() on the layout in response to the // "style-set" and "direction-changed" signals for the widget. protected Pango.Layout[] pango_cache; // Layout information protected int focus_first; protected int focus_end; protected int[] focus_starts; protected bool focus_locked; protected int focus_centre; 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; } } public FisheyeList() { model = null; backing_store = null; list = new string[300]; for (int i = 0; i < 300; ++i) list[i] = "Lorem Ipsum %d".printf(i); pango_cache = new Pango.Layout[30]; for (int i = 0; i < 30; ++i) pango_cache[i] = null; // Defaults for properties focus_size = 20; distortion_factor = 30; cur_el = 0; focus_centre = 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) { this.model = model; } /* 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, list[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, list[cur_el]); // ... 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 * list.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) { backing_store = new Gdk.Pixmap(window, allocation.width, allocation.height, -1); focus_layout_needed = true; queue_draw(); return false; } /* Widget is asked to draw itself */ public override bool expose_event (Gdk.EventExpose event) { if (backing_store == null) return false; draw(backing_store); 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; } protected int el_y(int idx) { // Undistorted Y int undy = idx * allocation.height / list.length; // Distorted position int pos = fisheye(undy, focus_centre, distortion_factor, 0, allocation.height); //stderr.printf("%d %f %f\n", idx, undy, pos); return pos; } protected void focus_layout() { // Anchor point focus_centre = cur_el*allocation.height/list.length; focus_first = cur_el > focus_size/2 ? cur_el-focus_size/2 : 0; focus_end = focus_first + focus_size; if (focus_end >= list.length) focus_end = list.length; // Compute starting positions for all items in focus for (int idx = focus_first; idx < focus_end; ++idx) { int posprev = idx == 0 ? 0 : el_y(idx-1); int pos = el_y(idx); int posnext = idx == list.length-1 ? 1 : el_y(idx+1); int y0 = (pos+posprev)/2; int y1 = (pos+posnext)/2; focus_starts[idx - focus_first] = y0; focus_starts[idx - focus_first + 1] = y1; } focus_layout_needed = false; } protected void draw(Gdk.Drawable drawable) { if (focus_layout_needed) focus_layout(); Gtk.Style style = get_style(); // Background drawable.draw_rectangle(style.bg_gc[Gtk.StateType.NORMAL], true, 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 movement area drawable.draw_rectangle(style.bg_gc[Gtk.StateType.INSENSITIVE], true, allocation.width/2, 0, allocation.width, allocation.height); // Create a Cairo context //var context = Gdk.cairo_create (drawable); // Paint items around focus //context.select_font_face(style.font_desc.get_family(), Cairo.FontSlant.NORMAL, Cairo.FontWeight.NORMAL); for (int idx = focus_first; idx < focus_end; ++idx) { int y0 = focus_starts[idx - focus_first]; int y1 = focus_starts[idx - focus_first + 1]; Gtk.StateType itemState = Gtk.StateType.NORMAL; if (idx == cur_el) { itemState = Gtk.StateType.SELECTED; drawable.draw_rectangle(style.bg_gc[itemState], true, 0, y0, allocation.width, y1-y0); } // TODO: cache pango contexts instead of fontdescs int size = (y1-y0)*80/100; if (size <= 0) size = 1; if (size >= pango_cache.length) size = pango_cache.length - 1; if (pango_cache[size] == null) { var fd = style.font_desc.copy(); fd.set_absolute_size(size*Pango.SCALE); var pc = create_pango_context(); pc.set_font_description(fd); pango_cache[size] = new Pango.Layout(pc); } pango_cache[size].set_text(list[idx], -1); var layout = pango_cache[size]; //stderr.printf("AZAZA %p\n", layout.get_attributes()); //var attrlist = layout.get_attributes().copy(); //stderr.printf("AL %p\n", attrlist); //var attrlist = new Pango.AttrList(); //stderr.printf("SIZE %d\n", y1-y0); //attrlist.insert(new Pango.AttrSize(y1-y0)); //var attrlist = layout.get_attributes(); //attrlist.change(new Pango.AttrSize(y1-y0)); //layout.set_attributes(attrlist); //layout.set_height(y1-y0); //int w, h; //layout.get_pixel_size(out w, out h); Gdk.draw_layout(drawable, style.fg_gc[itemState], 0, y0, layout); } } /* * 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 x, int a, int d, int min, int max) { if ( d != 0 ) { bool left = x