- You are here: developers
- articles
- api overview
API Overview
Introduction
The MonoDevelop API is extensive and still changing. This article provides an overview of this API, explaining where to find the most important services and how to use them.
The API is split in three layers:
- The Core layer provides basic services such as logging, progress monitoring or file management which are used through the whole IDE.
- The Projects layer implements the project object model. It provides methods for reading, writing and building projects, as well as a parsing service.
- The top layer is the IDE itself.
It is worth noting that the Core and Projects layers are not tied to the IDE application, so it is possible to create independent applications which make use of those services.
Core Services
Add-in Engine
The add-in engine API is explained in detail in the Mono.Addins Reference Manual.
Information about installed assemblies, frameworks and runtimes
The object MonoDevelop.Core.Runtime.SystemAssemblyService provides methods for getting installed frameworks and runtimes. Once you have a TargetRuntime object you can query information about installed packages and add-ins. Using this API you can:
- Get a list of installed runtimes.
- Get a list of installed frameworks, for each runtime.
- Get a list of packages installed for a given runtime.
- Get the list of assemblies of a package
- Given an assembly name, find the location of the assembly, or the package providing it
- Given a partial assembly name, get the full name
Here are some classes you can use:
Localization
Add-ins must use add-in localizers for translating strings in the code. See the Getting Localized Strings of the Mono.Addins reference manual to learn how to do it.
The class MonoDevelop.Core.GettextCatalog can be used to get translated strings in the main IDE solution.
Logging
Global Configuration Properties
Parsing Strings with Tags
The MonoDevelop.Core.StringParserService class can be used to parse strings which contain tags and replace them with actual values. For example:
var values = new Dictionary<string,object> ();
values ["name"] = "Novell Inc";
values ["year"] = 2010;
string result = StringParserService.Parse ("Copyright (c) ${year} ${name)", values);
// The result will be: "Copyright (c) 2010 Novell Inc"
This service is explained in detail in the String Parser Service article.
Execution of Processes and External Commands
The object MonoDevelop.Core.Runtime.ProcessService offers several ways of starting processes.
Running a process
ProcessService has many overloads of the method StartProcess which allows running a process. Those overloads make it easier to collect output from the process.
Running a managed process
To execute an assembly, instead of starting a "mono" process, the best option is to use the TargetRuntime.ExecuteAssembly method. For example:
Runtime.SystemAssemblyService.CurrentRuntime.ExecuteAssembly ("someAssembly.exe", "some args");
This method allows running an assembly using a specific runtime, and will work both for Mono and MS.NET runtimes.
Creating an object running outside the main MonoDevelop process
The CreateExternalProcessObject method can be used to create an object which runs outside the main MonoDevelop process. ProcessService takes care of creating the external process and setting up the remote comunication. The methods takes as parameter the type of the object to create, which must be of type MonoDevelop.Core.Execution.RemoteProcessObject.
Working with the File System
Working with Text Files
The MonoDevelop.Projects.Text namespace has several classes which help working with text files.
Progress Monitoring and Asynchronous Operations
Many methods in the MonoDevelop API take as parameter a progress monitor, an object implementing IProgressMonitor. Those are usually methods which perform long operations and which use this object to provide feedback about the progress of the operation.
There are also several methods in the API which execute asynchronously. Those methods sometimes take an IProgressMonitor as parameter (where progess will be reported) and return an IAsyncOperation object, which the caller can use to control the execution of the operation. For example, it allows canceling the operation or waiting until completed.
The MonoDevelop.Core.ProgressMonitoring and MonoDevelop.Core.Gui.ProgressMonitoring namespaces provide several useful classes for implementing progress monitors and for combining them.
NullProgressMonitor
This is the most basic implementation of a progress monitor. It doesn't report progress anywere, although it is fully functional. For example, you can get an IAsyncOperation object from the AsyncOperation property you can use to cancel the operation.
This class can be also used as a base class for more complex progress monitors.
MonoDevelop.Core.ProgressMonitoring
SimpleProgressMonitor
ConsoleProgressMonitor A progress monitor which logs output to the standard output.
MonoDevelop.Core.ProgressMonitoring
SynchronizedProgressMonitor
AggregatedProgressMonitor This class can be used to aggregate several progress monitor in a single one. For example, you can use it to show the progress in several monitors at the same time, such as a log window and the status bar. You would create an AggregatedProgressMonitor instance, and then you would register the other monitors on it. Then you would provide the aggregated monitor to the method running the operation.
AsyncOperation An implementation of IAsyncOperation. You can use it when implementing your own progress monitor.
LogTextWriter This TextWriter subclass allow chaining several text writers. All text written to the writter will be replicated to all chained writters.
AggregatedOperationMonitor
This class can be used to synchronize asynchronous operations. For example, when implementing a method which takes an IProgressMonitor as parameter and which needs to internally execute an asynchronous operation and wait for it to complete. You would do something like this:
public void DoLongLastingOperation (IProgressMonitor monitor) { // ... AggregatedOperationMonitor om = new AggregatedOperationMonitor (monitor); IAsyncOperation aop = StartSomeAsyncOperation (); om.AddOperation (aop); om.WaitForCompleted (); om.Dispose (); // .... }
In this example, DoLongLastingOperation needs to internally execute an async operation. The AggregatedOperationMonitor object wraps the progress monitor of the method, and will propagate cancel requests to the child async operations.
General Purpose Data Serializer
MonoDevelop provides a data serializer which can be used by add-ins to serialize data to files. This serializer is also used by the project service to read and write projects and solutions. This API is available in the MonoDevelop.Core.Serialization namespace.
See Using The Data Serializer for more information.
Integration with the Desktop
Core GUI Services
Image and Stock Icon Management
The class MonoDevelop.Core.Gui.ImageService provides several methods for managing images and stock icons:
Displaying Alerts and other Message Dialogs
The Message Service
The class MonoDevelop.Core.Gui.MessageService provides methods for showing alert and other message dialogs.
Here are some of the most useful methods:
Some of those methods take a MonoDevelop.Core.Gui.AlertButton as parameter. There are several pre-defined alert buttons, such as AlertButton.Ok, AlertButton.Close, AlertButton.Cancel, etc (all defined as static members of AlertButton). You can also create your own AlertButton instances with custom icons and response codes.
The arguments for the Confirm and AskQuestion methods can also be provided using objects of type ConfirmationMessage and QuestionMessage respectively. Using those objects it is possible to specify the same arguments available in the other overloads, but they allow specifying additional options:
Here is an example:
public static void DeleteFiles (string[] files)
{
// Creates the confirmation dialog data object and specifies that
// it has to show the "Apply to all" checkbox
ConfirmationMessage msg = new ConfirmationMessage ();
msg.ConfirmButton = AlertButton.Delete;
msg.ApplyToAllButton = true;
foreach (string file in files) {
// Shows the confirmation dialog to the user
// If the user activated the "Apply to all" checkbox,
// this call will not show the dialog and will return the value
// selected when the option was activated.
msg.Text = string.Format ("Do you want to delete the file '{0}'?", file);
if (MessageService.Confirm (msg))
File.Delete (file);
}
}
Additional Dialogs
There are also a couple of dialogs which can be used to display messages:
Common Widgets and Dialogs
MonoDevelop implements several widgets which can be reused by add-ins:
Project Model Services
The Projects layer implements the project object model. It provides methods for reading, writing and building projects, as well as a parsing service.
Creating, Reading, Writing and Exporting Projects
MonoDevelop.Projects.Services
ProjectService
MonoDevelop.Ide.Gui
IdeApp.Services.ProjectService
This class provides several methods for:
- Creating new projects, using information from a template.
- Reading and writing solution items (including projects) and workspace items (including solutions and workspaces).
- Checking if a file is a known project or solution.
- Exporting items from one format to another format.
- Getting and setting the default file format.
- Getting and setting the default target framework.
Generating and Manipulating Code
CodeRefactorer
This class can be used to generate code in new or existing classes. The api is language-agnostic, although not all operations are available for all languages. The most important operations are:
- Create a class.
- Rename a class or a class member.
- Find all references to a class or class member.
- Add a member to a class.
- Remove a member from a class.
- Implement overridable or interface members.
- Add a custom attribute to a class.
Querying information about types
Management of Project and Solution Policies
Language Bindings
IDE Services
Working with the Main Window of the IDE
The obect MonoDevelop.Ide.Gui.IdeApp.Workbench provides all you need to show and manage information in the main window of the IDE. It allows opening and managing documents (files), pads (such as the solution pad or the errors pad), and the main window layout.
Opening and Managing Documents
The obect MonoDevelop.Ide.Gui.IdeApp.Workbench provides several operations for working with documents:
Accessing the Content and Controlling Opened Documents
To access the content of a document you need to get a MonoDevelop.Ide.Gui.Document object. You can get it using some of the properties and methods of the MonoDevelop.Ide.Gui.IdeApp.Workbench object (see above).
Here are some of the most useful methods and properties of the Document class:
Managing Pads
The Workbench object provides the following methods for managing pads:
The Status Bar
The obect MonoDevelop.Ide.Gui.IdeApp.Workbench.StatusBar provides several operations for showing information in the status bar:
It is also possible to show a progress bar in the status bar by using a status bar progress monitor. You can get such monitor from MonoDevelop.Ide.Gui.IdeApp.Workbench.ProgressMonitors.GetStatusProgressMonitor ().
The CreateContext() method returns an object of type StatusBarContext which can be used to show messages in the status bar, just like when you call ShowMessage directly on the status bar. The difference is that when the StatusBarContext object is disposed, the message shown through the context will be cleared, restoring any message previous to the context creation.
Status bar contexts are useful for example to display temporary progress status, or to show some information while the user is hovering over some visual component.
Here is an example:
// Display "Ready" in the status bar
IdeApp.Workbench.StatusBar.ShowMessage ("Ready");
StatusBarContext ctx = IdeApp.Workbench.StatusBar.CreateContext ();
using (ctx) {
ctx.ShowMessage ("Doing some work");
// The status bar now shows "Doing some work"
...
}
// When the context is disposed, the old message is restored,
// so the status bar will now show "Ready" again
Changing the Layout of the Main Window
The following methods are defined in the MonoDevelop.Ide.Gui.IdeApp.Workbench object.
Accessing the Project Model
The MonoDevelop.Ide.Gui.IdeApp.Workspace object provides many methods, properties and events for working with the current loaded solution (or solutions, since MonoDevelop allows opening several solutions at once). Here are some of the most important members:
Project Operations: Loading, Saving, Building, Displaying Options
The Project Object Model provides methods for loading, building and do all sort of operations with projects and solutions. However, those are low level operations which don’t provide direct feedback to the user.
The MonoDevelop.Ide.Gui.IdeApp.ProjectOperations object provides several methods for doing the same set of operations, and it provides feedback to the user through the status bar, output pads, or other IDE resources.
The most important operations you can do with this object are:
Some of those operations (such as Build) are asynchronous. All asynchronous operation return an IAsyncOperation object that can be used to control the operation. It can be used for example to cancel it or to wait until it is complete.
Displaying Progress and Process Output
If you need to perform a long operation in the background, the IDE offers several stock progress monitors you can use to show feedback to the user. Those progress monitors are provided by the MonoDevelop.Ide.Gui.IdeApp.Workbench.ProgressMonitors object. You can use the following methods to create new monitors:
There are also other classes which can be used to show progress in a dialog:
IDE Preferences
The MonoDevelop.Ide.Gui.IdeApp.Preferences object has several properties for getting and setting IDE preferences. It has also events which are fired when preferences are changed.