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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26