/[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 22 by twoaday, Wed Aug 10 11:33:35 2005 UTC revision 355 by twoaday, Sat Dec 3 18:59:01 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 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   *   *
6   * WinPT is free software; you can redistribute it and/or modify   * WinPT is free software; you can redistribute it and/or modify
7   * it under the terms of the GNU General Public License as published by   * it under the terms of the GNU General Public License as published by
8   * the Free Software Foundation; either version 2 of the License, or   * the Free Software Foundation; either version 2 of the License, or
9   * (at your option) any later version.   * (at your option) any later version.
10   *   *
11   * WinPT is distributed in the hope that it will be useful,   * WinPT is distributed in the hope that it will be useful,
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.
15   *   */
16   * You should have received a copy of the GNU General Public License  #ifdef HAVE_CONFIG_H
17   * along with WinPT; if not, write to the Free Software Foundation,  #include <config.h>
18   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA  #endif
19   */  
20    #include <windows.h>
21  #include <windows.h>  #include <stdio.h>
22  #include <stdio.h>  #include <sys/types.h>
23  #include <sys/types.h>  #include <sys/stat.h>
24  #include <sys/stat.h>  #include <shellapi.h>
25  #include <shellapi.h>  #include <shlobj.h>
26  #include <shlobj.h>  #include <commctrl.h>
27  #include <commctrl.h>  #include <time.h>
28    
29  #include "wptNLS.h"  #include "wptNLS.h"
30  #include "wptW32API.h"  #include "wptW32API.h"
31  #include "wptErrors.h"  #include "wptErrors.h"
32  #include "wptVersion.h"  #include "wptVersion.h"
33  #include "wptTypes.h"  #include "wptUTF8.h"
34    #include "wptTypes.h"
35    #include "StringBuffer.h"
36  extern "C" void _SHFree (void *p);  
37    
38  /*  extern "C" void _SHFree (void *p);
39   * The the text of a menu item.  
40   */  
41  void  /* Insert a new item into the menu @hm at position @pos. */
42  set_menu_text (HMENU menu, int m_uid, const char *text)  void
43  {  insert_menu_item (HMENU hm, int pos, UINT m_id, const char *text)
44      MENUITEMINFO mii;  {
45      char menu_text[80];      MENUITEMINFO mi;
46        
47      memset (&mii, 0, sizeof (mii));      memset (&mi, 0, sizeof mi);
48      mii.cbSize = sizeof (mii);      mi.cbSize = sizeof mi;
49      mii.fMask = MIIM_TYPE;      mi.fType = MF_STRING;
50      mii.dwTypeData = menu_text;      mi.dwTypeData = (char *)text;
51      mii.cch = sizeof (menu_text);      mi.cch = strlen (text);
52      GetMenuItemInfo (menu, m_uid, FALSE, &mii);      mi.wID = m_id;
53            mi.fMask = MIIM_ID|MIIM_DATA| MIIM_TYPE;
54      memset (&mii, 0, sizeof mii);      InsertMenuItem (hm, pos, FALSE, &mi);
55      mii.cbSize = sizeof mii;  }
56      mii.fMask = MIIM_TYPE;  
57      mii.fType = MFT_STRING;  
58      mii.dwTypeData = (char *) text;  static void
59      SetMenuItemInfo (menu, m_uid, FALSE, &mii);  set_menu_text_ext (HMENU menu, int by_pos, int m_uid, const char *text)
60  } /* set_menu_text */  {
61        MENUITEMINFO mii;
62        
63  void      memset (&mii, 0, sizeof mii);
64  set_menu_state (HMENU menu, int m_uid, int state)      mii.cbSize = sizeof mii;
65  {            mii.fMask = MIIM_TYPE;
66      MENUITEMINFO mii;      mii.fType = MFT_STRING;
67        mii.dwTypeData = (char *) text;
68      memset( &mii, 0, sizeof (mii) );      SetMenuItemInfo (menu, m_uid, by_pos? TRUE : FALSE, &mii);
69      mii.cbSize = sizeof (mii);  }
70      mii.fMask = MIIM_STATE;  
71      mii.fState = state;  
72      SetMenuItemInfo (menu, m_uid, FALSE, &mii);  /* Set the text of a menu item @m_uid to @text. */
73  } /* set_menu_state */  void
74    set_menu_text (HMENU menu, UINT m_uid, const char *text)
75    {
76        set_menu_text_ext (menu, 0, m_uid, text);
77  const char *  }
78  get_filename_dlg (HWND hwnd, int id, const char * title,  
79                    const char * filter, const char * name)  
80  {  /* Set the text of a menu item with the position @pos to @text. */
81      static char file[512] = "";  void
82      OPENFILENAME open;  set_menu_text_bypos (HMENU menu, UINT pos, const char *text)
83        {
84      if (name && strlen (name) < (sizeof (file)-1))      set_menu_text_ext (menu, 1, pos, text);
85          strcpy (file, name);  }
86      else  
87          memset (file, 0, sizeof (file));  
88      if (!filter)  /* Set the state of a menu item @m_uid to @state. */
89          filter = _("All Files (*.*)\0*.*");  void
90      memset (&open, 0, sizeof (open));  set_menu_state (HMENU menu, UINT m_uid, UINT state)
91      open.lStructSize = sizeof (OPENFILENAME);  {      
92      open.hInstance = glob_hinst;      MENUITEMINFO mii;
93      open.lpstrTitle = title;  
94      open.lpstrFilter = filter;      memset (&mii, 0, sizeof (mii));
95      open.hwndOwner = hwnd;      mii.cbSize = sizeof (mii);
96      open.lpstrFile = file;      mii.fMask = MIIM_STATE;
97      open.nMaxFile = sizeof (file) - 1;      mii.fState = state;
98      open.Flags = 0;      SetMenuItemInfo (menu, m_uid, FALSE, &mii);
99    }
100      if (id == 0 && GetOpenFileName (&open))  
101          return open.lpstrFile;  
102      else if (id == 1 && GetSaveFileName (&open))  /* Retrieve the state of the menu item @m_uid and return it. */
103          return open.lpstrFile;  UINT
104        get_menu_state (HMENU menu, UINT m_uid)
105      return NULL;  {
106  } /* get_filename_dlg */      MENUITEMINFO mii;
107    
108        memset (&mii, 0, sizeof (mii));
109  const char *      mii.cbSize = sizeof (mii);
110  get_folder_dlg (HWND hwnd, const char * title, const char * name)      mii.fMask = MIIM_STATE;
111  {      GetMenuItemInfo (menu, m_uid, FALSE, &mii);
112      static char folder[MAX_PATH] = "";      return mii.fState;
113      BROWSEINFO bi;  }
114      ITEMIDLIST * il;  
115    
116      memset (&bi, 0, sizeof (bi));  enum {
117      bi.hwndOwner = hwnd;      CDLG_FILE_OPEN = 0,
118      if (title)      CDLG_FILE_SAVE = 1
119          bi.lpszTitle = title;  };
120      if (name && strlen (name) < MAX_PATH-1)  
121          strcpy (folder, name);  /* Use the common dialog to request a file from the user.
122      else     id can be either FILE_OPEN or FILE_SAVE.
123          memset (folder, 0, sizeof (folder));     The return value is the file name or NULL if cancel was chosen. */
124      il = SHBrowseForFolder (&bi);  const char *
125      if (il) {  get_filename_dlg (HWND hwnd, int id, const char *title,
126          SHGetPathFromIDList (il, folder);                    const char *filter, const char *name)
127          _SHFree (il);  {
128          return folder;      static char file[2*MAX_PATH+1] = "";
129      }      char _filter[512];
130      return NULL;      char workdir[MAX_PATH+1];
131  }      char *sel_file = NULL;
132        OPENFILENAME open;
133        
134  char*      if (name && strlen (name) < (DIM (file)-1))
135  get_clip_text (HWND hwnd)          strcpy (file, name);
136  {      else
137      HANDLE clipmem;          memset (file, 0, sizeof (file));
138      char *cliptxt, *p;        if (!filter) {      
139      int len;          strcpy (_filter, _("All Files (*.*)"));
140                memcpy (_filter+strlen (_filter)+1, "*.*\0\0", 5);
141      if (OpenClipboard (hwnd) == FALSE)          filter = _filter;
142          return NULL;      }
143      clipmem = GetClipboardData (CF_TEXT);  
144      if (clipmem == NULL) {      GetCurrentDirectory (MAX_PATH, workdir);
145          p = NULL;      memset (&open, 0, sizeof (open));
146          goto leave;      open.lStructSize = sizeof (OPENFILENAME);
147      }      open.hInstance = glob_hinst;
148      cliptxt = (char *) GlobalLock (clipmem);      open.lpstrTitle = title;
149      if (cliptxt == NULL) {      open.lpstrFilter = filter;
150          p = NULL;      open.hwndOwner = hwnd;
151          goto leave;      open.lpstrFile = file;
152      }      open.nMaxFile = DIM (file) - 1;
153            if (id == CDLG_FILE_OPEN)
154      len =  strlen (cliptxt);          open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
155      p = new char[len + 1];      else
156      if (!p)          open.Flags = OFN_OVERWRITEPROMPT;
157          BUG (NULL);  
158      memcpy (p, cliptxt, len);      if (id == CDLG_FILE_OPEN && GetOpenFileName (&open))
159      p[len] = '\0';          sel_file = open.lpstrFile;
160      GlobalUnlock (clipmem);      else if (id == CDLG_FILE_SAVE && GetSaveFileName (&open))
161                sel_file = open.lpstrFile;
162  leave:      SetCurrentDirectory (workdir);
163      CloseClipboard ();      
164      return p;      return sel_file;
165  } /* get_clip_text */  }
166    
167    const char*
168  int  get_filesave_dlg (HWND hwnd, const char *title,
169  set_clip_text (HWND hwnd, const char *text, int nbytes)                    const char *filter, const char *name)
170  {      {
171      HANDLE clipmem;      return get_filename_dlg (hwnd, CDLG_FILE_SAVE, title, filter, name);
172      int rc = 0;  }
173      char *p;  
174        const char*
175      if (OpenClipboard (hwnd) == FALSE)    get_fileopen_dlg (HWND hwnd, const char *title, const char *filter,
176          return WPTERR_CLIP_OPEN;                    const char *name)
177      EmptyClipboard ();  {
178        return get_filename_dlg (hwnd, CDLG_FILE_OPEN, title, filter, name);
179      clipmem = GlobalAlloc (GHND, nbytes + 1);  }
180      if (clipmem == NULL)  
181          BUG (NULL);  
182      p = (char *) GlobalLock (clipmem);  /* Use the common dialog to allow the user to select a folder.
183      if (p == NULL) {     The return value is either the folder path or NULL if cancel was chosen. */
184          rc = WPTERR_GENERAL;;  const char*
185          goto leave;      get_folder_dlg (HWND hwnd, const char *title, const char *name)
186      }  {
187      memcpy (p, text, nbytes);      static char folder[2*MAX_PATH+1] = "";
188      p[nbytes] = '\0';      BROWSEINFO bi;
189            ITEMIDLIST *il;
190      GlobalUnlock (clipmem);  
191      SetClipboardData (CF_TEXT, clipmem);      memset (&bi, 0, sizeof (bi));
192            bi.hwndOwner = hwnd;
193  leave:      if (title)
194      CloseClipboard ();          bi.lpszTitle = title;
195      return rc;      if (name && strlen (name) < MAX_PATH-1)
196  } /* set_clip_text */          strcpy (folder, name);
197        else
198            memset (folder, 0, sizeof (folder));
199  int      il = SHBrowseForFolder (&bi);
200  set_clip_text2 (HWND hwnd, const char *text, int nbytes, int head_foot)      if (il != NULL) {
201  {          SHGetPathFromIDList (il, folder);
202      char *p, *new_text;          _SHFree (il);
203            return folder;
204      p = get_clip_text (hwnd);      }
205      if (!p)      return NULL;
206          return WPTERR_CLIP_GET;  }
207      new_text = new char [strlen (p)+strlen (text)+8];  
208      if (!new_text)  
209          BUG (0);  /* Return the clipboard contents as a string or NULL
210      if (head_foot == 0)     if the clipboard does not contain text. */
211          sprintf (new_text, "%s\r\n%s\r\n\r\n", text, p);  char*
212      else  get_clip_text (HWND hwnd)
213          sprintf (new_text, "%s\n%s\n\n", p, text);  {
214      set_clip_text (hwnd, new_text, strlen (new_text)+1);      HANDLE clipmem;
215      free_if_alloc (p);      char *cliptxt, *p;  
216      free_if_alloc (new_text);      int len;
217      return 0;      
218  }      if (OpenClipboard (hwnd) == FALSE)
219            return NULL;
220        clipmem = GetClipboardData (CF_TEXT);
221  char*      if (clipmem == NULL) {
222  make_filename( const char *path, const char *file, const char *ext )          p = NULL;
223  {          goto leave;
224      char *p;      }
225      size_t size = 0;      cliptxt = (char *) GlobalLock (clipmem);
226        if (cliptxt == NULL) {
227      if( path && *path )          p = NULL;
228          size += strlen( path );          goto leave;
229      if( file && *file )      }
230          size += strlen( file );      
231      if( ext && *ext )      len =  strlen (cliptxt);
232          size += strlen( ext );      p = new char[len + 1];
233      p = new char[size + 4];      if (!p)
234      memset( p, 0, size );          BUG (NULL);
235      if( path ) {      memcpy (p, cliptxt, len);
236          strcat( p, path );      p[len] = '\0';
237          if( path[strlen( path ) -1] != '\\' )      GlobalUnlock (clipmem);
238              strcat( p, "\\" );      
239      }  leave:
240      if( file )      CloseClipboard ();
241          strcat( p, file );      return p;
242      if( ext ) {  }
243          strcat( p, "." );  
244          strcat( p, ext );  
245      }  /* Set @text as the new clipboard content. */
246      return p;  int
247  } /* make_filename */  set_clip_text (HWND hwnd, const char *text, int nbytes)
248    {    
249        HANDLE clipmem;
250  /* return 0 if it exists, otherwise >0. */      int rc = 0;
251  int      char *p;
252  file_exist_check (const char * fname)      
253  {            if (OpenClipboard (hwnd) == FALSE)  
254      HANDLE fh;            return WPTERR_CLIP_OPEN;
255        EmptyClipboard ();
256      fh = CreateFile( fname, GENERIC_READ, FILE_SHARE_READ,  
257                       NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );      clipmem = GlobalAlloc (GHND, nbytes + 1);
258      if( fh == INVALID_HANDLE_VALUE )      if (clipmem == NULL)
259          return WPTERR_FILE_EXIST;          BUG (NULL);
260      CloseHandle( fh );      p = (char *) GlobalLock (clipmem);
261      return 0;      if (p == NULL) {
262  } /* file_exist_check */          CloseClipboard ();
263            GlobalFree (clipmem);
264            return WPTERR_GENERAL;
265  int      }
266  dir_exist_check( const char *dir )      memcpy (p, text, nbytes);
267  {      p[nbytes] = '\0';
268      struct stat statbuf;          
269            SetClipboardData (CF_TEXT, clipmem);
270      if( stat( dir, &statbuf ) == -1 )      GlobalUnlock (clipmem);
271          return WPTERR_GENERAL;      CloseClipboard ();
272      if( statbuf.st_mode & _S_IFDIR )      GlobalFree (clipmem);
273          return 0;      
274      return WPTERR_GENERAL;      return rc;
275  } /* dir_exist_check */  }
276    
277    
278  size_t  /* Append or prepend some text to the clipboard contents.
279  get_file_size( const char *fname )     If as_footer = 1, append the text otherwise prepend. */
280  {  int
281      size_t fsize;  set_clip_text2 (HWND hwnd, const char *text, int nbytes, int as_footer)
282      HANDLE fh;  {
283        const char *fmt;
284      fh = CreateFile( fname, GENERIC_READ, FILE_SHARE_READ,      char *p, *new_text;
285                      NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );  
286      if( fh == INVALID_HANDLE_VALUE )      p = get_clip_text (hwnd);
287          return 0;      if (!p)
288      fsize = GetFileSize( fh, NULL );          return WPTERR_CLIP_GET;
289      if( fsize == 0xFFFFFFFF )      if (as_footer == 0)
290          fsize = 0;          fmt = "%s\r\n%s\r\n\r\n";
291      CloseHandle( fh );      else
292      return fsize;          fmt = "%s\n%s\n\n";
293  } /* get_file_size */      new_text = new char [strlen (p)+strlen (text)+strlen (fmt)+2];
294        if (!new_text)
295            BUG (0);
296  int      if (as_footer == 0)
297  init_file_lock( LOCK *ctx, const char *file )          sprintf (new_text, fmt, text, p);
298  {      else
299                sprintf (new_text, fmt, p, text);
300      ctx->size = get_file_size( file );      set_clip_text (hwnd, new_text, strlen (new_text)+1);
301      ctx->file = m_strdup( file );      free_if_alloc (p);
302      ctx->fh = CreateFile( file, GENERIC_READ, FILE_SHARE_READ, NULL,      free_if_alloc (new_text);
303                           OPEN_ALWAYS, 0, NULL );      return 0;
304      if( ctx->fh == INVALID_HANDLE_VALUE )  }
305          return WPTERR_GENERAL;  
306      if( LockFile( ctx->fh, 0, 0, ctx->size, 0 ) == FALSE ) {  
307          CloseHandle( ctx->fh );  /* Make a file name out of the path @path, the file @file and
308          ctx->fh = INVALID_HANDLE_VALUE;     an extension. @ext.
309          ctx->size = 0;     Return value: the full file name on success. */
310          free( ctx->file );  char*
311          return WPTERR_GENERAL;  make_filename (const char *path, const char *file, const char *ext)
312      }  {
313      return 0;      char *p;
314  } /* init_file_lock */      size_t size = 0;
315    
316        if (path && *path)
317  void          size += strlen (path);
318  release_file_lock( LOCK *ctx )      if (file && *file)
319  {          size += strlen (file);
320      free_if_alloc( ctx->file );      if (ext && *ext)
321      ctx->file = NULL;          size += strlen (ext);
322      ctx->size = 0;      p = new char[size + 4];
323      CloseHandle( ctx->fh );      if (!p)
324  } /* release_file_lock */          BUG (0);
325        memset (p, 0, size);
326        if (path) {
327  int          strcat (p, path);
328  dialog_box_param( HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,          if (path[strlen (path) -1] != '\\')
329                                    LPARAM param, LPCTSTR title, int title_id )              strcat (p, "\\");
330  {      }
331      #ifndef LANG_DE      if (file != NULL)
332      if( FindWindowEx( GetDesktopWindow(), NULL, NULL, title ) )          strcat (p, file);
333          return -1;            if (ext != NULL) {
334      #else          strcat (p, ".");
335      char strdesc[256];          strcat (p, ext);
336      LoadString( glob_hinst, title_id, strdesc, sizeof (strdesc) -1 );      }
337      if( FindWindowEx( GetDesktopWindow(), NULL, NULL, strdesc ) )      return p;
338          return -1;  }
339      #endif  
340        
341      return DialogBoxParam( hinst, name, parent, fnc, param );  /* Generates a file name from a special dirctory. */
342  } /* dialog_box_param */  char*
343    make_special_filename (int folder, const char *file, const char *ext)
344    {
345  int      BOOL ec;
346  msg_box( HWND hwnd, const char *text, const char *title, int mode )      char path[MAX_PATH+1], *p;
347  {      size_t n=0;
348      mode |= MB_SETFOREGROUND;  
349      mode |= MB_TASKMODAL;      /* MSDN: buf must be at least MAX_PATH=256 bytes */
350      mode |= MB_TOPMOST;      ec = SHGetSpecialFolderPath (HWND_DESKTOP, path, folder, TRUE);
351      return MessageBox(hwnd, text, title, mode);      if (ec != 1) {
352  } /* msg_box */          log_debug ("SHGetSpecialFolderPath() failed\r\n", (int)GetLastError ());
353            return NULL;
354        }
355  void  
356  set_active_window( HWND dlg )      n = strlen (path)+1;
357  {            if (file)
358      activ_hwnd = dlg;          n += strlen (file)+1;
359  } /* set_active_window */      if (ext)
360            n += strlen (ext)+1;
361  void      p = new char[n+2];
362  reset_active_window( void )      if (!p)
363  {                BUG (0);
364      activ_hwnd = NULL;      memset (p, 0, n+2);
365  } /* reset_active_window */      strcpy (p, path);    
366        if (file) {
367            strcat (p, "\\");
368  static DWORD CALLBACK          strcat (p, file);
369  reminder_thread( void *ctx )      }
370  {      if (ext)
371      reminder_ctx_s *c = (reminder_ctx_s *)ctx;          strcat (p, ext);
372        return p;
373      Sleep( c->msecs );  }
374      SetForegroundWindow( activ_hwnd );  
375    
376      return 0;  /* return 0 if the file @fname exists, otherwise >0. */
377  } /* reminder_thread */  int
378    file_exist_check (const char *fname)
379  HANDLE  {
380  window_reminder( struct reminder_ctx_s *ctx )      struct stat st;
381  {      if (stat (fname, &st) == -1)
382      DWORD tid = 0;          return WPTERR_FILE_EXIST;
383            return 0;
384      return CreateThread( NULL, 0, reminder_thread, ctx, 0, &tid );  }
385  } /* window_reminder */  
386    
387    /* Check if the current folder exists.
388  char *     Return 0 for success. */
389  m_strdup (const char *str)  int
390  {  dir_exist_check (const char *dir)
391      char * p = new char[strlen (str) + 1];  {
392      if (p)      struct stat statbuf;    
393          strcpy (p, str);      
394      return p;      if (stat (dir, &statbuf) == -1)
395  } /* m_strdup */          return WPTERR_GENERAL;
396        if (statbuf.st_mode & _S_IFDIR)
397            return 0;
398  void      return WPTERR_GENERAL;
399  center_window2 (HWND hwndChild, HWND style)  }
400  {      
401      HWND hwndParent;  
402      RECT rChild, rParent;      /* Return the file size of the given file @fname. */
403      HDC hdc;  DWORD
404      int wChild, hChild, wParent, hParent;      get_file_size (const char *fname)
405      int wScreen, hScreen, xNew, yNew;  {
406      int flags = SWP_NOSIZE | SWP_NOZORDER;      DWORD fsize;
407        HANDLE fh;
408      hwndParent = GetDesktopWindow ();  
409      GetWindowRect (hwndChild, &rChild);          fh = CreateFile (fname, GENERIC_READ, FILE_SHARE_READ,
410      wChild = rChild.right - rChild.left;                           NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
411      hChild = rChild.bottom - rChild.top;      if (fh == INVALID_HANDLE_VALUE)
412            return 0;
413      GetWindowRect (hwndParent, &rParent);          fsize = GetFileSize (fh, NULL);
414      wParent = rParent.right - rParent.left;          if (fsize == 0xFFFFFFFF)
415      hParent = rParent.bottom - rParent.top;                fsize = 0;
416            CloseHandle (fh);
417      hdc = GetDC (hwndChild);          return fsize;
418      wScreen = GetDeviceCaps (hdc, HORZRES);      }
419      hScreen = GetDeviceCaps (hdc, VERTRES);      
420      ReleaseDC (hwndChild, hdc);        
421      xNew = rParent.left + ((wParent - wChild) /2);      /* Start a dialog with the exception that before it is checked that the
422      if (xNew < 0)     dialog is not already openened. */
423          xNew = 0;  int
424      else if ((xNew+wChild) > wScreen)  dialog_box_param (HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,
425          xNew = wScreen - wChild;                    LPARAM param, LPCTSTR title, int title_id)
426      yNew = rParent.top  + ((hParent - hChild) /2);  {
427      if (yNew < 0)      if (FindWindowEx (GetDesktopWindow (), NULL, NULL, title))
428          yNew = 0;          return -1;
429      else if ((yNew+hChild) > hScreen)      return DialogBoxParam (hinst, name, parent, fnc, param);
430          yNew = hScreen - hChild;  }
431      if (style == HWND_TOPMOST || style == HWND_NOTOPMOST)  
432          flags = SWP_NOMOVE | SWP_NOSIZE;  
433      SetWindowPos (hwndChild, style? style : NULL, xNew, yNew, 0, 0, flags);  /* Wrapper for message box which forces the message box into the
434  }     foreground and it is displayed always on top. */
435    int
436    msg_box (HWND hwnd, const char *text, const char *title, int mode)
437  void  {
438  center_window (HWND hwndChild)      mode |= MB_SETFOREGROUND;
439  {      mode |= MB_TASKMODAL;
440      center_window2 (hwndChild, NULL);      mode |= MB_TOPMOST;
441  }      return MessageBox(hwnd, text, title, mode);
442    }
443    
444    
445    /* Center the hwndChild relative to parent.
446       The style param allows to specificy additional styles (like topmost). */
447    void
448    center_window2 (HWND hwndChild, HWND parent, HWND style)
449    {    
450        HWND hwndParent;
451        RECT rChild, rParent;    
452        HDC hdc;
453        int wChild, hChild, wParent, hParent;    
454        int wScreen, hScreen, xNew, yNew;
455        int flags = SWP_NOSIZE | SWP_NOZORDER;
456    
457        hwndParent = parent;
458        if (hwndParent == NULL)
459            hwndParent = GetDesktopWindow ();
460        GetWindowRect (hwndChild, &rChild);    
461        wChild = rChild.right - rChild.left;    
462        hChild = rChild.bottom - rChild.top;
463    
464        GetWindowRect (hwndParent, &rParent);    
465        wParent = rParent.right - rParent.left;    
466        hParent = rParent.bottom - rParent.top;      
467        
468        hdc = GetDC (hwndChild);    
469        wScreen = GetDeviceCaps (hdc, HORZRES);    
470        hScreen = GetDeviceCaps (hdc, VERTRES);    
471        ReleaseDC (hwndChild, hdc);      
472        xNew = rParent.left + ((wParent - wChild) /2);    
473        if (xNew < 0)
474            xNew = 0;
475        else if ((xNew+wChild) > wScreen)
476            xNew = wScreen - wChild;
477        yNew = rParent.top  + ((hParent - hChild) /2);
478        if (yNew < 0)
479            yNew = 0;
480        else if ((yNew+hChild) > hScreen)
481            yNew = hScreen - hChild;
482        if (style == HWND_TOPMOST || style == HWND_NOTOPMOST)
483            flags = SWP_NOMOVE | SWP_NOSIZE;
484        SetWindowPos (hwndChild, style? style : NULL, xNew, yNew, 0, 0, flags);
485    }
486    
487    
488    /* Center the given hwndChild window with no special style. */
489    void
490    center_window (HWND child, HWND parent)
491    {
492        center_window2 (child, parent, NULL);
493    }
494    
495    
496    
497    
498    /* Return date in a format which complies with the
499       locale user settings. */
500    const char*
501    get_locale_date (long tm_t, char *buf, DWORD buflen)
502    {
503        SYSTEMTIME st;
504        struct tm *ptm;
505    
506        ptm = localtime (&tm_t);
507        memset (&st, 0, sizeof (st));
508        st.wYear = (WORD)ptm->tm_year+1900;
509        st.wMonth = (WORD)ptm->tm_mon+1;
510        st.wDay = (WORD)ptm->tm_mday;
511        if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st,
512                            NULL, buf, buflen))
513            return NULL;
514        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
559       temp path and optionally a name @name provided by the caller.
560       Return value: 0 on success. */
561    int
562    get_temp_name (char *buf, DWORD buflen, const char *name)
563    {
564        char tmp[32];
565    
566        
567        if (!name) {
568            _snprintf (tmp, DIM (tmp)-1, "%08lX", GetTickCount ());
569            name = tmp;
570        }
571    
572        if (!GetTempPath (buflen - strlen (name) -2, buf)) {
573            log_debug ("GetTempPath() failed ec=%d\n", (int)GetLastError ());
574            return -1;
575        }
576    
577        strcat (buf, name);
578        return 0;
579    }
580    
581    
582    void
583    ListBox_AddString_utf8 (HWND lb, const char *txt)
584    {
585        char *utf8_txt;
586    
587        utf8_txt = utf8_to_native (txt);
588        SendMessage (lb, LB_ADDSTRING, 0, (LPARAM)(LPCSTR)(utf8_txt));
589        safe_free (utf8_txt);
590    }
591    
592    
593    void
594    ComboBox_AddString_utf8 (HWND cb, const char *txt)
595    {
596        char *utf8_txt;
597    
598        utf8_txt = utf8_to_native (txt);
599        SendMessage ((cb), CB_ADDSTRING, 0, (LPARAM)(LPCSTR)(utf8_txt));
600        safe_free (utf8_txt);
601    }
602    
603    
604    /* GetDlgItemText replacement with UTF8 support. */
605    int
606    GetDlgItemText_utf8 (HWND dlg, int id, char **r_txt)
607    {
608        int len = GetWindowTextLength (GetDlgItem (dlg, id));
609        char *txt;
610    
611        *r_txt = NULL;
612        if (len < 1)
613            return 0;
614        txt = new char[len+2];
615        if (!txt)
616            BUG (NULL);
617        GetDlgItemText (dlg, id, txt, len+1);
618        *r_txt = native_to_utf8 (txt);
619        free_if_alloc (txt);
620        return len;
621    }
622    
623    int
624    html_help_show (int map_id)
625    {
626        STARTUPINFO si;
627        PROCESS_INFORMATION pi;
628        SECURITY_ATTRIBUTES sec_attr;
629        char path[2*MAX_PATH+1];
630        
631        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",
638                   map_id, gettext_get_langid ());
639        CreateProcess (NULL, path, &sec_attr, &sec_attr,
640                       FALSE, 0, NULL, NULL, &si, &pi);
641        CloseHandle (pi.hProcess);
642        CloseHandle (pi.hThread);
643        return 0;
644    }
645                      
646      
647    /* Return TRUE if the current user has admin privileges. */
648    BOOL
649    user_is_admin (void)
650    {
651        SID_IDENTIFIER_AUTHORITY SystemSidAuthority = SECURITY_NT_AUTHORITY;
652        HANDLE hd;
653        TOKEN_GROUPS  *ptg = NULL;
654        DWORD ngtoken;
655        DWORD i;
656        BOOL admin = FALSE;
657        PSID psid = 0;
658    
659        if (!OpenThreadToken (GetCurrentThread (), TOKEN_QUERY, FALSE, &hd) &&
660            !OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &hd))
661            return FALSE;
662        
663        if (!GetTokenInformation (hd, TokenGroups, NULL, 0, &ngtoken) &&
664            GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
665            ptg = (TOKEN_GROUPS*)GlobalAlloc (GPTR, ngtoken);
666            if (!ptg)
667                return FALSE;
668    
669            if (!GetTokenInformation (hd, TokenGroups, ptg, ngtoken, &ngtoken)) {
670                GlobalFree (ptg);
671                return FALSE;
672            }
673            AllocateAndInitializeSid (&SystemSidAuthority,
674                                      2, SECURITY_BUILTIN_DOMAIN_RID,
675                                      DOMAIN_ALIAS_RID_ADMINS,
676                                      0, 0, 0, 0, 0, 0,
677                                      &psid);
678            for (i = 0; i < ptg->GroupCount; i++) {
679                if (EqualSid (ptg->Groups[i].Sid, psid)) {
680                    admin = TRUE;
681                    break;
682                }
683            }
684            FreeSid (psid);
685            GlobalFree (ptg);
686        }
687    
688        CloseHandle (hd);
689        return admin;
690    }

Legend:
Removed from v.22  
changed lines
  Added in v.355

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26