summaryrefslogtreecommitdiff
path: root/inputmethods/dasher/DasherInterface.cpp
Unidiff
Diffstat (limited to 'inputmethods/dasher/DasherInterface.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--inputmethods/dasher/DasherInterface.cpp636
1 files changed, 636 insertions, 0 deletions
diff --git a/inputmethods/dasher/DasherInterface.cpp b/inputmethods/dasher/DasherInterface.cpp
new file mode 100644
index 0000000..bb5b85e
--- a/dev/null
+++ b/inputmethods/dasher/DasherInterface.cpp
@@ -0,0 +1,636 @@
1// DasherInterface.cpp
2//
3/////////////////////////////////////////////////////////////////////////////
4//
5// Copyright (c) 2002 Iain Murray
6//
7/////////////////////////////////////////////////////////////////////////////
8
9
10
11#include "DasherInterface.h"
12//#include "EnglishAlphabet.h"
13#include "CustomAlphabet.h"
14#include "DasherViewSquare.h"
15#include "PPMLanguageModel.h"
16#include <iostream>
17namespace {
18 #include "stdio.h"
19}
20using namespace Dasher;
21using namespace std;
22
23
24const string CDasherInterface::EmptyString = "";
25
26
27CDasherInterface::CDasherInterface()
28 : m_DashEditbox(0), m_DasherScreen(0), m_LanguageModel(0), TrainContext(0), m_Alphabet(0),
29 m_DasherModel(0), m_DasherView(0), AlphabetID(""), LanguageModelID(-1), ViewID(-1),
30 m_MaxBitRate(-1), m_Orientation(Opts::LeftToRight), m_SettingsStore(0), m_SettingsUI(0),
31 m_UserLocation("usr_"), m_SystemLocation("sys_"), m_AlphIO(0), m_TrainFile(""),
32 m_DasherFont(""), m_EditFont(""), m_EditFontSize(0), m_DrawKeyboard(false)
33{
34}
35
36
37CDasherInterface::~CDasherInterface()
38{
39 if (m_LanguageModel)
40 m_LanguageModel->ReleaseNodeContext(TrainContext);
41 delete m_DasherModel; // The order of some of these deletions matters
42 delete m_LanguageModel; // eg DasherModel has a pointer to LanguageModel.
43 delete m_Alphabet; // DM baulks if LM is deleted before it is.
44 delete m_DasherView;
45 // Do NOT delete Edit box or Screen. This class did not create them.
46}
47
48
49void CDasherInterface::SetSettingsStore(CSettingsStore* SettingsStore)
50{
51 delete m_SettingsStore;
52 m_SettingsStore = SettingsStore;
53 this->SettingsDefaults(m_SettingsStore);
54}
55
56
57void CDasherInterface::SetSettingsUI(CDasherSettingsInterface* SettingsUI)
58{
59 delete m_SettingsUI;
60 m_SettingsUI = SettingsUI;
61 //this->SettingsDefaults(m_SettingsStore);
62 m_SettingsUI->SettingsDefaults(m_SettingsStore);
63}
64
65
66void CDasherInterface::SetUserLocation(std::string UserLocation)
67{
68 // Nothing clever updates. (At the moment) it is assumed that
69 // this is set before anything much happens and that it does
70 // not require changing.
71 m_UserLocation = UserLocation;
72 if (m_Alphabet!=0)
73 m_TrainFile = m_UserLocation + m_Alphabet->GetTrainingFile();
74}
75
76
77void CDasherInterface::SetSystemLocation(std::string SystemLocation)
78{
79 // Nothing clever updates. (At the moment) it is assumed that
80 // this is set before anything much happens and that it does
81 // not require changing.
82 m_SystemLocation = SystemLocation;
83}
84
85
86void CDasherInterface::CreateDasherModel()
87{
88
89 if (m_DashEditbox!=0 && m_LanguageModel!=0) {
90 delete m_DasherModel;
91 m_DasherModel = new CDasherModel(m_DashEditbox, m_LanguageModel, m_Dimensions);
92 if (m_MaxBitRate>=0)
93 m_DasherModel->SetMaxBitrate(m_MaxBitRate);
94 if (ViewID!=-1)
95 ChangeView(ViewID);
96 }
97
98
99}
100
101
102void CDasherInterface::Start()
103{
104 if (m_DasherModel!=0)
105 m_DasherModel->Start();
106}
107
108
109void CDasherInterface::PauseAt(int MouseX, int MouseY)
110{
111 if (m_DasherView!=0)
112 m_DasherView->FlushAt(MouseX, MouseY);
113 if (m_DashEditbox!=0) {
114 m_DashEditbox->write_to_file();
115 if (m_CopyAllOnStop)
116 m_DashEditbox->CopyAll();
117 }
118}
119
120
121void CDasherInterface::Unpause(unsigned long Time)
122{
123 if (m_DashEditbox!=0)
124 m_DashEditbox->unflush();
125 if (m_DasherModel!=0)
126 m_DasherModel->Reset_framerate(Time);
127}
128
129
130void CDasherInterface::Redraw()
131{
132
133 if (m_DasherView!=0) {
134 m_DasherView->Render();
135 m_DasherView->Display();
136 }
137
138}
139
140
141void CDasherInterface::TapOn(int MouseX, int MouseY, unsigned long Time)
142{
143 if (m_DasherView!=0) {
144 m_DasherView->TapOnDisplay(MouseX, MouseY, Time);
145 m_DasherView->Render();
146 if (m_DrawMouse==true) {
147 m_DasherView->DrawMouse(MouseX, MouseY);
148 }
149 if (m_DrawKeyboard==true) {
150 m_DasherView->DrawKeyboard();
151 }
152 m_DasherView->Display();
153 }
154 if (m_DasherModel!=0)
155 m_DasherModel->NewFrame(Time);
156}
157
158
159void CDasherInterface::ChangeAlphabet(const std::string& NewAlphabetID)
160{
161 if (m_SettingsUI!=0)
162 m_SettingsUI->ChangeAlphabet(NewAlphabetID);
163 if (m_SettingsStore!=0)
164 m_SettingsStore->SetStringOption(Keys::ALPHABET_ID, NewAlphabetID);
165
166 AlphabetID = NewAlphabetID;
167 if (!m_AlphIO)
168 m_AlphIO = new CAlphIO(m_SystemLocation, m_UserLocation);
169 CAlphIO::AlphInfo Info = m_AlphIO->GetInfo(NewAlphabetID);
170
171 CAlphabet* old = m_Alphabet;
172 m_Alphabet = new CCustomAlphabet(Info);
173
174 // Apply options from alphabet
175
176 m_TrainFile = m_UserLocation + m_Alphabet->GetTrainingFile();
177
178 // Recreate widgets and language model
179 if (m_DashEditbox!=0)
180 m_DashEditbox->SetInterface(this);
181 if (m_DasherScreen!=0)
182 m_DasherScreen->SetInterface(this);
183 if (LanguageModelID!=-1 || m_LanguageModel)
184 ChangeLanguageModel(LanguageModelID);
185
186 delete old; // only delete old alphabet after telling all other objects not to use it
187
188 Start();
189
190 // We can only change the orientation after we have called
191 // Start, as this will prompt a redraw, which will fail if the
192 // model hasn't been updated for the new alphabet
193
194 if (m_Orientation==Opts::Alphabet)
195 ChangeOrientation(Opts::Alphabet);
196
197 Redraw();
198}
199
200
201void CDasherInterface::ChangeMaxBitRate(double NewMaxBitRate)
202{
203 m_MaxBitRate = NewMaxBitRate;
204
205 if (m_DasherModel!=0)
206 m_DasherModel->SetMaxBitrate(m_MaxBitRate);
207 if (m_SettingsUI!=0)
208 m_SettingsUI->ChangeMaxBitRate(m_MaxBitRate);
209 if (m_SettingsStore!=0)
210 m_SettingsStore->SetLongOption(Keys::MAX_BITRATE_TIMES100, long(m_MaxBitRate*100) );
211
212 if (m_DrawKeyboard==true && m_DasherView!=NULL) {
213 m_DasherView->DrawKeyboard();
214 }
215}
216
217
218void CDasherInterface::ChangeLanguageModel(unsigned int NewLanguageModelID)
219{
220 LanguageModelID = NewLanguageModelID;
221 if (m_Alphabet!=0) {
222 if (m_LanguageModel)
223 m_LanguageModel->ReleaseNodeContext(TrainContext);
224 TrainContext = 0;
225 delete m_DasherModel; // Have to delete DasherModel, or removing its LanguageModel will confuse it
226 m_DasherModel = 0;
227 delete m_LanguageModel;
228 // TODO Use LanguageModelID to decide which model to use
229 m_LanguageModel = new CPPMLanguageModel(m_Alphabet,1<<10);
230 TrainContext = m_LanguageModel->GetRootNodeContext();
231 string T = m_Alphabet->GetTrainingFile();
232 TrainFile(m_SystemLocation+T);
233 TrainFile(m_UserLocation+T);
234 CreateDasherModel();
235 }
236}
237
238
239void CDasherInterface::ChangeScreen()
240{
241 if (m_DasherView!=0) {
242 m_DasherView->ChangeScreen(m_DasherScreen);
243 } else {
244 if (ViewID!=-1)
245 ChangeView(ViewID);
246 }
247}
248
249
250void CDasherInterface::ChangeScreen(CDasherScreen* NewScreen)
251{
252 m_DasherScreen = NewScreen;
253 m_DasherScreen->SetFont(m_DasherFont);
254 m_DasherScreen->SetInterface(this);
255 ChangeScreen();
256 Redraw();
257}
258
259
260void CDasherInterface::ChangeView(unsigned int NewViewID)
261{
262 //TODO Use DasherViewID
263 ViewID = NewViewID;
264 if (m_DasherScreen!=0 && m_DasherModel!=0) {
265 delete m_DasherView;
266 if (m_Orientation==Opts::Alphabet)
267 m_DasherView = new CDasherViewSquare(m_DasherScreen, *m_DasherModel, GetAlphabetOrientation());
268 else
269 m_DasherView = new CDasherViewSquare(m_DasherScreen, *m_DasherModel, m_Orientation);
270 }
271}
272
273
274void CDasherInterface::ChangeOrientation(Opts::ScreenOrientations Orientation)
275{
276 m_Orientation = Orientation;
277 if (m_DasherView!=0) {
278 if (Orientation==Opts::Alphabet)
279 m_DasherView->ChangeOrientation(GetAlphabetOrientation());
280 else
281 m_DasherView->ChangeOrientation(Orientation);
282 }
283 if (m_SettingsUI!=0)
284 m_SettingsUI->ChangeOrientation(Orientation);
285 if (m_SettingsStore!=0)
286 m_SettingsStore->SetLongOption(Keys::SCREEN_ORIENTATION, Orientation);
287}
288
289
290void CDasherInterface::SetFileEncoding(Opts::FileEncodingFormats Encoding)
291{
292 if (m_SettingsUI!=0)
293 m_SettingsUI->SetFileEncoding(Encoding);
294 if (m_SettingsStore!=0)
295 m_SettingsStore->SetLongOption(Keys::FILE_ENCODING, Encoding);
296 if (m_DashEditbox)
297 m_DashEditbox->SetEncoding(Encoding);
298}
299
300
301void CDasherInterface::ShowToolbar(bool Value)
302{
303 if (m_SettingsUI!=0)
304 m_SettingsUI->ShowToolbar(Value);
305 if (m_SettingsStore!=0)
306 m_SettingsStore->SetBoolOption(Keys::SHOW_TOOLBAR, Value);
307}
308
309
310void CDasherInterface::ShowToolbarText(bool Value)
311{
312 if (m_SettingsUI!=0)
313 m_SettingsUI->ShowToolbarText(Value);
314 if (m_SettingsStore!=0)
315 m_SettingsStore->SetBoolOption(Keys::SHOW_TOOLBAR_TEXT, Value);
316}
317
318
319void CDasherInterface::ShowToolbarLargeIcons(bool Value)
320{
321 if (m_SettingsUI!=0)
322 m_SettingsUI->ShowToolbarLargeIcons(Value);
323 if (m_SettingsStore!=0)
324 m_SettingsStore->SetBoolOption(Keys::SHOW_LARGE_ICONS, Value);
325}
326
327
328void CDasherInterface::ShowSpeedSlider(bool Value)
329{
330 if (m_SettingsUI!=0)
331 m_SettingsUI->ShowSpeedSlider(Value);
332 if (m_SettingsStore!=0)
333 m_SettingsStore->SetBoolOption(Keys::SHOW_SLIDER, Value);
334}
335
336
337void CDasherInterface::FixLayout(bool Value)
338{
339 if (m_SettingsUI!=0)
340 m_SettingsUI->FixLayout(Value);
341 if (m_SettingsStore!=0)
342 m_SettingsStore->SetBoolOption(Keys::FIX_LAYOUT, Value);
343}
344
345
346void CDasherInterface::TimeStampNewFiles(bool Value)
347{
348 if (m_SettingsUI!=0)
349 m_SettingsUI->TimeStampNewFiles(Value);
350 if (m_SettingsStore!=0)
351 m_SettingsStore->SetBoolOption(Keys::TIME_STAMP, Value);
352 if (m_DashEditbox!=0)
353 m_DashEditbox->TimeStampNewFiles(Value);
354}
355
356
357void CDasherInterface::CopyAllOnStop(bool Value)
358{
359 m_CopyAllOnStop = Value;
360 if (m_SettingsUI!=0)
361 m_SettingsUI->CopyAllOnStop(Value);
362 if (m_SettingsStore!=0)
363 m_SettingsStore->SetBoolOption(Keys::COPY_ALL_ON_STOP, Value);
364}
365
366void CDasherInterface::DrawMouse(bool Value)
367{
368 m_DrawMouse = Value;
369 if (m_SettingsUI!=0)
370 m_SettingsUI->DrawMouse(Value);
371 if (m_SettingsStore!=0)
372 m_SettingsStore->SetBoolOption(Keys::DRAW_MOUSE, Value);
373}
374
375void CDasherInterface::StartOnSpace(bool Value)
376{
377 m_StartSpace = Value;
378 if (m_SettingsUI!=0)
379 m_SettingsUI->StartOnSpace(Value);
380 if (m_SettingsStore!=0)
381 m_SettingsStore->SetBoolOption(Keys::START_SPACE, Value);
382}
383
384void CDasherInterface::StartOnLeft(bool Value)
385{
386 m_StartLeft = Value;
387 if (m_SettingsUI!=0)
388 m_SettingsUI->StartOnLeft(Value);
389 if (m_SettingsStore!=0)
390 m_SettingsStore->SetBoolOption(Keys::START_MOUSE, Value);
391}
392
393void CDasherInterface::KeyControl(bool Value)
394{
395 m_KeyControl = Value;
396 if (m_SettingsUI!=0)
397 m_SettingsUI->KeyControl(Value);
398 if (m_SettingsStore!=0)
399 m_SettingsStore->SetBoolOption(Keys::KEY_CONTROL, Value);
400}
401
402void CDasherInterface::WindowPause(bool Value)
403{
404 m_KeyControl = Value;
405 if (m_SettingsUI!=0)
406 m_SettingsUI->WindowPause(Value);
407 if (m_SettingsStore!=0)
408 m_SettingsStore->SetBoolOption(Keys::WINDOW_PAUSE, Value);
409}
410
411void CDasherInterface::SetEditFont(string Name, long Size)
412{
413 m_EditFont = Name;
414 m_EditFontSize = Size;
415 if (m_DashEditbox)
416 m_DashEditbox->SetFont(Name, Size);
417 if (m_SettingsUI!=0)
418 m_SettingsUI->SetEditFont(Name, Size);
419 if (m_SettingsStore!=0) {
420 m_SettingsStore->SetStringOption(Keys::EDIT_FONT, Name);
421 m_SettingsStore->SetLongOption(Keys::EDIT_FONT_SIZE, Size);
422 }
423}
424
425
426void CDasherInterface::SetDasherFont(string Name)
427{
428 if (m_SettingsStore!=0)
429 m_SettingsStore->SetStringOption(Keys::DASHER_FONT, Name);
430 m_DasherFont = Name;
431 if (m_DasherScreen!=0)
432 m_DasherScreen->SetFont(Name);
433 Redraw();
434}
435
436void CDasherInterface::SetDasherFontSize(FontSize fontsize)
437{
438 if (m_SettingsStore!=0)
439 m_SettingsStore->SetLongOption(Keys::DASHER_FONTSIZE, fontsize);
440 if (m_DasherScreen!=0) {
441 m_DasherScreen->SetFontSize(fontsize);
442 }
443 Redraw();
444}
445
446void CDasherInterface::SetDasherDimensions(bool Value)
447{
448 m_Dimensions=Value;
449 if (m_SettingsStore!=0)
450 m_SettingsStore->SetBoolOption(Keys::DASHER_DIMENSIONS, Value);
451 if (m_DasherModel!=0) {
452 m_DasherModel->Set_dimensions(Value);
453 }
454 if (m_SettingsUI!=0) {
455 m_SettingsUI->SetDasherDimensions(Value);
456 }
457}
458
459
460unsigned int CDasherInterface::GetNumberSymbols()
461{
462 if (m_Alphabet!=0)
463 return m_Alphabet->GetNumberSymbols();
464 else
465 return 0;
466}
467
468
469const string& CDasherInterface::GetDisplayText(symbol Symbol)
470{
471 if (m_Alphabet!=0)
472 return m_Alphabet->GetDisplayText(Symbol);
473 else
474 return EmptyString;
475}
476
477
478const string& CDasherInterface::GetEditText(symbol Symbol)
479{
480 if (m_Alphabet!=0)
481 return m_Alphabet->GetText(Symbol);
482 else
483 return EmptyString;
484}
485
486int CDasherInterface::GetTextColour(symbol Symbol)
487{
488 if (m_Alphabet!=0)
489 return m_Alphabet->GetTextColour(Symbol);
490 else
491 return 0;
492}
493
494
495Opts::ScreenOrientations CDasherInterface::GetAlphabetOrientation()
496{
497 return m_Alphabet->GetOrientation();
498}
499
500
501Opts::AlphabetTypes CDasherInterface::GetAlphabetType()
502{
503 return m_Alphabet->GetType();
504}
505
506
507const std::string& CDasherInterface::GetTrainFile()
508{
509 return m_TrainFile;
510}
511
512
513void CDasherInterface::GetAlphabets(std::vector< std::string >* AlphabetList)
514{
515 if (!m_AlphIO)
516 m_AlphIO = new CAlphIO(m_SystemLocation, m_UserLocation);
517 m_AlphIO->GetAlphabets(AlphabetList);
518}
519
520
521const CAlphIO::AlphInfo& CDasherInterface::GetInfo(const std::string& AlphID)
522{
523 if (!m_AlphIO)
524 m_AlphIO = new CAlphIO(m_SystemLocation, m_UserLocation);
525
526 return m_AlphIO->GetInfo(AlphID);
527}
528
529
530void CDasherInterface::SetInfo(const CAlphIO::AlphInfo& NewInfo)
531{
532 if (!m_AlphIO)
533 m_AlphIO = new CAlphIO(m_SystemLocation, m_UserLocation);
534
535 m_AlphIO->SetInfo(NewInfo);
536}
537
538
539void CDasherInterface::DeleteAlphabet(const std::string& AlphID)
540{
541 if (!m_AlphIO)
542 m_AlphIO = new CAlphIO(m_SystemLocation, m_UserLocation);
543
544 m_AlphIO->Delete(AlphID);
545}
546
547
548void CDasherInterface::ChangeEdit()
549{
550 CreateDasherModel();
551 if (m_DasherModel!=0)
552 m_DasherModel->Start();
553 if (m_DasherView!=0)
554 m_DasherView->Render();
555}
556
557
558void CDasherInterface::ChangeEdit(CDashEditbox* NewEdit)
559{
560 m_DashEditbox = NewEdit;
561 m_DashEditbox->SetFont(m_EditFont, m_EditFontSize);
562 m_DashEditbox->SetInterface(this);
563 if (m_SettingsStore!=0)
564 m_DashEditbox->TimeStampNewFiles(m_SettingsStore->GetBoolOption(Keys::TIME_STAMP));
565 m_DashEditbox->New("");
566 ChangeEdit();
567}
568
569
570void CDasherInterface::Train(string* TrainString, bool IsMore)
571{
572 m_LanguageModel->LearnText(TrainContext, TrainString, IsMore);
573 return;
574}
575
576
577/*
578 I've used C style I/O because I found that C++ style I/O bloated
579 the Win32 code enormously. The overhead of loading the buffer into
580 the string instead of reading straight into a string seems to be
581 negligible compared to huge requirements elsewhere.
582*/
583void CDasherInterface::TrainFile(string Filename)
584{
585 if (Filename=="")
586 return;
587
588 FILE* InputFile;
589 if ( (InputFile = fopen(Filename.c_str(), "r")) == (FILE*)0)
590 return;
591
592 const int BufferSize = 1024;
593 char InputBuffer[BufferSize];
594 string StringBuffer;
595 int NumberRead;
596
597 do {
598 NumberRead = fread(InputBuffer, 1, BufferSize-1, InputFile);
599 InputBuffer[NumberRead] = '\0';
600 StringBuffer += InputBuffer;
601 if (NumberRead == (BufferSize-1)) {
602 m_LanguageModel->LearnText(TrainContext, &StringBuffer, true);
603 } else {
604 m_LanguageModel->LearnText(TrainContext, &StringBuffer, false);
605 }
606 } while (NumberRead==BufferSize-1);
607
608 fclose(InputFile);
609}
610
611void CDasherInterface::GetFontSizes(std::vector<int> *FontSizes)
612{
613 FontSizes->push_back(20);
614 FontSizes->push_back(14);
615 FontSizes->push_back(11);
616 FontSizes->push_back(40);
617 FontSizes->push_back(28);
618 FontSizes->push_back(22);
619 FontSizes->push_back(80);
620 FontSizes->push_back(56);
621 FontSizes->push_back(44);
622}
623
624
625double CDasherInterface::GetCurCPM()
626{
627 //
628 return 0;
629}
630
631
632double CDasherInterface::GetCurFPS()
633{
634 //
635 return 0;
636}