3d57f2f88edf25b6b5248d21edfd217132f9a908
[blender.git] / source / blender / editors / space_sequencer / sequencer_select.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2003-2009, Campbell Barton
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/space_sequencer/sequencer_select.c
27  *  \ingroup spseq
28  */
29
30
31 #include <stdlib.h>
32 #include <math.h>
33 #include <string.h>
34
35 #ifndef WIN32
36 #include <unistd.h>
37 #else
38 #include <io.h>
39 #endif
40 #include <sys/types.h>
41
42 #include "BLI_blenlib.h"
43 #include "BLI_math.h"
44 #include "BLI_utildefines.h"
45
46 #include "DNA_scene_types.h"
47
48 #include "BKE_context.h"
49 #include "BKE_report.h"
50 #include "BKE_sequencer.h"
51
52 #include "WM_api.h"
53 #include "WM_types.h"
54
55 #include "RNA_define.h"
56
57 /* for menu/popup icons etc etc*/
58
59 #include "ED_types.h"
60 #include "ED_screen.h"
61 #include "ED_sequencer.h"
62
63 #include "UI_view2d.h"
64
65 /* own include */
66 #include "sequencer_intern.h"
67 static void *find_nearest_marker(int UNUSED(d1), int UNUSED(d2))
68 {
69         return NULL;
70 }
71         
72 static void select_surrounding_handles(Scene *scene, Sequence *test) /* XXX BRING BACK */
73 {
74         Sequence *neighbor;
75         
76         neighbor = find_neighboring_sequence(scene, test, SEQ_SIDE_LEFT, -1);
77         if (neighbor) {
78                 neighbor->flag |= SELECT;
79                 recurs_sel_seq(neighbor);
80                 neighbor->flag |= SEQ_RIGHTSEL;
81         }
82         neighbor = find_neighboring_sequence(scene, test, SEQ_SIDE_RIGHT, -1);
83         if (neighbor) {
84                 neighbor->flag |= SELECT;
85                 recurs_sel_seq(neighbor);
86                 neighbor->flag |= SEQ_LEFTSEL;
87         }
88         test->flag |= SELECT;
89 }
90
91 /* used for mouse selection and for SEQUENCER_OT_select_active_side() */
92 static void select_active_side(ListBase *seqbase, int sel_side, int channel, int frame)
93 {
94         Sequence *seq;
95
96         for (seq = seqbase->first; seq; seq = seq->next) {
97                 if (channel == seq->machine) {
98                         switch (sel_side) {
99                                 case SEQ_SIDE_LEFT:
100                                         if (frame > (seq->startdisp)) {
101                                                 seq->flag &= ~(SEQ_RIGHTSEL | SEQ_LEFTSEL);
102                                                 seq->flag |= SELECT;
103                                         }
104                                         break;
105                                 case SEQ_SIDE_RIGHT:
106                                         if (frame < (seq->startdisp)) {
107                                                 seq->flag &= ~(SEQ_RIGHTSEL | SEQ_LEFTSEL);
108                                                 seq->flag |= SELECT;
109                                         }
110                                         break;
111                                 case SEQ_SIDE_BOTH:
112                                         seq->flag &= ~(SEQ_RIGHTSEL | SEQ_LEFTSEL);
113                                         break;
114                         }
115                 }
116         }
117 }
118
119 /* used for mouse selection and for SEQUENCER_OT_select_active_side() */
120 static void select_linked_time(ListBase *seqbase, Sequence *seq_link)
121 {
122         Sequence *seq;
123
124         for (seq = seqbase->first; seq; seq = seq->next) {
125                 if (seq_link->machine != seq->machine) {
126                         int left_match = (seq->startdisp == seq_link->startdisp) ? 1 : 0;
127                         int right_match = (seq->enddisp == seq_link->enddisp) ? 1 : 0;
128
129                         if (left_match && right_match) {
130                                 /* a direct match, copy the selection settinhs */
131                                 seq->flag &= ~(SELECT | SEQ_LEFTSEL | SEQ_RIGHTSEL);
132                                 seq->flag |= seq_link->flag & (SELECT | SEQ_LEFTSEL | SEQ_RIGHTSEL);
133
134                                 recurs_sel_seq(seq);
135                         }
136                         else if (seq_link->flag & SELECT && (left_match || right_match)) {
137
138                                 /* clear for reselection */
139                                 seq->flag &= ~(SEQ_LEFTSEL | SEQ_RIGHTSEL);
140
141                                 if (left_match && seq_link->flag & SEQ_LEFTSEL)
142                                         seq->flag |= SELECT | SEQ_LEFTSEL;
143
144                                 if (right_match && seq_link->flag & SEQ_RIGHTSEL)
145                                         seq->flag |= SELECT | SEQ_RIGHTSEL;
146
147                                 recurs_sel_seq(seq);
148                         }
149                 }
150         }
151 }
152
153 #if 0 // BRING BACK
154 void select_surround_from_last(Scene *scene)
155 {
156         Sequence *seq = get_last_seq(scene);
157         
158         if (seq == NULL)
159                 return;
160         
161         select_surrounding_handles(scene, seq);
162 }
163 #endif
164
165 void ED_sequencer_select_sequence_single(Scene * scene, Sequence * seq, int deselect_all)
166 {
167         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
168         
169         if (deselect_all)
170                 ED_sequencer_deselect_all(scene);
171
172         BKE_sequencer_active_set(scene, seq);
173
174         if ((seq->type == SEQ_TYPE_IMAGE) || (seq->type == SEQ_TYPE_MOVIE)) {
175                 if (seq->strip)
176                         BLI_strncpy(ed->act_imagedir, seq->strip->dir, FILE_MAXDIR);
177         }
178         else if (seq->type == SEQ_TYPE_SOUND_RAM) {
179                 if (seq->strip)
180                         BLI_strncpy(ed->act_sounddir, seq->strip->dir, FILE_MAXDIR);
181         }
182         seq->flag |= SELECT;
183         recurs_sel_seq(seq);
184 }
185
186 #if 0
187 static void select_neighbor_from_last(Scene *scene, int lr)
188 {
189         Sequence *seq = BKE_sequencer_active_get(scene);
190         Sequence *neighbor;
191         int change = 0;
192         if (seq) {
193                 neighbor = find_neighboring_sequence(scene, seq, lr, -1);
194                 if (neighbor) {
195                         switch (lr) {
196                                 case SEQ_SIDE_LEFT:
197                                         neighbor->flag |= SELECT;
198                                         recurs_sel_seq(neighbor);
199                                         neighbor->flag |= SEQ_RIGHTSEL;
200                                         seq->flag |= SEQ_LEFTSEL;
201                                         break;
202                                 case SEQ_SIDE_RIGHT:
203                                         neighbor->flag |= SELECT;
204                                         recurs_sel_seq(neighbor);
205                                         neighbor->flag |= SEQ_LEFTSEL;
206                                         seq->flag |= SEQ_RIGHTSEL;
207                                         break;
208                         }
209                         seq->flag |= SELECT;
210                         change = 1;
211                 }
212         }
213         if (change) {
214         }
215 }
216 #endif
217
218 /* (de)select operator */
219 static int sequencer_de_select_all_exec(bContext *C, wmOperator *op)
220 {
221         int action = RNA_enum_get(op->ptr, "action");
222
223         Scene *scene = CTX_data_scene(C);
224         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
225         Sequence *seq;
226
227         if (action == SEL_TOGGLE) {
228                 action = SEL_SELECT;
229                 for (seq = ed->seqbasep->first; seq; seq = seq->next) {
230                         if (seq->flag & SEQ_ALLSEL) {
231                                 action = SEL_DESELECT;
232                                 break;
233                         }
234                 }
235         }
236
237         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
238                 switch (action) {
239                         case SEL_SELECT:
240                                 seq->flag &= ~(SEQ_LEFTSEL + SEQ_RIGHTSEL);
241                                 seq->flag |= SELECT;
242                                 break;
243                         case SEL_DESELECT:
244                                 seq->flag &= ~SEQ_ALLSEL;
245                                 break;
246                         case SEL_INVERT:
247                                 if (seq->flag & SEQ_ALLSEL) {
248                                         seq->flag &= ~SEQ_ALLSEL;
249                                 }
250                                 else {
251                                         seq->flag &= ~(SEQ_LEFTSEL + SEQ_RIGHTSEL);
252                                         seq->flag |= SELECT;
253                                 }
254                                 break;
255                 }
256         }
257
258         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
259
260         return OPERATOR_FINISHED;
261 }
262
263 void SEQUENCER_OT_select_all(struct wmOperatorType *ot)
264 {
265         /* identifiers */
266         ot->name = "(De)select All";
267         ot->idname = "SEQUENCER_OT_select_all";
268         ot->description = "Select or deselect all strips";
269         
270         /* api callbacks */
271         ot->exec = sequencer_de_select_all_exec;
272         ot->poll = sequencer_edit_poll;
273         
274         /* flags */
275         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
276
277         WM_operator_properties_select_all(ot);
278 }
279
280
281 /* (de)select operator */
282 static int sequencer_select_inverse_exec(bContext *C, wmOperator *UNUSED(op))
283 {
284         Scene *scene = CTX_data_scene(C);
285         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
286         Sequence *seq;
287
288         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
289                 if (seq->flag & SELECT) {
290                         seq->flag &= ~SEQ_ALLSEL;
291                 }
292                 else {
293                         seq->flag &= ~(SEQ_LEFTSEL + SEQ_RIGHTSEL);
294                         seq->flag |= SELECT;
295                 }
296         }
297
298         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
299         
300         return OPERATOR_FINISHED;
301 }
302
303 void SEQUENCER_OT_select_inverse(struct wmOperatorType *ot)
304 {
305         /* identifiers */
306         ot->name = "Select Inverse";
307         ot->idname = "SEQUENCER_OT_select_inverse";
308         ot->description = "Select unselected strips";
309         
310         /* api callbacks */
311         ot->exec = sequencer_select_inverse_exec;
312         ot->poll = sequencer_edit_poll;
313         
314         /* flags */
315         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
316 }
317
318 static int sequencer_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
319 {
320         View2D *v2d = UI_view2d_fromcontext(C);
321         Scene *scene = CTX_data_scene(C);
322         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
323         short extend = RNA_boolean_get(op->ptr, "extend");
324         short linked_handle = RNA_boolean_get(op->ptr, "linked_handle");
325         short left_right = RNA_boolean_get(op->ptr, "left_right");
326         short linked_time = RNA_boolean_get(op->ptr, "linked_time");
327         
328         Sequence *seq, *neighbor, *act_orig;
329         int hand, sel_side;
330         TimeMarker *marker;
331
332         if (ed == NULL)
333                 return OPERATOR_CANCELLED;
334         
335         marker = find_nearest_marker(SCE_MARKERS, 1); //XXX - dummy function for now
336         
337         seq = find_nearest_seq(scene, v2d, &hand, event->mval);
338
339         // XXX - not nice, Ctrl+RMB needs to do left_right only when not over a strip
340         if (seq && linked_time && left_right)
341                 left_right = FALSE;
342
343
344         if (marker) {
345                 int oldflag;
346                 /* select timeline marker */
347                 if (extend) {
348                         oldflag = marker->flag;
349                         if (oldflag & SELECT)
350                                 marker->flag &= ~SELECT;
351                         else
352                                 marker->flag |= SELECT;
353                 }
354                 else {
355                         /* deselect_markers(0, 0); */ /* XXX, in 2.4x, seq selection used to deselect all, need to re-thnik this for 2.5 */
356                         marker->flag |= SELECT;                         
357                 }
358                 
359         }
360         else if (left_right) {
361                 /* use different logic for this */
362                 float x;
363                 ED_sequencer_deselect_all(scene);
364                 UI_view2d_region_to_view(v2d, event->mval[0], event->mval[1], &x, NULL);
365
366                 SEQP_BEGIN (ed, seq)
367                 {
368                         if (x < CFRA) {
369                                 if (seq->enddisp < CFRA) {
370                                         seq->flag |= SELECT;
371                                         recurs_sel_seq(seq);
372                                 }
373                         }
374                         else {
375                                 if (seq->startdisp > CFRA) {
376                                         seq->flag |= SELECT;
377                                         recurs_sel_seq(seq);
378                                 }
379                         }
380                 }
381                 SEQ_END
382                 
383                 {
384                         SpaceSeq *sseq = CTX_wm_space_seq(C);
385                         if (sseq && sseq->flag & SEQ_MARKER_TRANS) {
386                                 TimeMarker *tmarker;
387
388                                 for (tmarker = scene->markers.first; tmarker; tmarker = tmarker->next) {
389                                         if (((x <  CFRA) && tmarker->frame <  CFRA) ||
390                                             ((x >= CFRA) && tmarker->frame >= CFRA))
391                                         {
392                                                 tmarker->flag |= SELECT;
393                                         }
394                                         else {
395                                                 tmarker->flag &= ~SELECT;
396                                         }
397                                 }
398                         }
399                 }
400         }
401         else {
402                 // seq= find_nearest_seq(scene, v2d, &hand, mval);
403
404                 act_orig = ed->act_seq;
405
406                 if (extend == 0 && linked_handle == 0)
407                         ED_sequencer_deselect_all(scene);
408         
409                 if (seq) {
410                         BKE_sequencer_active_set(scene, seq);
411         
412                         if ((seq->type == SEQ_TYPE_IMAGE) || (seq->type == SEQ_TYPE_MOVIE)) {
413                                 if (seq->strip) {
414                                         BLI_strncpy(ed->act_imagedir, seq->strip->dir, FILE_MAXDIR);
415                                 }
416                         }
417                         else
418                         if (seq->type == SEQ_TYPE_SOUND_RAM) {
419                                 if (seq->strip) {
420                                         BLI_strncpy(ed->act_sounddir, seq->strip->dir, FILE_MAXDIR);
421                                 }
422                         }
423         
424                         if (extend && (seq->flag & SELECT) && ed->act_seq == act_orig) {
425                                 switch (hand) {
426                                         case SEQ_SIDE_NONE:
427                                                 if (linked_handle == 0)
428                                                         seq->flag &= ~SEQ_ALLSEL;
429                                                 break;
430                                         case SEQ_SIDE_LEFT:
431                                                 seq->flag ^= SEQ_LEFTSEL;
432                                                 break;
433                                         case SEQ_SIDE_RIGHT:
434                                                 seq->flag ^= SEQ_RIGHTSEL;
435                                                 break;
436                                 }
437                         }
438                         else {
439                                 seq->flag |= SELECT;
440                                 if (hand == SEQ_SIDE_LEFT) seq->flag |= SEQ_LEFTSEL;
441                                 if (hand == SEQ_SIDE_RIGHT) seq->flag |= SEQ_RIGHTSEL;
442                         }
443                         
444                         /* On Alt selection, select the strip and bordering handles */
445                         if (linked_handle && !ELEM(hand, SEQ_SIDE_LEFT, SEQ_SIDE_RIGHT)) {
446                                 if (extend == 0) ED_sequencer_deselect_all(scene);
447                                 seq->flag |= SELECT;
448                                 select_surrounding_handles(scene, seq);
449                         }
450                         else if (linked_handle && ELEM(hand, SEQ_SIDE_LEFT, SEQ_SIDE_RIGHT) && (seq->flag & SELECT)) {
451                                 /*
452                                  * First click selects adjacent handles on that side.
453                                  * Second click selects all strips in that direction.
454                                  * If there are no adjacent strips, it just selects all in that direction.
455                                  */
456                                 sel_side = hand;
457                                 neighbor = find_neighboring_sequence(scene, seq, sel_side, -1);
458                                 if (neighbor) {
459                                         switch (sel_side) {
460                                                 case SEQ_SIDE_LEFT:
461                                                         if ((seq->flag & SEQ_LEFTSEL) && (neighbor->flag & SEQ_RIGHTSEL)) {
462                                                                 if (extend == 0) ED_sequencer_deselect_all(scene);
463                                                                 seq->flag |= SELECT;
464
465                                                                 select_active_side(ed->seqbasep, SEQ_SIDE_LEFT, seq->machine, seq->startdisp);
466                                                         }
467                                                         else {
468                                                                 if (extend == 0) ED_sequencer_deselect_all(scene);
469                                                                 seq->flag |= SELECT;
470
471                                                                 neighbor->flag |= SELECT;
472                                                                 recurs_sel_seq(neighbor);
473                                                                 neighbor->flag |= SEQ_RIGHTSEL;
474                                                                 seq->flag |= SEQ_LEFTSEL;
475                                                         }
476                                                         break;
477                                                 case SEQ_SIDE_RIGHT:
478                                                         if ((seq->flag & SEQ_RIGHTSEL) && (neighbor->flag & SEQ_LEFTSEL)) {
479                                                                 if (extend == 0) ED_sequencer_deselect_all(scene);
480                                                                 seq->flag |= SELECT;
481
482                                                                 select_active_side(ed->seqbasep, SEQ_SIDE_RIGHT, seq->machine, seq->startdisp);
483                                                         }
484                                                         else {
485                                                                 if (extend == 0) ED_sequencer_deselect_all(scene);
486                                                                 seq->flag |= SELECT;
487
488                                                                 neighbor->flag |= SELECT;
489                                                                 recurs_sel_seq(neighbor);
490                                                                 neighbor->flag |= SEQ_LEFTSEL;
491                                                                 seq->flag |= SEQ_RIGHTSEL;
492                                                         }
493                                                         break;
494                                         }
495                                 }
496                                 else {
497                                         if (extend == 0) ED_sequencer_deselect_all(scene);
498                                         select_active_side(ed->seqbasep, sel_side, seq->machine, seq->startdisp);
499                                 }
500                         }
501                         recurs_sel_seq(seq);
502
503                         if (linked_time) {
504                                 select_linked_time(ed->seqbasep, seq);
505                         }
506                 }
507         }
508         
509         /* marker transform */
510 #if 0 // XXX probably need to redo this differently for 2.5
511         if (marker) {
512                 int mval[2], xo, yo;
513 //              getmouseco_areawin(mval);
514                 xo = mval[0];
515                 yo = mval[1];
516                 
517                 while (get_mbut()) {
518 //                      getmouseco_areawin(mval);
519                         if (abs(mval[0] - xo) + abs(mval[1] - yo) > 4) {
520                                 transform_markers('g', 0);
521                                 return;
522                         }
523                 }
524         }
525 #endif
526         
527         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
528
529         /* allowing tweaks */
530         return OPERATOR_FINISHED | OPERATOR_PASS_THROUGH;
531 }
532
533 void SEQUENCER_OT_select(wmOperatorType *ot)
534 {
535         /* identifiers */
536         ot->name = "Activate/Select";
537         ot->idname = "SEQUENCER_OT_select";
538         ot->description = "Select a strip (last selected becomes the \"active strip\")";
539         
540         /* api callbacks */
541         ot->invoke = sequencer_select_invoke;
542         ot->poll = ED_operator_sequencer_active;
543         
544         /* flags */
545         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
546         
547         /* properties */
548         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the selection");
549         RNA_def_boolean(ot->srna, "linked_handle", 0, "Linked Handle", "Select handles next to the active strip");
550         /* for animation this is an enum but atm having an enum isn't useful for us */
551         RNA_def_boolean(ot->srna, "left_right", 0, "Left/Right", "Select based on the current frame side the cursor is on");
552         RNA_def_boolean(ot->srna, "linked_time", 0, "Linked Time", "Select other strips at the same time");
553 }
554
555
556
557
558 /* run recursively to select linked */
559 static int select_more_less_seq__internal(Scene *scene, int sel, int linked)
560 {
561         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
562         Sequence *seq, *neighbor;
563         int change = 0;
564         int isel;
565         
566         if (ed == NULL) return 0;
567         
568         if (sel) {
569                 sel = SELECT;
570                 isel = 0;
571         }
572         else {
573                 sel = 0;
574                 isel = SELECT;
575         }
576         
577         if (!linked) {
578                 /* if not linked we only want to touch each seq once, newseq */
579                 for (seq = ed->seqbasep->first; seq; seq = seq->next) {
580                         seq->tmp = NULL;
581                 }
582         }
583         
584         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
585                 if ((int)(seq->flag & SELECT) == sel) {
586                         if ((linked == 0 && seq->tmp) == 0) {
587                                 /* only get unselected nabours */
588                                 neighbor = find_neighboring_sequence(scene, seq, SEQ_SIDE_LEFT, isel);
589                                 if (neighbor) {
590                                         if (sel) { neighbor->flag |= SELECT; recurs_sel_seq(neighbor); }
591                                         else neighbor->flag &= ~SELECT;
592                                         if (linked == 0) neighbor->tmp = (Sequence *)1;
593                                         change = 1;
594                                 }
595                                 neighbor = find_neighboring_sequence(scene, seq, SEQ_SIDE_RIGHT, isel);
596                                 if (neighbor) {
597                                         if (sel) { neighbor->flag |= SELECT; recurs_sel_seq(neighbor); }
598                                         else neighbor->flag &= ~SELECT;
599                                         if (linked == 0) neighbor->tmp = (void *)1;
600                                         change = 1;
601                                 }
602                         }
603                 }
604         }
605         
606         return change;
607 }
608
609
610
611 /* select more operator */
612 static int sequencer_select_more_exec(bContext *C, wmOperator *UNUSED(op))
613 {
614         Scene *scene = CTX_data_scene(C);
615         
616         if (!select_more_less_seq__internal(scene, 0, 0))
617                 return OPERATOR_CANCELLED;
618
619         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
620         
621         return OPERATOR_FINISHED;
622 }
623
624 void SEQUENCER_OT_select_more(wmOperatorType *ot)
625 {
626         /* identifiers */
627         ot->name = "Select More";
628         ot->idname = "SEQUENCER_OT_select_more";
629         ot->description = "Select more strips adjacent to the current selection";
630         
631         /* api callbacks */
632         ot->exec = sequencer_select_more_exec;
633         ot->poll = sequencer_edit_poll;
634         
635         /* flags */
636         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
637         
638         /* properties */
639 }
640
641
642 /* select less operator */
643 static int sequencer_select_less_exec(bContext *C, wmOperator *UNUSED(op))
644 {
645         Scene *scene = CTX_data_scene(C);
646         
647         if (!select_more_less_seq__internal(scene, 1, 0))
648                 return OPERATOR_CANCELLED;
649  
650         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
651         
652         return OPERATOR_FINISHED;
653 }
654
655 void SEQUENCER_OT_select_less(wmOperatorType *ot)
656 {
657         /* identifiers */
658         ot->name = "Select Less";
659         ot->idname = "SEQUENCER_OT_select_less";
660         ot->description = "Shrink the current selection of adjacent selected strips";
661         
662         /* api callbacks */
663         ot->exec = sequencer_select_less_exec;
664         ot->poll = sequencer_edit_poll;
665         
666         /* flags */
667         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
668         
669         /* properties */
670 }
671
672
673 /* select pick linked operator (uses the mouse) */
674 static int sequencer_select_linked_pick_invoke(bContext *C, wmOperator *op, wmEvent *event)
675 {
676         Scene *scene = CTX_data_scene(C);
677         View2D *v2d = UI_view2d_fromcontext(C);
678         
679         short extend = RNA_boolean_get(op->ptr, "extend");
680         
681         Sequence *mouse_seq;
682         int selected, hand;
683
684         /* this works like UV, not mesh */
685         mouse_seq = find_nearest_seq(scene, v2d, &hand, event->mval);
686         if (!mouse_seq)
687                 return OPERATOR_FINISHED;  /* user error as with mesh?? */
688         
689         if (extend == 0)
690                 ED_sequencer_deselect_all(scene);
691         
692         mouse_seq->flag |= SELECT;
693         recurs_sel_seq(mouse_seq);
694         
695         selected = 1;
696         while (selected) {
697                 selected = select_more_less_seq__internal(scene, 1, 1);
698         }
699         
700         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
701         
702         return OPERATOR_FINISHED;
703 }
704
705 void SEQUENCER_OT_select_linked_pick(wmOperatorType *ot)
706 {
707         /* identifiers */
708         ot->name = "Select pick linked";
709         ot->idname = "SEQUENCER_OT_select_linked_pick";
710         ot->description = "Select a chain of linked strips nearest to the mouse pointer";
711         
712         /* api callbacks */
713         ot->invoke = sequencer_select_linked_pick_invoke;
714         ot->poll = ED_operator_sequencer_active;
715         
716         /* flags */
717         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
718         
719         /* properties */
720         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the selection");
721 }
722
723
724 /* select linked operator */
725 static int sequencer_select_linked_exec(bContext *C, wmOperator *UNUSED(op))
726 {
727         Scene *scene = CTX_data_scene(C);
728         int selected;
729
730         selected = 1;
731         while (selected) {
732                 selected = select_more_less_seq__internal(scene, 1, 1);
733         }
734
735         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
736
737         return OPERATOR_FINISHED;
738 }
739
740 void SEQUENCER_OT_select_linked(wmOperatorType *ot)
741 {
742         /* identifiers */
743         ot->name = "Select linked";
744         ot->idname = "SEQUENCER_OT_select_linked";
745         ot->description = "Select all strips adjacent to the current selection";
746         
747         /* api callbacks */
748         ot->exec = sequencer_select_linked_exec;
749         ot->poll = sequencer_edit_poll;
750         
751         /* flags */
752         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
753         
754         /* properties */
755 }
756
757
758 /* select handles operator */
759 static int sequencer_select_handles_exec(bContext *C, wmOperator *op)
760 {
761         Scene *scene = CTX_data_scene(C);
762         Editing *ed = BKE_sequencer_editing_get(scene, 0);
763         Sequence *seq;
764         int sel_side = RNA_enum_get(op->ptr, "side");
765
766
767         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
768                 if (seq->flag & SELECT) {
769                         switch (sel_side) {
770                                 case SEQ_SIDE_LEFT:
771                                         seq->flag &= ~SEQ_RIGHTSEL;
772                                         seq->flag |= SEQ_LEFTSEL;
773                                         break;
774                                 case SEQ_SIDE_RIGHT:
775                                         seq->flag &= ~SEQ_LEFTSEL;
776                                         seq->flag |= SEQ_RIGHTSEL;
777                                         break;
778                                 case SEQ_SIDE_BOTH:
779                                         seq->flag |= SEQ_LEFTSEL + SEQ_RIGHTSEL;
780                                         break;
781                         }
782                 }
783         }
784
785         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
786
787         return OPERATOR_FINISHED;
788 }
789
790 void SEQUENCER_OT_select_handles(wmOperatorType *ot)
791 {
792         /* identifiers */
793         ot->name = "Select Handles";
794         ot->idname = "SEQUENCER_OT_select_handles";
795         ot->description = "Select manipulator handles on the sides of the selected strip";
796         
797         /* api callbacks */
798         ot->exec = sequencer_select_handles_exec;
799         ot->poll = sequencer_edit_poll;
800         
801         /* flags */
802         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
803         
804         /* properties */
805         RNA_def_enum(ot->srna, "side", prop_side_types, SEQ_SIDE_BOTH, "Side", "The side of the handle that is selected");
806 }
807
808 /* select side operator */
809 static int sequencer_select_active_side_exec(bContext *C, wmOperator *op)
810 {
811         Scene *scene = CTX_data_scene(C);
812         Editing *ed = BKE_sequencer_editing_get(scene, 0);
813         Sequence *seq_act = BKE_sequencer_active_get(scene);
814
815         if (ed == NULL || seq_act == NULL)
816                 return OPERATOR_CANCELLED;
817
818         seq_act->flag |= SELECT;
819
820         select_active_side(ed->seqbasep, RNA_enum_get(op->ptr, "side"), seq_act->machine, seq_act->startdisp);
821
822         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
823
824         return OPERATOR_FINISHED;
825 }
826
827 void SEQUENCER_OT_select_active_side(wmOperatorType *ot)
828 {
829         /* identifiers */
830         ot->name = "Select Active Side";
831         ot->idname = "SEQUENCER_OT_select_active_side";
832         ot->description = "Select strips on the nominated side of the active strip";
833         
834         /* api callbacks */
835         ot->exec = sequencer_select_active_side_exec;
836         ot->poll = sequencer_edit_poll;
837
838         /* flags */
839         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
840
841         /* properties */
842         RNA_def_enum(ot->srna, "side", prop_side_types, SEQ_SIDE_BOTH, "Side", "The side of the handle that is selected");
843 }
844
845
846 /* borderselect operator */
847 static int sequencer_borderselect_exec(bContext *C, wmOperator *op)
848 {
849         Scene *scene = CTX_data_scene(C);
850         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
851         View2D *v2d = UI_view2d_fromcontext(C);
852         
853         Sequence *seq;
854         rcti rect;
855         rctf rectf, rq;
856         short selecting = (RNA_int_get(op->ptr, "gesture_mode") == GESTURE_MODAL_SELECT);
857         int extend = RNA_boolean_get(op->ptr, "extend");
858         int mval[2];
859
860         if (ed == NULL)
861                 return OPERATOR_CANCELLED;
862
863         WM_operator_properties_border_to_rcti(op, &rect);
864         
865         mval[0] = rect.xmin;
866         mval[1] = rect.ymin;
867         UI_view2d_region_to_view(v2d, mval[0], mval[1], &rectf.xmin, &rectf.ymin);
868         mval[0] = rect.xmax;
869         mval[1] = rect.ymax;
870         UI_view2d_region_to_view(v2d, mval[0], mval[1], &rectf.xmax, &rectf.ymax);
871
872         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
873                 seq_rectf(seq, &rq);
874                 
875                 if (BLI_rctf_isect(&rq, &rectf, NULL)) {
876                         if (selecting) seq->flag |= SELECT;
877                         else seq->flag &= ~SEQ_ALLSEL;
878                         recurs_sel_seq(seq);
879                 }
880                 else if (!extend) {
881                         seq->flag &= ~SEQ_ALLSEL;
882                         recurs_sel_seq(seq);
883                 }
884         }
885
886         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
887
888         return OPERATOR_FINISHED;
889
890
891
892 /* ****** Border Select ****** */
893 void SEQUENCER_OT_select_border(wmOperatorType *ot)
894 {
895         /* identifiers */
896         ot->name = "Border Select";
897         ot->idname = "SEQUENCER_OT_select_border";
898         ot->description = "Enable border select mode";
899         
900         /* api callbacks */
901         ot->invoke = WM_border_select_invoke;
902         ot->exec = sequencer_borderselect_exec;
903         ot->modal = WM_border_select_modal;
904         ot->cancel = WM_border_select_cancel;
905         
906         ot->poll = ED_operator_sequencer_active;
907         
908         /* flags */
909         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
910         
911         /* rna */
912         WM_operator_properties_gesture_border(ot, TRUE);
913 }
914
915 /* ****** Selected Grouped ****** */
916
917 static EnumPropertyItem sequencer_prop_select_grouped_types[] = {
918         {1, "TYPE", 0, "Type", "Shared strip type"},
919         {2, "TYPE_BASIC", 0, "Global Type", "All strips of same basic type (Graphical or Sound)"},
920         {3, "TYPE_EFFECT", 0, "Effect Type",
921          "Shared strip effect type (if active strip is not an effect one, select all non-effect strips)"},
922         {4, "DATA", 0, "Data", "Shared data (scene, image, sound, etc.)"},
923         {5, "EFFECT", 0, "Effect", "Shared effects"},
924         {6, "EFFECT_LINK", 0, "Effect/Linked",
925          "Other strips affected by the active one (sharing some time, and below or effect-assigned)"},
926         {7, "OVERLAP", 0, "Overlap", "Overlapping time"},
927         {0, NULL, 0, NULL, NULL}
928 };
929
930 #define SEQ_IS_SOUND(_seq) ((_seq->type & SEQ_TYPE_SOUND_RAM) && !(_seq->type & SEQ_TYPE_EFFECT))
931
932 #define SEQ_IS_EFFECT(_seq) (_seq->type & SEQ_TYPE_EFFECT)
933
934 #define SEQ_USE_DATA(_seq) (ELEM3(_seq->type, SEQ_TYPE_SCENE, SEQ_TYPE_MOVIECLIP, SEQ_TYPE_MASK) || SEQ_HAS_PATH(_seq))
935
936 static short select_grouped_type(Editing *ed, Sequence *actseq)
937 {
938         Sequence *seq;
939         short changed = FALSE;
940
941         SEQP_BEGIN (ed, seq)
942         {
943                 if (seq->type == actseq->type) {
944                         seq->flag |= SELECT;
945                         changed = TRUE;
946                 }
947         }
948         SEQ_END;
949
950         return changed;
951 }
952
953 static short select_grouped_type_basic(Editing *ed, Sequence *actseq)
954 {
955         Sequence *seq;
956         short changed = FALSE;
957         short is_sound = SEQ_IS_SOUND(actseq);
958
959         SEQP_BEGIN (ed, seq)
960         {
961                 if (is_sound ? SEQ_IS_SOUND(seq) : !SEQ_IS_SOUND(seq)) {
962                         seq->flag |= SELECT;
963                         changed = TRUE;
964                 }
965         }
966         SEQ_END;
967
968         return changed;
969 }
970
971 static short select_grouped_type_effect(Editing *ed, Sequence *actseq)
972 {
973         Sequence *seq;
974         short changed = FALSE;
975         short is_effect = SEQ_IS_EFFECT(actseq);
976
977         SEQP_BEGIN (ed, seq)
978         {
979                 if (is_effect ? SEQ_IS_EFFECT(seq) : !SEQ_IS_EFFECT(seq)) {
980                         seq->flag |= SELECT;
981                         changed = TRUE;
982                 }
983         }
984         SEQ_END;
985
986         return changed;
987 }
988
989 static short select_grouped_data(Editing *ed, Sequence *actseq)
990 {
991         Sequence *seq;
992         short changed = FALSE;
993         char *dir = actseq->strip ? actseq->strip->dir : NULL;
994
995         if (!SEQ_USE_DATA(actseq))
996                 return changed;
997
998         if (SEQ_HAS_PATH(actseq) && dir) {
999                 SEQP_BEGIN (ed, seq)
1000                 {
1001                         if (SEQ_HAS_PATH(seq) && seq->strip && strcmp(seq->strip->dir, dir) == 0) {
1002                                 seq->flag |= SELECT;
1003                                 changed = TRUE;
1004                         }
1005                 }
1006                 SEQ_END;
1007         }
1008         else if (actseq->type == SEQ_TYPE_SCENE) {
1009                 Scene *sce = actseq->scene;
1010                 SEQP_BEGIN (ed, seq)
1011                 {
1012                         if (seq->type == SEQ_TYPE_SCENE && seq->scene == sce) {
1013                                 seq->flag |= SELECT;
1014                                 changed = TRUE;
1015                         }
1016                 }
1017                 SEQ_END;
1018         }
1019         else if (actseq->type == SEQ_TYPE_MOVIECLIP) {
1020                 MovieClip *clip = actseq->clip;
1021                 SEQP_BEGIN (ed, seq)
1022                 {
1023                         if (seq->type == SEQ_TYPE_MOVIECLIP && seq->clip == clip) {
1024                                 seq->flag |= SELECT;
1025                                 changed = TRUE;
1026                         }
1027                 }
1028                 SEQ_END;
1029         }
1030         else if (actseq->type == SEQ_TYPE_MASK) {
1031                 struct Mask *mask = actseq->mask;
1032                 SEQP_BEGIN (ed, seq)
1033                 {
1034                         if (seq->type == SEQ_TYPE_MASK && seq->mask == mask) {
1035                                 seq->flag |= SELECT;
1036                                 changed = TRUE;
1037                         }
1038                 }
1039                 SEQ_END;
1040         }
1041
1042         return changed;
1043 }
1044
1045 static short select_grouped_effect(Editing *ed, Sequence *actseq)
1046 {
1047         Sequence *seq;
1048         short changed = FALSE;
1049         short effects[SEQ_TYPE_EFFECT_MAX + 1];
1050         int i;
1051
1052         for (i = 0; i <= SEQ_TYPE_EFFECT_MAX; i++)
1053                 effects[i] = FALSE;
1054
1055         SEQP_BEGIN (ed, seq)
1056         {
1057                 if (ELEM3(actseq, seq->seq1, seq->seq2, seq->seq3)) {
1058                         effects[seq->type] = TRUE;
1059                 }
1060         }
1061         SEQ_END;
1062
1063         SEQP_BEGIN (ed, seq)
1064         {
1065                 if (effects[seq->type]) {
1066                         if (seq->seq1) seq->seq1->flag |= SELECT;
1067                         if (seq->seq2) seq->seq2->flag |= SELECT;
1068                         if (seq->seq3) seq->seq3->flag |= SELECT;
1069                         changed = TRUE;
1070                 }
1071         }
1072         SEQ_END;
1073
1074         return changed;
1075 }
1076
1077 static short select_grouped_time_overlap(Editing *ed, Sequence *actseq)
1078 {
1079         Sequence *seq;
1080         short changed = FALSE;
1081
1082         SEQP_BEGIN (ed, seq)
1083         {
1084                 if (!((seq->startdisp >= actseq->enddisp) || (seq->enddisp < actseq->startdisp))) {
1085                         seq->flag |= SELECT;
1086                         changed = TRUE;
1087                 }
1088         }
1089         SEQ_END;
1090
1091         return changed;
1092 }
1093
1094 static short select_grouped_effect_link(Editing *ed, Sequence *actseq)
1095 {
1096         Sequence *seq = NULL;
1097         short changed = FALSE;
1098         short is_audio = ((actseq->type == SEQ_TYPE_META) || SEQ_IS_SOUND(actseq));
1099         int startdisp = actseq->startdisp;
1100         int enddisp   = actseq->enddisp;
1101         int machine   = actseq->machine;
1102         SeqIterator iter;
1103
1104         SEQP_BEGIN (ed, seq)
1105         {
1106                 seq->tmp = NULL;
1107         }
1108         SEQ_END;
1109
1110         actseq->tmp = SET_INT_IN_POINTER(TRUE);
1111
1112         for (BKE_seqence_iterator_begin(ed, &iter, TRUE); iter.valid; BKE_seqence_iterator_next(&iter)) {
1113                 seq = iter.seq;
1114
1115                 /* Ignore all seqs already selected! */
1116                 /* Ignore all seqs not sharing some time with active one. */
1117                 /* Ignore all seqs of incompatible types (audio vs video). */
1118                 if ((seq->flag & SELECT) || (seq->startdisp >= enddisp) || (seq->enddisp < startdisp) ||
1119                     (!is_audio && SEQ_IS_SOUND(seq)) ||
1120                     (is_audio && !((seq->type == SEQ_TYPE_META) || SEQ_IS_SOUND(seq))))
1121                 {
1122                         continue;
1123                 }
1124
1125                 /* If the seq is an effect one, we need extra cheking! */
1126                 if (SEQ_IS_EFFECT(seq) && ((seq->seq1 && seq->seq1->tmp) ||
1127                                            (seq->seq2 && seq->seq2->tmp) ||
1128                                            (seq->seq3 && seq->seq3->tmp)))
1129                 {
1130                         if (startdisp > seq->startdisp) startdisp = seq->startdisp;
1131                         if (enddisp < seq->enddisp) enddisp = seq->enddisp;
1132                         if (machine < seq->machine) machine = seq->machine;
1133
1134                         seq->tmp = SET_INT_IN_POINTER(TRUE);
1135
1136                         seq->flag |= SELECT;
1137                         changed = TRUE;
1138
1139                         /* Unfortunately, we must restart checks from the beginning. */
1140                         BKE_seqence_iterator_end(&iter);
1141                         BKE_seqence_iterator_begin(ed, &iter, TRUE);
1142                 }
1143
1144                 /* Video strips bellow active one, or any strip for audio (order do no matters here!). */
1145                 else if (seq->machine < machine || is_audio) {
1146                         seq->flag |= SELECT;
1147                         changed = TRUE;
1148                 }
1149         }
1150         BKE_seqence_iterator_end(&iter);
1151
1152         return changed;
1153 }
1154
1155 #undef SEQ_IS_SOUND
1156 #undef SEQ_IS_EFFECT
1157 #undef SEQ_USE_DATA
1158
1159 static int sequencer_select_grouped_exec(bContext *C, wmOperator *op)
1160 {
1161         Scene *scene  = CTX_data_scene(C);
1162         Editing *ed   = BKE_sequencer_editing_get(scene, 0);
1163         Sequence *seq, *actseq = BKE_sequencer_active_get(scene);
1164         int type = RNA_enum_get(op->ptr, "type");
1165         short changed = 0, extend;
1166
1167         extend = RNA_boolean_get(op->ptr, "extend");
1168
1169         if (actseq == NULL) {
1170                 BKE_report(op->reports, RPT_ERROR, "No Active Sequence!");
1171                 return OPERATOR_CANCELLED;
1172         }
1173
1174         if (extend == 0) {
1175                 SEQP_BEGIN (ed, seq)
1176                 {
1177                         seq->flag &= ~SELECT;
1178                         changed = TRUE;
1179                 }
1180                 SEQ_END;
1181         }
1182
1183         if      (type == 1) changed |= select_grouped_type(ed, actseq);
1184         else if (type == 2) changed |= select_grouped_type_basic(ed, actseq);
1185         else if (type == 3) changed |= select_grouped_type_effect(ed, actseq);
1186         else if (type == 4) changed |= select_grouped_data(ed, actseq);
1187         else if (type == 5) changed |= select_grouped_effect(ed, actseq);
1188         else if (type == 6) changed |= select_grouped_effect_link(ed, actseq);
1189         else if (type == 7) changed |= select_grouped_time_overlap(ed, actseq);
1190
1191         if (changed) {
1192                 WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
1193                 return OPERATOR_FINISHED;
1194         }
1195
1196         return OPERATOR_CANCELLED;
1197 }
1198
1199 void SEQUENCER_OT_select_grouped(wmOperatorType *ot)
1200 {
1201         /* identifiers */
1202         ot->name = "Select Grouped";
1203         ot->description = "Select all strips grouped by various properties";
1204         ot->idname = "SEQUENCER_OT_select_grouped";
1205         
1206         /* api callbacks */
1207         ot->invoke = WM_menu_invoke;
1208         ot->exec = sequencer_select_grouped_exec;
1209         ot->poll = sequencer_edit_poll;
1210         
1211         /* flags */
1212         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1213         
1214         /* properties */
1215         RNA_def_boolean(ot->srna, "extend", FALSE, "Extend", "Extend selection instead of deselecting everything first");
1216         ot->prop = RNA_def_enum(ot->srna, "type", sequencer_prop_select_grouped_types, 0, "Type", "");
1217 }
1218