Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import PackageDescription
/// update regions, and the content's buffer the host flushes.
/// - `GraphiteMenus`: the Menu Manager: menus and their items, the menu
/// bar, and the 'MENU' resources that define them.
/// - `GraphiteControls`: the Control Manager: controls of the five kinds,
/// their values, parts and states, the window's list of them, and the
/// 'CNTL' resources that define them.
/// - `Graphite`: re-exports the modules above.
///
/// Everything here depends only on the Swift standard library, the C
Expand Down Expand Up @@ -83,6 +86,10 @@ let package = Package(
.library(
name: "GraphiteMenus",
targets: ["GraphiteMenus"]
),
.library(
name: "GraphiteControls",
targets: ["GraphiteControls"]
)
],
// FreeType reads outline fonts; builds without it keep the bitmap font.
Expand Down Expand Up @@ -146,9 +153,13 @@ let package = Package(
name: "GraphiteMenus",
dependencies: ["GraphiteBase"]
),
.target(
name: "GraphiteControls",
dependencies: ["GraphiteBase"]
),
.target(
name: "Graphite",
dependencies: ["GraphiteBase", "GraphiteTasks", "GraphiteFiles", "GraphiteResources", "GraphiteAppleEvents", "GraphiteQuickDraw", "GraphiteText", "GraphiteSound", "GraphiteWindows", "GraphiteMenus"]
dependencies: ["GraphiteBase", "GraphiteTasks", "GraphiteFiles", "GraphiteResources", "GraphiteAppleEvents", "GraphiteQuickDraw", "GraphiteText", "GraphiteSound", "GraphiteWindows", "GraphiteMenus", "GraphiteControls"]
),
.testTarget(
name: "GraphiteBaseTests",
Expand Down Expand Up @@ -189,6 +200,10 @@ let package = Package(
.testTarget(
name: "GraphiteMenusTests",
dependencies: ["GraphiteMenus"]
),
.testTarget(
name: "GraphiteControlsTests",
dependencies: ["GraphiteControls"]
)
]
)
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ Any UI it shows will be drawn with [Cacao](https://github.com/PureSwift/Cacao).

> **Status:** early development. `GraphiteBase`, `GraphiteTasks`,
> `GraphiteFiles`, `GraphiteResources`, `GraphiteAppleEvents`,
> `GraphiteQuickDraw`, `GraphiteText`, `GraphiteSound`, `GraphiteWindows`
> and `GraphiteMenus` are in place, apart from the MACE codecs and
> double-buffered sound. The Toolbox's controls and dialogs come next.
> `GraphiteQuickDraw`, `GraphiteText`, `GraphiteSound`, `GraphiteWindows`,
> `GraphiteMenus` and `GraphiteControls` are in place, apart from the MACE
> codecs and double-buffered sound. The Toolbox's dialogs come next.

## Modules

Expand All @@ -45,6 +45,7 @@ Any UI it shows will be drawn with [Cacao](https://github.com/PureSwift/Cacao).
| `GraphiteSound` | The Sound Manager: sampled sounds from `'snd '` resources (formats 1 and 2, with the standard, compressed and extended headers) and AIFF and AIFF-C files, read to 16-bit PCM; the µ-law, A-law and IMA4 codecs (MACE is recognized but not decoded yet); sound channels with queued commands (`SndDoCommand`) including `callBackCmd`, `waitCmd` and `syncCmd`, immediate ones, volume, pan, pitch, loops and completion; a mixer that renders them to stereo float frames, for an audio device the host provides; and the frequencies of MIDI notes. |
| `GraphiteWindows` | The Window Manager: windows of every class, from documents, dialogs and alerts to palettes, sheets and drawers, each with the standard Mac OS 8 and 9 frame or a window definition the host brings (the WDEF); the window list ordered by layer, `FrontWindow`, and the active document and the active palette; `FindWindow` and every part of a frame; showing, hiding, selecting, zooming between the user and standard states, collapsing; `MoveWindow`, `SizeWindow` and `SetWindowBounds`, with centering, cascading and the alert position; each window's content in an offscreen buffer with its visible region — what no window in front covers — its update region with `BeginUpdate` and `EndUpdate`, and the part of it the host is told to flush. |
| `GraphiteMenus` | The Menu Manager: menus and their items — text in MacRoman, marks (the check mark and the diamond), icons, styles, keyboard equivalents with their modifiers, submenus by menu ID, command IDs — built with `AppendMenu`'s metacharacter strings or item by item; checked, enabled and disabled; the menu bar with `InsertMenu`'s order and its hierarchical list, `GetMenuHandle`, `MenuKey` and `EnableMenuCommand`; menu definitions (the MDEF) with the standard definition's sizes and item geometry; and `'MENU'` resources read with every field as the classic kept it, enable flags and submenus included. |
| `GraphiteControls` | The Control Manager: controls of the classic's five kinds — buttons, checkboxes, radio buttons, scroll bars and pop-up menus, their control definition IDs and the `useWFont` variation read as the kind beneath it; values, ranges and the pinning SetControlValue did, with the pop-up menu's resource ID and title width in its minimum and maximum as the classic kept them; titles, reference values, command IDs, identifiers, enabling, disabling, activating and `HiliteControl`'s pressed part; the parts a point hits (inButton, inCheckBox, a scroll bar's arrows, pages and thumb), the fixed-size thumb of the classic's scroll bars and the value a drag names; a window's control list, FindControl, GetControlByID and Carbon's root control; and `'CNTL'` resources read field by field. |
| `Graphite` | Re-exports the modules above. |

Everything depends only on the Swift standard library, the C library
Expand Down
1 change: 1 addition & 0 deletions Sources/Graphite/Graphite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
@_exported import GraphiteSound
@_exported import GraphiteWindows
@_exported import GraphiteMenus
@_exported import GraphiteControls
35 changes: 35 additions & 0 deletions Sources/GraphiteBase/MacError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ extension MacError: CustomStringConvertible {
-210: "buffersTooSmall", -223: "siInvalidCompression",
-5603: "menuPropertyInvalidErr", -5604: "menuPropertyNotFoundErr", -5620: "menuNotFoundErr",
-5621: "menuUsesSystemDefErr", -5622: "menuItemNotFoundErr", -5623: "menuInvalidErr",
-30581: "errDataNotSupported", -30582: "errControlDoesntSupportFocus",
-30583: "errWindowDoesntSupportFocus", -30584: "errUnknownControl", -30585: "errCouldntSetFocus",
-30586: "errNoRootControl", -30587: "errRootAlreadyExists", -30588: "errInvalidPartCode",
-30589: "errControlsAlreadyExist", -30590: "errControlIsNotEmbedder",
-30592: "errControlHiddenOrDisabled", -30596: "errItemNotControl",
]
guard let name = names[code] else { return "\(code)" }
return "\(name) (\(code))"
Expand Down Expand Up @@ -245,3 +250,33 @@ public extension MacError {
/// menuInvalidErr: the menu or its resource is invalid.
static var menuInvalid: MacError { MacError(-5623) }
}

// MARK: - Control Manager

public extension MacError {

/// errDataNotSupported: the control doesn't take the kind of data.
static var dataNotSupported: MacError { MacError(-30581) }
/// errControlDoesntSupportFocus.
static var controlDoesntSupportFocus: MacError { MacError(-30582) }
/// errWindowDoesntSupportFocus.
static var windowDoesntSupportFocus: MacError { MacError(-30583) }
/// errUnknownControl.
static var unknownControl: MacError { MacError(-30584) }
/// errCouldntSetFocus.
static var couldntSetFocus: MacError { MacError(-30585) }
/// errNoRootControl.
static var noRootControl: MacError { MacError(-30586) }
/// errRootAlreadyExists.
static var rootAlreadyExists: MacError { MacError(-30587) }
/// errInvalidPartCode.
static var invalidPartCode: MacError { MacError(-30588) }
/// errControlsAlreadyExist.
static var controlsAlreadyExist: MacError { MacError(-30589) }
/// errControlIsNotEmbedder.
static var controlIsNotEmbedder: MacError { MacError(-30590) }
/// errControlHiddenOrDisabled: it can't be focused or hit.
static var controlHiddenOrDisabled: MacError { MacError(-30592) }
/// errItemNotControl: a dialog item that isn't one.
static var itemNotControl: MacError { MacError(-30596) }
}
190 changes: 190 additions & 0 deletions Sources/GraphiteControls/Control.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
// A control: the rectangle it occupies in its window, its value and its
// range, its title and its reference value, its visibility, enabling and
// activity, and the action procedure the classic's TrackControl called as
// the user worked the control. The host draws it and tracks it; the
// definition gives its parts' shapes, and the Control Manager keeps the
// rest.

import GraphiteBase

#if canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(Darwin)
import Darwin
#endif

public final class Control {

/// The kind of control the standard definition functions draw; a
/// control a host defines of its own keeps nil here.
public let kind: ControlKind?

/// The shape of the control's parts. The standard kinds share one.
public let definition: any ControlDefinition

/// The control's rectangle, in its owner's local coordinates
/// (GetControlBounds, SetControlBounds, MoveControl, SizeControl).
public internal(set) var bounds: Rect

/// GetControlValue, SetControlValue: the control's setting — a button's
/// 0, a checkbox's or radio button's 0 or 1, a scroll bar's place in
/// its range, a pop-up menu's chosen item from 1.
public var value: Int32 = 0 {
didSet { value = min(max(value, minimum), max(minimum, maximum)) }
}

/// GetControlMinimum, SetControlMinimum.
public var minimum: Int32 = 0

/// GetControlMaximum, SetControlMaximum. A scroll bar whose maximum
/// meets its minimum is inactive, as the classic's definition made it,
/// and active again when the range opens. A pop-up menu keeps its
/// menu's resource ID in its minimum and its title's width in its
/// maximum, so a maximum below a minimum is allowed: the classic's own
/// fields were the program's to read.
public var maximum: Int32 = 0

/// GetControlReference, SetControlReference: whatever the program
/// keeps with the control. A pop-up menu keeps its menu's resource ID.
public var reference: Int32 = 0

/// GetControlTitle, SetControlTitle.
public var title: PascalString

/// ShowControl and HideControl.
public internal(set) var isVisible = true

/// EnableControl and DisableControl. A disabled control draws dimmed
/// and is hit nowhere.
public internal(set) var isEnabled = true

/// ActivateControl and DeactivateControl: an active control is the
/// one its window's user can work. An inactive one draws as the
/// classic's inactive controls did, but is hit as ever.
public internal(set) var isActive = true

/// HiliteControl: the part drawn pressed, as TrackControl pressed a
/// button while the mouse was down in it; none while it isn't.
public internal(set) var hilitedPart: ControlPart = .none

/// SetControlCommandID; 0 is none.
public var commandID: GraphiteBase.FourCharCode = GraphiteBase.FourCharCode(rawValue: 0)

/// SetControlID: the signature and the ID the Control Manager finds a
/// control by (GetControlByID).
public var identifier: (signature: GraphiteBase.FourCharCode, id: Int32)?

/// SetControlAction, GetControlAction: the procedure the classic's
/// TrackControl called over and over as the user held the control —
/// the part and the reference constant its work took. The host calls
/// it as it tracks.
public var action: (@Sendable (_ part: ControlPart, _ reference: Int32) -> Void)?

/// The control's part a point of its owner is on — FindControl's
/// answer for this control alone — or none.
public func hitTest(_ point: Point) -> ControlPart {
guard let kind else { return .none }
return definition.hitTest(point, bounds: bounds, kind: kind, value: value,
minimum: minimum, maximum: maximum, isEnabled: isEnabled)
}

public init(kind: ControlKind, bounds: Rect, title: PascalString = "",
definition: (any ControlDefinition)? = nil) {
self.kind = kind
self.definition = definition ?? StandardControlDefinition()
self.bounds = bounds
self.title = title
// A button's maximum is 1 and a checkbox's or radio button's 1
// too, as the classic's resources kept them: on.
self.maximum = kind == .scrollBar || kind == .popUpMenu ? 0 : 1
}

/// A control of the host's own definition: no standard kind, whatever
/// shape the definition gives it.
public init(bounds: Rect, definition: any ControlDefinition) {
self.kind = nil
self.definition = definition
self.bounds = bounds
title = PascalString()
maximum = 0
}

// MARK: Value

/// GetControlValue's kind: what the control keeps.
public var valueKind: ControlValueKind {
kind.map { definition.valueKind(of: $0) } ?? .none
}

/// SetControlValue, as the classic pinned it: a value below the
/// minimum or above the maximum stops at its edge. Kept for the
/// layer that exports Carbon, which sets the property directly.
public func setValue(_ newValue: Int32) {
value = min(max(newValue, minimum), max(minimum, maximum))
}

/// The value a scroll bar's thumb is dragged to, at a point of the
/// bar: the place in its range the point names, the thumb centered
/// under the point as it is dragged. Not for a control without a
/// thumb.
public func value(at point: Point) -> Int32? {
guard kind == .scrollBar, maximum > minimum else { return nil }
let vertical = bounds.height >= bounds.width
let travel = (vertical ? bounds.height : bounds.width) - 2 * 16
guard travel > 0 else { return value }
let thumbLength = 16
let offset = vertical ? Int(point.v) - Int(bounds.top) - 16
: Int(point.h) - Int(bounds.left) - 16
let centered = min(max(offset - thumbLength / 2, 0), Int(travel - thumbLength))
let place = Int32(Double(centered) * Double(maximum - minimum) / Double(travel - thumbLength))
return min(max(minimum + place, minimum), maximum)
}

/// A checkbox's or radio button's on-or-off answer.
public var isOn: Bool {
value != 0
}

/// Whether the control can be worked: visible, enabled and active.
public var isLive: Bool {
isVisible && isEnabled && isActive
}

// MARK: Changes

/// MoveControl: the control's top left moves to (h, v), its size kept.
public func move(h: Int16, v: Int16) {
bounds.offset(dh: h &- bounds.left, dv: v &- bounds.top)
}

/// SizeControl: the control takes a new size, its top left kept.
public func size(width: Int16, height: Int16) {
bounds = Rect(top: bounds.top, left: bounds.left,
bottom: bounds.top + height, right: bounds.left + width)
}

/// ShowControl.
public func show() { isVisible = true }

/// HideControl.
public func hide() { isVisible = false }

/// EnableControl.
public func enable() { isEnabled = true }

/// DisableControl.
public func disable() { isEnabled = false }

/// ActivateControl.
public func activate() { isActive = true }

/// DeactivateControl.
public func deactivate() { isActive = false }

/// HiliteControl: the part drawn pressed.
public func hilite(_ part: ControlPart) {
hilitedPart = part
}
}
Loading
Loading