Overview
The Canvas class represents a Pure Data patch’s visual canvas. It manages object creation, connections, selection, viewport, and rendering. Each canvas corresponds to one Pure Data patch (pd::Patch) and provides the interactive editing environment.
Location: Source/Canvas.h and Source/Canvas.cpp
Class Hierarchy
class Canvas final : public Component
, public Value::Listener
, public SettingsFileListener
, public LassoSource<WeakReference<Component>>
, public ModifierKeyListener
, public pd::MessageListener
, public AsyncUpdater
, public NVGComponent
, public ChangeListener
Constructor
Canvas(PluginEditor* parent, pd::Patch::Ptr patch, Component* parentGraph = nullptr);
The plugin editor that owns this canvas.
The Pure Data patch this canvas represents.
parentGraph
Component*
default:"nullptr"
Optional parent graph component (for graph-on-parent subpatches).
Key Properties
Reference to the plugin processor/Pure Data instance.
The underlying Pure Data patch.
viewport
std::unique_ptr<CanvasViewport>
Viewport for scrolling and zooming the canvas.
All objects on this canvas.
connections
PooledPtrArray<Connection>
All patch connections on this canvas.
selectedComponents
SelectedItemSet<WeakReference<Component>>
Currently selected objects and connections.
Whether the canvas is in locked (run) mode vs edit mode.
Whether the canvas is in presentation mode (showing only GUI objects).
Current zoom level (1.0 = 100%).
Object Management
void synchronise();
void performSynchronise();
void synchroniseAllCanvases();
Synchronize the visual canvas with the underlying Pure Data patch structure. Called after patch modifications to update the GUI.
Update all drawable components on the canvas.
Selection and Editing
void setSelected(Component* component, bool shouldNowBeSelected,
bool updateCommandStatus = true, bool broadcastChange = true);
Select or deselect a component on the canvas.
void deselectAll(bool broadcastChange = true);
Clear all selections.
template<typename T>
SmallArray<T*> getSelectionOfType();
Get all selected components of a specific type.Example:auto selectedObjects = canvas->getSelectionOfType<Object>();
for (auto* obj : selectedObjects) {
// Process each selected object
}
Clipboard Operations
Copy selected objects to clipboard.
Paste objects from clipboard.
void duplicateSelection();
Duplicate selected objects in place.
void removeSelection();
void removeSelectedConnections();
Delete selected objects or connections.
void dragAndDropPaste(String const& patchString, Point<int> mousePos,
int patchWidth, int patchHeight, String const& name = String());
Paste objects at a specific position (used for drag-and-drop).
Advanced Editing
void encapsulateSelection();
Wrap selected objects in a subpatch.
Auto-arrange selected objects neatly.
void alignObjects(Align alignment);
Align selected objects (left, right, top, bottom, center).
Auto-connect selected objects where possible.
Undo/Redo
void undo();
void redo();
Undo or redo the last action on this canvas.
Viewport and Navigation
Zoom to fit all objects in the viewport.
Reset viewport to origin (0, 0).
void saveViewportState();
void restoreViewportState();
Save and restore viewport position and zoom.
Rendering
void performRender(NVGcontext* nvg, Rectangle<int> invalidRegion);
void renderAllObjects(NVGcontext* nvg, Rectangle<int> area);
void renderAllConnections(NVGcontext* nvg, Rectangle<int> area);
Render the canvas using NanoVG. Called automatically by the rendering system.
Overlays and Display Options
bool shouldShowObjectActivity() const;
bool shouldShowIndex() const;
bool shouldShowConnectionDirection() const;
bool shouldShowConnectionActivity() const;
Query current overlay display settings.
File Operations
void save(std::function<void()> const& nestedCallback = [] { });
void saveAs(std::function<void()> const& nestedCallback = [] { });
Save the patch to disk. Optionally provide a callback to execute after save completes.
Message Handling
void receiveMessage(t_symbol* symbol, SmallArray<pd::Atom> const& atoms) override;
Receive messages from Pure Data (implements pd::MessageListener).
Suggestions and Autocomplete
void showSuggestions(Object* object, TextEditor* textEditor);
void hideSuggestions();
Show or hide object name suggestions while typing.
Inspector Integration
ObjectParameters& getInspectorParameters();
Get parameters for the inspector panel.
Constants
Maximum canvas size: 128,000 pixels
Usage Example
// Create a canvas for a patch
auto patch = pd->openPatch(File("/path/to/patch.pd"));
auto* canvas = new Canvas(editor, patch);
// Lock/unlock the canvas
canvas->locked.setValue(true); // Lock (run mode)
canvas->locked.setValue(false); // Unlock (edit mode)
// Work with selections
canvas->selectAll();
auto selectedObjs = canvas->getSelectionOfType<Object>();
canvas->alignObjects(Align::Left);
canvas->tidySelection();
// Clipboard operations
canvas->copySelection();
canvas->pasteSelection();
// Viewport control
canvas->zoomToFitAll();
canvas->saveViewportState();
// Undo/redo
canvas->undo();
canvas->redo();
// Save the patch
canvas->save([](){
DBG("Save complete!");
});
See Also
Object - Individual patch objects
Connection - Patch cord connections
PluginEditor - Editor window
pd::Patch - Underlying Pure Data patch
CanvasViewport - Viewport and scrolling