slk.c File Reference

Soft label key functions. More...

#include <curses.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "mucurses.h"
#include "cursor.h"

Go to the source code of this file.

Data Structures

struct  _softlabel
struct  _softlabelkeys

Defines

#define MIN_SPACE_SIZE   2
#define SLK_MAX_LABEL_LEN   8
#define SLK_MAX_NUM_LABELS   12
#define SLK_MAX_NUM_SPACES   2

Functions

static void _enter_slk (void)
static void _leave_slk (void)
static void _print_label (struct _softlabel sl)
attr_t slk_attr (void)
 Return the attribute used for the soft function keys.
int slk_attroff (const chtype attrs)
 Turn off soft function key attributes.
int slk_attron (const chtype attrs)
 Turn on soft function key attributes.
int slk_attrset (const chtype attrs)
 Set soft function key attributes.
int slk_attr_off (const attr_t attrs, void *opts __unused)
 Turn off soft function key attributes.
int slk_attr_on (attr_t attrs, void *opts __unused)
 Turn on soft function key attributes.
int slk_attr_set (const attr_t attrs, short colour_pair_number, void *opts __unused)
 Set soft function key attributes.
int slk_clear (void)
 Clear the soft function key labels from the screen.
int slk_colour (short colour_pair_number)
 Set soft label colour pair.
int slk_init (int fmt)
 Initialise the soft function keys.
char * slk_label (int labnum)
 Return the label for the specified soft key.
int slk_restore (void)
 Restore soft function key labels to the screen.
int slk_set (int labnum, const char *label, int fmt)
 Configure specified soft key.

Variables

static struct _softlabelkeysslks


Detailed Description

Soft label key functions.

Definition in file slk.c.


Define Documentation

#define MIN_SPACE_SIZE   2

Definition at line 14 of file slk.c.

Referenced by slk_init().

#define SLK_MAX_LABEL_LEN   8

Definition at line 16 of file slk.c.

Referenced by _print_label().

#define SLK_MAX_NUM_LABELS   12

Definition at line 18 of file slk.c.

#define SLK_MAX_NUM_SPACES   2

Definition at line 20 of file slk.c.


Function Documentation

static void _enter_slk ( void   )  [static]

static void _leave_slk ( void   )  [static]

static void _print_label ( struct _softlabel  sl  )  [static]

Definition at line 76 of file slk.c.

References _wputstr(), assert, _softlabel::fmt, _softlabel::label, _softlabelkeys::max_label_len, memset(), NOWRAP, NULL, SLK_MAX_LABEL_LEN, stdscr, strcat(), and strlen().

Referenced by slk_restore().

00076                                                   {
00077         int space_ch;
00078         char str[SLK_MAX_LABEL_LEN + 1];
00079 
00080         assert ( slks->max_label_len <= SLK_MAX_LABEL_LEN );
00081         space_ch = ' ';
00082 
00083         // protect against gaps in the soft label keys array
00084         if ( sl.label == NULL ) {
00085                 memset( str, space_ch, (size_t)(slks->max_label_len) );
00086         } else {
00087                 /* we need to pad the label with varying amounts of leading
00088                    pad depending on the format of the label */
00089                 if ( sl.fmt == 1 ) {
00090                         memset( str, space_ch, 
00091                                 (size_t)(slks->max_label_len 
00092                                          - strlen(sl.label)) / 2 );
00093                 }
00094                 if ( sl.fmt == 2 ) {
00095                         memset( str, space_ch,
00096                                 (size_t)(slks->max_label_len 
00097                                          - strlen(sl.label)) );
00098                 }
00099                 strcat(str,sl.label);
00100                 
00101                 // post-padding
00102                 memset(str+strlen(str), space_ch,
00103                        (size_t)(slks->max_label_len - strlen(str)) );
00104         }
00105 
00106         // print the formatted label
00107         _wputstr ( stdscr, str, NOWRAP, slks->max_label_len );
00108 }

attr_t slk_attr ( void   ) 

Return the attribute used for the soft function keys.

Return values:
attrs the current attributes of the soft function keys

Definition at line 115 of file slk.c.

References _softlabelkeys::attrs, and NULL.

00115                          {
00116         return ( slks == NULL ? 0 : slks->attrs );
00117 }

int slk_attroff ( const chtype  attrs  ) 

Turn off soft function key attributes.

Parameters:
attrs attribute bit mask
Return values:
rc return status code

Definition at line 125 of file slk.c.

References A_ATTRIBUTES, _softlabelkeys::attrs, ERR, NULL, and OK.

Referenced by slk_attr_off().

00125                                        {
00126         if ( slks == NULL ) 
00127                 return ERR;
00128         slks->attrs &= ~( attrs & A_ATTRIBUTES );
00129         return OK;
00130 }

int slk_attron ( const chtype  attrs  ) 

Turn on soft function key attributes.

Parameters:
attrs attribute bit mask
Return values:
rc return status code

Definition at line 138 of file slk.c.

References A_ATTRIBUTES, _softlabelkeys::attrs, ERR, NULL, and OK.

Referenced by slk_attr_on().

00138                                       {
00139         if ( slks == NULL )
00140                 return ERR;
00141         slks->attrs |= ( attrs & A_ATTRIBUTES );
00142         return OK;
00143 }

int slk_attrset ( const chtype  attrs  ) 

Set soft function key attributes.

Parameters:
attrs attribute bit mask
Return values:
rc return status code

Definition at line 151 of file slk.c.

References A_ATTRIBUTES, _softlabelkeys::attrs, ERR, NULL, and OK.

00151                                        {
00152         if ( slks == NULL ) 
00153                 return ERR;
00154         slks->attrs = ( attrs & A_ATTRIBUTES );
00155         return OK;
00156 }

int slk_attr_off ( const attr_t  attrs,
void *opts  __unused 
)

Turn off soft function key attributes.

Parameters:
attrs attribute bit mask
*opts undefined (for future implementation)
Return values:
rc return status code

Definition at line 165 of file slk.c.

References slk_attroff().

00165                                                              {
00166         return slk_attroff( attrs );
00167 }

int slk_attr_on ( attr_t  attrs,
void *opts  __unused 
)

Turn on soft function key attributes.

Parameters:
attrs attribute bit mask
*opts undefined (for future implementation)
Return values:
rc return status code

Definition at line 176 of file slk.c.

References slk_attron().

00176                                                       {
00177         return slk_attron( attrs );
00178 }

int slk_attr_set ( const attr_t  attrs,
short  colour_pair_number,
void *opts  __unused 
)

Set soft function key attributes.

Parameters:
attrs attribute bit mask
colour_pair_number colour pair integer
*opts undefined (for future implementation)
Return values:
rc return status code

Definition at line 188 of file slk.c.

References A_ATTRIBUTES, _softlabelkeys::attrs, COLORS, CPAIR_SHIFT, ERR, NULL, and OK.

00189                                          {
00190         if ( slks == NULL ) 
00191                 return ERR;
00192 
00193         if ( ( unsigned short )colour_pair_number > COLORS )
00194                 return ERR;
00195 
00196         slks->attrs = ( (unsigned short)colour_pair_number << CPAIR_SHIFT ) |
00197                 ( attrs & A_ATTRIBUTES );
00198         return OK;
00199 }

int slk_clear ( void   ) 

Clear the soft function key labels from the screen.

Return values:
rc return status code

Definition at line 206 of file slk.c.

References _enter_slk(), _leave_slk(), ERR, NULL, OK, stdscr, and wclrtoeol().

Referenced by slk_refresh().

00206                        {
00207         if ( slks == NULL )
00208                 return ERR;
00209 
00210         _enter_slk();
00211         wclrtoeol ( stdscr );
00212         _leave_slk();
00213 
00214         return OK;
00215 }

int slk_colour ( short  colour_pair_number  ) 

Set soft label colour pair.

Definition at line 220 of file slk.c.

References A_ATTRIBUTES, _softlabelkeys::attrs, COLORS, CPAIR_SHIFT, ERR, NULL, and OK.

00220                                             {
00221         if ( slks == NULL ) 
00222                 return ERR;
00223         if ( ( unsigned short )colour_pair_number > COLORS )
00224                 return ERR;
00225 
00226         slks->attrs = ( (unsigned short)colour_pair_number << CPAIR_SHIFT )
00227                 | ( slks->attrs & A_ATTRIBUTES );
00228 
00229         return OK;
00230 }

int slk_init ( int  fmt  ) 

Initialise the soft function keys.

Parameters:
fmt format of keys
Return values:
rc return status code

Definition at line 238 of file slk.c.

References A_DEFAULT, _softlabelkeys::attrs, calloc(), COLS, ERR, _softlabelkeys::fmt, LINES, _softlabelkeys::maj_space_len, _softlabelkeys::max_label_len, MIN_SPACE_SIZE, _softlabelkeys::num_labels, _softlabelkeys::num_spaces, OK, and _softlabelkeys::spaces.

00238                          {
00239         unsigned short nmaj, nmin, nblocks, available_width;
00240 
00241         if ( (unsigned)fmt > 3 ) {
00242                 return ERR;
00243         }
00244 
00245         /* There seems to be no API call to free this data structure... */
00246         if ( ! slks )
00247                 slks = calloc(1,sizeof(*slks));
00248         if ( ! slks )
00249                 return ERR;
00250 
00251         slks->attrs = A_DEFAULT;
00252         slks->fmt = fmt;
00253         switch(fmt) {
00254         case 0:
00255                 nblocks = 8; nmaj = 2; nmin = 5;
00256                 slks->spaces[0] = 2; slks->spaces[1] = 4;
00257                 break;
00258         case 1:
00259                 nblocks = 8; nmaj = 1; nmin = 6;
00260                 slks->spaces[0] = 3;
00261                 break;
00262         case 2:
00263                 // same allocations as format 3
00264         case 3:
00265                 nblocks = 12; nmaj = 2; nmin = 9;
00266                 slks->spaces[0] = 3; slks->spaces[1] = 7;
00267                 break;
00268         default:
00269                 nblocks = 0; nmaj = 0; nmin = 0;
00270                 break;
00271         }
00272 
00273         // determine maximum label length and major space size
00274         available_width = COLS - ( ( MIN_SPACE_SIZE * nmaj ) + nmin );
00275         slks->max_label_len = available_width / nblocks;
00276         slks->maj_space_len = MIN_SPACE_SIZE + 
00277                 ( available_width % nblocks ) / nmaj;
00278         slks->num_spaces = nmaj;
00279         slks->num_labels = nblocks;
00280 
00281         // strip a line from the screen
00282         LINES -= 1;
00283 
00284         return OK;
00285 }

char* slk_label ( int  labnum  ) 

Return the label for the specified soft key.

Parameters:
labnum soft key identifier
Return values:
label return label

Definition at line 293 of file slk.c.

References _softlabelkeys::fkeys, _softlabel::label, and NULL.

00293                                {
00294         if ( slks == NULL ) 
00295                 return NULL;
00296 
00297         return slks->fkeys[labnum].label;
00298 }

int slk_restore ( void   ) 

Restore soft function key labels to the screen.

Return values:
rc return status code

Definition at line 305 of file slk.c.

References _enter_slk(), _leave_slk(), _print_label(), _wputch(), _softlabelkeys::attrs, COLS, ERR, _softlabelkeys::fkeys, _softlabelkeys::maj_space_len, _softlabelkeys::max_label_len, NOWRAP, NULL, _softlabelkeys::num_labels, _softlabelkeys::num_spaces, OK, _softlabelkeys::spaces, and stdscr.

Referenced by slk_refresh().

00305                          {
00306         unsigned int i, j, pos_x,
00307                 *next_space, *last_space;
00308         chtype space_ch;
00309 
00310         if ( slks == NULL )
00311                 return ERR;
00312 
00313         pos_x = 0;
00314 
00315         _enter_slk();
00316 
00317         space_ch = (chtype)' ' | slks->attrs;
00318         next_space = &(slks->spaces[0]);
00319         last_space = &(slks->spaces[slks->num_spaces-1]);
00320 
00321         for ( i = 0; i < slks->num_labels ; i++ ) {
00322                 _print_label( slks->fkeys[i] );
00323                 pos_x += slks->max_label_len;
00324 
00325                 if ( i == *next_space ) {
00326                         for ( j = 0; j < slks->maj_space_len; j++, pos_x++ )
00327                                 _wputch ( stdscr, space_ch, NOWRAP );
00328                         if ( next_space < last_space )
00329                                 next_space++;
00330                 } else {
00331                         if ( pos_x < COLS )
00332                                 _wputch ( stdscr, space_ch, NOWRAP );
00333                         pos_x++;
00334                 }
00335         }
00336 
00337         _leave_slk();
00338 
00339         return OK;
00340 }

int slk_set ( int  labnum,
const char *  label,
int  fmt 
)

Configure specified soft key.

Parameters:
labnum soft label position to configure
*label string to use as soft key label
fmt justification format of label
Return values:
rc return status code

Definition at line 350 of file slk.c.

References ERR, _softlabelkeys::fkeys, _softlabel::fmt, _softlabel::label, NULL, _softlabelkeys::num_labels, OK, and strncpy().

00350                                                        {
00351         if ( slks == NULL ) 
00352                 return ERR;
00353         if ( (unsigned short)labnum >= slks->num_labels )
00354                 return ERR;
00355         if ( (unsigned short)fmt >= 3 )
00356                 return ERR;
00357 
00358         strncpy(slks->fkeys[labnum].label, label,
00359                 sizeof(slks->fkeys[labnum].label));
00360         slks->fkeys[labnum].fmt = fmt;
00361 
00362         return OK;
00363 }


Variable Documentation

struct _softlabelkeys* slks [static]

Definition at line 53 of file slk.c.


Generated on Tue Apr 6 20:01:46 2010 for gPXE by  doxygen 1.5.7.1