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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26