ButtonBar

ButtonBarApplyKey

Handle mouse input, shortcuts and resize events for the button bar

Syntax

Signature
xBool       ButtonBarApplyKey( ButtonBar *buttonBar, int key, int *id );

Arguments

buttonBar the button bar.

key a key code or event returned by Inkey().

id receives the activated item's identifier, or 0 when no action is dispatched. Use NULL to discard the identifier.

Return value

T for a matching shortcut or a mouse move, left press, double-click or left release over a bar slot. F for resize, other unhandled events or a NULL object.

Description

Call from the application's input loop. Use an `Inkey()` mask that includes `INKEY_MOVE`, `INKEY_LDOWN` and `INKEY_LUP` for mouse handling. Mouse movement updates the highlight. A left press and release on the same selectable button runs its action. A matching shortcut runs the first selectable button with that code.

Mouse events over disabled items and separators are handled but run no action. Moving off the bar clears its highlight and returns `F`. `K_RESIZE` clears the pending press, updates the highlight, redraws the bar for the new size and returns `F` so the application can handle the resize too.

The function initializes a supplied `id` to `0` on entry. It captures the item's identifier before calling the callback, then writes that identifier to `id`. A callback may change the bar. If it keeps the bar alive, the application can redraw explicitly after the action.

Example

buttonbarapplykey.c
#include "xdev-wtui.h"
#include "xdev-wtui-classes.h"
#include "xdev-wtui-inkey.h"

static void ShowButton( MenuItem *item )
{
   SetPos( 0, 0 );
   DispOut( MenuItemGetCaption( item ), NULL );
}

int main( void )
{
   ButtonBar *bar = ButtonBarNew();
   int        id = 0;

   ButtonBarAddItem( bar, MenuItemNewB( "Help", ShowButton, K_F1, NULL, 1 ) );
   ButtonBarAddItem( bar, MenuItemNewB( "Exit", ShowButton, K_F10, NULL, 10 ) );
   ButtonBarDisplay( bar );

   while( id != 10 )
   {
      int key = Inkey( 0, INKEY_ALL );

      if( !ButtonBarApplyKey( bar, key, &id ) && key == K_ESC )
         break;
   }

   ButtonBarFree( bar );
   return 0;
}

See also