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