/[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 105 by twoaday, Wed Nov 30 10:22:00 2005 UTC revision 190 by twoaday, Mon Mar 27 10:05:14 2006 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, 2005 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
5   *   *
# Line 17  Line 17 
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  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
22  #include <config.h>  #include <config.h>
23  #endif  #endif
# Line 28  Line 29 
29  #include <shellapi.h>  #include <shellapi.h>
30  #include <shlobj.h>  #include <shlobj.h>
31  #include <commctrl.h>  #include <commctrl.h>
32    #include <time.h>
33    
34  #include "wptNLS.h"  #include "wptNLS.h"
35  #include "wptW32API.h"  #include "wptW32API.h"
# Line 55  set_menu_text_ext (HMENU menu, int by_po Line 57  set_menu_text_ext (HMENU menu, int by_po
57    
58  /* Set the text of a menu item @m_uid to @text. */  /* Set the text of a menu item @m_uid to @text. */
59  void  void
60  set_menu_text (HMENU menu, int m_uid, const char *text)  set_menu_text (HMENU menu, UINT m_uid, const char *text)
61  {  {
62      set_menu_text_ext (menu, 0, m_uid, text);      set_menu_text_ext (menu, 0, m_uid, text);
63  }  }
# Line 63  set_menu_text (HMENU menu, int m_uid, co Line 65  set_menu_text (HMENU menu, int m_uid, co
65    
66  /* Set the text of a menu item with the position @pos to @text. */  /* Set the text of a menu item with the position @pos to @text. */
67  void  void
68  set_menu_text_bypos (HMENU menu, int pos, const char *text)  set_menu_text_bypos (HMENU menu, UINT pos, const char *text)
69  {  {
70      set_menu_text_ext (menu, 1, pos, text);      set_menu_text_ext (menu, 1, pos, text);
71  }  }
# Line 71  set_menu_text_bypos (HMENU menu, int pos Line 73  set_menu_text_bypos (HMENU menu, int pos
73    
74  /* Set the state of a menu item @m_uid to @state. */  /* Set the state of a menu item @m_uid to @state. */
75  void  void
76  set_menu_state (HMENU menu, int m_uid, int state)  set_menu_state (HMENU menu, UINT m_uid, UINT state)
77  {        {      
78      MENUITEMINFO mii;      MENUITEMINFO mii;
79    
80      memset( &mii, 0, sizeof (mii) );      memset (&mii, 0, sizeof (mii));
81      mii.cbSize = sizeof (mii);      mii.cbSize = sizeof (mii);
82      mii.fMask = MIIM_STATE;      mii.fMask = MIIM_STATE;
83      mii.fState = state;      mii.fState = state;
# Line 83  set_menu_state (HMENU menu, int m_uid, i Line 85  set_menu_state (HMENU menu, int m_uid, i
85  }  }
86    
87    
88    /* Retrieve the state of the menu item @m_uid and return it. */
89    UINT
90    get_menu_state (HMENU menu, UINT m_uid)
91    {
92        MENUITEMINFO mii;
93    
94        memset (&mii, 0, sizeof (mii));
95        mii.cbSize = sizeof (mii);
96        mii.fMask = MIIM_STATE;
97        GetMenuItemInfo (menu, m_uid, FALSE, &mii);
98        return mii.fState;
99    }
100    
101    
102  enum {  enum {
103      CDLG_FILE_OPEN = 0,      CDLG_FILE_OPEN = 0,
104      CDLG_FILE_SAVE = 1      CDLG_FILE_SAVE = 1
# Line 103  get_filename_dlg (HWND hwnd, int id, con Line 119  get_filename_dlg (HWND hwnd, int id, con
119      else      else
120          memset (file, 0, sizeof (file));          memset (file, 0, sizeof (file));
121      if (!filter)      if (!filter)
122          filter = _("All Files (*.*)\0*.*\0\0");          filter = "All Files (*.*)\0*.*\0\0";
123      /* XXX: problem with gettext because of the 'artificial'      /* XXX: problem with gettext because of the 'artificial'
124              double string termination!. */              double string termination!. */
125      memset (&open, 0, sizeof (open));      memset (&open, 0, sizeof (open));
# Line 134  get_filesave_dlg (HWND hwnd, const char Line 150  get_filesave_dlg (HWND hwnd, const char
150      return get_filename_dlg (hwnd, CDLG_FILE_SAVE, title, filter, name);      return get_filename_dlg (hwnd, CDLG_FILE_SAVE, title, filter, name);
151  }  }
152    
153  const char *  const char*
154  get_fileopen_dlg (HWND hwnd, const char *title, const char *filter,  get_fileopen_dlg (HWND hwnd, const char *title, const char *filter,
155                    const char *name)                    const char *name)
156  {  {
# Line 145  get_fileopen_dlg (HWND hwnd, const char Line 161  get_fileopen_dlg (HWND hwnd, const char
161  /* Use the common dialog to allow the user to select a folder.  /* Use the common dialog to allow the user to select a folder.
162     The return value is either the folder path or NULL if cancel was chosen. */     The return value is either the folder path or NULL if cancel was chosen. */
163  const char*  const char*
164  get_folder_dlg (HWND hwnd, const char * title, const char * name)  get_folder_dlg (HWND hwnd, const char *title, const char *name)
165  {  {
166      static char folder[MAX_PATH+1] = "";      static char folder[MAX_PATH+1] = "";
167      BROWSEINFO bi;      BROWSEINFO bi;
168      ITEMIDLIST * il;      ITEMIDLIST *il;
169    
170      memset (&bi, 0, sizeof (bi));      memset (&bi, 0, sizeof (bi));
171      bi.hwndOwner = hwnd;      bi.hwndOwner = hwnd;
# Line 230  set_clip_text (HWND hwnd, const char *te Line 246  set_clip_text (HWND hwnd, const char *te
246            
247      GlobalUnlock (clipmem);      GlobalUnlock (clipmem);
248      SetClipboardData (CF_TEXT, clipmem);      SetClipboardData (CF_TEXT, clipmem);
249        GlobalFree (clipmem);
250            
251  leave:  leave:
252      CloseClipboard ();      CloseClipboard ();
253      return rc;      return rc;
254  } /* set_clip_text */  }
255    
256    
257  /* Append or prepend some text to the clipboard contents.  /* Append or prepend some text to the clipboard contents.
# Line 261  set_clip_text2 (HWND hwnd, const char *t Line 278  set_clip_text2 (HWND hwnd, const char *t
278  }  }
279    
280    
281  /* Make a file name out of the path, the file and an extension. */  /* Make a file name out of the path @path, the file @file and
282       an extension. @ext.
283       Return value: the full file name on success. */
284  char*  char*
285  make_filename (const char *path, const char *file, const char *ext)  make_filename (const char *path, const char *file, const char *ext)
286  {  {
# Line 288  make_filename (const char *path, const c Line 307  make_filename (const char *path, const c
307          strcat( p, ext );          strcat( p, ext );
308      }      }
309      return p;      return p;
310  } /* make_filename */  }
311    
312    
313    /* Generate a file name from a special dirctory. */
314    char*
315    make_special_filename (int folder, const char *file, const char *ext)
316    {
317        BOOL ec;
318        char path[MAX_PATH], *p;
319        size_t n=0;
320    
321        /* MSDN: buf must be at least MAX_PATH=256 bytes */
322        ec = SHGetSpecialFolderPath (HWND_DESKTOP, path, folder, TRUE);
323        if (ec != 1) {
324            log_debug ("SHGetSpecialFolderPath() failed\r\n", (int)GetLastError ());
325            return NULL;
326        }
327    
328        n = strlen (path)+1;
329        if (file)
330            n += strlen (file)+1;
331        if (ext)
332            n += strlen (ext)+1;
333        p = new char[n+2];
334        if (!p)
335            BUG (0);
336        memset (p, 0, n+2);
337        strcpy (p, path);    
338        if (file) {
339            strcat (p, "\\");
340            strcat (p, file);
341        }
342        if (ext)
343            strcat (p, ext);
344        return p;
345    }
346    
347    
348  /* return 0 if it exists, otherwise >0. */  /* return 0 if the file @fname exists, otherwise >0. */
349  int  int
350  file_exist_check (const char * fname)  file_exist_check (const char *fname)
351  {  {
352      struct stat st;      struct stat st;
353      if (stat (fname, &st) == -1)      if (stat (fname, &st) == -1)
# Line 336  get_file_size (const char *fname) Line 390  get_file_size (const char *fname)
390  }  }
391    
392    
 int  
 init_file_lock( LOCK *ctx, const char *file )  
 {  
       
     ctx->size = get_file_size( file );  
     ctx->file = m_strdup( file );  
     ctx->fh = CreateFile( file, GENERIC_READ, FILE_SHARE_READ, NULL,  
                          OPEN_ALWAYS, 0, NULL );  
     if( ctx->fh == INVALID_HANDLE_VALUE )  
         return WPTERR_GENERAL;  
     if( LockFile( ctx->fh, 0, 0, ctx->size, 0 ) == FALSE ) {  
         CloseHandle( ctx->fh );  
         ctx->fh = INVALID_HANDLE_VALUE;  
         ctx->size = 0;  
         free( ctx->file );  
         return WPTERR_GENERAL;  
     }  
     return 0;  
 } /* init_file_lock */  
   
   
 void  
 release_file_lock( LOCK *ctx )  
 {  
     free_if_alloc( ctx->file );  
     ctx->file = NULL;  
     ctx->size = 0;  
     CloseHandle( ctx->fh );  
 } /* release_file_lock */  
   
   
393  /* Start a dialog with the exception that before it is checked that the  /* Start a dialog with the exception that before it is checked that the
394     dialog is not already openened. */     dialog is not already openened. */
395  int  int
# Line 391  msg_box (HWND hwnd, const char *text, co Line 414  msg_box (HWND hwnd, const char *text, co
414  }  }
415    
416    
417  void  /* Safe strdup version (C++ version). */
 set_active_window( HWND dlg)  
 {        
     activ_hwnd = dlg;  
 } /* set_active_window */  
   
 void  
 reset_active_window( void )  
 {        
     activ_hwnd = NULL;  
 } /* reset_active_window */  
   
   
 static DWORD CALLBACK  
 reminder_thread (void *ctx)  
 {  
     reminder_ctx_s *c = (reminder_ctx_s *)ctx;  
   
     Sleep( c->msecs );  
     SetForegroundWindow( activ_hwnd );  
   
     return 0;  
 } /* reminder_thread */  
   
   
 HANDLE  
 window_reminder( struct reminder_ctx_s *ctx )  
 {  
     DWORD tid = 0;  
       
     return CreateThread( NULL, 0, reminder_thread, ctx, 0, &tid );  
 } /* window_reminder */  
   
   
418  char*  char*
419  m_strdup (const char *str)  m_strdup (const char *str)
420  {  {
421      char * p = new char[strlen (str) + 1];      char *p = new char[strlen (str) + 1];
422      if (p)      if (!p)
423          strcpy (p, str);          BUG (NULL);
424        strcpy (p, str);
425      return p;      return p;
426  } /* m_strdup */  }
427    
428    
429  /* Center the hwndChild relative to parent.  /* Center the hwndChild relative to parent.
# Line 484  center_window (HWND hwndChild, HWND hwnd Line 475  center_window (HWND hwndChild, HWND hwnd
475  {  {
476      center_window2 (hwndChild, hwndParent, NULL);      center_window2 (hwndChild, hwndParent, NULL);
477  }  }
478    
479    
480    /* Retrieve the product verion of the given file @fname.
481       Format: MAJOR.MINOR.PATCH1.PATCH2
482       Return value: 0 on success. */
483    int
484    get_file_version (const char *fname, WORD *major, WORD *minor,
485                      WORD *patch1, WORD *patch2)
486    {
487        VS_FIXEDFILEINFO *inf = {0};
488        char file[MAX_PATH+1] = {0};
489        LPVOID buf, data;
490        DWORD arg;
491        DWORD size;
492        UINT qlen;
493    
494        strncpy (file, fname, MAX_PATH);
495        size = GetFileVersionInfoSize (file, &arg);
496        if (!size)
497            return -1;
498        buf = (LPVOID)new CHAR[size];
499        if (!buf)
500            BUG (NULL);
501        GetFileVersionInfo (file, 0, size, buf);
502    
503        qlen=0;
504        VerQueryValue (buf, "\\", &data, &qlen);
505        if (!qlen) {
506            delete [] (char*)buf;
507            return -1;
508        }
509        inf = (VS_FIXEDFILEINFO*)data;
510    
511        if (major)
512            *major = HIWORD (inf->dwProductVersionMS);
513        if (minor)
514            *minor = LOWORD (inf->dwProductVersionMS);
515        if (patch1)
516            *patch1 = HIWORD (inf->dwProductVersionLS);    
517        if (patch2)
518            *patch2 = LOWORD (inf->dwProductVersionLS);
519    
520        delete [] (char*)buf;
521        return 0;
522    }
523    
524    
525    /* Return date in a format which complies with the
526       system locale settings. */
527    const char*
528    get_locale_date (long tm_t, char *buf, DWORD buflen)
529    {
530        SYSTEMTIME st;
531        struct tm *ptm;
532    
533        ptm = localtime (&tm_t);  
534        st.wYear = (WORD)ptm->tm_year;
535        st.wMonth = (WORD)ptm->tm_mon;
536        st.wDay = (WORD)ptm->tm_mday;
537        st.wYear += 1900;
538        st.wMonth += 1;
539        if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st,
540                            NULL, buf, buflen))
541            return NULL;
542        return buf;
543    }
544    
545    
546    /* Generate a temporary file name by using the users
547       temp path and optionally a name @name provided by the caller.
548       Return value: 0 on success. */
549    int
550    get_temp_name (char *buf, DWORD buflen, const char *name)
551    {
552        char tmp[32];
553    
554        if (!name) {
555            sprintf (tmp, "%08lX", GetTickCount ());
556            name = tmp;
557        }
558    
559        /* in the mobile mode we use a local temp folder
560           with the fixed name 'temp'. */
561        if (mobile_mode_active) {
562            _snprintf (buf, buflen-1, "temp\\%s", name);
563            return 0;
564        }
565    
566        if (!GetTempPath (buflen - strlen (name) -2, buf)) {
567            log_debug ("GetTempPath() failed ec=%d\n", (int)GetLastError ());
568            return -1;
569        }
570        strcat (buf, name);
571        return 0;
572    }
573    
574    
575    struct reminder_hd_s {
576        int msecs;
577        HWND dlg;
578        HANDLE hd;
579    };
580    
581    
582    static DWORD CALLBACK
583    foreground_reminder_thread (void *c)
584    {
585        struct reminder_hd_s *ctx = (struct reminder_hd_s *)c;
586        Sleep (ctx->msecs);
587        SetForegroundWindow (ctx->dlg);
588        CloseHandle (ctx->hd);
589        delete ctx;
590        ExitThread (0);
591        return 0;
592    }
593    
594    /* Try to force the window @dlg to the foreground.
595       On NT5 or later this will not work if the user
596       is working in another window (console for example). */
597    void
598    force_foreground_window (HWND dlg, int msecs)
599    {
600        struct reminder_hd_s *hd;
601        DWORD tid;
602    
603        hd = new reminder_hd_s;
604        hd->dlg = dlg;
605        hd->msecs = msecs;
606        hd->hd = CreateThread (NULL, 0, foreground_reminder_thread,
607                               hd, NULL, &tid);
608    }

Legend:
Removed from v.105  
changed lines
  Added in v.190

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26