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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26