3 Minimum Profit - Programmer Text Editor
7 Copyright (C) 1991-2009 Angel Ortega <angel@triptico.com>
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 http://www.triptico.com
54 /* font handlers and metrics */
55 HFONT font_normal
= NULL
;
56 HFONT font_underline
= NULL
;
60 /* height of the tab set */
63 /* height of the status bar */
64 int status_height
= 16;
66 int is_wm_keydown
= 0;
69 static COLORREF
*inks
= NULL
;
70 static COLORREF
*papers
= NULL
;
71 int *underlines
= NULL
;
74 /* code for the 'normal' attribute */
75 static int normal_attr
= 0;
78 static HMENU menu
= NULL
;
80 /* mp.drv.form() controls */
82 static mpdm_t form_args
= NULL
;
83 static mpdm_t form_values
= NULL
;
86 static int mouse_down
= 0;
89 static mpdm_t timer_func
= NULL
;
93 static void update_window_size(void)
94 /* updates the viewport size in characters */
100 /* no font information? go */
101 if (font_width
== 0 || font_height
== 0)
104 GetClientRect(hwnd
, &rect
);
106 /* calculate the size in chars */
107 tx
= ((rect
.right
- rect
.left
) / font_width
) + 1;
108 ty
= (rect
.bottom
- rect
.top
- tab_height
) / font_height
;
110 /* store the 'window' size */
111 v
= mpdm_hget_s(mp
, L
"window");
112 mpdm_hset_s(v
, L
"tx", MPDM_I(tx
));
113 mpdm_hset_s(v
, L
"ty", MPDM_I(ty
));
117 static void build_fonts(HDC hdc
)
118 /* build the fonts */
123 char * font_face
= "Lucida Console";
126 if (font_normal
!= NULL
) {
127 SelectObject(hdc
, GetStockObject(SYSTEM_FONT
));
128 DeleteObject(font_normal
);
131 /* get current configuration */
132 if ((c
= mpdm_hget_s(mp
, L
"config")) != NULL
) {
135 if ((v
= mpdm_hget_s(c
, L
"font_size")) != NULL
)
136 font_size
= mpdm_ival(v
);
138 mpdm_hset_s(c
, L
"font_size", MPDM_I(font_size
));
140 if ((v
= mpdm_hget_s(c
, L
"font_face")) != NULL
) {
141 v
= MPDM_2MBS(v
->data
);
142 font_face
= (char *)v
->data
;
145 mpdm_hset_s(c
, L
"font_face", MPDM_MBS(font_face
));
149 n
= -MulDiv(font_size
, GetDeviceCaps(hdc
, LOGPIXELSY
), 72);
151 font_normal
= CreateFont(n
, 0, 0, 0, 0, 0, 0,
152 0, 0, 0, 0, 0, 0, font_face
);
154 font_underline
= CreateFont(n
, 0, 0, 0, 0, 0, 1,
155 0, 0, 0, 0, 0, 0, font_face
);
157 SelectObject(hdc
, font_normal
);
158 GetTextMetrics(hdc
, &tm
);
161 font_height
= tm
.tmHeight
;
162 font_width
= tm
.tmAveCharWidth
;
164 update_window_size();
168 static void build_colors(void)
169 /* builds the colors */
176 /* gets the color definitions and attribute names */
177 colors
= mpdm_hget_s(mp
, L
"colors");
178 l
= mpdm_keys(colors
);
181 /* redim the structures */
182 inks
= realloc(inks
, sizeof(COLORREF
) * s
);
183 papers
= realloc(papers
, sizeof(COLORREF
) * s
);
184 underlines
= realloc(underlines
, sizeof(int) * s
);
186 /* loop the colors */
187 for (n
= 0; n
< s
&& (c
= mpdm_aget(l
, n
)) != NULL
; n
++) {
188 mpdm_t d
= mpdm_hget(colors
, c
);
189 mpdm_t v
= mpdm_hget_s(d
, L
"gui");
192 /* store the 'normal' attribute */
193 if (wcscmp(mpdm_string(c
), L
"normal") == 0)
197 mpdm_hset_s(d
, L
"attr", MPDM_I(n
));
199 m
= mpdm_ival(mpdm_aget(v
, 0));
200 inks
[n
] = ((m
& 0x000000ff) << 16)|
202 ((m
& 0x00ff0000) >> 16);
203 m
= mpdm_ival(mpdm_aget(v
, 1));
204 papers
[n
] = ((m
& 0x000000ff) << 16)|
206 ((m
& 0x00ff0000) >> 16);
209 v
= mpdm_hget_s(d
, L
"flags");
211 underlines
[n
] = mpdm_seek_s(v
, L
"underline", 1) != -1 ? 1 : 0;
213 if (mpdm_seek_s(v
, L
"reverse", 1) != -1) {
222 /* create the background brush */
223 bgbrush
= CreateSolidBrush(papers
[normal_attr
]);
227 static void build_menu(void)
228 /* builds the menu */
232 int win32_menu_id
= 1000;
234 /* gets the current menu */
235 if ((m
= mpdm_hget_s(mp
, L
"menu")) == NULL
)
243 for (n
= 0; n
< mpdm_size(m
); n
++) {
247 HMENU submenu
= CreatePopupMenu();
249 /* get the label and the items */
250 mi
= mpdm_aget(m
, n
);
251 v
= mpdm_gettext(mpdm_aget(mi
, 0));
252 l
= mpdm_aget(mi
, 1);
254 /* create the submenus */
255 for (i
= 0; i
< mpdm_size(l
); i
++) {
257 mpdm_t v
= mpdm_aget(l
, i
);
259 /* if the action is a separator... */
260 if (*((wchar_t *)v
->data
) == L
'-')
261 AppendMenu(submenu
, MF_SEPARATOR
, 0, NULL
);
264 mpdm_t d
= mp_menu_label(v
);
267 ptr
= mpdm_wcstombs(mpdm_string(d
), NULL
);
268 AppendMenu(submenu
, MF_STRING
, win32_menu_id
, ptr
);
271 /* store the action inside the menu */
272 memset(&mi
, '\0', sizeof(mi
));
273 mi
.cbSize
= sizeof(mi
);
274 mi
.fMask
= MIIM_DATA
;
275 mi
.dwItemData
= (unsigned long)v
;
277 SetMenuItemInfo(submenu
, win32_menu_id
, FALSE
, &mi
);
283 /* now store the popup inside the menu */
284 ptr
= mpdm_wcstombs(mpdm_string(v
), NULL
);
285 AppendMenu(menu
, MF_STRING
|MF_POPUP
, (UINT
)submenu
, ptr
);
293 static void draw_filetabs(void)
294 /* draws the filetabs */
296 static mpdm_t last
= NULL
;
303 names
= mp_get_doc_names();
305 /* is the list different from the previous one? */
306 if (mpdm_cmp(names
, last
) != 0) {
307 TabCtrl_DeleteAllItems(hwtabs
);
309 for (n
= 0; n
< mpdm_size(names
); n
++) {
312 mpdm_t v
= mpdm_aget(names
, n
);
315 ptr
= mpdm_wcstombs(v
->data
, NULL
);
321 TabCtrl_InsertItem(hwtabs
, n
, &ti
);
326 /* store for the next time */
327 mpdm_unref(last
); last
= mpdm_ref(names
);
330 /* set the active one */
331 TabCtrl_SetCurSel(hwtabs
, mpdm_ival(mpdm_hget_s(mp
, L
"active_i")));
335 static void draw_scrollbar(void)
336 /* updates the scrollbar */
343 /* gets the active document */
344 if ((d
= mp_active()) == NULL
)
347 /* get the coordinates */
348 v
= mpdm_hget_s(d
, L
"txt");
349 pos
= mpdm_ival(mpdm_hget_s(v
, L
"vy"));
350 max
= mpdm_size(mpdm_hget_s(v
, L
"lines"));
352 v
= mpdm_hget_s(mp
, L
"window");
353 size
= mpdm_ival(mpdm_hget_s(v
, L
"ty"));
355 si
.cbSize
= sizeof(si
);
362 SetScrollInfo(hwnd
, SB_VERT
, &si
, TRUE
);
366 void draw_status(void)
367 /* draws the status line */
371 if (hwstatus
!= NULL
&& (t
= mp_build_status_line()) != NULL
) {
372 t
= MPDM_2MBS(t
->data
);
375 SetWindowText(hwstatus
, t
->data
);
380 static void win32_draw(HWND hwnd
, mpdm_t doc
)
381 /* win32 document draw function */
391 hdc
= BeginPaint(hwnd
, &ps
);
393 /* no font? construct it */
394 if (font_normal
== NULL
) {
399 /* no document? end */
400 if ((d
= mp_draw(doc
, 0)) == NULL
) {
405 /* select defaults to start painting */
406 SelectObject(hdc
, font_normal
);
408 GetClientRect(hwnd
, &rect
);
411 r2
.top
+= tab_height
;
412 r2
.bottom
= r2
.top
+ font_height
;
414 for (n
= 0; n
< mpdm_size(d
); n
++) {
415 mpdm_t l
= mpdm_aget(d
, n
);
419 for (m
= 0; m
< mpdm_size(l
); m
++) {
423 /* get the attribute and the string */
424 attr
= mpdm_ival(mpdm_aget(l
, m
++));
427 SetTextColor(hdc
, inks
[attr
]);
428 SetBkColor(hdc
, papers
[attr
]);
430 SelectObject(hdc
, underlines
[attr
] ?
431 font_underline
: font_normal
);
433 TextOutW(hdc
, r2
.left
, r2
.top
, s
->data
, mpdm_size(s
));
434 r2
.left
+= mpdm_size(s
) * font_width
;
437 /* fills the rest of the line */
438 FillRect(hdc
, &r2
, bgbrush
);
440 r2
.top
+= font_height
;
441 r2
.bottom
+= font_height
;
452 static void redraw(void)
454 InvalidateRect(hwnd
, NULL
, TRUE
);
458 static void win32_vkey(int c
)
459 /* win32 virtual key processing */
461 wchar_t * ptr
= NULL
;
462 static int maxed
= 0;
464 /* set mp.shift_pressed */
465 if (GetKeyState(VK_SHIFT
) & 0x8000)
466 mpdm_hset_s(mp
, L
"shift_pressed", MPDM_I(1));
468 if (GetKeyState(VK_CONTROL
) & 0x8000 ||
469 GetKeyState(VK_MENU
) & 0x8000) {
471 case VK_UP
: ptr
= L
"ctrl-cursor-up"; break;
472 case VK_DOWN
: ptr
= L
"ctrl-cursor-down"; break;
473 case VK_LEFT
: ptr
= L
"ctrl-cursor-left"; break;
474 case VK_RIGHT
: ptr
= L
"ctrl-cursor-right"; break;
475 case VK_PRIOR
: ptr
= L
"ctrl-page-up"; break;
476 case VK_NEXT
: ptr
= L
"ctrl-page-down"; break;
477 case VK_HOME
: ptr
= L
"ctrl-home"; break;
478 case VK_END
: ptr
= L
"ctrl-end"; break;
479 case VK_SPACE
: ptr
= L
"ctrl-space"; break;
480 case VK_DIVIDE
: ptr
= L
"ctrl-kp-divide"; break;
481 case VK_MULTIPLY
: ptr
= L
"ctrl-kp-multiply"; break;
482 case VK_SUBTRACT
: ptr
= L
"ctrl-kp-minus"; break;
483 case VK_ADD
: ptr
= L
"ctrl-kp-plus"; break;
484 case VK_RETURN
: ptr
= L
"ctrl-enter"; break;
485 case VK_F1
: ptr
= L
"ctrl-f1"; break;
486 case VK_F2
: ptr
= L
"ctrl-f2"; break;
487 case VK_F3
: ptr
= L
"ctrl-f3"; break;
488 case VK_F4
: ptr
= L
"ctrl-f4"; break;
489 case VK_F5
: ptr
= L
"ctrl-f5"; break;
490 case VK_F6
: ptr
= L
"ctrl-f6"; break;
491 case VK_F7
: ptr
= L
"ctrl-f7"; break;
492 case VK_F8
: ptr
= L
"ctrl-f8"; break;
493 case VK_F9
: ptr
= L
"ctrl-f9"; break;
494 case VK_F10
: ptr
= L
"ctrl-f10"; break;
495 case VK_F11
: ptr
= L
"ctrl-f11"; break;
497 SendMessage(hwnd
, WM_SYSCOMMAND
,
498 maxed
? SC_RESTORE
: SC_MAXIMIZE
, 0);
507 case VK_UP
: ptr
= L
"cursor-up"; break;
508 case VK_DOWN
: ptr
= L
"cursor-down"; break;
509 case VK_LEFT
: ptr
= L
"cursor-left"; break;
510 case VK_RIGHT
: ptr
= L
"cursor-right"; break;
511 case VK_PRIOR
: ptr
= L
"page-up"; break;
512 case VK_NEXT
: ptr
= L
"page-down"; break;
513 case VK_HOME
: ptr
= L
"home"; break;
514 case VK_END
: ptr
= L
"end"; break;
515 case VK_RETURN
: ptr
= L
"enter"; break;
516 case VK_BACK
: ptr
= L
"backspace"; break;
517 case VK_DELETE
: ptr
= L
"delete"; break;
518 case VK_INSERT
: ptr
= L
"insert"; break;
519 case VK_DIVIDE
: ptr
= L
"kp-divide"; break;
520 case VK_MULTIPLY
: ptr
= L
"kp-multiply"; break;
521 case VK_SUBTRACT
: ptr
= L
"kp-minus"; break;
522 case VK_ADD
: ptr
= L
"kp-plus"; break;
523 case VK_F1
: ptr
= L
"f1"; break;
524 case VK_F2
: ptr
= L
"f2"; break;
525 case VK_F3
: ptr
= L
"f3"; break;
526 case VK_F4
: ptr
= L
"f4"; break;
527 case VK_F5
: ptr
= L
"f5"; break;
528 case VK_F6
: ptr
= L
"f6"; break;
529 case VK_F7
: ptr
= L
"f7"; break;
530 case VK_F8
: ptr
= L
"f8"; break;
531 case VK_F9
: ptr
= L
"f9"; break;
532 case VK_F10
: ptr
= L
"f10"; break;
533 case VK_F11
: ptr
= L
"f11"; break;
534 case VK_F12
: ptr
= L
"f12"; break;
539 mp_process_event(MPDM_S(ptr
));
543 if (mp_keypress_throttle(1))
549 #define ctrl(c) ((c) & 31)
551 static void win32_akey(int k
)
552 /* win32 alphanumeric key processing */
555 wchar_t * ptr
= NULL
;
557 /* set mp.shift_pressed */
558 if (GetKeyState(VK_SHIFT
) & 0x8000)
559 mpdm_hset_s(mp
, L
"shift_pressed", MPDM_I(1));
562 case ctrl(' '): ptr
= L
"ctrl-space"; break;
563 case ctrl('a'): ptr
= L
"ctrl-a"; break;
564 case ctrl('b'): ptr
= L
"ctrl-b"; break;
565 case ctrl('c'): ptr
= L
"ctrl-c"; break;
566 case ctrl('d'): ptr
= L
"ctrl-d"; break;
567 case ctrl('e'): ptr
= L
"ctrl-e"; break;
568 case ctrl('f'): ptr
= L
"ctrl-f"; break;
569 case ctrl('g'): ptr
= L
"ctrl-g"; break;
570 case ctrl('h'): /* same as backspace */ break;
571 case ctrl('i'): ptr
= L
"ctrl-i"; break;
572 case ctrl('j'): ptr
= L
"ctrl-j"; break;
573 case ctrl('k'): ptr
= L
"ctrl-k"; break;
574 case ctrl('l'): ptr
= L
"ctrl-l"; break;
575 case ctrl('m'): /* same as ENTER */ break;
576 case ctrl('n'): ptr
= L
"ctrl-n"; break;
577 case ctrl('o'): ptr
= L
"ctrl-o"; break;
578 case ctrl('p'): ptr
= L
"ctrl-p"; break;
579 case ctrl('q'): ptr
= L
"ctrl-q"; break;
580 case ctrl('r'): ptr
= L
"ctrl-r"; break;
581 case ctrl('s'): ptr
= L
"ctrl-s"; break;
582 case ctrl('t'): ptr
= L
"ctrl-t"; break;
583 case ctrl('u'): ptr
= L
"ctrl-u"; break;
584 case ctrl('v'): ptr
= L
"ctrl-v"; break;
585 case ctrl('w'): ptr
= L
"ctrl-w"; break;
586 case ctrl('x'): ptr
= L
"ctrl-x"; break;
587 case ctrl('y'): ptr
= L
"ctrl-y"; break;
588 case ctrl('z'): ptr
= L
"ctrl-z"; break;
589 case ' ': ptr
= L
"space"; break;
590 case 27: ptr
= L
"escape"; break;
593 /* this is probably very bad */
602 mp_process_event(MPDM_S(ptr
));
609 static void win32_vscroll(UINT wparam
)
610 /* scrollbar messages handler */
612 wchar_t * ptr
= NULL
;
615 switch (LOWORD(wparam
)) {
616 case SB_PAGEUP
: ptr
= L
"page-up"; break;
617 case SB_PAGEDOWN
: ptr
= L
"page-down"; break;
618 case SB_LINEUP
: ptr
= L
"cursor-up"; break;
619 case SB_LINEDOWN
: ptr
= L
"cursor-down"; break;
620 case SB_THUMBPOSITION
:
622 /* set both y and vy */
623 txt
= mpdm_hget_s(mp_active(), L
"txt");
624 mp_set_y(mp_active(), HIWORD(wparam
));
625 mpdm_hset_s(txt
, L
"vy", MPDM_I(HIWORD(wparam
)));
631 mp_process_event(MPDM_S(ptr
));
637 static void action_by_menu(int item
)
638 /* execute an action triggered by the menu */
642 memset(&mi
, '\0', sizeof(mi
));
643 mi
.cbSize
= sizeof(mi
);
644 mi
.fMask
= MIIM_DATA
;
646 if (GetMenuItemInfo(menu
, item
, FALSE
, &mi
)) {
647 if (mi
.dwItemData
!= 0) {
648 mp_process_action((mpdm_t
)mi
.dwItemData
);
655 static void dropped_files(HDROP hDrop
)
656 /* fill the mp.dropped_files array with the dropped files */
658 mpdm_t a
= MPDM_A(0);
662 n
= DragQueryFile(hDrop
, 0xffffffff, NULL
, sizeof(tmp
) - 1);
665 DragQueryFile(hDrop
, n
, tmp
, sizeof(tmp
) - 1);
666 mpdm_push(a
, MPDM_MBS(tmp
));
671 mpdm_hset_s(mp
, L
"dropped_files", a
);
672 mp_process_event(MPDM_LS(L
"dropped-files"));
677 #ifndef WM_MOUSEWHEEL
678 #define WM_MOUSEWHEEL 0x020A
681 long STDCALL
WndProc(HWND hwnd
, UINT msg
, UINT wparam
, LONG lparam
)
682 /* main window Proc */
686 wchar_t * ptr
= NULL
;
692 DragAcceptFiles(hwnd
, TRUE
);
697 dropped_files((HDROP
) wparam
);
704 if (mp_keypress_throttle(0))
721 win32_vscroll(wparam
);
726 if (mpdm_size(mpdm_hget_s(mp
, L
"docs")))
727 win32_draw(hwnd
, mp_active());
733 if (!IsIconic(hwnd
)) {
734 update_window_size();
736 MoveWindow(hwtabs
, 0, 0, LOWORD(lparam
), tab_height
, FALSE
);
738 MoveWindow(hwstatus
, 0, HIWORD(lparam
) - status_height
,
739 LOWORD(lparam
), status_height
, FALSE
);
754 x
= (LOWORD(lparam
)) / font_width
;
755 y
= (HIWORD(lparam
) - tab_height
) / font_height
;
757 mpdm_hset_s(mp
, L
"mouse_x", MPDM_I(x
));
758 mpdm_hset_s(mp
, L
"mouse_y", MPDM_I(y
));
761 case WM_LBUTTONDOWN
: ptr
= L
"mouse-left-button"; break;
762 case WM_RBUTTONDOWN
: ptr
= L
"mouse-right-button"; break;
763 case WM_MBUTTONDOWN
: ptr
= L
"mouse-middle-button"; break;
767 mp_process_event(MPDM_S(ptr
));
781 x
= (LOWORD(lparam
)) / font_width
;
782 y
= (HIWORD(lparam
) - tab_height
) / font_height
;
784 mpdm_hset_s(mp
, L
"mouse_to_x", MPDM_I(x
));
785 mpdm_hset_s(mp
, L
"mouse_to_y", MPDM_I(y
));
787 mp_process_event(MPDM_LS(L
"mouse-drag"));
795 if ((int) wparam
> 0)
796 ptr
= L
"mouse-wheel-up";
798 ptr
= L
"mouse-wheel-down";
801 mp_process_event(MPDM_S(ptr
));
809 action_by_menu(LOWORD(wparam
));
816 if (!mp_exit_requested
)
817 mp_process_event(MPDM_LS(L
"close-window"));
819 if (mp_exit_requested
)
831 if (p
->code
== TCN_SELCHANGE
) {
832 /* tab selected by clicking on it */
833 int n
= TabCtrl_GetCurSel(hwtabs
);
835 /* set mp.active_i to this */
836 mpdm_hset_s(mp
, L
"active_i", MPDM_I(n
));
844 mpdm_exec(timer_func
, NULL
);
849 if (mp_exit_requested
)
850 PostMessage(hwnd
, WM_CLOSE
, 0, 0);
852 return DefWindowProcW(hwnd
, msg
, wparam
, lparam
);
856 static mpdm_t
win32_drv_clip_to_sys(mpdm_t a
)
857 /* driver-dependent mp to system clipboard */
865 /* convert the clipboard to DOS text */
866 d
= mpdm_hget_s(mp
, L
"clipboard");
868 if (mpdm_size(d
) == 0)
871 d
= mpdm_join(MPDM_LS(L
"\r\n"), d
);
872 ptr
= mpdm_wcstombs(d
->data
, &s
);
874 /* allocates a handle and copies */
875 hclp
= GlobalAlloc(GHND
, s
+ 1);
876 clpptr
= (char *)GlobalLock(hclp
);
877 memcpy(clpptr
, ptr
, s
);
885 SetClipboardData(CF_TEXT
, hclp
);
892 static mpdm_t
win32_drv_sys_to_clip(mpdm_t a
)
893 /* driver-dependent system to mp clipboard */
899 hclp
= GetClipboardData(CF_TEXT
);
902 if (hclp
&& (ptr
= GlobalLock(hclp
)) != NULL
) {
905 /* create a value and split */
907 d
= mpdm_split(MPDM_LS(L
"\r\n"), d
);
909 /* and set as the clipboard */
910 mpdm_hset_s(mp
, L
"clipboard", d
);
911 mpdm_hset_s(mp
, L
"clipboard_vertical", MPDM_I(0));
920 static mpdm_t
win32_drv_main_loop(mpdm_t a
)
924 if (!mp_exit_requested
) {
927 while (GetMessage(&msg
, NULL
, 0, 0)) {
928 TranslateMessage(&msg
);
929 DispatchMessage(&msg
);
937 static mpdm_t
win32_drv_shutdown(mpdm_t a
)
941 SendMessage(hwnd
, WM_CLOSE
, 0, 0);
943 if ((v
= mpdm_hget_s(mp
, L
"exit_message")) != NULL
) {
944 char * ptr
= mpdm_wcstombs(mpdm_string(v
), NULL
);
945 MessageBox(NULL
, ptr
, "mp " VERSION
, MB_ICONWARNING
|MB_OK
);
953 static mpdm_t
win32_drv_alert(mpdm_t a
)
954 /* alert driver function */
960 wptr
= mpdm_string(mpdm_aget(a
, 0));
962 if ((ptr
= mpdm_wcstombs(wptr
, NULL
)) != NULL
) {
963 MessageBox(hwnd
, ptr
, "mp " VERSION
, MB_ICONWARNING
|MB_OK
);
971 static mpdm_t
win32_drv_confirm(mpdm_t a
)
972 /* confirm driver function */
979 wptr
= mpdm_string(mpdm_aget(a
, 0));
981 if ((ptr
= mpdm_wcstombs(wptr
, NULL
)) != NULL
) {
982 ret
= MessageBox(hwnd
, ptr
, "mp " VERSION
, MB_ICONQUESTION
|MB_YESNOCANCEL
);
998 static LPWORD
lpwAlign(LPWORD lpIn
)
999 /* aligns a pointer to DWORD boundary (for dialog templates) */
1011 #define LABEL_ID 1000
1012 #define CTRL_ID 2000
1014 BOOL CALLBACK
formDlgProc(HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
1015 /* mp.drv.form() dialog proc */
1023 SetWindowText(hwnd
, "mp " VERSION
);
1025 hf
= GetStockObject(DEFAULT_GUI_FONT
);
1027 /* fill controls with its initial data */
1028 for (n
= 0; n
< mpdm_size(form_args
); n
++) {
1029 mpdm_t w
= mpdm_aget(form_args
, n
);
1032 int ctrl
= CTRL_ID
+ n
;
1036 if ((t
= mpdm_hget_s(w
, L
"label")) != NULL
) {
1037 if ((ptr
= mpdm_wcstombs(mpdm_string(t
), NULL
)) != NULL
) {
1038 SetDlgItemText(hwnd
, LABEL_ID
+ n
, ptr
);
1041 SendDlgItemMessage(hwnd
, LABEL_ID
+ n
, WM_SETFONT
,
1042 (WPARAM
) hf
, MAKELPARAM(FALSE
, 0));
1046 SendDlgItemMessage(hwnd
, ctrl
, WM_SETFONT
,
1047 (WPARAM
) hf
, MAKELPARAM(FALSE
, 0));
1049 type
= mpdm_string(mpdm_hget_s(w
, L
"type"));
1051 if (wcscmp(type
, L
"text") == 0) {
1052 if ((t
= mpdm_hget_s(w
, L
"value")) != NULL
&&
1053 (ptr
= mpdm_wcstombs(mpdm_string(t
), NULL
)) != NULL
) {
1054 SetDlgItemText(hwnd
, ctrl
, ptr
);
1058 /* store the history into combo_items */
1059 if ((t
= mpdm_hget_s(w
, L
"history")) != NULL
) {
1060 t
= mp_get_history(t
);
1063 for (i
= 0; i
< mpdm_size(t
); i
++) {
1064 mpdm_t v
= mpdm_aget(t
, i
);
1066 if ((ptr
= mpdm_wcstombs(v
->data
,
1068 SendDlgItemMessage(hwnd
,
1078 if (wcscmp(type
, L
"password") == 0) {
1079 SendDlgItemMessage(hwnd
, ctrl
,
1080 EM_SETPASSWORDCHAR
, (WPARAM
)'*', (LPARAM
)0);
1083 if (wcscmp(type
, L
"checkbox") == 0) {
1084 if ((t
= mpdm_hget_s(w
, L
"value")) != NULL
)
1085 SendDlgItemMessage(hwnd
, ctrl
,
1086 BM_SETCHECK
, mpdm_ival(t
) ?
1087 BST_CHECKED
: BST_UNCHECKED
,
1091 if (wcscmp(type
, L
"list") == 0) {
1094 t
= mpdm_hget_s(w
, L
"list");
1097 for (i
= 0; i
< mpdm_size(t
); i
++) {
1098 wptr
= mpdm_string(mpdm_aget(t
, i
));
1099 if ((ptr
= mpdm_wcstombs(wptr
, NULL
)) != NULL
) {
1100 SendDlgItemMessage(hwnd
, ctrl
,
1101 LB_ADDSTRING
, 0, (LPARAM
) ptr
);
1107 SendDlgItemMessage(hwnd
, ctrl
, LB_SETCURSEL
,
1108 mpdm_ival(mpdm_hget_s(w
, L
"value")), 0);
1112 /* FIXME: untranslated strings */
1114 SetDlgItemText(hwnd
, IDOK
, "OK");
1115 SendDlgItemMessage(hwnd
, IDOK
, WM_SETFONT
,
1116 (WPARAM
) hf
, MAKELPARAM(FALSE
, 0));
1118 SetDlgItemText(hwnd
, IDCANCEL
, "Cancel");
1119 SendDlgItemMessage(hwnd
, IDCANCEL
, WM_SETFONT
,
1120 (WPARAM
) hf
, MAKELPARAM(FALSE
, 0));
1126 if (LOWORD(wparam
) == IDCANCEL
) {
1131 if (LOWORD(wparam
) != IDOK
)
1134 /* fill all return values */
1135 for (n
= 0; n
< mpdm_size(form_args
); n
++) {
1136 mpdm_t w
= mpdm_aget(form_args
, n
);
1137 wchar_t * type
= mpdm_string(mpdm_hget_s(w
, L
"type"));
1138 int ctrl
= CTRL_ID
+ n
;
1140 if (wcscmp(type
, L
"text") == 0) {
1145 GetDlgItemText(hwnd
, ctrl
, tmp
, sizeof(tmp
) - 1);
1148 mpdm_aset(form_values
, v
, n
);
1150 /* if it has history, fill it */
1151 if ((h
= mpdm_hget_s(w
, L
"history")) != NULL
&&
1152 v
!= NULL
&& mpdm_cmp(v
, MPDM_LS(L
"")) != 0) {
1153 h
= mp_get_history(h
);
1155 if (mpdm_cmp(v
, mpdm_aget(h
, -1)) != 0)
1159 if (wcscmp(type
, L
"password") == 0) {
1162 GetDlgItemText(hwnd
, ctrl
, tmp
, sizeof(tmp
) - 1);
1163 mpdm_aset(form_values
, MPDM_MBS(tmp
), n
);
1166 if (wcscmp(type
, L
"checkbox") == 0) {
1167 mpdm_aset(form_values
,
1168 MPDM_I(SendDlgItemMessage(hwnd
, ctrl
,
1169 BM_GETCHECK
, 0, 0)), n
);
1172 if (wcscmp(type
, L
"list") == 0) {
1173 mpdm_aset(form_values
,
1174 MPDM_I(SendDlgItemMessage(hwnd
, ctrl
,
1175 LB_GETCURSEL
, 0, 0)), n
);
1187 static void build_form_data(mpdm_t widget_list
)
1188 /* builds the necessary information for a list of widgets */
1190 mpdm_unref(form_args
);
1191 form_args
= mpdm_ref(widget_list
);
1193 mpdm_unref(form_values
);
1194 form_values
= widget_list
== NULL
? NULL
:
1195 mpdm_ref(MPDM_A(mpdm_size(form_args
)));
1199 LPWORD
static build_control(LPWORD lpw
, int x
, int y
,
1200 int cx
, int cy
, int id
, int class, int style
)
1201 /* fills a control structure in a hand-made dialog template */
1203 LPDLGITEMTEMPLATE lpdit
;
1205 lpw
= lpwAlign(lpw
);
1206 lpdit
= (LPDLGITEMTEMPLATE
)lpw
;
1207 lpdit
->x
= x
; lpdit
->y
= y
;
1208 lpdit
->cx
= cx
; lpdit
->cy
= cy
;
1210 lpdit
->style
= style
;
1212 lpw
= (LPWORD
)(lpdit
+ 1);
1216 /* no text (will be set on dialog setup) */
1220 /* Align creation data on DWORD boundary */
1221 lpw
= lpwAlign(lpw
);
1222 /* No creation data */
1229 static mpdm_t
win32_drv_form(mpdm_t a
)
1230 /* mp.drv.form() function */
1239 /* first argument: list of widgets */
1240 build_form_data(mpdm_aget(a
, 0));
1242 /* On-the-fly dialog template creation */
1243 /* Note: all this crap is taken from MSDN, no less */
1245 /* magic size; looking for problems */
1246 hgbl
= GlobalAlloc(GMEM_ZEROINIT
, 4096);
1247 lpdt
= (LPDLGTEMPLATE
)GlobalLock(hgbl
);
1249 lpdt
->style
= WS_POPUP
| WS_BORDER
| WS_SYSMENU
| DS_MODALFRAME
| WS_CAPTION
;
1250 lpdt
->cdit
= (2 * mpdm_size(form_args
)) + 2;
1251 lpdt
->x
= 20; lpdt
->y
= 20;
1254 lpw
= (LPWORD
)(lpdt
+ 1);
1255 *lpw
++ = 0; /* No menu */
1256 *lpw
++ = 0; /* Predefined dialog box class (by default) */
1257 *lpw
++ = 0; /* No title */
1259 /* first pass: calculate maximum size of labels */
1260 for (n
= 0; n
< mpdm_size(form_args
); n
++) {
1261 mpdm_t w
= mpdm_aget(form_args
, n
);
1262 int l
= mpdm_size(mpdm_hget_s(w
, L
"label"));
1268 /* second pass: create the dialog controls */
1269 for (n
= p
= 0; n
< mpdm_size(form_args
); n
++) {
1270 mpdm_t w
= mpdm_aget(form_args
, n
);
1278 lpw
= build_control(lpw
, 0, 5 + p
* il
,
1279 lbl
* 3, 20, LABEL_ID
+ n
, 0x0082,
1280 WS_CHILD
| WS_VISIBLE
| SS_RIGHT
);
1282 type
= mpdm_string(mpdm_hget_s(w
, L
"type"));
1284 if (wcscmp(type
, L
"text") == 0) {
1286 style
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
|
1287 CBS_DROPDOWN
| CBS_AUTOHSCROLL
| WS_VSCROLL
;
1293 if (wcscmp(type
, L
"password") == 0) {
1295 style
= WS_CHILD
| WS_VISIBLE
| WS_BORDER
| WS_TABSTOP
;
1298 if (wcscmp(type
, L
"checkbox") == 0) {
1300 style
= WS_CHILD
| WS_VISIBLE
| BS_AUTOCHECKBOX
| WS_TABSTOP
;
1303 if (wcscmp(type
, L
"list") == 0) {
1305 style
= WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| WS_BORDER
|
1306 LBS_NOINTEGRALHEIGHT
| WS_VSCROLL
|
1307 LBS_NOTIFY
| LBS_USETABSTOPS
;
1314 lpw
= build_control(lpw
, 10 + lbl
* 3, 5 + p
* il
,
1315 245 - lbl
* 3, inc
* il
* sz
, CTRL_ID
+ n
,
1322 /* set total height */
1323 lpdt
->cy
= 30 + p
* il
;
1326 lpw
= build_control(lpw
, 170, 10 + p
* il
, 40, 15, IDOK
,
1327 0x0080, WS_CHILD
| WS_VISIBLE
| BS_DEFPUSHBUTTON
| WS_TABSTOP
);
1330 lpw
= build_control(lpw
, 215, 10 + p
* il
, 40, 15, IDCANCEL
,
1331 0x0080, WS_CHILD
| WS_VISIBLE
| BS_PUSHBUTTON
| WS_TABSTOP
);
1334 n
= DialogBoxIndirect(hinst
, (LPDLGTEMPLATE
)hgbl
,
1335 hwnd
, (DLGPROC
)formDlgProc
);
1339 return n
? form_values
: NULL
;
1343 static mpdm_t
open_or_save(int o
, mpdm_t a
)
1344 /* manages an open or save file dialog */
1349 char buf
[1024] = "";
1353 /* 1# arg: prompt */
1354 wptr
= mpdm_string(mpdm_aget(a
, 0));
1355 ptr
= mpdm_wcstombs(wptr
, NULL
);
1357 memset(&ofn
, '\0', sizeof(OPENFILENAME
));
1358 ofn
.lStructSize
= sizeof(OPENFILENAME
);
1359 ofn
.hwndOwner
= hwnd
;
1360 ofn
.lpstrFilter
= "*.*\0*.*\0";
1361 ofn
.nFilterIndex
= 1;
1362 ofn
.lpstrFile
= buf
;
1363 ofn
.nMaxFile
= sizeof(buf
);
1364 ofn
.lpstrTitle
= ptr
;
1365 ofn
.lpstrDefExt
= "";
1367 GetCurrentDirectory(sizeof(buf2
), buf2
);
1368 ofn
.lpstrInitialDir
= buf2
;
1370 /* ofn.lpstrDefExt=(def==NULL ? "" : def);*/
1373 ofn
.Flags
= OFN_PATHMUSTEXIST
|OFN_HIDEREADONLY
|
1374 OFN_NOCHANGEDIR
|OFN_FILEMUSTEXIST
;
1376 r
= GetOpenFileName(&ofn
);
1379 ofn
.Flags
= OFN_HIDEREADONLY
;
1381 r
= GetSaveFileName(&ofn
);
1387 return MPDM_MBS(buf
);
1393 static mpdm_t
win32_drv_openfile(mpdm_t a
)
1394 /* openfile driver function */
1396 return open_or_save(1, a
);
1400 static mpdm_t
win32_drv_savefile(mpdm_t a
)
1401 /* savefile driver function */
1403 return open_or_save(0, a
);
1407 static mpdm_t
win32_drv_update_ui(mpdm_t a
)
1409 build_fonts(GetDC(hwnd
));
1417 static mpdm_t
win32_drv_timer(mpdm_t a
)
1419 int msecs
= mpdm_ival(mpdm_aget(a
, 0));
1420 mpdm_t func
= mpdm_aget(a
, 1);
1423 /* previously defined one? remove */
1424 if (timer_func
!= NULL
)
1427 /* if msecs and func are set, program timer */
1428 if (msecs
> 0 && func
!= NULL
)
1429 SetTimer(hwnd
, 1, msecs
, NULL
);
1431 r
= mpdm_unref(timer_func
);
1432 timer_func
= mpdm_ref(func
);
1438 static mpdm_t
win32_drv_busy(mpdm_t a
)
1440 int onoff
= mpdm_ival(mpdm_aget(a
, 0));
1442 SetCursor(LoadCursor(NULL
, onoff
? IDC_WAIT
: IDC_ARROW
));
1448 static void register_functions(void)
1452 drv
= mpdm_hget_s(mp
, L
"drv");
1453 mpdm_hset_s(drv
, L
"main_loop", MPDM_X(win32_drv_main_loop
));
1454 mpdm_hset_s(drv
, L
"shutdown", MPDM_X(win32_drv_shutdown
));
1456 mpdm_hset_s(drv
, L
"clip_to_sys", MPDM_X(win32_drv_clip_to_sys
));
1457 mpdm_hset_s(drv
, L
"sys_to_clip", MPDM_X(win32_drv_sys_to_clip
));
1458 mpdm_hset_s(drv
, L
"update_ui", MPDM_X(win32_drv_update_ui
));
1459 mpdm_hset_s(drv
, L
"timer", MPDM_X(win32_drv_timer
));
1460 mpdm_hset_s(drv
, L
"busy", MPDM_X(win32_drv_busy
));
1462 mpdm_hset_s(drv
, L
"alert", MPDM_X(win32_drv_alert
));
1463 mpdm_hset_s(drv
, L
"confirm", MPDM_X(win32_drv_confirm
));
1464 mpdm_hset_s(drv
, L
"openfile", MPDM_X(win32_drv_openfile
));
1465 mpdm_hset_s(drv
, L
"savefile", MPDM_X(win32_drv_savefile
));
1466 mpdm_hset_s(drv
, L
"form", MPDM_X(win32_drv_form
));
1470 static mpdm_t
win32_drv_startup(mpdm_t a
)
1476 register_functions();
1478 InitCommonControls();
1480 /* register the window */
1481 wc
.style
= CS_HREDRAW
|CS_VREDRAW
;
1482 wc
.lpfnWndProc
= WndProc
;
1485 wc
.hInstance
= hinst
;
1486 wc
.hIcon
= LoadIcon(hinst
,"MP_ICON");
1487 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
);
1488 wc
.hbrBackground
= NULL
;
1489 wc
.lpszMenuName
= NULL
;
1490 wc
.lpszClassName
= L
"minimumprofit5.x";
1492 RegisterClassW(&wc
);
1494 /* create the window */
1495 hwnd
= CreateWindowW(L
"minimumprofit5.x", L
"mp " VERSION
,
1496 WS_OVERLAPPEDWINDOW
| WS_CLIPCHILDREN
| WS_VSCROLL
,
1497 CW_USEDEFAULT
, CW_USEDEFAULT
,
1498 CW_USEDEFAULT
, CW_USEDEFAULT
,
1499 NULL
, NULL
, hinst
, NULL
);
1501 ShowWindow(hwnd
, SW_SHOW
);
1504 GetClientRect(hwnd
, &r
);
1506 hwtabs
= CreateWindow(WC_TABCONTROL
, "tab",
1507 WS_CHILD
| TCS_TABS
| TCS_SINGLELINE
| TCS_FOCUSNEVER
,
1508 0, 0, r
.right
- r
.left
, tab_height
, hwnd
, NULL
, hinst
, NULL
);
1510 SendMessage(hwtabs
, WM_SETFONT
,
1511 (WPARAM
) GetStockObject(DEFAULT_GUI_FONT
), 0);
1513 ShowWindow(hwtabs
, SW_SHOW
);
1514 UpdateWindow(hwtabs
);
1516 hwstatus
= CreateWindow(WC_STATIC
, "status",
1518 0, r
.bottom
- r
.top
- status_height
,
1519 r
.right
- r
.left
, status_height
, hwnd
, NULL
, hinst
, NULL
);
1521 win32_drv_update_ui(NULL
);
1523 SendMessage(hwstatus
, WM_SETFONT
,
1524 (WPARAM
) GetStockObject(DEFAULT_GUI_FONT
), 0);
1526 ShowWindow(hwstatus
, SW_SHOW
);
1527 UpdateWindow(hwstatus
);
1529 if ((v
= mpdm_hget_s(mp
, L
"config")) != NULL
&&
1530 mpdm_ival(mpdm_hget_s(v
, L
"maximize")) > 0)
1531 SendMessage(hwnd
, WM_SYSCOMMAND
, SC_MAXIMIZE
, 0);
1537 int win32_drv_detect(int * argc
, char *** argv
)
1541 drv
= mpdm_hget_s(mp
, L
"drv");
1542 mpdm_hset_s(drv
, L
"id", MPDM_LS(L
"win32"));
1543 mpdm_hset_s(drv
, L
"startup", MPDM_X(win32_drv_startup
));
1548 #endif /* CONFOPT_WIN32 */