Click here to Skip to main content
15,886,519 members
Articles / Programming Languages / C#

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
24 Jan 2016CPOL4 min read 16.8K   160   2   3
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)


Written By
Team Leader Celonis SA
Germany Germany
I am currently the CEO of Symbioworld GmbH and as such responsible for personnel management, information security, data protection and certifications. Furthermore, as a senior programmer, I am responsible for the automatic layout engine, the simulation (Activity Based Costing), the automatic creation of Word/RTF reports and the data transformation in complex migration projects.

The main focus of my work as a programmer is the development of Microsoft Azure Services using C# and Visual Studio.

Privately, I am interested in C++ and Linux in addition to C#. I like the approach of open source software and like to support OSS with own contributions.

Comments and Discussions

 
QuestionHave you consider to post this as a tip? Pin
Nelek25-Jan-16 0:16
protectorNelek25-Jan-16 0:16 
AnswerRe: Have you consider to post this as a tip? Pin
Steffen Ploetz29-Jan-16 19:10
mvaSteffen Ploetz29-Jan-16 19:10 
GeneralRe: Have you consider to post this as a tip? Pin
Nelek30-Jan-16 10:03
protectorNelek30-Jan-16 10:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.