Update Lua action-icon doc
[ardour.git] / scripts / split_all_markers.lua
1 ardour { ["type"] = "EditorAction", name = "Marker Split",
2         license     = "MIT",
3         author      = "Ardour Team",
4         description = [[Split regions on selected tracks at all locations markers]]
5 }
6
7 function factory (params) return function ()
8
9         local loc = Session:locations () -- all marker locations
10
11         -- http://manual.ardour.org/lua-scripting/class_reference/#ArdourUI:Selection
12         local sel = Editor:get_selection ()
13
14         -- prepare undo operation
15         -- see http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:Session
16         Session:begin_reversible_command ("Auto Region Split")
17         local add_undo = false -- keep track if something has changed
18
19         -- Track/Bus Selection -- iterate over all Editor-GUI selected tracks
20         -- http://manual.ardour.org/lua-scripting/class_reference/#ArdourUI:TrackSelection
21         for r in sel.tracks:routelist ():iter () do
22                 -- each of the items 'r' is-a
23                 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:Route
24
25                 local track = r:to_track () -- see if it's a track
26                 if track:isnil () then
27                         -- if not, skip it
28                         goto continue
29                 end
30
31                 -- get track's playlist
32                 -- http://manual.ardour.org/lua-scripting/class_reference/#ARDOUR:Playlist
33                 local playlist = track:playlist ()
34
35                 -- clear existing changes, prepare "diff" of state for undo
36                 playlist:to_stateful ():clear_changes ()
37
38                 -- iterate over all location markers
39                 for l in loc:list ():iter () do
40                         if l:is_mark() then
41                                 -- get all regions on the given track's playlist (may be stacked)
42                                 for reg in playlist:regions_at (l:start ()):iter () do
43                                         playlist:split_region (reg, ARDOUR.MusicFrame (l:start(), 0))
44                                         -- the above operation will invalidate the playlist's region list:
45                                         -- split creates 2 new regions.
46                                         --
47                                         -- Hence this script does it the way it does: the inner-most loop
48                                         -- is over playlist-regions.
49                                 end
50                         end
51                 end
52
53                 -- collect undo data
54                 if not Session:add_stateful_diff_command (playlist:to_statefuldestructible ()):empty () then
55                         -- is something has changed, we need to save it at the end.
56                         add_undo = true
57                 end
58
59                 ::continue::
60         end
61
62         -- all done, commit the combined Undo Operation
63         if add_undo then
64                 -- the 'nil' Command here mean to use the collected diffs added above
65                 Session:commit_reversible_command (nil)
66         else
67                 Session:abort_reversible_command ()
68         end
69
70 end end
71
72
73 -- render an icon for the toolbar action-button
74 -- this is genrally square width == height.
75 -- The background is set according to the theme (leave transparent when drawing).
76 -- A foreground color is passed as parameter 'fg'
77 --
78 -- ctx is-a http://manual.ardour.org/lua-scripting/class_reference/#Cairo:Context
79 -- 2D vector graphics http://cairographics.org/
80 function icon (params) return function (ctx, width, height, fg)
81         local mh = height - 3.5;
82         local m3 = width / 3;
83         local m6 = width / 6;
84
85         -- compare to gtk2_ardour/marker.cc "Marker"
86         ctx:set_source_rgba (.8, .8, .2, 1.0)
87         ctx:move_to (width / 2 - m6, 2)
88         ctx:rel_line_to (m3, 0)
89         ctx:rel_line_to (0, mh * 0.4)
90         ctx:rel_line_to (-m6, mh * 0.6)
91         ctx:rel_line_to (-m6, -mh * 0.6)
92         ctx:close_path ()
93         ctx:fill ()
94
95         -- draw an arrow  <--|--> on top, using the foreground color
96         ctx:set_source_rgba (LuaCairo.color_to_rgba (fg))
97         ctx:set_line_width (1)
98
99         ctx:move_to (width * .5, height * .4)
100         ctx:line_to (width * .5, height * .6)
101         ctx:stroke ()
102
103         ctx:move_to (2, height * .5)
104         ctx:line_to (width - 2, height * .5)
105         ctx:stroke ()
106
107         ctx:move_to (width - 2, height * .5)
108         ctx:rel_line_to (-m6, -m6)
109         ctx:rel_line_to (0, m3)
110         ctx:close_path ()
111         ctx:fill ()
112
113         ctx:move_to (2, height * .5)
114         ctx:rel_line_to (m6, -m6)
115         ctx:rel_line_to (0, m3)
116         ctx:close_path ()
117         ctx:fill ()
118 end end