Subversion Repositories Kolibri OS

Rev

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