• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeui
 

tdeui

  • tdeui
kcharselect.cpp
1/* This file is part of the KDE libraries
2
3 Copyright (C) 1999 Reginald Stadlbauer <reggie@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include "kcharselect.h"
22#include "kcharselect.moc"
23
24#include <tqbrush.h>
25#include <tqcolor.h>
26#include <tqevent.h>
27#include <tqfont.h>
28#include <tqfontdatabase.h>
29#include <tqhbox.h>
30#include <tqkeycode.h>
31#include <tqlabel.h>
32#include <tqpainter.h>
33#include <tqpen.h>
34#include <tqregexp.h>
35#include <tqstyle.h>
36#include <tqtooltip.h>
37#include <tqvalidator.h>
38
39#include <tdeapplication.h>
40#include <kdebug.h>
41#include <kdialog.h>
42#include <klineedit.h>
43#include <tdelocale.h>
44
45class KCharSelect::KCharSelectPrivate
46{
47public:
48 TQLineEdit *unicodeLine;
49};
50
51TQFontDatabase * KCharSelect::fontDataBase = 0;
52
53void KCharSelect::cleanupFontDatabase()
54{
55 delete fontDataBase;
56 fontDataBase = 0;
57}
58
59/******************************************************************/
60/* Class: KCharSelectTable */
61/******************************************************************/
62
63//==================================================================
64KCharSelectTable::KCharSelectTable( TQWidget *parent, const char *name, const TQString &_font,
65 const TQChar &_chr, int _tableNum )
66 : TQGridView( parent, name ), vFont( _font ), vChr( _chr ),
67 vTableNum( _tableNum ), vPos( 0, 0 ), focusItem( _chr ), focusPos( 0, 0 ), d(0)
68{
69 setBackgroundColor( colorGroup().base() );
70
71 setCellWidth( 20 );
72 setCellHeight( 25 );
73
74 setNumCols( 32 );
75 setNumRows( 8 );
76
77 repaintContents( false );
78
79 setToolTips();
80
81 setFocusPolicy( TQWidget::StrongFocus );
82 setBackgroundMode( TQWidget::NoBackground );
83}
84
85//==================================================================
86void KCharSelectTable::setFont( const TQString &_font )
87{
88 vFont = _font;
89 repaintContents( false );
90
91 setToolTips();
92}
93
94//==================================================================
95void KCharSelectTable::setChar( const TQChar &_chr )
96{
97 vChr = _chr;
98 repaintContents( false );
99}
100
101//==================================================================
102void KCharSelectTable::setTableNum( int _tableNum )
103{
104 focusItem = TQChar( _tableNum * 256 );
105
106 vTableNum = _tableNum;
107 repaintContents( false );
108
109 setToolTips();
110}
111
112//==================================================================
113TQSize KCharSelectTable::sizeHint() const
114{
115 int w = cellWidth();
116 int h = cellHeight();
117
118 w *= numCols();
119 h *= numRows();
120
121 return TQSize( w, h );
122}
123
124//==================================================================
125void KCharSelectTable::resizeEvent( TQResizeEvent * e )
126{
127 const int new_w = (e->size().width() - 2*(margin()+frameWidth())) / numCols();
128 const int new_h = (e->size().height() - 2*(margin()+frameWidth())) / numRows();
129
130 if( new_w != cellWidth())
131 setCellWidth( new_w );
132 if( new_h != cellHeight())
133 setCellHeight( new_h );
134
135 setToolTips();
136}
137
138//==================================================================
139void KCharSelectTable::paintCell( class TQPainter* p, int row, int col )
140{
141 const int w = cellWidth();
142 const int h = cellHeight();
143 const int x2 = w - 1;
144 const int y2 = h - 1;
145
146 //if( row == 0 && col == 0 ) {
147 // printf("Repaint %d\n", temp++);
148 // fflush( stdout );
149 // }
150
151 TQFont font = TQFont( vFont );
152 font.setPixelSize( int(.7 * h) );
153
154 unsigned short c = vTableNum * 256;
155 c += row * numCols();
156 c += col;
157
158 if ( c == vChr.unicode() ) {
159 p->setBrush( TQBrush( colorGroup().highlight() ) );
160 p->setPen( NoPen );
161 p->drawRect( 0, 0, w, h );
162 p->setPen( colorGroup().highlightedText() );
163 vPos = TQPoint( col, row );
164 } else {
165 TQFontMetrics fm = TQFontMetrics( font );
166 if( fm.inFont( c ) )
167 p->setBrush( TQBrush( colorGroup().base() ) );
168 else
169 p->setBrush( TQBrush( colorGroup().button() ) );
170 p->setPen( NoPen );
171 p->drawRect( 0, 0, w, h );
172 p->setPen( colorGroup().text() );
173 }
174
175 if ( c == focusItem.unicode() && hasFocus() ) {
176 style().drawPrimitive( TQStyle::PE_FocusRect, p, TQRect( 2, 2, w - 4, h - 4 ),
177 colorGroup() );
178 focusPos = TQPoint( col, row );
179 }
180
181 p->setFont( font );
182
183 p->drawText( 0, 0, x2, y2, AlignHCenter | AlignVCenter, TQString( TQChar( c ) ) );
184
185 p->setPen( colorGroup().text() );
186 p->drawLine( x2, 0, x2, y2 );
187 p->drawLine( 0, y2, x2, y2 );
188
189 if ( row == 0 )
190 p->drawLine( 0, 0, x2, 0 );
191 if ( col == 0 )
192 p->drawLine( 0, 0, 0, y2 );
193}
194
195//==================================================================
196void KCharSelectTable::mouseMoveEvent( TQMouseEvent *e )
197{
198 const int row = rowAt( e->y() );
199 const int col = columnAt( e->x() );
200 if ( row >= 0 && row < numRows() && col >= 0 && col < numCols() ) {
201 const TQPoint oldPos = vPos;
202
203 vPos.setX( col );
204 vPos.setY( row );
205
206 vChr = TQChar( vTableNum * 256 + numCols() * vPos.y() + vPos.x() );
207
208 const TQPoint oldFocus = focusPos;
209
210 focusPos = vPos;
211 focusItem = vChr;
212
213 repaintCell( oldFocus.y(), oldFocus.x(), true );
214 repaintCell( oldPos.y(), oldPos.x(), true );
215 repaintCell( vPos.y(), vPos.x(), true );
216
217 emit highlighted( vChr );
218 emit highlighted();
219
220 emit focusItemChanged( focusItem );
221 emit focusItemChanged();
222 }
223}
224
225//==================================================================
226void KCharSelectTable::keyPressEvent( TQKeyEvent *e )
227{
228 switch ( e->key() ) {
229 case Key_Left:
230 gotoLeft();
231 break;
232 case Key_Right:
233 gotoRight();
234 break;
235 case Key_Up:
236 gotoUp();
237 break;
238 case Key_Down:
239 gotoDown();
240 break;
241 case Key_Next:
242 emit tableDown();
243 break;
244 case Key_Prior:
245 emit tableUp();
246 break;
247 case Key_Space:
248 emit activated( ' ' );
249 emit activated();
250 emit highlighted( ' ' );
251 emit highlighted();
252 break;
253 case Key_Enter: case Key_Return: {
254 const TQPoint oldPos = vPos;
255
256 vPos = focusPos;
257 vChr = focusItem;
258
259 repaintCell( oldPos.y(), oldPos.x(), true );
260 repaintCell( vPos.y(), vPos.x(), true );
261
262 emit activated( vChr );
263 emit activated();
264 emit highlighted( vChr );
265 emit highlighted();
266 } break;
267 }
268}
269
270//==================================================================
271void KCharSelectTable::gotoLeft()
272{
273 if ( focusPos.x() > 0 ) {
274 const TQPoint oldPos = focusPos;
275
276 focusPos.setX( focusPos.x() - 1 );
277
278 focusItem = TQChar( vTableNum * 256 + numCols() * focusPos.y() + focusPos.x() );
279
280 repaintCell( oldPos.y(), oldPos.x(), true );
281 repaintCell( focusPos.y(), focusPos.x(), true );
282
283 emit focusItemChanged( vChr );
284 emit focusItemChanged();
285 }
286}
287
288//==================================================================
289void KCharSelectTable::gotoRight()
290{
291 if ( focusPos.x() < numCols()-1 ) {
292 const TQPoint oldPos = focusPos;
293
294 focusPos.setX( focusPos.x() + 1 );
295
296 focusItem = TQChar( vTableNum * 256 + numCols() * focusPos.y() + focusPos.x() );
297
298 repaintCell( oldPos.y(), oldPos.x(), true );
299 repaintCell( focusPos.y(), focusPos.x(), true );
300
301 emit focusItemChanged( vChr );
302 emit focusItemChanged();
303 }
304}
305
306//==================================================================
307void KCharSelectTable::gotoUp()
308{
309 if ( focusPos.y() > 0 ) {
310 const TQPoint oldPos = focusPos;
311
312 focusPos.setY( focusPos.y() - 1 );
313
314 focusItem = TQChar( vTableNum * 256 + numCols() * focusPos.y() + focusPos.x() );
315
316 repaintCell( oldPos.y(), oldPos.x(), true );
317 repaintCell( focusPos.y(), focusPos.x(), true );
318
319 emit focusItemChanged( vChr );
320 emit focusItemChanged();
321 }
322}
323
324//==================================================================
325void KCharSelectTable::gotoDown()
326{
327 if ( focusPos.y() < numRows()-1 ) {
328 const TQPoint oldPos = focusPos;
329
330 focusPos.setY( focusPos.y() + 1 );
331
332 focusItem = TQChar( vTableNum * 256 + numCols() * focusPos.y() + focusPos.x() );
333
334 repaintCell( oldPos.y(), oldPos.x(), true );
335 repaintCell( focusPos.y(), focusPos.x(), true );
336
337 emit focusItemChanged( vChr );
338 emit focusItemChanged();
339 }
340}
341
342//==================================================================
343void KCharSelectTable::setToolTips()
344{
345 const int rowCount = numRows();
346 const int colCount = numCols();
347 for( int i=0 ; i< rowCount; ++i )
348 {
349 for( int j=0; j< colCount; ++j )
350 {
351 const TQRect r( cellWidth()*j, cellHeight()*i, cellWidth(), cellHeight() );
352 TQToolTip::remove(this,r);
353 const ushort uni = vTableNum * 256 + numCols()*i + j;
354 TQString s;
355 s.sprintf( "%04X", uint( uni ) );
356 TQString character; // Character (which sometimes need to be escaped)
357 switch ( uni )
358 {
359 case 0x3c: character = "&lt;"; break;
360 case 0x3e: character = "&gt;"; break;
361 case 0x26: character = "&amp;"; break;
362 case 0x27: character = "&apos;"; break;
363 case 0x22: character = "&quot;"; break;
364 default: character = TQChar( uni ); break;
365 }
366 TQToolTip::add(this, r, i18n( "Character","<qt><font size=\"+4\" face=\"%1\">%2</font><br>Unicode code point: U+%3<br>(In decimal: %4)<br>(Character: %5)</qt>" ).arg( vFont ).arg( character ).arg( s ).arg( uni ).arg( character ) );
367 }
368 }
369}
370
371/******************************************************************/
372/* Class: KCharSelect */
373/******************************************************************/
374
375//==================================================================
376KCharSelect::KCharSelect( TQWidget *parent, const char *name, const TQString &_font, const TQChar &_chr, int _tableNum )
377 : TQVBox( parent, name ), d(new KCharSelectPrivate)
378{
379 setSpacing( KDialog::spacingHint() );
380 TQHBox* const bar = new TQHBox( this );
381 bar->setSpacing( KDialog::spacingHint() );
382
383 TQLabel* const lFont = new TQLabel( i18n( "Font:" ), bar );
384 lFont->resize( lFont->sizeHint() );
385 lFont->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
386 lFont->setMaximumWidth( lFont->sizeHint().width() );
387
388 fontCombo = new TQComboBox( true, bar );
389 fillFontCombo();
390 fontCombo->resize( fontCombo->sizeHint() );
391
392 connect( fontCombo, TQ_SIGNAL( activated( const TQString & ) ), this, TQ_SLOT( fontSelected( const TQString & ) ) );
393
394 TQLabel* const lTable = new TQLabel( i18n( "Table:" ), bar );
395 lTable->resize( lTable->sizeHint() );
396 lTable->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
397 lTable->setMaximumWidth( lTable->sizeHint().width() );
398
399 tableSpinBox = new TQSpinBox( 0, 255, 1, bar );
400 tableSpinBox->resize( tableSpinBox->sizeHint() );
401
402 connect( tableSpinBox, TQ_SIGNAL( valueChanged( int ) ), this, TQ_SLOT( tableChanged( int ) ) );
403
404 TQLabel* const lUnicode = new TQLabel( i18n( "&Unicode code point:" ), bar );
405 lUnicode->resize( lUnicode->sizeHint() );
406 lUnicode->setAlignment( TQt::AlignRight | TQt::AlignVCenter );
407 lUnicode->setMaximumWidth( lUnicode->sizeHint().width() );
408
409 const TQRegExp rx( "[a-fA-F0-9]{1,4}" );
410 TQValidator* const validator = new TQRegExpValidator( rx, this );
411
412 d->unicodeLine = new KLineEdit( bar );
413 d->unicodeLine->setValidator(validator);
414 lUnicode->setBuddy(d->unicodeLine);
415 d->unicodeLine->resize( d->unicodeLine->sizeHint() );
416 slotUpdateUnicode(_chr);
417
418 connect( d->unicodeLine, TQ_SIGNAL( returnPressed() ), this, TQ_SLOT( slotUnicodeEntered() ) );
419
420 charTable = new KCharSelectTable( this, name, _font.isEmpty() ? TQString(TQVBox::font().family()) : _font, _chr, _tableNum );
421 const TQSize sz( charTable->contentsWidth() + 4 ,
422 charTable->contentsHeight() + 4 );
423 charTable->resize( sz );
424 //charTable->setMaximumSize( sz );
425 charTable->setMinimumSize( sz );
426 charTable->setHScrollBarMode( TQScrollView::AlwaysOff );
427 charTable->setVScrollBarMode( TQScrollView::AlwaysOff );
428
429 setFont( _font.isEmpty() ? TQString(TQVBox::font().family()) : _font );
430 setTableNum( _tableNum );
431
432 connect( charTable, TQ_SIGNAL( highlighted( const TQChar & ) ), this, TQ_SLOT( slotUpdateUnicode( const TQChar & ) ) );
433 connect( charTable, TQ_SIGNAL( highlighted( const TQChar & ) ), this, TQ_SLOT( charHighlighted( const TQChar & ) ) );
434 connect( charTable, TQ_SIGNAL( highlighted() ), this, TQ_SLOT( charHighlighted() ) );
435 connect( charTable, TQ_SIGNAL( activated( const TQChar & ) ), this, TQ_SLOT( charActivated( const TQChar & ) ) );
436 connect( charTable, TQ_SIGNAL( activated() ), this, TQ_SLOT( charActivated() ) );
437 connect( charTable, TQ_SIGNAL( focusItemChanged( const TQChar & ) ),
438 this, TQ_SLOT( charFocusItemChanged( const TQChar & ) ) );
439 connect( charTable, TQ_SIGNAL( focusItemChanged() ), this, TQ_SLOT( charFocusItemChanged() ) );
440 connect( charTable, TQ_SIGNAL( tableUp() ), this, TQ_SLOT( charTableUp() ) );
441 connect( charTable, TQ_SIGNAL( tableDown() ), this, TQ_SLOT( charTableDown() ) );
442
443 connect( charTable, TQ_SIGNAL(doubleClicked()),this,TQ_SLOT(slotDoubleClicked()));
444
445 setFocusPolicy( TQWidget::StrongFocus );
446 setFocusProxy( charTable );
447}
448
449KCharSelect::~KCharSelect()
450{
451 delete d;
452}
453
454//==================================================================
455TQSize KCharSelect::sizeHint() const
456{
457 return TQVBox::sizeHint();
458}
459
460//==================================================================
461void KCharSelect::setFont( const TQString &_font )
462{
463 const TQValueList<TQString>::Iterator it = fontList.find( _font );
464 if ( it != fontList.end() ) {
465 TQValueList<TQString>::Iterator it2 = fontList.begin();
466 int pos = 0;
467 for ( ; it != it2; ++it2, ++pos);
468 fontCombo->setCurrentItem( pos );
469 charTable->setFont( _font );
470 }
471 else
472 kdWarning() << "Can't find Font: " << _font << endl;
473}
474
475//==================================================================
476void KCharSelect::setChar( const TQChar &_chr )
477{
478 charTable->setChar( _chr );
479 slotUpdateUnicode( _chr );
480}
481
482//==================================================================
483void KCharSelect::setTableNum( int _tableNum )
484{
485 tableSpinBox->setValue( _tableNum );
486 charTable->setTableNum( _tableNum );
487}
488
489//==================================================================
490void KCharSelect::fillFontCombo()
491{
492 if ( !fontDataBase ) {
493 fontDataBase = new TQFontDatabase();
494 tqAddPostRoutine( cleanupFontDatabase );
495 }
496 fontList=fontDataBase->families();
497 fontCombo->insertStringList( fontList );
498}
499
500//==================================================================
501void KCharSelect::fontSelected( const TQString &_font )
502{
503 charTable->setFont( _font );
504 emit fontChanged( _font );
505}
506
507//==================================================================
508void KCharSelect::tableChanged( int _value )
509{
510 charTable->setTableNum( _value );
511}
512
513//==================================================================
514void KCharSelect::slotUnicodeEntered( )
515{
516 const TQString s = d->unicodeLine->text();
517 if (s.isEmpty())
518 return;
519
520 bool ok;
521 const int uc = s.toInt(&ok, 16);
522 if (!ok)
523 return;
524
525 const int table = uc / 256;
526 charTable->setTableNum( table );
527 tableSpinBox->setValue(table);
528 const TQChar ch(uc);
529 charTable->setChar( ch );
530 charActivated( ch );
531}
532
533void KCharSelect::slotUpdateUnicode( const TQChar &c )
534{
535 const int uc = c.unicode();
536 TQString s;
537 s.sprintf("%04X", uc);
538 d->unicodeLine->setText(s);
539}
540
541void KCharSelectTable::virtual_hook( int, void*)
542{ /*BASE::virtual_hook( id, data );*/ }
543
544void KCharSelect::virtual_hook( int, void* )
545{ /*BASE::virtual_hook( id, data );*/ }
546
KCharSelectTable
Character selection table.
Definition: kcharselect.h:52
KCharSelect::KCharSelect
KCharSelect(TQWidget *parent, const char *name, const TQString &font=TQString::null, const TQChar &chr=' ', int tableNum=0)
Constructor.
Definition: kcharselect.cpp:376
KCharSelect::setChar
virtual void setChar(const TQChar &chr)
Sets the currently selected character to chr.
Definition: kcharselect.cpp:476
KCharSelect::setFont
virtual void setFont(const TQString &font)
Sets the font which is displayed to font.
Definition: kcharselect.cpp:461
KCharSelect::sizeHint
virtual TQSize sizeHint() const
Reimplemented.
Definition: kcharselect.cpp:455
KCharSelect::setTableNum
virtual void setTableNum(int tableNum)
Sets the currently displayed table to tableNum.
Definition: kcharselect.cpp:483
KDialog::spacingHint
static int spacingHint()
Return the number of pixels you shall use between widgets inside a dialog according to the KDE standa...
Definition: kdialog.cpp:110
KLineEdit
An enhanced TQLineEdit widget for inputting text.
Definition: klineedit.h:146
kdWarning
kdbgstream kdWarning(int area=0)
endl
kndbgstream & endl(kndbgstream &s)
TDEStdAccel::name
TQString name(StdAccel id)
tdelocale.h

tdeui

Skip menu "tdeui"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdeui

Skip menu "tdeui"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeui by doxygen 1.9.4
This website is maintained by Timothy Pearson.