Dear Sirs/Madams,
I am new in C programming and in GTK, I made a window with a timer, some buttons and a scalebar,
the scalebar is for adjusting the transaparency of the whole window but does not work and at this moment I am stuck, can you please have a look at the code and if you have any good idea how to do it, I will be very happy, thanks a lot,
Best regards: Pablo
#include <gtk/gtk.h>
int seconds = 0;
guint timerId = 0;
GtkWidget *timer;
static gdouble val = 0.5;
gboolean timerTick(__attribute__((unused)) gpointer userData)
{
static GString *time = NULL;
if(time == NULL)
{
time = g_string_sized_new(10);
}
seconds += 1;
printf("Timer Tick %d \n",seconds);
g_string_printf(time,"%02d:%02d",seconds/60,seconds%60);
gtk_entry_set_text (GTK_ENTRY(timer),time->str);
return TRUE;
}
void stopButtonClicked(__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) gpointer data)
{
g_print("Stop Clicked\n");
if(timerId != 0)
{
g_source_remove(timerId);
timerId = 0;
}
else
g_print("Not Running\n");
}
void startButtonClicked(__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) gpointer data)
{
g_print("Start Clicked\n");
if(timerId == 0)
timerId = g_timeout_add(1000,timerTick,NULL);
else
g_print("Already Running\n");
}
void clearButtonClicked(__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) gpointer data)
{
g_print("Clear Clicked\n");
if(timerId == 0)
{
seconds = 0;
gtk_entry_set_text (GTK_ENTRY(timer),"00:00");
}
else
g_print("Ignored, Running\n");
}
void quitButtonClicked(__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) gpointer data)
{
g_print("Quit Clicked\n");
gtk_main_quit();
}
// Handle the user trying to close the window
gboolean windowDelete(__attribute__((unused)) GtkWidget *widget,
__attribute__((unused)) GdkEvent *event,
__attribute__((unused)) gpointer data)
{
g_print("%s called.\n",__FUNCTION__);
return TRUE; // Returning TRUE stops the window being deleted.
// Returning FALSE allows deletion.
}
void value_changed(GtkRange *range, gpointer window) {
val = gtk_range_get_value(range);
gtk_widget_set_opacity (GTK_WIDGET (window), val);
}
int main ( int argc, char **argv) {
GtkWidget *window;
GtkWidget *box;
GtkWidget *hscale;
GtkWidget *startButton;
GtkWidget *stopButton;
GtkWidget *clearButton;
GtkWidget *quitButton;
gtk_init(&argc , &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 300, 250);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
gtk_window_set_title(GTK_WINDOW(window), "Timer");
box = gtk_button_box_new(GTK_ORIENTATION_VERTICAL);
gtk_container_add(GTK_CONTAINER (window), box);
timer = gtk_entry_new();
gtk_entry_set_text(GTK_ENTRY(timer),"00:00");
gtk_container_add(GTK_CONTAINER (box), timer);
hscale = gtk_hscale_new_with_range(0, 1, 0.1);
gtk_container_add(GTK_CONTAINER (box), hscale);
startButton = gtk_button_new_with_label("START");
gtk_container_add(GTK_CONTAINER (box), startButton);
g_signal_connect(startButton, "clicked", G_CALLBACK (startButtonClicked), NULL);
stopButton = gtk_button_new_with_label("STOP");
gtk_container_add(GTK_CONTAINER (box), stopButton);
g_signal_connect(stopButton, "clicked", G_CALLBACK (stopButtonClicked), NULL);
clearButton = gtk_button_new_with_label("CLEAR");
gtk_container_add(GTK_CONTAINER (box), clearButton);
g_signal_connect(clearButton, "clicked", G_CALLBACK (clearButtonClicked), NULL);
quitButton = gtk_button_new_with_label("QUIT");
gtk_container_add(GTK_CONTAINER (box), quitButton);
g_signal_connect(quitButton, "clicked", G_CALLBACK (quitButtonClicked), NULL);
g_signal_connect(window, "delete_event", G_CALLBACK(windowDelete), NULL);
g_signal_connect(hscale, "value-changed", G_CALLBACK(value_changed), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
} |