xref: /haiku/src/apps/debuganalyzer/gui/thread_window/ThreadWindow.cpp (revision 2222d0559df303a9846a2fad53741f8b20b14d7c)
1 /*
2  * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include "ThreadWindow.h"
7 
8 #include <stdio.h>
9 
10 #include <Application.h>
11 #include <GroupLayoutBuilder.h>
12 #include <String.h>
13 #include <TabView.h>
14 
15 #include <AutoLocker.h>
16 
17 #include "MessageCodes.h"
18 #include "ThreadModel.h"
19 #include "ThreadModelLoader.h"
20 
21 #include "thread_window/ActivityPage.h"
22 #include "thread_window/GeneralPage.h"
23 #include "thread_window/WaitObjectsPage.h"
24 
25 
26 static BString
27 get_window_name(Model::Thread* thread)
28 {
29 	char buffer[1024];
30 	snprintf(buffer, sizeof(buffer), "Thread: %s (%ld)", thread->Name(),
31 		thread->ID());
32 	return BString(buffer);
33 }
34 
35 
36 ThreadWindow::ThreadWindow(SubWindowManager* manager, Model* model,
37 	Model::Thread* thread)
38 	:
39 	SubWindow(manager, BRect(50, 50, 599, 499),
40 		get_window_name(thread).String(), B_DOCUMENT_WINDOW,
41 		B_ASYNCHRONOUS_CONTROLS),
42 	fMainTabView(NULL),
43 	fGeneralPage(NULL),
44 	fWaitObjectsPage(NULL),
45 	fActivityPage(NULL),
46 	fModel(model),
47 	fThread(thread),
48 	fThreadModel(NULL),
49 	fThreadModelLoader(NULL)
50 {
51 	BGroupLayout* rootLayout = new BGroupLayout(B_VERTICAL);
52 	SetLayout(rootLayout);
53 
54 	fMainTabView = new BTabView("main tab view");
55 
56 	BGroupLayoutBuilder(rootLayout)
57 		.Add(fMainTabView);
58 
59 	fMainTabView->AddTab(fGeneralPage = new GeneralPage);
60 	fMainTabView->AddTab(fWaitObjectsPage = new WaitObjectsPage);
61 	fMainTabView->AddTab(fActivityPage = new ActivityPage);
62 
63 	fGeneralPage->SetModel(fModel, fThread);
64 
65 	fModel->AddReference();
66 
67 	// create a thread model loader
68 	fThreadModelLoader = new ThreadModelLoader(fModel, fThread,
69 		BMessenger(this), NULL);
70 }
71 
72 
73 ThreadWindow::~ThreadWindow()
74 {
75 	if (fThreadModelLoader != NULL)
76 		fThreadModelLoader->Delete();
77 
78 	delete fThreadModel;
79 
80 	fModel->RemoveReference();
81 }
82 
83 
84 void
85 ThreadWindow::MessageReceived(BMessage* message)
86 {
87 	switch (message->what) {
88 		case MSG_MODEL_LOADED_SUCCESSFULLY:
89 		{
90 printf("MSG_MODEL_LOADED_SUCCESSFULLY\n");
91 			ThreadModel* model = fThreadModelLoader->DetachModel();
92 			fThreadModelLoader->Delete();
93 			fThreadModelLoader = NULL;
94 			_SetModel(model);
95 			break;
96 		}
97 
98 		case MSG_MODEL_LOADED_FAILED:
99 		case MSG_MODEL_LOADED_ABORTED:
100 		{
101 printf("MSG_MODEL_LOADED_FAILED/MSG_MODEL_LOADED_ABORTED\n");
102 			fThreadModelLoader->Delete();
103 			fThreadModelLoader = NULL;
104 			// TODO: User feedback (in failed case)!
105 			break;
106 		}
107 
108 		default:
109 			SubWindow::MessageReceived(message);
110 			break;
111 	}
112 }
113 
114 
115 void
116 ThreadWindow::Quit()
117 {
118 	if (fThreadModelLoader != NULL)
119 		fThreadModelLoader->Abort(true);
120 
121 	SubWindow::Quit();
122 }
123 
124 
125 void
126 ThreadWindow::Show()
127 {
128 	SubWindow::Show();
129 
130 	AutoLocker<ThreadWindow> locker;
131 
132 	if (fThreadModelLoader == NULL)
133 		return;
134 
135 	status_t error = fThreadModelLoader->StartLoading();
136 	if (error != B_OK) {
137 		fThreadModelLoader->Delete();
138 		fThreadModelLoader = NULL;
139 		// TODO: User feedback!
140 	}
141 }
142 
143 
144 void
145 ThreadWindow::_SetModel(ThreadModel* model)
146 {
147 	delete fThreadModel;
148 
149 	fThreadModel = model;
150 
151 	fWaitObjectsPage->SetModel(fThreadModel);
152 	fActivityPage->SetModel(fThreadModel);
153 }
154