object-oriented capability – signal systems, properties, etc. Floating referenced object Base class for all GTK+ objects Abstract base class for all widgets – style properties and standard functions Abstract class for all container widgets Abstract class for widgets containing only 1 child Use GTK+ documentation to find the object hierarchy, properties, and signals of widgets:
GLib.Object { /* subclassing 'GLib.Object' */ public double mass; /* a public field */ public double name { get; set; } /* a public property */ private bool terminated = false; /* a private field */ public void terminate() { /* a public method */ terminated = true; } }
app = new Application(); var hpaned = new HPaned(); var button1 = new Button.with_label("Resize"); var button2 = new Button.with_label("Me!"); button1.clicked.connect(app.destroy_window); button2.clicked.connect(app.destroy_window); hpaned.add1(button1); hpaned.add2(button2); app.add_to_window(hpaned); app.show(); Gtk.main(); return 0; } 5.panes.vala
app = new Application(); var expander = new Expander.with_mnemonic("Click _Me For More!"); var label = new Label("Hide me or show me,\nthat is your choice."); expander.add(label); expander.set_expanded(true); app.add_to_window(expander); app.show(); Gtk.main(); return 0; } 7.expanders.vala
app = new Application(); var notebook = new Notebook(); var label1 = new Label("Page One"); var label2 = new Label("Page Two"); var child1 = new Button.with_label("Go to page 2 to find the answer."); var child2 = new Button.with_label("Go to page 1 to find the answer."); child1.clicked.connect(() => { switch_page(notebook); }); child2.clicked.connect(() => { switch_page(notebook); }); notebook.append_page(child1, label1); notebook.append_page(child2, label2); notebook.set_tab_pos(PositionType.BOTTOM); app.add_to_window(notebook); app.show(); Gtk.main(); return 0; } 8.notebooks.vala public static void switch_page(Notebook nb) { if ( nb.get_current_page() == 0 ) { nb.set_current_page(1); } else { nb.set_current_page(0); } }
{ other.set_sensitive(!own.get_active()); } public static int main(string[] args) { Gtk.init(ref args); var app = new Application(); var vbox = new VBox(true, 5); var toggle1 = new ToggleButton.with_mnemonic("_Deactive the other one!"); var toggle2 = new ToggleButton.with_mnemonic("_No! Deactivate that one!"); toggle1.toggled.connect(() => { Application.button_toggled(toggle1, toggle2); }); toggle2.toggled.connect(() => { Application.button_toggled(toggle2, toggle1); }); vbox.pack_start(toggle1); vbox.pack_start(toggle2); app.add_to_window(vbox); app.show(); Gtk.main(); return 0; } 10.togglebuttons.vala
} public static int main(string[] args) { // . . . . . var check1 = new CheckButton.with_label("I am the main option."); var check2 = new CheckButton.with_label("I rely on the other guy."); /* Only enable the second check button when the first is enabled. */ check2.set_sensitive(false); check1.toggled.connect(() => { check2.set_sensitive(check1.get_active()); }); var close = new Button.from_stock(STOCK_CLOSE); close.clicked.connect(() => { app.destroy(); }); var vbox = new VBox(false, 5); vbox.pack_start(check1, false, true, 0); vbox.pack_start(check2, false, true, 0); vbox.pack_start(close, false, true, 0); app.add_to_window(vbox); app.show(); Gtk.main(); return 0; } 11.checkbuttons.vala
app = new Application(); var radio1 = new RadioButton.with_label(null, "I want to be clicked!"); var radio2 = new RadioButton.with_label_from_widget(radio1, "Click me instead!"); var radio3 = new RadioButton.with_label_from_widget(radio1, "No! Click me!"); var radio4 = new RadioButton.with_label_from_widget(radio3, "No! Click me instead!"); var vbox = new VBox(false, 5); vbox.pack_start(radio1); vbox.pack_start(radio2); vbox.pack_start(radio3); vbox.pack_start(radio4); app.add_to_window(vbox); app.show(); Gtk.main(); return 0; } 12.radiobuttons.vala
app = new Application(); string str = "What is the password for " + Environment.get_user_name() + "?"; var question = new Label(str); var label = new Label("Password: "); var pass = new Entry(); pass.set_visibility(false); pass.set_invisible_char('*'); var hbox = new HBox(false, 5); hbox.pack_start(label); hbox.pack_start(pass); var vbox = new VBox(false, 5); vbox.pack_start(question); vbox.pack_start(hbox); app.add_to_window(vbox); app.show(); Gtk.main(); return 0; } 13.entries.vala
app = new Application(); var integer = new Adjustment(5.0, 0.0, 10.0, 1.0, 2.0, 2.0); var float_pt = new Adjustment(0.5, 0.0, 1.0, 0.1, 0.5, 0.5); var spin_int = new SpinButton(integer, 1.0, 0); var spin_float = new SpinButton(float_pt, 0.1, 1); var vbox = new VBox(false, 5); vbox.pack_start(spin_int); vbox.pack_start(spin_float); app.add_to_window(vbox); app.show(); Gtk.main(); return 0; } 14.spinbuttons.vala
new Dialog.with_buttons("Information", this.window, DialogFlags.MODAL, STOCK_OK, ResponseType.OK); dialog.set_has_separator(false); var label = new Label("The button was clicked!"); var image = new Image.from_stock(STOCK_DIALOG_INFO, IconSize.DIALOG); var hbox = new HBox(false, 5); hbox.set_border_width(10); hbox.pack_start(image); hbox.pack_start(label); dialog.vbox.pack_start(hbox); dialog.show_all(); dialog.run(); dialog.destroy(); } public static int main(string[] args) { Gtk.init(ref args); var app = new Application(); var button = new Button.with_mnemonic("_Click me"); button.clicked.connect(() => { app.button_clicked(button); }); app.add_to_window(button); app.show(); Gtk.main(); return 0; } 16.modaldialogs.vala
new Dialog.with_buttons("Information", this.window, DialogFlags.DESTROY_WITH_PARENT, STOCK_OK, ResponseType.OK); dialog.set_has_separator(false); var label = new Label("The button was clicked!"); var image = new Image.from_stock(STOCK_DIALOG_INFO, IconSize.DIALOG); var hbox = new HBox(false, 5); hbox.set_border_width(10); hbox.pack_start(image); hbox.pack_start(label); dialog.vbox.pack_start(hbox); dialog.show_all(); dialog.response.connect(() => { dialog.destroy(); }); } public static int main(string[] args) { Gtk.init(ref args); var app = new Application(); var button = new Button.with_mnemonic("_Click me"); button.clicked.connect(() => { app.button_clicked(button); }); app.add_to_window(button); app.show(); Gtk.main(); return 0; } 17.nonmodaldialogs.vala
. . . . var font = FontDescription.from_string("Monospace Bold 10"); var tv = new TextView(); tv.modify_font(font); tv.set_wrap_mode(Gtk.WrapMode.WORD); tv.set_justification(Justification.RIGHT); tv.set_editable(true); tv.set_cursor_visible(true); tv.set_pixels_above_lines(5); tv.set_pixels_below_lines(5); tv.set_pixels_inside_wrap(5); tv.set_left_margin(10); tv.set_right_margin(10); var buffer = tv.get_buffer(); buffer.set_text("This is some text!\nChange me!\nPlease!"); var scrolled_win = new ScrolledWindow(null, null); scrolled_win.set_policy(PolicyType.AUTOMATIC, PolicyType.ALWAYS); scrolled_win.add(tv); app.add_to_window(scrolled_win); app.show(); Gtk.main(); return 0; } 20.properties.vala
HTML backend update http://blogs.gnome.org/alexl/2011/03/15/gtk-html-backend-update/ Foundations of GTK+ Development http://www.amazon.com/Foundations-Development-Experts-Voice-Source/dp/1590597931/ Samples in this tutorial are mostly from this book GTK+ API Documentation http://www.gtk.org/documentation.html Valadoc – Vala API Documentation http://live.gnome.org/Vala/Documentation