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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26