source: git/src/cavernlog.cc @ e3f7ea7

stereo-2025
Last change on this file since e3f7ea7 was 7962c9d, checked in by Olly Betts <olly@…>, 4 months ago

Renumber messages to align with IMG_* error codes

  • Property mode set to 100644
File size: 18.8 KB
RevLine 
[6bec10c]1/* cavernlog.cc
2 * Run cavern inside an Aven window
3 *
[ac20829b]4 * Copyright (C) 2005-2024 Olly Betts
[6bec10c]5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19 */
20
[4c83f84]21#include <config.h>
[6bec10c]22
23#include "aven.h"
24#include "cavernlog.h"
25#include "filename.h"
[fb5887c]26#include "mainfrm.h"
[6bec10c]27#include "message.h"
[4b40a9c]28#include "osalloc.h"
[6bec10c]29
[7af6fff]30#include <algorithm>
31
[baeae66]32#include <errno.h>
[6bec10c]33#include <stdio.h>
[a90632c]34#include <stdlib.h>
[6bec10c]35
36#include <sys/time.h>
37#include <sys/types.h>
38#include <unistd.h>
39
[15ba0b5]40#include <wx/process.h>
41
[cc2d7ad]42#define GVIM_COMMAND "gvim +'call cursor($l,$c)' $f"
43#define VIM_COMMAND "x-terminal-emulator -e vim +'call cursor($l,$c)' $f"
[81d94a4]44#define NVIM_COMMAND "x-terminal-emulator -e nvim +'call cursor($l,$c)' $f"
[cc2d7ad]45#define GEDIT_COMMAND "gedit $f +$l:$c"
46// Pluma currently ignores the column, but include it assuming some future
47// version will add support.
48#define PLUMA_COMMAND "pluma +$l:$c $f"
49#define EMACS_COMMAND "x-terminal-emulator -e emacs +$l:$c $f"
50#define NANO_COMMAND "x-terminal-emulator -e nano +$l,$c $f"
51#define JED_COMMAND "x-terminal-emulator -e jed $f -g $l"
52#define KATE_COMMAND "kate -l $l -c $c $f"
53
54#ifdef __WXMSW__
55# define DEFAULT_EDITOR_COMMAND "notepad $f"
56#elif defined __WXMAC__
57# define DEFAULT_EDITOR_COMMAND "open -t $f"
58#else
59# define DEFAULT_EDITOR_COMMAND VIM_COMMAND
60#endif
61
[0e81a88]62enum { LOG_REPROCESS = 1234, LOG_SAVE = 1235 };
[fb5887c]63
[e768f29]64static const wxString badutf8_html(
65    wxT("<span style=\"color:white;background-color:red;\">&#xfffd;</span>"));
66static const wxString badutf8(wxUniChar(0xfffd));
67
[ac20829b]68// New event type for signalling cavern output to process.
69wxDEFINE_EVENT(EVT_CAVERN_OUTPUT, wxCommandEvent);
[fc626ae]70
[ac20829b]71void
72CavernLogWindow::CheckForOutput(bool immediate)
73{
74    timer.Stop();
75    if (cavern_out == NULL) return;
[fc626ae]76
[ac20829b]77    wxInputStream * in = cavern_out->GetInputStream();
[fc626ae]78
[ac20829b]79    if (!in->CanRead()) {
80        timer.StartOnce();
81        return;
[fc626ae]82    }
83
[ac20829b]84    size_t real_size = log_txt.size();
85    size_t allow = 1024;
86    log_txt.resize(real_size + allow);
87    in->Read(&log_txt[real_size], allow);
88    size_t n = in->LastRead();
89    log_txt.resize(real_size + n);
90    if (n) {
91        if (immediate) {
92            ProcessCavernOutput();
93        } else {
94            QueueEvent(new wxCommandEvent(EVT_CAVERN_OUTPUT));
95        }
[fc626ae]96    }
[ac20829b]97}
[fc626ae]98
[ac20829b]99int
100CavernLogWindow::OnPaintButton(wxButton* b, int x)
[fc626ae]101{
[ac20829b]102    if (b) {
103        x -= 4;
104        const wxSize& bsize = b->GetSize();
105        x -= bsize.x;
106        b->SetSize(x, 4, bsize.x, bsize.y);
107        x -= 4;
[15ba0b5]108    }
[ac20829b]109    return x;
[15ba0b5]110}
111
112void
[ac20829b]113CavernLogWindow::OnPaint(wxPaintEvent&)
[15ba0b5]114{
[ac20829b]115    wxPaintDC dc(this);
116    wxFont font = dc.GetFont();
117    wxFont bold_font = font.Bold();
118    wxFont underlined_font = font.Underlined();
119    const wxRegion& region = GetUpdateRegion();
120    const wxRect& rect = region.GetBox();
121    int scroll_x = 0, scroll_y = 0;
122    GetViewStart(&scroll_x, &scroll_y);
123    int fsize = dc.GetFont().GetPixelSize().GetHeight();
124    int limit = min((rect.y + rect.height + fsize - 1) / fsize + scroll_y, int(line_info.size()) - 1);
125    for (int i = max(rect.y / fsize, scroll_y); i <= limit ; ++i) {
126        LineInfo& info = line_info[i];
127        // Leave a small margin to the left.
128        int x = fsize / 2 - scroll_x * fsize;
129        int y = (i - scroll_y) * fsize;
130        unsigned offset = info.start_offset;
131        unsigned len = info.len;
132        if (info.link_len) {
133            dc.SetFont(underlined_font);
134            dc.SetTextForeground(wxColour(192, 0, 192));
135            wxString link = wxString::FromUTF8(&log_txt[offset], info.link_len);
136            offset += info.link_len;
137            len -= info.link_len;
138            dc.DrawText(link, x, y);
139            x += info.link_pixel_width;
140            dc.SetFont(font);
[fc626ae]141        }
[ac20829b]142        if (info.colour_len) {
143            dc.SetTextForeground(*wxBLACK);
144            {
145                size_t s_len = info.start_offset + info.colour_start - offset;
146                wxString s = wxString::FromUTF8(&log_txt[offset], s_len);
147                offset += s_len;
148                len -= s_len;
149                dc.DrawText(s, x, y);
150                x += dc.GetTextExtent(s).GetWidth();
151            }
152            switch (info.colour) {
153                case LOG_ERROR:
154                    dc.SetTextForeground(*wxRED);
155                    break;
156                case LOG_WARNING:
157                    dc.SetTextForeground(wxColour(0xf2, 0x8C, 0x28));
158                    break;
159                case LOG_INFO:
160                    dc.SetTextForeground(*wxBLUE);
161                    break;
162            }
163            dc.SetFont(bold_font);
164            wxString d = wxString::FromUTF8(&log_txt[offset], info.colour_len);
165            offset += info.colour_len;
166            len -= info.colour_len;
167            dc.DrawText(d, x, y);
168            x += dc.GetTextExtent(d).GetWidth();
169            dc.SetFont(font);
170        }
171        dc.SetTextForeground(*wxBLACK);
172        dc.DrawText(wxString::FromUTF8(&log_txt[offset], len), x, y);
[15ba0b5]173    }
[ac20829b]174    int x = GetClientSize().x;
175    x = OnPaintButton(ok_button, x);
176    x = OnPaintButton(reprocess_button, x);
177    OnPaintButton(save_button, x);
[fc626ae]178}
179
[ac20829b]180BEGIN_EVENT_TABLE(CavernLogWindow, wxScrolledWindow)
[81e1aa4]181    EVT_BUTTON(LOG_REPROCESS, CavernLogWindow::OnReprocess)
[0e81a88]182    EVT_BUTTON(LOG_SAVE, CavernLogWindow::OnSave)
[fb5887c]183    EVT_BUTTON(wxID_OK, CavernLogWindow::OnOK)
[ac20829b]184    EVT_COMMAND(wxID_ANY, EVT_CAVERN_OUTPUT, CavernLogWindow::OnCavernOutput)
[8991d7f]185    EVT_IDLE(CavernLogWindow::OnIdle)
[ac20829b]186    EVT_TIMER(wxID_ANY, CavernLogWindow::OnTimer)
187    EVT_PAINT(CavernLogWindow::OnPaint)
188    EVT_MOTION(CavernLogWindow::OnMouseMove)
189    EVT_LEFT_UP(CavernLogWindow::OnLinkClicked)
[15ba0b5]190    EVT_END_PROCESS(wxID_ANY, CavernLogWindow::OnEndProcess)
[fb5887c]191END_EVENT_TABLE()
192
[549eb37]193wxString escape_for_shell(wxString s, bool protect_dash)
[6bec10c]194{
195#ifdef __WXMSW__
[faf83bee]196    // Correct quoting rules are insane:
197    //
[764fe32]198    // http://blogs.msdn.com/b/twistylittlepassagesallalike/archive/2011/04/23/everyone-quotes-arguments-the-wrong-way.aspx
[faf83bee]199    //
200    // Thankfully wxExecute passes the command string to CreateProcess(), so
201    // at least we don't need to quote for cmd.exe too.
[8204040]202    if (protect_dash && !s.empty() && s[0u] == '-') {
203        // If the filename starts with a '-', protect it from being
204        // treated as an option by prepending ".\".
205        s.insert(0, wxT(".\\"));
206    }
[faf83bee]207    if (s.empty() || s.find_first_of(wxT(" \"\t\n\v")) != s.npos) {
208        // Need to quote.
209        s.insert(0, wxT('"'));
210        for (size_t p = 1; p < s.size(); ++p) {
211            size_t backslashes = 0;
212            while (s[p] == wxT('\\')) {
213                ++backslashes;
214                if (++p == s.size()) {
215                    // Escape all the backslashes, since they're before
216                    // the closing quote we add below.
217                    s.append(backslashes, wxT('\\'));
218                    goto done;
219                }
220            }
221
222            if (s[p] == wxT('"')) {
223                // Escape any preceding backslashes and this quote.
224                s.insert(p, backslashes + 1, wxT('\\'));
225                p += backslashes + 1;
[eff69a7]226            }
[6bec10c]227        }
[faf83bee]228done:
229        s.append(wxT('"'));
[6bec10c]230    }
231#else
[faf83bee]232    size_t p = 0;
[6bec10c]233    if (protect_dash && !s.empty() && s[0u] == '-') {
234        // If the filename starts with a '-', protect it from being
235        // treated as an option by prepending "./".
[5627cbb]236        s.insert(0, wxT("./"));
[6bec10c]237        p = 2;
238    }
239    while (p < s.size()) {
240        // Exclude a few safe characters which are common in filenames
[8adbe49]241        if (!isalnum((unsigned char)s[p]) && strchr("/._-", s[p]) == NULL) {
[6baad4a]242            s.insert(p, 1, wxT('\\'));
[6bec10c]243            ++p;
244        }
245        ++p;
246    }
247#endif
248    return s;
249}
250
[549eb37]251wxString get_command_path(const wxChar * command_name)
252{
253#ifdef __WXMSW__
254    wxString cmd;
255    {
256        DWORD len = 256;
257        wchar_t *buf = NULL;
258        while (1) {
259            DWORD got;
260            buf = (wchar_t*)osrealloc(buf, len * 2);
261            got = GetModuleFileNameW(NULL, buf, len);
262            if (got < len) break;
263            len += len;
264        }
265        /* Strange Win32 nastiness - strip prefix "\\?\" if present */
266        wchar_t *start = buf;
267        if (wcsncmp(start, L"\\\\?\\", 4) == 0) start += 4;
268        wchar_t * slash = wcsrchr(start, L'\\');
269        if (slash) {
270            cmd.assign(start, slash - start + 1);
271        }
272        osfree(buf);
273    }
274#else
[ac20829b]275    wxString cmd = wxString::FromUTF8(msg_exepth());
[549eb37]276#endif
277    cmd += command_name;
278    return cmd;
279}
280
[d7b53e3]281CavernLogWindow::CavernLogWindow(MainFrm * mainfrm_, const wxString & survey_, wxWindow * parent)
[ac20829b]282    : wxScrolledWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
283                       wxFULL_REPAINT_ON_RESIZE),
[76bc864f]284      mainfrm(mainfrm_),
[ac20829b]285      survey(survey_),
286      timer(this)
[fb5887c]287{
[93ff5cc]288}
289
[8991d7f]290CavernLogWindow::~CavernLogWindow()
291{
[ac20829b]292    timer.Stop();
[c1144fe]293    if (cavern_out) {
294        wxEndBusyCursor();
[15ba0b5]295        cavern_out->Detach();
[c1144fe]296    }
[8991d7f]297}
298
[fc626ae]299void
[ac20829b]300CavernLogWindow::OnMouseMove(wxMouseEvent& e)
[fc626ae]301{
[ac20829b]302    const auto& pos = e.GetPosition();
303    int fsize = GetFont().GetPixelSize().GetHeight();
304    int scroll_x = 0, scroll_y = 0;
305    GetViewStart(&scroll_x, &scroll_y);
306    unsigned line = pos.y / fsize + scroll_y;
307    unsigned x = pos.x + scroll_x * fsize - fsize / 2;
308    if (line < line_info.size() && x <= line_info[line].link_pixel_width) {
309        SetCursor(wxCursor(wxCURSOR_HAND));
310    } else {
311        SetCursor(wxNullCursor);
[fc626ae]312    }
313}
314
315void
[ac20829b]316CavernLogWindow::OnLinkClicked(wxMouseEvent& e)
[6bec10c]317{
[ac20829b]318    const auto& pos = e.GetPosition();
319    int fsize = GetFont().GetPixelSize().GetHeight();
320    int scroll_x = 0, scroll_y = 0;
321    GetViewStart(&scroll_x, &scroll_y);
322    unsigned line = pos.y / fsize + scroll_y;
323    unsigned x = pos.x + scroll_x * fsize - fsize / 2;
324    if (!(line < line_info.size() && x <= line_info[line].link_pixel_width))
[3d3fb6c]325        return;
[ac20829b]326
327    const char* cur = &log_txt[line_info[line].start_offset];
328    size_t link_len = line_info[line].link_len;
329    size_t colon = link_len;
330    while (colon > 1 && (unsigned)(cur[--colon] - '0') <= 9) { }
331    size_t colon2 = colon;
332    while (colon > 1 && (unsigned)(cur[--colon] - '0') <= 9) { }
333    if (cur[colon] != ':') {
334        colon = colon2;
335        colon2 = link_len;
336    }
337
[cc2d7ad]338    wxString cmd;
[b8ba399]339    wxChar * p = wxGetenv(wxT("SURVEXEDITOR"));
[3d3fb6c]340    if (p) {
[b8ba399]341        cmd = p;
[3d3fb6c]342        if (!cmd.find(wxT("$f"))) {
343            cmd += wxT(" $f");
[6bec10c]344        }
[cc2d7ad]345    } else {
346        p = wxGetenv(wxT("VISUAL"));
347        if (!p) p = wxGetenv(wxT("EDITOR"));
348        if (!p) {
349            cmd = wxT(DEFAULT_EDITOR_COMMAND);
350        } else {
351            cmd = p;
352            if (cmd == "gvim") {
353                cmd = wxT(GVIM_COMMAND);
354            } else if (cmd == "vim") {
355                cmd = wxT(VIM_COMMAND);
[81d94a4]356            } else if (cmd == "nvim") {
357                cmd = wxT(NVIM_COMMAND);
[cc2d7ad]358            } else if (cmd == "gedit") {
359                cmd = wxT(GEDIT_COMMAND);
360            } else if (cmd == "pluma") {
361                cmd = wxT(PLUMA_COMMAND);
362            } else if (cmd == "emacs") {
363                cmd = wxT(EMACS_COMMAND);
364            } else if (cmd == "nano") {
365                cmd = wxT(NANO_COMMAND);
366            } else if (cmd == "jed") {
367                cmd = wxT(JED_COMMAND);
368            } else if (cmd == "kate") {
369                cmd = wxT(KATE_COMMAND);
370            } else {
371                // Escape any $.
372                cmd.Replace(wxT("$"), wxT("$$"));
373                cmd += wxT(" $f");
374            }
375        }
[3d3fb6c]376    }
377    size_t i = 0;
378    while ((i = cmd.find(wxT('$'), i)) != wxString::npos) {
379        if (++i >= cmd.size()) break;
380        switch ((int)cmd[i]) {
381            case wxT('$'):
382                cmd.erase(i, 1);
383                break;
384            case wxT('f'): {
[ac20829b]385                wxString f = escape_for_shell(wxString(cur, colon), true);
[3d3fb6c]386                cmd.replace(i - 1, 2, f);
387                i += f.size() - 1;
388                break;
389            }
390            case wxT('l'): {
[ac20829b]391                wxString l = escape_for_shell(wxString(cur + colon + 1, colon2 - colon - 1));
[3d3fb6c]392                cmd.replace(i - 1, 2, l);
393                i += l.size() - 1;
394                break;
[1d71195]395            }
[3d3fb6c]396            case wxT('c'): {
397                wxString l;
[ac20829b]398                if (colon2 == link_len)
[3d3fb6c]399                    l = wxT("0");
400                else
[ac20829b]401                    l = escape_for_shell(wxString(cur + colon2 + 1, link_len - colon2 - 1));
[3d3fb6c]402                cmd.replace(i - 1, 2, l);
403                i += l.size() - 1;
404                break;
405            }
406            default:
407                ++i;
[6bec10c]408        }
[3d3fb6c]409    }
[faf83bee]410
411    if (wxExecute(cmd, wxEXEC_ASYNC|wxEXEC_MAKE_GROUP_LEADER) >= 0)
[3d3fb6c]412        return;
[faf83bee]413
[3d3fb6c]414    wxString m;
[736f7df]415    // TRANSLATORS: %s is replaced by the command we attempted to run.
[3d3fb6c]416    m.Printf(wmsg(/*Couldn’t run external command: “%s”*/17), cmd.c_str());
417    m += wxT(" (");
[ac20829b]418    m += wxString::FromUTF8(strerror(errno));
[3d3fb6c]419    m += wxT(')');
420    wxGetApp().ReportError(m);
[6bec10c]421}
422
[8991d7f]423void
[6bec10c]424CavernLogWindow::process(const wxString &file)
425{
[ac20829b]426    timer.Stop();
[c1144fe]427    if (cavern_out) {
[15ba0b5]428        cavern_out->Detach();
[15033fd]429        cavern_out = NULL;
[c1144fe]430    } else {
431        wxBeginBusyCursor();
432    }
433
[fb5887c]434    SetFocus();
435    filename = file;
436
[76bc864f]437    info_count = 0;
[8991d7f]438    link_count = 0;
[0e81a88]439    log_txt.resize(0);
[ac20829b]440    line_info.resize(0);
441    // Reserve enough that we won't need to grow the allocations in normal cases.
442    log_txt.reserve(16384);
443    line_info.reserve(256);
444    ptr = 0;
445    save_button = nullptr;
446    reprocess_button = nullptr;
447    ok_button = nullptr;
448    DestroyChildren();
449    SetVirtualSize(0, 0);
[0e81a88]450
[6bec10c]451#ifdef __WXMSW__
[15322f2]452    SetEnvironmentVariable(wxT("SURVEX_UTF8"), wxT("1"));
[6bec10c]453#else
[06b1227]454    setenv("SURVEX_UTF8", "1", 1);
[6bec10c]455#endif
[93ff5cc]456
[6bec10c]457    wxString escaped_file = escape_for_shell(file, true);
[549eb37]458    wxString cmd = get_command_path(L"cavern");
[9e50f755]459    cmd = escape_for_shell(cmd, false);
[5627cbb]460    cmd += wxT(" -o ");
[6bec10c]461    cmd += escaped_file;
[5627cbb]462    cmd += wxT(' ');
[6bec10c]463    cmd += escaped_file;
464
[15ba0b5]465    cavern_out = wxProcess::Open(cmd);
[6bec10c]466    if (!cavern_out) {
[5627cbb]467        wxString m;
[3d3fb6c]468        m.Printf(wmsg(/*Couldn’t run external command: “%s”*/17), cmd.c_str());
[5627cbb]469        m += wxT(" (");
[ac20829b]470        m += wxString::FromUTF8(strerror(errno));
[5627cbb]471        m += wxT(')');
[6bec10c]472        wxGetApp().ReportError(m);
[8991d7f]473        return;
[6bec10c]474    }
475
[15ba0b5]476    // We want to receive the wxProcessEvent when cavern exits.
477    cavern_out->SetNextHandler(this);
[40b02e8]478
[ac20829b]479    // Check for output after 500ms if we don't get an idle event sooner.
480    timer.StartOnce(500);
[fc626ae]481}
482
483void
[ac20829b]484CavernLogWindow::ProcessCavernOutput()
[fc626ae]485{
[ac20829b]486    // ptr gives the start of the first line we've not yet processed.
487
488    size_t nl;
489    while ((nl = log_txt.find('\n', ptr)) != std::string::npos) {
490        if (nl == ptr || (nl - ptr == 1 && log_txt[ptr] == '\r')) {
491            // Don't show empty lines in the window.
492            ptr = nl + 1;
493            continue;
494        }
495        size_t line_len = nl - ptr - (log_txt[nl - 1] == '\r');
496        // FIXME: Avoid copy, use string_view?
497        string cur(log_txt, ptr, line_len);
498        if (log_txt[ptr] == ' ') {
499            if (expecting_caret_line) {
500                // FIXME: Check the line is only space, `^` and `~`?
501                // Otherwise an error without caret info followed
502                // by an error which contains a '^' gets
503                // mishandled...
504                size_t caret = cur.rfind('^');
505                if (caret != wxString::npos) {
506                    size_t tilde = cur.rfind('~');
507                    if (tilde == wxString::npos || tilde < caret) {
508                        tilde = caret;
[b3ee5f5]509                    }
[ac20829b]510                    line_info.back().colour = line_info[line_info.size() - 2].colour;
511                    line_info.back().colour_start = caret;
512                    line_info.back().colour_len = tilde - caret + 1;
513                    expecting_caret_line = false;
514                    ptr = nl + 1;
515                    continue;
[6bec10c]516                }
517            }
[ac20829b]518            expecting_caret_line = true;
519        }
520        line_info.emplace_back(ptr);
521        line_info.back().len = line_len;
522        size_t colon = cur.find(": ");
523        if (colon != wxString::npos) {
524            size_t link_len = colon;
525            while (colon > 1 && (unsigned)(cur[--colon] - '0') <= 9) { }
526            if (cur[colon] == ':') {
527                line_info.back().link_len = link_len;
528
529                static string info_marker = string(msg(/*info*/485)) + ':';
[7962c9d]530                static string warning_marker = string(msg(/*warning*/106)) + ':';
[ac20829b]531                static string error_marker = string(msg(/*error*/93)) + ':';
532
533                size_t offset = link_len + 2;
534                if (cur.compare(offset, info_marker.size(), info_marker) == 0) {
535                    // Show "info" marker in blue.
536                    ++info_count;
537                    line_info.back().colour = LOG_INFO;
538                    line_info.back().colour_start = offset;
539                    line_info.back().colour_len = info_marker.size() - 1;
540                } else if (cur.compare(offset, warning_marker.size(), warning_marker) == 0) {
541                    // Show "warning" marker in orange.
542                    line_info.back().colour = LOG_WARNING;
543                    line_info.back().colour_start = offset;
544                    line_info.back().colour_len = warning_marker.size() - 1;
545                } else if (cur.compare(offset, error_marker.size(), error_marker) == 0) {
546                    // Show "error" marker in red.
547                    line_info.back().colour = LOG_ERROR;
548                    line_info.back().colour_start = offset;
549                    line_info.back().colour_len = error_marker.size() - 1;
[e768f29]550                }
[ac20829b]551                ++link_count;
[b3ee5f5]552            }
[6bec10c]553        }
[b3ee5f5]554
[ac20829b]555        int fsize = GetFont().GetPixelSize().GetHeight();
556        SetScrollRate(fsize, fsize);
557
558        auto& info = line_info.back();
559        info.link_pixel_width = GetTextExtent(wxString(&log_txt[ptr], info.link_len)).GetWidth();
560        auto rest_pixel_width = GetTextExtent(wxString(&log_txt[ptr + info.link_len], info.len - info.link_len)).GetWidth();
561        int width = max(GetVirtualSize().GetWidth(),
562                        int(fsize + info.link_pixel_width + rest_pixel_width));
563        int height = line_info.size();
564        SetVirtualSize(width, height * fsize);
565        if (!link_count) {
566            // Auto-scroll until the first diagnostic.
567            int scroll_x = 0, scroll_y = 0;
568            GetViewStart(&scroll_x, &scroll_y);
569            int xs, ys;
570            GetClientSize(&xs, &ys);
571            Scroll(scroll_x, line_info.size() * fsize - ys);
572        }
573        ptr = nl + 1;
[6bec10c]574    }
[ac20829b]575}
[8991d7f]576
[ac20829b]577void
578CavernLogWindow::OnEndProcess(wxProcessEvent & evt)
579{
580    bool cavern_success = evt.GetExitCode() == 0;
[f207751]581
[ac20829b]582    // Read and process any remaining buffered output.
583    wxInputStream* in = cavern_out->GetInputStream();
584    while (!in->Eof()) {
585        CheckForOutput(true);
[f207751]586    }
[fb5887c]587
[c1144fe]588    wxEndBusyCursor();
[ac20829b]589
[15ba0b5]590    delete cavern_out;
[8991d7f]591    cavern_out = NULL;
[ac20829b]592
593    // Initially place buttons off the right of the window - they get moved to
594    // the desired position by OnPaintButton().
595    wxPoint off_right(GetSize().x, 0);
596    /* TRANSLATORS: Label for button in aven’s cavern log window which
597     * allows the user to save the log to a file. */
598    save_button = new wxButton(this, LOG_SAVE, wmsg(/*&Save Log*/446), off_right);
599
600    /* TRANSLATORS: Label for button in aven’s cavern log window which
601     * causes the survey data to be reprocessed. */
602    reprocess_button = new wxButton(this, LOG_REPROCESS, wmsg(/*&Reprocess*/184), off_right);
603
604    if (cavern_success) {
605        ok_button = new wxButton(this, wxID_OK, wxString(), off_right);
606        ok_button->SetDefault();
607    }
608
609    Refresh();
610    if (!cavern_success) {
[8991d7f]611        return;
[6bec10c]612    }
[ac20829b]613
[d7b53e3]614    init_done = false;
[fb5887c]615
[15ba0b5]616    {
617        wxString file3d(filename, 0, filename.length() - 3);
618        file3d.append(wxT("3d"));
619        if (!mainfrm->LoadData(file3d, survey)) {
620            return;
621        }
[d7b53e3]622    }
[15ba0b5]623
[76bc864f]624    // Don't stay on log if there there are only "info" diagnostics.
625    if (link_count == info_count) {
[8991d7f]626        wxCommandEvent dummy;
627        OnOK(dummy);
[fb5887c]628    }
[15ba0b5]629}
630
[0e81a88]631void
[330cb03]632CavernLogWindow::OnReprocess(wxCommandEvent &)
[8991d7f]633{
634    process(filename);
635}
636
637void
[0e81a88]638CavernLogWindow::OnSave(wxCommandEvent &)
639{
640    wxString filelog(filename, 0, filename.length() - 3);
[ac20829b]641#ifdef __WXMSW__
642    // We need to consistently use `\` here.
643    filelog.Replace("/", "\\");
644#endif
[0e81a88]645    filelog += wxT("log");
646#ifdef __WXMOTIF__
647    wxString ext(wxT("*.log"));
648#else
[bb71423]649    /* TRANSLATORS: Log files from running cavern (extension .log) */
[0e81a88]650    wxString ext = wmsg(/*Log files*/447);
651    ext += wxT("|*.log");
652#endif
653    wxFileDialog dlg(this, wmsg(/*Select an output filename*/319),
654                     wxString(), filelog, ext,
655                     wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
656    if (dlg.ShowModal() != wxID_OK) return;
657    filelog = dlg.GetPath();
[3206c12]658    FILE * fh_log = wxFopen(filelog, wxT("w"));
[0e81a88]659    if (!fh_log) {
[7962c9d]660        wxGetApp().ReportError(wxString::Format(wmsg(/*Error writing to file “%s”*/7), filelog.c_str()));
[0e81a88]661        return;
662    }
[87c9067]663    FWRITE_(log_txt.data(), log_txt.size(), 1, fh_log);
[0e81a88]664    fclose(fh_log);
665}
666
[fb5887c]667void
668CavernLogWindow::OnOK(wxCommandEvent &)
669{
[d7b53e3]670    if (init_done) {
671        mainfrm->HideLog(this);
672    } else {
[5e0b9f9d]673        mainfrm->InitialiseAfterLoad(filename, survey);
[d7b53e3]674        init_done = true;
675    }
[fb5887c]676}
Note: See TracBrowser for help on using the repository browser.