Refactoring, dialog work

This commit is contained in:
elasota
2019-12-31 02:42:20 -05:00
parent 04a955213c
commit 84e4f9fb0b
26 changed files with 817 additions and 271 deletions

View File

@@ -1,5 +1,6 @@
#include "DialogManager.h"
#include "ResourceManager.h"
#include "PLArrayView.h"
#include "PLDialogs.h"
#include "PLBigEndian.h"
#include "PLPasStr.h"
@@ -13,21 +14,247 @@
namespace PortabilityLayer
{
namespace SerializedDialogItemTypeCodes
{
enum SerializedDialogItemTypeCode
{
kUserItem = 0x00,
kButton = 0x04,
kCheckBox = 0x05,
kRadioButton = 0x06,
kCustomControl = 0x07,
kLabel = 0x08,
kEditBox = 0x10,
kIcon = 0x20,
kImage = 0x40,
};
}
typedef SerializedDialogItemTypeCodes::SerializedDialogItemTypeCode SerializedDialogItemTypeCode_t;
struct DialogTemplateItem
{
Rect m_rect;
int16_t m_id;
uint8_t m_serializedType;
bool m_enabled;
Str255 m_name;
};
class DialogItemImpl : public DialogItem
{
public:
DialogItemImpl(const DialogTemplateItem &templateItem);
virtual ~DialogItemImpl();
Rect GetRect() const override;
virtual bool Init() = 0;
virtual void Destroy() = 0;
protected:
Rect m_rect;
int16_t m_id;
bool m_enabled;
PascalStr<255> m_name;
};
template<class T>
class DialogItemSpec : public DialogItemImpl
{
public:
DialogItemSpec(const DialogTemplateItem &tmpl)
: DialogItemImpl(tmpl)
{
}
void Destroy() override
{
static_cast<T*>(this)->~T();
free(static_cast<T*>(this));
}
static DialogItemSpec *Create(const DialogTemplateItem &tmpl)
{
void *storage = malloc(sizeof(T));
if (!storage)
return nullptr;
T *item = new (storage) T(tmpl);
DialogItemImpl *dItem = static_cast<DialogItemImpl*>(item);
if (!dItem->Init())
{
dItem->Destroy();
return nullptr;
}
return item;
}
};
class DialogItem_EditBox final : public DialogItemSpec<DialogItem_EditBox>
{
public:
explicit DialogItem_EditBox(const DialogTemplateItem &tmpl);
bool Init() override;
};
class DialogItem_Label final : public DialogItemSpec<DialogItem_Label>
{
public:
explicit DialogItem_Label(const DialogTemplateItem &tmpl);
bool Init() override;
};
class DialogItem_Unknown final : public DialogItemSpec<DialogItem_Unknown>
{
public:
explicit DialogItem_Unknown(const DialogTemplateItem &tmpl);
bool Init() override;
};
class DialogTemplate final
{
public:
DialogTemplate(DialogTemplateItem *itemStorage, size_t numItems);
void DeserializeItems(const uint8_t *data);
void Destroy();
ArrayView<const DialogTemplateItem> GetItems() const;
private:
DialogTemplateItem *m_items;
size_t m_numItems;
};
class DialogImpl final : public Dialog
{
public:
void Destroy() override;
Window *GetWindow() const override;
ArrayView<DialogItem*const> GetItems() const override;
static DialogImpl *Create(Window *window);
bool Populate(DialogTemplate *tmpl);
static DialogImpl *Create(Window *window, size_t numItems);
private:
explicit DialogImpl(Window *window);
explicit DialogImpl(Window *window, DialogItem **items, size_t numItems);
~DialogImpl();
Window *m_window;
DialogItem **m_items;
size_t m_numItems;
};
DialogItemImpl::DialogItemImpl(const DialogTemplateItem &templateItem)
: m_enabled(templateItem.m_enabled)
, m_id(templateItem.m_id)
, m_name(PLPasStr(templateItem.m_name))
, m_rect(templateItem.m_rect)
{
}
DialogItemImpl::~DialogItemImpl()
{
}
Rect DialogItemImpl::GetRect() const
{
return m_rect;
}
DialogItem_EditBox::DialogItem_EditBox(const DialogTemplateItem &tmpl)
: DialogItemSpec<DialogItem_EditBox>(tmpl)
{
}
bool DialogItem_EditBox::Init()
{
return true;
}
DialogItem_Label::DialogItem_Label(const DialogTemplateItem &tmpl)
: DialogItemSpec<DialogItem_Label>(tmpl)
{
}
bool DialogItem_Label::Init()
{
return true;
}
DialogItem_Unknown::DialogItem_Unknown(const DialogTemplateItem &tmpl)
: DialogItemSpec<DialogItem_Unknown>(tmpl)
{
}
bool DialogItem_Unknown::Init()
{
return true;
}
DialogTemplate::DialogTemplate(DialogTemplateItem *itemStorage, size_t numItems)
: m_items(itemStorage)
, m_numItems(numItems)
{
}
void DialogTemplate::DeserializeItems(const uint8_t *data)
{
for (size_t i = 0; i < m_numItems; i++)
{
BERect itemRect;
uint8_t itemType;
data += 4; // Unused
memcpy(&itemRect, data, 8);
data += 8;
itemType = *data;
data++;
uint8_t nameLength = *data;
data++;
const uint8_t *nameBytes = data;
size_t nameLengthPadded = nameLength;
if ((nameLength & 1) == 1)
nameLengthPadded++;
data += nameLengthPadded;
DialogTemplateItem &item = m_items[i];
item.m_rect = itemRect.ToRect();
item.m_id = 0;
item.m_serializedType = (itemType & 0x7f);
item.m_enabled = ((itemType & 0x80) == 0);
item.m_name[0] = nameLength;
memcpy(item.m_name + 1, nameBytes, nameLength);
if (item.m_serializedType == SerializedDialogItemTypeCodes::kCustomControl || item.m_serializedType == SerializedDialogItemTypeCodes::kImage || item.m_serializedType == SerializedDialogItemTypeCodes::kIcon)
{
memcpy(&item.m_id, item.m_name + 1, 2);
ByteSwap::BigInt16(item.m_id);
}
}
}
void DialogTemplate::Destroy()
{
this->~DialogTemplate();
free(this);
}
ArrayView<const DialogTemplateItem> DialogTemplate::GetItems() const
{
return ArrayView<const DialogTemplateItem>(m_items, m_numItems);
}
void DialogImpl::Destroy()
{
PortabilityLayer::WindowManager::GetInstance()->DestroyWindow(m_window);
@@ -41,29 +268,85 @@ namespace PortabilityLayer
return m_window;
}
DialogImpl *DialogImpl::Create(Window *window)
ArrayView<DialogItem*const> DialogImpl::GetItems() const
{
void *storage = malloc(sizeof(DialogImpl));
ArrayView<DialogItem*const> iter(m_items, m_numItems);
return ArrayView<DialogItem*const>(m_items, m_numItems);
}
bool DialogImpl::Populate(DialogTemplate *tmpl)
{
ArrayView<const DialogTemplateItem> templateItems = tmpl->GetItems();
const size_t numItems = templateItems.Count();
for (size_t i = 0; i < numItems; i++)
{
const DialogTemplateItem &templateItem = templateItems[i];
DialogItem *ditem = nullptr;
switch (templateItem.m_serializedType)
{
case SerializedDialogItemTypeCodes::kLabel:
ditem = DialogItem_Label::Create(templateItem);
break;
case SerializedDialogItemTypeCodes::kEditBox:
ditem = DialogItem_EditBox::Create(templateItem);
break;
default:
ditem = DialogItem_Unknown::Create(templateItem);
break;
}
if (!ditem)
return false;
m_items[i] = ditem;
}
return true;
}
DialogImpl *DialogImpl::Create(Window *window, size_t numItems)
{
size_t alignedSize = sizeof(DialogImpl) + PL_SYSTEM_MEMORY_ALIGNMENT + 1;
alignedSize -= alignedSize % PL_SYSTEM_MEMORY_ALIGNMENT;
const size_t itemsSize = sizeof(DialogItemImpl) * numItems;
void *storage = malloc(alignedSize + itemsSize);
if (!storage)
return nullptr;
return new (storage) DialogImpl(window);
DialogItem **itemsList = reinterpret_cast<DialogItem **>(static_cast<uint8_t*>(storage) + alignedSize);
for (size_t i = 0; i < numItems; i++)
itemsList[i] = nullptr;
return new (storage) DialogImpl(window, itemsList, numItems);
}
DialogImpl::DialogImpl(Window *window)
DialogImpl::DialogImpl(Window *window, DialogItem **itemsList, size_t numItems)
: m_window(window)
, m_items(itemsList)
, m_numItems(numItems)
{
}
DialogImpl::~DialogImpl()
{
for (size_t i = 0; i < m_numItems; i++)
{
if (DialogItem *item = m_items[i])
static_cast<DialogItemImpl*>(item)->Destroy();
}
}
// DLOG resource format:
// DialogResourceDataHeader
// Variable-length PStr: Title
// Optional: Positioning (2 byte mask)
struct DialogResourceDataHeader
{
BERect m_rect;
@@ -80,6 +363,7 @@ namespace PortabilityLayer
{
public:
Dialog *LoadDialog(int16_t resID, Window *behindWindow) override;
DialogTemplate *LoadDialogTemplate(int16_t resID);
static DialogManagerImpl *GetInstance();
@@ -110,31 +394,83 @@ namespace PortabilityLayer
dlogH.Dispose();
DialogTemplate *dtemplate = LoadDialogTemplate(header.m_itemsResID);
const size_t numItems = (dtemplate == nullptr) ? 0 : dtemplate->GetItems().Count();
if (!rect.IsValid())
{
dtemplate->Destroy();
return nullptr;
}
WindowManager *wm = PortabilityLayer::WindowManager::GetInstance();
WindowDef wdef = WindowDef::Create(rect, 0, header.m_visible != 0, header.m_hasCloseBox != 0, header.m_referenceConstant, positionSpec, PLPasStr(titlePStr));
Window *window = wm->CreateWindow(wdef);
if (!window)
{
dtemplate->Destroy();
return nullptr;
}
wm->PutWindowBehind(window, behindWindow);
THandle<uint8_t> dtemplateH = rm->GetResource('DITL', header.m_itemsResID).StaticCast<uint8_t>();
Dialog *dialog = DialogImpl::Create(window);
DialogImpl *dialog = DialogImpl::Create(window, numItems);
if (!dialog)
{
dtemplate->Destroy();
wm->DestroyWindow(window);
return nullptr;
}
if (!dialog->Populate(dtemplate))
{
dialog->Destroy();
dtemplate->Destroy();
wm->DestroyWindow(window);
return nullptr;
}
return dialog;
}
DialogTemplate *DialogManagerImpl::LoadDialogTemplate(int16_t resID)
{
ResourceManager *rm = ResourceManager::GetInstance();
THandle<uint8_t> dtemplateH = rm->GetResource('DITL', resID).StaticCast<uint8_t>();
if (!dtemplateH)
return nullptr;
uint16_t numItems;
memcpy(&numItems, *dtemplateH, 2);
ByteSwap::BigUInt16(numItems);
size_t dtlAlignedSize = sizeof(DialogTemplate) + PL_SYSTEM_MEMORY_ALIGNMENT - 1;
dtlAlignedSize -= dtlAlignedSize % PL_SYSTEM_MEMORY_ALIGNMENT;
const size_t dtlItemSize = sizeof(DialogTemplateItem) * numItems;
void *storage = malloc(dtlAlignedSize + dtlItemSize);
if (!storage)
{
dtemplateH.Dispose();
return nullptr;
}
uint8_t *itemsLoc = static_cast<uint8_t*>(storage) + dtlAlignedSize;
DialogTemplate *dtemplate = new (storage) DialogTemplate(reinterpret_cast<DialogTemplateItem*>(itemsLoc), numItems);
dtemplate->DeserializeItems((*dtemplateH) + 2);
dtemplateH.Dispose();
return dtemplate;
}
DialogManagerImpl *DialogManagerImpl::GetInstance()
{
return &ms_instance;

View File

@@ -0,0 +1,64 @@
#pragma once
#include <stdint.h>
template<class T>
class ArrayViewIterator;
template<class T>
class ArrayView
{
public:
ArrayView(const T *items, size_t count);
ArrayView(const ArrayView<T> &other);
size_t Count() const;
const T &operator[](size_t index) const;
ArrayViewIterator<T> begin() const;
ArrayViewIterator<T> end() const;
private:
const T *m_items;
size_t m_count;
};
#include "PLArrayViewIterator.h"
template<class T>
inline ArrayView<T>::ArrayView(const T *items, size_t count)
: m_items(items)
, m_count(count)
{
}
template<class T>
inline ArrayView<T>::ArrayView(const ArrayView<T> &other)
: m_items(other.m_items)
, m_count(other.m_count)
{
}
template<class T>
inline size_t ArrayView<T>::Count() const
{
return m_count;
}
template<class T>
const T &ArrayView<T>::operator[](size_t index) const
{
return m_items[index];
}
template<class T>
inline ArrayViewIterator<T> ArrayView<T>::begin() const
{
return ArrayViewIterator<T>(m_items);
}
template<class T>
inline ArrayViewIterator<T> ArrayView<T>::end() const
{
return ArrayViewIterator<T>(m_items + m_count);
}

View File

@@ -0,0 +1,102 @@
#pragma once
#include <stdint.h>
template<class T>
class ArrayViewIterator
{
public:
ArrayViewIterator(T *item);
ArrayViewIterator(const ArrayViewIterator<T> &other);
ArrayViewIterator<T> operator++(int);
ArrayViewIterator<T> &operator++();
ArrayViewIterator<T> operator--(int);
ArrayViewIterator<T> &operator--();
ArrayViewIterator<T> &operator+=(ptrdiff_t delta);
ArrayViewIterator<T> &operator-=(ptrdiff_t delta);
bool operator==(const ArrayViewIterator<T> &other) const;
bool operator!=(const ArrayViewIterator<T> &other) const;
operator T*() const;
private:
T *m_iter;
};
template<class T>
inline ArrayViewIterator<T>::ArrayViewIterator(T *item)
: m_iter(item)
{
}
template<class T>
inline ArrayViewIterator<T>::ArrayViewIterator(const ArrayViewIterator<T> &other)
: m_iter(other.m_iter)
{
}
template<class T>
inline ArrayViewIterator<T> ArrayViewIterator<T>::operator++(int)
{
ArrayViewIterator<T> copy = *this;
m_iter++;
return copy;
}
template<class T>
inline ArrayViewIterator<T> &ArrayViewIterator<T>::operator++()
{
m_iter++;
return *this;
}
template<class T>
inline ArrayViewIterator<T> ArrayViewIterator<T>::operator--(int)
{
ArrayViewIterator<T> copy = *this;
m_iter--;
return copy;
}
template<class T>
inline ArrayViewIterator<T> &ArrayViewIterator<T>::operator--()
{
m_iter--;
return *this;
}
template<class T>
inline ArrayViewIterator<T> &ArrayViewIterator<T>::operator+=(ptrdiff_t delta)
{
m_iter += delta;
return *this;
}
template<class T>
inline ArrayViewIterator<T> &ArrayViewIterator<T>::operator-=(ptrdiff_t delta)
{
m_iter += delta;
return *this;
}
template<class T>
inline bool ArrayViewIterator<T>::operator==(const ArrayViewIterator<T> &other) const
{
return m_iter == other.m_iter;
}
template<class T>
inline bool ArrayViewIterator<T>::operator!=(const ArrayViewIterator<T> &other) const
{
return m_iter == other.m_iter;
}
template<class T>
inline ArrayViewIterator<T>::operator T*() const
{
return m_iter;
}

View File

@@ -1020,5 +1020,5 @@ Window::Window()
DrawSurface *Window::GetDrawSurface() const
{
return const_cast<DrawSurface*>(&m_graf);
return const_cast<DrawSurface*>(&m_graf);
}

View File

@@ -1,23 +1,23 @@
#include "PLDialogs.h"
void DrawDialog(DialogPtr dialog)
void DrawDialog(Dialog *dialog)
{
PL_NotYetImplemented();
}
DialogPtr GetNewDialog(int resID, void *unknown, WindowPtr behind)
Dialog *GetNewDialog(int resID, void *unknown, WindowPtr behind)
{
PL_NotYetImplemented();
return nullptr;
}
DrawSurface *GetDialogPort(DialogPtr dialog)
DrawSurface *GetDialogPort(Dialog *dialog)
{
PL_NotYetImplemented();
return nullptr;
}
void GetDialogItem(DialogPtr dialog, int index, short *itemType, THandle<Control> *itemHandle, Rect *itemRect)
void GetDialogItem(Dialog *dialog, int index, short *itemType, THandle<Control> *itemHandle, Rect *itemRect)
{
PL_NotYetImplemented();
}
@@ -27,7 +27,7 @@ void GetDialogItemText(THandle<Control> handle, StringPtr str)
PL_NotYetImplemented();
}
void SetDialogItem(DialogPtr dialog, int index, short itemType, THandle<Control> itemHandle, const Rect *itemRect)
void SetDialogItem(Dialog *dialog, int index, short itemType, THandle<Control> itemHandle, const Rect *itemRect)
{
PL_NotYetImplemented();
}
@@ -37,7 +37,7 @@ void SetDialogItemText(THandle<Control> handle, const PLPasStr &str)
PL_NotYetImplemented();
}
void SelectDialogItemText(DialogPtr dialog, int item, int firstSelChar, int lastSelCharExclusive)
void SelectDialogItemText(Dialog *dialog, int item, int firstSelChar, int lastSelCharExclusive)
{
PL_NotYetImplemented();
}
@@ -52,7 +52,7 @@ void ModalDialog(ModalFilterUPP filter, short *item)
PL_NotYetImplemented();
}
void DisposeDialog(DialogPtr dialog)
void DisposeDialog(Dialog *dialog)
{
PL_NotYetImplemented();
}
@@ -62,12 +62,12 @@ void DisposeModalFilterUPP(ModalFilterUPP upp)
PL_NotYetImplemented();
}
void ShowDialogItem(DialogPtr dialog, int item)
void ShowDialogItem(Dialog *dialog, int item)
{
PL_NotYetImplemented();
}
void HideDialogItem(DialogPtr dialog, int item)
void HideDialogItem(Dialog *dialog, int item)
{
PL_NotYetImplemented();
}

View File

@@ -4,19 +4,22 @@
#include "PLCore.h"
template<class T>
class ArrayView;
class PLPasStr;
struct Control;
struct DialogItem
{
virtual Rect GetRect() const = 0;
};
struct Dialog
{
virtual void Destroy() = 0;
virtual Window *GetWindow() const = 0;
};
struct DialogTemplate
{
// FIXME: Audit
Rect boundsRect;
virtual ArrayView<DialogItem*const> GetItems() const = 0;
};
enum TEMode
@@ -24,33 +27,28 @@ enum TEMode
teCenter
};
typedef Dialog *DialogPtr;
typedef Boolean(*ModalFilterUPP)(Dialog *dial, EventRecord *event, short *item);
typedef THandle<DialogTemplate> DialogTHndl;
void DrawDialog(Dialog *dialog);
DrawSurface *GetDialogPort(Dialog *dialog);
typedef Boolean(*ModalFilterUPP)(DialogPtr dial, EventRecord *event, short *item);
void DrawDialog(DialogPtr dialog);
DrawSurface *GetDialogPort(DialogPtr dialog);
void GetDialogItem(DialogPtr dialog, int index, short *itemType, THandle<Control> *itemHandle, Rect *itemRect);
void GetDialogItem(Dialog *dialog, int index, short *itemType, THandle<Control> *itemHandle, Rect *itemRect);
void GetDialogItemText(THandle<Control> handle, StringPtr str);
void SetDialogItem(DialogPtr dialog, int index, short itemType, THandle<Control> itemHandle, const Rect *itemRect);
void SetDialogItem(Dialog *dialog, int index, short itemType, THandle<Control> itemHandle, const Rect *itemRect);
void SetDialogItemText(THandle<Control> handle, const PLPasStr &str);
void SelectDialogItemText(DialogPtr dialog, int item, int firstSelChar, int lastSelCharExclusive);
void SelectDialogItemText(Dialog *dialog, int item, int firstSelChar, int lastSelCharExclusive);
ModalFilterUPP NewModalFilterUPP(ModalFilterUPP func);
void ModalDialog(ModalFilterUPP filter, short *item);
void DisposeDialog(DialogPtr dialog);
void DisposeDialog(Dialog *dialog);
void DisposeModalFilterUPP(ModalFilterUPP upp);
void ShowDialogItem(DialogPtr dialog, int item);
void HideDialogItem(DialogPtr dialog, int item);
void ShowDialogItem(Dialog *dialog, int item);
void HideDialogItem(Dialog *dialog, int item);
void TETextBox(const PLPasStr &str, short len, const Rect *rect, TEMode teMode);

View File

@@ -1020,6 +1020,12 @@ void DrawSurface::FrameRect(const Rect &rect)
}
}
void DrawSurface::FrameRoundRect(const Rect &rect, int quadrantWidth, int quadrantHeight)
{
PL_NotYetImplemented_TODO("RoundRect");
this->FrameRect(rect);
}
void DrawSurface::InvertFrameRect(const Rect &rect, const uint8_t *pattern)
{
PL_NotYetImplemented();

View File

@@ -1,9 +1,8 @@
#pragma once
#ifndef __PL_PASCALSTR_H__
#define __PL_PASCALSTR_H__
#include "UnsafePascalStr.h"
#include "UnsafePascalStr.h"
class PLPasStr;
namespace PortabilityLayer
{
@@ -12,11 +11,13 @@ namespace PortabilityLayer
{
public:
PascalStr();
PascalStr(size_t size, const char *str);
PascalStr(size_t size, const char *str);
explicit PascalStr(const PLPasStr &pstr);
};
}
#include <string.h>
#include <string.h>
#include "PLPasStr.h"
namespace PortabilityLayer
{
@@ -31,6 +32,10 @@ namespace PortabilityLayer
: UnsafePascalStr<TSize, true>(size, str)
{
}
}
#endif
template<size_t TSize>
PascalStr<TSize>::PascalStr(const PLPasStr &pstr)
: UnsafePascalStr<TSize, true>(pstr.Length(), pstr.Chars())
{
}
}

View File

@@ -186,6 +186,8 @@
<ClInclude Include="PLAppleEvents.h" />
<ClInclude Include="PLAppleEventsCommonTypes.h" />
<ClInclude Include="PLApplication.h" />
<ClInclude Include="PLArrayView.h" />
<ClInclude Include="PLArrayViewIterator.h" />
<ClInclude Include="PLBigEndian.h" />
<ClInclude Include="PLControlDefinitions.h" />
<ClInclude Include="PLCore.h" />

View File

@@ -402,6 +402,12 @@
<ClInclude Include="ResolvedColor.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="PLArrayView.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="PLArrayViewIterator.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="CFileStream.cpp">

View File

@@ -54,6 +54,7 @@ struct DrawSurface final
void FillRect(const Rect &rect);
void FillRectWithPattern8x8(const Rect &rect, const uint8_t *pattern);
void FrameRect(const Rect &rect);
void FrameRoundRect(const Rect &rect, int quadrantWidth, int quadrantHeight);
void InvertFrameRect(const Rect &rect, const uint8_t *pattern);
void InvertFillRect(const Rect &rect, const uint8_t *pattern);

View File

@@ -26,6 +26,8 @@ struct Rect
bool IsValid() const;
Rect Intersect(const Rect &rect) const;
Rect MakeValid() const;
Rect operator-(const Point &point) const;
Rect operator+(const Point &point) const;
static Rect Create(int16_t top, int16_t left, int16_t bottom, int16_t right);
static Rect CreateFromPoints(const Point &topLeft, const Point &bottomRight);
@@ -152,6 +154,16 @@ inline Rect Rect::MakeValid() const
return result;
}
inline Rect Rect::operator-(const Point &point) const
{
return Rect::Create(this->top - point.v, this->left - point.h, this->bottom - point.v, this->right - point.h);
}
inline Rect Rect::operator+(const Point &point) const
{
return Rect::Create(this->top + point.v, this->left + point.h, this->bottom + point.v, this->right + point.h);
}
inline Rect Rect::Create(int16_t top, int16_t left, int16_t bottom, int16_t right)
{
Rect result;