File: FormExample.C

package info (click to toggle)
witty 3.1.2-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 45,512 kB
  • ctags: 35,832
  • sloc: cpp: 69,469; ansic: 66,945; xml: 4,383; sh: 594; perl: 108; makefile: 106
file content (94 lines) | stat: -rw-r--r-- 2,329 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
 *
 * See the LICENSE file for terms of use.
 */

#include "FormExample.h"
#include "Form.h"

#include <Wt/WApplication>
#include <Wt/WText>
#include <Wt/WStringUtil>

FormExample::FormExample(WContainerWidget *parent)
  : WContainerWidget(parent)
{
  WContainerWidget *langLayout = new WContainerWidget(this);
  langLayout->setContentAlignment(AlignRight);
  new WText(tr("language"), langLayout);

  const char *lang[] = { "en", "nl" };

  for (int i = 0; i < 2; ++i) {
    WText *t = new WText(widen(lang[i]), langLayout);
    t->setMargin(5);
    t->clicked().connect(SLOT(this, FormExample::changeLanguage));

    languageSelects_.push_back(t);
  }

  /*
   * Start with the reported locale, if available
   */
  setLanguage(wApp->locale());

  Form *form = new Form(this);
  form->setMargin(20);
}

void FormExample::setLanguage(const std::string lang)
{
  bool haveLang = false;

  for (unsigned i = 0; i < languageSelects_.size(); ++i) {
    WText *t = languageSelects_[i];

    // prefix match, e.g. en matches en-us.
    bool isLang = lang.find(narrow(t->text().value())) == 0;
    t->setStyleClass(isLang ? L"langcurrent" : L"lang");

    haveLang = haveLang || isLang;
  }

  if (!haveLang) {
    languageSelects_[0]->setStyleClass(L"langcurrent");
    WApplication::instance()
      ->setLocale(narrow(languageSelects_[0]->text().value()));
  } else
    WApplication::instance()->setLocale(lang);
}

void FormExample::changeLanguage()
{
  WText *t = (WText *)sender();
  setLanguage(narrow(t->text().value()));
}

WApplication *createApplication(const WEnvironment& env)
{
  WApplication *app = new WApplication(env);
  app->messageResourceBundle().use("form-example");
  app->setTitle("Form example");

  app->root()->addWidget(new FormExample());

  WCssDecorationStyle langStyle;
  langStyle.font().setSize(WFont::Smaller);
  langStyle.setCursor(PointingHandCursor);
  langStyle.setForegroundColor(blue);
  langStyle.setTextDecoration(WCssDecorationStyle::Underline);
  app->styleSheet().addRule(".lang", langStyle);

  langStyle.setCursor(ArrowCursor);
  langStyle.font().setWeight(WFont::Bold);
  app->styleSheet().addRule(".langcurrent", langStyle);

  return app;
}

int main(int argc, char **argv)
{
   return WRun(argc, argv, &createApplication);
}