File: myx_gc_canvas.cpp

package info (click to toggle)
mysql-query-browser 1.1.6-1sarge0
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 36,320 kB
  • ctags: 24,680
  • sloc: pascal: 203,479; xml: 136,561; ansic: 47,502; cpp: 28,926; sh: 12,433; objc: 4,823; java: 1,849; php: 1,485; python: 1,225; sql: 1,128; makefile: 872
file content (907 lines) | stat: -rw-r--r-- 28,032 bytes parent folder | download
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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
/* Copyright (C) 2004 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/**
 * @file myx_gc_canvas.cpp 
 * @brief Generic canvas main class and entry point.
 * 
 */

#ifdef _WINDOWS
  #include <windows.h>
#else
#endif // ifdef _WINDOWS

#include <assert.h>
#include <gl/gl.h>
#include <gl/glu.h>

#include "myx_gc_figure.h"
#include "myx_gc_layer.h"
#include "myx_gc_model.h"
#include "myx_gc_canvas.h"
#include "myx_gc_gl_helper.h"
#include "myx_gc_font_manager.h"
#include "myx_gc_texture.h"

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

// Factory function for a canvas.
GENERIC_CANVAS_API CGenericCanvas* CreateGenericCanvas(GCContext Context)
{
	return new CGenericCanvas(Context);
}

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

void CHitResults::AddHit(CFigureInstance* Instance, double Min, double Max)
{
  THitEntry Entry;
  Entry.Instance = Instance;
  Entry.ZMax = Max;
  Entry.ZMin = Min;
  FEntries.push_back(Entry);
}

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

int CHitResults::Count(void)
{
  return FEntries.size();
}

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

THitEntry* CHitResults::Get(int I)
{
  return &FEntries[I];
}

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

void CHitResults::Release(void)
{
  delete this;
}

//----------------- CGenericCanvas -------------------------------------------------------------------------------------

CGenericCanvas::CGenericCanvas(GCContext Context)
{ 
  FIsPicking = false;
	FContext = Context;
  FViewport.Left = 0;
  FViewport.Top = 0;
  FViewport.Width = 100;
  FViewport.Height = 100;
  FUpdateCount = 0;
  FZoomX = 1;
  FZoomY = 1;
  FOffsetX = 0;
  FOffsetY = 0;

  FModel = new CGCModel(this);
  FontManager()->CreateDefaultEntry();
  FSelectionLayer = new CSelectionLayer(this);

  FBackgroundColor = 0;
  glClearColor(0, 0, 0, 1);

  // TODO: make this dynamically configurable (where applicable).
  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  glFrontFace(GL_CW);
  glEnable(GL_AUTO_NORMAL);
  glEnable(GL_CULL_FACE);
  glDisable(GL_DEPTH_TEST);

  glEnable(GL_LINE_SMOOTH);
  glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
  glEnable(GL_POLYGON_SMOOTH);
  glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}

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

CGenericCanvas::~CGenericCanvas(void)
{
  // Free any dynamically allocated memory. Explicitely increase update count. We don't want any recursive
  // notifcations anymore.
  FUpdateCount++;
  FSelectionLayer->BeginUpdate();
  for (CLayerIterator Iterator = FLayers.begin(); Iterator != FLayers.end(); Iterator++)
  {
    CLayer* Layer = *Iterator;
    delete Layer;
  };
  ClearTemplates();
  delete FModel;
  delete FSelectionLayer;
}

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

void CGenericCanvas::ApplyViewport(void)
{
  glViewport(FViewport.Left, FViewport.Top, FViewport.Width, FViewport.Height);
}

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

void CGenericCanvas::ClearBuffers(void)
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

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

/*
 * Looks through the templates and attempts to find one with the given name.
 *
 * @param Name The name of the template to fined.
 * @return Returns the -1 if the template could not be found. Otherwise the result is the associated OpenGL display list.
 */
GLuint CGenericCanvas::FindTemplate(const string& Name)
{
  CTemplateIterator Iterator = FTemplates.find(Name);
  if (Iterator == FTemplates.end())
    return -1;
  else
    return Iterator->second;
}

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

/**
 * @brief Adds the given layer at the end of the layer list. The new layer becomes the top layer in the view then.
 * @param Layer The layer to add.
 */
void CGenericCanvas::AddLayer(CLayer* Layer)
{
  FLayers.push_back(Layer);
  if (FUpdateCount == 0)
    Invalidate();
}

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

/**
 * @brief Adds a listener to the internal list.
 *
 * @param Listener The listener to add.
 */
void CGenericCanvas::AddListener(CGCListener* Listener)
{
  FListeners.push_back(Listener);
}

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

/** 
 * Reads the layout info stored in the given (XML) file and creates OpenGL display lists. These lists are stored in a 
 * hash map together with their names, so they can quickly be found by the figures.
 *
 * @param Filename The name of the file to load.
 * @return Returns GC_NO_ERROR if everything was ok, otherwise an error code.
 */
TGCError CGenericCanvas::AddTemplatesFromFile(const char* Filename)
{
  TGCError Result = GC_NO_ERROR;

  xmlDocPtr Document;
  xmlNodePtr Root, Current;

  Result = GC_NO_ERROR;

  string CurrentDir = GetCurrentDir();
  Document = xmlParseFile(Utf8ToANSI(string(Filename)).c_str());

  if (Document == NULL)
    return GC_XML_PARSE_ERROR;

  Root = xmlDocGetRootElement(Document);

  if (Root == NULL)
  {
    xmlFreeDoc(Document);
    return GC_XML_EMPTY_DOCUMENT;
  }
  
  if (xmlStrcmp(Root->name, (const xmlChar *) "gc-layout") != 0)
  {
    xmlFreeDoc(Document);
    return GC_XML_INVALID_DOCUMENT;
  }

  // Switch to the directory of the given file. This is necessary to make relative file names working.
  string Path = ExtractFilePath(Filename);
  SetCurrentDir(Path);

  // Parse description elements.
  Current = Root->children;
  while (Current != NULL)
  {
    // Be flexible, ignore any unknown layout entries.
    if ((Current->type == XML_ELEMENT_NODE) && (xmlStrcmp(Current->name, (const xmlChar *) "layout-definition") == 0))
    {
      xmlChar* Attribute = xmlGetProp(Current, (const xmlChar*) "id");
      if (Attribute != NULL)
      {
        // Handle sub entries of the layout definition.
        xmlNodePtr Element = Current->children;
        GLuint DisplayList = 0;
        while (Element != NULL)
        {
          if (Element->type == XML_ELEMENT_NODE)
          {
            if (xmlStrcmp(Element->name, (const xmlChar *) "svg") == 0)
              DisplayList = ReadTemplateDefinition(Element);
              CheckError();

            // Ignore everything else.
            break;
          };
          Element = Element->next;
        };
        string Key((char*) Attribute);
        FTemplates[Key] = DisplayList;
      };
    }
    else
      if ((Current->type == XML_ELEMENT_NODE) && (xmlStrcmp(Current->name, (const xmlChar *) "font") == 0))
      {
        // Adds a new font definition to the font manager.
        ParseFontEntry(Current);
        CheckError();
      }
      else
        if ((Current->type == XML_ELEMENT_NODE) && (xmlStrcmp(Current->name, (const xmlChar *) "texture") == 0))
        {
          // Adds a new texture to the texture list.
          ParseTextureEntry(Current);
          CheckError();
        };
    Current = Current->next;
  }

  SetCurrentDir(CurrentDir);
  xmlFreeDoc(Document);

  return Result;
}

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

/**
 * Adds the given figure instance to the current selection. This call is simply forwared to the selection layer.
 * 
 * @param Instance The instance to be added to the selection. It is taken care that an instance is only added once.
 */
void CGenericCanvas::AddToSelection(CFigureInstance* Instance)
{
  FSelectionLayer->AddToSelection(Instance);
  Invalidate();
}

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

/**
 * Increases the update count by 1 to stop any recursive update until (@see EndUpdate()) was called.
 */
void CGenericCanvas::BeginUpdate(void)
{
  FUpdateCount++;
}

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

/**
 * Notifies all registered listeners that a change has occured.
 *
 * @param AObject The reference to an object for which the change event is triggered. Can be anything like a figure, figure
 *                instance, the canvas itself, a layer etc.
 * @param Reason Indicates what change actually happend.
 */
void CGenericCanvas::Change(void* AObject, TGCChangeReason Reason)
{
  for (CListenerIterator Iterator = FListeners.begin(); Iterator != FListeners.end(); Iterator++)
  {
    try
    {
      CGCListener* Listener = *Iterator;
      Listener->OnChange(AObject, Reason);
    }
    catch(...)
    {
      // If there was an exception while executing the method the listener is removed from our list
      // to avoid further harm.
      FListeners.erase(Iterator);
    };
  };
}

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

/**
 * Checks if there is an OpenGL error registered and triggers the error method if so.
 */
void CGenericCanvas::CheckError(void)
{
  GLenum OGLError = glGetError();
  if (OGLError != GL_NO_ERROR)
  {
    char Buffer[100];
    sprintf(Buffer, "OpenGL error encountered (0x%x).", OGLError);
    Error(Buffer);
  };
}

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

/**
 * Removes all currently selected figure instances from the selection set. This call is simply forwareded to the
 * selection layer.
 */
void CGenericCanvas::ClearSelection(void)
{
  FSelectionLayer->ClearSelection();
  Invalidate();
}

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

void CGenericCanvas::ClearTemplates(void)
{
  BeginUpdate();
  try
  {
    FTemplates.clear();
    EndUpdate();
  }
  catch(...)
  {
    EndUpdate();
    throw;
  };
}

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

/**
 * Creates a new layer with the given name and returns it to the caller. The new layer is added to this canvas.
 *
 * @param Name The layer identification, encoded in UTF-8.
 */
CLayer* CGenericCanvas::CreateLayer(const char* Name, TGCLayerType Type)
{
  CLayer* Layer = NULL;
  switch (Type) 
  {
    case GC_LAYER_NORMAL:
      {
        Layer = new CLayer(this);
        break;
      };
    case GC_LAYER_GRID:
      {
        Layer = new CGridLayer(this);
        break;
      };
  };

  if (Layer != NULL)
  {
    Layer->FName = Utf8ToUtf16(Name);
    AddLayer(Layer);
  };

  return Layer;
}

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

/**
 * The counterpart to (@see BeginUpdate). It releases one update lock and invalidates the canvas if the count drops to 0.
 */
void CGenericCanvas::EndUpdate(void)
{
  if (FUpdateCount > 0)
    FUpdateCount--;
  if (FUpdateCount == 0)
    Invalidate();
}

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

/**
 * @brief Notifies all registered listeners about an error that occured.
 */
void CGenericCanvas::Error(const char* Message)
{
  for (CListenerIterator Iterator = FListeners.begin(); Iterator != FListeners.end(); Iterator++)
  {
    try
    {
      CGCListener* Listener = *Iterator;
      Listener->OnError(Message);
    }
    catch(...)
    {
      // If there was an exception while executing the method the listener is removed from our list
      // to avoid further harm.
      FListeners.erase(Iterator);
    };
  };
}

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

/**
 * Takes the given coordinates and tries to find a figure that was rendered at this position.
 *
 * @param X The horizontal window (viewer) coordinate.
 * @param Y The vertical coordinate. 
 * @return A hit result class is returned regardless of the actual number of hits. It must be freed by the caller.
 * @note: Don't turn the Y value (e.g. on Windows where positive Y points down). All necessary adjustments are done implicitely.
 */
CHitResults* CGenericCanvas::GetHitTestInfoAt(const int X, const int Y)
{
  FIsPicking = true;

  GLint Viewport[4];
  glGetIntegerv(GL_VIEWPORT, Viewport);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPickMatrix(X, Y, 4, 4, Viewport);
  // The projection is set up so that the center of the scene (the origin) is located in the upper left corner of the viewport.
  glOrtho(0, Viewport[2], 0, Viewport[3], -100, 100);

  GLsizei BufferSize = 500;
  GLuint* Buffer = NULL;
  int Hits = 0;
  do
  {
    Buffer = (GLuint*) realloc(Buffer, BufferSize * sizeof(GLuint));
    glSelectBuffer(BufferSize, Buffer);

    glRenderMode(GL_SELECT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glInitNames();
    glPushName(0);

    // Render the scene (in select mode nothing is drawn).
    glTranslated(FOffsetX, FOffsetY, 0);
    glScaled(FZoomX, FZoomY, 1);
    for (CLayerIterator Iterator = FLayers.begin(); Iterator != FLayers.end(); Iterator++)
    {
      CLayer* Layer = *Iterator;
      if (Layer->FEnabled)
        Layer->Render();
    };

    Hits = glRenderMode(GL_RENDER);
    BufferSize <<= 1;
  }
  while (Hits < 0);

  CheckError();
  FIsPicking = false;

  CHitResults* Result = new CHitResults();

  GLuint* Run = Buffer;
  for (int I = 0; I < Hits; I++)
  {
    int Count = *Run++;
    // The depth values are normalized so that the largest value corresponds to 1, while the smallest one is 0.
    // To store this as integer a mapping is applied to make 1 <=> 0xFFFFFFFF.
    double ZMin = (double) *Run++ / 0xFFFFFFFF;
    double ZMax = (double) *Run++ / 0xFFFFFFFF;
    CFigureInstance* Instance = (CFigureInstance*) (*Run++);
    Result->AddHit(Instance, ZMin, ZMax);

    // Usually we have only one entry per hit record.
    // That's the way it works when we only use glLoadName when rendering figures.
    // In order to be error tolerant we skip unused entries accordingly.
    Run += Count - 1;
  };

  free(Buffer);
  return Result;
}

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

CGCModel* CGenericCanvas::GetModel(void)
{
  return FModel;
}

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

/**
 * Retrieves the value of the given property, if it is a property of this class.
 *
 * @param Property The property to retrieve.
 * @param Value [out] The value of the property.
 * @return True if the asked property is known in this class, false otherwise (in this case Value is not changed).
 * @note Implicit datatype conversion is performed if datatypes of property and Value parameter do not match.
 *       This can result in lost precision or even conversion errors. So make sure to use the right datatype for the call.
 */
bool CGenericCanvas::GetProperty(TProperty Property, double& Value)
{
  bool Result = true;
  switch (Property)
  {
    case GC_PROPERTY_HANDLE_SIZE:
      {
        break;
      };
    default:
      {
        // Property is not known here so try the special classes.
        Result = FSelectionLayer->GetProperty(Property, Value);
        break;
      };
  };

  return Result;
}

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

/**
 * Retrieves the value of the given property, if it is a property of this class.
 *
 * @param Property The property to retrieve.
 * @param Value [out] The value of the property.
 * @return True if the asked property is known in this class, false otherwise (in this case Value is not changed).
 * @note Implicit datatype conversion is performed if datatypes of property and Value parameter do not match.
 *       This can result in lost precision or even conversion errors. So make sure to use the right datatype for the call.
 */
bool CGenericCanvas::GetProperty(TProperty Property, int& Value)
{
  bool Result = true;
  switch (Property)
  {
    case GC_PROPERTY_HANDLE_SIZE:
      {
        break;
      };
    default:
      {
        // Property is not known here so try the special classes.
        Result = FSelectionLayer->GetProperty(Property, Value);
        break;
      };
  };

  return Result;
}

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

/**
 * Determines whether the given position corresponds to any of the parts (body, handles) of a selection decoration.
 * This test is quite fast and can be used for cursor feedback and such. This method is simply forwarded to
 * CSelectionLayer::GetSelectionInfo after modelview and projection matrix are constructed with the current settings.
 *
 * @param X The horizontal mouse coordinate.
 * @param Y The vertical mouse coordinate.
 * @return One of the selection info flags.
 */
TGCSelectionInfo CGenericCanvas::GetSelectionInfo(const int X, const int Y)
{
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  // The projection is set up so that the center of the scene (the origin) is located in the upper left corner of the viewport.
  glOrtho(0, FViewport.Width, 0, FViewport.Height, -100, 100);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glTranslated(FOffsetX, FOffsetY, 0);
  glScaled(FZoomX, FZoomY, 1);

  return FSelectionLayer->GetSelectionInfo(X, Y);
}

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

/**
 * Notifies all registered listeners that a change has occured and they need to invalidate their viewers.
 */
void CGenericCanvas::Invalidate(void)
{
  for (CListenerIterator Iterator = FListeners.begin(); Iterator != FListeners.end(); Iterator++)
  {
    try
    {
      CGCListener* Listener = *Iterator;
      Listener->OnInvalidate();
    }
    catch(...)
    {
      // If there was an exception while executing the method the listener is removed from our list
      // to avoid further harm.
      FListeners.erase(Iterator);
    };
  };
}

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

/**
 * Tells the selection layer to recompute the bounds of the selection decoration for the given figure instance.
 *
 * @param Instance The figure instance for which to recompute the selection decoration.
 */
void CGenericCanvas::InvalidateSelectionBounds(CFigureInstance* Instance)
{
  FSelectionLayer->InvalidateBounds(Instance);
}

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

/**
 * Tells the caller whether the canvas is currently being updated.
 */
bool CGenericCanvas::IsUpdating(void)
{
  return FUpdateCount != 0;
}

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

/**
 * Destroys this canvas instance. The release method is used by non-C++ languages access this code in order to avoid
 * releasing memory in an environment where it wasn't allocated.
 */
void CGenericCanvas::Release(void)
{
  delete this;
}

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

/**
 * Removes the given figure instance from the current selection. This call is simply forwared to the selection layer.
 *
 * @param Instance The figure instance to be removed from the selection. Nothing happens if it isn't selected.
 */
void CGenericCanvas::RemoveFromSelection(CFigureInstance* Instance)
{
  FSelectionLayer->RemoveFromSelection(Instance);
  Invalidate();
}

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

/**
 * Removes the given layer from the internal layer list. The layer itself will not be destroed, just removed.
 *
 * @param Layer The layer to be removed.
 */
void CGenericCanvas::RemoveLayer(CLayer* Layer)
{
  for (CLayerIterator Iterator = FLayers.begin(); Iterator != FLayers.end(); Iterator++)
    if (*Iterator == Layer)
    {
      FLayers.erase(Iterator);
      Layer->FCanvas = NULL;
      Invalidate();
      break;
    };
}

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

/**
 * Removes a listener from the internal list. If the listener is not in the list then this call has no effect.
 *
 * @param Listener The listener to remove.
 */
void CGenericCanvas::RemoveListener(CGCListener* Listener)
{
  for (CListenerIterator Iterator = FListeners.begin(); Iterator != FListeners.end(); Iterator++)
    if (*Iterator == Listener)
    {
      FListeners.erase(Iterator);
      break;
    };
}

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

/**
 * This is the main paint routine. It must be called by the viewer holding the reference to this canvas (e.g. when a 
 * window must be redrawn).
 */
void CGenericCanvas::Render(void)
{
  // No display if the canvas is currently being updated.
  if (FUpdateCount == 0)
  {
    ClearBuffers();
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    // The projection is set up so that the center of the scene (the origin) is located in the upper left corner of the viewport.
    glOrtho(0, FViewport.Width, 0, FViewport.Height, -100, 100);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    
    glTranslated(FOffsetX, FOffsetY, 0);
    glScaled(FZoomX, FZoomY, 1);
    for (CLayerIterator Iterator = FLayers.begin(); Iterator != FLayers.end(); Iterator++)
      (*Iterator)->Render();
    FSelectionLayer->Render();

    CheckError();
  };
}

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

void CGenericCanvas::SetBackgroundColor(COLORREF NewColor)
{
  if (NewColor != FBackgroundColor)
  {
    FBackgroundColor = NewColor;
    glClearColor((float) GetRValue(FBackgroundColor) / 255, (float) GetGValue(FBackgroundColor) / 255, 
      (float) GetBValue(FBackgroundColor) / 255, 1);
  }
}

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

/**
 * Sets the current display offsets.
 *
 * @param X The horizontal display offset.
 * @param Y The vertical display offset.
 */
void CGenericCanvas::SetOffset(double X, double Y)
{
  FOffsetX = X;
  FOffsetY = Y;
  Invalidate();
}

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

/**
 * Sets the the value of the given property, if it is a property of this class.
 *
 * @param Property The property to set.
 * @param Value The new value of the property.
 * @return True if the asked property is known in this class, false otherwise (in this case the property is not changed).
 * @note Implicit datatype conversion is performed if datatypes of property and Value parameter do not match.
 *       This can result in lost precision or even conversion errors. So make sure to use the right datatype for the call.
 */
bool CGenericCanvas::SetProperty(TProperty Property, double Value)
{
  bool Result = true;
  switch (Property)
  {
    case GC_PROPERTY_HANDLE_SIZE:
      {
        break;
      };
    default:
      {
        // Property is not known here so try the special classes.
        Result = FSelectionLayer->SetProperty(Property, Value);
        break;
      };
  };

  if (Result)
    Invalidate();

  return Result;
}

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

/**
 * Sets the the value of the given property, if it is a property of this class.
 *
 * @param Property The property to set.
 * @param Value The new value of the property.
 * @return True if the asked property is known in this class, false otherwise (in this case the property is not changed).
 * @note Implicit datatype conversion is performed if datatypes of property and Value parameter do not match.
 *       This can result in lost precision or even conversion errors. So make sure to use the right datatype for the call.
 */
bool CGenericCanvas::SetProperty(TProperty Property, int Value)
{
  bool Result = true;
  switch (Property)
  {
    case GC_PROPERTY_HANDLE_SIZE:
      {
        break;
      };
    default:
      {
        // Property is not known here so try the special classes.
        Result = FSelectionLayer->SetProperty(Property, Value);
        break;
      };
  };

  if (Result)
    Invalidate();

  return Result;
}

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

/**
 * Sets the viewport of the canvas. The viewport is the area in the viewer where the canvas may draw its content.
 * Hence the viewport is given in pixels in the viewer (window) space.
 *
 * @param NewViewport The new viewport to use.
 */
void CGenericCanvas::SetViewportV(TGCViewport* NewViewport)
{
  FViewport = *NewViewport;
  ApplyViewport();
}

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

/**
 * Sets the viewport of the canvas. The viewport is the area in the viewer where the canvas may draw its content.
 * Hence the viewport is given in pixels in the viewer (window) space.
 *
 * @param Left The left pixel position for the output area.
 * @param Top The upper pixel position for the output area.
 * @param Width The width for the output area.
 * @param Height The height for the output area.
 */
void CGenericCanvas::SetViewport(int Left, int Top, int Width, int Height)
{
  FViewport.Left = Left;
  FViewport.Top = Top;
  FViewport.Width = Width;
  FViewport.Height = Height;
  ApplyViewport();
}

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

void CGenericCanvas::SetZoom(double X, double Y)
{
  FZoomX = X;
  FZoomY = Y;
  Invalidate();
}

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

void CGenericCanvas::ShowSelection(bool Visible)
{
  FSelectionLayer->SetVisible(Visible);
}

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