/[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 205 by twoaday, Thu Apr 27 12:46:03 2006 UTC revision 376 by twoaday, Fri Dec 9 12:25:36 2011 UTC
# Line 1  Line 1 
1  /* wptW32API.cpp - Common W32 API functions  /* wptW32API.cpp - Common W32 API functions
2   *      Copyright (C) 2001, 2002, 2003, 2005 Timo Schulz   *      Copyright (C) 2001-2003, 2005-2006, 2009 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
5   *   *
# Line 12  Line 12 
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.   * GNU General Public License for more details.
  *  
  * You should have received a copy of the GNU General Public License  
  * along with WinPT; if not, write to the Free Software Foundation,  
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA  
15   */   */
   
16  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
17  #include <config.h>  #include <config.h>
18  #endif  #endif
# Line 37  Line 32 
32  #include "wptVersion.h"  #include "wptVersion.h"
33  #include "wptUTF8.h"  #include "wptUTF8.h"
34  #include "wptTypes.h"  #include "wptTypes.h"
35    #include "StringBuffer.h"
36    
37    
38  extern "C" void _SHFree (void *p);  extern "C" void _SHFree (void *p);
39    
40    
   
41  /* Insert a new item into the menu @hm at position @pos. */  /* Insert a new item into the menu @hm at position @pos. */
42  void  void
43  insert_menu_item (HMENU hm, int pos, UINT m_id, const char *text)  insert_menu_item (HMENU hm, int pos, UINT m_id, const char *text)
# Line 130  const char * Line 125  const char *
125  get_filename_dlg (HWND hwnd, int id, const char *title,  get_filename_dlg (HWND hwnd, int id, const char *title,
126                    const char *filter, const char *name)                    const char *filter, const char *name)
127  {  {
128      static char file[512] = "";      static char file[2*MAX_PATH+1] = "";
129        char _filter[512];
130        char workdir[MAX_PATH+1];
131        char *sel_file = NULL;
132      OPENFILENAME open;      OPENFILENAME open;
133            
134      if (name && strlen (name) < (sizeof (file)-1))      if (name && strlen (name) < (DIM (file)-1))
135          strcpy (file, name);          strcpy (file, name);
136      else      else
137          memset (file, 0, sizeof (file));          memset (file, 0, sizeof (file));
138      if (!filter)      if (!filter) {      
139          filter = "All Files (*.*)\0*.*\0\0";          strcpy (_filter, _("All Files (*.*)"));
140      /* XXX: problem with gettext because of the 'artificial'          memcpy (_filter+strlen (_filter)+1, "*.*\0\0", 5);
141              double string termination!. */          filter = _filter;
142        }
143    
144        GetCurrentDirectory (MAX_PATH, workdir);
145      memset (&open, 0, sizeof (open));      memset (&open, 0, sizeof (open));
146      open.lStructSize = sizeof (OPENFILENAME);      open.lStructSize = sizeof (OPENFILENAME);
147      open.hInstance = glob_hinst;      open.hInstance = glob_hinst;
# Line 148  get_filename_dlg (HWND hwnd, int id, con Line 149  get_filename_dlg (HWND hwnd, int id, con
149      open.lpstrFilter = filter;      open.lpstrFilter = filter;
150      open.hwndOwner = hwnd;      open.hwndOwner = hwnd;
151      open.lpstrFile = file;      open.lpstrFile = file;
152      open.nMaxFile = sizeof (file) - 1;      open.nMaxFile = DIM (file) - 1;
153      if (id == CDLG_FILE_OPEN)      if (id == CDLG_FILE_OPEN)
154          open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;          open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
155      else      else
156          open.Flags = OFN_OVERWRITEPROMPT;          open.Flags = OFN_OVERWRITEPROMPT;
157    
158      if (id == CDLG_FILE_OPEN && GetOpenFileName (&open))      if (id == CDLG_FILE_OPEN && GetOpenFileName (&open))
159          return open.lpstrFile;          sel_file = open.lpstrFile;
160      else if (id == CDLG_FILE_SAVE && GetSaveFileName (&open))      else if (id == CDLG_FILE_SAVE && GetSaveFileName (&open))
161          return open.lpstrFile;          sel_file = open.lpstrFile;
162        SetCurrentDirectory (workdir);
163            
164      return NULL;      return sel_file;
165  }  }
166    
167  const char*  const char*
# Line 182  get_fileopen_dlg (HWND hwnd, const char Line 184  get_fileopen_dlg (HWND hwnd, const char
184  const char*  const char*
185  get_folder_dlg (HWND hwnd, const char *title, const char *name)  get_folder_dlg (HWND hwnd, const char *title, const char *name)
186  {  {
187      static char folder[MAX_PATH+1] = "";      static char folder[2*MAX_PATH+1] = "";
188      BROWSEINFO bi;      BROWSEINFO bi;
189      ITEMIDLIST *il;      ITEMIDLIST *il;
190    
# Line 195  get_folder_dlg (HWND hwnd, const char *t Line 197  get_folder_dlg (HWND hwnd, const char *t
197      else      else
198          memset (folder, 0, sizeof (folder));          memset (folder, 0, sizeof (folder));
199      il = SHBrowseForFolder (&bi);      il = SHBrowseForFolder (&bi);
200      if (il) {      if (il != NULL) {
201          SHGetPathFromIDList (il, folder);          SHGetPathFromIDList (il, folder);
202          _SHFree (il);          _SHFree (il);
203          return folder;          return folder;
# Line 278  set_clip_text (HWND hwnd, const char *te Line 280  set_clip_text (HWND hwnd, const char *te
280  int  int
281  set_clip_text2 (HWND hwnd, const char *text, int nbytes, int as_footer)  set_clip_text2 (HWND hwnd, const char *text, int nbytes, int as_footer)
282  {  {
283        const char *fmt;
284      char *p, *new_text;      char *p, *new_text;
285    
286      p = get_clip_text (hwnd);      p = get_clip_text (hwnd);
287      if (!p)      if (!p)
288          return WPTERR_CLIP_GET;          return WPTERR_CLIP_GET;
289      new_text = new char [strlen (p)+strlen (text)+8];      if (as_footer == 0)
290            fmt = "%s\r\n%s\r\n\r\n";
291        else
292            fmt = "%s\n%s\n\n";
293        new_text = new char [strlen (p)+strlen (text)+strlen (fmt)+2];
294      if (!new_text)      if (!new_text)
295          BUG (0);          BUG (0);
296      if (as_footer == 0)      if (as_footer == 0)
297          sprintf (new_text, "%s\r\n%s\r\n\r\n", text, p);          sprintf (new_text, fmt, text, p);
298      else      else
299          sprintf (new_text, "%s\n%s\n\n", p, text);          sprintf (new_text, fmt, p, text);
300      set_clip_text (hwnd, new_text, strlen (new_text)+1);      set_clip_text (hwnd, new_text, strlen (new_text)+1);
301      free_if_alloc (p);      free_if_alloc (p);
302      free_if_alloc (new_text);      free_if_alloc (new_text);
# Line 306  make_filename (const char *path, const c Line 313  make_filename (const char *path, const c
313      char *p;      char *p;
314      size_t size = 0;      size_t size = 0;
315    
316      if( path && *path )      if (path && *path)
317          size += strlen( path );          size += strlen (path);
318      if( file && *file )      if (file && *file)
319          size += strlen( file );          size += strlen (file);
320      if( ext && *ext )      if (ext && *ext)
321          size += strlen( ext );          size += strlen (ext);
322      p = new char[size + 4];      p = new char[size + 4];
323      memset( p, 0, size );      if (!p)
324      if( path ) {          BUG (0);
325          strcat( p, path );      memset (p, 0, size);
326          if( path[strlen( path ) -1] != '\\' )      if (path) {
327              strcat( p, "\\" );          strcat (p, path);
328      }          if (path[strlen (path) -1] != '\\')
329      if( file )              strcat (p, "\\");
330          strcat( p, file );      }
331      if( ext ) {      if (file != NULL)
332          strcat( p, "." );          strcat (p, file);
333          strcat( p, ext );      if (ext != NULL) {
334            strcat (p, ".");
335            strcat (p, ext);
336      }      }
337      return p;      return p;
338  }  }
339    
340    
341  /* Generate a file name from a special dirctory. */  /* Generates a file name from a special dirctory. */
342  char*  char*
343  make_special_filename (int folder, const char *file, const char *ext)  make_special_filename (int folder, const char *file, const char *ext)
344  {  {
345      BOOL ec;      BOOL ec;
346      char path[MAX_PATH], *p;      char path[MAX_PATH+1], *p;
347      size_t n=0;      size_t n=0;
348    
349      /* MSDN: buf must be at least MAX_PATH=256 bytes */      /* MSDN: buf must be at least MAX_PATH=256 bytes */
# Line 433  msg_box (HWND hwnd, const char *text, co Line 442  msg_box (HWND hwnd, const char *text, co
442  }  }
443    
444    
 /* Safe strdup version (C++ version). */  
 char*  
 m_strdup (const char *str)  
 {  
     char *p = new char[strlen (str) + 1];  
     if (!p)  
         BUG (NULL);  
     strcpy (p, str);  
     return p;  
 }  
   
   
445  /* Center the hwndChild relative to parent.  /* Center the hwndChild relative to parent.
446     The style param allows to specificy additional styles (like topmost). */     The style param allows to specificy additional styles (like topmost). */
447  void  void
# Line 490  center_window2 (HWND hwndChild, HWND par Line 487  center_window2 (HWND hwndChild, HWND par
487    
488  /* Center the given hwndChild window with no special style. */  /* Center the given hwndChild window with no special style. */
489  void  void
490  center_window (HWND hwndChild, HWND hwndParent)  center_window (HWND child, HWND parent)
491  {  {
492      center_window2 (hwndChild, hwndParent, NULL);      center_window2 (child, parent, NULL);
493  }  }
494    
495    
 /* Retrieve the product verion of the given file @fname.  
    Format: MAJOR.MINOR.PATCH1.PATCH2  
    Return value: 0 on success. */  
 int  
 get_file_version (const char *fname, WORD *major, WORD *minor,  
                   WORD *patch1, WORD *patch2)  
 {  
     VS_FIXEDFILEINFO *inf = {0};  
     char file[MAX_PATH+1] = {0};  
     LPVOID buf, data;  
     DWORD arg;  
     DWORD size;  
     UINT qlen;  
   
     strncpy (file, fname, MAX_PATH);  
     size = GetFileVersionInfoSize (file, &arg);  
     if (!size)  
         return -1;  
     buf = (LPVOID)new CHAR[size];  
     if (!buf)  
         BUG (NULL);  
     GetFileVersionInfo (file, 0, size, buf);  
   
     qlen=0;  
     VerQueryValue (buf, "\\", &data, &qlen);  
     if (!qlen) {  
         delete [] (char*)buf;  
         return -1;  
     }  
     inf = (VS_FIXEDFILEINFO*)data;  
   
     if (major)  
         *major = HIWORD (inf->dwProductVersionMS);  
     if (minor)  
         *minor = LOWORD (inf->dwProductVersionMS);  
     if (patch1)  
         *patch1 = HIWORD (inf->dwProductVersionLS);      
     if (patch2)  
         *patch2 = LOWORD (inf->dwProductVersionLS);  
   
     delete [] (char*)buf;  
     return 0;  
 }  
496    
497    
498  /* Return date in a format which complies with the  /* Return date in a format which complies with the
499     system locale settings. */     locale user settings. */
500  const char*  const char*
501  get_locale_date (long tm_t, char *buf, DWORD buflen)  get_locale_date (long tm_t, char *buf, DWORD buflen)
502  {  {
503      SYSTEMTIME st;      SYSTEMTIME st;
504      struct tm *ptm;      struct tm *ptm;
505    
506      ptm = localtime (&tm_t);        ptm = localtime (&tm_t);
507      st.wYear = (WORD)ptm->tm_year;      memset (&st, 0, sizeof (st));
508      st.wMonth = (WORD)ptm->tm_mon;      st.wYear = (WORD)ptm->tm_year+1900;
509        st.wMonth = (WORD)ptm->tm_mon+1;
510      st.wDay = (WORD)ptm->tm_mday;      st.wDay = (WORD)ptm->tm_mday;
     st.wYear += 1900;  
     st.wMonth += 1;  
511      if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st,      if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st,
512                          NULL, buf, buflen))                          NULL, buf, buflen))
513          return NULL;          return NULL;
514      return buf;      return buf;
515  }  }
516    
517    /* Return the time in a format which complies with the locale user
518       settings. */
519    const char*
520    get_locale_time (long tm_t, char *buf, DWORD buflen)
521    {
522        SYSTEMTIME st;
523        struct tm *ptm;
524    
525        ptm = localtime (&tm_t);
526        memset (&st, 0, sizeof (st));
527        st.wMinute = (WORD)ptm->tm_min;
528        st.wHour = (WORD)ptm->tm_hour;
529        st.wSecond = (WORD)ptm->tm_sec;
530    
531        if (!GetTimeFormat (LOCALE_USER_DEFAULT, TIME_FORCE24HOURFORMAT, &st,
532                            NULL, buf, buflen))
533            return NULL;
534        return buf;
535    }
536    
537    
538    /* Return a combination of date+time which complies to the
539       local user settings. */
540    const char*
541    get_locale_timedate (long tm_t, char *buf, DWORD buflen)
542    {
543        DWORD nleft = buflen;
544    
545        if (!get_locale_date (tm_t, buf, buflen))
546            return NULL;
547        nleft -= strlen (buf)+1;
548        if (nleft < 1)
549            return NULL;
550        strcat (buf, " ");
551        nleft--;
552        if (!get_locale_time (tm_t, buf+strlen (buf), nleft))
553            return NULL;
554        return buf;
555    }
556    
557    
558  /* Generate a temporary file name by using the users  /* Generate a temporary file name by using the users
559     temp path and optionally a name @name provided by the caller.     temp path and optionally a name @name provided by the caller.
# Line 570  get_temp_name (char *buf, DWORD buflen, Line 563  get_temp_name (char *buf, DWORD buflen,
563  {  {
564      char tmp[32];      char tmp[32];
565    
566        
567      if (!name) {      if (!name) {
568          sprintf (tmp, "%08lX", GetTickCount ());          _snprintf (tmp, DIM (tmp)-1, "%08lX", GetTickCount ());
569          name = tmp;          name = tmp;
570      }      }
571    
     /* in the mobile mode we use a local temp folder  
        with the fixed name 'temp'. */  
     if (mobile_mode_active) {  
         _snprintf (buf, buflen-1, "temp\\%s", name);  
         return 0;  
     }  
   
572      if (!GetTempPath (buflen - strlen (name) -2, buf)) {      if (!GetTempPath (buflen - strlen (name) -2, buf)) {
573          log_debug ("GetTempPath() failed ec=%d\n", (int)GetLastError ());          log_debug ("GetTempPath() failed ec=%d\n", (int)GetLastError ());
574          return -1;          return -1;
575      }      }
576    
577      strcat (buf, name);      strcat (buf, name);
578      return 0;      return 0;
579  }  }
# Line 605  ListBox_AddString_utf8 (HWND lb, const c Line 593  ListBox_AddString_utf8 (HWND lb, const c
593  void  void
594  ComboBox_AddString_utf8 (HWND cb, const char *txt)  ComboBox_AddString_utf8 (HWND cb, const char *txt)
595  {  {
   
596      char *utf8_txt;      char *utf8_txt;
597    
598      utf8_txt = utf8_to_native (txt);      utf8_txt = utf8_to_native (txt);
# Line 633  GetDlgItemText_utf8 (HWND dlg, int id, c Line 620  GetDlgItemText_utf8 (HWND dlg, int id, c
620      return len;      return len;
621  }  }
622    
623    int
624  struct reminder_hd_s {  html_help_show (int map_id)
     int msecs;  
     HWND dlg;  
     HANDLE hd;  
 };  
   
   
 static DWORD CALLBACK  
 foreground_reminder_thread (void *c)  
625  {  {
626      struct reminder_hd_s *ctx = (struct reminder_hd_s *)c;      STARTUPINFO si;
627      Sleep (ctx->msecs);      PROCESS_INFORMATION pi;
628      SetForegroundWindow (ctx->dlg);      SECURITY_ATTRIBUTES sec_attr;
629      CloseHandle (ctx->hd);      char path[2*MAX_PATH+1];
630      delete ctx;      
631      ExitThread (0);      memset (&sec_attr, 0, sizeof (sec_attr));
632        memset (&si, 0, sizeof (si));
633        si.cb = sizeof (STARTUPINFO);
634        memset (&pi, 0, sizeof (pi));
635        //hh.exe -mapid 40 "winpt.$lang.chm"
636        
637        _snprintf (path, 2*MAX_PATH, "hh.exe -mapid %d winpt.%s.chm", map_id, gettext_get_langid ());
638        CreateProcess (NULL, path, &sec_attr, &sec_attr, FALSE, 0, NULL, NULL, &si, &pi);
639        CloseHandle (pi.hProcess);
640        CloseHandle (pi.hThread);
641      return 0;      return 0;
642  }  }
643                      
644  /* Try to force the window @dlg to the foreground.    
645     On NT5 or later this will not work if the user  /* Return TRUE if the current user has admin privileges. */
646     is working in another window (console for example). */  BOOL
647  void  user_is_admin (void)
 force_foreground_window (HWND dlg, int msecs)  
648  {  {
649      struct reminder_hd_s *hd;      SID_IDENTIFIER_AUTHORITY SystemSidAuthority = {SECURITY_NT_AUTHORITY};
650      DWORD tid;      HANDLE hd;
651        TOKEN_GROUPS  *ptg = NULL;
652        DWORD ngtoken;
653        DWORD i;
654        BOOL admin = FALSE;
655        PSID psid = 0;
656    
657        if (!OpenThreadToken (GetCurrentThread (), TOKEN_QUERY, FALSE, &hd) &&
658            !OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &hd))
659            return FALSE;
660        
661        if (!GetTokenInformation (hd, TokenGroups, NULL, 0, &ngtoken) &&
662            GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
663            ptg = (TOKEN_GROUPS*)GlobalAlloc (GPTR, ngtoken);
664            if (!ptg)
665                return FALSE;
666    
667            if (!GetTokenInformation (hd, TokenGroups, ptg, ngtoken, &ngtoken)) {
668                GlobalFree (ptg);
669                return FALSE;
670            }
671            AllocateAndInitializeSid (&SystemSidAuthority,
672                                      2, SECURITY_BUILTIN_DOMAIN_RID,
673                                      DOMAIN_ALIAS_RID_ADMINS,
674                                      0, 0, 0, 0, 0, 0,
675                                      &psid);
676            for (i = 0; i < ptg->GroupCount; i++) {
677                if (EqualSid (ptg->Groups[i].Sid, psid)) {
678                    admin = TRUE;
679                    break;
680                }
681            }
682            FreeSid (psid);
683            GlobalFree (ptg);
684        }
685    
686      hd = new reminder_hd_s;      CloseHandle (hd);
687      hd->dlg = dlg;      return admin;
     hd->msecs = msecs;  
     hd->hd = CreateThread (NULL, 0, foreground_reminder_thread,  
                            hd, NULL, &tid);  
688  }  }

Legend:
Removed from v.205  
changed lines
  Added in v.376

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26