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

Legend:
Removed from v.25  
changed lines
  Added in v.36

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26