How do I use a GtkComboBox with objects, as opposed to strings? - mono

The usual use for a combo box is to let it display options to the user, and then you get an OBJECT out of it. In Win32, you do it by using the CB_SETITEMDATA and CB_GETITEMDATA messages, casting between int and object pointers. In XAML, you set up a data template and the item in the list IS the object.
What is the Correct way to get this effect with a GtkComboBox?

GtkComboBox normally uses a GtkListStore as the underlaying model.
You need to create one with an extra column for the object you want to store and as you insert new items in the combo's model you also need to provide the object you want to associate with that row/item.

Related

EaselJS - Storing Shape Graphics in Database

I'm using EaselJS for a project, and have the need to store the details of various 'Shape' objects in a database, so that they can be re-drawn at another time.
My project has the 'Stage' set up so that I can drag-draw a box, which is saved into the database when completed. I intend to make it usable for drawing polygons also.
My database has a table in it called 'polyshapes', which stores the Shape.id, Shape.name, Shape.x, Shape.y, Shape.rotation. It also has a field called 'graphics' where I can store some sort of text string representing the Shape.graphics object.
When I draw a box, I'm able to store a string of the 'graphics' using: JSON.stringify(Myshape.graphics).
When I load this shape from the database, I create a new createjs.Shape() object, and then use Myshape.graphics = JSON.parse(graphicsString) to load the graphics.
Unfortunately this doesn't seem to work, as it doesn't load the required functions within the object.
I have also tried just individually updating some specific graphics attributes such as 'command', '_instructions', '_stroke', '_strokeStyle'. But I still can get the required graphic to re-draw.
Ideally, I'd only need to store the 'instructions' attribute of the Shape.graphics when I drag-draw it, and then when loading from the DB, I would just use a method to re-create those graphics when provided with the 'instructions' data. However I can't seem to see a suitable method for doing this.
I have also looked into the decodePath() graphics method. This would be useful, except that there doesn't appear to be an inverse method 'encodePath()' to allow me to serialize it in the first place.
How can I store the Shape.graphics data in a database, and then recall it later to rebuild a new Shape() object?
Thanks,
Hugh.

Check whether the given object is a list?

How can we check whether the given object is a list or other type
in velocity. In that list i have another list which i need to iterate again.
I also have another data in the parent list which i want to print while iterating parent list. But the problem is the child list object also get printing with actual data. So i want to print the data by checking whether its list or not. Any help is much appreciated.
Before you get any remarks on using too much logic in templates, try this reflection based approach :
velocity (test instanceof)

DataTemplateSelector in SemanticZoom.ZoomedOutView

I have a set of groups of different types of item, all inheriting form a common base type (ItemBase). Each of my groups has an Items collection of type ObservableCollection<ItemBase>.
In my SemanticZoom.ZoomedInView, I can set a DataTemplateSelector, and in SelectTemplateCore() I can cast the item parameter to detrmine which template to apply.
In my zoomed out view, though, the objects are passed around as DependencyObjects, and I can't for the life of me figure out how I can take the data passsed in to determine which template to use.
To set the items source of the GridView in the zoomed out view, I use
(semZm.ZoomedOutView as ListViewBase).ItemsSource = this.groupedItemsViewSource.View.CollectionGroups;
as this appears to be the only way to get the zoomedin and zoomedout views to synchronise (when you click on a group in the zoomed out view it should take me to the appropriate place in the zoomedinview to see that group's detail.
So, am I missing something obvious in terms of getting the actual group in SelectTemplateCore(), or failing that is there a better way of binding my ItemsSource of the ZoomedOutView?
For the appropriate way to handle casting of the DependencyObject to a usable type, see the answer to This question.
Essentially, cast it to ICollectionViewGroup to access the members.

Finding variables that share common properties

I'm using Mathematica and have a set of variables (A,B,C,D,...) with properties A=(blue, big, rounded), B=(red, small, spiky), and so forth. Those properties can be common between variables. What would be the best, general way to find all variables that share a common property (of being, for instance, small)? Thanks.
Here's a list of possible properties:
In[1]:= properties={"red","green","blue","big","small","rounded","spiky"};
And here's a list of objects with some of those properties
In[2]:= list={{"blue","big","rounded"},{"red","small","spiky"},
{"red","big","rounded"},{"blue","small","spiky"}};
You can find all objects that have the property of, e.g., being "blue" using Select
In[3]:= Select[list, MemberQ[#,"blue"]&]
Out[3]= {{blue,big,rounded},{blue,small,spiky}}
This could be wrapped up into a function. Although how I would write that function would depend on the data structures and usage that you're planning.
Actually, I just reread you question you have a list of objects with some properties and you want to refer to those objects by name. So you probably want something more like
In[1]:= listProperties["A"]:={"blue","big","rounded"}
listProperties["B"]:={"red","small","spiky"}
listProperties["C"]:={"red","big","rounded"}
listProperties["D"]:={"blue","small","spiky"}
Above I defined some properties that are associated with certain strings. You don't have to use strings in the above or below, and you can create a better structure than that if you want. You could also make a constructor to create the above, such a constructor could also check if the list of properties supplied is of the right form - i.e. does not have contradictory properties, are all in a list of known properties etc...
We then define a function to test if an object/string has a certain property associated with it
In[2]:= hasProperty[obj_, property_]:=MemberQ[listProperties[obj],property]
You might want to return an error or warning message if listProperties[obj] does not have a definition/rule associated with it.
Use Select to find all "objects" in a list that have the associated property "blue":
In[3]:= Select[{"A","B","C","D"}, hasProperty[#,"blue"]&]
Out[3]= {A,D}
There are other ways (probably better ways) to set up such a data structure. But this is one of the simplest ways in Mathematica.

How to use GtkTreeView correctly

I am using a TreeView with a ListStore as model. When the user clicks on a row I want to take some action but not using the values in the cells, but using the data I created the row from...
Currently I have the TreeView, the TreeModel (ListStore) and my own data (which I ironically call model)..
So the Questions are:
Is it "right" to have a model - an object representation of the data I want to display and fill a ListStore with that data to display in a TreeView, or would it be better to implement an own version of TreeModel (wrapping my data-model) to display the data?
And also:
If someone double-clicks in a row I can get the RowActivated event (using C#/Gtk#) which provides a Path to the activated row. With that I can get a TreeIter and using that I can get the value of a cell. But what is the best practice to find the data object from which the row was constructed in the first place?\
(Somehow this question got me to the first one - by thinking would getting the data object more easy if I tried to implement my own TreeModel...)
It's quite awkward/difficult to implement TreeModel, so most people simply synch the data from their "real" model into a TreeStore or ListStore.
The columns in the store do not have to match the columns in the view in any way. For example, you can have a column that contains your real managed data objects.
When you add a cellrenderer to a TreeView (visual) column, you can add mappings between its properties and the columns of the store. For example, you could map one store column to the font of a text cellrenderer, and another store column to the text property of the same cellrenderer. Each time the cellrenderer is used to render a particular cell, the mappings will be used to retrieve the values from the store and apply them to the properties of the renderer before it renders.
Here's an example of a mapping:
treeView.AppendColumn ("Title", renderer, "text", 0, "editable", 4);
This maps store column 0 to the renderer's text GTK property and maps store column 4 to the editable property. For GTK property names you can check the GTK docs. Note that the example above uses a convenience method that adds a column, adds a renderer to it and add an arbitrary number of mapping via params. To add mappings directly to a column, for example a column with multiple renderers, pack the renderers into the column then use TreeViewColumn.AddAttribute or TreeViewColumn.SetAttributes.
You can also set up a custom data function that will be used instead of mappings. This allows you to set the properties of the renderer directly, given a TreeIter and the store - so, if all the data you want to display is trivially derived from your real data objects, you could even have your store only contain a single column of these objects, and use data funcs for all the view columns.
Here's an example of a data func that does exactly what the mapping example above does:
treeColumn.SetCellDataFunc (renderer, delegate (TreeViewColumn col,
CellRenderer cell, TreeModel model, TreeIter iter)
{
var textCell = (CellRendererText) cell;
textCell.Text = (string) model.GetValue (iter, 0);
textCell.Editable = (bool) model.GetValue (iter, 4);
});
Obviously data functions are much more powerful because they enable you not only to use properties of more complex GTK objects, but also to implement more complex display logic - for example, lazily processing derived values only when the cell is actually rendered.