Click here to Skip to main content
15,888,610 members
Articles / Programming Languages / C#

How we can write on C# in Linux: Implementing PanelApplet to Gnome Desktop

Rate me:
Please Sign up or sign in to vote.
4.86/5 (48 votes)
8 Mar 2010CPOL12 min read 85.1K   382   95  
This article describes how we can use our programming skills on the .NET framework to implement platform-independent code
using System;

using Gtk;
using GConf;

using WeatherApplet.Model;

namespace WeatherApplet.View
{

	public class TreeView:Window
	{


	Label filterEntry;
	public TreeView ():base("asdasd")
	{
			
		//Create window
		ScrolledWindow sw = new Gtk.ScrolledWindow ();
		this.SetSizeRequest (500,200);
		 
		// Create an Entry used to filter the tree
		filterEntry = new Gtk.Label ();
 
		// Fire off an event when the text in the Entry changes
		//filterEntry.Changed += OnFilterEntryTextChanged;
		
		// Create a nice label describing the Entry
		Gtk.Label filterLabel = new Gtk.Label ("Selected country:");
		
		// Put them both into a little box so they show up side by side
		Gtk.HBox filterBox = new Gtk.HBox ();
			
		filterBox.PackStart (filterLabel, false, false, 5);
		filterBox.PackStart (filterEntry, true, true, 5);
 
		// Create our TreeView
		Gtk.TreeView tree = new Gtk.TreeView ();
 
		// Create a box to hold the Entry and Tree
		Gtk.VBox box = new Gtk.VBox ();
 
		// Add the widgets to the box
		box.PackStart (filterBox, false, false, 5);
		sw.Add (tree);
		box.PackStart (sw, true, true, 5);

		
		//Create column	
		Gtk.TreeViewColumn countriesColumn = new Gtk.TreeViewColumn ();
		countriesColumn.Title = "Countries";
 		Gtk.CellRendererText countriesNameCell = new Gtk.CellRendererText ();
		countriesColumn.PackStart (countriesNameCell, true);
		tree.AppendColumn (countriesColumn);
 		countriesColumn.AddAttribute(countriesNameCell, "text", 0);
			
		//Create list of countries	
		tree.Model = BuildTree();
		
		tree.Selection.Changed += OnSelectionChanged; 	
			
 		this.Add(box);
		this.ShowAll ();
	}
		
		private TreeStore BuildTree()
		{
			Gtk.TreeStore countryListStore = new Gtk.TreeStore(typeof (string));
			
			Gtk.TreeIter europe = countryListStore.AppendValues ("Europe");
		
			SetCountryCities("Belarus", europe, ref countryListStore);
			SetCountryCities("Russia", europe, ref countryListStore);
						
			return countryListStore;
		}
			        
		private void SetCountryCities(string country,TreeIter region, ref TreeStore store)
		{
			TreeIter countryIter = store.AppendNode(region);
			store.SetValue(countryIter,0, country);
		
			foreach(var city in WeatherService.GetCitiesByCountry(country))
				store.AppendValues (countryIter, city);
			
		}
		
		 void OnSelectionChanged (object o, EventArgs args)
        {
			try
			{
                TreeIter iter;
                TreeModel model;
 
                if (((TreeSelection)o).GetSelected (out model, out iter))
                {
					if(model.IterHasChild(iter)) return;
						
						TreeIter parent;
						model.IterParent(out parent, iter);
					
						string state = (string) model.GetValue (parent, 0);
                        string city = (string) model.GetValue (iter, 0);
						filterEntry.Text = String.Format("{0}-{1} was selected", state, city);
						
						SettingsStore.Set("state", state);
						SettingsStore.Set("city", city);
                }
			}
			catch
			{
			}
        }


	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Nokia
Germany Germany
Interested in design/development of framework functionality using the best patterns and practices.

Comments and Discussions