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
31 ***************************************************************************/
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>
57 struct GfxBase
*GfxBase
;
58 struct IntuitionBase
*IntuitionBase
;
59 struct Library
*MUIMasterBase
;
61 struct Library
*RexxSysBase
;
63 struct RxsLib
*RexxSysBase
;
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
;
77 #ifndef MUIA_Application_UsedClasses
78 #define MUIA_Application_UsedClasses 0x8042e9a7 /* V20 STRPTR * i.. */
80 #define MUIV_RunARexxScript 0xad800000
84 Object
*app
, *window
, *editorgad
;
85 const char *StdEntries
[] = { "Kind regards ", "Yours ", "Mvh ", NULL
};
88 HOOKPROTONH(ARexxHookCode
, LONG
, Object
*app
, struct RexxMsg
*rexxmsg
)
92 result
= DoMethod(editorgad
, MUIM_TextEditor_ARexxCmd
, rexxmsg
->rm_Args
[0]);
98 set(app
, MUIA_Application_RexxString
, result
);
99 FreeVec((APTR
)result
);
105 MakeStaticHook(ARexxHook
, ARexxHookCode
);
107 DISPATCHER(TextEditor_Dispatcher
)
109 switch(msg
->MethodID
)
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
);
137 struct ColorMap
*cm
= muiRenderInfo(obj
)->mri_Screen
->ViewPort
.ColorMap
;
140 set(obj
, MUIA_TextEditor_ColorMap
, NULL
);
142 for(c
= 0; c
< 8; c
++)
145 ReleasePen(cm
, cmap
[c
]);
157 struct MUIP_DragDrop
*drop_msg
= (struct MUIP_DragDrop
*)msg
;
160 if(GetAttr(MUIA_List_Active
, drop_msg
->obj
, (IPTR
*)&active
))
161 DoMethod(obj
, MUIM_TextEditor_InsertText
, StdEntries
[active
]);
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.";
178 case Error_ClipboardIsNotFTXT
:
179 errortxt
= "\33cThe clipboard does not contain text.";
182 case Error_MacroBufferIsFull
:
183 case Error_MemoryAllocationFailed
:
184 case Error_NoMacroDefined
:
185 case Error_StringNotFound
:
188 case Error_NoAreaMarked
:
189 errortxt
= "\33cNo area marked.";
192 case Error_NothingToRedo
:
193 errortxt
= "\33cNothing to redo.";
196 case Error_NothingToUndo
:
197 errortxt
= "\33cNothing to undo.";
200 case Error_NotEnoughUndoMem
:
201 errortxt
= "There is not enough memory\nto keep the undo buffer.\n\nThe undobuffer is lost.";
204 case Error_NoBookmarkInstalled
:
205 errortxt
= "There is no bookmark installed!";
208 case Error_BookmarkHasBeenLost
:
209 errortxt
= "Your bookmark has unfortunately been lost.";
214 MUI_Request(app
, window
, 0L, NULL
, "Continue", errortxt
);
219 return DoSuperMethodA(cl
, obj
, (Msg
)msg
);
222 static Object
*ImageGad(const char *text
, UBYTE key
)
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
,
233 MUIA_InputMode
, MUIV_InputMode_Toggle
,
234 MUIA_ControlChar
, key
,
235 MUIA_CycleChain
, TRUE
,
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
)
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
);
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
);
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
);
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
,
312 MUIA_Background
, MUII_GroupBack
,
313 MUIA_Frame
, MUIV_Frame_Group
,
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..."),
331 Child
, ischanged
= MUI_MakeObject(MUIO_Checkmark
, "Is changed?"),
333 MUIA_Text_Contents
, "Is changed?",
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'),
348 MUIA_Group_Spacing
, 0,
349 Child
, editorgad
= NewObject(editor_mcc
->mcc_Class
, NULL
,
350 MUIA_CycleChain
, TRUE
,
352 Child
, slider
= ScrollbarObject
,
357 Child
, ListviewObject
,
358 MUIA_Listview_DragType
, MUIV_Listview_DragType_Immediate
,
359 MUIA_Listview_List
, ListObject
,
361 MUIA_Background
, MUII_ListBack
,
362 MUIA_List_AdjustWidth
, TRUE
,
363 MUIA_List_AdjustHeight
, TRUE
,
364 MUIA_List_SourceArray
, StdEntries
,
367 Child
, RectangleObject
,
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
;
393 size
= Read(fh
, text
, (300*1024)-2);
399 while(*buffer
!= '\n' && buffer
< &text
[size
])
401 while(*buffer
++ != '\n');
408 MUIA_TextEditor_ImportHook
, MUIV_TextEditor_ImportHook_MIMEQuoted
,
409 MUIA_TextEditor_ImportWrap
, 80,
410 MUIA_TextEditor_ExportHook
, MUIV_TextEditor_ExportHook_EMail
,
418 MUIA_TextEditor_ImportHook
, MUIV_TextEditor_ImportHook_MIME
,
419 MUIA_TextEditor_ExportHook
, MUIV_TextEditor_ExportHook_EMail
,
427 MUIA_TextEditor_ImportHook
, MUIV_TextEditor_ImportHook_EMail
,
428 MUIA_TextEditor_ExportHook
, MUIV_TextEditor_ExportHook_EMail
,
434 set(editorgad
, MUIA_TextEditor_Contents
, buffer
);
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
,
484 struct MsgPort
*myport
= NULL
;
485 struct RexxMsg
*rxmsg
= NULL
;
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()))
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
);
513 sigs
= Wait(sigs
| SIGBREAKF_CTRL_C
| (myport
? 1<<myport
->mp_SigBit
: 0));
514 if(myport
&& (sigs
& 1<<myport
->mp_SigBit
))
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
);
526 if(sigs
& SIGBREAKF_CTRL_C
)
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]);
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
));
551 MUI_DisposeObject(app
);
554 printf("Failed to create application\n");
556 MUI_DeleteCustomClass(editor_mcc
);
559 printf("Failed to open TextEditor.mcc\n");
564 printf("Failed to open MUIMaster.Library V%d\n", MUIMASTER_VMIN
);
571 LONG error
= IoErr();
573 GetProgramName(prgname
, 32);
574 PrintFault(error
, prgname
);