Adsense code (2021-10-05)

Tuesday, January 28, 2014

cal - Linux command to display calendar

cal
cal command
cal is a Linux command run on Terminal, to display built-in calendar on Terminal in text mode.


Monday, January 27, 2014

GtkColorChooser example

GtkColorChooser
GtkColorChooser

#include <gtk/gtk.h>

static void 
chooser_color_activated(GtkColorChooser *chooser, 
 GdkRGBA *color, gpointer user_data)
{
 GtkLabel *label = user_data;
 gtk_label_set_text (label, gdk_rgba_to_string(color));
}

int main( int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *box;
    GtkWidget *label;
    GtkWidget *chooser;

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 250);
    gtk_window_set_title(GTK_WINDOW(window), "Linux-buddy.blogspot.com");

    box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);

 label = gtk_label_new("label");
    gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 3);

 //Create GtkColorChooser
    chooser = gtk_color_chooser_widget_new();
    gtk_box_pack_start(GTK_BOX(box), chooser, FALSE, FALSE, 3);
    g_signal_connect (GTK_COLOR_CHOOSER(chooser),
                    "color-activated", 
                    G_CALLBACK (chooser_color_activated), 
                    label);

    g_signal_connect(G_OBJECT(window), "destroy", 
        G_CALLBACK(gtk_main_quit), NULL);

    gtk_container_add(GTK_CONTAINER(window), box);
    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}


Note: double click the color buttons to activate color change.

Saturday, January 25, 2014

View hiden file start with a "."

Filenames which start with a "." (dot) are normally not shown. Use 'ls .*' or 'ls -a ...' to see them.


Wednesday, January 22, 2014

GTK+ example: Scale

GTK+ example: Scale
GTK+ example: Scale

#include <gtk/gtk.h>

static gboolean
scale_changed (GtkRange *range, gpointer data)
{
    gchar *text;
    gdouble value;
    GtkLabel *label;

    label = data;
    value = gtk_range_get_value (range);
    text = g_strdup_printf ("%.1f", value);
    gtk_label_set_text (label, text);
    g_free (text);

    return TRUE;
}

int main( int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *box;
    GtkWidget *label;
    GtkWidget *spinButton;
    GtkWidget *scale;

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 250);
    gtk_window_set_title(GTK_WINDOW(window), "Linux-buddy.blogspot.com");

    box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);

    //Create Label
    label = gtk_label_new("label");
    gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 3);

    //Create Scale
    scale = gtk_scale_new_with_range (
        GTK_ORIENTATION_HORIZONTAL, 
        0,      //gdouble min
        10,     //gdouble max
        0.5);     //gdouble step
    gtk_widget_set_hexpand (scale, TRUE);
    g_signal_connect(G_OBJECT(scale), "value-changed", 
        G_CALLBACK(scale_changed), label);

    gtk_box_pack_start(GTK_BOX(box), scale, FALSE, FALSE, 3);

    //Connects GCallback function gtk_main_quit to "destroy" signal for "window"
    g_signal_connect(G_OBJECT(window), "destroy", 
        G_CALLBACK(gtk_main_quit), NULL);

    gtk_container_add(GTK_CONTAINER(window), box);
    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}

Saturday, January 11, 2014

GTK+ example: SpinButton

SpinButton
SpinButton displayed on Ubuntu
#include <gtk/gtk.h>

static gboolean
spinButton_changed (GtkSpinButton *spin, gpointer data)
{
    gchar *text;
    int value;
    GtkLabel *label;

    label = data;
    value = gtk_spin_button_get_value_as_int (spin);
    text = g_strdup_printf ("%02d", value);
    gtk_label_set_text (label, text);
    g_free (text);

    return TRUE;
}

int main( int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *box;
    GtkWidget *label;
    GtkWidget *spinButton;

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 250);
    gtk_window_set_title(GTK_WINDOW(window), "Linux-buddy.blogspot.com");

    box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);

    //Create Label
    label = gtk_label_new("label");
    gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 3);

    //Create SpinButton
    spinButton = gtk_spin_button_new_with_range  (
        0,      //gdouble min
        10,     //gdouble max
        2);     //gdouble step
    gtk_widget_set_hexpand (spinButton, TRUE);
    g_signal_connect(G_OBJECT(spinButton), "value-changed", 
        G_CALLBACK(spinButton_changed), label);

    gtk_box_pack_start(GTK_BOX(box), spinButton, FALSE, FALSE, 3);

    //Connects GCallback function gtk_main_quit to "destroy" signal for "window"
    g_signal_connect(G_OBJECT(window), "destroy", 
        G_CALLBACK(gtk_main_quit), NULL);

    gtk_container_add(GTK_CONTAINER(window), box);
    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}



Remark: wrong icon displayed on Lubuntu:
SpinButton on Lubuntu
SpinButton with wrong icon displayed on Lubuntu

Thursday, January 9, 2014

Download file using SSH with scp command

The Linux command scp, securely copying files, uses ssh for data transfer, to copy file to, from, or between different hosts.

To download file from server using ssh, using the command:

$ scp user@hostname:file local-directory

example:
scp pi@192.168.111.109:~/log1 ~/PiLog
where pi is user name in server with IP 192.168.111.109, copy ~/log1 from server to local directory ~/PiLog.

scp
scp



Redirect command output to both stdout and file

The linux command tee read from standard input and write to standard output and files.

Example:
$ ls | tee filelist.txt

Monday, January 6, 2014

GTK+ example: Button

GTK+ example: Button
GTK+ example: Button

#include <gtk/gtk.h>

static void
button_toggled (GtkWidget *button, gpointer data)
{
    GtkLabel *label = data;

    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(button)))
        gtk_label_set_text (label, "Active");
    else {
        gtk_label_set_text (label, "In-Active");
    }
}

int main( int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *box;
    GtkWidget *label;
    GtkWidget *button;

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 250);
    gtk_window_set_title(GTK_WINDOW(window), "Linux-buddy.blogspot.com");

    box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);

    //Create Label
    label = gtk_label_new("label");
    gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 3);

    //Create Button
    button = gtk_toggle_button_new_with_label ("Button");
    gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 3);

    g_signal_connect (GTK_TOGGLE_BUTTON (button), "toggled", 
                    G_CALLBACK (button_toggled), label);

    //Connects GCallback function gtk_main_quit to "destroy" signal for "window"
    g_signal_connect(G_OBJECT(window), "destroy", 
        G_CALLBACK(gtk_main_quit), NULL);

    gtk_container_add(GTK_CONTAINER(window), box);
    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}

Sunday, January 5, 2014

GTK+ example: Switch

GTK+ example: Switch
GTK+ example: Switch

#include <gtk/gtk.h>

static void
switch_active (GtkSwitch *myswitch, GParamSpec *pspec, GtkLabel *label)
{
    if (gtk_switch_get_active (myswitch)){
        gtk_label_set_text (label, "Active");
    }else{
        gtk_label_set_text (label, "In-Active");
    }
}

int main( int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *box;
    GtkWidget *label1;
    GtkWidget *label2;
    GtkWidget *switch1;
    GtkWidget *switch2;

    GObject *context_object;

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 250);
    gtk_window_set_title(GTK_WINDOW(window), "Linux-buddy.blogspot.com");

    box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);

    //Create labels
    label1 = gtk_label_new("label1");
    gtk_box_pack_start(GTK_BOX(box), label1, FALSE, FALSE, 3);
    label2 = gtk_label_new("label2");
    gtk_box_pack_start(GTK_BOX(box), label2, FALSE, FALSE, 3);

    //Create Switchs
    //switch1 with default actived
    switch1 = gtk_switch_new ();
    gtk_switch_set_active (GTK_SWITCH (switch1), TRUE);
    gtk_box_pack_start(GTK_BOX(box), switch1, FALSE, FALSE, 3);
    //switch2 with default in-actived
    switch2 = gtk_switch_new ();
    gtk_switch_set_active (GTK_SWITCH (switch2), FALSE);
    gtk_box_pack_start(GTK_BOX(box), switch2, FALSE, FALSE, 3);

    g_signal_connect(GTK_SWITCH (switch1), "notify::active", 
        G_CALLBACK (switch_active), label1);
    g_signal_connect(GTK_SWITCH (switch2), "notify::active", 
        G_CALLBACK (switch_active), label2);

    //Connects GCallback function gtk_main_quit to "destroy" signal for "window"
    g_signal_connect(G_OBJECT(window), "destroy", 
        G_CALLBACK(gtk_main_quit), NULL);

    gtk_container_add(GTK_CONTAINER(window), box);
    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}

Thursday, January 2, 2014

GTK+ example: pass more data to callback using g_object_set_data()

This example show how to attach data to GTK+ object, then pass to callback function.
pass more data to callback using g_object_set_data()
pass more data to callback using g_object_set_data()
#include <gtk/gtk.h>

static void
accept_clicked (GtkButton *button, GObject *context_object)
{
    GtkLabel *accept_lable1 = g_object_get_data (context_object, "label1");
    GtkLabel *accept_lable2 = g_object_get_data (context_object, "label2");
    GtkEntry *accept_entry1 = g_object_get_data (context_object, "entry1");
    GtkEntry *accept_entry2 = g_object_get_data (context_object, "entry2");

    const char *entry1_in = gtk_entry_get_text (accept_entry1);
    gtk_label_set_text (accept_lable1, entry1_in);
    const char *entry2_in = gtk_entry_get_text (accept_entry2);
    gtk_label_set_text (accept_lable2, entry2_in);
}

int main( int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *box;
    GtkWidget *label1;
    GtkWidget *label2;
    GtkWidget *entry1;
    GtkWidget *entry2;
    GtkWidget *buttonAccept;

    GObject *context_object;

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 250);
    gtk_window_set_title(GTK_WINDOW(window), "Linux-buddy.blogspot.com");

    box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);

    //Create label
    label1 = gtk_label_new("label1");
    gtk_box_pack_start(GTK_BOX(box), label1, FALSE, FALSE, 3);
    label2 = gtk_label_new("label2");
    gtk_box_pack_start(GTK_BOX(box), label2, FALSE, FALSE, 3);

    //Create entryBox
    entry1 = gtk_entry_new ();
    gtk_box_pack_start(GTK_BOX(box), entry1, FALSE, FALSE, 3);
    entry2 = gtk_entry_new ();
    gtk_box_pack_start(GTK_BOX(box), entry2, FALSE, FALSE, 3);
    
    buttonAccept = gtk_button_new_with_label ("Accept");
    gtk_box_pack_start(GTK_BOX(box), buttonAccept, FALSE, FALSE, 3);
    g_object_set_data(G_OBJECT(buttonAccept), "label1", label1);
    g_object_set_data(G_OBJECT(buttonAccept), "label2", label2);
    g_object_set_data(G_OBJECT(buttonAccept), "entry1", entry1);
    g_object_set_data(G_OBJECT(buttonAccept), "entry2", entry2);
    g_signal_connect(GTK_BUTTON (buttonAccept), "clicked", 
        G_CALLBACK (accept_clicked), buttonAccept);

    //Connects GCallback function gtk_main_quit to "destroy" signal for "window"
    g_signal_connect(G_OBJECT(window), "destroy", 
        G_CALLBACK(gtk_main_quit), NULL);

    gtk_container_add(GTK_CONTAINER(window), box);
    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}

GTK+ example: Entry

Entry is EditText for GTK+
GTK+ example: Entry
GTK+ example: Entry
#include <gtk/gtk.h>

static void
entry_activate (GtkEntry *entry, GtkLabel *label)
{
    const char *entry_in = gtk_entry_get_text (entry);
    gtk_label_set_text (label, entry_in);
}

int main( int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *box;
    GtkWidget *label;
    GtkWidget *entry;

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 250);
    gtk_window_set_title(GTK_WINDOW(window), "Linux-buddy.blogspot.com");

    box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);

    //Create label
    label = gtk_label_new("label");
    gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 3);

    //Create entryBox
    entry = gtk_entry_new ();
    gtk_box_pack_start(GTK_BOX(box), entry, FALSE, FALSE, 3);
    g_signal_connect (GTK_ENTRY(entry), "activate", 
        G_CALLBACK(entry_activate), label);
    
    //Connects GCallback function gtk_main_quit to "destroy" signal for "window"
    g_signal_connect(G_OBJECT(window), "destroy", 
        G_CALLBACK(gtk_main_quit), NULL);

    gtk_container_add(GTK_CONTAINER(window), box);
    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}

Wednesday, January 1, 2014

GTK+ example: handle "toggled" signal of RadioButton and determine the active selected RadioButton in group

This example implement radioButton_toggled() to handle the "toggled" signal from RadioButtons. Inside radioButton_toggled(), check if the button is active by calling gtk_toggle_button_get_active(), to determine the active selected RadioButton in group, then display on Label.

handle "toggled" signal of RadioButton
handle "toggled" signal of RadioButton

#include <gtk/gtk.h>

static void
radioButton_toggled (GtkWidget *button, GtkLabel  *label)
{
    char *button_state;
    const char *button_label = 
        gtk_button_get_label (GTK_BUTTON (button));

    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button))){
        button_state = "on";
        gtk_label_set_text (label, button_label);
    }
    else {
        button_state = "off";
    }

    g_print ("%s : %s\n", button_label, button_state);
}

int main( int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *box;
    GtkWidget *label;

    GtkWidget *radioButton1;
    GtkWidget *radioButton2;
    GtkWidget *radioButton3;

    GtkWidget *radioButton4;
    GtkWidget *radioButton5;
    GSList *groupB;

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 400, 250);
    gtk_window_set_title(GTK_WINDOW(window), "Linux-buddy.blogspot.com");
    box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);

    label = gtk_label_new ("");
    gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 3);

    //new RadioButton1, 2, 3 in same group
    radioButton1 = gtk_radio_button_new_with_label (
        NULL, "Radio Button 1");
    radioButton2 = gtk_radio_button_new_with_label_from_widget (
        GTK_RADIO_BUTTON (radioButton1), "Radio Button 2");
    radioButton3 = gtk_radio_button_new_with_label_from_widget (
        GTK_RADIO_BUTTON (radioButton1), "Radio Button 3");
    g_signal_connect (GTK_TOGGLE_BUTTON (radioButton1), "toggled", 
        G_CALLBACK (radioButton_toggled), label);
    g_signal_connect (GTK_TOGGLE_BUTTON (radioButton2), "toggled", 
        G_CALLBACK (radioButton_toggled), label);
    g_signal_connect (GTK_TOGGLE_BUTTON (radioButton3), "toggled", 
        G_CALLBACK (radioButton_toggled), label);
    gtk_box_pack_start(GTK_BOX(box), radioButton1, FALSE, FALSE, 3);
    gtk_box_pack_start(GTK_BOX(box), radioButton2, FALSE, FALSE, 3);
    gtk_box_pack_start(GTK_BOX(box), radioButton3, FALSE, FALSE, 3);

    //new RadioButton4, 5 in another group
    radioButton4 = gtk_radio_button_new_with_label (
        NULL, "Group B Button 4");
    groupB = gtk_radio_button_get_group (GTK_RADIO_BUTTON (radioButton4));
    radioButton5 = gtk_radio_button_new_with_label (
        groupB, "Group B Button 5");
    gtk_box_pack_start(GTK_BOX(box), radioButton4, FALSE, FALSE, 3);
    gtk_box_pack_start(GTK_BOX(box), radioButton5, FALSE, FALSE, 3);
    g_signal_connect (radioButton4, "toggled", 
        G_CALLBACK (radioButton_toggled), label);
    g_signal_connect (radioButton5, "toggled", 
        G_CALLBACK (radioButton_toggled), label);

    //Connects GCallback function gtk_main_quit to "destroy" signal for "window"
    g_signal_connect(G_OBJECT(window), "destroy", 
        G_CALLBACK(gtk_main_quit), NULL);

    gtk_container_add(GTK_CONTAINER(window), box);
    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}