/[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 77 by twoaday, Mon Nov 14 15:01:01 2005 UTC revision 129 by twoaday, Fri Dec 30 13:56:10 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, 2005 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
5   *   *
# Line 29  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 56  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 64  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 72  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 84  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 135  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 146  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 262  set_clip_text2 (HWND hwnd, const char *t Line 277  set_clip_text2 (HWND hwnd, const char *t
277  }  }
278    
279    
280  /* 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
281       an extension. @ext.
282       Return value: the full file name on success. */
283  char*  char*
284  make_filename (const char *path, const char *file, const char *ext)  make_filename (const char *path, const char *file, const char *ext)
285  {  {
# Line 289  make_filename (const char *path, const c Line 306  make_filename (const char *path, const c
306          strcat( p, ext );          strcat( p, ext );
307      }      }
308      return p;      return p;
309  } /* make_filename */  }
310    
311    
312  /* return 0 if it exists, otherwise >0. */  /* return 0 if the file @fname exists, otherwise >0. */
313  int  int
314  file_exist_check (const char * fname)  file_exist_check (const char *fname)
315  {  {
316      struct stat st;      struct stat st;
317      if (stat (fname, &st) == -1)      if (stat (fname, &st) == -1)
# Line 310  dir_exist_check (const char *dir) Line 327  dir_exist_check (const char *dir)
327  {  {
328      struct stat statbuf;          struct stat statbuf;    
329            
330      if( stat( dir, &statbuf ) == -1 )      if (stat (dir, &statbuf) == -1)
331          return WPTERR_GENERAL;          return WPTERR_GENERAL;
332      if( statbuf.st_mode & _S_IFDIR )      if (statbuf.st_mode & _S_IFDIR)
333          return 0;          return 0;
334      return WPTERR_GENERAL;      return WPTERR_GENERAL;
335  }  }
# Line 371  release_file_lock( LOCK *ctx ) Line 388  release_file_lock( LOCK *ctx )
388  /* 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
389     dialog is not already openened. */     dialog is not already openened. */
390  int  int
391  dialog_box_param( HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,  dialog_box_param (HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,
392                                    LPARAM param, LPCTSTR title, int title_id )                    LPARAM param, LPCTSTR title, int title_id)
393  {  {
394      #ifndef LANG_DE      if (FindWindowEx (GetDesktopWindow (), NULL, NULL, title))
     if( FindWindowEx( GetDesktopWindow(), NULL, NULL, title ) )  
         return -1;        
     #else  
     char strdesc[256];  
     LoadString( glob_hinst, title_id, strdesc, sizeof (strdesc) -1 );  
     if( FindWindowEx( GetDesktopWindow(), NULL, NULL, strdesc ) )  
395          return -1;          return -1;
396      #endif      return DialogBoxParam (hinst, name, parent, fnc, param);
397        }
     return DialogBoxParam( hinst, name, parent, fnc, param );  
 } /* dialog_box_param */  
398    
399    
400  /* Wrapper for message box which forces the message box into the  /* Wrapper for message box which forces the message box into the
# Line 400  msg_box (HWND hwnd, const char *text, co Line 409  msg_box (HWND hwnd, const char *text, co
409  }  }
410    
411    
412  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 */  
   
   
413  char*  char*
414  m_strdup (const char *str)  m_strdup (const char *str)
415  {  {
416      char * p = new char[strlen (str) + 1];      char *p = new char[strlen (str) + 1];
417      if (p)      if (!p)
418          strcpy (p, str);          BUG (NULL);
419        strcpy (p, str);
420      return p;      return p;
421  } /* m_strdup */  }
422    
423    
424  /* Center the hwndChild relative to parent.  /* Center the hwndChild relative to parent.
# Line 493  center_window (HWND hwndChild, HWND hwnd Line 470  center_window (HWND hwndChild, HWND hwnd
470  {  {
471      center_window2 (hwndChild, hwndParent, NULL);      center_window2 (hwndChild, hwndParent, NULL);
472  }  }
473    
474    
475    /* Retrieve the product verion of the given file @fname.
476       Format: MAJOR.MINOR.PATCH1.PATCH2
477       Return value: 0 on success. */
478    int
479    get_file_version (const char *fname, WORD *major, WORD *minor,
480                      WORD *patch1, WORD *patch2)
481    {
482        VS_FIXEDFILEINFO *inf = {0};
483        char file[MAX_PATH+1] = {0};
484        LPVOID buf, data;
485        DWORD arg;
486        DWORD size;
487        UINT qlen;
488    
489        strncpy (file, fname, MAX_PATH);
490        size = GetFileVersionInfoSize (file, &arg);
491        if (!size)
492            return -1;
493        buf = (LPVOID)new CHAR[size];
494        if (!buf)
495            BUG (NULL);
496        GetFileVersionInfo (file, 0, size, buf);
497    
498        qlen=0;
499        VerQueryValue (buf, "\\", &data, &qlen);
500        if (!qlen) {
501            delete [] (char*)buf;
502            return -1;
503        }
504        inf = (VS_FIXEDFILEINFO*)data;
505    
506        if (major)
507            *major = HIWORD (inf->dwProductVersionMS);
508        if (minor)
509            *minor = LOWORD (inf->dwProductVersionMS);
510        if (patch1)
511            *patch1 = HIWORD (inf->dwProductVersionLS);    
512        if (patch2)
513            *patch2 = LOWORD (inf->dwProductVersionLS);
514    
515        delete [] (char*)buf;
516        return 0;
517    }
518    
519    
520    /* Return date in a format which complies with the
521       system locale settings. */
522    const char*
523    get_locale_date (long tm_t, char *buf, DWORD buflen)
524    {
525        SYSTEMTIME st;
526        struct tm *ptm;
527    
528        ptm = localtime (&tm_t);  
529        st.wYear = (WORD)ptm->tm_year;
530        st.wMonth = (WORD)ptm->tm_mon;
531        st.wDay = (WORD)ptm->tm_mday;
532        st.wYear += 1900;
533        st.wMonth += 1;
534        if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st,
535                            NULL, buf, buflen))
536            return NULL;
537        return buf;
538    }
539    
540    
541    struct reminder_hd_s {
542        int msecs;
543        HWND dlg;
544        HANDLE hd;
545    };
546    
547    
548    static DWORD CALLBACK
549    foreground_reminder_thread (void *c)
550    {
551        struct reminder_hd_s *ctx = (struct reminder_hd_s *)c;
552        Sleep (ctx->msecs);
553        SetForegroundWindow (ctx->dlg);
554        CloseHandle (ctx->hd);
555        delete ctx;
556        ExitThread (0);
557        return 0;
558    }
559    
560    /* Try to force the window @dlg to the foreground.
561       On NT5 or later this will not work if the user
562       is working in another window (console for example). */
563    void
564    force_foreground_window (HWND dlg, int msecs)
565    {
566        struct reminder_hd_s *hd;
567        DWORD tid;
568    
569        hd = new reminder_hd_s;
570        hd->dlg = dlg;
571        hd->msecs = msecs;
572        hd->hd = CreateThread (NULL, 0, foreground_reminder_thread,
573                               hd, NULL, &tid);
574    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26