📄 text_dialog.cpp
字号:
TextRepeatTimeSpinner_adj = gtk_adjustment_new(tp != NULL ? tp->GetFloatValue(CFG_TEXT_REPEAT_TIME_SECS) : 1, 0, 100, 1, 10, 10); TextRepeatTimeSpinner = gtk_spin_button_new(GTK_ADJUSTMENT(TextRepeatTimeSpinner_adj), 1, 1); gtk_widget_show(TextRepeatTimeSpinner); gtk_table_attach(GTK_TABLE(table8), TextRepeatTimeSpinner, 1, 2, 2, 3, (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), (GtkAttachOptions)(0), 0, 0); gtk_spin_button_set_numeric(GTK_SPIN_BUTTON(TextRepeatTimeSpinner), TRUE); label213 = gtk_label_new(_("Text Profile:")); gtk_widget_show(label213); gtk_table_attach(GTK_TABLE(table8), label213, 0, 1, 0, 1, (GtkAttachOptions)(GTK_FILL), (GtkAttachOptions)(0), 0, 0); gtk_misc_set_alignment(GTK_MISC(label213), 0, 0.5); TextProfileEntry = gtk_entry_new(); gtk_widget_show (TextProfileEntry); gtk_table_attach (GTK_TABLE(table8), TextProfileEntry, 1, 2, 0, 1, (GtkAttachOptions)(GTK_EXPAND | GTK_FILL), (GtkAttachOptions)(0), 0, 0); dialog_action_area10 = GTK_DIALOG(TextEncoderDialog)->action_area; gtk_widget_show(dialog_action_area10); gtk_button_box_set_layout(GTK_BUTTON_BOX(dialog_action_area10), GTK_BUTTONBOX_END); cancelbutton8 = gtk_button_new_from_stock("gtk-cancel"); gtk_widget_show(cancelbutton8); gtk_dialog_add_action_widget(GTK_DIALOG(TextEncoderDialog), cancelbutton8, GTK_RESPONSE_CANCEL); GTK_WIDGET_SET_FLAGS(cancelbutton8, GTK_CAN_DEFAULT); okbutton10 = gtk_button_new_from_stock("gtk-ok"); gtk_widget_show(okbutton10); gtk_dialog_add_action_widget(GTK_DIALOG(TextEncoderDialog), okbutton10, GTK_RESPONSE_OK); GTK_WIDGET_SET_FLAGS(okbutton10, GTK_CAN_DEFAULT); g_signal_connect((gpointer) TextEncoderDialog, "response", G_CALLBACK(on_TextEncoderDialog_response), tp); /* Store pointers to all widgets, for use by lookup_widget(). */ GLADE_HOOKUP_OBJECT_NO_REF(TextEncoderDialog, TextEncoderDialog, "TextEncoderDialog"); GLADE_HOOKUP_OBJECT_NO_REF(TextEncoderDialog, dialog_vbox11, "dialog_vbox11"); GLADE_HOOKUP_OBJECT(TextEncoderDialog, table8, "table8"); GLADE_HOOKUP_OBJECT(TextEncoderDialog, TextEncodingOptionMenu, "TextEncodingOptionMenu"); GLADE_HOOKUP_OBJECT(TextEncoderDialog, label211, "label211"); GLADE_HOOKUP_OBJECT(TextEncoderDialog, label212, "label212"); GLADE_HOOKUP_OBJECT(TextEncoderDialog, TextRepeatTimeSpinner, "TextRepeatTimeSpinner"); GLADE_HOOKUP_OBJECT (TextEncoderDialog, label213, "label213"); GLADE_HOOKUP_OBJECT (TextEncoderDialog, TextProfileEntry, "TextProfileEntry"); GLADE_HOOKUP_OBJECT_NO_REF(TextEncoderDialog, dialog_action_area10, "dialog_action_area10"); GLADE_HOOKUP_OBJECT(TextEncoderDialog, cancelbutton8, "cancelbutton8"); GLADE_HOOKUP_OBJECT(TextEncoderDialog, okbutton10, "okbutton10"); uint encoderIndex = 0; uint ix = 0; encoderNames = (const char **)malloc(text_encoder_table_size * sizeof(const char *)); for (ix = 0; ix < text_encoder_table_size; ix++) { if (tp != NULL && strcasecmp(tp->GetStringValue(CFG_TEXT_ENCODING), text_encoder_table[ix].text_encoding) == 0) { encoderIndex = ix; } encoderNames[ix] = text_encoder_table[ix].dialog_selection_name; } CreateOptionMenu(TextEncodingOptionMenu, encoderNames, text_encoder_table_size, encoderIndex); if (tp != NULL) { gtk_entry_set_text(GTK_ENTRY(TextProfileEntry), tp->GetStringValue(CFG_TEXT_PROFILE_NAME)); gtk_widget_set_sensitive(TextProfileEntry, false); } gtk_widget_show(TextEncoderDialog);}/*************************************************************************** * Text File Dialog for running events ***************************************************************************/static guint timer_id = 0;static const GtkTargetEntry drop_types[] = { { "text/plain", 0, 1 }, { "text/uri-list", 0, 2}, { "text/x-moz-url", 0, 3}, { "text/html", 0, 4}, { "UTF8_STRING", 0, 5}, { "STRING", 0, 6},};typedef struct text_line_offset_t { text_line_offset_t *next_line; uint index; uint64_t offset;} text_line_offset_t;typedef struct text_file_data_t { FILE *m_file; char m_buffer[PATH_MAX]; // a nice large number uint m_index; // line in file we're on uint m_max_index; text_line_offset_t *m_line_offset_head, *m_line_offset_tail;};static bool ReadNextLine (text_file_data_t *tptr){ off_t start; start = ftello(tptr->m_file); if (fgets(tptr->m_buffer, PATH_MAX, tptr->m_file) == NULL) { tptr->m_max_index = tptr->m_index; return false; } char *end = tptr->m_buffer + strlen(tptr->m_buffer) - 1; while (isspace(*end) && end > tptr->m_buffer) { *end = '\0'; end--; } debug_message("Read line %u %s", tptr->m_index, tptr->m_buffer); if (tptr->m_line_offset_tail == NULL || tptr->m_line_offset_tail->index < tptr->m_index) { text_line_offset_t *tlptr = MALLOC_STRUCTURE(text_line_offset_t); tlptr->next_line = NULL; tlptr->index = tptr->m_index; tlptr->offset = start; if (tptr->m_line_offset_head == NULL) { tptr->m_line_offset_head = tptr->m_line_offset_tail = tlptr; } else { tptr->m_line_offset_tail->next_line = tlptr; tptr->m_line_offset_tail = tlptr; } debug_message("Add to end"); } tptr->m_index++; return true;}static void GoToLine (text_file_data_t *tptr, text_line_offset_t *tlptr) { debug_message("Go to line - %u offset "U64, tlptr->index, tlptr->offset); fseeko(tptr->m_file, tlptr->offset, SEEK_SET); tptr->m_index = tlptr->index; ReadNextLine(tptr);}static void GoToLine (text_file_data_t *tptr, uint index){ uint ix; debug_message("go to line %u", index); if (tptr->m_line_offset_tail != NULL) { debug_message("tail index %u", index); } if (tptr->m_line_offset_tail != NULL && tptr->m_line_offset_tail->index >= index) { debug_message("Looking for tail"); text_line_offset_t *tlptr; for (ix = 0, tlptr = tptr->m_line_offset_head; ix < index; ix++) { tlptr = tlptr->next_line; } if (tlptr->index != index) { error_message("Seek not right %u %u", tlptr->index, index); } GoToLine(tptr, tlptr); return; } uint start_index = 0; if (tptr->m_line_offset_tail) { start_index = tptr->m_line_offset_tail->index; GoToLine(tptr, tptr->m_line_offset_tail); } for (ix = start_index; ix < index; ix++) { if (ReadNextLine(tptr) == false) return; }}static void GoToLine (text_file_data_t *tptr, int index){ return GoToLine(tptr, (uint)index);}static gbooleanon_TextFileDialog_delete_event (GtkWidget *widget, GdkEvent *event, gpointer user_data){ // don't do anything - the main window will control this dialog return TRUE;}static text_file_data_t *GetTextFileDataFromUserData (gpointer user_data){ GtkWidget *dialog = GTK_WIDGET(user_data); return (text_file_data_t *)lookup_widget(dialog, "TextFileData");}static void DisplayLineInBuffer (gpointer user_data, text_file_data_t *tptr){ GtkWidget *dialog = GTK_WIDGET(user_data); GtkWidget *entry = lookup_widget(dialog, "LineEntry"); gtk_entry_set_text(GTK_ENTRY(entry), tptr->m_buffer); debug_message("Set text to %s", tptr->m_buffer);} static voidon_TextFileDialog_destroy (GtkObject *widget, gpointer user_data){ text_file_data_t *tptr = (text_file_data_t *)lookup_widget(GTK_WIDGET(widget), "TextFileData"); if (timer_id != 0) gtk_timeout_remove(timer_id); if (tptr != NULL) { text_line_offset_t *tlptr = tptr->m_line_offset_head; while (tlptr != NULL) { tptr->m_line_offset_head = tlptr->next_line; free(tlptr); tlptr = tptr->m_line_offset_head; } free(tptr); }}static voidon_StartButton_clicked (GtkButton *button, gpointer user_data){ debug_message("start clicked"); text_file_data_t *tptr = GetTextFileDataFromUserData(user_data); GoToLine(tptr, 0); DisplayLineInBuffer(user_data, tptr);}static voidon_PrevButton_clicked (GtkButton *button, gpointer user_data){ debug_message("prev clicked"); text_file_data_t *tptr = GetTextFileDataFromUserData(user_data); uint index = tptr->m_index; if (tptr->m_index > 2) index = tptr->m_index - 2; else index = 0; GoToLine(tptr, index); DisplayLineInBuffer(user_data, tptr);}static voidon_NextButton_clicked (GtkButton *button, gpointer user_data){ text_file_data_t *tptr = GetTextFileDataFromUserData(user_data); ReadNextLine(tptr); DisplayLineInBuffer(user_data, tptr);}static voidon_EndButton_clicked (GtkButton *button, gpointer user_data){ text_file_data_t *tptr = GetTextFileDataFromUserData(user_data); if (tptr->m_line_offset_tail != NULL) { if (tptr->m_index < tptr->m_line_offset_tail->index) { GoToLine(tptr, tptr->m_line_offset_tail); } } while (ReadNextLine(tptr)); DisplayLineInBuffer(user_data, tptr);}gint on_TextDialog_timeout (gpointer user_data){ GtkWidget *dialog = GTK_WIDGET(user_data); GtkWidget *wid = lookup_widget(dialog, "statusbar2"); gtk_statusbar_pop(GTK_STATUSBAR(wid), 0); gtk_statusbar_push(GTK_STATUSBAR(wid), 0, ""); timer_id = 0; return 0;}static voidon_SendButton_clicked (GtkButton *button, gpointer user_data){ GtkWidget *dialog = GTK_WIDGET(user_data); GtkWidget *wid = lookup_widget(dialog, "LineEntry"); const char *line = gtk_entry_get_text(GTK_ENTRY(wid)); if (AVFlow->GetTextSource() == NULL) { error_message("no text source"); return; } AVFlow->GetTextSource()->SourceString(line); char buffer[60]; snprintf(buffer, sizeof(buffer),"Wrote: %.25s...", line); wid = lookup_widget(dialog, "statusbar2"); gtk_statusbar_pop(GTK_STATUSBAR(wid), 0); gtk_statusbar_push(GTK_STATUSBAR(wid), 0, buffer); if (timer_id != 0) { gtk_timeout_remove(timer_id); } timer_id = gtk_timeout_add(3 * 1000, on_TextDialog_timeout, user_data); text_file_data_t *tptr = GetTextFileDataFromUserData(user_data); if (tptr != NULL) { ReadNextLine(tptr); DisplayLineInBuffer(user_data, tptr); } }static voidon_LineEntry_activate (GtkEntry *ent, gpointer user_data){ on_SendButton_clicked(NULL, user_data);}static void on_drag_data_received_entry (GtkWidget *widget, GdkDragContext *context, gint x, gint y, GtkSelectionData *selection_data, guint info, guint time){ gchar *temp, *string; string = (gchar *)selection_data->data; ADV_SPACE(string); temp = string + strlen(string) - 1; while (isspace(*temp)) { *temp = '\0'; temp--; } gtk_entry_set_text(GTK_ENTRY(widget), string);}static void on_drag_data_received (GtkWidget *widget, GdkDragContext *context, gint x, gint y, GtkSelectionData *selection_data, guint info, guint time){ GtkWidget *entry = lookup_widget(widget, "LineEntry"); on_drag_data_received_entry(entry, context, x, y, selection_data, info, time);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -