1 | // |
---|
2 | // "$Id$" |
---|
3 | // |
---|
4 | // Line style code for the Fast Light Tool Kit (FLTK). |
---|
5 | // |
---|
6 | // Copyright 1998-2005 by Bill Spitzak and others. |
---|
7 | // |
---|
8 | // This library is free software; you can redistribute it and/or |
---|
9 | // modify it under the terms of the GNU Library General Public |
---|
10 | // License as published by the Free Software Foundation; either |
---|
11 | // version 2 of the License, or (at your option) any later version. |
---|
12 | // |
---|
13 | // This library is distributed in the hope that it will be useful, |
---|
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | // Library General Public License for more details. |
---|
17 | // |
---|
18 | // You should have received a copy of the GNU Library General Public |
---|
19 | // License along with this library; if not, write to the Free Software |
---|
20 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
---|
21 | // USA. |
---|
22 | // |
---|
23 | // Please report all bugs and problems on the following page: |
---|
24 | // |
---|
25 | // http://www.fltk.org/str.php |
---|
26 | // |
---|
27 | |
---|
28 | #include <FL/Fl.H> |
---|
29 | #include <FL/fl_draw.H> |
---|
30 | #include <FL/x.H> |
---|
31 | #include "flstring.h" |
---|
32 | #include <stdio.h> |
---|
33 | |
---|
34 | #ifdef __APPLE_QUARTZ__ |
---|
35 | float fl_quartz_line_width_ = 1.0f; |
---|
36 | static enum CGLineCap fl_quartz_line_cap_ = kCGLineCapButt; |
---|
37 | static enum CGLineJoin fl_quartz_line_join_ = kCGLineJoinMiter; |
---|
38 | static float *fl_quartz_line_pattern = 0; |
---|
39 | static int fl_quartz_line_pattern_size = 0; |
---|
40 | void fl_quartz_restore_line_style_() { |
---|
41 | CGContextSetLineWidth(fl_gc, fl_quartz_line_width_); |
---|
42 | CGContextSetLineCap(fl_gc, fl_quartz_line_cap_); |
---|
43 | CGContextSetLineJoin(fl_gc, fl_quartz_line_join_); |
---|
44 | CGContextSetLineDash(fl_gc, 0, fl_quartz_line_pattern, fl_quartz_line_pattern_size); |
---|
45 | } |
---|
46 | #endif |
---|
47 | |
---|
48 | void fl_line_style(int style, int width, char* dashes) { |
---|
49 | #ifdef WIN32 |
---|
50 | // According to Bill, the "default" cap and join should be the |
---|
51 | // "fastest" mode supported for the platform. I don't know why |
---|
52 | // they should be different (same graphics cards, etc., right?) MRS |
---|
53 | static DWORD Cap[4]= {PS_ENDCAP_FLAT, PS_ENDCAP_FLAT, PS_ENDCAP_ROUND, PS_ENDCAP_SQUARE}; |
---|
54 | static DWORD Join[4]={PS_JOIN_ROUND, PS_JOIN_MITER, PS_JOIN_ROUND, PS_JOIN_BEVEL}; |
---|
55 | int s1 = PS_GEOMETRIC | Cap[(style>>8)&3] | Join[(style>>12)&3]; |
---|
56 | DWORD a[16]; int n = 0; |
---|
57 | if (dashes && dashes[0]) { |
---|
58 | s1 |= PS_USERSTYLE; |
---|
59 | for (n = 0; n < 16 && *dashes; n++) a[n] = *dashes++; |
---|
60 | } else { |
---|
61 | s1 |= style & 0xff; // allow them to pass any low 8 bits for style |
---|
62 | } |
---|
63 | if ((style || n) && !width) width = 1; // fix cards that do nothing for 0? |
---|
64 | LOGBRUSH penbrush = {BS_SOLID,fl_RGB(),0}; // can this be fl_brush()? |
---|
65 | HPEN newpen = ExtCreatePen(s1, width, &penbrush, n, n ? a : 0); |
---|
66 | if (!newpen) { |
---|
67 | Fl::error("fl_line_style(): Could not create GDI pen object."); |
---|
68 | return; |
---|
69 | } |
---|
70 | HPEN oldpen = (HPEN)SelectObject(fl_gc, newpen); |
---|
71 | DeleteObject(oldpen); |
---|
72 | DeleteObject(fl_current_xmap->pen); |
---|
73 | fl_current_xmap->pen = newpen; |
---|
74 | #elif defined(__APPLE_QD__) |
---|
75 | // QuickDraw supports pen size and pattern, but no arbitrary line styles. |
---|
76 | static Pattern styles[] = { |
---|
77 | { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } }, // FL_SOLID |
---|
78 | { { 0xf0, 0xf0, 0xf0, 0xf0, 0x0f, 0x0f, 0x0f, 0x0f } }, // FL_DASH |
---|
79 | { { 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55 } } // FL_DOT |
---|
80 | }; |
---|
81 | |
---|
82 | if (!width) width = 1; |
---|
83 | PenSize(width, width); |
---|
84 | |
---|
85 | style &= 0xff; |
---|
86 | if (style > 2) style = 2; |
---|
87 | PenPat(styles + style); |
---|
88 | #elif defined(__APPLE_QUARTZ__) |
---|
89 | static enum CGLineCap Cap[4] = { kCGLineCapButt, kCGLineCapButt, |
---|
90 | kCGLineCapRound, kCGLineCapSquare }; |
---|
91 | static enum CGLineJoin Join[4] = { kCGLineJoinMiter, kCGLineJoinMiter, |
---|
92 | kCGLineJoinRound, kCGLineJoinBevel }; |
---|
93 | if (width<1) width = 1; |
---|
94 | fl_quartz_line_width_ = (float)width; |
---|
95 | fl_quartz_line_cap_ = Cap[(style>>8)&3]; |
---|
96 | fl_quartz_line_join_ = Join[(style>>12)&3]; |
---|
97 | char *d = dashes; |
---|
98 | static float pattern[16]; |
---|
99 | if (d && *d) { |
---|
100 | float *p = pattern; |
---|
101 | while (*d) { *p++ = (float)*d++; } |
---|
102 | fl_quartz_line_pattern = pattern; |
---|
103 | fl_quartz_line_pattern_size = d-dashes; |
---|
104 | } else if (style & 0xff) { |
---|
105 | char dash, dot, gap; |
---|
106 | // adjust lengths to account for cap: |
---|
107 | if (style & 0x200) { |
---|
108 | dash = char(2*width); |
---|
109 | dot = 1; |
---|
110 | gap = char(2*width-1); |
---|
111 | } else { |
---|
112 | dash = char(3*width); |
---|
113 | dot = gap = char(width); |
---|
114 | } |
---|
115 | float *p = pattern; |
---|
116 | switch (style & 0xff) { |
---|
117 | case FL_DASH: *p++ = dash; *p++ = gap; break; |
---|
118 | case FL_DOT: *p++ = dot; *p++ = gap; break; |
---|
119 | case FL_DASHDOT: *p++ = dash; *p++ = gap; *p++ = dot; *p++ = gap; break; |
---|
120 | case FL_DASHDOTDOT: *p++ = dash; *p++ = gap; *p++ = dot; *p++ = gap; *p++ = dot; *p++ = gap; break; |
---|
121 | } |
---|
122 | fl_quartz_line_pattern_size = p-pattern; |
---|
123 | fl_quartz_line_pattern = pattern; |
---|
124 | } else { |
---|
125 | fl_quartz_line_pattern = 0; fl_quartz_line_pattern_size = 0; |
---|
126 | } |
---|
127 | fl_quartz_restore_line_style_(); |
---|
128 | #else |
---|
129 | int ndashes = dashes ? strlen(dashes) : 0; |
---|
130 | // emulate the WIN32 dash patterns on X |
---|
131 | char buf[7]; |
---|
132 | if (!ndashes && (style&0xff)) { |
---|
133 | int w = width ? width : 1; |
---|
134 | char dash, dot, gap; |
---|
135 | // adjust lengths to account for cap: |
---|
136 | if (style & 0x200) { |
---|
137 | dash = char(2*w); |
---|
138 | dot = 1; // unfortunately 0 does not work |
---|
139 | gap = char(2*w-1); |
---|
140 | } else { |
---|
141 | dash = char(3*w); |
---|
142 | dot = gap = char(w); |
---|
143 | } |
---|
144 | char* p = dashes = buf; |
---|
145 | switch (style & 0xff) { |
---|
146 | case FL_DASH: *p++ = dash; *p++ = gap; break; |
---|
147 | case FL_DOT: *p++ = dot; *p++ = gap; break; |
---|
148 | case FL_DASHDOT: *p++ = dash; *p++ = gap; *p++ = dot; *p++ = gap; break; |
---|
149 | case FL_DASHDOTDOT: *p++ = dash; *p++ = gap; *p++ = dot; *p++ = gap; *p++ = dot; *p++ = gap; break; |
---|
150 | } |
---|
151 | ndashes = p-buf; |
---|
152 | } |
---|
153 | static int Cap[4] = {CapButt, CapButt, CapRound, CapProjecting}; |
---|
154 | static int Join[4] = {JoinMiter, JoinMiter, JoinRound, JoinBevel}; |
---|
155 | XSetLineAttributes(fl_display, fl_gc, width, |
---|
156 | ndashes ? LineOnOffDash : LineSolid, |
---|
157 | Cap[(style>>8)&3], Join[(style>>12)&3]); |
---|
158 | if (ndashes) XSetDashes(fl_display, fl_gc, 0, dashes, ndashes); |
---|
159 | #endif |
---|
160 | } |
---|
161 | |
---|
162 | |
---|
163 | // |
---|
164 | // End of "$Id$". |
---|
165 | // |
---|