Updated PCI IDs to latest snapshot.
[tangerine.git] / workbench / classes / gadgets / texteditor / demo / TextEditor-Demo.c
blobdec9b87cffe5a22b2837c81d80b52e8bda5f2e65
1 #include <StdIO.h>
2 #include <String.h>
3 #include <Exec/Tasks.h>
4 #include <Libraries/MUI.h>
5 #include <Rexx/Storage.h>
6 #include <Utility/Hooks.h>
7 #include <Proto/DOS.h>
8 #include <Proto/Exec.h>
9 #include <Proto/Graphics.h>
10 #include <Proto/MUIMaster.h>
11 #include <Proto/Intuition.h>
12 #include <Proto/RexxSysLib.h>
14 #include <TextEditor_mcc.h>
16 struct Library *MUIMasterBase;
17 Object *app, *window, *editorgad;
18 STRPTR StdEntries[] = {"Kind regards ", "Yours ", "Mvh ", NULL}; //"Duff@DIKU.DK", "http://www.DIKU.dk/students/duff/", NULL};
19 LONG cmap[8];
21 LONG ARexxHookCode (register __a1 struct RexxMsg *rexxmsg, register __a2 Object *app)
23 LONG result;
25 if(result = DoMethod(editorgad, MUIM_TextEditor_ARexxCmd, rexxmsg->rm_Args[0]))
27 if(result != TRUE)
29 set(app, MUIA_Application_RexxString, result);
30 FreeVec((APTR)result);
33 return(0);
35 struct Hook ARexxHook = {0, 0, (APTR)ARexxHookCode};
37 ULONG TextEditor_Dispatcher (register __a0 struct IClass *cl, register __a2 Object *obj, register __a1 struct MUIP_TextEditor_HandleError *msg)
39 switch(msg->MethodID)
41 case MUIM_Show:
43 struct ColorMap *cm = muiRenderInfo(obj)->mri_Screen->ViewPort.ColorMap;
45 cmap[0] = ObtainBestPenA(cm, 0x00<<24, 0x00<<24, 0x00<<24, NULL);
46 cmap[1] = ObtainBestPenA(cm, 0xff<<24, 0xff<<24, 0xff<<24, NULL);
47 cmap[2] = ObtainBestPenA(cm, 0xff<<24, 0x00<<24, 0x00<<24, NULL);
48 cmap[3] = ObtainBestPenA(cm, 0x00<<24, 0xff<<24, 0x00<<24, NULL);
49 cmap[4] = ObtainBestPenA(cm, 0x00<<24, 0xff<<24, 0xff<<24, NULL);
50 cmap[5] = ObtainBestPenA(cm, 0xff<<24, 0xff<<24, 0x00<<24, NULL);
51 cmap[6] = ObtainBestPenA(cm, 0x00<<24, 0x00<<24, 0xff<<24, NULL);
52 cmap[7] = ObtainBestPenA(cm, 0xff<<24, 0x00<<24, 0xff<<24, NULL);
53 break;
56 case MUIM_Hide:
58 struct ColorMap *cm = muiRenderInfo(obj)->mri_Screen->ViewPort.ColorMap;
59 int c;
61 for(c = 0; c < 8; c++)
63 if(cmap[c] >= 0)
65 ReleasePen(cm, cmap[c]);
68 break;
71 case MUIM_DragQuery:
73 return(TRUE);
76 case MUIM_DragDrop:
78 struct MUIP_DragDrop *drop_msg = (struct MUIP_DragDrop *)msg;
79 ULONG active;
81 if(GetAttr(MUIA_List_Active, drop_msg->obj, &active))
83 DoMethod(obj, MUIM_TextEditor_InsertText, StdEntries[active]);
85 break;
88 case MUIM_TextEditor_HandleError:
90 char *errortxt = NULL;
92 switch(msg->errorcode)
94 case Error_ClipboardIsEmpty:
95 errortxt = "\33cThe clipboard is empty.";
96 break;
97 case Error_ClipboardIsNotFTXT:
98 errortxt = "\33cThe clipboard does not contain text.";
99 break;
100 case Error_MacroBufferIsFull:
101 break;
102 case Error_MemoryAllocationFailed:
103 break;
104 case Error_NoAreaMarked:
105 errortxt = "\33cNo area marked.";
106 break;
107 case Error_NoMacroDefined:
108 break;
109 case Error_NothingToRedo:
110 errortxt = "\33cNothing to redo.";
111 break;
112 case Error_NothingToUndo:
113 errortxt = "\33cNothing to undo.";
114 break;
115 case Error_NotEnoughUndoMem:
116 errortxt = "There is not enough memory\nto keep the undo buffer.\n\nThe undobuffer is lost.";
117 break;
118 case Error_StringNotFound:
119 break;
120 case Error_NoBookmarkInstalled:
121 errortxt = "There is no bookmark installed!";
122 break;
123 case Error_BookmarkHasBeenLost:
124 errortxt = "Your bookmark has unfortunately been lost.";
125 break;
127 if(errortxt)
129 MUI_Request(app, window, 0L, NULL, "Continue", errortxt);
131 break;
134 return(DoSuperMethodA(cl, obj, (Msg)msg));
137 Object *ImageGad (STRPTR text, UBYTE key)
139 return(TextObject,
140 MUIA_Background, MUII_ButtonBack,
141 MUIA_Frame, MUIV_Frame_Button,
142 MUIA_Text_PreParse, "\33c",
143 MUIA_Font, MUIV_Font_Tiny,
144 MUIA_Text_Contents, text,
145 MUIA_Text_SetVMax, FALSE,
146 MUIA_FixHeight, 17,
147 MUIA_FixWidth, 24,
148 MUIA_InputMode, MUIV_InputMode_Toggle,
149 MUIA_ControlChar, key,
150 MUIA_CycleChain, TRUE,
151 End);
154 #define MUIV_RunARexxScript 0xad800000
156 VOID main (VOID)
158 struct RDArgs *args;
159 struct StackSwapStruct stackswap;
160 struct Task *mytask = FindTask(NULL);
161 Object *slider;
162 LONG argarray[6] = {0,0,0,0,0,0};
163 ULONG stacksize = (ULONG)mytask->tc_SPUpper-(ULONG)mytask->tc_SPLower+8192;
164 APTR newstack = AllocVec(stacksize, 0L);
166 stackswap.stk_Lower = newstack;
167 stackswap.stk_Upper = (ULONG)newstack+stacksize;
168 stackswap.stk_Pointer = (APTR)stackswap.stk_Upper;
169 if(newstack)
171 StackSwap(&stackswap);
173 if(args = ReadArgs("Filename/A,EMail/S,MIME/S,MIMEQuoted/S,SkipHeader/S,Fixed/S", argarray, NULL))
175 if(MUIMasterBase = OpenLibrary("muimaster.library", 11)) //MUIMASTER_VMIN))
177 struct MUI_CustomClass *editor_mcc;
178 Object *clear, *cut, *copy, *paste, *erase,
179 *bold, *italic, *underline, *ischanged, *undo, *redo,
180 *flow, *separator, *color, *config;
181 STRPTR flow_text[] = {"Left", "Center", "Right", NULL};
182 STRPTR colors[] = {"Normal", "Black", "White", "Red", "Gren", "Cyan", "Yellow", "Blue", "Magenta", NULL};
183 STRPTR classes[] = {"TextEditor.mcc"};
185 if(editor_mcc = MUI_CreateCustomClass(NULL, "TextEditor.mcc", NULL, 0, (void *)TextEditor_Dispatcher))
187 app = ApplicationObject,
188 MUIA_Application_Author, "Allan Odgaard",
189 MUIA_Application_Base, "TextEditor-Demo",
190 MUIA_Application_Copyright, "®1997 Allan Odgaard",
191 MUIA_Application_Description, "TextEditor.mcc demonstration program",
192 MUIA_Application_RexxHook, &ARexxHook,
193 MUIA_Application_Title, "TextEditor-Demo",
194 MUIA_Application_Version, "$VER: TextEditor-Demo V1.0 (6-Aug-97)",
195 MUIA_Application_UsedClasses, classes,
196 SubWindow, window = WindowObject,
197 MUIA_Window_Title, "TextEditor-Demo",
198 MUIA_Window_ID, 'MAIN',
199 WindowContents, VGroup,
200 Child, VGroup,
201 MUIA_Background, MUII_GroupBack,
202 MUIA_Frame, MUIV_Frame_Group,
204 Child, HGroup,
206 Child, RowGroup(2),
207 Child, cut = MUI_MakeObject(MUIO_Button, "Cut"),
208 Child, paste = MUI_MakeObject(MUIO_Button, "Paste"),
209 Child, undo = MUI_MakeObject(MUIO_Button, "Undo"),
210 Child, flow = MUI_MakeObject(MUIO_Cycle, NULL, flow_text),
211 Child, separator = MUI_MakeObject(MUIO_Button, "Separator"),
212 Child, clear = MUI_MakeObject(MUIO_Button, "Clear _Text"),
214 Child, copy = MUI_MakeObject(MUIO_Button, "Copy"),
215 Child, erase = MUI_MakeObject(MUIO_Button, "Erase"),
216 Child, redo = MUI_MakeObject(MUIO_Button, "Redo"),
217 Child, color = MUI_MakeObject(MUIO_Cycle, NULL, colors),
218 Child, config = MUI_MakeObject(MUIO_Button, "Config..."),
219 Child, HGroup,
220 Child, ischanged = MUI_MakeObject(MUIO_Checkmark, "Is changed?"),
221 Child, TextObject,
222 MUIA_Text_Contents, "Is changed?",
223 End,
224 End,
225 End,
227 Child, RectangleObject, End,
229 Child, bold = ImageGad("\33I[5:ProgDir:Bold.Brush]\n\n\nBold", 'b'),
230 Child, italic = ImageGad("\33I[5:ProgDir:Italic.Brush]\n\n\nItalic", 'i'),
231 Child, underline = ImageGad("\33I[5:ProgDir:Underline.Brush]\n\n\nUnderline", 'u'),
232 End,
234 Child, HGroup,
236 Child, HGroup,
237 MUIA_Group_Spacing, 0,
238 Child, editorgad = NewObject(editor_mcc->mcc_Class, NULL,
239 MUIA_TextEditor_Slider, slider,
240 MUIA_TextEditor_ColorMap, cmap,
241 MUIA_CycleChain, TRUE,
242 End,
243 Child, slider = ScrollbarObject,
244 End,
245 End,
247 Child, VGroup,
248 Child, ListviewObject,
249 MUIA_Listview_DragType, MUIV_Listview_DragType_Immediate,
250 MUIA_Listview_List, ListObject,
251 InputListFrame,
252 MUIA_Background, MUII_ListBack,
253 MUIA_List_AdjustWidth, TRUE,
254 MUIA_List_AdjustHeight, TRUE,
255 MUIA_List_SourceArray, StdEntries,
256 End,
257 End,
258 Child, RectangleObject,
259 End,
260 End,
261 End,
262 End,
263 End,
264 End,
265 End;
267 if(app)
269 ULONG sigs;
270 ULONG running = 1;
271 BPTR fh;
273 if(argarray[5])
275 set(editorgad, MUIA_TextEditor_FixedFont, TRUE);
277 if(fh = Open((STRPTR)argarray[0], MODE_OLDFILE))
279 STRPTR text = AllocVec(300*1024, 0L);
280 STRPTR buffer = text;
281 LONG size;
283 if(text)
285 size = Read(fh, text, (300*1024)-2);
286 text[size] = '\0';
287 Close(fh);
289 if(argarray[4])
291 while(*buffer != '\n' && buffer < &text[size])
293 while(*buffer++ != '\n');
297 if(argarray[3])
299 SetAttrs(editorgad,
300 MUIA_TextEditor_ImportHook, MUIV_TextEditor_ImportHook_MIMEQuoted,
301 MUIA_TextEditor_ImportWrap, 80,
302 MUIA_TextEditor_ExportHook, MUIV_TextEditor_ExportHook_EMail,
303 TAG_DONE);
305 else
307 if(argarray[2])
309 SetAttrs(editorgad,
310 MUIA_TextEditor_ImportHook, MUIV_TextEditor_ImportHook_MIME,
311 MUIA_TextEditor_ExportHook, MUIV_TextEditor_ExportHook_EMail,
312 TAG_DONE);
314 else
316 if(argarray[1])
318 SetAttrs(editorgad,
319 MUIA_TextEditor_ImportHook, MUIV_TextEditor_ImportHook_EMail,
320 MUIA_TextEditor_ExportHook, MUIV_TextEditor_ExportHook_EMail,
321 TAG_DONE);
325 set(editorgad, MUIA_TextEditor_Contents, buffer);
326 FreeVec(text);
327 set(editorgad, MUIA_TextEditor_ImportHook, MUIV_TextEditor_ImportHook_Plain);
331 DoMethod(window, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, MUIV_Notify_Application, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
333 DoMethod(window, MUIM_Notify, MUIA_Window_InputEvent, "f1", MUIV_Notify_Application, 2, MUIM_Application_ReturnID, MUIV_RunARexxScript);
335 DoMethod(flow, MUIM_Notify, MUIA_Cycle_Active, MUIV_EveryTime, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_Flow, MUIV_TriggerValue);
336 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_Flow, MUIV_EveryTime, flow, 3, MUIM_NoNotifySet, MUIA_Cycle_Active, MUIV_TriggerValue);
338 DoMethod(color, MUIM_Notify, MUIA_Cycle_Active, MUIV_EveryTime, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_Pen, MUIV_TriggerValue);
339 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_Pen, MUIV_EveryTime, color, 3, MUIM_NoNotifySet, MUIA_Cycle_Active, MUIV_TriggerValue);
341 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_StyleBold, MUIV_EveryTime, bold, 3, MUIM_NoNotifySet, MUIA_Selected, MUIV_TriggerValue);
342 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_StyleItalic, MUIV_EveryTime, italic, 3, MUIM_NoNotifySet, MUIA_Selected, MUIV_TriggerValue);
343 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_StyleUnderline, MUIV_EveryTime, underline, 3, MUIM_NoNotifySet, MUIA_Selected, MUIV_TriggerValue);
345 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_HasChanged, MUIV_EveryTime, ischanged, 3, MUIM_NoNotifySet, MUIA_Selected, MUIV_TriggerValue);
346 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_HasChanged, MUIV_EveryTime, ischanged, 3, MUIM_NoNotifySet, MUIA_Image_State, MUIV_TriggerValue);
347 DoMethod(ischanged, MUIM_Notify, MUIA_Selected, MUIV_EveryTime, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_HasChanged, MUIV_TriggerValue);
349 DoMethod(separator, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_InsertText, "\n\33c\33[s:2]\n");
351 DoMethod(config, MUIM_Notify, MUIA_Pressed, FALSE, MUIV_Notify_Application, 2, MUIM_Application_OpenConfigWindow, 1, "TextEditor.mcc");
353 DoMethod(clear, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "Clear");
354 DoMethod(clear, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_HasChanged, FALSE);
356 DoMethod(cut, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "Cut");
357 DoMethod(copy, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "Copy");
358 DoMethod(paste, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "Paste");
359 DoMethod(erase, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "Erase");
360 DoMethod(undo, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "Undo");
361 DoMethod(redo, MUIM_Notify, MUIA_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "Redo");
363 DoMethod(bold, MUIM_Notify, MUIA_Selected, MUIV_EveryTime, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_StyleBold, MUIV_TriggerValue);
364 DoMethod(italic, MUIM_Notify, MUIA_Selected, MUIV_EveryTime, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_StyleItalic, MUIV_TriggerValue);
365 DoMethod(underline, MUIM_Notify, MUIA_Selected, MUIV_EveryTime, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_StyleUnderline, MUIV_TriggerValue);
367 SetAttrs(window, MUIA_Window_ActiveObject, editorgad,
368 MUIA_Window_Open, TRUE,
369 TAG_DONE);
371 do {
372 struct MsgPort *myport = NULL;
373 struct RexxMsg *rxmsg;
374 ULONG changed;
375 ULONG ReturnID;
376 BPTR rxstdout = NULL;
378 while((ReturnID = DoMethod(app, MUIM_Application_NewInput, &sigs)) != MUIV_Application_ReturnID_Quit)
380 if(ReturnID == MUIV_RunARexxScript && !myport)
382 struct MsgPort *rexxport;
383 STRPTR script = "Rexx:TextEditor/Demo.Rexx";
385 if((rexxport = FindPort("REXX")) && (myport = CreateMsgPort()))
387 if(!rxstdout)
388 rxstdout = Open("Con://640/100/TextEditor-Demo, ARexx output:/Close/Wait/Auto/InActive", MODE_READWRITE);
389 rxmsg = CreateRexxMsg(myport, NULL, "TEXTEDITOR-DEMO.1");
390 rxmsg->rm_Action = RXCOMM;
391 rxmsg->rm_Stdin = rxstdout;
392 rxmsg->rm_Stdout = rxstdout;
393 rxmsg->rm_Args[0] = CreateArgstring(script, strlen(script));
394 PutMsg(rexxport, (struct Message *)rxmsg);
397 if(sigs)
399 sigs = Wait(sigs | SIGBREAKF_CTRL_C | (myport ? 1<<myport->mp_SigBit : 0));
400 if(myport && (sigs & 1<<myport->mp_SigBit))
402 GetMsg(myport);
403 if(!rxmsg->rm_Result1 && rxmsg->rm_Result2)
404 DeleteArgstring((STRPTR)rxmsg->rm_Result2);
405 DeleteArgstring(rxmsg->rm_Args[0]);
406 DeleteRexxMsg(rxmsg);
407 DeleteMsgPort(myport);
408 myport = NULL;
410 if(sigs & SIGBREAKF_CTRL_C)
411 break;
414 if(rxstdout)
415 Close(rxstdout);
417 get(editorgad, MUIA_TextEditor_HasChanged, &changed);
418 if(changed && !(sigs & SIGBREAKF_CTRL_C))
419 running = MUI_Request(app, window, 0L, "Warning", "_Proceed|*_Save|_Cancel", "\33cText '%s'\n is modified. Save it?", argarray[0]);
421 } while(running == 0);
423 if(running == 2)
425 STRPTR text = (STRPTR)DoMethod(editorgad, MUIM_TextEditor_ExportText);
427 if(fh = Open((STRPTR)argarray[0], MODE_NEWFILE))
429 Write(fh, text, strlen(text));
430 Close(fh);
432 FreeVec(text);
434 MUI_DisposeObject(app);
436 else printf("Failed to create application\n");
437 MUI_DeleteCustomClass(editor_mcc);
439 else printf("Failed to open TextEditor.mcc\n");
440 CloseLibrary(MUIMasterBase);
442 else printf("Failed to open MUIMaster.Library V%d\n", MUIMASTER_VMIN);
443 FreeArgs(args);
445 else
447 UBYTE prgname[32];
448 LONG error = IoErr();
450 GetProgramName(prgname, 32);
451 PrintFault(error, prgname);
453 StackSwap(&stackswap);
454 FreeVec(newstack);
458 VOID wbmain (VOID)