Subversion Repositories Kolibri OS

Rev

Rev 4636 | Rev 4644 | 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
4643 leency 33
	char version[]=" Текстовый браузер 0.99.7";
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
4643 leency 40
	char version[]=" Text-based Browser 0.99.7";
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;
4643 leency 81
	int bufpointer_len;
3067 leency 82
 
83
	mem_Init();
4536 leency 84
	CursorPointer.Load(#CursorFile);
3107 leency 85
	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 86
	if (load_dll2(libio, #libio_init,1)!=0) notify("Error: library doesn't exists - libio");
87
	if (load_dll2(libimg, #libimg_init,1)!=0) notify("Error: library doesn't exists - libimg");
4540 leency 88
	if (load_dll2(libHTTP, #http_lib_init,1)!=0) notify("Error: library doesn't exists - http");
3067 leency 89
 
4536 leency 90
	if (!URL) strcpy(#URL, "/sys/index.htm");
3067 leency 91
	Form.width=WIN_W;
92
	Form.height=WIN_H;
93
	SetElementSizes();
4416 leency 94
	OpenPage();
3067 leency 95
 
4536 leency 96
	SetEventMask(0xa7);
3067 leency 97
	loop()
98
	{
99
		WaitEventTimeout(2);
100
		switch(EAX & 0xFF)
101
		{
102
			CASE evMouse:
4081 leency 103
				if (!CheckActiveProcess(Form.ID)) break;
4636 leency 104
				//Edit URL
3466 leency 105
				edit_box_mouse stdcall (#address_box);
3067 leency 106
				m.get();
4636 leency 107
				//Links hover
4550 leency 108
				if (m.y>WB1.list.y) PageLinks.Hover(m.x, m.y, link_color_inactive, link_color_active, bg_color);
4636 leency 109
				//Menu
4540 leency 110
				if (m.y>WB1.list.y) && (m.y
3067 leency 111
				{
4026 leency 112
					if (m.pkm)
113
					{
114
						show_menu = 1;
115
					}
116
					if (!m.pkm) && (show_menu)
117
					{
118
						show_menu = 0;
119
						SwitchToAnotherThread();
4074 leency 120
						CreateThread(#menu_rmb,#stak+4092);
4026 leency 121
						break;
122
					}
3067 leency 123
				}
4636 leency 124
				//Mouse scroll
4415 leency 125
				if (m.vert)
3067 leency 126
				{
4636 leency 127
					if (WB1.list.MouseScroll(m.vert)) WB1.Parse();
3067 leency 128
				}
4636 leency 129
				//Drag scroller
3067 leency 130
				if (!m.lkm) scroll_used=0;
4416 leency 131
				if (m.x>=scroll_wv.start_x) && (m.x<=scroll_wv.start_x+scroll_wv.size_x)
132
				&& (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 133
				&& (WB1.list.count>WB1.list.visible) && (m.lkm)
4026 leency 134
				{
135
					scroll_used=1;
4636 leency 136
				}
3067 leency 137
				if (scroll_used)
138
				{
4415 leency 139
					half_scroll_size = WB1.list.h - 16 * WB1.list.visible / WB1.list.count - 3 /2;
4416 leency 140
					if (half_scroll_size+WB1.list.y>m.y) || (m.y<0) || (m.y>4000) m.y=half_scroll_size+WB1.list.y;
141
					btn=WB1.list.first;
4415 leency 142
					WB1.list.first = m.y -half_scroll_size -WB1.list.y * WB1.list.count / WB1.list.h;
143
					if (WB1.list.visible+WB1.list.first>WB1.list.count) WB1.list.first=WB1.list.count-WB1.list.visible;
4636 leency 144
					if (btn<>WB1.list.first) WB1.Parse();
3067 leency 145
				}
146
				break;
147
			case evButton:
148
				btn=GetButtonID();
4636 leency 149
				if (btn==1)	ExitProcess();
150
				Scan(btn);
3067 leency 151
				break;
152
			case evKey:
153
				key = GetKey();
154
 
4416 leency 155
				if (address_box.flags & 0b10) SWITCH(key)
3067 leency 156
					{ CASE 52: CASE 53: CASE 54: goto _EDIT_MARK; }
157
 
4413 leency 158
				Scan(key);
3067 leency 159
 
160
				_EDIT_MARK:
4416 leency 161
				if (key<>0x0d) && (key<>183) && (key<>184) {EAX=key<<8; edit_box_key stdcall(#address_box);}
3067 leency 162
				break;
163
			case evReDraw:
4636 leency 164
				if (action_buf) Scan(action_buf);
3067 leency 165
				Draw_Window();
166
				break;
4540 leency 167
 
4536 leency 168
			case evNetwork:
4540 leency 169
				if (http_transfer > 0) {
4536 leency 170
					http_process stdcall (http_transfer);
4554 leency 171
					if (EAX == 0) {
4558 hidnplayr 172
						ESI = http_transfer;
173
						// Handle redirects
4563 leency 174
						if (ESI.http_msg.status >= 300) && (ESI.http_msg.status < 400)
175
						{
4558 hidnplayr 176
							redirected++;
4563 leency 177
							if (redirected<=5)
178
							{
4558 hidnplayr 179
								http_find_header_field stdcall (http_transfer, #str_location);
180
								if (EAX!=0) {
181
									ESI = EAX;
182
									EDI = #URL;
183
									do {
184
										$lodsb;
185
										$stosb;
186
									} while (AL != 0) && (AL != 13) && (AL != 10));
187
									DSBYTE[EDI-1]='\0';
188
								}
4563 leency 189
							}
190
							else
191
							{
4558 hidnplayr 192
							//TODO: display error (too many redirects)
193
							}
4563 leency 194
						}
195
						else
196
						{
4558 hidnplayr 197
							redirected = 0;
198
						}
4554 leency 199
						// Loading the page is complete, free resources
4563 leency 200
						if (redirected>0)
201
						{
4643 leency 202
							http_free stdcall (http_transfer);
203
							http_transfer=0;
4558 hidnplayr 204
							WB1.GetNewUrl();
205
							strcpy(#editURL, #URL);
4563 leency 206
							BrowserHistory.current--;
4558 hidnplayr 207
							OpenPage();
4563 leency 208
						}
209
						else
210
						{
4643 leency 211
							if (o_bufpointer) o_bufpointer = free(o_bufpointer);
212
							ESI = http_transfer;
213
							bufpointer = ESI.http_msg.content_ptr;
214
							bufsize = ESI.http_msg.content_received;
215
							bufpointer_len = strlen(bufpointer);
216
							http_free stdcall (http_transfer);
217
							http_transfer=0;
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:
4643 leency 338
			if (http_transfer > 0)
339
			{
340
				StopLoading();
341
				Draw_Window();
342
			}
4636 leency 343
			else OpenPage();
4413 leency 344
			return;
4415 leency 345
		case 014:
346
		case 020:
4413 leency 347
		case NEWTAB:
348
			MoveSize(190,80,OLD,OLD);
349
			RunProgram(#program_path, #URL);
350
			return;
351
 
352
		case HOME:
4571 hidnplayr 353
			strcpy(#editURL, "http://kolibrios.org/");
4413 leency 354
		case GOTOURL:
355
		case 0x0D: //enter
356
			if ((strstr(#editURL,"ttp://")==0) && (editURL[0]!='/')) strcpy(#URL,"http://"); else URL[0] = 0;
357
			strcat(#URL, #editURL);
4416 leency 358
			OpenPage();
4413 leency 359
			return;
360
		case SEARCHWEB:
361
			strcpy(#URL, #search_path);
362
			strcat(#URL, #editURL);
4416 leency 363
			OpenPage();
4413 leency 364
			return;
365
 
366
		case 183: //PgDown
4415 leency 367
			if (WB1.list.count < WB1.list.visible) return;
368
			IF(WB1.list.first == WB1.list.count - WB1.list.visible) return;
369
			WB1.list.first += WB1.list.visible + 2;
370
			IF(WB1.list.visible + WB1.list.first > WB1.list.count) WB1.list.first = WB1.list.count - WB1.list.visible;
4636 leency 371
			WB1.Parse();
4415 leency 372
			return;
373
 
4413 leency 374
		case 184: //PgUp
4415 leency 375
			if (WB1.list.count < WB1.list.visible) return;
376
			IF(WB1.list.first == 0) return;
377
			WB1.list.first -= WB1.list.visible - 2;
378
			IF(WB1.list.first < 0) WB1.list.first = 0;
4636 leency 379
			WB1.Parse();
4415 leency 380
			return;
381
 
382
		case 178:
4544 leency 383
		case BTN_UP:
4416 leency 384
			if (WB1.list.first <= 0) return;
4415 leency 385
			WB1.list.first--;
4636 leency 386
			WB1.Parse();
4415 leency 387
			return;
388
 
389
		case 177:
4544 leency 390
		case BTN_DOWN:
4416 leency 391
			if (WB1.list.visible + WB1.list.first >= WB1.list.count) return;
4415 leency 392
			WB1.list.first++;
4636 leency 393
			WB1.Parse();
4415 leency 394
			return;
395
 
4413 leency 396
		case 180: //home
4636 leency 397
			if (WB1.list.KeyHome()) WB1.Parse();
4415 leency 398
			return;
399
 
4413 leency 400
		case 181: //end
4415 leency 401
			if (WB1.list.count < WB1.list.visible) return;
4636 leency 402
			if (WB1.list.KeyEnd()) WB1.Parse();
4413 leency 403
			return;
404
	}
405
}
406
 
407
 
408
 
4415 leency 409
void ProcessLinks(int id)
410
{
4544 leency 411
	strcpy(#URL, PageLinks.GetURL(id-401));
4417 leency 412
	//$1 - Condition Script
413
	if (URL[0] == '$')
414
	{
415
		if (URL[1]=='-') && (condition_href) condition_href--;
416
		if (URL[1]=='+')
417
		{
418
			if (condition_href
419
		}
420
		if (URL[1]!='-') && (URL[1]!='+') condition_href = atoi(#URL+1);
421
		strcpy(#URL, BrowserHistory.CurrentUrl());
4508 leency 422
		ShowPage();
4417 leency 423
		return;
424
	}
4415 leency 425
	//#1
426
	if (URL[0] == '#')
427
	{
4417 leency 428
		strcpy(#anchor, #URL+strrchr(#URL, '#'));
4415 leency 429
		strcpy(#URL, BrowserHistory.CurrentUrl());
430
		WB1.list.first=WB1.list.count-WB1.list.visible;
4508 leency 431
		ShowPage();
4415 leency 432
		return;
433
	}
434
	//liner.ru#1
4417 leency 435
	if (strrchr(#URL, '#')!=-1)
4415 leency 436
	{
437
		strcpy(#anchor, #URL+strrchr(#URL, '#'));
4416 leency 438
		URL[strrchr(#URL, '#')-1] = 0x00;
4415 leency 439
	}
440
 
441
	WB1.GetNewUrl();
442
 
443
	if (!strcmp(#URL + strlen(#URL) - 4, ".gif")) || (!strcmp(#URL + strlen(#URL) - 4, ".png")) || (!strcmp(#URL + strlen(#URL) - 4, ".jpg"))
444
	{
445
		//if (strstr(#URL,"http:"))
446
		RunProgram("/sys/media/kiv", #URL);
447
		strcpy(#editURL, BrowserHistory.CurrentUrl());
448
		strcpy(#URL, BrowserHistory.CurrentUrl());
449
		return;
450
	}
451
	if (!strcmpn(#URL,"mailto:", 7))
452
	{
453
		notify(#URL);
454
		strcpy(#editURL, BrowserHistory.CurrentUrl());
455
		strcpy(#URL, BrowserHistory.CurrentUrl());
456
		return;
457
	}
4416 leency 458
	OpenPage();
4415 leency 459
	return;
460
}
461
 
4636 leency 462
void StopLoading()
463
{
464
	if (http_transfer<>0)
465
	{
466
		EAX = http_transfer;
467
		EAX = EAX.http_msg.content_ptr;		// get pointer to data
468
		$push	EAX							// save it on the stack
469
		http_free stdcall (http_transfer);	// abort connection
470
		$pop	EAX
471
		mem_Free(EAX);						// free data
472
		http_transfer=0;
473
		bufsize = 0;
474
	}
475
	anchor_line_num=WB1.list.first;
476
	anchor[0]='|';
477
}
478
 
4416 leency 479
void OpenPage()
480
{
4636 leency 481
	if (http_transfer<>0) PutPaletteImage(#toolbar,200,42,0,0,8,#toolbar_pal);
482
	StopLoading();
4416 leency 483
	strcpy(#editURL, #URL);
484
	BrowserHistory.AddUrl();
485
	strcpy(#header, #version);
486
	pre_text =0;
4636 leency 487
	WB1.list.ClearList();
488
	if (strncmp(#URL,"http:",5)==0)
4416 leency 489
	{
4636 leency 490
		http_get stdcall (#URL, #accept_language);
491
		http_transfer = EAX;
492
		cur_encoding = _DEFAULT;
493
		IF (http_transfer < 0) notify("Error from HTTP lib");
4416 leency 494
		Draw_Window();
495
		return;
496
	}
4636 leency 497
	else
498
	{
499
		file_size stdcall (#URL);
500
		bufsize = EBX;
501
		if (!bufsize) return;
502
		mem_Free(bufpointer);
503
		cur_encoding = _DEFAULT;
504
		if (o_bufpointer) o_bufpointer = free(o_bufpointer);
505
		bufpointer = mem_Alloc(bufsize);
506
		ReadFile(0, bufsize, bufpointer, #URL);
507
	}
4508 leency 508
	ShowPage();
4416 leency 509
}
4415 leency 510
 
4508 leency 511
void ShowPage()
512
{
513
	address_box.size = address_box.pos = strlen(#editURL);
514
	address_box.offset=0;
515
	edit_box_draw stdcall(#address_box);
4415 leency 516
 
4544 leency 517
	if (strcmp(#URL, URL_HISTORY)==0) ShowHistory(); else
4540 leency 518
	if (!bufsize)
4508 leency 519
	{
520
		PageLinks.Clear();
4636 leency 521
		if (http_transfer<>0)
522
		{
523
			WB1.Prepare(#loading, sizeof(loading));
524
		}
4508 leency 525
		else
4636 leency 526
			WB1.Prepare(#page_not_found, sizeof(page_not_found));
4508 leency 527
	}
528
	else
4636 leency 529
		WB1.Parse();
4415 leency 530
 
4508 leency 531
	if (!header) strcpy(#header, #version);
532
	if (!strcmp(#version, #header)) DrawTitle(#header);
533
}
534
 
535
 
4544 leency 536
 
4636 leency 537
 
3067 leency 538
stop: