OSDN Git Service

For supporting macOS Monterey 13.3 Beta 1
[letter-fix/LetterFix.git] / LFMenu.m
1 //
2 //  LFMenu.m
3 //  LetterFix2
4 //
5 //  Created by kuri on 10/02/26.
6 //  Copyright 2010-2021 kuri. All rights reserved.
7 //
8
9 #import "LFMenu.h"
10
11 @implementation LFMenu
12
13 - (id) initWithApp:(LFApp *)app
14 {
15     self = [super init];
16     
17     _app       = app;
18     
19     return self;
20 }
21
22 - (BOOL) inject
23 {
24     NSMenu *newmenu, *appmenu;
25     NSMenuItem *item;
26     
27     @try {
28         [self retain];
29         
30         newmenu = [[NSMenu alloc] initWithTitle:@"LetterFix Plug-in"];
31         
32         onoff = [self newMenuItem:@"LetterFix を有効にする" action:@selector(onMenuIsActive:) state:[_app isActive] indent:0];
33         [newmenu addItem:onoff];
34         [newmenu addItem:[self newMenuItem:@"機種依存文字を代替形式に変換" action:@selector(onMenuShouldFixDependents:) state:_app.shouldFixOsDependents indent:1]];
35         [newmenu addItem:[self newMenuItem:@"編集開始時に㈪㈫…㈵㈼…を丸数字・ローマ数字と見なす" action:@selector(onMenuShouldFixParenSymbols:) state:_app.shouldFixParenSymbols indent:2]];
36         [newmenu addItem:[self newMenuItem:@"㈹㈱㈲は⑫⑭⑰と見なさない" action:@selector(onMenuShouldNotFixSomeParenSymbols:) state:_app.shouldNotFixSomeParenSymbols indent:3]];
37         [newmenu addItem:[self newMenuItem:@"ISO 2022-JP に変換できない文字を「〓」に修正" action:@selector(onMenuShouldFixAllLetters:) state:_app.shouldFixAllLetters indent:1]];
38         [newmenu addItem:[self newMenuItem:@"修正時に〓に加えて元の文字コードを付加する" action:@selector(onMenuShouldAppendCodeInfo:) state:_app.shouldAppendCodeInfo indent:2]];
39         [newmenu addItem:[self newMenuItem:@"ZERO WIDTH SPACEを削除する" action:@selector(onMenuShouldIgnoreZeroWidthSpace:) state:_app.shouldIgnoreZeroWidthSpace indent:2]];
40         [newmenu addItem:[self newMenuItem:@"件名をチェックする" action:@selector(onMenuShouldCheckSubject:) state:_app.shouldCheckSubject indent:1]];
41         [newmenu addItem:[self newMenuItem:@"保存時にチェックする" action:@selector(onMenuShouldCheckOnSave:) state:_app.shouldCheckOnSave indent:1]];
42         [newmenu addItem:[NSMenuItem separatorItem]];
43         [newmenu addItem:[self newMenuItem:@"メッセージ編集開始時の動作をリセット" action:@selector(onRestOperationModeOnLoad:) state:NO indent:0]];
44         [newmenu addItem:[self newMenuItem:@"LetterFixについて…" action:@selector(showAboutLetterfix:) state:NO indent:0]];
45         
46         item = [[NSMenuItem alloc] initWithTitle:@"LetterFix Plug-in" action:NULL keyEquivalent:@""];
47         [item setSubmenu:newmenu];
48         
49         appmenu = [[[[NSApplication sharedApplication] mainMenu] itemAtIndex:0] submenu];
50         [appmenu insertItem:[NSMenuItem separatorItem] atIndex:1];
51         [appmenu insertItem:item atIndex:2];
52     }
53     @catch (NSException * e) {
54         NSLog(@"raise %@", e);
55     }
56     
57     return YES;
58 }
59
60 - (NSMenuItem *)newMenuItem:(NSString *)title action:(SEL)sel state:(BOOL)state indent:(NSInteger)indent
61 {
62     NSMenuItem *item;
63     item = [[NSMenuItem alloc] initWithTitle:title action:sel keyEquivalent:@""];
64     [item setTarget:self];
65     [item setState:state];
66     [item setIndentationLevel:indent];
67     return item;
68 }
69
70 - (void) onMenuIsActive:(NSMenuItem *)sender
71 {
72     [_app setIsActive:(!sender.state)];
73     [sender setState:[_app isActive]];
74 }
75
76 - (void) onMenuShouldFixDependents:(NSMenuItem *)sender
77 {
78     _app.shouldFixOsDependents = !sender.state;
79     sender.state = !sender.state;
80 }
81
82 - (void) onMenuShouldFixParenSymbols:(NSMenuItem *)sender
83 {
84     _app.shouldFixParenSymbols = !sender.state;
85     sender.state = !sender.state;
86 }
87
88 - (void) onMenuShouldNotFixSomeParenSymbols:(NSMenuItem *)sender
89 {
90     _app.shouldNotFixSomeParenSymbols = !sender.state;
91     sender.state = !sender.state;
92 }
93
94 - (void) onMenuShouldFixAllLetters:(NSMenuItem *)sender
95 {
96     _app.shouldFixAllLetters = !sender.state;
97     sender.state = !sender.state;
98 }
99
100 - (void) onMenuShouldAppendCodeInfo:(NSMenuItem *)sender
101 {
102     _app.shouldAppendCodeInfo = !sender.state;
103     sender.state = !sender.state;
104 }
105
106 - (void) onMenuShouldIgnoreZeroWidthSpace:(NSMenuItem *)sender
107 {
108     _app.shouldIgnoreZeroWidthSpace = !sender.state;
109     sender.state = !sender.state;
110 }
111
112 - (void) onRestOperationModeOnLoad:(NSMenuItem *)sender
113 {
114     _app.operationModeOnLoad = 0;
115 }
116
117 - (void) onMenuShouldCheckSubject:(NSMenuItem *)sender
118 {
119     _app.shouldCheckSubject = !sender.state;
120     sender.state = !sender.state;
121 }
122
123 - (void) onMenuShouldCheckOnSave:(NSMenuItem *)sender
124 {
125     _app.shouldCheckOnSave = !sender.state;
126     sender.state = !sender.state;
127 }
128
129 - (void) showAboutLetterfix:(NSMenuItem *)sender
130 {
131     NSAlert *alert = [[NSAlert alloc] init];
132     [alert setMessageText:[NSString stringWithFormat:@"LetterFix plug-in v%s",(LETTERFIX_VERSION)]];
133     [alert setInformativeText:@"このプラグインは,メールに含まれる ISO 2022-JP に変換できない文字を置換し,"
134      @"ISO 2022-JP を用いてメールを返信・転送可能にするためのものです。"];
135     [alert setAlertStyle:NSInformationalAlertStyle];
136     [alert runModal];
137     [alert release];
138 }
139
140 - (BOOL) validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem
141 {
142     if (!_app.isActive) {
143         if (([anItem action] == @selector(onMenuShouldFixDependents:))||
144             ([anItem action] == @selector(onMenuShouldFixParenSymbols:))||
145             ([anItem action] == @selector(onMenuShouldNotFixSomeParenSymbols:))||
146             ([anItem action] == @selector(onMenuShouldFixAllLetters:))||
147             ([anItem action] == @selector(onMenuShouldAppendCodeInfo:))||
148             ([anItem action] == @selector(onMenuShouldIgnoreZeroWidthSpace:))||
149             ([anItem action] == @selector(onMenuShouldCheckSubject:))||
150             ([anItem action] == @selector(onMenuShouldCheckOnSave:)))
151             return NO;
152     } else if (!_app.shouldFixOsDependents) {
153         if (([anItem action] == @selector(onMenuShouldFixParenSymbols:))||
154             ([anItem action] == @selector(onMenuShouldNotFixSomeParenSymbols:)))
155             return NO;
156     } else if (!_app.shouldFixAllLetters){
157         if (([anItem action] == @selector(onMenuShouldAppendCodeInfo:))||
158             ([anItem action] == @selector(onMenuShouldIgnoreZeroWidthSpace:))) {
159             return NO;
160         }
161     } else if (!_app.shouldFixParenSymbols) {
162         if ([anItem action] == @selector(onMenuShouldNotFixSomeParenSymbols:))
163             return NO;
164     }
165     return YES;
166 }
167
168 - (NSMenuItem *)onoffMenuItem
169 {
170     return onoff;
171 }
172 @end