如何避免在我的GTK3应用程序中使用全局指针变量

时间:2019-06-07 16:52:56

标签: c gtk3 linuxmint gcc9

我正在用GTK3创建一个C CHAT应用程序(在Linux上(现在是Linux mint 19)),我无法弄清楚如何避免在脚本中使用全局(Label)指针。设计整个程序的方式。

有一个包含按钮,条目和标签的窗口。 当用户键入某些内容并按Enter或按Button时,该消息应打印在Label部分。

除事实之外,所有事情都可以,除了我要在此处避免使用的地方使用全局指针更新Label并在Signal函数中使用它之外。

这是一个可以很好地编译并可以进行测试的示例:

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

1 个答案:

答案 0 :(得分:2)

免责声明:我从未使用过GTK,而且实际上无法测试我的代码。以下内容基于对文档的粗读。

信号回调的最后一个参数可以是您想要的任何东西(只要它是一个指针即可)。

因此,您应该能够通过此参数传递条目和标签。通过将所有数据捆绑到自定义结构中。

该结构本身可以是main中的局部变量,其地址将传递给回调,然后该回调可以根据需要恢复内容。

例如:

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

    ...
}