Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
7739 leency 1
//Copyright 2007-2020 by Veliant & Leency
6978 leency 2
//Asper, lev, Lrz, Barsuk, Nable, hidnplayr...
3067 leency 3
 
7842 leency 4
//Licence restriction: compiling this app for WIN32 is forbidden.
5
 
4085 leency 6
#ifndef AUTOBUILD
7
	#include "lang.h--"
8
#endif
9
 
3107 leency 10
//libraries
7771 leency 11
#define MEMSIZE 1024 * 1000
5499 leency 12
#include "..\lib\gui.h"
4508 leency 13
#include "..\lib\draw_buf.h"
14
#include "..\lib\list_box.h"
15
#include "..\lib\cursor.h"
5978 leency 16
#include "..\lib\collection.h"
6795 leency 17
#include "..\lib\random.h"
7037 leency 18
#include "..\lib\clipboard.h"
5981 leency 19
 
7437 leency 20
// *.obj libraries
5499 leency 21
#include "..\lib\obj\box_lib.h"
7049 leency 22
#include "..\lib\obj\libio.h"
23
#include "..\lib\obj\libimg.h"
5499 leency 24
#include "..\lib\obj\http.h"
5690 leency 25
#include "..\lib\obj\iconv.h"
7748 leency 26
#include "..\lib\obj\proc_lib.h"
7757 leency 27
 
5408 leency 28
//useful patterns
5978 leency 29
#include "..\lib\patterns\history.h"
6058 leency 30
#include "..\lib\patterns\http_downloader.h"
7748 leency 31
#include "..\lib\patterns\simple_open_dialog.h"
7759 leency 32
#include "..\lib\patterns\toolbar_button.h"
7914 leency 33
#include "..\lib\patterns\restart_process.h"
5408 leency 34
 
7771 leency 35
#include "texts.h"
36
#include "cache.h"
7757 leency 37
#include "show_src.h"
38
#include "download_manager.h"
7771 leency 39
 
7757 leency 40
bool debug_mode = false;
7771 leency 41
 
7791 leency 42
enum {
43
	NEW_TAB=600,
44
	ENCODINGS=700,
45
	BACK_BUTTON=800,
46
	FORWARD_BUTTON,
47
	REFRESH_BUTTON,
48
	GOTOURL_BUTTON,
49
	CHANGE_ENCODING,
50
	SANDWICH_BUTTON,
51
	VIEW_SOURCE,
52
	EDIT_SOURCE,
53
	OPEN_FILE,
54
	NEW_WINDOW,
55
	VIEW_HISTORY,
56
	DOWNLOAD_MANAGER,
57
	CLEAR_CACHE,
58
	UPDATE_BROWSER,
59
	IN_NEW_TAB,
60
	IN_NEW_WINDOW,
61
	COPY_LINK_URL,
62
	DOWNLOAD_LINK_CONTENTS,
63
	TAB_ID,
64
	TAB_CLOSE_ID = 900
65
};
66
 
7970 leency 67
enum { TARGET_SAME_TAB, TARGET_NEW_WINDOW, TARGET_NEW_TAB };
68
 
7759 leency 69
#include "..\TWB\TWB.c" //HTML Parser, a core component
7757 leency 70
 
7759 leency 71
TWebBrowser WB1;
7771 leency 72
_history history;
7758 leency 73
 
7771 leency 74
#include "history.h"
7752 leency 75
 
7758 leency 76
#define PADDING 9
77
#define TSZE 25
7771 leency 78
#define STATUSBAR_H 15
79
#define TAB_H 20
7758 leency 80
dword TOOLBAR_H = PADDING+TSZE+PADDING+2;
4677 leency 81
 
7771 leency 82
#define URL_SIZE 4000
83
 
7750 leency 84
int action_buf;
7425 leency 85
 
7759 leency 86
_http http = 0;
7758 leency 87
 
7750 leency 88
bool source_mode = false;
7282 leency 89
 
5718 leency 90
progress_bar wv_progress_bar;
7750 leency 91
char stak[4096];
92
proc_info Form;
7748 leency 93
 
7780 leency 94
int menu_id=NULL;
95
 
7771 leency 96
#include "tabs.h"
97
 
7748 leency 98
char default_dir[] = "/rd/1";
7788 leency 99
od_filter filter2 = { 22, "TXT\0HTM\0HTML\0DOCX\0\0" };
7748 leency 100
 
7755 leency 101
char editURL[URL_SIZE+1];
7758 leency 102
edit_box address_box = {, PADDING+TSZE*2+PADDING+6, PADDING+3, 0xffffff,
103
	0x94AECE, 0xffffff, 0xffffff,0x10000000,URL_SIZE-2,#editURL,0,,19,19};
4534 leency 104
 
7784 leency 105
char editbox_icons[] = FROM "editbox_icons.raw";
4534 leency 106
 
7914 leency 107
dword shared_url;
7784 leency 108
 
7748 leency 109
void LoadLibraries()
110
{
111
	load_dll(boxlib,    #box_lib_init,0);
112
	load_dll(libio,     #libio_init,1);
113
	load_dll(libimg,    #libimg_init,1);
114
	load_dll(libHTTP,   #http_lib_init,1);
115
	load_dll(iconv_lib, #iconv_open,0);
116
	load_dll(Proc_lib,  #OpenDialog_init,0);
117
	OpenDialog_init stdcall (#o_dialog);
118
}
119
 
120
void HandleParam()
121
{
122
	if (param) {
7765 leency 123
		if (!strncmp(#param, "-download_and_exit ", 19)) {
124
			download_and_exit = true;
125
			strcpy(#downloader_edit, #param+19);
126
			Downloader();
7748 leency 127
			ExitProcess();
7765 leency 128
		} else if (!strncmp(#param, "-download ", 10)) {
129
			strcpy(#downloader_edit, #param+10);
130
			Downloader();
131
			ExitProcess();
132
		} else if (!strncmp(#param, "-source ", 8)) {
7750 leency 133
			source_mode = true;
7765 leency 134
			history.add(#param + 8);
7936 leency 135
		} else if (!strncmp(#param, "-new ", 5)) {
136
			history.add(#param + 5);
7748 leency 137
		} else {
7914 leency 138
			if (GetProcessesCount("WEBVIEW") == 1) {
139
				history.add(#param);
140
			} else {
141
				shared_url = memopen(#webview_shared, URL_SIZE+1, SHM_OPEN + SHM_WRITE);
142
				strncpy(shared_url, #param, URL_SIZE);
143
				ExitProcess();
144
			}
7748 leency 145
		}
146
	} else {
7755 leency 147
		history.add(URL_SERVICE_HOMEPAGE);
7748 leency 148
	}
7914 leency 149
	shared_url = memopen(#webview_shared, URL_SIZE+1, SHM_CREATE + SHM_WRITE);
7920 leency 150
	ESDWORD[shared_url] = '\0';
7748 leency 151
}
152
 
3067 leency 153
void main()
154
{
7970 leency 155
	int i, redirect_count=0;
7748 leency 156
	LoadLibraries();
7762 leency 157
	CreateDir("/tmp0/1/Downloads");
7764 leency 158
	//CreateDir("/tmp0/1/WebView_Cache");
7748 leency 159
	HandleParam();
6045 leency 160
	WB1.list.SetFont(8, 14, 10011000b);
5779 leency 161
	WB1.list.no_selection = true;
7759 leency 162
	WB1.custom_encoding = -1;
6978 leency 163
	SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER + EVM_STACK);
7914 leency 164
	loop() switch(@WaitEventTimeout(30))
6978 leency 165
	{
166
		case evMouse:
167
			edit_box_mouse stdcall (#address_box);
168
			mouse.get();
7970 leency 169
 
7282 leency 170
			if (WB1.list.MouseScroll(mouse.vert)) WB1.DrawPage();
7970 leency 171
 
6978 leency 172
			scrollbar_v_mouse (#scroll_wv);
7970 leency 173
			if (scroll_wv.delta) {
6978 leency 174
				WB1.list.first = scroll_wv.position;
175
				WB1.DrawPage();
3067 leency 176
				break;
6978 leency 177
			}
7970 leency 178
 
179
			if (links.hover(WB1.list.y, WB1.list.first))
7758 leency 180
			{
7970 leency 181
				if (mouse.mkm) {
182
					if (key_modifier&KEY_LSHIFT) || (key_modifier&KEY_RSHIFT) {
183
						EventClickLink(TARGET_NEW_WINDOW);
184
					} else {
185
						EventClickLink(TARGET_NEW_TAB);
186
					}
187
				}
188
				if (mouse.lkm) {
189
					CursorPointer.Restore();
190
					EventClickLink(TARGET_SAME_TAB);
191
				}
192
				if (mouse.pkm) {
193
					CursorPointer.Restore();
194
					EventShowLinkMenu();
195
				}
196
			} else {
197
				CursorPointer.Restore();
198
				if (mouse.pkm) && (WB1.list.MouseOver(mouse.x, mouse.y)) {
199
					EventShowPageMenu();
200
				}
7758 leency 201
			}
6978 leency 202
			break;
5711 leency 203
 
6978 leency 204
		case evButton:
7970 leency 205
			ProcessEvent( GetButtonID() );
6978 leency 206
			break;
5711 leency 207
 
6978 leency 208
		case evKey:
209
			GetKeys();
7771 leency 210
 
211
			if (key_modifier&KEY_LSHIFT) || (key_modifier&KEY_RSHIFT) {
212
				if (key_scancode == SCAN_CODE_TAB) {EventActivatePreviousTab();break;}
213
			}
214
 
7772 leency 215
			if (ProcessCtrlKeyEvent()) break;
7743 leency 216
 
217
			if (key_scancode == SCAN_CODE_F5) ProcessEvent(REFRESH_BUTTON);
218
 
7506 leency 219
			if (address_box.flags & ed_focus)
6978 leency 220
			{
7742 leency 221
				if (key_scancode == SCAN_CODE_ENTER) {
222
					ProcessEvent(key_scancode);
7771 leency 223
				} else {
6978 leency 224
					EAX = key_editbox;
225
					edit_box_key stdcall(#address_box);
5530 leency 226
				}
7771 leency 227
			} else {
7422 leency 228
				#define KEY_SCROLL_N 11
229
				if (SCAN_CODE_UP   == key_scancode) for (i=0;i
230
				if (SCAN_CODE_DOWN == key_scancode) for (i=0;i
7743 leency 231
				if (key_scancode == SCAN_CODE_F6) {address_box.flags=ed_focus; DrawOmnibox();}
6978 leency 232
				if (WB1.list.ProcessKey(key_scancode)) WB1.DrawPage();
233
				else ProcessEvent(key_scancode);
234
			}
235
			break;
236
 
237
		case evReDraw:
7780 leency 238
			DefineAndDrawWindow(GetScreenWidth()-800/2-random(80), //40
7755 leency 239
				GetScreenHeight()-700/2-random(80),800,700,0x73,0,0,0);
6978 leency 240
			GetProcessInfo(#Form, SelfInfo);
7780 leency 241
			ProcessMenuClick();
7806 leency 242
			sc.get();
7759 leency 243
			if (Form.status_window>2) break;
6978 leency 244
			if (Form.height<120) { MoveSize(OLD,OLD,OLD,120); break; }
245
			if (Form.width<280) { MoveSize(OLD,OLD,280,OLD); break; }
7748 leency 246
			draw_window();
6978 leency 247
			break;
248
 
249
		case evNetwork:
7758 leency 250
			if (http.transfer <= 0) break;
251
			http.receive();
252
			EventUpdateProgressBar();
253
			if (http.receive_result != 0) break;
254
			if (http.status_code >= 300) && (http.status_code < 400)
255
			{
256
				// Handle redirects
257
				if (redirect_count<=5) {
258
					redirect_count++;
259
					http.handle_redirect();
7282 leency 260
					http.free();
7758 leency 261
					GetAbsoluteURL(#http.redirect_url, history.current());
262
					history.back();
263
					OpenPage(#http.redirect_url);
264
				} else {
265
					notify("'Too many redirects.' -E");
266
					StopLoading();
7791 leency 267
					redirect_count = 0;
4536 leency 268
				}
7758 leency 269
			} else {
270
				// Loading the page is complete, free resources
271
				redirect_count = 0;
272
				http.free();
7764 leency 273
				pages_cache.add(history.current(), http.content_pointer, http.content_received);
7758 leency 274
				LoadInternalPage(http.content_pointer, http.content_received);
6978 leency 275
			}
7914 leency 276
			break;
277
		default:
278
			if (ESDWORD[shared_url] != '\0') {
279
				EventOpenNewTab(shared_url);
280
				ESDWORD[shared_url] = '\0';
281
				ActivateWindow(GetProcessSlot(Form.ID));
282
			}
6978 leency 283
	}
3067 leency 284
}
285
 
7772 leency 286
bool ProcessCtrlKeyEvent()
287
{
288
	if (key_modifier&KEY_LCTRL) || (key_modifier&KEY_RCTRL) switch(key_scancode)
289
	{
290
		case SCAN_CODE_KEY_O:
291
			EventOpenDialog();
292
			return true;
293
		case SCAN_CODE_KEY_H:
294
			ProcessEvent(VIEW_HISTORY);
295
			return true;
296
		case SCAN_CODE_KEY_U:
297
			EventViewSource();
298
			return true;
299
		case SCAN_CODE_KEY_T:
300
			EventOpenNewTab(URL_SERVICE_HOMEPAGE);
301
			return true;
302
		case SCAN_CODE_KEY_N:
303
			RunProgram(#program_path, NULL);
304
			return true;
305
		case SCAN_CODE_KEY_J:
306
			ProcessEvent(DOWNLOAD_MANAGER);
307
			return true;
308
		case SCAN_CODE_KEY_R:
309
			ProcessEvent(REFRESH_BUTTON);
310
			return true;
311
		case SCAN_CODE_ENTER:
312
			EventSeachWeb();
313
			return true;
314
		case SCAN_CODE_LEFT:
315
			 ProcessEvent(BACK_BUTTON);
316
			 return true;
317
		case SCAN_CODE_RIGHT:
318
			ProcessEvent(FORWARD_BUTTON);
319
			return true;
320
		case SCAN_CODE_KEY_W:
321
			EventCloseActiveTab();
322
			return true;
323
		case SCAN_CODE_TAB:
324
			EventActivateNextTab();
325
			return true;
326
	}
327
	return false;
328
}
329
 
3067 leency 330
void SetElementSizes()
331
{
7758 leency 332
	address_box.width = Form.cwidth - address_box.left - 52 - 16;
7771 leency 333
	WB1.list.SetSizes(0, TOOLBAR_H+TAB_H, Form.width - 10 - scroll_wv.size_x,
334
		Form.cheight - TOOLBAR_H - STATUSBAR_H - TAB_H, BASIC_LINE_H);
7759 leency 335
	WB1.list.wheel_size = 7 * BASIC_LINE_H;
7746 leency 336
	WB1.list.column_max = WB1.list.w - scroll_wv.size_x / WB1.list.font_w + 1;
6794 leency 337
	WB1.list.visible = WB1.list.h;
3067 leency 338
}
339
 
5981 leency 340
 
7748 leency 341
void draw_window()
3067 leency 342
{
7758 leency 343
	int i;
7910 leency 344
	bool burger_active = false;
345
	if (menu_id == OPEN_FILE) burger_active = true;
346
 
3067 leency 347
	SetElementSizes();
7758 leency 348
 
7806 leency 349
	DrawBar(0,0, Form.cwidth,PADDING, sc.work);
350
	DrawBar(0,PADDING+TSZE+1, Form.cwidth,PADDING-1, sc.work);
351
	DrawBar(0,TOOLBAR_H-2, Form.cwidth,1, MixColors(sc.work_dark, sc.work, 180));
352
	DrawBar(0,TOOLBAR_H-1, Form.cwidth,1, sc.work_graph);
353
	DrawBar(0, PADDING, address_box.left-2, TSZE+1, sc.work);
354
	DrawBar(address_box.left+address_box.width+18, PADDING, Form.cwidth-address_box.left-address_box.width-18, TSZE+1, sc.work);
7758 leency 355
 
7910 leency 356
	DrawTopPanelButton(BACK_BUTTON, PADDING-1, PADDING, 30, false);
357
	DrawTopPanelButton(FORWARD_BUTTON, PADDING+TSZE+PADDING-2, PADDING, 31, false);
358
	DrawTopPanelButton(SANDWICH_BUTTON, Form.cwidth-PADDING-TSZE-3, PADDING, -1, burger_active); //burger menu
7758 leency 359
 
7806 leency 360
	DrawBar(0,Form.cheight - STATUSBAR_H, Form.cwidth,1, sc.work_graph);
7758 leency 361
 
362
	DrawRectangle(WB1.list.x + WB1.list.w, WB1.list.y, scroll_wv.size_x,
363
		WB1.list.h-1, scroll_wv.bckg_col);
364
 
7771 leency 365
	if (!BrowserWidthChanged()) {
366
		WB1.DrawPage();
367
		DrawOmnibox();
368
	}
369
	DrawProgress();
7970 leency 370
	DrawStatusBar();
7771 leency 371
	DrawTabsBar();
372
}
373
 
374
bool BrowserWidthChanged()
375
{
376
	dword source_mode_holder;
377
	if (WB1.list.w!=DrawBuf.bufw) {
378
		DrawBuf.Init(WB1.list.x, WB1.list.y, WB1.list.w, 400*20);
7758 leency 379
		if (!strncmp(history.current(),"http",4)) {
7759 leency 380
			//nihuya ne izyashnoe reshenie, no pust' poka butet tak
7771 leency 381
			source_mode_holder = source_mode;
7758 leency 382
			LoadInternalPage(#loading_text, sizeof(loading_text));
7771 leency 383
			source_mode = source_mode_holder;
7758 leency 384
		}
385
		OpenPage(history.current());
7771 leency 386
		return true;
7282 leency 387
	}
7771 leency 388
	return false;
3067 leency 389
}
390
 
7771 leency 391
 
7759 leency 392
void EventChangeEncodingAndLoadPage(int _new_encoding)
393
{
394
	dword newbuf, newsize;
395
	WB1.custom_encoding = _new_encoding;
396
	newsize = strlen(WB1.o_bufpointer);
397
	newbuf = malloc(newsize);
398
	memmov(newbuf, WB1.o_bufpointer, newsize);
399
	LoadInternalPage(newbuf, newsize);
400
	free(newbuf);
401
}
3067 leency 402
 
7759 leency 403
 
6698 leency 404
void ProcessEvent(dword id__)
4413 leency 405
{
6698 leency 406
	switch (id__)
4413 leency 407
	{
7970 leency 408
		case 1:
409
			ExitProcess();
410
			break;
7759 leency 411
		case ENCODINGS...ENCODINGS+6:
412
			EventChangeEncodingAndLoadPage(id__-ENCODINGS);
413
			return;
7758 leency 414
		case NEW_WINDOW:
415
			RunProgram(#program_path, NULL);
6698 leency 416
			return;
5711 leency 417
		case SCAN_CODE_BS:
5718 leency 418
		case BACK_BUTTON:
6021 leency 419
			if (history.back()) {
7755 leency 420
				OpenPage(history.current());
5978 leency 421
			}
5711 leency 422
			return;
5718 leency 423
		case FORWARD_BUTTON:
6021 leency 424
			if (history.forward()) {
7755 leency 425
				OpenPage(history.current());
5978 leency 426
			}
5711 leency 427
			return;
5718 leency 428
		case GOTOURL_BUTTON:
5772 leency 429
		case SCAN_CODE_ENTER:
7742 leency 430
			EventSubmitOmnibox();
5711 leency 431
			return;
5718 leency 432
		case REFRESH_BUTTON:
7893 leency 433
			EventRefreshPage();
5713 leency 434
			return;
7759 leency 435
		case CHANGE_ENCODING:
7770 leency 436
			EventShowEncodingsList();
7759 leency 437
			return;
5718 leency 438
		case SANDWICH_BUTTON:
7770 leency 439
			EventShowMainMenu();
4415 leency 440
			return;
5718 leency 441
		case VIEW_SOURCE:
7750 leency 442
			EventViewSource();
5718 leency 443
			break;
444
		case EDIT_SOURCE:
7755 leency 445
			if (check_is_the_adress_local(history.current())) {
446
				RunProgram("/rd/1/tinypad", history.current());
7750 leency 447
			} else {
7759 leency 448
				CreateFile(WB1.bufsize, WB1.bufpointer, "/tmp0/1/WebView_tmp.htm");
5718 leency 449
				if (!EAX) RunProgram("/rd/1/tinypad", "/tmp0/1/WebView_tmp.htm");
450
			}
4415 leency 451
			return;
5718 leency 452
		case VIEW_HISTORY:
7755 leency 453
			OpenPage(URL_SERVICE_HISTORY);
4544 leency 454
			return;
5718 leency 455
		case DOWNLOAD_MANAGER:
5519 leency 456
			if (!downloader_opened) {
6001 leency 457
				downloader_edit = NULL;
5519 leency 458
				CreateThread(#Downloader,#downloader_stak+4092);
459
			}
4413 leency 460
			return;
7765 leency 461
		case UPDATE_BROWSER:
462
			EventUpdateBrowser();
463
			return;
7770 leency 464
		case CLEAR_CACHE:
465
			pages_cache.clear();
466
			notify(#clear_cache_ok);
7893 leency 467
			EventRefreshPage();
7770 leency 468
			return;
7789 simsanutiy 469
		case IN_NEW_TAB:
7970 leency 470
			EventClickLink(TARGET_NEW_TAB);
7789 simsanutiy 471
			return;
472
		case IN_NEW_WINDOW:
7970 leency 473
			EventClickLink(TARGET_NEW_WINDOW);
7789 simsanutiy 474
			return;
7282 leency 475
		case COPY_LINK_URL:
7970 leency 476
			Clipboard__CopyText(GetAbsoluteActiveURL());
7037 leency 477
			notify("'URL copied to clipboard'O");
478
			return;
7208 leency 479
		case DOWNLOAD_LINK_CONTENTS:
480
			if (!downloader_opened) {
7970 leency 481
				strcpy(#downloader_edit, GetAbsoluteActiveURL());
7208 leency 482
				CreateThread(#Downloader,#downloader_stak+4092);
483
			}
484
			return;
7758 leency 485
		case OPEN_FILE:
486
			EventOpenDialog();
487
			return;
7282 leency 488
		case SCAN_CODE_F12:
489
			debug_mode ^= 1;
490
			if (debug_mode) notify("'Debug mode ON'-I");
491
			else notify("'Debug mode OFF'-I");
492
			return;
7771 leency 493
		case NEW_TAB:
494
			if (http.transfer) break;
495
			EventOpenNewTab(URL_SERVICE_HOMEPAGE);
496
			return;
497
		case TAB_ID...TAB_ID+TABS_MAX:
498
			if (http.transfer) break;
499
			if (mouse.mkm) {
500
				EventTabClose(id__ - TAB_ID);
501
			} else {
502
				EventTabClick(id__ - TAB_ID);
503
			}
504
			return;
505
		case TAB_CLOSE_ID...TAB_CLOSE_ID+TABS_MAX:
506
			EventTabClose(id__ - TAB_CLOSE_ID);
507
			return;
4413 leency 508
	}
509
}
510
 
4636 leency 511
void StopLoading()
512
{
7281 leency 513
	if (http.transfer)
4647 leency 514
	{
7281 leency 515
		EAX = http.transfer;
4647 leency 516
		EAX = EAX.http_msg.content_ptr;		// get pointer to data
517
		$push	EAX							// save it on the stack
7281 leency 518
		http_free stdcall (http.transfer);	// abort connection
4647 leency 519
		$pop	EAX
5631 pavelyakov 520
		free(EAX);						// free data
7281 leency 521
		http.transfer=0;
7746 leency 522
		pause(10);
4647 leency 523
	}
5519 leency 524
	wv_progress_bar.value = 0;
7743 leency 525
	DrawOmnibox();
4645 leency 526
}
527
 
7756 leency 528
//rewrite into
7757 leency 529
//bool strrpl(dword dst, from, into, dst_len);
530
bool ReplaceSpaceInUrl(dword url, size) {
7756 leency 531
	unsigned int i, j;
7757 leency 532
	bool was_changed=false;
533
	for (i=url+size-3; i>url; i--)
7720 leency 534
	{
7756 leency 535
		if (ESBYTE[i]!=' ') continue;
7757 leency 536
		for (j=url+size-3; j>=i; j--) {
537
			ESBYTE[j+3]=ESBYTE[j+2];
7756 leency 538
			ESBYTE[j+2]=ESBYTE[j+1];
539
			ESBYTE[j+1]=ESBYTE[j];
540
		}
541
		ESBYTE[i] = '%';
542
		ESBYTE[i+1] = '2';
543
		ESBYTE[i+2] = '0';
7757 leency 544
		was_changed = true;
7720 leency 545
	}
7757 leency 546
	return was_changed;
7720 leency 547
}
548
 
7801 leency 549
bool HandleUrlFiles(dword _path, _data)
550
{
551
	dword url_from_file;
552
	if (!UrlExtIs(_path, "url")) return false;
7945 leency 553
	if (! url_from_file = strstri(_data, "URL=")) return false;
7801 leency 554
	replace_char(url_from_file, '\n', '\0', strlen(url_from_file));
7945 leency 555
	OpenPage(url_from_file);
556
	return true;
7801 leency 557
}
558
 
7755 leency 559
bool GetLocalFileData(dword _path)
4416 leency 560
{
7801 leency 561
	dword data, size;
7755 leency 562
	file_size stdcall (_path);
7801 leency 563
	if (!EBX) return false;
564
 
565
	size = EBX;
566
	data = malloc(size);
567
	ReadFile(0, size, data, _path);
568
	if (!HandleUrlFiles(_path, data)) {
569
		LoadInternalPage(data, size);
4692 leency 570
	}
7801 leency 571
	free(data);
572
	return true;
7755 leency 573
}
7282 leency 574
 
7755 leency 575
void OpenPage(dword _open_URL)
576
{
577
	char new_url[URL_SIZE+1];
7970 leency 578
	char new_url_full[URL_SIZE+1];
7788 leency 579
	int unz_id;
7755 leency 580
 
581
	StopLoading();
582
 
7771 leency 583
	SetOmniboxText(_open_URL);
584
 
7755 leency 585
	strncpy(#new_url, _open_URL, URL_SIZE);
586
 
587
	//Exclude # from the URL to the load page
588
	//We will bring it back when we get the buffer
7970 leency 589
	if (strrchr(#new_url, '#')) {
590
		anchors.take_anchor_from(#new_url);
591
	}
7755 leency 592
 
593
	history.add(#new_url);
594
 
7764 leency 595
	if (pages_cache.has(#new_url)) {
596
		//CACHED PAGE
597
		LoadInternalPage(pages_cache.current_page_buf, pages_cache.current_page_size);
598
 
599
	} else if (!strncmp(#new_url,"WebView:",8)) {
7755 leency 600
		//INTERNAL PAGE
601
		if (!strcmp(#new_url, URL_SERVICE_HOMEPAGE)) LoadInternalPage(#homepage, sizeof(homepage));
602
		else if (!strcmp(#new_url, URL_SERVICE_HELP)) LoadInternalPage(#help, sizeof(help));
603
		else if (!strcmp(#new_url, URL_SERVICE_HISTORY)) ShowHistory();
604
		else LoadInternalPage(#page_not_found, sizeof(page_not_found));
7800 leency 605
 
7755 leency 606
	} else if (!strncmp(#new_url,"http:",5)) || (!strncmp(#new_url,"https:",6)) {
607
		//WEB PAGE
7757 leency 608
		if (ReplaceSpaceInUrl(#new_url, URL_SIZE)) {
609
			strcpy(#editURL, #new_url);
610
		}
611
 
7755 leency 612
		if (!strncmp(#new_url,"http:",5)) {
613
			http.get(#new_url);
614
		} else if (!strncmp(#new_url,"https://",8)) {
7970 leency 615
			strcpy(#new_url_full, "http://gate.aspero.pro/?site=");
616
			strncat(#new_url_full, #new_url, URL_SIZE);
617
			http.get(#new_url_full);
7282 leency 618
		}
7758 leency 619
 
620
		DrawOmnibox();
621
 
7755 leency 622
		if (!http.transfer) {
4646 leency 623
			StopLoading();
7755 leency 624
			LoadInternalPage(#page_not_found, sizeof(page_not_found));
4646 leency 625
		}
7755 leency 626
	} else {
627
		//LOCAL PAGE
7788 leency 628
		if (UrlExtIs(#new_url,".docx")) {
629
			DeleteFile("/tmp0/1/temp/word/document.xml");
630
			CreateDir("/tmp0/1/temp");
631
			unz_id = RunProgram("/sys/unz", sprintf(#param, "-o \"/tmp0/1/temp\" -h \"%s\"", #new_url));
632
			while (GetProcessSlot(unz_id)) pause(2);
633
			strcpy(#new_url, "/tmp0/1/temp/word/document.xml");
634
		}
7755 leency 635
		if (!GetLocalFileData(#new_url)) {
636
			LoadInternalPage(#page_not_found, sizeof(page_not_found));
4650 leency 637
		}
4636 leency 638
	}
4416 leency 639
}
4415 leency 640
 
7970 leency 641
 
642
bool EventClickAnchor()
4508 leency 643
{
7970 leency 644
	dword aURL = links.active_url;
645
 
646
	if (anchors.get_pos_by_name(aURL+1)!=-1) {
647
		WB1.list.first = anchors.get_pos_by_name(aURL+1);
648
		//WB1.list.CheckDoesValuesOkey();
649
		strcpy(#editURL, history.current());
650
		strcat(#editURL, aURL);
651
		DrawOmnibox();
652
		WB1.DrawPage();
653
		return true;
654
	}
655
	return false;
656
}
657
 
658
void EventClickLink(dword _target)
659
{
7756 leency 660
	char new_url[URL_SIZE+1];
7936 leency 661
	char new_url_full[URL_SIZE+1];
7970 leency 662
	dword aURL = GetAbsoluteActiveURL();
663
	if (!aURL) return;
4415 leency 664
 
7970 leency 665
	strcpy(#new_url, aURL);
666
 
667
	if (ESBYTE[aURL]=='#') {
668
		if (_target == TARGET_SAME_TAB) {
669
			EventClickAnchor();
670
			return;
671
		} else {
672
			strcpy(#new_url, history.current());
673
			strcat(#new_url, aURL);
674
		}
675
	}
676
 
677
	if (_target == TARGET_NEW_TAB) {
678
		EventOpenNewTab(#new_url);
7758 leency 679
		return;
680
	}
681
 
7970 leency 682
	if (_target == TARGET_NEW_WINDOW) {
683
		strcpy(#new_url_full, "-new ");
684
		strncat(#new_url_full, #new_url, URL_SIZE);
685
		RunProgram(#program_path, #new_url_full);
7755 leency 686
		return;
5768 leency 687
	}
7742 leency 688
 
7970 leency 689
	if (!strncmp(#new_url,"mailto:", 7)) || (!strncmp(#new_url,"tel:", 4)) {
690
		notify(#new_url);
7755 leency 691
		return;
7742 leency 692
	}
4508 leency 693
 
7771 leency 694
	if (http.transfer) {
5774 leency 695
		StopLoading();
6021 leency 696
		history.back();
5774 leency 697
	}
6730 leency 698
 
7755 leency 699
	if (strrchr(#new_url, '#')!=0) {
700
		anchors.take_anchor_from(#new_url);
701
		OpenPage(#new_url);
6730 leency 702
		return;
703
	}
704
 
7755 leency 705
	if (!strncmp(#new_url,"WebView:",8)) {
706
		OpenPage(#new_url);
7742 leency 707
		return;
708
	}
6730 leency 709
 
7755 leency 710
	if (strncmp(#new_url,"http://",7)!=0) && (strncmp(#new_url,"https://",8)!=0)
5774 leency 711
	{
7755 leency 712
		if (UrlExtIs(#new_url,".htm")!=true) && (UrlExtIs(#new_url,".html")!=true)
6730 leency 713
		{
7755 leency 714
			if (strchr(#new_url, '|')) {
715
				ESBYTE[strchr(#new_url, '|')] = NULL;
716
				RunProgram(#new_url, strlen(#new_url)+1+#new_url);
717
			} else {
718
				RunProgram("/sys/@open", #new_url);
7422 leency 719
			}
6730 leency 720
			return;
721
		}
7755 leency 722
	} else {
723
		if (UrlExtIs(#new_url,".png")==true) || (UrlExtIs(#new_url,".jpg")==true)
724
		|| (UrlExtIs(#new_url,".zip")==true) || (UrlExtIs(#new_url,".kex")==true) || (UrlExtIs(#new_url,".pdf")==true)
725
		|| (UrlExtIs(#new_url,".7z")==true) {
6986 leency 726
			if (!downloader_opened) {
7755 leency 727
				strcpy(#downloader_edit, #new_url);
6986 leency 728
				CreateThread(#Downloader,#downloader_stak+4092);
729
			}
730
			else notify("'WebView\nPlease, start a new download only when previous ended.'Et");
6730 leency 731
			return;
5774 leency 732
		}
733
	}
7755 leency 734
	OpenPage(#new_url);
735
}
736
 
737
void EventSubmitOmnibox()
738
{
739
	char new_url[URL_SIZE+1];
740
	if (!editURL[0]) return;
741
	if (!strncmp(#editURL,"http:",5)) || (editURL[0]=='/')
742
	|| (!strncmp(#editURL,"https:",6)) || (!strncmp(#editURL,"WebView:",8)) {
743
		OpenPage(#editURL);
744
	} else {
745
		strcpy(#new_url, "http://");
7970 leency 746
		strncat(#new_url, #editURL, URL_SIZE-1);
7755 leency 747
		OpenPage(#new_url);
6931 leency 748
	}
5774 leency 749
}
750
 
7755 leency 751
void LoadInternalPage(dword _bufdata, _in_bufsize){
752
	if (!_bufdata) || (!_in_bufsize) {
7752 leency 753
		LoadInternalPage(#page_not_found, sizeof(page_not_found));
7755 leency 754
	} else {
755
		WB1.list.first = 0; //scroll page to the top
756
		DrawOmnibox();
757
		if(!strrchr(#editURL, '#')) {
758
			strcat(#editURL, #anchors.current);
759
			DrawOmnibox();
760
		}
7759 leency 761
		WB1.ParseHtml(_bufdata, _in_bufsize);
7970 leency 762
		DrawStatusBar();
7771 leency 763
		DrawActiveTab();
7756 leency 764
		if (source_mode) {
765
			source_mode = false;
7799 leency 766
			WB1.custom_encoding = CH_CP866;
7759 leency 767
			ShowSource(WB1.bufpointer, _in_bufsize);
7757 leency 768
		} else {
769
			WB1.DrawPage();
7756 leency 770
		}
7742 leency 771
	}
772
}
773
 
7799 leency 774
bool UrlExtIs(dword base, ext)
7742 leency 775
{
7755 leency 776
	if (!strcmpi(base + strlen(base) - strlen(ext), ext)) return true;
7742 leency 777
	return false;
778
}
779
 
780
void DrawProgress()
781
{
782
	dword persent;
783
	if (http.transfer == 0) return;
7755 leency 784
	if (wv_progress_bar.max) {
785
		persent = wv_progress_bar.value*100/wv_progress_bar.max;
786
	} else {
787
		persent = 10;
788
	}
7758 leency 789
	DrawBar(address_box.left-1, address_box.top+20, persent*address_box.width+16/100, 2, 0x72B7EB);
7742 leency 790
}
791
 
7770 leency 792
void EventShowPageMenu()
6782 leency 793
{
7780 leency 794
	open_lmenu(Form.left + mouse.x+4, Form.top + skin_height + mouse.y, MENU_ALIGN_TOP_LEFT, NULL, #rmb_menu);
795
	menu_id = VIEW_SOURCE;
6782 leency 796
}
797
 
7770 leency 798
void EventShowLinkMenu()
7759 leency 799
{
7780 leency 800
	open_lmenu(Form.left + mouse.x+4, Form.top + skin_height + mouse.y, MENU_ALIGN_TOP_LEFT, NULL, #link_menu);
7789 simsanutiy 801
	menu_id = IN_NEW_TAB;
7759 leency 802
}
803
 
7780 leency 804
void EventShowMainMenu()
805
{
806
	open_lmenu(Form.left + Form.cwidth - PADDING, Form.top + skin_height + PADDING + TSZE + 3,
807
		MENU_ALIGN_TOP_RIGHT, NULL, #main_menu);
808
	menu_id = OPEN_FILE;
809
}
810
 
7770 leency 811
void EventShowEncodingsList()
7758 leency 812
{
7970 leency 813
	open_lmenu(Form.left + Form.cwidth, Form.top + skin_height + Form.cheight - STATUSBAR_H + 12,
7780 leency 814
		MENU_ALIGN_BOT_RIGHT, WB1.cur_encoding + 1, "UTF-8\nKOI8-RU\nCP1251\nCP1252\nISO8859-5\nCP866");
815
	menu_id = ENCODINGS;
7758 leency 816
}
817
 
7780 leency 818
void ProcessMenuClick()
6795 leency 819
{
7780 leency 820
	int click_id;
7910 leency 821
	if (menu_id) {
822
		if (click_id = get_menu_click()) {
823
			click_id += menu_id - 1;
824
			ProcessEvent(click_id);
825
		}
826
		if (!menu_process_id) menu_id = NULL;
7780 leency 827
	}
6795 leency 828
}
829
 
7281 leency 830
void EventUpdateProgressBar()
831
{
832
	wv_progress_bar.max = http.content_length;
833
	if (wv_progress_bar.value != http.content_received)
834
	{
835
		wv_progress_bar.value = http.content_received;
836
		DrawProgress();
837
	}
838
}
6795 leency 839
 
7743 leency 840
void EventSeachWeb()
841
{
7755 leency 842
	char new_url[URL_SIZE+1];
843
	replace_char(#editURL, ' ', '_', URL_SIZE);
844
	strcpy(#new_url, "https://www.google.com/search?q=");
845
	strncat(#new_url, #editURL, URL_SIZE);
846
	OpenPage(#new_url);
7743 leency 847
}
848
 
7748 leency 849
void EventOpenDialog()
850
{
851
	OpenDialog_start stdcall (#o_dialog);
852
	if (o_dialog.status) {
7755 leency 853
		OpenPage(#openfile_path);
7748 leency 854
	}
855
}
856
 
7750 leency 857
void EventViewSource()
858
{
7755 leency 859
	char source_view_param[URL_SIZE+1];
7771 leency 860
	//strcpy(#source_view_param, "-source ");
861
	//strncat(#source_view_param, history.current(), URL_SIZE);
862
	//RunProgram(#program_path, #source_view_param);
863
	source_mode = true;
864
	EventOpenNewTab(history.current());
7750 leency 865
}
866
 
7893 leency 867
void EventRefreshPage()
868
{
869
	if (http.transfer) {
870
		StopLoading();
871
		draw_window();
872
	} else {
873
		OpenPage(history.current());
874
	}
875
}
876
 
7765 leency 877
dword GetFileSize(dword _path)
878
{
879
	BDVK bdvk;
880
	if (GetFileInfo(_path, #bdvk)!=0) {
881
		return 0;
882
	} else {
883
		return bdvk.sizelo;
884
	}
885
}
886
 
887
void EventUpdateBrowser()
888
{
889
	dword downloader_id, slot_n;
890
	dword current_size;
891
	dword new_size;
892
	int error;
893
 
894
	draw_window();
895
 
896
	downloader_id = RunProgram(#program_path, #update_param);
897
	do {
898
		slot_n = GetProcessSlot(downloader_id);
899
		pause(10);
900
	} while (slot_n!=0);
901
 
902
	current_size = GetFileSize(#program_path);
903
	new_size = GetFileSize("/tmp0/1/Downloads/WebView.com");
904
 
905
	if (!new_size) || (new_size<5000) {
906
		notify(#update_download_error);
907
		return;
908
	}
909
 
910
	if (current_size == new_size) {
911
		notify(#update_is_current);
912
		return;
913
	}
914
 
915
	if (error = CopyFileAtOnce(new_size, "/tmp0/1/Downloads/WebView.com", #program_path)) {
916
		notify(#update_can_not_copy);
917
	} else {
918
		notify(#update_ok);
919
		RunProgram(#program_path, history.current());
920
		ExitProcess();
921
	}
922
}
923
 
7970 leency 924
void DrawStatusBar()
6795 leency 925
{
7970 leency 926
	dword status_y = Form.cheight - STATUSBAR_H + 4;
927
	dword status_w = Form.cwidth - 90;
7806 leency 928
	DrawBar(0,Form.cheight - STATUSBAR_H+1, Form.cwidth,STATUSBAR_H-1, sc.work);
7970 leency 929
	if (links.active_url) {
930
		ESI = math.min(status_w/6, strlen(links.active_url));
931
		WriteText(10, status_y, 0, sc.work_text, links.active_url);
7758 leency 932
	}
7970 leency 933
	DefineHiddenButton(status_w+20, status_y-3, 60, 12, CHANGE_ENCODING);
934
	WriteTextCenter(status_w+20, status_y, 60, sc.work_text, WB1.cur_encoding*10+#charsets);
7758 leency 935
}
936
 
937
void DrawOmnibox()
938
{
7784 leency 939
	int imgxoff;
7758 leency 940
 
7806 leency 941
	DrawOvalBorder(address_box.left-2, address_box.top-3, address_box.width+18, 24, sc.work_graph,
942
		sc.work_graph, sc.work_graph, sc.work_dark);
7758 leency 943
	DrawBar(address_box.left-1, address_box.top-2, address_box.width+18, 1, 0xD8DCD8);
944
	DrawBar(address_box.left-1, address_box.top-1, address_box.width+18, 1, address_box.color);
945
	DrawBar(address_box.left-1, address_box.top, 1, 22, address_box.color);
946
 
947
	if (address_box.flags & ed_focus) address_box.flags = ed_focus; else address_box.flags = 0;
948
	EditBox_UpdateText(#address_box, address_box.flags);
949
	edit_box_draw stdcall(#address_box);
7784 leency 950
	if (http.transfer) imgxoff = 16*23*3; else imgxoff = 0;
951
	_PutImage(address_box.left+address_box.width+1, address_box.top-1, 16, 23, imgxoff + #editbox_icons);
952
	DefineHiddenButton(address_box.left+address_box.width-1, address_box.top-2, 17, 23, REFRESH_BUTTON);
7758 leency 953
 
954
	DrawProgress();
955
}
956
 
7771 leency 957
void SetOmniboxText(dword _text)
958
{
959
	strcpy(#editURL, _text);
960
	address_box.flags=0;
961
	DrawOmnibox();
962
}
7758 leency 963
 
7970 leency 964
dword GetAbsoluteActiveURL()
965
{
966
	char abs_url[URL_SIZE];
967
	if (links.active_url) {
968
		strncpy(#abs_url, links.active_url, URL_SIZE);
969
		GetAbsoluteURL(#abs_url, history.current());
970
		return #abs_url;
971
	}
972
	return 0;
973
}
7771 leency 974
 
5631 pavelyakov 975
stop: