add Autosave continuous backups by Andras Reuss and Andrew-R
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / manualgoto.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2004 Andraz Tori
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 "editpanel.h"
24 #include "edl.h"
25 #include "edlsession.h"
26 #include "keys.h"
27 #include "language.h"
28 #include "manualgoto.h"
29 #include "mwindow.h"
30 #include "mwindowgui.h"
31 #include "theme.h"
32
33 #define MGT_W xS(285)
34 #define MGT_H yS(80)
35
36 ManualGoto::ManualGoto(MWindow *mwindow, EditPanel *panel)
37  : BC_DialogThread()
38 {
39         this->mwindow = mwindow;
40         this->panel = panel;
41         window = 0;
42 }
43
44 ManualGoto::~ManualGoto()
45 {
46         close_window();
47 }
48
49 BC_Window *ManualGoto::new_gui()
50 {
51         BC_DisplayInfo dpy_info;
52         int x = dpy_info.get_abs_cursor_x() - MGT_W / 2;
53         int y = dpy_info.get_abs_cursor_y() - MGT_H / 2;
54         window = new ManualGotoWindow(this, x, y);
55         window->create_objects();
56         window->show_window();
57         return window;
58 }
59
60 void ManualGoto::handle_done_event(int result)
61 {
62         if( result ) return;
63         double current_position = panel->get_position();
64         const char *text = window->time_text->get_text();
65         double new_position = Units::text_to_seconds(text,
66                         mwindow->edl->session->sample_rate,
67                         window->time_format,
68                         mwindow->edl->session->frame_rate,
69                         mwindow->edl->session->frames_per_foot);
70         char modifier = window->direction->get_text()[0];
71         switch( modifier ) {
72         case '+':  new_position = current_position + new_position;  break;
73         case '-':  new_position = current_position - new_position;  break;
74         default: break;
75         }
76         panel->subwindow->lock_window("ManualGoto::handle_done_event");
77         panel->set_position(new_position);
78         panel->subwindow->unlock_window();
79 }
80
81 ManualGotoWindow::ManualGotoWindow(ManualGoto *mango, int x, int y)
82  : BC_Window(_(PROGRAM_NAME ": Goto position"), x, y,
83         MGT_W, MGT_H, MGT_W, MGT_H, 0, 0, 1)
84 {
85         this->mango = mango;
86 }
87
88 ManualGotoWindow::~ManualGotoWindow()
89 {
90 }
91
92
93 ManualGotoText::ManualGotoText(ManualGotoWindow *window, int x, int y, int w)
94  : BC_TextBox(x, y, w, 1, "")
95 {
96         this->window = window;
97 }
98
99 int ManualGotoText::keypress_event()
100 {
101         int key = get_keypress();
102         if( key == '+' || key == '-' || key == '=' ) {
103                 char text[2];  text[0] = key;  text[1] = 0;
104                 window->direction->set_text(text);
105                 return 1;
106         }
107         if( key == RETURN ) {
108                 unlock_window();
109                 window->mango->handle_done_event(0);
110                 lock_window("ManualGotoText::handle_event");
111                 return 1;
112         }
113         return BC_TextBox::keypress_event();
114 }
115
116 void ManualGotoWindow::update(double position)
117 {
118         MWindow *mwindow = mango->mwindow;
119         format_text->update(Units::timetype_toformat(time_format));
120         char string[BCSTRLEN];
121         Units::totext(string, position, time_format,
122                 mwindow->edl->session->sample_rate,
123                 mwindow->edl->session->frame_rate,
124                 mwindow->edl->session->frames_per_foot,
125                 mwindow->get_timecode_offset());
126         time_text->update(string);
127 }
128
129 void ManualGotoWindow::update()
130 {
131         double position = mango->panel->get_position();
132         update(position);
133 }
134
135 ManualGotoKeyItem::ManualGotoKeyItem(ManualGotoDirection *popup,
136                 const char *text, const char *htxt)
137  : BC_MenuItem(text, htxt, htxt[0])
138 {
139         this->popup = popup;
140         this->htxt = htxt;
141 }
142
143 int ManualGotoKeyItem::handle_event()
144 {
145         popup->set_text(htxt);
146         return 1;
147 }
148
149 ManualGotoDirection::ManualGotoDirection(ManualGotoWindow *window,
150         int x, int y, int w)
151  : BC_PopupMenu(x, y, w, "=", -1, 0, 1)
152 {
153         this->window = window;
154 }
155
156 void ManualGotoDirection::create_objects()
157 {
158         add_item(new ManualGotoKeyItem(this, _("Forward"),  "+"));
159         add_item(new ManualGotoKeyItem(this, _("Position"), "="));
160         add_item(new ManualGotoKeyItem(this, _("Reverse"),  "-"));
161 }
162
163 ManualGotoUnitItem::ManualGotoUnitItem(ManualGotoUnits *popup, int type)
164  : BC_MenuItem(Units::timetype_toformat(type))
165 {
166         this->popup = popup;
167         this->type = type;
168 }
169
170 int ManualGotoUnitItem::handle_event()
171 {
172         popup->window->time_format = type;
173         popup->window->update();
174         return 1;
175 }
176
177 ManualGotoUnits::ManualGotoUnits(ManualGotoWindow *window, int x, int y, int w)
178  : BC_PopupMenu(x, y, w, "", 1, 0, 0)
179 {
180         this->window = window;
181 }
182
183 void ManualGotoUnits::create_objects()
184 {
185         add_item(new ManualGotoUnitItem(this, TIME_HMS));
186         add_item(new ManualGotoUnitItem(this, TIME_HMSF));
187         add_item(new ManualGotoUnitItem(this, TIME_TIMECODE));
188         add_item(new ManualGotoUnitItem(this, TIME_FRAMES));
189         add_item(new ManualGotoUnitItem(this, TIME_SAMPLES));
190         add_item(new ManualGotoUnitItem(this, TIME_SAMPLES_HEX));
191         add_item(new ManualGotoUnitItem(this, TIME_SECONDS));
192         add_item(new ManualGotoUnitItem(this, TIME_FEET_FRAMES));
193 }
194
195 void ManualGotoWindow::create_objects()
196 {
197         lock_window("ManualGotoWindow::create_objects");
198         MWindow *mwindow = mango->mwindow;
199         time_format = mwindow->edl->session->time_format;
200         int margin = mwindow->theme->widget_border;
201         int x = xS(10) + BC_OKButton::calculate_w() + margin, y = yS(10);
202         int pop_w = xS(24), x1 = x + pop_w + margin;
203         add_subwindow(format_text = new BC_Title(x1, y, ""));
204         y += format_text->get_h() + margin;
205         add_subwindow(direction = new ManualGotoDirection(this, x, y, pop_w));
206         direction->create_objects();
207         int tw = get_w() - x1 - xS(10) - BC_CancelButton::calculate_w() - margin - pop_w - margin;
208         add_subwindow(time_text = new ManualGotoText(this, x1, y, tw));
209         x1 += time_text->get_w() + margin;
210         add_subwindow(units = new ManualGotoUnits(this, x1, y, pop_w));
211         units->create_objects();
212         update();
213         add_subwindow(new BC_OKButton(this));
214         add_subwindow(new BC_CancelButton(this));
215         unlock_window();
216 }
217