xref: /haiku/src/apps/showimage/ShowImageApp.cpp (revision 51978af14a173e7fae0563b562be5603bc652aeb)
1 /*****************************************************************************/
2 // ShowImageApp
3 // Written by Fernando Francisco de Oliveira, Michael Wilber
4 //
5 // ShowImageApp.cpp
6 //
7 //
8 // Copyright (c) 2003 OpenBeOS Project
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal in the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included
18 // in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 // DEALINGS IN THE SOFTWARE.
27 /*****************************************************************************/
28 
29 #include <stdio.h>
30 #include <Alert.h>
31 #include <FilePanel.h>
32 
33 #include "ShowImageConstants.h"
34 #include "ShowImageApp.h"
35 #include "ShowImageWindow.h"
36 
37 int main(int, char **)
38 {
39 	ShowImageApp theApp;
40 	theApp.Run();
41 	return 0;
42 }
43 
44 #define WINDOWS_TO_IGNORE 1
45 
46 ShowImageApp::ShowImageApp()
47 	: BApplication(APP_SIG)
48 {
49 	fbpulseStarted = false;
50 	fpopenPanel = new BFilePanel(B_OPEN_PANEL);
51 }
52 
53 void
54 ShowImageApp::AboutRequested()
55 {
56 	BAlert* pAlert = new BAlert("About ShowImage",
57 		"OBOS ShowImage\n\nby Fernando F. Oliveira and Michael Wilber", "OK");
58 	pAlert->Go();
59 }
60 
61 void
62 ShowImageApp::ReadyToRun()
63 {
64 	if (CountWindows() == WINDOWS_TO_IGNORE)
65 		fpopenPanel->Show();
66 	else
67 		// If image windows are already open
68 		// (paths supplied on the command line)
69 		// start checking the number of open windows
70 		StartPulse();
71 }
72 
73 void
74 ShowImageApp::StartPulse()
75 {
76 	if (!fbpulseStarted) {
77 		// Tell the app to begin checking
78 		// for the number of open windows
79 		fbpulseStarted = true;
80 		SetPulseRate(250000);
81 			// Set pulse to every 1/4 second
82 	}
83 }
84 
85 void
86 ShowImageApp::Pulse()
87 {
88 	if (!IsLaunching() && CountWindows() <= WINDOWS_TO_IGNORE)
89 		// If the application is not launching and
90 		// all windows are closed except for the file open panel,
91 		// quit the application
92 		PostMessage(B_QUIT_REQUESTED);
93 }
94 
95 void
96 ShowImageApp::ArgvReceived(int32 argc, char **argv)
97 {
98 	BMessage *pmsg = NULL;
99 	for (int32 i = 1; i < argc; i++) {
100 		entry_ref ref;
101 		status_t err = get_ref_for_path(argv[i], &ref);
102 		if (err == B_OK) {
103 			if (!pmsg) {
104 				pmsg = new BMessage;
105 				pmsg->what = B_REFS_RECEIVED;
106 			}
107 			pmsg->AddRef("refs", &ref);
108 		}
109 	}
110 	if (pmsg)
111 		RefsReceived(pmsg);
112 }
113 
114 void
115 ShowImageApp::MessageReceived(BMessage *pmsg)
116 {
117 	switch (pmsg->what) {
118 		case MSG_FILE_OPEN:
119 			fpopenPanel->Show();
120 			break;
121 
122 		case MSG_WINDOW_QUIT:
123 			break;
124 
125 		case B_CANCEL:
126 			// File open panel was closed,
127 			// start checking count of open windows
128 			StartPulse();
129 			break;
130 
131 		default:
132 			BApplication::MessageReceived(pmsg);
133 			break;
134 	}
135 }
136 
137 void
138 ShowImageApp::RefsReceived(BMessage *pmsg)
139 {
140 	uint32 type;
141 	int32 count;
142 	status_t ret = pmsg->GetInfo("refs", &type, &count);
143 	if (ret != B_OK || type != B_REF_TYPE)
144 		return;
145 
146 	entry_ref ref;
147 	for (int32 i = 0; i < count; i++) {
148    		if (pmsg->FindRef("refs", i, &ref) == B_OK)
149    			Open(&ref);
150    	}
151 }
152 
153 void
154 ShowImageApp::Open(const entry_ref *pref)
155 {
156 	new ShowImageWindow(pref);
157 }
158