Adsense code (2021-10-05)
Wednesday, September 10, 2014
Free software, free society: Richard Stallman at TEDxGeneva 2014
It is the first TEDx talk of the founder of Free Software movement. Stallman, RMS for short, has changed the world with his vision of freedom for the digital age. He launched the GNU operating system, used with Linux as a component, and inspired the development of Creative Commons licences and Wikipedia project. In this talk, Stallman describes how nonfree programs give companies control of their users and what users can do in order to recover control over their computing.
Tuesday, September 2, 2014
Install Node.js on Ubuntu 14.04 via PPA
To install Node.js on Ubuntu 14.04, enter the command:
$ sudo add-apt-repository ppa:chris-lea/node.js
$ sudo apt-get update
$ sudo apt-get install nodejs
$ sudo add-apt-repository ppa:chris-lea/node.js
$ sudo apt-get update
$ sudo apt-get install nodejs
Linux command - tree, to list directories in a tree-like format
tree is a Linux command to list contents of directories in a tree-like format.
To install tree on Ubuntu:
$ sudo apt-get install tree
To install tree on Ubuntu:
$ sudo apt-get install tree
Thursday, August 28, 2014
nomacs, a free, small, fast and multi-format image viewer
nomacs is a free image viewer for windows, linux, and mac systems, which is licensed under the GNU General Public License v3. nomacs is small, fast and able to handle the most common image formats including RAW images. Additionally it is possible to synchronize multiple viewers. A synchronization of viewers running on the same computer or via LAN is possible. It allows to compare images and spot the differences (e.g. schemes of architects to show the progress).
To install nomacs on Ubuntu, add repository of ppa:nomacs/stable:
$ sudo add-apt-repository ppa:nomacs/stable
Then, enter the commands:
$ sudo apt-get update
$ sudo apt-get install nomacs
To install nomacs on Ubuntu, add repository of ppa:nomacs/stable:
$ sudo add-apt-repository ppa:nomacs/stable
Then, enter the commands:
$ sudo apt-get update
$ sudo apt-get install nomacs
Monday, August 25, 2014
Linux command, lscpu, to get CPU architecture information
The Linux command lscpu gathers CPU architecture information like number of CPUs, threads, cores, sockets, NUMA nodes, information about CPU caches, CPU family, model, bogoMIPS, byte order and stepping from sysfs and /proc/cpuinfo, and prints it in a human-readable format. It supports both online and offline CPUs. It can also print out in a parsable for‐mat, including how different caches are shared by different CPUs, which can be fed to other programs.
Code::Blocks + wxWidgets + wxSmith, to open text file with wxFileDialog
This video show steps to create wxWidgets project in Code::Blocks, using wxSmith, to open text file with wxFileDialog.
The only programmatic code is in wxTextFileFrame::OnMenuOpenSelected() of the file wxTextFileMain.cpp.
And also include wx/textfile.h.
The only programmatic code is in wxTextFileFrame::OnMenuOpenSelected() of the file wxTextFileMain.cpp.
void wxTextFileFrame::OnMenuOpenSelected(wxCommandEvent& event)
{
wxTextFile file;
wxString text;
int result;
result = FileDialog1->ShowModal();
if(result==wxID_OK){
SetTitle(FileDialog1->GetPath());
file.Open(FileDialog1->GetPath());
for(size_t i=0; i<file.GetLineCount(); i++){
text << file.GetLine(i) << _T("\r\n");
}
file.Close();
TextCtrl1->SetValue(text);
}
}
And also include wx/textfile.h.
#include <wx/textfile.h>
Sunday, August 24, 2014
"wxSmith plugin is not loaded, can not continue" in Code::Blocks
If you try to New a wxWidgets Project in Code::Blocls, with GUI Builder of wxSmith; may be you will be complained with error of "wxSmith plugin is not loaded, can not continue".
You have to install codeblocks-contrib, it is a contrib plugins for Code::Blocks IDE. To install it on Ubuntu, enter the command:
$ sudo apt-get install codeblocks-contrib
And then restart Code::Blocks.
You have to install codeblocks-contrib, it is a contrib plugins for Code::Blocks IDE. To install it on Ubuntu, enter the command:
$ sudo apt-get install codeblocks-contrib
And then restart Code::Blocks.
Wednesday, August 20, 2014
Add Button to wxWidgets project
Create wxWidgets Project with Code::Blocks, as shown in last post.
To add a Buuton, modify HelloWxMain.h and HelloWxMain.h.cpp.
No modify on HelloWxApp.h and HelloWxApp.cpp
To add a Buuton, modify HelloWxMain.h and HelloWxMain.h.cpp.
/***************************************************************
* Name: HelloWxMain.h
* Purpose: Defines Application Frame
* Author: Buddy ()
* Created: 2014-08-21
* Copyright: Buddy (linux-buddy.blogspot.com)
* License:
**************************************************************/
#ifndef HELLOWXMAIN_H
#define HELLOWXMAIN_H
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include "HelloWxApp.h"
class HelloWxFrame: public wxFrame
{
public:
HelloWxFrame(wxFrame *frame, const wxString& title);
~HelloWxFrame();
private:
//add for "Hello Button"
static const long idButtonHello;
wxBoxSizer* m_MainBox;
wxButton* m_ButtonHello;
void onClickHello(wxCommandEvent& event);
//end of "Hello Button"
enum
{
idMenuQuit = 1000,
idMenuAbout
};
void OnClose(wxCloseEvent& event);
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
DECLARE_EVENT_TABLE()
};
#endif // HELLOWXMAIN_H
/***************************************************************
* Name: HelloWxMain.cpp
* Purpose: Code for Application Frame
* Author: Buddy ()
* Created: 2014-08-21
* Copyright: Buddy (linux-buddy.blogspot.com)
* License:
**************************************************************/
#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif
#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__
#include "HelloWxMain.h"
//helper functions
enum wxbuildinfoformat {
short_f, long_f };
wxString wxbuildinfo(wxbuildinfoformat format)
{
wxString wxbuild(wxVERSION_STRING);
if (format == long_f )
{
#if defined(__WXMSW__)
wxbuild << _T("-Windows");
#elif defined(__WXMAC__)
wxbuild << _T("-Mac");
#elif defined(__UNIX__)
wxbuild << _T("-Linux");
#endif
#if wxUSE_UNICODE
wxbuild << _T("-Unicode build");
#else
wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
}
return wxbuild;
}
const long HelloWxFrame::idButtonHello = ::wxNewId(); //Hello Button
BEGIN_EVENT_TABLE(HelloWxFrame, wxFrame)
EVT_CLOSE(HelloWxFrame::OnClose)
EVT_MENU(idMenuQuit, HelloWxFrame::OnQuit)
EVT_MENU(idMenuAbout, HelloWxFrame::OnAbout)
EVT_BUTTON(idButtonHello, HelloWxFrame::onClickHello) //Hello Button
END_EVENT_TABLE()
HelloWxFrame::HelloWxFrame(wxFrame *frame, const wxString& title)
: wxFrame(frame, -1, title)
{
#if wxUSE_MENUS
// create a menu bar
wxMenuBar* mbar = new wxMenuBar();
wxMenu* fileMenu = new wxMenu(_T(""));
fileMenu->Append(idMenuQuit, _("&Quit\tAlt-F4"), _("Quit the application"));
mbar->Append(fileMenu, _("&File"));
wxMenu* helpMenu = new wxMenu(_T(""));
helpMenu->Append(idMenuAbout, _("&About\tF1"), _("Show info about this application"));
mbar->Append(helpMenu, _("&Help"));
SetMenuBar(mbar);
#endif // wxUSE_MENUS
#if wxUSE_STATUSBAR
// create a status bar with some information about the used wxWidgets version
CreateStatusBar(2);
SetStatusText(_("Hello Code::Blocks user!"),0);
SetStatusText(wxbuildinfo(short_f), 1);
#endif // wxUSE_STATUSBAR
//Hello Button
this->SetSizeHints(wxDefaultSize, wxDefaultSize);
m_MainBox = new wxBoxSizer(wxHORIZONTAL);
m_ButtonHello = new wxButton(
this,
idButtonHello,
_T("Hello...!"),
wxDefaultPosition,
wxDefaultSize,
0);
m_MainBox->Add(m_ButtonHello,
0,
wxALL,
5);
this->SetSizer(m_MainBox);
this->Layout();
}
HelloWxFrame::~HelloWxFrame()
{
}
void HelloWxFrame::OnClose(wxCloseEvent &event)
{
Destroy();
}
void HelloWxFrame::OnQuit(wxCommandEvent &event)
{
Destroy();
}
void HelloWxFrame::OnAbout(wxCommandEvent &event)
{
wxString msg = wxbuildinfo(long_f);
wxMessageBox(msg, _("Welcome to..."));
}
//Hello Button Clicked
void HelloWxFrame::onClickHello(wxCommandEvent &event)
{
wxString msg = _T("Hello World");
wxString info = _T("linux-buddy");
wxMessageBox(msg,
info,
wxOK | wxICON_INFORMATION,
this);
}
No modify on HelloWxApp.h and HelloWxApp.cpp
/***************************************************************
* Name: HelloWxApp.h
* Purpose: Defines Application Class
* Author: Buddy ()
* Created: 2014-08-21
* Copyright: Buddy (linux-buddy.blogspot.com)
* License:
**************************************************************/
#ifndef HELLOWXAPP_H
#define HELLOWXAPP_H
#include <wx/app.h>
class HelloWxApp : public wxApp
{
public:
virtual bool OnInit();
};
#endif // HELLOWXAPP_H
/***************************************************************
* Name: HelloWxApp.cpp
* Purpose: Code for Application Class
* Author: Buddy ()
* Created: 2014-08-21
* Copyright: Buddy (linux-buddy.blogspot.com)
* License:
**************************************************************/
#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif
#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__
#include "HelloWxApp.h"
#include "HelloWxMain.h"
IMPLEMENT_APP(HelloWxApp);
bool HelloWxApp::OnInit()
{
HelloWxFrame* frame = new HelloWxFrame(0L, _("wxWidgets Application Template"));
frame->Show();
return true;
}
Create wxWidgets Project with Code::Blocks
With Code::Blocks installed, you can create wxWidgets Project by new a wxWidgets project (File > Project... > Category GUI > wxWidgets Project).
If you have error of:
fatal error: wx/app.h: No such file or directory
You need to install wx-common, and also libwxgtk2.8-dev or libwxgtk3.0-dev.
$ sudo apt-get install wx-common
$ sudo apt-get install libwxgtk2.8-dev
$ sudo apt-get install libwxgtk3.0-dev
The videos show how to install and run, on Ubuntu Linux.
Next:
- Add Button to wxWidgets project
If you have error of:
fatal error: wx/app.h: No such file or directory
You need to install wx-common, and also libwxgtk2.8-dev or libwxgtk3.0-dev.
$ sudo apt-get install wx-common
$ sudo apt-get install libwxgtk2.8-dev
$ sudo apt-get install libwxgtk3.0-dev
The videos show how to install and run, on Ubuntu Linux.
Next:
- Add Button to wxWidgets project
Tuesday, August 19, 2014
Install Code::Blocks on Ubuntu
Code::Blocks is a cross-platform Integrated Development Environment (IDE). To install Code::Blocks on Ubuntu, simple search Code::Blocks in Ubuntu Software Center, or click this link.
Create Hello World (hello.cpp) using Code::Blocks
Create Hello World Project using Code::Blocks
Hello World Project with multiple files, Code::Blocks
- With Code::Blocks installed, you can create wxWidgets Project.
Create Hello World (hello.cpp) using Code::Blocks
Create Hello World Project using Code::Blocks
Hello World Project with multiple files, Code::Blocks
- With Code::Blocks installed, you can create wxWidgets Project.
Friday, August 15, 2014
Display info kernel module of with modinfo command
To display info kernel module, enter the command:
$ modinfo hello.ko
where hello.ko is the kernel object file.
$ modinfo hello.ko
where hello.ko is the kernel object file.
Develop Hello World of Linux Kernel Module
To create Linux Kernel Module, install the development tools:
Make and switch to a fold for your works. Create a c program, hello.c, and Makefile file. Please notice that both hello.c and Makefile have to be in the same folder.
hello.c
Makefile
Make the kernel module by enter the command:
$ make
Then insert the module with:
$ sudo insmod hello.ko
You can use the dmesg to view the kermel message printed by the code printk() in c program.
$ dmesg
List installed modules:
$ lsmod
To remove the module:
$ sudo rmmod hello.ko
$ sudo apt-get install gcc
$ sudo apt-get install kernel-package
$ sudo apt-get install make
Make and switch to a fold for your works. Create a c program, hello.c, and Makefile file. Please notice that both hello.c and Makefile have to be in the same folder.
hello.c
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
static int __init hello_init(void)
{
printk(KERN_INFO "Hello World! pid = %d\n", current->pid);
return 0;
}
static void __exit hello_exit(void)
{
printk(KERN_INFO "Hello Bye!\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Linux-Buddy: linux-buddy.blogspot.com");
MODULE_DESCRIPTION("Test Module: Hello World");
MODULE_VERSION("1.0");
Makefile
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Make the kernel module by enter the command:
$ make
Then insert the module with:
$ sudo insmod hello.ko
You can use the dmesg to view the kermel message printed by the code printk() in c program.
$ dmesg
List installed modules:
$ lsmod
To remove the module:
$ sudo rmmod hello.ko
Wednesday, August 13, 2014
Install Adobe Reader on Ubuntu 14.04
To install Adobe Reader (version 9.5.5) on Ubuntu 14.04, download from http://ardownload.adobe.com/pub/adobe/reader/unix/9.x/9.5.5/enu/AdbeRdr9.5.5-1_i386linux_enu.deb, then install with Ubuntu Software Center by right clicking on the file.
Adobe Reader run on Ubuntu 14.04
Friday, July 18, 2014
Flashback Ubuntu 14.04 to legacy desktop
To flashback Ubuntu 14.04 to use legacy desktop, to remove the 3D effect and animation, enter the command:
$ sudo apt-get install gnome-session-flashback
Log-out after installed. Click the Ubuntu button when log-in, select GNOME Flashback (Metacity).
$ sudo apt-get install gnome-session-flashback
Log-out after installed. Click the Ubuntu button when log-in, select GNOME Flashback (Metacity).
Tuesday, March 4, 2014
Install Node.js on Ubuntu
From Ubuntu 12.04 to 13.04, an old version (0.6.x) of Node is in the standard repository. To install, just run:
$ sudo apt-get install nodejs
Obtaining a recent version of Node or installing on older Ubuntu and other apt-based distributions may require a few extra steps. Example install:
$ sudo apt-get update
$ sudo apt-get install -y python-software-properties python g++ make
$ sudo add-apt-repository ppa:chris-lea/node.js
$ sudo apt-get update
$ sudo apt-get install nodejs
source: https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager#ubuntu-mint
$ sudo apt-get install nodejs
Obtaining a recent version of Node or installing on older Ubuntu and other apt-based distributions may require a few extra steps. Example install:
$ sudo apt-get update
$ sudo apt-get install -y python-software-properties python g++ make
$ sudo add-apt-repository ppa:chris-lea/node.js
$ sudo apt-get update
$ sudo apt-get install nodejs
source: https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager#ubuntu-mint
Wednesday, February 19, 2014
Get my Process ID and Parent Process ID in C, by calling getpid() and getppid()
Example to Get my Process ID (PID) and Parent Process ID (PPID) by calling getpid() and getppid().
#include <stdio.h>
#include <sys/types.h>
main(){
pid_t mypid;
pid_t myppid;
mypid = getpid();
myppid = getppid();
printf("My PID: %ld\n", (long)mypid);
printf("My PPID: %ld\n", (long)myppid);
}
getpid() and getppid() |
Get GNU libc version at run-time using C
To get GNU libc version in C language at run-time, you can call gnu_get_libc_version() and gnu_get_libc_release().
- gnu_get_libc_version() returns string that identifies the glibc version available on the system.
- gnu_get_libc_release() returns string indicates the release status of the glibc version available on the system.
And, you can retrieve _CS_GNU_LIBC_VERSION with confstr().
Here is example, test_version.c:
- gnu_get_libc_version() returns string that identifies the glibc version available on the system.
- gnu_get_libc_release() returns string indicates the release status of the glibc version available on the system.
And, you can retrieve _CS_GNU_LIBC_VERSION with confstr().
Here is example, test_version.c:
#include <stdio.h>
#include <gnu/libc-version.h>
#include <stdlib.h>
#include <unistd.h>
main(){
printf("GNU libc version: %s\n", gnu_get_libc_version());
printf("GNU libc release: %s\n", gnu_get_libc_release());
char *buf; size_t n;
n = confstr(_CS_GNU_LIBC_VERSION, NULL, (size_t)0);
if ((buf = malloc(n)) == NULL)
abort();
confstr(_CS_GNU_LIBC_VERSION, buf, n);
printf("_CS_GNU_LIBC_VERSION: %s\n", buf );
free( buf );
}
Get GNU libc version at run-time using C |
Tuesday, January 28, 2014
cal - Linux command to display calendar
Monday, January 27, 2014
GtkColorChooser example
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 |
#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 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 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.
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 |
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
Example:
$ ls | tee filelist.txt
Monday, January 6, 2014
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 |
#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() |
#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 |
#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 |
#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;
}
Subscribe to:
Posts (Atom)