Add countdown functionality with same modes as the
[stopish] / src / stopish-stopwatch.c
index 5129c8d..02f19b7 100644 (file)
@@ -32,10 +32,8 @@ struct timerData {
 };
 
 static struct timerData timerdata;
-static GtkWidget *timerHistoryLabel1 = NULL;
-static GtkWidget *timerHistoryLabel2 = NULL;
-static GtkWidget *timerHistoryLabel3 = NULL;
-static GtkWidget *timerHistoryLabel4 = NULL;
+static GtkWidget *timerHistoryLabel[4];
+static GtkWidget *resetButton;
 static GSList *historyList = NULL;
 static int timerHandle = -1;
 
@@ -47,7 +45,7 @@ static void reset_cb( GtkButton* button, gpointer data );
 
 GtkWindow *stopish_stopwatch_new( void )
 {
-    GtkWidget *window, *button, *button0, *label;
+    GtkWidget *window, *button, *label;
     GtkWidget *vBoxMain, *vBox0, *hBox0;
 
     window = hildon_stackable_window_new(  );
@@ -70,20 +68,20 @@ GtkWindow *stopish_stopwatch_new( void )
     stopish_stopwatch_label_timer_landscape(  );
 
     // history area
-    timerHistoryLabel1 = gtk_label_new( NULL );
-    gtk_label_set_markup( GTK_LABEL( timerHistoryLabel1 ),
+    timerHistoryLabel[0] = gtk_label_new( NULL );
+    gtk_label_set_markup( GTK_LABEL( timerHistoryLabel[0] ),
                           "<span size=\"large\"> </span>" );
-    gtk_box_pack_start( GTK_BOX( vBox0 ), timerHistoryLabel1, FALSE, FALSE, 0 );
-    timerHistoryLabel2 = gtk_label_new( NULL );
-    gtk_box_pack_start( GTK_BOX( vBox0 ), timerHistoryLabel2, FALSE, FALSE, 0 );
-    timerHistoryLabel3 = gtk_label_new( NULL );
-    gtk_label_set_markup( GTK_LABEL( timerHistoryLabel3 ),
+    gtk_box_pack_start( GTK_BOX( vBox0 ), timerHistoryLabel[0], FALSE, FALSE, 0 );
+    timerHistoryLabel[1] = gtk_label_new( NULL );
+    gtk_box_pack_start( GTK_BOX( vBox0 ), timerHistoryLabel[1], FALSE, FALSE, 0 );
+    timerHistoryLabel[2] = gtk_label_new( NULL );
+    gtk_label_set_markup( GTK_LABEL( timerHistoryLabel[2] ),
                           "<span size=\"small\"> </span>" );
-    gtk_box_pack_start( GTK_BOX( vBox0 ), timerHistoryLabel3, FALSE, FALSE, 0 );
-    timerHistoryLabel4 = gtk_label_new( NULL );
-    gtk_label_set_markup( GTK_LABEL( timerHistoryLabel4 ),
+    gtk_box_pack_start( GTK_BOX( vBox0 ), timerHistoryLabel[2], FALSE, FALSE, 0 );
+    timerHistoryLabel[3] = gtk_label_new( NULL );
+    gtk_label_set_markup( GTK_LABEL( timerHistoryLabel[3] ),
                           "<span size=\"x-small\"> </span>" );
-    gtk_box_pack_start( GTK_BOX( vBox0 ), timerHistoryLabel4, FALSE, FALSE, 0 );
+    gtk_box_pack_start( GTK_BOX( vBox0 ), timerHistoryLabel[3], FALSE, FALSE, 0 );
 
     gtk_container_add( GTK_CONTAINER( vBoxMain ), vBox0 );
 
@@ -99,18 +97,18 @@ GtkWindow *stopish_stopwatch_new( void )
     button = hildon_button_new_with_text( HILDON_SIZE_HALFSCREEN_WIDTH,
                                           HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
                                           "Start", NULL );
-    button0 = hildon_button_new_with_text( HILDON_SIZE_HALFSCREEN_WIDTH,
-                                           HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
-                                           "Reset", NULL );
+    resetButton = hildon_button_new_with_text( HILDON_SIZE_HALFSCREEN_WIDTH,
+                                               HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
+                                               "Reset", NULL );
     g_signal_connect( G_OBJECT( button ), "clicked",
-                      G_CALLBACK( start_cb ), button0 );
+                      G_CALLBACK( start_cb ), resetButton );
     gtk_container_add( GTK_CONTAINER( hBox0 ), button );
 
     // reset button
-    gtk_widget_set_sensitive( button0, FALSE );
-    g_signal_connect( G_OBJECT( button0 ), "clicked",
+    gtk_widget_set_sensitive( resetButton, FALSE );
+    g_signal_connect( G_OBJECT( resetButton ), "clicked",
                       G_CALLBACK( reset_cb ), button );
-    gtk_container_add( GTK_CONTAINER( hBox0 ), button0 );
+    gtk_container_add( GTK_CONTAINER( hBox0 ), resetButton );
 
     gtk_box_pack_start( GTK_BOX( vBoxMain ), hBox0, FALSE, FALSE, 0 );
 
@@ -122,6 +120,15 @@ GtkWindow *stopish_stopwatch_new( void )
 }
 
 
+void stopish_stopwatch_reset( void )
+{
+    if ( timerHandle == -1 &&
+         stopish_get_mode(  ) != STOPISH_MODE_RESUME )
+        return;
+    gtk_button_clicked( GTK_BUTTON( resetButton ) );
+}
+
+
 void stopish_stopwatch_perf_timer_hour( GtkRadioButton* radio, GtkLabel *label )
 {
     stopish_timer_set_precision( TIMER_PRECISION_HOUR );
@@ -151,12 +158,12 @@ void stopish_stopwatch_label_timer_landscape( void )
     if ( stopish_timer_get_precision(  ) == TIMER_PRECISION_MINUTE )
         gtk_label_set_markup( GTK_LABEL( timerdata.label ),
                               "<span font_family=\"monospace\" "
-                              "size=\"70000\" weight=\"ultrabold\">"
+                              "size=\"80000\" weight=\"ultrabold\">"
                               "00:00.0</span>" );
     else
         gtk_label_set_markup( GTK_LABEL( timerdata.label ),
                               "<span font_family=\"monospace\" "
-                              "size=\"70000\" weight=\"ultrabold\">"
+                              "size=\"80000\" weight=\"ultrabold\">"
                               "00:00:00.0</span>" );
     gtk_misc_set_alignment( GTK_MISC( timerdata.label ), 0.5f, 0.5f );
     gtk_container_add( GTK_CONTAINER( timerdata.vBox ), timerdata.label );
@@ -184,7 +191,7 @@ void stopish_stopwatch_label_timer_portrait( void )
         gtk_misc_set_alignment( GTK_MISC( timerdata.labelHour ), 0.0f, 0.5f );
         gtk_label_set_markup( GTK_LABEL( timerdata.labelHour ),
                               "<span font_family=\"monospace\" "
-                              "size=\"80000\" weight=\"ultrabold\">"
+                              "size=\"90000\" weight=\"ultrabold\">"
                               "00</span>" );
         gtk_container_add( GTK_CONTAINER( hBox ), timerdata.labelHour );
         gtk_container_add( GTK_CONTAINER( vBox ), hBox );
@@ -200,7 +207,7 @@ void stopish_stopwatch_label_timer_portrait( void )
     gtk_misc_set_alignment( GTK_MISC( timerdata.labelMinute ), 0.0f, 0.5f );
     gtk_label_set_markup( GTK_LABEL( timerdata.labelMinute ),
                           "<span font_family=\"monospace\" "
-                          "size=\"80000\" weight=\"ultrabold\">"
+                          "size=\"90000\" weight=\"ultrabold\">"
                           "00</span>" );
     gtk_container_add( GTK_CONTAINER( hBox ), timerdata.labelMinute );
     gtk_container_add( GTK_CONTAINER( vBox ), hBox );
@@ -215,7 +222,7 @@ void stopish_stopwatch_label_timer_portrait( void )
     gtk_misc_set_alignment( GTK_MISC( timerdata.labelSecond ), 0.0f, 0.5f );
     gtk_label_set_markup( GTK_LABEL( timerdata.labelSecond ),
                           "<span font_family=\"monospace\" "
-                          "size=\"80000\" weight=\"ultrabold\">"
+                          "size=\"90000\" weight=\"ultrabold\">"
                           "00.0</span>" );
     gtk_container_add( GTK_CONTAINER( hBox ), timerdata.labelSecond );
     gtk_container_add( GTK_CONTAINER( vBox ), hBox );
@@ -226,7 +233,6 @@ void stopish_stopwatch_label_timer_portrait( void )
 }
 
 
-
 //
 // Timer callback
 //
@@ -239,7 +245,7 @@ static gint timeout_cb( gpointer data )
     tempString = stopish_get_time_string(  );
     if ( stopish_get_orientation(  ) == STOPISH_LANDSCAPE ) {
         sprintf( formatBuffer, "<span font_family=\"monospace\" "
-                               "size=\"70000\" weight=\"ultrabold\">"
+                               "size=\"80000\" weight=\"ultrabold\">"
                                "%s</span>", tempString );
         gtk_label_set_markup( GTK_LABEL( timerdata.label ), formatBuffer );
     }
@@ -247,7 +253,7 @@ static gint timeout_cb( gpointer data )
         if ( stopish_timer_get_precision(  ) == TIMER_PRECISION_HOUR ) {
             sprintf( tempBuffer, "%.2s", tempString );
             sprintf( formatBuffer, "<span font_family=\"monospace\" "
-                                   "size=\"80000\" weight=\"ultrabold\">"
+                                   "size=\"90000\" weight=\"ultrabold\">"
                                    "%s</span>", tempBuffer );
             gtk_label_set_markup( GTK_LABEL( timerdata.labelHour ),
                                   formatBuffer );
@@ -257,7 +263,7 @@ static gint timeout_cb( gpointer data )
         else
             sprintf( tempBuffer, "%.2s", tempString );
         sprintf( formatBuffer, "<span font_family=\"monospace\" "
-                               "size=\"80000\" weight=\"ultrabold\">"
+                               "size=\"90000\" weight=\"ultrabold\">"
                                "%s</span>", tempBuffer );
         gtk_label_set_markup( GTK_LABEL( timerdata.labelMinute ),
                               formatBuffer );
@@ -266,7 +272,7 @@ static gint timeout_cb( gpointer data )
         else
             sprintf( tempBuffer, "%.4s", tempString + 3 );
         sprintf( formatBuffer, "<span font_family=\"monospace\" "
-                               "size=\"80000\" weight=\"ultrabold\">"
+                               "size=\"90000\" weight=\"ultrabold\">"
                                "%s</span>", tempBuffer );
         gtk_label_set_markup( GTK_LABEL( timerdata.labelSecond ),
                               formatBuffer );
@@ -299,6 +305,7 @@ static void start_cb( GtkButton* button, gpointer data )
         stopish_set_mode( STOPISH_MODE_RESUME );
         g_source_remove( timerHandle );
         stopish_timer_save(  );
+        timerHandle = -1;
     }
 
     // allow user to reset timer
@@ -318,43 +325,38 @@ static void reset_cb( GtkButton* button, gpointer data )
     // set label text and remove timer handle
     gtk_button_set_label( GTK_BUTTON( data ), "Start" );
     stopish_set_mode( STOPISH_MODE_START );
-    if ( stopish_timer_get_precision(  ) == TIMER_PRECISION_MINUTE )
-        gtk_label_set_markup( GTK_LABEL( timerdata.label ),
-                              "<span font_family=\"monospace\" "
-                              "size=\"70000\" weight=\"ultrabold\">"
-                              "00:00.0</span>" );
+    if ( stopish_get_orientation(  ) == STOPISH_LANDSCAPE )
+        stopish_stopwatch_label_timer_landscape(  );
     else
-        gtk_label_set_markup( GTK_LABEL( timerdata.label ),
-                              "<span font_family=\"monospace\" "
-                              "size=\"70000\" weight=\"ultrabold\">"
-                              "00:00:00.0</span>" );
+        stopish_stopwatch_label_timer_portrait(  );
     g_source_remove( timerHandle );
+    timerHandle = -1;
 
     // add current time to history
     historyList = g_slist_prepend( historyList,
                                    ( gpointer ) stopish_get_time_string(  ) );
     sprintf( formatString, "<span size=\"large\">%s</span>",
              ( char * ) historyList->data );
-    gtk_label_set_markup( GTK_LABEL( timerHistoryLabel1 ),
+    gtk_label_set_markup( GTK_LABEL( timerHistoryLabel[0] ),
                           formatString );
     tempList = historyList;
     tempList = g_slist_next( tempList );
     if ( tempList ) {
-        gtk_label_set_text( GTK_LABEL( timerHistoryLabel2 ),
+        gtk_label_set_text( GTK_LABEL( timerHistoryLabel[1] ),
                             ( char * ) tempList->data );
     }
     tempList = g_slist_next( tempList );
     if ( tempList ) {
         sprintf( formatString, "<span size=\"small\">%s</span>",
                  ( char * ) tempList->data );
-        gtk_label_set_markup( GTK_LABEL( timerHistoryLabel3 ),
+        gtk_label_set_markup( GTK_LABEL( timerHistoryLabel[2] ),
                               formatString );
     }
     tempList = g_slist_next( tempList );
     if ( tempList ) {
         sprintf( formatString, "<span size=\"x-small\">%s</span>",
                  ( char * ) tempList->data );
-        gtk_label_set_markup( GTK_LABEL( timerHistoryLabel4 ),
+        gtk_label_set_markup( GTK_LABEL( timerHistoryLabel[3] ),
                               formatString );
     }