File: cgets.ml

package info (click to toggle)
lablgtk3 3.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,796 kB
  • sloc: ml: 40,890; ansic: 22,312; makefile: 133; sh: 17
file content (62 lines) | stat: -rw-r--r-- 1,885 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
(**************************************************************************)
(*    Lablgtk - Examples                                                  *)
(*                                                                        *)
(*    This code is in the public domain.                                  *)
(*    You may freely copy parts of it in your application.                *)
(*                                                                        *)
(**************************************************************************)

open Printf

let pr_targets targets =
  printf "%d targets\n" (List.length targets);
  let pr atom = printf "%s\n" (Gdk.Atom.name atom) in
  List.iter pr targets;
  flush stdout

let get_contents targets =
  let rec loop ls =
    match ls with
    | [] -> []
    | atom::xs ->
        try
	  let content = (atom, GMain.clipboard#get_contents ~target:atom) in
	  content :: loop xs
        with _ -> loop xs
  in
  loop targets

let pr_contents cnt_list =
  let pr (atom, sdata) = 
    printf "-----\n";
    printf "  target [%s]\n" sdata#target;
    printf "  typ [%s]\n" sdata#typ;
    printf "  format [%d]\n" sdata#format;
    begin try
      printf "  data length (%d) [%s]\n" (String.length sdata#data) sdata#data;
    with _ -> printf "  data (NULL)\n"
    end;
    flush stdout;
  in
  List.iter pr cnt_list

let get_targets () =
  let targets = GMain.clipboard#targets in
  pr_targets targets;
  let contents = get_contents targets in
  pr_contents contents;
  ()

let main () =
  GMain.init ();
  (* Create the toplevel window *)
  let window = GWindow.window ~title:"Clipboard" ~border_width:10 () in
  window#connect#destroy ~callback:GMain.quit;

  let btn = GButton.button ~label:"Get Targets" ~packing:window#add () in
  btn#connect#clicked ~callback:get_targets;

  window#show ();
  GMain.main ()

let _ = Printexc.print main ()