Platforms to show: All Mac Windows Linux Cross-Platform

/MacFrameworks/Cocoa Controls/Custom NSView


Required plugins for this example: MBS MacBase Plugin, MBS Main Plugin, MBS MacFrameworks Plugin, MBS MacControls Plugin, MBS MacCocoa Plugin

Last modified Sat, 10th May 2024.

You find this example project in your MBS Xojo Plugin download as a Xojo project file within the examples folder: /MacFrameworks/Cocoa Controls/Custom NSView

Download this example: Custom NSView.zip

Project "Custom NSView.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class Window1 Inherits Window
Control CocoaControlMBS1 Inherits CocoaControlMBS
ControlInstance CocoaControlMBS1 Inherits CocoaControlMBS
EventHandler Function GetView() As NSViewMBS n=new CustomNSView(0,0,me.Width,me.Height) Return n End EventHandler
End Control
Control Canvas2 Inherits Canvas
ControlInstance Canvas2 Inherits Canvas
End Control
Control NSViewControl1 Inherits NSViewControl
ControlInstance NSViewControl1 Inherits NSViewControl
End Control
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control Canvas1 Inherits Canvas
ControlInstance Canvas1 Inherits Canvas
End Control
Control Label2 Inherits Label
ControlInstance Label2 Inherits Label
End Control
Control Label3 Inherits Label
ControlInstance Label3 Inherits Label
End Control
Control Label4 Inherits Label
ControlInstance Label4 Inherits Label
End Control
Control Separator1 Inherits Separator
ControlInstance Separator1 Inherits Separator
End Control
EventHandler Sub Activate() var img As NSImageMBS var pic as Picture img = n.RenderImage() pic = img.CopyPicture Canvas1.Backdrop = pic img = NSViewControl1.NSViewMBS.RenderImage() pic = img.CopyPicture Canvas2.Backdrop = pic End EventHandler
EventHandler Sub Open() if not CocoaControlMBS1.Available then MsgBox "This example requires Mac OS X 10.5." quit end if End EventHandler
Property n As CustomNSView
End Class
Class NSViewControl Inherits NSViewControlMBS
EventHandler Sub DrawRect(g as NSGraphicsMBS, left as double, top as double, width as double, height as double) g = FontDraw.Make(g,left,top,width,height) End EventHandler
EventHandler Function isFlipped() As Boolean return false End EventHandler
End Class
Class CustomNSView Inherits CustomNSViewMBS
EventHandler Sub DrawRect(g as NSGraphicsMBS, left as double, top as double, width as double, height as double) g = FontDraw.Make(g,Left,top,width,height) End EventHandler
EventHandler Function isFlipped() As Boolean return false End EventHandler
End Class
Module FontDraw
Protected Function Make(g as NSGraphicsMBS, left as double, top as double, width as double, height as double) As NSGraphicsMBS //setting 'font size var fontSize as Double = Round((width/2)) 'font info var upm as Integer = 2048 'Helvetica var descender as Integer = -348 'Helvetica 'shift position var shiftPos as Double = 0.5 'axis dim baselinePosY as Double = Round(((height-fontSize)/2)+(-(descender/upm)*fontSize))+shiftPos dim zeroPosX as Double = Round((width-fontSize)/2)+shiftPos //draw line var pStart as NSPointMBS var pEnd as NSPointMBS 'back color g.setFillColor NSColorMBS.whiteColor g.fillRect(NSMakeRectMBS(0, 0, width, height)) 'border Rect g.setFillColor NSColorMBS.grayColor g.strokeRect(NSMakeRectMBS(0.5, 0.5, width-1, height-1)) 'X-axis baseline g.setStrokeColor NSColorMBS.grayColor pStart = New NSPointMBS(0,baselinePosY) pEnd = new NSPointMBS(Width,baselinePosY) g.strokeLine(pStart, pEnd) 'Y-axis g.setStrokeColor NSColorMBS.grayColor pStart = New NSPointMBS(zeroPosX,0) pEnd = new NSPointMBS(zeroPosX,Height) g.strokeLine(pStart, pEnd) //draw glyph g = MakeGlyph(fontSize, baselinePosY, zeroPosX, shiftPos) //draw text var textFont as NSFontMBS = NSFontMBS.userFixedPitchFontOfSize(20.0) var textColor as NSColorMBS = NSColorMBS.grayColor var a as NSAttributedStringMBS var m as NSMutableAttributedStringMBS a = NSAttributedStringMBS.attributedStringWithString("Helvetica") m = a.mutableCopy m.addAttribute(a.NSFontAttributeName, textFont, new NSRangeMBS(0, m.length)) m.addAttribute(a.NSForegroundColorAttributeName, textColor, new NSRangeMBS(0, m.length)) g.drawAtPoint m, new NSPointMBS(10, height-30) Return g End Function
Private Function MakeGlyph(fontSize as double, baselinePosY as double, zeroPosX as double, shiftPos as Double) As NSGraphicsMBS 'font var theFont as NSFontMBS = NSFontMBS.fontWithName("Helvetica", fontSize) //draw path var glyphPosX as double = zeroPosX-shiftPos var glyphPosY as double = baselinePosY-shiftPos var nsg as new NSGraphicsMBS() var thePath as NSBezierPathMBS thePath = NSBezierPathMBS.bezierPath thePath.moveToPoint(NSMakePointMBS(glyphPosX,glyphPosY)) thePath.appendBezierPathWithGlyph(42,theFont) 'G var cBlack as NSColorMBS = NSColorMBS.blackColor nsg.setFillColor cBlack nsg.fill(thePath) Return nsg End Function
End Module
Sign
End Sign
End Project

See also:

Download this example: Custom NSView.zip

The items on this page are in the following plugins: MBS MacFrameworks Plugin.


The biggest plugin in space...