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);
Related
This code make a program that create a window with label, if you click the label, the program executes an fprintf of buffer passed as an argument of g_signal_connect(G_OBJECT(eventbox), "button_press_event", G_CALLBACK(on_event_clicked), buffer). Previously the program put in buffer the string "Hello Word" and then the program should print this message, but maybe the program print only garbled char. Where I'm wrong?
#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>
void on_event_clicked (GtkWidget* widget, gpointer user_data);
int main (int argc, char **argv) {
GtkWidget *window;
GtkWidget *eventbox;
GtkWidget *label;
char* buffer = malloc(sizeof(char)*10);
strcpy(buffer, "Hello Word\0");
gtk_init (&argc,&argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
label = gtk_label_new ("Hello Word");
eventbox = gtk_event_box_new ();
gtk_container_add (GTK_CONTAINER(eventbox), label);
gtk_container_add (GTK_CONTAINER(window), eventbox);
gtk_widget_show_all (window);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
g_signal_connect(G_OBJECT(eventbox), "button_press_event",
G_CALLBACK(on_event_clicked), buffer);
gtk_main();
return 0;
}
void on_event_clicked (GtkWidget *widget, gpointer user_data) {
char* pn = user_data;
fprintf(stderr, "%s\n", pn);
}
Your prototype for on_event_clicked() is wrong, it doesn't match what GTK+ expects.
It should be:
gboolean user_function (GtkWidget *widget, GdkEvent *event, gpointer user_data);
You need to add the missing argument to your function, and also deal with the requirement for a return value. Remember to read the signal documentation seriously.
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;
}
Currently I am writing a program in C, on a linux system (Raspberry Pi to be exact) which should draw to a GTK window using Cairo. I've been following the tutorial at: http://zetcode.com/gfx/cairo/ . But it is way to vague with it's explanations at certain points.
It does not explain two points that I really need:
I can't figure out a way to draw to the window with a proper function call.
It removes what is already drawn.
I need a piece of code that does some simple things, in a very Object-Oriented manner:
Draw lines to a GTK window with a function call, given X and Y for both starting and end point;
Do not remove what is previously drawn;
All initializations of variables and the window should be outside the main function.
So basically something similar to this:
#include <cairo.h>
#include <gtk/gtk.h>
void drawLine(int xStart, int yStart, int yEnd, int xEnd) {
//Drawing code here.
}
void initializeCairo() {
//Insert cairo initialization.
}
void initializeGTK() {
//Insert GTK initialization.
}
/*If needed a general initializer for both cairo and GTK*/
void initialize() {
//Insert general initialization.
}
int main (int argc, char *archv[]) {
intializeGTK();
initializeCairo();
if(doSomething) {
drawLine(10, 10, 20, 20);
}
}
If it could be explained what a method does (in proper English please, not a reference to the documentation), that'd be absolutely great.
Also please include the gcc build command used.
Thanks in advance!
The answers from andlabs are fine. Here is in addition a short (although not entirely elegant) example. It will "kind of remember" the last NUM lines - creation/resize/activation/deactivation of the window will trigger a "draw" of the content. A Next button click will add a new line to the output. Check also the command-line output for an update of
the array values that are drawn.
#include <gtk/gtk.h>
#include <glib/gprintf.h>
#include <cairo.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#define NUM 3
typedef struct {
GtkApplication *app;
GtkWidget *window;
GtkWidget *button;
GtkWidget *da;
cairo_t* cr;
gboolean redraw;
gint xsize;
gint ysize;
} appWidgets;
gboolean drawEvent (GSimpleAction *action, GVariant *parameter, gpointer data);
void nextCallback (GtkWidget *widget, gpointer data);
void nextCallback (GtkWidget *widget, gpointer data)
{
appWidgets *w = (appWidgets*) data;
static gint cnt = 0;
static gdouble x[NUM], y[NUM], u[NUM], v[NUM];
// determine the next coordinates for a line
if (w->redraw == FALSE) {
x[cnt] = g_random_double();
y[cnt] = g_random_double();
u[cnt] = g_random_double();
v[cnt] = g_random_double();
}
w->cr = gdk_cairo_create (gtk_widget_get_window (w->da));
// map (0,0)...(xsize,ysize) to (0,0)...(1,1)
cairo_translate (w->cr, 0, 0);
cairo_scale (w->cr, w->xsize, w->ysize);
// set linewidth
cairo_set_line_width (w->cr, 0.005);
// draw the lines
for (int k = 0; k < NUM; k++) {
cairo_move_to (w->cr, x[k], y[k]);
cairo_line_to (w->cr, u[k], v[k]);
cairo_stroke (w->cr);
g_print("k=%d:(%1.2lf,%1.2lf).(%1.2lf,%1.2lf) ",
k, x[k], y[k], u[k], v[k]);
}
g_print("\n");
cairo_destroy (w->cr);
if (w->redraw == FALSE) {
cnt++;
if (cnt == NUM)
cnt = 0;
}
}
gboolean drawEvent (GSimpleAction *action, GVariant *parameter, gpointer data)
{
appWidgets *w = (appWidgets*) data;
w->xsize = gtk_widget_get_allocated_width (w->da);
w->ysize = gtk_widget_get_allocated_height (w->da);
w->redraw = TRUE;
nextCallback (NULL, w);
w->redraw = FALSE;
return TRUE;
}
void activate (GtkApplication *app, gpointer data)
{
GtkWidget *box;
appWidgets *w = (appWidgets*) data;
w->window = gtk_application_window_new (w->app);
gtk_window_set_application (GTK_WINDOW (w->window), GTK_APPLICATION (w->app));
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add (GTK_CONTAINER (w->window), box);
w->da = gtk_drawing_area_new();
gtk_widget_set_size_request (w->da, 400, 400);
gtk_box_pack_start (GTK_BOX (box), w->da, TRUE, TRUE, 0);
g_signal_connect (w->da, "draw", G_CALLBACK (drawEvent), (gpointer) w);
w->button = gtk_button_new_with_label ("Next");
g_signal_connect (G_OBJECT (w->button), "clicked", G_CALLBACK (nextCallback),
(gpointer) w);
gtk_box_pack_start (GTK_BOX (box), w->button, FALSE, TRUE, 0);
gtk_widget_show_all (GTK_WIDGET (w->window));
w->redraw = FALSE;
}
int main (int argc, char *argv[])
{
gint status;
appWidgets *w = g_malloc (sizeof (appWidgets));
w->app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (w->app, "activate", G_CALLBACK (activate), (gpointer) w);
status = g_application_run (G_APPLICATION (w->app), argc, argv);
g_object_unref (w->app);
g_free (w);
w = NULL;
return status;
}
Build the program as usual:
gcc example.c -o example `pkg-config --cflags --libs gtk+-3.0`
I am trying to draw an image on a drawing area with no luck.I saw a couple of python examples but I was not able to implement them into c ,which I am using (eg draw an image to gtk.DrawingArea?)
I have already created a Pixbuf variable to store the image I want to draw on the drawing area,but there are no functions like gtk_drawing_area_draw_pixbuf or something related to that.Any suggestion is appreciated.
You need to make use of expose-event callback (assuming you are working with Gtk+ 2.0) to draw the pixbuf onto drawing area. There is no gtk_drawing_area_draw_pixbuf instead you have gdk_draw_pixbuf. This has been deprecated in favour of gdk_cairo_set_source_pixbuf from version 2.22 onwards. You can call these function in your expose event callback something on these lines (please use them as reference only):
If your Gtk version is < 2.22
static gboolean
da_expose (GtkWidget *da, GdkEvent *event, gpointer data)
{
(void)event; (void)data;
GdkPixbuf *pix;
GError *err = NULL;
/* Create pixbuf */
pix = gdk_pixbuf_new_from_file("/usr/share/icons/cab_view.png", &err);
if(err)
{
printf("Error : %s\n", err->message);
g_error_free(err);
return FALSE;
}
GdkDrawable *draw = gtk_widget_get_window(da);
/* Draw pixbuf */
gdk_draw_pixbuf(draw, NULL, pix, 0, 0, 0, 0, -1, -1, GDK_RGB_DITHER_NONE, 0, 0);
return FALSE;
}
Version 2.22 onwards you will have to make use of cairo something on these lines:
static gboolean
da_expose (GtkWidget *da, GdkEvent *event, gpointer data)
{
(void)event; (void)data;
GdkPixbuf *pix;
GError *err = NULL;
/* Create pixbuf */
pix = gdk_pixbuf_new_from_file("/usr/share/icons/cab_view.png", &err);
if(err)
{
printf("Error : %s\n", err->message);
g_error_free(err);
return FALSE;
}
cairo_t *cr;
cr = gdk_cairo_create (da->window);
gdk_cairo_set_source_pixbuf(cr, pix, 0, 0);
cairo_paint(cr);
cairo_fill (cr);
cairo_destroy (cr);
return FALSE;
}
Of course you would have connected to the callback using g_signal_connect (say g_signal_connect (da, "expose-event", G_CALLBACK (da_expose), NULL);). If you are using Gtk+ 3.0 then you will be making use of draw instead of expose-event. You can always refer to gtk-demo/gtk3-demo application which are available to see the samples along with the code. This should be available in the package repository of your distro or you can always get it from source.
Hope this helps!
PS: This link might provide you with some pointers
Now GTK-version is GTK+3.0.
If you are using GTK+3.0, please use as follows.
// gcc expose.c -o expose `pkg-config gtk+-3.0 --cflags --libs`
#include <gtk/gtk.h>
#include <stdlib.h>
static gboolean
on_window_draw (GtkWidget *da, GdkEvent *event, gpointer data)
{
(void)event; (void)data;
GdkPixbuf *pix;
GError *err = NULL;
/* Create pixbuf */
pix = gdk_pixbuf_new_from_file("/usr/share/icons/cab_view.png", &err);
if(err)
{
printf("Error : %s\n", err->message);
g_error_free(err);
return FALSE;
}
cairo_t *cr;
cr = gdk_cairo_create (gtk_widget_get_window(da));
// cr = gdk_cairo_create (da->window);
gdk_cairo_set_source_pixbuf(cr, pix, 0, 0);
cairo_paint(cr);
// cairo_fill (cr);
cairo_destroy (cr);
// return FALSE;
}
int main ( int argc, char **argv) {
GtkWidget *window;
GtkWidget *canvas;
gtk_init (&argc , &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request (window,
50, 50);
g_signal_connect (window, "destroy",
G_CALLBACK (gtk_main_quit) , NULL);
canvas = gtk_drawing_area_new ();
gtk_container_add (GTK_CONTAINER (window), canvas);
g_signal_connect (canvas, "draw", (GCallback) on_window_draw, NULL);
gtk_widget_set_app_paintable(canvas, TRUE);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
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 :-)