1 /* 2 * Copyright 2010, Haiku. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Clemens Zeidler <haiku@clemens-zeidler.de> 7 */ 8 9 #include "WindowStackTest.h" 10 11 #include <Alert.h> 12 #include <Application.h> 13 #include <ControlLook.h> 14 #include <Roster.h> 15 #include <String.h> 16 #include <Window.h> 17 18 #include <WindowStack.h> 19 20 21 const int32 kGetWindows = '&GeW'; 22 const int32 kAddWindow = '&AdW'; 23 const int32 kRemoveWindow = '&ReW'; 24 25 26 WindowListItem::WindowListItem(const char* text, BWindow* window) 27 : 28 BStringItem(text), 29 fWindow(window) 30 { 31 32 } 33 34 35 MainView::MainView() 36 : 37 BBox("MainView") 38 { 39 fStackedWindowsLabel = new BStringView("label", "Stacked windows:"); 40 fStackedWindowsList = new BListView; 41 fGetWindowsButton = new BButton("Get Windows", new BMessage(kGetWindows)); 42 fAddWindowButton = new BButton("Add Window", new BMessage(kAddWindow)); 43 fRemoveWindowButton = new BButton("Remove Window", 44 new BMessage(kRemoveWindow)); 45 46 float spacing = be_control_look->DefaultItemSpacing(); 47 SetLayout(new BGroupLayout(B_HORIZONTAL)); 48 AddChild(BGroupLayoutBuilder(B_VERTICAL, spacing) 49 .AddGroup(B_HORIZONTAL, spacing) 50 .Add(fStackedWindowsLabel) 51 .AddGlue() 52 .End() 53 .Add(fStackedWindowsList) 54 .AddGroup(B_HORIZONTAL, spacing) 55 .AddGlue() 56 .Add(fGetWindowsButton) 57 .Add(fRemoveWindowButton) 58 .Add(fAddWindowButton) 59 .End() 60 //.SetInsets(spacing, spacing, spacing, spacing) 61 ); 62 63 } 64 65 66 void 67 MainView::AttachedToWindow() 68 { 69 fGetWindowsButton->SetTarget(this); 70 fAddWindowButton->SetTarget(this); 71 fRemoveWindowButton->SetTarget(this); 72 } 73 74 75 void 76 MainView::MessageReceived(BMessage* message) 77 { 78 switch (message->what) { 79 case kGetWindows: 80 { 81 BWindowStack windowStack(Window()); 82 /*BString string; 83 string << windowStack.CountWindows(); 84 BAlert* alert = new BAlert("title", "Count: ", string.String()); 85 alert->Go();*/ 86 int32 stackWindowCount = windowStack.CountWindows(); 87 fStackedWindowsList->MakeEmpty(); 88 for (int i = 0; i < stackWindowCount; i++) { 89 BString result; 90 91 BMessenger messenger;//(NULL, Window()); 92 windowStack.WindowAt(i, messenger); 93 94 // don't deadlock 95 if (!messenger.IsTargetLocal()) { 96 BMessage message(B_GET_PROPERTY); 97 message.AddSpecifier("Title"); 98 BMessage reply; 99 100 messenger.SendMessage(&message, &reply); 101 reply.FindString("result", &result); 102 } 103 else 104 result = Window()->Title(); 105 106 fStackedWindowsList->AddItem(new BStringItem( 107 result.String())); 108 } 109 break; 110 } 111 112 case kAddWindow: 113 { 114 app_info appInfo; 115 if (be_app->GetAppInfo(&appInfo) != B_OK) 116 break; 117 118 team_id team; 119 BRoster roster; 120 //roster.Launch("application/x-vnd.windowstack_test", (BMessage*)NULL, 121 // &team); 122 roster.Launch(&appInfo.ref, (BMessage*)NULL, 123 &team); 124 125 BMessage message(B_GET_PROPERTY); 126 message.AddSpecifier("Window", int32(0)); 127 BMessage reply; 128 BMessenger appMessenger(NULL, team); 129 appMessenger.SendMessage(&message, &reply); 130 131 BMessenger window; 132 reply.FindMessenger("result", &window); 133 int32 error = 0; 134 reply.FindInt32("error", &error); 135 136 BWindowStack windowStack(Window()); 137 if (windowStack.HasWindow(window)) { 138 BAlert* alert = new BAlert("API Error", 139 "Window on stack but should not be there!", "Ok"); 140 alert->Go(); 141 } 142 windowStack.AddWindow(window); 143 if (!windowStack.HasWindow(window)) { 144 BAlert* alert = new BAlert("API Error", 145 "Window not on stack but should be there!", "Ok"); 146 alert->Go(); 147 } 148 break; 149 } 150 151 case kRemoveWindow: 152 { 153 BWindowStack windowStack(Window()); 154 BMessenger messenger; 155 windowStack.WindowAt(0, messenger); 156 windowStack.RemoveWindow(messenger); 157 break; 158 } 159 } 160 161 BView::MessageReceived(message); 162 } 163 164 165 int main() 166 { 167 BApplication app("application/x-vnd.windowstack_test"); 168 BWindow *window = new BWindow(BRect(100, 100, 500, 300), 169 "BWindowStackTest", B_TITLED_WINDOW, B_QUIT_ON_WINDOW_CLOSE); 170 window->SetLayout(new BGroupLayout(B_VERTICAL)); 171 window->AddChild(BGroupLayoutBuilder(B_VERTICAL, 10) 172 .Add(new MainView) 173 .SetInsets(10, 10, 10, 10) 174 ); 175 176 window->Show(); 177 app.Run(); 178 } 179