sabato 8 settembre 2012

Esempio GUI con Gtk/Glade(GtkBuilder) in C

Un ennesimo esempio con l'impiego delle GTK associate al Gui Designer denominato Glade


La caratteristica differente rispetto alle altre librerie viste finora e' la gestione in GTK delle proprieta' dello slider. Per inserire valore massimo, valore minimo e passo dello slider si deve associare un Adjustement,
Si deve quindi cliccare sulla proprieta' del wigdet e creare un nuovo adjustement, a questo punto scorrendo l'albero dei widget compare negli Oggetti un nuovo ramo; cliccando su questo e' possibile modificare le proprieta' dello slider

Schermata di Glade

Schermata di Glade . Gestione degli Adjustement
Glade permette la generazione di due formati file (che di fatto sono entrambi xml) ovvero LibGlade e LibGtkBuilder, nell'esempio di seguito e' stato usato il secondo sistema che di fatto e' quello piu' supportato da GTK2

main.c
---------------------------------------------------------

#include <gtk/gtk.h>
 GtkBuilder *gtkBuilder;
 GtkWidget *mainwin;
 GtkProgressBar *progress;
 GtkLabel *label;
 GtkHScale *scala;

void on_window1_delete_event()
{
  gtk_main_quit();
}

void on_hscale1_value_changed(GtkWidget* widget,gpointer user_data) {
   gdouble current_value = gtk_range_get_value(GTK_RANGE(scala));

 char buffer [14] = { '\0' };
 sprintf ( buffer, "%.0f", current_value);
 gtk_label_set_text(label,buffer);
 gtk_progress_bar_set_fraction(progress,current_value/100);
}


int main (int argc, char **argv) {

 gtk_set_locale();
 /* Initialize the widget set */
 gtk_init (&argc, &argv);
 /* Create the main window */
 gtkBuilder= gtk_builder_new();
 gtk_builder_add_from_file(gtkBuilder,"progressbar.xml",NULL);
 gtk_builder_connect_signals ( gtkBuilder, NULL );

 mainwin= GTK_WIDGET(gtk_builder_get_object(gtkBuilder,"window1"));
 label= GTK_WIDGET(gtk_builder_get_object(gtkBuilder,"label1"));
 progress= GTK_WIDGET(gtk_builder_get_object(gtkBuilder,"progressbar1"));
 scala= GTK_WIDGET(gtk_builder_get_object(gtkBuilder,"hscale1"));

 g_signal_connect (mainwin, "delete-event", G_CALLBACK (gtk_widget_hide_on_delete), NULL);

 g_object_unref ( G_OBJECT(gtkBuilder) );
 /* Show the application window */
 gtk_widget_show_all ( mainwin );
 /* Enter the main event loop, and wait for user interaction */
 gtk_main ();
 /* The user lost interest */
 return 0;
}

---------------------------------------------------------
progressbar.xml
---------------------------------------------------------

<?xml version="1.0"?>
<interface>
  <requires lib="gtk+" version="2.16"/>
  <!-- interface-naming-policy project-wide -->
  <object class="GtkWindow" id="window1">
    <property name="title" translatable="yes">Progress Bar</property>
    <property name="default_width">200</property>
    <property name="default_height">90</property>
    <signal name="delete_event" handler="on_window1_delete_event"/>
    <child>
      <object class="GtkVBox" id="vbox1">
        <property name="visible">True</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkHScale" id="hscale1">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="adjustment">adjustment1</property>
            <property name="lower_stepper_sensitivity">off</property>
            <property name="upper_stepper_sensitivity">off</property>
            <property name="digits">0</property>
            <property name="draw_value">False</property>
            <property name="value_pos">left</property>
            <signal name="value_changed" handler="on_hscale1_value_changed"/>
          </object>
          <packing>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="label1">
            <property name="visible">True</property>
            <property name="label" translatable="yes">0
</property>
          </object>
          <packing>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkProgressBar" id="progressbar1">
            <property name="visible">True</property>
          </object>
          <packing>
            <property name="position">2</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkAdjustment" id="adjustment1">
    <property name="upper">110</property>
    <property name="step_increment">1</property>
    <property name="page_increment">10</property>
    <property name="page_size">10</property>
  </object>
</interface>

---------------------------------------------------------
Makefile
--------------------------------------------------------

LIBS=$(shell pkg-config --cflags --libs gtk+-2.0)
CFLAGS=-Wall -g -export-dynamic
hello: main.c
gcc -o progress_bar main.c $(LIBS) $(CFLAGS)

--------------------------------------------------------
A questo link i file di esempio