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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26