1 /*---------------------------------------------------------------------------- 2 -- 3 -- Module: xtmFormat 4 -- 5 -- Project: Xdiary 6 -- System: xtm - X Desktop Calendar 7 -- Subsystem: <> 8 -- Function block: <> 9 -- 10 -- Description: 11 -- Various format routines. 12 -- 13 -- Filename: xtmFormat.c 14 -- 15 -- Authors: Roger Larsson, Ulrika Bornetun 16 -- Creation date: 1992-01-07 17 -- 18 -- 19 -- (C) Copyright Ulrika Bornetun, Roger Larsson (1995) 20 -- All rights reserved 21 -- 22 -- Permission to use, copy, modify, and distribute this software and its 23 -- documentation for any purpose and without fee is hereby granted, 24 -- provided that the above copyright notice appear in all copies. Ulrika 25 -- Bornetun and Roger Larsson make no representations about the usability 26 -- of this software for any purpose. It is provided "as is" without express 27 -- or implied warranty. 28 ----------------------------------------------------------------------------*/ 29 30 /* SCCS module identifier. */ 31 static char SCCSID[] = "@(#) Module: xtmFormat.c, Version: 1.1, Date: 95/02/18 15:52:21"; 32 33 34 /*---------------------------------------------------------------------------- 35 -- Include files 36 ----------------------------------------------------------------------------*/ 37 38 #include <stdio.h> 39 #include <string.h> 40 #include <ctype.h> 41 42 #include <X11/Intrinsic.h> 43 44 #include <Xm/Xm.h> 45 46 #include "System.h" 47 #include "Message.h" 48 #include "TimDate.h" 49 50 #include "msgXdiary.h" 51 #include "xtmGlobal.h" 52 #include "xtmDbTools.h" 53 #include "xitError.h" 54 #include "xitTools.h" 55 #include "XmUbTimeB.h" 56 #include "xtmFormat.h" 57 58 59 /*---------------------------------------------------------------------------- 60 -- Macro definitions 61 ----------------------------------------------------------------------------*/ 62 63 64 /*---------------------------------------------------------------------------- 65 -- Type declarations 66 ----------------------------------------------------------------------------*/ 67 68 69 /*---------------------------------------------------------------------------- 70 -- Global definitions 71 ----------------------------------------------------------------------------*/ 72 73 /* Name of module. */ 74 static char *module_name = "xtmFormat"; 75 76 77 /*---------------------------------------------------------------------------- 78 -- Function prototypes 79 ----------------------------------------------------------------------------*/ 80 81 82 83 /*---------------------------------------------------------------------------- 84 -- Functions 85 ----------------------------------------------------------------------------*/ 86 87 Boolean xtmFoFetchDate(Widget parentW,Widget dateW,XTM_FO_FETCH_WHAT fetch_what,Boolean empty_allowed,TIM_TIME_REF * use_time)88 xtmFoFetchDate( Widget parentW, 89 Widget dateW, 90 XTM_FO_FETCH_WHAT fetch_what, 91 Boolean empty_allowed, 92 TIM_TIME_REF *use_time ) 93 { 94 95 /* Variables. */ 96 XmUbTimeBoxStatus status; 97 98 99 /* Code. */ 100 101 *use_time = 0; 102 103 if( fetch_what == XTM_FO_START_DATE ) 104 status = XmUbTimeBoxGetStartDate( dateW, use_time ); 105 else 106 status = XmUbTimeBoxGetEndDate( dateW, use_time ); 107 108 if( status == TBOX_EMPTY && empty_allowed ) { 109 *use_time = 0; 110 return( True ); 111 } 112 113 if( status == TBOX_OK ) 114 return( True ); 115 116 *use_time = 0; 117 118 if( parentW != NULL ) 119 (void) xitCreateErrorDialog( 120 parentW, "ErrorDialog", 121 msgGetText( MXDI_ERROR_MESSAGE_LABEL ), 122 msgGetText( MXDI_INVALID_DATE ), 123 NULL, NULL ); 124 125 126 return( False ); 127 128 } /* xtmFoFetchDate */ 129 130 131 /*----------------------------------------------------------------------*/ 132 133 Boolean xtmFoFetchMinuteTime(char * char_ref,Boolean empty_allowed,Widget parent,int max_minutes,int * return_minutes)134 xtmFoFetchMinuteTime( char *char_ref, 135 Boolean empty_allowed, 136 Widget parent, 137 int max_minutes, 138 int *return_minutes ) 139 { 140 141 /* Variables. */ 142 Boolean found = False; 143 int days; 144 int hours; 145 int items; 146 int minutes; 147 char *msg_ref; 148 char *text_ref; 149 Widget tempW; 150 151 152 /* Code. */ 153 154 *return_minutes = -1; 155 156 /* Empty field? */ 157 text_ref = char_ref; 158 while( isspace( *text_ref ) ) 159 text_ref++; 160 161 if( *text_ref == '\0' ) 162 return( empty_allowed ); 163 164 /* Start looking. */ 165 if( ! found ) { 166 items = sscanf( char_ref, "%d %d:%d", &days, &hours, &minutes ); 167 if( items == 3 ) 168 found = True; 169 } 170 171 if( ! found ) { 172 items = sscanf( char_ref, "%d:%d", &hours, &minutes ); 173 if( items == 2 ) { 174 found = True; 175 days = 0; 176 } 177 } 178 179 if( ! found ) { 180 items = sscanf( char_ref, "%d", &minutes ); 181 if( items == 1 ) { 182 found = True; 183 days = 0; 184 hours = 0; 185 } 186 } 187 188 /* Did we find anything? */ 189 if( found && 190 days >= 0 && 191 hours >= 0 && hours <= 23 && 192 minutes >= 0 && minutes <= 59 ) { 193 minutes = days * 24 * 60 + hours * 60 + minutes; 194 195 if( minutes <= max_minutes ) { 196 *return_minutes = minutes; 197 198 return( True ); 199 } 200 } 201 202 if( parent != NULL ) { 203 if( found ) 204 msg_ref = msgGetText( MXDI_TIME_OUTSIDE_LIMITS ); 205 else 206 msg_ref = msgGetText( MXDI_INVALID_MINUTE_TIME ); 207 208 tempW = xitCreateErrorDialog( 209 parent, "ErrorDialog", 210 msgGetText( MXDI_ERROR_MESSAGE_LABEL ), 211 msg_ref, 212 NULL, NULL ); 213 } 214 215 216 return( False ); 217 218 } /* xtmFoFetchMinuteTime */ 219 220 221 /*----------------------------------------------------------------------*/ 222 223 Boolean xtmFoFetchTime(Widget parentW,Widget timeW,XTM_FO_FETCH_WHAT fetch_what,Boolean empty_allowed,TIM_TIME_REF * use_time)224 xtmFoFetchTime( Widget parentW, 225 Widget timeW, 226 XTM_FO_FETCH_WHAT fetch_what, 227 Boolean empty_allowed, 228 TIM_TIME_REF *use_time ) 229 { 230 231 /* Variables. */ 232 XmUbTimeBoxStatus status; 233 234 235 /* Code. */ 236 237 *use_time = 0; 238 239 if( fetch_what == XTM_FO_START_TIME ) 240 status = XmUbTimeBoxGetStartTime( timeW, use_time ); 241 else 242 status = XmUbTimeBoxGetEndTime( timeW, use_time ); 243 244 if( status == TBOX_EMPTY && empty_allowed ) { 245 *use_time = 0; 246 return( True ); 247 } 248 249 if( status == TBOX_OK ) 250 return( True ); 251 252 *use_time = 0; 253 254 if( parentW != NULL ) 255 (void) xitCreateErrorDialog( 256 parentW, "ErrorDialog", 257 msgGetText( MXDI_ERROR_MESSAGE_LABEL ), 258 msgGetText( MXDI_INVALID_ENTRY_TIME ), 259 NULL, NULL ); 260 261 262 return( False ); 263 264 } /* xtmFoFetchTime */ 265 266 267 /*----------------------------------------------------------------------*/ 268 269 void xtmFoFormatDate(TIM_TIME_REF use_time,char * buffer,int buffer_size)270 xtmFoFormatDate( TIM_TIME_REF use_time, 271 char *buffer, 272 int buffer_size ) 273 { 274 275 /* Code. */ 276 277 TimFormatDate( use_time, buffer, buffer_size ); 278 279 280 return; 281 282 } /* xtmFoFormatDate */ 283 284 285 /*----------------------------------------------------------------------*/ 286 287 void xtmFoFormatEntryFlags(XTM_GL_CUSTOM_DATA_REF custom_data,XTM_DB_ALL_ENTRY_REF entry_record,char * flags_buffer,int flags_buffer_size)288 xtmFoFormatEntryFlags( XTM_GL_CUSTOM_DATA_REF custom_data, 289 XTM_DB_ALL_ENTRY_REF entry_record, 290 char *flags_buffer, 291 int flags_buffer_size ) 292 { 293 294 /* Variables. */ 295 char buffer[ 100 ]; 296 char tmp_buffer[ 50 ]; 297 298 299 /* Code. */ 300 301 *flags_buffer = '\0'; 302 buffer[ 0 ] = '\0'; 303 304 /* Get the flags. */ 305 if( flagIsSet( entry_record -> entry.flags, XTM_DB_FLAG_ALARM ) ) { 306 sprintf( tmp_buffer, "%c", custom_data -> alarm_marker ); 307 strcat( buffer, tmp_buffer ); 308 } 309 310 if( flagIsSet( entry_record -> entry.flags, 311 (XTM_DB_FLAG_ACTION_SCRIPT | XTM_DB_FLAG_ACTION_TEXT) ) ) { 312 sprintf( tmp_buffer, "%c", custom_data -> action_alarm_marker ); 313 strcat( buffer, tmp_buffer ); 314 } 315 316 if( flagIsSet( entry_record -> entry.flags, XTM_DB_FLAG_ARCHIVED ) ) { 317 sprintf( tmp_buffer, "%c", custom_data -> archive_marker ); 318 strcat( buffer, tmp_buffer ); 319 } 320 321 if( flagIsSet( entry_record -> entry.flags, XTM_DB_FLAG_IMPORTANT ) ) { 322 sprintf( tmp_buffer, "%c", custom_data -> important_marker ); 323 strcat( buffer, tmp_buffer ); 324 } 325 326 if( flagIsSet( entry_record -> entry.flags, XTM_DB_FLAG_PRIVATE ) ) { 327 sprintf( tmp_buffer, "%c", custom_data -> private_marker ); 328 strcat( buffer, tmp_buffer ); 329 } 330 331 if( flagIsSet( entry_record -> entry.flags, XTM_DB_FLAG_INCLUDE ) ) { 332 sprintf( tmp_buffer, "%c", custom_data -> include_marker ); 333 strcat( buffer, tmp_buffer ); 334 } 335 336 if( entry_record -> entry.entry_category == XTM_DB_REP_ENTRY_LIST ) { 337 sprintf( tmp_buffer, "%c", custom_data -> standing_marker ); 338 strcat( buffer, tmp_buffer ); 339 } 340 341 if( entry_record -> entry.entry_type == XTM_DB_DAY_NOTE ) { 342 if( flagIsSet( entry_record -> entry.flags, XTM_DB_FLAG_NOTE_DONE ) ) { 343 sprintf( tmp_buffer, "%c", custom_data -> note_done_marker ); 344 strcat( buffer, tmp_buffer ); 345 } else { 346 if( custom_data -> note_not_done_marker != 32 ) { 347 sprintf( tmp_buffer, "%c", custom_data -> note_not_done_marker ); 348 strcat( buffer, tmp_buffer ); 349 } 350 } 351 } 352 353 /* Save the result. */ 354 if( strlen( buffer ) < flags_buffer_size ) 355 strcpy( flags_buffer, buffer ); 356 357 358 return; 359 360 } /* xtmFoFormatEntryFlags */ 361 362 363 /*----------------------------------------------------------------------*/ 364 365 void xtmFoFormatEntryTimes(XTM_DB_ALL_ENTRY_REF entry_record,char * time_buffer,int time_buffer_size)366 xtmFoFormatEntryTimes( XTM_DB_ALL_ENTRY_REF entry_record, 367 char *time_buffer, 368 int time_buffer_size ) 369 { 370 371 /* Variables. */ 372 char buffer[ 100 ]; 373 char buffer1[ 50 ]; 374 char buffer2[ 50 ]; 375 TIM_TIME_REF start_time; 376 TIM_TIME_REF end_time; 377 378 379 /* Code. */ 380 381 strcpy( time_buffer, "" ); 382 383 if( entry_record -> entry.entry_type == XTM_DB_DAY_NOTE ) 384 return; 385 386 start_time = entry_record -> entry.time_stamp; 387 end_time = start_time; 388 389 TimAddMinutes( &end_time, (int) entry_record -> entry.duration ); 390 391 xtmFoFormatTime( start_time, buffer1, sizeof( buffer1 ) ); 392 xtmFoFormatTime( end_time, buffer2, sizeof( buffer2 ) ); 393 394 if( entry_record -> entry.duration > 0 ) 395 sprintf( buffer, "%s - %s", buffer1, buffer2 ); 396 else 397 sprintf( buffer, "%s", buffer1 ); 398 399 /* Save the result. */ 400 if( strlen( buffer ) < time_buffer_size ) 401 strcpy( time_buffer, buffer ); 402 403 404 return; 405 406 } /* xtmFoFormatEntryTimes */ 407 408 409 /*----------------------------------------------------------------------*/ 410 411 char xtmFoFormatText(char * format_text,int indent,int max_lines,int max_line_length)412 *xtmFoFormatText( char *format_text, 413 int indent, 414 int max_lines, 415 int max_line_length ) 416 { 417 418 /* Variables. */ 419 Boolean line_written = False; 420 int SEGMENT_SIZE = 10000; 421 int length; 422 int lines_read; 423 int text_buffer_size; 424 char buffer[ 100 ]; 425 char format_buffer[ 20 ]; 426 char line[ 100 ]; 427 char *char_ref; 428 char *eol_ref; 429 char *text_buffer; 430 431 432 /* Code. */ 433 434 if( format_text == NULL || *format_text == '\0' ) 435 return( NULL ); 436 437 /* Format for the message line. */ 438 sprintf( format_buffer, "%%-%d.%ds%%-%d.%ds", 439 indent, indent, max_line_length, max_line_length ); 440 441 char_ref = format_text; 442 lines_read = 0; 443 444 if( max_lines < 1 ) 445 max_lines = 1; 446 447 text_buffer = SysMalloc( SEGMENT_SIZE ); 448 *text_buffer = '\0'; 449 text_buffer_size = SEGMENT_SIZE; 450 451 while( *char_ref != '\0' && lines_read < max_lines ) { 452 453 eol_ref = strchr( char_ref, '\n' ); 454 if( eol_ref == NULL ) 455 eol_ref = (char_ref + strlen( char_ref ) - 1); 456 else 457 eol_ref--; 458 459 /* The length of the string cannot exceed n characters. */ 460 length = eol_ref - char_ref + 1; 461 if( length > max_line_length ) 462 length = max_line_length; 463 464 strncpy( buffer, char_ref, length ); 465 buffer[ length ] ='\0'; 466 467 sprintf( line, format_buffer, "", buffer ); 468 469 /* Extend the text buffer? */ 470 if( strlen( text_buffer ) + strlen( line ) + 10 > text_buffer_size ) { 471 text_buffer_size = text_buffer_size + SEGMENT_SIZE; 472 473 text_buffer = SysRealloc( text_buffer, text_buffer_size ); 474 } 475 476 /* Add a linefeed between the lines, not before and not after. */ 477 if( line_written ) 478 strcat( text_buffer, "\n" ); 479 else 480 line_written = True; 481 482 strcat( text_buffer, line ); 483 484 lines_read++; 485 486 if( *(eol_ref + 1) == '\n' ) 487 char_ref = eol_ref + 2; 488 else 489 char_ref = eol_ref + 1; 490 491 } /* while */ 492 493 494 return( text_buffer ); 495 496 } /* xtmFoFormatText */ 497 498 499 /*----------------------------------------------------------------------*/ 500 501 void xtmFoFormatTime(TIM_TIME_REF use_time,char * buffer,int buffer_size)502 xtmFoFormatTime( TIM_TIME_REF use_time, 503 char *buffer, 504 int buffer_size ) 505 { 506 507 /* Code. */ 508 509 TimFormatTime( use_time, buffer, buffer_size ); 510 511 512 return; 513 514 } /* xtmFoFormatTime */ 515 516 517 /*----------------------------------------------------------------------*/ 518 519 void xtmFoFormatFullTime(TIM_TIME_REF use_time,char * buffer,int buffer_size)520 xtmFoFormatFullTime( TIM_TIME_REF use_time, 521 char *buffer, 522 int buffer_size ) 523 { 524 525 /* Variables. */ 526 char date_buffer[ 50 ]; 527 char day_buffer[ 50 ]; 528 char full_time_buffer[ 50 ]; 529 char time_buffer[ 50 ]; 530 531 532 /* Code. */ 533 534 *buffer = '\0'; 535 536 /* The date. */ 537 TimFormatFullDate( use_time, date_buffer, sizeof( date_buffer ) ); 538 539 /* The time. */ 540 TimFormatTime( use_time, time_buffer, sizeof( time_buffer ) ); 541 542 /* The day. */ 543 TimFormatStrTime( use_time, "%a", day_buffer, sizeof( day_buffer ) ); 544 545 /* The full date. */ 546 sprintf( full_time_buffer, "%s\n%s, %s", 547 date_buffer, day_buffer, time_buffer ); 548 549 if( strlen( full_time_buffer ) < buffer_size ) 550 strcpy( buffer, full_time_buffer ); 551 552 553 return; 554 555 } /* xtmFoFormatFullTime */ 556