PatternTranspose::PatternTranspose(Window *p_parent,Editor *p_editor) : Window(p_parent,Window::MODE_POPUP,Window::SIZE_CENTER) {

	editor=p_editor;
	
	VBoxContainer *vb = new VBoxContainer;
	set_root_frame( vb );
	
	MarginGroup *mg=vb->add(new MarginGroup("Note Relationship"));
	
	for (int i=0;i<12;i++) {
		
		static const char* notes[12]={"C","C#","D","D#","E","F","F#","G","G#","A","A#","B"};
		
		HBoxContainer *hb=mg->add(new HBoxContainer);
		
		LineEdit *le=hb->add(new LineEdit,1);
		le->set_text(notes[i]);
		le->set_editable( false );
		
		hb->add(new HSeparator,2);
		
		trans[i]=hb->add(new ComboBox,2);
		
		for (int j=0;j<12;j++) {
			
			trans[i]->add_string(notes[j]);
		}
		
		trans[i]->select(i);
	}
	
	mg=vb->add(new MarginGroup("Actions"));
	
	HBoxContainer *hb = mg->add(new CenterContainer)->set(new HBoxContainer);
		
	hb->add(new Button("Clear"))->pressed_signal.connect(this,&PatternTranspose::reset);
	hb->add(new VSeparator);
	hb->add(new Button("Lower"))->pressed_signal.connect(this,&PatternTranspose::lower);
	hb->add(new Button("Raise"))->pressed_signal.connect(this,&PatternTranspose::raise);
	hb->add(new VSeparator);
	apply_b=hb->add(new 	Button("Apply"));
	apply_b->pressed_signal.connect(this,&PatternTranspose::apply);

	
	
}
PathsDialog::PathsDialog(Window *p_parent,DefaultPaths *p_paths,ConfigApi *p_config) : Window(p_parent,MODE_POPUP,SIZE_TOPLEVEL_CENTER) {

	
	paths=p_paths;
	

	config=p_config;
	WindowBox *vb = new WindowBox("Default Paths");
	set_root_frame( vb );
	
	MarginGroup *mg = vb->add( new MarginGroup("Song Path:"));

	HBoxContainer *hbc = mg->add(new HBoxContainer);
	
	song_path=hbc->add(new LineEdit,1);
	song_path->text_changed_signal.connect(this,&PathsDialog::paths_text_changed);
	hbc->add(new Button("Browse"))->pressed_signal.connect(this,&PathsDialog::browse_song_path);
	
	mg = vb->add( new MarginGroup("Sample Path:"));

	hbc = mg->add(new HBoxContainer);
	
	sample_path=hbc->add(new LineEdit,1);
	sample_path->text_changed_signal.connect(this,&PathsDialog::paths_text_changed);
	hbc->add(new Button("Browse"))->pressed_signal.connect(this,&PathsDialog::browse_sample_path);
	
	mg = vb->add( new MarginGroup("Instrument Path:"));

	hbc = mg->add(new HBoxContainer);
	
	instrument_path=hbc->add(new LineEdit,1);
	instrument_path->text_changed_signal.connect(this,&PathsDialog::paths_text_changed);
	
	hbc->add(new Button("Browse"))->pressed_signal.connect(this,&PathsDialog::browse_instrument_path);
	
	browser = new FileDialog(get_root());
	browser->dir_selected_signal.connect(this,&PathsDialog::dir_selected );
	
	config->write_entry_signal.connect( this, &PathsDialog::save_slot );
	config->read_entry_signal.connect( this, &PathsDialog::read_slot );
	config->read_finished_signal.connect( this, &PathsDialog::read_finished );
	
	updating=false;
	
	update_paths();
}
SoundDriverDialog::SoundDriverDialog(Window *p_parent,ConfigApi *p_config) : Window(p_parent,MODE_POPUP,SIZE_CENTER) {
	
	config=p_config;
	WindowBox *vb = new WindowBox("Sound Driver");		
	
	set_root_frame( vb );
	
	MarginGroup *mg = vb->add( new MarginGroup("Selected Driver:"));
	sound_driver=mg->add(new ComboBox);
	sound_driver->selected_signal.connect( this, &SoundDriverDialog::sound_driver_changed );
	
	HBoxContainer *hbc = mg->add( new HBoxContainer );
	hbc->add( new Button("Disable"),1)->pressed_signal.connect(this,&SoundDriverDialog::disable);
	hbc->add( new Button("Reset"),1)->pressed_signal.connect(this,&SoundDriverDialog::restart);
	vb->add(new HSeparator);
	mg = vb->add( new MarginGroup("Mixing Frequency:"));
	mix_freq=mg->add(new ComboBox);
	mix_freq->add_string("4096");
	mix_freq->add_string("8192");
	mix_freq->add_string("11025");
	mix_freq->add_string("22050");
	mix_freq->add_string("44100");
	mix_freq->add_string("48000");
	mix_freq->add_string("96000");
	mix_freq->add_string("192000");
	mix_freq->selected_signal.connect( this, &SoundDriverDialog::parameter_changed_i );

	mg = vb->add( new MarginGroup("Format:"));
	bits=mg->add(new ComboBox);
	bits->add_string( "8 Bits" );
	bits->add_string( "16 Bits" );
	bits->add_string( "24/32 Bits" );
	bits->selected_signal.connect( this, &SoundDriverDialog::parameter_changed_i );
	
	stereo=mg->add(new CheckButton("Stereo"));
	stereo->pressed_signal.connect(this, &SoundDriverDialog::parameter_changed );
	
	mg = vb->add( new MarginGroup("Buffer Size (frames):"));
	buffsize=mg->add(new ComboBox);
	buffsize->add_string("128");
	buffsize->add_string("256");
	buffsize->add_string("512");
	buffsize->add_string("1024");
	buffsize->add_string("2048");
	buffsize->add_string("4096");
	buffsize->add_string("8192");
	buffsize->add_string("16384");
	buffsize->add_string("32768");
	buffsize->selected_signal.connect( this, &SoundDriverDialog::parameter_changed_i );
	mg = vb->add( new MarginGroup("Status:"));
	status=mg->add(new LineEdit("Active"));
	status->set_editable( false );
	
	updating=false;
	updating_parameters=false;
	
	config->write_entry_signal.connect( this, &SoundDriverDialog::save_slot );
	config->read_entry_signal.connect( this, &SoundDriverDialog::read_slot );
	config->read_finished_signal.connect( this, &SoundDriverDialog::read_finished );
	
	config_must_enable_driver=false;
	config_driver=-1;
}
MainWindow::MainWindow(Painter *p_painter,Timer *p_timer,Skin *p_skin) : Window(p_painter,p_timer,p_skin) {

	main_vbc = new VBoxContainer;
	set_root_frame(main_vbc);
	
	HBoxContainer *hb = main_vbc->add(new HBoxContainer);
	
	tab_bar = hb->add( new TabBar );
	
	tab_bar->add_tab("Global");
	tab_bar->add_tab("Tracks");
	tab_bar->add_tab("Graph");
	
	hb->add( new VSeparator );
	
	MenuBox * file_menu = hb->add( new MenuBox("File") );
	
	file_menu->item_selected_signal.connect( this, &MainWindow::menu_callback );
	
	file_menu->add_item("New Song",FILE_NEW_SONG);
	file_menu->add_item("Open Song",FILE_OPEN_SONG);
	file_menu->add_item("Save Song",FILE_SAVE_SONG);
	file_menu->add_item("Save Song As",FILE_SAVE_SONG_AS);
	file_menu->add_separator();
	file_menu->add_item("Import MIDI File",FILE_IMPORT_MIDI);
	file_menu->add_item("Export MIDI File",FILE_EXPORT_MIDI);
	file_menu->add_separator();
	file_menu->add_item("Export WAV",FILE_EXPORT_WAV);
	file_menu->add_separator();
	file_menu->add_item("Quit",FILE_QUIT);
	
	MenuBox * track_menu = hb->add( new MenuBox("Track") );
	
	track_menu->item_selected_signal.connect( this, &MainWindow::menu_callback );
	
	track_menu->add_item("Add Pattern Track",TRACK_ADD_PATTERN);
	track_menu->add_item("Add Audio Track",TRACK_ADD_AUDIO);
	track_menu->add_item("Add Automation Track",TRACK_ADD_AUTOMATION);
	
	MenuBox * graph_menu = hb->add( new MenuBox("Graph") );
	graph_menu->add_item("Add Node",GRAPH_ADD_NODE);
	graph_menu->add_separator();
	graph_menu->add_item("Add Layer",GRAPH_ADD_LAYER);
	graph_menu->add_item("Change Layer",GRAPH_CHANGE_LAYER);
	graph_menu->add_item("Erase Layer",GRAPH_ERASE_LAYER);
	
	hb->add( new VSeparator );
	
	hb->add( new MenuButton(get_skin()->get_bitmap(BITMAP_ICON_UNDO)) )->pressed_signal.connect( Method( Method1<int>(this, &MainWindow::menu_callback), (int)EDIT_UNDO ));
	hb->add( new MenuButton(get_skin()->get_bitmap(BITMAP_ICON_REDO)) )->pressed_signal.connect( Method(Method1<int>(this, &MainWindow::menu_callback), (int)EDIT_REDO ));	
	
	hb->add( new VSeparator );

	hb->add( new MenuButton(get_skin()->get_bitmap(BITMAP_CONTROL_RW)) )->pressed_signal.connect( Method( Method1<int>(this, &MainWindow::menu_callback), (int)CONTROL_RW ));
	hb->add( new MenuButton(get_skin()->get_bitmap(BITMAP_CONTROL_PLAY)) )->pressed_signal.connect( Method( Method1<int>(this, &MainWindow::menu_callback), (int)CONTROL_PLAY ));
	hb->add( new MenuButton(get_skin()->get_bitmap(BITMAP_CONTROL_LOOP)) )->pressed_signal.connect( Method( Method1<int>(this, &MainWindow::menu_callback), (int)CONTROL_LOOP ));
	hb->add( new MenuButton(get_skin()->get_bitmap(BITMAP_CONTROL_PAUSE)) )->pressed_signal.connect( Method( Method1<int>(this, &MainWindow::menu_callback), (int)CONTROL_PAUSE ));
	hb->add( new MenuButton(get_skin()->get_bitmap(BITMAP_CONTROL_STOP)) )->pressed_signal.connect( Method( Method1<int>(this, &MainWindow::menu_callback), (int)CONTROL_STOP ));
	hb->add( new MenuButton(get_skin()->get_bitmap(BITMAP_CONTROL_FF)) )->pressed_signal.connect( Method( Method1<int>(this, &MainWindow::menu_callback), (int)CONTROL_FF ));
	hb->add( new MenuButton(get_skin()->get_bitmap(BITMAP_CONTROL_REC)) )->pressed_signal.connect( Method( Method1<int>(this, &MainWindow::menu_callback), (int)CONTROL_REC ));
	hb->add( new MenuButton(get_skin()->get_bitmap(BITMAP_CONTROL_REC_AUTO)) )->pressed_signal.connect( Method( Method1<int>(this, &MainWindow::menu_callback), (int)CONTROL_REC_AUTO ));
	
	hb->add( new VSeparator );
	
	hb->add( new Widget, 1 ); //expand
	
	hb->add( new VSeparator );
	
	MenuBox * tools_menu = hb->add( new MenuBox("Config") );
	
	tools_menu->item_selected_signal.connect( this, &MainWindow::menu_callback );
	
	tools_menu->add_item("Audio && MIDI",TOOL_AUDIO_SETTINGS);
	tools_menu->add_item("Keyboard",TOOL_KEYBOARD);
	tools_menu->add_item("Interface",TOOL_INTERFACE);
	tools_menu->add_item("Paths",TOOL_DEFAULT_PATHS);
	
	MenuBox * help_menu = hb->add( new MenuBox("Help") );	
	
	help_menu->item_selected_signal.connect( this, &MainWindow::menu_callback );
	
	help_menu->add_item("Help!",HELP_HELP);
	help_menu->add_item("About",HELP_ABOUT);
	
	main_stack = main_vbc->add( new StackContainer, 1 );
	
	graph_screen = main_stack->add(new GraphScreen(song.get_audio_graph()) );
	
	
	hb = main_vbc->add( new HBoxContainer );
	
	hb->add( new Icon( get_skin()->get_bitmap( BITMAP_ICON_INFO ) ) );
	
	info_line = hb->add( new LineEdit, 4 );
	
	hb->add( new Icon( get_skin()->get_bitmap( BITMAP_ICON_TIME ) ) );
	
	time_line = hb->add( new LineEdit );
	time_line->set_minimum_size( Size(150,-1));
	
	/* Pages */
	
	
//	global_view_frame =main_stack->add( new GlobalViewFrame(data.editor) );
	//edit_view_frame =main_stack->add( new EditViewFrame(update_notify,data.editor) );
	//rack_view_frame =main_stack->add( new RackViewFrame(update_notify, data.editor) );
	
	tab_bar->tab_changed_signal.connect( main_stack, &StackContainer::raise );
	
	/* UPDATE NOTIFY ASSIGN */
	//update_notify->notify_action_signal.connect(this, &MainWindow::notify_action_slot);
	//update_notify->track_list_changed_signal.connect(this,&MainWindow::rebuild_track_lists);
	
	//edit_view_frame->show_track_dialog_signal.connect( track_dialog, &TrackDialog::show );
	
//	create_keybindings();	
		
	new_track_dialog = new NewTrackDialog(this);
	audio_settings_dialog = new AudioSettingsDialog(this,&update_notify);
	quit=false;
}
SampleScreen::SampleScreen(Tracker *p_tracker,bool p_mini) {

	
	mini_tabbox = p_mini?add(new TabBox,1):0;

	tracker=p_tracker;

	MarginGroup *mg=p_mini?mini_tabbox->add("List",new MarginGroup("Sample List")):add(new MarginGroup("Sample List"),3);
	
	mg->get_label_hb()->add( new Widget, 1);
	edit = mg->get_label_hb()->add( new MenuBox("File.."));
	edit->add_item( "Clear" );
	edit->add_separator();
	edit->add_item( "Load" );
	edit->add_item( "Save" );
	edit->add_separator();
	edit->add_item( "Copy" );
	edit->add_item( "Cut" );
	edit->add_item( "Paste" );
	edit->add_separator();
	edit->add_item( "Make Instrument" );
	edit->item_selected_signal.connect( this, &SampleScreen::sample_file_action_callback );
	
	HBoxContainer *slit_hb=mg->add(new HBoxContainer,1);
	
	sample_status=slit_hb->add( new SampleStatusList(p_tracker),0);
	sample_list=slit_hb->add( new List, 1 );
	slit_hb->add( new VScrollBar, 0 )->set_range( sample_list->get_range() );
	sample_status->set_range( sample_list->get_range() );
	
	for (int i=0;i<Song::MAX_SAMPLES;i++)
		sample_list->add_string( "" );
	
	sample_list->set_number_elements( 1 );
	sample_list->set_number_min_integers( 2 );
	sample_list->set_editable( true );
	sample_list->set_max_string_length( 25 );
	sample_list->set_incremental_search(false);
	
	mg=p_mini?mini_tabbox->add("Edit",new MarginGroup("Edit:")):add(new MarginGroup("Parameters:"),5);
	
	mg->get_label_hb()->add( new Widget, 1);
	
//	mg->get_label_hb()->add( new Widget, 1);
	sample_edit = mg->get_label_hb()->add( new MenuBox("Edit"));
	sample_edit->add_item("Copy",SampleEditorEffects::CMD_COPY);
	sample_edit->add_item("Cut",SampleEditorEffects::CMD_CUT);
	sample_edit->add_item("Paste",SampleEditorEffects::CMD_PASTE);
	sample_edit->add_item("Trim",SampleEditorEffects::CMD_TRIM);
	sample_edit->add_separator();
	sample_edit->add_item("PreLoop Cut",SampleEditorEffects::CMD_PRELOOP_CUT);
	sample_edit->add_item("PostLoop Cut",SampleEditorEffects::CMD_POSTLOOP_CUT);
	sample_edit->add_separator();
	sample_edit->add_item("Selection->Loop",SampleEditorEffects::CMD_SEL_TO_LOOP);
	sample_edit->add_item("Align Loop by Resizing",SampleEditorEffects::CMD_LOOP_TO_BOUNDARY);
	sample_edit->add_separator();
	sample_edit->add_item("Unroll Loop",SampleEditorEffects::CMD_LOOP_UNROLL);
	
	sample_edit->item_selected_signal.connect(this,&SampleScreen::sample_data_edit_menu_callback);
	sample_edit->add_separator();
	sample_edit->add_item("Insert Silence at End",SampleEditorEffects::CMD_ADD_SILENCE_AT_END);
	sample_edit->add_separator();
	sample_edit->add_item("Align Loop (ALL SAMPLES)",SampleEditor::CMD_ALIGN_SALL_SAMPLES);
	sample_edit->add_item("Unroll Loop (ALL SAMPLES)",SampleEditor::CMD_UNROLL_ALL_LOOPS);
	sample_edit->add_item("Convert to 16 (ALL SAMPLES)",SampleEditor::CMD_CONVERT_ALL_TO_16);
	sample_edit->add_item("Amplify All (ALL SAMPLES)",SampleEditor::CMD_AMPLIFY_ALL);
	
	
	sample_fx = mg->get_label_hb()->add( new MenuBox("FX"));
	sample_fx->add_item("Toggle Sign",SampleEditorEffects::CMD_TOGGLE_SIGN);
	sample_fx->add_item("Toggle Depth",SampleEditorEffects::CMD_TOGGLE_DEPTH);
	sample_fx->add_item("Toggle Stereo",SampleEditorEffects::CMD_TOGGLE_STEREO);
	sample_fx->add_item("Swap Stereo Chans",SampleEditorEffects::CMD_SWAP_STEREO_CHANS);
	sample_fx->add_separator();
	sample_fx->add_item("Amplify",SampleEditorEffects::CMD_AMPLIFY);
	sample_fx->add_item("Punch Envelope",SampleEditorEffects::CMD_PUNCH_ENVELOPE);
	sample_fx->add_item("Fade In",SampleEditorEffects::CMD_FADE_IN);
	sample_fx->add_item("Fade Out",SampleEditorEffects::CMD_FADE_OUT);
	sample_fx->add_separator();
	sample_fx->add_item("Center",SampleEditorEffects::CMD_CENTER);
	sample_fx->add_item("Reverse",SampleEditorEffects::CMD_REVERSE);
	sample_fx->add_separator();
	sample_fx->add_item("Fix Looping",SampleEditorEffects::CMD_FIX_LOOPING);
	sample_fx->add_item("Resample (Frames)",SampleEditorEffects::CMD_RESAMPLE);
	sample_fx->add_item("Resample (hz)",SampleEditorEffects::CMD_RESAMPLE_FREQ);
	sample_fx->add_item("Delay Left Channel",SampleEditorEffects::CMD_LEFT_CHAN_DELAY);
	sample_fx->add_item("Match Loop End",SampleEditorEffects::CMD_MATCH_LOOP_END_BY_NOTE);
	sample_fx->add_separator();
	sample_fx->add_item("Reverb",SampleEditor::CMD_REVERB);
	
	sample_fx->item_selected_signal.connect(this,&SampleScreen::sample_data_edit_menu_callback);
	
	sample_properties =mg->add( new VBoxContainer, 1 );
	
	sample_list->selected_signal.connect( this, &SampleScreen::list_sample_selected );
	sample_list->item_edited_signal.connect( this, &SampleScreen::sample_name_edited );
	
	sample_editor =  sample_properties->add( new SampleEditor(tracker->editor, tracker->mixer,p_mini), 1);
	
	if (!p_mini)
		sample_properties->add( new HSeparator,0 );
	/* Sample stuff */
	
	HBoxContainer *hbc = p_mini?sample_editor->get_mini_tabbox()->add("Vol/Pan/Vib",new HBoxContainer):sample_properties->add( new HBoxContainer, 0 );
	
	mg = hbc->add( new MarginGroup("Volume"),1);
	
	GridContainer *gc = mg->add( new GridContainer(3), 1);
	
	gc->add( new Label("Global"), false, false );
	
	global_volume=gc->add( new HSlider, true, false );
	gc->add( new ValueLabel, false, false )->set_range( global_volume->get_range() );
	
	global_volume->get_range()->value_changed_signal.connect( this, &SampleScreen::parameter_changed_p );
	
	gc->add( new Label("Default"), false, false );
	
	default_volume=gc->add( new HSlider, true, false );
	gc->add( new ValueLabel, false, false )->set_range( default_volume->get_range() );
	default_volume->get_range()->value_changed_signal.connect( this, &SampleScreen::parameter_changed_p );
	
	use_default_pan=gc->add( new CheckButton("Pan"), false, false );
	
	use_default_pan->pressed_signal.connect( this , &SampleScreen::parameter_changed );
	
	default_pan=gc->add( new HSlider, true, false );
	gc->add( new ValueLabel, false, false )->set_range( default_pan->get_range() );
	
	default_pan->get_range()->value_changed_signal.connect( this, &SampleScreen::parameter_changed_p );
	
	mg = hbc->add( new MarginGroup("Vibrato"),1);
	
	gc = mg->add( new GridContainer(3), 1);
	
	gc->add( new Label("Wave"), false, false );
	
	vibrato_type=gc->add( new ComboBox, true, false );
	
	vibrato_type->add_string( "Sine" );
	vibrato_type->add_string( "Saw" );
	vibrato_type->add_string( "Square" );
	vibrato_type->add_string( "Random" );
	
	vibrato_type->selected_signal.connect( this, &SampleScreen::parameter_changed_c );
	
	gc->add( new Widget, false, false );
	gc->add( new Label("Speed"), false, false );
	
	vibrato_speed=gc->add( new HSlider, true, false );
	gc->add( new ValueLabel, false, false )->set_range( vibrato_speed->get_range() );
	
	vibrato_speed->get_range()->value_changed_signal.connect( this, &SampleScreen::parameter_changed_p );
	
	gc->add( new Label("Depth"), false, false );
	
	vibrato_depth=gc->add( new HSlider, true, false );
	gc->add( new ValueLabel, false, false )->set_range( vibrato_depth->get_range() );
	
	vibrato_depth->get_range()->value_changed_signal.connect( this, &SampleScreen::parameter_changed_p );
	
	gc->add( new Label("Rate"), false, false );
	
	vibrato_rate=gc->add( new HSlider, true, false );
	gc->add( new ValueLabel, false, false )->set_range( vibrato_rate->get_range() );
	
	vibrato_rate->get_range()->value_changed_signal.connect( this, &SampleScreen::parameter_changed_p );
	
	global_volume->get_range()->set_max(64);
	default_volume->get_range()->set_max(64);
	default_pan->get_range()->set_min(-32);
	default_pan->get_range()->set_max(32);
	vibrato_speed->get_range()->set_max(64);
	vibrato_depth->get_range()->set_max(32);
	vibrato_rate->get_range()->set_max(64);
	
	selecting_outside=false;
	updating=false;	
	selected=0;	
	
	sample_file_dialog=0;
	file_dialog=0;
}