OpenCores
URL https://opencores.org/ocsvn/layer2/layer2/trunk

Subversion Repositories layer2

[/] [layer2/] [trunk/] [sw/] [lib/] [ui.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 idiolatrie
/******************************************************************************
2
 * User Interface                                                             *
3
 ******************************************************************************
4
 * Copyright (C)2011  Mathias Hörtnagl <mathias.hoertnagl@gmail.com>          *
5
 *                                                                            *
6
 * This program is free software: you can redistribute it and/or modify       *
7
 * it under the terms of the GNU General Public License as published by       *
8
 * the Free Software Foundation, either version 3 of the License, or          *
9
 * (at your option) any later version.                                        *
10
 *                                                                            *
11
 * This program is distributed in the hope that it will be useful,            *
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
14
 * GNU General Public License for more details.                               *
15
 *                                                                            *
16
 * You should have received a copy of the GNU General Public License          *
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
18
 ******************************************************************************/
19
#include "ui.h"
20
 
21
/* A container for all Error Windows.
22
   Used by 'void drawErrorWindow(Message *errmsg)'. */
23
static Window wError = {
24
   {25, 12, 50, 5},
25
   {RED, BLACK},
26
   "FATAL ERROR",
27
   1,
28
   NULL
29
};
30
 
31
/******************************************************************************
32
 * Pattern Functions                                                          *
33
 ******************************************************************************/
34
 
35
/* Print a single character multiple times. */
36
void putnc(const uchar c, uchar n) {
37
   for(uchar i=0; i++ < n; putc(c));
38
}
39
 
40
/* Print character l once. Print character c  multiple times. Print r once. */
41
void putlrnc(const uchar l, const uchar c, const uchar r, uchar n) {
42
   putc(l); putnc(c, n-2); putc(r);
43
}
44
 
45
/******************************************************************************
46
 * Windows                                                                    *
47
 ******************************************************************************/
48
/* Draws a Window. */
49
void drawWindow(Window *win) {
50
 
51
   uchar rb = win->box.x + win->box.w - 1;     // Right border position.
52
   uchar lb = win->box.y + win->box.h - 1;     // Lower border position.
53
 
54
   setcolor(win->col.fg, win->col.bg);
55
 
56
   // Print upper horizontal border.
57
   gotoxy(win->box.x, win->box.y);
58
   putlrnc(
59
      DOUBLE_UPPER_LEFT,
60
      DOUBLE_HORIZONTAL,
61
      DOUBLE_UPPER_RIGHT,
62
      win->box.w
63
   );
64
 
65
   // Draw vertical left and right border.
66
   gotoxy(win->box.x, win->box.y + 1);
67
   putlrnc(DOUBLE_VERTICAL, ' ', DOUBLE_VERTICAL, win->box.w);
68
 
69
   // Draw the title.
70
   gotoxy(win->box.x + 2, win->box.y + 1); puts(win->title);
71
 
72
   // Print divider between header and contents plane.
73
   gotoxy(win->box.x, win->box.y + 2);
74
   putlrnc(
75
      DOUBLE_DOUBLE_T_LEFT,
76
      DOUBLE_HORIZONTAL,
77
      DOUBLE_DOUBLE_T_RIGHT,
78
      win->box.w
79
   );
80
 
81
   // Print vertical left and right border.
82
   for(uchar i = win->box.y + 3; i < lb; i++) {
83
      gotoxy(win->box.x, i);
84
      putc(DOUBLE_VERTICAL);
85
      gotoxy(rb, i);
86
      putc(DOUBLE_VERTICAL);
87
   }
88
 
89
   // Print lower horizontal border.
90
   gotoxy(win->box.x, lb);
91
   putlrnc(
92
      DOUBLE_LOWER_LEFT,
93
      DOUBLE_HORIZONTAL,
94
      DOUBLE_LOWER_RIGHT,
95
      win->box.w
96
   );
97
 
98
   // Draw contents components.
99
   drawComponents(win);
100
 
101
   setcolor(WHITE, BLACK);
102
}
103
 
104
/* Draw all the contents of the Window 'win'. */
105
void drawComponents(Window *win) {
106
 
107
   WindowItem *item;
108
 
109
   for(uchar i = 0; i < win->itemc; i++) {
110
 
111
      item = (WindowItem *) win->itemv[i];
112
 
113
      switch(item->id) {
114
         case ID_MESSAGE:
115
            drawMessage(win, (Message *) item);
116
            break;
117
         case ID_MENU:
118
            drawMenu(win, (Menu *) item);
119
            break;
120
         case ID_PROGRESS_BAR:
121
            drawProgressBar(win, (ProgressBar *) item);
122
            break;
123
         default:
124
            break;
125
      }
126
   }
127
}
128
 
129
/* Prints an Error Message Window. The window is always 'wError' defined in this
130
   file. The function set the Boxes width and x-axis according to the message
131
   length. The message should not exceed 96 characters. */
132
void drawErrorWindow(Message *errmsg) {
133
 
134
   uchar w = strlen(errmsg->msg) + 4;
135
 
136
   // Clean screen. we only want to display the errror message.
137
   cls();
138
 
139
   // Adjust the Error Box size according to message size.
140
   wError.box.w = w;
141
   wError.box.x = (100 - w) / 2;
142
   wError.itemv[0] = errmsg;
143
 
144
   drawWindow(&wError);
145
}
146
 
147
 
148
/******************************************************************************
149
 * Message                                                                    *
150
 ******************************************************************************/
151
/* Draws a simple text message.
152
   Or override an existing Message, by setting the same relativ position of the
153
   new Message 'msg' like the old message. */
154
void drawMessage(Window *win, Message *msg) {
155
 
156
   uint len = strlen(msg->msg);
157
   uint width = win->box.w - len - msg->pos.x;
158
 
159
   gotoxy(win->box.x + msg->pos.x + 1, win->box.y + msg->pos.y + 3);
160
   putc(' '); puts(msg->msg); putnc(' ', width - 3);
161
}
162
 
163
 
164
/******************************************************************************
165
 * Progress Bar                                                               *
166
 ******************************************************************************/
167
/* Draws a Progress Bar element.
168
   The value 'ProgressBar.val' must be between 0 and 63. The Window should be
169
   therefor 68 wide. */
170
void drawProgressBar(Window *win, ProgressBar *bar) {
171
 
172
   //uint width = win->box.w - bar->pos.x - 3;
173
   //uint factor = div( mul(bar->val, width), bar->max );
174
 
175
   gotoxy(win->box.x + bar->pos.x + 2, win->box.y + bar->pos.y + 3);
176
 
177
   // for(uint i=0; i < width; i++) {
178
      // if(i <= factor)
179
         // putc(SQUARE_BOX);
180
      // else
181
         // putc(' ');
182
   // }
183
 
184
   putnc(SQUARE_BOX, bar->val);
185
   putnc(' ', 64 - bar->val);
186
}
187
 
188
 
189
/******************************************************************************
190
 * Menus                                                                      *
191
 ******************************************************************************/
192
/* Redraw the Menu by selecting the next element of the MenuItems and set
193
   'menu.index' acordingly.
194
   If the current selected MenuItem is 'menu.itemv[menu.itemc-1]' the next
195
   MenuItem will be 'menu.itemv[0]'. */
196
void menuKeyDown(Window *win, Menu *menu) {
197
 
198
   if(menu->index == menu->itemc - 1)
199
      menu->index = 0;
200
   else
201
      menu->index++;
202
 
203
   drawMenu(win, menu);
204
}
205
 
206
/* Redraw the Menu by selecting the previous element of the MenuItems and set
207
   'menu.index' acordingly.
208
   If the current selected MenuItem is 'menu.itemv[0]' the next MenuItem will be
209
   'menu.itemv[menu.itemc-1]'. */
210
void menuKeyUp(Window *win, Menu *menu) {
211
 
212
   if(menu->index == 0)
213
      menu->index = menu->itemc - 1;
214
   else
215
      menu->index--;
216
 
217
   drawMenu(win, menu);
218
}
219
 
220
/* Draws a vertical menu. */
221
void drawMenu(Window *win, Menu *menu) {
222
 
223
   MenuItem *item;
224
   cursor pos;
225
   uint len;
226
   uint width;
227
 
228
   pos.x = win->box.x + menu->pos.x + 1;
229
   pos.y = win->box.y + menu->pos.y + 3;
230
   width = win->box.w - menu->pos.x;
231
 
232
   for(uchar idx = 0; idx < menu->itemc; idx++) {
233
 
234
      gotoxy(pos.x, pos.y + idx);
235
 
236
      item = menu->itemv[idx];
237
      len = strlen(item->name);
238
 
239
      if(menu->index == idx) {
240
 
241
         // Invert color scheme for selected items.
242
         setcolor(win->col.bg, win->col.fg);
243
 
244
         putc(VERTICAL_BLOCK_LEFT);
245
         puts(item->name); putnc(' ', width - len - 4);
246
         putc(VERTICAL_BLOCK_RIGHT);
247
 
248
         // Reset color options to default.
249
         setcolor(win->col.fg, win->col.bg);
250
      }
251
      else {
252
         putc(' '); puts(item->name); putnc(' ', width - len - 3);
253
      }
254
   }
255
}
256
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.