rollback to 3428, before the mysterious removal of libs/* at 3431/3432
[ardour.git] / libs / gtkmm2ext / textviewer.cc
1 /*
2     Copyright (C) 1999 Paul Barton-Davis 
3     This program is free software; you can redistribute it and/or modify
4     it under the terms of the GNU General Public License as published by
5     the Free Software Foundation; either version 2 of the License, or
6     (at your option) any later version.
7
8     This program is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11     GNU General Public License for more details.
12
13     You should have received a copy of the GNU General Public License
14     along with this program; if not, write to the Free Software
15     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
17     $Id$
18 */
19
20 #include <string>
21 #include <fstream>
22
23 #include <gtkmm2ext/textviewer.h>
24
25 #include "i18n.h"
26
27 using namespace std;
28 using namespace Gtkmm2ext;
29 using namespace sigc;
30
31 TextViewer::TextViewer (size_t xsize, size_t ysize) :
32         Gtk::Window (Gtk::WINDOW_TOPLEVEL),
33         Transmitter (Transmitter::Info), /* channel arg is irrelevant */
34         dismiss (_("Close"))
35 {
36         set_size_request (xsize, ysize);
37
38         set_title ("Text Viewer");
39         set_name ("TextViewer");
40         set_resizable (true);
41         set_border_width (0);
42
43         vbox1.set_homogeneous (false);
44         vbox1.set_spacing (0);
45         add (vbox1);
46         vbox1.show ();
47
48         vbox2.set_homogeneous (false);
49         vbox2.set_spacing (10);
50         //vbox2.set_border_width (10);
51
52         vbox1.pack_start (vbox2, true, true, 0);
53         vbox2.show ();
54
55         vbox2.pack_start (scrollwin, TRUE, TRUE, 0);
56         scrollwin.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
57         scrollwin.show ();
58
59         etext.set_editable (false);
60         etext.set_wrap_mode (Gtk::WRAP_WORD);
61         scrollwin.add (etext);
62         etext.show ();
63
64         vbox1.pack_start (dismiss, false, false, 0);
65         dismiss.show ();
66
67         dismiss.signal_clicked().connect(mem_fun (*this, &TextViewer::signal_released_handler));
68 }
69
70 void
71 TextViewer::signal_released_handler()
72 {
73         hide();
74 }
75
76 void
77 TextViewer::insert_file (const string &path)
78
79 {
80         char buf[1024];
81         ifstream f (path.c_str());
82
83         if (!f) {
84                 return;
85         }
86
87         Glib::RefPtr<Gtk::TextBuffer> tb (etext.get_buffer());
88
89         tb->begin_user_action();
90         while (f) {
91                 f.read (buf, sizeof (buf));
92
93                 if (f.gcount()) {
94                         buf[f.gcount()] = '\0';
95                         string foo (buf);
96                         tb->insert (tb->end(), foo);
97                 }
98         }
99         tb->end_user_action();
100 }
101
102 void
103 TextViewer::scroll_to_bottom ()
104
105 {
106         Gtk::Adjustment *adj;
107
108         adj = scrollwin.get_vadjustment();
109         adj->set_value (MAX(0,(adj->get_upper() - adj->get_page_size())));
110 }
111         
112 void
113 TextViewer::deliver ()
114
115 {
116         char buf[1024];
117         Glib::RefPtr<Gtk::TextBuffer> tb (etext.get_buffer());
118
119         while (!eof()) {
120                 read (buf, sizeof (buf));
121                 buf[gcount()] = '\0';
122                 string foo (buf);
123                 tb->insert (tb->end(), foo);
124         }
125         scroll_to_bottom ();
126         clear ();
127 }