implemented most of DataEditor; fixed some bugs in Settings

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@78 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2006-11-20 00:09:09 +00:00
parent 040e87e938
commit ecf28b00a3
10 changed files with 288 additions and 28 deletions
+168 -2
View File
@@ -8,23 +8,189 @@
#include <gui/control/card_editor.hpp>
#include <gui/value/editor.hpp>
#include <gui/icon_menu.hpp>
#include <data/field.hpp>
#include <data/stylesheet.hpp>
#include <data/settings.hpp>
DECLARE_TYPEOF_COLLECTION(ValueViewerP);
// ----------------------------------------------------------------------------- : DataEditor
#define FOR_EACH_EDITOR \
FOR_EACH(v, viewers) \
#define FOR_EACH_EDITOR \
FOR_EACH(v, viewers) \
if (ValueEditor* e = v->getEditor())
#define FOR_EACH_EDITOR_REVERSE \
FOR_EACH_REVERSE(v, viewers) \
if (ValueEditor* e = v->getEditor())
DataEditor::DataEditor(Window* parent, int id, long style)
: CardViewer(parent, id, style)
, current_viewer(nullptr)
, current_editor(nullptr)
{}
ValueViewerP DataEditor::makeViewer(const StyleP& style) {
return style->makeEditor(*this, style);
}
// ----------------------------------------------------------------------------- : Utility for ValueViewers
bool DataEditor::drawBorders() const {
return !nativeLook() && settings.stylesheetSettingsFor(*set->stylesheetFor(card)).card_borders();
}
bool DataEditor::drawEditing() const {
return true;
}
wxPen DataEditor::borderPen(bool active) const {
return active ? wxPen(Color(0,128,255), 1, wxSOLID)
: wxPen(Color(128,128,128), 1, wxDOT);
}
ValueViewer* DataEditor::focusedViewer() const {
return current_viewer;
}
// ----------------------------------------------------------------------------- : Selection
// ----------------------------------------------------------------------------- : Clipboard & Formatting
bool DataEditor::canCut() const { return current_editor && current_editor->canCut(); }
bool DataEditor::canCopy() const { return current_editor && current_editor->canCopy(); }
bool DataEditor::canPaste() const { return current_editor && current_editor->canPaste(); }
bool DataEditor::canFormat(int type) const { return current_editor && current_editor->canFormat(type); }
bool DataEditor::hasFormat(int type) const { return current_editor && current_editor->hasFormat(type); }
void DataEditor::doCut() { if (current_editor) current_editor->doCut(); }
void DataEditor::doCopy() { if (current_editor) current_editor->doCopy(); }
void DataEditor::doPaste() { if (current_editor) current_editor->doPaste(); }
void DataEditor::doFormat(int type) { if (current_editor) current_editor->doFormat(type); }
// ----------------------------------------------------------------------------- : Mouse events
void DataEditor::onLeftDown(wxMouseEvent& ev) {
ev.Skip(); // for focus
CaptureMouse();
// change selection?
selectField(ev, &ValueEditor::onLeftDown);
}
void DataEditor::onLeftUp(wxMouseEvent& ev) {
if (HasCapture()) ReleaseMouse();
if (current_editor) current_editor->onLeftUp(mousePoint(ev), ev);
}
void DataEditor::onLeftDClick(wxMouseEvent& ev) {
if (current_editor) current_editor->onLeftDClick(mousePoint(ev), ev);
}
void DataEditor::onRightDown(wxMouseEvent& ev) {
ev.Skip(); // for context menu
// change selection?
selectField(ev, &ValueEditor::onRightDown);
}
void DataEditor::onMouseWheel(wxMouseEvent& ev) {
if (current_editor) current_editor->onMouseWheel(mousePoint(ev), ev);
}
void DataEditor::onMotion(wxMouseEvent& ev) {
RealPoint pos = mousePoint(ev);
if (current_editor && ev.LeftIsDown()) {
current_editor->onMotion(pos, ev);
}
if (!HasCapture()) {
// change cursor and set status text
wxFrame* frame = dynamic_cast<wxFrame*>( wxGetTopLevelParent(this) );
FOR_EACH_EDITOR_REVERSE { // find high z index fields first
if (v->containsPoint(pos) && v->getField()->editable) {
wxCursor c = e->cursor();
if (c.Ok()) SetCursor(c);
else SetCursor(wxCURSOR_ARROW);
if (frame) frame->SetStatusText(v->getField()->description);
return;
}
}
// no field under cursor
SetCursor(wxCURSOR_ARROW);
if (frame) frame->SetStatusText(wxEmptyString);
}
}
void DataEditor::onMouseLeave(wxMouseEvent& ev) {
wxFrame* frame = dynamic_cast<wxFrame*>( wxGetTopLevelParent(this) );
if (frame) frame->SetStatusText(wxEmptyString);
}
void DataEditor::selectField(wxMouseEvent& ev, void (ValueEditor::*event)(const RealPoint&, wxMouseEvent&)) {
RealPoint pos = mousePoint(ev);
// change viewer/editor
ValueEditor* old_editor = current_editor;
selectFieldNoEvents(pos);
if (old_editor != current_editor) {
// selection has changed, send focus events
if (old_editor) old_editor->onLoseFocus();
if (current_editor) current_editor->onFocus();
}
// pass event
if (current_editor) (current_editor->*event)(pos, ev);
// refresh?
if (old_editor != current_editor) {
// selection has changed, refresh viewers
// NOTE: after passing mouse down event, otherwise opening combo box produces flicker
onChange();
}
}
void DataEditor::selectFieldNoEvents(const RealPoint& p) {
FOR_EACH_EDITOR_REVERSE { // find high z index fields first
if (v->containsPoint(p) && v->getField()->editable) {
current_viewer = v.get();
current_editor = e;
return;
}
}
current_viewer = nullptr;
current_editor = nullptr;
}
RealPoint DataEditor::mousePoint(const wxMouseEvent& ev) {
StyleSheetP stylesheet = set->stylesheetFor(card);
StyleSheetSettings& ss = settings.stylesheetSettingsFor(*stylesheet);
Rotation rot(ss.card_angle(), stylesheet->getCardRect(), ss.card_zoom());
return rot.trInv(RealPoint(ev.GetX(), ev.GetY()));
}
// ----------------------------------------------------------------------------- : Keyboard events
// ----------------------------------------------------------------------------- : Menu events
void DataEditor::onContextMenu(wxContextMenuEvent& ev) {
if (current_editor) {
IconMenu m;
m.Append(wxID_CUT, _("TOOL_CUT"), _("Cu&t"), _("Move the selected text to the clipboard"));
m.Append(wxID_COPY, _("TOOL_COPY"), _("&Copy"), _("Place the selected text on the clipboard"));
m.Append(wxID_PASTE, _("TOOL_PASTE"), _("&Paste"), _("Inserts the text from the clipboard"));
m.Enable(wxID_CUT, canCut());
m.Enable(wxID_COPY, canCopy());
m.Enable(wxID_PASTE, canPaste());
if (current_editor->onContextMenu(m, ev)) {
PopupMenu(&m);
}
}
}
// ----------------------------------------------------------------------------- : Focus events
// ----------------------------------------------------------------------------- : Event table
BEGIN_EVENT_TABLE(DataEditor, CardViewer)
EVT_LEFT_DOWN (DataEditor::onLeftDown)
EVT_LEFT_UP (DataEditor::onLeftUp)
EVT_LEFT_DCLICK (DataEditor::onLeftDClick)
EVT_RIGHT_DOWN (DataEditor::onRightDown)
EVT_MOTION (DataEditor::onMotion)
EVT_MOUSEWHEEL (DataEditor::onMouseWheel)
EVT_LEAVE_WINDOW (DataEditor::onMouseLeave)
// EVT_CONTEXT_MENU (DataEditor::onContextMenu)
// EVT_CHAR (DataEditor::onChar)
// EVT_SET_FOCUS (DataEditor::onFocus)
// EVT_KILL_FOCUS (DataEditor::onLoseFocus)
// EVT_MENU (wxID_ANY, DataEditor::onMenu)
END_EVENT_TABLE ()
+62
View File
@@ -12,6 +12,8 @@
#include <util/prec.hpp>
#include <gui/control/card_viewer.hpp>
class ValueEditor;
// ----------------------------------------------------------------------------- : DataEditor
/// An editor for data values (usually a card)
@@ -19,12 +21,72 @@ class DataEditor : public CardViewer {
public:
DataEditor(Window* parent, int id, long style = 0);
// --------------------------------------------------- : Utility for ValueViewers
virtual bool drawBorders() const;
virtual bool drawEditing() const;
virtual wxPen borderPen(bool active) const;
virtual ValueViewer* focusedViewer() const;
// --------------------------------------------------- : Selection
// TODO
// --------------------------------------------------- : Clipboard
bool canCut() const;
bool canCopy() const;
bool canPaste() const;
void doCut();
void doCopy();
void doPaste();
// --------------------------------------------------- : Formatting
bool canFormat(int type) const;
bool hasFormat(int type) const;
void doFormat (int type);
// --------------------------------------------------- : ValueViewers
protected:
/// Create an editor for the given style (as opposed to a normal viewer)
virtual ValueViewerP makeViewer(const StyleP&);
// --------------------------------------------------- : Data
private:
DECLARE_EVENT_TABLE();
ValueViewer* current_viewer; ///< The currently selected viewer
ValueEditor* current_editor; ///< The currently selected editor, corresponding to the viewer
// --------------------------------------------------- : Events
void onLeftDown (wxMouseEvent&);
void onLeftUp (wxMouseEvent&);
void onLeftDClick(wxMouseEvent&);
void onRightDown (wxMouseEvent&);
void onMotion (wxMouseEvent&);
void onMouseWheel(wxMouseEvent&);
void onMouseLeave(wxMouseEvent&);
void onChar (wxKeyEvent&);
void onContextMenu(wxContextMenuEvent&);
void onMenu (wxCommandEvent& e);
void onFocus (wxFocusEvent&);
void onLoseFocus(wxFocusEvent&);
// --------------------------------------------------- : Functions
/// Changes the selection to the field at the specified coordinates
/** Sends an event to the event function of the current viewer */
void selectField(wxMouseEvent& ev, void (ValueEditor::*event)(const RealPoint&, wxMouseEvent&));
// selectField, but don't send events
void selectFieldNoEvents(const RealPoint& pos);
/// Convert mouse coordinates to internal coordinates
RealPoint mousePoint(const wxMouseEvent& e);
};
/// By default a DataEditor edits cards
+8 -8
View File
@@ -39,21 +39,21 @@ class ValueEditor {
virtual void onLoseFocus() {}
/// Handle mouse events
virtual void onMouseLeftDown (RealPoint pos, wxMouseEvent& ev) {}
virtual void onMouseLeftUp (RealPoint pos, wxMouseEvent& ev) {}
virtual void onMouseDClick (RealPoint pos, wxMouseEvent& ev) {}
virtual void onMouseRightDown(RealPoint pos, wxMouseEvent& ev) {}
virtual void onMouseMove (RealPoint pos, wxMouseEvent& ev) {}
virtual void onMouseWheel (RealPoint pos, wxMouseEvent& ev) {}
virtual void onLeftDown (const RealPoint& pos, wxMouseEvent& ev) {}
virtual void onLeftUp (const RealPoint& pos, wxMouseEvent& ev) {}
virtual void onLeftDClick (const RealPoint& pos, wxMouseEvent& ev) {}
virtual void onRightDown (const RealPoint& pos, wxMouseEvent& ev) {}
virtual void onMotion (const RealPoint& pos, wxMouseEvent& ev) {}
virtual void onMouseWheel (const RealPoint& pos, wxMouseEvent& ev) {}
/// Key events
virtual void onChar(wxKeyEvent ev) {}
/// A menu item was selected
virtual void onMenu(wxCommandEvent& ev) { ev.Skip(); }
/// a context menu is requested, add extra items to the menu m
/** return false to suppress menu */
virtual bool onContextMenu(wxMenu& m, wxContextMenuEvent& ev) { return true; }
/// A menu item was selected
virtual void onMenu(wxCommandEvent& ev) { ev.Skip(); }
// --------------------------------------------------- : Clipboard