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

Legend:
Removed from v.2  
changed lines
  Added in v.167

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26