forked from sebastianbiallas/ht
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htapp.cc
3402 lines (2981 loc) · 73.1 KB
/
htapp.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* HT Editor
* htapp.cc
*
* Copyright (C) 1999-2002 Stefan Weyergraf
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "analy.h"
#include "cmds.h"
#include "log.h"
#include "mfile.h"
#include "htapp.h"
#include "atom.h"
#include "display.h"
#include "htcfg.h"
#include "htclipboard.h"
#include "htdialog.h"
#include "hteval.h"
#include "hthist.h"
#include "htidle.h"
#include "htinfo.h"
#include "htiobox.h"
#include "keyb.h"
#include "htmenu.h"
#include "htpal.h"
#include "htsearch.h"
#include "strtools.h"
#include "sys.h"
#include "httree.h"
#include "infoview.h"
#include "snprintf.h"
#include "stream.h"
#include "textedit.h"
#include "textfile.h"
#include "tools.h"
#include "vfsview.h"
#include "formats.h"
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <exception>
extern "C" {
#include "regex.h"
}
#define ATOM_HT_APP MAGIC32("APP\x00")
#define ATOM_HT_PROJECT MAGIC32("APP\x01")
#define ATOM_HT_PROJECT_ITEM MAGIC32("APP\x02")
#define ATOM_COMPARE_KEYS_PROJECT_ITEM MAGIC32("APP\x10")
#define HT_PROJECT_CONFIG_SUFFIX ".htprj"
#define HT_FILE_CONFIG_SUFFIX ".htcfg"
ht_log *loglines;
/*
* CLASS ht_help_window
*/
class ht_help_window : public ht_window {
public:
/* overwritten */
virtual void handlemsg(htmsg *msg);
};
void ht_help_window::handlemsg(htmsg *msg)
{
ht_window::handlemsg(msg);
if (msg->msg == msg_keypressed) {
switch (msg->data1.integer) {
case K_Escape: {
htmsg m;
m.type = mt_empty;
m.msg = cmd_window_close;
((ht_app*)app)->queuemsg(app, m);
clearmsg(msg);
return;
}
}
}
}
bool file_new_dialog(uint *mode)
{
Bounds b, c;
app->getbounds(&b);
b.x = (b.w - 40) / 2,
b.y = (b.h - 8) / 2;
b.w = 40;
b.h = 8;
ht_dialog *d=new ht_dialog();
d->init(&b, "create new file", FS_KILLER | FS_TITLE | FS_MOVE | FS_RESIZE);
b.x=0;
b.y=0;
/* mode (input) */
c=b;
c.x=0;
c.y=1;
c.w=b.w-2-c.x;
c.h=b.h-2-c.y;
ht_text_listbox *mode_input = new ht_text_listbox();
mode_input->init(&c);
mode_input->insert_str(FOM_TEXT, "text");
mode_input->insert_str(FOM_BIN, "binary");
mode_input->update();
d->insert(mode_input);
/* mode (text) */
c=b;
c.x=0;
c.y=0;
c.w=12;
c.h=1;
ht_label *mode_text=new ht_label();
mode_text->init(&c, "choose ~type", mode_input);
d->insert(mode_text);
bool retval = false;
if (d->run(false)) {
ht_listbox_data type;
ViewDataBuf vdb(mode_input, &type, sizeof type);
*mode = mode_input->getID(type.data->cursor_ptr);
retval = true;
}
d->done();
delete d;
return retval;
}
/*
* class FileBrowserVfsListbox
*/
class FileBrowser;
#define FileBrowserVfsListboxData VfsListboxData
class FileBrowserVfsListbox: public VfsListbox {
protected:
FileBrowser *file_browser;
public:
void init(Bounds *b, List *vfs_list, ht_text *show_pos, FileBrowser *file_browser);
/* overwritten */
virtual void stateChanged();
};
/*
* class FileBrowser
*/
struct FileBrowserData {
ht_strinputfield_data name;
ht_listbox_data listbox;
};
class FileBrowser: public ht_dialog {
protected:
ht_strinputfield *name_input;
FileBrowserVfsListbox *listbox;
public:
virtual void init(Bounds *b, Bounds *clientarea, const char *title, const char *starturl);
virtual void setstate(int state, int return_val);
virtual bool extract_url(char *buf);
virtual void listbox_changed();
};
/**/
void FileBrowserVfsListbox::init(Bounds *b, List *vfs_list, ht_text *show_pos, FileBrowser *fb)
{
file_browser = NULL;
VfsListbox::init(b, vfs_list, show_pos);
file_browser = fb;
}
void FileBrowserVfsListbox::stateChanged()
{
if (file_browser) file_browser->listbox_changed();
VfsListbox::stateChanged();
}
/**/
void FileBrowser::init(Bounds *n, Bounds *clientarea, const char *title, const char *starturl)
{
ht_dialog::init(n, title, FS_KILLER | FS_TITLE | FS_MOVE | FS_RESIZE);
Bounds b = *clientarea, c;
/* name (input) */
c = b;
c.x = 1;
c.y = 1;
c.w -= 2;
c.h = 1;
List *hist = (List*)getAtomValue(HISTATOM_FILE);
name_input = new ht_strinputfield();
name_input->init(&c, 128, hist);
insert(name_input);
/* name (text) */
c = b;
c.x = 1;
c.y = 0;
c.w = 9;
c.h = 1;
ht_label *name_text = new ht_label();
name_text->init(&c, "~name", name_input);
insert(name_text);
/* vfslistbox */
c = b;
c.x = 1;
c.y = 4;
c.w -= 2;
c.h -= 4;
listbox = new FileBrowserVfsListbox();
listbox->init(&c, virtual_fs_list, NULL, this);
listbox->changeURL(starturl);
insert(listbox);
/* vfslistbox (text) */
c.assign(1, 3, 9, 1);
ht_label *name_listbox = new ht_label();
name_listbox->init(&c, "~files", listbox);
insert(name_listbox);
}
bool FileBrowser::extract_url(char *buf)
{
ht_strinputfield_data i;
ViewDataBuf vdb(name_input, &i, sizeof i);
/* ht_text_listbox_item *t = (ht_text_listbox_item*)listbox->getbyid(d.listbox.cursor_id);
vfs_extra *x = (vfs_extra*)t->extra_data;*/
Vfs *vfs = listbox->getCurVfs();
if (vfs) {
char fname[VFS_URL_MAX];
String res;
bin2str(fname, i.text, i.textlen);
vfs->canonicalize(res, fname, listbox->getCurDir());
int buflen = ht_snprintf(buf, VFS_URL_MAX, "%s:%y", listbox->getCurProto(), &res);
return true;
}
return false;
}
void FileBrowser::listbox_changed()
{
FileBrowserVfsListboxData l;
ViewDataBuf vdb(listbox, &l, sizeof l);
ht_text_listbox_item *t = (ht_text_listbox_item*)l.data->cursor_ptr;
if (t) {
vfs_extra *x = (vfs_extra*)t->extra_data;
ht_strinputfield_data i;
i.textlen = strlen(x->name);
i.text = (byte*)x->name;
name_input->databuf_set(&i, sizeof i);
}
}
void FileBrowser::setstate(int state, int return_val)
{
if (state == ds_term_ok) {
ht_strinputfield_data i;
ViewDataBuf vdb(name_input, &i, sizeof i);
pstat_t s;
String fn(i.text, i.textlen);
int e = sys_pstat(s, fn.contentChar());
if (e == 0 && (s.caps & pstat_mode_type) && (s.mode & HT_S_IFDIR)) {
fn.prepend("local:");
listbox->changeURL(fn.contentChar());
return;
}
}
ht_dialog::setstate(state, return_val);
}
/**/
bool file_chooser(const char *title, char *buf, int bufsize)
{
Bounds b, c;
app->getbounds(&b);
c = b;
b.w = 60;
b.h = 20;
b.x = (c.w - b.w) / 2,
b.y = (c.h - b.h) / 2;
c = b;
c.x = 0;
c.y = 0;
c.w -= 2;
c.h -= 3;
// FIXME: hacked!
char cwd[HT_NAME_MAX];
strcpy(cwd, "local:");
if (!getcwd(cwd+6, (sizeof cwd)-6)) {
cwd[6] = 0;
}
FileBrowser *d = new FileBrowser();
d->init(&b, &c, title, cwd);
List *hist = (List*)getAtomValue(HISTATOM_FILE);
/* go! */
if (d->run(false)) {
char b[VFS_URL_MAX];
d->extract_url(b);
// FIXME: urls not fully supported...
if (ht_strncmp(b, "local:", 6) == 0) {
ht_strlcpy(buf, b+6, bufsize);
if (hist) insert_history_entry(hist, buf, NULL);
d->done();
delete d;
return true;
}
}
d->done();
delete d;
return false;
}
/**/
bool file_open_dialog(char **name, uint *mode)
{
Bounds b, c;
app->getbounds(&b);
c = b;
b.w = 60;
b.h = 20;
b.x = (c.w - b.w) / 2,
b.y = (c.h - b.h) / 2;
c = b;
c.x = 0;
c.y = 0;
c.w -= 2;
c.h -= 5;
// FIXME: hacked!
char cwd[HT_NAME_MAX];
strcpy(cwd, "local:");
if (!getcwd(cwd+6, (sizeof cwd)-6)) {
cwd[6] = 0;
}
FileBrowser *d = new FileBrowser();
d->init(&b, &c, "open file", cwd);
List *hist = (List*)getAtomValue(HISTATOM_FILE);
/* mode (input) */
c = b;
c.x = 6;
c.y = b.h-4;
c.w = 12;
c.h = 1;
ht_listpopup *mode_input = new ht_listpopup();
mode_input->init(&c);
mode_input->insertstring("autodetect");
mode_input->insertstring("binary");
mode_input->insertstring("text");
mode_input->growmode = MK_GM(GMH_LEFT, GMV_BOTTOM);
d->insert(mode_input);
/* mode (text) */
c = b;
c.x = 1;
c.y = b.h-4;
c.w = 9;
c.h = 1;
ht_label *mode_text = new ht_label();
mode_text->init(&c, "~mode", mode_input);
mode_text->growmode = MK_GM(GMH_LEFT, GMV_BOTTOM);
d->insert(mode_text);
/* go! */
if (d->run(false)) {
struct {
FileBrowserData browser;
ht_listpopup_data mode;
} data;
ViewDataBuf vdb(d, &data, sizeof data);
char buf[VFS_URL_MAX];
d->extract_url(buf);
// FIXME: urls not fully supported...
if (ht_strncmp(buf, "local:", 6) == 0) {
*name = ht_strdup(buf+6);
if (hist) insert_history_entry(hist, *name, 0);
switch (data.mode.cursor_pos) {
case 0: *mode=FOM_AUTO; break;
case 1: *mode=FOM_BIN; break;
case 2: *mode=FOM_TEXT; break;
}
d->done();
delete d;
return true;
}
}
d->done();
delete d;
return false;
}
static uint autodetect_file_open_mode(const char *filename)
{
#define AUTODETECT_SIZE 128
FILE *f=fopen(filename, "rb");
uint r=FOM_BIN;
if (f) {
byte buf[AUTODETECT_SIZE];
int c=fread(buf, 1, AUTODETECT_SIZE, f);
/* empty files are text files */
if (!c) return FOM_TEXT;
bool is_bin=false;
uint prob_bin_chars=0;
for (int i=0; i<c; i++) {
if (buf[i]==0) {
is_bin=true;
break;
} else if (buf[i]<32) {
prob_bin_chars++;
} else if (buf[i]>0xa9) {
prob_bin_chars++;
}
}
if (c) {
if (prob_bin_chars*100/c>=50) is_bin=true;
} else is_bin=true;
if (!is_bin) r=FOM_TEXT;
fclose(f);
return r;
}
return FOM_BIN;
}
void file_window_load_fcfg_func(ObjectStream &f, void *context)
{
ht_file_window *w = (ht_file_window*)context;
pstat_t p;
FileOfs oldsize = GETX_INT64D(f, "filesize");
uint32 oldtime = GETX_INT32X(f, "filetime");
FileOfs newsize = w->file->getSize();
w->file->pstat(p);
uint32 newtime = (p.caps & pstat_mtime) ? p.mtime : 0;
if (newsize != oldsize || newtime != oldtime) {
char s_oldtime[64], s_newtime[64];
struct tm *t;
time_t tt = newtime;
t = localtime(&tt);
strftime(s_newtime, sizeof s_newtime, "%X %d %b %Y", t);
tt = oldtime;
t = localtime(&tt);
strftime(s_oldtime, sizeof s_oldtime, "%X %d %b %Y", t);
String fn;
if (confirmbox_c("\ecconfig file applies to different version of file '%y'.\n\n"
"\elcurrent: %10qd %s\n\elold: %10qd %s\n\n"
"\ecload config file?", &w->file->getDesc(fn), newsize, s_newtime, oldsize, s_oldtime) != button_yes) {
return;
}
}
Analyser *a = f.getObject("analyser");
htmsg m;
m.msg = msg_set_analyser;
m.type = mt_broadcast;
m.data1.ptr = a;
w->sendmsg(&m);
}
void file_window_store_fcfg_func(ObjectStream &f, void *context)
{
ht_file_window *w = (ht_file_window*)context;
htmsg m;
m.msg = msg_get_analyser;
m.type = mt_broadcast;
m.data1.ptr = NULL;
w->sendmsg(&m);
if (m.msg == msg_retval && m.data1.ptr) {
pstat_t s;
w->file->pstat(s);
uint32 t = (s.caps & pstat_mtime) ? s.mtime : 0;
PUTX_INT64D(f, w->file->getSize(), "filesize");
PUTX_INT32X(f, t, "filetime");
Analyser *a = (Analyser*)m.data1.ptr;
f.putObject(a, "analyser");
}
}
void file_project_store_fcfg_func(ObjectStream &f, void *context)
{
PUT_OBJECT(f, project);
}
void file_project_load_fcfg_func(ObjectStream &f, void *context)
{
GET_OBJECT(f, project);
}
/*
* app_stream_error_func()
*/
#if 0
int app_stream_error_func(ht_stream *stream)
{
int err=stream->get_error();
const char *name = stream->get_desc();
if (err & STERR_SYSTEM) {
err=err&0xffff;
switch (err) {
case 4: { /* EACCES*/
#ifdef DJGPP
struct stat sbuf;
stat(name, &sbuf);
if (!(sbuf.st_mode & S_IWUSR)) {
if (msgbox(btmask_yes | btmask_no, "title", 1, align_center, "%s: stream error (Permission denied), seems to be a (DOS) read-only file. Change attribute?", name)==button_yes) {
if (chmod(name, S_IRUSR | S_IWUSR)) {
errorbox_modal("%s: error (%04x) changing attribute", name, errno & 0xffff);
} else {
stat(name, &sbuf);
if (!(sbuf.st_mode & S_IWUSR)) {
errorbox_modal("%s: error (%04x) changing attribute", name, errno & 0xffff);
} else {
return SERR_RETRY;
}
}
}
}
break;
#endif
}
default:
errorbox_modal("%s: stream error %04x: %s", name, err, strerror(err));
}
} else {
err=err&0xffff;
errorbox_modal("%s: internal stream error %04x", name, err);
}
return SERR_FAIL;
}
#endif
/*
* app_out_of_memory_proc()
*/
void *app_memory_reserve = NULL;
int app_out_of_memory_proc(int size)
{
if (app_memory_reserve) {
free(app_memory_reserve);
app_memory_reserve = 0;
warnbox_modal("the memory is getting low...");
return OUT_OF_MEMORY_RETRY;
} else {
return OUT_OF_MEMORY_FAIL;
}
}
/*
* CLASS ht_project
*/
ht_project::ht_project(const char *fn)
: AVLTree(true)
{
filename = ht_strdup(fn);
}
ht_project::~ht_project()
{
free(filename);
}
const char *ht_project::get_filename()
{
return filename;
}
void ht_project::load(ObjectStream &s)
{
// FIXME: probably not The Right Thing
String fn;
filename = ht_strdup(s.getDesc(fn).contentChar());
AVLTree::load(s);
}
ObjectID ht_project::getObjectID() const
{
return ATOM_HT_PROJECT;
}
void ht_project::store(ObjectStream &s) const
{
AVLTree::store(s);
}
/*
* CLASS ht_project_item
*/
ht_project_item::ht_project_item(const char *f, const char *p)
{
filename = ht_strdup(f);
path = ht_strdup(p);
}
ht_project_item::~ht_project_item()
{
free(filename);
free(path);
}
const char *ht_project_item::get_filename() const
{
return filename;
}
const char *ht_project_item::get_path() const
{
return path;
}
int ht_project_item::compareTo(const Object *obj) const
{
ht_project_item *b = (ht_project_item *)obj;
int c = sys_filename_cmp(get_path(), b->get_path());
return (c == 0) ? sys_filename_cmp(get_filename(), b->get_filename()) : c;
}
void ht_project_item::load(ObjectStream &s)
{
GET_STRING(s, path);
GET_STRING(s, filename);
}
ObjectID ht_project_item::getObjectID() const
{
return ATOM_HT_PROJECT_ITEM;
}
void ht_project_item::store(ObjectStream &s) const
{
PUT_STRING(s, path);
PUT_STRING(s, filename);
}
/*
* CLASS ht_project_list
*/
void ht_project_listbox::init(Bounds *b, ht_project *p)
{
project = p;
ht_listbox::init(b);
colwidths[0] = 16;
colwidths[1] = 16;
}
int ht_project_listbox::calcCount()
{
return project ? project->count() : 0;
}
void ht_project_listbox::draw()
{
if (project) {
ht_listbox::draw();
} else {
vcp fc = focused ? getcolor(palidx_generic_list_focused_unselected) :
getcolor(palidx_generic_list_unfocused_unselected);
clear(fc);
buf->print(0, 0, fc, "<no project>");
}
}
const char *ht_project_listbox::func(uint i, bool execute)
{
return NULL;
}
void *ht_project_listbox::getFirst()
{
if (project && project->count()) {
return (void*)1;
} else {
return NULL;
}
}
void *ht_project_listbox::getLast()
{
if (project && project->count()) {
return (void*)(project->count());
} else {
return NULL;
}
}
void *ht_project_listbox::getNext(void *entry)
{
unsigned long e=(unsigned long)entry;
if (!e) return NULL;
if (project && (e < project->count())) {
return (void*)(e+1);
} else {
return NULL;
}
}
void *ht_project_listbox::getPrev(void *entry)
{
unsigned long e=(unsigned long)entry;
if (e > 1) {
return (void*)(e-1);
} else {
return NULL;
}
}
const char *ht_project_listbox::getStr(int col, void *entry)
{
static char mybuf[32];
if (project) switch (col) {
case 0:
ht_snprintf(mybuf, sizeof mybuf, "%s", ((ht_project_item*)(*project)[(long)entry-1])->get_filename());
break;
case 1:
ht_snprintf(mybuf, sizeof mybuf, "%s", ((ht_project_item*)(*project)[(long)entry-1])->get_path());
break;
default:
strcpy(mybuf, "?");
} else {
strcpy(mybuf, "<no project>");
}
return mybuf;
}
void ht_project_listbox::handlemsg(htmsg *msg)
{
switch (msg->msg) {
case cmd_project_add_item: {
if (project) {
char fn[FILENAME_MAX];
if (file_chooser("Add project item", fn, sizeof fn)) {
char ffn[HT_NAME_MAX];
char *dir;
if (sys_common_canonicalize(ffn, sizeof ffn, fn, NULL, sys_is_path_delim) == 0
&& sys_basename(fn, ffn) == 0
&& (dir = sys_dirname(ffn)) ) {
ht_project_item *p = new ht_project_item(fn, dir);
((ht_project*)project)->insert(p);
app->sendmsg(msg_project_changed);
free(dir);
}
}
}
clearmsg(msg);
return;
}
case cmd_project_remove_item: {
int p = pos;
ht_project_item *pi = (ht_project_item*)(*project)[p];
if (calcCount() && confirmbox("Really remove item '%s'?", pi->get_filename()) == button_ok) {
cursorUp(1);
project->delObj(pi);
update();
if (p) cursorDown(1);
dirtyview();
rearrangeColumns();
}
clearmsg(msg);
return;
}
case msg_keypressed: {
switch (msg->data1.integer) {
case K_Return: {
if (calcCount() && selectEntry(e_cursor)) {
clearmsg(msg);
return;
}
}
}
break;
}
}
ht_listbox::handlemsg(msg);
}
int ht_project_listbox::numColumns()
{
return 2;
}
void *ht_project_listbox::quickfind(const char *s)
{
void *item = getFirst();
int slen = strlen(s);
while (item && (ht_strncmp(getStr(0, item), s, slen)!=0)) {
item = getNext(item);
}
return item;
}
char *ht_project_listbox::quickfindCompletition(const char *s)
{
void *item = getFirst();
char *res = NULL;
int slen = strlen(s);
while (item) {
if (ht_strncmp(getStr(0, item), s, slen)==0) {
if (!res) {
res = ht_strdup(getStr(0, item));
} else {
int a = ht_strccomm(res, getStr(0, item));
res[a] = 0;
}
}
item = getNext(item);
}
return res;
}
bool ht_project_listbox::selectEntry(void *entry)
{
int p = pos;
ht_project_item *i = (ht_project_item *)(*project)[p];
char fn[HT_NAME_MAX];
if (sys_common_canonicalize(fn, sizeof fn, i->get_filename(), i->get_path(), sys_is_path_delim)==0) {
((ht_app*)app)->create_window_file(fn, FOM_AUTO, false);
}
return true;
}
void ht_project_listbox::set_project(ht_project *p)
{
project = p;
update();
}
/*
* CLASS ht_project_window
*/
void ht_project_window::init(Bounds *b, const char *desc, uint framestyle, uint number, ht_project **p)
{
ht_window::init(b, desc, framestyle, number);
project = p;
Bounds c = *b;
c.x = 0;
c.y = 0;
c.w -= 2;
c.h -= 2;
plb = new ht_project_listbox();
plb->init(&c, *p);
insert(plb);
sendmsg(msg_project_changed);
}
void ht_project_window::done()
{
ht_window::done();
}
void ht_project_window::handlemsg(htmsg *msg)
{
switch (msg->msg) {
case msg_project_changed: {
const char *t = *project ? (*project)->get_filename() : NULL;
if (t) {
ht_snprintf(wtitle, sizeof wtitle, "project '%s'", t);
} else {
strcpy(wtitle, "project window");
}
settitle(wtitle);
plb->set_project(*project);
break;
}
case msg_contextmenuquery: {
ht_static_context_menu *projects = new ht_static_context_menu();
projects->init("~Project");
projects->insert_entry("~Add item", "Insert", cmd_project_add_item, K_Insert, 1);
projects->insert_entry("~Remove item", "Delete", cmd_project_remove_item, K_Delete, 1);
// projects->insert_entry("~Edit item", NULL, cmd_project_edit_item, 0, 1);
msg->msg = msg_retval;
msg->data1.ptr = projects;
return;
}
}
ht_window::handlemsg(msg);
}
/*
* CLASS ht_status
*/
void ht_status::init(Bounds *b)
{
ht_view::init(b, VO_TRANSPARENT_CHARS | VO_RESIZE, 0);
VIEW_DEBUG_NAME("ht_status");
growmode = MK_GM(GMH_FIT, GMV_TOP);
idle_count = 0;
analy_ani = 0;
clear_len = 0;
format = get_config_string("misc/statusline");
if (!format) {
format = strdup(STATUS_DEFAULT_FORMAT);
}
render();
register_idle_object(this);
}
void ht_status::done()
{
unregister_idle_object(this);
free(format);
ht_view::done();
}
const char *ht_status::defaultpalette()