can anybody help me figure out why the two hbox buttons don't show?




people, i am trying to get the "decrement" and "increment" buttons 
to display side by side between the vertical stuff, but only the
filename and the window entry show up.  anybody know where i'm
messing up?

gary


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

#define DEFAULT "talk."
#define DOT '.'

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

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 
     */
    if (prefix == NULL)
    {
        puts("pre == null, gfreeing");
        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, *hbox;

    if (prefix == NULL)
    {
    puts("prefix is NULL");
    }


    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 ();

  hbox = gtk_hbox_new (FALSE, 2);
  gtk_container_add (GTK_CONTAINER (window), hbox);
  gtk_widget_show (hbox);


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

    button_inc = gtk_button_new_from_stock ("Increase counter");
    g_signal_connect (button_inc, "clicked", G_CALLBACK (inc_button_click_cb),
                      button_dec);
    //gtk_widget_show(button_inc);

    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 (hbox), button_inc, FALSE, FALSE, 2);
    gtk_box_pack_start (GTK_BOX (hbox), button_dec, FALSE, FALSE, 2);
    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]