slk.c
Go to the documentation of this file.00001 #include <curses.h>
00002 #include <stddef.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005 #include <assert.h>
00006 #include "mucurses.h"
00007 #include "cursor.h"
00008
00009
00010
00011
00012
00013
00014 #define MIN_SPACE_SIZE 2
00015
00016 #define SLK_MAX_LABEL_LEN 8
00017
00018 #define SLK_MAX_NUM_LABELS 12
00019
00020 #define SLK_MAX_NUM_SPACES 2
00021
00022 struct _softlabel {
00023
00024 char label[SLK_MAX_LABEL_LEN];
00025
00026
00027
00028
00029
00030 unsigned int fmt;
00031 };
00032
00033 struct _softlabelkeys {
00034 struct _softlabel fkeys[SLK_MAX_NUM_LABELS];
00035 attr_t attrs;
00036
00037
00038
00039
00040
00041
00042 unsigned int fmt;
00043 unsigned int max_label_len;
00044 unsigned int maj_space_len;
00045 unsigned int num_labels;
00046 unsigned int num_spaces;
00047 unsigned int spaces[SLK_MAX_NUM_SPACES];
00048 struct cursor_pos saved_cursor;
00049 attr_t saved_attrs;
00050 short saved_pair;
00051 };
00052
00053 static struct _softlabelkeys *slks;
00054
00055
00056
00057
00058
00059
00060
00061
00062 static void _enter_slk ( void ) {
00063 _store_curs_pos ( stdscr, &slks->saved_cursor );
00064 wattr_get ( stdscr, &slks->saved_attrs, &slks->saved_pair, NULL );
00065 LINES++;
00066 wmove ( stdscr, LINES, 0 );
00067 wattrset ( stdscr, slks->attrs );
00068 }
00069
00070 static void _leave_slk ( void ) {
00071 LINES--;
00072 wattr_set ( stdscr, slks->saved_attrs, slks->saved_pair, NULL );
00073 _restore_curs_pos ( stdscr, &slks->saved_cursor );
00074 }
00075
00076 static void _print_label ( struct _softlabel sl ) {
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
00084 if ( sl.label == NULL ) {
00085 memset( str, space_ch, (size_t)(slks->max_label_len) );
00086 } else {
00087
00088
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
00102 memset(str+strlen(str), space_ch,
00103 (size_t)(slks->max_label_len - strlen(str)) );
00104 }
00105
00106
00107 _wputstr ( stdscr, str, NOWRAP, slks->max_label_len );
00108 }
00109
00110
00111
00112
00113
00114
00115 attr_t slk_attr ( void ) {
00116 return ( slks == NULL ? 0 : slks->attrs );
00117 }
00118
00119
00120
00121
00122
00123
00124
00125 int slk_attroff ( const chtype attrs ) {
00126 if ( slks == NULL )
00127 return ERR;
00128 slks->attrs &= ~( attrs & A_ATTRIBUTES );
00129 return OK;
00130 }
00131
00132
00133
00134
00135
00136
00137
00138 int slk_attron ( const chtype attrs ) {
00139 if ( slks == NULL )
00140 return ERR;
00141 slks->attrs |= ( attrs & A_ATTRIBUTES );
00142 return OK;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151 int slk_attrset ( const chtype attrs ) {
00152 if ( slks == NULL )
00153 return ERR;
00154 slks->attrs = ( attrs & A_ATTRIBUTES );
00155 return OK;
00156 }
00157
00158
00159
00160
00161
00162
00163
00164
00165 int slk_attr_off ( const attr_t attrs, void *opts __unused ) {
00166 return slk_attroff( attrs );
00167 }
00168
00169
00170
00171
00172
00173
00174
00175
00176 int slk_attr_on ( attr_t attrs, void *opts __unused ) {
00177 return slk_attron( attrs );
00178 }
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 int slk_attr_set ( const attr_t attrs, short colour_pair_number,
00189 void *opts __unused ) {
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 }
00200
00201
00202
00203
00204
00205
00206 int slk_clear ( void ) {
00207 if ( slks == NULL )
00208 return ERR;
00209
00210 _enter_slk();
00211 wclrtoeol ( stdscr );
00212 _leave_slk();
00213
00214 return OK;
00215 }
00216
00217
00218
00219
00220 int slk_colour ( short colour_pair_number ) {
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 }
00231
00232
00233
00234
00235
00236
00237
00238 int slk_init ( int fmt ) {
00239 unsigned short nmaj, nmin, nblocks, available_width;
00240
00241 if ( (unsigned)fmt > 3 ) {
00242 return ERR;
00243 }
00244
00245
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
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
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
00282 LINES -= 1;
00283
00284 return OK;
00285 }
00286
00287
00288
00289
00290
00291
00292
00293 char* slk_label ( int labnum ) {
00294 if ( slks == NULL )
00295 return NULL;
00296
00297 return slks->fkeys[labnum].label;
00298 }
00299
00300
00301
00302
00303
00304
00305 int slk_restore ( void ) {
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 }
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350 int slk_set ( int labnum, const char *label, int fmt ) {
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 }