Виджет, который содержит текст, должен иметь размеры такие, чтобы хватало только на начало и конец текста. а тут большое пространство возникает после текста.
вот я записал видео.
click_video
в видео видно в чем ошибка.
вот код.
#include <gtk/gtk.h>
#include <time.h>
#include <string.h>
GtkWidget *menu, *item;
GtkWidget *window;
GtkApplication *app;
GtkWidget *frame_chat_view;
GtkWidget *text_box;
GtkWidget *entry_input_text;
static gboolean window_delete ( GtkWidget *widget, GdkEvent *event, gpointer user_data ) {
g_application_quit ( ( GApplication * ) app );
exit ( EXIT_SUCCESS );
return FALSE;
}
const char *style = "* { background-color: #3c3c3c; } entry { color: #ffffff; } frame#frame_text * { background-color: #1c1c1c; border-radius: 6px; } textview text { color: #ffffff; } textview#text_view_w * { border-radius: 0px; }";
static gboolean entry_input_text_cb ( GtkWidget *widget, GdkEvent *event, gpointer data ) {
GdkEventKey *key = ( GdkEventKey * ) event;
if ( key->keyval == GDK_KEY_Return ) {
const char *t = gtk_entry_get_text ( ( GtkEntry * ) entry_input_text );
if ( strlen ( t ) == 0 ) return FALSE;
GtkTextBuffer *buf = gtk_text_buffer_new ( NULL );
gtk_text_buffer_set_text ( buf, t, -1 );
GtkWidget *frame = g_object_new ( GTK_TYPE_FRAME, "shadow-type", GTK_SHADOW_NONE, "name", "frame_text", NULL );
GtkWidget *text_view = gtk_text_view_new ( );
gtk_text_view_set_editable ( ( GtkTextView * ) text_view, FALSE );
gtk_text_view_set_wrap_mode ( ( GtkTextView * ) text_view, GTK_WRAP_WORD_CHAR );
gtk_text_view_set_buffer ( ( GtkTextView * ) text_view, buf );
gtk_widget_set_name ( text_view, "text_view_w" );
gtk_widget_set_margin_start ( frame, 10 );
gtk_widget_set_margin_end ( frame, 10 );
gtk_widget_set_margin_top ( frame, 10 );
gtk_widget_set_margin_bottom ( frame, 0 );
gtk_widget_set_margin_top ( text_view, 10 );
gtk_widget_set_margin_bottom ( text_view, 10 );
gtk_widget_set_margin_start ( text_view, 10 );
gtk_container_add ( ( GtkContainer * ) frame, text_view );
gtk_box_pack_start ( ( GtkBox * ) text_box, frame, FALSE, FALSE, 0 );
gtk_widget_show_all ( text_box );
gtk_entry_set_text ( ( GtkEntry * ) entry_input_text, "" );
}
return FALSE;
}
static void g_startup ( GtkApplication *app, gpointer data ) {
GMainLoop *loop = g_main_loop_new ( NULL, FALSE );
window = gtk_application_window_new ( app );
gtk_window_set_default_size ( ( GtkWindow * ) window, 1024, 600 );
GdkDisplay *display = gdk_display_get_default ();
GdkScreen *screen = gdk_display_get_default_screen (display);
GtkCssProvider *pr = gtk_css_provider_new ( );
GError *error;
gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER (pr), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
gtk_css_provider_load_from_data ( ( GtkCssProvider * ) pr, style, strlen ( style ), &error );
if ( error ) {
printf ( "%s\n", error->message );
}
GtkStyleContext *ctx = gtk_style_context_new ( );
gtk_style_context_add_provider ( ( GtkStyleContext * ) ctx, ( GtkStyleProvider * ) pr, GTK_STYLE_PROVIDER_PRIORITY_USER );
frame_chat_view = g_object_new ( GTK_TYPE_FRAME, "shadow-type", GTK_SHADOW_NONE, NULL );
g_signal_connect ( window, "delete-event", G_CALLBACK ( window_delete ), NULL );
GtkWidget *main_box = gtk_box_new ( GTK_ORIENTATION_VERTICAL, 0 );
GtkWidget *input_box = gtk_box_new ( GTK_ORIENTATION_HORIZONTAL, 0 );
text_box = gtk_box_new ( GTK_ORIENTATION_VERTICAL, 0 );
gtk_widget_set_name ( text_box, "text_box" );
entry_input_text = gtk_entry_new ( );
gtk_entry_set_width_chars ( ( GtkEntry * ) entry_input_text, 128 );
gtk_box_pack_start ( ( GtkBox * ) main_box, frame_chat_view, TRUE, TRUE, 0 );
gtk_box_pack_start ( ( GtkBox * ) main_box, input_box, FALSE, FALSE, 0 );
gtk_box_pack_end ( ( GtkBox * ) input_box, entry_input_text, TRUE, TRUE, 0 );
g_signal_connect ( entry_input_text, "key-press-event", G_CALLBACK ( entry_input_text_cb ), NULL );
gtk_container_add ( ( GtkContainer * ) frame_chat_view, text_box );
gtk_container_add ( ( GtkContainer * ) window, main_box );
gtk_widget_show_all ( window );
g_main_loop_run ( loop );
}
int main ( int argc, char **argv ) {
app = gtk_application_new ( "org.xverizex.lanxchat", G_APPLICATION_FLAGS_NONE );
g_application_register ( ( GApplication * ) app, NULL, NULL );
g_signal_connect ( app, "activate", G_CALLBACK ( g_startup ), NULL );
int ret = g_application_run ( ( GApplication * ) app, argc, argv );
g_object_unref ( app );
return ret;
}