/*  SpiralSynth
 *  Copyleft (C) 2000 David Griffiths <dave@pawfal.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/ 

#include "SequencerGUI.h"

SequencerGUI::SequencerGUI(Sequencer *o)
{
 int n=0;
 for(int i=0; i<16; i++)
	for(int j=0; j<12; j++)
	{
		EventTable[i][j]=n++;	
	}	
	
	m_seq=o;	
}
		
void SequencerGUI::CreateGUI(int xoff=0, int yoff=0, char *name="")
{
	 Fl_Group* o = GUISeqGroup = new Fl_Group(xoff, yoff, 380, 320, name);
      o->type(1);
	  o->box(FL_UP_BOX);
      o->labeltype(FL_ENGRAVED_LABEL);
      o->align(FL_ALIGN_TOP_LEFT|FL_ALIGN_INSIDE);
	  o->user_data((void*)(this));
       	 
	  for(int i=0; i<16; i++)
	  	for(int j=0; j<12; j++)
		{
		 Event[i][j] = new Fl_Button(50+xoff+(20*i), 20+yoff+(20*j), 19, 19);
     	 Event[i][j]->type(1);
      	 Event[i][j]->selection_color(SpiralInfo::GUI_COLOUR);
		 
		 switch(j)
		 {
		 	case 1:
			case 3:
			case 6:
			case 8:
			case 10: 
				Event[i][j]->color(0);
				break;
			default:
				Event[i][j]->color(7);
				break;
		 }
		 
	  	 Event[i][j]->callback((Fl_Callback*)cb_Event,(void*)&EventTable[i][j]);
		}
		   
	 Speed = new Fl_Knob(12+xoff,  220+yoff, 30, 30, "Speed");
     Speed->color(SpiralInfo::GUI_COLOUR);
	 Speed->type(1);
     Speed->selection_color(0);
     Speed->labelsize(10);
     Speed->maximum(0.5);
     Speed->step(0.01);
     Speed->value(0.05);
	 Speed->callback((Fl_Callback*)cb_Speed);
     
	 Play = new Fl_Button(10+xoff, 270+yoff, 50, 30, "Play");
     Play->type(1);
     Play->selection_color(SpiralInfo::GUI_COLOUR);
	 Play->callback((Fl_Callback*)cb_Play);
   
	 Octave = new Fl_Knob(70+xoff,  270+yoff, 30, 30, "Octave");
     Octave->color(SpiralInfo::GUI_COLOUR);
	 Octave->type(1);
     Octave->selection_color(0);
     Octave->labelsize(10);
     Octave->maximum(5);
     Octave->step(1);
     Octave->value(3);
	 Octave->callback((Fl_Callback*)cb_Octave);
   
	 Length = new Fl_Knob(110+xoff,  270+yoff, 30, 30, "Length");
     Length->color(SpiralInfo::GUI_COLOUR);
	 Length->type(1);
     Length->selection_color(0);
     Length->labelsize(10);
     Length->maximum(15);
     Length->step(1);
     Length->value(15);
	 Length->callback((Fl_Callback*)cb_Length);
	 
     Arp = new Fl_Button(150+xoff, 270+yoff, 50, 30, "Arpeg");
     Arp->type(1);
     Arp->selection_color(SpiralInfo::GUI_COLOUR);
	 Arp->callback((Fl_Callback*)cb_Arp);
	
     ArpType = new Fl_Button(210+xoff, 270+yoff, 50, 30, "AType");
     ArpType->type(1);
     ArpType->selection_color(SpiralInfo::GUI_COLOUR);
	 ArpType->callback((Fl_Callback*)cb_ArpType);
	
	 ArpNotes = new Fl_Knob(270+xoff,  270+yoff, 30, 30, "ArpNotes");
     ArpNotes->color(SpiralInfo::GUI_COLOUR);
	 ArpNotes->type(1);
     ArpNotes->selection_color(0);
     ArpNotes->labelsize(10);
     ArpNotes->maximum(5);
     ArpNotes->step(1);
     ArpNotes->value(2);
	 ArpNotes->callback((Fl_Callback*)cb_ArpNotes);
	 
     o->end(); 
}

//// Callbacks ////

inline void SequencerGUI::cb_Event_i(Fl_Button* o, void *v) 
{
	if (o->value()) m_seq->SetEvent(*(int*)(v));
	else m_seq->UnSetEvent(*(int*)(v));
}
void SequencerGUI::cb_Event(Fl_Button* o, void *v) 
{ ((SequencerGUI*)(o->parent()->user_data()))->cb_Event_i(o,v); }

inline void SequencerGUI::cb_Play_i(Fl_Button* o, void* v) 
{ m_seq->Play(o->value()); }
void SequencerGUI::cb_Play(Fl_Button* o, void* v) 
{ ((SequencerGUI*)(o->parent()->user_data()))->cb_Play_i(o,v); }

inline void SequencerGUI::cb_Speed_i(Fl_Knob* o, void* v) 
{ m_seq->SetSpeed(o->value()); }
void SequencerGUI::cb_Speed(Fl_Knob* o, void* v) 
{ ((SequencerGUI*)(o->parent()->user_data()))->cb_Speed_i(o,v); }

inline void SequencerGUI::cb_Octave_i(Fl_Knob* o, void* v) 
{ m_seq->SetOctave(o->value()); }
void SequencerGUI::cb_Octave(Fl_Knob* o, void* v) 
{ ((SequencerGUI*)(o->parent()->user_data()))->cb_Octave_i(o,v); }

inline void SequencerGUI::cb_Length_i(Fl_Knob* o, void* v) 
{ m_seq->SetLength(o->value()); }
void SequencerGUI::cb_Length(Fl_Knob* o, void* v) 
{ ((SequencerGUI*)(o->parent()->user_data()))->cb_Length_i(o,v); }

inline void SequencerGUI::cb_Arp_i(Fl_Button* o, void* v) 
{ m_seq->SetArp(o->value()); }
void SequencerGUI::cb_Arp(Fl_Button* o, void* v) 
{ ((SequencerGUI*)(o->parent()->user_data()))->cb_Arp_i(o,v); }

inline void SequencerGUI::cb_ArpType_i(Fl_Button* o, void* v) 
{ m_seq->SetArpType(o->value()); }
void SequencerGUI::cb_ArpType(Fl_Button* o, void* v) 
{ ((SequencerGUI*)(o->parent()->user_data()))->cb_ArpType_i(o,v); }

inline void SequencerGUI::cb_ArpNotes_i(Fl_Knob* o, void* v) 
{ m_seq->SetArpNotes(o->value()); }
void SequencerGUI::cb_ArpNotes(Fl_Knob* o, void* v) 
{ ((SequencerGUI*)(o->parent()->user_data()))->cb_ArpNotes_i(o,v); }

