6f413cb41bbb451de0719b51ff0afa28d81aa303
[vlc.git] / modules / gui / macosx / VideoEffects.m
1 /*****************************************************************************
2  * VideoEffects.m: MacOS X interface module
3  *****************************************************************************
4  * Copyright (C) 2011-2012 Felix Paul Kühne
5  * $Id$
6  *
7  * Authors: Felix Paul Kühne <fkuehne -at- videolan -dot- org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #import "CompatibilityFixes.h"
25 #import "intf.h"
26 #import <vlc_common.h>
27 #import <vlc_modules.h>
28 #import <vlc_charset.h>
29 #import "VideoEffects.h"
30 #import "SharedDialogs.h"
31
32 @interface VLCVideoEffects (Internal)
33 - (void)resetProfileSelector;
34 @end
35
36 #pragma mark -
37 #pragma mark Initialization
38
39 @implementation VLCVideoEffects
40 static VLCVideoEffects *_o_sharedInstance = nil;
41
42 @synthesize cropLeftValue, cropTopValue, cropRightValue, cropBottomValue;
43 @synthesize puzzleRowsValue, puzzleColumnsValue;
44 @synthesize wallRowsValue, wallColumnsValue;
45 @synthesize cloneValue;
46 @synthesize sepiaValue;
47 @synthesize posterizeValue;
48
49 + (VLCVideoEffects *)sharedInstance
50 {
51     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
52 }
53
54 + (void)initialize
55 {
56     NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:[NSArray arrayWithObject:@";;;0;1.000000;1.000000;1.000000;1.000000;0.050000;16;2.000000;OTA=;4;4;16711680;20;15;120;Z3JhZGllbnQ=;1;0;16711680;6;80;VkxD;-1;;-1;255;2;3;3"], @"VideoEffectProfiles",
57                                  [NSArray arrayWithObject:_NS("Default")], @"VideoEffectProfileNames", nil];
58     [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
59 }
60
61 - (id)init
62 {
63     if (_o_sharedInstance)
64         [self dealloc];
65     else {
66         p_intf = VLCIntf;
67         i_old_profile_index = -1;
68         _o_sharedInstance = [super init];
69     }
70
71     return _o_sharedInstance;
72 }
73
74 - (void)awakeFromNib
75 {
76     [o_window setTitle: _NS("Video Effects")];
77     [o_window setExcludedFromWindowsMenu:YES];
78     if (!OSX_SNOW_LEOPARD)
79         [o_window setCollectionBehavior: NSWindowCollectionBehaviorFullScreenAuxiliary];
80
81     [[o_tableView tabViewItemAtIndex:[o_tableView indexOfTabViewItemWithIdentifier:@"basic"]] setLabel:_NS("Basic")];
82     [[o_tableView tabViewItemAtIndex:[o_tableView indexOfTabViewItemWithIdentifier:@"crop"]] setLabel:_NS("Crop")];
83     [[o_tableView tabViewItemAtIndex:[o_tableView indexOfTabViewItemWithIdentifier:@"geometry"]] setLabel:_NS("Geometry")];
84     [[o_tableView tabViewItemAtIndex:[o_tableView indexOfTabViewItemWithIdentifier:@"color"]] setLabel:_NS("Color")];
85     [[o_tableView tabViewItemAtIndex:[o_tableView indexOfTabViewItemWithIdentifier:@"misc"]] setLabel:_NS("Miscellaneous")];
86
87     [self resetProfileSelector];
88
89     [o_adjust_ckb setTitle:_NS("Image Adjust")];
90     [o_adjust_hue_lbl setStringValue:_NS("Hue")];
91     [o_adjust_contrast_lbl setStringValue:_NS("Contrast")];
92     [o_adjust_brightness_lbl setStringValue:_NS("Brightness")];
93     [o_adjust_brightness_ckb setTitle:_NS("Brightness Threshold")];
94     [o_adjust_saturation_lbl setStringValue:_NS("Saturation")];
95     [o_adjust_gamma_lbl setStringValue:_NS("Gamma")];
96     [o_adjust_reset_btn setTitle: _NS("Reset")];
97     [o_sharpen_ckb setTitle:_NS("Sharpen")];
98     [o_sharpen_lbl setStringValue:_NS("Sigma")];
99     [o_banding_ckb setTitle:_NS("Banding removal")];
100     [o_banding_lbl setStringValue:_NS("Radius")];
101     [o_grain_ckb setTitle:_NS("Film Grain")];
102     [o_grain_lbl setStringValue:_NS("Variance")];
103     [o_crop_top_lbl setStringValue:_NS("Top")];
104     [o_crop_left_lbl setStringValue:_NS("Left")];
105     [o_crop_right_lbl setStringValue:_NS("Right")];
106     [o_crop_bottom_lbl setStringValue:_NS("Bottom")];
107     [o_crop_sync_top_bottom_ckb setTitle:_NS("Synchronize top and bottom")];
108     [o_crop_sync_left_right_ckb setTitle:_NS("Synchronize left and right")];
109
110     [o_transform_ckb setTitle:_NS("Transform")];
111     [o_transform_pop removeAllItems];
112     [o_transform_pop addItemWithTitle: _NS("Rotate by 90 degrees")];
113     [[o_transform_pop lastItem] setTag: 90];
114     [o_transform_pop addItemWithTitle: _NS("Rotate by 180 degrees")];
115     [[o_transform_pop lastItem] setTag: 180];
116     [o_transform_pop addItemWithTitle: _NS("Rotate by 270 degrees")];
117     [[o_transform_pop lastItem] setTag: 270];
118     [o_transform_pop addItemWithTitle: _NS("Flip horizontally")];
119     [[o_transform_pop lastItem] setTag: 1];
120     [o_transform_pop addItemWithTitle: _NS("Flip vertically")];
121     [[o_transform_pop lastItem] setTag: 2];
122     [o_zoom_ckb setTitle:_NS("Magnification/Zoom")];
123     [o_puzzle_ckb setTitle:_NS("Puzzle game")];
124     [o_puzzle_rows_lbl setStringValue:_NS("Rows")];
125     [o_puzzle_columns_lbl setStringValue:_NS("Columns")];
126     [o_clone_ckb setTitle:_NS("Clone")];
127     [o_clone_number_lbl setStringValue:_NS("Number of clones")];
128     [o_wall_ckb setTitle:_NS("Wall")];
129     [o_wall_numofrows_lbl setStringValue:_NS("Rows")];
130     [o_wall_numofcols_lbl setStringValue:_NS("Columns")];
131
132     [o_threshold_ckb setTitle:_NS("Color threshold")];
133     [o_threshold_color_lbl setStringValue:_NS("Color")];
134     [o_threshold_saturation_lbl setStringValue:_NS("Saturation")];
135     [o_threshold_similarity_lbl setStringValue:_NS("Similarity")];
136     [o_sepia_ckb setTitle:_NS("Sepia")];
137     [o_sepia_lbl setStringValue:_NS("Intensity")];
138     [o_noise_ckb setTitle:_NS("Noise")];
139     [o_gradient_ckb setTitle:_NS("Gradient")];
140     [o_gradient_mode_lbl setStringValue:_NS("Mode")];
141     [o_gradient_mode_pop removeAllItems];
142     [o_gradient_mode_pop addItemWithTitle: _NS("Gradient")];
143     [[o_gradient_mode_pop lastItem] setTag: 1];
144     [o_gradient_mode_pop addItemWithTitle: _NS("Edge")];
145     [[o_gradient_mode_pop lastItem] setTag: 2];
146     [o_gradient_mode_pop addItemWithTitle: _NS("Hough")];
147     [[o_gradient_mode_pop lastItem] setTag: 3];
148     [o_gradient_color_ckb setTitle:_NS("Color")];
149     [o_gradient_cartoon_ckb setTitle:_NS("Cartoon")];
150     [o_extract_ckb setTitle:_NS("Color extraction")];
151     [o_extract_lbl setStringValue:_NS("Color")];
152     [o_invert_ckb setTitle:_NS("Invert colors")];
153     [o_posterize_ckb setTitle:_NS("Posterize")];
154     [o_posterize_lbl setStringValue:_NS("Posterize level")];
155     [o_blur_ckb setTitle:_NS("Motion blur")];
156     [o_blur_lbl setStringValue:_NS("Factor")];
157     [o_motiondetect_ckb setTitle:_NS("Motion Detect")];
158     [o_watereffect_ckb setTitle:_NS("Water effect")];
159     [o_waves_ckb setTitle:_NS("Waves")];
160     [o_psychedelic_ckb setTitle:_NS("Psychedelic")];
161     [o_anaglyph_ckb setTitle:_NS("Anaglyph")];
162
163     [o_addtext_ckb setTitle:_NS("Add text")];
164     [o_addtext_text_lbl setStringValue:_NS("Text")];
165     [o_addtext_pos_lbl setStringValue:_NS("Position")];
166     [o_addtext_pos_pop removeAllItems];
167     [o_addtext_pos_pop addItemWithTitle: _NS("Center")];
168     [[o_addtext_pos_pop lastItem] setTag: 0];
169     [o_addtext_pos_pop addItemWithTitle: _NS("Left")];
170     [[o_addtext_pos_pop lastItem] setTag: 1];
171     [o_addtext_pos_pop addItemWithTitle: _NS("Right")];
172     [[o_addtext_pos_pop lastItem] setTag: 2];
173     [o_addtext_pos_pop addItemWithTitle: _NS("Top")];
174     [[o_addtext_pos_pop lastItem] setTag: 4];
175     [o_addtext_pos_pop addItemWithTitle: _NS("Bottom")];
176     [[o_addtext_pos_pop lastItem] setTag: 8];
177     [o_addtext_pos_pop addItemWithTitle: _NS("Top-Left")];
178     [[o_addtext_pos_pop lastItem] setTag: 5];
179     [o_addtext_pos_pop addItemWithTitle: _NS("Top-Right")];
180     [[o_addtext_pos_pop lastItem] setTag: 6];
181     [o_addtext_pos_pop addItemWithTitle: _NS("Bottom-Left")];
182     [[o_addtext_pos_pop lastItem] setTag: 9];
183     [o_addtext_pos_pop addItemWithTitle: _NS("Bottom-Right")];
184     [[o_addtext_pos_pop lastItem] setTag: 10];
185     [o_addlogo_ckb setTitle:_NS("Add logo")];
186     [o_addlogo_logo_lbl setStringValue:_NS("Logo")];
187     [o_addlogo_pos_lbl setStringValue:_NS("Position")];
188     [o_addlogo_pos_pop removeAllItems];
189     [o_addlogo_pos_pop addItemWithTitle: _NS("Center")];
190     [[o_addlogo_pos_pop lastItem] setTag: 0];
191     [o_addlogo_pos_pop addItemWithTitle: _NS("Left")];
192     [[o_addlogo_pos_pop lastItem] setTag: 1];
193     [o_addlogo_pos_pop addItemWithTitle: _NS("Right")];
194     [[o_addlogo_pos_pop lastItem] setTag: 2];
195     [o_addlogo_pos_pop addItemWithTitle: _NS("Top")];
196     [[o_addlogo_pos_pop lastItem] setTag: 4];
197     [o_addlogo_pos_pop addItemWithTitle: _NS("Bottom")];
198     [[o_addlogo_pos_pop lastItem] setTag: 8];
199     [o_addlogo_pos_pop addItemWithTitle: _NS("Top-Left")];
200     [[o_addlogo_pos_pop lastItem] setTag: 5];
201     [o_addlogo_pos_pop addItemWithTitle: _NS("Top-Right")];
202     [[o_addlogo_pos_pop lastItem] setTag: 6];
203     [o_addlogo_pos_pop addItemWithTitle: _NS("Bottom-Left")];
204     [[o_addlogo_pos_pop lastItem] setTag: 9];
205     [o_addlogo_pos_pop addItemWithTitle: _NS("Bottom-Right")];
206     [[o_addlogo_pos_pop lastItem] setTag: 10];
207     [o_addlogo_transparency_lbl setStringValue:_NS("Transparency")];
208
209     [o_tableView selectFirstTabViewItem:self];
210
211     [self resetValues];
212 }
213
214 - (void)updateCocoaWindowLevel:(NSInteger)i_level
215 {
216     if (o_window && [o_window isVisible] && [o_window level] != i_level)
217         [o_window setLevel: i_level];
218 }
219
220 #pragma mark -
221 #pragma mark internal functions
222 - (void)resetProfileSelector
223 {
224     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
225     [o_profile_pop removeAllItems];
226
227     NSArray * profileNames = [defaults objectForKey:@"VideoEffectProfileNames"];
228     [o_profile_pop addItemsWithTitles:profileNames];
229
230     [[o_profile_pop menu] addItem:[NSMenuItem separatorItem]];
231     [o_profile_pop addItemWithTitle:_NS("Duplicate current profile...")];
232     [[o_profile_pop lastItem] setTarget: self];
233     [[o_profile_pop lastItem] setAction: @selector(addProfile:)];
234
235     if ([profileNames count] > 1) {
236         [o_profile_pop addItemWithTitle:_NS("Organize profiles...")];
237         [[o_profile_pop lastItem] setTarget: self];
238         [[o_profile_pop lastItem] setAction: @selector(removeProfile:)];
239     }
240
241     [o_profile_pop selectItemAtIndex:[defaults integerForKey:@"VideoEffectSelectedProfile"]];
242     [self profileSelectorAction:self];
243 }
244
245 - (void)resetValues
246 {
247     NSString *tmpString;
248     char *tmpChar;
249     BOOL b_state;
250
251     /* do we have any filter enabled? if yes, show it. */
252     char * psz_vfilters;
253     psz_vfilters = config_GetPsz(p_intf, "video-filter");
254     if (psz_vfilters) {
255         [o_adjust_ckb setState: (NSInteger)strstr(psz_vfilters, "adjust")];
256         [o_sharpen_ckb setState: (NSInteger)strstr(psz_vfilters, "sharpen")];
257         [o_banding_ckb setState: (NSInteger)strstr(psz_vfilters, "gradfun")];
258         [o_grain_ckb setState: (NSInteger)strstr(psz_vfilters, "grain")];
259         [o_transform_ckb setState: (NSInteger)strstr(psz_vfilters, "transform")];
260         [o_zoom_ckb setState: (NSInteger)strstr(psz_vfilters, "magnify")];
261         [o_puzzle_ckb setState: (NSInteger)strstr(psz_vfilters, "puzzle")];
262         [o_threshold_ckb setState: (NSInteger)strstr(psz_vfilters, "colorthres")];
263         [o_sepia_ckb setState: (NSInteger)strstr(psz_vfilters, "sepia")];
264         [o_noise_ckb setState: (NSInteger)strstr(psz_vfilters, "noise")];
265         [o_gradient_ckb setState: (NSInteger)strstr(psz_vfilters, "gradient")];
266         [o_extract_ckb setState: (NSInteger)strstr(psz_vfilters, "extract")];
267         [o_invert_ckb setState: (NSInteger)strstr(psz_vfilters, "invert")];
268         [o_posterize_ckb setState: (NSInteger)strstr(psz_vfilters, "posterize")];
269         [o_blur_ckb setState: (NSInteger)strstr(psz_vfilters, "motionblur")];
270         [o_motiondetect_ckb setState: (NSInteger)strstr(psz_vfilters, "motiondetect")];
271         [o_watereffect_ckb setState: (NSInteger)strstr(psz_vfilters, "ripple")];
272         [o_waves_ckb setState: (NSInteger)strstr(psz_vfilters, "wave")];
273         [o_psychedelic_ckb setState: (NSInteger)strstr(psz_vfilters, "psychedelic")];
274         [o_anaglyph_ckb setState: (NSInteger)strstr(psz_vfilters, "anaglyph")];
275         free(psz_vfilters);
276     } else {
277         [o_adjust_ckb setState: NSOffState];
278         [o_sharpen_ckb setState: NSOffState];
279         [o_banding_ckb setState: NSOffState];
280         [o_grain_ckb setState: NSOffState];
281         [o_transform_ckb setState: NSOffState];
282         [o_zoom_ckb setState: NSOffState];
283         [o_puzzle_ckb setState: NSOffState];
284         [o_threshold_ckb setState: NSOffState];
285         [o_sepia_ckb setState: NSOffState];
286         [o_noise_ckb setState: NSOffState];
287         [o_gradient_ckb setState: NSOffState];
288         [o_extract_ckb setState: NSOffState];
289         [o_invert_ckb setState: NSOffState];
290         [o_posterize_ckb setState: NSOffState];
291         [o_blur_ckb setState: NSOffState];
292         [o_motiondetect_ckb setState: NSOffState];
293         [o_watereffect_ckb setState: NSOffState];
294         [o_waves_ckb setState: NSOffState];
295         [o_psychedelic_ckb setState: NSOffState];
296         [o_anaglyph_ckb setState: NSOffState];
297     }
298
299     psz_vfilters = config_GetPsz(p_intf, "sub-source");
300     if (psz_vfilters) {
301         [o_addtext_ckb setState: (NSInteger)strstr(psz_vfilters, "marq")];
302         [o_addlogo_ckb setState: (NSInteger)strstr(psz_vfilters, "logo")];
303         free(psz_vfilters);
304     } else {
305         [o_addtext_ckb setState: NSOffState];
306         [o_addlogo_ckb setState: NSOffState];
307     }
308
309     psz_vfilters = config_GetPsz(p_intf, "video-splitter");
310     if (psz_vfilters) {
311         [o_clone_ckb setState: (NSInteger)strstr(psz_vfilters, "clone")];
312         [o_wall_ckb setState: (NSInteger)strstr(psz_vfilters, "wall")];
313         free(psz_vfilters);
314     } else {
315         [o_clone_ckb setState: NSOffState];
316         [o_wall_ckb setState: NSOffState];
317     }
318
319     /* fetch and show the various values */
320     [o_adjust_hue_sld setIntValue: config_GetInt(p_intf, "hue")];
321     [o_adjust_contrast_sld setFloatValue: config_GetFloat(p_intf, "contrast")];
322     [o_adjust_brightness_sld setFloatValue: config_GetFloat(p_intf, "brightness")];
323     [o_adjust_saturation_sld setFloatValue: config_GetFloat(p_intf, "saturation")];
324     [o_adjust_gamma_sld setFloatValue: config_GetFloat(p_intf, "gamma")];
325     [o_adjust_brightness_sld setToolTip: [NSString stringWithFormat:@"%0.3f", config_GetFloat(p_intf, "brightness")]];
326     [o_adjust_contrast_sld setToolTip: [NSString stringWithFormat:@"%0.3f", config_GetFloat(p_intf, "contrast")]];
327     [o_adjust_gamma_sld setToolTip: [NSString stringWithFormat:@"%0.3f", config_GetFloat(p_intf, "gamma")]];
328     [o_adjust_hue_sld setToolTip: [NSString stringWithFormat:@"%lli", config_GetInt(p_intf, "hue")]];
329     [o_adjust_saturation_sld setToolTip: [NSString stringWithFormat:@"%0.3f", config_GetFloat(p_intf, "saturation")]];
330     b_state = [o_adjust_ckb state];
331     [o_adjust_brightness_sld setEnabled: b_state];
332     [o_adjust_brightness_ckb setEnabled: b_state];
333     [o_adjust_contrast_sld setEnabled: b_state];
334     [o_adjust_gamma_sld setEnabled: b_state];
335     [o_adjust_hue_sld setEnabled: b_state];
336     [o_adjust_saturation_sld setEnabled: b_state];
337     [o_adjust_brightness_lbl setEnabled: b_state];
338     [o_adjust_contrast_lbl setEnabled: b_state];
339     [o_adjust_gamma_lbl setEnabled: b_state];
340     [o_adjust_hue_lbl setEnabled: b_state];
341     [o_adjust_saturation_lbl setEnabled: b_state];
342     [o_adjust_reset_btn setEnabled: b_state];
343
344     [o_sharpen_sld setFloatValue: config_GetFloat(p_intf, "sharpen-sigma")];
345     [o_sharpen_sld setToolTip: [NSString stringWithFormat:@"%0.3f", config_GetFloat(p_intf, "sharpen-sigma")]];
346     [o_sharpen_sld setEnabled: [o_sharpen_ckb state]];
347     [o_sharpen_lbl setEnabled: [o_sharpen_ckb state]];
348
349     [o_banding_sld setIntValue: config_GetInt(p_intf, "gradfun-radius")];
350     [o_banding_sld setToolTip: [NSString stringWithFormat:@"%lli", config_GetInt(p_intf, "gradfun-radius")]];
351     [o_banding_sld setEnabled: [o_banding_ckb state]];
352     [o_banding_lbl setEnabled: [o_banding_ckb state]];
353
354     [o_grain_sld setFloatValue: config_GetFloat(p_intf, "grain-variance")];
355     [o_grain_sld setToolTip: [NSString stringWithFormat:@"%0.3f", config_GetFloat(p_intf, "grain-variance")]];
356     [o_grain_sld setEnabled: [o_grain_ckb state]];
357     [o_grain_lbl setEnabled: [o_grain_ckb state]];
358
359     [self setCropLeftValue: 0];
360     [self setCropTopValue: 0];
361     [self setCropRightValue: 0];
362     [self setCropBottomValue: 0];
363     [o_crop_sync_top_bottom_ckb setState: NSOffState];
364     [o_crop_sync_left_right_ckb setState: NSOffState];
365
366     tmpChar = config_GetPsz(p_intf, "transform-type");
367     tmpString = [NSString stringWithUTF8String:tmpChar];
368     if ([tmpString isEqualToString:@"hflip"])
369         [o_transform_pop selectItemWithTag: 1];
370     else if ([tmpString isEqualToString:@"vflip"])
371         [o_transform_pop selectItemWithTag: 2];
372     else
373         [o_transform_pop selectItemWithTag:[tmpString intValue]];
374     FREENULL(tmpChar);
375     [o_transform_pop setEnabled: [o_transform_ckb state]];
376
377     [self setPuzzleColumnsValue: config_GetInt(p_intf, "puzzle-cols")];
378     [self setPuzzleRowsValue: config_GetInt(p_intf, "puzzle-rows")];
379     b_state = [o_puzzle_ckb state];
380     [o_puzzle_rows_fld setEnabled: b_state];
381     [o_puzzle_rows_stp setEnabled: b_state];
382     [o_puzzle_rows_lbl setEnabled: b_state];
383     [o_puzzle_columns_fld setEnabled: b_state];
384     [o_puzzle_columns_stp setEnabled: b_state];
385     [o_puzzle_columns_lbl setEnabled: b_state];
386
387     [self setCloneValue: config_GetInt(p_intf, "clone-count")];
388     b_state = [o_clone_ckb state];
389     [o_clone_number_lbl setEnabled: b_state];
390     [o_clone_number_fld setEnabled: b_state];
391     [o_clone_number_stp setEnabled: b_state];
392
393     b_state = [o_wall_ckb state];
394     [self setWallRowsValue: config_GetInt(p_intf, "wall-rows")];
395     [o_wall_numofrows_lbl setEnabled: b_state];
396     [o_wall_numofrows_fld setEnabled: b_state];
397     [o_wall_numofrows_stp setEnabled: b_state];
398     [self setWallColumnsValue: config_GetInt(p_intf, "wall-cols")];
399     [o_wall_numofcols_lbl setEnabled: b_state];
400     [o_wall_numofcols_fld setEnabled: b_state];
401     [o_wall_numofcols_stp setEnabled: b_state];
402
403     [o_threshold_color_fld setStringValue: [[NSString stringWithFormat:@"%llx", config_GetInt(p_intf, "colorthres-color")] uppercaseString]];
404     [o_threshold_saturation_sld setIntValue: config_GetInt(p_intf, "colorthres-saturationthres")];
405     [o_threshold_saturation_sld setToolTip: [NSString stringWithFormat:@"%lli", config_GetInt(p_intf, "colorthres-saturationthres")]];
406     [o_threshold_similarity_sld setIntValue: config_GetInt(p_intf, "colorthres-similaritythres")];
407     [o_threshold_similarity_sld setToolTip: [NSString stringWithFormat:@"%lli", config_GetInt(p_intf, "colorthres-similaritythres")]];
408
409     b_state = [o_threshold_ckb state];
410     [o_threshold_color_fld setEnabled: b_state];
411     [o_threshold_color_lbl setEnabled: b_state];
412     [o_threshold_saturation_sld setEnabled: b_state];
413     [o_threshold_saturation_lbl setEnabled: b_state];
414     [o_threshold_similarity_sld setEnabled: b_state];
415     [o_threshold_similarity_lbl setEnabled: b_state];
416
417     [self setSepiaValue: config_GetInt(p_intf, "sepia-intensity")];
418     b_state = [o_sepia_ckb state];
419     [o_sepia_fld setEnabled: b_state];
420     [o_sepia_stp setEnabled: b_state];
421     [o_sepia_lbl setEnabled: b_state];
422
423     tmpChar = config_GetPsz(p_intf, "gradient-mode");
424     tmpString = [NSString stringWithUTF8String:tmpChar];
425     if ([tmpString isEqualToString:@"hough"])
426         [o_gradient_mode_pop selectItemWithTag: 3];
427     else if ([tmpString isEqualToString:@"edge"])
428         [o_gradient_mode_pop selectItemWithTag: 2];
429     else
430         [o_gradient_mode_pop selectItemWithTag: 1];
431     FREENULL(tmpChar);
432     [o_gradient_cartoon_ckb setState: config_GetInt(p_intf, "gradient-cartoon")];
433     [o_gradient_color_ckb setState: config_GetInt(p_intf, "gradient-type")];
434     b_state = [o_gradient_ckb state];
435     [o_gradient_mode_pop setEnabled: b_state];
436     [o_gradient_mode_lbl setEnabled: b_state];
437     [o_gradient_cartoon_ckb setEnabled: b_state];
438     [o_gradient_color_ckb setEnabled: b_state];
439
440     [o_extract_fld setStringValue: [[NSString stringWithFormat:@"%llx", config_GetInt(p_intf, "extract-component")] uppercaseString]];
441     [o_extract_fld setEnabled: [o_extract_ckb state]];
442     [o_extract_lbl setEnabled: [o_extract_ckb state]];
443
444     [self setPosterizeValue: config_GetInt(p_intf, "posterize-level")];
445     b_state = [o_posterize_ckb state];
446     [o_posterize_fld setEnabled: b_state];
447     [o_posterize_stp setEnabled: b_state];
448     [o_posterize_lbl setEnabled: b_state];
449
450     [o_blur_sld setIntValue: config_GetInt(p_intf, "blur-factor")];
451     [o_blur_sld setToolTip: [NSString stringWithFormat:@"%lli", config_GetInt(p_intf, "blur-factor")]];
452     [o_blur_sld setEnabled: [o_blur_ckb state]];
453     [o_blur_lbl setEnabled: [o_blur_ckb state]];
454
455     tmpChar = config_GetPsz(p_intf, "marq-marquee");
456     if (tmpChar) {
457         [o_addtext_text_fld setStringValue: [NSString stringWithUTF8String:tmpChar]];
458         FREENULL(tmpChar);
459     } else
460         [o_addtext_text_fld setStringValue: @""];
461     [o_addtext_pos_pop selectItemWithTag: config_GetInt(p_intf, "marq-position")];
462     b_state = [o_addtext_ckb state];
463     [o_addtext_pos_pop setEnabled: b_state];
464     [o_addtext_pos_lbl setEnabled: b_state];
465     [o_addtext_text_lbl setEnabled: b_state];
466     [o_addtext_text_fld setEnabled: b_state];
467
468     tmpChar = config_GetPsz(p_intf, "logo-file");
469     if (tmpChar) {
470         [o_addlogo_logo_fld setStringValue: [NSString stringWithUTF8String:tmpChar]];
471         FREENULL(tmpChar);
472     } else
473         [o_addlogo_logo_fld setStringValue: @""];
474     [o_addlogo_pos_pop selectItemWithTag: config_GetInt(p_intf, "logo-position")];
475     [o_addlogo_transparency_sld setIntValue: config_GetInt(p_intf, "logo-opacity")];
476     [o_addlogo_transparency_sld setToolTip: [NSString stringWithFormat:@"%lli", config_GetInt(p_intf, "logo-opacity")]];
477     b_state = [o_addlogo_ckb state];
478     [o_addlogo_pos_pop setEnabled: b_state];
479     [o_addlogo_pos_lbl setEnabled: b_state];
480     [o_addlogo_logo_fld setEnabled: b_state];
481     [o_addlogo_logo_lbl setEnabled: b_state];
482     [o_addlogo_transparency_sld setEnabled: b_state];
483     [o_addlogo_transparency_lbl setEnabled: b_state];
484 }
485
486 - (const char *)getFilterType:(char *)psz_name
487 {
488     module_t *p_obj = module_find(psz_name);
489     if (!p_obj) {
490         return NULL;
491     }
492
493     if (module_provides(p_obj, "video splitter")) {
494         return "video-splitter";
495     } else if (module_provides(p_obj, "video filter2")) {
496         return "video-filter";
497     } else if (module_provides(p_obj, "sub source")) {
498         return "sub-source";
499     } else if (module_provides(p_obj, "sub filter")) {
500         return "sub-filter";
501     } else {
502         msg_Err(p_intf, "Unknown video filter type.");
503         return NULL;
504     }
505 }
506
507 - (void)setVideoFilter: (char *)psz_name on:(BOOL)b_on
508 {
509     char *psz_string, *psz_parser;
510
511     const char *psz_filter_type = [self getFilterType:psz_name];
512     if (!psz_filter_type) {
513         msg_Err(p_intf, "Unable to find filter module \"%s\".", psz_name);
514         return;
515     }
516
517     msg_Dbg(p_intf, "will set filter '%s'", psz_name);
518
519
520     psz_string = config_GetPsz(p_intf, psz_filter_type);
521
522     if (b_on) {
523         if (!psz_string) {
524             free(psz_string);
525             psz_string = strdup(psz_name);
526         } else if (strstr(psz_string, psz_name) == NULL) {
527             char *psz_tmp = strdup([[NSString stringWithFormat: @"%s:%s", psz_string, psz_name] UTF8String]);
528             free(psz_string);
529             psz_string = psz_tmp;
530         }
531     } else {
532         if (!psz_string)
533             return;
534
535         psz_parser = strstr(psz_string, psz_name);
536         if (psz_parser) {
537             if (*(psz_parser + strlen(psz_name)) == ':') {
538                 memmove(psz_parser, psz_parser + strlen(psz_name) + 1,
539                         strlen(psz_parser + strlen(psz_name) + 1) + 1);
540             } else {
541                 *psz_parser = '\0';
542             }
543
544             /* Remove trailing : : */
545             if (strlen(psz_string) > 0 && *(psz_string + strlen(psz_string) -1) == ':')
546                 *(psz_string + strlen(psz_string) -1) = '\0';
547         } else {
548             free(psz_string);
549             return;
550         }
551     }
552     config_PutPsz(p_intf, psz_filter_type, psz_string);
553
554     /* Try to set on the fly */
555     if (!strcmp(psz_filter_type, "video-splitter")) {
556         playlist_t *p_playlist = pl_Get(p_intf);
557         var_SetString(p_playlist, psz_filter_type, psz_string);
558     } else {
559         vout_thread_t *p_vout = getVout();
560         if (p_vout) {
561             var_SetString(p_vout, psz_filter_type, psz_string);
562             vlc_object_release(p_vout);
563         }
564     }
565
566     free(psz_string);
567 }
568
569 - (void)restartFilterIfNeeded: (char *)psz_filter option: (char *)psz_name
570 {
571     vout_thread_t *p_vout = getVout();
572
573     if (p_vout == NULL)
574         return;
575     else
576         vlc_object_release(p_vout);
577
578     vlc_object_t *p_filter = vlc_object_find_name(pl_Get(p_intf), psz_filter);
579     if (p_filter) {
580
581         /* we cannot rely on the p_filter existence.
582          This filter might be just
583          disabled, but the object still exists. Therefore, the string
584          is checked, additionally.
585          */
586         const char *psz_filter_type = [self getFilterType:psz_filter];
587         if (!psz_filter_type) {
588             msg_Err(p_intf, "Unable to find filter module \"%s\".", psz_name);
589             goto out;
590         }
591
592         char *psz_string = config_GetPsz(p_intf, psz_filter_type);
593         if (!psz_string) {
594             goto out;
595         }
596         if (strstr(psz_string, psz_filter) == NULL) {
597             free(psz_string);
598             goto out;
599         }
600         free(psz_string);
601
602         int i_type;
603         i_type = var_Type(p_filter, psz_name);
604         if (i_type == 0)
605             i_type = config_GetType(p_intf, psz_name);
606
607         if (!(i_type & VLC_VAR_ISCOMMAND)) {
608             msg_Warn(p_intf, "Brute-restarting filter '%s', because the last changed option isn't a command", psz_name);
609
610             [self setVideoFilter: psz_filter on: NO];
611             [self setVideoFilter: psz_filter on: YES];
612         } else
613             msg_Dbg(p_intf, "restart not needed");
614
615         out:
616         vlc_object_release(p_filter);
617     }
618 }
619
620 - (void)setVideoFilterProperty: (char *)psz_name forFilter: (char *)psz_filter integer: (int)i_value
621 {
622     vout_thread_t *p_vout = getVout();
623     vlc_object_t *p_filter;
624
625     config_PutInt(p_intf, psz_name, i_value);
626
627     if (p_vout) {
628         p_filter = vlc_object_find_name(pl_Get(p_intf), psz_filter);
629
630         if (!p_filter) {
631             msg_Warn(p_intf, "filter '%s' isn't enabled", psz_filter);
632             vlc_object_release(p_vout);
633             return;
634         }
635         var_SetInteger(p_filter, psz_name, i_value);
636         vlc_object_release(p_vout);
637         vlc_object_release(p_filter);
638
639         [self restartFilterIfNeeded: psz_filter option: psz_name];
640     }
641 }
642
643 - (void)setVideoFilterProperty: (char *)psz_name forFilter: (char *)psz_filter float: (float)f_value
644 {
645     vout_thread_t *p_vout = getVout();
646     vlc_object_t *p_filter;
647
648     config_PutFloat(p_intf, psz_name, f_value);
649
650     if (p_vout) {
651         p_filter = vlc_object_find_name(pl_Get(p_intf), psz_filter);
652
653         if (!p_filter) {
654             msg_Warn(p_intf, "filter '%s' isn't enabled", psz_filter);
655             vlc_object_release(p_vout);
656             return;
657         }
658         var_SetFloat(p_filter, psz_name, f_value);
659         vlc_object_release(p_vout);
660         vlc_object_release(p_filter);
661
662         [self restartFilterIfNeeded: psz_filter option: psz_name];
663     }
664 }
665
666 - (void)setVideoFilterProperty: (char *)psz_name forFilter: (char *)psz_filter string: (const char *)psz_value
667 {
668     char *psz_new_value = strdup(psz_value);
669     vout_thread_t *p_vout = getVout();
670     vlc_object_t *p_filter;
671
672     config_PutPsz(p_intf, psz_name, EnsureUTF8(psz_new_value));
673
674     if (p_vout) {
675         p_filter = vlc_object_find_name(pl_Get(p_intf), psz_filter);
676
677         if (!p_filter) {
678             msg_Warn(p_intf, "filter '%s' isn't enabled", psz_filter);
679             vlc_object_release(p_vout);
680             return;
681         }
682         var_SetString(p_filter, psz_name, EnsureUTF8(psz_new_value));
683         vlc_object_release(p_vout);
684         vlc_object_release(p_filter);
685
686         [self restartFilterIfNeeded: psz_filter option: psz_name];
687     }
688
689     free(psz_new_value);
690 }
691
692 - (void)setVideoFilterProperty: (char *)psz_name forFilter: (char *)psz_filter boolean: (BOOL)b_value
693 {
694     vout_thread_t *p_vout = getVout();
695     vlc_object_t *p_filter;
696
697     config_PutInt(p_intf, psz_name, b_value);
698
699     if (p_vout) {
700         p_filter = vlc_object_find_name(pl_Get(p_intf), psz_filter);
701
702         if (!p_filter) {
703             msg_Warn(p_intf, "filter '%s' isn't enabled", psz_filter);
704             vlc_object_release(p_vout);
705             return;
706         }
707         var_SetBool(p_filter, psz_name, b_value);
708         vlc_object_release(p_vout);
709         vlc_object_release(p_filter);
710     }
711 }
712
713 - (NSString *)generateProfileString
714 {
715     return [NSString stringWithFormat:@"%@;%@;%@;%lli;%f;%f;%f;%f;%f;%lli;%f;%@;%lli;%lli;%lli;%lli;%lli;%lli;%@;%lli;%lli;%lli;%lli;%lli;%@;%lli;%@;%lli;%lli;%lli;%lli;%lli",
716             B64EncAndFree(config_GetPsz(p_intf, "video-filter")),
717             B64EncAndFree(config_GetPsz(p_intf, "sub-source")),
718             B64EncAndFree(config_GetPsz(p_intf, "video-splitter")),
719             config_GetInt(p_intf, "hue"),
720             config_GetFloat(p_intf, "contrast"),
721             config_GetFloat(p_intf, "brightness"),
722             config_GetFloat(p_intf, "saturation"),
723             config_GetFloat(p_intf, "gamma"),
724             config_GetFloat(p_intf, "sharpen-sigma"),
725             config_GetInt(p_intf, "gradfun-radius"),
726             config_GetFloat(p_intf, "grain-variance"),
727             B64EncAndFree(config_GetPsz(p_intf, "transform-type")),
728             config_GetInt(p_intf, "puzzle-rows"),
729             config_GetInt(p_intf, "puzzle-cols"),
730             config_GetInt(p_intf, "colorthres-color"),
731             config_GetInt(p_intf, "colorthres-saturationthres"),
732             config_GetInt(p_intf, "colorthres-similaritythres"),
733             config_GetInt(p_intf, "sepia-intensity"),
734             B64EncAndFree(config_GetPsz(p_intf, "gradient-mode")),
735             config_GetInt(p_intf, "gradient-cartoon"),
736             config_GetInt(p_intf, "gradient-type"),
737             config_GetInt(p_intf, "extract-component"),
738             config_GetInt(p_intf, "posterize-level"),
739             config_GetInt(p_intf, "blur-factor"),
740             B64EncAndFree(config_GetPsz(p_intf, "marq-marquee")),
741             config_GetInt(p_intf, "marq-position"),
742             B64EncAndFree(config_GetPsz(p_intf, "logo-file")),
743             config_GetInt(p_intf, "logo-position"),
744             config_GetInt(p_intf, "logo-opacity"),
745             config_GetInt(p_intf, "clone-count"),
746             config_GetInt(p_intf, "wall-rows"),
747             config_GetInt(p_intf, "wall-cols")
748             ];
749 }
750
751 #pragma mark -
752 #pragma mark generic UI code
753
754 - (void)saveCurrentProfile
755 {
756     if (i_old_profile_index == -1)
757         return;
758
759     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
760     /* fetch all the current settings in a uniform string */
761     NSString *newProfile = [self generateProfileString];
762
763     NSMutableArray *workArray = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:@"VideoEffectProfiles"]];
764     if (i_old_profile_index >= [workArray count])
765         return;
766
767     [workArray replaceObjectAtIndex:i_old_profile_index withObject:newProfile];
768     [defaults setObject:[NSArray arrayWithArray:workArray] forKey:@"VideoEffectProfiles"];
769     [workArray release];
770     [defaults synchronize];
771 }
772
773 - (IBAction)toggleWindow:(id)sender
774 {
775     if ([o_window isVisible])
776         [o_window orderOut:sender];
777     else {
778         [o_window setLevel: [[[VLCMain sharedInstance] voutController] currentWindowLevel]];
779         [o_window makeKeyAndOrderFront:sender];
780     }
781 }
782
783 - (IBAction)profileSelectorAction:(id)sender
784 {
785     [self saveCurrentProfile];
786     i_old_profile_index = [o_profile_pop indexOfSelectedItem];
787
788     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
789     NSUInteger selectedProfile = [o_profile_pop indexOfSelectedItem];
790
791     /* disable all current video filters, if a vout is available */
792     vout_thread_t *p_vout = getVout();
793     if (p_vout) {
794         var_SetString(p_vout, "video-filter", "");
795         var_SetString(p_vout, "sub-source", "");
796         var_SetString(p_vout, "video-splitter", "");
797         vlc_object_release(p_vout);
798     }
799
800     /* fetch preset */
801     NSArray *items = [[[defaults objectForKey:@"VideoEffectProfiles"] objectAtIndex:selectedProfile] componentsSeparatedByString:@";"];
802
803     /* filter handling */
804     NSString *tempString = B64DecNSStr([items objectAtIndex:0]);
805     NSArray *tempArray;
806     NSUInteger count;
807
808     /* enable the new filters */
809     config_PutPsz(p_intf, "video-filter", "");
810     if ([tempString length] > 0) {
811         tempArray = [tempString componentsSeparatedByString:@":"];
812         count = [tempArray count];
813         for (NSUInteger x = 0; x < count; x++)
814             [self setVideoFilter:(char *)[[tempArray objectAtIndex:x] UTF8String] on:YES];
815     }
816
817     tempString = B64DecNSStr([items objectAtIndex:1]);
818     /* enable another round of new filters */
819     config_PutPsz(p_intf,"sub-source", "");
820     if ([tempString length] > 0) {
821         tempArray = [tempString componentsSeparatedByString:@":"];
822         count = [tempArray count];
823         for (NSUInteger x = 0; x < count; x++)
824             [self setVideoFilter:(char *)[[tempArray objectAtIndex:x] UTF8String] on:YES];
825     }
826
827     tempString = B64DecNSStr([items objectAtIndex:2]);
828     /* enable another round of new filters */
829     config_PutPsz(p_intf,"video-splitter", "");
830     if ([tempString length] > 0) {
831         tempArray = [tempString componentsSeparatedByString:@":"];
832         count = [tempArray count];
833         for (NSUInteger x = 0; x < count; x++)
834             [self setVideoFilter:(char *)[[tempArray objectAtIndex:x] UTF8String] on:YES];
835     }
836
837     /* try to set filter values on-the-fly and store them appropriately */
838     [self setVideoFilterProperty:"hue" forFilter:"adjust" integer:[[items objectAtIndex:3] intValue]];
839     [self setVideoFilterProperty:"contrast" forFilter:"adjust" float:[[items objectAtIndex:4] floatValue]];
840     [self setVideoFilterProperty:"brightness" forFilter:"adjust" float:[[items objectAtIndex:5] floatValue]];
841     [self setVideoFilterProperty:"saturation" forFilter:"adjust" float:[[items objectAtIndex:6] floatValue]];
842     [self setVideoFilterProperty:"gamma" forFilter:"adjust" float:[[items objectAtIndex:7] floatValue]];
843     [self setVideoFilterProperty:"sharpen-sigma" forFilter:"sharpen" float:[[items objectAtIndex:8] floatValue]];
844     [self setVideoFilterProperty:"gradfun-radius" forFilter:"gradfun" integer:[[items objectAtIndex:9] intValue]];
845     [self setVideoFilterProperty:"grain-variance" forFilter:"grain" float:[[items objectAtIndex:10] floatValue]];
846     [self setVideoFilterProperty:"transform-type" forFilter:"transform" string:[B64DecNSStr([items objectAtIndex:11]) UTF8String]];
847     [self setVideoFilterProperty:"puzzle-rows" forFilter:"puzzle" integer:[[items objectAtIndex:12] intValue]];
848     [self setVideoFilterProperty:"puzzle-cols" forFilter:"puzzle" integer:[[items objectAtIndex:13] intValue]];
849     [self setVideoFilterProperty:"colorthres-color" forFilter:"colorthres" integer:[[items objectAtIndex:14] intValue]];
850     [self setVideoFilterProperty:"colorthres-saturationthres" forFilter:"colorthres" integer:[[items objectAtIndex:15] intValue]];
851     [self setVideoFilterProperty:"colorthres-similaritythres" forFilter:"colorthres" integer:[[items objectAtIndex:16] intValue]];
852     [self setVideoFilterProperty:"sepia-intensity" forFilter:"sepia" integer:[[items objectAtIndex:17] intValue]];
853     [self setVideoFilterProperty:"gradient-mode" forFilter:"gradient" string:[B64DecNSStr([items objectAtIndex:18]) UTF8String]];
854     [self setVideoFilterProperty:"gradient-cartoon" forFilter:"gradient" integer:[[items objectAtIndex:19] intValue]];
855     [self setVideoFilterProperty:"gradient-type" forFilter:"gradient" integer:[[items objectAtIndex:20] intValue]];
856     [self setVideoFilterProperty:"extract-component" forFilter:"extract" integer:[[items objectAtIndex:21] intValue]];
857     [self setVideoFilterProperty:"posterize-level" forFilter:"posterize" integer:[[items objectAtIndex:22] intValue]];
858     [self setVideoFilterProperty:"blur-factor" forFilter:"motionblur" integer:[[items objectAtIndex:23] intValue]];
859     [self setVideoFilterProperty:"marq-marquee" forFilter:"marq" string:[B64DecNSStr([items objectAtIndex:24]) UTF8String]];
860     [self setVideoFilterProperty:"marq-position" forFilter:"marq" integer:[[items objectAtIndex:25] intValue]];
861     [self setVideoFilterProperty:"logo-file" forFilter:"logo" string:[B64DecNSStr([items objectAtIndex:26]) UTF8String]];
862     [self setVideoFilterProperty:"logo-position" forFilter:"logo" integer:[[items objectAtIndex:27] intValue]];
863     [self setVideoFilterProperty:"logo-opacity" forFilter:"logo" integer:[[items objectAtIndex:28] intValue]];
864     [self setVideoFilterProperty:"clone-count" forFilter:"clone" integer:[[items objectAtIndex:29] intValue]];
865     [self setVideoFilterProperty:"wall-rows" forFilter:"wall" integer:[[items objectAtIndex:30] intValue]];
866     [self setVideoFilterProperty:"wall-cols" forFilter:"wall" integer:[[items objectAtIndex:31] intValue]];
867
868     [defaults setInteger:selectedProfile forKey:@"VideoEffectSelectedProfile"];
869     [defaults synchronize];
870
871     [self resetValues];
872 }
873
874 - (IBAction)addProfile:(id)sender
875 {
876     /* show panel */
877     VLCEnterTextPanel * panel = [VLCEnterTextPanel sharedInstance];
878     [panel setTitle: _NS("Duplicate current profile for a new profile")];
879     [panel setSubTitle: _NS("Enter a name for the new profile:")];
880     [panel setCancelButtonLabel: _NS("Cancel")];
881     [panel setOKButtonLabel: _NS("Save")];
882     [panel setTarget:self];
883
884     [panel runModalForWindow:o_window];
885 }
886
887 - (void)panel:(VLCEnterTextPanel *)panel returnValue:(NSUInteger)value text:(NSString *)text
888 {
889     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
890
891     if (value != NSOKButton) {
892         [o_profile_pop selectItemAtIndex:[defaults integerForKey:@"VideoEffectSelectedProfile"]];
893         return;
894     }
895
896     NSArray *profileNames = [defaults objectForKey:@"VideoEffectProfileNames"];
897
898     // duplicate names are not allowed in the popup control
899     if ([text length] == 0 || [profileNames containsObject:text]) {
900         [o_profile_pop selectItemAtIndex:[defaults integerForKey:@"VideoEffectSelectedProfile"]];
901
902         NSAlert *alert = [[[NSAlert alloc] init] autorelease];
903         [alert setAlertStyle:NSCriticalAlertStyle];
904         [alert setMessageText:_NS("Please enter a unique name for the new profile.")];
905         [alert setInformativeText:_NS("Multiple profiles with the same name are not allowed.")];
906
907         [alert beginSheetModalForWindow:o_window
908                           modalDelegate:nil
909                          didEndSelector:nil
910                             contextInfo:nil];
911         return;
912     }
913
914     /* fetch all the current settings in a uniform string */
915     NSString *newProfile = [self generateProfileString];
916
917     /* add string to user defaults as well as a label */
918
919     NSMutableArray *workArray = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:@"VideoEffectProfiles"]];
920     [workArray addObject:newProfile];
921     [defaults setObject:[NSArray arrayWithArray:workArray] forKey:@"VideoEffectProfiles"];
922     [defaults setInteger:[workArray count] - 1 forKey:@"VideoEffectSelectedProfile"];
923     [workArray release];
924
925     workArray = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:@"VideoEffectProfileNames"]];
926     [workArray addObject:text];
927     [defaults setObject:[NSArray arrayWithArray:workArray] forKey:@"VideoEffectProfileNames"];
928     [workArray release];
929
930     /* save defaults */
931     [defaults synchronize];
932
933     /* refresh UI */
934     [self resetProfileSelector];
935 }
936
937 - (IBAction)removeProfile:(id)sender
938 {
939     /* show panel */
940     VLCSelectItemInPopupPanel * panel = [VLCSelectItemInPopupPanel sharedInstance];
941     [panel setTitle:_NS("Remove a preset")];
942     [panel setSubTitle:_NS("Select the preset you would like to remove:")];
943     [panel setOKButtonLabel:_NS("Remove")];
944     [panel setCancelButtonLabel:_NS("Cancel")];
945     [panel setPopupButtonContent:[[NSUserDefaults standardUserDefaults] objectForKey:@"VideoEffectProfileNames"]];
946     [panel setTarget:self];
947
948     [panel runModalForWindow:o_window];
949 }
950
951 - (void)panel:(VLCSelectItemInPopupPanel *)panel returnValue:(NSUInteger)value item:(NSUInteger)item
952 {
953     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
954
955     if (value != NSOKButton) {
956         [o_profile_pop selectItemAtIndex:[defaults integerForKey:@"VideoEffectSelectedProfile"]];
957         return;
958     }
959
960     /* remove selected profile from settings */
961     NSMutableArray *workArray = [[NSMutableArray alloc] initWithArray: [defaults objectForKey:@"VideoEffectProfiles"]];
962     [workArray removeObjectAtIndex:item];
963     [defaults setObject:[NSArray arrayWithArray:workArray] forKey:@"VideoEffectProfiles"];
964     [workArray release];
965
966     workArray = [[NSMutableArray alloc] initWithArray: [defaults objectForKey:@"VideoEffectProfileNames"]];
967     [workArray removeObjectAtIndex:item];
968     [defaults setObject:[NSArray arrayWithArray:workArray] forKey:@"VideoEffectProfileNames"];
969     [workArray release];
970
971     if (i_old_profile_index >= item)
972         [defaults setInteger:i_old_profile_index - 1 forKey:@"VideoEffectSelectedProfile"];
973
974     /* save defaults */
975     [defaults synchronize];
976
977     /* do not save deleted profile */
978     i_old_profile_index = -1;
979     /* refresh UI */
980     [self resetProfileSelector];
981 }
982
983 #pragma mark -
984 #pragma mark basic
985 - (IBAction)enableAdjust:(id)sender
986 {
987     BOOL b_state = [o_adjust_ckb state];
988
989     [self setVideoFilter: "adjust" on: b_state];
990     [o_adjust_brightness_sld setEnabled: b_state];
991     [o_adjust_brightness_ckb setEnabled: b_state];
992     [o_adjust_brightness_lbl setEnabled: b_state];
993     [o_adjust_contrast_sld setEnabled: b_state];
994     [o_adjust_contrast_lbl setEnabled: b_state];
995     [o_adjust_gamma_sld setEnabled: b_state];
996     [o_adjust_gamma_lbl setEnabled: b_state];
997     [o_adjust_hue_sld setEnabled: b_state];
998     [o_adjust_hue_lbl setEnabled: b_state];
999     [o_adjust_saturation_sld setEnabled: b_state];
1000     [o_adjust_saturation_lbl setEnabled: b_state];
1001     [o_adjust_reset_btn setEnabled: b_state];
1002 }
1003
1004 - (IBAction)adjustSliderChanged:(id)sender
1005 {
1006     if (sender == o_adjust_brightness_sld)
1007         [self setVideoFilterProperty: "brightness" forFilter: "adjust" float: [o_adjust_brightness_sld floatValue]];
1008     else if (sender == o_adjust_contrast_sld)
1009         [self setVideoFilterProperty: "contrast" forFilter: "adjust" float: [o_adjust_contrast_sld floatValue]];
1010     else if (sender == o_adjust_gamma_sld)
1011         [self setVideoFilterProperty: "gamma" forFilter: "adjust" float: [o_adjust_gamma_sld floatValue]];
1012     else if (sender == o_adjust_hue_sld)
1013         [self setVideoFilterProperty: "hue" forFilter: "adjust" integer: [o_adjust_hue_sld intValue]];
1014     else if (sender == o_adjust_saturation_sld)
1015         [self setVideoFilterProperty: "saturation" forFilter: "adjust" float: [o_adjust_saturation_sld floatValue]];
1016
1017     if (sender == o_adjust_hue_sld)
1018         [o_adjust_hue_sld setToolTip: [NSString stringWithFormat:@"%i", [o_adjust_hue_sld intValue]]];
1019     else
1020         [sender setToolTip: [NSString stringWithFormat:@"%0.3f", [sender floatValue]]];
1021 }
1022
1023 - (IBAction)enableAdjustBrightnessThreshold:(id)sender
1024 {
1025     if (sender == o_adjust_reset_btn) {
1026         [o_adjust_brightness_sld setFloatValue: 1.0];
1027         [o_adjust_contrast_sld setFloatValue: 1.0];
1028         [o_adjust_gamma_sld setFloatValue: 1.0];
1029         [o_adjust_hue_sld setIntValue: 0];
1030         [o_adjust_saturation_sld setFloatValue: 1.0];
1031         [o_adjust_brightness_sld setToolTip: [NSString stringWithFormat:@"%0.3f", 1.0]];
1032         [o_adjust_contrast_sld setToolTip: [NSString stringWithFormat:@"%0.3f", 1.0]];
1033         [o_adjust_gamma_sld setToolTip: [NSString stringWithFormat:@"%0.3f", 1.0]];
1034         [o_adjust_hue_sld setToolTip: [NSString stringWithFormat:@"%i", 0]];
1035         [o_adjust_saturation_sld setToolTip: [NSString stringWithFormat:@"%0.3f", 1.0]];
1036         [self setVideoFilterProperty: "brightness" forFilter: "adjust" float: 1.0];
1037         [self setVideoFilterProperty: "contrast" forFilter: "adjust" float: 1.0];
1038         [self setVideoFilterProperty: "gamma" forFilter: "adjust" float: 1.0];
1039         [self setVideoFilterProperty: "hue" forFilter: "adjust" integer: 0.0];
1040         [self setVideoFilterProperty: "saturation" forFilter: "adjust" float: 1.0];
1041     } else
1042         config_PutInt(p_intf, "brightness-threshold", [o_adjust_brightness_ckb state]);
1043 }
1044
1045 - (IBAction)enableSharpen:(id)sender
1046 {
1047     BOOL b_state = [o_sharpen_ckb state];
1048
1049     [self setVideoFilter: "sharpen" on: b_state];
1050     [o_sharpen_sld setEnabled: b_state];
1051     [o_sharpen_lbl setEnabled: b_state];
1052 }
1053
1054 - (IBAction)sharpenSliderChanged:(id)sender
1055 {
1056     [self setVideoFilterProperty: "sharpen-sigma" forFilter: "sharpen" float: [sender floatValue]];
1057     [sender setToolTip: [NSString stringWithFormat:@"%0.3f", [sender floatValue]]];
1058 }
1059
1060 - (IBAction)enableBanding:(id)sender
1061 {
1062     BOOL b_state = [o_banding_ckb state];
1063
1064     [self setVideoFilter: "gradfun" on: b_state];
1065     [o_banding_sld setEnabled: b_state];
1066     [o_banding_lbl setEnabled: b_state];
1067 }
1068
1069 - (IBAction)bandingSliderChanged:(id)sender
1070 {
1071     [self setVideoFilterProperty: "gradfun-radius" forFilter: "gradfun" integer: [sender intValue]];
1072     [sender setToolTip: [NSString stringWithFormat:@"%i", [sender intValue]]];
1073 }
1074
1075 - (IBAction)enableGrain:(id)sender
1076 {
1077     BOOL b_state = [o_grain_ckb state];
1078
1079     [self setVideoFilter: "grain" on: b_state];
1080     [o_grain_sld setEnabled: b_state];
1081     [o_grain_lbl setEnabled: b_state];
1082 }
1083
1084 - (IBAction)grainSliderChanged:(id)sender
1085 {
1086     [self setVideoFilterProperty: "grain-variance" forFilter: "grain" float: [sender floatValue]];
1087     [sender setToolTip: [NSString stringWithFormat:@"%0.3f", [sender floatValue]]];
1088 }
1089
1090
1091 #pragma mark -
1092 #pragma mark crop
1093
1094 - (IBAction)cropObjectChanged:(id)sender
1095 {
1096     if ([o_crop_sync_top_bottom_ckb state]) {
1097         if (sender == o_crop_bottom_fld || sender == o_crop_bottom_stp)
1098             [self setCropTopValue: [self cropBottomValue]];
1099         else
1100             [self setCropBottomValue: [self cropTopValue]];
1101     }
1102     if ([o_crop_sync_left_right_ckb state]) {
1103         if (sender == o_crop_right_fld || sender == o_crop_right_stp)
1104             [self setCropLeftValue: [self cropRightValue]];
1105         else
1106             [self setCropRightValue: [self cropLeftValue]];
1107     }
1108
1109     vout_thread_t *p_vout = getVout();
1110     if (p_vout) {
1111         var_SetInteger(p_vout, "crop-top", [o_crop_top_fld intValue]);
1112         var_SetInteger(p_vout, "crop-bottom", [o_crop_bottom_fld intValue]);
1113         var_SetInteger(p_vout, "crop-left", [o_crop_left_fld intValue]);
1114         var_SetInteger(p_vout, "crop-right", [o_crop_right_fld intValue]);
1115         vlc_object_release(p_vout);
1116     }
1117 }
1118
1119 #pragma mark -
1120 #pragma mark geometry
1121 - (IBAction)enableTransform:(id)sender
1122 {
1123     [self setVideoFilter: "transform" on: [o_transform_ckb state]];
1124     [o_transform_pop setEnabled: [o_transform_ckb state]];
1125 }
1126
1127 - (IBAction)transformModifierChanged:(id)sender
1128 {
1129     NSInteger tag = [[o_transform_pop selectedItem] tag];
1130     const char *psz_string = [[NSString stringWithFormat:@"%li", tag] UTF8String];
1131     if (tag == 1)
1132         psz_string = "hflip";
1133     else if (tag == 2)
1134         psz_string = "vflip";
1135
1136     [self setVideoFilterProperty: "transform-type" forFilter: "transform" string: psz_string];
1137 }
1138
1139 - (IBAction)enableZoom:(id)sender
1140 {
1141     [self setVideoFilter: "magnify" on: [o_zoom_ckb state]];
1142 }
1143
1144 - (IBAction)enablePuzzle:(id)sender
1145 {
1146     BOOL b_state = [o_puzzle_ckb state];
1147
1148     [self setVideoFilter: "puzzle" on: b_state];
1149     [o_puzzle_columns_fld setEnabled: b_state];
1150     [o_puzzle_columns_stp setEnabled: b_state];
1151     [o_puzzle_columns_lbl setEnabled: b_state];
1152     [o_puzzle_rows_fld setEnabled: b_state];
1153     [o_puzzle_rows_stp setEnabled: b_state];
1154     [o_puzzle_rows_lbl setEnabled: b_state];
1155 }
1156
1157 - (IBAction)puzzleModifierChanged:(id)sender
1158 {
1159     if (sender == o_puzzle_columns_fld || sender == o_puzzle_columns_stp)
1160         [self setVideoFilterProperty: "puzzle-cols" forFilter: "puzzle" integer: [sender intValue]];
1161     else
1162         [self setVideoFilterProperty: "puzzle-rows" forFilter: "puzzle" integer: [sender intValue]];
1163 }
1164
1165 - (IBAction)enableClone:(id)sender
1166 {
1167     BOOL b_state = [o_clone_ckb state];
1168
1169     if (b_state && [o_wall_ckb state]) {
1170         [o_wall_ckb setState: NSOffState];
1171         [self enableWall: o_wall_ckb];
1172     }
1173
1174     [self setVideoFilter: "clone" on: b_state];
1175     [o_clone_number_lbl setEnabled: b_state];
1176     [o_clone_number_fld setEnabled: b_state];
1177     [o_clone_number_stp setEnabled: b_state];
1178 }
1179
1180 - (IBAction)cloneModifierChanged:(id)sender
1181 {
1182     [self setVideoFilterProperty: "clone-count" forFilter: "clone" integer: [o_clone_number_fld intValue]];
1183 }
1184
1185 - (IBAction)enableWall:(id)sender
1186 {
1187     BOOL b_state = [o_wall_ckb state];
1188
1189     if (b_state && [o_clone_ckb state]) {
1190         [o_clone_ckb setState: NSOffState];
1191         [self enableClone: o_clone_ckb];
1192     }
1193
1194     [self setVideoFilter: "wall" on: b_state];
1195     [o_wall_numofcols_fld setEnabled: b_state];
1196     [o_wall_numofcols_stp setEnabled: b_state];
1197     [o_wall_numofcols_lbl setEnabled: b_state];
1198
1199     [o_wall_numofrows_fld setEnabled: b_state];
1200     [o_wall_numofrows_stp setEnabled: b_state];
1201     [o_wall_numofrows_lbl setEnabled: b_state];
1202 }
1203
1204 - (IBAction)wallModifierChanged:(id)sender
1205 {
1206     if (sender == o_wall_numofcols_fld || sender == o_wall_numofcols_stp)
1207         [self setVideoFilterProperty: "wall-cols" forFilter: "wall" integer: [sender intValue]];
1208     else
1209         [self setVideoFilterProperty: "wall-rows" forFilter: "wall" integer: [sender intValue]];
1210 }
1211
1212 #pragma mark -
1213 #pragma mark color
1214 - (IBAction)enableThreshold:(id)sender
1215 {
1216     BOOL b_state = [o_threshold_ckb state];
1217
1218     [self setVideoFilter: "colorthres" on: b_state];
1219     [o_threshold_color_fld setEnabled: b_state];
1220     [o_threshold_color_lbl setEnabled: b_state];
1221     [o_threshold_saturation_sld setEnabled: b_state];
1222     [o_threshold_saturation_lbl setEnabled: b_state];
1223     [o_threshold_similarity_sld setEnabled: b_state];
1224     [o_threshold_similarity_lbl setEnabled: b_state];
1225 }
1226
1227 - (IBAction)thresholdModifierChanged:(id)sender
1228 {
1229     if (sender == o_threshold_color_fld)
1230         [self setVideoFilterProperty: "colorthres-color" forFilter: "colorthres" integer: [o_threshold_color_fld intValue]];
1231     else if (sender == o_threshold_saturation_sld) {
1232         [self setVideoFilterProperty: "colorthres-saturationthres" forFilter: "colorthres" integer: [o_threshold_saturation_sld intValue]];
1233         [o_threshold_saturation_sld setToolTip: [NSString stringWithFormat:@"%i", [o_threshold_saturation_sld intValue]]];
1234     } else {
1235         [self setVideoFilterProperty: "colorthres-similaritythres" forFilter: "colorthres" integer: [o_threshold_similarity_sld intValue]];
1236         [o_threshold_similarity_sld setToolTip: [NSString stringWithFormat:@"%i", [o_threshold_similarity_sld intValue]]];
1237     }
1238 }
1239
1240 - (IBAction)enableSepia:(id)sender
1241 {
1242     BOOL b_state = [o_sepia_ckb state];
1243
1244     [self setVideoFilter: "sepia" on: b_state];
1245     [o_sepia_fld setEnabled: b_state];
1246     [o_sepia_stp setEnabled: b_state];
1247     [o_sepia_lbl setEnabled: b_state];
1248 }
1249
1250 - (IBAction)sepiaModifierChanged:(id)sender
1251 {
1252     [self setVideoFilterProperty: "sepia-intensity" forFilter: "sepia" integer: [o_sepia_fld intValue]];
1253 }
1254
1255 - (IBAction)enableNoise:(id)sender
1256 {
1257     [self setVideoFilter: "noise" on: [o_noise_ckb state]];
1258 }
1259
1260 - (IBAction)enableGradient:(id)sender
1261 {
1262     BOOL b_state = [o_gradient_ckb state];
1263
1264     [self setVideoFilter: "gradient" on: b_state];
1265     [o_gradient_mode_pop setEnabled: b_state];
1266     [o_gradient_mode_lbl setEnabled: b_state];
1267     [o_gradient_color_ckb setEnabled: b_state];
1268     [o_gradient_cartoon_ckb setEnabled: b_state];
1269 }
1270
1271 - (IBAction)gradientModifierChanged:(id)sender
1272 {
1273     if (sender == o_gradient_mode_pop) {
1274         if ([[o_gradient_mode_pop selectedItem] tag] == 3)
1275             [self setVideoFilterProperty: "gradient-mode" forFilter: "gradient" string: "hough"];
1276         else if ([[o_gradient_mode_pop selectedItem] tag] == 2)
1277             [self setVideoFilterProperty: "gradient-mode" forFilter: "gradient" string: "edge"];
1278         else
1279             [self setVideoFilterProperty: "gradient-mode" forFilter: "gradient" string: "gradient"];
1280     } else if (sender == o_gradient_color_ckb)
1281         [self setVideoFilterProperty: "gradient-type" forFilter: "gradient" integer: [o_gradient_color_ckb state]];
1282     else
1283         [self setVideoFilterProperty: "gradient-cartoon" forFilter: "gradient" boolean: [o_gradient_cartoon_ckb state]];
1284 }
1285
1286 - (IBAction)enableExtract:(id)sender
1287 {
1288     BOOL b_state = [o_extract_ckb state];
1289     [self setVideoFilter: "extract" on: b_state];
1290     [o_extract_fld setEnabled: b_state];
1291     [o_extract_lbl setEnabled: b_state];
1292 }
1293
1294 - (IBAction)extractModifierChanged:(id)sender
1295 {
1296     [self setVideoFilterProperty: "extract-component" forFilter: "extract" integer: [o_extract_fld intValue]];
1297 }
1298
1299 - (IBAction)enableInvert:(id)sender
1300 {
1301     [self setVideoFilter: "invert" on: [o_invert_ckb state]];
1302 }
1303
1304 - (IBAction)enablePosterize:(id)sender
1305 {
1306     BOOL b_state = [o_posterize_ckb state];
1307
1308     [self setVideoFilter: "posterize" on: b_state];
1309     [o_posterize_fld setEnabled: b_state];
1310     [o_posterize_stp setEnabled: b_state];
1311     [o_posterize_lbl setEnabled: b_state];
1312 }
1313
1314 - (IBAction)posterizeModifierChanged:(id)sender
1315 {
1316     [self setVideoFilterProperty: "posterize-level" forFilter: "posterize" integer: [o_posterize_fld intValue]];
1317 }
1318
1319 - (IBAction)enableBlur:(id)sender
1320 {
1321     BOOL b_state = [o_blur_ckb state];
1322
1323     [self setVideoFilter: "motionblur" on: b_state];
1324     [o_blur_sld setEnabled: b_state];
1325     [o_blur_lbl setEnabled: b_state];
1326 }
1327
1328 - (IBAction)blurModifierChanged:(id)sender
1329 {
1330     [self setVideoFilterProperty: "blur-factor" forFilter: "motionblur" integer: [sender intValue]];
1331     [sender setToolTip: [NSString stringWithFormat:@"%i", [sender intValue]]];
1332 }
1333
1334 - (IBAction)enableMotionDetect:(id)sender
1335 {
1336     [self setVideoFilter: "motiondetect" on: [o_motiondetect_ckb state]];
1337 }
1338
1339 - (IBAction)enableWaterEffect:(id)sender
1340 {
1341     [self setVideoFilter: "ripple" on: [o_watereffect_ckb state]];
1342 }
1343
1344 - (IBAction)enableWaves:(id)sender
1345 {
1346     [self setVideoFilter: "wave" on: [o_waves_ckb state]];
1347 }
1348
1349 - (IBAction)enablePsychedelic:(id)sender
1350 {
1351     [self setVideoFilter: "psychedelic" on: [o_psychedelic_ckb state]];
1352 }
1353
1354 #pragma mark -
1355 #pragma mark Miscellaneous
1356 - (IBAction)enableAddText:(id)sender
1357 {
1358     BOOL b_state = [o_addtext_ckb state];
1359
1360     [o_addtext_pos_pop setEnabled: b_state];
1361     [o_addtext_pos_lbl setEnabled: b_state];
1362     [o_addtext_text_lbl setEnabled: b_state];
1363     [o_addtext_text_fld setEnabled: b_state];
1364     [self setVideoFilter: "marq" on: b_state];
1365     [self setVideoFilterProperty: "marq-marquee" forFilter: "marq" string: (char *)[[o_addtext_text_fld stringValue] UTF8String]];
1366     [self setVideoFilterProperty: "marq-position" forFilter: "marq" integer: [[o_addtext_pos_pop selectedItem] tag]];
1367 }
1368
1369 - (IBAction)addTextModifierChanged:(id)sender
1370 {
1371     if (sender == o_addtext_text_fld)
1372         [self setVideoFilterProperty: "marq-marquee" forFilter: "marq" string: (char *)[[o_addtext_text_fld stringValue] UTF8String]];
1373     else
1374         [self setVideoFilterProperty: "marq-position" forFilter: "marq" integer: [[o_addtext_pos_pop selectedItem] tag]];
1375 }
1376
1377 - (IBAction)enableAddLogo:(id)sender
1378 {
1379     BOOL b_state = [o_addlogo_ckb state];
1380
1381     [o_addlogo_pos_pop setEnabled: b_state];
1382     [o_addlogo_pos_lbl setEnabled: b_state];
1383     [o_addlogo_logo_fld setEnabled: b_state];
1384     [o_addlogo_logo_lbl setEnabled: b_state];
1385     [o_addlogo_transparency_sld setEnabled: b_state];
1386     [o_addlogo_transparency_lbl setEnabled: b_state];
1387     [self setVideoFilter: "logo" on: b_state];
1388 }
1389
1390 - (IBAction)addLogoModifierChanged:(id)sender
1391 {
1392     if (sender == o_addlogo_logo_fld)
1393         [self setVideoFilterProperty: "logo-file" forFilter: "logo" string: (char *)[[o_addlogo_logo_fld stringValue] UTF8String]];
1394     else if (sender == o_addlogo_pos_pop)
1395         [self setVideoFilterProperty: "logo-position" forFilter: "logo" integer: [[o_addlogo_pos_pop selectedItem] tag]];
1396     else {
1397         [self setVideoFilterProperty: "logo-opacity" forFilter: "logo" integer: [o_addlogo_transparency_sld intValue]];
1398         [o_addlogo_transparency_sld setToolTip: [NSString stringWithFormat:@"%i", [o_addlogo_transparency_sld intValue]]];
1399     }
1400 }
1401
1402 - (IBAction)enableAnaglyph:(id)sender
1403 {
1404     [self setVideoFilter: "anaglyph" on: [o_anaglyph_ckb state]];
1405 }
1406
1407 @end