forked from uboborov/EFM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
efm_c.c
3371 lines (2993 loc) · 93.1 KB
/
efm_c.c
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2008-2016 Yuri Bobrov
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.
*/
#include <stdio.h>
#include <string.h>
#include "efm_c.h"
#include "dav.h"
#if defined(CONFIG_EMBEDDED) || defined (OS_LINUX)
#include "cport.h"
#endif
#define T_HORIZONTAL 0
#define T_VERTICAL 1
#define T_NORMAL 0
#define T_INVERSE 1
#define T_CURSOR_OFF 0
#define T_CURSOR_ON 1
#ifndef CONFIG_EMBEDDED
#include <dirent.h>
#include <sys/stat.h>
#include <wchar.h>
#endif
#ifdef NET_IO
extern struct netconn *get_conn();
extern int telnet_get_char(int tm);
#endif
int init_window(window_t *pWindow, unsigned int height, unsigned int width,
unsigned int ncol, unsigned int nrow, unsigned int x,
unsigned int y, unsigned int npanel,
int (*handler) (void *arg), char *text, window_t *parent);
int edit_en_callback(void *parg);
int en_callback(void *parg);
void init_graphic(int type);
static int win_show_dir(window_t *win);
int win_read_dir_context(window_t *win, char *dir);
int fs_item_is_dir(fs_dirent *direntp, char *path);
//********************* Terminal *****************************************
static void vt_set_cursor_pos(int line, int col);
static void vt_clear_screen();
static void vt_clear_line(unsigned int mode);
static void vt_set_mode(unsigned int mode);
//****************** console dependent functions *************************
#ifdef NET_IO
void net_wr_str(const char *str) {
struct netconn *conn = get_conn();
netconn_write(conn, (void *) str, strlen(str), NETCONN_COPY);
}
void net_wr_data(char *data, int len) {
struct netconn *conn = get_conn();
netconn_write(conn, (void *) data, len, NETCONN_COPY);
}
void net_wr_char(char c) {
struct netconn *conn = get_conn();
netconn_write(conn, (void *) &c, 1, NETCONN_COPY);
}
int net_get_char() {
return telnet_get_char(0);
}
#else
#if defined(PC_IO) || defined(OS_LINUX)
void com_wr_str(const char *str) {
cputs((char *) str);
}
void com_wr_data(char *data, int len) {
write_port(data, len);
}
void com_wr_char(char c) {
cputchar(c);
}
int com_get_char() {
return cgetchar();
}
#endif
#endif
//********************* COMMON functions *********************************
/*
* make last entrie
*/
void last_entrie(char *p, int len) {
unsigned int i;
unsigned int cur_bs_pos = 0;
for (i = 0; i < len; i++) {
if (p[i] == EFM_DIR_SLASH_C) {
cur_bs_pos = i;
}
}
#if defined(CONFIG_EMBEDDED) || defined (OS_LINUX)
if (cur_bs_pos == 0) {
p[0] = EFM_DIR_SLASH_C;
p[1] = 0;
return;
}
#else
if (cur_bs_pos <= 3) {
strcpy(&p[0], "C:"EFM_DIR_SLASH_S);
return;
}
#endif
p[cur_bs_pos] = 0;
}
/*
* find last entrie pos
*/
int find_last_entrie(char *p) {
unsigned int len = 0;
len = strlen(p);
#if defined(CONFIG_EMBEDDED) || defined (OS_LINUX)
if (len == 1)
len = 0;
#else
if (len == 3)
len = 2;
#endif
return len;
}
/*
* set ROOT directory to the path
*/
int set_root(char *p, int len) {
unsigned int i;
unsigned int cur_bs_pos = 0;
for (i = 0; i < len; i++) {
if (p[i] == EFM_DIR_SLASH_C) {
cur_bs_pos = i + 1;
break;
}
}
if (i == 0) {
p[0] = EFM_DIR_SLASH_C;
p[1] = 0;
return 0;
}
p[cur_bs_pos] = 0;
}
/*
* add entrie
*/
int add_entrie(char *path, char *name, unsigned int mode) {
unsigned int entrie, i;
if (mode) {
for (i = 0; i < strlen(name); i++) {
if (name[i] == ' ')
name[i] = 0;
}
}
entrie = find_last_entrie(path);
path[entrie] = EFM_DIR_SLASH_C;
strcpy(&path[entrie + 1], name);
return 0;
}
/*
* vt100 set cursor
*/
static void vt_set_cursor_pos(int line, int col) {
char buf[10];
sprintf(buf, "\x1B[%d;%dH", line, col);
wr_str(buf);
}
/*
* low level set position in window
*/
static void ll_set_pos(window_t *win, unsigned int x, unsigned int y) {
win->cur_pos_x = x;
win->cur_pos_y = y;
set_cursor_pos(y, x);
}
/*
* low level get current position of cursor in window
*/
static int ll_get_pos(window_t *win, unsigned int *x, unsigned int *y) {
if (!win)
return -1;
*x = win->cur_pos_x;
*y = win->cur_pos_y;
return 0;
}
/*
* low level write to terminal screen
*/
static void ll_write(char *screen, unsigned int start, unsigned int len) {
wr_data(&screen[start], len);
}
/*
* vt100 clear screen
*/
static void vt_clear_screen() {
wr_str("\x1B[2J");
}
/*
* low level clear screen
*/
static void ll_clear_screen() {
clear_screen();
}
/*
* vt100 clear line
*/
static void vt_clear_line(unsigned int mode) {
wr_str("\x1B[2K");
}
/*
* low level clear line
*/
static void ll_clear_line(unsigned int mode) {
clear_line(mode);
}
/*
* write text to the window:
* win - window
* data - text to write
* row - start row
* col - start column
* len - text length
* mode - horizontal or vertical text
*/
static int win_write(window_t *win, char *data, unsigned int *row,
unsigned int *col, unsigned int len, unsigned int mode) {
unsigned int offset, nrow = (*row), ncol = (*col);
if (!win)
return -1;
offset = ((win->width * (*row - win->s_y)) + (*col - win->s_x));
if (mode == 0)
ncol = win->cur_pos_x + (len % win->width);
else
nrow = win->cur_pos_y + (len % win->height);
memcpy(&win->screen[offset], data, len);
ll_write(win->screen, offset, len);
ll_set_pos(win, ncol, nrow);
return 0;
}
static int win_write_dry(window_t *win, char *data, unsigned int *row,
unsigned int *col, unsigned int len, unsigned int mode) {
unsigned int offset, nrow = (*row), ncol = (*col);
if (!win)
return -1;
offset = ((win->width * (*row - win->s_y)) + (*col - win->s_x));
if (mode == 0)
ncol = win->cur_pos_x + (len % win->width);
else
nrow = win->cur_pos_y + (len % win->height);
memcpy(&win->screen[offset], data, len);
// set current position intead writing on screen
win->cur_pos_x = ncol;
win->cur_pos_y = nrow;
return 0;
}
/*
* read text from window:
* win - window
* data - where to read text
* row - start row
* col - start column
* len - text len (not used now)
*/
static int win_read(window_t *win, char *data, unsigned int *row,
unsigned int *col, unsigned int len) {
unsigned int offset;
offset = ((win->width * (*row - win->s_y)) + (*col - win->s_x));
//memcpy(data, &win->screen[offset],len);
strcpy(data, &win->screen[offset]);
return 0;
}
/*
* clear terminal screen and set cursor to 1:1
*/
int win_clear_screen(window_t *win) {
if (!win)
return -1;
ll_clear_screen();
ll_set_pos(win, 1, 1);
return 0;
}
/*
* clear line in window
* win - window
* mode - vertical or horizontal line (not implemented)
*/
int win_clear_cur_line(window_t *win, unsigned int mode) {
unsigned int x, y, offset;
if (!win)
return -1;
ll_get_pos(win, &x, &y);
x = 1;
offset = (((win->width * y) - win->s_y) + (x - win->s_x));
memset(&win->screen[offset], 0, win->width);
ll_clear_line(mode);
ll_set_pos(win, x, y);
return 0;
}
/*
* pd_u - panel delimiter upper;
* pd_l - panel delimiter lower;
* cd_u - column delimiter upper;
* cd_l - column delimiter lower;
* llh - line horizontal;
* llv - line vertical;
* llvp - panel line vertical;
* cel - colunm element;
* ruc - right upper corner;
* luc - left upper corner;
* llc - left lower corner;
* rlc - right lower corner;
*/
static char pd_u, cd_u, cd_l, pd_l, llh, llv, ruc, luc, llc, rlc, llvp, cel;
void init_graphic(int type) {
if (type == 0) {
pd_u = cd_u = cd_l = pd_l = '+';
llh = '='; llv = '|'; llvp = 'I', cel = '|';
ruc = luc = llc = rlc = '+';
} else {
llh = 205; llv = 186, llvp = 186, cel = 179;
ruc = 201; luc = 187; llc = 188, rlc = 200;
pd_u = 203, cd_u = 209, pd_l = 202, cd_l = 207;
}
}
/*
* draw line
* win - window
* line_element - charachter element for lin drowing ('*')
* lelen - line elements length (for additional use)
* mode - vertical or horizontal line
* x - start x position
* y - start y position
* len - line length in elements
*/
int draw_line(window_t *win, char *line_element, unsigned int lelen,
unsigned int mode, unsigned int x, unsigned int y,
unsigned int len) {
unsigned int i = 0, j;
ll_set_pos(win, x, y);
if (mode == T_HORIZONTAL) {
// hor
if (len > (win->width))
len = win->width;
for (j = 0; j < len; j++) {
if (lelen > 1) {
for (i = 0; i < lelen - 1; i++)
wr_char(line_element[i]);
} else
win_write(win, &line_element[i], &win->cur_pos_y,
&win->cur_pos_x, 1, T_HORIZONTAL);
}
} else if (mode == T_VERTICAL) {
// vert
if (len > (win->height))
len = win->height;
for (j = 0; j < len; j++) {
if (lelen > 1) {
for (i = 0; i < lelen - 1; i++)
wr_char(line_element[i]);
} else
win_write(win, &line_element[i], &win->cur_pos_y,
&win->cur_pos_x, 1, T_VERTICAL);
}
}
return 0;
}
/*
* draw rectangle
* win - window
* x - start x position
* y - start y position
* width - rec width
* height - rec heigth
*/
int draw_rectangle(window_t *win, unsigned int x, unsigned int y,
unsigned int width, unsigned int height) {
unsigned int tx, ty;
tx = x;
ty = y;
// left top corner
draw_line(win, &ruc, 1, T_HORIZONTAL, tx, ty, 1);
// top horizontal line
draw_line(win, &llh, 1, T_HORIZONTAL, tx + 1, ty, width - 1);
tx += width - 1;
// right top corner
draw_line(win, &luc, 1, T_HORIZONTAL, tx, ty, 1);
// right vertical line
draw_line(win, &llv, 1, T_VERTICAL, tx, ty + 1, height - 1);
ty += height - 1;
tx -= width - 1;
// buttom horizontal line
draw_line(win, &llh, 1, T_HORIZONTAL, tx, ty, width - 1);
tx += width - 1;
// right buttom corner
draw_line(win, &llc, 1, T_HORIZONTAL, tx, ty, 1);
tx -= width - 1;
ty -= height - 1;
// left vertical line
draw_line(win, &llv, 1, T_VERTICAL, tx, ty + 1, height - 1);
ty += height - 1;
// left buttom corner
draw_line(win, &rlc, 1, T_HORIZONTAL, tx, ty, 1);
return 0;
}
/*
* set VT cursor ON/OFF
*/
static void vt_cursor_mode(unsigned int mode) {
if (mode == T_CURSOR_ON)
wr_str("\x1B[?25h");
else if (mode == T_CURSOR_OFF)
wr_str("\x1B[?25l");
}
/*
* set VT normal or inverse mode
*/
static void vt_set_mode(unsigned int mode) {
if (mode == T_NORMAL)
wr_str("\x1B[0m");
else if (mode == T_INVERSE)
wr_str("\x1B[7m");
//wr_str("\x1B#7\x1B[7m");
}
/*
* draw text
* win - window
* x - start x position
* y - start y position
* text - text to write
* len - text length
* mode - horizontal or vertical text
*/
int draw_text(window_t *win, unsigned int x, unsigned int y, char *text,
unsigned int len, unsigned int mode) {
char *p = text;
char *pc = NULL;
int cur_y = y;
int cur_len = 0;
int ret = 0;
set_mode(mode);
while (!ret) {
pc = strchr(p, '\n');
if (pc) {
cur_len = pc - p;
} else {
cur_len = len;
ret = 1;
}
ll_set_pos(win, x, cur_y);
win_write(win, p, &win->cur_pos_y, &win->cur_pos_x, cur_len,
T_HORIZONTAL);
len = len - (cur_len + 1);
if (len) {
p = pc + 1;
if (++cur_y >= win->num_row) {
break;
}
} else
break;
}
// reverse mode
switch (mode) {
case T_NORMAL:
// normal mode
// stand int this mode
break;
case T_INVERSE:
// inverse mode
// return in normal mode
set_mode(T_NORMAL);
break;
}
return 0;
}
/*
* draw text to the screen buffer only (need to be printed)
*/
int draw_text_dry(window_t *win, unsigned int x, unsigned int y, char *text,
unsigned int len, unsigned int mode) {
char *p = text;
char *pc = NULL;
int cur_y = y;
int cur_len = 0;
int ret = 0;
while (!ret) {
pc = strchr(p, '\n');
if (pc) {
cur_len = pc - p;
} else {
cur_len = len;
ret = 1;
}
win->cur_pos_x = x;
win->cur_pos_y = cur_y;
win_write_dry(win, p, &win->cur_pos_y, &win->cur_pos_x, cur_len,
T_HORIZONTAL);
len = len - (cur_len + 1);
if (len) {
p = pc + 1;
if (++cur_y >= win->num_row) {
break;
}
} else
break;
}
return 0;
}
/*
* alloc window structure
*/
window_t * new_window() {
return efm_malloc(sizeof(window_t));
}
#if 0
/*
* create subwindow in the window:
* win - subwindow structure
* height - height
* width - width
* nrow - rows in symbols
* ncol - columns in symbols
* x - start x position
* y - start y position
*/
int win_create_sub_window(window_t *win, unsigned int height,
unsigned int width, unsigned int nrow,
unsigned int ncol, unsigned int x, unsigned int y) {
win->screen = (char *) efm_malloc(height * width);
if (!win->screen)
return -1;
win->num_col = ncol;
win->num_row = nrow;
win->s_x = x;
win->s_y = y;
memset(win->screen, 0, height * width);
return 0;
}
#endif
/*
* create new EDIT object:
* caption - caption text
* callback - edit callback function
* len - edit length
* active - activ or not in window
*/
edit_t * new_edit(char *caption, win_callback_t callback, unsigned int len,
unsigned int active) {
edit_t *edit = (edit_t *) efm_malloc(sizeof(edit_t));
if (!edit)
return NULL;
edit->buf = (char *) efm_malloc(len);
edit->len = len;
edit->edit_func = callback;
edit->id = EDIT;
return edit;
}
/*
* convert EDIT to element structure
*/
element_t * edit_2_el(edit_t *edit) {
element_t *el = (element_t *) efm_malloc(sizeof(element_t));
if (!el)
return NULL;
el->element_type = EDIT;
el->pelement = (void *) edit;
return el;
}
/*
* draw text in the EDIT
*/
int edit_add_text(window_t *win, edit_t *edit, char *text, unsigned int len) {
if (len >= edit->len)
return -1;
if (edit->cur_pos_x >= (edit->x + edit->w - 1))
return -1;
draw_text(win, edit->cur_pos_x, edit->cur_pos_y, text, len, T_NORMAL);
edit->cur_pos_x++;
return 0;
}
/*
* delete text in the EDIT
*/
int edit_del_text(window_t *win, edit_t *edit, unsigned int len) {
char *buf = (char *) efm_malloc(128);
unsigned int offset;
if (!buf)
return -1;
memset(buf, 0, 128);
if (edit->cur_pos_x > edit->x + 1) {
edit->cur_pos_x--;
offset = ((win->width * (edit->cur_pos_y - win->s_y)) +
(edit->cur_pos_x - win->s_x));
win->screen[offset] = 0;
ll_set_pos(win, edit->x + 1, edit->cur_pos_y);
win_read(win, buf, &win->cur_pos_y, &win->cur_pos_x, edit->len);
// clear char on screen
ll_set_pos(win, edit->cur_pos_x, edit->cur_pos_y);
wr_char(' ');
draw_text(win, edit->x + 1, edit->cur_pos_y, buf, strlen(buf),
T_NORMAL);
} else {
edit->cur_pos_x = edit->x + 1;
}
efm_free(buf);
return 0;
}
/*
* get text from the EDIT
*/
int edit_get_text(window_t *win, edit_t *edit, void *text) {
ll_set_pos(win, edit->x + 1, edit->cur_pos_y);
win_read(win, (char *) text, &win->cur_pos_y, &win->cur_pos_x, edit->len);
return 0;
}
/*
* set EDIT selection
*/
int win_set_edit_sel(window_t *win, edit_t *e, unsigned int mode) {
ll_set_pos(win, e->cur_pos_x, e->cur_pos_y);
return 0;
}
/*
* return first EDIT element if exists
*/
edit_t * win_get_edit(window_t *win) {
unsigned int i;
element_t *el;
for (i = 0; i < win->nelement; i++) {
el = (element_t *) win->element[i];
if (el->element_type == EDIT) {
edit_t *e;
e = (edit_t *) el->pelement;
return e;
}
}
return NULL;
}
/*
* create new BUTTON structure:
* caption - caption text
* callback - callback function
* cx - start x position
* cy - start y position
* active - active button or not in window
*/
button_t * new_button(char *caption, win_callback_t callback, unsigned int cx,
unsigned int cy, unsigned int active) {
button_t *but = (button_t *) efm_malloc(sizeof(button_t));
if (!but)
return NULL;
but->caption = caption;
but->button_func = callback;
but->id = BUTTON;
but->c_x = cx;
but->c_y = cy;
return but;
}
/*
* convert BUTTON to element structure
*/
element_t * but_2_el(button_t *but) {
element_t *el = (element_t *) efm_malloc(sizeof(element_t));
if (!el)
return NULL;
el->element_type = BUTTON;
el->pelement = (void *) but;
return el;
}
/*
* reurn active BUTTON in the window if exists, or NULL
*/
button_t * get_ative_but(window_t *win) {
unsigned int i;
element_t *el;
button_t *b;
for (i = 0; i < win->nelement; i++) {
el = win->element[i];
if (el->element_type == BUTTON) {
b = (button_t *) el->pelement;
if (b->active)
return b;
}
}
return NULL;
}
/*
* set button selection
*/
int win_set_but_sel(window_t *win, button_t *b, unsigned int mode) {
char buf[20];
unsigned int len;
memset(buf, 0, 20);
b->active = mode;
ll_set_pos(win, b->c_x, b->c_y);
win_read(win, buf, &b->c_y, &b->c_x, 1);
len = strlen(buf);
if (len) {
if (mode)
draw_text(win, b->c_x, b->c_y, buf, len, T_INVERSE);
else
draw_text(win, b->c_x, b->c_y, buf, len, T_NORMAL);
}
return 0;
}
/*
* set selected nex button
*/
int win_next_but(window_t *win) {
button_t *but = get_ative_but(win);
button_t *b = but->next;
if (!but)
return 0;
if (b) {
but->active = 0;
b->active = 1;
win_set_but_sel(win, but, T_NORMAL);
win_set_but_sel(win, b, T_INVERSE);
} else {
but->active = 1;
win_set_but_sel(win, but, T_INVERSE);
}
return 0;
}
/*
* place element
* win - window
* el - element type
* x - start x position
* y - start y position
* w - width of element
* h - height of element
*/
int win_place_element(window_t *win, element_t *el, unsigned int x,
unsigned int y, unsigned int w, unsigned int h) {
element_t *pel;
el->next = el;
if (win->nelement > 0) {
pel = (element_t *) win->element[win->nelement - 1];
pel->next = el;
el->next = win->element[0];
}
win->element[win->nelement++] = (void *) el;
if (el->element_type == BUTTON) {
button_t *b = (button_t *) el->pelement;
b->x = x;
b->y = y;
b->w = w;
b->h = h;
b->c_x = x + 1 + (((w - 1) - strlen(b->caption)) / 2);
b->c_y = y + 1;
} else if (el->element_type == EDIT) {
edit_t *e = (edit_t *) el->pelement;
e->x = x;
e->y = y;
e->w = w;
e->h = h;
e->cur_pos_x = x + 1;
e->cur_pos_y = y + 1;
}
return 0;
}
/*
* return active element
*/
int win_get_active_element(window_t *win) {
unsigned int i;
element_t *el;
for (i = 0; i < win->nelement; i++) {
el = (element_t *) win->element[i];
if (el->active)
return i;
}
return -1;
}
/*
* set active element
* nel - element index
*/
int win_set_active_element(window_t *win, unsigned int nel) {
element_t *el;
int nnel = -1;
if (nel < win->nelement) {
nnel = win_get_active_element(win);
if (nnel >= 0) {
el = (element_t *) win->element[nnel];
el->active = 0;
}
el = (element_t *) win->element[nel];
el->active = 1;
}
return 0;
}
/*
* set next elemet selected
*/
void win_next_element(window_t *win) {
element_t *el;
int nel = -1;
nel = win_get_active_element(win);
if (nel < 0)
return;
el = (element_t *) win->element[nel];
if (el->element_type == BUTTON) {
button_t *b = (button_t *) el->pelement;
b->active = 0;
win_set_but_sel(win, b, T_NORMAL);
}
el->active = 0;
el->next->active = 1;
el = el->next;
if (el->element_type == BUTTON) {
button_t *b = (button_t *) el->pelement;
b->active = 1;
win_set_but_sel(win, b, T_INVERSE);
//win_next_but(win);
} else if (el->element_type == EDIT) {
edit_t *e = (edit_t *) el->pelement;
win_set_edit_sel(win, e, 0);
}
}
/*
* set element of index nel selected
*/
void win_set_element_sel(window_t *win, unsigned int nel) {
unsigned int nnel = -1;
element_t *el;
nnel = win_get_active_element(win);
if (nnel >= 0) {
el = (element_t *) win->element[nnel];
el->active = 0;
if (el->element_type == BUTTON) {
button_t *b = (button_t *) el->pelement;
win_set_but_sel(win, b, T_NORMAL);
}
}
el = (element_t *) win->element[nel];
el->active = 1;
if (el->element_type == BUTTON) {
button_t *b = (button_t *) el->pelement;
win_set_but_sel(win, b, T_INVERSE);
} else if (el->element_type == EDIT) {
edit_t *e = (edit_t *) el->pelement;
win_set_edit_sel(win, e, 0);
}
}
/*
* create sub window with edit for 50 symbols and "YES/NO" buttons
* in the middle of the screen
*/
int win_create_edit_en_win(window_t *win, char *text, window_t *parent) {
button_t *but_y, *but_n;
edit_t *edit;
element_t *el1, *el2, *el3;
init_window(win, 9, 40, 1, 9, 30, 7, 1, edit_en_callback, text, parent);
/*
* caption text
* callback function
* start x position
* start y position
* active button or not
*/
but_y = new_button("YES", NULL, 0, 0, 1);
but_n = new_button("NO", NULL, 0, 0, 0);
but_y->next = but_n;
but_n->next = but_y;
el1 = but_2_el(but_y);
el2 = but_2_el(but_n);
edit = new_edit(NULL, NULL, 50, 0);
el3 = edit_2_el(edit);
win_place_element(win, el1, win->s_x + 2, win->s_y + 5, 10, 3);
win_place_element(win, el2, (win->s_x + (win->width - 2 - 10)),
win->s_y + 5, 10, 3);
win_place_element(win, el3, win->s_x + 2, win->s_y + 2, win->width - 4, 3);
win_set_active_element(win, 2);
return 0;
}
/*
* create subwindow with "OK" button
*/
int win_create_help_win(window_t *win, char *text, window_t *parent) {
button_t *but_ok;
element_t *el1;
init_window(win, 15, 60, 1, 15, 2, 2, 1, en_callback, text, parent);
but_ok = new_button("OK", NULL, 0, 0, 1);
but_ok->next = but_ok;
el1 = but_2_el(but_ok);
win_place_element(win, el1, win->s_x + 25, win->s_y + 10, 10, 3);