├── menubar ├── gresources.xml └── menubar.ui ├── fontbutton.vala ├── entry.vala ├── paned.vala ├── searchentry.vala ├── scale.vala ├── grid.vala ├── buttonbox.vala ├── filechooserbutton.vala ├── spinbutton.vala ├── flowbox.vala ├── separator.vala ├── listbox.vala ├── notebook.vala ├── button.vala ├── checkbutton.vala ├── entrybuffer.vala ├── stackswitcher.vala ├── comboboxtext.vala ├── box.vala ├── stacksidebar.vala ├── actionbar.vala ├── spinner.vala ├── togglebutton.vala ├── label.vala ├── entrycompletion.vala ├── radiobutton.vala ├── stack.vala ├── combobox.vala ├── popover.vala ├── menubar.vala ├── treeview.vala ├── texteditor.vala ├── README.md ├── webkit2.vala ├── meson.build ├── LICENSE └── sourceview.vala /menubar/gresources.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | menubar.ui 5 | 6 | 7 | -------------------------------------------------------------------------------- /fontbutton.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * The FontButton provides the user with a button and dialog from which to 3 | * choose a font type, size, and styling options. 4 | * 5 | * Compile using: 6 | * valac fontbutton.vala --pkg gtk+-3.0 7 | * 8 | * Author: Andrew Steele 9 | */ 10 | 11 | using Gtk; 12 | 13 | public class Example : Window 14 | { 15 | public Example() 16 | { 17 | this.title = "FontButton"; 18 | this.destroy.connect(Gtk.main_quit); 19 | 20 | var fontbutton = new FontButton(); 21 | fontbutton.font_set.connect(on_fontbutton_changed); 22 | this.add(fontbutton); 23 | } 24 | 25 | public void on_fontbutton_changed(FontButton fontbutton) 26 | { 27 | var font = fontbutton.get_font_name(); 28 | stdout.printf("%s\n", (font)); 29 | } 30 | 31 | public static int main(string[] args) 32 | { 33 | Gtk.init(ref args); 34 | 35 | var window = new Example(); 36 | window.show_all(); 37 | 38 | Gtk.main(); 39 | 40 | return 0; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /entry.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * An Entry is used to receive and display short lines of text, with functions 3 | * to handle the data. 4 | * 5 | * Compile using: 6 | * valac entry.vala --pkg gtk+-3.0 7 | * 8 | * Author: Andrew Steele 9 | */ 10 | 11 | using Gtk; 12 | 13 | public class EntryExample : Window 14 | { 15 | private Entry entry; 16 | 17 | public EntryExample() 18 | { 19 | this.title = "Entry"; 20 | this.destroy.connect(Gtk.main_quit); 21 | 22 | entry = new Entry(); 23 | entry.set_placeholder_text("Enter some text..."); 24 | entry.activate.connect(on_entry_activated); 25 | this.add(entry); 26 | } 27 | 28 | public void on_entry_activated() 29 | { 30 | var text = entry.get_text(); 31 | stdout.printf("%s\n", text); 32 | } 33 | 34 | public static int main(string[] args) 35 | { 36 | Gtk.init(ref args); 37 | 38 | var window = new EntryExample(); 39 | window.show_all(); 40 | 41 | Gtk.main(); 42 | 43 | return 0; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /paned.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * The Paned widget provides two panels oriented vertically or horizontally. 3 | * Widgets can be added to the Paned container, with the separator between the 4 | * two panes being adjustable. 5 | * 6 | * Compile using: 7 | * valac paned.vala --pkg gtk+-3.0 8 | * 9 | * Author: Andrew Steele 10 | */ 11 | 12 | using Gtk; 13 | 14 | public class Example : Window 15 | { 16 | public Example() 17 | { 18 | this.title = "Paned"; 19 | this.destroy.connect(Gtk.main_quit); 20 | 21 | var paned = new Paned(Gtk.Orientation.VERTICAL); 22 | this.add(paned); 23 | 24 | var label1 = new Label("Paned area 1"); 25 | paned.add1(label1); 26 | 27 | var label2 = new Label("Paned area 2"); 28 | paned.add2(label2); 29 | } 30 | 31 | public static int main(string[] args) 32 | { 33 | Gtk.init(ref args); 34 | 35 | var window = new Example(); 36 | window.set_default_size(200, 200); 37 | window.show_all(); 38 | 39 | Gtk.main(); 40 | 41 | return 0; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /searchentry.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * A SearchEntry has an appearance similar to a standard Entry, but is tailored 3 | * for use when being used to provide search functionality. 4 | * 5 | * Compile using: 6 | * valac searchentry.vala --pkg gtk+-3.0 7 | * 8 | * Author: Andrew Steele 9 | */ 10 | 11 | using Gtk; 12 | 13 | public class Example : Window 14 | { 15 | public Example() 16 | { 17 | this.title = "SearchEntry"; 18 | this.destroy.connect(Gtk.main_quit); 19 | 20 | var searchentry = new SearchEntry(); 21 | searchentry.set_placeholder_text("Enter search text..."); 22 | searchentry.activate.connect(on_searchentry_activated); 23 | this.add(searchentry); 24 | } 25 | 26 | public void on_searchentry_activated(Entry searchentry) 27 | { 28 | var text = searchentry.get_text(); 29 | stdout.printf("%s\n", text); 30 | } 31 | 32 | public static int main(string[] args) 33 | { 34 | Gtk.init(ref args); 35 | 36 | var window = new Example(); 37 | window.show_all(); 38 | 39 | Gtk.main(); 40 | 41 | return 0; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /scale.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * The Scale widget provides a way for adjusting values between a set range, 3 | * with the user sliding a knob to the preferred value. 4 | * 5 | * Compile using: 6 | * valac scale.vala --pkg gtk+-3.0 7 | * 8 | * Author: Andrew Steele 9 | */ 10 | 11 | using Gtk; 12 | 13 | public class Example : Window 14 | { 15 | private Scale scale; 16 | 17 | public Example() 18 | { 19 | this.title = "Scale"; 20 | this.set_default_size(50, 400); 21 | this.destroy.connect(Gtk.main_quit); 22 | 23 | scale = new Scale.with_range(Gtk.Orientation.VERTICAL, 0, 100, 1); 24 | scale.set_value(50); 25 | scale.adjustment.value_changed.connect(on_scale_changed); 26 | this.add(scale); 27 | } 28 | 29 | private void on_scale_changed(Adjustment adjustment) 30 | { 31 | var value = scale.get_value(); 32 | stdout.printf("%.2f\n", value); 33 | } 34 | 35 | public static int main(string[] args) 36 | { 37 | Gtk.init(ref args); 38 | 39 | var window = new Example(); 40 | window.show_all(); 41 | 42 | Gtk.main(); 43 | 44 | return 0; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /grid.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * The Grid allows widgets to be placed horizontally and vertically across one 3 | * or more cells with options provided for spacing and sizing. 4 | * 5 | * Compile using: 6 | * valac grid.vala --pkg gtk+-3.0 7 | * 8 | * Author: Andrew Steele 9 | */ 10 | 11 | using Gtk; 12 | 13 | public class Example : Window 14 | { 15 | public Example() 16 | { 17 | this.title = "Grid"; 18 | this.destroy.connect(Gtk.main_quit); 19 | 20 | var grid = new Grid(); 21 | grid.set_row_spacing(5); 22 | grid.set_column_spacing(5); 23 | this.add(grid); 24 | 25 | var button1 = new Button.with_label("Button 1"); 26 | grid.attach(button1, 0, 0, 1, 1); 27 | var button2 = new Button.with_label("Button 2"); 28 | grid.attach(button2, 1, 0, 1, 2); 29 | var button3 = new Button.with_label("Button 3"); 30 | grid.attach(button3, 0, 1, 1, 1); 31 | } 32 | 33 | public static int main(string[] args) 34 | { 35 | Gtk.init(ref args); 36 | 37 | var window = new Example(); 38 | window.show_all(); 39 | 40 | Gtk.main(); 41 | 42 | return 0; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /buttonbox.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * A ButtonBox is a container often used to house many buttons in vertical or 3 | * horizontal orientations. It aids in keeping the child widgets a similar size. 4 | * 5 | * Compile using: 6 | * valac buttonbox.vala --pkg gtk+-3.0 7 | * 8 | * Author: Andrew Steele 9 | */ 10 | 11 | using Gtk; 12 | 13 | public class Example : Window 14 | { 15 | public Example() 16 | { 17 | this.title = "ButtonBox"; 18 | this.destroy.connect(Gtk.main_quit); 19 | 20 | var buttonbox = new ButtonBox(Gtk.Orientation.HORIZONTAL); 21 | buttonbox.set_spacing(5); 22 | this.add(buttonbox); 23 | 24 | var button1 = new Button.with_label("Button with extra text"); 25 | buttonbox.add(button1); 26 | var button2 = new Button.with_label("Button"); 27 | buttonbox.add(button2); 28 | var button3 = new ToggleButton.with_label("ToggleButton"); 29 | buttonbox.add(button3); 30 | } 31 | 32 | public static int main(string[] args) 33 | { 34 | Gtk.init(ref args); 35 | 36 | var window = new Example(); 37 | window.show_all(); 38 | 39 | Gtk.main(); 40 | 41 | return 0; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /filechooserbutton.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * The FileChooserButton provides a Button-like widget used for the selection 3 | * of files and/or folders from the file system. 4 | * 5 | * Compile using: 6 | * valac filechooserbutton.vala --pkg gtk+-3.0 7 | * 8 | * Author: Andrew Steele 9 | */ 10 | 11 | using Gtk; 12 | 13 | public class Example : Window 14 | { 15 | public Example() 16 | { 17 | this.title = "FileChooserButton"; 18 | this.set_default_size(200, -1); 19 | this.destroy.connect(Gtk.main_quit); 20 | 21 | var filechooserbutton = new Gtk.FileChooserButton("Select File", Gtk.FileChooserAction.OPEN); 22 | filechooserbutton.file_set.connect(on_file_set); 23 | this.add(filechooserbutton); 24 | } 25 | 26 | private void on_file_set(FileChooserButton filechooserbutton) 27 | { 28 | var filename = filechooserbutton.get_filename(); 29 | stdout.printf("Selected filename: %s\n", filename); 30 | } 31 | 32 | public static int main(string[] args) 33 | { 34 | Gtk.init(ref args); 35 | 36 | var window = new Example(); 37 | window.show_all(); 38 | 39 | Gtk.main(); 40 | 41 | return 0; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /spinbutton.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * A SpinButton provides a way to enter numbers either by textual entry from the 3 | * user, or by changing the value with up/down buttons. Ranges to limit the 4 | * value entered are able to be set. 5 | * 6 | * Compile using: 7 | * valac spinbutton.vala --pkg gtk+-3.0 8 | * 9 | * Author: Andrew Steele 10 | */ 11 | 12 | using Gtk; 13 | 14 | public class Example : Window 15 | { 16 | private SpinButton spinbutton; 17 | 18 | public Example() 19 | { 20 | this.title = "SpinButton"; 21 | this.destroy.connect(Gtk.main_quit); 22 | 23 | spinbutton = new SpinButton.with_range(0, 10, 1); 24 | spinbutton.set_value(2); 25 | spinbutton.value_changed.connect(on_spinbutton_changed); 26 | this.add(spinbutton); 27 | } 28 | 29 | private void on_spinbutton_changed(SpinButton spinbutton) 30 | { 31 | var value = spinbutton.get_value(); 32 | stdout.printf("%.2f\n", value); 33 | } 34 | 35 | public static int main(string[] args) 36 | { 37 | Gtk.init(ref args); 38 | 39 | var window = new Example(); 40 | window.show_all(); 41 | 42 | Gtk.main(); 43 | 44 | return 0; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /flowbox.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * The FlowBox positions child widgets either horizontally or vertically 3 | * depending on how much size the container is allocated. Widgets are moved 4 | * dynamically as the container changes size and shape. 5 | * 6 | * Compile using: 7 | * valac flowbox.vala --pkg gtk+-3.0 8 | * 9 | * Author: Andrew Steele 10 | */ 11 | 12 | using Gtk; 13 | 14 | public class Example : Window 15 | { 16 | public Example() 17 | { 18 | this.title = "FlowBox"; 19 | this.destroy.connect(Gtk.main_quit); 20 | 21 | var flowbox = new FlowBox(); 22 | this.add(flowbox); 23 | 24 | var button = new Button.with_label("Button in FlowBox"); 25 | flowbox.insert(button, 0); 26 | 27 | var togglebutton = new ToggleButton.with_label("ToggleButton in FlowBox"); 28 | flowbox.insert(togglebutton, 1); 29 | 30 | var checkbutton = new CheckButton.with_label("CheckButton in FlowBox"); 31 | flowbox.insert(checkbutton, -1); 32 | } 33 | 34 | public static int main(string[] args) 35 | { 36 | Gtk.init(ref args); 37 | 38 | var window = new Example(); 39 | window.show_all(); 40 | 41 | Gtk.main(); 42 | 43 | return 0; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /separator.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * The Separator is a sparsely used widget to visually separate content being 3 | * displayed. They can be horizontally or vertically oriented. 4 | * 5 | * Compile using: 6 | * valac separator.vala --pkg gtk+-3.0 7 | * 8 | * Author: Andrew Steele 9 | */ 10 | 11 | using Gtk; 12 | 13 | public class Example : Window 14 | { 15 | public Example() 16 | { 17 | this.title = "Separator"; 18 | this.set_default_size(400, 200); 19 | this.destroy.connect(Gtk.main_quit); 20 | 21 | var grid = new Grid(); 22 | this.add(grid); 23 | 24 | var hseparator = new Separator(Gtk.Orientation.HORIZONTAL); 25 | hseparator.set_vexpand(true); 26 | hseparator.set_hexpand(true); 27 | grid.attach(hseparator, 0, 0, 1, 1); 28 | 29 | var vseparator = new Separator(Gtk.Orientation.VERTICAL); 30 | vseparator.set_vexpand(true); 31 | vseparator.set_hexpand(true); 32 | grid.attach(vseparator, 1, 0, 1, 1); 33 | } 34 | 35 | public static int main(string[] args) 36 | { 37 | Gtk.init(ref args); 38 | 39 | var window = new Example(); 40 | window.show_all(); 41 | 42 | Gtk.main(); 43 | 44 | return 0; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /listbox.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * The ListBox widget provides a vertical container holding ListBoxRow children. 3 | * The children are created automatically when another widget is added.. The 4 | * container also provides sorting and filtering, allowing more complex layouts 5 | * than can be achieved via a CellRenderer. 6 | * 7 | * Compile using: 8 | * valac listbox.vala --pkg gtk+-3.0 9 | * 10 | * Author: Andrew Steele 11 | */ 12 | 13 | using Gtk; 14 | 15 | public class Example : Window 16 | { 17 | public Example() 18 | { 19 | this.title = "ListBox"; 20 | this.set_default_size(200, 200); 21 | this.destroy.connect(Gtk.main_quit); 22 | 23 | var listbox = new ListBox(); 24 | this.add(listbox); 25 | 26 | var label1 = new Label("Label 1"); 27 | listbox.insert(label1, -1); 28 | 29 | var label2 = new Label("Label 2"); 30 | listbox.insert(label2, -1); 31 | 32 | var label3 = new Label("Label 3"); 33 | listbox.insert(label3, -1); 34 | } 35 | 36 | public static int main(string[] args) 37 | { 38 | Gtk.init(ref args); 39 | 40 | var window = new Example(); 41 | window.show_all(); 42 | 43 | Gtk.main(); 44 | 45 | return 0; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /notebook.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * A Notebook can be used to provide tabulated pages on which different content 3 | * can be added. 4 | * 5 | * Compile using: 6 | * valac notebook.vala --pkg gtk+-3.0 7 | * 8 | * Author: Andrew Steele 9 | */ 10 | 11 | using Gtk; 12 | 13 | public class Example : Window 14 | { 15 | private Notebook notebook; 16 | 17 | public Example() 18 | { 19 | this.title = "Notebook"; 20 | this.set_default_size(200, 200); 21 | this.destroy.connect(Gtk.main_quit); 22 | 23 | notebook = new Notebook(); 24 | this.add(notebook); 25 | 26 | int count; 27 | 28 | for (count = 1; count <= 3; count++) 29 | { 30 | var text1 = "Tab %i".printf(count); 31 | 32 | var label = new Label(null); 33 | label.set_label(text1); 34 | 35 | var text2 = "Button %i in Tab %i".printf(count, count); 36 | 37 | var button = new Button.with_label(text2); 38 | notebook.append_page(button, label); 39 | } 40 | } 41 | 42 | public static int main(string[] args) 43 | { 44 | Gtk.init(ref args); 45 | 46 | var window = new Example(); 47 | window.show_all(); 48 | 49 | Gtk.main(); 50 | 51 | return 0; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /button.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * The Button widget is commonly found in programs and used to launch processes 3 | * and operations. 4 | * 5 | * Compile using: 6 | * valac button.vala --pkg gtk+-3.0 7 | * 8 | * Author: Andrew Steele 9 | */ 10 | 11 | using Gtk; 12 | 13 | public class Example : Window 14 | { 15 | private Button button; 16 | 17 | public Example() 18 | { 19 | this.title = "Button"; 20 | this.destroy.connect(Gtk.main_quit); 21 | 22 | var box = new Box(Gtk.Orientation.VERTICAL, 5); 23 | this.add(box); 24 | 25 | button = new Button(); 26 | button.set_label("Button 1"); 27 | button.clicked.connect(on_button_clicked); 28 | box.add(button); 29 | button = new Button(); 30 | button.set_label("Button 2"); 31 | button.clicked.connect(on_button_clicked); 32 | box.add(button); 33 | } 34 | 35 | private void on_button_clicked(Button button) 36 | { 37 | var label = button.get_label(); 38 | stdout.printf("%s clicked\n", label); 39 | } 40 | 41 | public static int main(string[] args) 42 | { 43 | Gtk.init(ref args); 44 | 45 | var window = new Example(); 46 | window.show_all(); 47 | 48 | Gtk.main(); 49 | 50 | return 0; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /checkbutton.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * Similar to the RadioButton, the CheckButton combines a label and box to 3 | * indicate the current state. When the CheckButton is toggled, a tick is either 4 | * shown or hidden. 5 | * 6 | * Compile using: 7 | * valac checkbutton.vala --pkg gtk+-3.0 8 | * 9 | * Author: Andrew Steele 10 | */ 11 | 12 | using Gtk; 13 | 14 | public class Example : Window 15 | { 16 | private CheckButton checkbutton; 17 | 18 | public Example() 19 | { 20 | this.title = "CheckButton"; 21 | this.destroy.connect(Gtk.main_quit); 22 | 23 | checkbutton = new CheckButton(); 24 | checkbutton.set_label("Toggle the CheckButton"); 25 | checkbutton.toggled.connect(on_checkbutton_toggle); 26 | this.add(checkbutton); 27 | } 28 | 29 | private void on_checkbutton_toggle() 30 | { 31 | var active = checkbutton.get_active(); 32 | 33 | if (active == true) 34 | stdout.printf("CheckButton toggled on\n"); 35 | else 36 | stdout.printf("CheckButton toggled off\n"); 37 | } 38 | 39 | public static int main(string[] args) 40 | { 41 | Gtk.init(ref args); 42 | 43 | var window = new Example(); 44 | window.show_all(); 45 | 46 | Gtk.main(); 47 | 48 | return 0; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /entrybuffer.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * The EntryBuffer object provides a way to store text held in an Entry, with 3 | * functionality for handling the text and sharing to other Entry widgets. 4 | * 5 | * Compile using: 6 | * valac entrybuffer.vala --pkg gtk+-3.0 7 | * 8 | * Author: Andrew Steele 9 | */ 10 | 11 | using Gtk; 12 | 13 | public class EntryExample : Window 14 | { 15 | private Entry entry; 16 | private EntryBuffer entrybuffer; 17 | 18 | public EntryExample() 19 | { 20 | this.title = "EntryBuffer"; 21 | this.destroy.connect(Gtk.main_quit); 22 | 23 | var grid = new Grid(); 24 | this.add(grid); 25 | 26 | unowned uint8[] text = (uint8[]) "Entry with EntryBuffer"; 27 | entrybuffer = new EntryBuffer(text); 28 | 29 | entry = new Entry(); 30 | entry.set_buffer(entrybuffer); 31 | grid.add(entry); 32 | 33 | entry = new Entry(); 34 | entry.set_buffer(entrybuffer); 35 | grid.add(entry); 36 | 37 | entry = new Entry(); 38 | entry.set_buffer(entrybuffer); 39 | grid.add(entry); 40 | } 41 | 42 | public static int main(string[] args) 43 | { 44 | Gtk.init(ref args); 45 | 46 | var window = new EntryExample(); 47 | window.show_all(); 48 | 49 | Gtk.main(); 50 | 51 | return 0; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /stackswitcher.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * The StackSwitcher can be added to the Stack to provide a way to change the 3 | * visible child using buttons. 4 | * 5 | * Compile using: 6 | * valac stackswitcher.vala --pkg gtk+-3.0 7 | * 8 | * Author: Andrew Steele 9 | */ 10 | 11 | using Gtk; 12 | 13 | public class Example : Window 14 | { 15 | public Example() 16 | { 17 | this.title = "Stack"; 18 | this.destroy.connect(Gtk.main_quit); 19 | 20 | var grid = new Grid(); 21 | this.add(grid); 22 | 23 | var stackswitcher = new StackSwitcher(); 24 | grid.attach(stackswitcher, 0, 0, 1, 1); 25 | 26 | var stack = new Stack(); 27 | stack.set_vexpand(true); 28 | stack.set_hexpand(true); 29 | stackswitcher.set_stack(stack); 30 | grid.attach(stack, 0, 1, 1, 1); 31 | 32 | var label1 = new Label("Page 1 of Stack"); 33 | stack.add_titled(label1, "Page1", "Page 1"); 34 | 35 | var label2 = new Label("Page 2 of Stack"); 36 | stack.add_titled(label2, "Page2", "Page 2"); 37 | } 38 | 39 | public static int main(string[] args) 40 | { 41 | Gtk.init(ref args); 42 | 43 | var window = new Example(); 44 | window.set_default_size(200, 200); 45 | window.show_all(); 46 | 47 | Gtk.main(); 48 | 49 | return 0; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /comboboxtext.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * The ComboBoxText provides a simple dropdown menu to select values from a 3 | * list. Text is also permitted to be entered if the option is set. 4 | * 5 | * Compile using: 6 | * valac comboboxtext.vala --pkg gtk+-3.0 7 | * 8 | * Author: Andrew Steele 9 | */ 10 | 11 | using Gtk; 12 | 13 | public class Example : Window 14 | { 15 | private ComboBoxText comboboxtext; 16 | 17 | public Example() 18 | { 19 | this.title = "ComboBoxText"; 20 | this.destroy.connect(Gtk.main_quit); 21 | 22 | comboboxtext = new ComboBoxText(); 23 | comboboxtext.append_text("Madrid"); 24 | comboboxtext.append_text("Valencia"); 25 | comboboxtext.append_text("Seville"); 26 | comboboxtext.append_text("Bilbao"); 27 | comboboxtext.set_active(0); 28 | comboboxtext.changed.connect(on_comboboxtext_changed); 29 | this.add(comboboxtext); 30 | } 31 | 32 | private void on_comboboxtext_changed() 33 | { 34 | var selection = comboboxtext.get_active_text(); 35 | stdout.printf("Selection is '%s'\n", selection); 36 | } 37 | 38 | public static int main(string[] args) 39 | { 40 | Gtk.init(ref args); 41 | 42 | var window = new Example(); 43 | window.show_all(); 44 | 45 | Gtk.main(); 46 | 47 | return 0; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /box.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * The Box container allows child widgets to be added in a horizontal or 3 | * vertical orientation, with customisations for the sizing on the added widget. 4 | * 5 | * Compile using: 6 | * valac box.vala --pkg gtk+-3.0 7 | * 8 | * Author: Andrew Steele 9 | */ 10 | 11 | using Gtk; 12 | 13 | public class Example : Window 14 | { 15 | public Example() 16 | { 17 | this.title = "Box"; 18 | this.destroy.connect(Gtk.main_quit); 19 | 20 | var vbox = new Box(Gtk.Orientation.VERTICAL, 5); 21 | vbox.set_spacing(5); 22 | this.add(vbox); 23 | 24 | var button1 = new Button.with_label("Button 1"); 25 | vbox.add(button1); 26 | var button2 = new Button.with_label("Button 2"); 27 | vbox.pack_end(button2, true, false, 25); 28 | 29 | var hbox = new Box(Gtk.Orientation.HORIZONTAL, 5); 30 | hbox.set_spacing(5); 31 | vbox.add(hbox); 32 | 33 | var button3 = new Button.with_label("Button 3"); 34 | hbox.pack_start(button3, true, true, 0); 35 | var button4 = new Button.with_label("Button 4"); 36 | hbox.add(button4); 37 | } 38 | 39 | public static int main(string[] args) 40 | { 41 | Gtk.init(ref args); 42 | 43 | var window = new Example(); 44 | window.show_all(); 45 | 46 | Gtk.main(); 47 | 48 | return 0; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /stacksidebar.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * The StackSidebar works in a similar way to the StackSwitcher, however it 3 | * offers the choice of visible child in the Stack via a sidebar, with each item 4 | * listed vertically. 5 | * 6 | * Compile using: 7 | * valac stacksidebar.vala --pkg gtk+-3.0 8 | * 9 | * Author: Andrew Steele 10 | */ 11 | 12 | using Gtk; 13 | 14 | public class Example : Window 15 | { 16 | public Example() 17 | { 18 | this.title = "StackSidebar"; 19 | this.destroy.connect(Gtk.main_quit); 20 | 21 | var grid = new Grid(); 22 | this.add(grid); 23 | 24 | var stacksidebar = new StackSidebar(); 25 | grid.attach(stacksidebar, 0, 0, 1, 1); 26 | 27 | var stack = new Stack(); 28 | stack.set_vexpand(true); 29 | stack.set_hexpand(true); 30 | stacksidebar.set_stack(stack); 31 | grid.attach(stack, 1, 0, 1, 1); 32 | 33 | var label1 = new Label("Page 1 of Stack"); 34 | stack.add_titled(label1, "Page1", "Page 1"); 35 | 36 | var label2 = new Label("Page 2 of Stack"); 37 | stack.add_titled(label2, "Page2", "Page 2"); 38 | } 39 | 40 | public static int main(string[] args) 41 | { 42 | Gtk.init(ref args); 43 | 44 | var window = new Example(); 45 | window.set_default_size(200, 200); 46 | window.show_all(); 47 | 48 | Gtk.main(); 49 | 50 | return 0; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /actionbar.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * The ActionBar container is often placed below the content of the window, and 3 | * is used to show contextual actions. 4 | * 5 | * Compile using: 6 | * valac actionbar.vala --pkg gtk+-3.0 7 | * 8 | * Author: Andrew Steele 9 | */ 10 | 11 | using Gtk; 12 | 13 | public class Example : Window 14 | { 15 | public Example() 16 | { 17 | this.title = "ActionBar"; 18 | this.set_default_size(200, 200); 19 | this.destroy.connect(Gtk.main_quit); 20 | 21 | var grid = new Grid(); 22 | grid.set_row_spacing(5); 23 | grid.set_column_spacing(5); 24 | this.add(grid); 25 | 26 | var label = new Label(""); 27 | label.set_vexpand(true); 28 | grid.attach(label, 0, 0, 1, 1); 29 | 30 | var actionbar = new ActionBar(); 31 | actionbar.set_hexpand(true); 32 | grid.attach(actionbar, 0, 1, 1, 1); 33 | 34 | var button1 = new Button.with_label("Cut"); 35 | actionbar.pack_start(button1); 36 | var button2 = new Button.with_label("Copy"); 37 | actionbar.pack_start(button2); 38 | var button3 = new Button.with_label("Paste"); 39 | actionbar.pack_end(button3); 40 | } 41 | 42 | public static int main(string[] args) 43 | { 44 | Gtk.init(ref args); 45 | 46 | var window = new Example(); 47 | window.show_all(); 48 | 49 | Gtk.main(); 50 | 51 | return 0; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /spinner.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * The Spinner widget provides an animated indicator of activity in the program, 3 | * and is useful to indicate a long-running task. 4 | * 5 | * Compile using: 6 | * valac spinner.vala --pkg gtk+-3.0 7 | * 8 | * Author: Andrew Steele 9 | */ 10 | 11 | using Gtk; 12 | 13 | public class Example : Window 14 | { 15 | private Spinner spinner; 16 | 17 | public Example() 18 | { 19 | this.title = "Spinner"; 20 | this.set_default_size(200, 200); 21 | this.destroy.connect(Gtk.main_quit); 22 | 23 | var grid = new Gtk.Grid(); 24 | this.add(grid); 25 | 26 | var buttonStart = new Gtk.Button.with_label("Start"); 27 | buttonStart.clicked.connect(on_start_button_clicked); 28 | grid.attach(buttonStart, 0, 1, 1, 1); 29 | var buttonStop = new Gtk.Button.with_label("Stop"); 30 | buttonStop.clicked.connect(on_stop_button_clicked); 31 | grid.attach(buttonStop, 1, 1, 1, 1); 32 | 33 | spinner = new Spinner(); 34 | spinner.set_vexpand(true); 35 | spinner.set_hexpand(true); 36 | grid.attach(spinner, 0, 0, 2, 1); 37 | } 38 | 39 | private void on_start_button_clicked(Button button) 40 | { 41 | spinner.start(); 42 | } 43 | 44 | private void on_stop_button_clicked(Button button) 45 | { 46 | spinner.stop(); 47 | } 48 | 49 | public static int main(string[] args) 50 | { 51 | Gtk.init(ref args); 52 | 53 | var window = new Example(); 54 | window.show_all(); 55 | 56 | Gtk.main(); 57 | 58 | return 0; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /togglebutton.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * ToggleButton widgets is used to indicate whether something is active or not, 3 | * with the appearance of a standard Button widget. 4 | * 5 | * Compile using: 6 | * valac togglebutton.vala --pkg gtk+-3.0 7 | * 8 | * Author: Andrew Steele 9 | */ 10 | 11 | using Gtk; 12 | 13 | public class Example : Window 14 | { 15 | private ToggleButton togglebutton; 16 | 17 | public Example() 18 | { 19 | this.title = "ToggleButton"; 20 | this.destroy.connect(Gtk.main_quit); 21 | 22 | var box = new Box(Gtk.Orientation.VERTICAL, 5); 23 | this.add(box); 24 | 25 | togglebutton = new ToggleButton(); 26 | togglebutton.set_label("ToggleButton 1"); 27 | togglebutton.toggled.connect(on_checkbutton_toggle); 28 | box.add(togglebutton); 29 | togglebutton = new ToggleButton(); 30 | togglebutton.set_label("ToggleButton 2"); 31 | togglebutton.toggled.connect(on_checkbutton_toggle); 32 | box.add(togglebutton); 33 | } 34 | 35 | private void on_checkbutton_toggle(ToggleButton togglebutton) 36 | { 37 | var active = togglebutton.get_active(); 38 | var label = togglebutton.get_label(); 39 | 40 | if (active == true) 41 | stdout.printf("%s toggled on\n", label); 42 | else 43 | stdout.printf("%s toggled off\n", label); 44 | } 45 | 46 | public static int main(string[] args) 47 | { 48 | Gtk.init(ref args); 49 | 50 | var window = new Example(); 51 | window.show_all(); 52 | 53 | Gtk.main(); 54 | 55 | return 0; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /label.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * The Label is commonly used for basic purposes such as displaying short 3 | * amounts of text. It does however provide a number of extra features allowing 4 | * the display of complex text layouts. 5 | * 6 | * Compile using: 7 | * valac label.vala --pkg gtk+-3.0 8 | * 9 | * Author: Andrew Steele 10 | */ 11 | 12 | using Gtk; 13 | 14 | public class Example : Window 15 | { 16 | public Example() 17 | { 18 | this.title = "Label"; 19 | this.destroy.connect(Gtk.main_quit); 20 | 21 | var vbox = new Box(Gtk.Orientation.VERTICAL, 5); 22 | vbox.set_spacing(5); 23 | this.add(vbox); 24 | 25 | var label1 = new Label("This is a single-line example."); 26 | vbox.add(label1); 27 | var label2 = new Label("This is a multiple\nline\nexample using new line breaks."); 28 | vbox.add(label2); 29 | 30 | var hbox = new Box(Gtk.Orientation.HORIZONTAL, 5); 31 | hbox.set_spacing(5); 32 | vbox.add(hbox); 33 | 34 | var label3 = new Label(null); 35 | label3.set_justify(Gtk.Justification.RIGHT); 36 | label3.set_label("This is an\nexample label\nright justified."); 37 | hbox.add(label3); 38 | var label4 = new Label(null); 39 | label4.set_justify(Gtk.Justification.CENTER); 40 | label4.set_markup("This is a\ncenter aligned\n label with markup."); 41 | hbox.pack_end(label4, true, true, 0); 42 | } 43 | 44 | public static int main(string[] args) 45 | { 46 | Gtk.init(ref args); 47 | 48 | var window = new Example(); 49 | window.show_all(); 50 | 51 | Gtk.main(); 52 | 53 | return 0; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /entrycompletion.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * Coupled with an Entry, the EntryCompletion object provides matching of text 3 | * to a list of entries, allowing the user to select a value. 4 | * 5 | * Compile using: 6 | * valac entrycompletion.vala --pkg gtk+-3.0 7 | * 8 | * Author: Andrew Steele 9 | */ 10 | 11 | using Gtk; 12 | 13 | public class EntryExample : Window 14 | { 15 | private Gtk.ListStore liststore; 16 | private Entry entry; 17 | private EntryCompletion entrycompletion; 18 | 19 | public EntryExample() 20 | { 21 | this.title = "Entry"; 22 | this.destroy.connect(Gtk.main_quit); 23 | 24 | entry = new Entry(); 25 | this.add(entry); 26 | 27 | liststore = new Gtk.ListStore(1, typeof(string)); 28 | 29 | Gtk.TreeIter iter; 30 | liststore.append(out iter); 31 | liststore.set(iter, 0, "Oklahoma"); 32 | liststore.append(out iter); 33 | liststore.set(iter, 0, "California"); 34 | liststore.append(out iter); 35 | liststore.set(iter, 0, "Texas"); 36 | liststore.append(out iter); 37 | liststore.set(iter, 0, "Connecticut"); 38 | liststore.append(out iter); 39 | liststore.set(iter, 0, "Arizona"); 40 | 41 | entrycompletion = new EntryCompletion(); 42 | entrycompletion.set_model(liststore); 43 | entrycompletion.set_text_column(0); 44 | entrycompletion.set_popup_completion(true); 45 | entry.set_completion(entrycompletion); 46 | } 47 | 48 | public static int main(string[] args) 49 | { 50 | Gtk.init(ref args); 51 | 52 | var window = new EntryExample(); 53 | window.show_all(); 54 | 55 | Gtk.main(); 56 | 57 | return 0; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /radiobutton.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * A RadioButton is often combined with others to indicate the status from a 3 | * number of items. Each provides a label and display to indicate which of the 4 | * group is selected. 5 | * 6 | * Compile using: 7 | * valac radiobutton.vala --pkg gtk+-3.0 8 | * 9 | * Author: Andrew Steele 10 | */ 11 | 12 | using Gtk; 13 | 14 | public class Example : Window 15 | { 16 | public Example() 17 | { 18 | this.title = "RadioButton"; 19 | this.destroy.connect(Gtk.main_quit); 20 | 21 | var box = new Gtk.Box(Gtk.Orientation.VERTICAL, 5); 22 | this.add(box); 23 | 24 | var radiobutton1 = new RadioButton(null); 25 | radiobutton1.set_label("RadioButton 1"); 26 | radiobutton1.toggled.connect(on_radiobutton_toggle); 27 | box.add(radiobutton1); 28 | 29 | var radiobutton2 = new RadioButton(radiobutton1.get_group()); 30 | radiobutton2.set_label("RadioButton 2"); 31 | radiobutton2.toggled.connect(on_radiobutton_toggle); 32 | box.add(radiobutton2); 33 | 34 | var radiobutton3 = new RadioButton(radiobutton1.get_group()); 35 | radiobutton3.set_label("RadioButton 3"); 36 | radiobutton3.toggled.connect(on_radiobutton_toggle); 37 | box.add(radiobutton3); 38 | } 39 | 40 | private void on_radiobutton_toggle(Gtk.ToggleButton radiobutton) 41 | { 42 | if (radiobutton.get_active()) 43 | { 44 | var label = radiobutton.get_label(); 45 | print("%s toggled\n", label); 46 | } 47 | } 48 | 49 | public static int main(string[] args) 50 | { 51 | Gtk.init(ref args); 52 | 53 | var window = new Example(); 54 | window.show_all(); 55 | 56 | Gtk.main(); 57 | 58 | return 0; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /stack.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * The Stack widget is similar to a Notebook in providing a container where the 3 | * visible object can be changed. On its own however, a Stack does not provide 4 | * a way for the user to change what is visible. 5 | * 6 | * Compile using: 7 | * valac stack.vala --pkg gtk+-3.0 8 | * 9 | * Author: Andrew Steele 10 | */ 11 | 12 | using Gtk; 13 | 14 | public class Example : Window 15 | { 16 | private Stack stack; 17 | 18 | public Example() 19 | { 20 | this.title = "Stack"; 21 | this.destroy.connect(Gtk.main_quit); 22 | 23 | var grid = new Grid(); 24 | this.add(grid); 25 | 26 | stack = new Stack(); 27 | stack.set_vexpand(true); 28 | stack.set_hexpand(true); 29 | grid.attach(stack, 0, 0, 2, 1); 30 | 31 | var button1 = new Button.with_label("Page 1"); 32 | button1.clicked.connect(on_button1_clicked); 33 | grid.attach(button1, 0, 1, 1, 1); 34 | var button2 = new Button.with_label("Page 2"); 35 | button2.clicked.connect(on_button2_clicked); 36 | grid.attach(button2, 1, 1, 1, 1); 37 | 38 | var label1 = new Label("Page 1 of Stack"); 39 | stack.add_named(label1, "Page1"); 40 | 41 | var label2 = new Label("Page 2 of Stack"); 42 | stack.add_named(label2, "Page2"); 43 | } 44 | 45 | private void on_button1_clicked(Button button) 46 | { 47 | stack.set_visible_child_name("Page1"); 48 | } 49 | 50 | private void on_button2_clicked(Button button) 51 | { 52 | stack.set_visible_child_name("Page2"); 53 | } 54 | 55 | public static int main(string[] args) 56 | { 57 | Gtk.init(ref args); 58 | 59 | var window = new Example(); 60 | window.set_default_size(200, 200); 61 | window.show_all(); 62 | 63 | Gtk.main(); 64 | 65 | return 0; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /combobox.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * Similar to a ComboBoxText, the ComboBox allows selection of items from a 3 | * dropdown list. It provides more features, and is capable of displaying 4 | * options of different types other than text. 5 | * 6 | * Compile using: 7 | * valac combobox.vala --pkg gtk+-3.0 8 | * 9 | * Author: Andrew Steele 10 | */ 11 | 12 | using Gtk; 13 | 14 | public class Example : Window 15 | { 16 | private Gtk.ListStore liststore; 17 | private ComboBox combobox; 18 | private CellRendererText cellrenderertext; 19 | 20 | public Example() 21 | { 22 | this.title = "ComboBox"; 23 | this.destroy.connect(Gtk.main_quit); 24 | 25 | liststore = new Gtk.ListStore(1, typeof (string)); 26 | Gtk.TreeIter iter; 27 | 28 | liststore.append(out iter); 29 | liststore.set(iter, 0, "Rafael Nadal", -1); 30 | liststore.append(out iter); 31 | liststore.set(iter, 0, "Roger Federer", -1); 32 | liststore.append(out iter); 33 | liststore.set(iter, 0, "Novak Djokovic", -1); 34 | 35 | cellrenderertext = new CellRendererText(); 36 | 37 | combobox = new ComboBox(); 38 | combobox.set_model(liststore); 39 | combobox.pack_start(cellrenderertext, true); 40 | combobox.add_attribute(cellrenderertext, "text", 0); 41 | combobox.changed.connect(on_combobox_changed); 42 | this.add(combobox); 43 | } 44 | 45 | private void on_combobox_changed() 46 | { 47 | Gtk.TreeIter iter; 48 | Value val; 49 | 50 | combobox.get_active_iter(out iter); 51 | liststore.get_value(iter, 0, out val); 52 | 53 | stdout.printf("Selection is '%s'\n", (string) val); 54 | } 55 | 56 | public static int main(string[] args) 57 | { 58 | Gtk.init(ref args); 59 | 60 | var window = new Example(); 61 | window.show_all(); 62 | 63 | Gtk.main(); 64 | 65 | return 0; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /popover.vala: -------------------------------------------------------------------------------- 1 | public class Application : Gtk.Window { 2 | 3 | public GLib.Menu menu_model { get; set; } 4 | 5 | public Application () { 6 | this.set_default_size (700, 600); 7 | 8 | var header_bar = new Gtk.HeaderBar (); 9 | header_bar.set_title ("teste"); 10 | header_bar.show_close_button = true; 11 | 12 | var button = new Gtk.Button(); 13 | button.set_image (new Gtk.Image.from_icon_name ("open-menu-symbolic", Gtk.IconSize.LARGE_TOOLBAR)); 14 | 15 | var menu = new GLib.Menu (); 16 | var item1 = new GLib.MenuItem ("item 1", null); 17 | var item2 = new GLib.MenuItem ("item 2", null); 18 | menu.append_item (item1); 19 | menu.append_item (item2); 20 | 21 | Gtk.Grid search_grid = new Gtk.Grid (); 22 | search_grid.set_column_spacing (10); 23 | search_grid.set_row_spacing (10); 24 | search_grid.set_margin_top (10); 25 | search_grid.set_margin_end (10); 26 | search_grid.set_margin_bottom (10); 27 | search_grid.set_margin_start (10); 28 | 29 | var label = new Gtk.Label ("Option"); 30 | search_grid.attach (label, 0, 0); 31 | 32 | var widget = new Gtk.Switch (); 33 | search_grid.attach (widget, 1, 0); 34 | 35 | label = new Gtk.Label ("Option 2"); 36 | search_grid.attach (label, 0, 1); 37 | 38 | widget = new Gtk.Switch (); 39 | search_grid.attach (widget, 1, 1); 40 | 41 | search_grid.show_all (); 42 | 43 | Gtk.SearchEntry entry = new Gtk.SearchEntry (); 44 | entry.set_placeholder_text ("Search..."); 45 | header_bar.set_custom_title (entry); 46 | 47 | Gtk.Popover search_pop = new Gtk.Popover (entry); 48 | search_pop.add (search_grid); 49 | 50 | entry.enter_notify_event.connect ((e) => { 51 | search_pop.set_visible (true); 52 | return true; 53 | }); 54 | 55 | var menu_popover = new Gtk.Popover(button); 56 | menu_popover.position = Gtk.PositionType.BOTTOM; 57 | menu_popover.set_size_request (256, -1); 58 | menu_popover.modal = false; 59 | menu_popover.bind_model (menu, null); 60 | 61 | button.clicked.connect (() => { 62 | menu_popover.set_visible (true); 63 | }); 64 | 65 | header_bar.pack_end (button); 66 | 67 | this.set_titlebar (header_bar); 68 | } 69 | 70 | public static int main (string[] args) { 71 | Gtk.init (ref args); 72 | 73 | Application app = new Application (); 74 | app.show_all (); 75 | Gtk.main (); 76 | return 0; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /menubar.vala: -------------------------------------------------------------------------------- 1 | public class MyWindow : Gtk.ApplicationWindow { 2 | 3 | /* Callback functions for the window actions. */ 4 | void copy_cb (SimpleAction action, Variant? parameter) { 5 | print ("\"Copy\" activated\n"); 6 | } 7 | 8 | void paste_cb (SimpleAction action, Variant? parameter) { 9 | print ("\"Paste\" activated\n"); 10 | } 11 | 12 | void shape_cb (SimpleAction action, Variant? parameter) { 13 | print ("shape is set to %s\n", parameter.get_string(null)); 14 | action.set_state (parameter); 15 | } 16 | 17 | /* Create the window actions. */ 18 | const ActionEntry[] actions = { 19 | /*{ "action name", cb to connect to "activate" signal, parameter type, 20 | initial state, cb to connect to "change-state" signal } */ 21 | { "copy", copy_cb }, 22 | { "paste", paste_cb }, 23 | { "shape", shape_cb, "s", "'line'"} 24 | }; 25 | 26 | internal MyWindow (MyApplication app) { 27 | Object (application: app, title: "MenuBar Example"); 28 | this.set_default_size (200, 200); 29 | 30 | /* Setup window actions. */ 31 | this.add_action_entries (actions, this); 32 | } 33 | } 34 | 35 | class MyApplication: Gtk.Application { 36 | protected override void activate () { 37 | new MyWindow (this).show (); 38 | } 39 | 40 | /* Callback functions for the application actions. */ 41 | void new_cb (SimpleAction action, Variant? parameter) { 42 | //new MyWindow (this).show (); 43 | print ("You clicked \"New\"\n"); 44 | } 45 | 46 | void quit_cb (SimpleAction action, Variant? parameter) { 47 | print ("You clicked \"Quit\"\n"); 48 | //this.quit (); **Bug #674090** 49 | } 50 | 51 | void awesome_cb (SimpleAction action, Variant? parameter) { 52 | var active = action.get_state ().get_boolean (); 53 | action.set_state (new Variant.boolean (!active)); 54 | if (active) 55 | print ("You unchecked \"Awesome\"\n"); 56 | else 57 | print ("You checked \"Awesome\"\n"); 58 | } 59 | 60 | void state_cb (SimpleAction action, Variant? parameter) { 61 | print ("state is set to %s\n", parameter.get_string(null)); 62 | action.set_state (parameter); 63 | } 64 | 65 | /* Create the application actions. */ 66 | const ActionEntry[] actions = { 67 | { "new", new_cb }, 68 | { "quit", quit_cb }, 69 | { "awesome", awesome_cb, null, "false" }, 70 | { "state", state_cb, "s", "'off'" } 71 | }; 72 | 73 | protected override void startup () { 74 | base.startup (); 75 | 76 | /* Setup application actions. */ 77 | this.add_action_entries (actions, this); 78 | 79 | /* Setup menubar and app_menu. */ 80 | /* Get the UI file. */ 81 | var builder = new Gtk.Builder (); 82 | try { 83 | builder.add_from_resource ("/menus/menubar.ui"); 84 | } catch (Error e) { 85 | error ("Unable to load file: %s", e.message); 86 | } 87 | 88 | /* Get the menubar from the builder. */ 89 | this.menubar = builder.get_object ("menubar") as MenuModel; 90 | 91 | /* Get the app_menu from the builder. */ 92 | this.app_menu = builder.get_object ("appmenu") as MenuModel; 93 | } 94 | } 95 | 96 | /* main creates and runs the application. */ 97 | public int main (string[] args) { 98 | return new MyApplication ().run (args); 99 | } 100 | -------------------------------------------------------------------------------- /menubar/menubar.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | File 6 |
7 | 8 | New 9 | app.new 10 | 11 | 12 | Quit 13 | app.quit 14 | 15 |
16 |
17 | 18 | Edit 19 |
20 | 21 | Copy 22 | win.copy 23 | 24 | 25 | Paste 26 | win.paste 27 | 28 |
29 |
30 | 31 | Choices 32 | 33 | Shapes 34 |
35 | 36 | Line 37 | win.shape 38 | line 39 | 40 | 41 | Triangle 42 | win.shape 43 | triangle 44 | 45 | 46 | Square 47 | win.shape 48 | square 49 | 50 | 51 | Polygon 52 | win.shape 53 | polygon 54 | 55 | 56 | Circle 57 | win.shape 58 | circle 59 | 60 |
61 |
62 |
63 | 64 | On 65 | app.state 66 | on 67 | 68 | 69 | Off 70 | app.state 71 | off 72 | 73 |
74 |
75 | 76 | Awesome 77 | app.awesome 78 | 79 |
80 |
81 | 82 | Help 83 |
84 | 85 | About 86 | win.about 87 | 88 |
89 |
90 |
91 | 92 |
93 | 94 | New 95 | app.new 96 | 97 | 98 | Quit 99 | app.quit 100 | 101 |
102 |
103 |
104 | -------------------------------------------------------------------------------- /treeview.vala: -------------------------------------------------------------------------------- 1 | using Gtk; 2 | 3 | public class TreeViewSample : Window { 4 | 5 | private Popover pop; 6 | 7 | public TreeViewSample () { 8 | this.title = "TreeView Sample"; 9 | set_default_size (250, 100); 10 | var view = new TreeView (); 11 | setup_treeview (view); 12 | add (view); 13 | this.destroy.connect (Gtk.main_quit); 14 | } 15 | 16 | private void setup_treeview (TreeView view) { 17 | 18 | /* 19 | * Use ListStore to hold accountname, accounttype, balance and 20 | * color attribute. For more info on how TreeView works take a 21 | * look at the GTK+ API. 22 | */ 23 | 24 | var listmodel = new Gtk.ListStore (4, typeof (string), typeof (string), 25 | typeof (string), typeof (string)); 26 | view.set_model (listmodel); 27 | 28 | var grid = new Gtk.Grid (); 29 | grid.set_column_spacing (10); 30 | grid.set_row_spacing (10); 31 | grid.set_margin_top (10); 32 | grid.set_margin_end (10); 33 | grid.set_margin_bottom (10); 34 | grid.set_margin_start (10); 35 | 36 | view.insert_column_with_attributes (-1, "Account Name", new CellRendererText (), "text", 0); 37 | var column = view.get_column(0); 38 | column.set_reorderable(true); 39 | column.set_resizable(true); 40 | 41 | var label = new Gtk.Label (column.title); 42 | grid.attach (label, 0, 0); 43 | var widget = new Gtk.Switch (); 44 | widget.set_active(true); 45 | grid.attach (widget, 1, 0); 46 | { 47 | var c = column; 48 | widget.state_set.connect((status) => { 49 | print("visible"); 50 | c.set_visible(!c.visible); 51 | return true; 52 | }); 53 | } 54 | 55 | view.insert_column_with_attributes (-1, "Type", new CellRendererText (), "text", 1); 56 | column = view.get_column(1); 57 | column.set_reorderable(true); 58 | column.set_resizable(true); 59 | 60 | label = new Gtk.Label (column.title); 61 | grid.attach (label, 0, 1); 62 | widget = new Gtk.Switch (); 63 | widget.set_active(true); 64 | grid.attach (widget, 1, 1); 65 | { 66 | var c = column; 67 | widget.state_set.connect((status) => { 68 | print("visible"); 69 | c.set_visible(!c.visible); 70 | return true; 71 | }); 72 | } 73 | 74 | var cell = new CellRendererText (); 75 | cell.set ("foreground_set", true); 76 | view.insert_column_with_attributes (-1, "Balance", cell, "text", 2, "foreground", 3); 77 | column = view.get_column(2); 78 | column.set_reorderable(true); 79 | column.set_resizable(true); 80 | 81 | label = new Gtk.Label (column.title); 82 | grid.attach (label, 0, 2); 83 | widget = new Gtk.Switch (); 84 | widget.set_active(true); 85 | grid.attach (widget, 1, 2); 86 | { 87 | var c = column; 88 | widget.state_set.connect((status) => { 89 | print("visible"); 90 | c.set_visible(!c.visible); 91 | return true; 92 | }); 93 | } 94 | 95 | grid.show_all (); 96 | 97 | pop = new Gtk.Popover (view); 98 | pop.add (grid); 99 | 100 | view.button_release_event.connect((w, e) => { 101 | var rect = Gdk.Rectangle(); 102 | rect.x = (int)e.x; 103 | rect.y = (int)e.y; 104 | rect.width = rect.height = 1; 105 | pop.set_relative_to(view); 106 | pop.set_pointing_to(rect); 107 | pop.show_all(); 108 | pop.set_visible (true); 109 | return true; 110 | }); 111 | 112 | TreeIter iter; 113 | listmodel.append (out iter); 114 | listmodel.set (iter, 0, "My Visacard", 1, "card", 2, "102,10", 3, "red"); 115 | 116 | listmodel.append (out iter); 117 | listmodel.set (iter, 0, "My Mastercard", 1, "card", 2, "10,20", 3, "red"); 118 | } 119 | 120 | public static int main (string[] args) { 121 | Gtk.init (ref args); 122 | 123 | var sample = new TreeViewSample (); 124 | sample.show_all (); 125 | Gtk.main (); 126 | 127 | return 0; 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /texteditor.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * TextView is a Gtk widget used to display text replicating the same appearance 3 | * of many text editors. 4 | * This is not a minimal example, is it a fairly complex example 5 | * Compile using: 6 | * valac texteditor.vala --pkg gtk+-3.0 --pkg gio-2.0 7 | * 8 | * TextView documentation https://valadoc.org/gtk+-3.0/Gtk.TextView.html 9 | * Based on the example of the same page. 10 | * FileChooser documentation https://valadoc.org/gtk+-3.0/Gtk.FileChooser.html 11 | * GLib.File part of gio-2.0 https://valadoc.org/gtk+-3.0/Gtk.FileChooser.html 12 | * 13 | * Author: Geronimo Bareiro https://github.com/gerito1 14 | */ 15 | 16 | public class TextEditor : Gtk.Window { 17 | private const string TITLE = "My Text Editor"; 18 | 19 | 20 | private Gtk.TextView text_view; 21 | private Gtk.MenuBar menu_bar; 22 | private Gtk.MenuItem item_open; 23 | private Gtk.MenuItem item_save; 24 | private Gtk.MenuItem item_quit; 25 | private File file; 26 | 27 | public TextEditor () {} 28 | 29 | /* Using GObject-Style construction 30 | * See https://chebizarro.gitbooks.io/the-vala-tutorial/content/gobject-style-construction.html 31 | */ 32 | construct { 33 | title = TextEditor.TITLE; 34 | set_default_size (800, 600); 35 | window_position = Gtk.WindowPosition.CENTER; 36 | 37 | file = null; 38 | menu_bar = new Gtk.MenuBar (); 39 | 40 | Gtk.MenuItem item_file = new Gtk.MenuItem.with_label ("File"); 41 | menu_bar.add (item_file); 42 | 43 | Gtk.Menu file_menu = new Gtk.Menu (); 44 | item_file.set_submenu (file_menu); 45 | 46 | item_open = new Gtk.MenuItem.with_label ("Open"); 47 | file_menu.add (item_open); 48 | 49 | item_save = new Gtk.MenuItem.with_label ("Save"); 50 | file_menu.add (item_save); 51 | 52 | item_quit = new Gtk.MenuItem.with_label ("Quit"); 53 | file_menu.add (item_quit); 54 | 55 | text_view = new Gtk.TextView (); 56 | text_view.set_wrap_mode (Gtk.WrapMode.WORD); 57 | text_view.buffer.text = ""; 58 | 59 | var scrolled_window = new Gtk.ScrolledWindow (null, null); 60 | scrolled_window.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC); 61 | scrolled_window.add (text_view); 62 | scrolled_window.hexpand = true; 63 | scrolled_window.vexpand = true; 64 | 65 | var grid = new Gtk.Grid (); 66 | grid.attach (menu_bar, 0, 0, 1, 1); 67 | grid.attach (scrolled_window, 0, 1, 1, 1); 68 | add (grid as Gtk.Widget); 69 | show_all (); 70 | 71 | connect_signals (); 72 | } 73 | 74 | private void connect_signals () { 75 | destroy.connect (Gtk.main_quit); 76 | item_open.activate.connect (() => { 77 | Gtk.FileChooserDialog chooser = new Gtk.FileChooserDialog ( 78 | "Select a file to edit", this, Gtk.FileChooserAction.OPEN, 79 | "_Cancel", 80 | Gtk.ResponseType.CANCEL, 81 | "_Open", 82 | Gtk.ResponseType.ACCEPT); 83 | chooser.set_select_multiple (false); 84 | chooser.run (); 85 | chooser.close (); 86 | if (chooser.get_file () != null) { 87 | file = chooser.get_file (); 88 | 89 | try { 90 | uint8[] contents; 91 | string etag_out; 92 | file.load_contents (null, out contents, out etag_out); 93 | text_view.buffer.text = (string) contents; 94 | } catch (Error e) { 95 | stdout.printf ("Error: %s\n", e.message); 96 | } 97 | } 98 | }); 99 | 100 | item_save.activate.connect(()=> { 101 | if (file != null) { 102 | try { 103 | file.replace_contents (text_view.buffer.text.data, null, false, FileCreateFlags.NONE, null); 104 | } catch (Error e) { 105 | stdout.printf ("Error: %s\n", e.message); 106 | } 107 | } 108 | }); 109 | item_quit.activate.connect (Gtk.main_quit); 110 | } 111 | 112 | public static int main (string[] args) { 113 | Gtk.init (ref args); 114 | 115 | var my_editor = new TextEditor (); 116 | my_editor.show_all (); 117 | 118 | Gtk.main (); 119 | 120 | return 0; 121 | } 122 | } 123 | 124 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Examples for Vala Gtk 2 | 3 | This repository contains some basic examples for using Vala and GTK+ 3. 4 | Each example tries to showcase some use of the widget in an easy-to-understand form. 5 | 6 | ## Usage 7 | 8 | The examples are distributed as source code, if you want to run the examples you 9 | need to build them. 10 | 11 | You can build them using a build system, or manually. 12 | 13 | ## Using a buildsystem 14 | 15 | With meson and ninja you can automatically build all the examples. 16 | 17 | In the directory of the repo run 18 | 19 | ``` 20 | meson build 21 | ninja -C build 22 | ``` 23 | 24 | If built succesfully, every executable should be inside of the `./build` 25 | directory 26 | 27 | ``` 28 | ./build/box 29 | ./build/sourceview 30 | ``` 31 | 32 | You can modify each example and play around for testing purposes. 33 | Just run ninja again in the repo directory, and ninja will rebuild only 34 | the examples that were modified. 35 | 36 | ``` 37 | ninja -C build 38 | ``` 39 | 40 | 41 | Make sure you have installed the vala compiler, meson and ninja. 42 | 43 | 44 | In Debian/Ubuntu or derivatives 45 | ``` 46 | sudo apt-get install valac meson ninja 47 | ``` 48 | 49 | In Arch, or Arch based systems 50 | ``` 51 | sudo pacman -S vala meson ninja 52 | ``` 53 | 54 | 55 | ## Manually build each example 56 | 57 | 58 | ### Install the vala compiler 59 | 60 | In Debian/Ubuntu or derivatives 61 | 62 | ``` 63 | sudo apt-get install valac 64 | ``` 65 | 66 | In Arch, or Arch based systems 67 | 68 | ``` 69 | sudo pacman -S vala 70 | ``` 71 | 72 | ### To check the version installed in your system 73 | 74 | ``` 75 | vala --version 76 | ``` 77 | 78 | ### Compile the example 79 | 80 | Every example is a standalone file `filename.vala`, you can compile a file like this 81 | 82 | ``` 83 | valac filename.vala --pkg gtk+-3.0 84 | ``` 85 | 86 | This will generate an executable `filename` in the directory of the repo 87 | You can then run the example 88 | 89 | ``` 90 | ./filename 91 | ``` 92 | 93 | Keep in mind that some examples could need other packages besides gtk. Every additional 94 | package can be included in the compiler with directive `--pkg` in the form 95 | `--pkg package-name` for example: 96 | 97 | ``` 98 | valac webkit2.vala --pkg gtk+-3.0 --pkg webkit2gtk-4.0 99 | ``` 100 | 101 | Also it is posible that you will need to install said package in your system. 102 | Check your system documentation. 103 | 104 | ## Aditional Resources 105 | [The Vala Online Documentation](https://valadoc.org) 106 | 107 | [The official GNOME Vala Project page](https://wiki.gnome.org/Projects/Vala) 108 | 109 | [The Vala Tutorial by GNOME](https://wiki.gnome.org/Projects/Vala/Tutorial) 110 | 111 | [The vala tutorial by Chebizarro](https://www.gitbook.com/book/chebizarro/the-vala-tutorial/details) 112 | A gitbook with a great tutorial for vala. Can be downloaded as pdf. 113 | 114 | [Vala for C# Programmers by GNOME](https://wiki.gnome.org/Projects/Vala/ValaForCSharpProgrammers) 115 | 116 | [Vala for Java Programmers by GNOME](https://wiki.gnome.org/Projects/Vala/ValaForJavaProgrammers) 117 | 118 | #### elementery OS has a nice amount of resources online 119 | 120 | [elementary OS Developers' Reference](https://elementary.io/docs/code/reference) 121 | It contains guidelines for coding style, reporting bugs and proposing changes. You should take a look. 122 | 123 | [elementary OS' Getting Started guide](https://elementary.io/docs/code/getting-started) 124 | A nice guide with little code examples, guides to set up your environment. Guides 125 | to Bazaar (another source version control software similar to git). Guides to 126 | Launchpad (a repositories for code and binaries used in many Ubuntu derivatives 127 | and by Canonical for Ubuntu). How to create deb packages and more. 128 | 129 | [elementary OS' Human Interfaces Guidelines](https://elementary.io/docs/human-interface-guidelines) 130 | A great resource to learn a little about design the GUI for your app. 131 | 132 | #### Videos 133 | 134 | [Gtk+ Kick-Start Tutorial for Vala by Alberto Ruiz](https://vimeo.com/9617309) 135 | 136 | [Vala Language Introduction by Andre Masella](https://www.youtube.com/watch?v=Eqa38B0GV6U) 137 | 138 | ## Other Notes 139 | A tutorial to further explain the use of each widget may be written in the future, possibly when GTK+ 4 is released. ;) 140 | 141 | ## Authors 142 | #### Original Author 143 | 144 | [Andrew Steele](https://github.com/steeleyuk) 145 | 146 | #### Contributors 147 | 148 | [Geronimo Bareiro](https://github.com/gerito1) 149 | 150 | [Douglas Chidester](https://github.com/objectDisorientedProgrammer) 151 | 152 | [Alberto Fanjul](https://github.com/albfan) 153 | 154 | ## To contribute 155 | 156 | Just submit a pull request, or open an issue. 157 | -------------------------------------------------------------------------------- /webkit2.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * Webkit2gtk is a port of the webkit2 widget to Gtk. With it you can embed a 3 | * browser into your project. 4 | * Compile using: 5 | * valac webkit2.vala --pkg gtk+-3.0 --pkg webkit2gtk-4.0 6 | * 7 | * Based on https://wiki.gnome.org/Projects/Vala/WebKitSample 8 | * WebKit documentation https://valadoc.org/webkit2gtk-4.0/index.htm 9 | * 10 | * Make sure you have installed the package libwebkit2gtk-4.0-dev in debian or 11 | * derivative systems 12 | * $ apt-get install libwebkit2gtk-4.0-dev 13 | * 14 | * Author: Geronimo Bareiro https://github.com/gerito1 15 | */ 16 | 17 | 18 | public class MyWebkitWindow : Gtk.Window { 19 | 20 | private const string BROWSER_TITLE = "My WebKit Browser"; 21 | private const string DEFAULT_PROTOCOL = "http"; 22 | private GLib.Regex protocol_regex; 23 | 24 | private WebKit.WebView web_view; //This is the widget that shows the page 25 | private Gtk.Label status_bar; 26 | private Gtk.Entry url_bar; 27 | private Gtk.ToolButton back_button; 28 | private Gtk.ToolButton forward_button; 29 | private Gtk.ToolButton reload_button; 30 | private Gtk.ToolButton stop_button; 31 | 32 | public MyWebkitWindow () {} 33 | 34 | /* Using GObject-Style construction 35 | * See https://chebizarro.gitbooks.io/the-vala-tutorial/content/gobject-style-construction.html 36 | */ 37 | construct { 38 | title = MyWebkitWindow.BROWSER_TITLE; 39 | set_default_size (800, 600); 40 | try { 41 | protocol_regex = new Regex (".*://.*"); 42 | } catch (RegexError e) { 43 | critical ("%s", e.message); 44 | } 45 | 46 | var toolbar = new Gtk.Toolbar (); 47 | var back_image = new Gtk.Image.from_icon_name ("go-previous", Gtk.IconSize.SMALL_TOOLBAR); 48 | back_button = new Gtk.ToolButton (back_image, null); 49 | var forward_image = new Gtk.Image.from_icon_name ("go-next", Gtk.IconSize.SMALL_TOOLBAR); 50 | forward_button = new Gtk.ToolButton (forward_image, null); 51 | var reload_image = new Gtk.Image.from_icon_name ("view-refresh", Gtk.IconSize.SMALL_TOOLBAR); 52 | reload_button = new Gtk.ToolButton (reload_image, null); 53 | var stop_image = new Gtk.Image.from_icon_name ("process-stop", Gtk.IconSize.SMALL_TOOLBAR); 54 | stop_button = new Gtk.ToolButton (stop_image, null); 55 | toolbar.add (back_button); 56 | toolbar.add (forward_button); 57 | toolbar.add (reload_button); 58 | toolbar.add (stop_button); 59 | 60 | url_bar = new Gtk.Entry (); 61 | web_view = new WebKit.WebView (); 62 | var scrolled_window = new Gtk.ScrolledWindow (null, null); //The WebView doesn't contain scroll bars by its own. 63 | scrolled_window.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC); 64 | scrolled_window.add (web_view); 65 | scrolled_window.hexpand = true; 66 | scrolled_window.vexpand = true; 67 | status_bar = new Gtk.Label ("Welcome to %s".printf(BROWSER_TITLE)); 68 | status_bar.xalign = 0; 69 | 70 | var grid = new Gtk.Grid (); 71 | grid.attach (toolbar, 0, 0, 1, 1); 72 | grid.attach (url_bar, 0, 1, 1, 1); 73 | grid.attach (scrolled_window, 0, 2, 1, 1); 74 | grid.attach (status_bar, 0, 3, 1, 1); 75 | add (grid as Gtk.Widget); 76 | show_all (); 77 | 78 | connect_signals (); 79 | } 80 | 81 | private void connect_signals () { 82 | destroy.connect (Gtk.main_quit); 83 | 84 | url_bar.activate.connect (() => { 85 | open_url (url_bar.text); 86 | }); 87 | 88 | web_view.load_changed.connect ((load_event) => { 89 | url_bar.text = web_view.get_uri (); 90 | if (web_view.title == null) { 91 | title = MyWebkitWindow.BROWSER_TITLE; 92 | } else { 93 | title = "%s - %s".printf (web_view.title, MyWebkitWindow.BROWSER_TITLE); 94 | } 95 | 96 | back_button.sensitive = web_view.can_go_back (); 97 | forward_button.sensitive = web_view.can_go_forward (); 98 | if (load_event == WebKit.LoadEvent.FINISHED) { 99 | stop_button.sensitive = false; 100 | } else { 101 | stop_button.sensitive = true; 102 | } 103 | }); 104 | 105 | back_button.clicked.connect (web_view.go_back); 106 | forward_button.clicked.connect (web_view.go_forward); 107 | reload_button.clicked.connect (web_view.reload); 108 | stop_button.clicked.connect (web_view.stop_loading); 109 | } 110 | 111 | void open_url (string page_url) { 112 | var url = page_url; 113 | if (!protocol_regex.match (page_url)) { 114 | url = "%s://%s".printf (MyWebkitWindow.DEFAULT_PROTOCOL, page_url); 115 | } 116 | web_view.load_uri (url); 117 | } 118 | 119 | public static int main (string[] args) { 120 | Gtk.init (ref args); 121 | 122 | var my_webkit_window = new MyWebkitWindow (); 123 | my_webkit_window.open_url ("https://valadoc.org/webkit2gtk-4.0/WebKit.html"); 124 | 125 | Gtk.main (); 126 | 127 | return 0; 128 | } 129 | 130 | } 131 | -------------------------------------------------------------------------------- /meson.build: -------------------------------------------------------------------------------- 1 | project('vala-gtk-examples', ['c', 'vala'], 2 | version : '0.1', 3 | default_options : ['warning_level=3']) 4 | 5 | gtk_dep = dependency('gtk+-3.0') 6 | 7 | executable('actionbar', 8 | 'actionbar.vala', 9 | dependencies : [gtk_dep], 10 | install : true) 11 | 12 | executable('box', 13 | 'box.vala', 14 | dependencies : [gtk_dep], 15 | install : true) 16 | 17 | executable('buttonbox', 18 | 'buttonbox.vala', 19 | dependencies : [gtk_dep], 20 | install : true) 21 | 22 | executable('button', 23 | 'button.vala', 24 | dependencies : [gtk_dep], 25 | install : true) 26 | 27 | executable('checkbutton', 28 | 'checkbutton.vala', 29 | dependencies : [gtk_dep], 30 | install : true) 31 | 32 | executable('comboboxtext', 33 | 'comboboxtext.vala', 34 | dependencies : [gtk_dep], 35 | install : true) 36 | 37 | executable('combobox', 38 | 'combobox.vala', 39 | dependencies : [gtk_dep], 40 | install : true) 41 | 42 | executable('entrybuffer', 43 | 'entrybuffer.vala', 44 | dependencies : [gtk_dep], 45 | install : true) 46 | 47 | executable('entrycompletion', 48 | 'entrycompletion.vala', 49 | dependencies : [gtk_dep], 50 | install : true) 51 | 52 | executable('entry', 53 | 'entry.vala', 54 | dependencies : [gtk_dep], 55 | install : true) 56 | 57 | executable('filechooserbutton', 58 | 'filechooserbutton.vala', 59 | dependencies : [gtk_dep], 60 | install : true) 61 | 62 | executable('flowbox', 63 | 'flowbox.vala', 64 | dependencies : [gtk_dep], 65 | install : true) 66 | 67 | executable('fontbutton', 68 | 'fontbutton.vala', 69 | dependencies : [gtk_dep], 70 | install : true) 71 | 72 | executable('grid', 73 | 'grid.vala', 74 | dependencies : [gtk_dep], 75 | install : true) 76 | 77 | executable('label', 78 | 'label.vala', 79 | dependencies : [gtk_dep], 80 | install : true) 81 | 82 | executable('listbox', 83 | 'listbox.vala', 84 | dependencies : [gtk_dep], 85 | install : true) 86 | 87 | executable('notebook', 88 | 'notebook.vala', 89 | dependencies : [gtk_dep], 90 | install : true) 91 | 92 | executable('paned', 93 | 'paned.vala', 94 | dependencies : [gtk_dep], 95 | install : true) 96 | 97 | executable('radiobutton', 98 | 'radiobutton.vala', 99 | dependencies : [gtk_dep], 100 | install : true) 101 | 102 | executable('scale', 103 | 'scale.vala', 104 | dependencies : [gtk_dep], 105 | install : true) 106 | 107 | executable('searchentry', 108 | 'searchentry.vala', 109 | dependencies : [gtk_dep], 110 | install : true) 111 | 112 | executable('separator', 113 | 'separator.vala', 114 | dependencies : [gtk_dep], 115 | install : true) 116 | 117 | 118 | gtksourceview_dep = dependency('gtksourceview-4', required : false) 119 | if gtksourceview_dep.found() 120 | executable('sourceview', 121 | 'sourceview.vala', 122 | dependencies : [gtk_dep, gtksourceview_dep], 123 | install : true) 124 | else 125 | warning ('[-] Skipping sourceview.vala') 126 | warning ('\tIt requires gtksourceview-4, but is not available.' ) 127 | warning ('\tCheck your system documentation to install it.') 128 | endif 129 | 130 | executable('spinbutton', 131 | 'spinbutton.vala', 132 | dependencies : [gtk_dep], 133 | install : true) 134 | 135 | executable('spinner', 136 | 'spinner.vala', 137 | dependencies : [gtk_dep], 138 | install : true) 139 | 140 | executable('stacksidebar', 141 | 'stacksidebar.vala', 142 | dependencies : [gtk_dep], 143 | install : true) 144 | 145 | executable('stackswitcher', 146 | 'stackswitcher.vala', 147 | dependencies : [gtk_dep], 148 | install : true) 149 | 150 | executable('stack', 151 | 'stack.vala', 152 | dependencies : [gtk_dep], 153 | install : true) 154 | 155 | executable('texteditor', 156 | 'texteditor.vala', 157 | dependencies : [gtk_dep], 158 | install : true) 159 | 160 | executable('togglebutton', 161 | 'togglebutton.vala', 162 | dependencies : [gtk_dep], 163 | install : true) 164 | 165 | 166 | gnome = import('gnome') 167 | 168 | menu_resources = gnome.compile_resources( 169 | 'menu-resources', 'menubar/gresources.xml', 170 | source_dir: 'menubar', 171 | ) 172 | 173 | executable('menubar', 174 | [ 175 | 'menubar.vala', 176 | menu_resources 177 | ], 178 | dependencies : [gtk_dep], 179 | install : true) 180 | 181 | executable('popover', 182 | 'popover.vala', 183 | dependencies : [gtk_dep], 184 | install : true) 185 | 186 | executable('treeview', 187 | 'treeview.vala', 188 | dependencies : [gtk_dep], 189 | install : true) 190 | 191 | webkit2gtk_dep = dependency('webkit2gtk-4.0', required : false) 192 | if webkit2gtk_dep.found() 193 | executable('webkit2', 194 | 'webkit2.vala', 195 | dependencies : [gtk_dep, webkit2gtk_dep], 196 | install : true) 197 | else 198 | warning ('[-] Skipping webkit2.vala') 199 | warning ('\tIt requires webkit2gtk-4.0, but is not available.') 200 | warning ('\tCheck your system documentation to install it.') 201 | endif 202 | 203 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | CC0 1.0 Universal 2 | 3 | Statement of Purpose 4 | 5 | The laws of most jurisdictions throughout the world automatically confer 6 | exclusive Copyright and Related Rights (defined below) upon the creator and 7 | subsequent owner(s) (each and all, an "owner") of an original work of 8 | authorship and/or a database (each, a "Work"). 9 | 10 | Certain owners wish to permanently relinquish those rights to a Work for the 11 | purpose of contributing to a commons of creative, cultural and scientific 12 | works ("Commons") that the public can reliably and without fear of later 13 | claims of infringement build upon, modify, incorporate in other works, reuse 14 | and redistribute as freely as possible in any form whatsoever and for any 15 | purposes, including without limitation commercial purposes. These owners may 16 | contribute to the Commons to promote the ideal of a free culture and the 17 | further production of creative, cultural and scientific works, or to gain 18 | reputation or greater distribution for their Work in part through the use and 19 | efforts of others. 20 | 21 | For these and/or other purposes and motivations, and without any expectation 22 | of additional consideration or compensation, the person associating CC0 with a 23 | Work (the "Affirmer"), to the extent that he or she is an owner of Copyright 24 | and Related Rights in the Work, voluntarily elects to apply CC0 to the Work 25 | and publicly distribute the Work under its terms, with knowledge of his or her 26 | Copyright and Related Rights in the Work and the meaning and intended legal 27 | effect of CC0 on those rights. 28 | 29 | 1. Copyright and Related Rights. A Work made available under CC0 may be 30 | protected by copyright and related or neighboring rights ("Copyright and 31 | Related Rights"). Copyright and Related Rights include, but are not limited 32 | to, the following: 33 | 34 | i. the right to reproduce, adapt, distribute, perform, display, communicate, 35 | and translate a Work; 36 | 37 | ii. moral rights retained by the original author(s) and/or performer(s); 38 | 39 | iii. publicity and privacy rights pertaining to a person's image or likeness 40 | depicted in a Work; 41 | 42 | iv. rights protecting against unfair competition in regards to a Work, 43 | subject to the limitations in paragraph 4(a), below; 44 | 45 | v. rights protecting the extraction, dissemination, use and reuse of data in 46 | a Work; 47 | 48 | vi. database rights (such as those arising under Directive 96/9/EC of the 49 | European Parliament and of the Council of 11 March 1996 on the legal 50 | protection of databases, and under any national implementation thereof, 51 | including any amended or successor version of such directive); and 52 | 53 | vii. other similar, equivalent or corresponding rights throughout the world 54 | based on applicable law or treaty, and any national implementations thereof. 55 | 56 | 2. Waiver. To the greatest extent permitted by, but not in contravention of, 57 | applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and 58 | unconditionally waives, abandons, and surrenders all of Affirmer's Copyright 59 | and Related Rights and associated claims and causes of action, whether now 60 | known or unknown (including existing as well as future claims and causes of 61 | action), in the Work (i) in all territories worldwide, (ii) for the maximum 62 | duration provided by applicable law or treaty (including future time 63 | extensions), (iii) in any current or future medium and for any number of 64 | copies, and (iv) for any purpose whatsoever, including without limitation 65 | commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes 66 | the Waiver for the benefit of each member of the public at large and to the 67 | detriment of Affirmer's heirs and successors, fully intending that such Waiver 68 | shall not be subject to revocation, rescission, cancellation, termination, or 69 | any other legal or equitable action to disrupt the quiet enjoyment of the Work 70 | by the public as contemplated by Affirmer's express Statement of Purpose. 71 | 72 | 3. Public License Fallback. Should any part of the Waiver for any reason be 73 | judged legally invalid or ineffective under applicable law, then the Waiver 74 | shall be preserved to the maximum extent permitted taking into account 75 | Affirmer's express Statement of Purpose. In addition, to the extent the Waiver 76 | is so judged Affirmer hereby grants to each affected person a royalty-free, 77 | non transferable, non sublicensable, non exclusive, irrevocable and 78 | unconditional license to exercise Affirmer's Copyright and Related Rights in 79 | the Work (i) in all territories worldwide, (ii) for the maximum duration 80 | provided by applicable law or treaty (including future time extensions), (iii) 81 | in any current or future medium and for any number of copies, and (iv) for any 82 | purpose whatsoever, including without limitation commercial, advertising or 83 | promotional purposes (the "License"). The License shall be deemed effective as 84 | of the date CC0 was applied by Affirmer to the Work. Should any part of the 85 | License for any reason be judged legally invalid or ineffective under 86 | applicable law, such partial invalidity or ineffectiveness shall not 87 | invalidate the remainder of the License, and in such case Affirmer hereby 88 | affirms that he or she will not (i) exercise any of his or her remaining 89 | Copyright and Related Rights in the Work or (ii) assert any associated claims 90 | and causes of action with respect to the Work, in either case contrary to 91 | Affirmer's express Statement of Purpose. 92 | 93 | 4. Limitations and Disclaimers. 94 | 95 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 96 | surrendered, licensed or otherwise affected by this document. 97 | 98 | b. Affirmer offers the Work as-is and makes no representations or warranties 99 | of any kind concerning the Work, express, implied, statutory or otherwise, 100 | including without limitation warranties of title, merchantability, fitness 101 | for a particular purpose, non infringement, or the absence of latent or 102 | other defects, accuracy, or the present or absence of errors, whether or not 103 | discoverable, all to the greatest extent permissible under applicable law. 104 | 105 | c. Affirmer disclaims responsibility for clearing rights of other persons 106 | that may apply to the Work or any use thereof, including without limitation 107 | any person's Copyright and Related Rights in the Work. Further, Affirmer 108 | disclaims responsibility for obtaining any necessary consents, permissions 109 | or other rights required for any use of the Work. 110 | 111 | d. Affirmer understands and acknowledges that Creative Commons is not a 112 | party to this document and has no duty or obligation with respect to this 113 | CC0 or use of the Work. 114 | 115 | For more information, please see 116 | 117 | 118 | -------------------------------------------------------------------------------- /sourceview.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * SourceView is a Gtk widget used to display text similarly to TextView widget 3 | * but also is support syntax highlights as many code editors. 4 | * 5 | * Compile using: 6 | * valac sourceview.vala --pkg gtk+-3.0 --pkg gtksourceview-3.0 7 | * SourceView Documentation https://valadoc.org/gtksourceview-3.0/Gtk.SourceView.html 8 | * 9 | * Author: Geronimo Bareiro https://github.com/gerito1 10 | */ 11 | 12 | public class SourceEditor : Gtk.Window { 13 | private const string TITLE = "My Source Code Editor"; 14 | 15 | private Gtk.SourceView source_view; 16 | private Gtk.SourceLanguageManager language_manager; 17 | private Gtk.MenuBar menu_bar; 18 | private Gtk.MenuItem item_open; 19 | private Gtk.MenuItem item_save; 20 | private Gtk.MenuItem item_quit; 21 | private Gtk.SourceFile file; 22 | 23 | public SourceEditor () {} 24 | 25 | /* Using GObject-Style construction 26 | * See https://chebizarro.gitbooks.io/the-vala-tutorial/content/gobject-style-construction.html 27 | */ 28 | construct { 29 | title = SourceEditor.TITLE; 30 | set_default_size (800, 600); 31 | window_position = Gtk.WindowPosition.CENTER; 32 | 33 | file = null; 34 | menu_bar = new Gtk.MenuBar (); 35 | 36 | Gtk.MenuItem item_file = new Gtk.MenuItem.with_label ("File"); 37 | menu_bar.add (item_file); 38 | 39 | Gtk.Menu file_menu = new Gtk.Menu (); 40 | item_file.set_submenu (file_menu); 41 | 42 | item_open = new Gtk.MenuItem.with_label ("Open"); 43 | file_menu.add (item_open); 44 | 45 | item_save = new Gtk.MenuItem.with_label ("Save"); 46 | file_menu.add (item_save); 47 | 48 | item_quit = new Gtk.MenuItem.with_label ("Quit"); 49 | file_menu.add (item_quit); 50 | 51 | source_view = new Gtk.SourceView (); 52 | source_view.set_wrap_mode (Gtk.WrapMode.WORD); 53 | source_view.buffer.text = ""; 54 | language_manager = Gtk.SourceLanguageManager.get_default (); 55 | 56 | var scrolled_window = new Gtk.ScrolledWindow (null, null); 57 | scrolled_window.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC); 58 | scrolled_window.add (source_view); 59 | scrolled_window.hexpand = true; 60 | scrolled_window.vexpand = true; 61 | 62 | var grid = new Gtk.Grid (); 63 | grid.attach (menu_bar, 0, 0, 1, 1); 64 | grid.attach (scrolled_window, 0, 1, 1, 1); 65 | add (grid as Gtk.Widget); 66 | show_all (); 67 | 68 | connect_signals (); 69 | } 70 | 71 | private void connect_signals () { 72 | destroy.connect (Gtk.main_quit); 73 | 74 | /* 75 | * Set the callbacks for the items in the File Menu 76 | */ 77 | item_open.activate.connect (on_open); 78 | item_save.activate.connect (on_save); 79 | item_quit.activate.connect (Gtk.main_quit); 80 | 81 | /* 82 | *Populate the contextual menu after the right click. We need to select 83 | *a language for our sourceview. 84 | */ 85 | source_view.populate_popup.connect (on_populate_menu); 86 | 87 | } 88 | 89 | /* 90 | * We will select a file using FileChooser and load it to the editor. 91 | */ 92 | void on_open () { 93 | Gtk.FileChooserDialog chooser = new Gtk.FileChooserDialog ( 94 | "Select a file to edit", this, Gtk.FileChooserAction.OPEN, 95 | "_Cancel", 96 | Gtk.ResponseType.CANCEL, 97 | "_Open", 98 | Gtk.ResponseType.ACCEPT); 99 | chooser.set_select_multiple (false); 100 | chooser.run (); 101 | chooser.close (); 102 | 103 | if (chooser.get_file () != null) { 104 | file = new Gtk.SourceFile (); 105 | file.location = chooser.get_file (); 106 | var file_loader = new Gtk.SourceFileLoader (source_view.buffer as Gtk.SourceBuffer, file); 107 | try { 108 | file_loader.load_async.begin (Priority.DEFAULT, null, null); 109 | } catch (Error e) { 110 | stdout.printf ("Error: %s\n", e.message); 111 | } 112 | } 113 | } 114 | 115 | /* 116 | *This will save the file to the location we had defined before. 117 | *It doesn't consider the case where you didn't "select" a file before. 118 | */ 119 | void on_save () { 120 | if (file != null && !file.is_readonly () ) { 121 | var file_saver = new Gtk.SourceFileSaver (source_view.buffer as Gtk.SourceBuffer, file); 122 | try { 123 | file_saver.save_async.begin (Priority.DEFAULT, null, null); 124 | } catch (Error e) { 125 | stdout.printf ("Error: %s\n", e.message); 126 | } 127 | } 128 | } 129 | 130 | /* 131 | * Create the submenu to select language for our source view, using the right-click 132 | * contextual menu 133 | */ 134 | void on_populate_menu (Gtk.Menu menu) { 135 | var language_menu = new Gtk.MenuItem (); 136 | language_menu.set_label ("Language"); 137 | 138 | var submenu = new Gtk.Menu (); 139 | language_menu.set_submenu (submenu); 140 | 141 | // Create the list of items 142 | unowned SList group = null; 143 | Gtk.RadioMenuItem? item = null; 144 | 145 | //Add an entry with No Language, or normal. 146 | item = new Gtk.RadioMenuItem (group); 147 | item.set_label ("Normal Text"); 148 | item.toggled.connect (() => { 149 | 150 | //No language, aka normal text edit. 151 | (source_view.buffer as Gtk.SourceBuffer).set_language (null); 152 | }); 153 | 154 | submenu.add (item); 155 | 156 | // Set the Language entries 157 | var ids = language_manager.get_language_ids (); 158 | foreach (var id in ids) { 159 | var lang = language_manager.get_language (id); 160 | group = item.get_group (); 161 | item = new Gtk.RadioMenuItem (group); 162 | item.set_label (lang.name); 163 | 164 | submenu.add (item); 165 | item.toggled.connect (() => { 166 | (source_view.buffer as Gtk.SourceBuffer).set_language (lang); 167 | }); 168 | 169 | // Active item 170 | if ((source_view.buffer as Gtk.SourceBuffer).language != null && id == (source_view.buffer as Gtk.SourceBuffer).language.id) { 171 | item.active = true; 172 | } 173 | } 174 | 175 | // Add our Language selection menu to the menu provided in the callback 176 | menu.add (language_menu); 177 | menu.show_all (); 178 | } 179 | 180 | public static int main (string[] args) { 181 | Gtk.init (ref args); 182 | 183 | var my_editor = new SourceEditor (); 184 | my_editor.show_all (); 185 | 186 | Gtk.main (); 187 | 188 | return 0; 189 | } 190 | } 191 | 192 | --------------------------------------------------------------------------------