176145367SMichael Pfeiffer/*
276145367SMichael Pfeiffer * DialogWindow.h
376145367SMichael Pfeiffer * Copyright 2004 Michael Pfeiffer. All Rights Reserved.
476145367SMichael Pfeiffer */
576145367SMichael Pfeiffer
676145367SMichael Pfeiffer#include "DialogWindow.h"
776145367SMichael Pfeiffer
876145367SMichael Pfeiffer#include <Messenger.h>
976145367SMichael Pfeiffer
1076145367SMichael PfeifferDialogWindow::DialogWindow(BRect frame,
1176145367SMichael Pfeiffer			const char *title,
1276145367SMichael Pfeiffer			window_type type,
1376145367SMichael Pfeiffer			uint32 flags,
1476145367SMichael Pfeiffer			uint32 workspace)
1576145367SMichael Pfeiffer	: BWindow(frame, title, type, flags, workspace)
1676145367SMichael Pfeiffer	, fPreviousResult(B_OK)
1776145367SMichael Pfeiffer	, fResult(NULL)
1876145367SMichael Pfeiffer{
1976145367SMichael Pfeiffer	// nothing to do
2076145367SMichael Pfeiffer}
2176145367SMichael Pfeiffer
2276145367SMichael PfeifferDialogWindow::DialogWindow(BRect frame,
2376145367SMichael Pfeiffer			const char *title,
2476145367SMichael Pfeiffer			window_look look,
2576145367SMichael Pfeiffer			window_feel feel,
2676145367SMichael Pfeiffer			uint32 flags,
2776145367SMichael Pfeiffer			uint32 workspace)
2876145367SMichael Pfeiffer	: BWindow(frame, title, look, feel, flags, workspace)
2976145367SMichael Pfeiffer	, fPreviousResult(B_OK)
3076145367SMichael Pfeiffer	, fResult(NULL)
3176145367SMichael Pfeiffer{
3276145367SMichael Pfeiffer	// nothing to do
3376145367SMichael Pfeiffer}
3476145367SMichael Pfeiffer
3576145367SMichael Pfeiffervoid DialogWindow::MessageReceived(BMessage *msg)
3676145367SMichael Pfeiffer{
3776145367SMichael Pfeiffer	if (msg->what == kGetThreadId) {
3876145367SMichael Pfeiffer		BMessage reply;
3976145367SMichael Pfeiffer		reply.AddInt32("thread_id", Thread());
4076145367SMichael Pfeiffer		msg->SendReply(&reply);
4176145367SMichael Pfeiffer		return;
4276145367SMichael Pfeiffer	}
4376145367SMichael Pfeiffer	BWindow::MessageReceived(msg);
4476145367SMichael Pfeiffer}
4576145367SMichael Pfeiffer
4676145367SMichael Pfeifferstatus_t DialogWindow::Go()
4776145367SMichael Pfeiffer{
4876145367SMichael Pfeiffer	BMessenger messenger(this, this);
4976145367SMichael Pfeiffer	// store result in local variable and
5076145367SMichael Pfeiffer	// initialize it with previous result
5176145367SMichael Pfeiffer	volatile status_t result = fPreviousResult;
5276145367SMichael Pfeiffer	// new results are stored on the stack
5376145367SMichael Pfeiffer	fResult = &result;
5476145367SMichael Pfeiffer
5576145367SMichael Pfeiffer	// show the window
5676145367SMichael Pfeiffer	Show();
5776145367SMichael Pfeiffer	// at this point we must not access member variables,
5876145367SMichael Pfeiffer	// because this object (the window) could already be deleted.
5976145367SMichael Pfeiffer
6076145367SMichael Pfeiffer	// get thread id of window thread
6176145367SMichael Pfeiffer	BMessage reply;
6276145367SMichael Pfeiffer	if (messenger.SendMessage(kGetThreadId, &reply) != B_OK) {
6376145367SMichael Pfeiffer		return B_ERROR;
6476145367SMichael Pfeiffer	}
6576145367SMichael Pfeiffer	thread_id windowThread;
6676145367SMichael Pfeiffer	if (reply.FindInt32("thread_id", &windowThread) != B_OK) {
6776145367SMichael Pfeiffer		return B_ERROR;
6876145367SMichael Pfeiffer	}
6976145367SMichael Pfeiffer
7076145367SMichael Pfeiffer	// wait for window thread to die
7176145367SMichael Pfeiffer	// The window thread will crash if the image holding the
7276145367SMichael Pfeiffer	// code used by the window thread is unloaded while the thread is
7376145367SMichael Pfeiffer	// still running!!!
7476145367SMichael Pfeiffer	status_t status = B_ERROR;
7576145367SMichael Pfeiffer	wait_for_thread(windowThread, &status);
7676145367SMichael Pfeiffer
7776145367SMichael Pfeiffer	return result;
7876145367SMichael Pfeiffer}
7976145367SMichael Pfeiffer
8076145367SMichael Pfeiffervoid DialogWindow::SetResult(status_t result)
8176145367SMichael Pfeiffer{
8276145367SMichael Pfeiffer	if (fResult != NULL) {
8376145367SMichael Pfeiffer		*fResult = result;
8476145367SMichael Pfeiffer	} else {
8576145367SMichael Pfeiffer		fPreviousResult = result;
8676145367SMichael Pfeiffer	}
8776145367SMichael Pfeiffer}
8876145367SMichael Pfeiffer
89