Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C#

Custom controls with MonoDevelop and GTK#

Rate me:
Please Sign up or sign in to vote.
4.40/5 (9 votes)
18 May 2008GPL35 min read 112.2K   1.7K   32  
Movable controls with customized rendering on Linux with Mono.
// MVObject.cs
// 
// Copyright (C) 2008 Olivier Lecointre - Cadexis
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System;
using Gtk;

namespace GtkControl.Control
{
	
	public class MVObject : Gtk.DrawingArea 
	{
		Pango.Layout titleLayout;
		Gtk.DrawingArea da;
		string parentName = "";
		string caption = "";
		Gtk.Menu popup = null;
		string body = "";
		int width = 0;
		
		public MVObject(string pName, string cap)
		{

			popup = new Gtk.Menu();
			
			Gtk.MenuItem text1 = new MenuItem("Test1");
			text1.Activated+=new EventHandler(Menu1Clicked);
			Gtk.MenuItem text2 = new MenuItem("Test2");
			text2.Activated+=new EventHandler(Menu2Clicked);
			
			popup.Add(text1);			
			popup.Add(text2);
			
			parentName = pName;
			caption = cap;
			width = caption.Length*8+10;
			
			this.Name = parentName+"MVObject";
			
			this.SetSizeRequest(width,40);
			
			titleLayout = GetLayout(caption);
		}
		
		public void ShowMenu()
		{
			if (popup!=null)
			{
				popup.Popup();
				popup.ShowAll();
			}
		}
		
		public void Edit()
		{
			body="Edit";
			this.QueueDraw();
		}
		
		public string Caption
		{
			get
			{
				return caption;
			}
		}
		
		public void Redraw()
		{
			Console.WriteLine("Redrawing");
			this.QueueDraw();
		}
		
		private Pango.Layout GetLayout(string text)
		{
			Pango.Layout layout = new Pango.Layout(this.PangoContext);
			layout.FontDescription = Pango.FontDescription.FromString("monospace 8");
			layout.SetMarkup("<span color=\"black\">" + text + "</span>");
			return layout;
		}
		
		protected void Menu1Clicked(object sender, EventArgs args)
		{
			Console.WriteLine("Test");
			body = "Test1";
			this.QueueDraw();
		}
		
		protected void Menu2Clicked(object sender, EventArgs args)
		{
			Console.WriteLine("Test");
			body = "Test2";
			this.QueueDraw();
		}
		
		protected override bool OnExposeEvent (Gdk.EventExpose args)
		{	
			Gdk.Window win = args.Window;
			Gdk.Rectangle area = args.Area;
			
			
			win.DrawRectangle(Style.DarkGC(StateType.Normal), true, area);
			win.DrawRectangle(Style.BlackGC,false,area);
			win.DrawRectangle(Style.MidGC(StateType.Normal),true,0,15,1000,1000);
			win.DrawLine(Style.BlackGC,0,15,1000,15);			
			win.DrawLayout(Style.BlackGC,2,2,titleLayout);

			if (!string.IsNullOrEmpty(body))
			{
				win.DrawLayout(Style.BlackGC,2,17,GetLayout(body));
			}
			return true;
		} 
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions