-
Notifications
You must be signed in to change notification settings - Fork 3
/
hgui.c
2803 lines (2645 loc) · 106 KB
/
hgui.c
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
// Hermit simple GUI toolkit (with C and Bash support), mainly for Herminux
//=========================================================================
//------------------------ included libraries --------------------------
#include <stdlib.h> //C standard library
#include <stdio.h> //printf,sprintf,fprintf,etc.
#include <stdarg.h>
#include <string.h> //operations like strlen()
//#include <math.h>
//#include <time.h>
#include <errno.h>
#include <X11/Xlib.h> //libX11
#include <X11/Xos.h> //minimize system-dependencies
#include <X11/Xutil.h> //XWMHints,XChangeProperty,etc.
#include <X11/keysym.h> //key symbols
#include <X11/Xatom.h> //X11 predefined atoms & properties (e.g. size-hints)
#include <X11/Xlocale.h> //Internationalization and Unicode support (better doing it with freetype2 library)
//#ifdef USE_XFT
// #include <X11/Xft/Xft.h>
// XftDraw *xftdraw; XftColor xftcolor; XRenderColor xrcolor; XftFont *xftfont;
//#endif
#include "hgui.h"
#if defined(__cplusplus)
extern "C"
{
#endif
//window definition aliases/dependencies
#define NOICONIFY NOTASKBAR
#define NOMAXIMIZE NORESIZE //nomaximize is an alias to noresize, means: no maximize-button, which is straightforward in case of non-resizable window
//handle internal widget dependencies
#ifdef HGUI_BUTTONS
#define HGUI_TEXTLINES
#endif
#ifdef HGUI_PICBUTTONS
#define HGUI_BUTTONS
#define HGUI_PICTURES
#endif
#ifdef HGUI_TEXTBLOCKS
#define HGUI_TEXTLINES
#endif
#ifdef HGUI_TEXTBOXES
#define HGUI_TEXTLINES
#define HGUI_TEXTBLOCKS
#define HGUI_BLOCKS
#define HGUI_SCROLLBARS
#endif
#ifdef HGUI_SCROLLBARS
#define HGUI_BLOCKS
#endif
#ifdef HGUI_PICANIMS
#define HGUI_PICTURES
#endif
#ifdef HGUI_TEXTANIMS
#define HGUI_TEXTLINES
#endif
#if defined(HGUI_SLIDERS) || defined(HGUI_PROGBARS)
#define HGUI_TEXTLINES //for caption
#endif
//#if defined(HGUI_FILEBROWSER) || defined(HGUI_COLORCHOOSER)
// #define HGUI_CHILDWINDOWS
// #define HGUI_CHILDWIN_COMPOSITES
//#endif
//handle internal widget-groups
#if defined (HGUI_TEXTBUTTONS) || defined(HGUI_PICBUTTONS)
#define HGUI_BUTTONS
#endif
#if defined(HGUI_TEXTLINES) || defined(HGUI_TEXTBLOCKS) || defined(HGUI_TEXTBOXES) || defined(HGUI_TEXTANIMS)
#define HGUI_TEXTFIELDS
#endif
#if defined(HGUI_TEXTANIMS) || defined(HGUI_PICANIMS)
#define HGUI_ANIMATIONS
#endif
//----------------internal program constants and settings---------------
//some EWMH (Extended Window Manager Hints) values
#define _NET_WM_STATE_REMOVE 0
#define _NET_WM_STATE_ADD 1
#define _NET_WM_STATE_TOGGLE 2
#define SOURCE_INDICATION_NO_OLD 0
#define SOURCE_INDICATION_NORMAL 1
#define SOURCE_INDICATION_PAGERS 2
//#define DEBUG True //debug mode
#define EXIT_OK 0
#define EXIT_ERROR 1
#define EXIT_OUTOFMEM 2
#define RET_ERR -1
#define LINESIZE_MAX 1024 //maximum width of a text-line (in conversion)
#define TEXTSIZE_MAX 1000000 //maximum amount (bytes) of text to display in a textbox/textblock
#define XPM_DEPTH 32 //not the colour-depth, but the representation in memory
#define REFRESHRATE 100 //Hz the maximum frequency to refresh window-contents in case of destructive resizing / moving / covering (Expose-event)
#define REFRESHDELAY 1000000/REFRESHRATE
#define CHILDWIN_AMOUNT_MAX 64 //64
#define WIDGET_AMOUNT_MAX 1024
//#define POLYGON_MAX_POINTS 256 (using 'char' type prevents problems aready)
#define ANIM_AMOUNT_MAX WIDGET_AMOUNT_MAX
#define SCROLLTEXTWIDTH_MAX 1024 //max.length of a scrolltext-bar in characters
#define PROPSIZE_MAX 64 //Window-Manager property text max. size
//#define XBUILTINFONT "-*-fixed-medium-r-*-*-13-*-*-*-c-*-*-*" //"fixed" //"6x13" //probably built into every Xorg-servers, if no font described, this comes into play
#define XBUILTINFONT_WIDTH 6 //if the OS has a different font as default, these settings should be changed or loading XBUILTINFONT will be necessary at window-creation
#define XBUILTINFONT_HEIGHT 13
#define XBUILTINFONT_ASCENT 10
#define XBUILTINFONT_DESCENT XBUILTINFONT_HEIGHT-XBUILTINFONT_ASCENT
#define TABSIZE 4
#define BORDERSIZE 2
#define TRANSPCOLOR 0x808080 //fallback when no background-colour is given for transparency-supported XPM
#define BUTTFADE 0.1
//default theme (used for simple/automatic routines not having all inputs):
#define CONTRAST 0.4 //0.0 to 1.0
#define BGCOLOR 0xCEC8CA
#define FGCOLOR 0x001000
//#define DEFAULTFONT XBUILTINFONT //e.g. "-*-arial-medium-r-*-*-17-*-*-*-p-*-*-*"
#define HOLECOLOR 0x001100
#define BUTTBGCOL 0x10D428
#define BUTTFGCOL 0x202020
#define BUTTBEVEL 2
#define BUTTBORDER 1
#define BUTTBORDCOL HOLECOLOR
#define TEXTBGCOL 0xE0F0E0
#define TEXTFGCOL 0x203010
#define PROGBGCOL 0x80E0C0
#define PROGFGCOL 0x1020E0
#define PROGBEVEL BUTTBEVEL
#define PROGTEXTCOL 0xFFFFFF
#define SLIDERHOLE 2
#define SLIDSCALEW 20 //slide-scalemark-width should be automatic depending on horizontal knob-size
//#define SLIDEMARG 10 //should be automatic depending on vertical knob-size (which is derived from horizontal knobsize)
#define SLIDKNOBSIZE 16
#define SLIDBGCOL 0xC0E0D0
#define SLIDFGCOL 0x20C080
#define SLIDKNOBCOL 0xC020E0
#define SCROLLBGCOL 0xC0A080
#define SCROLLFGCOL 0x624050
#define SCROLLKNOBCOL 0xD8D0D4
#define SCROLLBUTTSIZE 14
#define ANIMDELAY 4 //in frames
#define SCROLLSPEEDY 1 //4 character-rows to jump on scrolloffsetheel in textbox
#define SCROLLSPEEDX 8 //pixel-rows to jump with horizontal scrolling
enum WidgetTypes { BUTTON,BUTTONDYN,BUTTONXPM,VSLIDER,HSLIDER,TEXTLINE,TEXTCENTER,TEXTBLOCK,TEXTBOX,HPROGBAR,VPROGBAR,VSCROLL,HSCROLL,BLOCK,BLOCKFILL,PICTURE,XPM,ANIM,ANIMXPM,TEXTSCROLL,
POINT,LINE,RECT,RECTFILL,ARC,ARCFILL,CIRCLE,CIRCLEFILL,ELLIPSE,ELLIPSEFILL,POLYGON,FILLSOLID
};
//---------------- macros (for readability and going towards cross-platform/framebuf(fbterm,kmscon)/SDL/alternative-X(nanoX,DirectFB) support) --------------
#ifdef __unix__ //X11 Xorg (note: space is not allowed between macro-function-name and left curly brace)
#define SETBGCOL(col) ( XSetBackground(hw->d,hw->gc,col) )
#define SETDRAWCOL(col) ( XSetForeground(hw->d,hw->gc,col) )
#define DRAWPOINT(x,y,col) ( SETDRAWCOL(col), XDrawPoint(hw->d,hw->w,hw->gc,x,y) )
#define DRAWLINE(x1,y1,x2,y2) ( XDrawLine(hw->d,hw->w,hw->gc,x1,y1,x2,y2) )
#define DRAWRECT(x1,y1,x2,y2) ( XDrawRectangle(hw->d,hw->w,hw->gc,x1,y1,x2,y2) )
#define DRAWRECTFILL(x1,y1,x2,y2) ( XFillRectangle(hw->d,hw->w,hw->gc,x1,y1,x2,y2) )
#define DRAWARC(x,y,wi,h,startdeg,extentdeg) ( XDrawArc(hw->d,hw->w,hw->gc,x,y,wi,h,startdeg*64,extentdeg*64) )
#define DRAWARCFILL(x,y,wi,h,startdeg,extentdeg) ( XFillArc(hw->d,hw->w,hw->gc,x,y,wi,h,startdeg*64,extentdeg*64) )
#define DRAWSTRINGN(x,y,text,col,font,len) ( XDrawString(hw->d,hw->w,setFontGC(hw,hw->bgcol,col,font), x, y, text, len) )
#define DRAWSTRING(x,y,text,col,font) ( XDrawString(hw->d,hw->w,setFontGC(hw,hw->bgcol,col,font), x, y, text, strlen(text)) )
#endif
//#ifdef __WIN32__
// #define SETDRAWCOL(col) ( ?! )
// #define DRAWPOINT(x,y,color) \ ( ? )
// #define DRAWLINE(x1,y1,x2,y2) \ ( ? )
// ....
//#endif
//------------------------- type and struct definitions ----------------
typedef struct Widget { //this is the common struct for widgets,
//1st public part: structure should match exactly to the 1st part of specific structs in hgui.h (like Button,VScroll,etc.))
union {
int x,x1;
} ;
union {
int y,y1;
} ;
union {
int x2w,x2,w,r,rx;
} ;
union {
int y2h,y2,h,ry;
} ;
union {
int zh,hpos,border,knobsize,depth,startdeg,scrollpixcnt;
} ;
union {
long zv,vpos,bevel,scalewidth,degree,scrollindex;
} ;
union {
unsigned long bgcol,color,transpcol;
} ;
union {
unsigned long fgcol,textcol;
} ;
union {
unsigned long knobcol,progtextcol,vpixsize,scrolldisplen;
} ;
union {
unsigned long captcol,vpixpos,scrolloffset;
} ;
union {
void *data,*text,*content,*title,*caption,*pixdata,*xpm,*xpmlist;
} ;
union {
float pos;
} ;
union {
float ratio,scale;
} ;
union {
char opt1,wrap,framedelay;
} ;
union {
char opt2,scrollbars,spdcnt;
} ;
union {
char opt3,pixstep,autofit;
} ; //char modeswitches; an alternative parametering method for one-bit mode-switches like wrap/scrollbars/autofit/edit/readonly/etc.
union {
char state,buttonstate,framecount,scrollendstate;
} ;
int actx1,acty1,actx2,acty2; //read-only (writing could fuck up the GUI)
//2nd private part: this must be hidden from the outside wordld, the following members are for internal use only. if needed, write function to get/set them
union {
void *fsp, *font, *fontstruct;
} ; //XFontStruct pointer
void (*callback)(); //callback-routine pointer
char grabbed;
int margin;
union {
int knobx1,knoby1;
} ;
union {
int hpixsize,knobx2,knoby2;
}; //active-area is calculated by widget-drawing routines in drawWidget()
char type;
int id;
void* parenthw;
void* argptr; //'id' is set by addWidget()
union {
void *child1,*vscroll;
} ;
union {
void *parentbox,*child2,*hscroll;
} ; //link parent (e.g. textbox) with children (e.g.scrollbars)
} Widget;
//#undef hWindow
typedef struct hWindow {
//1st more public part: structure should match the one in hgui.h if exists
unsigned long bgcol;
float contrast;
int eventx,eventy;
char eventbutton;
int width, height;
//2nd private part for internal use
int orig_width,orig_height;
//int norm_x,norm_y, norm_width,norm_height; //fullscreen status and position+dimensions before going to fullscreen state (restoring window-size/position should be handled by window-manager)
char options;
char *title;
void (*exitCallback)();
void *parenthw; //hWindow*
void* Child[CHILDWIN_AMOUNT_MAX];
int ChildAmount;
Widget W[WIDGET_AMOUNT_MAX];
int WidgetAmount; //'W':widget-array, WidgetAmount counts added items, used for Widget-ID
int AnimID[ANIM_AMOUNT_MAX];
int AnimAmount; //so no need to look up all widget-array to check if animations are added
Display *d;
int fd; /*Drawable*/ Window w;
int s;
GC gc, defgc;
Atom wm_protocols,delete_window;
char dragging;
int exposecnt, resizecnt, exitsig;
int id;
} hWindow;
typedef struct TextDim {
int x,y;
long w;
short h, ascent,descent;
} TextDim;
typedef struct TextBoxDim {
long w, h, rowcount, vpixpos;
} TextBoxDim;
//----------------- global (preferably constant) content ---------------
const char ErrPfx[]="ERROR(hgui): ", WarnPfx[]="WARNING(hgui): ";
const char ERRORTXT_OUTOFMEM[]="Out of memory!", EmptyString[]="";
Widget eWidget;
void* EmptyWidget=&eWidget;
XFontStruct eFontStruct;
XFontStruct* EmptyFontStruct=&eFontStruct;
XFontStruct *xfont=NULL; //built-in Xorg font
short DownArrowPoints[]= {-4,5, 5,5, 0,10}, UpArrowPoints[]= {-5,9, 5,9, 0,3};
short RightArrowPoints[]= {5,-5, 5,5, 10,0}, LeftArrowPoints[]= {9,-5, 9,5, 4,0};
//--------------------- internal function prototypes -------------------
//(public functions are prototyped in the header-file, local functions are in dependency-order)
int dynCoordX(hWindow *hw, int x);
int dynCoordY(hWindow *hw, int y);
TextDim drawTextScrollFrame(hWindow *hw, Widget *wp, int x1, int y1, int x2, int y2, char *text);
void cleanUpRect(hWindow *hw, int x1o, int y1o, int x2o, int y2o, int x1, int y1, int x2, int y2);
void refreshWidget(Widget *w);
Bool putAnimXPMframe(hWindow *hw, int id, int x, int y, unsigned long transpcol, void* xpmlist[], int framenumber );
//------------------------- basic GUI routines -------------------------
XPMstruct XPMtoPixels(char* source[], unsigned long bgcol) //only suitable for XPMs with around max. 50 colours (single-character colour-lookups)
{
int i,j,k,sourceindex,pixdataindex,width,height,colours,charpix,transpchar=0xff;
unsigned int colr[256],colg[256],colb[256];
XPMstruct xpms;
unsigned char colchar[256], colcode,colindex, transpr=(bgcol&0xFF0000)/65536,transpg=(bgcol&0xFF00)/256,transpb=bgcol&0xFF;
sscanf(source[0],"%d %d %d %d",&width,&height,&colours,&charpix); //printf("width:%d, height:%d, colours:%d, charpix:%d",width,height,colours,charpix);
unsigned char *pixdata = malloc (width*height*XPM_DEPTH/8); //32 is sure to have enough room
for (i=0; i<colours; i++) {
if (sscanf(source[1+i],"%c%*s #%2X%2X%2X",&colchar[i],&colr[i],&colg[i],&colb[i])!=4) transpchar=colchar[i];
}
for (i=0; i<height; i++) {
for (j=0; j<width; j++) {
sourceindex=(i*width+j);
colcode=source[colours+1+i][j];
for(k=0; k<colours; k++) if (colcode==colchar[k]) break;
colindex=k;
pixdataindex=(i*width+j)*XPM_DEPTH/8;
pixdata[pixdataindex+0] = (colcode!=transpchar)?colb[colindex]:transpb; //Blue 0x502040
pixdata[pixdataindex+1] = (colcode!=transpchar)?colg[colindex]:transpg; //Green
pixdata[pixdataindex+2] = (colcode!=transpchar)?colr[colindex]:transpr; //Red
pixdata[pixdataindex+3] = (colcode==transpchar)?0x00:0xff ;//Aplha - 0:fully transparent...0xFF:fully opaque /Xlib has no alpha-channel support, Xrender has transparency-support
}
}
xpms.pixdata=pixdata;
xpms.width=width;
xpms.height=height;
return xpms;
}
Display* openDisplay()
{
Display *d=XOpenDisplay(NULL);
if(d==NULL) {
fprintf(stderr,"%s Can't open display.\n",ErrPfx);
exit(EXIT_ERROR);
}
return d;
}
//tip: 'xprop' is an useful utility to check the window's properties
Atom changeWMprop(hWindow *hw, char* property, char* data, Bool overwrite) //window-manager hint properties _NET_WM_WINDOW_TYPE / _NET_WM_STATE / MOTIF_WM_HINTS / etc. which are not settable directly by Xlib convenience functions
{
Atom wmatom = XInternAtom(hw->d,data,False);
XChangeProperty(hw->d,hw->w, XInternAtom(hw->d,property,False), XA_ATOM, 32,overwrite?PropModeReplace:PropModeAppend, (unsigned char*) &wmatom, 1);
return wmatom;
}
void setWindowType(hWindow *hw, char *type)
{
//called when creating window, preferably not called afterwards
char prop[PROPSIZE_MAX];
sprintf(prop,"_NET_WM_WINDOW_TYPE_%s",type); //printf("Setting window as type %s\n",type);
changeWMprop(hw,"_NET_WM_WINDOW_TYPE",prop,True);
}
Atom addWindowState(hWindow *hw, char *type)
{
char prop[PROPSIZE_MAX];
sprintf(prop,"_NET_WM_STATE_%s",type); //printf("Setting window to state %s\n",type);
return changeWMprop(hw,"_NET_WM_STATE",prop,False);
}
void sendStateClientMessage(hWindow *hw, char *type, char action)
{
//informs the window-manager about the state-change
char prop[PROPSIZE_MAX];
sprintf(prop,"_NET_WM_STATE_%s",type);
Atom msgtypeatom=XInternAtom(hw->d,"_NET_WM_STATE",False), stateatom=XInternAtom(hw->d,prop,False);
XEvent stev;
stev.xclient.type=ClientMessage;
stev.xclient.serial=0;
stev.xclient.send_event=True;
stev.xclient.message_type=msgtypeatom;
stev.xclient.window=hw->w;
stev.xclient.format=32;
stev.xclient.data.l[0]=action;
stev.xclient.data.l[1]=stateatom;
stev.xclient.data.l[2]=0;
stev.xclient.data.l[3]=SOURCE_INDICATION_NORMAL;
stev.xclient.data.l[4]=0;
XSendEvent(hw->d, RootWindow(hw->d,hw->s), True, StructureNotifyMask | SubstructureNotifyMask | SubstructureRedirectMask , &stev);
XSync(hw->d,True);
}
void setWindowStates(hWindow *hw) //all-at-once, starting from empty to avoid accumulation/duplication
{
//based on current value of hw->options //XWMHints *hints = XAllocWMHints(); hints->flags=StateHint; hints->initial_state=IconicState; XSetWMHints(hw->d,hw->w,hints);
Atom msgtypeatom=XInternAtom(hw->d,"_NET_WM_STATE",False);
XDeleteProperty(hw->d,hw->w,msgtypeatom);
if (hw->options&NOTASKBAR) addWindowState(hw,"SKIP_TASKBAR");
if (hw->options&ALWAYSONTOP) addWindowState(hw,"ABOVE");
if (hw->options&MODALWIN) addWindowState(hw,"MODAL");
if (hw->options&FULLSCREEN) addWindowState(hw,"FULLSCREEN");
//sendStateClientMessage should be called after these changes to inform window-manager with XSendEvent()
}
void setWinTypeToOptions(hWindow *hw, short options)
{
//at the moment I don't know about distinct properties (other than nonstandard _MOTIF_WM_HINTS) for titlebar-button-selection / window-decoration-disabling, until that this function is handy
if (options==NOMAXIMIZE) setWindowType(hw,"UTILITY");
else if ((options&NODECOR && options&ALWAYSONTOP) || (options&TRAYAPP)) setWindowType(hw,"DOCK"); //DOCKAPP needs proper initialization afterwards (not to seen in taskbar, but be seen in system-tray)
else if (options&NOTASKBAR) {
if (options&NODECOR) setWindowType(hw,"NOTIFICATION");
else if (options&NOICONIFY) setWindowType(hw,"DIALOG"); //NOICONIFY is only relevant if there's NOTASKBAR, vice-versa, and is more important (not to have minimize-icon) than NOMAXIMIZE, ALWAYSONTOP can be set later
else if (options&NOMAXIMIZE) {
if (options&ALWAYSONTOP) setWindowType(hw,"TOOLBAR");
else setWindowType(hw,"MENU");
}
} else if (options&NODECOR) setWindowType(hw,"SPLASH");
else setWindowType(hw,"NORMAL"); //printf("%d\n",options);
}
hWindow* createAndSetWindow(hWindow *hw, hWindow *parenthw, Window *rootw, int x, int y, int w, int h, char *title, unsigned long bgcol, unsigned long fgcol, int minwidth, int minheight, short options, char* xpmicon[], void* exitCallback, int argc, char **argv )
{
if(hw==NULL) return NULL;
Pixmap icon=None;
hw->exitsig=0;
hw->orig_width=w;
hw->orig_height=h;
hw->bgcol=bgcol; //hw->norm_x=x; hw->norm_y=y; hw->norm_width=w; hw->norm_height=h;
if (options&NORESIZE) {
minwidth=w;
minheight=h;
};
hw->exitCallback=exitCallback;
hw->w=XCreateSimpleWindow(hw->d,*rootw,x,y,w,h,(options&(NODECOR||NOWINMAN))?0:BORDERSIZE,BlackPixel(hw->d,hw->s),bgcol);
hw->options=options;
hw->gc=XCreateGC(hw->d,hw->w,0,NULL);
hw->defgc=XCreateGC(hw->d,hw->w,0,NULL); //DefaultGC(hw->d,hw->s); //3rd parameter for XCreateGC could be (GCFont|GCForeground|GCBackground), 4th is &XGCValues
XSizeHints *sizehints=XAllocSizeHints();
sizehints->flags=PPosition|PMinSize;
sizehints->min_width=minwidth;
sizehints->min_height=minheight;
if (options&NORESIZE) {
sizehints->flags|=PMaxSize;
sizehints->max_width=minwidth;
sizehints->max_height=minheight;
}
if (xpmicon!=NULL) {
XPMstruct xpms=XPMtoPixels(xpmicon,TRANSPCOLOR);
XImage *iconimg = XCreateImage(hw->d,CopyFromParent,DefaultDepth(hw->d,hw->s),ZPixmap,0,xpms.pixdata,xpms.width,xpms.height,XPM_DEPTH,0);
XInitImage(iconimg); //Pixmap icon=XCreatePixmapFromBitmapData(hw->d,hw->w,icon_pixels,ICON_WIDTH,ICON_HEIGHT,0x001122,0xFFEECC,XPM_DEPTH);
icon=XCreatePixmap(hw->d,hw->w,xpms.width,xpms.height,DefaultDepth(hw->d,hw->s));
XPutImage(hw->d,icon,hw->gc,iconimg,0,0,0,0,xpms.width,xpms.height);
XDestroyImage(iconimg);
}
XSetStandardProperties(hw->d,hw->w,title,title,icon,argv,argc,sizehints); //XSetWMProperties(hw->d,hw->w,NULL,NULL,NULL,0,sizehints,NULL,NULL); //XSetWMSizeHints(hw->d,hw->w,sizehints,sizeatom); //setIconXPM(hw->d,hw->w,hw->s,icon_xpm); //setTitle(hw->d,hw->w,title); //XListFonts()
//XSetWindowBackgroundPixmap(hw->d,hw->w,None); //avoid flicker with trick, by setting empty pixmap background and repainting the window in eventloop at expose-events
XSetWindowAttributes xwa;
xwa.bit_gravity=StaticGravity;
xwa.override_redirect=(options&NOWINMAN)?True:False; //override-redirect causes window to be a 'splash-creen' with no titlebar (but black border?) on top of all windows, also _NET_WM_WINDOW_TYPE property can (& should) be used to set splash
XChangeWindowAttributes(hw->d,hw->w,CWBitGravity|CWOverrideRedirect,&xwa); //bit_gravity=StaticGravity to avoid flickering made by repainting background by X automatically when resizing window
if(options) setWinTypeToOptions(hw,options); //this is not needed if I find a way to tell WM which particular buttons to include in titlebar or not to display window-decoration at all
if(options) setWindowStates(hw); //initialize window-state based on 'options'
//#ifdef USE_XFT xftdraw=XftDrawCreate(hw->d,hw->w,DefaultVisual(hw->d,hw->s),DefaultColorMap(hw->d,hw->s));
XMapWindow(hw->d,hw->w);
XClearWindow(hw->d,hw->w);
XFlush(hw->d);
XSelectInput(hw->d,hw->w,ExposureMask|StructureNotifyMask|KeyPressMask|ButtonPressMask|Button1MotionMask|Button3MotionMask); //XCopyGC(hw->d,XDefaultGC(hw->d,hw->s),hw->gc,GCFont);
xfont=NULL;//=loadFont(XBUILTINFONT); //Font *font=XLoadFont(hw->d,"6x13"); XFontStruct *fs=XLoadQueryFont(hw->d,"6x13"); //XGCValues gcval; XGetGCValues(hw->d,DefaultGC(hw->d,hw->s),GCFont,&gcval);
SETDRAWCOL(FGCOLOR); //XSetFont(hw->d,hw->gc,defont->fid); //mkfontscale,mkfontdir->/usr/share/fonts.dir //Font fontx=gcval.font; xfont=XQueryFont(hw->d,fontx);
hw->wm_protocols=XInternAtom(hw->d,"WM_PROTOCOLS",False);
hw->delete_window=XInternAtom(hw->d,"WM_DELETE_WINDOW",False);
XSetWMProtocols(hw->d,hw->w,&hw->delete_window,1); //Set WM protocols (for proper close-button hangling throug ClientMessage event-type)
if(parenthw!=NULL) {
hw->parenthw=parenthw;
parenthw->Child[parenthw->ChildAmount]=hw;
hw->id=parenthw->ChildAmount;
parenthw->ChildAmount++;
} else hw->parenthw=NULL;
hw->title=getTitle(hw);
hw->contrast=CONTRAST;
hw->width=getWinWidth(hw);
hw->height=getWinHeight(hw);
hw->WidgetAmount=0;
hw->AnimAmount=0;
return hw;
}
hWindow* createWindow (int x, int y, int w, int h, char *title, unsigned long bgcol, unsigned long fgcol, int minwidth, int minheight, short options, char* xpmicon[], void (*exitCallback)() )
{
//setlocale(LC_CTYPE,"en_EN.UTF-8"); if(XSupportsLocale()!=True){printf("\n %s No Locale Support\n",WarnPfx);} else XSetLocaleModifiers("");
hWindow *hw=malloc(sizeof(hWindow));
if(hw==NULL) {
printf("%s %s",ErrPfx,ERRORTXT_OUTOFMEM);
exit(EXIT_OUTOFMEM);
}
hw->d=openDisplay();
hw->s=DefaultScreen(hw->d);
hw->fd=ConnectionNumber(hw->d);
return createAndSetWindow(hw,NULL,&RootWindow(hw->d,hw->s),x,y,w,h,title,bgcol,fgcol,minwidth,minheight,options,xpmicon,exitCallback,0,NULL);
}
#ifdef HGUI_CHILDWINDOWS
hWindow* createChildWindow (hWindow *parenthw, int x, int y, int w, int h, char *title, unsigned long bgcol, unsigned long fgcol, int minwidth, int minheight, short options, char* xpmicon[], void (*exitCallback)() )
{
if(parenthw->ChildAmount>=CHILDWIN_AMOUNT_MAX) {
printf("%s Reached maximum child-window amount (%d). Child-window \"%s\" was not created!\n",ErrPfx,CHILDWIN_AMOUNT_MAX,title);
return NULL;
}
//x+=getWinX(parenthw);y+=getWinY(parenthw); relative location to parent-window (might be an issue if parent-window is not in place / minimized -> better leave it for Window-Manager
hWindow *hw=malloc(sizeof(hWindow));
if(hw==NULL) {
printf("%s %s",ErrPfx,ERRORTXT_OUTOFMEM);
return NULL;
}
hw->d=parenthw->d;
hw->s=parenthw->s;
createAndSetWindow(hw,parenthw,&RootWindow(hw->d,hw->s),x,y,w,h,title,bgcol,fgcol,minwidth,minheight,options,xpmicon,exitCallback,0,NULL);
return hw;
}
hWindow* createSubWindow(hWindow *parenthw, int x1, int y1, int x2, int y2, char *title, unsigned long bgcol, unsigned long fgcol)
{
if(parenthw->ChildAmount>=CHILDWIN_AMOUNT_MAX) {
printf("%s Reached maximum child-window amount (%d). Subwindow \"%s\" was not created!\n",ErrPfx,CHILDWIN_AMOUNT_MAX,title);
return NULL;
}
x1=dynCoordX(parenthw,x1);
y1=dynCoordY(parenthw,y1);
x2=dynCoordX(parenthw,x2);
y2=dynCoordY(parenthw,y2); //pos might be relative to right/bottom window-edges (negative coordinate-values)
hWindow *hw=malloc(sizeof(hWindow));
if(hw==NULL) {
printf("%s",ERRORTXT_OUTOFMEM);
return NULL;
}
hw->d=parenthw->d;
hw->s=parenthw->s; //hw->options=0;
createAndSetWindow(hw,parenthw,&parenthw->w,x1,y1,x2-x1,y2-y1,title,bgcol,fgcol,0,0,0b0000,NULL,NULL,0,NULL);
return hw;
}
#endif //HGUI_CHILDWINDOWS
void moveWindow (hWindow *hw, int x, int y)
{
if(hw==NULL) return;
XMoveWindow(hw->d,hw->w,x,y);
}
void resizeWindow (hWindow *hw, int w, int h)
{
if(hw==NULL) return;
XResizeWindow(hw->d,hw->w,w,h);
}
void iconifyWindow(hWindow *hw)
{
if(hw==NULL) return;
XIconifyWindow(hw->d,hw->w,hw->s);
}
void hideWindow(hWindow *hw)
{
if(hw==NULL) return; //_NET_WM_STATE_HIDDEN or XWithdrawWindow(hw->d,hw->w,hw->s) does nearly the same (no window and no icon is seen)
XUnmapWindow(hw->d, hw->w);
}
void showWindow(hWindow *hw)
{
if(hw==NULL) return;
XMapWindow(hw->d, hw->w);
}
void toggleFullscreen(hWindow *hw)
{
if(hw==NULL) return;
hw->options^=FULLSCREEN;
//normally restoring original position and size after fullscreen should be handled by window-manager
//if(hw->options&FULLSCREEN) { hw->norm_width=getWinWidth(hw); hw->norm_height=getWinHeight(hw); hw->norm_x=getWinX(hw); hw->norm_y=getWinY(hw); setWindowStates(hw); }
//else { setWindowStates(hw); resizeWindow(hw,hw->norm_width,hw->norm_height); moveWindow(hw,hw->norm_x,hw->norm_y); }
//XUnpapMapWindow-XMapWindow trickery is not good, use XSendEvent to send _NET_WM_STATE ClientMessage to rootwindow instead:
setWindowStates(hw);
sendStateClientMessage(hw,"FULLSCREEN",hw->options&FULLSCREEN?_NET_WM_STATE_ADD:_NET_WM_STATE_REMOVE);
} //XMapWindow(hw->d,hw->w); XSync(hw->d,True); }
void hideMouseCursor(hWindow *hw) //{ XUndefineCursor(hw->d,hw->w); } //XDefineCursor(hw->d,hw->w,None); }
{
XColor black;
black.red=black.green=black.blue=0;
static char emptydat[]= {0,0,0,0,0,0,0,0};
Pixmap emptypix=XCreateBitmapFromData(hw->d,hw->w,emptydat,8,8);
Cursor blankcur=XCreatePixmapCursor(hw->d,emptypix,emptypix,&black,&black,0,0);
XDefineCursor(hw->d,hw->w,blankcur);
XFreeCursor(hw->d,blankcur);
XFreePixmap(hw->d,emptypix);
}
//void setMouseCursor(hWindow *hw) { XColor fc, bc; Cursor cur=XCreatePixmapCursor(hw->d,icon,icon,&fc,&bc,1,1); XDefineCursor(hw->d,hw->w,cur);}
hWindow* destroyWindow(hWindow *hw)
{
if(hw==NULL) return NULL;
hWindow *parenthw=hw->parenthw;
//#ifdef USE_XFT XftDrawDestroy(xftdraw);
if(parenthw==NULL)XFreeGC(hw->d,hw->gc);
XDestroyWindow(hw->d,hw->w);
if(parenthw==NULL)XCloseDisplay(hw->d);
else {
parenthw->Child[hw->id]=NULL;
}
free(hw);
return NULL;
}
void setClass(hWindow *hw, char *class, char *name)
{
if(hw==NULL) return;
XClassHint *ch=XAllocClassHint();
ch->res_name=name;
ch->res_class=class;
XSetClassHint(hw->d,hw->w,ch);
};
void setTitle(hWindow *hw, char *title)
{
if(hw==NULL) return; //XSetWMName(hw->d,hw->w,XTextProperty *text)
XStoreName(hw->d,hw->w,title);
XSetIconName(hw->d,hw->w,title);
hw->title=title;
}
void setIconXPM(hWindow *hw, char *xpm[])
{
if(hw==NULL) return;
XPMstruct xpms=XPMtoPixels(xpm,TRANSPCOLOR);
XImage *iconimg = XCreateImage(hw->d,CopyFromParent,DefaultDepth(hw->d,hw->s),ZPixmap,0,xpms.pixdata,xpms.width,xpms.height,XPM_DEPTH,0); //bitmap_pad: how much bits a colour is stored on
XInitImage(iconimg); //pic->byte_order = LSBFirst; pic->bitmap_bit_order = LSBFirst; //The bitmap_bit_order doesn't matter with ZPixmap images.
Pixmap icon=XCreatePixmap(hw->d,hw->w,xpms.width,xpms.height,DefaultDepth(hw->d,hw->s));
XPutImage(hw->d,icon,hw->gc,iconimg,0,0,0,0,xpms.width,xpms.height); //Pixmap icon=XCreatePixmapFromBitmapData(hw->d,hw->w,icon_pixels,ICON_WIDTH,ICON_HEIGHT,0xffffff,0,XPM_DEPTH);
XWMHints *hints = XAllocWMHints();
hints->flags=IconPixmapHint;
hints->icon_pixmap=icon;
XSetWMHints(hw->d,hw->w,hints);
XDestroyImage(iconimg);
XUnmapWindow(hw->d,hw->w);
XMapWindow(hw->d,hw->w);
XFree(hints);
XSync(hw->d,False); //XFlush(hw->d); //XUnmapWindow-XMapWindow - a dirty hack for JWM to refresh icon (XSync seems not enough. What about XSendEvent ClientMessage _NET_WM_ICON?)
}
//to get display-size (monitor-resolution), DisplayWidth(hw->d,hw->s) and DisplayHeight(hw->d,hw->s) can be used, or XGetGeometry()
hWindow* getWin(void *widgetptr)
{
Widget* wp=widgetptr;
return wp->parenthw;
}
int getScreenWidth()
{
Display *d=openDisplay();
return XDisplayWidth(d,DefaultScreen(d));
}
int getScreenHeight()
{
Display *d=openDisplay();
return XDisplayHeight(d,DefaultScreen(d));
}
int getWinWidth(hWindow *hw)
{
if(hw==NULL) return RET_ERR;
XWindowAttributes xwa;
XGetWindowAttributes(hw->d,hw->w,&xwa);
return xwa.width;
}
int getWinHeight(hWindow *hw)
{
if(hw==NULL) return RET_ERR;
XWindowAttributes xwa;
XGetWindowAttributes(hw->d,hw->w,&xwa);
return xwa.height;
}
int getWinX(hWindow *hw)
{
if(hw==NULL) return RET_ERR;
XWindowAttributes xwa;
int x,y;
Window child;
XTranslateCoordinates(hw->d,hw->w,RootWindow(hw->d,hw->s),0,0,&x,&y,&child);
XGetWindowAttributes(hw->d,hw->w,&xwa);
return x-xwa.x;
}
int getWinY(hWindow *hw)
{
if(hw==NULL) return RET_ERR;
XWindowAttributes xwa;
int x,y;
Window child;
XTranslateCoordinates(hw->d,hw->w,RootWindow(hw->d,hw->s),0,0,&x,&y,&child);
XGetWindowAttributes(hw->d,hw->w,&xwa);
return y-xwa.y;
}
int getWidgetCount(hWindow *hw)
{
if(hw==NULL) return RET_ERR;
return hw->WidgetAmount;
}
char* getTitle (hWindow *hw)
{
if (hw==NULL) return (char*)EmptyString;
char* charptr;
XFetchName(hw->d,hw->w,&charptr);
return charptr;
}
float getContrast (hWindow *hw)
{
if(hw==NULL) return RET_ERR;
return hw->contrast;
}
void setContrast(hWindow *hw, float contrast)
{
if(hw==NULL) return;
correctRange(&contrast);
hw->contrast=contrast;
refreshWindow(hw);
}
void setWinCallback (hWindow *hw, void (*callback)() )
{
if(hw==NULL) return;
hw->exitCallback=callback;
}
char eventButton (hWindow *hw)
{
if(hw==NULL) return RET_ERR;
return hw->eventbutton;
}
int eventX (hWindow *hw)
{
if(hw==NULL) return RET_ERR;
return hw->eventx;
}
int eventY (hWindow *hw)
{
if(hw==NULL) return RET_ERR;
return hw->eventy;
}
unsigned long fadeCol(unsigned int col, float bri) //positive bri values cause brightening, negative values cause darkening
{
int r,g,b;
if (bri==0.0) return col;
if (bri<-1.0) bri=-1.0;
if(bri>1.0) bri=1.0; //saturation logic for input
else {
r=(int)(((col&0xFF0000)/65536)*(1.0+bri));
g=(int)(((col&0xFF00)/256)*(1.0+bri));
b=(int)((col&0xFF)*(1.0+bri));
}
if (r>255) r=255;
if(g>255) g=255;
if(b>255) b=255;
if(r<0) r=0;
if(g<0) g=0;
if(b<0) b=0; //saturation logic for output
return r*65536+g*256+b;
}
int roundfl (float num)
{
if (num>=0 && num-(int)num>=0.5) return ((int)num+1);
else if (num<0 && num-(int)num<-0.5) return ((int)num)-1;
else return (int)num;
}
int dynCoordX(hWindow *hw, int x) //turn negative X coordinate into dynamic coordinate (counted from right-edge of the window)
{
if(hw->options&ZOOMABLE) x=roundfl(x*((float)hw->width/(float)hw->orig_width));
if(x<0) return hw->width+x;
else return x;
}
//these 2 functions also provide zooming if set for the window.
int dynCoordY(hWindow *hw, int y) //turn negative Y coordinate into dynamic coordinate (counted from bottom-edge of the window)
{
if(hw->options&ZOOMABLE) y=roundfl(y*((float)hw->height/(float)hw->orig_height));
if(y<0) return hw->height+y;
else return y;
}
//this sets the region where the widget is sensitive for mouse-click/scroll events
int setActiveRegion(hWindow *hw, int id, int x1, int y1, int x2, int y2, char margin, int knobxy1, int knobxy2)
{
hw->W[id].actx1=x1;
hw->W[id].acty1=y1;
hw->W[id].actx2=x2;
hw->W[id].acty2=y2;
hw->W[id].margin=margin;
hw->W[id].knobx1=knobxy1;
hw->W[id].knobx2=knobxy2;
}
#if defined(HGUI_TEXTFIELDS) || defined(HGUI_TEXTBUTTONS)
XFontStruct* loadFont(hWindow *hw, char *fontname)
{
//#ifdef USE_XFT return XftFontOpenName(hw->d,hw->s,"Arial-17");
if (fontname==NULL) return NULL;
XFontStruct* fs = XLoadQueryFont(hw->d,fontname);
if(fs==NULL) printf("%s Font %s not found! Using Xorg built-in 6x13 fixed font in place of it.\n",WarnPfx,fontname); //XLoadQueryFont returns NULL ('xfont' fallback) of font not found
return fs;
}
XFontStruct* getFontStruct(hWindow *hw, char *fontname)
{
if(hw==NULL) return EmptyFontStruct; //should prevent redundand loading of fonts; also: only creating FontStruct for a new size of the same type
return loadFont(hw,fontname);
}
GC setFontGC (hWindow *hw, unsigned long bgcol, unsigned long fgcol, XFontStruct *font)
{
if (font==NULL) {
XSetBackground (hw->d,hw->defgc,bgcol);
XSetForeground(hw->d,hw->defgc,fgcol);
return hw->defgc;
} else {
XSetBackground (hw->d,hw->gc,bgcol);
SETDRAWCOL(fgcol);
XSetFont(hw->d,hw->gc,font->fid);
}
return hw->gc;
}
int getTextWidth(char *text, XFontStruct *font,int charcount)
{
if (font!=NULL) return XTextWidth(font,text,charcount);
else return XBUILTINFONT_WIDTH*charcount;
}
TextDim getTextDimN (char *text, XFontStruct *font, int len)
{
TextDim dim;
int dir,ascent,descent;
XCharStruct overall; //XChar2b widetxt[]; // font->max_bounds.ascent; font->max_bounds.width ?
//#ifdef USE_XFT XGlyphInfo extents; XftTextExtents8(hw->d,font,(XftChar8*)text,len,&extents); -> extents.width; extents.height;
if (font!=NULL) {
XTextExtents(font,text,len,&dir,&ascent,&descent,&overall); //printf("\n%d,%d,%s",overall.width,height,text);
dim.h=ascent+descent;
dim.w=overall.width;
dim.ascent=ascent;
dim.descent=descent;
} else {
dim.w=XBUILTINFONT_WIDTH*len;
dim.h=XBUILTINFONT_HEIGHT;
dim.ascent=XBUILTINFONT_ASCENT;
dim.descent=XBUILTINFONT_DESCENT;
}
return dim;
}
TextDim getTextDim (char *text, XFontStruct *font)
{
return getTextDimN(text,font,strlen(text));
}
#endif //textfields/buttons
#ifdef HGUI_TEXTBLOCKS
long findSpaceBack(char *text, long pos)
{
for(; pos>0; pos--) {
if(text[pos]==' '||text[pos]=='\t') break;
}
return pos;
}
//long findNonSpaceBack(char *text, long pos) { for(;pos>0;pos--) {if(text[pos]!=' '&&text[pos]!='\t') break;} return pos; }
#endif
void setClipRegion(hWindow *hw, int x1, int y1, int x2, int y2)
{
XRectangle rectangle[1];
rectangle[0].x=x1, rectangle[0].y=y1, rectangle[0].width=x2-x1, rectangle[0].height=y2-y1;
XSetClipRectangles(hw->d,hw->gc,0,0,rectangle,1,Unsorted);
XSetClipRectangles(hw->d,hw->defgc,0,0,rectangle,1,Unsorted);
}
void removeClipRegion(hWindow *hw)
{
XGCValues gcval; //XGetGCValues(hw->d,gcontext,GCClipXOrigin|GCClipYOrigin|GCClipMask,&gcval);
gcval.clip_x_origin=0;
gcval.clip_y_origin=0;
gcval.clip_mask=None;
XChangeGC(hw->d,hw->gc,GCClipXOrigin|GCClipYOrigin|GCClipMask,&gcval);
XChangeGC(hw->d,hw->defgc,GCClipXOrigin|GCClipYOrigin|GCClipMask,&gcval);
}
//-------------------------- Graphic Primitives ------------------------
#ifdef HGUI_PRIMITIVES
void drawPoint(hWindow *hw, int x, int y, unsigned long col)
{
if(hw==NULL) return;
DRAWPOINT(x,y,col);
}
void drawLine(hWindow *hw, int x1, int y1, int x2, int y2, unsigned long col)
{
if(hw==NULL) return;
SETDRAWCOL(col);
DRAWLINE(x1,y1,x2,y2);
}
void drawArc(hWindow *hw, int x1, int y1, int x2, int y2, int startdeg, int degree, unsigned long col)
{
if(hw==NULL) return;
SETDRAWCOL(col);
DRAWARC(x1,y1,x2-x1,y2-y1,startdeg,degree);
}
void drawArcFill(hWindow *hw, int x1, int y1, int x2, int y2, int startdeg, int degree, unsigned long col)
{
if(hw==NULL) return;
SETDRAWCOL(col);
DRAWARCFILL(x1,y1,x2-x1,y2-y1,startdeg,degree);
}
void drawRect(hWindow *hw, int x1, int y1, int x2, int y2, unsigned long col)
{
if(hw==NULL) return;
SETDRAWCOL(col);
DRAWRECT(x1,y1,x2-x1,y2-y1);
}
void drawRectFill(hWindow *hw, int x1, int y1, int x2, int y2, unsigned long col)
{
if(hw==NULL) return;
SETDRAWCOL(col);
DRAWRECTFILL(x1,y1,x2-x1,y2-y1);
}
void drawCircle(hWindow *hw, int x, int y, int r, unsigned long col)
{
if(hw==NULL) return;
SETDRAWCOL(col);
DRAWARC(x-r,y-r,r*2,r*2,0,360);
}
void drawCircleFill(hWindow *hw, int x, int y, int r, unsigned long col)
{
if(hw==NULL) return;
SETDRAWCOL(col);
DRAWARCFILL(x-r,y-r,r*2,r*2,0,360);
}
void drawEllipse(hWindow *hw, int x, int y, int rx, int ry, unsigned long col)
{
if(hw==NULL) return;
SETDRAWCOL(col);
DRAWARC(x-rx,y-ry,rx*2,ry*2,0,360);
}
void drawEllipseFill(hWindow *hw, int x, int y, int rx, int ry, unsigned long col)
{
if(hw==NULL) return;
SETDRAWCOL(col);
DRAWARCFILL(x-rx,y-ry,rx*2,ry*2,0,360);
}
void drawPolygon(hWindow *hw, unsigned long col, char count, ...) //variadic function with ellipsis (...) as last parameter, taking 'count' number of parameters
{
if(hw==NULL) return;
va_list arg;
int i,x,y;
va_start (arg, count);
XPoint *points=malloc(count*sizeof(XPoint));
for (i=0; i<count; i++) {
points[i].x=va_arg(arg,int);
points[i].y=va_arg(arg,int);
} //points[i].x=dynCoordX(hw,points[i].x); points[i].y=dynCoordX(hw,points[i].y); }
va_end (arg);
XFillPolygon(hw->d,hw->w,hw->gc,points,count,Complex,CoordModeOrigin);
free(points);
}
#endif //HGUI_PRIMITIVES
//-------------------------- Basic widget drawing routines -----------------------------
#ifdef HGUI_PICTURES
XImage* putPicture(hWindow *hw, int x, int y, char* pixeldata, int width, int height, int depth, int id)
{
//no scaling or colour conversion, the image-data must be prepared; transparency could be handled for images that have alpha channel
XImage *pic = XCreateImage(hw->d,CopyFromParent,DefaultDepth(hw->d,hw->s),ZPixmap,0,pixeldata,width,height,(depth>=24)?32:16,0); //bitmap_pad: how much bits a colour is stored on, 32 for 24/32bit, 16 for 15/16bit
XInitImage(pic); //pic->byte_order = LSBFirst; //pic->bitmap_bit_order = LSBFirst; //The bitmap_bit_order doesn't matter with ZPixmap images.
XPutImage(hw->d,hw->w,hw->gc,pic,0,0,x,y,width,height);
return pic; //XDestroyImage(pic);
}