1  /*
2   * E-UAE - The portable Amiga Emulator
3   *
4   * Custom Gtk+ LED widget
5   *
6   * Copyright 2004 Martin Garton
7   * Copyright 2006 Richard Drummond
8   */
9 
10 #include "sysdeps.h"
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <unistd.h>
14 #include <string.h>
15 
16 #include "gcc_warnings.h"
17 GCC_DIAG_OFF(strict-prototypes)
18 #include <gtk/gtk.h>
19 GCC_DIAG_ON(strict-prototypes)
20 
21 #include "led.h"
22 
23 #define LED_W 20
24 #define LED_H 10
25 
26 static void led_init (Led *theled);
27 static void led_class_init (LedClass *class);
28 static gint led_expose (GtkWidget *w, GdkEventExpose *event);
29 static void led_destroy (GtkObject *object);
30 static void led_realize (GtkWidget *widget);
31 static void led_unrealize (GtkWidget *widget);
32 static void led_size_request (GtkWidget *widget, GtkRequisition *requisition);
33 static void led_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
34 
35 
led_get_type()36 GtkType led_get_type ()
37 {
38 	static bool hasLed = false;
39     static GtkType led_type;
40 
41     if (!hasLed) {
42 	static const GtkTypeInfo led_info = {
43 	    (char *) "Led",
44 	    sizeof (Led),
45 	    sizeof (LedClass),
46 	    (GtkClassInitFunc) led_class_init,
47 	    (GtkObjectInitFunc) led_init,
48 	    NULL,
49 	    NULL,
50 	    (GtkClassInitFunc) NULL
51 	};
52 	led_type = gtk_type_unique (gtk_misc_get_type (), &led_info);
53 		hasLed = true;
54     }
55     return led_type;
56 }
57 
58 static GtkObjectClass *parent_class;
59 
led_class_init(LedClass * class)60 static void led_class_init (LedClass *class)
61 {
62     GtkObjectClass *object_class = (GtkObjectClass *) class;
63     GtkWidgetClass *widget_class = (GtkWidgetClass *) class;
64     parent_class = gtk_type_class (gtk_widget_get_type ());
65 
66     object_class->destroy = led_destroy;
67     widget_class->expose_event = led_expose;
68     widget_class->realize = led_realize;
69     widget_class->unrealize = led_unrealize;
70     widget_class->size_request = led_size_request;
71     widget_class->size_allocate = led_size_allocate;
72 }
73 
led_init(Led * theled)74 static void led_init (Led *theled)
75 {
76     theled->color = LED_OFF;
77 }
78 
led_new(void)79 GtkWidget *led_new (void)
80 {
81 //    return gtk_type_new (led_get_type ());
82     Led *w = LED (gtk_type_new (led_get_type()));
83 
84     return GTK_WIDGET (w);
85 }
86 
led_expose(GtkWidget * w,GdkEventExpose * event)87 static gint led_expose (GtkWidget *w, GdkEventExpose *event)
88 {
89     if (w && GTK_WIDGET_DRAWABLE (w)) {
90 	Led *theled = LED (w);
91 	gdk_draw_rectangle (w->window, theled->gc, true, 0, 0,
92 			    w->allocation.width, w->allocation.height);
93     }
94     return 0;
95 }
96 
led_realize(GtkWidget * widget)97 static void led_realize (GtkWidget *widget)
98 {
99     Led *theled;
100     GdkWindowAttr attributes;
101     gint attributes_mask;
102 
103     g_return_if_fail (widget != NULL);
104     g_return_if_fail (IS_LED (widget));
105 
106     GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
107     theled = LED (widget);
108 
109     attributes.x           = widget->allocation.x;
110     attributes.y           = widget->allocation.y;
111     attributes.width       = widget->allocation.width;
112     attributes.height      = widget->allocation.height;
113     attributes.wclass      = GDK_INPUT_OUTPUT;
114     attributes.window_type = GDK_WINDOW_CHILD;
115     attributes.event_mask  = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
116     attributes.visual      = gtk_widget_get_visual (widget);
117     attributes.colormap    = gtk_widget_get_colormap (widget);
118 
119     attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
120     widget->window = gdk_window_new (widget->parent->window, &attributes, attributes_mask);
121 
122     gdk_window_set_user_data (widget->window, widget);
123 
124     theled->gc = gdk_gc_new (widget->window);
125     gdk_gc_set_rgb_fg_color (theled->gc, &theled->color);
126 
127     led_expose (widget, NULL);
128 }
129 
led_unrealize(GtkWidget * widget)130 static void led_unrealize (GtkWidget *widget)
131 {
132     Led *theled = LED (widget);
133 
134     g_object_unref (theled->gc);
135     theled->gc = NULL;
136 
137     GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
138 }
139 
led_size_request(GtkWidget * widget,GtkRequisition * requisition)140 static void led_size_request (GtkWidget *widget, GtkRequisition *requisition)
141 {
142     requisition->width  = LED_W;
143     requisition->height = LED_H;
144 }
145 
led_size_allocate(GtkWidget * widget,GtkAllocation * allocation)146 static void led_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
147 {
148 //    Led *theled = LED (widget);
149 
150     g_return_if_fail (widget != NULL);
151     g_return_if_fail (IS_LED (widget));
152     g_return_if_fail (allocation != NULL);
153 
154     widget->allocation = *allocation;
155 
156     if (GTK_WIDGET_REALIZED (widget)) {
157 	gdk_window_move_resize (widget->window,
158 				allocation->x, allocation->y,
159 				allocation->width, allocation->height);
160      }
161 }
162 
led_destroy(GtkObject * o)163 static void led_destroy (GtkObject *o)
164 {
165     g_return_if_fail (o != NULL);
166     g_return_if_fail (IS_LED (o));
167 
168     // TODO: ?? free any stuff here.
169 
170     GTK_OBJECT_CLASS (parent_class)->destroy (o);
171 }
172 
led_set_color(Led * l,GdkColor col)173 void led_set_color (Led *l, GdkColor col)
174 {
175     l->color = col;
176 
177     if (GTK_WIDGET_REALIZED (l)) {
178 	gdk_gc_set_rgb_fg_color (l->gc, &l->color);
179 	led_expose (GTK_WIDGET (l), NULL);
180     }
181 }
182