Toutes les langues

Simple Gedit plugin

No votes yet
/**
* You'll need to grab the attached files and remove the .txt at the end of them in order to use this.
* I put both the vapi and deps file in /usr/share/vala/vapi/ to have them readily available.  You can also keep them in your project folder if you want, * but you'll need to update the compile line to reflect that
* the compiled .so and gedit-plugin files go in ~/.gnome2/gedit/plugins/
* Sample makefile is also attached.
* 
* Displays a button on the top Gedit panel that displays a color dialog when clicked.  Formatted result is inserted at the cursor.
**/
using Gee;
using Gtk;
class CSPlugin : Gedit.Plugin
{
	public Gee.ArrayList<CSInstance> instances = new Gee.ArrayList<CSInstance>();
	public Gedit.Tab active_window;
	public override void activate(Gedit.Window window){
		CSInstance new_instance=new CSInstance(window,this);
		instances.add(new_instance);
		active_window=window.get_active_tab();
	}
	CSInstance? find(Gedit.Window window)
	{
		foreach (CSInstance i in instances)
		{
			if(i.window == window)
			{
				return i;
			}
		}
		return null;
	}
 
	public override void update_ui(Gedit.Window window)
	{
		if(active_window!=window.get_active_tab())
		{
			CSInstance c=find(window);
			c.ui_update();
		}
		active_window=window.get_active_tab();
	}
	public override void deactivate(Gedit.Window window)
	{
		CSInstance i=find(window);
		i.deactivate();
		instances.remove(i);
		active_window=null;
	}
}
 
class CSInstance : GLib.Object
{
	public weak Gedit.Window window;
	public weak CSPlugin plugin;
	private uint ui_id;
 
	public CSInstance(Gedit.Window window,CSPlugin plugin)
	{
		this.window=window;
		this.plugin=plugin;
 
		string ui_string="<ui><toolbar name=\"ToolBar\"><separator /><toolitem name=\"Color\" action=\"Color\" /></toolbar></ui>";
		Gtk.ActionGroup actions=new Gtk.ActionGroup("Color");
		Gtk.UIManager manager=window.get_ui_manager();
		Gtk.Action action=new Gtk.Action("Color","Color","Insert Color","gtk-execute");
		action.set_accel_path("<Ctrl><Shift>c");
		action.activate+=get_color;
		actions.add_action(action);
		manager.insert_action_group(actions,-1);
		ui_id=manager.add_ui_from_string(ui_string,(ssize_t)ui_string.size());
	}
	public void get_color()
	{
		ColorSelectionDialog dlg=new ColorSelectionDialog("Choose color");
		Gdk.Color cx;
		int res=dlg.run();
		switch(res)
		{
			case ResponseType.OK:
				ColorSelection sel=(ColorSelection)dlg.color_selection;
				sel.get_current_color(out cx);
				dlg.destroy();
			break;
			default:
				dlg.destroy();
				return;
			break;
		}
 
		string final="#%02x%02x%02x".printf(cx.red >> 8,cx.blue >> 8,cx.green >> 8);
		window.get_active_document().insert_at_cursor(final,7);
	}
	public void deactivate()
	{
		//Clean up
	}
}
[ModuleInit]
public Type register_gedit_plugin (TypeModule module) {
    return typeof (CSPlugin);
}
Fichier attachéTaille
gedit-2.20.vapi_.txt35.67 Ko
gedit-2.20.deps_.txt28 octets
colors.gedit-plugin.txt194 octets
Makefile.txt825 octets