+-- Basically create symbol taken from "symbols.lua" with two minor
+-- changes. First, the symbol is created only if the call to
+-- check_decorator_object is successful. Second, the prefix "deco/"
+-- is added to the symbols name.
+function create_deco_obj(model, num)
+ local p = model:page()
+ local prim = p:primarySelection()
+ if not prim then model.ui:explain("no selection") return end
+ if not check_decorator_object(model, p[prim]) then
+ model.ui:explain("no selection")
+ return
+ end
+ local str = model:getString("Enter name of new symbol")
+ if not str or str:match("^%s*$") then return end
+ local name = "deco/" .. str:match("^%s*%S+%s*$")
+ local old = model.doc:sheets():find("symbol", name)
+ if old then
+ local r = ipeui.messageBox(mainWindow(model), "question",
+ "Symbol '" .. name .. "' already exists",
+ "Do you want to proceed?",
+ "okcancel")
+ if r <= 0 then return end
+ end
+
+ if num == 2 then -- new stylesheet
+ local sheet = ipe.Sheet()
+ sheet:add("symbol", name, p[prim])
+ local t = { label = methods[num].label,
+ sheet = sheet,
+ }
+ t.redo = function (t, doc)
+ doc:sheets():insert(1, t.sheet:clone())
+ end
+ t.undo = function (t, doc)
+ doc:sheets():remove(1)
+ end
+ model:register(t)
+ else -- top stylesheet
+ local sheet = model.doc:sheets():sheet(1)
+ local t = { label = methods[num].label,
+ original = sheet:clone(),
+ final = sheet:clone(),
+ }
+ t.final:add("symbol", name, p[prim])
+ t.redo = function (t, doc)
+ doc:sheets():remove(1)
+ doc:sheets():insert(1, t.final:clone())
+ end
+ t.undo = function (t, doc)
+ doc:sheets():remove(1)
+ doc:sheets():insert(1, t.original:clone())
+ end
+ model:register(t)
+ end
+end
+