2 terminatorX - realtime audio scratching software
3 Copyright (C) 1999 Alexander König
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 Description: This contains the implementation of the tx_flash_flash widget.
26 #include <gtk/gtkwindow.h>
38 #endif /* __cplusplus */
40 #define MAX_VALUE 32767
41 #define RED_BORDER 25000
42 #define TX_FLASH_DEFAULT_SIZE_X 17
43 #define TX_FLASH_DEFAULT_SIZE_Y 30
50 #define MAX_MAX_CYCLES 20;
53 static void gtk_tx_flash_class_init (GtkTxFlashClass *);
54 static void gtk_tx_flash_init (GtkTxFlash *tx_flash);
55 GtkWidget* gtk_tx_flash_new ();
56 static void gtk_tx_flash_destroy (GtkObject *object);
57 static void gtk_tx_flash_realize (GtkWidget *widget);
58 static void gtk_tx_flash_size_request (GtkWidget *widget, GtkRequisition *requisition);
59 static void gtk_tx_flash_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
60 static gint gtk_tx_flash_expose (GtkWidget *widget, GdkEventExpose *event);
61 //static void gtk_tx_flash_prepare (GtkWidget *widget);
62 //static void gtk_tx_flash_set_level(GtkWidget *widget, f_prec new_value);
66 static GtkWidgetClass *parent_class = NULL;
69 gtk_tx_flash_get_type ()
71 static guint tx_flash_type = 0;
75 GtkTypeInfo tx_flash_info =
79 sizeof (GtkTxFlashClass),
80 (GtkClassInitFunc) gtk_tx_flash_class_init,
81 (GtkObjectInitFunc) gtk_tx_flash_init,
86 tx_flash_type = gtk_type_unique (gtk_widget_get_type (), &tx_flash_info);
93 gtk_tx_flash_class_init (GtkTxFlashClass *gclass)
95 GtkObjectClass *object_class;
96 GtkWidgetClass *widget_class;
98 object_class = (GtkObjectClass*) gclass;
99 widget_class = (GtkWidgetClass*) gclass;
101 parent_class = gtk_type_class (gtk_widget_get_type ());
103 object_class->destroy = gtk_tx_flash_destroy;
105 widget_class->realize = gtk_tx_flash_realize;
106 widget_class->expose_event = gtk_tx_flash_expose;
107 widget_class->size_request = gtk_tx_flash_size_request;
108 widget_class->size_allocate = gtk_tx_flash_size_allocate;
109 // widget_class->button_press_event = gtk_tx_flash_button_press;
110 // widget_class->button_release_event = gtk_tx_flash_button_release;
111 // widget_class->motion_notify_event = gtk_tx_flash_motion_notify;
114 void gtk_tx_flash_mk_col(GtkTxFlash *tx_flash, GdkColor *col, float r, float g, float b)
118 col->red=(gint) (r*max);
119 col->green=(gint) (g*max);
120 col->blue=(gint) (b*max);
121 gdk_colormap_alloc_color (gtk_widget_get_colormap (GTK_WIDGET(tx_flash)), col, 1, 1);
125 gtk_tx_flash_init (GtkTxFlash *tx_flash)
129 priv=gdk_colormap_new(gtk_widget_get_visual(GTK_WIDGET(tx_flash)), 6);
131 gtk_widget_set_colormap(GTK_WIDGET(tx_flash), priv);
133 gtk_tx_flash_mk_col(tx_flash, &tx_flash->black, 0, 0, 0);
134 gtk_tx_flash_mk_col(tx_flash, &tx_flash->red, 1, 0.5, 0.5);
135 gtk_tx_flash_mk_col(tx_flash, &tx_flash->green, 0.5, 1, 0.5);
136 gtk_tx_flash_mk_col(tx_flash, &tx_flash->lightgreen, 0, 0.7, 0);
137 gtk_tx_flash_mk_col(tx_flash, &tx_flash->redgreen, 0.7, 0, 0);
143 GtkTxFlash *tx_flash;
145 tx_flash = gtk_type_new (gtk_tx_flash_get_type ());
147 return GTK_WIDGET (tx_flash);
151 gtk_tx_flash_destroy (GtkObject *object)
153 g_return_if_fail (object != NULL);
154 g_return_if_fail (GTK_IS_TX_FLASH (object));
156 if (GTK_OBJECT_CLASS (parent_class)->destroy)
157 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
161 gtk_tx_flash_realize (GtkWidget *widget)
163 GtkTxFlash *tx_flash;
164 GdkWindowAttr attributes;
165 gint attributes_mask;
167 g_return_if_fail (widget != NULL);
168 g_return_if_fail (GTK_IS_TX_FLASH (widget));
170 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
171 tx_flash = GTK_TX_FLASH (widget);
173 attributes.x = widget->allocation.x;
174 attributes.y = widget->allocation.y;
175 attributes.width = widget->allocation.width;
176 attributes.height = widget->allocation.height;
177 attributes.wclass = GDK_INPUT_OUTPUT;
178 attributes.window_type = GDK_WINDOW_CHILD;
179 attributes.event_mask = gtk_widget_get_events (widget) |
181 attributes.visual = gtk_widget_get_visual (widget);
182 attributes.colormap = gtk_widget_get_colormap (widget);
184 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
186 widget->window = gdk_window_new (widget->parent->window, &attributes, attributes_mask);
188 widget->style = gtk_style_attach (widget->style, widget->window);
190 gdk_window_set_user_data (widget->window, widget);
192 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
196 gtk_tx_flash_size_request (GtkWidget *widget, GtkRequisition *requisition)
198 requisition->width = TX_FLASH_DEFAULT_SIZE_X;
199 requisition->height = TX_FLASH_DEFAULT_SIZE_Y;
203 gtk_tx_flash_prepare (GtkWidget *widget)
205 GtkTxFlash *tx_flash;
207 g_return_if_fail (widget != NULL);
208 g_return_if_fail (GTK_IS_TX_FLASH (widget));
210 tx_flash=GTK_TX_FLASH(widget);
212 tx_flash->levels=(widget->allocation.height-(2*DY))/DLEVEL;
213 tx_flash->last_level=0;
214 tx_flash->level_value=MAX_VALUE/(f_prec) tx_flash->levels;
215 tx_flash->red_level=(RED_BORDER/tx_flash->level_value);
217 tx_flash->x1=DMINIX+S_MINIX+2;
218 tx_flash->x2=widget->allocation.width-tx_flash->x1-1;
223 gtk_tx_flash_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
225 GtkTxFlash *tx_flash;
227 g_return_if_fail (widget != NULL);
228 g_return_if_fail (GTK_IS_TX_FLASH (widget));
229 g_return_if_fail (allocation != NULL);
231 widget->allocation = *allocation;
233 gtk_tx_flash_prepare(widget);
235 if (GTK_WIDGET_REALIZED (widget))
237 tx_flash = GTK_TX_FLASH (widget);
239 gdk_window_move_resize (widget->window,
240 allocation->x, allocation->y,
241 allocation->width, allocation->height);
246 static void gtk_tx_flash_paint_yourself(GtkWidget *widget)
248 GtkTxFlash *tx_flash;
249 gint i, x11, x12,x21, x22, y;
252 tx_flash = GTK_TX_FLASH (widget);
254 gdk_gc_set_foreground(widget->style->fg_gc[widget->state], &tx_flash->black);
256 gdk_draw_rectangle(widget->window, widget->style->fg_gc[widget->state], 1, 0, 0, widget->allocation.width,widget->allocation.height);
258 gdk_gc_set_foreground(widget->style->fg_gc[widget->state], &tx_flash->lightgreen);
261 x21=widget->allocation.width-1-x12;
263 for (i=0, y=widget->allocation.height-DY; i<=tx_flash->levels; y-=DLEVEL, i++)
269 else if (i==tx_flash->red_level-1)
273 else if (i==tx_flash->red_level)
276 gdk_gc_set_foreground(widget->style->fg_gc[widget->state], &tx_flash->redgreen);
278 else if (i==tx_flash->levels)
295 gdk_draw_line(widget->window, widget->style->fg_gc[widget->state], x11, y, x12, y);
296 gdk_draw_line(widget->window, widget->style->fg_gc[widget->state], x21, y, x22, y);
300 static gint gtk_tx_flash_expose (GtkWidget *widget, GdkEventExpose *event)
303 g_return_val_if_fail (widget != NULL, FALSE);
304 g_return_val_if_fail (GTK_IS_TX_FLASH (widget), FALSE);
305 g_return_val_if_fail (event != NULL, FALSE);
307 if (event->count > 0)
310 gtk_tx_flash_prepare(widget);
311 gtk_tx_flash_paint_yourself(widget);
317 gtk_tx_flash_update (GtkTxFlash *tx_flash)
319 g_return_if_fail (tx_flash != NULL);
320 g_return_if_fail (GTK_IS_TX_FLASH (tx_flash));
322 gtk_widget_draw (GTK_WIDGET(tx_flash), NULL);
326 gtk_tx_flash_set_level(GtkWidget *widget, f_prec new_value)
328 GtkTxFlash *tx_flash;
330 int new_level, end_level;
333 g_return_if_fail (widget != NULL);
334 g_return_if_fail (GTK_IS_TX_FLASH (widget));
336 tx_flash = GTK_TX_FLASH (widget);
338 new_level=(int) (new_value/tx_flash->level_value);
340 if (new_level>tx_flash->levels) new_level=tx_flash->levels;
342 // printf ("%f, %i, %i\n", tx_flash->level_value,new_level, tx_flash->last_level);
345 if (new_level>tx_flash->max)
347 tx_flash->max=new_level;
348 tx_flash->max_cycles=MAX_MAX_CYCLES;
352 tx_flash->max_cycles--;
355 if (tx_flash->max_cycles <= 0)
357 y=widget->allocation.height-(DY+(tx_flash->max)*DLEVEL);
358 gdk_gc_set_foreground(widget->style->fg_gc[widget->state], &tx_flash->black);
359 gdk_draw_line(widget->window, widget->style->fg_gc[widget->state], tx_flash->x1, y, tx_flash->x2, y);
365 if (tx_flash->max>tx_flash->red_level)
367 gdk_gc_set_foreground(widget->style->fg_gc[widget->state], &tx_flash->red);
371 gdk_gc_set_foreground(widget->style->fg_gc[widget->state], &tx_flash->green);
373 gdk_draw_line(widget->window, widget->style->fg_gc[widget->state], tx_flash->x1, y, tx_flash->x2, y);
377 if (new_level==tx_flash->last_level) return;
379 if (new_level<tx_flash->last_level) // make it look more realistic
381 new_level=tx_flash->last_level*0.95;
384 if (new_level>tx_flash->last_level)
386 gdk_gc_set_foreground(widget->style->fg_gc[widget->state], &tx_flash->green);
388 for (i=tx_flash->last_level, y=widget->allocation.height-(DY+tx_flash->last_level*DLEVEL); i<=new_level; y-=DLEVEL, i++)
392 if (i>=tx_flash->red_level)
394 gdk_gc_set_foreground(widget->style->fg_gc[widget->state], &tx_flash->red);
398 gdk_draw_line(widget->window, widget->style->fg_gc[widget->state], tx_flash->x1, y, tx_flash->x2, y);
403 gdk_gc_set_foreground(widget->style->fg_gc[widget->state], &tx_flash->black);
405 if (tx_flash->last_level==tx_flash->max)
407 i=tx_flash->last_level-1;
411 i=tx_flash->last_level;
414 for (y=widget->allocation.height-(DY+i*DLEVEL); i>new_level; y+=DLEVEL, i--)
416 gdk_draw_line(widget->window, widget->style->fg_gc[widget->state], tx_flash->x1, y, tx_flash->x2, y);
419 tx_flash->last_level=new_level;
423 gtk_tx_flash_clear (GtkWidget *widget)
425 GtkTxFlash *tx_flash;
427 tx_flash = GTK_TX_FLASH (widget);
430 tx_flash->max_cycles=0;
431 tx_flash->last_level=0;
433 gtk_tx_flash_paint_yourself(widget);
438 #endif /* __cplusplus */