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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26