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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26