1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
module CustomList;
import glib.RandG;
import gobject.ObjectG;
import gobject.Value;
import gtk.TreeIter;
import gtk.TreePath;
import gtk.TreeModelIF;
import gtk.TreeModelT;
import gtkd.Implement;
struct CustomRecord
{
/* data - you can extend this */
string name;
uint yearBorn;
/* admin stuff used by the custom list model */
uint pos; /* pos within the array */
}
enum CustomListColumn
{
Record = 0,
Name,
YearBorn,
NColumns,
}
class CustomList : ObjectG, TreeModelIF
{
uint numRows;
int nColumns;
int stamp;
GType[3] columnTypes;
CustomRecord*[] rows;
mixin ImplementInterface!(GObject, GtkTreeModelIface);
mixin TreeModelT!(GtkTreeModel);
public this()
{
super(getType(), null);
nColumns = columnTypes.length;
columnTypes[0] = GType.POINTER;
columnTypes[1] = GType.STRING;
columnTypes[2] = GType.UINT;
stamp = RandG.randomInt();
}
/*
* tells the rest of the world whether our tree model
* has any special characteristics. In our case,
* we have a list model (instead of a tree), and each
* tree iter is valid as long as the row in question
* exists, as it only contains a pointer to our struct.
*/
override GtkTreeModelFlags getFlags()
{
return (GtkTreeModelFlags.LIST_ONLY | GtkTreeModelFlags.ITERS_PERSIST);
}
/*
* tells the rest of the world how many data
* columns we export via the tree model interface
*/
override int getNColumns()
{
return nColumns;
}
/*
* tells the rest of the world which type of
* data an exported model column contains
*/
override GType getColumnType(int index)
{
if ( index >= nColumns || index < 0 )
return GType.INVALID;
return columnTypes[index];
}
/*
* converts a tree path (physical position) into a
* tree iter structure (the content of the iter
* fields will only be used internally by our model).
* We simply store a pointer to our CustomRecord
* structure that represents that row in the tree iter.
*/
override int getIter(TreeIter iter, TreePath path)
{
CustomRecord* record;
int[] indices;
int n, depth;
indices = path.getIndices();
depth = path.getDepth();
/* we do not allow children */
if (depth != 1)
return false;//throw new Exception("We only except lists");
n = indices[0]; /* the n-th top level row */
if ( n >= numRows || n < 0 )
return false;
record = rows[n];
if ( record is null )
throw new Exception("Not Exsisting record requested");
if ( record.pos != n )
throw new Exception("record.pos != TreePath.getIndices()[0]");
/* We simply store a pointer to our custom record in the iter */
iter.stamp = stamp;
iter.userData = record;
return true;
}
/*
* converts a tree iter into a tree path (ie. the
* physical position of that row in the list).
*/
override TreePath getPath(TreeIter iter)
{
TreePath path;
CustomRecord* record;
if ( iter is null || iter.userData is null || iter.stamp != stamp )
return null;
record = cast(CustomRecord*) iter.userData;
path = new TreePath(record.pos);
return path;
}
/*
* Returns a row's exported data columns
* (_get_value is what gtk_tree_model_get uses)
*/
override Value getValue(TreeIter iter, int column, Value value = null)
{
CustomRecord *record;
if ( value is null )
value = new Value();
if ( iter is null || column >= nColumns || iter.stamp != stamp )
return null;
value.init(columnTypes[column]);
record = cast(CustomRecord*) iter.userData;
if ( record is null || record.pos >= numRows )
return null;
switch(column)
{
case CustomListColumn.Record:
value.setPointer(record);
break;
case CustomListColumn.Name:
value.setString(record.name);
break;
case CustomListColumn.YearBorn:
value.setUint(record.yearBorn);
break;
default:
break;
}
return value;
}
/*
* Takes an iter structure and sets it to point
* to the next row.
*/
override bool iterNext(TreeIter iter)
{
CustomRecord* record, nextrecord;
if ( iter is null || iter.userData is null || iter.stamp != stamp )
return false;
record = cast(CustomRecord*) iter.userData;
/* Is this the last record in the list? */
if ( (record.pos + 1) >= numRows)
return false;
nextrecord = rows[(record.pos + 1)];
if ( nextrecord is null || nextrecord.pos != record.pos + 1 )
throw new Exception("Invalid next record");
iter.stamp = stamp;
iter.userData = nextrecord;
return true;
}
/*
* Returns TRUE or FALSE depending on whether
* the row specified by 'parent' has any children.
* If it has children, then 'iter' is set to
* point to the first child. Special case: if
* 'parent' is NULL, then the first top-level
* row should be returned if it exists.
*/
override bool iterChildren(out TreeIter iter, TreeIter parent)
{
/* this is a list, nodes have no children */
if ( parent !is null )
return false;
/* No rows => no first row */
if ( numRows == 0 )
return false;
/* Set iter to first item in list */
iter = new TreeIter();
iter.stamp = stamp;
iter.userData = rows[0];
return true;
}
/*
* Returns TRUE or FALSE depending on whether
* the row specified by 'iter' has any children.
* We only have a list and thus no children.
*/
override bool iterHasChild(TreeIter iter)
{
return false;
}
/*
* Returns the number of children the row
* specified by 'iter' has. This is usually 0,
* as we only have a list and thus do not have
* any children to any rows. A special case is
* when 'iter' is NULL, in which case we need
* to return the number of top-level nodes,
* ie. the number of rows in our list.
*/
override int iterNChildren(TreeIter iter)
{
/* special case: if iter == NULL, return number of top-level rows */
if ( iter is null )
return numRows;
return 0; /* otherwise, this is easy again for a list */
}
/*
* If the row specified by 'parent' has any
* children, set 'iter' to the n-th child and
* return TRUE if it exists, otherwise FALSE.
* A special case is when 'parent' is NULL, in
* which case we need to set 'iter' to the n-th
* row if it exists.
*/
override bool iterNthChild(out TreeIter iter, TreeIter parent, int n)
{
CustomRecord *record;
/* a list has only top-level rows */
if( parent !is null )
return false;
if( n >= numRows )
return false;
record = rows[n];
if ( record == null || record.pos != n )
throw new Exception("Invalid record");
iter = new TreeIter();
iter.stamp = stamp;
iter.userData = record;
return true;
}
/*
* Point 'iter' to the parent node of 'child'. As
* we have a list and thus no children and no
* parents of children, we can just return FALSE.
*/
override bool iterParent(out TreeIter iter, TreeIter child)
{
return false;
}
/*
* Empty lists are boring. This function can
* be used in your own code to add rows to the
* list. Note how we emit the "row-inserted"
* signal after we have appended the row
* internally, so the tree view and other
* interested objects know about the new row.
*/
void appendRecord(string name, uint yearBorn)
{
TreeIter iter;
TreePath path;
CustomRecord* newrecord;
uint pos;
if ( name is null )
return;
pos = numRows;
numRows++;
newrecord = new CustomRecord;
newrecord.name = name;
newrecord.yearBorn = yearBorn;
rows ~= newrecord;
newrecord.pos = pos;
/* inform the tree view and other interested objects
* (e.g. tree row references) that we have inserted
* a new row, and where it was inserted */
path = new TreePath(pos);
iter = new TreeIter();
getIter(iter, path);
rowInserted(path, iter);
}
}
|