arch/cpu.resource: remove dead code
[AROS.git] / workbench / classes / zune / texteditor / demo / TextEditor-Demo.c
blobf69c1af1ddcd3f2a1195ea3a0f4351482e16c192
1 /***************************************************************************
3 TextEditor-Demo - Textediting MUI Custom Class Demonstration Program
4 Copyright (C) 1997-2000 Allan Odgaard
5 Copyright (C) 2005-2014 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 #if defined(__amigaos4__)
51 #include <dos/obsolete.h>
52 #endif
54 #include "SDI_hook.h"
56 #ifndef __amigaos4__
57 struct GfxBase *GfxBase;
58 struct IntuitionBase *IntuitionBase;
59 struct Library *MUIMasterBase;
60 #ifdef __MORPHOS__
61 struct Library *RexxSysBase;
62 #else
63 struct RxsLib *RexxSysBase;
64 #endif
65 #else
66 struct Library *GfxBase;
67 struct Library *IntuitionBase;
68 struct Library *MUIMasterBase;
69 struct Library *RexxSysBase;
71 struct GraphicsIFace *IGraphics;
72 struct IntuitionIFace *IIntuition;
73 struct MUIMasterIFace *IMUIMaster;
74 struct RexxSysIFace *IRexxSys;
75 #endif
77 #ifndef MUIA_Application_UsedClasses
78 #define MUIA_Application_UsedClasses 0x8042e9a7 /* V20 STRPTR * i.. */
79 #endif
80 #define MUIV_RunARexxScript 0xad800000
82 long __stack = 16384;
84 Object *app, *window, *editorgad;
85 const char *StdEntries[] = { "Kind regards ", "Yours ", "Mvh ", NULL };
86 LONG cmap[8];
88 HOOKPROTONH(ARexxHookCode, LONG, Object *app, struct RexxMsg *rexxmsg)
90 IPTR result;
92 result = DoMethod(editorgad, MUIM_TextEditor_ARexxCmd, rexxmsg->rm_Args[0]);
94 if(result)
96 if(result != TRUE)
98 set(app, MUIA_Application_RexxString, result);
99 FreeVec((APTR)result);
103 return(0);
105 MakeStaticHook(ARexxHook, ARexxHookCode);
107 DISPATCHER(TextEditor_Dispatcher)
109 switch(msg->MethodID)
111 case MUIM_Setup:
113 if(DoSuperMethodA(cl, obj, (Msg)msg))
115 struct ColorMap *cm = muiRenderInfo(obj)->mri_Screen->ViewPort.ColorMap;
117 cmap[0] = ObtainBestPenA(cm, 0x00<<24, 0x00<<24, 0x00<<24, NULL);
118 cmap[1] = ObtainBestPenA(cm, 0xff<<24, 0xff<<24, 0xff<<24, NULL);
119 cmap[2] = ObtainBestPenA(cm, 0xff<<24, 0x00<<24, 0x00<<24, NULL);
120 cmap[3] = ObtainBestPenA(cm, 0x00<<24, 0xff<<24, 0x00<<24, NULL);
121 cmap[4] = ObtainBestPenA(cm, 0x00<<24, 0xff<<24, 0xff<<24, NULL);
122 cmap[5] = ObtainBestPenA(cm, 0xff<<24, 0xff<<24, 0x00<<24, NULL);
123 cmap[6] = ObtainBestPenA(cm, 0x00<<24, 0x00<<24, 0xff<<24, NULL);
124 cmap[7] = ObtainBestPenA(cm, 0xff<<24, 0x00<<24, 0xff<<24, NULL);
126 set(obj, MUIA_TextEditor_ColorMap, cmap);
128 return TRUE;
130 else
131 return FALSE;
133 break;
135 case MUIM_Cleanup:
137 struct ColorMap *cm = muiRenderInfo(obj)->mri_Screen->ViewPort.ColorMap;
138 int c;
140 set(obj, MUIA_TextEditor_ColorMap, NULL);
142 for(c = 0; c < 8; c++)
144 if(cmap[c] != -1)
145 ReleasePen(cm, cmap[c]);
148 break;
150 case MUIM_DragQuery:
152 return TRUE;
155 case MUIM_DragDrop:
157 struct MUIP_DragDrop *drop_msg = (struct MUIP_DragDrop *)msg;
158 ULONG active = 0;
160 if(GetAttr(MUIA_List_Active, drop_msg->obj, (IPTR *)&active))
161 DoMethod(obj, MUIM_TextEditor_InsertText, StdEntries[active]);
163 break;
165 case MUIM_TextEditor_HandleError:
167 const char *errortxt = NULL;
168 struct MUIP_TextEditor_HandleError *msgerr;
170 msgerr = (struct MUIP_TextEditor_HandleError *)msg;
172 switch(msgerr->errorcode)
174 case Error_ClipboardIsEmpty:
175 errortxt = "\33cThe clipboard is empty.";
176 break;
178 case Error_ClipboardIsNotFTXT:
179 errortxt = "\33cThe clipboard does not contain text.";
180 break;
182 case Error_MacroBufferIsFull:
183 case Error_MemoryAllocationFailed:
184 case Error_NoMacroDefined:
185 case Error_StringNotFound:
186 break;
188 case Error_NoAreaMarked:
189 errortxt = "\33cNo area marked.";
190 break;
192 case Error_NothingToRedo:
193 errortxt = "\33cNothing to redo.";
194 break;
196 case Error_NothingToUndo:
197 errortxt = "\33cNothing to undo.";
198 break;
200 case Error_NotEnoughUndoMem:
201 errortxt = "There is not enough memory\nto keep the undo buffer.\n\nThe undobuffer is lost.";
202 break;
204 case Error_NoBookmarkInstalled:
205 errortxt = "There is no bookmark installed!";
206 break;
208 case Error_BookmarkHasBeenLost:
209 errortxt = "Your bookmark has unfortunately been lost.";
210 break;
213 if(errortxt != NULL)
214 MUI_Request(app, window, 0L, NULL, "Continue", errortxt);
216 break;
219 return DoSuperMethodA(cl, obj, (Msg)msg);
222 static Object *ImageGad(const char *text, UBYTE key)
224 return(TextObject,
225 MUIA_Background, MUII_ButtonBack,
226 MUIA_Frame, MUIV_Frame_Button,
227 MUIA_Text_PreParse, "\33c",
228 MUIA_Font, MUIV_Font_Tiny,
229 MUIA_Text_Contents, text,
230 MUIA_Text_SetVMax, FALSE,
231 MUIA_FixHeight, 17,
232 MUIA_FixWidth, 24,
233 MUIA_InputMode, MUIV_InputMode_Toggle,
234 MUIA_ControlChar, key,
235 MUIA_CycleChain, TRUE,
236 End);
239 static ULONG OpenLibs(void)
241 GfxBase = (APTR)OpenLibrary("graphics.library", 39);
242 IntuitionBase = (APTR)OpenLibrary("intuition.library", 39);
243 MUIMasterBase = OpenLibrary("muimaster.library", 11); //MUIMASTER_VMIN)
244 RexxSysBase = (APTR)OpenLibrary("rexxsyslib.library", 36);
246 if(GfxBase && MUIMasterBase && IntuitionBase && RexxSysBase)
248 #ifdef __amigaos4__
249 IGraphics = (struct GraphicsIFace *)GetInterface((struct Library *)GfxBase, "main", 1, NULL);
250 IIntuition = (struct IntuitionIFace *)GetInterface((struct Library *)IntuitionBase, "main", 1, NULL);
251 IMUIMaster = (struct MUIMasterIFace *)GetInterface(MUIMasterBase, "main", 1, NULL);
252 IRexxSys = (struct RexxSysIFace *)GetInterface(RexxSysBase, "main", 1, NULL);
253 #endif
255 return TRUE;
257 else
259 return FALSE;
263 void CloseLibs(void)
265 #ifdef __amigaos4__
266 if (IRexxSys) DropInterface((struct Interface *)IRexxSys);
267 if (IMUIMaster) DropInterface((struct Interface *)IMUIMaster);
268 if (IIntuition) DropInterface((struct Interface *)IIntuition);
269 if (IGraphics) DropInterface((struct Interface *)IGraphics);
270 #endif
272 if (RexxSysBase) CloseLibrary((struct Library *)RexxSysBase);
273 if (MUIMasterBase) CloseLibrary(MUIMasterBase);
274 if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
275 if (GfxBase) CloseLibrary((struct Library *)GfxBase);
278 int main(VOID)
280 struct RDArgs *args;
281 Object *slider;
282 IPTR argarray[6] = { 0,0,0,0,0,0 };
284 if((args = ReadArgs("Filename/F,EMail/S,MIME/S,MIMEQuoted/S,SkipHeader/S,Fixed/S", (IPTR *)argarray, NULL)))
286 if(OpenLibs() == TRUE)
288 struct MUI_CustomClass *editor_mcc;
289 Object *clear, *cut, *copy, *paste, *erase,
290 *bold, *italic, *underline, *ischanged, *undo, *redo,
291 *flow, *separator, *color, *config;
292 const char *flow_text[] = { "Left", "Center", "Right", NULL };
293 const char *colors[] = { "Normal", "Black", "White", "Red", "Green", "Cyan", "Yellow", "Blue", "Magenta", NULL };
294 const char *classes[] = { "TextEditor.mcc" };
296 if((editor_mcc = MUI_CreateCustomClass(NULL, "TextEditor.mcc", NULL, 0, ENTRY(TextEditor_Dispatcher))))
298 app = ApplicationObject,
299 MUIA_Application_Author, "TextEditor.mcc Open Source Team",
300 MUIA_Application_Base, "TextEditor-Demo",
301 MUIA_Application_Copyright, "(c) 2005-2010 by TextEditor.mcc Open Source Team",
302 MUIA_Application_Description, "TextEditor.mcc demonstration program",
303 MUIA_Application_RexxHook, &ARexxHook,
304 MUIA_Application_Title, "TextEditor-Demo",
305 MUIA_Application_Version, "$VER: TextEditor-Demo (" __DATE__ ")",
306 MUIA_Application_UsedClasses, classes,
307 SubWindow, window = WindowObject,
308 MUIA_Window_Title, "TextEditor-Demo",
309 MUIA_Window_ID, MAKE_ID('M','A','I','N'),
310 WindowContents, VGroup,
311 Child, VGroup,
312 MUIA_Background, MUII_GroupBack,
313 MUIA_Frame, MUIV_Frame_Group,
315 Child, HGroup,
317 Child, RowGroup(2),
318 Child, cut = MUI_MakeObject(MUIO_Button, "Cut"),
319 Child, paste = MUI_MakeObject(MUIO_Button, "Paste"),
320 Child, undo = MUI_MakeObject(MUIO_Button, "Undo"),
321 Child, flow = MUI_MakeObject(MUIO_Cycle, NULL, flow_text),
322 Child, separator = MUI_MakeObject(MUIO_Button, "Separator"),
323 Child, clear = MUI_MakeObject(MUIO_Button, "Clear _Text"),
325 Child, copy = MUI_MakeObject(MUIO_Button, "Copy"),
326 Child, erase = MUI_MakeObject(MUIO_Button, "Erase"),
327 Child, redo = MUI_MakeObject(MUIO_Button, "Redo"),
328 Child, color = MUI_MakeObject(MUIO_Cycle, NULL, colors),
329 Child, config = MUI_MakeObject(MUIO_Button, "Config..."),
330 Child, HGroup,
331 Child, ischanged = MUI_MakeObject(MUIO_Checkmark, "Is changed?"),
332 Child, TextObject,
333 MUIA_Text_Contents, "Is changed?",
334 End,
335 End,
336 End,
338 Child, RectangleObject, End,
340 Child, bold = ImageGad("\33I[5:ProgDir:Bold.Brush]\n\n\nBold", 'b'),
341 Child, italic = ImageGad("\33I[5:ProgDir:Italic.Brush]\n\n\nItalic", 'i'),
342 Child, underline = ImageGad("\33I[5:ProgDir:Underline.Brush]\n\n\nUnderline", 'u'),
343 End,
345 Child, HGroup,
347 Child, HGroup,
348 MUIA_Group_Spacing, 0,
349 Child, editorgad = NewObject(editor_mcc->mcc_Class, NULL,
350 MUIA_CycleChain, TRUE,
351 End,
352 Child, slider = ScrollbarObject,
353 End,
354 End,
356 Child, VGroup,
357 Child, ListviewObject,
358 MUIA_Listview_DragType, MUIV_Listview_DragType_Immediate,
359 MUIA_Listview_List, ListObject,
360 InputListFrame,
361 MUIA_Background, MUII_ListBack,
362 MUIA_List_AdjustWidth, TRUE,
363 MUIA_List_AdjustHeight, TRUE,
364 MUIA_List_SourceArray, StdEntries,
365 End,
366 End,
367 Child, RectangleObject,
368 End,
369 End,
370 End,
371 End,
372 End,
373 End,
374 End;
376 if(app)
378 ULONG sigs;
379 ULONG running = 1;
380 BPTR fh;
382 if(argarray[5])
383 set(editorgad, MUIA_TextEditor_FixedFont, TRUE);
385 if(argarray[0] && (fh = Open((STRPTR)argarray[0], MODE_OLDFILE)))
387 STRPTR text = AllocVec(300*1024, 0L);
388 STRPTR buffer = text;
389 LONG size;
391 if(text)
393 size = Read(fh, text, (300*1024)-2);
394 text[size] = '\0';
395 Close(fh);
397 if(argarray[4])
399 while(*buffer != '\n' && buffer < &text[size])
401 while(*buffer++ != '\n');
405 if(argarray[3])
407 SetAttrs(editorgad,
408 MUIA_TextEditor_ImportHook, MUIV_TextEditor_ImportHook_MIMEQuoted,
409 MUIA_TextEditor_ImportWrap, 80,
410 MUIA_TextEditor_ExportHook, MUIV_TextEditor_ExportHook_EMail,
411 TAG_DONE);
413 else
415 if(argarray[2])
417 SetAttrs(editorgad,
418 MUIA_TextEditor_ImportHook, MUIV_TextEditor_ImportHook_MIME,
419 MUIA_TextEditor_ExportHook, MUIV_TextEditor_ExportHook_EMail,
420 TAG_DONE);
422 else
424 if(argarray[1])
426 SetAttrs(editorgad,
427 MUIA_TextEditor_ImportHook, MUIV_TextEditor_ImportHook_EMail,
428 MUIA_TextEditor_ExportHook, MUIV_TextEditor_ExportHook_EMail,
429 TAG_DONE);
434 set(editorgad, MUIA_TextEditor_Contents, buffer);
435 FreeVec(text);
436 set(editorgad, MUIA_TextEditor_ImportHook, MUIV_TextEditor_ImportHook_Plain);
440 DoMethod(window, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, MUIV_Notify_Application, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
442 DoMethod(window, MUIM_Notify, MUIA_Window_InputEvent, "f1", MUIV_Notify_Application, 2, MUIM_Application_ReturnID, MUIV_RunARexxScript);
444 DoMethod(flow, MUIM_Notify, MUIA_Cycle_Active, MUIV_EveryTime, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_Flow, MUIV_TriggerValue);
445 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_Flow, MUIV_EveryTime, flow, 3, MUIM_NoNotifySet, MUIA_Cycle_Active, MUIV_TriggerValue);
447 DoMethod(color, MUIM_Notify, MUIA_Cycle_Active, MUIV_EveryTime, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_Pen, MUIV_TriggerValue);
448 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_Pen, MUIV_EveryTime, color, 3, MUIM_NoNotifySet, MUIA_Cycle_Active, MUIV_TriggerValue);
450 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_StyleBold, MUIV_EveryTime, bold, 3, MUIM_NoNotifySet, MUIA_Selected, MUIV_TriggerValue);
451 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_StyleItalic, MUIV_EveryTime, italic, 3, MUIM_NoNotifySet, MUIA_Selected, MUIV_TriggerValue);
452 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_StyleUnderline, MUIV_EveryTime, underline, 3, MUIM_NoNotifySet, MUIA_Selected, MUIV_TriggerValue);
454 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_HasChanged, MUIV_EveryTime, ischanged, 3, MUIM_NoNotifySet, MUIA_Selected, MUIV_TriggerValue);
455 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_HasChanged, MUIV_EveryTime, ischanged, 3, MUIM_NoNotifySet, MUIA_Image_State, MUIV_TriggerValue);
456 DoMethod(ischanged, MUIM_Notify, MUIA_Selected, MUIV_EveryTime, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_HasChanged, MUIV_TriggerValue);
458 DoMethod(separator, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_InsertText, "\n\33c\33[s:2]\n");
460 DoMethod(config, MUIM_Notify, MUIA_Pressed, FALSE, MUIV_Notify_Application, 3, MUIM_Application_OpenConfigWindow, 1, "TextEditor.mcc");
462 DoMethod(clear, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "Clear");
463 DoMethod(clear, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_HasChanged, FALSE);
465 DoMethod(cut, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "Cut");
466 DoMethod(copy, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "Copy");
467 DoMethod(paste, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "Paste");
468 DoMethod(erase, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "Erase");
469 DoMethod(undo, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "Undo");
470 DoMethod(redo, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "Redo");
472 DoMethod(bold, MUIM_Notify, MUIA_Selected, MUIV_EveryTime, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_StyleBold, MUIV_TriggerValue);
473 DoMethod(italic, MUIM_Notify, MUIA_Selected, MUIV_EveryTime, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_StyleItalic, MUIV_TriggerValue);
474 DoMethod(underline, MUIM_Notify, MUIA_Selected, MUIV_EveryTime, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_StyleUnderline, MUIV_TriggerValue);
476 set(editorgad, MUIA_TextEditor_Slider, slider);
478 SetAttrs(window, MUIA_Window_ActiveObject, editorgad,
479 MUIA_Window_Open, TRUE,
480 TAG_DONE);
484 struct MsgPort *myport = NULL;
485 struct RexxMsg *rxmsg = NULL;
486 ULONG changed;
487 ULONG ReturnID;
488 BPTR rxstdout = 0L;
490 while((LONG)(ReturnID = DoMethod(app, MUIM_Application_NewInput, &sigs)) != (LONG)MUIV_Application_ReturnID_Quit)
492 if(ReturnID == MUIV_RunARexxScript && !myport)
494 struct MsgPort *rexxport;
495 const char *script = "Rexx:TextEditor/Demo.Rexx";
497 if((rexxport = FindPort((STRPTR)"REXX")) && (myport = CreateMsgPort()))
499 if(!rxstdout)
500 rxstdout = Open("CON://640/100/TextEditor-Demo, ARexx output:/Close/Wait/Auto/InActive", MODE_READWRITE);
502 rxmsg = CreateRexxMsg(myport, NULL, "TEXTEDITOR-DEMO.1");
503 rxmsg->rm_Action = RXCOMM;
504 rxmsg->rm_Stdin = rxstdout;
505 rxmsg->rm_Stdout = rxstdout;
506 rxmsg->rm_Args[0] = (IPTR)CreateArgstring((STRPTR)script, strlen(script));
507 PutMsg(rexxport, (struct Message *)rxmsg);
511 if(sigs)
513 sigs = Wait(sigs | SIGBREAKF_CTRL_C | (myport ? 1<<myport->mp_SigBit : 0));
514 if(myport && (sigs & 1<<myport->mp_SigBit))
516 GetMsg(myport);
517 if(!rxmsg->rm_Result1 && rxmsg->rm_Result2)
518 DeleteArgstring((STRPTR)rxmsg->rm_Result2);
520 DeleteArgstring((UBYTE *)rxmsg->rm_Args[0]);
521 DeleteRexxMsg(rxmsg);
522 DeleteMsgPort(myport);
523 myport = NULL;
526 if(sigs & SIGBREAKF_CTRL_C)
527 break;
531 if(rxstdout)
532 Close(rxstdout);
534 get(editorgad, MUIA_TextEditor_HasChanged, &changed);
535 if(argarray[0] && changed && !(sigs & SIGBREAKF_CTRL_C))
536 running = MUI_Request(app, window, 0L, "Warning", "_Proceed|*_Save|_Cancel", "\33cText '%s'\n is modified. Save it?", argarray[0]);
538 while(running == 0);
540 if(running == 2)
542 STRPTR text = (STRPTR)DoMethod(editorgad, MUIM_TextEditor_ExportText);
544 if(argarray[0] && (fh = Open((STRPTR)argarray[0], MODE_NEWFILE)))
546 Write(fh, text, strlen(text));
547 Close(fh);
549 FreeVec(text);
551 MUI_DisposeObject(app);
553 else
554 printf("Failed to create application\n");
556 MUI_DeleteCustomClass(editor_mcc);
558 else
559 printf("Failed to open TextEditor.mcc\n");
561 CloseLibs();
563 else
564 printf("Failed to open MUIMaster.Library V%d\n", MUIMASTER_VMIN);
566 FreeArgs(args);
568 else
570 char prgname[32];
571 LONG error = IoErr();
573 GetProgramName(prgname, 32);
574 PrintFault(error, prgname);
577 return 0;