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

  1. /* 
  2.  *  Auxiliary.cpp
  3.  *
  4.  *    Copyright (C) Alberto Vigata - January 2000 - ultraflask@yahoo.com
  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 <commctrl.h>
  26. #include "auxiliary.h"
  27. #include "resource.h"
  28. #include "stdio.h"
  29. #include <math.h>
  30.  
  31.  
  32.  
  33. int GetOutputFileName(HWND hWnd, char* fileName,char* Dir, char* Title){
  34.     OPENFILENAME oifn;
  35.         // Initialize OPENFILENAME
  36.         ZeroMemory(&oifn, sizeof(OPENFILENAME));
  37.         oifn.lStructSize = sizeof(OPENFILENAME);
  38.         oifn.hwndOwner = hWnd;
  39.         oifn.lpstrFile = fileName;
  40.         oifn.nMaxFile = 1024;
  41.         oifn.lpstrFilter = "";
  42.         oifn.nFilterIndex = 1;
  43.         oifn.lpstrFileTitle = NULL;
  44.         oifn.lpstrTitle = Title;
  45.         oifn.nMaxFileTitle = 0;
  46.         oifn.lpstrInitialDir = Dir;
  47.         oifn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  48.         if(!GetSaveFileName(&oifn))
  49.             return 0;
  50.         return 1;
  51. }
  52.  
  53. HWND CreateNormalWindow(WNDPROC WndProc, HINSTANCE hInstance, HWND hParent){
  54.         
  55.    return CreateDialog(hInstance, MAKEINTRESOURCE(IDD_DISPLAY), hParent, (DLGPROC)WndProc);
  56.     
  57.  
  58. }
  59.  
  60. void WindowShow(HWND hWnd){
  61.     ShowWindow(hWnd, SW_SHOW);
  62. }
  63. void WindowHide(HWND hWnd){
  64.     ShowWindow(hWnd, SW_HIDE);
  65. }
  66.  
  67. void WindowResize(HWND window, int w, int h){
  68.     RECT rc;
  69.     GetWindowRect(window, &rc);
  70.     MoveWindow(window, rc.left, rc.top, w, h , TRUE);
  71. }
  72.  
  73. void WindowClientResize(HWND window, int w, int h){
  74.     RECT rc;
  75.     SetRect(&rc, 0, 0, w, h);
  76.     AdjustWindowRectEx(&rc,GetWindowLong(window,GWL_STYLE), (int)GetMenu(window), GetWindowLong(window,GWL_EXSTYLE) );
  77.     WindowResize(window, rc.right-rc.left, rc.bottom - rc.top);
  78.     
  79. }
  80.  
  81. void WindowMove(HWND window, int x, int y){
  82.   SetWindowPos( window, NULL, x, y, 0, 0, SWP_NOACTIVATE|SWP_NOZORDER|SWP_NOSIZE);
  83. }
  84. void SetText(HWND hWnd, char *text){
  85.     if(text==NULL)
  86.         return;
  87.     SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)text);
  88. }
  89. void MenuSetText(HMENU hMenu,int item ,char *text ){
  90.     MENUITEMINFO item_info;
  91.  
  92.     if(text==NULL)
  93.         return;
  94.  
  95.     item_info.cbSize = sizeof(item_info);
  96. //    item_info.fMask  = MIIM_DATA|MIIM_ID|MIIM_STATE|MIIM_SUBMENU|MIIM_TYPE; 
  97. //    GetMenuItemInfo( hMenu, item, TRUE, &item_info);
  98.  
  99.     item_info.fMask  = MIIM_TYPE; 
  100.     item_info.fType         = MFT_STRING;
  101.     item_info.dwTypeData    = text;
  102.     SetMenuItemInfo( hMenu, item, TRUE, &item_info);
  103.  
  104. }
  105.  
  106. void MenuAddItem(HMENU hMenu,int item , int wID,char *text){
  107.     MENUITEMINFO item_info;
  108.  
  109.     if(text==NULL)
  110.         return;
  111.  
  112.     item_info.cbSize = sizeof(item_info);
  113.  
  114.     item_info.fMask  = MIIM_TYPE|MIIM_ID; 
  115.     item_info.fType         = MFT_STRING;
  116.     item_info.dwTypeData    = text;
  117.     item_info.wID           = wID;
  118.     InsertMenuItem( hMenu, item, TRUE, &item_info);
  119. }
  120.  
  121. void MenuAddSeparator(HMENU hMenu,int item ){
  122.     MENUITEMINFO item_info;
  123.  
  124.     item_info.cbSize = sizeof(item_info);
  125.  
  126.     item_info.fMask  = MIIM_TYPE; 
  127.     item_info.fType         = MFT_SEPARATOR;
  128.  
  129.     InsertMenuItem( hMenu, item, TRUE, &item_info);
  130. }
  131.  
  132. HMENU MenuGetPopup( HMENU hMenu, int item ){
  133.     MENUITEMINFO item_info;
  134.  
  135.     item_info.cbSize = sizeof(item_info);
  136.     item_info.fMask  = MIIM_SUBMENU; 
  137.     GetMenuItemInfo( hMenu, item, TRUE, &item_info);
  138.     return item_info.hSubMenu;
  139.  
  140. }
  141. void MenuCheck(HWND hWnd, int item)
  142. {
  143.     CheckMenuItem(GetMenu(hWnd),item, MF_CHECKED);
  144. }
  145. void MenuUnCheck(HWND hWnd, int item)
  146. {
  147.     CheckMenuItem(GetMenu(hWnd),item, MF_UNCHECKED);
  148.  
  149. }
  150. void MenuEnable(HWND hWnd, int item)
  151. {
  152.     EnableMenuItem(GetMenu(hWnd), item, MF_ENABLED);
  153. }
  154. void MenuDisable(HWND hWnd, int item)
  155. {
  156.     EnableMenuItem(GetMenu(hWnd), item, MF_GRAYED);
  157. }
  158. void MenuSetText(HWND hWnd, int item){
  159.  
  160. }
  161. void TabSetText(HWND hTab, int item, char *text){
  162.     TCITEM props;
  163.  
  164.     if(text==NULL)
  165.         return;
  166.     
  167.     props.mask    = TCIF_TEXT;
  168.     props.pszText = text;
  169.     SendMessage( hTab, TCM_SETITEM, (WPARAM) item, (LPARAM)&props);
  170.  
  171. }
  172. void DlgSetFocus(HWND hDlg, int control){
  173.     SetFocus(GetDlgItem(hDlg, control));
  174. }
  175.  
  176. void DlgAttachToolTip(HINSTANCE hInst, HWND hDlg, int nId, char *pTitle, char *pText)
  177. {
  178.   AttachTooltip( hInst, GetDlgItem(hDlg, nId ), pTitle, pText);
  179. }
  180. /* CREATE A TOOLTIP CONTROL OVER THE ENTIRE WINDOW AREA */
  181. void AttachTooltip (HINSTANCE hInst, HWND hwnd, char *pTitle, char *pText)
  182. {
  183.   // struct specifying control classes to register
  184.   INITCOMMONCONTROLSEX iccex; 
  185.   HWND hwndTT;                 // handle to the ToolTip control
  186.   // struct specifying info about tool in ToolTip control
  187.   TOOLINFO ti;
  188.   unsigned int uid = 0;       // for ti initialization
  189.  
  190.   LPTSTR lptstr = pText;
  191.   RECT rect;                  // for client area coordinates
  192.   
  193.   /* INITIALIZE COMMON CONTROLS */
  194.   iccex.dwICC = ICC_WIN95_CLASSES;
  195.   iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
  196.   InitCommonControlsEx(&iccex);
  197.   
  198.   /* CREATE A TOOLTIP WINDOW */
  199.   hwndTT = CreateWindowEx(WS_EX_TOPMOST,
  200.     TOOLTIPS_CLASS,
  201.     NULL,
  202.     WS_POPUP | TTS_NOPREFIX | TTS_BALLOON,        
  203.     CW_USEDEFAULT,
  204.     CW_USEDEFAULT,
  205.     CW_USEDEFAULT,
  206.     CW_USEDEFAULT,
  207.     hwnd,
  208.     NULL,
  209.     hInst,
  210.     NULL
  211.     );
  212.   
  213.   SetWindowPos(hwndTT,
  214.     HWND_TOPMOST,
  215.     0,
  216.     0,
  217.     0,
  218.     0,
  219.     SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
  220.   
  221.   /* GET COORDINATES OF THE MAIN CLIENT AREA */
  222.   GetClientRect (hwnd, &rect);
  223.   
  224.   /* INITIALIZE MEMBERS OF THE TOOLINFO STRUCTURE */
  225.   ti.cbSize = sizeof(TOOLINFO);
  226.   ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND ;
  227.   ti.hwnd = hwnd;
  228.   ti.hinst = hInst;
  229.   ti.uId = (UINT_PTR)hwnd;
  230.   ti.lpszText = pText;
  231.   // ToolTip control will cover the whole window
  232.   ti.rect = rect;    
  233.   
  234.   /* SEND AN ADDTOOL MESSAGE TO THE TOOLTIP CONTROL WINDOW */
  235.   SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti);
  236.   SendMessage(hwndTT, TTM_SETTITLE, 1, (LPARAM) pTitle);
  237.  
  238. void DlgCheck(HWND hDlg, int item, bool check)
  239. {
  240.   SendDlgItemMessage( hDlg, item, BM_SETCHECK, check ? BST_CHECKED:BST_UNCHECKED ,0);
  241. }
  242.  
  243. void DlgCheck(HWND hDlg, int item){
  244.  
  245.     SendDlgItemMessage( hDlg, item, BM_SETCHECK, BST_CHECKED ,0);
  246. }
  247.  
  248. void DlgUnCheck(HWND hDlg, int item){
  249.     SendDlgItemMessage( hDlg, item, BM_SETCHECK, BST_UNCHECKED ,0);
  250. }
  251.  
  252. bool DlgIsChecked(HWND hDlg, int item){
  253.     return (SendDlgItemMessage( hDlg, item, BM_GETCHECK, 0, 0)==BST_CHECKED);
  254. }
  255.  
  256. void DlgEnable(HWND hDlg, int item, bool enable)
  257. {
  258.      EnableWindow( GetDlgItem(hDlg, item), enable); 
  259. }
  260.  
  261. void DlgEnable(HWND hDlg, int item){
  262.      EnableWindow( GetDlgItem(hDlg, item), true); 
  263. }
  264. void DlgDisable(HWND hDlg, int item){
  265.          EnableWindow( GetDlgItem(hDlg, item), false); 
  266. }
  267.  
  268.  
  269. char *DlgGetText( HWND hDlg, int item, char* text){
  270.     if(text==NULL)
  271.         return NULL;
  272.  
  273.     GetDlgItemText( hDlg, item, text, 1024);
  274.     return text;
  275. }
  276. int DlgGetText( HWND hDlg, int item){
  277.     char szTemp[1024];
  278.     GetDlgItemText( hDlg, item, szTemp, 1024);
  279.     return atoi(szTemp);
  280. }
  281. void DlgSetText(HWND hDlg, int item, int number){
  282.  
  283.     char szTemp[1024];
  284.     SetDlgItemText(hDlg, item, itoa(number, szTemp, 10));
  285. }
  286. void DlgSetDouble(HWND hDlg, int item, double number){
  287.     char szTemp[1024];
  288.     sprintf(szTemp, "%.2f", number);
  289.     SetDlgItemText(hDlg, item, szTemp);
  290. }
  291.  
  292. void DlgSetText(HWND hDlg, int item, char* text){
  293.     if(text==NULL)
  294.         return;
  295.  
  296.     SetDlgItemText(hDlg, item, text);
  297. }
  298.  
  299. int DlgGetInt(HWND hDlg, int item){
  300.     char szTemp[1024];
  301.     GetDlgItemText( hDlg, item, szTemp, 1024);    
  302.     return atoi(szTemp);
  303. }
  304.  
  305. void DlgCheckBoxState(HWND hDlg, int item, int state)
  306. {
  307.     if(state)
  308.         DlgCheck(hDlg, item);
  309.     else
  310.         DlgUnCheck(hDlg, item);
  311. }
  312. // LIST BOXES
  313. void ListAddText(HWND list, char *text){
  314.     SendMessage(list, CB_ADDSTRING, 0, (LPARAM) text);
  315. }
  316. void ListClean(HWND list)
  317. {
  318.     SendMessage(list, CB_RESETCONTENT, 0, 0);
  319.     return;
  320. }
  321. int ListGetCur(HWND list){
  322.     return  SendMessage(list, CB_GETCURSEL, 0,0);
  323. }
  324. void ListSetCur(HWND list, int cur){
  325.     SendMessage(list, CB_SETCURSEL, cur, 0);
  326. }
  327. void MillisecondsToTime( char *str, DWORD time)
  328. {
  329.  
  330.     //Me pasan el tiempo que tengo que formatear en milisegundos
  331.     int segundos, minutos, horas;
  332.     if(str==NULL) return;
  333.     segundos= (int)floor(time/1000);
  334.     segundos=segundos%60;
  335.     minutos= (int)floor(time/60000);
  336.     minutos= minutos%60;
  337.     horas=   (int)floor((double)time/3600000);
  338.     if (minutos <=9)
  339.         if (segundos<=9)
  340.             sprintf( str, "%d:0%d:0%d", horas, minutos, segundos);
  341.         else
  342.             sprintf( str, "%d:0%d:%d", horas, minutos, segundos);
  343.     else
  344.         if (segundos<=9)
  345.             sprintf( str, "%d:%d:0%d", horas, minutos, segundos);
  346.         else
  347.             sprintf( str, "%d:%d:%d", horas, minutos, segundos);
  348.  
  349.     return;
  350. }
  351.  
  352. // takes the local time, adds msec milliseconds to it and writes a
  353. // string with the resulting time in hh:mm:ss format 
  354. // Sorry ((former) colonies of the) Brittish, none of that am/pm nonsense,
  355. // just good old 24hr format.
  356. void LocalTimePlusMilliseconds(char *str, DWORD msec, bool show_seconds)
  357. {
  358.     SYSTEMTIME loctime;
  359.     GetLocalTime(&loctime);
  360.     int hr,min,sec;
  361.     sec = loctime.wSecond + msec / 1000;
  362.     min = loctime.wMinute + sec / 60;
  363.     sec = sec % 60;
  364.     hr = loctime.wHour + min / 60;
  365.     min = min % 60;
  366.     hr = hr % 24;
  367.     if (show_seconds)
  368.         sprintf(str, "%d:%02d:%02d", hr, min, sec);
  369.     else
  370.         sprintf(str, "%d:%02d", hr, min);
  371. }
  372.  
  373. void StringFrameRate(int fr, char *sFrameRate)
  374. {
  375.         switch( fr )
  376.     {
  377.     case 23976://24000/1001
  378.       strcpy(sFrameRate, "FILM at 23.976 fps");
  379.       break;
  380.     case 24:
  381.       strcpy(sFrameRate, "FILM at 24 fps");
  382.       break;
  383.     case 25:
  384.       strcpy(sFrameRate, "PAL at 25 fps");
  385.       break;
  386.     case 2997:
  387.       strcpy(sFrameRate, "NTSC at 29.97 fps");
  388.       break;
  389.     case 30:
  390.       strcpy(sFrameRate, "NTSC at 30 fps");
  391.       break;
  392.     default:
  393.       strcpy(sFrameRate, "unknown");
  394.     }
  395. }
  396.  
  397. #define isvalidchar(c) (((c>=65 && c<=90) ||  (c>=97 && c<=122)  || (c>=48 && c<=57)))
  398.  
  399. // Make a file name, valid for the file system
  400. bool validateFileName( char *filename )
  401. {
  402.   int len = strlen( filename );
  403.   char *i, *o;
  404.   
  405.   i = o = filename;
  406.   while(len--)
  407.   {
  408.     if( isvalidchar(*i) )
  409.       *o++ = *i;
  410.     *i++;
  411.   }
  412.   *o=0;
  413.  
  414.   return i==o;
  415. }
  416.