00001 #include <gui/ProgressBar.h>
00002 #include <gui/MessageBox.h>
00003 #include <gui/MessageQueue.h>
00004 #include <gui/Button.h>
00005 #include <gui/ListView.h>
00006 #include <gui/TopView.h>
00007 #include <gui/Exceptions.h>
00008 #include <gui/Window.h>
00009 #include <gui/StringView.h>
00010 #include <gui/ApplicationWindow.h>
00011 #include <gui/Application.h>
00012 #include <gui/ToolTip.h>
00013 #include <gui/Menu.h>
00014 #include <gui/StatusBar.h>
00015 #include <gui/CheckBox.h>
00016 #include <iostream>
00017
00018 using namespace SkyGI;
00019
00020 enum
00021 {
00022 ID_0 = 20000,
00023 ID_10,
00024 ID_20,
00025 ID_40,
00026 ID_60,
00027 ID_80,
00028 ID_ADD_1000,
00029 ID_ADD_10000,
00030 ID_ADD_100000,
00031 ID_ADD_CHILD_ROWS,
00032 ID_HIDE,
00033 ID_SHOW
00034 };
00035
00036 static int j=0;
00037 static int iCount = 0;
00038 static int iColumn=1;
00039 class MyListView : public View
00040 {
00041 public:
00042 MyListView(Window *pParent, const Rect &rFrame, unsigned int nWindowLayoutFlags);
00043 void OnCommand(const MessageCommand *pMessage);
00044 ListView *pListView;
00045 void OnRename();
00046 void OnContextMenu(ListViewRow* pRow, const InputEvent& nInputEvent);
00047 void Add(int iItems, bool bAsChild = false);
00048 void OnHeaderClicked();
00049 void SetSize(int iSize);
00050 void EditConfirm(ListViewNode *pNode, TextView *pTextView);
00051 };
00052
00053 void MyListView::EditConfirm(ListViewNode *pNode, TextView *pTextView)
00054 {
00055 if (pTextView->GetBuffer()[0].szText.empty())
00056 return;
00057
00058 ((ListViewStringNode*)pNode)->SetText(pTextView->GetBuffer()[0].szText);
00059 pListView->InvalidateNode(pNode);
00060 }
00061
00062
00063 void MyListView::Add(int iItems, bool bAsChild)
00064 {
00065
00066 ListViewRow *pRow;
00067 ListViewNode *pNode;
00068
00069 Image *pImage = new Image("/boot/addon/icons/cdrom.ico");
00070
00071 for (int i=0;i<iItems;i++)
00072 {
00073 char str[255];
00074
00075 pRow = new ListViewRow();
00076 pRow->SetIcon(pImage);
00077 sprintf(str, "line %d", iCount);
00078
00079
00080 sprintf(str, "line %d column 0", iCount);
00081 pNode = new ListViewStringNode(str, (void*)iCount);
00082 pNode->SetEditable(true);
00083 pNode->EditConfirm.Connect(this, &MyListView::EditConfirm);
00084 pRow->Append(pNode);
00085
00086 if (j > 0)
00087 {
00088 sprintf(str, "line %d column 1", iCount);
00089 pNode = new ListViewStringNode(str, (void*)iCount);
00090 pNode->SetEditable(true);
00091 pNode->EditConfirm.Connect(this, &MyListView::EditConfirm);
00092 pRow->Append(pNode);
00093 }
00094 if (j > 1)
00095 {
00096 sprintf(str, "line %d column 2", iCount);
00097 pNode = new ListViewStringNode(str, (void*)iCount);
00098 pNode->SetEditable(true);
00099 pNode->EditConfirm.Connect(this, &MyListView::EditConfirm);
00100 pRow->Append(pNode);
00101 }
00102 if (j > 2)
00103 {
00104 sprintf(str, "line %d column 3", iCount);
00105 pNode = new ListViewStringNode(str, (void*)iCount);
00106 pNode->SetEditable(true);
00107 pNode->EditConfirm.Connect(this, &MyListView::EditConfirm);
00108 pRow->Append(pNode);
00109 }
00110 if (j > 3)
00111 {
00112 sprintf(str, "line %d column 4", iCount);
00113 pNode = new ListViewStringNode(str, (void*)iCount);
00114 pNode->SetEditable(true);
00115 pNode->EditConfirm.Connect(this, &MyListView::EditConfirm);
00116 pRow->Append(pNode);
00117 }
00118 j++;
00119 if (j == 5)
00120 j = 0;
00121
00122 if (bAsChild)
00123 {
00124 pListView->Append(pRow, pListView->GetSelected());
00125 pListView->GetSelected()->Collapse(false);
00126 pListView->Invalidate();
00127 }
00128 else
00129 pListView->Append(pRow);
00130
00131 iCount++;
00132 }
00133 }
00134
00135 void MyListView::SetSize(int iSize)
00136 {
00137 std::vector<ListViewRow*> pSelection;
00138
00139 pSelection = pListView->GetSelection();
00140
00141 for (int i=0;i<pSelection.size();i++)
00142 {
00143 printf("set size to %d\n", iSize);
00144
00145 pSelection[i]->SetHeight(iSize);
00146 }
00147 pListView->Refresh();
00148
00149 }
00150
00151 void MyListView::OnRename()
00152 {
00153 ListViewRow *pRow;
00154 ListViewNode *pNode;
00155
00156 pRow = pListView->GetSelected();
00157 if (!pRow)
00158 return;
00159
00160 pNode = pRow->GetNode(0);
00161 if (!pNode)
00162 return;
00163
00164 pNode->Edit();
00165 }
00166
00167 void MyListView::OnContextMenu(ListViewRow* _pRow, const InputEvent& nInputEvent)
00168 {
00169 ListViewRow *pRow;
00170
00171 Menu *pMenu = new Menu(MENU_LAYOUT_VERTICAL);
00172 Menu *pMenuSize = new Menu(MENU_LAYOUT_VERTICAL);
00173 Menu *pMenuAdd = new Menu(MENU_LAYOUT_VERTICAL);
00174 MenuItem *pMenuItem;
00175
00176 pMenuAdd->Add(new MenuItem("1000 items", true, ID_ADD_1000, NULL));
00177 pMenuAdd->Add(new MenuItem("10000 items", true, ID_ADD_10000, NULL));
00178 pMenuAdd->Add(new MenuItem("100000 items", true, ID_ADD_100000, NULL));
00179 pMenuAdd->Add(new MenuItem("10 child rows", true, ID_ADD_CHILD_ROWS, NULL));
00180
00181 pMenu->Add(new MenuItem("Hide", true, ID_HIDE, 0));
00182 pMenu->Add(new MenuItem("Show all", true, ID_SHOW, 0));
00183
00184 pMenuItem = new MenuItem("Add", true, 0, 0);
00185 pMenuItem->Add(pMenuAdd);
00186 pMenu->Add(pMenuItem);
00187
00188 pMenuSize->Add(new MenuItem("0px", true, ID_0, NULL));
00189 pMenuSize->Add(new MenuItem("10px", true, ID_10, NULL));
00190 pMenuSize->Add(new MenuItem("20px", true, ID_20, NULL));
00191 pMenuSize->Add(new MenuItem("40px", true, ID_40, NULL));
00192 pMenuSize->Add(new MenuItem("60px", true, ID_60, NULL));
00193 pMenuSize->Add(new MenuItem("80px", true, ID_80, NULL));
00194
00195 pMenuItem = new MenuItem("Size", true, 0, 0);
00196 pMenuItem->Add(pMenuSize);
00197 pMenu->Add(pMenuItem);
00198
00199 PopupMenu *pPopup = new PopupMenu(this, ConvertToScreen(nInputEvent.m_pPosition), pMenu, 0);
00200 pPopup->Show();
00201
00202
00203 }
00204 void MyListView::OnHeaderClicked()
00205 {
00206 pListView->ShowHeader(!pListView->HasHeader());
00207 }
00208
00209 void MyListView::OnCommand(const MessageCommand *pMessage)
00210 {
00211 ListViewRow *pRow;
00212 String s;
00213 char str[255];
00214 std::vector<ListViewRow*> v;
00215
00216 switch (pMessage->GetID())
00217 {
00218 case ID_0: SetSize(-1); break;
00219 case ID_10: SetSize(10); break;
00220 case ID_20: SetSize(20); break;
00221 case ID_40: SetSize(40); break;
00222 case ID_60: SetSize(60); break;
00223 case ID_80: SetSize(80); break;
00224 case ID_ADD_1000: Add(1000); break;
00225 case ID_ADD_10000: Add(10000); break;
00226 case ID_ADD_100000: Add(100000); break;
00227 case ID_ADD_CHILD_ROWS: Add(10, true);break;
00228 case ID_HIDE:
00229 {
00230 pRow = pListView->GetSelected();
00231 if (pRow)
00232 pRow->Show(false);
00233 pListView->Refresh();
00234 }
00235 break;
00236 case ID_SHOW:
00237 {
00238 int i = 0;
00239 pRow = pListView->GetFirst();
00240 while (pRow)
00241 {
00242 pRow->Show(true);
00243
00244 pRow = pRow->GetNext();
00245 if (pRow == pListView->GetFirst())
00246 break;
00247 }
00248
00249 pListView->Refresh();
00250 }
00251 break;
00252 }
00253
00254 if (pMessage->GetID() == 10000)
00255 {
00256 int iSelected[2];
00257
00258 iSelected[0] = pListView->GetRowCount();
00259 iSelected[1] = pListView->GetRowCount(true);
00260
00261 sprintf(str, "Rows: %d, Including child: %d\n",
00262 iSelected[0],iSelected[1]);
00263 s += str;
00264 pRow = pListView->GetSelected();
00265 if (pRow)
00266 {
00267 sprintf(str, "Single selection reports: %d\n", (int)pRow->GetNode(0)->GetCookie());
00268 s += str;
00269
00270 sprintf(str, "Columns:\n");
00271 s += str;
00272
00273 for (int i=0;i<pRow->GetNodes();i++)
00274 {
00275 s += ((ListViewStringNode*)pRow->GetNode(i))->GetText();
00276 s += "\n";
00277 }
00278
00279 }
00280 else
00281 {
00282 sprintf(str, "Single selection reports: <nothing selected>\n");
00283 s += str;
00284 }
00285
00286 sprintf(str, "Multiple selection reports: \n");
00287 s += str;
00288
00289 v = pListView->GetSelection();
00290
00291 if (v.empty())
00292 {
00293 sprintf(str, "<nothing selected>\n");
00294 }
00295 else
00296 sprintf(str, "%d items selected, first one is %d, last one is %d\n",
00297 v.size(),
00298 (int)v[0]->GetNode(0)->GetCookie(),
00299 (int)v[v.size() - 1]->GetNode(0)->GetCookie());
00300 s+=str;
00301
00302 MessageBox *pMsgBox = new MessageBox("ListView selection", s.c_str(), (MessageBoxFlags)(MESSAGEBOX_FLAG_OK | MESSAGEBOX_FLAG_INFO), NULL);
00303 pMsgBox->Show();
00304 }
00305 if (pMessage->GetID() == 10002)
00306 {
00307 char str[255];
00308
00309 sprintf(str, "Column %d", iColumn++);
00310 pListView->AppendColumn(new ListViewColumn(str, 100));
00311 }
00312
00313 if (pMessage->GetID() == 10004)
00314 {
00315 pListView->SetShowTree(!(pListView->GetShowTree()));
00316 }
00317
00318 if (pMessage->GetID() == 10003)
00319 {
00320
00321 printf("%08X\n", pListView->GetFirst());
00322 ListViewIterator pIterator(pListView);
00323
00324 while (pRow = pIterator.GetNext(true))
00325 {
00326 printf("%d\n", (int)pRow->GetNode(0)->GetCookie());
00327 }
00328 }
00329
00330 }
00331 MyListView::MyListView(Window *pParent, const Rect &rFrame, unsigned int nWindowLayoutFlags) : View(pParent, rFrame, nWindowLayoutFlags)
00332 {
00333
00334 pListView = new ListView(this, Rect(10, 10, 0, Height() - 80), WINDOW_LAYOUT_SAME_WIDTH | WINDOW_LAYOUT_FOLLOW_BOTTOM , 0);
00335 pListView->SetAutoSortChildren(true);
00336 pListView->ShowIcons(true);
00337 pListView->SetKeyLookupColumn(0);
00338
00339 pListView->ContextMenu.Connect(this, &MyListView::OnContextMenu);
00340
00341 Button *pButton = new Button(this, Rect(10, Height() - 70, 10+100, Height() - 70 + 22), "Get", WINDOW_LAYOUT_FOLLOW_BOTTOM | WINDOW_LAYOUT_FOLLOW_TOP, new MessageCommand(10000));
00342 pButton = new Button(this, Rect(130, Height() - 70, 130 + 100, Height() - 70 + 22), "Header", WINDOW_LAYOUT_FOLLOW_BOTTOM | WINDOW_LAYOUT_FOLLOW_TOP, new MessageCommand(10001));
00343 pButton->Clicked.Connect(this, &MyListView::OnHeaderClicked);
00344
00345 pButton = new Button(this, Rect(250, Height() - 70, 250 + 100, Height() - 70 + 22), "Tree", WINDOW_LAYOUT_FOLLOW_BOTTOM | WINDOW_LAYOUT_FOLLOW_TOP, new MessageCommand(10004));
00346
00347
00348 pButton = new Button(this, Rect(10, Height() - 40, 100, Height() - 40 + 22), "Add", WINDOW_LAYOUT_FOLLOW_BOTTOM | WINDOW_LAYOUT_FOLLOW_TOP, new MessageCommand(10002));
00349
00350 pButton = new Button(this, Rect(130, Height() - 40, 130 + 100, Height() - 40 + 22), "Rename", WINDOW_LAYOUT_FOLLOW_BOTTOM | WINDOW_LAYOUT_FOLLOW_TOP);
00351 pButton->Clicked.Connect(this, &MyListView::OnRename);
00352
00353 new StringView(this, Rect(10, Height() - 13, 300, Height() ), "Make a slow double click on a node to edit it", WINDOW_LAYOUT_FOLLOW_BOTTOM | WINDOW_LAYOUT_FOLLOW_TOP);
00354
00355
00356 Add(10);
00357 }
00358
00359 void ListViewTest()
00360 {
00361 Rect r(Point(100, 100), Point(500, 440));
00362
00363 ApplicationWindow* pApplicationWindow = new ApplicationWindow(r, "ListView", WINDOW_LAYOUT_NOTHING, APPLICATION_WINDOW_NO_VIEW);
00364
00365 pApplicationWindow->GetTitleWindow()->SetFlags( (TitleWindowFlags)(pApplicationWindow->GetTitleWindow()->GetFlags() ));
00366 pApplicationWindow->AttachView(new MyListView(pApplicationWindow, pApplicationWindow->GetClientRect(), WINDOW_LAYOUT_NOTHING));
00367 pApplicationWindow->Show();
00368 }
00369