2

I'm creating a GTK3 CHAT application with C On Linux ( Linux mint 19 right now) and I can not figure out how to avoid the use of the global (Label) pointer in the way how I designed the whole program.

There is a window which has a Button, an Entry and a Label. When the User types something And hits Enter or press the Button, the message should be printed in the Label section.

Every thing is OK, except the fact, that I use a Global pointer to update the Label here where I would like to avoid it, and use it within the Signal function.

Here is an working example which compiles fine and can be tested:

#include <gtk/gtk.h>

GtkWidget *label;

static void display ( GtkWidget *widget, GtkWidget *data )
{
    (void)widget;
    const gchar *const buffer = gtk_entry_get_text ( GTK_ENTRY ( data ) );

    if ( strlen ( buffer ) > 0 )
    {
        g_print ( "%s\n", buffer );
        gtk_label_set_text ( GTK_LABEL ( label ), buffer );
    }

    gtk_entry_set_text          ( GTK_ENTRY ( data ), "" );
    gtk_editable_select_region  ( GTK_EDITABLE ( data ), 0, -1 );
    gtk_editable_copy_clipboard ( GTK_EDITABLE ( data ) );
}

GtkWidget *createWind   ( void );
GtkWidget *createGrid   ( GtkWidget *window );
GtkWidget *createButton ( GtkWidget *grid, const gint left, const gint top, const gint width, const gint height );
GtkWidget *createEntry  ( GtkWidget *grid, const gint left, const gint top, const gint width, const gint height );
GtkWidget *createLabel  ( GtkWidget *grid, const gint left, const gint top, const gint width, const gint height );

int main ( void )
{
    GtkWidget *window;
    GtkWidget *grid;
    GtkWidget *entry;

    GtkWidget *button;
    gtk_init ( NULL, NULL );

    window = createWind ();
    gtk_widget_show ( window );

    grid = createGrid ( window );
    gtk_widget_show ( grid );

    entry = createEntry ( grid, 0, 1, 1, 1 );
    gtk_widget_show ( entry );

    button = createButton ( grid, 1, 1, 1, 1 );
    gtk_widget_show ( button );

    label = createLabel ( grid, 0, 0, 1, 1 );
    gtk_widget_show ( label );

    g_signal_connect ( button, "clicked",  G_CALLBACK ( display ), entry );
    g_signal_connect ( entry,  "activate", G_CALLBACK ( display ), entry );
    //gtk_widget_show_all ( window );
    gtk_main();
}

GtkWidget *createWind ( void )
{
    GdkRectangle display = { 0 };
    GtkWidget *window = gtk_window_new ( GTK_WINDOW_TOPLEVEL );
    gdk_monitor_get_workarea ( gdk_display_get_primary_monitor ( gdk_display_get_default() ), &display );
    gtk_window_move ( GTK_WINDOW ( window ), display.width, display.height );
    gtk_window_set_title ( GTK_WINDOW ( window ), "MyApp" );
    gtk_window_set_default_size ( GTK_WINDOW ( window ), 850, 100 );
    gtk_window_set_resizable ( GTK_WINDOW ( window ), FALSE );
    gtk_container_set_border_width ( GTK_CONTAINER ( window ), 5 );
    g_signal_connect ( window, "destroy", G_CALLBACK ( gtk_main_quit ), NULL );
    return window;
}

GtkWidget *createGrid ( GtkWidget *window )
{
    GtkWidget *grid = gtk_grid_new();
    gtk_widget_set_name ( grid, "Grid" );
    gtk_container_add ( GTK_CONTAINER ( window ), grid );
    return grid;
}

GtkWidget *createEntry ( GtkWidget *grid, const gint left, const gint top, const gint width, const gint height )
{
    GtkWidget *entry = gtk_entry_new();
    gtk_widget_set_name ( grid, "Entry" );
    gtk_widget_set_size_request ( entry, 800, 50 );
    g_object_set ( entry, "margin", 22, NULL );
    gtk_grid_attach ( GTK_GRID ( grid ), entry, left, top, width, height );
    return entry;
}

GtkWidget *createLabel ( GtkWidget *grid, const gint left, const gint top, const gint width, const gint height )
{
    GtkWidget *ret = gtk_label_new ( "Type a Message" );
    gtk_widget_set_name ( ret, "Label" );
    gtk_widget_set_size_request ( ret, 850, 250 );
    gtk_label_set_justify ( GTK_LABEL ( ret ), GTK_JUSTIFY_CENTER );
    gtk_grid_attach ( GTK_GRID ( grid ), ret, left, top, width, height );
    return ret;
}

GtkWidget *createButton ( GtkWidget *grid, const gint left, const gint top, const gint width, const gint height )
{
    GtkWidget *button = gtk_button_new_with_mnemonic ( "Send" );
    gtk_widget_set_name ( button, "Button" );
    gtk_widget_set_size_request ( button, 50, 50 );
    g_object_set ( button, "margin", 22, NULL );
    gtk_grid_attach ( GTK_GRID ( grid ), button, left, top, width, height );
    return button;
}
Michi
  • 5,175
  • 7
  • 33
  • 58

1 Answers1

2

Disclaimer: I've never used GTK and I can't actually test my code. The following is based on a cursory reading of the documentation.

The last argument of your signal callback can be anything you want (as long as it is a pointer).

Therefore you should be able to pass both the entry and the label through this parameter, e.g. by bundling all data up in a custom struct.

The struct itself can be a local variable in main whose address is passed to the callback, which can then recover the contents as required.

For example:

struct EntryLabelPair {
    GtkWidget *entry;
    GtkWidget *label;
};

static void display ( GtkWidget *widget, gpointer *data )
{
    (void)widget;
    struct EntryLabelPair *pair = data;
    const gchar *const buffer = gtk_entry_get_text ( GTK_ENTRY ( pair->entry ) );

    if ( strlen ( buffer ) > 0 )
    {
        g_print ( "%s\n", buffer );
        gtk_label_set_text ( GTK_LABEL ( pair->label ), buffer );
    }

    gtk_entry_set_text          ( GTK_ENTRY ( pair->entry ), "" );
    gtk_editable_select_region  ( GTK_EDITABLE ( pair->entry ), 0, -1 );
    gtk_editable_copy_clipboard ( GTK_EDITABLE ( pair->entry ) );
}

...

int main ( void )
{
    GtkWidget *window;
    GtkWidget *grid;
    struct EntryLabelPair pair;

    ...

    pair.entry = createEntry ( grid, 0, 1, 1, 1 );
    gtk_widget_show ( pair.entry );

    pair.label = createLabel ( grid, 0, 0, 1, 1 );
    gtk_widget_show ( pair.label );

    g_signal_connect ( button, "clicked",  G_CALLBACK ( display ), &pair );
    g_signal_connect ( entry,  "activate", G_CALLBACK ( display ), &pair );

    ...
}
melpomene
  • 84,125
  • 8
  • 85
  • 148
  • This is a good approach and I was thinking also to adopt it, but I was wondering how can I do it from main in the way my program was designed. – Michi Jun 07 '19 at 17:39
  • @Michi What do you mean by "from main"? – melpomene Jun 07 '19 at 18:27
  • I meant to find a way without a global Variable (even without struct). I will stick with your solution for the moment. I was thinking of a solution to use the 3 widgets from main, entry, label and button only. Thank you. – Michi Jun 07 '19 at 19:09
  • @Michi Still need a variable to keep that state (used by other widgets), and that has to live in the part of the program that runs the main loop. (Where else?) – zdim Jun 09 '19 at 06:00