/[winpt]/trunk/Src/wptW32API.cpp
ViewVC logotype

Diff of /trunk/Src/wptW32API.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 77 by twoaday, Mon Nov 14 15:01:01 2005 UTC revision 137 by twoaday, Mon Jan 9 14:01:51 2006 UTC
# Line 1  Line 1 
1  /* wptW32API.cpp - Common W32 API functions  /* wptW32API.cpp - Common W32 API functions
2   *      Copyright (C) 2001, 2002, 2003 Timo Schulz   *      Copyright (C) 2001, 2002, 2003, 2005 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
5   *   *
# Line 29  Line 29 
29  #include <shellapi.h>  #include <shellapi.h>
30  #include <shlobj.h>  #include <shlobj.h>
31  #include <commctrl.h>  #include <commctrl.h>
32    #include <time.h>
33    
34  #include "wptNLS.h"  #include "wptNLS.h"
35  #include "wptW32API.h"  #include "wptW32API.h"
# Line 56  set_menu_text_ext (HMENU menu, int by_po Line 57  set_menu_text_ext (HMENU menu, int by_po
57    
58  /* Set the text of a menu item @m_uid to @text. */  /* Set the text of a menu item @m_uid to @text. */
59  void  void
60  set_menu_text (HMENU menu, int m_uid, const char *text)  set_menu_text (HMENU menu, UINT m_uid, const char *text)
61  {  {
62      set_menu_text_ext (menu, 0, m_uid, text);      set_menu_text_ext (menu, 0, m_uid, text);
63  }  }
# Line 64  set_menu_text (HMENU menu, int m_uid, co Line 65  set_menu_text (HMENU menu, int m_uid, co
65    
66  /* Set the text of a menu item with the position @pos to @text. */  /* Set the text of a menu item with the position @pos to @text. */
67  void  void
68  set_menu_text_bypos (HMENU menu, int pos, const char *text)  set_menu_text_bypos (HMENU menu, UINT pos, const char *text)
69  {  {
70      set_menu_text_ext (menu, 1, pos, text);      set_menu_text_ext (menu, 1, pos, text);
71  }  }
# Line 72  set_menu_text_bypos (HMENU menu, int pos Line 73  set_menu_text_bypos (HMENU menu, int pos
73    
74  /* Set the state of a menu item @m_uid to @state. */  /* Set the state of a menu item @m_uid to @state. */
75  void  void
76  set_menu_state (HMENU menu, int m_uid, int state)  set_menu_state (HMENU menu, UINT m_uid, UINT state)
77  {        {      
78      MENUITEMINFO mii;      MENUITEMINFO mii;
79    
80      memset( &mii, 0, sizeof (mii) );      memset (&mii, 0, sizeof (mii));
81      mii.cbSize = sizeof (mii);      mii.cbSize = sizeof (mii);
82      mii.fMask = MIIM_STATE;      mii.fMask = MIIM_STATE;
83      mii.fState = state;      mii.fState = state;
# Line 84  set_menu_state (HMENU menu, int m_uid, i Line 85  set_menu_state (HMENU menu, int m_uid, i
85  }  }
86    
87    
88    /* Retrieve the state of the menu item @m_uid and return it. */
89    UINT
90    get_menu_state (HMENU menu, UINT m_uid)
91    {
92        MENUITEMINFO mii;
93    
94        memset (&mii, 0, sizeof (mii));
95        mii.cbSize = sizeof (mii);
96        mii.fMask = MIIM_STATE;
97        GetMenuItemInfo (menu, m_uid, FALSE, &mii);
98        return mii.fState;
99    }
100    
101    
102  enum {  enum {
103      CDLG_FILE_OPEN = 0,      CDLG_FILE_OPEN = 0,
104      CDLG_FILE_SAVE = 1      CDLG_FILE_SAVE = 1
# Line 135  get_filesave_dlg (HWND hwnd, const char Line 150  get_filesave_dlg (HWND hwnd, const char
150      return get_filename_dlg (hwnd, CDLG_FILE_SAVE, title, filter, name);      return get_filename_dlg (hwnd, CDLG_FILE_SAVE, title, filter, name);
151  }  }
152    
153  const char *  const char*
154  get_fileopen_dlg (HWND hwnd, const char *title, const char *filter,  get_fileopen_dlg (HWND hwnd, const char *title, const char *filter,
155                    const char *name)                    const char *name)
156  {  {
# Line 146  get_fileopen_dlg (HWND hwnd, const char Line 161  get_fileopen_dlg (HWND hwnd, const char
161  /* Use the common dialog to allow the user to select a folder.  /* Use the common dialog to allow the user to select a folder.
162     The return value is either the folder path or NULL if cancel was chosen. */     The return value is either the folder path or NULL if cancel was chosen. */
163  const char*  const char*
164  get_folder_dlg (HWND hwnd, const char * title, const char * name)  get_folder_dlg (HWND hwnd, const char *title, const char *name)
165  {  {
166      static char folder[MAX_PATH+1] = "";      static char folder[MAX_PATH+1] = "";
167      BROWSEINFO bi;      BROWSEINFO bi;
168      ITEMIDLIST * il;      ITEMIDLIST *il;
169    
170      memset (&bi, 0, sizeof (bi));      memset (&bi, 0, sizeof (bi));
171      bi.hwndOwner = hwnd;      bi.hwndOwner = hwnd;
# Line 262  set_clip_text2 (HWND hwnd, const char *t Line 277  set_clip_text2 (HWND hwnd, const char *t
277  }  }
278    
279    
280  /* Make a file name out of the path, the file and an extension. */  /* Make a file name out of the path @path, the file @file and
281       an extension. @ext.
282       Return value: the full file name on success. */
283  char*  char*
284  make_filename (const char *path, const char *file, const char *ext)  make_filename (const char *path, const char *file, const char *ext)
285  {  {
# Line 289  make_filename (const char *path, const c Line 306  make_filename (const char *path, const c
306          strcat( p, ext );          strcat( p, ext );
307      }      }
308      return p;      return p;
309  } /* make_filename */  }
310    
311    
312  /* return 0 if it exists, otherwise >0. */  /* return 0 if the file @fname exists, otherwise >0. */
313  int  int
314  file_exist_check (const char * fname)  file_exist_check (const char *fname)
315  {  {
316      struct stat st;      struct stat st;
317      if (stat (fname, &st) == -1)      if (stat (fname, &st) == -1)
# Line 310  dir_exist_check (const char *dir) Line 327  dir_exist_check (const char *dir)
327  {  {
328      struct stat statbuf;          struct stat statbuf;    
329            
330      if( stat( dir, &statbuf ) == -1 )      if (stat (dir, &statbuf) == -1)
331          return WPTERR_GENERAL;          return WPTERR_GENERAL;
332      if( statbuf.st_mode & _S_IFDIR )      if (statbuf.st_mode & _S_IFDIR)
333          return 0;          return 0;
334      return WPTERR_GENERAL;      return WPTERR_GENERAL;
335  }  }
# Line 337  get_file_size (const char *fname) Line 354  get_file_size (const char *fname)
354  }  }
355    
356    
 int  
 init_file_lock( LOCK *ctx, const char *file )  
 {  
       
     ctx->size = get_file_size( file );  
     ctx->file = m_strdup( file );  
     ctx->fh = CreateFile( file, GENERIC_READ, FILE_SHARE_READ, NULL,  
                          OPEN_ALWAYS, 0, NULL );  
     if( ctx->fh == INVALID_HANDLE_VALUE )  
         return WPTERR_GENERAL;  
     if( LockFile( ctx->fh, 0, 0, ctx->size, 0 ) == FALSE ) {  
         CloseHandle( ctx->fh );  
         ctx->fh = INVALID_HANDLE_VALUE;  
         ctx->size = 0;  
         free( ctx->file );  
         return WPTERR_GENERAL;  
     }  
     return 0;  
 } /* init_file_lock */  
   
   
 void  
 release_file_lock( LOCK *ctx )  
 {  
     free_if_alloc( ctx->file );  
     ctx->file = NULL;  
     ctx->size = 0;  
     CloseHandle( ctx->fh );  
 } /* release_file_lock */  
   
   
357  /* Start a dialog with the exception that before it is checked that the  /* Start a dialog with the exception that before it is checked that the
358     dialog is not already openened. */     dialog is not already openened. */
359  int  int
360  dialog_box_param( HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,  dialog_box_param (HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,
361                                    LPARAM param, LPCTSTR title, int title_id )                    LPARAM param, LPCTSTR title, int title_id)
362  {  {
363      #ifndef LANG_DE      if (FindWindowEx (GetDesktopWindow (), NULL, NULL, title))
     if( FindWindowEx( GetDesktopWindow(), NULL, NULL, title ) )  
         return -1;        
     #else  
     char strdesc[256];  
     LoadString( glob_hinst, title_id, strdesc, sizeof (strdesc) -1 );  
     if( FindWindowEx( GetDesktopWindow(), NULL, NULL, strdesc ) )  
364          return -1;          return -1;
365      #endif      return DialogBoxParam (hinst, name, parent, fnc, param);
366        }
     return DialogBoxParam( hinst, name, parent, fnc, param );  
 } /* dialog_box_param */  
367    
368    
369  /* Wrapper for message box which forces the message box into the  /* Wrapper for message box which forces the message box into the
# Line 400  msg_box (HWND hwnd, const char *text, co Line 378  msg_box (HWND hwnd, const char *text, co
378  }  }
379    
380    
381  void  /* Safe strdup version (C++ version). */
 set_active_window( HWND dlg)  
 {        
     activ_hwnd = dlg;  
 } /* set_active_window */  
   
 void  
 reset_active_window( void )  
 {        
     activ_hwnd = NULL;  
 } /* reset_active_window */  
   
   
 static DWORD CALLBACK  
 reminder_thread (void *ctx)  
 {  
     reminder_ctx_s *c = (reminder_ctx_s *)ctx;  
   
     Sleep( c->msecs );  
     SetForegroundWindow( activ_hwnd );  
   
     return 0;  
 } /* reminder_thread */  
   
   
 HANDLE  
 window_reminder( struct reminder_ctx_s *ctx )  
 {  
     DWORD tid = 0;  
       
     return CreateThread( NULL, 0, reminder_thread, ctx, 0, &tid );  
 } /* window_reminder */  
   
   
382  char*  char*
383  m_strdup (const char *str)  m_strdup (const char *str)
384  {  {
385      char * p = new char[strlen (str) + 1];      char *p = new char[strlen (str) + 1];
386      if (p)      if (!p)
387          strcpy (p, str);          BUG (NULL);
388        strcpy (p, str);
389      return p;      return p;
390  } /* m_strdup */  }
391    
392    
393  /* Center the hwndChild relative to parent.  /* Center the hwndChild relative to parent.
# Line 493  center_window (HWND hwndChild, HWND hwnd Line 439  center_window (HWND hwndChild, HWND hwnd
439  {  {
440      center_window2 (hwndChild, hwndParent, NULL);      center_window2 (hwndChild, hwndParent, NULL);
441  }  }
442    
443    
444    /* Retrieve the product verion of the given file @fname.
445       Format: MAJOR.MINOR.PATCH1.PATCH2
446       Return value: 0 on success. */
447    int
448    get_file_version (const char *fname, WORD *major, WORD *minor,
449                      WORD *patch1, WORD *patch2)
450    {
451        VS_FIXEDFILEINFO *inf = {0};
452        char file[MAX_PATH+1] = {0};
453        LPVOID buf, data;
454        DWORD arg;
455        DWORD size;
456        UINT qlen;
457    
458        strncpy (file, fname, MAX_PATH);
459        size = GetFileVersionInfoSize (file, &arg);
460        if (!size)
461            return -1;
462        buf = (LPVOID)new CHAR[size];
463        if (!buf)
464            BUG (NULL);
465        GetFileVersionInfo (file, 0, size, buf);
466    
467        qlen=0;
468        VerQueryValue (buf, "\\", &data, &qlen);
469        if (!qlen) {
470            delete [] (char*)buf;
471            return -1;
472        }
473        inf = (VS_FIXEDFILEINFO*)data;
474    
475        if (major)
476            *major = HIWORD (inf->dwProductVersionMS);
477        if (minor)
478            *minor = LOWORD (inf->dwProductVersionMS);
479        if (patch1)
480            *patch1 = HIWORD (inf->dwProductVersionLS);    
481        if (patch2)
482            *patch2 = LOWORD (inf->dwProductVersionLS);
483    
484        delete [] (char*)buf;
485        return 0;
486    }
487    
488    
489    /* Return date in a format which complies with the
490       system locale settings. */
491    const char*
492    get_locale_date (long tm_t, char *buf, DWORD buflen)
493    {
494        SYSTEMTIME st;
495        struct tm *ptm;
496    
497        ptm = localtime (&tm_t);  
498        st.wYear = (WORD)ptm->tm_year;
499        st.wMonth = (WORD)ptm->tm_mon;
500        st.wDay = (WORD)ptm->tm_mday;
501        st.wYear += 1900;
502        st.wMonth += 1;
503        if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st,
504                            NULL, buf, buflen))
505            return NULL;
506        return buf;
507    }
508    
509    
510    struct reminder_hd_s {
511        int msecs;
512        HWND dlg;
513        HANDLE hd;
514    };
515    
516    
517    static DWORD CALLBACK
518    foreground_reminder_thread (void *c)
519    {
520        struct reminder_hd_s *ctx = (struct reminder_hd_s *)c;
521        Sleep (ctx->msecs);
522        SetForegroundWindow (ctx->dlg);
523        CloseHandle (ctx->hd);
524        delete ctx;
525        ExitThread (0);
526        return 0;
527    }
528    
529    /* Try to force the window @dlg to the foreground.
530       On NT5 or later this will not work if the user
531       is working in another window (console for example). */
532    void
533    force_foreground_window (HWND dlg, int msecs)
534    {
535        struct reminder_hd_s *hd;
536        DWORD tid;
537    
538        hd = new reminder_hd_s;
539        hd->dlg = dlg;
540        hd->msecs = msecs;
541        hd->hd = CreateThread (NULL, 0, foreground_reminder_thread,
542                               hd, NULL, &tid);
543    }

Legend:
Removed from v.77  
changed lines
  Added in v.137

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26