Wt examples: hangman/LettersWidget.C Source File

Wt examples 4.10.4

Loading...
Searching...
No Matches
LettersWidget.C
Go to the documentation of this file.
1/*
2 * Copyright (C) 2011 Emweb bv, Herent, Belgium
3 *
4 * See the LICENSE file for terms of use.
5 */
6
7#include "LettersWidget.h"
8
9#include <Wt/WApplication.h>
10#include <Wt/WEvent.h>
11#include <Wt/WPushButton.h>
12#include <Wt/WTable.h>
13
14using namespace Wt;
15
16LettersWidget::LettersWidget()
17{
18 impl_ = setNewImplementation<WTable>();
19
20 impl_->resize(13*30, WLength::Auto);
21
22 for (unsigned int i = 0; i < 26; ++i) {
23 std::string c(1, 'A' + i);
24 auto character = impl_->elementAt(i / 13, i % 13)->addNew<WPushButton>(c);
25 letterButtons_.push_back(character);
26 character->resize(WLength(30), WLength::Auto);
27
28 character->clicked().connect
29 (std::bind(&LettersWidget::processButton, this, character));
30
31 connections_.push_back(WApplication::instance()->globalKeyPressed().connect
32 (std::bind(&LettersWidget::processButtonPushed, this, std::placeholders::_1, character)));
33 }
34}
35
36LettersWidget::~LettersWidget()
37{
38 for (auto &connection : connections_)
39 connection.disconnect();
40}
41
42 void LettersWidget::processButton(WPushButton *b)
43{
44 b->disable();
45 letterPushed_.emit(b->text().toUTF8()[0]);
46}
47
48 void LettersWidget::processButtonPushed(const WKeyEvent &e, WPushButton *b)
49{
50 if (isHidden())
51 return;
52
53 if(e.key() == static_cast<Key>(b->text().toUTF8()[0]))
54 processButton(b);
55}
56
57 void LettersWidget::reset()
58{
59 for (auto& letterButton : letterButtons_)
60 letterButton->enable();
61
62 show();
63}
LettersWidget.h
LettersWidget::processButton
void processButton(Wt::WPushButton *b)
Definition LettersWidget.C:42
LettersWidget::letterButtons_
std::vector< Wt::WPushButton * > letterButtons_
Definition LettersWidget.h:31
LettersWidget::connections_
std::vector< Wt::Signals::connection > connections_
Definition LettersWidget.h:32
LettersWidget::impl_
Wt::WTable * impl_
Definition LettersWidget.h:30
LettersWidget::~LettersWidget
virtual ~LettersWidget()
Definition LettersWidget.C:36
LettersWidget::LettersWidget
LettersWidget()
Definition LettersWidget.C:16
LettersWidget::processButtonPushed
void processButtonPushed(const Wt::WKeyEvent &e, Wt::WPushButton *b)
Definition LettersWidget.C:48
LettersWidget::letterPushed_
Wt::Signal< char > letterPushed_
Definition LettersWidget.h:34
LettersWidget::reset
void reset()
Definition LettersWidget.C:57
Wt
Definition AddresseeEdit.h:16