~cairo-dock-team/ubuntu/quantal/cairo-dock-plug-ins/3.0.2

« back to all changes in this revision

Viewing changes to Dbus/demos/demo_mono/demo_mono.cs

Tags: upstream-2.2.0~0beta4
ImportĀ upstreamĀ versionĀ 2.2.0~0beta4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
//
 
3
//This is a part of the external demo applet for Cairo-Dock
 
4
//
 
5
//Copyright : (C) 2010 by Fabounet
 
6
//E-mail : fabounet@glx-dock.org
 
7
//
 
8
//
 
9
//This program is free software; you can redistribute it and/or
 
10
//modify it under the terms of the GNU General Public License
 
11
//as published by the Free Software Foundation; either version 2
 
12
//of the License, or (at your option) any later version.
 
13
//
 
14
//This program is distributed in the hope that it will be useful,
 
15
//but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
17
//GNU General Public License for more details.
 
18
//http://www.gnu.org/licenses/licenses.html//GPL
 
19
 
 
20
//The name of this applet is "demo_mono"; it is placed in a folder named "demo_mono", with a file named "auto-load.conf" which describes it.
 
21
//Copy this folder into ~/.config/cairo-dock/third-party to let the dock register it automatically.
 
22
//In the folder we have :
 
23
//"demo_mono" (the executable script), "demo_mono.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)
 
24
//
 
25
//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.
 
26
//Scroll on the icon increase or decrease the counter.
 
27
//The menu offers the possibility to set some default value.
 
28
//Left click on the icon will set a random value.
 
29
//Middle click on the icon will raise a dialog asking you to set the value you want.
 
30
//If you drop some text on the icon, it will be used as the icon's label.
 
31
 
 
32
// Compile it with the following command, then rename 'demo_mono.exe' to 'demo_mono'.
 
33
//gmcs demo_mono.cs -r:/usr/lib/mono/gtk-sharp-2.0/glib-sharp.dll -r:/usr/lib/cli/ndesk-dbus-glib-1.0/NDesk.DBus.GLib.dll -r:/usr/lib/cli/ndesk-dbus-1.0/NDesk.DBus.dll
 
34
 
 
35
  //////////////////////////
 
36
 ////// dependancies //////
 
37
//////////////////////////
 
38
using System;
 
39
using GLib;
 
40
using NDesk.DBus;
 
41
 
 
42
  ////////////////////////////
 
43
 ////// DBus Interface //////
 
44
////////////////////////////
 
45
public delegate void OnClickEvent (int i);
 
46
public delegate void OnMiddleClickEvent ();
 
47
public delegate void OnScrollEvent (bool b);
 
48
public delegate void OnBuildMenuEvent ();
 
49
public delegate void OnMenuSelectEvent (int i);
 
50
public delegate void OnDropDataEvent (string s);
 
51
public delegate void OnChangeFocusEvent (bool b);
 
52
public delegate void OnAnswerEvent (System.Object v);
 
53
public delegate void OnStopModuleEvent ();
 
54
public delegate void OnReloadModuleEvent (bool b);
 
55
[NDesk.DBus.Interface ("org.cairodock.CairoDock.applet")]
 
56
public interface IIcon
 
57
{
 
58
        System.Object Get (string s);
 
59
        void GetAll ();
 
60
        void SetLabel (string s);
 
61
        void SetIcon (string s);
 
62
        void SetEmblem (string s, int i);
 
63
        void Animate (string s, int i);
 
64
        void SetQuickInfo (string s);
 
65
        void ShowDialog (string s, int i);
 
66
        void AskQuestion (string s);
 
67
        void AskValue (string s, double x, double y);
 
68
        void AskText (string s, string t);
 
69
        void AddDataRenderer (string s, int i, string t);
 
70
        void RenderValues (double[] x);
 
71
        void ControlAppli (string s);
 
72
        void PopulateMenu (string[] s);
 
73
        event OnClickEvent on_click; 
 
74
        event OnMiddleClickEvent on_middle_click; 
 
75
        event OnScrollEvent on_scroll; 
 
76
        event OnBuildMenuEvent on_build_menu; 
 
77
        event OnMenuSelectEvent on_menu_select; 
 
78
        event OnDropDataEvent on_drop_data; 
 
79
        event OnChangeFocusEvent on_change_focus; 
 
80
        event OnAnswerEvent on_answer; 
 
81
        event OnStopModuleEvent on_stop_module; 
 
82
        event OnReloadModuleEvent on_reload_module; 
 
83
}
 
84
 
 
85
public struct Config {
 
86
        public bool yesno;
 
87
        public int iMaxValue;
 
88
        public string cTheme;
 
89
}
 
90
 
 
91
public class Applet
 
92
{
 
93
        public static string applet_name = "demo_mono";  // the name of the applet must the same as the folder.
 
94
        public static string applet_path = "/org/cairodock/CairoDock/"+applet_name;  // path where our object is stored on the bus.
 
95
        public static string conf_file = Environment.GetEnvironmentVariable("HOME")+"/.config/cairo-dock/current_theme/plug-ins/"+applet_name+"/"+applet_name+".conf";  // path to the conf file of our applet.
 
96
        public IIcon myIcon = null;
 
97
        public Config myConfig;
 
98
        private GLib.MainLoop loop = null;
 
99
        private int count = 0;
 
100
        
 
101
        public void begin()
 
102
        {
 
103
                connect_to_dock ();
 
104
                get_config ();
 
105
                myIcon.ShowDialog ("I'm connected to Cairo-Dock !", 4);
 
106
                myIcon.AddDataRenderer("gauge", 1, myConfig.cTheme);
 
107
                this.set_counter (0);
 
108
                loop = new GLib.MainLoop();
 
109
                loop.Run();
 
110
        }
 
111
        public void end()
 
112
        {
 
113
                loop.Quit();
 
114
        }
 
115
        public void connect_to_dock()
 
116
        {
 
117
                BusG.Init ();
 
118
                Bus bus = Bus.Session;
 
119
                myIcon = bus.GetObject<IIcon> ("org.cairodock.CairoDock", new ObjectPath (applet_path));
 
120
                myIcon.on_click                         += new OnClickEvent (action_on_click);
 
121
                myIcon.on_middle_click  += new OnMiddleClickEvent (action_on_middle_click);
 
122
                myIcon.on_scroll                += new OnScrollEvent (action_on_scroll);
 
123
                myIcon.on_build_menu    += new OnBuildMenuEvent (action_on_build_menu);
 
124
                myIcon.on_menu_select   += new OnMenuSelectEvent (action_on_menu_select);
 
125
                myIcon.on_drop_data             += new OnDropDataEvent (action_on_drop_data);
 
126
                myIcon.on_answer                += new OnAnswerEvent (action_on_answer);
 
127
                myIcon.on_stop_module   += new OnStopModuleEvent (action_on_stop_module);
 
128
                myIcon.on_reload_module         += new OnReloadModuleEvent (action_on_reload_module);
 
129
        }
 
130
        public void get_config ()
 
131
        {
 
132
                /// read this.conf_file ...
 
133
                myConfig.iMaxValue = 100;
 
134
                myConfig.cTheme = "Turbo-night";
 
135
                myConfig.yesno = true;
 
136
        }
 
137
          ////////////////////////////////////////
 
138
         ////// callbacks on the main icon //////
 
139
        ////////////////////////////////////////
 
140
        private void action_on_click (int iClickState)
 
141
        {
 
142
                Console.WriteLine(">>> click");
 
143
        }
 
144
        private void action_on_middle_click ()
 
145
        {
 
146
                Console.WriteLine(">>> middle click");
 
147
                myIcon.AskValue("Set the value you want", this.count, 100);
 
148
        }
 
149
        private void action_on_scroll (bool bScrollUp)
 
150
        {
 
151
                Console.WriteLine(">>> scroll up " + bScrollUp);
 
152
                int n;
 
153
                if (bScrollUp)
 
154
                        n = Math.Min(100, this.count+1);
 
155
                else
 
156
                        n = Math.Max(0, this.count-1);
 
157
                this.set_counter(n);
 
158
        }
 
159
        private void action_on_build_menu ()
 
160
        {
 
161
                Console.WriteLine(">>> build menu");
 
162
                myIcon.PopulateMenu(new string [] {"set min value", "set medium value", "set max value"});
 
163
        }
 
164
        private void action_on_menu_select (int iNumEntry)
 
165
        {
 
166
                Console.WriteLine(">>> select entry : "+iNumEntry);
 
167
                if (iNumEntry == 0)
 
168
                        this.set_counter(0);
 
169
                else if (iNumEntry == 1)
 
170
                        this.set_counter(100/2);
 
171
                else if (iNumEntry == 2)
 
172
                        this.set_counter(100);
 
173
        }
 
174
        private void action_on_drop_data (string cReceivedData)
 
175
        {
 
176
                Console.WriteLine(">>> drop : "+cReceivedData);
 
177
                myIcon.SetLabel(cReceivedData);
 
178
        }
 
179
        private void action_on_answer (System.Object answer)
 
180
        {
 
181
                Console.WriteLine(">>> answer : "+(double)answer);
 
182
                double x = (double)answer;
 
183
                this.set_counter((int) x);
 
184
        }
 
185
          /////////////////////////////////////
 
186
         ////// callbacks on the applet //////
 
187
        /////////////////////////////////////
 
188
        private void action_on_stop_module ()
 
189
        {
 
190
                Console.WriteLine(">>> stop");
 
191
                this.end();
 
192
        }
 
193
        private void action_on_reload_module (bool bConfigHasChanged)
 
194
        {
 
195
                Console.WriteLine(">>> our module is reloaded");
 
196
                if (bConfigHasChanged)
 
197
                {
 
198
                        Console.WriteLine (">>>  and our config has changed");
 
199
                        this.get_config();
 
200
                        myIcon.AddDataRenderer("gauge", 1, this.myConfig.cTheme);
 
201
                        this.set_counter (Math.Min (this.count, this.myConfig.iMaxValue));
 
202
                }
 
203
        }
 
204
        
 
205
          ////////////////////////////
 
206
         ////// applet methods //////
 
207
        ////////////////////////////
 
208
        public void set_counter(int n)
 
209
        {
 
210
                this.count = n;
 
211
                myIcon.RenderValues(new double[] {(double)n/myConfig.iMaxValue});
 
212
                myIcon.SetQuickInfo(String.Format(n.ToString()));
 
213
        }
 
214
        
 
215
          //////////////////
 
216
         ////// main //////
 
217
        //////////////////
 
218
        public static void Main ()
 
219
        {
 
220
                Applet myApplet = new Applet ();
 
221
                myApplet.begin();
 
222
                Console.WriteLine(">>> bye");
 
223
        }
 
224
}
 
225