Learn how to create form inputs using theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/devhammed/react-gtk/llms.txt
Use this file to discover all available pages before exploring further.
GtkEntry component, handle user input, and apply text styling with Pango attributes.
Overview
TheGtkEntry component provides text input functionality in GTK applications. It supports:
- Single-line text entry
- Text change events
- Programmatic text manipulation via buffer API
- Rich text styling with Pango attributes
Basic Text Input
Create a simple text entry field:onChanged event fires whenever the text in the entry changes, providing access to the entry instance with the current text value.
Complete Form Input Example
Here’s a full example demonstrating text input with styling and programmatic manipulation:Pango Text Attributes
Style portions of the entry text using Pango attributes. Each attribute defines styling for a character range:Attribute Structure
Each attribute object has these properties:start: Starting character position (0-based)end: Ending character position (exclusive)type: The attribute type (see below)value: The attribute value
Common Attribute Types
Multiple Attributes Example
Apply multiple styling attributes to different text ranges:- Characters 0-5 are red
- Characters 5-11 are bold
- Characters 11-20 are italic
Text Buffer API
Access the entry’s text buffer through refs for programmatic text manipulation:Setting Text
Setting Text
Replace the entire text content:
Inserting Text
Inserting Text
Insert text at a specific position:Parameters:
position: Character position to insert at (0-based)text: The text to insertlength: Number of characters to insert (-1 for all)
Deleting Text
Deleting Text
Delete a range of characters:Parameters:
start: Starting position (0-based)end: Ending position (-1 for end of text)
Handling Text Changes
TheonChanged event provides access to the entry instance whenever text is modified:
The
onChanged event is fired for every text modification, including programmatic changes via the buffer API. Be careful to avoid infinite loops when modifying text inside the event handler.Complete Working Example
Here’s the complete form input example from the React GTK source:app.tsx
Best Practices
Use Refs for Initialization: When you need to set initial text programmatically, use refs in a
useEffect hook to ensure the component is mounted before manipulating the buffer.Attribute Positions: Pango attributes use character positions, not byte positions. Make sure your start and end values correspond to actual character boundaries in the text.
Event Handler Performance: The
onChanged event fires frequently. Avoid expensive operations in the handler. Consider debouncing or throttling for operations like API calls or validation.Next Steps
- Explore basic application structure
- Learn about multiple windows
- Check out the GtkEntry component reference for all available properties
- See GtkLabel documentation for display-only text with Pango attributes
