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

Legend:
Removed from v.22  
changed lines
  Added in v.105

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26