cocoagui.bmx 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. Strict
  2. Import MaxGUI.MaxGUI
  3. Import Pub.MacOs
  4. Import brl.systemdefault
  5. Import "-framework WebKit"
  6. Import "cocoa.macos.m"
  7. Extern
  8. Function bbSystemEmitOSEvent( nsevent:Byte Ptr,nsview:Byte Ptr,source:Object )
  9. Function ScheduleEventDispatch()
  10. Function NSBegin()
  11. Function NSEnd()
  12. Function NSGetSysColor(colorindex,r:Int Ptr,g:Int Ptr, b:Int Ptr)
  13. Function NSColorRequester(r,g,b)
  14. Function NSSetPointer(shape)
  15. Function NSCharWidth(font:Byte Ptr,charcode)
  16. ' create
  17. Function NSInitGadget(gadget:TNSGadget)
  18. ' generic
  19. Function NSActiveGadget:Byte Ptr()
  20. Function NSFreeGadget(gadget:TNSGadget)
  21. Function NSClientWidth(gadget:TNSGadget)
  22. Function NSClientHeight(gadget:TNSGadget)
  23. Function NSRethink(gadget:TNSGadget)
  24. Function NSRedraw(gadget:TNSGadget)
  25. Function NSActivate(gadget:TNSGadget,code)
  26. Function NSState(gadget:TNSGadget)
  27. Function NSShow(gadget:TNSGadget,bool)
  28. Function NSEnable(gadget:TNSGadget,bool)
  29. Function NSCheck(gadget:TNSGadget,bool)
  30. Function NSSetNextView(gadget:TNSGadget,nextgadget:TNSGadget)
  31. Function NSSetHotKey(gadget:TNSGadget,hotkey,modifier)
  32. Function NSSetTooltip(gadget:TNSGadget,tip$)
  33. Function NSGetTooltip$(gadget:TNSGadget)
  34. Function NSSuperview:Byte Ptr(view:Byte Ptr)
  35. ' window
  36. Function NSSetStatus(gadget:TNSGadget,text$,pos)
  37. Function NSSetMinimumSize(gadget:TNSGadget,width,height)
  38. Function NSSetMaximumSize(gadget:TNSGadget,width,height)
  39. Function NSPopupMenu(gadget:TNSGadget,menu:TNSGadget)
  40. ' font
  41. Function NSRequestFont:Byte Ptr(font:Byte Ptr)
  42. Function NSLoadFont:Byte Ptr(name$,size:Double,flags)
  43. Function NSGetDefaultFont:Byte Ptr()
  44. Function NSSetFont(gadget:TNSGadget,font:Byte Ptr)
  45. Function NSFontName$(font:Byte Ptr)
  46. Function NSFontStyle(font:Byte Ptr)
  47. Function NSFontSize:Double(font:Byte Ptr)
  48. ' items
  49. Function NSClearItems(gadget:TNSGadget)
  50. Function NSAddItem(gadget:TNSGadget,index,text$,tip$,image:Byte Ptr,extra:Object)
  51. Function NSSetItem(gadget:TNSGadget,index,text$,tip$,image:Byte Ptr,extra:Object)
  52. Function NSRemoveItem(gadget:TNSGadget,index)
  53. Function NSSelectItem(gadget:TNSGadget,index,state)
  54. Function NSSelectedItem(gadget:TNSGadget,index)
  55. Function NSSelectedNode:Byte Ptr(gadget:TNSGadget)
  56. ' text
  57. Function NSSetText(gadget:TNSGadget,text$)
  58. Function NSGetText$(gadget:TNSGadget)
  59. Function NSReplaceText(gadget:TNSGadget,pos,length,text$,units)
  60. Function NSAddText(gadget:TNSGadget,text$)
  61. Function NSAreaText$(gadget:TNSGadget,pos,length,units)
  62. Function NSAreaLen(gadget:TNSGadget,units)
  63. Function NSLockText(gadget:TNSGadget)
  64. Function NSUnlockText(gadget:TNSGadget)
  65. Function NSSetTabs(gadget:TNSGadget,tabwidth)
  66. Function NSSetMargins(gadget:TNSGadget,leftmargin)
  67. Function NSSetColor(gadget:TNSGadget,r,g,b)
  68. Function NSRemoveColor(gadget:TNSGadget)
  69. Function NSSetAlpha(gadget:TNSGadget,alpha#)
  70. Function NSSetTextColor(gadget:TNSGadget,r,g,b)
  71. Function NSGetCursorPos(gadget:TNSGadget,units)
  72. Function NSGetSelectionlength(gadget:TNSGadget,units)
  73. Function NSSetStyle(gadget:TNSGadget,r,g,b,flags,pos,length,units)
  74. Function NSSetSelection(gadget:TNSGadget,pos,length,units)
  75. Function NSCharAt(gadget:TNSGadget,line)
  76. Function NSLineAt(gadget:TNSGadget,index)
  77. Function NSCharX(gadget:TGadget,char)
  78. Function NSCharY(gadget:TGadget,char)
  79. ' prop
  80. Function NSSetValue(gadget:TNSGadget,value#)
  81. ' slider
  82. Function NSSetSlider(gadget:TNSGadget,value:Double,small:Double,big:Double)
  83. Function NSGetSlider:Double(gadget:TNSGadget)
  84. ' images for panels and nodes
  85. Function NSPixmapImage:Byte Ptr(image:TPixmap)
  86. Function NSSetImage(gadget:TNSGadget,nsimage:Byte Ptr,flags)
  87. Function NSSetIcon(gadget:TNSGadget,nsimage:Byte Ptr)
  88. Function NSCountKids(gadget:TNSGadget)
  89. ' html
  90. Function NSRun$(gadget:TNSGadget,script$)
  91. ' misc
  92. Function NSRelease(nsobject:Byte Ptr)
  93. ' system
  94. Function NSGetUserName$()
  95. Function NSGetComputerName$()
  96. EndExtern
  97. Global GadgetMap:TMap=New TMap
  98. maxgui_driver=New TCocoaMaxGuiDriver
  99. Type TCocoaMaxGUIDriver Extends TMaxGUIDriver
  100. Global CocoaGuiFont:TCocoaGuiFont
  101. Method New()
  102. NSBegin
  103. atexit_ NSEnd
  104. If Not CocoaGuiFont Then CocoaGuiFont = TCocoaGuiFont(LibraryFont(GUIFONT_SYSTEM))
  105. End Method
  106. Method UserName$()
  107. Return NSGetUserName$()
  108. End Method
  109. Method ComputerName$()
  110. Return NSGetComputerName$()
  111. End Method
  112. Method CreateGadget:TGadget(internalclass,name$,x,y,w,h,group:TGadget,style)
  113. Local p,hotkey
  114. If internalclass=GADGET_MENUITEM
  115. name=name.Replace("&","")
  116. ElseIf internalclass=GADGET_BUTTON
  117. p=name.Find("&")
  118. If p>-1
  119. ' hotkey=Asc(name[p..p+1]) 'to do - convert and call SetHotKey before return
  120. name=name[..p]+name[p+1..]
  121. EndIf
  122. ElseIf internalclass=GADGET_TOOLBAR
  123. Global _toolbarcount
  124. _toolbarcount:+1
  125. name="Toolbar"+_toolbarcount
  126. EndIf
  127. Local gadget:TNSGadget = TNSGadget.Create(internalclass,name,x,y,w,h,TNSGadget(group),style)
  128. If internalclass<>GADGET_WINDOW And internalclass<>GADGET_MENUITEM And internalclass<>GADGET_DESKTOP
  129. gadget.SetLayout EDGE_CENTERED,EDGE_CENTERED,EDGE_CENTERED,EDGE_CENTERED
  130. EndIf
  131. If group Then gadget._SetParent group
  132. gadget.SetTextColor(0,0,0)
  133. gadget.LinkView
  134. Return gadget
  135. End Method
  136. Function CreateFont:TGuiFont(handle:Byte Ptr,flags=FONT_NORMAL)
  137. Local font:TGuiFont = New TCocoaGuiFont
  138. font.handle = handle
  139. font.name = NSFontName(handle)
  140. font.size = NSFontSize(handle)
  141. font.style = NSFontStyle(handle)|flags
  142. Return font
  143. EndFunction
  144. Method LoadFont:TGuiFont(name$,size,flags)
  145. Return CreateFont(NSLoadFont(name,Double(size),flags),flags)
  146. End Method
  147. Method LoadFontWithDouble:TGuiFont(name$,size:Double,flags)
  148. Return CreateFont(NSLoadFont(name,size,flags),flags)
  149. End Method
  150. Method LibraryFont:TGuiFont( pFontType% = GUIFONT_SYSTEM, pFontSize:Double = 0, pFontStyle% = FONT_NORMAL )
  151. If pFontType = GUIFONT_SYSTEM Then
  152. Local tmpHandle:Byte Ptr = NSGetDefaultFont()
  153. If pFontSize <= 0 Then pFontSize = NSFontSize(tmpHandle)
  154. Return LoadFontWithDouble( NSFontName(tmpHandle), pFontSize, NSFontStyle(tmpHandle)|pFontStyle )
  155. Else
  156. Return Super.LibraryFont( pFontType, pFontSize, pFontStyle )
  157. EndIf
  158. EndMethod
  159. Method LookupColor( colorindex:Int, red:Byte Var, green:Byte Var, blue:Byte Var )
  160. Local r, g, b
  161. If NSGetSysColor( colorindex, Varptr r, Varptr g, Varptr b )
  162. red = r & $FF
  163. green = g & $FF
  164. blue = b & $FF
  165. Return True
  166. EndIf
  167. Return Super.LookupColor( colorindex, red, green, blue )
  168. EndMethod
  169. Method RequestColor(r,g,b)
  170. Return NSColorRequester(r,g,b)
  171. End Method
  172. Method RequestFont:TGuiFont(font:TGuiFont)
  173. Local handle:Byte Ptr
  174. If font handle=font.handle
  175. handle=NSRequestFont(handle)
  176. If handle
  177. If font And handle=font.handle Return font
  178. Return CreateFont(handle)
  179. EndIf
  180. End Method
  181. Method SetPointer(shape)
  182. NSSetPointer shape
  183. End Method
  184. Method ActiveGadget:TGadget()
  185. PollSystem()
  186. Local handle:Byte Ptr = NSActiveGadget()
  187. If handle Return GadgetFromHandle(handle)
  188. End Method
  189. Method LoadIconStrip:TIconStrip(source:Object)
  190. Return TCocoaIconStrip.Create(source)
  191. End Method
  192. End Type
  193. Function GadgetFromHandle:TNSGadget( handle:Byte Ptr )
  194. Return TNSGadget( GadgetMap.ValueForKey( TPtrWrapper.Create(handle) ) )
  195. End Function
  196. Function EmitCocoaOSEvent( event:Byte Ptr,handle:Byte Ptr,gadget:Object = Null )
  197. Local owner:TGadget = TGadget(gadget)
  198. If Not owner Then owner = GadgetFromHandle( handle )
  199. If owner Then
  200. While owner.source
  201. owner = owner.source
  202. Wend
  203. EndIf
  204. bbSystemEmitOSEvent event,handle,owner
  205. End Function
  206. Function EmitCocoaMouseEvent( event:Byte Ptr, handle:Byte Ptr )
  207. Local gadget:TNSGadget
  208. ' While handle
  209. gadget = GadgetFromHandle( handle )
  210. If gadget Then
  211. If (gadget.sensitivity & SENSITIZE_MOUSE) Then
  212. EmitCocoaOSEvent( event, handle, gadget )
  213. Return 1
  214. EndIf
  215. Return 0
  216. EndIf
  217. ' handle = NSSuperview(handle)
  218. ' Wend
  219. End Function
  220. Function EmitCocoaKeyEvent( event:Byte Ptr, handle:Byte Ptr )
  221. Local gadget:TNSGadget
  222. While handle
  223. gadget = GadgetFromHandle( handle )
  224. If gadget Then
  225. If (gadget.sensitivity & SENSITIZE_KEYS) Then
  226. EmitCocoaOSEvent( event, handle, gadget )
  227. Return 1
  228. EndIf
  229. Return 0
  230. EndIf
  231. handle = NSSuperview(handle)
  232. Wend
  233. End Function
  234. ?Not x64
  235. Function PostCocoaGuiEvent( id,handle:Byte Ptr,data,mods,x,y,extra:Object )
  236. ?x64
  237. Function PostCocoaGuiEvent( id,handle:Byte Ptr,data:Long,mods,x,y,extra:Object )
  238. ?
  239. 'DebugLog "PostCocoaGuiEvent"
  240. Local gadget:TNSGadget
  241. DispatchGuiEvents()
  242. If handle Then
  243. gadget = GadgetFromHandle(handle)
  244. If gadget Then
  245. Select gadget.internalclass
  246. Case GADGET_TREEVIEW
  247. extra=GadgetFromHandle(data)
  248. data = 0
  249. EndSelect
  250. Select id
  251. Case EVENT_WINDOWSIZE
  252. If gadget.width <> x Or gadget.height <> y Then
  253. gadget.SetRect gadget.xpos,gadget.ypos,x,y
  254. gadget.LayoutKids
  255. Else
  256. Return
  257. EndIf
  258. Case EVENT_WINDOWMOVE
  259. If gadget.xpos <> x Or gadget.ypos <> y Then
  260. gadget.SetRect x,y,gadget.width,gadget.height
  261. Else
  262. Return
  263. EndIf
  264. Case EVENT_MENUACTION
  265. extra=TNSGadget.popupextra
  266. TNSGadget.popupextra=Null
  267. Case EVENT_GADGETACTION
  268. Select gadget.internalclass
  269. Case GADGET_SLIDER
  270. Local oldValue:Int = gadget.GetProp()
  271. If data Then
  272. Select (gadget.style&(SLIDER_SCROLLBAR|SLIDER_TRACKBAR|SLIDER_STEPPER))
  273. Case SLIDER_SCROLLBAR
  274. If data > 1 Then
  275. data = gadget.small
  276. ElseIf data < -1 Then
  277. data = -gadget.small
  278. EndIf
  279. EndSelect
  280. gadget.SetProp(oldValue+data)
  281. data=gadget.GetProp()
  282. If (data = oldValue) Then Return
  283. Else
  284. data=gadget.GetProp()
  285. EndIf
  286. Case GADGET_LISTBOX, GADGET_COMBOBOX, GADGET_TABBER
  287. If (data > -1 And data < gadget.items.length) extra=gadget.ItemExtra(data)
  288. Case GADGET_BUTTON
  289. Select (gadget.style&7)
  290. Case BUTTON_CHECKBOX
  291. If ButtonState(gadget) = CHECK_INDETERMINATE Then SetButtonState(gadget, CHECK_SELECTED )
  292. Case BUTTON_RADIO
  293. If (gadget.style&BUTTON_PUSH) Then SetButtonState(gadget,CHECK_SELECTED)
  294. gadget.ExcludeOthers()
  295. EndSelect
  296. data=ButtonState(gadget)
  297. Case GADGET_TOOLBAR
  298. If data>-1 Then
  299. extra=gadget.ItemExtra(data)
  300. If (gadget.ItemFlags(data)&GADGETITEM_TOGGLE) Then gadget.SelectItem(data,2)
  301. EndIf
  302. EndSelect
  303. Case EVENT_GADGETSELECT, EVENT_GADGETMENU
  304. Select gadget.internalclass
  305. Case GADGET_LISTBOX, GADGET_COMBOBOX, GADGET_TABBER
  306. If data>-1 Then extra=gadget.ItemExtra(data)
  307. EndSelect
  308. Case EVENT_GADGETLOSTFOCUS
  309. QueueGuiEvent id,gadget,data,mods,x,y,extra
  310. ScheduleEventDispatch()
  311. Return
  312. EndSelect
  313. EndIf
  314. EndIf
  315. PostGuiEvent id,gadget,data,mods,x,y,extra
  316. EndFunction
  317. Function FilterKeyDown( handle:Byte Ptr,key,mods )
  318. Local source:TNSGadget
  319. If handle
  320. source=GadgetFromHandle(handle)
  321. EndIf
  322. If source And source.eventfilter<>Null
  323. Local event:TEvent=CreateEvent(EVENT_KEYDOWN,source,key,mods)
  324. Return source.eventfilter(event,source.context)
  325. EndIf
  326. Return 1
  327. End Function
  328. Function FilterChar( handle:Byte Ptr,key,mods )
  329. Local source:TNSGadget
  330. Select key
  331. ' Return true if they are arrow key characters
  332. Case 63232, 63233, 63234, 63235
  333. Return 1
  334. EndSelect
  335. If handle
  336. source=GadgetFromHandle(handle)
  337. EndIf
  338. If source And source.eventfilter<>Null 'Return source.charfilter(char,mods,source.context)
  339. Local event:TEvent=CreateEvent(EVENT_KEYCHAR,source,key,mods)
  340. Return source.eventfilter(event,source.context)
  341. EndIf
  342. Return 1
  343. End Function
  344. Type TNSGadget Extends TGadget
  345. Field internalclass, origclass 'internalclass: Class the Cocoa driver uses to draw the gadget, origclass: Expected class to be returned by Class() method
  346. Field handle:Byte Ptr
  347. Field view:Byte Ptr, textcolor:Byte Ptr 'view: NSView handle, textcolor: NSColor handle for Objective-C code
  348. Field intFontStyle 'Copy of font.style used by cocoa.macos.m to handle underlining/strikethrough etc. that isn't included in NSFont
  349. Field pixmap:TPixmap
  350. Field icons:TCocoaIconStrip
  351. Field small, big
  352. Field canvas:TGraphics
  353. Field font:TCocoaGuiFont
  354. Field enabled:Int = True, forceDisable:Int = False
  355. ' main factory command
  356. Function Create:TNSGadget(internalclass,text$,x,y,w,h,group:TGadget,style)
  357. Local gadget:TNSGadget = New TNSGadget
  358. gadget.origclass = internalclass
  359. gadget.internalclass = internalclass
  360. If Not group And internalclass<>GADGET_DESKTOP Then group = Desktop()
  361. gadget.parent = group
  362. gadget.name = text
  363. gadget.SetRect x,y,w,h 'setarea
  364. gadget.style = style
  365. gadget.font = TCocoaMaxGUIDriver.CocoaGUIFont
  366. If TNSGadget(group) Then
  367. gadget.forceDisable = Not (TNSGadget(group).enabled And Not TNSGadget(group).forceDisable)
  368. EndIf
  369. NSInitGadget gadget
  370. If internalclass<>GADGET_TOOLBAR 'toolbars retain name to key insertgadgetitem
  371. gadget.name = Null
  372. EndIf
  373. GadgetMap.Insert TPtrWrapper.Create(gadget.handle),gadget
  374. If gadget.view And gadget.handle <> gadget.view Then
  375. GadgetMap.Insert TPtrWrapper.Create(gadget.view),gadget
  376. EndIf
  377. If internalclass=GADGET_SLIDER Then gadget.SetRange(1,10)
  378. gadget.LockLayout()
  379. If (internalclass=GADGET_WINDOW) And (style&WINDOW_STATUS) Then
  380. If (style&WINDOW_CLIENTCOORDS) Then
  381. gadget.SetMinimumSize(25,0)
  382. Else
  383. gadget.SetMinimumSize(25,70)
  384. EndIf
  385. EndIf
  386. If LocalizationMode() & LOCALIZATION_OVERRIDE Then LocalizeGadget(gadget,text,"")
  387. gadget.SetEnabled(gadget.enabled)
  388. Return gadget
  389. End Function
  390. Method Class()
  391. Return origclass
  392. EndMethod
  393. Function ToView:TNSGadget(value:Object)
  394. Local view:TNSGadget = TNSGadget(value)
  395. If Not view Return
  396. Select view.internalclass
  397. Case GADGET_DESKTOP,GADGET_WINDOW,GADGET_TOOLBAR,GADGET_LABEL,GADGET_PROGBAR,GADGET_MENUITEM,GADGET_NODE
  398. Return Null
  399. End Select
  400. Return view
  401. End Function
  402. Method LinkView()
  403. Local First:TNSGadget
  404. Local prev:TNSGadget
  405. Local i,n
  406. If Not parent Return
  407. If Not ToView(Self) Return
  408. n=parent.kids.count()-1
  409. If n<0 Return
  410. ' find first view in family
  411. For i=0 Until n
  412. First=ToView(parent.kids.ValueAtIndex(i))
  413. If First Exit
  414. Next
  415. If Not First Return
  416. ' find last view in family
  417. For i=n-1 To 0 Step -1
  418. prev=ToView(parent.kids.ValueAtIndex(i))
  419. If prev Exit
  420. Next
  421. If Not prev Return
  422. NSSetNextView(prev,Self)
  423. NSSetNextView(Self,First)
  424. End Method
  425. Method Delete()
  426. Free()
  427. End Method
  428. ' generic gadget commands
  429. Method Query:Byte Ptr(queryid)
  430. Select queryid
  431. Case QUERY_NSVIEW
  432. Return handle
  433. Case QUERY_NSVIEW_CLIENT
  434. Return view
  435. End Select
  436. End Method
  437. Method Free()
  438. If handle Then
  439. If canvas Then canvas.close
  440. GadgetMap.Remove TPtrWrapper.Create(handle)
  441. If view And handle <> view Then
  442. GadgetMap.Remove TPtrWrapper.Create(view)
  443. view = Null
  444. EndIf
  445. If parent Then
  446. parent.kids.Remove Self
  447. End If
  448. NSFreeGadget Self
  449. font = Null
  450. handle = Null
  451. EndIf
  452. End Method
  453. Method Rethink() 'resize - was recursive
  454. NSRethink( Self )
  455. End Method
  456. Method ClientWidth()
  457. Return Max(NSClientWidth(Self),0)
  458. End Method
  459. Method ClientHeight()
  460. Return Max(NSClientHeight(Self),0)
  461. End Method
  462. Method Activate(cmd)
  463. NSActivate( Self, cmd )
  464. End Method
  465. Method State()
  466. Local tmpState:Int = NSState(Self)&~STATE_DISABLED
  467. If Not enabled Then tmpState:|STATE_DISABLED
  468. Return tmpState
  469. End Method
  470. Method SetShow(bool)
  471. NSShow( Self, bool )
  472. End Method
  473. Method SetText(msg$)
  474. If internalclass=GADGET_HTMLVIEW
  475. Local anchor$,a
  476. a=msg.Find("#")
  477. If a<>-1 anchor=msg[a..];msg=msg[..a]
  478. If msg[0..7].ToLower()<>"http://" And msg[0..7].ToLower()<>"file://"
  479. If FileType(msg)
  480. msg="file://"+msg
  481. Else
  482. msg="http://"+msg
  483. EndIf
  484. EndIf
  485. msg:+anchor
  486. msg=msg.Replace(" ","%20")
  487. ElseIf internalclass=GADGET_MENUITEM
  488. msg=msg.Replace("&", "")
  489. EndIf
  490. NSSetText Self,msg
  491. End Method
  492. Method Run$(msg$)
  493. If internalclass=GADGET_HTMLVIEW Return NSRun(Self,msg)
  494. End Method
  495. Method GetText$()
  496. Return NSGetText(Self)
  497. End Method
  498. Method SetFont(pFont:TGuiFont)
  499. If Not TCocoaGuiFont(pFont) Then pFont = TCocoaMaxGUIDriver.CocoaGuiFont
  500. font = TCocoaGuiFont(pFont)
  501. intFontStyle = font.style
  502. NSSetFont( Self, font.handle )
  503. End Method
  504. Method SetColor(r,g,b)
  505. NSSetColor Self,r,g,b
  506. End Method
  507. Method RemoveColor()
  508. NSRemoveColor Self
  509. End Method
  510. Method SetAlpha(alpha#)
  511. NSSetAlpha Self,alpha
  512. End Method
  513. Method SetTextColor(r,g,b)
  514. NSSetTextColor Self,r,g,b
  515. End Method
  516. Method SetPixmap(pixmap:TPixmap,flags)
  517. Local nsimage:Byte Ptr, x
  518. If pixmap
  519. Select pixmap.format
  520. Case PF_I8,PF_BGR888
  521. pixmap=pixmap.Convert( PF_RGB888 )
  522. Case PF_A8,PF_BGRA8888
  523. pixmap=pixmap.Convert( PF_RGBA8888 )
  524. End Select
  525. If AlphaBitsPerPixel[ pixmap.format ]
  526. For Local y=0 Until pixmap.height
  527. For x=0 Until pixmap.width
  528. Local argb=pixmap.ReadPixel( x,y )
  529. pixmap.WritePixel x,y,premult(argb)
  530. Next
  531. Next
  532. EndIf
  533. nsimage=NSPixmapImage(pixmap)
  534. EndIf
  535. NSSetImage(Self,nsimage,flags)
  536. End Method
  537. Method SetTooltip(pTip$)
  538. Select internalclass
  539. Case GADGET_WINDOW, GADGET_DESKTOP, GADGET_LISTBOX, GADGET_MENUITEM, GADGET_TOOLBAR, GADGET_TABBER, GADGET_NODE
  540. Default;Return NSSetTooltip( Self, pTip )
  541. EndSelect
  542. EndMethod
  543. Method GetTooltip$()
  544. Select internalclass
  545. Case GADGET_WINDOW, GADGET_DESKTOP, GADGET_LISTBOX, GADGET_MENUITEM, GADGET_TOOLBAR, GADGET_TABBER, GADGET_NODE
  546. Default;Return NSGetTooltip( Self )
  547. EndSelect
  548. EndMethod
  549. Method ExcludeOthers()
  550. For Local g:TNSGadget = EachIn parent.kids
  551. If g<>Self And g.internalclass=GADGET_BUTTON And (g.style&7)=BUTTON_RADIO
  552. NSCheck g,False
  553. EndIf
  554. Next
  555. End Method
  556. Method SetSelected(bool)
  557. NSCheck Self,bool
  558. If internalclass=GADGET_BUTTON And (style&7)=BUTTON_RADIO And bool
  559. ExcludeOthers
  560. EndIf
  561. End Method
  562. Method SetEnabled(enable)
  563. Local old:Int = enabled And Not forceDisable
  564. enabled = enable
  565. If Class() = GADGET_WINDOW Then
  566. NSEnable Self, enable
  567. Else
  568. enable = enable And Not forceDisable
  569. NSEnable Self, enable
  570. If (enable <> old) Then
  571. For Local tmpGadget:TNSGadget = EachIn kids
  572. tmpGadget.forceDisable = Not enable
  573. If tmpGadget.Class() <> GADGET_WINDOW Then tmpGadget.SetEnabled(tmpGadget.enabled)
  574. Next
  575. EndIf
  576. EndIf
  577. End Method
  578. Method SetHotKey(hotkey,modifier)
  579. NSSetHotKey Self,hotkey,modifier
  580. End Method
  581. ' window commands
  582. Field _statustext$
  583. Method GetStatusText$()
  584. Return _statustext
  585. EndMethod
  586. Method SetStatusText(msg$)
  587. Local t,m0$,m1$,m2$
  588. _statustext = msg
  589. m0=msg
  590. t=m0.find("~t");If t<>-1 m1=m0[t+1..];m0=m0[..t];
  591. t=m1.find("~t");If t<>-1 m2=m1[t+1..];m1=m1[..t];
  592. NSSetStatus Self,m0,0
  593. NSSetStatus Self,m1,1
  594. NSSetStatus Self,m2,2
  595. End Method
  596. Method GetMenu:TGadget()
  597. Return Self
  598. End Method
  599. Global popupextra:Object
  600. Method PopupMenu(menu:TGadget,extra:Object)
  601. popupextra=extra
  602. NSPopupMenu Self,TNSGadget(menu)
  603. End Method
  604. Method UpdateMenu()
  605. End Method
  606. Method SetMinimumSize(w,h)
  607. NSSetMinimumSize Self,w,h
  608. End Method
  609. Method SetMaximumSize(w,h)
  610. NSSetMaximumSize Self,w,h
  611. End Method
  612. Method SetIconStrip(iconstrip:TIconStrip)
  613. icons=TCocoaIconStrip(iconstrip)
  614. End Method
  615. ' item handling commands
  616. Method ClearListItems()
  617. NSClearItems Self
  618. End Method
  619. Method InsertListItem(index,item$,tip$,icon,extra:Object)
  620. Local image:Byte Ptr
  621. If internalclass=GADGET_TOOLBAR
  622. item=name+":"+index
  623. EndIf
  624. If icons And icon>=0 image=icons.images[icon]
  625. NSAddItem Self,index,item,tip,image,extra
  626. End Method
  627. Method SetListItem(index,item$,tip$,icon,extra:Object)
  628. Local image:Byte Ptr
  629. If internalclass=GADGET_TOOLBAR
  630. item=name+":"+index
  631. EndIf
  632. If icons And icon>=0 image=icons.images[icon]
  633. NSSetItem Self,index,item,tip,image,extra
  634. End Method
  635. Method RemoveListItem(index)
  636. NSRemoveItem Self,index
  637. End Method
  638. Method SetListItemState(index,state)
  639. NSSelectItem Self,index,state
  640. End Method
  641. Method ListItemState(index)
  642. Return NSSelectedItem(Self,index)
  643. End Method
  644. ' treeview commands
  645. Method RootNode:TGadget()
  646. Return Self
  647. End Method
  648. Method SetIcon(icon)
  649. Local p:TNSGadget
  650. p=Self
  651. While p
  652. If p.icons Exit
  653. p=TNSGadget(p.parent)
  654. Wend
  655. If p
  656. If icon>-1
  657. NSSetIcon Self,p.icons.images[icon]
  658. Else
  659. NSSetIcon Self,Null
  660. EndIf
  661. EndIf
  662. End Method
  663. Method InsertNode:TGadget(index,text$,icon)
  664. Local node:TNSGadget = Create(GADGET_NODE,text,0,0,0,0,Self,index)
  665. node.SetIcon icon
  666. node._SetParent Self
  667. Return node
  668. End Method
  669. Method ModifyNode(text$,icon)
  670. NSSetText Self,text
  671. SetIcon icon
  672. End Method
  673. Method SelectedNode:TGadget()
  674. Local index:Byte Ptr = NSSelectedNode(Self)
  675. If (index) Return GadgetFromHandle(index)
  676. End Method
  677. Method CountKids()
  678. Return NSCountKids(Self)
  679. End Method
  680. ' textarea commands
  681. Method ReplaceText(pos,length,text$,units)
  682. ?debug
  683. If pos<0 Or pos+length>AreaLen(units) Throw "Illegal Range"
  684. ?
  685. NSReplaceText Self,pos,length,text$,units
  686. End Method
  687. Method AddText(text$)
  688. NSAddText Self,text
  689. End Method
  690. Method AreaText$(pos,length,units)
  691. ?debug
  692. If pos<0 Or pos+length>AreaLen(units) Throw "Illegal Range"
  693. ?
  694. Return NSAreaText(Self,pos,length,units)
  695. End Method
  696. Method AreaLen(units)
  697. Return NSAreaLen(Self,units)
  698. End Method
  699. Method LockText()
  700. NSLockText Self
  701. End Method
  702. Method UnlockText()
  703. NSUnlockText Self
  704. End Method
  705. Method SetTabs(tabwidth)
  706. NSSetTabs Self,tabwidth
  707. End Method
  708. Method SetMargins(leftmargin)
  709. NSSetMargins Self,leftmargin
  710. End Method
  711. Method GetCursorPos(units)
  712. Return NSGetCursorPos(Self,units)
  713. End Method
  714. Method GetSelectionLength(units)
  715. Return NSGetSelectionLength(Self,units)
  716. End Method
  717. Method SetStyle(r,g,b,flags,pos,length,units)
  718. ?debug
  719. If pos<0 Or pos+length>AreaLen(units) Throw "Illegal Range"
  720. ?
  721. If length NSSetStyle Self,r,g,b,flags,pos,length,units
  722. End Method
  723. Method SetSelection(pos,length,units)
  724. ?debug
  725. If pos<0 Or pos+length>AreaLen(units) Throw "Illegal Range"
  726. ?
  727. NSSetSelection Self,pos,length,units
  728. End Method
  729. Method CharAt(line)
  730. ?debug
  731. If line<0 Or line>AreaLen(TEXTAREA_LINES) Throw "Parameter Out Of Range"
  732. ?
  733. Return NSCharAt(Self,line)
  734. End Method
  735. Method LineAt(index)
  736. ?debug
  737. If index<0 Or index>AreaLen(TEXTAREA_CHARS) Throw "Parameter Out Of Range"
  738. ?
  739. Return NSLineAt(Self,index)
  740. End Method
  741. Method CharX(char)
  742. Return NSCharX(Self,char)
  743. EndMethod
  744. Method CharY(char)
  745. Return NSCharY(Self,char)
  746. EndMethod
  747. ' progbar
  748. Method SetValue(value#)
  749. NSSetValue Self,value
  750. End Method
  751. ' slider / scrollbar
  752. Method SetRange(_small,_big)
  753. small=_small
  754. big=_big
  755. NSSetSlider Self,GetProp(),small,big
  756. End Method
  757. Method SetProp(pos)
  758. NSSetSlider Self,pos,small,big
  759. End Method
  760. Method GetProp()
  761. Local value:Double = NSGetSlider(Self)
  762. If Not (style&(SLIDER_TRACKBAR|SLIDER_STEPPER))
  763. value:*(big-small)
  764. If value>big-small value=big-small
  765. EndIf
  766. Return Int(value+0.5:Double)
  767. End Method
  768. ' canvas
  769. Method AttachGraphics:TGraphics( flags )
  770. canvas=brl.Graphics.AttachGraphics( Query(QUERY_NSVIEW_CLIENT),flags )
  771. End Method
  772. Method CanvasGraphics:TGraphics()
  773. Return canvas
  774. End Method
  775. End Type
  776. Type TCocoaIconStrip Extends TIconStrip
  777. Field images:Byte Ptr[]
  778. Function IsNotBlank(pixmap:TPixmap)
  779. Local y, h = pixmap.height
  780. Local c = pixmap.ReadPixel(0,0)
  781. For Local x = 0 Until h
  782. For y = 0 Until h
  783. If pixmap.ReadPixel(x,y)<>c Return True
  784. Next
  785. Next
  786. End Function
  787. Function Create:TCocoaIconStrip(source:Object)
  788. Local icons:TCocoaIconStrip
  789. Local pixmap:TPixmap,pix:TPixmap
  790. Local n,x,w
  791. pixmap=TPixmap(source)
  792. If Not pixmap pixmap=LoadPixmap(source)
  793. If Not pixmap Return
  794. Select pixmap.format
  795. Case PF_I8,PF_BGR888
  796. pixmap=pixmap.Convert( PF_RGB888 )
  797. Case PF_A8,PF_BGRA8888
  798. pixmap=pixmap.Convert( PF_RGBA8888 )
  799. End Select
  800. If AlphaBitsPerPixel[ pixmap.format ]
  801. For Local y=0 Until pixmap.height
  802. For x=0 Until pixmap.width
  803. Local argb=pixmap.ReadPixel( x,y )
  804. pixmap.WritePixel x,y,premult(argb)
  805. Next
  806. Next
  807. EndIf
  808. n=pixmap.width/pixmap.height;
  809. If n=0 Return
  810. icons=New TCocoaIconStrip
  811. icons.pixmap=pixmap
  812. icons.count=n
  813. icons.images=New Byte Ptr[n]
  814. w=pixmap.width/n
  815. For x=0 Until n
  816. pix=pixmap.Window(x*w,0,w,pixmap.height)
  817. If IsNotBlank(pix) icons.images[x]=NSPixmapImage(pix)
  818. Next
  819. Return icons
  820. EndFunction
  821. EndType
  822. Type TCocoaGuiFont Extends TGuiFont
  823. Method Delete()
  824. If handle Then
  825. NSRelease(handle)
  826. handle = 0
  827. EndIf
  828. EndMethod
  829. Method CharWidth(char)
  830. If handle
  831. Return NSCharWidth(handle,char)
  832. EndIf
  833. Return 0
  834. EndMethod
  835. EndType
  836. Type TPtrWrapper Final
  837. Field value:Byte Ptr
  838. Function Create:TPtrWrapper(value:Byte Ptr)
  839. Local tmpWrapper:TPtrWrapper = New TPtrWrapper
  840. tmpWrapper.value = value
  841. Return tmpWrapper
  842. EndFunction
  843. Method Compare( o:Object )
  844. Local c:TPtrWrapper = TPtrWrapper(o)
  845. If c Then Return (value - c.value)
  846. Return Super.Compare(o)
  847. EndMethod
  848. Method ToString$()
  849. 'Return value
  850. EndMethod
  851. EndType
  852. Private
  853. Function premult(argb)
  854. Local a = ((argb Shr 24) & $FF)
  855. Return ((((argb&$ff00ff)*a)Shr 8)&$ff00ff)|((((argb&$ff00)*a)Shr 8)&$ff00)|(a Shl 24)
  856. End Function