HandleTable Class Reference

Provide a mirror of the table of handles maintained by GDI when importing a MetaFile. This is necessary because, given a GDI handle, it is not possible to ask GDI what kind of object it is, and hence work out e.g. what kind of pen/brush it is. This class therefore creates, selects, and deletes handle objects in the same way that GDI would (see EnumMetafile help in SDK documentation for further details...possibly). At the moment, this HandleTable only supports pens and brushes. More...

List of all members.

Public Member Functions

 HandleTable (MetaFileFilter *)
 Initialises the HandleTable. The table initially contains no entries.
 ~HandleTable ()
 Deinitialises a handle table; deallocates the handle table.
BOOL CreatePen (LOGPEN_16 *)
 Create a handle for the specified pen in the HandleTable, and store the pen information in this.
BOOL CreateBrush (LOGBRUSH_16 *)
 Create a handle for the specified brush in the HandleTable, and store the brush information in this.
BOOL CreatePalette (LOGPALETTE_16 *)
 Creates a Palette object ready for use.
BOOL CreatePatternBrush (WORD *)
 Creates a PatternBrush object ready for use.
BOOL CreateFontIndirect (LOGFONT_16 *)
 Creates a Font object ready for use.
BOOL CreateRegion (WORD *)
 Creates a Region object ready for use.
BOOL SelectObject (INT32 Index)
 Mimic the selection of a GDI object. This function will update the relevant status variables in the associated MetaFileFilter object.
BOOL DeleteObject (INT32 Index)
 Deletes a handle from the Handle table. The index of this handle should no longer be used (unless, of course, it is reallocated to a new handle).

Private Member Functions

INT32 FindFreeSlot ()
 Find the index of the first free slot available in the handle table. If no free handles are found, the table is expanded and the index of the first new slot is returned. If it is not possible to expand the table (out of memerory) then BAD_SLOT is returned.

Private Attributes

MetaFileFilterContext
HandleRecordHandles
INT32 TableSize


Detailed Description

Provide a mirror of the table of handles maintained by GDI when importing a MetaFile. This is necessary because, given a GDI handle, it is not possible to ask GDI what kind of object it is, and hence work out e.g. what kind of pen/brush it is. This class therefore creates, selects, and deletes handle objects in the same way that GDI would (see EnumMetafile help in SDK documentation for further details...possibly). At the moment, this HandleTable only supports pens and brushes.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
28/03/94
See also:
HandleRecord; MetaFileFilter

Definition at line 260 of file metafilt.cpp.


Constructor & Destructor Documentation

HandleTable::HandleTable MetaFileFilter pFilter  ) 
 

Initialises the HandleTable. The table initially contains no entries.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
28/03/94
See also:
HandleTable

Definition at line 302 of file metafilt.cpp.

00303 {
00304     Handles = NULL;
00305     TableSize = 0;
00306     Context = pFilter;
00307 }

HandleTable::~HandleTable  ) 
 

Deinitialises a handle table; deallocates the handle table.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
28/03/94
See also:
HandleTable

Definition at line 320 of file metafilt.cpp.

00321 {
00322     CCFree(Handles);
00323     Handles = NULL;
00324     TableSize = 0;
00325 }


Member Function Documentation

BOOL HandleTable::CreateBrush LOGBRUSH_16 *  pBrush  ) 
 

Create a handle for the specified brush in the HandleTable, and store the brush information in this.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
28/03/94
Parameters:
pBrush - the logical brush information for this brush. [INPUTS] (See LOGBRUSH in Win16 SDK Help)
Returns:
TRUE if handle was created ok, FALSE if not.

Errors: Out of memory.

See also:
HandleTable::CreatePen

Definition at line 477 of file metafilt.cpp.

00478 {
00479     INT32 Style = pBrush->lbStyle;
00480     COLORREF Col = (COLORREF) pBrush->lbColor;
00481 
00482     // Get a new slot to put this pen in
00483     INT32 Slot = FindFreeSlot();
00484 
00485     if (Slot == BAD_SLOT)
00486         // Could not create this pen
00487         return FALSE;
00488 
00489     Handles[Slot].Type = HANDLE_BRUSH;
00490 
00491     switch (Style)
00492     {
00493         case BS_SOLID:
00494             Handles[Slot].Colour = DocColour(GetRValue(Col), GetGValue(Col), GetBValue(Col));
00495             break;
00496 
00497         case BS_NULL:
00498             Handles[Slot].Colour = DocColour(COLOUR_TRANS);
00499             break;
00500 
00501         default:
00502             ENSURE(FALSE, "Unknown brush style in metafile!");
00503             ERROR(_R(IDT_BAD_METAFILE), FALSE);
00504     }
00505 
00506     // All ok
00507     return TRUE;
00508 }

BOOL HandleTable::CreateFontIndirect LOGFONT_16 *  pNewFont  ) 
 

Creates a Font object ready for use.

Author:
Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
Date:
18/5/95
Parameters:
pNewFont - the font we are creating [INPUTS]
Returns:
TRUE if it worked, FALSE if there was a problem

Definition at line 576 of file metafilt.cpp.

00577 {
00578     // Get a new slot to put this pen in
00579     INT32 Slot = FindFreeSlot();
00580 
00581     // Could not create this Palette
00582     if (Slot == BAD_SLOT)
00583         return FALSE;
00584 
00585     // Say what this is
00586     Handles[Slot].Type = HANDLE_FONTINDIRECT;
00587     Handles[Slot].pFont = pNewFont;
00588 
00589     // return Happy
00590     return TRUE;
00591 }

BOOL HandleTable::CreatePalette LOGPALETTE_16 *   ) 
 

Creates a Palette object ready for use.

Author:
Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
Date:
18/5/95
Returns:
TRUE if it worked, FALSE if there was a problem

Definition at line 521 of file metafilt.cpp.

00522 {
00523     // Get a new slot to put this pen in
00524     INT32 Slot = FindFreeSlot();
00525 
00526     // Could not create this Palette
00527     if (Slot == BAD_SLOT)
00528         return FALSE;
00529 
00530     // Say what this is
00531     Handles[Slot].Type = HANDLE_PALETTE;
00532 
00533     // return Happy
00534     return TRUE;
00535 }

BOOL HandleTable::CreatePatternBrush WORD  ) 
 

Creates a PatternBrush object ready for use.

Author:
Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
Date:
18/5/95
Returns:
TRUE if it worked, FALSE if there was a problem

Definition at line 548 of file metafilt.cpp.

00549 {
00550     // Get a new slot to put this pen in
00551     INT32 Slot = FindFreeSlot();
00552 
00553     // Could not create this Palette
00554     if (Slot == BAD_SLOT)
00555         return FALSE;
00556 
00557     // Say what this is
00558     Handles[Slot].Type = HANDLE_PATTERNBRUSH;
00559 
00560     // return Happy
00561     return TRUE;
00562 }

BOOL HandleTable::CreatePen LOGPEN_16 *  pPen  ) 
 

Create a handle for the specified pen in the HandleTable, and store the pen information in this.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
28/03/94
Parameters:
pPen - the logical pen information for this pen. [INPUTS] (See LOGPEN in Win16 SDK Help)
Returns:
TRUE if handle was created ok, FALSE if not.

Errors: Out of memory.

See also:
HandleTable::CreateBrush

Definition at line 422 of file metafilt.cpp.

00423 {
00424     INT32 Style = pPen->lopnStyle;
00425     COLORREF Col = (COLORREF) pPen->lopnColor;
00426 
00427     // Get a new slot to put this pen in
00428     INT32 Slot = FindFreeSlot();
00429 
00430     if (Slot == BAD_SLOT)
00431         // Could not create this pen
00432         return FALSE;
00433 
00434     Handles[Slot].Type = HANDLE_PEN;
00435 
00436     switch (Style)
00437     {
00438         case PS_SOLID:
00439         case PS_INSIDEFRAME:
00440             Handles[Slot].Colour = DocColour(GetRValue(Col), GetGValue(Col), GetBValue(Col));
00441             break;
00442 
00443         case PS_NULL:
00444             Handles[Slot].Colour = DocColour(COLOUR_TRANS);
00445             break;
00446 
00447         default:
00448             ENSURE(FALSE, "Unknown pen style in metafile!");
00449             ERROR(_R(IDT_BAD_METAFILE), FALSE);
00450     }
00451 
00452     // Scale pen width to document coordinates.
00453     DocCoord PenSize(pPen->lopnWidth.x, 0);
00454     Context->ScaleCoord(&PenSize);
00455     Handles[Slot].PenWidth = PenSize.x;
00456 
00457     // All ok
00458     return TRUE;
00459 }

BOOL HandleTable::CreateRegion WORD  ) 
 

Creates a Region object ready for use.

Author:
Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
Date:
18/5/95
Returns:
TRUE if it worked, FALSE if there was a problem

Definition at line 604 of file metafilt.cpp.

00605 {
00606     // Get a new slot to put this pen in
00607     INT32 Slot = FindFreeSlot();
00608 
00609     // Could not create this Palette
00610     if (Slot == BAD_SLOT)
00611         return FALSE;
00612 
00613     // Say what this is
00614     Handles[Slot].Type = HANDLE_REGION;
00615 
00616     // return Happy
00617     return TRUE;
00618 }

BOOL HandleTable::DeleteObject INT32  Index  ) 
 

Deletes a handle from the Handle table. The index of this handle should no longer be used (unless, of course, it is reallocated to a new handle).

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
28/03/94
Parameters:
Index - index of the handle object to delete from the table/ [INPUTS]
Returns:
TRUE if handle deleted ok; FALSE if not.

Errors: Bad index handle (i.e. specified handle does not exist)

See also:
HandleTable::SelectObject

Definition at line 701 of file metafilt.cpp.

00702 {
00703     // Sanity check
00704     if ((Index < 0) || (Index >= TableSize))
00705     {
00706         // Out of range index
00707         if (IsUserName("Rik"))
00708             TRACE( _T("Tried to delete out of range object\n"));
00709         return FALSE;
00710     }
00711 
00712     // Try to select the object.
00713     switch (Handles[Index].Type)
00714     {
00715         case HANDLE_NONE:
00716             // No such object
00717             if (IsUserName("Rik"))
00718                 TRACE( _T("Tried to delete non-existent object\n"));
00719             return FALSE;
00720 
00721         case HANDLE_PEN:
00722         case HANDLE_BRUSH:
00723         case HANDLE_PALETTE:
00724         case HANDLE_PATTERNBRUSH:
00725         case HANDLE_FONTINDIRECT:
00726         case HANDLE_REGION:
00727             Handles[Index].Type = HANDLE_NONE;
00728             break;
00729 
00730         default:
00731             ENSURE(FALSE, "Bad metafile handle type!");
00732             ERROR(_R(IDT_BAD_METAFILE), FALSE);
00733     }
00734 
00735     // To get here we must have deleted the object ok
00736     return TRUE;
00737 }

INT32 HandleTable::FindFreeSlot  )  [private]
 

Find the index of the first free slot available in the handle table. If no free handles are found, the table is expanded and the index of the first new slot is returned. If it is not possible to expand the table (out of memerory) then BAD_SLOT is returned.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
28/03/94
Returns:
Index of first free slot in the handle table, or BAD_SLOT if there is no free slot.

Errors: Out of memory.

See also:
HandleTable

Definition at line 354 of file metafilt.cpp.

00355 {
00356     INT32 Slot;
00357 
00358     // Search the existing table for blanks
00359     for (Slot = 0; Slot < TableSize; Slot++)
00360     {
00361         if (Handles[Slot].Type == HANDLE_NONE)
00362             return Slot;
00363     }
00364 
00365     // Didn't find a free slot - get some more memory and return the first slot we get.
00366 
00367     INT32 NewTableSize = TableSize + TableGranularity;
00368 
00369     // If no slots yet, then allocate a table, otherwise expand the existing table
00370     HandleRecord *NewHandles;
00371     if (Handles == NULL)
00372         NewHandles = (HandleRecord *) CCMalloc(NewTableSize * sizeof(HandleRecord));
00373     else
00374         NewHandles = (HandleRecord *) CCRealloc(Handles, NewTableSize * sizeof(HandleRecord));
00375 
00376     // Did that work?
00377     if (NewHandles == NULL)
00378     {
00379         // No more memory available, but error state set by ccmalloc
00380         return BAD_SLOT;
00381     }
00382 
00383     // Initialise the new slots
00384     for (Slot = TableSize; Slot < NewTableSize; Slot++)
00385     {
00386         NewHandles[Slot].Type = HANDLE_NONE;
00387 
00388         // This bizarre syntax of 'new' allows us to call the constructor for an object
00389         // that has already been allocated.  We need to do this otherwise all the 
00390         // DocColour objects in the handle table are not initialised.
00391         // The constructors are not called because we just CCMalloc() the block of memory,
00392         // because we want to be able to resize the block (using CCRealloc).
00393         new(&NewHandles[Slot].Colour) DocColour;
00394     }
00395 
00396     // Remember the first slot
00397     Slot = TableSize;
00398 
00399     // Update handle table variables
00400     Handles   = NewHandles;
00401     TableSize = NewTableSize;
00402 
00403     // Return the first new slot
00404     return Slot;
00405 }

BOOL HandleTable::SelectObject INT32  Index  ) 
 

Mimic the selection of a GDI object. This function will update the relevant status variables in the associated MetaFileFilter object.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
28/03/94
Parameters:
Index - the index of the handle to select. [INPUTS]
Returns:
TRUE if the handle was selected ok; FALSE if not.

Errors: Bad index handle (i.e. specified handle does not exist)

See also:
HandleTable::DeleteObject

Definition at line 635 of file metafilt.cpp.

00636 {
00637     // Sanity check
00638     if ((Index < 0) || (Index >= TableSize))
00639     {
00640         // Out of range index
00641         if (IsUserName("Rik"))
00642             TRACE( _T("Tried to select out of range object\n"));
00643         ERROR(_R(IDT_BAD_METAFILE), FALSE);
00644     }
00645 
00646     // Try to select the object.
00647     switch (Handles[Index].Type)
00648     {
00649         case HANDLE_NONE:
00650             // No such object
00651             if (IsUserName("Rik"))
00652                 TRACE( _T("Tried to select non-existent object\n"));
00653             return FALSE;
00654 
00655         case HANDLE_PEN:
00656             Context->SetLineColour(Handles[Index].Colour);
00657             Context->SetLineWidth(Handles[Index].PenWidth);
00658             break;
00659 
00660         case HANDLE_BRUSH:
00661             Context->SetFillColour(Handles[Index].Colour);
00662             break;
00663 
00664         case HANDLE_PALETTE:
00665             break;
00666 
00667         case HANDLE_PATTERNBRUSH:
00668             break;
00669 
00670         case HANDLE_FONTINDIRECT:
00671             Context->SetLogicalFont(Handles[Index].pFont);
00672             break;
00673     
00674         case HANDLE_REGION:
00675             break;
00676 
00677         default:
00678             ENSURE(FALSE, "Bad metafile handle type!");
00679             ERROR(_R(IDT_BAD_METAFILE), FALSE);
00680     }
00681 
00682     // To get here we must have selected the object ok
00683     return TRUE;
00684 }


Member Data Documentation

MetaFileFilter* HandleTable::Context [private]
 

Definition at line 282 of file metafilt.cpp.

HandleRecord* HandleTable::Handles [private]
 

Definition at line 285 of file metafilt.cpp.

INT32 HandleTable::TableSize [private]
 

Definition at line 288 of file metafilt.cpp.


The documentation for this class was generated from the following file:
Generated on Sat Nov 10 03:55:06 2007 for Camelot by  doxygen 1.4.4