| 1 |
#include "grid_wrapper.h" |
| 2 |
#include "allocator_mgr.h" |
| 3 |
#include "bool_property.h" |
| 4 |
#include "choice_property.h" |
| 5 |
#include "grid_column_wrapper.h" |
| 6 |
#include "grid_row_wrapper.h" |
| 7 |
#include "multi_strings_property.h" |
| 8 |
#include "wxgui_defs.h" |
| 9 |
#include "wxgui_helpers.h" |
| 10 |
|
| 11 |
GridWrapper::GridWrapper() |
| 12 |
: wxcWidget(ID_WXGRID) |
| 13 |
{ |
| 14 |
EnableStyle(wxT("wxWANTS_CHARS"), true); |
| 15 |
|
| 16 |
wxArrayString vOpts, hOpts; |
| 17 |
vOpts.Add("wxALIGN_TOP"); |
| 18 |
vOpts.Add("wxALIGN_CENTRE"); |
| 19 |
vOpts.Add("wxALIGN_BOTTOM"); |
| 20 |
|
| 21 |
hOpts.Add("wxALIGN_LEFT"); |
| 22 |
hOpts.Add("wxALIGN_CENTRE"); |
| 23 |
hOpts.Add("wxALIGN_RIGHT"); |
| 24 |
|
| 25 |
SetPropertyString(_("Common Settings"), "wxGrid"); |
| 26 |
AddProperty(new CategoryProperty(_("wxGrid Header"))); |
| 27 |
AddProperty(new BoolProperty(PROP_AUTOSIZE_COL, true, _("Auto size column content to fit the column header"))); |
| 28 |
AddProperty( |
| 29 |
new BoolProperty(PROP_GRID_NATIVE_LOOK, true, _("Enable the use of native header window for column labels"))); |
| 30 |
// AddProperty(new BoolProperty(PROP_GRID_NATIVE_COL_LABELS, true, _("Call this in order to make the column labels |
| 31 |
// use a native look"))); |
| 32 |
|
| 33 |
AddProperty(new CategoryProperty(_("wxGrid Columns Labels"))); |
| 34 |
AddInteger(PROP_HEIGHT, _("Sets the height of the column label"), -1); |
| 35 |
AddProperty(new ChoiceProperty(PROP_COL_LABEL_H_ALIGN, hOpts, 1, _("Sets the horizontal alignment of the label"))); |
| 36 |
AddProperty(new ChoiceProperty(PROP_COL_LABEL_V_ALIGN, vOpts, 1, _("Sets the vertical alignment of the label"))); |
| 37 |
|
| 38 |
AddProperty(new CategoryProperty(_("wxGrid Rows Labels"))); |
| 39 |
AddInteger(PROP_WIDTH, _("Sets the width of the row labels"), -1); |
| 40 |
AddProperty(new ChoiceProperty(PROP_ROW_LABEL_H_ALIGN, hOpts, 2, _("Sets the horizontal alignment of the label"))); |
| 41 |
AddProperty(new ChoiceProperty(PROP_ROW_LABEL_V_ALIGN, vOpts, 1, _("Sets the vertical alignment of the label"))); |
| 42 |
|
| 43 |
AddProperty(new CategoryProperty(_("wxGrid Cells"))); |
| 44 |
AddProperty(new BoolProperty(PROP_ALLOW_EDITING, true, _("Allow editing grid content"))); |
| 45 |
|
| 46 |
RegisterEvent(wxT("wxEVT_GRID_CELL_CHANGING"), wxT("wxGridEvent"), |
| 47 |
_("The user is about to change the data in a cell. The new cell value as string is available from " |
| 48 |
"GetString() event object method. This event can be vetoed if the change is not allowed. " |
| 49 |
"Processes a wxEVT_GRID_CELL_CHANGING event type")); |
| 50 |
RegisterEvent(wxT("wxEVT_GRID_CELL_CHANGED"), wxT("wxGridEvent"), |
| 51 |
_("The user changed the data in a cell. The old cell value as string is available from GetString() " |
| 52 |
"event object method. Notice that vetoing this event still works for backwards compatibility " |
| 53 |
"reasons but any new code should only veto EVT_GRID_CELL_CHANGING event and not this one. " |
| 54 |
"Processes a wxEVT_GRID_CELL_CHANGED event type.")); |
| 55 |
RegisterEvent(wxT("wxEVT_GRID_CELL_LEFT_CLICK"), wxT("wxGridEvent"), |
| 56 |
_("The user clicked a cell with the left mouse button. Processes a wxEVT_GRID_CELL_LEFT_CLICK.")); |
| 57 |
RegisterEvent( |
| 58 |
wxT("wxEVT_GRID_CELL_LEFT_DCLICK"), wxT("wxGridEvent"), |
| 59 |
_("The user double-clicked a cell with the left mouse button. Processes a wxEVT_GRID_CELL_LEFT_DCLICK")); |
| 60 |
RegisterEvent(wxT("wxEVT_GRID_CELL_RIGHT_CLICK"), wxT("wxGridEvent"), |
| 61 |
_("The user clicked a cell with the right mouse button. Processes a wxEVT_GRID_CELL_RIGHT_CLICK.")); |
| 62 |
RegisterEvent( |
| 63 |
wxT("wxEVT_GRID_CELL_RIGHT_DCLICK"), wxT("wxGridEvent"), |
| 64 |
_("The user double-clicked a cell with the right mouse button. Processes a wxEVT_GRID_CELL_RIGHT_DCLICK.")); |
| 65 |
RegisterEvent(wxT("wxEVT_GRID_EDITOR_HIDDEN"), wxT("wxGridEvent"), |
| 66 |
_("The editor for a cell was hidden. Processes a wxEVT_GRID_EDITOR_HIDDEN event type.")); |
| 67 |
RegisterEvent(wxT("wxEVT_GRID_EDITOR_SHOWN"), wxT("wxGridEvent"), |
| 68 |
_("The editor for a cell was shown. Processes a wxEVT_GRID_EDITOR_SHOWN event type")); |
| 69 |
RegisterEvent(wxT("wxEVT_GRID_LABEL_LEFT_CLICK"), wxT("wxGridEvent"), |
| 70 |
_("The user clicked a label with the left mouse button. Processes a wxEVT_GRID_LABEL_LEFT_CLICK.")); |
| 71 |
RegisterEvent( |
| 72 |
wxT("wxEVT_GRID_LABEL_LEFT_DCLICK"), wxT("wxGridEvent"), |
| 73 |
_("The user double-clicked a label with the left mouse button. Processes a wxEVT_GRID_LABEL_LEFT_DCLICK.")); |
| 74 |
RegisterEvent( |
| 75 |
wxT("wxEVT_GRID_LABEL_RIGHT_CLICK"), wxT("wxGridEvent"), |
| 76 |
_("The user clicked a label with the right mouse button. Processes a wxEVT_GRID_LABEL_RIGHT_CLICK.")); |
| 77 |
RegisterEvent( |
| 78 |
wxT("wxEVT_GRID_LABEL_RIGHT_DCLICK"), wxT("wxGridEvent"), |
| 79 |
_("The user double-clicked a label with the right mouse button. Processes a wxEVT_GRID_LABEL_RIGHT_DCLICK.")); |
| 80 |
RegisterEvent(wxT("wxEVT_GRID_SELECT_CELL"), wxT("wxGridEvent"), |
| 81 |
_("The user moved to, and selected a cell. Processes a wxEVT_GRID_SELECT_CELL.")); |
| 82 |
RegisterEvent( |
| 83 |
wxT("wxEVT_GRID_COL_MOVE"), wxT("wxGridEvent"), |
| 84 |
_("The user tries to change the order of the columns in the grid by dragging the column specified by GetCol(). " |
| 85 |
"This event can be vetoed to either prevent the user from reordering the column change completely (but notice " |
| 86 |
"that if you don't want to allow it at all, you simply shouldn't call wxGrid::EnableDragColMove() in the first " |
| 87 |
"place), " |
| 88 |
"vetoed but handled in some way in the handler, e.g. by really moving the column to the new position at the " |
| 89 |
"associated table level, " |
| 90 |
"or allowed to proceed in which case wxGrid::SetColPos() is used to reorder the columns display order without " |
| 91 |
"affecting the use of " |
| 92 |
"the column indices otherwise. This event macro corresponds to wxEVT_GRID_COL_MOVE event type.")); |
| 93 |
RegisterEvent(wxT("wxEVT_GRID_COL_SORT"), wxT("wxGridEvent"), |
| 94 |
_("This event is generated when a column is clicked by the user and its name is explained by the fact " |
| 95 |
"that the custom reaction to a " |
| 96 |
"click on a column is to sort the grid contents by this column. However the grid itself has no " |
| 97 |
"special support for sorting and it's " |
| 98 |
"up to the handler of this event to update the associated table. But if the event is handled (and " |
| 99 |
"not vetoed) the grid supposes that " |
| 100 |
"the table was indeed resorted and updates the column to indicate the new sort order and refreshes " |
| 101 |
"itself. This event macro corresponds " |
| 102 |
"to wxEVT_GRID_COL_SORT event type.")); |
| 103 |
RegisterEvent(wxT("wxEVT_GRID_TABBING"), wxT("wxGridEvent"), |
| 104 |
_("This event is generated when the user presses TAB or Shift-TAB in the grid. It can be used to " |
| 105 |
"customize the simple default TAB handling " |
| 106 |
"logic, e.g. to go to the next non-empty cell instead of just the next cell. See also " |
| 107 |
"wxGrid::SetTabBehaviour()." |
| 108 |
" This event is new since wxWidgets 2.9.5.")); |
| 109 |
m_namePattern = wxT("m_grid"); |
| 110 |
SetName(GenerateName()); |
| 111 |
} |
| 112 |
|
| 113 |
GridWrapper::~GridWrapper() {} |
| 114 |
|
| 115 |
wxcWidget* GridWrapper::Clone() const { return new GridWrapper(); } |
| 116 |
|
| 117 |
wxString GridWrapper::CppCtorCode() const |
| 118 |
{ |
| 119 |
wxString cppCode, cppRowColCode; |
| 120 |
cppCode << CPPStandardWxCtor(wxT("wxWANTS_CHARS")); |
| 121 |
|
| 122 |
// We need to count the number of children of type Col/Row we have |
| 123 |
GridColumnWrapper::Vector_t cols; |
| 124 |
GridRowWrapper::Vector_t rows; |
| 125 |
|
| 126 |
GetRowsCols(rows, cols); |
| 127 |
|
| 128 |
for(size_t i = 0; i < rows.size(); ++i) { |
| 129 |
cppRowColCode << rows.at(i)->Code(i); |
| 130 |
} |
| 131 |
|
| 132 |
for(size_t i = 0; i < cols.size(); ++i) { |
| 133 |
cppRowColCode << cols.at(i)->Code(i); |
| 134 |
} |
| 135 |
|
| 136 |
cppCode << GetName() << wxT("->CreateGrid(" << rows.size() << ", ") << cols.size() << wxT(");\n"); |
| 137 |
cppCode << cppRowColCode; |
| 138 |
|
| 139 |
// set the aligment |
| 140 |
cppCode << GetName() << "->SetRowLabelAlignment(" << PropertyString(PROP_ROW_LABEL_H_ALIGN) << ", " |
| 141 |
<< PropertyString(PROP_ROW_LABEL_V_ALIGN) << ");\n"; |
| 142 |
cppCode << GetName() << "->SetColLabelAlignment(" << PropertyString(PROP_COL_LABEL_H_ALIGN) << ", " |
| 143 |
<< PropertyString(PROP_COL_LABEL_V_ALIGN) << ");\n"; |
| 144 |
cppCode << wxCrafter::WX294_BLOCK_START(); |
| 145 |
cppCode << GetName() << "->UseNativeColHeader(" << PropertyBool(PROP_GRID_NATIVE_LOOK) << ");\n"; |
| 146 |
cppCode << wxCrafter::WXVER_CHECK_BLOCK_END(); |
| 147 |
cppCode << GetName() << wxT("->EnableEditing(") << PropertyBool(PROP_ALLOW_EDITING) << ");\n"; |
| 148 |
|
| 149 |
int colHeight = PropertyInt(PROP_HEIGHT, -1); |
| 150 |
if(colHeight != -1) { cppCode << GetName() << "->SetColLabelSize(" << colHeight << ");\n"; } |
| 151 |
|
| 152 |
int rowLabelWidth = PropertyInt(PROP_WIDTH, -1); |
| 153 |
if(rowLabelWidth != -1) { cppCode << GetName() << "->SetRowLabelSize(" << rowLabelWidth << ");\n"; } |
| 154 |
return cppCode; |
| 155 |
} |
| 156 |
|
| 157 |
void GridWrapper::GetIncludeFile(wxArrayString& headers) const { headers.Add(wxT("#include <wx/grid.h>")); } |
| 158 |
|
| 159 |
wxString GridWrapper::GetWxClassName() const { return wxT("wxGrid"); } |
| 160 |
|
| 161 |
void GridWrapper::ToXRC(wxString& text, XRC_TYPE type) const |
| 162 |
{ |
| 163 |
if(type == XRC_LIVE) { |
| 164 |
// Native wx do not support what we do... |
| 165 |
text << XRCPrefix() << XRCSize() << XRCStyle() << XRCCommonAttributes() << XRCSuffix(); |
| 166 |
|
| 167 |
} else { |
| 168 |
|
| 169 |
GridColumnWrapper::Vector_t cols; |
| 170 |
GridRowWrapper::Vector_t rows; |
| 171 |
GetRowsCols(rows, cols); |
| 172 |
|
| 173 |
// wxBORDER_THEME seems to be breaking the preview when wxGrid is placed inside |
| 174 |
// a wxDialog. So we disable it for the preview only |
| 175 |
#ifdef __WXGTK__ |
| 176 |
wxString style = StyleFlags(); |
| 177 |
|
| 178 |
wxArrayString styleArr = wxCrafter::Split(style, "|"); |
| 179 |
if(styleArr.Index("wxBORDER_THEME") != wxNOT_FOUND) { styleArr.Remove("wxBORDER_THEME"); } |
| 180 |
style = wxCrafter::Join(styleArr, "|"); |
| 181 |
style.Prepend("<style>").Append("</style>"); |
| 182 |
#else |
| 183 |
wxString style = XRCStyle(); |
| 184 |
#endif |
| 185 |
|
| 186 |
text << XRCPrefix() << XRCSize() << style << XRCCommonAttributes(); |
| 187 |
text << wxT("<autosizecol>") << PropertyString(PROP_AUTOSIZE_COL) << wxT("</autosizecol>"); |
| 188 |
text << wxT("<editable>") << PropertyString(PROP_ALLOW_EDITING) << wxT("</editable>"); |
| 189 |
|
| 190 |
// Columns |
| 191 |
text << wxT("<columns>"); |
| 192 |
for(size_t i = 0; i < cols.size(); ++i) { |
| 193 |
cols.at(i)->ToXRC(text, type); |
| 194 |
} |
| 195 |
text << wxT("</columns>"); |
| 196 |
|
| 197 |
// Rows |
| 198 |
text << wxT("<rows>"); |
| 199 |
for(size_t i = 0; i < rows.size(); ++i) { |
| 200 |
rows.at(i)->ToXRC(text, type); |
| 201 |
} |
| 202 |
text << wxT("</rows>"); |
| 203 |
|
| 204 |
// alignment |
| 205 |
text << "<row-label-v-alignment>" << PropertyString(PROP_ROW_LABEL_V_ALIGN) << "</row-label-v-alignment>"; |
| 206 |
text << "<row-label-h-alignment>" << PropertyString(PROP_ROW_LABEL_H_ALIGN) << "</row-label-h-alignment>"; |
| 207 |
|
| 208 |
text << "<col-label-v-alignment>" << PropertyString(PROP_COL_LABEL_V_ALIGN) << "</col-label-v-alignment>"; |
| 209 |
text << "<col-label-h-alignment>" << PropertyString(PROP_COL_LABEL_H_ALIGN) << "</col-label-h-alignment>"; |
| 210 |
text << "<col-label-size>" << PropertyInt(PROP_HEIGHT) << "</col-label-size>"; |
| 211 |
text << "<row-label-size>" << PropertyInt(PROP_WIDTH) << "</row-label-size>"; |
| 212 |
|
| 213 |
text << "<native-header>" << PropertyString(PROP_GRID_NATIVE_LOOK) << "</native-header>"; |
| 214 |
// text << "<native-col-labels>" << PropertyString(PROP_GRID_NATIVE_COL_LABELS) << "</native-col-labels>"; |
| 215 |
text << XRCSuffix(); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
void GridWrapper::UnSerialize(const JSONElement& json) |
| 220 |
{ |
| 221 |
m_sizerItem.SetBorder(json.namedObject(wxT("border")).toInt(5)); |
| 222 |
m_sizerItem.SetProportion(json.namedObject(wxT("proportion")).toInt(0)); |
| 223 |
m_gbSpan = json.namedObject(wxT("gbSpan")).toString(); |
| 224 |
m_gbPos = json.namedObject(wxT("gbPosition")).toString(); |
| 225 |
|
| 226 |
m_auiPaneInfo.FromJSON(json.namedObject("wxAuiPaneInfo")); |
| 227 |
|
| 228 |
// Unserialize the styles |
| 229 |
DoClearFlags(m_styles); |
| 230 |
DoClearFlags(m_sizerFlags); |
| 231 |
|
| 232 |
m_connectedEvents.Clear(); |
| 233 |
|
| 234 |
JSONElement styles = json.namedObject(wxT("m_styles")); |
| 235 |
int nCount = styles.arraySize(); |
| 236 |
for(int i = 0; i < nCount; i++) { |
| 237 |
wxString styleName = styles.arrayItem(i).toString(); |
| 238 |
EnableStyle(styleName, true); |
| 239 |
} |
| 240 |
|
| 241 |
JSONElement sizerFlags = json.namedObject(wxT("m_sizerFlags")); |
| 242 |
nCount = sizerFlags.arraySize(); |
| 243 |
for(int i = 0; i < nCount; i++) { |
| 244 |
wxString styleName = sizerFlags.arrayItem(i).toString(); |
| 245 |
EnableSizerFlag(styleName, true); |
| 246 |
} |
| 247 |
|
| 248 |
// Unserialize the properties |
| 249 |
JSONElement properties = json.namedObject(wxT("m_properties")); |
| 250 |
nCount = properties.arraySize(); |
| 251 |
|
| 252 |
for(int i = 0; i < nCount; i++) { |
| 253 |
JSONElement jsonProp = properties.arrayItem(i); |
| 254 |
wxString propLabel = jsonProp.namedObject(wxT("m_label")).toString(); |
| 255 |
|
| 256 |
if(propLabel == PROP_COLS_LIST) { |
| 257 |
wxString cols_string = jsonProp.namedObject(wxT("m_value")).toString(); |
| 258 |
wxArrayString colsArr = wxCrafter::Split(cols_string, ";"); |
| 259 |
|
| 260 |
for(size_t i = 0; i < colsArr.GetCount(); ++i) { |
| 261 |
// The old way of creating rows - create new children and add them here |
| 262 |
wxcWidget* col = Allocator::Instance()->Create(ID_WXGRIDCOL); |
| 263 |
col->SetName(colsArr.Item(i)); |
| 264 |
AddChild(col); |
| 265 |
} |
| 266 |
|
| 267 |
} else if(propLabel == PROP_ROWS_LIST) { |
| 268 |
wxString rows_string = jsonProp.namedObject(wxT("m_value")).toString(); |
| 269 |
wxArrayString rowsArr = wxCrafter::Split(rows_string, ";"); |
| 270 |
|
| 271 |
for(size_t i = 0; i < rowsArr.GetCount(); ++i) { |
| 272 |
// The old way of creating rows - create new children and add them here |
| 273 |
wxcWidget* row = Allocator::Instance()->Create(ID_WXGRIDROW); |
| 274 |
row->SetName(rowsArr.Item(i)); |
| 275 |
AddChild(row); |
| 276 |
} |
| 277 |
|
| 278 |
} else if(m_properties.Contains(propLabel)) { |
| 279 |
m_properties.Item(propLabel)->UnSerialize(jsonProp); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
// Unserialize the events |
| 284 |
JSONElement events = json.namedObject(wxT("m_events")); |
| 285 |
nCount = events.arraySize(); |
| 286 |
for(int i = 0; i < nCount; i++) { |
| 287 |
JSONElement jsonEvent = events.arrayItem(i); |
| 288 |
ConnectDetails details; |
| 289 |
details.FromJSON(jsonEvent); |
| 290 |
m_connectedEvents.PushBack(details.GetEventName(), details); |
| 291 |
} |
| 292 |
|
| 293 |
JSONElement children = json.namedObject(wxT("m_children")); |
| 294 |
int nChildren = children.arraySize(); |
| 295 |
for(int i = 0; i < nChildren; i++) { |
| 296 |
JSONElement child = children.arrayItem(i); |
| 297 |
wxcWidget* wrapper = Allocator::Instance()->CreateWrapperFromJSON(child); |
| 298 |
if(wrapper) { AddChild(wrapper); } |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
void GridWrapper::GetRowsCols(GridRowWrapper::Vector_t& rows, GridColumnWrapper::Vector_t& cols) const |
| 303 |
{ |
| 304 |
wxcWidget::List_t::const_iterator iter = m_children.begin(); |
| 305 |
for(; iter != m_children.end(); ++iter) { |
| 306 |
if((*iter)->GetType() == ID_WXGRIDCOL) { |
| 307 |
cols.push_back(static_cast<GridColumnWrapper*>((*iter))); |
| 308 |
} else { |
| 309 |
rows.push_back(static_cast<GridRowWrapper*>((*iter))); |
| 310 |
} |
| 311 |
} |
| 312 |
} |