1 /* coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
2 * Understanding is not required. Only obedience.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 module tklog
is aliced
;
23 import arsd
.simpledisplay
;
28 import iv
.nanovega
.blendish
;
29 import iv
.nanovega
.textlayouter
;
47 // ////////////////////////////////////////////////////////////////////////// //
48 alias LayTextClass
= LayTextD
;
51 // ////////////////////////////////////////////////////////////////////////// //
52 __gshared LayTextClass lay
;
53 __gshared
int layWinHeight
= 0;
54 __gshared
int layOffset
= 0; // from bottom
56 static struct LayUrl
{
58 uint wordidx
; // first word
61 __gshared LayUrl
[uint] layUrlList
; // for each word
64 // ////////////////////////////////////////////////////////////////////////// //
65 class MessageStart
: LayObject
{
66 long msgid
; // >0: outgoing, unacked yet
68 this (long aid
=-1) nothrow @safe @nogc { msgid
= aid
; }
70 override int width () => 0;
71 override int spacewidth () => 0;
72 override int height () => 0;
73 override int ascent () => 0;
74 override int descent () => 0;
75 override bool canbreak () => true;
76 override bool spaced () => false;
78 override void draw (NVGContext ctx
, float x
, float y
) {}
82 class MessageOutMark
: LayObject
{
83 long msgid
; // >=0: outgoing, unacked yet (0: sent in offline mode)
84 TextDigest digest
; // text digest
87 this (long aid
, SysTime atime
, const(void)[] atext
) nothrow @safe @nogc { msgid
= aid
; time
= atime
; digest
= textDigest(atext
); }
89 override int width () => kittyOut
.width
;
90 override int spacewidth () => 4;
91 override int height () => kittyOut
.height
;
92 override int ascent () => 0;
93 override int descent () => 0;
94 override bool canbreak () => true;
95 override bool spaced () => false;
97 override void draw (NVGContext ctx
, float x
, float y
) {
100 scope(exit
) nvg
.restore
;
103 nvg
.rect(x
, y
-height
+3, width
, height
);
104 nvg
.fillPaint(nvg
.imagePattern(x
, y
-height
+3, width
, height
, 0, kittyOut
));
109 final bool isOurMark (const(void)[] atext
, SysTime atime
) nothrow @trusted {
110 pragma(inline
, true);
111 return (atime
== time
&& digest
[] == textDigest(atext
));
114 final bool isOurMark (long aid
, const(void)[] atext
, SysTime atime
) nothrow @trusted {
115 pragma(inline
, true);
116 return (aid
> 0 ?
(msgid
== aid
) : (atime
== time
&& digest
[] == textDigest(atext
)));
121 // ////////////////////////////////////////////////////////////////////////// //
122 void logFixAckMessageId (long oldid
, long newid
, const(void)[] text
, SysTime time
) {
123 if (oldid
== newid
) return; // nothing to do
124 foreach (immutable uint widx
; 0..lay
.wordCount
) {
125 auto w
= lay
.wordByIndex(widx
);
126 int oidx
= w
.objectIdx
;
128 if (auto maw
= cast(MessageOutMark
)lay
.objectAtIndex(oidx
)) {
129 if (maw
.isOurMark(oldid
, text
, time
)) {
131 glconPostScreenRepaint(); // redraw
139 void logResetAckMessageIds () {
140 foreach (immutable uint widx
; 0..lay
.wordCount
) {
141 auto w
= lay
.wordByIndex(widx
);
142 int oidx
= w
.objectIdx
;
144 if (auto maw
= cast(MessageOutMark
)lay
.objectAtIndex(oidx
)) {
146 glconPostScreenRepaint(); // redraw
153 void ackLogMessage (long msgid
) {
154 if (msgid
<= 0) return;
155 foreach (immutable uint widx
; 0..lay
.wordCount
) {
156 auto w
= lay
.wordByIndex(widx
);
157 int oidx
= w
.objectIdx
;
159 if (auto maw
= cast(MessageOutMark
)lay
.objectAtIndex(oidx
)) {
160 if (maw
.msgid
== msgid
) {
161 maw
.msgid
= -1; // reset mark
162 glconPostScreenRepaint(); // redraw
170 // coordinates are adjusted so (0, 0) points to logical layouter top-left
172 bool logCheckCancelMark (int mx, int my) {
173 bool removeFromResendQueue (long msgid, in ref TextDigest digest, SysTime time) {
177 // ////////////////////////////////////////////////////////////////////////// //
179 lay
.wipeAll(true); // clear log, but delete objects
185 void addDividerLine (bool doflushgui
=false) {
186 if (glconCtlWindow
is null || glconCtlWindow
.closed
) return;
189 bool inFrame
= nvg
.inFrame
;
191 glconCtlWindow
.setAsCurrentOpenGlContext(); // make this window active
192 nvg
.beginFrame(glconCtlWindow
.width
, glconCtlWindow
.height
);
197 if (doflushgui
) flushGui();
198 glconCtlWindow
.releaseCurrentOpenGlContext();
202 lay
.fontStyle
.fontsize
= 2;
203 lay
.fontStyle
.color
= NVGColor
.k8orange
.asUint
;
204 lay
.fontStyle
.bgcolor
= NVGColor("#aa0").asUint
;
205 lay
.fontStyle
.monospace
= true;
212 glconPostScreenRepaint();
216 // `ct` can be `null` for "my message" or "system message"
217 void addTextToLog (Account acc
, Contact ct
, LogFile
.Msg
.Kind kind
, bool action
, const(char)[] msg
, SysTime time
, long msgid
=-1, bool doflushgui
=false) {
218 if (glconCtlWindow
is null || glconCtlWindow
.closed
) return;
221 bool inFrame
= nvg
.inFrame
;
223 glconCtlWindow
.setAsCurrentOpenGlContext(); // make this window active
224 nvg
.beginFrame(glconCtlWindow
.width
, glconCtlWindow
.height
);
229 if (doflushgui
) flushGui();
230 glconCtlWindow
.releaseCurrentOpenGlContext();
234 // add "message start" mark
235 lay
.putObject(new MessageStart(msgid
));
237 lay
.fontStyle
.fontsize
= 16;
238 lay
.fontStyle
.color
= NVGColor
.k8orange
.asUint
;
239 lay
.fontStyle
.bgcolor
= NVGColor("#222").asUint
;
240 lay
.fontStyle
.monospace
= true;
244 final switch (kind
) {
245 case LogFile
.Msg
.Kind
.Outgoing
: textColor
= NVGColor
.k8orange
; lay
.fontStyle
.color
= NVGColor("#c40").asUint
; lay
.put(acc
.info
.nick
); break;
246 case LogFile
.Msg
.Kind
.Incoming
: textColor
= NVGColor("#ccc"); lay
.fontStyle
.color
= NVGColor("#666").asUint
; lay
.put(ct
.info
.nick
); break;
247 case LogFile
.Msg
.Kind
.Notification
: textColor
= NVGColor("#0c0"); lay
.fontStyle
.color
= textColor
.asUint
; lay
.put("*system*"); break;
250 lay
.fontStyle
.monospace
= false;
251 //lay.putHardSpace(64);
253 // add "message outgoing" mark
254 if (kind
== LogFile
.Msg
.Kind
.Outgoing
) {
255 //conwriteln("OG: msgid=", msgid);
257 msgid
= ct
.findInResendQueue(msg
, time
);
258 //conwriteln(" new msgid=", msgid);
260 lay
.putObject(new MessageOutMark(msgid
, time
, msg
));
264 if (kind
== LogFile
.Msg
.Kind
.Incoming
) lay
.fontStyle
.color
= NVGColor("#888").asUint
;
267 import std
.format
: format
;
268 auto dt = cast(DateTime
)time
;
269 string tstr
= "%04u/%02u/%02u".format(dt.year
, dt.month
, dt.day
);
274 lay
.fontStyle
.color
= (kind
== LogFile
.Msg
.Kind
.Incoming ?
NVGColor("#888").asUint
: NVGColor("#666").asUint
);
275 lay
.fontStyle
.bgcolor
= NVGColor("#006").asUint
;
278 import std
.format
: format
;
279 auto dt = cast(DateTime
)time
;
280 string tstr
= "%02u:%02u:%02u".format(dt.hour
, dt.minute
, dt.second
);
286 lay
.fontStyle
.bgcolor
= NVGColor
.transparent
.asUint
;
287 lay
.fontStyle
.fontsize
= 20;
290 lay
.fontStyle
.color
= NVGColor("#fff").asUint
;
294 void xput (const(char)[] str) {
296 auto nl
= str.indexOf('\n');
297 if (nl
< 0) { lay
.put(str); break; }
298 if (nl
> 0) lay
.put(str[0..nl
]);
304 msg
= msg
.xstripright
;
306 lay
.fontStyle
.color
= textColor
.asUint
;
308 auto nfo
= urlDetect(msg
);
314 xput(msg
[0..nfo
.pos
]);
315 string url
= msg
[nfo
.pos
..nfo
.end
].idup
;
316 msg
= msg
[nfo
.end
..$];
317 auto stword
= lay
.nextWordIndex
;
319 auto c
= lay
.fontStyle
.color
;
320 scope(exit
) { lay
.popStyles
; lay
.fontStyle
.color
= c
; }
321 lay
.fontStyle
.href
= true;
322 lay
.fontStyle
.underline
= true;
323 lay
.fontStyle
.color
= NVGColor("#06f").asUint
;
325 while (stword
< lay
.nextWordIndex
) {
326 layUrlList
[stword
] = LayUrl(url
, stword
);
335 glconPostScreenRepaint();
339 void addTextToLog (Account acc
, Contact ct
, in ref LogFile
.Msg msg
, long msgid
=-1, bool doflushgui
=false) {
340 import iv
.utfutil
: utf8Encode
;
343 scope(exit
) delete text
;
345 foreach (dchar dc
; msg
.byDChar
) {
347 auto len
= utf8Encode(buf
[], dc
);
350 addTextToLog(acc
, ct
, msg
.kind
, msg
.isMe
, text
, msg
.time
, msgid
, doflushgui
);