1. Disabled button with XPMs ...one solution
- Posted by Jonas Temple <jtemple at yhti.net> Apr 03, 2003
- 370 views
I posted a question earlier about using XPMs with disabled picturebuttons in a toolbar. Using standard win32lib/IDE the disabled button has a dark grey square, not a shadow of the real image. I did some digging and I noticed that Judith's IDE has disabled buttons using XPMs with the shadowed image displaying correctly. I then discovered that she was using two buttons, hiding or showing the disabled/enabled button as appropriate. This also meant she had to create another version of her XPM for the disabled image. I did something similar, presented here for your consideration. First, I created a routine that would set all but the transparent color in a Euphoria bitmap to a user specified color. This function accepts a Euphoria bitmap, the transparent color (tuple) used in the bitmap and a {r,g,b} sequence for the new color: function greyEuBmp(sequence bmp, atom trans_color, sequence grey_color) atom tuple for i = 1 to length(bmp[1]) do tuple = (bmp[1][i][1] + (bmp[1][i][2] * 256) + (bmp[1][i][3] * 256 * 256)) if tuple != trans_color then bmp[1][i] = grey_color end if end for return bmp end function Then I set up a routine to create the enabled/disabled bitmaps for my buttons: procedure drawButtons() sequence wrk_bmp atom bitmap -- If bitmaps already exist delete if edit_e then deleteObject(edit_e) deleteObject(edit_d) deleteObject(save_e) deleteObject(save_d) deleteObject(undo_e) deleteObject(undo_d) deleteObject(copy_e) deleteObject(copy_d) deleteObject(cut_e) deleteObject(cut_d) deleteObject(paste_e) deleteObject(paste_d) end if setTransparentColor(getSysColor(COLOR_BTNFACE)) -- Create the new and exit bitmaps bitmap = createDIB(xpmToEuBmp(new_xpm)) setBitmap(NewPB, bitmap) bitmap = createDIB(xpmToEuBmp(exit_xpm)) setBitmap(ExitPB, bitmap) -- Create the bitmaps for the edit button wrk_bmp = xpmToEuBmp(edit_xpm) edit_e = createDIB(wrk_bmp) wrk_bmp = greyEuBmp(wrk_bmp, getSysColor(COLOR_BTNFACE),{128,128,128}) edit_d = createDIB(wrk_bmp) if isEnabled(FileEditMI) then setSoftEnable(EditPB, True, edit_e) else setSoftEnable(EditPB, False, edit_d) end if -- Create the bitmaps for the save button wrk_bmp = xpmToEuBmp(save_xpm) save_e = createDIB(wrk_bmp) wrk_bmp = greyEuBmp(wrk_bmp, getSysColor(COLOR_BTNFACE),{128,128,128}) save_d = createDIB(wrk_bmp) if isEnabled(FileSaveMI) then setSoftEnable(SavePB, True, save_e) else setSoftEnable(SavePB, False, save_d) end if -- Create the bitmaps for the undo button wrk_bmp = xpmToEuBmp(undo_xpm) undo_e = createDIB(wrk_bmp) wrk_bmp = greyEuBmp(wrk_bmp, getSysColor(COLOR_BTNFACE),{128,128,128}) undo_d = createDIB(wrk_bmp) if isEnabled(EditUndoMI) then setSoftEnable(UndoPB, True, undo_e) else setSoftEnable(UndoPB, False, undo_d) end if -- Create the bitmaps for the copy button wrk_bmp = xpmToEuBmp(copy_xpm) copy_e = createDIB(wrk_bmp) wrk_bmp = greyEuBmp(wrk_bmp, getSysColor(COLOR_BTNFACE),{128,128,128}) copy_d = createDIB(wrk_bmp) if isEnabled(EditCopyMI) then setSoftEnable(CopyPB, True, copy_e) else setSoftEnable(CopyPB, False, copy_d) end if -- Create the bitmaps for the cut button wrk_bmp = xpmToEuBmp(cut_xpm) cut_e = createDIB(wrk_bmp) wrk_bmp = greyEuBmp(wrk_bmp, getSysColor(COLOR_BTNFACE),{128,128,128}) cut_d = createDIB(wrk_bmp) if isEnabled(EditCutMI) then setSoftEnable(CutPB, True, cut_e) else setSoftEnable(CutPB, False, cut_d) end if -- Create the bitmaps for the paste button wrk_bmp = xpmToEuBmp(paste_xpm) paste_e = createDIB(wrk_bmp) wrk_bmp = greyEuBmp(wrk_bmp, getSysColor(COLOR_BTNFACE),{128,128,128}) paste_d = createDIB(wrk_bmp) if isEnabled(EditPasteMI) then setSoftEnable(PastePB, True, paste_e) else setSoftEnable(PastePB, False, paste_d) end if end procedure With the prior code, you can create a "shadowed" version of any xpm without having to do it by hand. Then, I created the setSoftEnable() routine that would set the image for the button and assign a w32HEvent routine that blocked any message except WM_PAINT and BM_SETIMAGE: procedure setSoftEnable(atom id, atom cond, atom bitmap) setBitmap(id, bitmap) if cond then setHandler(id, w32HEvent, -1) else setHandler(id, w32HEvent, routine_id("disabled")) end if end procedure Finally, the event handler: procedure disabled(integer self, integer event, sequence params)--params is ( int iMsg, atom wParm, atom lParm ) if params[1] != WM_PAINT and params[1] != BM_SETIMAGE then returnValue(True) end if end procedure The end effect is I have a disabled button (soft disabled) that has the shadowed image that I'm looking for. Of course you don't need this code if you're using .bmp for the images since disabling the button shadows the image correctly. As always, I invite any comments, criticisms, suggestions, questions as to why I'm coding, etc.............. :) Jonas