home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2004 March / PCWELT_3_2004.ISO / pcwsoft / flaskmpeg_078_39_src.z.exe / flaskmpeg / Misc / SelectorDialog.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  7.8 KB  |  269 lines

  1. /* 
  2.  *  SelectorDialog.cpp
  3.  *
  4.  *    Copyright (C) Alberto Vigata - January 2000
  5.  *
  6.  *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
  7.  *    
  8.  *  FlasKMPEG is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2, or (at your option)
  11.  *  any later version.
  12.  *   
  13.  *  FlasKMPEG is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *   
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with GNU Make; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  21.  *
  22.  */
  23.  
  24. #include <windows.h>
  25. #include "..\resource.h"
  26. #include "..\auxiliary.h"
  27.  
  28. #include "selectordialog.h"
  29. //global
  30. TSelectorDialog   *set;
  31. CArr <int>        listbox_mirror;
  32.  
  33.  
  34. /* Rendering selections */
  35. void RenderSectionSelection(HWND hDlg, int section)
  36. {
  37.     int i;  
  38.     for(i=0; i<set->strings[section].GetCount(); i++)
  39.     {
  40.         if( set->selected[section][i] )
  41.             SendDlgItemMessage( hDlg, IDC_SELECTOR, LB_SELITEMRANGE, true,MAKELPARAM(set->first_in_section[section]+i,set->first_in_section[section]+i ));
  42.     }
  43. }
  44. void RenderSelection( HWND hDlg )
  45. {
  46.     int i;
  47.     // First, unselect all items
  48.     int list_items = SendDlgItemMessage( hDlg, IDC_SELECTOR, LB_GETCOUNT   , 0     , 0);
  49.     for( i=0; i<list_items; i++)
  50.         SendDlgItemMessage( hDlg, IDC_SELECTOR, LB_SELITEMRANGE, false, MAKELPARAM(i,i) );
  51.     // Now, render selections
  52.     for(i=0; i<set->section_count; i++)
  53.         RenderSectionSelection(hDlg, i);
  54. }
  55. void MirrorList(HWND hDlg, CArr<int> &array)
  56. {
  57.  
  58.     int placed,i;
  59.  
  60.     int list_items = SendDlgItemMessage( hDlg, IDC_SELECTOR, LB_GETCOUNT   , 0     , 0);
  61.     int    *selected  = new int[list_items];
  62.     placed         = SendDlgItemMessage( hDlg, IDC_SELECTOR, LB_GETSELITEMS, list_items, (LPARAM)selected);
  63.  
  64.  
  65.     //reset current array
  66.     array.SetArraySize(list_items);
  67.     for(i=0; i<list_items; i++)
  68.          array[i] = 0;
  69.     // mark selected items
  70.     for(i=0; i<placed; i++)
  71.          array[ selected[i] ] = 1;
  72.     delete [] selected;
  73. }
  74.  
  75.  
  76. /* Section handling */
  77. int  GetSection(HWND hDlg, int listbox_item)
  78. {
  79.     int i;
  80.     CArr<int>      current_selected;
  81.     MirrorList(hDlg, current_selected);
  82.  
  83.     for(i=0; i<set->section_count; i++)
  84.     {
  85.         if(listbox_item>= set->first_in_section[i]   &&
  86.            listbox_item<= set->last_in_section[i])
  87.            return i;
  88.     }
  89.     return 0;
  90. }
  91.  
  92. int  GetSectionPos(HWND hDlg, int listbox_item)
  93. {
  94.     int i;
  95.     CArr<int>      current_selected;
  96.     MirrorList(hDlg, current_selected);
  97.  
  98.     for(i=0; i<set->section_count; i++)
  99.     {
  100.         if(listbox_item>= set->first_in_section[i]   &&
  101.            listbox_item<= set->last_in_section[i])
  102.            return listbox_item - set->first_in_section[i];
  103.     }
  104.     return 0;
  105. }
  106. int GetSectionSelCount(HWND hDlg, int section)
  107. {
  108.     int selected=0,i;
  109.     for(i=0; i<set->strings[section].GetCount(); i++)
  110.     {
  111.         if(set->selected[section][i])
  112.             selected++;
  113.     }
  114.     return selected;
  115. }
  116. bool ItemIsInSection(HWND hDlg, int item)
  117. {
  118.     int i;
  119.     CArr<int>      current_selected;
  120.     MirrorList(hDlg, current_selected);
  121.  
  122.     for(i=0; i<set->section_count; i++)
  123.     {
  124.         if(item>= set->first_in_section[i]   &&
  125.            item<= set->last_in_section[i])
  126.            return true;
  127.     }
  128.     return false;
  129. }
  130. int  FindFirstDiff(CArr<int> &first, CArr<int> &second)
  131. {
  132.     int count,i;
  133.     if( first.GetCount() != second.GetCount() )
  134.         return 0;
  135.     count = first.GetCount();
  136.     for(i=0; i<count; i++)
  137.         if(first[i]!=second[i])
  138.             break;
  139.     return i;
  140. }
  141.  
  142.  
  143. LRESULT CALLBACK DlgSelector(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  144. {
  145.   unsigned int i,j, clicked_listitem, section, section_pos;
  146.   CArr<int>  current_selected;
  147.   
  148.   switch (message)
  149.   {
  150.         case WM_INITDIALOG:    
  151.       SetText(hDlg, set->tittle);
  152.       DlgSetText(hDlg, R_RIGHT_SELECTOR_DIALOG, set->lateral_text);
  153.       DlgSetText(hDlg, IDOK, set->button_text );
  154.       
  155.       // Build the presentation list
  156.       for(i=0; i<set->section_count; i++){
  157.         SendDlgItemMessage(hDlg, IDC_SELECTOR, LB_ADDSTRING, 0, (LPARAM)set->sections_titles[i]);
  158.         set->first_in_section[i] = 
  159.           1 + SendDlgItemMessage(hDlg, IDC_SELECTOR, LB_ADDSTRING, 0, (LPARAM)"------------------------------------------------------------");
  160.         
  161.         for(j=0; j<set->strings[i].GetCount(); j++){
  162.           set->last_in_section[i]=SendDlgItemMessage(hDlg, IDC_SELECTOR, LB_ADDSTRING, 0, (LPARAM)set->strings[i][j]);
  163.         }
  164.         SendDlgItemMessage(hDlg, IDC_SELECTOR, LB_ADDSTRING, 0, (LPARAM)"");
  165.         //Select streams
  166.       }
  167.       RenderSelection(hDlg);
  168.       // Now get a mirror image of the list box
  169.       MirrorList(hDlg, listbox_mirror);
  170.       return TRUE;
  171.       
  172.     case WM_COMMAND:
  173.       if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
  174.       {
  175.         EndDialog(hDlg, LOWORD(wParam));
  176.         return TRUE;
  177.       }
  178.       
  179.       
  180.       switch (LOWORD(wParam)) 
  181.       { 
  182.       case IDC_SELECTOR: 
  183.         switch (HIWORD(wParam)) 
  184.         { 
  185.         case LBN_SELCHANGE: 
  186.           // Guess if the item clicked belonged to a section
  187.           MirrorList(hDlg, current_selected);
  188.           clicked_listitem = FindFirstDiff(listbox_mirror, current_selected);
  189.           if( ItemIsInSection(hDlg, clicked_listitem) )
  190.           {
  191.             // The item belongs to a section.
  192.             // Guess section
  193.             section      =    GetSection( hDlg, clicked_listitem );
  194.             section_pos  = GetSectionPos( hDlg, clicked_listitem );
  195.             
  196.             if (set->helpstrings[section].GetCount() > section_pos)
  197.                 DlgSetText(hDlg, IDC_HELPSTRING, set->helpstrings[section][section_pos]);
  198.             else
  199.                 DlgSetText(hDlg, IDC_HELPSTRING, "");
  200.  
  201.             // if SINGLE_SELECT
  202.             if( set->section_mode[section]&SINGLE_SELECT )
  203.             {
  204.               int clicked_state = set->selected[section][section_pos];
  205.               // Unselect all items in section
  206.               for(i=0; i<set->strings[section].GetCount(); i++)
  207.                 set->selected[section][i] = 0;
  208.               // select clicked item
  209.               set->selected[section][section_pos] = !clicked_state;
  210.             }
  211.             if( set->section_mode[section]&MULTIPLE_SELECT )
  212.             {
  213.               set->selected[section][section_pos] = !set->selected[section][section_pos];
  214.             }
  215.             if( set->section_mode[section]&MUST_SELECT )
  216.             {
  217.               // if there are no selected items in this section
  218.               if (GetSectionSelCount(hDlg, section) == 0)
  219.                 set->selected[section][section_pos] = 1;
  220.             }
  221.             
  222.           }
  223.           RenderSelection(hDlg);
  224.           // Now get a mirror image of the list box
  225.           MirrorList(hDlg, listbox_mirror);
  226.           return TRUE; 
  227.           
  228.         } 
  229.         break; 
  230.         
  231.         // Destroy the dialog box. 
  232.         
  233.         EndDialog(hDlg, TRUE); 
  234.         return TRUE; 
  235.         
  236.         default: 
  237.           return FALSE; 
  238.       } 
  239.       
  240.       break;
  241.   }
  242.   return FALSE;
  243. }
  244. int GetFirstSelected( TSelectorDialog *set, int section )
  245. {
  246.   int i;
  247.   for( i=0; i <set->strings[section].GetCount(); i++ ){
  248.     if( set->selected[section][i]==1 )
  249.       break;
  250.   }
  251.   return i;
  252. }
  253. void ResetSelections(TSelectorDialog *set){
  254.     int i,j;
  255.     for(i=0; i<set->section_count; i++)
  256.         set->selected[i].SetArraySize( set->strings[i].GetCount() );
  257.  
  258.     for(i=0; i<set->section_count; i++)
  259.     {
  260.         for(j=0; j<set->strings[i].GetCount(); j++)
  261.             set->selected[i][j]=0;
  262.     }
  263. }
  264.  
  265. int OpenSelectorDialog(HWND hWnd, HINSTANCE hInstance, TSelectorDialog *settings){
  266.     set = settings;
  267.     return DialogBox(hInstance, (LPCTSTR)(IDD_STRSELECTOR), hWnd ,(DLGPROC)DlgSelector);
  268. }
  269.