comparison os2/dw.c @ 278:df0665ba147f

Initial code for dw_bitmapbutton_new_from_file and added filename parameter to dw_window_set_bitmap().
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Fri, 14 Mar 2003 18:03:35 +0000
parents dfbda83b7cad
children 030188d45a69
comparison
equal deleted inserted replaced
277:d706e0dbb0cb 278:df0665ba147f
244 } 244 }
245 WinEndEnumWindows(henum); 245 WinEndEnumWindows(henum);
246 return; 246 return;
247 } 247 }
248 248
249 /* Free bitmap data associated with a window */
250 void _free_bitmap(HWND handle)
251 {
252 HBITMAP hbm = (HBITMAP)dw_window_get_data(handle, "_dw_bitmap");
253 HPS hps = (HPS)dw_window_get_data(handle, "_dw_hps");
254 HDC hdc = (HDC)dw_window_get_data(handle, "_dw_hdc");
255
256 if(hps)
257 {
258 GpiSetBitmap(hps, NULLHANDLE);
259 GpiAssociate(hps, NULLHANDLE);
260 GpiDestroyPS(hps);
261 }
262
263 if(hdc)
264 DevCloseDC(hdc);
265
266 if(hbm)
267 GpiDeleteBitmap(hbm);
268
269 }
270
249 /* This function removes and handlers on windows and frees 271 /* This function removes and handlers on windows and frees
250 * the user memory allocated to it. 272 * the user memory allocated to it.
251 */ 273 */
252 void _free_window_memory(HWND handle) 274 void _free_window_memory(HWND handle)
253 { 275 {
273 295
274 if(ptr) 296 if(ptr)
275 { 297 {
276 WindowData *wd = (WindowData *)ptr; 298 WindowData *wd = (WindowData *)ptr;
277 char tmpbuf[100]; 299 char tmpbuf[100];
278 HBITMAP hbm = (HBITMAP)dw_window_get_data(handle, "_dw_bitmap");
279 300
280 /* If this window has an associate bitmap destroy it. */ 301 /* If this window has an associate bitmap destroy it. */
281 if(hbm) 302 _free_bitmap(handle);
282 GpiDeleteBitmap(hbm);
283 303
284 WinQueryClassName(handle, 99, tmpbuf); 304 WinQueryClassName(handle, 99, tmpbuf);
285 305
286 if(strncmp(tmpbuf, "#1", 3)==0) 306 if(strncmp(tmpbuf, "#1", 3)==0)
287 { 307 {
3788 } 3808 }
3789 3809
3790 /* 3810 /*
3791 * Create a bitmap object to be packed. 3811 * Create a bitmap object to be packed.
3792 * Parameters: 3812 * Parameters:
3793 * id: An ID to be used with WinWindowFromID() or 0L. 3813 * id: An ID to be used with dw_window_from_id() or 0L.
3794 */ 3814 */
3795 HWND API dw_bitmap_new(ULONG id) 3815 HWND API dw_bitmap_new(ULONG id)
3796 { 3816 {
3797 return WinCreateWindow(HWND_OBJECT, 3817 return WinCreateWindow(HWND_OBJECT,
3798 WC_STATIC, 3818 WC_STATIC,
4066 4086
4067 /* 4087 /*
4068 * Create a new static text window (widget) to be packed. 4088 * Create a new static text window (widget) to be packed.
4069 * Parameters: 4089 * Parameters:
4070 * text: The text to be display by the static text widget. 4090 * text: The text to be display by the static text widget.
4071 * id: An ID to be used with WinWindowFromID() or 0L. 4091 * id: An ID to be used with dw_window_from_id() or 0L.
4072 */ 4092 */
4073 HWND API dw_text_new(char *text, ULONG id) 4093 HWND API dw_text_new(char *text, ULONG id)
4074 { 4094 {
4075 HWND tmp = WinCreateWindow(HWND_OBJECT, 4095 HWND tmp = WinCreateWindow(HWND_OBJECT,
4076 WC_STATIC, 4096 WC_STATIC,
4089 4109
4090 /* 4110 /*
4091 * Create a new status text window (widget) to be packed. 4111 * Create a new status text window (widget) to be packed.
4092 * Parameters: 4112 * Parameters:
4093 * text: The text to be display by the static text widget. 4113 * text: The text to be display by the static text widget.
4094 * id: An ID to be used with WinWindowFromID() or 0L. 4114 * id: An ID to be used with dw_window_from_id() or 0L.
4095 */ 4115 */
4096 HWND API dw_status_text_new(char *text, ULONG id) 4116 HWND API dw_status_text_new(char *text, ULONG id)
4097 { 4117 {
4098 WindowData *blah = calloc(sizeof(WindowData), 1); 4118 WindowData *blah = calloc(sizeof(WindowData), 1);
4099 HWND tmp = WinCreateWindow(HWND_OBJECT, 4119 HWND tmp = WinCreateWindow(HWND_OBJECT,
4118 #endif 4138 #endif
4119 4139
4120 /* 4140 /*
4121 * Create a new Multiline Editbox window (widget) to be packed. 4141 * Create a new Multiline Editbox window (widget) to be packed.
4122 * Parameters: 4142 * Parameters:
4123 * id: An ID to be used with WinWindowFromID() or 0L. 4143 * id: An ID to be used with dw_window_from_id() or 0L.
4124 */ 4144 */
4125 HWND API dw_mle_new(ULONG id) 4145 HWND API dw_mle_new(ULONG id)
4126 { 4146 {
4127 WindowData *blah = calloc(1, sizeof(WindowData)); 4147 WindowData *blah = calloc(1, sizeof(WindowData));
4128 HWND tmp = WinCreateWindow(HWND_OBJECT, 4148 HWND tmp = WinCreateWindow(HWND_OBJECT,
4146 4166
4147 /* 4167 /*
4148 * Create a new Entryfield window (widget) to be packed. 4168 * Create a new Entryfield window (widget) to be packed.
4149 * Parameters: 4169 * Parameters:
4150 * text: The default text to be in the entryfield widget. 4170 * text: The default text to be in the entryfield widget.
4151 * id: An ID to be used with WinWindowFromID() or 0L. 4171 * id: An ID to be used with dw_window_from_id() or 0L.
4152 */ 4172 */
4153 HWND API dw_entryfield_new(char *text, ULONG id) 4173 HWND API dw_entryfield_new(char *text, ULONG id)
4154 { 4174 {
4155 4175
4156 WindowData *blah = calloc(1, sizeof(WindowData)); 4176 WindowData *blah = calloc(1, sizeof(WindowData));
4174 4194
4175 /* 4195 /*
4176 * Create a new Entryfield (password) window (widget) to be packed. 4196 * Create a new Entryfield (password) window (widget) to be packed.
4177 * Parameters: 4197 * Parameters:
4178 * text: The default text to be in the entryfield widget. 4198 * text: The default text to be in the entryfield widget.
4179 * id: An ID to be used with WinWindowFromID() or 0L. 4199 * id: An ID to be used with dw_window_from_id() or 0L.
4180 */ 4200 */
4181 HWND API dw_entryfield_password_new(char *text, ULONG id) 4201 HWND API dw_entryfield_password_new(char *text, ULONG id)
4182 { 4202 {
4183 WindowData *blah = calloc(1, sizeof(WindowData)); 4203 WindowData *blah = calloc(1, sizeof(WindowData));
4184 HWND tmp = WinCreateWindow(HWND_OBJECT, 4204 HWND tmp = WinCreateWindow(HWND_OBJECT,
4201 4221
4202 /* 4222 /*
4203 * Create a new Combobox window (widget) to be packed. 4223 * Create a new Combobox window (widget) to be packed.
4204 * Parameters: 4224 * Parameters:
4205 * text: The default text to be in the combpbox widget. 4225 * text: The default text to be in the combpbox widget.
4206 * id: An ID to be used with WinWindowFromID() or 0L. 4226 * id: An ID to be used with dw_window_from_id() or 0L.
4207 */ 4227 */
4208 HWND API dw_combobox_new(char *text, ULONG id) 4228 HWND API dw_combobox_new(char *text, ULONG id)
4209 { 4229 {
4210 WindowData *blah = calloc(1, sizeof(WindowData)); 4230 WindowData *blah = calloc(1, sizeof(WindowData));
4211 HWND tmp = WinCreateWindow(HWND_OBJECT, 4231 HWND tmp = WinCreateWindow(HWND_OBJECT,
4238 4258
4239 /* 4259 /*
4240 * Create a new button window (widget) to be packed. 4260 * Create a new button window (widget) to be packed.
4241 * Parameters: 4261 * Parameters:
4242 * text: The text to be display by the static text widget. 4262 * text: The text to be display by the static text widget.
4243 * id: An ID to be used with WinWindowFromID() or 0L. 4263 * id: An ID to be used with dw_window_from_id() or 0L.
4244 */ 4264 */
4245 HWND API dw_button_new(char *text, ULONG id) 4265 HWND API dw_button_new(char *text, ULONG id)
4246 { 4266 {
4247 BubbleButton *bubble = calloc(sizeof(BubbleButton), 1); 4267 BubbleButton *bubble = calloc(sizeof(BubbleButton), 1);
4248 4268
4335 WinSetWindowPtr(tmp, QWP_USER, bubble); 4355 WinSetWindowPtr(tmp, QWP_USER, bubble);
4336 return tmp; 4356 return tmp;
4337 } 4357 }
4338 4358
4339 /* 4359 /*
4360 * Create a new bitmap button window (widget) to be packed from a file.
4361 * Parameters:
4362 * text: Bubble help text to be displayed.
4363 * id: An ID to be used with dw_window_from_id() or 0L.
4364 * filename: Name of the file, omit extention to have
4365 * DW pick the appropriate file extension.
4366 * (BMP on OS/2 or Windows, XPM on Unix)
4367 */
4368 HWND dw_bitmapbutton_new_from_file(char *text, unsigned long id, char filename)
4369 {
4370 /* TODO: Actually get it to draw the bitmap */
4371 BubbleButton *bubble = calloc(sizeof(BubbleButton), 1);
4372 HWND tmp = WinCreateWindow(HWND_OBJECT,
4373 WC_BUTTON,
4374 "",
4375 WS_VISIBLE | BS_PUSHBUTTON |
4376 BS_BITMAP | BS_AUTOSIZE |
4377 BS_NOPOINTERFOCUS,
4378 0,0,2000,1000,
4379 NULLHANDLE,
4380 HWND_TOP,
4381 id,
4382 NULL,
4383 NULL);
4384
4385 bubble->id = id;
4386 strncpy(bubble->bubbletext, text, BUBBLE_HELP_MAX - 1);
4387 bubble->bubbletext[BUBBLE_HELP_MAX - 1] = '\0';
4388 bubble->pOldProc = WinSubclassWindow(tmp, _BtProc);
4389
4390 WinSetWindowPtr(tmp, QWP_USER, bubble);
4391 return tmp;
4392 }
4393
4394 /*
4340 * Create a new spinbutton window (widget) to be packed. 4395 * Create a new spinbutton window (widget) to be packed.
4341 * Parameters: 4396 * Parameters:
4342 * text: The text to be display by the static text widget. 4397 * text: The text to be display by the static text widget.
4343 * id: An ID to be used with WinWindowFromID() or 0L. 4398 * id: An ID to be used with dw_window_from_id() or 0L.
4344 */ 4399 */
4345 HWND API dw_spinbutton_new(char *text, ULONG id) 4400 HWND API dw_spinbutton_new(char *text, ULONG id)
4346 { 4401 {
4347 WindowData *blah = calloc(sizeof(WindowData), 1); 4402 WindowData *blah = calloc(sizeof(WindowData), 1);
4348 HWND tmp = WinCreateWindow(HWND_OBJECT, 4403 HWND tmp = WinCreateWindow(HWND_OBJECT,
4368 4423
4369 /* 4424 /*
4370 * Create a new radiobutton window (widget) to be packed. 4425 * Create a new radiobutton window (widget) to be packed.
4371 * Parameters: 4426 * Parameters:
4372 * text: The text to be display by the static text widget. 4427 * text: The text to be display by the static text widget.
4373 * id: An ID to be used with WinWindowFromID() or 0L. 4428 * id: An ID to be used with dw_window_from_id() or 0L.
4374 */ 4429 */
4375 HWND API dw_radiobutton_new(char *text, ULONG id) 4430 HWND API dw_radiobutton_new(char *text, ULONG id)
4376 { 4431 {
4377 WindowData *blah = calloc(sizeof(WindowData), 1); 4432 WindowData *blah = calloc(sizeof(WindowData), 1);
4378 HWND tmp = WinCreateWindow(HWND_OBJECT, 4433 HWND tmp = WinCreateWindow(HWND_OBJECT,
4397 /* 4452 /*
4398 * Create a new slider window (widget) to be packed. 4453 * Create a new slider window (widget) to be packed.
4399 * Parameters: 4454 * Parameters:
4400 * vertical: TRUE or FALSE if slider is vertical. 4455 * vertical: TRUE or FALSE if slider is vertical.
4401 * increments: Number of increments available. 4456 * increments: Number of increments available.
4402 * id: An ID to be used with WinWindowFromID() or 0L. 4457 * id: An ID to be used with dw_window_from_id() or 0L.
4403 */ 4458 */
4404 HWND API dw_slider_new(int vertical, int increments, ULONG id) 4459 HWND API dw_slider_new(int vertical, int increments, ULONG id)
4405 { 4460 {
4406 WindowData *blah = calloc(1, sizeof(WindowData)); 4461 WindowData *blah = calloc(1, sizeof(WindowData));
4407 SLDCDATA sldcData = { 0, 0, 0, 0, 0 }; 4462 SLDCDATA sldcData = { 0, 0, 0, 0, 0 };
4430 /* 4485 /*
4431 * Create a new scrollbar window (widget) to be packed. 4486 * Create a new scrollbar window (widget) to be packed.
4432 * Parameters: 4487 * Parameters:
4433 * vertical: TRUE or FALSE if scrollbar is vertical. 4488 * vertical: TRUE or FALSE if scrollbar is vertical.
4434 * increments: Number of increments available. 4489 * increments: Number of increments available.
4435 * id: An ID to be used with WinWindowFromID() or 0L. 4490 * id: An ID to be used with dw_window_from_id() or 0L.
4436 */ 4491 */
4437 HWND API dw_scrollbar_new(int vertical, int increments, ULONG id) 4492 HWND API dw_scrollbar_new(int vertical, int increments, ULONG id)
4438 { 4493 {
4439 return WinCreateWindow(HWND_OBJECT, 4494 return WinCreateWindow(HWND_OBJECT,
4440 WC_SCROLLBAR, 4495 WC_SCROLLBAR,
4450 } 4505 }
4451 4506
4452 /* 4507 /*
4453 * Create a new percent bar window (widget) to be packed. 4508 * Create a new percent bar window (widget) to be packed.
4454 * Parameters: 4509 * Parameters:
4455 * id: An ID to be used with WinWindowFromID() or 0L. 4510 * id: An ID to be used with dw_window_from_id() or 0L.
4456 */ 4511 */
4457 HWND API dw_percent_new(ULONG id) 4512 HWND API dw_percent_new(ULONG id)
4458 { 4513 {
4459 WindowData *blah = calloc(1, sizeof(WindowData)); 4514 WindowData *blah = calloc(1, sizeof(WindowData));
4460 HWND tmp = WinCreateWindow(HWND_OBJECT, 4515 HWND tmp = WinCreateWindow(HWND_OBJECT,
4476 4531
4477 /* 4532 /*
4478 * Create a new checkbox window (widget) to be packed. 4533 * Create a new checkbox window (widget) to be packed.
4479 * Parameters: 4534 * Parameters:
4480 * text: The text to be display by the static text widget. 4535 * text: The text to be display by the static text widget.
4481 * id: An ID to be used with WinWindowFromID() or 0L. 4536 * id: An ID to be used with dw_window_from_id() or 0L.
4482 */ 4537 */
4483 HWND API dw_checkbox_new(char *text, ULONG id) 4538 HWND API dw_checkbox_new(char *text, ULONG id)
4484 { 4539 {
4485 BubbleButton *bubble = calloc(sizeof(BubbleButton), 1); 4540 BubbleButton *bubble = calloc(sizeof(BubbleButton), 1);
4486 HWND tmp = WinCreateWindow(HWND_OBJECT, 4541 HWND tmp = WinCreateWindow(HWND_OBJECT,
4503 } 4558 }
4504 4559
4505 /* 4560 /*
4506 * Create a new listbox window (widget) to be packed. 4561 * Create a new listbox window (widget) to be packed.
4507 * Parameters: 4562 * Parameters:
4508 * id: An ID to be used with WinWindowFromID() or 0L. 4563 * id: An ID to be used with dw_window_from_id() or 0L.
4509 * multi: Multiple select TRUE or FALSE. 4564 * multi: Multiple select TRUE or FALSE.
4510 */ 4565 */
4511 HWND API dw_listbox_new(ULONG id, int multi) 4566 HWND API dw_listbox_new(ULONG id, int multi)
4512 { 4567 {
4513 WindowData *blah = calloc(sizeof(WindowData), 1); 4568 WindowData *blah = calloc(sizeof(WindowData), 1);
4541 4596
4542 icon = WinLoadPointer(HWND_DESKTOP,NULLHANDLE,id); 4597 icon = WinLoadPointer(HWND_DESKTOP,NULLHANDLE,id);
4543 WinSendMsg(handle, WM_SETICON, (MPARAM)icon, 0); 4598 WinSendMsg(handle, WM_SETICON, (MPARAM)icon, 0);
4544 } 4599 }
4545 4600
4601 /* Internal function to load a bitmap from a file and return handles
4602 * to the bitmap, presentation space etc.
4603 */
4604 int _load_bitmap_file(char *file, HWND handle, HBITMAP *hbm, HDC *hdc, HPS *hps, unsigned long *width, unsigned long *height)
4605 {
4606 HFILE BitmapFileHandle = NULLHANDLE; /* handle for the file */
4607 ULONG OpenAction = 0;
4608 PBYTE BitmapFileBegin; /* pointer to the first byte of bitmap data */
4609 FILESTATUS BitmapStatus;
4610 ULONG cbRead;
4611 PBITMAPFILEHEADER2 pBitmapFileHeader;
4612 PBITMAPINFOHEADER2 pBitmapInfoHeader;
4613 ULONG ScanLines, ulFlags;
4614 HPS hps1;
4615 HDC hdc1;
4616 SIZEL sizl = { 0, 0 };
4617
4618 /* open bitmap file */
4619 DosOpen(file, &BitmapFileHandle, &OpenAction, 0L,
4620 FILE_ARCHIVED | FILE_NORMAL | FILE_READONLY,
4621 OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
4622 OPEN_SHARE_DENYNONE | OPEN_ACCESS_READONLY |
4623 OPEN_FLAGS_NOINHERIT, 0L);
4624
4625 if(!BitmapFileHandle)
4626 return 0;
4627
4628 /* find out how big the file is */
4629 DosQueryFileInfo(BitmapFileHandle, 1, &BitmapStatus,
4630 sizeof(BitmapStatus));
4631
4632 /* allocate memory to load the bitmap */
4633 DosAllocMem((PPVOID)&BitmapFileBegin, (ULONG)BitmapStatus.cbFile,
4634 PAG_READ | PAG_WRITE | PAG_COMMIT);
4635
4636 /* read bitmap file into memory buffer */
4637 DosRead(BitmapFileHandle, (PVOID)BitmapFileBegin,
4638 BitmapStatus.cbFile, &cbRead);
4639
4640 /* access first bytes as bitmap header */
4641 pBitmapFileHeader = (PBITMAPFILEHEADER2)BitmapFileBegin;
4642
4643 /* check if it's a valid bitmap data file */
4644 if((pBitmapFileHeader->usType != BFT_BITMAPARRAY) &&
4645 (pBitmapFileHeader->usType != BFT_BMAP))
4646 {
4647 /* free memory of bitmap file buffer */
4648 DosFreeMem(BitmapFileBegin);
4649 /* close the bitmap file */
4650 DosClose(BitmapFileHandle);
4651 return 0;
4652 }
4653
4654 /* check if it's a file with multiple bitmaps */
4655 if(pBitmapFileHeader->usType == BFT_BITMAPARRAY)
4656 {
4657 /* we'll just use the first bitmap and ignore the others */
4658 pBitmapFileHeader = &(((PBITMAPARRAYFILEHEADER2)BitmapFileBegin)->bfh2);
4659 }
4660
4661 /* set pointer to bitmap information block */
4662 pBitmapInfoHeader = &pBitmapFileHeader->bmp2;
4663
4664 /* find out if it's the new 2.0 format or the old format */
4665 /* and query number of lines */
4666 if(pBitmapInfoHeader->cbFix == sizeof(BITMAPINFOHEADER))
4667 ScanLines = (ULONG)((PBITMAPINFOHEADER)pBitmapInfoHeader)->cy;
4668 else
4669 ScanLines = pBitmapInfoHeader->cy;
4670
4671 /* now we need a presentation space, get it from static control */
4672 hps1 = WinGetPS(handle);
4673
4674 hdc1 = GpiQueryDevice(hps1);
4675 ulFlags = GpiQueryPS(hps1, &sizl);
4676
4677 *hdc = DevOpenDC(dwhab, OD_MEMORY, "*", 0L, NULL, hdc1);
4678 *hps = GpiCreatePS (dwhab, *hdc, &sizl, ulFlags | GPIA_ASSOC);
4679
4680 *width = pBitmapInfoHeader->cx; *height = pBitmapInfoHeader->cy;
4681
4682 /* create bitmap now using the parameters from the info block */
4683 *hbm = GpiCreateBitmap(*hps, pBitmapInfoHeader, 0L, NULL, NULL);
4684
4685 /* select the new bitmap into presentation space */
4686 GpiSetBitmap(*hps, *hbm);
4687
4688 /* now copy the bitmap data into the bitmap */
4689 GpiSetBitmapBits(*hps, 0L, ScanLines,
4690 BitmapFileBegin + pBitmapFileHeader->offBits,
4691 (PBITMAPINFO2)pBitmapInfoHeader);
4692
4693 WinReleasePS(hps1);
4694
4695 /* free memory of bitmap file buffer */
4696 DosFreeMem(BitmapFileBegin);
4697 /* close the bitmap file */
4698 DosClose(BitmapFileHandle);
4699 return 1;
4700 }
4701
4546 /* 4702 /*
4547 * Sets the bitmap used for a given static window. 4703 * Sets the bitmap used for a given static window.
4548 * Parameters: 4704 * Parameters:
4549 * handle: Handle to the window. 4705 * handle: Handle to the window.
4550 * id: An ID to be used to specify the icon. 4706 * id: An ID to be used to specify the icon,
4551 */ 4707 * (pass 0 if you use the filename param)
4552 void API dw_window_set_bitmap(HWND handle, ULONG id) 4708 * filename: a path to a file (Bitmap on OS/2 or
4709 * Windows and a pixmap on Unix, pass
4710 * NULL if you use the id param)
4711 */
4712 void API dw_window_set_bitmap(HWND handle, unsigned long id, char *filename)
4553 { 4713 {
4554 HBITMAP hbm; 4714 HBITMAP hbm;
4555 HPS hps = WinGetPS(handle); 4715 HPS hps;
4556 4716
4557 hbm = GpiLoadBitmap(hps, NULLHANDLE, id, 0, 0); 4717 /* Destroy any old bitmap data */
4718 _free_bitmap(handle);
4719
4720 /* If id is non-zero use the resource */
4721 if(id)
4722 {
4723 hps = WinGetPS(handle);
4724
4725 hbm = GpiLoadBitmap(hps, NULLHANDLE, id, 0, 0);
4726 }
4727 else if(filename)
4728 {
4729 HDC hdc;
4730 unsigned long width, height;
4731 char *file = alloca(strlen(filename) + 5);
4732
4733 if(!file)
4734 return;
4735
4736 strcpy(file, filename);
4737
4738 /* check if we can read from this file (it exists and read permission) */
4739 if(access(file, 04) != 0)
4740 {
4741 /* Try with .bmp extention */
4742 strcat(file, ".bmp");
4743 if(access(file, 04) != 0)
4744 return;
4745 }
4746
4747 if(!_load_bitmap_file(file, handle, &hbm, &hdc, &hps, &width, &height))
4748 return;
4749
4750 dw_window_set_data(handle, "_dw_hps", (void *)hps);
4751 dw_window_set_data(handle, "_dw_hdc", (void *)hdc);
4752 dw_window_set_data(handle, "_dw_width", (void *)width);
4753 dw_window_set_data(handle, "_dw_height", (void *)height);
4754 }
4755 else
4756 return;
4757
4558 WinSetWindowBits(handle,QWL_STYLE,SS_BITMAP,SS_BITMAP | 0x7f); 4758 WinSetWindowBits(handle,QWL_STYLE,SS_BITMAP,SS_BITMAP | 0x7f);
4559 WinSendMsg( handle, SM_SETHANDLE, MPFROMP(hbm), NULL ); 4759 WinSendMsg( handle, SM_SETHANDLE, MPFROMP(hbm), NULL );
4560 WinReleasePS(hps); 4760 if(id)
4761 WinReleasePS(hps);
4561 dw_window_set_data(handle, "_dw_bitmap", (void *)hbm); 4762 dw_window_set_data(handle, "_dw_bitmap", (void *)hbm);
4562 } 4763 }
4563 4764
4564 /* 4765 /*
4565 * Sets the text used for a given window. 4766 * Sets the text used for a given window.
6795 * Returns: 6996 * Returns:
6796 * A handle to a pixmap or NULL on failure. 6997 * A handle to a pixmap or NULL on failure.
6797 */ 6998 */
6798 HPIXMAP API dw_pixmap_new_from_file(HWND handle, char *filename) 6999 HPIXMAP API dw_pixmap_new_from_file(HWND handle, char *filename)
6799 { 7000 {
6800 HFILE BitmapFileHandle = NULLHANDLE; /* handle for the file */
6801 ULONG OpenAction = 0;
6802 PBYTE BitmapFileBegin; /* pointer to the first byte of bitmap data */
6803 FILESTATUS BitmapStatus;
6804 ULONG cbRead;
6805 PBITMAPFILEHEADER2 pBitmapFileHeader;
6806 PBITMAPINFOHEADER2 pBitmapInfoHeader;
6807 ULONG ScanLines, ulFlags;
6808 HPS hps;
6809 HDC hdc;
6810 SIZEL sizl = { 0, 0 };
6811 HPIXMAP pixmap; 7001 HPIXMAP pixmap;
6812 char *file = alloca(strlen(filename) + 5); 7002 char *file = alloca(strlen(filename) + 5);
6813 7003
6814 if(!file || !(pixmap = calloc(1,sizeof(struct _hpixmap)))) 7004 if(!file || !(pixmap = calloc(1,sizeof(struct _hpixmap))))
6815 return NULL; 7005 return NULL;
6826 free(pixmap); 7016 free(pixmap);
6827 return NULL; 7017 return NULL;
6828 } 7018 }
6829 } 7019 }
6830 7020
6831 /* open bitmap file */ 7021 /* Try to load the bitmap from file */
6832 DosOpen(filename, &BitmapFileHandle, &OpenAction, 0L, 7022 if(!_load_bitmap_file(file, handle, &pixmap->hbm, &pixmap->hdc, &pixmap->hps, &pixmap->width, &pixmap->height))
6833 FILE_ARCHIVED | FILE_NORMAL | FILE_READONLY,
6834 OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
6835 OPEN_SHARE_DENYNONE | OPEN_ACCESS_READONLY |
6836 OPEN_FLAGS_NOINHERIT, 0L);
6837 if(!BitmapFileHandle)
6838 { 7023 {
6839 free(pixmap); 7024 free(pixmap);
6840 return NULL; 7025 return NULL;
6841 } 7026 }
6842 7027
6843 /* find out how big the file is */ 7028 /* Success fill in other values */
6844 DosQueryFileInfo(BitmapFileHandle, 1, &BitmapStatus,
6845 sizeof(BitmapStatus));
6846
6847 /* allocate memory to load the bitmap */
6848 DosAllocMem((PPVOID)&BitmapFileBegin, (ULONG)BitmapStatus.cbFile,
6849 PAG_READ | PAG_WRITE | PAG_COMMIT);
6850
6851 /* read bitmap file into memory buffer */
6852 DosRead(BitmapFileHandle, (PVOID)BitmapFileBegin,
6853 BitmapStatus.cbFile, &cbRead);
6854
6855 /* access first bytes as bitmap header */
6856 pBitmapFileHeader = (PBITMAPFILEHEADER2)BitmapFileBegin;
6857
6858 /* check if it's a valid bitmap data file */
6859 if((pBitmapFileHeader->usType != BFT_BITMAPARRAY) &&
6860 (pBitmapFileHeader->usType != BFT_BMAP))
6861 {
6862 /* free memory of bitmap file buffer */
6863 DosFreeMem(BitmapFileBegin);
6864 /* close the bitmap file */
6865 DosClose(BitmapFileHandle);
6866 return NULL;
6867 }
6868
6869 /* check if it's a file with multiple bitmaps */
6870 if(pBitmapFileHeader->usType == BFT_BITMAPARRAY)
6871 {
6872 /* we'll just use the first bitmap and ignore the others */
6873 pBitmapFileHeader = &(((PBITMAPARRAYFILEHEADER2)BitmapFileBegin)->bfh2);
6874 }
6875
6876 /* set pointer to bitmap information block */
6877 pBitmapInfoHeader = &pBitmapFileHeader->bmp2;
6878
6879 /* find out if it's the new 2.0 format or the old format */
6880 /* and query number of lines */
6881 if(pBitmapInfoHeader->cbFix == sizeof(BITMAPINFOHEADER))
6882 ScanLines = (ULONG)((PBITMAPINFOHEADER)pBitmapInfoHeader)->cy;
6883 else
6884 ScanLines = pBitmapInfoHeader->cy;
6885
6886 /* now we need a presentation space, get it from static control */
6887 hps = WinGetPS(handle);
6888
6889 hdc = GpiQueryDevice(hps);
6890 ulFlags = GpiQueryPS(hps, &sizl);
6891
6892 pixmap->handle = handle; 7029 pixmap->handle = handle;
6893 pixmap->hdc = DevOpenDC(dwhab, OD_MEMORY, "*", 0L, NULL, hdc);
6894 pixmap->hps = GpiCreatePS (dwhab, pixmap->hdc, &sizl, ulFlags | GPIA_ASSOC);
6895
6896 pixmap->width = pBitmapInfoHeader->cx; pixmap->height = pBitmapInfoHeader->cy;
6897
6898 /* create bitmap now using the parameters from the info block */
6899 pixmap->hbm = GpiCreateBitmap(pixmap->hps, pBitmapInfoHeader, 0L, NULL, NULL);
6900
6901 /* select the new bitmap into presentation space */
6902 GpiSetBitmap(pixmap->hps, pixmap->hbm);
6903
6904 /* now copy the bitmap data into the bitmap */
6905 GpiSetBitmapBits(pixmap->hps, 0L, ScanLines,
6906 BitmapFileBegin + pBitmapFileHeader->offBits,
6907 (PBITMAPINFO2)pBitmapInfoHeader);
6908
6909 /* free memory of bitmap file buffer */
6910 DosFreeMem(BitmapFileBegin);
6911 /* close the bitmap file */
6912 DosClose(BitmapFileHandle);
6913 7030
6914 return pixmap; 7031 return pixmap;
6915 } 7032 }
6916 7033
6917 /* 7034 /*