forked from xcatsan/SimpleCap-1.2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleViewerController.m
1064 lines (913 loc) · 29.2 KB
/
SimpleViewerController.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
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// SimpleViewerController.m
// SimpleCap
//
// Created by - on 08/12/19.
// Copyright 2008 Hiroshi Hashiguchi. All rights reserved.
//
#import "SimpleViewerController.h"
#import "SimpleViewerPanel.h"
#import "SimpleViewerImageView.h"
#import "SimpleViewerInfoView.h"
#import "SimpleViewerBackgroundView.h"
#import "ThinButtonBar.h"
#import "ApplicationButtonPallete.h"
#import "ApplicationButtonCell.h"
#import "AppController.h"
#import "FileManager.h"
#import "ApplicationMenu.h"
#import "FilenameTextField.h"
#import "FileEntry.h"
#import "UserDefaults.h"
enum SV_TAG {
SV_TAG_TRASH,
SV_TAG_PREFERENCE,
SV_TAG_RETAKE,
SV_TAG_APPLICATION,
SV_TAG_NEXT_FILE,
SV_TAG_PREVIOUS_FILE,
SV_TAG_CLOSE,
SV_TAG_SAVE,
SV_TAG_CAPTURE,
SV_TAG_CONFIG,
SV_TAG_COPY,
SV_TAG_CAPTURE_AGAIN,
SV_TAG_OPERATION,
};
#define FILENAME_TEXTFIELD_HEIGHT 20
#define FILENAME_TEXTFIELD_WRATIO 1.0
#define FILENAME_TEXTFIELD_PADDING_Y 7
#define METALINE_MARGIN_X 0
#define METALINE_MARGIN_Y 3
#define METALINE_HEIGHT (FILENAME_TEXTFIELD_HEIGHT+METALINE_MARGIN_Y*2+2)
#define BUTTON_BAR_HEIGHT (12.0+13)
#define BUTTON_MARGIN_BOTTOM 8
#define BUTTON_MARGIN_LEFT 5
#define IMAGEVIEW_MARGIN_X 1
#define IMAGEVIEW_MARGIN_Y 1
@implementation SimpleViewerController
#pragma mark -
#pragma mark Handling FSEvent
-(void)showMessage:(NSString*)message
{
[_app_controller showMessage:message];
}
void fsevents_callback(
ConstFSEventStreamRef streamRef,
void *userData,
size_t numEvents,
void *eventPaths,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[])
{
// debug code
/*
int i;
char **paths = eventPaths;
NSLog(@"clicentCallBackInfo: %@", userData);
for (i=0; i<numEvents; i++) {
printf("Change %llu in %s, flags %lu\n",
eventIds[i], paths[i], eventFlags[i]);
}
*/
SimpleViewerController* svc = (SimpleViewerController*)userData;
if ([svc isOpened] && [svc updateOpendFile]) {
[svc showMessage:NSLocalizedString(@"FileUpdated", @"")];
}
}
- (void)updateFSEventStream
{
if (_fsevent_stream) {
FSEventStreamStop(_fsevent_stream);
FSEventStreamInvalidate(_fsevent_stream);
}
NSString* path = [UserDefaults valueForKey:UDKEY_IMAGE_LOCATION];
NSArray* pathsToWatch = [NSArray arrayWithObjects:path, nil];
void *selfPointer = (void*)self;
FSEventStreamContext context = {0, selfPointer, NULL, NULL, NULL};
NSTimeInterval latency = 3.0; /* Latency in seconds */
/*
SimpleCap[28181] (CarbonCore.framework) FSEventStreamCreate: _FSEventStreamCreate: ERROR: could not allocate 0 bytes for array of path strings
SimpleCap[28181] (CarbonCore.framework) FSEventStreamScheduleWithRunLoop(): failed assertion 'streamRef != NULL'
SimpleCap[28181] (CarbonCore.framework) FSEventStreamStart(): failed assertion 'streamRef != NULL'
*/
/* Create the stream, passing in a callback */
_fsevent_stream = FSEventStreamCreate(NULL,
&fsevents_callback,
&context,
(CFArrayRef)pathsToWatch,
kFSEventStreamEventIdSinceNow,
latency,
kFSEventStreamCreateFlagNone /* Flags explained in reference */
);
/* Create the stream before calling this. */
FSEventStreamScheduleWithRunLoop(_fsevent_stream,
CFRunLoopGetCurrent(),
kCFRunLoopDefaultMode
);
FSEventStreamStart(_fsevent_stream);
}
#pragma mark -
#pragma mark Handling KVO event
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
// keyPath=values.General_ImageLocation
[self updateFSEventStream];
}
#pragma mark -
#pragma mark Initialization and deallocation
- (id)init
{
self = [super init];
if (self) {
_panel = [[SimpleViewerPanel alloc] initWithController:self];
[_panel setDelegate:self];
_button_bar = [[[ThinButtonBar alloc] initWithFrame:NSZeroRect] autorelease];
/*
[_button_bar addButtonWithImageResource:@"viewer_close"
alterImageResource:@"viewer_close2"
tag:SV_TAG_CLOSE
tooltip:NSLocalizedString(@"ViewerClose", @"")
group:nil];
*/
[_button_bar addButtonWithImageResource:@"viewer_trash"
alterImageResource:@"viewer_trash2"
tag:SV_TAG_TRASH
tooltip:NSLocalizedString(@"ViewerTrash", @"")
group:nil
isActOnMouseDown:NO];
[_button_bar addButtonWithImageResource:@"viewer_repeat"
alterImageResource:@"viewer_repeat2"
tag:SV_TAG_CAPTURE_AGAIN
tooltip:NSLocalizedString(@"ViewerRepeat", @"")
group:@"REPEAT"
isActOnMouseDown:NO];
[_button_bar addButtonWithImageResource:@"viewer_repeat"
alterImageResource:@"viewer_repeat2"
tag:SV_TAG_RETAKE
tooltip:NSLocalizedString(@"ViewerRepeat", @"")
group:@"REPEAT"
isActOnMouseDown:NO];
[_button_bar addButtonWithImageResource:@"viewer_config"
alterImageResource:@"viewer_config2"
tag:SV_TAG_CONFIG
tooltip:NSLocalizedString(@"ViewerConfig", @"")
group:nil
isActOnMouseDown:YES];
/*
[_button_bar addButtonWithImageResource:@"viewer_preference"
alterImageResource:@"viewer_preference2"
tag:SV_TAG_PREFERENCE
tooltip:NSLocalizedString(@"ViewerPreference", @"")
group:nil];
*/
/*
[_button_bar addButtonWithImageResource:@"viewer_retake"
alterImageResource:@"viewer_retake2"
tag:SV_TAG_RETAKE
tooltip:NSLocalizedString(@"ViewerRetake", @"")
group:nil];
*/
/*
[_button_bar addButtonWithImageResource:@"viewer_application"
alterImageResource:@"viewer_application2"
tag:SV_TAG_APPLICATION
tooltip:NSLocalizedString(@"ViewerApplication", @"")
group:nil
isActOnMouseDown:YES];
*/
/*
[_button_bar addButtonWithImageResource:@"viewer_copy"
alterImageResource:@"viewer_copy2"
tag:SV_TAG_COPY
tooltip:NSLocalizedString(@"ViewerCopy", @"")
group:nil];
*/
[_button_bar addButtonWithImageResource:@"viewer_capture"
alterImageResource:@"viewer_capture2"
tag:SV_TAG_CAPTURE
tooltip:NSLocalizedString(@"ViewerCapture", @"")
group:nil
isActOnMouseDown:YES];
[_button_bar addButtonWithImageResource:@"viewer_operation"
alterImageResource:@"viewer_operation2"
tag:SV_TAG_OPERATION
tooltip:NSLocalizedString(@"ViewerOperation", @"")
group:nil
isActOnMouseDown:YES];
[_button_bar addButtonWithImageResource:@"viewer_left"
alterImageResource:@"viewer_left2"
tag:SV_TAG_PREVIOUS_FILE
tooltip:NSLocalizedString(@"ViewerPreviousFile", @"")
group:nil
isActOnMouseDown:NO];
[_button_bar addButtonWithImageResource:@"viewer_right"
alterImageResource:@"viewer_right2"
tag:SV_TAG_NEXT_FILE
tooltip:NSLocalizedString(@"ViewerNextFile", @"")
group:nil
isActOnMouseDown:NO];
_button_bar2 = [[[ThinButtonBar alloc] initWithFrame:NSZeroRect] autorelease];
/* [_button_bar2 addButtonWithImageResource:@"viewer_save"
alterImageResource:@"viewer_save2"
tag:SV_TAG_SAVE
tooltip:NSLocalizedString(@"ViewerSave", @"")
group:nil];
*/
_image_view = [[[SimpleViewerImageView alloc]
initWithFrame:NSZeroRect withController:self] autorelease];
_info_view = [[[SimpleViewerInfoView alloc] initWithFrame:NSZeroRect] autorelease];
_background_view = [[[SimpleViewerBackgroundView alloc] initWithFrame:NSZeroRect] autorelease];
_filename_textfiled = [[[FilenameTextField alloc] initWithController:self] autorelease];
NSView* content_view = [_panel contentView];
[content_view addSubview:_background_view];
[content_view addSubview:_image_view];
[content_view addSubview:_info_view];
[content_view addSubview:_filename_textfiled];
[content_view addSubview:_button_bar]; // must be top of view hierarchy
[content_view addSubview:_button_bar2];
[_button_bar setDelegate:self];
[_button_bar setPosition:SC_BUTTON_POSITION_LEFT_TOP];
[_button_bar setDrawOffset:NSMakePoint(-BUTTON_MARGIN_LEFT, -BUTTON_MARGIN_BOTTOM)];
[_button_bar setButtonBarWithFrame:[content_view frame]];
[_button_bar show];
[_button_bar2 setDelegate:self];
[_button_bar2 setPosition:SC_BUTTON_POSITION_RIGHT_TOP];
[_button_bar2 setDrawOffset:NSMakePoint(0, -BUTTON_MARGIN_BOTTOM)];
[_button_bar2 setButtonBarWithFrame:[content_view frame]];
[_button_bar2 show];
[_info_view setDirInfoOffsetX:[_button_bar size].width];
_is_init_panel_position = YES;
NSString* path = [[NSBundle mainBundle] pathForImageResource:@"dummy.png"];
_app_menu = [[ApplicationMenu alloc] initWithTargetPath:path
Delegate:self];
_app_pallete = [[ApplicationButtonPallete alloc] init];
_app_pallete.action = @selector(clickApplicationPalleteAtRow:withCell:);
_app_pallete.target = self;
[_app_pallete addToView:content_view];
}
return self;
}
- (void) dealloc
{
[_app_controller release];
[_app_menu release];
[_panel release];
[_filename release];
[_app_pallete release];
[super dealloc];
}
-(void)awakeFromNib
{
[_capture_app_menu setDelegate:self];
[_config_menu setDelegate:self];
[_context_menu setDelegate:self];
[_operation_menu setDelegate:self];
// initialize FSEvent handler
[self updateFSEventStream];
[UserDefaults addObserver:self forKey:UDKEY_IMAGE_LOCATION];
}
- (void)menuWillOpen:(NSMenu *)menu
{
if (menu == _capture_app_menu) {
[_app_controller updateApplicationMenu:_capture_app_menu];
}
}
//
// private methods
//
- (void)updateInfomation
{
int index = [[_app_controller fileManager] index];
int count = [[_app_controller fileManager] count];
if (count == 0) {
index = 0;
} else {
index++;
}
NSString* dir_info = [NSString stringWithFormat:@"%d / %d", index, count];
[_info_view setDirectoryInfomation:dir_info];
}
- (void)layoutViews
{
NSRect frame_content_view = [[_panel contentView] frame];
NSRect frame_image_view = frame_content_view;
frame_image_view.size.height -= BUTTON_BAR_HEIGHT + METALINE_HEIGHT;
frame_image_view.origin.y += BUTTON_BAR_HEIGHT + METALINE_HEIGHT;
[_background_view setFrame:frame_image_view];
frame_image_view.size.width -= IMAGEVIEW_MARGIN_X*2;
frame_image_view.origin.x += IMAGEVIEW_MARGIN_X;
frame_image_view.size.height -= IMAGEVIEW_MARGIN_Y*2;
frame_image_view.origin.y += IMAGEVIEW_MARGIN_Y;
[_image_view setFrame:frame_image_view];
NSRect header_frame = frame_content_view;
header_frame.size.height = FILENAME_TEXTFIELD_HEIGHT;
header_frame.size.width = (int)(frame_content_view.size.width*FILENAME_TEXTFIELD_WRATIO)-METALINE_MARGIN_X*2;
header_frame.origin.x = METALINE_MARGIN_X;
header_frame.origin.y += BUTTON_BAR_HEIGHT + FILENAME_TEXTFIELD_PADDING_Y;
[_filename_textfiled setFrame:header_frame];
NSRect frame_info_view = frame_content_view;
frame_info_view.size.height = BUTTON_BAR_HEIGHT + METALINE_HEIGHT;
[_info_view setFrame:frame_info_view];
[_info_view setRatio:[_image_view reductionRatio]];
CGFloat margin = [_info_view infoStringSize].width+2.0;
[_button_bar2 setButtonBarWithFrame:frame_content_view];
[_button_bar2 setDrawOffset:NSMakePoint(-margin, -BUTTON_MARGIN_BOTTOM)];
[_app_pallete updateLayout];
}
- (NSSize)minimumContentSize
{
NSSize size = NSMakeSize(340.0, 200.0);
return size;
}
- (NSSize)maximumContentSize
{
NSRect frame = [[NSScreen mainScreen] visibleFrame];
NSSize size;
size.width = (int)(frame.size.width * 0.8);
size.height = (int)(frame.size.height * 0.8);
return size;
}
- (NSSize)adjustContentSize:(NSSize)content_size
{
NSSize min_size = [self minimumContentSize];
if (content_size.width < min_size.width) {
content_size.width = min_size.width;
}
if (content_size.height < min_size.height) {
content_size.height = min_size.height;
}
return content_size;
}
- (void)adjustPanelFrameWithContentSize:(NSSize)content_size
{
// (1) adjust size
NSSize max_size = [self maximumContentSize];
CGFloat ratio_w = max_size.width / content_size.width;
CGFloat ratio_h = max_size.height / content_size.height;
if (ratio_w < 1.0 || ratio_h < 1.0) {
CGFloat ratio = fminf(ratio_w, ratio_h);
content_size.width *= ratio;
content_size.height *= ratio;
}
NSRect rect = NSZeroRect;
rect.size = [self adjustContentSize:content_size];
rect.size.height += BUTTON_BAR_HEIGHT;
NSRect panel_frame = [_panel frameRectForContentRect:rect];
// (2) adjust position
NSRect screen_frame = [[NSScreen mainScreen] visibleFrame];
panel_frame.origin.x = (screen_frame.size.width - panel_frame.size.width) / 2.0;
panel_frame.origin.y = (screen_frame.size.height - panel_frame.size.height) / 2.0;
panel_frame.origin.y += screen_frame.origin.y; // for Dock Height
[_panel setFrame:panel_frame display:YES];
}
//
// delegate for SimpleViewerPanel
//
- (void)windowDidResize:(NSNotification *)notification
{
// _is_operating = NO;
[self layoutViews];
}
- (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
{
NSRect rect = [sender frame];
rect.size = frameSize;
rect = [sender contentRectForFrameRect:rect];
rect.size.height -= BUTTON_BAR_HEIGHT;
rect.size = [self adjustContentSize:rect.size];
rect.size.height += BUTTON_BAR_HEIGHT;
rect = [sender frameRectForContentRect:rect];
return rect.size;
}
-(NSString*)title
{
/*
NSSize size = [[_image_view image] size];
NSDate* created = [[_app_controller fileManager] currentFile].created;
NSString* title = [NSString stringWithFormat:@"%@ (%dx%d, %@)",
[_filename lastPathComponent],
(int)size.width, (int)size.height,
[created descriptionWithCalendarFormat:@"%m-%d %H:%M"
timeZone:nil locale:nil]];
NSString* title = [NSString stringWithFormat:@"%@ (%@)",
[_filename lastPathComponent],
[[_app_controller fileManager] currentFile].created];
*/
NSString* title = [_filename lastPathComponent];
return title;
}
-(NSDate*)fileModificationDateWithPath:(NSString*)path
{
NSFileManager* fm = [NSFileManager defaultManager];
NSError* error = nil;
NSDictionary* attrs = [fm attributesOfItemAtPath:path error:&error];
if (error) {
NSLog(@"error: %@, %@", path, error);
}
return [attrs fileModificationDate];
}
-(void)setFilename:(NSString*)filename
{
[filename retain];
[_filename release];
_filename = filename;
[_filename_textfiled setStringValue:[self title]];
// [_panel setTitle:@"SimpleCap Viewer"];
[_file_lastmodified release];
if (_filename) {
_file_lastmodified = [[self fileModificationDateWithPath:_filename] retain];
} else {
_file_lastmodified = nil;
}
}
- (NSString*)filename
{
return _filename;
}
-(BOOL)updateOpendFile
{
NSString* filename = [self filename];
NSDate* date = [self fileModificationDateWithPath:filename];
if ([date compare:_file_lastmodified] == NSOrderedDescending) {
[self showFile:filename isFitToImage:NO withDirection:SV_TRANTYPE_UPDATE];
return YES;
}
return NO;
}
//
// for client code
//
- (void)showFile:(NSString*)filename isFitToImage:(BOOL)is_fit withDirection:(int)direction
{
NSImage* image = [[[NSImage alloc] initWithContentsOfFile:filename] autorelease];
NSString* title;
[_image_view setImage:image withDirection:direction];
if (!filename || ![[NSFileManager defaultManager] fileExistsAtPath:filename]) {
filename = nil;
title = @"";
}
// show content
if (is_fit) {
if (_is_init_panel_position) {
[self adjustPanelFrameWithContentSize:[image size]];
_is_init_panel_position = NO;
}
}
[self layoutViews];
[self setFilename:filename];
CGFloat margin = [_info_view infoStringSize].width + 2;
[_button_bar2 setButtonBarWithFrame:[[_panel contentView] frame]];
[_button_bar2 setDrawOffset:NSMakePoint(-margin, -BUTTON_MARGIN_BOTTOM)];
[self updateInfomation];
[_image_view setNeedsDisplay:YES];
[_info_view setNeedsDisplay:YES];
}
- (void)openWithFile:(NSString*)filename isNew:(BOOL)new_flag
{
int dir;
if (new_flag) {
dir = SV_TRANTYPE_NEW;
} else {
dir = SV_TRANTYPE_OPEN;
}
[self showFile:filename isFitToImage:YES withDirection:dir];
[_panel show];
}
- (void)show
{
[self flagsChanged:nil];
[_panel show];
}
-(void)close
{
[_panel hide];
}
- (BOOL)isOpened
{
return [_panel isOpened];
}
// manage filename changing
- (void)endEditFilenameIsCancel:(BOOL)is_cancel
{
if (![_filename_textfiled isEnabled]) {
return;
}
if (is_cancel) {
[_filename_textfiled setStringValue:[self title]];
[_filename_textfiled setDisable];
} else {
NSString* input_value = [_filename_textfiled stringValue];
if ([input_value isEqualToString:@""] ||
[input_value rangeOfString:@"/"].location != NSNotFound ||
[input_value hasPrefix:@"."]) {
[_panel makeFirstResponder:_filename_textfiled];
return;
}
NSString* ext = [_filename pathExtension];
NSString* new_filename = [input_value stringByAppendingPathExtension:ext];
NSString* new_path = [[_filename stringByDeletingLastPathComponent]
stringByAppendingPathComponent:new_filename];
if ([[_app_controller fileManager] isExistPath:new_path]) {
// *TODO* show message
// @"ViewerFileExists"
[_panel makeFirstResponder:_filename_textfiled];
return;
}
BOOL is_set_disable = YES;
if (![[_filename lastPathComponent] isEqualToString:new_filename]) {
if ([[_app_controller fileManager] renameFrom:_filename
To:new_path]) {
[self setFilename:new_path];
} else {
is_set_disable = NO;
}
}
if (is_set_disable) {
[_filename_textfiled setDisable];
} else {
[_panel makeFirstResponder:_filename_textfiled];
}
}
}
// handling buttons
-(void)clickedAtTag:(NSNumber*)tag event:(NSEvent*)event
{
if ([_filename_textfiled isEditing]) {
[_filename_textfiled textDidEndEditing:nil];
}
NSString* other_filename;
CGFloat ratio;
NSBitmapImageRep* bitmap;
NSString* new_filename;
switch ([tag intValue]) {
case SV_TAG_TRASH:
if ([[_app_controller fileManager] count] > 0) {
other_filename = [[_app_controller fileManager] filenameAfterDeleteFilename:_filename];
[[_app_controller fileManager] moveToTrash:_filename]; // must be in this order
[_app_controller showMessage:
[NSString stringWithFormat:NSLocalizedString(@"FileDeleted", @""), [_filename lastPathComponent]]];
if (other_filename) {
[self showFile:other_filename isFitToImage:NO withDirection:SV_TRANTYPE_TRASH];
} else {
// "no files"
}
}
break;
case SV_TAG_PREFERENCE:
[_app_controller openPereferecesWindow:_app_controller];
break;
case SV_TAG_RETAKE:
[self close];
// [[_app_controller fileManager] moveToTrash:_filename];
[self clickedAtTag:[NSNumber numberWithInt:SV_TAG_TRASH] event:nil];
[_app_controller captureByLastHandler];
break;
case SV_TAG_APPLICATION:
[NSMenu popUpContextMenu:[_app_menu menu] withEvent:event forView:nil];
break;
case SV_TAG_PREVIOUS_FILE:
other_filename = [[_app_controller fileManager] previousFilenameInSaveFolderWithCurrentFilename:_filename];
if (other_filename && ![_filename isEqualToString:other_filename]) {
[self showFile:other_filename isFitToImage:NO withDirection:SV_TRANTYPE_PREVIOUS];
}
break;
case SV_TAG_NEXT_FILE:
other_filename = [[_app_controller fileManager] nextFilenameInSaveFolderWithCurrentFilename:_filename];
if (other_filename && ![_filename isEqualToString:other_filename]) {
[self showFile:other_filename isFitToImage:NO withDirection:SV_TRANTYPE_NEXT];
}
break;
case SV_TAG_CLOSE:
[self close];
break;
case SV_TAG_SAVE:
ratio = [self reductionRatio];
if (ratio < 1.0) {
//[[_app_controller fileManager] moveToTrash:_filename];
new_filename = [NSString stringWithFormat:@"%@/%@-%d%%.%@",
[_filename stringByDeletingLastPathComponent],
[[_filename lastPathComponent] stringByDeletingPathExtension],
(int)(ratio*100),
[_filename pathExtension]];
[[_app_controller fileManager] saveImage:[self currentBitmapImageRepIsReduction:YES]
withFilename:new_filename];
[_app_controller showMessage:
[NSString stringWithFormat:NSLocalizedString(@"SavedAsRatio", @""), (int)(100*ratio)]];
// reload
[[_app_controller fileManager] lastFilename];
[self showFile:new_filename isFitToImage:NO withDirection:SV_TRANTYPE_NEW];
}
break;
case SV_TAG_CAPTURE:
/*
[self close];
[_app_controller captureByLastHandler];
*/
[NSMenu popUpContextMenu:_capture_menu withEvent:event forView:nil];
// [[_app_controller fileManager] lastFilename]; // only to update directory info
break;
case SV_TAG_CAPTURE_AGAIN:
[self close];
[_app_controller captureByLastHandler];
break;
case SV_TAG_COPY:
bitmap = [self currentBitmapImageRepIsReduction:NO];
if (bitmap) {
[_app_controller copyImageWithBitmapImageRep:bitmap];
[_app_controller showMessage:NSLocalizedString(@"CopiedObjects", @"")];
}
break;
case SV_TAG_CONFIG:
[NSMenu popUpContextMenu:_config_menu withEvent:event forView:nil];
break;
case SV_TAG_OPERATION:
[NSMenu popUpContextMenu:_operation_menu withEvent:event forView:nil];
break;
}
[self endEditFilenameIsCancel:NO];
}
- (CGFloat)reductionRatio
{
return [_image_view reductionRatio];
}
- (NSBitmapImageRep*)currentBitmapImageRepIsReduction:(BOOL)is_reduction
{
NSImage* image = [_image_view image];
if (!image) {
return nil;
}
CGFloat ratio;
if (is_reduction) {
ratio = [self reductionRatio];
} else {
ratio = 1.0;
}
NSRect rect = NSZeroRect;
NSRect new_rect = NSZeroRect;
rect.size = [image size];
new_rect.size.width = (int)(rect.size.width * ratio);
new_rect.size.height = (int)(rect.size.height * ratio);
NSImage* new_image = [[[NSImage alloc] initWithSize:new_rect.size] autorelease];
[new_image lockFocus];
[NSGraphicsContext saveGraphicsState];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
[image drawInRect:new_rect
fromRect:NSZeroRect
operation:NSCompositeSourceOver
fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
[new_image unlockFocus];
NSData* data = [new_image TIFFRepresentation];
return [NSBitmapImageRep imageRepWithData:data];
}
- (void)keyDown:(id)theEvent
{
int is_command = [theEvent modifierFlags] & NSCommandKeyMask;
int is_alternate = [theEvent modifierFlags] & NSAlternateKeyMask;
// int is_shift = [theEvent modifierFlags] & NSShiftKeyMask;
switch ([theEvent keyCode]) {
case 126:
// key up
break;
case 125:
// key down
break;
case 124:
// key right
[self clickedAtTag:[NSNumber numberWithInt:SV_TAG_NEXT_FILE] event:nil];
break;
case 123:
// key left
[self clickedAtTag:[NSNumber numberWithInt:SV_TAG_PREVIOUS_FILE] event:nil];
break;
case 53:
// esc
[self close];
break;
case 117:
case 51:
[self clickedAtTag:[NSNumber numberWithInt:SV_TAG_TRASH] event:nil];
// delete
break;
case 49:
// space key
if (is_alternate) {
[self clickedAtTag:[NSNumber numberWithInt:SV_TAG_RETAKE] event:nil];
} else {
[self clickedAtTag:[NSNumber numberWithInt:SV_TAG_CAPTURE_AGAIN] event:nil]; }
break;
case 36:
case 76:
// 36:return
// 76:enter
[_filename_textfiled startEdit];
break;
case 1:
// 's'
if (is_command) {
// command + 's'
[self clickedAtTag:[NSNumber numberWithInt:SV_TAG_SAVE] event:nil];
}
break;
case 2:
// 'd'
if (is_command) {
// command + 'd'
[self clickedDuplicate:nil];
}
break;
case 8:
// 'c'
if (is_command) {
// command + 'c'
[self clickedAtTag:[NSNumber numberWithInt:SV_TAG_COPY] event:nil];
}
break;
case 13:
// 'w'
if (is_command) {
// command + 'w'
[self close];
}
break;
}
}
// Panel delegate
- (void)windowDidResignKey:(NSNotification *)notification
{
[self endEditFilenameIsCancel:NO];
}
// Filename delegate
- (BOOL)control:(NSControl *)control
textView:(NSTextView *)textView doCommandBySelector:(SEL)command
{
if (command == @selector(cancel:)) {
[self endEditFilenameIsCancel:YES];
return YES;
}
return NO;
}
// for Application Menu
- (void)openPereferecesWindow:(id)tab_index
{
[_app_controller openPereferecesWindow:tab_index];
}
// for SimpleViewerImageView
- (NSMenu *)menuForEvent:(NSEvent *)theEvent
{
return _context_menu;
}
- (void)copyFileTo:(NSURL*)dst_url
{
id dst_path = [[dst_url relativePath]
stringByAppendingPathComponent:[_filename lastPathComponent]];
[[NSFileManager defaultManager] copyPath:_filename
toPath:dst_path
handler:nil];
}
// for IB
- (IBAction)clickedCopy:(id)sender
{
[self clickedAtTag:[NSNumber numberWithInt:SV_TAG_COPY] event:nil];
}
- (IBAction)clickedMoveToTrash:(id)sender
{
[self clickedAtTag:[NSNumber numberWithInt:SV_TAG_TRASH] event:nil];
}
- (IBAction)clickedSave:(id)sender
{
[self clickedAtTag:[NSNumber numberWithInt:SV_TAG_SAVE] event:nil];
}
- (IBAction)clickedPrevious:(id)sender
{
[self clickedAtTag:[NSNumber numberWithInt:SV_TAG_PREVIOUS_FILE] event:nil];
}
- (IBAction)clickedNext:(id)sender
{
[self clickedAtTag:[NSNumber numberWithInt:SV_TAG_NEXT_FILE] event:nil];
}
- (IBAction)clickedCaptureAgain:(id)sender
{
[self clickedAtTag:[NSNumber numberWithInt:SV_TAG_CAPTURE_AGAIN] event:nil];
}
- (IBAction)clickedRetake:(id)sender
{
[self clickedAtTag:[NSNumber numberWithInt:SV_TAG_RETAKE] event:nil];
}
- (IBAction)clickedOpenWithApplication:(id)sender
{
// *not implemented*
}
-(NSString*)dupFilename:(NSString*)baseFilename extension:(NSString*)ext path:(NSString*)path number:(NSUInteger)num
{
NSString* filename = [NSString stringWithFormat:@"%@/%@(%d).%@",
path, baseFilename, num, ext];
NSFileManager* fm = [NSFileManager defaultManager];
if ([fm fileExistsAtPath:filename]) {
filename = [self dupFilename:baseFilename extension:ext path:path number:num+1];
}
return filename;
}
-(NSString*)dupFilename:(NSString*)filePath
{
NSString* filename = [filePath lastPathComponent];
NSString* filenameWithoutExtension = [filename stringByDeletingPathExtension];
NSString* extension = [filePath pathExtension];
NSRange r1 = [filenameWithoutExtension rangeOfString:@"(" options:NSBackwardsSearch];
NSRange r2 = [filenameWithoutExtension rangeOfString:@")" options:NSBackwardsSearch];
NSString* baseFilename = filenameWithoutExtension;
NSInteger num = 0;
if (r1.location != NSNotFound && r2.location != NSNotFound && r2.location == [filenameWithoutExtension length]-1) {
NSRange rnum = NSMakeRange(r1.location+r1.length, r2.location-r1.location-1);
NSRange rbase = NSMakeRange(0, r1.location);
NSString* numStr = [filenameWithoutExtension substringWithRange:rnum];
baseFilename = [filenameWithoutExtension substringWithRange:rbase];
num = [numStr integerValue];
}
num++;
NSString* path = [filePath stringByDeletingLastPathComponent];
NSString* newFilename = [self dupFilename:baseFilename extension:extension path:path number:num];
return newFilename;
}
- (IBAction)clickedDuplicate:(id)sender
{
NSString* new_filename = [self dupFilename:_filename];
NSFileManager* fm = [NSFileManager defaultManager];
NSError* error = nil;
if ([fm copyItemAtPath:_filename toPath:new_filename error:&error]) {
[_app_controller showMessage:NSLocalizedString(@"Duplicated", @"")];
// reload
[[_app_controller fileManager] lastFilename];
[self showFile:new_filename isFitToImage:NO withDirection:SV_TRANTYPE_NEW];
} else {
NSLog(@"%@", error);
}
}
// for Context Menu (bindings)
- (BOOL)backgroundBlack
{
return ([[UserDefaults valueForKey:UDKEY_VIEWER_BACKGROUND] intValue] == 0);
}