From 6529f6df89ef67a14c5f7de18c872b8200895baa Mon Sep 17 00:00:00 2001 From: twanvl Date: Mon, 18 Dec 2006 21:43:28 +0000 Subject: [PATCH] Card list for selecting cards; window using that list; print function using that window (actual printing todo) git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@116 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/gui/card_select_window.cpp | 52 +++++++++++ src/gui/card_select_window.hpp | 41 +++++++++ src/gui/control/select_card_list.cpp | 93 ++++++++++++++++++- src/gui/control/select_card_list.hpp | 3 +- src/gui/print_window.cpp | 132 +++++++++++++++++++++++++++ src/gui/print_window.hpp | 25 +++++ 6 files changed, 344 insertions(+), 2 deletions(-) create mode 100644 src/gui/card_select_window.cpp create mode 100644 src/gui/card_select_window.hpp create mode 100644 src/gui/print_window.cpp create mode 100644 src/gui/print_window.hpp diff --git a/src/gui/card_select_window.cpp b/src/gui/card_select_window.cpp new file mode 100644 index 00000000..717aadf6 --- /dev/null +++ b/src/gui/card_select_window.cpp @@ -0,0 +1,52 @@ +//+----------------------------------------------------------------------------+ +//| Description: Magic Set Editor - Program to make Magic (tm) cards | +//| Copyright: (C) 2001 - 2006 Twan van Laarhoven | +//| License: GNU General Public License 2 or later (see file COPYING) | +//+----------------------------------------------------------------------------+ + +// ----------------------------------------------------------------------------- : Includes + +#include +#include +#include + +// ----------------------------------------------------------------------------- : CardSelectWindow + +CardSelectWindow::CardSelectWindow(Window* parent, const SetP& set, const String& label) + : wxDialog(parent, wxID_ANY, _TITLE_("select cards"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) + , set(set) +{ + // init controls + list = new SelectCardList(this, wxID_ANY); + list->setSet(set); + wxButton* sel_all = new wxButton(this, ID_SELECT_ALL, _BUTTON_("select all")); + wxButton* sel_none = new wxButton(this, ID_SELECT_NONE, _BUTTON_("select none")); + // init sizers + wxSizer* s = new wxBoxSizer(wxVERTICAL); + s->Add(new wxStaticText(this, wxID_ANY, label), 0, wxALL & ~wxBOTTOM, 8); + s->Add(list, 1, wxEXPAND | wxALL, 8); + wxSizer* s2 = new wxBoxSizer(wxHORIZONTAL); + s2->Add(sel_all, 0, wxEXPAND | wxRIGHT, 8); + s2->Add(sel_none, 0, wxEXPAND | wxRIGHT, 8); + s2->Add(CreateButtonSizer(wxOK | wxCANCEL), 1, wxEXPAND, 8); + s->Add(s2, 0, wxEXPAND | wxALL & ~wxTOP, 8); + s->SetSizeHints(this); + SetSizer(s); + SetSize(500,500); +} + +bool CardSelectWindow::isSelected(const CardP& card) const { + return list->isSelected(card); +} + +void CardSelectWindow::onSelectAll(wxCommandEvent&) { + list->selectAll(); +} +void CardSelectWindow::onSelectNone(wxCommandEvent&) { + list->selectNone(); +} + +BEGIN_EVENT_TABLE(CardSelectWindow,wxDialog) + EVT_BUTTON (ID_SELECT_ALL, CardSelectWindow::onSelectAll) + EVT_BUTTON (ID_SELECT_NONE, CardSelectWindow::onSelectNone) +END_EVENT_TABLE () diff --git a/src/gui/card_select_window.hpp b/src/gui/card_select_window.hpp new file mode 100644 index 00000000..9d766ae4 --- /dev/null +++ b/src/gui/card_select_window.hpp @@ -0,0 +1,41 @@ +//+----------------------------------------------------------------------------+ +//| Description: Magic Set Editor - Program to make Magic (tm) cards | +//| Copyright: (C) 2001 - 2006 Twan van Laarhoven | +//| License: GNU General Public License 2 or later (see file COPYING) | +//+----------------------------------------------------------------------------+ + +#ifndef HEADER_GUI_CARD_SELECT_WINDOW +#define HEADER_GUI_CARD_SELECT_WINDOW + +// ----------------------------------------------------------------------------- : Includes + +#include + +DECLARE_POINTER_TYPE(Set); +DECLARE_POINTER_TYPE(Card); +class SelectCardList; + +// ----------------------------------------------------------------------------- : CardSelectWindow + +/// A window for selecting a subset of the cards from a set. +/** this is used when printing or exporting + */ +class CardSelectWindow : public wxDialog { + public: + CardSelectWindow(Window* parent, const SetP& set, const String& label); + + /// Is the given card selected? + bool isSelected(const CardP& card) const; + + protected: + DECLARE_EVENT_TABLE(); + + SelectCardList* list; + SetP set; + + void onSelectAll (wxCommandEvent&); + void onSelectNone(wxCommandEvent&); +}; + +// ----------------------------------------------------------------------------- : EOF +#endif diff --git a/src/gui/control/select_card_list.cpp b/src/gui/control/select_card_list.cpp index 3faa12d3..2c5b350a 100644 --- a/src/gui/control/select_card_list.cpp +++ b/src/gui/control/select_card_list.cpp @@ -7,7 +7,98 @@ // ----------------------------------------------------------------------------- : Includes #include +#include + +DECLARE_TYPEOF_COLLECTION(CardP); // ----------------------------------------------------------------------------- : SelectCardList -// TODO +SelectCardList::SelectCardList(Window* parent, int id, long additional_style) + : CardListBase(parent, id, additional_style) +{ + // create image list + wxImageList* il = new wxImageList(15,15); + il->Add(Bitmap(_("SORT_ASC")), Color(255,0,255)); + il->Add(Bitmap(_("SORT_DESC")), Color(255,0,255)); + il->Add(load_resource_image(_("DESELECTED"))); + il->Add(load_resource_image(_("SELECTED"))); + AssignImageList(il, wxIMAGE_LIST_SMALL); +} + +void SelectCardList::selectAll() { + FOR_EACH_CONST(c, getCards()) { + selected.insert(c); + } + Refresh(false); +} +void SelectCardList::selectNone() { + selected.clear(); + Refresh(false); +} +bool SelectCardList::isSelected(const CardP& card) const { + return selected.find(card) != selected.end(); +} + +void SelectCardList::onChangeSet() { + CardListBase::onChangeSet(); + // init selected list: select all + selected.clear(); + selectAll(); +} + +int SelectCardList::OnGetItemImage(long pos) const { + return isSelected(getCard(pos)) ? 3 : 2; +} + +// ----------------------------------------------------------------------------- : Events + +void SelectCardList::toggle(const CardP& card) { + if (isSelected(card)) { + selected.erase(card); + } else { + selected.insert(card); + } +} + +void SelectCardList::onKeyDown(wxKeyEvent& ev) { + if (selected_card_pos == -1 || !selected_card) { + // no selection + ev.Skip(); + return; + } + switch (ev.GetKeyCode()) { + case WXK_SPACE: { + toggle(selected_card); + RefreshItem(selected_card_pos); + break; + } + case WXK_NUMPAD_ADD: case '+': { + selected.insert(selected_card); + RefreshItem(selected_card_pos); + break; + } + case WXK_NUMPAD_SUBTRACT: case '-': { + selected.erase(selected_card); + RefreshItem(selected_card_pos); + break; + } + default: + ev.Skip(); + } +} + +void SelectCardList::onLeftDown(wxMouseEvent& ev) { + int flags; + long item = HitTest(wxPoint(ev.GetX(), ev.GetY()), flags); + if (flags == wxLIST_HITTEST_ONITEMICON) { + // only clicking the icon toggles + toggle(getCard(item)); + RefreshItem(item); + } + ev.Skip(); +} + +BEGIN_EVENT_TABLE(SelectCardList, CardListBase) + EVT_KEY_DOWN (SelectCardList::onKeyDown) + EVT_LEFT_DOWN (SelectCardList::onLeftDown) +END_EVENT_TABLE () diff --git a/src/gui/control/select_card_list.hpp b/src/gui/control/select_card_list.hpp index 7bed954a..80a85e6d 100644 --- a/src/gui/control/select_card_list.hpp +++ b/src/gui/control/select_card_list.hpp @@ -25,7 +25,8 @@ class SelectCardList : public CardListBase { /// Is the given card selected? bool isSelected(const CardP& card) const; protected: - int OnGetItemImage(long pos) const; + virtual int OnGetItemImage(long pos) const; + virtual void onChangeSet(); private: DECLARE_EVENT_TABLE(); diff --git a/src/gui/print_window.cpp b/src/gui/print_window.cpp new file mode 100644 index 00000000..86c1b2fb --- /dev/null +++ b/src/gui/print_window.cpp @@ -0,0 +1,132 @@ +//+----------------------------------------------------------------------------+ +//| Description: Magic Set Editor - Program to make Magic (tm) cards | +//| Copyright: (C) 2001 - 2006 Twan van Laarhoven | +//| License: GNU General Public License 2 or later (see file COPYING) | +//+----------------------------------------------------------------------------+ + +// ----------------------------------------------------------------------------- : Includes + +#include +#include +#include +#include + +DECLARE_TYPEOF_COLLECTION(CardP); + +// ----------------------------------------------------------------------------- : Buffering DC + +/// MemoryDC that buffers calls to write text +/** The printer device doesn't support alpha channels (at least not in wxMSW) + * This would result in black backgrounds where symbols should be transparent + * Our solution is: + * 1. Write all bitmaps to a buffer DC, initially white + * 2. When drawing with alpha: blend with the buffer + * 3. When drawing text: buffer the call + * 4. Draw the buffer image to the device + * 5. Replay the buffered text draw calls + * To simplify things this class itself is a fullblown DC, only text calls are buffered for later + * Actually buffering text separatly would not be necessary at all, but if we don't the text will be + * printed in a low resolution. + */ +class TextBufferDC : public wxMemoryDC { + public: + TextBufferDC(UInt width, UInt height); + + virtual void DoDrawText(const String& str, int x, int y); + virtual void DoDrawRotatedText(const String& str, int x, int y, double angle); + + /// Copy the contents of the DC to a target device, this DC becomes invalid + void drawToDevice(DC& dc, int x = 0, int y = 0); + + private: + // A call to DrawText + struct TextDraw { + wxFont font; + Color color; + int x, y; + String text; + double angle; + + TextDraw(wxFont font, Color color, int x, int y, String text, double angle = 0) + : font(font), color(color), x(x), y(y), text(text), angle(angle) + {} + }; + public: + typedef shared_ptr TextDrawP; + private: + vector text; + Bitmap buffer; +}; + +TextBufferDC::TextBufferDC(UInt width, UInt height) + : buffer(width, height, 32) +{ + SelectObject(buffer); + // initialize to white + clearDC(*this,*wxWHITE_BRUSH); +} +void TextBufferDC::DoDrawText(const String& str, int x, int y) { + text.push_back( new_shared5(GetFont(), GetTextForeground(), x, y, str) ); +} +void TextBufferDC::DoDrawRotatedText(const String& str, int x, int y, double angle) { + text.push_back( new_shared6(GetFont(), GetTextForeground(), x, y, str, angle) ); +} + +DECLARE_TYPEOF_COLLECTION(TextBufferDC::TextDrawP); + +void TextBufferDC::drawToDevice(DC& dc, int x, int y) { + SelectObject(wxNullBitmap); + dc.DrawBitmap(buffer, x, y); + FOR_EACH(t, text) { + dc.SetFont (t->font); + dc.SetTextForeground(t->color); + if (t->angle) { + dc.DrawRotatedText(t->text, t->x + x, t->y + y, t->angle); + } else { + dc.DrawText(t->text, t->x + x, t->y + y); + } + } +} + +// ----------------------------------------------------------------------------- : Printout + +// ----------------------------------------------------------------------------- : PrintWindow + +void print_preview(Window* parent, const SetP& set) { + // Let the user choose cards + CardSelectWindow wnd(parent, set, _LABEL_("select cards print")); + if (wnd.ShowModal() != wxID_OK) { + return; // cancel + } + vector selected; + FOR_EACH(c, set->cards) { + if (wnd.isSelected(c)) selected.push_back(c); + } +/* // Show the print preview + wxPreviewFrame frame = new wxPreviewFrame( + new wxPrintPreview( + new CardsPrintout(set, selected), + new CardsPrintout(set, selected) + ), parent, _TITLE_("print preview")); + frame->Initialize(); + frame->Maximize(true); + frame->Show(); +*/ +} + +void print_set(Window* parent, const SetP& set) { + // Let the user choose cards + CardSelectWindow wnd(parent, set, _LABEL_("select cards print")); + if (wnd.ShowModal() != wxID_OK) { + return; // cancel + } + vector selected; + FOR_EACH(c, set->cards) { + if (wnd.isSelected(c)) selected.push_back(c); + } +/* // Print the cards + wxPrinter p; + CardsPrintout pout(set, selected); + p.Print(parent, &pout, true); +*/ +} diff --git a/src/gui/print_window.hpp b/src/gui/print_window.hpp new file mode 100644 index 00000000..ec61f44f --- /dev/null +++ b/src/gui/print_window.hpp @@ -0,0 +1,25 @@ +//+----------------------------------------------------------------------------+ +//| Description: Magic Set Editor - Program to make Magic (tm) cards | +//| Copyright: (C) 2001 - 2006 Twan van Laarhoven | +//| License: GNU General Public License 2 or later (see file COPYING) | +//+----------------------------------------------------------------------------+ + +#ifndef HEADER_GUI_PRINT_WINDOW +#define HEADER_GUI_PRINT_WINDOW + +// ----------------------------------------------------------------------------- : Includes + +#include + +DECLARE_POINTER_TYPE(Set); + +// ----------------------------------------------------------------------------- : Printing + +/// Show a print preview for the given set +void print_preview(Window* parent, const SetP& set); + +/// Print the given set +void print_set(Window* parent, const SetP& set); + +// ----------------------------------------------------------------------------- : EOF +#endif