Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can someone help with this? GTK / Ncurses

38 views
Skip to first unread message

ChuckB

unread,
Dec 14, 2007, 11:20:12 AM12/14/07
to
Ok, I'm trying to create a GTK+ application that scrolls Hs across the
screen until you press q or Q. I've got it working in console wise
using ncurses. What I'm trying to do now is make a full screen GTK+
gui based way of it to scroll the Hs until the keypress. I tried
manipulating them together, but the console loads up first, and when
you hit q, it'll fill the data into the GTK+ text_view, but it's quit
at that time.

If anyone could shed some light and help I'd apprietiate it.

-Chuck

** CODE FOLLOWS **

#include <gtk/gtk.h>
#include <ncurses.h>

static gboolean delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
g_print ("delete event occurred\n");
return TRUE;

}

/* Another callback */
static void destroy( GtkWidget *widget,
gpointer data )
{
gtk_main_quit ();

}

int main( int argc,
char *argv[] )
{
GtkWidget *window;
GtkWidget *textview;
GtkTextBuffer *buffer;

gtk_init (&argc, &argv);

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_fullscreen(GTK_WINDOW(window));

g_signal_connect (G_OBJECT (window), "delete_event",
G_CALLBACK (delete_event), NULL);
g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (destroy), NULL);

textview = gtk_text_view_new ();
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textview));

gtk_container_add (GTK_CONTAINER (window), textview);
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview), GTK_WRAP_CHAR);
gtk_widget_show (textview);
gtk_widget_show (window);
/* int timer = 0;
while (timer != 10000)
{
gtk_text_buffer_insert_at_cursor(buffer, "H", -1);
timer = timer + 1;
}*/

//
*******************************************************************************************************
int ch = 0;
int backg = 0;
int foreg = 7;
initscr();
start_color();
init_pair(1, 2, 3); // 0-7
cbreak();
noecho();
nodelay(stdscr, TRUE);
do
{
ch = getch();
init_pair(1, foreg, backg);
attron(COLOR_PAIR(1));
gtk_text_buffer_insert_at_cursor(buffer, "H", -1); //
wprintw(stdscr,
"H");
switch( ch )
{
case 'Q' :
case 'q' : gtk_text_buffer_insert_at_cursor(buffer,
"QUITTING",
-1); //wprintw(stdscr, "\nQuitting...");
break;
case 'B' :
case 'b' : if (backg == 7) backg = 0;
else backg = backg + 1;
break;
case 'F' :
case 'f' : if (foreg == 7) foreg = 0;
else foreg = foreg + 1;
break;
default : break;
}
attroff(COLOR_PAIR(1));
refresh();

} while ((ch != 113) && (ch != 81));
gtk_main ();
refresh();
getch();
endwin();
//
*********************************************************************************************************

return 0;

}

Simon Gerber

unread,
Dec 14, 2007, 1:29:59 PM12/14/07
to
ChuckB wrote on Fri, 14 Dec 2007 08:20:12 -0800:

> Ok, I'm trying to create a GTK+ application that scrolls Hs across the
> screen until you press q or Q.

I tried to build that. As I also noted in the Code below it doesn't work
too well, as I suspect there are some buffering issues that block the Hs
from being displayed as they get put into the GtkTextBuffer


> I've got it working in console wise using ncurses. What I'm trying to
> do now is make a full screen GTK+ gui based way of it to scroll the Hs
> until the keypress. I tried manipulating them together, but the console
> loads up first, and when you hit q, it'll fill the data into the GTK+
> text_view, but it's quit at that time.

I built a version that doesn't print anything onto the console, not sure
if this is really what you want, but it should be easy to modify that.


>
> If anyone could shed some light and help I'd apprietiate it.

I hope my attempt helps.
>
<snip code>

HTH
Simon

*** code gtk_mod.c ***

/* compiled fine on my system with
* gcc `pkg-config --libs --cflags gtk+-2.0` \
-g -Wall -o gtk_mod gtk_mod.c
*/
#include <gtk/gtk.h>
/* stdlib.h is there for the EXIT_SUCCESS definition */
#include <stdlib.h>

/* max length of X-Window positioning string */
#define STRLEN 20
/* timeout for periodic functions */
#define TIMEOUT 5

/* the buffer, for ease of use as a global variable */
static GtkTextBuffer *buf;
/* the id for the key snooper, to remove it when
* closing the application */
static guint snooper_id;

/* Quit callback */
static void quit_cb(GtkWidget *widget, gpointer data) {
gtk_key_snooper_remove(snooper_id);
gtk_main_quit();
return;
}

/* key handler to exit on pressing Q or q */
static gint key_handler(GtkWidget *grab_widget, GdkEventKey *event,
gpointer data) {
/* if key == 'q'||'Q' call quit callback */
if((event->keyval == 'q') || (event->keyval == 'Q')) {
/* this somehow doesn't work as I expected might have
* to do with the buffer being a buffer and not just a
* pipe to the screen...
*/
gtk_text_buffer_insert_at_cursor(buf, "QUITTING", -1);
/* sleep for 100000 microseconds (== 0.1 seconds approx)
* so the user has the chance to see the QUITTING message
*/
g_usleep(1e5);
quit_cb(grab_widget, NULL);
return TRUE;
}
return FALSE;
}

/* the function that fills the buffer with Hs */
static gboolean print_h(gpointer data) {

/* insert H */
gtk_text_buffer_insert_at_cursor(buf, "H", -1);

/* return FALSE, if you want to remove
* the function from the main loop */
/* keep the function in the loop */
return TRUE;
}

/* main function */
int main(int argc, char *argv[]) {

GtkWidget *win, *textview;
GdkScreen *win_scr;
gint screen_width, screen_height;
gchar geom_str[STRLEN];

gtk_init(&argc, &argv);

/* initialize textview and window */
buf = gtk_text_buffer_new(NULL);
textview = gtk_text_view_new_with_buffer(buf);
/* set the textview to wrap long lines, so we don't need
* to bother with newlines between the 'H's:
* GTK_WRAP_CHAR is a GtkWrapMode meaning to allow to wrap
* text anywhere the cursor can appear (e.g. after each
* character)
*/
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textview),
GTK_WRAP_CHAR);
/* create the window object */
win = gtk_window_new(GTK_WINDOW_TOPLEVEL);

/* get screen size to make window full screen */
win_scr = gtk_window_get_screen(GTK_WINDOW(win));
screen_width = gdk_screen_get_width(win_scr);
screen_height = gdk_screen_get_height(win_scr);

/* I've done the size & position with a X-Window position string
* could also be done per GdkGeometry */
snprintf(geom_str, STRLEN, "%dx%d+0+0", screen_width,
screen_height);
gtk_window_parse_geometry(GTK_WINDOW(win), geom_str);
gtk_container_add(GTK_CONTAINER(win), textview);

/* signals to quit the application */
g_signal_connect(G_OBJECT(win), "destroy",
G_CALLBACK(quit_cb), NULL);
g_signal_connect(G_OBJECT(win), "delete_event",
G_CALLBACK(quit_cb), NULL);

/* install a key snooper to quit on pressing Q or q
* could probably also be done differently, but I couldn't find
* anything right now */
snooper_id = gtk_key_snooper_install(key_handler, NULL);

/* register periodic function to scroll characters until
* the user presses 'q' or 'Q' */
/* guint g_timeout_add(guint interval,
GSourceFunc function,
gpointer data);
* where interval is in milliseconds */
g_timeout_add(TIMEOUT, print_h, NULL);

/* show all widgets which are somehow packed in win */
gtk_widget_show_all(win);
/* gtk main loop */
gtk_main();
return EXIT_SUCCESS;

}

*** end code gtk_mod.c ***

--
Simon Gerber
simug...@student.ethz.ch

0 new messages