summaryrefslogtreecommitdiff
path: root/ldmicro/helpdialog.cpp
blob: 71cbd1a9990c70a7f4f88f485d305d49cc0dd26e (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//-----------------------------------------------------------------------------
// Copyright 2007 Jonathan Westhues
//
// This file is part of LDmicro.
// 
// LDmicro is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// LDmicro is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with LDmicro.  If not, see <http://www.gnu.org/licenses/>.
//------
//
// Window to show our internal help. Roll my own using a rich edit control so
// that I don't have to distribute a separate Windows Help or HTML file; the
// manual is compiled in to the exe. Do pretty syntax highlighting style
// colours.
// Jonathan Westhues, Dec 2004
//-----------------------------------------------------------------------------
#include "linuxUI.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
//#include <commctrl.h>
//#include <richedit.h>

#include "ldmicro.h"

using namespace std;

extern char *HelpText[];
extern char *HelpTextDe[];
extern char *HelpTextFr[];
extern char *HelpTextTr[];

static char *AboutText[] = {
"",
"ABOUT LDMICRO",
"=============",
"",
"LDmicro is a ladder logic editor, simulator and compiler for 8-bit",
"microcontrollers. It can generate native code for Atmel AVR and Microchip",
"PIC16 CPUs from a ladder diagram.",
"",
"This program is free software: you can redistribute it and/or modify it",
"under the terms of the GNU General Public License as published by the",
"Free Software Foundation, either version 3 of the License, or (at your",
"option) any later version.",
"",
"This program is distributed in the hope that it will be useful, but",
"WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY",
"or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License",
"for more details.",
"",
"You should have received a copy of the GNU General Public License along",
"with this program. If not, see <http://www.gnu.org/licenses/>.",
"",
"The source code for LDmicro is available at",
"",
"    http://cq.cx/ladder.pl",
"",
"Copyright 2005-2010 Jonathan Westhues",
"Release 2.2, built " __TIME__ " " __DATE__ ".",
"",
"Email: user jwesthues, at host cq.cx",
"",
NULL
};

static char **Text[] = {
#if defined(LDLANG_EN) || \
    defined(LDLANG_ES) || \
    defined(LDLANG_IT) || \
    defined(LDLANG_PT)
    HelpText,
#elif defined(LDLANG_DE)
    HelpTextDe,
#elif defined(LDLANG_FR)
    HelpTextFr,
#elif defined(LDLANG_TR)
    HelpTextTr,
#else
#   error "Bad language"
#endif

    // Let's always keep the about text in English.
    AboutText
};

static HWID HelpDialog[2];
static HWID RichEdit[2];

static BOOL HelpWindowOpen[2];

static int TitleHeight;

HWID PackBoxHelp;
HWID TextView;
GtkTextBuffer* TextBuffer;
GtkTextIter* TextIter = new GtkTextIter;

#define RICH_EDIT_HEIGHT(h)  \
    ((((h) - 3 + (FONT_HEIGHT/2)) / FONT_HEIGHT) * FONT_HEIGHT)

static void SizeRichEdit(int a)
{
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (RichEdit[a]),
				                          GTK_POLICY_AUTOMATIC, 
				                          GTK_POLICY_ALWAYS);
    gtk_widget_set_hexpand(GTK_WIDGET(RichEdit[a]), TRUE);  
    gtk_widget_set_vexpand(GTK_WIDGET(RichEdit[a]), TRUE);
}

// static BOOL Resizing(RECT *r, int wParam)
// {
//     BOOL touched = FALSE;
//     if(r->right - r->left < 650) {
//         int diff = 650 - (r->right - r->left);
//         if(wParam == WMSZ_RIGHT || wParam == WMSZ_TOPRIGHT ||
//             wParam == WMSZ_BOTTOMRIGHT)
//         {
//             r->right += diff;
//         } else {
//             r->left -= diff;
//         }
//         touched = TRUE;
//     }

//     if(!(wParam == WMSZ_LEFT || wParam == WMSZ_RIGHT)) {
//         int h = r->bottom - r->top - TitleHeight - 5;
//         if(RICH_EDIT_HEIGHT(h) != h) {
//             int diff = h - RICH_EDIT_HEIGHT(h);
//             if(wParam == WMSZ_TOP || wParam == WMSZ_TOPRIGHT ||
//                 wParam == WMSZ_TOPLEFT)
//             {
//                 r->top += diff;
//             } else {
//                 r->bottom -= diff;
//             }
//             touched = TRUE;
//         }
//     }

//     return !touched;
// }

static void MakeControls(int a)
{
    // HMODULE re = LoadLibrary("RichEd20.dll");
    // if(!re) oops();

    RichEdit[a] = gtk_scrolled_window_new (NULL, NULL);
    TextView = gtk_text_view_new ();
    TextBuffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (TextView));
    gtk_text_view_set_editable (GTK_TEXT_VIEW (TextView), FALSE);
    SizeRichEdit(a);
    gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (TextView), GTK_WRAP_WORD);
    gtk_text_buffer_get_start_iter (TextBuffer, TextIter);
    // COLORREF color;
    // gtk_text_buffer_create_tag (TextBuffer, "ForegroundColor1",
    //                         "foreground", "blue");

    int i;
    BOOL nextSubHead = FALSE;
    for(i = 0; Text[a][i]; i++) {
        char *s = Text[a][i];
        gtk_text_buffer_get_iter_at_offset (TextBuffer, TextIter, -1);

        if((s[0] == '=') ||
           (Text[a][i+1] && Text[a][i+1][0] == '='))
        {
            COLORREF color = RGB(255, 255, 110);
            // gtk_widget_override_color (TextView, GTK_STATE_FLAG_NORMAL, &color);
            
        }
        else if(s[3] == '|' && s[4] == '|') {
            // COLORREF color = RGB(255, 110, 255);
            // gtk_widget_override_color (TextView, GTK_STATE_FLAG_NORMAL, &color);
        }
        else if(s[0] == '>' || nextSubHead) {
            // Need to make a copy because the strings we are passed aren't
            // mutable.
            char copy[1024];
            if(strlen(s) >= sizeof(copy)) oops();
            strcpy(copy, s);

            int j;
            for(j = 1; copy[j]; j++) {
                if(copy[j] == ' ' && copy[j-1] == ' ')
                    break;
            }
            BOOL justHeading = (copy[j] == '\0');
            copy[j] = '\0';
            COLORREF color = RGB(110, 255, 110);
            // gtk_widget_override_color (TextView, GTK_STATE_FLAG_NORMAL, &color);
            gtk_text_buffer_insert (TextBuffer, TextIter, copy, -1);
            gtk_text_buffer_get_iter_at_offset (TextBuffer, TextIter, -1);
            
            // Special case if there's nothing except title on the line
            if(!justHeading) {
                copy[j] = ' ';
            }
            s += j;
            // color = RGB(255, 110, 255);
            // gtk_widget_override_color (TextView, GTK_STATE_FLAG_NORMAL, &color);
            nextSubHead = !nextSubHead;
        }
        else {
            COLORREF color = RGB(255, 255, 255);
            gtk_widget_override_color (TextView, GTK_STATE_FLAG_NORMAL, &color);
        }

    // gtk_text_buffer_insert_with_tags_by_name (TextBuffer, TextIter,
    //                             s, -1, "ForegroundColor1", NULL);
    gtk_text_buffer_insert (TextBuffer, TextIter, s, -1);

        if(Text[a][i+1]) {
            gtk_text_buffer_insert (TextBuffer, TextIter, "\n", -1);
        }
    }
    gtk_widget_override_background_color (TextView, GTK_STATE_FLAG_NORMAL,
                        ((HBRUSH)GetStockObject(BLACK_BRUSH)));
    gtk_container_add (GTK_CONTAINER(RichEdit[a]), TextView);

}

//-----------------------------------------------------------------------------
// Window proc for the help dialog.
//-----------------------------------------------------------------------------
// static LRESULT CALLBACK HelpProc(HWND hwnd, UINT msg, WPARAM wParam,
//     LPARAM lParam)
// {
//     int a = (hwnd == HelpDialog[0] ? 0 : 1);
//     switch (msg) {
//         case WM_SIZING: {
//             RECT *r = (RECT *)lParam;
//             return Resizing(r, wParam);
//             break;
//         }
//         case WM_SIZE:
//             SizeRichEdit(a);
//             break;

//         case WM_ACTIVATE:
//         case WM_KEYDOWN:
//             SetFocus(RichEdit[a]);
//             break;
    
//         case WM_DESTROY:
//         case WM_CLOSE:
//             HelpWindowOpen[a] = FALSE;
//             // fall through
//         default:
//             return DefWindowProc(hwnd, msg, wParam, lParam);
//     }

//     return 1;
// }

//-----------------------------------------------------------------------------
// Create the class for the help window.
//-----------------------------------------------------------------------------
static void MakeClass(void)
{
//     WNDCLASSEX wc;
//     memset(&wc, 0, sizeof(wc));
//     wc.cbSize = sizeof(wc);

//     wc.style            = CS_BYTEALIGNCLIENT | CS_BYTEALIGNWINDOW | CS_OWNDC |
//                           CS_DBLCLKS;
//     wc.lpfnWndProc      = (WNDPROC)HelpProc;
//     wc.hInstance        = Instance;
//     wc.lpszClassName    = "LDmicroHelp";
//     wc.lpszMenuName     = NULL;
//     wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
//     wc.hIcon            = (HICON)LoadImage(Instance, MAKEINTRESOURCE(4000),
//                             IMAGE_ICON, 32, 32, 0);
//     wc.hIconSm          = (HICON)LoadImage(Instance, MAKEINTRESOURCE(4000),
//                             IMAGE_ICON, 16, 16, 0);


//     RegisterClassEx(&wc);
}

void ShowHelpDialog(BOOL about)
{
    int a = about ? 1 : 0;
    
    MakeClass();

    const char *s = about ? "About LDmicro" : "LDmicro Help";

    MakeControls(a);

    PackBoxHelp = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
    gtk_box_pack_start(GTK_BOX(PackBoxHelp), RichEdit[a], FALSE, TRUE, 0);

    HelpDialog[a] = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(HelpDialog[a]), 650, 300+10*FONT_HEIGHT);
    gtk_window_set_title(GTK_WINDOW(HelpDialog[a]), s);
    gtk_container_add(GTK_CONTAINER(HelpDialog[a]), PackBoxHelp);
   
    gtk_widget_show_all (HelpDialog[a]);
    gtk_widget_grab_focus (RichEdit[a]);

    if(HelpWindowOpen[a]) {
        gtk_widget_grab_focus (HelpDialog[a]);
        return;
    }
    HelpWindowOpen[a] = TRUE;
}