diff --git a/include/lgi/common/Array.h b/include/lgi/common/Array.h --- a/include/lgi/common/Array.h +++ b/include/lgi/common/Array.h @@ -1,932 +1,930 @@ /// \file /// \author Matthew Allen /// \brief Growable, type-safe array. #pragma once #include #include #include #include #include #define GARRAY_MIN_SIZE 16 #if defined(LGI_CHECK_MALLOC) && !defined(LGI_MEM_DEBUG) #error "Include GMem.h first" #endif #if defined(PLATFORM_MINGW) #ifdef __cplusplus extern "C" { #endif #ifndef _QSORT_S_DEFINED #define _QSORT_S_DEFINED _CRTIMP void __cdecl qsort_s(void *_Base, size_t _NumOfElements, size_t _SizeOfElements, int (__cdecl *_PtFuncCompare)(void *,const void *,const void *), void *_Context); #endif #ifdef __cplusplus } #endif #endif #include "lgi/common/Range.h" /// \brief Growable type-safe array. /// \ingroup Base /// /// You can store simple objects inline in this array, but all their contents are initialized /// to the octet 0x00. Which limits use to objects that don't have a virtual table and /// don't need construction (constructors are not called). /// /// The objects are copied around during use so you can't assume their pointer /// will remain the same over time either. However when objects are deleted from the /// array their destructors WILL be called. This allows you to have simple objects that /// have dynamically allocated pointers that are freed properly. A good example of this /// type of object is the LVariant or LAutoString class. /// /// If you want to store objects with a virtual table, or that need their constructor /// to be called then you should create the LArray with pointers to the objects instead /// of inline objects. And to clean up the memory you can call LArray::DeleteObjects or /// LArray::DeleteArrays. template class LArray { Type *p; size_t len; size_t alloc; #ifdef _DEBUG public: size_t GetAlloc() { return alloc; } #endif protected: - bool fixed; + bool fixed = false; + bool warnResize = true; public: typedef Type ItemType; /// Constructor LArray(size_t PreAlloc = 0) { p = 0; alloc = len = PreAlloc; fixed = false; if (alloc) { size_t Bytes = sizeof(Type) * alloc; p = (Type*) malloc(Bytes); if (p) { memset(p, 0, Bytes); } else { alloc = len = 0; } } } LArray(std::initializer_list il) { p = NULL; alloc = len = 0; fixed = false; if (Length(il.size())) { size_t n = 0; for (auto i: il) p[n++] = i; } } LArray(const LArray &c) { p = 0; alloc = len = 0; fixed = false; *this = c; } /// Destructor ~LArray() { Length(0); } /// This frees the memory without calling the destructor of the elements. /// Useful if you're storing large amounts of plain data types, like char or int. void Free() { if (p) { free(p); p = NULL; } len = alloc = 0; } /// Does a range check on a pointer... /// \returns true if the pointer is pointing to a valid object /// in this array. bool PtrCheck(void *Ptr) { return p != NULL && Ptr >= p && Ptr < &p[len]; } /// Does a range check on an index... bool IdxCheck(ssize_t i) const { return i >= 0 && i < (ssize_t)len; } /// Returns the number of used entries size_t Length() const { return len; } /// Makes the length fixed.. void SetFixedLength(bool fix = true) { fixed = fix; } /// Emtpies the array of all objects. bool Empty() { return Length(0); } /// Sets the length of available entries bool Length(size_t i) { if (i > 0) { if (i > len && fixed) { assert(!"Attempt to enlarged fixed array."); return false; } size_t nalloc = alloc; if (i < len) { // Shrinking } else { // Expanding nalloc = 0x10; while (nalloc < i) nalloc <<= 1; assert(nalloc >= i); } if (nalloc != alloc) { Type *np = (Type*)malloc(sizeof(Type) * nalloc); if (!np) { return false; } memset(np + len, 0, (nalloc - len) * sizeof(Type)); if (p) { // copy across common elements memcpy(np, p, MIN(len, i) * sizeof(Type)); free(p); } p = np; alloc = nalloc; } if (i > len) { // zero new elements memset(p + len, 0, sizeof(Type) * (nalloc - len)); } else if (i < len) { for (size_t n=i; n &a) const { if (Length() != a.Length()) return false; for (size_t i=0; i &a) const { return !(*this == a); } LArray &operator =(const LArray &a) { Length(a.Length()); if (p && a.p) { for (size_t i=0; i= 0 && (uint32_t)i < len) return p[i]; static Type t; return t; } // Returns the address of an item or NULL if index is out of range Type *AddressOf(size_t i = 0) { return i < len ? p + i : NULL; } /// \brief Returns a reference a given entry. /// /// If the entry is off the end of the array and "fixed" is false, /// it will grow to make it valid. Type &operator [](size_t i) { static Type t; if ( (fixed && (uint32_t)i >= len) ) { - memset(&t, 0, sizeof(t)); - if (fixed && (uint32_t)i >= len) - { + if (warnResize) assert(!"Attempt to enlarged fixed array."); - } return t; } if (i >= (int)alloc) { // increase array length size_t nalloc = MAX(alloc, GARRAY_MIN_SIZE); while (nalloc <= (uint32_t)i) { nalloc <<= 1; } #if 0 if (nalloc > 1<<30) { #if defined(_DEBUG) && defined(_MSC_VER) LAssert(0); #endif ZeroObj(t); return t; } #endif // alloc new array Type *np = (Type*) malloc(sizeof(Type) * nalloc); if (np) { // clear new cells memset(np + len, 0, (nalloc - len) * sizeof(Type)); if (p) { // copy across old cells memcpy(np, p, len * sizeof(Type)); // clear old array free(p); } // new values p = np; alloc = nalloc; } else { static Type *t = 0; return *t; } } // adjust length of the the array if ((uint32_t)i + 1 > len) { len = i + 1; } return p[i]; } /// Delete all the entries as if they are pointers to objects void DeleteObjects() { if (len > 0) { size_t InitialLen = len; delete p[0]; if (InitialLen == len) { // Non self deleting for (uint i=1; i= 0; } /// Removes last element bool PopLast() { if (len <= 0) return false; return Length(len - 1); } /// Deletes an entry bool DeleteAt ( /// The index of the entry to delete size_t Index, /// true if the order of the array matters, otherwise false. bool Ordered = false ) { if (p && Index < len) { // Delete the object p[Index].~Type(); // Move the memory up if (Index < len - 1) { if (Ordered) { memmove(p + Index, p + Index + 1, (len - Index - 1) * sizeof(Type) ); } else { p[Index] = p[len-1]; } } // Adjust length len--; // Kill the element at the end... otherwise New() returns non-zero data. memset(p + len, 0, sizeof(Type)); return true; } return false; } /// Deletes a range. /// This operation always maintains order. /// \returns the number of removed elements ssize_t DeleteRange(LRange r) { // Truncate range to the size of this array if (r.Start < 0) r.Start = 0; if (r.End() >= (ssize_t)len) r.Len = len - r.Start; // Delete all the elements we are removing for (ssize_t i=r.Start; i 0) memmove(&p[r.Start], &p[r.End()], sizeof(Type)*Remain); len = r.Start + Remain; return r.Len; } /// Deletes the entry 'n' bool Delete ( /// The value of the entry to delete Type n, /// true if the order of the array matters, otherwise false. bool Ordered = false ) { ssize_t i = IndexOf(n); if (p && i >= 0) { return DeleteAt(i, Ordered); } return false; } /// Appends an element void Add ( /// Item to insert const Type &n ) { (*this)[len] = n; } /// Appends multiple elements bool Add ( /// Items to insert const Type *s, /// Length of array ssize_t count ) { if (!s || count < 1) return false; ssize_t i = len; if (!Length(len + count)) return false; Type *d = p + i; while (count--) { *d++ = *s++; } return true; } /// Appends an array of elements bool Add ( /// Array to insert const LArray &a ) { ssize_t old = len; if (Length(len + a.Length())) { for (unsigned i=0; i operator +(const LArray &b) { LArray a = *this; a.Add(b); return a; } LArray &operator +=(const LArray &b) { Add(b); return *this; } /// Inserts an element into the array bool AddAt ( /// Item to insert before size_t Index, /// Item to insert Type n ) { // Make room if (!Length(len + 1)) return false; if (Index < len - 1) // Shift elements after insert point up one memmove(p + Index + 1, p + Index, (len - Index - 1) * sizeof(Type) ); else // Add at the end, not after the end... Index = len - 1; // Insert item memset(p + Index, 0, sizeof(*p)); p[Index] = n; return true; } /// Inserts an array into the array at a position bool AddAt ( /// Item to insert before size_t Index, /// Array to insert const LArray &a ) { // Make room if (!Length(len + a.Length())) return false; if (Index < len - 1) // Shift elements after insert point up one memmove(p + Index + a.Length(), p + Index, (len - Index - a.Length()) * sizeof(Type) ); else // Add at the end, not after the end... Index = len - 1; // Insert items memset(p + Index, 0, a.Length() * sizeof(*p)); for (size_t i=0; i 0) return 1; return r < 0 ? -1 : 0; }); } /// Sorts the array via a callback. void Sort(std::function Compare) { #if !defined(WINDOWS) && !defined(HAIKU) && !defined(LINUX) #define USER_DATA_FIRST 1 #else #define USER_DATA_FIRST 0 #endif #if defined(WINDOWS) /* _ACRTIMP void __cdecl qsort_s(void* _Base, rsize_t _NumOfElements, rsize_t _SizeOfElements, int (__cdecl* _PtFuncCompare)(void*, void const*, void const*), void* _Context); */ qsort_s #else qsort_r #endif ( p, len, sizeof(Type), #if USER_DATA_FIRST &Compare, #endif #if defined(HAIKU) || defined(LINUX) // typedef int (*_compare_function_qsort_r)(const void*, const void*, void*); // extern void qsort_r(void* base, size_t numElements, size_t sizeOfElement, _compare_function_qsort_r, void* cookie); [](const void *a, const void *b, void *ud) -> int #else [](void *ud, const void *a, const void *b) -> int #endif { return (*( (std::function*)ud ))((Type*)a, (Type*)b); } #if !USER_DATA_FIRST , &Compare #endif ); } /// \returns a reference to a new object on the end of the array /// /// Never assign this to an existing variable. e.g: /// LArray a; /// MyObject &o = a.New(); /// o.Type = something; /// o = a.New(); /// o.Type = something else; /// /// This causes the first object to be overwritten with a blank copy. Type &New() { return (*this)[len]; } /// Returns the memory held by the array and sets itself to empty Type *Release() { Type *Ptr = p; p = 0; len = alloc = 0; return Ptr; } void Swap(LArray &other) { LSwap(p, other.p); LSwap(len, other.len); LSwap(alloc, other.alloc); } /// Swaps a range of elements between this array and 'b' bool SwapRange ( // The range of 'this' to swap out LRange aRange, // The other array to swap with LArray &b, // The range of 'b' to swap with this array LRange bRange ) { LArray Tmp; // Store entries in this that will be swapped Tmp.Add(AddressOf(aRange.Start), aRange.Len); // Copy b's range into this ssize_t Common = MIN(bRange.Len, aRange.Len); ssize_t aIdx = aRange.Start; ssize_t bIdx = bRange.Start; for (int i=0; i bRange.Len) { // Shrink the range in this to fit 'b' ssize_t Del = aRange.Len - bRange.Len; for (ssize_t i=0; i bRange.Len) { // Grow range to fit this ssize_t Add = aRange.Len - bRange.Len; for (ssize_t i=0; i class Iter { friend class ::LArray; ssize_t i; char each_dir; LArray *a; public: Iter(LArray *arr) // 'End' constructor { i = -1; a = arr; each_dir = 0; } Iter(LArray *arr, size_t pos) { i = pos; a = arr; each_dir = 0; } bool operator ==(const Iter &it) const { int x = (int)In() + (int)it.In(); if (x == 2) return (a == it.a) && (i == it.i); return x == 0; } bool operator !=(const Iter &it) const { return !(*this == it); } operator bool() const { return In(); } bool In() const { return i >= 0 && i < (ssize_t)a->Length(); } bool End() const { return i < 0 || i >= a->Length(); } T &operator *() { return (*a)[i]; } Iter &operator ++() { i++; return *this; } Iter &operator --() { i--; return *this; } Iter &operator ++(int) { i++; return *this; } Iter &operator --(int) { i--; return *this; } }; typedef Iter I; I begin(size_t start = 0) { return I(this, start); } I rbegin() { return I(this, len-1); } I end() { return I(this); } bool Delete(I &It) { return DeleteAt(It.i, true); } LArray Slice(ssize_t Start, ssize_t End = -1) { LArray a; // Range limit the inputs... if (Start < 0) Start = len + Start; if (Start > (ssize_t)len) Start = len; if (End < 0) End = len + End + 1; if (End > (ssize_t)len) End = len; if (End > Start) { a.Length(End - Start); for (size_t i=0; i Reverse() { LArray r; r.Length(len); for (size_t i=0, k=len-1; i Compare) { static Type Empty; if (!Compare) return Empty; for (size_t s = 0, e = len - 1; s <= e; ) { if (e - s < 2) { if (Compare(p[s]) == 0) return p[s]; if (e > s && Compare(p[e]) == 0) return p[e]; break; // Not found } size_t mid = s + ((e - s) >> 1); int result = Compare(p[mid]); if (result == 0) return p[mid]; if (result < 0) s = mid + 1; // search after the mid point else e = mid - 1; // search before } return Empty; } }; diff --git a/include/lgi/common/StringClass.h b/include/lgi/common/StringClass.h --- a/include/lgi/common/StringClass.h +++ b/include/lgi/common/StringClass.h @@ -1,1248 +1,1255 @@ /* * A mis-guided attempt to make a string class look and feel like a python string. * * Author: Matthew Allen * Email: fret@memecode.com * Created: 16 Sept 2014 */ #pragma once #include #include #include #if defined(_MSC_VER) || defined(__GTK_H__) // This fixes compile errors in VS2008/Gtk #undef _SIGN_DEFINED #undef abs #endif #include #if defined(_MSC_VER) && _MSC_VER < 1800/*_MSC_VER_VS2013*/ #include #define PRId64 "I64i" #else #define __STDC_FORMAT_MACROS 1 #include #include #ifndef PRId64 #warning "PRId64 not defined." #define PRId64 "Ld" #endif #endif #include "LgiOsDefs.h" #include "lgi/common/Unicode.h" #include "lgi/common/Array.h" #ifndef IsDigit #define IsDigit(ch) ((ch) >= '0' && (ch) <= '9') #endif LgiExtern int LPrintf(class LString &Str, const char *Format, va_list &Arg); /// A pythonic string class. class LString { protected: /// This structure holds the string's data itself and is shared /// between one or more LString instances. struct RefStr { /// A reference count int32 Refs; /// The bytes in 'Str' not including the NULL terminator size_t Len; /// The first byte of the string. Further bytes are allocated /// off the end of the structure using malloc. This must always /// be the last element in the struct. char Str[1]; } *Str; inline void _strip(LString &ret, const char *set, bool left, bool right) { if (!Str) return; char *s = Str->Str; char *e = s + Str->Len; if (!set) set = " \t\r\n"; if (left) { while (s < e && strchr(set, *s)) s++; } if (right) { while (e > s && strchr(set, e[-1])) e--; } if (e > s) ret.Set(s, e - s); } public: #ifdef LGI_UNIT_TESTS static int32 RefStrCount; #endif /// A copyable array of strings class Array : public LArray { public: - Array(size_t PreAlloc = 0) : LArray(PreAlloc) {} + Array(size_t PreAlloc = 0) : LArray(PreAlloc) + { + // This allows the parent array to return an empty + // string without asserting if the caller requests an + // out of range index + warnResize = false; + } + Array(const Array &a) { *this = (Array&)a; } - + Array &operator =(const Array &a) { SetFixedLength(false); *((LArray*)this) = a; SetFixedLength(true); return *this; } Array &operator +=(const Array &a) { SetFixedLength(false); Add(a); SetFixedLength(true); return *this; } Array &operator +=(const LArray &a) { SetFixedLength(false); Add(a); SetFixedLength(true); return *this; } }; /// Empty constructor LString() { Str = NULL; } // This odd looking constructor allows the object to be used as the value type // in a GHashTable, where the initialiser is '0', an integer. LString(int i) { Str = NULL; } #ifndef _MSC_VER // This odd looking constructor allows the object to be used as the value type // in a GHashTable, where the initialiser is '0', an integer. LString(long int i) { Str = NULL; } #endif /// String constructor LString(const char *str, ptrdiff_t bytes) { Str = NULL; Set(str, bytes); } /// const char* constructor LString(const char *str) { Str = NULL; Set(str); } /// const char16* constructor LString(const wchar_t *str, ptrdiff_t wchars = -1) { Str = NULL; SetW(str, wchars); } #if defined(_WIN32) || defined(MAC) /// const uint32* constructor LString(const uint32_t *str, ptrdiff_t chars = -1) { Str = NULL; if (chars < 0) chars = Strlen(str); ptrdiff_t utf_len = 0; const uint32_t *end = str + chars; const uint32_t *c = str; while (c < end) { uint8_t utf[6], *u = utf; ssize_t len = sizeof(utf); if (!LgiUtf32To8(*c++, u, len)) break; utf_len += u - utf; } if (Length((uint32_t)utf_len)) { c = str; uint8_t *u = (uint8_t*)Str->Str; ssize_t len = Str->Len; while (c < end) { if (!LgiUtf32To8(*c++, u, len)) break; } *u++ = 0; } } #endif /// LString constructor LString(const LString &s) { Str = s.Str; if (Str) Str->Refs++; } ~LString() { Empty(); } /// Removes a reference to the string and deletes if needed void Empty() { if (!Str) return; Str->Refs--; if (Str->Refs < 0) { assert(!"Invalid refs"); } if (Str->Refs == 0) { free(Str); #ifdef LGI_UNIT_TESTS RefStrCount--; #endif } Str = NULL; } /// Returns the pointer to the string data char *Get() const { return Str ? Str->Str : NULL; } /// Sets the string to a new value bool Set ( /// Can be a pointer to string data or NULL to create an empty buffer (requires valid length) const char *str, /// Byte length of input string or -1 to copy till the NULL terminator. ptrdiff_t bytes = -1 ) { Empty(); if (bytes < 0) { if (str) bytes = strlen(str); else return false; } Str = (RefStr*)malloc(sizeof(RefStr) + bytes); if (!Str) return false; Str->Refs = 1; Str->Len = (uint32_t)bytes; #ifdef LGI_UNIT_TESTS RefStrCount++; #endif if (str) memcpy(Str->Str, str, bytes); Str->Str[bytes] = 0; return true; } /// Sets the string to a new value bool SetW ( /// Can be a pointer to string data or NULL to create an empty buffer (requires valid length) const wchar_t *str, /// Number of 'char16' values in the input string or -1 to copy till the NULL terminator. ptrdiff_t wchars = -1 ) { size_t Sz = WideToUtf8Len(str, wchars); if (Length(Sz)) { #ifdef _MSC_VER const uint16 *i = (const uint16*) str; ssize_t InLen = wchars >= 0 ? wchars << 1 : 0x7fffffff; assert(sizeof(*i) == sizeof(*str)); uint8_t *o = (uint8_t*)Str->Str; ssize_t OutLen = Str->Len; for (uint32_t ch; ch = LgiUtf16To32(i, InLen); ) { if (!LgiUtf32To8(ch, o, OutLen)) { *o = 0; break; } } #else uint8_t *o = (uint8_t*)Str->Str; ssize_t OutLen = Str->Len; if (wchars >= 0) { const wchar_t *end = str + wchars; for (const wchar_t *ch = str; ch < end; ch++) { if (!LgiUtf32To8(*ch, o, OutLen)) { *o = 0; break; } } } else { for (const wchar_t *ch = str; *ch; ch++) { if (!LgiUtf32To8(*ch, o, OutLen)) { *o = 0; break; } } } #endif *o = 0; } return true; } /// Equality operator (case sensitive) bool operator ==(const LString &s) { const char *a = Get(); const char *b = s.Get(); if (!a && !b) return true; if (!a || !b) return false; return !strcmp(a, b); } bool operator !=(const LString &s) { return !(*this == s); } // Equality function (default: case insensitive, as the operator== is case sensitive) bool Equals(const char *b, bool CaseInsensitive = true) const { const char *a = Get(); if (!a && !b) return true; if (!a || !b) return false; return !(CaseInsensitive ? _stricmp(a, b) : strcmp(a, b)); } // Equality function (default: case insensitive, as the operator== is case sensitive) bool Equals(const LString &s, bool CaseInsensitive = true) const { const char *a = Get(); const char *b = s.Get(); if (!a && !b) return true; if (!a || !b) return false; return !(CaseInsensitive ? _stricmp(a, b) : strcmp(a, b)); } /// Assignment operator to copy one string to another LString &operator =(const LString &s) { if (this != &s) { Empty(); Str = s.Str; if (Str) Str->Refs++; } return *this; } /// Equality with a C string (case sensitive) bool operator ==(const char *b) { const char *a = Get(); if (!a && !b) return true; if (!a || !b) return false; return !strcmp(a, b); } bool operator !=(const char *b) { return !(*this == b); } /// Assignment operators LString &operator =(const char *s) { if (Str == NULL || s < Str->Str || s > Str->Str + Str->Len) { Empty(); Set(s); } else if (s != Str->Str) { // Special case for setting it to part of itself // If you try and set a string to the start, it's a NOP ptrdiff_t Off = s - Str->Str; memmove(Str->Str, s, Str->Len - Off + 1); Str->Len -= (uint32_t)Off; } return *this; } LString &operator =(const wchar_t *s) { SetW(s); return *this; } LString &operator =(int val) { char n[32]; sprintf_s(n, sizeof(n), "%i", val); Set(n); return *this; } LString &operator =(int64 val) { char n[32]; sprintf_s(n, sizeof(n), "%" PRId64, (int64_t)val); Set(n); return *this; } /// Cast to C string operator operator char *() const { return Str && Str->Len > 0 ? Str->Str : NULL; } int operator -(const LString &s) const { return Stricmp(Get(), s.Get()); } /// Concatenation operator LString operator +(const LString &s) { LString Ret; size_t Len = Length() + s.Length(); if (Ret.Set(NULL, Len)) { char *p = Ret.Get(); if (p) { if (Str) { memcpy(p, Str->Str, Str->Len); p += Str->Len; } if (s.Str) { memcpy(p, s.Str->Str, s.Str->Len); p += s.Str->Len; } *p++ = 0; } } return Ret; } /// Concatenation / assignment operator LString &operator +=(const LString &s) { ssize_t Len = Length() + s.Length(); ssize_t Alloc = sizeof(RefStr) + Len; RefStr *rs = (RefStr*)malloc(Alloc); if (rs) { rs->Refs = 1; rs->Len = Len; #ifdef LGI_UNIT_TESTS RefStrCount++; #endif char *p = rs->Str; if (Str) { memcpy(p, Str->Str, Str->Len); p += Str->Len; } if (s.Str) { memcpy(p, s.Str->Str, s.Str->Len); p += s.Str->Len; } *p++ = 0; assert(p - (char*)rs <= Alloc); Empty(); Str = rs; } return *this; } LString operator *(ssize_t mul) { LString s; if (Str) { s.Length(Str->Len * mul); char *out = s.Get(); for (ssize_t i=0; iLen) memcpy(out, Str->Str, Str->Len); *out = 0; } return s; } /// Gets the length in bytes size_t Length() const { return Str ? Str->Len : 0; } size_t Length(ssize_t NewLen) { if (NewLen < 0) { LAssert(!"No negative string len."); Empty(); } else if (Str) { if (NewLen <= (ssize_t)Str->Len) { Str->Len = NewLen; Str->Str[NewLen] = 0; } else { RefStr *n = (RefStr*)malloc(sizeof(RefStr) + NewLen); if (n) { n->Len = NewLen; n->Refs = 1; memcpy(n->Str, Str->Str, Str->Len); n->Str[Str->Len] = 0; // NULL terminate... Empty(); // Deref the old string... Str = n; } else return 0; } } else { Str = (RefStr*)malloc(sizeof(RefStr) + NewLen); if (Str) { Str->Len = NewLen; Str->Refs = 1; Str->Str[0] = 0; // NULL terminate... } else return 0; } return Str ? Str->Len : 0; } /// Splits the string into parts using a separator Array Split(const char *Sep, int Count = -1, bool CaseSen = false) { Array a; if (Str && Sep) { const char *s = Str->Str, *Prev = s; const char *end = s + Str->Len; size_t SepLen = strlen(Sep); if (s[Str->Len] == 0) { while ((s = CaseSen ? strstr(s, Sep) : Stristr(s, Sep))) { if (s > Prev) a.New().Set(Prev, s - Prev); s += SepLen; Prev = s; if (Count > 0 && a.Length() >= (uint32_t)Count) break; } if (Prev < end) a.New().Set(Prev, end - Prev); a.SetFixedLength(); } else assert(!"String not NULL terminated."); } return a; } /// Splits the string into parts using a separator Array RSplit(const char *Sep, int Count = -1, bool CaseSen = false) { Array a; if (Str && Sep) { const char *s = Get(); size_t SepLen = strlen(Sep); LArray seps; while ((s = CaseSen ? strstr(s, Sep) : Stristr(s, Sep))) { seps.Add(s); s += SepLen; } ssize_t i, Last = seps.Length() - 1; LString p; for (i=Last; i>=0; i--) { const char *part = seps[i] + SepLen; if (i == Last) p.Set(part); else p.Set(part, seps[i+1]-part); a.AddAt(0, p); if (Count > 0 && a.Length() >= (uint32_t)Count) break; } const char *End = seps[i > 0 ? i : 0]; p.Set(Get(), End - Get()); a.AddAt(0, p); } a.SetFixedLength(); return a; } /// Splits the string into parts using delimiter chars Array SplitDelimit(const char *Delimiters = NULL, int Count = -1, bool GroupDelimiters = true) const { Array a; if (Str) { const char *delim = Delimiters ? Delimiters : " \t\r\n"; const char *s = Get(), *end = s + Length(); while (s < end) { // Skip over non-delimiters const char *e = s; while (e < end && !strchr(delim, *e)) e++; if (e > s || !GroupDelimiters) a.New().Set(s, e - s); s = e; if (*s) s++; if (GroupDelimiters) { // Skip any delimiters while (s < end && strchr(delim, *s)) s++; } // Create the string if (Count > 0 && a.Length() >= (uint32_t)Count) break; } if ( s < end || ( !GroupDelimiters && s > Get() && strchr(delim, s[-1]) ) ) a.New().Set(s); } a.SetFixedLength(); return a; } /// Joins an array of strings using a separator LString Join(const LArray &a) { LString ret; if (a.Length() == 0) return ret; char *Sep = Get(); size_t SepLen = Sep ? strlen(Sep) : 0; size_t Bytes = SepLen * (a.Length() - 1); LArray ALen; for (unsigned i=0; iStr; LArray Matches; while ((Match = (CaseSen ? strstr(Match, Old) : Stristr(Match, Old)))) { Matches.Add(Match); if (Count >= 0 && (int)Matches.Length() >= Count) break; Match += OldLen; } size_t NewSize = Str->Len + (Matches.Length() * (NewLen - OldLen)); s.Length((uint32_t)NewSize); char *Out = s.Get(); char *In = Str->Str; // For each match... for (unsigned i=0; iStr + Str->Len; if (In < End) { ptrdiff_t Bytes = End - In; memcpy(Out, In, Bytes); Out += Bytes; } assert(Out - s.Get() == NewSize); // Check we got the size right... *Out = 0; // Null terminate } else { s = *this; } return s; } /// Convert string to double double Float() { return Str ? atof(Str->Str) : NAN; } /// Convert to integer int64 Int(int Base = 10) { if (!Str) return -1; if ( Str->Len > 2 && Str->Str[0] == '0' && ( Str->Str[1] == 'x' || Str->Str[1] == 'X' ) ) { return Atoi(Str->Str+2, 16); } return Atoi(Str->Str, Base); } /// Checks if the string is a number bool IsNumeric() { if (!Str) return false; for (char *s = Str->Str; *s; s++) { if (!IsDigit(*s) && !strchr("e-+.", *s)) return false; } return true; } /// Check for non whitespace bool IsEmpty() { if (!Str) return true; for (char *s = Str->Str; *s; s++) { if (*s != ' ' && *s != '\t' && *s != '\r' && *s != '\n') return false; } return true; } /// Reverses all the characters in the string LString Reverse() { LString s; if (Length() > 0) { s = Str->Str; for (auto *a = s.Get(), *b = s.Get() + s.Length() - 1; a < b; a++, b--) { char t = *a; *a = *b; *b = t; } } return s; } /// Find a sub-string ssize_t Find(const char *needle, ssize_t start = 0, ssize_t end = -1) { if (!needle) return -1; char *c = Get(); if (!c) return -1; char *pos = c + start; while (c < pos) { if (!*c) return -1; c++; } char *found = (end > 0) ? Strnstr(c, needle, end - start) : strstr(c, needle); return (found) ? found - Get() : -1; } /// Reverse find a string (starting from the end) ssize_t RFind(const char *needle, int start = 0, ssize_t end = -1) { if (!needle) return -1; char *c = Get(); if (!c) return -1; char *pos = c + start; while (c < pos) { if (!*c) return -1; c++; } char *found, *prev = NULL; size_t str_len = strlen(needle); while (( found = ( (end > 0) ? Strnstr(c, needle, end - start) : strstr(c, needle) ) )) { prev = found; c = found + str_len; } return (prev) ? prev - Get() : -1; } /// Returns a copy of the string with all the characters converted to lower case LString Lower() { LString s; if (Str && s.Set(Str->Str, Str->Len)) Strlwr(s.Get()); return s; } /// Returns a copy of the string with all the characters converted to upper case LString Upper() { LString s; if (Str && s.Set(Str->Str, Str->Len)) Strupr(s.Get()); return s; } void Swap(LString &s) { LSwap(Str, s.Str); } /// Gets the character at 'index' int operator() (ssize_t index) const { if (!Str) return 0; char *c = Str->Str; if (index < 0) { size_t idx = Str->Len + index; return c[idx]; } else if (index < (int)Str->Len) { return c[index]; } return 0; } /// Gets the string between at 'start' and 'end' (not including the end'th character) LString operator() (ptrdiff_t start, ptrdiff_t end) { LString s; if (Str) { ptrdiff_t start_idx = start < 0 ? Str->Len + start + 1 : start; if (start_idx >= 0 && (uint32_t)start_idx < Str->Len) { ptrdiff_t end_idx = end < 0 ? Str->Len + end + 1 : end; if (end_idx >= start_idx && (uint32_t)end_idx <= Str->Len) s.Set(Str->Str + start_idx, end_idx - start_idx); } } return s; } /// Strip off any leading and trailing characters from 'set' (or whitespace if NULL) LString Strip(const char *set = NULL) { LString ret; _strip(ret, set, true, true); return ret; } /// Strip off any leading characters from 'set' (or whitespace if NULL) LString LStrip(const char *set = NULL) { LString ret; _strip(ret, set, true, false); return ret; } /// Strip off any trailing characters from 'set' (or whitespace if NULL) LString RStrip(const char *set = NULL) { LString ret; _strip(ret, set, false, true); return ret; } /// Prints a formatted string to this object int Printf(const char *Fmt, ...) { Empty(); va_list Arg; va_start(Arg, Fmt); int Bytes = Printf(Arg, Fmt); va_end(Arg); return Bytes; } /// Prints a varargs string int Printf(va_list &Arg, const char *Fmt) { Empty(); return LPrintf(*this, Fmt, Arg); } static LString Escape(const char *In, ssize_t Len = -1, const char *Chars = "\r\n\b\\\'\"") { LString s; if (In && Chars) { char Buf[256]; int Ch = 0; if (Len < 0) Len = strlen(In); while (Len-- > 0) { if (Ch > sizeof(Buf)-4) { // Buffer full, add substring to 's' Buf[Ch] = 0; s += Buf; Ch = 0; } if (strchr(Chars, *In)) { Buf[Ch++] = '\\'; switch (*In) { #undef EscChar #define EscChar(from, to) \ case from: Buf[Ch++] = to; break EscChar('\n', 'n'); EscChar('\r', 'r'); EscChar('\\', '\\'); EscChar('\b', 'b'); EscChar('\a', 'a'); EscChar('\t', 't'); EscChar('\v', 'v'); EscChar('\'', '\''); EscChar('\"', '\"'); EscChar('&', '&'); EscChar('?', '?'); #undef EscChar default: Ch += sprintf_s(Buf+Ch, sizeof(Buf)-Ch, "x%02x", *In); break; } } else Buf[Ch++] = *In; In++; } if (Ch > 0) { Buf[Ch] = 0; s += Buf; } } return s; } template static LString UnEscape(const T *In, ssize_t Len = -1) { if (!In) return LString(); LString s; if (Len < 0) // As memory allocation/copying around data is far slower then // just scanning the string for size... don't try and chunk the // processing. Len = Strlen(In); if (!s.Length(Len)) return LString(); auto *Out = s.Get(); auto *End = In + Len; while (In < End) { if (*In == '\\') { In++; switch (*In) { case 'n': case 'N': *Out++ = '\n'; break; case 'r': case 'R': *Out++ = '\r'; break; case 'b': case 'B': *Out++ = '\b'; break; case 't': case 'T': *Out++ = '\t'; break; default: *Out++ = *In; break; case 0: break; } if (*In) In++; else break; } else *Out++ = *In++; } // Trim excess size off string s.Length(Out - s.Get()); return s; } static LString UnEscape(LString s) { return UnEscape(s.Get(), s.Length()); } #if defined(__GTK_H__) #elif defined(MAC) // && __COREFOUNDATION_CFBASE__ LString(const CFStringRef r) { Str = NULL; *this = r; } LString &operator =(CFStringRef r) { if (r) { CFIndex length = CFStringGetLength(r); CFRange range = CFRangeMake(0, length); CFIndex usedBufLen = 0; CFIndex slen = CFStringGetBytes(r, range, kCFStringEncodingUTF8, '?', false, NULL, 0, &usedBufLen); if (Set(NULL, usedBufLen)) { slen = CFStringGetBytes( r, range, kCFStringEncodingUTF8, '?', false, (UInt8*)Str->Str, Str->Len, &usedBufLen); Str->Str[usedBufLen] = 0; // NULL terminate } } return *this; } CFStringRef CreateStringRef() { char *s = Get(); if (!s) return NULL; return CFStringCreateWithCString(kCFAllocatorDefault, s, kCFStringEncodingUTF8); } #ifdef __OBJC__ NSString *NsStr() { if (Str) return [[NSString alloc] initWithBytes:Str->Str length:Str->Len encoding:NSUTF8StringEncoding]; return nil; } bool operator=(NSString *const s) { *this = [s UTF8String]; return !IsEmpty(); } LString(NSString *const s) { Str = NULL; *this = [s UTF8String]; } #endif #endif }; diff --git a/include/lgi/common/vCard-vCal.h b/include/lgi/common/vCard-vCal.h --- a/include/lgi/common/vCard-vCal.h +++ b/include/lgi/common/vCard-vCal.h @@ -1,110 +1,109 @@ #ifndef _VCARD_VCAL_H #define _VCARD_VCAL_H // #include "ScribeDefs.h" #include "Store3.h" -#include "lgi/common/Token.h" #define FIELD_PERMISSIONS 2000 class VIoPriv; class VIo { public: struct Parameter { LString Field; LString Value; void Set(const char *f, const char *v) { Field = f; Value = v; } }; struct TimeZoneSection { int From, To; LDateTime Start; LString Rule; }; struct TimeZoneInfo { LString Name; TimeZoneSection Normal; TimeZoneSection Daylight; }; class ParamArray : public LArray { public: ParamArray(const char *Init = NULL) { if (Init) { LString s = Init; LString::Array a = s.SplitDelimit(","); for (unsigned i=0; i #include #include #include #include #include "lgi/common/Lgi.h" #include "lgi/common/Html.h" #include "lgi/common/ScrollBar.h" #include "lgi/common/Variant.h" #include "lgi/common/FindReplaceDlg.h" #include "lgi/common/Unicode.h" #include "lgi/common/Emoji.h" #include "lgi/common/ClipBoard.h" #include "lgi/common/Button.h" #include "lgi/common/Edit.h" #include "lgi/common/Combo.h" #include "lgi/common/GdcTools.h" #include "lgi/common/DisplayString.h" #include "lgi/common/Palette.h" #include "lgi/common/Path.h" #include "lgi/common/CssTools.h" #include "lgi/common/LgiRes.h" #include "lgi/common/Net.h" #include "lgi/common/Base64.h" #include "lgi/common/Menu.h" #include "lgi/common/FindReplaceDlg.h" #include "lgi/common/Homoglyphs.h" #include "lgi/common/Charset.h" #include "HtmlPriv.h" #define DEBUG_TABLE_LAYOUT 1 #define DEBUG_DRAW_TD 0 #define DEBUG_RESTYLE 0 #define DEBUG_TAG_BY_POS 0 #define DEBUG_SELECTION 0 #define DEBUG_TEXT_AREA 0 #define ENABLE_IMAGE_RESIZING 1 #define DOCUMENT_LOAD_IMAGES 1 #define MAX_RECURSION_DEPTH 300 #define ALLOW_TABLE_GROWTH 1 #define LGI_HTML_MAXPAINT_TIME 350 // ms #define FLOAT_TOLERANCE 0.001 #define CRASH_TRACE 0 #ifdef MAC #define HTML_USE_DOUBLE_BUFFER 0 #else #define HTML_USE_DOUBLE_BUFFER 1 #endif #define GT_TRANSPARENT 0x00000000 #ifndef IDC_HAND #define IDC_HAND MAKEINTRESOURCE(32649) #endif #undef CellSpacing #define DefaultCellSpacing 0 #define DefaultCellPadding 1 #ifdef MAC #define MinimumPointSize 9 #define MinimumBodyFontSize 12 #else #define MinimumPointSize 8 #define MinimumBodyFontSize 11 #endif // #define DefaultFont "font-family: Times; font-size: 16pt;" #define DefaultBodyMargin "5px" #define DefaultImgSize 16 #define DefaultMissingCellColour GT_TRANSPARENT // Rgb32(0xf0,0xf0,0xf0) #define ShowNbsp 0 #define FontPxHeight(fnt) (fnt->GetHeight() - (int)(fnt->Leading() + 0.5)) #if 0 // def _DEBUG #define DefaultTableBorder Rgb32(0xf8, 0xf8, 0xf8) #else #define DefaultTableBorder GT_TRANSPARENT #endif #if defined(_DEBUG) && DEBUG_TABLE_LAYOUT #define DEBUG_LOG(...) if (Table->Debug) LgiTrace(__VA_ARGS__) #else #define DEBUG_LOG(...) #endif #define IsTableCell(id) ( ((id) == TAG_TD) || ((id) == TAG_TH) ) #define IsTableTag() (TagId == TAG_TABLE || TagId == TAG_TR || TagId == TAG_TD || TagId == TAG_TH) #define GetCssLen(a, b) a().Type == LCss::LenInherit ? b() : a() static char WordDelim[] = ".,<>/?[]{}()*&^%$#@!+|\'\""; static char16 WhiteW[] = {' ', '\t', '\r', '\n', 0}; #if 0 static char DefaultCss[] = { "a { color: blue; text-decoration: underline; }" "body { margin: 8px; }" "strong { font-weight: bolder; }" "pre { font-family: monospace }" "h1 { font-size: 2em; margin: .67em 0px; }" "h2 { font-size: 1.5em; margin: .75em 0px; }" "h3 { font-size: 1.17em; margin: .83em 0px; }" "h4, p," "blockquote, ul," "fieldset, form," "ol, dl, dir," "menu { margin: 1.12em 0px; }" "h5 { font-size: .83em; margin: 1.5em 0px; }" "h6 { font-size: .75em; margin: 1.67em 0px; }" "strike, del { text-decoration: line-through; }" "hr { border: 1px inset; }" "center { text-align: center; }" "h1, h2, h3, h4," "h5, h6, b," "strong { font-weight: bolder; }" }; #endif template void RemoveChars(T *str, T *remove_list) { T *i = str, *o = str, *c; while (*i) { for (c = remove_list; *c; c++) { if (*c == *i) break; } if (*c == 0) *o++ = *i; i++; } *o++ = NULL; } ////////////////////////////////////////////////////////////////////// using namespace Html1; namespace Html1 { class LHtmlPrivate { public: LHashTbl, LTag*> Loading; LHtmlStaticInst Inst; bool CursorVis; LRect CursorPos; LPoint Content; bool WordSelectMode; bool LinkDoubleClick; LAutoString OnLoadAnchor; bool DecodeEmoji; LAutoString EmojiImg; int NextCtrlId; uint64 SetScrollTime; int DeferredLoads; bool IsParsing; bool IsLoaded; bool StyleDirty; // Paint time limits... bool MaxPaintTimeout = false; int MaxPaintTime = LGI_HTML_MAXPAINT_TIME; // Find settings LAutoWString FindText; bool MatchCase; LHtmlPrivate() { IsLoaded = false; StyleDirty = false; IsParsing = false; LinkDoubleClick = true; WordSelectMode = false; NextCtrlId = 2000; SetScrollTime = 0; CursorVis = false; CursorPos.ZOff(-1, -1); DeferredLoads = 0; char EmojiPng[MAX_PATH_LEN]; #ifdef MAC LMakePath(EmojiPng, sizeof(EmojiPng), LGetExeFile(), "Contents/Resources/Emoji.png"); #else LGetSystemPath(LSP_APP_INSTALL, EmojiPng, sizeof(EmojiPng)); LMakePath(EmojiPng, sizeof(EmojiPng), EmojiPng, "resources/emoji.png"); #endif if (LFileExists(EmojiPng)) { DecodeEmoji = true; EmojiImg.Reset(NewStr(EmojiPng)); } else DecodeEmoji = false; } ~LHtmlPrivate() { } }; class InputButton : public LButton { LTag *Tag; public: InputButton(LTag *tag, int Id, const char *Label) : LButton(Id, 0, 0, -1, -1, Label) { Tag = tag; } void OnClick(const LMouse &m) { Tag->OnClick(m); } }; class LFontCache { LHtml *Owner; List Fonts; public: LFontCache(LHtml *owner) { Owner = owner; } ~LFontCache() { Fonts.DeleteObjects(); } LFont *FontAt(int i) { return Fonts.ItemAt(i); } LFont *FindMatch(LFont *m) { for (auto f: Fonts) { if (*f == *m) { return f; } } return 0; } LFont *GetFont(LCss *Style) { if (!Style) return NULL; LFont *Default = Owner->GetFont(); LCss::StringsDef Face = Style->FontFamily(); if (Face.Length() < 1 || !ValidStr(Face[0])) { Face.Empty(); const char *DefFace = Default->Face(); LAssert(ValidStr(DefFace)); Face.Add(NewStr(DefFace)); } LAssert(ValidStr(Face[0])); LCss::Len Size = Style->FontSize(); LCss::FontWeightType Weight = Style->FontWeight(); bool IsBold = Weight == LCss::FontWeightBold || Weight == LCss::FontWeightBolder || Weight > LCss::FontWeight400; bool IsItalic = Style->FontStyle() == LCss::FontStyleItalic; bool IsUnderline = Style->TextDecoration() == LCss::TextDecorUnderline; if (Size.Type == LCss::LenInherit || Size.Type == LCss::LenNormal) { Size.Type = LCss::LenPt; Size.Value = (float)Default->PointSize(); } auto Scale = Owner->GetDpiScale(); if (Size.Type == LCss::LenPx) { Size.Value *= (float) Scale.y; int RequestPx = (int) Size.Value; // Look for cached fonts of the right size... for (auto f: Fonts) { if (f->Face() && _stricmp(f->Face(), Face[0]) == 0 && f->Bold() == IsBold && f->Italic() == IsItalic && f->Underline() == IsUnderline) { int Px = FontPxHeight(f); int Diff = Px - RequestPx; if (Diff >= 0 && Diff <= 2) return f; } } } else if (Size.Type == LCss::LenPt) { double Pt = Size.Value; for (auto f: Fonts) { if (!f->Face() || Face.Length() == 0) { LAssert(0); break; } auto FntSz = f->Size(); if (f->Face() && _stricmp(f->Face(), Face[0]) == 0 && FntSz.Type == LCss::LenPt && std::abs(FntSz.Value - Pt) < FLOAT_TOLERANCE && f->Bold() == IsBold && f->Italic() == IsItalic && f->Underline() == IsUnderline) { // Return cached font return f; } } } else if (Size.Type == LCss::LenPercent) { // Most of the percentages will be resolved in the "Apply" stage // of the CSS calculations, any that appear here have no "font-size" // in their parent tree, so we just use the default font size times // the requested percent Size.Type = LCss::LenPt; Size.Value *= Default->PointSize() / 100.0f; if (Size.Value < MinimumPointSize) Size.Value = MinimumPointSize; } else if (Size.Type == LCss::LenEm) { // Most of the relative sizes will be resolved in the "Apply" stage // of the CSS calculations, any that appear here have no "font-size" // in their parent tree, so we just use the default font size times // the requested percent Size.Type = LCss::LenPt; Size.Value *= Default->PointSize(); if (Size.Value < MinimumPointSize) Size.Value = MinimumPointSize; } else if (Size.Type == LCss::SizeXXSmall || Size.Type == LCss::SizeXSmall || Size.Type == LCss::SizeSmall || Size.Type == LCss::SizeMedium || Size.Type == LCss::SizeLarge || Size.Type == LCss::SizeXLarge || Size.Type == LCss::SizeXXLarge) { int Idx = Size.Type-LCss::SizeXXSmall; LAssert(Idx >= 0 && Idx < CountOf(LCss::FontSizeTable)); Size.Type = LCss::LenPt; Size.Value = Default->PointSize() * LCss::FontSizeTable[Idx]; if (Size.Value < MinimumPointSize) Size.Value = MinimumPointSize; } else if (Size.Type == LCss::SizeSmaller) { Size.Type = LCss::LenPt; Size.Value = (float)(Default->PointSize() - 1); } else if (Size.Type == LCss::SizeLarger) { Size.Type = LCss::LenPt; Size.Value = (float)(Default->PointSize() + 1); } else LAssert(!"Not impl."); LFont *f; if ((f = new LFont)) { auto ff = ValidStr(Face[0]) ? Face[0] : Default->Face(); f->Face(ff); f->Size(Size.IsValid() ? Size : Default->Size()); f->Bold(IsBold); f->Italic(IsItalic); f->Underline(IsUnderline); // printf("Add cache font %s,%i %i,%i,%i\n", f->Face(), f->PointSize(), f->Bold(), f->Italic(), f->Underline()); if (std::abs(Size.Value) < FLOAT_TOLERANCE) ; else if (!f->Create((char*)0, 0)) { // Broken font... f->Face(Default->Face()); LFont *DefMatch = FindMatch(f); // printf("Falling back to default face for '%s:%i', DefMatch=%p\n", ff, f->PointSize(), DefMatch); if (DefMatch) { DeleteObj(f); return DefMatch; } else { if (!f->Create((char*)0, 0)) { DeleteObj(f); return Fonts[0]; } } } // Not already cached Fonts.Insert(f); if (!f->Face()) { LAssert(0); } return f; } return 0; } }; class LFlowRegion { LCss::LengthType Align = LCss::LenInherit; List Line; // These pointers aren't owned by the flow region // When the line is finish, all the tag regions // will need to be vertically aligned struct LFlowStack { int LeftAbs; int RightAbs; int TopAbs; }; LArray Stack; public: LHtml *Html; int x1, x2; // Left and right margins int y1; // Current y position int y2; // Maximum used y position int cx; // Current insertion point int my; // How much of the area above y2 was just margin LPoint MAX; // Max dimensions int Inline; int InBody; LFlowRegion(LHtml *html, bool inbody) { Html = html; x1 = x2 = y1 = y2 = cx = my = 0; Inline = 0; InBody = inbody; } LFlowRegion(LHtml *html, LRect r, bool inbody) { Html = html; MAX.x = cx = x1 = r.x1; MAX.y = y1 = y2 = r.y1; x2 = r.x2; my = 0; Inline = 0; InBody = inbody; } LFlowRegion(LFlowRegion &r) { Html = r.Html; x1 = r.x1; x2 = r.x2; y1 = r.y1; MAX.x = cx = r.cx; MAX.y = y2 = r.y2; my = r.my; Inline = r.Inline; InBody = r.InBody; } LString ToString() { LString s; s.Printf("Flow: x=%i(%i)%i y=%i,%i my=%i inline=%i", x1, cx, x2, y1, y2, my, Inline); return s; } int X() { return x2 - cx; } void X(int newx) { x2 = x1 + newx - 1; } int Width() { return x2 - x1 + 1; } LFlowRegion &operator +=(LRect r) { x1 += r.x1; cx += r.x1; x2 -= r.x2; y1 += r.y1; y2 += r.y1; return *this; } LFlowRegion &operator -=(LRect r) { x1 -= r.x1; cx -= r.x1; x2 += r.x2; y1 += r.y2; y2 += r.y2; return *this; } void AlignText(); void FinishLine(bool Margin = false); void EndBlock(); void Insert(LFlowRect *Tr, LCss::LengthType Align); LRect *LineBounds(); void Indent(LTag *Tag, LCss::Len Left, LCss::Len Top, LCss::Len Right, LCss::Len Bottom, bool IsMargin) { LFlowRegion This(*this); LFlowStack &Fs = Stack.New(); Fs.LeftAbs = Left.IsValid() ? ResolveX(Left, Tag, IsMargin) : 0; Fs.RightAbs = Right.IsValid() ? ResolveX(Right, Tag, IsMargin) : 0; Fs.TopAbs = Top.IsValid() ? ResolveY(Top, Tag, IsMargin) : 0; x1 += Fs.LeftAbs; cx += Fs.LeftAbs; x2 -= Fs.RightAbs; y1 += Fs.TopAbs; y2 += Fs.TopAbs; if (IsMargin) my += Fs.TopAbs; } void Indent(LRect &Px, bool IsMargin) { LFlowRegion This(*this); LFlowStack &Fs = Stack.New(); Fs.LeftAbs = Px.x1; Fs.RightAbs = Px.x2; Fs.TopAbs = Px.y1; x1 += Fs.LeftAbs; cx += Fs.LeftAbs; x2 -= Fs.RightAbs; y1 += Fs.TopAbs; y2 += Fs.TopAbs; if (IsMargin) my += Fs.TopAbs; } void Outdent(LRect &Px, bool IsMargin) { LFlowRegion This = *this; ssize_t len = Stack.Length(); if (len > 0) { LFlowStack &Fs = Stack[len-1]; int &BottomAbs = Px.y2; x1 -= Fs.LeftAbs; cx -= Fs.LeftAbs; x2 += Fs.RightAbs; y2 += BottomAbs; if (IsMargin) my += BottomAbs; Stack.Length(len-1); } else LAssert(!"Nothing to pop."); } void Outdent(LTag *Tag, LCss::Len Left, LCss::Len Top, LCss::Len Right, LCss::Len Bottom, bool IsMargin) { LFlowRegion This = *this; ssize_t len = Stack.Length(); if (len > 0) { LFlowStack &Fs = Stack[len-1]; int BottomAbs = Bottom.IsValid() ? ResolveY(Bottom, Tag, IsMargin) : 0; x1 -= Fs.LeftAbs; cx -= Fs.LeftAbs; x2 += Fs.RightAbs; y2 += BottomAbs; if (IsMargin) my += BottomAbs; Stack.Length(len-1); } else LAssert(!"Nothing to pop."); } int ResolveX(LCss::Len l, LTag *t, bool IsMargin) { LFont *f = t->GetFont(); switch (l.Type) { default: case LCss::LenInherit: return IsMargin ? 0 : X(); case LCss::LenPx: // return MIN((int)l.Value, X()); return (int)l.Value; case LCss::LenPt: return (int) (l.Value * LScreenDpi().x / 72.0); case LCss::LenCm: return (int) (l.Value * LScreenDpi().x / 2.54); case LCss::LenEm: { if (!f) { LAssert(!"No font?"); f = LSysFont; } return (int)(l.Value * f->GetHeight()); } case LCss::LenEx: { if (!f) { LAssert(!"No font?"); f = LSysFont; } return (int) (l.Value * f->GetHeight() / 2); // More haha, who uses 'ex' anyway? } case LCss::LenPercent: { int my_x = X(); int px = (int) (l.Value * my_x / 100.0); return px; } case LCss::LenAuto: { if (IsMargin) return 0; else return X(); break; } case LCss::SizeSmall: { return 1; // px } case LCss::SizeMedium: { return 2; // px } case LCss::SizeLarge: { return 3; // px } } return 0; } bool LimitX(int &x, LCss::Len Min, LCss::Len Max, LFont *f) { bool Limited = false; if (Min.IsValid()) { int Px = Min.ToPx(x2 - x1 + 1, f, false); if (Px > x) { x = Px; Limited = true; } } if (Max.IsValid()) { int Px = Max.ToPx(x2 - x1 + 1, f, false); if (Px < x) { x = Px; Limited = true; } } return Limited; } int ResolveY(LCss::Len l, LTag *t, bool IsMargin) { LFont *f = t->GetFont(); switch (l.Type) { case LCss::LenInherit: case LCss::LenAuto: case LCss::LenNormal: case LCss::LenPx: return (int)l.Value; case LCss::LenPt: return (int) (l.Value * LScreenDpi().y / 72.0); case LCss::LenCm: return (int) (l.Value * LScreenDpi().y / 2.54); case LCss::LenEm: { if (!f) { f = LSysFont; LAssert(!"No font"); } return (int) (l.Value * f->GetHeight()); } case LCss::LenEx: { if (!f) { f = LSysFont; LAssert(!"No font"); } return (int) (l.Value * f->GetHeight() / 2); // More haha, who uses 'ex' anyway? } case LCss::LenPercent: { // Walk up tree of tags to find an absolute size... LCss::Len Ab; for (LTag *p = ToTag(t->Parent); p; p = ToTag(p->Parent)) { auto h = p->Height(); if (h.IsValid() && !h.IsDynamic()) { Ab = h; break; } } if (!Ab.IsValid()) { LAssert(Html != NULL); Ab.Type = LCss::LenPx; Ab.Value = (float)Html->Y(); } LCss::Len m = Ab * l; return (int)m.ToPx(0, f);; } case LCss::SizeSmall: { return 1; // px } case LCss::SizeMedium: { return 2; // px } case LCss::SizeLarge: { return 3; // px } case LCss::AlignLeft: case LCss::AlignRight: case LCss::AlignCenter: case LCss::AlignJustify: case LCss::VerticalBaseline: case LCss::VerticalSub: case LCss::VerticalSuper: case LCss::VerticalTop: case LCss::VerticalTextTop: case LCss::VerticalMiddle: case LCss::VerticalBottom: case LCss::VerticalTextBottom: { // Meaningless in this context break; } default: { LAssert(!"Not supported."); break; } } return 0; } bool LimitY(int &y, LCss::Len Min, LCss::Len Max, LFont *f) { bool Limited = false; int TotalY = Html ? Html->Y() : 0; if (Min.IsValid()) { int Px = Min.ToPx(TotalY, f, false); if (Px > y) { y = Px; Limited = true; } } if (Max.IsValid()) { int Px = Max.ToPx(TotalY, f, false); if (Px < y) { y = Px; Limited = true; } } return Limited; } LRect ResolveMargin(LCss *Src, LTag *Tag) { LRect r; r.x1 = ResolveX(Src->MarginLeft(), Tag, true); r.y1 = ResolveY(Src->MarginTop(), Tag, true); r.x2 = ResolveX(Src->MarginRight(), Tag, true); r.y2 = ResolveY(Src->MarginBottom(), Tag, true); return r; } LRect ResolveBorder(LCss *Src, LTag *Tag) { LRect r; r.x1 = ResolveX(Src->BorderLeft(), Tag, true); r.y1 = ResolveY(Src->BorderTop(), Tag, true); r.x2 = ResolveX(Src->BorderRight(), Tag, true); r.y2 = ResolveY(Src->BorderBottom(), Tag, true); return r; } LRect ResolvePadding(LCss *Src, LTag *Tag) { LRect r; r.x1 = ResolveX(Src->PaddingLeft(), Tag, true); r.y1 = ResolveY(Src->PaddingTop(), Tag, true); r.x2 = ResolveX(Src->PaddingRight(), Tag, true); r.y2 = ResolveY(Src->PaddingBottom(), Tag, true); return r; } }; }; ////////////////////////////////////////////////////////////////////// static bool ParseDistance(char *s, float &d, char *units = 0) { if (!s) return false; while (*s && IsWhiteSpace(*s)) s++; if (!IsDigit(*s) && !strchr("-.", *s)) return false; d = (float)atof(s); while (*s && (IsDigit(*s) || strchr("-.", *s))) s++; while (*s && IsWhiteSpace(*s)) s++; char _units[128]; char *o = units = units ? units : _units; while (*s && (IsAlpha(*s) || *s == '%')) { *o++ = *s++; } *o++ = 0; return true; } LHtmlLength::LHtmlLength() { d = 0; PrevAbs = 0; u = LCss::LenInherit; } LHtmlLength::LHtmlLength(char *s) { Set(s); } bool LHtmlLength::IsValid() { return u != LCss::LenInherit; } bool LHtmlLength::IsDynamic() { return u == LCss::LenPercent || d == 0.0; } LHtmlLength::operator float () { return d; } LHtmlLength &LHtmlLength::operator =(float val) { d = val; u = LCss::LenPx; return *this; } LCss::LengthType LHtmlLength::GetUnits() { return u; } void LHtmlLength::Set(char *s) { if (ValidStr(s)) { char Units[256] = ""; if (ParseDistance(s, d, Units)) { if (Units[0]) { if (strchr(Units, '%')) { u = LCss::LenPercent; } else if (stristr(Units, "pt")) { u = LCss::LenPt; } else if (stristr(Units, "em")) { u = LCss::LenEm; } else if (stristr(Units, "ex")) { u = LCss::LenEx; } else { u = LCss::LenPx; } } else { u = LCss::LenPx; } } } } float LHtmlLength::Get(LFlowRegion *Flow, LFont *Font, bool Lock) { switch (u) { default: break; case LCss::LenEm: { return PrevAbs = d * (Font ? Font->GetHeight() : 14); break; } case LCss::LenEx: { return PrevAbs = (Font ? Font->GetHeight() * d : 14) / 2; break; } case LCss::LenPercent: { if (Lock || PrevAbs == 0.0) { return PrevAbs = (Flow->X() * d / 100); } else { return PrevAbs; } break; } } float FlowX = Flow ? Flow->X() : d; return PrevAbs = MIN(FlowX, d); } LHtmlLine::LHtmlLine() { LineStyle = -1; LineReset = 0x80000000; } LHtmlLine::~LHtmlLine() { } LHtmlLine &LHtmlLine::operator =(int i) { d = (float)i; return *this; } void LHtmlLine::Set(char *s) { - LToken t(s, " \t"); + auto t = LString(s).SplitDelimit(" \t"); LineReset = 0x80000000; LineStyle = -1; char *Style = 0; for (unsigned i=0; iColourMap.Find(c) ) { LHtmlParser::ParseColour(c, Colour); } else if (_strnicmp(c, "rgb(", 4) == 0) { char Buf[256]; strcpy_s(Buf, sizeof(Buf), c); while (!strchr(c, ')') && (c = t[++i])) { strcat(Buf, c); } LHtmlParser::ParseColour(Buf, Colour); } else if (IsDigit(*c)) { LHtmlLength::Set(c); } else if (_stricmp(c, "none") == 0) { Style = 0; } else if ( _stricmp(c, "dotted") == 0 || _stricmp(c, "dashed") == 0 || _stricmp(c, "solid") == 0 || _stricmp(c, "float") == 0 || _stricmp(c, "groove") == 0 || _stricmp(c, "ridge") == 0 || _stricmp(c, "inset") == 0 || _stricmp(c, "outse") == 0) { Style = c; } else { // ??? } } if (Style && _stricmp(Style, "dotted") == 0) { switch ((int)d) { case 2: { LineStyle = 0xcccccccc; break; } case 3: { LineStyle = 0xe38e38; LineReset = 0x800000; break; } case 4: { LineStyle = 0xf0f0f0f0; break; } case 5: { LineStyle = 0xf83e0; LineReset = 0x80000; break; } case 6: { LineStyle = 0xfc0fc0; LineReset = 0x800000; break; } case 7: { LineStyle = 0xfe03f80; LineReset = 0x8000000; break; } case 8: { LineStyle = 0xff00ff00; break; } case 9: { LineStyle = 0x3fe00; LineReset = 0x20000; break; } default: { LineStyle = 0xaaaaaaaa; break; } } } } ////////////////////////////////////////////////////////////////////// LRect LTag::GetRect(bool Client) { LRect r(Pos.x, Pos.y, Pos.x + Size.x - 1, Pos.y + Size.y - 1); if (!Client) { for (LTag *p = ToTag(Parent); p; p=ToTag(p->Parent)) { r.Offset(p->Pos.x, p->Pos.y); } } return r; } LCss::LengthType LTag::GetAlign(bool x) { for (LTag *t = this; t; t = ToTag(t->Parent)) { LCss::Len l; if (x) { if (IsTableCell(TagId) && Cell && Cell->XAlign) l.Type = Cell->XAlign; else l = t->TextAlign(); } else { l = t->VerticalAlign(); } if (l.Type != LenInherit) { return l.Type; } if (t->TagId == TAG_TABLE) break; } return LenInherit; } ////////////////////////////////////////////////////////////////////// void LFlowRegion::EndBlock() { if (cx > x1) FinishLine(); } void LFlowRegion::AlignText() { if (Align != LCss::AlignLeft) { int Used = 0; for (auto l : Line) Used += l->X(); int Total = x2 - x1 + 1; if (Used < Total) { int Offset = 0; if (Align == LCss::AlignCenter) Offset = (Total - Used) / 2; else if (Align == LCss::AlignRight) Offset = Total - Used; if (Offset) for (auto l : Line) { if (l->Tag->Display() != LCss::DispInlineBlock) l->Offset(Offset, 0); } } } } void LFlowRegion::FinishLine(bool Margin) { // AlignText(); if (y2 > y1) { my = Margin ? y2 - y1 : 0; y1 = y2; } else { int fy = Html->DefFont()->GetHeight(); my = Margin ? fy : 0; y1 += fy; } cx = x1; y2 = y1; Line.Empty(); } LRect *LFlowRegion::LineBounds() { auto It = Line.begin(); LFlowRect *Prev = *It; LFlowRect *r=Prev; if (r) { LRect b; b = *r; int Ox = r->Tag->AbsX(); int Oy = r->Tag->AbsY(); b.Offset(Ox, Oy); // int Ox = 0, Oy = 0; while ((r = *(++It) )) { LRect c = *r; Ox = r->Tag->AbsX(); Oy = r->Tag->AbsY(); c.Offset(Ox, Oy); /* Ox += r->Tag->Pos.x - Prev->Tag->Pos.x; Oy += r->Tag->Pos.y - Prev->Tag->Pos.y; c.Offset(Ox, Oy); */ b.Union(&c); Prev = r; } static LRect Rgn; Rgn = b; return &Rgn; } return 0; } void LFlowRegion::Insert(LFlowRect *Tr, LCss::LengthType align) { if (Tr) { Align = align; Line.Insert(Tr); } } ////////////////////////////////////////////////////////////////////// LTag::LTag(LHtml *h, LHtmlElement *p) : LHtmlElement(p), Attr(8) { Display(DispInline); Html = h; } LTag::~LTag() { if (Html->Cursor == this) { Html->Cursor = 0; } if (Html->Selection == this) { Html->Selection = 0; } DeleteObj(Ctrl); Attr.DeleteArrays(); DeleteObj(Cell); } void LTag::OnChange(PropType Prop) { } bool LTag::OnClick(const LMouse &m) { if (!Html->Environment) return false; const char *OnClick = NULL; if (Get("onclick", OnClick)) { Html->Environment->OnExecuteScript(Html, (char*)OnClick); } else { OnNotify(LNotification(m)); } return true; } void LTag::Set(const char *attr, const char *val) { char *existing = Attr.Find(attr); if (existing) DeleteArray(existing); if (val) Attr.Add(attr, NewStr(val)); } bool LTag::GetVariant(const char *Name, LVariant &Value, const char *Array) { LDomProperty Fld = LStringToDomProp(Name); switch (Fld) { case ObjStyle: // Type: LCssStyle { Value = &StyleDom; return true; } case ObjTextContent: // Type: String { Value = Text(); return true; } default: { char *a = Attr.Find(Name); if (a) { Value = a; return true; } break; } } return false; } bool LTag::SetVariant(const char *Name, LVariant &Value, const char *Array) { LDomProperty Fld = LStringToDomProp(Name); switch (Fld) { case ObjStyle: { const char *Defs = Value.Str(); if (!Defs) return false; return Parse(Defs, ParseRelaxed); } case ObjTextContent: { const char *s = Value.Str(); if (s) { LAutoWString w(CleanText(s, strlen(s), "utf-8", true, true)); Txt = w; return true; } break; } case ObjInnerHtml: // Type: String { // Clear out existing tags.. Children.DeleteObjects(); char *Doc = Value.CastString(); if (Doc) { // Create new tags... bool BackOut = false; while (Doc && *Doc) { LTag *t = new LTag(Html, this); if (t) { Doc = Html->ParseHtml(t, Doc, 1, false, &BackOut); if (!Doc) break; } else break; } } else return false; break; } default: { Set(Name, Value.CastString()); SetStyle(); break; } } Html->ViewWidth = -1; return true; } ssize_t LTag::GetTextStart() { if (PreText() && TextPos.Length() > 1) { LFlowRect *t = TextPos[1]; if (t) return t->Text - Text(); } else if (TextPos.Length() > 0) { LFlowRect *t = TextPos[0]; if (t && Text()) { LAssert(t->Text >= Text() && t->Text <= Text()+2); return t->Text - Text(); } } return 0; } static bool TextToStream(LStream &Out, char16 *Text) { if (!Text) return true; uint8_t Buf[256]; uint8_t *s = Buf; ssize_t Len = sizeof(Buf); while (*Text) { #define WriteExistingContent() \ if (s > Buf) \ Out.Write(Buf, (int)(s - Buf)); \ s = Buf; \ Len = sizeof(Buf); \ Buf[0] = 0; if (*Text == '<' || *Text == '>') { WriteExistingContent(); Out.Print("&%ct;", *Text == '<' ? 'l' : 'g'); } else if (*Text == 0xa0) { WriteExistingContent(); Out.Write((char*)" ", 6); } else { LgiUtf32To8(*Text, s, Len); if (Len < 16) { WriteExistingContent(); } } Text++; } if (s > Buf) Out.Write(Buf, s - Buf); return true; } bool LTag::CreateSource(LStringPipe &p, int Depth, bool LastWasBlock) { char *Tabs = new char[Depth+1]; memset(Tabs, '\t', Depth); Tabs[Depth] = 0; if (ValidStr(Tag)) { if (IsBlock()) { p.Print("%s%s<%s", TagId != TAG_HTML ? "\n" : "", Tabs, Tag.Get()); } else { p.Print("<%s", Tag.Get()); } if (Attr.Length()) { // const char *a; // for (char *v = Attr.First(&a); v; v = Attr.Next(&a)) for (auto v : Attr) { if (_stricmp(v.key, "style")) p.Print(" %s=\"%s\"", v.key, v.value); } } if (Props.Length()) { LCss *Css = this; LCss Tmp; #define DelProp(p) \ if (Css == this) { Tmp = *Css; Css = &Tmp; } \ Css->DeleteProp(p); // Clean out any default CSS properties where we can... LHtmlElemInfo *i = LHtmlStatic::Inst->GetTagInfo(Tag); if (i) { if (Props.Find(PropDisplay) && ( (!i->Block() && Display() == DispInline) || (i->Block() && Display() == DispBlock) )) { DelProp(PropDisplay); } switch (TagId) { default: break; case TAG_A: { LCss::ColorDef Blue(LCss::ColorRgb, Rgb32(0, 0, 255)); if (Props.Find(PropColor) && Color() == Blue) DelProp(PropColor); if (Props.Find(PropTextDecoration) && TextDecoration() == LCss::TextDecorUnderline) DelProp(PropTextDecoration) break; } case TAG_BODY: { LCss::Len FivePx(LCss::LenPx, 5.0f); if (Props.Find(PropPaddingLeft) && PaddingLeft() == FivePx) DelProp(PropPaddingLeft) if (Props.Find(PropPaddingTop) && PaddingTop() == FivePx) DelProp(PropPaddingTop) if (Props.Find(PropPaddingRight) && PaddingRight() == FivePx) DelProp(PropPaddingRight) break; } case TAG_B: { if (Props.Find(PropFontWeight) && FontWeight() == LCss::FontWeightBold) DelProp(PropFontWeight); break; } case TAG_U: { if (Props.Find(PropTextDecoration) && TextDecoration() == LCss::TextDecorUnderline) DelProp(PropTextDecoration); break; } case TAG_I: { if (Props.Find(PropFontStyle) && FontStyle() == LCss::FontStyleItalic) DelProp(PropFontStyle); break; } } } // Convert CSS props to a string and emit them... auto s = Css->ToString(); if (ValidStr(s)) { // Clean off any trailing whitespace... char *e = s ? s + strlen(s) : NULL; while (e && strchr(WhiteSpace, e[-1])) *--e = 0; // Print them to the tags attributes... p.Print(" style=\"%s\"", s.Get()); } } } if (Children.Length() || TagId == TAG_STYLE) // { if (Tag) { p.Write((char*)">", 1); TextToStream(p, Text()); } bool Last = IsBlock(); for (unsigned i=0; iCreateSource(p, Parent ? Depth+1 : 0, Last); Last = c->IsBlock(); } if (Tag) { if (IsBlock()) { if (Children.Length()) p.Print("\n%s", Tabs); } p.Print("", Tag.Get()); } } else if (Tag) { if (Text()) { p.Write((char*)">", 1); TextToStream(p, Text()); p.Print("", Tag.Get()); } else { p.Print("/>\n"); } } else { TextToStream(p, Text()); } DeleteArray(Tabs); return true; } void LTag::SetTag(const char *NewTag) { Tag.Reset(NewStr(NewTag)); if (NewTag) { Info = Html->GetTagInfo(Tag); if (Info) { TagId = Info->Id; Display(Info->Flags & LHtmlElemInfo::TI_BLOCK ? LCss::DispBlock : LCss::DispInline); } } else { Info = NULL; TagId = CONTENT; } SetStyle(); } LColour LTag::_Colour(bool f) { for (LTag *t = this; t; t = ToTag(t->Parent)) { ColorDef c = f ? t->Color() : t->BackgroundColor(); if (c.Type != ColorInherit) { return LColour(c.Rgb32, 32); } #if 1 if (!f && t->TagId == TAG_TABLE) break; #else /* This implements some basic level of colour inheritance for background colours. See test case 'cisra-cqs.html'. */ if (!f && t->TagId == TAG_TABLE) break; #endif } return LColour(); } void LTag::CopyClipboard(LMemQueue &p, bool &InSelection) { ssize_t Min = -1; ssize_t Max = -1; if (Cursor >= 0 && Selection >= 0) { Min = MIN(Cursor, Selection); Max = MAX(Cursor, Selection); } else if (InSelection) { Max = MAX(Cursor, Selection); } else { Min = MAX(Cursor, Selection); } ssize_t Off = -1; ssize_t Chars = 0; auto Start = GetTextStart(); auto t = Text() + Start; if (Min >= 0 && Max >= 0) { Off = Min + Start; Chars = Max - Min; } else if (Min >= 0) { Off = Min + Start; Chars = StrlenW(t) - Min; InSelection = true; } else if (Max >= 0) { Off = Start; Chars = Max; InSelection = false; } else if (InSelection) { Off = Start; Chars = StrlenW(t); } if (Off >= 0 && Chars > 0) { #ifdef _DEBUG for (int i=0; iCopyClipboard(p, InSelection); } } static char* _DumpColour(LCss::ColorDef c) { static char Buf[4][32]; #ifdef _MSC_VER static LONG Cur = 0; LONG Idx = InterlockedIncrement(&Cur); #else static int Cur = 0; int Idx = __sync_fetch_and_add(&Cur, 1); #endif char *b = Buf[Idx % 4]; if (c.Type == LCss::ColorInherit) strcpy_s(b, 32, "Inherit"); else sprintf_s(b, 32, "%2.2x,%2.2x,%2.2x(%2.2x)", R32(c.Rgb32),G32(c.Rgb32),B32(c.Rgb32),A32(c.Rgb32)); return b; } void LTag::_Dump(LStringPipe &Buf, int Depth) { LString Tabs; Tabs.Set(NULL, Depth); memset(Tabs.Get(), '\t', Depth); const char *Empty = ""; char *ElementName = TagId == CONTENT ? (char*)"Content" : (TagId == ROOT ? (char*)"Root" : Tag); Buf.Print( "%s%s(%p)%s%s%s (%i) Pos=%i,%i Size=%i,%i Color=%s/%s", Tabs.Get(), ElementName, this, HtmlId ? "#" : Empty, HtmlId ? HtmlId : Empty, #ifdef _DEBUG Debug ? " debug" : Empty, #else Empty, #endif WasClosed, Pos.x, Pos.y, Size.x, Size.y, _DumpColour(Color()), _DumpColour(BackgroundColor())); for (unsigned i=0; iText, Tr->Len)); if (Utf8) { size_t Len = strlen(Utf8); if (Len > 40) { Utf8[40] = 0; } } else if (Tr->Text) { Utf8.Reset(NewStr("")); } Buf.Print("Tr(%i,%i %ix%i '%s') ", Tr->x1, Tr->y1, Tr->X(), Tr->Y(), Utf8.Get()); } Buf.Print("\r\n"); for (unsigned i=0; i_Dump(Buf, Depth+1); } if (Children.Length()) { Buf.Print("%s/%s\r\n", Tabs.Get(), ElementName); } } LAutoWString LTag::DumpW() { LStringPipe Buf; // Buf.Print("Html pos=%s\n", Html?Html->GetPos().GetStr():0); _Dump(Buf, 0); LAutoString a(Buf.NewStr()); LAutoWString w(Utf8ToWide(a)); return w; } LAutoString LTag::DescribeElement() { LStringPipe s(256); s.Print("%s", Tag ? Tag.Get() : "CONTENT"); if (HtmlId) s.Print("#%s", HtmlId); for (unsigned i=0; iDefFont(); } return f; } LFont *LTag::GetFont() { if (!Font) { if (PropAddress(PropFontFamily) != 0 || FontSize().Type != LenInherit || FontStyle() != FontStyleInherit || FontVariant() != FontVariantInherit || FontWeight() != FontWeightInherit || TextDecoration() != TextDecorInherit) { LCss c; LCss::PropMap Map; Map.Add(PropFontFamily, new LCss::PropArray); Map.Add(PropFontSize, new LCss::PropArray); Map.Add(PropFontStyle, new LCss::PropArray); Map.Add(PropFontVariant, new LCss::PropArray); Map.Add(PropFontWeight, new LCss::PropArray); Map.Add(PropTextDecoration, new LCss::PropArray); for (LTag *t = this; t; t = ToTag(t->Parent)) { if (t->TagId == TAG_IFRAME) break; if (!c.InheritCollect(*t, Map)) break; } c.InheritResolve(Map); Map.DeleteObjects(); if ((Font = Html->FontCache->GetFont(&c))) return Font; } else { LTag *t = this; while (!t->Font && t->Parent) { t = ToTag(t->Parent); } if (t->Font) return t->Font; } Font = Html->DefFont(); } return Font; } LTag *LTag::PrevTag() { if (Parent) { ssize_t i = Parent->Children.IndexOf(this); if (i >= 0) { return ToTag(Parent->Children[i - 1]); } } return 0; } void LTag::Invalidate() { LRect p = GetRect(); for (LTag *t=ToTag(Parent); t; t=ToTag(t->Parent)) { p.Offset(t->Pos.x, t->Pos.y); } Html->Invalidate(&p); } LTag *LTag::IsAnchor(LString *Uri) { LTag *a = 0; for (LTag *t = this; t; t = ToTag(t->Parent)) { if (t->TagId == TAG_A) { a = t; break; } } if (a && Uri) { const char *u = 0; if (a->Get("href", u)) { LAutoWString w(CleanText(u, strlen(u), "utf-8")); if (w) { *Uri = w; } } } return a; } bool LTag::OnMouseClick(LMouse &m) { bool Processed = false; if (m.IsContextMenu()) { LString Uri; const char *ImgSrc = NULL; LTag *a = IsAnchor(&Uri); bool IsImg = TagId == TAG_IMG; if (IsImg) Get("src", ImgSrc); bool IsAnchor = a && ValidStr(Uri); if (IsAnchor || IsImg) { LSubMenu RClick; #define IDM_COPY_LINK 100 #define IDM_COPY_IMG 101 if (Html->GetMouse(m, true)) { int Id = 0; if (IsAnchor) RClick.AppendItem(LLoadString(L_COPY_LINK_LOCATION, "&Copy Link Location"), IDM_COPY_LINK, Uri != NULL); if (IsImg) RClick.AppendItem("Copy Image Location", IDM_COPY_IMG, ImgSrc != NULL); if (Html->GetEnv()) Html->GetEnv()->AppendItems(&RClick, Uri); switch (Id = RClick.Float(Html, m.x, m.y)) { case IDM_COPY_LINK: { LClipBoard Clip(Html); Clip.Text(Uri); break; } case IDM_COPY_IMG: { LClipBoard Clip(Html); Clip.Text(ImgSrc); break; } default: { if (Html->GetEnv()) Html->GetEnv()->OnMenu(Html, Id, a); break; } } } Processed = true; } } else if (m.Down() && m.Left()) { #ifdef _DEBUG if (m.Ctrl()) { auto Style = ToString(); LStringPipe p(256); p.Print("Tag: %s\n", Tag ? Tag.Get() : "CONTENT"); if (Class.Length()) { p.Print("Class(es): "); for (unsigned i=0; iParent; t=ToTag(t->Parent)) { LStringPipe Tmp; Tmp.Print(" %s", t->Tag ? t->Tag.Get() : "CONTENT"); if (t->HtmlId) { Tmp.Print("#%s", t->HtmlId); } for (unsigned i=0; iClass.Length(); i++) { Tmp.Print(".%s", t->Class[i].Get()); } LAutoString Txt(Tmp.NewStr()); p.Print("%s", Txt.Get()); LDisplayString Ds(LSysFont, Txt); int Px = 170 - Ds.X(); int Chars = Px / Sp.X(); for (int c=0; cPos.x, t->Pos.y, t->Size.x, t->Size.y); } LAutoString a(p.NewStr()); LgiMsg( Html, "%s", Html->GetClass(), MB_OK, a.Get()); } else #endif { LString Uri; if (Html && Html->Environment) { if (IsAnchor(&Uri)) { if (Uri) { if (!Html->d->LinkDoubleClick || m.Double()) { Html->Environment->OnNavigate(Html, Uri); Processed = true; } } const char *OnClk = NULL; if (!Processed && Get("onclick", OnClk)) { Html->Environment->OnExecuteScript(Html, (char*)OnClk); } } else { Processed = OnClick(m); } } } } return Processed; } LTag *LTag::GetBlockParent(ssize_t *Idx) { if (IsBlock()) { if (Idx) *Idx = 0; return this; } for (LTag *t = this; t; t = ToTag(t->Parent)) { if (ToTag(t->Parent)->IsBlock()) { if (Idx) { *Idx = t->Parent->Children.IndexOf(t); } return ToTag(t->Parent); } } return 0; } LTag *LTag::GetAnchor(char *Name) { if (!Name) return 0; const char *n; if (IsAnchor(0) && Get("name", n) && n && !_stricmp(Name, n)) { return this; } for (unsigned i=0; iGetAnchor(Name); if (Result) return Result; } return 0; } LTag *LTag::GetTagByName(const char *Name) { if (Name) { if (Tag && _stricmp(Tag, Name) == 0) { return this; } for (unsigned i=0; iGetTagByName(Name); if (Result) return Result; } } return 0; } static int IsNearRect(LRect *r, int x, int y) { if (r->Overlap(x, y)) { return 0; } else if (x >= r->x1 && x <= r->x2) { if (y < r->y1) return r->y1 - y; else return y - r->y2; } else if (y >= r->y1 && y <= r->y2) { if (x < r->x1) return r->x1 - x; else return x - r->x2; } int64 dx = 0; int64 dy = 0; if (x < r->x1) { if (y < r->y1) { // top left dx = r->x1 - x; dy = r->y1 - y; } else { // bottom left dx = r->x1 - x; dy = y - r->y2; } } else { if (y < r->y1) { // top right dx = x - r->x2; dy = r->y1 - y; } else { // bottom right dx = x - r->x2; dy = y - r->y2; } } return (int) sqrt( (double) ( (dx * dx) + (dy * dy) ) ); } ssize_t LTag::NearestChar(LFlowRect *Tr, int x, int y) { LFont *f = GetFont(); if (f) { LDisplayString ds(f, Tr->Text, Tr->Len); ssize_t c = ds.CharAt(x - Tr->x1); if (Tr->Text == PreText()) { return 0; } else { char16 *t = Tr->Text + c; size_t Len = StrlenW(Text()); if (t >= Text() && t <= Text() + Len) { return (t - Text()) - GetTextStart(); } else { LgiTrace("%s:%i - Error getting char at position.\n", _FL); } } } return -1; } void LTag::GetTagByPos(LTagHit &TagHit, int x, int y, int Depth, bool InBody, bool DebugLog) { /* InBody: Originally I had this test in the code but it seems that some test cases have actual content after the body. And testing for "InBody" breaks functionality for those cases (see "spam4.html" and the unsubscribe link at the end of the doc). */ if (TagId == TAG_IMG) { LRect img(0, 0, Size.x - 1, Size.y - 1); if (/*InBody &&*/ img.Overlap(x, y)) { TagHit.Direct = this; TagHit.Block = 0; } } else if (/*InBody &&*/ TextPos.Length()) { for (unsigned i=0; i= Tr->y1 && y <= Tr->y2; int Near = IsNearRect(Tr, x, y); if (Near >= 0 && Near < 100) { if ( !TagHit.NearestText || ( SameRow && !TagHit.NearSameRow ) || ( SameRow == TagHit.NearSameRow && Near < TagHit.Near ) ) { TagHit.NearestText = this; TagHit.NearSameRow = SameRow; TagHit.Block = Tr; TagHit.Near = Near; TagHit.Index = NearestChar(Tr, x, y); if (DebugLog) { LgiTrace("%i:GetTagByPos HitText %s #%s, idx=%i, near=%i, txt='%S'\n", Depth, Tag.Get(), HtmlId, TagHit.Index, TagHit.Near, Tr->Text); } if (!TagHit.Near) { TagHit.Direct = this; TagHit.LocalCoords.x = x; TagHit.LocalCoords.y = y; } } } } } else if ( TagId != TAG_TR && Tag && x >= 0 && y >= 0 && x < Size.x && y < Size.y // && InBody ) { // Direct hit TagHit.Direct = this; TagHit.LocalCoords.x = x; TagHit.LocalCoords.y = y; if (DebugLog) { LgiTrace("%i:GetTagByPos DirectHit %s #%s, idx=%i, near=%i\n", Depth, Tag.Get(), HtmlId, TagHit.Index, TagHit.Near); } } if (TagId == TAG_BODY) InBody = true; for (unsigned i=0; iPos.x >= 0 && t->Pos.y >= 0) { t->GetTagByPos(TagHit, x - t->Pos.x, y - t->Pos.y, Depth + 1, InBody, DebugLog); } } } int LTag::OnNotify(LNotification n) { if (!Ctrl || !Html->InThread()) return 0; switch (CtrlType) { case CtrlSubmit: { LTag *Form = this; while (Form && Form->TagId != TAG_FORM) Form = ToTag(Form->Parent); if (Form) Html->OnSubmitForm(Form); break; } default: { CtrlValue = Ctrl->Name(); break; } } return 0; } void LTag::CollectFormValues(LHashTbl,char*> &f) { if (CtrlType != CtrlNone) { const char *Name; if (Get("name", Name)) { char *Existing = f.Find(Name); if (Existing) DeleteArray(Existing); char *Val = CtrlValue.Str(); if (Val) { LStringPipe p(256); for (char *v = Val; *v; v++) { if (*v == ' ') p.Write("+", 1); else if (IsAlpha(*v) || IsDigit(*v) || *v == '_' || *v == '.') p.Write(v, 1); else p.Print("%%%02.2X", *v); } f.Add(Name, p.NewStr()); } else { f.Add(Name, NewStr("")); } } } for (unsigned i=0; iCollectFormValues(f); } } LTag *LTag::FindCtrlId(int Id) { if (Ctrl && Ctrl->GetId() == Id) return this; for (unsigned i=0; iFindCtrlId(Id); if (f) return f; } return NULL; } void LTag::Find(int TagType, LArray &Out) { if (TagId == TagType) { Out.Add(this); } for (unsigned i=0; iFind(TagType, Out); } } void LTag::SetImage(const char *Uri, LSurface *Img) { if (Img) { if (TagId != TAG_IMG) { ImageDef *Def = (ImageDef*)LCss::Props.Find(PropBackgroundImage); if (Def) { Def->Type = ImageOwn; DeleteObj(Def->Img); Def->Img = Img; } } else { if (Img->GetColourSpace() == CsIndex8) { if (Image.Reset(new LMemDC(Img->X(), Img->Y(), System32BitColourSpace))) { Image->Colour(0, 32); Image->Rectangle(); Image->Blt(0, 0, Img); } else LgiTrace("%s:%i - SetImage can't promote 8bit image to 32bit.\n", _FL); } else Image.Reset(Img); LRect r = XSubRect(); if (r.Valid()) { LAutoPtr t(new LMemDC(r.X(), r.Y(), Image->GetColourSpace())); if (t) { t->Blt(0, 0, Image, &r); Image = t; } } } for (unsigned i=0; iCell) { t->Cell->MinContent = 0; t->Cell->MaxContent = 0; } } } else { Html->d->Loading.Add(Uri, this); } } void LTag::LoadImage(const char *Uri) { #if DOCUMENT_LOAD_IMAGES if (!Html->Environment) return; LUri u(Uri); bool LdImg = Html->GetLoadImages(); bool IsRemote = u.sProtocol && ( !_stricmp(u.sProtocol, "http") || !_stricmp(u.sProtocol, "https") || !_stricmp(u.sProtocol, "ftp") ); if (IsRemote && !LdImg) { Html->NeedsCapability("RemoteContent"); return; } else if (u.IsProtocol("data")) { if (!u.sPath) return; const char *s = u.sPath; if (*s++ != '/') return; LAutoString Type(LTokStr(s)); if (*s++ != ',') return; auto p = LString(Type).SplitDelimit(",;:"); if (p.Length() != 2 || !p.Last().Equals("base64")) return; LString Name = LString("name.") + p[0]; auto Filter = LFilterFactory::New(Name, FILTER_CAP_READ, NULL); if (!Filter) return; auto slen = strlen(s); auto blen = BufferLen_64ToBin(slen); LMemStream bin; bin.SetSize(blen); ConvertBase64ToBinary((uint8_t*)bin.GetBasePtr(), blen, s, slen); bin.SetPos(0); if (!Image.Reset(new LMemDC)) return; auto result = Filter->ReadImage(Image, &bin); if (result != LFilter::IoSuccess) Image.Reset(); return; } LDocumentEnv::LoadJob *j = Html->Environment->NewJob(); if (j) { LAssert(Html != NULL); j->Uri.Reset(NewStr(Uri)); j->Env = Html->Environment; j->UserData = this; j->UserUid = Html->GetDocumentUid(); // LgiTrace("%s:%i - new job %p, %p\n", _FL, j, j->UserData); LDocumentEnv::LoadType Result = Html->Environment->GetContent(j); if (Result == LDocumentEnv::LoadImmediate) { SetImage(Uri, j->pDC.Release()); } else if (Result == LDocumentEnv::LoadDeferred) { Html->d->DeferredLoads++; } DeleteObj(j); } #endif } void LTag::LoadImages() { const char *Uri = 0; if (Html->Environment && TagId == TAG_IMG && !Image) { if (Get("src", Uri)) LoadImage(Uri); } for (unsigned i=0; iLoadImages(); } } void LTag::ImageLoaded(char *uri, LSurface *Img, int &Used) { const char *Uri = 0; if (!Image && Get("src", Uri)) { if (strcmp(Uri, uri) == 0) { if (Used == 0) { SetImage(Uri, Img); } else { SetImage(Uri, new LMemDC(Img)); } Used++; } } for (unsigned i=0; iImageLoaded(uri, Img, Used); } } struct LTagElementCallback : public LCss::ElementCallback { const char *Val; const char *GetElement(LTag *obj) { return obj->Tag; } const char *GetAttr(LTag *obj, const char *Attr) { if (obj->Get(Attr, Val)) return Val; return NULL; } bool GetClasses(LString::Array &Classes, LTag *obj) { Classes = obj->Class; return Classes.Length() > 0; } LTag *GetParent(LTag *obj) { return ToTag(obj->Parent); } LArray GetChildren(LTag *obj) { LArray c; for (unsigned i=0; iChildren.Length(); i++) c.Add(ToTag(obj->Children[i])); return c; } }; void LTag::RestyleAll() { Restyle(); for (unsigned i=0; iRestyleAll(); } } // After CSS has changed this function scans through the CSS and applies any rules // that match the current tag. void LTag::Restyle() { // Use the matching built into the LCss Store. LCss::SelArray Styles; LTagElementCallback Context; if (Html->CssStore.Match(Styles, &Context, this)) { for (unsigned i=0; iStyle); } } // Do the element specific styles const char *s; if (Get("style", s)) SetCssStyle(s); #if DEBUG_RESTYLE && defined(_DEBUG) if (Debug) { auto Style = ToString(); LgiTrace(">>>> %s <<<<:\n%s\n\n", Tag.Get(), Style.Get()); } #endif } void LTag::SetStyle() { const static float FntMul[] = { 0.6f, // size=1 0.89f, // size=2 1.0f, // size=3 1.2f, // size=4 1.5f, // size=5 2.0f, // size=6 3.0f // size=7 }; const char *s = 0; #ifdef _DEBUG if (Get("debug", s)) { if ((Debug = atoi(s))) { LgiTrace("Debug Tag: %p '%s'\n", this, Tag ? Tag.Get() : "CONTENT"); } } #endif if (Get("Color", s)) { ColorDef Def; if (LHtmlParser::ParseColour(s, Def)) { Color(Def); } } if (Get("Background", s) || Get("bgcolor", s)) { ColorDef Def; if (LHtmlParser::ParseColour(s, Def)) { BackgroundColor(Def); } else { LCss::ImageDef Img; Img.Type = ImageUri; Img.Uri = s; BackgroundImage(Img); BackgroundRepeat(RepeatBoth); } } switch (TagId) { default: { if (!Stricmp(Tag.Get(), "o:p")) Display(LCss::DispNone); break; } case TAG_LINK: { const char *Type, *Href; if (Html->Environment && Get("type", Type) && Get("href", Href) && !Stricmp(Type, "text/css") && !Html->CssHref.Find(Href)) { LDocumentEnv::LoadJob *j = Html->Environment->NewJob(); if (j) { LAssert(Html != NULL); LTag *t = this; j->Uri.Reset(NewStr(Href)); j->Env = Html->Environment; j->UserData = t; j->UserUid = Html->GetDocumentUid(); LDocumentEnv::LoadType Result = Html->Environment->GetContent(j); if (Result == LDocumentEnv::LoadImmediate) { LStreamI *s = j->GetStream(); if (s) { int Len = (int)s->GetSize(); if (Len > 0) { LAutoString a(new char[Len+1]); ssize_t r = s->Read(a, Len); a[r] = 0; Html->CssHref.Add(Href, true); Html->OnAddStyle("text/css", a); } } } else if (Result == LDocumentEnv::LoadDeferred) { Html->d->DeferredLoads++; } DeleteObj(j); } } break; } case TAG_BLOCKQUOTE: { MarginTop(Len("8px")); MarginBottom(Len("8px")); MarginLeft(Len("16px")); if (Get("Type", s)) { if (_stricmp(s, "cite") == 0) { BorderLeft(BorderDef(this, "1px solid blue")); PaddingLeft(Len("0.5em")); /* ColorDef Def; Def.Type = ColorRgb; Def.Rgb32 = Rgb32(0x80, 0x80, 0x80); Color(Def); */ } } break; } case TAG_P: { MarginBottom(Len("1em")); break; } case TAG_A: { const char *Href; if (Get("href", Href)) { ColorDef c; c.Type = ColorRgb; c.Rgb32 = Rgb32(0, 0, 255); Color(c); TextDecoration(TextDecorUnderline); } break; } case TAG_TABLE: { Len l; if (!Cell) Cell = new TblCell; if (Get("border", s)) { BorderDef b; if (b.Parse(this, s)) { BorderLeft(b); BorderRight(b); BorderTop(b); BorderBottom(b); } } if (Get("cellspacing", s) && l.Parse(s, PropBorderSpacing, ParseRelaxed)) { BorderSpacing(l); } else { // BorderSpacing(LCss::Len(LCss::LenPx, 2.0f)); } if (Get("cellpadding", s) && l.Parse(s, Prop_CellPadding, ParseRelaxed)) { _CellPadding(l); } if (Get("align", s)) { Len l; if (l.Parse(s)) Cell->XAlign = l.Type; } break; } case TAG_TD: case TAG_TH: { if (!Cell) Cell = new TblCell; LTag *Table = GetTable(); if (Table) { Len l = Table->_CellPadding(); if (!l.IsValid()) { l.Type = LCss::LenPx; l.Value = DefaultCellPadding; } PaddingLeft(l); PaddingRight(l); PaddingTop(l); PaddingBottom(l); } if (TagId == TAG_TH) FontWeight(LCss::FontWeightBold); break; } case TAG_BODY: { MarginLeft(Len(Get("leftmargin", s) ? s : DefaultBodyMargin)); MarginTop(Len(Get("topmargin", s) ? s : DefaultBodyMargin)); MarginRight(Len(Get("rightmargin", s) ? s : DefaultBodyMargin)); if (Get("text", s)) { ColorDef c; if (c.Parse(s)) { Color(c); } } break; } case TAG_OL: case TAG_UL: { MarginLeft(Len("16px")); break; } case TAG_STRONG: case TAG_B: { FontWeight(FontWeightBold); break; } case TAG_I: { FontStyle(FontStyleItalic); break; } case TAG_U: { TextDecoration(TextDecorUnderline); break; } case TAG_SUP: { VerticalAlign(VerticalSuper); FontSize(SizeSmaller); break; } case TAG_SUB: { VerticalAlign(VerticalSub); FontSize(SizeSmaller); break; } case TAG_TITLE: { Display(LCss::DispNone); break; } } if (Get("width", s)) { Len l; if (l.Parse(s, PropWidth, ParseRelaxed)) { Width(l); } } if (Get("height", s)) { Len l; if (l.Parse(s, PropHeight, ParseRelaxed)) Height(l); } if (Get("align", s)) { if (_stricmp(s, "left") == 0) TextAlign(Len(AlignLeft)); else if (_stricmp(s, "right") == 0) TextAlign(Len(AlignRight)); else if (_stricmp(s, "center") == 0) TextAlign(Len(AlignCenter)); } if (Get("valign", s)) { if (_stricmp(s, "top") == 0) VerticalAlign(Len(VerticalTop)); else if (_stricmp(s, "middle") == 0) VerticalAlign(Len(VerticalMiddle)); else if (_stricmp(s, "bottom") == 0) VerticalAlign(Len(VerticalBottom)); } Get("id", HtmlId); if (Get("class", s)) { Class = LString(s).SplitDelimit(" \t"); } Restyle(); switch (TagId) { default: break; case TAG_BIG: { LCss::Len l; l.Type = SizeLarger; FontSize(l); break; } /* case TAG_META: { LAutoString Cs; const char *s; if (Get("http-equiv", s) && _stricmp(s, "Content-Type") == 0) { const char *ContentType; if (Get("content", ContentType)) { char *CharSet = stristr(ContentType, "charset="); if (CharSet) { char16 *cs = NULL; Html->ParsePropValue(CharSet + 8, cs); Cs.Reset(WideToUtf8(cs)); DeleteArray(cs); } } } if (Get("name", s) && _stricmp(s, "charset") == 0 && Get("content", s)) { Cs.Reset(NewStr(s)); } else if (Get("charset", s)) { Cs.Reset(NewStr(s)); } if (Cs) { if (Cs && _stricmp(Cs, "utf-16") != 0 && _stricmp(Cs, "utf-32") != 0 && LGetCsInfo(Cs)) { // Html->SetCharset(Cs); } } break; } */ case TAG_BODY: { LCss::ColorDef Bk = BackgroundColor(); if (Bk.Type != ColorInherit) { // Copy the background up to the LHtml wrapper Html->GetCss(true)->BackgroundColor(Bk); } /* LFont *f = GetFont(); if (FontSize().Type == LenInherit) { FontSize(Len(LenPt, (float)f->PointSize())); } */ break; } case TAG_HEAD: { Display(DispNone); break; } case TAG_PRE: { LFontType Type; if (Type.GetSystemFont("Fixed")) { LAssert(ValidStr(Type.GetFace())); FontFamily(StringsDef(Type.GetFace())); } break; } case TAG_TR: break; case TAG_TD: case TAG_TH: { LAssert(Cell != NULL); const char *s; if (Get("colspan", s)) Cell->Span.x = atoi(s); else Cell->Span.x = 1; if (Get("rowspan", s)) Cell->Span.y = atoi(s); else Cell->Span.y = 1; Cell->Span.x = MAX(Cell->Span.x, 1); Cell->Span.y = MAX(Cell->Span.y, 1); if (Display() == DispInline || Display() == DispInlineBlock) { Display(DispBlock); // Inline-block TD??? Nope. } break; } case TAG_IMG: { const char *Uri; if (Html->Environment && Get("src", Uri)) { // printf("Uri: %s\n", Uri); LoadImage(Uri); } break; } case TAG_H1: { char s[32]; sprintf_s(s, sizeof(s), "%ipt", (int)((float)Html->DefFont()->PointSize() * FntMul[5])); FontSize(Len(s)); FontWeight(FontWeightBold); break; } case TAG_H2: { char s[32]; sprintf_s(s, sizeof(s), "%ipt", (int)((float)Html->DefFont()->PointSize() * FntMul[4])); FontSize(Len(s)); FontWeight(FontWeightBold); break; } case TAG_H3: { char s[32]; sprintf_s(s, sizeof(s), "%ipt", (int)((float)Html->DefFont()->PointSize() * FntMul[3])); FontSize(Len(s)); FontWeight(FontWeightBold); break; } case TAG_H4: { char s[32]; sprintf_s(s, sizeof(s), "%ipt", (int)((float)Html->DefFont()->PointSize() * FntMul[2])); FontSize(Len(s)); FontWeight(FontWeightBold); break; } case TAG_H5: { char s[32]; sprintf_s(s, sizeof(s), "%ipt", (int)((float)Html->DefFont()->PointSize() * FntMul[1])); FontSize(Len(s)); FontWeight(FontWeightBold); break; } case TAG_H6: { char s[32]; sprintf_s(s, sizeof(s), "%ipt", (int)((float)Html->DefFont()->PointSize() * FntMul[0])); FontSize(Len(s)); FontWeight(FontWeightBold); break; } case TAG_FONT: { const char *s = 0; if (Get("Face", s)) { - char16 *cw = CleanText(s, strlen(s), "utf-8", true); - char *c8 = WideToUtf8(cw); - DeleteArray(cw); - LToken Faces(c8, ","); - DeleteArray(c8); - char *face = TrimStr(Faces[0]); - if (ValidStr(face)) - { - FontFamily(face); - DeleteArray(face); - } + LAutoWString cw(CleanText(s, strlen(s), "utf-8", true)); + LAutoString c8(WideToUtf8(cw)); + + auto Faces = LString(c8).SplitDelimit(","); + auto Face = Faces[0].Strip(); + if (ValidStr(Face)) + FontFamily(c8.Get()); else - { - LgiTrace("%s:%i - No face for font tag.\n", __FILE__, __LINE__); - } + LgiTrace("%s:%i - No face for font tag.\n", _FL); } if (Get("Size", s)) { bool Digit = false, NonW = false; for (auto *c = s; *c; c++) { if (IsDigit(*c) || *c == '-') Digit = true; else if (!IsWhiteSpace(*c)) NonW = true; } if (Digit && !NonW) { auto Sz = atoi(s); switch (Sz) { case 1: FontSize(Len(LCss::LenEm, 0.63f)); break; case 2: FontSize(Len(LCss::LenEm, 0.82f)); break; case 3: FontSize(Len(LCss::LenEm, 1.0f)); break; case 4: FontSize(Len(LCss::LenEm, 1.13f)); break; case 5: FontSize(Len(LCss::LenEm, 1.5f)); break; case 6: FontSize(Len(LCss::LenEm, 2.0f)); break; case 7: FontSize(Len(LCss::LenEm, 3.0f)); break; } } else { FontSize(Len(s)); } } break; } case TAG_SELECT: { if (!Html->InThread()) break; LAssert(!Ctrl); Ctrl = new LCombo(Html->d->NextCtrlId++, 0, 0, 100, LSysFont->GetHeight() + 8, NULL); CtrlType = CtrlSelect; break; } case TAG_INPUT: { if (!Html->InThread()) break; LAssert(!Ctrl); const char *Type, *Value = NULL; Get("value", Value); LAutoWString CleanValue(Value ? CleanText(Value, strlen(Value), "utf-8", true, true) : NULL); if (CleanValue) { CtrlValue = CleanValue; } if (Get("type", Type)) { if (!_stricmp(Type, "password")) CtrlType = CtrlPassword; else if (!_stricmp(Type, "email")) CtrlType = CtrlEmail; else if (!_stricmp(Type, "text")) CtrlType = CtrlText; else if (!_stricmp(Type, "button")) CtrlType = CtrlButton; else if (!_stricmp(Type, "submit")) CtrlType = CtrlSubmit; else if (!_stricmp(Type, "hidden")) CtrlType = CtrlHidden; DeleteObj(Ctrl); if (CtrlType == CtrlEmail || CtrlType == CtrlText || CtrlType == CtrlPassword) { LEdit *Ed; LAutoString UtfCleanValue(WideToUtf8(CleanValue)); Ctrl = Ed = new LEdit(Html->d->NextCtrlId++, 0, 0, 60, LSysFont->GetHeight() + 8, UtfCleanValue); if (Ctrl) { Ed->Sunken(false); Ed->Password(CtrlType == CtrlPassword); } } else if (CtrlType == CtrlButton || CtrlType == CtrlSubmit) { LAutoString UtfCleanValue(WideToUtf8(CleanValue)); if (UtfCleanValue) { Ctrl = new InputButton(this, Html->d->NextCtrlId++, UtfCleanValue); } } } break; } } if (IsBlock()) { LCss::ImageDef bk = BackgroundImage(); if (bk.Type == LCss::ImageUri && ValidStr(bk.Uri) && !bk.Uri.Equals("transparent")) { LoadImage(bk.Uri); } } if (Ctrl) { LFont *f = GetFont(); if (f) Ctrl->SetFont(f, false); } } void LTag::OnStyleChange(const char *name) { if (!Stricmp(name, "display") && Html) { Html->Layout(true); Html->Invalidate(); } } void LTag::SetCssStyle(const char *Style) { if (Style) { // Strip out comments char *Comment = NULL; while ((Comment = strstr((char*)Style, "/*"))) { char *End = strstr(Comment+2, "*/"); if (!End) break; for (char *c = Comment; cDocCharSet && Html->Charset) { DocAndCsTheSame = _stricmp(Html->DocCharSet, Html->Charset) == 0; } if (SourceCs) { t = (char16*) LNewConvertCp(LGI_WideCharset, s, SourceCs, Len); } else if (Html->DocCharSet && Html->Charset && !DocAndCsTheSame && !Html->OverideDocCharset) { char *DocText = (char*)LNewConvertCp(Html->DocCharSet, s, Html->Charset, Len); t = (char16*) LNewConvertCp(LGI_WideCharset, DocText, Html->DocCharSet, -1); DeleteArray(DocText); } else if (Html->DocCharSet) { t = (char16*) LNewConvertCp(LGI_WideCharset, s, Html->DocCharSet, Len); } else { t = (char16*) LNewConvertCp(LGI_WideCharset, s, Html->Charset.Get() ? Html->Charset.Get() : DefaultCs, Len); } if (t && ConversionAllowed) { char16 *o = t; for (char16 *i=t; *i; ) { switch (*i) { case '&': { i++; if (*i == '#') { // Unicode Number char n[32] = "", *p = n; i++; if (*i == 'x' || *i == 'X') { // Hex number i++; while ( *i && ( IsDigit(*i) || (*i >= 'A' && *i <= 'F') || (*i >= 'a' && *i <= 'f') ) && (p - n) < 31) { *p++ = (char)*i++; } } else { // Decimal number while (*i && IsDigit(*i) && (p - n) < 31) { *p++ = (char)*i++; } } *p++ = 0; char16 Ch = atoi(n); if (Ch) { *o++ = Ch; } if (*i && *i != ';') i--; } else { // Named Char char16 *e = i; while (*e && IsAlpha(*e) && *e != ';') { e++; } LAutoWString Var(NewStrW(i, e-i)); char16 Char = LHtmlStatic::Inst->VarMap.Find(Var); if (Char) { *o++ = Char; i = e; } else { i--; *o++ = *i; } } break; } case '\r': { break; } case ' ': case '\t': case '\n': { if (KeepWhiteSpace) { *o++ = *i; } else { *o++ = ' '; // Skip furthur whitespace while (i[1] && IsWhiteSpace(i[1])) { i++; } } break; } default: { // Normal char *o++ = *i; break; } } if (*i) i++; else break; } *o++ = 0; } if (t && !*t) { DeleteArray(t); } return t; } char *LTag::ParseText(char *Doc) { ColorDef c; c.Type = ColorRgb; c.Rgb32 = LColour(L_WORKSPACE).c32(); BackgroundColor(c); TagId = TAG_BODY; Tag.Reset(NewStr("body")); Info = Html->GetTagInfo(Tag); char *OriginalCp = NewStr(Html->Charset); LStringPipe Utf16; char *s = Doc; while (s) { if (*s == '\r') { s++; } else if (*s == '<') { // Process tag char *e = s; e++; while (*e && *e != '>') { if (*e == '\"' || *e == '\'') { char *q = strchr(e + 1, *e); if (q) e = q + 1; else e++; } else e++; } if (*e == '>') e++; // Output tag Html->SetCharset("iso-8859-1"); char16 *t = CleanText(s, e - s, NULL, false); if (t) { Utf16.Push(t); DeleteArray(t); } s = e; } else if (!*s || *s == '\n') { // Output previous line char16 *Line = Utf16.NewStrW(); if (Line) { LTag *t = new LTag(Html, this); if (t) { t->Color(LColour(L_TEXT)); t->Text(Line); } } if (*s == '\n') { s++; LTag *t = new LTag(Html, this); if (t) { t->TagId = TAG_BR; t->Tag.Reset(NewStr("br")); t->Info = Html->GetTagInfo(t->Tag); } } else break; } else { // Seek end of text char *e = s; while (*e && *e != '\r' && *e != '\n' && *e != '<') e++; // Output text Html->SetCharset(OriginalCp); LAutoWString t(CleanText(s, e - s, NULL, false)); if (t) { Utf16.Push(t); } s = e; } } Html->SetCharset(OriginalCp); DeleteArray(OriginalCp); return 0; } bool LTag::ConvertToText(TextConvertState &State) { const static char *Rule = "------------------------------------------------------"; int DepthInc = 0; switch (TagId) { default: break; case TAG_P: if (State.GetPrev()) State.NewLine(); break; case TAG_UL: case TAG_OL: DepthInc = 2; break; } if (ValidStrW(Txt)) { for (int i=0; iConvertToUnicode(Txt); else u.Reset(WideToUtf8(Txt)); if (u) { size_t u_len = strlen(u); State.Write(u, u_len); } } State.Depth += DepthInc; for (unsigned i=0; iConvertToText(State); } State.Depth -= DepthInc; if (IsBlock()) { if (State.CharsOnLine) State.NewLine(); } else { switch (TagId) { case TAG_A: { // Emit the link to the anchor if it's different from the text of the span... const char *Href; if (Get("href", Href) && ValidStrW(Txt)) { if (_strnicmp(Href, "mailto:", 7) == 0) Href += 7; size_t HrefLen = strlen(Href); LAutoWString h(CleanText(Href, HrefLen, "utf-8")); if (h && StrcmpW(h, Txt) != 0) { // Href different from the text of the link State.Write(" (", 2); State.Write(Href, HrefLen); State.Write(")", 1); } } break; } case TAG_HR: { State.Write(Rule, strlen(Rule)); State.NewLine(); break; } case TAG_BR: { State.NewLine(); break; } default: break; } } return true; } char *LTag::NextTag(char *s) { while (s && *s) { char *n = strchr(s, '<'); if (n) { if (!n[1]) return NULL; if (IsAlpha(n[1]) || strchr("!/", n[1]) || n[1] == '?') { return n; } s = n + 1; } else break; } return 0; } void LHtml::CloseTag(LTag *t) { if (!t) return; OpenTags.Delete(t); } bool LTag::OnUnhandledColor(LCss::ColorDef *def, const char *&s) { const char *e = s; while (*e && (IsText(*e) || *e == '_')) e++; char tmp[256]; ssize_t len = e - s; memcpy(tmp, s, len); tmp[len] = 0; int m = LHtmlStatic::Inst->ColourMap.Find(tmp); s = e; if (m >= 0) { def->Type = LCss::ColorRgb; def->Rgb32 = Rgb24To32(m); return true; } return false; } void LTag::ZeroTableElements() { if (TagId == TAG_TABLE || TagId == TAG_TR || IsTableCell(TagId)) { Size.x = 0; Size.y = 0; if (Cell) { Cell->MinContent = 0; Cell->MaxContent = 0; } for (unsigned i=0; iZeroTableElements(); } } } void LTag::ResetCaches() { /* If during the parse process a callback causes a layout to happen then it's possible to have partial information in the LHtmlTableLayout structure, like missing TD cells. Because they haven't been parsed yet. This is called at the end of the parsing to reset all the cached info in LHtmlTableLayout. That way when the first real layout happens all the data is there. */ if (Cell) DeleteObj(Cell->Cells); for (size_t i=0; iResetCaches(); } LPoint LTag::GetTableSize() { LPoint s(0, 0); if (Cell && Cell->Cells) { Cell->Cells->GetSize(s.x, s.y); } return s; } LTag *LTag::GetTableCell(int x, int y) { LTag *t = this; while ( t && !t->Cell && !t->Cell->Cells && t->Parent) { t = ToTag(t->Parent); } if (t && t->Cell && t->Cell->Cells) { return t->Cell->Cells->Get(x, y); } return 0; } // This function gets the largest and smallest piece of content // in this cell and all it's children. bool LTag::GetWidthMetrics(LTag *Table, uint16 &Min, uint16 &Max) { bool Status = true; int MarginPx = 0; int LineWidth = 0; if (Display() == LCss::DispNone) return true; // Break the text into words and measure... if (Text()) { int MinContent = 0; int MaxContent = 0; LFont *f = GetFont(); if (f) { for (char16 *s = Text(); s && *s; ) { // Skip whitespace... while (*s && StrchrW(WhiteW, *s)) s++; // Find end of non-whitespace char16 *e = s; while (*e && !StrchrW(WhiteW, *e)) e++; // Find size of the word ssize_t Len = e - s; if (Len > 0) { LDisplayString ds(f, s, Len); MinContent = MAX(MinContent, ds.X()); } // Move to the next word. s = (*e) ? e + 1 : 0; } LDisplayString ds(f, Text()); LineWidth = MaxContent = ds.X(); } #if 0//def _DEBUG if (Debug) { LgiTrace("GetWidthMetrics Font=%p Sz=%i,%i\n", f, MinContent, MaxContent); } #endif Min = MAX(Min, MinContent); Max = MAX(Max, MaxContent); } // Specific tag handling? switch (TagId) { default: { if (IsBlock()) { MarginPx = (int)(BorderLeft().ToPx() + BorderRight().ToPx() + PaddingLeft().ToPx() + PaddingRight().ToPx()); } break; } case TAG_IMG: { Len w = Width(); if (w.IsValid()) { int x = (int) w.Value; Min = MAX(Min, x); Max = MAX(Max, x); } else if (Image) { Min = Max = Image->X(); } else { Size.x = Size.y = DefaultImgSize; Min = MAX(Min, Size.x); Max = MAX(Max, Size.x); } break; } case TAG_TD: case TAG_TH: { Len w = Width(); if (w.IsValid()) { if (w.IsDynamic()) { Min = MAX(Min, (int)w.Value); Max = MAX(Max, (int)w.Value); } else { Max = w.ToPx(0, GetFont()); } } else { LCss::BorderDef BLeft = BorderLeft(); LCss::BorderDef BRight = BorderRight(); LCss::Len PLeft = PaddingLeft(); LCss::Len PRight = PaddingRight(); MarginPx = (int)(PLeft.ToPx() + PRight.ToPx() + BLeft.ToPx()); if (Table->BorderCollapse() == LCss::CollapseCollapse) MarginPx += BRight.ToPx(); } break; } case TAG_TABLE: { Len w = Width(); if (w.IsValid() && !w.IsDynamic()) { // Fixed width table... int CellSpacing = BorderSpacing().ToPx(Min, GetFont()); int Px = ((int)w.Value) + (CellSpacing << 1); Min = MAX(Min, Px); Max = MAX(Max, Px); return true; } else { LPoint s; LHtmlTableLayout c(this); c.GetSize(s.x, s.y); // Auto layout table LArray ColMin, ColMax; for (int y=0; yGetWidthMetrics(Table, a, b)) { ColMin[x] = MAX(ColMin[x], a); ColMax[x] = MAX(ColMax[x], b); } x += t->Cell->Span.x; } else break; } } int MinSum = 0, MaxSum = 0; for (int i=0; iGetWidthMetrics(Table, Min, TagMax); LineWidth += TagMax; if (c->TagId == TAG_BR || c->TagId == TAG_LI) { Max = MAX(Max, LineWidth); LineWidth = 0; } } Max = MAX(Max, LineWidth); Min += MarginPx; Max += MarginPx; return Status; } static void DistributeSize(LArray &a, int Start, int Span, int Size, int Border) { // Calculate the current size of the cells int Cur = -Border; for (int i=0; i T Sum(LArray &a) { T s = 0; for (unsigned i=0; iCells) { #if defined(_DEBUG) && DEBUG_TABLE_LAYOUT if (Debug) { //int asd=0; } #endif Cell->Cells = new LHtmlTableLayout(this); #if defined(_DEBUG) && DEBUG_TABLE_LAYOUT if (Cell->Cells && Debug) Cell->Cells->Dump(); #endif } if (Cell->Cells) Cell->Cells->LayoutTable(f, Depth); } void LHtmlTableLayout::AllocatePx(int StartCol, int Cols, int MinPx, bool HasToFillAllAvailable) { // Get the existing total size and size of the column set int CurrentTotalX = GetTotalX(); int CurrentSpanX = GetTotalX(StartCol, Cols); int MaxAdditionalPx = AvailableX - CurrentTotalX; if (MaxAdditionalPx <= 0) return; // Calculate the maximum space we have for this column set int AvailPx = (CurrentSpanX + MaxAdditionalPx) - BorderX1 - BorderX2; // Allocate any remaining space... int RemainingPx = MaxAdditionalPx; LArray Growable, NonGrowable, SizeInherit; int GrowablePx = 0; for (int x=StartCol; x 0) { GrowablePx += DiffPx; Growable.Add(x); } else if (MinCol[x] > 0) { NonGrowable.Add(x); } else if (MinCol[x] == 0 && CurrentSpanX < AvailPx) { // Growable.Add(x); } if (SizeCol[x].Type == LCss::LenInherit) SizeInherit.Add(x); } if (GrowablePx < RemainingPx && HasToFillAllAvailable) { if (Growable.Length() == 0) { // Add any suitable non-growable columns as well for (unsigned i=0; i MinCol[Largest]) Largest = i; } Growable.Add(Largest); } } if (Growable.Length()) { // Some growable columns... int Added = 0; // Reasonably increase the size of the columns... for (unsigned i=0; i 0) { AddPx = DiffPx; } else if (DiffPx > 0) { double Ratio = (double)DiffPx / GrowablePx; AddPx = (int) (Ratio * RemainingPx); } else { AddPx = RemainingPx / (int)Growable.Length(); } LAssert(AddPx >= 0); MinCol[x] += AddPx; LAssert(MinCol[x] >= 0); Added += AddPx; } if (Added < RemainingPx && HasToFillAllAvailable) { // Still more to add, so if (SizeInherit.Length()) { Growable = SizeInherit; } else { int Largest = -1; for (unsigned i=0; i MinCol[Largest]) Largest = x; } Growable.Length(1); Growable[0] = Largest; } int AddPx = (RemainingPx - Added) / (int)Growable.Length(); for (unsigned i=0; i= 0); } else { MinCol[x] += AddPx; LAssert(MinCol[x] >= 0); Added += AddPx; } } } } } struct ColInfo { int Large; int Growable; int Idx; int Px; }; int ColInfoCmp(ColInfo *a, ColInfo *b) { int LDiff = b->Large - a->Large; int LGrow = b->Growable - a->Growable; int LSize = b->Px - a->Px; return LDiff + LGrow + LSize; } void LHtmlTableLayout::DeallocatePx(int StartCol, int Cols, int MaxPx) { int TotalPx = GetTotalX(StartCol, Cols); if (TotalPx <= MaxPx || MaxPx == 0) return; int TrimPx = TotalPx - MaxPx; LArray Inf; int HalfMax = MaxPx >> 1; unsigned Interesting = 0; int InterestingPx = 0; for (int x=StartCol; x HalfMax; ci.Growable = MinCol[x] < MaxCol[x]; if (ci.Large || ci.Growable) { Interesting++; InterestingPx += ci.Px; } } Inf.Sort(ColInfoCmp); if (InterestingPx > 0) { for (unsigned i=0; i= 0); } else break; } } } int LHtmlTableLayout::GetTotalX(int StartCol, int Cols) { if (Cols < 0) Cols = s.x; int TotalX = BorderX1 + BorderX2 + CellSpacing; for (int x=StartCol; xZeroTableElements(); MinCol.Length(0); MaxCol.Length(0); MaxRow.Length(0); SizeCol.Length(0); LCss::Len BdrSpacing = Table->BorderSpacing(); CellSpacing = BdrSpacing.IsValid() ? (int)BdrSpacing.Value : 0; // Resolve total table width. TableWidth = Table->Width(); if (TableWidth.IsValid()) AvailableX = f->ResolveX(TableWidth, Table, false); else AvailableX = f->X(); LCss::Len MaxWidth = Table->MaxWidth(); if (MaxWidth.IsValid()) { int Px = f->ResolveX(MaxWidth, Table, false); if (Px < AvailableX) AvailableX = Px; } TableBorder = f->ResolveBorder(Table, Table); if (Table->BorderCollapse() != LCss::CollapseCollapse) TablePadding = f->ResolvePadding(Table, Table); else TablePadding.ZOff(0, 0); BorderX1 = TableBorder.x1 + TablePadding.x1; BorderX2 = TableBorder.x2 + TablePadding.x2; #if defined(_DEBUG) && DEBUG_TABLE_LAYOUT if (Table->Debug) LgiTrace("AvailableX=%i, BorderX1=%i, BorderX2=%i\n", AvailableX, BorderX1, BorderX2); #endif #ifdef _DEBUG if (Table->Debug) { printf("Table Debug\n"); } #endif // Size detection pass int y; for (y=0; yGetFont(); t->Cell->BorderPx = f->ResolveBorder(t, t); t->Cell->PaddingPx = f->ResolvePadding(t, t); if (t->Cell->Pos.x == x && t->Cell->Pos.y == y) { LCss::DisplayType Disp = t->Display(); if (Disp == LCss::DispNone) continue; LCss::Len Content = t->Width(); if (Content.IsValid() && t->Cell->Span.x == 1) { if (SizeCol[x].IsValid()) { int OldPx = f->ResolveX(SizeCol[x], t, false); int NewPx = f->ResolveX(Content, t, false); if (NewPx > OldPx) { SizeCol[x] = Content; } } else { SizeCol[x] = Content; } } if (!t->GetWidthMetrics(Table, t->Cell->MinContent, t->Cell->MaxContent)) { t->Cell->MinContent = 16; t->Cell->MaxContent = 16; } #if defined(_DEBUG) && DEBUG_TABLE_LAYOUT if (Table->Debug) LgiTrace("Content[%i,%i] MIN=%i MAX=%i\n", x, y, t->Cell->MinContent, t->Cell->MaxContent); #endif if (t->Cell->Span.x == 1) { int BoxPx = t->Cell->BorderPx.x1 + t->Cell->BorderPx.x2 + t->Cell->PaddingPx.x1 + t->Cell->PaddingPx.x2; MinCol[x] = MAX(MinCol[x], t->Cell->MinContent + BoxPx); LAssert(MinCol[x] >= 0); MaxCol[x] = MAX(MaxCol[x], t->Cell->MaxContent + BoxPx); } } x += t->Cell->Span.x; } else break; } } // How much space used so far? int TotalX = GetTotalX(); if (TotalX > AvailableX) { // FIXME: // Off -> 'cisra-cqs.html' renders correctly. // On -> 'cisra_outage.html', 'steam1.html' renders correctly. #if 1 DeallocatePx(0, (int)MinCol.Length(), AvailableX); TotalX = GetTotalX(); #endif } #if defined(_DEBUG) && DEBUG_TABLE_LAYOUT #define DumpCols(msg) \ if (Table->Debug) \ { \ LgiTrace("%s Ln%i - TotalX=%i AvailableX=%i\n", msg, __LINE__, TotalX, AvailableX); \ for (unsigned i=0; iDebug) { printf("TableDebug\n"); } #endif // Process spanned cells for (y=0; yCell->Pos.x == x && t->Cell->Pos.y == y) { if (t->Cell->Span.x > 1 || t->Cell->Span.y > 1) { int i; int ColMin = -CellSpacing; int ColMax = -CellSpacing; for (i=0; iCell->Span.x; i++) { ColMin += MinCol[x + i] + CellSpacing; ColMax += MaxCol[x + i] + CellSpacing; } LCss::Len Width = t->Width(); if (Width.IsValid()) { int Px = f->ResolveX(Width, t, false); t->Cell->MinContent = MAX(t->Cell->MinContent, Px); t->Cell->MaxContent = MAX(t->Cell->MaxContent, Px); } #if defined(_DEBUG) && DEBUG_TABLE_LAYOUT if (Table->Debug) LgiTrace("Content[%i,%i] MIN=%i MAX=%i\n", x, y, t->Cell->MinContent, t->Cell->MaxContent); #endif if (t->Cell->MinContent > ColMin) AllocatePx(t->Cell->Pos.x, t->Cell->Span.x, t->Cell->MinContent, false); if (t->Cell->MaxContent > ColMax) DistributeSize(MaxCol, t->Cell->Pos.x, t->Cell->Span.x, t->Cell->MaxContent, CellSpacing); } x += t->Cell->Span.x; } else break; } } TotalX = GetTotalX(); DumpCols("AfterSpannedCells"); // Sometimes the web page specifies too many percentages: // Scale them all. float PercentSum = 0.0f; for (int i=0; i 100.0) { float Ratio = PercentSum / 100.0f; for (int i=0; iResolveX(w, Table, false); if (w.Type == LCss::LenPercent) { MaxCol[x] = Px; } else if (Px > MinCol[x]) { int RemainingPx = AvailableX - TotalX; int AddPx = Px - MinCol[x]; AddPx = MIN(RemainingPx, AddPx); TotalX += AddPx; MinCol[x] += AddPx; LAssert(MinCol[x] >= 0); } } } } TotalX = GetTotalX(); DumpCols("AfterCssNonPercentageSizes"); if (TotalX > AvailableX) { #if !ALLOW_TABLE_GROWTH // Deallocate space if overused // Take some from the largest column int Largest = 0; for (int i=0; i MinCol[Largest]) { Largest = i; } } int Take = TotalX - AvailableX; if (Take < MinCol[Largest]) { MinCol[Largest] = MinCol[Largest] - Take; LAssert(MinCol[Largest] >= 0); TotalX -= Take; } DumpCols("AfterSpaceDealloc"); #endif } else if (TotalX < AvailableX) { AllocatePx(0, s.x, AvailableX, TableWidth.IsValid()); DumpCols("AfterRemainingAlloc"); } // Layout cell horizontally and then flow the contents to get // the height of all the cells LArray RowPad; MaxRow.Length(s.y); for (y=0; yCell->Pos.x == x && t->Cell->Pos.y == y) { t->Pos.x = XPos; t->Size.x = -CellSpacing; XPos -= CellSpacing; RowPad[y].y1 = MAX(RowPad[y].y1, t->Cell->BorderPx.y1 + t->Cell->PaddingPx.y1); RowPad[y].y2 = MAX(RowPad[y].y2, t->Cell->BorderPx.y2 + t->Cell->PaddingPx.y2); LRect Box(0, 0, -CellSpacing, 0); for (int i=0; iCell->Span.x; i++) { int ColSize = MinCol[x + i] + CellSpacing; LAssert(ColSize >= 0); if (ColSize < 0) break; t->Size.x += ColSize; XPos += ColSize; Box.x2 += ColSize; } LCss::Len Ht = t->Height(); LFlowRegion r(Table->Html, Box, true); t->OnFlow(&r, Depth+1); if (r.MAX.y > r.y2) { t->Size.y = MAX(r.MAX.y, t->Size.y); } if (Ht.IsValid() && Ht.Type != LCss::LenPercent) { int h = f->ResolveY(Ht, t, false); t->Size.y = MAX(h, t->Size.y); DistributeSize(MaxRow, y, t->Cell->Span.y, t->Size.y, CellSpacing); } } x += t->Cell->Span.x; } } #if defined(_DEBUG) DEBUG_LOG("%s:%i - AfterCellFlow\n", _FL); for (unsigned i=0; iCell->Pos.x == x && t->Cell->Pos.y == y) { LCss::Len Ht = t->Height(); if (!(Ht.IsValid() && Ht.Type != LCss::LenPercent)) { DistributeSize(MaxRow, y, t->Cell->Span.y, t->Size.y, CellSpacing); } } x += t->Cell->Span.x; } else break; } } // Cell positioning int Cx = BorderX1 + CellSpacing; int Cy = TableBorder.y1 + TablePadding.y1 + CellSpacing; for (y=0; yParent); if (Row && Row->TagId == TAG_TR) { t = new LTag(Table->Html, Row); if (t) { t->TagId = TAG_TD; t->Tag.Reset(NewStr("td")); t->Info = Table->Html->GetTagInfo(t->Tag); if ((t->Cell = new LTag::TblCell)) { t->Cell->Pos.x = x; t->Cell->Pos.y = y; t->Cell->Span.x = 1; t->Cell->Span.y = 1; } t->BackgroundColor(LCss::ColorDef(LCss::ColorRgb, DefaultMissingCellColour)); Set(Table); } else break; } else break; } if (t) { if (t->Cell->Pos.x == x && t->Cell->Pos.y == y) { int RowPadOffset = RowPad[y].y1 - t->Cell->BorderPx.y1 - t->Cell->PaddingPx.y1; t->Pos.x = Cx; t->Pos.y = Cy + RowPadOffset; t->Size.x = -CellSpacing; for (int i=0; iCell->Span.x; i++) { int w = MinCol[x + i] + CellSpacing; t->Size.x += w; Cx += w; } t->Size.y = -CellSpacing; for (int n=0; nCell->Span.y; n++) { t->Size.y += MaxRow[y+n] + CellSpacing; } Table->Size.x = MAX(Cx + BorderX2, Table->Size.x); #if defined(_DEBUG) && DEBUG_TABLE_LAYOUT if (Table->Debug) { LgiTrace("cell(%i,%i) = pos(%i,%i)+size(%i,%i)\n", t->Cell->Pos.x, t->Cell->Pos.y, t->Pos.x, t->Pos.y, t->Size.x, t->Size.y); } #endif } else { Cx += t->Size.x + CellSpacing; } x += t->Cell->Span.x; } else break; Prev = t; } Cx = BorderX1 + CellSpacing; Cy += MaxRow[y] + CellSpacing; } switch (Table->Cell->XAlign ? Table->Cell->XAlign : ToTag(Table->Parent)->GetAlign(true)) { case LCss::AlignCenter: { int fx = f->X(); int Ox = (fx-Table->Size.x) >> 1; Table->Pos.x = f->x1 + MAX(Ox, 0); DEBUG_LOG("%s:%i - AlignCenter fx=%i ox=%i pos.x=%i size.x=%i\n", _FL, fx, Ox, Table->Pos.x, Table->Size.x); break; } case LCss::AlignRight: { Table->Pos.x = f->x2 - Table->Size.x; DEBUG_LOG("%s:%i - AlignRight f->x2=%i size.x=%i pos.x=%i\n", _FL, f->x2, Table->Size.x, Table->Pos.x); break; } default: { Table->Pos.x = f->x1; DEBUG_LOG("%s:%i - AlignLeft f->x1=%i size.x=%i pos.x=%i\n", _FL, f->x2, Table->Size.x, Table->Pos.x); break; } } Table->Pos.y = f->y1; Table->Size.y = Cy + TablePadding.y2 + TableBorder.y2; } LRect LTag::ChildBounds() { LRect b(0, 0, -1, -1); for (unsigned i=0; iGetRect(); b.Union(&c); } else { b = t->GetRect(); } } return b; } LPoint LTag::AbsolutePos() { LPoint p; for (LTag *t=this; t; t=ToTag(t->Parent)) { p += t->Pos; } return p; } void LTag::SetSize(LPoint &s) { Size = s; } LHtmlArea::~LHtmlArea() { DeleteObjects(); } LRect LHtmlArea::Bounds() { LRect n(0, 0, -1, -1); for (unsigned i=0; iLength(); i++) { LRect *r = (*c)[i]; if (!Top || (r && (r->y1 < Top->y1))) { Top = r; } } return Top; } void LHtmlArea::FlowText(LTag *Tag, LFlowRegion *Flow, LFont *Font, int LineHeight, char16 *Text, LCss::LengthType Align) { if (!Flow || !Text || !Font) return; SetFixedLength(false); char16 *Start = Text; size_t FullLen = StrlenW(Text); #if 1 if (!Tag->Html->GetReadOnly() && !*Text) { // Insert a text rect for this tag, even though it's empty. // This allows the user to place the cursor on a blank line. LFlowRect *Tr = new LFlowRect; Tr->Tag = Tag; Tr->Text = Text; Tr->x1 = Flow->cx; Tr->x2 = Tr->x1 + 1; Tr->y1 = Flow->y1; Tr->y2 = Tr->y1 + Font->GetHeight(); LAssert(Tr->y2 >= Tr->y1); Flow->y2 = MAX(Flow->y2, Tr->y2+1); Flow->cx = Tr->x2 + 1; Add(Tr); Flow->Insert(Tr, Align); return; } #endif while (*Text) { LFlowRect *Tr = new LFlowRect; if (!Tr) break; Tr->Tag = Tag; Restart: Tr->x1 = Flow->cx; Tr->y1 = Flow->y1; #if 1 // I removed this at one stage but forget why. // Remove white space at start of line if not in edit mode.. if (Tag->Html->GetReadOnly() && Flow->x1 == Flow->cx && *Text == ' ') { Text++; if (!*Text) { DeleteObj(Tr); break; } } #endif Tr->Text = Text; LDisplayString ds(Font, Text, MIN(1024, FullLen - (Text-Start))); ssize_t Chars = ds.CharAt(Flow->X()); bool Wrap = false; if (Text[Chars]) { // Word wrap // Seek back to the nearest break opportunity ssize_t n = Chars; while (n > 0 && !StrchrW(WhiteW, Text[n])) n--; if (n == 0) { if (Flow->x1 == Flow->cx) { // Already started from the margin and it's too long to // fit across the entire page, just let it hang off the right edge. // Seek to the end of the word for (Tr->Len = Chars; Text[Tr->Len] && !StrchrW(WhiteW, Text[Tr->Len]); Tr->Len++) ; // Wrap... if (*Text == ' ') Text++; } else { // Not at the start of the margin Flow->FinishLine(); goto Restart; } } else { Tr->Len = n; LAssert(Tr->Len > 0); Wrap = true; } } else { // Fits.. Tr->Len = Chars; LAssert(Tr->Len > 0); } LDisplayString ds2(Font, Tr->Text, Tr->Len); Tr->x2 = ds2.X(); Tr->y2 = LineHeight > 0 ? LineHeight - 1 : 0; if (Wrap) { Flow->cx = Flow->x1; Flow->y1 += Tr->y2 + 1; Tr->x2 = Flow->x2 - Tag->RelX(); } else { Tr->x2 += Tr->x1 - 1; Flow->cx = Tr->x2 + 1; } Tr->y2 += Tr->y1; Flow->y2 = MAX(Flow->y2, Tr->y2 + 1); Add(Tr); Flow->Insert(Tr, Align); Text += Tr->Len; if (Wrap) { while (*Text == ' ') Text++; } Tag->Size.x = MAX(Tag->Size.x, Tr->x2 + 1); Tag->Size.y = MAX(Tag->Size.y, Tr->y2 + 1); Flow->MAX.x = MAX(Flow->MAX.x, Tr->x2); Flow->MAX.y = MAX(Flow->MAX.y, Tr->y2); if (Tr->Len == 0) break; } SetFixedLength(true); } char16 htoi(char16 c) { if (c >= '0' && c <= '9') return c - '0'; if (c >= 'a' && c <= 'f') return c - 'a' + 10; if (c >= 'A' && c <= 'F') return c - 'A' + 10; LAssert(0); return 0; } bool LTag::Serialize(LXmlTag *t, bool Write) { LRect pos; if (Write) { // Obj -> Tag if (Tag) t->SetAttr("tag", Tag); pos.ZOff(Size.x, Size.y); pos.Offset(Pos.x, Pos.y); t->SetAttr("pos", pos.GetStr()); t->SetAttr("tagid", TagId); if (Txt) { LStringPipe p(256); for (char16 *c = Txt; *c; c++) { if (*c > ' ' && *c < 127 && !strchr("%<>\'\"", *c)) p.Print("%c", (char)*c); else p.Print("%%%.4x", *c); } LAutoString Tmp(p.NewStr()); t->SetContent(Tmp); } if (Props.Length()) { auto CssStyles = ToString(); LAssert(!strchr(CssStyles, '\"')); t->SetAttr("style", CssStyles); } if (Html->Cursor == this) { LAssert(Cursor >= 0); t->SetAttr("cursor", (int64)Cursor); } else LAssert(Cursor < 0); if (Html->Selection == this) { LAssert(Selection >= 0); t->SetAttr("selection", (int64)Selection); } else LAssert(Selection < 0); for (unsigned i=0; iInsertTag(child); if (!tag->Serialize(child, Write)) { return false; } } } else { // Tag -> Obj Tag.Reset(NewStr(t->GetAttr("tag"))); TagId = (HtmlTag) t->GetAsInt("tagid"); pos.SetStr(t->GetAttr("pos")); if (pos.Valid()) { Pos.x = pos.x1; Pos.y = pos.y1; Size.x = pos.x2; Size.y = pos.y2; } if (ValidStr(t->GetContent())) { LStringPipe p(256); char *c = t->GetContent(); SkipWhiteSpace(c); for (; *c && *c > ' '; c++) { char16 ch; if (*c == '%') { ch = 0; for (int i=0; i<4 && *c; i++) { ch <<= 4; ch |= htoi(*++c); } } else ch = *c; p.Write(&ch, sizeof(ch)); } Txt.Reset(p.NewStrW()); } const char *s = t->GetAttr("style"); if (s) Parse(s, ParseRelaxed); s = t->GetAttr("cursor"); if (s) { LAssert(Html->Cursor == NULL); Html->Cursor = this; Cursor = atoi(s); LAssert(Cursor >= 0); } s = t->GetAttr("selection"); if (s) { LAssert(Html->Selection == NULL); Html->Selection = this; Selection = atoi(s); LAssert(Selection >= 0); } #ifdef _DEBUG s = t->GetAttr("debug"); if (s && atoi(s) != 0) Debug = true; #endif for (int i=0; iChildren.Length(); i++) { LXmlTag *child = t->Children[i]; if (child->IsTag("e")) { LTag *tag = new LTag(Html, NULL); if (!tag) { LAssert(0); return false; } if (!tag->Serialize(child, Write)) { return false; } Attach(tag); } } } return true; } /* /// This method centers the text in the area given to the tag. Used for inline block elements. void LTag::CenterText() { if (!Parent) return; // Find the size of the text elements. int ContentPx = 0; for (unsigned i=0; iX(); } LFont *f = GetFont(); int ParentPx = ToTag(Parent)->Size.x; int AvailPx = Size.x; // Remove the border and padding from the content area AvailPx -= BorderLeft().ToPx(ParentPx, f); AvailPx -= BorderRight().ToPx(ParentPx, f); AvailPx -= PaddingLeft().ToPx(ParentPx, f); AvailPx -= PaddingRight().ToPx(ParentPx, f); if (AvailPx > ContentPx) { // Now offset all the regions to the right int OffPx = (AvailPx - ContentPx) >> 1; for (unsigned i=0; iOffset(OffPx, 0); } } } */ void LTag::OnFlow(LFlowRegion *Flow, uint16 Depth) { if (Depth >= MAX_RECURSION_DEPTH) return; DisplayType Disp = Display(); if (Disp == DispNone) return; LFont *f = GetFont(); LFlowRegion Local(*Flow); bool Restart = true; int BlockFlowWidth = 0; const char *ImgAltText = NULL; Size.x = 0; Size.y = 0; LCssTools Tools(this, f); LRect rc(Flow->X(), Html->Y()); PadPx = Tools.GetPadding(rc); if (TipId) { Html->Tip.DeleteTip(TipId); TipId = 0; } switch (TagId) { default: break; case TAG_BODY: { Flow->InBody++; break; } case TAG_IFRAME: { LFlowRegion Temp = *Flow; Flow->EndBlock(); Flow->Indent(this, MarginLeft(), MarginTop(), MarginRight(), MarginBottom(), true); // Flow children for (unsigned i=0; iOnFlow(&Temp, Depth + 1); if (TagId == TAG_TR) { Temp.x2 -= MIN(t->Size.x, Temp.X()); } } Flow->Outdent(this, MarginLeft(), MarginTop(), MarginRight(), MarginBottom(), true); BoundParents(); return; break; } case TAG_TR: { Size.x = Flow->X(); break; } case TAG_IMG: { Size.x = Size.y = 0; LCss::Len w = Width(); LCss::Len h = Height(); // LCss::Len MinX = MinWidth(); // LCss::Len MaxX = MaxWidth(); LCss::Len MinY = MinHeight(); LCss::Len MaxY = MaxHeight(); LAutoPtr a; int ImgX, ImgY; if (Image) { ImgX = Image->X(); ImgY = Image->Y(); } else if (Get("alt", ImgAltText) && ValidStr(ImgAltText)) { LDisplayString a(f, ImgAltText); ImgX = a.X() + 4; ImgY = a.Y() + 4; } else { ImgX = DefaultImgSize; ImgY = DefaultImgSize; } double AspectRatio = ImgY != 0 ? (double)ImgX / ImgY : 1.0; bool XLimit = false, YLimit = false; double Scale = 1.0; if (w.IsValid() && w.Type != LenAuto) { Size.x = Flow->ResolveX(w, this, false); XLimit = true; } else { int Fx = Flow->x2 - Flow->x1 + 1; if (ImgX > Fx) { Size.x = Fx; // * 0.8; if (Image) Scale = (double) Fx / ImgX; } else { Size.x = ImgX; } } XLimit |= Flow->LimitX(Size.x, MinWidth(), MaxWidth(), f); if (h.IsValid() && h.Type != LenAuto) { Size.y = Flow->ResolveY(h, this, false); YLimit = true; } else { Size.y = (int) (ImgY * Scale); } YLimit |= Flow->LimitY(Size.y, MinHeight(), MaxHeight(), f); if ( (XLimit ^ YLimit) && Image ) { if (XLimit) { Size.y = (int) ceil((double)Size.x / AspectRatio); } else { Size.x = (int) ceil((double)Size.y * AspectRatio); } } if (MinY.IsValid()) { int Px = Flow->ResolveY(MinY, this, false); if (Size.y < Px) Size.y = Px; } if (MaxY.IsValid()) { int Px = Flow->ResolveY(MaxY, this, false); if (Size.y > Px) Size.y = Px; } if (Disp == DispInline || Disp == DispInlineBlock) { Restart = false; if (Flow->cx > Flow->x1 && Size.x > Flow->X()) { Flow->FinishLine(); } Pos.y = Flow->y1; Flow->y2 = MAX(Flow->y1, Pos.y + Size.y - 1); LCss::LengthType a = GetAlign(true); switch (a) { case AlignCenter: { int Fx = Flow->x2 - Flow->x1; Pos.x = Flow->x1 + ((Fx - Size.x) / 2); break; } case AlignRight: { Pos.x = Flow->x2 - Size.x; break; } default: { Pos.x = Flow->cx; break; } } } break; } case TAG_HR: { Flow->FinishLine(); Pos.x = Flow->x1; Pos.y = Flow->y1 + 7; Size.x = Flow->X(); Size.y = 2; Flow->cx ++; Flow->y2 += 16; Flow->FinishLine(); return; break; } case TAG_TABLE: { Flow->EndBlock(); LCss::Len left = GetCssLen(MarginLeft, Margin); LCss::Len top = GetCssLen(MarginTop, Margin); LCss::Len right = GetCssLen(MarginRight, Margin); LCss::Len bottom = GetCssLen(MarginBottom, Margin); Flow->Indent(this, left, top, right, bottom, true); LayoutTable(Flow, Depth + 1); Flow->y1 += Size.y; Flow->y2 = Flow->y1; Flow->cx = Flow->x1; Flow->my = 0; Flow->MAX.y = MAX(Flow->MAX.y, Flow->y2); Flow->Outdent(this, left, top, right, bottom, true); BoundParents(); return; } } if (Disp == DispBlock || Disp == DispInlineBlock) { // This is a block level element, so end the previous non-block elements if (Disp == DispBlock) Flow->EndBlock(); #ifdef _DEBUG if (Debug) LgiTrace("Before %s\n", Flow->ToString().Get()); #endif BlockFlowWidth = Flow->X(); // Indent the margin... LCss::Len left = GetCssLen(MarginLeft, Margin); LCss::Len top = GetCssLen(MarginTop, Margin); LCss::Len right = GetCssLen(MarginRight, Margin); LCss::Len bottom = GetCssLen(MarginBottom, Margin); Flow->Indent(this, left, top, right, bottom, true); // Set the width if any LCss::Len Wid = Width(); if (!IsTableCell(TagId) && Wid.IsValid()) Size.x = Flow->ResolveX(Wid, this, false); else if (TagId != TAG_IMG) { if (Disp == DispInlineBlock) // Flow->Inline) Size.x = 0; // block inside inline-block default to fit the content else Size.x = Flow->X(); } else if (Disp == DispInlineBlock) Size.x = 0; if (MaxWidth().IsValid()) { int Px = Flow->ResolveX(MaxWidth(), this, false); if (Size.x > Px) Size.x = Px; } if (MinWidth().IsValid()) { int Px = Flow->ResolveX(MinWidth(), this, false); if (Size.x < Px) Size.x = Px; } Pos.x = Disp == DispInlineBlock ? Flow->cx : Flow->x1; Pos.y = Flow->y1; Flow->y1 -= Pos.y; Flow->y2 -= Pos.y; if (Disp == DispBlock) { Flow->x1 -= Pos.x; Flow->x2 = Flow->x1 + Size.x; Flow->cx -= Pos.x; Flow->Indent(this, LCss::BorderLeft(), LCss::BorderTop(), LCss::BorderRight(), LCss::BorderBottom(), false); Flow->Indent(PadPx, false); } else { Flow->x2 = Flow->X(); Flow->x1 = Flow->ResolveX(BorderLeft(), this, true) + Flow->ResolveX(PaddingLeft(), this, true); Flow->cx = Flow->x1; Flow->y1 += Flow->ResolveY(BorderTop(), this, true) + Flow->ResolveY(PaddingTop(), this, true); Flow->y2 = Flow->y1; if (!IsTableTag()) Flow->Inline++; } } else { Flow->Indent(PadPx, false); } if (f) { // Clear the previous text layout... TextPos.DeleteObjects(); switch (TagId) { default: break; case TAG_LI: { // Insert the list marker if (!PreText()) { LCss::ListStyleTypes s = Parent->ListStyleType(); if (s == ListInherit) { if (Parent->TagId == TAG_OL) s = ListDecimal; else if (Parent->TagId == TAG_UL) s = ListDisc; } switch (s) { default: break; case ListDecimal: { ssize_t Index = Parent->Children.IndexOf(this); char Txt[32]; sprintf_s(Txt, sizeof(Txt), "%i. ", (int)(Index + 1)); PreText(Utf8ToWide(Txt)); break; } case ListDisc: { PreText(NewStrW(LHtmlListItem)); break; } } } if (PreText()) TextPos.FlowText(this, Flow, f, f->GetHeight(), PreText(), AlignLeft); break; } case TAG_IMG: { if (Disp == DispBlock) { Flow->cx += Size.x; Flow->y2 += Size.y; } break; } } if (Text() && Flow->InBody) { // Setup the line height cache if (LineHeightCache < 0) { LCss::Len LineHt; LFont *LineFnt = GetFont(); for (LTag *t = this; t && !LineHt.IsValid(); t = ToTag(t->Parent)) { LineHt = t->LineHeight(); if (t->TagId == TAG_TABLE) break; } if (LineFnt) { int FontPx = LineFnt->GetHeight(); if (!LineHt.IsValid() || LineHt.Type == LCss::LenAuto || LineHt.Type == LCss::LenNormal) { LineHeightCache = FontPx; // LgiTrace("LineHeight FontPx=%i Px=%i Auto\n", FontPx, LineHeightCache); } else if (LineHt.Type == LCss::LenPx) { auto Scale = Html->GetDpiScale().y; LineHt.Value *= (float)Scale; LineHeightCache = LineHt.ToPx(FontPx, f); // LgiTrace("LineHeight FontPx=%i Px=%i (Scale=%f)\n", FontPx, LineHeightCache, Scale); } else { LineHeightCache = LineHt.ToPx(FontPx, f); // LgiTrace("LineHeight FontPx=%i Px=%i ToPx\n", FontPx, LineHeightCache); } } } // Flow in the rest of the text... char16 *Txt = Text(); LCss::LengthType Align = GetAlign(true); TextPos.FlowText(this, Flow, f, LineHeightCache, Txt, Align); #ifdef _DEBUG if (Debug) LgiTrace("%s:%i - %p.size=%p\n", _FL, this, &Size.x); #endif } } // Flow children PostFlowAlign.Length(0); for (unsigned i=0; iPosition()) { case PosStatic: case PosAbsolute: case PosFixed: { LFlowRegion old = *Flow; t->OnFlow(Flow, Depth + 1); // Try and reset the flow to how it was before... Flow->x1 = old.x1; Flow->x2 = old.x2; Flow->cx = old.cx; Flow->y1 = old.y1; Flow->y2 = old.y2; Flow->MAX.x = MAX(Flow->MAX.x, old.MAX.x); Flow->MAX.y = MAX(Flow->MAX.y, old.MAX.y); break; } default: { t->OnFlow(Flow, Depth + 1); break; } } if (TagId == TAG_TR) { Flow->x2 -= MIN(t->Size.x, Flow->X()); } } LCss::LengthType XAlign = GetAlign(true); int FlowSz = Flow->Width(); // Align the children... for (auto &group: PostFlowAlign) { int MinX = FlowSz, MaxX = 0; for (auto &a: group) { MinX = MIN(MinX, a.t->Pos.x); MaxX = MAX(MaxX, a.t->Pos.x + a.t->Size.x - 1); } int TotalX = MaxX - MinX + 1; int FirstX = group.Length() ? group[0].t->Pos.x : 0; for (auto &a: group) { if (a.XAlign == LCss::AlignCenter) { int OffX = (Size.x - TotalX) >> 1; if (OffX > 0) { a.t->Pos.x += OffX; } } else if (a.XAlign == LCss::AlignRight) { int OffX = FlowSz - FirstX - TotalX; if (OffX > 0) { a.t->Pos.x += OffX; } } } } if (Disp == DispBlock || Disp == DispInlineBlock) { LCss::Len Ht = Height(); LCss::Len MaxHt = MaxHeight(); // I dunno, there should be a better way... :-( if (MarginLeft().Type == LenAuto && MarginRight().Type == LenAuto) { XAlign = LCss::AlignCenter; } bool AcceptHt = !IsTableCell(TagId) && Ht.Type != LenPercent; if (AcceptHt) { if (Ht.IsValid()) { int HtPx = Flow->ResolveY(Ht, this, false); if (HtPx > Flow->y2) Flow->y2 = HtPx; } if (MaxHt.IsValid()) { int MaxHtPx = Flow->ResolveY(MaxHt, this, false); if (MaxHtPx < Flow->y2) { Flow->y2 = MaxHtPx; Flow->MAX.y = MIN(Flow->y2, Flow->MAX.y); } } } if (Disp == DispBlock) { Flow->EndBlock(); int OldFlowSize = Flow->x2 - Flow->x1 + 1; Flow->Outdent(this, PaddingLeft(), PaddingTop(), PaddingRight(), PaddingBottom(), false); Flow->Outdent(this, LCss::BorderLeft(), LCss::BorderTop(), LCss::BorderRight(), LCss::BorderBottom(), false); Size.y = Flow->y2 > 0 ? Flow->y2 : 0; Flow->Outdent(this, MarginLeft(), MarginTop(), MarginRight(), MarginBottom(), true); int NewFlowSize = Flow->x2 - Flow->x1 + 1; int Diff = NewFlowSize - OldFlowSize; if (Diff) Flow->MAX.x += Diff; Flow->y1 = Flow->y2; Flow->x2 = Flow->x1 + BlockFlowWidth; } else { LCss::Len Wid = Width(); int WidPx = Wid.IsValid() ? Flow->ResolveX(Wid, this, true) : 0; Size.x = MAX(WidPx, Size.x); Size.x += Flow->ResolveX(PaddingRight(), this, true); Size.x += Flow->ResolveX(BorderRight(), this, true); int MarginR = Flow->ResolveX(MarginRight(), this, true); int MarginB = Flow->ResolveX(MarginBottom(), this, true); Flow->x1 = Local.x1 - Pos.x; Flow->cx = Local.cx + Size.x + MarginR - Pos.x; Flow->x2 = Local.x2 - Pos.x; if (Height().IsValid()) { Size.y = Flow->ResolveY(Height(), this, false); Flow->y2 = MAX(Flow->y1 + Size.y + MarginB - 1, Flow->y2); } else { Flow->y2 += Flow->ResolveX(PaddingBottom(), this, true); Flow->y2 += Flow->ResolveX(BorderBottom(), this, true); Size.y = Flow->y2; } Flow->y1 = Local.y1 - Pos.y; Flow->y2 = MAX(Local.y2, Flow->y1+Size.y-1); if (!IsTableTag()) Flow->Inline--; } // Can't do alignment here because pos is used to // restart the parents flow region... } else { Flow->Outdent(PadPx, false); switch (TagId) { default: break; case TAG_SELECT: case TAG_INPUT: { if (Html->InThread() && Ctrl) { LRect r = Ctrl->GetPos(); if (Width().IsValid()) Size.x = Flow->ResolveX(Width(), this, false); else Size.x = r.X(); if (Height().IsValid()) Size.y = Flow->ResolveY(Height(), this, false); else Size.y = r.Y(); if (Html->IsAttached() && !Ctrl->IsAttached()) Ctrl->Attach(Html); } Flow->cx += Size.x; Flow->y2 = MAX(Flow->y2, Flow->y1 + Size.y - 1); break; } case TAG_IMG: { Flow->cx += Size.x; Flow->y2 = MAX(Flow->y2, Flow->y1 + Size.y - 1); break; } case TAG_BR: { int OldFlowY2 = Flow->y2; Flow->FinishLine(); Size.y = Flow->y2 - OldFlowY2; Flow->y2 = MAX(Flow->y2, Flow->y1 + Size.y - 1); break; } case TAG_CENTER: { int Px = Flow->X(); for (auto e: Children) { LTag *t = ToTag(e); if (t && t->IsBlock() && t->Size.x < Px) { t->Pos.x = (Px - t->Size.x) >> 1; } } break; } } } BoundParents(); if (Restart) { Flow->x1 += Pos.x; Flow->x2 += Pos.x; Flow->cx += Pos.x; Flow->y1 += Pos.y; Flow->y2 += Pos.y; Flow->MAX.y = MAX(Flow->MAX.y, Flow->y2); } if (Disp == DispBlock || Disp == DispInlineBlock) { if (XAlign == LCss::AlignCenter || XAlign == LCss::AlignRight) { int Match = 0; auto parent = ToTag(Parent); for (auto &grp: parent->PostFlowAlign) { bool Overlaps = false; for (auto &a: grp) { if (a.Overlap(this)) { Overlaps = true; break; } } if (!Overlaps) Match++; } auto &grp = parent->PostFlowAlign[Match]; if (grp.Length() == 0) { grp.x1 = Flow->x1; grp.x2 = Flow->x2; } auto &pf = grp.New(); pf.Disp = Disp; pf.XAlign = XAlign; pf.t = this; } } if (TagId == TAG_BODY && Flow->InBody > 0) { Flow->InBody--; } } bool LTag::PeekTag(char *s, char *tag) { bool Status = false; if (s && tag) { if (*s == '<') { char *t = 0; Html->ParseName(++s, &t); if (t) { Status = _stricmp(t, tag) == 0; } DeleteArray(t); } } return Status; } LTag *LTag::GetTable() { LTag *t = 0; for (t=ToTag(Parent); t && t->TagId != TAG_TABLE; t = ToTag(t->Parent)) ; return t; } void LTag::BoundParents() { if (!Parent) return; LTag *np; for (LTag *n=this; n; n = np) { np = ToTag(n->Parent); if (!np || np->TagId == TAG_IFRAME) break; np->Size.x = MAX(np->Size.x, n->Pos.x + n->Size.x); np->Size.y = MAX(np->Size.y, n->Pos.y + n->Size.y); } } struct DrawBorder { LSurface *pDC; uint32_t LineStyle; uint32_t LineReset; uint32_t OldStyle; DrawBorder(LSurface *pdc, LCss::BorderDef &d) { LineStyle = 0xffffffff; LineReset = 0x80000000; if (d.Style == LCss::BorderDotted) { switch ((int)d.Value) { case 2: { LineStyle = 0xcccccccc; break; } case 3: { LineStyle = 0xe38e38; LineReset = 0x800000; break; } case 4: { LineStyle = 0xf0f0f0f0; break; } case 5: { LineStyle = 0xf83e0; LineReset = 0x80000; break; } case 6: { LineStyle = 0xfc0fc0; LineReset = 0x800000; break; } case 7: { LineStyle = 0xfe03f80; LineReset = 0x8000000; break; } case 8: { LineStyle = 0xff00ff00; break; } case 9: { LineStyle = 0x3fe00; LineReset = 0x20000; break; } default: { LineStyle = 0xaaaaaaaa; break; } } } pDC = pdc; OldStyle = pDC->LineStyle(); } ~DrawBorder() { pDC->LineStyle(OldStyle); } }; void LTag::GetInlineRegion(LRegion &rgn, int ox, int oy) { if (TagId == TAG_IMG) { LRect rc(0, 0, Size.x-1, Size.y-1); rc.Offset(ox + Pos.x, oy + Pos.y); rgn.Union(&rc); } else { for (unsigned i=0; iGetInlineRegion(rgn, ox + Pos.x, oy + Pos.y); } } class CornersImg : public LMemDC { public: int Px, Px2; CornersImg( float RadPx, LRect *BorderPx, LCss::BorderDef **defs, LColour &Back, bool DrawBackground) { Px = 0; Px2 = 0; //Radius.Type != LCss::LenInherit && if (RadPx > 0.0f) { Px = (int)ceil(RadPx); Px2 = Px << 1; if (Create(Px2, Px2, System32BitColourSpace)) { #if 1 Colour(0, 32); #else Colour(LColour(255, 0, 255)); #endif Rectangle(); LPointF ctr(Px, Px); LPointF LeftPt(0.0, Px); LPointF TopPt(Px, 0.0); LPointF RightPt(X(), Px); LPointF BottomPt(Px, Y()); int x_px[4] = {BorderPx->x1, BorderPx->x2, BorderPx->x2, BorderPx->x1}; int y_px[4] = {BorderPx->y1, BorderPx->y1, BorderPx->y2, BorderPx->y2}; LPointF *pts[4] = {&LeftPt, &TopPt, &RightPt, &BottomPt}; // Draw border parts.. for (int i=0; i<4; i++) { int k = (i + 1) % 4; // Setup the stops LBlendStop stops[2] = { {0.0, 0}, {1.0, 0} }; uint32_t iColour = defs[i]->Color.IsValid() ? defs[i]->Color.Rgb32 : Back.c32(); uint32_t kColour = defs[k]->Color.IsValid() ? defs[k]->Color.Rgb32 : Back.c32(); if (defs[i]->IsValid() && defs[k]->IsValid()) { stops[0].c32 = iColour; stops[1].c32 = kColour; } else if (defs[i]->IsValid()) { stops[0].c32 = stops[1].c32 = iColour; } else { stops[0].c32 = stops[1].c32 = kColour; } // Create a brush LLinearBlendBrush br ( *pts[i], *pts[k], 2, stops ); // Setup the clip LRect clip( (int)MIN(pts[i]->x, pts[k]->x), (int)MIN(pts[i]->y, pts[k]->y), (int)MAX(pts[i]->x, pts[k]->x)-1, (int)MAX(pts[i]->y, pts[k]->y)-1); ClipRgn(&clip); // Draw the arc... LPath p; p.Circle(ctr, Px); if (defs[i]->IsValid() || defs[k]->IsValid()) p.Fill(this, br); // Fill the background p.Empty(); p.Ellipse(ctr, Px-x_px[i], Px-y_px[i]); if (DrawBackground) { LSolidBrush br(Back); p.Fill(this, br); } else { LEraseBrush br; p.Fill(this, br); } ClipRgn(NULL); } #ifdef MAC ConvertPreMulAlpha(true); #endif #if 0 static int count = 0; LString file; file.Printf("c:\\temp\\img-%i.bmp", ++count); GdcD->Save(file, Corners); #endif } } } }; void LTag::PaintBorderAndBackground(LSurface *pDC, LColour &Back, LRect *BorderPx) { LArray r; LRect BorderPxRc; bool DrawBackground = !Back.IsTransparent(); #ifdef _DEBUG if (Debug) { //int asd=0; } #endif if (!BorderPx) BorderPx = &BorderPxRc; BorderPx->ZOff(0, 0); // Get all the border info and work out the pixel sizes. LFont *f = GetFont(); #define DoEdge(coord, axis, name) \ BorderDef name = Border##name(); \ BorderPx->coord = name.Style != LCss::BorderNone ? name.ToPx(Size.axis, f) : 0; #define BorderValid(name) \ ((name).IsValid() && (name).Style != LCss::BorderNone) DoEdge(x1, x, Left); DoEdge(y1, y, Top); DoEdge(x2, x, Right); DoEdge(y2, y, Bottom); LCss::BorderDef *defs[4] = {&Left, &Top, &Right, &Bottom}; if (BorderValid(Left) || BorderValid(Right) || BorderValid(Top) || BorderValid(Bottom) || DrawBackground) { // Work out the rectangles switch (Display()) { case DispInlineBlock: case DispBlock: { r[0].ZOff(Size.x-1, Size.y-1); break; } case DispInline: { LRegion rgn; GetInlineRegion(rgn); if (BorderPx) { for (int i=0; ix1 -= BorderPx->x1 + PadPx.x1; r->y1 -= BorderPx->y1 + PadPx.y1; r->x2 += BorderPx->x2 + PadPx.x2; r->y2 += BorderPx->y2 + PadPx.y2; } } r.Length(rgn.Length()); auto p = r.AddressOf(); for (auto i = rgn.First(); i; i = rgn.Next()) *p++ = *i; break; } default: return; } // If we are drawing rounded corners, draw them into a memory context LAutoPtr Corners; int Px = 0, Px2 = 0; LCss::Len Radius = BorderRadius(); float RadPx = Radius.Type == LCss::LenPx ? Radius.Value : Radius.ToPx(Size.x, GetFont()); bool HasRadius = Radius.Type != LCss::LenInherit && RadPx > 0.0f; // Loop over the rectangles and draw everything int Op = pDC->Op(GDC_ALPHA); for (unsigned i=0; i rc.Y()) { Px = rc.Y() / 2; Px2 = Px << 1; } if (!Corners || Corners->Px2 != Px2) { Corners.Reset(new CornersImg((float)Px, BorderPx, defs, Back, DrawBackground)); } // top left LRect r(0, 0, Px-1, Px-1); pDC->Blt(rc.x1, rc.y1, Corners, &r); // top right r.Set(Px, 0, Corners->X()-1, Px-1); pDC->Blt(rc.x2-Px+1, rc.y1, Corners, &r); // bottom left r.Set(0, Px, Px-1, Corners->Y()-1); pDC->Blt(rc.x1, rc.y2-Px+1, Corners, &r); // bottom right r.Set(Px, Px, Corners->X()-1, Corners->Y()-1); pDC->Blt(rc.x2-Px+1, rc.y2-Px+1, Corners, &r); #if 1 pDC->Colour(Back); pDC->Rectangle(rc.x1+Px, rc.y1, rc.x2-Px, rc.y2); pDC->Rectangle(rc.x1, rc.y1+Px, rc.x1+Px-1, rc.y2-Px); pDC->Rectangle(rc.x2-Px+1, rc.y1+Px, rc.x2, rc.y2-Px); #else pDC->Colour(LColour(255, 0, 0, 0x80)); pDC->Rectangle(rc.x1+Px, rc.y1, rc.x2-Px, rc.y2); pDC->Colour(LColour(0, 255, 0, 0x80)); pDC->Rectangle(rc.x1, rc.y1+Px, rc.x1+Px-1, rc.y2-Px); pDC->Colour(LColour(0, 0, 255, 0x80)); pDC->Rectangle(rc.x2-Px+1, rc.y1+Px, rc.x2, rc.y2-Px); #endif } else if (DrawBackground) { pDC->Colour(Back); pDC->Rectangle(&rc); } LCss::BorderDef *b; if ((b = &Left) && BorderValid(*b)) { pDC->Colour(b->Color.Rgb32, 32); DrawBorder db(pDC, *b); for (int i=0; iValue; i++) { pDC->LineStyle(db.LineStyle, db.LineReset); pDC->Line(rc.x1 + i, rc.y1+Px, rc.x1+i, rc.y2-Px); } } if ((b = &Top) && BorderValid(*b)) { pDC->Colour(b->Color.Rgb32, 32); DrawBorder db(pDC, *b); for (int i=0; iValue; i++) { pDC->LineStyle(db.LineStyle, db.LineReset); pDC->Line(rc.x1+Px, rc.y1+i, rc.x2-Px, rc.y1+i); } } if ((b = &Right) && BorderValid(*b)) { pDC->Colour(b->Color.Rgb32, 32); DrawBorder db(pDC, *b); for (int i=0; iValue; i++) { pDC->LineStyle(db.LineStyle, db.LineReset); pDC->Line(rc.x2-i, rc.y1+Px, rc.x2-i, rc.y2-Px); } } if ((b = &Bottom) && BorderValid(*b)) { pDC->Colour(b->Color.Rgb32, 32); DrawBorder db(pDC, *b); for (int i=0; iValue; i++) { pDC->LineStyle(db.LineStyle, db.LineReset); pDC->Line(rc.x1+Px, rc.y2-i, rc.x2-Px, rc.y2-i); } } } pDC->Op(Op); } } static void FillRectWithImage(LSurface *pDC, LRect *r, LSurface *Image, LCss::RepeatType Repeat) { int Px = 0, Py = 0; int Old = pDC->Op(GDC_ALPHA); if (!Image) return; switch (Repeat) { default: case LCss::RepeatBoth: { for (int y=0; yY(); y += Image->Y()) { for (int x=0; xX(); x += Image->X()) { pDC->Blt(Px + x, Py + y, Image); } } break; } case LCss::RepeatX: { for (int x=0; xX(); x += Image->X()) { pDC->Blt(Px + x, Py, Image); } break; } case LCss::RepeatY: { for (int y=0; yY(); y += Image->Y()) { pDC->Blt(Px, Py + y, Image); } break; } case LCss::RepeatNone: { pDC->Blt(Px, Py, Image); break; } } pDC->Op(Old); } void LTag::OnPaint(LSurface *pDC, bool &InSelection, uint16 Depth) { if (Depth >= MAX_RECURSION_DEPTH || Display() == DispNone) return; if ( #ifdef _DEBUG !Html->_Debug && #endif LCurrentTime() - Html->PaintStart > Html->d->MaxPaintTime) { Html->d->MaxPaintTimeout = true; return; } int Px, Py; pDC->GetOrigin(Px, Py); #if 0 if (Debug) { Gtk::cairo_matrix_t mx; Gtk::cairo_get_matrix(pDC->Handle(), &mx); LPoint Offset; Html->WindowVirtualOffset(&Offset); LRect cli; pDC->GetClient(&cli); printf("\tTag paint mx=%g,%g off=%i,%i p=%i,%i Pos=%i,%i cli=%s\n", mx.x0, mx.y0, Offset.x, Offset.y, Px, Py, Pos.x, Pos.y, cli.GetStr()); } #endif switch (TagId) { case TAG_INPUT: case TAG_SELECT: { if (Ctrl) { int64 Sx = 0, Sy = 0; int64 LineY = GetFont()->GetHeight(); Html->GetScrollPos(Sx, Sy); Sx *= LineY; Sy *= LineY; LRect r(0, 0, Size.x-1, Size.y-1), Px; LColour back = _Colour(false); PaintBorderAndBackground(pDC, back, &Px); if (!dynamic_cast(Ctrl)) { r.x1 += Px.x1; r.y1 += Px.y1; r.x2 -= Px.x2; r.y2 -= Px.y2; } r.Offset(AbsX() - (int)Sx, AbsY() - (int)Sy); Ctrl->SetPos(r); } if (TagId == TAG_SELECT) return; break; } case TAG_BODY: { auto b = _Colour(false); if (!b.IsTransparent()) { pDC->Colour(b); pDC->Rectangle(Pos.x, Pos.y, Pos.x+Size.x, Pos.y+Size.y); } if (Image) { LRect r; r.ZOff(Size.x-1, Size.y-1); FillRectWithImage(pDC, &r, Image, BackgroundRepeat()); } break; } case TAG_HEAD: { // Nothing under here to draw. return; } case TAG_HR: { pDC->Colour(L_MED); pDC->Rectangle(0, 0, Size.x - 1, Size.y - 1); break; } case TAG_TR: case TAG_TBODY: case TAG_META: { // Draws nothing... break; } case TAG_IMG: { LRect Clip(0, 0, Size.x-1, Size.y-1); pDC->ClipRgn(&Clip); if (Image) { #if ENABLE_IMAGE_RESIZING if ( !ImageResized && ( Size.x != Image->X() || Size.y != Image->Y() ) ) { ImageResized = true; LColourSpace Cs = Image->GetColourSpace(); if (Cs == CsIndex8 && Image->AlphaDC()) Cs = System32BitColourSpace; LAutoPtr r(new LMemDC(Size.x, Size.y, Cs)); if (r) { if (Cs == CsIndex8) r->Palette(new LPalette(Image->Palette())); ResampleDC(r, Image); Image = r; } } #endif int Old = pDC->Op(GDC_ALPHA); pDC->Blt(0, 0, Image); pDC->Op(Old); } else if (Size.x > 1 && Size.y > 1) { LRect b(0, 0, Size.x-1, Size.y-1); LColour Fill(LColour(L_MED).Mix(LColour(L_LIGHT), 0.2f)); LColour Border(L_MED); // Border pDC->Colour(Border); pDC->Box(&b); b.Inset(1, 1); pDC->Box(&b); b.Inset(1, 1); pDC->Colour(Fill); pDC->Rectangle(&b); const char *Alt; LColour Red(LColour(255, 0, 0).Mix(Fill, 0.3f)); if (Get("alt", Alt) && ValidStr(Alt)) { LDisplayString Ds(Html->GetFont(), Alt); Html->GetFont()->Colour(Red, Fill); Ds.Draw(pDC, 2, 2, &b); } else if (Size.x >= 16 && Size.y >= 16) { // Red 'x' int Cx = b.x1 + (b.X()/2); int Cy = b.y1 + (b.Y()/2); LRect c(Cx-4, Cy-4, Cx+4, Cy+4); pDC->Colour(Red); pDC->Line(c.x1, c.y1, c.x2, c.y2); pDC->Line(c.x1, c.y2, c.x2, c.y1); pDC->Line(c.x1, c.y1 + 1, c.x2 - 1, c.y2); pDC->Line(c.x1 + 1, c.y1, c.x2, c.y2 - 1); pDC->Line(c.x1 + 1, c.y2, c.x2, c.y1 + 1); pDC->Line(c.x1, c.y2 - 1, c.x2 - 1, c.y1); } } pDC->ClipRgn(0); break; } default: { LColour fore = _Colour(true); LColour back = _Colour(false); if (Display() == DispBlock && Html->Environment) { LCss::ImageDef Img = BackgroundImage(); if (Img.Img) { LRect Clip(0, 0, Size.x-1, Size.y-1); pDC->ClipRgn(&Clip); FillRectWithImage(pDC, &Clip, Img.Img, BackgroundRepeat()); pDC->ClipRgn(NULL); back.Empty(); } } PaintBorderAndBackground(pDC, back, NULL); LFont *f = GetFont(); #if DEBUG_TEXT_AREA bool IsEditor = Html ? !Html->GetReadOnly() : false; #else bool IsEditor = false; #endif if (f && TextPos.Length()) { // This is the non-display part of the font bounding box int LeadingPx = (int)(f->Leading() + 0.5); // This is the displayable part of the font int FontPx = f->GetHeight() - LeadingPx; // This is the pixel height we're aiming to fill int EffectiveLineHt = LineHeightCache >= 0 ? MAX(FontPx, LineHeightCache) : FontPx; // This gets added to the y coord of each piece of text int LineHtOff = ((EffectiveLineHt - FontPx + 1) >> 1) - LeadingPx; #define FontColour(InSelection) \ f->Transparent(!InSelection && !IsEditor); \ if (InSelection) \ f->Colour(L_FOCUS_SEL_FORE, L_FOCUS_SEL_BACK); \ else \ { \ LColour bk(back.IsTransparent() ? LColour(L_WORKSPACE) : back); \ LColour fr(fore.IsTransparent() ? LColour(DefaultTextColour) : fore); \ if (IsEditor) \ bk = bk.Mix(LColour::Black, 0.05f); \ f->Colour(fr, bk); \ } if (Html->HasSelection() && (Selection >= 0 || Cursor >= 0) && Selection != Cursor) { ssize_t Min = -1; ssize_t Max = -1; ssize_t Base = GetTextStart(); if (Cursor >= 0 && Selection >= 0) { Min = MIN(Cursor, Selection) + Base; Max = MAX(Cursor, Selection) + Base; } else if (InSelection) { Max = MAX(Cursor, Selection) + Base; } else { Min = MAX(Cursor, Selection) + Base; } LRect CursorPos; CursorPos.ZOff(-1, -1); for (unsigned i=0; iText - Text(); ssize_t Done = 0; int x = Tr->x1; if (Tr->Len == 0) { // Is this a selection edge point? if (!InSelection && Min == 0) { InSelection = !InSelection; } else if (InSelection && Max == 0) { InSelection = !InSelection; } if (Cursor >= 0) { // Is this the cursor, then draw it and save it's position if (Cursor == Start + Done - Base) { Html->d->CursorPos.Set(x, Tr->y1 + LineHtOff, x + 1, Tr->y2 - LineHtOff); if (Html->d->CursorPos.x1 > Tr->x2) Html->d->CursorPos.Offset(Tr->x2 - Html->d->CursorPos.x1, 0); CursorPos = Html->d->CursorPos; Html->d->CursorPos.Offset(AbsX(), AbsY()); } } break; } while (Done < Tr->Len) { ssize_t c = Tr->Len - Done; FontColour(InSelection); // Is this a selection edge point? if ( !InSelection && Min - Start >= Done && Min - Start < Done + Tr->Len) { InSelection = !InSelection; c = Min - Start - Done; } else if ( InSelection && Max - Start >= Done && Max - Start <= Tr->Len) { InSelection = !InSelection; c = Max - Start - Done; } // Draw the text run LDisplayString ds(f, Tr->Text + Done, c); if (IsEditor) { LRect r(x, Tr->y1, x + ds.X() - 1, Tr->y2); ds.Draw(pDC, x, Tr->y1 + LineHtOff, &r); } else { ds.Draw(pDC, x, Tr->y1 + LineHtOff); } x += ds.X(); Done += c; // Is this is end of the tag? if (Tr->Len == Done) { // Is it also a selection edge? if ( !InSelection && Min - Start == Done) { InSelection = !InSelection; } else if ( InSelection && Max - Start == Done) { InSelection = !InSelection; } } if (Cursor >= 0) { // Is this the cursor, then draw it and save it's position if (Cursor == Start + Done - Base) { Html->d->CursorPos.Set(x, Tr->y1 + LineHtOff, x + 1, Tr->y2 - LineHtOff); if (Html->d->CursorPos.x1 > Tr->x2) Html->d->CursorPos.Offset(Tr->x2 - Html->d->CursorPos.x1, 0); CursorPos = Html->d->CursorPos; Html->d->CursorPos.Offset(AbsX(), AbsY()); } } } } if (Html->d->CursorVis && CursorPos.Valid()) { pDC->Colour(L_TEXT); pDC->Rectangle(&CursorPos); } } else if (Cursor >= 0) { FontColour(InSelection); ssize_t Base = GetTextStart(); for (unsigned i=0; iText - Text()) - Base; LAssert(Tr->y2 >= Tr->y1); LDisplayString ds(f, Tr->Text, Tr->Len); ds.Draw(pDC, Tr->x1, Tr->y1 + LineHtOff, IsEditor ? Tr : NULL); if ( ( Tr->Text == PreText() && !ValidStrW(Text()) ) || ( Cursor >= Pos && Cursor <= Pos + Tr->Len ) ) { ssize_t Off = Tr->Text == PreText() ? StrlenW(PreText()) : Cursor - Pos; pDC->Colour(L_TEXT); LRect c; if (Off) { LDisplayString ds(f, Tr->Text, Off); int x = ds.X(); if (x >= Tr->X()) x = Tr->X()-1; c.Set(Tr->x1 + x, Tr->y1, Tr->x1 + x + 1, Tr->y1 + f->GetHeight()); } else { c.Set(Tr->x1, Tr->y1, Tr->x1 + 1, Tr->y1 + f->GetHeight()); } Html->d->CursorPos = c; if (Html->d->CursorVis) pDC->Rectangle(&c); Html->d->CursorPos.Offset(AbsX(), AbsY()); } } } else { FontColour(InSelection); for (auto &Tr: TextPos) { LDisplayString ds(f, Tr->Text, Tr->Len); ds.Draw(pDC, Tr->x1, Tr->y1 + LineHtOff, IsEditor ? Tr : NULL); } } } break; } } #if DEBUG_TABLE_LAYOUT && 0 if (IsTableCell(TagId)) { LTag *Tbl = this; while (Tbl->TagId != TAG_TABLE && Tbl->Parent) Tbl = Tbl->Parent; if (Tbl && Tbl->TagId == TAG_TABLE && Tbl->Debug) { pDC->Colour(LColour(255, 0, 0)); pDC->Box(0, 0, Size.x-1, Size.y-1); } } #endif for (unsigned i=0; iSetOrigin(Px - t->Pos.x, Py - t->Pos.y); t->OnPaint(pDC, InSelection, Depth + 1); pDC->SetOrigin(Px, Py); } #if DEBUG_DRAW_TD if (TagId == TAG_TD) { LTag *Tbl = this; while (Tbl && Tbl->TagId != TAG_TABLE) Tbl = ToTag(Tbl->Parent); if (Tbl && Tbl->Debug) { int Ls = pDC->LineStyle(LSurface::LineDot); pDC->Colour(LColour::Blue); pDC->Box(0, 0, Size.x-1, Size.y-1); pDC->LineStyle(Ls); } } #endif } ////////////////////////////////////////////////////////////////////// LHtml::LHtml(int id, int x, int y, int cx, int cy, LDocumentEnv *e) : LDocView(e), ResObject(Res_Custom), LHtmlParser(NULL) { View = this; d = new LHtmlPrivate; SetReadOnly(true); ViewWidth = -1; SetId(id); LRect r(x, y, x+cx, y+cy); SetPos(r); Cursor = 0; Selection = 0; DocumentUid = 0; _New(); } LHtml::~LHtml() { _Delete(); DeleteObj(d); if (JobSem.Lock(_FL)) { JobSem.Jobs.DeleteObjects(); JobSem.Unlock(); } } void LHtml::_New() { d->StyleDirty = false; d->IsLoaded = false; d->Content.x = d->Content.y = 0; d->DeferredLoads = 0; Tag = 0; DocCharSet.Reset(); IsHtml = true; #ifdef DefaultFont LFont *Def = new LFont; if (Def) { if (Def->CreateFromCss(DefaultFont)) SetFont(Def, true); else DeleteObj(Def); } #endif FontCache = new LFontCache(this); SetScrollBars(false, false); } void LHtml::_Delete() { LAssert(!d->IsParsing); CssStore.Empty(); CssHref.Empty(); OpenTags.Length(0); Source.Reset(); DeleteObj(Tag); DeleteObj(FontCache); } LFont *LHtml::DefFont() { return GetFont(); } void LHtml::OnAddStyle(const char *MimeType, const char *Styles) { if (Styles) { const char *c = Styles; bool Status = CssStore.Parse(c); if (Status) { d->StyleDirty = true; } #if 0 // def _DEBUG bool LogCss = false; if (!Status) { char p[MAX_PATH_LEN]; sprintf_s(p, sizeof(p), "c:\\temp\\css_parse_failure_%i.txt", LRand()); LFile f; if (f.Open(p, O_WRITE)) { f.SetSize(0); if (CssStore.Error) f.Print("Error: %s\n\n", CssStore.Error.Get()); f.Write(Styles, strlen(Styles)); f.Close(); } } if (LogCss) { LStringPipe p; CssStore.Dump(p); LAutoString a(p.NewStr()); LFile f; if (f.Open("C:\\temp\\css.txt", O_WRITE)) { f.Write(a, strlen(a)); f.Close(); } } #endif } } void LHtml::ParseDocument(const char *Doc) { if (!Tag) { Tag = new LTag(this, 0); } if (GetCss()) GetCss()->DeleteProp(LCss::PropBackgroundColor); if (Tag) { Tag->TagId = ROOT; OpenTags.Length(0); if (IsHtml) { Parse(Tag, Doc); // Add body tag if not specified... LTag *Html = Tag->GetTagByName("html"); LTag *Body = Tag->GetTagByName("body"); if (!Html && !Body) { if ((Html = new LTag(this, 0))) Html->SetTag("html"); if ((Body = new LTag(this, Html))) Body->SetTag("body"); Html->Attach(Body); if (Tag->Text()) { LTag *Content = new LTag(this, Body); if (Content) { Content->TagId = CONTENT; Content->Text(NewStrW(Tag->Text())); } } while (Tag->Children.Length()) { LTag *t = ToTag(Tag->Children.First()); Body->Attach(t, Body->Children.Length()); } DeleteObj(Tag); Tag = Html; } else if (!Body) { if ((Body = new LTag(this, Html))) Body->SetTag("body"); for (unsigned i=0; iChildren.Length(); i++) { LTag *t = ToTag(Html->Children[i]); if (t->TagId != TAG_HEAD) { Body->Attach(t); i--; } } Html->Attach(Body); } if (Html && Body) { char16 *t = Tag->Text(); if (t) { if (ValidStrW(t)) { LTag *Content = new LTag(this, 0); if (Content) { Content->Text(NewStrW(Tag->Text())); Body->Attach(Content, 0); } } Tag->Text(0); } #if 0 // Enabling this breaks the test file 'gw2.html'. for (LTag *t = Html->Tags.First(); t; ) { if (t->Tag && t->Tag[0] == '!') { Tag->Attach(t, 0); t = Html->Tags.Current(); } else if (t->TagId != TAG_HEAD && t != Body) { if (t->TagId == TAG_HTML) { LTag *c; while ((c = t->Tags.First())) { Html->Attach(c, 0); } t->Detach(); DeleteObj(t); } else { t->Detach(); Body->Attach(t); } t = Html->Tags.Current(); } else { t = Html->Tags.Next(); } } #endif if (Environment) { const char *OnLoad; if (Body->Get("onload", OnLoad)) { Environment->OnExecuteScript(this, (char*)OnLoad); } } } } else { Tag->ParseText(Source); } } ViewWidth = -1; if (Tag) Tag->ResetCaches(); Invalidate(); } bool LHtml::NameW(const char16 *s) { LAutoPtr utf(WideToUtf8(s)); return Name(utf); } const char16 *LHtml::NameW() { LBase::Name(Source); return LBase::NameW(); } bool LHtml::Name(const char *s) { int Uid = -1; if (Environment) Uid = Environment->NextUid(); if (Uid < 0) Uid = GetDocumentUid() + 1; SetDocumentUid(Uid); _Delete(); _New(); IsHtml = false; // Detect HTML const char *c = s; while ((c = strchr(c, '<'))) { char *t = 0; c = ParseName((char*) ++c, &t); if (t && GetTagInfo(t)) { DeleteArray(t); IsHtml = true; break; } DeleteArray(t); } // Parse d->IsParsing = true; ParseDocument(s); d->IsParsing = false; if (Tag && d->StyleDirty) { d->StyleDirty = false; Tag->RestyleAll(); } if (d->DeferredLoads == 0) { OnLoad(); } Invalidate(); return true; } const char *LHtml::Name() { if (!Source && Tag) { LStringPipe s(1024); Tag->CreateSource(s); Source.Reset(s.NewStr()); } return Source; } LMessage::Result LHtml::OnEvent(LMessage *Msg) { switch (Msg->Msg()) { case M_COPY: { Copy(); break; } case M_JOBS_LOADED: { bool Update = false; int InitDeferredLoads = d->DeferredLoads; if (JobSem.Lock(_FL)) { for (unsigned i=0; iUserData); if (j->UserUid == MyUid && j->UserData != NULL) { Html1::LTag *r = static_cast(j->UserData); if (d->DeferredLoads > 0) d->DeferredLoads--; // Check the tag is still in our tree... if (Tag->HasChild(r)) { // Process the returned data... if (r->TagId == TAG_IMG) { if (j->pDC) { r->SetImage(j->Uri, j->pDC.Release()); ViewWidth = 0; Update = true; } else if (j->Stream) { LAutoPtr pDC(GdcD->Load(dynamic_cast(j->Stream.Get()))); if (pDC) { r->SetImage(j->Uri, pDC.Release()); ViewWidth = 0; Update = true; } else LgiTrace("%s:%i - Image decode failed for '%s'\n", _FL, j->Uri.Get()); } else if (j->Status == LDocumentEnv::LoadJob::JobOk) LgiTrace("%s:%i - Unexpected job type for '%s'\n", _FL, j->Uri.Get()); } else if (r->TagId == TAG_LINK) { if (!CssHref.Find(j->Uri)) { LStreamI *s = j->GetStream(); if (s) { s->ChangeThread(); int Size = (int)s->GetSize(); LAutoString Style(new char[Size+1]); ssize_t rd = s->Read(Style, Size); if (rd > 0) { Style[rd] = 0; CssHref.Add(j->Uri, true); OnAddStyle("text/css", Style); ViewWidth = 0; Update = true; } } } } else if (r->TagId == TAG_IFRAME) { // Remote IFRAME loading not support for security reasons. } else LgiTrace("%s:%i - Unexpected tag '%s' for URI '%s'\n", _FL, r->Tag.Get(), j->Uri.Get()); } else { /* Html1::LTag *p = ToTag(r->Parent); while (p && p->Parent) p = ToTag(p->Parent); */ LgiTrace("%s:%i - No child tag for job.\n", _FL); } } // else it's from another (historical) HTML control, ignore } JobSem.Jobs.DeleteObjects(); JobSem.Unlock(); } if (InitDeferredLoads > 0 && d->DeferredLoads <= 0) { LAssert(d->DeferredLoads == 0); d->DeferredLoads = 0; OnLoad(); } if (Update) { OnPosChange(); Invalidate(); } break; } } return LDocView::OnEvent(Msg); } int LHtml::OnNotify(LViewI *c, LNotification n) { switch (c->GetId()) { case IDC_VSCROLL: { int LineY = GetFont()->GetHeight(); if (Tag) Tag->ClearToolTips(); if (n.Type == LNotifyScrollBarCreate && VScroll && LineY > 0) { int y = Y(); int p = MAX(y / LineY, 1); int fy = d->Content.y / LineY; VScroll->SetPage(p); VScroll->SetRange(fy); } Invalidate(); break; } default: { LTag *Ctrl = Tag ? Tag->FindCtrlId(c->GetId()) : NULL; if (Ctrl) return Ctrl->OnNotify(n); break; } } return LLayout::OnNotify(c, n); } void LHtml::OnPosChange() { LLayout::OnPosChange(); if (ViewWidth != X()) { Invalidate(); } } bool LHtml::OnLayout(LViewLayoutInfo &Inf) { if (!Inf.Width.Min) { Inf.Width.Min = Inf.FILL; Inf.Width.Max = Inf.FILL; } else { Inf.Height.Min = Inf.FILL; Inf.Height.Max = Inf.FILL; } return true; } LPoint LHtml::Layout(bool ForceLayout) { LRect Client = GetClient(); if (Tag && (ViewWidth != Client.X() || ForceLayout)) { LFlowRegion f(this, Client, false); // Flow text, width is different Tag->OnFlow(&f, 0); ViewWidth = Client.X(); d->Content.x = f.MAX.x + 1; d->Content.y = f.MAX.y + 1; // Set up scroll box bool Sy = f.y2 > Y(); int LineY = GetFont()->GetHeight(); uint64 Now = LCurrentTime(); if (Now - d->SetScrollTime > 100) { d->SetScrollTime = Now; SetScrollBars(false, Sy); if (Sy && VScroll && LineY > 0) { int y = Y(); int p = MAX(y / LineY, 1); int fy = f.y2 / LineY; VScroll->SetPage(p); VScroll->SetRange(fy); } } else { // LgiTrace("%s - Dropping SetScroll, loop detected: %i ms\n", GetClass(), (int)(Now - d->SetScrollTime)); } } return d->Content; } LPointF LHtml::GetDpiScale() { LPointF Scale(1.0, 1.0); auto Wnd = GetWindow(); if (Wnd) Scale = Wnd->GetDpiScale(); return Scale; } void LHtml::OnPaint(LSurface *ScreenDC) { // LProfile Prof("LHtml::OnPaint"); #if HTML_USE_DOUBLE_BUFFER LRect Client = GetClient(); if (ScreenDC->IsScreen()) { if (!MemDC || (MemDC->X() < Client.X() || MemDC->Y() < Client.Y())) { if (MemDC.Reset(new LMemDC)) { int Sx = Client.X() + 10; int Sy = Client.Y() + 10; if (!MemDC->Create(Sx, Sy, System32BitColourSpace)) { MemDC.Reset(); } } } if (MemDC) { MemDC->ClipRgn(NULL); #if 0//def _DEBUG MemDC->Colour(LColour(255, 0, 255)); MemDC->Rectangle(); #endif } } #endif LSurface *pDC = MemDC ? MemDC : ScreenDC; #if 0 Gtk::cairo_matrix_t mx; Gtk::cairo_get_matrix(pDC->Handle(), &mx); LPoint Offset; WindowVirtualOffset(&Offset); printf("\tHtml paint mx=%g,%g off=%i,%i\n", mx.x0, mx.y0, Offset.x, Offset.y); #endif LColour cBack; if (GetCss()) { LCss::ColorDef Bk = GetCss()->BackgroundColor(); if (Bk.Type == LCss::ColorRgb) cBack = Bk; } if (!cBack.IsValid()) cBack = LColour(Enabled() ? L_WORKSPACE : L_MED); pDC->Colour(cBack); pDC->Rectangle(); if (Tag) { Layout(); if (VScroll) { int LineY = GetFont()->GetHeight(); int Vs = (int)VScroll->Value(); pDC->SetOrigin(0, Vs * LineY); } bool InSelection = false; PaintStart = LCurrentTime(); d->MaxPaintTimeout = false; Tag->OnPaint(pDC, InSelection, 0); if (d->MaxPaintTimeout) { LgiTrace("%s:%i - Html max paint time reached: %i ms.\n", _FL, LCurrentTime() - PaintStart); } } #if HTML_USE_DOUBLE_BUFFER if (MemDC) { pDC->SetOrigin(0, 0); ScreenDC->Blt(0, 0, MemDC); } #endif if (d->OnLoadAnchor && VScroll) { LAutoString a = d->OnLoadAnchor; GotoAnchor(a); LAssert(d->OnLoadAnchor == 0); } } bool LHtml::HasSelection() { if (Cursor && Selection) { return Cursor->Cursor >= 0 && Selection->Selection >= 0 && !(Cursor == Selection && Cursor->Cursor == Selection->Selection); } return false; } void LHtml::UnSelectAll() { bool i = false; if (Cursor) { Cursor->Cursor = -1; Cursor = NULL; i = true; } if (Selection) { Selection->Selection = -1; Selection = NULL; i = true; } if (i) { Invalidate(); } } void LHtml::SelectAll() { } LTag *LHtml::GetLastChild(LTag *t) { if (t && t->Children.Length()) { for (LTag *i = ToTag(t->Children.Last()); i; ) { LTag *c = i->Children.Length() ? ToTag(i->Children.Last()) : NULL; if (c) i = c; else return i; } } return 0; } LTag *LHtml::PrevTag(LTag *t) { // This returns the previous tag in the tree as if all the tags were // listed via recursion using "in order". // Walk up the parent chain looking for a prev for (LTag *p = t; p; p = ToTag(p->Parent)) { // Does this tag have a parent? if (p->Parent) { // Prev? LTag *pp = ToTag(p->Parent); ssize_t Idx = pp->Children.IndexOf(p); LTag *Prev = Idx > 0 ? ToTag(pp->Children[Idx - 1]) : NULL; if (Prev) { LTag *Last = GetLastChild(Prev); return Last ? Last : Prev; } else { return ToTag(p->Parent); } } } return 0; } LTag *LHtml::NextTag(LTag *t) { // This returns the next tag in the tree as if all the tags were // listed via recursion using "in order". // Does this have a child tag? if (t->Children.Length() > 0) { return ToTag(t->Children.First()); } else { // Walk up the parent chain for (LTag *p = t; p; p = ToTag(p->Parent)) { // Does this tag have a next? if (p->Parent) { LTag *pp = ToTag(p->Parent); size_t Idx = pp->Children.IndexOf(p); LTag *Next = pp->Children.Length() > Idx + 1 ? ToTag(pp->Children[Idx + 1]) : NULL; if (Next) { return Next; } } } } return 0; } int LHtml::GetTagDepth(LTag *Tag) { // Returns the depth of the tag in the tree. int n = 0; for (LTag *t = Tag; t; t = ToTag(t->Parent)) { n++; } return n; } bool LHtml::IsCursorFirst() { if (!Cursor || !Selection) return false; return CompareTagPos(Cursor, Cursor->Cursor, Selection, Selection->Selection); } bool LHtml::CompareTagPos(LTag *a, ssize_t AIdx, LTag *b, ssize_t BIdx) { // Returns true if the 'a' is before 'b' point. if (!a || !b) return false; if (a == b) { return AIdx < BIdx; } else { LArray ATree, BTree; for (LTag *t = a; t; t = ToTag(t->Parent)) ATree.AddAt(0, t); for (LTag *t = b; t; t = ToTag(t->Parent)) BTree.AddAt(0, t); ssize_t Depth = MIN(ATree.Length(), BTree.Length()); for (int i=0; i 0); LTag *p = ATree[i-1]; LAssert(BTree[i-1] == p); ssize_t ai = p->Children.IndexOf(at); ssize_t bi = p->Children.IndexOf(bt); return ai < bi; } } } return false; } void LHtml::SetLoadImages(bool i) { if (i ^ GetLoadImages()) { LDocView::SetLoadImages(i); SendNotify(LNotifyShowImagesChanged); if (GetLoadImages() && Tag) { Tag->LoadImages(); } } } char *LHtml::GetSelection() { char *s = 0; if (Cursor && Selection) { LMemQueue p; bool InSelection = false; Tag->CopyClipboard(p, InSelection); int Len = (int)p.GetSize(); if (Len > 0) { char16 *t = (char16*)p.New(sizeof(char16)); if (t) { size_t Len = StrlenW(t); for (int i=0; iOnFind(Dlg); } */ void BuildTagList(LArray &t, LTag *Tag) { t.Add(Tag); for (unsigned i=0; iChildren.Length(); i++) { LTag *c = ToTag(Tag->Children[i]); BuildTagList(t, c); } } static void FormEncode(LStringPipe &p, const char *c) { const char *s = c; while (*c) { while (*c && *c != ' ') c++; if (c > s) { p.Write(s, c - s); c = s; } if (*c == ' ') { p.Write("+", 1); s = c; } else break; } } bool LHtml::OnSubmitForm(LTag *Form) { if (!Form || !Environment) { LAssert(!"Bad param"); return false; } const char *Method = NULL; const char *Action = NULL; if (!Form->Get("method", Method) || !Form->Get("action", Action)) { LAssert(!"Missing form action/method"); return false; } LHashTbl,char*> f; Form->CollectFormValues(f); bool Status = false; if (!_stricmp(Method, "post")) { LStringPipe p(256); bool First = true; // const char *Field; // for (char *Val = f.First(&Field); Val; Val = f.Next(&Field)) for (auto v : f) { if (First) First = false; else p.Write("&", 1); FormEncode(p, v.key); p.Write("=", 1); FormEncode(p, v.value); } LAutoPtr Data(p.NewStr()); Status = Environment->OnPostForm(this, Action, Data); } else if (!_stricmp(Method, "get")) { Status = Environment->OnNavigate(this, Action); } else { LAssert(!"Bad form method."); } f.DeleteArrays(); return Status; } bool LHtml::OnFind(LFindReplaceCommon *Params) { bool Status = false; if (Params) { if (!Params->Find) return Status; d->FindText.Reset(Utf8ToWide(Params->Find)); d->MatchCase = Params->MatchCase; } if (!Cursor) Cursor = Tag; if (Cursor && d->FindText) { LArray Tags; BuildTagList(Tags, Tag); ssize_t Start = Tags.IndexOf(Cursor); for (unsigned i=1; iText()) { char16 *Hit; if (d->MatchCase) Hit = StrstrW(s->Text(), d->FindText); else Hit = StristrW(s->Text(), d->FindText); if (Hit) { // found something... UnSelectAll(); Selection = Cursor = s; Cursor->Cursor = Hit - s->Text(); Selection->Selection = Cursor->Cursor + StrlenW(d->FindText); OnCursorChanged(); if (VScroll) { // Scroll the tag into view... int y = s->AbsY(); int LineY = GetFont()->GetHeight(); int Val = y / LineY; SetVScroll(Val); } Invalidate(); Status = true; break; } } } } return Status; } void LHtml::DoFind(std::function Callback) { LFindDlg *Dlg = new LFindDlg(this, [this](auto dlg, auto action) { OnFind(dlg); delete dlg; }); Dlg->DoModal(NULL); } bool LHtml::OnKey(LKey &k) { bool Status = false; if (k.Down()) { int Dy = 0; int LineY = GetFont()->GetHeight(); int Page = GetClient().Y() / LineY; switch (k.vkey) { case LK_F3: { OnFind(NULL); break; } #ifdef WIN32 case LK_INSERT: goto DoCopy; #endif case LK_UP: { Dy = -1; Status = true; break; } case LK_DOWN: { Dy = 1; Status = true; break; } case LK_PAGEUP: { Dy = -Page; Status = true; break; } case LK_PAGEDOWN: { Dy = Page; Status = true; break; } case LK_HOME: { Dy = (int) (VScroll ? -VScroll->Value() : 0); Status = true; break; } case LK_END: { if (VScroll) { LRange r = VScroll->GetRange(); Dy = (int)(r.End() - Page); } Status = true; break; } default: { switch (k.c16) { case 'f': case 'F': { if (k.CtrlCmd()) { DoFind(NULL); Status = true; } break; } case 'c': case 'C': { #ifdef WIN32 DoCopy: #endif if (k.CtrlCmd()) { Copy(); Status = true; } break; } } break; } } if (Dy && VScroll) SetVScroll(VScroll->Value() + Dy); } return Status; } int LHtml::ScrollY() { return GetFont()->GetHeight() * (VScroll ? (int)VScroll->Value() : 0); } void LHtml::OnMouseClick(LMouse &m) { Capture(m.Down()); SetPulse(m.Down() ? 200 : -1); if (m.Down()) { Focus(true); int Offset = ScrollY(); bool TagProcessedClick = false; LTagHit Hit; if (Tag) { Tag->GetTagByPos(Hit, m.x, m.y + Offset, 0, false, DEBUG_TAG_BY_POS); #if DEBUG_TAG_BY_POS Hit.Dump("MouseClick"); #endif } if (m.Left() && !m.IsContextMenu()) { if (m.Double()) { d->WordSelectMode = true; if (Cursor) { // Extend the selection out to the current word's boundaries. Selection = Cursor; Selection->Selection = Cursor->Cursor; if (Cursor->Text()) { ssize_t Base = Cursor->GetTextStart(); char16 *Text = Cursor->Text() + Base; while (Text[Cursor->Cursor]) { char16 c = Text[Cursor->Cursor]; if (strchr(WordDelim, c) || StrchrW(WhiteW, c)) break; Cursor->Cursor++; } } if (Selection->Text()) { ssize_t Base = Selection->GetTextStart(); char16 *Sel = Selection->Text() + Base; while (Selection->Selection > 0) { char16 c = Sel[Selection->Selection - 1]; if (strchr(WordDelim, c) || StrchrW(WhiteW, c)) break; Selection->Selection--; } } Invalidate(); SendNotify(LNotifySelectionChanged); } } else if (Hit.NearestText) { d->WordSelectMode = false; UnSelectAll(); Cursor = Hit.NearestText; Cursor->Cursor = Hit.Index; #if DEBUG_SELECTION LgiTrace("StartSelect Near='%20S' Idx=%i\n", Hit.NearestText->Text(), Hit.Index); #endif OnCursorChanged(); SendNotify(LNotifySelectionChanged); } else { #if DEBUG_SELECTION LgiTrace("StartSelect no text hit %p, %p\n", Cursor, Selection); #endif } } if (Hit.NearestText && Hit.Near == 0) { TagProcessedClick = Hit.NearestText->OnMouseClick(m); } else if (Hit.Direct) { TagProcessedClick = Hit.Direct->OnMouseClick(m); } #ifdef _DEBUG else if (m.Left() && m.Ctrl()) { LgiMsg(this, "No tag under the cursor.", GetClass()); } #endif if (!TagProcessedClick && m.IsContextMenu()) { LSubMenu RClick; enum ContextMenuCmds { IDM_DUMP = 100, IDM_COPY_SRC, IDM_VIEW_SRC, IDM_EXTERNAL, IDM_COPY, IDM_VIEW_IMAGES, }; #define IDM_CHARSET_BASE 10000 RClick.AppendItem (LLoadString(L_TEXTCTRL_COPY, "Copy"), IDM_COPY, HasSelection()); LMenuItem *Vs = RClick.AppendItem (LLoadString(L_VIEW_SOURCE, "View Source"), IDM_VIEW_SRC, Source != 0); RClick.AppendItem (LLoadString(L_COPY_SOURCE, "Copy Source"), IDM_COPY_SRC, Source != 0); LMenuItem *Load = RClick.AppendItem (LLoadString(L_VIEW_IMAGES, "View External Images"), IDM_VIEW_IMAGES, true); if (Load) Load->Checked(GetLoadImages()); RClick.AppendItem (LLoadString(L_VIEW_IN_DEFAULT_BROWSER, "View in Default Browser"), IDM_EXTERNAL, Source != 0); LSubMenu *Cs = RClick.AppendSub (LLoadString(L_CHANGE_CHARSET, "Change Charset")); if (Cs) { int n=0; for (LCharset *c = LGetCsList(); c->Charset; c++, n++) { Cs->AppendItem(c->Charset, IDM_CHARSET_BASE + n, c->IsAvailable()); } } if (!GetReadOnly() || // Is editor #ifdef _DEBUG 1 #else 0 #endif ) { RClick.AppendSeparator(); RClick.AppendItem("Dump Layout", IDM_DUMP, Tag != 0); } if (Vs) { Vs->Checked(!IsHtml); } if (OnContextMenuCreate(Hit, RClick) && GetMouse(m, true)) { int Id = RClick.Float(this, m.x, m.y); switch (Id) { case IDM_COPY: { Copy(); break; } case IDM_VIEW_SRC: { if (Vs) { DeleteObj(Tag); IsHtml = !IsHtml; ParseDocument(Source); } break; } case IDM_COPY_SRC: { if (Source) { LClipBoard c(this); const char *ViewCs = GetCharset(); if (ViewCs) { LAutoWString w((char16*)LNewConvertCp(LGI_WideCharset, Source, ViewCs)); if (w) c.TextW(w); } else c.Text(Source); } break; } case IDM_VIEW_IMAGES: { SetLoadImages(!GetLoadImages()); break; } case IDM_DUMP: { if (Tag) { LAutoWString s = Tag->DumpW(); if (s) { LClipBoard c(this); c.TextW(s); } } break; } case IDM_EXTERNAL: { if (!Source) { LgiTrace("%s:%i - No HTML source code.\n", _FL); break; } char Path[MAX_PATH_LEN]; if (!LGetSystemPath(LSP_TEMP, Path, sizeof(Path))) { LgiTrace("%s:%i - Failed to get the system path.\n", _FL); break; } char f[32]; sprintf_s(f, sizeof(f), "_%i.html", LRand(1000000)); LMakePath(Path, sizeof(Path), Path, f); LFile F; if (!F.Open(Path, O_WRITE)) { LgiTrace("%s:%i - Failed to open '%s' for writing.\n", _FL, Path); break; } LStringPipe Ex; bool Error = false; F.SetSize(0); LAutoWString SrcMem; const char *ViewCs = GetCharset(); if (ViewCs) SrcMem.Reset((char16*)LNewConvertCp(LGI_WideCharset, Source, ViewCs)); else SrcMem.Reset(Utf8ToWide(Source)); for (char16 *s=SrcMem; s && *s;) { char16 *cid = StristrW(s, L"cid:"); while (cid && !strchr("\'\"", cid[-1])) { cid = StristrW(cid+1, L"cid:"); } if (cid) { char16 Delim = cid[-1]; char16 *e = StrchrW(cid, Delim); if (e) { *e = 0; if (StrchrW(cid, '\n')) { *e = Delim; Error = true; break; } else { char File[MAX_PATH_LEN] = ""; if (Environment) { LDocumentEnv::LoadJob *j = Environment->NewJob(); if (j) { j->Uri.Reset(WideToUtf8(cid)); j->Env = Environment; j->Pref = LDocumentEnv::LoadJob::FmtFilename; j->UserUid = GetDocumentUid(); LDocumentEnv::LoadType Result = Environment->GetContent(j); if (Result == LDocumentEnv::LoadImmediate) { if (j->Filename) strcpy_s(File, sizeof(File), j->Filename); } else if (Result == LDocumentEnv::LoadDeferred) { d->DeferredLoads++; } DeleteObj(j); } } *e = Delim; Ex.Push(s, cid - s); if (File[0]) { char *d; while ((d = strchr(File, '\\'))) { *d = '/'; } Ex.Push(L"file:///"); LAutoWString w(Utf8ToWide(File)); Ex.Push(w); } s = e; } } else { Error = true; break; } } else { Ex.Push(s); break; } } if (!Error) { int64 WideChars = Ex.GetSize() / sizeof(char16); LAutoWString w(Ex.NewStrW()); LAutoString u(WideToUtf8(w, WideChars)); if (u) F.Write(u, strlen(u)); F.Close(); LString Err; if (!LExecute(Path, NULL, NULL, &Err)) { LgiMsg( this, "Failed to open '%s'\n%s", LAppInst ? LAppInst->LBase::Name() : GetClass(), MB_OK, Path, Err.Get()); } } break; } default: { if (Id >= IDM_CHARSET_BASE) { LCharset *c = LGetCsList() + (Id - IDM_CHARSET_BASE); if (c->Charset) { Charset = c->Charset; OverideDocCharset = true; char *Src = Source.Release(); _Delete(); _New(); Source.Reset(Src); ParseDocument(Source); Invalidate(); SendNotify(LNotifyCharsetChanged); } } else { OnContextMenuCommand(Hit, Id); } break; } } } } } else // Up Click { if (Selection && Cursor && Selection == Cursor && Selection->Selection == Cursor->Cursor) { Selection->Selection = -1; Selection = 0; SendNotify(LNotifySelectionChanged); #if DEBUG_SELECTION LgiTrace("NoSelect on release\n"); #endif } } } void LHtml::OnLoad() { d->IsLoaded = true; SendNotify(LNotifyDocLoaded); } LTag *LHtml::GetTagByPos(int x, int y, ssize_t *Index, LPoint *LocalCoords, bool DebugLog) { LTag *Status = NULL; if (Tag) { if (DebugLog) LgiTrace("GetTagByPos starting...\n"); LTagHit Hit; Tag->GetTagByPos(Hit, x, y, 0, DebugLog); if (DebugLog) LgiTrace("GetTagByPos Hit=%s, %i, %i...\n\n", Hit.Direct ? Hit.Direct->Tag.Get() : 0, Hit.Index, Hit.Near); Status = Hit.NearestText && Hit.Near == 0 ? Hit.NearestText : Hit.Direct; if (Hit.NearestText && Hit.Near < 30) { if (Index) *Index = Hit.Index; if (LocalCoords) *LocalCoords = Hit.LocalCoords; } } return Status; } void LHtml::SetVScroll(int64 v) { if (!VScroll) return; if (Tag) Tag->ClearToolTips(); VScroll->Value(v); Invalidate(); } bool LHtml::OnMouseWheel(double Lines) { if (VScroll) SetVScroll(VScroll->Value() + (int64)Lines); return true; } LCursor LHtml::GetCursor(int x, int y) { int Offset = ScrollY(); ssize_t Index = -1; LPoint LocalCoords; LTag *Tag = GetTagByPos(x, y + Offset, &Index, &LocalCoords); if (Tag) { LString Uri; if (LocalCoords.x >= 0 && LocalCoords.y >= 0 && Tag->IsAnchor(&Uri)) { LRect c = GetClient(); c.Offset(-c.x1, -c.y1); if (c.Overlap(x, y) && ValidStr(Uri)) { return LCUR_PointingHand; } } } return LCUR_Normal; } void LTag::ClearToolTips() { if (TipId) { Html->Tip.DeleteTip(TipId); TipId = 0; } for (auto c: Children) ToTag(c)->ClearToolTips(); } void LHtml::OnMouseMove(LMouse &m) { if (!Tag) return; int Offset = ScrollY(); LTagHit Hit; Tag->GetTagByPos(Hit, m.x, m.y + Offset, 0, false); if (!Hit.Direct && !Hit.NearestText) return; LString Uri; LTag *HitTag = Hit.NearestText && Hit.Near == 0 ? Hit.NearestText : Hit.Direct; if (HitTag && HitTag->TipId == 0 && Hit.LocalCoords.x >= 0 && Hit.LocalCoords.y >= 0 && HitTag->IsAnchor(&Uri) && Uri) { if (!Tip.GetParent()) { Tip.Attach(this); } LRect r = HitTag->GetRect(false); r.Offset(0, -Offset); if (!HitTag->TipId) HitTag->TipId = Tip.NewTip(Uri, r); // LgiTrace("NewTip: %s @ %s, ID=%i\n", Uri.Get(), r.GetStr(), HitTag->TipId); } if (IsCapturing() && Cursor && Hit.NearestText) { if (!Selection) { Selection = Cursor; Selection->Selection = Cursor->Cursor; Cursor = Hit.NearestText; Cursor->Cursor = Hit.Index; OnCursorChanged(); Invalidate(); SendNotify(LNotifySelectionChanged); #if DEBUG_SELECTION LgiTrace("CreateSelection '%20S' %i\n", Hit.NearestText->Text(), Hit.Index); #endif } else if ((Cursor != Hit.NearestText) || (Cursor->Cursor != Hit.Index)) { // Move the cursor to track the mouse if (Cursor) { Cursor->Cursor = -1; } Cursor = Hit.NearestText; Cursor->Cursor = Hit.Index; #if DEBUG_SELECTION LgiTrace("ExtendSelection '%20S' %i\n", Hit.NearestText->Text(), Hit.Index); #endif if (d->WordSelectMode && Cursor->Text()) { ssize_t Base = Cursor->GetTextStart(); if (IsCursorFirst()) { // Extend the cursor up the document to include the whole word while (Cursor->Cursor > 0) { char16 c = Cursor->Text()[Base + Cursor->Cursor - 1]; if (strchr(WordDelim, c) || StrchrW(WhiteW, c)) break; Cursor->Cursor--; } } else { // Extend the cursor down the document to include the whole word while (Cursor->Text()[Base + Cursor->Cursor]) { char16 c = Cursor->Text()[Base + Cursor->Cursor]; if (strchr(WordDelim, c) || StrchrW(WhiteW, c)) break; Cursor->Cursor++; } } } OnCursorChanged(); Invalidate(); SendNotify(LNotifySelectionChanged); } } } void LHtml::OnPulse() { if (VScroll && IsCapturing()) { int Fy = DefFont() ? DefFont()->GetHeight() : 16; LMouse m; if (GetMouse(m, false)) { LRect c = GetClient(); int Lines = 0; if (m.y < c.y1) { // Scroll up Lines = (c.y1 - m.y + Fy - 1) / -Fy; } else if (m.y > c.y2) { // Scroll down Lines = (m.y - c.y2 + Fy - 1) / Fy; } if (Lines && VScroll) SetVScroll(VScroll->Value() + Lines); } } } LRect *LHtml::GetCursorPos() { return &d->CursorPos; } void LHtml::SetCursorVis(bool b) { if (d->CursorVis ^ b) { d->CursorVis = b; Invalidate(); } } bool LHtml::GetCursorVis() { return d->CursorVis; } LDom *ElementById(LTag *t, char *id) { if (t && id) { const char *i; if (t->Get("id", i) && _stricmp(i, id) == 0) return t; for (unsigned i=0; iChildren.Length(); i++) { LTag *c = ToTag(t->Children[i]); LDom *n = ElementById(c, id); if (n) return n; } } return 0; } LDom *LHtml::getElementById(char *Id) { return ElementById(Tag, Id); } bool LHtml::GetLinkDoubleClick() { return d->LinkDoubleClick; } void LHtml::SetLinkDoubleClick(bool b) { d->LinkDoubleClick = b; } bool LHtml::GetFormattedContent(const char *MimeType, LString &Out, LArray *Media) { if (!MimeType) { LAssert(!"No MIME type for getting formatted content"); return false; } if (!_stricmp(MimeType, "text/html")) { // We can handle this type... LArray Imgs; if (Media) { // Find all the image tags... Tag->Find(TAG_IMG, Imgs); // Give them CID's if they don't already have them for (unsigned i=0; iGet("src", Src) && !Img->Get("cid", Cid)) { char id[256]; sprintf_s(id, sizeof(id), "%x.%x", (unsigned)LCurrentTime(), (unsigned)LRand()); Img->Set("cid", id); Img->Get("cid", Cid); } if (Src && Cid) { LFile *f = new LFile; if (f) { if (f->Open(Src, O_READ)) { // Add the exported image stream to the media array LDocView::ContentMedia &m = Media->New(); m.Id = Cid; m.Stream.Reset(f); } } } } } // Export the HTML, including the CID's from the first step Out = Name(); } else if (!_stricmp(MimeType, "text/plain")) { // Convert DOM tree down to text instead... LStringPipe p(512); if (Tag) { LTag::TextConvertState State(&p); Tag->ConvertToText(State); } Out = p.NewLStr(); } return false; } void LHtml::OnContent(LDocumentEnv::LoadJob *Res) { if (JobSem.Lock(_FL)) { JobSem.Jobs.Add(Res); JobSem.Unlock(); PostEvent(M_JOBS_LOADED); } } LHtmlElement *LHtml::CreateElement(LHtmlElement *Parent) { return new LTag(this, Parent); } bool LHtml::GetVariant(const char *Name, LVariant &Value, const char *Array) { if (!_stricmp(Name, "supportLists")) // Type: Bool Value = false; else if (!_stricmp(Name, "vml")) // Type: Bool // Vector Markup Language Value = false; else if (!_stricmp(Name, "mso")) // Type: Bool // mso = Microsoft Office Value = false; else return false; return true; } bool LHtml::EvaluateCondition(const char *Cond) { if (!Cond) return true; // This is a really bad attempt at writing an expression evaluator. // I could of course use the scripting language but that would pull // in a fairly large dependency on the HTML control. However user // apps that already have that could reimplement this virtual function // if they feel like it. LArray Str; for (const char *c = Cond; *c; ) { if (IsAlpha(*c)) { Str.Add(LTokStr(c)); } else if (IsWhiteSpace(*c)) { c++; } else { const char *e = c; while (*e && !IsWhiteSpace(*e) && !IsAlpha(*e)) e++; Str.Add(NewStr(c, e - c)); LAssert(e > c); if (e > c) c = e; else break; } } bool Result = true; bool Not = false; for (unsigned i=0; iGetAnchor(Name); if (a) { if (VScroll) { int LineY = GetFont()->GetHeight(); int Ay = a->AbsY(); int Scr = Ay / LineY; SetVScroll(Scr); VScroll->SendNotify(); } else d->OnLoadAnchor.Reset(NewStr(Name)); } } return false; } bool LHtml::GetEmoji() { return d->DecodeEmoji; } void LHtml::SetEmoji(bool i) { d->DecodeEmoji = i; } void LHtml::SetMaxPaintTime(int Ms) { d->MaxPaintTime = Ms; } bool LHtml::GetMaxPaintTimeout() { return d->MaxPaintTimeout; } //////////////////////////////////////////////////////////////////////// class LHtml_Factory : public LViewFactory { LView *NewView(const char *Class, LRect *Pos, const char *Text) { if (_stricmp(Class, "LHtml") == 0) { return new LHtml(-1, 0, 0, 100, 100, new LDefaultDocumentEnv); } return 0; } } LHtml_Factory; ////////////////////////////////////////////////////////////////////// struct BuildContext { LHtmlTableLayout *Layout; LTag *Table; LTag *TBody; LTag *CurTr; LTag *CurTd; int cx, cy; BuildContext() { Layout = NULL; cx = cy = 0; Table = NULL; TBody = NULL; CurTr = NULL; CurTd = NULL; } bool Build(LTag *t, int Depth) { bool RetReattach = false; switch (t->TagId) { case TAG_TABLE: { if (!Table) Table = t; else return false; break; } case TAG_TBODY: { if (TBody) return false; TBody = t; break; } case TAG_TR: { CurTr = t; break; } case TAG_TD: { CurTd = t; if (t->Parent != CurTr) { if ( !CurTr && (Table || TBody) ) { LTag *p = TBody ? TBody : Table; CurTr = new LTag(p->Html, p); if (CurTr) { CurTr->Tag.Reset(NewStr("tr")); CurTr->TagId = TAG_TR; ssize_t Idx = t->Parent->Children.IndexOf(t); t->Parent->Attach(CurTr, Idx); } } if (CurTr) { CurTr->Attach(t); RetReattach = true; } else { LAssert(0); return false; } } t->Cell->Pos.x = cx; t->Cell->Pos.y = cy; Layout->Set(t); break; } default: { if (CurTd == t->Parent) return false; break; } } for (unsigned n=0; nChildren.Length(); n++) { LTag *c = ToTag(t->Children[n]); bool Reattached = Build(c, Depth+1); if (Reattached) n--; } if (t->TagId == TAG_TR) { CurTr = NULL; cy++; cx = 0; Layout->s.y = cy; } if (t->TagId == TAG_TD) { CurTd = NULL; cx += t->Cell->Span.x; Layout->s.x = MAX(cx, Layout->s.x); } return RetReattach; } }; LHtmlTableLayout::LHtmlTableLayout(LTag *table) { Table = table; if (!Table) return; #if 0 BuildContext Ctx; Ctx.Layout = this; Ctx.Build(table, 0); #else int y = 0; LTag *FakeRow = 0; LTag *FakeCell = 0; LTag *r; for (size_t i=0; iChildren.Length(); i++) { r = ToTag(Table->Children[i]); if (r->Display() == LCss::DispNone) continue; if (r->TagId == TAG_TR) { FakeRow = 0; FakeCell = 0; } else if (r->TagId == TAG_TBODY) { ssize_t Index = Table->Children.IndexOf(r); for (size_t n=0; nChildren.Length(); n++) { LTag *t = ToTag(r->Children[n]); Table->Children.AddAt(++Index, t); t->Parent = Table; /* LgiTrace("Moving '%s'(%p) from TBODY(%p) into '%s'(%p)\n", t->Tag, t, r, t->Parent->Tag, t->Parent); */ } r->Children.Length(0); } else { if (!FakeRow) { if ((FakeRow = new LTag(Table->Html, 0))) { FakeRow->Tag.Reset(NewStr("tr")); FakeRow->TagId = TAG_TR; ssize_t Idx = Table->Children.IndexOf(r); Table->Attach(FakeRow, Idx); } } if (FakeRow) { if (!IsTableCell(r->TagId) && !FakeCell) { if ((FakeCell = new LTag(Table->Html, FakeRow))) { FakeCell->Tag.Reset(NewStr("td")); FakeCell->TagId = TAG_TD; if ((FakeCell->Cell = new LTag::TblCell)) { FakeCell->Cell->Span.x = 1; FakeCell->Cell->Span.y = 1; } } } ssize_t Idx = Table->Children.IndexOf(r); r->Detach(); if (IsTableCell(r->TagId)) { FakeRow->Attach(r); } else { LAssert(FakeCell != NULL); FakeCell->Attach(r); } i = Idx - 1; } } } FakeCell = NULL; for (size_t n=0; nChildren.Length(); n++) { LTag *r = ToTag(Table->Children[n]); if (r->TagId == TAG_TR) { int x = 0; for (size_t i=0; iChildren.Length(); i++) { LTag *cell = ToTag(r->Children[i]); if (!IsTableCell(cell->TagId)) { if (!FakeCell) { // Make a fake TD cell FakeCell = new LTag(Table->Html, NULL); FakeCell->Tag.Reset(NewStr("td")); FakeCell->TagId = TAG_TD; if ((FakeCell->Cell = new LTag::TblCell)) { FakeCell->Cell->Span.x = 1; FakeCell->Cell->Span.y = 1; } // Join the fake TD into the TR r->Children[i] = FakeCell; FakeCell->Parent = r; } else { // Not the first non-TD tag, so delete it from the TR. Only the // fake TD will remain in the TR. r->Children.DeleteAt(i--, true); } // Insert the tag into it as a child FakeCell->Children.Add(cell); cell->Parent = FakeCell; cell = FakeCell; } else { FakeCell = NULL; } if (IsTableCell(cell->TagId)) { if (cell->Display() == LCss::DispNone) continue; while (Get(x, y)) { x++; } cell->Cell->Pos.x = x; cell->Cell->Pos.y = y; Set(cell); x += cell->Cell->Span.x; } } y++; FakeCell = NULL; } } #endif } void LHtmlTableLayout::Dump() { int Sx, Sy; GetSize(Sx, Sy); LgiTrace("Table %i x %i cells.\n", Sx, Sy); for (int x=0; xCell->Pos.x, t->Cell->Pos.y, t->Cell->Span.x, t->Cell->Span.y); LgiTrace("%-10s", s); } LgiTrace("\n"); } LgiTrace("\n"); } void LHtmlTableLayout::GetAll(List &All) { LHashTbl, bool> Added; for (size_t y=0; y= (int) c.Length()) return NULL; CellArray &a = c[y]; if (x >= (int) a.Length()) return NULL; return a[x]; } bool LHtmlTableLayout::Set(LTag *t) { if (!t) return false; for (int y=0; yCell->Span.y; y++) { for (int x=0; xCell->Span.x; x++) { // LAssert(!c[y][x]); c[t->Cell->Pos.y + y][t->Cell->Pos.x + x] = t; } } return true; } void LTagHit::Dump(const char *Desc) { LArray d, n; LTag *t = Direct; unsigned i; for (i=0; i<3 && t; t = ToTag(t->Parent), i++) { d.AddAt(0, t); } t = NearestText; for (i=0; i<3 && t; t = ToTag(t->Parent), i++) { n.AddAt(0, t); } LgiTrace("Hit: %s Direct: ", Desc); for (i=0; i%s", d[i]->Tag ? d[i]->Tag.Get() : "CONTENT"); LgiTrace(" Nearest: "); for (i=0; i%s", n[i]->Tag ? n[i]->Tag.Get() : "CONTENT"); LgiTrace(" Local: %ix%i Index: %i Block: %s '%.10S'\n", LocalCoords.x, LocalCoords.y, Index, Block ? Block->GetStr() : NULL, Block ? Block->Text + Index : NULL); } diff --git a/src/common/Text/HtmlPriv.h b/src/common/Text/HtmlPriv.h --- a/src/common/Text/HtmlPriv.h +++ b/src/common/Text/HtmlPriv.h @@ -1,483 +1,482 @@ #ifndef _GHTML1_PRIV_H_ #define _GHTML1_PRIV_H_ #include "lgi/common/Css.h" -#include "lgi/common/Token.h" #include "lgi/common/HtmlParser.h" class HtmlEdit; namespace Html1 { #define DefaultTextColour LColour(L_TEXT) ////////////////////////////////////////////////////////////////////////////////// // Structs & Classes // ////////////////////////////////////////////////////////////////////////////////// class LFlowRect; class LFlowRegion; #define ToTag(t) dynamic_cast(t) struct LTagHit { LTag *Direct; // Tag directly under cursor LTag *NearestText; // Nearest tag with text int Near; // How close in px was the position to NearestText. // 0 if a direct hit, >0 is near miss, -1 if invalid. bool NearSameRow; // True if 'NearestText' on the same row as click. LPoint LocalCoords; // The position in local co-ords of the tag LFlowRect *Block; // Text block hit ssize_t Index; // If Block!=NULL then index into text, otherwise -1. LTagHit() { Direct = NULL; NearestText = NULL; NearSameRow = false; Block = 0; Near = -1; Index = -1; LocalCoords.x = LocalCoords.y = -1; } void Dump(const char *Desc); }; class LHtmlLength { protected: float d; float PrevAbs; LCss::LengthType u; public: LHtmlLength(); LHtmlLength(char *s); bool IsValid(); bool IsDynamic(); float GetPrevAbs() { return PrevAbs; } operator float(); LHtmlLength &operator =(float val); LCss::LengthType GetUnits(); void Set(char *s); float Get(LFlowRegion *Flow, LFont *Font, bool Lock = false); float GetRaw() { return d; } }; class LHtmlLine : public LHtmlLength { public: int LineStyle; int LineReset; LCss::ColorDef Colour; LHtmlLine(); ~LHtmlLine(); LHtmlLine &operator =(int i); void Set(char *s); }; class LFlowRect : public LRect { public: LTag *Tag; char16 *Text; ssize_t Len; LFlowRect() { Tag = 0; Text = 0; Len = 0; } ~LFlowRect() { } int Start(); bool OverlapX(int x) { return x >= x1 && x <= x2; } bool OverlapY(int y) { return y >= y1 && y <= y2; } bool OverlapX(LFlowRect *b) { return !(b->x2 < x1 || b->x1 > x2); } bool OverlapY(LFlowRect *b) { return !(b->y2 < y1 || b->y1 > y2); } }; class LHtmlArea : public LArray { public: ~LHtmlArea(); void Empty() { DeleteObjects(); } LRect Bounds(); LRect *TopRect(LRegion *c); void FlowText(LTag *Tag, LFlowRegion *c, LFont *Font, int LineHeight, char16 *Text, LCss::LengthType Align); }; struct LHtmlTableLayout { typedef LArray CellArray; LArray c; LTag *Table; LPoint s; LCss::Len TableWidth; // Various pixels sizes int AvailableX; int CellSpacing; int BorderX1, BorderX2; LRect TableBorder, TablePadding; // in Px // The col and row sizes LArray MinCol, MaxCol, MaxRow; LArray SizeCol; LHtmlTableLayout(LTag *table); void GetSize(int &x, int &y); void GetAll(List &All); LTag *Get(int x, int y); bool Set(LTag *t); int GetTotalX(int StartCol = 0, int Cols = -1); void AllocatePx(int StartCol, int Cols, int MinPx, bool FillWidth); void DeallocatePx(int StartCol, int Cols, int MaxPx); void LayoutTable(LFlowRegion *f, uint16 Depth); void Dump(); }; class LTag : public LHtmlElement { friend struct LHtmlTableLayout; friend class ::HtmlEdit; public: enum HtmlControlType { CtrlNone, CtrlPassword, CtrlEmail, CtrlText, CtrlButton, CtrlSubmit, CtrlSelect, CtrlHidden, }; class TextConvertState { LStream *Out; ssize_t PrevLineLen; LArray Buf; public: int Depth; ssize_t CharsOnLine; TextConvertState(LStream *o) { Out = o; Depth = 0; CharsOnLine = 0; PrevLineLen = 0; } ~TextConvertState() { if (CharsOnLine) NewLine(); } ssize_t _Write(const void *Ptr, ssize_t Bytes) { // Check if we have enough space to store the string.. size_t Total = CharsOnLine + Bytes; if (Buf.Length() < Total) { // Extend the memory buffer if (!Buf.Length(Total + 32)) return -1; } // Store the string into a line buffer memcpy(&Buf[CharsOnLine], Ptr, Bytes); CharsOnLine += Bytes; return Bytes; } ssize_t Write(const void *Ptr, ssize_t Bytes) { char *start = (char*) Ptr, *cur; char *end = start + Bytes; const char *eol = "\r\n"; while (start < end) { for (cur = start; *cur && cur < end && !strchr(eol, *cur); cur++) ; if (!*cur || cur >= end) break; _Write(start, (int) (cur - start)); start = cur; while (*start && start < end && strchr(eol, *start)) start++; } return _Write(start, (int) (end - start)); } ssize_t GetPrev() { return PrevLineLen; } void NewLine() { bool Valid = false; const uint8_t Ws[] = {' ', '\t', 0xa0, 0}; LUtf8Ptr p(&Buf[0]); uint8_t *End = (uint8_t*) &Buf[CharsOnLine]; while (p.GetPtr() < End) { if (!strchr((char*)Ws, p)) { Valid = true; break; } p++; } if (!Valid) CharsOnLine = 0; Buf[CharsOnLine] = 0; if (CharsOnLine || PrevLineLen) { Out->Write(&Buf[0], CharsOnLine); Out->Write("\n", 1); PrevLineLen = CharsOnLine; CharsOnLine = 0; } } }; protected: /// A hash table of attributes. /// /// All strings stored in here should be in UTF-8. Each string is allocated on the heap. LHashTbl, char*> Attr; // Post flow alignment struct AlignInfo { DisplayType Disp; LCss::LengthType XAlign; LTag *t; bool Overlap(LTag *b) { LRange tRng(t->Pos.y, t->Size.y); LRange bRng(b->Pos.y, b->Size.y); return tRng.Overlap(bRng).Valid(); } }; struct AlignGroup : public LArray { int x1, x2; }; LArray PostFlowAlign; // Forms LViewI *Ctrl = NULL; LVariant CtrlValue; HtmlControlType CtrlType = CtrlNone; // Text LAutoWString PreTxt; // Debug stuff void _Dump(LStringPipe &Buf, int Depth); void _TraceOpenTags(); // Private methods LFont *NewFont(); ssize_t NearestChar(LFlowRect *Fr, int x, int y); LTag *HasOpenTag(char *t); LTag *PrevTag(); LRect ChildBounds(); bool GetWidthMetrics(LTag *Table, uint16 &Min, uint16 &Max); void LayoutTable(LFlowRegion *f, uint16 Depth); void BoundParents(); bool PeekTag(char *s, char *tag); LTag *GetTable(); char *NextTag(char *s); void ZeroTableElements(); bool OnUnhandledColor(LCss::ColorDef *def, const char *&s); // void CenterText(); bool Serialize(LXmlTag *t, bool Write); LColour _Colour(bool Fore); public: // Object LString::Array Class; const char *HtmlId = NULL; LAutoString Condition; int TipId = 0; // Hierarchy LHtml *Html = NULL; bool IsBlock() { return Display() == LCss::DispBlock; } LTag *GetBlockParent(ssize_t *Idx = 0); LFont *GetFont(); // Style LPoint Pos; LPoint Size; LFont *Font = NULL; int LineHeightCache = -1; LRect PadPx; // Images bool ImageResized = false; LAutoPtr Image; void SetImage(const char *uri, LSurface *i); void LoadImage(const char *Uri); // Load just this URI void LoadImages(); // Recursive load all image URI's void ImageLoaded(char *uri, LSurface *img, int &Used); void ClearToolTips(); // Table stuff struct TblCell { LPoint Pos; LPoint Span; LRect BorderPx; LRect PaddingPx; uint16 MinContent, MaxContent; LCss::LengthType XAlign; LHtmlTableLayout *Cells; TblCell() { Cells = NULL; MinContent = 0; MaxContent = 0; XAlign = LCss::LenInherit; BorderPx.ZOff(0, 0); PaddingPx.ZOff(0, 0); } ~TblCell() { DeleteObj(Cells); } } *Cell = NULL; #ifdef _DEBUG int Debug = false; #endif // Text ssize_t Cursor = -1; // index into text of the cursor ssize_t Selection = -1; // index into the text of the selection edge LHtmlArea TextPos; LTag(LHtml *h, LHtmlElement *p); ~LTag(); // Events void OnChange(PropType Prop); bool OnClick(const LMouse &m); // Attributes bool Get(const char *attr, const char *&val) { val = Attr.Find(attr); return val != 0; } void Set(const char *attr, const char *val); // Methods char16 *Text() { return Txt; } void Text(char16 *t) { Txt.Reset(t); TextPos.Empty(); } char16 *PreText() { return PreTxt; } void PreText(char16 *t) { PreTxt.Reset(t); TextPos.Empty(); } ssize_t GetTextStart(); LAutoWString DumpW(); LAutoString DescribeElement(); char16 *CleanText(const char *s, ssize_t len, const char *SourceCs, bool ConversionAllowed = true, bool KeepWhiteSpace = false); char *ParseText(char *Doc); bool ConvertToText(TextConvertState &State); /// Configures the tag's styles. void SetStyle(); /// Called to apply CSS selectors on initialization and also when properties change at runtime. void Restyle(); /// Recursively call restyle on all nodes in the doc tree void RestyleAll(); /// Takes the CSS styles, parses and stores them in the current object, //// overwriting any duplicate properties. void SetCssStyle(const char *Style); /// Event received by scripts change CSS properties. void OnStyleChange(const char *name); /// Positions the tag according to the flow region passed in void OnFlow(LFlowRegion *Flow, uint16 Depth); /// Paints the border and background of the tag void PaintBorderAndBackground( /// The surface to paint on LSurface *pDC, /// The background colour (transparent is OK) LColour &Back, /// [Optional] The size of the border painted LRect *Px = NULL); /// This fills 'rgn' with all the rectangles making up the inline tags region void GetInlineRegion(LRegion &rgn, int ox = 0, int oy = 0); void OnPaint(LSurface *pDC, bool &InSelection, uint16 Depth); void SetSize(LPoint &s); void SetTag(const char *Tag); void GetTagByPos(LTagHit &TagHit, int x, int y, int Depth, bool InBody, bool DebugLog = false); LTag *GetTagByName(const char *Name); void CopyClipboard(LMemQueue &p, bool &InSelection); LTag *IsAnchor(LString *Uri); bool CreateSource(LStringPipe &p, int Depth = 0, bool LastWasBlock = true); void Find(int TagType, LArray &Tags); LTag *GetAnchor(char *Name); // Control handling LTag *FindCtrlId(int Id); int OnNotify(LNotification n); void CollectFormValues(LHashTbl,char*> &f); // GDom impl bool GetVariant(const char *Name, LVariant &Value, const char *Array = 0); bool SetVariant(const char *Name, LVariant &Value, const char *Array = 0); // Window bool OnMouseClick(LMouse &m); void Invalidate(); // Positioning int RelX() { return Pos.x + (int)MarginLeft().Value; } int RelY() { return Pos.y + (int)MarginTop().Value; } LPoint AbsolutePos(); inline int AbsX() { return AbsolutePos().x; } inline int AbsY() { return AbsolutePos().y; } LRect GetRect(bool Client = true); LCss::LengthType GetAlign(bool x); // Tables LTag *GetTableCell(int x, int y); LPoint GetTableSize(); void ResetCaches(); }; } #endif diff --git a/src/common/Text/vCard-vCal.cpp b/src/common/Text/vCard-vCal.cpp --- a/src/common/Text/vCard-vCal.cpp +++ b/src/common/Text/vCard-vCal.cpp @@ -1,1526 +1,1525 @@ #include #include #include #include "lgi/common/Lgi.h" #include "lgi/common/vCard-vCal.h" #include "ScribeDefs.h" #include "lgi/common/Json.h" #define DEBUG_LOGGING 0 #define Push(s) Write(s, (int)strlen(s)) #define ClearFields() \ Field.Empty(); \ Params.Empty(); \ Data.Empty() #define IsType(str) (Params.Find(str) != 0) struct TzMap { int Offset; const char *Name; }; #define TZ(h,m) (((h)*100) + (m)) static TzMap Timezones[] = { {TZ(11,0), "Australia/Sydney"}, }; int TimezoneToOffset(const char *Tz) { if (!Tz) return 0; for (int i=0; iName, Tz)) return t->Offset; } return (int)Atoi(Tz); } #if 1 bool IsVar(char *field, const char *s) { if (!s) return false; char *dot = strchr(field, '.'); if (dot) return _stricmp(dot + 1, s) == 0; return _stricmp(field, s) == 0; } #else #define IsVar(field, str) (field != 0 && _stricmp(field, str) == 0) #endif char *DeEscape(char *s, bool QuotedPrintable) { if (!s) return 0; char *i = s; char *o = s; while (*i) { if (*i == '\\') { i++; switch (*i) { case 'n': case 'N': *o++ = '\n'; break; case ',': case ';': case ':': *o++ = *i; break; default: *o++ = '\\'; i--; break; } } else if (QuotedPrintable && *i == '=' && i[1] && i[2]) { i++; char h[3] = { i[0], i[1], 0}; *o++ = htoi(h); i++; } else { *o++ = *i; } i++; } *o = 0; return s; } ///////////////////////////////////////////////////////////// // General IO class class VIoPriv { public: LStringPipe Buf; }; VIo::VIo() : d(new VIoPriv) { } VIo::~VIo() { DeleteObj(d); } bool VIo::ParseDate(LDateTime &Out, char *In) { bool Status = false; if (In) { Out.SetTimeZone(0, false); - LToken v(In, "T"); + auto v = LString(In).SplitDelimit("T"); if (v.Length() > 0) { - char *d = v[0]; - if (d && strlen(d) == 8) + if (v[0].Length() == 8) { - char Year[5] = {d[0], d[1], d[2], d[3], 0}; - char Month[3] = {d[4], d[5], 0}; - char Day[3] = {d[6], d[7], 0}; - Out.Year(atoi(Year)); - Out.Month(atoi(Month)); - Out.Day(atoi(Day)); + auto Year = v[0](0, 4); + auto Month = v[0](4, 6); + auto Day = v[0](6, 8); + Out.Year ((int)Year.Int()); + Out.Month((int)Month.Int()); + Out.Day ((int)Day.Int()); Status = true; } char *t = v[1]; if (t && strlen(t) >= 6) { char Hour[3] = {t[0], t[1], 0}; char Minute[3] = {t[2], t[3], 0}; char Second[3] = {t[4], t[5], 0}; Out.Hours(atoi(Hour)); Out.Minutes(atoi(Minute)); Out.Seconds(atoi(Second)); Status = true; } } } return Status; } bool VIo::ParseDuration(LDateTime &Out, int &Sign, char *In) { bool Status = false; if (In) { Sign = 1; if (*In == '-') { Sign = -1; In++; } if (toupper(*In++) == 'P' && toupper(*In++) == 'T') { while (*In) { int i = atoi(In); while (IsDigit(*In)) In++; switch (toupper(*In++)) { case 'W': { Out.Day(Out.Day() + (i * 7)); break; } case 'D': { Out.Day(Out.Day() + i); break; } case 'H': { Out.Hours(Out.Hours() + i); break; } case 'M': { Out.Minutes(Out.Minutes() + i); break; } case 'S': { Out.Seconds(Out.Seconds() + i); break; } } } Status = true; } } return Status; } void VIo::Fold(LStreamI &o, const char *i, int pre_chars) { int x = pre_chars; for (const char *s=i; s && *s;) { if (x >= 74) { // wrapping o.Write(i, (int)(s-i)); o.Write((char*)"\r\n\t", 3); x = 0; i = s; } else if (*s == '=' || ((((uint8_t)*s) & 0x80) != 0)) { // quoted printable o.Write(i, (int)(s-i)); LStreamPrint(&o, "=%02.2x", (uint8_t)*s); x += 3; i = ++s; } else if (*s == '\n') { // new line o.Write(i, (int)(s-i)); o.Write((char*)"\\n", 2); x += 2; i = ++s; } else if (*s == '\r') { o.Write(i, (int)(s-i)); i = ++s; } else { s++; x++; } } o.Write(i, (int)strlen(i)); } char *VIo::Unfold(char *In) { if (In) { LStringPipe p(256); for (char *i=In; i && *i; i++) { if (*i == '\n') { if (i[1] && strchr(" \t", i[1])) { i++; } else { p.Write(i, 1); } } else { p.Write(i, 1); } } return p.NewStr(); } return 0; } char *VIo::UnMultiLine(char *In) { if (In) { LStringPipe p; char *n; for (char *i=In; i && *i; i=n) { n = stristr(i, "\\n"); if (n) { p.Write(i, (int)(n-i)); p.Push((char*)"\n"); n += 2; } else { p.Push(i); } } return p.NewStr(); } return 0; } ///////////////////////////////////////////////////////////// // VCard class bool VCard::Import(LDataPropI *c, LStreamI *s) { bool Status = false; if (!c || !s) return false; LString Field; ParamArray Params; LString Data; ssize_t PrefEmail = -1; LArray Emails; while (ReadField(*s, Field, &Params, Data)) { if (_stricmp(Field, "begin") == 0 && _stricmp(Data, "vcard") == 0) { while (ReadField(*s, Field, &Params, Data)) { if (_stricmp(Field, "end") == 0 && _stricmp(Data, "vcard") == 0) goto ExitLoop; if (IsVar(Field, "n")) { - LToken Name(Data, ";", false); + auto Name = Data.SplitDelimit(";", -1, false); char *First = Name[1]; char *Last = Name[0]; char *Title = Name[3]; if (First) { c->SetStr(FIELD_FIRST_NAME, First); Status = true; } if (Last) { c->SetStr(FIELD_LAST_NAME, Last); Status = true; } if (Title) { c->SetStr(FIELD_TITLE, Title); Status = true; } } else if (IsVar(Field, "nickname")) { c->SetStr(FIELD_NICK, Data); } else if (IsVar(Field, "tel")) { - LToken Phone(Data, ";", false); + auto Phone = Data.SplitDelimit(";", -1, false); for (uint32_t p=0; pSetStr(FIELD_WORK_MOBILE, Phone[p]); } else { c->SetStr(FIELD_HOME_MOBILE, Phone[p]); } } else if (IsType("fax")) { if (IsType("Work")) { c->SetStr(FIELD_WORK_FAX, Phone[p]); } else { c->SetStr(FIELD_HOME_FAX, Phone[p]); } } else { if (IsType("Work")) { c->SetStr(FIELD_WORK_PHONE, Phone[p]); } else { c->SetStr(FIELD_HOME_PHONE, Phone[p]); } } } } else if (IsVar(Field, "email")) { if (IsType("pref")) { PrefEmail = Emails.Length(); } Emails.Add(NewStr(Data)); } else if (IsVar(Field, "org")) { - LToken Org(Data, ";", false); + auto Org = Data.SplitDelimit(";", -1, false); if (Org[0]) c->SetStr(FIELD_COMPANY, Org[0]); } else if (IsVar(Field, "adr")) { bool IsWork = IsType("work"); // bool IsHome = IsType("home"); - LToken Addr(Data, ";", false); + auto Addr = Data.SplitDelimit(";", -1, false); if (Addr[2]) { - LToken A(Addr[2], "\r\n"); + auto A = Addr[2].SplitDelimit("\r\n"); if (A.Length() > 1) { c->SetStr(IsWork ? FIELD_WORK_STREET : FIELD_HOME_STREET, A[0]); if (A[1]) c->SetStr(IsWork ? FIELD_WORK_SUBURB : FIELD_HOME_SUBURB, A[1]); if (A[2]) c->SetStr(IsWork ? FIELD_WORK_COUNTRY : FIELD_HOME_COUNTRY, A[2]); } else { c->SetStr(IsWork ? FIELD_WORK_STREET : FIELD_HOME_STREET, Addr[2]); } } if (Addr[3]) c->SetStr(IsWork ? FIELD_WORK_SUBURB : FIELD_HOME_SUBURB, Addr[3]); if (Addr[4]) c->SetStr(IsWork ? FIELD_WORK_STATE : FIELD_HOME_STATE, Addr[4]); if (Addr[5]) c->SetStr(IsWork ? FIELD_WORK_POSTCODE : FIELD_HOME_POSTCODE, Addr[5]); if (Addr[6]) c->SetStr(IsWork ? FIELD_WORK_COUNTRY : FIELD_HOME_COUNTRY, Addr[6]); } else if (IsVar(Field, "note")) { c->SetStr(FIELD_NOTE, Data); } else if (IsVar(Field, "uid")) { - LToken n(Data, ";", false); + auto n = Data.SplitDelimit(";", -1, false); c->SetStr(FIELD_UID, n[0]); } else if (IsVar(Field, "x-perm")) { int Perms = atoi(Data); c->SetInt(FIELD_PERMISSIONS, Perms); } else if (IsVar(Field, "url")) { bool IsWork = IsType("work"); bool IsHome = IsType("home"); if (IsWork) { c->SetStr(FIELD_HOME_WEBPAGE, Data); } else if (IsHome) { c->SetStr(FIELD_WORK_WEBPAGE, Data); } } else if (IsVar(Field, "nickname")) { c->SetStr(FIELD_NICK, Data); } else if (IsVar(Field, "photo")) { size_t B64Len = strlen(Data); ssize_t BinLen = BufferLen_64ToBin(B64Len); LAutoPtr Bin(new uint8_t[BinLen]); if (Bin) { ssize_t Bytes = ConvertBase64ToBinary(Bin.Get(), BinLen, Data, B64Len); LVariant v; if (v.SetBinary(Bytes, Bin.Release(), true)) { c->SetVar(FIELD_CONTACT_IMAGE, &v); } } } } } } ExitLoop: if (Emails.Length()) { if (PrefEmail < 0) PrefEmail = 0; c->SetStr(FIELD_EMAIL, Emails[PrefEmail]); Emails.DeleteAt(PrefEmail); if (Emails.Length()) { LStringPipe p; for (uint32_t i=0; iSetStr(FIELD_ALT_EMAIL, v); DeleteArray(v); } } } ClearFields(); return Status; } bool VIo::ReadField(LStreamI &s, LString &Name, ParamArray *Params, LString &Data) { bool Status = false; ParamArray LocalParams; Name.Empty(); Data.Empty(); if (Params) Params->Empty(); else Params = &LocalParams; char Temp[1024]; LArray p; bool Done = false; while (!Done) { bool EatNext = false; ReadNextLine: Temp[0] = 0; int64 r = d->Buf.Pop(Temp, sizeof(Temp)); if (r <= 0) { // Try reading more data... r = s.Read(Temp, sizeof(Temp)); if (r > 0) d->Buf.Write(Temp, r); else break; r = d->Buf.Pop(Temp, sizeof(Temp)); } else if (Temp[0] == '\r' || Temp[1] == '\n') { // Skip empty lines... goto ReadNextLine; } if (r <= 0) break; if ((r == 1 && Temp[0] == '\n') || (r == 2 && Temp[0] == '\r' && Temp[1] == '\n')) // Blank line case... continue; // Unfold for (char *c = Temp; *c; c++) { if (*c == '\r') { // do nothing } else if (*c == '\n') { char Next; r = d->Buf.Peek((uchar*) &Next, 1); if (r == 0) { r = s.Read(Temp, sizeof(Temp)); if (r <= 0) break; d->Buf.Write(Temp, (int)r); r = d->Buf.Peek((uchar*) &Next, 1); } if (r == 1) { if (Next == ' ' || Next == '\t') { // Wrapped, do nothing EatNext = true; goto ReadNextLine; } else { Done = true; break; } } else { break; } } else if (EatNext) { EatNext = false; } else { p.Add(*c); } } } p.Add(0); char *f = p.Length() > 1 ? &p[0] : 0; if (f) { char *e = strchr(f, ':'); if (e) { *e++ = 0; - LToken t(f, ";"); + auto t = LString(f).SplitDelimit(";"); if (t.Length() > 0) { Name = t[0]; for (uint32_t i=1; iFind("charset"); if (Charset) { LAutoString u((char*)LNewConvertCp("utf-8", e, Charset)); Data = u.Get(); } else { Data = e; } Status = Name.Length() > 0; } return Status; } void VIo::WriteField(LStreamI &s, const char *Name, ParamArray *Params, const char *Data) { if (Name && Data) { int64 Size = s.GetSize(); LStreamPrint(&s, "%s", Name); if (Params) { for (uint32_t i=0; iLength(); i++) { Parameter &p = (*Params)[i]; LStreamPrint(&s, "%s%s=%s", i?"":";", p.Field.Get(), p.Value.Get()); } } bool Is8Bit = false; bool HasEq = false; for (uint8_t *c = (uint8_t*)Data; *c; c++) { if ((*c & 0x80) != 0) { Is8Bit = true; } else if (*c == '=') { HasEq = true; } } if (Is8Bit || HasEq) { if (Is8Bit) s.Write((char*)";charset=utf-8", 14); s.Write((char*)";encoding=quoted-printable", 26); } s.Write((char*)":", 1); Fold(s, Data, (int) (s.GetSize() - Size)); s.Write((char*)"\r\n", 2); } } bool VCard::Export(LDataPropI *c, LStreamI *o) { if (!c || !o) return false; bool Status = true; char s[512]; const char *Empty = ""; o->Push("begin:vcard\r\n"); o->Push("version:3.0\r\n"); const char *First = 0, *Last = 0, *Title = 0; First = c->GetStr(FIELD_FIRST_NAME); Last = c->GetStr(FIELD_LAST_NAME); Title = c->GetStr(FIELD_TITLE); if (First || Last) { sprintf_s(s, sizeof(s), "%s;%s;%s", Last?Last:Empty, First?First:Empty, Title?Title:Empty); WriteField(*o, "n", 0, s); } #define OutputTypedField(Field, Name, Type) \ { const char *str = 0; \ if ((str = c->GetStr(Name))) \ { \ WriteField(*o, Field, Type, str); \ } } #define OutputField(Field, Name) \ { const char *str = 0; \ if ((str = c->GetStr(Name))) \ { \ WriteField(*o, Field, 0, str); \ } } ParamArray Work("Work"), WorkCell("Work,Cell"), WorkFax("Work,Fax"); ParamArray Home("Home"), HomeCell("Home,Cell"), HomeFax("Home,Fax"); ParamArray InetPref("internet,pref"), Inet("internet"); OutputTypedField("tel", FIELD_WORK_PHONE, &Work); OutputTypedField("tel", FIELD_WORK_MOBILE, &WorkCell); OutputTypedField("tel", FIELD_WORK_FAX, &WorkFax); OutputTypedField("tel", FIELD_HOME_PHONE, &Home); OutputTypedField("tel", FIELD_HOME_MOBILE, &HomeCell); OutputTypedField("tel", FIELD_HOME_FAX, &HomeFax); OutputField("org", FIELD_COMPANY); OutputTypedField("email", FIELD_EMAIL, &InetPref); const char *Alt; if ((Alt = c->GetStr(FIELD_ALT_EMAIL))) { - LToken t(Alt, ","); + auto t = LString(Alt).SplitDelimit(","); for (unsigned i=0; iGetStr(FIELD_UID))) { LStreamPrint(o, "UID:%s\r\n", Uid); } const char *Street, *Suburb, *PostCode, *State, *Country; Street = Suburb = PostCode = State = Country = 0; Street = c->GetStr(FIELD_HOME_STREET); Suburb = c->GetStr(FIELD_HOME_SUBURB); PostCode = c->GetStr(FIELD_HOME_POSTCODE); State = c->GetStr(FIELD_HOME_STATE); Country = c->GetStr(FIELD_HOME_COUNTRY); if (Street || Suburb || PostCode || State || Country) { sprintf_s(s, sizeof(s), ";;%s;%s;%s;%s;%s", Street?Street:Empty, Suburb?Suburb:Empty, State?State:Empty, PostCode?PostCode:Empty, Country?Country:Empty); WriteField(*o, "adr", &Home, s); } Street = Suburb = PostCode = State = Country = 0; Street = c->GetStr(FIELD_WORK_STREET); Suburb = c->GetStr(FIELD_WORK_SUBURB); PostCode = c->GetStr(FIELD_WORK_POSTCODE); State = c->GetStr(FIELD_WORK_STATE); Country = c->GetStr(FIELD_WORK_COUNTRY); if (Street || Suburb || PostCode || State || Country) { sprintf_s(s, sizeof(s), ";;%s;%s;%s;%s;%s", Street?Street:Empty, Suburb?Suburb:Empty, State?State:Empty, PostCode?PostCode:Empty, Country?Country:Empty); WriteField(*o, "adr", &Work, s); } // OutputField("X-Perm", FIELD_PERMISSIONS); const char *Url; if ((Url = c->GetStr(FIELD_HOME_WEBPAGE))) { WriteField(*o, "url", &Home, Url); } if ((Url = c->GetStr(FIELD_WORK_WEBPAGE))) { WriteField(*o, "url", &Work, Url); } const char *Nick; if ((Nick = c->GetStr(FIELD_NICK))) { WriteField(*o, "nickname", 0, Nick); } const char *Note; if ((Note = c->GetStr(FIELD_NOTE))) { WriteField(*o, "note", 0, Note); } const LVariant *Photo = c->GetVar(FIELD_CONTACT_IMAGE); if (Photo && Photo->Type == GV_BINARY) { ssize_t B64Len = BufferLen_BinTo64(Photo->Value.Binary.Length); LAutoPtr B64Buf(new char[B64Len]); if (B64Buf) { ssize_t Bytes = ConvertBinaryToBase64(B64Buf, B64Len, (uchar*)Photo->Value.Binary.Data, Photo->Value.Binary.Length); if (Bytes > 0) { LStreamPrint(o, "photo;type=jpeg;encoding=base64:\r\n"); int LineChar = 76; for (ssize_t i=0; i LineChar ? LineChar : Remain; o->Write(" ", 1); o->Write(B64Buf + i, Wr); o->Write("\r\n", 2); i += Wr; } } } } o->Push("end:vcard\r\n"); return Status; } ///////////////////////////////////////////////////////////// // VCal class int StringToWeekDay(const char *s) { const char *days[] = {"SU","MO","TU","WE","TH","FR","SA"}; for (unsigned i=0; i TzInfos; TimeZoneInfo *TzInfo = NULL; bool IsNormalTz = false, IsDaylightTz = false; LJson To; int Attendee = 0; LArray Alarms; int AlarmIdx = -1; while (ReadField(*In, Field, &Params, Data)) { if (!_stricmp(Field, "begin")) { if (_stricmp(Data, "vevent") == 0 || _stricmp(Data, "vtodo") == 0) { IsEvent = true; SectionType = Data; int Type = _stricmp(Data, "vtodo") == 0 ? 1 : 0; c->SetInt(FIELD_CAL_TYPE, Type); } else if (!_stricmp(Data, "vtimezone")) { IsTimeZone = true; TzInfo = &TzInfos.New(); } else if (_stricmp(Data, "vcalendar") == 0) IsCal = true; else if (!_stricmp(Data, "standard")) IsNormalTz = true; else if (!_stricmp(Data, "daylight")) IsDaylightTz = true; else if (!_stricmp(Data, "valarm")) { IsAlarm = true; AlarmIdx++; } } else if (_stricmp(Field, "end") == 0) { if (_stricmp(Data, "vcalendar") == 0) { IsCal = false; } else if (SectionType && _stricmp(Data, SectionType) == 0) { Status = true; IsEvent = false; break; // exit loop } else if (!_stricmp(Data, "vtimezone")) { IsTimeZone = false; TzInfo = NULL; } else if (!_stricmp(Data, "standard")) IsNormalTz = false; else if (!_stricmp(Data, "daylight")) IsDaylightTz = false; else if (!_stricmp(Data, "valarm")) IsAlarm = false; } else if (IsEvent) { if (IsAlarm) { auto &a = Alarms[AlarmIdx]; if (IsVar(Field, "ACTION")) a.Action = Data; else if (IsVar(Field, "DESCRIPTION")) a.Desc = Data; else if (IsVar(Field, "TRIGGER")) a.Trigger = Data; else if (IsVar(Field, "X-PARAM")) a.Param = Data; } else if (IsVar(Field, "dtstart")) { ParseDate(EventStart, Data); StartTz = Params.Find("TZID"); } else if (IsVar(Field, "dtend")) { ParseDate(EventEnd, Data); EndTz = Params.Find("TZID"); } else if (IsVar(Field, "summary")) { c->SetStr(FIELD_CAL_SUBJECT, Data); } else if (IsVar(Field, "description")) { LAutoString Sum(UnMultiLine(Data)); if (Sum) c->SetStr(FIELD_CAL_NOTES, Sum); } else if (IsVar(Field, "location")) { c->SetStr(FIELD_CAL_LOCATION, Data); } else if (IsVar(Field, "uid")) { char *Uid = Data; c->SetStr(FIELD_UID, Uid); } else if (IsVar(Field, "x-showas")) { char *n = Data; if (Stricmp(n, "TENTATIVE") == 0) c->SetInt(FIELD_CAL_SHOW_TIME_AS, CalTentative); else if (Stricmp(n, "BUSY") == 0) c->SetInt(FIELD_CAL_SHOW_TIME_AS, CalBusy); else if (Stricmp(n, "OUT") == 0) c->SetInt(FIELD_CAL_SHOW_TIME_AS, CalOut); else c->SetInt(FIELD_CAL_SHOW_TIME_AS, CalFree); } else if (IsVar(Field, "attendee")) { auto Email = Data.SplitDelimit(":=").Last(); if (LIsValidEmail(Email)) { const char *Name = Params.Find("CN"); const char *Role = Params.Find("Role"); LString k; k.Printf("[%i].email", Attendee); To.Set(k, Email); if (Name) { k.Printf("[%i].name", Attendee); To.Set(k, Name); } if (Role) { k.Printf("[%i].role", Attendee); To.Set(k, Role); } Attendee++; } } else if (IsVar(Field, "status")) { c->SetStr(FIELD_CAL_STATUS, Data); } } else if (IsTimeZone && TzInfo) { /* e.g.: TZID:Pacific Standard Time BEGIN:STANDARD DTSTART:16010101T020000 TZOFFSETFROM:-0700 TZOFFSETTO:-0800 RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=1SU;BYMONTH=11 END:STANDARD BEGIN:DAYLIGHT DTSTART:16010101T020000 TZOFFSETFROM:-0800 TZOFFSETTO:-0700 RRULE:FREQ=YEARLY;INTERVAL=1;BYDAY=2SU;BYMONTH=3 END:DAYLIGHT */ if (IsVar(Field, "TZID")) { TzInfo->Name = Data; } else if (IsNormalTz || IsDaylightTz) { TimeZoneSection &Sect = IsNormalTz ? TzInfo->Normal : TzInfo->Daylight; if (IsVar(Field, "DTSTART")) ParseDate(Sect.Start, Data); else if (IsVar(Field, "TZOFFSETFROM")) Sect.From = (int)Data.Int(); else if (IsVar(Field, "TZOFFSETTO")) Sect.To = (int)Data.Int(); else if (IsVar(Field, "RRULE")) Sect.Rule = Data; } } } if (Attendee > 0) { auto j = To.GetJson(); c->SetStr(FIELD_ATTENDEE_JSON, j); } if (StartTz || EndTz) { // Did we get a timezone defn? TimeZoneInfo *Match = NULL; for (unsigned i=0; iNormal, EventStart.Year()) && EvalRule(Dst, Match->Daylight, EventStart.Year())) { bool IsDst = false; if (Dst < Norm) { // DST in the middle of the year // |Jan----DST------Norm----Dec| if (EventStart >= Dst && EventStart <= Norm) IsDst = true; } else { // DST over the start and end of the year // |Jan----Norm------DST----Dec| if (EventStart >= Norm && EventStart <= Dst) IsDst = false; else IsDst = true; } #if DEBUG_LOGGING LgiTrace("Eval Start=%s, Norm=%s, Dst=%s, IsDst=%i\n", EventStart.Get().Get(), Norm.Get().Get(), Dst.Get().Get(), IsDst); #endif EffectiveTz = IsDst ? Match->Daylight.To : Match->Normal.To; LString sTz; sTz.Printf("%4.4i,%s", EffectiveTz, StartTz.Get()); c->SetStr(FIELD_CAL_TIMEZONE, sTz); } else goto StoreStringTz; } else { // Store whatever they gave us StoreStringTz: if (StartTz.Equals(EndTz)) c->SetStr(FIELD_CAL_TIMEZONE, StartTz); else if (StartTz.Get() && EndTz.Get()) { LString s; s.Printf("%s,%s", StartTz.Get(), EndTz.Get()); c->SetStr(FIELD_CAL_TIMEZONE, s); } else if (StartTz) c->SetStr(FIELD_CAL_TIMEZONE, StartTz); else if (EndTz) c->SetStr(FIELD_CAL_TIMEZONE, EndTz); if (StartTz) EffectiveTz = TimezoneToOffset(StartTz); } if (EffectiveTz) { // Convert the event to UTC int e = abs(EffectiveTz); int Mins = (((e / 100) * 60) + (e % 100)) * (EffectiveTz < 0 ? -1 : 1); #if DEBUG_LOGGING LgiTrace("%s:%i - EffectiveTz=%i, Mins=%i\n", _FL, EffectiveTz, Mins); #endif if (EventStart.IsValid()) { #if DEBUG_LOGGING LgiTrace("EventStart=%s\n", EventStart.Get().Get()); #endif EventStart.AddMinutes(-Mins); #if DEBUG_LOGGING LgiTrace("EventStart=%s\n", EventStart.Get().Get()); #endif } if (EventEnd.IsValid()) EventEnd.AddMinutes(-Mins); } } if (EventStart.IsValid()) c->SetDate(FIELD_CAL_START_UTC, &EventStart); if (EventEnd.IsValid()) c->SetDate(FIELD_CAL_END_UTC, &EventEnd); if (Alarms.Length()) { LString::Array s; for (LAlarm &a: Alarms) { LString r = a.GetStr(); if (r) s.Add(r); } if (s.Length()) { c->SetStr(FIELD_CAL_REMINDERS, LString("\n").Join(s)); } } ClearFields(); return Status; } LString ToString(LDateTime &dt) { LString s; s.Printf("%04.4i%02.2i%02.2iT%02.2i%02.2i%02.2i%s", dt.Year(), dt.Month(), dt.Day(), dt.Hours(), dt.Minutes(), dt.Seconds(), dt.GetTimeZone() ? "" : "Z"); return s; } bool VCal::Export(LDataPropI *c, LStreamI *o) { if (!c || !o) return false; int64 Type = c->GetInt(FIELD_CAL_TYPE); // auto *TimeZone = c->GetStr(FIELD_CAL_TIMEZONE); char *TypeStr = Type == 1 ? (char*)"VTODO" : (char*)"VEVENT"; o->Push((char*)"BEGIN:VCALENDAR\r\n" "VERSION:2.0\r\n" "CALSCALE:GREGORIAN\r\n" "PRODID:Memecode vcal filter\r\n"); if (c) { LStreamPrint(o, "BEGIN:%s\r\n", TypeStr); #define OutputStr(Field, Name) \ { \ const char *_s = 0; \ if ((_s = c->GetStr(Field))) \ { \ WriteField(*o, Name, 0, _s); \ } \ } LDateTime Now; Now.SetNow(); Now.ToUtc(); LStreamPrint(o, "DTSTAMP:%s\r\n", ToString(Now).Get()); OutputStr(FIELD_CAL_SUBJECT, "SUMMARY"); OutputStr(FIELD_CAL_LOCATION, "LOCATION"); OutputStr(FIELD_CAL_NOTES, "DESCRIPTION"); OutputStr(FIELD_CAL_STATUS, "STATUS"); int64 ShowAs; if ((ShowAs = c->GetInt(FIELD_CAL_SHOW_TIME_AS))) { switch (ShowAs) { default: case CalFree: o->Push((char*)"X-SHOWAS:FREE\r\n"); break; case CalTentative: o->Push((char*)"X-SHOWAS:TENTATIVE\r\n"); break; case CalBusy: o->Push((char*)"X-SHOWAS:BUSY\r\n"); break; case CalOut: o->Push((char*)"X-SHOWAS:OUT\r\n"); break; } } const char *Uid; if ((Uid = c->GetStr(FIELD_UID))) { LStreamPrint(o, "UID:%s\r\n", Uid); } const LDateTime *Dt; if ((Dt = c->GetDate(FIELD_CAL_START_UTC))) { LDateTime dt = *Dt; dt.ToUtc(); LStreamPrint(o, "DTSTART:%s\r\n", ToString(dt).Get()); } if ((Dt = c->GetDate(FIELD_CAL_END_UTC))) { LDateTime dt = *Dt; dt.ToUtc(); LStreamPrint(o, "DTEND:%s\r\n", ToString(dt).Get()); } const char *Reminders; if ((Reminders = c->GetStr(FIELD_CAL_REMINDERS))) { auto Lines = LString(Reminders).SplitDelimit("\n"); for (auto Ln: Lines) { // Fields are: Number, CalendarReminderUnits, CalendarReminderType, Param auto p = Ln.SplitDelimit(","); if (p.Length() >= 3) { const char *Duration = NULL; int64 Count = p[0].Int(); switch (p[1].Int()) { case CalMinutes: Duration = "M"; break; case CalHours: Duration = "H"; break; case CalDays: Duration = "D"; break; case CalWeeks: Duration = "D"; Count *= 7; break; } const char *Action = NULL; switch (p[2].Int()) { case CalEmail: Action = "EMAIL"; break; case CalPopup: Action = "DISPLAY"; break; case CalScriptCallback: Action = "X-SCRIPT"; break; } if (Action && Duration) { LString s; s.Printf( "BEGIN:VALARM\r\n" "ACTION:%s\r\n" "DESCRIPTION:REMINDER\r\n" "TRIGGER:-PT" LPrintfInt64 "%s\r\n", Action, Count, Duration ); if (p.Length() > 3) s += LString("X-PARAM: ") + p[3]; s += "END:VALARM\r\n"; o->Write(s.Get(), s.Length()); } else LAssert(0); } } } LJson j(c->GetStr(FIELD_ATTENDEE_JSON)); for (auto g: j.GetArray(NULL)) { // e.g.: ATTENDEE;CN="Matthew Allen";ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:matthew@company.com.au auto Email = g.Get("email"); if (Email) { auto Name = g.Get("name"); auto Role = g.Get("role"); char s[400]; int ch = sprintf_s(s, sizeof(s), "ATTENDEE"); if (Name) ch += sprintf_s(s+ch, sizeof(s)-ch, ";CN=\"%s\"", Name.Get()); if (Role) ch += sprintf_s(s+ch, sizeof(s)-ch, ";ROLE=\"%s\"", Role.Get()); ch += sprintf_s(s+ch, sizeof(s)-ch, ":MAILTO=%s\r\n", Email.Get()); o->Write(s, ch); } } LStreamPrint(o, "END:%s\r\n", TypeStr); } o->Push((char*)"END:VCALENDAR\r\n"); return true; } diff --git a/src/win/Lgi/App.cpp b/src/win/Lgi/App.cpp --- a/src/win/Lgi/App.cpp +++ b/src/win/Lgi/App.cpp @@ -1,924 +1,923 @@ #define _WIN32_WINNT 0x600 #ifndef _MSC_VER #include #endif #include #include #include #include #include #include "lgi/common/Lgi.h" #include "lgi/common/SkinEngine.h" #include "lgi/common/DocView.h" -#include "lgi/common/Token.h" #include "lgi/common/Css.h" #include "lgi/common/SpellCheck.h" #include "lgi/common/Json.h" #include "AppPriv.h" // Don't have a better place to put this... const char LSpellCheck::Delimiters[] = { ' ', '\t', '\r', '\n', ',', ',', '.', ':', ';', '{', '}', '[', ']', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '-', '+', '=', '|', '\\', '/', '?', '\"', 0 }; HINSTANCE _lgi_app_instance = NULL; extern LPTOP_LEVEL_EXCEPTION_FILTER _PrevExceptionHandler; OsAppArguments::OsAppArguments() { _Default(); } OsAppArguments::OsAppArguments(int Args, const char **Arg) { _Default(); Set(Args, Arg); } OsAppArguments &OsAppArguments::operator =(OsAppArguments &p) { hInstance = p.hInstance; Pid = p.Pid; nCmdShow = p.nCmdShow; lpCmdLine = 0; CmdLine.Reset(NewStrW(p.lpCmdLine)); lpCmdLine = CmdLine; return *this; } void OsAppArguments::_Default() { hInstance = 0; lpCmdLine = 0; Pid = GetCurrentProcessId(); nCmdShow = SW_RESTORE; } void OsAppArguments::Set(int Args, const char **Arg) { LStringPipe p; for (int i=0; i_ExceptionFilter(e, LAppInst->d->ProductId); else LgiTrace("_ExceptionFilter_Redir error: No application ptr.\n"); return 0; } ///////////////////////////////////////////////////////////////////////////// LSkinEngine *LApp::SkinEngine = NULL; LMouseHook *LApp::MouseHook = NULL; LMouseHook *LApp::GetMouseHook() { return MouseHook; } static LAutoString ParseStr(LPointer &p, bool Pad = true) { char16 *Key = p.w; // Skip 'key' string while (*p.w) p.w++; // Skip NULL p.w++; if (Pad) { // Align to 32-bit boundry while ((NativeInt)p.u8 & 3) p.u8++; } return LAutoString(WideToUtf8(Key)); } static LAutoString ParseVer(void *Resource, char *Part) { - LToken Parts(Part, "."); + auto Parts = LString(Part).SplitDelimit("."); if (Parts.Length() == 3) { LPointer p; p.u8 = (uint8_t*)Resource; uint16 Len = *p.u16++; uint16 ValueLen = *p.u16++; uint16 Type = *p.u16++; LAutoString Key = ParseStr(p); // Read VS_FIXEDFILEINFO structure DWORD dwSig = *p.u32++; DWORD dwStrucVersion = *p.u32++; DWORD dwFileVersionMS = *p.u32++; DWORD dwFileVersionLS = *p.u32++; DWORD dwProductVersionMS = *p.u32++; DWORD dwProductVersionLS = *p.u32++; DWORD dwFileFlagsMask = *p.u32++; DWORD dwFileFlags = *p.u32++; DWORD dwFileOS = *p.u32++; DWORD dwFileType = *p.u32++; DWORD dwFileSubtype = *p.u32++; DWORD dwFileDateMS = *p.u32++; DWORD dwFileDateLS = *p.u32++; // Align to 32-bit boundry while ((NativeInt)p.u8 & 3) p.u8++; // Children while (p.u8 < (uint8_t*)Resource + Len) { // Read StringFileInfo structures... uint8_t *fStart = p.u8; uint16 fLength = *p.u16++; uint16 fValueLength = *p.u16++; uint16 fType = *p.u16++; LAutoString fKey = ParseStr(p); if (strcmp(fKey, "StringFileInfo")) break; while (p.u8 < fStart + fLength) { // Read StringTable entries uint8_t *tStart = p.u8; uint16 tLength = *p.u16++; uint16 tValueLength = *p.u16++; uint16 tType = *p.u16++; LAutoString tKey = ParseStr(p); while (p.u8 < tStart + tLength) { // Read String entries uint8_t *sStart = p.u8; uint16 sLength = *p.u16++; uint16 sValueLength = *p.u16++; uint16 sType = *p.u16++; LAutoString sKey = ParseStr(p); LAutoString sValue; if (p.u8 < sStart + sLength) sValue = ParseStr(p); if (!stricmp(Parts[0], fKey) && !stricmp(Parts[1], tKey) && !stricmp(Parts[2], sKey)) { return sValue; } } } } } return LAutoString(); } void LInvalidParam(const wchar_t * expression, const wchar_t * function, const wchar_t * file, unsigned int line, uintptr_t pReserved) { LgiTrace("Invalid Parameter: %S (%S @ %S:%i)\n", expression, function, file, line); } ///////////////////////////////////////////////////////////////////////////// #include #include extern int MouseRollMsg; typedef HRESULT (CALLBACK *fDllGetVersion)(DLLVERSIONINFO *); LApp::LApp(OsAppArguments &AppArgs, const char *AppName, LAppArguments *Opts) { // LApp instance LAssert(TheApp == 0); TheApp = this; LAssert(AppName != NULL); Name(AppName); int64 Time = LCurrentTime(); #define DumpTime(str) /* \ { int64 n = LCurrentTime(); \ LgiTrace("%s=%ims\n", str, (int)(n-Time)); \ Time = n; } */ // Sanity Checks LAssert(sizeof(int8) == 1); LAssert(sizeof(uint8_t) == 1); LAssert(sizeof(int16) == 2); LAssert(sizeof(uint16) == 2); LAssert(sizeof(int32) == 4); LAssert(sizeof(uint32_t) == 4); LAssert(sizeof(int64) == 8); LAssert(sizeof(uint64) == 8); LAssert(sizeof(char16) == 2); LAssert(LDisplayString::FScale == (1 << LDisplayString::FShift)); DumpTime("start"); // Private data d = new LAppPrivate(this); char Mime[256]; sprintf_s(Mime, sizeof(Mime), "application/x-%s", AppName); d->Mime.Reset(NewStr(Mime)); DumpTime("priv"); InitializeCriticalSection(&StackTraceSync); if (!Opts || Opts->NoCrashHandler == false) { // Setup exception handler HRSRC hRsrc = ::FindResource(NULL, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION); HGLOBAL hGlobal = ::LoadResource(NULL, hRsrc); LPVOID pVersionResource = ::LockResource(hGlobal); LAutoString ProductName, ProductVer; // replace "040904e4" with the language ID of your resources if (pVersionResource) { ProductName = ParseVer(pVersionResource, "StringFileInfo.0c0904b0.ProductName"); ProductVer = ParseVer(pVersionResource, "StringFileInfo.0c0904b0.ProductVersion"); } if (ProductName && ProductVer) { char s[256]; sprintf_s(s, sizeof(s), "%s-%s", ProductName.Get(), ProductVer.Get()); d->ProductId.Reset(NewStr(s)); } #if !defined(_DEBUG) _PrevExceptionHandler = SetUnhandledExceptionFilter(_ExceptionFilter_Redir); #endif _set_invalid_parameter_handler(LInvalidParam); } DumpTime("exception handler"); // Initialize windows dll's OleInitialize(NULL); CoInitialize(NULL); InitCommonControls(); { /* LLibrary ComCtl32("ComCtl32.dll"); DLLVERSIONINFO info; ZeroObj(info); info.cbSize = sizeof(info); fDllGetVersion DllGetVersion = (fDllGetVersion)ComCtl32.GetAddress("DllGetVersion"); if (DllGetVersion) { HRESULT ret = DllGetVersion(&info); d->ThemeAware = info.dwMajorVersion >= 6; LgiTrace("ComCtl32.dll v%i.%i found (ret=%x)\n", info.dwMajorVersion, info.dwMinorVersion, ret); ) */ LArray Ver; LGetOs(&Ver); if (Ver.Length() > 1) { // LgiTrace("Windows v%i.%i\n", Ver[0], Ver[1]); if (Ver[0] < 6) { d->ThemeAware = false; } } #ifdef _MSC_VER if (!d->ThemeAware) { SetThemeAppProperties(0); } #endif } DumpTime("init common ctrls"); // Setup LGI Sub-systems LFontSystem::Inst(); DumpTime("font sys"); d->FileSystem = new LFileSystem; DumpTime("file sys"); d->GdcSystem = new GdcDevice; DumpTime("gdc"); // Vars... AppWnd = 0; SetAppArgs(AppArgs); DumpTime("vars"); // System font LFontType SysFontType; if (SysFontType.GetSystemFont("System")) { SystemNormal = SysFontType.Create(); if (SystemNormal) { // Force load SystemNormal->Create(); SystemNormal->WarnOnDelete(true); } else { LgiMsg(0, "Error: SysFontType.Create() failed.", "Lgi Error"); LExitApp(); } SystemBold = SysFontType.Create(); if (SystemBold) { SystemBold->Bold(true); SystemBold->Create(); } } else { LgiMsg(0, "Error: GetSystemFont failed.", "Lgi Error"); LExitApp(); } DumpTime("fonts"); // Other vars and init hNormalCursor = LoadCursor(NULL, IDC_ARROW); LRandomize((uint) (LCurrentTime()*GetCurrentThreadId())); MouseRollMsg = RegisterWindowMessage(L"MSWHEEL_ROLLMSG"); DumpTime("cursor/rand/msg"); LColour::OnChange(); DumpTime("colours"); // Setup mouse hook MouseHook = new LMouseHook; DumpTime("ms hook"); if ( (!Opts || !Opts->NoSkin) && !GetOption("noskin") ) { extern LSkinEngine *CreateSkinEngine(LApp *App); SkinEngine = CreateSkinEngine(this); } DumpTime("skin"); } LApp::~LApp() { CommonCleanup(); DeleteObj(AppWnd); SystemNormal->WarnOnDelete(false); DeleteObj(SystemNormal); DeleteObj(SystemBold); DeleteObj(MouseHook); TheApp = NULL; DeleteObj(SkinEngine); DeleteObj(LFontSystem::Me); DeleteObj(d->FileSystem); DeleteObj(d->GdcSystem); d->Classes.DeleteObjects(); CoUninitialize(); OleUninitialize(); DeleteObj(d); DeleteCriticalSection(&StackTraceSync); } bool LApp::IsOk() { bool Status = (this != 0) && (d != 0) && (d->FileSystem != 0) && (d->GdcSystem != 0); if (!Status) LAssert(!"Hash table error"); return Status; } int LApp::GetCpuCount() { SYSTEM_INFO si; ZeroObj(si); GetSystemInfo(&si); return si.dwNumberOfProcessors ? si.dwNumberOfProcessors : -1; } OsThread LApp::_GetGuiThread() { return d->GuiThread; } bool LApp::InThread() { auto GuiId = GetGuiThreadId(); auto MyId = GetCurrentThreadId(); return GuiId == MyId; } OsThreadId LApp::GetGuiThreadId() { return GetThreadId(d->GuiThread); } LApp::ClassContainer *LApp::GetClasses() { return IsOk() ? &d->Classes : 0; } OsAppArguments *LApp::GetAppArgs() { return IsOk() ? &d->Args : 0; } const char *LApp::GetArgumentAt(int n) { if (d->Args.lpCmdLine) { char16 *s = d->Args.lpCmdLine; for (int i=0; i<=n; i++) { char16 *e = 0; while (*s && strchr(WhiteSpace, *s)) s++; if (*s == '\'' || *s == '\"') { char16 Delim = *s++; e = StrchrW(s, Delim); } else { for (e = s; *e && !strchr(WhiteSpace, *e); e++) ; } if (i == n) { return WideToUtf8(s, e - s); } s = *e ? e + 1 : e; } } return 0; } bool LApp::GetOption(const char *Option, LString &Buf) { if (!ValidStr(Option)) { return false; } char16 *c = d->Args.lpCmdLine; char16 *Opt = Utf8ToWide(Option); auto OptLen = StrlenW(Opt); while (c && *c) { if (*c == '/' || *c == '-') { c++; char16 *e = c; while (*e && (IsAlpha(*e)) || StrchrW(L"_-", *e)) e++; if (e - c == OptLen && !StrnicmpW(c, Opt, OptLen)) { c += OptLen; if (*c) { // skip leading whitespace while (*c && strchr(WhiteSpace, *c)) { c++; } // write str out if they want it char16 End = (*c == '\'' || *c == '\"') ? *c++ : ' '; char16 *e = StrchrW(c, End); if (!e) e = c + StrlenW(c); Buf.SetW(c, e-c); } // yeah we got the option DeleteArray(Opt); return true; } } c = StrchrW(c, ' '); if (c) c++; } DeleteArray(Opt); return false; } bool LApp::GetOption(const char *Option, char *Dest, int DestLen) { LString Buf; if (GetOption(Option, Buf)) { if (Dest) strcpy_s(Dest, DestLen, &Buf[0]); return true; } return false; } // #include "LInput.h" void LApp::SetAppArgs(OsAppArguments &AppArgs) { d->Args = AppArgs; } void LApp::OnCommandLine() { char WhiteSpace[] = " \r\n\t"; char *CmdLine = WideToUtf8(d->Args.lpCmdLine); if (ValidStr(CmdLine)) { // LgiTrace("CmdLine='%s'\n", CmdLine); LArray Files; char *Delim = "\'\""; char *s; for (s = CmdLine; *s; ) { // skip ws while (*s && strchr(WhiteSpace, *s)) s++; // read to end of token char *e = s; if (strchr(Delim, *s)) { char Delim = *s++; e = strchr(s, Delim); if (!e) e = s + strlen(s); } else { for (; *e && !strchr(WhiteSpace, *e); e++) ; } char *Arg = NewStr(s, e - s); if (Arg) { if (LFileExists(Arg)) { Files.Add(Arg); } else { DeleteArray(Arg); } } // next s = (*e) ? e + 1 : e; } // call app if (Files.Length() > 0) { OnReceiveFiles(Files); } // clear up Files.DeleteArrays(); } DeleteArray(CmdLine); } void LApp::OnUrl(const char *Url) { if (AppWnd) AppWnd->OnUrl(Url); } void LApp::OnReceiveFiles(LArray &Files) { if (AppWnd) AppWnd->OnReceiveFiles(Files); } int32 LApp::GetMetric(LSystemMetric Metric) { int32 Status = 0; switch (Metric) { case LGI_MET_DECOR_X: { Status = GetSystemMetrics(SM_CXFRAME) * 2; break; } case LGI_MET_DECOR_Y: { Status = GetSystemMetrics(SM_CYFRAME) * 2; Status += GetSystemMetrics(SM_CYCAPTION); break; } case LGI_MET_DECOR_CAPTION: { Status = GetSystemMetrics(SM_CYCAPTION); break; } case LGI_MET_MENU: { Status = GetSystemMetrics(SM_CYMENU); break; } case LGI_MET_THEME_AWARE: { Status = d->ThemeAware; break; } case LGI_MET_WINDOW_SHADOW: { Status = 7; // px; break; } } return Status; } LViewI *LApp::GetFocus() { HWND h = ::GetFocus(); if (h) { return LWindowFromHandle(h); } return 0; } HINSTANCE LApp::GetInstance() { if (this && IsOk()) { return d->Args.hInstance; } return (HINSTANCE)GetCurrentProcess(); } OsProcessId LApp::GetProcessId() { if (this) { return IsOk() ? d->Args.Pid : 0; } return GetCurrentProcessId(); } int LApp::GetShow() { return IsOk() ? d->Args.nCmdShow : 0; } bool LApp::Run(OnIdleProc IdleCallback, void *IdleParam) { MSG Msg = {0}; bool status = true; OnCommandLine(); if (IdleCallback) { bool DontWait = true; while (!d->QuitReceived) { while (1) { bool Status; if (DontWait) { Status = PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE) != 0; } else { Status = GetMessage(&Msg, NULL, 0, 0) > 0; DontWait = true; } #if 0 char m[256]; sprintf_s(m, sizeof(m), "Msg=%i hwnd=%p %i,%i\n", Msg.message, Msg.hwnd, Msg.wParam, Msg.lParam); OutputDebugStringA(m); #endif if (!Status || Msg.message == WM_QUIT) break; #ifdef _DEBUG int64 Last = LCurrentTime(); #endif TranslateMessage(&Msg); DispatchMessage(&Msg); #ifdef _DEBUG int64 Now = LCurrentTime(); if (Now - Last > 10000) { LgiTrace("%s:%i - Msg Loop Blocked: %i ms (Msg: 0x%.4x)\n", _FL, (int) (Now - Last), Msg.message); } #endif } if (Msg.message == WM_QUIT) { d->QuitReceived = true; } else { DontWait = IdleCallback(IdleParam); } } } else { while (!d->QuitReceived && GetMessage(&Msg, NULL, 0, 0) > 0) { #ifdef _DEBUG int64 Last = LCurrentTime(); #endif TranslateMessage(&Msg); DispatchMessage(&Msg); #ifdef _DEBUG int64 Now = LCurrentTime(); if (Now - Last > 1000) { LgiTrace("%s:%i - Msg Loop Blocked: %i ms (Msg: 0x%.4x)\n", _FL, (int) (Now - Last), Msg.message); } #endif } } return Msg.message != WM_QUIT; } bool LApp::Yield() { MSG Msg = {0}; while ( PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE) && Msg.message != WM_QUIT) { TranslateMessage(&Msg); DispatchMessage(&Msg); } if (Msg.message == WM_QUIT) { d->QuitReceived = true; } return Msg.message != WM_QUIT; } void LApp::Exit(int Code) { if (Code) { exit(0); } else { LAssert(InThread()); // Doesn't work for worker threads. PostQuitMessage(Code); } } LString LApp::GetFileMimeType(const char *File) { return LGetFileMimeType(File); } bool LApp::GetAppsForMimeType(const char *Mime, LArray &Apps) { return LGetAppsForMimeType(Mime, Apps); } LSymLookup *LApp::GetSymLookup() { if (!this) return 0; if (!d->SymLookup) d->SymLookup = new LSymLookup; return d->SymLookup; } bool LApp::IsWine() { if (d->LinuxWine < 0) { HMODULE hntdll = GetModuleHandle(L"ntdll.dll"); if (hntdll) { typedef const char * (CDECL *pwine_get_version)(void); pwine_get_version wine_get_version = (pwine_get_version)GetProcAddress(hntdll, "wine_get_version"); d->LinuxWine = wine_get_version != 0; } } return d->LinuxWine > 0; } bool LApp::IsElevated() { bool fRet = false; HANDLE hToken = NULL; if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken )) { TOKEN_ELEVATION Elevation; DWORD cbSize = sizeof(Elevation); if (GetTokenInformation(hToken, TokenElevation, &Elevation, sizeof(Elevation), &cbSize)) fRet = Elevation.TokenIsElevated != 0; } if (hToken) CloseHandle(hToken); return fRet; } LFontCache *LApp::GetFontCache() { if (!d->FontCache) d->FontCache.Reset(new LFontCache(SystemNormal)); return d->FontCache; } diff --git a/src/win/Lgi/Menu.cpp b/src/win/Lgi/Menu.cpp --- a/src/win/Lgi/Menu.cpp +++ b/src/win/Lgi/Menu.cpp @@ -1,1325 +1,1324 @@ /*hdr ** FILE: GuiMenu.cpp ** AUTHOR: Matthew Allen ** DATE: 18/7/98 ** DESCRIPTION: Gui menu system ** ** Copyright (C) 1998, Matthew Allen ** fret@memecode.com */ #include #include #include #include "lgi/common/Lgi.h" -#include "lgi/common/Token.h" #include "lgi/common/DisplayString.h" #include "lgi/common/Menu.h" #include "lgi/common/ToolBar.h" /////////////////////////////////////////////////////////////////////////////////////////////// #define LGI_OWNER_DRAW_MENUS LColour LGetSysColor(int nIndex) { LColour c; auto i = GetSysColor(nIndex); c.Rgb(GetRValue(i), GetGValue(i), GetBValue(i)); return c; } class LMenuPrivate { public: LColour RootMenuBack; LMenuPrivate() { LArray Ver; int Os = LGetOs(&Ver); if ( ( Os == LGI_OS_WIN32 || Os == LGI_OS_WIN64 ) && ( Ver[0] > 5 || (Ver[0] == 5 && Ver[1] > 0) ) ) { #ifndef SPI_GETFLATMENU #define SPI_GETFLATMENU 0x1022 #endif BOOL Flat = true; SystemParametersInfo(SPI_GETFLATMENU, 0, &Flat, 0); if (Flat) RootMenuBack = LGetSysColor(COLOR_MENUBAR); else RootMenuBack = LGetSysColor(COLOR_MENU); // LgiTrace("NT: flat=%i, menu=%x\n", Flat, RootMenuBack); } else { RootMenuBack = LGetSysColor(COLOR_MENU); // LgiTrace("9x: menu=%x\n", RootMenuBack); } } }; /////////////////////////////////////////////////////////////////////////////////////////////// LSubMenu::LSubMenu(const char *name, bool Popup) { if (Popup) { Info = CreatePopupMenu(); } else { Info = CreateMenu(); } Menu = 0; Parent = 0; TrackHandle = 0; Window = 0; } LSubMenu::~LSubMenu() { if (TrackHandle) { // At least in Win98, if you call InvalidateRect(...) on a window // multiple times just after you use TracePopupMenu(...) on that // window the invalid region isn't updated. // // This behaviour is not by design, it is a bug. However this // update forces the window to repaint thus working around the // bug in windows. UpdateWindow(TrackHandle); TrackHandle = 0; } if (Info) { DestroyMenu(Info); Info = 0; } if (Parent) { Parent->Child = 0; } LMenuItem *i; while (i = Items[0]) { LAssert(i->Parent == this); DeleteObj(i); } } void LSubMenu::SysMouseClick(LMouse &m) { } LMenuItem *LSubMenu::AppendItem(const char *Str, int Id, bool Enabled, int Where, const char *Shortcut) { int Pos = (int) (Where < 0 ? Items.Length() : min(Where, Items.Length())); LMenuItem *Item = new LMenuItem(Menu, this, Str, Id, Pos, Shortcut); if (Item) { Item->Enabled(Enabled); Items.Insert(Item, Where); Item->ScanForAccel(); } if (Menu) { Menu->OnChange(); } return Item; } LMenuItem *LSubMenu::AppendSeparator(int Where) { LMenuItem *Item = new LMenuItem; if (Item) { Item->Menu = Menu; Items.Insert(Item, Where); Item->Position = (int) Items.IndexOf(Item); Item->Separator(true); Item->Parent = this; Item->Insert(Item->Position); } if (Menu) { Menu->OnChange(); } return Item; } LSubMenu *LSubMenu::AppendSub(const char *Str, int Where) { int Pos = (int) (Where < 0 ? Items.Length() : min(Where, Items.Length())); LMenuItem *Item = new LMenuItem(Menu, this, Str, ItemId_Submenu, Pos); LSubMenu *Sub = new LSubMenu; if (Item && Sub) { Items.Insert(Item, Where); Item->Sub(Sub); } else { DeleteObj(Item); DeleteObj(Sub); } if (Menu) { Menu->OnChange(); } return Sub; } void LSubMenu::Empty() { while (RemoveItem(0)); } bool LSubMenu::RemoveItem(int i) { LMenuItem *Item = Items[i]; if (Item && Item->Remove()) { DeleteObj(Item); return true; } return false; } bool LSubMenu::RemoveItem(LMenuItem *Item) { if (Item && Items.HasItem(Item) && Item->Remove()) { return true; } return false; } int LSubMenu::Float(LView *From, int x, int y, int Button) { int Cmd = 0; if (From && Info) { LViewI *Wnd = From; while (Wnd && !Wnd->Handle()) { Wnd = Wnd->GetParent(); } if (Wnd && Wnd->Handle()) { Cmd = TrackPopupMenu( Info, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD | ((Button == BtnLeft) ? TPM_LEFTBUTTON : TPM_RIGHTBUTTON), x, y, 0, Wnd->Handle(), NULL); } else { LgiTrace("%s:%i - No handle to track popup menu.\n", __FILE__, __LINE__); } } return Cmd; } size_t LSubMenu::Length() { return Items.Length(); } LMenuItem *LSubMenu::ItemAt(int Id) { return Items.ItemAt(Id); } LSubMenu *LSubMenu::FindSubMenu(int Id) { for (auto i: Items) { LSubMenu *Sub = i->Sub(); if (i->Id() == Id) { return Sub; } else if (Sub) { LSubMenu *m = Sub->FindSubMenu(Id); if (m) { return m; } } } return 0; } LMenuItem *LSubMenu::FindItem(int Id) { for (auto i: Items) { LSubMenu *Sub = i->Sub(); if (i->Id() == Id) { return i; } else if (Sub) { i = Sub->FindItem(Id); if (i) { return i; } } } return 0; } /////////////////////////////////////////////////////////////////////////////////////////////// class LMenuItemPrivate { public: bool StartUnderline; // Underline the first display string bool HasAccel; // The last display string should be right aligned List Strs; // Draw each alternate display string with underline // except the last in the case of HasAccel==true. LString Shortcut; LMenuItemPrivate() { HasAccel = false; StartUnderline = false; } ~LMenuItemPrivate() { Strs.DeleteObjects(); } void UpdateStrings(LFont *Font, char *n) { // Build up our display strings, Strs.DeleteObjects(); StartUnderline = false; char *Tab = strrchr(n, '\t'); if (Tab) { *Tab = 0; } char *Amp = 0, *a = n; while (a = strchr(a, '&')) { if (a[1] != '&') { Amp = a; break; } a++; } if (Amp) { // Before amp Strs.Insert(new LDisplayString(Font, n, Amp - n )); // Amp'd letter char *e = LSeekUtf8(++Amp, 1); Strs.Insert(new LDisplayString(Font, Amp, e - Amp )); // After Amp if (*e) { Strs.Insert(new LDisplayString(Font, e)); } } else { Strs.Insert(new LDisplayString(Font, n)); } if (HasAccel = (Tab != 0)) { Strs.Insert(new LDisplayString(Font, Tab + 1)); *Tab = '\t'; } else if (HasAccel = (Shortcut.Get() != 0)) { Strs.Insert(new LDisplayString(Font, Shortcut)); } } }; LMenuItem::LMenuItem() { d = new LMenuItemPrivate; Menu = NULL; Parent = NULL; Child = 0; Position = -1; _Icon = -1; ZeroObj(Info); Info.cbSize = sizeof(Info); #ifdef LGI_OWNER_DRAW_MENUS Info.fMask = MIIM_DATA; Info.fType = MFT_OWNERDRAW; #if _MSC_VER >= _MSC_VER_VS2005 Info.dwItemData = (ULONG_PTR)this; #else Info.dwItemData = (DWORD)this; #endif #endif Enabled(true); } LMenuItem::LMenuItem(LMenu *m, LSubMenu *p, const char *Txt, int id, int Pos, const char *Shortcut) { d = new LMenuItemPrivate; d->Shortcut = Shortcut; Position = Pos; Parent = NULL; Menu = m; Parent = NULL; Child = 0; _Icon = -1; ZeroObj(Info); Info.cbSize = sizeof(Info); #ifdef LGI_OWNER_DRAW_MENUS Info.fMask = MIIM_DATA; Info.fType = MFT_OWNERDRAW; #if _MSC_VER >= _MSC_VER_VS2005 Info.dwItemData = (ULONG_PTR)this; #else Info.dwItemData = (DWORD)this; #endif #endif Id(id); Enabled(true); Name(Txt); Parent = p; Insert(Position); } LMenuItem::~LMenuItem() { if (Parent) { if (Parent->Handle()) { int Index = (int)Parent->Items.IndexOf(this); RemoveMenu(Parent->Handle(), Index, MF_BYPOSITION); } Parent->Items.Delete(this); } DeleteObj(Child); DeleteObj(d); } // the following 3 functions paint the menus according the to // windows standard. but also allow for correct drawing of menuitem // icons. some implementations of windows force the program back // to the 8-bit palette when specifying the icon graphic, thus removing // control over the colours displayed. these functions remove that // limitation and also provide the application the ability to override // the default painting behaviour if desired. void LMenuItem::_Measure(LPoint &Size) { if (Separator()) { Size.x = 8; Size.y = 8; } else { LFont *Font = Menu && Menu->GetFont() ? Menu->GetFont() : LSysFont; bool BaseMenu = Parent == Menu; // true if attached to a windows menu // else is a submenu int Ht = Font->GetHeight(); Size.x = BaseMenu ? 0 : 20; for (auto s: d->Strs) { Size.x += s->X(); } if (d->Shortcut || strchr(Name(), '\t')) { Size.x += 32; } if (!BaseMenu) { // leave room for child pointer Size.x += Child ? 8 : 0; } Size.y = BaseMenu ? GetSystemMetrics(SM_CYMENU)+1 : Ht+2; if (Size.y < 16) Size.y = 16; } } void LMenuItem::_PaintText(LSurface *pDC, int x, int y, int Width) { bool Underline = d->StartUnderline; LFont *Font = Menu && Menu->GetFont() ? Menu->GetFont() : LSysFont; for (int i=0; i < (d->Strs.Length() - (d->HasAccel ? 1 : 0)); i++) { LDisplayString *s = d->Strs[i]; if (!s) break; s->Draw(pDC, x, y); if (Underline) { int UnderX = s->X(); pDC->Colour(Font->Fore()); pDC->Line(x, y+s->Y()-1, x+max(s->X()-2, 1), y+s->Y()-1); } Underline = !Underline; x += s->X(); } if (d->HasAccel) { LDisplayString *s = *d->Strs.rbegin(); if (s) { s->Draw(pDC, Width - s->X() - 8, y); } } } void LMenuItem::_Paint(LSurface *pDC, int Flags) { bool BaseMenu = Parent == Menu; int IconX = BaseMenu ? 5 : 20; bool Selected = TestFlag(Flags, ODS_SELECTED); bool Disabled = TestFlag(Flags, ODS_DISABLED); bool Checked = TestFlag(Flags, ODS_CHECKED); LRect r(0, 0, pDC->X()-1, pDC->Y()-1); auto Text = Name(); if (Separator()) { // paint a separator int Cy = pDC->Y() / 2; pDC->Colour(L_MENU_BACKGROUND); pDC->Rectangle(); pDC->Colour(L_LOW); pDC->Line(0, Cy-1, pDC->X()-1, Cy-1); pDC->Colour(L_LIGHT); pDC->Line(0, Cy, pDC->X()-1, Cy); } else { // paint a text menu item LColour Fore(Selected ? L_FOCUS_SEL_FORE : L_MENU_TEXT); LColour Back(BaseMenu ? Menu->d->RootMenuBack : (Selected ? LColour(L_FOCUS_SEL_BACK) : LColour(L_MENU_BACKGROUND))); int x = IconX; LFont *Font = Menu && Menu->GetFont() ? Menu->GetFont() : LSysFont; int y = (pDC->Y() - Font->GetHeight()) >> 1; // paint the background if (BaseMenu) { // for a menu, sunken on selected LRect rgn = r; if (Selected) { LThinBorder(pDC, rgn, DefaultSunkenEdge); Fore = LColour(L_MENU_TEXT); x++; y++; } // always dialog colour pDC->Colour(Back); pDC->Rectangle(&rgn); } else { // for a sub menu pDC->Colour(Back); pDC->Rectangle(); } // draw the text on top Font->Transparent(true); if (Disabled) { // disabled text if (!Selected) { Font->Colour(L_LIGHT); _PaintText(pDC, x+1, y, r.X()); } // else selected... don't draw the highlight // "grayed" text... Font->Colour(L_LOW); _PaintText(pDC, x, y-1, r.X()-1); } else { // normal coloured text Font->Colour(Fore, Back); _PaintText(pDC, x, y-1, r.X()); } LImageList *ImgLst = (Menu && Menu->GetImageList()) ? Menu->GetImageList() : Parent ? Parent->GetImageList() : 0; // draw icon/check mark if (Checked && IconX > 0) { // it's a check! int x = 4; int y = 6; pDC->Colour(Fore); pDC->Line(x, y, x+2, y+2); pDC->Line(x+2, y+2, x+6, y-2); y++; pDC->Line(x, y, x+2, y+2); pDC->Line(x+2, y+2, x+6, y-2); y++; pDC->Line(x, y, x+2, y+2); pDC->Line(x+2, y+2, x+6, y-2); } else if (ImgLst && _Icon >= 0) { // it's an icon! ImgLst->Draw(pDC, 0, 0, _Icon, Back); } } } bool LMenuItem::ScanForAccel() { LString Accel; if (d->Shortcut) { Accel = d->Shortcut; } else { auto n = LBase::Name(); if (n) { auto Tab = strchr(n, '\t'); if (Tab) Accel = Tab + 1; } } if (Accel) { auto Keys = Accel.SplitDelimit("-+"); if (Keys.Length() > 0) { int Flags = 0; int Vkey = 0; int Chr = 0; bool AccelDirty = false; for (int i=0; iShortcut = LString("+").Join(Keys); LString n = Name(); LFont *Font = Menu && Menu->GetFont() ? Menu->GetFont() : LSysFont; d->UpdateStrings(Font, n); } if ((Vkey || Chr) && Menu) { Menu->Accel.Insert( new LAccelerator(Flags, Vkey, Chr, Id()) ); } } } return false; } LSubMenu *LMenuItem::GetParent() { return Parent; } bool LMenuItem::Remove() { bool Status = false; if (Parent) { int Index = (int) Parent->Items.IndexOf(this); Parent->Items.Delete(this); Status = RemoveMenu(Parent->Handle(), Index, MF_BYPOSITION) != 0; int n=0; for (auto i: Parent->Items) { i->Position = n++; } } return Status; } bool LMenuItem::Update() { bool Status = FALSE; if (Parent && Parent->Handle()) { Status = SetMenuItemInfo(Parent->Handle(), Position, true, &Info) != 0; LAssert(Status); } return Status; } void LMenuItem::Icon(int i) { _Icon = i; } int LMenuItem::Icon() { return _Icon; } void LMenuItem::Id(int i) { Info.wID = i; Info.fMask |= MIIM_ID; Update(); } void LMenuItem::Separator(bool s) { if (s) { Info.fType |= MFT_SEPARATOR; } else { Info.fType &= ~MFT_SEPARATOR; } Info.fMask |= MIIM_TYPE; Update(); } void LMenuItem::Checked(bool c) { if (c) { Info.fState |= MFS_CHECKED; } else { Info.fState &= ~MFS_CHECKED; } Info.fMask |= MIIM_STATE; Update(); } bool LMenuItem::Name(const char *Txt) { bool Status = LBase::Name(Txt); if (Status) { LString n = NewStr(Txt); if (n) { // Set OS menu structure Info.dwTypeData = (LPWSTR)LBase::NameW(); Info.cch = (UINT) StrlenW(LBase::NameW()); Info.fType |= MFT_STRING; Info.fMask |= MIIM_TYPE | MIIM_DATA; LFont *Font = Menu && Menu->GetFont() ? Menu->GetFont() : LSysFont; d->UpdateStrings(Font, n); // Tell the OS Update(); } } return Status; } void LMenuItem::Enabled(bool e) { Info.fState &= ~(MFS_ENABLED|MFS_DISABLED|MFS_GRAYED); if (!e) { Info.fState |= MFS_DISABLED|MFS_GRAYED; } Info.fMask |= MIIM_STATE; Update(); // LgiTrace("%s:%i - LMenuItem::Enabled(%i) %s\n", _FL, e, Name()); } void LMenuItem::Focus(bool f) { if (f) { Info.fState |= MFS_HILITE; } else { Info.fState &= ~MFS_HILITE; } Info.fMask |= MIIM_STATE; Update(); } void LMenuItem::Sub(LSubMenu *s) { Child = s; if (Child) { Info.hSubMenu = Child->Handle(); s->Menu = Menu; s->Parent = this; } else { Info.hSubMenu = 0; } Info.fMask |= MIIM_SUBMENU; Update(); } bool LMenuItem::Insert(int Pos) { bool Status = false; if (Parent && Parent->Handle()) { LAssert(Position >= 0); Position = Pos; Status = InsertMenuItem(Parent->Handle(), Position, true, &Info) != 0; LAssert(Status); } return Status; } void LMenuItem::Visible(bool i) { } int LMenuItem::Id() { return Info.wID; } const char *LMenuItem::Name() { return LBase::Name(); } bool LMenuItem::Separator() { return (Info.fType & MFT_SEPARATOR) != 0; } bool LMenuItem::Checked() { return (Info.fState & MF_CHECKED) != 0; } bool LMenuItem::Enabled() { return (Info.fState & MFS_DISABLED) == 0; } bool LMenuItem::Visible() { return true; } bool LMenuItem::Focus() { return (Info.fState & MFS_HILITE) != 0; } LSubMenu *LMenuItem::Sub() { return Child; } /////////////////////////////////////////////////////////////////////////////////////////////// LMenu::LMenu(const char *AppName) : LSubMenu("", false) { d = new LMenuPrivate; Menu = this; Window = NULL; } LMenu::~LMenu() { Accel.DeleteObjects(); DeleteObj(d); } void LMenu::OnChange() { if (Info && Window) { if (::GetMenu(Window->Handle()) != Info) { SetMenu(Window->Handle(), Info); } } } struct LMenuFont { LFont *f; LMenuFont() { f = NULL; } ~LMenuFont() { DeleteObj(f); } } MenuFont; LFont *LMenu::GetFont() { if (!MenuFont.f) { LFontType Type; if (Type.GetSystemFont("Menu")) { MenuFont.f = Type.Create(); } } return MenuFont.f ? MenuFont.f : LSysFont; } bool LMenu::Attach(LViewI *p) { Window = p; return true; } bool LMenu::Detach() { bool Status = FALSE; if (Window) { HMENU hWndMenu = ::GetMenu(Window->Handle()); if (hWndMenu == Info) { Status = SetMenu(Window->Handle(), NULL) != 0; if (Status) { Window = NULL; } } } return Status; } bool LMenu::OnKey(LView *v, LKey &k) { if (k.Down() && k.vkey != 17) { for (auto a: Accel) { if (a->Match(k)) { LMenuItem *i = FindItem(a->GetId()); if (!i || i->Enabled()) { int Cmd = a->GetId(); Window->OnCommand(Cmd, 0, 0); return true; } } } } return false; } int LMenu::_OnEvent(LMessage *Msg) { switch (Msg->Msg()) { case WM_MENUCHAR: { short Key = Msg->A() & 0xffff; HMENU View = (HMENU)Msg->B(); MENUITEMINFO Info; ZeroObj(Info); Info.cbSize = sizeof(Info); Info.fMask = MIIM_DATA; if (GetMenuItemInfo(View, 0, true, &Info)) { LMenuItem *Item = (LMenuItem*)Info.dwItemData; if (Item) { LSubMenu *Menu = Item->Parent; if (Menu) { int Index=0; for (auto i: Menu->Items) { auto n = i->Name(); if (n) { char *Amp = strchr(n, '&'); if (Amp && toupper(Amp[1]) == toupper(Key)) { return (MNC_EXECUTE << 16) | Index; } } Index++; } } } } break; } case WM_MEASUREITEM: { LPMEASUREITEMSTRUCT Item = (LPMEASUREITEMSTRUCT)Msg->b; if (Item) { LPoint Size; ((LMenuItem*)Item->itemData)->_Measure(Size); Item->itemWidth = Size.x; Item->itemHeight = Size.y; return true; } break; } case WM_DRAWITEM: { LPDRAWITEMSTRUCT Item = (LPDRAWITEMSTRUCT)Msg->b; if (Item && Item->CtlType == ODT_MENU) { LRect r = Item->rcItem; LScreenDC Dc(Item->hDC, Item->hwndItem); // Get the original origin. We need to do this because when // "animated menus" are on the offset starts at -3,-3 and throws // the menu items off. This is a bug in windows, but watcha // gonna do? int x, y; Dc.GetOrigin(x, y); // Clip and offset so that the menu item draws in client co-ords. Dc.SetOrigin(-r.x1+x, -r.y1+y); Dc.SetSize(r.X(), r.Y()); // Paint the item... ((LMenuItem*)Item->itemData)->_Paint(&Dc, Item->itemState); // Set the origin back the original value. Dc.SetOrigin(x, y); return true; } break; } } return 0; } bool LMenu::SetPrefAndAboutItems(int PrefId, int AboutId) { return false; } //////////////////////////////////////////////////////////////////////////// LAccelerator::LAccelerator(int flags, int vkey, int chr, int id) { Flags = flags; Vkey = vkey; Chr = chr; Id = id; } bool LAccelerator::Match(LKey &k) { if ( !k.IsChar && ( (Chr != 0 && tolower(k.c16) == tolower(Chr)) || (Vkey != 0 && k.vkey == Vkey) ) ) { if ( (Ctrl() ^ k.Ctrl()) == 0 && (Alt() ^ k.Alt()) == 0 && (Shift() ^ k.Shift()) == 0 && (!TestFlag(Flags, LGI_EF_IS_CHAR) || k.IsChar) && (!TestFlag(Flags, LGI_EF_IS_NOT_CHAR) || !k.IsChar) ) { return true; } } return false; } //////////////////////////////////////////////////////////////////////////// LCommand::LCommand() { } LCommand::~LCommand() { } bool LCommand::Enabled() { if (ToolButton) return ToolButton->Enabled(); if (MenuItem) return MenuItem->Enabled(); return false; } void LCommand::Enabled(bool e) { if (ToolButton) { ToolButton->Enabled(e); } if (MenuItem) { MenuItem->Enabled(e); } } bool LCommand::Value() { bool HasChanged = false; if (ToolButton) { HasChanged |= (ToolButton->Value() != 0) ^ PrevValue; } if (MenuItem) { HasChanged |= (MenuItem->Checked() != 0) ^ PrevValue; } if (HasChanged) { Value(!PrevValue); } return PrevValue; } void LCommand::Value(bool v) { if (ToolButton) { ToolButton->Value(v); } if (MenuItem) { MenuItem->Checked(v); } PrevValue = v; } diff --git a/src/win/Lgi/Window.cpp b/src/win/Lgi/Window.cpp --- a/src/win/Lgi/Window.cpp +++ b/src/win/Lgi/Window.cpp @@ -1,1423 +1,1418 @@ #include #include #include "lgi/common/Lgi.h" #include "lgi/common/Edit.h" #include "lgi/common/Popup.h" #include "lgi/common/ToolBar.h" #include "lgi/common/Panel.h" #include "lgi/common/Variant.h" -#include "lgi/common/Token.h" #include "lgi/common/Button.h" #include "lgi/common/Notifications.h" #include "lgi/common/CssTools.h" #include "lgi/common/Menu.h" #include "ViewPriv.h" #define DEBUG_WINDOW_PLACEMENT 0 #define DEBUG_HANDLE_VIEW_KEY 0 #define DEBUG_HANDLE_VIEW_MOUSE 0 #define DEBUG_SERIALIZE_STATE 0 #define DEBUG_SETFOCUS 0 extern bool In_SetWindowPos; typedef UINT (WINAPI *ProcGetDpiForWindow)(_In_ HWND hwnd); typedef UINT (WINAPI *ProcGetDpiForSystem)(VOID); LLibrary User32("User32"); LPoint LGetDpiForWindow(HWND hwnd) { static bool init = false; static ProcGetDpiForWindow pGetDpiForWindow = NULL; static ProcGetDpiForSystem pGetDpiForSystem = NULL; if (!init) { init = true; pGetDpiForWindow = (ProcGetDpiForWindow) User32.GetAddress("GetDpiForWindow"); pGetDpiForSystem = (ProcGetDpiForSystem) User32.GetAddress("GetDpiForSystem"); } if (pGetDpiForWindow && pGetDpiForSystem) { auto Dpi = hwnd ? pGetDpiForWindow(hwnd) : pGetDpiForSystem(); return LPoint(Dpi, Dpi); } return LScreenDpi(); } /////////////////////////////////////////////////////////////////////////////////////////////// class HookInfo { public: int Flags; LView *Target; }; class LWindowPrivate { public: LArray Hooks; bool SnapToEdge; bool AlwaysOnTop; LWindowZoom Show; bool InCreate; LAutoPtr Wp; LPoint Dpi; int ShowCmd = SW_NORMAL; // Focus stuff LViewI *Focus; LWindowPrivate() { Focus = NULL; InCreate = true; Show = LZoomNormal; SnapToEdge = false; AlwaysOnTop = false; } ~LWindowPrivate() { } ssize_t GetHookIndex(LView *Target, bool Create = false) { for (int i=0; iTarget = Target; n->Flags = 0; return (ssize_t)Hooks.Length() - 1; } } return -1; } }; /////////////////////////////////////////////////////////////////////////////////////////////// LWindow::LWindow() : LView(0) { _Window = this; d = new LWindowPrivate; SetStyle(GetStyle() | WS_TILEDWINDOW | WS_CLIPCHILDREN); SetStyle(GetStyle() & ~WS_CHILD); SetExStyle(GetExStyle() | WS_EX_CONTROLPARENT); LWindowsClass *c = LWindowsClass::Create(GetClass()); if (c) c->Register(); Visible(false); _Default = 0; _Lock = new LMutex("LWindow"); _QuitOnClose = false; } LWindow::~LWindow() { if (LAppInst && LAppInst->AppWnd == this) LAppInst->AppWnd = NULL; if (Menu) { Menu->Detach(); DeleteObj(Menu); } DeleteObj(_Lock); DeleteObj(d); } int LWindow::WaitThread() { // No thread to wait on... return 0; } bool LWindow::SetTitleBar(bool ShowTitleBar) { if (ShowTitleBar) { SetStyle(GetStyle() | WS_TILEDWINDOW); } else { SetStyle(GetStyle() & ~WS_TILEDWINDOW); SetStyle(GetStyle() | WS_POPUP); } return true; } bool LWindow::SetIcon(const char *Icon) { return CreateClassW32(LAppInst->Name(), LoadIcon(LProcessInst(), (LPCWSTR)Icon)) != 0; } LViewI *LWindow::GetFocus() { return d->Focus; } static LAutoString DescribeView(LViewI *v) { if (!v) return LAutoString(NewStr("NULL")); char s[512]; int ch = 0; ::LArray p; for (LViewI *i = v; i; i = i->GetParent()) { p.Add(i); } for (auto n=MIN(3, (ssize_t)p.Length()-1); n>=0; n--) { v = p[n]; ch += sprintf_s(s + ch, sizeof(s) - ch, ">%s", v->GetClass()); } return LAutoString(NewStr(s)); } static bool HasParentPopup(LViewI *v) { for (; v; v = v->GetParent()) { if (dynamic_cast(v)) return true; } return false; } bool LWindow::SetWillFocus(bool f) { d->ShowCmd = f ? SW_NORMAL : SW_SHOWNOACTIVATE; return true; } void LWindow::SetFocus(LViewI *ctrl, FocusType type) { const char *TypeName = NULL; switch (type) { case GainFocus: TypeName = "Gain"; break; case LoseFocus: TypeName = "Lose"; break; case ViewDelete: TypeName = "Delete"; break; } switch (type) { case GainFocus: { LViewI *This = this; if (ctrl == This && d->Focus) { // The main LWindow is getting focus. // Check if we can re-focus the previous child focus... LView *v = d->Focus->GetGView(); if (v && !HasParentPopup(v)) { // We should never return focus to a popup, or it's child. if (!(v->WndFlags & GWF_FOCUS)) { // Yes, the child view doesn't think it has focus... // So re-focus it... if (v->Handle()) { // Non-virtual window... ::SetFocus(v->Handle()); } v->WndFlags |= GWF_FOCUS; v->OnFocus(true); v->Invalidate(); #if DEBUG_SETFOCUS LAutoString _set = DescribeView(ctrl); LAutoString _foc = DescribeView(d->Focus); LgiTrace("LWindow::SetFocus(%s, %s) refocusing: %s\n", _set.Get(), TypeName, _foc.Get()); #endif return; } } } // Check if the control already has focus if (d->Focus == ctrl) return; if (d->Focus) { LView *v = d->Focus->GetGView(); if (v) v->WndFlags &= ~GWF_FOCUS; d->Focus->OnFocus(false); d->Focus->Invalidate(); #if DEBUG_SETFOCUS LAutoString _foc = DescribeView(d->Focus); LgiTrace(".....defocus: %s\n", _foc.Get()); #endif } d->Focus = ctrl; if (d->Focus) { LView *v = d->Focus->GetGView(); if (v) v->WndFlags |= GWF_FOCUS; d->Focus->OnFocus(true); d->Focus->Invalidate(); #if DEBUG_SETFOCUS LAutoString _set = DescribeView(d->Focus); LgiTrace("LWindow::SetFocus(%s, %s) focusing\n", _set.Get(), TypeName); #endif } break; } case LoseFocus: { if (ctrl == d->Focus) { LView *v = d->Focus->GetGView(); if (v) { if (v->WndFlags & GWF_FOCUS) { // View thinks it has focus v->WndFlags &= ~GWF_FOCUS; d->Focus->OnFocus(false); // keep d->Focus pointer, as we want to be able to re-focus the child // view when we get focus again #if DEBUG_SETFOCUS LAutoString _ctrl = DescribeView(ctrl); LAutoString _foc = DescribeView(d->Focus); LgiTrace("LWindow::SetFocus(%s, %s) keep_focus: %s\n", _ctrl.Get(), TypeName, _foc.Get()); #endif } // else view doesn't think it has focus anyway... } else { // Non LView handler d->Focus->OnFocus(false); d->Focus->Invalidate(); d->Focus = NULL; } } else { /* LgiTrace("LWindow::SetFocus(%p.%s, %s) error on losefocus: %p(%s)\n", ctrl, ctrl ? ctrl->GetClass() : NULL, TypeName, d->Focus, d->Focus ? d->Focus->GetClass() : NULL); */ } break; } case ViewDelete: { if (ctrl == d->Focus) { #if DEBUG_SETFOCUS LgiTrace("LWindow::SetFocus(%p.%s, %s) delete_focus: %p(%s)\n", ctrl, ctrl ? ctrl->GetClass() : NULL, TypeName, d->Focus, d->Focus ? d->Focus->GetClass() : NULL); #endif d->Focus = NULL; } break; } } } bool LWindow::GetSnapToEdge() { return d->SnapToEdge; } void LWindow::SetSnapToEdge(bool b) { d->SnapToEdge = b; } bool LWindow::GetAlwaysOnTop() { return d->AlwaysOnTop; } void LWindow::SetAlwaysOnTop(bool b) { d->AlwaysOnTop = b; if (_View) SetWindowPos(_View, b ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE); } void LWindow::Raise() { if (_View) { DWORD dwFGProcessId; DWORD dwFGThreadId = GetWindowThreadProcessId(_View, &dwFGProcessId); DWORD dwThisThreadId = GetCurrentThreadId(); AttachThreadInput(dwThisThreadId, dwFGThreadId, true); SetForegroundWindow(_View); BringWindowToTop(_View); if (In_SetWindowPos) { assert(0); LgiTrace("%s:%i - SetFocus(%p)\n", __FILE__, __LINE__, _View); } ::SetFocus(_View); AttachThreadInput(dwThisThreadId, dwFGThreadId, false); } } LWindowZoom LWindow::GetZoom() { if (IsZoomed(Handle())) { return LZoomMax; } else if (IsIconic(Handle())) { return LZoomMin; } return LZoomNormal; } void LWindow::SetZoom(LWindowZoom i) { if (_View && IsWindowVisible(_View)) { switch (i) { case LZoomMax: { ShowWindow(Handle(), SW_MAXIMIZE); break; } case LZoomMin: { ShowWindow(Handle(), SW_MINIMIZE); break; } case LZoomNormal: { if (!Visible()) { Visible(true); } if (IsIconic(Handle()) || IsZoomed(Handle())) { ShowWindow(Handle(), d->ShowCmd); } LYield(); RECT r; GetWindowRect(Handle(), &r); if (r.left != Pos.x1 || r.top != Pos.y1) { int Shadow = WINDOWS_SHADOW_AMOUNT; SetWindowPos(Handle(), 0, Pos.x1, Pos.y1, Pos.X() + Shadow, Pos.Y() + Shadow, SWP_NOZORDER); } break; } } } d->Show = i; } bool LWindow::OnRequestClose(bool OsShuttingDown) { if (GetQuitOnClose()) { LCloseApp(); } return true; } bool LWindow::HandleViewMouse(LView *v, LMouse &m) { #if DEBUG_HANDLE_VIEW_MOUSE m.Trace("HandleViewMouse"); #endif for (int i=0; iHooks.Length(); i++) { if (d->Hooks[i].Flags & LMouseEvents) { LView *t = d->Hooks[i].Target; if (!t->OnViewMouse(v, m)) { #if DEBUG_HANDLE_VIEW_MOUSE if (m.IsMove()) LgiTrace(" Hook %i of %i ate mouse event: '%s'\n", i, d->Hooks.Length(), d->Hooks[i].Target->GetClass()); #endif return false; } } } #if DEBUG_HANDLE_VIEW_MOUSE if (!m.IsMove()) LgiTrace(" Passing mouse event to '%s'\n", v->GetClass()); #endif return true; } bool LWindow::HandleViewKey(LView *v, LKey &k) { #if DEBUG_HANDLE_VIEW_KEY char msg[256]; sprintf_s(msg, sizeof(msg), "HandleViewKey, v=%s", v ? v->GetClass() : "NULL"); k.Trace(msg); #endif // Any window in a pop up always gets the key... LViewI *p; for (p = v->GetParent(); p; p = p->GetParent()) { if (dynamic_cast(p)) { #if DEBUG_HANDLE_VIEW_KEY LgiTrace(" Popup %s handling key.\n", p->GetClass()); #endif return v->OnKey(k); } } // Allow any hooks to see the key... #if DEBUG_HANDLE_VIEW_KEY LgiTrace(" d->Hooks.Length()=%i.\n", (int)d->Hooks.Length()); #endif for (int i=0; iHooks.Length(); i++) { if (d->Hooks[i].Flags & LKeyEvents) { if (d->Hooks[i].Target->OnViewKey(v, k)) { #if DEBUG_HANDLE_VIEW_KEY LgiTrace(" Hook[%i] %s handling key.\n", i, d->Hooks[i].Target->GetClass()); #endif return true; } } } // Give the key to the focused window... if (d->Focus && d->Focus->OnKey(k)) { #if DEBUG_HANDLE_VIEW_KEY LgiTrace(" d->Focus %s handling key.\n", d->Focus->GetClass()); #endif return true; } // Check default controls p = 0; if (k.c16 == VK_RETURN) { if (!_Default) p = _Default = FindControl(IDOK); else p = _Default; #if DEBUG_HANDLE_VIEW_KEY LgiTrace(" Using _Default ctrl (%s).\n", p ? p->GetClass() : "NULL"); #endif } else if (k.c16 == VK_ESCAPE) { p = FindControl(IDCANCEL); if (p) { #if DEBUG_HANDLE_VIEW_KEY LgiTrace(" Using IDCANCEL ctrl (%s).\n", p->GetClass()); #endif } } if (p && p->OnKey(k)) { #if DEBUG_HANDLE_VIEW_KEY LgiTrace(" Default control %s handled key.\n", p->GetClass()); #endif return true; } // Menu shortcut? if (Menu && Menu->OnKey(v, k)) { #if DEBUG_HANDLE_VIEW_KEY LgiTrace(" Menu handled key.\n"); #endif return true; } // Control shortcut? if (k.Down() && k.Alt() && k.c16 > ' ') { ShortcutMap Map; BuildShortcuts(Map); LViewI *c = Map.Find(ToUpper(k.c16)); if (c) { c->OnNotify(c, LNotifyActivate); return true; } } #if DEBUG_HANDLE_VIEW_KEY LgiTrace(" No one handled key.\n"); #endif return false; } void LWindow::OnPaint(LSurface *pDC) { auto c = GetClient(); LCssTools Tools(this); Tools.PaintContent(pDC, c); } bool LWindow::Obscured() { RECT tRect; bool isObscured = false; if (GetWindowRect(_View, &tRect)) { RECT nRect; HWND walker = _View; while (walker = ::GetNextWindow(walker, GW_HWNDPREV)) { if (IsWindowVisible(walker)) { if ((::GetWindowRect(walker, &nRect))) { RECT iRect; IntersectRect(&iRect, &tRect, &nRect); if (iRect.bottom || iRect.top || iRect.left || iRect.right) { isObscured = true; break; } } } } } return isObscured; } bool LWindow::Visible() { return LView::Visible(); } void LWindow::Visible(bool v) { if (v) PourAll(); if (v) { SetStyle(GetStyle() | WS_VISIBLE); if (_View) { LWindowZoom z = d->Show; char *Cmd = 0; LAutoPtr Wp(new WINDOWPLACEMENT); if (Wp) { ZeroObj(*Wp.Get()); Wp->length = sizeof(*Wp); Wp->flags = 2; Wp->ptMaxPosition.x = -1; Wp->ptMaxPosition.y = -1; if (d->Show == LZoomMax) { Wp->showCmd = SW_MAXIMIZE; Cmd = "SW_MAXIMIZE"; } else if (d->Show == LZoomMin) { Wp->showCmd = SW_MINIMIZE; Cmd = "SW_MINIMIZE"; } else { Wp->showCmd = d->ShowCmd; Cmd = "SW_NORMAL"; } Wp->rcNormalPosition = Pos; #if DEBUG_WINDOW_PLACEMENT LgiTrace("%s:%i - SetWindowPlacement, pos=%s, show=%i\n", __FILE__, __LINE__, Pos.GetStr(), Wp->showCmd); #endif SetWindowPlacement(_View, Wp); if (d->InCreate) d->Wp = Wp; } } } else { #if DEBUG_WINDOW_PLACEMENT LgiTrace("%s:%i - Visible(%i)\n", __FILE__, __LINE__, v); #endif LView::Visible(v); } if (v) { OnZoom(d->Show); } } static bool IsAppWnd(HWND h) { if (!IsWindowVisible(h)) return false; auto flags = GetWindowLong(h, GWL_STYLE); if (flags & WS_POPUP) return false; return true; } bool LWindow::IsActive() { auto top = GetTopWindow(GetDesktopWindow()); while (top && !IsAppWnd(top)) top = ::GetWindow(top, GW_HWNDNEXT); return top == _View; } bool LWindow::SetActive() { if (!_View) return false; return SetWindowPos(_View, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE) != 0; } void LWindow::PourAll() { LRegion Client(GetClient()); LRegion Update; bool HasTools = false; { LRegion Tools; for (auto v: Children) { LView *k = dynamic_cast(v); if (k && k->_IsToolBar) { LRect OldPos = v->GetPos(); Update.Union(&OldPos); if (HasTools) { // 2nd and later toolbars if (v->Pour(Tools)) { if (!v->Visible()) { v->Visible(true); } auto vpos = v->GetPos(); if (OldPos != vpos) { // position has changed update... v->Invalidate(); } // Has it increased the size of the toolbar area? auto b = Tools.Bound(); if (vpos.y2 >= b.y2) { LRect Bar = Client; Bar.y2 = vpos.y2; Client.Subtract(&Bar); // LgiTrace("IncreaseToolbar=%s\n", Bar.GetStr()); } Tools.Subtract(&vpos); Update.Subtract(&vpos); // LgiTrace("vpos=%s\n", vpos.GetStr()); } } else { // First toolbar if (v->Pour(Client)) { HasTools = true; if (!v->Visible()) { v->Visible(true); } if (OldPos != v->GetPos()) { v->Invalidate(); } LRect Bar(v->GetPos()); Bar.x2 = GetClient().x2; Tools = Bar; Tools.Subtract(&v->GetPos()); Client.Subtract(&Bar); Update.Subtract(&Bar); } } } } } // LgiTrace("Client=%s\n", Client.Bound().GetStr()); for (auto v: Children) { LView *k = dynamic_cast(v); if (!(k && k->_IsToolBar)) { LRect OldPos = v->GetPos(); Update.Union(&OldPos); if (v->Pour(Client)) { if (!v->Visible()) { v->Visible(true); } if (OldPos != v->GetPos()) { // position has changed update... v->Invalidate(); } Client.Subtract(&v->GetPos()); Update.Subtract(&v->GetPos()); } else { // make the view not visible // v->Visible(FALSE); } } } for (int i=0; iMsg()) { case WM_DPICHANGED: { d->Dpi.x = HIWORD(Msg->A()); d->Dpi.y = LOWORD(Msg->A()); OnPosChange(); break; } case M_ASSERT_UI: { LAutoPtr Str((LString*)Msg->A()); extern void LAssertDlg(LString Msg, std::function Callback); if (Str) LAssertDlg(Str ? *Str : "Error: no msg.", NULL); break; } case M_SET_WINDOW_PLACEMENT: { /* Apparently if you use SetWindowPlacement inside the WM_CREATE handler, then the restored rect doesn't "stick", it gets stomped on by windows. So this code... RESETS it to be what we set earlier. Windows sucks. */ if (!d->Wp || !_View) break; LRect r = d->Wp->rcNormalPosition; if (!LView::Visible()) d->Wp->showCmd = SW_HIDE; #if DEBUG_WINDOW_PLACEMENT LgiTrace("%s:%i - SetWindowPlacement, pos=%s, show=%i\n", __FILE__, __LINE__, r.GetStr(), d->Wp->showCmd); #endif SetWindowPlacement(_View, d->Wp); d->Wp.Reset(); break; } case WM_SYSCOLORCHANGE: { LColour::OnChange(); break; } case WM_WINDOWPOSCHANGING: { bool Icon = IsIconic(Handle()) != 0; bool Zoom = IsZoomed(Handle()) != 0; if (!Icon && (_Dialog || !Zoom)) { WINDOWPOS *Info = (LPWINDOWPOS) Msg->b; if (!Info) break; if (Info->flags == (SWP_NOSIZE | SWP_NOMOVE) && _Dialog) { // Info->flags |= SWP_NOZORDER; Info->hwndInsertAfter = _Dialog->Handle(); } if (GetMinimumSize().x && GetMinimumSize().x > Info->cx) { Info->cx = GetMinimumSize().x; } if (GetMinimumSize().y && GetMinimumSize().y > Info->cy) { Info->cy = GetMinimumSize().y; } /* This is broken on windows 10... windows get stuck on the edge of the desktop. RECT Rc; if (d->SnapToEdge && SystemParametersInfo(SPI_GETWORKAREA, 0, &Rc, SPIF_SENDCHANGE)) { LRect r = Rc; LRect p(Info->x, Info->y, Info->x + Info->cx - 1, Info->y + Info->cy - 1); if (r.Valid() && p.Valid()) { int Snap = 12; if (abs(p.x1 - r.x1) <= Snap) { // Snap left edge Info->x = r.x1; } else if (abs(p.x2 - r.x2) <= Snap) { // Snap right edge Info->x = r.x2 - Info->cx + 1; } if (abs(p.y1 - r.y1) <= Snap) { // Snap top edge Info->y = r.y1; } else if (abs(p.y2 - r.y2) <= Snap) { // Snap bottom edge Info->y = r.y2 - Info->cy + 1; } } } */ } break; } case WM_SIZE: { if (Visible()) { LWindowZoom z = d->Show; switch (Msg->a) { case SIZE_MINIMIZED: { z = LZoomMin; break; } case SIZE_MAXIMIZED: { z = LZoomMax; break; } case SIZE_RESTORED: { z = LZoomNormal; break; } } if (z != d->Show) { OnZoom(d->Show = z); } } Status = LView::OnEvent(Msg) != 0; break; } case WM_CREATE: { if (d->AlwaysOnTop) SetAlwaysOnTop(true); PourAll(); OnCreate(); if (!_Default) { _Default = FindControl(IDOK); if (_Default) _Default->Invalidate(); } d->InCreate = false; if (d->Wp) { PostEvent(M_SET_WINDOW_PLACEMENT); } break; } case WM_WINDOWPOSCHANGED: { d->Wp.Reset(); Status = LView::OnEvent(Msg) != 0; break; } case WM_QUERYENDSESSION: case WM_CLOSE: { bool QuitApp; bool OsShuttingDown = Msg->Msg() == WM_QUERYENDSESSION; if (QuitApp = OnRequestClose(OsShuttingDown)) { Quit(); } if (Msg->Msg() == WM_CLOSE) { return 0; } else { return QuitApp; } break; } case WM_SYSCOMMAND: { if (Msg->a == SC_CLOSE) { if (OnRequestClose(false)) { Quit(); } return 0; } else { Status = LView::OnEvent(Msg) != 0; } break; } case WM_DROPFILES: { HDROP hDrop = (HDROP) Msg->a; if (hDrop) { LArray FileNames; int Count = 0; Count = DragQueryFileW(hDrop, -1, NULL, 0); for (int i=0; i 0) { FileNames.Add(WideToUtf8(FileName)); } } OnReceiveFiles(FileNames); FileNames.DeleteArrays(); } break; } case M_HANDLEMOUSEMOVE: { // This receives events fired from the LMouseHookPrivate class so that // non-LGI windows create mouse hook events as well. LTempView v((OsView)Msg->B()); LMouse m; m.x = LOWORD(Msg->A()); m.y = HIWORD(Msg->A()); HandleViewMouse(&v, m); break; } case M_COMMAND: { HWND OurWnd = Handle(); // copy onto the stack, because // we might lose the 'this' object in the // OnCommand handler which would delete // the memory containing the handle. Status = OnCommand((int) Msg->a, 0, (OsView) Msg->b); if (!IsWindow(OurWnd)) { // The window was deleted so break out now break; } // otherwise fall thru to the LView handler } default: { Status = (int) LView::OnEvent(Msg); break; } } return Status; } LPoint LWindow::GetDpi() { if (!d->Dpi.x) d->Dpi = LGetDpiForWindow(_View); return d->Dpi; } LPointF LWindow::GetDpiScale() { auto Dpi = GetDpi(); LPointF r( Dpi.x / 96.0, Dpi.y / 96.0 ); return r; } LRect &LWindow::GetPos() { if (_View && IsZoomed(_View)) { static LRect r; RECT rc; GetWindowRect(_View, &rc); r = rc; return r; } return Pos; } void LWindow::OnPosChange() { PourAll(); } bool LWindow::RegisterHook(LView *Target, LWindowHookType EventType, int Priority) { bool Status = false; if (Target && EventType) { auto i = d->GetHookIndex(Target, true); if (i >= 0) { d->Hooks[i].Flags = EventType; Status = true; } } return Status; } LViewI *LWindow::GetDefault() { return _Default; } void LWindow::SetDefault(LViewI *v) { #if WINNATIVE LButton *Btn; if (Btn = dynamic_cast(_Default)) Btn->Default(false); #endif _Default = v; #if WINNATIVE if (Btn = dynamic_cast(_Default)) Btn->Default(true); #endif } bool LWindow::UnregisterHook(LView *Target) { auto i = d->GetHookIndex(Target); if (i >= 0) { d->Hooks.DeleteAt(i); return true; } return false; } bool LWindow::SerializeState(LDom *Store, const char *FieldName, bool Load) { if (!Store || !FieldName) return false; #if DEBUG_SERIALIZE_STATE LgiTrace("LWindow::SerializeState(%p, %s, %i)\n", Store, FieldName, Load); #endif if (Load) { LVariant v; if (Store->GetValue(FieldName, v) && v.Str()) { LRect Position(0, 0, -1, -1); LWindowZoom State = LZoomNormal; #if DEBUG_SERIALIZE_STATE LgiTrace("\t::SerializeState:%i v=%s\n", __LINE__, v.Str()); #endif - LToken t(v.Str(), ";"); - for (int i=0; iSetValue(FieldName, v)) return false; } return true; } void LWindow::OnTrayClick(LMouse &m) { if (m.Down() || m.IsContextMenu()) { LSubMenu RClick; OnTrayMenu(RClick); if (GetMouse(m, true)) { #if WINNATIVE SetForegroundWindow(Handle()); #endif int Result = RClick.Float(this, m); #if WINNATIVE PostMessage(Handle(), WM_NULL, 0, 0); #endif OnTrayMenuResult(Result); } } }