Delphi Prism in MonoDevelop at a glance

Our beta users (marc already did mention you can apply for the Prism beta here, didn’t he?) are already playing around with this for some time now, and we are going to ship it with the upcoming release: the Delphi Prism MonoDevelop integration. Marc already mentioned this some time ago and a lot has happened since then. This article should give you a ‘what is it and how could I start?’ introduction into Delphi Prism in MonoDevelop.

MonoDevelop LogoMonoDevelop is a free and open source .NET IDE. and has its roots in the also free and open-source SharpDevelop .NET IDE. With SharpDevelop still being a Windows-only IDE using Windows Forms, MonoDevelop (or in short MD, as we call it internally) was ported to Gtk# and massively extended in regards to cross-platform compatibility. It is now able to work smoothly on Linux, Mac OS X and, of course, Windows. You can use your solutions from Visual Studio with MD and vice versa, so you are able to have a single source solution for different platforms.

With the Delphi Prism MonoDevelop integration we target mainly the Mac and then Windows. For Windows we have the Visual Studio integration and we ship the Visual Studio Shell which is, in fact and to be honest, a far more comfortable and powerful IDE. That said: Of course we want to make sure that MonoDevelop on Windows (MD/Win) also works like a charm, but MD/Mac is of a higher priority because you don’t have an alternative IDE there.

The MD/Mac flavor of our MonoDevelop integration comes in form of a complete Mac application. It looks like it’s the original MonoDevelop, but additionally contains our Delphi Prism plug-in. That means you will have to install the MonoDevelop prerequisite by yourself (which is Mono >= 2.4). You can grab it here.

After startup, the Delphi Prism will tell you that it is not licensed and ask you to register your license (if you don’t have a Delphi Prism license yet you can get your copy and a 30-day trial key from the Delphi Prism download page).

When selecting “Start a new Solution” you have several options: The first one is a Console application, but you also have the choice to work with ASP.NET projects, create iPhone or iPod touch applications (be sure to have the iPhone SDK and at least the Trial of MonoTouch ( installed for this) and you can build Moonlight applications (the Mono alternative to Silverlight).

For this article we’re going for an iPhone application. Not because we want you to go and buy you into the iPhone Developer Program and additionally buy MonoTouch from Novell, but just because it’s a neat thing to show ;)

In this dialog you see the options I set for the Hello world iPhone application. Be sure to meet the requirements (MD prereqs, iPhone SDK and MonoTouch) if you want to follow this article in code yourself. The next thing you’ll see is the MD editor with the project opened and ready to start.

We’re not going too deep into the MVC approach of the iPhone development, which is also the key to general Cocoa development, but you need to know that the UI is separated from your code. Unlike in Delphi or Windows forms where controls dropped on the form are automatically properties of your form class, you need to define which controls and events (called Actions) are visible to your code using defined classes.

The first thing we’re creating is the GUI. No demo without a ‘Hello World’, so we simply want to display the text entered in a text box on a label when the user presses a button. In the ‘Solution’ part of the MonoDevelop IDE to your left we expand the project and double-click on the ‘MainWindow.xib’ file. This should launch a program called ‘Interface Builder’ which belongs to the iPhone SDK. The .xib file is somewhat compareable to .dfm files you know from Delphi or like .Designer.pas files which define the layout of a Windows Forms form. The following part of designing the GUI and connecting the outlets and signals is in fact the most difficult if you never worked with interface builder. Because of that I also created a short video of this walk-through so that you have the chance to actually see what I’m going to describe now.

On the ‘Library’ window you select the ‘Objects’ part on top and use the search box on the bottom to search for a ‘Toolbar’. Take it and drop it on the window and let it dock on the top. Then do the same for a ‘Text Field’ and a ‘Label’. You can drag them to the width that pleases you. Now that all controls are in place it’s time to make them accessible. In the ‘Library’ Windows please select ‘Classes’ on top and type ‘App’ in the search box. Then you can select the ‘AppDelegate’ class which is the interface between our application code and the GUI. Still in the library window select ‘Outlets’ in the lower half. We’re going to define accessors to the text field and the label now by clicking on the ‘+’ below the listed outlets and enter text and repeat that for label. Now we’re going to make our click event accessible by selecting ‘Actions’ instead of outlets and add the action ‘onClick:’ (mind the colon!) to the AppDelegate class.

The next task is to connect our outlets and the action to the controls. To do this, you simply select the toolbar button (labeled ‘Item’) in the window. In the property window you select the connections (white arrow on blue circle). There is an area called ‘Sent actions’ with an entry for ‘Selector’. Use the mouse and click in the circle to the right and drag this on the yellow ‘App Delegate’ box in the ‘MainWindow.xib’ window. On dropping you are able to select the action you want to link with the sent ‘selector’ action of the item. For both the text field and the label drag the ‘New referencing outlet’ circle on the App delegate and link them to the text and label outlets we defined before. Now you can press Apple+S to save the MainWindow.xib file.

MonoDevelop generates the code behind for us automatically. Please open the ‘MainWindow.xib.designer.pas’ file (expand the ‘MainWindow.xib’ node in your solution to find it) to have a loot at the generated code. Locate the

method onClick(sender: MonoTouch.UIKit.UIBarButtonItem);partial;empty;

line and copy it (withouth the empty;). Pase that in the ‘private’ section of the ‘AppDelegate’ class in the Program.pas file and double check that the empty; part did not get pasted too. This is the method that get’s called when the event (sorry, Signal ;-) ) fires from the GUI. In the implementation part complete the partial method and fill in it’s body:

method AppDelegate.onClick(sender: UIBarButtonItem);begin label.Text:= textbox.Text;end;

In fact that’s all. Compile the project using the building blocks icon in the toolbar of MD (or by pressing Apple-B). Your code will be compiled into an assembly and the assemblies then will automatically be compiled in to native code for the iPhone by the MonoTouch compiler. When clicking on ‘Run’ or ‘Debug’ the iPhone simulator application should pop up and run our Hello world application:

As you can see building an application for the iPhone using Delphi Prism is not only working, but you can make use of the full Cocoa API the iPhone SDK is offering you. Of course this only works if you have the iPhone SDK and MonoTouch, but to develop for the simulator as we just did, the free version of both is enough. You would only need to pay for the Apple developer program membership and for the MonoTouch licence if you’re going to deploy your programs on a physical device. In other words: Unless you want to start selling your application and earn money in return, you can work for free.

Here you can watch a little video that shows how the application was build:

Download this Video: as mp4 or in open ogg format.

This was a first feature preview of Delphi Prism in MonoDevelop and we hope you like it and stay tuned for more.