Debian Codegraph @

examples/gtk/roots_of_unity.c

raw
1 /**
2 * Simple GTK program to demonstrate possible strategies
3 * for MPSolve integration inside a graphical application.
4 *
5 * It uses MPSolve to solve the polynomial x^n - 1 with the n
6 * specfied by the user and then displays the roots on a plot.
7 *
8 * Author: Leonardo Robol <[email protected]>
9 *
10 * Copyright (C) 2001-2013, Dipartimento di Matematica "L. Tonelli", Pisa.
11 * License: http://www.gnu.org/licenses/gpl.html GPL version 3 or higher
12 */
13
14 #include <mps/mps.h>
15 #include <gtk/gtk.h>
16 #include <cairo/cairo.h>
17
18 #if GTK_MAJOR_VERSION < 3
19
20 #ifndef gtk_widget_get_allocated_width
21 #define gtk_widget_get_allocated_width(widget) (widget->allocation.width)
22 #endif
23
24 #ifndef gtk_widget_get_allocated_height
25 #define gtk_widget_get_allocated_height(widget) (widget->allocation.height)
26 #endif
27
28 #endif
29
30 cplx_t * points = NULL;
31
32 int degree = 0;
33
34 GtkWidget * drawing_area = NULL;
35
36 void on_drawing_area_draw (GtkWidget* , cairo_t*);
37
38 #if GTK_MAJOR_VERSION < 3
39 void
40 on_expose_event (GtkWidget * widget, GdkEvent * event)
41 {
42 cairo_t * cr = gdk_cairo_create (widget->window);
43 on_drawing_area_draw (widget, cr);
44 }
45 #endif
46
47 void
48 on_drawing_area_draw (GtkWidget * widget,
49 cairo_t * cr)
50 {
51 int width, height;
52
53 width = gtk_widget_get_allocated_width (widget);
54 height = gtk_widget_get_allocated_height (widget);
55
56 /* Draw background */
57 cairo_rectangle (cr, 0, 0, width, height);
58 cairo_set_source_rgb (cr, 1, 1, 1);
59 cairo_fill (cr);
60
61 /* Draw x and y axes */
62 cairo_set_line_width (cr, 1);
63 cairo_set_source_rgb (cr, 0, 0, 0);
64
65 cairo_move_to (cr, 6, 0.5 * height);
66 cairo_line_to (cr, width - 6, 0.5 * height);
67
68 cairo_move_to (cr, 0.5 * width, height - 6);
69 cairo_line_to (cr, 0.5 * width, 6);
70
71 cairo_stroke (cr);
72
73 /* Draw points if present */
74 if (points)
75 {
76 int i;
77 double x, y;
78 cairo_set_source_rgb (cr, 0.9, 0.1, 0.1);
79 #define PADDING (24)
80
81 /* Check if we can draw in here. */
82 if (width < 2 * PADDING || height < 2 * PADDING)
83 return;
84
85 for (i = 0; i < degree; i++)
86 {
87 x = cplx_Re (points[i]) * (0.5 * width - PADDING) + width / 2;
88 y = -cplx_Im (points[i]) * (0.5 * height - PADDING) + height / 2;
89 cairo_arc (cr, x, y, 1.3, 0, 6.29);
90 cairo_fill (cr);
91 }
92 #undef PADDING
93 }
94 }
95
96 gboolean
97 update_drawing_area (void * user_data)
98 {
99 gtk_widget_queue_draw (drawing_area);
100 return false;
101 }
102
103 void
104 on_polynomial_solved (mps_context * s, GtkButton * button)
105 {
106 points = NULL;
107 mps_context_get_roots_d (s, &points, NULL);
108 degree = mps_context_get_degree (s);
109
110 /* Call the update function from the right thread! */
111 g_idle_add (update_drawing_area, NULL);
112
113 gtk_widget_set_sensitive (GTK_WIDGET (button), true);
114 mps_polynomial_free (s, mps_context_get_active_poly (s));
115 mps_context_free (s);
116 }
117
118 void
119 on_solve_button_clicked (GtkButton * button, GtkSpinButton * spin_button)
120 {
121 int degree = gtk_spin_button_get_value_as_int (spin_button);
122 mps_context * s = mps_context_new ();
123 mps_monomial_poly * p = mps_monomial_poly_new (s, degree);
124
125 /* Please note that here a mutex will be required to avoid
126 * freeing data while it's used to render the view. But it
127 * is not added in this example to avoid complications. */
128 if (points)
129 {
130 cplx_vfree (points);
131 points = NULL;
132 }
133
134 gtk_widget_set_sensitive (GTK_WIDGET (button), false);
135
136 /* Setting the coefficients of the input polynomial */
137 mps_monomial_poly_set_coefficient_d (s, p, 0, -1, 0);
138 mps_monomial_poly_set_coefficient_d (s, p, degree, 1, 0);
139
140 /* Asking MPSolve to solve it asynchronously */
141 mps_context_set_input_poly (s, MPS_POLYNOMIAL (p));
142 mps_mpsolve_async (s, (mps_callback) on_polynomial_solved, button);
143 }
144
145 int
146 main (int argc, char *argv[])
147 {
148 /* Init the GTK environment */
149 gtk_init (&argc, &argv);
150
151 /* The window we are using for this simple example, where all the widgets will
152 * be packed. */
153 GtkWidget * window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
154
155 /* We will use this drawing area to draw the computed roots. */
156 drawing_area = gtk_drawing_area_new ();
157
158 /* This spin button will be used to change the degree of the computed polynomials. */
159 GtkWidget * spin_button = gtk_spin_button_new_with_range (1.0, 10000.0, 1.0);
160 GtkWidget * degree_label = gtk_label_new ("Degree:");
161 GtkWidget * solve_button = gtk_button_new_with_label ("Solve");
162
163 #if GTK_MAJOR_VERSION < 3
164 GtkWidget * degree_box = gtk_hbox_new (FALSE, 0);
165 #else
166 GtkWidget * degree_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
167 #endif
168
169 gtk_box_pack_start (GTK_BOX (degree_box), GTK_WIDGET (degree_label), false, true, 6);
170 gtk_box_pack_start (GTK_BOX (degree_box), GTK_WIDGET (spin_button), true, true, 6);
171 gtk_box_pack_start (GTK_BOX (degree_box), solve_button, false, true, 6);
172
173 #if GTK_MAJOR_VERSION < 3
174 GtkWidget * main_box = gtk_vbox_new (FALSE, 6);
175 #else
176 GtkWidget * main_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
177 #endif
178 gtk_box_pack_start (GTK_BOX (main_box), GTK_WIDGET (degree_box), false, true, 6);
179 gtk_box_pack_start (GTK_BOX (main_box), GTK_WIDGET (drawing_area), true, true, 0);
180
181 gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (main_box));
182 gtk_widget_show_all (GTK_WIDGET (window));
183
184 gtk_window_set_title (GTK_WINDOW (window), "MPSolve's roots-of-unity finder");
185 gtk_widget_set_size_request (window, 420, 280);
186
187 /* Connecting standard callbacks and the one to handle polynomial solving */
188 g_signal_connect (window, "destroy", gtk_main_quit, NULL);
189 g_signal_connect (solve_button, "clicked", G_CALLBACK (on_solve_button_clicked), spin_button);
190
191 #if GTK_MAJOR_VERSION < 3
192 g_signal_connect (drawing_area, "expose-event", G_CALLBACK (on_expose_event), NULL);
193 #else
194 g_signal_connect (drawing_area, "draw", G_CALLBACK (on_drawing_area_draw), NULL);
195 #endif
196
197 gtk_main ();
198
199 return EXIT_SUCCESS;
200 }