/[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 236 by twoaday, Wed Jun 28 06:59:30 2006 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-2006 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    #ifdef HAVE_CONFIG_H
21  #include <windows.h>  #include <config.h>
22  #include <stdio.h>  #endif
23    
24  #include "wptTypes.h"  #include <windows.h>
25  #include "wptW32API.h"  #include <stdio.h>
26  #include "wptVersion.h"  
27  #include "wptErrors.h"  #include "wptTypes.h"
28  #include "wptGPG.h"  #include "wptW32API.h"
29  #include "wptNLS.h"  #include "wptVersion.h"
30  #include "../resource.h"  #include "wptErrors.h"
31    #include "wptGPG.h"
32    #include "wptNLS.h"
33  BOOL CALLBACK  #include "resource.h"
34  clip_edit_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)  #include "wptCommonCtl.h"
35  {  #include "wptContext.h"
36      HANDLE clipmem;  #include "wptKeylist.h"
37      OPENFILENAME open;  
38      FILE * fp;  #ifdef _MSC_VER
39      char * cliptext = NULL, * newtext = NULL,  * p;  #include "winpt_header.h"
40      char file[512] = "";  
41      int nbytes, id, size;  static DWORD help_arr[] = {
42            IDC_CLIPEDIT_SEND,  WPT_CLIPEDIT_COPY,
43      switch ( msg )  {      IDC_CLIPEDIT_CLEAR, WPT_CLIPEDIT_CLEAR,
44      case WM_INITDIALOG:      IDC_CLIPEDIT_LOAD,  WPT_CLIPEDIT_LOAD,
45          #ifndef LANG_DE      IDC_CLIPEDIT_SAVE,  WPT_CLIPEDIT_SAVE,
46          SetWindowText (dlg, _("Clipboard Editor"));      IDC_CLIPEDIT_QUOTE, WPT_CLIPEDIT_QUOTE,
47          SetDlgItemText (dlg, IDC_CLIPEDIT_SEND, _("&Copy"));      0, 0};
48          SetDlgItemText (dlg, IDC_CLIPEDIT_CLEAR, _("Clea&r"));  #endif
49          SetDlgItemText (dlg, IDC_CLIPEDIT_LOAD, _("&Load"));      
50          SetDlgItemText (dlg, IDC_CLIPEDIT_SAVE, _("&Save"));  
51          #endif  void verify_get_clip_info (gpgme_signature_t sig,
52          CloseClipboard (); /* make sure it is closed. */                             char **r_header, char **r_footer);
53          if( OpenClipboard( NULL ) == FALSE ) {  
54              msg_box( dlg, winpt_strerror( WPTERR_CLIP_OPEN ), _("Clipboard"), MB_ERR );  
55              return FALSE;  /* Load the clipboard contents into the text field in @dlg.
56          }     Optinally @header and @footer can be used to set a header and footer.
57          clipmem = GetClipboardData (CF_TEXT);     Return value: 0 on success. */
58          if (clipmem == NULL) {  static int
59              center_window (dlg);  load_clipboard (HWND dlg,
60              SetForegroundWindow (dlg);                  const char *header, const char *footer)
61              CloseClipboard ();  {
62              return FALSE;      HANDLE clipmem;
63          }      char *cliptext;
64          cliptext = (char *) GlobalLock (clipmem);      char *p, *pp;
65          if (cliptext == NULL || !strlen (cliptext)) {  
66              CloseClipboard ();      CloseClipboard (); /* make sure it is closed. */
67              return FALSE;      if (OpenClipboard (NULL) == FALSE) {
68          }          msg_box (dlg, winpt_strerror (WPTERR_CLIP_OPEN), _("Clipboard"), MB_ERR);
69          p = new char [strlen (cliptext)+2];          return -1;
70          if (!p)      }
71              BUG (0);      clipmem = GetClipboardData (CF_TEXT);
72          strcpy (p, cliptext);      if (clipmem == NULL) {
73          GlobalUnlock (clipmem);          CloseClipboard ();
74          CloseClipboard ();          return -1;
75          SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p);      }
76          free_if_alloc (p);      
77          center_window (dlg);      cliptext = (char *) GlobalLock (clipmem);
78          SetForegroundWindow (dlg);      if (cliptext == NULL || !strlen (cliptext)) {    
79          return TRUE;          CloseClipboard ();
80                    return -1;
81      case WM_SYSCOMMAND:      }
82          if (LOWORD (wparam) == SC_CLOSE)  
83              EndDialog (dlg, TRUE);      p = new char [strlen (cliptext)+2];
84          return FALSE;      if (!p)    
85                    BUG (0);        
86      case WM_COMMAND:      strcpy (p, cliptext);
87          switch (LOWORD (wparam)) {      
88          case IDOK:                    GlobalUnlock (clipmem);
89              EndDialog (dlg, TRUE);      CloseClipboard ();
90              return TRUE;  
91                    if (header && footer) {
92          case IDC_CLIPEDIT_SEND:          const char *fmt = "%s\r\n%s\r\n%s";
93              if (IsDlgButtonChecked (dlg, IDC_CLIPEDIT_QUOTE)) {          int len;
94                  gpgme_data_t txt;  
95                  gpgme_data_new_from_clipboard (&txt);          len = strlen (header) + strlen (footer) + strlen (fmt) + strlen (p);
96                  gpgme_data_mail_quote (&txt);          pp = new char[len + 1];
97                  gpgme_data_release_and_set_clipboard (txt);          if (!pp)
98                  SendMessage (dlg, WM_INITDIALOG, 0, 0);              BUG (0);
99                  return TRUE;          _snprintf (pp, len, fmt, header, p, footer);
100              }          SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, pp);
101              SendDlgItemMessage (dlg, IDC_CLIPEDIT_CLIPTEXT2, EM_SETSEL, 0, -1);          free_if_alloc (pp);
102              SendDlgItemMessage (dlg, IDC_CLIPEDIT_CLIPTEXT2, WM_COPY, 0, 0);      }
103              return TRUE;      else
104                        SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p);
105          case IDC_CLIPEDIT_CLEAR:      free_if_alloc (p);
106              if( OpenClipboard( NULL ) == FALSE ) {            return 0;
107                  msg_box( dlg,  winpt_strerror( WPTERR_CLIP_OPEN ), _("Clipboard"), MB_ERR );  }
108                  return FALSE;  
109              }                    
110              if (EmptyClipboard () == FALSE) {  /* Load the clipboard from the selected file. */
111                  CloseClipboard ();  static int
112                  msg_box (dlg, winpt_strerror (WPTERR_CLIP_EMPTY), _("Clipboard"), MB_ERR);  load_clipboard_from_file (HWND dlg)
113                  return FALSE;  {
114              }      OPENFILENAME open;
115              CloseClipboard ();      FILE *fp;
116              SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, "");      char *p;
117              return TRUE;      char file[300] = "";
118                    DWORD size;
119          case IDC_CLIPEDIT_LOAD:      int id;
120              memset (&open, 0, sizeof (open));  
121              open.lStructSize = sizeof (OPENFILENAME);      memset (&open, 0, sizeof (open));
122              open.hInstance = glob_hinst;      open.lStructSize = sizeof (OPENFILENAME);
123              open.lpstrTitle = _("File Open");      open.hInstance = glob_hinst;
124              open.lpstrFilter = (char *)_("All Files (*.*)\0*.*");      open.lpstrTitle = _("File Open");
125              open.hwndOwner = dlg;      open.lpstrFilter = "All Files (*.*)\0*.*\0\0";
126              open.lpstrFile = file;            open.hwndOwner = dlg;          
127              open.nMaxFile = sizeof (file)-1;          open.lpstrFile = file;                  
128              open.Flags = 0;      open.nMaxFile = sizeof (file)-1;                
129              if (GetOpenFileName (&open)) {      open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
130                  if( (size=get_file_size( file )) > MAX_CLIPTEXT_SIZE ) {              
131                      id = msg_box( dlg, _("The file you want to add is very large.\n"      if (GetOpenFileName (&open)) {
132                                          "Still proceed?"), _("Clipboard"), MB_INFO|MB_YESNO );          if( (size=get_file_size (file)) > MAX_CLIPTEXT_SIZE) {
133                      if( id == IDNO )              id = msg_box (dlg, _("The file you want to add is very large.\n"
134                          return FALSE;                                   "Still proceed?"), _("Clipboard"),
135                  }                            MB_INFO|MB_YESNO);
136                  fp = fopen( file, "rb" );              if (id == IDNO)
137                  if (!fp) {                  return -1;
138                      msg_box (dlg, winpt_strerror (WPTERR_FILE_OPEN), _("Clipboard"), MB_ERR);          }
139                      return FALSE;          fp = fopen (file, "rb");
140                  }          if (!fp) {
141                  p = new char[size+1];              msg_box (dlg, winpt_strerror (WPTERR_FILE_OPEN),
142                  if (!p)                       _("Clipboard"), MB_ERR);
143                      BUG (0);              return -1;
144                  fread (p, 1, size, fp);          }
145                  p[size] = '\0';          p = new char[size+1];
146                  fclose (fp);          if (!p)
147                  SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p);              BUG (0);
148                  free_if_alloc (p);          memset (p, 0, size+1);
149                  /* XXX: for some files the code produces a crash! */          size = fread (p, 1, size, fp);
150                  PostMessage (dlg, WM_COMMAND, MAKEWPARAM(IDC_CLIPEDIT_SEND, 0), NULL);          fclose (fp);
151                  return TRUE;          SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p);        
152              }          free_if_alloc (p);
153              break;      }
154                    return 0;
155          case IDC_CLIPEDIT_SAVE:  }
156              memset (&open, 0, sizeof (open));  
157              open.lStructSize = sizeof (OPENFILENAME);  
158              open.hInstance = glob_hinst;  /* Save the contents of the clipboard to the selected file. */
159              open.lpstrTitle = _("File Save");  static int
160              open.lpstrFile = (char *)_("All Files (*.*)\0*.*");  save_clipboard_to_file (HWND dlg)
161              open.hwndOwner = dlg;        {
162              open.lpstrFile = file;            OPENFILENAME save;
163              open.nMaxFile = sizeof (file) - 1;      FILE *fp;
164                    char *p=NULL;
165              if( GetSaveFileName( &open ) ) {      char file[300] = "";
166                  if( file_exist_check( file ) == 0 ) {      DWORD nbytes;
167                      id = log_box (_("Clipboard"), MB_YESNO,      int id;
168                                    _("\"%s\" already exists.\n"  
169                                    "Replace existing file?"), file);      memset (&save, 0, sizeof (save));      
170                      if( id == IDNO )      save.lStructSize = sizeof (OPENFILENAME);      
171                          return FALSE;      save.hInstance = glob_hinst;            
172                  }      save.lpstrTitle = _("File Save");      
173                  fp = fopen( file, "wb" );      save.lpstrFile = "All Files (*.*)\0*.*\0\0";
174                  if( !fp ) {      save.hwndOwner = dlg;
175                      msg_box( dlg, winpt_strerror( WPTERR_FILE_CREAT ), _("Clipboard"), MB_ERR );      save.lpstrFile = file;
176                      return FALSE;                    save.nMaxFile = sizeof (file) - 1;
177                  }      save.Flags = OFN_OVERWRITEPROMPT;
178                  nbytes = SendDlgItemMessage( dlg, IDC_CLIPEDIT_CLIPTEXT2, WM_GETTEXTLENGTH, 0, 0 );  
179                  if( nbytes > 0 ) {      if (GetSaveFileName (&save)) {
180                      p = new char[nbytes+1];                      if (file_exist_check (file) == 0) {
181                      if( !p )              id = log_box (_("Clipboard"), MB_YESNO,
182                          BUG( NULL );                            _("\"%s\" already exists.\n"
183                      GetDlgItemText( dlg, IDC_CLIPEDIT_CLIPTEXT2, p, nbytes );                              "Replace existing file?"), file);      
184                      fwrite( p, 1, nbytes, fp );              if (id == IDNO)    
185                  }                            return -1;
186                  fclose( fp );            }
187                  free_if_alloc( p );          fp = fopen (file, "wb");
188              }          if (!fp) {
189              break;              msg_box (dlg, winpt_strerror (WPTERR_FILE_CREAT),
190          }                       _("Clipboard"), MB_ERR);
191          break;              return -1;
192      }          }
193                nbytes = SendDlgItemMessage (dlg, IDC_CLIPEDIT_CLIPTEXT2,
194      return FALSE;                                       WM_GETTEXTLENGTH, 0, 0);
195  } /* clip_edit_dlg_proc */          if (nbytes > 0) {
196                p = new char[nbytes+1];
197                if (!p)
198                    BUG (NULL);
199                GetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, p, nbytes);
200                fwrite (p, 1, nbytes, fp);
201                
202            }          
203            fclose (fp);
204            free_if_alloc (p);
205        }
206        return 0;
207    }
208    
209    
210    /* Dialog box procedure for the clipboard editor. */
211    BOOL CALLBACK
212    clip_edit_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
213    {
214        char *head=NULL, *foot=NULL;
215        gpgme_data_t txt;
216    
217        switch (msg)  {
218        case WM_INITDIALOG:
219            /* XXX: it would be really nice to have tempest prevention fonts. */
220            SetWindowText (dlg, _("Clipboard Editor"));
221            SetDlgItemText (dlg, IDC_CLIPEDIT_SEND, _("&Copy"));
222            SetDlgItemText (dlg, IDC_CLIPEDIT_CLEAR, _("Clea&r"));
223            SetDlgItemText (dlg, IDC_CLIPEDIT_LOAD, _("&Load"));    
224            SetDlgItemText (dlg, IDC_CLIPEDIT_SAVE, _("&Save"));
225            SetDlgItemText (dlg, IDC_CLIPEDIT_QUOTE, _("&Quote"));
226            SetDlgItemText (dlg, IDOK, _("&Close"));
227    
228            /* If there is a param, we expect a signature and initialize
229               the header and footer variable accordingly. */
230            if (lparam)
231                verify_get_clip_info ((gpgme_signature_t)lparam, &head, &foot);
232            load_clipboard (dlg, head, foot);
233            free_if_alloc (head);
234            free_if_alloc (foot);
235            center_window (dlg, NULL);
236            SetForegroundWindow (dlg);
237            return TRUE;
238    
239        case WM_HELP:
240            html_help_dispatch (lparam, "winpt.chm::winpt_texts.txt", help_arr);
241            break;
242            
243        case WM_COMMAND:
244            switch (LOWORD (wparam)) {
245            case IDCANCEL:
246                EndDialog (dlg, FALSE);
247                return TRUE;
248    
249            case IDOK:              
250                EndDialog (dlg, TRUE);
251                return TRUE;
252                
253            case IDC_CLIPEDIT_QUOTE:
254                if (gpg_data_new_from_clipboard (&txt, 0))
255                    return TRUE;
256                gpg_data_mail_quote (&txt);
257                gpg_data_release_and_set_clipboard (txt, 0);
258                load_clipboard (dlg, head, foot);
259                return TRUE;
260    
261            case IDC_CLIPEDIT_SEND:
262                SendDlgItemMessage (dlg, IDC_CLIPEDIT_CLIPTEXT2, EM_SETSEL, 0, -1);
263                SendDlgItemMessage (dlg, IDC_CLIPEDIT_CLIPTEXT2, WM_COPY, 0, 0);
264                return TRUE;
265                
266            case IDC_CLIPEDIT_CLEAR:
267                if (OpenClipboard (NULL) == FALSE) {
268                    msg_box (dlg,  winpt_strerror (WPTERR_CLIP_OPEN),
269                             _("Clipboard"), MB_ERR);
270                    return TRUE;
271                }                  
272                if (EmptyClipboard () == FALSE)            
273                    msg_box (dlg, winpt_strerror (WPTERR_CLIP_EMPTY),
274                             _("Clipboard"), MB_ERR);
275                else
276                    SetDlgItemText (dlg, IDC_CLIPEDIT_CLIPTEXT2, "");
277                CloseClipboard ();
278                return TRUE;
279                
280            case IDC_CLIPEDIT_LOAD:
281                if (!load_clipboard_from_file (dlg))
282                    PostMessage (dlg, WM_COMMAND, MAKEWPARAM (IDC_CLIPEDIT_SEND, 0), 0);
283                return TRUE;
284                
285            case IDC_CLIPEDIT_SAVE:
286                save_clipboard_to_file (dlg);          
287                return TRUE;
288            }
289            break;
290        }
291        
292        return FALSE;
293    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26