Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
5496 leency 1
#include 
4680 right-hear 2
#define _WIN32
3
#include "fitz.h"
4
#include "mupdf.h"
5
#include "muxps.h"
6
#include "pdfapp.h"
4696 right-hear 7
#include "icons/allbtns.h"
7475 leency 8
#include "kolibri.c"
4680 right-hear 9
 
5496 leency 10
// need to be a part of menuet/os.h
11
#define BT_DEL      0x80000000
12
#define BT_HIDE     0x40000000
13
#define BT_NOFRAME  0x20000000
4680 right-hear 14
 
5496 leency 15
#define evReDraw  1
16
#define evKey     2
17
#define evButton  3
18
#define evMouse   6
19
#define evNetwork 8
4680 right-hear 20
 
5496 leency 21
#define ASCII_KEY_LEFT  176
22
#define ASCII_KEY_RIGHT 179
23
#define ASCII_KEY_DOWN  177
24
#define ASCII_KEY_UP    178
25
#define ASCII_KEY_HOME  180
26
#define ASCII_KEY_END   181
27
#define ASCII_KEY_PGDN  183
28
#define ASCII_KEY_PGUP  184
4680 right-hear 29
 
5496 leency 30
#define ASCII_KEY_BS    8
31
#define ASCII_KEY_TAB   9
32
#define ASCII_KEY_ENTER 13
33
#define ASCII_KEY_ESC   27
34
#define ASCII_KEY_DEL   182
35
#define ASCII_KEY_INS   185
36
#define ASCII_KEY_SPACE 032
4680 right-hear 37
 
5496 leency 38
struct blit_call
4680 right-hear 39
{
40
   int dstx;
41
   int dsty;
42
   int w;
43
   int h;
44
 
45
   int srcx;
46
   int srcy;
47
   int srcw;
48
   int srch;
49
 
50
   unsigned char *d;
51
   int   stride;
52
};
53
 
54
void blit(int dstx, int dsty, int w, int h, int srcx, int srcy,int srcw, int srch, int stride, char *d) //Вызов сисфункции Blitter
55
{
5496 leency 56
	struct blit_call image;
4680 right-hear 57
	image.dstx=dstx;
58
	image.dsty=dsty;
59
	image.w=w;
60
	image.h=h;
61
	image.srcx=srcx;
62
	image.srcy=srcy;
63
	image.srcw=srcw;
64
	image.srch=srch;
65
	image.stride=stride;
66
	image.d=d;
5496 leency 67
	asm ("int $0x40"::"a"(73),"b"(0),"c"(&image));
68
}
4680 right-hear 69
 
70
 
5496 leency 71
int __menuet__get_mouse_wheels(void)
72
{
73
    int val;
74
    asm ("int $0x40":"=a"(val):"a"(37),"b"(7));
75
    return val;
76
};
4680 right-hear 77
 
5496 leency 78
/*==== DATA ====*/
79
 
80
static char Title[1024] = "uPDF";
81
static pdfapp_t gapp;
82
char debugstr[256];
83
char do_not_blit=0;
84
 
85
#define TOOLBAR_HEIGHT 34
86
struct process_table_entry Form;
87
 
88
#define DOCUMENT_BORDER 0x979797
89
#define DOCUMENT_BG 0xABABAB
90
 
5508 leency 91
#define SCROLL_H 25
5496 leency 92
 
5508 leency 93
short show_area_w = 65;
94
short show_area_x;
95
 
96
char key_mode_enter_page_number;
97
int new_page_number;
98
 
99
static short window_center, draw_h, draw_w;
100
 
5496 leency 101
const char *help[] = {
102
	"Keys:",
103
	"  ",
104
	"PageUp   - go to previous page",
105
	"PageDown - go to next page",
106
	"Home     - go to first page",
107
	"End      - go to last page",
108
	"Down arrow - scroll current page down",
109
	"Up arrow   - scroll current page up",
110
	"+/- - zoom in/out",
111
	"[ or l - rotate page 90 deg to the left",
112
	"] or r - rotate page 90 deg to the right",
5508 leency 113
	"g - grayscale on/off",
5496 leency 114
	"  ",
115
	"Press Escape to hide help",
116
 
117
};
118
 
119
/*==== CODE ====*/
120
 
121
 
122
// not implemented yet
123
void wincursor(pdfapp_t *app, int curs) { }
124
void winhelp(pdfapp_t *app) { }
125
void winresize(pdfapp_t *app, int w, int h) { }
126
void windocopy(pdfapp_t *app) { }
127
void winopenuri(pdfapp_t *app, char *buf) { }
128
void winrepaintsearch(pdfapp_t *app) { }
129
 
130
 
4680 right-hear 131
void winwarn(pdfapp_t *app, char *msg)
132
{
133
	fprintf(stderr, "mupdf: %s\n", msg);
134
}
135
 
5496 leency 136
 
4680 right-hear 137
void winerror(pdfapp_t *app, fz_error error)
138
{
139
	fz_catch(error, "aborting");
140
	exit(1);
141
}
142
 
5496 leency 143
 
4680 right-hear 144
char *winpassword(pdfapp_t *app, char *filename)
145
{
146
	char *r = "";
147
	return r;
148
}
149
 
150
 
151
void wintitle(pdfapp_t *app, char *s)
152
{
5496 leency 153
	char* param = *(char**)0x1C;
154
	sprintf(Title,"%s - uPDF", strrchr(param, '/') + 1 );
4680 right-hear 155
}
156
 
157
 
158
void winreloadfile(pdfapp_t *app)
159
{
5496 leency 160
	//pdfapp_close(app);
161
	//pdfapp_open(app, filename, 0, 1);
4680 right-hear 162
}
163
 
164
void winclose(pdfapp_t *app)
165
{
166
	pdfapp_close(&gapp);
167
	__menuet__sys_exit();
168
}
169
 
5496 leency 170
 
171
void winrepaint(pdfapp_t *app)
4680 right-hear 172
{
5496 leency 173
	winblit(&gapp);
4680 right-hear 174
}
175
 
176
 
5496 leency 177
void winblit(pdfapp_t *app)
4680 right-hear 178
{
179
 
5496 leency 180
	if (do_not_blit) return;
4680 right-hear 181
 
5508 leency 182
	if (key_mode_enter_page_number==1) HandleNewPageNumber(0); else DrawPagination();
183
 
5496 leency 184
	if (Form.client_width > gapp.image->w) window_center = (Form.client_width - gapp.image->w) / 2; else window_center = 0;
4680 right-hear 185
 
5821 leency 186
	gapp.panx = 0;
7475 leency 187
	if (gapp.image->n == 4) {
5496 leency 188
		 	blit(window_center + Form.client_left,
189
		 		Form.client_top + TOOLBAR_HEIGHT,
190
		 		Form.client_width,
191
		 		Form.client_height - TOOLBAR_HEIGHT,
192
		 		gapp.panx,
193
		 		gapp.pany,
194
		 		gapp.image->w,
195
		 		gapp.image->h,
196
		 		gapp.image->w * gapp.image->n,
197
		 		gapp.image->samples
198
		 	);
7475 leency 199
	}
4680 right-hear 200
	else if (gapp.image->n == 2)
201
	{
202
		int i = gapp.image->w*gapp.image->h;
203
		unsigned char *color = malloc(i*4);
204
		if (color != NULL)
205
		{
206
			unsigned char *s = gapp.image->samples;
207
			unsigned char *d = color;
208
			for (; i > 0 ; i--)
209
			{
210
				d[2] = d[1] = d[0] = *s++;
211
				d[3] = *s++;
212
				d += 4;
213
			}
5496 leency 214
			blit(window_center + Form.client_left,
215
				Form.client_top + TOOLBAR_HEIGHT,
216
				Form.client_width,
217
				Form.client_height - TOOLBAR_HEIGHT,
218
		 		gapp.panx,
219
		 		gapp.pany,
220
				gapp.image->w,
221
				gapp.image->h,
222
				gapp.image->w * 4,
223
				color
224
			);
4680 right-hear 225
			free(color);
226
		}
5508 leency 227
	}
228
}
229
 
230
 
231
void DrawPageSides(void)
232
{
233
	if (Form.client_width > gapp.image->w) window_center = (Form.client_width - gapp.image->w) / 2; else window_center = 0;
234
	if (gapp.image->h < Form.client_height - TOOLBAR_HEIGHT) draw_h = gapp.image->h - gapp.pany; else draw_h = Form.client_height - TOOLBAR_HEIGHT;
235
	if (gapp.image->w < Form.client_width)
5496 leency 236
	{
7475 leency 237
		kol_paint_bar(0, TOOLBAR_HEIGHT, window_center-1, Form.client_height - TOOLBAR_HEIGHT, DOCUMENT_BG);
238
		kol_paint_bar(window_center-1, TOOLBAR_HEIGHT, 1, draw_h, DOCUMENT_BORDER);
239
		kol_paint_bar(window_center + gapp.image->w, TOOLBAR_HEIGHT, 1, draw_h, DOCUMENT_BORDER);
240
		kol_paint_bar(window_center + gapp.image->w+1, TOOLBAR_HEIGHT, Form.client_width - window_center - gapp.image->w - 1, Form.client_height - TOOLBAR_HEIGHT, DOCUMENT_BG);
4680 right-hear 241
	}
5508 leency 242
	if (gapp.image->w < Form.client_width)
243
	{
244
		draw_w = gapp.image->w + 2;
245
	}
246
	else
247
	{
248
		window_center = 1;
249
		draw_w = Form.client_width;
250
	}
7475 leency 251
	kol_paint_bar(window_center - 1, gapp.image->h - gapp.pany + TOOLBAR_HEIGHT, draw_w, 1, DOCUMENT_BORDER);
252
	kol_paint_bar(window_center - 1, gapp.image->h - gapp.pany + TOOLBAR_HEIGHT + 1, draw_w, Form.client_height - gapp.image->h - TOOLBAR_HEIGHT + gapp.pany - 1, DOCUMENT_BG);
4680 right-hear 253
}
254
 
255
 
5508 leency 256
 
4680 right-hear 257
int main (void)
258
{
5496 leency 259
	char ii, mouse_wheels_state;
4680 right-hear 260
	char* original_command_line = *(char**)0x1C;
7475 leency 261
	kol_board_puts(original_command_line);
262
	kol_board_puts("\n");
4680 right-hear 263
 
264
	char buf[128];
265
	int resolution = 72;
5496 leency 266
	int pageno = 1;
267
	fz_accelerate();
7475 leency 268
	kol_board_puts("PDF init\n");
5496 leency 269
	pdfapp_init(&gapp);
4680 right-hear 270
	gapp.scrw = 600;
271
	gapp.scrh = 400;
272
	gapp.resolution = resolution;
273
	gapp.pageno = pageno;
7475 leency 274
	kol_board_puts("PDF Open\n");
4680 right-hear 275
	pdfapp_open(&gapp, original_command_line, 0, 0);
7475 leency 276
	kol_board_puts("PDF Opened\n");
5508 leency 277
	wintitle(&gapp, 0);
7475 leency 278
 
279
	kol_board_puts("Inital paint\n");
4680 right-hear 280
 
5496 leency 281
	int butt, key, screen_max_x, screen_max_y;
282
	__menuet__get_screen_max(&screen_max_x, &screen_max_y);
283
	__menuet__set_bitfield_for_wanted_events(EVENT_REDRAW+EVENT_KEY+EVENT_BUTTON+EVENT_MOUSE_CHANGE);
4680 right-hear 284
 
285
 for(;;)
286
 {
287
 
5496 leency 288
	switch(__menuet__wait_for_event())
289
	{
290
		case evReDraw:
5508 leency 291
			// gapp.shrinkwrap = 2;
5496 leency 292
			__menuet__window_redraw(1);
7475 leency 293
			__menuet__define_window(screen_max_x / 2 - 350-50+kos_random(50),
294
			screen_max_y / 2 - 300-50+kos_random(50),
295
			700, 600, 0x73000000, 0x800000FF, Title);
5496 leency 296
			__menuet__window_redraw(2);
297
			__menuet__get_process_table(&Form, PID_WHOAMI);
298
			if (Form.window_state > 2) continue; //fix rolled up
299
			Form.client_width++; //fix for Menuet kernel bug
300
			Form.client_height++; //fix for Menuet kernel bug
301
			DrawWindow();
302
			break;
303
 
304
		case evKey:
305
			key = __menuet__getkey();
5508 leency 306
			if (key_mode_enter_page_number)
307
			{
308
				HandleNewPageNumber(key);
309
				break;
310
			}
5496 leency 311
			if (key==ASCII_KEY_ESC)  DrawWindow(); //close help
312
			if (key==ASCII_KEY_PGDN) pdfapp_onkey(&gapp, ']');
313
			if (key==ASCII_KEY_PGUP) pdfapp_onkey(&gapp, '[');
314
			if (key==ASCII_KEY_HOME) pdfapp_onkey(&gapp, 'g');
315
			if (key==ASCII_KEY_END ) pdfapp_onkey(&gapp, 'G');
5497 leency 316
			if (key=='g' ) pdfapp_onkey(&gapp, 'c');
317
			if ((key=='[' ) || (key=='l')) PageRotateLeft();
318
			if ((key==']' ) || (key=='r')) PageRotateRight();
5496 leency 319
			if (key==ASCII_KEY_DOWN ) PageScrollDown();
320
			if (key==ASCII_KEY_UP ) PageScrollUp();
321
			if (key=='-') PageZoomOut();
322
			if ((key=='=') || (key=='+')) PageZoomIn();
323
			break;
324
 
325
		case evButton:
326
			butt = __menuet__get_button_id();
327
			if(butt==1) __menuet__sys_exit();
7475 leency 328
			if(butt==10) RunApp("/sys/lod", "*pdf* /kolibrios/media/updf");
5496 leency 329
			if(butt==11) PageZoomOut(); //magnify -
330
			if(butt==12) PageZoomIn(); //magnify +
331
			if(butt==13) //show help
332
			{
7475 leency 333
				kol_paint_bar(0, TOOLBAR_HEIGHT, Form.client_width, Form.client_height - TOOLBAR_HEIGHT, 0xF2F2F2);
334
				__menuet__write_text(20, TOOLBAR_HEIGHT + 20      , 0x90000000, "uPDF for KolibriOS v1.2", 0);
335
				__menuet__write_text(21, TOOLBAR_HEIGHT + 20      , 0x90000000, "uPDF for KolibriOS v1.2", 0);
5496 leency 336
				for (ii=0; help[ii]!=0; ii++) {
337
					__menuet__write_text(20, TOOLBAR_HEIGHT + 60 + ii * 15, 0x80000000, help[ii], 0);
4680 right-hear 338
				}
5496 leency 339
			}
340
			if(butt==14) pdfapp_onkey(&gapp, '['); //previous page
341
			if(butt==15) pdfapp_onkey(&gapp, ']'); //next page
5508 leency 342
			if(butt==16) PageRotateLeft();
343
			if(butt==17) PageRotateRight();
344
			if(butt==20) GetNewPageNumber();
5496 leency 345
			break;
346
 
347
		case evMouse:
348
			if (mouse_wheels_state = __menuet__get_mouse_wheels())
349
			{
350
				if (mouse_wheels_state==1) { PageScrollDown(); PageScrollDown(); }
351
				if (mouse_wheels_state==-1) { PageScrollUp();  PageScrollUp();   }
352
			}
353
			//sprintf (debugstr, "mouse_wheels_state: %d \n", mouse_wheels_state);
7475 leency 354
			//kol_board_puts(debugstr);
5496 leency 355
			//pdfapp_onmouse(&gapp, int x, int y, int btn, int modifiers, int state)
356
			break;
357
	}
4680 right-hear 358
  }
359
}
5496 leency 360
 
361
 
5508 leency 362
void GetNewPageNumber(void)
363
{
364
	new_page_number = gapp.pageno;
365
	key_mode_enter_page_number = 1;
366
	HandleNewPageNumber(0);
367
}
5496 leency 368
 
5508 leency 369
void HandleNewPageNumber(unsigned char key)
5496 leency 370
{
5508 leency 371
	char label_new_page[8];
372
 
373
	if ((key >= '0') && (key <= '9'))
5496 leency 374
	{
5508 leency 375
		new_page_number = new_page_number * 10 + key - '0';
5496 leency 376
	}
5508 leency 377
	if (key == ASCII_KEY_BS)
378
	{
379
		new_page_number /= 10;
380
	}
381
	if (key == ASCII_KEY_ENTER)
382
	{
383
		ApplyNewPageNumber();
384
		return;
385
	}
386
	if (key==ASCII_KEY_ESC)
387
	{
388
		key_mode_enter_page_number = 0;
389
		DrawWindow();
390
		return;
391
	}
392
 
393
	itoa(new_page_number, label_new_page, 10);
394
	strcat(label_new_page, "_");
7475 leency 395
	kol_paint_bar(show_area_x,  6, show_area_w, 22, 0xFDF88E);
5508 leency 396
	__menuet__write_text(show_area_x + show_area_w/2 - strlen(label_new_page)*6/2, 14, 0x000000, label_new_page, strlen(label_new_page));
397
 
398
	if (new_page_number > gapp.pagecount) ApplyNewPageNumber();
5496 leency 399
}
400
 
5508 leency 401
void ApplyNewPageNumber(void)
402
{
403
	key_mode_enter_page_number = 0;
404
	gapp.pageno = new_page_number -1;
405
	pdfapp_onkey(&gapp, ']');
406
}
5496 leency 407
 
408
void DrawPagination(void)
409
{
410
	char pages_display[12];
7475 leency 411
	kol_paint_bar(show_area_x,  6, show_area_w, 22, 0xF4F4F4);
5496 leency 412
	sprintf (pages_display, "%d/%d", gapp.pageno, gapp.pagecount);
413
	__menuet__write_text(show_area_x + show_area_w/2 - strlen(pages_display)*6/2, 14, 0x000000, pages_display, strlen(pages_display));
414
}
415
 
416
 
5508 leency 417
 
418
 
5496 leency 419
void DrawWindow(void)
420
{
7475 leency 421
	kol_paint_bar(0, 0, Form.client_width, TOOLBAR_HEIGHT - 1, 0xe1e1e1); // bar on the top (buttons holder)
422
	kol_paint_bar(0, TOOLBAR_HEIGHT - 1, Form.client_width, 1, 0x7F7F7F);
5508 leency 423
	DrawToolbarButton(8,0); //open_folder
424
	DrawToolbarButton(42,1); //magnify -
425
	DrawToolbarButton(67,2);  //magnify +
426
	DrawToolbarButton(101,6); //rotate left
427
	DrawToolbarButton(126,7); //rotate right
428
	DrawToolbarButton(Form.client_width - 160,3); //show help
429
	show_area_x = Form.client_width - show_area_w - 34;
430
	DrawToolbarButton(show_area_x - 26,4); //prev page
431
	DrawToolbarButton(show_area_x + show_area_w,5); //nex page
432
	__menuet__make_button(show_area_x-1,  5, show_area_w+1, 23, 20 + BT_HIDE, 0xA4A4A4);
7475 leency 433
	kol_paint_bar(show_area_x,  5, show_area_w, 1, 0xA4A4A4);
434
	kol_paint_bar(show_area_x, 28, show_area_w, 1, 0xA4A4A4);
5496 leency 435
	winblit(&gapp);
436
	DrawPageSides();
437
}
438
 
5508 leency 439
void DrawToolbarButton(int x, char image_id)
5496 leency 440
{
5508 leency 441
	__menuet__make_button(x, 5, 26-1, 24-1, 10 + image_id + BT_HIDE, 0);
442
	__menuet__putimage(x, 5, 26, 24, image_id * 24 * 26 * 3 + toolbar_image);
5496 leency 443
}
444
 
445
 
446
/* Actions */
447
 
448
void PageScrollDown(void)
449
{
5497 leency 450
	//pdfapp_onkey(&gapp, 'k'); //move down
5496 leency 451
	if (gapp.image->h - gapp.pany - SCROLL_H < Form.client_height - TOOLBAR_HEIGHT)
452
	{
453
		pdfapp_onkey(&gapp, '.');
454
	}
455
	else {
456
		gapp.pany += SCROLL_H;
457
		winblit(&gapp);
458
	}
459
}
460
 
461
 
462
void PageScrollUp(void)
463
{
5497 leency 464
	//pdfapp_onkey(&gapp, 'j'); //move up
5496 leency 465
	if (gapp.pany >= SCROLL_H) {
466
		gapp.pany -= SCROLL_H;
467
		winblit(&gapp);
468
	}
469
	else {
5497 leency 470
		//not very nice way of using do_not_blit, but it simple
5496 leency 471
		if (gapp.pageno == 1) return;
472
		do_not_blit = 1;
473
		pdfapp_onkey(&gapp, ',');
474
		do_not_blit = 0;
475
		gapp.pany = gapp.image->h - SCROLL_H - Form.client_height + TOOLBAR_HEIGHT;
476
		if (gapp.pany < 0) gapp.pany = 0;
477
		//sprintf (debugstr, "gapp.pany: %d \n", gapp.pany);
7475 leency 478
		//kol_board_puts(debugstr);
5496 leency 479
		winblit(&gapp);
480
	}
481
}
482
 
7475 leency 483
void RunApp(char app[], char param[])
484
{
485
	kol_struct70 r;
486
	r.p00 = 7;
487
	r.p04 = 0;
488
	r.p08 = param;
489
	r.p12 = 0;
490
	r.p16 = 0;
491
	r.p20 = 0;
492
	r.p21 = app;
493
	kol_file_70(&r);
494
}
5496 leency 495
 
7475 leency 496
 
5496 leency 497
void PageZoomIn(void)
498
{
499
	pdfapp_onkey(&gapp, '+');
500
	DrawPageSides();
501
}
502
 
503
 
504
void PageZoomOut(void)
505
{
506
	pdfapp_onkey(&gapp, '-');
507
	DrawPageSides();
508
}
509
 
5497 leency 510
void PageRotateLeft(void)
511
{
512
	pdfapp_onkey(&gapp, 'L');
513
	DrawPageSides();
514
}
5496 leency 515
 
5497 leency 516
void PageRotateRight(void)
517
{
518
	pdfapp_onkey(&gapp, 'R');
519
	DrawPageSides();
520
}
521