Why cani i initialize "prefix" with a default string?




        guys, i am appending a piece of gtk test code.  it saves the
        internal filename buffer for "espeak -f %s ifbuf" to run
        on--to be the user's voice.  if i set 

        static char *prefix = "talk"; 

        then back up and enter another string in the box at the
        bottom, strange things happen.  Anybody know where i am
        messing up?

        tia,

        gary

        PS.  i am thru with the "play" button and callback, but
        would like to understand my errors above first!


-- 
 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.


/*
 * compile with:
 * gcc -Wall -Wextra -Werror -g x.c -o x1 `pkg-config --cflags gtk+-2.0` `pkg-config --libs gtk+-2.0`
 */

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

static int counter = 1;
static GtkWidget *label;
static char *suffix = ".txt";
static char *prefix;
char ifbuf[1024];  /*  internal filename buffer--CURRENT--for espeak -f   */


#define DOT '.'


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

    /*
       If counter is < 1, use markup to highlight text
     */
    if (counter >= 1)
      {
          if (prefix)
          {
              g_snprintf (buffer, 1023,
                          "<span foreground=\"red\" background=\"yellow\" size=\"x-large\">%s%c%d%s</span>", 
prefix, DOT, 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%c%d%s", prefix, DOT, 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 
     */

    if (prefix)
    {
        sprintf (ifbuf, "%s%c%d%s", prefix, DOT, counter, suffix);
        fprintf(stdout, "DEBUG: internsl filename = [%s]\n", ifbuf);
    }
    else
    {
        sprintf (ifbuf, "%d%s",  counter, suffix);
        fprintf(stdout, "DEBUG: internsl filename = [%s]\n", ifbuf);
    }

    update_label ();
    return;
}

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

    /*
       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 
     */

     if (prefix)
     {
        sprintf (ifbuf, "%s%c%d%s", prefix, DOT, counter, suffix);
        fprintf(stdout, "DEBUG: internsl filename = [%s]\n", ifbuf);
    }
    else
    {
        sprintf (ifbuf, "%d%s",  counter, suffix);
        fprintf(stdout, "DEBUG: internsl filename = [%s]\n", ifbuf);
    }

    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), "VoiceByComputer");
    g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
    vbox = gtk_vbox_new (FALSE, 5);
    gtk_container_add (GTK_CONTAINER (window), vbox);
    gtk_container_set_border_width (GTK_CONTAINER (window), 10);

    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 in filename prefix 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]