patch #7318
[mldonkey.git] / src / gtk / newgui / gui_console.ml
blob8915cdefa962f8b55ec8b9f3560a04b79518373c
1 (* Copyright 2001, 2002 b8_bavard, b8_fee_carabine, INRIA *)
2 (*
3 This file is part of mldonkey.
5 mldonkey is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 mldonkey is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with mldonkey; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 (** GUI for console. *)
22 open CommonTypes
23 open GuiProto
25 module M = Gui_messages
26 module P = Gpattern
27 module O = Gui_options
29 let (!!) = Options.(!!)
31 let max_console_lines = ref 500
32 let line_threshold = 50
34 class box () =
35 object (self)
36 inherit Gui_console_base.box () as box
38 val mutable lines = Fifo.create ()
39 val mutable removed_size = 0
40 val mutable current_pos = 0
41 val mutable history = Fifo.create ()
43 method set_list_bg bg font =
44 let style = text#misc#style#copy in
45 style#set_base [ (`NORMAL, bg)];
46 style#set_font font;
47 text#misc#set_style style
49 method insert_line t =
50 if Fifo.length lines > !max_console_lines + line_threshold then begin
51 let (line_pos, len) = Fifo.take lines in
52 let line_pos = line_pos - removed_size in
53 let rec iter n len =
54 if n = 0 then len else
55 let (_, addlen) = Fifo.take lines in
56 iter (n-1) (len+addlen)
58 let len = iter line_threshold len in
59 text#delete_text ~start: line_pos ~stop: (line_pos+len);
60 removed_size <- removed_size + len;
61 end;
62 let len = String.length t in
63 Fifo.put lines (current_pos, len);
64 current_pos <- current_pos + len;
65 ignore (text#insert_text t ~pos: text#length (* ~pos:0 *) );
67 method insert t =
68 text#freeze ();
69 let rec iter list =
70 match list with
71 [] | [""] -> ()
72 | s:: tail -> self#insert_line (s ^ "\n"); iter tail
73 in
74 iter (String2.split t '\n');
75 text#thaw ();
76 text#set_position text#length
79 method on_entry_return () =
80 match we_command#entry#text with
81 "" -> ()
82 | s ->
83 Gui_com.send (GuiProto.Command s);
84 if not (Fifo.mem history s) then begin
85 Fifo.put history s;
86 (* we_command#set_item_string (GList.list_item ~label:s ()) s;*)
88 let list = we_command#list in
89 let strings = Fifo.to_list history in
91 we_command#disable_activate ();
92 list#clear_items ~start:0 ~stop:(-1);
93 List.iter (fun s ->
94 let li = GList.list_item ~label: s () in
95 li#misc#show ();
96 list#add li;
97 ) strings;
100 (*we_command#set_popdown_strings strings*)
101 end;
102 we_command#entry#set_text "";
104 initializer
105 (try
106 let font = Gdk.Font.load_fontset "fixed" in
107 let style = text#misc#style#copy in
108 style#set_font font;
109 text#misc#set_style style;
110 with _ -> ());
111 we_command#set_use_arrows `ALWAYS;
112 Okey.add we_command#entry
113 ~mods: []
114 GdkKeysyms._Return
115 self#on_entry_return;
117 ignore (wb_clear_console#connect#clicked
118 (fun () ->
119 text#delete_text 0 text#length;
120 Fifo.clear lines;
121 removed_size <- 0;
122 current_pos <- 0;