more Vala examples

This commit is contained in:
Kolan Sh 2012-09-30 19:46:05 +04:00
parent 56155f56c9
commit 1326067d6a
6 changed files with 106 additions and 12 deletions

View File

@ -0,0 +1,23 @@
interface IfaceA : Object {
public abstract void method_a ();
}
interface IfaceB : Object, IfaceA {
public abstract void method_b ();
}
class Demo : Object, IfaceA, IfaceB {
public void method_a () { stdout.printf ("a called\n"); }
public void method_b () { stdout.printf ("b called\n"); }
public void method_c () { stdout.printf ("c called\n"); }
static int main ()
{
stdout.printf ("Hello world!\n");
var demo = new Demo ();
demo.method_a ();
demo.method_b ();
demo.method_c ();
return 0;
}
}

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

@ -0,0 +1,20 @@
/* Vala Lock Example */
using GLib;
public class Sample : GLib.Object {
private int critical_data;
public void inc () {
lock (critical_data) {
critical_data++;
}
}
static void main (string[] args) {
var sample = new Sample ();
sample.inc ();
}
}

10
vala/hello/notify.vala Normal file
View File

@ -0,0 +1,10 @@
public class Demo : Object {
public string title { get; set; }
}
void main () {
var demo = new Demo ();
demo.notify.connect ((s, p) => stdout.printf ("Property %s changed\n", p.name));
demo.title = "hello";
demo.title = "world";
}

View File

@ -0,0 +1,17 @@
int main ()
{
int[] a = {1, 2, 3};
double b = 5.33;
var s ="Hello, %s\n";
stdout.printf (s, b.to_string());
a[8] = 7;
foreach (int i in a)
{
stdout.printf ("%d\n", i);
}
return 0;
}

23
vala/hello/signal.vala Normal file
View File

@ -0,0 +1,23 @@
public class MyButton : Object {
public signal void clicked ();
public void test () {
clicked (); // emit signal
}
}
void handler_c (MyButton source) {
stdout.printf ("handler C\n");
}
void main () {
var b = new MyButton ();
b.clicked.connect ((s) => stdout.printf ("handler A\n"));
b.clicked.connect ((s) => {
stdout.printf ("handler B\n");
});
b.clicked.connect (handler_c);
b.test ();
b.clicked.disconnect (handler_c);
}

View File

@ -4,7 +4,7 @@ public class TextFileViewer : Window
{
private TextView text_view;
public TextFileViewer()
public TextFileViewer()
{
this.title = "Просмотр текстового файла";
//this.position = WindowPosition.CENTER;
@ -29,34 +29,35 @@ public class TextFileViewer : Window
this.add( vbox );
}
private void on_open_clicked()
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 )
if( file_chooser.run() == ResponseType.ACCEPT )
{
open_file( file_chooser.get_filename() );
}
file_chooser.destroy();
}
private void open_file( string filename )
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 )
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 )
public static int main( string[] args )
{
Gtk.init( ref args );
var window = new TextFileViewer();