An Introduction To Embedded Tk (page 6 of 32)

[Previous Page][Next Page][Table of Contents]

5 The Main Body Of An ET Application

The main() routines for ET applications all look pretty much alike. Here's a template:

  void main(int argc, char **argv){
    Et_Init(&argc,argv);   /* Start the Tcl/Tk interpreter */
    /* Create new Tcl/Tk commands here */
    /* Initialize data structures here */
    /* Create windows for the application here */
    Et_MainLoop();         /* The event loop */
  }
When you need to write an ET application, but you aren't sure where to begin, this template is a good starting point. Type in the above template and make sure you can successfully compile and run it. (The program that results from compiling the template creates a blank window that doesn't respond to any mouse or keyboard inputs. Its the equivalent of ``wish /dev/null''.) After you get the template running, slowly begin adding bits of code, recompiling and testing as you go, until you have a complete application.

Let's take a closer look at each line of the template, so that you can better understand what is going on.

The first line of main() is a call to the Et_Init() procedure. The Et_Init() procedure initializes the ET system and the Tcl/Tk interpreter. It must be called before any other ET function or statement. The parameters are the argc and argv formal parameters of main(). Et_Init() uses these parameters to look for command-line options. ET currently understands four:

Notice the ``&'' before the argc parameter to Et_Init(). The number of command line arguments is passed to Et_Init() by address, not by value. This is so Et_Init() can change the value of argc. Whenever Et_Init() sees one of the above command-line options, it removes that option from the option list in argc and argv. Hence, after Et_Init() returns, only application-specific command line options remain.

For example, suppose you invoke an ET program like this:

    myapp -quiet -display stego:0 file1.data
The values of argc and argv passed into the Et_Init() function are:
    argc = 5
    argv = { "myapp", "-quiet", "-display", "stego:0", "file1.data", 0 }
The Et_Init() function will see the -display stego:0 part and act upon it accordingly. It will then remove those fields from the argument list, so that after Et_Init() returns, the values are these:
    argc = 3
    argv = { "myapp", "-quiet", "file1.data", 0 }
In this way, the initialization code that follows Et_Init() never sees the ET-specific command line arguments.

After the Et_Init() procedure comes the initialization code. Normally, you begin the initialization by creating and registering all the new Tcl/Tk commands you will need. The details are described in section 6. Basically it involves replacing the comment in the template with one or more ET_INSTALL_COMMANDS statements. Once you've created the new Tcl/Tk commands, you may need to construct internal C data structures, or create linkages between C variables and Tcl variables using Tcl's Tcl_LinkVar() function. Command-line options that haven't been removed by Et_Init() are often processed here, as well. Finally, you will probably want to create the initial windows for the application. The ET() function (see section 7) and ET_INCLUDE() procedure (see section 8) are both good for this.

Of course, this is only a suggested outline of how to initialize your application. You should feel free to do something different if your program requires it. The only ground rule is that the initialization code shouldn't try to interact with the user. Instead, use callback routines to respond to user inputs.

The last line of main() is a call to the Et_MainLoop() procedure. Et_MainLoop() implements the event loop. It will not return until the program is ready to exit.

[Next Page][Table of Contents]