Subversion Repositories Kolibri OS

Rev

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

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