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

Legend:
Removed from v.25  
changed lines
  Added in v.65

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26