00001 /* 00002 * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium. 00003 * 00004 * See the LICENSE file for terms of use. 00005 */ 00006 00007 #include "FormExample.h" 00008 #include "Form.h" 00009 00010 #include <WApplication> 00011 #include <WText> 00012 #include <WStringUtil> 00013 00014 FormExample::FormExample(WContainerWidget *parent) 00015 : WContainerWidget(parent) 00016 { 00017 WContainerWidget *langLayout = new WContainerWidget(this); 00018 langLayout->setContentAlignment(AlignRight); 00019 new WText(tr("language"), langLayout); 00020 00021 char *lang[] = { "en", "nl" }; 00022 00023 for (int i = 0; i < 2; ++i) { 00024 WText *t = new WText(widen(lang[i]), langLayout); 00025 t->setMargin(5); 00026 t->clicked.connect(SLOT(this, FormExample::changeLanguage)); 00027 00028 languageSelects_.push_back(t); 00029 } 00030 00031 /* 00032 * Start with the reported locale, if available 00033 */ 00034 setLanguage(wApp->locale()); 00035 00036 Form *form = new Form(this); 00037 form->setMargin(20); 00038 } 00039 00040 void FormExample::setLanguage(const std::string lang) 00041 { 00042 bool haveLang = false; 00043 00044 for (unsigned i = 0; i < languageSelects_.size(); ++i) { 00045 WText *t = languageSelects_[i]; 00046 00047 // prefix match, e.g. en matches en-us. 00048 bool isLang = lang.find(narrow(t->text().value())) == 0; 00049 t->setStyleClass(isLang ? L"langcurrent" : L"lang"); 00050 00051 haveLang = haveLang || isLang; 00052 } 00053 00054 if (!haveLang) { 00055 languageSelects_[0]->setStyleClass(L"langcurrent"); 00056 WApplication::instance() 00057 ->setLocale(narrow(languageSelects_[0]->text().value())); 00058 } else 00059 WApplication::instance()->setLocale(lang); 00060 } 00061 00062 void FormExample::changeLanguage() 00063 { 00064 WText *t = (WText *)sender(); 00065 setLanguage(narrow(t->text().value())); 00066 } 00067 00068 WApplication *createApplication(const WEnvironment& env) 00069 { 00070 WApplication *app = new WApplication(env); 00071 app->messageResourceBundle().use("form-example"); 00072 app->setTitle(L"Form example"); 00073 00074 app->root()->addWidget(new FormExample()); 00075 00076 WCssDecorationStyle langStyle; 00077 langStyle.font().setSize(WFont::Smaller); 00078 langStyle.setCursor(WCssDecorationStyle::Pointer); 00079 langStyle.setForegroundColor(Wt::blue); 00080 langStyle.setTextDecoration(WCssDecorationStyle::Underline); 00081 app->styleSheet().addRule(".lang", langStyle); 00082 00083 langStyle.setCursor(WCssDecorationStyle::Default); 00084 langStyle.font().setWeight(WFont::Bold); 00085 app->styleSheet().addRule(".langcurrent", langStyle); 00086 00087 return app; 00088 } 00089 00090 int main(int argc, char **argv) 00091 { 00092 return WRun(argc, argv, &createApplication); 00093 } 00094