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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26