3f2cb74b614912c794863b337585d76debf8e971
[goodguy/history.git] / cinelerra-5.1 / cinelerra / renderprofiles.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2007 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 "clip.h"
23 #include "language.h"
24 #include "renderprofiles.h"
25 #include "mwindow.h"
26 #include "theme.h"
27 #include "bchash.h"
28 #include "string.h"
29 #include "render.h"
30 #include "asset.h"
31 #include "errorbox.h"
32 #include "mwindowgui.h"
33
34 #define LISTWIDTH 200
35
36 RenderProfileItem::RenderProfileItem(const char *text, int value)
37  : BC_ListBoxItem(text)
38 {
39         this->value = value;
40 }
41
42
43 RenderProfile::RenderProfile(MWindow *mwindow,
44         RenderWindow *rwindow,
45         int x,
46         int y,
47         int use_nothing)
48 {
49         this->mwindow = mwindow;
50         this->rwindow = rwindow;
51         this->x = x;
52         this->y = y;
53         this->use_nothing = use_nothing;
54         for (int i = 1; i < MAX_PROFILES; i++)
55         {
56                 char string_name[100];
57                 char name[100] = "";
58                 sprintf(string_name, "RENDER_%i_PROFILE_NAME", i);
59                 mwindow->defaults->get(string_name, name);
60                 if (strlen(name) != 0)
61                         profiles.append(new RenderProfileItem(name, i));
62
63         }
64 }
65
66 RenderProfile::~RenderProfile()
67 {
68 //      delete title;
69 //      delete textbox;
70 //      delete listbox;
71         for(int i = 0; i < profiles.total; i++)
72                 delete profiles.values[i];
73 }
74
75
76
77 int RenderProfile::calculate_h(BC_WindowBase *gui)
78 {
79         return BC_TextBox::calculate_h(gui, MEDIUMFONT, 1, 1);
80 }
81
82 int RenderProfile::create_objects()
83 {
84         int x = this->x, y = this->y;
85         const char *default_text = "";
86         rwindow->add_subwindow(new BC_Title(x,
87                 y,
88                         _("RenderProfile:")));
89
90
91         int old_y = y;
92         rwindow->add_subwindow(title = new BC_Title(x, y, _("Render profile:")));
93         y += 25;
94         rwindow->add_subwindow(textbox = new BC_TextBox(x,
95                 y,
96                 LISTWIDTH,
97                 1,
98                 default_text));
99         x += textbox->get_w();
100         rwindow->add_subwindow(listbox = new RenderProfileListBox(rwindow, this, x, y));
101
102         y = old_y;
103         x += listbox->get_w() + 10;
104         rwindow->add_subwindow(saveprofile = new SaveRenderProfileButton(this,
105                 x,
106                 y));
107         y += 25;
108         rwindow->add_subwindow(deleteprofile = new DeleteRenderProfileButton(this,
109                 x,
110                 y));
111
112
113
114         return 0;
115 }
116
117 int RenderProfile::get_h()
118 {
119         int result = 0;
120         result = MAX(result, title->get_h());
121         result = MAX(result, textbox->get_h());
122         return result;
123 }
124
125 int RenderProfile::get_x()
126 {
127         return x;
128 }
129
130 int RenderProfile::get_y()
131 {
132         return y;
133 }
134
135 int RenderProfile::reposition_window(int x, int y)
136 {
137         this->x = x;
138         this->y = y;
139         title->reposition_window(x, y);
140         y += 20;
141         textbox->reposition_window(x, y);
142         x += textbox->get_w();
143         listbox->reposition_window(x,
144                 y,
145                 LISTWIDTH);
146         return 0;
147 }
148
149
150 RenderProfileListBox::RenderProfileListBox(BC_WindowBase *window,
151         RenderProfile *renderprofile,
152         int x,
153         int y)
154  : BC_ListBox(x,
155         y,
156         LISTWIDTH,
157         150,
158         LISTBOX_TEXT,
159         (ArrayList<BC_ListBoxItem *>*)&renderprofile->profiles,
160         0,
161         0,
162         1,
163         0,
164         1)
165 {
166         this->window = window;
167         this->renderprofile = renderprofile;
168 }
169
170 RenderProfileListBox::~RenderProfileListBox()
171 {
172 }
173
174 int RenderProfileListBox::handle_event()
175 {
176         if(get_selection(0, 0) >= 0)
177         {
178                 renderprofile->textbox->update(get_selection(0, 0)->get_text());
179                 renderprofile->rwindow->load_profile(((RenderProfileItem*)get_selection(0, 0))->value);
180         }
181         return 1;
182 }
183
184 int RenderProfile::get_profile_slot_by_name(const char *profile_name)
185 {
186         for (int i = 1; i < MAX_PROFILES; i++)
187         {
188                 char string_name[100];
189                 char name[100] = "";
190                 sprintf(string_name, "RENDER_%i_PROFILE_NAME", i);
191
192                 mwindow->defaults->get(string_name, name);
193                 if (strcmp(name, profile_name) == 0)
194                         return i;
195         }
196 // No free profile slots!
197         return -1;
198 }
199
200 int RenderProfile::get_new_profile_slot()
201 {
202         for (int i = 1; i < MAX_PROFILES; i++)
203         {
204                 char string_name[100];
205                 char name[100] = "";
206                 sprintf(string_name, "RENDER_%i_PROFILE_NAME", i);
207                 mwindow->defaults->get(string_name, name);
208                 if (strlen(name) == 0)
209                         return i;
210         }
211         return -1;
212 }
213
214
215 int RenderProfile::save_to_slot(int profile_slot, const char *profile_name)
216 {
217         char string_name[100];
218         sprintf(string_name, "RENDER_%i_PROFILE_NAME", profile_slot);
219         mwindow->defaults->update(string_name, profile_name);
220
221         sprintf(string_name, "RENDER_%i_STRATEGY", profile_slot);
222         mwindow->defaults->update(string_name, rwindow->render->strategy);
223         sprintf(string_name, "RENDER_%i_LOADMODE", profile_slot);
224         mwindow->defaults->update(string_name, rwindow->render->load_mode);
225         sprintf(string_name, "RENDER_%i_RANGE_TYPE", profile_slot);
226         mwindow->defaults->update(string_name, rwindow->render->range_type);
227
228         sprintf(string_name, "RENDER_%i_", profile_slot);
229         rwindow->asset->save_defaults(mwindow->defaults,
230                 string_name,
231                 1,
232                 1,
233                 1,
234                 1,
235                 1);
236
237         mwindow->save_defaults();
238         return 0;
239 }
240
241
242
243 SaveRenderProfileButton::SaveRenderProfileButton(RenderProfile *profile, int x, int y)
244  : BC_GenericButton(x, y, _("Save profile"))
245 {
246         this->profile = profile;
247 }
248 int SaveRenderProfileButton::handle_event()
249 {
250
251         const char *profile_name = profile->textbox->get_text();
252         if (strlen(profile_name) == 0)     // Don't save when name not defined
253                 return 1;
254         int slot = profile->get_profile_slot_by_name(profile_name);
255         if (slot < 0)
256         {
257                 slot = profile->get_new_profile_slot();
258                 if (slot < 0)
259                 {
260                         ErrorBox error_box(_(PROGRAM_NAME ": Error"),
261                                            profile->mwindow->gui->get_abs_cursor_x(1),
262                                            profile->mwindow->gui->get_abs_cursor_y(1));
263                         error_box.create_objects(_("Maximum number of render profiles reached"));
264                         error_box.raise_window();
265                         error_box.run_window();
266                         return 1;
267                 }
268
269                 profile->profiles.append(new RenderProfileItem(profile_name, slot));
270                 profile->listbox->update((ArrayList<BC_ListBoxItem *>*)&(profile->profiles), 0, 0, 1);
271
272         }
273
274         if (slot >= 0)
275         {
276                 profile->save_to_slot(slot, profile_name);
277         }
278         return 1;
279 }
280
281
282 DeleteRenderProfileButton::DeleteRenderProfileButton(RenderProfile *profile, int x, int y)
283  : BC_GenericButton(x, y, _("Delete profile"))
284 {
285         this->profile = profile;
286 }
287 int DeleteRenderProfileButton::handle_event()
288 {
289         const char *profile_name = profile->textbox->get_text();
290         int slot = profile->get_profile_slot_by_name(profile_name);
291         if (slot >= 0)
292         {
293                 for(int i = 0; i < profile->profiles.total; i++)
294                 {
295                         if(profile->profiles.values[i]->value == slot)
296                         {
297                                 profile->profiles.remove_object_number(i);
298                                 profile->save_to_slot(slot, "");
299
300                                 break;
301                         }
302                 }
303                 profile->listbox->update((ArrayList<BC_ListBoxItem *>*)&(profile->profiles), 0, 0, 1);
304                 profile->textbox->update("");
305
306         }
307
308
309         return 1;
310 }
311
312
313
314
315