Vala: hello.vala

This commit is contained in:
Kolan Sh 2012-09-26 19:45:46 +04:00
parent 1882501451
commit 56155f56c9
4 changed files with 109 additions and 0 deletions

5
vala/hello/hello.vala Normal file
View File

@ -0,0 +1,5 @@
int main ()
{
print ("Hello World\n");
return 0;
}

20
vala/hello/hello_gtk.vala Normal file
View File

@ -0,0 +1,20 @@
using Gtk;
int main (string[] args) {
Gtk.init (ref args);
var window = new Window ();
window.title = "Hello, World!";
window.border_width = 10;
window.window_position = WindowPosition.CENTER;
window.set_default_size (350, 70);
window.destroy.connect (Gtk.main_quit);
var label = new Label ("Hello, World!");
window.add (label);
window.show_all();
Gtk.main();
return 0;
}

16
vala/hello/hello_oop.vala Normal file
View File

@ -0,0 +1,16 @@
using GLib;
class Sample : Object
{
void run ()
{
stdout.printf ("Hello World\n");
}
static int main (string[] args)
{
var sample = new Sample ();
sample.run ();
return 0;
}
}

View File

@ -0,0 +1,68 @@
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;
}
}