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

Legend:
Removed from v.32  
changed lines
  Added in v.77

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26