Adsense code (2021-10-05)

Tuesday, December 31, 2013

GTK+ example: RadioButton

GTK+ example: RadioButton
GTK+ example: RadioButton

#include <gtk/gtk.h>

int main( int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *box;
    GtkWidget *radioButton1;
    GtkWidget *radioButton2;
    GtkWidget *radioButton3;
    GtkWidget *radioButton4;
    GtkWidget *radioButton5;

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

    //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");

    //new RadioButton4, 5 in another group
    radioButton5 = gtk_radio_button_new_with_label (
        NULL, "Another Group Button 5");
    radioButton4 = gtk_radio_button_new_with_label_from_widget (
        GTK_RADIO_BUTTON (radioButton5), "Another Group Button 4");

    box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
    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);
    gtk_box_pack_start(GTK_BOX(box), radioButton4, FALSE, FALSE, 3);
    gtk_box_pack_start(GTK_BOX(box), radioButton5, 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;
}



Next: Handle "toggled" signal of RadioButton and determine the active selected RadioButton in group

Monday, December 30, 2013

GTK+ example: LinkButton, button to open URL in browser

LinkButton
LinkButton

#include <gtk/gtk.h>

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

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

    linkbutton_linuxbuddy = gtk_link_button_new ("linux-buddy.blogspot.com");
    gtk_link_button_set_uri(GTK_LINK_BUTTON(linkbutton_linuxbuddy), "http://linux-buddy.blogspot.com/");
    linkbutton_google = gtk_link_button_new ("Google");
    gtk_link_button_set_uri(GTK_LINK_BUTTON(linkbutton_google), "http://google.com/");
    linkbutton_youtube = gtk_link_button_new ("Youtube");
    gtk_link_button_set_uri(GTK_LINK_BUTTON(linkbutton_youtube), "http://www.youtube.com");

    box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
    gtk_box_pack_start(GTK_BOX(box), linkbutton_linuxbuddy, FALSE, FALSE, 3);
    gtk_box_pack_start(GTK_BOX(box), linkbutton_google, FALSE, FALSE, 3);
    gtk_box_pack_start(GTK_BOX(box), linkbutton_youtube, FALSE, FALSE, 3);

    gtk_container_add(GTK_CONTAINER(window), box);

    //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_widget_show_all(window);
    gtk_main();

    return 0;
}

Saturday, December 28, 2013

GTK+ example create menu bar

Example to create menu bar with GTK+ in C language.

GTK+ example create menu bar
GTK+ example create menu bar

#include <gtk/gtk.h>

static void open_activated(GtkWidget *f)
{
    g_print("File -> Open activated.\n");
}

static void quit_activated(GtkWidget *f)
{
    g_print("File -> Quit activated...bye.\n");
    gtk_main_quit();
}

static void another_activated(GtkWidget *widget, gpointer data)
{
    g_print("%s clicked.\n", (gchar*)data);
}

int main( int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *box;
    GtkWidget *menubar;
    GtkWidget *filemenu;
    GtkWidget *file;
    GtkWidget *open;
    GtkWidget *quit;

    GtkWidget *anothermenu;
    GtkWidget *another;
    GtkWidget *anothermenuitem;

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

    menubar = gtk_menu_bar_new();
    
    filemenu = gtk_menu_new();
    file = gtk_menu_item_new_with_label("File");
    open = gtk_menu_item_new_with_label("Open");
    quit = gtk_menu_item_new_with_label("Quit");
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(file), filemenu);
    gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), open);
    gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), quit);
    gtk_menu_shell_append(GTK_MENU_SHELL(menubar), file);
    //Connects GCallback function open_activated to "activate" signal for "open" menu item
    g_signal_connect(G_OBJECT(open), "activate", G_CALLBACK(open_activated), NULL);
    //Connects GCallback function quit_activated to "activate" signal for "quit" menu item
    g_signal_connect(G_OBJECT(quit), "activate", G_CALLBACK(quit_activated), NULL);

    anothermenu = gtk_menu_new();
    another = gtk_menu_item_new_with_label("Another");
    anothermenuitem = gtk_menu_item_new_with_label("Another Menu Item");
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(another), anothermenu);
    gtk_menu_shell_append(GTK_MENU_SHELL(anothermenu), anothermenuitem);
    gtk_menu_shell_append(GTK_MENU_SHELL(menubar), another);
    //Connects GCallback function another_activated to "activate" signal for another
    g_signal_connect(G_OBJECT(anothermenuitem), "activate", G_CALLBACK(another_activated), "anothermenuitem");
    g_signal_connect(G_OBJECT(another), "activate", G_CALLBACK(another_activated), "another");

    box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
    gtk_box_pack_start(GTK_BOX(box), menubar, FALSE, FALSE, 3);
    gtk_container_add(GTK_CONTAINER(window), box);

    //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_widget_show_all(window);
    gtk_main();

    return 0;
}

Example of using GtkBox

GtkBox
GtkBox

#include <gtk/gtk.h>

static void myButtonClicked(GtkWidget *widget, gpointer   data)
{
    g_print("Button clicked: Hello GTK+\n");
}

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

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 300, 250);

    /*
    warning: 
        ‘gtk_vbox_new’ is deprecated 
        Use 'gtk_box_new' instead
    */
    //box = gtk_vbox_new(FALSE, 0);
    box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);

    myImage = gtk_image_new_from_file ("Linux.png");

    myButton = gtk_button_new_with_label("Hello GTK+ from Linux-Buddy");
    g_signal_connect(myButton, "clicked", 
        G_CALLBACK(myButtonClicked), NULL);

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

    
    gtk_box_pack_start(GTK_BOX(box), myImage, TRUE, TRUE, 5);
    gtk_box_pack_end(GTK_BOX(box), myButton, TRUE, TRUE, 5);

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

    gtk_main();
    return(0);
}

Thursday, December 26, 2013

GTK+3 example of Grid

GTK+3 example of Grid
GTK+3 example of Grid

#include <gtk/gtk.h>

int main(int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *myGrid;
    GtkWidget *button1, *button2, *button3, *button4, 
        *button5, *button6, *button7;


    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 300, 250);

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

    myGrid = gtk_grid_new();

    button1 = gtk_button_new_with_label("Button 1");
    button2 = gtk_button_new_with_label("Button 2");
    button3 = gtk_button_new_with_label("Button 3");
    button4 = gtk_button_new_with_label("Button 4");
    button5 = gtk_button_new_with_label("Button 5");
    button6 = gtk_button_new_with_label("Button 6");
    button7 = gtk_button_new_with_label("Button 7");
    
    gtk_grid_attach(GTK_GRID(myGrid), button1, 0, 0, 1, 1);
    gtk_grid_attach(GTK_GRID(myGrid), button2, 1, 0, 1, 1);
    gtk_grid_attach(GTK_GRID(myGrid), button3, 2, 0, 1, 1);
    gtk_grid_attach(GTK_GRID(myGrid), button4, 3, 0, 1, 1);
    gtk_grid_attach(GTK_GRID(myGrid), button5, 0, 1, 3, 1);
    gtk_grid_attach(GTK_GRID(myGrid), button6, 3, 1, 1, 2);
    gtk_grid_attach(GTK_GRID(myGrid), button7, 0, 2, 1, 1);

    gtk_container_add(GTK_CONTAINER (window), myGrid);
    gtk_widget_show_all (window);

    gtk_main();
    return(0);
}

GTK+ example with image

GTK+ example with image
GTK+ example with image

#include <gtk/gtk.h>

static void myButtonClicked(GtkWidget *widget, gpointer   data)
{
    g_print("Button clicked: Hello GTK+\n");
}

int main(int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *myImage;
    GtkWidget *myButton;
    GtkWidget *myPaned;

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 300, 250);

    myPaned = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);

    myImage = gtk_image_new_from_file ("Linux.png");

    myButton = gtk_button_new_with_label("Hello GTK+ from Linux-Buddy");
    g_signal_connect(myButton, "clicked", 
        G_CALLBACK(myButtonClicked), NULL);

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

    gtk_paned_add1 (GTK_PANED(myPaned), myImage);
    gtk_paned_add2 (GTK_PANED(myPaned), myButton);

    gtk_container_add(GTK_CONTAINER (window), myPaned);
    gtk_widget_show_all (window);

    gtk_main();
    return(0);
}

"Hello World" of GTK+ 3 with C language

"Hello World" of GTK+ 3 with C language
"Hello World" of GTK+ 3 with C language
hello.c
#include <gtk/gtk.h>

static void myButtonClicked(GtkWidget *widget, gpointer   data)
{
    g_print("Button clicked: Hello GTK+\n");
}

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

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 300, 250);

    myButton = gtk_button_new_with_label("Hello GTK+ from Linux-Buddy");
    g_signal_connect(myButton, "clicked", 
        G_CALLBACK(myButtonClicked), NULL);

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

    gtk_container_add(GTK_CONTAINER (window), myButton);
    gtk_widget_show(myButton);
    gtk_widget_show(window);

    gtk_main();
    return(0);
}
Compile with:
gcc hello.c -o hello `pkg-config --cflags --libs gtk+-3.0`
Run:
./hello

Install GTK+3 on Ubuntu

To install GTK+3 development tools on Ubuntu Linux, enter the command in terminal:

sudo apt-get install libgtk-3-dev


Change computer name for Ubuntu Linux

To change your computer name in Ubuntu Linux, you have to be the administrator of your system. Enter the command in Terminal:

gksudo gedit /etc/hostname

Edit the file /etc/hostname, replace the original computer name with new one. Then save and exit.

And also need to edit /etc/hosts, replace the entry of the original computer name with the new name.

$ gksudo gedit /etc/hosts

Then reboot your computer.

Thursday, December 19, 2013

Sublime Text, a sophisticated text editor for code, markup and prose

Sublime Text is a sophisticated text editor for code, markup and prose. It is available for OS X, Windows and Linux.

Sublime Text uses a custom UI toolkit, optimized for speed and beauty, while taking advantage of native functionality on each platform.

link: http://www.sublimetext.com/

Sublime Text
Sublime Text, a sophisticated text editor for code, markup and prose



Tuesday, December 17, 2013

Geany - small and fast code editor and IDE

Geany is a text editor using the GTK2 toolkit with basic features of an integrated development environment. It was developed to provide a small and fast IDE, which has only a few dependencies from other packages. It supports many filetypes and has some nice features.


To install geany:
$sudo apt-get install geany