]> git.mdlowis.com Git - archive/tide-ocaml.git/commitdiff
Added draw module and functions for common drawing operations
authorMichael D. Lowis <mike.lowis@gentex.com>
Tue, 24 Oct 2017 20:04:35 +0000 (16:04 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Tue, 24 Oct 2017 20:04:35 +0000 (16:04 -0400)
Makefile
edit.ml
lib/draw.ml [new file with mode: 0644]
lib/draw.mli [new file with mode: 0644]
lib/x11.ml

index ebe4d596c60e5f9ea319c94bfd6f36763829877b..68f86daec9839e2aca43dacd0c3cfaa119f3073e 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -29,6 +29,7 @@ LIBOBJS = \
     lib/misc.$(OBJEXT) \
     lib/x11.$(OBJEXT) \
     lib/cfg.$(OBJEXT) \
+    lib/draw.$(OBJEXT) \
     lib/rope.$(OBJEXT) \
     lib/buf.$(OBJEXT) \
     lib/scrollmap.$(OBJEXT) \
diff --git a/edit.ml b/edit.ml
index 3403e0cb58d95a68b0aecfffae117569dfd26238..541e11106d2d15d580458cd6937dbe5bfec53847 100644 (file)
--- a/edit.ml
+++ b/edit.ml
@@ -1,10 +1,6 @@
 open X11
 
-let font_times = font_load "Times New Roman:size=12"
-let font_monaco = font_load "Monaco:size=10"
-let font_verdana = font_load "Verdana:size=11"
-
-let font = font_verdana
+let font = Draw.font
 let tabglyph = 0x30
 let tabwidth = 4
 
@@ -18,7 +14,6 @@ type drawpos = { x: int; y: int }
 let draw_bkg color width height pos =
   draw_rect { x = pos.x; y = pos.y; w = width; h = height; c = color }
 
-(* curried helpers *)
 let draw_dark_bkg = draw_bkg Cfg.Color.palette.(0)
 let draw_light_bkg = draw_bkg Cfg.Color.palette.(1)
 let draw_gray_bkg = draw_bkg Cfg.Color.palette.(3)
@@ -81,17 +76,13 @@ let draw_edit pos width height =
 
 (* Event functions
  ******************************************************************************)
-let onfocus focused =
-  () (*print_endline "onfocus"*)
+let onfocus focused = ()
 
-let onkeypress mods rune =
-  ()
+let onkeypress mods rune = ()
 
-let onmousebtn mods btn x y pressed =
-  ()
+let onmousebtn mods btn x y pressed = ()
 
-let onmousemove mods x y =
-  () (*print_endline "onmousemove"*)
+let onmousemove mods x y = ()
 
 let onupdate width height =
   let (pos : drawpos) = { x = 0; y = 0 } in
@@ -100,8 +91,7 @@ let onupdate width height =
   let pos = draw_scroll pos height in
   let _   = draw_edit pos width height in ()
 
-let onshutdown () =
-  print_endline "onshutdown"
+let onshutdown () = ()
 
 let onevent = function
   | Focus state      -> onfocus state
diff --git a/lib/draw.ml b/lib/draw.ml
new file mode 100644 (file)
index 0000000..26484ef
--- /dev/null
@@ -0,0 +1,87 @@
+module Cursor = struct
+  type t = {
+    height : int;
+    width : int;
+    startx : int;
+    starty : int;
+    mutable x: int;
+    mutable y: int
+  }
+
+  let make dim x y =
+    let height, width = dim in
+    { height = height; width = width;
+      startx = x; starty = y; x = x; y = y }
+end
+
+open Cursor
+
+let font = X11.font_load "Verdana:size=11"
+
+let rectangle color width height csr =
+  X11.draw_rect (X11.make_rect csr.x csr.y width height color)
+
+(* curried helpers *)
+let dark_bkg = rectangle Cfg.Color.palette.(0)
+let light_bkg = rectangle Cfg.Color.palette.(1)
+let rule_bkg = rectangle Cfg.Color.palette.(3)
+
+let string text csr =
+  X11.draw_string font Cfg.Color.palette.(5) text (csr.x + 2, csr.y + 2);
+  csr.y <- csr.y + 4 + font.height
+
+let hrule width csr =
+  rule_bkg width 1 csr;
+  csr.y <- csr.y + 1
+
+let vrule height csr =
+  rule_bkg 1 (height - csr.y) csr;
+  csr.x <- csr.x + 1
+
+(*
+type drawpos = { x: int; y: int }
+
+let draw_status pos width text =
+  let height = (4 + font.height) in
+  draw_dark_bkg width height pos;
+  let pos = draw_text text pos in
+  draw_hrule width pos
+
+let draw_tags pos width maxlns text =
+  let bkgheight = ((font.height * maxlns) + 4) in
+  draw_light_bkg width bkgheight pos;
+  let pos = draw_text text pos in
+  draw_hrule width pos
+
+let draw_scroll pos height =
+  let rulepos = { pos with x = 14 } in
+  draw_gray_bkg rulepos.x height pos;
+  draw_dark_bkg rulepos.x (height/2) pos;
+  draw_vrule height rulepos
+
+let draw_buffer pos width height =
+  let x = ref pos.x and y = ref pos.y in
+  let newline () = x := pos.x; y := !y + font.height in
+  let draw_char c =
+    let glyph = (X11.get_glyph font c) in
+    (match c with
+    | 0x0A -> newline ()
+    | 0x0D -> ()
+    | 0x09 ->
+        let tabsz = ((X11.get_glyph font tabglyph).xoff * tabwidth) in
+        x := pos.x + (((!x - pos.x) + tabsz) / tabsz * tabsz)
+    | _    -> begin
+        if (!x + glyph.xoff) > width then (newline ());
+        let off = X11.draw_glyph Cfg.Color.palette.(5) glyph (!x, !y) in
+        x := !x + off
+    end);
+    ((!y + font.height) < height)
+  in
+  Buf.iter_from draw_char !edit_buf (Buf.start !edit_buf);
+  pos
+
+let draw_edit pos width height =
+  draw_dark_bkg (width - pos.x) (height - pos.y) pos;
+  let pos = { x = pos.x + 2; y = pos.y + 2 } in
+  draw_buffer pos width height
+*)
diff --git a/lib/draw.mli b/lib/draw.mli
new file mode 100644 (file)
index 0000000..028d186
--- /dev/null
@@ -0,0 +1,15 @@
+module Cursor : sig
+  type t
+  val make : (int * int) -> int -> int -> t
+end
+
+val font : X11.font
+
+val rectangle : int -> int -> int -> Cursor.t -> unit
+val dark_bkg : int -> int -> Cursor.t -> unit
+val light_bkg : int -> int -> Cursor.t -> unit
+val rule_bkg : int -> int -> Cursor.t -> unit
+
+val string : string -> Cursor.t -> unit
+val hrule : int -> Cursor.t -> unit
+val vrule : int -> Cursor.t -> unit
index 35f4b229455e89ee0c7a1cac241091a8d66b17a8..f7c42814f28829b93ddb36fb8eacce4d17a4e44f 100644 (file)
@@ -141,6 +141,9 @@ let rec draw_stringi font color str coord index =
 let draw_string font color str coord =
   draw_stringi font color str coord 0
 
+let make_rect x y w h c =
+  { x = x; y = y; w = w; h = h; c = c }
+
 (* Automatically connect and disconnect to the display server *)
 let () =
   connect ();