dev/vala/hello/textviewer.vala

70 lines
1.8 KiB
Vala

using Gtk;
public class TextFileViewer : Window
{
private TextView text_view;
public TextFileViewer()
{
this.title = "Просмотр текстового файла";
//this.position = WindowPosition.CENTER;
this.set_default_size( 500, 400 );
var toolbar = new Toolbar();
var open_button = new ToolButton.from_stock( STOCK_OPEN );
toolbar.add( open_button );
open_button.clicked.connect( on_open_clicked );
this.text_view = new TextView();
this.text_view.editable = false;
this.text_view.cursor_visible = false;
var scroll = new ScrolledWindow( null, null );
scroll.set_policy( PolicyType.AUTOMATIC, PolicyType.AUTOMATIC );
scroll.add( this.text_view );
var vbox = new VBox( false, 0 );
vbox.pack_start( toolbar, false, true, 0 );
vbox.pack_start( scroll, true, true, 0 );
this.add( vbox );
}
private void on_open_clicked()
{
var file_chooser = new FileChooserDialog( "Открыть файл", this,
FileChooserAction.OPEN,
STOCK_CANCEL, ResponseType.CANCEL,
STOCK_OPEN, ResponseType.ACCEPT, null );
if( file_chooser.run() == ResponseType.ACCEPT )
{
open_file( file_chooser.get_filename() );
}
file_chooser.destroy();
}
private void open_file( string filename )
{
try
{
string text;
size_t size;
FileUtils.get_contents( filename, out text, out size );
this.text_view.buffer.set_text( text, (int) size );
}
catch( Error e )
{
stderr.printf( "Ошибка: %s\n", e.message );
}
}
public static int main( string[] args )
{
Gtk.init( ref args );
var window = new TextFileViewer();
window.destroy.connect( Gtk.main_quit );
window.show_all();
Gtk.main();
return 0;
}
}