You are on page 1of 1

CHAPTER 1: Layout Guides and grids 39

Sample code

The following example demonstrates how to create a vertical ruler guide item on the first page of the
active spread. For code simplicity, error checking is omitted.
InterfacePtr<ILayoutControlData>
layoutData(Utils<ILayoutUtils>()->QueryFrontLayoutData());
// Get the active document layer
InterfacePtr<IDocumentLayer> docLayer(layoutData->
QueryActiveDocLayer());

InterfacePtr<ISpread> spread(layoutData->GetSpread(), IID_ISPREAD);


IDataBase* db = ::GetDataBase(spread);

// Get the guide spread layer for the active spread.


InterfacePtr<ISpreadLayer> spreadLayer(spread->QueryLayer(docLayer, nil, kTrue));

// The parent for the new guide is the guide spread layer.
UID parent = ::GetUID(spreadLayer);
UIDRef parentUIDRef(db, parent);

// Get the first page UID. ownerUID is a page for short guides.
UID ownerUID = spread->GetNthPageUID(0);

// Note: The parent for the guide we are to create is the spread. Each
// page owns its guides. We need to convert the guide coordinates
// to its parent space - spread space.

// Get the bounding box of the page in spread space.


InterfacePtr<IGeometry> geometry(db, ownerUID, IID_IGEOMETRY);
PBPMRect bBox = geometry->
GetStrokeBoundingBox(::InnerToParentMatrix(geometry));

InterfacePtr<ICommand> newGuideCmd(CmdUtils::CreateCommand(kNewGuideCmdBoss));

InterfacePtr<INewGuideCmdData> newGuideCmdData(newGuideCmd, IID_INEWGUIDECMDDATA);

// The distance the guide is located at.


PMReal distance = bBox.Left() + bBox.GetHCenter();

// Get the default guide preference


InterfacePtr<IGuidePrefs>
iGuideDefault((IGuidePrefs*)::QueryPreferences(IID_IGUIDEPREFERENCES,
kGetFrontmostPrefs));

// Get the guide threshold and the color index


PMReal guideThreshold = iGuideDefault->GetGuidesThreshold();
int32 guideColorIndex = iGuideDefault->GetGuidesColorIndex();

newGuideCmdData->Set(parentUIDRef, kFalse, distance, ownerUID, kTrue, guideThreshold,


guideColorIndex);

if (CmdUtils::ProcessCommand(newGuideCmd) != kSuccess)
// Report process command failure.

Related APIs

 IGuideData

You might also like