XForms: Review and Tutorial
We can rely on fdesign to generate the code for the definition of all forms and objects. That leaves us with two tasks: the initialization code and the callback code.
The initialization code is shown in listing 1. This code defines the main() function in which XForms is initialized, the forms are created, the first form is put on-screen, and the main XForms loop is entered. You can see the call to create_the_forms(); the code of this function is generated by fdesign in the file design.c.
/* xviewfile.c -- main function of xviewfile */
#include \<>forms.h
#include "design.h"
void main (int argc, char **argv)
{
/* initialize XForms, parse arguments */
fl_initialize (argv [0], "XViewfile", 0, 0, &argc, argv);
/* create the forms (fdesign-generated function) */
create_the_forms ();
/* show the name input form */
fl_show_form (name_form, FL_PLACE_MOUSE, FL_FULLBORDER, "Enter a name");
/* enter XForms loop */
fl_do_forms ();
/* not reached... */
}
Now for the second task, the callback code. The callback functions are shown in listing 2. The names of the callback functions were already mentioned in the design specifications; it is therefore important that the exact names are used when writing the functions.
Each callback activated by an object is passed two arguments. The first argument is a pointer to a FL_OBJECT. This argument points by definition to the object which invoked the callback. We will see how this argument is used in the input_cb() function. The second argument is a long int—a value which can be set to any number when defining the callback in the designer. For example, you could have two different objects invoking the same callback function but providing different numeric arguments; inside the function you could distinguish by inspecting the second argument. We won't use this approach in this application.
The callback activated by the quit button is called exit_cb(). This is the easy one—all it does is exit(0). The callback for the input object, input_cb(), needs to perform more tasks. First, the name which was typed by the user must be retrieved. This is done by XForms' function fl_get_input(). Then, we must decide what to do with the input. As specified by the program description above, an empty name should lead to the removal of the browser window from the screen. A non-empty name should be interpreted as a request to view a file.
To accomplish this, input_cb() uses a static int variable to flag whether the browser form is yet on-screen. When a non-empty name is entered and when the browser form is not yet on the screen, fl_show_form() is called to show the browser. Similarly, when an empty name is entered and when the browser form is on-screen, fl_hide_form() is called to remove the browser form. (Instead of using an extra static int, you could also inspect the field int visible, which is part of the FL_FORM struct. I leave such optimizations to the reader.)
The browser itself is manipulated with browser-specific functions fl_clear_browser() and fl_load_browser().
/* callbacks.c --contains the callback routines */
#include <\<>forms.h>
#include "design.h"
void exit_cb (FL_OBJECT *obj, long data)
{
exit (0);
}
void input_cb (FL_OBJECT *obj, long data)
{
char const
*name; /* entered filename */
static int
browser_on_screen = 0; /* is browser on-screen yet ? */
/* determine the entered name */
name = fl_get_input (obj);
if (name && *name) /* a name was entered */
{
if (! browser_on_screen) /* make sure browser is there */
{
fl_show_form (browser_form, FL_PLACE_CENTER,
FL_FULLBORDER, name);
browser_on_screen = 1;
}
fl_clear_browser (file_browser); /* clear previous contents */
fl_load_browser (file_browser, /* load in file */
name);
}
else /* empty input was given */
{
if (browser_on_screen) /* remove browser from screen */
{
fl_hide_form (browser_form);
browser_on_screen = 0;
}
}
}
Finally, no program is complete without an automatic maintenance description in a Makefile. Here is an example:
# Makefile -- makefile for xviewfile.
# Used objects:
OBJ = xviewfile.o callbacks.o design.o
# Compilation flags:
CFLAGS = -c -O2 -Wall
# How to make the program:
xviewfile: $(OBJ)
$(CC) -o xviewfile $(OBJ) -lforms -lX11 -lm
-s
# How to clean up the mess.
clean:
rm -f $(OBJ)
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- RSS Feeds
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Home, My Backup Data Center
- A Topic for Discussion - Open Source Feature-Richness?
- What's the tweeting protocol?
- Dart: a New Web Programming Experience
- Developer Poll
- Trying to Tame the Tablet
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.




2 hours 49 min ago
5 hours 22 min ago
6 hours 39 min ago
7 hours 14 min ago
7 hours 36 min ago
12 hours 25 min ago
13 hours 12 min ago
14 hours 45 min ago
16 hours 22 min ago
18 hours 20 min ago