1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
---|
2 | /* ***** BEGIN LICENSE BLOCK ***** |
---|
3 | * Version: NPL 1.1/GPL 2.0/LGPL 2.1 |
---|
4 | * |
---|
5 | * The contents of this file are subject to the Netscape Public License |
---|
6 | * Version 1.1 (the "License"); you may not use this file except in |
---|
7 | * compliance with the License. You may obtain a copy of the License at |
---|
8 | * http://www.mozilla.org/NPL/ |
---|
9 | * |
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis, |
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
---|
12 | * for the specific language governing rights and limitations under the |
---|
13 | * License. |
---|
14 | * |
---|
15 | * The Original Code is Mozilla Communicator client code. |
---|
16 | * |
---|
17 | * The Initial Developer of the Original Code is |
---|
18 | * Netscape Communications Corporation. |
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998 |
---|
20 | * the Initial Developer. All Rights Reserved. |
---|
21 | * |
---|
22 | * Contributor(s): |
---|
23 | * |
---|
24 | * Alternatively, the contents of this file may be used under the terms of |
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead |
---|
28 | * of those above. If you wish to allow use of your version of this file only |
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to |
---|
30 | * use your version of this file under the terms of the NPL, indicate your |
---|
31 | * decision by deleting the provisions above and replace them with the notice |
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete |
---|
33 | * the provisions above, a recipient may use your version of this file under |
---|
34 | * the terms of any one of the NPL, the GPL or the LGPL. |
---|
35 | * |
---|
36 | * ***** END LICENSE BLOCK ***** */ |
---|
37 | |
---|
38 | #include <dirent.h> //For random splash screen. |
---|
39 | #include "nsNativeAppSupport.h" |
---|
40 | |
---|
41 | #define PX_IMAGE_MODULES |
---|
42 | #define PX_BMP_SUPPORT |
---|
43 | #include <photon/PxImage.h> |
---|
44 | |
---|
45 | #include <Ph.h> |
---|
46 | #include <Pt.h> |
---|
47 | |
---|
48 | class nsSplashScreenPh : public nsISplashScreen { |
---|
49 | public: |
---|
50 | nsSplashScreenPh() |
---|
51 | : mDialog( 0 ), mRefCnt( 0 ) { |
---|
52 | |
---|
53 | /* initialize widget library and attach to Photon */ |
---|
54 | if( PtInit( NULL ) ) |
---|
55 | exit( EXIT_FAILURE ); |
---|
56 | |
---|
57 | /* increase the draw buffer */ |
---|
58 | PgSetDrawBufferSize( 0x8000 ); |
---|
59 | } |
---|
60 | ~nsSplashScreenPh() { |
---|
61 | Hide(); |
---|
62 | } |
---|
63 | |
---|
64 | NS_IMETHOD Show(); |
---|
65 | NS_IMETHOD Hide(); |
---|
66 | |
---|
67 | // nsISupports methods |
---|
68 | NS_IMETHOD_(nsrefcnt) AddRef() { |
---|
69 | mRefCnt++; |
---|
70 | return mRefCnt; |
---|
71 | } |
---|
72 | NS_IMETHOD_(nsrefcnt) Release() { |
---|
73 | --mRefCnt; |
---|
74 | if ( !mRefCnt ) { |
---|
75 | delete this; |
---|
76 | return 0; |
---|
77 | } |
---|
78 | return mRefCnt; |
---|
79 | } |
---|
80 | NS_IMETHOD QueryInterface( const nsIID &iid, void**p ) { |
---|
81 | nsresult rv = NS_OK; |
---|
82 | if ( p ) { |
---|
83 | *p = 0; |
---|
84 | if ( iid.Equals( NS_GET_IID( nsISplashScreen ) ) ) { |
---|
85 | nsISplashScreen *result = this; |
---|
86 | *p = result; |
---|
87 | NS_ADDREF( result ); |
---|
88 | } else if ( iid.Equals( NS_GET_IID( nsISupports ) ) ) { |
---|
89 | nsISupports *result = NS_STATIC_CAST( nsISupports*, this ); |
---|
90 | *p = result; |
---|
91 | NS_ADDREF( result ); |
---|
92 | } else { |
---|
93 | rv = NS_NOINTERFACE; |
---|
94 | } |
---|
95 | } else { |
---|
96 | rv = NS_ERROR_NULL_POINTER; |
---|
97 | } |
---|
98 | return rv; |
---|
99 | } |
---|
100 | |
---|
101 | PtWidget_t *mDialog; |
---|
102 | nsrefcnt mRefCnt; |
---|
103 | }; // class nsSplashScreenPh |
---|
104 | |
---|
105 | char *RandomSplash( char *gob ) |
---|
106 | { |
---|
107 | int i=0; |
---|
108 | int value=0; |
---|
109 | int foundIT=0; |
---|
110 | char duper[80]; |
---|
111 | |
---|
112 | DIR* dirp; |
---|
113 | struct dirent* direntp; |
---|
114 | |
---|
115 | dirp = opendir( gob ); |
---|
116 | if( dirp != NULL ) |
---|
117 | { |
---|
118 | for(;;) |
---|
119 | { |
---|
120 | direntp = readdir( dirp ); |
---|
121 | if( direntp == NULL ) break; |
---|
122 | i++;// number of available splash screens |
---|
123 | } |
---|
124 | |
---|
125 | closedir( dirp ); |
---|
126 | |
---|
127 | // generate random number based on currnet time |
---|
128 | srand(time(NULL)); |
---|
129 | for(value = 0; value < 9999; value = rand()); |
---|
130 | |
---|
131 | dirp = opendir( gob ); |
---|
132 | if( dirp != NULL ) { |
---|
133 | for(foundIT=1;foundIT<=i;foundIT++) { |
---|
134 | direntp = readdir( dirp ); |
---|
135 | if( direntp == NULL ) break; |
---|
136 | if( foundIT == value%i ) break; |
---|
137 | } |
---|
138 | |
---|
139 | closedir( dirp ); |
---|
140 | |
---|
141 | // return the original splash.bmp if the directory entries are "." or ".." |
---|
142 | if (((value%i) == 1) || ((value%i) == 2)) |
---|
143 | return "../splash.bmp"; |
---|
144 | |
---|
145 | return direntp->d_name; |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | } |
---|
150 | return (char *)EXIT_FAILURE; |
---|
151 | } |
---|
152 | |
---|
153 | NS_IMETHODIMP |
---|
154 | nsSplashScreenPh::Show() |
---|
155 | { |
---|
156 | PhImage_t *img = nsnull; |
---|
157 | char *p = NULL; |
---|
158 | char *splash = NULL; |
---|
159 | //char splash[80]; |
---|
160 | char splashdir[80]; |
---|
161 | int inp_grp,n=0; |
---|
162 | PhRid_t rid; |
---|
163 | PhRegion_t region; |
---|
164 | PhRect_t rect; |
---|
165 | PRInt32 aWidth, aHeight; |
---|
166 | PhRect_t console; |
---|
167 | struct stat sbuf; |
---|
168 | |
---|
169 | /* Get the Screen Size and Depth, so I can center the splash dialog, there has to be a better way!*/ |
---|
170 | p = getenv("PHIG"); |
---|
171 | if( p ) inp_grp = atoi( p ); |
---|
172 | else inp_grp = 1; |
---|
173 | |
---|
174 | PhQueryRids( 0, 0, inp_grp, Ph_INPUTGROUP_REGION, 0, 0, 0, &rid, 1 ); |
---|
175 | PhRegionQuery( rid, ®ion, &rect, NULL, 0 ); |
---|
176 | inp_grp = region.input_group; |
---|
177 | PhWindowQueryVisible( Ph_QUERY_INPUT_GROUP | Ph_QUERY_EXACT, 0, inp_grp, &rect ); |
---|
178 | aWidth = rect.lr.x - rect.ul.x + 1; |
---|
179 | aHeight = rect.lr.y - rect.ul.y + 1; |
---|
180 | |
---|
181 | /* Get the current console offset */ |
---|
182 | if (PhWindowQueryVisible( Ph_QUERY_GRAPHICS, 0, inp_grp, &console ) == 0) |
---|
183 | |
---|
184 | mDialog = nsnull; |
---|
185 | |
---|
186 | /* Setup proper path to the splash.bmp so it loads the splash screen at start up. */ |
---|
187 | /* Only try and open the splash screen bitmap if running exec'ing from mozilla shellscript*/ |
---|
188 | splash = getenv ("ADDON_PATH"); |
---|
189 | if ( splash ) { |
---|
190 | // if ADDON_PATH/splash/ exists -- randomly choose a splash screen from here... |
---|
191 | strcpy (splashdir,"/opt/Mozilla/mozilla/splash/"); |
---|
192 | if( lstat( splashdir, &sbuf ) == 0) |
---|
193 | { |
---|
194 | strcpy (splash,splashdir); |
---|
195 | strcat (splash,(char *)RandomSplash(splashdir)); |
---|
196 | } |
---|
197 | else |
---|
198 | strcat (splash,"/splash.bmp"); |
---|
199 | |
---|
200 | if ((img = PxLoadImage(splash, NULL)) !=NULL ) |
---|
201 | { |
---|
202 | PtArg_t arg[6]; |
---|
203 | PhPoint_t pos; |
---|
204 | |
---|
205 | img->flags = img->flags | Ph_RELEASE_IMAGE_ALL; |
---|
206 | |
---|
207 | pos.x = (aWidth/2) - (img->size.w/2); |
---|
208 | pos.y = (aHeight/2) - (img->size.h/2); |
---|
209 | |
---|
210 | pos.x += console.ul.x; |
---|
211 | pos.y += console.ul.y; |
---|
212 | |
---|
213 | PtSetArg( &arg[n], Pt_ARG_DIM, &img->size, 0 ); |
---|
214 | PtSetArg( &arg[++n], Pt_ARG_POS, &pos, 0 ); |
---|
215 | PtSetArg( &arg[++n], Pt_ARG_WINDOW_RENDER_FLAGS, 0, 0xFFFFFFFF ); |
---|
216 | PtSetArg( &arg[++n], Pt_ARG_FILL_COLOR, Pg_BLACK, 0 ); |
---|
217 | PtSetArg( &arg[++n], Pt_ARG_WINDOW_MANAGED_FLAGS, Ph_WM_CLOSE | Ph_WM_TOFRONT | Ph_WM_TOBACK | Ph_WM_MOVE, ~0 ); |
---|
218 | PtSetArg( &arg[++n], Pt_ARG_WINDOW_MANAGED_FLAGS, Ph_WM_RESIZE, ~0 ); |
---|
219 | mDialog = PtCreateWidget( PtWindow, NULL, n, arg ); |
---|
220 | |
---|
221 | n=0; |
---|
222 | PtSetArg( &arg[n], Pt_ARG_LABEL_TYPE, Pt_IMAGE, 0 ); |
---|
223 | PtSetArg( &arg[++n], Pt_ARG_LABEL_DATA, img, sizeof(PhImage_t) ); |
---|
224 | PtSetArg( &arg[++n], Pt_ARG_BASIC_FLAGS, 0, 0xFFFFFFFF ); |
---|
225 | PtSetArg( &arg[++n], Pt_ARG_FLAGS, 0, Pt_HIGHLIGHTED); |
---|
226 | PtSetArg( &arg[++n], Pt_ARG_MARGIN_HEIGHT, 0, 0); |
---|
227 | PtSetArg( &arg[++n], Pt_ARG_MARGIN_WIDTH, 0, 0); |
---|
228 | PtCreateWidget( PtLabel, mDialog, n, arg ); |
---|
229 | PtRealizeWidget( mDialog ); |
---|
230 | PtFlush(); |
---|
231 | } |
---|
232 | } |
---|
233 | else |
---|
234 | { |
---|
235 | fprintf( stderr, "Error loading splash screen image: %s\n", splash ); |
---|
236 | } |
---|
237 | |
---|
238 | return NS_OK; |
---|
239 | } |
---|
240 | |
---|
241 | NS_IMETHODIMP |
---|
242 | nsSplashScreenPh::Hide() { |
---|
243 | if ( mDialog ) { |
---|
244 | PtDestroyWidget(mDialog); |
---|
245 | mDialog = nsnull; |
---|
246 | } |
---|
247 | return NS_OK; |
---|
248 | } |
---|
249 | |
---|
250 | nsresult NS_CreateSplashScreen(nsISplashScreen**aResult) |
---|
251 | { |
---|
252 | if ( aResult ) { |
---|
253 | *aResult = new nsSplashScreenPh; |
---|
254 | if ( *aResult ) { |
---|
255 | NS_ADDREF( *aResult ); |
---|
256 | return NS_OK; |
---|
257 | } else { |
---|
258 | return NS_ERROR_OUT_OF_MEMORY; |
---|
259 | } |
---|
260 | } else { |
---|
261 | return NS_ERROR_NULL_POINTER; |
---|
262 | } |
---|
263 | } |
---|
264 | |
---|
265 | PRBool NS_CanRun() |
---|
266 | { |
---|
267 | return PR_TRUE; |
---|
268 | } |
---|