From 669d638ab09c10452fae09767d727ebfcb47f263 Mon Sep 17 00:00:00 2001 From: Jonathan Moore Liles Date: Thu, 14 Feb 2008 01:40:47 -0600 Subject: [PATCH] Support trimming of region. --- Makefile | 4 +-- Region.C | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ Region.H | 32 ++++++++++++++++++ Waveform.C | 22 +++++++++---- Waveform.H | 3 ++ main.C | 13 +++++++- 6 files changed, 160 insertions(+), 9 deletions(-) create mode 100644 Region.C create mode 100644 Region.H diff --git a/Makefile b/Makefile index 0bfd029..aaa0a30 100644 --- a/Makefile +++ b/Makefile @@ -4,10 +4,10 @@ CXXFLAGS=-ggdb LIBS=`fltk-config --ldflags` # CXXFLAGS=`fltk-config -cxxflags` -OBJS=Waveform.o main.o +OBJS=Waveform.o Region.o main.o .C.o: $(CXX) $(CXXFLAGS) -c $< -o $@ -test: Waveform.o main.o +test: $(OBJS) $(CXX) $(CXXFLAGS) $(LIBS) $(OBJS) -o $@ diff --git a/Region.C b/Region.C new file mode 100644 index 0000000..24d6d12 --- /dev/null +++ b/Region.C @@ -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 +#include +#include + +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(); +} diff --git a/Region.H b/Region.H new file mode 100644 index 0000000..402237c --- /dev/null +++ b/Region.H @@ -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 ); +}; diff --git a/Waveform.C b/Waveform.C index 8585ac5..3f7786a 100644 --- a/Waveform.C +++ b/Waveform.C @@ -93,10 +93,19 @@ Waveform::handle ( int m ) return 0; } + +int measure = 50; + 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; @@ -118,17 +127,18 @@ Waveform::draw ( int X, int Y, int W, int H ) float _scale = 1; - int start = (X - x()) * 2; + int start = (_start + (X - x())) * 2; j = 0; for ( int x = X; x < X + W; ++x ) { - float lo = _peaks[ start + _start + j++ ] * _scale; - float hi = _peaks[ start + _start + j++ ] * _scale; + float lo = _peaks[ start + j++ ] * _scale; + float hi = _peaks[ start + j++ ] * _scale; int mid = Y + (H / 2); fl_line( x, mid + (H * lo), x, mid + (H * hi) ); + } fl_color( fl_darker( fl_darker( selection_color() ) ) ); @@ -138,7 +148,7 @@ Waveform::draw ( int X, int Y, int W, int H ) j = 0; for ( int x = X; x < X + W; ++x ) { - float v = _peaks[ start + _start + j ] * _scale; + float v = _peaks[ start + j ] * _scale; j += 2; 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; for ( int x = X; x < X + W; ++x ) { - float v = _peaks[ start + _start + j ] * _scale; + float v = _peaks[ start + j ] * _scale; j += 2; fl_vertex( x, Y + (H / 2) + ((float)H * v )); } diff --git a/Waveform.H b/Waveform.H index 6800131..1135ecd 100644 --- a/Waveform.H +++ b/Waveform.H @@ -17,6 +17,8 @@ /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /*******************************************************************************/ +#pragma once + #include #include @@ -24,6 +26,7 @@ typedef unsigned long tick_t; class Waveform : public Fl_Widget { +protected: float *_peaks; tick_t _start; diff --git a/main.C b/main.C index 56a54e5..96af6e5 100644 --- a/main.C +++ b/main.C @@ -21,8 +21,11 @@ #include #include #include +#include +#include #include "Waveform.H" +#include "Region.H" #include #include @@ -49,7 +52,12 @@ main ( int argc, char **argv ) 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; @@ -80,6 +88,9 @@ main ( int argc, char **argv ) wave->selection_color( FL_GREEN ); + pack->add( wave ); + + pack->end(); scroll->end(); main_window->end();