Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Reflections on a GUI Toolkit Based on OpenGL/OpenTK in MONO/.NET for Serious Applications

5.00/5 (2 votes)
24 Jan 2016CPOL4 min read 17.6K   161  
Check OpenGL as a basis for appealing applications, that are not necessarily games.

Image 1 Download solution for Linux, .NET/MONO 4.0 and OpenGL

Introduction

Even if Microsoft announced (Oct. 2010) to shift from Silverlight to HTML 5 for cross-platform solution and Moonlight (Dec. 2011) has been abandoned, the desire to develop cross-platform applications with .NET seems to grow furthermore. Mono and MonoDevelop are alife and in a good shape, Microsoft announced (Nov. 2014) and released (Nov. 2015) .NET Core 5.0 to improve cross-platform and open source support and Xamarin successfully supports iOS and Android with Mono and Xamarin.Forms besides Xamarin.iOS & Xamarin.Android.

According to this situation - wouldn't it be a great idea to develop MVVM/XAML based applications for Linux/Unix desktops too? Wouldn't it be beneficial to share as much code as possible?

Background

Currently, none of the dominant Linux/Unix players GTK+ and Qt support MVVM/XAML. The MVVM/XAML support of the Roma Widget Set is a good proof of concept, but it is limited to widget functionality right from the start. (It is based on Xlib/X11 and X11 extensions.)

To provide a competitive complement to Microsoft's MVVM/XAML implementation WPF, that is based on DirectX, an equivalent base, e. g. OpenGL, is required.

There have been two preliminary investigations to check out this approach:

  1. Getting started with OpenGL/OpenTK in MONO/.NET for serious applications and
  2. Abstract of the text rendering with OpenGL/OpenTK in MONO/.NET

The results look very promising and consequently, I start following this vision: OpenGL Presentation Foundation

What do you think about it? I would be very pleased if you comment on this idea!

Using the Code

Please read the article Getting started with OpenGL/OpenTK in MONO/.NET for serious applications for information about a minimal development environment setup (Mesa, Mono+MonoDevelop, OpenTK).

Meanwhile, I switched to openSUSE Leap 42 and updated my MonoDevelop to version 5.10. (I recommend to read the download information carefully - and to download the Linux distribution packages from Mono:Factory via the 1 Click install, not the Xamarin packages.)

The openSUSE Leap 42 included MonoDevelop version 5.0.1 crashes frequently during debug sessions. MonoDevelop version 5.10 is much more stable (even if it is announced as a probably instable package). I hope, I'll get a MonoDevelop 64bit version in the near future that is as stable and productive as the old 32bit version 2.4.1.

For text output, an enhanced version of the FtFont class is used (please read the article Abstract of the text rendering with OpenGL/OpenTK in MONO/.NET for details).

First Serious Looking Application with OpenGL Presentation Foundation - OpfDesigner-01

The sample application demonstrates a very basic implementation of the controls DockPanel, Menu, MenuItem and Button, the MouseOver behaviour and Click callback.

Image 2

The solution consists of the two projects:

  • OpfPreprocessor, that contains the source code of the XAML preprocessor
  • OpfDesigner, that contains the source code of the sample application

and the folder References, that contains the referenced assemblies of the OpenTK and the OpenGLPresentationFoundation.

To understand the functioning of the OpfPreprocessor, please read the Writing a XAML dialog application for X11 article's chapter Step by step instruction.

The solution of the sample application is 64bit and linked against .NET/MONO 4.0. All required references are provided with the solution's sub-folder References. The OpenGL Presentation Foundation assembly is not provided in the source code and is available for 64bit only, because at the moment, the source code changes are massive.

Application File Content

The XAML (App.xaml)

The first XAML file to take a look at is App.xaml.

XML
<Application x:Class="OpfDesigner.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:OpfDesigner"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <!-- <ResourceDictionary Source="RTheme.xaml"/> -->
    </Application.Resources>
</Application>

The XAML code is fully Microsoft® WPF compatible.

The code behind (App.xaml.cs)

The corresponding C# code behind file is App.xaml.cs.

C#
using System;
using System.Diagnostics;

// Replica
using System.Windows;

namespace OpfDesigner
{
    /// <summary>Interaction logic for App.xaml</summary>
    public partial class App : Application
    {
        
        // ###############################################################################
        // ### M E T H O D S
        // ###############################################################################

        #region Methods

        #endregion Methods
    }
}

The complete code behind is fully Microsoft® WPF compatible.

Main View File Content

The XAML (MainWindow.xaml)

The second XAML file to take a look at is MainWindow.xaml.

XML
<Window x:Class="OpfDesigner.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:OpfDesigner"
        Title="MainWindow" Height="350" Width="525">
    <DockPanel Name="MainDock">
        <Menu x:Name="MainMenu" DockPanel.Dock="Top">
            <MenuItem Name="File_MainMenuItem" Header="File"></MenuItem>
            <MenuItem Name="Edit_MainMenuItem" Header="Edit"></MenuItem>
        </Menu>
        <Button Name="Button1" Content="Button 1" IsEnabled="False"></Button>
        <Button Name="Button2" Content="Button 2"></Button>
        <Button Name="Button3" Content="Click me to close the window!"
                FontSize="16" Click="Button3_Click"></Button>
    </DockPanel>
</Window>

The complete XAML code is fully Microsoft® WPF compatible.

The code behind (MainWindow.xaml.cs)

The corresponding C# code file is MainWindow.xaml.cs. It contains the Click delegate for the Button control.

C#
using System;

// Replica
using  System.Windows;
using  System.Windows.Controls;

namespace OpfDesigner
{
    public partial class MainWindow : Window
    {
        
        /// <summary>The default constructor.</summary>
        public MainWindow ()
        {
            InitializeComponent ();
            // will be called after construction by generated code!
        }

        private void Button3_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}

The complete code behind is fully Microsoft® WPF compatible.

Next Challenges

  • Dialog windows
  • Popups like menus

Points of Interest

Is it possible to create MVVM design pattern based X11 application with XAML using OpenGL? At the moment, I would say: YES it is!

History

  • 25th January, 2015: First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)