Blame browser/jg_message.ml

Packit bd2e5d
(*************************************************************************)
Packit bd2e5d
(*                                                                       *)
Packit bd2e5d
(*                         OCaml LablTk library                          *)
Packit bd2e5d
(*                                                                       *)
Packit bd2e5d
(*            Jacques Garrigue, Kyoto University RIMS                    *)
Packit bd2e5d
(*                                                                       *)
Packit bd2e5d
(*   Copyright 1999 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.                                 *)
Packit bd2e5d
(*                                                                       *)
Packit bd2e5d
(*************************************************************************)
Packit bd2e5d
Packit bd2e5d
(* $Id$ *)
Packit bd2e5d
Packit bd2e5d
open StdLabels
Packit bd2e5d
open Tk
Packit bd2e5d
open Jg_tk
Packit bd2e5d
Packit bd2e5d
(*
Packit bd2e5d
class formatted ~parent ~width ~maxheight ~minheight =
Packit bd2e5d
  val parent = (parent : Widget.any Widget.widget)
Packit bd2e5d
  val width = width
Packit bd2e5d
  val maxheight = maxheight
Packit bd2e5d
  val minheight = minheight
Packit bd2e5d
  val tw = Text.create ~parent ~width ~wrap:`Word
Packit bd2e5d
  val fof = Format.get_formatter_output_functions ()
Packit bd2e5d
  method parent = parent
Packit bd2e5d
  method init =
Packit bd2e5d
    pack [tw] ~side:`Left ~fill:`Both ~expand:true;
Packit bd2e5d
    Format.print_flush ();
Packit bd2e5d
    Format.set_margin (width - 2);
Packit bd2e5d
    Format.set_formatter_output_functions ~out:(Jg_text.output tw)
Packit bd2e5d
      ~flush:(fun () -> ())
Packit bd2e5d
  method finish =
Packit bd2e5d
    Format.print_flush ();
Packit bd2e5d
    Format.set_formatter_output_functions ~out:(fst fof) ~flush:(snd fof);
Packit bd2e5d
    let `Linechar (l, _) = Text.index tw ~index:(tposend 1) in
Packit bd2e5d
    Text.configure tw ~height:(max minheight (min l maxheight));
Packit bd2e5d
    if l > 5 then
Packit bd2e5d
    pack [Jg_text.add_scrollbar tw] ~before:tw ~side:`Right ~fill:`Y
Packit bd2e5d
end
Packit bd2e5d
*)
Packit bd2e5d
Packit bd2e5d
let formatted ~title ?on ?(ppf = Format.std_formatter)
Packit bd2e5d
  ?(width=60) ?(maxheight=10) ?(minheight=0) () =
Packit bd2e5d
  let tl, frame =
Packit bd2e5d
    match on with
Packit bd2e5d
      Some frame ->
Packit bd2e5d
(*        let label = Label.create frame ~anchor:`W ~padx:10 ~text:title in
Packit bd2e5d
        pack [label] ~side:`Top ~fill:`X;
Packit bd2e5d
        let frame2 = Frame.create frame in
Packit bd2e5d
        pack [frame2] ~side:`Bottom ~fill:`Both ~expand:true; *)
Packit bd2e5d
        coe frame, frame
Packit bd2e5d
    | None ->
Packit bd2e5d
        let tl = Jg_toplevel.titled title in
Packit bd2e5d
        Jg_bind.escape_destroy tl;
Packit bd2e5d
        let frame = Frame.create tl in
Packit bd2e5d
        pack [frame] ~side:`Top ~fill:`Both ~expand:true;
Packit bd2e5d
        coe tl, frame
Packit bd2e5d
  in
Packit bd2e5d
  let tw = Text.create frame ~width ~wrap:`Word  in
Packit bd2e5d
  pack [tw] ~side:`Left ~fill:`Both ~expand:true;
Packit bd2e5d
  Format.pp_print_flush ppf ();
Packit bd2e5d
  Format.pp_set_margin ppf (width - 2);
Packit bd2e5d
  let fof,fff = Format.pp_get_formatter_output_functions ppf () in
Packit bd2e5d
  Format.pp_set_formatter_output_functions ppf
Packit bd2e5d
    (fun buf pos len -> Jg_text.output tw ~buf ~pos ~len)
Packit bd2e5d
    ignore;
Packit bd2e5d
  tl, tw,
Packit bd2e5d
  begin fun () ->
Packit bd2e5d
    Format.pp_print_flush ppf ();
Packit bd2e5d
    Format.pp_set_formatter_output_functions ppf fof fff;
Packit bd2e5d
    let `Linechar (l, _) = Text.index tw ~index:(tposend 1) in
Packit bd2e5d
    Text.configure tw ~height:(max minheight (min l maxheight));
Packit bd2e5d
    if l > 5 then
Packit bd2e5d
    pack [Jg_text.add_scrollbar tw] ~before:tw ~side:`Right ~fill:`Y
Packit bd2e5d
  end
Packit bd2e5d
Packit bd2e5d
let ask ~title ?master ?(no=true) ?(cancel=true) text =
Packit bd2e5d
  let tl = Jg_toplevel.titled title in
Packit bd2e5d
  begin match master with None -> ()
Packit bd2e5d
  | Some master -> Wm.transient_set tl ~master
Packit bd2e5d
  end;
Packit bd2e5d
  let mw = Message.create tl ~text ~padx:20 ~pady:10
Packit bd2e5d
           ~width:250 ~justify:`Left ~aspect:400 ~anchor:`W
Packit bd2e5d
  and fw = Frame.create tl
Packit bd2e5d
  and sync = Textvariable.create ~on:tl ()
Packit bd2e5d
  and r = ref (`Cancel : [`Yes|`No|`Cancel]) in
Packit bd2e5d
  let accept = Button.create fw
Packit bd2e5d
      ~text:(if no || cancel then "Yes" else "Dismiss")
Packit bd2e5d
      ~command:(fun () -> r := `Yes; destroy tl)
Packit bd2e5d
  and refuse = Button.create fw ~text:"No"
Packit bd2e5d
      ~command:(fun () -> r := `No; destroy tl)
Packit bd2e5d
  and cancelB = Button.create fw ~text:"Cancel"
Packit bd2e5d
      ~command:(fun () -> r := `Cancel; destroy tl)
Packit bd2e5d
  in
Packit bd2e5d
  bind tl ~events:[`Destroy] ~extend:true
Packit bd2e5d
    ~action:(fun _ -> Textvariable.set sync "1");
Packit bd2e5d
  pack [accept] ~side:`Left ~fill:`X ~expand:true;
Packit bd2e5d
  if no then pack [refuse] ~side:`Left ~fill:`X ~expand:true;
Packit bd2e5d
  if cancel then pack [cancelB] ~side:`Left ~fill:`X ~expand:true;
Packit bd2e5d
  pack [mw] ~side:`Top ~fill:`Both;
Packit bd2e5d
  pack [fw] ~side:`Bottom ~fill:`X ~expand:true;
Packit bd2e5d
  Grab.set tl;
Packit bd2e5d
  Tkwait.variable sync;
Packit bd2e5d
  !r
Packit bd2e5d
Packit bd2e5d
let info ~title ?master text =
Packit bd2e5d
  ignore (ask ~title ?master ~no:false ~cancel:false text)