Subversion Repositories Kolibri OS

Rev

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