arch/cpu.resource: remove dead code
[AROS.git] / workbench / classes / zune / texteditor / demo / TextEditor-Demo-Toolbar.c
blob9adf3c8449ebd5b30180434caa64076b4db6c865
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 <MUI/InfoText_mcc.h>
15 #include <MUI/TextEditor_mcc.h>
16 #include <MUI/Toolbar_mcc.h>
18 struct Library *MUIMasterBase;
19 Object *app, *window, *editorgad, *StatusLine;
20 LONG cmap[8];
22 LONG ARexxHookCode (register __a1 struct RexxMsg *rexxmsg, register __a2 Object *app)
24 LONG result;
26 if(result = DoMethod(editorgad, MUIM_TextEditor_ARexxCmd, rexxmsg->rm_Args[0]))
28 if(result != TRUE)
30 set(app, MUIA_Application_RexxString, result);
31 FreeVec((APTR)result);
34 return(0);
36 struct Hook ARexxHook = {0, 0, (APTR)ARexxHookCode};
38 ULONG TextEditor_Dispatcher (register __a0 struct IClass *cl, register __a2 Object *obj, register __a1 struct MUIP_TextEditor_HandleError *msg)
40 switch(msg->MethodID)
42 case MUIM_Show:
44 struct ColorMap *cm = muiRenderInfo(obj)->mri_Screen->ViewPort.ColorMap;
45 struct Window *procwindow;
47 get(muiRenderInfo(obj)->mri_WindowObject, MUIA_Window_Window, &procwindow);
48 ((struct Process *)FindTask(NULL))->pr_WindowPtr = procwindow;
50 cmap[0] = ObtainBestPenA(cm, 0x00<<24, 0x00<<24, 0x00<<24, NULL);
51 cmap[1] = ObtainBestPenA(cm, 0xff<<24, 0xff<<24, 0xff<<24, NULL);
52 cmap[2] = ObtainBestPenA(cm, 0xff<<24, 0x00<<24, 0x00<<24, NULL);
53 cmap[3] = ObtainBestPenA(cm, 0x00<<24, 0xff<<24, 0x00<<24, NULL);
54 cmap[4] = ObtainBestPenA(cm, 0x00<<24, 0xff<<24, 0xff<<24, NULL);
55 cmap[5] = ObtainBestPenA(cm, 0xff<<24, 0xff<<24, 0x00<<24, NULL);
56 cmap[6] = ObtainBestPenA(cm, 0x00<<24, 0x00<<24, 0xff<<24, NULL);
57 cmap[7] = ObtainBestPenA(cm, 0xff<<24, 0x00<<24, 0xff<<24, NULL);
58 break;
61 case MUIM_Hide:
63 struct ColorMap *cm = muiRenderInfo(obj)->mri_Screen->ViewPort.ColorMap;
64 int c;
66 for(c = 0; c < 8; c++)
68 if(cmap[c] >= 0)
70 ReleasePen(cm, cmap[c]);
73 break;
76 case MUIM_DragQuery:
78 return(TRUE);
81 /* case MUIM_DragDrop:
83 struct MUIP_DragDrop *drop_msg = (struct MUIP_DragDrop *)msg;
84 ULONG active;
86 if(GetAttr(MUIA_List_Active, drop_msg->obj, &active))
88 DoMethod(obj, MUIM_TextEditor_InsertText, StdEntries[active]);
90 break;
93 case MUIM_TextEditor_HandleError:
95 char *errortxt = NULL;
97 switch(msg->errorcode)
99 case Error_ClipboardIsEmpty:
100 errortxt = "The clipboard is empty.";
101 break;
102 case Error_ClipboardIsNotFTXT:
103 errortxt = "The clipboard does not contain text.";
104 break;
105 case Error_MacroBufferIsFull:
106 break;
107 case Error_MemoryAllocationFailed:
108 break;
109 case Error_NoAreaMarked:
110 errortxt = "No area marked.";
111 break;
112 case Error_NoMacroDefined:
113 break;
114 case Error_NothingToRedo:
115 errortxt = "Nothing to redo.";
116 break;
117 case Error_NothingToUndo:
118 errortxt = "Nothing to undo.";
119 break;
120 case Error_NotEnoughUndoMem:
121 errortxt = "Out of memory! The undobuffer is lost.";
122 break;
123 case Error_StringNotFound:
124 break;
125 case Error_NoBookmarkInstalled:
126 errortxt = "There is no bookmark installed!";
127 break;
128 case Error_BookmarkHasBeenLost:
129 errortxt = "Your bookmark has unfortunately been lost.";
130 break;
132 if(errortxt)
134 set(StatusLine, MUIA_InfoText_Contents, errortxt);
136 break;
139 return(DoSuperMethodA(cl, obj, (Msg)msg));
143 #define MUIV_RunARexxScript 0xad800000
145 enum
147 NEW = 0, OPEN, x1, CUT, COPY, PASTE, UNDO, x2, BOLD, ITALIC, UNDERLINE, COLORED, x3, LEFT, CENTER, RIGHT, x4, AREXX
151 VOID main (VOID)
153 struct RDArgs *args;
154 struct StackSwapStruct stackswap;
155 struct Task *mytask = FindTask(NULL);
156 Object *slider;
157 LONG argarray[6] = {0,0,0,0,0,0};
158 ULONG stacksize = (ULONG)mytask->tc_SPUpper-(ULONG)mytask->tc_SPLower+8192;
159 APTR newstack = AllocVec(stacksize, 0L);
160 UBYTE wintitle[64];
162 stackswap.stk_Lower = newstack;
163 stackswap.stk_Upper = (ULONG)newstack+stacksize;
164 stackswap.stk_Pointer = (APTR)stackswap.stk_Upper;
165 if(newstack)
167 StackSwap(&stackswap);
169 if(args = ReadArgs("Filename,EMail/S,MIME/S,MIMEQuoted/S,SkipHeader/S,Fixed/S", argarray, NULL))
171 if(MUIMasterBase = OpenLibrary("muimaster.library", MUIMASTER_VMIN))
173 struct MUI_CustomClass *editor_mcc;
174 Object /**color, */*ToolstripObj;
175 STRPTR colors[] = {"Normal", "Black", "White", "Red", "Gren", "Cyan", "Yellow", "Blue", "Magenta", NULL};
176 STRPTR classes[] = {"TextEditor.mcc", "Toolbar.mcc", NULL};
178 struct MUIP_Toolbar_Description Toolstrip[] =
180 { TDT_BUTTON, 0, 0L, NULL, "Clear document contents", 0},
181 { TDT_IGNORE, 0, TDF_GHOSTED, NULL, "Load text from file", 0},
182 { TDT_SPACE, 0, 0L, NULL, NULL, 0 },
183 { TDT_BUTTON, 0, 0L, NULL, "Cut marked text", 0},
184 { TDT_BUTTON, 0, 0L, NULL, "Copy marked text", 0},
185 { TDT_BUTTON, 0, 0L, NULL, "Paste from clipboard", 0},
186 { TDT_BUTTON, 0, 0L, NULL, "Undo last change", 0},
187 { TDT_SPACE, 0, 0L, NULL, NULL, 0 },
188 { TDT_BUTTON, 'b', TDF_TOGGLE, NULL, "Bold text", 0},
189 { TDT_BUTTON, 'i', TDF_TOGGLE, NULL, "Italic text", 0},
190 { TDT_BUTTON, 'u', TDF_TOGGLE, NULL, "Underlined text", 0},
191 { TDT_BUTTON, 'r', TDF_TOGGLE, NULL, "Colored text", 0},
192 { TDT_SPACE, 0, 0L, NULL, NULL, 0 },
193 { TDT_BUTTON, '[', TDF_RADIOTOGGLE | TDF_SELECTED, NULL, "Left aligned", 1<<CENTER | 1<<RIGHT},
194 { TDT_BUTTON, '|', TDF_RADIOTOGGLE, NULL, "Centered", 1<<LEFT | 1<<RIGHT},
195 { TDT_BUTTON, ']', TDF_RADIOTOGGLE, NULL, "Right aligned", 1<<LEFT | 1<<CENTER},
196 // { TDT_SPACE, 0, 0L, NULL, NULL, 0 },
197 { TDT_IGNORE, 0, TDF_GHOSTED, NULL, "Run an ARexx script", 0},
198 { TDT_END, 0, 0L, NULL, NULL, 0 }
201 if((editor_mcc = MUI_CreateCustomClass(NULL, "TextEditor.mcc", NULL, 0, (void *)TextEditor_Dispatcher))/* && (toolbar_mcc = MUI_CreateCustomClass(NULL, "Toolbar.mcc", NULL, 0, (void *)Toolbar_Dispatcher))*/)
203 app = ApplicationObject,
204 MUIA_Application_Author, "Allan Odgaard",
205 MUIA_Application_Base, "TextEditor-Demo",
206 MUIA_Application_Copyright, "ยฎ1997 Allan Odgaard",
207 MUIA_Application_Description, "TextEditor.mcc demonstration program",
208 MUIA_Application_RexxHook, &ARexxHook,
209 MUIA_Application_Title, "TextEditor-Demo",
210 MUIA_Application_Version, "$VER: TextEditor-Demo V1.0 (6-Aug-97)",
211 MUIA_Application_UsedClasses, classes,
212 SubWindow, window = WindowObject,
213 MUIA_Window_ID, 'MAIN',
214 WindowContents, VGroup,
215 Child, VGroup,
216 MUIA_Background, MUII_GroupBack,
217 MUIA_Frame, MUIV_Frame_Group,
219 Child, HGroup,
220 // Child, VirtgroupObject,
221 Child, ToolstripObj = ToolbarObject,
222 MUIA_Toolbar_Description, Toolstrip,
223 MUIA_Toolbar_ImageNormal, "ProgDir:Toolstrip.ILBM",
224 MUIA_Toolbar_ImageGhost, "ProgDir:Toolstrip_G.ILBM",
225 MUIA_Toolbar_ImageType, MUIV_Toolbar_ImageType_File,
226 MUIA_Font, MUIV_Font_Tiny,
227 End,
228 // End,
229 // Child, color = MUI_MakeObject(MUIO_Cycle, NULL, colors),
230 Child, RectangleObject, End,
231 End,
233 Child, HGroup,
234 Child, HGroup,
235 MUIA_Group_Spacing, 0,
236 Child, editorgad = NewObject(editor_mcc->mcc_Class, NULL,
237 MUIA_TextEditor_Slider, slider,
238 MUIA_TextEditor_ColorMap, cmap,
239 MUIA_CycleChain, TRUE,
240 End,
241 Child, slider = ScrollbarObject,
242 End,
243 End,
244 End,
245 Child, StatusLine = InfoTextObject,
246 End,
248 End,
249 End,
250 End,
251 End;
253 if(app)
255 STRPTR ARexxPort;
256 ULONG sigs;
257 ULONG running = 1;
258 BPTR fh;
260 if(argarray[5])
262 set(editorgad, MUIA_TextEditor_FixedFont, TRUE);
264 if(argarray[0] && (fh = Open((STRPTR)argarray[0], MODE_OLDFILE)))
266 STRPTR text = AllocVec(300*1024, 0L);
267 STRPTR buffer = text;
268 LONG size;
270 if(text)
272 size = Read(fh, text, (300*1024)-2);
273 text[size] = '\0';
274 Close(fh);
276 if(argarray[4])
278 while(*buffer != '\n' && buffer < &text[size])
280 while(*buffer++ != '\n');
284 if(argarray[3])
286 SetAttrs(editorgad,
287 MUIA_TextEditor_ImportHook, MUIV_TextEditor_ImportHook_MIMEQuoted,
288 MUIA_TextEditor_ImportWrap, 80,
289 MUIA_TextEditor_ExportHook, MUIV_TextEditor_ExportHook_EMail,
290 TAG_DONE);
292 else
294 if(argarray[2])
296 SetAttrs(editorgad,
297 MUIA_TextEditor_ImportHook, MUIV_TextEditor_ImportHook_MIME,
298 MUIA_TextEditor_ExportHook, MUIV_TextEditor_ExportHook_EMail,
299 TAG_DONE);
301 else
303 if(argarray[1])
305 SetAttrs(editorgad,
306 MUIA_TextEditor_ImportHook, MUIV_TextEditor_ImportHook_EMail,
307 MUIA_TextEditor_ExportHook, MUIV_TextEditor_ExportHook_EMail,
308 TAG_DONE);
312 set(editorgad, MUIA_TextEditor_Contents, buffer);
313 FreeVec(text);
314 set(editorgad, MUIA_TextEditor_ImportHook, MUIV_TextEditor_ImportHook_Plain);
318 DoMethod(ToolstripObj, MUIM_Notify, MUIA_Toolbar_HelpString, MUIV_EveryTime, StatusLine, 3, MUIM_Set, MUIA_Text_Contents, MUIV_TriggerValue);
320 DoMethod(window, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, MUIV_Notify_Application, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
321 DoMethod(window, MUIM_Notify, MUIA_Window_InputEvent, "f1", MUIV_Notify_Application, 2, MUIM_Application_ReturnID, MUIV_RunARexxScript);
323 /* Mew, cut, copy, paste & undo */
324 DoMethod(ToolstripObj, MUIM_Toolbar_Notify, NEW, MUIV_Toolbar_Notify_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "CLEAR");
325 DoMethod(ToolstripObj, MUIM_Toolbar_Notify, NEW, MUIV_Toolbar_Notify_Pressed, FALSE, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_HasChanged, FALSE);
326 DoMethod(ToolstripObj, MUIM_Toolbar_Notify, CUT, MUIV_Toolbar_Notify_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "CUT");
327 DoMethod(ToolstripObj, MUIM_Toolbar_Notify, COPY, MUIV_Toolbar_Notify_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "COPY");
328 DoMethod(ToolstripObj, MUIM_Toolbar_Notify, PASTE, MUIV_Toolbar_Notify_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "PASTE");
329 DoMethod(ToolstripObj, MUIM_Toolbar_Notify, UNDO, MUIV_Toolbar_Notify_Pressed, FALSE, editorgad, 2, MUIM_TextEditor_ARexxCmd, "UNDO");
331 /* Bold, italic, underline & colored */
332 DoMethod(ToolstripObj, MUIM_Toolbar_Notify, BOLD, MUIV_Toolbar_Notify_Pressed, MUIV_EveryTime, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_StyleBold, MUIV_TriggerValue);
333 DoMethod(ToolstripObj, MUIM_Toolbar_Notify, ITALIC, MUIV_Toolbar_Notify_Pressed, MUIV_EveryTime, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_StyleItalic, MUIV_TriggerValue);
334 DoMethod(ToolstripObj, MUIM_Toolbar_Notify, UNDERLINE, MUIV_Toolbar_Notify_Pressed, MUIV_EveryTime, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_StyleUnderline, MUIV_TriggerValue);
335 DoMethod(ToolstripObj, MUIM_Toolbar_Notify, COLORED, MUIV_Toolbar_Notify_Pressed, TRUE, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_Pen, 7);
336 DoMethod(ToolstripObj, MUIM_Toolbar_Notify, COLORED, MUIV_Toolbar_Notify_Pressed, FALSE, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_Pen, 0);
337 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_StyleBold, MUIV_EveryTime, ToolstripObj, 4, MUIM_Toolbar_Set, BOLD, MUIV_Toolbar_Set_Selected, MUIV_TriggerValue);
338 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_StyleItalic, MUIV_EveryTime, ToolstripObj, 4, MUIM_Toolbar_Set, ITALIC, MUIV_Toolbar_Set_Selected, MUIV_TriggerValue);
339 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_StyleUnderline, MUIV_EveryTime, ToolstripObj, 4, MUIM_Toolbar_Set, UNDERLINE, MUIV_Toolbar_Set_Selected, MUIV_TriggerValue);
340 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_Pen, 7, ToolstripObj, 4, MUIM_Toolbar_Set, COLORED, MUIV_Toolbar_Set_Selected, TRUE);
341 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_Pen, 0, ToolstripObj, 4, MUIM_Toolbar_Set, COLORED, MUIV_Toolbar_Set_Selected, FALSE);
343 /* Left, center & right */
344 DoMethod(ToolstripObj, MUIM_Toolbar_Notify, LEFT, MUIV_Toolbar_Notify_Pressed, TRUE, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_Flow, MUIV_TextEditor_Flow_Left);
345 DoMethod(ToolstripObj, MUIM_Toolbar_Notify, CENTER, MUIV_Toolbar_Notify_Pressed, TRUE, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_Flow, MUIV_TextEditor_Flow_Center);
346 DoMethod(ToolstripObj, MUIM_Toolbar_Notify, RIGHT, MUIV_Toolbar_Notify_Pressed, TRUE, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_Flow, MUIV_TextEditor_Flow_Right);
347 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_Flow, MUIV_TextEditor_Flow_Left, ToolstripObj, 4, MUIM_Toolbar_Set, LEFT, MUIV_Toolbar_Set_Selected, TRUE);
348 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_Flow, MUIV_TextEditor_Flow_Center, ToolstripObj, 4, MUIM_Toolbar_Set, CENTER, MUIV_Toolbar_Set_Selected, TRUE);
349 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_Flow, MUIV_TextEditor_Flow_Right, ToolstripObj, 4, MUIM_Toolbar_Set, RIGHT, MUIV_Toolbar_Set_Selected, TRUE);
351 /* AreaMarked? UndoAvailable? */
352 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_AreaMarked, MUIV_EveryTime, ToolstripObj, 6, MUIM_Toolbar_MultiSet, MUIV_Toolbar_Set_Ghosted, MUIV_NotTriggerValue, CUT, COPY, -1);
353 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_UndoAvailable, MUIV_EveryTime, ToolstripObj, 4, MUIM_Toolbar_Set, UNDO, MUIV_Toolbar_Set_Ghosted, MUIV_NotTriggerValue);
355 /* Unsupported... */
356 /* DoMethod(color, MUIM_Notify, MUIA_Cycle_Active, MUIV_EveryTime, editorgad, 3, MUIM_NoNotifySet, MUIA_TextEditor_Pen, MUIV_TriggerValue);
357 DoMethod(editorgad, MUIM_Notify, MUIA_TextEditor_Pen, MUIV_EveryTime, color, 3, MUIM_NoNotifySet, MUIA_Cycle_Active, MUIV_TriggerValue);
359 get(app, MUIA_Application_Base, &ARexxPort);
360 sprintf(wintitle, "TextEditor-Demo ยท ArexxPort: %s", ARexxPort);
361 SetAttrs(window, MUIA_Window_ActiveObject, editorgad,
362 MUIA_Window_Title, wintitle,
363 MUIA_Window_Open, TRUE,
364 TAG_DONE);
366 do {
367 struct MsgPort *myport = NULL;
368 struct RexxMsg *rxmsg;
369 ULONG changed;
370 ULONG ReturnID;
371 BPTR rxstdout = NULL;
373 while((ReturnID = DoMethod(app, MUIM_Application_NewInput, &sigs)) != MUIV_Application_ReturnID_Quit)
375 if(ReturnID == MUIV_RunARexxScript && !myport)
377 struct MsgPort *rexxport;
378 STRPTR script = "Rexx:TextEditor/Demo.Rexx";
380 if((rexxport = FindPort("REXX")) && (myport = CreateMsgPort()))
382 if(!rxstdout)
383 rxstdout = Open("Con://640/100/TextEditor-Demo, ARexx output:/Close/Wait/Auto/InActive", MODE_READWRITE);
384 rxmsg = CreateRexxMsg(myport, NULL, ARexxPort);
385 rxmsg->rm_Action = RXCOMM;
386 rxmsg->rm_Stdin = rxstdout;
387 rxmsg->rm_Stdout = rxstdout;
388 rxmsg->rm_Args[0] = CreateArgstring(script, strlen(script));
389 PutMsg(rexxport, (struct Message *)rxmsg);
392 if(sigs)
394 sigs = Wait(sigs | SIGBREAKF_CTRL_C | (myport ? 1<<myport->mp_SigBit : 0));
395 if(myport && (sigs & 1<<myport->mp_SigBit))
397 GetMsg(myport);
398 if(!rxmsg->rm_Result1 && rxmsg->rm_Result2)
399 DeleteArgstring((STRPTR)rxmsg->rm_Result2);
400 DeleteArgstring(rxmsg->rm_Args[0]);
401 DeleteRexxMsg(rxmsg);
402 DeleteMsgPort(myport);
403 myport = NULL;
405 if(sigs & SIGBREAKF_CTRL_C)
406 break;
409 if(rxstdout)
410 Close(rxstdout);
412 get(editorgad, MUIA_TextEditor_HasChanged, &changed);
413 if(argarray[0] && changed && !(sigs & SIGBREAKF_CTRL_C))
414 running = MUI_Request(app, window, 0L, "Warning", "_Proceed|*_Save|_Cancel", "\33cText '%s'\n is modified. Save it?", argarray[0]);
416 } while(running == 0);
418 if(running == 2)
420 STRPTR text = (STRPTR)DoMethod(editorgad, MUIM_TextEditor_ExportText);
422 if(fh = Open((STRPTR)argarray[0], MODE_NEWFILE))
424 Write(fh, text, strlen(text));
425 Close(fh);
427 FreeVec(text);
429 MUI_DisposeObject(app);
431 else printf("Failed to create application\n");
432 MUI_DeleteCustomClass(editor_mcc);
434 else printf("Failed to open TextEditor.mcc\n");
435 CloseLibrary(MUIMasterBase);
437 else printf("Failed to open MUIMaster.Library V%d\n", MUIMASTER_VMIN);
438 FreeArgs(args);
440 else
442 UBYTE prgname[32];
443 LONG error = IoErr();
445 GetProgramName(prgname, 32);
446 PrintFault(error, prgname);
448 StackSwap(&stackswap);
449 FreeVec(newstack);
453 VOID wbmain (VOID)