root/lang/ruby/Chemr/CHMDocument.rb @ 669

Revision 669, 5.3 kB (checked in by cho45, 6 years ago)

lang/ruby/Chemr/CHMDocument.rb:

ヒットなしの状態で RET したとき落ちていたのを修正

Line 
1#!rake ;#
2
3class CHMDocument < NSDocument
4        attr_reader :chm
5
6        #- (void)makeWindowControllers
7        def makeWindowControllers
8                c = CHMWindowController.alloc.initWithWindowNibName("CHMDocument")
9                self.addWindowController(c)
10        end
11
12        #- (BOOL)readFromURL:(NSURL *)inAbsoluteURL ofType:(NSString *)inTypeName error:(NSError **)outError
13        def readFromURL_ofType_error(url, type, error)
14                @chm = Chmlib::Chm.new(url.path.to_s)
15                true
16        end
17
18        #- (BOOL)writeToURL:(NSURL *)inAbsoluteURL ofType:(NSString *)inTypeName error:(NSError **)outError
19        def writeToURL_ofType_error(url, type, error)
20                false
21        end
22
23        #- (void)windowControllerDidLoadWindowNib:(NSWindowController *)windowController
24        def windowControllerDidLoadWindowNib(cont)
25                log "wCDLWN", cont
26        end
27
28#       def dataRepresentationOfType(aType)
29#       end
30#
31#       def loadDataRepresentation_ofType(data, aType)
32#       end
33
34        def displayName
35                dc = NSDocumentController.sharedDocumentController
36                i = dc.documents.index(self) + 1
37                cmd = [8984].pack("U")
38                "#{cmd}#{i}| #{@chm.title}"
39        end
40
41        def windowControllerWillLoadNib(cont)
42                log cont
43        end
44
45        def winwowNibName
46                "CHMDocument"
47        end
48end
49
50class MySearchWindow < NSWindow
51
52        def sendEvent(e)
53                if e.oc_type == NSKeyDown
54                        return if delegate.process_keybinds(e)
55                end
56                super_sendEvent(e)
57        end
58
59end
60
61
62class CHMWindowController < NSWindowController
63        ib_outlet :webview
64        ib_outlet :list
65        ib_outlet :drawer
66        ib_outlet :search
67
68        def windowDidLoad
69                @chm = self.document.chm
70                browse @chm.home
71                @now = @index = @chm.index.to_a.sort_by {|k,v| k} # cache
72                @list.setDataSource(self)
73                @list.setDoubleAction("clicked_")
74                @list.setAction("clicked_")
75                @search.setDelegate(self)
76                @drawer.open
77        end
78
79        # Tableview
80        def numberOfRowsInTableView(table)
81                @now.length
82        end
83
84        def tableView_objectValueForTableColumn_row(table, column, row)
85                @now[row][0]
86        end
87
88#       def tableView_setObjectValue_forTableColumn_row(table, value, column, row)
89#       end
90
91        def tableView_willDisplayCell_forTableColumn_row(table, cell, column, row)
92#               case column.identifier.to_s
93#               when 'regexp'
94#               when 'color'
95#                       _, r, g, b = */(..)(..)(..)$/.match(cell.stringValue.to_s).to_a.map{|i| i.to_i(16) / 255.0 }
96#                       color = NSColor.colorWithCalibratedRed_green_blue_alpha(r, g, b, 1)
97#                       cell.setDrawsBackground(true)
98#                       cell.setTextColor(color)
99#                       cell.setBackgroundColor(NSColor.blackColor)
100#               when 'hilight'
101#               end
102        end
103
104        def textShouldBeginEditing(text)
105                true
106        end
107
108        def textShouldEndEditing(text)
109                true
110        end
111
112        def acceptsFirstResponder
113                true
114        end
115
116        def controlTextDidChange(anot)
117                filtering @search.stringValue
118                @list.selectRowIndexes_byExtendingSelection(NSIndexSet.alloc.initWithIndex(0), false)
119        end
120
121        def controlTextDidEndEditing(anot)
122                log "end #{@now.first.inspect}"
123                #jumpToCurrent
124        end
125
126        def jumpToCurrent(sender)
127                unless @now.length.zero?
128                        browse @now.first[1].first
129                end
130        end
131
132        def filtering(str)
133                str = str.to_s
134                if str =~ /[A-Z]/
135                        r = /^#{str}/
136                else
137                        r = /^#{str}/i
138                end
139                @now = @index.select {|k,v|
140                        k =~ r
141                }.sort_by {|k,v| k }
142                @list.reloadData
143        end
144
145        def clicked(sender)
146                browse @now[@list.selectedRow][1].first
147        end
148
149        def browse(path)
150                if path
151                        path = "/#{path}" unless path[0] == ?/
152                        r = NSURLRequest.requestWithURL CHMInternalURLProtocol.url_for(@chm, path)
153                        @webview.mainFrame.loadRequest r
154                end
155        end
156
157        def completion(sender)
158                # not implemented yet
159        end
160
161        # from menu
162        def searchActivate(sender)
163                log "activate"
164                @search.window.makeFirstResponder(@search)
165        end
166
167        def nextCandidate(sender)
168                if @list.selectedRow <= @now.size
169                        @list.selectRowIndexes_byExtendingSelection(NSIndexSet.alloc.initWithIndex(@list.selectedRow+1), false)
170                        clicked(nil)
171                end
172        end
173
174        def prevCandidate(sender)
175                if @list.selectedRow > 0
176                        @list.selectRowIndexes_byExtendingSelection(NSIndexSet.alloc.initWithIndex(@list.selectedRow-1), false)
177                        clicked(nil)
178                end
179        end
180
181        # from MySearchWindow
182
183        def process_keybinds(e)
184                key = key_string(e)
185                log "keyDown (#{e.characters}:#{e.charactersIgnoringModifiers}) -> '#{key}'"
186                keybinds = {
187                        "C-j" => self.method(:nextCandidate),
188                        "C-n" => self.method(:nextCandidate),
189                        "C-k" => self.method(:prevCandidate),
190                        "C-p" => self.method(:prevCandidate),
191                        "\r"  => self.method(:jumpToCurrent),
192                        "\t"  => self.method(:completion),
193                        " "   => Proc.new {|s|
194                                @webview.stringByEvaluatingJavaScriptFromString <<-JS
195                                        window.scrollBy(0, 200);
196                                JS
197                        },
198                        "C-u" => Proc.new {|s|
199                                @search.stringValue = ""
200                        },
201                        "G-[" => Proc.new {|s|
202                                @webview.goBack
203                        },
204                        "G-]" => Proc.new {|s|
205                                @webview.goForward
206                        },
207                        "G-F" => Proc.new {|s|
208                                @webview.performFindPanelAction(self)
209                        },
210                        "G-=" => Proc.new {|s|
211                                @webview.makeTextLarger(self)
212                        },
213                        "G--" => Proc.new {|s|
214                                @webview.makeTextSmaller(self)
215                        },
216                }
217                (1..9).each do |i|
218                        keybinds["G-#{i}"] = Proc.new {|s|
219                                dc = NSDocumentController.sharedDocumentController
220                                if dc.documents[i-1]
221                                        log(dc.documents[i-1].windowControllers)
222                                        dc.documents[i-1].windowControllers.first.showWindow(self)
223                                end
224                        }
225                end
226                if keybinds.key?(key)
227                        keybinds[key].call(self)
228                        true
229                else
230                        false
231                end
232        end
233
234        def key_string(e)
235                key = ""
236                m = e.modifierFlags
237                key << "S-" if m & NSShiftKeyMask > 0
238                key << "C-" if m & NSControlKeyMask > 0
239                key << "M-" if m & NSAlternateKeyMask > 0
240                key << "G-" if m & NSCommandKeyMask > 0 # TODO
241                key << e.charactersIgnoringModifiers.to_s
242                key
243        end
244end
Note: See TracBrowser for help on using the browser.