forked from coo-ona/cooViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccessoryView.m
931 lines (848 loc) · 28.7 KB
/
AccessoryView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
//
// AccessoryView.m
// cooViewer
//
// Created by coo on 08/02/12.
// Copyright 2008 coo. All rights reserved.
//
#import "AccessoryView.h"
#import "Controller.h"
#import "CustomImageView.h"
#import "NSBezierPath_Adding.h"
#import "NSAttributedString_Adding.h"
@implementation AccessoryView
NSRect COIntRect(NSRect aRect)
{
NSRect tempRect = aRect;
tempRect.origin.x = (int)(tempRect.origin.x+0.5);
tempRect.origin.y = (int)(tempRect.origin.y+0.5);
tempRect.size.width = (int)(tempRect.size.width+0.5);
tempRect.size.height = (int)(tempRect.size.height+0.5);
return tempRect;
}
- (void)setPreferences
{
if (!didFirst) {
didFirst = YES;
pageBarBGColor = nil;
pageBarBorderColor = nil;
pageBarReadedColor = nil;
pageBarFontColor = nil;
pageBarFont = nil;
textFontColor = nil;
textFont = nil;
textBGColor = nil;
textBorderColor = nil;
autoHidePageBar = NO;
autoHidedPageBar = NO;
autoHidePageString = NO;
autoHidedPageString = NO;
pageString = nil;
infoString = nil;
pageBarRect = NSZeroRect;
pageStringRect = NSZeroRect;
infoStringRect = NSZeroRect;
pageMoverRect = NSZeroRect;
pageBarStringRect = NSZeroRect;
pageStringPosition = 0;
pageBarBezierPath = nil;
//pageStringBezierPath = nil;
pageStringAttr = nil;
//pageBarStringAttr = nil;
accessoryTimer = nil;
} else {
[pageBarBGColor release];
[pageBarBorderColor release];
[pageBarReadedColor release];
[pageBarFontColor release];
[pageBarFont release];
[textFontColor release];
[textFont release];
[textBGColor release];
[textBorderColor release];
[pageBarBezierPath release];
//[pageStringBezierPath release];
[pageStringAttr release];
//[pageBarStringAttr release];
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
autoHidePageBar = [defaults boolForKey:@"PageBarAutoHide"];
autoHidePageString = [defaults boolForKey:@"PageNumAutoHide"];
pageBarShowThumbnail = (int)[defaults integerForKey:@"PageBarShowThumbnail"];
pageBarPosition = (int)[defaults integerForKey:@"PageBarPosition"];
NSDictionary *dic = [defaults dictionaryForKey:@"PageBarSize"];
pageBarWidth = [[dic objectForKey:@"width"] intValue];
pageBarHeight = [[dic objectForKey:@"height"] intValue];
if ([defaults objectForKey:@"PageBarFontColor"]) {
pageBarFontColor = [[NSUnarchiver unarchiveObjectWithData:[defaults objectForKey:@"PageBarFontColor"]] retain];
} else {
pageBarFontColor = [[NSColor whiteColor] retain];
}
if ([defaults objectForKey:@"PageBarTextFont"]) {
pageBarFont = [[NSUnarchiver unarchiveObjectWithData:[defaults objectForKey:@"PageBarTextFont"]] retain];
} else {
pageBarFont = [[NSFont userFontOfSize:14] retain];
}
if ([defaults objectForKey:@"PageBarBGColor"]) {
pageBarBGColor = [[NSUnarchiver unarchiveObjectWithData:[defaults objectForKey:@"PageBarBGColor"]] retain];
} else {
pageBarBGColor = [[[NSColor blackColor] colorWithAlphaComponent:0.8] retain];
}
if ([defaults objectForKey:@"PageBarBorderColor"]) {
pageBarBorderColor = [[NSUnarchiver unarchiveObjectWithData:[defaults objectForKey:@"PageBarBorderColor"]] retain];
} else {
pageBarBorderColor = [[NSColor whiteColor] retain];
}
if ([defaults objectForKey:@"PageBarReadedColor"]) {
pageBarReadedColor = [[NSUnarchiver unarchiveObjectWithData:[defaults objectForKey:@"PageBarReadedColor"]] retain];
} else {
pageBarReadedColor = [[[NSColor whiteColor] colorWithAlphaComponent:0.5] retain];
}
if ([defaults objectForKey:@"TextFont"]) {
textFont = [[NSUnarchiver unarchiveObjectWithData:[defaults objectForKey:@"TextFont"]] retain];
} else {
textFont = [[NSFont controlContentFontOfSize:11] retain];
}
if ([defaults objectForKey:@"TextColor"]) {
textFontColor = [[NSUnarchiver unarchiveObjectWithData:[defaults objectForKey:@"TextColor"]] retain];
} else {
textFontColor = [[NSColor whiteColor] retain];
}
if ([defaults objectForKey:@"TextBGColor"]) {
textBGColor = [[NSUnarchiver unarchiveObjectWithData:[defaults objectForKey:@"TextBGColor"]] retain];
} else {
textBGColor = [[[NSColor blackColor] colorWithAlphaComponent:0.8] retain];
}
if ([defaults objectForKey:@"TextBorderColor"]) {
textBorderColor = [[NSUnarchiver unarchiveObjectWithData:[defaults objectForKey:@"TextBorderColor"]] retain];
} else {
textBorderColor = [[NSColor whiteColor] retain];
}
pageStringPosition = (int)[defaults integerForKey:@"PageNumPosition"];
pageMargin = NSZeroPoint;
if ([defaults dictionaryForKey:@"Margin_Page"]) {
pageMargin.x=[[[defaults dictionaryForKey:@"Margin_Page"] objectForKey:@"x"] intValue];
pageMargin.y=[[[defaults dictionaryForKey:@"Margin_Page"] objectForKey:@"y"] intValue];
} else {
[defaults setObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:0],@"x",[NSNumber numberWithInt:0],@"y",nil]
forKey:@"Margin_Page"];
}
pageBarMargin=NSZeroPoint;
if ([defaults dictionaryForKey:@"Margin_PageBar"]) {
pageBarMargin.x=[[[defaults dictionaryForKey:@"Margin_PageBar"] objectForKey:@"x"] intValue];
pageBarMargin.y=[[[defaults dictionaryForKey:@"Margin_PageBar"] objectForKey:@"y"] intValue];
} else {
[defaults setObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:0],@"x",[NSNumber numberWithInt:0],@"y",nil]
forKey:@"Margin_PageBar"];
}
pageBarRect = [self pageBarRect];
NSRect innerRect = NSInsetRect(pageBarRect,1,1);
pageBarBezierPath = [[NSBezierPath bezierPathWithRectWithDoubleArc:innerRect] retain];
[pageBarBezierPath closePath];
if ([textBGColor isEqualTo:[NSColor clearColor]]) {
NSColor *shadowColor = [textFontColor colorUsingColorSpaceName:NSCalibratedWhiteColorSpace];
CGFloat white,alpha;
[shadowColor getWhite:&white alpha:&alpha];
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowBlurRadius:white];
[shadow setShadowColor:textFontColor];
pageStringAttr = [[NSDictionary dictionaryWithObjectsAndKeys:
textFontColor,NSForegroundColorAttributeName,
textFont,NSFontAttributeName,
shadow,NSShadowAttributeName,
nil] retain];
[shadow release];
} else {
pageStringAttr = [[NSDictionary dictionaryWithObjectsAndKeys:
textFontColor,NSForegroundColorAttributeName,
textFont,NSFontAttributeName,
nil] retain];
}
if (didFirst) [self setPageString:[self pageString]];
if (autoHidePageBar) {
autoHidedPageBar=YES;
} else {
autoHidedPageBar=NO;
}
if (autoHidePageString) {
autoHidedPageString=YES;
} else {
autoHidedPageString=NO;
}
[self setPageString:[pageString string]];
[self display];
}
- (void)setFrame:(NSRect)frameRect
{
[super setFrame:frameRect];
[pageBarBezierPath release];
pageBarRect = [self pageBarRect];
NSRect innerRect = NSInsetRect(pageBarRect,1,1);
pageBarBezierPath = [[NSBezierPath bezierPathWithRectWithDoubleArc:innerRect] retain];
[pageBarBezierPath closePath];
}
- (void)mouseMoved:(NSEvent *)theEvent
{
NSDisableScreenUpdates();
NSPoint lensOldPoint;
if ([controller indicator] && ![imageView loupeIsVisible]) {
lensOldPoint = [[self window] mouseLocationOutsideOfEventStream];
if (autoHidedPageBar) {
autoHidedPageBar = NO;
[self displayRect:[self pageBarRect]];
}
if (autoHidedPageString) {
autoHidedPageString = NO;
[self displayRect:[self pageStringRect]];
[self setInfoString:[infoString string]];
}
if (autoHidePageBar || autoHidePageString) {
[accessoryTimer invalidate];
accessoryTimer = [NSTimer scheduledTimerWithTimeInterval:2
target:self
selector:@selector(hideAccessory)
userInfo:nil
repeats:NO];
}
NSRect tempPageBarRect = NSInsetRect([self pageBarRect],2,2);
if (NSPointInRect(lensOldPoint, tempPageBarRect)) {
[self display];
} else {
if (!NSIsEmptyRect(pageBarStringRect)) {
[self display];
}
}
}
NSEnableScreenUpdates();
}
- (void)drawPageBarBubble
{
NSPoint lensOldPoint;
if (!NSIsEmptyRect(pageBarStringRect)) {
pageBarStringRect = NSZeroRect;
}
if ([controller indicator] && ![imageView loupeIsVisible]) {
lensOldPoint = [[self window] mouseLocationOutsideOfEventStream];
NSRect tempPageBarRect = NSInsetRect([self pageBarRect],2,2);
if (NSPointInRect(lensOldPoint, tempPageBarRect)) {
[self resetCursorRects];
NSPoint tempPoint = lensOldPoint;
NSRect tempRect = tempPageBarRect;
float temp = tempPoint.x - tempRect.origin.x;
if (![controller readFromLeft]) {
temp = tempRect.size.width - temp-1;
}
temp = temp/tempRect.size.width;
float fPage = [(Controller *)controller pageCount]*temp;
int page = (int)fPage;
NSMutableDictionary* attr = [NSMutableDictionary dictionary];
[attr setObject:pageBarFontColor forKey:NSForegroundColorAttributeName];
[attr setObject:pageBarFont forKey:NSFontAttributeName];
if ([pageBarBGColor isEqualTo:[NSColor clearColor]]) {
NSColor *shadowColor = [pageBarFontColor colorUsingColorSpaceName:NSCalibratedWhiteColorSpace];
CGFloat white,alpha;
[shadowColor getWhite:&white alpha:&alpha];
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowBlurRadius:white];
[shadow setShadowColor:pageBarFontColor];
[attr setObject:shadow forKey:NSShadowAttributeName];
[shadow release];
}
NSAttributedString *string = [[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@" %i ",page+1] attributes:attr] autorelease];
NSPoint mouseOrigin = [[[self window] contentView] convertPoint:lensOldPoint fromView:self];
mouseOrigin.x = (int)mouseOrigin.x;
mouseOrigin.y = (int)mouseOrigin.y;
NSPoint pt = mouseOrigin;
pt.y = tempRect.origin.y;
int twidth = 6;
int theight = 6;
int tMargin = 10;
int basemargin=7;
if (pageBarShowThumbnail) {
switch (pageBarPosition) {
case 0:case 1:
pt.y+=tempRect.size.height/2;
break;
case 2:case 3:
pt.y-=5+tempRect.size.height/2;
break;
}
twidth = 10;
theight = 10;
float rad = 10.0;
NSImage *thumbnail = [controller loadThumbnailImage:page];
NSInteger widthValue = [thumbnail size].width;
NSInteger heightValue = [thumbnail size].height;
float imageRectWidth = 200;
float imageRectHeight = 200;
float width,height;
float rate = imageRectWidth/widthValue;
width = widthValue*rate;
height = heightValue*rate;
if (height>imageRectHeight) {
rate = imageRectHeight/heightValue;
width = widthValue*rate;
height = heightValue*rate;
}
width = (int)width;
height = (int)height;
NSRect thumbnailRect=NSMakeRect(pt.x,pt.y,width+4+4,height+5+1+1+[string size].height);
NSRect imageRect = NSZeroRect;
switch (pageBarPosition) {
case 0:
thumbnailRect.origin.y -= thumbnailRect.size.height+theight;
thumbnailRect.origin.x -= tMargin+rad+twidth/2-basemargin;
break;
case 1:
thumbnailRect.origin.y -= thumbnailRect.size.height+theight;
thumbnailRect.origin.x -= thumbnailRect.size.width-tMargin-rad-twidth/2+basemargin;
break;
case 2:
thumbnailRect.origin.y += [self pageBarRect].size.height+theight;
thumbnailRect.origin.x -= tMargin+rad+twidth/2-basemargin;
break;
case 3:
thumbnailRect.origin.y += [self pageBarRect].size.height+theight;
thumbnailRect.origin.x -= thumbnailRect.size.width-tMargin-rad-twidth/2+basemargin;
break;
default:
break;
}
if ([string size].width > thumbnailRect.size.width) thumbnailRect.size.width=(int)[string size].width;
if (thumbnailRect.origin.x < 5) thumbnailRect.origin.x = 5;
if (thumbnailRect.origin.x+thumbnailRect.size.width+5 > [self frame].size.width) {
float x = [self frame].size.width - (thumbnailRect.size.width+5);
thumbnailRect.origin.x = x;
}
pt.x=thumbnailRect.origin.x+2;
pt.y=thumbnailRect.origin.y+thumbnailRect.size.height-2-[string size].height;
imageRect = thumbnailRect;
imageRect.origin.x += 5;
imageRect.origin.y += 5;
imageRect.size.width = width;
imageRect.size.height = height;
NSBezierPath *bezier;
NSPoint tl,tt,tr;
if (pageBarPosition < 2) {
bezier = [NSBezierPath bezierPathWithRectWithArc:thumbnailRect rad:rad open:1];
tt = NSMakePoint(mouseOrigin.x,thumbnailRect.origin.y+thumbnailRect.size.height+theight);
switch (pageBarPosition) {
case 0://左上
tl = NSMakePoint(thumbnailRect.origin.x+rad+tMargin,tt.y-theight);
tr = NSMakePoint(tl.x+twidth,tl.y);
break;
case 1://右上
tr = NSMakePoint(thumbnailRect.origin.x+thumbnailRect.size.width-rad-tMargin,tt.y-theight);
tl = NSMakePoint(tr.x-twidth,tr.y);
break;
default:
break;
}
[bezier appendBezierPathWithPoints:&tr count:1];
[bezier appendBezierPathWithPoints:&tt count:1];
[bezier appendBezierPathWithPoints:&tl count:1];
} else {
bezier = [NSBezierPath bezierPathWithRectWithArc:thumbnailRect rad:rad open:3];
tt = NSMakePoint(mouseOrigin.x,thumbnailRect.origin.y-theight);
switch (pageBarPosition) {
case 2://左下
tl = NSMakePoint(thumbnailRect.origin.x+rad+tMargin,tt.y+theight);
tr = NSMakePoint(tl.x+twidth,tl.y);
break;
case 3://右下
tr = NSMakePoint(thumbnailRect.origin.x+thumbnailRect.size.width-rad-tMargin,tt.y+theight);
tl = NSMakePoint(tr.x-twidth,tr.y);
break;
default:
break;
}
[bezier appendBezierPathWithPoints:&tl count:1];
[bezier appendBezierPathWithPoints:&tt count:1];
[bezier appendBezierPathWithPoints:&tr count:1];
}
[bezier closePath];
pageBarStringRect = NSInsetRect(COIntRect([bezier bounds]),-3,-3);
if (![pageBarBGColor isEqualTo:[NSColor clearColor]]) {
[pageBarBGColor set];
[bezier fill];
}
if (![pageBarBorderColor isEqualTo:[NSColor clearColor]]) {
[pageBarBorderColor set];
[bezier stroke];
}
[thumbnail drawInRect:imageRect
fromRect:NSMakeRect(0,0,widthValue,heightValue)
operation:NSCompositeSourceOver fraction:1.0];
[string drawAtPoint:pt];
} else {
switch (pageBarPosition) {
case 0:case 1:
pt.y+=tempRect.size.height/2;
break;
case 2:case 3:
pt.y-=tempRect.size.height/2;
break;
}
int stringSize = [string size].width;
if (stringSize < twidth+tMargin*2) tMargin = (stringSize-twidth)/2;
switch (pageBarPosition) {
case 0:
pt.x += basemargin;
pt.y -= [string size].height+theight;
pt.x -= tMargin+twidth/2;
//pt.x += 15;
break;
case 1:
pt.x -= basemargin;
pt.y -= [string size].height+theight;
pt.x += tMargin+twidth/2;
pt.x -= stringSize;
//pt.x -= 10;
break;
case 2:
pt.x += basemargin;
pt.y += [self pageBarRect].size.height+1;
pt.x -= tMargin+twidth/2;
break;
case 3:
pt.x -= basemargin;
pt.y += [self pageBarRect].size.height+1;
pt.x += tMargin+twidth/2;
pt.x -= stringSize;
break;
default:
break;
}
if (pt.x < 5) pt.x = 5;
if (pt.x + stringSize + 5> [self frame].size.width) {
float x = [self frame].size.width - (stringSize+5);
pt.x = x;
}
NSBezierPath *bezier;
NSRect temp = NSMakeRect(pt.x,pt.y,[string size].width,[string size].height);
NSPoint tl,tt,tr;
if (pageBarPosition >= 2) {
bezier = [NSBezierPath bezierPathWithRectWithArc:temp rad:0 open:3];
tt = NSMakePoint(mouseOrigin.x,pt.y-theight);
switch (pageBarPosition) {
case 2://左下
tl = NSMakePoint(pt.x+tMargin,tt.y+theight);
tr = NSMakePoint(tl.x+twidth,tl.y);
break;
case 3://右下
tr = NSMakePoint(pt.x+stringSize-tMargin,tt.y+theight);
tl = NSMakePoint(tr.x-twidth,tr.y);
break;
default:
break;
}
[bezier appendBezierPathWithPoints:&tl count:1];
[bezier appendBezierPathWithPoints:&tt count:1];
[bezier appendBezierPathWithPoints:&tr count:1];
} else {
bezier = [NSBezierPath bezierPathWithRectWithArc:temp rad:0 open:1];
tt = NSMakePoint(mouseOrigin.x,pt.y+[string size].height+theight);
switch (pageBarPosition) {
case 0://左上
tl = NSMakePoint(pt.x+tMargin,tt.y-theight);
tr = NSMakePoint(tl.x+twidth,tl.y);
break;
case 1://右上
tr = NSMakePoint(pt.x+stringSize-tMargin,tt.y-theight);
tl = NSMakePoint(tr.x-twidth,tr.y);
break;
default:
break;
}
[bezier appendBezierPathWithPoints:&tr count:1];
[bezier appendBezierPathWithPoints:&tt count:1];
[bezier appendBezierPathWithPoints:&tl count:1];
}
[bezier closePath];
pageBarStringRect = NSInsetRect(COIntRect([bezier bounds]),-5,-5);
if (![pageBarBGColor isEqualTo:[NSColor clearColor]]) {
[pageBarBGColor set];
[bezier fill];
}
if (![pageBarBorderColor isEqualTo:[NSColor clearColor]]) {
[pageBarBorderColor set];
[bezier stroke];
}
[string drawAtPoint:pt];
}
} else {
[self resetCursorRects];
}
}
}
-(void)drawAccessory
{
if (!NSIsEmptyRect(pageBarStringRect)) {
[self setNeedsDisplayInRect:pageBarStringRect];
pageBarStringRect = NSZeroRect;
}
if (!autoHidedPageBar) [self setNeedsDisplayInRect:[self pageBarRect]];
if (!autoHidedPageString) [self setNeedsDisplayInRect:[self pageStringRect]];
/*
if (!slideshow) {
[infoString release];
infoString = nil;
[self setNeedsDisplayInRect:infoStringRect];
}*/
}
-(void)drawRect:(NSRect)frameRect
{
if ([controller indicator] && [imageView image]) {
if (!autoHidedPageBar) {
pageBarRect = [self pageBarRect];
float nowPar = [controller nowPar];
float now = pageBarRect.size.width*nowPar;
NSBezierPath *base = pageBarBezierPath;
if (![pageBarBGColor isEqualTo:[NSColor clearColor]]) {
[pageBarBGColor set];
[base fill];
}
if (![pageBarReadedColor isEqualTo:[NSColor clearColor]]) {
NSRect rect = NSInsetRect(pageBarRect,1,1);
NSRect slice,remainder;
if ([controller readFromLeft]) {
NSDivideRect(rect, &slice, &remainder, now, NSMinXEdge);
} else {
NSDivideRect(rect, &slice, &remainder, now, NSMaxXEdge);
}
[NSGraphicsContext saveGraphicsState];
[base addClip];
[pageBarReadedColor set];
NSRectFillUsingOperation(slice,NSCompositeSourceOver);
[NSGraphicsContext restoreGraphicsState];
}
if (![pageBarBorderColor isEqualTo:[NSColor clearColor]]) {
[pageBarBorderColor set];
[base stroke];
}
} else {
pageBarRect = NSZeroRect;
}
} else {
//pageBarRect = NSZeroRect;
}
if (pageMover && tempPageNum>=0) {
pageMoverRect = [self pageMoverRect];
pageMoverRect = NSInsetRect(pageMoverRect,4,4);
float rad = 10.0;
NSBezierPath *bezier = [NSBezierPath bezierPathWithRectWithArc:pageMoverRect rad:rad open:0];
[bezier closePath];
if (![pageBarBGColor isEqualTo:[NSColor clearColor]]) {
[pageBarBGColor set];
[bezier fill];
}
if (![pageBarBorderColor isEqualTo:[NSColor clearColor]]) {
[pageBarBorderColor set];
[bezier stroke];
}
pageMoverRect = NSInsetRect([bezier bounds],-4,-4);
if (tempPageNum != 0) {
NSMutableDictionary* attr = [NSMutableDictionary dictionary];
[attr setObject:pageBarFontColor forKey:NSForegroundColorAttributeName];
[attr setObject:[NSFont fontWithName:[pageBarFont fontName] size:[pageBarFont pointSize]+10] forKey:NSFontAttributeName];
NSAttributedString *string = [[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%i",tempPageNum] attributes:attr] autorelease];
NSRect tempRect = pageMoverRect;
NSPoint pt;
pt = NSMakePoint((tempRect.size.width-[string size].width)/2+tempRect.origin.x,tempRect.origin.y+(tempRect.size.height-[string size].height)/2);
[string drawAtPoint:pt];
}
} else if (!pageMover) {
pageMoverRect = NSZeroRect;
}
if (pageString && [imageView image]) {
if (!autoHidedPageString) {
pageStringRect = [self pageStringRect];
[pageString drawAtPoint:pageStringRect.origin bg:textBGColor border:textBorderColor];
} else {
pageStringRect = NSZeroRect;
}
} else {
//pageStringRect = NSZeroRect;
}
if (infoString && [imageView image]) {
if (![[infoString string] isEqualToString:@""]) {
infoStringRect = [self infoStringRect];
[infoString drawAtPoint:infoStringRect.origin bg:textBGColor border:textBorderColor];
if (![[infoString string] isEqualToString:[infoStringTimer userInfo]]) {
[infoStringTimer invalidate];
infoStringTimer = [NSTimer scheduledTimerWithTimeInterval:2
target:self
selector:@selector(setZeroInfoString)
userInfo:[infoString string]
repeats:NO];
}
}
} else {
//infoStringRect = NSZeroRect;
}
[self drawPageBarBubble];
}
#pragma mark infoString
-(void)setZeroInfoString
{
infoStringTimer = nil;
NSRect oldRect=NSUnionRect(infoStringRect,pageStringRect);
[infoString release];
infoString = nil;
[self displayRect:NSUnionRect(NSUnionRect([self infoStringRect],[self pageStringRect]),oldRect)];
}
-(void)setSlideshow:(BOOL)b
{
slideshow = b;
if (slideshow == NO) {
[self setInfoString:@"stop slideshow"];
[self displayRect:NSUnionRect([self infoStringRect],[self pageStringRect])];
/*
NSRect temp = NSUnionRect(infoStringRect,[self pageStringRect]);
[infoString release];
infoString = nil;
infoStringRect = NSZeroRect;
[self displayRect:temp];
*/
} else {
[self setInfoString:@"start slideshow"];
[self displayRect:NSUnionRect([self infoStringRect],[self pageStringRect])];
}
}
-(void)setInfoString:(NSString*)string
{
if (!string) {
return;
}
NSRect oldRect=NSUnionRect(infoStringRect,pageStringRect);
if (!infoString) {
infoString = [[NSAttributedString alloc] initWithString:string attributes:pageStringAttr];
} else {
[infoString initWithString:string attributes:pageStringAttr];
}
if (NSIsEmptyRect(oldRect)) {
[self displayRect:NSUnionRect([self infoStringRect],[self pageStringRect])];
} else {
[self displayRect:NSUnionRect(NSUnionRect([self infoStringRect],[self pageStringRect]),oldRect)];
}
}
-(NSRect)infoStringRect
{
if (!infoString) return NSZeroRect;
NSRect contentFrame = [self frame];
NSRect rect = NSMakeRect(0,0,[infoString sizeWithBG].width,[infoString sizeWithBG].height);
rect.size.width = (int)rect.size.width + 1;
rect.size.height = (int)rect.size.height + 1;
switch (pageStringPosition) {
case 0:
rect.origin.x = pageMargin.x+2;
rect.origin.y = contentFrame.size.height-(int)rect.size.height-pageMargin.y;
break;
case 1:
rect.origin.x = contentFrame.size.width-(int)rect.size.width-pageMargin.x-2;
rect.origin.y = contentFrame.size.height-(int)rect.size.height-pageMargin.y;
break;
case 2:
rect.origin.x = pageMargin.x+2;
rect.origin.y = 17+pageMargin.y+2;
break;
case 3:
rect.origin.x = contentFrame.size.width-(int)rect.size.width-pageMargin.x-2;
rect.origin.y = 17+pageMargin.y+2;
break;
default:
break;
}
return COIntRect(rect);
}
#pragma mark accessory
-(void)hideAccessory
{
accessoryTimer = nil;
NSRect updateRect = NSZeroRect;
if (autoHidePageBar) {
autoHidedPageBar = YES;
if (NSEqualRects(updateRect,NSZeroRect)) {
updateRect = [self pageBarRect];
} else {
updateRect = NSUnionRect(updateRect,[self pageBarRect]);
}
}
if (autoHidePageString) {
autoHidedPageString = YES;
[self setInfoString:[infoString string]];
if (NSEqualRects(updateRect,NSZeroRect)) {
updateRect = [self pageStringRect];
} else {
updateRect = NSUnionRect(updateRect,[self pageStringRect]);
}
}
if (!NSEqualRects(updateRect,NSZeroRect)) {
[self displayRect:updateRect];
}
}
#pragma mark pageString
-(void)setPageString:(NSString*)string
{
if (!string) {
[pageString release];
pageString = nil;
[self setNeedsDisplayInRect:pageStringRect];
pageStringRect = NSZeroRect;
return;
}
NSRect oldRect=pageStringRect;
if (!pageString) {
pageString = [[NSAttributedString alloc] initWithString:string attributes:pageStringAttr];
} else {
[pageString initWithString:string attributes:pageStringAttr];
}
if (!autoHidedPageString) {
if (NSIsEmptyRect(oldRect)) {
[self setNeedsDisplayInRect:[self pageStringRect]];
} else {
[self setNeedsDisplayInRect:NSUnionRect([self pageStringRect],oldRect)];
}
}
}
-(NSString*)pageString
{
return [pageString string];
}
-(NSRect)pageStringRect
{
NSRect contentFrame = [self frame];
NSRect rect = NSMakeRect(0,0,[pageString sizeWithBG].width,[pageString sizeWithBG].height);
rect.size.width = rect.size.width + 1;
rect.size.height = rect.size.height + 1;
switch (pageStringPosition) {
case 0:
rect.origin.x = pageMargin.x+2 +[self infoStringRect].size.width;
rect.origin.y = contentFrame.size.height-rect.size.height-pageMargin.y;
break;
case 1:
rect.origin.x = contentFrame.size.width-rect.size.width-pageMargin.x-2 -[self infoStringRect].size.width;
rect.origin.y = contentFrame.size.height-rect.size.height-pageMargin.y;
break;
case 2:
rect.origin.x = pageMargin.x+2 +[self infoStringRect].size.width;
rect.origin.y = 17+pageMargin.y+2;
break;
case 3:
rect.origin.x = contentFrame.size.width-rect.size.width-pageMargin.x-2 -[self infoStringRect].size.width;
rect.origin.y = 17+pageMargin.y+2;
break;
default:
break;
}
return COIntRect(rect);
}
#pragma mark pageBar
-(void)drawPageBar
{
if (NSIsEmptyRect(pageBarRect)) {
[self setNeedsDisplayInRect:[self pageBarRect]];
} else {
[self setNeedsDisplayInRect:pageBarRect];
}
}
-(NSRect)pageBarRect
{
if ([controller indicator]) {
float width = pageBarWidth+1;
float height = pageBarHeight+1;
NSRect rect;
NSRect contentFrame = [self frame];
switch (pageBarPosition) {
case 0:
rect = NSMakeRect(contentFrame.origin.x+pageBarMargin.x+2,
contentFrame.size.height-17-height-pageBarMargin.y-3,
width,height);
break;
case 1:
rect = NSMakeRect(contentFrame.origin.x+contentFrame.size.width-width-pageBarMargin.x-3,
contentFrame.origin.y-17-height+contentFrame.size.height-pageBarMargin.y-3,
width,height);
break;
case 2:
rect = NSMakeRect(contentFrame.origin.x+pageBarMargin.x+2,
contentFrame.origin.y+pageBarMargin.y+2,
width,height);
break;
case 3:
rect = NSMakeRect(contentFrame.origin.x+contentFrame.size.width-width-pageBarMargin.x-3,
contentFrame.origin.y+pageBarMargin.y+2,
width,height);
break;
default:
rect = NSMakeRect(0,0,width,height);
break;
}
return COIntRect(rect);
}
return NSZeroRect;
}
-(NSRect)pageMoverRect
{
if (pageMover == YES) {
NSRect contentFrame = [self frame];
NSDictionary* attr = [NSDictionary dictionaryWithObjectsAndKeys:pageBarFontColor,NSForegroundColorAttributeName,
[NSFont fontWithName:[pageBarFont fontName] size:[pageBarFont pointSize]+10],NSFontAttributeName,nil];
NSAttributedString *string = [[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%i",tempPageNum] attributes:attr] autorelease];
NSRect rect = NSMakeRect(0,0,[string size].width+50,[string size].height+20);
float width = rect.size.width;
float height = rect.size.height;
rect = NSMakeRect((int)(((contentFrame.size.width-width)/2)+contentFrame.origin.x),
(int)(((contentFrame.size.height-height)/2)+contentFrame.origin.y),
(int)width,(int)height);
return rect;
}
return NSZeroRect;
}
-(void)drawPageMover:(int)page
{
if (page >= 0) {
pageMover = YES;
tempPageNum = tempPageNum*10;
tempPageNum = tempPageNum+page;
pageMoverRect = [self pageMoverRect];
} else if (page == -1) {
pageMover = NO;
tempPageNum = 0;
} else if (page == -2) {
tempPageNum = tempPageNum/10;
}
[self displayRect:pageMoverRect];
}
-(BOOL)pageMover
{
return pageMover;
}
-(int)tempPageNum
{
return tempPageNum;
}
#pragma mark -
- (void)resetCursorRects
{
[imageView resetCursorRects];
/*
[pageBarCursor release];
pageBarCursor = nil;
if (NSPointInRect([[self window] mouseLocationOutsideOfEventStream], [self pageBarRect])) {
NSImage *cursorImage = [[[NSImage alloc] initWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:@"cross" ofType:@"tiff"]] autorelease];
pageBarCursor = [[NSCursor alloc] initWithImage:cursorImage
hotSpot:NSMakePoint(7,8)];
[pageBarCursor set];
//[imageView addCursorRect:[imageView bounds] cursor:pageBarCursor];
} else {
[[NSCursor arrowCursor] set];
//[super resetCursorRects];
}*/
}
- (BOOL)isMouseInPageBar
{
return NSPointInRect([[self window] mouseLocationOutsideOfEventStream], [self pageBarRect]);
}
@end