summaryrefslogtreecommitdiff
path: root/src/gui/elems/sampleEditor/waveTools.cpp
blob: e3c0c13207ce1cf5e3613e58f627d301851e8dc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* -----------------------------------------------------------------------------
 *
 * Giada - Your Hardcore Loopmachine
 *
 * -----------------------------------------------------------------------------
 *
 * Copyright (C) 2010-2017 Giovanni A. Zuliani | Monocasual
 *
 * This file is part of Giada - Your Hardcore Loopmachine.
 *
 * Giada - Your Hardcore Loopmachine 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 3 of the License, or (at your option) any later version.
 *
 * Giada - Your Hardcore Loopmachine 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 Giada - Your Hardcore Loopmachine. If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * -------------------------------------------------------------------------- */


#include "../../../core/const.h"
#ifdef G_OS_MAC  // our Clang still doesn't know about cstdint (c++11 stuff)
	#include <stdint.h>
#else
	#include <cstdint>
#endif
#include <FL/Fl_Menu_Item.H>
#include <FL/Fl_Menu_Button.H>
#include "../../../core/sampleChannel.h"
#include "../../../core/waveFx.h"
#include "../../../glue/sampleEditor.h"
#include "../basics/boxtypes.h"
#include "waveform.h"
#include "waveTools.h"


using namespace giada;


namespace
{
enum class Menu
{
  CUT = 0,
  TRIM,
  SILENCE,
  FADE_IN,
  FADE_OUT,
  SMOOTH_EDGES,
  SET_START_END
};


/* -------------------------------------------------------------------------- */


void menuCallback(Fl_Widget* w, void* v)
{
  geWaveTools* wavetools = static_cast<geWaveTools*>(w);
  Menu selectedItem = (Menu) (intptr_t) v;

  int a = wavetools->waveform->getSelectionA();
  int b = wavetools->waveform->getSelectionB();

  switch (selectedItem) {
  	case Menu::CUT:
      c::sampleEditor::cut(wavetools->ch, a, b);
  		break;
  	case Menu::TRIM:
      c::sampleEditor::trim(wavetools->ch, a, b);
  		break;
  	case Menu::SILENCE:
  		c::sampleEditor::silence(wavetools->ch, a, b);
  		break;	
  	case Menu::FADE_IN:
  		c::sampleEditor::fade(wavetools->ch, a, b, 0);
  		break;
  	case Menu::FADE_OUT:
  		c::sampleEditor::fade(wavetools->ch, a, b, 1);
  		break;
  	case Menu::SMOOTH_EDGES:
  		c::sampleEditor::smoothEdges(wavetools->ch, a, b);
  		break;
  	case Menu::SET_START_END:
  		c::sampleEditor::setStartEnd(wavetools->ch, a, b);
  		break;
  }
}
}; // {anonymous}


/* -------------------------------------------------------------------------- */


geWaveTools::geWaveTools(int x, int y, int w, int h, SampleChannel *ch, const char *l)
	: Fl_Scroll(x, y, w, h, l),
	  ch       (ch)
{
	type(Fl_Scroll::HORIZONTAL_ALWAYS);
	hscrollbar.color(G_COLOR_GREY_2);
	hscrollbar.selection_color(G_COLOR_GREY_4);
	hscrollbar.labelcolor(G_COLOR_LIGHT_1);
	hscrollbar.slider(G_CUSTOM_BORDER_BOX);

	waveform = new geWaveform(x, y, w, h-24, ch);
}



/* -------------------------------------------------------------------------- */


void geWaveTools::updateWaveform()
{
	waveform->refresh();
}


/* -------------------------------------------------------------------------- */


void geWaveTools::redrawWaveformAsync()
{
	if (ch->status & (STATUS_PLAY | STATUS_ENDING))
		waveform->redraw();
}


/* -------------------------------------------------------------------------- */


void geWaveTools::resize(int x, int y, int w, int h)
{
	if (this->w() == w || (this->w() != w && this->h() != h)) {   // vertical or both resize
		Fl_Widget::resize(x, y, w, h);
		waveform->resize(x, y, waveform->w(), h-24);
		updateWaveform();
	}
	else {                                                        // horizontal resize
		Fl_Widget::resize(x, y, w, h);
	}

	if (this->w() > waveform->w())
		waveform->stretchToWindow();

	int offset = waveform->x() + waveform->w() - this->w() - this->x();
	if (offset < 0)
		waveform->position(waveform->x()-offset, this->y());
}


/* -------------------------------------------------------------------------- */


int geWaveTools::handle(int e)
{
	int ret = Fl_Group::handle(e);
	switch (e) {
		case FL_MOUSEWHEEL: {
			waveform->setZoom(Fl::event_dy());
			redraw();
			ret = 1;
			break;
		}
		case FL_PUSH: {
			if (Fl::event_button3()) {  // right button
				openMenu();
				ret = 1;
			}
			break;
		}
	}
	return ret;
}


/* -------------------------------------------------------------------------- */


void geWaveTools::openMenu()
{
	if (!waveform->isSelected())
		return;

	Fl_Menu_Item menu[] = {
		{"Cut",                0, menuCallback, (void*) Menu::CUT},
    {"Trim",               0, menuCallback, (void*) Menu::TRIM},
    {"Silence",            0, menuCallback, (void*) Menu::SILENCE},
    {"Fade in",            0, menuCallback, (void*) Menu::FADE_IN},
    {"Fade out",           0, menuCallback, (void*) Menu::FADE_OUT},
    {"Smooth edges",       0, menuCallback, (void*) Menu::SMOOTH_EDGES},
    {"Set start/end here", 0, menuCallback, (void*) Menu::SET_START_END},
    {0}
	};

  if (ch->status == STATUS_PLAY) {
    menu[(int)Menu::CUT].deactivate();
    menu[(int)Menu::TRIM].deactivate();
  }


	Fl_Menu_Button *b = new Fl_Menu_Button(0, 0, 100, 50);
	b->box(G_CUSTOM_BORDER_BOX);
	b->textsize(G_GUI_FONT_SIZE_BASE);
	b->textcolor(G_COLOR_LIGHT_2);
	b->color(G_COLOR_GREY_2);

	const Fl_Menu_Item *m = menu->popup(Fl::event_x(), Fl::event_y(), 0, 0, b);
  if (m)
    m->do_callback(this, m->user_data());
  return;
}