#include <stdlib.h>
#include <gtk/gtk.h>
#define MAXTEST 100000000
typedef struct WidgetBank
{
GtkWidget *window;
GtkWidget *fixedspace;
GtkWidget *button;
GtkWidget *PrimeToFind;
GtkWidget *readoutCount;
GtkWidget *readoutPrime;
GtkEntryBuffer *PrimeToFindBuffer;
} Widgets;
int isitprime(unsigned long int index);
static void destroy (GtkWidget *window, gpointer data)
{
gtk_main_quit ();
}
//callback function that starts prime finder and updates labels in main window
static void FindNthPrime (GtkWidget *button, gpointer data)
{
Widgets *widgets = data;
gchar primesfoundresults[100], currentprimesresults[100];
int check = 0, primecount = 0, currentprime = 0;
unsigned long int x = 0;
//get address of text in box
const gchar *PrimeToFind = (gtk_entry_buffer_get_text(widgets->PrimeToFindBuffer));
//put the ascii data at that address into an int
check = atoi(PrimeToFind);
//Here's the actual finding of the Nth prime
for (x = 2; x <= MAXTEST; x++)
{
if (isitprime(x))
{
currentprime = x;
sprintf(currentprimesresults, "Current Prime:%i", currentprime);
gtk_label_set_text((GtkLabel *)widgets->readoutPrime, currentprimesresults);
primecount++;
sprintf(primesfoundresults, "Primes Found:%i", (primecount +1));
gtk_label_set_text((GtkLabel *)widgets->readoutCount, primesfoundresults);
}
if ((primecount+1) == check) {break;}
}
}
int main (int argc, char *argv[])
{
//GtkWidget *window, *fixedspace, *button, *readoutCount, *readoutPrime;
gtk_init (&argc, &argv);
Widgets PE7_Widgets;
//Set up widget pointers
PE7_Widgets.window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
PE7_Widgets.fixedspace = gtk_fixed_new();
PE7_Widgets.button = gtk_button_new_with_label("Start");
PE7_Widgets.readoutCount = gtk_label_new("Primes Found: 0");
PE7_Widgets.readoutPrime = gtk_label_new("Current Prime: 0");
PE7_Widgets.PrimeToFindBuffer = gtk_entry_buffer_new(NULL, -1);
PE7_Widgets.PrimeToFind = gtk_entry_new_with_buffer(PE7_Widgets.PrimeToFindBuffer);
//pack and set up locations
gtk_container_add(GTK_CONTAINER (PE7_Widgets.window), PE7_Widgets.fixedspace);
gtk_fixed_put((GtkFixed *)PE7_Widgets.fixedspace, PE7_Widgets.button, 0, 0);
gtk_fixed_put((GtkFixed *)PE7_Widgets.fixedspace, PE7_Widgets.readoutCount, 0, 30);
gtk_fixed_put((GtkFixed *)PE7_Widgets.fixedspace, PE7_Widgets.readoutPrime, 0, 50);
gtk_fixed_put((GtkFixed *)PE7_Widgets.fixedspace, PE7_Widgets.PrimeToFind, 45, 0);
//connect signals for closing main window and the button
g_signal_connect (G_OBJECT (PE7_Widgets.window), "destroy", G_CALLBACK (destroy), NULL);
g_signal_connect (G_OBJECT (PE7_Widgets.button), "clicked", G_CALLBACK (FindNthPrime), &PE7_Widgets);
//display and wait
gtk_widget_show_all(PE7_Widgets.window);
gtk_main ();
return 0;
}