/[winpt]/trunk/Src/wptClipEditDlg.cpp
ViewVC logotype

Diff of /trunk/Src/wptClipEditDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 12 by twoaday, Thu Apr 14 12:56:25 2005 UTC revision 32 by twoaday, Mon Oct 24 08:03:48 2005 UTC
# Line 1  Line 1 
1  /* wptClipEditDlg.cpp - Clipboard Editor dialog  /* wptClipEditDlg.cpp - Clipboard Editor dialog
2   *      Copyright (C) 2000-2004 Timo Schulz   *      Copyright (C) 2000-2005 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
5   *   *
# Line 30  Line 30 
30  #include "../resource.h"  #include "../resource.h"
31    
32    
33  BOOL CALLBACK  /* Load the clipboard contents into the text field in @dlg.
34  clip_edit_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)     Return value: 0 on success. */
35    static int
36    load_clipboard (HWND dlg)
37  {  {
38      HANDLE clipmem;      HANDLE clipmem;
39      OPENFILENAME open;      char *cliptext;
40      FILE * fp;      char *p;
41      char * cliptext = NULL, * newtext = NULL,  * p;  
42      char file[512] = "";      CloseClipboard (); /* make sure it is closed. */
43      int nbytes, id, size;      if (OpenClipboard (NULL) == FALSE) {
44            msg_box (dlg, winpt_strerror (WPTERR_CLIP_OPEN), _("Clipboard"), MB_ERR);
45            return -1;
46        }
47        clipmem = GetClipboardData (CF_TEXT);
48        if (clipmem == NULL) {
49            CloseClipboard ();
50            return -1;
51        }
52        
53        cliptext = (char *) GlobalLock (clipmem);
54        if (cliptext == NULL || !strlen (cliptext)) {    
55            CloseClipboard ();
56            return -1;
57        }
58    
59        p = new char [strlen (cliptext)+2];
60        if (!p)    
61            BUG (0);        
62        strcpy (p, cliptext);
63        
64        GlobalUnlock (clipmem);
65        CloseClipboard ();  
66        SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p);    
67        free_if_alloc (p);
68            
69      switch ( msg )  {      return 0;
70    }
71    
72    
73    /* Load the clipboard from the selected file. */
74    static int
75    load_clipboard_from_file (HWND dlg)
76    {
77        OPENFILENAME open;
78        FILE *fp;
79        char *p;
80        char file[300] = "";
81        DWORD size;
82        int id;
83    
84        memset (&open, 0, sizeof (open));
85        open.lStructSize = sizeof (OPENFILENAME);      
86        open.hInstance = glob_hinst;            
87        open.lpstrTitle = _("File Open");      
88        open.lpstrFilter = (char *)_("All Files (*.*)\0*.*\0\0");      
89        open.hwndOwner = dlg;          
90        open.lpstrFile = file;                  
91        open.nMaxFile = sizeof (file)-1;                
92        open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
93                
94        if (GetOpenFileName (&open)) {
95            if( (size=get_file_size (file)) > MAX_CLIPTEXT_SIZE) {      
96                id = msg_box (dlg, _("The file you want to add is very large.\n"
97                                     "Still proceed?"), _("Clipboard"), MB_INFO|MB_YESNO);      
98                if (id == IDNO)    
99                    return -1;
100            }
101            fp = fopen (file, "rb");
102            if (!fp) {
103                msg_box (dlg, winpt_strerror (WPTERR_FILE_OPEN), _("Clipboard"), MB_ERR);
104                return FALSE;
105            }
106            p = new char[size+1];
107            if (!p)
108                BUG (0);
109            fread (p, 1, size, fp);
110            p[size] = '\0';
111            fclose (fp);
112            SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p);        
113            free_if_alloc (p);
114        }
115        return 0;
116    }
117    
118    
119    /* Save the contents of the clipboard to the selected file. */
120    static int
121    save_clipboard_to_file (HWND dlg)
122    {
123        OPENFILENAME save;
124        FILE *fp;
125        char *p;
126        char file[300] = "";
127        DWORD nbytes;
128        int id;
129    
130        memset (&save, 0, sizeof (save));      
131        save.lStructSize = sizeof (OPENFILENAME);      
132        save.hInstance = glob_hinst;            
133        save.lpstrTitle = _("File Save");      
134        save.lpstrFile = (char *)_("All Files (*.*)\0*.*\0\0");        
135        save.hwndOwner = dlg;                  
136        save.lpstrFile = file;                  
137        save.nMaxFile = sizeof (file) - 1;
138        save.Flags = OFN_OVERWRITEPROMPT;
139                        
140        if( GetSaveFileName( &save ) ) {
141            if( file_exist_check( file ) == 0 ) {      
142                id = log_box (_("Clipboard"), MB_YESNO,
143                              _("\"%s\" already exists.\n"
144                                "Replace existing file?"), file);      
145                if( id == IDNO )    
146                    return FALSE;
147            }
148            fp = fopen (file, "wb");
149            if (!fp) {
150                msg_box (dlg, winpt_strerror (WPTERR_FILE_CREAT), _("Clipboard"), MB_ERR);
151                return FALSE;
152            }
153            /* XXX: there is a NUL-byte at the end. */
154            nbytes = SendDlgItemMessage (dlg, IDC_CLIPEDIT_CLIPTEXT2, WM_GETTEXTLENGTH, 0, 0);
155            if (nbytes > 0) {
156                p = new char[nbytes+1];
157                if (!p)
158                    BUG (NULL);
159                GetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p, nbytes);
160                fwrite (p, 1, nbytes, fp);
161            }          
162            fclose (fp);
163            free_if_alloc (p);
164        }
165        return 0;
166    }
167    
168    
169    /* Dialog box procedure for the clipboard editor. */
170    BOOL CALLBACK
171    clip_edit_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
172    {
173        switch (msg)  {
174      case WM_INITDIALOG:      case WM_INITDIALOG:
175          #ifndef LANG_DE          #ifndef LANG_DE
176          SetWindowText (dlg, _("Clipboard Editor"));          SetWindowText (dlg, _("Clipboard Editor"));
# Line 49  clip_edit_dlg_proc (HWND dlg, UINT msg, Line 179  clip_edit_dlg_proc (HWND dlg, UINT msg,
179          SetDlgItemText (dlg, IDC_CLIPEDIT_LOAD, _("&Load"));              SetDlgItemText (dlg, IDC_CLIPEDIT_LOAD, _("&Load"));    
180          SetDlgItemText (dlg, IDC_CLIPEDIT_SAVE, _("&Save"));          SetDlgItemText (dlg, IDC_CLIPEDIT_SAVE, _("&Save"));
181          #endif          #endif
182          CloseClipboard (); /* make sure it is closed. */          load_clipboard (dlg);
183          if( OpenClipboard( NULL ) == FALSE ) {          center_window (dlg, NULL);
             msg_box( dlg, winpt_strerror( WPTERR_CLIP_OPEN ), _("Clipboard"), MB_ERR );  
             return FALSE;  
         }  
         clipmem = GetClipboardData (CF_TEXT);  
         if (clipmem == NULL) {  
             center_window (dlg);  
             SetForegroundWindow (dlg);  
             CloseClipboard ();  
             return FALSE;  
         }  
         cliptext = (char *) GlobalLock (clipmem);  
         if (cliptext == NULL || !strlen (cliptext)) {  
             CloseClipboard ();  
             return FALSE;  
         }  
         p = new char [strlen (cliptext)+2];  
         if (!p)  
             BUG (0);  
         strcpy (p, cliptext);  
         GlobalUnlock (clipmem);  
         CloseClipboard ();  
         SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p);  
         free_if_alloc (p);  
         center_window (dlg);  
184          SetForegroundWindow (dlg);          SetForegroundWindow (dlg);
185          return TRUE;          return TRUE;
186                    
187      case WM_SYSCOMMAND:      case WM_SYSCOMMAND:
188          if (LOWORD (wparam) == SC_CLOSE)          if (LOWORD (wparam) == SC_CLOSE)
189              EndDialog (dlg, TRUE);              EndDialog (dlg, TRUE);
190          return FALSE;          return TRUE;
191                    
192      case WM_COMMAND:      case WM_COMMAND:
193          switch (LOWORD (wparam)) {          switch (LOWORD (wparam)) {
# Line 92  clip_edit_dlg_proc (HWND dlg, UINT msg, Line 198  clip_edit_dlg_proc (HWND dlg, UINT msg,
198          case IDC_CLIPEDIT_SEND:          case IDC_CLIPEDIT_SEND:
199              if (IsDlgButtonChecked (dlg, IDC_CLIPEDIT_QUOTE)) {              if (IsDlgButtonChecked (dlg, IDC_CLIPEDIT_QUOTE)) {
200                  gpgme_data_t txt;                  gpgme_data_t txt;
201                  gpgme_data_new_from_clipboard (&txt);                  gpg_data_new_from_clipboard (&txt, 0);
202                  gpgme_data_mail_quote (&txt);                  gpg_data_mail_quote (&txt);
203                  gpgme_data_release_and_set_clipboard (txt);                  gpg_data_release_and_set_clipboard (txt, 0);
204                  SendMessage (dlg, WM_INITDIALOG, 0, 0);                  SendMessage (dlg, WM_INITDIALOG, 0, 0);
205                  return TRUE;                  return TRUE;
206              }              }
# Line 103  clip_edit_dlg_proc (HWND dlg, UINT msg, Line 209  clip_edit_dlg_proc (HWND dlg, UINT msg,
209              return TRUE;              return TRUE;
210                            
211          case IDC_CLIPEDIT_CLEAR:          case IDC_CLIPEDIT_CLEAR:
212              if( OpenClipboard( NULL ) == FALSE ) {                    if (OpenClipboard (NULL) == FALSE) {
213                  msg_box( dlg,  winpt_strerror( WPTERR_CLIP_OPEN ), _("Clipboard"), MB_ERR );                  msg_box (dlg,  winpt_strerror (WPTERR_CLIP_OPEN),
214                  return FALSE;                           _("Clipboard"), MB_ERR);
215                    return TRUE;
216              }                                }                  
217              if (EmptyClipboard () == FALSE) {              if (EmptyClipboard () == FALSE)            
218                  CloseClipboard ();                  msg_box (dlg, winpt_strerror (WPTERR_CLIP_EMPTY),
219                  msg_box (dlg, winpt_strerror (WPTERR_CLIP_EMPTY), _("Clipboard"), MB_ERR);                           _("Clipboard"), MB_ERR);
220                  return FALSE;              else
221              }                  SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, "");
222              CloseClipboard ();              CloseClipboard ();
             SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, "");  
223              return TRUE;              return TRUE;
224                            
225          case IDC_CLIPEDIT_LOAD:          case IDC_CLIPEDIT_LOAD:
226              memset (&open, 0, sizeof (open));              if (!load_clipboard_from_file (dlg)) {
             open.lStructSize = sizeof (OPENFILENAME);  
             open.hInstance = glob_hinst;  
             open.lpstrTitle = _("File Open");  
             open.lpstrFilter = (char *)_("All Files (*.*)\0*.*");  
             open.hwndOwner = dlg;  
             open.lpstrFile = file;        
             open.nMaxFile = sizeof (file)-1;      
             open.Flags = 0;  
             if (GetOpenFileName (&open)) {  
                 if( (size=get_file_size( file )) > MAX_CLIPTEXT_SIZE ) {  
                     id = msg_box( dlg, _("The file you want to add is very large.\n"  
                                         "Still proceed?"), _("Clipboard"), MB_INFO|MB_YESNO );  
                     if( id == IDNO )  
                         return FALSE;  
                 }  
                 fp = fopen( file, "rb" );  
                 if (!fp) {  
                     msg_box (dlg, winpt_strerror (WPTERR_FILE_OPEN), _("Clipboard"), MB_ERR);  
                     return FALSE;  
                 }  
                 p = new char[size+1];  
                 if (!p)  
                     BUG (0);  
                 fread (p, 1, size, fp);  
                 p[size] = '\0';  
                 fclose (fp);  
                 SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p);  
                 free_if_alloc (p);  
227                  /* XXX: for some files the code produces a crash! */                  /* XXX: for some files the code produces a crash! */
228                  PostMessage (dlg, WM_COMMAND, MAKEWPARAM(IDC_CLIPEDIT_SEND, 0), NULL);                  PostMessage (dlg, WM_COMMAND, MAKEWPARAM(IDC_CLIPEDIT_SEND, 0), NULL);
                 return TRUE;  
229              }              }
230              break;              return TRUE;
231                            
232          case IDC_CLIPEDIT_SAVE:          case IDC_CLIPEDIT_SAVE:
233              memset (&open, 0, sizeof (open));              save_clipboard_to_file (dlg);          
234              open.lStructSize = sizeof (OPENFILENAME);              return TRUE;
             open.hInstance = glob_hinst;  
             open.lpstrTitle = _("File Save");  
             open.lpstrFile = (char *)_("All Files (*.*)\0*.*");  
             open.hwndOwner = dlg;        
             open.lpstrFile = file;        
             open.nMaxFile = sizeof (file) - 1;  
               
             if( GetSaveFileName( &open ) ) {  
                 if( file_exist_check( file ) == 0 ) {  
                     id = log_box (_("Clipboard"), MB_YESNO,  
                                   _("\"%s\" already exists.\n"  
                                   "Replace existing file?"), file);  
                     if( id == IDNO )  
                         return FALSE;  
                 }  
                 fp = fopen( file, "wb" );  
                 if( !fp ) {  
                     msg_box( dlg, winpt_strerror( WPTERR_FILE_CREAT ), _("Clipboard"), MB_ERR );  
                     return FALSE;                
                 }  
                 nbytes = SendDlgItemMessage( dlg, IDC_CLIPEDIT_CLIPTEXT2, WM_GETTEXTLENGTH, 0, 0 );  
                 if( nbytes > 0 ) {  
                     p = new char[nbytes+1];              
                     if( !p )  
                         BUG( NULL );  
                     GetDlgItemText( dlg, IDC_CLIPEDIT_CLIPTEXT2, p, nbytes );  
                     fwrite( p, 1, nbytes, fp );  
                 }            
                 fclose( fp );    
                 free_if_alloc( p );  
             }  
             break;  
235          }          }
236          break;          break;
237      }      }
238            
239      return FALSE;      return FALSE;
240  } /* clip_edit_dlg_proc */  }
241    

Legend:
Removed from v.12  
changed lines
  Added in v.32

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26