/[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 167 by twoaday, Thu Jan 26 10:17:17 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 104  get_filename_dlg (HWND hwnd, int id, con Line 119  get_filename_dlg (HWND hwnd, int id, con
119      else      else
120          memset (file, 0, sizeof (file));          memset (file, 0, sizeof (file));
121      if (!filter)      if (!filter)
122          filter = _("All Files (*.*)\0*.*\0\0");          filter = "All Files (*.*)\0*.*\0\0";
123      /* XXX: problem with gettext because of the 'artificial'      /* XXX: problem with gettext because of the 'artificial'
124              double string termination!. */              double string termination!. */
125      memset (&open, 0, sizeof (open));      memset (&open, 0, sizeof (open));
# 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    /* Generate a file name from a special dirctory. */
313    char*
314    make_special_filename (int folder, const char *file, const char *ext)
315    {
316        BOOL ec;
317        char path[MAX_PATH], *p;
318        size_t n=0;
319    
320        /* MSDN: buf must be at least MAX_PATH=256 bytes */
321        ec = SHGetSpecialFolderPath (HWND_DESKTOP, path, folder, TRUE);
322        if (ec != 1) {
323            log_debug ("SHGetSpecialFolderPath() failed\r\n", (int)GetLastError ());
324            return NULL;
325        }
326    
327        n = strlen (path)+1;
328        if (file)
329            n += strlen (file)+1;
330        if (ext)
331            n += strlen (ext)+1;
332        p = new char[n+2];
333        if (!p)
334            BUG (0);
335        memset (p, 0, n+2);
336        strcpy (p, path);    
337        if (file) {
338            strcat (p, "\\");
339            strcat (p, file);
340        }
341        if (ext)
342            strcat (p, ext);
343        return p;
344    }
345    
346    
347  /* return 0 if it exists, otherwise >0. */  /* return 0 if the file @fname exists, otherwise >0. */
348  int  int
349  file_exist_check (const char * fname)  file_exist_check (const char *fname)
350  {  {
351      struct stat st;      struct stat st;
352      if (stat (fname, &st) == -1)      if (stat (fname, &st) == -1)
# Line 310  dir_exist_check (const char *dir) Line 362  dir_exist_check (const char *dir)
362  {  {
363      struct stat statbuf;          struct stat statbuf;    
364            
365      if( stat( dir, &statbuf ) == -1 )      if (stat (dir, &statbuf) == -1)
366          return WPTERR_GENERAL;          return WPTERR_GENERAL;
367      if( statbuf.st_mode & _S_IFDIR )      if (statbuf.st_mode & _S_IFDIR)
368          return 0;          return 0;
369      return WPTERR_GENERAL;      return WPTERR_GENERAL;
370  }  }
# Line 337  get_file_size (const char *fname) Line 389  get_file_size (const char *fname)
389  }  }
390    
391    
 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 */  
   
   
392  /* 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
393     dialog is not already openened. */     dialog is not already openened. */
394  int  int
395  dialog_box_param( HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,  dialog_box_param (HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,
396                                    LPARAM param, LPCTSTR title, int title_id )                    LPARAM param, LPCTSTR title, int title_id)
397  {  {
398      #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 ) )  
399          return -1;          return -1;
400      #endif      return DialogBoxParam (hinst, name, parent, fnc, param);
401        }
     return DialogBoxParam( hinst, name, parent, fnc, param );  
 } /* dialog_box_param */  
402    
403    
404  /* 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 413  msg_box (HWND hwnd, const char *text, co
413  }  }
414    
415    
416  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 */  
   
   
417  char*  char*
418  m_strdup (const char *str)  m_strdup (const char *str)
419  {  {
420      char * p = new char[strlen (str) + 1];      char *p = new char[strlen (str) + 1];
421      if (p)      if (!p)
422          strcpy (p, str);          BUG (NULL);
423        strcpy (p, str);
424      return p;      return p;
425  } /* m_strdup */  }
426    
427    
428  /* Center the hwndChild relative to parent.  /* Center the hwndChild relative to parent.
# Line 493  center_window (HWND hwndChild, HWND hwnd Line 474  center_window (HWND hwndChild, HWND hwnd
474  {  {
475      center_window2 (hwndChild, hwndParent, NULL);      center_window2 (hwndChild, hwndParent, NULL);
476  }  }
477    
478    
479    /* Retrieve the product verion of the given file @fname.
480       Format: MAJOR.MINOR.PATCH1.PATCH2
481       Return value: 0 on success. */
482    int
483    get_file_version (const char *fname, WORD *major, WORD *minor,
484                      WORD *patch1, WORD *patch2)
485    {
486        VS_FIXEDFILEINFO *inf = {0};
487        char file[MAX_PATH+1] = {0};
488        LPVOID buf, data;
489        DWORD arg;
490        DWORD size;
491        UINT qlen;
492    
493        strncpy (file, fname, MAX_PATH);
494        size = GetFileVersionInfoSize (file, &arg);
495        if (!size)
496            return -1;
497        buf = (LPVOID)new CHAR[size];
498        if (!buf)
499            BUG (NULL);
500        GetFileVersionInfo (file, 0, size, buf);
501    
502        qlen=0;
503        VerQueryValue (buf, "\\", &data, &qlen);
504        if (!qlen) {
505            delete [] (char*)buf;
506            return -1;
507        }
508        inf = (VS_FIXEDFILEINFO*)data;
509    
510        if (major)
511            *major = HIWORD (inf->dwProductVersionMS);
512        if (minor)
513            *minor = LOWORD (inf->dwProductVersionMS);
514        if (patch1)
515            *patch1 = HIWORD (inf->dwProductVersionLS);    
516        if (patch2)
517            *patch2 = LOWORD (inf->dwProductVersionLS);
518    
519        delete [] (char*)buf;
520        return 0;
521    }
522    
523    
524    /* Return date in a format which complies with the
525       system locale settings. */
526    const char*
527    get_locale_date (long tm_t, char *buf, DWORD buflen)
528    {
529        SYSTEMTIME st;
530        struct tm *ptm;
531    
532        ptm = localtime (&tm_t);  
533        st.wYear = (WORD)ptm->tm_year;
534        st.wMonth = (WORD)ptm->tm_mon;
535        st.wDay = (WORD)ptm->tm_mday;
536        st.wYear += 1900;
537        st.wMonth += 1;
538        if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st,
539                            NULL, buf, buflen))
540            return NULL;
541        return buf;
542    }
543    
544    
545    struct reminder_hd_s {
546        int msecs;
547        HWND dlg;
548        HANDLE hd;
549    };
550    
551    
552    static DWORD CALLBACK
553    foreground_reminder_thread (void *c)
554    {
555        struct reminder_hd_s *ctx = (struct reminder_hd_s *)c;
556        Sleep (ctx->msecs);
557        SetForegroundWindow (ctx->dlg);
558        CloseHandle (ctx->hd);
559        delete ctx;
560        ExitThread (0);
561        return 0;
562    }
563    
564    /* Try to force the window @dlg to the foreground.
565       On NT5 or later this will not work if the user
566       is working in another window (console for example). */
567    void
568    force_foreground_window (HWND dlg, int msecs)
569    {
570        struct reminder_hd_s *hd;
571        DWORD tid;
572    
573        hd = new reminder_hd_s;
574        hd->dlg = dlg;
575        hd->msecs = msecs;
576        hd->hd = CreateThread (NULL, 0, foreground_reminder_thread,
577                               hd, NULL, &tid);
578    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26