The Sample Application
Now it's time to try out MonoDevelop. If you created your own VM, copy the source code to the VM's filesystem. The sample app source code is available online; see "Resource Center," page 5. Download the source code and unzip it to a flash drive. Click the Safely Remove Hardware icon in your task bar and remove the flash drive from Windows. Go to Server. Select VM/Removable Devices and click on your device in the cascading menu. In the File Browser, select Edit/Select All and Edit/Copy. Click the Home toolbar button. Click Edit/Paste and the source code will be copied to your filesystem.
Select Applications/Programming/MonoDevelop. Click File/Open and navigate to /home/user/DigitsOfPi. Open the solution file DigitsOfPi.mds. Click Project/Run (or F5) to launch the application. DigitsOfPi (Figure 1) calculates p to a ludicrous precision, thanks to an algorithm written by Fabrice Bellard (bellard.org/pi/pi.c). Specify the Number of Digits and click Calculate. DigitsOfPi uses Gtk#, a managed interface into the Gnome Toolkit, instead of Windows Forms or Windows Presentation Foundation. I chose to use Gtk# for two reasons: Most Linux users prefer the Gtk look-and-feel, and Windows Forms is not available from MonoDevelop without extra setup and configuration.
I had some problems getting the busy cursor to work properly during the lengthy calculation. I used the GdkWindow.Cursor property to set the cursors (Listing One). When I ran the program, I never saw the busy cursor because the lengthy computations prevented the GUI thread from updating the window. There are at least two ways to address this issue. If you implement lengthy computations as Idle Handlers, the busy cursor will display properly. Just put your lengthy calculations in a method and return a value of false. Make this method an Idle Handler by passing it to Glib.Idle.Add. The method will be called the next time the GUI thread is idle. Because the method returns false, it only runs once, rather than after each idle period (Listing Two). When I implemented the calculations as an Idle Handler, the busy cursor displayed correctly. But when I added a Status Bar and a Progress Bar, I noticed that progress updates were not displayed during the calculations. The solution was to call ProcessEvents after updating the Progress Bar's Fraction property. ProcessEvents executes all pending GUI events, allowing the window to redraw completely (Listing Three).
protected virtual void CalculateButtonClicked (object sender, System.EventArgs e) { GdkWindow.Cursor = new Gdk.Cursor(Gdk.CursorType.Watch); // lengthy computation GdkWindow.Cursor = new Gdk.Cursor(Gdk.CursorType.LeftPtr); }
protected virtual void CalculateButtonClicked (object sender, System.EventArgs e) { GLib.Idle.Add(OnIdleCalculatePi); } private bool OnIdleCalculatePi() { GdkWindow.Cursor = new Gdk.Cursor(Gdk.CursorType.Watch); // lengthy computation GdkWindow.Cursor = new Gdk.Cursor(Gdk.CursorType.LeftPtr); // Return false to ensure that this callback // is not automatically called again at the next Idle moment. return false; }
progressbar.Fraction = Math.Min((double) currentDigit / (double) totalDigits, 1.0); GuiUtils.ProcessEvents(); ... public static void ProcessEvents() { while (GLib.MainContext.Pending()) { Gtk.Main.Iteration(); } }
Because I'm a newcomer to Gtk#, I relied heavily on online help and tooltips to figure out the Gtk# API. Note, when displaying pop-up help for overloaded methods (Figure 2), use the left and right arrow keys to scroll through the overloads.
To create your own Gtk# application, select MonoDevelop's File/New Solution menu item. Go to the C# templates and click "Gtk# 2.0 Project." After the project is created, double-click MainWindow.cs and click the Designer button to graphically edit your program's GUI. In the toolbox window, click the Widgets and Containers triangle icons to display the controls and control containers available to Gtk# applications. Widgets can't be added directly to a window. First add a Container to the window, then add Widgets to the Container. Drag a VBox container and drop it on your window. Then drag Widgets and drop them into the VBox. The VBox container arranges the controls in a vertical grid. Containers can contain other Containers. For example, the DigitsOfPi program's GUI consists of a VBox with four rows. Row 1 contains the menus, row 2 contains an HBox that contains the number of digits spin button and the Calculate button, and so on.