NTW - Hello World in C

Source code for this tutorial

#include <ntwserver/ntw.h>
The first task is to include the ntw header file. This tutorial assumes you're on a Unix type machine and have installed the ntw server as a library, which also installs the header files.

/** Callback Function.
 * We can attach this function to a widget
 * so that something happens after an event.
finished(ntw_event_data *evdata, ntwWidget *userdata){
  printf("Window closed\n");
This is a callback function. A callback function is run in response to a user interface event, like "button clicked" or "mouse moved." They are always void functions. We'll learn more about callbacks later. For now, this one ends execution of the program.
/** Main.
int main (int argc, char **argv){
  /** Defining the widgets.
   * We'll use a window, button, and label to start off.
  ntwWidget *window;
  ntwWidget *button;
  ntwWidget *label;

Here we've defined the widgets we are going to use. This is a simple application with just three -- a window with a button in it that says "Click to Quit". When the user clicks the button or uses the titlebar to close the window, the program will call the finished function we just defined, which will exit.
  /** ntw_init must be called before we actually 
   * do anything.  This sets up the ntw server to listen on
   * our port. It also does some widget preparation.
  ntw_init(argc, argv);
We have to call ntw_init before doing anything else. This sets up communications between the client and our helloworld server. The code below won't be executed until a client connects.
  /** Now we'll create a window.
   * The first two arguments are the size of the window in pixels,
   * and the last is the height.
  window = ntw_window_new(150, 50, "Hello World");

  /** Creating the button.
   * Like the window, the button is a container that 
   * can hold one other widget.
  button = ntw_button_new();
  /** Now that we have a button, we can put it 
   * inside the window.
  ntw_container_add(window, button);

  /** Creating the label.
   * We don't just want a blank button. 
   * The label widget is a string of text that we 
   * can put in the button.
  label = ntw_label_new("Click to Quit");
  /** Now that we have a label, we can put it 
   * inside the button, the same way that we 
   * put the button in the window.
  ntw_container_add(button, label);
  /** Now let's handle some events by attaching callback functions to widgets.
   * This first will call finished when the button is clicked.
  ntw_add_callback(button, BUTTON_CLICKED, (func_ptr) &finished, NULL, ASYNC);
  /** And these will call finished if the window if the user closes the window.
  ntw_add_callback(window, DESTROY_EVENT, (func_ptr) &finished, NULL, ASYNC);
  ntw_add_callback(window, DELETE_EVENT, (func_ptr) &finished, NULL, ASYNC);

  /** By default, top level windows are not visible.
   * This is so that we don't see the widgets appear
   * one by one as they're added to the window.
   * Now that everything's ready, we can make the window
   * visible.

  /** ntw_main -- the main event loop
   * This is what handles events, and we have to call it after
   * everything else is prepared.

  return 0;


To compile, use this command:
gcc -I/usr/local/include/ -L/usr/local/lib -lntwserver -o helloworld helloworld.c
You should now have a helloworld executable, which you can run like this:

The ntw server will start. In another command window, start an ntw client. The default IP address and port should be ok when the client asks. After clicking OK, you should see the Hello World window appear.

You can also try running the client from another machine by using the server machine's IP address. Have fun!