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