# Edit Tutorial

We will discuss the following program for this tutorial, it can be found in EditSample.cpp:

>     00001:   #include <WKGL.h>
>     00002:   using namespace wkgl;
>     00003:
>     00004:   //Callback prototypes
>     00005:   BOOL OnButton( Component*, UINT, WPARAM, LPARAM );
>     00006:
>     00007:   //Declare global component pointers.
>     00008:   MultilineEdit *edit;
>     00009:
>     00010:   WPARAM main()
>     00011:   {
>     00012:    Window win( "Edit Test" );
>     00013:    Button button( "Display Text", 0, 120 );
>     00014:
>     00015:    //Create the edit controls.
>     00016:    edit = new MultilineEdit( 10, 5, 0, 0 );
>     00017:
>     00018:    //Add the controls to the window.
>     00019:    win.add( &button ); //Add a button.
>     00020:    win.add( edit );    //Add an edit.
>     00021:
>     00022:    //Add the callback for the button
>     00023:    button.addCallBack( OnButton );
>     00024:
>     00025:    //Show the window
>     00026:    win.show();
>     00027:
>     00028:    WPARAM ret = win.beginMessageLoop();
>     00029:
>     00030:    delete edit;
>     00031:
>     00032:    return ret;
>     00033:   }
>     00034:
>     00035:   //Callback for button
>     00036:   BOOL OnButton( Component *com, UINT message, WPARAM wParam, LPARAM lParam )
>     00037:   {
>     00038:     static char buf[50] = {0}; //Buffer for message text.
>     00039:
>     00040:     if ( message == BN_CLICKED )
>     00041:     {
>     00042:       //Get the text from the edit box
>     00043:       edit->getText( buf, 50 );
>     00044:
>     00045:       //Set the button's text.
>     00046:       showMessageBox( com, buf, "The Edit Says" );
>     00047:
>     00048:       return TRUE;
>     00049:     }
>     00050:     return FALSE;
>     00051:   }

First let me point out that Edit and MultilineEdit function the exact same way with one exception, the former is good only for editing a single line. We declare edit as a global pointer because otherwise we would not be able to minipulate it in OnButton(). This is a pretty simple program, I will explain showMessageBox() at a later time. The getText() and setText() methods are the most commonly used methods on an edit component.

The most important aspect of this program is to notice the different way we are handling what is returned by beginMessageLoop(). We do it this way so that we can delete any global pointers. The return still needs to be caught though so we use the ret variable to do this and simply return it.

At this time I would like to discuss when components are created. The window is merely a hunk of memory until you attempt to show it. When that happens, the window is created and then each of its controls are created. This means that for optimization pruposes you should add the controls before the window is shown for the first time. If you do not then you must call the create method of each subsequent control to make it visible.