Login:    Password:      Don't have a username? Register!

Home > Projects > Envoy

Callbacks and Other Phenomena

If you're comfortable with the basics of convey, the only other topics we need to cover are layouts, and callbacks for dealing with user input:

#include <convey.h>

char* message = 0;

void list_select( cvywidget list, cvydata userdata, cvydata calldata )
{
	if( message )
		free( message );
	message = strdup( (const char*)calldata );
}

void button_select( cvywidget button, cvydata userdata, cvydata calldata )
{
	if( !message )
		return;

	convey_label_set( (cvywidget)userdata, message );
}

int main( int argc, char** argv )
{
  cvyappwindow app =
    convey_app_new(
      &argc,
      argv,
      "Goodbye World",
      200,
      300
    );

  cvylayout main = convey_layout_new(
    convey_app_get_layout( app ),
    1
  );

  cvywidget label = convey_label_new(
    main,
    "This Space for Hire"
  );

  cvylayout choice = convey_layout_new( main, 0 );

  cvywidget list = convey_listbox_new(
    choice,
    list_select,
    0,
    "Messages",
    3
  );
  convey_listbox_add( list, "BOOOOOO!" );
  convey_listbox_add( list, "Are we having fun yet?" );
  convey_listbox_add( list, "What is they bidding, master?" );
  convey_listbox_add( list, "You suck." );
  convey_listbox_add( list, "Quantam sufficit" );

  convey_button_new( choice, button_select, label, "Set" );

  convey_app_display( app );
  return 0;
}
 

After the standard initialisation we create a vertical layout. Note that it still needs a parent layout, so we use the default one that came with the application window.

The label creation is the same as the simple example, but using the manually created layout. We also keep the pointer to the widget this time, since we want to use it later.

Now we create a second layout as a child of the first, and this one has its vertical flag set to false, ie. is horizontal.

The final two widgets are created within the horizontal layout - a listbox and a button. The listbox will hold the message choices, and the button causes the selected choice to be displayed. The observant amongst you may wonder if the button is necessary. Well, no, not really, but doing it this way shows off a bit more of the functionality. Consider removing the button homework, if you like :P

The listbox entries are added with a seperate function, convey_listbox_add, which takes a widget as the first argument and the text to add as the second. Note that the list can contain more items than its size - the size only defines how many are visible at one time.

You'll notice that the callback for the listbox ignores its userdata parameter (since that will be NULL anyway), but reads the text of the selected item from the calldata parameter. The button callback, however, is the opposite. There is no calldata for a button, but we needed the label widget to be able to change its text.

Once all that's setup, then the main application loop is triggered as before. Compile and run the code, and marvel at the power set before you. Or something.