Subversion Repositories Kolibri OS

Rev

Rev 7976 | Rev 8016 | 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) {
7987 leency 481
				id__ = GetAbsoluteActiveURL();
482
				strcpy(#downloader_edit, id__);
7208 leency 483
				CreateThread(#Downloader,#downloader_stak+4092);
484
			}
485
			return;
7758 leency 486
		case OPEN_FILE:
487
			EventOpenDialog();
488
			return;
7282 leency 489
		case SCAN_CODE_F12:
490
			debug_mode ^= 1;
491
			if (debug_mode) notify("'Debug mode ON'-I");
492
			else notify("'Debug mode OFF'-I");
493
			return;
7771 leency 494
		case NEW_TAB:
495
			if (http.transfer) break;
496
			EventOpenNewTab(URL_SERVICE_HOMEPAGE);
497
			return;
498
		case TAB_ID...TAB_ID+TABS_MAX:
499
			if (http.transfer) break;
500
			if (mouse.mkm) {
501
				EventTabClose(id__ - TAB_ID);
502
			} else {
503
				EventTabClick(id__ - TAB_ID);
504
			}
505
			return;
506
		case TAB_CLOSE_ID...TAB_CLOSE_ID+TABS_MAX:
507
			EventTabClose(id__ - TAB_CLOSE_ID);
508
			return;
4413 leency 509
	}
510
}
511
 
4636 leency 512
void StopLoading()
513
{
7281 leency 514
	if (http.transfer)
4647 leency 515
	{
7281 leency 516
		EAX = http.transfer;
4647 leency 517
		EAX = EAX.http_msg.content_ptr;		// get pointer to data
518
		$push	EAX							// save it on the stack
7281 leency 519
		http_free stdcall (http.transfer);	// abort connection
4647 leency 520
		$pop	EAX
5631 pavelyakov 521
		free(EAX);						// free data
7281 leency 522
		http.transfer=0;
7746 leency 523
		pause(10);
4647 leency 524
	}
5519 leency 525
	wv_progress_bar.value = 0;
7743 leency 526
	DrawOmnibox();
4645 leency 527
}
528
 
7756 leency 529
//rewrite into
7757 leency 530
//bool strrpl(dword dst, from, into, dst_len);
531
bool ReplaceSpaceInUrl(dword url, size) {
7756 leency 532
	unsigned int i, j;
7757 leency 533
	bool was_changed=false;
534
	for (i=url+size-3; i>url; i--)
7720 leency 535
	{
7756 leency 536
		if (ESBYTE[i]!=' ') continue;
7757 leency 537
		for (j=url+size-3; j>=i; j--) {
538
			ESBYTE[j+3]=ESBYTE[j+2];
7756 leency 539
			ESBYTE[j+2]=ESBYTE[j+1];
540
			ESBYTE[j+1]=ESBYTE[j];
541
		}
542
		ESBYTE[i] = '%';
543
		ESBYTE[i+1] = '2';
544
		ESBYTE[i+2] = '0';
7757 leency 545
		was_changed = true;
7720 leency 546
	}
7757 leency 547
	return was_changed;
7720 leency 548
}
549
 
7801 leency 550
bool HandleUrlFiles(dword _path, _data)
551
{
552
	dword url_from_file;
553
	if (!UrlExtIs(_path, "url")) return false;
7945 leency 554
	if (! url_from_file = strstri(_data, "URL=")) return false;
7801 leency 555
	replace_char(url_from_file, '\n', '\0', strlen(url_from_file));
7945 leency 556
	OpenPage(url_from_file);
557
	return true;
7801 leency 558
}
559
 
7755 leency 560
bool GetLocalFileData(dword _path)
4416 leency 561
{
7801 leency 562
	dword data, size;
7755 leency 563
	file_size stdcall (_path);
7801 leency 564
	if (!EBX) return false;
565
 
566
	size = EBX;
567
	data = malloc(size);
568
	ReadFile(0, size, data, _path);
569
	if (!HandleUrlFiles(_path, data)) {
570
		LoadInternalPage(data, size);
4692 leency 571
	}
7801 leency 572
	free(data);
573
	return true;
7755 leency 574
}
7282 leency 575
 
7755 leency 576
void OpenPage(dword _open_URL)
577
{
578
	char new_url[URL_SIZE+1];
7970 leency 579
	char new_url_full[URL_SIZE+1];
7788 leency 580
	int unz_id;
7755 leency 581
 
582
	StopLoading();
583
 
7771 leency 584
	SetOmniboxText(_open_URL);
585
 
7755 leency 586
	strncpy(#new_url, _open_URL, URL_SIZE);
587
 
588
	//Exclude # from the URL to the load page
589
	//We will bring it back when we get the buffer
7970 leency 590
	if (strrchr(#new_url, '#')) {
591
		anchors.take_anchor_from(#new_url);
592
	}
7755 leency 593
 
594
	history.add(#new_url);
595
 
7976 leency 596
	/*
597
	There could be several possible types of addresses:
598
	- cached page (only http/https)
599
	- internal page
600
	- web page
601
	- local file
602
	So we need to detect what incoming address is
603
	and then halndle it in the propper way.
604
	*/
605
 
7764 leency 606
	if (pages_cache.has(#new_url)) {
607
		//CACHED PAGE
608
		LoadInternalPage(pages_cache.current_page_buf, pages_cache.current_page_size);
609
 
610
	} else if (!strncmp(#new_url,"WebView:",8)) {
7755 leency 611
		//INTERNAL PAGE
7976 leency 612
		if (streq(#new_url, URL_SERVICE_HOMEPAGE)) LoadInternalPage(#buidin_page_home, sizeof(buidin_page_home));
613
		else if (streq(#new_url, URL_SERVICE_HELP)) LoadInternalPage(#buidin_page_help, sizeof(buidin_page_help));
614
		else if (streq(#new_url, URL_SERVICE_HISTORY)) ShowHistory();
615
		else LoadInternalPage(#buidin_page_error, sizeof(buidin_page_error));
7800 leency 616
 
7755 leency 617
	} else if (!strncmp(#new_url,"http:",5)) || (!strncmp(#new_url,"https:",6)) {
618
		//WEB PAGE
7757 leency 619
		if (ReplaceSpaceInUrl(#new_url, URL_SIZE)) {
620
			strcpy(#editURL, #new_url);
621
		}
622
 
7755 leency 623
		if (!strncmp(#new_url,"http:",5)) {
624
			http.get(#new_url);
625
		} else if (!strncmp(#new_url,"https://",8)) {
7970 leency 626
			strcpy(#new_url_full, "http://gate.aspero.pro/?site=");
627
			strncat(#new_url_full, #new_url, URL_SIZE);
628
			http.get(#new_url_full);
7282 leency 629
		}
7758 leency 630
 
631
		DrawOmnibox();
632
 
7755 leency 633
		if (!http.transfer) {
4646 leency 634
			StopLoading();
7976 leency 635
			LoadInternalPage(#buidin_page_error, sizeof(buidin_page_error));
4646 leency 636
		}
7755 leency 637
	} else {
638
		//LOCAL PAGE
7788 leency 639
		if (UrlExtIs(#new_url,".docx")) {
640
			DeleteFile("/tmp0/1/temp/word/document.xml");
641
			CreateDir("/tmp0/1/temp");
642
			unz_id = RunProgram("/sys/unz", sprintf(#param, "-o \"/tmp0/1/temp\" -h \"%s\"", #new_url));
643
			while (GetProcessSlot(unz_id)) pause(2);
644
			strcpy(#new_url, "/tmp0/1/temp/word/document.xml");
645
		}
7755 leency 646
		if (!GetLocalFileData(#new_url)) {
7976 leency 647
			LoadInternalPage(#buidin_page_error, sizeof(buidin_page_error));
4650 leency 648
		}
4636 leency 649
	}
4416 leency 650
}
4415 leency 651
 
7970 leency 652
 
653
bool EventClickAnchor()
4508 leency 654
{
7970 leency 655
	dword aURL = links.active_url;
656
 
657
	if (anchors.get_pos_by_name(aURL+1)!=-1) {
658
		WB1.list.first = anchors.get_pos_by_name(aURL+1);
659
		//WB1.list.CheckDoesValuesOkey();
660
		strcpy(#editURL, history.current());
661
		strcat(#editURL, aURL);
662
		DrawOmnibox();
663
		WB1.DrawPage();
664
		return true;
665
	}
666
	return false;
667
}
668
 
669
void EventClickLink(dword _target)
670
{
7756 leency 671
	char new_url[URL_SIZE+1];
7936 leency 672
	char new_url_full[URL_SIZE+1];
7970 leency 673
	dword aURL = GetAbsoluteActiveURL();
674
	if (!aURL) return;
4415 leency 675
 
7970 leency 676
	strcpy(#new_url, aURL);
677
 
678
	if (ESBYTE[aURL]=='#') {
679
		if (_target == TARGET_SAME_TAB) {
680
			EventClickAnchor();
681
			return;
682
		} else {
683
			strcpy(#new_url, history.current());
684
			strcat(#new_url, aURL);
685
		}
686
	}
687
 
688
	if (_target == TARGET_NEW_TAB) {
689
		EventOpenNewTab(#new_url);
7758 leency 690
		return;
691
	}
692
 
7970 leency 693
	if (_target == TARGET_NEW_WINDOW) {
694
		strcpy(#new_url_full, "-new ");
695
		strncat(#new_url_full, #new_url, URL_SIZE);
696
		RunProgram(#program_path, #new_url_full);
7755 leency 697
		return;
5768 leency 698
	}
7742 leency 699
 
7970 leency 700
	if (!strncmp(#new_url,"mailto:", 7)) || (!strncmp(#new_url,"tel:", 4)) {
701
		notify(#new_url);
7755 leency 702
		return;
7742 leency 703
	}
4508 leency 704
 
7771 leency 705
	if (http.transfer) {
5774 leency 706
		StopLoading();
6021 leency 707
		history.back();
5774 leency 708
	}
6730 leency 709
 
7755 leency 710
	if (strrchr(#new_url, '#')!=0) {
711
		anchors.take_anchor_from(#new_url);
712
		OpenPage(#new_url);
6730 leency 713
		return;
714
	}
715
 
7755 leency 716
	if (!strncmp(#new_url,"WebView:",8)) {
717
		OpenPage(#new_url);
7742 leency 718
		return;
719
	}
6730 leency 720
 
7755 leency 721
	if (strncmp(#new_url,"http://",7)!=0) && (strncmp(#new_url,"https://",8)!=0)
5774 leency 722
	{
7755 leency 723
		if (UrlExtIs(#new_url,".htm")!=true) && (UrlExtIs(#new_url,".html")!=true)
6730 leency 724
		{
7755 leency 725
			if (strchr(#new_url, '|')) {
726
				ESBYTE[strchr(#new_url, '|')] = NULL;
727
				RunProgram(#new_url, strlen(#new_url)+1+#new_url);
728
			} else {
729
				RunProgram("/sys/@open", #new_url);
7422 leency 730
			}
6730 leency 731
			return;
732
		}
7755 leency 733
	} else {
734
		if (UrlExtIs(#new_url,".png")==true) || (UrlExtIs(#new_url,".jpg")==true)
735
		|| (UrlExtIs(#new_url,".zip")==true) || (UrlExtIs(#new_url,".kex")==true) || (UrlExtIs(#new_url,".pdf")==true)
736
		|| (UrlExtIs(#new_url,".7z")==true) {
6986 leency 737
			if (!downloader_opened) {
7755 leency 738
				strcpy(#downloader_edit, #new_url);
6986 leency 739
				CreateThread(#Downloader,#downloader_stak+4092);
740
			}
741
			else notify("'WebView\nPlease, start a new download only when previous ended.'Et");
6730 leency 742
			return;
5774 leency 743
		}
744
	}
7755 leency 745
	OpenPage(#new_url);
746
}
747
 
748
void EventSubmitOmnibox()
749
{
750
	char new_url[URL_SIZE+1];
751
	if (!editURL[0]) return;
752
	if (!strncmp(#editURL,"http:",5)) || (editURL[0]=='/')
753
	|| (!strncmp(#editURL,"https:",6)) || (!strncmp(#editURL,"WebView:",8)) {
754
		OpenPage(#editURL);
755
	} else {
756
		strcpy(#new_url, "http://");
7970 leency 757
		strncat(#new_url, #editURL, URL_SIZE-1);
7755 leency 758
		OpenPage(#new_url);
6931 leency 759
	}
5774 leency 760
}
761
 
7755 leency 762
void LoadInternalPage(dword _bufdata, _in_bufsize){
763
	if (!_bufdata) || (!_in_bufsize) {
7976 leency 764
		LoadInternalPage(#buidin_page_error, sizeof(buidin_page_error));
7755 leency 765
	} else {
766
		WB1.list.first = 0; //scroll page to the top
767
		DrawOmnibox();
768
		if(!strrchr(#editURL, '#')) {
769
			strcat(#editURL, #anchors.current);
770
			DrawOmnibox();
771
		}
7759 leency 772
		WB1.ParseHtml(_bufdata, _in_bufsize);
7970 leency 773
		DrawStatusBar();
7771 leency 774
		DrawActiveTab();
7756 leency 775
		if (source_mode) {
776
			source_mode = false;
7799 leency 777
			WB1.custom_encoding = CH_CP866;
7759 leency 778
			ShowSource(WB1.bufpointer, _in_bufsize);
7757 leency 779
		} else {
780
			WB1.DrawPage();
7756 leency 781
		}
7742 leency 782
	}
783
}
784
 
7799 leency 785
bool UrlExtIs(dword base, ext)
7742 leency 786
{
7755 leency 787
	if (!strcmpi(base + strlen(base) - strlen(ext), ext)) return true;
7742 leency 788
	return false;
789
}
790
 
791
void DrawProgress()
792
{
793
	dword persent;
794
	if (http.transfer == 0) return;
7755 leency 795
	if (wv_progress_bar.max) {
796
		persent = wv_progress_bar.value*100/wv_progress_bar.max;
797
	} else {
798
		persent = 10;
799
	}
7758 leency 800
	DrawBar(address_box.left-1, address_box.top+20, persent*address_box.width+16/100, 2, 0x72B7EB);
7742 leency 801
}
802
 
7770 leency 803
void EventShowPageMenu()
6782 leency 804
{
7780 leency 805
	open_lmenu(Form.left + mouse.x+4, Form.top + skin_height + mouse.y, MENU_ALIGN_TOP_LEFT, NULL, #rmb_menu);
806
	menu_id = VIEW_SOURCE;
6782 leency 807
}
808
 
7770 leency 809
void EventShowLinkMenu()
7759 leency 810
{
7780 leency 811
	open_lmenu(Form.left + mouse.x+4, Form.top + skin_height + mouse.y, MENU_ALIGN_TOP_LEFT, NULL, #link_menu);
7789 simsanutiy 812
	menu_id = IN_NEW_TAB;
7759 leency 813
}
814
 
7780 leency 815
void EventShowMainMenu()
816
{
817
	open_lmenu(Form.left + Form.cwidth - PADDING, Form.top + skin_height + PADDING + TSZE + 3,
818
		MENU_ALIGN_TOP_RIGHT, NULL, #main_menu);
819
	menu_id = OPEN_FILE;
820
}
821
 
7770 leency 822
void EventShowEncodingsList()
7758 leency 823
{
7970 leency 824
	open_lmenu(Form.left + Form.cwidth, Form.top + skin_height + Form.cheight - STATUSBAR_H + 12,
7780 leency 825
		MENU_ALIGN_BOT_RIGHT, WB1.cur_encoding + 1, "UTF-8\nKOI8-RU\nCP1251\nCP1252\nISO8859-5\nCP866");
826
	menu_id = ENCODINGS;
7758 leency 827
}
828
 
7780 leency 829
void ProcessMenuClick()
6795 leency 830
{
7780 leency 831
	int click_id;
7910 leency 832
	if (menu_id) {
833
		if (click_id = get_menu_click()) {
834
			click_id += menu_id - 1;
835
			ProcessEvent(click_id);
836
		}
837
		if (!menu_process_id) menu_id = NULL;
7780 leency 838
	}
6795 leency 839
}
840
 
7281 leency 841
void EventUpdateProgressBar()
842
{
843
	wv_progress_bar.max = http.content_length;
844
	if (wv_progress_bar.value != http.content_received)
845
	{
846
		wv_progress_bar.value = http.content_received;
847
		DrawProgress();
848
	}
849
}
6795 leency 850
 
7743 leency 851
void EventSeachWeb()
852
{
7755 leency 853
	char new_url[URL_SIZE+1];
854
	replace_char(#editURL, ' ', '_', URL_SIZE);
855
	strcpy(#new_url, "https://www.google.com/search?q=");
856
	strncat(#new_url, #editURL, URL_SIZE);
857
	OpenPage(#new_url);
7743 leency 858
}
859
 
7748 leency 860
void EventOpenDialog()
861
{
862
	OpenDialog_start stdcall (#o_dialog);
863
	if (o_dialog.status) {
7755 leency 864
		OpenPage(#openfile_path);
7748 leency 865
	}
866
}
867
 
7750 leency 868
void EventViewSource()
869
{
7755 leency 870
	char source_view_param[URL_SIZE+1];
7771 leency 871
	//strcpy(#source_view_param, "-source ");
872
	//strncat(#source_view_param, history.current(), URL_SIZE);
873
	//RunProgram(#program_path, #source_view_param);
874
	source_mode = true;
875
	EventOpenNewTab(history.current());
7750 leency 876
}
877
 
7893 leency 878
void EventRefreshPage()
879
{
880
	if (http.transfer) {
881
		StopLoading();
882
		draw_window();
883
	} else {
884
		OpenPage(history.current());
885
	}
886
}
887
 
7765 leency 888
dword GetFileSize(dword _path)
889
{
890
	BDVK bdvk;
891
	if (GetFileInfo(_path, #bdvk)!=0) {
892
		return 0;
893
	} else {
894
		return bdvk.sizelo;
895
	}
896
}
897
 
898
void EventUpdateBrowser()
899
{
900
	dword downloader_id, slot_n;
901
	dword current_size;
902
	dword new_size;
903
	int error;
904
 
905
	draw_window();
906
 
907
	downloader_id = RunProgram(#program_path, #update_param);
908
	do {
909
		slot_n = GetProcessSlot(downloader_id);
910
		pause(10);
911
	} while (slot_n!=0);
912
 
913
	current_size = GetFileSize(#program_path);
914
	new_size = GetFileSize("/tmp0/1/Downloads/WebView.com");
915
 
916
	if (!new_size) || (new_size<5000) {
917
		notify(#update_download_error);
918
		return;
919
	}
920
 
921
	if (current_size == new_size) {
922
		notify(#update_is_current);
923
		return;
924
	}
925
 
926
	if (error = CopyFileAtOnce(new_size, "/tmp0/1/Downloads/WebView.com", #program_path)) {
927
		notify(#update_can_not_copy);
928
	} else {
929
		notify(#update_ok);
930
		RunProgram(#program_path, history.current());
931
		ExitProcess();
932
	}
933
}
934
 
7970 leency 935
void DrawStatusBar()
6795 leency 936
{
7970 leency 937
	dword status_y = Form.cheight - STATUSBAR_H + 4;
938
	dword status_w = Form.cwidth - 90;
7806 leency 939
	DrawBar(0,Form.cheight - STATUSBAR_H+1, Form.cwidth,STATUSBAR_H-1, sc.work);
7970 leency 940
	if (links.active_url) {
941
		ESI = math.min(status_w/6, strlen(links.active_url));
942
		WriteText(10, status_y, 0, sc.work_text, links.active_url);
7758 leency 943
	}
7970 leency 944
	DefineHiddenButton(status_w+20, status_y-3, 60, 12, CHANGE_ENCODING);
945
	WriteTextCenter(status_w+20, status_y, 60, sc.work_text, WB1.cur_encoding*10+#charsets);
7758 leency 946
}
947
 
948
void DrawOmnibox()
949
{
7784 leency 950
	int imgxoff;
7758 leency 951
 
7806 leency 952
	DrawOvalBorder(address_box.left-2, address_box.top-3, address_box.width+18, 24, sc.work_graph,
953
		sc.work_graph, sc.work_graph, sc.work_dark);
7758 leency 954
	DrawBar(address_box.left-1, address_box.top-2, address_box.width+18, 1, 0xD8DCD8);
955
	DrawBar(address_box.left-1, address_box.top-1, address_box.width+18, 1, address_box.color);
956
	DrawBar(address_box.left-1, address_box.top, 1, 22, address_box.color);
957
 
958
	if (address_box.flags & ed_focus) address_box.flags = ed_focus; else address_box.flags = 0;
959
	EditBox_UpdateText(#address_box, address_box.flags);
960
	edit_box_draw stdcall(#address_box);
7784 leency 961
	if (http.transfer) imgxoff = 16*23*3; else imgxoff = 0;
962
	_PutImage(address_box.left+address_box.width+1, address_box.top-1, 16, 23, imgxoff + #editbox_icons);
963
	DefineHiddenButton(address_box.left+address_box.width-1, address_box.top-2, 17, 23, REFRESH_BUTTON);
7758 leency 964
 
965
	DrawProgress();
966
}
967
 
7771 leency 968
void SetOmniboxText(dword _text)
969
{
970
	strcpy(#editURL, _text);
971
	address_box.flags=0;
972
	DrawOmnibox();
973
}
7758 leency 974
 
7970 leency 975
dword GetAbsoluteActiveURL()
976
{
977
	char abs_url[URL_SIZE];
978
	if (links.active_url) {
979
		strncpy(#abs_url, links.active_url, URL_SIZE);
980
		GetAbsoluteURL(#abs_url, history.current());
981
		return #abs_url;
982
	}
983
	return 0;
984
}
7771 leency 985
 
5631 pavelyakov 986
stop: