/* * readInBitmapFile - read in the bitmap file, initialize the draw area and * the view window */ static BOOL readInBitmapFile( char *fullname ) { bitmap_info bmi; HBITMAP hbitmap; HCURSOR prevcursor; prevcursor = SetCursor( LoadCursor( NULL, IDC_WAIT ) ); hbitmap = ReadBitmapFile( HMainWindow, fullname, &bmi ); SetCursor( prevcursor ); if( hbitmap == (HBITMAP)NULL ) { if( bmi.u.bm_info != NULL && bmi.u.bm_info->bmiHeader.biBitCount > 32 ) { /* Was 8 */ WImgEditError( WIE_ERR_TOO_MANY_COLORS, fullname ); } else { WImgEditError( WIE_ERR_BAD_BITMAP_FILE, fullname ); } if( bmi.u.bm_info != NULL ) { MemFree( bmi.u.bm_info ); } return( FALSE ); } return( doReadInBitmapFile( hbitmap, &bmi, fullname, NULL, NULL ) ); } /* readInBitmapFile */
/* * SelectIcon - Sets the current icon */ void SelectIcon( short index ) { img_node *node; if (activeImage->imgtype != ICON_IMG) { WImgEditError( WIE_ERR_BAD_IMAGE_TYPE, NULL ); return; } node = GetNthIcon( activeImage->hwnd, index ); if (node) { SelIconUndoStack( activeImage->hwnd, index ); ResetDrawArea( node ); RePositionViewWnd( node ); SetNumColours( 1<<(node->bitcount) ); SetMenus( node ); } else { WImgEditError( WIE_ERR_BAD_SELECTION, NULL ); return; } activeImage = node; DisplayImageText( activeImage ); } /* SelectIcon */
/* * WinMain - main entry point */ int WINMAINENTRY WinMain( HINSTANCE currinst, HINSTANCE previnst, LPSTR cmdline, int cmdshow ) { MSG msg; cmdline = cmdline; #if defined( __NT__ ) && !defined( __WATCOMC__ ) _argc = __argc; _argv = __argv; #endif WRInit(); if( _argc > 1 ) { parseArgs( _argc, _argv ); } if( !imgEditInit( currinst, previnst, cmdshow ) ) { if( ImgedIsDDE ) { IEDDEDumpConversation( currinst ); } return( 0 ); } if( ImgedIsDDE ) { if( IEDDEStart( currinst ) ) { if( !IEDDEStartConversation() ) { WImgEditError( WIE_DDE_INIT_FAILED, NULL ); PostMessage( HMainWindow, WM_CLOSE, (WPARAM)1, 0 ); } } else { WImgEditError( WIE_DDE_INIT_FAILED, NULL ); PostMessage( HMainWindow, WM_CLOSE, (WPARAM)1, 0 ); } } IEEnableMenuInput( TRUE ); if( _argc > 1 ) { parseCmdLine( _argc, _argv ); } while( GetMessage( &msg, (HWND)NULL, 0, 0 ) ) { if( !TranslateMDISysAccel( ClientWindow, &msg ) && !TranslateAccelerator( HMainWindow, hAccel, &msg ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } } if( ImgedIsDDE ) { IEDDEEndConversation(); IEDDEEnd(); } imgEditFini(); WRFini(); return( 1 ); } /* WinMain */
/* * ReadCursorFromData - read the cursor data and set up structures */ BOOL ReadCursorFromData( void *data, char *fname, WRInfo *info, WResLangNode *lnode ) { unsigned pos; an_img_file *cursorfile; an_img *cursor; BOOL ret; pos = 0; cursorfile = ImageOpenData( (BYTE *)data, &pos ); if( cursorfile == NULL ) { WImgEditError( WIE_ERR_BAD_CURSOR_DATA, fname ); return( FALSE ); } cursor = ImgResourceToImgData( (BYTE *)data, &pos, cursorfile, 0 ); ret = doReadCursor( fname, cursorfile, cursor, info, lnode ); if( ret ) { SetupMenuAfterOpen(); } return( ret ); } /* ReadCursorFromData */
/* * CloseCurrentImage - Gets the current image the user is editing and deletes * the node from the linked list and sends a message to * destroy the mdi child (this will activate another * child then). */ void CloseCurrentImage( HWND hwnd ) { img_node *node; char file_name[ _MAX_PATH ]; BOOL ret; node = SelectImage( hwnd ); if (!node) return; ret = DestroyWindow( _wpi_getframe(node->viewhwnd) ); GetFnameFromPath( node->fname, file_name ); DeleteUndoStack( hwnd ); if ( !DeleteNode(hwnd) ) { WImgEditError( WIE_ERR_BAD_HWND, WIE_INTERNAL_002 ); return; } DeleteActiveImage(); ClearImageText(); GrayEditOptions(); PrintHintTextByID( WIE_FILEHASBEENCLOSED, file_name ); SetWindowText( _wpi_getframe(HMainWindow), IEAppTitle ); #ifdef __OS2_PM__ ret = DestroyWindow( _wpi_getframe(hwnd) ); #else SendMessage(ClientWindow, WM_MDIDESTROY, (WPARAM)hwnd, 0L); #endif } /* CloseCurrentImage */
/* * reallyOpenImage */ static int reallyOpenImage( char *fname ) { char filename[_MAX_FNAME + _MAX_EXT]; switch( imgType ) { case BITMAP_IMG: if( !readInBitmapFile( fname ) ) { return( FALSE ); } break; case ICON_IMG: if( !readInIconFile( fname ) ) { return( FALSE ); } break; case CURSOR_IMG: if( !readInCursorFile( fname ) ) { return( FALSE ); } break; case RESOURCE_IMG: if( !readInResourceFile( fname ) ) { return( FALSE ); } break; default: GetFnameFromPath( fname, filename ); WImgEditError( WIE_ERR_BAD_FILE_EXT, filename ); imgType = BITMAP_IMG; return( FALSE ); } return( imgType ); } /* reallyOpenImage */
/* * closeTheImage - get the current image the user is editing, delete * the node from the linked list, and send a message to * destroy the MDI child (this will activate another child) */ static void closeTheImage( img_node *node ) { char file_name[_MAX_PATH]; BOOL ret; HWND hwnd; if( node == NULL ) { return; } ret = _wpi_destroywindow( _wpi_getframe( node->viewhwnd ) ); GetFnameFromPath( node->fname, file_name ); hwnd = node->hwnd; DeleteUndoStack( hwnd ); if( !DeleteNode( hwnd ) ) { WImgEditError( WIE_ERR_BAD_HWND, WIE_INTERNAL_002 ); return; } #ifdef __OS2_PM__ ret = DestroyWindow( _wpi_getframe( hwnd ) ); #else SendMessage( ClientWindow, WM_MDIDESTROY, (WPARAM)hwnd, 0L ); #endif } /* closeTheImage */
/* * LoadColorPalette - load a palette */ BOOL LoadColorPalette( void ) { char fname[_MAX_PATH]; char filename[_MAX_FNAME + _MAX_EXT]; a_pal_file *pal_file; FILE *fp; WORD file_type; if( !getOpenPalName( fname ) ) { if( CommDlgExtendedError() == FNERR_INVALIDFILENAME ) { WImgEditError( WIE_ERR_BAD_FILENAME, fname ); return( FALSE ); } return( TRUE ); } pal_file = MemAlloc( sizeof( a_pal_file ) ); fp = fopen( fname, "rb" ); if( fp == NULL ) { WImgEditError( WIE_ERR_FILE_NOT_OPENED, fname ); return( FALSE ); } GetFnameFromPath( fname, filename ); fseek( fp, 0L, SEEK_SET ); fread( &file_type, sizeof( WORD ), 1, fp ); if( file_type != PALETTE_FILE ) { WImgEditError( WIE_ERR_BAD_PALFILE, filename ); fclose( fp ); return( FALSE ); } fseek( fp, 0L, SEEK_SET ); fread( pal_file, sizeof( a_pal_file ), 1, fp ); fclose( fp ); SetNewPalette( pal_file ); PrintHintTextByID( WIE_PALETTELOADEDFROM, filename ); MemFree( pal_file ); return( TRUE ); } /* LoadColorPalette */
/* * GetNthIcon - get the nth (index th) icon from the linked list */ img_node *GetNthIcon( HWND hwnd, short index ) { img_node *node; index_table *tableptr; int i; node = imgHead; tableptr = indexHead; while( node != NULL ) { if( node->hwnd == hwnd ) { break; } node = node->next; tableptr = tableptr->next; } if( node == NULL ) { WImgEditError( WIE_ERR_BAD_HWND, WIE_INTERNAL_006 ); return( NULL ); } if( index > node->num_of_images ) { WImgEditError( WIE_ERR_BAD_ICONINDEX, WIE_INTERNAL_007 ); return( NULL ); } for( i = 1; i <= index; i++ ) { if( node->nexticon != NULL ) { node = node->nexticon; } else { break; } } tableptr->index = min( i, index ); return( node ); } /* GetNthIcon */
/* * readInCursorFile - Read the cursor file and set up structures. */ static BOOL readInCursorFile( char *fname ) { FILE *fp; an_img_file *cursorfile; an_img *cursor; fp = fopen( fname, "rb" ); if (!fp) { WImgEditError( WIE_ERR_FILE_NOT_OPENED, fname ); return( FALSE ); } cursorfile = ImageOpen( fp ); if (!cursorfile) { fclose( fp ); WImgEditError( WIE_ERR_BAD_CURSOR_FILE, fname ); return( FALSE ); } cursor = ImgResourceToImg( fp, cursorfile, 0 ); fclose( fp ); return( DoReadCursor( fname, cursorfile, cursor, NULL, NULL ) ); } /* readInCursorFile */
/* * OpenImage - get the filename of the file to open * - depending on the extension, set the type (.ico, .bmp, .cur) and call the * appropriate function to open it */ int OpenImage( HANDLE hDrop ) { char fname[_MAX_PATH]; int rv = FALSE; if( NULL == hDrop ) { /* * Not doing a drag-drop */ if( !getOpenFName( fname ) ) { if( CommDlgExtendedError() == FNERR_INVALIDFILENAME ) { WImgEditError( WIE_ERR_BAD_FILENAME, fname ); return( FALSE ); } return( FALSE ); } rv = reallyOpenImage( fname ); } else { /* * hDrop is only ever !NULL when we're dealing with a WM_DROPFILES * message, and that only happens with __NT__ */ #ifdef __NT__ int nFiles = DragQueryFile( hDrop, 0xFFFFFFFF, NULL, 0 ); int i; for( i = 0, rv = TRUE; rv && i < nFiles; i++ ) { DragQueryFile( hDrop, i, fname, _MAX_PATH - 1 ); imgType = getImageTypeFromFilename( fname ); rv = reallyOpenImage( fname ); } #endif } if( rv ) { SetupMenuAfterOpen(); } return( rv ); } /* OpenImage */
/* * FocusOnImage - Selects one of the mdi children. */ void FocusOnImage( HWND hwnd ) { char current_file[ _MAX_PATH ]; char *text; if (activeImage) { RedrawPrevClip(activeImage->hwnd); SetRectExists( FALSE ); } activeImage = SelectImage( hwnd ); if (!activeImage) { WImgEditError( WIE_ERR_BAD_HWND, WIE_INTERNAL_001 ); return; } ResetViewWindow( activeImage->viewhwnd ); CreateDrawnImage( activeImage ); SetMenus( activeImage ); SetNumColours( 1<<(activeImage->bitcount) ); SetHotSpot( activeImage ); DisplayImageText( activeImage ); CheckForUndo( activeImage ); GetFnameFromPath( activeImage->fname, current_file ); text = (char *) MemAlloc( strlen( IEAppTitle ) + strlen( current_file ) + 3 + 1 ); if( text ) { strcpy( text, IEAppTitle ); strcat( text, " - " ); strcat( text, current_file ); _wpi_setwindowtext( _wpi_getframe(HMainWindow), text ); MemFree( text ); } #ifndef __OS2_PM__ RedrawWindow( hwnd, NULL, NULL, RDW_UPDATENOW ); #endif } /* FocusOnImage */
/* * OpenFileOnStart - open a file on program startup */ void OpenFileOnStart( char *fname ) { int namelen; char ext[_MAX_EXT]; FILE *fp; fp = fopen( fname, "r" ); if( fp == NULL ) { if( OpenNewFiles ) { if( NewImage( UNDEF_IMG, fname ) ) { return; } } WImgEditError( WIE_ERR_STARTUP_FNO, fname ); return; } fclose( fp ); namelen = strlen( fname ); strcpy( ext, &fname[namelen - 3] ); if( stricmp( ext, "bmp" ) == 0 ) { if( !readInBitmapFile( fname ) ) { return; } } else if( stricmp( ext, "ico" ) == 0 ) { if( !readInIconFile( fname ) ) { return; } } else if( stricmp( ext, "cur" ) == 0 ) { if( !readInCursorFile( fname ) ) { return; } } else { return; } SetupMenuAfterOpen(); } /* OpenFileOnStart */
/* * ReadBitmapFromData */ BOOL ReadBitmapFromData( void *data, char *fullname, WRInfo *info, WResLangNode *lnode ) { bitmap_info bmi; HBITMAP hbitmap; HCURSOR prevcursor; BOOL ret; prevcursor = SetCursor( LoadCursor( NULL, IDC_WAIT ) ); hbitmap = BitmapFromData( data, &bmi ); SetCursor( prevcursor ); if( hbitmap == (HBITMAP)NULL ) { WImgEditError( WIE_ERR_BAD_BITMAP_DATA, fullname ); return( FALSE ); } ret = doReadInBitmapFile( hbitmap, &bmi, fullname, info, lnode ); if( ret ) { SetupMenuAfterOpen(); } return( ret ); } /* ReadBitmapFromData */
/* * readInIconFile - read the icon file and set up structures */ static BOOL readInIconFile( char *fname ) { FILE *fp; an_img_file *iconfile; img_node *node; int num_of_images; HDC hdc; int i; an_img *icon; char filename[_MAX_FNAME + _MAX_EXT]; fp = fopen( fname, "rb" ); if( fp == NULL ) { WImgEditError( WIE_ERR_FILE_NOT_OPENED, fname ); return( FALSE ); } GetFnameFromPath( fname, filename ); #ifdef JAMIE { char msg[80]; sprintf( msg, "Jamie: IconHeader size = %d", sizeof( an_img_file ) ); MessageBox( HMainWindow, msg, "FYI", MB_OK ); } #endif iconfile = ImageOpen( fp ); if( iconfile == NULL ) { fclose( fp ); WImgEditError( WIE_ERR_BAD_ICON_FILE, filename ); return( FALSE ); } num_of_images = iconfile->count; #if 0 /* See biBitCount test below... */ for( i = 0; i < num_of_images; i++ ) { if( iconfile->resources[i].color_count != 2 && iconfile->resources[i].color_count != 8 && iconfile->resources[i].color_count != 16 && iconfile->resources[i].color_count != 0 ) { WImgEditError( WIE_ERR_BAD_ICON_CLR, filename ); ImageClose( iconfile ); fclose( fp ); return( FALSE ); } } #endif node = MemAlloc( sizeof( img_node ) * num_of_images ); hdc = GetDC( NULL ); for( i = 0; i < num_of_images; i++ ) { icon = ImgResourceToImg( fp, iconfile, i ); if( icon->bm->bmiHeader.biBitCount != 4 && icon->bm->bmiHeader.biBitCount != 1 && icon->bm->bmiHeader.biBitCount != 8 ) { WImgEditError( WIE_ERR_BAD_ICON_CLR, filename ); ReleaseDC( NULL, hdc ); ImageFini( icon ); ImageClose( iconfile ); fclose( fp ); MemFree( node ); return( FALSE ); } node[i].imgtype = ICON_IMG; node[i].bitcount = icon->bm->bmiHeader.biBitCount; node[i].width = icon->bm->bmiHeader.biWidth; node[i].height = icon->bm->bmiHeader.biHeight; node[i].hotspot.x = 0; node[i].hotspot.y = 0; node[i].handbitmap = ImgToAndBitmap( hdc, icon ); node[i].hxorbitmap = ImgToXorBitmap( hdc, icon ); node[i].num_of_images = num_of_images; node[i].viewhwnd = NULL; node[i].wrinfo = NULL; node[i].lnode = NULL; if( i > 0 ) { node[i - 1].nexticon = &node[i]; } node[i].issaved = TRUE; node[i].next = NULL; strcpy( node[i].fname, strupr( fname ) ); ImageFini( icon ); } node[i - 1].nexticon = NULL; ReleaseDC( NULL, hdc ); ImageClose( iconfile ); fclose( fp ); WriteIconLoadedText( filename, node->num_of_images ); CreateNewDrawPad( node ); MemFree( node ); return( TRUE ); } /* readInIconFile */
/* * doReadInBitmapFile */ static BOOL doReadInBitmapFile( HBITMAP hbitmap, bitmap_info *bmi, char *fullname, WRInfo *info, WResLangNode *lnode ) { HBITMAP oldbmp1; HBITMAP oldbmp2; img_node node; char filename[_MAX_FNAME + _MAX_EXT]; BITMAPINFOHEADER *h; HDC hdc; HDC srcdc; HDC destdc; GetFnameFromPath( fullname, filename ); if( hbitmap != NULL ) { h = &bmi->u.bm_info->bmiHeader; if( h->biWidth > MAX_DIM || h->biHeight > MAX_DIM ) { WImgEditError( WIE_ERR_BITMAP_TOO_BIG, filename ); MemFree( bmi->u.bm_info ); DeleteObject( hbitmap ); return( FALSE ); } #if 0 /* Should not be an error... */ else if( h->biBitCount > 8 ) { WImgEditError( WIE_ERR_TOO_MANY_COLOURS, filename ); MemFree( bmi->u.bm_info ); DeleteObject( hbitmap ); return( FALSE ); } #endif node.imgtype = BITMAP_IMG; node.width = bmi->u.bm_info->bmiHeader.biWidth; node.height = bmi->u.bm_info->bmiHeader.biHeight; node.bitcount = bmi->u.bm_info->bmiHeader.biBitCount; node.hotspot.x = 0; node.hotspot.y = 0; node.num_of_images = 1; node.nexticon = NULL; node.issaved = TRUE; if( node.bitcount == 1 ) { hdc = GetDC( NULL ); srcdc = CreateCompatibleDC( hdc ); destdc = CreateCompatibleDC( hdc ); ReleaseDC( NULL, hdc ); node.hxorbitmap = CreateCompatibleBitmap( destdc, node.width, node.height ); oldbmp1 = SelectObject( srcdc, hbitmap ); oldbmp2 = SelectObject( destdc, node.hxorbitmap ); BitBlt( destdc, 0, 0, node.width, node.height, srcdc, 0, 0, SRCCOPY ); SelectObject( srcdc, oldbmp1 ); SelectObject( destdc, oldbmp2 ); DeleteDC( srcdc ); DeleteDC( destdc ); DeleteObject( hbitmap ); } else { node.hxorbitmap = hbitmap; } strcpy( node.fname, fullname ); PrintHintTextByID( WIE_OPENEDTEXT, filename ); node.wrinfo = info; node.lnode = lnode; MakeBitmap( &node, FALSE ); CreateNewDrawPad( &node ); MemFree( bmi->u.bm_info ); return( TRUE ); } return( FALSE ); } /* doReadInBitmapFile */
/* * ReadIconFromData - read the icon data and set up structures */ BOOL ReadIconFromData( void *data, char *fname, WRInfo *info, WResLangNode *lnode ) { unsigned pos; an_img_file *iconfile; img_node *node; int num_of_images; HDC hdc; int i; an_img *icon; char filename[_MAX_FNAME + _MAX_EXT]; pos = 0; GetFnameFromPath( fname, filename ); iconfile = ImageOpenData( (BYTE *)data, &pos ); if( iconfile == NULL ) { WImgEditError( WIE_ERR_BAD_ICON_DATA, filename ); return( FALSE ); } num_of_images = iconfile->count; #if 0 /* See biBitCount test below... */ for( i = 0; i < num_of_images; i++ ) { if( iconfile->resources[i].color_count != 2 && iconfile->resources[i].color_count != 8 && iconfile->resources[i].color_count != 16 && iconfile->resources[i].color_count != 0 ) { WImgEditError( WIE_ERR_BAD_ICON_CLR, filename ); ImageClose( iconfile ); return( FALSE ); } } #endif node = MemAlloc( sizeof( img_node ) * num_of_images ); hdc = GetDC( NULL ); for( i = 0; i < num_of_images; i++ ) { icon = ImgResourceToImgData( (BYTE *)data, &pos, iconfile, i ); if( icon->bm->bmiHeader.biBitCount != 4 && icon->bm->bmiHeader.biBitCount != 1 && icon->bm->bmiHeader.biBitCount != 8 ) { WImgEditError( WIE_ERR_BAD_ICON_CLR, filename ); ReleaseDC( NULL, hdc ); ImageFini( icon ); ImageClose( iconfile ); MemFree( node ); return( FALSE ); } node[i].imgtype = ICON_IMG; node[i].bitcount = icon->bm->bmiHeader.biBitCount; node[i].width = icon->bm->bmiHeader.biWidth; node[i].height = icon->bm->bmiHeader.biHeight; node[i].hotspot.x = 0; node[i].hotspot.y = 0; node[i].handbitmap = ImgToAndBitmap( hdc, icon ); node[i].hxorbitmap = ImgToXorBitmap( hdc, icon ); node[i].num_of_images = num_of_images; node[i].viewhwnd = NULL; if( i > 0 ) { node[i - 1].nexticon = &node[i]; } node[i].wrinfo = NULL; node[i].lnode = NULL; if( i == 0 ) { node[i].wrinfo = info; node[i].lnode = lnode; } node[i].issaved = TRUE; node[i].next = NULL; strcpy( node[i].fname, strupr( fname ) ); ImageFini( icon ); } node[i - 1].nexticon = NULL; ReleaseDC( NULL, hdc ); ImageClose( iconfile ); WriteIconLoadedText( filename, node->num_of_images ); CreateNewDrawPad( node ); MemFree( node ); SetupMenuAfterOpen(); return( TRUE ); } /* ReadIconFromData */