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

Revision 717, 8.1 kB (checked in by cho45, 6 years ago)

lang/ruby/Chemr/CHMDocument.rb,
lang/ruby/Chemr/English.lproj/CHMDocument.nib/info.nib,
lang/ruby/Chemr/English.lproj/CHMDocument.nib/keyedobjects.nib:

chmlib binding の変更に追従
おちにくいようにコード変更
おちやすいようにコード変更

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
62require "uri"
63class CHMWindowController < NSWindowController
64        ib_outlet :webview
65        ib_outlet :list
66        ib_outlet :tree
67        ib_outlet :drawer
68        ib_outlet :search
69
70        def windowDidLoad
71                @chm = self.document.chm
72                uri  = URI(self.document.fileURL.absoluteString)
73                log uri
74                browse @chm.home
75                @now = @index = @chm.index.to_a.sort_by {|k,v| k} # cache
76                @list.setDataSource(self)
77                @list.setDoubleAction("clicked_")
78                @list.setAction("clicked_")
79
80                @tree.setAction("treeclicked_")
81
82                @search.setDelegate(self)
83                @drawer.open
84        end
85
86        # OutlineView
87        #    * outlineView:child:ofItem:
88        #    * outlineView:isItemExpandable:
89        #    * outlineView:numberOfChildrenOfItem:
90        #    * outlineView:objectValueForTableColumn:byItem:
91        #    * outlineView:setObjectValue:forTableColumn:byItem:
92
93        def outlineView_child_ofItem(ov, index, item)
94                (item || @topics)[:children][index]
95        end
96
97        def outlineView_isItemExpandable(ov, item)
98                (item || @topics)[:children].length.nonzero?
99        end
100
101        def outlineView_numberOfChildrenOfItem(ov, item)
102                (item || @topics)[:children].length
103        end
104
105        def outlineView_objectValueForTableColumn_byItem(ov, column, item)
106                if item
107                        item[:name]
108                else
109                        ""
110                end
111        end
112
113        def treeclicked(sender)
114                path = sender.itemAtRow(sender.selectedRow)[:local]
115                log "Tree Clicked: #{path}"
116                browse path unless path.empty?
117        end
118
119
120        # Tableview
121        def numberOfRowsInTableView(table)
122                @now.length
123        end
124
125        def tableView_objectValueForTableColumn_row(table, column, row)
126                @now[row][0]
127        end
128
129        def tableView_setObjectValue_forTableColumn_row(table, value, column, row)
130        end
131
132        def tableView_willDisplayCell_forTableColumn_row(table, cell, column, row)
133        end
134
135        def textShouldBeginEditing(text)
136                true
137        end
138
139        def textShouldEndEditing(text)
140                true
141        end
142
143        def acceptsFirstResponder
144                true
145        end
146
147        # TabView
148        def tabView_willSelectTabViewItem(sender, item)
149                log item.label
150                if item.label == "Tree"
151                        Thread.start do
152                                @topics = @chm.topics
153                                # おちやすい
154                                @tree.setDataSource(self)
155                                @tree.reloadData
156                        end
157                end
158        end
159
160        # general
161
162        def controlTextDidChange(anot)
163                filtering @search.stringValue
164                @list.selectRowIndexes_byExtendingSelection(NSIndexSet.alloc.initWithIndex(0), false)
165        end
166
167        def controlTextDidEndEditing(anot)
168                log "end #{@now.first.inspect}"
169        end
170
171        def jumpToCurrent(sender)
172                clicked(sender)
173        end
174
175        def filtering(str)
176                str = str.to_s
177                if str =~ /[A-Z]/
178                        r = /^#{str}/
179                else
180                        r = /^#{str}/i
181                end
182                @now = @index.select {|k,v|
183                        k =~ r
184                }.sort_by {|k,v| k.length }
185                @list.reloadData
186        end
187
188        def clicked(sender)
189                if @now[@list.selectedRow]
190                        browse @now[@list.selectedRow][1].first
191                end
192        end
193
194        def browse(path)
195                if path
196                        path = "/#{path}" unless path[0] == ?/
197                        h = @webview.stringByEvaluatingJavaScriptFromString("location.pathname+location.hash")
198                        unless path == h
199                                r = NSURLRequest.requestWithURL CHMInternalURLProtocol.url_for(@chm, path)
200                                log r
201                                @webview.mainFrame.loadRequest r
202                        end
203                end
204        end
205
206        def completion(sender)
207                return if @search.stringValue.empty?
208                return if @now.empty?
209                common = ""
210                keys = @now.map{|k,v| k.split(//)}
211                if @search.stringValue.to_s =~ /[A-Z]/
212                        keys[0].zip(*keys[1..-1]) do |a|
213                                m = a.first
214                                if a.all? {|v| m == v}
215                                        common << m
216                                else
217                                        break
218                                end
219                        end
220                else
221                        keys[0].zip(*keys[1..-1]) do |a|
222                                m = a.first.downcase
223                                if a.all? {|v| v && (m == v.downcase)}
224                                        common << m
225                                else
226                                        break
227                                end
228                        end
229                end
230                if common.length > @search.stringValue.length
231                        @search.stringValue = common
232                end
233        end
234
235        # from menu
236        def searchActivate(sender)
237                log "activate"
238                @search.window.makeFirstResponder(@search)
239        end
240
241        def nextCandidate(sender)
242                if @list.selectedRow <= @now.size
243                        @list.selectRowIndexes_byExtendingSelection(NSIndexSet.alloc.initWithIndex(@list.selectedRow+1), false)
244                        @list.scrollRowToVisible(@list.selectedRow)
245                        clicked(nil)
246                end
247        end
248
249        def prevCandidate(sender)
250                if @list.selectedRow > 0
251                        @list.selectRowIndexes_byExtendingSelection(NSIndexSet.alloc.initWithIndex(@list.selectedRow-1), false)
252                        @list.scrollRowToVisible(@list.selectedRow)
253                        clicked(nil)
254                end
255        end
256
257        def jumpToHome(sender)
258                browse @chm.home
259        end
260
261        def performFindPanelAction(sender)
262                log "performFindPanelAction"
263                # @webview.performFindPanelAction(sender) # なぜかうごかない
264                text = @search.stringValue
265                @webview.objc_send(
266                        :searchFor, text,
267                        :direction, true,
268                        :caseSensitive, false,
269                        :wrap, false
270                )
271        end
272
273        # from MySearchWindow
274
275        def process_keybinds(e)
276                if NSInputManager.currentInputManager
277                        return false unless NSInputManager.currentInputManager.markedRange.empty?
278                end
279                key = key_string(e)
280                log "keyDown (#{e.characters}:#{e.charactersIgnoringModifiers}) -> '#{key}'"
281                keybinds = {
282                        "C-j" => self.method(:nextCandidate),
283                        "C-n" => self.method(:nextCandidate),
284                        "C-k" => self.method(:prevCandidate),
285                        "C-p" => self.method(:prevCandidate),
286                        "\r"  => self.method(:jumpToCurrent),
287                        "\t"  => self.method(:completion),
288                        " "   => Proc.new {|s|
289                                @webview.stringByEvaluatingJavaScriptFromString <<-JS
290                                        window.scrollBy(0, 200);
291                                JS
292                        },
293                        "S- " => Proc.new {|s|
294                                @webview.stringByEvaluatingJavaScriptFromString <<-JS
295                                        window.scrollBy(0, -200);
296                                JS
297                        },
298                        "C-\r" => Proc.new {|s|
299                                @now = @chm.search(@search.stringValue).map {|title,url|
300                                        [title, [url]]
301                                }
302                                @list.reloadData
303                        },
304                        "C-u" => Proc.new {|s|
305                                @search.stringValue = ""
306                        },
307                        "G-[" => Proc.new {|s|
308                                @webview.goBack
309                        },
310                        "G-]" => Proc.new {|s|
311                                @webview.goForward
312                        },
313                        "G-=" => Proc.new {|s|
314                                @webview.makeTextLarger(self)
315                        },
316                        "G--" => Proc.new {|s|
317                                @webview.makeTextSmaller(self)
318                        },
319                }
320                (1..9).each do |i|
321                        keybinds["G-#{i}"] = Proc.new {|s|
322                                dc = NSDocumentController.sharedDocumentController
323                                if dc.documents[i-1]
324                                        log(dc.documents[i-1].windowControllers)
325                                        dc.documents[i-1].windowControllers.first.showWindow(self)
326                                end
327                        }
328                end
329                if keybinds.key?(key)
330                        keybinds[key].call(self)
331                        true
332                else
333                        false
334                end
335        end
336
337        def key_string(e)
338                key = ""
339                m = e.modifierFlags
340                key << "S-" if m & NSShiftKeyMask > 0
341                key << "C-" if m & NSControlKeyMask > 0
342                key << "M-" if m & NSAlternateKeyMask > 0
343                key << "G-" if m & NSCommandKeyMask > 0 # TODO
344                key << e.charactersIgnoringModifiers.to_s
345                key
346        end
347
348        # webview policyDelegate
349        def webView_decidePolicyForNavigationAction_request_frame_decisionListener(
350                sender,
351                actionInformation,
352                request,
353                frame,
354                listener
355        )
356
357                if CHMInternalURLProtocol.canHandleURL(request.URL)
358                        listener.use
359                else
360                        NSWorkspace.sharedWorkspace.openURL(request.URL)
361                        listener.ignore
362                end
363        end
364
365        def webView_decidePolicyForNewWindowAction_request_newFrameName_decisionListener(
366                sender,
367                actionInformation,
368                request,
369                frameName,
370                listener
371        )
372                if CHMInternalURLProtocol.canHandleURL(request.URL)
373                        listener.use
374                else
375                        NSWorkspace.sharedWorkspace.openURL(request.URL)
376                        listener.ignore
377                end
378        end
379
380        # webview loading delegate
381        def webView_resource_didFinishLoadingFromDataSource(sender, id, datasource)
382#               log "loaded"
383        end
384
385
386end
Note: See TracBrowser for help on using the browser.