2

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

有一个窗口,它有一个按钮、一个条目和一个标签。当用户键入内容并按 Enter 或按下按钮时,应在标签部分打印消息。

一切都很好,除了我使用全局指针来更新我想避免它的标签,并在 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;
}
4

1 回答 1

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

    ...
}
于 2019-06-07T17:30:15.940 回答