All pastes #13376 Raw Edit

Untitled

public text v1 · immutable
#13376 ·published 2005-06-04 17:46 UTC
rendered paste body
Contenidos de ManPage.cs

using System;
using System.IO;
using System.Text;
using ICSharpCode.SharpZipLib.GZip;

namespace SuperMan
{

	public class ManPage
	{
		
		int mannumber;

		string manname, mancontent;

		public int PageNumber {
			get {
				return mannumber;
			}
		}

		public string PageName {
			get {
				return manname;
			}
		}
			
		public string Content {
			get {
				return mancontent;
			}
		}
		
		public ManPage (string ManFile)
		{
			/*
		 	* We get the man page number and the name of the man page
		 	* the manpage filename format is manpagename.manpagenumber
		 	* 
		 	*/
			
			string file = Path.GetFileNameWithoutExtension (ManFile);
			int dotIndex = file.LastIndexOf ('.');
			manname = file.Substring (0, dotIndex);
			mannumber = int.Parse (file.Substring (dotIndex + 1));
					
			/*
		 	* TODO: If the file it's not gziped just read the file
		 	* 
		 	*/
					
			GZipInputStream GZipStream = new GZipInputStream (File.OpenRead (ManFile));
			int BytesReaded;
			Decoder UTF8Decoder = Encoding.UTF8.GetDecoder();
			StringBuilder ManBuilder = new StringBuilder();
			do
			{
				byte[] Chunk = new byte[2048];
				BytesReaded = GZipStream.Read (Chunk, 0, Chunk.Length);
				char[] ManPiece = new char[UTF8Decoder.GetCharCount (Chunk, 0, BytesReaded)];
				UTF8Decoder.GetChars (Chunk, 0, BytesReaded, ManPiece, 0);
				ManBuilder.Append (ManPiece);
			} while (BytesReaded > 0);
			mancontent = ManBuilder.ToString();
		}
	}
}



Contenido de MainWindow.cs


using System;
using Gtk;

namespace SuperMan 
{

	public class MainWindow : Window 
	{
		public static void Main (string[] args) {
			Application.Init();
			new MainWindow (new ManPage (args[0]));
			Application.Run();
		}	

		void OnDelete (object sender, DeleteEventArgs args) {
			Application.Quit();
		}
		
		public MainWindow (ManPage page) : base ("SuperMan") {
			DeleteEvent += new DeleteEventHandler (OnDelete);
			VBox caja = new VBox();
			Label label = new Label ();
			label.Markup = "<b>Nombre:</b> " + page.PageName;
			caja.PackStart (label, false, false, 5);
			label = new Label ();
			label.Markup = "<b>Pagina:</b> " + page.PageNumber;
			caja.PackStart (label, false, false, 5);
			TextView tview = new TextView ();
			tview.Editable = false;
			string[] ManLines = page.Content.Split ('\n');
			int startOffset, endOffset;
			startOffset = endOffset = 0;
			for (int i = 0; i < ManLines.Length; i++)
			{
				string Line = ManLines[i];
				Line = Line.Replace ("\\-", "-");
				if (Line.StartsWith (".\\\""))
					continue;
				if (Line.StartsWith (".B ")) {
					TextTag tag = InsertTextAndTagToBuffer (tview.Buffer, Line.Substring (3), ref startOffset, ref endOffset);
					Pango.FontDescription fdesc = new Pango.FontDescription ();
					fdesc.Weight = Pango.Weight.Bold;
					tag.FontDesc = fdesc;
				}
				else if (Line.StartsWith (".I ")) {
					TextTag tag = InsertTextAndTagToBuffer (tview.Buffer, Line.Substring (3), ref startOffset, ref endOffset);
					tag.Style = Pango.Style.Italic;
				}
				else
					tview.Buffer.InsertAtCursor (Line + "\n");
			}
			ScrolledWindow tviews = new ScrolledWindow ();
			tviews.Add (tview);
			caja.PackStart (tviews, true, true, 0);
			Add (caja);
			ShowAll ();
		}

		TextTag InsertTextAndTagToBuffer (TextBuffer buffer, string Text, ref int startOffset, ref int endOffset)
		{
			startOffset = buffer.EndIter.Offset;
			buffer.InsertAtCursor (Text);
			endOffset = buffer.EndIter.Offset;
			buffer.InsertAtCursor ("\n");
			TextTag tag = new TextTag (startOffset.ToString ());
			buffer.TagTable.Add (tag);
			TextIter StartIter = buffer.GetIterAtOffset (startOffset);
			TextIter EndIter = buffer.GetIterAtOffset (endOffset);
			buffer.ApplyTag (tag, StartIter, EndIter);
			return tag;
		}
	}
}