Skip to content

Commit

Permalink
UI: add containing View for each Label
Browse files Browse the repository at this point in the history
`WC_STATIC` control doesn't receive events like `WM_CONTEXTMENU`. To be able to handle those events, let's create containing views for `WC_STATIC` instances.

Co-authored-by: Saleem Abdulrasool <[email protected]>
  • Loading branch information
egorzhdan and compnerd committed Apr 6, 2021
1 parent 3078755 commit 90630fd
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
62 changes: 56 additions & 6 deletions Sources/SwiftWin32/Views and Controls/Label.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,71 @@ private let SwiftLabelProc: SUBCLASSPROC = { (hWnd, uMsg, wParam, lParam, uIdSub
}

public class Label: Control {
private static let `class`: WindowClass = WindowClass(named: WC_STATIC)
private static let style: WindowStyle = (base: WS_TABSTOP | DWORD(SS_NOTIFY), extended: 0)
private static let `class`: WindowClass =
WindowClass(hInst: GetModuleHandleW(nil), name: "Swift.Label")
private static let style: WindowStyle = (base: WS_TABSTOP, extended: 0)

private var staticHWnd: HWND!

public var text: String? {
get {
let szLength: Int32 = GetWindowTextLengthW(self.staticHWnd)
let buffer: [WCHAR] = Array<WCHAR>(unsafeUninitializedCapacity: Int(szLength) + 1) {
$1 = Int(GetWindowTextW(self.staticHWnd, $0.baseAddress!, CInt($0.count)))
}
return String(decodingCString: buffer, as: UTF16.self)
}
set(value) { _ = SetWindowTextW(self.staticHWnd, value?.wide) }
}

public override var font: Font! {
get { return super.font }
set(value) { super.font = value }
didSet {
SendMessageW(self.staticHWnd, UINT(WM_SETFONT),
unsafeBitCast(self.font?.hFont.value, to: WPARAM.self),
LPARAM(1))
}
}

@_Win32WindowText
public var text: String?
public override var frame: Rect {
didSet {
// Scale window for DPI
var client: Rect = self.frame
ScaleClient(rect: &client, for: GetDpiForWindow(self.staticHWnd),
(base: 0, extended: 0))

// Resize and Position the Window
_ = SetWindowPos(self.staticHWnd, nil,
CInt(client.origin.x), CInt(client.origin.y),
CInt(client.size.width), CInt(client.size.height),
UINT(SWP_NOZORDER | SWP_FRAMECHANGED))
}
}

public init(frame: Rect) {
super.init(frame: frame, class: Label.class, style: Label.style)
_ = SetWindowSubclass(hWnd, SwiftLabelProc, UINT_PTR(1),
unsafeBitCast(self as AnyObject, to: DWORD_PTR.self))

var staticFrame = frame
ClientToWindow(size: &staticFrame.size, for: (base: 0, extended: 0))
self.staticHWnd = CreateWindowExW(0, WC_STATIC.wide, nil, 0,
0, 0,
Int32(staticFrame.size.width),
Int32(staticFrame.size.height),
nil, nil, GetModuleHandleW(nil), nil)!

var staticStyle: LONG = GetWindowLongW(self.staticHWnd, WinSDK.GWL_STYLE)
staticStyle |= WS_CHILD
staticStyle &= ~LONG(bitPattern: WS_POPUP | WS_CAPTION)
_ = SetWindowLongW(self.staticHWnd, WinSDK.GWL_STYLE, staticStyle)

_ = SetParent(self.staticHWnd, self.hWnd)

self.font = Font.systemFont(ofSize: Font.systemFontSize)
}

deinit {
DestroyWindow(self.staticHWnd)
}

// ContentSizeCategoryAdjusting
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftWin32/Views and Controls/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private let SwiftViewProc: SUBCLASSPROC = { (hWnd, uMsg, wParam, lParam, uIdSubc

internal typealias WindowStyle = (base: DWORD, extended: DWORD)

private func ClientToWindow(size: inout Size, for style: WindowStyle) {
internal func ClientToWindow(size: inout Size, for style: WindowStyle) {
var r: RECT =
RECT(left: 0, top: 0, right: LONG(size.width), bottom: LONG(size.height))
if !AdjustWindowRect(&r, style.base, false) {
Expand All @@ -65,7 +65,7 @@ private func ClientToWindow(size: inout Size, for style: WindowStyle) {
size = Size(width: Double(r.right - r.left), height: Double(r.bottom - r.top))
}

private func ScaleClient(rect: inout Rect, for dpi: UINT, _ style: WindowStyle) {
internal func ScaleClient(rect: inout Rect, for dpi: UINT, _ style: WindowStyle) {
let scale: Double = Double(dpi) / Double(USER_DEFAULT_SCREEN_DPI)

var r: RECT =
Expand Down

0 comments on commit 90630fd

Please sign in to comment.