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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26