-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathiMage.cpp
3267 lines (2761 loc) · 84.7 KB
/
iMage.cpp
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
//Author: Srinivas Nayak
//Email: sinu.nayak2001@gmail.com
//Copyright (c) 2022 Srinivas Nayak - All Rights Reserved.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
//AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
//BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
//ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
//OR OTHER DEALINGS IN THE SOFTWARE.
//To compile and link FLTK programs
//get fltk-1.3.5-source.tar.gz
//extract and do
//# cd spot/fltk-1.3.5
//# make
//fltk got compiled.
//now do
//# cd spot/fltk-1.3.5
//# ./fltk-config --cxxflags
//and
//# ./fltk-config --ldflags
//these two gives needed compiler and linker flags for our program.
//first one gives
//-I. -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/freetype2 -I/usr/include/libpng16 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_THREAD_SAFE -D_REENTRANT
//second one gives
//-L./lib -lfltk -lXrender -lXcursor -lXfixes -lXext -lXft -lfontconfig -lXinerama -lpthread -ldl -lm -lX11
//now do
//# cd ..
//now add the above outputs to your compile command, to get
//g++ iMage.cpp -Wall -g
//-I./fltk-1.3.5 -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/freetype2 -I/usr/include/libpng16 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_THREAD_SAFE -D_REENTRANT
//-L./fltk-1.3.5/lib -lfltk -lXrender -lXcursor -lXfixes -lXext -lXft -lfontconfig -lXinerama -lpthread -ldl -lm -lX11
//now this command will compile your code.
//in a single line, your command will be...
//g++ iMage.cpp -Wall -g -I./fltk-1.3.5 -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/freetype2 -I/usr/include/libpng16 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_THREAD_SAFE -D_REENTRANT -L./fltk-1.3.5/lib -lfltk -lXrender -lXcursor -lXfixes -lXext -lXft -lfontconfig -lXinerama -lpthread -ldl -lm -lX11
//in Windows, for fltk 1.3.7, to get a statically linked exe, command is
//g++ iMage.cpp -Wall -g -o iMage.exe -I./fltk-1.3.7 -mwindows -DWIN32 -DUSE_OPENGL32 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -mwindows ./fltk-1.3.7/lib/libfltk.a -lole32 -luuid -lcomctl32 -static-libgcc -static-libstdc++ -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive
//note that: for static linking we ran --ldstaticflags option, not --ldflags
//after adding all command fragments, we even added -static-libgcc -static-libstdc++ -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive
//this is to get away with the libstdc++-6.dll, libgcc_s_seh-1.dll, libwinpthread-1.dll and link them statically.
//this will give us statically linked a.exe
//however, in windows, the created iMage.exe has some properties,
//like File description, Product name, Copyright etc.
//which is shown to us while hovering mouse on the exe file.
//these properties are added to a program via a resource script file (*.rc)
//first *.rc file is compiled by "windres" tool to get *.o file
//and then the *.o file is linked to our program.
//here, while statically linking to libwinpthread, we get its default resource file.
//so when we mouse hover on iMage.exe, we see File description as "POSIX WinThreads for Windows".
//to remove it, we have to copy C:\msys64\mingw64\x86_64-w64-mingw32\lib\libwinpthread.a
//to our iMage.cpp file directory. now we have to remove resource file object from it.
//using command, ar -d libwinpthread.a version.o
//finally we have to use this libwinpthread.a in our command like this
//g++ iMage.cpp -Wall -g -o iMage.exe -I./fltk-1.3.7 -mwindows -DWIN32 -DUSE_OPENGL32 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -mwindows ./fltk-1.3.7/lib/libfltk.a -lole32 -luuid -lcomctl32 -static-libgcc -static-libstdc++ -Wl,-Bstatic,--whole-archive ./libwinpthread.a -Wl,--no-whole-archive
//******************
//Data Structures:
//1. element list has element nodes
//2. each element node has color and a list of point nodes
//3. each point node has a pixel position
//1. text list has text nodes
//2. each text node has one text and associated text box widget
//******************
//General Algorithm:
//on click on iMage app, open clean slate
//on click on iMage*.1mg file, open iMage*.1mg
//on close, save iMage_closed_timesecs.1mg
//for save mode
//on save, save iMage_saved_timesecs.1mg
//for move delete mode
//on left push bring to top
//on left drag reposition and redraw
//on right push delete it
//for other modes
//push drag is one drawing task
//on left push create top element
//on left drag append top one and redraw
//for all modes, use release for cleanup
//******************
//Flickering avoidance usually involves one of these methods:
//- Overlay plane drawing
//- XOR drawing
//- redrawing entire image with graphic overlays
//- drawing into a separate widget
//******************
//Flickering avoidance Algorithm:
//mov mode:
//push:
//find element
//remove element
//add element to top
//erase top from (buffer)
//remove element
//draw rest all to (buffer)
//add element to top
//drag:
//erase top from (screen)
//modify top element
//draw top on (screen)
//del mode:
//push:
//(same as move mode push)
//find element
//remove element
//add element to top
//erase top from (buffer)
//remove element
//draw rest all to (buffer)
//add element to top
//drag:
//erase top from (screen)
//pen mode:
//push:
//add element to top
//draw top on (screen)
//drag:
//modify top element
//draw top on (screen)
//rect, line, ellip, arrow mode:
//push:
//add element to top
//remove element
//draw rest all to (buffer)
//add element to top
//draw top on (screen)
//drag:
//(same as mov mode drag)
//erase top from (screen)
//modify top element
//draw top on (screen)
//******************
//History:
//- initial implementation
#include<stdio.h> //printf, sprintf
#include<stdlib.h> //malloc, free
#include<string.h> //memset, strcpy
#include<limits.h> //INT_MAX
#include<math.h> //pow
#include<time.h> //time
//#include<sys/time.h> //gettimeofday
#include <FL/Fl.H> //must for all fltk programs
#include <FL/Fl_Window.H> //every widget class needs its own header file
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>
#include <FL/fl_draw.H>
#include <FL/Fl_Input.H>
#include <FL/fl_ask.H>
#include <FL/fl_utf8.h>
#include<float.h> //FLT_MIN
//******************
class TxtBox: public Fl_Box
{
public:
TxtBox(int x, int y, int w, int h);
protected:
//must be defined for this widget's draw to happen
void draw();
int handle(int e);
private:
};
class ImgBox: public Fl_Box
{
public:
ImgBox(int x, int y, int w, int h);
protected:
//must be defined for this widget's draw to happen
void draw();
int handle(int e);
private:
};
//******************
#define MAX_X 1339
#define MAX_Y 649
#define MAX_TXT_LEN 127
typedef struct point point;
struct point
{
int x;
int y;
};
typedef struct node node;
typedef struct list list;
struct node
{
point pixel;
Fl_Color color;
list* point_list;
char* txt;
TxtBox* tbox;
node* next;
node* prev;
};
struct list
{
node* head;
node* tail;
};
//******************
node* nalloc();
void ninitpix(node* n, int x, int y);
void ninitplist(node* n, list* pointlist, Fl_Color color);
void ninittbox(node* n, TxtBox* tbox);
void nfree(node* n);
list* lalloc();
void lfree(list* l);
int lempty(list* l);
node* linsert(list* l, node* n);
node* lremfifo(list* l);
node* lremlifo(list* l);
node* lremmid(list* l, node* n);
//******************
node* nalloc()
{
node* n = (node*)malloc(sizeof(node));
if( ! n)
return 0;
n->pixel.x = -1;
n->pixel.y = -1;
n->color = FL_BLACK;
n->point_list = 0;
n->txt = 0;
n->tbox = 0;
n->next = 0;
n->prev = 0;
return n;
}
void ninitpix(node* n, int x, int y)
{
if(x<0||x>MAX_X||y<0||y>MAX_Y)
printf("bad pixel node: %d, %d\n", x, y);
n->pixel.x = x;
n->pixel.y = y;
}
void ninitplist(node* n, list* pointlist, Fl_Color color)
{
n->color = color;
n->point_list = pointlist;
}
void ninittbox(node* n, TxtBox* tbox)
{
n->txt = 0;
n->tbox = tbox;
}
void nfree(node* n)
{
if(n)
free(n);
}
list* lalloc()
{
list* l = (list *)malloc(sizeof(list));
if( ! l)
return 0;
node* h = nalloc();
if( ! h)
{free(l); return 0;}
node* t = nalloc();
if( ! t)
{free(l); nfree(h); return 0;}
h->prev = 0;
h->next = t;
t->prev = h;
t->next = 0;
l->head = h;
l->tail = t;
return l;
}
void lfree(list* l)
{
if(l)
{
if(l->head)
nfree(l->head);
if(l->tail)
nfree(l->tail);
free(l);
}
}
int lempty(list* l)
{
node* h = l->head;
node* t = l->tail;
if(h->next == t)
return 1;
else
return 0;
}
node* linsert(list* l, node* n)
{
node* h = l->head;
node* o = h->next;
h->next = n;
n->prev = h;
n->next = o;
o->prev = n;
return n;
}
node* lremfifo(list* l)
{
if(lempty(l))
return 0;
node* t = l->tail;
node* n = t->prev;
node* o = n->prev;
o->next = t;
t->prev = o;
n->next = 0;
n->prev = 0;
return n;
}
node* lremlifo(list* l)
{
if(lempty(l))
return 0;
node* h = l->head;
node* n = h->next;
node* o = n->next;
o->prev = h;
h->next = o;
n->next = 0;
n->prev = 0;
return n;
}
node* lremmid(list* l, node* n)
{
if(lempty(l))
return 0;
n->prev->next = n->next;
n->next->prev = n->prev;
n->next = 0;
n->prev = 0;
return n;
}
//******************
typedef struct
{
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char a;
} pix;
pix red = {255, 0, 0, 0};
pix green = {0, 255, 0, 0};
pix blue = {0, 0, 255, 0};
pix white = {255, 255, 255, 0};
pix slate[MAX_Y+1][MAX_X+1] = {0};
typedef struct
{
Fl_Window *window;
Fl_Box* obox;
Fl_Group *b1g;
Fl_Button* mov_delb;
Fl_Button* penb;
Fl_Button* rectb;
Fl_Button* lineb;
Fl_Button* ellipb;
Fl_Button* arrowb;
Fl_Button* saveb;
Fl_Button* helpb;
Fl_Button* aboutb;
ImgBox* ibox;
//Fl_Group *b2g;
} widgets;
widgets w;
typedef enum
{
MOV_DEL_MODE,
PEN_MODE,
RECT_MODE,
LINE_MODE,
ELLIP_MODE,
ARROW_MODE
} mode;
mode curr_mode = MOV_DEL_MODE;
mode prev_mode = PEN_MODE;
Fl_Color curr_color = FL_BLUE;
list* elem_l = NULL;
list* txt_l = NULL;
int prog_start = 1;
Fl_Input* intxt = 0;
int on_left_drag = 0;
int x_grabbed_to_move = -1;
int y_grabbed_to_move = -1;
int x_of_ellipse = -1;
int y_of_ellipse = -1;
int x_grabbed_to_del = -1;
int y_grabbed_to_del = -1;
int show_event = 0; //for case of window minimize/restore
int txt_box_resized = 0;
node* txt_grabbed_to_move = 0;
int txt_x_grabbed_to_move = -1;
int txt_y_grabbed_to_move = -1;
int prep_buf_for_txt_drag = 0;
node* txt_grabbed_to_del = 0;
FILE* infp = NULL;
FILE* outfp = NULL;
char save_path[1000+1]={0};
//******************
void globalize(int o_x, int o_y, double o_t, int p_x, int p_y, int* res_x, int* res_y);
double angle_of_rotation(int x1, int y1, int x2, int y2);
void mov_delb_callback(Fl_Widget* wd, void* data);
void penb_callback(Fl_Widget* wd, void* data);
void rectb_callback(Fl_Widget* wd, void* data);
void lineb_callback(Fl_Widget* wd, void* data);
void ellipb_callback(Fl_Widget* wd, void* data);
void arrowb_callback(Fl_Widget* wd, void* data);
void saveb_callback(Fl_Widget* wd, void* data);
void helpb_callback(Fl_Widget* wd, void* data);
void aboutb_callback(Fl_Widget* wd, void* data);
void mainwin_callback(Fl_Widget* wd, void* data);
void set_next_color(Fl_Color curr_color);
void reset_all_button_color();
pix* get_pix_rgb_from_col_name(Fl_Color col);
Fl_Color get_col_name_from_pix_rgb(pix* pix_col);
void linemostlyhoriz(list* pt_l, int x1, int y1, int x2, int y2);
void linemostlyvert(list* pt_l, int x1, int y1, int x2, int y2);
void linepoints(list* pt_l, int x1, int y1, int x2, int y2);
void putpix(list* pt_l, int x, int y);
void check_n_put(list* pt_l, int x, int y);
void new_ellipsepoints(list* pt_l, int x1, int y1, int x2, int y2);
void ellipsepoints(list* pt_l, int x1, int y1, int x2, int y2);
void ellipsemostlyhoriz(list* pt_l, int x1, int y1, int x2, int y2);
void ellipsemostlyvert(list* pt_l, int x1, int y1, int x2, int y2);
void ellipsesympoints(list* pt_l, int midx, int midy, int x, int y);
void swapuc(unsigned char* a, unsigned char* b);
void swapi(int* a, int p, int q);
void inssort(int* a, int l);
int cmp(const void* pa, const void* pb);
//void copy_slate_to_snap();
//void copy_snap_to_slate();
void draw_top_element_on_screen(ImgBox* imgbox);
void draw_all_elements_on_buffer();
void erase_top_element_on_screen(ImgBox* imgbox);
void erase_top_element_on_buffer();
void copy_area_from_buf_to_screen(int x1, int y1, int x2, int y2);
void convert_input_to_box();
void create_txt_node_with_first_point(int mouse_x, int mouse_y);
void create_element_node_with_first_point(int mouse_x, int mouse_y);
void destroy_element_node(node* elem_n);
node* find_element_node(int mouse_x, int mouse_y);
node* find_txt_node(int pos_x, int pos_y);
void save_to_file(int app_closed);
void save_elem_list();
void save_tbox_list();
void read_from_file();
void reposition_box(Fl_Widget* box, int new_x, int new_y); //new_x is wrt app window
int main(int argc, char **argv);
//******************
void globalize(int o_x, int o_y, double o_t, int p_x, int p_y, int* res_x, int* res_y)
{
if(p_x==0)
p_x=1;
double tanb = p_y*1.0/p_x; //*1.0 for making division floating
double b = atan(tanb); //rad
double r = sqrt(p_x*p_x + p_y*p_y);
double p_global_x=0;
double p_global_y=0;
double t;
double g;
if(0<=o_t && o_t<90)
{
t = o_t-0;
t = t*3.142/180; //rad
g = t-b;
if(g>=0)
{
p_global_x = o_x + r*cos(g);
p_global_y = o_y - r*sin(g);
}
else //g<0
{
g = 0-g;
p_global_x = o_x + r*cos(g);
p_global_y = o_y + r*sin(g);
}
}
if(90<=o_t && o_t<180)
{
t = o_t-90;
t = t*3.142/180; //rad
g = t-b;
if(g>=0)
{
p_global_x = o_x - r*sin(g);
p_global_y = o_y - r*cos(g);
}
else //g<0
{
g = 0-g;
p_global_x = o_x + r*sin(g);
p_global_y = o_y - r*cos(g);
}
}
if(180<=o_t && o_t<270)
{
t = o_t-180;
t = t*3.142/180; //rad
g = t-b;
if(g>=0)
{
p_global_x = o_x - r*cos(g);
p_global_y = o_y + r*sin(g);
}
else //g<0
{
g = 0-g;
p_global_x = o_x - r*cos(g);
p_global_y = o_y - r*sin(g);
}
}
if(270<=o_t && o_t<360)
{
t = o_t-270;
t = t*3.142/180; //rad
g = t-b;
if(g>=0)
{
p_global_x = o_x + r*sin(g);
p_global_y = o_y + r*cos(g);
}
else //g<0
{
g = 0-g;
p_global_x = o_x - r*sin(g);
p_global_y = o_y + r*cos(g);
}
}
*res_x = p_global_x;
*res_y = p_global_y;
}
double angle_of_rotation(int x1, int y1, int x2, int y2)
{
double t=0;
double rad;
if(x2>x1 && y2<y1)
{
//1st q
rad = atan(abs(y2-y1)*1.0/abs(x2-x1)); //*1.0 for making division floating
t = 0 + rad*180/3.142;
}
if(x2<x1 && y2<y1)
{
//2nd q
rad = atan(abs(x2-x1)*1.0/abs(y2-y1));
t = 90 + rad*180/3.142;
}
if(x2<x1 && y2>y1)
{
//3rd q
rad = atan(abs(y2-y1)*1.0/abs(x2-x1));
t = 180 + rad*180/3.142;
}
if(x2>x1 && y2>y1)
{
//4th q
rad = atan(abs(x2-x1)*1.0/abs(y2-y1));
t = 270 + rad*180/3.142;
}
if(x2>x1 && y2==y1)
{
//x axis
t = 0;
}
if(x2<x1 && y2==y1)
{
//-x axis
t = 180;
}
if(x2==x1 && y2<y1)
{
//y axis
t = 90;
}
if(x2==x1 && y2>y1)
{
//-y axis
t = 270;
}
return t;
}
//******************
//find_element_node
node* find_element_node(int mouse_x, int mouse_y)
{
int found = 0;
//loop all element node
node* elem_n = elem_l->head->next;
while (elem_n->next != 0) //elem_n->next == 0 for dummy tail node
{
//get point list
list* pt_l = elem_n->point_list;
//loop all point node
node* pt_n = pt_l->head->next;
while (pt_n->next != 0) //pt_n->next == 0 for dummy tail node
{
if(pt_n->pixel.x == mouse_x &&
pt_n->pixel.y == mouse_y)
{
found = 1; break;
}
pt_n = pt_n->next;
}
if(found == 1)
break;
elem_n = elem_n->next;
}
if(found == 1)
return elem_n;
else
return NULL;
}
//create_element_node
void create_element_node_with_first_point(int mouse_x, int mouse_y)
{
node* elem_n = nalloc();
linsert(elem_l, elem_n);
//create point list
list* pt_l = lalloc();
ninitplist(elem_n, pt_l, curr_color);
//create first point node
node* pt_n = nalloc();
ninitpix(pt_n, mouse_x, mouse_y);
linsert(pt_l, pt_n);
}
void create_txt_node_with_first_point(int mouse_x, int mouse_y)
{
TxtBox* txtb = new TxtBox(w.ibox->x()+mouse_x, w.ibox->y()+mouse_y, 100, 24); //default font size is 14 pixel, we add 5 pixel margin
txtb->box(FL_ROUNDED_BOX); //FL_NO_BOX/FL_PLASTIC_UP_BOX/FL_ROUNDED_BOX better
//txtb->labelfont(FL_SCREEN); //monospaced font type
//txtb->labelsize(14); //font size in pixel
reposition_box(txtb, w.ibox->x()+mouse_x, w.ibox->y()+mouse_y);
//txtb->labelcolor(FL_BLACK);
//txtb->labelfont(FL_SCREEN); //font type
//txtb->labelsize(14); //font size in pixel
//txtb->label("click!");
node* txt_n = nalloc();
ninittbox(txt_n, txtb);
linsert(txt_l, txt_n);
w.window->add(*txtb); //append this widget at end of main window's child list
//txtb->hide(); //causes something weird, ibox gets redrawn :-(
intxt = new Fl_Input(w.ibox->x()+mouse_x, w.ibox->y()+mouse_y, 100, 24);
Fl::focus(intxt);
w.window->add(*intxt); //append this widget at end of main window's child list
reposition_box(intxt, w.ibox->x()+mouse_x, w.ibox->y()+mouse_y);
intxt->redraw();
}
void convert_input_to_box()
{
int ww; int hh;
//close top text input box
if(intxt != NULL)
{
node* txt_n = txt_l->head->next;
int in_len = strlen(intxt->value());
if(in_len==0)
{
lremmid(txt_l, txt_n);
w.window->remove(txt_n->tbox);
Fl::delete_widget(txt_n->tbox); //safe widget delete
nfree(txt_n);
}
else
{
ww=0; hh=0; //needed for fl_measure or measure_label
//needed for fl_measure or measure_label etc
//so better set this in the beginning
fl_font(FL_HELVETICA, 14);
txt_n->txt = (char*)malloc(MAX_TXT_LEN+1);
strncpy(txt_n->txt, intxt->value(), MAX_TXT_LEN);
txt_n->txt[MAX_TXT_LEN] = '\0';
txt_n->tbox->label(txt_n->txt);
//txt_n->tbox->labelfont(FL_SCREEN); //monospaced font type
//txt_n->tbox->labelsize(14); //font size in pixel
txt_n->tbox->measure_label(ww, hh);
ww = ww + 10; //10 for left right margin
if(ww < 24) ww = 24; //min width 24
txt_n->tbox->size(ww, 24); //height always 24
//txt_n->tbox->show(); //no hide no show
reposition_box(txt_n->tbox, txt_n->tbox->x(), txt_n->tbox->y());
}
::w.window->remove(*intxt);
//when we type into input text box, mouse cursor disappears
//if while typing in input text box, we press escape button,
//mouse cursor will anyway disappear for typing,
//and input text box will disappear for escape,
//then we have no cursor at all in the whole window.
//so below code will help in restoring cursor for the window
::w.window->cursor(FL_CURSOR_DEFAULT); //for cursor disappearing issue
Fl::delete_widget(intxt); //safe widget delete
intxt = 0;
txt_box_resized = 1;
}
}
void destroy_element_node(node* elem_n)
{
//delete all points
list* pt_l = elem_n->point_list;
while (pt_l->head->next != pt_l->tail) //pt_n->next == 0 for dummy tail node
{
node *n = lremlifo(pt_l);
nfree(n);
}
//delete point list
lfree(pt_l);
//delete element node
nfree(elem_n);
}
//******************
ImgBox::ImgBox(int x, int y, int w, int h):Fl_Box(x, y, w, h)
{
}
int ImgBox::handle(int e)
{
if(e == FL_SHOW)
{
//printf("show event\n");
show_event=1; //we draw full after window restore
return 1;
}
if(curr_mode == MOV_DEL_MODE)
{
if(e == FL_PUSH && Fl::event_button() == FL_LEFT_MOUSE)
{
//fprintf(stderr, "left push event, x,y: %d,%d\n", Fl::event_x(), Fl::event_y());
int mouse_x = Fl::event_x() - Fl_Box::x();
int mouse_y = Fl::event_y() - Fl_Box::y();
//fprintf(stderr, "mouse, x,y: %d,%d\n", mouse_x, mouse_y);
if(mouse_x<0)
mouse_x = 0;
if(mouse_y<0)
mouse_y = 0;
if(mouse_x>MAX_X)
mouse_x = MAX_X;
if(mouse_y>MAX_Y)
mouse_y = MAX_Y;
node* found_element = find_element_node(mouse_x, mouse_y);
if(found_element != NULL)
{
//remove the elem_n
lremmid(elem_l, found_element);
//now add it to top
linsert(elem_l, found_element);
//erase top from buffer
erase_top_element_on_buffer();
//remove the elem_n
lremmid(elem_l, found_element);
//draw rest all elem_n on slate
draw_all_elements_on_buffer();
//now add it to top
linsert(elem_l, found_element);
x_grabbed_to_move = mouse_x;
y_grabbed_to_move = mouse_y;
}
else
{
x_grabbed_to_move = -1;
y_grabbed_to_move = -1;
}
//fl_cursor(FL_CURSOR_MOVE);
redraw();
return 1;
}
if(e == FL_DRAG && Fl::event_button() == FL_LEFT_MOUSE)
{
if (x_grabbed_to_move != -1
&& y_grabbed_to_move != -1)
{
//fprintf(stderr, "left drag event, x,y: %d,%d\n", Fl::event_x(), Fl::event_y());
int mouse_x = Fl::event_x() - Fl_Box::x();
int mouse_y = Fl::event_y() - Fl_Box::y();
//fprintf(stderr, "mouse, x,y: %d,%d\n", mouse_x, mouse_y);
if(mouse_x<0)
mouse_x = 0;
if(mouse_y<0)
mouse_y = 0;
if(mouse_x>MAX_X)
mouse_x = MAX_X;
if(mouse_y>MAX_Y)
mouse_y = MAX_Y;
int dx = mouse_x - x_grabbed_to_move;
int dy = mouse_y - y_grabbed_to_move;
//for next drag event
x_grabbed_to_move = mouse_x;
y_grabbed_to_move = mouse_y;
//copy top element pixels from slate to screen
erase_top_element_on_screen(this);
//change top element point
//values because of drag
//get top element
node* elem_n = elem_l->head->next;
if (elem_n->next != 0) //elem_n->next == 0 for dummy tail node
{
list* pt_l = elem_n->point_list;
//loop all pt_n
node* pt_n = pt_l->head->next;
while (pt_n->next != 0) //pt_n->next == 0 for dummy tail node
{
pt_n->pixel.x += dx; //TODO
pt_n->pixel.y += dy;
pt_n = pt_n->next;
}
}
//draw top element on screen
draw_top_element_on_screen(this);
}
redraw();
return 1;
}
if(e == FL_RELEASE && Fl::event_button() == FL_LEFT_MOUSE)
{
if (x_grabbed_to_move != -1
&& y_grabbed_to_move != -1)
{
//little clean up
x_grabbed_to_move = -1;
y_grabbed_to_move = -1;
}
return 1;
}
//*****************
if(e == FL_PUSH && Fl::event_button() == FL_RIGHT_MOUSE)
{
//fprintf(stderr, "right push event, x,y: %d,%d\n", Fl::event_x(), Fl::event_y());
int mouse_x = Fl::event_x() - Fl_Box::x();
int mouse_y = Fl::event_y() - Fl_Box::y();
if(mouse_x<0)
mouse_x = 0;
if(mouse_y<0)
mouse_y = 0;
if(mouse_x>MAX_X)
mouse_x = MAX_X;
if(mouse_y>MAX_Y)
mouse_y = MAX_Y;
node* found_element = find_element_node(mouse_x, mouse_y);
if(found_element != NULL)
{
//remove element node
lremmid(elem_l, found_element);
//now add it to top
linsert(elem_l, found_element);
//erase top from buffer
erase_top_element_on_buffer();
//remove the elem_n
lremmid(elem_l, found_element);