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

Revision 657, 3.4 kB (checked in by cho45, 6 years ago)

lang/ruby/Chemr/CHMDocument.rb:

候補のマッチをマシに
デフォルトで先頭マッチ
正規表現かけるのでどうしてもってときは .* いれればいいかな

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#               log chm
16#               r = NSURLRequest.requestWithURL CHMInternalURLProtocol.url_for(chm, chm.home)
17#               @webview.mainFrame.loadRequest r
18                true
19        end
20
21        #- (BOOL)writeToURL:(NSURL *)inAbsoluteURL ofType:(NSString *)inTypeName error:(NSError **)outError
22        def writeToURL_ofType_error(url, type, error)
23                false
24        end
25
26        #- (void)windowControllerDidLoadWindowNib:(NSWindowController *)windowController
27        def windowControllerDidLoadWindowNib(cont)
28                log "wCDLWN", cont
29        end
30
31#       def dataRepresentationOfType(aType)
32#       end
33#
34#       def loadDataRepresentation_ofType(data, aType)
35#       end
36
37#       def displayName
38#       end
39        def windowControllerWillLoadNib(cont)
40                log cont
41        end
42
43        def winwowNibName
44                "CHMDocument"
45        end
46end
47
48#class MySearchField < NSSearchField
49#
50#       attr_accessor :list
51#
52#       ib_action :keyDown do |e|
53#               log "keyDown #{e.keyCode} #{e.characters}"
54#       end
55#
56#       ib_action :moveDown do |sender|
57#               log "moveDown"
58#       end
59#
60#       ib_action :mouseDown do |sender|
61#               log "mouseDown"
62#       end
63#
64#       ib_action :moveUp do |sender|
65#               log "moveUp"
66#       end
67#
68#       def performKeyEquivalent(e)
69#               log "performKeyEquivalent"
70#               false
71#       end
72#end
73
74
75class CHMWindowController < NSWindowController
76        ib_outlet :webview
77        ib_outlet :list
78        ib_outlet :drawer
79        ib_outlet :search
80
81        def windowDidLoad
82                @chm = self.document.chm
83                browse @chm.home
84                @now = @index = @chm.index.to_a.sort_by {|k,v| k} # cache
85                @list.setDataSource(self)
86                @list.setDoubleAction("clicked_")
87                @list.setAction("clicked_")
88                @search.setDelegate(self)
89                @drawer.open
90        end
91
92        # Tableview
93        def numberOfRowsInTableView(table)
94                @now.length
95        end
96
97        def tableView_objectValueForTableColumn_row(table, column, row)
98                @now[row][0]
99        end
100
101#       def tableView_setObjectValue_forTableColumn_row(table, value, column, row)
102#       end
103
104        def tableView_willDisplayCell_forTableColumn_row(table, cell, column, row)
105#               case column.identifier.to_s
106#               when 'regexp'
107#               when 'color'
108#                       _, r, g, b = */(..)(..)(..)$/.match(cell.stringValue.to_s).to_a.map{|i| i.to_i(16) / 255.0 }
109#                       color = NSColor.colorWithCalibratedRed_green_blue_alpha(r, g, b, 1)
110#                       cell.setDrawsBackground(true)
111#                       cell.setTextColor(color)
112#                       cell.setBackgroundColor(NSColor.blackColor)
113#               when 'hilight'
114#               end
115        end
116
117        def textShouldBeginEditing(text)
118                true
119        end
120
121        def textShouldEndEditing(text)
122                true
123        end
124
125        def acceptsFirstResponder
126                true
127        end
128
129        def controlTextDidChange(anot)
130                filtering @search.stringValue
131                @list.selectRowIndexes_byExtendingSelection(NSIndexSet.alloc.initWithIndex(0), false)
132        end
133
134        def controlTextDidEndEditing(anot)
135                log "end #{@now.first.inspect}"
136                browse @now.first[1].first
137        end
138
139        def filtering(str)
140                if str =~ /[A-Z]/
141                        r = /^#{str}/
142                else
143                        r = /^#{str}/i
144                end
145                @now = @index.select {|k,v|
146                        k =~ r
147                }.sort_by {|k,v| k }
148                @list.reloadData
149        end
150
151        def clicked(sender)
152                browse @now[@list.selectedRow][1].first
153        end
154
155        def browse(path)
156                if path
157                        path = "/#{path}" unless path[0] == ?/
158                        r = NSURLRequest.requestWithURL CHMInternalURLProtocol.url_for(@chm, path)
159                        @webview.mainFrame.loadRequest r
160                end
161        end
162
163        def searchActivate(sender)
164                log "activate"
165                @search.window.makeFirstResponder(@search)
166        end
167end
Note: See TracBrowser for help on using the browser.