File: select.c

package info (click to toggle)
fuse-emulator 1.0.0.1a%2Bdfsg1-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 9,568 kB
  • sloc: ansic: 67,895; sh: 10,265; perl: 3,386; makefile: 787; yacc: 227; lex: 139
file content (212 lines) | stat: -rw-r--r-- 5,517 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* select.c: generic selection widget
   Copyright (c) 2001-2004 Philip Kendall, Witold Filipczyk

   $Id: select.c 4103 2009-11-21 10:16:36Z fredm $

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program; if not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

   Author contact information:

   E-mail: philip-fuse@shadowmagic.org.uk

*/

#include <config.h>

#include <string.h>

#include "widget_internals.h"

/* Data for drawing the cursor */
static size_t highlight_line;

const char *title;
const char **options;
static int finish_all;
static size_t count;
static int *result;

static void print_item (int left_edge, int width, int index, int colour)
{
  char key[] = "\x0A ";
  key[1] = 'A' + index;
  left_edge = ( left_edge + 1 ) * 8 + 1;
  left_edge = widget_printstring( left_edge, index * 8 + 24, colour, key );
  left_edge = widget_printstring( left_edge + 1, index * 8 + 24, colour, ": " );
  widget_printstring( left_edge + 1, index * 8 + 24, colour, options[index] );
}

const int select_vert_external_margin = 8;

static int
widget_calculate_select_width( const char* title )
{
  int i;
  int max_width = widget_stringwidth( title )+5*8;
  /* Leave room for the option labels we'll be adding */
  int label_width = widget_stringwidth( "A: " );

  for( i = 0; i < count; i++ ) {
    int total_width = label_width + widget_stringwidth( options[i] ) + 3 * 8;
    if( total_width > max_width )
      max_width = total_width;
  }

  return ( max_width + select_vert_external_margin * 2 ) / 8;
}

int
widget_select_draw( void *data )
{
  int i;
  size_t width;
  int menu_left_edge_x;

  if( data ) {

    widget_select_t *ptr = data;

    title = ptr->title;
    options = ptr->options;
    count = ptr->count;
    result = &( ptr->result );
    finish_all = ptr->finish_all;

    highlight_line = ptr->current;

  }

  width = widget_calculate_select_width( title );
  menu_left_edge_x = DISPLAY_WIDTH_COLS/2-width/2;

  /* Blank the main display area */
  widget_dialog_with_border( menu_left_edge_x, 2, width, count + 2 );

  widget_printstring( menu_left_edge_x*8+2, 16, WIDGET_COLOUR_TITLE, title );

  for( i = 0; i < count; i++ ) {
    if( i == highlight_line ) {
      widget_rectangle( menu_left_edge_x*8+1, i*8+24, width*8-2, 1*8, WIDGET_COLOUR_HIGHLIGHT );
    }
    print_item( menu_left_edge_x, width, i, WIDGET_COLOUR_FOREGROUND );
  }

  widget_display_lines( 2, count + 2 );

  return 0;
}

void
widget_select_keyhandler( input_key key )
{
  int new_highlight_line = 0;
  int cursor_pressed = 0;
  size_t width = widget_calculate_select_width( title );
  int menu_left_edge_x = DISPLAY_WIDTH_COLS/2-width/2;

  switch( key ) {

#if 0
  case INPUT_KEY_Resize:	/* Fake keypress used on window resize */
    widget_select_draw( NULL );
    break;
#endif

  case INPUT_KEY_Escape:
  case INPUT_JOYSTICK_FIRE_2:
    widget_end_widget( WIDGET_FINISHED_CANCEL );
    return;

  case INPUT_KEY_Return:
  case INPUT_KEY_KP_Enter:
  case INPUT_JOYSTICK_FIRE_1:
    widget_end_widget( WIDGET_FINISHED_OK );
    return;

  case INPUT_KEY_Up:
  case INPUT_KEY_7:
  case INPUT_JOYSTICK_UP:
    if ( highlight_line ) {
      new_highlight_line = highlight_line - 1;
      cursor_pressed = 1;
    }
    break;

  case INPUT_KEY_Down:
  case INPUT_KEY_6:
  case INPUT_JOYSTICK_DOWN:
    if ( highlight_line + 1 < (ptrdiff_t)count ) {
      new_highlight_line = highlight_line + 1;
      cursor_pressed = 1;
    }
    break;

  case INPUT_KEY_Home:
    if ( highlight_line ) {
      new_highlight_line = 0;
      cursor_pressed = 1;
    }
    break;

  case INPUT_KEY_End:
    if ( highlight_line + 2 < (ptrdiff_t)count ) {
      new_highlight_line = (ptrdiff_t)count - 1;
      cursor_pressed = 1;
    }
    break;

  default:	/* Keep gcc happy */
    break;

  }

  if( cursor_pressed                                  ||
      ( key >= INPUT_KEY_a && key <= INPUT_KEY_z &&
	key - INPUT_KEY_a < (ptrdiff_t)count        )
    ) {
    
    /* Remove the old highlight */
    widget_rectangle( menu_left_edge_x * 8 + 1, highlight_line * 8 + 24,
                      width * 8 - 2, 1 * 8, WIDGET_COLOUR_BACKGROUND );
    print_item( menu_left_edge_x, width, highlight_line,
                WIDGET_COLOUR_FOREGROUND );

    /*  draw the new one */
    if( cursor_pressed ) {
      highlight_line = new_highlight_line;
    } else {
      highlight_line = key - INPUT_KEY_a;
    }
    
    widget_rectangle( menu_left_edge_x * 8 + 1, highlight_line * 8 + 24,
                      width * 8 - 2, 1 * 8, WIDGET_COLOUR_HIGHLIGHT );
    print_item( menu_left_edge_x, width, highlight_line, WIDGET_COLOUR_FOREGROUND );

    widget_display_lines( 2, count + 2 );
  }
}

int widget_select_finish( widget_finish_state finished )
{
  if( finished == WIDGET_FINISHED_OK ) {
    *result = highlight_line;
    if( finish_all )
      widget_end_all( WIDGET_FINISHED_OK );
  } else {
    *result = -1;
  }

  return 0;
}