summaryrefslogtreecommitdiffstats
path: root/khangman/khangman/khangmanview.cpp
blob: 3cf188e2e39fd6a70783e1e526757bcacb7fe6e2 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/*
 * Copyright (C) 2001-2005 Anne-Marie Mahfouf <annma@kde.org>

    This program is free software; you can redistribute it and/or
    modify it under the terms of version 2 of the GNU General Public
    License as published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <kapplication.h>
#include <kaudioplayer.h>
#include <kdebug.h>
#include <klineedit.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kpassivepopup.h>
#include <kpushbutton.h>
#include <kstandarddirs.h>

#include <tqimage.h>
#include <tqlabel.h>
#include <tqpainter.h>
#include <tqpoint.h>
#include <tqregexp.h>
#include <tqtimer.h>
#include <tqtooltip.h>
#include <tqvbox.h>
#include <tqwidget.h>

//project headers
#include "prefs.h"
#include "khangman.h"
#include "khangmanview.h"


KHangManView::KHangManView(KHangMan*parent, const char *name)
    : TQWidget(parent, name, WStaticContents | WNoAutoErase)
{
    khangman = parent;
    
    // The widget for entering letters.
    m_letterInput = new KLineEdit( this, "charWrite" );
    m_letterInput->setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType) 1, 
					       (TQSizePolicy::SizeType) 0, 
					       0, 0, 
					       m_letterInput->sizePolicy().hasHeightForWidth() ) );
    m_letterInput->setMaxLength( 1 );
    m_letterInput->setAlignment( int( TQLineEdit::AlignHCenter ) );

    // Press this button to enter a letter (or press enter)
    m_guessButton = new KPushButton( this, "guessButton" );
    m_guessButton->setSizePolicy( TQSizePolicy( (TQSizePolicy::SizeType) 1, 
					       (TQSizePolicy::SizeType) 0,
					       0, 0, 
					       m_guessButton->sizePolicy().hasHeightForWidth() ) );
    m_guessButton->setAutoMask( true );
    m_guessButton->setFlat( true );
    m_guessButton->setText( i18n( "G&uess" ) );
    
    // Get background from config file - default is sea
    loadAnimation();
    
    setMinimumSize( TQSize( 660, 370 ) );
    setBackground( m_originalBackground );

    // Some misc initializations.
    m_numMissedLetters = 0;
    m_lastWordNumber   = -1;
    m_accentedLetters  = true;
    m_hintExists       = true;	// Assume hint exists

    connect( m_letterInput, TQT_SIGNAL( returnPressed() ),
	     this,          TQT_SLOT( slotTry() ) );
    connect( m_guessButton, TQT_SIGNAL( clicked() ),
	     this,          TQT_SLOT( slotTry() ));
}


KHangManView::~KHangManView()
{
}


// Handle a guess of the letter in sChar.
//

void KHangManView::replaceLetters(const TQString& sChar)
{
    int   index = 0;
    bool  b_end = false;

    kdDebug() << "word " << m_word << endl;

    // Replace letter in the word
    if (Prefs::oneLetter()) {
	// We just replace the next instance.
        for (int count=0; count < m_word.contains(sChar, false); count++) {

            index = m_word.find(sChar, index, false);
            if (m_goodWord.at(2*index)=='_') {
		m_goodWord.replace((2*index), 1, m_word.at(index));

                kdDebug() << "m_goodword " << m_goodWord << endl;
                if (count == m_word.contains(sChar, false)-1)
		    b_end = true;
                break;
            }
	    else
		index++;

	    if (count == m_word.contains(sChar, false)-1)
		b_end = true;
        }
    }
    else {
        for (int count=0; count < m_word.contains(sChar, false); count++) {
            //searching for letter location
            index = m_word.find(sChar, index, false);
            //we replace it...
            m_goodWord.replace((2*index), 1,m_word.at(index));
            index++;
	}
    }

    if (m_accentedLetters && !Prefs::accentedLetters()) {
        if (sChar=="i") replaceLetters(TQString::fromUtf8("í"));
        if (sChar=="a") replaceLetters(TQString::fromUtf8("à"));
        if (sChar=="a") replaceLetters(TQString::fromUtf8("á"));
        if (sChar=="a") replaceLetters(TQString::fromUtf8("ã"));
        if (sChar=="u") replaceLetters(TQString::fromUtf8("ü"));
        if (sChar=="o") replaceLetters(TQString::fromUtf8("ò"));
        if (sChar=="o") replaceLetters(TQString::fromUtf8("ó"));
        if (sChar=="o") replaceLetters(TQString::fromUtf8("õ"));
        if (sChar=="e") replaceLetters(TQString::fromUtf8("è"));
        if (sChar=="e") replaceLetters(TQString::fromUtf8("é"));
        if (sChar=="u") replaceLetters(TQString::fromUtf8("ù"));
    }
    if (!Prefs::oneLetter()) 
        m_guessedLetters << sChar; //appends the list only if not in One Letter only mode...

    if (m_word.contains(sChar, false) == 1) 
        m_guessedLetters << sChar; //append if only one instance

    if (Prefs::oneLetter() && b_end) 
        m_guessedLetters << sChar; 
}


bool KHangManView::containsChar(const TQString &sChar)
{
    bool b = false;
    if (m_accentedLetters && !Prefs::accentedLetters()) {
        if (sChar=="i")
	    b = m_word.contains(TQString::fromUtf8("í"), false);

        if (sChar=="a")
	    b = m_word.contains(TQString::fromUtf8("à"), false)
		|| m_word.contains(TQString::fromUtf8("á"), false)
		|| m_word.contains(TQString::fromUtf8("ã"), false);

        if (sChar=="u")
	    b = m_word.contains(TQString::fromUtf8("ü"), false)
		|| m_word.contains(TQString::fromUtf8("ù"), false);

        if (sChar=="o")
	    b = m_word.contains(TQString::fromUtf8("ò"), false)
		|| m_word.contains(TQString::fromUtf8("ó"), false)
		|| m_word.contains(TQString::fromUtf8("õ"), false);

        if (sChar=="e")
	    b = m_word.contains(TQString::fromUtf8("è"), false)
		|| m_word.contains(TQString::fromUtf8("é"), false);
    }

    return (b || m_word.contains(sChar, false));
}


// ----------------------------------------------------------------
//                           Event handlers


void KHangManView::mousePressEvent(TQMouseEvent *mouse)
{
    if (mouse->button() == Qt::RightButton && m_hintExists && Prefs::hint()) {

        KPassivePopup *myPopup = new KPassivePopup( m_letterInput);
        myPopup->setView(i18n("Hint"), m_hint );
        myPopup->setPalette(TQToolTip::palette());
        myPopup->setTimeout(Prefs::hintTimer()*1000); //show for 4 seconds as default
        int x=0, y=0;

        TQPoint abspos = mapToGlobal( TQPoint( 0, 0 ) );
        x = abspos.x() + width()*70/700;
        y = abspos.y() + height()*150/535;

	TQPoint  point = TQPoint(x, y);
        myPopup->show(mapToGlobal(point));

        // Maybe it's better to popup where the mouse clicks, in that
        // case kill the popup before new click
        //myPopup->move(mouse->pos());
    }

    //update();
}


// ----------------------------------------------------------------

// FIXME: Move this function somewhere logical


void KHangManView::setTheme()
{
    loadAnimation();
    setBackground(m_originalBackground); 
    update();
}


// ----------------------------------------------------------------
//                           Painting


void KHangManView::paintEvent( TQPaintEvent * )
{
    // This pixmap implements double buffering to remove all forms of
    // flicker in the repainting.
    TQPixmap   buf(width(), height());

    // Repaint the contents of the khangman view into the pixmap.
    TQPainter  p(&buf);
    p.drawPixmap(0, 0, m_resizedBackground);
    paintHangman(p);
    paintWord(p);
    paintMisses(p);

    // ...and finally, put the pixmap into the widget.
    bitBlt(this, 0, 0, &buf);
}


void KHangManView::paintHangman(TQPainter &p)
{
    // Draw the animated hanged K
    if (Prefs::mode() == 0)  // sea
        p.drawPixmap(TQRect(0, 0, 
			   width()*630/700, height()*285/535),
		     m_animationPics[m_numMissedLetters]);
    else
        p.drawPixmap(TQRect(width()*68/700, height()*170/535, 
			   width()*259/700, height()*228/535), 
		     m_animationPics[m_numMissedLetters]);
}


void KHangManView::paintWord(TQPainter &p)
{
    TQRect  myRect;
    if (Prefs::mode() == 0)   //sea
        myRect = TQRect(0, height()-height()*126/535,
		       width()*417/700, height()*126/535);
    else  
        myRect = TQRect(0, height()-height()*126/535, 
		       width()*327/700, height()*126/535);

    TQFont tFont;
    if (Prefs::mode() == 0)   //sea
        p.setPen( TQColor(148, 156, 167));
    else  
        p.setPen( TQColor(87, 0, 0));

    if (Prefs::selectedLanguage() =="tg")
        tFont.setFamily( "URW Bookman" );
    else
        tFont.setFamily( "Sans Serif" );

    // FIXME: This has to be scaled depending of the dpi
    tFont.setPixelSize( 28 ); 

    p.setFont(tFont);
    p.drawText(myRect, AlignCenter|AlignCenter, m_goodWord);
}


void KHangManView::paintMisses(TQPainter &p)
{
    // Get the color for the letters.
    TQColor  letterColor;
    if (Prefs::mode() == 0)   //sea
	letterColor = TQColor(148, 156, 167);
    else
	letterColor = TQColor(87, 0, 0);

    // Draw the missed letters
    TQFont tFont;
    if (Prefs::selectedLanguage() =="tg")
        tFont.setFamily( "URW Bookman" );
    else
        tFont.setFamily( "Arial" );
    tFont.setPixelSize( 28 );
    p.setPen( letterColor );
    p.setFont(tFont);

    TQFontMetrics  fm(tFont);
    TQRect         fmRect(fm.boundingRect(m_missedLetters));
    TQRect         myRect = TQRect(width() - fmRect.width(), 15, 
				 fmRect.width(), fm.height());
    p.drawText(myRect, AlignLeft, m_missedLetters);

    // Draw the "Misses" word
    TQString  misses = i18n("Misses");
    TQFont  mFont = TQFont(Prefs::missesFontFamily());
    mFont.setPointSize(30);
    p.setFont(mFont);

    TQFontMetrics  fm2(mFont);
    TQRect         fmRect2(fm2.boundingRect(misses));
    TQRect         myRect2(width()- fmRect.width() - fmRect2.width() - 15,
			  15 - fm2.height() + fm.height(),
			  fmRect2.width(), fm2.height());
    p.setPen( letterColor );
    p.drawText(myRect2, AlignLeft|AlignCenter, misses);
}


void KHangManView::resizeEvent(TQResizeEvent *)
{
    if(!m_originalBackground.isNull())
        setBackground(m_originalBackground);
    
    m_letterInput->setMinimumSize( TQSize( height()/18, 0 ) );

    TQFont charWrite_font( m_letterInput->font() );
    charWrite_font.setPointSize( height()/18 );
    charWrite_font.setFamily( "Sans Serif" );

    m_letterInput->setFont( charWrite_font ); 
    m_letterInput->setGeometry(width()-2*height()/12, height()-2*height()/16, 
			       height()/10, height()/10);
    m_guessButton->setFont(TQFont(Prefs::guessFontFamily(), height()/22));
    m_guessButton->setGeometry(width() - 2*height()/12 
			       - m_guessButton->width()-5, 
			       height() - 2*height()/16, 
			       m_guessButton->width(), height()/10);
}


// ----------------------------------------------------------------
//                             Slots


void KHangManView::setBackground(TQPixmap& bgPix)
{
    TQImage img = bgPix.convertToImage();
    m_resizedBackground.resize(size());
    m_resizedBackground.convertFromImage(img.smoothScale( width(), height()));
}


void KHangManView::slotTry()
{
    TQString guess = m_letterInput->text();
    kdDebug() << "guess as entered: " << guess << endl;

    // If the char is not a letter, empty the input and return.
    if (!guess.at(0).isLetter()) {
	m_letterInput->setText("");
	return;
    }

    // Handle the guess.
    if (!m_guessedLetters.contains(guess.lower())) {
	// The letter is not already guessed.
	
	if (containsChar(guess)) {
	    replaceLetters(guess);

	    // This is needed because of the white spaces.
	    TQString  stripWord = m_goodWord;
	    TQString  sword     = m_word;
	    if (dd > 0)  {
		stripWord.replace(2*c,   1, "");
		stripWord.replace(2*c-1, 1, "");

		stripWord.replace(2*(dd-1),   1, "");
		stripWord.replace(2*(dd-1)-1, 1, "");
	    }

	    TQStringList  rightChars = TQStringList::split(" ", stripWord, true);
	    TQString      rightWord  = rightChars.join("");
	    update();
	    sword.remove(TQRegExp(" "));

	    // If the user made it...
	    if (rightWord.stripWhiteSpace().lower() 
		== sword.stripWhiteSpace().lower()) { 

		// We reset everything...
		// pixImage->setPixmap(m_animationPics[10]);
		//TODO find a better way to finish
		//
		if (Prefs::sound()) {
		    TQString soundFile = locate("data", "khangman/sounds/EW_Dialogue_Appear.ogg");
		    if (soundFile != 0) 
			KAudioPlayer::play(soundFile);
		}

		if (Prefs::wonDialog()) {
		    // TODO: hide Hint KPassivePopup if any
		    TQPoint point;
		    KPassivePopup *popup = new KPassivePopup( this, "popup" );
		    popup->setAutoDelete( true );
		    popup->setTimeout( 4*1000 );
		    popup->setView(i18n("Congratulations,\nyou won!") );

		    int x =0, y = 0;
		    TQPoint abspos = popup->pos();
		    x = abspos.x() + width()*50/700;
		    y = abspos.y() + height()*20/535;
		    point = TQPoint(x, y);
		    popup->show(mapToGlobal(point));
		    TQTimer::singleShot( 4*1000, this, TQT_SLOT(slotNewGame()) );
		}
		else if (KMessageBox::questionYesNo(this, i18n("Congratulations! You won! Do you want to play again?"),TQString(),i18n("Play Again"), i18n("Do Not Play")) == 3)
		    slotNewGame();
		else
		    kapp->quit();
	    }
	}
	else {
	    // The char is missed.

	    m_guessedLetters << guess.lower();	
            m_guessedLetters << guess.upper();
	    m_missedLetters = m_missedLetters.replace((2 * m_numMissedLetters), 
						      1, guess.lower());
	    m_numMissedLetters++;
	    update();

	    // Check if we have reached the limit of wrong guesses.
	    if (m_numMissedLetters >= MAXWRONGGUESSES) {

		//TODO sequence to finish when hanged
		TQStringList charList=TQStringList::split("", m_word);
		TQString theWord=charList.join(" ");
		m_goodWord = theWord;
		//usability: find another way to start a new game
		TQString newGameString = i18n("You lost. Do you want to play again?");
		if (Prefs::wonDialog()) {
		    // TODO: hide Hint KPassivePopup if any
		    TQPoint point;
		    KPassivePopup *popup = new KPassivePopup(KPassivePopup::Boxed,  this, "popup" );
		    popup->setAutoDelete( true );
		    popup->setTimeout( 4*1000 );

		    TQVBox *vb = new TQVBox( popup );

		    TQLabel *popLabel = new TQLabel( vb);
		    popLabel->setFont(TQFont("Sans Serif", 14, TQFont::Normal));
		    popLabel->setText(i18n("<qt>You lost!\nThe word was\n<b>%1</b></qt>").arg(m_word));
		    popup->setView( vb );

		    TQPoint abspos = popup->pos();
		    int  x = abspos.x() + width()  * 50 / 700;
		    int  y = abspos.y() + height() * 20 / 535;
		    popup->show(mapToGlobal(TQPoint(x, y)));

		    TQTimer::singleShot( 4 * 1000, this, TQT_SLOT(slotNewGame()) );
		}
		else if (KMessageBox::questionYesNo(this, newGameString, TQString(), i18n("Play Again"), i18n("Do Not Play")) == 3)
		    slotNewGame();
		else
		    kapp->quit();
	    }
	}
    }
    else {
	// The letter is already guessed.

	// Show a popup that says as much.
	TQPoint point;
	KPassivePopup *popup = new KPassivePopup(KPassivePopup::Balloon, this, "popup" );
	popup->setAutoDelete( true );
	popup->setTimeout( 1000 );
	popup->setView(i18n("This letter has already been guessed.") );

	int  x = 0;
	int  y = 0;
	if (m_missedLetters.contains(guess, false) > 0) {
	    // FIXME: popup should be better placed.

	    TQPoint abspos = popup->pos();
	    x = abspos.x() + width()*400/700;
	    y = abspos.y() + height()*50/535;
	    point = TQPoint(x, y);

	    // Create a 1 second single-shot timer, and reenable user
	    // input after this time.
	    TQTimer::singleShot( Prefs::missedTimer() * 1000, 
				this, TQT_SLOT(enableUserInput()) );

	    // Disable any possible entry
	    m_letterInput->setEnabled(false);
	}

	if (m_goodWord.contains(guess, false) > 0) {
	    TQPoint abspos = popup->pos();

	    if (Prefs::mode() == 0) {
		// sea
		x = abspos.x() + width()*250/700;
		y = abspos.y() + height()*485/535;
	    }
	    else {
		x = abspos.x() + width()*200/700;
		y = abspos.y() + height()*485/535;
	    }
	    point = TQPoint(x, y);

	    TQTimer::singleShot( Prefs::missedTimer() * 1000,
				this, TQT_SLOT(enableUserInput()) );

	    // Disable any possible entry
	    m_letterInput->setEnabled(false);	
	}

	popup->show(mapToGlobal(point));
    }

    // Reset the entry field after guess.
    m_letterInput->setText("");
}


void KHangManView::enableUserInput()
{
    m_letterInput->setEnabled(true);
    m_letterInput->setFocus();
}


void KHangManView::slotNewGame()
{
    if (Prefs::sound()) {
	TQString soundFile = locate("data", "khangman/sounds/new_game.ogg");
	if (soundFile != 0) 
	    KAudioPlayer::play(soundFile);
    }

    reset();
    game();
    m_letterInput->setFocus();
}


void KHangManView::reset()
{
    m_goodWord = "";
    m_word     = "";

    m_guessedLetters.clear();
    m_numMissedLetters = 0;
    m_missedLetters    = "_ _ _ _ _ _ _ _ _ _  ";

    // Clear the input field.
    m_letterInput->setText("");

    update();
}


void KHangManView::game()
{
    kdDebug() << "language " << Prefs::selectedLanguage() << endl;
    kdDebug() << "level "    << Prefs::levelFile()        << endl;

    // Check if the data files are installed in the correct dir.
    TQString  myString = TQString("khangman/data/%1/%2")
                          .arg(Prefs::selectedLanguage())
                          .arg(Prefs::levelFile());
    TQFile    myFile;
    myFile.setName(locate("data", myString));
    if (!myFile.exists()) {
	TQString  mString = i18n("File $TDEDIR/share/apps/khangman/data/%1/%2 not found!\n"
				"Check your installation, please!")
	             .arg(Prefs::selectedLanguage())
	             .arg(Prefs::levelFile());
	KMessageBox::sorry( this, mString, i18n("Error") );
	kapp->quit();
    }

    // We open the file and store info into the stream...
    TQFile  openFileStream(myFile.name());
    openFileStream.open(IO_ReadOnly);
    TQTextStream readFileStr(&openFileStream);
    readFileStr.setEncoding(TQTextStream::UnicodeUTF8);

    // Alldata contains all the words from the file
    TQStringList allData = TQStringList::split("\n", readFileStr.read(), true);
    openFileStream.close();

    // Detects if file is a kvtml file so that it's a hint enable file
    if (allData.first() == "<?xml version=\"1.0\"?>") {
        readFile();
    }
    else {
	//TODO abort if not a kvtml file maybe
        kdDebug() << "Not a kvtml file!" << endl;
    }
    kdDebug() << m_word << endl;

    // Display the number of letters to guess with _
    for (unsigned int i = 0; i < m_word.length(); i++)
        m_goodWord.append("_ ");

    // Remove the last trailing space.
    m_goodWord.remove(m_goodWord.length()-1);
    
    kdDebug() << m_goodWord << endl;

    // If needed, display white space or - if in word or semi dot.

    // 1. Find dashes.
    int f = m_word.find( "-" );
    if (f>0) {
        m_goodWord.replace(2*f, 1, "-");

        int g = m_word.find( "-", f+1);
        if (g>0) 
	    m_goodWord.replace(2*g, 3, "-");
        if (g>1)
	    m_goodWord.append("_");
    }

    // 2. Find white space.
    c = m_word.find( " " );
    if (c > 0) {
        m_goodWord.replace(2*c, 1, " ");
        dd = m_word.find( " ", c+1);
        if (dd > 0)
	    m_goodWord.replace(2*dd, c+1, " ");
    }

    // 3. Find ·
    int e = m_word.find( TQString::fromUtf8("·") );
    if (e>0)
	m_goodWord.replace(2*e, 1, TQString::fromUtf8("·") );

    // 4. Find '
    int h = m_word.find( "'" );
    if (h>0)
	m_goodWord.replace(2*h, 1, "'");
}


void KHangManView::readFile()
{
    kdDebug() << "in read kvtml file " << endl;
    TQString myString=TQString("khangman/data/%1/%2").arg(Prefs::selectedLanguage()).arg(Prefs::levelFile());
    myString= locate("data", myString);
    KEduVocDataItemList verbs = KEduVocData::parse(myString);

    //how many words in the file
    int NumberOfWords = verbs.count();

    //pick a number in random

    // Make sure the same word is not chosen twice in a row.
    int  wordNumber = m_random.getLong(NumberOfWords);
    if (m_lastWordNumber != -1) {
        while (wordNumber == m_lastWordNumber)
	    wordNumber = m_random.getLong(NumberOfWords);
    }
    m_lastWordNumber = wordNumber;

    m_word = verbs[wordNumber].originalText();
    m_hint = verbs[wordNumber].translatedText(); 

    if (m_word.isEmpty()) 
        readFile();

    if (m_hint.isEmpty()) {
        m_hintExists = false;	// Hint does not exist.

	// FIXME: Make this a signal instead.
        khangman->changeStatusbar("", 103);
    }
    else {
        m_hintExists = true;
        khangman->setMessages();
    }
}


void KHangManView::loadAnimation()
{
    switch (Prefs::mode())  {
        case Prefs::EnumMode::sea:
            m_originalBackground = TQPixmap(locate("data", "khangman/pics/sea/sea_theme.png") );
            m_themeName = "sea";
            m_letterInput->setPaletteForegroundColor( TQColor(  83,  40,  14) );
            m_guessButton->setPaletteBackgroundColor( TQColor( 115,  64,  49) );
            m_guessButton->setPaletteForegroundColor( TQColor( 148, 156, 167) );
            m_guessButton->setBackgroundOrigin( KPushButton::ParentOrigin );
            break;

        case Prefs::EnumMode::desert:
            m_originalBackground = TQPixmap(locate("data","khangman/pics/desert/desert_theme.png") );
            m_themeName = "desert";
            m_letterInput->setPaletteForegroundColor( TQColor(  87,   0,  0) );
            m_guessButton->setPaletteBackgroundColor( TQColor( 205, 214, 90) );
            m_guessButton->setPaletteForegroundColor( TQColor(  87,   0,  0) );
            m_guessButton->setBackgroundOrigin( KPushButton::ParentOrigin );
            break;
    }	

    // Now we load the pixmaps...
    for (uint i = 0; i < 11; i++) {
	m_animationPics[i].load(locate( "data", 
		 	   TQString("khangman/pics/%1/animation%2.png")
			   .arg(m_themeName).arg(i) ));
    }
}

#include "khangmanview.moc"