/[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 204 by twoaday, Wed Apr 26 06:37:23 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"
36  #include "wptErrors.h"  #include "wptErrors.h"
37  #include "wptVersion.h"  #include "wptVersion.h"
38    #include "wptUTF8.h"
39  #include "wptTypes.h"  #include "wptTypes.h"
40    
41    
42  extern "C" void _SHFree (void *p);  extern "C" void _SHFree (void *p);
43    
44    
45    
46    /* Insert a new item into the menu @hm at position @pos. */
47    void
48    insert_menu_item (HMENU hm, int pos, UINT m_id, const char *text)
49    {
50        MENUITEMINFO mi;
51    
52        memset (&mi, 0, sizeof mi);
53        mi.cbSize = sizeof mi;
54        mi.fType = MF_STRING;
55        mi.dwTypeData = (char *)text;
56        mi.cch = strlen (text);
57        mi.wID = m_id;
58        mi.fMask = MIIM_ID|MIIM_DATA| MIIM_TYPE;
59        InsertMenuItem (hm, pos, FALSE, &mi);
60    }
61    
62    
63  static void  static void
64  set_menu_text_ext (HMENU menu, int by_pos, int m_uid, const char *text)  set_menu_text_ext (HMENU menu, int by_pos, int m_uid, const char *text)
65  {  {
# Line 56  set_menu_text_ext (HMENU menu, int by_po Line 76  set_menu_text_ext (HMENU menu, int by_po
76    
77  /* Set the text of a menu item @m_uid to @text. */  /* Set the text of a menu item @m_uid to @text. */
78  void  void
79  set_menu_text (HMENU menu, int m_uid, const char *text)  set_menu_text (HMENU menu, UINT m_uid, const char *text)
80  {  {
81      set_menu_text_ext (menu, 0, m_uid, text);      set_menu_text_ext (menu, 0, m_uid, text);
82  }  }
# Line 64  set_menu_text (HMENU menu, int m_uid, co Line 84  set_menu_text (HMENU menu, int m_uid, co
84    
85  /* 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. */
86  void  void
87  set_menu_text_bypos (HMENU menu, int pos, const char *text)  set_menu_text_bypos (HMENU menu, UINT pos, const char *text)
88  {  {
89      set_menu_text_ext (menu, 1, pos, text);      set_menu_text_ext (menu, 1, pos, text);
90  }  }
# Line 72  set_menu_text_bypos (HMENU menu, int pos Line 92  set_menu_text_bypos (HMENU menu, int pos
92    
93  /* Set the state of a menu item @m_uid to @state. */  /* Set the state of a menu item @m_uid to @state. */
94  void  void
95  set_menu_state (HMENU menu, int m_uid, int state)  set_menu_state (HMENU menu, UINT m_uid, UINT state)
96  {        {      
97      MENUITEMINFO mii;      MENUITEMINFO mii;
98    
99      memset( &mii, 0, sizeof (mii) );      memset (&mii, 0, sizeof (mii));
100      mii.cbSize = sizeof (mii);      mii.cbSize = sizeof (mii);
101      mii.fMask = MIIM_STATE;      mii.fMask = MIIM_STATE;
102      mii.fState = state;      mii.fState = state;
# Line 84  set_menu_state (HMENU menu, int m_uid, i Line 104  set_menu_state (HMENU menu, int m_uid, i
104  }  }
105    
106    
107    /* Retrieve the state of the menu item @m_uid and return it. */
108    UINT
109    get_menu_state (HMENU menu, UINT m_uid)
110    {
111        MENUITEMINFO mii;
112    
113        memset (&mii, 0, sizeof (mii));
114        mii.cbSize = sizeof (mii);
115        mii.fMask = MIIM_STATE;
116        GetMenuItemInfo (menu, m_uid, FALSE, &mii);
117        return mii.fState;
118    }
119    
120    
121  enum {  enum {
122      CDLG_FILE_OPEN = 0,      CDLG_FILE_OPEN = 0,
123      CDLG_FILE_SAVE = 1      CDLG_FILE_SAVE = 1
# Line 93  enum { Line 127  enum {
127     id can be either FILE_OPEN or FILE_SAVE.     id can be either FILE_OPEN or FILE_SAVE.
128     The return value is the file name or NULL if cancel was chosen. */     The return value is the file name or NULL if cancel was chosen. */
129  const char *  const char *
130  get_filename_dlg (HWND hwnd, int id, const char * title,  get_filename_dlg (HWND hwnd, int id, const char *title,
131                    const char * filter, const char * name)                    const char *filter, const char *name)
132  {  {
133      static char file[512] = "";      static char file[512] = "";
134      OPENFILENAME open;      OPENFILENAME open;
# Line 104  get_filename_dlg (HWND hwnd, int id, con Line 138  get_filename_dlg (HWND hwnd, int id, con
138      else      else
139          memset (file, 0, sizeof (file));          memset (file, 0, sizeof (file));
140      if (!filter)      if (!filter)
141          filter = _("All Files (*.*)\0*.*\0\0");          filter = "All Files (*.*)\0*.*\0\0";
142      /* XXX: problem with gettext because of the 'artificial'      /* XXX: problem with gettext because of the 'artificial'
143              double string termination!. */              double string termination!. */
144      memset (&open, 0, sizeof (open));      memset (&open, 0, sizeof (open));
# Line 135  get_filesave_dlg (HWND hwnd, const char Line 169  get_filesave_dlg (HWND hwnd, const char
169      return get_filename_dlg (hwnd, CDLG_FILE_SAVE, title, filter, name);      return get_filename_dlg (hwnd, CDLG_FILE_SAVE, title, filter, name);
170  }  }
171    
172  const char *  const char*
173  get_fileopen_dlg (HWND hwnd, const char *title, const char *filter,  get_fileopen_dlg (HWND hwnd, const char *title, const char *filter,
174                    const char *name)                    const char *name)
175  {  {
# Line 146  get_fileopen_dlg (HWND hwnd, const char Line 180  get_fileopen_dlg (HWND hwnd, const char
180  /* Use the common dialog to allow the user to select a folder.  /* Use the common dialog to allow the user to select a folder.
181     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. */
182  const char*  const char*
183  get_folder_dlg (HWND hwnd, const char * title, const char * name)  get_folder_dlg (HWND hwnd, const char *title, const char *name)
184  {  {
185      static char folder[MAX_PATH+1] = "";      static char folder[MAX_PATH+1] = "";
186      BROWSEINFO bi;      BROWSEINFO bi;
187      ITEMIDLIST * il;      ITEMIDLIST *il;
188    
189      memset (&bi, 0, sizeof (bi));      memset (&bi, 0, sizeof (bi));
190      bi.hwndOwner = hwnd;      bi.hwndOwner = hwnd;
# Line 223  set_clip_text (HWND hwnd, const char *te Line 257  set_clip_text (HWND hwnd, const char *te
257          BUG (NULL);          BUG (NULL);
258      p = (char *) GlobalLock (clipmem);      p = (char *) GlobalLock (clipmem);
259      if (p == NULL) {      if (p == NULL) {
260          rc = WPTERR_GENERAL;;          CloseClipboard ();
261          goto leave;              GlobalFree (clipmem);
262            return WPTERR_GENERAL;
263      }      }
264      memcpy (p, text, nbytes);      memcpy (p, text, nbytes);
265      p[nbytes] = '\0';      p[nbytes] = '\0';
266            
     GlobalUnlock (clipmem);  
267      SetClipboardData (CF_TEXT, clipmem);      SetClipboardData (CF_TEXT, clipmem);
268            GlobalUnlock (clipmem);
 leave:  
269      CloseClipboard ();      CloseClipboard ();
270        GlobalFree (clipmem);
271        
272      return rc;      return rc;
273  } /* set_clip_text */  }
274    
275    
276  /* Append or prepend some text to the clipboard contents.  /* Append or prepend some text to the clipboard contents.
# Line 262  set_clip_text2 (HWND hwnd, const char *t Line 297  set_clip_text2 (HWND hwnd, const char *t
297  }  }
298    
299    
300  /* 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
301       an extension. @ext.
302       Return value: the full file name on success. */
303  char*  char*
304  make_filename (const char *path, const char *file, const char *ext)  make_filename (const char *path, const char *file, const char *ext)
305  {  {
# Line 289  make_filename (const char *path, const c Line 326  make_filename (const char *path, const c
326          strcat( p, ext );          strcat( p, ext );
327      }      }
328      return p;      return p;
329  } /* make_filename */  }
330    
331    
332    /* Generate a file name from a special dirctory. */
333    char*
334    make_special_filename (int folder, const char *file, const char *ext)
335    {
336        BOOL ec;
337        char path[MAX_PATH], *p;
338        size_t n=0;
339    
340        /* MSDN: buf must be at least MAX_PATH=256 bytes */
341        ec = SHGetSpecialFolderPath (HWND_DESKTOP, path, folder, TRUE);
342        if (ec != 1) {
343            log_debug ("SHGetSpecialFolderPath() failed\r\n", (int)GetLastError ());
344            return NULL;
345        }
346    
347        n = strlen (path)+1;
348        if (file)
349            n += strlen (file)+1;
350        if (ext)
351            n += strlen (ext)+1;
352        p = new char[n+2];
353        if (!p)
354            BUG (0);
355        memset (p, 0, n+2);
356        strcpy (p, path);    
357        if (file) {
358            strcat (p, "\\");
359            strcat (p, file);
360        }
361        if (ext)
362            strcat (p, ext);
363        return p;
364    }
365    
366    
367  /* return 0 if it exists, otherwise >0. */  /* return 0 if the file @fname exists, otherwise >0. */
368  int  int
369  file_exist_check (const char * fname)  file_exist_check (const char *fname)
370  {  {
371      struct stat st;      struct stat st;
372      if (stat (fname, &st) == -1)      if (stat (fname, &st) == -1)
# Line 310  dir_exist_check (const char *dir) Line 382  dir_exist_check (const char *dir)
382  {  {
383      struct stat statbuf;          struct stat statbuf;    
384            
385      if( stat( dir, &statbuf ) == -1 )      if (stat (dir, &statbuf) == -1)
386          return WPTERR_GENERAL;          return WPTERR_GENERAL;
387      if( statbuf.st_mode & _S_IFDIR )      if (statbuf.st_mode & _S_IFDIR)
388          return 0;          return 0;
389      return WPTERR_GENERAL;      return WPTERR_GENERAL;
390  }  }
# Line 337  get_file_size (const char *fname) Line 409  get_file_size (const char *fname)
409  }  }
410    
411    
 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 */  
   
   
412  /* 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
413     dialog is not already openened. */     dialog is not already openened. */
414  int  int
415  dialog_box_param( HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,  dialog_box_param (HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,
416                                    LPARAM param, LPCTSTR title, int title_id )                    LPARAM param, LPCTSTR title, int title_id)
417  {  {
418      #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 ) )  
419          return -1;          return -1;
420      #endif      return DialogBoxParam (hinst, name, parent, fnc, param);
421        }
     return DialogBoxParam( hinst, name, parent, fnc, param );  
 } /* dialog_box_param */  
422    
423    
424  /* 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 433  msg_box (HWND hwnd, const char *text, co
433  }  }
434    
435    
436  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 */  
   
   
437  char*  char*
438  m_strdup (const char *str)  m_strdup (const char *str)
439  {  {
440      char * p = new char[strlen (str) + 1];      char *p = new char[strlen (str) + 1];
441      if (p)      if (!p)
442          strcpy (p, str);          BUG (NULL);
443        strcpy (p, str);
444      return p;      return p;
445  } /* m_strdup */  }
446    
447    
448  /* Center the hwndChild relative to parent.  /* Center the hwndChild relative to parent.
# Line 493  center_window (HWND hwndChild, HWND hwnd Line 494  center_window (HWND hwndChild, HWND hwnd
494  {  {
495      center_window2 (hwndChild, hwndParent, NULL);      center_window2 (hwndChild, hwndParent, NULL);
496  }  }
497    
498    
499    /* Retrieve the product verion of the given file @fname.
500       Format: MAJOR.MINOR.PATCH1.PATCH2
501       Return value: 0 on success. */
502    int
503    get_file_version (const char *fname, WORD *major, WORD *minor,
504                      WORD *patch1, WORD *patch2)
505    {
506        VS_FIXEDFILEINFO *inf = {0};
507        char file[MAX_PATH+1] = {0};
508        LPVOID buf, data;
509        DWORD arg;
510        DWORD size;
511        UINT qlen;
512    
513        strncpy (file, fname, MAX_PATH);
514        size = GetFileVersionInfoSize (file, &arg);
515        if (!size)
516            return -1;
517        buf = (LPVOID)new CHAR[size];
518        if (!buf)
519            BUG (NULL);
520        GetFileVersionInfo (file, 0, size, buf);
521    
522        qlen=0;
523        VerQueryValue (buf, "\\", &data, &qlen);
524        if (!qlen) {
525            delete [] (char*)buf;
526            return -1;
527        }
528        inf = (VS_FIXEDFILEINFO*)data;
529    
530        if (major)
531            *major = HIWORD (inf->dwProductVersionMS);
532        if (minor)
533            *minor = LOWORD (inf->dwProductVersionMS);
534        if (patch1)
535            *patch1 = HIWORD (inf->dwProductVersionLS);    
536        if (patch2)
537            *patch2 = LOWORD (inf->dwProductVersionLS);
538    
539        delete [] (char*)buf;
540        return 0;
541    }
542    
543    
544    /* Return date in a format which complies with the
545       system locale settings. */
546    const char*
547    get_locale_date (long tm_t, char *buf, DWORD buflen)
548    {
549        SYSTEMTIME st;
550        struct tm *ptm;
551    
552        ptm = localtime (&tm_t);  
553        st.wYear = (WORD)ptm->tm_year;
554        st.wMonth = (WORD)ptm->tm_mon;
555        st.wDay = (WORD)ptm->tm_mday;
556        st.wYear += 1900;
557        st.wMonth += 1;
558        if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st,
559                            NULL, buf, buflen))
560            return NULL;
561        return buf;
562    }
563    
564    
565    /* Generate a temporary file name by using the users
566       temp path and optionally a name @name provided by the caller.
567       Return value: 0 on success. */
568    int
569    get_temp_name (char *buf, DWORD buflen, const char *name)
570    {
571        char tmp[32];
572    
573        if (!name) {
574            sprintf (tmp, "%08lX", GetTickCount ());
575            name = tmp;
576        }
577    
578        /* in the mobile mode we use a local temp folder
579           with the fixed name 'temp'. */
580        if (mobile_mode_active) {
581            _snprintf (buf, buflen-1, "temp\\%s", name);
582            return 0;
583        }
584    
585        if (!GetTempPath (buflen - strlen (name) -2, buf)) {
586            log_debug ("GetTempPath() failed ec=%d\n", (int)GetLastError ());
587            return -1;
588        }
589        strcat (buf, name);
590        return 0;
591    }
592    
593    
594    /* SetDlgItemText replacement with UTF8 support. */  
595    void
596    SetDlgItemText_utf8 (HWND dlg, int id, const char *txt)
597    {
598        char *utf8_txt;
599    
600        utf8_txt = utf8_to_native (txt);
601        SetDlgItemText (dlg, id, utf8_txt);
602        safe_free (utf8_txt);
603    }
604    
605    /* SetWindowText replacement with UTF8 support. */
606    void
607    SetWindowText_utf8 (HWND hwnd, const char *txt)
608    {
609        char *utf8_txt;
610    
611        utf8_txt = utf8_to_native (txt);
612        SetWindowText (hwnd, utf8_txt);
613        safe_free (utf8_txt);
614    }
615    
616    
617    void
618    ListBox_AddString_utf8 (HWND lb, const char *txt)
619    {
620        char *utf8_txt;
621    
622        utf8_txt = utf8_to_native (txt);
623        SendMessage (lb, LB_ADDSTRING, 0, (LPARAM)(LPCSTR)(utf8_txt));
624        safe_free (utf8_txt);
625    }
626    
627    
628    void
629    ComboBox_AddString_utf8 (HWND cb, const char *txt)
630    {
631    
632        char *utf8_txt;
633    
634        utf8_txt = utf8_to_native (txt);
635        SendMessage ((cb), CB_ADDSTRING, 0, (LPARAM)(LPCSTR)(utf8_txt));
636        safe_free (utf8_txt);
637    }
638    
639    
640    /* GetDlgItemText replacement with UTF8 support. */
641    int
642    GetDlgItemText_utf8 (HWND dlg, int id, char **r_txt)
643    {
644        int len = GetWindowTextLength (GetDlgItem (dlg, id));
645        char *txt;
646    
647        *r_txt = NULL;
648        if (len < 1)
649            return 0;
650        txt = new char[len+2];
651        if (!txt)
652            BUG (NULL);
653        GetDlgItemText (dlg, id, txt, len+1);
654        *r_txt = native_to_utf8 (txt);
655        free_if_alloc (txt);
656        return len;
657    }
658    
659    
660    struct reminder_hd_s {
661        int msecs;
662        HWND dlg;
663        HANDLE hd;
664    };
665    
666    
667    static DWORD CALLBACK
668    foreground_reminder_thread (void *c)
669    {
670        struct reminder_hd_s *ctx = (struct reminder_hd_s *)c;
671        Sleep (ctx->msecs);
672        SetForegroundWindow (ctx->dlg);
673        CloseHandle (ctx->hd);
674        delete ctx;
675        ExitThread (0);
676        return 0;
677    }
678    
679    /* Try to force the window @dlg to the foreground.
680       On NT5 or later this will not work if the user
681       is working in another window (console for example). */
682    void
683    force_foreground_window (HWND dlg, int msecs)
684    {
685        struct reminder_hd_s *hd;
686        DWORD tid;
687    
688        hd = new reminder_hd_s;
689        hd->dlg = dlg;
690        hd->msecs = msecs;
691        hd->hd = CreateThread (NULL, 0, foreground_reminder_thread,
692                               hd, NULL, &tid);
693    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26