Support trimming of region.

This commit is contained in:
Jonathan Moore Liles 2008-02-14 01:40:47 -06:00
parent 4b290d3372
commit 669d638ab0
6 changed files with 160 additions and 9 deletions

View File

@ -4,10 +4,10 @@ CXXFLAGS=-ggdb
LIBS=`fltk-config --ldflags` LIBS=`fltk-config --ldflags`
# CXXFLAGS=`fltk-config -cxxflags` # CXXFLAGS=`fltk-config -cxxflags`
OBJS=Waveform.o main.o OBJS=Waveform.o Region.o main.o
.C.o: .C.o:
$(CXX) $(CXXFLAGS) -c $< -o $@ $(CXX) $(CXXFLAGS) -c $< -o $@
test: Waveform.o main.o test: $(OBJS)
$(CXX) $(CXXFLAGS) $(LIBS) $(OBJS) -o $@ $(CXX) $(CXXFLAGS) $(LIBS) $(OBJS) -o $@

95
Region.C Normal file
View File

@ -0,0 +1,95 @@
/*******************************************************************************/
/* Copyright (C) 2008 Jonathan Moore Liles */
/* */
/* 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; see the file COPYING. If not,write to the Free Software */
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/*******************************************************************************/
#include "Region.H"
#include <FL/fl_draw.H>
#include <FL/Fl.H>
#include <FL/Fl_Group.H>
Region::Region ( int X, int Y, int W, int H, const char *L=0 ) : Waveform( X, Y, W, H, L )
{
align( FL_ALIGN_INSIDE | FL_ALIGN_LEFT | FL_ALIGN_BOTTOM );
labeltype( FL_SHADOW_LABEL );
labelcolor( FL_WHITE );
box( FL_PLASTIC_UP_BOX );
}
int
Region::handle ( int m )
{
if ( Fl_Widget::handle( m ) )
return 1;
switch ( m )
{
case FL_PUSH:
{
int X = Fl::event_x();
int Y = Fl::event_y();
if ( Fl::event_state() & FL_CTRL )
{
switch ( Fl::event_button() )
{
case 1:
{
/* trim */
int d = X - x();
_start += d;
resize( x() + d, y(), w() - d, h() );
redraw();
parent()->redraw();
break;
}
case 3:
{
int d = (x() + w()) - X;
_end -= d;
resize( x(), y(), w() - d, h() );
redraw();
parent()->redraw();
break;
}
default:
return 0;
}
return 1;
}
return 0;
break;
}
default:
return 0;
break;
}
}
void
Region::draw ( void )
{
draw_box();
Waveform::draw();
draw_label();
}

32
Region.H Normal file
View File

@ -0,0 +1,32 @@
/*******************************************************************************/
/* Copyright (C) 2008 Jonathan Moore Liles */
/* */
/* 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; see the file COPYING. If not,write to the Free Software */
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/*******************************************************************************/
#include "Waveform.H"
class Region : public Waveform
{
public:
Region ( int X, int Y, int W, int H, const char *L );
int handle ( int m );
void draw ( void );
};

View File

@ -93,10 +93,19 @@ Waveform::handle ( int m )
return 0; return 0;
} }
int measure = 50;
void void
Waveform::draw ( void ) Waveform::draw ( void )
{ {
draw_box( FL_PLASTIC_UP_BOX, x(), y(), w(), h(), color() ); // draw_box( FL_PLASTIC_UP_BOX, x(), y(), w(), h(), color() );
/* fl_color( fl_lighter( color() ) ); */
/* for ( int nx = x(); nx < x() + w(); ++nx ) */
/* if ( ! (nx % measure) ) */
/* fl_line( nx, y(), nx, y() + h() ); */
int X, Y, W, H; int X, Y, W, H;
@ -118,17 +127,18 @@ Waveform::draw ( int X, int Y, int W, int H )
float _scale = 1; float _scale = 1;
int start = (X - x()) * 2; int start = (_start + (X - x())) * 2;
j = 0; j = 0;
for ( int x = X; x < X + W; ++x ) for ( int x = X; x < X + W; ++x )
{ {
float lo = _peaks[ start + _start + j++ ] * _scale; float lo = _peaks[ start + j++ ] * _scale;
float hi = _peaks[ start + _start + j++ ] * _scale; float hi = _peaks[ start + j++ ] * _scale;
int mid = Y + (H / 2); int mid = Y + (H / 2);
fl_line( x, mid + (H * lo), x, mid + (H * hi) ); fl_line( x, mid + (H * lo), x, mid + (H * hi) );
} }
fl_color( fl_darker( fl_darker( selection_color() ) ) ); fl_color( fl_darker( fl_darker( selection_color() ) ) );
@ -138,7 +148,7 @@ Waveform::draw ( int X, int Y, int W, int H )
j = 0; j = 0;
for ( int x = X; x < X + W; ++x ) for ( int x = X; x < X + W; ++x )
{ {
float v = _peaks[ start + _start + j ] * _scale; float v = _peaks[ start + j ] * _scale;
j += 2; j += 2;
fl_vertex( x, Y + (H / 2) + ((float)H * v )); fl_vertex( x, Y + (H / 2) + ((float)H * v ));
} }
@ -150,7 +160,7 @@ Waveform::draw ( int X, int Y, int W, int H )
j = 1; j = 1;
for ( int x = X; x < X + W; ++x ) for ( int x = X; x < X + W; ++x )
{ {
float v = _peaks[ start + _start + j ] * _scale; float v = _peaks[ start + j ] * _scale;
j += 2; j += 2;
fl_vertex( x, Y + (H / 2) + ((float)H * v )); fl_vertex( x, Y + (H / 2) + ((float)H * v ));
} }

View File

@ -17,6 +17,8 @@
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/*******************************************************************************/ /*******************************************************************************/
#pragma once
#include <FL/Fl_Widget.H> #include <FL/Fl_Widget.H>
#include <FL/fl_draw.H> #include <FL/fl_draw.H>
@ -24,6 +26,7 @@ typedef unsigned long tick_t;
class Waveform : public Fl_Widget class Waveform : public Fl_Widget
{ {
protected:
float *_peaks; float *_peaks;
tick_t _start; tick_t _start;

13
main.C
View File

@ -21,8 +21,11 @@
#include <FL/Fl.H> #include <FL/Fl.H>
#include <FL/Fl_Window.H> #include <FL/Fl_Window.H>
#include <FL/Fl_Scroll.H> #include <FL/Fl_Scroll.H>
#include <FL/Fl_Pack.H>
#include <FL/Fl_Group.H>
#include "Waveform.H" #include "Waveform.H"
#include "Region.H"
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
@ -49,7 +52,12 @@ main ( int argc, char **argv )
Fl_Scroll *scroll = new Fl_Scroll( 0, 0, 800, 600 ); Fl_Scroll *scroll = new Fl_Scroll( 0, 0, 800, 600 );
Waveform *wave = new Waveform( 0, 0, 5000, 100, "foo" ); Fl_Group *pack = new Fl_Group( 0, 0, 5000, 600 );
// pack->type( Fl_Pack::VERTICAL );
pack->box( FL_DOWN_BOX );
Region *wave = new Region( 0, 0, 5000, 100, "foo" );
FILE *fp; FILE *fp;
@ -80,6 +88,9 @@ main ( int argc, char **argv )
wave->selection_color( FL_GREEN ); wave->selection_color( FL_GREEN );
pack->add( wave );
pack->end();
scroll->end(); scroll->end();
main_window->end(); main_window->end();