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

Legend:
Removed from v.2  
changed lines
  Added in v.120

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26