| 1 | #!rake ;# |
|---|
| 2 | |
|---|
| 3 | class 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 |
|---|
| 46 | end |
|---|
| 47 | |
|---|
| 48 | class MySearchWindow < NSWindow |
|---|
| 49 | |
|---|
| 50 | def sendEvent(e) |
|---|
| 51 | if e.oc_type == NSKeyDown |
|---|
| 52 | return if delegate.process_keybinds(e) |
|---|
| 53 | end |
|---|
| 54 | super_sendEvent(e) |
|---|
| 55 | end |
|---|
| 56 | |
|---|
| 57 | end |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | class CHMWindowController < NSWindowController |
|---|
| 61 | ib_outlet :webview |
|---|
| 62 | ib_outlet :list |
|---|
| 63 | ib_outlet :drawer |
|---|
| 64 | ib_outlet :search |
|---|
| 65 | |
|---|
| 66 | def windowDidLoad |
|---|
| 67 | @chm = self.document.chm |
|---|
| 68 | browse @chm.home |
|---|
| 69 | @now = @index = @chm.index.to_a.sort_by {|k,v| k} # cache |
|---|
| 70 | @list.setDataSource(self) |
|---|
| 71 | @list.setDoubleAction("clicked_") |
|---|
| 72 | @list.setAction("clicked_") |
|---|
| 73 | @search.setDelegate(self) |
|---|
| 74 | @drawer.open |
|---|
| 75 | end |
|---|
| 76 | |
|---|
| 77 | # Tableview |
|---|
| 78 | def numberOfRowsInTableView(table) |
|---|
| 79 | @now.length |
|---|
| 80 | end |
|---|
| 81 | |
|---|
| 82 | def tableView_objectValueForTableColumn_row(table, column, row) |
|---|
| 83 | @now[row][0] |
|---|
| 84 | end |
|---|
| 85 | |
|---|
| 86 | # def tableView_setObjectValue_forTableColumn_row(table, value, column, row) |
|---|
| 87 | # end |
|---|
| 88 | |
|---|
| 89 | def tableView_willDisplayCell_forTableColumn_row(table, cell, column, row) |
|---|
| 90 | # case column.identifier.to_s |
|---|
| 91 | # when 'regexp' |
|---|
| 92 | # when 'color' |
|---|
| 93 | # _, r, g, b = */(..)(..)(..)$/.match(cell.stringValue.to_s).to_a.map{|i| i.to_i(16) / 255.0 } |
|---|
| 94 | # color = NSColor.colorWithCalibratedRed_green_blue_alpha(r, g, b, 1) |
|---|
| 95 | # cell.setDrawsBackground(true) |
|---|
| 96 | # cell.setTextColor(color) |
|---|
| 97 | # cell.setBackgroundColor(NSColor.blackColor) |
|---|
| 98 | # when 'hilight' |
|---|
| 99 | # end |
|---|
| 100 | end |
|---|
| 101 | |
|---|
| 102 | def textShouldBeginEditing(text) |
|---|
| 103 | true |
|---|
| 104 | end |
|---|
| 105 | |
|---|
| 106 | def textShouldEndEditing(text) |
|---|
| 107 | true |
|---|
| 108 | end |
|---|
| 109 | |
|---|
| 110 | def acceptsFirstResponder |
|---|
| 111 | true |
|---|
| 112 | end |
|---|
| 113 | |
|---|
| 114 | def controlTextDidChange(anot) |
|---|
| 115 | filtering @search.stringValue |
|---|
| 116 | @list.selectRowIndexes_byExtendingSelection(NSIndexSet.alloc.initWithIndex(0), false) |
|---|
| 117 | end |
|---|
| 118 | |
|---|
| 119 | def controlTextDidEndEditing(anot) |
|---|
| 120 | log "end #{@now.first.inspect}" |
|---|
| 121 | #jumpToCurrent |
|---|
| 122 | end |
|---|
| 123 | |
|---|
| 124 | def jumpToCurrent(sender) |
|---|
| 125 | browse @now.first[1].first |
|---|
| 126 | end |
|---|
| 127 | |
|---|
| 128 | def filtering(str) |
|---|
| 129 | str = str.to_s |
|---|
| 130 | if str =~ /[A-Z]/ |
|---|
| 131 | r = /^#{str}/ |
|---|
| 132 | else |
|---|
| 133 | r = /^#{str}/i |
|---|
| 134 | end |
|---|
| 135 | @now = @index.select {|k,v| |
|---|
| 136 | k =~ r |
|---|
| 137 | }.sort_by {|k,v| k } |
|---|
| 138 | @list.reloadData |
|---|
| 139 | end |
|---|
| 140 | |
|---|
| 141 | def clicked(sender) |
|---|
| 142 | browse @now[@list.selectedRow][1].first |
|---|
| 143 | end |
|---|
| 144 | |
|---|
| 145 | def browse(path) |
|---|
| 146 | if path |
|---|
| 147 | path = "/#{path}" unless path[0] == ?/ |
|---|
| 148 | r = NSURLRequest.requestWithURL CHMInternalURLProtocol.url_for(@chm, path) |
|---|
| 149 | @webview.mainFrame.loadRequest r |
|---|
| 150 | end |
|---|
| 151 | end |
|---|
| 152 | |
|---|
| 153 | def completion(sender) |
|---|
| 154 | # not implemented yet |
|---|
| 155 | end |
|---|
| 156 | |
|---|
| 157 | # from menu |
|---|
| 158 | def searchActivate(sender) |
|---|
| 159 | log "activate" |
|---|
| 160 | @search.window.makeFirstResponder(@search) |
|---|
| 161 | end |
|---|
| 162 | |
|---|
| 163 | def nextCandidate(sender) |
|---|
| 164 | if @list.selectedRow <= @now.size |
|---|
| 165 | @list.selectRowIndexes_byExtendingSelection(NSIndexSet.alloc.initWithIndex(@list.selectedRow+1), false) |
|---|
| 166 | clicked(nil) |
|---|
| 167 | end |
|---|
| 168 | end |
|---|
| 169 | |
|---|
| 170 | def prevCandidate(sender) |
|---|
| 171 | if @list.selectedRow > 0 |
|---|
| 172 | @list.selectRowIndexes_byExtendingSelection(NSIndexSet.alloc.initWithIndex(@list.selectedRow-1), false) |
|---|
| 173 | clicked(nil) |
|---|
| 174 | end |
|---|
| 175 | end |
|---|
| 176 | |
|---|
| 177 | # from MySearchWindow |
|---|
| 178 | |
|---|
| 179 | def process_keybinds(e) |
|---|
| 180 | key = key_string(e) |
|---|
| 181 | #log "keyDown (#{e.characters}:#{e.charactersIgnoringModifiers}) -> '#{key}'" |
|---|
| 182 | keybinds = { |
|---|
| 183 | "C-j" => self.method(:nextCandidate), |
|---|
| 184 | "C-n" => self.method(:nextCandidate), |
|---|
| 185 | "C-k" => self.method(:prevCandidate), |
|---|
| 186 | "C-p" => self.method(:prevCandidate), |
|---|
| 187 | "\r" => self.method(:jumpToCurrent), |
|---|
| 188 | "\t" => self.method(:completion), |
|---|
| 189 | " " => Proc.new {|s| |
|---|
| 190 | @webview.stringByEvaluatingJavaScriptFromString <<-JS |
|---|
| 191 | window.scrollBy(0, 200); |
|---|
| 192 | JS |
|---|
| 193 | }, |
|---|
| 194 | "C-u" => Proc.new {|s| |
|---|
| 195 | @search.stringValue = "" |
|---|
| 196 | }, |
|---|
| 197 | "G-[" => Proc.new {|s| |
|---|
| 198 | @webview.goBack |
|---|
| 199 | }, |
|---|
| 200 | "G-]" => Proc.new {|s| |
|---|
| 201 | @webview.goForward |
|---|
| 202 | }, |
|---|
| 203 | "G-F" => Proc.new {|s| |
|---|
| 204 | @webview.performFindPanelAction(self) |
|---|
| 205 | }, |
|---|
| 206 | "G-=" => Proc.new {|s| |
|---|
| 207 | @webview.makeTextLarger(self) |
|---|
| 208 | }, |
|---|
| 209 | "G--" => Proc.new {|s| |
|---|
| 210 | @webview.makeTextSmaller(self) |
|---|
| 211 | }, |
|---|
| 212 | } |
|---|
| 213 | if keybinds.key?(key) |
|---|
| 214 | keybinds[key].call(self) |
|---|
| 215 | true |
|---|
| 216 | else |
|---|
| 217 | false |
|---|
| 218 | end |
|---|
| 219 | end |
|---|
| 220 | |
|---|
| 221 | def key_string(e) |
|---|
| 222 | key = "" |
|---|
| 223 | m = e.modifierFlags |
|---|
| 224 | key << "S-" if m & NSShiftKeyMask > 0 |
|---|
| 225 | key << "C-" if m & NSControlKeyMask > 0 |
|---|
| 226 | key << "M-" if m & NSAlternateKeyMask > 0 |
|---|
| 227 | key << "G-" if m & NSCommandKeyMask > 0 # TODO |
|---|
| 228 | key << e.charactersIgnoringModifiers.to_s |
|---|
| 229 | key |
|---|
| 230 | end |
|---|
| 231 | end |
|---|