Subversion Repositories Kolibri OS

Rev

Rev 4571 | Rev 4643 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3067 leency 1
//HTML Viewer in C--
3987 leency 2
//Copyright 2007-2013 by Veliant & Leency
3107 leency 3
//Asper, lev, Lrz, Barsuk, Nable...
3067 leency 4
//home icon - rachel fu, GPL licence
5
 
4085 leency 6
#ifndef AUTOBUILD
7
	#include "lang.h--"
8
#endif
9
 
3107 leency 10
//libraries
3363 leency 11
#define MEMSIZE 0x100000
3067 leency 12
#include "..\lib\kolibri.h"
13
#include "..\lib\strings.h"
14
#include "..\lib\figures.h"
15
#include "..\lib\encoding.h"
16
#include "..\lib\file_system.h"
17
#include "..\lib\mem.h"
18
#include "..\lib\dll.h"
4508 leency 19
#include "..\lib\draw_buf.h"
20
#include "..\lib\list_box.h"
21
#include "..\lib\cursor.h"
22
 
3107 leency 23
//*.obj libraries
24
#include "..\lib\lib.obj\box_lib.h"
25
#include "..\lib\lib.obj\libio_lib.h"
26
#include "..\lib\lib.obj\libimg_lib.h"
4536 leency 27
#include "..\lib\lib.obj\http.h"
3107 leency 28
//images
3067 leency 29
#include "img\toolbar_icons.c"
30
#include "img\URLgoto.txt";
31
 
4026 leency 32
#ifdef LANG_RUS
4636 leency 33
	char version[]=" Текстовый браузер 0.99.69";
4416 leency 34
	?define IMAGES_CACHE_CLEARED "Кэш картинок очищен"
4417 leency 35
	?define T_LAST_SLIDE "Это последний слайд"
4550 leency 36
	char loading[] = "Загрузка страницы...
";
4544 leency 37
	unsigned char page_not_found[] = FROM "html\page_not_found_ru.htm";
4571 hidnplayr 38
	char accept_language[]= "Accept-Language: ru\n\0";
4026 leency 39
#else
4636 leency 40
	char version[]=" Text-based Browser 0.99.69";
4026 leency 41
	?define IMAGES_CACHE_CLEARED "Images cache cleared"
4417 leency 42
	?define T_LAST_SLIDE "This slide is the last"
4550 leency 43
	char loading[] = "Loading...
";
4544 leency 44
	unsigned char page_not_found[] = FROM "html\page_not_found_en.htm";
4571 hidnplayr 45
	char accept_language[]= "Accept-Language: en\n\0";
4026 leency 46
#endif
47
 
3067 leency 48
proc_info Form;
49
#define WIN_W 640
50
#define WIN_H 480
51
 
4416 leency 52
char search_path[]="http://nigma.ru/index.php?s=";
4558 hidnplayr 53
char str_location[]="location\0";
54
int redirected = 0;
3067 leency 55
 
4074 leency 56
char stak[4096];
3067 leency 57
mouse m;
4026 leency 58
int action_buf;
59
 
4565 leency 60
dword http_transfer = 0;
4537 leency 61
dword http_buffer;
4534 leency 62
 
4411 leency 63
#include "..\TWB\TWB.c"
64
#include "menu_rmb.h"
4636 leency 65
#include "history.h"
3067 leency 66
 
4534 leency 67
char editURL[sizeof(URL)];
68
int	mouse_twb;
69
edit_box address_box= {250,207,16,0xffffff,0x94AECE,0xffffff,0xffffff,0,sizeof(URL),#editURL,#mouse_twb,2,19,19};
70
 
4544 leency 71
#define URL_HISTORY "WebView://history"
4534 leency 72
 
4488 leency 73
enum { BACK=300, FORWARD, REFRESH, HOME, NEWTAB, GOTOURL, SEARCHWEB, INPUT_CH, INPUT_BT, BTN_UP, BTN_DOWN };
3067 leency 74
 
4488 leency 75
 
3067 leency 76
void main()
77
{
4081 leency 78
	int key, btn;
3067 leency 79
	int half_scroll_size;
4026 leency 80
	int scroll_used=0, show_menu;
3067 leency 81
 
82
	mem_Init();
4536 leency 83
	CursorPointer.Load(#CursorFile);
3107 leency 84
	if (load_dll2(boxlib, #box_lib_init,0)!=0) {notify("System Error: library doesn't exists /rd/1/lib/box_lib.obj"); ExitProcess();}
4536 leency 85
	if (load_dll2(libio, #libio_init,1)!=0) notify("Error: library doesn't exists - libio");
86
	if (load_dll2(libimg, #libimg_init,1)!=0) notify("Error: library doesn't exists - libimg");
4540 leency 87
	if (load_dll2(libHTTP, #http_lib_init,1)!=0) notify("Error: library doesn't exists - http");
3067 leency 88
 
4536 leency 89
	if (!URL) strcpy(#URL, "/sys/index.htm");
3067 leency 90
	Form.width=WIN_W;
91
	Form.height=WIN_H;
92
	SetElementSizes();
4416 leency 93
	OpenPage();
3067 leency 94
 
4536 leency 95
	SetEventMask(0xa7);
3067 leency 96
	loop()
97
	{
98
		WaitEventTimeout(2);
99
		switch(EAX & 0xFF)
100
		{
101
			CASE evMouse:
4081 leency 102
				if (!CheckActiveProcess(Form.ID)) break;
4636 leency 103
				//Edit URL
3466 leency 104
				edit_box_mouse stdcall (#address_box);
3067 leency 105
				m.get();
4636 leency 106
				//Links hover
4550 leency 107
				if (m.y>WB1.list.y) PageLinks.Hover(m.x, m.y, link_color_inactive, link_color_active, bg_color);
4636 leency 108
				//Menu
4540 leency 109
				if (m.y>WB1.list.y) && (m.y
3067 leency 110
				{
4026 leency 111
					if (m.pkm)
112
					{
113
						show_menu = 1;
114
					}
115
					if (!m.pkm) && (show_menu)
116
					{
117
						show_menu = 0;
118
						SwitchToAnotherThread();
4074 leency 119
						CreateThread(#menu_rmb,#stak+4092);
4026 leency 120
						break;
121
					}
3067 leency 122
				}
4636 leency 123
				//Mouse scroll
4415 leency 124
				if (m.vert)
3067 leency 125
				{
4636 leency 126
					if (WB1.list.MouseScroll(m.vert)) WB1.Parse();
3067 leency 127
				}
4636 leency 128
				//Drag scroller
3067 leency 129
				if (!m.lkm) scroll_used=0;
4416 leency 130
				if (m.x>=scroll_wv.start_x) && (m.x<=scroll_wv.start_x+scroll_wv.size_x)
131
				&& (m.y>=scroll_wv.start_y+scroll_wv.btn_height) && (-scroll_wv.btn_height+scroll_wv.start_y+scroll_wv.size_y>m.y)
4415 leency 132
				&& (WB1.list.count>WB1.list.visible) && (m.lkm)
4026 leency 133
				{
134
					scroll_used=1;
4636 leency 135
				}
3067 leency 136
				if (scroll_used)
137
				{
4415 leency 138
					half_scroll_size = WB1.list.h - 16 * WB1.list.visible / WB1.list.count - 3 /2;
4416 leency 139
					if (half_scroll_size+WB1.list.y>m.y) || (m.y<0) || (m.y>4000) m.y=half_scroll_size+WB1.list.y;
140
					btn=WB1.list.first;
4415 leency 141
					WB1.list.first = m.y -half_scroll_size -WB1.list.y * WB1.list.count / WB1.list.h;
142
					if (WB1.list.visible+WB1.list.first>WB1.list.count) WB1.list.first=WB1.list.count-WB1.list.visible;
4636 leency 143
					if (btn<>WB1.list.first) WB1.Parse();
3067 leency 144
				}
145
				break;
146
			case evButton:
147
				btn=GetButtonID();
4636 leency 148
				if (btn==1)	ExitProcess();
149
				Scan(btn);
3067 leency 150
				break;
151
			case evKey:
152
				key = GetKey();
153
 
4416 leency 154
				if (address_box.flags & 0b10) SWITCH(key)
3067 leency 155
					{ CASE 52: CASE 53: CASE 54: goto _EDIT_MARK; }
156
 
4413 leency 157
				Scan(key);
3067 leency 158
 
159
				_EDIT_MARK:
4416 leency 160
				if (key<>0x0d) && (key<>183) && (key<>184) {EAX=key<<8; edit_box_key stdcall(#address_box);}
3067 leency 161
				break;
162
			case evReDraw:
4636 leency 163
				if (action_buf) Scan(action_buf);
3067 leency 164
				Draw_Window();
165
				break;
4540 leency 166
 
4536 leency 167
			case evNetwork:
4540 leency 168
				if (http_transfer > 0) {
4536 leency 169
					http_process stdcall (http_transfer);
170
					$push EAX
171
					ESI = http_transfer;
4636 leency 172
					if (o_bufpointer) o_bufpointer = free(o_bufpointer);
4540 leency 173
					bufpointer = ESI.http_msg.content_ptr;
4554 leency 174
					bufsize = ESI.http_msg.content_received;
4636 leency 175
					WB1.Parse();
4554 leency 176
 
4536 leency 177
					$pop EAX
4554 leency 178
					if (EAX == 0) {
4558 hidnplayr 179
						ESI = http_transfer;
180
						// Handle redirects
4563 leency 181
						if (ESI.http_msg.status >= 300) && (ESI.http_msg.status < 400)
182
						{
4558 hidnplayr 183
							redirected++;
4563 leency 184
							if (redirected<=5)
185
							{
4558 hidnplayr 186
								http_find_header_field stdcall (http_transfer, #str_location);
187
								if (EAX!=0) {
188
									ESI = EAX;
189
									EDI = #URL;
190
									do {
191
										$lodsb;
192
										$stosb;
193
									} while (AL != 0) && (AL != 13) && (AL != 10));
194
									DSBYTE[EDI-1]='\0';
195
								}
4563 leency 196
							}
197
							else
198
							{
4558 hidnplayr 199
							//TODO: display error (too many redirects)
200
							}
4563 leency 201
						}
202
						else
203
						{
4558 hidnplayr 204
							redirected = 0;
205
						}
4554 leency 206
						// Loading the page is complete, free resources
4536 leency 207
						http_free stdcall (http_transfer);
4636 leency 208
						http_transfer=0;
4563 leency 209
						if (redirected>0)
210
						{
4558 hidnplayr 211
							WB1.GetNewUrl();
212
							strcpy(#editURL, #URL);
4563 leency 213
							BrowserHistory.current--;
4558 hidnplayr 214
							OpenPage();
4563 leency 215
						}
216
						else
217
						{
4558 hidnplayr 218
							Draw_Window();		// stop button => refresh button
219
						}
4536 leency 220
					}
221
				}
3067 leency 222
		}
223
	}
224
}
225
 
226
void SetElementSizes()
227
{
4416 leency 228
	address_box.width = Form.width - 266;
229
	WB1.list.SetSizes(0, 44, Form.width - 10 - scroll_wv.size_x, Form.cheight - 44, 0, 10);
4415 leency 230
	WB1.list.column_max = WB1.list.w - 30 / 6;
231
	WB1.list.visible = WB1.list.h - 3 / WB1.list.line_h - 2;
4475 leency 232
	WB1.DrawBuf.Init(WB1.list.x, WB1.list.y, WB1.list.w, WB1.list.h, WB1.list.line_h);
3067 leency 233
}
234
 
235
 
236
void Draw_Window()
237
{
238
	int j;
3466 leency 239
	DefineAndDrawWindow(215,100,WIN_W,WIN_H,0x73,0xE4DFE1,0,0);
3067 leency 240
 
241
	GetProcessInfo(#Form, SelfInfo);
3464 leency 242
	if (Form.status_window>2)
3067 leency 243
	{
244
		DrawTitle(#header);
245
		return;
246
	}
247
	if (Form.height<120) MoveSize(OLD,OLD,OLD,120);
248
	if (Form.width<280) MoveSize(OLD,OLD,280,OLD);
249
 
250
	PutPaletteImage(#toolbar,200,42,0,0,8,#toolbar_pal);
4636 leency 251
	if (http_transfer > 0) _PutImage(88,10, 24,24, #stop_btn);
3067 leency 252
 
3464 leency 253
	DrawBar(200,0,Form.cwidth-200,43,0xE4DFE1);
254
	DrawBar(0,42,Form.cwidth,1,0xE2DBDC);
255
	DrawBar(0,43,Form.cwidth,1,0xD2CED0);
3067 leency 256
	for (j=0; j<5; j++) DefineButton(j*37+11, 7, 29, 29, 300+j+BT_HIDE, 0xE4DFE1);
3464 leency 257
	_PutImage(Form.cwidth-48,14, 40,19, #URLgoto);
258
	DefineButton(Form.cwidth-28,15, 18, 16, GOTOURL+BT_HIDE, 0xE4DFE1);
259
	DefineButton(Form.cwidth-47,15, 17, 16, SEARCHWEB+BT_HIDE, 0xE4DFE1);
260
	DrawRectangle(205,14,Form.cwidth-205-49,18,0x94AECE); //around adress bar
261
	DrawRectangle(206,15,Form.cwidth-205-50,16,0xE4ECF3);
3067 leency 262
 
263
	SetElementSizes();
4508 leency 264
	ShowPage();
3464 leency 265
 
4416 leency 266
	DefineButton(scroll_wv.start_x+1, scroll_wv.start_y+1, 16, 16, BTN_UP+BT_HIDE, 0xE4DFE1);
267
	DefineButton(scroll_wv.start_x+1, scroll_wv.start_y+scroll_wv.size_y-18, 16, 16, BTN_DOWN+BT_HIDE, 0xE4DFE1);
3067 leency 268
}
269
 
270
 
4636 leency 271
void ChangeCharset(byte new_charset)
272
{
273
	BufEncode(new_charset);
274
	WB1.Parse();
275
}
276
 
4413 leency 277
void Scan(int id)
278
{
4636 leency 279
	action_buf=0;
280
	if (id >= 400)
281
	{
282
		ProcessLinks(id);
283
		return;
284
	}
4413 leency 285
	switch (id)
286
	{
287
		case 011: //Ctrk+K
4636 leency 288
			ChangeCharset(_KOI);
4415 leency 289
			return;
290
 
4413 leency 291
		case 021: //Ctrl+U
4636 leency 292
			ChangeCharset(_UTF);
4415 leency 293
			return;
294
 
4413 leency 295
		case 004: //Ctrl+D
4636 leency 296
			ChangeCharset(_DOS);
4415 leency 297
			return;
298
 
4533 leency 299
		case 005: //Win encoding
4636 leency 300
			ChangeCharset(_WIN);
4533 leency 301
			return;
302
 
303
		case 009: //free img cache
4491 leency 304
			ImgCache.Free();
4416 leency 305
			notify(IMAGES_CACHE_CLEARED);
4636 leency 306
			WB1.Parse();
4415 leency 307
			return;
308
 
4546 leency 309
		case 003: //history
4544 leency 310
			strcpy(#URL, URL_HISTORY);
311
			OpenPage();
312
			return;
313
 
4413 leency 314
		case BACK:
315
			if (!BrowserHistory.GoBack()) return;
4416 leency 316
			OpenPage();
4413 leency 317
			return;
318
		case FORWARD:
319
			if (!BrowserHistory.GoForward()) return;
4416 leency 320
			OpenPage();
4413 leency 321
			return;
322
		case 052:  //F3
4636 leency 323
			if (strncmp(#URL,"http:",5)==0)
324
			{
325
				WriteFile(bufsize, bufpointer, "/tmp0/1/webview.tmp");
326
				if (EAX==0) RunProgram("/rd/1/tinypad", "/tmp0/1/webview.tmp");
327
			}
328
			else
329
			{
330
				RunProgram("/rd/1/tinypad", #URL);
331
			}
4413 leency 332
			return;
333
		case 054: //F5
4636 leency 334
			IF(address_box.flags & 0b10) WB1.Parse();
4415 leency 335
			return;
336
 
4413 leency 337
		case REFRESH:
4636 leency 338
			if (http_transfer > 0) StopLoading();
339
			else OpenPage();
4413 leency 340
			return;
4415 leency 341
		case 014:
342
		case 020:
4413 leency 343
		case NEWTAB:
344
			MoveSize(190,80,OLD,OLD);
345
			RunProgram(#program_path, #URL);
346
			return;
347
 
348
		case HOME:
4571 hidnplayr 349
			strcpy(#editURL, "http://kolibrios.org/");
4413 leency 350
		case GOTOURL:
351
		case 0x0D: //enter
352
			if ((strstr(#editURL,"ttp://")==0) && (editURL[0]!='/')) strcpy(#URL,"http://"); else URL[0] = 0;
353
			strcat(#URL, #editURL);
4416 leency 354
			OpenPage();
4413 leency 355
			return;
356
		case SEARCHWEB:
357
			strcpy(#URL, #search_path);
358
			strcat(#URL, #editURL);
4416 leency 359
			OpenPage();
4413 leency 360
			return;
361
 
362
		case 183: //PgDown
4415 leency 363
			if (WB1.list.count < WB1.list.visible) return;
364
			IF(WB1.list.first == WB1.list.count - WB1.list.visible) return;
365
			WB1.list.first += WB1.list.visible + 2;
366
			IF(WB1.list.visible + WB1.list.first > WB1.list.count) WB1.list.first = WB1.list.count - WB1.list.visible;
4636 leency 367
			WB1.Parse();
4415 leency 368
			return;
369
 
4413 leency 370
		case 184: //PgUp
4415 leency 371
			if (WB1.list.count < WB1.list.visible) return;
372
			IF(WB1.list.first == 0) return;
373
			WB1.list.first -= WB1.list.visible - 2;
374
			IF(WB1.list.first < 0) WB1.list.first = 0;
4636 leency 375
			WB1.Parse();
4415 leency 376
			return;
377
 
378
		case 178:
4544 leency 379
		case BTN_UP:
4416 leency 380
			if (WB1.list.first <= 0) return;
4415 leency 381
			WB1.list.first--;
4636 leency 382
			WB1.Parse();
4415 leency 383
			return;
384
 
385
		case 177:
4544 leency 386
		case BTN_DOWN:
4416 leency 387
			if (WB1.list.visible + WB1.list.first >= WB1.list.count) return;
4415 leency 388
			WB1.list.first++;
4636 leency 389
			WB1.Parse();
4415 leency 390
			return;
391
 
4413 leency 392
		case 180: //home
4636 leency 393
			if (WB1.list.KeyHome()) WB1.Parse();
4415 leency 394
			return;
395
 
4413 leency 396
		case 181: //end
4415 leency 397
			if (WB1.list.count < WB1.list.visible) return;
4636 leency 398
			if (WB1.list.KeyEnd()) WB1.Parse();
4413 leency 399
			return;
400
	}
401
}
402
 
403
 
404
 
4415 leency 405
void ProcessLinks(int id)
406
{
4544 leency 407
	strcpy(#URL, PageLinks.GetURL(id-401));
4417 leency 408
	//$1 - Condition Script
409
	if (URL[0] == '$')
410
	{
411
		if (URL[1]=='-') && (condition_href) condition_href--;
412
		if (URL[1]=='+')
413
		{
414
			if (condition_href
415
		}
416
		if (URL[1]!='-') && (URL[1]!='+') condition_href = atoi(#URL+1);
417
		strcpy(#URL, BrowserHistory.CurrentUrl());
4508 leency 418
		ShowPage();
4417 leency 419
		return;
420
	}
4415 leency 421
	//#1
422
	if (URL[0] == '#')
423
	{
4417 leency 424
		strcpy(#anchor, #URL+strrchr(#URL, '#'));
4415 leency 425
		strcpy(#URL, BrowserHistory.CurrentUrl());
426
		WB1.list.first=WB1.list.count-WB1.list.visible;
4508 leency 427
		ShowPage();
4415 leency 428
		return;
429
	}
430
	//liner.ru#1
4417 leency 431
	if (strrchr(#URL, '#')!=-1)
4415 leency 432
	{
433
		strcpy(#anchor, #URL+strrchr(#URL, '#'));
4416 leency 434
		URL[strrchr(#URL, '#')-1] = 0x00;
4415 leency 435
	}
436
 
437
	WB1.GetNewUrl();
438
 
439
	if (!strcmp(#URL + strlen(#URL) - 4, ".gif")) || (!strcmp(#URL + strlen(#URL) - 4, ".png")) || (!strcmp(#URL + strlen(#URL) - 4, ".jpg"))
440
	{
441
		//if (strstr(#URL,"http:"))
442
		RunProgram("/sys/media/kiv", #URL);
443
		strcpy(#editURL, BrowserHistory.CurrentUrl());
444
		strcpy(#URL, BrowserHistory.CurrentUrl());
445
		return;
446
	}
447
	if (!strcmpn(#URL,"mailto:", 7))
448
	{
449
		notify(#URL);
450
		strcpy(#editURL, BrowserHistory.CurrentUrl());
451
		strcpy(#URL, BrowserHistory.CurrentUrl());
452
		return;
453
	}
4416 leency 454
	OpenPage();
4415 leency 455
	return;
456
}
457
 
4636 leency 458
void StopLoading()
459
{
460
	if (http_transfer<>0)
461
	{
462
		EAX = http_transfer;
463
		EAX = EAX.http_msg.content_ptr;		// get pointer to data
464
		$push	EAX							// save it on the stack
465
		http_free stdcall (http_transfer);	// abort connection
466
		$pop	EAX
467
		mem_Free(EAX);						// free data
468
		http_transfer=0;
469
		bufsize = 0;
470
	}
471
	anchor_line_num=WB1.list.first;
472
	anchor[0]='|';
473
}
474
 
4416 leency 475
void OpenPage()
476
{
4636 leency 477
	if (http_transfer<>0) PutPaletteImage(#toolbar,200,42,0,0,8,#toolbar_pal);
478
	StopLoading();
4416 leency 479
	strcpy(#editURL, #URL);
480
	BrowserHistory.AddUrl();
481
	strcpy(#header, #version);
482
	pre_text =0;
4636 leency 483
	WB1.list.ClearList();
484
	if (strncmp(#URL,"http:",5)==0)
4416 leency 485
	{
4636 leency 486
		http_get stdcall (#URL, #accept_language);
487
		http_transfer = EAX;
488
		cur_encoding = _DEFAULT;
489
		IF (http_transfer < 0) notify("Error from HTTP lib");
4416 leency 490
		Draw_Window();
491
		return;
492
	}
4636 leency 493
	else
494
	{
495
		file_size stdcall (#URL);
496
		bufsize = EBX;
497
		if (!bufsize) return;
498
		mem_Free(bufpointer);
499
		cur_encoding = _DEFAULT;
500
		if (o_bufpointer) o_bufpointer = free(o_bufpointer);
501
		bufpointer = mem_Alloc(bufsize);
502
		ReadFile(0, bufsize, bufpointer, #URL);
503
	}
4508 leency 504
	ShowPage();
4416 leency 505
}
4415 leency 506
 
4508 leency 507
void ShowPage()
508
{
509
	address_box.size = address_box.pos = strlen(#editURL);
510
	address_box.offset=0;
511
	edit_box_draw stdcall(#address_box);
4415 leency 512
 
4544 leency 513
	if (strcmp(#URL, URL_HISTORY)==0) ShowHistory(); else
4540 leency 514
	if (!bufsize)
4508 leency 515
	{
516
		PageLinks.Clear();
4636 leency 517
		if (http_transfer<>0)
518
		{
519
			WB1.Prepare(#loading, sizeof(loading));
520
		}
4508 leency 521
		else
4636 leency 522
			WB1.Prepare(#page_not_found, sizeof(page_not_found));
4508 leency 523
	}
524
	else
4636 leency 525
		WB1.Parse();
4415 leency 526
 
4508 leency 527
	if (!header) strcpy(#header, #version);
528
	if (!strcmp(#version, #header)) DrawTitle(#header);
529
}
530
 
531
 
4544 leency 532
 
4636 leency 533
 
3067 leency 534
stop: