Subversion Repositories Kolibri OS

Rev

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