我正在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;
}