/[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 32 by twoaday, Mon Oct 24 08:03:48 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    #include "wptTypes.h"
39  static void  
40  set_menu_text_ext (HMENU menu, int by_pos, int m_uid, const char *text)  
41  {  extern "C" void _SHFree (void *p);
42      MENUITEMINFO mii;  
43        
44      memset (&mii, 0, sizeof mii);  static void
45      mii.cbSize = sizeof mii;  set_menu_text_ext (HMENU menu, int by_pos, int m_uid, const char *text)
46      mii.fMask = MIIM_TYPE;  {
47      mii.fType = MFT_STRING;      MENUITEMINFO mii;
48      mii.dwTypeData = (char *) text;      
49      SetMenuItemInfo (menu, m_uid, by_pos? TRUE : FALSE, &mii);      memset (&mii, 0, sizeof mii);
50  }      mii.cbSize = sizeof mii;
51        mii.fMask = MIIM_TYPE;
52        mii.fType = MFT_STRING;
53  /* Set the text of a menu item @m_uid to @text. */      mii.dwTypeData = (char *) text;
54  void      SetMenuItemInfo (menu, m_uid, by_pos? TRUE : FALSE, &mii);
55  set_menu_text (HMENU menu, int m_uid, const char *text)  }
56  {  
57      set_menu_text_ext (menu, 0, m_uid, text);  
58  }  /* 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  /* Set the text of a menu item with the position @pos to @text. */  {
62  void      set_menu_text_ext (menu, 0, m_uid, text);
63  set_menu_text_bypos (HMENU menu, int pos, const char *text)  }
64  {  
65      set_menu_text_ext (menu, 1, pos, text);  
66  }  /* Set the text of a menu item with the position @pos to @text. */
67    void
68    set_menu_text_bypos (HMENU menu, UINT pos, const char *text)
69  /* Set the state of a menu item @m_uid to @state. */  {
70  void      set_menu_text_ext (menu, 1, pos, text);
71  set_menu_state (HMENU menu, int m_uid, int state)  }
72  {        
73      MENUITEMINFO mii;  
74    /* Set the state of a menu item @m_uid to @state. */
75      memset( &mii, 0, sizeof (mii) );  void
76      mii.cbSize = sizeof (mii);  set_menu_state (HMENU menu, UINT m_uid, UINT state)
77      mii.fMask = MIIM_STATE;  {      
78      mii.fState = state;      MENUITEMINFO mii;
79      SetMenuItemInfo (menu, m_uid, FALSE, &mii);  
80  }      memset (&mii, 0, sizeof (mii));
81        mii.cbSize = sizeof (mii);
82        mii.fMask = MIIM_STATE;
83        mii.fState = state;
84  /* Use the common dialog to request a file from the user.      SetMenuItemInfo (menu, m_uid, FALSE, &mii);
85     id can be either FILE_OPEN or FILE_SAVE.  }
86     The return value is the file name or NULL if cancel was chosen. */  
87  const char *  
88  get_filename_dlg (HWND hwnd, int id, const char * title,  /* Retrieve the state of the menu item @m_uid and return it. */
89                    const char * filter, const char * name)  UINT
90  {  get_menu_state (HMENU menu, UINT m_uid)
91      static char file[512] = "";  {
92      OPENFILENAME open;      MENUITEMINFO mii;
93        
94      if (name && strlen (name) < (sizeof (file)-1))      memset (&mii, 0, sizeof (mii));
95          strcpy (file, name);      mii.cbSize = sizeof (mii);
96      else      mii.fMask = MIIM_STATE;
97          memset (file, 0, sizeof (file));      GetMenuItemInfo (menu, m_uid, FALSE, &mii);
98      if (!filter)      return mii.fState;
99          filter = _("All Files (*.*)\0*.*\0\0");  }
100      /* XXX: problem with gettext because of the 'artificial'  
101              double string termination!. */  
102      memset (&open, 0, sizeof (open));  enum {
103      open.lStructSize = sizeof (OPENFILENAME);      CDLG_FILE_OPEN = 0,
104      open.hInstance = glob_hinst;      CDLG_FILE_SAVE = 1
105      open.lpstrTitle = title;  };
106      open.lpstrFilter = filter;  
107      open.hwndOwner = hwnd;  /* Use the common dialog to request a file from the user.
108      open.lpstrFile = file;     id can be either FILE_OPEN or FILE_SAVE.
109      open.nMaxFile = sizeof (file) - 1;     The return value is the file name or NULL if cancel was chosen. */
110      if (id == FILE_OPEN)  const char *
111          open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;  get_filename_dlg (HWND hwnd, int id, const char * title,
112      else                    const char * filter, const char * name)
113          open.Flags = OFN_OVERWRITEPROMPT;  {
114        static char file[512] = "";
115      if (id == FILE_OPEN && GetOpenFileName (&open))      OPENFILENAME open;
116          return open.lpstrFile;      
117      else if (id == FILE_SAVE && GetSaveFileName (&open))      if (name && strlen (name) < (sizeof (file)-1))
118          return open.lpstrFile;          strcpy (file, name);
119            else
120      return NULL;          memset (file, 0, sizeof (file));
121  }      if (!filter)
122            filter = _("All Files (*.*)\0*.*\0\0");
123  const char*      /* XXX: problem with gettext because of the 'artificial'
124  get_filesave_dlg (HWND hwnd, const char *title,              double string termination!. */
125                    const char *filter, const char *name)      memset (&open, 0, sizeof (open));
126  {      open.lStructSize = sizeof (OPENFILENAME);
127      return get_filename_dlg (hwnd, FILE_SAVE, title, filter, name);      open.hInstance = glob_hinst;
128  }      open.lpstrTitle = title;
129        open.lpstrFilter = filter;
130  const char *      open.hwndOwner = hwnd;
131  get_fileopen_dlg (HWND hwnd, const char *title, const char *filter,      open.lpstrFile = file;
132                    const char *name)      open.nMaxFile = sizeof (file) - 1;
133  {      if (id == CDLG_FILE_OPEN)
134      return get_filename_dlg (hwnd, FILE_OPEN, title, filter, name);          open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
135  }      else
136            open.Flags = OFN_OVERWRITEPROMPT;
137    
138  /* Use the common dialog to allow the user to select a folder.      if (id == CDLG_FILE_OPEN && GetOpenFileName (&open))
139     The return value is either the folder path or NULL if cancel was chosen. */          return open.lpstrFile;
140  const char*      else if (id == CDLG_FILE_SAVE && GetSaveFileName (&open))
141  get_folder_dlg (HWND hwnd, const char * title, const char * name)          return open.lpstrFile;
142  {      
143      static char folder[MAX_PATH+1] = "";      return NULL;
144      BROWSEINFO bi;  }
145      ITEMIDLIST * il;  
146    const char*
147      memset (&bi, 0, sizeof (bi));  get_filesave_dlg (HWND hwnd, const char *title,
148      bi.hwndOwner = hwnd;                    const char *filter, const char *name)
149      if (title)  {
150          bi.lpszTitle = title;      return get_filename_dlg (hwnd, CDLG_FILE_SAVE, title, filter, name);
151      if (name && strlen (name) < MAX_PATH-1)  }
152          strcpy (folder, name);  
153      else  const char*
154          memset (folder, 0, sizeof (folder));  get_fileopen_dlg (HWND hwnd, const char *title, const char *filter,
155      il = SHBrowseForFolder (&bi);                    const char *name)
156      if (il) {  {
157          SHGetPathFromIDList (il, folder);      return get_filename_dlg (hwnd, CDLG_FILE_OPEN, title, filter, name);
158          _SHFree (il);  }
159          return folder;  
160      }  
161      return NULL;  /* 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    const char*
164    get_folder_dlg (HWND hwnd, const char *title, const char *name)
165  /* Return the clipboard contents as a string or NULL  {
166     if the clipboard does not contain text. */      static char folder[MAX_PATH+1] = "";
167  char*      BROWSEINFO bi;
168  get_clip_text (HWND hwnd)      ITEMIDLIST *il;
169  {  
170      HANDLE clipmem;      memset (&bi, 0, sizeof (bi));
171      char *cliptxt, *p;        bi.hwndOwner = hwnd;
172      int len;      if (title)
173                bi.lpszTitle = title;
174      if (OpenClipboard (hwnd) == FALSE)      if (name && strlen (name) < MAX_PATH-1)
175          return NULL;          strcpy (folder, name);
176      clipmem = GetClipboardData (CF_TEXT);      else
177      if (clipmem == NULL) {          memset (folder, 0, sizeof (folder));
178          p = NULL;      il = SHBrowseForFolder (&bi);
179          goto leave;      if (il) {
180      }          SHGetPathFromIDList (il, folder);
181      cliptxt = (char *) GlobalLock (clipmem);          _SHFree (il);
182      if (cliptxt == NULL) {          return folder;
183          p = NULL;      }
184          goto leave;      return NULL;
185      }  }
186        
187      len =  strlen (cliptxt);  
188      p = new char[len + 1];  /* Return the clipboard contents as a string or NULL
189      if (!p)     if the clipboard does not contain text. */
190          BUG (NULL);  char*
191      memcpy (p, cliptxt, len);  get_clip_text (HWND hwnd)
192      p[len] = '\0';  {
193      GlobalUnlock (clipmem);      HANDLE clipmem;
194            char *cliptxt, *p;  
195  leave:      int len;
196      CloseClipboard ();      
197      return p;      if (OpenClipboard (hwnd) == FALSE)
198  }          return NULL;
199        clipmem = GetClipboardData (CF_TEXT);
200        if (clipmem == NULL) {
201  /* Set @text as the new clipboard content. */          p = NULL;
202  int          goto leave;
203  set_clip_text (HWND hwnd, const char *text, int nbytes)      }
204  {          cliptxt = (char *) GlobalLock (clipmem);
205      HANDLE clipmem;      if (cliptxt == NULL) {
206      int rc = 0;          p = NULL;
207      char *p;          goto leave;
208            }
209      if (OpenClipboard (hwnd) == FALSE)        
210          return WPTERR_CLIP_OPEN;      len =  strlen (cliptxt);
211      EmptyClipboard ();      p = new char[len + 1];
212        if (!p)
213      clipmem = GlobalAlloc (GHND, nbytes + 1);          BUG (NULL);
214      if (clipmem == NULL)      memcpy (p, cliptxt, len);
215          BUG (NULL);      p[len] = '\0';
216      p = (char *) GlobalLock (clipmem);      GlobalUnlock (clipmem);
217      if (p == NULL) {      
218          rc = WPTERR_GENERAL;;  leave:
219          goto leave;          CloseClipboard ();
220      }      return p;
221      memcpy (p, text, nbytes);  }
222      p[nbytes] = '\0';  
223        
224      GlobalUnlock (clipmem);  /* Set @text as the new clipboard content. */
225      SetClipboardData (CF_TEXT, clipmem);  int
226        set_clip_text (HWND hwnd, const char *text, int nbytes)
227  leave:  {    
228      CloseClipboard ();      HANDLE clipmem;
229      return rc;      int rc = 0;
230  } /* set_clip_text */      char *p;
231        
232        if (OpenClipboard (hwnd) == FALSE)  
233  /* Append or prepend some text to the clipboard contents.          return WPTERR_CLIP_OPEN;
234     If as_footer = 1, append the text otherwise prepend. */      EmptyClipboard ();
235  int  
236  set_clip_text2 (HWND hwnd, const char *text, int nbytes, int as_footer)      clipmem = GlobalAlloc (GHND, nbytes + 1);
237  {      if (clipmem == NULL)
238      char *p, *new_text;          BUG (NULL);
239        p = (char *) GlobalLock (clipmem);
240      p = get_clip_text (hwnd);      if (p == NULL) {
241      if (!p)          rc = WPTERR_GENERAL;;
242          return WPTERR_CLIP_GET;          goto leave;    
243      new_text = new char [strlen (p)+strlen (text)+8];      }
244      if (!new_text)      memcpy (p, text, nbytes);
245          BUG (0);      p[nbytes] = '\0';
246      if (as_footer == 0)      
247          sprintf (new_text, "%s\r\n%s\r\n\r\n", text, p);      GlobalUnlock (clipmem);
248      else      SetClipboardData (CF_TEXT, clipmem);
249          sprintf (new_text, "%s\n%s\n\n", p, text);      
250      set_clip_text (hwnd, new_text, strlen (new_text)+1);  leave:
251      free_if_alloc (p);      CloseClipboard ();
252      free_if_alloc (new_text);      return rc;
253      return 0;  } /* set_clip_text */
254  }  
255    
256    /* Append or prepend some text to the clipboard contents.
257  /* Make a file name out of the path, the file and an extension. */     If as_footer = 1, append the text otherwise prepend. */
258  char*  int
259  make_filename (const char *path, const char *file, const char *ext)  set_clip_text2 (HWND hwnd, const char *text, int nbytes, int as_footer)
260  {  {
261      char *p;      char *p, *new_text;
262      size_t size = 0;  
263        p = get_clip_text (hwnd);
264      if( path && *path )      if (!p)
265          size += strlen( path );          return WPTERR_CLIP_GET;
266      if( file && *file )      new_text = new char [strlen (p)+strlen (text)+8];
267          size += strlen( file );      if (!new_text)
268      if( ext && *ext )          BUG (0);
269          size += strlen( ext );      if (as_footer == 0)
270      p = new char[size + 4];          sprintf (new_text, "%s\r\n%s\r\n\r\n", text, p);
271      memset( p, 0, size );      else
272      if( path ) {          sprintf (new_text, "%s\n%s\n\n", p, text);
273          strcat( p, path );      set_clip_text (hwnd, new_text, strlen (new_text)+1);
274          if( path[strlen( path ) -1] != '\\' )      free_if_alloc (p);
275              strcat( p, "\\" );      free_if_alloc (new_text);
276      }      return 0;
277      if( file )  }
278          strcat( p, file );  
279      if( ext ) {  
280          strcat( p, "." );  /* Make a file name out of the path @path, the file @file and
281          strcat( p, ext );     an extension. @ext.
282      }     Return value: the full file name on success. */
283      return p;  char*
284  } /* make_filename */  make_filename (const char *path, const char *file, const char *ext)
285    {
286        char *p;
287  /* return 0 if it exists, otherwise >0. */      size_t size = 0;
288  int  
289  file_exist_check (const char * fname)      if( path && *path )
290  {          size += strlen( path );
291      struct stat st;      if( file && *file )
292      if (stat (fname, &st) == -1)          size += strlen( file );
293          return WPTERR_FILE_EXIST;      if( ext && *ext )
294      return 0;          size += strlen( ext );
295  }      p = new char[size + 4];
296        memset( p, 0, size );
297        if( path ) {
298  /* Check if the current folder exists.          strcat( p, path );
299     Return 0 for success. */          if( path[strlen( path ) -1] != '\\' )
300  int              strcat( p, "\\" );
301  dir_exist_check (const char *dir)      }
302  {      if( file )
303      struct stat statbuf;              strcat( p, file );
304            if( ext ) {
305      if( stat( dir, &statbuf ) == -1 )          strcat( p, "." );
306          return WPTERR_GENERAL;          strcat( p, ext );
307      if( statbuf.st_mode & _S_IFDIR )      }
308          return 0;      return p;
309      return WPTERR_GENERAL;  }
310  }  
311    
312    /* return 0 if the file @fname exists, otherwise >0. */
313  /* Return the file size of the given file. */  int
314  size_t  file_exist_check (const char *fname)
315  get_file_size (const char *fname)  {
316  {      struct stat st;
317      size_t fsize;      if (stat (fname, &st) == -1)
318      HANDLE fh;          return WPTERR_FILE_EXIST;
319        return 0;
320      fh = CreateFile( fname, GENERIC_READ, FILE_SHARE_READ,  }
321                      NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );  
322      if( fh == INVALID_HANDLE_VALUE )  
323          return 0;  /* Check if the current folder exists.
324      fsize = GetFileSize( fh, NULL );     Return 0 for success. */
325      if( fsize == 0xFFFFFFFF )  int
326          fsize = 0;  dir_exist_check (const char *dir)
327      CloseHandle( fh );  {
328      return fsize;      struct stat statbuf;    
329  } /* get_file_size */      
330        if (stat (dir, &statbuf) == -1)
331            return WPTERR_GENERAL;
332  int      if (statbuf.st_mode & _S_IFDIR)
333  init_file_lock( LOCK *ctx, const char *file )          return 0;
334  {      return WPTERR_GENERAL;
335        }
336      ctx->size = get_file_size( file );  
337      ctx->file = m_strdup( file );  
338      ctx->fh = CreateFile( file, GENERIC_READ, FILE_SHARE_READ, NULL,  /* Return the file size of the given file @fname. */
339                           OPEN_ALWAYS, 0, NULL );  DWORD
340      if( ctx->fh == INVALID_HANDLE_VALUE )  get_file_size (const char *fname)
341          return WPTERR_GENERAL;  {
342      if( LockFile( ctx->fh, 0, 0, ctx->size, 0 ) == FALSE ) {      DWORD fsize;
343          CloseHandle( ctx->fh );      HANDLE fh;
344          ctx->fh = INVALID_HANDLE_VALUE;  
345          ctx->size = 0;      fh = CreateFile (fname, GENERIC_READ, FILE_SHARE_READ,
346          free( ctx->file );                       NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
347          return WPTERR_GENERAL;      if (fh == INVALID_HANDLE_VALUE)
348      }          return 0;
349      return 0;      fsize = GetFileSize (fh, NULL);
350  } /* init_file_lock */      if (fsize == 0xFFFFFFFF)
351            fsize = 0;
352        CloseHandle (fh);
353  void      return fsize;
354  release_file_lock( LOCK *ctx )  }
355  {  
356      free_if_alloc( ctx->file );  
357      ctx->file = NULL;  int
358      ctx->size = 0;  init_file_lock( LOCK *ctx, const char *file )
359      CloseHandle( ctx->fh );  {
360  } /* release_file_lock */      
361        ctx->size = get_file_size( file );
362        ctx->file = m_strdup( file );
363  /* Start a dialog with the exception that before it is checked that the      ctx->fh = CreateFile( file, GENERIC_READ, FILE_SHARE_READ, NULL,
364     dialog is not already openened. */                           OPEN_ALWAYS, 0, NULL );
365  int      if( ctx->fh == INVALID_HANDLE_VALUE )
366  dialog_box_param( HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,          return WPTERR_GENERAL;
367                                    LPARAM param, LPCTSTR title, int title_id )      if( LockFile( ctx->fh, 0, 0, ctx->size, 0 ) == FALSE ) {
368  {          CloseHandle( ctx->fh );
369      #ifndef LANG_DE          ctx->fh = INVALID_HANDLE_VALUE;
370      if( FindWindowEx( GetDesktopWindow(), NULL, NULL, title ) )          ctx->size = 0;
371          return -1;                free( ctx->file );
372      #else          return WPTERR_GENERAL;
373      char strdesc[256];      }
374      LoadString( glob_hinst, title_id, strdesc, sizeof (strdesc) -1 );      return 0;
375      if( FindWindowEx( GetDesktopWindow(), NULL, NULL, strdesc ) )  } /* init_file_lock */
376          return -1;  
377      #endif  
378        void
379      return DialogBoxParam( hinst, name, parent, fnc, param );  release_file_lock( LOCK *ctx )
380  } /* dialog_box_param */  {
381        free_if_alloc( ctx->file );
382        ctx->file = NULL;
383  /* Wrapper for message box which forces the message box into the      ctx->size = 0;
384     foreground and it is displayed always on top. */      CloseHandle( ctx->fh );
385  int  } /* release_file_lock */
386  msg_box (HWND hwnd, const char *text, const char *title, int mode)  
387  {  
388      mode |= MB_SETFOREGROUND;  /* Start a dialog with the exception that before it is checked that the
389      mode |= MB_TASKMODAL;     dialog is not already openened. */
390      mode |= MB_TOPMOST;  int
391      return MessageBox(hwnd, text, title, mode);  dialog_box_param (HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,
392  }                    LPARAM param, LPCTSTR title, int title_id)
393    {
394        if (FindWindowEx (GetDesktopWindow (), NULL, NULL, title))
395  void          return -1;
396  set_active_window( HWND dlg)      return DialogBoxParam (hinst, name, parent, fnc, param);
397  {        }
398      activ_hwnd = dlg;  
399  } /* set_active_window */  
400    /* Wrapper for message box which forces the message box into the
401  void     foreground and it is displayed always on top. */
402  reset_active_window( void )  int
403  {        msg_box (HWND hwnd, const char *text, const char *title, int mode)
404      activ_hwnd = NULL;  {
405  } /* reset_active_window */      mode |= MB_SETFOREGROUND;
406        mode |= MB_TASKMODAL;
407        mode |= MB_TOPMOST;
408  static DWORD CALLBACK      return MessageBox(hwnd, text, title, mode);
409  reminder_thread (void *ctx)  }
410  {  
411      reminder_ctx_s *c = (reminder_ctx_s *)ctx;  
412    /* Safe strdup version (C++ version). */
413      Sleep( c->msecs );  char*
414      SetForegroundWindow( activ_hwnd );  m_strdup (const char *str)
415    {
416      return 0;      char *p = new char[strlen (str) + 1];
417  } /* reminder_thread */      if (!p)
418            BUG (NULL);
419        strcpy (p, str);
420  HANDLE      return p;
421  window_reminder( struct reminder_ctx_s *ctx )  }
422  {  
423      DWORD tid = 0;  
424        /* Center the hwndChild relative to parent.
425      return CreateThread( NULL, 0, reminder_thread, ctx, 0, &tid );     The style param allows to specificy additional styles (like topmost). */
426  } /* window_reminder */  void
427    center_window2 (HWND hwndChild, HWND parent, HWND style)
428    {    
429  char*      HWND hwndParent;
430  m_strdup (const char *str)      RECT rChild, rParent;    
431  {      HDC hdc;
432      char * p = new char[strlen (str) + 1];      int wChild, hChild, wParent, hParent;    
433      if (p)      int wScreen, hScreen, xNew, yNew;
434          strcpy (p, str);      int flags = SWP_NOSIZE | SWP_NOZORDER;
435      return p;  
436  } /* m_strdup */      hwndParent = parent;
437        if (hwndParent == NULL)
438            hwndParent = GetDesktopWindow ();
439  /* Center the hwndChild relative to parent.      GetWindowRect (hwndChild, &rChild);    
440     The style param allows to specificy additional styles (like topmost). */      wChild = rChild.right - rChild.left;    
441  void      hChild = rChild.bottom - rChild.top;
442  center_window2 (HWND hwndChild, HWND parent, HWND style)  
443  {          GetWindowRect (hwndParent, &rParent);    
444      HWND hwndParent;      wParent = rParent.right - rParent.left;    
445      RECT rChild, rParent;          hParent = rParent.bottom - rParent.top;      
446      HDC hdc;      
447      int wChild, hChild, wParent, hParent;          hdc = GetDC (hwndChild);    
448      int wScreen, hScreen, xNew, yNew;      wScreen = GetDeviceCaps (hdc, HORZRES);    
449      int flags = SWP_NOSIZE | SWP_NOZORDER;      hScreen = GetDeviceCaps (hdc, VERTRES);    
450        ReleaseDC (hwndChild, hdc);      
451      hwndParent = parent;      xNew = rParent.left + ((wParent - wChild) /2);    
452      if (hwndParent == NULL)      if (xNew < 0)
453          hwndParent = GetDesktopWindow ();          xNew = 0;
454      GetWindowRect (hwndChild, &rChild);          else if ((xNew+wChild) > wScreen)
455      wChild = rChild.right - rChild.left;              xNew = wScreen - wChild;
456      hChild = rChild.bottom - rChild.top;      yNew = rParent.top  + ((hParent - hChild) /2);
457        if (yNew < 0)
458      GetWindowRect (hwndParent, &rParent);              yNew = 0;
459      wParent = rParent.right - rParent.left;          else if ((yNew+hChild) > hScreen)
460      hParent = rParent.bottom - rParent.top;                yNew = hScreen - hChild;
461            if (style == HWND_TOPMOST || style == HWND_NOTOPMOST)
462      hdc = GetDC (hwndChild);              flags = SWP_NOMOVE | SWP_NOSIZE;
463      wScreen = GetDeviceCaps (hdc, HORZRES);          SetWindowPos (hwndChild, style? style : NULL, xNew, yNew, 0, 0, flags);
464      hScreen = GetDeviceCaps (hdc, VERTRES);      }
465      ReleaseDC (hwndChild, hdc);        
466      xNew = rParent.left + ((wParent - wChild) /2);      
467      if (xNew < 0)  /* Center the given hwndChild window with no special style. */
468          xNew = 0;  void
469      else if ((xNew+wChild) > wScreen)  center_window (HWND hwndChild, HWND hwndParent)
470          xNew = wScreen - wChild;  {
471      yNew = rParent.top  + ((hParent - hChild) /2);      center_window2 (hwndChild, hwndParent, NULL);
472      if (yNew < 0)  }
473          yNew = 0;  
474      else if ((yNew+hChild) > hScreen)  
475          yNew = hScreen - hChild;  /* Retrieve the product verion of the given file @fname.
476      if (style == HWND_TOPMOST || style == HWND_NOTOPMOST)     Format: MAJOR.MINOR.PATCH1.PATCH2
477          flags = SWP_NOMOVE | SWP_NOSIZE;     Return value: 0 on success. */
478      SetWindowPos (hwndChild, style? style : NULL, xNew, yNew, 0, 0, flags);  int
479  }  get_file_version (const char *fname, WORD *major, WORD *minor,
480                      WORD *patch1, WORD *patch2)
481    {
482  /* Center the given hwndChild window with no special style. */      VS_FIXEDFILEINFO *inf = {0};
483  void      char file[MAX_PATH+1] = {0};
484  center_window (HWND hwndChild, HWND hwndParent)      LPVOID buf, data;
485  {      DWORD arg;
486      center_window2 (hwndChild, hwndParent, NULL);      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.32  
changed lines
  Added in v.129

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26