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

Legend:
Removed from v.6  
changed lines
  Added in v.36

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26