Computer Programming/Event driven programming
In this type of programming, a section of code is written to respond to each event. Events can be user events such as button clicks or mouse moves. Events can also be generated within the computer, for example timer events. Finally, the programmer can generate events.
Responding to an event is sometimes termed handling an event.
The program simply consists of sections that respond to events. These may, in turn use other sections (procedures, methods).
Example
editThis simple GTK+ program makes a GUI, with a button, that responds to the click event, after we enter a main-loop given by the gtk_main() function. When ever events occur, like "clicked" event, we lookup event-handlers connected to it via g_signal_connect() function, and delegate functions by invoking them. In this case the handler, btn_clicked is invoked.
/*
# GtkBook :
# A Book for GTK+ users.
# Copyright (C) July, 2005 Muthiah Annamalai
#
# This program is free software; Licensed under GPL.
*/
#include<gtk/gtk.h>
gboolean
btn_clicked(GtkWidget *w,gpointer data)
{
GtkButton *btn=GTK_BUTTON(w);
gtk_button_set_label(btn,"You Cliked Me");
return TRUE;
}
int main()
{
GtkWidget *w,*box,*btn;
gtk_init(NULL,NULL);
w=gtk_window_new(GTK_WINDOW_TOPLEVEL);
box=gtk_vbox_new(!FALSE,!FALSE);
gtk_window_set_title(GTK_WINDOW(w),"Button Widget");
btn=gtk_button_new_with_label("Hello World! Click Me:");
gtk_container_add(GTK_CONTAINER(w),btn);
g_signal_connect(G_OBJECT(btn),"clicked",G_CALLBACK(btn_clicked),NULL);
gtk_window_set_position(GTK_WINDOW(w),GTK_WIN_POS_CENTER);
gtk_widget_show_all(w);
gtk_main();
return 0;
}
/*
gcc -o button button.c -Wall -ggdb `pkg-config gtk+-2.0 --cflags --libs`
*/