LDFLAGS += -laxwin3
-OBJ = main.o strings.o
+OBJ = main.o strings.o toolbar.o
BIN = ate
DIR := Apps/AxWin/3.0
#include <stdio.h>
#include "include/file.h"
#include "include/syntax.h"
+#include <assert.h>
+
+static const int MAXLINE = 1024;
+static const int LINEALLOCSIZE = 64;
// === CODE ===
tFile *File_New(void)
{
- tFile *ret = malloc(sizeof(tFile) + 1);
- ret->Handle = NULL;
+ tFile *ret = calloc(1, sizeof(tFile) + 1);
ret->nLines = 0;
- ret->Lines = NULL;
+ ret->FirstLine = NULL;
ret->NameOfs = 0;
ret->Path[0] = 0;
return ret;
tFile *File_Load(const char *Path)
{
- return NULL;
+ FILE *fp = fopen(Path, "r");
+ if( !fp ) {
+ return NULL;
+ }
+
+ // Create file structure
+ tFile *ret = calloc(1, sizeof(tFile) + strlen(Path) + 1);
+ assert(ret);
+
+ const char *lastslash = strrchr(Path, '/');
+ ret->NameOfs = (lastslash ? lastslash - Path + 1 : 0);
+ strcpy(ret->Path, Path);
+
+ // Read in lines
+ int nLines = 0;
+ char tmpbuf[MAXLINE];
+ tFileLine *lastline = NULL;
+ while( fgets(tmpbuf, MAXLINE, fp) )
+ {
+ tFileLine *new = malloc(sizeof(tFileLine));
+ assert(new);
+ new->Prev = lastline;
+ new->Next = NULL;
+ if(lastline) lastline->Next = new;
+ new->Length = strlen(tmpbuf);
+ new->Space = (new->Length + LINEALLOCSIZE-1) & ~(LINEALLOCSIZE-1);
+
+ new->Data = malloc(new->Space);
+ assert(new->Data);
+ memcpy(new->Data, tmpbuf, new->Length);
+ nLines ++;
+ }
+ ret->nLines = nLines;
+
+ return ret;
}
int File_Save(tFile *File)
int File_Close(tFile *File, int bDiscard)
{
- //if( file->bIsDirty && !bDiscard )
- // return 1;
+ if( file->bIsDirty && !bDiscard )
+ return 1;
if( file->Handle )
fclose(File->Handle);
+
+ while( file->FirstLine )
+ {
+ tFileLine *next = file->FirstLine->Next;
+ // TODO: Highlighting free
+ free(file->FirstLine->Data);
+ free(file->FirstLine);
+ file->FirstLine = next;
+ }
+
+ free(file);
return 0;
}
+int File_InsertBytes(tFile *File, void *Buffer, size_t Bytes)
+{
+
+}
+
+int File_Delete(tFile *File, enum eFile_DeleteType Type)
+{
+}
+
+/**
+ * Amt = INT_MAX : End of file
+ * Amt = INT_MIN : Start of file
+ */
+int File_CursorDown(tFile *File, int Amount)
+{
+}
+
+/**
+ * |Amt| = 1 : Single character
+ * |Amt| = 2 : Word
+ * |Amt| = 3 : Start/End of line
+ */
+int File_CursorRight(tFile *File, int Amount)
+{
+}
+
+void *File_GetAbsLine(tFile *File, unsigned int LineNum)
+{
+ tFileLine *line = File->FirstLine;
+ while( LineNum-- && line )
+ line = line->Next;
+ return line;
+}
+
+void *File_GetRelLine(tFile *File, unsigned int LinesBeforeCurrent)
+{
+ tFileLine *line = File->CurrentLine;
+ while(LinesBeforeCurrent -- && line->Prev)
+ line = line->Prev;
+ return line;
+}
+
+void *File_GetRenderedData(tFile *File, void *Handle, size_t const char **StringPtr)
+{
+ if( !Handle )
+ return NULL;
+ tFileLine *line = Handle;
+ if( StringPtr )
+ *StringPtr = (line->Rendered ? line->Rendered : line->Data);
+ return line->Next;
+}
+
--- /dev/null
+/*
+ * Acess Text Editor (ATE)
+ * - By John Hodge (thePowersGang)
+ *
+ * common.h
+ * - Core header
+ */
+#ifndef _COMMON_H_
+#define _COMMON_H_
+
+extern void Toolbar_Init(tAxWin3_Widget *Parent);
+
+#endif
+
#define _ATE__FILE_H_
#include <stdio.h>
-typedef struct sFileLine
+typedef struct sFileLine tFileLine;
+
+struct sFileLine
{
+ tFileLine *Next;
+ tFileLine *Prev;
int Num;
// State data for hilighting
int Space;
int Length;
- char Data[];
-} tFileLine;
+ char *Data;
+};
typedef struct sFile
{
- FILE *Handle;
int nLines;
- tFileLine **Lines; // TODO: Handle very large files?
+ tFileLine *FirstLine;
+ tFileLine *CurrentLine;
+ int CursorOfs;
+
int NameOfs;
const char Path[];
} tFile;
+enum eFile_DeleteType
+{
+ DELTYPE_BACK,
+ DELTYPE_BACK_WORD,
+ DELTYPE_FORWARD,
+ DELTYPE_FORWARD_WORD,
+ DELTYPE_TO_EOL,
+ DELTYPE_TO_SOL,
+ DELTYPE_LINE
+};
+
#endif
#include <axwin3/menu.h>
#include <axwin3/richtext.h>
#include <stdio.h>
+#include "include/common.h"
#include "strings.h"
// === PROTOTYPES ===
int main(int argc, char *argv[]);
int TextArea_KeyHandler(tHWND Window, int bPress, uint32_t KeySym, uint32_t Translated);
int TextArea_MouseHandler(tHWND Window, int bPress, int Button, int Row, int Col);
-void add_toolbar_button(tAxWin3_Widget *Toolbar, const char *Ident, tAxWin3_Widget_FireCb Callback);
- int Toolbar_New(tAxWin3_Widget *Widget);
- int Toolbar_Open(tAxWin3_Widget *Widget);
- int Toolbar_Save(tAxWin3_Widget *Widget);
- int Toolbar_Close(tAxWin3_Widget *Widget);
// === GLOBALS ===
tHWND gMainWindow;
// Create toolbar
gMainWindow_Toolbar = AxWin3_Widget_AddWidget(gMainWindow_Root,
ELETYPE_TOOLBAR, ELEFLAG_NOSTRETCH, "Toolbar");
- add_toolbar_button(gMainWindow_Toolbar, "BtnNew", Toolbar_New);
- add_toolbar_button(gMainWindow_Toolbar, "BtnOpen", Toolbar_Open);
- add_toolbar_button(gMainWindow_Toolbar, "BtnSave", Toolbar_Save);
- add_toolbar_button(gMainWindow_Toolbar, "BtnClose", Toolbar_Close);
- AxWin3_Widget_AddWidget(gMainWindow_Toolbar, ELETYPE_SPACER, ELEFLAG_NOSTRETCH, "");
- add_toolbar_button(gMainWindow_Toolbar, "BtnUndo", NULL);
- add_toolbar_button(gMainWindow_Toolbar, "BtnRedo", NULL);
- AxWin3_Widget_AddWidget(gMainWindow_Toolbar, ELETYPE_SPACER, ELEFLAG_NOSTRETCH, "");
- add_toolbar_button(gMainWindow_Toolbar, "BtnCut", NULL);
- add_toolbar_button(gMainWindow_Toolbar, "BtnCopy", NULL);
- add_toolbar_button(gMainWindow_Toolbar, "BtnPaste", NULL);
- AxWin3_Widget_AddWidget(gMainWindow_Toolbar, ELETYPE_SPACER, ELEFLAG_NOSTRETCH, "");
- add_toolbar_button(gMainWindow_Toolbar, "BtnSearch", NULL);
- add_toolbar_button(gMainWindow_Toolbar, "BtnReplace", NULL);
+ Toolbar_Init(gMainWindow_Toolbar);
// TODO: Tab control?
gMainWindow_TextArea = AxWin3_RichText_CreateWindow(gMainWindow, 0);
- AxWin3_Widget_AddWidget_SubWindow(gMainWindow_Root, gMainWindow_TextArea, 0, "TextArea");
AxWin3_RichText_SetKeyHandler (gMainWindow_TextArea, TextArea_KeyHandler);
AxWin3_RichText_SetMouseHandler (gMainWindow_TextArea, TextArea_MouseHandler);
AxWin3_RichText_SetBackground (gMainWindow_TextArea, 0xFFFFFF);
// <testing>
AxWin3_RichText_SetLineCount(gMainWindow_TextArea, 3);
AxWin3_RichText_SendLine(gMainWindow_TextArea, 0, "First line!");
- AxWin3_RichText_SendLine(gMainWindow_TextArea, 2, "Third line! \x01""ff0000red\x01""00ff00green");
+ AxWin3_RichText_SendLine(gMainWindow_TextArea, 2, "Third line! \001ff0000red\00100ff00green");
// </testing>
+ AxWin3_Widget_AddWidget_SubWindow(gMainWindow_Root, gMainWindow_TextArea, 0, "TextArea");
AxWin3_ShowWindow(gMainWindow_TextArea, 1);
// TODO: Status Bar?
AxWin3_MoveWindow(gMainWindow, 50, 50);
AxWin3_ShowWindow(gMainWindow, 1);
AxWin3_FocusWindow(gMainWindow);
+ AxWin3_FocusWindow(gMainWindow_TextArea);
// Main loop
AxWin3_MainLoop();
return 0;
}
-void add_toolbar_button(tAxWin3_Widget *Toolbar, const char *Ident, tAxWin3_Widget_FireCb Callback)
-{
- tAxWin3_Widget *btn = AxWin3_Widget_AddWidget(Toolbar, ELETYPE_BUTTON, ELEFLAG_NOSTRETCH, Ident);
- const char *img = getimg(Ident);
- if( img )
- {
- tAxWin3_Widget *txt = AxWin3_Widget_AddWidget(btn, ELETYPE_IMAGE, 0, Ident);
- AxWin3_Widget_SetText(txt, img);
- // TODO: tooltip?
- }
- else
- {
- tAxWin3_Widget *txt = AxWin3_Widget_AddWidget(btn, ELETYPE_TEXT, 0, Ident);
- AxWin3_Widget_SetText(txt, getstr(Ident));
- }
- AxWin3_Widget_SetFireHandler(btn, Callback);
-}
-
-int Toolbar_New(tAxWin3_Widget *Widget)
-{
- return 0;
-}
-int Toolbar_Open(tAxWin3_Widget *Widget)
-{
- return 0;
-}
-int Toolbar_Save(tAxWin3_Widget *Widget)
-{
- return 0;
-}
-int Toolbar_Close(tAxWin3_Widget *Widget)
-{
- AxWin3_StopMainLoop(1);
- return 0;
-}
-
--- /dev/null
+/*
+ * Acess Text Editor (ATE)
+ * - By John Hodge (thePowersGang)
+ *
+ * toolbar.c
+ * - Main toolbar code
+ */
+#include <axwin3/axwin.h>
+#include <axwin3/widget.h>
+#include "include/common.h"
+#include "strings.h"
+
+// === PROTOTYPES ===
+void add_toolbar_button(tAxWin3_Widget *Toolbar, const char *Ident, tAxWin3_Widget_FireCb Callback);
+ int Toolbar_Cb_New(tAxWin3_Widget *Widget);
+ int Toolbar_Cb_Open(tAxWin3_Widget *Widget);
+ int Toolbar_Cb_Save(tAxWin3_Widget *Widget);
+ int Toolbar_Cb_Close(tAxWin3_Widget *Widget);
+
+// === CODE ===
+void Toolbar_Init(tAxWin3_Widget *Toolbar)
+{
+ add_toolbar_button(Toolbar, "BtnNew", Toolbar_Cb_New);
+ add_toolbar_button(Toolbar, "BtnOpen", Toolbar_Cb_Open);
+ add_toolbar_button(Toolbar, "BtnSave", Toolbar_Cb_Save);
+ add_toolbar_button(Toolbar, "BtnClose", Toolbar_Cb_Close);
+ AxWin3_Widget_AddWidget(Toolbar, ELETYPE_SPACER, ELEFLAG_NOSTRETCH, "");
+ add_toolbar_button(Toolbar, "BtnUndo", NULL);
+ add_toolbar_button(Toolbar, "BtnRedo", NULL);
+ AxWin3_Widget_AddWidget(Toolbar, ELETYPE_SPACER, ELEFLAG_NOSTRETCH, "");
+ add_toolbar_button(Toolbar, "BtnCut", NULL);
+ add_toolbar_button(Toolbar, "BtnCopy", NULL);
+ add_toolbar_button(Toolbar, "BtnPaste", NULL);
+ AxWin3_Widget_AddWidget(Toolbar, ELETYPE_SPACER, ELEFLAG_NOSTRETCH, "");
+ add_toolbar_button(Toolbar, "BtnSearch", NULL);
+ add_toolbar_button(Toolbar, "BtnReplace", NULL);
+}
+
+void add_toolbar_button(tAxWin3_Widget *Toolbar, const char *Ident, tAxWin3_Widget_FireCb Callback)
+{
+ tAxWin3_Widget *btn = AxWin3_Widget_AddWidget(Toolbar, ELETYPE_BUTTON, ELEFLAG_NOSTRETCH, Ident);
+ const char *img = getimg(Ident);
+ if( img )
+ {
+ tAxWin3_Widget *txt = AxWin3_Widget_AddWidget(btn, ELETYPE_IMAGE, 0, Ident);
+ AxWin3_Widget_SetText(txt, img);
+ // TODO: tooltip?
+ }
+ else
+ {
+ tAxWin3_Widget *txt = AxWin3_Widget_AddWidget(btn, ELETYPE_TEXT, 0, Ident);
+ AxWin3_Widget_SetText(txt, getstr(Ident));
+ }
+ AxWin3_Widget_SetFireHandler(btn, Callback);
+}
+
+int Toolbar_Cb_New(tAxWin3_Widget *Widget)
+{
+ return 0;
+}
+int Toolbar_Cb_Open(tAxWin3_Widget *Widget)
+{
+ return 0;
+}
+int Toolbar_Cb_Save(tAxWin3_Widget *Widget)
+{
+ return 0;
+}
+int Toolbar_Cb_Close(tAxWin3_Widget *Widget)
+{
+ AxWin3_StopMainLoop(1);
+ return 0;
+}
+