主要倚賴了Glade,否則這麼標準(繁複)的版面,一個字一個字慢慢刻上可是會死人的。
此簡單小程式擁有記事本的基本功能,文件的開啟、修改和儲存;剪貼簿的剪下、複製和貼上;還有挺陽春的搜尋和取代功能。雖然這些都很基本,但還是花了不少時間,且經常陷入瓶頸,不得不說,網路上的幾份技術文件真的幫了大忙,尤其是http://www.bravegnu.org/gtktext,多虧他的幾份有關複製貼上和搜尋的解說和完整程式範例,否則真不之從何下手。感謝Vijay Kumar!
這隻小程式的全部圖片皆取用自mathilde所設計的Sketchy圖示庫,也謝謝他,設計得挺有個性,我很喜歡。
因為程式碼大部分都是Glade自動生成的,所以只貼callbacks.c和support.c。
完整程式碼 + 執行檔:
http://popodragon.myweb.hinet.net/Program/project_Notepad_v1.0.rar
callbacks.c:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
#ifdef HAVE_CONFIG_H #include <config.h> #endif #include <gtk/gtk.h> #include "support.h" #include "callbacks.h" #include "interface.h" const gchar *gSearchWord ; void on________n_1_activate (GtkMenuItem *menuitem, AD data) { GtkTextBuffer *buffer; buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (data->textview)); gtk_text_buffer_set_text (buffer, "", 0); gtk_window_set_title (GTK_WINDOW (data->window), utf8("新文件")); } void on________o_1_activate (GtkMenuItem *menuitem, AD data) { //GtkWidget *dialog = create_file_chooser_open_dialog (GTK_WINDOW(window)); GtkWidget *dialog = gtk_file_chooser_dialog_new ("Open File", GTK_WINDOW(data->window), GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL); if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) { char *filename; filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog)); open_file (filename, GTK_TEXT_VIEW (data->textview)); gtk_window_set_title (GTK_WINDOW (data->window), filename); g_free (filename); } gtk_widget_destroy (dialog); } void on________s_1_activate (GtkMenuItem *menuitem, AD data) // 儲存檔案 { GtkWidget *dialog; dialog = gtk_file_chooser_dialog_new ("Save File", GTK_WINDOW(data->window), GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL); gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE); if (0)//user_edited_a_new_document) { gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), "NEW.txt");//default_folder_for_saving); gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), "Untitled document"); } else gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (dialog), "OLD.txt");//filename_for_existing_document); if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) { char *filename; filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog)); save_to_file (filename, GTK_TEXT_VIEW (data->textview)); gtk_window_set_title (GTK_WINDOW (data->window), filename); g_free (filename); } gtk_widget_destroy (dialog); } void on______________a_1_activate (GtkMenuItem *menuitem, AD data) { } void on________q_1_activate (GtkMenuItem *menuitem, AD data) // 離開 { gtk_widget_destroy(data->window); } void on________t_1_activate (GtkMenuItem *menuitem, AD data) // 剪下 { GtkTextBuffer *buffer; GtkClipboard *clipboard; // 若GDK_NONE不行就換GDK_SELECTION_PRIMARY clipboard = gtk_clipboard_get( GDK_NONE ); // 取得反白選取的部份 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (data->textview)); // 取得整個編輯緩衝區 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE); // 將反白選取的部份剪下到剪貼簿 } void on________c_1_activate (GtkMenuItem *menuitem, AD data) // 複製 { GtkTextBuffer *buffer; GtkClipboard *clipboard; clipboard = gtk_clipboard_get( GDK_NONE ); // 取得反白選取的部份 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (data->textview)); // 取得整個編輯緩衝區 gtk_text_buffer_copy_clipboard (buffer, clipboard); // 將反白選取的部份複製到剪貼簿 } void on________p_1_activate (GtkMenuItem *menuitem, AD data) // 貼上 { GtkTextBuffer *buffer; GtkClipboard *clipboard; clipboard = gtk_clipboard_get( GDK_NONE ); // 取得反白選取的部份 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (data->textview)); // 取得整個編輯緩衝區 gtk_text_buffer_paste_clipboard (buffer, clipboard, NULL, TRUE); // 將剪貼簿的內容貼到指定區域 } void on________d_1_activate (GtkMenuItem *menuitem, AD data) // 刪除 { GtkTextBuffer *buffer; buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (data->textview)); // 取得整個編輯緩衝區 gtk_text_buffer_delete_selection (buffer, TRUE, TRUE); // 將反白選取的部份刪除 } void on_about1_activate (GtkMenuItem *menuitem, AD data) // 開啟關於dialog { GtkWidget *dialog1_information = create_dialog1_information (); gtk_widget_show (dialog1_information); gtk_dialog_run (GTK_DIALOG (dialog1_information)); gtk_widget_destroy (dialog1_information); } void on_mainfile1_activate (GtkMenuItem *menuitem, AD data) // 開啟main.c { char *filename; filename = "..\\src\\main.c"; open_file (filename, GTK_TEXT_VIEW (data->textview)); gtk_window_set_title (GTK_WINDOW (data->window), filename); g_free (filename); } void on_interfacefile1_activate (GtkMenuItem *menuitem, AD data) // 開啟interface.c { char *filename; filename = "..\\src\\support.c"; open_file (filename, GTK_TEXT_VIEW (data->textview)); gtk_window_set_title (GTK_WINDOW (data->window), filename); g_free (filename); } void on_callbacksfile1_activate (GtkMenuItem *menuitem, AD data) // 開啟callbacks.c { char *filename; filename = "..\\src\\callbacks.c"; open_file (filename, GTK_TEXT_VIEW (data->textview)); gtk_window_set_title (GTK_WINDOW (data->window), filename); g_free (filename); } void on_quit1_activate (GtkMenuItem *menuitem, AD data) // 離開 { } void on_search_button_clicked (GtkWidget *search_button, AD data) // 搜尋按鈕的功能 { const gchar *text = ""; GtkTextBuffer *buffer; GtkTextIter iter; int result = 0 ; GtkWidget *search_dialog = create_search_dialog (data); gtk_widget_show (search_dialog); //result = gtk_dialog_run (GTK_DIALOG (search_dialog)); //gtk_widget_destroy (search_dialog); } void on_search_cancelbutton_clicked (GtkWidget *button, gpointer search_dialog) // 銷毀search dialog { gtk_widget_destroy (search_dialog); } void on_search_okbutton_clicked (GtkWidget *search_button, AD data) // 搜尋按鈕的功能,可連續搜尋 { const gchar *text = "e"; GtkTextBuffer *buffer; GtkTextIter iter; GtkTextMark *last_pos; int result = 0 ; gboolean found = FALSE; data->searchWord = gtk_entry_get_text (GTK_ENTRY (data->entry)); // 取得輸入欄的字串 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (data->textview)); last_pos = gtk_text_buffer_get_mark (buffer, "last_pos"); // 查看有無last_pos的mark if (last_pos == NULL) gtk_text_buffer_get_start_iter (buffer, &iter); // 若無,就從頭搜尋 else gtk_text_buffer_get_iter_at_mark (buffer, &iter, last_pos); // 若有,就從最後一個last_pos的後面開始搜尋 found = find (GTK_TEXT_VIEW (data->textview), data->searchWord, &iter); if (!found) gtk_text_buffer_delete_mark (buffer, last_pos); // 搜尋到最後可回到起點重新搜尋 } void on_next_button_clicked (GtkWidget *next_button, GdkEventKey *event, AD data) // 按下『F3』找下一個符合字串 // *因為無法把searchWord正確傳遞,所以無法正常使用此函式。 { const gchar *text; GtkTextBuffer *buffer; GtkTextMark *last_pos; GtkTextIter iter; if ( !strcmp( gdk_keyval_name( event->keyval ), "F3" ) ) { text = "e" ; // 預設搜尋e //text = data->searchWord; buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (data->textview)); last_pos = gtk_text_buffer_get_mark (buffer, "last_pos"); if (last_pos == NULL) gtk_text_buffer_get_start_iter (buffer, &iter); // 若無,就從頭搜尋 else gtk_text_buffer_get_iter_at_mark (buffer, &iter, last_pos); //find (GTK_TEXT_VIEW (data->textview), text, &iter); find (GTK_TEXT_VIEW (data->textview), text, &iter); } } void on_replace_button_clicked (GtkWidget *replace_button, AD data) // 搜尋按鈕的功能 { const gchar *text = ""; GtkTextBuffer *buffer; GtkTextIter iter; int result = 0 ; GtkWidget *replace_dialog = create_replace_dialog (data); gtk_widget_show (replace_dialog); //result = gtk_dialog_run (GTK_DIALOG (search_dialog)); //gtk_widget_destroy (search_dialog); } void on_replace_cancelbutton_clicked (GtkWidget *button, gpointer replace_dialog) // 銷毀replace dialog { gtk_widget_destroy (replace_dialog); } void on_replace_okbutton_clicked (GtkWidget *replace_button, AD data) // 取代按鈕的功能,可連續取代 { const gchar *text = "e"; GtkTextBuffer *buffer; GtkTextIter iter; GtkTextMark *last_pos; int result = 0 ; data->searchWord = gtk_entry_get_text (GTK_ENTRY (data->entry)); // 取得輸入欄的字串 data->replaceWord = gtk_entry_get_text (GTK_ENTRY (data->entry2)); // 取得輸入欄的字串 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (data->textview)); last_pos = gtk_text_buffer_get_mark (buffer, "last_pos"); // 查看有無last_pos的mark if (last_pos == NULL) gtk_text_buffer_get_start_iter (buffer, &iter); // 若無,就從頭搜尋 else gtk_text_buffer_get_iter_at_mark (buffer, &iter, last_pos); // 若有,就從最後一個last_pos的後面開始搜尋 replace (GTK_TEXT_VIEW (data->textview), data->searchWord, data->replaceWord, &iter); }
support.c:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
/* * DO NOT EDIT THIS FILE - it is generated by Glade. */ #ifdef HAVE_CONFIG_H #include <config.h> #endif #include <sys/types.h> #include <sys/stat.h> #ifdef HAVE_UNISTD_H #include <unistd.h> #endif #include <string.h> #include <stdio.h> #include <gtk/gtk.h> #include <glib.h> #include "support.h" gchar *utf8( const char *str ) // 解決無法顯示中文的窘境 { return g_locale_to_utf8( str, -1, NULL, NULL, NULL); } gchar *fromUtf8( const char *str ) { return g_locale_from_utf8 (str, -1, NULL, NULL, NULL); } void replace (GtkTextView *text_view, const gchar *text, const gchar *replaceText, GtkTextIter *iter) // 取代主函式 { GtkTextIter mstart, mend; GtkTextBuffer *buffer; gboolean found; GtkTextMark *last_pos; GtkClipboard *clipboard; buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view)); found = gtk_text_iter_forward_search (iter, text, 0, &mstart, &mend, NULL); if (found) { clipboard = gtk_clipboard_get( GDK_NONE ); // 取得反白選取的部份 gtk_text_buffer_cut_clipboard (buffer, clipboard, TRUE); // 剪下選取的反白字串 gtk_clipboard_set_text (clipboard, replaceText, -1); // 將剪貼簿修改為replaceText gtk_text_buffer_select_range (buffer, &mstart, &mend); last_pos = gtk_text_buffer_create_mark (buffer, "last_pos", &mend, FALSE); // 註記起來 之後才可往下搜尋 gtk_text_view_scroll_mark_onscreen(text_view, last_pos); // 移動捲軸 可追蹤目前搜尋的位置 gtk_text_buffer_paste_clipboard (buffer, clipboard, &mend, TRUE); // 將剪貼簿的內容貼到指定區域 } } gboolean find (GtkTextView *text_view, const gchar *text, GtkTextIter *iter) // 搜尋主函式 { GtkTextIter mstart, mend; GtkTextBuffer *buffer; gboolean found; GtkTextMark *last_pos; buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view)); found = gtk_text_iter_forward_search (iter, text, 0, &mstart, &mend, NULL); if (found) { gtk_text_buffer_select_range (buffer, &mstart, &mend); last_pos = gtk_text_buffer_create_mark (buffer, "last_pos", &mend, FALSE); // 註記起來 之後才可往下搜尋 gtk_text_view_scroll_mark_onscreen(text_view, last_pos); // 移動捲軸 可追蹤目前搜尋的位置 } return found ; } void open_file(gchar *filename, GtkTextView *textView) // 開啟文件檔並顯示在textview上 // 目前瓶頸:無法開啟UTF-16編碼的文件 { gchar *content, *content2, *ascii2utf8; gsize bytes, bytes_read, bytes_written; GError *error = NULL; GtkTextBuffer *buffer; buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW (textView)); g_file_get_contents (filename, &content, &bytes, &error); //g_file_set_contents ("TEST.txt", content, bytes, &error); //g_convert (content, -1, "UTF-16", "UTF-8", NULL, NULL, &error); ascii2utf8 = g_locale_to_utf8( content, -1, NULL, NULL, &error); if (error) // 若不能轉換,代表原本就是utf-8了 gtk_text_buffer_set_text (buffer, content, -1); // 用來開UTF-8文件 else gtk_text_buffer_set_text (buffer, ascii2utf8, -1); // 用來開ANSI文件 free (content); free (ascii2utf8); } void save_to_file(gchar *filename, GtkTextView *textView) // 將緩衝區的文字儲存到名為filename的檔案裡 { gchar *content; gsize bytes; GError *error = NULL; GtkTextBuffer *buffer; GtkTextIter start, end; buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (textView)); gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (buffer), &start, &end);/*取得開始和結束位置的Iter*/ content = gtk_text_buffer_get_text (GTK_TEXT_BUFFER (buffer), &start, &end, FALSE);/*取得整份文字資料*/ g_file_set_contents (filename, content, -1, &error); } // ================以下為本來有的============== GtkWidget* lookup_widget (GtkWidget *widget, const gchar *widget_name) { GtkWidget *parent, *found_widget; for (;;) { if (GTK_IS_MENU (widget)) parent = gtk_menu_get_attach_widget (GTK_MENU (widget)); else parent = widget->parent; if (!parent) parent = (GtkWidget*) g_object_get_data (G_OBJECT (widget), "GladeParentKey"); if (parent == NULL) break; widget = parent; } found_widget = (GtkWidget*) g_object_get_data (G_OBJECT (widget), widget_name); if (!found_widget) g_warning ("Widget not found: %s", widget_name); return found_widget; } static GList *pixmaps_directories = NULL; /* Use this function to set the directory containing installed pixmaps. */ void add_pixmap_directory (const gchar *directory) { pixmaps_directories = g_list_prepend (pixmaps_directories, g_strdup (directory)); } /* This is an internally used function to find pixmap files. */ static gchar* find_pixmap_file (const gchar *filename) { GList *elem; /* We step through each of the pixmaps directory to find it. */ elem = pixmaps_directories; while (elem) { gchar *pathname = g_strdup_printf ("%s%s%s", (gchar*)elem->data, G_DIR_SEPARATOR_S, filename); if (g_file_test (pathname, G_FILE_TEST_EXISTS)) return pathname; g_free (pathname); elem = elem->next; } return NULL; } /* This is an internally used function to create pixmaps. */ GtkWidget* create_pixmap (GtkWidget *widget, const gchar *filename) { gchar *pathname = NULL; GtkWidget *pixmap; if (!filename || !filename[0]) return gtk_image_new (); pathname = find_pixmap_file (filename); if (!pathname) { g_warning (_("Couldnt find pixmap file: %s"), filename); return gtk_image_new (); } pixmap = gtk_image_new_from_file (pathname); g_free (pathname); return pixmap; } /* This is an internally used function to create pixmaps. */ GdkPixbuf* create_pixbuf (const gchar *filename) { gchar *pathname = NULL; GdkPixbuf *pixbuf; GError *error = NULL; if (!filename || !filename[0]) return NULL; pathname = find_pixmap_file (filename); if (!pathname) { g_warning (_("Couldnt find pixmap file: %s"), filename); return NULL; } pixbuf = gdk_pixbuf_new_from_file (pathname, &error); if (!pixbuf) { fprintf (stderr, "Failed to load pixbuf file: %s: %s\n", pathname, error->message); g_error_free (error); } g_free (pathname); return pixbuf; } /* This is used to set ATK action descriptions. */ void glade_set_atk_action_description (AtkAction *action, const gchar *action_name, const gchar *description) { gint n_actions, i; n_actions = atk_action_get_n_actions (action); for (i = 0; i < n_actions; i++) { if (!strcmp (atk_action_get_name (action, i), action_name)) atk_action_set_description (action, i, description); } }
No response to “GTK實作之記事本 v1.0” ;
張貼留言