Subversion Repositories Kolibri OS

Rev

Rev 5497 | Rev 5821 | 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"
4680 right-hear 8
 
5496 leency 9
// need to be a part of menuet/os.h
10
#define BT_DEL      0x80000000
11
#define BT_HIDE     0x40000000
12
#define BT_NOFRAME  0x20000000
4680 right-hear 13
 
5496 leency 14
#define evReDraw  1
15
#define evKey     2
16
#define evButton  3
17
#define evMouse   6
18
#define evNetwork 8
4680 right-hear 19
 
5496 leency 20
#define ASCII_KEY_LEFT  176
21
#define ASCII_KEY_RIGHT 179
22
#define ASCII_KEY_DOWN  177
23
#define ASCII_KEY_UP    178
24
#define ASCII_KEY_HOME  180
25
#define ASCII_KEY_END   181
26
#define ASCII_KEY_PGDN  183
27
#define ASCII_KEY_PGUP  184
4680 right-hear 28
 
5496 leency 29
#define ASCII_KEY_BS    8
30
#define ASCII_KEY_TAB   9
31
#define ASCII_KEY_ENTER 13
32
#define ASCII_KEY_ESC   27
33
#define ASCII_KEY_DEL   182
34
#define ASCII_KEY_INS   185
35
#define ASCII_KEY_SPACE 032
4680 right-hear 36
 
5496 leency 37
struct blit_call
4680 right-hear 38
{
39
   int dstx;
40
   int dsty;
41
   int w;
42
   int h;
43
 
44
   int srcx;
45
   int srcy;
46
   int srcw;
47
   int srch;
48
 
49
   unsigned char *d;
50
   int   stride;
51
};
52
 
53
void blit(int dstx, int dsty, int w, int h, int srcx, int srcy,int srcw, int srch, int stride, char *d) //Вызов сисфункции Blitter
54
{
5496 leency 55
	struct blit_call image;
4680 right-hear 56
	image.dstx=dstx;
57
	image.dsty=dsty;
58
	image.w=w;
59
	image.h=h;
60
	image.srcx=srcx;
61
	image.srcy=srcy;
62
	image.srcw=srcw;
63
	image.srch=srch;
64
	image.stride=stride;
65
	image.d=d;
5496 leency 66
	asm ("int $0x40"::"a"(73),"b"(0),"c"(&image));
67
}
4680 right-hear 68
 
69
 
5496 leency 70
int __menuet__get_mouse_wheels(void)
71
{
72
    int val;
73
    asm ("int $0x40":"=a"(val):"a"(37),"b"(7));
74
    return val;
75
};
4680 right-hear 76
 
5496 leency 77
/*==== DATA ====*/
78
 
79
static char Title[1024] = "uPDF";
80
static char * filename = "/hd0/1/yand.pdf";
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
 
5496 leency 186
	if (gapp.image->n == 4)
187
		 	blit(window_center + Form.client_left,
188
		 		Form.client_top + TOOLBAR_HEIGHT,
189
		 		Form.client_width,
190
		 		Form.client_height - TOOLBAR_HEIGHT,
191
		 		gapp.panx,
192
		 		gapp.pany,
193
		 		gapp.image->w,
194
		 		gapp.image->h,
195
		 		gapp.image->w * gapp.image->n,
196
		 		gapp.image->samples
197
		 	);
4680 right-hear 198
	else if (gapp.image->n == 2)
199
	{
200
		int i = gapp.image->w*gapp.image->h;
201
		unsigned char *color = malloc(i*4);
202
		if (color != NULL)
203
		{
204
			unsigned char *s = gapp.image->samples;
205
			unsigned char *d = color;
206
			for (; i > 0 ; i--)
207
			{
208
				d[2] = d[1] = d[0] = *s++;
209
				d[3] = *s++;
210
				d += 4;
211
			}
5496 leency 212
			blit(window_center + Form.client_left,
213
				Form.client_top + TOOLBAR_HEIGHT,
214
				Form.client_width,
215
				Form.client_height - TOOLBAR_HEIGHT,
216
		 		gapp.panx,
217
		 		gapp.pany,
218
				gapp.image->w,
219
				gapp.image->h,
220
				gapp.image->w * 4,
221
				color
222
			);
4680 right-hear 223
			free(color);
224
		}
5508 leency 225
	}
226
}
227
 
228
 
229
void DrawPageSides(void)
230
{
231
	if (Form.client_width > gapp.image->w) window_center = (Form.client_width - gapp.image->w) / 2; else window_center = 0;
232
	if (gapp.image->h < Form.client_height - TOOLBAR_HEIGHT) draw_h = gapp.image->h - gapp.pany; else draw_h = Form.client_height - TOOLBAR_HEIGHT;
233
	if (gapp.image->w < Form.client_width)
5496 leency 234
	{
5508 leency 235
		__menuet__bar(0, TOOLBAR_HEIGHT, window_center-1, Form.client_height - TOOLBAR_HEIGHT, DOCUMENT_BG);
236
		__menuet__bar(window_center-1, TOOLBAR_HEIGHT, 1, draw_h, DOCUMENT_BORDER);
237
		__menuet__bar(window_center + gapp.image->w, TOOLBAR_HEIGHT, 1, draw_h, DOCUMENT_BORDER);
238
		__menuet__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 239
	}
5508 leency 240
	if (gapp.image->w < Form.client_width)
241
	{
242
		draw_w = gapp.image->w + 2;
243
	}
244
	else
245
	{
246
		window_center = 1;
247
		draw_w = Form.client_width;
248
	}
249
	__menuet__bar(window_center - 1, gapp.image->h - gapp.pany + TOOLBAR_HEIGHT, draw_w, 1, DOCUMENT_BORDER);
250
	__menuet__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 251
}
252
 
253
 
5508 leency 254
 
4680 right-hear 255
int main (void)
256
{
5496 leency 257
	char ii, mouse_wheels_state;
4680 right-hear 258
	char* original_command_line = *(char**)0x1C;
259
	__menuet__debug_out(original_command_line);
260
 
261
	char buf[128];
262
	int resolution = 72;
5496 leency 263
	int pageno = 1;
264
	fz_accelerate();
265
	__menuet__debug_out("PDF init\n");
266
	pdfapp_init(&gapp);
4680 right-hear 267
	gapp.scrw = 600;
268
	gapp.scrh = 400;
269
	gapp.resolution = resolution;
270
	gapp.pageno = pageno;
271
	__menuet__debug_out("PDF Open\n");
272
	pdfapp_open(&gapp, original_command_line, 0, 0);
5496 leency 273
	__menuet__debug_out("PDF Opened\n");
5508 leency 274
	wintitle(&gapp, 0);
4680 right-hear 275
 
5496 leency 276
 
277
	__menuet__debug_out("Inital paint\n");
4680 right-hear 278
 
5496 leency 279
	int butt, key, screen_max_x, screen_max_y;
280
	__menuet__get_screen_max(&screen_max_x, &screen_max_y);
281
	__menuet__set_bitfield_for_wanted_events(EVENT_REDRAW+EVENT_KEY+EVENT_BUTTON+EVENT_MOUSE_CHANGE);
4680 right-hear 282
 
283
 for(;;)
284
 {
285
 
5496 leency 286
	switch(__menuet__wait_for_event())
287
	{
288
		case evReDraw:
5508 leency 289
			// gapp.shrinkwrap = 2;
5496 leency 290
			__menuet__window_redraw(1);
291
			__menuet__define_window(screen_max_x / 2 - 350, screen_max_y / 2 - 300, 700, 600, 0x73000000, 0x800000FF, Title);
292
			__menuet__window_redraw(2);
293
			__menuet__get_process_table(&Form, PID_WHOAMI);
294
			if (Form.window_state > 2) continue; //fix rolled up
295
			Form.client_width++; //fix for Menuet kernel bug
296
			Form.client_height++; //fix for Menuet kernel bug
297
			DrawWindow();
298
			break;
299
 
300
		case evKey:
301
			key = __menuet__getkey();
5508 leency 302
			if (key_mode_enter_page_number)
303
			{
304
				HandleNewPageNumber(key);
305
				break;
306
			}
5496 leency 307
			if (key==ASCII_KEY_ESC)  DrawWindow(); //close help
308
			if (key==ASCII_KEY_PGDN) pdfapp_onkey(&gapp, ']');
309
			if (key==ASCII_KEY_PGUP) pdfapp_onkey(&gapp, '[');
310
			if (key==ASCII_KEY_HOME) pdfapp_onkey(&gapp, 'g');
311
			if (key==ASCII_KEY_END ) pdfapp_onkey(&gapp, 'G');
5497 leency 312
			if (key=='g' ) pdfapp_onkey(&gapp, 'c');
313
			if ((key=='[' ) || (key=='l')) PageRotateLeft();
314
			if ((key==']' ) || (key=='r')) PageRotateRight();
5496 leency 315
			if (key==ASCII_KEY_DOWN ) PageScrollDown();
316
			if (key==ASCII_KEY_UP ) PageScrollUp();
317
			if (key=='-') PageZoomOut();
318
			if ((key=='=') || (key=='+')) PageZoomIn();
319
			break;
320
 
321
		case evButton:
322
			butt = __menuet__get_button_id();
323
			if(butt==1) __menuet__sys_exit();
324
			if(butt==10) ;//mag open file
325
			if(butt==11) PageZoomOut(); //magnify -
326
			if(butt==12) PageZoomIn(); //magnify +
327
			if(butt==13) //show help
328
			{
329
				__menuet__bar(0, TOOLBAR_HEIGHT, Form.client_width, Form.client_height - TOOLBAR_HEIGHT, 0xF2F2F2);
5508 leency 330
				__menuet__write_text(20, TOOLBAR_HEIGHT + 20      , 0x90000000, "uPDF for KolibriOS v1.2", 0);
331
				__menuet__write_text(21, TOOLBAR_HEIGHT + 20      , 0x90000000, "uPDF for KolibriOS v1.2", 0);
5496 leency 332
				for (ii=0; help[ii]!=0; ii++) {
333
					__menuet__write_text(20, TOOLBAR_HEIGHT + 60 + ii * 15, 0x80000000, help[ii], 0);
4680 right-hear 334
				}
5496 leency 335
			}
336
			if(butt==14) pdfapp_onkey(&gapp, '['); //previous page
337
			if(butt==15) pdfapp_onkey(&gapp, ']'); //next page
5508 leency 338
			if(butt==16) PageRotateLeft();
339
			if(butt==17) PageRotateRight();
340
			if(butt==20) GetNewPageNumber();
5496 leency 341
			break;
342
 
343
		case evMouse:
344
			if (mouse_wheels_state = __menuet__get_mouse_wheels())
345
			{
346
				if (mouse_wheels_state==1) { PageScrollDown(); PageScrollDown(); }
347
				if (mouse_wheels_state==-1) { PageScrollUp();  PageScrollUp();   }
348
			}
349
			//sprintf (debugstr, "mouse_wheels_state: %d \n", mouse_wheels_state);
350
			//__menuet__debug_out(debugstr);
351
			//pdfapp_onmouse(&gapp, int x, int y, int btn, int modifiers, int state)
352
			break;
353
	}
4680 right-hear 354
  }
355
}
5496 leency 356
 
357
 
5508 leency 358
void GetNewPageNumber(void)
359
{
360
	new_page_number = gapp.pageno;
361
	key_mode_enter_page_number = 1;
362
	HandleNewPageNumber(0);
363
}
5496 leency 364
 
5508 leency 365
void HandleNewPageNumber(unsigned char key)
5496 leency 366
{
5508 leency 367
	char label_new_page[8];
368
 
369
	if ((key >= '0') && (key <= '9'))
5496 leency 370
	{
5508 leency 371
		new_page_number = new_page_number * 10 + key - '0';
5496 leency 372
	}
5508 leency 373
	if (key == ASCII_KEY_BS)
374
	{
375
		new_page_number /= 10;
376
	}
377
	if (key == ASCII_KEY_ENTER)
378
	{
379
		ApplyNewPageNumber();
380
		return;
381
	}
382
	if (key==ASCII_KEY_ESC)
383
	{
384
		key_mode_enter_page_number = 0;
385
		DrawWindow();
386
		return;
387
	}
388
 
389
	itoa(new_page_number, label_new_page, 10);
390
	strcat(label_new_page, "_");
391
	__menuet__bar(show_area_x,  6, show_area_w, 22, 0xFDF88E);
392
	__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));
393
 
394
	if (new_page_number > gapp.pagecount) ApplyNewPageNumber();
5496 leency 395
}
396
 
5508 leency 397
void ApplyNewPageNumber(void)
398
{
399
	key_mode_enter_page_number = 0;
400
	gapp.pageno = new_page_number -1;
401
	pdfapp_onkey(&gapp, ']');
402
}
5496 leency 403
 
404
void DrawPagination(void)
405
{
406
	char pages_display[12];
407
	__menuet__bar(show_area_x,  6, show_area_w, 22, 0xF4F4F4);
408
	sprintf (pages_display, "%d/%d", gapp.pageno, gapp.pagecount);
409
	__menuet__write_text(show_area_x + show_area_w/2 - strlen(pages_display)*6/2, 14, 0x000000, pages_display, strlen(pages_display));
410
}
411
 
412
 
5508 leency 413
 
414
 
5496 leency 415
void DrawWindow(void)
416
{
417
	__menuet__bar(0, 0, Form.client_width, TOOLBAR_HEIGHT - 1, 0xe1e1e1); // bar on the top (buttons holder)
418
	__menuet__bar(0, TOOLBAR_HEIGHT - 1, Form.client_width, 1, 0x7F7F7F);
5508 leency 419
	DrawToolbarButton(8,0); //open_folder
420
	DrawToolbarButton(42,1); //magnify -
421
	DrawToolbarButton(67,2);  //magnify +
422
	DrawToolbarButton(101,6); //rotate left
423
	DrawToolbarButton(126,7); //rotate right
424
	DrawToolbarButton(Form.client_width - 160,3); //show help
425
	show_area_x = Form.client_width - show_area_w - 34;
426
	DrawToolbarButton(show_area_x - 26,4); //prev page
427
	DrawToolbarButton(show_area_x + show_area_w,5); //nex page
428
	__menuet__make_button(show_area_x-1,  5, show_area_w+1, 23, 20 + BT_HIDE, 0xA4A4A4);
429
	__menuet__bar(show_area_x,  5, show_area_w, 1, 0xA4A4A4);
430
	__menuet__bar(show_area_x, 28, show_area_w, 1, 0xA4A4A4);
5496 leency 431
	winblit(&gapp);
432
	DrawPageSides();
433
}
434
 
5508 leency 435
void DrawToolbarButton(int x, char image_id)
5496 leency 436
{
5508 leency 437
	__menuet__make_button(x, 5, 26-1, 24-1, 10 + image_id + BT_HIDE, 0);
438
	__menuet__putimage(x, 5, 26, 24, image_id * 24 * 26 * 3 + toolbar_image);
5496 leency 439
}
440
 
441
 
442
/* Actions */
443
 
444
void PageScrollDown(void)
445
{
5497 leency 446
	//pdfapp_onkey(&gapp, 'k'); //move down
5496 leency 447
	if (gapp.image->h - gapp.pany - SCROLL_H < Form.client_height - TOOLBAR_HEIGHT)
448
	{
449
		pdfapp_onkey(&gapp, '.');
450
	}
451
	else {
452
		gapp.pany += SCROLL_H;
453
		winblit(&gapp);
454
	}
455
}
456
 
457
 
458
void PageScrollUp(void)
459
{
5497 leency 460
	//pdfapp_onkey(&gapp, 'j'); //move up
5496 leency 461
	if (gapp.pany >= SCROLL_H) {
462
		gapp.pany -= SCROLL_H;
463
		winblit(&gapp);
464
	}
465
	else {
5497 leency 466
		//not very nice way of using do_not_blit, but it simple
5496 leency 467
		if (gapp.pageno == 1) return;
468
		do_not_blit = 1;
469
		pdfapp_onkey(&gapp, ',');
470
		do_not_blit = 0;
471
		gapp.pany = gapp.image->h - SCROLL_H - Form.client_height + TOOLBAR_HEIGHT;
472
		if (gapp.pany < 0) gapp.pany = 0;
473
		//sprintf (debugstr, "gapp.pany: %d \n", gapp.pany);
474
		//__menuet__debug_out(debugstr);
475
		winblit(&gapp);
476
	}
477
}
478
 
479
 
480
void PageZoomIn(void)
481
{
482
	pdfapp_onkey(&gapp, '+');
483
	DrawPageSides();
484
}
485
 
486
 
487
void PageZoomOut(void)
488
{
489
	pdfapp_onkey(&gapp, '-');
490
	DrawPageSides();
491
}
492
 
5497 leency 493
void PageRotateLeft(void)
494
{
495
	pdfapp_onkey(&gapp, 'L');
496
	DrawPageSides();
497
}
5496 leency 498
 
5497 leency 499
void PageRotateRight(void)
500
{
501
	pdfapp_onkey(&gapp, 'R');
502
	DrawPageSides();
503
}
504