File: CoderWidget.C

package info (click to toggle)
witty 3.3.3%2Bdfsg-4.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 28,228 kB
  • ctags: 26,694
  • sloc: cpp: 147,809; ansic: 77,999; xml: 16,331; sh: 1,303; makefile: 198; java: 86; sql: 14
file content (132 lines) | stat: -rw-r--r-- 3,104 bytes parent folder | download | duplicates (2)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright (C) 2010 Emweb bvba, Heverlee, Belgium.
 *
 * See the LICENSE file for terms of use.
 */
#include <Wt/WApplication>
#include <Wt/WPushButton>
#include <Wt/WLineEdit>
#include <Wt/WTextArea>
#include <Wt/WText>

#include "CoderWidget.h"

using namespace Wt;

class BufferEditorWidget : public WContainerWidget
{
public:
  BufferEditorWidget()
  {
    setStyleClass("editor");

    new WText("File: ", this);
    nameEdit_ = new WLineEdit(this);

    textArea_ = new WTextArea(this);
    textArea_->setAttributeValue("spellcheck", "false");
    textArea_->setInline(false);
    textArea_->setColumns(80);
    textArea_->resize(WLength::Auto, 300);
  }

  WString name() { return nameEdit_->text(); }
  WString text() {
    std::wstring t = textArea_->text();

    if (textArea_->hasFocus()) {
      if (textArea_->hasSelectedText()) {
	int i = textArea_->selectionStart();
	int j = ((std::wstring) textArea_->selectedText()).length();

	t = escape(t.substr(0, i)) + L"<span class=\"sel\">"
	  + escape(t.substr(i, j)) + L"</span>"
	  + escape(t.substr(i + j));
      } else {
	int i = textArea_->cursorPosition();
	if (i >= 0) {
	  std::wstring s;
	  s = escape(t.substr(0, i)) + L"<span class=\"pos\">";

	  if (i + 1 < (int)t.length()) {
	    if (t[i] == '\n')
	      s += L' ';
	    s += escape(t.substr(i, 1)) + L"</span>";
	    s += escape(t.substr(i + 1));
	  } else
	    s += L" </span>";

	  t = s;
	}
      }
    } else
      t = escape(t);

    return t; 
  }

  WTextArea *textArea() { return textArea_; }

private:
  WLineEdit *nameEdit_;
  WTextArea *textArea_;

  std::wstring escape(const std::wstring& s) {
    return (std::wstring) escapeText(WString(s));
  }
};

CoderWidget::CoderWidget()
{
  WApplication::instance()->enableUpdates(true);

  session_ = new CodeSession(boost::bind(&CoderWidget::sessionChanged, this));

  WApplication::instance()->setInternalPath("/" + session_->id());

  WPushButton *addBuffer = new WPushButton("Add file", this);
  observerCount_ = new WText("Observers: 0", this);

  addBuffer->clicked().connect(this, &CoderWidget::addBuffer);

  buffers_ = new WContainerWidget(this);

  insertBuffer(0);
}

CoderWidget::~CoderWidget()
{
  session_->removeCoder();
  WApplication::instance()->enableUpdates(false);
}

void CoderWidget::addBuffer()
{
  insertBuffer(buffers_->count());
}

void CoderWidget::insertBuffer(int index)
{
  session_->insertBuffer(index);

  BufferEditorWidget *editor = new BufferEditorWidget();
  editor->keyWentUp().connect(boost::bind(&CoderWidget::changed, this, editor));
  editor->clicked().connect(boost::bind(&CoderWidget::changed, this, editor));
  editor->textArea()->blurred()
    .connect(boost::bind(&CoderWidget::changed, this, editor));

  buffers_->insertWidget(index, editor);
}

void CoderWidget::changed(BufferEditorWidget *editor)
{
  session_->updateBuffer(buffers_->indexOf(editor), editor->name(),
			 editor->text());
}

void CoderWidget::sessionChanged()
{
  observerCount_->setText(WString("Observers: {1}")
			  .arg(session_->observerCount()));
  WApplication::instance()->triggerUpdate();
}