add missing GPL information in guicast program files
[goodguy/cinelerra.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 xS(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, y, _("RenderProfile:")));
87
88         int old_y = y;
89         rwindow->add_subwindow(title = new BC_Title(x, y, _("Render profile:")));
90         y += yS(25);
91         rwindow->add_subwindow(textbox = new BC_TextBox(x, y, LISTWIDTH, 1, default_text));
92         x += textbox->get_w();
93         rwindow->add_subwindow(listbox = new RenderProfileListBox(rwindow, this, x, y));
94
95         y = old_y;
96         x += listbox->get_w() + xS(10);
97         rwindow->add_subwindow(saveprofile = new SaveRenderProfileButton(this, x, y));
98         y += yS(25);
99         rwindow->add_subwindow(deleteprofile = new DeleteRenderProfileButton(this, x, y));
100
101         return 0;
102 }
103
104 int RenderProfile::get_h()
105 {
106         int result = 0;
107         result = MAX(result, title->get_h());
108         result = MAX(result, textbox->get_h());
109         return result;
110 }
111
112 int RenderProfile::get_x()
113 {
114         return x;
115 }
116
117 int RenderProfile::get_y()
118 {
119         return y;
120 }
121
122 int RenderProfile::reposition_window(int x, int y)
123 {
124         this->x = x;
125         this->y = y;
126         title->reposition_window(x, y);
127         y += yS(20);
128         textbox->reposition_window(x, y);
129         x += textbox->get_w();
130         listbox->reposition_window(x,
131                 y,
132                 LISTWIDTH);
133         return 0;
134 }
135
136
137 RenderProfileListBox::RenderProfileListBox(BC_WindowBase *window,
138         RenderProfile *renderprofile, int x, int y)
139  : BC_ListBox(x, y, LISTWIDTH, yS(150), LISTBOX_TEXT,
140         (ArrayList<BC_ListBoxItem *>*)&renderprofile->profiles,
141         0, 0, 1, 0, 1)
142 {
143         this->window = window;
144         this->renderprofile = renderprofile;
145 }
146
147 RenderProfileListBox::~RenderProfileListBox()
148 {
149 }
150
151 int RenderProfileListBox::handle_event()
152 {
153         RenderProfileItem *item = (RenderProfileItem *)get_selection(0, 0);
154         if( item ) {
155                 renderprofile->textbox->update(item->get_text());
156                 renderprofile->rwindow->load_profile(item->value);
157         }
158         return 1;
159 }
160
161 int RenderProfile::get_profile_slot_by_name(const char *profile_name)
162 {
163         for (int i = 1; i < MAX_PROFILES; i++)
164         {
165                 char string_name[100];
166                 char name[100] = "";
167                 sprintf(string_name, "RENDER_%i_PROFILE_NAME", i);
168
169                 mwindow->defaults->get(string_name, name);
170                 if (strcmp(name, profile_name) == 0)
171                         return i;
172         }
173 // No free profile slots!
174         return -1;
175 }
176
177 int RenderProfile::get_new_profile_slot()
178 {
179         for (int i = 1; i < MAX_PROFILES; i++)
180         {
181                 char string_name[100];
182                 char name[100] = "";
183                 sprintf(string_name, "RENDER_%i_PROFILE_NAME", i);
184                 mwindow->defaults->get(string_name, name);
185                 if (strlen(name) == 0)
186                         return i;
187         }
188         return -1;
189 }
190
191
192 int RenderProfile::save_to_slot(int profile_slot, const char *profile_name)
193 {
194         char string_name[100];
195         sprintf(string_name, "RENDER_%i_PROFILE_NAME", profile_slot);
196         mwindow->defaults->update(string_name, profile_name);
197
198         sprintf(string_name, "RENDER_%i_FILE_PER_LABEL", profile_slot);
199         mwindow->defaults->update(string_name,
200                 rwindow->render->use_labels ? FILE_PER_LABEL : SINGLE_PASS);
201         sprintf(string_name, "RENDER_%i_LOADMODE", profile_slot);
202         mwindow->defaults->update(string_name, rwindow->render->load_mode);
203         sprintf(string_name, "RENDER_%i_RANGE_TYPE", profile_slot);
204         mwindow->defaults->update(string_name, rwindow->render->range_type);
205
206         sprintf(string_name, "RENDER_%i_", profile_slot);
207         rwindow->asset->save_defaults(mwindow->defaults,
208                 string_name, 1, 1, 1, 1, 1);
209
210         mwindow->save_defaults();
211         return 0;
212 }
213
214
215
216 SaveRenderProfileButton::SaveRenderProfileButton(RenderProfile *profile, int x, int y)
217  : BC_GenericButton(x, y, _("Save profile"))
218 {
219         this->profile = profile;
220 }
221 int SaveRenderProfileButton::handle_event()
222 {
223
224         const char *profile_name = profile->textbox->get_text();
225         if (strlen(profile_name) == 0)     // Don't save when name not defined
226                 return 1;
227         int slot = profile->get_profile_slot_by_name(profile_name);
228         if (slot < 0)
229         {
230                 slot = profile->get_new_profile_slot();
231                 if (slot < 0)
232                 {
233                         ErrorBox error_box(_(PROGRAM_NAME ": Error"),
234                                            profile->mwindow->gui->get_abs_cursor_x(1),
235                                            profile->mwindow->gui->get_abs_cursor_y(1));
236                         error_box.create_objects(_("Maximum number of render profiles reached"));
237                         error_box.raise_window();
238                         error_box.run_window();
239                         return 1;
240                 }
241
242                 profile->profiles.append(new RenderProfileItem(profile_name, slot));
243                 profile->listbox->update((ArrayList<BC_ListBoxItem *>*)&(profile->profiles), 0, 0, 1);
244
245         }
246
247         if (slot >= 0)
248         {
249                 profile->save_to_slot(slot, profile_name);
250         }
251         return 1;
252 }
253
254
255 DeleteRenderProfileButton::DeleteRenderProfileButton(RenderProfile *profile, int x, int y)
256  : BC_GenericButton(x, y, _("Delete profile"))
257 {
258         this->profile = profile;
259 }
260 int DeleteRenderProfileButton::handle_event()
261 {
262         const char *profile_name = profile->textbox->get_text();
263         int slot = profile->get_profile_slot_by_name(profile_name);
264         if (slot >= 0)
265         {
266                 for(int i = 0; i < profile->profiles.total; i++)
267                 {
268                         if(profile->profiles.values[i]->value == slot)
269                         {
270                                 profile->profiles.remove_object_number(i);
271                                 profile->save_to_slot(slot, "");
272
273                                 break;
274                         }
275                 }
276                 profile->listbox->update((ArrayList<BC_ListBoxItem *>*)&(profile->profiles), 0, 0, 1);
277                 profile->textbox->update("");
278
279         }
280         return 1;
281 }
282