OSDN Git Service

svn rev.329より移動。
[chnosproject/CHNOSProject.git] / CHNOSProject / chnos / tolset_chn_000 / chnos_009 / chnos / inputbox.c
1 \r
2 #include "core.h"\r
3 \r
4 UI_InputBox *InputBox_Initialise(UI_Sheet_Control *sheetctrl, IO_MemoryControl *memctrl, uint x, uint y, uint xsize, uint ysize, uint txtbufsize, uint forecol, uint backcol, uint height)\r
5 {\r
6         UI_InputBox *box;\r
7 \r
8         box = MemoryBlock_Allocate_System(sizeof(UI_InputBox));\r
9         MemoryBlock_Write_Description(box, "UI_InputBox");\r
10 \r
11         xsize = xsize & ~7;\r
12         ysize = ysize & ~15;\r
13         box->sheet = Sheet_Get(sheetctrl, xsize, ysize, 0, 0);\r
14         box->input_buf_size = txtbufsize;\r
15         box->input_buf = (uchar *)MemoryBlock_Allocate_User(box->input_buf_size, memctrl);\r
16         MemoryBlock_Write_Description(box->input_buf, "INPBOX_TXTBUF");\r
17         box->input_count = 0;\r
18         box->cursor.x = 0;\r
19         box->cursor.y = 0;\r
20         box->prompt.x = -8;\r
21         box->prompt.y = 0;\r
22         box->forecol = forecol;\r
23         box->backcol = backcol;\r
24         box->cursor_state = false;\r
25         box->record = true;\r
26         Draw_Fill_Rectangle(box->sheet->vram, box->sheet->size.x, box->backcol, 0, 0, box->sheet->size.x - 1, box->sheet->size.y - 1);\r
27         Sheet_Show(box->sheet, x, y, height);\r
28         return box;\r
29 }\r
30 \r
31 int InputBox_Put_String(UI_InputBox *box, const uchar *s)\r
32 {\r
33         uint i;\r
34         int count, old;\r
35 \r
36         if(!box->record){\r
37                 InputBox_Put_String_Main(box, s);\r
38                 return 0;\r
39         }\r
40 \r
41         count = 0;\r
42 \r
43         for(i = 0; s[i] != 0x00; i++){\r
44                 if(s[i] == '\b'){\r
45                         if(count != 0){\r
46                                 count--;\r
47                         }\r
48                 } else{\r
49                         count++;\r
50                 }\r
51         }\r
52 \r
53         if(count > (box->input_buf_size - box->input_count)){\r
54                 return -1;\r
55         }\r
56 \r
57         InputBox_Put_String_Main(box, s);\r
58 \r
59         old = count;\r
60         count = 0;\r
61 \r
62         for(i = 0; count < old; i++){\r
63                 if(s[i] == '\b'){\r
64                         if(count != 0) count--;\r
65                 } else{\r
66                         box->input_buf[box->input_count + count] = s[i];\r
67                         count++;\r
68                 }\r
69         }\r
70         box->input_buf[box->input_count + count] = 0x00;\r
71         box->input_count += count;\r
72 \r
73         return count;   \r
74 }\r
75 \r
76 int InputBox_Put_Character(UI_InputBox *box, uchar c)\r
77 {\r
78         uchar str[2];\r
79 \r
80         str[0] = c;\r
81         str[1] = 0x00;\r
82         return InputBox_Put_String(box, str);\r
83 }\r
84 \r
85 void InputBox_Put_String_Main(UI_InputBox *box, const uchar *str)\r
86 {\r
87         int i;\r
88         uchar s[3];\r
89 \r
90         InputBox_Check_NewLine(box);\r
91 \r
92         for(i = 0; ; i++){\r
93                 if(str[i] == 0x00){\r
94                         break;\r
95                 } else if(str[i] == '\r'){\r
96 \r
97                 } else if(str[i] == '\n'){\r
98                         InputBox_NewLine_No_Prompt(box);\r
99                 } else if(str[i] == '\t'){\r
100                         for(;;){\r
101                                 Sheet_Draw_Fill_Rectangle(box->sheet, box->backcol, box->cursor.x, box->cursor.y, box->cursor.x + 8 - 1, box->cursor.y + 16 - 1);\r
102                                 box->cursor.x += 8;\r
103                                 InputBox_Check_NewLine(box);\r
104                                 if((box->cursor.x & 0x1f) == 0 && box->cursor.x != 0) break;\r
105                         }\r
106                 } else if(str[i] == '\b'){\r
107                         Sheet_Draw_Fill_Rectangle(box->sheet, box->backcol, box->cursor.x, box->cursor.y, box->cursor.x + 8 - 1, box->cursor.y + 16 - 1);\r
108                         box->cursor.x -= 8;\r
109                         InputBox_Check_NewLine(box);\r
110                         Sheet_Draw_Fill_Rectangle(box->sheet, box->backcol, box->cursor.x, box->cursor.y, box->cursor.x + 8 - 1, box->cursor.y + 16 - 1);\r
111                         if(box->input_count != 0 && box->record){\r
112                                 box->input_count--;\r
113                                 box->input_buf[box->input_count] = 0x00;\r
114                         }\r
115                 } else{\r
116                         s[0] = str[i];\r
117                         s[1] = 0x00;\r
118                         Sheet_Draw_Fill_Rectangle(box->sheet, box->backcol, box->cursor.x, box->cursor.y, box->cursor.x + 8 - 1, box->cursor.y + 16 - 1);\r
119                         Sheet_Draw_Put_String(box->sheet, box->cursor.x, box->cursor.y, box->forecol, s);\r
120                         box->cursor.x += 8;\r
121                         InputBox_Check_NewLine(box);\r
122                 }\r
123         }\r
124         return;\r
125 }\r
126 \r
127 void InputBox_Check_NewLine(UI_InputBox *box)\r
128 {\r
129         if(box->cursor.x <= box->prompt.x || box->cursor.x < 0){        /*\83v\83\8d\83\93\83v\83g\82Ü\82½\82Í\89æ\96Ê\82æ\82è\8d\82É\88Ú\93®\82µ\82æ\82¤\82Æ\82µ\82½*/\r
130                 if(box->cursor.y != box->prompt.y){     /*\83v\83\8d\83\93\83v\83g\82Æ\93¯\82\8ds\82Å\82Í\82È\82¢*/\r
131                                 box->cursor.y -= 16;\r
132                                 box->cursor.x = box->sheet->size.x - 8;\r
133                 } else{/*\83v\83\8d\83\93\83v\83g\82Æ\93¯\82\8ds*/\r
134                         box->cursor.y = box->prompt.y;\r
135                         box->cursor.x = box->prompt.x + 8;\r
136                 }\r
137         } else if(box->cursor.x >= box->sheet->size.x){\r
138                 if(box->cursor.y <= box->sheet->size.y - 17){\r
139                         box->cursor.x = box->prompt.x + 8;\r
140                         box->cursor.y += 16;\r
141                 } else{\r
142                         InputBox_Slide_Line(box);\r
143                         box->cursor.x = box->prompt.x + 8;\r
144                         if(box->prompt.y > 0){\r
145                                 box->prompt.y -= 16;\r
146                         } else{\r
147                                 box->prompt.y = 0;\r
148                         }\r
149                 }\r
150         }\r
151         if(box->cursor.y < box->prompt.y){\r
152                 box->cursor.y = box->prompt.y;\r
153         }\r
154         return;\r
155 }\r
156 \r
157 void InputBox_NewLine_No_Prompt(UI_InputBox *box)\r
158 {\r
159         if(box->cursor.y <= box->sheet->size.y - 17){\r
160                 Sheet_Draw_Fill_Rectangle(box->sheet, box->backcol, box->cursor.x, box->cursor.y, box->cursor.x + 8 - 1, box->cursor.y + 16 - 1);\r
161                 box->cursor.x = box->prompt.x + 8;\r
162                 box->cursor.y = box->cursor.y + 16;\r
163         } else{\r
164                 Sheet_Draw_Fill_Rectangle(box->sheet, box->backcol, box->cursor.x, box->cursor.y, box->cursor.x + 8 - 1, box->cursor.y + 16 - 1);\r
165                 InputBox_Slide_Line(box);\r
166                 box->cursor.x = box->prompt.x + 8;\r
167         }\r
168         InputBox_Check_NewLine(box);\r
169         return;\r
170 }\r
171 \r
172 void InputBox_NewLine(UI_InputBox *box)\r
173 {\r
174         if(box->cursor.y <= box->sheet->size.y - 17){\r
175                 box->prompt.y = box->cursor.y + 16;\r
176                 InputBox_Put_Prompt(box);\r
177         } else{\r
178                 Sheet_Draw_Fill_Rectangle(box->sheet, box->backcol, box->cursor.x, box->cursor.y, box->cursor.x + 16 - 1, box->cursor.y + 16 - 1);\r
179                 InputBox_Slide_Line(box);\r
180                 box->prompt.y = box->sheet->size.y - 16;\r
181                 InputBox_Put_Prompt(box);\r
182         }\r
183         return;\r
184 }\r
185 \r
186 void InputBox_Slide_Line(UI_InputBox *box)\r
187 {\r
188 //      Sheet_Draw_Fill_Rectangle(box->sheet, box->backcol, box->cursor.x, box->cursor.y, box->cursor.x + 8 - 1, box->cursor.y + 16 - 1);\r
189         Draw_Slide_Line(box->sheet->vram, box->sheet->size.x, box->sheet->size.y, box->sheet->size.x, 0, 0);\r
190         Sheet_Draw_Fill_Rectangle(box->sheet, box->backcol, 0, box->sheet->size.y - 16, box->sheet->size.x - 1, box->sheet->size.y - 1);\r
191         box->sheet->Refresh(box->sheet, 0, 0, box->sheet->size.x - 1, box->sheet->size.y - 1);\r
192         return;\r
193 }\r
194 \r
195 void InputBox_Put_Prompt(UI_InputBox *box)\r
196 {\r
197         Sheet_Draw_Fill_Rectangle(box->sheet, box->backcol, box->cursor.x, box->cursor.y, box->cursor.x + 16 - 1, box->cursor.y + 16 - 1);\r
198         box->prompt.x = 0;\r
199         Sheet_Draw_Put_String(box->sheet, box->prompt.x, box->prompt.y, box->forecol, ">");\r
200         box->cursor.x = box->prompt.x + 8;\r
201         box->cursor.y = box->prompt.y;\r
202         return;\r
203 }\r
204 \r
205 void InputBox_Reset_Input_Buffer(UI_InputBox *box)\r
206 {\r
207         box->input_buf[0] = 0x00;\r
208         box->input_count = 0;\r
209         return;\r
210 }\r
211 \r
212 void InputBox_Change_Cursor_State(UI_InputBox *box)\r
213 {\r
214         if(box->cursor_state){\r
215                 Sheet_Draw_Fill_Rectangle(box->sheet, box->backcol, box->cursor.x, box->cursor.y, box->cursor.x + 8 - 1, box->cursor.y + 16 - 1);\r
216                 box->cursor_state = false;\r
217         } else{\r
218                 Sheet_Draw_Fill_Rectangle(box->sheet, box->forecol, box->cursor.x, box->cursor.y, box->cursor.x + 8 - 1, box->cursor.y + 16 - 1);\r
219                 box->cursor_state = true;\r
220         }\r
221         return;\r
222 }\r
223 \r
224 void InputBox_Clear(UI_InputBox *box)\r
225 {\r
226         InputBox_Reset_Input_Buffer(box);\r
227         box->cursor.x = 0;\r
228         box->cursor.y = 0;\r
229         box->prompt.x = -8;\r
230         box->prompt.y = 0;\r
231         Sheet_Draw_Fill_Rectangle(box->sheet, box->backcol, 0, 0, box->sheet->size.x - 1, box->sheet->size.y - 1);\r
232         return;\r
233 }\r
234 \r
235 void InputBox_Set_Record(UI_InputBox *box, bool record)\r
236 {\r
237         box->record = record;\r
238         return;\r
239 }\r