From fb1900a61f16ff86f1c77a7788efd185305c511b Mon Sep 17 00:00:00 2001 From: scuri Date: Fri, 12 Nov 2010 00:39:43 +0000 Subject: *** empty log message *** --- html/examples/im_paint.c | 245 +++++++++++++++++++++++++++++++++++++++++++++++ html/examples/im_view.c | 48 ++++------ 2 files changed, 264 insertions(+), 29 deletions(-) create mode 100644 html/examples/im_paint.c (limited to 'html/examples') diff --git a/html/examples/im_paint.c b/html/examples/im_paint.c new file mode 100644 index 0000000..eb69e16 --- /dev/null +++ b/html/examples/im_paint.c @@ -0,0 +1,245 @@ +#include +#include +#include +#include +#include +#include + +#include +#include + +/* TODO: + cdCanvasPixel is not apropriate, must use cdCanvasLine for continuity. + if resized, the content is lost + SaveAs is not implemented +*/ + +static int cbCanvasRepaint(Ihandle* iup_canvas) +{ + cdCanvas* dbuffer_canvas = (cdCanvas*)IupGetAttribute(iup_canvas, "cdCanvasDBuf"); + + cdCanvasActivate(dbuffer_canvas); + cdCanvasFlush(dbuffer_canvas); + + return IUP_DEFAULT; +} + +static int drawing = 0; + +static long GetFgColor(Ihandle* ih) +{ + int r, g, b; + Ihandle* fgcolor_but = IupGetDialogChild(ih, "fgcolor_but"); + sscanf(IupGetAttribute(fgcolor_but, "BGCOLOR"), "%d %d %d", &r, &g, &b); + return cdEncodeColor((unsigned char)r, (unsigned char)g, (unsigned char)b); +} + +static int cbCanvasButton(Ihandle* ih, int but, int pressed, int x, int y) +{ + if (but == IUP_BUTTON1 && pressed) + { + cdCanvas* dbuffer_canvas = (cdCanvas*)IupGetAttribute(ih, "cdCanvasDBuf"); + long fgcolor = GetFgColor(ih); + + cdCanvasActivate(dbuffer_canvas); + cdCanvasPixel(dbuffer_canvas, x, cdCanvasInvertYAxis(dbuffer_canvas, y), fgcolor); + cdCanvasFlush(dbuffer_canvas); + drawing = 1; + } + else + drawing = 0; + + return IUP_DEFAULT; +} + +static int cbCanvasMotion(Ihandle *ih, int x, int y, char *status) +{ + if (iup_isbutton1(status) && drawing) + { + cdCanvas* dbuffer_canvas = (cdCanvas*)IupGetAttribute(ih, "cdCanvasDBuf"); + long fgcolor = GetFgColor(ih); + + cdCanvasActivate(dbuffer_canvas); + cdCanvasPixel(dbuffer_canvas, x, cdCanvasInvertYAxis(dbuffer_canvas, y), fgcolor); + cdCanvasFlush(dbuffer_canvas); + } + + return IUP_DEFAULT; +} + +static int cbCanvasMap(Ihandle* ih) +{ + cdCanvas* cd_canvas = cdCreateCanvas(CD_IUP, ih); + cdCanvas* dbuffer_canvas = cdCreateCanvas(CD_DBUFFER, cd_canvas); + IupSetAttribute(IupGetDialog(ih), "cdCanvas", (char*)cd_canvas); + IupSetAttribute(IupGetDialog(ih), "cdCanvasDBuf", (char*)dbuffer_canvas); + return IUP_DEFAULT; +} + +static int cbDialogClose(Ihandle* ih) +{ + cdCanvas* cd_canvas = (cdCanvas*)IupGetAttribute(ih, "cdCanvas"); + cdCanvas* dbuffer_canvas = (cdCanvas*)IupGetAttribute(ih, "cdCanvasDBuf"); + + if (dbuffer_canvas) cdKillCanvas(dbuffer_canvas); + if (cd_canvas) cdKillCanvas(cd_canvas); + + IupSetAttribute(ih, "cdCanvas", NULL); + IupSetAttribute(ih, "cdCanvasDBuf", NULL); + + return IUP_CLOSE; +} + +static int cbMenuNew(Ihandle* ih) +{ + cdCanvas* dbuffer_canvas = (cdCanvas*)IupGetAttribute(ih, "cdCanvasDBuf"); + cdCanvasActivate(dbuffer_canvas); + cdCanvasClear(dbuffer_canvas); + cdCanvasFlush(dbuffer_canvas); + return IUP_DEFAULT; +} + +static void PrintError(int error) +{ + switch (error) + { + case IM_ERR_OPEN: + IupMessage("IM", "Error Opening File."); + break; + case IM_ERR_MEM: + IupMessage("IM", "Insuficient memory."); + break; + case IM_ERR_ACCESS: + IupMessage("IM", "Error Accessing File."); + break; + case IM_ERR_DATA: + IupMessage("IM", "Image type not Suported."); + break; + case IM_ERR_FORMAT: + IupMessage("IM", "Invalid Format."); + break; + case IM_ERR_COMPRESS: + IupMessage("IM", "Invalid or unsupported compression."); + break; + default: + IupMessage("IM", "Unknown Error."); + } +} + +static int cbMenuOpen(Ihandle* ih) +{ + int error; + char file_name[200] = "*.*"; + imImage* image; + cdCanvas* dbuffer_canvas = (cdCanvas*)IupGetAttribute(IupGetDialog(ih), "cdCanvasDBuf"); + + if (IupGetFile(file_name) != 0) + return IUP_DEFAULT; + + image = imFileImageLoadBitmap(file_name, 0, &error); + if (error) PrintError(error); + if (!image) return IUP_DEFAULT; + + cdCanvasActivate(dbuffer_canvas); + cdCanvasClear(dbuffer_canvas); + + imcdCanvasPutImage(dbuffer_canvas, image, 0, 0, image->width, image->height, 0, 0, 0, 0); + imImageDestroy(image); + + cdCanvasFlush(dbuffer_canvas); + + return IUP_DEFAULT; +} + +static int cbMenuSaveAs(Ihandle* ih) +{ + return IUP_DEFAULT; +} + +static int cbFgColor(Ihandle* ih) +{ + Ihandle* color_dlg = IupColorDlg(); + IupSetAttributeHandle(color_dlg, "PARENTDIALOG", IupGetDialog(ih)); + IupSetAttribute(color_dlg, "TITLE", "Choose Color"); + IupSetAttribute(color_dlg, "VALUE", IupGetAttribute(ih, "BGCOLOR")); + + IupPopup(color_dlg, IUP_CENTER, IUP_CENTER); + + if (IupGetInt(color_dlg, "STATUS")==1) + { + char* value = IupGetAttribute(color_dlg, "VALUE"); + IupStoreAttribute(ih, "BGCOLOR", value); + } + + IupDestroy(color_dlg); + + return IUP_DEFAULT; +} + +static int cbMenuAbout(void) +{ + IupMessage("About", "Simple Paint\nVersion 1.0"); + return IUP_DEFAULT; +} + +static Ihandle* CreateDialog(void) +{ + Ihandle *iup_dialog, *iup_canvas, *menu; + + menu = IupMenu( + IupSubmenu("&File", IupMenu( + IupSetCallbacks(IupItem("New", NULL), "ACTION", cbMenuNew, NULL), + IupSetCallbacks(IupItem("Open...\tCtrl+O", NULL), "ACTION", cbMenuOpen, NULL), + IupSetCallbacks(IupItem("Save As...", NULL), "ACTION", cbMenuSaveAs, NULL), + IupSetCallbacks(IupItem("E&xit", NULL), "ACTION", cbDialogClose, NULL), /* can do that because the menu inherit from the dialog */ + NULL)), + IupSubmenu("&Help", IupMenu( + IupSetCallbacks(IupItem("About", NULL), "ACTION", (Icallback)cbMenuAbout, NULL), + NULL)), + NULL); + + iup_canvas = IupCanvas(NULL); + IupSetCallback(iup_canvas, "BUTTON_CB", (Icallback)cbCanvasButton); + IupSetCallback(iup_canvas, "MOTION_CB", (Icallback)cbCanvasMotion); + IupSetCallback(iup_canvas, "ACTION", cbCanvasRepaint); + IupSetCallback(iup_canvas, "MAP_CB", cbCanvasMap); + + iup_dialog = IupDialog( + IupVbox( + IupSetAttributes(IupHbox( + IupSetCallbacks(IupSetAttributes(IupButton(NULL, NULL), "IMAGE=IUP_FileNew, FLAT=Yes"), "ACTION", (Icallback)cbMenuNew, NULL), + IupSetCallbacks(IupSetAttributes(IupButton(NULL, NULL), "IMAGE=IUP_FileOpen, FLAT=Yes"), "ACTION", (Icallback)cbMenuOpen, NULL), + IupSetCallbacks(IupSetAttributes(IupButton(NULL, NULL), "IMAGE=IUP_FileSave, FLAT=Yes"), "ACTION", (Icallback)cbMenuSaveAs, NULL), + IupSetAttributes(IupFill(), "SIZE=50"), + IupLabel("Color:"), + IupSetCallbacks(IupSetAttributes(IupButton(NULL, NULL), "BGCOLOR=\"0 0 0\", SIZE=20x, FLAT=Yes, NAME=fgcolor_but"), "ACTION", (Icallback)cbFgColor, NULL), + NULL), "MARGIN=5x5, GAP=5, ALIGNMENT=ACENTER"), + iup_canvas, + NULL) + ); + IupSetCallback(iup_dialog, "CLOSE_CB", cbDialogClose); + IupSetCallback(iup_dialog, "K_cO", cbMenuOpen), + IupSetAttribute(iup_dialog, "SIZE", "HALFxHALF"); /* initial size */ + IupSetAttribute(iup_dialog, "TITLE", "Simple Paint"); + IupSetAttributeHandle(iup_dialog, "MENU", menu); + + return iup_dialog; +} + +int main(int argc, char* argv[]) +{ + Ihandle* dlg; + + IupOpen(&argc, &argv); + IupImageLibOpen(); + + dlg = CreateDialog(); + + IupShow(dlg); + + IupMainLoop(); + IupDestroy(dlg); + IupClose(); + + return 0; +} diff --git a/html/examples/im_view.c b/html/examples/im_view.c index e271394..872048e 100644 --- a/html/examples/im_view.c +++ b/html/examples/im_view.c @@ -49,7 +49,7 @@ static void PrintError(int error) } } -static int cbRepaint(Ihandle* iup_canvas) +static int cbCanvasRepaint(Ihandle* iup_canvas) { cdCanvas* cd_canvas = (cdCanvas*)IupGetAttribute(iup_canvas, "cdCanvas"); imImage* image = (imImage*)IupGetAttribute(iup_canvas, "imImage"); @@ -84,10 +84,10 @@ static void ShowImage(char* file_name, Ihandle* iup_dialog) IupSetAttribute(iup_dialog, "imImage", (char*)image); IupStoreAttribute(iup_dialog, "TITLE", file_name); - cbRepaint(iup_dialog); /* we can do this because canvas inherit attributes from the dialog */ + cbCanvasRepaint(iup_dialog); /* we can do this because canvas inherit attributes from the dialog */ } -static int cbButton(Ihandle* iup_canvas, int but, int pressed) +static int cbCanvasButton(Ihandle* iup_canvas, int but, int pressed) { char file_name[200] = "*.*"; @@ -107,7 +107,14 @@ static int cbButton(Ihandle* iup_canvas, int but, int pressed) return IUP_DEFAULT; } -static int cbClose(Ihandle* iup_dialog) +static int cbCanvasMap(Ihandle* iup_canvas) +{ + cdCanvas* cd_canvas = cdCreateCanvas(CD_IUP, iup_canvas); + IupSetAttribute(IupGetDialog(iup_canvas), "cdCanvas", (char*)cd_canvas); + return IUP_DEFAULT; +} + +static int cbDialogClose(Ihandle* iup_dialog) { cdCanvas* cd_canvas = (cdCanvas*)IupGetAttribute(iup_dialog, "cdCanvas"); imImage* image = (imImage*)IupGetAttribute(iup_dialog, "imImage"); @@ -123,37 +130,24 @@ static int cbClose(Ihandle* iup_dialog) static Ihandle* CreateDialog(void) { - Ihandle *iup_dialog; - Ihandle *iup_canvas; - cdCanvas* cd_canvas; + Ihandle *iup_dialog, *iup_canvas; - iup_canvas = IupCanvas("do_nothing"); - IupSetAttribute(iup_canvas, IUP_BUTTON_CB, "cbButton"); - IupSetAttribute(iup_canvas, IUP_ACTION, "cbRepaint"); + iup_canvas = IupCanvas(NULL); + IupSetCallback(iup_canvas, "BUTTON_CB", (Icallback)cbCanvasButton); + IupSetCallback(iup_canvas, "ACTION", (Icallback)cbCanvasRepaint); + IupSetCallback(iup_canvas, "MAP_CB", (Icallback)cbCanvasMap); iup_dialog = IupDialog(iup_canvas); - IupSetAttribute(iup_dialog, IUP_CLOSE_CB, "cbClose"); - IupSetAttribute(iup_dialog, IUP_SIZE, "HALFxHALF"); - - IupSetFunction("cbRepaint", (Icallback)cbRepaint); - IupSetFunction("cbButton", (Icallback)cbButton); - IupSetFunction("cbClose", (Icallback)cbClose); - - IupMap(iup_dialog); - - cd_canvas = cdCreateCanvas(CD_IUP, iup_canvas); - IupSetAttribute(iup_dialog, "cdCanvas", (char*)cd_canvas); + IupSetCallback(iup_dialog, "CLOSE_CB", (Icallback)cbDialogClose); + IupSetAttribute(iup_dialog, "SIZE", "HALFxHALF"); /* initial size */ return iup_dialog; } -//#include "im_format_avi.h" - int main(int argc, char* argv[]) { Ihandle* dlg; - //imFormatRegisterAVI(); IupOpen(&argc, &argv); dlg = CreateDialog(); @@ -162,21 +156,17 @@ int main(int argc, char* argv[]) /* Try to get a file name from the command line. */ if (argc > 1) - { ShowImage(argv[1], dlg); - } else { char file_name[1024] = "*.*"; if (IupGetFile(file_name) == 0) - { ShowImage(file_name, dlg); - } } IupMainLoop(); IupDestroy(dlg); IupClose(); - return 1; + return 0; } -- cgit v1.2.3