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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26