Tizen 2.1 base
[sdk/ide/native-sample.git] / samples / native / cpp / Sample / Tizen C++ / DrawingBoard / DrawingBoard / project / src / DrawingBoard.cpp
1 //
2 // Tizen C++ SDK
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.tizenopensource.org/license
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include "DrawingBoard.h"
19
20 using namespace Osp::App;
21 using namespace Osp::Base;
22 using namespace Osp::Base::Runtime;
23 using namespace Osp::Base::Utility;
24 using namespace Osp::Graphics;
25 using namespace Osp::Media;
26 using namespace Osp::System;
27 using namespace Osp::Ui;
28 using namespace Osp::Ui::Controls;
29
30
31 #define RED32(color32)   static_cast<byte>(0x00F8 & ((color32) >> 16))
32 #define GREEN32(color32) static_cast<byte>(0x00FC & ((color32) >> 8))
33 #define BLUE32(color32)  static_cast<byte>(0x00F8 & (color32))
34
35 #define RGB888ToCOLOR16(red, green, blue) (static_cast<unsigned short>((static_cast<byte>((blue) >> 3)) | (static_cast<unsigned short>(static_cast<byte>((green) >> 2)) << 5)) | ((static_cast<unsigned short>(static_cast<byte>((red) >> 3)) << 11)))
36 #define COLOR32ToCOLOR16(color32)         RGB888ToCOLOR16(RED32(color32), GREEN32(color32), BLUE32(color32))
37
38
39 const int REPEAT_COUNT   = 100;
40 const int TIME_OUT       = 10;
41 const int MAX_LINE_WIDTH = 10;
42 const int MAX_FONT_SIZE  = 50;
43
44
45 class DrawingForm
46         : public Osp::Ui::Controls::Form
47 {
48 public:
49         DrawingForm(DrawingBoard* pApp)
50                 : __pApp(pApp)
51         {
52         }
53
54         virtual ~DrawingForm(void)
55         {
56         }
57
58 public:
59         virtual result OnDraw(void)
60         {
61                 if (__pApp)
62                 {
63                         __pApp->Draw();
64                 }
65
66                 return E_SUCCESS;
67         }
68
69 private:
70         DrawingBoard* __pApp;
71 };
72
73
74 class Painter
75 {
76 public:
77         Painter(const Control& control)
78         {
79                 _count = 0;
80                 _pCanvas = control.GetCanvasN();
81                 _pCanvas->SetBackgroundColor(Color::GetColor(COLOR_ID_BLACK));
82                 _pCanvas->Clear();
83                 _pCanvas->Show();
84
85                 _bounds = _pCanvas->GetBounds();
86         }
87
88         virtual ~Painter()
89         {
90                 delete _pCanvas;
91         }
92
93         virtual bool Draw(void) = 0;
94
95 protected:
96         Canvas* _pCanvas;
97         int _count;
98         Rectangle _bounds;
99 };
100
101 class PixelPainter
102         : public Painter
103 {
104 public:
105         PixelPainter(const Control& control)
106                 : Painter(control)
107         {
108         }
109
110         virtual bool Draw(void)
111         {
112                 if (REPEAT_COUNT < _count++)
113                 {
114                         return false;
115                 }
116
117                 byte red   = Math::Rand() % 0xFF;
118                 byte green = Math::Rand() % 0xFF;
119                 byte blue  = Math::Rand() % 0xFF;
120                 byte alpha = Math::Rand() % 0xFF;
121
122                 Color color(red, green, blue, alpha);
123
124                 _pCanvas->SetForegroundColor(color);
125
126                 int x = (Math::Rand() % (_bounds.width -2)) + 1;
127                 int y = (Math::Rand() % (_bounds.height-2)) + 1;
128
129                 _pCanvas->SetPixel(Point(x - 1, y - 1));
130                 _pCanvas->SetPixel(Point(x + 1, y - 1));
131                 _pCanvas->SetPixel(Point(x, y - 1));
132                 _pCanvas->SetPixel(Point(x, y));
133                 _pCanvas->SetPixel(Point(x - 1, y + 1));
134                 _pCanvas->SetPixel(Point(x, y + 1));
135                 _pCanvas->SetPixel(Point(x + 1, y + 1));
136
137                 _pCanvas->Show();
138
139                 return true;
140         }
141 };
142
143
144 class LinePainter
145         : public Painter
146 {
147 public:
148         LinePainter(const Control& control)
149                 : Painter(control)
150         {
151         }
152
153         bool Draw(void)
154         {
155                 if (REPEAT_COUNT < _count++)
156                 {
157                         return false;
158                 }
159
160                 int lineWidth = (Math::Rand() % MAX_LINE_WIDTH) + 1;
161
162                 byte red   = Math::Rand() % 0xFF;
163                 byte green = Math::Rand() % 0xFF;
164                 byte blue  = Math::Rand() % 0xFF;
165                 byte alpha = Math::Rand() % 0xFF;
166
167                 Color color(red, green, blue, alpha);
168                 _pCanvas->SetForegroundColor(color);
169
170                 Point point1(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height);
171                 Point point2(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height);
172
173                 _pCanvas->SetLineWidth(lineWidth);
174                 _pCanvas->DrawLine(point1, point2);
175
176                 _pCanvas->Show();
177
178                 return true;
179         }
180 };
181
182
183 class TrianglePainter
184         : public Painter
185 {
186 public:
187         TrianglePainter(const Control& control)
188                 : Painter(control)
189         {
190         }
191
192         bool Draw(void)
193         {
194                 if (REPEAT_COUNT < _count++)
195                 {
196                         return false;
197                 }
198
199                 int lineWidth = (Math::Rand() % MAX_LINE_WIDTH) + 1;
200
201                 _pCanvas->SetLineWidth(lineWidth);
202
203                 byte red   = Math::Rand() % 0xFF;
204                 byte green = Math::Rand() % 0xFF;
205                 byte blue  = Math::Rand() % 0xFF;
206                 byte alpha = Math::Rand() % 0xFF;
207
208                 Color color(red, green, blue, alpha);
209
210                 _pCanvas->SetForegroundColor(color);
211
212                 Point point1(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height);
213                 Point point2(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height);
214                 Point point3(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height);
215
216                 _pCanvas->DrawTriangle(point1, point2, point3);
217
218                 point1.x = Math::Rand() % _bounds.width;
219                 point1.y = Math::Rand() % _bounds.height;
220                 point2.x = Math::Rand() % _bounds.width;
221                 point2.y = Math::Rand() % _bounds.height;
222                 point3.x = Math::Rand() % _bounds.width;
223                 point3.y = Math::Rand() % _bounds.height;
224
225                 _pCanvas->FillTriangle(color, point1, point2, point3);
226
227                 _pCanvas->Show();
228
229                 return true;
230         }
231 };
232
233
234 class RectanglePainter
235         : public Painter
236 {
237 public:
238         RectanglePainter(const Control& control)
239                 : Painter(control)
240         {
241         }
242
243         bool Draw(void)
244         {
245                 if (REPEAT_COUNT < _count++)
246                 {
247                         return false;
248                 }
249
250                 int lineWidth = (Math::Rand() % MAX_LINE_WIDTH) + 1;
251
252                 _pCanvas->SetLineWidth(lineWidth);
253
254                 byte red   = Math::Rand() % 0xFF;
255                 byte green = Math::Rand() % 0xFF;
256                 byte blue  = Math::Rand() % 0xFF;
257                 byte alpha = Math::Rand() % 0xFF;
258
259                 Color color(red, green, blue, alpha);
260
261                 _pCanvas->SetForegroundColor(color);
262
263                 Rectangle rect(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height, Math::Rand() % _bounds.width, Math::Rand() % _bounds.height);
264
265                 _pCanvas->DrawRectangle(rect);
266
267                 rect.x = Math::Rand() % _bounds.width;
268                 rect.y = Math::Rand() % _bounds.height;
269                 rect.width  = Math::Rand() % _bounds.width;
270                 rect.height = Math::Rand() % _bounds.height;
271
272                 _pCanvas->FillRectangle(color, rect);
273
274                 _pCanvas->Show();
275
276                 return true;
277         }
278 };
279
280
281 class RoundRectanglePainter
282         : public Painter
283 {
284 public:
285         RoundRectanglePainter(const Control& control)
286                 : Painter(control)
287         {
288         }
289
290         bool Draw(void)
291         {
292                 if (REPEAT_COUNT < _count++)
293                 {
294                         return false;
295                 }
296
297                 int lineWidth = (Math::Rand() % MAX_LINE_WIDTH) + 1;
298
299                 _pCanvas->SetLineWidth(lineWidth);
300
301                 byte red   = Math::Rand() % 0xFF;
302                 byte green = Math::Rand() % 0xFF;
303                 byte blue  = Math::Rand() % 0xFF;
304                 byte alpha = Math::Rand() % 0xFF;
305
306                 Color color(red, green, blue, alpha);
307
308                 _pCanvas->SetForegroundColor(color);
309
310                 Rectangle rect(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height, Math::Rand() % _bounds.width + 1, Math::Rand() % _bounds.height + 1);
311                 Dimension round(rect.width / MAX_LINE_WIDTH, rect.height / MAX_LINE_WIDTH);
312
313                 _pCanvas->DrawRoundRectangle(rect, round);
314
315                 rect.x = Math::Rand() % _bounds.width;
316                 rect.y = Math::Rand() % _bounds.height;
317                 rect.width  = Math::Rand() % _bounds.width;
318                 rect.height = Math::Rand() % _bounds.height;
319
320                 round.width = rect.width / MAX_LINE_WIDTH;
321                 round.height = rect.height / MAX_LINE_WIDTH;
322
323                 _pCanvas->FillRoundRectangle(color, rect, round);
324
325                 _pCanvas->Show();
326
327                 return true;
328         }
329 };
330
331
332 class EllipsePainter
333         : public Painter
334 {
335 public:
336         EllipsePainter(const Control& control)
337                 : Painter(control)
338         {
339         }
340
341         bool Draw(void)
342         {
343                 if (REPEAT_COUNT < _count++)
344                 {
345                         return false;
346                 }
347
348                 int lineWidth = (Math::Rand() % MAX_LINE_WIDTH) + 1;
349
350                 _pCanvas->SetLineWidth(lineWidth);
351
352                 byte red   = Math::Rand() % 0xFF;
353                 byte green = Math::Rand() % 0xFF;
354                 byte blue  = Math::Rand() % 0xFF;
355                 byte alpha = Math::Rand() % 0xFF;
356
357                 Color color(red, green, blue, alpha);
358
359                 _pCanvas->SetForegroundColor(color);
360
361                 Rectangle rect(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height, Math::Rand() % _bounds.width, Math::Rand() % _bounds.height);
362
363                 _pCanvas->DrawEllipse(rect);
364
365                 rect.x = Math::Rand() % _bounds.width;
366                 rect.y = Math::Rand() % _bounds.height;
367                 rect.width  = Math::Rand() % _bounds.width;
368                 rect.height = Math::Rand() % _bounds.height;
369
370                 _pCanvas->FillEllipse(color, rect);
371
372                 _pCanvas->Show();
373
374                 return true;
375         }
376 };
377
378
379 class ArcPainter
380         : public Painter
381 {
382 public:
383         ArcPainter(const Control& control)
384                 : Painter(control)
385         {
386         }
387
388         bool Draw(void)
389         {
390                 if (REPEAT_COUNT < _count++)
391                 {
392                         return false;
393                 }
394
395                 int lineWidth = (Math::Rand() % MAX_LINE_WIDTH) + 1;
396
397                 _pCanvas->SetLineWidth(lineWidth);
398
399                 byte red   = Math::Rand() % 0xFF;
400                 byte green = Math::Rand() % 0xFF;
401                 byte blue  = Math::Rand() % 0xFF;
402                 byte alpha = Math::Rand() % 0xFF;
403
404                 Color color(red, green, blue, alpha);
405                 _pCanvas->SetForegroundColor(color);
406
407                 Rectangle rect(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height, Math::Rand() % _bounds.width, Math::Rand() % _bounds.height);
408
409                 int startAngle = Math::Rand() % 360;
410                 int endAngle = Math::Rand() % 360;
411
412                 ArcStyle arcStyle = ARC_STYLE_ONLY;
413
414                 switch (Math::Rand() % 3)
415                 {
416                 case 0:
417                         arcStyle = ARC_STYLE_ONLY;
418                         break;
419                 case 1:
420                         arcStyle = ARC_STYLE_CHORD;
421                         break;
422                 default:
423                         arcStyle = ARC_STYLE_PIE;
424                         break;
425                 }
426
427                 _pCanvas->DrawArc(rect, startAngle, endAngle, arcStyle);
428
429                 _pCanvas->Show();
430
431                 return true;
432         }
433 };
434
435
436 class PolylinePainter
437         : public Painter
438 {
439 public:
440         PolylinePainter(const Control& control)
441                 : Painter(control)
442         {
443                 for (int i = 0; i < 6; i++)
444                 {
445                         __pointList.Add(*(new (std::nothrow) Point));
446                 }
447         }
448
449         virtual ~PolylinePainter()
450         {
451                 __pointList.RemoveAll(true);
452         }
453
454         bool Draw(void)
455         {
456                 if (REPEAT_COUNT < _count++)
457                 {
458                         return false;
459                 }
460
461                 int lineWidth = (Math::Rand() % MAX_LINE_WIDTH) + 1;
462
463                 _pCanvas->SetLineWidth(lineWidth);
464
465                 byte red   = Math::Rand() % 0xFF;
466                 byte green = Math::Rand() % 0xFF;
467                 byte blue  = Math::Rand() % 0xFF;
468                 byte alpha = Math::Rand() % 0xFF;
469
470                 Color color(red, green, blue, alpha);
471
472                 _pCanvas->SetForegroundColor(color);
473
474                 for (int i = 0; i < __pointList.GetCount(); i++)
475                 {
476                         Point* pPoint = dynamic_cast<Point*>(__pointList.GetAt(i));
477
478                         if (pPoint)
479                         {
480                                 pPoint->x = Math::Rand() % _bounds.width;
481                                 pPoint->y = Math::Rand() % _bounds.height;
482                         }
483                 }
484
485                 _pCanvas->DrawPolyline(__pointList);
486
487                 _pCanvas->Show();
488
489                 return true;
490         }
491
492 private:
493         Osp::Base::Collection::ArrayList __pointList;
494 };
495
496
497 class PolygonPainter
498         : public Painter
499 {
500 public:
501         PolygonPainter(const Control& control)
502                 : Painter(control)
503         {
504                 for (int i = 0; i < 6; i++)
505                 {
506                         __pointList.Add(*(new (std::nothrow) Point));
507                 }
508         }
509
510         virtual ~PolygonPainter()
511         {
512                 __pointList.RemoveAll(true);
513         }
514
515         bool Draw(void)
516         {
517                 if (REPEAT_COUNT < _count++)
518                 {
519                         return false;
520                 }
521
522                 int lineWidth = (Math::Rand() % MAX_LINE_WIDTH) + 1;
523
524                 _pCanvas->SetLineWidth(lineWidth);
525
526                 byte red   = Math::Rand() % 0xFF;
527                 byte green = Math::Rand() % 0xFF;
528                 byte blue  = Math::Rand() % 0xFF;
529                 byte alpha = Math::Rand() % 0xFF;
530
531                 Color color(red, green, blue, alpha);
532
533                 _pCanvas->SetForegroundColor(color);
534
535                 for (int i = 0; i < __pointList.GetCount(); i++)
536                 {
537                         Point* pPoint = dynamic_cast<Point*>(__pointList.GetAt(i));
538
539                         if (pPoint)
540                         {
541                                 pPoint->x = Math::Rand() % _bounds.width;
542                                 pPoint->y = Math::Rand() % _bounds.height;
543                         }
544                 }
545
546                 _pCanvas->DrawPolygon(__pointList);
547
548                 for (int i = 0; i < __pointList.GetCount(); i++)
549                 {
550                         Point* pPoint = dynamic_cast<Point*>(__pointList.GetAt(i));
551
552                         if (pPoint)
553                         {
554                                 pPoint->x = Math::Rand() % _bounds.width;
555                                 pPoint->y = Math::Rand() % _bounds.height;
556                         }
557                 }
558
559                 _pCanvas->FillPolygon(color, __pointList);
560
561                 _pCanvas->Show();
562
563                 return true;
564         }
565
566 private:
567         Osp::Base::Collection::ArrayList __pointList;
568 };
569
570
571 class TextPainter
572         : public Painter
573 {
574 public:
575         TextPainter(const Control& control)
576                 : Painter(control)
577         {
578         }
579
580         bool Draw(void)
581         {
582                 if (REPEAT_COUNT < _count++)
583                 {
584                         return false;
585                 }
586
587                 int fontStyle = FONT_STYLE_PLAIN;
588
589                 switch (Math::Rand() % 3)
590                 {
591                 case 0:
592                         fontStyle = FONT_STYLE_PLAIN;
593                         break;
594                 case 1:
595                         fontStyle = FONT_STYLE_BOLD;
596                         break;
597                 default:
598                         fontStyle = FONT_STYLE_ITALIC;
599                         break;
600                 }
601
602                 int fontSize = Math::Rand() % MAX_FONT_SIZE;
603
604                 if (fontSize < 10)
605                 {
606                         fontSize = 10;
607                 }
608
609                 Font font;
610
611                 result r = font.Construct(fontStyle, fontSize);
612
613                 if (IsFailed(r))
614                 {
615                         AppLog("Fails to construct font.\n");
616                         return false;
617                 }
618
619                 _pCanvas->SetFont(font);
620
621                 byte red   = Math::Rand() % 0xFF;
622                 byte green = Math::Rand() % 0xFF;
623                 byte blue  = Math::Rand() % 0xFF;
624                 byte alpha = Math::Rand() % 0xFF;
625
626                 Color color(red, green, blue, alpha);
627
628                 _pCanvas->SetForegroundColor(color);
629
630                 Point point(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height);
631
632                 Osp::Base::String text(L"DrawingBoard Sample");
633
634                 _pCanvas->DrawText(point, text);
635
636                 _pCanvas->Show();
637
638                 return true;
639         }
640 };
641
642
643 class BitmapPainter
644         : public Painter
645 {
646 public:
647         BitmapPainter(const Control& control)
648                 : Painter(control)
649                 , __pBitmap16(null)
650                 , __pBitmap32(null)
651         {
652                 Image imageDecoder;
653
654                 imageDecoder.Construct();
655
656                 __pBitmap16 = imageDecoder.DecodeN(App::GetInstance()->GetAppRootPath() + L"res/tizen.png", BITMAP_PIXEL_FORMAT_RGB565);
657                 __pBitmap32 = imageDecoder.DecodeN(App::GetInstance()->GetAppRootPath() + L"res/tizen.png", BITMAP_PIXEL_FORMAT_ARGB8888);
658         }
659
660         virtual ~BitmapPainter()
661         {
662                 delete __pBitmap16;
663                 delete __pBitmap32;
664         }
665
666         bool Draw(void)
667         {
668                 if (REPEAT_COUNT < _count++)
669                 {
670                         return false;
671                 }
672
673                 Point pos(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height);
674
675                 _pCanvas->DrawBitmap(pos, *__pBitmap16);
676
677                 pos.x = Math::Rand() % _bounds.width;
678                 pos.y = Math::Rand() % _bounds.height;
679
680                 _pCanvas->DrawBitmap(pos, *__pBitmap32);
681
682                 Rectangle rect(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height, (Math::Rand() % (_bounds.width - 1)) + 1, (Math::Rand() % (_bounds.height- 1)) + 1);
683
684                 _pCanvas->DrawBitmap(rect, *__pBitmap16);
685
686                 rect.x = Math::Rand() % _bounds.width;
687                 rect.y = Math::Rand() % _bounds.height;
688                 rect.width = (Math::Rand() % (_bounds.width - 1)) + 1;
689                 rect.height = (Math::Rand() % (_bounds.height- 1)) + 1;
690
691                 _pCanvas->DrawBitmap(rect, *__pBitmap32);
692
693                 rect.x = Math::Rand() % __pBitmap16->GetWidth();
694                 rect.y = Math::Rand() % __pBitmap16->GetHeight();
695                 rect.width = (Math::Rand() % (__pBitmap16->GetWidth() - 1)) + 1;
696                 rect.height = (Math::Rand() % (__pBitmap16->GetHeight() - 1)) + 1;
697
698                 if (rect.x + rect.width > __pBitmap16->GetWidth())
699                 {
700                         rect.width = __pBitmap16->GetWidth() - rect.x;
701                 }
702
703                 if (rect.y + rect.height > __pBitmap16->GetHeight())
704                 {
705                         rect.height = __pBitmap16->GetHeight() - rect.y;
706                 }
707
708                 Rectangle destRect(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height, (Math::Rand() % (_bounds.width - 1)) + 1, (Math::Rand() % (_bounds.height- 1)) + 1);
709
710                 _pCanvas->DrawBitmap(destRect, *__pBitmap16, rect);
711
712                 rect.x = Math::Rand() % __pBitmap32->GetWidth();
713                 rect.y = Math::Rand() % __pBitmap32->GetHeight();
714                 rect.width = (Math::Rand() % (__pBitmap32->GetWidth() - 1)) + 1;
715                 rect.height = (Math::Rand() % (__pBitmap32->GetHeight()- 1)) + 1;
716
717                 if (rect.x + rect.width > __pBitmap32->GetWidth())
718                 {
719                         rect.width = __pBitmap32->GetWidth() - rect.x;
720                 }
721
722                 if (rect.y + rect.height > __pBitmap32->GetHeight())
723                 {
724                         rect.height = __pBitmap32->GetHeight() - rect.y;
725                 }
726
727                 destRect.x = Math::Rand() % _bounds.width;
728                 destRect.y = Math::Rand() % _bounds.height;
729                 destRect.width  = (Math::Rand() % (_bounds.width - 1)) + 1;
730                 destRect.height = (Math::Rand() % (_bounds.height- 1)) + 1;
731
732                 _pCanvas->DrawBitmap(destRect, *__pBitmap32, rect);
733
734                 _pCanvas->Show();
735
736                 return true;
737         }
738
739 private:
740         Bitmap* __pBitmap16;
741         Bitmap* __pBitmap32;
742 };
743
744
745 class DirectBufferPainter
746         : public Painter
747 {
748 public:
749         DirectBufferPainter(const Control& control)
750                 : Painter(control)
751         {
752         }
753
754         bool Draw(void)
755         {
756                 if (REPEAT_COUNT < _count++)
757                 {
758                         return false;
759                 }
760
761                 byte red   = Math::Rand() % 0xFF;
762                 byte green = Math::Rand() % 0xFF;
763                 byte blue  = Math::Rand() % 0xFF;
764                 byte alpha = Math::Rand() % 0xFF;
765
766                 Color color(red, green, blue, alpha);
767
768                 _pCanvas->SetForegroundColor(color);
769
770                 Rectangle rect(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height, Math::Rand() % _bounds.width, Math::Rand() % _bounds.height);
771
772                 FillRectangle(*_pCanvas, color, rect);
773
774                 _pCanvas->Show();
775
776                 return true;
777         }
778
779         void FillRectangle(Canvas& canvas, const Color &color, const Rectangle &rect)
780         {
781                 BufferInfo bufferInfo;
782                 int x;
783                 int y;
784                 int offset;
785                 byte srcR;
786                 byte srcG;
787                 byte srcB;
788                 byte destR;
789                 byte destG;
790                 byte destB;
791                 int alphaLevel;
792                 int srcLevel;
793                 int destLevel;
794                 int r;
795                 int g;
796                 int b;
797                 unsigned short color16;
798                 unsigned short* pBuffer16;
799                 unsigned long* pBuffer32;
800                 int bufferWidth;
801                 int bufferX1;
802                 int bufferY1;
803                 int bufferX2;
804                 int bufferY2;
805                 int x1;
806                 int y1;
807                 int x2;
808                 int y2;
809                 int width;
810                 int height;
811
812                 _pCanvas->Lock(bufferInfo);
813
814                 x1 = rect.x;
815                 y1 = rect.y;
816                 width  = rect.width;
817                 height = rect.height;
818
819                 x2 = x1 + width - 1;
820                 y2 = y1 + height - 1;
821
822                 bufferX1 = 0;
823                 bufferY1 = 0;
824                 bufferX2 = bufferX1 + bufferInfo.width - 1;
825                 bufferY2 = bufferY1 + bufferInfo.height - 1;
826
827                 if (x1 > bufferX2 || x2 < bufferX1)
828                 {
829                         return;
830                 }
831
832                 if (y1 > bufferY2 || y2 < bufferY1)
833                 {
834                         return;
835                 }
836
837                 if (x1 < bufferX1)
838                 {
839                         x1 = bufferX1;
840                 }
841
842                 if (x2 > bufferX2)
843                 {
844                         x2 = bufferX2;
845                 }
846
847                 if (y1 < bufferY1)
848                 {
849                         y1 = bufferY1;
850                 }
851
852                 if (y2 > bufferY2)
853                 {
854                         y2 = bufferY2;
855                 }
856
857                 width  = x2 - x1 + 1;
858                 height = y2 - y1 + 1;
859
860                 bufferWidth = bufferInfo.width;
861
862                 if (bufferInfo.bitsPerPixel == 16)
863                 {
864
865                         color16 = static_cast<unsigned short>(COLOR32ToCOLOR16(color.GetRGB32()));
866                         pBuffer16 = static_cast<unsigned short*>(bufferInfo.pPixels);
867
868                         alphaLevel = 100 - static_cast<int>(static_cast<float>(color.GetAlpha() / 2.56));
869                         srcLevel = alphaLevel;
870                         destLevel = 100 - alphaLevel;
871
872                         srcR = static_cast<byte>(color16 >> 11);
873                         srcG = static_cast<byte>((color16 >> 6) & 0x001F);
874                         srcB = static_cast<byte>(color16 & 0x001F);
875
876                         for (y = y1; y < y1 + height; y++)
877                         {
878                                 for (x = x1; x < x1 + width; x++)
879                                 {
880                                         offset = bufferWidth * y + x;
881                                         destR = static_cast<byte>(pBuffer16[offset] >> 11);
882                                         destG = static_cast<byte>((pBuffer16[offset] >> 6) & 0x001F);
883                                         destB = static_cast<byte>(pBuffer16[offset] & 0x001F);
884
885                                         r = (destR * destLevel + srcR * srcLevel) / 100;
886                                         g = (destG * destLevel + srcG * srcLevel) / 100;
887                                         b = (destB * destLevel + srcB * srcLevel) / 100;
888
889                                         destR = static_cast<byte>(r);
890                                         destG = static_cast<byte>(g);
891                                         destB = static_cast<byte>(b);
892                                         pBuffer16[offset] = static_cast<unsigned short>((destR << 11) | (destG << 6) | (destB));
893                                 }
894                         }
895                 }
896                 else if (bufferInfo.bitsPerPixel == 32)
897                 {
898                         pBuffer32 = (unsigned long*)bufferInfo.pPixels;
899
900                         for (y = y1; y < y1 + height; y++)
901                         {
902                                 for (x = x1; x < x1 + width; x++)
903                                 {
904                                         offset = bufferWidth * y + x;
905                                         pBuffer32[offset] = color.GetRGB32();
906                                 }
907                         }
908                 }
909
910                 _pCanvas->Unlock();
911         }
912 };
913
914
915 class ColorTablePainter
916         : public Painter
917 {
918 public:
919         ColorTablePainter(const Control& control)
920                 : Painter(control)
921                 , __x(0)
922                 , __y(0)
923                 , __width(25)
924                 , __height(25)
925                 , __red(0)
926                 , __green(0)
927                 , __blue(0)
928                 , __alpha(0xFF)
929         {
930         }
931
932         bool Draw(void)
933         {
934                 if (__y >= _bounds.height)
935                 {
936                         return false;
937                 }
938
939                 Color color(__red, __green, __blue, __alpha);
940
941                 _pCanvas->FillRectangle(color, Rectangle(__x, __y, __width, __height));
942
943                 _pCanvas->Show();
944
945                 if (__red < 0xFF)
946                 {
947                         __red++;
948                 }
949
950                 if (__red >= 0xFF && __green < 0xFF)
951                 {
952                         __green++;
953                 }
954
955                 if (__green >= 0xFF && __blue < 0xFF)
956                 {
957                         __blue++;
958                 }
959
960                 __x += __width;
961
962                 if (__x >= _bounds.width)
963                 {
964                         __x = 0;
965                         __y += __height;
966                 }
967
968                 return true;
969         }
970
971 private:
972         int __x;
973         int __y;
974         int __width;
975         int __height;
976         byte __red;
977         byte __green;
978         byte __blue;
979         byte __alpha;
980 };
981
982
983 DrawingBoard::DrawingBoard()
984         : __pForm(null)
985         , __pPainter(null)
986         , __pTimer(null)
987         , __scenarioIndex(0)
988 {
989 }
990
991
992 DrawingBoard::~DrawingBoard()
993 {
994 }
995
996
997 Application*
998 DrawingBoard::CreateInstance(void)
999 {
1000         // You can create the instance through another constructor.
1001         return new (std::nothrow) DrawingBoard();
1002 }
1003
1004 bool
1005 DrawingBoard::OnAppInitializing(AppRegistry& appRegistry)
1006 {
1007         Frame* pAppFrame = new (std::nothrow) Frame();
1008
1009         pAppFrame->Construct();
1010
1011         this->AddFrame(*pAppFrame);
1012
1013         result r = E_SUCCESS;
1014
1015         __pForm = new (std::nothrow) DrawingForm(this);
1016         TryCatch(__pForm != null, , "Failed to allocate memory.");
1017
1018         r = __pForm->Construct(FORM_STYLE_NORMAL);
1019         TryCatch(!IsFailed(r), , "__pForm->Construct(FORM_STYLE_NORMAL) failed.");
1020
1021         __pForm->SetBackgroundColor(Color::GetColor(COLOR_ID_BLACK));
1022         r = this->GetAppFrame()->GetFrame()->AddControl(*__pForm);
1023         TryCatch(!IsFailed(r), , "__pForm->Construct(FORM_STYLE_NORMAL) failed.");
1024
1025         __pTimer = new (std::nothrow) Timer;
1026         TryCatch(__pTimer != null, , "Failed to allocate memory.");
1027
1028         r = __pTimer->Construct(*this);
1029         TryCatch(!IsFailed(r), , "Failed to construct timer.");
1030
1031         return true;
1032
1033 CATCH:
1034         delete __pForm;
1035         __pForm = null;
1036
1037         if (__pTimer != null)
1038         {
1039                 __pTimer->Cancel();
1040
1041                 delete __pTimer;
1042                 __pTimer = null;
1043         }
1044
1045         return false;
1046 }
1047
1048
1049 bool
1050 DrawingBoard::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
1051 {
1052         if (__pTimer != null)
1053         {
1054                 __pTimer->Cancel();
1055
1056                 delete __pTimer;
1057                 __pTimer = null;
1058         }
1059
1060         if (__pPainter != null)
1061         {
1062                 delete __pPainter;
1063                 __pPainter = null;
1064         }
1065
1066         return true;
1067 }
1068
1069
1070 void
1071 DrawingBoard::OnForeground(void)
1072 {
1073         if (__pTimer != null)
1074         {
1075                 __pTimer->Start(TIME_OUT);
1076         }
1077 }
1078
1079
1080 void
1081 DrawingBoard::OnBackground(void)
1082 {
1083         if (__pTimer != null)
1084         {
1085                 __pTimer->Cancel();
1086         }
1087
1088         if (__pForm != null)
1089         {
1090                 Canvas* pCanvas = __pForm->GetCanvasN();
1091
1092                 pCanvas->SetBackgroundColor(Color(0x00000000));
1093                 pCanvas->Clear();
1094
1095                 pCanvas->Show();
1096
1097                 delete pCanvas;
1098         }
1099 }
1100
1101
1102 void
1103 DrawingBoard::OnLowMemory(void)
1104 {
1105 }
1106
1107
1108 void
1109 DrawingBoard::OnBatteryLevelChanged(BatteryLevel batteryLevel)
1110 {
1111 }
1112
1113
1114 void
1115 DrawingBoard::OnTimerExpired(Timer& timer)
1116 {
1117         if (__pTimer == null)
1118         {
1119                 return;
1120         }
1121
1122         if (__pPainter == null)
1123         {
1124                 ChangePainter();
1125         }
1126         else
1127         {
1128                 bool result = __pPainter->Draw();
1129
1130                 if (!result)
1131                 {
1132                         ChangePainter();
1133                 }
1134         }
1135
1136         __pTimer->Start(TIME_OUT);
1137 }
1138
1139 void
1140 DrawingBoard::ChangePainter()
1141 {
1142         if (__pForm == null)
1143         {
1144                 return;
1145         }
1146
1147         delete __pPainter;
1148         __pPainter = null;
1149
1150         switch (__scenarioIndex)
1151         {
1152         case 0:
1153                 __pPainter = new (std::nothrow) PixelPainter(*__pForm);
1154                 break;
1155         case 1:
1156                 __pPainter = new (std::nothrow) LinePainter(*__pForm);
1157                 break;
1158         case 2:
1159                 __pPainter = new (std::nothrow) TrianglePainter(*__pForm);
1160                 break;
1161         case 3:
1162                 __pPainter = new (std::nothrow) RectanglePainter(*__pForm);
1163                 break;
1164         case 4:
1165                 __pPainter = new (std::nothrow) RoundRectanglePainter(*__pForm);
1166                 break;
1167         case 5:
1168                 __pPainter = new (std::nothrow) EllipsePainter(*__pForm);
1169                 break;
1170         case 6:
1171                 __pPainter = new (std::nothrow) ArcPainter(*__pForm);
1172                 break;
1173         case 7:
1174                 __pPainter = new (std::nothrow) TextPainter(*__pForm);
1175                 break;
1176         case 8:
1177                 __pPainter = new (std::nothrow) BitmapPainter(*__pForm);
1178                 break;
1179         case 9:
1180                 __pPainter = new (std::nothrow) DirectBufferPainter(*__pForm);
1181                 break;
1182         case 10:
1183                 __pPainter = new (std::nothrow) ColorTablePainter(*__pForm);
1184                 break;
1185         case 11:
1186                 __pPainter = new (std::nothrow) PolylinePainter(*__pForm);
1187                 break;
1188         case 12:
1189                 __pPainter = new (std::nothrow) PolygonPainter(*__pForm);
1190                 break;
1191         default:
1192                 __scenarioIndex = 0;
1193                 break;
1194         }
1195
1196         __scenarioIndex++;
1197
1198         if (__scenarioIndex > 12)
1199         {
1200                 __scenarioIndex = 0;
1201         }
1202 }
1203
1204 void
1205 DrawingBoard::Draw(void)
1206 {
1207         if (__pPainter != null)
1208         {
1209                 __pPainter->Draw();
1210         }
1211 }
1212
1213 void
1214 DrawingBoard::OnScreenOn (void)
1215 {
1216 }
1217
1218 void
1219 DrawingBoard::OnScreenOff (void)
1220 {
1221 }
1222