Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

.NET Development on Linux


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.

[Click image to view at full size]

Figure 1: The DigitsOfPi application.

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);
}

Listing One


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;
}

Listing Two


progressbar.Fraction = Math.Min((double) currentDigit / (double) totalDigits, 1.0);
GuiUtils.ProcessEvents();
 ...
public static void ProcessEvents()
{
   while (GLib.MainContext.Pending())
    {
        Gtk.Main.Iteration();
    }
}


Listing Three

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.

[Click image to view at full size]

Figure 2: Pop-up help.

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.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.