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

Legend:
Removed from v.34  
changed lines
  Added in v.88

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26