/* * 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 { private string[] list; protected int cur_el; protected double distortion_factor; // Number of items shown before and after the focus element protected int focus_first; protected int focus_end; protected int focus_size; protected int[] focus_starts; protected bool focus_locked; protected double focus_centre; public FisheyeList() { list = new string[300]; for (int i = 0; i < 300; ++i) list[i] = "Antani %d".printf(i); cur_el = 0; focus_centre = 0; focus_size = 20; focus_starts = new int[focus_size + 1]; focus_locked = false; distortion_factor = 30; focus_layout(); add_events(Gdk.EventMask.POINTER_MOTION_MASK | Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK); } /* 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 = 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(); } //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; } /* Widget is asked to draw itself */ public override bool expose_event (Gdk.EventExpose event) { // Create a Cairo context var cr = Gdk.cairo_create (this.window); // Set clipping area in order to avoid unnecessary drawing cr.rectangle(event.area.x, event.area.y, event.area.width, event.area.height); cr.clip(); draw(cr); return false; } protected int el_y(int idx) { // Undistorted Y double undy = (double)idx * allocation.height / list.length; // Distorted position double pos = fisheye(undy, focus_centre, distortion_factor, 0, allocation.height); //stderr.printf("%d %f %f\n", idx, undy, pos); return (int)Math.round(pos); } protected void focus_layout() { // Anchor point focus_centre = (double)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; } } protected void draw(Cairo.Context context) { Gtk.Style style = get_style(); /* public enum StateType { NORMAL, ACTIVE, PRELIGHT, SELECTED, INSENSITIVE } style.bg[Gtk.StateType.NORMAL]; */ // Background Gdk.cairo_set_source_color(context, style.bg[Gtk.StateType.NORMAL]); context.paint(); // Focus lock area Gdk.cairo_set_source_color(context, style.bg[Gtk.StateType.ACTIVE]); context.new_path(); context.rectangle(0, focus_starts[0], allocation.width/2, focus_starts[focus_end - focus_first]); context.fill(); context.stroke(); // Focus movement area Gdk.cairo_set_source_color(context, style.bg[Gtk.StateType.INSENSITIVE]); context.new_path(); context.rectangle(allocation.width/2, 0, allocation.width, allocation.height); context.fill(); context.stroke(); // 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]; if (idx == cur_el) { Gdk.cairo_set_source_color(context, style.bg[Gtk.StateType.SELECTED]); context.new_path(); context.rectangle(0, y0, allocation.width, y1-y0); context.fill(); context.stroke(); Gdk.cairo_set_source_color(context, style.fg[Gtk.StateType.SELECTED]); } else { Gdk.cairo_set_source_color(context, style.fg[Gtk.StateType.NORMAL]); } context.new_path(); context.set_font_size(y1-y0); Cairo.FontExtents extents; context.font_extents(out extents); context.move_to(0, y1-extents.descent); context.text_path(list[idx]); context.fill(); context.stroke(); } } /* * 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 double fisheye(double x, double a, double d, double min, double max) { if ( d != 0 ) { bool left = x