2 terminatorX - realtime audio scratching software
3 Copyright (C) 1999-2003 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>
30 #include "tX_global.h"
39 #endif /* __cplusplus */
41 #define MAX_VALUE 32767
42 #define RED_BORDER 25000
43 #define TX_FLASH_DEFAULT_SIZE_X 17
44 #define TX_FLASH_DEFAULT_SIZE_Y 30
51 #define MAX_MAX_CYCLES 20;
54 static void gtk_tx_flash_class_init (GtkTxFlashClass *);
55 static void gtk_tx_flash_init (GtkTxFlash *tx_flash);
56 GtkWidget* gtk_tx_flash_new ();
57 static void gtk_tx_flash_destroy (GtkObject *object);
58 static void gtk_tx_flash_realize (GtkWidget *widget);
59 static void gtk_tx_flash_size_request (GtkWidget *widget, GtkRequisition *requisition);
60 static void gtk_tx_flash_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
61 static gint gtk_tx_flash_expose (GtkWidget *widget, GdkEventExpose *event);
62 //static void gtk_tx_flash_prepare (GtkWidget *widget);
63 //static void gtk_tx_flash_set_level(GtkWidget *widget, f_prec new_value);
67 static GtkWidgetClass *parent_class = NULL;
70 gtk_tx_flash_get_type ()
72 static guint tx_flash_type = 0;
76 GtkTypeInfo tx_flash_info =
80 sizeof (GtkTxFlashClass),
81 (GtkClassInitFunc) gtk_tx_flash_class_init,
82 (GtkObjectInitFunc) gtk_tx_flash_init,
88 tx_flash_type = gtk_type_unique (gtk_widget_get_type (), &tx_flash_info);
95 gtk_tx_flash_class_init (GtkTxFlashClass *gclass)
97 GtkObjectClass *object_class;
98 GtkWidgetClass *widget_class;
100 object_class = (GtkObjectClass*) gclass;
101 widget_class = (GtkWidgetClass*) gclass;
103 parent_class = gtk_type_class (gtk_widget_get_type ());
105 object_class->destroy = gtk_tx_flash_destroy;
107 widget_class->realize = gtk_tx_flash_realize;
108 widget_class->expose_event = gtk_tx_flash_expose;
109 widget_class->size_request = gtk_tx_flash_size_request;
110 widget_class->size_allocate = gtk_tx_flash_size_allocate;
111 // widget_class->button_press_event = gtk_tx_flash_button_press;
112 // widget_class->button_release_event = gtk_tx_flash_button_release;
113 // widget_class->motion_notify_event = gtk_tx_flash_motion_notify;
116 void gtk_tx_flash_mk_col(GtkTxFlash *tx_flash, GdkColor *col, float r, float g, float b)
120 col->red=(gint) (r*max);
121 col->green=(gint) (g*max);
122 col->blue=(gint) (b*max);
123 gdk_colormap_alloc_color (gtk_widget_get_colormap (GTK_WIDGET(tx_flash)), col, 1, 1);
127 gtk_tx_flash_init (GtkTxFlash *tx_flash)
131 priv=gdk_colormap_new(gtk_widget_get_visual(GTK_WIDGET(tx_flash)), 6);
133 gtk_widget_set_colormap(GTK_WIDGET(tx_flash), priv);
135 gtk_tx_flash_mk_col(tx_flash, &tx_flash->black, 0, 0, 0);
136 gtk_tx_flash_mk_col(tx_flash, &tx_flash->red, 1, 0.5, 0.5);
137 gtk_tx_flash_mk_col(tx_flash, &tx_flash->green, 0.5, 1, 0.5);
138 gtk_tx_flash_mk_col(tx_flash, &tx_flash->lightgreen, 0, 0.7, 0);
139 gtk_tx_flash_mk_col(tx_flash, &tx_flash->redgreen, 0.7, 0, 0);
145 GtkTxFlash *tx_flash;
147 tx_flash = gtk_type_new (gtk_tx_flash_get_type ());
149 return GTK_WIDGET (tx_flash);
153 gtk_tx_flash_destroy (GtkObject *object)
155 g_return_if_fail (object != NULL);
156 g_return_if_fail (GTK_IS_TX_FLASH (object));
158 if (GTK_OBJECT_CLASS (parent_class)->destroy)
159 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
163 gtk_tx_flash_realize (GtkWidget *widget)
165 GtkTxFlash *tx_flash;
166 GdkWindowAttr attributes;
167 gint attributes_mask;
169 g_return_if_fail (widget != NULL);
170 g_return_if_fail (GTK_IS_TX_FLASH (widget));
172 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
173 tx_flash = GTK_TX_FLASH (widget);
175 attributes.x = widget->allocation.x;
176 attributes.y = widget->allocation.y;
177 attributes.width = widget->allocation.width;
178 attributes.height = widget->allocation.height;
179 attributes.wclass = GDK_INPUT_OUTPUT;
180 attributes.window_type = GDK_WINDOW_CHILD;
181 attributes.event_mask = gtk_widget_get_events (widget) |
183 attributes.visual = gtk_widget_get_visual (widget);
184 attributes.colormap = gtk_widget_get_colormap (widget);
186 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
188 widget->window = gdk_window_new (widget->parent->window, &attributes, attributes_mask);
190 widget->style = gtk_style_attach (widget->style, widget->window);
192 gdk_window_set_user_data (widget->window, widget);
194 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
198 gtk_tx_flash_size_request (GtkWidget *widget, GtkRequisition *requisition)
200 requisition->width = TX_FLASH_DEFAULT_SIZE_X;
201 requisition->height = TX_FLASH_DEFAULT_SIZE_Y;
205 gtk_tx_flash_prepare (GtkWidget *widget)
207 GtkTxFlash *tx_flash;
209 g_return_if_fail (widget != NULL);
210 g_return_if_fail (GTK_IS_TX_FLASH (widget));
212 tx_flash=GTK_TX_FLASH(widget);
214 tx_flash->levels=(widget->allocation.height-(2*DY))/DLEVEL;
215 tx_flash->last_level=0;
216 tx_flash->level_value=MAX_VALUE/(f_prec) tx_flash->levels;
217 tx_flash->red_level=(RED_BORDER/tx_flash->level_value);
219 tx_flash->x1=DMINIX+S_MINIX+2;
220 tx_flash->x2=widget->allocation.width-tx_flash->x1-1;
225 gtk_tx_flash_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
227 GtkTxFlash *tx_flash;
229 g_return_if_fail (widget != NULL);
230 g_return_if_fail (GTK_IS_TX_FLASH (widget));
231 g_return_if_fail (allocation != NULL);
233 widget->allocation = *allocation;
235 gtk_tx_flash_prepare(widget);
237 if (GTK_WIDGET_REALIZED (widget))
239 tx_flash = GTK_TX_FLASH (widget);
241 gdk_window_move_resize (widget->window,
242 allocation->x, allocation->y,
243 allocation->width, allocation->height);
248 static void gtk_tx_flash_paint_yourself(GtkWidget *widget)
250 GtkTxFlash *tx_flash;
251 gint i, x11, x12,x21, x22, y;
254 tx_flash = GTK_TX_FLASH (widget);
256 gdk_gc_set_foreground(widget->style->fg_gc[widget->state], &tx_flash->black);
258 gdk_draw_rectangle(widget->window, widget->style->fg_gc[widget->state], 1, 0, 0, widget->allocation.width,widget->allocation.height);
260 gdk_gc_set_foreground(widget->style->fg_gc[widget->state], &tx_flash->lightgreen);
263 x21=widget->allocation.width-1-x12;
265 for (i=0, y=widget->allocation.height-DY; i<=tx_flash->levels; y-=DLEVEL, i++)
271 else if (i==tx_flash->red_level-1)
275 else if (i==tx_flash->red_level)
278 gdk_gc_set_foreground(widget->style->fg_gc[widget->state], &tx_flash->redgreen);
280 else if (i==tx_flash->levels)
297 gdk_draw_line(widget->window, widget->style->fg_gc[widget->state], x11, y, x12, y);
298 gdk_draw_line(widget->window, widget->style->fg_gc[widget->state], x21, y, x22, y);
302 static gint gtk_tx_flash_expose (GtkWidget *widget, GdkEventExpose *event)
305 g_return_val_if_fail (widget != NULL, FALSE);
306 g_return_val_if_fail (GTK_IS_TX_FLASH (widget), FALSE);
307 g_return_val_if_fail (event != NULL, FALSE);
309 if (event->count > 0)
312 gtk_tx_flash_prepare(widget);
313 gtk_tx_flash_paint_yourself(widget);
319 gtk_tx_flash_set_level(GtkWidget *widget, f_prec new_value)
321 GtkTxFlash *tx_flash;
326 g_return_if_fail (widget != NULL);
327 g_return_if_fail (GTK_IS_TX_FLASH (widget));
329 tx_flash = GTK_TX_FLASH (widget);
331 new_level=(int) (new_value/tx_flash->level_value);
333 if (new_level>tx_flash->levels) new_level=tx_flash->levels;
335 // printf ("%f, %i, %i\n", tx_flash->level_value,new_level, tx_flash->last_level);
338 if (new_level>tx_flash->max)
340 tx_flash->max=new_level;
341 tx_flash->max_cycles=MAX_MAX_CYCLES;
345 tx_flash->max_cycles--;
348 if (tx_flash->max_cycles <= 0)
350 y=widget->allocation.height-(DY+(tx_flash->max)*DLEVEL);
351 gdk_gc_set_foreground(widget->style->fg_gc[widget->state], &tx_flash->black);
352 gdk_draw_line(widget->window, widget->style->fg_gc[widget->state], tx_flash->x1, y, tx_flash->x2, y);
358 if (tx_flash->max>tx_flash->red_level)
360 gdk_gc_set_foreground(widget->style->fg_gc[widget->state], &tx_flash->red);
364 gdk_gc_set_foreground(widget->style->fg_gc[widget->state], &tx_flash->green);
366 gdk_draw_line(widget->window, widget->style->fg_gc[widget->state], tx_flash->x1, y, tx_flash->x2, y);
370 if (new_level==tx_flash->last_level) return;
372 if (new_level<tx_flash->last_level) // make it look more realistic
374 new_level=tx_flash->last_level*globals.flash_response;
377 if (new_level>tx_flash->last_level)
379 gdk_gc_set_foreground(widget->style->fg_gc[widget->state], &tx_flash->green);
381 for (i=tx_flash->last_level, y=widget->allocation.height-(DY+tx_flash->last_level*DLEVEL); i<=new_level; y-=DLEVEL, i++)
385 if (i>=tx_flash->red_level)
387 gdk_gc_set_foreground(widget->style->fg_gc[widget->state], &tx_flash->red);
391 gdk_draw_line(widget->window, widget->style->fg_gc[widget->state], tx_flash->x1, y, tx_flash->x2, y);
396 gdk_gc_set_foreground(widget->style->fg_gc[widget->state], &tx_flash->black);
398 if (tx_flash->last_level==tx_flash->max)
400 i=tx_flash->last_level-1;
404 i=tx_flash->last_level;
407 for (y=widget->allocation.height-(DY+i*DLEVEL); i>new_level; y+=DLEVEL, i--)
409 gdk_draw_line(widget->window, widget->style->fg_gc[widget->state], tx_flash->x1, y, tx_flash->x2, y);
412 tx_flash->last_level=new_level;
416 gtk_tx_flash_clear (GtkWidget *widget)
418 GtkTxFlash *tx_flash;
420 tx_flash = GTK_TX_FLASH (widget);
423 tx_flash->max_cycles=0;
424 tx_flash->last_level=0;
426 gtk_tx_flash_paint_yourself(widget);
431 #endif /* __cplusplus */