Rev 7798 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
5962 | leency | 1 | #define MEMSIZE 4096*25 |
5819 | leency | 2 | |
7466 | leency | 3 | //===================================================// |
4 | // // |
||
5 | // LIB // |
||
6 | // // |
||
7 | //===================================================// |
||
8 | |||
5886 | pavelyakov | 9 | #include "../lib/io.h" |
7288 | leency | 10 | #include "../lib/gui.h" |
5819 | leency | 11 | #include "../lib/list_box.h" |
7286 | leency | 12 | #include "../lib/kfont.h" |
7230 | leency | 13 | |
5819 | leency | 14 | #include "../lib/obj/box_lib.h" |
15 | #include "../lib/obj/libini.h" |
||
7230 | leency | 16 | #include "../lib/obj/libimg.h" |
5819 | leency | 17 | #include "../lib/obj/iconv.h" |
18 | #include "../lib/obj/proc_lib.h" |
||
7230 | leency | 19 | |
5819 | leency | 20 | #include "../lib/patterns/simple_open_dialog.h" |
21 | |||
7466 | leency | 22 | //===================================================// |
23 | // // |
||
24 | // DATA // |
||
25 | // // |
||
26 | //===================================================// |
||
27 | |||
5819 | leency | 28 | #define TOOLBAR_H 34 |
7784 | leency | 29 | #define TOOLBAR_ICON_WIDTH 24 |
30 | #define TOOLBAR_ICON_HEIGHT 22 |
||
5819 | leency | 31 | |
5923 | leency | 32 | #define DEFAULT_EDITOR "/sys/tinypad" |
33 | |||
5980 | leency | 34 | #define INTRO_TEXT "This is a plain Text Reader.\nTry to open some text file." |
7466 | leency | 35 | #define VERSION "Text Reader v1.3" |
5962 | leency | 36 | #define ABOUT "Idea: Leency, punk_joker |
37 | Code: Leency, Veliant, KolibriOS Team |
||
5923 | leency | 38 | |
5962 | leency | 39 | Hotkeys: |
40 | Ctrl+O - open file |
||
7218 | leency | 41 | Ctrl+I - show file properties |
5962 | leency | 42 | Ctrl+Up - bigger font |
43 | Ctrl+Down - smaller font |
||
44 | Ctrl+Tab - select charset |
||
7462 | leency | 45 | Ctrl+E - reopen current file in another app |
5962 | leency | 46 | |
47 | Press any key..." |
||
48 | |||
7466 | leency | 49 | dword color_schemes[] = { |
50 | 0xFFFfff, 0, |
||
51 | 0xF0F0F0, 0, |
||
7493 | leency | 52 | 0xFDF6E3, 0x101A21, |
53 | 0xFCF0DA, 0x171501, |
||
7466 | leency | 54 | 0xF0F0C7, 0, |
55 | 0x282C34, 0xABB2BF, |
||
56 | 0x282923, 0xD8D8D2 |
||
57 | }; |
||
58 | |||
59 | char color_scheme_names[] = |
||
60 | "White & Black |
||
61 | Grey & Black RtfRead |
||
7493 | leency | 62 | Linen & Black Horst |
63 | Antique & Black Pocket |
||
7466 | leency | 64 | Lemon & Black Fb2Read |
65 | DarkGrey & Grey Godot |
||
66 | DarkGrey & Grey Monokai"; |
||
67 | |||
5819 | leency | 68 | char default_dir[] = "/rd/1"; |
7286 | leency | 69 | od_filter filter2 = { 8, "TXT\0\0" }; |
5819 | leency | 70 | |
71 | scroll_bar scroll = { 15,200,398,44,0,2,115,15,0,0xeeeeee,0xBBBbbb,0xeeeeee,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1}; |
||
72 | llist list; |
||
73 | |||
74 | proc_info Form; |
||
75 | char title[4196]; |
||
76 | |||
7218 | leency | 77 | bool help_opened = false; |
7466 | leency | 78 | int charsets_mx; |
79 | int reopenin_mx; |
||
80 | int colscheme_mx; |
||
5819 | leency | 81 | |
7466 | leency | 82 | int curcol_scheme; |
83 | |||
5819 | leency | 84 | enum { |
5962 | leency | 85 | OPEN_FILE, |
5819 | leency | 86 | MAGNIFY_MINUS, |
87 | MAGNIFY_PLUS, |
||
88 | CHANGE_ENCODING, |
||
89 | RUN_EDIT, |
||
90 | SHOW_INFO, |
||
7466 | leency | 91 | SHOW_FILE_PROPERTIES, |
92 | COLOR_SCHEME |
||
5819 | leency | 93 | }; |
94 | |||
7051 | leency | 95 | int encoding; |
96 | |||
7466 | leency | 97 | dword bg_color; |
98 | dword text_color; |
||
7462 | leency | 99 | |
7781 | leency | 100 | dword menu_id; |
101 | |||
7466 | leency | 102 | //===================================================// |
103 | // // |
||
104 | // INTERNAL INCLUDES // |
||
105 | // // |
||
106 | //===================================================// |
||
107 | |||
5819 | leency | 108 | #include "ini.h" |
5980 | leency | 109 | #include "prepare_page.h" |
5819 | leency | 110 | |
7466 | leency | 111 | //===================================================// |
112 | // // |
||
113 | // CODE // |
||
114 | // // |
||
115 | //===================================================// |
||
116 | |||
5923 | leency | 117 | void InitDlls() |
118 | { |
||
119 | load_dll(boxlib, #box_lib_init, 0); |
||
120 | load_dll(libio, #libio_init, 1); |
||
121 | load_dll(libimg, #libimg_init, 1); |
||
122 | load_dll(libini, #lib_init, 1); |
||
123 | load_dll(iconv_lib, #iconv_open, 0); |
||
124 | load_dll(Proc_lib, #OpenDialog_init,0); |
||
125 | } |
||
126 | |||
5819 | leency | 127 | void main() |
5923 | leency | 128 | { |
5987 | leency | 129 | InitDlls(); |
5819 | leency | 130 | OpenDialog_init stdcall (#o_dialog); |
7516 | leency | 131 | if (param) strcpy(#openfile_path, #param); |
7286 | leency | 132 | LoadIniSettings(); |
7466 | leency | 133 | EventSetColorScheme(curcol_scheme); |
6806 | leency | 134 | kfont.init(DEFAULT_FONT); |
5819 | leency | 135 | Libimg_LoadImage(#skin, abspath("toolbar.png")); |
136 | OpenFile(#param); |
||
137 | list.no_selection = true; |
||
7227 | leency | 138 | SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER); |
5819 | leency | 139 | loop() |
140 | { |
||
5923 | leency | 141 | switch(WaitEvent()) |
142 | { |
||
143 | case evMouse: |
||
144 | HandleMouseEvent(); |
||
5819 | leency | 145 | break; |
5923 | leency | 146 | case evKey: |
147 | HandleKeyEvent(); |
||
148 | break; |
||
149 | case evButton: |
||
150 | HandleButtonEvent(); |
||
151 | break; |
||
152 | case evReDraw: |
||
7781 | leency | 153 | if (CheckActiveProcess(Form.ID)) EventMenuClick(); |
5923 | leency | 154 | draw_window(); |
155 | } |
||
5819 | leency | 156 | } |
157 | } |
||
158 | |||
7466 | leency | 159 | //===================================================// |
160 | // // |
||
161 | // EVENTS // |
||
162 | // // |
||
163 | //===================================================// |
||
5980 | leency | 164 | |
165 | void HandleButtonEvent() |
||
5819 | leency | 166 | { |
5923 | leency | 167 | |
5980 | leency | 168 | byte btn = GetButtonID(); |
169 | if (btn==1) { |
||
170 | SaveIniSettings(); |
||
171 | ExitProcess(); |
||
172 | } |
||
173 | btn-=10; |
||
174 | switch(btn) |
||
5923 | leency | 175 | { |
5980 | leency | 176 | case OPEN_FILE: |
177 | EventOpenFile(); |
||
178 | break; |
||
7218 | leency | 179 | case SHOW_FILE_PROPERTIES: |
180 | EventShowFileProperties(); |
||
181 | break; |
||
5980 | leency | 182 | case MAGNIFY_PLUS: |
183 | EventMagnifyPlus(); |
||
184 | break; |
||
185 | case MAGNIFY_MINUS: |
||
186 | EventMagnifyMinus(); |
||
187 | break; |
||
188 | case CHANGE_ENCODING: |
||
7462 | leency | 189 | EventShowEncodingList(); |
5980 | leency | 190 | break; |
191 | case RUN_EDIT: |
||
7466 | leency | 192 | EventShowReopenMenu(); |
5980 | leency | 193 | break; |
7466 | leency | 194 | case COLOR_SCHEME: |
195 | EventShowColorSchemesList(); |
||
196 | break; |
||
5980 | leency | 197 | case SHOW_INFO: |
198 | EventShowInfo(); |
||
199 | break; |
||
5923 | leency | 200 | } |
5819 | leency | 201 | } |
202 | |||
203 | |||
5980 | leency | 204 | void HandleKeyEvent() |
5819 | leency | 205 | { |
5980 | leency | 206 | if (help_opened) { |
207 | help_opened = false; |
||
208 | DrawPage(); |
||
209 | return; |
||
210 | } |
||
211 | GetKeys(); |
||
7218 | leency | 212 | if (key_scancode == SCAN_CODE_F1) { |
5980 | leency | 213 | EventShowInfo(); |
214 | return; |
||
215 | } |
||
216 | if (key_modifier & KEY_LCTRL) || (key_modifier & KEY_RCTRL) { |
||
217 | switch (key_scancode) |
||
218 | { |
||
7218 | leency | 219 | case SCAN_CODE_KEY_O: |
5980 | leency | 220 | EventOpenFile(); |
221 | break; |
||
7218 | leency | 222 | case SCAN_CODE_KEY_I: |
223 | EventShowFileProperties(); |
||
224 | break; |
||
7798 | leency | 225 | case SCAN_CODE_PLUS: |
5980 | leency | 226 | case SCAN_CODE_UP: |
227 | EventMagnifyPlus(); |
||
228 | break; |
||
229 | case SCAN_CODE_DOWN: |
||
7798 | leency | 230 | case SCAN_CODE_MINUS: |
5980 | leency | 231 | EventMagnifyMinus(); |
232 | break; |
||
7218 | leency | 233 | case SCAN_CODE_KEY_E: |
7466 | leency | 234 | EventShowReopenMenu(); |
5980 | leency | 235 | break; |
236 | case SCAN_CODE_TAB: |
||
237 | EventChangeEncoding(); |
||
238 | break; |
||
5819 | leency | 239 | } |
5980 | leency | 240 | return; |
5819 | leency | 241 | } |
5980 | leency | 242 | if (list.ProcessKey(key_scancode)) |
243 | DrawPage(); |
||
244 | } |
||
5819 | leency | 245 | |
246 | |||
5980 | leency | 247 | void HandleMouseEvent() |
248 | { |
||
249 | mouse.get(); |
||
250 | list.wheel_size = 7; |
||
251 | if (list.MouseScroll(mouse.vert)) { |
||
252 | DrawPage(); |
||
253 | return; |
||
5819 | leency | 254 | } |
5980 | leency | 255 | scrollbar_v_mouse (#scroll); |
256 | if (list.first != scroll.position) { |
||
257 | list.first = scroll.position; |
||
258 | DrawPage(); |
||
259 | } |
||
5819 | leency | 260 | } |
261 | |||
5980 | leency | 262 | /* ----------------------------------------------------- */ |
263 | |||
264 | void EventOpenFile() |
||
5819 | leency | 265 | { |
5980 | leency | 266 | OpenDialog_start stdcall (#o_dialog); |
7286 | leency | 267 | if (o_dialog.status) { |
268 | OpenFile(#openfile_path); |
||
269 | PreparePage(); |
||
270 | } |
||
5819 | leency | 271 | } |
272 | |||
7218 | leency | 273 | void EventShowFileProperties() |
274 | { |
||
275 | char ss_param[4096]; |
||
276 | if (!param) return; |
||
7768 | leency | 277 | strcpy(#ss_param, "-p "); |
278 | strcpy(#ss_param+3, #param); |
||
7462 | leency | 279 | RunProgram("/sys/File managers/Eolite", #ss_param); |
7218 | leency | 280 | } |
281 | |||
5980 | leency | 282 | void EventMagnifyPlus() |
5819 | leency | 283 | { |
6806 | leency | 284 | kfont.size.pt++; |
285 | if(!kfont.changeSIZE()) |
||
286 | kfont.size.pt--; |
||
5980 | leency | 287 | else |
288 | PreparePage(); |
||
5819 | leency | 289 | } |
290 | |||
5980 | leency | 291 | void EventMagnifyMinus() |
292 | { |
||
6806 | leency | 293 | kfont.size.pt--; |
294 | if(!kfont.changeSIZE()) |
||
295 | kfont.size.pt++; |
||
5980 | leency | 296 | else |
297 | PreparePage(); |
||
298 | } |
||
299 | |||
7466 | leency | 300 | void EventShowEncodingList() |
5980 | leency | 301 | { |
7782 | leency | 302 | open_lmenu(Form.left+5 + charsets_mx, Form.top+29+skin_height, MENU_ALIGN_TOP_LEFT, |
303 | encoding+1, "UTF-8\nKOI8-RU\nCP1251\nCP1252\nISO8859-5\nCP866"); |
||
7781 | leency | 304 | menu_id = 10; |
7466 | leency | 305 | } |
306 | |||
307 | void EventShowReopenMenu() |
||
308 | { |
||
7781 | leency | 309 | open_lmenu(Form.left+5 + reopenin_mx, Form.top+29+skin_height, MENU_ALIGN_TOP_LEFT, 0, |
310 | "Tinypad\nTextEdit\nWebView\nFB2Read\nHexView\nOther"); |
||
311 | menu_id = 20; |
||
5980 | leency | 312 | } |
313 | |||
7466 | leency | 314 | void EventShowColorSchemesList() |
5980 | leency | 315 | { |
7781 | leency | 316 | open_lmenu(Form.left+5 + colscheme_mx, Form.top+29+skin_height, MENU_ALIGN_TOP_LEFT, |
317 | curcol_scheme+1, #color_scheme_names); |
||
318 | menu_id = 30; |
||
5980 | leency | 319 | } |
320 | |||
7466 | leency | 321 | void EventSetColorScheme(dword _setn) |
322 | { |
||
323 | curcol_scheme = _setn; |
||
324 | bg_color = color_schemes[curcol_scheme*2]; |
||
325 | text_color = color_schemes[curcol_scheme*2+1]; |
||
326 | } |
||
327 | |||
5980 | leency | 328 | void EventShowInfo() { |
329 | help_opened = true; |
||
330 | DrawBar(list.x, list.y, list.w, list.h, 0xFFFfff); |
||
331 | WriteText(list.x + 10, list.y + 10, 10000001b, 0x555555, VERSION); |
||
332 | WriteTextLines(list.x + 10, list.y+40, 10110000b, 0, ABOUT, 20); |
||
333 | } |
||
334 | |||
7462 | leency | 335 | void EventChangeEncoding(dword id) |
336 | { |
||
337 | encoding = id; |
||
338 | OpenFile(#openfile_path); |
||
339 | PreparePage(); |
||
340 | draw_window(); |
||
341 | } |
||
342 | |||
343 | void EventOpenFileInAnotherProgram(dword _app) |
||
344 | { |
||
345 | RunProgram(_app, #param); |
||
346 | } |
||
347 | |||
348 | void EventMenuClick() |
||
349 | { |
||
7466 | leency | 350 | byte open_param[4096]; |
7781 | leency | 351 | dword click_id = get_menu_click(); |
352 | if (click_id) switch(click_id + menu_id - 1) |
||
7462 | leency | 353 | { |
354 | //Encoding |
||
355 | case 10...15: |
||
7781 | leency | 356 | EventChangeEncoding(click_id-1); |
7462 | leency | 357 | break; |
358 | //Reopen |
||
359 | case 20: |
||
360 | EventOpenFileInAnotherProgram("/sys/tinypad"); |
||
361 | break; |
||
362 | case 21: |
||
363 | EventOpenFileInAnotherProgram("/sys/develop/t_edit"); |
||
364 | break; |
||
365 | case 22: |
||
366 | EventOpenFileInAnotherProgram("/sys/network/webview"); |
||
367 | break; |
||
368 | case 23: |
||
369 | EventOpenFileInAnotherProgram("/sys/fb2read"); |
||
370 | break; |
||
371 | case 24: |
||
372 | EventOpenFileInAnotherProgram("/sys/develop/heed"); |
||
373 | break; |
||
7466 | leency | 374 | case 25: |
7768 | leency | 375 | open_param[0]='~'; |
376 | strcpy(#open_param+1,#param); |
||
7466 | leency | 377 | RunProgram("/sys/@open", #open_param); |
378 | break; |
||
379 | //ColorSchemes |
||
380 | case 30...38: |
||
7781 | leency | 381 | EventSetColorScheme(click_id-1); |
7466 | leency | 382 | PreparePage(); |
383 | break; |
||
7462 | leency | 384 | } |
385 | } |
||
386 | |||
7466 | leency | 387 | //===================================================// |
388 | // // |
||
389 | // DRAWS AND OTHER FUNCS // |
||
390 | // // |
||
391 | //===================================================// |
||
5980 | leency | 392 | |
5819 | leency | 393 | void OpenFile(dword f_path) |
394 | { |
||
395 | int tmp; |
||
396 | if (ESBYTE[f_path]) { |
||
397 | strcpy(#param, f_path); |
||
398 | io.read(#param); |
||
399 | strcpy(#title, #param); |
||
400 | strcat(#title, " - Text Reader"); |
||
401 | } |
||
402 | else { |
||
403 | if (list.count) return; |
||
5923 | leency | 404 | io.buffer_data = INTRO_TEXT; |
5819 | leency | 405 | strcpy(#title, "Text Reader"); |
406 | } |
||
7781 | leency | 407 | if (encoding!=CH_CP866) ChangeCharset(encoding, "CP866", io.buffer_data); |
5819 | leency | 408 | list.KeyHome(); |
409 | list.ClearList(); |
||
410 | } |
||
411 | |||
5980 | leency | 412 | void draw_window() |
413 | { |
||
7466 | leency | 414 | #define BUTTONS_GAP 6 |
415 | #define BLOCKS_GAP 15 |
||
7218 | leency | 416 | #define TOOLBAR_BUTTON_WIDTH 26 |
417 | incn x; |
||
6746 | leency | 418 | DefineAndDrawWindow(Form.left,Form.top,Form.width,Form.height,0x73,0,#title,0); |
5980 | leency | 419 | GetProcessInfo(#Form, SelfInfo); |
7806 | leency | 420 | sc.get(); |
5980 | leency | 421 | if (Form.status_window>2) return; |
422 | |||
423 | if (Form.width < 200) { MoveSize(OLD,OLD,200,OLD); return; } |
||
424 | if (Form.height < 200) { MoveSize(OLD,OLD,OLD,200); return; } |
||
425 | |||
7806 | leency | 426 | DrawBar(0, 0, Form.cwidth, TOOLBAR_H - 1, sc.work); |
427 | DrawBar(0, TOOLBAR_H - 1, Form.cwidth, 1, sc.work_graph); |
||
5980 | leency | 428 | |
7218 | leency | 429 | x.n = 0; |
430 | DrawToolbarButton(OPEN_FILE, x.inc(8)); |
||
7466 | leency | 431 | DrawToolbarButton(SHOW_FILE_PROPERTIES, x.inc(TOOLBAR_BUTTON_WIDTH + BUTTONS_GAP)); |
432 | |||
433 | DrawToolbarButton(MAGNIFY_MINUS, x.inc(TOOLBAR_BUTTON_WIDTH + BLOCKS_GAP)); |
||
7428 | leency | 434 | DrawToolbarButton(MAGNIFY_PLUS, x.inc(TOOLBAR_BUTTON_WIDTH - 1)); |
7466 | leency | 435 | DrawToolbarButton(COLOR_SCHEME, x.inc(TOOLBAR_BUTTON_WIDTH + BUTTONS_GAP)); colscheme_mx = x.n; |
436 | |||
437 | DrawToolbarButton(CHANGE_ENCODING, x.inc(TOOLBAR_BUTTON_WIDTH + BLOCKS_GAP)); charsets_mx = x.n; |
||
438 | DrawToolbarButton(RUN_EDIT, x.inc(TOOLBAR_BUTTON_WIDTH + BLOCKS_GAP)); reopenin_mx = x.n; |
||
5980 | leency | 439 | DrawToolbarButton(SHOW_INFO, Form.cwidth - 34); |
440 | |||
441 | if ((Form.cwidth-scroll.size_x-1 == list.w) && |
||
442 | (Form.cheight-TOOLBAR_H == list.h) && |
||
443 | (list.count) |
||
7798 | leency | 444 | ) { |
5980 | leency | 445 | DrawPage(); |
446 | } else { |
||
447 | PreparePage(); |
||
7466 | leency | 448 | } |
449 | |||
5980 | leency | 450 | DrawRectangle(scroll.start_x, scroll.start_y, scroll.size_x, scroll.size_y-1, scroll.bckg_col); |
5923 | leency | 451 | } |
5980 | leency | 452 | |
453 | void DrawPage() |
||
454 | { |
||
6808 | leency | 455 | kfont.ShowBufferPart(list.x, list.y, list.w, list.h, list.first*list.item_h*list.w); |
5980 | leency | 456 | DrawScroller(); |
7286 | leency | 457 | } |
458 | |||
459 | void DrawToolbarButton(char image_id, int x) |
||
460 | { |
||
7784 | leency | 461 | DefineButton(x+1, 6, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, 10+image_id + BT_HIDE, 0); |
462 | //img_draw stdcall(skin.image, x, 5, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, TOOLBAR_ICON_WIDTH-1*image_id, 0); |
||
7806 | leency | 463 | DrawOvalBorder(x, 5, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, sc.work_graph, |
464 | sc.work_graph,sc.work_graph, sc.work_dark); |
||
7784 | leency | 465 | img_draw stdcall(skin.image, x+1, 5+1, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, TOOLBAR_ICON_WIDTH*image_id, 0); |
7286 | leency | 466 | } |
467 | |||
468 | void DrawScroller() |
||
469 | { |
||
470 | scroll.max_area = list.count; |
||
471 | scroll.cur_area = list.visible; |
||
472 | scroll.position = list.first; |
||
473 | scroll.all_redraw = 0; |
||
474 | scroll.start_x = list.x + list.w; |
||
475 | scroll.start_y = list.y; |
||
476 | scroll.size_y = list.h; |
||
477 | scrollbar_v_draw(#scroll); |
||
5980 | leency | 478 | }>> |