Blame examples_camltk/fileinput.ml

Packit bd2e5d
(***********************************************************************)
Packit bd2e5d
(*                                                                     *)
Packit bd2e5d
(*                 MLTk, Tcl/Tk interface of OCaml                     *)
Packit bd2e5d
(*                                                                     *)
Packit bd2e5d
(*    Francois Rouaix, Francois Pessaux, Jun Furuse and Pierre Weis    *)
Packit bd2e5d
(*               projet Cristal, INRIA Rocquencourt                    *)
Packit bd2e5d
(*            Jacques Garrigue, Kyoto University RIMS                  *)
Packit bd2e5d
(*                                                                     *)
Packit bd2e5d
(*  Copyright 2002 Institut National de Recherche en Informatique et   *)
Packit bd2e5d
(*  en Automatique and Kyoto University.  All rights reserved.         *)
Packit bd2e5d
(*  This file is distributed under the terms of the GNU Library        *)
Packit bd2e5d
(*  General Public License, with the special exception on linking      *)
Packit bd2e5d
(*  described in file LICENSE found in the OCaml source tree.          *)
Packit bd2e5d
(*                                                                     *)
Packit bd2e5d
(***********************************************************************)
Packit bd2e5d
open Camltk ;;
Packit bd2e5d
Packit bd2e5d
let top_w = opentk () ;;
Packit bd2e5d
let buffer = String.create 256 ;;
Packit bd2e5d
let (fd_in, fd_out) = Unix.pipe () ;;
Packit bd2e5d
let text0_w = Text.create top_w [] ;;
Packit bd2e5d
let entry0_w = Entry.create top_w [] ;;
Packit bd2e5d
let button0_w = Button.create top_w [Text "Quit"; Command (fun _ -> exit 0)] ;;
Packit bd2e5d
Fileevent.add_fileinput fd_in (fun _ ->
Packit bd2e5d
                  let n = Unix.read fd_in buffer 0 (String.length buffer) in
Packit bd2e5d
                  let txt = String.sub buffer 0 n in
Packit bd2e5d
                  Text.insert text0_w (TextIndex (End, [])) txt []) ;;
Packit bd2e5d
let send _ =
Packit bd2e5d
 let txt = Entry.get entry0_w ^ "\n" in
Packit bd2e5d
 Entry.delete_range entry0_w (At 0) End ;
Packit bd2e5d
 ignore (Unix.write fd_out txt 0 (String.length txt));;
Packit bd2e5d
Packit bd2e5d
bind entry0_w [([], KeyPressDetail "Return")] (BindSet ([], send)) ;
Packit bd2e5d
pack [text0_w; entry0_w; button0_w][Side Side_Top; Fill Fill_X; Expand true] ;;
Packit bd2e5d
mainLoop () ;;