0cce8a81c847711559c373849a97f278c720ca4b
[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, bool 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 if (seq->type == SEQ_TYPE_SOUND_RAM) {
418                                 if (seq->strip) {
419                                         BLI_strncpy(ed->act_sounddir, seq->strip->dir, FILE_MAXDIR);
420                                 }
421                         }
422         
423                         if (extend && (seq->flag & SELECT) && ed->act_seq == act_orig) {
424                                 switch (hand) {
425                                         case SEQ_SIDE_NONE:
426                                                 if (linked_handle == 0)
427                                                         seq->flag &= ~SEQ_ALLSEL;
428                                                 break;
429                                         case SEQ_SIDE_LEFT:
430                                                 seq->flag ^= SEQ_LEFTSEL;
431                                                 break;
432                                         case SEQ_SIDE_RIGHT:
433                                                 seq->flag ^= SEQ_RIGHTSEL;
434                                                 break;
435                                 }
436                         }
437                         else {
438                                 seq->flag |= SELECT;
439                                 if (hand == SEQ_SIDE_LEFT) seq->flag |= SEQ_LEFTSEL;
440                                 if (hand == SEQ_SIDE_RIGHT) seq->flag |= SEQ_RIGHTSEL;
441                         }
442                         
443                         /* On Alt selection, select the strip and bordering handles */
444                         if (linked_handle && !ELEM(hand, SEQ_SIDE_LEFT, SEQ_SIDE_RIGHT)) {
445                                 if (extend == 0) ED_sequencer_deselect_all(scene);
446                                 seq->flag |= SELECT;
447                                 select_surrounding_handles(scene, seq);
448                         }
449                         else if (linked_handle && ELEM(hand, SEQ_SIDE_LEFT, SEQ_SIDE_RIGHT) && (seq->flag & SELECT)) {
450                                 /*
451                                  * First click selects adjacent handles on that side.
452                                  * Second click selects all strips in that direction.
453                                  * If there are no adjacent strips, it just selects all in that direction.
454                                  */
455                                 sel_side = hand;
456                                 neighbor = find_neighboring_sequence(scene, seq, sel_side, -1);
457                                 if (neighbor) {
458                                         switch (sel_side) {
459                                                 case SEQ_SIDE_LEFT:
460                                                         if ((seq->flag & SEQ_LEFTSEL) && (neighbor->flag & SEQ_RIGHTSEL)) {
461                                                                 if (extend == 0) ED_sequencer_deselect_all(scene);
462                                                                 seq->flag |= SELECT;
463
464                                                                 select_active_side(ed->seqbasep, SEQ_SIDE_LEFT, seq->machine, seq->startdisp);
465                                                         }
466                                                         else {
467                                                                 if (extend == 0) ED_sequencer_deselect_all(scene);
468                                                                 seq->flag |= SELECT;
469
470                                                                 neighbor->flag |= SELECT;
471                                                                 recurs_sel_seq(neighbor);
472                                                                 neighbor->flag |= SEQ_RIGHTSEL;
473                                                                 seq->flag |= SEQ_LEFTSEL;
474                                                         }
475                                                         break;
476                                                 case SEQ_SIDE_RIGHT:
477                                                         if ((seq->flag & SEQ_RIGHTSEL) && (neighbor->flag & SEQ_LEFTSEL)) {
478                                                                 if (extend == 0) ED_sequencer_deselect_all(scene);
479                                                                 seq->flag |= SELECT;
480
481                                                                 select_active_side(ed->seqbasep, SEQ_SIDE_RIGHT, seq->machine, seq->startdisp);
482                                                         }
483                                                         else {
484                                                                 if (extend == 0) ED_sequencer_deselect_all(scene);
485                                                                 seq->flag |= SELECT;
486
487                                                                 neighbor->flag |= SELECT;
488                                                                 recurs_sel_seq(neighbor);
489                                                                 neighbor->flag |= SEQ_LEFTSEL;
490                                                                 seq->flag |= SEQ_RIGHTSEL;
491                                                         }
492                                                         break;
493                                         }
494                                 }
495                                 else {
496                                         if (extend == 0) ED_sequencer_deselect_all(scene);
497                                         select_active_side(ed->seqbasep, sel_side, seq->machine, seq->startdisp);
498                                 }
499                         }
500                         recurs_sel_seq(seq);
501
502                         if (linked_time) {
503                                 select_linked_time(ed->seqbasep, seq);
504                         }
505                 }
506         }
507         
508         /* marker transform */
509 #if 0 // XXX probably need to redo this differently for 2.5
510         if (marker) {
511                 int mval[2], xo, yo;
512 //              getmouseco_areawin(mval);
513                 xo = mval[0];
514                 yo = mval[1];
515                 
516                 while (get_mbut()) {
517 //                      getmouseco_areawin(mval);
518                         if (abs(mval[0] - xo) + abs(mval[1] - yo) > 4) {
519                                 transform_markers('g', 0);
520                                 return;
521                         }
522                 }
523         }
524 #endif
525         
526         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
527
528         /* allowing tweaks */
529         return OPERATOR_FINISHED | OPERATOR_PASS_THROUGH;
530 }
531
532 void SEQUENCER_OT_select(wmOperatorType *ot)
533 {
534         /* identifiers */
535         ot->name = "Activate/Select";
536         ot->idname = "SEQUENCER_OT_select";
537         ot->description = "Select a strip (last selected becomes the \"active strip\")";
538         
539         /* api callbacks */
540         ot->invoke = sequencer_select_invoke;
541         ot->poll = ED_operator_sequencer_active;
542         
543         /* flags */
544         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
545         
546         /* properties */
547         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the selection");
548         RNA_def_boolean(ot->srna, "linked_handle", 0, "Linked Handle", "Select handles next to the active strip");
549         /* for animation this is an enum but atm having an enum isn't useful for us */
550         RNA_def_boolean(ot->srna, "left_right", 0, "Left/Right", "Select based on the current frame side the cursor is on");
551         RNA_def_boolean(ot->srna, "linked_time", 0, "Linked Time", "Select other strips at the same time");
552 }
553
554
555
556
557 /* run recursively to select linked */
558 static int select_more_less_seq__internal(Scene *scene, int sel, int linked)
559 {
560         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
561         Sequence *seq, *neighbor;
562         int change = 0;
563         int isel;
564         
565         if (ed == NULL) return 0;
566         
567         if (sel) {
568                 sel = SELECT;
569                 isel = 0;
570         }
571         else {
572                 sel = 0;
573                 isel = SELECT;
574         }
575         
576         if (!linked) {
577                 /* if not linked we only want to touch each seq once, newseq */
578                 for (seq = ed->seqbasep->first; seq; seq = seq->next) {
579                         seq->tmp = NULL;
580                 }
581         }
582         
583         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
584                 if ((int)(seq->flag & SELECT) == sel) {
585                         if ((linked == 0 && seq->tmp) == 0) {
586                                 /* only get unselected nabours */
587                                 neighbor = find_neighboring_sequence(scene, seq, SEQ_SIDE_LEFT, isel);
588                                 if (neighbor) {
589                                         if (sel) { neighbor->flag |= SELECT; recurs_sel_seq(neighbor); }
590                                         else neighbor->flag &= ~SELECT;
591                                         if (linked == 0) neighbor->tmp = (Sequence *)1;
592                                         change = 1;
593                                 }
594                                 neighbor = find_neighboring_sequence(scene, seq, SEQ_SIDE_RIGHT, isel);
595                                 if (neighbor) {
596                                         if (sel) { neighbor->flag |= SELECT; recurs_sel_seq(neighbor); }
597                                         else neighbor->flag &= ~SELECT;
598                                         if (linked == 0) neighbor->tmp = (void *)1;
599                                         change = 1;
600                                 }
601                         }
602                 }
603         }
604         
605         return change;
606 }
607
608
609
610 /* select more operator */
611 static int sequencer_select_more_exec(bContext *C, wmOperator *UNUSED(op))
612 {
613         Scene *scene = CTX_data_scene(C);
614         
615         if (!select_more_less_seq__internal(scene, 0, 0))
616                 return OPERATOR_CANCELLED;
617
618         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
619         
620         return OPERATOR_FINISHED;
621 }
622
623 void SEQUENCER_OT_select_more(wmOperatorType *ot)
624 {
625         /* identifiers */
626         ot->name = "Select More";
627         ot->idname = "SEQUENCER_OT_select_more";
628         ot->description = "Select more strips adjacent to the current selection";
629         
630         /* api callbacks */
631         ot->exec = sequencer_select_more_exec;
632         ot->poll = sequencer_edit_poll;
633         
634         /* flags */
635         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
636         
637         /* properties */
638 }
639
640
641 /* select less operator */
642 static int sequencer_select_less_exec(bContext *C, wmOperator *UNUSED(op))
643 {
644         Scene *scene = CTX_data_scene(C);
645         
646         if (!select_more_less_seq__internal(scene, 1, 0))
647                 return OPERATOR_CANCELLED;
648  
649         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
650         
651         return OPERATOR_FINISHED;
652 }
653
654 void SEQUENCER_OT_select_less(wmOperatorType *ot)
655 {
656         /* identifiers */
657         ot->name = "Select Less";
658         ot->idname = "SEQUENCER_OT_select_less";
659         ot->description = "Shrink the current selection of adjacent selected strips";
660         
661         /* api callbacks */
662         ot->exec = sequencer_select_less_exec;
663         ot->poll = sequencer_edit_poll;
664         
665         /* flags */
666         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
667         
668         /* properties */
669 }
670
671
672 /* select pick linked operator (uses the mouse) */
673 static int sequencer_select_linked_pick_invoke(bContext *C, wmOperator *op, wmEvent *event)
674 {
675         Scene *scene = CTX_data_scene(C);
676         View2D *v2d = UI_view2d_fromcontext(C);
677         
678         short extend = RNA_boolean_get(op->ptr, "extend");
679         
680         Sequence *mouse_seq;
681         int selected, hand;
682
683         /* this works like UV, not mesh */
684         mouse_seq = find_nearest_seq(scene, v2d, &hand, event->mval);
685         if (!mouse_seq)
686                 return OPERATOR_FINISHED;  /* user error as with mesh?? */
687         
688         if (extend == 0)
689                 ED_sequencer_deselect_all(scene);
690         
691         mouse_seq->flag |= SELECT;
692         recurs_sel_seq(mouse_seq);
693         
694         selected = 1;
695         while (selected) {
696                 selected = select_more_less_seq__internal(scene, 1, 1);
697         }
698         
699         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
700         
701         return OPERATOR_FINISHED;
702 }
703
704 void SEQUENCER_OT_select_linked_pick(wmOperatorType *ot)
705 {
706         /* identifiers */
707         ot->name = "Select Pick Linked";
708         ot->idname = "SEQUENCER_OT_select_linked_pick";
709         ot->description = "Select a chain of linked strips nearest to the mouse pointer";
710         
711         /* api callbacks */
712         ot->invoke = sequencer_select_linked_pick_invoke;
713         ot->poll = ED_operator_sequencer_active;
714         
715         /* flags */
716         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
717         
718         /* properties */
719         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the selection");
720 }
721
722
723 /* select linked operator */
724 static int sequencer_select_linked_exec(bContext *C, wmOperator *UNUSED(op))
725 {
726         Scene *scene = CTX_data_scene(C);
727         int selected;
728
729         selected = 1;
730         while (selected) {
731                 selected = select_more_less_seq__internal(scene, 1, 1);
732         }
733
734         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
735
736         return OPERATOR_FINISHED;
737 }
738
739 void SEQUENCER_OT_select_linked(wmOperatorType *ot)
740 {
741         /* identifiers */
742         ot->name = "Select Linked";
743         ot->idname = "SEQUENCER_OT_select_linked";
744         ot->description = "Select all strips adjacent to the current selection";
745         
746         /* api callbacks */
747         ot->exec = sequencer_select_linked_exec;
748         ot->poll = sequencer_edit_poll;
749         
750         /* flags */
751         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
752         
753         /* properties */
754 }
755
756
757 /* select handles operator */
758 static int sequencer_select_handles_exec(bContext *C, wmOperator *op)
759 {
760         Scene *scene = CTX_data_scene(C);
761         Editing *ed = BKE_sequencer_editing_get(scene, 0);
762         Sequence *seq;
763         int sel_side = RNA_enum_get(op->ptr, "side");
764
765
766         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
767                 if (seq->flag & SELECT) {
768                         switch (sel_side) {
769                                 case SEQ_SIDE_LEFT:
770                                         seq->flag &= ~SEQ_RIGHTSEL;
771                                         seq->flag |= SEQ_LEFTSEL;
772                                         break;
773                                 case SEQ_SIDE_RIGHT:
774                                         seq->flag &= ~SEQ_LEFTSEL;
775                                         seq->flag |= SEQ_RIGHTSEL;
776                                         break;
777                                 case SEQ_SIDE_BOTH:
778                                         seq->flag |= SEQ_LEFTSEL + SEQ_RIGHTSEL;
779                                         break;
780                         }
781                 }
782         }
783
784         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
785
786         return OPERATOR_FINISHED;
787 }
788
789 void SEQUENCER_OT_select_handles(wmOperatorType *ot)
790 {
791         /* identifiers */
792         ot->name = "Select Handles";
793         ot->idname = "SEQUENCER_OT_select_handles";
794         ot->description = "Select manipulator handles on the sides of the selected strip";
795         
796         /* api callbacks */
797         ot->exec = sequencer_select_handles_exec;
798         ot->poll = sequencer_edit_poll;
799         
800         /* flags */
801         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
802         
803         /* properties */
804         RNA_def_enum(ot->srna, "side", prop_side_types, SEQ_SIDE_BOTH, "Side", "The side of the handle that is selected");
805 }
806
807 /* select side operator */
808 static int sequencer_select_active_side_exec(bContext *C, wmOperator *op)
809 {
810         Scene *scene = CTX_data_scene(C);
811         Editing *ed = BKE_sequencer_editing_get(scene, 0);
812         Sequence *seq_act = BKE_sequencer_active_get(scene);
813
814         if (ed == NULL || seq_act == NULL)
815                 return OPERATOR_CANCELLED;
816
817         seq_act->flag |= SELECT;
818
819         select_active_side(ed->seqbasep, RNA_enum_get(op->ptr, "side"), seq_act->machine, seq_act->startdisp);
820
821         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
822
823         return OPERATOR_FINISHED;
824 }
825
826 void SEQUENCER_OT_select_active_side(wmOperatorType *ot)
827 {
828         /* identifiers */
829         ot->name = "Select Active Side";
830         ot->idname = "SEQUENCER_OT_select_active_side";
831         ot->description = "Select strips on the nominated side of the active strip";
832         
833         /* api callbacks */
834         ot->exec = sequencer_select_active_side_exec;
835         ot->poll = sequencer_edit_poll;
836
837         /* flags */
838         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
839
840         /* properties */
841         RNA_def_enum(ot->srna, "side", prop_side_types, SEQ_SIDE_BOTH, "Side", "The side of the handle that is selected");
842 }
843
844
845 /* borderselect operator */
846 static int sequencer_borderselect_exec(bContext *C, wmOperator *op)
847 {
848         Scene *scene = CTX_data_scene(C);
849         Editing *ed = BKE_sequencer_editing_get(scene, FALSE);
850         View2D *v2d = UI_view2d_fromcontext(C);
851         
852         Sequence *seq;
853         rcti rect;
854         rctf rectf, rq;
855         short selecting = (RNA_int_get(op->ptr, "gesture_mode") == GESTURE_MODAL_SELECT);
856         int extend = RNA_boolean_get(op->ptr, "extend");
857         int mval[2];
858
859         if (ed == NULL)
860                 return OPERATOR_CANCELLED;
861
862         WM_operator_properties_border_to_rcti(op, &rect);
863         
864         mval[0] = rect.xmin;
865         mval[1] = rect.ymin;
866         UI_view2d_region_to_view(v2d, mval[0], mval[1], &rectf.xmin, &rectf.ymin);
867         mval[0] = rect.xmax;
868         mval[1] = rect.ymax;
869         UI_view2d_region_to_view(v2d, mval[0], mval[1], &rectf.xmax, &rectf.ymax);
870
871         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
872                 seq_rectf(seq, &rq);
873                 
874                 if (BLI_rctf_isect(&rq, &rectf, NULL)) {
875                         if (selecting) seq->flag |= SELECT;
876                         else seq->flag &= ~SEQ_ALLSEL;
877                         recurs_sel_seq(seq);
878                 }
879                 else if (!extend) {
880                         seq->flag &= ~SEQ_ALLSEL;
881                         recurs_sel_seq(seq);
882                 }
883         }
884
885         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
886
887         return OPERATOR_FINISHED;
888
889
890
891 /* ****** Border Select ****** */
892 void SEQUENCER_OT_select_border(wmOperatorType *ot)
893 {
894         /* identifiers */
895         ot->name = "Border Select";
896         ot->idname = "SEQUENCER_OT_select_border";
897         ot->description = "Enable border select mode";
898         
899         /* api callbacks */
900         ot->invoke = WM_border_select_invoke;
901         ot->exec = sequencer_borderselect_exec;
902         ot->modal = WM_border_select_modal;
903         ot->cancel = WM_border_select_cancel;
904         
905         ot->poll = ED_operator_sequencer_active;
906         
907         /* flags */
908         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
909         
910         /* rna */
911         WM_operator_properties_gesture_border(ot, TRUE);
912 }
913
914 /* ****** Selected Grouped ****** */
915
916 static EnumPropertyItem sequencer_prop_select_grouped_types[] = {
917         {1, "TYPE", 0, "Type", "Shared strip type"},
918         {2, "TYPE_BASIC", 0, "Global Type", "All strips of same basic type (Graphical or Sound)"},
919         {3, "TYPE_EFFECT", 0, "Effect Type",
920          "Shared strip effect type (if active strip is not an effect one, select all non-effect strips)"},
921         {4, "DATA", 0, "Data", "Shared data (scene, image, sound, etc.)"},
922         {5, "EFFECT", 0, "Effect", "Shared effects"},
923         {6, "EFFECT_LINK", 0, "Effect/Linked",
924          "Other strips affected by the active one (sharing some time, and below or effect-assigned)"},
925         {7, "OVERLAP", 0, "Overlap", "Overlapping time"},
926         {0, NULL, 0, NULL, NULL}
927 };
928
929 #define SEQ_IS_SOUND(_seq) ((_seq->type & SEQ_TYPE_SOUND_RAM) && !(_seq->type & SEQ_TYPE_EFFECT))
930
931 #define SEQ_IS_EFFECT(_seq) (_seq->type & SEQ_TYPE_EFFECT)
932
933 #define SEQ_USE_DATA(_seq) (ELEM3(_seq->type, SEQ_TYPE_SCENE, SEQ_TYPE_MOVIECLIP, SEQ_TYPE_MASK) || SEQ_HAS_PATH(_seq))
934
935 static short select_grouped_type(Editing *ed, Sequence *actseq)
936 {
937         Sequence *seq;
938         short changed = FALSE;
939
940         SEQP_BEGIN (ed, seq)
941         {
942                 if (seq->type == actseq->type) {
943                         seq->flag |= SELECT;
944                         changed = TRUE;
945                 }
946         }
947         SEQ_END;
948
949         return changed;
950 }
951
952 static short select_grouped_type_basic(Editing *ed, Sequence *actseq)
953 {
954         Sequence *seq;
955         short changed = FALSE;
956         short is_sound = SEQ_IS_SOUND(actseq);
957
958         SEQP_BEGIN (ed, seq)
959         {
960                 if (is_sound ? SEQ_IS_SOUND(seq) : !SEQ_IS_SOUND(seq)) {
961                         seq->flag |= SELECT;
962                         changed = TRUE;
963                 }
964         }
965         SEQ_END;
966
967         return changed;
968 }
969
970 static short select_grouped_type_effect(Editing *ed, Sequence *actseq)
971 {
972         Sequence *seq;
973         short changed = FALSE;
974         short is_effect = SEQ_IS_EFFECT(actseq);
975
976         SEQP_BEGIN (ed, seq)
977         {
978                 if (is_effect ? SEQ_IS_EFFECT(seq) : !SEQ_IS_EFFECT(seq)) {
979                         seq->flag |= SELECT;
980                         changed = TRUE;
981                 }
982         }
983         SEQ_END;
984
985         return changed;
986 }
987
988 static short select_grouped_data(Editing *ed, Sequence *actseq)
989 {
990         Sequence *seq;
991         short changed = FALSE;
992         char *dir = actseq->strip ? actseq->strip->dir : NULL;
993
994         if (!SEQ_USE_DATA(actseq))
995                 return changed;
996
997         if (SEQ_HAS_PATH(actseq) && dir) {
998                 SEQP_BEGIN (ed, seq)
999                 {
1000                         if (SEQ_HAS_PATH(seq) && seq->strip && strcmp(seq->strip->dir, dir) == 0) {
1001                                 seq->flag |= SELECT;
1002                                 changed = TRUE;
1003                         }
1004                 }
1005                 SEQ_END;
1006         }
1007         else if (actseq->type == SEQ_TYPE_SCENE) {
1008                 Scene *sce = actseq->scene;
1009                 SEQP_BEGIN (ed, seq)
1010                 {
1011                         if (seq->type == SEQ_TYPE_SCENE && seq->scene == sce) {
1012                                 seq->flag |= SELECT;
1013                                 changed = TRUE;
1014                         }
1015                 }
1016                 SEQ_END;
1017         }
1018         else if (actseq->type == SEQ_TYPE_MOVIECLIP) {
1019                 MovieClip *clip = actseq->clip;
1020                 SEQP_BEGIN (ed, seq)
1021                 {
1022                         if (seq->type == SEQ_TYPE_MOVIECLIP && seq->clip == clip) {
1023                                 seq->flag |= SELECT;
1024                                 changed = TRUE;
1025                         }
1026                 }
1027                 SEQ_END;
1028         }
1029         else if (actseq->type == SEQ_TYPE_MASK) {
1030                 struct Mask *mask = actseq->mask;
1031                 SEQP_BEGIN (ed, seq)
1032                 {
1033                         if (seq->type == SEQ_TYPE_MASK && seq->mask == mask) {
1034                                 seq->flag |= SELECT;
1035                                 changed = TRUE;
1036                         }
1037                 }
1038                 SEQ_END;
1039         }
1040
1041         return changed;
1042 }
1043
1044 static short select_grouped_effect(Editing *ed, Sequence *actseq)
1045 {
1046         Sequence *seq;
1047         short changed = FALSE;
1048         short effects[SEQ_TYPE_EFFECT_MAX + 1];
1049         int i;
1050
1051         for (i = 0; i <= SEQ_TYPE_EFFECT_MAX; i++)
1052                 effects[i] = FALSE;
1053
1054         SEQP_BEGIN (ed, seq)
1055         {
1056                 if (ELEM3(actseq, seq->seq1, seq->seq2, seq->seq3)) {
1057                         effects[seq->type] = TRUE;
1058                 }
1059         }
1060         SEQ_END;
1061
1062         SEQP_BEGIN (ed, seq)
1063         {
1064                 if (effects[seq->type]) {
1065                         if (seq->seq1) seq->seq1->flag |= SELECT;
1066                         if (seq->seq2) seq->seq2->flag |= SELECT;
1067                         if (seq->seq3) seq->seq3->flag |= SELECT;
1068                         changed = TRUE;
1069                 }
1070         }
1071         SEQ_END;
1072
1073         return changed;
1074 }
1075
1076 static short select_grouped_time_overlap(Editing *ed, Sequence *actseq)
1077 {
1078         Sequence *seq;
1079         short changed = FALSE;
1080
1081         SEQP_BEGIN (ed, seq)
1082         {
1083                 if (!((seq->startdisp >= actseq->enddisp) || (seq->enddisp < actseq->startdisp))) {
1084                         seq->flag |= SELECT;
1085                         changed = TRUE;
1086                 }
1087         }
1088         SEQ_END;
1089
1090         return changed;
1091 }
1092
1093 static short select_grouped_effect_link(Editing *ed, Sequence *actseq)
1094 {
1095         Sequence *seq = NULL;
1096         short changed = FALSE;
1097         short is_audio = ((actseq->type == SEQ_TYPE_META) || SEQ_IS_SOUND(actseq));
1098         int startdisp = actseq->startdisp;
1099         int enddisp   = actseq->enddisp;
1100         int machine   = actseq->machine;
1101         SeqIterator iter;
1102
1103         SEQP_BEGIN (ed, seq)
1104         {
1105                 seq->tmp = NULL;
1106         }
1107         SEQ_END;
1108
1109         actseq->tmp = SET_INT_IN_POINTER(TRUE);
1110
1111         for (BKE_sequence_iterator_begin(ed, &iter, TRUE); iter.valid; BKE_sequence_iterator_next(&iter)) {
1112                 seq = iter.seq;
1113
1114                 /* Ignore all seqs already selected! */
1115                 /* Ignore all seqs not sharing some time with active one. */
1116                 /* Ignore all seqs of incompatible types (audio vs video). */
1117                 if ((seq->flag & SELECT) || (seq->startdisp >= enddisp) || (seq->enddisp < startdisp) ||
1118                     (!is_audio && SEQ_IS_SOUND(seq)) ||
1119                     (is_audio && !((seq->type == SEQ_TYPE_META) || SEQ_IS_SOUND(seq))))
1120                 {
1121                         continue;
1122                 }
1123
1124                 /* If the seq is an effect one, we need extra cheking! */
1125                 if (SEQ_IS_EFFECT(seq) && ((seq->seq1 && seq->seq1->tmp) ||
1126                                            (seq->seq2 && seq->seq2->tmp) ||
1127                                            (seq->seq3 && seq->seq3->tmp)))
1128                 {
1129                         if (startdisp > seq->startdisp) startdisp = seq->startdisp;
1130                         if (enddisp < seq->enddisp) enddisp = seq->enddisp;
1131                         if (machine < seq->machine) machine = seq->machine;
1132
1133                         seq->tmp = SET_INT_IN_POINTER(TRUE);
1134
1135                         seq->flag |= SELECT;
1136                         changed = TRUE;
1137
1138                         /* Unfortunately, we must restart checks from the beginning. */
1139                         BKE_sequence_iterator_end(&iter);
1140                         BKE_sequence_iterator_begin(ed, &iter, TRUE);
1141                 }
1142
1143                 /* Video strips bellow active one, or any strip for audio (order do no matters here!). */
1144                 else if (seq->machine < machine || is_audio) {
1145                         seq->flag |= SELECT;
1146                         changed = TRUE;
1147                 }
1148         }
1149         BKE_sequence_iterator_end(&iter);
1150
1151         return changed;
1152 }
1153
1154 #undef SEQ_IS_SOUND
1155 #undef SEQ_IS_EFFECT
1156 #undef SEQ_USE_DATA
1157
1158 static int sequencer_select_grouped_exec(bContext *C, wmOperator *op)
1159 {
1160         Scene *scene  = CTX_data_scene(C);
1161         Editing *ed   = BKE_sequencer_editing_get(scene, 0);
1162         Sequence *seq, *actseq = BKE_sequencer_active_get(scene);
1163         int type = RNA_enum_get(op->ptr, "type");
1164         short changed = 0, extend;
1165
1166         extend = RNA_boolean_get(op->ptr, "extend");
1167
1168         if (actseq == NULL) {
1169                 BKE_report(op->reports, RPT_ERROR, "No active sequence!");
1170                 return OPERATOR_CANCELLED;
1171         }
1172
1173         if (extend == 0) {
1174                 SEQP_BEGIN (ed, seq)
1175                 {
1176                         seq->flag &= ~SELECT;
1177                         changed = TRUE;
1178                 }
1179                 SEQ_END;
1180         }
1181
1182         if      (type == 1) changed |= select_grouped_type(ed, actseq);
1183         else if (type == 2) changed |= select_grouped_type_basic(ed, actseq);
1184         else if (type == 3) changed |= select_grouped_type_effect(ed, actseq);
1185         else if (type == 4) changed |= select_grouped_data(ed, actseq);
1186         else if (type == 5) changed |= select_grouped_effect(ed, actseq);
1187         else if (type == 6) changed |= select_grouped_effect_link(ed, actseq);
1188         else if (type == 7) changed |= select_grouped_time_overlap(ed, actseq);
1189
1190         if (changed) {
1191                 WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
1192                 return OPERATOR_FINISHED;
1193         }
1194
1195         return OPERATOR_CANCELLED;
1196 }
1197
1198 void SEQUENCER_OT_select_grouped(wmOperatorType *ot)
1199 {
1200         /* identifiers */
1201         ot->name = "Select Grouped";
1202         ot->description = "Select all strips grouped by various properties";
1203         ot->idname = "SEQUENCER_OT_select_grouped";
1204         
1205         /* api callbacks */
1206         ot->invoke = WM_menu_invoke;
1207         ot->exec = sequencer_select_grouped_exec;
1208         ot->poll = sequencer_edit_poll;
1209         
1210         /* flags */
1211         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1212         
1213         /* properties */
1214         RNA_def_boolean(ot->srna, "extend", FALSE, "Extend", "Extend selection instead of deselecting everything first");
1215         ot->prop = RNA_def_enum(ot->srna, "type", sequencer_prop_select_grouped_types, 0, "Type", "");
1216 }
1217