首页 > 代码库 > LocalUI

LocalUI

  1 //LocaleUI.cpp  2   3   4 #include "vcf/ApplicationKit/ApplicationKit.h"  5 #include "vcf/ApplicationKit/TitledBorder.h"  6   7 using namespace VCF;  8   9  10  11 class DateTimeLabel : public CustomControl { 12 protected: 13     TimerComponent* timer; 14     String extraTxt; 15     Locale* locale; 16     void onTimer( Event* e ) { 17         repaint(); 18     } 19 public: 20     DateTimeLabel() : CustomControl(false),locale(NULL){ 21  22         setBorder( new TitledBorder() ); 23  24         timer = new TimerComponent(this); 25         timer->setTimeoutInterval ( 1000 ); 26         timer->TimerPulse += 27             new ClassProcedure1<Event*,DateTimeLabel>(this, &DateTimeLabel::onTimer, "DateTimeLabel::onTimer" ); 28     } 29  30     virtual ~DateTimeLabel() { 31         delete locale; 32     } 33  34     void setLocale( Locale* loc ) { 35         if ( NULL != locale ) { 36             delete locale; 37         } 38  39         locale = new Locale( loc->getLanguageCode(), loc->getCountryCode() ); 40  41         TitledBorder* border = (TitledBorder*)getBorder(); 42         border->setCaption( locale->getLanguageName() ); 43         border->getFont()->setLocale( locale ); 44     } 45  46     void setLocale( const String& lang, const String& country ) { 47         if ( NULL != locale ) { 48             delete locale; 49         } 50  51         locale = new Locale( lang, country ); 52  53         TitledBorder* border = (TitledBorder*)getBorder(); 54         border->setCaption( locale->getLanguageName() ); 55         border->getFont()->setLocale( locale ); 56     } 57  58     void start() { 59         timer->setActivated ( true ); 60     } 61  62     void stop() { 63         timer->setActivated ( false ); 64     } 65  66     void setExtraTxt( const String& val ) { 67         extraTxt = val; 68         repaint(); 69     } 70  71     String getExtraTxt() { 72         return extraTxt; 73     } 74  75     virtual void paint( GraphicsContext* ctx ) { 76         CustomControl::paint( ctx ); 77  78         //get the current locale 79         Locale* currentLocale = System::getCurrentThreadLocale(); 80  81         if ( NULL != locale ) { 82             currentLocale = locale; 83         } 84  85  86  87         String localizedExtra = extraTxt; 88  89         ctx->getCurrentFont()->setName( "Times New Roman" ); 90         ctx->getCurrentFont()->setPointSize( 16 ); 91  92  93  94  95         //check if we can localize the string 96         if ( getUseLocaleStrings() ) { 97             //Yep! Let‘s get the localized version. Worst case scenario is that 98             //no translation exists, which means localizedExtra will be the same 99             //as extraTxt100             localizedExtra =   currentLocale->translate( extraTxt );101         }102 103         DateTime dt = DateTime::now();104         //localize the date/time value into a string105         String dateStr = currentLocale->toStringFromDate( dt, "dddd, MMM d yyyy" );106         String timeStr = currentLocale->toStringFromTime ( dt );107 108         Rect r = getClientBounds();109 110         Rect xtraRect = r;111         xtraRect.bottom_ = xtraRect.top_ + ctx->getTextHeight( localizedExtra );112 113         int32 textDrawOptions = GraphicsContext::tdoCenterHorzAlign;114 115         ctx->textBoundedBy( &xtraRect, localizedExtra, textDrawOptions );116 117         Rect textRect = r;118 119         textRect.inflate( -10, -10 );120         textRect.top_ = xtraRect.bottom_;121 122 123         ctx->getCurrentFont()->setBold( true );124 125         textDrawOptions = GraphicsContext::tdoWordWrap | GraphicsContext::tdoCenterHorzAlign;126         ctx->textBoundedBy( &textRect, dateStr + "\n" + timeStr, textDrawOptions );127     }128 };129 130 class LocaleUIWindow : public Window {131 public:132     LocaleUIWindow() {133         setCaption( "LocaleUI" );134 135         DateTimeLabel* label;136 137         label = new DateTimeLabel();138 139         label->setLocale( "en", "US" );140 141         label->setExtraTxt( "Hello it‘s:" );142 143         label->setHeight( 100 );144 145         add( label, AlignTop );146 147         label->start();148 149 150         label = new DateTimeLabel();151 152         label->setLocale( "it", "IT" );153 154         label->setExtraTxt( "Hello it‘s:" );155 156         label->setHeight( 100 );157 158         add( label, AlignTop );159 160         label->start();161 162 163         label = new DateTimeLabel();164 165         label->setLocale( "pl", "PL" );166 167         label->setExtraTxt( "Hello it‘s:" );168 169         label->setHeight( 100 );170 171         add( label, AlignTop );172 173         label->start();174 175 176         label = new DateTimeLabel();177 178         label->setLocale( "de", "DE" );179 180         label->setExtraTxt( "Hello it‘s:" );181 182         label->setHeight( 100 );183 184         add( label, AlignTop );185 186         label->start();187 188 189         label = new DateTimeLabel();190 191         Locale loc( Locale::lcJapanese, Locale::ccJapan );192         label->setLocale( &loc );193 194         label->setExtraTxt( "Hello it‘s:" );195 196         label->setHeight( 100 );197 198         add( label, AlignTop );199 200         label->start();201 202         label = new DateTimeLabel();203 204         Locale loc2( Locale::lcRussian, Locale::ccRussianFederation );205         label->setLocale( &loc2 );206 207         label->setExtraTxt( "Hello it‘s:" );208 209         label->setHeight( 100 );210 211         add( label, AlignTop );212 213         label->start();214     }215 216     virtual ~LocaleUIWindow(){};217 218 };219 220 221 222 223 class LocaleUIApplication : public Application {224 public:225 226     LocaleUIApplication( int argc, char** argv ) : Application(argc, argv) {227 228     }229 230     virtual bool initRunningApplication(){231         bool result = Application::initRunningApplication();232 233         Window* mainWindow = new LocaleUIWindow();234         setMainWindow(mainWindow);235         mainWindow->setBounds( 100.0, 100.0, 350.0, 620.0 );236         mainWindow->show();237 238         return result;239     }240 241 };242 243 244 int main(int argc, char *argv[])245 {246     Application* app = new LocaleUIApplication( argc, argv );247 248     Application::main();249 250     return 0;251 }

 

LocalUI