1/*
2 * DialogWindow.h
3 * Copyright 2004 Michael Pfeiffer. All Rights Reserved.
4 */
5
6#include "DialogWindow.h"
7
8#include <Messenger.h>
9
10DialogWindow::DialogWindow(BRect frame,
11			const char *title,
12			window_type type,
13			uint32 flags,
14			uint32 workspace)
15	: BWindow(frame, title, type, flags, workspace)
16	, fPreviousResult(B_OK)
17	, fResult(NULL)
18{
19	// nothing to do
20}
21
22DialogWindow::DialogWindow(BRect frame,
23			const char *title,
24			window_look look,
25			window_feel feel,
26			uint32 flags,
27			uint32 workspace)
28	: BWindow(frame, title, look, feel, flags, workspace)
29	, fPreviousResult(B_OK)
30	, fResult(NULL)
31{
32	// nothing to do
33}
34
35void DialogWindow::MessageReceived(BMessage *msg)
36{
37	if (msg->what == kGetThreadId) {
38		BMessage reply;
39		reply.AddInt32("thread_id", Thread());
40		msg->SendReply(&reply);
41		return;
42	}
43	BWindow::MessageReceived(msg);
44}
45
46status_t DialogWindow::Go()
47{
48	BMessenger messenger(this, this);
49	// store result in local variable and
50	// initialize it with previous result
51	volatile status_t result = fPreviousResult;
52	// new results are stored on the stack
53	fResult = &result;
54
55	// show the window
56	Show();
57	// at this point we must not access member variables,
58	// because this object (the window) could already be deleted.
59
60	// get thread id of window thread
61	BMessage reply;
62	if (messenger.SendMessage(kGetThreadId, &reply) != B_OK) {
63		return B_ERROR;
64	}
65	thread_id windowThread;
66	if (reply.FindInt32("thread_id", &windowThread) != B_OK) {
67		return B_ERROR;
68	}
69
70	// wait for window thread to die
71	// The window thread will crash if the image holding the
72	// code used by the window thread is unloaded while the thread is
73	// still running!!!
74	status_t status = B_ERROR;
75	wait_for_thread(windowThread, &status);
76
77	return result;
78}
79
80void DialogWindow::SetResult(status_t result)
81{
82	if (fResult != NULL) {
83		*fResult = result;
84	} else {
85		fPreviousResult = result;
86	}
87}
88
89