mono:gtkSharp ~ Sliding Text at Drawing Area (Pango.Layout)
PROJECT REFERENCES:
CODE:
using System;
using Gtk;
using System.Xml;
//REFERENCE LINK: http://www.mono-project.com/Pango:Beginners
public class sliding_MyWindow : Window
{
Pango.Layout layout;
Gtk.DrawingArea da;
int width = 500;
int height = 500;
int slideIndex=500;
int arrayIndex=1;
private string[] newsArray;
public sliding_MyWindow()
:base("sliding_MyWindow")
{
this.SetDefaultSize (width, height);
this.DeleteEvent += new DeleteEventHandler (Onsliding_MyWindowDelete);
da = new Gtk.DrawingArea();
da.SetSizeRequest(width, height);
layout = new Pango.Layout(this.PangoContext);
layout.Width = Pango.Units.FromPixels(width);
layout.Wrap = Pango.WrapMode.Word;
layout.Alignment = Pango.Alignment.Left;
layout.FontDescription = Pango.FontDescription.FromString("Arial bold 14");
getNews("http://www.cnnturk.com/servisler/rss/turkiye.rss");
GLib.Timeout.Add(45, new GLib.TimeoutHandler(updateText));
this.Add(da);
this.ShowAll();
}
bool updateText()
{
slideIndex=slideIndex-2;
da.GdkWindow.Clear();
da.GdkWindow.DrawLayout(da.Style.TextGC (StateType.Normal),slideIndex,5,layout);
if(slideIndex==0)
{
layout.SetMarkup(newsArray[arrayIndex]);
arrayIndex++;
slideIndex=500;
}
return true;
}
private void getNews(string newsAddress)
{
try
{
XmlDocument rssDoc = new XmlDocument();
System.Net.HttpWebRequest req = (System.Net.HttpWebRequest) System.Net.WebRequest.Create(newsAddress);
req.Timeout = 1000; // milliseconds
System.Net.WebResponse res = req.GetResponse();
System.IO.Stream responseStream = res.GetResponseStream();
rssDoc.Load(responseStream);
responseStream.Close();
XmlNodeList _ngroups = rssDoc.GetElementsByTagName("item");
if(_ngroups.Count > 0)
{
newsArray = new string[_ngroups.Count];
byte spaces0 = 8, spaces1 = 9, spaces2 = 10, spaces3 = 11, spaces4 = 12, spaces5 = 13;
for(int x = 0; x < newsArray.Length; x++)
{
for (int y = 0; y< _ngroups[x].ChildNodes.Count; y++)
{
switch(_ngroups[x].ChildNodes[y].Name)
{
case "title":
newsArray[x] = _ngroups[x].ChildNodes[y].InnerText + " : ";
break;
case "description":
newsArray[x] += _ngroups[x].ChildNodes[y].InnerText;
break;
}
newsArray[x] = newsArray[x].Replace('\n', ' ').Replace((char)spaces0, ' ').Replace((char)spaces1, ' ').Replace((char)spaces2, ' ').Replace((char)spaces3, ' ').Replace((char)spaces4, ' ').Replace((char)spaces5, ' ');
}
}
}
else
{
Console.WriteLine("_ngroups.Count < 0");
}
if(newsArray!=null)
{
if(newsArray.Length>0)
{
layout.SetMarkup(newsArray[0]);
Console.WriteLine(newsArray[0]);
}
}
}
catch(Exception e)
{
Console.WriteLine("error in getNews():"+e);
}
}
void Onsliding_MyWindowDelete (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}
public static void Main()
{
Application.Init();
new sliding_MyWindow();
Application.Run();
}
}
OUTPUT:
Advertisement



