home *** CD-ROM | disk | FTP | other *** search
/ HTBasic 9.3 / HTBasic 9.3.iso / 93win / data1.cab / DLL_Toolkit / Source / HTBListBox / Listbox.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-03-02  |  15.4 KB  |  696 lines

  1. //    Listbox.cpp : Defines the initialization routines for the DLL and the exported functions
  2. //        High Tech BASIC, Copyright (C) TransEra Corp 1999, All Rights Reserved.
  3. //
  4. /*************************************************************************
  5.  *                                                                       *
  6.  *  ListBox DLL (Win32 / MFC)                                            *
  7.  *  Author:      Sven Henze, Tech Soft GmbH                              *
  8.  *  Date:        03-Sep-1999                                             *
  9.  *  Last Update: 28-Sep-1999                                             *
  10.  *                                                                       *
  11.  *  This code is intended for the usage under BASIC for Windows V8.X     *
  12.  *                                                                       *
  13.  *  Version info:                                                        *
  14.  *     03-Sep-1999  V1.0▀ Initial release of DLL                         *
  15.  *     27-Sep-1999  Getselitems added                                    *
  16.  *     14-Oct-1999  Bug repaired (new/edit in single selection style)    *
  17.  *                                                                       *
  18.  *************************************************************************/
  19.  
  20.  
  21. #include "stdafx.h"
  22. #include "Listbox.h"
  23. #include "ListboxDlg.h"
  24. #include "DialogThread.h"
  25. #include "assert.h"
  26.  
  27. #ifdef _DEBUG
  28. #define new DEBUG_NEW
  29. #undef THIS_FILE
  30. static char THIS_FILE[] = __FILE__;
  31. #endif
  32.  
  33.  
  34. //
  35. //    Note!
  36. //
  37. //        If this DLL is dynamically linked against the MFC
  38. //        DLLs, any functions exported from this DLL which
  39. //        call into MFC must have the AFX_MANAGE_STATE macro
  40. //        added at the very beginning of the function.
  41. //
  42. //        For example:
  43. //
  44. //        extern "C" BOOL PASCAL EXPORT ExportedFunction()
  45. //        {
  46. //            AFX_MANAGE_STATE(AfxGetStaticModuleState());
  47. //            // normal function body here
  48. //        }
  49. //
  50. //        It is very important that this macro appear in each
  51. //        function, prior to any calls into MFC.  This means that
  52. //        it must appear as the first statement within the 
  53. //        function, even before any object variable declarations
  54. //        as their constructors may generate calls into the MFC
  55. //        DLL.
  56. //
  57. //        Please see MFC Technical Notes 33 and 58 for additional
  58. //        details.
  59. //
  60.  
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CListBoxApp
  63.  
  64. BEGIN_MESSAGE_MAP(CListBoxApp, CWinApp)
  65.     //{{AFX_MSG_MAP(CListBoxApp)
  66.         // NOTE - the ClassWizard will add and remove mapping macros here.
  67.         //    DO NOT EDIT what you see in these blocks of generated code!
  68.     //}}AFX_MSG_MAP
  69. END_MESSAGE_MAP()
  70.  
  71. /////////////////////////////////////////////////////////////////////////////
  72. // CListBoxApp construction
  73.  
  74. CListBoxApp::CListBoxApp()
  75. {
  76.     // TODO: add construction code here,
  77.     // Place all significant initialization in InitInstance
  78. }
  79.  
  80. /////////////////////////////////////////////////////////////////////////////
  81. // The one and only CListBoxApp object
  82.  
  83. CListBoxApp theApp;
  84.  
  85.  
  86. //****************************************************************************************************
  87. // begin global variables -- used to communicate between threads and different instances of classes
  88.  
  89.  
  90. ListboxDlg *g_plistDlg = NULL;            // global pointer to button dialog used for setting focus and closing
  91. CWnd *g_pWnd;                            // global pointer to Dialog
  92. CWinThread *pThread;
  93.  
  94. long g_Width;                // width of the dialog box
  95. long g_Height;                // the height of the window
  96. short g_ButtAppear;
  97. CString g_Title;            // Window title
  98. CString    g_Description;        // Text above buttons will wrap to two lines if too long
  99. CString g_Lstrings;
  100. CListBox *g_pList;            // global pointer to listbox control
  101.  
  102. //    end global variables
  103. //***************************************************************************************************
  104. //  begin global functions not exported
  105.  
  106. // end global functions not exported
  107. //*****************************************************************************************************
  108. // begin exported functions
  109.  
  110.  
  111. // Showlistbox is the main function for setting up and displaying the ListBox dialog box.  
  112. //    Params:
  113. //        height is the desired height of the dialog window
  114. //        width is the desired width of the dialog window and buttons
  115. //      appearance is a variable which controls the apearance of the dialog
  116. //        title is the text for the window title
  117. //        description is the text for the group frame
  118.  
  119. short Showlistbox(long height,long width,short appearance, char * title,char * description)
  120. {    AFX_MANAGE_STATE(AfxGetStaticModuleState());
  121.  
  122.     short result = 0;
  123.  
  124.     if (g_plistDlg == NULL)                        // Button dialog already active
  125.     {    
  126.         if (height < MINHEIGHT || height > MAXHEIGHT)
  127.         {
  128.             g_Height = MAXHEIGHT;                    // default to MAXLINES lines for bad input
  129.         }
  130.         else
  131.         {
  132.             g_Height = height;                        // save current number of visible lines
  133.         }
  134.  
  135.         g_Width = width;                            // save width
  136.         g_ButtAppear = appearance;
  137.  
  138.         g_Title = title;
  139.         g_Description = description;
  140.  
  141.     
  142.         pThread = AfxBeginThread(RUNTIME_CLASS (DialogThread));    // create thread to execute dialog
  143.         Sleep(50);                     // wait until the thread is ready
  144.         result = 1;
  145.  
  146.     }
  147.  
  148.     return(result);
  149. }
  150.  
  151.  
  152. // end Showbutton
  153. //*****************************************************************************************
  154. // Setfocus -- used to bring the ListBox dialog box to the foreground and activate.
  155.  
  156.  
  157. void Setfocus()
  158. {    
  159.     SetForegroundWindow(g_plistDlg->m_hWnd);        // bring window to foreground and activate
  160. }
  161.  
  162.  
  163. // end Setfocus
  164. //*****************************************************************************************
  165. // Closebutton -- Used to close ListBox dialog box when running in its own thread.
  166.  
  167.  
  168. void Closelistbox()
  169. {
  170.     if (g_plistDlg != NULL)
  171.     {    
  172.         // now sets the return value !
  173.         
  174.         g_plistDlg->KillTimer(1);                // stop timer from updating dialog
  175.         g_plistDlg->EndDialog(0);                // close dialog and allow spawned thread to run out
  176.         g_plistDlg = NULL;                        // reset pointer to dialog
  177.         //Sleep(0);                                // This line allows the thread to shut down before Basic regains control
  178.     }
  179. }
  180.  
  181.  
  182. // end CloseListBox
  183. //*****************************************************************************************
  184. /////////////////////////////////////////////////////////////////////////////
  185.  
  186.  
  187. /******** Exported functions ***********/
  188.  
  189.  
  190. // **********  Addstring **********
  191. // Add a string to the ListBox (includes sorting)
  192. //
  193. // HTB Usage:
  194. //             DLL GET "VOID Listbox:Addstring"
  195. //             DIM A$[128]
  196. //             A$="Hello"
  197. //             Addstring("Teststring")
  198. //             Addstring(A$)             ! adds string "Hello"
  199. //
  200. void Addstring(char* str)
  201. {
  202.  
  203. //   ASSERT(g_pList != NULL);
  204.  
  205.    if (g_pList != NULL)
  206.    {
  207.        g_pList->AddString( str );
  208.    }
  209.  
  210. }  // End (Addstring)
  211.  
  212.  
  213. // **********  Insertstring **********
  214. // Inserts a string to the ListBox at a specified position
  215. //
  216. // HTB Usage:
  217. //             DLL GET "VOID Listbox:Insertstring"
  218. //             DIM A$[128]
  219. //             INTEGER P
  220. //             A$="Hello"
  221. //             P=8
  222. //             Addstring(3,"Teststring")
  223. //             Addstring((P),A$)
  224. //
  225. void Insertstring(short sel, char * str)
  226. {
  227.  
  228. //   ASSERT(g_pList != NULL);
  229.  
  230.    if (g_pList != NULL)
  231.    {
  232.        g_pList->InsertString( sel-1, str );
  233.    }
  234.  
  235. } // End (Insertstring)
  236.  
  237.  
  238. // **********  Getcount **********
  239. // Get number of items in ListBox
  240. //
  241. // HTB Usage:
  242. //             DLL GET "SHORT Listbox:Getcount"
  243. //             INTEGER P
  244. //             P=FNGetcount
  245. //
  246. short Getcount()
  247. {
  248.  
  249. //   ASSERT(g_pList != NULL);
  250.  
  251.    int cnt;
  252.  
  253.    if (g_pList == NULL)
  254.    {
  255.        Sleep(50);                 // wait until the thread is ready
  256.    }
  257.    
  258.    if (g_pList != NULL)
  259.    {
  260.        cnt=g_pList->GetCount();
  261.    }
  262.  
  263.    return(cnt);
  264.  
  265. } // End (Getcount)
  266.  
  267.  
  268.  
  269. // **********  Setsel **********
  270. // set/toggle selection of a ListBox item
  271. //
  272. // HTB Usage:
  273. //             DLL GET "VOID Listbox:Setsel"
  274. //             INTEGER Sel
  275. //             Sel=9
  276. //             Setsel((Sel))
  277. //             Setsel(5)
  278. //
  279. void Setsel(short sel)
  280. {
  281.  
  282.     bool selstat;
  283.  
  284.     selstat = ((g_pList->GetSel(sel-1)) == 0);
  285.  
  286.     if( SingleSelection )
  287.     {   // single selection listbox
  288.         if (g_pList != NULL)
  289.         {
  290.             g_pList->SetCurSel((int)sel-1);
  291.         }
  292.  
  293.     }
  294.     else 
  295.     {   // multiple selection listbox
  296.         if (g_pList != NULL)
  297.         {
  298.             g_pList->SetSel((int)sel-1,selstat);
  299.         }
  300.     
  301.     }
  302.  
  303. } // End (Setsel)
  304.  
  305.  
  306.  
  307. // **********  Getsel **********
  308. // Retrieves the selection state of a ListBox item
  309. //
  310. // HTB Usage:
  311. //             DLL GET "SHORT Listbox:Getsel"
  312. //             INTEGER Sel,Gsel
  313. //             Sel=9
  314. //             Gsel=FNGetsel((Sel))
  315. //             Gsel=FNGetsel(5)
  316. //
  317. short Getsel(short sel)
  318. {
  319.  
  320.     int selstat = 0;
  321.  
  322.     if (g_pList != NULL)
  323.     {
  324.         selstat = g_pList->GetSel((int)sel-1);
  325.  
  326.         if (selstat==LB_ERR)
  327.         {
  328.             selstat = 0;
  329.         }
  330.  
  331.         if (selstat > 0)
  332.         {
  333.             selstat = 1;
  334.         }
  335.  
  336.     }
  337.  
  338.     return(selstat);
  339.  
  340. } // End (Getsel)
  341.  
  342.  
  343.  
  344. // **********  Getselcount **********
  345. // Retrieves the number of selected items in ListBox
  346. // works with both single and multiple listboxes !
  347. //
  348. // HTB Usage:
  349. //             DLL GET "SHORT Listbox:Getselcount"
  350. //             INTEGER Gselcnt
  351. //             Gselcnt=FNGetselcount
  352. //
  353. short Getselcount()
  354. {
  355.  
  356.     int selstat;
  357.  
  358.     if( SingleSelection )
  359.     {
  360.         if (g_pList != NULL)
  361.         {
  362.             selstat = g_pList->GetCurSel();
  363.         }
  364.  
  365.         if ( selstat == LB_ERR )
  366.         {
  367.             selstat = 0;
  368.         }
  369.         else 
  370.         {
  371.             selstat=1;
  372.         }
  373.     
  374.     }
  375.     else
  376.     {
  377.         if (g_pList != NULL)
  378.         {
  379.             selstat = g_pList->GetSelCount();
  380.         }
  381.  
  382.         if ( selstat == LB_ERR )
  383.         {
  384.             selstat = 0;
  385.         }
  386.     
  387.     }
  388.  
  389.     return(selstat);
  390.  
  391. } // End (Getselcount)
  392.  
  393.  
  394.  
  395. // **********  Deletestring **********
  396. // Deletes an item in a ListBox
  397. //
  398. // HTB Usage:
  399. //             DLL GET "VOID Listbox:Deletestring"
  400. //             INTEGER Sel
  401. //             Sel=9
  402. //             Deletestring((Sel))
  403. //             Deletestring(4)
  404. //
  405. void Deletestring(short sel)
  406. {
  407.  
  408.     int selstat;
  409.  
  410.     if (g_pList != NULL)
  411.     {
  412.         selstat = g_pList->DeleteString((int)sel-1);
  413.  
  414.     }
  415.  
  416. } // End (Deletestring)
  417.  
  418.  
  419.  
  420. // **********  Resetcontent **********
  421. // Resets the content of a ListBox
  422. //
  423. // HTB Usage:
  424. //             DLL GET "VOID Listbox:Resetcontent"
  425. //             Resetcontent
  426. //
  427. void Resetcontent()
  428. {
  429.     if (g_pList != NULL)
  430.     {
  431.         g_pList->ResetContent();
  432.     }
  433.  
  434. } // End (Resetcontent)
  435.  
  436.  
  437.  
  438. // **********  Getsel **********
  439. // Shows files and directories
  440. //
  441. // HTB Usage:
  442. //             DLL GET "VOID Listbox:Dirlist"
  443. //             INTEGER Attr
  444. //             DIM Wildc$[256]
  445. //             Gsel=FNDirlist(Attr, Wildc$)
  446. //
  447. void Dirlist( short attr, char * wildcards)
  448. {
  449.     if (g_pList != NULL)
  450.     {
  451.         g_pList->Dir( attr, wildcards );
  452.  
  453.     }
  454.  
  455. } // End (Dirlist)
  456.  
  457.  
  458.  
  459. // **********  Findstring **********
  460. // Find the first appearance of a string in a ListBox (not case-sensitive)
  461. //
  462. // HTB Usage:
  463. //             DLL GET "SHORT Listbox:Findstring"
  464. //             DIM Search$[128]
  465. //             INTEGER Start
  466. //             A$="Search string"
  467. //             Start=8                        ! Search start at listbox entry 8
  468. //             Findstring(8,(Search$))
  469. //             Findstring((Start),"Search for")
  470. //
  471. short Findstring( short start, char * search )
  472. {
  473.     int result = 0;
  474.     
  475.     if (g_pList != NULL)
  476.     {
  477.         if(search != NULL)
  478.         {
  479.             result = g_pList->FindString( (int)start-1, search );
  480.     
  481.             if (result == LB_ERR)
  482.             {
  483.                 result = 0;
  484.             }
  485.             else
  486.             {
  487.                 result++;
  488.             }
  489.  
  490.         }    // search not NULL
  491.  
  492.     }    // g_pList not NULL
  493.  
  494.     return(result);
  495.  
  496. } // End (Findstring)
  497.  
  498.  
  499.  
  500. // **********  Findstringexact **********
  501. // Find a complete string in a ListBox (case-sensitive)
  502. //
  503. // HTB Usage:
  504. //             DLL GET "SHORT Listbox:Findstringexact"
  505. //             DIM Search$[128]
  506. //             INTEGER Start
  507. //             A$="Complete search string"
  508. //             Start=0                        ! Search the whole list box
  509. //             Findstringexact(8,(Search$))
  510. //             Findstringexact((Start),"Search for complete string")
  511. //
  512. short Findstringexact( short start, char * search )
  513. {
  514.     int result = 0;
  515.     
  516.     if (g_pList != NULL)
  517.     {
  518.         if(search != NULL)
  519.         {
  520.             result = g_pList->FindStringExact( (int)start-1, search );
  521.     
  522.             if (result == LB_ERR)
  523.             {
  524.                 result = 0;
  525.             }
  526.             else
  527.             {
  528.                 result++;
  529.             }
  530.  
  531.         }    // search not NULL
  532.  
  533.     }    // g_pList not NULL
  534.  
  535.     return(result);
  536.  
  537. } // End (Findstringexact)
  538.  
  539.  
  540.  
  541. // **********  Gettextlen **********
  542. // Returns the string length of a specified ListBox entry
  543. //
  544. // HTB Usage:
  545. //             DLL GET "SHORT Listbox:Gettextlen"
  546. //             INTEGER Sel
  547. //             Sel=4
  548. //             Gettextlen((Sel))
  549. //             Gettextlen(10)
  550. //
  551. short Gettextlen( short sel )
  552. {
  553.     int result;
  554.     if (g_pList != NULL)
  555.     {
  556.         result = g_pList->GetTextLen( (int)sel-1);
  557.     
  558.         if (result == LB_ERR)
  559.         {
  560.             result = 0;
  561.         }
  562.         else
  563.         {
  564.             result += 5;        // just to be sure !
  565.         }
  566.  
  567.     }
  568.     else
  569.     {
  570.         result = NULL;
  571.     }
  572.  
  573.     return(result);
  574.  
  575. } // End (Gettextlen)
  576.  
  577.  
  578.  
  579. // **********  Gettext **********
  580. // Returns the string of a specified listbox entry
  581. //
  582. // HTB Usage:
  583. //             DLL GET "VOID Listbox:Gettext"
  584. //             DIM Text$[256]
  585. //             INTEGER Sel
  586. //             Sel=4
  587. //             Gettext((Sel), Text$)
  588. //             Gettext(10, Text$)
  589. //
  590. void Gettext( short sel, char *item )
  591. {
  592.     int result;
  593.  
  594.     if (g_pList != NULL)
  595.     {
  596.         if (item != NULL)
  597.         {
  598.             result = g_pList->GetText( (int)sel-1, item);
  599.  
  600.             if (result == LB_ERR)
  601.             {
  602.                 *item = '\0';    // zero string indicates error !
  603.  
  604.             }    // got an error
  605.         
  606.         }    // item not NULL
  607.  
  608.     }    // g_pList not NULL
  609.  
  610. } // End (Gettext)
  611.  
  612.  
  613.  
  614. // **********  Getselitems **********
  615. // Returns a list of all selected items. Not for single selection listboxes !
  616. // If called for a single selection listbox it returns -1
  617. //
  618. // HTB Usage:
  619. //             DLL GET "VOID Listbox:Getselitems"
  620. //             INTEGER Getlist(100)
  621. //             INTEGER Maxitems
  622. //             MAT Getlist=(0)
  623. //             Getselitems(10, Getlist(*))
  624. //             Maxitems=SIZE(Getlist,1)
  625. //             Getselitems((Maxitems), Getlist(*))
  626. //
  627. short Getselitems( short nMaxItems, short* iarray)
  628. {
  629.     if (SingleSelection)
  630.     {
  631.         return(-1);       // does not work with single selection style
  632.     }
  633.  
  634.     short i;
  635.  
  636.     for ( i = 1; i <= nMaxItems; i++)
  637.     {
  638.         if ( Getsel(i)==1 )
  639.         {
  640.             *iarray++ = i;
  641.         }
  642.  
  643.     }    // for loop
  644.  
  645.     return(0);
  646.  
  647. } // End (Getselitems)
  648.  
  649.  
  650.  
  651. // **********  Getcursel **********
  652. // Returns the index of the selected item (or 0 if no item is selected).
  653. // Only for single selection style listboxes
  654. //
  655. // HTB Usage:
  656. //             DLL GET "SHORT Listbox:Getcursel"
  657. //             INTEGER Cursel
  658. //             Cursel=FNGetcursel
  659. //             Maxitems=SIZE(Getlist,1)
  660. //             Getcursel((Maxitems), Getlist(*))
  661. //
  662. short Getcursel()
  663. {
  664.     int result;
  665.  
  666.     if (SingleSelection)
  667.     {
  668.         if (g_pList != NULL)
  669.         {
  670.             result = g_pList->GetCurSel();
  671.  
  672.             if (result == LB_ERR)
  673.             {
  674.                 result = 0;    // zero indicates error !
  675.             }
  676.             else
  677.             {
  678.                 result++;
  679.             }
  680.  
  681.         }    // list not NULL if statement
  682.         else
  683.         {
  684.             result = 0;
  685.         }
  686.  
  687.     }
  688.     else
  689.     {
  690.         result = -1;    // does only work with single selection style
  691.     }
  692.  
  693.     return(result);
  694.  
  695. } // End (Getcursel)
  696.