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

Legend:
Removed from v.24  
changed lines
  Added in v.47

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26