1/* This is a part of the external demo applet for Cairo-Dock
2
3Copyright : (C) 2010-2011 by Fabounet
4E-mail : fabounet@glx-dock.org
5
6This program is free software; you can redistribute it and/or
7modify it under the terms of the GNU General Public License
8as published by the Free Software Foundation; either version 2
9of the License, or (at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15http://www.gnu.org/licenses/licenses.html#GPL */
16
17/// The name of this applet is "demo_vala"; it is placed in a folder named "demo_vala", with a file named "auto-load.conf" which describes it.
18/// Copy this folder into ~/.config/cairo-dock/third-party to let the dock register it automatically.
19/// In the folder we have :
20/// "demo_vala" (the executable script), "demo_vala.conf" (the default config file), "auto-load.conf" (the file describing our applet), "icon" (the default icon of the applet) and "preview" (a preview of this applet)
21
22/// This very simple applet features a counter from 0 to iMaxValue It displays the counter on the icon with a gauge and a quick info.
23/// Scroll on the icon increase or decrease the counter.
24/// The menu offers the possibility to set some default value.
25/// Left click on the icon will set a random value.
26/// Middle click on the icon will raise a dialog asking you to set the value you want.
27/// If you drop some text on the icon, it will be used as the icon's label.
28
29/// Compile it with (you may have to install valac) :
30/// valac --pkg CDApplet -o demo_vala demo_vala.vala
31/// ou
32/// valac -q -C --disable-warnings --pkg CDApplet demo_vala.vala
33/// gcc -o demo_vala $(pkg-config --cflags --libs CDApplet) demo_vala.c
34
35
36  /////////////////////////
37 ///// dependancies //////
38/////////////////////////
39using GLib;
40using CairoDock.Applet;
41
42struct Config {
43    public string cTheme;
44    public int iMaxValue;
45    public bool yesno;
46}
47
48public class MyApplet : CDApplet
49{
50	// my config.
51	private Config config;
52	// my data.
53	private int count;
54
55	public MyApplet(string[] argv)
56	{
57		//this.config = Config();
58		base(argv);
59	}
60
61	  ///////////////////////
62	 /// private methods ///
63	///////////////////////
64
65	private void set_counter(int count)
66	{
67		this.count = count;
68		double[] percent = {1.0*this.count/this.config.iMaxValue};
69		base.icon.RenderValues(percent);
70		this.icon.SetQuickInfo("%d".printf(this.count));
71	}
72
73	  ////////////////////////////////////////
74	 ////// callbacks on the main icon //////
75	////////////////////////////////////////
76	public override void on_click(int iState)
77	{
78		print ("*** clic !\n");
79		set_counter(GLib.Random.int_range(0,this.config.iMaxValue+1));
80	}
81	public override void on_middle_click()
82	{
83		print ("*** middle clic !\n");
84		//this.icon.AskValue("Set the value you want", (double)this.count, (double)this.config.iMaxValue);
85		HashTable<string,Variant>dialog_attributes = new HashTable<string,Variant>(str_hash, str_equal);
86		dialog_attributes.insert("icon", "stock_properties");
87		dialog_attributes.insert("message", "Set the value you want");
88		dialog_attributes.insert("buttons", "ok;cancel");
89		HashTable<string,Variant> widget_attributes = new HashTable<string,Variant>(str_hash, str_equal);  // even if you don't have widget attributes, you must fill the hash-table with at least 1 value, otherwise vala will crash :-/
90		widget_attributes.insert("widget-type","scale");
91		widget_attributes.insert("max-value",this.config.iMaxValue);
92		widget_attributes.insert("message","Set the value you want");
93		this.icon.PopupDialog(dialog_attributes, widget_attributes);
94	}
95	public override void on_build_menu()
96	{
97		print ("*** build menu !\n");
98		HashTable<string,Variant>[] pItems = {};
99		HashTable<string,Variant> pItem;
100
101		pItem = new HashTable<string,Variant?>(str_hash, str_equal);
102		pItem.insert("label", "set min value");
103		pItem.insert("icon", "gtk-zoom-out");
104		pItem.insert("id", 0);
105		pItems += pItem;
106
107		pItem = new HashTable<string,Variant?>(str_hash, str_equal);
108		pItem.insert("label", "set medium value");
109		pItem.insert("icon", "gtk-zoom-fit");
110		pItem.insert("id", 1);
111		pItems += pItem;
112
113		pItem = new HashTable<string,Variant?>(str_hash, str_equal);
114		pItem.insert("label", "set max value");
115		pItem.insert("icon", "gtk-zoom-in");
116		pItem.insert("id", 2);
117		pItems += pItem;
118
119		this.icon.AddMenuItems(pItems);
120	}
121	public override void on_menu_select(int iNumEntry)
122	{
123		print ("*** choice %d has been selected !\n", iNumEntry);
124		if (iNumEntry == 0)
125			this.set_counter(0);
126		else if (iNumEntry == 1)
127			this.set_counter(this.config.iMaxValue/2);
128		else if (iNumEntry == 2)
129			this.set_counter(this.config.iMaxValue);
130	}
131	public override void on_scroll(bool bScrollUp)
132	{
133		print ("*** scroll !\n");
134		int count;
135		if (bScrollUp)
136			count = int.min(this.config.iMaxValue, this.count+1);
137		else
138			count = int.max(0, this.count-1);
139		this.set_counter(count);
140	}
141	public override void on_drop_data(string cReceivedData)
142	{
143		print ("*** received : %s\n",cReceivedData);
144		this.icon.SetLabel(cReceivedData);
145	}
146	public override void on_answer_dialog(int iButton, Variant answer)
147	{
148		print ("*** answer : %d\n",(int)answer.get_double());
149		if (iButton == 0)  // ok
150			this.set_counter((int)answer.get_double());
151	}
152
153	  ////////////////////////////////////////
154	 ////// callbacks on the sub-icons //////
155	////////////////////////////////////////
156	public override void on_click_sub_icon(int iState, string cIconID)
157	{
158		print ("clic on the sub-icon '%s' !\n", cIconID);
159	}
160
161	  ///////////////////////////////
162	 ////// applet definition //////
163	///////////////////////////////
164	public override void end()
165	{
166		print ("*** our module is stopped\n");
167	}
168
169	public override void reload()
170	{
171		this.icon.AddDataRenderer("gauge", 1, this.config.cTheme);
172		double[] percent = {1.0*this.count/this.config.iMaxValue};
173		this.icon.RenderValues(percent);
174		this.sub_icons.RemoveSubIcon("any");
175		string[] subicons = {"icon 1", "firefox-3.0", "id1", "icon 3", "thunderbird", "id3", "icon 4", "nautilus", "id4"};
176		this.sub_icons.AddSubIcons(subicons);
177	}
178	public override void get_config(GLib.KeyFile keyfile)
179	{
180		this.config.cTheme 	= keyfile.get_string("Configuration", "theme");
181		print ("cTheme: %s\n", this.config.cTheme);
182		this.config.iMaxValue 	= keyfile.get_integer("Configuration", "max value");
183		this.config.yesno 	= keyfile.get_boolean("Configuration", "yesno");
184	}
185	public override void begin()
186	{
187		this.icon.ShowDialog("I'm connected to Cairo-Dock !", 4);  // show a dialog with this message for 4 seconds.
188		this.icon.AddDataRenderer("gauge", 1, this.config.cTheme);  // set a gauge with the theme read in config to display the value of the counter.
189		double[] percent  = {1.0*this.count/this.config.iMaxValue};
190		this.icon.RenderValues(percent);  // draw the gauge with an initial value.
191		string[] subicons ={"icon 1", "firefox-3.0", "id1", "icon 2", "trash", "id2", "icon 3", "thunderbird", "id3", "icon 4", "nautilus", "id4"};
192		this.sub_icons.AddSubIcons(subicons);  // add 4 icons in our sub-dock. The tab contains triplets of {label, image, ID}.
193		this.sub_icons.RemoveSubIcon("id2");  // remove the 2nd icon of our sub-dock.
194		this.sub_icons.SetQuickInfo("1", "id1");  // write the ID on each icon of the sub-dock.
195		this.sub_icons.SetQuickInfo("3", "id3");
196		this.sub_icons.SetQuickInfo("4", "id4");
197		print ("DEMO %s\n", this.config.cTheme);
198	}
199}
200
201  //////////////////
202 ////// main //////
203//////////////////
204static int main (string[] argv)
205{
206	var myApplet = new MyApplet(argv);
207	myApplet.run();
208	return 0;
209}
210