3 // Copyright 2010 Michael Cronenworth <mike@cchtml.com>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 // MA 02110-1301, USA.
21 #include <hildon/hildon.h>
30 GtkWidget *labelMinute;
31 GtkWidget *labelSecond;
34 static struct timerData timerdata;
35 static GtkWidget *timerHistoryLabel1 = NULL;
36 static GtkWidget *timerHistoryLabel2 = NULL;
37 static GtkWidget *timerHistoryLabel3 = NULL;
38 static GtkWidget *timerHistoryLabel4 = NULL;
39 static GSList *historyList = NULL;
40 static int timerHandle = -1;
43 static gint timeout_cb( gpointer data );
44 static void start_cb( GtkButton* button, gpointer data );
45 static void reset_cb( GtkButton* button, gpointer data );
48 GtkWindow *stopish_stopwatch_new( void )
50 GtkWidget *window, *button, *button0, *label;
51 GtkWidget *vBoxMain, *vBox0, *hBox0;
53 window = hildon_stackable_window_new( );
55 gtk_container_set_border_width( GTK_CONTAINER( window ), 20 );
57 gtk_window_set_title( GTK_WINDOW( window ), "Stopish" );
59 vBoxMain = gtk_vbox_new( FALSE, 10 );
62 label = gtk_label_new( NULL );
63 gtk_container_add( GTK_CONTAINER( vBoxMain ), label );
65 vBox0 = gtk_vbox_new( FALSE, 5 );
68 timerdata.vBox = gtk_vbox_new( FALSE, 0 );
69 gtk_container_add( GTK_CONTAINER( vBox0 ), timerdata.vBox );
70 stopish_stopwatch_label_timer_landscape( );
73 timerHistoryLabel1 = gtk_label_new( NULL );
74 gtk_label_set_markup( GTK_LABEL( timerHistoryLabel1 ),
75 "<span size=\"large\"> </span>" );
76 gtk_box_pack_start( GTK_BOX( vBox0 ), timerHistoryLabel1, FALSE, FALSE, 0 );
77 timerHistoryLabel2 = gtk_label_new( NULL );
78 gtk_box_pack_start( GTK_BOX( vBox0 ), timerHistoryLabel2, FALSE, FALSE, 0 );
79 timerHistoryLabel3 = gtk_label_new( NULL );
80 gtk_label_set_markup( GTK_LABEL( timerHistoryLabel3 ),
81 "<span size=\"small\"> </span>" );
82 gtk_box_pack_start( GTK_BOX( vBox0 ), timerHistoryLabel3, FALSE, FALSE, 0 );
83 timerHistoryLabel4 = gtk_label_new( NULL );
84 gtk_label_set_markup( GTK_LABEL( timerHistoryLabel4 ),
85 "<span size=\"x-small\"> </span>" );
86 gtk_box_pack_start( GTK_BOX( vBox0 ), timerHistoryLabel4, FALSE, FALSE, 0 );
88 gtk_container_add( GTK_CONTAINER( vBoxMain ), vBox0 );
91 label = gtk_label_new( NULL );
92 gtk_container_add( GTK_CONTAINER( vBoxMain ), label );
95 hBox0 = gtk_hbox_new( FALSE, 15 );
96 gtk_widget_set_size_request( hBox0, -1, 80 );
98 // start/pause stopwatch button
99 button = hildon_button_new_with_text( HILDON_SIZE_HALFSCREEN_WIDTH,
100 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
102 button0 = hildon_button_new_with_text( HILDON_SIZE_HALFSCREEN_WIDTH,
103 HILDON_BUTTON_ARRANGEMENT_HORIZONTAL,
105 g_signal_connect( G_OBJECT( button ), "clicked",
106 G_CALLBACK( start_cb ), button0 );
107 gtk_container_add( GTK_CONTAINER( hBox0 ), button );
110 gtk_widget_set_sensitive( button0, FALSE );
111 g_signal_connect( G_OBJECT( button0 ), "clicked",
112 G_CALLBACK( reset_cb ), button );
113 gtk_container_add( GTK_CONTAINER( hBox0 ), button0 );
115 gtk_box_pack_start( GTK_BOX( vBoxMain ), hBox0, FALSE, FALSE, 0 );
117 gtk_container_add( GTK_CONTAINER( window ), vBoxMain );
119 gtk_widget_show_all( window );
121 return GTK_WINDOW( window );
125 void stopish_stopwatch_perf_timer_hour( GtkRadioButton* radio, GtkLabel *label )
127 stopish_timer_set_precision( TIMER_PRECISION_HOUR );
128 if ( stopish_get_orientation( ) == STOPISH_LANDSCAPE )
129 stopish_stopwatch_label_timer_landscape( );
131 stopish_stopwatch_label_timer_portrait( );
135 void stopish_stopwatch_perf_timer_minute( GtkRadioButton* radio, GtkLabel *label )
137 stopish_timer_set_precision( TIMER_PRECISION_MINUTE );
138 if ( stopish_get_orientation( ) == STOPISH_LANDSCAPE )
139 stopish_stopwatch_label_timer_landscape( );
141 stopish_stopwatch_label_timer_portrait( );
145 void stopish_stopwatch_label_timer_landscape( void )
147 gtk_widget_set_size_request( timerdata.vBox, 800, -1 );
149 gtk_widget_destroy( timerdata.label );
150 timerdata.label = gtk_label_new( NULL );
151 if ( stopish_timer_get_precision( ) == TIMER_PRECISION_MINUTE )
152 gtk_label_set_markup( GTK_LABEL( timerdata.label ),
153 "<span font_family=\"monospace\" "
154 "size=\"70000\" weight=\"ultrabold\">"
157 gtk_label_set_markup( GTK_LABEL( timerdata.label ),
158 "<span font_family=\"monospace\" "
159 "size=\"70000\" weight=\"ultrabold\">"
160 "00:00:00.0</span>" );
161 gtk_misc_set_alignment( GTK_MISC( timerdata.label ), 0.5f, 0.5f );
162 gtk_container_add( GTK_CONTAINER( timerdata.vBox ), timerdata.label );
163 gtk_widget_show( timerdata.label );
167 void stopish_stopwatch_label_timer_portrait( void )
169 GtkWidget *vBox, *hBox, *label;
171 gtk_widget_set_size_request( timerdata.vBox, 480, -1 );
173 gtk_widget_destroy( timerdata.label );
174 vBox = gtk_vbox_new( FALSE, 10 );
176 if ( stopish_timer_get_precision( ) == TIMER_PRECISION_HOUR ) {
177 hBox = gtk_hbox_new( FALSE, 10 );
178 label = gtk_label_new( "Hours" );
179 gtk_widget_set_size_request( label, 100, -1 );
180 gtk_misc_set_alignment( GTK_MISC( label ), 1.0f, 0.5f );
181 gtk_container_add( GTK_CONTAINER( hBox ), label );
182 timerdata.labelHour = gtk_label_new( NULL );
183 gtk_widget_set_size_request( timerdata.labelHour, 350, -1 );
184 gtk_misc_set_alignment( GTK_MISC( timerdata.labelHour ), 0.0f, 0.5f );
185 gtk_label_set_markup( GTK_LABEL( timerdata.labelHour ),
186 "<span font_family=\"monospace\" "
187 "size=\"80000\" weight=\"ultrabold\">"
189 gtk_container_add( GTK_CONTAINER( hBox ), timerdata.labelHour );
190 gtk_container_add( GTK_CONTAINER( vBox ), hBox );
193 hBox = gtk_hbox_new( FALSE, 10 );
194 label = gtk_label_new( "Minutes" );
195 gtk_widget_set_size_request( label, 100, -1 );
196 gtk_misc_set_alignment( GTK_MISC( label ), 1.0f, 0.5f );
197 gtk_container_add( GTK_CONTAINER( hBox ), label );
198 timerdata.labelMinute = gtk_label_new( NULL );
199 gtk_widget_set_size_request( timerdata.labelMinute, 350, -1 );
200 gtk_misc_set_alignment( GTK_MISC( timerdata.labelMinute ), 0.0f, 0.5f );
201 gtk_label_set_markup( GTK_LABEL( timerdata.labelMinute ),
202 "<span font_family=\"monospace\" "
203 "size=\"80000\" weight=\"ultrabold\">"
205 gtk_container_add( GTK_CONTAINER( hBox ), timerdata.labelMinute );
206 gtk_container_add( GTK_CONTAINER( vBox ), hBox );
208 hBox = gtk_hbox_new( FALSE, 10 );
209 label = gtk_label_new( "Seconds" );
210 gtk_widget_set_size_request( label, 100, -1 );
211 gtk_misc_set_alignment( GTK_MISC( label ), 1.0f, 0.5f );
212 gtk_container_add( GTK_CONTAINER( hBox ), label );
213 timerdata.labelSecond = gtk_label_new( NULL );
214 gtk_widget_set_size_request( timerdata.labelSecond, 350, -1 );
215 gtk_misc_set_alignment( GTK_MISC( timerdata.labelSecond ), 0.0f, 0.5f );
216 gtk_label_set_markup( GTK_LABEL( timerdata.labelSecond ),
217 "<span font_family=\"monospace\" "
218 "size=\"80000\" weight=\"ultrabold\">"
220 gtk_container_add( GTK_CONTAINER( hBox ), timerdata.labelSecond );
221 gtk_container_add( GTK_CONTAINER( vBox ), hBox );
223 timerdata.label = vBox;
224 gtk_container_add( GTK_CONTAINER( timerdata.vBox ), vBox );
225 gtk_widget_show_all( vBox );
233 static gint timeout_cb( gpointer data )
235 char formatBuffer[128], tempBuffer[8];
239 tempString = stopish_get_time_string( );
240 if ( stopish_get_orientation( ) == STOPISH_LANDSCAPE ) {
241 sprintf( formatBuffer, "<span font_family=\"monospace\" "
242 "size=\"70000\" weight=\"ultrabold\">"
243 "%s</span>", tempString );
244 gtk_label_set_markup( GTK_LABEL( timerdata.label ), formatBuffer );
247 if ( stopish_timer_get_precision( ) == TIMER_PRECISION_HOUR ) {
248 sprintf( tempBuffer, "%.2s", tempString );
249 sprintf( formatBuffer, "<span font_family=\"monospace\" "
250 "size=\"80000\" weight=\"ultrabold\">"
251 "%s</span>", tempBuffer );
252 gtk_label_set_markup( GTK_LABEL( timerdata.labelHour ),
255 if ( stopish_timer_get_precision( ) == TIMER_PRECISION_HOUR )
256 sprintf( tempBuffer, "%.2s", tempString + 3 );
258 sprintf( tempBuffer, "%.2s", tempString );
259 sprintf( formatBuffer, "<span font_family=\"monospace\" "
260 "size=\"80000\" weight=\"ultrabold\">"
261 "%s</span>", tempBuffer );
262 gtk_label_set_markup( GTK_LABEL( timerdata.labelMinute ),
264 if ( stopish_timer_get_precision( ) == TIMER_PRECISION_HOUR )
265 sprintf( tempBuffer, "%.4s", tempString + 6 );
267 sprintf( tempBuffer, "%.4s", tempString + 3 );
268 sprintf( formatBuffer, "<span font_family=\"monospace\" "
269 "size=\"80000\" weight=\"ultrabold\">"
270 "%s</span>", tempBuffer );
271 gtk_label_set_markup( GTK_LABEL( timerdata.labelSecond ),
280 static void start_cb( GtkButton* button, gpointer data )
282 if ( stopish_get_mode( ) == STOPISH_MODE_START ) {
283 // set label text and add timer handle
284 gtk_button_set_label( button, "Pause" );
285 stopish_set_mode( STOPISH_MODE_PAUSE );
286 stopish_set_time_start( stopish_current_time( ) );
287 timerHandle = g_timeout_add( 100, timeout_cb, NULL );
289 else if ( stopish_get_mode( ) == STOPISH_MODE_RESUME ) {
291 gtk_button_set_label( button, "Pause" );
292 stopish_set_mode( STOPISH_MODE_PAUSE );
293 stopish_timer_resume( );
294 timerHandle = g_timeout_add( 100, timeout_cb, NULL );
297 // pause timer, remove timeout
298 gtk_button_set_label( button, "Resume" );
299 stopish_set_mode( STOPISH_MODE_RESUME );
300 g_source_remove( timerHandle );
301 stopish_timer_save( );
304 // allow user to reset timer
305 gtk_widget_set_sensitive( GTK_WIDGET( data ), TRUE );
309 static void reset_cb( GtkButton* button, gpointer data )
313 char formatString[128];
315 if ( stopish_get_mode( ) == STOPISH_MODE_RESUME )
316 stopish_timer_resume( );
318 // set label text and remove timer handle
319 gtk_button_set_label( GTK_BUTTON( data ), "Start" );
320 stopish_set_mode( STOPISH_MODE_START );
321 if ( stopish_timer_get_precision( ) == TIMER_PRECISION_MINUTE )
322 gtk_label_set_markup( GTK_LABEL( timerdata.label ),
323 "<span font_family=\"monospace\" "
324 "size=\"70000\" weight=\"ultrabold\">"
327 gtk_label_set_markup( GTK_LABEL( timerdata.label ),
328 "<span font_family=\"monospace\" "
329 "size=\"70000\" weight=\"ultrabold\">"
330 "00:00:00.0</span>" );
331 g_source_remove( timerHandle );
333 // add current time to history
334 historyList = g_slist_prepend( historyList,
335 ( gpointer ) stopish_get_time_string( ) );
336 sprintf( formatString, "<span size=\"large\">%s</span>",
337 ( char * ) historyList->data );
338 gtk_label_set_markup( GTK_LABEL( timerHistoryLabel1 ),
340 tempList = historyList;
341 tempList = g_slist_next( tempList );
343 gtk_label_set_text( GTK_LABEL( timerHistoryLabel2 ),
344 ( char * ) tempList->data );
346 tempList = g_slist_next( tempList );
348 sprintf( formatString, "<span size=\"small\">%s</span>",
349 ( char * ) tempList->data );
350 gtk_label_set_markup( GTK_LABEL( timerHistoryLabel3 ),
353 tempList = g_slist_next( tempList );
355 sprintf( formatString, "<span size=\"x-small\">%s</span>",
356 ( char * ) tempList->data );
357 gtk_label_set_markup( GTK_LABEL( timerHistoryLabel4 ),
361 // remove the history time after the 4th
362 tempList = g_slist_next( tempList );
364 tempString = tempList->data;
365 historyList = g_slist_remove( historyList, tempList->data );
370 stopish_set_time_start( 0 );
372 // disallow user to reset timer
373 gtk_widget_set_sensitive( GTK_WIDGET( button ), FALSE );