cocoagui.bmx 25 KB

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