add missing GPL information in guicast program files
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / editlength.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2009 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "bcdisplayinfo.h"
23 #include "clip.h"
24 #include "edit.h"
25 #include "editlength.h"
26 #include "edits.h"
27 #include "edl.h"
28 #include "edlsession.h"
29 #include "language.h"
30 #include "localsession.h"
31 #include "mwindow.h"
32 #include "mwindowgui.h"
33 #include "track.h"
34 #include "tracks.h"
35
36
37 EditLengthThread::EditLengthThread(MWindow *mwindow)
38  : BC_DialogThread()
39 {
40         this->mwindow = mwindow;
41 }
42
43 EditLengthThread::~EditLengthThread()
44 {
45         close_window();
46 }
47
48 void EditLengthThread::start(Edit *edit)
49 {
50         this->edit = edit;
51
52         orig_length = 0;
53         length = 0;
54
55         double start = mwindow->edl->local_session->get_selectionstart();
56         double end = mwindow->edl->local_session->get_selectionend();
57
58 // get the default length from the first edit selected
59         if(!edit)
60         {
61                 Track *track = 0;
62                 int got_it = 0;
63                 for(track = mwindow->edl->tracks->first;
64                         track && !got_it;
65                         track = track->next)
66                 {
67                         if(track->is_armed())
68                         {
69                                 int64_t start_units = track->to_units(start, 0);
70                                 int64_t end_units = track->to_units(end, 0);
71
72                                 for(edit = track->edits->first;
73                                         edit && !got_it;
74                                         edit = edit->next)
75                                 {
76                                         if(edit->startproject >= start_units &&
77                                                 edit->startproject < end_units)
78                                         {
79                                                 this->length =
80                                                         this->orig_length =
81                                                         track->from_units(edit->length);
82                                                 got_it = 1;
83                                         }
84                                 }
85                         }
86                 }
87         }
88
89         BC_DialogThread::start();
90 }
91
92 #define ELW_W xS(300)
93 #define ELW_H yS(100)
94
95 BC_Window* EditLengthThread::new_gui()
96 {
97         BC_DisplayInfo display_info;
98         int x = display_info.get_abs_cursor_x() - ELW_W/2;
99         int y = display_info.get_abs_cursor_y() - ELW_H/2;
100         EditLengthDialog *gui = new EditLengthDialog(mwindow, this, x, y);
101         gui->create_objects();
102         return gui;
103 }
104
105 void EditLengthThread::handle_close_event(int result)
106 {
107         if(!result) {
108                 if(edit) {
109 //                      mwindow->set_edit_length(edit, length);
110                 }
111                 else {
112                         mwindow->set_edit_length(length);
113                 }
114         }
115 }
116
117
118 EditLengthDialog::EditLengthDialog(MWindow *mwindow,
119         EditLengthThread *thread,
120         int x,
121         int y)
122  : BC_Window(_(PROGRAM_NAME ": Edit length"), x, y,
123         ELW_W, ELW_H, -1, -1, 0, 0, 1)
124 {
125         this->mwindow = mwindow;
126         this->thread = thread;
127 // *** CONTEXT_HELP ***
128         context_help_set_keyword("Edit Length");
129 }
130
131 EditLengthDialog::~EditLengthDialog()
132 {
133 }
134
135
136 void EditLengthDialog::create_objects()
137 {
138         int xs10 = xS(10), xs100 = xS(100);
139         int ys10 = yS(10);
140         lock_window("EditLengthDialog::create_objects");
141         add_subwindow(new BC_Title(xs10, ys10, _("Seconds:")));
142         text = new EditLengthText(mwindow, this, xs100, ys10);
143         text->create_objects();
144         add_subwindow(new BC_OKButton(this));
145         add_subwindow(new BC_CancelButton(this));
146         show_window();
147         unlock_window();
148 }
149
150 int EditLengthDialog::close_event()
151 {
152         set_done(0);
153         return 1;
154 }
155
156
157 EditLengthText::EditLengthText(MWindow *mwindow,
158         EditLengthDialog *gui, int x, int y)
159  : BC_TumbleTextBox(gui, (float)gui->thread->length,
160         0.f, 100.f, x, y, xS(100))
161 {
162         this->mwindow = mwindow;
163         this->gui = gui;
164 }
165
166 int EditLengthText::handle_event()
167 {
168         double result = atof(get_text());
169         if(fabs(result - gui->thread->length) > 0.000001)
170         {
171                 gui->thread->length = result;
172         }
173
174         return 1;
175 }
176