home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2004 March / PCWELT_3_2004.ISO / pcwsoft / flaskmpeg_078_39_src.z.exe / mism_null / SelectorDialog.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  7.8 KB  |  270 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.  
  27. #include "selectordialog.h"
  28. //global
  29. TSelectorDialog   *set;
  30. CArr <int>        listbox_mirror;
  31.  
  32.  
  33. /* Rendering selections */
  34. void RenderSectionSelection(HWND hDlg, int section)
  35. {
  36.     int i;  
  37.     for(i=0; i<set->strings[section].GetCount(); i++)
  38.     {
  39.         if( set->selected[section][i] )
  40.             SendDlgItemMessage( hDlg, IDC_SELECTOR, LB_SELITEMRANGE, true,MAKELPARAM(set->first_in_section[section]+i,set->first_in_section[section]+i ));
  41.     }
  42. }
  43. void RenderSelection( HWND hDlg )
  44. {
  45.     int i;
  46.     // First, unselect all items
  47.     int list_items = SendDlgItemMessage( hDlg, IDC_SELECTOR, LB_GETCOUNT   , 0     , 0);
  48.     for( i=0; i<list_items; i++)
  49.         SendDlgItemMessage( hDlg, IDC_SELECTOR, LB_SELITEMRANGE, false, MAKELPARAM(i,i) );
  50.     // Now, render selections
  51.     for(i=0; i<set->section_count; i++)
  52.         RenderSectionSelection(hDlg, i);
  53. }
  54. void MirrorList(HWND hDlg, CArr<int> &array)
  55. {
  56.  
  57.     int placed,i;
  58.  
  59.     int list_items = SendDlgItemMessage( hDlg, IDC_SELECTOR, LB_GETCOUNT   , 0     , 0);
  60.     int    *selected  = new int[list_items];
  61.     placed         = SendDlgItemMessage( hDlg, IDC_SELECTOR, LB_GETSELITEMS, list_items, (LPARAM)selected);
  62.  
  63.  
  64.     //reset current array
  65.     array.SetArraySize(list_items);
  66.     for(i=0; i<list_items; i++)
  67.          array[i] = 0;
  68.     // mark selected items
  69.     for(i=0; i<placed; i++)
  70.          array[ selected[i] ] = 1;
  71.     delete [] selected;
  72. }
  73.  
  74.  
  75. /* Section handling */
  76. int  GetSection(HWND hDlg, int listbox_item)
  77. {
  78.     int i;
  79.     CArr<int>      current_selected;
  80.     MirrorList(hDlg, current_selected);
  81.  
  82.     for(i=0; i<set->section_count; i++)
  83.     {
  84.         if(listbox_item>= set->first_in_section[i]   &&
  85.            listbox_item<= set->last_in_section[i])
  86.            return i;
  87.     }
  88.     return 0;
  89. }
  90.  
  91. int  GetSectionPos(HWND hDlg, int listbox_item)
  92. {
  93.     int i;
  94.     CArr<int>      current_selected;
  95.     MirrorList(hDlg, current_selected);
  96.  
  97.     for(i=0; i<set->section_count; i++)
  98.     {
  99.         if(listbox_item>= set->first_in_section[i]   &&
  100.            listbox_item<= set->last_in_section[i])
  101.            return listbox_item - set->first_in_section[i];
  102.     }
  103.     return 0;
  104. }
  105. int GetSectionSelCount(HWND hDlg, int section)
  106. {
  107.     int selected=0,i;
  108.     for(i=0; i<set->strings[section].GetCount(); i++)
  109.     {
  110.         if(set->selected[section][i])
  111.             selected++;
  112.     }
  113.     return selected;
  114. }
  115. bool ItemIsInSection(HWND hDlg, int item)
  116. {
  117.     int i;
  118.     CArr<int>      current_selected;
  119.     MirrorList(hDlg, current_selected);
  120.  
  121.     for(i=0; i<set->section_count; i++)
  122.     {
  123.         if(item>= set->first_in_section[i]   &&
  124.            item<= set->last_in_section[i])
  125.            return true;
  126.     }
  127.     return false;
  128. }
  129. int  FindFirstDiff(CArr<int> &first, CArr<int> &second)
  130. {
  131.     int count,i;
  132.     if( first.GetCount() != second.GetCount() )
  133.         return 0;
  134.     count = first.GetCount();
  135.     for(i=0; i<count; i++)
  136.         if(first[i]!=second[i])
  137.             break;
  138.     return i;
  139. }
  140.  
  141. // helper function
  142. void DlgSetText(HWND hDlg, int item, int number){
  143.   
  144.   char szTemp[1024];
  145.   SetDlgItemText(hDlg, item, itoa(number, szTemp, 10));
  146. }
  147.  
  148. LRESULT CALLBACK DlgSelector(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  149. {
  150.   int i,j, clicked_listitem, section, section_pos;
  151.   CArr<int>  current_selected;
  152.   
  153.   switch (message)
  154.   {
  155.         case WM_INITDIALOG:    
  156.       SendMessage(hDlg, WM_SETTEXT, 0, (LPARAM)set->tittle);
  157.  
  158.       SetDlgItemText(hDlg, R_RIGHT_SELECTOR_DIALOG, set->lateral_text);
  159.       SetDlgItemText(hDlg, IDOK,  set->button_text);
  160.       
  161.       // Build the presentation list
  162.       for(i=0; i<set->section_count; i++){
  163.         SendDlgItemMessage(hDlg, IDC_SELECTOR, LB_ADDSTRING, 0, (LPARAM)set->sections_titles[i]);
  164.         set->first_in_section[i] = 
  165.           1 + SendDlgItemMessage(hDlg, IDC_SELECTOR, LB_ADDSTRING, 0, (LPARAM)"------------------------------------------------------------");
  166.         
  167.         for(j=0; j<set->strings[i].GetCount(); j++){
  168.           set->last_in_section[i]=SendDlgItemMessage(hDlg, IDC_SELECTOR, LB_ADDSTRING, 0, (LPARAM)set->strings[i][j]);
  169.         }
  170.         SendDlgItemMessage(hDlg, IDC_SELECTOR, LB_ADDSTRING, 0, (LPARAM)"");
  171.         //Select streams
  172.       }
  173.       RenderSelection(hDlg);
  174.       // Now get a mirror image of the list box
  175.       MirrorList(hDlg, listbox_mirror);
  176.       return TRUE;
  177.       
  178.     case WM_COMMAND:
  179.       if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
  180.       {
  181.         EndDialog(hDlg, LOWORD(wParam));
  182.         return TRUE;
  183.       }
  184.       
  185.       
  186.       switch (LOWORD(wParam)) 
  187.       { 
  188.       case IDC_SELECTOR: 
  189.         switch (HIWORD(wParam)) 
  190.         { 
  191.         case LBN_SELCHANGE: 
  192.           // Guess if the item clicked belonged to a section
  193.           MirrorList(hDlg, current_selected);
  194.           clicked_listitem = FindFirstDiff(listbox_mirror, current_selected);
  195.           if( ItemIsInSection(hDlg, clicked_listitem) )
  196.           {
  197.             // The item belongs to a section.
  198.             // Guess section
  199.             section      =    GetSection( hDlg, clicked_listitem );
  200.             section_pos  = GetSectionPos( hDlg, clicked_listitem );
  201.             
  202.             // if SINGLE_SELECT
  203.             if( set->section_mode[section]&SINGLE_SELECT )
  204.             {
  205.               int clicked_state = set->selected[section][section_pos];
  206.               // Unselect all items in section
  207.               for(i=0; i<set->strings[section].GetCount(); i++)
  208.                 set->selected[section][i] = 0;
  209.               // select clicked item
  210.               set->selected[section][section_pos] = !clicked_state;
  211.             }
  212.             if( set->section_mode[section]&MULTIPLE_SELECT )
  213.             {
  214.               set->selected[section][section_pos] = !set->selected[section][section_pos];
  215.             }
  216.             if( set->section_mode[section]&MUST_SELECT )
  217.             {
  218.               // if there are no selected items in this section
  219.               if (GetSectionSelCount(hDlg, section) == 0)
  220.                 set->selected[section][section_pos] = 1;
  221.             }
  222.             
  223.           }
  224.           RenderSelection(hDlg);
  225.           // Now get a mirror image of the list box
  226.           MirrorList(hDlg, listbox_mirror);
  227.           return TRUE; 
  228.           
  229.         } 
  230.         break; 
  231.         
  232.         // Destroy the dialog box. 
  233.         
  234.         EndDialog(hDlg, TRUE); 
  235.         return TRUE; 
  236.         
  237.         default: 
  238.           return FALSE; 
  239.       } 
  240.       
  241.       break;
  242.   }
  243.   return FALSE;
  244. }
  245. int GetFirstSelected( TSelectorDialog *set, int section )
  246. {
  247.   int i;
  248.   for( i=0; i <set->strings[section].GetCount(); i++ ){
  249.     if( set->selected[section][i]==1 )
  250.       break;
  251.   }
  252.   return i;
  253. }
  254. void ResetSelections(TSelectorDialog *set){
  255.     int i,j;
  256.     for(i=0; i<set->section_count; i++)
  257.         set->selected[i].SetArraySize( set->strings[i].GetCount() );
  258.  
  259.     for(i=0; i<set->section_count; i++)
  260.     {
  261.         for(j=0; j<set->strings[i].GetCount(); j++)
  262.             set->selected[i][j]=0;
  263.     }
  264. }
  265.  
  266. int OpenSelectorDialog(HWND hWnd, HINSTANCE hInstance, TSelectorDialog *settings){
  267.     set = settings;
  268.     return DialogBox(hInstance, (LPCTSTR)(IDD_STRSELECTOR), hWnd ,(DLGPROC)DlgSelector);
  269. }
  270.