home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / OWLSRC.PAK / GADGETLI.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  4.0 KB  |  192 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.5  $
  6. //
  7. // Implementation of class TGadgetList, the base for classes that own gadgets
  8. //----------------------------------------------------------------------------
  9. #include <owl/pch.h>
  10. #if !defined(OWL_GADGET_H)
  11. # include <owl/gadget.h>
  12. #endif
  13. #if !defined(OWL_GADGETWI_H)
  14. # include <owl/gadgetwi.h>
  15. #endif
  16.  
  17. OWL_DIAGINFO;
  18.  
  19. //
  20. // Return the gadget that a given window-relative point is in, 0 if none found
  21. //
  22. TGadget*
  23. TGadgetList::GadgetFromPoint(TPoint& point) const
  24. {
  25.   TGadget* gadget = Gadgets;
  26.   for (; gadget; gadget = gadget->Next)
  27.     if (gadget->PtIn(point - *(TSize*)&gadget->Bounds.TopLeft()))
  28.       break;
  29.  
  30.   return gadget;
  31. }
  32.  
  33. //
  34. // Return the gadget with a given Id, 0 if none found.
  35. //
  36. TGadget*
  37. TGadgetList::GadgetWithId(int id) const
  38. {
  39.   for (TGadget* g = Gadgets; g; g = g->NextGadget())
  40.     if (g->GetId() == id)
  41.       return g;
  42.   return 0;
  43. }
  44.  
  45. //
  46. // Return gadget at a given index.
  47. //
  48. TGadget*
  49. TGadgetList::operator [](uint index)
  50. {
  51.   PRECONDITION(index < NumGadgets);
  52.  
  53.   TGadget* g;
  54.   for (g = FirstGadget(); index > 0; index--)
  55.     g = NextGadget(*g);
  56.   return g;
  57. }
  58.  
  59. //
  60. // Insert a Gadget. You can specify a sibling Gadget that the new Gadget
  61. // is to be inserted before or after
  62. //
  63. // If "sibling" is 0 then the new Gadget is inserted at the beginning or
  64. // the end. The default is to insert the new Gadget at the end
  65. //
  66. void
  67. TGadgetList::Insert(TGadget& gadget, TPlacement placement, TGadget* sibling)
  68. {
  69.   PRECONDITION(!gadget.Window);  // Would cause problems if still in a window
  70.  
  71.   TGadget**  g = &Gadgets;
  72.  
  73.   // Locate spot to insert gadget
  74.   //
  75.   if (sibling || placement == After) {
  76.     while (*g && *g != sibling)
  77.       g = &(*g)->Next;
  78.  
  79.     CHECK(*g == sibling);
  80.   }
  81.   if (placement == After && *g)
  82.     g = &(*g)->Next;
  83.  
  84.   // Link supplied gadget into this list
  85.   //
  86.   gadget.Next = *g;
  87.   *g = &gadget;
  88.  
  89.   // Update count
  90.   //
  91.   NumGadgets++;
  92.  
  93.   Inserted(gadget);
  94. }
  95.  
  96. //
  97. // Insert a list of Gadget. Gadgets are removed from the source list
  98. //
  99. void
  100. TGadgetList::InsertFrom(TGadgetList& list, TPlacement placement, TGadget* sibling)
  101. {
  102.   if (!list.Gadgets)
  103.     return;
  104.  
  105.   TGadget**  g = &Gadgets;
  106.  
  107.   // Locate spot to insert gadgets
  108.   //
  109.   if (sibling || placement == After) {
  110.     while (*g && *g != sibling)
  111.       g = &(*g)->Next;
  112.  
  113.     CHECK(*g == sibling);
  114.   }
  115.   if (placement == After && *g)
  116.     g = &(*g)->Next;
  117.  
  118.   // Find tail of list. Let the source list & this list know about the transfer
  119.   // as we go.
  120.   //
  121.   TGadget* last;
  122.   for (last = list.Gadgets; last->Next; last++) {
  123.     list.Removed(*last);
  124.     Inserted(*last);
  125.   }
  126.  
  127.   // Link supplied list into this list
  128.   //
  129.   last->Next = *g;
  130.   *g = list.Gadgets;
  131.  
  132.   // Update counts & source list pointer
  133.   //
  134.   NumGadgets += list.NumGadgets;
  135.   list.Gadgets = 0;
  136.   list.NumGadgets = 0;
  137. }
  138.  
  139. //
  140. // Removes (unlinks) a gadget from this gadget window. The gadget is
  141. // returned but not destroyed. Returns 0 if gadget is not in this window
  142. //
  143. TGadget*
  144. TGadgetList::Remove(TGadget& gadget)
  145. {
  146.   if (!Gadgets)
  147.     return 0;
  148.  
  149.   // Handle head-of-list case
  150.   //
  151.   if (&gadget == Gadgets) {
  152.     Gadgets = Gadgets->Next;
  153.   }
  154.   // Scan for gadget, looking one link ahead
  155.   //
  156.   else {
  157.     TGadget* g = Gadgets;
  158.  
  159.     while (g->Next && g->Next != &gadget)
  160.       g = g->Next;
  161.  
  162.     if (!g->Next)   // not found
  163.       return 0;
  164.  
  165.     g->Next = g->Next->Next;
  166.   }
  167.  
  168.   NumGadgets--;
  169.  
  170.   Removed(gadget);
  171.  
  172.   return &gadget;
  173. }
  174.  
  175. //
  176. // A gadget has been inserted. Derived class can override this to update
  177. // internals
  178. //
  179. void
  180. TGadgetList::Inserted(TGadget& /*gadget*/)
  181. {
  182. }
  183.  
  184. //
  185. // A gadget has been removed. Derived class can override this to update
  186. // internals
  187. //
  188. void
  189. TGadgetList::Removed(TGadget& /*gadget*/)
  190. {
  191. }
  192.