/[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 35 by twoaday, Wed Oct 26 11:20:09 2005 UTC revision 36 by werner, Thu Oct 27 15:25:13 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 <windows.h>
27  #include "wptErrors.h"  #include <stdio.h>
28  #include "wptGPG.h"  
29  #include "wptNLS.h"  #include "wptTypes.h"
30  #include "../resource.h"  #include "wptW32API.h"
31    #include "wptVersion.h"
32    #include "wptErrors.h"
33  /* Load the clipboard contents into the text field in @dlg.  #include "wptGPG.h"
34     Return value: 0 on success. */  #include "wptNLS.h"
35  static int  #include "../resource.h"
36  load_clipboard (HWND dlg)  
37  {  
38      HANDLE clipmem;  /* Load the clipboard contents into the text field in @dlg.
39      char *cliptext;     Return value: 0 on success. */
40      char *p;  static int
41    load_clipboard (HWND dlg)
42      CloseClipboard (); /* make sure it is closed. */  {
43      if (OpenClipboard (NULL) == FALSE) {      HANDLE clipmem;
44          msg_box (dlg, winpt_strerror (WPTERR_CLIP_OPEN), _("Clipboard"), MB_ERR);      char *cliptext;
45          return -1;      char *p;
46      }  
47      clipmem = GetClipboardData (CF_TEXT);      CloseClipboard (); /* make sure it is closed. */
48      if (clipmem == NULL) {      if (OpenClipboard (NULL) == FALSE) {
49          CloseClipboard ();          msg_box (dlg, winpt_strerror (WPTERR_CLIP_OPEN), _("Clipboard"), MB_ERR);
50          return -1;          return -1;
51      }      }
52            clipmem = GetClipboardData (CF_TEXT);
53      cliptext = (char *) GlobalLock (clipmem);      if (clipmem == NULL) {
54      if (cliptext == NULL || !strlen (cliptext)) {              CloseClipboard ();
55          CloseClipboard ();          return -1;
56          return -1;      }
57      }      
58        cliptext = (char *) GlobalLock (clipmem);
59      p = new char [strlen (cliptext)+2];      if (cliptext == NULL || !strlen (cliptext)) {    
60      if (!p)              CloseClipboard ();
61          BUG (0);                  return -1;
62      strcpy (p, cliptext);      }
63        
64      GlobalUnlock (clipmem);      p = new char [strlen (cliptext)+2];
65      CloseClipboard ();        if (!p)    
66      SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p);              BUG (0);        
67      free_if_alloc (p);      strcpy (p, cliptext);
68            
69      return 0;      GlobalUnlock (clipmem);
70  }      CloseClipboard ();  
71        SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p);    
72        free_if_alloc (p);
73  /* Load the clipboard from the selected file. */      
74  static int      return 0;
75  load_clipboard_from_file (HWND dlg)  }
76  {  
77      OPENFILENAME open;  
78      FILE *fp;  /* Load the clipboard from the selected file. */
79      char *p;  static int
80      char file[300] = "";  load_clipboard_from_file (HWND dlg)
81      DWORD size;  {
82      int id;      OPENFILENAME open;
83        FILE *fp;
84      memset (&open, 0, sizeof (open));      char *p;
85      open.lStructSize = sizeof (OPENFILENAME);            char file[300] = "";
86      open.hInstance = glob_hinst;                  DWORD size;
87      open.lpstrTitle = _("File Open");            int id;
88      open.lpstrFilter = (char *)_("All Files (*.*)\0*.*\0\0");        
89      open.hwndOwner = dlg;                memset (&open, 0, sizeof (open));
90      open.lpstrFile = file;                        open.lStructSize = sizeof (OPENFILENAME);      
91      open.nMaxFile = sizeof (file)-1;                      open.hInstance = glob_hinst;            
92      open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;      open.lpstrTitle = _("File Open");      
93                    open.lpstrFilter = (char *)_("All Files (*.*)\0*.*\0\0");      
94      if (GetOpenFileName (&open)) {      open.hwndOwner = dlg;          
95          if( (size=get_file_size (file)) > MAX_CLIPTEXT_SIZE) {            open.lpstrFile = file;                  
96              id = msg_box (dlg, _("The file you want to add is very large.\n"      open.nMaxFile = sizeof (file)-1;                
97                                   "Still proceed?"), _("Clipboard"), MB_INFO|MB_YESNO);            open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
98              if (id == IDNO)                  
99                  return -1;      if (GetOpenFileName (&open)) {
100          }          if( (size=get_file_size (file)) > MAX_CLIPTEXT_SIZE) {      
101          fp = fopen (file, "rb");              id = msg_box (dlg, _("The file you want to add is very large.\n"
102          if (!fp) {                                   "Still proceed?"), _("Clipboard"), MB_INFO|MB_YESNO);      
103              msg_box (dlg, winpt_strerror (WPTERR_FILE_OPEN), _("Clipboard"), MB_ERR);              if (id == IDNO)    
104              return FALSE;                  return -1;
105          }          }
106          p = new char[size+1];          fp = fopen (file, "rb");
107          if (!p)          if (!fp) {
108              BUG (0);              msg_box (dlg, winpt_strerror (WPTERR_FILE_OPEN), _("Clipboard"), MB_ERR);
109          fread (p, 1, size, fp);              return FALSE;
110          p[size] = '\0';          }
111          fclose (fp);          p = new char[size+1];
112          SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p);                  if (!p)
113          free_if_alloc (p);              BUG (0);
114      }          fread (p, 1, size, fp);
115      return 0;          p[size] = '\0';
116  }          fclose (fp);
117            SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p);        
118            free_if_alloc (p);
119  /* Save the contents of the clipboard to the selected file. */      }
120  static int      return 0;
121  save_clipboard_to_file (HWND dlg)  }
122  {  
123      OPENFILENAME save;  
124      FILE *fp;  /* Save the contents of the clipboard to the selected file. */
125      char *p;  static int
126      char file[300] = "";  save_clipboard_to_file (HWND dlg)
127      DWORD nbytes;  {
128      int id;      OPENFILENAME save;
129        FILE *fp;
130      memset (&save, 0, sizeof (save));            char *p;
131      save.lStructSize = sizeof (OPENFILENAME);            char file[300] = "";
132      save.hInstance = glob_hinst;                  DWORD nbytes;
133      save.lpstrTitle = _("File Save");            int id;
134      save.lpstrFile = (char *)_("All Files (*.*)\0*.*\0\0");          
135      save.hwndOwner = dlg;                        memset (&save, 0, sizeof (save));      
136      save.lpstrFile = file;                        save.lStructSize = sizeof (OPENFILENAME);      
137      save.nMaxFile = sizeof (file) - 1;      save.hInstance = glob_hinst;            
138      save.Flags = OFN_OVERWRITEPROMPT;      save.lpstrTitle = _("File Save");      
139                            save.lpstrFile = (char *)_("All Files (*.*)\0*.*\0\0");        
140      if( GetSaveFileName( &save ) ) {      save.hwndOwner = dlg;                  
141          if( file_exist_check( file ) == 0 ) {            save.lpstrFile = file;                  
142              id = log_box (_("Clipboard"), MB_YESNO,      save.nMaxFile = sizeof (file) - 1;
143                            _("\"%s\" already exists.\n"      save.Flags = OFN_OVERWRITEPROMPT;
144                              "Replace existing file?"), file);                            
145              if( id == IDNO )          if( GetSaveFileName( &save ) ) {
146                  return FALSE;          if( file_exist_check( file ) == 0 ) {      
147          }              id = log_box (_("Clipboard"), MB_YESNO,
148          fp = fopen (file, "wb");                            _("\"%s\" already exists.\n"
149          if (!fp) {                              "Replace existing file?"), file);      
150              msg_box (dlg, winpt_strerror (WPTERR_FILE_CREAT), _("Clipboard"), MB_ERR);              if( id == IDNO )    
151              return FALSE;                  return FALSE;
152          }          }
153          /* XXX: there is a NUL-byte at the end. */          fp = fopen (file, "wb");
154          nbytes = SendDlgItemMessage (dlg, IDC_CLIPEDIT_CLIPTEXT2, WM_GETTEXTLENGTH, 0, 0);          if (!fp) {
155          if (nbytes > 0) {              msg_box (dlg, winpt_strerror (WPTERR_FILE_CREAT), _("Clipboard"), MB_ERR);
156              p = new char[nbytes+1];              return FALSE;
157              if (!p)          }
158                  BUG (NULL);          /* XXX: there is a NUL-byte at the end. */
159              GetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p, nbytes);          nbytes = SendDlgItemMessage (dlg, IDC_CLIPEDIT_CLIPTEXT2, WM_GETTEXTLENGTH, 0, 0);
160              fwrite (p, 1, nbytes, fp);          if (nbytes > 0) {
161          }                        p = new char[nbytes+1];
162          fclose (fp);              if (!p)
163          free_if_alloc (p);                  BUG (NULL);
164      }              GetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p, nbytes);
165      return 0;              fwrite (p, 1, nbytes, fp);
166  }          }          
167            fclose (fp);
168            free_if_alloc (p);
169  /* Dialog box procedure for the clipboard editor. */      }
170  BOOL CALLBACK      return 0;
171  clip_edit_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  }
172  {  
173      switch (msg)  {  
174      case WM_INITDIALOG:  /* Dialog box procedure for the clipboard editor. */
175          SetWindowText (dlg, _("Clipboard Editor"));  BOOL CALLBACK
176          SetDlgItemText (dlg, IDC_CLIPEDIT_SEND, _("&Copy"));  clip_edit_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
177          SetDlgItemText (dlg, IDC_CLIPEDIT_CLEAR, _("Clea&r"));  {
178          SetDlgItemText (dlg, IDC_CLIPEDIT_LOAD, _("&Load"));          switch (msg)  {
179          SetDlgItemText (dlg, IDC_CLIPEDIT_SAVE, _("&Save"));      case WM_INITDIALOG:
180            SetWindowText (dlg, _("Clipboard Editor"));
181          load_clipboard (dlg);          SetDlgItemText (dlg, IDC_CLIPEDIT_SEND, _("&Copy"));
182          center_window (dlg, NULL);          SetDlgItemText (dlg, IDC_CLIPEDIT_CLEAR, _("Clea&r"));
183          SetForegroundWindow (dlg);          SetDlgItemText (dlg, IDC_CLIPEDIT_LOAD, _("&Load"));    
184          return TRUE;          SetDlgItemText (dlg, IDC_CLIPEDIT_SAVE, _("&Save"));
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), NULL);
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.35  
changed lines
  Added in v.36

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26