Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

htclipboard.cc

Go to the documentation of this file.
00001 /*
00002  *      HT Editor
00003  *      htclipboard.cc
00004  *
00005  *      Copyright (C) 1999-2002 Stefan Weyergraf (stefan@weyergraf.de)
00006  *
00007  *      This program is free software; you can redistribute it and/or modify
00008  *      it under the terms of the GNU General Public License version 2 as
00009  *      published by the Free Software Foundation.
00010  *
00011  *      This program is distributed in the hope that it will be useful,
00012  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *      GNU General Public License for more details.
00015  *
00016  *      You should have received a copy of the GNU General Public License
00017  *      along with this program; if not, write to the Free Software
00018  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 #include "htclipboard.h"
00022 #include "htctrl.h"
00023 #include "htsearch.h"
00024 #include "htstring.h"
00025 #include "snprintf.h"
00026 #include "tools.h"
00027 
00028 #include <stdlib.h>
00029 #include <time.h>
00030 
00031 ht_clipboard *clipboard;
00032 
00033 class ht_clipboard_copy_history: public Object {
00034 public:
00035         virtual ~ht_clipboard_copy_history() {
00036                 if (source) free(source);
00037         }
00038 
00039         char *source;
00040         dword start;
00041         dword size;
00042         time_t time;
00043 };
00044 
00045 /*
00046  *      CLASS ht_clipboard
00047  */
00048 
00049 void ht_clipboard::init()
00050 {
00051         ht_mem_file::init(0, 16, FAM_READ | FAM_WRITE);
00052         copy_history=new ht_clist();
00053         ((ht_clist*)copy_history)->init();
00054         select_start=0;
00055         select_len=0;
00056 }
00057 
00058 void ht_clipboard::done()
00059 {
00060         copy_history->destroy();
00061         delete copy_history;
00062         ht_mem_file::done();
00063 }
00064 
00065 void ht_clipboard::clear()
00066 {
00067         ht_mem_file::truncate(0);
00068         copy_history->destroy();
00069         delete copy_history;
00070         copy_history=new ht_clist();
00071         ((ht_clist*)copy_history)->init();
00072         htmsg m;
00073         m.msg=msg_file_changed;
00074         m.data1.ptr=this;
00075         m.type=mt_broadcast;
00076         app->sendmsg(&m);
00077 }
00078 
00079 UINT    ht_clipboard::write(const void *buf, UINT size)
00080 {
00081         htmsg m;
00082         m.msg=msg_file_changed;
00083         m.data1.ptr=this;
00084         m.type=mt_broadcast;
00085         app->sendmsg(&m);
00086         return ht_mem_file::write(buf, size);
00087 }
00088 
00089 /*
00090  *      CLASS ht_clipboard_viewer
00091  */
00092 
00093 void ht_clipboard_viewer::init(bounds *b, char *desc, int caps, ht_clipboard *clipb, ht_format_group *format_group)
00094 {
00095         ht_uformat_viewer::init(b, desc, caps, clipboard, format_group);
00096         
00097         search_caps|=SEARCHMODE_BIN | SEARCHMODE_EVALSTR | SEARCHMODE_EXPR;
00098 
00099         lastentrycount=999999999;
00100         update_content();
00101 }
00102 
00103 void ht_clipboard_viewer::draw()
00104 {
00105         update_content();
00106         ht_uformat_viewer::draw();
00107 }
00108 
00109 void ht_clipboard_viewer::handlemsg(htmsg *msg)
00110 {
00111         if (msg->msg == msg_file_changed) {
00112                 if (msg->data1.ptr == clipboard) {
00113                         dirtyview();
00114                         clearmsg(msg);
00115                         return;
00116                 }
00117         }
00118         ht_uformat_viewer::handlemsg(msg);
00119 }
00120 
00121 void ht_clipboard_viewer::pselect_add(FILEOFS start, FILEOFS end)
00122 {
00123         ht_uformat_viewer::pselect_add(start, end);
00124         selection_changed();
00125 }
00126 
00127 void ht_clipboard_viewer::pselect_set(FILEOFS start, FILEOFS end)
00128 {
00129         ht_uformat_viewer::pselect_set(start, end);
00130         selection_changed();
00131 }
00132 
00133 void ht_clipboard_viewer::selection_changed()
00134 {
00135         FILEOFS s, e;
00136         pselect_get(&s, &e);
00137         clipboard->select_start=s;
00138         clipboard->select_len=e-s;
00139 }
00140 
00141 void ht_clipboard_viewer::update_content()
00142 {
00143         if (clipboard->copy_history->count()==lastentrycount) return;
00144         clear_subs();
00145         ht_clipboard *clipboard=(ht_clipboard*)file;
00146         int c=clipboard->copy_history->count();
00147         char title[512];        /* secure */
00148 
00149         for (int i=0; i<c; i++) {
00150                 ht_clipboard_copy_history *j=(ht_clipboard_copy_history*)clipboard->copy_history->get(i);
00151 
00152                 tm *t=localtime(&j->time);
00153                 ht_snprintf(title, sizeof title, "*** %02d:%02d:%02d, size %d(%xh), from %s", t->tm_hour, t->tm_min, t->tm_sec, j->size, j->size, j->source);
00154 
00155                 ht_mask_sub *m=new ht_mask_sub();
00156                 m->init(clipboard, i);
00157                 m->add_mask(title);
00158                 insertsub(m);
00159 
00160                 ht_hex_sub *h=new ht_hex_sub();
00161                 h->init(clipboard, j->start, j->size, 16, 0);
00162                 insertsub(h);
00163         }
00164         pselect_set(clipboard->select_start, clipboard->select_start+clipboard->select_len);
00165         lastentrycount=clipboard->copy_history->count();
00166 
00167         sendmsg(msg_complete_init, 0);
00168 }
00169 
00170 void ht_clipboard_viewer::get_pindicator_str(char *buf)
00171 {
00172         FILEOFS o;
00173         FILEOFS sel_start, sel_end;
00174         pselect_get(&sel_start, &sel_end);
00175         if (get_current_offset(&o)) {
00176                 char ttemp[1024];
00177                 if (sel_end-sel_start > 0) {
00178                         ht_snprintf(ttemp, sizeof ttemp, "selection %xh-%xh (%d byte%s)", sel_start, sel_end-1, sel_end-sel_start, sel_end-sel_start==1?"":"s");
00179                 } else {
00180                         ttemp[0]=0;
00181                 }
00182                 // FIXME: sizeof buf
00183                 ht_snprintf(buf, 1024, " %s %xh/%u %s", edit() ? "edit" : "view", o, o, ttemp);
00184         } else {
00185                 strcpy(buf, "?");
00186         }
00187 }
00188 
00189 /* clipboard functions */
00190 
00191 void clipboard_add_copy_history_entry(char *source, dword start, dword size, time_t time)
00192 {
00193         ht_clipboard_copy_history *h=new ht_clipboard_copy_history();
00194         h->source=ht_strdup(source);
00195         h->start=start;
00196         h->size=size;
00197         h->time=time;
00198         clipboard->copy_history->insert(h);
00199 }
00200 
00201 #define CLIPBOARD_TRANSFER_BUF_SIZE     32*1024
00202 //#define CLIPBOARD_TRANSFER_BUF_SIZE   2
00203 
00204 int clipboard_copy(char *source_desc, void *buf, dword len)
00205 {
00206         int r=0;
00207         if (len) {
00208                 dword size=clipboard->get_size();
00209                 clipboard->seek(size);
00210                 r=clipboard->write(buf, len);
00211                 clipboard->select_start=size;
00212                 clipboard->select_len=r;
00213                 clipboard_add_copy_history_entry(source_desc, size, r, time(0));
00214         }               
00215         return r;
00216 }
00217 
00218 int clipboard_copy(char *source_desc, ht_streamfile *file, dword offset, dword len)
00219 {
00220         if (!len) return 0;
00221 
00222         dword size=clipboard->get_size();
00223         dword temp=file->tell();
00224         dword cpos=size, spos=offset;
00225         byte *buf=(byte*)malloc(CLIPBOARD_TRANSFER_BUF_SIZE);
00226         dword l=len, r=0;
00227 
00228         while ((len) && (l)) {
00229                 l=len;
00230                 if (l>CLIPBOARD_TRANSFER_BUF_SIZE) l=CLIPBOARD_TRANSFER_BUF_SIZE;
00231                 file->seek(spos);
00232                 l=file->read(buf, l);
00233                 spos+=l;
00234                 clipboard->seek(cpos);
00235                 clipboard->write(buf, l);
00236                 cpos+=l;
00237                 len-=l;
00238                 r+=l;
00239         }
00240         file->seek(temp);
00241         clipboard->select_start=size;
00242         clipboard->select_len=r;
00243         clipboard_add_copy_history_entry(source_desc, size, r, time(0));
00244         free(buf);
00245         
00246         return r;
00247 }
00248 
00249 int clipboard_paste(void *buf, dword maxlen)
00250 {
00251         clipboard->seek(clipboard->select_start);
00252         return clipboard->read(buf, MIN(clipboard->select_len, maxlen));
00253 }
00254 
00255 int clipboard_paste(ht_streamfile *file, dword offset)
00256 {
00257         dword len=clipboard->select_len;
00258         dword temp=file->tell();
00259         dword cpos=clipboard->select_start, spos=offset;
00260         byte *buf=(byte*)malloc(CLIPBOARD_TRANSFER_BUF_SIZE);
00261         dword l=len, r=0;
00262         while ((len) && (l)) {
00263                 l=len;
00264                 if (l>CLIPBOARD_TRANSFER_BUF_SIZE) l=CLIPBOARD_TRANSFER_BUF_SIZE;
00265                 clipboard->seek(cpos);
00266                 l=clipboard->read(buf, l);
00267                 cpos+=l;
00268                 file->seek(spos);
00269                 file->write(buf, l);
00270                 spos+=l;
00271                 len-=l;
00272                 r+=l;
00273         }
00274         file->seek(temp);
00275         free(buf);
00276         return r;
00277 }
00278 
00279 int clipboard_clear()
00280 {
00281         clipboard->clear();
00282         return 1;
00283 }
00284 
00285 dword clipboard_getsize()
00286 {
00287         return clipboard->select_len;
00288 }
00289 
00290 /*
00291  *      INIT
00292  */
00293 
00294 bool init_clipboard()
00295 {
00296         clipboard=new ht_clipboard();
00297         clipboard->init();
00298         return 1;
00299 }
00300 
00301 /*
00302  *      DONE
00303  */
00304 
00305 void done_clipboard()
00306 {
00307         clipboard->done();
00308         delete clipboard;
00309 }
00310 

Generated on Fri May 7 21:15:31 2004 by doxygen 1.3.5