Otk Tutorial 1 - Hello-World

This is the first of several tutorials showing how to use Otk to make graphical user interfaces (GUI's) for C applications. The classic Helloworld program is the simplest example,

Let's begin by dissecting the simple hello-world example.

1. #include "otk_lib/otk_lib.c"
2. 
3. main( int argc, char **argv )
4. {
5.   OtkInitWindow( 350, 140, argc, argv );
6.   OtkMakeButton( OtkOuterWindow, 10.0, 10.0, 80.0, 80.0, "Hello World !", 0, 0 );
7.   OtkMainLoop();
8. }

The first line includes the Otk library.

Line 5 opens the outer window, to be of size 350x140 pixels. This is only time that coordinates or sizes will be specified in pixels. When selecting the initial window size, consider that most computer screens span from 480x600 (lowest-resolution) to about 1020x1280 or more. But the typical screen is often at least 780x1020 nowadays. And remember that the user can always resize the window, if it is too small or too large. By default, a widget variable is defined that becomes your handle for the outer window, and it is called, OtkOuterWindow.

Line 6 makes a button which will be contained by the OtkOuterWindow. Therefore, OtkOuterWindow is the parent- or container- window for the button. The button's upper-left corner will be placed 10% from the left and 10% from the top of it's parent window. It will occupy a length which is 80% of the parent or outer window. And it will occupy a height which is 80% of the height of the parent or outer window. In other words, the lower-right corner of the button will be at (90.0%, 90.0%) in the parent window. This is an rather large button! Most applications would not specify such a large button, but this is the helloworld example.

The text words, "Hello World!", that should appear on the button come next. Lastly the callback routine with it's optional parameter could be passed as the last two parameters of the OtkMakeButton call. However, 0 (null) is used for this simple example, because we do not wish to call any other routines.

Finally, line 7 calls the OtkMainLoop() function which actually makes the window visible, and would handle user-interactions.

All Otk applications will have the OtkInitWindow and OtkMainLoop calls. And they will tend to have more buttons or other gadgets.

To compile this application on Linux:
      cc -I/usr/X11R6/include -L/usr/X11R6/lib hello.c -lGLU -lGL -lXmu -lXext -lX11 -lm -o hello.exe

When you run it, you should see something like this:









Return to Tutorials page.

Back to main page

SourceForge.net Logo