OSDN Git Service

client 0.6.2 release
[unagi/old-svn-converted.git] / client / tag / 0.6.2 / anago / cui_gauge.c
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <stdarg.h>
5 #ifdef WIN32
6   #include <windows.h>
7 #endif
8 #include "type.h"
9 #include "memory_manage.h"
10 #include "widget.h"
11
12 struct cui_gauge{
13         const wgChar *name;
14         int value, range;
15         int lineback, lineforward;
16 };
17
18 static void range_set(void *obj, int value)
19 {
20         struct cui_gauge *t = obj;
21         t->range = value;
22 }
23
24 #ifdef _UNICODE
25   #define PRINTF wprintf
26   #define VPRINTF vwprintf
27 #else
28   #define PRINTF printf
29   #define VPRINTF vprintf
30 #endif
31 static void console_move(int line)
32 {
33         if(line == 0){
34                 return;
35         }
36 #ifdef WIN32
37         HANDLE c;
38         CONSOLE_SCREEN_BUFFER_INFO info;
39         c = GetStdHandle(STD_OUTPUT_HANDLE);
40         if(GetConsoleScreenBufferInfo(c, &info) == 0){
41                 //command.com, cygwin shell, mingw shell
42                 if(line < 0){
43                         PRINTF(wgT("\x1b[%dA"), -line);
44                 }else if(line == 1){
45                         PRINTF(wgT("\n"));
46                         fflush(stdout);
47                 }else{
48                         PRINTF(wgT("\n"));
49                         fflush(stdout);
50                         PRINTF(wgT("\x1b[%dB"), line - 1);
51 //                      PRINTF(wgT("\x1b[%dB"), line - 1);
52 //                      fflush(stdout);
53                 }
54         }else{
55                 //cmd.exe
56                 info.dwCursorPosition.X = 0;
57                 info.dwCursorPosition.Y += line;
58                 SetConsoleCursorPosition(c, info.dwCursorPosition);
59         }
60 #else
61         if(line < 0){
62                 PRINTF(wgT("\x1b[%dA\x1b[35D"), -line);
63         }else{
64                 PRINTF(wgT("\x1b[%dB\x1b[35D"), line);
65         }
66 #endif
67 //      fflush(stdout);
68 }
69
70 static void draw(const struct cui_gauge *t)
71 {
72         const int barnum = 16;
73         const int unit = t->range / barnum;
74         int igeta = t->value / unit;
75         wgChar bar[barnum + 3 + 1];
76         wgChar *text = bar;
77         int i;
78         assert(igeta <= barnum);
79         PRINTF(wgT("%s 0x%06x/0x%06x "), t->name, (int) t->value, (int) t->range);
80         *text++ = wgT('|');
81         for(i = 0; i < igeta; i++){
82                 if(i == barnum / 2){
83                         *text++ = wgT('|');
84                 }
85                 *text++ = wgT('#');
86         }
87         for(; i < barnum; i++){
88                 if(i == barnum / 2){
89                         *text++ = wgT('|');
90                 }
91                 *text++ = wgT(' ');
92         }
93         *text++ = wgT('|');
94         *text = wgT('\0');
95         PRINTF(bar);
96 //      fflush(stdout);
97 }
98
99
100 static void value_set(void *obj, void *d, int value)
101 {
102         struct cui_gauge *t = (struct cui_gauge *) obj;
103         t->value = value;
104         if(t->range != 0){
105                 draw(t);
106         }else{
107                 PRINTF(wgT("%s skip"), t->name);
108         }
109         console_move(1);
110 }
111
112 static void value_add(void *obj, void *d, int value)
113 {
114         struct cui_gauge *t = (struct cui_gauge *) obj;
115         t->value += value;
116         console_move(t->lineback);
117         draw(t);
118         console_move(t->lineforward);
119 }
120
121 static void name_set(void *obj, const wgChar *name, int lineforward, int lineback)
122 {
123         struct cui_gauge *t = (struct cui_gauge *) obj;
124         t->name = name;
125         t->lineforward = lineforward;
126         t->lineback = lineback;
127 }
128
129 static void label_set(void *obj, const wgChar *format, ...)
130 {
131         va_list list;
132         const struct cui_gauge *t = (const struct cui_gauge *) obj;
133
134         va_start(list, format);
135         console_move(t->lineback);
136         PRINTF(wgT("%s "), t->name);
137         VPRINTF(format, list);
138         console_move(t->lineforward);
139         va_end(list);
140 }
141
142 void cui_gauge_new(struct gauge *t, const wgChar *name, int lineforward, int lineback)
143 {
144         t->bar =  Malloc(sizeof(struct cui_gauge));
145         t->label = t->bar;
146         name_set(t->bar, name, lineforward, lineback);
147         t->range_set = range_set;
148         t->value_set = value_set;
149         t->value_add = value_add;
150         t->label_set = label_set;
151 }
152
153 void cui_gauge_destory(struct gauge *t)
154 {
155         Free(t->bar);
156         t->bar = NULL;
157         t->label = NULL;
158         t->range_set = NULL;
159         t->value_set = NULL;
160         t->value_add = NULL;
161         t->label_set = NULL;
162 }