1 /*
2  * $Id: TestUI.cs 179 2007-04-21 15:01:29Z meebey $
3  * $URL: svn+ssh://svn.qnetp.net/svn/smuxi/smuxi/trunk/src/Frontend-Test/TestUI.cs $
4  * $Rev: 179 $
5  * $Author: meebey $
6  * $Date: 2007-04-21 17:01:29 +0200 (Sat, 21 Apr 2007) $
7  *
8  * Smuxi - Smart MUltipleXed Irc
9  *
10  * Copyright (c) 2005-2006 Mirco Bauer <meebey@meebey.net>
11  *
12  * Full GPL License: <http://www.gnu.org/licenses/gpl.txt>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
27  */
28 
29 using System;
30 using System.IO;
31 using System.Text;
32 using System.Runtime.InteropServices;
33 using System.Reflection;
34 
35 namespace Stfl
36 {
37     public class Form : IDisposable
38     {
39         IntPtr f_Handle;
40 
41         bool Disposed { get; set; }
42 
43         public event KeyPressedEventHandler KeyPressed;
44         public event EventHandler<EventReceivedEventArgs> EventReceived;
45         public event EventHandler Resized;
46 
47         public string this[string name] {
48             get {
49                 CheckDisposed();
50                 return StflApi.stfl_get(f_Handle, name);
51             }
52             set {
53                 CheckDisposed();
54                 StflApi.stfl_set(f_Handle, name, value);
55             }
56         }
57 
Form(string text)58         public Form(string text)
59         {
60             f_Handle = StflApi.stfl_create(text);
61 
62             // initialize ncurses
63             StflApi.stfl_run(f_Handle, -3);
64             //StflApi.raw();
65             NcursesApi.nocbreak();
66         }
67 
Form(Assembly assembly, string resourceName)68         public Form(Assembly assembly, string resourceName)
69         {
70             if (assembly == null) {
71                 assembly = Assembly.GetCallingAssembly();
72             }
73 
74             using (Stream stream = assembly.GetManifestResourceStream(resourceName))
75             using (StreamReader reader = new StreamReader(stream)) {
76                 if (stream == null) {
77                     throw new ArgumentException(resourceName + " could not be found in assembly", "resourceName");
78                 }
79                 string text = reader.ReadToEnd();
80                 if (String.IsNullOrEmpty(text)) {
81                     throw new ArgumentException(resourceName + " in assembly is missing or empty.", "resourceName");
82                 }
83                 f_Handle = StflApi.stfl_create(text);
84             }
85         }
86 
~Form()87         ~Form()
88         {
89             Dispose(false);
90         }
91 
Dispose(bool disposing)92         protected virtual void Dispose(bool disposing)
93         {
94             var disposed = Disposed;
95             if (disposed) {
96                 return;
97             }
98 
99             if (f_Handle != IntPtr.Zero) {
100                 StflApi.stfl_free(f_Handle);
101             }
102         }
103 
Dispose()104         public virtual void Dispose()
105         {
106             Dispose(true);
107             GC.SuppressFinalize(this);
108         }
109 
Run(int timeout)110         public virtual void Run(int timeout)
111         {
112             CheckDisposed();
113             string @event = StflApi.stfl_run(f_Handle, timeout);
114             if (timeout == -3) {
115                 // HACK: timeout of -3 should never return an event but
116                 // sometimes does which causes event duplication
117                 return;
118             }
119             ProcessEvent(@event);
120         }
121 
Run()122         public void Run()
123         {
124             Run(0);
125         }
126 
Modify(string name, string mode, string text)127         public void Modify(string name, string mode, string text)
128         {
129             CheckDisposed();
130             StflApi.stfl_modify(f_Handle, name, mode, text);
131         }
132 
Dump(string name, string prefix, int focus)133         public string Dump(string name, string prefix, int focus)
134         {
135             CheckDisposed();
136             return StflApi.stfl_dump(f_Handle, name, prefix, focus);
137         }
138 
Reset()139         public void Reset()
140         {
141             CheckDisposed();
142             Dispose();
143             StflApi.stfl_reset();
144         }
145 
ProcessEvent(string @event)146         protected virtual void ProcessEvent(string @event)
147         {
148             OnEventReceived(new EventReceivedEventArgs(@event));
149             switch (@event) {
150                 case null:
151                 case "TIMEOUT":
152                     return;
153                 case "RESIZE":
154                     OnResized(EventArgs.Empty);
155                     return;
156             }
157             ProcessKey(@event);
158         }
159 
OnEventReceived(EventReceivedEventArgs e)160         protected virtual void OnEventReceived(EventReceivedEventArgs e)
161         {
162             if (EventReceived != null) {
163                 EventReceived(this, e);
164             }
165         }
166 
OnResized(EventArgs e)167         protected virtual void OnResized(EventArgs e)
168         {
169             if (Resized != null) {
170                 Resized(this, e);
171             }
172         }
173 
ProcessKey(string key)174         protected virtual void ProcessKey(string key)
175         {
176             CheckDisposed();
177             string focus = StflApi.stfl_get_focus(f_Handle);
178             OnKeyPressed(new KeyPressedEventArgs(key, focus));
179         }
180 
OnKeyPressed(KeyPressedEventArgs e)181         protected virtual void OnKeyPressed(KeyPressedEventArgs e)
182         {
183             if (KeyPressed != null) {
184                 KeyPressed(this, e);
185             }
186         }
187 
CheckDisposed()188         void CheckDisposed()
189         {
190             if (!Disposed) {
191                 return;
192             }
193             throw new ObjectDisposedException(GetType().Name);
194         }
195     }
196 }
197