summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-console/widget_layer.cpp
blob: 96dda1c343755b6527103ccd2b6909cb25c1705f (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* ------------------------------------------------------------------------- */
/*                                                                           */
/*  widget_layer.cpp          Widget Layer                                   */
/*                                                                           */
/*              opie developers <opie@handhelds.org>                         */
/*                                                                           */
/* ------------------------------------------------------------------------- */



#include "widget_layer.h"


#include <string.h>
//#include <




WidgetLayer::WidgetLayer( const Profile &config, QWidget *parent, const char *name  ) : QFrame( parent, name )
{
	// get the clipboard
	m_clipboard = QApplication::clipboard();
	
	// when data on clipboard changes, clear selection
	QObject::connect( (QObject*) m_clipboard, SIGNAL( dataChanged() ),
			(QObject*)this, SLOT( onClearSelection() ) );

	// initialize vars:
	m_lines = 1;
	m_columns = 1;
	m_resizing = false;
	
	// just for demonstrating
	//m_image = QArray<Character>( m_lines * m_columns ); 
	m_image = QArray<Character>( 1 );
	
	// we need to install an event filter,
	// to emit keypresses.
	qApp->installEventFilter( this );

}


WidgetLayer::~WidgetLayer()
{
	// clean up
	delete m_image;
}


QSize WidgetLayer::sizeHint()
{
    return size();
}


/* --------------------------------- audio ---------------------------------- */

void WidgetLayer::bell()
{
	QApplication::beep();
}

bool WidgetLayer::eventFilter( QObject *obj, QEvent *e )
{
    if ( (e->type() == QEvent::Accel ||
          e->type() == QEvent::AccelAvailable ) && qApp->focusWidget() == this )  {
        static_cast<QKeyEvent *>( e )->ignore();
        return true;
    }
    if ( obj != this /* when embedded */ && obj != parent() /* when standalone */ )
        return false; // not us

#ifdef FAKE_CTRL_AND_ALT
    static bool control = false;
    static bool alt = false;
//    qDebug(" Has a keyboard with no CTRL and ALT keys, but we fake it:");
    bool dele = false;
    if ( e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease ) {
        QKeyEvent* ke = (QKeyEvent*)e;
        bool keydown = e->type() == QEvent::KeyPress || ke->isAutoRepeat();
        switch (ke->key()) {
          case Key_F9: // let this be "Control"
              control = keydown;
              e = new QKeyEvent(QEvent::KeyPress, Key_Control, 0, ke->state());
              dele=TRUE;
              break;
          case Key_F13: // let this be "Alt"
	      alt = keydown;
              e = new QKeyEvent(QEvent::KeyPress, Key_Alt, 0, ke->state());
              dele=TRUE;
              break;
          default:
              if ( control ) {
                  int a = toupper(ke->ascii())-64;
                  if ( a >= 0 && a < ' ' ) {
                      e = new QKeyEvent(e->type(), ke->key(),
                                        a, ke->state()|ControlButton,
QChar(a,0));
                      dele=TRUE;
                  }
              }
              if ( alt ) {
                  e = new QKeyEvent(e->type(), ke->key(),
                                    ke->ascii(), ke->state()|AltButton, ke->text());
                  dele=TRUE;
              }
        }
    }
#endif

    if ( e->type() == QEvent::KeyPress )  {
        QKeyEvent* ke = (QKeyEvent*)e;
        //actSel=0; // Key stroke implies a screen update, so Widget won't
          // know where the current selection is.

//     qDebug("key pressed is 0x%x",ke->key());

        if( ke->state() == ShiftButton && ke->key() == Key_Tab) { //lets hardcode this sucker
            insertText("\\"); // expose
        } else
            emit keyPressed( ke ); // expose
        ke->accept();
#ifdef FAKE_CTRL_AND_ALT
        if ( dele ) delete e;
#endif
        return true;               // stop the event
    }
    return QFrame::eventFilter( obj, e );
}


/* --------------------------------- screen --------------------------------- */


void WidgetLayer::propagateSize()
{
	QArray<Character> oldimage = m_image.copy();
	int oldlines = m_lines;
	int oldcolumns = m_columns;
	
	makeImage();
	
	// copy old image, to reduce flicker
	if ( ! oldimage.isEmpty() )
	{
		int lins = QMIN( oldlines, m_lines );
		int cols = QMIN( oldcolumns, m_columns );
		for ( int lin = 0; lin < lins; ++lin )
		{
			memcpy( (void*) &m_image[m_columns*lin],
				(void*) &oldimage[oldcolumns*lin],
				cols*sizeof( Character ) );
		}
	}
	else
		clearImage();
	
	delete oldimage;

	m_resizing = true;
	emit imageSizeChanged( m_lines, m_columns );
	m_resizing = false;
}

void WidgetLayer::makeImage()
{
	calcGeometry();
	m_image = QArray<Character>( m_columns * m_lines );
	clearImage();
}

void WidgetLayer::clearImage()
{
	//should this belong here??
	for ( int y = 0; y < m_lines; y++ )
	for ( int x = 0; x < m_columns; x++ )
	{
		m_image[loc(x,y)].c = 0xff; 
		m_image[loc(x,y)].f = 0xff;
		m_image[loc(x,y)].b = 0xff;
		m_image[loc(x,y)].r = 0xff;
	}
}

/* --------------------------------- selection ------------------------------ */

void WidgetLayer::pasteClipboard()
{
	insertSelection();
}


void WidgetLayer::insertSelection()
{
	QString text = QApplication::clipboard()->text();
	if ( ! text.isNull() )
	{
		text.replace( QRegExp( "\n" ), "\r" );
		insertText( text );
		// selection should be unselected
		emit selectionCleared();
	}
}

void WidgetLayer::insertText( QString text )
{
	// text is inserted as key event
	QKeyEvent e( QEvent::KeyPress, 0, -1, 0, text);
	emit keyPressed( &e );
}

void WidgetLayer::onClearSelection()
{
	emit selectionCleared();
}

void WidgetLayer::setSelection( const QString& text )
{
	// why get the clipboard, we have it as instance var...
	QObject *m_clipboard = QApplication::clipboard();

	// we know, that cliboard changes, when we change clipboard
	QObject::disconnect( (QObject*) m_clipboard, SIGNAL( dataChanged() ),
			(QObject*) this, SLOT( selectionCleared() ) );
	
	QApplication::clipboard()->setText( text );

	QObject::connect( (QObject*) m_clipboard, SIGNAL( dataChanged() ),
			(QObject*) this, SLOT( selectionCleared() ) );
}


/////////
// special font characters
/////////
unsigned short vt100_graphics[32] =
{ // 0/8     1/9    2/10    3/11    4/12    5/13    6/14    7/15
  0x0020, 0x25C6, 0x2592, 0x2409, 0x240c, 0x240d, 0x240a, 0x00b0,
  0x00b1, 0x2424, 0x240b, 0x2518, 0x2510, 0x250c, 0x2514, 0x253c,
  0xF800, 0xF801, 0x2500, 0xF803, 0xF804, 0x251c, 0x2524, 0x2534,
  0x252c, 0x2502, 0x2264, 0x2265, 0x03C0, 0x2260, 0x00A3, 0x00b7
};