/[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 25 by twoaday, Wed Oct 12 10:04:26 2005 UTC revision 129 by twoaday, Fri Dec 30 13:56:10 2005 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   *   *
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   * You should have received a copy of the GNU General Public License
17   * along with WinPT; if not, write to the Free Software Foundation,   * along with WinPT; if not, write to the Free Software Foundation,
18   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19   */   */
20    
21  #include <windows.h>  #ifdef HAVE_CONFIG_H
22  #include <stdio.h>  #include <config.h>
23  #include <sys/types.h>  #endif
24  #include <sys/stat.h>  
25  #include <shellapi.h>  #include <windows.h>
26  #include <shlobj.h>  #include <stdio.h>
27  #include <commctrl.h>  #include <sys/types.h>
28    #include <sys/stat.h>
29  #include "wptNLS.h"  #include <shellapi.h>
30  #include "wptW32API.h"  #include <shlobj.h>
31  #include "wptErrors.h"  #include <commctrl.h>
32  #include "wptVersion.h"  #include <time.h>
33  #include "wptTypes.h"  
34    #include "wptNLS.h"
35    #include "wptW32API.h"
36  extern "C" void _SHFree (void *p);  #include "wptErrors.h"
37    #include "wptVersion.h"
38  /* The the text of a menu item. */  #include "wptTypes.h"
39  void  
40  set_menu_text (HMENU menu, int m_uid, const char *text)  
41  {  extern "C" void _SHFree (void *p);
42      MENUITEMINFO mii;  
43      char menu_text[80];  
44        static void
45      memset (&mii, 0, sizeof (mii));  set_menu_text_ext (HMENU menu, int by_pos, int m_uid, const char *text)
46      mii.cbSize = sizeof (mii);  {
47      mii.fMask = MIIM_TYPE;      MENUITEMINFO mii;
48      mii.dwTypeData = menu_text;      
49      mii.cch = sizeof (menu_text);      memset (&mii, 0, sizeof mii);
50      GetMenuItemInfo (menu, m_uid, FALSE, &mii);      mii.cbSize = sizeof mii;
51            mii.fMask = MIIM_TYPE;
52      memset (&mii, 0, sizeof mii);      mii.fType = MFT_STRING;
53      mii.cbSize = sizeof mii;      mii.dwTypeData = (char *) text;
54      mii.fMask = MIIM_TYPE;      SetMenuItemInfo (menu, m_uid, by_pos? TRUE : FALSE, &mii);
55      mii.fType = MFT_STRING;  }
56      mii.dwTypeData = (char *) text;  
57      SetMenuItemInfo (menu, m_uid, FALSE, &mii);  
58  } /* set_menu_text */  /* Set the text of a menu item @m_uid to @text. */
59    void
60    set_menu_text (HMENU menu, UINT m_uid, const char *text)
61  void  {
62  set_menu_state (HMENU menu, int m_uid, int state)      set_menu_text_ext (menu, 0, m_uid, text);
63  {        }
64      MENUITEMINFO mii;  
65    
66      memset( &mii, 0, sizeof (mii) );  /* Set the text of a menu item with the position @pos to @text. */
67      mii.cbSize = sizeof (mii);  void
68      mii.fMask = MIIM_STATE;  set_menu_text_bypos (HMENU menu, UINT pos, const char *text)
69      mii.fState = state;  {
70      SetMenuItemInfo (menu, m_uid, FALSE, &mii);      set_menu_text_ext (menu, 1, pos, text);
71  } /* set_menu_state */  }
72    
73    
74    /* Set the state of a menu item @m_uid to @state. */
75  /* Use the common dialog to request a file from the user.  void
76     id can be either FILE_OPEN or FILE_SAVE.  set_menu_state (HMENU menu, UINT m_uid, UINT state)
77     The return value is the file name or NULL if cancel was chosen. */  {      
78  const char *      MENUITEMINFO mii;
79  get_filename_dlg (HWND hwnd, int id, const char * title,  
80                    const char * filter, const char * name)      memset (&mii, 0, sizeof (mii));
81  {      mii.cbSize = sizeof (mii);
82      static char file[512] = "";      mii.fMask = MIIM_STATE;
83      OPENFILENAME open;      mii.fState = state;
84            SetMenuItemInfo (menu, m_uid, FALSE, &mii);
85      if (name && strlen (name) < (sizeof (file)-1))  }
86          strcpy (file, name);  
87      else  
88          memset (file, 0, sizeof (file));  /* Retrieve the state of the menu item @m_uid and return it. */
89      if (!filter)  UINT
90          filter = _("All Files (*.*)\0*.*\0\0");  get_menu_state (HMENU menu, UINT m_uid)
91      memset (&open, 0, sizeof (open));  {
92      open.lStructSize = sizeof (OPENFILENAME);      MENUITEMINFO mii;
93      open.hInstance = glob_hinst;  
94      open.lpstrTitle = title;      memset (&mii, 0, sizeof (mii));
95      open.lpstrFilter = filter;      mii.cbSize = sizeof (mii);
96      open.hwndOwner = hwnd;      mii.fMask = MIIM_STATE;
97      open.lpstrFile = file;      GetMenuItemInfo (menu, m_uid, FALSE, &mii);
98      open.nMaxFile = sizeof (file) - 1;      return mii.fState;
99      if (id == FILE_OPEN)  }
100          open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;  
101      else  
102          open.Flags = OFN_OVERWRITEPROMPT;  enum {
103        CDLG_FILE_OPEN = 0,
104      if (id == FILE_OPEN && GetOpenFileName (&open))      CDLG_FILE_SAVE = 1
105          return open.lpstrFile;  };
106      else if (id == FILE_SAVE && GetSaveFileName (&open))  
107          return open.lpstrFile;  /* Use the common dialog to request a file from the user.
108           id can be either FILE_OPEN or FILE_SAVE.
109      return NULL;     The return value is the file name or NULL if cancel was chosen. */
110  }  const char *
111    get_filename_dlg (HWND hwnd, int id, const char * title,
112  const char*                    const char * filter, const char * name)
113  get_filesave_dlg (HWND hwnd, const char *title,  {
114                    const char *filter, const char *name)      static char file[512] = "";
115  {      OPENFILENAME open;
116      return get_filename_dlg (hwnd, FILE_SAVE, title, filter, name);      
117  }      if (name && strlen (name) < (sizeof (file)-1))
118            strcpy (file, name);
119  const char *      else
120  get_fileopen_dlg (HWND hwnd, const char *title, const char *filter,          memset (file, 0, sizeof (file));
121                    const char *name)      if (!filter)
122  {          filter = _("All Files (*.*)\0*.*\0\0");
123      return get_filename_dlg (hwnd, FILE_OPEN, title, filter, name);      /* XXX: problem with gettext because of the 'artificial'
124  }              double string termination!. */
125        memset (&open, 0, sizeof (open));
126        open.lStructSize = sizeof (OPENFILENAME);
127  /* Use the common dialog to allow the user to select a folder.      open.hInstance = glob_hinst;
128     The return value is either the folder path or NULL if cancel was chosen. */      open.lpstrTitle = title;
129  const char*      open.lpstrFilter = filter;
130  get_folder_dlg (HWND hwnd, const char * title, const char * name)      open.hwndOwner = hwnd;
131  {      open.lpstrFile = file;
132      static char folder[MAX_PATH+1] = "";      open.nMaxFile = sizeof (file) - 1;
133      BROWSEINFO bi;      if (id == CDLG_FILE_OPEN)
134      ITEMIDLIST * il;          open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
135        else
136      memset (&bi, 0, sizeof (bi));          open.Flags = OFN_OVERWRITEPROMPT;
137      bi.hwndOwner = hwnd;  
138      if (title)      if (id == CDLG_FILE_OPEN && GetOpenFileName (&open))
139          bi.lpszTitle = title;          return open.lpstrFile;
140      if (name && strlen (name) < MAX_PATH-1)      else if (id == CDLG_FILE_SAVE && GetSaveFileName (&open))
141          strcpy (folder, name);          return open.lpstrFile;
142      else      
143          memset (folder, 0, sizeof (folder));      return NULL;
144      il = SHBrowseForFolder (&bi);  }
145      if (il) {  
146          SHGetPathFromIDList (il, folder);  const char*
147          _SHFree (il);  get_filesave_dlg (HWND hwnd, const char *title,
148          return folder;                    const char *filter, const char *name)
149      }  {
150      return NULL;      return get_filename_dlg (hwnd, CDLG_FILE_SAVE, title, filter, name);
151  }  }
152    
153    const char*
154  /* Return the clipboard contents as a string or NULL  get_fileopen_dlg (HWND hwnd, const char *title, const char *filter,
155     if the clipboard does not contain text. */                    const char *name)
156  char*  {
157  get_clip_text (HWND hwnd)      return get_filename_dlg (hwnd, CDLG_FILE_OPEN, title, filter, name);
158  {  }
159      HANDLE clipmem;  
160      char *cliptxt, *p;    
161      int len;  /* Use the common dialog to allow the user to select a folder.
162           The return value is either the folder path or NULL if cancel was chosen. */
163      if (OpenClipboard (hwnd) == FALSE)  const char*
164          return NULL;  get_folder_dlg (HWND hwnd, const char *title, const char *name)
165      clipmem = GetClipboardData (CF_TEXT);  {
166      if (clipmem == NULL) {      static char folder[MAX_PATH+1] = "";
167          p = NULL;      BROWSEINFO bi;
168          goto leave;      ITEMIDLIST *il;
169      }  
170      cliptxt = (char *) GlobalLock (clipmem);      memset (&bi, 0, sizeof (bi));
171      if (cliptxt == NULL) {      bi.hwndOwner = hwnd;
172          p = NULL;      if (title)
173          goto leave;          bi.lpszTitle = title;
174      }      if (name && strlen (name) < MAX_PATH-1)
175                strcpy (folder, name);
176      len =  strlen (cliptxt);      else
177      p = new char[len + 1];          memset (folder, 0, sizeof (folder));
178      if (!p)      il = SHBrowseForFolder (&bi);
179          BUG (NULL);      if (il) {
180      memcpy (p, cliptxt, len);          SHGetPathFromIDList (il, folder);
181      p[len] = '\0';          _SHFree (il);
182      GlobalUnlock (clipmem);          return folder;
183            }
184  leave:      return NULL;
185      CloseClipboard ();  }
186      return p;  
187  }  
188    /* Return the clipboard contents as a string or NULL
189       if the clipboard does not contain text. */
190  /* Set the the given text to the clipboard. */  char*
191  int  get_clip_text (HWND hwnd)
192  set_clip_text (HWND hwnd, const char *text, int nbytes)  {
193  {          HANDLE clipmem;
194      HANDLE clipmem;      char *cliptxt, *p;  
195      int rc = 0;      int len;
196      char *p;      
197            if (OpenClipboard (hwnd) == FALSE)
198      if (OpenClipboard (hwnd) == FALSE)            return NULL;
199          return WPTERR_CLIP_OPEN;      clipmem = GetClipboardData (CF_TEXT);
200      EmptyClipboard ();      if (clipmem == NULL) {
201            p = NULL;
202      clipmem = GlobalAlloc (GHND, nbytes + 1);          goto leave;
203      if (clipmem == NULL)      }
204          BUG (NULL);      cliptxt = (char *) GlobalLock (clipmem);
205      p = (char *) GlobalLock (clipmem);      if (cliptxt == NULL) {
206      if (p == NULL) {          p = NULL;
207          rc = WPTERR_GENERAL;;          goto leave;
208          goto leave;          }
209      }      
210      memcpy (p, text, nbytes);      len =  strlen (cliptxt);
211      p[nbytes] = '\0';      p = new char[len + 1];
212            if (!p)
213      GlobalUnlock (clipmem);          BUG (NULL);
214      SetClipboardData (CF_TEXT, clipmem);      memcpy (p, cliptxt, len);
215            p[len] = '\0';
216  leave:      GlobalUnlock (clipmem);
217      CloseClipboard ();      
218      return rc;  leave:
219  } /* set_clip_text */      CloseClipboard ();
220        return p;
221    }
222  /* Append or prepend some text to the clipboard contents.  
223     If as_footer = 1, append the text otherwise prepend. */  
224  int  /* Set @text as the new clipboard content. */
225  set_clip_text2 (HWND hwnd, const char *text, int nbytes, int as_footer)  int
226  {  set_clip_text (HWND hwnd, const char *text, int nbytes)
227      char *p, *new_text;  {    
228        HANDLE clipmem;
229      p = get_clip_text (hwnd);      int rc = 0;
230      if (!p)      char *p;
231          return WPTERR_CLIP_GET;      
232      new_text = new char [strlen (p)+strlen (text)+8];      if (OpenClipboard (hwnd) == FALSE)  
233      if (!new_text)          return WPTERR_CLIP_OPEN;
234          BUG (0);      EmptyClipboard ();
235      if (as_footer == 0)  
236          sprintf (new_text, "%s\r\n%s\r\n\r\n", text, p);      clipmem = GlobalAlloc (GHND, nbytes + 1);
237      else      if (clipmem == NULL)
238          sprintf (new_text, "%s\n%s\n\n", p, text);          BUG (NULL);
239      set_clip_text (hwnd, new_text, strlen (new_text)+1);      p = (char *) GlobalLock (clipmem);
240      free_if_alloc (p);      if (p == NULL) {
241      free_if_alloc (new_text);          rc = WPTERR_GENERAL;;
242      return 0;          goto leave;    
243  }      }
244        memcpy (p, text, nbytes);
245        p[nbytes] = '\0';
246  /* Make a file name out of the path, the file and an extension. */      
247  char*      GlobalUnlock (clipmem);
248  make_filename (const char *path, const char *file, const char *ext)      SetClipboardData (CF_TEXT, clipmem);
249  {      
250      char *p;  leave:
251      size_t size = 0;      CloseClipboard ();
252        return rc;
253      if( path && *path )  } /* set_clip_text */
254          size += strlen( path );  
255      if( file && *file )  
256          size += strlen( file );  /* Append or prepend some text to the clipboard contents.
257      if( ext && *ext )     If as_footer = 1, append the text otherwise prepend. */
258          size += strlen( ext );  int
259      p = new char[size + 4];  set_clip_text2 (HWND hwnd, const char *text, int nbytes, int as_footer)
260      memset( p, 0, size );  {
261      if( path ) {      char *p, *new_text;
262          strcat( p, path );  
263          if( path[strlen( path ) -1] != '\\' )      p = get_clip_text (hwnd);
264              strcat( p, "\\" );      if (!p)
265      }          return WPTERR_CLIP_GET;
266      if( file )      new_text = new char [strlen (p)+strlen (text)+8];
267          strcat( p, file );      if (!new_text)
268      if( ext ) {          BUG (0);
269          strcat( p, "." );      if (as_footer == 0)
270          strcat( p, ext );          sprintf (new_text, "%s\r\n%s\r\n\r\n", text, p);
271      }      else
272      return p;          sprintf (new_text, "%s\n%s\n\n", p, text);
273  } /* make_filename */      set_clip_text (hwnd, new_text, strlen (new_text)+1);
274        free_if_alloc (p);
275        free_if_alloc (new_text);
276  /* return 0 if it exists, otherwise >0. */      return 0;
277  int  }
278  file_exist_check (const char * fname)  
279  {  
280      struct stat st;  /* Make a file name out of the path @path, the file @file and
281      if (stat (fname, &st) == -1)     an extension. @ext.
282          return WPTERR_FILE_EXIST;     Return value: the full file name on success. */
283      return 0;  char*
284  }  make_filename (const char *path, const char *file, const char *ext)
285    {
286        char *p;
287  /* Check if the current folder exists.      size_t size = 0;
288     Return 0 for success. */  
289  int      if( path && *path )
290  dir_exist_check (const char *dir)          size += strlen( path );
291  {      if( file && *file )
292      struct stat statbuf;              size += strlen( file );
293            if( ext && *ext )
294      if( stat( dir, &statbuf ) == -1 )          size += strlen( ext );
295          return WPTERR_GENERAL;      p = new char[size + 4];
296      if( statbuf.st_mode & _S_IFDIR )      memset( p, 0, size );
297          return 0;      if( path ) {
298      return WPTERR_GENERAL;          strcat( p, path );
299  }          if( path[strlen( path ) -1] != '\\' )
300                strcat( p, "\\" );
301        }
302  /* Return the file size of the given file. */      if( file )
303  size_t          strcat( p, file );
304  get_file_size (const char *fname)      if( ext ) {
305  {          strcat( p, "." );
306      size_t fsize;          strcat( p, ext );
307      HANDLE fh;      }
308        return p;
309      fh = CreateFile( fname, GENERIC_READ, FILE_SHARE_READ,  }
310                      NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );  
311      if( fh == INVALID_HANDLE_VALUE )  
312          return 0;  /* return 0 if the file @fname exists, otherwise >0. */
313      fsize = GetFileSize( fh, NULL );  int
314      if( fsize == 0xFFFFFFFF )  file_exist_check (const char *fname)
315          fsize = 0;  {
316      CloseHandle( fh );      struct stat st;
317      return fsize;      if (stat (fname, &st) == -1)
318  } /* get_file_size */          return WPTERR_FILE_EXIST;
319        return 0;
320    }
321  int  
322  init_file_lock( LOCK *ctx, const char *file )  
323  {  /* Check if the current folder exists.
324           Return 0 for success. */
325      ctx->size = get_file_size( file );  int
326      ctx->file = m_strdup( file );  dir_exist_check (const char *dir)
327      ctx->fh = CreateFile( file, GENERIC_READ, FILE_SHARE_READ, NULL,  {
328                           OPEN_ALWAYS, 0, NULL );      struct stat statbuf;    
329      if( ctx->fh == INVALID_HANDLE_VALUE )      
330          return WPTERR_GENERAL;      if (stat (dir, &statbuf) == -1)
331      if( LockFile( ctx->fh, 0, 0, ctx->size, 0 ) == FALSE ) {          return WPTERR_GENERAL;
332          CloseHandle( ctx->fh );      if (statbuf.st_mode & _S_IFDIR)
333          ctx->fh = INVALID_HANDLE_VALUE;          return 0;
334          ctx->size = 0;      return WPTERR_GENERAL;
335          free( ctx->file );  }
336          return WPTERR_GENERAL;  
337      }  
338      return 0;  /* Return the file size of the given file @fname. */
339  } /* init_file_lock */  DWORD
340    get_file_size (const char *fname)
341    {
342  void      DWORD fsize;
343  release_file_lock( LOCK *ctx )      HANDLE fh;
344  {  
345      free_if_alloc( ctx->file );      fh = CreateFile (fname, GENERIC_READ, FILE_SHARE_READ,
346      ctx->file = NULL;                       NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
347      ctx->size = 0;      if (fh == INVALID_HANDLE_VALUE)
348      CloseHandle( ctx->fh );          return 0;
349  } /* release_file_lock */      fsize = GetFileSize (fh, NULL);
350        if (fsize == 0xFFFFFFFF)
351            fsize = 0;
352  /* Start a dialog with the exception that before it is checked that the      CloseHandle (fh);
353     dialog is not already openened. */      return fsize;
354  int  }
355  dialog_box_param( HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,  
356                                    LPARAM param, LPCTSTR title, int title_id )  
357  {  int
358      #ifndef LANG_DE  init_file_lock( LOCK *ctx, const char *file )
359      if( FindWindowEx( GetDesktopWindow(), NULL, NULL, title ) )  {
360          return -1;            
361      #else      ctx->size = get_file_size( file );
362      char strdesc[256];      ctx->file = m_strdup( file );
363      LoadString( glob_hinst, title_id, strdesc, sizeof (strdesc) -1 );      ctx->fh = CreateFile( file, GENERIC_READ, FILE_SHARE_READ, NULL,
364      if( FindWindowEx( GetDesktopWindow(), NULL, NULL, strdesc ) )                           OPEN_ALWAYS, 0, NULL );
365          return -1;      if( ctx->fh == INVALID_HANDLE_VALUE )
366      #endif          return WPTERR_GENERAL;
367            if( LockFile( ctx->fh, 0, 0, ctx->size, 0 ) == FALSE ) {
368      return DialogBoxParam( hinst, name, parent, fnc, param );          CloseHandle( ctx->fh );
369  } /* dialog_box_param */          ctx->fh = INVALID_HANDLE_VALUE;
370            ctx->size = 0;
371            free( ctx->file );
372  /* Wrapper for message box which forces the message box into the          return WPTERR_GENERAL;
373     foreground and it is displayed always on top. */      }
374  int      return 0;
375  msg_box (HWND hwnd, const char *text, const char *title, int mode)  } /* init_file_lock */
376  {  
377      mode |= MB_SETFOREGROUND;  
378      mode |= MB_TASKMODAL;  void
379      mode |= MB_TOPMOST;  release_file_lock( LOCK *ctx )
380      return MessageBox(hwnd, text, title, mode);  {
381  }      free_if_alloc( ctx->file );
382        ctx->file = NULL;
383        ctx->size = 0;
384  void      CloseHandle( ctx->fh );
385  set_active_window( HWND dlg)  } /* release_file_lock */
386  {        
387      activ_hwnd = dlg;  
388  } /* set_active_window */  /* Start a dialog with the exception that before it is checked that the
389       dialog is not already openened. */
390  void  int
391  reset_active_window( void )  dialog_box_param (HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,
392  {                          LPARAM param, LPCTSTR title, int title_id)
393      activ_hwnd = NULL;  {
394  } /* reset_active_window */      if (FindWindowEx (GetDesktopWindow (), NULL, NULL, title))
395            return -1;
396        return DialogBoxParam (hinst, name, parent, fnc, param);
397  static DWORD CALLBACK  }
398  reminder_thread (void *ctx)  
399  {  
400      reminder_ctx_s *c = (reminder_ctx_s *)ctx;  /* Wrapper for message box which forces the message box into the
401       foreground and it is displayed always on top. */
402      Sleep( c->msecs );  int
403      SetForegroundWindow( activ_hwnd );  msg_box (HWND hwnd, const char *text, const char *title, int mode)
404    {
405      return 0;      mode |= MB_SETFOREGROUND;
406  } /* reminder_thread */      mode |= MB_TASKMODAL;
407        mode |= MB_TOPMOST;
408        return MessageBox(hwnd, text, title, mode);
409  HANDLE  }
410  window_reminder( struct reminder_ctx_s *ctx )  
411  {  
412      DWORD tid = 0;  /* Safe strdup version (C++ version). */
413        char*
414      return CreateThread( NULL, 0, reminder_thread, ctx, 0, &tid );  m_strdup (const char *str)
415  } /* window_reminder */  {
416        char *p = new char[strlen (str) + 1];
417        if (!p)
418  char*          BUG (NULL);
419  m_strdup (const char *str)      strcpy (p, str);
420  {      return p;
421      char * p = new char[strlen (str) + 1];  }
422      if (p)  
423          strcpy (p, str);  
424      return p;  /* Center the hwndChild relative to parent.
425  } /* m_strdup */     The style param allows to specificy additional styles (like topmost). */
426    void
427    center_window2 (HWND hwndChild, HWND parent, HWND style)
428  /* Center the hwndChild relative to parent.  {    
429     The style param allows to specificy additional styles (like topmost). */      HWND hwndParent;
430  void      RECT rChild, rParent;    
431  center_window2 (HWND hwndChild, HWND parent, HWND style)      HDC hdc;
432  {          int wChild, hChild, wParent, hParent;    
433      HWND hwndParent;      int wScreen, hScreen, xNew, yNew;
434      RECT rChild, rParent;          int flags = SWP_NOSIZE | SWP_NOZORDER;
435      HDC hdc;  
436      int wChild, hChild, wParent, hParent;          hwndParent = parent;
437      int wScreen, hScreen, xNew, yNew;      if (hwndParent == NULL)
438      int flags = SWP_NOSIZE | SWP_NOZORDER;          hwndParent = GetDesktopWindow ();
439        GetWindowRect (hwndChild, &rChild);    
440      hwndParent = parent;      wChild = rChild.right - rChild.left;    
441      if (hwndParent == NULL)      hChild = rChild.bottom - rChild.top;
442          hwndParent = GetDesktopWindow ();  
443      GetWindowRect (hwndChild, &rChild);          GetWindowRect (hwndParent, &rParent);    
444      wChild = rChild.right - rChild.left;          wParent = rParent.right - rParent.left;    
445      hChild = rChild.bottom - rChild.top;      hParent = rParent.bottom - rParent.top;      
446        
447      GetWindowRect (hwndParent, &rParent);          hdc = GetDC (hwndChild);    
448      wParent = rParent.right - rParent.left;          wScreen = GetDeviceCaps (hdc, HORZRES);    
449      hParent = rParent.bottom - rParent.top;            hScreen = GetDeviceCaps (hdc, VERTRES);    
450            ReleaseDC (hwndChild, hdc);      
451      hdc = GetDC (hwndChild);          xNew = rParent.left + ((wParent - wChild) /2);    
452      wScreen = GetDeviceCaps (hdc, HORZRES);          if (xNew < 0)
453      hScreen = GetDeviceCaps (hdc, VERTRES);              xNew = 0;
454      ReleaseDC (hwndChild, hdc);            else if ((xNew+wChild) > wScreen)
455      xNew = rParent.left + ((wParent - wChild) /2);              xNew = wScreen - wChild;
456      if (xNew < 0)      yNew = rParent.top  + ((hParent - hChild) /2);
457          xNew = 0;      if (yNew < 0)
458      else if ((xNew+wChild) > wScreen)          yNew = 0;
459          xNew = wScreen - wChild;      else if ((yNew+hChild) > hScreen)
460      yNew = rParent.top  + ((hParent - hChild) /2);          yNew = hScreen - hChild;
461      if (yNew < 0)      if (style == HWND_TOPMOST || style == HWND_NOTOPMOST)
462          yNew = 0;          flags = SWP_NOMOVE | SWP_NOSIZE;
463      else if ((yNew+hChild) > hScreen)      SetWindowPos (hwndChild, style? style : NULL, xNew, yNew, 0, 0, flags);
464          yNew = hScreen - hChild;  }
465      if (style == HWND_TOPMOST || style == HWND_NOTOPMOST)  
466          flags = SWP_NOMOVE | SWP_NOSIZE;  
467      SetWindowPos (hwndChild, style? style : NULL, xNew, yNew, 0, 0, flags);  /* Center the given hwndChild window with no special style. */
468  }  void
469    center_window (HWND hwndChild, HWND hwndParent)
470    {
471  /* Center the given hwndChild window with no special style. */      center_window2 (hwndChild, hwndParent, NULL);
472  void  }
473  center_window (HWND hwndChild, HWND hwndParent)  
474  {  
475      center_window2 (hwndChild, hwndParent, NULL);  /* Retrieve the product verion of the given file @fname.
476  }     Format: MAJOR.MINOR.PATCH1.PATCH2
477       Return value: 0 on success. */
478    int
479    get_file_version (const char *fname, WORD *major, WORD *minor,
480                      WORD *patch1, WORD *patch2)
481    {
482        VS_FIXEDFILEINFO *inf = {0};
483        char file[MAX_PATH+1] = {0};
484        LPVOID buf, data;
485        DWORD arg;
486        DWORD size;
487        UINT qlen;
488    
489        strncpy (file, fname, MAX_PATH);
490        size = GetFileVersionInfoSize (file, &arg);
491        if (!size)
492            return -1;
493        buf = (LPVOID)new CHAR[size];
494        if (!buf)
495            BUG (NULL);
496        GetFileVersionInfo (file, 0, size, buf);
497    
498        qlen=0;
499        VerQueryValue (buf, "\\", &data, &qlen);
500        if (!qlen) {
501            delete [] (char*)buf;
502            return -1;
503        }
504        inf = (VS_FIXEDFILEINFO*)data;
505    
506        if (major)
507            *major = HIWORD (inf->dwProductVersionMS);
508        if (minor)
509            *minor = LOWORD (inf->dwProductVersionMS);
510        if (patch1)
511            *patch1 = HIWORD (inf->dwProductVersionLS);    
512        if (patch2)
513            *patch2 = LOWORD (inf->dwProductVersionLS);
514    
515        delete [] (char*)buf;
516        return 0;
517    }
518    
519    
520    /* Return date in a format which complies with the
521       system locale settings. */
522    const char*
523    get_locale_date (long tm_t, char *buf, DWORD buflen)
524    {
525        SYSTEMTIME st;
526        struct tm *ptm;
527    
528        ptm = localtime (&tm_t);  
529        st.wYear = (WORD)ptm->tm_year;
530        st.wMonth = (WORD)ptm->tm_mon;
531        st.wDay = (WORD)ptm->tm_mday;
532        st.wYear += 1900;
533        st.wMonth += 1;
534        if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st,
535                            NULL, buf, buflen))
536            return NULL;
537        return buf;
538    }
539    
540    
541    struct reminder_hd_s {
542        int msecs;
543        HWND dlg;
544        HANDLE hd;
545    };
546    
547    
548    static DWORD CALLBACK
549    foreground_reminder_thread (void *c)
550    {
551        struct reminder_hd_s *ctx = (struct reminder_hd_s *)c;
552        Sleep (ctx->msecs);
553        SetForegroundWindow (ctx->dlg);
554        CloseHandle (ctx->hd);
555        delete ctx;
556        ExitThread (0);
557        return 0;
558    }
559    
560    /* Try to force the window @dlg to the foreground.
561       On NT5 or later this will not work if the user
562       is working in another window (console for example). */
563    void
564    force_foreground_window (HWND dlg, int msecs)
565    {
566        struct reminder_hd_s *hd;
567        DWORD tid;
568    
569        hd = new reminder_hd_s;
570        hd->dlg = dlg;
571        hd->msecs = msecs;
572        hd->hd = CreateThread (NULL, 0, foreground_reminder_thread,
573                               hd, NULL, &tid);
574    }

Legend:
Removed from v.25  
changed lines
  Added in v.129

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26