Text Reader: refactoring

git-svn-id: svn://kolibrios.org@5980 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Kirill Lipatov (Leency) 2015-12-17 15:48:28 +00:00
parent e6ee9c8f61
commit 791c882e25
3 changed files with 245 additions and 247 deletions

View File

@ -0,0 +1,19 @@
void DrawToolbarButton(char image_id, int x)
{
DefineButton(x, 5, TOOLBAR_ICON_WIDTH-1, TOOLBAR_ICON_HEIGHT-1, 10+image_id + BT_HIDE, 0);
img_draw stdcall(skin.image, x, 5, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, 0, image_id*TOOLBAR_ICON_HEIGHT);
}
void DrawScroller()
{
scroll.max_area = list.count;
scroll.cur_area = list.visible;
scroll.position = list.first;
scroll.all_redraw = 0;
scroll.start_x = list.x + list.w;
scroll.start_y = list.y;
scroll.size_y = list.h;
scroll.start_x = list.x + list.w;
scrollbar_v_draw(#scroll);
}

View File

@ -0,0 +1,70 @@
void PreparePage()
{
char line[4096]=0;
char char_width[255];
dword line_start;
byte ch;
dword bufoff;
dword line_length=30;
dword stroka_y = 5;
dword stroka=0;
int i, srch_pos;
font.changeSIZE();
list.w = Form.cwidth-scroll.size_x-1;
//get font chars width, need to increase performance
for (i=0; i<256; i++) char_width[i] = font.symbol_size(i);
//get font buffer height
for (bufoff=io.buffer_data; ESBYTE[bufoff]; bufoff++)
{
ch = ESBYTE[bufoff];
line_length += char_width[ch];
if (line_length>=list.w) || (ch==10) {
srch_pos = bufoff;
loop()
{
if (__isWhite(ESBYTE[srch_pos])) { bufoff=srch_pos+1; break; } //normal word-break
if (srch_pos == line_start) break; //no white space found in whole line
srch_pos--;
}
line_start = bufoff;
line_length = 30;
stroka++;
}
}
//draw text in buffer
list.count = stroka+2;
list.SetSizes(0, TOOLBAR_H, list.w, Form.cheight-TOOLBAR_H, font.size.text+1);
if (list.count < list.visible) list.count = list.visible;
font.size.height = list.count+1*list.item_h;
font.buffer_size = 0;
line_length = 30;
line_start = io.buffer_data;
for (bufoff=io.buffer_data; ESBYTE[bufoff]; bufoff++)
{
ch = ESBYTE[bufoff];
line_length += char_width[ch];
if (line_length>=list.w) || (ch==10)
{
//set word break
srch_pos = bufoff;
loop()
{
if (__isWhite(ESBYTE[srch_pos])) { bufoff=srch_pos+1; break; } //normal word-break
if (srch_pos == line_start) break; //no white space found in whole line
srch_pos--;
}
i = bufoff-line_start;
strlcpy(#line, line_start, i);
font.prepare_buf(8,stroka_y,list.w,font.size.height, #line);
stroka_y += list.item_h;
line_start = bufoff;
line_length = 30;
}
}
font.prepare_buf(8,stroka_y,list.w,font.size.height, line_start);
SmoothFont(font.buffer, font.size.width, font.size.height);
DrawPage();
}

View File

@ -15,17 +15,11 @@
#define TOOLBAR_ICON_WIDTH 26 #define TOOLBAR_ICON_WIDTH 26
#define TOOLBAR_ICON_HEIGHT 24 #define TOOLBAR_ICON_HEIGHT 24
#define TOOLBAR_OPEN_FILE_LEFT 8
#define TOOLBAR_MAGNIFY_PLUS_LEFT 42
#define TOOLBAR_MAGNIFY_MINUS_LEFT 67
#define TOOLBAR_CHANGE_ENCODING_LEFT 101
#define TOOLBAR_RUN_EDIT_LEFT 135
#define DEFAULT_FONT "/sys/fonts/Tahoma.kf" #define DEFAULT_FONT "/sys/fonts/Tahoma.kf"
#define DEFAULT_EDITOR "/sys/tinypad" #define DEFAULT_EDITOR "/sys/tinypad"
#define INTRO_TEXT "This is a plain text reader.\nTry to open some text file." #define INTRO_TEXT "This is a plain Text Reader.\nTry to open some text file."
#define VERSION "Text Reader v1.02" #define VERSION "Text Reader v1.03"
#define ABOUT "Idea: Leency, punk_joker #define ABOUT "Idea: Leency, punk_joker
Code: Leency, Veliant, KolibriOS Team Code: Leency, Veliant, KolibriOS Team
@ -49,10 +43,6 @@ char title[4196];
byte help_opened = false; byte help_opened = false;
char char_width[255];
dword line_offset;
#define DWORD 4;
enum { enum {
OPEN_FILE, OPEN_FILE,
MAGNIFY_MINUS, MAGNIFY_MINUS,
@ -64,6 +54,9 @@ enum {
#include "ini.h" #include "ini.h"
#include "menu.h" #include "menu.h"
#include "gui.h"
#include "prepare_page.h"
void InitDlls() void InitDlls()
{ {
@ -75,129 +68,6 @@ void InitDlls()
load_dll(Proc_lib, #OpenDialog_init,0); load_dll(Proc_lib, #OpenDialog_init,0);
} }
void EventShowInfo()
{
ShowAbout();
}
void EventOpenFile()
{
OpenDialog_start stdcall (#o_dialog);
OpenFile(#openfile_path);
PreparePage();
}
void EventMagnifyPlus()
{
font.size.text++;
if(!font.changeSIZE())
font.size.text--;
else
PreparePage();
}
void EventMagnifyMinus()
{
font.size.text--;
if(!font.changeSIZE())
font.size.text++;
else
PreparePage();
}
void EventRunEdit()
{
io.run(DEFAULT_EDITOR, #param);
}
void EventChangeEncoding()
{
CreateThread(#menu_rmb,#stak+4092);
}
void HandleMouseEvent()
{
mouse.get();
list.wheel_size = 7;
if (list.MouseScroll(mouse.vert)) {
DrawPage();
return;
}
scrollbar_v_mouse (#scroll);
if (list.first != scroll.position) {
list.first = scroll.position;
DrawPage();
}
}
void HandleKeyEvent()
{
if (help_opened) {
help_opened = false;
DrawPage();
return;
}
GetKeys();
if (key_scancode==059) {
EventShowInfo();
return;
}
if (key_modifier & KEY_LCTRL) || (key_modifier & KEY_RCTRL) {
switch (key_scancode)
{
case 024:
EventOpenFile();
break;
case SCAN_CODE_UP:
EventMagnifyPlus();
break;
case SCAN_CODE_DOWN:
EventMagnifyMinus();
break;
case 018:
EventRunEdit();
break;
case SCAN_CODE_TAB:
EventChangeEncoding();
break;
}
return;
}
if (list.ProcessKey(key_scancode))
DrawPage();
}
void HandleButtonEvent()
{
byte btn = GetButtonID();
if (btn==1) {
SaveIniSettings();
ExitProcess();
}
btn-=10;
switch(btn)
{
case OPEN_FILE:
EventOpenFile();
break;
case MAGNIFY_PLUS:
EventMagnifyPlus();
break;
case MAGNIFY_MINUS:
EventMagnifyMinus();
break;
case CHANGE_ENCODING:
EventChangeEncoding();
break;
case RUN_EDIT:
EventRunEdit();
break;
case SHOW_INFO:
EventShowInfo();
break;
}
}
void main() void main()
{ {
@ -247,132 +117,141 @@ void main()
} }
} }
void draw_window()
void HandleButtonEvent()
{ {
DefineAndDrawWindow(Form.left,Form.top,Form.width,Form.height,0x73,0,#title);
GetProcessInfo(#Form, SelfInfo);
if (Form.status_window>2) return;
if (Form.width < 200) { MoveSize(OLD,OLD,200,OLD); return; } byte btn = GetButtonID();
if (Form.height < 200) { MoveSize(OLD,OLD,OLD,200); return; } if (btn==1) {
SaveIniSettings();
DrawBar(0, 0, Form.cwidth, TOOLBAR_H - 1, 0xe1e1e1); ExitProcess();
DrawBar(0, TOOLBAR_H - 1, Form.cwidth, 1, 0x7F7F7F); }
btn-=10;
DrawToolbarButton(OPEN_FILE, TOOLBAR_OPEN_FILE_LEFT); switch(btn)
DrawToolbarButton(MAGNIFY_PLUS, TOOLBAR_MAGNIFY_PLUS_LEFT);
DrawToolbarButton(MAGNIFY_MINUS, TOOLBAR_MAGNIFY_MINUS_LEFT);
DrawToolbarButton(CHANGE_ENCODING, TOOLBAR_CHANGE_ENCODING_LEFT);
DrawToolbarButton(RUN_EDIT, TOOLBAR_RUN_EDIT_LEFT);
DrawToolbarButton(SHOW_INFO, Form.cwidth - 34);
if ((Form.cwidth-scroll.size_x-1 == list.w) &&
(Form.cheight-TOOLBAR_H == list.h) &&
(list.count)
)
{ {
case OPEN_FILE:
EventOpenFile();
break;
case MAGNIFY_PLUS:
EventMagnifyPlus();
break;
case MAGNIFY_MINUS:
EventMagnifyMinus();
break;
case CHANGE_ENCODING:
EventChangeEncoding();
break;
case RUN_EDIT:
EventRunEdit();
break;
case SHOW_INFO:
EventShowInfo();
break;
}
}
void HandleKeyEvent()
{
if (help_opened) {
help_opened = false;
DrawPage(); DrawPage();
} else { return;
PreparePage();
} }
DrawRectangle(scroll.start_x, scroll.start_y, scroll.size_x, scroll.size_y-1, scroll.bckg_col); GetKeys();
} if (key_scancode==059) {
EventShowInfo();
void DrawPage() return;
{
_PutImage(list.x,list.y,list.w,list.h,list.first*list.item_h*list.w*3 + font.buffer);
DrawScroller();
}
void PreparePage()
{
char line[4096]=0;
dword line_start;
byte ch;
dword bufoff;
dword line_length=30;
dword stroka_y = 5;
dword stroka=0;
int i, srch_pos;
font.changeSIZE();
list.w = Form.cwidth-scroll.size_x-1;
//get font chars width, need to increase performance
for (i=0; i<256; i++) char_width[i] = font.symbol_size(i);
//get font buffer height
for (bufoff=io.buffer_data; ESBYTE[bufoff]; bufoff++)
{
ch = ESBYTE[bufoff];
line_length += char_width[ch];
if (line_length>=list.w) || (ch==10) {
srch_pos = bufoff;
loop()
{
if (__isWhite(ESBYTE[srch_pos])) { bufoff=srch_pos+1; break; } //normal word-break
if (srch_pos == line_start) break; //no white space found in whole line
srch_pos--;
}
line_start = bufoff;
line_length = 30;
stroka++;
}
} }
//draw text in buffer if (key_modifier & KEY_LCTRL) || (key_modifier & KEY_RCTRL) {
list.count = stroka+2; switch (key_scancode)
list.SetSizes(0, TOOLBAR_H, list.w, Form.cheight-TOOLBAR_H, font.size.text+1);
if (list.count < list.visible) list.count = list.visible;
font.size.height = list.count+1*list.item_h;
font.buffer_size = 0;
line_length = 30;
line_start = io.buffer_data;
for (bufoff=io.buffer_data; ESBYTE[bufoff]; bufoff++)
{
ch = ESBYTE[bufoff];
line_length += char_width[ch];
if (line_length>=list.w) || (ch==10)
{ {
//set word break case 024:
srch_pos = bufoff; EventOpenFile();
loop() break;
{ case SCAN_CODE_UP:
if (__isWhite(ESBYTE[srch_pos])) { bufoff=srch_pos+1; break; } //normal word-break EventMagnifyPlus();
if (srch_pos == line_start) break; //no white space found in whole line break;
srch_pos--; case SCAN_CODE_DOWN:
} EventMagnifyMinus();
i = bufoff-line_start; break;
strlcpy(#line, line_start, i); case 018:
font.prepare_buf(8,stroka_y,list.w,font.size.height, #line); EventRunEdit();
stroka_y += list.item_h; break;
line_start = bufoff; case SCAN_CODE_TAB:
line_length = 30; EventChangeEncoding();
break;
} }
return;
} }
font.prepare_buf(8,stroka_y,list.w,font.size.height, line_start); if (list.ProcessKey(key_scancode))
SmoothFont(font.buffer, font.size.width, font.size.height); DrawPage();
DrawPage();
} }
void DrawToolbarButton(char image_id, int x)
void HandleMouseEvent()
{ {
DefineButton(x, 5, TOOLBAR_ICON_WIDTH-1, TOOLBAR_ICON_HEIGHT-1, 10+image_id + BT_HIDE, 0); mouse.get();
img_draw stdcall(skin.image, x, 5, TOOLBAR_ICON_WIDTH, TOOLBAR_ICON_HEIGHT, 0, image_id*TOOLBAR_ICON_HEIGHT); list.wheel_size = 7;
if (list.MouseScroll(mouse.vert)) {
DrawPage();
return;
}
scrollbar_v_mouse (#scroll);
if (list.first != scroll.position) {
list.first = scroll.position;
DrawPage();
}
} }
void DrawScroller()
/* ----------------------------------------------------- */
void EventOpenFile()
{ {
scroll.max_area = list.count; OpenDialog_start stdcall (#o_dialog);
scroll.cur_area = list.visible; OpenFile(#openfile_path);
scroll.position = list.first; PreparePage();
scroll.all_redraw = 0;
scroll.start_x = list.x + list.w;
scroll.start_y = list.y;
scroll.size_y = list.h;
scroll.start_x = list.x + list.w;
scrollbar_v_draw(#scroll);
} }
void EventMagnifyPlus()
{
font.size.text++;
if(!font.changeSIZE())
font.size.text--;
else
PreparePage();
}
void EventMagnifyMinus()
{
font.size.text--;
if(!font.changeSIZE())
font.size.text++;
else
PreparePage();
}
void EventRunEdit()
{
io.run(DEFAULT_EDITOR, #param);
}
void EventChangeEncoding()
{
CreateThread(#menu_rmb,#stak+4092);
}
void EventShowInfo() {
help_opened = true;
DrawBar(list.x, list.y, list.w, list.h, 0xFFFfff);
WriteText(list.x + 10, list.y + 10, 10000001b, 0x555555, VERSION);
WriteTextLines(list.x + 10, list.y+40, 10110000b, 0, ABOUT, 20);
}
/* ------------------------------------------- */
void OpenFile(dword f_path) void OpenFile(dword f_path)
{ {
int tmp; int tmp;
@ -392,9 +271,39 @@ void OpenFile(dword f_path)
list.ClearList(); list.ClearList();
} }
ShowAbout() { void draw_window()
help_opened = true; {
DrawBar(list.x, list.y, list.w, list.h, 0xFFFfff); DefineAndDrawWindow(Form.left,Form.top,Form.width,Form.height,0x73,0,#title);
WriteText(list.x + 10, list.y + 10, 10000001b, 0x555555, VERSION); GetProcessInfo(#Form, SelfInfo);
WriteTextLines(list.x + 10, list.y+40, 10110000b, 0, ABOUT, 20); if (Form.status_window>2) return;
if (Form.width < 200) { MoveSize(OLD,OLD,200,OLD); return; }
if (Form.height < 200) { MoveSize(OLD,OLD,OLD,200); return; }
DrawBar(0, 0, Form.cwidth, TOOLBAR_H - 1, 0xe1e1e1);
DrawBar(0, TOOLBAR_H - 1, Form.cwidth, 1, 0x7F7F7F);
DrawToolbarButton(OPEN_FILE, 8);
DrawToolbarButton(MAGNIFY_PLUS, 42);
DrawToolbarButton(MAGNIFY_MINUS, 67);
DrawToolbarButton(CHANGE_ENCODING, 101);
DrawToolbarButton(RUN_EDIT, 135);
DrawToolbarButton(SHOW_INFO, Form.cwidth - 34);
if ((Form.cwidth-scroll.size_x-1 == list.w) &&
(Form.cheight-TOOLBAR_H == list.h) &&
(list.count)
)
{
DrawPage();
} else {
PreparePage();
}
DrawRectangle(scroll.start_x, scroll.start_y, scroll.size_x, scroll.size_y-1, scroll.bckg_col);
}
void DrawPage()
{
_PutImage(list.x,list.y,list.w,list.h,list.first*list.item_h*list.w*3 + font.buffer);
DrawScroller();
} }