function sFT = finetuning_drawgui( sFT, channel, pos, fh, callback, varargin )
  if nargin < 1
    sFT = finetuning_init;
  end
  if nargin < 2
    channel = 1;
  end
  if nargin < 3
    pos = [0 0 240 165];
  end
  guiw = pos(3);
  guih = pos(4);
  pos(3:4) = 0;
  if nargin < 4
    fh = figure;
  end
  if nargin < 5
    callback = @disp;
  end
  csSide = {'right ear','left ear'};
  ch = csSide{channel}(1);
  spos = pos;
  vCol = [0.6,0.4,0.4]*(2-channel)+[0.4,0.4,0.6]*(channel-1);
  uicontrol('style','frame','position',pos+[0,0,guiw,guih],...
	    'backgroundcolor',vCol,...
	    'tag',['finetuning_frame_',ch]);
  %uicontrol('style','text','position',pos+[5,guih-30,guiw-45,25],...
  %	    'backgroundcolor',vCol,...
  %	    'String',[csSide{channel},' ',method],'FontSize',14,'FontWeight','bold',...
  %	    'HorizontalAlignment','left',...
  %	    'tag','finetuning_frame');
  uicontrol('style','popupmenu','position',pos+[5,guih-40,guiw-10,35],...
	    'String',{'gain','maxgain'},'Value',1,...
	    'tag',['finetuning_method_',ch],...
	    'callback',@priv_cb_setmethod,...
	    'FontSize',14,'FontWeight','bold');
  global finetuning_sFT;
  finetuning_sFT = sFT;
  ud = struct('ch',ch,'callback',callback,'callback_data',{varargin});
  for k=1:length(sFT.f)
    f = sFT.f(k);
    fmin = 100;
    fmax = 8000;
    xpos = round(9+(k-1)/(length(sFT.f)-1)*(guiw-78));
    if f < 1000
      fstr = sprintf('%g',f);
    else
      fstr = sprintf('%gk',f/1000);
    end
    uicontrol('style','text','string',fstr,...
	      'position',pos+[xpos-5,102,30,16],...
	      'Fontsize',9,'fontweight','bold','backgroundcolor',vCol,...
	      'tag','finetuning_frame');
    uicontrol('style','pushbutton','string','+5',...
	      'position',pos+[xpos+2,84,18,18],...
	      'callback',@priv_cb_cmd_inc,...
	      'UserData',merge_structs(ud,struct('delta',5,'k',k)),...
	      'fontsize',8,...
	      'tag','finetuning_frame');
    uicontrol('style','pushbutton','string','+1',...
	      'position',pos+[xpos,62,22,22],...
	      'callback',@priv_cb_cmd_inc,...
	      'UserData',merge_structs(ud,struct('delta',1,'k',k)),...
	      'tag','finetuning_frame');
    uicontrol('style','text',...
	      'tag',sprintf('finetuning_val_%s_%d',ch,k),...
	      'position',pos+[xpos-5,46,30,16],...
	      'Fontsize',9,'fontweight','bold',...
	      'backgroundcolor',[1,0.87,0.3]);
    uicontrol('style','pushbutton','string','-1',...
	      'position',pos+[xpos,24,22,22],...
	      'callback',@priv_cb_cmd_inc,...
	      'UserData',merge_structs(ud,struct('delta',-1,'k',k)),...
	      'tag','finetuning_frame');
    uicontrol('style','pushbutton','string','-5',...
	      'position',pos+[xpos+2,6,18,18],...
	      'callback',@priv_cb_cmd_inc,...
	      'UserData',merge_structs(ud,struct('delta',-5,'k',k)),...
	      'fontsize',8,...
	      'tag','finetuning_frame');
  end
  xpos = guiw-32;
  uicontrol('style','text','string','bb',...
	    'position',pos+[xpos-5,104,30,16],...
	    'Fontsize',9,'fontweight','bold','backgroundcolor',vCol,...
	    'tag','finetuning_frame');
  uicontrol('style','pushbutton','string','+5',...
	    'position',pos+[xpos+2,86,18,18],...
	    'callback',@priv_cb_cmd_inc,...
	    'UserData',merge_structs(ud,struct('delta',5)),...
	    'fontsize',8,...
	    'tag','finetuning_frame');
  uicontrol('style','pushbutton','string','+1',...
	    'position',pos+[xpos,64,22,22],...
	    'callback',@priv_cb_cmd_inc,...
	    'UserData',merge_structs(ud,struct('delta',1)),...
	    'tag','finetuning_frame');
  uicontrol('style','pushbutton','string','zero!',...
	    'position',pos+[xpos-7,44,34,20],...
	    'Fontsize',9,'fontweight','bold',...
	    'callback',@priv_cb_cmd_bb_zero,...
	    'UserData',ud,...
	    'tag','finetuning_frame');
  uicontrol('style','pushbutton','string','-1',...
	    'position',pos+[xpos,22,22,22],...
	    'callback',@priv_cb_cmd_inc,...
	    'UserData',merge_structs(ud,struct('delta',-1)),...
	    'tag','finetuning_frame');
  uicontrol('style','pushbutton','string','-5',...
	    'position',pos+[xpos+2,4,18,18],...
	    'callback',@priv_cb_cmd_inc,...
	    'UserData',merge_structs(ud,struct('delta',-5)),...
	    'fontsize',8,...
	    'tag','finetuning_frame');
  finetuning_updategui;

function priv_cb_cmd_bb_zero(varargin)
  try
    ud = get(gcbo,'UserData');
    priv_set_val(ud.ch,zeros(size(finetuning_get_val(ud.ch))));
    finetuning_updategui;
    global finetuning_sFT;
    ud.callback(finetuning_sFT,ud.callback_data{:});
  catch
    disp_err_rethrow;
  end

function priv_cb_cmd_inc(varargin)
  try
    ud = get(gcbo,'UserData');
    if isfield(ud,'k')
      priv_set_val(ud.ch,ud.k,finetuning_get_val(ud.ch,ud.k)+ud.delta);
    else
      priv_set_val(ud.ch,finetuning_get_val(ud.ch)+ud.delta);
    end
    finetuning_updategui;
    global finetuning_sFT;
    ud.callback(finetuning_sFT,ud.callback_data{:});
  catch
    disp_err_rethrow;
  end
  
function priv_cb_setmethod(varargin)
  try
  val = get(gcbo,'Value');
  for ch='lr'
    set(findobj('tag',['finetuning_method_',ch]),'Value',val);
  end
  finetuning_updategui;
  catch
    disp_err_rethrow;
  end
  
function priv_set_val( ch, band, val )
  global finetuning_sFT;
  sMethod = finetuning_get_method(ch);
  if ~isempty(sMethod)
    if nargin == 3
      finetuning_sFT.(ch).(sMethod)(band) = val;
    else
      finetuning_sFT.(ch).(sMethod) = band;
    end
  end
  