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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26