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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26