Thread: Simple GUI framework

  1. #1
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104

    Simple GUI framework

    I want to create very simple graphical application, something similar to this - ToDoPilot - a lifestyle personal organizer for Windows

    However, my application will be much easier. I'm not interested in decorations. I am satisfied with the interface in the Win95 style.

    My OS: Windows 7. My compiler: TinyCC. Portability isn't required.

    The main requirement to create an interface was simple and obvious problem to be solved in just a few lines. If you have a favorite GUI framework, write such a program. Give me an example, please. Just skeleton of program, of course. You can make another program. For example, program with three text fields, the first two takes two numbers, and the third outputs their sum. I want to compare. Thanks!
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Look into GTK+. There are probably other C-based GUI toolkits out there, but GTK+ is by far the most common. Not sure if it supports your compiler. You'll need to find that out for yourself. I know that it does support MinGW/GCC, which is probably a better compiler than TinyCC, in many ways, although I have no evidence to support that claim.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User zub's Avatar
    Join Date
    May 2014
    Location
    Russia
    Posts
    104
    Quote Originally Posted by Elkvis View Post
    Look into GTK+.
    Requirements

    Packages
    You will need the GLib, cairo, Pango, ATK, gdk-pixbuf and GTK+ developer packages to build software against GTK+. To run GTK+ programs you will also need the gettext-runtime, fontconfig, freetype, expat, libpng and zlib packages.

    Somehow, I immediately remembered the words from the movie "Apocalypse Now": "Horror! Horror has a face, and you must make a friend of horror". However, I do not want to be friends with horror. I just want something small and simple. For example, I chose greatest as framework for unit testing, not least because it consists of a single file, and its use is trivial. I never took anything beyond its extremely narrow scope.
    Our goals are clear, tasks are defined! Let's work, comrades! -- Nikita Khrushchev

  4. #4
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Quote Originally Posted by zub View Post
    Requirements

    Packages
    You will need the GLib, cairo, Pango, ATK, gdk-pixbuf and GTK+ developer packages to build software against GTK+. To run GTK+ programs you will also need the gettext-runtime, fontconfig, freetype, expat, libpng and zlib packages.

    Somehow, I immediately remembered the words from the movie "Apocalypse Now": "Horror! Horror has a face, and you must make a friend of horror". However, I do not want to be friends with horror. I just want something small and simple. For example, I chose greatest as framework for unit testing, not least because it consists of a single file, and its use is trivial. I never took anything beyond its extremely narrow scope.
    That's true, GTK is not a simple GUI framework. However, I agree with Elkvis that it may be your best bet, due its enormous popularity (actually, I believe it is the only decent GUI framework for C).

    If you are on Linux, setting it up is just a breeze. For example, on Ubuntu to setup the GTK+2 devel goes something like this:

    Code:
    sudo apt-get install libgtk2.0-dev
    On Windows it's just a matter of downloading a zip file, extracting it into any folder, and finally setup a couple of environment variables (see this for example - the section: "Installing the GTK+2 Development Environment").

    The already suggested gcc/mingw tool-chains are the recommended ones for compiling GTK+ apps, but it's not mandatory. Hopefully the link in the previous paragraph also provides (in other sections) all the info you may need to set up GTK with any compiler (I've successfully done it with Pelles C on Windows for example).

    Anyway, as you asked, here's a sample GTK+2 program with 2 text-entries for the operands, a button for initiating the addition calculation and a label for displaying the result.

    The ui has been done with the Glade Ui Designer (the GTK+2 version of it) which actually produces an xml file. I load this file into a GtkBuilder object inside the code. That's not mandatory. One may directly create the ui dynamically, in his/her code.

    The .glade (xml) file is included in the attached zip file, along with the source code and a Windows 32bit executable (produced with the mingw-w64 tool-chain).

    Here's the code, in case someone is too lazy to download the zip-file (please keep in mind that it may contains bugs, it was written in a rush... e.g. for one thing, more error-checking is required).

    PS. The latest version of GTK is GTK+3, but I'm not familiar with (it is supposed to be easier than GTK+2, but they are not compatible).

    Code:
    #include <stdio.h>     /* for error-messages in stderr */
    #include <stdlib.h>    /* strtol() */
    #include <errno.h>
    
    #include <gtk/gtk.h>
    
    #define TXTF_RESULT     "Result: %ld"
    
    /* our GUI (bare) abstraction */
    typedef struct {
        GtkWidget *window;     /* main window */
        GtkWidget *op1, *op2;  /* text-entry widgets for the operands */
        GtkWidget *ok;         /* button widget */
        GtkWidget *result;     /* label-widget for the result */
    } Gui;
    
    /* -------------------------------------------------
     * Convert the text of the specified GtkEntry widget (te)
     * to a long int. Return FALSE on error, TRUE otherwise.
     * On success, the long int gets back to the caller via
     * the (ref) pointer.
     *
     * NOTE: On failure, the GtkEntry widget gets input focus,
     *       with all its contents selected.
     * -------------------------------------------------
     */
    gboolean te_get_long_int( GtkEntry *te, long int *ref )
    {
        long int temp = 0L;
        char *txt     = NULL;      /* for getting widget's text */
        char *tailptr = NULL;      /* for strtol() */
    
        /* sanity chekcs */
        if ( NULL == te || NULL == ref ) {
            fprintf( stderr, "%s: NULL pointer argument ", __func__ );
            return FALSE;
        }
    
        /* get widget's text */
        txt = g_strdup( gtk_entry_get_text(te) );
    
        /* validate it as long int */
        errno = 0;
        temp = strtol( txt, &tailptr, 10 );
        if ( ERANGE == errno || '\0' != *tailptr ) {
            gtk_widget_grab_focus( GTK_WIDGET(te) );
            gtk_editable_select_region( GTK_EDITABLE(te), 0, -1 );
            g_free( txt );
            return FALSE;
        }
        g_free( txt );
    
        /* update ref & exit */
        *ref = temp;
        return TRUE;
    }
    
    /* -------------------------------------------------
     * Callback function connected to the "clicked"
     * signal for the OK button.
     * -------------------------------------------------
     */
    void on_clicked_button_ok(
        GtkWidget  *button,
        Gui        *gui
        )
    {
        long int op1, op2;
    
        /* avoid compiler warnings for unused arg*/
        (void)button;
    
        /* sanity check */
        if ( NULL == gui ) {
            fprintf( stderr, "%s: NULL pointer argument!\n", __func__ );
            return;
        }
    
        /* get & validate operands */
        if ( !te_get_long_int( GTK_ENTRY(gui->op1), &op1 )
        || !te_get_long_int( GTK_ENTRY(gui->op2), &op2 )
        ){
            goto ret_failure;
        }
    
        /* Calc & display the result */
        char *txt = g_strdup_printf( TXTF_RESULT, op1 + op2 );
        gtk_label_set_text( GTK_LABEL( gui->result ), txt );
        g_free( txt );
    
        return;
    
    ret_failure:
        gtk_label_set_text(
            GTK_LABEL( gui->result ),
            "Invalid operand detected"
            );
    }
    
    /* -------------------------------------------------
     *
     * -------------------------------------------------
     */
    int main( int argc, char *argv[] )
    {
        GtkBuilder *builder = NULL;
        GError     *error   = NULL;
    
        Gui *gui = g_slice_alloc0( sizeof(*gui) );
        if ( NULL == gui ) {
            fputs( "gui allocation failed, bye...\n", stderr );
            return 1;
        }
    
        gtk_init( &argc, &argv );
    
        /*
         * Read glade file into builder
         */
    
        builder = gtk_builder_new();
        if( !gtk_builder_add_from_file( builder, "zub.glade", &error ) ) {
            g_warning( "%s", error->message );
            g_free( error );
            g_object_unref( G_OBJECT(builder) );
            return 1;
        }
    
        /*
         * Populate our GUI from builder (unref builder when done)
         */
    
        gui->window = GTK_WIDGET(
                gtk_builder_get_object( builder, "windMain" )
                );
        gui->op1    = GTK_WIDGET(
                gtk_builder_get_object( builder, "teOp1" )
                );
    
        gui->op2    = GTK_WIDGET(
                gtk_builder_get_object( builder, "teOp2" )
                );
        gui->ok    = GTK_WIDGET(
                gtk_builder_get_object( builder, "buttonOk" )
                );
        gui->result = GTK_WIDGET(
                gtk_builder_get_object( builder, "labelResult" )
                );
        g_object_unref( G_OBJECT( builder ) );
    
        /*
         * Connect callback functions to signals
         */
    
        g_signal_connect(
            gui->window,
            "destroy",
            G_CALLBACK( gtk_main_quit ),  /* provided by GTK+2 */
            NULL
            );
    
        g_signal_connect(
            gui->ok,
            "clicked",
            G_CALLBACK( on_clicked_button_ok ),
            gui
            );
        
    
        /*
         * Prepare some visuals before drawing the main window
         */
    
        gtk_window_set_title(
            GTK_WINDOW(gui->window),
            "GTK+2 sample for zub"
            );
        gtk_window_set_position(
            GTK_WINDOW( gui->window ),
            GTK_WIN_POS_CENTER
            );
    
        gchar *txt = g_strdup_printf( TXTF_RESULT, 0L );
        gtk_label_set_text( GTK_LABEL( gui->result ), txt );
        g_free( txt );
    
        /*
         * All set... go
         */
    
        gtk_widget_show( gui->window );
        gtk_main();
    
        /*
         * Cleanup & exit
         */
        g_slice_free1( sizeof(*gui), gui );
        return 0;
    }
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    Aug 2014
    Posts
    2
    Why not just use the Win32 api?
    Use something to create/edit a resource file like ResEdit Resource Editor - free resource editor for Win32 and just build what you want.

  6. #6
    Registered User
    Join Date
    Nov 2013
    Posts
    107
    I agree with directly using the Win32 api, CreateWindowEx() is the function for creating a window, there are a whole bunch of parameters you can pass to this to create specific controls within the window such as WC_LISTVIEWW to create a listview control etc.. You just need to add a callback function and learn a few things about this although it's almost cut and paste stuff.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by zub View Post
    Requirements

    Packages
    You will need the GLib, cairo, Pango, ATK, gdk-pixbuf and GTK+ developer packages to build software against GTK+. To run GTK+ programs you will also need the gettext-runtime, fontconfig, freetype, expat, libpng and zlib packages.

    Somehow, I immediately remembered the words from the movie "Apocalypse Now": "Horror! Horror has a face, and you must make a friend of horror". However, I do not want to be friends with horror. I just want something small and simple. For example, I chose greatest as framework for unit testing, not least because it consists of a single file, and its use is trivial. I never took anything beyond its extremely narrow scope.
    It looks more intimidating than it actually is. Installer packages are available, which contain everything you need to get started.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  8. #8
    Registered User
    Join Date
    Aug 2014
    Posts
    2
    Here is a tutorial I used to get the basics of win32 - theForger's Win32 API Tutorial

  9. #9
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    The Win32 API is by no means easier to program with, than GTK+2.

    However, if the target is Windows platforms only, then the end-users need do nothing in order to run the executables (while for GTK+2 they must install the GTK+2 runtime, if the developer does not bundle it with the installer of his application).
    Last edited by migf1; 08-14-2014 at 02:04 PM. Reason: typos

  10. #10
    Registered User
    Join Date
    Jun 2014
    Posts
    79
    As someone else already did, I suggest that you deal directly with the Win32 API. They allow the creation of very little and fast program files with just a few lines of code (well, in a sense).

    Moreover, the web is literally INFESTED with basic tutorials, samples and examples. As a reference material, in my opinion the "official" Win32 Help File is the best option, though to run it in Windows 7 you will need to download a small piece of free software from Microsoft's site.

    You can find the Win32 Help File here: http://www.carabez.com/downloads/win32api_big.zip
    I think that the help file has been written in the Win '98 system era, but I'm still using its directives today and my programs work fine in Windows 7.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Win32 is difficult to work with and most tutorials out there are just plain wrong, use insecure practices or both. It is difficult to get Win32 right.
    I really suggest you use a good framework, or use a higher-level language for building applications, such as C# (very easy to make apps with on Windows).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    I've only been learning the win32 api for about a week now, but I agree with Elysia. I'm not saying Win32 is bad (I actually like most of it and think it's clever), it is just one of those things where it takes a while to get enough groundwork laid to make things easier (I hope).

    It does have really great documentation though.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Alpo View Post
    ...it is just one of those things where it takes a while to get enough groundwork laid to make things easier (I hope).
    By which time you could have poured knowledge into a framework that is higher-level (hence easier to use, less learning curve, less chance to screw up) and portable, to boot.
    I stand by my opinion that Win32 is a mess and Microsoft seems unwilling to fix it. They keep adding more functionality that works with a legacy API (e.g. COM anyone?) instead of modernizing it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Quote Originally Posted by Elysia View Post
    Win32 is difficult to work with and most tutorials out there are just plain wrong, use insecure practices or both. It is difficult to get Win32 right.
    I really suggest you use a good framework, or use a higher-level language for building applications, such as C# (very easy to make apps with on Windows).
    I second that (although I don't think that the Win32 API is that difficult... but it surely is more difficult than modern C GUI frameworks such as GTK+).

    As I said in a previous post, I believe that if one is forced to use C for GUI apps, then I don't think he/she will find anything better than GTK+.

    PS. Vala is a relatively new language that attempts to offer a C#/Java like "experience" when programming GTK+ apps. However, on Windows C# is a much more sane option.

  15. #15
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    I'm currently taking the GTK+ route.Size of the resulting code is a minor consideration IMO when it gives the ability to create code that will run on several OS, thus widening options in a future in which Windows isn't quite the front runner it was.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# without .NET framework
    By heisel in forum C# Programming
    Replies: 2
    Last Post: 10-04-2009, 01:37 PM
  2. .net framework
    By mixalissen in forum C# Programming
    Replies: 3
    Last Post: 05-28-2008, 11:44 AM
  3. Net Framework
    By Hugo716 in forum C++ Programming
    Replies: 2
    Last Post: 05-18-2006, 02:05 PM
  4. A C++ GUI Framework
    By jinhao in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 05-11-2005, 06:34 AM
  5. c++ using .net framework
    By axr0284 in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2004, 09:45 AM