2447ca6cfbeb103b0a442d577ec9a6d91fa3f162
[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
62 #include "UI_view2d.h"
63
64 /* own include */
65 #include "sequencer_intern.h"
66 static void *find_nearest_marker(int UNUSED(d1), int UNUSED(d2))
67 {
68         return NULL;
69 }
70         
71 static void select_surrounding_handles(Scene *scene, Sequence *test) /* XXX BRING BACK */
72 {
73         Sequence *neighbor;
74         
75         neighbor = find_neighboring_sequence(scene, test, SEQ_SIDE_LEFT, -1);
76         if (neighbor) {
77                 neighbor->flag |= SELECT;
78                 recurs_sel_seq(neighbor);
79                 neighbor->flag |= SEQ_RIGHTSEL;
80         }
81         neighbor = find_neighboring_sequence(scene, test, SEQ_SIDE_RIGHT, -1);
82         if (neighbor) {
83                 neighbor->flag |= SELECT;
84                 recurs_sel_seq(neighbor);
85                 neighbor->flag |= SEQ_LEFTSEL;
86         }
87         test->flag |= SELECT;
88 }
89
90 /* used for mouse selection and for SEQUENCER_OT_select_active_side() */
91 static void select_active_side(ListBase *seqbase, int sel_side, int channel, int frame)
92 {
93         Sequence *seq;
94
95         for (seq = seqbase->first; seq; seq = seq->next) {
96                 if (channel == seq->machine) {
97                         switch (sel_side) {
98                                 case SEQ_SIDE_LEFT:
99                                         if (frame > (seq->startdisp)) {
100                                                 seq->flag &= ~(SEQ_RIGHTSEL | SEQ_LEFTSEL);
101                                                 seq->flag |= SELECT;
102                                         }
103                                         break;
104                                 case SEQ_SIDE_RIGHT:
105                                         if (frame < (seq->startdisp)) {
106                                                 seq->flag &= ~(SEQ_RIGHTSEL | SEQ_LEFTSEL);
107                                                 seq->flag |= SELECT;
108                                         }
109                                         break;
110                                 case SEQ_SIDE_BOTH:
111                                         seq->flag &= ~(SEQ_RIGHTSEL | SEQ_LEFTSEL);
112                                         break;
113                         }
114                 }
115         }
116 }
117
118 /* used for mouse selection and for SEQUENCER_OT_select_active_side() */
119 static void select_linked_time(ListBase *seqbase, Sequence *seq_link)
120 {
121         Sequence *seq;
122
123         for (seq = seqbase->first; seq; seq = seq->next) {
124                 if (seq_link->machine != seq->machine) {
125                         int left_match = (seq->startdisp == seq_link->startdisp) ? 1 : 0;
126                         int right_match = (seq->enddisp == seq_link->enddisp) ? 1 : 0;
127
128                         if (left_match && right_match) {
129                                 /* a direct match, copy the selection settinhs */
130                                 seq->flag &= ~(SELECT | SEQ_LEFTSEL | SEQ_RIGHTSEL);
131                                 seq->flag |= seq_link->flag & (SELECT | SEQ_LEFTSEL | SEQ_RIGHTSEL);
132
133                                 recurs_sel_seq(seq);
134                         }
135                         else if (seq_link->flag & SELECT && (left_match || right_match)) {
136
137                                 /* clear for reselection */
138                                 seq->flag &= ~(SEQ_LEFTSEL | SEQ_RIGHTSEL);
139
140                                 if (left_match && seq_link->flag & SEQ_LEFTSEL)
141                                         seq->flag |= SELECT | SEQ_LEFTSEL;
142
143                                 if (right_match && seq_link->flag & SEQ_RIGHTSEL)
144                                         seq->flag |= SELECT | SEQ_RIGHTSEL;
145
146                                 recurs_sel_seq(seq);
147                         }
148                 }
149         }
150 }
151
152 #if 0 // BRING BACK
153 void select_surround_from_last(Scene *scene)
154 {
155         Sequence *seq = get_last_seq(scene);
156         
157         if (seq == NULL)
158                 return;
159         
160         select_surrounding_handles(scene, seq);
161 }
162 #endif
163
164
165 static void UNUSED_FUNCTION(select_single_seq) (Scene * scene, Sequence * seq, int deselect_all) /* BRING BACK */
166 {
167         Editing *ed = seq_give_editing(scene, FALSE);
168         
169         if (deselect_all)
170                 deselect_all_seq(scene);
171         seq_active_set(scene, seq);
172
173         if ((seq->type == SEQ_IMAGE) || (seq->type == SEQ_MOVIE)) {
174                 if (seq->strip)
175                         BLI_strncpy(ed->act_imagedir, seq->strip->dir, FILE_MAXDIR);
176         }
177         else if (seq->type == SEQ_SOUND) {
178                 if (seq->strip)
179                         BLI_strncpy(ed->act_sounddir, seq->strip->dir, FILE_MAXDIR);
180         }
181         seq->flag |= SELECT;
182         recurs_sel_seq(seq);
183 }
184
185 #if 0
186 static void select_neighbor_from_last(Scene *scene, int lr)
187 {
188         Sequence *seq = seq_active_get(scene);
189         Sequence *neighbor;
190         int change = 0;
191         if (seq) {
192                 neighbor = find_neighboring_sequence(scene, seq, lr, -1);
193                 if (neighbor) {
194                         switch (lr) {
195                                 case SEQ_SIDE_LEFT:
196                                         neighbor->flag |= SELECT;
197                                         recurs_sel_seq(neighbor);
198                                         neighbor->flag |= SEQ_RIGHTSEL;
199                                         seq->flag |= SEQ_LEFTSEL;
200                                         break;
201                                 case SEQ_SIDE_RIGHT:
202                                         neighbor->flag |= SELECT;
203                                         recurs_sel_seq(neighbor);
204                                         neighbor->flag |= SEQ_LEFTSEL;
205                                         seq->flag |= SEQ_RIGHTSEL;
206                                         break;
207                         }
208                         seq->flag |= SELECT;
209                         change = 1;
210                 }
211         }
212         if (change) {
213         }
214 }
215 #endif
216
217 /* (de)select operator */
218 static int sequencer_de_select_all_exec(bContext *C, wmOperator *op)
219 {
220         int action = RNA_enum_get(op->ptr, "action");
221
222         Scene *scene = CTX_data_scene(C);
223         Editing *ed = seq_give_editing(scene, FALSE);
224         Sequence *seq;
225
226         if (action == SEL_TOGGLE) {
227                 action = SEL_SELECT;
228                 for (seq = ed->seqbasep->first; seq; seq = seq->next) {
229                         if (seq->flag & SEQ_ALLSEL) {
230                                 action = SEL_DESELECT;
231                                 break;
232                         }
233                 }
234         }
235
236         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
237                 switch (action) {
238                         case SEL_SELECT:
239                                 seq->flag &= ~(SEQ_LEFTSEL + SEQ_RIGHTSEL);
240                                 seq->flag |= SELECT;
241                                 break;
242                         case SEL_DESELECT:
243                                 seq->flag &= ~SEQ_ALLSEL;
244                                 break;
245                         case SEL_INVERT:
246                                 if (seq->flag & SEQ_ALLSEL) {
247                                         seq->flag &= ~SEQ_ALLSEL;
248                                 }
249                                 else {
250                                         seq->flag &= ~(SEQ_LEFTSEL + SEQ_RIGHTSEL);
251                                         seq->flag |= SELECT;
252                                 }
253                                 break;
254                 }
255         }
256
257         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
258
259         return OPERATOR_FINISHED;
260 }
261
262 void SEQUENCER_OT_select_all(struct wmOperatorType *ot)
263 {
264         /* identifiers */
265         ot->name = "(De)select All";
266         ot->idname = "SEQUENCER_OT_select_all";
267         ot->description = "Select or deselect all strips";
268         
269         /* api callbacks */
270         ot->exec = sequencer_de_select_all_exec;
271         ot->poll = sequencer_edit_poll;
272         
273         /* flags */
274         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
275
276         WM_operator_properties_select_all(ot);
277 }
278
279
280 /* (de)select operator */
281 static int sequencer_select_inverse_exec(bContext *C, wmOperator *UNUSED(op))
282 {
283         Scene *scene = CTX_data_scene(C);
284         Editing *ed = seq_give_editing(scene, FALSE);
285         Sequence *seq;
286
287         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
288                 if (seq->flag & SELECT) {
289                         seq->flag &= ~SEQ_ALLSEL;
290                 }
291                 else {
292                         seq->flag &= ~(SEQ_LEFTSEL + SEQ_RIGHTSEL);
293                         seq->flag |= SELECT;
294                 }
295         }
296
297         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
298         
299         return OPERATOR_FINISHED;
300 }
301
302 void SEQUENCER_OT_select_inverse(struct wmOperatorType *ot)
303 {
304         /* identifiers */
305         ot->name = "Select Inverse";
306         ot->idname = "SEQUENCER_OT_select_inverse";
307         ot->description = "Select unselected strips";
308         
309         /* api callbacks */
310         ot->exec = sequencer_select_inverse_exec;
311         ot->poll = sequencer_edit_poll;
312         
313         /* flags */
314         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
315 }
316
317 static int sequencer_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
318 {
319         View2D *v2d = UI_view2d_fromcontext(C);
320         Scene *scene = CTX_data_scene(C);
321         Editing *ed = seq_give_editing(scene, FALSE);
322         short extend = RNA_boolean_get(op->ptr, "extend");
323         short linked_handle = RNA_boolean_get(op->ptr, "linked_handle");
324         short left_right = RNA_boolean_get(op->ptr, "left_right");
325         short linked_time = RNA_boolean_get(op->ptr, "linked_time");
326         
327         Sequence *seq, *neighbor, *act_orig;
328         int hand, sel_side;
329         TimeMarker *marker;
330
331         if (ed == NULL)
332                 return OPERATOR_CANCELLED;
333         
334         marker = find_nearest_marker(SCE_MARKERS, 1); //XXX - dummy function for now
335         
336         seq = find_nearest_seq(scene, v2d, &hand, event->mval);
337
338         // XXX - not nice, Ctrl+RMB needs to do left_right only when not over a strip
339         if (seq && linked_time && left_right)
340                 left_right = FALSE;
341
342
343         if (marker) {
344                 int oldflag;
345                 /* select timeline marker */
346                 if (extend) {
347                         oldflag = marker->flag;
348                         if (oldflag & SELECT)
349                                 marker->flag &= ~SELECT;
350                         else
351                                 marker->flag |= SELECT;
352                 }
353                 else {
354                         /* deselect_markers(0, 0); */ /* XXX, in 2.4x, seq selection used to deselect all, need to re-thnik this for 2.5 */
355                         marker->flag |= SELECT;                         
356                 }
357                 
358         }
359         else if (left_right) {
360                 /* use different logic for this */
361                 float x;
362                 deselect_all_seq(scene);
363                 UI_view2d_region_to_view(v2d, event->mval[0], event->mval[1], &x, NULL);
364
365                 SEQP_BEGIN(ed, seq)
366                 {
367                         if (x < CFRA) {
368                                 if (seq->enddisp < CFRA) {
369                                         seq->flag |= SELECT;
370                                         recurs_sel_seq(seq);
371                                 }
372                         }
373                         else {
374                                 if (seq->startdisp > CFRA) {
375                                         seq->flag |= SELECT;
376                                         recurs_sel_seq(seq);
377                                 }
378                         }
379                 }
380                 SEQ_END
381                 
382                 {
383                         SpaceSeq *sseq = CTX_wm_space_seq(C);
384                         if (sseq && sseq->flag & SEQ_MARKER_TRANS) {
385                                 TimeMarker *tmarker;
386
387                                 for (tmarker = scene->markers.first; tmarker; tmarker = tmarker->next) {
388                                         if (((x <  CFRA) && tmarker->frame <  CFRA) ||
389                                             ((x >= CFRA) && tmarker->frame >= CFRA))
390                                         {
391                                                 tmarker->flag |= SELECT;
392                                         }
393                                         else {
394                                                 tmarker->flag &= ~SELECT;
395                                         }
396                                 }
397                         }
398                 }
399         }
400         else {
401                 // seq= find_nearest_seq(scene, v2d, &hand, mval);
402
403                 act_orig = ed->act_seq;
404
405                 if (extend == 0 && linked_handle == 0)
406                         deselect_all_seq(scene);
407         
408                 if (seq) {
409                         seq_active_set(scene, seq);
410         
411                         if ((seq->type == SEQ_IMAGE) || (seq->type == SEQ_MOVIE)) {
412                                 if (seq->strip) {
413                                         BLI_strncpy(ed->act_imagedir, seq->strip->dir, FILE_MAXDIR);
414                                 }
415                         }
416                         else
417                         if (seq->type == SEQ_SOUND) {
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) deselect_all_seq(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) deselect_all_seq(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) deselect_all_seq(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) deselect_all_seq(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) deselect_all_seq(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) deselect_all_seq(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 = seq_give_editing(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                 deselect_all_seq(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 = seq_give_editing(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 = seq_give_editing(scene, 0);
812         Sequence *seq_act = seq_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 = seq_give_editing(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         rect.xmin = RNA_int_get(op->ptr, "xmin");
863         rect.ymin = RNA_int_get(op->ptr, "ymin");
864         rect.xmax = RNA_int_get(op->ptr, "xmax");
865         rect.ymax = RNA_int_get(op->ptr, "ymax");
866         
867         mval[0] = rect.xmin;
868         mval[1] = rect.ymin;
869         UI_view2d_region_to_view(v2d, mval[0], mval[1], &rectf.xmin, &rectf.ymin);
870         mval[0] = rect.xmax;
871         mval[1] = rect.ymax;
872         UI_view2d_region_to_view(v2d, mval[0], mval[1], &rectf.xmax, &rectf.ymax);
873
874         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
875                 seq_rectf(seq, &rq);
876                 
877                 if (BLI_isect_rctf(&rq, &rectf, NULL)) {
878                         if (selecting) seq->flag |= SELECT;
879                         else seq->flag &= ~SEQ_ALLSEL;
880                         recurs_sel_seq(seq);
881                 }
882                 else if (!extend) {
883                         seq->flag &= ~SEQ_ALLSEL;
884                         recurs_sel_seq(seq);
885                 }
886         }
887
888         WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
889
890         return OPERATOR_FINISHED;
891
892
893
894 /* ****** Border Select ****** */
895 void SEQUENCER_OT_select_border(wmOperatorType *ot)
896 {
897         /* identifiers */
898         ot->name = "Border Select";
899         ot->idname = "SEQUENCER_OT_select_border";
900         ot->description = "Enable border select mode";
901         
902         /* api callbacks */
903         ot->invoke = WM_border_select_invoke;
904         ot->exec = sequencer_borderselect_exec;
905         ot->modal = WM_border_select_modal;
906         ot->cancel = WM_border_select_cancel;
907         
908         ot->poll = ED_operator_sequencer_active;
909         
910         /* flags */
911         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
912         
913         /* rna */
914         WM_operator_properties_gesture_border(ot, TRUE);
915 }
916
917 /* ****** Selected Grouped ****** */
918
919 static EnumPropertyItem sequencer_prop_select_grouped_types[] = {
920         {1, "TYPE", 0, "Type", "Shared strip type"},
921         {2, "TYPE_BASIC", 0, "Global Type", "All strips of same basic type (Graphical or Sound)"},
922         {3, "TYPE_EFFECT", 0, "Effect Type",
923          "Shared strip effect type (if active strip is not an effect one, select all non-effect strips)"},
924         {4, "DATA", 0, "Data", "Shared data (scene, image, sound, etc.)"},
925         {5, "EFFECT", 0, "Effect", "Shared effects"},
926         {6, "EFFECT_LINK", 0, "Effect/Linked",
927          "Other strips affected by the active one (sharing some time, and below or effect-assigned)"},
928         {7, "OVERLAP", 0, "Overlap", "Overlapping time"},
929         {0, NULL, 0, NULL, NULL}
930 };
931
932 #define SEQ_IS_SOUND(_seq) ((_seq->type & SEQ_SOUND) && !(_seq->type & SEQ_EFFECT))
933
934 #define SEQ_IS_EFFECT(_seq) (_seq->type & SEQ_EFFECT)
935
936 #define SEQ_USE_DATA(_seq) (ELEM(_seq->type, SEQ_SCENE, SEQ_MOVIECLIP) || SEQ_HAS_PATH(_seq))
937
938 static short select_grouped_type(Editing *ed, Sequence *actseq)
939 {
940         Sequence *seq;
941         short changed = FALSE;
942
943         SEQP_BEGIN(ed, seq) {
944                 if (seq->type == actseq->type) {
945                         seq->flag |= SELECT;
946                         changed = TRUE;
947                 }
948         }
949         SEQ_END;
950
951         return changed;
952 }
953
954 static short select_grouped_type_basic(Editing *ed, Sequence *actseq)
955 {
956         Sequence *seq;
957         short changed = FALSE;
958         short is_sound = SEQ_IS_SOUND(actseq);
959
960         SEQP_BEGIN(ed, seq) {
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                 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                         if (SEQ_HAS_PATH(seq) && seq->strip && strcmp(seq->strip->dir, dir) == 0) {
1000                                 seq->flag |= SELECT;
1001                                 changed = TRUE;
1002                         }
1003                 }
1004                 SEQ_END;
1005         }
1006         else if (actseq->type == SEQ_SCENE) {
1007                 Scene *sce = actseq->scene;
1008                 SEQP_BEGIN(ed, seq) {
1009                         if (seq->type == SEQ_SCENE && seq->scene == sce) {
1010                                 seq->flag |= SELECT;
1011                                 changed = TRUE;
1012                         }
1013                 }
1014                 SEQ_END;
1015         }
1016         else if (actseq->type == SEQ_MOVIECLIP) {
1017                 MovieClip *clip = actseq->clip;
1018                 SEQP_BEGIN(ed, seq) {
1019                         if (seq->type == SEQ_MOVIECLIP && seq->clip == clip) {
1020                                 seq->flag |= SELECT;
1021                                 changed = TRUE;
1022                         }
1023                 }
1024                 SEQ_END;
1025         }
1026
1027         return changed;
1028 }
1029
1030 static short select_grouped_effect(Editing *ed, Sequence *actseq)
1031 {
1032         Sequence *seq;
1033         short changed = FALSE;
1034         short effects[SEQ_EFFECT_MAX + 1];
1035         int i;
1036
1037         for (i = 0; i <= SEQ_EFFECT_MAX; i++)
1038                 effects[i] = FALSE;
1039
1040         SEQP_BEGIN(ed, seq) {
1041                 if (ELEM3(actseq, seq->seq1, seq->seq2, seq->seq3)) {
1042                         effects[seq->type] = TRUE;
1043                 }
1044         }
1045         SEQ_END;
1046
1047         SEQP_BEGIN(ed, seq) {
1048                 if (effects[seq->type]) {
1049                         if (seq->seq1) seq->seq1->flag |= SELECT;
1050                         if (seq->seq2) seq->seq2->flag |= SELECT;
1051                         if (seq->seq3) seq->seq3->flag |= SELECT;
1052                         changed = TRUE;
1053                 }
1054         }
1055         SEQ_END;
1056
1057         return changed;
1058 }
1059
1060 static short select_grouped_time_overlap(Editing *ed, Sequence *actseq)
1061 {
1062         Sequence *seq;
1063         short changed = FALSE;
1064
1065         SEQP_BEGIN(ed, seq) {
1066                 if (!((seq->startdisp >= actseq->enddisp) || (seq->enddisp < actseq->startdisp))) {
1067                         seq->flag |= SELECT;
1068                         changed = TRUE;
1069                 }
1070         }
1071         SEQ_END;
1072
1073         return changed;
1074 }
1075
1076 static short select_grouped_effect_link(Editing *ed, Sequence *actseq)
1077 {
1078         Sequence *seq = NULL;
1079         short changed = FALSE;
1080         short is_audio = ((actseq->type == SEQ_META) || SEQ_IS_SOUND(actseq));
1081         int startdisp = actseq->startdisp;
1082         int enddisp   = actseq->enddisp;
1083         int machine   = actseq->machine;
1084         SeqIterator iter;
1085
1086         SEQP_BEGIN(ed, seq) {
1087                 seq->tmp= NULL;
1088         }
1089         SEQ_END;
1090
1091         actseq->tmp = SET_INT_IN_POINTER(TRUE);
1092
1093         for (seq_begin(ed, &iter, 1); iter.valid; seq_next(&iter)) {
1094                 seq = iter.seq;
1095
1096                 /* Ignore all seqs already selected! */
1097                 /* Ignore all seqs not sharing some time with active one. */
1098                 /* Ignore all seqs of incompatible types (audio vs video). */
1099                 if ((seq->flag & SELECT) || (seq->startdisp >= enddisp) || (seq->enddisp < startdisp) ||
1100                     (!is_audio && SEQ_IS_SOUND(seq)) ||
1101                     (is_audio && !((seq->type == SEQ_META) || SEQ_IS_SOUND(seq))))
1102                 {
1103                         continue;
1104                 }
1105
1106                 /* If the seq is an effect one, we need extra cheking! */
1107                 if (SEQ_IS_EFFECT(seq) && ((seq->seq1 && seq->seq1->tmp) ||
1108                                            (seq->seq2 && seq->seq2->tmp) ||
1109                                            (seq->seq3 && seq->seq3->tmp)))
1110                 {
1111                         if (startdisp > seq->startdisp) startdisp = seq->startdisp;
1112                         if (enddisp < seq->enddisp) enddisp = seq->enddisp;
1113                         if (machine < seq->machine) machine = seq->machine;
1114
1115                         seq->tmp = SET_INT_IN_POINTER(TRUE);
1116
1117                         seq->flag |= SELECT;
1118                         changed = TRUE;
1119
1120                         /* Unfortunately, we must restart checks from the beginning. */
1121                         seq_end(&iter);
1122                         seq_begin(ed, &iter, 1);
1123                 }
1124
1125                 /* Video strips bellow active one, or any strip for audio (order do no matters here!). */
1126                 else if (seq->machine < machine || is_audio) {
1127                         seq->flag |= SELECT;
1128                         changed = TRUE;
1129                 }
1130         }
1131         seq_end(&iter);
1132
1133         return changed;
1134 }
1135
1136 #undef SEQ_IS_SOUND
1137 #undef SEQ_IS_EFFECT
1138 #undef SEQ_USE_DATA
1139
1140 static int sequencer_select_grouped_exec(bContext *C, wmOperator *op)
1141 {
1142         Scene *scene  = CTX_data_scene(C);
1143         Editing *ed   = seq_give_editing(scene, 0);
1144         Sequence *seq, *actseq = seq_active_get(scene);
1145         int type = RNA_enum_get(op->ptr, "type");
1146         short changed = 0, extend;
1147
1148         extend = RNA_boolean_get(op->ptr, "extend");
1149
1150         if (actseq == NULL) {
1151                 BKE_report(op->reports, RPT_ERROR, "No Active Sequence!");
1152                 return OPERATOR_CANCELLED;
1153         }
1154
1155         if (extend == 0) {
1156                 SEQP_BEGIN(ed, seq) {
1157                         seq->flag &= ~SELECT;
1158                         changed = TRUE;
1159                 }
1160                 SEQ_END;
1161         }
1162
1163         if      (type == 1) changed |= select_grouped_type(ed, actseq);
1164         else if (type == 2) changed |= select_grouped_type_basic(ed, actseq);
1165         else if (type == 3) changed |= select_grouped_type_effect(ed, actseq);
1166         else if (type == 4) changed |= select_grouped_data(ed, actseq);
1167         else if (type == 5) changed |= select_grouped_effect(ed, actseq);
1168         else if (type == 6) changed |= select_grouped_effect_link(ed, actseq);
1169         else if (type == 7) changed |= select_grouped_time_overlap(ed, actseq);
1170
1171         if (changed) {
1172                 WM_event_add_notifier(C, NC_SCENE | ND_SEQUENCER | NA_SELECTED, scene);
1173                 return OPERATOR_FINISHED;
1174         }
1175
1176         return OPERATOR_CANCELLED;
1177 }
1178
1179 void SEQUENCER_OT_select_grouped(wmOperatorType *ot)
1180 {
1181         /* identifiers */
1182         ot->name = "Select Grouped";
1183         ot->description = "Select all strips grouped by various properties";
1184         ot->idname = "SEQUENCER_OT_select_grouped";
1185         
1186         /* api callbacks */
1187         ot->invoke = WM_menu_invoke;
1188         ot->exec = sequencer_select_grouped_exec;
1189         ot->poll = sequencer_edit_poll;
1190         
1191         /* flags */
1192         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1193         
1194         /* properties */
1195         RNA_def_boolean(ot->srna, "extend", FALSE, "Extend", "Extend selection instead of deselecting everything first");
1196         ot->prop = RNA_def_enum(ot->srna, "type", sequencer_prop_select_grouped_types, 0, "Type", "");
1197 }
1198