Repository navigation
tkChip
Home › All Categories › tkControls › tkChip
Draws a chip: a compact pill holding a label, with an optional leading avatar, glyph or round image, a check mark when selected, a count badge and a remove button.
tkChip is the tkControls chip, in three kinds: an action chip that is
clicked, a toggle chip that is selected and deselected (a filter), and an input chip that stands for
an entity (a person, a tag) and can be removed. One layout serves the widget and a widgetless
painter, and tkChipGroup draws each of its items with that painter.
Every setter is silent. A click on an action or input chip fires Click; a click on a toggle chip
flips Selected and fires SelectedChanged. The remove button, or Delete / Backspace, fires
RemoveRequested, and the host removes the chip. The Form Designer's Chip binds Selected
as a boolean.
#import "vendor/tkControls/tkChip"
using tkChip
using tkHostThe module brings in tkHost (and through it tkPaint, SDL3, SDL_ttf and
SDL_image), tkTheme, tkTooltip, tkImage, tkBadge and
tkAvatar. Add using tkBadge for the TKBADGE_* status constants, and using tkPaint
for a paint callback or tkChip_PaintInto.
host is a TK_HOST ptr from tkHost_Create, set up as in the tkButton example.
sub Filter_Changed( byval hChip as TK_WIDGET ptr, byval isSelected as boolean )
print tkChip_GetText( hChip ) & iif( isSelected, " on", " off" )
end sub
sub Person_Remove( byval hChip as TK_WIDGET ptr )
tkWidget_Destroy( hChip ) ' the host removes the chip
end sub
dim as TK_WIDGET ptr flt = tkChip_Create( tkHost_Root( host ), 100 )
tkChip_SetKind( flt, TKCHIP_TOGGLE )
tkChip_SetText( flt, "Unread" )
tkChip_SetCount( flt, 12 )
tkChip_SetReserveCheck( flt, true ) ' the width stays put as it toggles
tkChip_SetSelected( flt, true ) ' silent: no callback
tkChip_SetSelectedChangedCallback( flt, @Filter_Changed )
dim as TK_WIDGET ptr who = tkChip_Create( tkHost_Root( host ), 101 )
tkChip_SetKind( who, TKCHIP_INPUT ) ' INPUT: removable
tkChip_SetText( who, "Ada Lovelace" )
tkChip_SetLeadingAvatar( who, "" ) ' initials from the label
tkChip_SetRemoveRequestedCallback( who, @Person_Remove )
dim as long w, h
tkChip_GetIdealSize( flt, w, h ) ' valid before the control is sized
tkWidget_SetPos( flt, tkHost_Scale( host, 20 ), tkHost_Scale( host, 20 ), w, h )
tkChip_GetIdealSize( who, w, h )
tkWidget_SetPos( who, tkHost_Scale( host, 20 ), tkHost_Scale( host, 60 ), w, h )SetKind takes one of three; TKCHIP_ACTION is the default.
| Kind | A click on the body |
|---|---|
TKCHIP_ACTION |
fires Click
|
TKCHIP_TOGGLE |
flips Selected and fires SelectedChanged
|
TKCHIP_INPUT |
fires Click; with SetSelectable( true ) it flips Selected instead, as a toggle does |
-
SetKind( TKCHIP_INPUT )makes the chip removable. A laterSetRemovable( false )turns that off; a laterSetKindto another kind leaves it on.SetRemovable( true )gives any kind a remove button. -
SetSelectedis silent and draws any kind selected. Only a toggle chip, or a selectable input chip, changes it on a click.
[pad][leading][gap][label ...][gap][count][gap][remove][pad]
-
Height:
TKCHIP_SMALL24,TKCHIP_MEDIUM32 (the default), in design units (SetSize). The width follows from the parts. The pad is 12 at an end with nothing beside it and 4 beside a leading part, a check or the remove button; the gap is 4. -
Leading part, a square the chip's height less 8:
-
SetLeadingAvatar( h, name, img )draws a tkAvatar: initials fromname("" uses the label), orimgas its picture (borrowed). The chip opens its own bold font for the initials. -
SetLeadingGlyphdraws a glyph. It needs a glyph font fromSetGlyphFontorApplyFonts; without one there is no leading part. -
SetLeadingImageFileandSetLeadingImagePixelsdraw an image the chip owns, clipped round. A load that fails leaves no leading part. -
ClearLeadingremoves it.
-
-
Check mark: drawn when the chip is selected and
ShowCheckis on (the default). It replaces the leading part when there is one; otherwise it is inserted before the label and the chip widens.SetReserveCheck( true )keeps its room while unselected, so a row of chips does not jump. -
Label: one line. Beyond
SetMaxLabelWidth(design units;TKCHIP_DEFAULT_MAXLABEL, 200, by default; 0 = no limit) it is ellipsised, and the tooltip shows it whole. -
Count:
SetCount( n )withn > 0draws a small tkBadge count after the label; 0 removes it. -
Remove button: a circle half the height plus 2 across, with an × drawn in two lines and a hot
background under the pointer. Its hit target is at least
TKCHIP_MIN_TARGET(24, scaled) across, centred on the button and kept inside the chip. A disabled chip hides it.
-
SetAppearance( TKCHIP_FILLED | TKCHIP_OUTLINED ). A filled Neutral chip has no border until it is selected. An outlined chip has a border and, while unselected, is filled with the back colour. -
SetShape( TKCHIP_PILL | TKCHIP_ROUNDED ): round ends, or rounded corners. -
SetStatustakes the badge's six:TKBADGE_NEUTRAL(the default),TKBADGE_ACCENT,TKBADGE_INFO,TKBADGE_SUCCESS,TKBADGE_WARNING,TKBADGE_ERROR. A chip with a status other than Neutral is filled with a 20 % tint of the status colour (30 % when hot or pressed), with the status colour as its border and text; selected, it is filled with the status colour itself. - Disabled, the chip takes the disabled colours, a leading avatar is drawn inactive and a leading image at half opacity.
- A press focuses the chip; on the body or the remove button it also captures the mouse. The release acts only on the part the press began on: a press that slides off is cancelled.
- The chip is a tab stop, with a focus ring round the pill.
-
Space, Enter and keypad Enter act as a click on the body. Delete and Backspace
fire
RemoveRequestedon a removable chip and are not claimed on another. Held keys do not repeat. - The wheel, other keys and any key with Ctrl, Alt or the GUI key held are not claimed. A disabled chip takes no input.
| Callback | When |
|---|---|
SetClickCallback |
a click (or Space / Enter) on an action chip, or on an input chip that is not selectable |
SetSelectedChangedCallback |
the user flipped Selected on a toggle chip or a selectable input chip |
SetRemoveRequestedCallback |
the remove button was clicked, or Delete / Backspace pressed; the host removes the chip |
SetPaintCallback |
replaces the drawing, focus ring included; tkChip_PaintDefault( p ) draws the stock chip without the ring |
SetMessageCallback |
the input stream, with the part under the pointer (TKCHIP_HIT_*); TRUE claims a press, a release (no click) or a key |
The state is updated before a callback runs: inside SelectedChanged, GetSelected already answers
the new value. Callbacks are fired last, so a callback may destroy the chip; RemoveRequested is
where a host usually does.
-
GetIdealSizeis valid before the control is sized: the chip plus the focus ring's band (TKCHIP_FOCUSGAPscaled, plusTKCHIP_FOCUSTHICK) on every side. -
GetParts( h, pt )fills aTKCHIP_PARTSin control-local pixels:rcChip,rcLeading,rcCheck,rcLabel,rcCount,rcRemove(the visible circle),rcRemoveTarget(the hit target) andbLabelCut. A rect the chip does not have is empty. The chip sits at the left after the ring band, centred vertically. -
HitTest( h, x, y )answersTKCHIP_HIT_NONE,TKCHIP_HIT_BODYorTKCHIP_HIT_REMOVE. -
GetTipTextanswers what the tooltip shows:SetTooltipText's text, else the full label when it is cut, else "".
A TKCHIP_SPEC holds everything that decides a chip's look and layout: the text, the leading part
(nLead, wszAvatarName, wszGlyph, pImage), bSelected, bShowCheck, bReserveCheck,
bRemovable, nCount, nStatus, nAppearance, nShape, nSize, nMaxLabel, bEnabled, the
live states bHot, bPressed and bRemoveHot, the fonts (fText, fGlyph, fAvatar), fScale
and pColors.
-
tkChip_LayoutSpec( s, x, y, pt )lays the chip out with its top-left at (x, y). -
tkChip_MeasureSpec( s, w, h )answers its size. -
tkChip_PaintInto( b, rc, s )draws it atrc.left, centred vertically inrcand clipped to it.pColors = 0uses the app theme's colours.
There is no widget to supply defaults: set fText (no font, no label), fScale, fGlyph for a
glyph and fAvatar for an avatar's initials. nMaxLabel is in pixels: -1 (the default) is
TKCHIP_DEFAULT_MAXLABEL scaled, 0 is no limit. A list or a grid cell can draw chips this way
without a widget for each.
TKCHIP_COLORS holds the Neutral status's colours: the back and the focus ring; the fill normal,
hot, pressed, selected, selected and hot, and disabled; the border normal, hot, selected and
disabled; the text normal, selected and disabled; the check; the remove button's × normal and hot,
and its hot background. Another status takes its colours from tkBadge's at paint.
tkChip_ThemeColors( t ) maps the struct from the theme's roles; ApplyTheme resolves it under
the style name "Chip". SetColors pins the struct (a theme change leaves it) until UnpinColors.
tkChipGroup uses the same struct.
The toolbox's Chip (Common) has the properties above, in the order the form sets them: Kind first, because Input makes the chip removable and a later Removable can turn that off; then Text, Selected, Removable, Selectable, LeadingGlyph, LeadingImage, AvatarName, ShowCheck, ReserveCheck, Count, Status, Appearance, Shape, Size, MaxLabelWidth, Font, GlyphFont (the form's glyph font, for LeadingGlyph) and TooltipText. Each of LeadingGlyph, LeadingImage and AvatarName sets the leading part, so of those given, the last wins. Its events are Click (the default), SelectedChanged, RemoveRequested, Paint and Message.
Bind moves Selected into and out of a field of the form's DataType, as a boolean. See
Form Designer, Data binding.
TKCHIP_ACTION, TKCHIP_TOGGLE, TKCHIP_INPUT · TKCHIP_LEAD_NONE, TKCHIP_LEAD_AVATAR,
TKCHIP_LEAD_GLYPH, TKCHIP_LEAD_IMAGE · TKCHIP_FILLED, TKCHIP_OUTLINED · TKCHIP_PILL,
TKCHIP_ROUNDED · TKCHIP_SMALL, TKCHIP_MEDIUM · TKCHIP_HIT_NONE, TKCHIP_HIT_BODY,
TKCHIP_HIT_REMOVE · TKCHIP_DEFAULT_MAXLABEL (200), TKCHIP_MIN_TARGET (24),
TKCHIP_FOCUSGAP, TKCHIP_FOCUSTHICK.
TKCHIP_COLORS, TKCHIP_SPEC (a chip's look and layout, with or without a widget),
TKCHIP_PARTS (the laid-out rects), TKCHIP_PAINTINFO (the chip, the paint target, the client
rect, the parts, the focus), TKCHIP_MESSAGEINFO, and the callback types CHP_ClickCallbackSub,
CHP_SelectedChangedCallbackSub, CHP_RemoveRequestedCallbackSub, CHP_PaintCallbackSub,
CHP_MessageCallbackFunc.
sub tkChip_LayoutSpec( byref s as TKCHIP_SPEC, byval x as long, byval y as long, byref pt as TKCHIP_PARTS )
sub tkChip_MeasureSpec( byref s as TKCHIP_SPEC, byref nWidth as long, byref nHeight as long )
sub tkChip_PaintInto( byval b as TK_PAINT ptr, byref rc as TKRECT, byref s as TKCHIP_SPEC )
function tkChip_Create( byval hParent as TK_WIDGET ptr, byval ctrlId as long ) as TK_WIDGET ptr
function tkChip_GetID( byval hChip as TK_WIDGET ptr ) as long
sub tkChip_SetEnabled( byval hChip as TK_WIDGET ptr, byval isEnabled as boolean )
function tkChip_GetFocused( byval hChip as TK_WIDGET ptr ) as boolean
function tkChip_GetText( byval hChip as TK_WIDGET ptr ) as ustring
sub tkChip_SetText( byval hChip as TK_WIDGET ptr, byval Text as ustring )
function tkChip_GetKind( byval hChip as TK_WIDGET ptr ) as long
sub tkChip_SetKind( byval hChip as TK_WIDGET ptr, byval nKind as long )
function tkChip_GetSelected( byval hChip as TK_WIDGET ptr ) as boolean
sub tkChip_SetSelected( byval hChip as TK_WIDGET ptr, byval bOn as boolean )
sub tkChip_SetSelectable( byval hChip as TK_WIDGET ptr, byval bOn as boolean )
function tkChip_GetRemovable( byval hChip as TK_WIDGET ptr ) as boolean
sub tkChip_SetRemovable( byval hChip as TK_WIDGET ptr, byval bOn as boolean )
sub tkChip_SetLeadingAvatar( byval hChip as TK_WIDGET ptr, byval Name_ as ustring, byval pImage as tkImageFile ptr = 0 )
sub tkChip_SetLeadingGlyph( byval hChip as TK_WIDGET ptr, byval Glyph as ustring )
sub tkChip_SetLeadingImageFile( byval hChip as TK_WIDGET ptr, byval Path as ustring )
sub tkChip_SetLeadingImagePixels( byval hChip as TK_WIDGET ptr, byval w as long, byval h as long, byval argb as ulong ptr )
sub tkChip_ClearLeading( byval hChip as TK_WIDGET ptr )
sub tkChip_SetShowCheck( byval hChip as TK_WIDGET ptr, byval bOn as boolean )
sub tkChip_SetReserveCheck( byval hChip as TK_WIDGET ptr, byval bOn as boolean )
sub tkChip_SetCount( byval hChip as TK_WIDGET ptr, byval nCount as long )
sub tkChip_SetStatus( byval hChip as TK_WIDGET ptr, byval nStatus as long )
sub tkChip_SetAppearance( byval hChip as TK_WIDGET ptr, byval nAppearance as long )
sub tkChip_SetShape( byval hChip as TK_WIDGET ptr, byval nShape as long )
sub tkChip_SetSize( byval hChip as TK_WIDGET ptr, byval nSize as long )
sub tkChip_SetMaxLabelWidth( byval hChip as TK_WIDGET ptr, byval nDu as long )
sub tkChip_SetFont( byval hChip as TK_WIDGET ptr, byval f as TTF_Font ptr )
sub tkChip_SetGlyphFont( byval hChip as TK_WIDGET ptr, byval f as TTF_Font ptr )
sub tkChip_ApplyFonts( byval hChip as TK_WIDGET ptr, byval fText as TTF_Font ptr, byval fGlyph as TTF_Font ptr )
sub tkChip_GetIdealSize( byval hChip as TK_WIDGET ptr, byref nWidth as long, byref nHeight as long )
sub tkChip_GetParts( byval hChip as TK_WIDGET ptr, byref pt as TKCHIP_PARTS )
function tkChip_HitTest( byval hChip as TK_WIDGET ptr, byval x as long, byval y as long ) as long
sub tkChip_SetTooltipText( byval hChip as TK_WIDGET ptr, byval Text as ustring )
function tkChip_GetTipText( byval hChip as TK_WIDGET ptr ) as ustring
sub tkChip_GetColors( byval hChip as TK_WIDGET ptr, byval pColors as TKCHIP_COLORS ptr )
sub tkChip_SetColors( byval hChip as TK_WIDGET ptr, byval pColors as TKCHIP_COLORS ptr )
function tkChip_ThemeColors( byref t as TKTHEME_COLORS ) as TKCHIP_COLORS
sub tkChip_ApplyTheme( byval hChip as TK_WIDGET ptr, byref t as TKTHEME_COLORS )
sub tkChip_UnpinColors( byval hChip as TK_WIDGET ptr )
sub tkChip_PaintDefault( byval pi as TKCHIP_PAINTINFO ptr )
sub tkChip_SetClickCallback( byval hChip as TK_WIDGET ptr, byval usersub as CHP_ClickCallbackSub )
sub tkChip_SetSelectedChangedCallback( byval hChip as TK_WIDGET ptr, byval usersub as CHP_SelectedChangedCallbackSub )
sub tkChip_SetRemoveRequestedCallback( byval hChip as TK_WIDGET ptr, byval usersub as CHP_RemoveRequestedCallbackSub )
sub tkChip_SetPaintCallback( byval hChip as TK_WIDGET ptr, byval usersub as CHP_PaintCallbackSub )
sub tkChip_SetMessageCallback( byval hChip as TK_WIDGET ptr, byval userfunc as CHP_MessageCallbackFunc )- tkControls
- tkChipGroup, many chips as items of one widget
- tkBadge, tkAvatar, the count and the leading avatar
- tkHost, tkTheme, tkTooltip, tkImage
- Form Designer, data binding
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Home
- What Is tiko-lang
- Quick Start
- Installing on macOS
- Debugging
- Feature Index
- Compatibility
- FAQ
- AI Cheatsheet
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Match
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Array Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Time Zones
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- tiko test
- tiko doc
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing