summaryrefslogtreecommitdiff
path: root/lib/internal.h
blob: 66eabbc2ba88c88dd342952c143344f1dd2425f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "bemenu.h"

#ifndef size_t
#   include <stddef.h> /* for size_t */
#endif

/**
 * Internal bmItem struct that is not exposed to public.
 * Represents a single item in menu.
 */
struct _bmItem {
    /**
     * Primary text shown on item as null terminated C "string".
     * Matching will be done against this text as well.
     */
    char *text;
};

/**
 * Internal bmRenderApi struct.
 * Renderers should be able to fill this one as they see fit.
 */
struct _bmRenderApi {
    /**
     * If the underlying renderer is a UI toolkit. (curses, etc...)
     * There might be possibility to get user input, and this should be thus implemented.
     */
    bmKey (*getKey)(unsigned int *unicode);

    /**
     * Tells underlying renderer to draw the menu.
     */
    void (*render)(const bmMenu *menu);

    /**
     * Release underlying renderer.
     */
    void (*free)(void);
};

/**
 * Internal bmMenu struct that is not exposed to public.
 */
struct _bmMenu {
    /**
     * Underlying renderer access.
     */
    struct _bmRenderApi renderApi;

    /**
     * All items contained in menu instance.
     */
    struct _bmItem **items;

    /**
     * Filtered/displayed items contained in menu instance.
     */
    struct _bmItem **filteredItems;

    /**
     * Menu instance title.
     */
    char *title;

    /**
     * Text used to filter matches.
     * XXX: Change this to a pointer?
     */
    char filter[1024];

    /**
     * Current byte offset on filter text.
     */
    unsigned int cursor;

    /**
     * Current column/cursor position on filter text.
     */
    unsigned int cursesCursor;

    /**
     * Number of items in menu instance.
     */
    unsigned int itemsCount;

    /**
     * Number of filtered items in menu instance.
     */
    unsigned int filteredCount;

    /**
     * Number of allocated items in menu instance.
     */
    unsigned int allocatedCount;

    /**
     * Current filtered item index in menu instance.
     * This index is valid for the list returned by bmMenuGetFilteredItems.
     */
    unsigned int index;

    /**
     * Current filtering method in menu instance.
     */
    bmFilterMode filterMode;

    /**
     * Drawing mode used in menu instance.
     */
    bmDrawMode drawMode;
};

/* draw/curses.c */
int _bmDrawCursesInit(struct _bmRenderApi *api);

/* filter.c */
bmItem** _bmFilterDmenu(bmMenu *menu, unsigned int *outNmemb, unsigned int *outSelected);
bmItem** _bmFilterDmenuCaseInsensitive(bmMenu *menu, unsigned int *outNmemb, unsigned int *outSelected);

/* util.c */
char* _bmStrdup(const char *s);
bmItem** _bmShrinkItemList(bmItem ***inOutList, size_t osize, size_t nsize);
int _bmUtf8StringScreenWidth(const char *string);
size_t _bmUtf8RuneNext(const char *string, size_t start);
size_t _bmUtf8RunePrev(const char *string, size_t start);
size_t _bmUtf8RuneWidth(const char *rune, unsigned int u8len);
size_t _bmUtf8RuneRemove(char *string, size_t start, size_t *outRuneWidth);
size_t _bmUtf8RuneInsert(char *string, size_t bufSize, size_t start, const char *rune, unsigned int u8len, size_t *outRuneWidth);
size_t _bmUnicodeInsert(char *string, size_t bufSize, size_t start, unsigned int unicode, size_t *outRuneWidth);

/* vim: set ts=8 sw=4 tw=0 :*/