a couple questions...




        two guys wrote the majority of this gtk widget code.  it is
        meant for the speech-impaired or mute.  they type into the
        editor spawned by gvim or kate, and when the file is written a
        speech tool of the linux box is their voice.  i keep track
        of the file by "inc" or "dec" in case somebody they are
        talking too didn't hear or understand part of the
        conversation.  if the user is a slow typist and has written
        a few paragraphs, he just looks back and forth thru the
        files to find the one that his friends want to hear again.

        i'll need a play button and callback that invokes, say,
        espeak -f on that file.

        if this much makes sense, below in the C code.

        tia for any insights.

        cheers,

        gary kline



-- 
 Gary Kline  kline thought org  http://www.thought.org  Public Service Unix
           Journey Toward the Dawn, E-Book: http://www.thought.org
          The 8.57a release of Jottings: http://jottings.thought.org
             Twenty-five years of service to the Unix community.




/*
 *
 * gcc -Wall -Wextra -Werror -g y.c -o y1 `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0`
 *
 */

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


#define DEFAULT "talk."
static int counter = 0;
static GtkWidget *label;
static char *suffix = ".txt";
static char *prefix = DEFAULT;


static void
update_label(void)
{
    char buffer[1024];
    memset(&buffer, 0, sizeof buffer);

    /*If counter is  1, use markup to highlight text*/
    if(counter >= 0 )
    {
        if(prefix)
            g_snprintf(buffer, 1023, "<span foreground=\"red\" background=\"yellow\" 
size=\"x-large\">%s%d%s</span>",prefix, counter, suffix);
        else
            g_snprintf(buffer, 1023, "<span foreground=\"red\" background=\"yellow\" 
size=\"x-large\">%d%s</span>", counter, suffix);
        gtk_label_set_markup(GTK_LABEL(label), buffer);
    }
    else
    {
        if(prefix)
            g_snprintf(buffer, 1023, "%s%d%s", prefix, counter, suffix);
        else
            g_snprintf(buffer, 1023, "%d%s", counter, suffix);
        gtk_label_set_label(GTK_LABEL(label), buffer);
    }
}

static void
inc_button_click_cb(GtkButton *button, gpointer data)
{
    (void)button;
    GtkWidget *dec_button = data;
    counter++;
    /* Change senstivity of the decrement button based on counter*/
    if(counter > 0 && !gtk_widget_is_sensitive(dec_button))
        gtk_widget_set_sensitive(dec_button, TRUE);
    /* Update label to show updated counter */
    update_label();
    return;
}

static void
dec_button_click_cb(GtkButton *button, gpointer data)
{
    (void)data;
    counter--;
    if (counter < 0)
    {
        counter = 0;
    }

    /* Change senstivity of the decrement button based on counter*/
    if(counter < 1 && gtk_widget_is_sensitive(GTK_WIDGET(button)))
        gtk_widget_set_sensitive(GTK_WIDGET(button), FALSE);
    /* Update label to show updated counter */
    update_label();
    return;
}


static void
entry_changed_cb(GtkEditable *editable, gpointer data)
{
    (void)data;
    /* Caller has to free the text, so call g_free */
    g_free(prefix);
    /* Get the complete text */
    prefix=gtk_editable_get_chars(editable,0, -1);
    /* Update label to show updated prefix */
    update_label();
    return;
}

int main(void)
{
    GtkWidget *button_inc;
    GtkWidget *button_dec;
    GtkWidget *entry_label;
    GtkWidget *entry;
    GtkWidget *window;
    GtkWidget *vbox;

    gtk_init(NULL, NULL);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_title(GTK_WINDOW(window), "TalkByComputer");
    gtk_container_set_border_width(GTK_CONTAINER(window), 10);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
    vbox = gtk_vbox_new(FALSE, 5);
    gtk_container_add(GTK_CONTAINER(window), vbox);

    label = gtk_label_new("");
    update_label();

    button_dec = gtk_button_new_with_label("Decrease counter");
    g_signal_connect(button_dec, "clicked", G_CALLBACK(dec_button_click_cb), NULL);
    gtk_widget_set_sensitive(button_dec, FALSE);

    button_inc = gtk_button_new_with_label("Increase counter");
    g_signal_connect(button_inc, "clicked", G_CALLBACK(inc_button_click_cb), button_dec);

    entry_label = gtk_label_new("Type the filename in the space below:");

    entry = gtk_entry_new();
    g_signal_connect(entry,"changed", G_CALLBACK(entry_changed_cb), NULL);

    gtk_box_pack_start(GTK_BOX(vbox), label, 0, 0, 0);
    gtk_box_pack_start(GTK_BOX(vbox), button_inc, 0, 0, 0);
    gtk_box_pack_start(GTK_BOX(vbox), button_dec, 0, 0, 0);
    gtk_box_pack_start(GTK_BOX(vbox), entry_label, 0, 0, 0);
    gtk_box_pack_start(GTK_BOX(vbox), entry, 0, 0, 0);

    gtk_widget_show_all(window);

    gtk_main();

    g_free(prefix);

    return 0;
}




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]