OSDN Git Service

searching can be executed when typing a return key, and some names of variables or...
[sawarabi-fonts/sawarabi-fonts.git] / chartool / objective-c / Controller.m
1 //
2 //  Controller.m
3 //  chartool
4 //
5 //  Created by mshio on 11/02/05.
6 //  Copyright 2011 mshio. All rights reserved.
7 //
8 #include <stdio.h>
9 #import "Controller.h"
10
11 @implementation Controller
12
13 - (id) init {
14         engine = [JavaScriptEngine instance];
15         charUtil = [UnicharUtil instance];
16         return [super init];
17 }
18
19 - (void) setupFontMenu {
20         NSArray* nm = [engine fontNamesWithArray];
21         NSMenuItem* menu = [glyphViewMenu itemAtIndex: 1];
22         NSMenu* p = [[NSMenu alloc] init];
23         
24         [p setAutoenablesItems: NO];
25         int i = 0;
26         do {
27                 NSString* t = i == 0 ? @"default" : [nm objectAtIndex: i - 1];
28                 NSMenuItem* m = [[NSMenuItem alloc] init];
29                 [m setTitle: t];
30                 [m setAction: @selector(changeFont:)];
31                 [m setTarget: self];
32                 [m setEnabled: YES];
33                 [p addItem: m];
34         } while (nm != NULL && ++i <= [nm count]);
35         [menu setSubmenu: p];
36 }
37
38 - (void) awakeFromNib {
39         [self setupFontMenu];
40 }
41
42 - (NSDictionary*) genFontAttrDict: (NSString*) fontName size: (float) size {
43         NSFont* f = fontName == nil || [fontName isEqualToString: @"default"] ?
44                 [NSFont messageFontOfSize: size] : 
45                 [NSFont fontWithName: fontName size: size];
46
47         return [NSDictionary dictionaryWithObjectsAndKeys:
48                         f, NSFontAttributeName, nil];
49 }
50
51 - (void) drawOnGlyphView: (NSString*) string {
52         float dummy = 24.0;
53         
54         NSSize viewSize = [glyphView bounds].size;
55         NSImage* image = [[NSImage alloc] initWithSize: viewSize];
56         NSSize stringSize = [string sizeWithAttributes: 
57                                                  [self genFontAttrDict: currentFont size: dummy]];
58         float fontSize = dummy * 
59                 (viewSize.width >= viewSize.height ? 1 / stringSize.height * viewSize.height :
60                  1 / stringSize.width * viewSize.width);
61         NSDictionary *attr = [self genFontAttrDict: currentFont size: fontSize];
62         stringSize = [string sizeWithAttributes: attr];
63         int margin = (viewSize.width - stringSize.width) / 2;
64         [image lockFocus];
65         [string drawAtPoint: NSMakePoint(margin, 0) withAttributes: attr];
66         [image unlockFocus];
67         [glyphView setImage: image];
68 }
69
70 - (NSRange) getTargetRangeFrom: (NSString*) text withLocation: (int) location {
71         unichar ch = [text characterAtIndex: location];
72         
73         int rlen = 0, llen = 0;
74         while (rlen <= 4 && [charUtil characterIsHexCharacter: ch]) {
75                 rlen++;
76                 if (location + rlen >= [text length]) break;
77                 ch = [text characterAtIndex: location + rlen];
78         }
79         if (rlen >= 1 && rlen < 5) {
80                 while (rlen + llen <= 4 && location - llen > 0) {
81                         ch = [text characterAtIndex: location - llen - 1];
82                         if (! [charUtil characterIsHexCharacter: ch]) break;
83                         llen++;
84                 }
85         }
86
87         return NSMakeRange(location - llen, rlen + llen);
88 }
89
90 - (NSString*) getCharacterFrom: (NSString*) text withRange: (NSRange) range andLocation: (int) location {
91         if (range.length >= 4) {
92                 return [charUtil getUnicharFromCharCode: [text substringWithRange: range]];
93         } else {
94                 unichar ch = [text characterAtIndex: range.location];
95                 BOOL high = [charUtil characterIsHighSurrogate: ch];
96                 BOOL low = [charUtil characterIsLowSurrogate: ch];
97                 int p = location - (low ? 1 : 0);
98                 return [text substringWithRange: 
99                                 NSMakeRange(p < 0 ? 0 : location, high || low ? 2 : 1)];
100         }
101 }
102
103 - (BOOL) control: (NSControl *) control textView: (NSTextView *) textView doCommandBySelector: (SEL) command {
104         if (command == @selector(insertNewline:)) {
105                 [self search: control];
106                 return YES;
107         }
108         return NO;
109 }
110
111 - (IBAction) search: (id) sender {
112         NSString* text = [searchField stringValue];
113         if ([text length] <= 0) return;
114
115         NSText* editor = [searchField currentEditor];
116         NSRange range = [editor selectedRange];
117         int loc = range.location + range.length < [text length] ? range.location + range.length : 0;
118         NSRange tr = [self getTargetRangeFrom: text withLocation: loc];
119         NSString* ch = [self getCharacterFrom: text withRange: tr andLocation: loc];
120
121         currentCharacter = ch;
122         [editor setSelectedRange: 
123          tr.length < 4 ? NSMakeRange(loc, [ch length]) : NSMakeRange(tr.location, tr.length)];
124
125         [self drawOnGlyphView: ch];
126
127         NSString* code = [charUtil getCharCodeFromUnichar: ch];
128         [codeField setStringValue: code];
129 }
130
131 - (IBAction) clickScriptButton: (id) sender {
132         int tag = [sender tag];
133         NSString* p = [tag == 0 ? codeField : nameField stringValue];
134         NSUInteger m = [NSEvent modifierFlags];
135         [engine executeScriptAtIndex: tag withProperty: p AndModifier: m];
136 }
137
138 - (IBAction) copyGlyphCharacter: (id) sender {
139         if (currentCharacter) {
140                 NSPasteboard* cb = [NSPasteboard generalPasteboard];
141                 [cb declareTypes: [NSArray arrayWithObject: NSStringPboardType] owner: nil];
142                 [cb setString: currentCharacter forType: NSStringPboardType];
143         }
144 }
145
146 - (IBAction) changeFont: (id) sender {
147         currentFont = [sender title];
148         if (currentCharacter != nil) {
149                 [self drawOnGlyphView: currentCharacter];
150         }
151 }
152
153 @end