|
Packit |
1470ea |
|
|
Packit |
1470ea |
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="treeview_simple_liststore.vala" xml:lang="cs">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">Jednoduchý Treeview s ListStore (Vala)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="beginner.vala#treeview"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="grid.vala"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="label.vala"/>
|
|
Packit |
1470ea |
<revision version="0.1" date="2012-05-09" status="draft"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<credit type="author copyright">
|
|
Packit |
1470ea |
<name>Tiffany Antopolski</name>
|
|
Packit |
1470ea |
<email its:translate="no">tiffany.antopolski@gmail.com</email>
|
|
Packit |
1470ea |
<years>2012</years>
|
|
Packit |
1470ea |
</credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<desc>Widget, který umí zobrazit libovolnou implementaci TreeModel (seznamy a stromy)</desc>
|
|
Packit |
1470ea |
</info>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<title>Jednoduchý Treeview s ListStore</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/treeview_simple_liststore.png"/>
|
|
Packit |
1470ea |
Tento TreeView zobrazuje jednoduchý ListStore s napojeným signálem "changed" (změna výběru).
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
public class PhoneBookEntry {
|
|
Packit |
1470ea |
public string firstname;
|
|
Packit |
1470ea |
public string lastname;
|
|
Packit |
1470ea |
public string phone;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
public PhoneBookEntry (string f, string l, string p) {
|
|
Packit |
1470ea |
this.firstname = f;
|
|
Packit |
1470ea |
this.lastname = l;
|
|
Packit |
1470ea |
this.phone = p;
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class TreeViewSimpleListStore : Gtk.ApplicationWindow {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Gtk.Label label;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
PhoneBookEntry[] phonebook = {
|
|
Packit |
1470ea |
new PhoneBookEntry ("Jurg", "Billeter", "555-0123"),
|
|
Packit |
1470ea |
new PhoneBookEntry ("Johannes", "Schmid", "555-1234"),
|
|
Packit |
1470ea |
new PhoneBookEntry ("Julita", "Inca", "555-2345"),
|
|
Packit |
1470ea |
new PhoneBookEntry ("Javier", "Jardon", "555-3456"),
|
|
Packit |
1470ea |
new PhoneBookEntry ("Jason", "Clinton", "555-4567"),
|
|
Packit |
1470ea |
new PhoneBookEntry ("Random J.", "Hacker", "555-5678")
|
|
Packit |
1470ea |
};
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
enum Column {
|
|
Packit |
1470ea |
FIRSTNAME,
|
|
Packit |
1470ea |
LASTNAME,
|
|
Packit |
1470ea |
PHONE
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
internal TreeViewSimpleListStore (MyApplication app) {
|
|
Packit |
1470ea |
Object (application: app, title: "My Phone Book");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
this.set_default_size (250, 100);
|
|
Packit |
1470ea |
this.border_width = 10;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
var view = new Gtk.TreeView ();
|
|
Packit |
1470ea |
this.setup_treeview (view);
|
|
Packit |
1470ea |
view.expand = true;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
label = new Gtk.Label ("");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
var grid = new Gtk.Grid ();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
grid.attach (view, 0, 0, 1, 1);
|
|
Packit |
1470ea |
grid.attach (label, 0, 1, 1, 1);
|
|
Packit |
1470ea |
this.add (grid);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
var selection = view.get_selection ();
|
|
Packit |
1470ea |
selection.changed.connect (this.on_changed);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
void setup_treeview (Gtk.TreeView view) {
|
|
Packit |
1470ea |
var listmodel = new Gtk.ListStore (3, typeof (string),
|
|
Packit |
1470ea |
typeof (string),
|
|
Packit |
1470ea |
typeof (string));
|
|
Packit |
1470ea |
view.set_model (listmodel);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
var cell = new Gtk.CellRendererText ();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* "weight" označuje tučnost písma
|
|
Packit |
1470ea |
* 400 je normální
|
|
Packit |
1470ea |
* 700 je tučné
|
|
Packit |
1470ea |
*/
|
|
Packit |
1470ea |
cell.set ("weight_set", true);
|
|
Packit |
1470ea |
cell.set ("weight", 700);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* Sloupce */
|
|
Packit |
1470ea |
view.insert_column_with_attributes (-1, "First Name",
|
|
Packit |
1470ea |
cell, "text",
|
|
Packit |
1470ea |
Column.FIRSTNAME);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
view.insert_column_with_attributes (-1, "Last Name",
|
|
Packit |
1470ea |
new Gtk.CellRendererText (),
|
|
Packit |
1470ea |
"text", Column.LASTNAME);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
view.insert_column_with_attributes (-1, "Phone Number",
|
|
Packit |
1470ea |
new Gtk.CellRendererText (),
|
|
Packit |
1470ea |
"text", Column.PHONE);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* Vloží telefonní seznam do ListStore */
|
|
Packit |
1470ea |
Gtk.TreeIter iter;
|
|
Packit |
1470ea |
for (int i = 0; i < phonebook.length; i++) {
|
|
Packit |
1470ea |
listmodel.append (out iter);
|
|
Packit |
1470ea |
listmodel.set (iter, Column.FIRSTNAME,
|
|
Packit |
1470ea |
phonebook[i].firstname,
|
|
Packit |
1470ea |
Column.LASTNAME, phonebook[i].lastname,
|
|
Packit |
1470ea |
Column.PHONE, phonebook[i].phone);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
void on_changed (Gtk.TreeSelection selection) {
|
|
Packit |
1470ea |
Gtk.TreeModel model;
|
|
Packit |
1470ea |
Gtk.TreeIter iter;
|
|
Packit |
1470ea |
string name;
|
|
Packit |
1470ea |
string lastname;
|
|
Packit |
1470ea |
string phone;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
if (selection.get_selected (out model, out iter)) {
|
|
Packit |
1470ea |
model.get (iter,
|
|
Packit |
1470ea |
Column.FIRSTNAME, out name,
|
|
Packit |
1470ea |
Column.LASTNAME, out lastname,
|
|
Packit |
1470ea |
Column.PHONE, out phone);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
label.set_text ("\n" + name + " " + lastname + " " + phone);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class MyApplication : Gtk.Application {
|
|
Packit |
1470ea |
protected override void activate () {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
/* Vytvoří nové okno a vše v něm zobrazí */
|
|
Packit |
1470ea |
new TreeViewSimpleListStore (this).show_all ();
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
internal MyApplication () {
|
|
Packit |
1470ea |
Object (application_id: "example.liststore.simple.treeview");
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
int main (string[] args) {
|
|
Packit |
1470ea |
return new MyApplication ().run (args);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
V této ukázce se používá následující:
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item><link href="http://www.valadoc.org/gtk+-3.0/Gtk.TreeView.html">Gtk.TreeView</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://www.valadoc.org/gtk+-3.0/Gtk.ListStore.html">Gtk.ListStore</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://www.valadoc.org/gtk+-3.0/Gtk.TreeSelection.html">Gtk.TreeSelection</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</page>
|