For anyone using these as examples in the future: This is the same program I posted earlier, except the user interface is compiled inline as a string with the source code (rather than loaded in at runtime from an XML file).
[The easiest way to do this is to generate the UI as a glade {XML} file from glade, then replace all " with \" and remove all newline characters {i used the kate text editor to replace \n with nothing (a literal nothing, not a space or a \0 null character)}. Copy the result into the source code, as seen below.]
[The easiest way to do this is to generate the UI as a glade {XML} file from glade, then replace all " with \" and remove all newline characters {i used the kate text editor to replace \n with nothing (a literal nothing, not a space or a \0 null character)}. Copy the result into the source code, as seen below.]
C:
//clang `pkg-config --cflags gtk+-3.0` -o gtk_example gtk_example.c `pkg-config --libs gtk+-3.0`
//can also use gcc
//This command won't work in the fish shell, use a different shell.
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
GtkBuilder *builder;
static void print_time (GtkWidget *widget, gpointer data)
{
GObject *label;
label = gtk_builder_get_object (builder, "label1"); //get the label1 GObject from the builder
///////////////////////////////////////Run A Command and Get the Output
FILE *file_pointer;
char variable[256];
/* Open the command for reading. */
file_pointer = popen("/bin/date \"+%r\"", "r");
if (file_pointer == NULL) {
printf("Didn't Work\n" );
exit(1);
}
// Read in one line of file_pointer to variable
fgets(variable, sizeof(variable), file_pointer);
/* close */
pclose(file_pointer);
///////////////////////////////////////I found this portion from a bing search.
//set the label.
g_object_set (label,"label",variable,NULL); //a GObject, Property Name, Property Value, NULL indicates no further properties to parse.
}
static void print_hello_label (GtkWidget *widget, gpointer data)
{
GObject *label;
label = gtk_builder_get_object (builder, "label1");
g_object_set (label,"label","Hello World!",NULL);
}
int main (int argc, char *argv[])
{
GObject *window;
GObject *button;
GError *error = NULL;
///////////////////////////
const char ui_string[] = "<interface><object id=\"window\" class=\"GtkWindow\"><property name=\"visible\">True</property><property name=\"title\">Grid</property><property name=\"window-position\">center</property><property name=\"default-width\">400</property><property name=\"default-height\">300</property><property name=\"border-width\">5</property><child><object id=\"vbox\" class=\"GtkBox\"><property name=\"homogeneous\">True</property><property name=\"visible\">True</property><property name=\"orientation\">vertical</property><property name=\"vexpand-set\">True</property><property name=\"vexpand\">True</property><child><object id=\"hbox\" class=\"GtkBox\"><property name=\"homogeneous\">True</property><property name=\"visible\">True</property><property name=\"orientation\">horizontal</property><property name=\"hexpand-set\">True</property><property name=\"hexpand\">True</property><child><object id=\"button1\" class=\"GtkButton\"><property name=\"visible\">True</property><property name=\"label\">Get Time</property></object><packing><property name=\"expand\">True</property></packing></child><child><object id=\"button2\" class=\"GtkButton\"><property name=\"visible\">True</property><property name=\"label\">Say Hello</property></object><packing><property name=\"expand\">True</property></packing></child><child><object id=\"quit\" class=\"GtkButton\"><property name=\"visible\">True</property><property name=\"label\">Quit</property></object><packing><property name=\"expand\">True</property></packing></child></object><packing></packing></child><child><object id=\"label1\" class=\"GtkLabel\"><property name=\"visible\">True</property><property name=\"label\">Am I Connected?</property></object></child></object><packing></packing></child></object></interface>";
///////////////////////////
gtk_init (&argc, &argv);
/* Construct a GtkBuilder instance and load our UI description */
builder = gtk_builder_new ();
//if (gtk_builder_add_from_file (builder, "builder.ui", &error) == 0)
if (gtk_builder_add_from_string (builder, ui_string, strlen(ui_string), &error) == 0)
{
g_printerr ("Error loading ui_string: %s\n", error->message);
g_clear_error (&error);
return 1;
}
/* Connect signal handlers to the constructed widgets. */
window = gtk_builder_get_object (builder, "window");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
button = gtk_builder_get_object (builder, "button1");
g_signal_connect (button, "clicked", G_CALLBACK (print_time), NULL);
button = gtk_builder_get_object (builder, "button2");
g_signal_connect (button, "clicked", G_CALLBACK (print_hello_label), NULL);
button = gtk_builder_get_object (builder, "quit");
g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL);
gtk_main ();
return 0;
}