/[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 255 by twoaday, Tue Aug 1 16: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  {  {
306      char *p;      char *p;
307      size_t size = 0;      size_t size = 0;
308    
309      if( path && *path )      if (path && *path)
310          size += strlen( path );          size += strlen (path);
311      if( file && *file )      if (file && *file)
312          size += strlen( file );          size += strlen (file);
313      if( ext && *ext )      if( ext && *ext )
314          size += strlen( ext );          size += strlen( ext );
315      p = new char[size + 4];      p = new char[size + 4];
316        if (!p)
317            BUG (0);
318      memset( p, 0, size );      memset( p, 0, size );
319      if( path ) {      if( path ) {
320          strcat( p, path );          strcat( p, path );
# Line 289  make_filename (const char *path, const c Line 328  make_filename (const char *path, const c
328          strcat( p, ext );          strcat( p, ext );
329      }      }
330      return p;      return p;
331  } /* make_filename */  }
332    
333    
334  /* return 0 if it exists, otherwise >0. */  /* Generate a file name from a special dirctory. */
335    char*
336    make_special_filename (int folder, const char *file, const char *ext)
337    {
338        BOOL ec;
339        char path[MAX_PATH], *p;
340        size_t n=0;
341    
342        /* MSDN: buf must be at least MAX_PATH=256 bytes */
343        ec = SHGetSpecialFolderPath (HWND_DESKTOP, path, folder, TRUE);
344        if (ec != 1) {
345            log_debug ("SHGetSpecialFolderPath() failed\r\n", (int)GetLastError ());
346            return NULL;
347        }
348    
349        n = strlen (path)+1;
350        if (file)
351            n += strlen (file)+1;
352        if (ext)
353            n += strlen (ext)+1;
354        p = new char[n+2];
355        if (!p)
356            BUG (0);
357        memset (p, 0, n+2);
358        strcpy (p, path);    
359        if (file) {
360            strcat (p, "\\");
361            strcat (p, file);
362        }
363        if (ext)
364            strcat (p, ext);
365        return p;
366    }
367    
368    
369    /* return 0 if the file @fname exists, otherwise >0. */
370  int  int
371  file_exist_check (const char * fname)  file_exist_check (const char *fname)
372  {  {
373      struct stat st;      struct stat st;
374      if (stat (fname, &st) == -1)      if (stat (fname, &st) == -1)
# Line 310  dir_exist_check (const char *dir) Line 384  dir_exist_check (const char *dir)
384  {  {
385      struct stat statbuf;          struct stat statbuf;    
386            
387      if( stat( dir, &statbuf ) == -1 )      if (stat (dir, &statbuf) == -1)
388          return WPTERR_GENERAL;          return WPTERR_GENERAL;
389      if( statbuf.st_mode & _S_IFDIR )      if (statbuf.st_mode & _S_IFDIR)
390          return 0;          return 0;
391      return WPTERR_GENERAL;      return WPTERR_GENERAL;
392  }  }
# Line 337  get_file_size (const char *fname) Line 411  get_file_size (const char *fname)
411  }  }
412    
413    
 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 */  
   
   
414  /* 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
415     dialog is not already openened. */     dialog is not already openened. */
416  int  int
417  dialog_box_param( HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,  dialog_box_param (HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,
418                                    LPARAM param, LPCTSTR title, int title_id )                    LPARAM param, LPCTSTR title, int title_id)
419  {  {
420      #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 ) )  
421          return -1;          return -1;
422      #endif      return DialogBoxParam (hinst, name, parent, fnc, param);
423        }
     return DialogBoxParam( hinst, name, parent, fnc, param );  
 } /* dialog_box_param */  
424    
425    
426  /* 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 435  msg_box (HWND hwnd, const char *text, co
435  }  }
436    
437    
438  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 */  
   
   
439  char*  char*
440  m_strdup (const char *str)  m_strdup (const char *str)
441  {  {
442      char * p = new char[strlen (str) + 1];      char *p = new char[strlen (str) + 1];
443      if (p)      if (!p)
444          strcpy (p, str);          BUG (NULL);
445        strcpy (p, str);
446      return p;      return p;
447  } /* m_strdup */  }
448    
449    
450  /* Center the hwndChild relative to parent.  /* Center the hwndChild relative to parent.
# Line 493  center_window (HWND hwndChild, HWND hwnd Line 496  center_window (HWND hwndChild, HWND hwnd
496  {  {
497      center_window2 (hwndChild, hwndParent, NULL);      center_window2 (hwndChild, hwndParent, NULL);
498  }  }
499    
500    
501    /* Retrieve the product verion of the given file @fname.
502       Format: MAJOR.MINOR.PATCH1.PATCH2
503       Return value: 0 on success. */
504    int
505    get_file_version (const char *fname, WORD *major, WORD *minor,
506                      WORD *patch1, WORD *patch2)
507    {
508        VS_FIXEDFILEINFO *inf;
509        char file[MAX_PATH+1] = {0};
510        LPVOID buf, data;
511        DWORD arg;
512        DWORD size;
513        UINT qlen;
514    
515        strncpy (file, fname, MAX_PATH);
516        size = GetFileVersionInfoSize (file, &arg);
517        if (!size)
518            return -1;
519        buf = (LPVOID)new char[size];
520        if (!buf)
521            BUG (NULL);
522        GetFileVersionInfo (file, 0, size, buf);
523    
524        qlen=0;
525        VerQueryValue (buf, "\\", &data, &qlen);
526        if (!qlen) {
527            delete [](char*)buf;
528            return -1;
529        }
530        inf = (VS_FIXEDFILEINFO*)data;
531    
532        if (major)
533            *major = HIWORD (inf->dwProductVersionMS);
534        if (minor)
535            *minor = LOWORD (inf->dwProductVersionMS);
536        if (patch1)
537            *patch1 = HIWORD (inf->dwProductVersionLS);    
538        if (patch2)
539            *patch2 = LOWORD (inf->dwProductVersionLS);
540    
541        delete [](char*)buf;
542        return 0;
543    }
544    
545    
546    /* Return date in a format which complies with the
547       system locale settings. */
548    const char*
549    get_locale_date (long tm_t, char *buf, DWORD buflen)
550    {
551        SYSTEMTIME st;
552        struct tm *ptm;
553    
554        ptm = localtime (&tm_t);  
555        st.wYear = (WORD)ptm->tm_year;
556        st.wMonth = (WORD)ptm->tm_mon;
557        st.wDay = (WORD)ptm->tm_mday;
558        st.wYear += 1900;
559        st.wMonth += 1;
560        if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st,
561                            NULL, buf, buflen))
562            return NULL;
563        return buf;
564    }
565    
566    
567    /* Generate a temporary file name by using the users
568       temp path and optionally a name @name provided by the caller.
569       Return value: 0 on success. */
570    int
571    get_temp_name (char *buf, DWORD buflen, const char *name)
572    {
573        char tmp[32];
574    
575        if (!name) {
576            sprintf (tmp, "%08lX", GetTickCount ());
577            name = tmp;
578        }
579    
580        /* in the mobile mode we use a local temp folder
581           with the fixed name 'temp'. */
582        if (mobile_mode_active) {
583            _snprintf (buf, buflen-1, "temp\\%s", name);
584            return 0;
585        }
586    
587        if (!GetTempPath (buflen - strlen (name) -2, buf)) {
588            log_debug ("GetTempPath() failed ec=%d\n", (int)GetLastError ());
589            return -1;
590        }
591        strcat (buf, name);
592        return 0;
593    }
594    
595    
596    void
597    ListBox_AddString_utf8 (HWND lb, const char *txt)
598    {
599        char *utf8_txt;
600    
601        utf8_txt = utf8_to_native (txt);
602        SendMessage (lb, LB_ADDSTRING, 0, (LPARAM)(LPCSTR)(utf8_txt));
603        safe_free (utf8_txt);
604    }
605    
606    
607    void
608    ComboBox_AddString_utf8 (HWND cb, const char *txt)
609    {
610    
611        char *utf8_txt;
612    
613        utf8_txt = utf8_to_native (txt);
614        SendMessage ((cb), CB_ADDSTRING, 0, (LPARAM)(LPCSTR)(utf8_txt));
615        safe_free (utf8_txt);
616    }
617    
618    
619    /* GetDlgItemText replacement with UTF8 support. */
620    int
621    GetDlgItemText_utf8 (HWND dlg, int id, char **r_txt)
622    {
623        int len = GetWindowTextLength (GetDlgItem (dlg, id));
624        char *txt;
625    
626        *r_txt = NULL;
627        if (len < 1)
628            return 0;
629        txt = new char[len+2];
630        if (!txt)
631            BUG (NULL);
632        GetDlgItemText (dlg, id, txt, len+1);
633        *r_txt = native_to_utf8 (txt);
634        free_if_alloc (txt);
635        return len;
636    }
637    
638    
639    /* Return TRUE if the current user has admin privileges. */
640    BOOL
641    user_is_admin (void)
642    {
643        SID_IDENTIFIER_AUTHORITY SystemSidAuthority = SECURITY_NT_AUTHORITY;
644        HANDLE hd;
645        TOKEN_GROUPS  *ptg = NULL;
646        DWORD ngtoken;
647        DWORD i;
648        BOOL admin = FALSE;
649        PSID psid = 0;
650    
651        if (GetVersion () & 0x80000000) /* Win9X */
652            return TRUE;
653    
654        if (!OpenThreadToken (GetCurrentThread (), TOKEN_QUERY, FALSE, &hd) &&
655            !OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &hd))
656            return FALSE;
657        
658        if (!GetTokenInformation (hd, TokenGroups, NULL, 0, &ngtoken) &&
659            GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
660            ptg = (TOKEN_GROUPS*)GlobalAlloc (GPTR, ngtoken);
661            if (!ptg)
662                return FALSE;
663    
664            if (!GetTokenInformation (hd, TokenGroups,
665                                      ptg, ngtoken, &ngtoken)) {
666                GlobalFree (ptg);
667                return FALSE;
668            }
669            AllocateAndInitializeSid (&SystemSidAuthority,
670                                      2, SECURITY_BUILTIN_DOMAIN_RID,
671                                      DOMAIN_ALIAS_RID_ADMINS,
672                                      0, 0, 0, 0, 0, 0,
673                                      &psid);
674            for (i = 0; i < ptg->GroupCount; i++) {
675                if (EqualSid (ptg->Groups[i].Sid, psid)) {
676                    admin = TRUE;
677                    break;
678                }
679            }
680            FreeSid (psid);
681            GlobalFree (ptg);
682        }
683    
684        CloseHandle (hd);
685        return admin;
686    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26