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

  1. // ListboxDlg.cpp : implementation file
  2. //    High Tech BASIC, Copyright (C) TransEra Corp 1999, All Rights Reserved.
  3. //
  4. // Author:       Sven Henze, Tech Soft GmbH (Germany)
  5. // Created:      22-Aug-1999
  6. // Last update:  28-Sep-1999
  7. //
  8.  
  9. #include "stdafx.h"
  10. #include "Listbox.h"
  11. #include "ListboxDlg.h"
  12. #include "export.h"
  13. #include "adderdlg.h"
  14. #include "editdlg.h"
  15.  
  16. #ifdef _DEBUG
  17. #define new DEBUG_NEW
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #define LINEHEIGHT 12
  23. #define WIDTHADJUST 23
  24. #define STARTHEIGHT 370
  25. #define HEIGHTADJUST 30
  26. #define MINWIDTH 150
  27.  
  28.  
  29. char tmp[256];
  30.  
  31.  
  32. /////////////////////////////////////////////////////////////////////////////
  33. // ListboxDlg dialog
  34.  
  35.  
  36. ListboxDlg::ListboxDlg(CWnd* pParent /*=NULL*/)
  37.     : CDialog(ListboxDlg::IDD, pParent)
  38. {
  39.     //{{AFX_DATA_INIT(ListboxDlg)
  40.     //}}AFX_DATA_INIT
  41.  
  42.     m_pModeless = NULL;
  43.     m_pModeless2 = NULL;
  44.  
  45. }
  46.  
  47. void ListboxDlg::DoDataExchange(CDataExchange* pDX)
  48. {
  49.     CDialog::DoDataExchange(pDX);
  50.     //{{AFX_DATA_MAP(ListboxDlg)
  51.     DDX_Control(pDX, IDC_LIST2, m_List2);
  52.     DDX_Control(pDX, IDC_LIST1, m_List1);
  53.     DDX_Control(pDX, IDC_BUTTON1, m_Butt1);
  54.     DDX_Control(pDX, IDC_BUTTON2, m_Butt2);
  55.     DDX_Control(pDX, IDC_BUTTON3, m_Butt3);
  56.     DDX_Control(pDX, IDC_BUTTON14, m_Butt4);
  57.     //}}AFX_DATA_MAP
  58. }
  59.  
  60.  
  61. BEGIN_MESSAGE_MAP(ListboxDlg, CDialog)
  62.     //{{AFX_MSG_MAP(ListboxDlg)
  63.     ON_BN_CLICKED(IDC_BUTTON13, OKPressed)
  64.     ON_BN_CLICKED(IDC_BUTTON14, CancelPressed)
  65.     ON_WM_CLOSE()
  66.     ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
  67.     ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  68.     ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  69.     //}}AFX_MSG_MAP
  70. END_MESSAGE_MAP()
  71.  
  72. /////////////////////////////////////////////////////////////////////////////
  73. // ListboxDlg message handlers
  74.  
  75. BOOL ListboxDlg::OnInitDialog() 
  76. {
  77.     CDialog::OnInitDialog();
  78.  
  79.     if (g_Width < MINWIDTH)
  80.     {
  81.         g_Width = MINWIDTH;
  82.     }
  83.  
  84.     g_pWnd = this;
  85.  
  86.     int adjust=0;
  87.     if (NoButtonVisible)
  88.     {
  89.         adjust = 28;
  90.     }
  91.  
  92.     
  93.     SetWindowText(g_Title);                                    // set window title
  94.  
  95.     SetDlgItemText(IDC_STATIC0,g_Description);                // set static text control text
  96.     SetDlgItemText(IDC_LIST1,g_Lstrings);                    // string list text (group text)
  97.  
  98.     CWnd* pStaticText = GetDlgItem(IDC_STATIC0);                    // get pointer to static text control text
  99.     pStaticText->SetWindowPos(NULL,0,0,g_Width+4,70+g_Height,SWP_NOZORDER | SWP_NOMOVE);    // set width of static text control
  100.     
  101.     CWnd* pStaticL1Text = GetDlgItem(IDC_LIST1);                    // get pointer to multiple selection style listbox
  102.     pStaticL1Text->SetWindowPos(NULL,0,0,g_Width-10,19+g_Height+adjust,SWP_NOZORDER | SWP_NOMOVE);    // set width of static text control
  103.     
  104.     CWnd* pStaticL2Text = GetDlgItem(IDC_LIST2);                    // get pointer to single selection style listbox
  105.     pStaticL2Text->SetWindowPos(NULL,0,0,g_Width-10,19+g_Height+adjust,SWP_NOZORDER | SWP_NOMOVE);    // set width of static text control
  106.  
  107.     CWnd* pNewButton = GetDlgItem(IDC_BUTTON1);                    // get pointer to New button
  108.     CWnd* pEditButton = GetDlgItem(IDC_BUTTON2);                // get pointer to Edit button
  109.     CWnd* pDelButton = GetDlgItem(IDC_BUTTON3);                    // get pointer to Delete button
  110.     CWnd* pOKButton = GetDlgItem(IDC_BUTTON13);                    // get pointer to 'OK' button
  111.     CWnd* pCancelButton = GetDlgItem(IDC_BUTTON14);                // get pointer to 'Cancel' button
  112.     
  113.  
  114.     // disable unused buttons
  115.     if (NewDisabled)
  116.     {
  117.         m_Butt1.ModifyStyle(0, WS_DISABLED, 0);
  118.     }
  119.  
  120.     if (EditDisabled)
  121.     {
  122.         m_Butt2.ModifyStyle(0, WS_DISABLED, 0);
  123.     }
  124.  
  125.     if (DeleteDisabled) 
  126.     {
  127.         m_Butt3.ModifyStyle(0, WS_DISABLED, 0);
  128.     }
  129.  
  130.     
  131.     if (CancelDisabled)  
  132.     {
  133.         // center the 'OK' button
  134.         pOKButton->SetWindowPos(NULL,(g_Width+WIDTHADJUST)/2-33,94+g_Height,60,23,SWP_NOZORDER);
  135.         m_Butt4.DestroyWindow();
  136.     }
  137.     else
  138.     {
  139.         // center the 'OK' and 'Cancel buttons
  140.         pOKButton->SetWindowPos(NULL,(g_Width+WIDTHADJUST)/2-72,94+g_Height,60,23,SWP_NOZORDER);    // set position of OK button
  141.         pCancelButton->SetWindowPos(NULL,(g_Width+WIDTHADJUST)/2+3,94+g_Height,60,23,SWP_NOZORDER);    // set position of Cancel button
  142.     }
  143.     
  144.  
  145.     // now delete unused buttons if all buttons are disabled
  146.     if (NoButtonVisible)
  147.     {
  148.       m_Butt1.DestroyWindow();
  149.       m_Butt2.DestroyWindow();
  150.       m_Butt3.DestroyWindow();
  151.     }
  152.     else
  153.     {
  154.         // center the 'special' buttons
  155.         adjust = (g_Width+WIDTHADJUST)/2 - 86;
  156.         pNewButton->SetWindowPos(NULL,12+adjust,52+g_Height,43,21,SWP_NOZORDER);    // set position of 'New' button
  157.         pEditButton->SetWindowPos(NULL,61+adjust,52+g_Height,43,21,SWP_NOZORDER);    // set position of 'Edit' button
  158.         pDelButton->SetWindowPos(NULL,109+adjust,52+g_Height,43,21,SWP_NOZORDER);    // set position of 'Delete' button
  159.     }
  160.  
  161.  
  162.     // select the appropriate ListBox ressource (single/multiple style)
  163.     if (SingleSelection) 
  164.     {
  165.         g_pList = (CListBox*) (g_pWnd->GetDlgItem(IDC_LIST2));
  166.         m_List1.DestroyWindow();
  167.     }
  168.     else 
  169.     {
  170.         g_pList = (CListBox*) (g_pWnd->GetDlgItem(IDC_LIST1));
  171.         m_List2.DestroyWindow();
  172.     }
  173.  
  174.     // set window size
  175.     SetWindowPos(NULL,0,0,g_Width+WIDTHADJUST,155+g_Height,SWP_NOZORDER );
  176.     
  177.     SetForegroundWindow();
  178.  
  179.     return TRUE;  // return TRUE unless you set the focus to a control
  180.                   // EXCEPTION: OCX Property Pages should return FALSE
  181.  
  182. }    // OnInitDialog
  183.  
  184.  
  185. void ListboxDlg::OnClose() 
  186. {    Closelistbox();    
  187.     CDialog::OnClose();
  188.  
  189. }    // OnClose
  190.  
  191.  
  192. void ListboxDlg::OKPressed()
  193. {    Closelistbox();
  194. }    // OKPressed
  195.  
  196.  
  197. void ListboxDlg::CancelPressed()
  198. {    Closelistbox();
  199.  
  200. }    // CancelPressed
  201.  
  202.  
  203. // Delete selected item(s)
  204. void ListboxDlg::OnButton3() 
  205. {
  206.     int result, cnt, selcnt, i, j;
  207.  
  208.     // first check if there are any items selected
  209.     selcnt = Getselcount();      // number of selected items
  210.  
  211.     if ( selcnt != 0 )
  212.     {
  213.         cnt = g_pList->GetCount();            // number of items in listbox
  214.         result = MessageBox( "Are you sure to delete the selected item(s)?", g_Title, MB_ICONQUESTION | MB_YESNO |  MB_APPLMODAL);
  215.  
  216.         if ( result == IDYES )
  217.         {
  218.             for ( i = cnt-1; i >=0; i-- )
  219.             {
  220.                 j = g_pList->GetSel(i);
  221.                 if ( (j!=0) && (j!=LB_ERR) )
  222.                 {
  223.                     g_pList->DeleteString(i);
  224.                 }
  225.  
  226.             }    // for loop
  227.     
  228.         }    // IDYES if statement
  229.  
  230.     }    // ( selcnt !=0 )
  231.     
  232. }    // OnButton3
  233.  
  234.  
  235. // Edit selected item
  236. void ListboxDlg::OnButton2() 
  237. {
  238.     int cnt, selcnt, i, j;
  239.  
  240.     // first check if there are any items selected
  241.     selcnt = Getselcount();      // number of selected items
  242.  
  243.     if ( selcnt == 1 )           // we can only edit one item at the same time !
  244.     {
  245.         // now search for item
  246.         cnt = Getcount();            // number of items in listbox
  247.  
  248.         i=0;
  249.         while (Getsel(i+1)==0)
  250.         {
  251.             i++;
  252.         }
  253.  
  254.         j=i;
  255.  
  256.         if ( m_pModeless2 == NULL )
  257.         {
  258.             m_pModeless2 = new CEditDlg(this);
  259.             m_pModeless2->Create();
  260.  
  261.             CEdit* pEdit = (CEdit*) m_pModeless2->GetDlgItem(IDC_EDIT1);
  262.  
  263.             g_pList->GetText(j, tmp);        // get listbox entry
  264.             pEdit->SetWindowText(tmp);     // transfer listbox entry into edit window
  265.             pEdit->SetSel(0, -1, FALSE);   // select the whole text
  266.  
  267.             (m_pModeless2->GetDlgItem(IDC_EDIT1))->SetFocus();  // give edit window focus
  268.  
  269.  
  270.         }
  271.         else
  272.         {
  273.              m_pModeless2->SetActiveWindow();
  274.         }
  275.  
  276.     }
  277.     
  278. }    // OnButton2
  279.  
  280.  
  281. // reset variable
  282. void ListboxDlg::AdderBoxDone()
  283. {
  284.     m_pModeless = NULL;
  285.  
  286. }    // AdderBoxDone
  287.  
  288.  
  289. // reset variable
  290. void ListboxDlg::EditBoxDone()
  291. {
  292.     m_pModeless2 = NULL;
  293.  
  294. }    // EditBoxDone
  295.  
  296.  
  297. // Add a new entry
  298. void ListboxDlg::OnButton1() 
  299. {
  300.     int selcnt;
  301.  
  302.     // first check if there are any items selected
  303.     selcnt = Getselcount();      // number of selected items
  304.  
  305.     g_bEdsort = (selcnt != 1);
  306.     
  307.     if ( m_pModeless == NULL )            // create window is neccessary
  308.     {
  309.         m_pModeless = new CAdderDlg(this);
  310.         m_pModeless->Create();
  311.  
  312.     }
  313.     else
  314.     {
  315.          m_pModeless->SetActiveWindow();  // windows exist, so give it the focus
  316.     }
  317.  
  318.     //  set focus to edit control
  319.     (m_pModeless->GetDlgItem(IDC_EDIT1))->SetFocus();
  320.     
  321. }    // OnButton1
  322.