/* example-start gtktherm gtktherm.c */ /* GTK - The GIMP Toolkit * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ #include #include #include #include #include #include #include "gtktherm.h" #define SCROLL_DELAY_LENGTH 300 #define THERM_DEFAULT_SIZE 100 /* Forward declarations */ static void gtk_therm_class_init (GtkThermClass *klass); static void gtk_therm_init (GtkTherm *therm); static void gtk_therm_destroy (GtkObject *object); static void gtk_therm_realize (GtkWidget *widget); static void gtk_therm_size_request (GtkWidget *widget, GtkRequisition *requisition); static void gtk_therm_size_allocate (GtkWidget *widget, GtkAllocation *allocation); static gint gtk_therm_expose (GtkWidget *widget, GdkEventExpose *event); static gint gtk_therm_button_press (GtkWidget *widget, GdkEventButton *event); static gint gtk_therm_button_release (GtkWidget *widget, GdkEventButton *event); static gint gtk_therm_motion_notify (GtkWidget *widget, GdkEventMotion *event); static gint gtk_therm_timer (GtkTherm *therm); static void gtk_therm_update_mouse (GtkTherm *therm, gint x, gint y); static void gtk_therm_update (GtkTherm *therm); static void gtk_therm_adjustment_changed (GtkAdjustment *adjustment, gpointer data); static void gtk_therm_adjustment_value_changed (GtkAdjustment *adjustment, gpointer data); /* Local data */ static GtkWidgetClass *parent_class = NULL; guint gtk_therm_get_type () { static guint therm_type = 0; if (!therm_type) { GtkTypeInfo therm_info = { "GtkTherm", sizeof (GtkTherm), sizeof (GtkThermClass), (GtkClassInitFunc) gtk_therm_class_init, (GtkObjectInitFunc) gtk_therm_init, /* (GtkArgSetFunc) NULL, */ /* (GtkArgGetFunc) NULL, */ (GtkClassInitFunc) NULL, }; therm_type = gtk_type_unique (gtk_widget_get_type (), &therm_info); } return therm_type; } static void gtk_therm_class_init (GtkThermClass *class) { GtkObjectClass *object_class; GtkWidgetClass *widget_class; object_class = (GtkObjectClass*) class; widget_class = (GtkWidgetClass*) class; parent_class = gtk_type_class (gtk_widget_get_type ()); object_class->destroy = gtk_therm_destroy; widget_class->realize = gtk_therm_realize; widget_class->expose_event = gtk_therm_expose; widget_class->size_request = gtk_therm_size_request; widget_class->size_allocate = gtk_therm_size_allocate; widget_class->button_press_event = gtk_therm_button_press; widget_class->button_release_event = gtk_therm_button_release; widget_class->motion_notify_event = gtk_therm_motion_notify; } static void gtk_therm_init (GtkTherm *therm) { therm->button = 0; therm->policy = GTK_UPDATE_CONTINUOUS; therm->timer = 0; therm->radius = 0; therm->pointer_width = 0; therm->temp = 0.0; therm->old_value = 0.0; therm->old_lower = 0.0; therm->old_upper = 0.0; therm->adjustment = NULL; } GtkWidget* gtk_therm_new (GtkAdjustment *adjustment) { GtkTherm *therm; therm = gtk_type_new (gtk_therm_get_type ()); if (!adjustment) adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0); gtk_therm_set_adjustment (therm, adjustment); return GTK_WIDGET (therm); } static void gtk_therm_destroy (GtkObject *object) { GtkTherm *therm; g_return_if_fail (object != NULL); g_return_if_fail (GTK_IS_THERM (object)); therm = GTK_THERM (object); if (therm->adjustment) gtk_object_unref (GTK_OBJECT (therm->adjustment)); if (GTK_OBJECT_CLASS (parent_class)->destroy) (* GTK_OBJECT_CLASS (parent_class)->destroy) (object); } GtkAdjustment* gtk_therm_get_adjustment (GtkTherm *therm) { g_return_val_if_fail (therm != NULL, NULL); g_return_val_if_fail (GTK_IS_THERM (therm), NULL); return therm->adjustment; } void gtk_therm_set_update_policy (GtkTherm *therm, GtkUpdateType policy) { g_return_if_fail (therm != NULL); g_return_if_fail (GTK_IS_THERM (therm)); therm->policy = policy; } void gtk_therm_set_adjustment (GtkTherm *therm, GtkAdjustment *adjustment) { g_return_if_fail (therm != NULL); g_return_if_fail (GTK_IS_THERM (therm)); printf("gtk_term_set_adjustment\n"); if (therm->adjustment) { gtk_signal_disconnect_by_data (GTK_OBJECT (therm->adjustment), (gpointer) therm); gtk_object_unref (GTK_OBJECT (therm->adjustment)); } therm->adjustment = adjustment; gtk_object_ref (GTK_OBJECT (therm->adjustment)); gtk_signal_connect (GTK_OBJECT (adjustment), "changed", (GtkSignalFunc) gtk_therm_adjustment_changed, (gpointer) therm); gtk_signal_connect (GTK_OBJECT (adjustment), "value_changed", (GtkSignalFunc) gtk_therm_adjustment_value_changed, (gpointer) therm); therm->old_value = adjustment->value; therm->old_lower = adjustment->lower; therm->old_upper = adjustment->upper; gtk_therm_update (therm); } static void gtk_therm_realize (GtkWidget *widget) { GtkTherm *therm; GdkWindowAttr attributes; gint attributes_mask; GdkColor white = { 0, 0xffff, 0xffff, 0xffff }; GdkColor red = { 0, 0xffff, 0x0000, 0x0000 }; GdkColor green = { 0, 0x0000, 0xffff, 0x0000 }; GdkColor blue = { 0, 0x0000, 0x0000, 0xffff }; GdkColor black = { 0, 0x0000, 0x0000, 0x0000 }; GtkStyle *defstyle, *style; g_return_if_fail (widget != NULL); g_return_if_fail (GTK_IS_THERM (widget)); GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED); therm = GTK_THERM (widget); attributes.x = widget->allocation.x; attributes.y = widget->allocation.y; attributes.width = widget->allocation.width; attributes.height = widget->allocation.height; attributes.wclass = GDK_INPUT_OUTPUT; attributes.window_type = GDK_WINDOW_CHILD; attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK; attributes.visual = gtk_widget_get_visual (widget); attributes.colormap = gtk_widget_get_colormap (widget); attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP; widget->window = gdk_window_new (widget->parent->window, &attributes, attributes_mask); gdk_color_alloc ( gdk_colormap_get_system(), &white ); gdk_color_alloc ( gdk_colormap_get_system(), &black ); gdk_color_alloc ( gdk_colormap_get_system(), &red ); gdk_color_alloc ( gdk_colormap_get_system(), &green ); gdk_color_alloc ( gdk_colormap_get_system(), &blue ); defstyle = gtk_widget_get_default_style(); style = gtk_style_copy( defstyle ); style->fg[GTK_STATE_NORMAL] = black; //style->bg[GTK_STATE_NORMAL] = white; style->fg[0] = blue; style->bg[0] = white; style->bg[1] = green; style->bg[2] = black; style->bg[3] = red; style->bg[4] = blue; gtk_widget_set_style ( widget, style ); /* widget->style = gtk_style_attach (widget->style, widget->window); */ gdk_window_set_user_data (widget->window, widget); gtk_style_set_background ( widget->style, widget->window, GTK_STATE_NORMAL); } static void gtk_therm_size_request (GtkWidget *widget, GtkRequisition *requisition) { requisition->width = THERM_DEFAULT_SIZE; requisition->height = THERM_DEFAULT_SIZE; } static void gtk_therm_size_allocate (GtkWidget *widget, GtkAllocation *allocation) { GtkTherm *therm; g_return_if_fail (widget != NULL); g_return_if_fail (GTK_IS_THERM (widget)); g_return_if_fail (allocation != NULL); widget->allocation = *allocation; therm = GTK_THERM (widget); if (GTK_WIDGET_REALIZED (widget)) { gdk_window_move_resize (widget->window, allocation->x, allocation->y, allocation->width, allocation->height); if( pixmap ) { gdk_pixmap_unref(pixmap); } pixmap = gdk_pixmap_create_from_xpm( widget->window, NULL, &widget->style->bg[GTK_STATE_NORMAL], "./gaugesmall.xpm"); } therm->radius = MIN(allocation->width,allocation->height) * 0.3; therm->pointer_width = therm->radius / 5; } static gint gtk_therm_expose (GtkWidget *widget, GdkEventExpose *event) { GtkTherm *therm; GdkPoint points[6]; gdouble s,c; gdouble theta, last, increment; GtkStyle *blankstyle; gint xc, yc; gint upper, lower; gint tick_length; gint i, inc; char tempbuf[10]; g_return_val_if_fail (widget != NULL, FALSE); g_return_val_if_fail (GTK_IS_THERM (widget), FALSE); g_return_val_if_fail (event != NULL, FALSE); if (event->count > 0) return FALSE; therm = GTK_THERM (widget); gdk_window_clear_area (widget->window, 0, 0, widget->allocation.width, widget->allocation.height); /* gdk_draw_pixmap(widget->window, widget->style->fg_gc[GTK_WIDGET_STATE(widget)], pixmap, event->area.x, event->area.y, event->area.x, event->area.y, event->area.width, event->area.height); */ xc = widget->allocation.width/2; yc = widget->allocation.height - 40 ; printf("xc = %ld yc = %ld \n", xc, yc ); // upper = therm->adjustment->upper; // lower = therm->adjustment->lower; /* Erase old pointer */ /* s = sin(therm->last_temp); c = cos(therm->last_temp); therm->last_temp = therm->temp; points[0].x = xc + s*therm->pointer_width/2; points[0].y = yc + c*therm->pointer_width/2; points[1].x = xc + c*therm->radius; points[1].y = yc - s*therm->radius; points[2].x = xc - s*therm->pointer_width/2; points[2].y = yc - c*therm->pointer_width/2; points[3].x = xc - c*therm->radius/10; points[3].y = yc + s*therm->radius/10; points[4].x = points[0].x; points[4].y = points[0].y; blankstyle = gtk_style_new (); blankstyle->bg_gc[GTK_STATE_NORMAL] = widget->style->bg_gc[GTK_STATE_NORMAL]; blankstyle->dark_gc[GTK_STATE_NORMAL] = widget->style->bg_gc[GTK_STATE_NORMAL]; blankstyle->light_gc[GTK_STATE_NORMAL] = widget->style->bg_gc[GTK_STATE_NORMAL]; blankstyle->black_gc = widget->style->bg_gc[GTK_STATE_NORMAL]; gtk_draw_polygon (blankstyle, widget->window, GTK_STATE_NORMAL, GTK_SHADOW_OUT, points, 5, FALSE); gtk_style_unref(blankstyle); */ /* Draw ticks */ /* if ((upper - lower) == 0) return; increment = (100*M_PI)/(therm->radius*therm->radius); inc = (upper - lower); while (inc < 100) inc *=10; while (inc >= 1000) inc /=10; last = -1; for (i=0; i<=inc; i++) { theta = ((gfloat)i*M_PI/(18*inc/24.) - M_PI/6.); if ((theta - last) < (increment)) continue; last = theta; s = sin(theta); c = cos(theta); tick_length = (i%(inc/10) == 0) ? therm->pointer_width : therm->pointer_width/2; gdk_draw_arc( widget->window, widget->style->fg_gc[widget->state], TRUE, xc + c*(therm->radius - tick_length), yc - s*(therm->radius - tick_length), 5, 5, 0, 360*64); gdk_draw_line (widget->window, widget->style->fg_gc[widget->state], xc + c*(therm->radius - tick_length), yc - s*(therm->radius - tick_length), xc + c*therm->radius, yc - s*therm->radius); } */ /* Draw widget */ /* Draw pillar */ points[0].x = xc - 5; points[0].y = yc + 5; points[1].x = xc - 5; points[1].y = yc - therm->temp; points[2].x = xc + 7; points[2].y = yc - therm->temp; points[3].x = xc + 7; points[3].y = yc + 5; points[4].x = points[0].x; points[4].y = points[0].y; if( therm->temp >= 100 ) { gdk_draw_polygon (widget->window, widget->style->bg_gc[3], TRUE, points, 5); } else { gdk_draw_polygon (widget->window, widget->style->fg_gc[0], TRUE, points, 5); } /* Draw the round blob */ /* Fill the interior of the circle */ gdk_draw_arc( widget->window, widget->style->fg_gc[widget->state], TRUE, xc - 10, yc, 21, 21, 0, 360*64); /* Draw the perimeter of the circle to make it look nicer */ gdk_draw_arc( widget->window, widget->style->fg_gc[widget->state], FALSE, xc - 10, yc, 21, 21, 0, 360*64); sprintf( tempbuf, "%3.0f", therm->temp); printf("Adjustment temp: %s\n", therm->adjustment->value ); printf("String temp: %s\n", tempbuf ); gdk_draw_text( widget->window, widget->style->font, widget->style->text_gc[GTK_STATE_NORMAL], xc - 11, yc + 35, tempbuf, strlen(tempbuf)); return FALSE; } static gint gtk_therm_button_press (GtkWidget *widget, GdkEventButton *event) { GtkTherm *therm; gint dx, dy; double s, c; double d_parallel; double d_perpendicular; g_return_val_if_fail (widget != NULL, FALSE); g_return_val_if_fail (GTK_IS_THERM (widget), FALSE); g_return_val_if_fail (event != NULL, FALSE); therm = GTK_THERM (widget); /* Determine if button press was within pointer region - we do this by computing the parallel and perpendicular distance of the point where the mouse was pressed from the line passing through the pointer */ /* dx = event->x - widget->allocation.width / 2; dy = widget->allocation.height / 2 - event->y; x = therm->temp); y = cos(therm->temp); d_parallel = s*dy + c*dx; d_perpendicular = fabs(s*dx - c*dy); if (!therm->button && (d_perpendicular < therm->pointer_width/2) && (d_parallel > - therm->pointer_width)) { gtk_grab_add (widget); therm->button = event->button; gtk_therm_update_mouse (therm, event->x, event->y); } */ return FALSE; } static gint gtk_therm_button_release (GtkWidget *widget, GdkEventButton *event) { GtkTherm *therm; g_return_val_if_fail (widget != NULL, FALSE); g_return_val_if_fail (GTK_IS_THERM (widget), FALSE); g_return_val_if_fail (event != NULL, FALSE); therm = GTK_THERM (widget); if (therm->button == event->button) { gtk_grab_remove (widget); therm->button = 0; if (therm->policy == GTK_UPDATE_DELAYED) gtk_timeout_remove (therm->timer); if ((therm->policy != GTK_UPDATE_CONTINUOUS) && (therm->old_value != therm->adjustment->value)) gtk_signal_emit_by_name (GTK_OBJECT (therm->adjustment), "value_changed"); } return FALSE; } static gint gtk_therm_motion_notify (GtkWidget *widget, GdkEventMotion *event) { GtkTherm *therm; GdkModifierType mods; gint x, y, mask; g_return_val_if_fail (widget != NULL, FALSE); g_return_val_if_fail (GTK_IS_THERM (widget), FALSE); g_return_val_if_fail (event != NULL, FALSE); therm = GTK_THERM (widget); if (therm->button != 0) { x = event->x; y = event->y; if (event->is_hint || (event->window != widget->window)) gdk_window_get_pointer (widget->window, &x, &y, &mods); switch (therm->button) { case 1: mask = GDK_BUTTON1_MASK; break; case 2: mask = GDK_BUTTON2_MASK; break; case 3: mask = GDK_BUTTON3_MASK; break; default: mask = 0; break; } if (mods & mask) gtk_therm_update_mouse (therm, x,y); } return FALSE; } static gint gtk_therm_timer (GtkTherm *therm) { g_return_val_if_fail (therm != NULL, FALSE); g_return_val_if_fail (GTK_IS_THERM (therm), FALSE); if (therm->policy == GTK_UPDATE_DELAYED) gtk_signal_emit_by_name (GTK_OBJECT (therm->adjustment), "value_changed"); return FALSE; } static void gtk_therm_update_mouse (GtkTherm *therm, gint x, gint y) { gint xc, yc; gfloat old_value; g_return_if_fail (therm != NULL); g_return_if_fail (GTK_IS_THERM (therm)); xc = GTK_WIDGET(therm)->allocation.width / 2; yc = GTK_WIDGET(therm)->allocation.height / 2; old_value = therm->adjustment->value; /* therm->temp = atan2(yc-y, x-xc); if (therm->angle < -M_PI/2.) therm->angle += 2*M_PI; if (therm->angle < -M_PI/6) therm->angle = -M_PI/6; if (therm->angle > 7.*M_PI/6.) therm->angle = 7.*M_PI/6.; therm->adjustment->value = therm->adjustment->lower + (7.*M_PI/6 - therm->angle) * (therm->adjustment->upper - therm->adjustment->lower) / (4.*M_PI/3.); if (therm->adjustment->value != old_value) { if (therm->policy == GTK_UPDATE_CONTINUOUS) { gtk_signal_emit_by_name (GTK_OBJECT (therm->adjustment), "value_changed"); } else { gtk_widget_draw (GTK_WIDGET(therm), NULL); if (therm->policy == GTK_UPDATE_DELAYED) { if (therm->timer) gtk_timeout_remove (therm->timer); therm->timer = gtk_timeout_add (SCROLL_DELAY_LENGTH, (GtkFunction) gtk_therm_timer, (gpointer) therm); } } } */ } static void gtk_therm_update (GtkTherm *therm) { gfloat new_value; printf("gtk_therm_update\n"); g_return_if_fail (therm != NULL); g_return_if_fail (GTK_IS_THERM (therm)); new_value = therm->adjustment->value; if (new_value < therm->adjustment->lower) new_value = therm->adjustment->lower; if (new_value > therm->adjustment->upper) new_value = therm->adjustment->upper; if (new_value != therm->adjustment->value) { therm->adjustment->value = new_value; gtk_signal_emit_by_name (GTK_OBJECT (therm->adjustment), "value_changed"); } therm->temp = therm->adjustment->value; /* therm->angle = 7.*M_PI/6. - (new_value - therm->adjustment->lower) * 4.*M_PI/3. / (therm->adjustment->upper - therm->adjustment->lower); */ gtk_widget_draw (GTK_WIDGET(therm), NULL); } static void gtk_therm_adjustment_changed (GtkAdjustment *adjustment, gpointer data) { GtkTherm *therm; g_return_if_fail (adjustment != NULL); g_return_if_fail (data != NULL); therm = GTK_THERM (data); if ((therm->old_value != adjustment->value) || (therm->old_lower != adjustment->lower) || (therm->old_upper != adjustment->upper)) { gtk_therm_update (therm); therm->old_value = adjustment->value; therm->old_lower = adjustment->lower; therm->old_upper = adjustment->upper; } } static void gtk_therm_adjustment_value_changed (GtkAdjustment *adjustment, gpointer data) { GtkTherm *therm; g_return_if_fail (adjustment != NULL); g_return_if_fail (data != NULL); therm = GTK_THERM (data); if (therm->old_value != adjustment->value) { gtk_therm_update (therm); therm->old_value = adjustment->value; } } /* example-end */