| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Util |
| 12 |
open Preferences |
| 13 |
|
| 14 |
type color = GDraw.color |
| 15 |
|
| 16 |
type model_event = [ `INSERT | `REMOVE | `SET of int * color ] |
| 17 |
|
| 18 |
class type model = |
| 19 |
object |
| 20 |
method changed : callback:(model_event -> unit) -> unit |
| 21 |
method length : int |
| 22 |
method fold : 'a. ('a -> color -> 'a) -> 'a -> 'a |
| 23 |
end |
| 24 |
|
| 25 |
let i2f = float_of_int |
| 26 |
let f2i = int_of_float |
| 27 |
|
| 28 |
let color_eq (c1 : GDraw.color) (c2 : GDraw.color) = match c1, c2 with |
| 29 |
| `BLACK, `BLACK -> true |
| 30 |
| `COLOR c1, `COLOR c2 -> c1 == c2 |
| 31 |
| `NAME s1, `NAME s2 -> String.equal s1 s2 |
| 32 |
| `RGB (r1, g1, b1), `RGB (r2, g2, b2) -> r1 = r2 && g1 = g2 && b1 = b2 |
| 33 |
| `WHITE, `WHITE -> true |
| 34 |
| _ -> false |
| 35 |
|
| 36 |
let set_cairo_color ctx c = |
| 37 |
let open Gdk.Color in |
| 38 |
let c = GDraw.color c in |
| 39 |
let cast i = i2f i /. 65536. in |
| 40 |
Cairo.set_source_rgb ctx (cast @@ red c) (cast @@ green c) (cast @@ blue c) |
| 41 |
|
| 42 |
class type segment_signals = |
| 43 |
object |
| 44 |
inherit GObj.misc_signals |
| 45 |
inherit GUtil.add_ml_signals |
| 46 |
method clicked : callback:(int -> unit) -> GtkSignal.id |
| 47 |
end |
| 48 |
|
| 49 |
class segment_signals_impl obj (clicked : 'a GUtil.signal) : segment_signals = |
| 50 |
object |
| 51 |
val after = false |
| 52 |
inherit GObj.misc_signals obj |
| 53 |
inherit GUtil.add_ml_signals obj [clicked#disconnect] |
| 54 |
method clicked = clicked#connect ~after |
| 55 |
end |
| 56 |
|
| 57 |
class segment () = |
| 58 |
let box = GBin.frame () in |
| 59 |
let draw = GMisc.drawing_area ~packing:box#add () in |
| 60 |
|
| 61 |
object (self) |
| 62 |
|
| 63 |
inherit GObj.widget box#as_widget |
| 64 |
|
| 65 |
val mutable width = 1 |
| 66 |
val mutable height = 20 |
| 67 |
val mutable model : model option = None |
| 68 |
val mutable default : color = `WHITE |
| 69 |
val clicked = new GUtil.signal () |
| 70 |
|
| 71 |
initializer |
| 72 |
box#misc#set_size_request ~height (); |
| 73 |
let cb rect = |
| 74 |
let w = rect.Gtk.width in |
| 75 |
let h = rect.Gtk.height in |
| 76 |
width <- w; |
| 77 |
height <- h |
| 78 |
in |
| 79 |
let _ = box#misc#connect#size_allocate ~callback:cb in |
| 80 |
let () = draw#event#add [`BUTTON_PRESS] in |
| 81 |
let clicked_cb ev = match model with |
| 82 |
| None -> true |
| 83 |
| Some md -> |
| 84 |
let x = GdkEvent.Button.x ev in |
| 85 |
let len = md#length in |
| 86 |
let idx = f2i ((x *. i2f len) /. i2f width) in |
| 87 |
let () = clicked#call idx in |
| 88 |
true |
| 89 |
in |
| 90 |
let _ = draw#event#connect#button_press ~callback:clicked_cb in |
| 91 |
let cb show = if show then self#misc#show () else self#misc#hide () in |
| 92 |
stick show_progress_bar self cb; |
| 93 |
let cb ctx = self#refresh ctx; false in |
| 94 |
let _ = draw#misc#connect#draw ~callback:cb in |
| 95 |
() |
| 96 |
|
| 97 |
method set_model md = |
| 98 |
model <- Some md; |
| 99 |
let changed_cb _ = self#misc#queue_draw () in |
| 100 |
md#changed ~callback:changed_cb |
| 101 |
|
| 102 |
method private fill_range ctx color i j = match model with |
| 103 |
| None -> () |
| 104 |
| Some md -> |
| 105 |
let i = i2f i in |
| 106 |
let j = i2f j in |
| 107 |
let width = i2f width in |
| 108 |
let len = i2f md#length in |
| 109 |
let x = f2i ((i *. width) /. len) in |
| 110 |
let x' = f2i ((j *. width) /. len) in |
| 111 |
let w = x' - x in |
| 112 |
set_cairo_color ctx color; |
| 113 |
Cairo.rectangle ctx (i2f x) 0. ~w:(i2f w) ~h:(i2f height); |
| 114 |
Cairo.fill ctx |
| 115 |
|
| 116 |
method set_default_color color = default <- color |
| 117 |
method default_color = default |
| 118 |
|
| 119 |
method private refresh ctx = match model with |
| 120 |
| None -> () |
| 121 |
| Some md -> |
| 122 |
set_cairo_color ctx default; |
| 123 |
Cairo.rectangle ctx 0. 0. ~w:(i2f width) ~h:(i2f height); |
| 124 |
Cairo.fill ctx; |
| 125 |
let make (k, cur, accu) v = match cur with |
| 126 |
| None -> pred k, Some (k, k, v), accu |
| 127 |
| Some (i, j, w) -> |
| 128 |
if k = j - 1 && color_eq v w then pred k, Some (k, i, w), accu |
| 129 |
else pred k, Some (k, k, v), (i, j, w) :: accu |
| 130 |
in |
| 131 |
let _, p, segments = md#fold make (md#length - 1, None, []) in |
| 132 |
let segments = match p with |
| 133 |
| None -> segments |
| 134 |
| Some p -> p :: segments |
| 135 |
in |
| 136 |
List.iter (fun (i, j, v) -> self#fill_range ctx v i (j + 1)) segments |
| 137 |
|
| 138 |
method connect = |
| 139 |
new segment_signals_impl box#as_widget clicked |
| 140 |
|
| 141 |
end |