I've got a button which when clicked copies and appends the text from a GtkEntry widget into a GtkTextView widget. (This code is a modified version of an example found in the "The Text View Widget" chapter of Foundations of GTK+ Development.)
I'm looking to insert a newline character before the text which gets copied and appended, such that each line of text will be on its own line in the GtkTextView widget. How would I do this? I'm brand new to GTK+.
Here's the code sample:
#include <gtk/gtk.h>
typedef struct
{
GtkWidget *entry, *textview;
} Widgets;
static void insert_text (GtkButton*, Widgets*);
int main (int argc,
char *argv[])
{
GtkWidget *window, *scrolled_win, *hbox, *vbox, *insert;
Widgets *w = g_slice_new (Widgets);
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Text Iterators");
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
gtk_widget_set_size_request (window, -1, 200);
w->textview = gtk_text_view_new ();
w->entry = gtk_entry_new ();
insert = gtk_button_new_with_label ("Insert Text");
g_signal_connect (G_OBJECT (insert), "clicked",
G_CALLBACK (insert_text),
(gpointer) w);
scrolled_win = gtk_scrolled_window_new (NULL, NULL);
gtk_container_add (GTK_CONTAINER (scrolled_win), w->textview);
hbox = gtk_hbox_new (FALSE, 5);
gtk_box_pack_start_defaults (GTK_BOX (hbox), w->entry);
gtk_box_pack_start_defaults (GTK_BOX (hbox), insert);
vbox = gtk_vbox_new (FALSE, 5);
gtk_box_pack_start (GTK_BOX (vbox), scrolled_win, TRUE, TRUE, 0);
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 0);
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_widget_show_all (window);
gtk_main();
return 0;
}
/* Insert the text from the GtkEntry into the GtkTextView. */
static void
insert_text (GtkButton *button,
Widgets *w)
{
GtkTextBuffer *buffer;
GtkTextMark *mark;
GtkTextIter iter;
const gchar *text;
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (w->textview));
text = gtk_entry_get_text (GTK_ENTRY (w->entry));
mark = gtk_text_buffer_get_insert (buffer);
gtk_text_buffer_get_iter_at_mark (buffer, &iter, mark);
gtk_text_buffer_insert (buffer, &iter, text, -1);
}
You can compile this code using the following command (assuming the file is named file.c):
gcc file.c -o file `pkg-config --cflags --libs gtk+-2.0`
Thanks everybody!
Perhaps you could just insert the newline character the same way you insert other characters:
...
mark = gtk_text_buffer_get_insert (buffer);
gtk_text_buffer_get_iter_at_mark (buffer, &iter, mark);
/* Insert newline (only if there's already text in the buffer). */
if (gtk_text_buffer_get_char_count(buffer))
gtk_text_buffer_insert (buffer, &iter, "\n", 1);
gtk_text_buffer_insert (buffer, &iter, text, -1);
...
"\n" is a string containing the newline character in C, and it works just fine in GTK (unless it doesn't in Windows for some weird reason).
Unhelpful blurb: The 1 can just as easily be -1 here; it just tells GTK it only needs to read 1 character, which might be a little faster. Granted, that line of code should almost never be a bottleneck :-)
Related
I am receiving this error and not sure why...Please take a look at my buttons array, maybe I messed smth there, I am not sure...
The error i get is: Xlib: extension "RANDR" missing on display ":24.0".
and nothing happens after that, meaning my program doesnt run at all....
#include <gtk/gtk.h>
/* Our new improved callback. The data passed to this function
* is printed to stdout. */
static void callback (GtkWidget *widget, gpointer data)
{
system ((gchar *) data);
}
/* another callback */
static gboolean delete_event (GtkWidget *widget, GdkEvent *event, gpointer data)
{
gtk_main_quit ();
return FALSE;
}
int main (int argc, char *argv[])
{
/* GtkWidget is the storage type for widgets */
GtkWidget *window;
GtkWidget *box1;
/* This is called in all GTK applications. Arguments are parsed
* from the command line and are returned to the application. */
gtk_init (&argc, &argv);
/* Create a new window */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
/* This is a new call, which just sets the title of our
* new window to "My Assignments" */
gtk_window_set_title (GTK_WINDOW (window), "My Assignments");
/* Here we just set a handler for delete_event that immediately
* exits GTK. */
g_signal_connect (window, "delete-event",
G_CALLBACK (delete_event), NULL);
/* Sets the border width of the window. */
gtk_container_set_border_width (GTK_CONTAINER (window), 50);
/* We create a box to pack widgets into. This is described in detail
* in the "packing" section. The box is not really visible, it
* is just used as a tool to arrange widgets. */
box1 = gtk_vbox_new (FALSE,0);
/* Put the box into the main window. */
gtk_container_add (GTK_CONTAINER (window), box1);
/*array is here*/
GtkWidget *button[2];
int i;
for (i=0; i<2; i++)
{
button[i]=gtk_button_new();
}
button[0] = gtk_button_new_with_label ("Run shellscript");
g_signal_connect (button[0], "clicked",
G_CALLBACK (callback), "shellscript");
gtk_box_pack_start (GTK_BOX (box1), button[0], TRUE, TRUE, 0);
gtk_widget_show(button[0]);
button[1] = gtk_button_new_with_label ("Run program2 ");
g_signal_connect (button[1], "clicked",
G_CALLBACK (callback), "program");
gtk_box_pack_start (GTK_BOX (box1), button[1], TRUE, TRUE, 0);
gtk_widget_show(button[1]);
gtk_widget_show (box1);
/* Rest in gtk_main and wait for the fun to begin! */
gtk_main ();
return 0;
}
A GtkGrid contains some widgets. When I hide some of this widgets there is still their space, instead I want to have all visible widgets re-aligned in a "natural way" (without preserving the space of the hidden widgets).
A little example:
#include <gtk/gtk.h>
GtkWidget *label2, *label3;
static gboolean on_button_click (GtkButton *button, gpointer data) {
gtk_widget_hide (label2);
gtk_widget_hide (label3);
return TRUE;
}
int main (int argc, char **argv) {
gtk_init(&argc, &argv);
GtkWidget *window;
GtkWidget *label1 = gtk_label_new("Widget1");
label2 = gtk_label_new("Widget2");
label3 = gtk_label_new("Widget3");
GtkWidget *label4 = gtk_label_new("Widget4");
GtkWidget *button = gtk_button_new_with_label ("Hide widget2 and widget3");
g_signal_connect(button, "clicked", G_CALLBACK (on_button_click), NULL);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
GtkGrid *grid = (GtkGrid*)gtk_grid_new ();
gtk_grid_set_row_spacing((GtkGrid*)grid, 50);
gtk_grid_set_column_spacing((GtkGrid*)grid, 50);
// 2 columns
gtk_grid_insert_column (grid, 0);
gtk_grid_insert_column (grid, 1);
// 2 rows
gtk_grid_insert_row (grid, 0);
gtk_grid_insert_row (grid, 1);
//Add widgets to the grid
gtk_grid_attach(GTK_GRID(grid), label1, 1, 0, 1, 1);
gtk_grid_attach(GTK_GRID(grid), label2, 2, 0, 1, 1);
gtk_grid_attach(GTK_GRID(grid), label3, 1, 1, 1, 1);
gtk_grid_attach(GTK_GRID(grid), label4, 2, 1, 1, 1);
gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX (box), (GtkWidget*)grid, FALSE, FALSE, 0);
gtk_container_add (GTK_CONTAINER (window), box);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
this program compiled with:
gcc `pkg-config --cflags gtk+-3.0` test.c `pkg-config --libs gtk+-3.0` -o test
give this window:
clicking on the button, I get this result:
but I want this result:
What is the simplest way to do this?
p.s. I also need to show again the hidden widgets in the same position where they were when happen some events. Imagine to have another button that shows widget2 and widget3 again obtaining the same initial window.
You'll have to remove widget4 and re-pack it into widget2's space. (You attached the widgets to those particular cells in the grid, and so they'll stay there.)
It would be better still to use a different widget than GtkGrid, such as GtkFlowBox, to achieve what you want.
I am making a program in C with GTK and Glade for a serial communication. I am having problem using g_timeout_add. For example I have a function serial_data() which contain my serial data and I have a button handler on_update_button_clicked(). So till now I have done that if update button is clicked, gtk_timeout should run. But it running just for one time.
on_update_button_clicked(GtkButton *Update_Button)
{
//2nd argument is serial_data function which contain actual data
g_timeout_add(250,serial_data,NULL);
}
where I am missing the point?
I have another button stop button. So i want that timeout should stop when stop button handler is clicked. How to do that.??
One more question to ask, I want to count the number of times timeout is running like a counter. So that I can display the numbers of counter. How is this possible.?
Please help thanks.
From the documentation, The function is called repeatedly until it returns FALSE. You can call on_update_button with a boolean argument to toggle the timeout call from being continually called, set it running when argument evaluates to TRUE, and delete the thread with g_source_remove(threadID) if argument is FALSE. Here's a demonstration:
// compiling with: gcc test.c `pkg-config --cflags gtk+-3.0` `pkg-config --libs gtk+-3.0` -o test
#include <stdio.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
guint threadID = 0;
guint serial_counter = 0;
static gboolean
serial_data (gpointer user_data)
{
// do something
printf("counter: %d\n", serial_counter);
serial_counter++;
return user_data;
}
static void
on_update_button_clicked (GtkButton* button, gpointer user_data)
{
if (user_data == 1)
{
threadID = g_timeout_add(250, serial_data, user_data);
}
else if (user_data == 0)
{
g_source_remove(threadID);
threadID = 0;
}
}
int
main (int argc, char *argv[])
{
GtkWidget *window;
gtk_init (&argc, &argv);
GtkWidget *update_button;
GtkWidget *stop_button;
GtkWidget *box;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "test.c");
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
update_button = gtk_button_new_with_label (_("Update"));
stop_button = gtk_button_new_with_label (_("Stop"));
gtk_box_pack_start (GTK_BOX (box), update_button, FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX (box), stop_button, FALSE, FALSE, 0);
gtk_container_add (GTK_CONTAINER (window), box);
g_signal_connect (update_button, "clicked", G_CALLBACK (on_update_button_clicked), 1);
g_signal_connect (stop_button, "clicked", G_CALLBACK (on_update_button_clicked), 0);
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
g_timeout_add() returns an event source id that you should store. You can use g_source_remove() with that id in your stop button handler to stop the timeout.
check the developer site
https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html#g-timeout-add
its quite explicit.
you can have a gpointer to a gboolean STOP, finish the serial_data func in return STOP, and make your stop button change that STOP = FALSE and it will stop calling on that function. Or something like that.
I have one issue with gtk in c . I tried to follow the tutorial but I am not able to pass a text-entry to a function when one clicks on the button in the widget.
The code compiles fine but when I press the button I get several warnings and the string from text-entry that was supposed to be printed are null
What did I do wrong?
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
static GtkWidget *asset_label;
static GtkWidget *frame;
static GtkWidget *entry;
static void entry_Submit(GtkWidget *widget, GtkWidget *entry)
{
const gchar *text = gtk_entry_get_text(GTK_ENTRY (entry));
printf ("Result: %s\n", text);
gtk_widget_destroy(GTK_WIDGET(asset_label));
asset_label = gtk_label_new (text);
gtk_container_add (GTK_CONTAINER (frame), asset_label);
gtk_widget_show_all(frame);
}
static void destroy(GtkWidget *widget, gpointer data)
{
gtk_main_quit ();
}
static void initialize_window(GtkWidget* window)
{
gtk_window_set_title(GTK_WINDOW(window),"My Window"); //Set window title
gtk_window_set_default_size (GTK_WINDOW (window), 400, 200); //Set default size for the window
g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL); //End application when close button clicked
}
int main (int argc, char *argv[])
{
GtkWidget *window,*table,*label, *button;
gtk_init(&argc, &argv);
//Create the main window
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
initialize_window(window);
/* Create a 1x2 table */
table = gtk_table_new (3, 3, TRUE);
gtk_container_add (GTK_CONTAINER (window), table);
/* create a new label. */
label = gtk_label_new ("Enter some text:" );
//gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
gtk_table_set_homogeneous(GTK_TABLE (table), TRUE);
gtk_table_attach_defaults (GTK_TABLE (table), label, 1, 2, 0, 1);
//create a text box
entry = gtk_entry_new ();
//gtk_entry_set_text (GTK_ENTRY (entry), "");
gtk_entry_set_max_length (GTK_ENTRY (entry),0);
gtk_table_attach_defaults (GTK_TABLE (table), entry, 0, 1, 0, 1);
button = gtk_button_new_with_label("Calculate");
g_signal_connect_swapped (button, "clicked", G_CALLBACK (entry_Submit), entry);
gtk_table_attach_defaults (GTK_TABLE (table), button, 0, 2, 1, 2);
//gtk_widget_show (button);
gtk_widget_show_all(window);
gtk_main ();
return 0;
}
And when the button is clicked I get this result:
Result: (null)
There is a problem in the callback or the way you are registering the callback (as fixing either one of them should fix your problem).
By default the clicked callback takes GtkButton as the first parameter and gpointer data as the second. By using g_signal_connect_swapped you are saying that in the callback function, the parameters will be swapped i.e., the first parameter will be gpointer data (GtkEntry in your code) and second GtkButton. But in your callback function you are treating second parameter entry which is in fact GtkButton as GtkEntry. Either use g_signal_connect instead of g_signal_connect_swapped or use the first parameter widget as GtkEntry in your callback function.
Side note: Regarding the warning, if the code which you have posted is the full code then in the callback function entry_Submit during first execution asset_label is null and thus gtk_widget_destroy(GTK_WIDGET(asset_label)); will throw a warning. Also, frame is unassigned before use in the callback function.
Hope this helps!
gtk+ ver 2.24.8
vte ver 0.28.2
I just starting out creating a terminal application, however, none of the examples I have found online (5) have working scrollback. Is there a problem with scrollback in vte or is it something I'm not seeing? Here is one example:
#include <gtk/gtk.h>
#include <vte/vte.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <gdk/gdkkeysyms.h> // includes GDK_q
/* gcc -Wall -g term.c -o term `pkg-config --cflags --libs gtk+-2.0 vte` */
long size;
char *buf;
char *ptr;
gboolean key_press_win_main (GtkWidget *widget, GdkEventKey *event, gpointer user_data);
void quit_activated();
static gboolean delete_event(GtkWidget *,GdkEvent *);
int main( int argc, char *argv[] )
{
size = pathconf(".", _PC_PATH_MAX);
if ((buf = (char *)malloc((size_t)size)) != NULL) ptr = getcwd(buf, (size_t)size);
GtkWidget *window_main;
GtkWidget *vbox;
GtkWidget *vte;
GtkWidget *notebook;
GtkWidget *scrolled_window;
GtkWidget *label;
gtk_init (&argc, &argv);
window_main = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window_main), "Caraterm v0.0.1");
vbox = gtk_vbox_new(FALSE,0);
gtk_container_add (GTK_CONTAINER (window_main), vbox);
notebook = gtk_notebook_new ();
gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_BOTTOM);
gtk_box_pack_start (GTK_BOX (vbox), notebook, TRUE, TRUE, 0);
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
label = gtk_label_new ("term1");
gtk_notebook_append_page (GTK_NOTEBOOK (notebook), scrolled_window, label);
vte = vte_terminal_new();
vte_terminal_set_background_transparent(VTE_TERMINAL(vte), FALSE);
vte_terminal_set_size(VTE_TERMINAL(vte), 80, 45);
vte_terminal_set_scrollback_lines(VTE_TERMINAL (vte), -1); /* infinite scrollback */
vte_terminal_fork_command(VTE_TERMINAL(vte), NULL, NULL, NULL, ptr, TRUE, TRUE,TRUE);
vte_terminal_set_scroll_on_keystroke(VTE_TERMINAL (vte), TRUE);
gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window), vte);
printf("%s\n", vte_terminal_get_emulation(VTE_TERMINAL (vte)));
g_signal_connect(G_OBJECT(window_main),"destroy",G_CALLBACK(quit_activated),NULL);
g_signal_connect(G_OBJECT(window_main),"delete_event",G_CALLBACK(delete_event),NULL);
g_signal_connect (G_OBJECT (window_main), "key_press_event", G_CALLBACK (key_press_win_main), NULL);
gtk_widget_show (label);
gtk_widget_show (notebook);
gtk_widget_show (scrolled_window);
gtk_widget_show (vte);
gtk_widget_show (vbox);
gtk_widget_show (window_main);
gtk_main ();
return 0;
}
gboolean key_press_win_main (GtkWidget *widget, GdkEventKey *event, gpointer user_data)
{
switch (event->keyval)
{
case GDK_q:
if (event -> state & GDK_CONTROL_MASK)
{
printf("entered q\n");
quit_activated();
}
break;
default:
return FALSE;
}
return FALSE;
}
void quit_activated()
{
gtk_main_quit();
}
static gboolean delete_event(GtkWidget *window_main,GdkEvent *event)
{
return FALSE;
}
This example will scroll but only to accomodate the original size of the vte set via vte_terminal_set_size(VTE_TERMINAL(vte), 80, 45); If more lines are added to the terminal by simply hitting return or producing output from a program the scrollback buffer does not expand. It is fixed at 45 lines despite the fact that an unlimited scrollback was specified vte_terminal_set_scrollback_lines(VTE_TERMINAL (vte), -1); /* infinite scrollback */
What am I missing? Thanks.
In order to get scrollback to work the line that needed to be changed was:
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
to:
scrolled_window = gtk_scrolled_window_new (NULL, terminal->adjustment);
where terminal is defined as:
VteTerminal *terminal;
vte = vte_terminal_new();
terminal = VTE_TERMINAL (vte);
I'm not all that confident since I haven't worked with VTE before. However, it looks like the vte_terminal is itself a scrollable widget. I'd suggest you try to get rid of the scrolled_window and see.
Also, here is what I've found as an example.
try this it's works
//gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window), vte);
gtk_container_add (GTK_CONTAINER(scrolled_window),vte);