Skip to main content

How to Convert the cxGetRecurrenceDescriptionString Routine From Delphi Into C++ Code

  • 3 minutes to read

If you need to change information provided by the cxGetRecurrenceDescriptionString routine (for example, for the localization purposes), implement a custom routine instead.

To inform a scheduler that a new routine is to be invoked in the application, use the cxGetRecurrenceDescriptionStringProc constant.

The following example demonstrates how to implement this routine in C++ code:

// ... 
AnsiString __fastcall <Form>::cxMyGetRecurrenceDescriptionString(TcxSchedulerEventRecurrenceInfo *ARecurrenceInfo, bool  AFullDescription = false)
{
   const AnsiString Weeks[] = {"first", "second", "third", "fourth", "last"};
   const AnsiString EveryMonths1[]  = {"day %d of every %d months", "day %d of every month"};
   const AnsiString EveryMonths2[]  = {"the %s %s of every %d months", "the %s %s of every month"};
   AnsiString GetDateParts(AnsiString &ADayStr, &AWeekStr){
     if ((ARecurrenceInfo->DayNumber >= 1)||(ARecurrenceInfo->DayNumber <= 5)){
       AWeekStr = Weeks[DayNumber];
      }
     else {
       AWeekStr = "";
     };
     if (ARecurrenceInfo->DayType == cxdtEveryDay){
       ADayStr = "day";
     }
     else if (ARecurrenceInfo->DayType == cxdtWeekDay){
       ADayStr = "weekday";
     }
     else if (ARecurrenceInfo->DayType == cxdtWeekEndDay){
       ADayStr = "weekend day";
     }
     else {
       ADayStr = LongDayNames[(int)DayType) - (int)cxdtSunday + 1];
     }
   }
   AnsiString OccurDaysToString(){
     TDay * ADay;
     int ACount, ASaveCount;
     AnsiString  Result;
     ACount = 0;
     for (int I = 0; I < 7; I++) {
       if (ARecurrenceInfo->OccurDays->Contains(ADay)) {
         ACount++
       }
     }
     ASaveCount = ACount;
     for (int I = 0; I < 7; I++) {
       if (ARecurrenceInfo->OccurDays->Contains(ADay)) {
         if (ASaveCount == 1){
           Result = LongDayNames[(int)ADay + 1];
         }
         else {
            ACount--;
            if (ACount > 1){
              Result = "" + LongDayNames[(int)ADay + 1] + ", " ;
             }
            else if (ACount == 1){
              Result = "" + LongDayNames[(int)ADay + 1] + " " ;
            }
            else {
              Result = "and " + LongDayNames[(int)ADay + 1];
             }
         }
       }
     }
     return Result;
   };
  AnsiString  GetTimeBounds(TcxSchedulerEvent *APattern){
    AnsiString  Result;
     if ((APattern->AllDayEvent) && (APattern->Duration == 1))
       Result = "";
     if (APattern->Duration < 1)
       Result = " from " + FormatDateTime("t", APattern->Start) + " to " + FormatDateTime("t", APattern->Finish)
     else
       Result= " from " + FormatDateTime("t", APattern->Start) + " for " + cxMinutesToTextProc(Floor(APattern->Duration / MinuteToTime));
    return Result;
  }
  String GetDateBounds(TcxSchedulerEvent *APattern: TcxSchedulerEvent){
    if (ARecurrenceInfo->Count) >= 0)
      return " effective " + FormatDateTime("ddddd", ARecurrenceInfo->Start) + " until " + FormatDateTime("ddddd", ARecurrenceInfo->GetEndDate)
    else
      return " effective " + FormatDateTime("ddddd", ARecurrenceInfo->Start);
  };
  AnsiString ADayStr, AWeekStr, AMonthStr, Result;
  TcxSchedulerEvent  *APattern;
  if ((ARecurrenceInfo == NULL)||(!(ARecurrenceInfo->Event->IsRecurring))){
    return "";
  }
  switch (ARecurrenceInfo->Recurrence){
    cxreDaily:
      if (ARecurrenceInfo->DayType == Cxschedulerstorage::cxdtWeekDay){
        Result = "every weekday";
      }
      else {
        if (ARecurrenceInfo->Periodicity == 1)
          Result = Format("every %d days", ARRAYOFCONST((ARecurrenceInfo->Periodicity)))
        else
          Result = Format("every day", ARRAYOFCONST((ARecurrenceInfo->Periodicity)));
      };
    break;
    cxreWeekly:
      if (ARecurrenceInfo->DayType ==  1)
        Result = "every " + OccurDaysToString;
      else {
        Result = Format("every %d weeks on %s", ARRAYOFCONST((ARecurrenceInfo->Periodicity, OccurDaysToString)));
    break;
    cxreMonthly:
      if (ARecurrenceInfo->DayType == Cxschedulerstorage::cxdtDay){
        if (ARecurrenceInfo->Periodicity == 1)
          Result = Format(EveryMonths1[0], ARRAYOFCONST((ARecurrenceInfo->DayNumber, ARecurrenceInfo->Periodicity)))
        else
          Result = Format(EveryMonths1[1], ARRAYOFCONST((ARecurrenceInfo->DayNumber, ARecurrenceInfo->Periodicity)));
      }
      else {
        GetDateParts(ADayStr, AWeekStr);
        if (ARecurrenceInfo->Periodicity == 1)
          Result = Format(EveryMonths2[0], ARRAYOFCONST((AWeekStr, ADayStr, ARecurrenceInfo->Periodicity)))
        else
          Result = Format(EveryMonths2[1], ARRAYOFCONST((AWeekStr, ADayStr, ARecurrenceInfo->Periodicity)));
      };
    break;
    cxreYearly:
      if ((ARecurrenceInfo->Periodicity >= 1)&&(ARecurrenceInfo->Periodicity <= 12)){
        AMonthStr = LongMonthNames[ARecurrenceInfo->Periodicity];
      }
      else {
        AMonthStr = "";
      }
      if (ARecurrenceInfo->DayType == Cxschedulerstorage::cxdtDay){
        Result = Format(("every %s %d", ARRAYOFCONST((AMonthStr, ARecurrenceInfo->DayNumber)));
      }
      else {
        ADayStr = GetDateParts(ADayStr, AWeekStr);
        Result = Format("the %s %s of %s", ARRAYOFCONST((AWeekStr, ADayStr, AMonthStr)));
      }
  }
  if (ARecurrenceInfo->Event->EventType == Cxschedulerstorage::etPattern){
      APattern = ARecurrenceInfo->Event;
  }
  else {
    APattern = ARecurrenceInfo->Event->Pattern;
    if (APattern == NULL){
      APattern = ARecurrenceInfo->Event;
    }
  }
  if (AFullDescription){
    return Result + "Occurs " + GetDateBounds(APattern) + GetTimeBounds(APattern) + '.';
  }
  else {
    return Result + GetTimeBounds(APattern);
  }
}
See Also