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

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1995 by Borland International, All Rights Reserved
  4. //
  5. // Filename:    TBExpert.cpp
  6. //
  7. // Date:        27-Sep-95
  8. //
  9. // Description:
  10. //----------------------------------------------------------------------------
  11. #include <owl/pch.h>
  12. #include <classlib/arrays.h>
  13. #include <dir.h>
  14. #include <cstring.h>
  15. #include "TBExpert.h"
  16.  
  17. const TSize KnownToolSize(30, 20);
  18. const TSize TargetSize(20, 20);
  19. const int MaxFilesInDir = 1000;
  20.  
  21. //
  22. // TBExpertWindow constructor
  23. //
  24. TTBExpertWindow::TTBExpertWindow()
  25. :
  26.   TLayoutWindow(0, 0, 0),
  27.   DragImage(0),
  28.   KnownImageList(0),
  29.   DynamicImageList(0),
  30.   DragListWindow(0),
  31.   DragItem(0)
  32. {
  33.   Attr.Style |= WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
  34.  
  35.   KnownListWindow = new TListWindow(this, KnownListWindowId, 40, 40, 400, 200);
  36.   KnownListWindow->Attr.Style |= LVS_SHAREIMAGELISTS | LVS_ICON | LVS_AUTOARRANGE;
  37.   TLayoutMetrics lmKnownListWindow;
  38.   lmKnownListWindow.X.SameAs(lmParent, lmLeft);
  39.   lmKnownListWindow.Y.SameAs(lmParent, lmTop);
  40.   lmKnownListWindow.Width.Absolute(TEdge(lmParent), 80);
  41.   lmKnownListWindow.Height.PercentOf(lmParent, 40);
  42.   SetChildLayoutMetrics(*KnownListWindow, lmKnownListWindow);
  43.  
  44.   DynamicListWindow = new TListWindow(this, DynamicListWindowId, 40, 40, 400, 200);
  45.   DynamicListWindow->Attr.Style |= LVS_SHAREIMAGELISTS | LVS_ICON | LVS_AUTOARRANGE;
  46.   TLayoutMetrics lmDynamicListWindow;
  47.   lmDynamicListWindow.X.SameAs(lmParent, lmLeft);
  48.   lmDynamicListWindow.Y.Below(KnownListWindow, 1);
  49.   lmDynamicListWindow.Width.SameAs(KnownListWindow, lmWidth);
  50.   lmDynamicListWindow.Height.PercentOf(lmParent, 60);
  51.   SetChildLayoutMetrics(*DynamicListWindow, lmDynamicListWindow);
  52.  
  53.   GadgetManagerHolder = new TGadgetManagerPlaceHolder(this);
  54.   TLayoutMetrics lmGadgetManagerPlaceHolder;
  55.   lmGadgetManagerPlaceHolder.X.RightOf(KnownListWindow, 1);
  56.   lmGadgetManagerPlaceHolder.Y.SameAs(lmParent, lmTop);
  57.   lmGadgetManagerPlaceHolder.Width.SameAs(lmParent, lmRight);
  58.   lmGadgetManagerPlaceHolder.Height.Absolute(50);
  59.   SetChildLayoutMetrics(*GadgetManagerHolder, lmGadgetManagerPlaceHolder);
  60.  
  61.   StatusManager = new TStatusManager(this);
  62.   TLayoutMetrics lmStatusManager;
  63.   lmStatusManager.X.RightOf(DynamicListWindow, 1);
  64.   lmStatusManager.Y.Below(GadgetManagerHolder, 1);
  65.   lmStatusManager.Width.SameAs(lmParent, lmRight);
  66.   lmStatusManager.Height.SameAs(lmParent, lmBottom);
  67.   SetChildLayoutMetrics(*StatusManager, lmStatusManager);
  68. }
  69.  
  70.  
  71. //
  72. // TBExpertWindow destructor
  73. //
  74. TTBExpertWindow::~TTBExpertWindow()
  75. {
  76.   delete KnownImageList;
  77.   delete DynamicImageList;
  78. }
  79.  
  80.  
  81. //
  82. // SetupWindow
  83. //
  84. void
  85. TTBExpertWindow::SetupWindow()
  86. {
  87.   TLayoutWindow::SetupWindow();
  88.  
  89.   // put new stuff here
  90.   //
  91.   SetupKnownListWindow();
  92.   SetDynamicDir(".");
  93.   StatusManager->SetDragStatus(TStatusManager::BeforeDrag);
  94. }
  95.  
  96.  
  97. //
  98. // CleanupWindow
  99. //
  100. void
  101. TTBExpertWindow::CleanupWindow()
  102. {
  103.   // put new stuff here
  104.   //
  105.  
  106.   TLayoutWindow::CleanupWindow();
  107. }
  108.  
  109. //
  110. // Response table for TBExpertWindow
  111. //
  112. DEFINE_RESPONSE_TABLE1(TTBExpertWindow, TLayoutWindow)
  113.   EV_WM_LBUTTONUP,
  114.   EV_WM_MOUSEMOVE,
  115.   EV_LVN_BEGINDRAG(KnownListWindowId,   LvnKnownBeginDrag),
  116.   EV_LVN_BEGINDRAG(DynamicListWindowId, LvnDynamicBeginDrag),
  117.   EV_MESSAGE(WUM_DONEMODIFYINGGADGET, EvDoneModifyingGadget),
  118. END_RESPONSE_TABLE;
  119.  
  120. void
  121. TTBExpertWindow::SetupKnownListWindow()
  122. {
  123.   struct {
  124.     char* Name;
  125.     int   Id;
  126.   } knownGadgets[] = {
  127.     { "Separator", IDB_SEPARATOR },
  128.     { "Mode",      IDB_MODE },
  129.     { "Time",      IDB_TIME },
  130.     { "Text",      IDB_TEXT },
  131.   };
  132.  
  133.   if (DynamicListWindow && DynamicListWindow->IsWindow()) {
  134.     KnownListWindow->DeleteAllItems();
  135.     delete KnownImageList;
  136.     KnownImageList = new TImageList(KnownToolSize, ILC_COLOR4, 15, 5);
  137.  
  138.     for (int i = 0; i < sizeof knownGadgets/sizeof knownGadgets[0]; i++)
  139.       KnownImageList->Add(TBitmap(*GetApplication(), knownGadgets[i].Id));
  140.  
  141.     KnownListWindow->SetImageList(*KnownImageList, TListWindow::Normal);
  142.  
  143.     for (int i = 0; i < sizeof knownGadgets/sizeof knownGadgets[0]; i++) {
  144.       TListWindItem item(knownGadgets[i].Name);
  145.       item.SetImageIndex(i);
  146.       KnownListWindow->InsertItem(item);
  147.     }
  148.   }
  149. }
  150.  
  151. void
  152. TTBExpertWindow::SetDynamicDir(const char* dir)
  153. {
  154.   if (DynamicListWindow && DynamicListWindow->IsWindow()) {
  155.     DynamicListWindow->DeleteAllItems();
  156.     delete DynamicImageList;
  157.  
  158.     DynamicImageList = new TImageList(TargetSize, ILC_COLOR4, 15, 5);
  159.  
  160.     TSArrayAsVector<string> files(MaxFilesInDir);
  161.  
  162.     // read .bmp file names into a sorted array
  163.     //
  164.     strcpy(CurrentDir, dir);
  165.     chdir(dir);
  166.     ffblk ff;
  167.     bool done = findfirst("*.bmp", &ff, 0);
  168.     while (!done) {
  169.       TDib dib(ff.ff_name);
  170.       if (dib.Size() == TargetSize)
  171.         files.Add(string(ff.ff_name));
  172.       done = findnext(&ff);
  173.     }
  174.  
  175.     // add each .bmp into the image list
  176.     //
  177.     TSArrayAsVectorIterator<string> iter(files);
  178.     while (iter) {
  179.       string f = iter.Current();
  180.       DynamicImageList->Add(TBitmap(TDib(f.c_str())));
  181.       iter++;
  182.     }
  183.  
  184.     // set the image list
  185.     //
  186.     DynamicListWindow->SetImageList(*DynamicImageList, TListWindow::Normal);
  187.  
  188.     // add the items into the list window
  189.     //
  190.     iter.Restart();
  191.     int index = 0;
  192.     static char buffer[MAXFILE];
  193.     while (iter) {
  194.       string f = iter.Current();
  195.       strcpy(buffer, f.c_str());
  196.       strlwr(buffer);
  197.       TListWindItem item(buffer);
  198.       item.SetImageIndex(index++);
  199.       DynamicListWindow->InsertItem(item);
  200.       iter++;
  201.     }
  202.   }
  203. }
  204.  
  205.  
  206. void
  207. TTBExpertWindow::EvLButtonUp(uint modKeys, TPoint& point)
  208. {
  209.   TWindow::EvLButtonUp(modKeys, point);
  210.   if (DragImage != 0) {
  211.     StatusManager->SetDragStatus(TStatusManager::EndDrag);
  212.     ReleaseCapture();
  213.     TImageList::DragLeave(*this);
  214.     TImageList::EndDrag();
  215.     delete DragImage;
  216.     DragImage = 0;
  217.     ShowCursor(true);
  218.  
  219.     TPoint curPos;
  220.     GetCursorPos(curPos);
  221.     TRect GadgetManagerRect = GadgetManagerHolder->GetWindowRect();
  222.     if (GadgetManagerRect.Contains(curPos)) {
  223.       char buffer[MAXFILE];
  224.       TListWindItem item(buffer, 0, MAXFILE);
  225.       item.SetIndex(DragItem);
  226.       DragListWindow->GetItem(item);
  227.       GadgetDropped(DragListWindow, buffer, DragItem);
  228.     }
  229.     DragListWindow = 0;
  230.     DragItem = 0;
  231.     StatusManager->SetDragStatus(TStatusManager::BeforeDrag);
  232.   }
  233. }
  234.  
  235. void
  236. TTBExpertWindow::GadgetDropped(TListWindow* window, char* gadgetName, int /*item*/)
  237. {
  238.   if (CountOfGadgetFactories >= MaxGadgetFactories) {
  239.     MessageBox("Too many gadgets", "Error");
  240.     return;
  241.   }
  242.  
  243.   TGadgetFactory* newFactory = 0;
  244.   if (window->GetId() == KnownListWindowId) {
  245.     if (strcmpi(gadgetName, "Separator") == 0) {
  246.       newFactory = new TGadgetFactory(0, TGadget::None, 6);
  247.     }
  248.     else if (strcmpi(gadgetName, "Mode") == 0) {
  249.       newFactory = new TGadgetFactory(0, TGadget::Recessed, VK_CONTROL, "Ctrl");
  250.     }
  251.     else if (strcmpi(gadgetName, "Time") == 0) {
  252.       newFactory = new TGadgetFactory(0, TGadget::Recessed);
  253.     }
  254.     else if (strcmpi(gadgetName, "Text") == 0) {
  255.       newFactory = new TGadgetFactory(0, TGadget::Recessed, TTextGadget::Left, 6, "Text");
  256.     }
  257.   }
  258.   else {
  259.     char buffer[MAXPATH];
  260.     strcpy(buffer, CurrentDir);
  261.     strcat(buffer, "\\");
  262.     strcat(buffer, gadgetName);
  263.     newFactory = new TGadgetFactory(0, TGadget::None, TButtonGadget::Command,
  264.                                    TButtonGadget::Up, buffer);
  265.   }
  266.  
  267.   if (newFactory != 0) {
  268.     GadgetFactories[CountOfGadgetFactories] = newFactory;
  269.     CountOfGadgetFactories++;
  270.   }
  271.   // Remove old gadget manager and create a new one
  272.   // Rely on the creation of the gadget manager to simulate adding
  273.   // gadgets
  274.   //
  275.   GadgetManagerHolder->CreateGadgetManager();
  276. }
  277.  
  278.  
  279. TResult
  280. TTBExpertWindow::EvDoneModifyingGadget(TParam1, TParam2)
  281. {
  282.   GadgetManagerHolder->CreateGadgetManager();
  283. //  StatusManager->SetDragStatus(TStatusManager::BeforeDrag);
  284.   return 0;
  285. }
  286.  
  287.  
  288. void
  289. TTBExpertWindow::EvMouseMove(uint /*modKeys*/, TPoint& point)
  290. {
  291.   if (DragImage != 0) {
  292.     StatusManager->SetDragStatus(TStatusManager::Dragging);
  293.     TImageList::DragMove(point.x + DragOffset.left, point.y + DragOffset.top);
  294.   }
  295. }
  296.  
  297. void
  298. TTBExpertWindow::LvnKnownBeginDrag(TLwNotify& notify)
  299. {
  300.   StatusManager->SetDragStatus(TStatusManager::BeginDrag);
  301.   LvnBeginDrag(KnownListWindow, notify);
  302. }
  303.  
  304. void
  305. TTBExpertWindow::LvnDynamicBeginDrag(TLwNotify& notify)
  306. {
  307.   LvnBeginDrag(DynamicListWindow, notify);
  308. }
  309.  
  310. void
  311. TTBExpertWindow::LvnBeginDrag(TListWindow* listWind, TLwNotify& notify)
  312. {
  313.   DragListWindow = listWind;
  314.   DragItem       = notify.iItem;
  315.   TPoint point;
  316.   DragImage = new TImageList(listWind->CreateDragImage(DragItem, &point));
  317.  
  318.   listWind->GetItemRect(notify.iItem, &DragOffset, TListWindow::Bounds);
  319.   DragImage->BeginDrag(0, point.x, point.y);
  320.   SetCapture();
  321.   ShowCursor(false);
  322.   TImageList::DragEnter(*this, point.x, point.y);
  323. }
  324.