summaryrefslogtreecommitdiff
path: root/inputmethods/dasher/DasherInterface.cpp
blob: 4699687e3e176f7951d73a39a75104864da11217 (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
// DasherInterface.cpp
//
/////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2002 Iain Murray
//
/////////////////////////////////////////////////////////////////////////////



#include "DasherInterface.h"
//#include "EnglishAlphabet.h"
#include "CustomAlphabet.h"
#include "DasherViewSquare.h"
#include "PPMLanguageModel.h"
#include <iostream>
namespace {
	#include "stdio.h"
}
using namespace Dasher;
using namespace std;


const string CDasherInterface::EmptyString = "";


CDasherInterface::CDasherInterface()
	: m_Alphabet(0),
	  m_LanguageModel(0), 
	  m_DasherModel(0), 
	  m_DashEditbox(0), 
	  m_DasherScreen(0), 
	  m_DasherView(0), 
	  m_SettingsStore(0), 
	  m_SettingsUI(0),
	  m_AlphIO(0), 
	  TrainContext(0), 
	  AlphabetID(""), 
	  LanguageModelID(-1), 
	  ViewID(-1),
	  m_MaxBitRate(-1), 
	  m_DrawKeyboard(false),
	  m_Orientation(Opts::LeftToRight), 
	  m_UserLocation("usr_"), 
	  m_SystemLocation("sys_"), 
	  m_TrainFile(""),
	  m_DasherFont(""), 
	  m_EditFont(""), 
	  m_EditFontSize(0)
{
}


CDasherInterface::~CDasherInterface()
{
	if (m_LanguageModel)
		m_LanguageModel->ReleaseNodeContext(TrainContext);
	delete m_DasherModel;   // The order of some of these deletions matters
	delete m_LanguageModel; // eg DasherModel has a pointer to LanguageModel.
	delete m_Alphabet;      // DM baulks if LM is deleted before it is.
	delete m_DasherView;
	// Do NOT delete Edit box or Screen. This class did not create them.
}


void CDasherInterface::SetSettingsStore(CSettingsStore* SettingsStore)
{
	delete m_SettingsStore;
	m_SettingsStore = SettingsStore;
	this->SettingsDefaults(m_SettingsStore);
}


void CDasherInterface::SetSettingsUI(CDasherSettingsInterface* SettingsUI)
{
	delete m_SettingsUI;
	m_SettingsUI = SettingsUI;
	//this->SettingsDefaults(m_SettingsStore);
	m_SettingsUI->SettingsDefaults(m_SettingsStore);
}


void CDasherInterface::SetUserLocation(std::string UserLocation)
{
	// Nothing clever updates. (At the moment) it is assumed that
	// this is set before anything much happens and that it does
	// not require changing.
	m_UserLocation = UserLocation;
	if (m_Alphabet!=0)
		m_TrainFile = m_UserLocation + m_Alphabet->GetTrainingFile();
}


void CDasherInterface::SetSystemLocation(std::string SystemLocation)
{
	// Nothing clever updates. (At the moment) it is assumed that
	// this is set before anything much happens and that it does
	// not require changing.
	m_SystemLocation = SystemLocation;
}


void CDasherInterface::CreateDasherModel()
{

  if (m_DashEditbox!=0 && m_LanguageModel!=0) {
    delete m_DasherModel;
    m_DasherModel = new CDasherModel(m_DashEditbox, m_LanguageModel, m_Dimensions);
    if (m_MaxBitRate>=0)
      m_DasherModel->SetMaxBitrate(m_MaxBitRate);
    if (ViewID!=-1)
      ChangeView(ViewID);
  }


}


void CDasherInterface::Start()
{
	if (m_DasherModel!=0)
		m_DasherModel->Start();
}


void CDasherInterface::PauseAt(int MouseX, int MouseY)
{
	if (m_DasherView!=0)
		m_DasherView->FlushAt(MouseX, MouseY);
	if (m_DashEditbox!=0) {
		m_DashEditbox->write_to_file();
		if (m_CopyAllOnStop)
			m_DashEditbox->CopyAll();
	}
}


void CDasherInterface::Unpause(unsigned long Time)
{
	if (m_DashEditbox!=0)
		m_DashEditbox->unflush();
	if (m_DasherModel!=0)
		m_DasherModel->Reset_framerate(Time);
}


void CDasherInterface::Redraw()
{

  if (m_DasherView!=0) {
    m_DasherView->Render();
    m_DasherView->Display();
  }

}


void CDasherInterface::TapOn(int MouseX, int MouseY, unsigned long Time)
{
	if (m_DasherView!=0) {
		m_DasherView->TapOnDisplay(MouseX, MouseY, Time);
		m_DasherView->Render();
		if (m_DrawMouse==true) {
		  m_DasherView->DrawMouse(MouseX, MouseY);
		}
		if (m_DrawKeyboard==true) {
		  m_DasherView->DrawKeyboard();
		}
		m_DasherView->Display();
	}
	if (m_DasherModel!=0)
		m_DasherModel->NewFrame(Time);
}


void CDasherInterface::ChangeAlphabet(const std::string& NewAlphabetID)
{
	if (m_SettingsUI!=0)
		m_SettingsUI->ChangeAlphabet(NewAlphabetID);
	if (m_SettingsStore!=0)
		m_SettingsStore->SetStringOption(Keys::ALPHABET_ID, NewAlphabetID);
	
	AlphabetID = NewAlphabetID;
	if (!m_AlphIO)
		m_AlphIO = new CAlphIO(m_SystemLocation, m_UserLocation);
	CAlphIO::AlphInfo Info = m_AlphIO->GetInfo(NewAlphabetID);

	CAlphabet* old = m_Alphabet;
	m_Alphabet = new CCustomAlphabet(Info);
	
	// Apply options from alphabet

	m_TrainFile = m_UserLocation + m_Alphabet->GetTrainingFile();
	
	// Recreate widgets and language model
	if (m_DashEditbox!=0)
		m_DashEditbox->SetInterface(this);
	if (m_DasherScreen!=0)
		m_DasherScreen->SetInterface(this);
	if (LanguageModelID!=-1 || m_LanguageModel)
		ChangeLanguageModel(LanguageModelID);

	delete old; // only delete old alphabet after telling all other objects not to use it

	Start();

	// We can only change the orientation after we have called
	// Start, as this will prompt a redraw, which will fail if the
	// model hasn't been updated for the new alphabet

	if (m_Orientation==Opts::Alphabet)
 		ChangeOrientation(Opts::Alphabet);

	Redraw();
}


void CDasherInterface::ChangeMaxBitRate(double NewMaxBitRate)
{
	m_MaxBitRate = NewMaxBitRate;
	
	if (m_DasherModel!=0)
		m_DasherModel->SetMaxBitrate(m_MaxBitRate);
	if (m_SettingsUI!=0)
		m_SettingsUI->ChangeMaxBitRate(m_MaxBitRate);
	if (m_SettingsStore!=0)
		m_SettingsStore->SetLongOption(Keys::MAX_BITRATE_TIMES100, long(m_MaxBitRate*100) );

	if (m_DrawKeyboard==true && m_DasherView!=NULL) {
	  m_DasherView->DrawKeyboard();
	}
}


void CDasherInterface::ChangeLanguageModel(unsigned int NewLanguageModelID)
{
	LanguageModelID = NewLanguageModelID;
	if (m_Alphabet!=0) {
		if (m_LanguageModel)
			m_LanguageModel->ReleaseNodeContext(TrainContext);
		TrainContext = 0;
		delete m_DasherModel; // Have to delete DasherModel, or removing its LanguageModel will confuse it
		m_DasherModel = 0;
		delete m_LanguageModel;
		// TODO Use LanguageModelID to decide which model to use
		m_LanguageModel = new CPPMLanguageModel(m_Alphabet,1<<10);
		TrainContext = m_LanguageModel->GetRootNodeContext();
		string T = m_Alphabet->GetTrainingFile();
		TrainFile(m_SystemLocation+T);
		TrainFile(m_UserLocation+T);
		CreateDasherModel();
	}
}


void CDasherInterface::ChangeScreen()
{
	if (m_DasherView!=0) {
		m_DasherView->ChangeScreen(m_DasherScreen);
	} else {
		if (ViewID!=-1)
			ChangeView(ViewID);
	}
}


void CDasherInterface::ChangeScreen(CDasherScreen* NewScreen)
{
	m_DasherScreen = NewScreen;
	m_DasherScreen->SetFont(m_DasherFont);
	m_DasherScreen->SetInterface(this);
	ChangeScreen();
	Redraw();
}


void CDasherInterface::ChangeView(unsigned int NewViewID)
{
	//TODO Use DasherViewID
	ViewID = NewViewID;
	if (m_DasherScreen!=0 && m_DasherModel!=0) {
		delete m_DasherView;
		if (m_Orientation==Opts::Alphabet)
			m_DasherView = new CDasherViewSquare(m_DasherScreen, *m_DasherModel, GetAlphabetOrientation());
		else
			m_DasherView = new CDasherViewSquare(m_DasherScreen, *m_DasherModel, m_Orientation);
	}
}


void CDasherInterface::ChangeOrientation(Opts::ScreenOrientations Orientation)
{
	m_Orientation = Orientation;
	if (m_DasherView!=0) {
		if (Orientation==Opts::Alphabet)
			m_DasherView->ChangeOrientation(GetAlphabetOrientation());
		else
			m_DasherView->ChangeOrientation(Orientation);
	}
	if (m_SettingsUI!=0)
		m_SettingsUI->ChangeOrientation(Orientation);
	if (m_SettingsStore!=0)
		m_SettingsStore->SetLongOption(Keys::SCREEN_ORIENTATION, Orientation);
}


void CDasherInterface::SetFileEncoding(Opts::FileEncodingFormats Encoding)
{
	if (m_SettingsUI!=0)
		m_SettingsUI->SetFileEncoding(Encoding);
	if (m_SettingsStore!=0)
		m_SettingsStore->SetLongOption(Keys::FILE_ENCODING, Encoding);
	if (m_DashEditbox)
			m_DashEditbox->SetEncoding(Encoding);
}


void CDasherInterface::ShowToolbar(bool Value)
{
	if (m_SettingsUI!=0)
		m_SettingsUI->ShowToolbar(Value);
	if (m_SettingsStore!=0)
		m_SettingsStore->SetBoolOption(Keys::SHOW_TOOLBAR, Value);
}


void CDasherInterface::ShowToolbarText(bool Value)
{
	if (m_SettingsUI!=0)
		m_SettingsUI->ShowToolbarText(Value);
	if (m_SettingsStore!=0)
		m_SettingsStore->SetBoolOption(Keys::SHOW_TOOLBAR_TEXT, Value);
}


void CDasherInterface::ShowToolbarLargeIcons(bool Value)
{
	if (m_SettingsUI!=0)
		m_SettingsUI->ShowToolbarLargeIcons(Value);
	if (m_SettingsStore!=0)
		m_SettingsStore->SetBoolOption(Keys::SHOW_LARGE_ICONS, Value);
}


void CDasherInterface::ShowSpeedSlider(bool Value)
{
	if (m_SettingsUI!=0)
		m_SettingsUI->ShowSpeedSlider(Value);
	if (m_SettingsStore!=0)
		m_SettingsStore->SetBoolOption(Keys::SHOW_SLIDER, Value);
}


void CDasherInterface::FixLayout(bool Value)
{
	if (m_SettingsUI!=0)
		m_SettingsUI->FixLayout(Value);
	if (m_SettingsStore!=0)
		m_SettingsStore->SetBoolOption(Keys::FIX_LAYOUT, Value);
}


void CDasherInterface::TimeStampNewFiles(bool Value)
{
	if (m_SettingsUI!=0)
		m_SettingsUI->TimeStampNewFiles(Value);
	if (m_SettingsStore!=0)
		m_SettingsStore->SetBoolOption(Keys::TIME_STAMP, Value);
	if (m_DashEditbox!=0)
		m_DashEditbox->TimeStampNewFiles(Value);
}


void CDasherInterface::CopyAllOnStop(bool Value)
{
	m_CopyAllOnStop = Value;
	if (m_SettingsUI!=0)
		m_SettingsUI->CopyAllOnStop(Value);
	if (m_SettingsStore!=0)
		m_SettingsStore->SetBoolOption(Keys::COPY_ALL_ON_STOP, Value);
}

void CDasherInterface::DrawMouse(bool Value)
{
        m_DrawMouse = Value;
	if (m_SettingsUI!=0)
                m_SettingsUI->DrawMouse(Value);
	if (m_SettingsStore!=0)
	        m_SettingsStore->SetBoolOption(Keys::DRAW_MOUSE, Value);
}

void CDasherInterface::StartOnSpace(bool Value)
{
        m_StartSpace = Value;
	if (m_SettingsUI!=0)
	  m_SettingsUI->StartOnSpace(Value);
	if (m_SettingsStore!=0)
	  m_SettingsStore->SetBoolOption(Keys::START_SPACE, Value);
}

void CDasherInterface::StartOnLeft(bool Value)
{
  m_StartLeft = Value;
	if (m_SettingsUI!=0)
	  m_SettingsUI->StartOnLeft(Value);
	if (m_SettingsStore!=0)
	  m_SettingsStore->SetBoolOption(Keys::START_MOUSE, Value);
}

void CDasherInterface::KeyControl(bool Value)
{
        m_KeyControl = Value;
	if (m_SettingsUI!=0)
	  m_SettingsUI->KeyControl(Value);
	if (m_SettingsStore!=0)
	  m_SettingsStore->SetBoolOption(Keys::KEY_CONTROL, Value);
}

void CDasherInterface::WindowPause(bool Value)
{
        m_KeyControl = Value;
	if (m_SettingsUI!=0)
	  m_SettingsUI->WindowPause(Value);
	if (m_SettingsStore!=0)
	  m_SettingsStore->SetBoolOption(Keys::WINDOW_PAUSE, Value);
}

void CDasherInterface::SetEditFont(string Name, long Size)
{
	m_EditFont = Name;
	m_EditFontSize = Size;
	if (m_DashEditbox)
		m_DashEditbox->SetFont(Name, Size);
	if (m_SettingsUI!=0)
		m_SettingsUI->SetEditFont(Name, Size);
	if (m_SettingsStore!=0) {
		m_SettingsStore->SetStringOption(Keys::EDIT_FONT, Name);
		m_SettingsStore->SetLongOption(Keys::EDIT_FONT_SIZE, Size);
	}
}


void CDasherInterface::SetDasherFont(string Name)
{
	if (m_SettingsStore!=0)
		m_SettingsStore->SetStringOption(Keys::DASHER_FONT, Name);
	m_DasherFont = Name;
	if (m_DasherScreen!=0)
		m_DasherScreen->SetFont(Name);
	Redraw();
}

void CDasherInterface::SetDasherFontSize(FontSize fontsize)
{
	if (m_SettingsStore!=0)
		m_SettingsStore->SetLongOption(Keys::DASHER_FONTSIZE, fontsize);
	if (m_DasherScreen!=0) {
	         m_DasherScreen->SetFontSize(fontsize);
	}
	Redraw();
}

void CDasherInterface::SetDasherDimensions(bool Value)
{
        m_Dimensions=Value;
	if (m_SettingsStore!=0)
		m_SettingsStore->SetBoolOption(Keys::DASHER_DIMENSIONS, Value);
	if (m_DasherModel!=0) {
	         m_DasherModel->Set_dimensions(Value);
	}
	if (m_SettingsUI!=0) {
	         m_SettingsUI->SetDasherDimensions(Value);
	}	  
}


unsigned int CDasherInterface::GetNumberSymbols()
{
	if (m_Alphabet!=0)
		return m_Alphabet->GetNumberSymbols();
	else
		return 0;
}


const string& CDasherInterface::GetDisplayText(symbol Symbol)
{
	if (m_Alphabet!=0)
		return m_Alphabet->GetDisplayText(Symbol);
	else
		return EmptyString;
}


const string& CDasherInterface::GetEditText(symbol Symbol)
{
	if (m_Alphabet!=0)
		return m_Alphabet->GetText(Symbol);
	else
		return EmptyString;
}

int CDasherInterface::GetTextColour(symbol Symbol)
{
	if (m_Alphabet!=0)
		return m_Alphabet->GetTextColour(Symbol);
	else
	        return 0;
}


Opts::ScreenOrientations CDasherInterface::GetAlphabetOrientation()
{
	return m_Alphabet->GetOrientation();
}


Opts::AlphabetTypes CDasherInterface::GetAlphabetType()
{
	return m_Alphabet->GetType();
}


const std::string& CDasherInterface::GetTrainFile()
{
	return m_TrainFile;
}


void CDasherInterface::GetAlphabets(std::vector< std::string >* AlphabetList)
{
	if (!m_AlphIO)
		m_AlphIO = new CAlphIO(m_SystemLocation, m_UserLocation);	
	m_AlphIO->GetAlphabets(AlphabetList);
}


const CAlphIO::AlphInfo& CDasherInterface::GetInfo(const std::string& AlphID)
{
	if (!m_AlphIO)
		m_AlphIO = new CAlphIO(m_SystemLocation, m_UserLocation);
	
	return m_AlphIO->GetInfo(AlphID);
}


void CDasherInterface::SetInfo(const CAlphIO::AlphInfo& NewInfo)
{
	if (!m_AlphIO)
		m_AlphIO = new CAlphIO(m_SystemLocation, m_UserLocation);
	
	m_AlphIO->SetInfo(NewInfo);
}


void CDasherInterface::DeleteAlphabet(const std::string& AlphID)
{
	if (!m_AlphIO)
		m_AlphIO = new CAlphIO(m_SystemLocation, m_UserLocation);
	
	m_AlphIO->Delete(AlphID);
}


void CDasherInterface::ChangeEdit()
{
	CreateDasherModel();
	if (m_DasherModel!=0)
		m_DasherModel->Start();
	if (m_DasherView!=0)
		m_DasherView->Render();
}


void CDasherInterface::ChangeEdit(CDashEditbox* NewEdit)
{
	m_DashEditbox = NewEdit;
	m_DashEditbox->SetFont(m_EditFont, m_EditFontSize);
	m_DashEditbox->SetInterface(this);
	if (m_SettingsStore!=0)
		m_DashEditbox->TimeStampNewFiles(m_SettingsStore->GetBoolOption(Keys::TIME_STAMP));
	m_DashEditbox->New("");
	ChangeEdit();
}


void CDasherInterface::Train(string* TrainString, bool IsMore)
{
	m_LanguageModel->LearnText(TrainContext, TrainString, IsMore);
	return;
}


/*
	I've used C style I/O because I found that C++ style I/O bloated
	the Win32 code enormously. The overhead of loading the buffer into
	the string instead of reading straight into a string seems to be
	negligible compared to huge requirements elsewhere.
*/
void CDasherInterface::TrainFile(string Filename)
{
	if (Filename=="")
		return;
	
	FILE* InputFile;
	if ( (InputFile = fopen(Filename.c_str(), "r")) == (FILE*)0)
		return;
	
	const int BufferSize = 1024;
	char InputBuffer[BufferSize];
	string StringBuffer;
	int NumberRead;
	
	do {
		NumberRead = fread(InputBuffer, 1, BufferSize-1, InputFile);
		InputBuffer[NumberRead] = '\0';
		StringBuffer += InputBuffer;
		if (NumberRead == (BufferSize-1)) {
		  m_LanguageModel->LearnText(TrainContext, &StringBuffer, true);
		} else {
		  m_LanguageModel->LearnText(TrainContext, &StringBuffer, false);
		}
	} while (NumberRead==BufferSize-1);

	fclose(InputFile);
}

void CDasherInterface::GetFontSizes(std::vector<int> *FontSizes)
{
  FontSizes->push_back(20);
  FontSizes->push_back(14);
  FontSizes->push_back(11);
  FontSizes->push_back(40);
  FontSizes->push_back(28);
  FontSizes->push_back(22);
  FontSizes->push_back(80);
  FontSizes->push_back(56);
  FontSizes->push_back(44);
}
  

double CDasherInterface::GetCurCPM()
{
	//
	return 0;
}


double CDasherInterface::GetCurFPS()
{
	//
	return 0;
}