Skip to content

Commit

Permalink
v1.0.4 Released!
Browse files Browse the repository at this point in the history
+Support Line number.
+Support Latex parsing in web browser.
+Fix a bug that unable to scroll after maximized the window.
  • Loading branch information
fungaren committed Aug 27, 2018
1 parent 7e169c3 commit 388c480
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 42 deletions.
24 changes: 12 additions & 12 deletions MarkdownParser.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#pragma once
#include "lex_parse.h"
#include <sstream>
#include <regex>
#include <cstdlib>
#include <iostream>
#pragma once
#include "lex_parse.h"
#include <sstream>
#include <regex>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#ifdef _DEBUG
#define SHOW_PARSEING_TIME
#define SHOW_PARSING_TIME
#endif
void parse_markdown(std::wstring& str)
{
//std::wregex regex_table_align(L"^([:\\s]?-+[:\\s]?\\|?)+$");
//bool re = std::regex_match(str, regex_table_align);
//int j = 5;
#ifdef SHOW_PARSEING_TIME
#ifdef SHOW_PARSING_TIME
clock_t begin = clock();
#endif

Expand All @@ -22,10 +22,10 @@ void parse_markdown(std::wstring& str)
auto scanned = scanner(str);
parse_fromlex(wos, std::begin(scanned), std::end(scanned));

#ifdef SHOW_PARSEING_TIME
#ifdef SHOW_PARSING_TIME
clock_t end = clock();
double t = double(end - begin) / CLOCKS_PER_SEC;
wos << L"<br><p>ÓÃʱ£º" << std::setprecision(2) << t << L" s</p>";
#endif // SHOW_PARSEING_TIME
wos << L"<br><p>用时:" << std::setprecision(2) << t << L" s</p>";
#endif // SHOW_PARSING_TIME
str = wos.str();
}
}
Binary file modified MyNotePad.rc
Binary file not shown.
110 changes: 87 additions & 23 deletions controller.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#pragma once
#include "MarkdownParser.h"

//---------------------------------
Article article = { Line() }; // article text
Cursor caret(article); // current position = end of selection
Cursor sel_begin(article); // beginning of selection
bool bSaved = true; // set false when file is modified
bool bSaved = true; // set false after file is modified
unsigned int textView_width = 0, textView_height = 0;
unsigned int caret_x, caret_y; // for IME (relative to EditArea)
unsigned int xoffset = 0; // offset-x of textView
unsigned int yoffset = 0; // offset-y of textView
size_t ClientWidth, ClientHeight, EditAreaWidth, EditAreaHeight;
bool word_wrap = false; // break word
bool show_line_number = false; // display line number
bool word_wrap = false; // break word (initial false)
bool show_line_number = true; // display line number (initial true)
std::unique_ptr<Font> line_number_font;
bool resized = false;
char32_t two_utf16_encoded_chars = 0; // if a unicode between U+10000 and U+10FFFF (eg. emoji)
// input by user, it will be encoded with UTF-16.
Expand Down Expand Up @@ -291,6 +291,7 @@ void repaintSelectedLines()
}

void OnPaint(HDC hdc) {
// Window size changed so we should adjust text.
if (word_wrap && resized)
{
// for selection
Expand Down Expand Up @@ -335,14 +336,37 @@ void OnPaint(HDC hdc) {

size_t y = 0;
Article::const_iterator l = article.begin();
// draw first line (probably can't be seen entirely)
size_t n_line = article.size(); // a counter for line number
if (show_line_number)
{
line_number_font->bind(clientDC);
if (n_line > 999)
MNP_PADDING_LEFT = 64;
else if (n_line > 99)
MNP_PADDING_LEFT = 50;
else if (n_line > 9)
MNP_PADDING_LEFT = 38;
else
MNP_PADDING_LEFT = 26;
n_line = 0;
}
// draw first line (probably can't be fully seen)
for (; l != article.end(); ++l)
{
y += l->text_height + l->padding_top;
if (y > yoffset) // first line in edit area
if (show_line_number && !l->child_line)
++n_line;
// the first line of edit area
if (y > yoffset)
{
if (show_line_number)
{
std::wstringstream ss; ss << n_line;
line_number_font->printLine(ss.str(),
4, 6+(y - l->text_height - l->padding_top) - yoffset);
}
BitBlt(clientDC,
MNP_PADDING_CLIENT, // dest x
MNP_PADDING_LEFT, // dest x
0, // dest y
l->text_width + l->padding_left, // dest width
y - yoffset, // dest height
Expand All @@ -367,22 +391,29 @@ void OnPaint(HDC hdc) {
{
caret_x -= xoffset;
caret_y = (y - l->text_height) - yoffset;
GDIUtil::line(clientDC, MNP_FONTCOLOR, caret_x + MNP_PADDING_CLIENT, caret_y,
caret_x + MNP_PADDING_CLIENT, caret_y + caret.getSentence()->text_height);
GDIUtil::line(clientDC, MNP_FONTCOLOR, caret_x + MNP_PADDING_LEFT, caret_y,
caret_x + MNP_PADDING_LEFT, caret_y + caret.getSentence()->text_height);
}
}
++l;
break;
}
}
y -= yoffset; // Y-Axis to paint the second line
// draw middle lines
// draw subsequent lines
for (; l != article.end(); ++l)
{
if (show_line_number)
{
if (!l->child_line)
++n_line;
std::wstringstream ss; ss << n_line;
line_number_font->printLine(ss.str(), 4, 6 + y);
}
if (y > EditAreaHeight) // last line (can't be seen entirely)
{
BitBlt(clientDC,
MNP_PADDING_CLIENT, // dest x
MNP_PADDING_LEFT, // dest x
y, // dest y
l->text_width + l->padding_left, // dest width
EditAreaHeight - (y - l->text_height - l->padding_top), // dest height
Expand All @@ -393,7 +424,7 @@ void OnPaint(HDC hdc) {
break;
}
BitBlt(clientDC,
MNP_PADDING_CLIENT, // dest x
MNP_PADDING_LEFT, // dest x
y, // dest y
l->text_width + l->padding_left, // dest width
l->text_height + l->padding_top, // dest height
Expand All @@ -418,15 +449,18 @@ void OnPaint(HDC hdc) {
{
caret_x -= xoffset;
caret_y = y + caret.getSentence()->padding_top;
GDIUtil::line(clientDC, MNP_FONTCOLOR, caret_x + MNP_PADDING_CLIENT, caret_y,
caret_x + MNP_PADDING_CLIENT, caret_y + caret.getSentence()->text_height);
GDIUtil::line(clientDC, MNP_FONTCOLOR, caret_x + MNP_PADDING_LEFT, caret_y,
caret_x + MNP_PADDING_LEFT, caret_y + caret.getSentence()->text_height);
}
}
y += l->text_height + l->padding_top;
}

if (show_line_number)
line_number_font->unbind();

// draw vertical scrollbar
if (textView_height > ClientHeight - MNP_PADDING_CLIENT) {
if (textView_height > ClientHeight - MNP_PADDING_LEFT) {
GDIUtil::fill(clientDC,
MNP_SCROLLBAR_BGCOLOR,
ClientWidth - MNP_SCROLLBAR_WIDTH,
Expand All @@ -443,7 +477,7 @@ void OnPaint(HDC hdc) {
);
}
// draw horizontal scrollbar
if (!word_wrap && textView_width > ClientWidth - MNP_PADDING_CLIENT) {
if (!word_wrap && textView_width > ClientWidth - MNP_PADDING_LEFT) {
GDIUtil::fill(clientDC,
MNP_SCROLLBAR_BGCOLOR,
0,
Expand Down Expand Up @@ -530,7 +564,7 @@ void repaintSelectionCanceledLines()
inline void OnSize(unsigned int width, unsigned int height) {
ClientWidth = width;
ClientHeight = height;
EditAreaWidth = ClientWidth - MNP_PADDING_CLIENT - MNP_SCROLLBAR_WIDTH;
EditAreaWidth = ClientWidth - MNP_PADDING_LEFT - MNP_SCROLLBAR_WIDTH;
EditAreaHeight = ClientHeight - MNP_SCROLLBAR_WIDTH;
resized = true;
if (word_wrap)
Expand Down Expand Up @@ -757,7 +791,7 @@ Cursor PosToCaret(int cursor_x, int cursor_y)
cursor_y += yoffset;
if (cursor_y < 0)
cursor_y = 0;
cursor_x += xoffset - MNP_PADDING_CLIENT;
cursor_x += xoffset - MNP_PADDING_LEFT;
if (cursor_x < 0)
cursor_x = 0;

Expand Down Expand Up @@ -825,9 +859,16 @@ inline void OnMouseMove(DWORD wParam, int x, int y) {
}

void OnMouseHWheel(short zDeta, int x, int y) {

if (textView_width < EditAreaWidth)
// no scroll bar
if (textView_width < EditAreaWidth && xoffset == 0)
{
if (xoffset != 0)
{
xoffset = 0;
force_OnPaint();
}
return;
}
xoffset += zDeta / 2;

if ((int)xoffset < 0)
Expand All @@ -844,8 +885,16 @@ void OnMouseWheel(short zDeta, int x, int y) {
return;
}

// no scroll bar
if (textView_height < EditAreaHeight)
{
if (yoffset != 0)
{
yoffset = 0;
force_OnPaint();
}
return;
}
yoffset -= zDeta / 2;

if ((int)yoffset < 0)
Expand Down Expand Up @@ -960,7 +1009,13 @@ inline void saveHTML(T pathname)
parse_markdown(str);
f << cvt.to_bytes(str);
f << "</main><center><small>Created by <a href=\"https:/" \
"/github.com/mooction/MyNotePad\">MyNotePad</a>.</small></center></body>";
"/github.com/mooction/MyNotePad\">MyNotePad</a>.</small></center><script src=\"https:/"\
"/cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=default\" type=\""\
"text/javascript\"></script><script type=\"text/x-mathjax-config;executed=true\">"\
"MathJax.Hub.Config({\n"\
" TeX: {equationNumbers: { autoNumber: \"AMS\" } },\n"\
" tex2jax: {inlineMath: [['$','$'], ['\\\\(','\\\\)']]}\n"\
"});</script></body>";
f.close();
}

Expand Down Expand Up @@ -1261,12 +1316,16 @@ void OnMenuTheme(UINT IDM_THEME)
themeDark();
else return;

line_number_font = std::make_unique<Font>(
MNP_LINENUM_SIZE, MNP_LINENUM_FONTFACE, MNP_LINENUM_FONTCOLOR);

HDC hdc = GetDC(hWnd);
for (auto& l : article)
{
l.background_color = MNP_BGCOLOR_EDIT;
repaintLine(hdc, l);
}
repaintSelectedLines();

OnPaint(hdc);
ReleaseDC(hWnd, hdc);
Expand All @@ -1276,12 +1335,17 @@ void OnMenuTheme(UINT IDM_THEME)

void OnMenuLineNumber()
{
HDC hdc = GetDC(hWnd);

show_line_number = !show_line_number;
HMENU hMenu = GetMenu(hWnd);
CheckMenuItem(hMenu, IDM_LINENUMBER, show_line_number ? MF_CHECKED : MF_UNCHECKED);

if (show_line_number)
line_number_font = std::make_unique<Font>(
MNP_LINENUM_SIZE, MNP_LINENUM_FONTFACE, MNP_LINENUM_FONTCOLOR);
else
MNP_PADDING_LEFT = 20;

HDC hdc = GetDC(hWnd);
OnPaint(hdc);
ReleaseDC(hWnd, hdc);

Expand Down
2 changes: 1 addition & 1 deletion model.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ struct Line
mdc(nullptr)
{ }

operator std::wstring() const
operator std::wstring() const
{
return Character::to_wstring(sentence.begin(),sentence.end());
}
Expand Down
20 changes: 14 additions & 6 deletions view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
HWND hWnd;
HINSTANCE hInst;

const int MNP_PADDING_CLIENT = 20; // padding 20px
int MNP_PADDING_LEFT = 20; // space for line number
LPCSTR MNP_CONFIG_FILE = "MyNotePad.conf";
LPCSTR MNP_CONFIG_THEME = "theme";
LPCSTR MNP_CONFIG_WORDWRAP = "wordwrap";
LPCSTR MNP_CONFIG_LINENUMBER = "linenumber";
LPCTSTR MNP_APPNAME = L"MyNotePad";
LPCTSTR MNP_FONTFACE = L"Microsoft Yahei UI"; // L"Lucida Console";
LPCTSTR MNP_LINENUM_FONTFACE = L"Lucida Console";
const int MNP_FONTSIZE = 28;
const int MNP_LINENUM_SIZE = 20;
const int MNP_LINEHEIGHT = MNP_FONTSIZE;
COLOR MNP_SCROLLBAR_WIDTH = 14;

Expand All @@ -24,6 +26,7 @@ THEME theme = THEME::THEME_LIGHT;
COLOR MNP_BGCOLOR_EDIT = 0x00EEEEEE;
COLOR MNP_BGCOLOR_SEL = 0x00CCCCCC;
COLOR MNP_FONTCOLOR = 0x00444444;
COLOR MNP_LINENUM_FONTCOLOR = MNP_BGCOLOR_SEL;

COLOR MNP_SCROLLBAR_BGCOLOR = 0x00E5E5E5;
COLOR MNP_SCROLLBAR_COLOR = 0x00D1D1D1;
Expand All @@ -33,6 +36,7 @@ inline void themeWhite()
MNP_BGCOLOR_EDIT = 0x00EEEEEE;
MNP_BGCOLOR_SEL = 0x00CCCCCC;
MNP_FONTCOLOR = 0x00444444;
MNP_LINENUM_FONTCOLOR = MNP_BGCOLOR_SEL;

MNP_SCROLLBAR_BGCOLOR = 0x00E5E5E5;
MNP_SCROLLBAR_COLOR = 0x00D1D1D1;
Expand All @@ -47,6 +51,7 @@ inline void themeDark()
MNP_BGCOLOR_EDIT = 0x001E1E1E;
MNP_BGCOLOR_SEL = COLOR(44, 92, 139);
MNP_FONTCOLOR = 0x00FFFFFF;
MNP_LINENUM_FONTCOLOR = MNP_BGCOLOR_SEL;

MNP_SCROLLBAR_BGCOLOR = 0x003E3E3E;
MNP_SCROLLBAR_COLOR = 0x00686868;
Expand Down Expand Up @@ -95,16 +100,19 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
HMENU hMenu = GetMenu(hWnd);
CheckMenuItem(hMenu, IDM_WORDWRAP, MF_CHECKED);
}
else if (key == MNP_CONFIG_LINENUMBER && val == "1")
else if (key == MNP_CONFIG_LINENUMBER && val == "0")
{
show_line_number = true;
show_line_number = false;
HMENU hMenu = GetMenu(hWnd);
CheckMenuItem(hMenu, IDM_LINENUMBER, MF_CHECKED);
CheckMenuItem(hMenu, IDM_LINENUMBER, MF_UNCHECKED);
}
}
}
}

// set default line_number_font
line_number_font = std::make_unique<Font>(
MNP_LINENUM_SIZE, MNP_LINENUM_FONTFACE, MNP_LINENUM_FONTCOLOR);

HDC hdc = GetDC(hWnd);
for (auto& l : article)
{
Expand Down Expand Up @@ -255,7 +263,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
HIMC hImc = ImmGetContext(hWnd);
COMPOSITIONFORM Composition;
Composition.dwStyle = CFS_POINT;
Composition.ptCurrentPos.x = caret_x + MNP_PADDING_CLIENT; // IME follow
Composition.ptCurrentPos.x = caret_x + MNP_PADDING_LEFT; // IME follow
Composition.ptCurrentPos.y = caret_y + MNP_FONTSIZE / 4; // set IME position
ImmSetCompositionWindow(hImc, &Composition);
ImmReleaseContext(hWnd, hImc);
Expand Down

0 comments on commit 388c480

Please sign in to comment.