Credit Andrew - improve in-tree documentation
[goodguy/cinelerra.git] / 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         RenderProfileItem *item = (RenderProfileItem *)get_selection(0, 0);
177         if( item ) {
178                 renderprofile->textbox->update(item->get_text());
179                 renderprofile->rwindow->load_profile(item->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_FILE_PER_LABEL", profile_slot);
222         mwindow->defaults->update(string_name,
223                 rwindow->render->use_labels ? FILE_PER_LABEL : SINGLE_PASS);
224         sprintf(string_name, "RENDER_%i_LOADMODE", profile_slot);
225         mwindow->defaults->update(string_name, rwindow->render->load_mode);
226         sprintf(string_name, "RENDER_%i_RANGE_TYPE", profile_slot);
227         mwindow->defaults->update(string_name, rwindow->render->range_type);
228
229         sprintf(string_name, "RENDER_%i_", profile_slot);
230         rwindow->asset->save_defaults(mwindow->defaults,
231                 string_name, 1, 1, 1, 1, 1);
232
233         mwindow->save_defaults();
234         return 0;
235 }
236
237
238
239 SaveRenderProfileButton::SaveRenderProfileButton(RenderProfile *profile, int x, int y)
240  : BC_GenericButton(x, y, _("Save profile"))
241 {
242         this->profile = profile;
243 }
244 int SaveRenderProfileButton::handle_event()
245 {
246
247         const char *profile_name = profile->textbox->get_text();
248         if (strlen(profile_name) == 0)     // Don't save when name not defined
249                 return 1;
250         int slot = profile->get_profile_slot_by_name(profile_name);
251         if (slot < 0)
252         {
253                 slot = profile->get_new_profile_slot();
254                 if (slot < 0)
255                 {
256                         ErrorBox error_box(_(PROGRAM_NAME ": Error"),
257                                            profile->mwindow->gui->get_abs_cursor_x(1),
258                                            profile->mwindow->gui->get_abs_cursor_y(1));
259                         error_box.create_objects(_("Maximum number of render profiles reached"));
260                         error_box.raise_window();
261                         error_box.run_window();
262                         return 1;
263                 }
264
265                 profile->profiles.append(new RenderProfileItem(profile_name, slot));
266                 profile->listbox->update((ArrayList<BC_ListBoxItem *>*)&(profile->profiles), 0, 0, 1);
267
268         }
269
270         if (slot >= 0)
271         {
272                 profile->save_to_slot(slot, profile_name);
273         }
274         return 1;
275 }
276
277
278 DeleteRenderProfileButton::DeleteRenderProfileButton(RenderProfile *profile, int x, int y)
279  : BC_GenericButton(x, y, _("Delete profile"))
280 {
281         this->profile = profile;
282 }
283 int DeleteRenderProfileButton::handle_event()
284 {
285         const char *profile_name = profile->textbox->get_text();
286         int slot = profile->get_profile_slot_by_name(profile_name);
287         if (slot >= 0)
288         {
289                 for(int i = 0; i < profile->profiles.total; i++)
290                 {
291                         if(profile->profiles.values[i]->value == slot)
292                         {
293                                 profile->profiles.remove_object_number(i);
294                                 profile->save_to_slot(slot, "");
295
296                                 break;
297                         }
298                 }
299                 profile->listbox->update((ArrayList<BC_ListBoxItem *>*)&(profile->profiles), 0, 0, 1);
300                 profile->textbox->update("");
301
302         }
303
304
305         return 1;
306 }
307
308
309
310
311