Updated from its repository.
[cake.git] / workbench / classes / gadgets / texteditor / demo / TextEditor-Demo.c
blob992d1a16a0a483ee1e5d542b1aa80d967df15e10
1 /***************************************************************************
3 TextEditor-Demo - Textediting MUI Custom Class Demonstration Program
4 Copyright (C) 1997-2000 Allan Odgaard
5 Copyright (C) 2005-2007 by TextEditor.mcc Open Source Team
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 TextEditor class Support Site: http://www.sf.net/projects/texteditor-mcc
19 To compile this demo of some features of the TextEditor class,
20 depending on your OS and compiler :
22 - MorphOS GCC : gcc -noixemul -o TextEditor-Demo TextEditor-Demo.c
23 - MorphOS VBCC : vc -o TextEditor-Demo TextEditor-Demo.c -lamiga
24 - AmigaOS4 GCC : gcc -ggdb -D__USE_BASETYPE__ -D__USE_INLINE__ -o TextEditor-Demo TextEditor-Demo.c
25 - AmigaOS 3.x SAS/C : sc TextEditor-Demo.c link to TextEditor-Demo
27 Requirements : SDI_headers
29 $Id$
31 ***************************************************************************/
33 #include <stdio.h>
34 #include <string.h>
36 #include <libraries/mui.h>
37 #include <libraries/iffparse.h>
39 #include <clib/alib_protos.h>
41 #include <proto/dos.h>
42 #include <proto/intuition.h>
43 #include <proto/muimaster.h>
44 #include <proto/exec.h>
45 #include <proto/graphics.h>
46 #include <proto/rexxsyslib.h>
48 #include <mui/TextEditor_mcc.h>
50 #include <SDI_hook.h>
52 #ifndef __amigaos4__
53 struct GfxBase *GfxBase;
54 struct IntuitionBase *IntuitionBase;
55 struct Library *MUIMasterBase;
56 #ifdef __MORPHOS__
57 struct Library *RexxSysBase;
58 #else
59 struct RxsLib *RexxSysBase;
60 #endif
61 #else
62 struct Library *GfxBase;
63 struct Library *IntuitionBase;
64 struct Library *MUIMasterBase;
65 struct Library *RexxSysBase;
67 struct GraphicsIFace *IGraphics;
68 struct IntuitionIFace *IIntuition;
69 struct MUIMasterIFace *IMUIMaster;
70 struct RexxSysIFace *IRexxSys;
71 #endif
73 #ifndef MUIA_Application_UsedClasses
74 #define MUIA_Application_UsedClasses 0x8042e9a7 /* V20 STRPTR * i.. */
75 #endif
76 #define MUIV_RunARexxScript 0xad800000
78 long __stack = 16384;
80 Object *app, *window, *editorgad;
81 const char *StdEntries[] = { "Kind regards ", "Yours ", "Mvh ", NULL };
82 LONG cmap[8];
84 HOOKPROTONH(ARexxHookCode, LONG, Object *app, struct RexxMsg *rexxmsg)
86 LONG result;
88 result = DoMethod(editorgad, MUIM_TextEditor_ARexxCmd, rexxmsg->rm_Args[0]);
90 if(result)
92 if(result != TRUE)
94 set(app, MUIA_Application_RexxString, result);
95 FreeVec((APTR)result);
99 return(0);
101 MakeStaticHook(ARexxHook, ARexxHookCode);
103 DISPATCHER(TextEditor_Dispatcher)
105 switch(msg->MethodID)
107 case MUIM_Show:
109 struct ColorMap *cm = muiRenderInfo(obj)->mri_Screen->ViewPort.ColorMap;
111 cmap[0] = ObtainBestPenA(cm, 0x00<<24, 0x00<<24, 0x00<<24, NULL);
112 cmap[1] = ObtainBestPenA(cm, 0xff<<24, 0xff<<24, 0xff<<24, NULL);
113 cmap[2] = ObtainBestPenA(cm, 0xff<<24, 0x00<<24, 0x00<<24, NULL);
114 cmap[3] = ObtainBestPenA(cm, 0x00<<24, 0xff<<24, 0x00<<24, NULL);
115 cmap[4] = ObtainBestPenA(cm, 0x00<<24, 0xff<<24, 0xff<<24, NULL);
116 cmap[5] = ObtainBestPenA(cm, 0xff<<24, 0xff<<24, 0x00<<24, NULL);
117 cmap[6] = ObtainBestPenA(cm, 0x00<<24, 0x00<<24, 0xff<<24, NULL);
118 cmap[7] = ObtainBestPenA(cm, 0xff<<24, 0x00<<24, 0xff<<24, NULL);
120 break;
122 case MUIM_Hide:
124 struct ColorMap *cm = muiRenderInfo(obj)->mri_Screen->ViewPort.ColorMap;
125 int c;
127 for(c = 0; c < 8; c++)
129 if(cmap[c] >= 0)
130 ReleasePen(cm, cmap[c]);
133 break;
135 case MUIM_DragQuery:
137 return(TRUE);
140 case MUIM_DragDrop:
142 struct MUIP_DragDrop *drop_msg = (struct MUIP_DragDrop *)msg;
143 ULONG active = 0;
145 if(GetAttr(MUIA_List_Active, drop_msg->obj, &active))
146 DoMethod(obj, MUIM_TextEditor_InsertText, StdEntries[active]);
148 break;
150 case MUIM_TextEditor_HandleError:
152 const char *errortxt = NULL;
153 struct MUIP_TextEditor_HandleError *msgerr;
155 msgerr = (struct MUIP_TextEditor_HandleError *)msg;
157 switch(msgerr->errorcode)
159 case Error_ClipboardIsEmpty:
160 errortxt = "\33cThe clipboard is empty.";
161 break;
163 case Error_ClipboardIsNotFTXT:
164 errortxt = "\33cThe clipboard does not contain text.";
165 break;
167 case Error_MacroBufferIsFull:
168 case Error_MemoryAllocationFailed:
169 case Error_NoMacroDefined:
170 case Error_StringNotFound:
171 break;
173 case Error_NoAreaMarked:
174 errortxt = "\33cNo area marked.";
175 break;
177 case Error_NothingToRedo:
178 errortxt = "\33cNothing to redo.";
179 break;
181 case Error_NothingToUndo:
182 errortxt = "\33cNothing to undo.";
183 break;
185 case Error_NotEnoughUndoMem:
186 errortxt = "There is not enough memory\nto keep the undo buffer.\n\nThe undobuffer is lost.";
187 break;
189 case Error_NoBookmarkInstalled:
190 errortxt = "There is no bookmark installed!";
191 break;
193 case Error_BookmarkHasBeenLost:
194 errortxt = "Your bookmark has unfortunately been lost.";
195 break;
198 if(errortxt)
199 MUI_Request(app, window, 0L, NULL, "Continue", errortxt);
201 break;
204 return DoSuperMethodA(cl, obj, (Msg)msg);
207 static Object *ImageGad(const char *text, UBYTE key)
209 return(TextObject,
210 MUIA_Background, MUII_ButtonBack,
211 MUIA_Frame, MUIV_Frame_Button,
212 MUIA_Text_PreParse, "\33c",
213 MUIA_Font, MUIV_Font_Tiny,
214 MUIA_Text_Contents, text,
215 MUIA_Text_SetVMax, FALSE,
216 MUIA_FixHeight, 17,
217 MUIA_FixWidth, 24,
218 MUIA_InputMode, MUIV_InputMode_Toggle,
219 MUIA_ControlChar, key,
220 MUIA_CycleChain, TRUE,
221 End);
224 static ULONG OpenLibs(void)
226 GfxBase = (APTR)OpenLibrary("graphics.library", 39);
227 IntuitionBase = (APTR)OpenLibrary("intuition.library", 39);
228 MUIMasterBase = OpenLibrary("muimaster.library", 11); //MUIMASTER_VMIN)
229 RexxSysBase = (APTR)OpenLibrary("rexxsyslib.library", 36);
231 if(GfxBase && MUIMasterBase && IntuitionBase && RexxSysBase)
233 #ifdef __amigaos4__
234 IGraphics = (struct GraphicsIFace *)GetInterface((struct Library *)GfxBase, "main", 1, NULL);
235 IIntuition = (struct IntuitionIFace *)GetInterface((struct Library *)IntuitionBase, "main", 1, NULL);
236 IMUIMaster = (struct MUIMasterIFace *)GetInterface(MUIMasterBase, "main", 1, NULL);
237 IRexxSys = (struct RexxSysIFace *)GetInterface(RexxSysBase, "main", 1, NULL);
238 #endif
240 return TRUE;
242 else
244 return FALSE;
248 void CloseLibs(void)
250 #ifdef __amigaos4__
251 if (IRexxSys) DropInterface((struct Interface *)IRexxSys);
252 if (IMUIMaster) DropInterface((struct Interface *)IMUIMaster);
253 if (IIntuition) DropInterface((struct Interface *)IIntuition);
254 if (IGraphics) DropInterface((struct Interface *)IGraphics);
255 #endif
257 if (RexxSysBase) CloseLibrary((struct Library *)RexxSysBase);
258 if (MUIMasterBase) CloseLibrary(MUIMasterBase);
259 if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
260 if (GfxBase) CloseLibrary((struct Library *)GfxBase);
263 int main(VOID)
265 struct RDArgs *args;
266 Object *slider;
267 LONG argarray[6] = { 0,0,0,0,0,0 };
269 if((args = ReadArgs("Filename/F,EMail/S,MIME/S,MIMEQuoted/S,SkipHeader/S,Fixed/S", argarray, NULL)))
271 if(OpenLibs() == TRUE)
273 struct MUI_CustomClass *editor_mcc;
274 Object *clear, *cut, *copy, *paste, *erase,
275 *bold, *italic, *underline, *ischanged, *undo, *redo,
276 *flow, *separator, *color, *config;
277 const char *flow_text[] = { "Left", "Center", "Right", NULL };
278 const char *colors[] = { "Normal", "Black", "White", "Red", "Green", "Cyan", "Yellow", "Blue", "Magenta", NULL };
279 const char *classes[] = { "TextEditor.mcc" };
281 if((editor_mcc = MUI_CreateCustomClass(NULL, "TextEditor.mcc", NULL, 0, ENTRY(TextEditor_Dispatcher))))
283 app = ApplicationObject,
284 MUIA_Application_Author, "TextEditor.mcc Open Source Team",
285 MUIA_Application_Base, "TextEditor-Demo",
286 MUIA_Application_Copyright, "(c) 2005-2007 by TextEditor.mcc Open Source Team",
287 MUIA_Application_Description, "TextEditor.mcc demonstration program",
288 MUIA_Application_RexxHook, &ARexxHook,
289 MUIA_Application_Title, "TextEditor-Demo",
290 MUIA_Application_Version, "$VER: TextEditor-Demo (" __DATE__ ")",
291 MUIA_Application_UsedClasses, classes,
292 SubWindow, window = WindowObject,
293 MUIA_Window_Title, "TextEditor-Demo",
294 MUIA_Window_ID, MAKE_ID('M','A','I','N'),
295 WindowContents, VGroup,
296 Child, VGroup,
297 MUIA_Background, MUII_GroupBack,
298 MUIA_Frame, MUIV_Frame_Group,
300 Child, HGroup,
302 Child, RowGroup(2),
303 Child, cut = MUI_MakeObject(MUIO_Button, "Cut"),
304 Child, paste = MUI_MakeObject(MUIO_Button, "Paste"),
305 Child, undo = MUI_MakeObject(MUIO_Button, "Undo"),
306 Child, flow = MUI_MakeObject(MUIO_Cycle, NULL, flow_text),
307 Child, separator = MUI_MakeObject(MUIO_Button, "Separator"),
308 Child, clear = MUI_MakeObject(MUIO_Button, "Clear _Text"),
310 Child, copy = MUI_MakeObject(MUIO_Button, "Copy"),
311 Child, erase = MUI_MakeObject(MUIO_Button, "Erase"),
312 Child, redo = MUI_MakeObject(MUIO_Button, "Redo"),
313 Child, color = MUI_MakeObject(MUIO_Cycle, NULL, colors),
314 Child, config = MUI_MakeObject(MUIO_Button, "Config..."),
315 Child, HGroup,
316 Child, ischanged = MUI_MakeObject(MUIO_Checkmark, "Is changed?"),
317 Child, TextObject,
318 MUIA_Text_Contents, "Is changed?",
319 End,
320 End,
321 End,
323 Child, RectangleObject, End,
325 Child, bold = ImageGad("\33I[5:ProgDir:Bold.Brush]\n\n\nBold", 'b'),
326 Child, italic = ImageGad("\33I[5:ProgDir:Italic.Brush]\n\n\nItalic", 'i'),
327 Child, underline = ImageGad("\33I[5:ProgDir:Underline.Brush]\n\n\nUnderline", 'u'),
328 End,
330 Child, HGroup,
332 Child, HGroup,
333 MUIA_Group_Spacing, 0,
334 Child, editorgad = NewObject(editor_mcc->mcc_Class, NULL,
335 MUIA_TextEditor_ColorMap, cmap,
336 MUIA_CycleChain, TRUE,
337 End,
338 Child, slider = ScrollbarObject,
339 End,
340 End,
342 Child, VGroup,
343 Child, ListviewObject,
344 MUIA_Listview_DragType, MUIV_Listview_DragType_Immediate,
345 MUIA_Listview_List, ListObject,
346 InputListFrame,
347 MUIA_Background, MUII_ListBack,
348 MUIA_List_AdjustWidth, TRUE,
349 MUIA_List_AdjustHeight, TRUE,
350 MUIA_List_SourceArray, StdEntries,
351 End,
352 End,
353 Child, RectangleObject,
354 End,
355 End,
356 End,
357 End,
358 End,
359 End,
360 End;
362 if(app)
364 ULONG sigs;
365 ULONG running = 1;
366 BPTR fh;
368 if(argarray[5])
369 set(editorgad, MUIA_TextEditor_FixedFont, TRUE);
371 if(argarray[0] && (fh = Open((STRPTR)argarray[0], MODE_OLDFILE)))
373 STRPTR text = AllocVec(300*1024, 0L);
374 STRPTR buffer = text;
375 LONG size;
377 if(text)
379 size = Read(fh, text, (300*1024)-2);
380 text[size] = '\0';
381 Close(fh);
383 if(argarray[4])
385 while(*buffer != '\n' && buffer < &text[size])
387 while(*buffer++ != '\n');
391 if(argarray[3])
393 SetAttrs(editorgad,
394 MUIA_TextEditor_ImportHook, MUIV_TextEditor_ImportHook_MIMEQuoted,
395 MUIA_TextEditor_ImportWrap, 80,
396 MUIA_TextEditor_ExportHook, MUIV_TextEditor_ExportHook_EMail,
397 TAG_DONE);
399 else
401 if(argarray[2])
403 SetAttrs(editorgad,
404 MUIA_TextEditor_ImportHook, MUIV_TextEditor_ImportHook_MIME,
405 MUIA_TextEditor_ExportHook, MUIV_TextEditor_ExportHook_EMail,
406 TAG_DONE);
408 else
410 if(argarray[1])
412 SetAttrs(editorgad,
413 MUIA_TextEditor_ImportHook, MUIV_TextEditor_ImportHook_EMail,
414 MUIA_TextEditor_ExportHook, MUIV_TextEditor_ExportHook_EMail,
415 TAG_DONE);
420 set(editorgad, MUIA_TextEditor_Contents, buffer);
421 FreeVec(text);
422 set(editorgad, MUIA_TextEditor_ImportHook, MUIV_TextEditor_ImportHook_Plain);
426 DoMethod(window, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, MUIV_Notify_Application, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
428 DoMethod(window, MUIM_Notify, MUIA_Window_InputEvent, "f1", MUIV_Notify_Application, 2, MUIM_Application_ReturnID, MUIV_RunARexxScript);
430 DoMethod(flow, MUIM_Notify, MUIA_Cycle_Active, MUIV_EveryTime, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_Flow, MUIV_TriggerValue);
431 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_Flow, MUIV_EveryTime, flow, 3, MUIM_NoNotifySet, MUIA_Cycle_Active, MUIV_TriggerValue);
433 DoMethod(color, MUIM_Notify, MUIA_Cycle_Active, MUIV_EveryTime, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_Pen, MUIV_TriggerValue);
434 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_Pen, MUIV_EveryTime, color, 3, MUIM_NoNotifySet, MUIA_Cycle_Active, MUIV_TriggerValue);
436 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_StyleBold, MUIV_EveryTime, bold, 3, MUIM_NoNotifySet, MUIA_Selected, MUIV_TriggerValue);
437 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_StyleItalic, MUIV_EveryTime, italic, 3, MUIM_NoNotifySet, MUIA_Selected, MUIV_TriggerValue);
438 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_StyleUnderline, MUIV_EveryTime, underline, 3, MUIM_NoNotifySet, MUIA_Selected, MUIV_TriggerValue);
440 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_HasChanged, MUIV_EveryTime, ischanged, 3, MUIM_NoNotifySet, MUIA_Selected, MUIV_TriggerValue);
441 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_HasChanged, MUIV_EveryTime, ischanged, 3, MUIM_NoNotifySet, MUIA_Image_State, MUIV_TriggerValue);
442 DoMethod(ischanged, MUIM_Notify, MUIA_Selected, MUIV_EveryTime, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_HasChanged, MUIV_TriggerValue);
444 DoMethod(separator, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_InsertText, "\n\33c\33[s:2]\n");
446 DoMethod(config, MUIM_Notify, MUIA_Pressed, FALSE, MUIV_Notify_Application, 2, MUIM_Application_OpenConfigWindow, 1, "TextEditor.mcc");
448 DoMethod(clear, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "Clear");
449 DoMethod(clear, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_HasChanged, FALSE);
451 DoMethod(cut, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "Cut");
452 DoMethod(copy, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "Copy");
453 DoMethod(paste, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "Paste");
454 DoMethod(erase, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "Erase");
455 DoMethod(undo, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "Undo");
456 DoMethod(redo, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "Redo");
458 DoMethod(bold, MUIM_Notify, MUIA_Selected, MUIV_EveryTime, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_StyleBold, MUIV_TriggerValue);
459 DoMethod(italic, MUIM_Notify, MUIA_Selected, MUIV_EveryTime, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_StyleItalic, MUIV_TriggerValue);
460 DoMethod(underline, MUIM_Notify, MUIA_Selected, MUIV_EveryTime, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_StyleUnderline, MUIV_TriggerValue);
462 set(editorgad, MUIA_TextEditor_Slider, slider);
464 SetAttrs(window, MUIA_Window_ActiveObject, editorgad,
465 MUIA_Window_Open, TRUE,
466 TAG_DONE);
470 struct MsgPort *myport = NULL;
471 struct RexxMsg *rxmsg = NULL;
472 ULONG changed;
473 ULONG ReturnID;
474 BPTR rxstdout = 0L;
476 while((LONG)(ReturnID = DoMethod(app, MUIM_Application_NewInput, &sigs)) != (LONG)MUIV_Application_ReturnID_Quit)
478 if(ReturnID == MUIV_RunARexxScript && !myport)
480 struct MsgPort *rexxport;
481 const char *script = "Rexx:TextEditor/Demo.Rexx";
483 if((rexxport = FindPort((STRPTR)"REXX")) && (myport = CreateMsgPort()))
485 if(!rxstdout)
486 rxstdout = Open("CON://640/100/TextEditor-Demo, ARexx output:/Close/Wait/Auto/InActive", MODE_READWRITE);
488 rxmsg = CreateRexxMsg(myport, NULL, "TEXTEDITOR-DEMO.1");
489 rxmsg->rm_Action = RXCOMM;
490 rxmsg->rm_Stdin = rxstdout;
491 rxmsg->rm_Stdout = rxstdout;
492 rxmsg->rm_Args[0] = CreateArgstring((STRPTR)script, strlen(script));
493 PutMsg(rexxport, (struct Message *)rxmsg);
497 if(sigs)
499 sigs = Wait(sigs | SIGBREAKF_CTRL_C | (myport ? 1<<myport->mp_SigBit : 0));
500 if(myport && (sigs & 1<<myport->mp_SigBit))
502 GetMsg(myport);
503 if(!rxmsg->rm_Result1 && rxmsg->rm_Result2)
504 DeleteArgstring((STRPTR)rxmsg->rm_Result2);
506 DeleteArgstring(rxmsg->rm_Args[0]);
507 DeleteRexxMsg(rxmsg);
508 DeleteMsgPort(myport);
509 myport = NULL;
512 if(sigs & SIGBREAKF_CTRL_C)
513 break;
517 if(rxstdout)
518 Close(rxstdout);
520 get(editorgad, MUIA_TextEditor_HasChanged, &changed);
521 if(argarray[0] && changed && !(sigs & SIGBREAKF_CTRL_C))
522 running = MUI_Request(app, window, 0L, "Warning", "_Proceed|*_Save|_Cancel", "\33cText '%s'\n is modified. Save it?", argarray[0]);
524 while(running == 0);
526 if(running == 2)
528 STRPTR text = (STRPTR)DoMethod(editorgad, MUIM_TextEditor_ExportText);
530 if(argarray[0] && (fh = Open((STRPTR)argarray[0], MODE_NEWFILE)))
532 Write(fh, text, strlen(text));
533 Close(fh);
535 FreeVec(text);
537 MUI_DisposeObject(app);
539 else
540 printf("Failed to create application\n");
542 MUI_DeleteCustomClass(editor_mcc);
544 else
545 printf("Failed to open TextEditor.mcc\n");
547 CloseLibs();
549 else
550 printf("Failed to open MUIMaster.Library V%d\n", MUIMASTER_VMIN);
552 FreeArgs(args);
554 else
556 char prgname[32];
557 LONG error = IoErr();
559 GetProgramName(prgname, 32);
560 PrintFault(error, prgname);
563 return 0;