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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26