Un editor sencillo con MONO para CSharp

un-editor-sencillo-con-mono-para-csharp

Hola de nuevo aquí­ les presento un editor que nos ayudará un poco a escribir código en C#. Se aceptan sugerencias … y mejoras.

Editor.cs
————————————————–
// Programa que muestra el editor visual.

using Glade;
using Gtk;
using System;
using System.IO;
using GtkSourceView;

class Editor {

[Widget] Window window1;
[Widget] MenuItem quit1, about1, ejecutar1, new1, open1, save1, save_as1;
[Widget] ScrolledWindow scrolledwindow1;
[Widget] Statusbar statusbar1;

FileSelection fs;
SourceView view;
SourceBuffer buffer;

Window g_win;
Button _btn;
Entry _ent;
VBox vbox;

static string file = “”;
bool activo = false;
const int id = 1;

public Editor () {

Application.Init ();

Glade.XML gxml = new Glade.XML (null, “interfaz.glade”, “window1″, null);

gxml.Autoconnect ( this );

// Agregando eventos …
quit1.Activated += new EventHandler ( On_Exit_Item_Activate );
about1.Activated += new EventHandler ( Acerca_de );
ejecutar1.Activated += new EventHandler ( Ejecutar );
new1.Activated += new EventHandler ( Nuevo );
open1.Activated += new EventHandler ( Abrir );
save1.Activated += new EventHandler ( Guardar );
save_as1.Activated += new EventHandler ( GuardarComo );
window1.DeleteEvent += new DeleteEventHandler ( Salir );

view = new SourceView (CreateBuffer ());
view.ShowLineNumbers = true;

scrolledwindow1.Add (view);

fs = new FileSelection (”Escoge fichero”);
fs.Response += new ResponseHandler (OnFileSelectionResponse);

statusbar1.Push (id, “Bienvenido”);

window1.Focus = scrolledwindow1;

window1.ShowAll ();

Application.Run ();
}

SourceBuffer CreateBuffer () {

SourceLanguagesManager manager = new SourceLanguagesManager ();
SourceLanguage lang = manager.GetLanguageFromMimeType (”text/x-csharp”);

buffer = new SourceBuffer (lang);
buffer.Highlight = true;
return buffer;
}

public static void Main () {

new Editor ();
}

void Salir (object o, DeleteEventArgs args) {

Application.Quit ();
Console.WriteLine (”Programa finalizado”);
}

void On_Exit_Item_Activate (object o, EventArgs args) {

Application.Quit ();
Console.WriteLine (”Programa finalizado”);
}

void Acerca_de (object o, EventArgs args) {

new PopUp ();
}

void Ejecutar (object o, EventArgs args) {

Console.WriteLine (”Agrega lo que quieras aki”);
}

void Nuevo (object o, EventArgs args) {

if(buffer.Text.Length != 0) {
buffer.Clear ();
file = “”;
window1.Title = “Editor C Sharp”;
activo = false;
statusbar1.Pop (id);
statusbar1.Push (1, “Nuevo buffer”);
}
else {
statusbar1.Pop (id);
statusbar1.Push (1, “Buffer Limpio”);
}
}

void Abrir (object o, EventArgs args) {

fs.Run ();
fs.Hide ();
}

void OnFileSelectionResponse (object o, ResponseArgs args) {

if (args.ResponseId == ResponseType.Ok) {

file = fs.Filename;

using (StreamReader sr = new StreamReader (file) ) {

buffer.Text = sr.ReadToEnd ();
window1.Title = file;
activo = true;
statusbar1.Pop (id);
statusbar1.Push (1, “Fichero cargado”);
}
}
}

void GuardarComo (object o, EventArgs args) {

Dibuja ();
activo = true;
}

void Guardar (object o, EventArgs args) {

if(activo == false) {
Dibuja ();
activo = true;
}

else {
if(file.Length > 0)
_Guardar (file);
}
}

void Dibuja () {

g_win = new Window (”Guardar”);
g_win.DeleteEvent += new DeleteEventHandler (Oculta);
g_win.SetPosition (WindowPosition.Center);

vbox = new VBox (false, 2);
vbox.Show ();

_ent = new Entry();
_ent.Show ();

_btn = new Button (”Guardar”);
_btn.Clicked += new EventHandler (GuardarNombre);
_btn.Show ();

vbox.PackStart(_ent, true, true , 0);
vbox.PackStart(_btn, true, true , 0);

g_win.Add (vbox);
g_win.ShowAll ();
}

void GuardarNombre (object o, EventArgs args) {

if(_ent.Text.Length > 0)
_Guardar(_ent.Text);

g_win.Hide ();
}

void _Guardar (string tmp) {

StreamWriter sw = new StreamWriter(tmp);
sw.Write (buffer.Text);
sw.Close();
window1.Title = tmp;
statusbar1.Pop (id);
statusbar1.Push (1, “Fichero guardado”);
}

void Oculta (object o, DeleteEventArgs args) {
g_win.Hide ();
g_win.Destroy ();
}
}

————————————————–

About.cs
————————————————–
// A Popup window … Acerca de
using System;
using Gtk;

class PopUp {

Gtk.Window win;
Fixed fxd;

public PopUp () {

win = new Gtk.Window (”Creditos”);
win.SetDefaultSize (184, 79);
win.Resizable = false;
win.SetPosition(WindowPosition.Center);
win.DeleteEvent += Window_Delete;

fxd = new Fixed ();
win.Add (fxd);

Image i = new Image (”images/Mono1.gif”);
fxd.Put (i, 0, 0);

Label lbl0 = new Label (”Hecho por:”);
fxd.Put (lbl0, 0, 100);

lbl0 = new Label (”GGC (gerardogc2378@yahoo.com.mx)”);
fxd.Put (lbl0, 0, 120);

lbl0 = new Label (”:-)”);
fxd.Put (lbl0, 0, 140);

win.ShowAll ();
}

void Window_Delete (object obj, EventArgs args) {

win.Hide ();
win.Destroy ();
}
}

————————————————–

Makefile
————————————————–
MCS=mcs
FLAGS= -pkg:gtk-sharp-2.0 -pkg:glade-sharp-2.0 -pkg:gtksourceview-sharp-2.0
RESOURCE= -resource:interfaz.glade
SOURCE= Editor.cs About.cs
CODE= -codepage:utf8

Main.exe: ${SOURCE}
${MCS} ${FLAGS} ${RESOURCE} ${SOURCE} ${CODE}

clean:
rm -rf Editor.exe
————————————————–

y por Último la interfaz hecha con GLADE-2

interfaz.glade
————————————————–

True Editor C Sharp GTK_WINDOW_TOPLEVEL GTK_WIN_POS_CENTER False 400 300 True False True False False GDK_WINDOW_TYPE_HINT_NORMAL GDK_GRAVITY_CENTER True False

True False 0

True GTK_PACK_DIRECTION_LTR GTK_PACK_DIRECTION_LTR

True _File True

True Ejecutar True

True gtk-apply 1 0.5 0.5 0 0

True gtk-new True

True gtk-open True

True gtk-save True

True gtk-save-as True

True

True gtk-quit True

True _Help True

True _About True

True gtk-about 1 0.5 0.5 0 0

0 False False

True False 0

True True GTK_POLICY_ALWAYS GTK_POLICY_ALWAYS GTK_SHADOW_NONE GTK_CORNER_TOP_LEFT

0 True True

True True

0 False False

0 True True

————————————————–

Obvio que esto es Software Libre no tengo que decirlo verdad?…

PD La imagen descargala de http://www.mono-project.com/Logos y crea un directorio llamado imágnes y colocarla ahií­ con el nombre respectivo. Utilizo la versión 1.1.15 de ls Suite de MONO, osea la penúltima.

Como sugerencia visita la siguiente página:

http://www.ecma-international.org/publications/standards/Ecma-334.htm


Deja un comentario