Reputation: 5
I am attempting to create a text area so that I can create a text editor but when ever the text reaches the end of the window, it just expands and becomes bigger and looks bad.
I have tried multiple things in my code to prevent this resizing and none of it has worked. Would a window with scrolling functionality be easier for this? (I just want to keep my code simple)
code:
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
static GtkTextBuffer *buffer;
void checkEndLine(GtkWidget *text, gpointer data){
//g_print(":%d ", gtk_text_buffer_get_char_count(GTK_TEXT_BUFFER(buffer)));
}
static void activate (GtkApplication *app, gpointer user_data){
GtkWidget *window, *grid, *calculate;
//Create Grid and Window
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Menu Test");
gtk_window_resize(GTK_WINDOW(window), 600, 600);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
grid = gtk_grid_new();
gtk_container_add(GTK_CONTAINER(window), grid);
//Text Field
GtkWidget *largeEntry = gtk_text_view_new ();
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 1);
gtk_widget_set_size_request(GTK_WIDGET(box), 100, 100);
gtk_widget_set_size_request(GTK_WIDGET(largeEntry), 100, 100);
gtk_widget_set_size_request(GTK_WIDGET(grid), 300, 300);
gtk_box_pack_start(GTK_BOX(box), largeEntry, 1, 1, 1);
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (largeEntry));
gtk_text_buffer_set_text (buffer, "He", -1);
gtk_grid_attach(GTK_GRID(grid), box, 0, 1, 1, 1);
gtk_widget_set_hexpand_set(GTK_WIDGET(largeEntry), FALSE);
g_signal_connect(GTK_TEXT_BUFFER(buffer), "changed", G_CALLBACK(checkEndLine), NULL);
//Showing widgets
gtk_widget_show_all(window);
gtk_main();
}
int main(int argc, char **argv){
GtkApplication *app;
int status;
app = gtk_application_new("com.calculate", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref(app);
return status;
}
Compiled with:
gcc note.c `pkg-config --cflags gtk+-3.0` -o out `pkg-config --libs gtk+-3.0`
Upvotes: 0
Views: 700
Reputation: 694
By settings wrap mode you stop the text from expanding the window
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(largeEntry), GTK_WRAP_WORD_CHAR);
But this will only stop it from horizontally expanding the window. to stop it from vertically expanding the window you have to put the text view inside a scrolled window.
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
static GtkTextBuffer *buffer;
void checkEndLine(GtkWidget *text, gpointer data)
{
//g_print(":%d ", gtk_text_buffer_get_char_count(GTK_TEXT_BUFFER(buffer)));
}
static void activate(GtkApplication *app, gpointer user_data)
{
GtkWidget *window, *grid, *calculate;
//Create Grid and Window
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Menu Test");
gtk_window_resize(GTK_WINDOW(window), 600, 600);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
grid = gtk_grid_new();
gtk_container_add(GTK_CONTAINER(window), grid);
//Text Field
GtkWidget *largeEntry = gtk_text_view_new();
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 1);
GtkWidget *swindow = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swindow), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
gtk_container_add(GTK_CONTAINER(swindow), largeEntry);
gtk_widget_set_size_request(GTK_WIDGET(swindow), 100, 100);
gtk_widget_set_size_request(GTK_WIDGET(largeEntry), 100, 100);
gtk_widget_set_size_request(GTK_WIDGET(grid), 300, 300);
gtk_box_pack_start(GTK_BOX(box), largeEntry, 1, 1, 1);
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(largeEntry), GTK_WRAP_WORD_CHAR);
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(largeEntry));
gtk_text_buffer_set_text(buffer, "He", -1);
gtk_grid_attach(GTK_GRID(grid), swindow, 0, 1, 1, 1);
gtk_widget_set_hexpand_set(GTK_WIDGET(largeEntry), FALSE);
g_signal_connect(GTK_TEXT_BUFFER(buffer), "changed", G_CALLBACK(checkEndLine), NULL);
//Showing widgets
gtk_widget_show_all(window);
gtk_main();
}
int main(int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new("com.calculate", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}
Upvotes: 3