void vsx_widget_base_edit::event_mouse_down(vsx_widget_distance distance,vsx_widget_coords coords,int button) { vsx_widget::event_mouse_down(distance,coords,button); if (!lines.size()) return; if (button == 0) { m_focus = this; // caretx = floor(distance.corner.x/(font_size*0.37)); //printf("distance.corner: %f\n",distance.corner.x); caretx = (int)floor(distance.corner.x/(font_size*0.37)); carety = (int)floor(((target_size.y-distance.corner.y)/font_size)); //printf("cx: %d, cy: %d\n",caretx, carety); if (carety < 0) carety = 0; if (carety > lines.size()-num_hidden_lines-1-scroll_y) carety = (int)floor(lines.size()-num_hidden_lines-1-scroll_y); if (lines_visible.size() == lines.size()) { if (selected_line_highlight) { int clicked_line = 0; clicked_line = carety + (int)scroll_y; int real_line = 0; int num_visible_found = 0; while (num_visible_found < clicked_line && real_line < (int)(lines.size()-1)) { if (lines_visible[real_line] == 0) num_visible_found++; real_line++; } while (lines_visible[real_line] != 0 && real_line < (int)(lines.size()-1)) real_line++; selected_line = real_line; } } if (caretx > lines[(int)(carety+scroll_y)].size()-scroll_x) { event_key_down(-GLFW_KEY_END,false,false,false); } //printf("carety: %d\n", carety); } if (mirror_mouse_down_object != 0) mirror_mouse_down_object->event_mouse_down(distance, coords, button); }
bool vsx_widget_base_edit::event_key_down(signed long key, bool alt, bool ctrl, bool shift) { if (!editing_enabled) return true; std::vector<vsx_string>::iterator it = lines.begin(); std::vector<int>::iterator itlv = lines_visible.begin(); std::vector<vsx_string>::iterator itp = lines_p.begin(); int c2 = 0; scroll_x = floor(scroll_x); vsx_string tempstring; vsx_string tempstring2; //printf("key: %d\n",key); if (ctrl && !alt && !shift) { //printf("ctrl! %d\n",key); switch(key) { case 10: //save(); break; case 'v': case 'V': #ifdef _WIN32 HANDLE hData; LPVOID pData; char* pszData = 0; HWND hwnd = GetForegroundWindow(); if (!IsClipboardFormatAvailable(CF_TEXT)) return false; OpenClipboard(hwnd); hData = GetClipboardData(CF_TEXT); pData = GlobalLock(hData); if (pszData) free(pszData); pszData = (char*)malloc(strlen((char*)pData) + 1); strcpy(pszData, (LPSTR)pData); vsx_string res = pszData; GlobalUnlock(hData); CloseClipboard(); res = str_replace("\n","",res); process_characters = false; for (int i = 0; i < res.size(); ++i) { event_key_down(res[i],false,false,false); } free(pszData); process_characters = true; process_lines(); // copying /*HGLOBAL hData; LPVOID pData; OpenClipboard(hwnd); EmptyClipboard(); hData = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, strlen(pszData) + 1); pData = GlobalLock(hData); strcpy((LPSTR)pData, pszData); GlobalUnlock(hData); SetClipboardData(CF_TEXT, hData); CloseClipboard();*/ #endif break; } } else switch(key) { // arrow left case -GLFW_KEY_LEFT: --caretx; if (caretx < 0) { if (scroll_x) { ++caretx; --scroll_x; //fix_pos(); } else if (carety) { --carety; event_key_down(-GLFW_KEY_END); } else caretx = 0; } break; // arrow right case -GLFW_KEY_RIGHT: ++caretx; if ((size_t)caretx > lines[carety+(int)scroll_y].size()-(int)scroll_x) { event_key_down(-GLFW_KEY_DOWN); event_key_down(-GLFW_KEY_HOME); } if (caretx > characters_width-3) { --caretx; ++scroll_x; } break; // arrow up case -GLFW_KEY_UP: if (!single_row) { --carety; if (carety < 0) { carety = 0; if (scroll_y) { --scroll_y; //fix_pos(); } } if ((size_t)caretx > lines[carety+(int)scroll_y].size()-(int)scroll_x) event_key_down(-GLFW_KEY_END); //caretx = lines[carety+(int)scroll_y].size()-(int)scroll_x; } break; // page up case -GLFW_KEY_PAGEUP: if (!single_row) { for (int zz = 0; zz < characters_height*0.95; ++zz) { event_key_down(-GLFW_KEY_UP); } } break; // arrow down case -GLFW_KEY_DOWN: if (!single_row) { ++carety; if (carety > lines.size()-1-scroll_y) carety = (int)((float)lines.size()-1.0f-scroll_y); if (carety > characters_height-2) { ++scroll_y; --carety; } if ((size_t)caretx > lines[carety+(int)scroll_y].size()-(int)scroll_x) caretx = lines[carety+(int)scroll_y].size()-(int)scroll_x; } break; // page down case -GLFW_KEY_PAGEDOWN: if (!single_row) { for (int zz = 0; zz < characters_height*0.95; ++zz) { event_key_down(-GLFW_KEY_DOWN,false,false,false); } } break; // home case -GLFW_KEY_HOME: scroll_x = 0; caretx = 0; //fix_pos(); break; // end case -GLFW_KEY_END: caretx = lines[carety+(int)scroll_y].size()-(int)scroll_x; //if (caretx < 0) caretx = 0; if (caretx > characters_width-3) { scroll_x += caretx - characters_width+3; //fix_pos(); caretx = (int)characters_width-3; } if (caretx < 0) { scroll_x += caretx-5;//lines[carety+(int)scroll_y].size()-5; if (scroll_x < 0) scroll_x = 0; caretx = lines[carety+(int)scroll_y].size()-(int)scroll_x; } //fix_pos(); break; // backspace case -GLFW_KEY_BACKSPACE: if (caretx+(int)scroll_x) { lines[carety+(int)scroll_y].erase(caretx-1+(int)scroll_x,1); --caretx; if (caretx < 0) {--scroll_x; ++caretx;} process_line(carety+(int)scroll_y); //fix_pos(); } else { if (scroll_y+carety) { while (c2 < carety+scroll_y) { ++c2; ++it; ++itp; ++itlv; } //++it; tempstring = lines[carety+(int)scroll_y]; lines.erase(it); lines_p.erase(itp); lines_visible.erase(itlv); event_key_down(-GLFW_KEY_UP,false,false,false); event_key_down(-GLFW_KEY_END,false,false,false); lines[carety+(int)scroll_y] += tempstring; lines_p[carety+(int)scroll_y] += tempstring; process_line(carety+(int)scroll_y); process_line(carety+(int)scroll_y+1); //fix_pos(); } } if (mirror_keystrokes_object) mirror_keystrokes_object->event_key_down(key, alt, ctrl, shift); break; // delete case -GLFW_KEY_DEL: event_key_down(-GLFW_KEY_RIGHT,false,false,false); event_key_down(-GLFW_KEY_BACKSPACE,false,false,false); process_line(carety+(int)scroll_y); if (mirror_keystrokes_object) mirror_keystrokes_object->event_key_down(key, alt, ctrl, shift); break; // enter case -GLFW_KEY_ENTER: if (single_row) { vsx_string d; if (command_prefix.size()) d = command_prefix+" "; command_q_b.add_raw(d+lines[0]); parent->vsx_command_queue_b(this); } else { if ((size_t)caretx+(size_t)scroll_x > lines[carety+(int)scroll_y].size()) event_key_down(-35,false,false,false); while (c2 < carety+(int)scroll_y) { ++c2; ++it; ++itp; ++itlv; } ++it; ++itp; ++itlv; tempstring = lines[carety+(int)scroll_y].substr(caretx+(int)scroll_x,lines[carety+(int)scroll_y].size()-(caretx+(int)scroll_x)); tempstring2 = lines[carety+(int)scroll_y].substr(0,caretx+(int)scroll_x); lines[carety+(int)scroll_y] = tempstring2; lines.insert(it,tempstring); lines_visible.insert(itlv,0); tempstring = lines_p[carety+(int)scroll_y].substr(caretx+(int)scroll_x,lines_p[carety+(int)scroll_y].size()-(caretx+(int)scroll_x)); tempstring2 = lines_p[carety+(int)scroll_y].substr(0,caretx+(int)scroll_x); lines_p[carety+(int)scroll_y] = tempstring2; lines_p.insert(itp,tempstring); event_key_down(-GLFW_KEY_DOWN,false,false,false); event_key_down(-GLFW_KEY_HOME,false,false,false); process_line(carety-1+(int)scroll_y); process_line(carety+(int)scroll_y); } if (mirror_keystrokes_object) mirror_keystrokes_object->event_key_down(key, alt, ctrl, shift); break; // esc case -GLFW_KEY_ESC: // da rest: if (single_row) { command_q_b.add_raw("cancel"); parent->vsx_command_queue_b(this); } else a_focus = k_focus = parent; break; default: if (key > 0) { if (allowed_chars.size()) { if (allowed_chars.find(key) == -1) { break; } } lines[carety+(int)scroll_y].insert(caretx+(int)scroll_x,(char)key); updates++; ++caretx; if ((size_t)caretx > lines[carety+(int)scroll_y].size()-(int)scroll_x) caretx = lines[carety+(int)scroll_y].size()-(int)scroll_x; int t_scroll_x = (int)scroll_x; if (caretx+(int)scroll_x > characters_width) ++scroll_x; //fix_pos(); //cout << scroll_x - t_scroll_x << endl; caretx -= (int)scroll_x - t_scroll_x; process_line(carety+(int)scroll_y); if (mirror_keystrokes_object) mirror_keystrokes_object->event_key_down(key, alt, ctrl, shift); } // FROO } calculate_scroll_size(); //process_lines(); if (longest_line-characters_width <= 0) { scrollbar_pos_x = 0; } else { scrollbar_pos_x = (float)scroll_x/(longest_line-characters_width); } if (longest_y-characters_height <= 0) { scrollbar_pos_y = 0; } else { scrollbar_pos_y = (float)scroll_y/(longest_y-characters_height); } //printf("scroll_x: %f scroll_y: %f\n caretx: %d carety: %d\n",scroll_x,scroll_y,caretx,carety); return false; }
void vsx_widget_base_edit::caret_goto_end() { event_key_down(-GLFW_KEY_END,false,false,false); }
int main(void) { int count = 0; int G_pressed = 0, R_pressed = 1; char vstring[10], wstring[10]; double x1_old, y1_old; double x2_old, y2_old; double x1_new, y1_new; double x2_new, y2_new; double vx, vy, wx, wy; double v, w; posvel pv; x1_old = 100.0; y1_old = 100.0; vx = 10.0; vy = 5.0; x2_old = 250.0; y2_old = 250.0; wx = -8.0; wy = 4.0; v = sqrt(pow(wx,2)+pow(wy,2)); w = sqrt(pow(vx,2)+pow(vy,2)); /* open the graphics window */ initwindow(XWINDOW, YWINDOW); /* allow mouse operations */ initmouse(); /* allow keyboard operations */ initkeyboard(); /* create an event queue */ create_event_queue(); /* register display, mouse and keyboard as event sources */ reg_display_events(); reg_mouse_events(); reg_keyboard_events(); /* initialize the font */ initfont(); outtextxy(4,5,"To quit press left mouse button or close graphics window"); outtextxy(4,15,"To pause press right mouse button"); outtextxy(4,25,"To change ball speed use arrow keys (up/down = red, left/right = blue)"); outtextxy(4,35,"To make the red ball green press G or g "); do { if (check_if_event()) { /* wait for event */ wait_for_event(); if (event_close_display()) break; else if (event_mouse_button_down()) { if (event_mouse_left_button_down()) break; else if (event_mouse_right_button_down()) wait_for_event(); } else if (event_key_down()) { /* change speed of first ball */ if(event_key_up_arrow()) { vx = 1.25*vx; vy = 1.25*vy; } else if(event_key_down_arrow()) { vx = 0.75*vx; vy = 0.75*vy; } else if(event_key_left_arrow()) { wx = 0.75*wx; wy = 0.75*wy; } else if(event_key_right_arrow()) { wx = 1.25*wx; wy = 1.25*wy; } if (event_key('G')) { G_pressed = 1; R_pressed = 0; } else if (event_key('R')) { R_pressed = 1; G_pressed = 0; } } } v = sqrt(pow(wx,2)+pow(wy,2)); w = sqrt(pow(vx,2)+pow(vy,2)); /* calculate new ball positions */ x1_new = x1_old + vx*TICK; y1_new = y1_old + vy*TICK; x2_new = x2_old + wx*TICK; y2_new = y2_old + wy*TICK; /* handle what to do if balls hit boundaries */ pv = hit_boundary(x1_new, vx, 1); x1_new = pv.pos; vx = pv.vel; pv = hit_boundary(x2_new, wx, 1); x2_new = pv.pos; wx = pv.vel; pv = hit_boundary(y1_new, vy, 0); y1_new = pv.pos; vy = pv.vel; pv = hit_boundary(y2_new, wy, 0); y2_new = pv.pos; wy = pv.vel; /* draw balls on screen buffer in new positions */ filled_circle(x1_new, y1_new, RADIUS , BLUE); if (G_pressed) filled_circle(x2_new, y2_new, RADIUS , GREEN); else if (R_pressed) filled_circle(x2_new, y2_new, RADIUS , RED); /* make the balls visible on the screen display and remove the balls in the previous positions */ sprintf(vstring, "%4.2lf", v); sprintf(wstring, "%4.2lf", w); setcolor(RED); outtextxy(VEL_TEXT_X,VEL_TEXT_Y,"v = "); /* clear area for numeric output */ filled_rectangle(VEL_TEXT_X+40, VEL_TEXT_Y-10, VEL_TEXT_X+100, VEL_TEXT_Y+20, BLACK); outtextxy(VEL_TEXT_X+60,VEL_TEXT_Y,vstring); setcolor(BLUE); outtextxy(VEL_TEXT_X+120,VEL_TEXT_Y,"w = "); /* clear area for numeric output */ filled_rectangle(VEL_TEXT_X+160, VEL_TEXT_Y-10, VEL_TEXT_X+220, VEL_TEXT_Y+20, BLACK); outtextxy(VEL_TEXT_X+180,VEL_TEXT_Y,wstring); update_display(); /* remove the balls in the previous positions on the screen buffer */ filled_circle(x1_old, y1_old, RADIUS , BLACK); filled_circle(x2_old, y2_old, RADIUS , BLACK); /* update the old positions */ x1_old = x1_new; y1_old = y1_new; x2_old = x2_new; y2_old = y2_new; count++; pausefor(8); /* wait 8 miliseconds */ } while (count < MAXCOUNT); /* close the mouse */ closemouse(); /* remove the display */ closegraph(); return 0; }